projects/linux·blob master · refs

601 lines · 15.9 KiB

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