debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  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.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <[email protected]>
  9. //
  10. // Generic debug routines used to export DSP MMIO and memories to userspace
  11. // for firmware debugging.
  12. //
  13. #include <linux/debugfs.h>
  14. #include <linux/io.h>
  15. #include <linux/pm_runtime.h>
  16. #include <sound/sof/ext_manifest.h>
  17. #include <sound/sof/debug.h>
  18. #include "sof-priv.h"
  19. #include "ops.h"
  20. static ssize_t sof_dfsentry_write(struct file *file, const char __user *buffer,
  21. size_t count, loff_t *ppos)
  22. {
  23. size_t size;
  24. char *string;
  25. int ret;
  26. string = kzalloc(count+1, GFP_KERNEL);
  27. if (!string)
  28. return -ENOMEM;
  29. size = simple_write_to_buffer(string, count, ppos, buffer, count);
  30. ret = size;
  31. kfree(string);
  32. return ret;
  33. }
  34. static ssize_t sof_dfsentry_read(struct file *file, char __user *buffer,
  35. size_t count, loff_t *ppos)
  36. {
  37. struct snd_sof_dfsentry *dfse = file->private_data;
  38. struct snd_sof_dev *sdev = dfse->sdev;
  39. loff_t pos = *ppos;
  40. size_t size_ret;
  41. int skip = 0;
  42. int size;
  43. u8 *buf;
  44. size = dfse->size;
  45. /* validate position & count */
  46. if (pos < 0)
  47. return -EINVAL;
  48. if (pos >= size || !count)
  49. return 0;
  50. /* find the minimum. min() is not used since it adds sparse warnings */
  51. if (count > size - pos)
  52. count = size - pos;
  53. /* align io read start to u32 multiple */
  54. pos = ALIGN_DOWN(pos, 4);
  55. /* intermediate buffer size must be u32 multiple */
  56. size = ALIGN(count, 4);
  57. /* if start position is unaligned, read extra u32 */
  58. if (unlikely(pos != *ppos)) {
  59. skip = *ppos - pos;
  60. if (pos + size + 4 < dfse->size)
  61. size += 4;
  62. }
  63. buf = kzalloc(size, GFP_KERNEL);
  64. if (!buf)
  65. return -ENOMEM;
  66. if (dfse->type == SOF_DFSENTRY_TYPE_IOMEM) {
  67. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
  68. /*
  69. * If the DSP is active: copy from IO.
  70. * If the DSP is suspended:
  71. * - Copy from IO if the memory is always accessible.
  72. * - Otherwise, copy from cached buffer.
  73. */
  74. if (pm_runtime_active(sdev->dev) ||
  75. dfse->access_type == SOF_DEBUGFS_ACCESS_ALWAYS) {
  76. memcpy_fromio(buf, dfse->io_mem + pos, size);
  77. } else {
  78. dev_info(sdev->dev,
  79. "Copying cached debugfs data\n");
  80. memcpy(buf, dfse->cache_buf + pos, size);
  81. }
  82. #else
  83. /* if the DSP is in D3 */
  84. if (!pm_runtime_active(sdev->dev) &&
  85. dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) {
  86. dev_err(sdev->dev,
  87. "error: debugfs entry cannot be read in DSP D3\n");
  88. kfree(buf);
  89. return -EINVAL;
  90. }
  91. memcpy_fromio(buf, dfse->io_mem + pos, size);
  92. #endif
  93. } else {
  94. memcpy(buf, ((u8 *)(dfse->buf) + pos), size);
  95. }
  96. /* copy to userspace */
  97. size_ret = copy_to_user(buffer, buf + skip, count);
  98. kfree(buf);
  99. /* update count & position if copy succeeded */
  100. if (size_ret)
  101. return -EFAULT;
  102. *ppos = pos + count;
  103. return count;
  104. }
  105. static const struct file_operations sof_dfs_fops = {
  106. .open = simple_open,
  107. .read = sof_dfsentry_read,
  108. .llseek = default_llseek,
  109. .write = sof_dfsentry_write,
  110. };
  111. /* create FS entry for debug files that can expose DSP memories, registers */
  112. static int snd_sof_debugfs_io_item(struct snd_sof_dev *sdev,
  113. void __iomem *base, size_t size,
  114. const char *name,
  115. enum sof_debugfs_access_type access_type)
  116. {
  117. struct snd_sof_dfsentry *dfse;
  118. if (!sdev)
  119. return -EINVAL;
  120. dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
  121. if (!dfse)
  122. return -ENOMEM;
  123. dfse->type = SOF_DFSENTRY_TYPE_IOMEM;
  124. dfse->io_mem = base;
  125. dfse->size = size;
  126. dfse->sdev = sdev;
  127. dfse->access_type = access_type;
  128. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
  129. /*
  130. * allocate cache buffer that will be used to save the mem window
  131. * contents prior to suspend
  132. */
  133. if (access_type == SOF_DEBUGFS_ACCESS_D0_ONLY) {
  134. dfse->cache_buf = devm_kzalloc(sdev->dev, size, GFP_KERNEL);
  135. if (!dfse->cache_buf)
  136. return -ENOMEM;
  137. }
  138. #endif
  139. debugfs_create_file(name, 0444, sdev->debugfs_root, dfse,
  140. &sof_dfs_fops);
  141. /* add to dfsentry list */
  142. list_add(&dfse->list, &sdev->dfsentry_list);
  143. return 0;
  144. }
  145. int snd_sof_debugfs_add_region_item_iomem(struct snd_sof_dev *sdev,
  146. enum snd_sof_fw_blk_type blk_type, u32 offset,
  147. size_t size, const char *name,
  148. enum sof_debugfs_access_type access_type)
  149. {
  150. int bar = snd_sof_dsp_get_bar_index(sdev, blk_type);
  151. if (bar < 0)
  152. return bar;
  153. return snd_sof_debugfs_io_item(sdev, sdev->bar[bar] + offset, size, name,
  154. access_type);
  155. }
  156. EXPORT_SYMBOL_GPL(snd_sof_debugfs_add_region_item_iomem);
  157. /* create FS entry for debug files to expose kernel memory */
  158. int snd_sof_debugfs_buf_item(struct snd_sof_dev *sdev,
  159. void *base, size_t size,
  160. const char *name, mode_t mode)
  161. {
  162. struct snd_sof_dfsentry *dfse;
  163. if (!sdev)
  164. return -EINVAL;
  165. dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
  166. if (!dfse)
  167. return -ENOMEM;
  168. dfse->type = SOF_DFSENTRY_TYPE_BUF;
  169. dfse->buf = base;
  170. dfse->size = size;
  171. dfse->sdev = sdev;
  172. debugfs_create_file(name, mode, sdev->debugfs_root, dfse,
  173. &sof_dfs_fops);
  174. /* add to dfsentry list */
  175. list_add(&dfse->list, &sdev->dfsentry_list);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(snd_sof_debugfs_buf_item);
  179. static int memory_info_update(struct snd_sof_dev *sdev, char *buf, size_t buff_size)
  180. {
  181. struct sof_ipc_cmd_hdr msg = {
  182. .size = sizeof(struct sof_ipc_cmd_hdr),
  183. .cmd = SOF_IPC_GLB_DEBUG | SOF_IPC_DEBUG_MEM_USAGE,
  184. };
  185. struct sof_ipc_dbg_mem_usage *reply;
  186. int len;
  187. int ret;
  188. int i;
  189. reply = kmalloc(SOF_IPC_MSG_MAX_SIZE, GFP_KERNEL);
  190. if (!reply)
  191. return -ENOMEM;
  192. ret = pm_runtime_resume_and_get(sdev->dev);
  193. if (ret < 0 && ret != -EACCES) {
  194. dev_err(sdev->dev, "error: enabling device failed: %d\n", ret);
  195. goto error;
  196. }
  197. ret = sof_ipc_tx_message(sdev->ipc, &msg, msg.size, reply, SOF_IPC_MSG_MAX_SIZE);
  198. pm_runtime_mark_last_busy(sdev->dev);
  199. pm_runtime_put_autosuspend(sdev->dev);
  200. if (ret < 0 || reply->rhdr.error < 0) {
  201. ret = min(ret, reply->rhdr.error);
  202. dev_err(sdev->dev, "error: reading memory info failed, %d\n", ret);
  203. goto error;
  204. }
  205. if (struct_size(reply, elems, reply->num_elems) != reply->rhdr.hdr.size) {
  206. dev_err(sdev->dev, "error: invalid memory info ipc struct size, %d\n",
  207. reply->rhdr.hdr.size);
  208. ret = -EINVAL;
  209. goto error;
  210. }
  211. for (i = 0, len = 0; i < reply->num_elems; i++) {
  212. ret = scnprintf(buf + len, buff_size - len, "zone %d.%d used %#8x free %#8x\n",
  213. reply->elems[i].zone, reply->elems[i].id,
  214. reply->elems[i].used, reply->elems[i].free);
  215. if (ret < 0)
  216. goto error;
  217. len += ret;
  218. }
  219. ret = len;
  220. error:
  221. kfree(reply);
  222. return ret;
  223. }
  224. static ssize_t memory_info_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
  225. {
  226. struct snd_sof_dfsentry *dfse = file->private_data;
  227. struct snd_sof_dev *sdev = dfse->sdev;
  228. int data_length;
  229. /* read memory info from FW only once for each file read */
  230. if (!*ppos) {
  231. dfse->buf_data_size = 0;
  232. data_length = memory_info_update(sdev, dfse->buf, dfse->size);
  233. if (data_length < 0)
  234. return data_length;
  235. dfse->buf_data_size = data_length;
  236. }
  237. return simple_read_from_buffer(to, count, ppos, dfse->buf, dfse->buf_data_size);
  238. }
  239. static int memory_info_open(struct inode *inode, struct file *file)
  240. {
  241. struct snd_sof_dfsentry *dfse = inode->i_private;
  242. struct snd_sof_dev *sdev = dfse->sdev;
  243. file->private_data = dfse;
  244. /* allocate buffer memory only in first open run, to save memory when unused */
  245. if (!dfse->buf) {
  246. dfse->buf = devm_kmalloc(sdev->dev, PAGE_SIZE, GFP_KERNEL);
  247. if (!dfse->buf)
  248. return -ENOMEM;
  249. dfse->size = PAGE_SIZE;
  250. }
  251. return 0;
  252. }
  253. static const struct file_operations memory_info_fops = {
  254. .open = memory_info_open,
  255. .read = memory_info_read,
  256. .llseek = default_llseek,
  257. };
  258. int snd_sof_dbg_memory_info_init(struct snd_sof_dev *sdev)
  259. {
  260. struct snd_sof_dfsentry *dfse;
  261. dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
  262. if (!dfse)
  263. return -ENOMEM;
  264. /* don't allocate buffer before first usage, to save memory when unused */
  265. dfse->type = SOF_DFSENTRY_TYPE_BUF;
  266. dfse->sdev = sdev;
  267. debugfs_create_file("memory_info", 0444, sdev->debugfs_root, dfse, &memory_info_fops);
  268. /* add to dfsentry list */
  269. list_add(&dfse->list, &sdev->dfsentry_list);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(snd_sof_dbg_memory_info_init);
  273. int snd_sof_dbg_init(struct snd_sof_dev *sdev)
  274. {
  275. struct snd_sof_dsp_ops *ops = sof_ops(sdev);
  276. const struct snd_sof_debugfs_map *map;
  277. int i;
  278. int err;
  279. /* use "sof" as top level debugFS dir */
  280. sdev->debugfs_root = debugfs_create_dir("sof", NULL);
  281. /* init dfsentry list */
  282. INIT_LIST_HEAD(&sdev->dfsentry_list);
  283. /* create debugFS files for platform specific MMIO/DSP memories */
  284. for (i = 0; i < ops->debug_map_count; i++) {
  285. map = &ops->debug_map[i];
  286. err = snd_sof_debugfs_io_item(sdev, sdev->bar[map->bar] +
  287. map->offset, map->size,
  288. map->name, map->access_type);
  289. /* errors are only due to memory allocation, not debugfs */
  290. if (err < 0)
  291. return err;
  292. }
  293. return snd_sof_debugfs_buf_item(sdev, &sdev->fw_state,
  294. sizeof(sdev->fw_state),
  295. "fw_state", 0444);
  296. }
  297. EXPORT_SYMBOL_GPL(snd_sof_dbg_init);
  298. void snd_sof_free_debug(struct snd_sof_dev *sdev)
  299. {
  300. debugfs_remove_recursive(sdev->debugfs_root);
  301. }
  302. EXPORT_SYMBOL_GPL(snd_sof_free_debug);
  303. static const struct soc_fw_state_info {
  304. enum sof_fw_state state;
  305. const char *name;
  306. } fw_state_dbg[] = {
  307. {SOF_FW_BOOT_NOT_STARTED, "SOF_FW_BOOT_NOT_STARTED"},
  308. {SOF_FW_BOOT_PREPARE, "SOF_FW_BOOT_PREPARE"},
  309. {SOF_FW_BOOT_IN_PROGRESS, "SOF_FW_BOOT_IN_PROGRESS"},
  310. {SOF_FW_BOOT_FAILED, "SOF_FW_BOOT_FAILED"},
  311. {SOF_FW_BOOT_READY_FAILED, "SOF_FW_BOOT_READY_FAILED"},
  312. {SOF_FW_BOOT_READY_OK, "SOF_FW_BOOT_READY_OK"},
  313. {SOF_FW_BOOT_COMPLETE, "SOF_FW_BOOT_COMPLETE"},
  314. {SOF_FW_CRASHED, "SOF_FW_CRASHED"},
  315. };
  316. static void snd_sof_dbg_print_fw_state(struct snd_sof_dev *sdev, const char *level)
  317. {
  318. int i;
  319. for (i = 0; i < ARRAY_SIZE(fw_state_dbg); i++) {
  320. if (sdev->fw_state == fw_state_dbg[i].state) {
  321. dev_printk(level, sdev->dev, "fw_state: %s (%d)\n",
  322. fw_state_dbg[i].name, i);
  323. return;
  324. }
  325. }
  326. dev_printk(level, sdev->dev, "fw_state: UNKNOWN (%d)\n", sdev->fw_state);
  327. }
  328. void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags)
  329. {
  330. char *level = (flags & SOF_DBG_DUMP_OPTIONAL) ? KERN_DEBUG : KERN_ERR;
  331. bool print_all = sof_debug_check_flag(SOF_DBG_PRINT_ALL_DUMPS);
  332. if (flags & SOF_DBG_DUMP_OPTIONAL && !print_all)
  333. return;
  334. if (sof_ops(sdev)->dbg_dump && !sdev->dbg_dump_printed) {
  335. dev_printk(level, sdev->dev,
  336. "------------[ DSP dump start ]------------\n");
  337. if (msg)
  338. dev_printk(level, sdev->dev, "%s\n", msg);
  339. snd_sof_dbg_print_fw_state(sdev, level);
  340. sof_ops(sdev)->dbg_dump(sdev, flags);
  341. dev_printk(level, sdev->dev,
  342. "------------[ DSP dump end ]------------\n");
  343. if (!print_all)
  344. sdev->dbg_dump_printed = true;
  345. } else if (msg) {
  346. dev_printk(level, sdev->dev, "%s\n", msg);
  347. }
  348. }
  349. EXPORT_SYMBOL(snd_sof_dsp_dbg_dump);
  350. static void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
  351. {
  352. if (sof_ops(sdev)->ipc_dump && !sdev->ipc_dump_printed) {
  353. dev_err(sdev->dev, "------------[ IPC dump start ]------------\n");
  354. sof_ops(sdev)->ipc_dump(sdev);
  355. dev_err(sdev->dev, "------------[ IPC dump end ]------------\n");
  356. if (!sof_debug_check_flag(SOF_DBG_PRINT_ALL_DUMPS))
  357. sdev->ipc_dump_printed = true;
  358. }
  359. }
  360. void snd_sof_handle_fw_exception(struct snd_sof_dev *sdev, const char *msg)
  361. {
  362. if (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_RETAIN_DSP_CONTEXT) ||
  363. sof_debug_check_flag(SOF_DBG_RETAIN_CTX)) {
  364. /* should we prevent DSP entering D3 ? */
  365. if (!sdev->ipc_dump_printed)
  366. dev_info(sdev->dev,
  367. "Attempting to prevent DSP from entering D3 state to preserve context\n");
  368. pm_runtime_get_if_in_use(sdev->dev);
  369. }
  370. /* dump vital information to the logs */
  371. snd_sof_ipc_dump(sdev);
  372. snd_sof_dsp_dbg_dump(sdev, msg, SOF_DBG_DUMP_REGS | SOF_DBG_DUMP_MBOX);
  373. sof_fw_trace_fw_crashed(sdev);
  374. }
  375. EXPORT_SYMBOL(snd_sof_handle_fw_exception);