sof-client-ipc-flood-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2022 Intel Corporation. All rights reserved.
  4. //
  5. // Authors: Ranjani Sridharan <[email protected]>
  6. // Peter Ujfalusi <[email protected]>
  7. //
  8. #include <linux/auxiliary_bus.h>
  9. #include <linux/completion.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/ktime.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include <sound/sof/header.h>
  18. #include "sof-client.h"
  19. #define MAX_IPC_FLOOD_DURATION_MS 1000
  20. #define MAX_IPC_FLOOD_COUNT 10000
  21. #define IPC_FLOOD_TEST_RESULT_LEN 512
  22. #define SOF_IPC_CLIENT_SUSPEND_DELAY_MS 3000
  23. #define DEBUGFS_IPC_FLOOD_COUNT "ipc_flood_count"
  24. #define DEBUGFS_IPC_FLOOD_DURATION "ipc_flood_duration_ms"
  25. struct sof_ipc_flood_priv {
  26. struct dentry *dfs_root;
  27. struct dentry *dfs_link[2];
  28. char *buf;
  29. };
  30. static int sof_ipc_flood_dfs_open(struct inode *inode, struct file *file)
  31. {
  32. struct sof_client_dev *cdev = inode->i_private;
  33. int ret;
  34. if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
  35. return -ENODEV;
  36. ret = debugfs_file_get(file->f_path.dentry);
  37. if (unlikely(ret))
  38. return ret;
  39. ret = simple_open(inode, file);
  40. if (ret)
  41. debugfs_file_put(file->f_path.dentry);
  42. return ret;
  43. }
  44. /*
  45. * helper function to perform the flood test. Only one of the two params, ipc_duration_ms
  46. * or ipc_count, will be non-zero and will determine the type of test
  47. */
  48. static int sof_debug_ipc_flood_test(struct sof_client_dev *cdev,
  49. bool flood_duration_test,
  50. unsigned long ipc_duration_ms,
  51. unsigned long ipc_count)
  52. {
  53. struct sof_ipc_flood_priv *priv = cdev->data;
  54. struct device *dev = &cdev->auxdev.dev;
  55. struct sof_ipc_cmd_hdr hdr;
  56. struct sof_ipc_reply reply;
  57. u64 min_response_time = U64_MAX;
  58. ktime_t start, end, test_end;
  59. u64 avg_response_time = 0;
  60. u64 max_response_time = 0;
  61. u64 ipc_response_time;
  62. int i = 0;
  63. int ret;
  64. /* configure test IPC */
  65. hdr.cmd = SOF_IPC_GLB_TEST_MSG | SOF_IPC_TEST_IPC_FLOOD;
  66. hdr.size = sizeof(hdr);
  67. /* set test end time for duration flood test */
  68. if (flood_duration_test)
  69. test_end = ktime_get_ns() + ipc_duration_ms * NSEC_PER_MSEC;
  70. /* send test IPC's */
  71. while (1) {
  72. start = ktime_get();
  73. ret = sof_client_ipc_tx_message(cdev, &hdr, &reply, sizeof(reply));
  74. end = ktime_get();
  75. if (ret < 0)
  76. break;
  77. /* compute min and max response times */
  78. ipc_response_time = ktime_to_ns(ktime_sub(end, start));
  79. min_response_time = min(min_response_time, ipc_response_time);
  80. max_response_time = max(max_response_time, ipc_response_time);
  81. /* sum up response times */
  82. avg_response_time += ipc_response_time;
  83. i++;
  84. /* test complete? */
  85. if (flood_duration_test) {
  86. if (ktime_to_ns(end) >= test_end)
  87. break;
  88. } else {
  89. if (i == ipc_count)
  90. break;
  91. }
  92. }
  93. if (ret < 0)
  94. dev_err(dev, "ipc flood test failed at %d iterations\n", i);
  95. /* return if the first IPC fails */
  96. if (!i)
  97. return ret;
  98. /* compute average response time */
  99. do_div(avg_response_time, i);
  100. /* clear previous test output */
  101. memset(priv->buf, 0, IPC_FLOOD_TEST_RESULT_LEN);
  102. if (!ipc_count) {
  103. dev_dbg(dev, "IPC Flood test duration: %lums\n", ipc_duration_ms);
  104. snprintf(priv->buf, IPC_FLOOD_TEST_RESULT_LEN,
  105. "IPC Flood test duration: %lums\n", ipc_duration_ms);
  106. }
  107. dev_dbg(dev, "IPC Flood count: %d, Avg response time: %lluns\n",
  108. i, avg_response_time);
  109. dev_dbg(dev, "Max response time: %lluns\n", max_response_time);
  110. dev_dbg(dev, "Min response time: %lluns\n", min_response_time);
  111. /* format output string and save test results */
  112. snprintf(priv->buf + strlen(priv->buf),
  113. IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf),
  114. "IPC Flood count: %d\nAvg response time: %lluns\n",
  115. i, avg_response_time);
  116. snprintf(priv->buf + strlen(priv->buf),
  117. IPC_FLOOD_TEST_RESULT_LEN - strlen(priv->buf),
  118. "Max response time: %lluns\nMin response time: %lluns\n",
  119. max_response_time, min_response_time);
  120. return ret;
  121. }
  122. /*
  123. * Writing to the debugfs entry initiates the IPC flood test based on
  124. * the IPC count or the duration specified by the user.
  125. */
  126. static ssize_t sof_ipc_flood_dfs_write(struct file *file, const char __user *buffer,
  127. size_t count, loff_t *ppos)
  128. {
  129. struct sof_client_dev *cdev = file->private_data;
  130. struct device *dev = &cdev->auxdev.dev;
  131. unsigned long ipc_duration_ms = 0;
  132. bool flood_duration_test = false;
  133. unsigned long ipc_count = 0;
  134. struct dentry *dentry;
  135. int err;
  136. size_t size;
  137. char *string;
  138. int ret;
  139. string = kzalloc(count + 1, GFP_KERNEL);
  140. if (!string)
  141. return -ENOMEM;
  142. size = simple_write_to_buffer(string, count, ppos, buffer, count);
  143. /*
  144. * write op is only supported for ipc_flood_count or
  145. * ipc_flood_duration_ms debugfs entries atm.
  146. * ipc_flood_count floods the DSP with the number of IPC's specified.
  147. * ipc_duration_ms test floods the DSP for the time specified
  148. * in the debugfs entry.
  149. */
  150. dentry = file->f_path.dentry;
  151. if (strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_COUNT) &&
  152. strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION)) {
  153. ret = -EINVAL;
  154. goto out;
  155. }
  156. if (!strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION))
  157. flood_duration_test = true;
  158. /* test completion criterion */
  159. if (flood_duration_test)
  160. ret = kstrtoul(string, 0, &ipc_duration_ms);
  161. else
  162. ret = kstrtoul(string, 0, &ipc_count);
  163. if (ret < 0)
  164. goto out;
  165. /* limit max duration/ipc count for flood test */
  166. if (flood_duration_test) {
  167. if (!ipc_duration_ms) {
  168. ret = size;
  169. goto out;
  170. }
  171. /* find the minimum. min() is not used to avoid warnings */
  172. if (ipc_duration_ms > MAX_IPC_FLOOD_DURATION_MS)
  173. ipc_duration_ms = MAX_IPC_FLOOD_DURATION_MS;
  174. } else {
  175. if (!ipc_count) {
  176. ret = size;
  177. goto out;
  178. }
  179. /* find the minimum. min() is not used to avoid warnings */
  180. if (ipc_count > MAX_IPC_FLOOD_COUNT)
  181. ipc_count = MAX_IPC_FLOOD_COUNT;
  182. }
  183. ret = pm_runtime_resume_and_get(dev);
  184. if (ret < 0 && ret != -EACCES) {
  185. dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
  186. goto out;
  187. }
  188. /* flood test */
  189. ret = sof_debug_ipc_flood_test(cdev, flood_duration_test,
  190. ipc_duration_ms, ipc_count);
  191. pm_runtime_mark_last_busy(dev);
  192. err = pm_runtime_put_autosuspend(dev);
  193. if (err < 0)
  194. dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
  195. /* return size if test is successful */
  196. if (ret >= 0)
  197. ret = size;
  198. out:
  199. kfree(string);
  200. return ret;
  201. }
  202. /* return the result of the last IPC flood test */
  203. static ssize_t sof_ipc_flood_dfs_read(struct file *file, char __user *buffer,
  204. size_t count, loff_t *ppos)
  205. {
  206. struct sof_client_dev *cdev = file->private_data;
  207. struct sof_ipc_flood_priv *priv = cdev->data;
  208. size_t size_ret;
  209. struct dentry *dentry;
  210. dentry = file->f_path.dentry;
  211. if (!strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_COUNT) ||
  212. !strcmp(dentry->d_name.name, DEBUGFS_IPC_FLOOD_DURATION)) {
  213. if (*ppos)
  214. return 0;
  215. count = min_t(size_t, count, strlen(priv->buf));
  216. size_ret = copy_to_user(buffer, priv->buf, count);
  217. if (size_ret)
  218. return -EFAULT;
  219. *ppos += count;
  220. return count;
  221. }
  222. return count;
  223. }
  224. static int sof_ipc_flood_dfs_release(struct inode *inode, struct file *file)
  225. {
  226. debugfs_file_put(file->f_path.dentry);
  227. return 0;
  228. }
  229. static const struct file_operations sof_ipc_flood_fops = {
  230. .open = sof_ipc_flood_dfs_open,
  231. .read = sof_ipc_flood_dfs_read,
  232. .llseek = default_llseek,
  233. .write = sof_ipc_flood_dfs_write,
  234. .release = sof_ipc_flood_dfs_release,
  235. .owner = THIS_MODULE,
  236. };
  237. /*
  238. * The IPC test client creates a couple of debugfs entries that will be used
  239. * flood tests. Users can write to these entries to execute the IPC flood test
  240. * by specifying either the number of IPCs to flood the DSP with or the duration
  241. * (in ms) for which the DSP should be flooded with test IPCs. At the
  242. * end of each test, the average, min and max response times are reported back.
  243. * The results of the last flood test can be accessed by reading the debugfs
  244. * entries.
  245. */
  246. static int sof_ipc_flood_probe(struct auxiliary_device *auxdev,
  247. const struct auxiliary_device_id *id)
  248. {
  249. struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
  250. struct dentry *debugfs_root = sof_client_get_debugfs_root(cdev);
  251. struct device *dev = &auxdev->dev;
  252. struct sof_ipc_flood_priv *priv;
  253. /* allocate memory for client data */
  254. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  255. if (!priv)
  256. return -ENOMEM;
  257. priv->buf = devm_kmalloc(dev, IPC_FLOOD_TEST_RESULT_LEN, GFP_KERNEL);
  258. if (!priv->buf)
  259. return -ENOMEM;
  260. cdev->data = priv;
  261. /* create debugfs root folder with device name under parent SOF dir */
  262. priv->dfs_root = debugfs_create_dir(dev_name(dev), debugfs_root);
  263. if (!IS_ERR_OR_NULL(priv->dfs_root)) {
  264. /* create read-write ipc_flood_count debugfs entry */
  265. debugfs_create_file(DEBUGFS_IPC_FLOOD_COUNT, 0644, priv->dfs_root,
  266. cdev, &sof_ipc_flood_fops);
  267. /* create read-write ipc_flood_duration_ms debugfs entry */
  268. debugfs_create_file(DEBUGFS_IPC_FLOOD_DURATION, 0644,
  269. priv->dfs_root, cdev, &sof_ipc_flood_fops);
  270. if (auxdev->id == 0) {
  271. /*
  272. * Create symlinks for backwards compatibility to the
  273. * first IPC flood test instance
  274. */
  275. char target[100];
  276. snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_COUNT,
  277. dev_name(dev));
  278. priv->dfs_link[0] =
  279. debugfs_create_symlink(DEBUGFS_IPC_FLOOD_COUNT,
  280. debugfs_root, target);
  281. snprintf(target, 100, "%s/" DEBUGFS_IPC_FLOOD_DURATION,
  282. dev_name(dev));
  283. priv->dfs_link[1] =
  284. debugfs_create_symlink(DEBUGFS_IPC_FLOOD_DURATION,
  285. debugfs_root, target);
  286. }
  287. }
  288. /* enable runtime PM */
  289. pm_runtime_set_autosuspend_delay(dev, SOF_IPC_CLIENT_SUSPEND_DELAY_MS);
  290. pm_runtime_use_autosuspend(dev);
  291. pm_runtime_enable(dev);
  292. pm_runtime_mark_last_busy(dev);
  293. pm_runtime_idle(dev);
  294. return 0;
  295. }
  296. static void sof_ipc_flood_remove(struct auxiliary_device *auxdev)
  297. {
  298. struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
  299. struct sof_ipc_flood_priv *priv = cdev->data;
  300. pm_runtime_disable(&auxdev->dev);
  301. if (auxdev->id == 0) {
  302. debugfs_remove(priv->dfs_link[0]);
  303. debugfs_remove(priv->dfs_link[1]);
  304. }
  305. debugfs_remove_recursive(priv->dfs_root);
  306. }
  307. static const struct auxiliary_device_id sof_ipc_flood_client_id_table[] = {
  308. { .name = "snd_sof.ipc_flood" },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(auxiliary, sof_ipc_flood_client_id_table);
  312. /*
  313. * No need for driver pm_ops as the generic pm callbacks in the auxiliary bus
  314. * type are enough to ensure that the parent SOF device resumes to bring the DSP
  315. * back to D0.
  316. * Driver name will be set based on KBUILD_MODNAME.
  317. */
  318. static struct auxiliary_driver sof_ipc_flood_client_drv = {
  319. .probe = sof_ipc_flood_probe,
  320. .remove = sof_ipc_flood_remove,
  321. .id_table = sof_ipc_flood_client_id_table,
  322. };
  323. module_auxiliary_driver(sof_ipc_flood_client_drv);
  324. MODULE_DESCRIPTION("SOF IPC Flood Test Client Driver");
  325. MODULE_LICENSE("GPL");
  326. MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);