1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
8 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
9 // Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
12 #include <linux/debugfs.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/string_helpers.h>
17 #include <sound/soc.h>
18 #include <sound/sof/header.h>
19 #include "sof-client.h"
20 #include "sof-client-probes.h"
22 #define SOF_PROBES_SUSPEND_DELAY_MS 3000
23 /* only extraction supported for now */
24 #define SOF_PROBES_NUM_DAI_LINKS 1
26 #define SOF_PROBES_INVALID_NODE_ID UINT_MAX
28 static bool __read_mostly sof_probes_enabled;
29 module_param_named(enable, sof_probes_enabled, bool, 0444);
30 MODULE_PARM_DESC(enable, "Enable SOF probes support");
32 struct sof_probes_priv {
33 struct dentry *dfs_points;
34 struct dentry *dfs_points_remove;
35 u32 extractor_stream_tag;
36 struct snd_soc_card card;
38 const struct sof_probes_host_ops *host_ops;
41 struct sof_probe_point_desc {
42 unsigned int buffer_id;
44 unsigned int stream_tag;
47 struct sof_probe_dma {
48 unsigned int stream_tag;
49 unsigned int dma_buffer_size;
52 struct sof_ipc_probe_dma_add_params {
53 struct sof_ipc_cmd_hdr hdr;
54 unsigned int num_elems;
55 struct sof_probe_dma dma[];
58 struct sof_ipc_probe_info_params {
59 struct sof_ipc_reply rhdr;
60 unsigned int num_elems;
62 struct sof_probe_dma dma[0];
63 struct sof_probe_point_desc desc[0];
67 struct sof_ipc_probe_point_add_params {
68 struct sof_ipc_cmd_hdr hdr;
69 unsigned int num_elems;
70 struct sof_probe_point_desc desc[];
73 struct sof_ipc_probe_point_remove_params {
74 struct sof_ipc_cmd_hdr hdr;
75 unsigned int num_elems;
76 unsigned int buffer_id[];
80 * sof_probes_init - initialize data probing
81 * @cdev: SOF client device
82 * @stream_tag: Extractor stream tag
83 * @buffer_size: DMA buffer size to set for extractor
85 * Host chooses whether extraction is supported or not by providing
86 * valid stream tag to DSP. Once specified, stream described by that
87 * tag will be tied to DSP for extraction for the entire lifetime of
90 * Probing is initialized only once and each INIT request must be
91 * matched by DEINIT call.
93 static int sof_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
96 struct sof_ipc_probe_dma_add_params *msg;
97 size_t size = struct_size(msg, dma, 1);
98 struct sof_ipc_reply reply;
101 msg = kmalloc(size, GFP_KERNEL);
104 msg->hdr.size = size;
105 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
107 msg->dma[0].stream_tag = stream_tag;
108 msg->dma[0].dma_buffer_size = buffer_size;
110 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
116 * sof_probes_deinit - cleanup after data probing
117 * @cdev: SOF client device
119 * Host sends DEINIT request to free previously initialized probe
120 * on DSP side once it is no longer needed. DEINIT only when there
121 * are no probes connected and with all injectors detached.
123 static int sof_probes_deinit(struct sof_client_dev *cdev)
125 struct sof_ipc_cmd_hdr msg;
126 struct sof_ipc_reply reply;
128 msg.size = sizeof(msg);
129 msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
131 return sof_client_ipc_tx_message(cdev, &msg, &reply, sizeof(reply));
134 static int sof_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
135 void **params, size_t *num_params)
137 size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
138 struct sof_ipc_probe_info_params msg = {{{0}}};
139 struct sof_ipc_probe_info_params *reply;
146 reply = kzalloc(max_msg_size, GFP_KERNEL);
149 msg.rhdr.hdr.size = sizeof(msg);
150 msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
152 ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
153 if (ret < 0 || reply->rhdr.error < 0)
156 if (!reply->num_elems)
159 if (cmd == SOF_IPC_PROBE_DMA_INFO)
160 bytes = sizeof(reply->dma[0]);
162 bytes = sizeof(reply->desc[0]);
163 bytes *= reply->num_elems;
164 *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
169 *num_params = reply->num_elems;
177 * sof_probes_points_info - retrieve list of active probe points
178 * @cdev: SOF client device
179 * @desc: Returned list of active probes
180 * @num_desc: Returned count of active probes
182 * Host sends PROBE_POINT_INFO request to obtain list of active probe
183 * points, valid for disconnection when given probe is no longer
186 static int sof_probes_points_info(struct sof_client_dev *cdev,
187 struct sof_probe_point_desc **desc,
190 return sof_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
191 (void **)desc, num_desc);
195 * sof_probes_points_add - connect specified probes
196 * @cdev: SOF client device
197 * @desc: List of probe points to connect
198 * @num_desc: Number of elements in @desc
200 * Dynamically connects to provided set of endpoints. Immediately
201 * after connection is established, host must be prepared to
202 * transfer data from or to target stream given the probing purpose.
204 * Each probe point should be removed using PROBE_POINT_REMOVE
205 * request when no longer needed.
207 static int sof_probes_points_add(struct sof_client_dev *cdev,
208 struct sof_probe_point_desc *desc,
211 struct sof_ipc_probe_point_add_params *msg;
212 size_t size = struct_size(msg, desc, num_desc);
213 struct sof_ipc_reply reply;
216 msg = kmalloc(size, GFP_KERNEL);
219 msg->hdr.size = size;
220 msg->num_elems = num_desc;
221 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
222 memcpy(&msg->desc[0], desc, size - sizeof(*msg));
224 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
230 * sof_probes_points_remove - disconnect specified probes
231 * @cdev: SOF client device
232 * @buffer_id: List of probe points to disconnect
233 * @num_buffer_id: Number of elements in @desc
235 * Removes previously connected probes from list of active probe
236 * points and frees all resources on DSP side.
238 static int sof_probes_points_remove(struct sof_client_dev *cdev,
239 unsigned int *buffer_id, size_t num_buffer_id)
241 struct sof_ipc_probe_point_remove_params *msg;
242 size_t size = struct_size(msg, buffer_id, num_buffer_id);
243 struct sof_ipc_reply reply;
246 msg = kmalloc(size, GFP_KERNEL);
249 msg->hdr.size = size;
250 msg->num_elems = num_buffer_id;
251 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
252 memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
254 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
259 static int sof_probes_compr_startup(struct snd_compr_stream *cstream,
260 struct snd_soc_dai *dai)
262 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
263 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
264 struct sof_probes_priv *priv = cdev->data;
265 const struct sof_probes_host_ops *ops = priv->host_ops;
268 if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
271 ret = sof_client_core_module_get(cdev);
275 ret = ops->startup(cdev, cstream, dai, &priv->extractor_stream_tag);
277 dev_err(dai->dev, "Failed to startup probe stream: %d\n", ret);
278 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
279 sof_client_core_module_put(cdev);
285 static int sof_probes_compr_shutdown(struct snd_compr_stream *cstream,
286 struct snd_soc_dai *dai)
288 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
289 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
290 struct sof_probes_priv *priv = cdev->data;
291 const struct sof_probes_host_ops *ops = priv->host_ops;
292 struct sof_probe_point_desc *desc;
296 /* disconnect all probe points */
297 ret = sof_probes_points_info(cdev, &desc, &num_desc);
299 dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
303 for (i = 0; i < num_desc; i++)
304 sof_probes_points_remove(cdev, &desc[i].buffer_id, 1);
308 ret = sof_probes_deinit(cdev);
310 dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
312 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
313 snd_compr_free_pages(cstream);
315 ret = ops->shutdown(cdev, cstream, dai);
317 sof_client_core_module_put(cdev);
322 static int sof_probes_compr_set_params(struct snd_compr_stream *cstream,
323 struct snd_compr_params *params,
324 struct snd_soc_dai *dai)
326 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
327 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
328 struct snd_compr_runtime *rtd = cstream->runtime;
329 struct sof_probes_priv *priv = cdev->data;
330 const struct sof_probes_host_ops *ops = priv->host_ops;
333 cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
334 cstream->dma_buffer.dev.dev = sof_client_get_dma_dev(cdev);
335 ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
339 ret = ops->set_params(cdev, cstream, params, dai);
343 ret = sof_probes_init(cdev, priv->extractor_stream_tag, rtd->dma_bytes);
345 dev_err(dai->dev, "Failed to init probe: %d\n", ret);
352 static int sof_probes_compr_trigger(struct snd_compr_stream *cstream, int cmd,
353 struct snd_soc_dai *dai)
355 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
356 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
357 struct sof_probes_priv *priv = cdev->data;
358 const struct sof_probes_host_ops *ops = priv->host_ops;
360 return ops->trigger(cdev, cstream, cmd, dai);
363 static int sof_probes_compr_pointer(struct snd_compr_stream *cstream,
364 struct snd_compr_tstamp *tstamp,
365 struct snd_soc_dai *dai)
367 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
368 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
369 struct sof_probes_priv *priv = cdev->data;
370 const struct sof_probes_host_ops *ops = priv->host_ops;
372 return ops->pointer(cdev, cstream, tstamp, dai);
375 static const struct snd_soc_cdai_ops sof_probes_compr_ops = {
376 .startup = sof_probes_compr_startup,
377 .shutdown = sof_probes_compr_shutdown,
378 .set_params = sof_probes_compr_set_params,
379 .trigger = sof_probes_compr_trigger,
380 .pointer = sof_probes_compr_pointer,
383 static int sof_probes_compr_copy(struct snd_soc_component *component,
384 struct snd_compr_stream *cstream,
385 char __user *buf, size_t count)
387 struct snd_compr_runtime *rtd = cstream->runtime;
388 unsigned int offset, n;
392 if (count > rtd->buffer_size)
393 count = rtd->buffer_size;
395 div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
396 ptr = rtd->dma_area + offset;
397 n = rtd->buffer_size - offset;
400 ret = copy_to_user(buf, ptr, count);
402 ret = copy_to_user(buf, ptr, n);
403 ret += copy_to_user(buf + n, rtd->dma_area, count - n);
411 static const struct snd_compress_ops sof_probes_compressed_ops = {
412 .copy = sof_probes_compr_copy,
415 static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
416 size_t count, loff_t *ppos)
418 struct sof_client_dev *cdev = file->private_data;
419 struct sof_probes_priv *priv = cdev->data;
420 struct device *dev = &cdev->auxdev.dev;
421 struct sof_probe_point_desc *desc;
422 int remaining, offset;
427 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
428 dev_warn(dev, "no extractor stream running\n");
432 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
436 ret = pm_runtime_resume_and_get(dev);
437 if (ret < 0 && ret != -EACCES) {
438 dev_err_ratelimited(dev, "debugfs read failed to resume %d\n", ret);
442 ret = sof_probes_points_info(cdev, &desc, &num_desc);
446 for (i = 0; i < num_desc; i++) {
447 offset = strlen(buf);
448 remaining = PAGE_SIZE - offset;
449 ret = snprintf(buf + offset, remaining,
450 "Id: %#010x Purpose: %u Node id: %#x\n",
451 desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
452 if (ret < 0 || ret >= remaining) {
453 /* truncate the output buffer at the last full line */
459 ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
464 pm_runtime_mark_last_busy(dev);
465 err = pm_runtime_put_autosuspend(dev);
467 dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
475 sof_probes_dfs_points_write(struct file *file, const char __user *from,
476 size_t count, loff_t *ppos)
478 struct sof_client_dev *cdev = file->private_data;
479 struct sof_probes_priv *priv = cdev->data;
480 struct device *dev = &cdev->auxdev.dev;
481 struct sof_probe_point_desc *desc;
482 u32 num_elems, *array;
486 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
487 dev_warn(dev, "no extractor stream running\n");
491 ret = parse_int_array_user(from, count, (int **)&array);
496 bytes = sizeof(*array) * num_elems;
497 if (bytes % sizeof(*desc)) {
502 desc = (struct sof_probe_point_desc *)&array[1];
504 ret = pm_runtime_resume_and_get(dev);
505 if (ret < 0 && ret != -EACCES) {
506 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
510 ret = sof_probes_points_add(cdev, desc, bytes / sizeof(*desc));
514 pm_runtime_mark_last_busy(dev);
515 err = pm_runtime_put_autosuspend(dev);
517 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
523 static const struct file_operations sof_probes_points_fops = {
525 .read = sof_probes_dfs_points_read,
526 .write = sof_probes_dfs_points_write,
527 .llseek = default_llseek,
529 .owner = THIS_MODULE,
533 sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
534 size_t count, loff_t *ppos)
536 struct sof_client_dev *cdev = file->private_data;
537 struct sof_probes_priv *priv = cdev->data;
538 struct device *dev = &cdev->auxdev.dev;
542 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
543 dev_warn(dev, "no extractor stream running\n");
547 ret = parse_int_array_user(from, count, (int **)&array);
551 ret = pm_runtime_resume_and_get(dev);
553 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
557 ret = sof_probes_points_remove(cdev, &array[1], array[0]);
561 pm_runtime_mark_last_busy(dev);
562 err = pm_runtime_put_autosuspend(dev);
564 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
570 static const struct file_operations sof_probes_points_remove_fops = {
572 .write = sof_probes_dfs_points_remove_write,
573 .llseek = default_llseek,
575 .owner = THIS_MODULE,
578 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
580 .name = "Probe Extraction CPU DAI",
581 .compress_new = snd_soc_new_compress,
582 .cops = &sof_probes_compr_ops,
584 .stream_name = "Probe Extraction",
587 .rates = SNDRV_PCM_RATE_48000,
594 static const struct snd_soc_component_driver sof_probes_component = {
595 .name = "sof-probes-component",
596 .compress_ops = &sof_probes_compressed_ops,
597 .module_get_upon_open = 1,
598 .legacy_dai_naming = 1,
601 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
603 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
604 const struct auxiliary_device_id *id)
606 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
607 struct dentry *dfsroot = sof_client_get_debugfs_root(cdev);
608 struct device *dev = &auxdev->dev;
609 struct snd_soc_dai_link_component platform_component[] = {
611 .name = dev_name(dev),
614 struct snd_soc_card *card;
615 struct sof_probes_priv *priv;
616 struct snd_soc_dai_link_component *cpus;
617 struct sof_probes_host_ops *ops;
618 struct snd_soc_dai_link *links;
621 /* do not set up the probes support if it is not enabled */
622 if (!sof_probes_enabled)
625 /* only ipc3 is supported */
626 if (sof_client_get_ipc_type(cdev) != SOF_IPC)
629 if (!dev->platform_data) {
630 dev_err(dev, "missing platform data\n");
634 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
638 ops = dev->platform_data;
640 if (!ops->startup || !ops->shutdown || !ops->set_params || !ops->trigger ||
642 dev_err(dev, "missing platform callback(s)\n");
646 priv->host_ops = ops;
649 /* register probes component driver and dai */
650 ret = devm_snd_soc_register_component(dev, &sof_probes_component,
652 ARRAY_SIZE(sof_probes_dai_drv));
654 dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
658 /* set client data */
659 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
661 /* create read-write probes_points debugfs entry */
662 priv->dfs_points = debugfs_create_file("probe_points", 0644, dfsroot,
663 cdev, &sof_probes_points_fops);
665 /* create read-write probe_points_remove debugfs entry */
666 priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
668 &sof_probes_points_remove_fops);
670 links = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*links), GFP_KERNEL);
671 cpus = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*cpus), GFP_KERNEL);
672 if (!links || !cpus) {
673 debugfs_remove(priv->dfs_points);
674 debugfs_remove(priv->dfs_points_remove);
678 /* extraction DAI link */
679 links[0].name = "Compress Probe Capture";
681 links[0].cpus = &cpus[0];
682 links[0].num_cpus = 1;
683 links[0].cpus->dai_name = "Probe Extraction CPU DAI";
684 links[0].codecs = dummy;
685 links[0].num_codecs = 1;
686 links[0].platforms = platform_component;
687 links[0].num_platforms = ARRAY_SIZE(platform_component);
688 links[0].nonatomic = 1;
693 card->name = "sof-probes";
694 card->owner = THIS_MODULE;
695 card->num_links = SOF_PROBES_NUM_DAI_LINKS;
696 card->dai_link = links;
698 /* set idle_bias_off to prevent the core from resuming the card->dev */
699 card->dapm.idle_bias_off = true;
701 snd_soc_card_set_drvdata(card, cdev);
703 ret = devm_snd_soc_register_card(dev, card);
705 debugfs_remove(priv->dfs_points);
706 debugfs_remove(priv->dfs_points_remove);
707 dev_err(dev, "Probes card register failed %d\n", ret);
711 /* enable runtime PM */
712 pm_runtime_set_autosuspend_delay(dev, SOF_PROBES_SUSPEND_DELAY_MS);
713 pm_runtime_use_autosuspend(dev);
714 pm_runtime_enable(dev);
715 pm_runtime_mark_last_busy(dev);
716 pm_runtime_idle(dev);
721 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
723 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
724 struct sof_probes_priv *priv = cdev->data;
726 if (!sof_probes_enabled)
729 pm_runtime_disable(&auxdev->dev);
730 debugfs_remove(priv->dfs_points);
731 debugfs_remove(priv->dfs_points_remove);
734 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
735 { .name = "snd_sof.hda-probes", },
738 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
740 /* driver name will be set based on KBUILD_MODNAME */
741 static struct auxiliary_driver sof_probes_client_drv = {
742 .probe = sof_probes_client_probe,
743 .remove = sof_probes_client_remove,
745 .id_table = sof_probes_client_id_table,
748 module_auxiliary_driver(sof_probes_client_drv);
750 MODULE_DESCRIPTION("SOF Probes Client Driver");
751 MODULE_LICENSE("GPL v2");
752 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);