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

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