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 <sound/soc.h>
16 #include <sound/sof/header.h>
17 #include "sof-client.h"
18 #include "sof-client-probes.h"
20 #define SOF_PROBES_SUSPEND_DELAY_MS 3000
21 /* only extraction supported for now */
22 #define SOF_PROBES_NUM_DAI_LINKS 1
24 #define SOF_PROBES_INVALID_NODE_ID UINT_MAX
26 static bool __read_mostly sof_probes_enabled;
27 module_param_named(enable, sof_probes_enabled, bool, 0444);
28 MODULE_PARM_DESC(enable, "Enable SOF probes support");
30 struct sof_probes_priv {
31 struct dentry *dfs_points;
32 struct dentry *dfs_points_remove;
33 u32 extractor_stream_tag;
34 struct snd_soc_card card;
36 const struct sof_probes_host_ops *host_ops;
39 struct sof_probe_point_desc {
40 unsigned int buffer_id;
42 unsigned int stream_tag;
45 struct sof_probe_dma {
46 unsigned int stream_tag;
47 unsigned int dma_buffer_size;
50 struct sof_ipc_probe_dma_add_params {
51 struct sof_ipc_cmd_hdr hdr;
52 unsigned int num_elems;
53 struct sof_probe_dma dma[];
56 struct sof_ipc_probe_info_params {
57 struct sof_ipc_reply rhdr;
58 unsigned int num_elems;
60 struct sof_probe_dma dma[0];
61 struct sof_probe_point_desc desc[0];
65 struct sof_ipc_probe_point_add_params {
66 struct sof_ipc_cmd_hdr hdr;
67 unsigned int num_elems;
68 struct sof_probe_point_desc desc[];
71 struct sof_ipc_probe_point_remove_params {
72 struct sof_ipc_cmd_hdr hdr;
73 unsigned int num_elems;
74 unsigned int buffer_id[];
78 * sof_probes_init - initialize data probing
79 * @cdev: SOF client device
80 * @stream_tag: Extractor stream tag
81 * @buffer_size: DMA buffer size to set for extractor
83 * Host chooses whether extraction is supported or not by providing
84 * valid stream tag to DSP. Once specified, stream described by that
85 * tag will be tied to DSP for extraction for the entire lifetime of
88 * Probing is initialized only once and each INIT request must be
89 * matched by DEINIT call.
91 static int sof_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
94 struct sof_ipc_probe_dma_add_params *msg;
95 size_t size = struct_size(msg, dma, 1);
96 struct sof_ipc_reply reply;
99 msg = kmalloc(size, GFP_KERNEL);
102 msg->hdr.size = size;
103 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
105 msg->dma[0].stream_tag = stream_tag;
106 msg->dma[0].dma_buffer_size = buffer_size;
108 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
114 * sof_probes_deinit - cleanup after data probing
115 * @cdev: SOF client device
117 * Host sends DEINIT request to free previously initialized probe
118 * on DSP side once it is no longer needed. DEINIT only when there
119 * are no probes connected and with all injectors detached.
121 static int sof_probes_deinit(struct sof_client_dev *cdev)
123 struct sof_ipc_cmd_hdr msg;
124 struct sof_ipc_reply reply;
126 msg.size = sizeof(msg);
127 msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
129 return sof_client_ipc_tx_message(cdev, &msg, &reply, sizeof(reply));
132 static int sof_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
133 void **params, size_t *num_params)
135 size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
136 struct sof_ipc_probe_info_params msg = {{{0}}};
137 struct sof_ipc_probe_info_params *reply;
144 reply = kzalloc(max_msg_size, GFP_KERNEL);
147 msg.rhdr.hdr.size = sizeof(msg);
148 msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
150 ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
151 if (ret < 0 || reply->rhdr.error < 0)
154 if (!reply->num_elems)
157 if (cmd == SOF_IPC_PROBE_DMA_INFO)
158 bytes = sizeof(reply->dma[0]);
160 bytes = sizeof(reply->desc[0]);
161 bytes *= reply->num_elems;
162 *params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
167 *num_params = reply->num_elems;
175 * sof_probes_points_info - retrieve list of active probe points
176 * @cdev: SOF client device
177 * @desc: Returned list of active probes
178 * @num_desc: Returned count of active probes
180 * Host sends PROBE_POINT_INFO request to obtain list of active probe
181 * points, valid for disconnection when given probe is no longer
184 static int sof_probes_points_info(struct sof_client_dev *cdev,
185 struct sof_probe_point_desc **desc,
188 return sof_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
189 (void **)desc, num_desc);
193 * sof_probes_points_add - connect specified probes
194 * @cdev: SOF client device
195 * @desc: List of probe points to connect
196 * @num_desc: Number of elements in @desc
198 * Dynamically connects to provided set of endpoints. Immediately
199 * after connection is established, host must be prepared to
200 * transfer data from or to target stream given the probing purpose.
202 * Each probe point should be removed using PROBE_POINT_REMOVE
203 * request when no longer needed.
205 static int sof_probes_points_add(struct sof_client_dev *cdev,
206 struct sof_probe_point_desc *desc,
209 struct sof_ipc_probe_point_add_params *msg;
210 size_t size = struct_size(msg, desc, num_desc);
211 struct sof_ipc_reply reply;
214 msg = kmalloc(size, GFP_KERNEL);
217 msg->hdr.size = size;
218 msg->num_elems = num_desc;
219 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
220 memcpy(&msg->desc[0], desc, size - sizeof(*msg));
222 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
228 * sof_probes_points_remove - disconnect specified probes
229 * @cdev: SOF client device
230 * @buffer_id: List of probe points to disconnect
231 * @num_buffer_id: Number of elements in @desc
233 * Removes previously connected probes from list of active probe
234 * points and frees all resources on DSP side.
236 static int sof_probes_points_remove(struct sof_client_dev *cdev,
237 unsigned int *buffer_id, size_t num_buffer_id)
239 struct sof_ipc_probe_point_remove_params *msg;
240 size_t size = struct_size(msg, buffer_id, num_buffer_id);
241 struct sof_ipc_reply reply;
244 msg = kmalloc(size, GFP_KERNEL);
247 msg->hdr.size = size;
248 msg->num_elems = num_buffer_id;
249 msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
250 memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
252 ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
257 static int sof_probes_compr_startup(struct snd_compr_stream *cstream,
258 struct snd_soc_dai *dai)
260 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
261 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
262 struct sof_probes_priv *priv = cdev->data;
263 const struct sof_probes_host_ops *ops = priv->host_ops;
266 if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
269 ret = sof_client_core_module_get(cdev);
273 ret = ops->assign(cdev, cstream, dai, &priv->extractor_stream_tag);
275 dev_err(dai->dev, "Failed to assign probe stream: %d\n", ret);
276 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
277 sof_client_core_module_put(cdev);
283 static int sof_probes_compr_shutdown(struct snd_compr_stream *cstream,
284 struct snd_soc_dai *dai)
286 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
287 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
288 struct sof_probes_priv *priv = cdev->data;
289 const struct sof_probes_host_ops *ops = priv->host_ops;
290 struct sof_probe_point_desc *desc;
294 /* disconnect all probe points */
295 ret = sof_probes_points_info(cdev, &desc, &num_desc);
297 dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
301 for (i = 0; i < num_desc; i++)
302 sof_probes_points_remove(cdev, &desc[i].buffer_id, 1);
306 ret = sof_probes_deinit(cdev);
308 dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
310 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
311 snd_compr_free_pages(cstream);
313 ret = ops->free(cdev, cstream, dai);
315 sof_client_core_module_put(cdev);
320 static int sof_probes_compr_set_params(struct snd_compr_stream *cstream,
321 struct snd_compr_params *params,
322 struct snd_soc_dai *dai)
324 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
325 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
326 struct snd_compr_runtime *rtd = cstream->runtime;
327 struct sof_probes_priv *priv = cdev->data;
328 const struct sof_probes_host_ops *ops = priv->host_ops;
331 cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
332 cstream->dma_buffer.dev.dev = sof_client_get_dma_dev(cdev);
333 ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
337 ret = ops->set_params(cdev, cstream, params, dai);
341 ret = sof_probes_init(cdev, priv->extractor_stream_tag, rtd->dma_bytes);
343 dev_err(dai->dev, "Failed to init probe: %d\n", ret);
350 static int sof_probes_compr_trigger(struct snd_compr_stream *cstream, int cmd,
351 struct snd_soc_dai *dai)
353 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
354 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
355 struct sof_probes_priv *priv = cdev->data;
356 const struct sof_probes_host_ops *ops = priv->host_ops;
358 return ops->trigger(cdev, cstream, cmd, dai);
361 static int sof_probes_compr_pointer(struct snd_compr_stream *cstream,
362 struct snd_compr_tstamp *tstamp,
363 struct snd_soc_dai *dai)
365 struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
366 struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
367 struct sof_probes_priv *priv = cdev->data;
368 const struct sof_probes_host_ops *ops = priv->host_ops;
370 return ops->pointer(cdev, cstream, tstamp, dai);
373 static const struct snd_soc_cdai_ops sof_probes_compr_ops = {
374 .startup = sof_probes_compr_startup,
375 .shutdown = sof_probes_compr_shutdown,
376 .set_params = sof_probes_compr_set_params,
377 .trigger = sof_probes_compr_trigger,
378 .pointer = sof_probes_compr_pointer,
381 static int sof_probes_compr_copy(struct snd_soc_component *component,
382 struct snd_compr_stream *cstream,
383 char __user *buf, size_t count)
385 struct snd_compr_runtime *rtd = cstream->runtime;
386 unsigned int offset, n;
390 if (count > rtd->buffer_size)
391 count = rtd->buffer_size;
393 div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
394 ptr = rtd->dma_area + offset;
395 n = rtd->buffer_size - offset;
398 ret = copy_to_user(buf, ptr, count);
400 ret = copy_to_user(buf, ptr, n);
401 ret += copy_to_user(buf + n, rtd->dma_area, count - n);
409 static const struct snd_compress_ops sof_probes_compressed_ops = {
410 .copy = sof_probes_compr_copy,
414 * strsplit_u32 - Split string into sequence of u32 tokens
415 * @buf: String to split into tokens.
416 * @delim: String containing delimiter characters.
417 * @tkns: Returned u32 sequence pointer.
418 * @num_tkns: Returned number of tokens obtained.
420 static int strsplit_u32(char *buf, const char *delim, u32 **tkns, size_t *num_tkns)
430 data = kcalloc(cap, sizeof(*data), GFP_KERNEL);
434 while ((s = strsep(&buf, delim)) != NULL) {
435 ret = kstrtouint(s, 0, data + count);
438 if (++count >= cap) {
440 tmp = krealloc(data, cap * sizeof(*data), GFP_KERNEL);
451 *tkns = kmemdup(data, count * sizeof(*data), GFP_KERNEL);
463 static int tokenize_input(const char __user *from, size_t count,
464 loff_t *ppos, u32 **tkns, size_t *num_tkns)
469 buf = kmalloc(count + 1, GFP_KERNEL);
473 ret = simple_write_to_buffer(buf, count, ppos, from, count);
475 ret = ret >= 0 ? -EIO : ret;
480 ret = strsplit_u32(buf, ",", tkns, num_tkns);
486 static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
487 size_t count, loff_t *ppos)
489 struct sof_client_dev *cdev = file->private_data;
490 struct sof_probes_priv *priv = cdev->data;
491 struct device *dev = &cdev->auxdev.dev;
492 struct sof_probe_point_desc *desc;
493 int remaining, offset;
498 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
499 dev_warn(dev, "no extractor stream running\n");
503 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
507 ret = pm_runtime_resume_and_get(dev);
508 if (ret < 0 && ret != -EACCES) {
509 dev_err_ratelimited(dev, "debugfs read failed to resume %d\n", ret);
513 ret = sof_probes_points_info(cdev, &desc, &num_desc);
517 pm_runtime_mark_last_busy(dev);
518 err = pm_runtime_put_autosuspend(dev);
520 dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
522 for (i = 0; i < num_desc; i++) {
523 offset = strlen(buf);
524 remaining = PAGE_SIZE - offset;
525 ret = snprintf(buf + offset, remaining,
526 "Id: %#010x Purpose: %u Node id: %#x\n",
527 desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
528 if (ret < 0 || ret >= remaining) {
529 /* truncate the output buffer at the last full line */
535 ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
544 sof_probes_dfs_points_write(struct file *file, const char __user *from,
545 size_t count, loff_t *ppos)
547 struct sof_client_dev *cdev = file->private_data;
548 struct sof_probes_priv *priv = cdev->data;
549 struct device *dev = &cdev->auxdev.dev;
550 struct sof_probe_point_desc *desc;
551 size_t num_tkns, bytes;
555 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
556 dev_warn(dev, "no extractor stream running\n");
560 ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
563 bytes = sizeof(*tkns) * num_tkns;
564 if (!num_tkns || (bytes % sizeof(*desc))) {
569 desc = (struct sof_probe_point_desc *)tkns;
571 ret = pm_runtime_resume_and_get(dev);
572 if (ret < 0 && ret != -EACCES) {
573 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
577 ret = sof_probes_points_add(cdev, desc, bytes / sizeof(*desc));
581 pm_runtime_mark_last_busy(dev);
582 err = pm_runtime_put_autosuspend(dev);
584 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
590 static const struct file_operations sof_probes_points_fops = {
592 .read = sof_probes_dfs_points_read,
593 .write = sof_probes_dfs_points_write,
594 .llseek = default_llseek,
596 .owner = THIS_MODULE,
600 sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
601 size_t count, loff_t *ppos)
603 struct sof_client_dev *cdev = file->private_data;
604 struct sof_probes_priv *priv = cdev->data;
605 struct device *dev = &cdev->auxdev.dev;
610 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
611 dev_warn(dev, "no extractor stream running\n");
615 ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
623 ret = pm_runtime_resume_and_get(dev);
625 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
629 ret = sof_probes_points_remove(cdev, tkns, num_tkns);
633 pm_runtime_mark_last_busy(dev);
634 err = pm_runtime_put_autosuspend(dev);
636 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
642 static const struct file_operations sof_probes_points_remove_fops = {
644 .write = sof_probes_dfs_points_remove_write,
645 .llseek = default_llseek,
647 .owner = THIS_MODULE,
650 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
652 .name = "Probe Extraction CPU DAI",
653 .compress_new = snd_soc_new_compress,
654 .cops = &sof_probes_compr_ops,
656 .stream_name = "Probe Extraction",
659 .rates = SNDRV_PCM_RATE_48000,
666 static const struct snd_soc_component_driver sof_probes_component = {
667 .name = "sof-probes-component",
668 .compress_ops = &sof_probes_compressed_ops,
669 .module_get_upon_open = 1,
672 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
674 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
675 const struct auxiliary_device_id *id)
677 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
678 struct dentry *dfsroot = sof_client_get_debugfs_root(cdev);
679 struct device *dev = &auxdev->dev;
680 struct snd_soc_dai_link_component platform_component[] = {
682 .name = dev_name(dev),
685 struct snd_soc_card *card;
686 struct sof_probes_priv *priv;
687 struct snd_soc_dai_link_component *cpus;
688 struct sof_probes_host_ops *ops;
689 struct snd_soc_dai_link *links;
692 /* do not set up the probes support if it is not enabled */
693 if (!sof_probes_enabled)
696 if (!dev->platform_data) {
697 dev_err(dev, "missing platform data\n");
701 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
705 ops = dev->platform_data;
707 if (!ops->assign || !ops->free || !ops->set_params || !ops->trigger ||
709 dev_err(dev, "missing platform callback(s)\n");
713 priv->host_ops = ops;
716 /* register probes component driver and dai */
717 ret = devm_snd_soc_register_component(dev, &sof_probes_component,
719 ARRAY_SIZE(sof_probes_dai_drv));
721 dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
725 /* set client data */
726 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
728 /* create read-write probes_points debugfs entry */
729 priv->dfs_points = debugfs_create_file("probe_points", 0644, dfsroot,
730 cdev, &sof_probes_points_fops);
732 /* create read-write probe_points_remove debugfs entry */
733 priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
735 &sof_probes_points_remove_fops);
737 links = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*links), GFP_KERNEL);
738 cpus = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*cpus), GFP_KERNEL);
739 if (!links || !cpus) {
740 debugfs_remove(priv->dfs_points);
741 debugfs_remove(priv->dfs_points_remove);
745 /* extraction DAI link */
746 links[0].name = "Compress Probe Capture";
748 links[0].cpus = &cpus[0];
749 links[0].num_cpus = 1;
750 links[0].cpus->dai_name = "Probe Extraction CPU DAI";
751 links[0].codecs = dummy;
752 links[0].num_codecs = 1;
753 links[0].platforms = platform_component;
754 links[0].num_platforms = ARRAY_SIZE(platform_component);
755 links[0].nonatomic = 1;
760 card->name = "sof-probes";
761 card->owner = THIS_MODULE;
762 card->num_links = SOF_PROBES_NUM_DAI_LINKS;
763 card->dai_link = links;
765 /* set idle_bias_off to prevent the core from resuming the card->dev */
766 card->dapm.idle_bias_off = true;
768 snd_soc_card_set_drvdata(card, cdev);
770 ret = devm_snd_soc_register_card(dev, card);
772 debugfs_remove(priv->dfs_points);
773 debugfs_remove(priv->dfs_points_remove);
774 dev_err(dev, "Probes card register failed %d\n", ret);
778 /* enable runtime PM */
779 pm_runtime_set_autosuspend_delay(dev, SOF_PROBES_SUSPEND_DELAY_MS);
780 pm_runtime_use_autosuspend(dev);
781 pm_runtime_enable(dev);
782 pm_runtime_mark_last_busy(dev);
783 pm_runtime_idle(dev);
788 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
790 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
791 struct sof_probes_priv *priv = cdev->data;
793 if (!sof_probes_enabled)
796 pm_runtime_disable(&auxdev->dev);
797 debugfs_remove(priv->dfs_points);
798 debugfs_remove(priv->dfs_points_remove);
801 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
802 { .name = "snd_sof.hda-probes", },
805 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
807 /* driver name will be set based on KBUILD_MODNAME */
808 static struct auxiliary_driver sof_probes_client_drv = {
809 .probe = sof_probes_client_probe,
810 .remove = sof_probes_client_remove,
812 .id_table = sof_probes_client_id_table,
815 module_auxiliary_driver(sof_probes_client_drv);
817 MODULE_DESCRIPTION("SOF Probes Client Driver");
818 MODULE_LICENSE("GPL v2");
819 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);