1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
10 // Generic debug routines used to export DSP MMIO and memories to userspace
11 // for firmware debugging.
14 #include <linux/debugfs.h>
16 #include <linux/pm_runtime.h>
20 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
24 * strsplit_u32 - Split string into sequence of u32 tokens
25 * @buf: String to split into tokens.
26 * @delim: String containing delimiter characters.
27 * @tkns: Returned u32 sequence pointer.
28 * @num_tkns: Returned number of tokens obtained.
31 strsplit_u32(char **buf, const char *delim, u32 **tkns, size_t *num_tkns)
41 data = kcalloc(cap, sizeof(*data), GFP_KERNEL);
45 while ((s = strsep(buf, delim)) != NULL) {
46 ret = kstrtouint(s, 0, data + count);
51 tmp = krealloc(data, cap * sizeof(*data), GFP_KERNEL);
62 *tkns = kmemdup(data, count * sizeof(*data), GFP_KERNEL);
74 static int tokenize_input(const char __user *from, size_t count,
75 loff_t *ppos, u32 **tkns, size_t *num_tkns)
80 buf = kmalloc(count + 1, GFP_KERNEL);
84 ret = simple_write_to_buffer(buf, count, ppos, from, count);
86 ret = ret >= 0 ? -EIO : ret;
91 ret = strsplit_u32((char **)&buf, ",", tkns, num_tkns);
97 static ssize_t probe_points_read(struct file *file,
98 char __user *to, size_t count, loff_t *ppos)
100 struct snd_sof_dfsentry *dfse = file->private_data;
101 struct snd_sof_dev *sdev = dfse->sdev;
102 struct sof_probe_point_desc *desc;
103 size_t num_desc, len = 0;
107 if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
108 dev_warn(sdev->dev, "no extractor stream running\n");
112 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
116 ret = sof_ipc_probe_points_info(sdev, &desc, &num_desc);
120 for (i = 0; i < num_desc; i++) {
121 ret = snprintf(buf + len, PAGE_SIZE - len,
122 "Id: %#010x Purpose: %d Node id: %#x\n",
123 desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
129 ret = simple_read_from_buffer(to, count, ppos, buf, len);
137 static ssize_t probe_points_write(struct file *file,
138 const char __user *from, size_t count, loff_t *ppos)
140 struct snd_sof_dfsentry *dfse = file->private_data;
141 struct snd_sof_dev *sdev = dfse->sdev;
142 struct sof_probe_point_desc *desc;
143 size_t num_tkns, bytes;
147 if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
148 dev_warn(sdev->dev, "no extractor stream running\n");
152 ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
155 bytes = sizeof(*tkns) * num_tkns;
156 if (!num_tkns || (bytes % sizeof(*desc))) {
161 desc = (struct sof_probe_point_desc *)tkns;
162 ret = sof_ipc_probe_points_add(sdev,
163 desc, bytes / sizeof(*desc));
171 static const struct file_operations probe_points_fops = {
173 .read = probe_points_read,
174 .write = probe_points_write,
175 .llseek = default_llseek,
178 static ssize_t probe_points_remove_write(struct file *file,
179 const char __user *from, size_t count, loff_t *ppos)
181 struct snd_sof_dfsentry *dfse = file->private_data;
182 struct snd_sof_dev *sdev = dfse->sdev;
187 if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
188 dev_warn(sdev->dev, "no extractor stream running\n");
192 ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
200 ret = sof_ipc_probe_points_remove(sdev, tkns, num_tkns);
208 static const struct file_operations probe_points_remove_fops = {
210 .write = probe_points_remove_write,
211 .llseek = default_llseek,
214 static int snd_sof_debugfs_probe_item(struct snd_sof_dev *sdev,
215 const char *name, mode_t mode,
216 const struct file_operations *fops)
218 struct snd_sof_dfsentry *dfse;
220 dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
224 dfse->type = SOF_DFSENTRY_TYPE_BUF;
227 debugfs_create_file(name, mode, sdev->debugfs_root, dfse, fops);
228 /* add to dfsentry list */
229 list_add(&dfse->list, &sdev->dfsentry_list);
235 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
236 #define MAX_IPC_FLOOD_DURATION_MS 1000
237 #define MAX_IPC_FLOOD_COUNT 10000
238 #define IPC_FLOOD_TEST_RESULT_LEN 512
240 static int sof_debug_ipc_flood_test(struct snd_sof_dev *sdev,
241 struct snd_sof_dfsentry *dfse,
242 bool flood_duration_test,
243 unsigned long ipc_duration_ms,
244 unsigned long ipc_count)
246 struct sof_ipc_cmd_hdr hdr;
247 struct sof_ipc_reply reply;
248 u64 min_response_time = U64_MAX;
249 ktime_t start, end, test_end;
250 u64 avg_response_time = 0;
251 u64 max_response_time = 0;
252 u64 ipc_response_time;
256 /* configure test IPC */
257 hdr.cmd = SOF_IPC_GLB_TEST_MSG | SOF_IPC_TEST_IPC_FLOOD;
258 hdr.size = sizeof(hdr);
260 /* set test end time for duration flood test */
261 if (flood_duration_test)
262 test_end = ktime_get_ns() + ipc_duration_ms * NSEC_PER_MSEC;
264 /* send test IPC's */
267 ret = sof_ipc_tx_message(sdev->ipc, hdr.cmd, &hdr, hdr.size,
268 &reply, sizeof(reply));
274 /* compute min and max response times */
275 ipc_response_time = ktime_to_ns(ktime_sub(end, start));
276 min_response_time = min(min_response_time, ipc_response_time);
277 max_response_time = max(max_response_time, ipc_response_time);
279 /* sum up response times */
280 avg_response_time += ipc_response_time;
284 if (flood_duration_test) {
285 if (ktime_to_ns(end) >= test_end)
295 "error: ipc flood test failed at %d iterations\n", i);
297 /* return if the first IPC fails */
301 /* compute average response time */
302 do_div(avg_response_time, i);
304 /* clear previous test output */
305 memset(dfse->cache_buf, 0, IPC_FLOOD_TEST_RESULT_LEN);
307 if (flood_duration_test) {
308 dev_dbg(sdev->dev, "IPC Flood test duration: %lums\n",
310 snprintf(dfse->cache_buf, IPC_FLOOD_TEST_RESULT_LEN,
311 "IPC Flood test duration: %lums\n", ipc_duration_ms);
315 "IPC Flood count: %d, Avg response time: %lluns\n",
316 i, avg_response_time);
317 dev_dbg(sdev->dev, "Max response time: %lluns\n",
319 dev_dbg(sdev->dev, "Min response time: %lluns\n",
322 /* format output string */
323 snprintf(dfse->cache_buf + strlen(dfse->cache_buf),
324 IPC_FLOOD_TEST_RESULT_LEN - strlen(dfse->cache_buf),
325 "IPC Flood count: %d\nAvg response time: %lluns\n",
326 i, avg_response_time);
328 snprintf(dfse->cache_buf + strlen(dfse->cache_buf),
329 IPC_FLOOD_TEST_RESULT_LEN - strlen(dfse->cache_buf),
330 "Max response time: %lluns\nMin response time: %lluns\n",
331 max_response_time, min_response_time);
337 static ssize_t sof_dfsentry_write(struct file *file, const char __user *buffer,
338 size_t count, loff_t *ppos)
340 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
341 struct snd_sof_dfsentry *dfse = file->private_data;
342 struct snd_sof_dev *sdev = dfse->sdev;
343 unsigned long ipc_duration_ms = 0;
344 bool flood_duration_test = false;
345 unsigned long ipc_count = 0;
346 struct dentry *dentry;
353 string = kzalloc(count, GFP_KERNEL);
357 size = simple_write_to_buffer(string, count, ppos, buffer, count);
360 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
362 * write op is only supported for ipc_flood_count or
363 * ipc_flood_duration_ms debugfs entries atm.
364 * ipc_flood_count floods the DSP with the number of IPC's specified.
365 * ipc_duration_ms test floods the DSP for the time specified
366 * in the debugfs entry.
368 dentry = file->f_path.dentry;
369 if (strcmp(dentry->d_name.name, "ipc_flood_count") &&
370 strcmp(dentry->d_name.name, "ipc_flood_duration_ms")) {
375 if (!strcmp(dentry->d_name.name, "ipc_flood_duration_ms"))
376 flood_duration_test = true;
378 /* test completion criterion */
379 if (flood_duration_test)
380 ret = kstrtoul(string, 0, &ipc_duration_ms);
382 ret = kstrtoul(string, 0, &ipc_count);
386 /* limit max duration/ipc count for flood test */
387 if (flood_duration_test) {
388 if (!ipc_duration_ms) {
393 /* find the minimum. min() is not used to avoid warnings */
394 if (ipc_duration_ms > MAX_IPC_FLOOD_DURATION_MS)
395 ipc_duration_ms = MAX_IPC_FLOOD_DURATION_MS;
402 /* find the minimum. min() is not used to avoid warnings */
403 if (ipc_count > MAX_IPC_FLOOD_COUNT)
404 ipc_count = MAX_IPC_FLOOD_COUNT;
407 ret = pm_runtime_get_sync(sdev->dev);
409 dev_err_ratelimited(sdev->dev,
410 "error: debugfs write failed to resume %d\n",
412 pm_runtime_put_noidle(sdev->dev);
417 ret = sof_debug_ipc_flood_test(sdev, dfse, flood_duration_test,
418 ipc_duration_ms, ipc_count);
420 pm_runtime_mark_last_busy(sdev->dev);
421 err = pm_runtime_put_autosuspend(sdev->dev);
423 dev_err_ratelimited(sdev->dev,
424 "error: debugfs write failed to idle %d\n",
427 /* return size if test is successful */
436 static ssize_t sof_dfsentry_read(struct file *file, char __user *buffer,
437 size_t count, loff_t *ppos)
439 struct snd_sof_dfsentry *dfse = file->private_data;
440 struct snd_sof_dev *sdev = dfse->sdev;
447 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
448 struct dentry *dentry;
450 dentry = file->f_path.dentry;
451 if ((!strcmp(dentry->d_name.name, "ipc_flood_count") ||
452 !strcmp(dentry->d_name.name, "ipc_flood_duration_ms")) &&
457 count = strlen(dfse->cache_buf);
458 size_ret = copy_to_user(buffer, dfse->cache_buf, count);
468 /* validate position & count */
471 if (pos >= size || !count)
473 /* find the minimum. min() is not used since it adds sparse warnings */
474 if (count > size - pos)
477 /* align io read start to u32 multiple */
478 pos = ALIGN_DOWN(pos, 4);
480 /* intermediate buffer size must be u32 multiple */
481 size = ALIGN(count, 4);
483 /* if start position is unaligned, read extra u32 */
484 if (unlikely(pos != *ppos)) {
486 if (pos + size + 4 < dfse->size)
490 buf = kzalloc(size, GFP_KERNEL);
494 if (dfse->type == SOF_DFSENTRY_TYPE_IOMEM) {
495 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
497 * If the DSP is active: copy from IO.
498 * If the DSP is suspended:
499 * - Copy from IO if the memory is always accessible.
500 * - Otherwise, copy from cached buffer.
502 if (pm_runtime_active(sdev->dev) ||
503 dfse->access_type == SOF_DEBUGFS_ACCESS_ALWAYS) {
504 memcpy_fromio(buf, dfse->io_mem + pos, size);
507 "Copying cached debugfs data\n");
508 memcpy(buf, dfse->cache_buf + pos, size);
511 /* if the DSP is in D3 */
512 if (!pm_runtime_active(sdev->dev) &&
513 dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) {
515 "error: debugfs entry cannot be read in DSP D3\n");
520 memcpy_fromio(buf, dfse->io_mem + pos, size);
523 memcpy(buf, ((u8 *)(dfse->buf) + pos), size);
526 /* copy to userspace */
527 size_ret = copy_to_user(buffer, buf + skip, count);
531 /* update count & position if copy succeeded */
540 static const struct file_operations sof_dfs_fops = {
542 .read = sof_dfsentry_read,
543 .llseek = default_llseek,
544 .write = sof_dfsentry_write,
547 /* create FS entry for debug files that can expose DSP memories, registers */
548 int snd_sof_debugfs_io_item(struct snd_sof_dev *sdev,
549 void __iomem *base, size_t size,
551 enum sof_debugfs_access_type access_type)
553 struct snd_sof_dfsentry *dfse;
558 dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
562 dfse->type = SOF_DFSENTRY_TYPE_IOMEM;
566 dfse->access_type = access_type;
568 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
570 * allocate cache buffer that will be used to save the mem window
571 * contents prior to suspend
573 if (access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) {
574 dfse->cache_buf = devm_kzalloc(sdev->dev, size, GFP_KERNEL);
575 if (!dfse->cache_buf)
580 debugfs_create_file(name, 0444, sdev->debugfs_root, dfse,
583 /* add to dfsentry list */
584 list_add(&dfse->list, &sdev->dfsentry_list);
588 EXPORT_SYMBOL_GPL(snd_sof_debugfs_io_item);
590 /* create FS entry for debug files to expose kernel memory */
591 int snd_sof_debugfs_buf_item(struct snd_sof_dev *sdev,
592 void *base, size_t size,
593 const char *name, mode_t mode)
595 struct snd_sof_dfsentry *dfse;
600 dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
604 dfse->type = SOF_DFSENTRY_TYPE_BUF;
609 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
611 * cache_buf is unused for SOF_DFSENTRY_TYPE_BUF debugfs entries.
612 * So, use it to save the results of the last IPC flood test.
614 dfse->cache_buf = devm_kzalloc(sdev->dev, IPC_FLOOD_TEST_RESULT_LEN,
616 if (!dfse->cache_buf)
620 debugfs_create_file(name, mode, sdev->debugfs_root, dfse,
622 /* add to dfsentry list */
623 list_add(&dfse->list, &sdev->dfsentry_list);
627 EXPORT_SYMBOL_GPL(snd_sof_debugfs_buf_item);
629 int snd_sof_dbg_init(struct snd_sof_dev *sdev)
631 const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
632 const struct snd_sof_debugfs_map *map;
636 /* use "sof" as top level debugFS dir */
637 sdev->debugfs_root = debugfs_create_dir("sof", NULL);
639 /* init dfsentry list */
640 INIT_LIST_HEAD(&sdev->dfsentry_list);
642 /* create debugFS files for platform specific MMIO/DSP memories */
643 for (i = 0; i < ops->debug_map_count; i++) {
644 map = &ops->debug_map[i];
646 err = snd_sof_debugfs_io_item(sdev, sdev->bar[map->bar] +
647 map->offset, map->size,
648 map->name, map->access_type);
649 /* errors are only due to memory allocation, not debugfs */
654 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
655 err = snd_sof_debugfs_probe_item(sdev, "probe_points",
656 0644, &probe_points_fops);
659 err = snd_sof_debugfs_probe_item(sdev, "probe_points_remove",
660 0200, &probe_points_remove_fops);
665 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
666 /* create read-write ipc_flood_count debugfs entry */
667 err = snd_sof_debugfs_buf_item(sdev, NULL, 0,
668 "ipc_flood_count", 0666);
670 /* errors are only due to memory allocation, not debugfs */
674 /* create read-write ipc_flood_duration_ms debugfs entry */
675 err = snd_sof_debugfs_buf_item(sdev, NULL, 0,
676 "ipc_flood_duration_ms", 0666);
678 /* errors are only due to memory allocation, not debugfs */
685 EXPORT_SYMBOL_GPL(snd_sof_dbg_init);
687 void snd_sof_free_debug(struct snd_sof_dev *sdev)
689 debugfs_remove_recursive(sdev->debugfs_root);
691 EXPORT_SYMBOL_GPL(snd_sof_free_debug);
693 void snd_sof_handle_fw_exception(struct snd_sof_dev *sdev)
695 if (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_RETAIN_DSP_CONTEXT) ||
696 (sof_core_debug & SOF_DBG_RETAIN_CTX)) {
697 /* should we prevent DSP entering D3 ? */
698 dev_info(sdev->dev, "info: preventing DSP entering D3 state to preserve context\n");
699 pm_runtime_get_noresume(sdev->dev);
702 /* dump vital information to the logs */
703 snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX);
704 snd_sof_ipc_dump(sdev);
705 snd_sof_trace_notify_for_error(sdev);
707 EXPORT_SYMBOL(snd_sof_handle_fw_exception);