xref: /linux/sound/soc/sof/sof-client-probes.c (revision d59fec29b131f30b27343d54bdf1071ee98eda8e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
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 exit;
222 
223 	pm_runtime_mark_last_busy(dev);
224 	err = pm_runtime_put_autosuspend(dev);
225 	if (err < 0)
226 		dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
227 
228 	for (i = 0; i < num_desc; i++) {
229 		offset = strlen(buf);
230 		remaining = PAGE_SIZE - offset;
231 		ret = snprintf(buf + offset, remaining,
232 			       "Id: %#010x  Purpose: %u  Node id: %#x\n",
233 				desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
234 		if (ret < 0 || ret >= remaining) {
235 			/* truncate the output buffer at the last full line */
236 			buf[offset] = '\0';
237 			break;
238 		}
239 	}
240 
241 	ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
242 
243 	kfree(desc);
244 exit:
245 	kfree(buf);
246 	return ret;
247 }
248 
249 static ssize_t
250 sof_probes_dfs_points_write(struct file *file, const char __user *from,
251 			    size_t count, loff_t *ppos)
252 {
253 	struct sof_client_dev *cdev = file->private_data;
254 	struct sof_probes_priv *priv = cdev->data;
255 	const struct sof_probes_ipc_ops *ipc = priv->ipc_ops;
256 	struct device *dev = &cdev->auxdev.dev;
257 	struct sof_probe_point_desc *desc;
258 	u32 num_elems, *array;
259 	size_t bytes;
260 	int ret, err;
261 
262 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
263 		dev_warn(dev, "no extractor stream running\n");
264 		return -ENOENT;
265 	}
266 
267 	ret = parse_int_array_user(from, count, (int **)&array);
268 	if (ret < 0)
269 		return ret;
270 
271 	num_elems = *array;
272 	bytes = sizeof(*array) * num_elems;
273 	if (bytes % sizeof(*desc)) {
274 		ret = -EINVAL;
275 		goto exit;
276 	}
277 
278 	desc = (struct sof_probe_point_desc *)&array[1];
279 
280 	ret = pm_runtime_resume_and_get(dev);
281 	if (ret < 0 && ret != -EACCES) {
282 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
283 		goto exit;
284 	}
285 
286 	ret = ipc->points_add(cdev, desc, bytes / sizeof(*desc));
287 	if (!ret)
288 		ret = count;
289 
290 	pm_runtime_mark_last_busy(dev);
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 	pm_runtime_mark_last_busy(dev);
339 	err = pm_runtime_put_autosuspend(dev);
340 	if (err < 0)
341 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
342 exit:
343 	kfree(array);
344 	return ret;
345 }
346 
347 static const struct file_operations sof_probes_points_remove_fops = {
348 	.open = simple_open,
349 	.write = sof_probes_dfs_points_remove_write,
350 	.llseek = default_llseek,
351 
352 	.owner = THIS_MODULE,
353 };
354 
355 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
356 {
357 	.name = "Probe Extraction CPU DAI",
358 	.compress_new = snd_soc_new_compress,
359 	.cops = &sof_probes_compr_ops,
360 	.capture = {
361 		.stream_name = "Probe Extraction",
362 		.channels_min = 1,
363 		.channels_max = 8,
364 		.rates = SNDRV_PCM_RATE_48000,
365 		.rate_min = 48000,
366 		.rate_max = 48000,
367 	},
368 },
369 };
370 
371 static const struct snd_soc_component_driver sof_probes_component = {
372 	.name = "sof-probes-component",
373 	.compress_ops = &sof_probes_compressed_ops,
374 	.module_get_upon_open = 1,
375 	.legacy_dai_naming = 1,
376 };
377 
378 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
379 
380 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
381 				   const struct auxiliary_device_id *id)
382 {
383 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
384 	struct dentry *dfsroot = sof_client_get_debugfs_root(cdev);
385 	struct device *dev = &auxdev->dev;
386 	struct snd_soc_dai_link_component platform_component[] = {
387 		{
388 			.name = dev_name(dev),
389 		}
390 	};
391 	struct snd_soc_card *card;
392 	struct sof_probes_priv *priv;
393 	struct snd_soc_dai_link_component *cpus;
394 	struct sof_probes_host_ops *ops;
395 	struct snd_soc_dai_link *links;
396 	int ret;
397 
398 	/* do not set up the probes support if it is not enabled */
399 	if (!sof_probes_enabled)
400 		return -ENXIO;
401 
402 	ops = dev_get_platdata(dev);
403 	if (!ops) {
404 		dev_err(dev, "missing platform data\n");
405 		return -ENODEV;
406 	}
407 	if (!ops->startup || !ops->shutdown || !ops->set_params || !ops->trigger ||
408 	    !ops->pointer) {
409 		dev_err(dev, "missing platform callback(s)\n");
410 		return -ENODEV;
411 	}
412 
413 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
414 	if (!priv)
415 		return -ENOMEM;
416 
417 	priv->host_ops = ops;
418 
419 	switch (sof_client_get_ipc_type(cdev)) {
420 #ifdef CONFIG_SND_SOC_SOF_INTEL_IPC4
421 	case SOF_INTEL_IPC4:
422 		priv->ipc_ops = &ipc4_probe_ops;
423 		break;
424 #endif
425 #ifdef CONFIG_SND_SOC_SOF_IPC3
426 	case SOF_IPC:
427 		priv->ipc_ops = &ipc3_probe_ops;
428 		break;
429 #endif
430 	default:
431 		dev_err(dev, "Matching IPC ops not found.");
432 		return -ENODEV;
433 	}
434 
435 	cdev->data = priv;
436 
437 	/* register probes component driver and dai */
438 	ret = devm_snd_soc_register_component(dev, &sof_probes_component,
439 					      sof_probes_dai_drv,
440 					      ARRAY_SIZE(sof_probes_dai_drv));
441 	if (ret < 0) {
442 		dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
443 		return ret;
444 	}
445 
446 	/* set client data */
447 	priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
448 
449 	/* create read-write probes_points debugfs entry */
450 	priv->dfs_points = debugfs_create_file("probe_points", 0644, dfsroot,
451 					       cdev, &sof_probes_points_fops);
452 
453 	/* create read-write probe_points_remove debugfs entry */
454 	priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
455 						      dfsroot, cdev,
456 						      &sof_probes_points_remove_fops);
457 
458 	links = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*links), GFP_KERNEL);
459 	cpus = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*cpus), GFP_KERNEL);
460 	if (!links || !cpus) {
461 		debugfs_remove(priv->dfs_points);
462 		debugfs_remove(priv->dfs_points_remove);
463 		return -ENOMEM;
464 	}
465 
466 	/* extraction DAI link */
467 	links[0].name = "Compress Probe Capture";
468 	links[0].id = 0;
469 	links[0].cpus = &cpus[0];
470 	links[0].num_cpus = 1;
471 	links[0].cpus->dai_name = "Probe Extraction CPU DAI";
472 	links[0].codecs = dummy;
473 	links[0].num_codecs = 1;
474 	links[0].platforms = platform_component;
475 	links[0].num_platforms = ARRAY_SIZE(platform_component);
476 	links[0].nonatomic = 1;
477 
478 	card = &priv->card;
479 
480 	card->dev = dev;
481 	card->name = "sof-probes";
482 	card->owner = THIS_MODULE;
483 	card->num_links = SOF_PROBES_NUM_DAI_LINKS;
484 	card->dai_link = links;
485 
486 	/* set idle_bias_off to prevent the core from resuming the card->dev */
487 	card->dapm.idle_bias_off = true;
488 
489 	snd_soc_card_set_drvdata(card, cdev);
490 
491 	ret = devm_snd_soc_register_card(dev, card);
492 	if (ret < 0) {
493 		debugfs_remove(priv->dfs_points);
494 		debugfs_remove(priv->dfs_points_remove);
495 		dev_err(dev, "Probes card register failed %d\n", ret);
496 		return ret;
497 	}
498 
499 	/* enable runtime PM */
500 	pm_runtime_set_autosuspend_delay(dev, SOF_PROBES_SUSPEND_DELAY_MS);
501 	pm_runtime_use_autosuspend(dev);
502 	pm_runtime_enable(dev);
503 	pm_runtime_mark_last_busy(dev);
504 	pm_runtime_idle(dev);
505 
506 	return 0;
507 }
508 
509 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
510 {
511 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
512 	struct sof_probes_priv *priv = cdev->data;
513 
514 	if (!sof_probes_enabled)
515 		return;
516 
517 	pm_runtime_disable(&auxdev->dev);
518 	debugfs_remove(priv->dfs_points);
519 	debugfs_remove(priv->dfs_points_remove);
520 }
521 
522 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
523 	{ .name = "snd_sof.hda-probes", },
524 	{},
525 };
526 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
527 
528 /* driver name will be set based on KBUILD_MODNAME */
529 static struct auxiliary_driver sof_probes_client_drv = {
530 	.probe = sof_probes_client_probe,
531 	.remove = sof_probes_client_remove,
532 
533 	.id_table = sof_probes_client_id_table,
534 };
535 
536 module_auxiliary_driver(sof_probes_client_drv);
537 
538 MODULE_DESCRIPTION("SOF Probes Client Driver");
539 MODULE_LICENSE("GPL v2");
540 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);
541