xref: /openbmc/linux/sound/soc/sof/ipc4-control.c (revision 3ad7bf5e)
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 
sof_ipc4_set_get_kcontrol_data(struct snd_sof_control * scontrol,bool set,bool lock)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
sof_ipc4_set_volume_data(struct snd_sof_dev * sdev,struct snd_sof_widget * swidget,struct snd_sof_control * scontrol,bool lock)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 
sof_ipc4_volume_put(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)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 
sof_ipc4_volume_get(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)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 
sof_ipc4_set_get_bytes_data(struct snd_sof_dev * sdev,struct snd_sof_control * scontrol,bool set,bool lock)204 static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
205 				       struct snd_sof_control *scontrol,
206 				       bool set, bool lock)
207 {
208 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
209 	struct sof_abi_hdr *data = cdata->data;
210 	struct sof_ipc4_msg *msg = &cdata->msg;
211 	int ret = 0;
212 
213 	/* Send the new data to the firmware only if it is powered up */
214 	if (set && !pm_runtime_active(sdev->dev))
215 		return 0;
216 
217 	msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
218 
219 	msg->data_ptr = data->data;
220 	msg->data_size = data->size;
221 
222 	ret = sof_ipc4_set_get_kcontrol_data(scontrol, set, lock);
223 	if (ret < 0)
224 		dev_err(sdev->dev, "Failed to %s for %s\n",
225 			set ? "set bytes update" : "get bytes",
226 			scontrol->name);
227 
228 	msg->data_ptr = NULL;
229 	msg->data_size = 0;
230 
231 	return ret;
232 }
233 
sof_ipc4_bytes_put(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)234 static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol,
235 			      struct snd_ctl_elem_value *ucontrol)
236 {
237 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
238 	struct snd_soc_component *scomp = scontrol->scomp;
239 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
240 	struct sof_abi_hdr *data = cdata->data;
241 	size_t size;
242 
243 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
244 		dev_err_ratelimited(scomp->dev,
245 				    "data max %zu exceeds ucontrol data array size\n",
246 				    scontrol->max_size);
247 		return -EINVAL;
248 	}
249 
250 	/* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
251 	if (data->size > scontrol->max_size - sizeof(*data)) {
252 		dev_err_ratelimited(scomp->dev,
253 				    "data size too big %u bytes max is %zu\n",
254 				    data->size, scontrol->max_size - sizeof(*data));
255 		return -EINVAL;
256 	}
257 
258 	size = data->size + sizeof(*data);
259 
260 	/* copy from kcontrol */
261 	memcpy(data, ucontrol->value.bytes.data, size);
262 
263 	sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
264 
265 	return 0;
266 }
267 
sof_ipc4_bytes_get(struct snd_sof_control * scontrol,struct snd_ctl_elem_value * ucontrol)268 static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol,
269 			      struct snd_ctl_elem_value *ucontrol)
270 {
271 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
272 	struct snd_soc_component *scomp = scontrol->scomp;
273 	struct sof_abi_hdr *data = cdata->data;
274 	size_t size;
275 
276 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
277 		dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
278 				    scontrol->max_size);
279 		return -EINVAL;
280 	}
281 
282 	if (data->size > scontrol->max_size - sizeof(*data)) {
283 		dev_err_ratelimited(scomp->dev,
284 				    "%u bytes of control data is invalid, max is %zu\n",
285 				    data->size, scontrol->max_size - sizeof(*data));
286 		return -EINVAL;
287 	}
288 
289 	size = data->size + sizeof(*data);
290 
291 	/* copy back to kcontrol */
292 	memcpy(ucontrol->value.bytes.data, data, size);
293 
294 	return 0;
295 }
296 
sof_ipc4_bytes_ext_put(struct snd_sof_control * scontrol,const unsigned int __user * binary_data,unsigned int size)297 static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
298 				  const unsigned int __user *binary_data,
299 				  unsigned int size)
300 {
301 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
302 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
303 	struct snd_soc_component *scomp = scontrol->scomp;
304 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
305 	struct sof_abi_hdr *data = cdata->data;
306 	struct sof_abi_hdr abi_hdr;
307 	struct snd_ctl_tlv header;
308 
309 	/*
310 	 * The beginning of bytes data contains a header from where
311 	 * the length (as bytes) is needed to know the correct copy
312 	 * length of data from tlvd->tlv.
313 	 */
314 	if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
315 		return -EFAULT;
316 
317 	/* make sure TLV info is consistent */
318 	if (header.length + sizeof(struct snd_ctl_tlv) > size) {
319 		dev_err_ratelimited(scomp->dev,
320 				    "Inconsistent TLV, data %d + header %zu > %d\n",
321 				    header.length, sizeof(struct snd_ctl_tlv), size);
322 		return -EINVAL;
323 	}
324 
325 	/* be->max is coming from topology */
326 	if (header.length > scontrol->max_size) {
327 		dev_err_ratelimited(scomp->dev,
328 				    "Bytes data size %d exceeds max %zu\n",
329 				    header.length, scontrol->max_size);
330 		return -EINVAL;
331 	}
332 
333 	/* Verify the ABI header first */
334 	if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr)))
335 		return -EFAULT;
336 
337 	if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) {
338 		dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n",
339 				    abi_hdr.magic);
340 		return -EINVAL;
341 	}
342 
343 	if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) {
344 		dev_err_ratelimited(scomp->dev,
345 				    "%u bytes of control data is invalid, max is %zu\n",
346 				    abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
347 		return -EINVAL;
348 	}
349 
350 	if (!scontrol->old_ipc_control_data) {
351 		/* Create a backup of the current, valid bytes control */
352 		scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
353 							 scontrol->max_size, GFP_KERNEL);
354 		if (!scontrol->old_ipc_control_data)
355 			return -ENOMEM;
356 	}
357 
358 	/* Copy the whole binary data which includes the ABI header and the payload */
359 	if (copy_from_user(data, tlvd->tlv, header.length)) {
360 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
361 		       scontrol->max_size);
362 		kfree(scontrol->old_ipc_control_data);
363 		scontrol->old_ipc_control_data = NULL;
364 		return -EFAULT;
365 	}
366 
367 	return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
368 }
369 
_sof_ipc4_bytes_ext_get(struct snd_sof_control * scontrol,const unsigned int __user * binary_data,unsigned int size,bool from_dsp)370 static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
371 				   const unsigned int __user *binary_data,
372 				   unsigned int size, bool from_dsp)
373 {
374 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
375 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
376 	struct snd_soc_component *scomp = scontrol->scomp;
377 	struct sof_abi_hdr *data = cdata->data;
378 	struct snd_ctl_tlv header;
379 	size_t data_size;
380 
381 	/*
382 	 * Decrement the limit by ext bytes header size to ensure the user space
383 	 * buffer is not exceeded.
384 	 */
385 	if (size < sizeof(struct snd_ctl_tlv))
386 		return -ENOSPC;
387 
388 	size -= sizeof(struct snd_ctl_tlv);
389 
390 	/* get all the component data from DSP */
391 	if (from_dsp) {
392 		struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
393 		int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true);
394 
395 		if (ret < 0)
396 			return ret;
397 
398 		/* Set the ABI magic (if the control is not initialized) */
399 		data->magic = SOF_IPC4_ABI_MAGIC;
400 	}
401 
402 	if (data->size > scontrol->max_size - sizeof(*data)) {
403 		dev_err_ratelimited(scomp->dev,
404 				    "%u bytes of control data is invalid, max is %zu\n",
405 				    data->size, scontrol->max_size - sizeof(*data));
406 		return -EINVAL;
407 	}
408 
409 	data_size = data->size + sizeof(struct sof_abi_hdr);
410 
411 	/* make sure we don't exceed size provided by user space for data */
412 	if (data_size > size)
413 		return -ENOSPC;
414 
415 	header.numid = scontrol->comp_id;
416 	header.length = data_size;
417 
418 	if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
419 		return -EFAULT;
420 
421 	if (copy_to_user(tlvd->tlv, data, data_size))
422 		return -EFAULT;
423 
424 	return 0;
425 }
426 
sof_ipc4_bytes_ext_get(struct snd_sof_control * scontrol,const unsigned int __user * binary_data,unsigned int size)427 static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
428 				  const unsigned int __user *binary_data,
429 				  unsigned int size)
430 {
431 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false);
432 }
433 
sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control * scontrol,const unsigned int __user * binary_data,unsigned int size)434 static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
435 					   const unsigned int __user *binary_data,
436 					   unsigned int size)
437 {
438 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true);
439 }
440 
441 /* set up all controls for the widget */
sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev * sdev,struct snd_sof_widget * swidget)442 static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
443 {
444 	struct snd_sof_control *scontrol;
445 	int ret = 0;
446 
447 	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
448 		if (scontrol->comp_id == swidget->comp_id) {
449 			switch (scontrol->info_type) {
450 			case SND_SOC_TPLG_CTL_VOLSW:
451 			case SND_SOC_TPLG_CTL_VOLSW_SX:
452 			case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
453 				ret = sof_ipc4_set_volume_data(sdev, swidget,
454 							       scontrol, false);
455 				break;
456 			case SND_SOC_TPLG_CTL_BYTES:
457 				ret = sof_ipc4_set_get_bytes_data(sdev, scontrol,
458 								  true, false);
459 				break;
460 			default:
461 				break;
462 			}
463 
464 			if (ret < 0) {
465 				dev_err(sdev->dev,
466 					"kcontrol %d set up failed for widget %s\n",
467 					scontrol->comp_id, swidget->widget->name);
468 				return ret;
469 			}
470 		}
471 	}
472 
473 	return 0;
474 }
475 
476 static int
sof_ipc4_set_up_volume_table(struct snd_sof_control * scontrol,int tlv[SOF_TLV_ITEMS],int size)477 sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
478 {
479 	int i;
480 
481 	/* init the volume table */
482 	scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
483 	if (!scontrol->volume_table)
484 		return -ENOMEM;
485 
486 	/* populate the volume table */
487 	for (i = 0; i < size ; i++) {
488 		u32 val = vol_compute_gain(i, tlv);
489 		u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
490 
491 		scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
492 						SOF_IPC4_VOL_ZERO_DB : q31val;
493 	}
494 
495 	return 0;
496 }
497 
498 const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
499 	.volume_put = sof_ipc4_volume_put,
500 	.volume_get = sof_ipc4_volume_get,
501 	.bytes_put = sof_ipc4_bytes_put,
502 	.bytes_get = sof_ipc4_bytes_get,
503 	.bytes_ext_put = sof_ipc4_bytes_ext_put,
504 	.bytes_ext_get = sof_ipc4_bytes_ext_get,
505 	.bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get,
506 	.widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
507 	.set_up_volume_table = sof_ipc4_set_up_volume_table,
508 };
509