sof-client-probes.c 16 KB

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