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