xref: /openbmc/linux/sound/soc/sof/ipc4-control.c (revision 3facc0417d3d7b3ba5822e74155bcb1267ce62c1)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2022 Intel Corporation. All rights reserved.
7 //
8 //
9 
10 #include "sof-priv.h"
11 #include "sof-audio.h"
12 #include "ipc4-priv.h"
13 #include "ipc4-topology.h"
14 
15 static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
16 					  bool set, bool lock)
17 {
18 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
19 	struct snd_soc_component *scomp = scontrol->scomp;
20 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
21 	const struct sof_ipc_ops *iops = sdev->ipc->ops;
22 	struct sof_ipc4_msg *msg = &cdata->msg;
23 	struct snd_sof_widget *swidget;
24 	bool widget_found = false;
25 	int ret = 0;
26 
27 	/* find widget associated with the control */
28 	list_for_each_entry(swidget, &sdev->widget_list, list) {
29 		if (swidget->comp_id == scontrol->comp_id) {
30 			widget_found = true;
31 			break;
32 		}
33 	}
34 
35 	if (!widget_found) {
36 		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
37 		return -ENOENT;
38 	}
39 
40 	if (lock)
41 		mutex_lock(&swidget->setup_mutex);
42 	else
43 		lockdep_assert_held(&swidget->setup_mutex);
44 
45 	/*
46 	 * Volatile controls should always be part of static pipelines and the
47 	 * widget use_count would always be > 0 in this case. For the others,
48 	 * just return the cached value if the widget is not set up.
49 	 */
50 	if (!swidget->use_count)
51 		goto unlock;
52 
53 	msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
54 	msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
55 
56 	ret = iops->set_get_data(sdev, msg, msg->data_size, set);
57 	if (!set)
58 		goto unlock;
59 
60 	/* It is a set-data operation, and we have a valid backup that we can restore */
61 	if (ret < 0) {
62 		if (!scontrol->old_ipc_control_data)
63 			goto unlock;
64 		/*
65 		 * Current ipc_control_data is not valid, we use the last known good
66 		 * configuration
67 		 */
68 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
69 		       scontrol->max_size);
70 		kfree(scontrol->old_ipc_control_data);
71 		scontrol->old_ipc_control_data = NULL;
72 		/* Send the last known good configuration to firmware */
73 		ret = iops->set_get_data(sdev, msg, msg->data_size, set);
74 		if (ret < 0)
75 			goto unlock;
76 	}
77 
78 unlock:
79 	if (lock)
80 		mutex_unlock(&swidget->setup_mutex);
81 
82 	return ret;
83 }
84 
85 static int
86 sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
87 			 struct snd_sof_control *scontrol, bool lock)
88 {
89 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
90 	struct sof_ipc4_gain *gain = swidget->private;
91 	struct sof_ipc4_msg *msg = &cdata->msg;
92 	struct sof_ipc4_gain_params params;
93 	bool all_channels_equal = true;
94 	u32 value;
95 	int ret, i;
96 
97 	/* check if all channel values are equal */
98 	value = cdata->chanv[0].value;
99 	for (i = 1; i < scontrol->num_channels; i++) {
100 		if (cdata->chanv[i].value != value) {
101 			all_channels_equal = false;
102 			break;
103 		}
104 	}
105 
106 	/*
107 	 * notify DSP with a single IPC message if all channel values are equal. Otherwise send
108 	 * a separate IPC for each channel.
109 	 */
110 	for (i = 0; i < scontrol->num_channels; i++) {
111 		if (all_channels_equal) {
112 			params.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;
113 			params.init_val = cdata->chanv[0].value;
114 		} else {
115 			params.channels = cdata->chanv[i].channel;
116 			params.init_val = cdata->chanv[i].value;
117 		}
118 
119 		/* set curve type and duration from topology */
120 		params.curve_duration_l = gain->data.params.curve_duration_l;
121 		params.curve_duration_h = gain->data.params.curve_duration_h;
122 		params.curve_type = gain->data.params.curve_type;
123 
124 		msg->data_ptr = &params;
125 		msg->data_size = sizeof(params);
126 
127 		ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
128 		msg->data_ptr = NULL;
129 		msg->data_size = 0;
130 		if (ret < 0) {
131 			dev_err(sdev->dev, "Failed to set volume update for %s\n",
132 				scontrol->name);
133 			return ret;
134 		}
135 
136 		if (all_channels_equal)
137 			break;
138 	}
139 
140 	return 0;
141 }
142 
143 static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,
144 				struct snd_ctl_elem_value *ucontrol)
145 {
146 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
147 	struct snd_soc_component *scomp = scontrol->scomp;
148 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
149 	unsigned int channels = scontrol->num_channels;
150 	struct snd_sof_widget *swidget;
151 	bool widget_found = false;
152 	bool change = false;
153 	unsigned int i;
154 	int ret;
155 
156 	/* update each channel */
157 	for (i = 0; i < channels; i++) {
158 		u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
159 					 scontrol->volume_table, scontrol->max + 1);
160 
161 		change = change || (value != cdata->chanv[i].value);
162 		cdata->chanv[i].channel = i;
163 		cdata->chanv[i].value = value;
164 	}
165 
166 	if (!pm_runtime_active(scomp->dev))
167 		return change;
168 
169 	/* find widget associated with the control */
170 	list_for_each_entry(swidget, &sdev->widget_list, list) {
171 		if (swidget->comp_id == scontrol->comp_id) {
172 			widget_found = true;
173 			break;
174 		}
175 	}
176 
177 	if (!widget_found) {
178 		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
179 		return false;
180 	}
181 
182 	ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, true);
183 	if (ret < 0)
184 		return false;
185 
186 	return change;
187 }
188 
189 static int sof_ipc4_volume_get(struct snd_sof_control *scontrol,
190 			       struct snd_ctl_elem_value *ucontrol)
191 {
192 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
193 	unsigned int channels = scontrol->num_channels;
194 	unsigned int i;
195 
196 	for (i = 0; i < channels; i++)
197 		ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
198 								scontrol->volume_table,
199 								scontrol->max + 1);
200 
201 	return 0;
202 }
203 
204 static int
205 sof_ipc4_set_generic_control_data(struct snd_sof_dev *sdev,
206 				  struct snd_sof_widget *swidget,
207 				  struct snd_sof_control *scontrol, bool lock)
208 {
209 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
210 	struct sof_ipc4_control_msg_payload *data;
211 	struct sof_ipc4_msg *msg = &cdata->msg;
212 	size_t data_size;
213 	unsigned int i;
214 	int ret;
215 
216 	data_size = struct_size(data, chanv, scontrol->num_channels);
217 	data = kzalloc(data_size, GFP_KERNEL);
218 	if (!data)
219 		return -ENOMEM;
220 
221 	data->id = cdata->index;
222 	data->num_elems = scontrol->num_channels;
223 	for (i = 0; i < scontrol->num_channels; i++) {
224 		data->chanv[i].channel = cdata->chanv[i].channel;
225 		data->chanv[i].value = cdata->chanv[i].value;
226 	}
227 
228 	msg->data_ptr = data;
229 	msg->data_size = data_size;
230 
231 	ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
232 	msg->data_ptr = NULL;
233 	msg->data_size = 0;
234 	if (ret < 0)
235 		dev_err(sdev->dev, "Failed to set control update for %s\n",
236 			scontrol->name);
237 
238 	kfree(data);
239 
240 	return ret;
241 }
242 
243 static bool sof_ipc4_switch_put(struct snd_sof_control *scontrol,
244 				struct snd_ctl_elem_value *ucontrol)
245 {
246 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
247 	struct snd_soc_component *scomp = scontrol->scomp;
248 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
249 	struct snd_sof_widget *swidget;
250 	bool widget_found = false;
251 	bool change = false;
252 	unsigned int i;
253 	u32 value;
254 	int ret;
255 
256 	/* update each channel */
257 	for (i = 0; i < scontrol->num_channels; i++) {
258 		value = ucontrol->value.integer.value[i];
259 		change = change || (value != cdata->chanv[i].value);
260 		cdata->chanv[i].channel = i;
261 		cdata->chanv[i].value = value;
262 	}
263 
264 	if (!pm_runtime_active(scomp->dev))
265 		return change;
266 
267 	/* find widget associated with the control */
268 	list_for_each_entry(swidget, &sdev->widget_list, list) {
269 		if (swidget->comp_id == scontrol->comp_id) {
270 			widget_found = true;
271 			break;
272 		}
273 	}
274 
275 	if (!widget_found) {
276 		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
277 		return false;
278 	}
279 
280 	ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);
281 	if (ret < 0)
282 		return false;
283 
284 	return change;
285 }
286 
287 static int sof_ipc4_switch_get(struct snd_sof_control *scontrol,
288 			       struct snd_ctl_elem_value *ucontrol)
289 {
290 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
291 	unsigned int i;
292 
293 	/* read back each channel */
294 	for (i = 0; i < scontrol->num_channels; i++)
295 		ucontrol->value.integer.value[i] = cdata->chanv[i].value;
296 
297 	return 0;
298 }
299 
300 static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
301 				       struct snd_sof_control *scontrol,
302 				       bool set, bool lock)
303 {
304 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
305 	struct sof_abi_hdr *data = cdata->data;
306 	struct sof_ipc4_msg *msg = &cdata->msg;
307 	int ret = 0;
308 
309 	/* Send the new data to the firmware only if it is powered up */
310 	if (set && !pm_runtime_active(sdev->dev))
311 		return 0;
312 
313 	msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
314 
315 	msg->data_ptr = data->data;
316 	msg->data_size = data->size;
317 
318 	ret = sof_ipc4_set_get_kcontrol_data(scontrol, set, lock);
319 	if (ret < 0)
320 		dev_err(sdev->dev, "Failed to %s for %s\n",
321 			set ? "set bytes update" : "get bytes",
322 			scontrol->name);
323 
324 	msg->data_ptr = NULL;
325 	msg->data_size = 0;
326 
327 	return ret;
328 }
329 
330 static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol,
331 			      struct snd_ctl_elem_value *ucontrol)
332 {
333 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
334 	struct snd_soc_component *scomp = scontrol->scomp;
335 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
336 	struct sof_abi_hdr *data = cdata->data;
337 	size_t size;
338 
339 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
340 		dev_err_ratelimited(scomp->dev,
341 				    "data max %zu exceeds ucontrol data array size\n",
342 				    scontrol->max_size);
343 		return -EINVAL;
344 	}
345 
346 	/* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
347 	if (data->size > scontrol->max_size - sizeof(*data)) {
348 		dev_err_ratelimited(scomp->dev,
349 				    "data size too big %u bytes max is %zu\n",
350 				    data->size, scontrol->max_size - sizeof(*data));
351 		return -EINVAL;
352 	}
353 
354 	size = data->size + sizeof(*data);
355 
356 	/* copy from kcontrol */
357 	memcpy(data, ucontrol->value.bytes.data, size);
358 
359 	sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
360 
361 	return 0;
362 }
363 
364 static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol,
365 			      struct snd_ctl_elem_value *ucontrol)
366 {
367 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
368 	struct snd_soc_component *scomp = scontrol->scomp;
369 	struct sof_abi_hdr *data = cdata->data;
370 	size_t size;
371 
372 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
373 		dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
374 				    scontrol->max_size);
375 		return -EINVAL;
376 	}
377 
378 	if (data->size > scontrol->max_size - sizeof(*data)) {
379 		dev_err_ratelimited(scomp->dev,
380 				    "%u bytes of control data is invalid, max is %zu\n",
381 				    data->size, scontrol->max_size - sizeof(*data));
382 		return -EINVAL;
383 	}
384 
385 	size = data->size + sizeof(*data);
386 
387 	/* copy back to kcontrol */
388 	memcpy(ucontrol->value.bytes.data, data, size);
389 
390 	return 0;
391 }
392 
393 static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
394 				  const unsigned int __user *binary_data,
395 				  unsigned int size)
396 {
397 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
398 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
399 	struct snd_soc_component *scomp = scontrol->scomp;
400 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
401 	struct sof_abi_hdr *data = cdata->data;
402 	struct sof_abi_hdr abi_hdr;
403 	struct snd_ctl_tlv header;
404 
405 	/*
406 	 * The beginning of bytes data contains a header from where
407 	 * the length (as bytes) is needed to know the correct copy
408 	 * length of data from tlvd->tlv.
409 	 */
410 	if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
411 		return -EFAULT;
412 
413 	/* make sure TLV info is consistent */
414 	if (header.length + sizeof(struct snd_ctl_tlv) > size) {
415 		dev_err_ratelimited(scomp->dev,
416 				    "Inconsistent TLV, data %d + header %zu > %d\n",
417 				    header.length, sizeof(struct snd_ctl_tlv), size);
418 		return -EINVAL;
419 	}
420 
421 	/* be->max is coming from topology */
422 	if (header.length > scontrol->max_size) {
423 		dev_err_ratelimited(scomp->dev,
424 				    "Bytes data size %d exceeds max %zu\n",
425 				    header.length, scontrol->max_size);
426 		return -EINVAL;
427 	}
428 
429 	/* Verify the ABI header first */
430 	if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr)))
431 		return -EFAULT;
432 
433 	if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) {
434 		dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n",
435 				    abi_hdr.magic);
436 		return -EINVAL;
437 	}
438 
439 	if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) {
440 		dev_err_ratelimited(scomp->dev,
441 				    "%u bytes of control data is invalid, max is %zu\n",
442 				    abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
443 		return -EINVAL;
444 	}
445 
446 	if (!scontrol->old_ipc_control_data) {
447 		/* Create a backup of the current, valid bytes control */
448 		scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
449 							 scontrol->max_size, GFP_KERNEL);
450 		if (!scontrol->old_ipc_control_data)
451 			return -ENOMEM;
452 	}
453 
454 	/* Copy the whole binary data which includes the ABI header and the payload */
455 	if (copy_from_user(data, tlvd->tlv, header.length)) {
456 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
457 		       scontrol->max_size);
458 		kfree(scontrol->old_ipc_control_data);
459 		scontrol->old_ipc_control_data = NULL;
460 		return -EFAULT;
461 	}
462 
463 	return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
464 }
465 
466 static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
467 				   const unsigned int __user *binary_data,
468 				   unsigned int size, bool from_dsp)
469 {
470 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
471 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
472 	struct snd_soc_component *scomp = scontrol->scomp;
473 	struct sof_abi_hdr *data = cdata->data;
474 	struct snd_ctl_tlv header;
475 	size_t data_size;
476 
477 	/*
478 	 * Decrement the limit by ext bytes header size to ensure the user space
479 	 * buffer is not exceeded.
480 	 */
481 	if (size < sizeof(struct snd_ctl_tlv))
482 		return -ENOSPC;
483 
484 	size -= sizeof(struct snd_ctl_tlv);
485 
486 	/* get all the component data from DSP */
487 	if (from_dsp) {
488 		struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
489 		int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true);
490 
491 		if (ret < 0)
492 			return ret;
493 
494 		/* Set the ABI magic (if the control is not initialized) */
495 		data->magic = SOF_IPC4_ABI_MAGIC;
496 	}
497 
498 	if (data->size > scontrol->max_size - sizeof(*data)) {
499 		dev_err_ratelimited(scomp->dev,
500 				    "%u bytes of control data is invalid, max is %zu\n",
501 				    data->size, scontrol->max_size - sizeof(*data));
502 		return -EINVAL;
503 	}
504 
505 	data_size = data->size + sizeof(struct sof_abi_hdr);
506 
507 	/* make sure we don't exceed size provided by user space for data */
508 	if (data_size > size)
509 		return -ENOSPC;
510 
511 	header.numid = scontrol->comp_id;
512 	header.length = data_size;
513 
514 	if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
515 		return -EFAULT;
516 
517 	if (copy_to_user(tlvd->tlv, data, data_size))
518 		return -EFAULT;
519 
520 	return 0;
521 }
522 
523 static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
524 				  const unsigned int __user *binary_data,
525 				  unsigned int size)
526 {
527 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false);
528 }
529 
530 static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
531 					   const unsigned int __user *binary_data,
532 					   unsigned int size)
533 {
534 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true);
535 }
536 
537 static int
538 sof_ipc4_volsw_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
539 		     struct snd_sof_control *scontrol)
540 {
541 	if (scontrol->max == 1)
542 		return sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, false);
543 
544 	return sof_ipc4_set_volume_data(sdev, swidget, scontrol, false);
545 }
546 
547 /* set up all controls for the widget */
548 static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
549 {
550 	struct snd_sof_control *scontrol;
551 	int ret = 0;
552 
553 	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
554 		if (scontrol->comp_id == swidget->comp_id) {
555 			switch (scontrol->info_type) {
556 			case SND_SOC_TPLG_CTL_VOLSW:
557 			case SND_SOC_TPLG_CTL_VOLSW_SX:
558 			case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
559 				ret = sof_ipc4_volsw_setup(sdev, swidget, scontrol);
560 				break;
561 			case SND_SOC_TPLG_CTL_BYTES:
562 				ret = sof_ipc4_set_get_bytes_data(sdev, scontrol,
563 								  true, false);
564 				break;
565 			default:
566 				break;
567 			}
568 
569 			if (ret < 0) {
570 				dev_err(sdev->dev,
571 					"kcontrol %d set up failed for widget %s\n",
572 					scontrol->comp_id, swidget->widget->name);
573 				return ret;
574 			}
575 		}
576 	}
577 
578 	return 0;
579 }
580 
581 static int
582 sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
583 {
584 	int i;
585 
586 	/* init the volume table */
587 	scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
588 	if (!scontrol->volume_table)
589 		return -ENOMEM;
590 
591 	/* populate the volume table */
592 	for (i = 0; i < size ; i++) {
593 		u32 val = vol_compute_gain(i, tlv);
594 		u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
595 
596 		scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
597 						SOF_IPC4_VOL_ZERO_DB : q31val;
598 	}
599 
600 	return 0;
601 }
602 
603 const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
604 	.volume_put = sof_ipc4_volume_put,
605 	.volume_get = sof_ipc4_volume_get,
606 	.switch_put = sof_ipc4_switch_put,
607 	.switch_get = sof_ipc4_switch_get,
608 	.bytes_put = sof_ipc4_bytes_put,
609 	.bytes_get = sof_ipc4_bytes_get,
610 	.bytes_ext_put = sof_ipc4_bytes_ext_put,
611 	.bytes_ext_get = sof_ipc4_bytes_ext_get,
612 	.bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get,
613 	.widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
614 	.set_up_volume_table = sof_ipc4_set_up_volume_table,
615 };
616