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>
16 #include <linux/stddef.h>
18 #include <sound/soc.h>
19 #include <sound/sof/header.h>
20 #include "sof-client.h"
21 #include "sof-client-probes.h"
23 #define SOF_PROBES_SUSPEND_DELAY_MS 3000
24 /* only extraction supported for now */
25 #define SOF_PROBES_NUM_DAI_LINKS 1
27 #define SOF_PROBES_INVALID_NODE_ID UINT_MAX
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");
33 static int sof_probes_compr_startup(struct snd_compr_stream *cstream,
34 struct snd_soc_dai *dai)
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;
42 if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
45 ret = sof_client_core_module_get(cdev);
49 ret = ops->startup(cdev, cstream, dai, &priv->extractor_stream_tag);
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);
59 static int sof_probes_compr_shutdown(struct snd_compr_stream *cstream,
60 struct snd_soc_dai *dai)
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;
71 /* disconnect all probe points */
72 ret = ipc->points_info(cdev, &desc, &num_desc);
74 dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
78 for (i = 0; i < num_desc; i++)
79 ipc->points_remove(cdev, &desc[i].buffer_id, 1);
83 ret = ipc->deinit(cdev);
85 dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
87 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
88 snd_compr_free_pages(cstream);
90 ret = ops->shutdown(cdev, cstream, dai);
92 sof_client_core_module_put(cdev);
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)
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;
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);
115 ret = ops->set_params(cdev, cstream, params, dai);
119 ret = ipc->init(cdev, priv->extractor_stream_tag, rtd->dma_bytes);
121 dev_err(dai->dev, "Failed to init probe: %d\n", ret);
128 static int sof_probes_compr_trigger(struct snd_compr_stream *cstream, int cmd,
129 struct snd_soc_dai *dai)
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;
136 return ops->trigger(cdev, cstream, cmd, dai);
139 static int sof_probes_compr_pointer(struct snd_compr_stream *cstream,
140 struct snd_compr_tstamp *tstamp,
141 struct snd_soc_dai *dai)
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;
148 return ops->pointer(cdev, cstream, tstamp, dai);
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,
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)
163 struct snd_compr_runtime *rtd = cstream->runtime;
164 unsigned int offset, n;
168 if (count > rtd->buffer_size)
169 count = rtd->buffer_size;
171 div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
172 ptr = rtd->dma_area + offset;
173 n = rtd->buffer_size - offset;
176 ret = copy_to_user(buf, ptr, count);
178 ret = copy_to_user(buf, ptr, n);
179 ret += copy_to_user(buf + n, rtd->dma_area, count - n);
187 static const struct snd_compress_ops sof_probes_compressed_ops = {
188 .copy = sof_probes_compr_copy,
191 static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
192 size_t count, loff_t *ppos)
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;
204 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
205 dev_warn(dev, "no extractor stream running\n");
209 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
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);
219 ret = ipc->points_info(cdev, &desc, &num_desc);
223 pm_runtime_mark_last_busy(dev);
224 err = pm_runtime_put_autosuspend(dev);
226 dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
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 */
241 ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
250 sof_probes_dfs_points_write(struct file *file, const char __user *from,
251 size_t count, loff_t *ppos)
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;
262 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
263 dev_warn(dev, "no extractor stream running\n");
267 ret = parse_int_array_user(from, count, (int **)&array);
272 bytes = sizeof(*array) * num_elems;
273 if (bytes % sizeof(*desc)) {
278 desc = (struct sof_probe_point_desc *)&array[1];
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);
286 ret = ipc->points_add(cdev, desc, bytes / sizeof(*desc));
290 pm_runtime_mark_last_busy(dev);
291 err = pm_runtime_put_autosuspend(dev);
293 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
299 static const struct file_operations sof_probes_points_fops = {
301 .read = sof_probes_dfs_points_read,
302 .write = sof_probes_dfs_points_write,
303 .llseek = default_llseek,
305 .owner = THIS_MODULE,
309 sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
310 size_t count, loff_t *ppos)
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;
319 if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
320 dev_warn(dev, "no extractor stream running\n");
324 ret = parse_int_array_user(from, count, (int **)&array);
328 ret = pm_runtime_resume_and_get(dev);
330 dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
334 ret = ipc->points_remove(cdev, &array[1], array[0]);
338 pm_runtime_mark_last_busy(dev);
339 err = pm_runtime_put_autosuspend(dev);
341 dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
347 static const struct file_operations sof_probes_points_remove_fops = {
349 .write = sof_probes_dfs_points_remove_write,
350 .llseek = default_llseek,
352 .owner = THIS_MODULE,
355 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
357 .name = "Probe Extraction CPU DAI",
358 .compress_new = snd_soc_new_compress,
359 .cops = &sof_probes_compr_ops,
361 .stream_name = "Probe Extraction",
364 .rates = SNDRV_PCM_RATE_48000,
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,
378 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
380 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
381 const struct auxiliary_device_id *id)
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[] = {
388 .name = dev_name(dev),
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;
398 /* do not set up the probes support if it is not enabled */
399 if (!sof_probes_enabled)
402 ops = dev_get_platdata(dev);
404 dev_err(dev, "missing platform data\n");
407 if (!ops->startup || !ops->shutdown || !ops->set_params || !ops->trigger ||
409 dev_err(dev, "missing platform callback(s)\n");
413 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
417 priv->host_ops = ops;
419 switch (sof_client_get_ipc_type(cdev)) {
420 #ifdef CONFIG_SND_SOC_SOF_INTEL_IPC4
422 priv->ipc_ops = &ipc4_probe_ops;
425 #ifdef CONFIG_SND_SOC_SOF_IPC3
427 priv->ipc_ops = &ipc3_probe_ops;
431 dev_err(dev, "Matching IPC ops not found.");
437 /* register probes component driver and dai */
438 ret = devm_snd_soc_register_component(dev, &sof_probes_component,
440 ARRAY_SIZE(sof_probes_dai_drv));
442 dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
446 /* set client data */
447 priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
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);
453 /* create read-write probe_points_remove debugfs entry */
454 priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
456 &sof_probes_points_remove_fops);
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);
466 /* extraction DAI link */
467 links[0].name = "Compress Probe Capture";
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;
481 card->name = "sof-probes";
482 card->owner = THIS_MODULE;
483 card->num_links = SOF_PROBES_NUM_DAI_LINKS;
484 card->dai_link = links;
486 /* set idle_bias_off to prevent the core from resuming the card->dev */
487 card->dapm.idle_bias_off = true;
489 snd_soc_card_set_drvdata(card, cdev);
491 ret = devm_snd_soc_register_card(dev, card);
493 debugfs_remove(priv->dfs_points);
494 debugfs_remove(priv->dfs_points_remove);
495 dev_err(dev, "Probes card register failed %d\n", ret);
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);
509 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
511 struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
512 struct sof_probes_priv *priv = cdev->data;
514 if (!sof_probes_enabled)
517 pm_runtime_disable(&auxdev->dev);
518 debugfs_remove(priv->dfs_points);
519 debugfs_remove(priv->dfs_points_remove);
522 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
523 { .name = "snd_sof.hda-probes", },
526 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
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,
533 .id_table = sof_probes_client_id_table,
536 module_auxiliary_driver(sof_probes_client_drv);
538 MODULE_DESCRIPTION("SOF Probes Client Driver");
539 MODULE_LICENSE("GPL v2");
540 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);