xref: /linux/sound/soc/sof/sof-client-probes.c (revision b4ada0618eed0fbd1b1630f73deb048c592b06a1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019-2022 Intel Corporation
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 // SOF client support:
8 //  Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
9 //  Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
10 //
11 
12 #include <linux/debugfs.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/string_helpers.h>
16 #include <linux/stddef.h>
17 
18 #include <sound/soc.h>
19 #include <sound/sof/header.h>
20 #include "sof-client.h"
21 #include "sof-client-probes.h"
22 
23 #define SOF_PROBES_SUSPEND_DELAY_MS 3000
24 /* only extraction supported for now */
25 #define SOF_PROBES_NUM_DAI_LINKS 1
26 
27 #define SOF_PROBES_INVALID_NODE_ID UINT_MAX
28 
29 static bool __read_mostly sof_probes_enabled;
30 module_param_named(enable, sof_probes_enabled, bool, 0444);
31 MODULE_PARM_DESC(enable, "Enable SOF probes support");
32 
33 static int sof_probes_compr_startup(struct snd_compr_stream *cstream,
34 				    struct snd_soc_dai *dai)
35 {
36 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
37 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
38 	struct sof_probes_priv *priv = cdev->data;
39 	const struct sof_probes_host_ops *ops = priv->host_ops;
40 	int ret;
41 
42 	if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
43 		return -ENODEV;
44 
45 	ret = sof_client_core_module_get(cdev);
46 	if (ret)
47 		return ret;
48 
49 	ret = ops->startup(cdev, cstream, dai, &priv->extractor_stream_tag);
50 	if (ret) {
51 		dev_err(dai->dev, "Failed to startup probe stream: %d\n", ret);
52 		priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
53 		sof_client_core_module_put(cdev);
54 	}
55 
56 	return ret;
57 }
58 
59 static int sof_probes_compr_shutdown(struct snd_compr_stream *cstream,
60 				     struct snd_soc_dai *dai)
61 {
62 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
63 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
64 	struct sof_probes_priv *priv = cdev->data;
65 	const struct sof_probes_host_ops *ops = priv->host_ops;
66 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
67 	struct sof_probe_point_desc *desc;
68 	size_t num_desc;
69 	int i, ret;
70 
71 	/* disconnect all probe points */
72 	ret = ipc->points_info(cdev, &desc, &num_desc);
73 	if (ret < 0) {
74 		dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
75 		goto exit;
76 	}
77 
78 	for (i = 0; i < num_desc; i++)
79 		ipc->points_remove(cdev, &desc[i].buffer_id, 1);
80 	kfree(desc);
81 
82 exit:
83 	ret = ipc->deinit(cdev);
84 	if (ret < 0)
85 		dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
86 
87 	priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
88 	snd_compr_free_pages(cstream);
89 
90 	ret = ops->shutdown(cdev, cstream, dai);
91 
92 	sof_client_core_module_put(cdev);
93 
94 	return ret;
95 }
96 
97 static int sof_probes_compr_set_params(struct snd_compr_stream *cstream,
98 				       struct snd_compr_params *params,
99 				       struct snd_soc_dai *dai)
100 {
101 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
102 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
103 	struct snd_compr_runtime *rtd = cstream->runtime;
104 	struct sof_probes_priv *priv = cdev->data;
105 	const struct sof_probes_host_ops *ops = priv->host_ops;
106 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
107 	int ret;
108 
109 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
110 	cstream->dma_buffer.dev.dev = sof_client_get_dma_dev(cdev);
111 	ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
112 	if (ret < 0)
113 		return ret;
114 
115 	ret = ops->set_params(cdev, cstream, params, dai);
116 	if (ret)
117 		return ret;
118 
119 	ret = ipc->init(cdev, priv->extractor_stream_tag, rtd->dma_bytes);
120 	if (ret < 0) {
121 		dev_err(dai->dev, "Failed to init probe: %d\n", ret);
122 		return ret;
123 	}
124 
125 	return 0;
126 }
127 
128 static int sof_probes_compr_trigger(struct snd_compr_stream *cstream, int cmd,
129 				    struct snd_soc_dai *dai)
130 {
131 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
132 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
133 	struct sof_probes_priv *priv = cdev->data;
134 	const struct sof_probes_host_ops *ops = priv->host_ops;
135 
136 	return ops->trigger(cdev, cstream, cmd, dai);
137 }
138 
139 static int sof_probes_compr_pointer(struct snd_compr_stream *cstream,
140 				    struct snd_compr_tstamp *tstamp,
141 				    struct snd_soc_dai *dai)
142 {
143 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
144 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
145 	struct sof_probes_priv *priv = cdev->data;
146 	const struct sof_probes_host_ops *ops = priv->host_ops;
147 
148 	return ops->pointer(cdev, cstream, tstamp, dai);
149 }
150 
151 static const struct snd_soc_cdai_ops sof_probes_compr_ops = {
152 	.startup = sof_probes_compr_startup,
153 	.shutdown = sof_probes_compr_shutdown,
154 	.set_params = sof_probes_compr_set_params,
155 	.trigger = sof_probes_compr_trigger,
156 	.pointer = sof_probes_compr_pointer,
157 };
158 
159 static int sof_probes_compr_copy(struct snd_soc_component *component,
160 				 struct snd_compr_stream *cstream,
161 				 char __user *buf, size_t count)
162 {
163 	struct snd_compr_runtime *rtd = cstream->runtime;
164 	unsigned int offset, n;
165 	void *ptr;
166 	int ret;
167 
168 	if (count > rtd->buffer_size)
169 		count = rtd->buffer_size;
170 
171 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
172 	ptr = rtd->dma_area + offset;
173 	n = rtd->buffer_size - offset;
174 
175 	if (count < n) {
176 		ret = copy_to_user(buf, ptr, count);
177 	} else {
178 		ret = copy_to_user(buf, ptr, n);
179 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
180 	}
181 
182 	if (ret)
183 		return count - ret;
184 	return count;
185 }
186 
187 static const struct snd_compress_ops sof_probes_compressed_ops = {
188 	.copy = sof_probes_compr_copy,
189 };
190 
191 static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
192 					  size_t count, loff_t *ppos)
193 {
194 	struct sof_client_dev *cdev = file->private_data;
195 	struct sof_probes_priv *priv = cdev->data;
196 	struct device *dev = &cdev->auxdev.dev;
197 	struct sof_probe_point_desc *desc;
198 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
199 	int remaining, offset;
200 	size_t num_desc;
201 	char *buf;
202 	int i, ret, err;
203 
204 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
205 		dev_warn(dev, "no extractor stream running\n");
206 		return -ENOENT;
207 	}
208 
209 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
210 	if (!buf)
211 		return -ENOMEM;
212 
213 	ret = pm_runtime_resume_and_get(dev);
214 	if (ret < 0 && ret != -EACCES) {
215 		dev_err_ratelimited(dev, "debugfs read failed to resume %d\n", ret);
216 		goto exit;
217 	}
218 
219 	ret = ipc->points_info(cdev, &desc, &num_desc);
220 	if (ret < 0)
221 		goto pm_error;
222 
223 	for (i = 0; i < num_desc; i++) {
224 		offset = strlen(buf);
225 		remaining = PAGE_SIZE - offset;
226 		ret = snprintf(buf + offset, remaining,
227 			       "Id: %#010x  Purpose: %u  Node id: %#x\n",
228 				desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
229 		if (ret < 0 || ret >= remaining) {
230 			/* truncate the output buffer at the last full line */
231 			buf[offset] = '\0';
232 			break;
233 		}
234 	}
235 
236 	ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
237 
238 	kfree(desc);
239 
240 pm_error:
241 	err = pm_runtime_put_autosuspend(dev);
242 	if (err < 0)
243 		dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
244 
245 exit:
246 	kfree(buf);
247 	return ret;
248 }
249 
250 static ssize_t
251 sof_probes_dfs_points_write(struct file *file, const char __user *from,
252 			    size_t count, loff_t *ppos)
253 {
254 	struct sof_client_dev *cdev = file->private_data;
255 	struct sof_probes_priv *priv = cdev->data;
256 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
257 	struct device *dev = &cdev->auxdev.dev;
258 	struct sof_probe_point_desc *desc;
259 	u32 num_elems, *array;
260 	size_t bytes;
261 	int ret, err;
262 
263 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
264 		dev_warn(dev, "no extractor stream running\n");
265 		return -ENOENT;
266 	}
267 
268 	ret = parse_int_array_user(from, count, (int **)&array);
269 	if (ret < 0)
270 		return ret;
271 
272 	num_elems = *array;
273 	bytes = sizeof(*array) * num_elems;
274 	if (bytes % sizeof(*desc)) {
275 		ret = -EINVAL;
276 		goto exit;
277 	}
278 
279 	desc = (struct sof_probe_point_desc *)&array[1];
280 
281 	ret = pm_runtime_resume_and_get(dev);
282 	if (ret < 0 && ret != -EACCES) {
283 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
284 		goto exit;
285 	}
286 
287 	ret = ipc->points_add(cdev, desc, bytes / sizeof(*desc));
288 	if (!ret)
289 		ret = count;
290 
291 	err = pm_runtime_put_autosuspend(dev);
292 	if (err < 0)
293 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
294 exit:
295 	kfree(array);
296 	return ret;
297 }
298 
299 static const struct file_operations sof_probes_points_fops = {
300 	.open = simple_open,
301 	.read = sof_probes_dfs_points_read,
302 	.write = sof_probes_dfs_points_write,
303 	.llseek = default_llseek,
304 
305 	.owner = THIS_MODULE,
306 };
307 
308 static ssize_t
309 sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
310 				   size_t count, loff_t *ppos)
311 {
312 	struct sof_client_dev *cdev = file->private_data;
313 	struct sof_probes_priv *priv = cdev->data;
314 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
315 	struct device *dev = &cdev->auxdev.dev;
316 	int ret, err;
317 	u32 *array;
318 
319 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
320 		dev_warn(dev, "no extractor stream running\n");
321 		return -ENOENT;
322 	}
323 
324 	ret = parse_int_array_user(from, count, (int **)&array);
325 	if (ret < 0)
326 		return ret;
327 
328 	ret = pm_runtime_resume_and_get(dev);
329 	if (ret < 0) {
330 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
331 		goto exit;
332 	}
333 
334 	ret = ipc->points_remove(cdev, &array[1], array[0]);
335 	if (!ret)
336 		ret = count;
337 
338 	err = pm_runtime_put_autosuspend(dev);
339 	if (err < 0)
340 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
341 exit:
342 	kfree(array);
343 	return ret;
344 }
345 
346 static const struct file_operations sof_probes_points_remove_fops = {
347 	.open = simple_open,
348 	.write = sof_probes_dfs_points_remove_write,
349 	.llseek = default_llseek,
350 
351 	.owner = THIS_MODULE,
352 };
353 
354 static const struct snd_soc_dai_ops sof_probes_dai_ops = {
355 	.compress_new = snd_soc_new_compress,
356 };
357 
358 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
359 {
360 	.name = "Probe Extraction CPU DAI",
361 	.ops  = &sof_probes_dai_ops,
362 	.cops = &sof_probes_compr_ops,
363 	.capture = {
364 		.stream_name = "Probe Extraction",
365 		.channels_min = 1,
366 		.channels_max = 8,
367 		.rates = SNDRV_PCM_RATE_48000,
368 		.rate_min = 48000,
369 		.rate_max = 48000,
370 	},
371 },
372 };
373 
374 static const struct snd_soc_component_driver sof_probes_component = {
375 	.name = "sof-probes-component",
376 	.compress_ops = &sof_probes_compressed_ops,
377 	.module_get_upon_open = 1,
378 	.legacy_dai_naming = 1,
379 };
380 
381 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
382 				   const struct auxiliary_device_id *id)
383 {
384 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
385 	struct dentry *dfsroot = sof_client_get_debugfs_root(cdev);
386 	struct device *dev = &auxdev->dev;
387 	struct snd_soc_dai_link_component platform_component[] = {
388 		{
389 			.name = dev_name(dev),
390 		}
391 	};
392 	struct snd_soc_card *card;
393 	struct sof_probes_priv *priv;
394 	struct snd_soc_dai_link_component *cpus;
395 	struct sof_probes_host_ops *ops;
396 	struct snd_soc_dai_link *links;
397 	int ret;
398 
399 	/* do not set up the probes support if it is not enabled */
400 	if (!sof_probes_enabled)
401 		return -ENXIO;
402 
403 	ops = dev_get_platdata(dev);
404 	if (!ops) {
405 		dev_err(dev, "missing platform data\n");
406 		return -ENODEV;
407 	}
408 	if (!ops->startup || !ops->shutdown || !ops->set_params || !ops->trigger ||
409 	    !ops->pointer) {
410 		dev_err(dev, "missing platform callback(s)\n");
411 		return -ENODEV;
412 	}
413 
414 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
415 	if (!priv)
416 		return -ENOMEM;
417 
418 	priv->host_ops = ops;
419 
420 	switch (sof_client_get_ipc_type(cdev)) {
421 #ifdef CONFIG_SND_SOC_SOF_IPC4
422 	case SOF_IPC_TYPE_4:
423 		priv->ipc_ops = &ipc4_probe_ops;
424 		break;
425 #endif
426 #ifdef CONFIG_SND_SOC_SOF_IPC3
427 	case SOF_IPC_TYPE_3:
428 		priv->ipc_ops = &ipc3_probe_ops;
429 		break;
430 #endif
431 	default:
432 		dev_err(dev, "Matching IPC ops not found.");
433 		return -ENODEV;
434 	}
435 
436 	cdev->data = priv;
437 
438 	/* register probes component driver and dai */
439 	ret = devm_snd_soc_register_component(dev, &sof_probes_component,
440 					      sof_probes_dai_drv,
441 					      ARRAY_SIZE(sof_probes_dai_drv));
442 	if (ret < 0) {
443 		dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
444 		return ret;
445 	}
446 
447 	/* set client data */
448 	priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
449 
450 	/* create read-write probes_points debugfs entry */
451 	priv->dfs_points = debugfs_create_file("probe_points", 0644, dfsroot,
452 					       cdev, &sof_probes_points_fops);
453 
454 	/* create read-write probe_points_remove debugfs entry */
455 	priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
456 						      dfsroot, cdev,
457 						      &sof_probes_points_remove_fops);
458 
459 	links = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*links), GFP_KERNEL);
460 	cpus = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*cpus), GFP_KERNEL);
461 	if (!links || !cpus) {
462 		debugfs_remove(priv->dfs_points);
463 		debugfs_remove(priv->dfs_points_remove);
464 		return -ENOMEM;
465 	}
466 
467 	/* extraction DAI link */
468 	links[0].name = "Compress Probe Capture";
469 	links[0].id = 0;
470 	links[0].cpus = &cpus[0];
471 	links[0].num_cpus = 1;
472 	links[0].cpus->dai_name = "Probe Extraction CPU DAI";
473 	links[0].codecs = &snd_soc_dummy_dlc;
474 	links[0].num_codecs = 1;
475 	links[0].platforms = platform_component;
476 	links[0].num_platforms = ARRAY_SIZE(platform_component);
477 	links[0].nonatomic = 1;
478 
479 	card = &priv->card;
480 
481 	card->dev = dev;
482 	card->name = "sof-probes";
483 	card->owner = THIS_MODULE;
484 	card->num_links = SOF_PROBES_NUM_DAI_LINKS;
485 	card->dai_link = links;
486 
487 	/* set idle_bias_off to prevent the core from resuming the card->dev */
488 	card->dapm.idle_bias_off = true;
489 
490 	snd_soc_card_set_drvdata(card, cdev);
491 
492 	ret = devm_snd_soc_register_card(dev, card);
493 	if (ret < 0) {
494 		debugfs_remove(priv->dfs_points);
495 		debugfs_remove(priv->dfs_points_remove);
496 		dev_err(dev, "Probes card register failed %d\n", ret);
497 		return ret;
498 	}
499 
500 	/* enable runtime PM */
501 	pm_runtime_set_autosuspend_delay(dev, SOF_PROBES_SUSPEND_DELAY_MS);
502 	pm_runtime_use_autosuspend(dev);
503 	pm_runtime_enable(dev);
504 	pm_runtime_mark_last_busy(dev);
505 	pm_runtime_idle(dev);
506 
507 	return 0;
508 }
509 
510 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
511 {
512 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
513 	struct sof_probes_priv *priv = cdev->data;
514 
515 	if (!sof_probes_enabled)
516 		return;
517 
518 	pm_runtime_disable(&auxdev->dev);
519 	debugfs_remove(priv->dfs_points);
520 	debugfs_remove(priv->dfs_points_remove);
521 }
522 
523 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
524 	{ .name = "snd_sof.hda-probes", },
525 	{ .name = "snd_sof.acp-probes", },
526 	{},
527 };
528 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
529 
530 /* driver name will be set based on KBUILD_MODNAME */
531 static struct auxiliary_driver sof_probes_client_drv = {
532 	.probe = sof_probes_client_probe,
533 	.remove = sof_probes_client_remove,
534 
535 	.id_table = sof_probes_client_id_table,
536 };
537 
538 module_auxiliary_driver(sof_probes_client_drv);
539 
540 MODULE_LICENSE("GPL v2");
541 MODULE_DESCRIPTION("SOF Probes Client Driver");
542 MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT");
543