ipc4-mtrace.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2022 Intel Corporation
  4. #include <linux/debugfs.h>
  5. #include <linux/sched/signal.h>
  6. #include <linux/sched/clock.h>
  7. #include <sound/sof/ipc4/header.h>
  8. #include "sof-priv.h"
  9. #include "ipc4-priv.h"
  10. /*
  11. * debug info window is organized in 16 (equal sized) pages:
  12. *
  13. * ------------------------
  14. * | Page0 - descriptors |
  15. * ------------------------
  16. * | Page1 - slot0 |
  17. * ------------------------
  18. * | Page2 - slot1 |
  19. * ------------------------
  20. * | ... |
  21. * ------------------------
  22. * | Page14 - slot13 |
  23. * ------------------------
  24. * | Page15 - slot14 |
  25. * ------------------------
  26. *
  27. * The slot size == page size
  28. *
  29. * The first page contains descriptors for the remaining 15 cores
  30. * The slot descriptor is:
  31. * u32 res_id;
  32. * u32 type;
  33. * u32 vma;
  34. *
  35. * Log buffer slots have the following layout:
  36. * u32 host_read_ptr;
  37. * u32 dsp_write_ptr;
  38. * u8 buffer[];
  39. *
  40. * The two pointers are offsets within the buffer.
  41. */
  42. #define FW_EPOCH_DELTA 11644473600LL
  43. #define MAX_ALLOWED_LIBRARIES 16
  44. #define SOF_IPC4_INVALID_SLOT_OFFSET 0xffffffff
  45. /* for debug and critical types */
  46. #define SOF_MTRACE_SLOT_CORE_MASK GENMASK(7, 0)
  47. #define SOF_MTRACE_SLOT_TYPE_MASK GENMASK(31, 8)
  48. #define DEFAULT_AGING_TIMER_PERIOD_MS 0x100
  49. #define DEFAULT_FIFO_FULL_TIMER_PERIOD_MS 0x1000
  50. /* ipc4 log level and source definitions for logs_priorities_mask */
  51. #define SOF_MTRACE_LOG_LEVEL_CRITICAL BIT(0)
  52. #define SOF_MTRACE_LOG_LEVEL_ERROR BIT(1)
  53. #define SOF_MTRACE_LOG_LEVEL_WARNING BIT(2)
  54. #define SOF_MTRACE_LOG_LEVEL_INFO BIT(3)
  55. #define SOF_MTRACE_LOG_LEVEL_VERBOSE BIT(4)
  56. #define SOF_MTRACE_LOG_SOURCE_INFRA BIT(5) /* log source 0 */
  57. #define SOF_MTRACE_LOG_SOURCE_HAL BIT(6)
  58. #define SOF_MTRACE_LOG_SOURCE_MODULE BIT(7)
  59. #define SOF_MTRACE_LOG_SOURCE_AUDIO BIT(8)
  60. #define SOF_MTRACE_LOG_SOURCE_SCHEDULER BIT(9)
  61. #define SOF_MTRACE_LOG_SOURCE_ULP_INFRA BIT(10)
  62. #define SOF_MTRACE_LOG_SOURCE_ULP_MODULE BIT(11)
  63. #define SOF_MTRACE_LOG_SOURCE_VISION BIT(12) /* log source 7 */
  64. #define DEFAULT_LOGS_PRIORITIES_MASK (SOF_MTRACE_LOG_LEVEL_CRITICAL | \
  65. SOF_MTRACE_LOG_LEVEL_ERROR | \
  66. SOF_MTRACE_LOG_LEVEL_WARNING | \
  67. SOF_MTRACE_LOG_LEVEL_INFO | \
  68. SOF_MTRACE_LOG_SOURCE_INFRA | \
  69. SOF_MTRACE_LOG_SOURCE_HAL | \
  70. SOF_MTRACE_LOG_SOURCE_MODULE | \
  71. SOF_MTRACE_LOG_SOURCE_AUDIO)
  72. struct sof_log_state_info {
  73. u32 aging_timer_period;
  74. u32 fifo_full_timer_period;
  75. u32 enable;
  76. u32 logs_priorities_mask[MAX_ALLOWED_LIBRARIES];
  77. } __packed;
  78. enum sof_mtrace_state {
  79. SOF_MTRACE_DISABLED,
  80. SOF_MTRACE_INITIALIZING,
  81. SOF_MTRACE_ENABLED,
  82. };
  83. struct sof_mtrace_core_data {
  84. struct snd_sof_dev *sdev;
  85. int id;
  86. u32 slot_offset;
  87. void *log_buffer;
  88. struct mutex buffer_lock; /* for log_buffer alloc/free */
  89. u32 host_read_ptr;
  90. u32 dsp_write_ptr;
  91. /* pos update IPC arrived before the slot offset is known, queried */
  92. bool delayed_pos_update;
  93. wait_queue_head_t trace_sleep;
  94. };
  95. struct sof_mtrace_priv {
  96. struct snd_sof_dev *sdev;
  97. enum sof_mtrace_state mtrace_state;
  98. struct sof_log_state_info state_info;
  99. struct sof_mtrace_core_data cores[];
  100. };
  101. static int sof_ipc4_mtrace_dfs_open(struct inode *inode, struct file *file)
  102. {
  103. struct sof_mtrace_core_data *core_data = inode->i_private;
  104. int ret;
  105. guard(mutex)(&core_data->buffer_lock);
  106. if (core_data->log_buffer)
  107. return -EBUSY;
  108. ret = debugfs_file_get(file->f_path.dentry);
  109. if (unlikely(ret))
  110. return ret;
  111. core_data->log_buffer = kmalloc(SOF_IPC4_DEBUG_SLOT_SIZE, GFP_KERNEL);
  112. if (!core_data->log_buffer) {
  113. debugfs_file_put(file->f_path.dentry);
  114. return -ENOMEM;
  115. }
  116. ret = simple_open(inode, file);
  117. if (ret) {
  118. kfree(core_data->log_buffer);
  119. debugfs_file_put(file->f_path.dentry);
  120. }
  121. return ret;
  122. }
  123. static bool sof_wait_mtrace_avail(struct sof_mtrace_core_data *core_data)
  124. {
  125. wait_queue_entry_t wait;
  126. /* data immediately available */
  127. if (core_data->host_read_ptr != core_data->dsp_write_ptr)
  128. return true;
  129. /* wait for available trace data from FW */
  130. init_waitqueue_entry(&wait, current);
  131. set_current_state(TASK_INTERRUPTIBLE);
  132. add_wait_queue(&core_data->trace_sleep, &wait);
  133. if (!signal_pending(current)) {
  134. /* set timeout to max value, no error code */
  135. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  136. }
  137. remove_wait_queue(&core_data->trace_sleep, &wait);
  138. if (core_data->host_read_ptr != core_data->dsp_write_ptr)
  139. return true;
  140. return false;
  141. }
  142. static ssize_t sof_ipc4_mtrace_dfs_read(struct file *file, char __user *buffer,
  143. size_t count, loff_t *ppos)
  144. {
  145. struct sof_mtrace_core_data *core_data = file->private_data;
  146. u32 log_buffer_offset, log_buffer_size, read_ptr, write_ptr;
  147. struct snd_sof_dev *sdev = core_data->sdev;
  148. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  149. void *log_buffer = core_data->log_buffer;
  150. loff_t lpos = *ppos;
  151. u32 avail;
  152. int ret;
  153. /* check pos and count */
  154. if (lpos < 0)
  155. return -EINVAL;
  156. if (!count || count < sizeof(avail))
  157. return 0;
  158. /* get available count based on current host offset */
  159. if (!sof_wait_mtrace_avail(core_data)) {
  160. /* No data available */
  161. avail = 0;
  162. if (copy_to_user(buffer, &avail, sizeof(avail)))
  163. return -EFAULT;
  164. return 0;
  165. }
  166. if (core_data->slot_offset == SOF_IPC4_INVALID_SLOT_OFFSET)
  167. return 0;
  168. /* The log data buffer starts after the two pointer in the slot */
  169. log_buffer_offset = core_data->slot_offset + (sizeof(u32) * 2);
  170. /* The log data size excludes the pointers */
  171. log_buffer_size = SOF_IPC4_DEBUG_SLOT_SIZE - (sizeof(u32) * 2);
  172. read_ptr = core_data->host_read_ptr;
  173. write_ptr = core_data->dsp_write_ptr;
  174. if (read_ptr < write_ptr)
  175. avail = write_ptr - read_ptr;
  176. else
  177. avail = log_buffer_size - read_ptr + write_ptr;
  178. if (!avail)
  179. return 0;
  180. if (avail > log_buffer_size)
  181. avail = log_buffer_size;
  182. /* Need space for the initial u32 of the avail */
  183. if (avail > count - sizeof(avail))
  184. avail = count - sizeof(avail);
  185. if (sof_debug_check_flag(SOF_DBG_PRINT_DMA_POSITION_UPDATE_LOGS))
  186. dev_dbg(sdev->dev,
  187. "core%d, host read: %#x, dsp write: %#x, avail: %#x\n",
  188. core_data->id, read_ptr, write_ptr, avail);
  189. if (read_ptr < write_ptr) {
  190. /* Read data between read pointer and write pointer */
  191. sof_mailbox_read(sdev, log_buffer_offset + read_ptr, log_buffer, avail);
  192. } else {
  193. /* read from read pointer to end of the slot */
  194. sof_mailbox_read(sdev, log_buffer_offset + read_ptr, log_buffer,
  195. avail - write_ptr);
  196. /* read from slot start to write pointer */
  197. if (write_ptr)
  198. sof_mailbox_read(sdev, log_buffer_offset,
  199. (u8 *)(log_buffer) + avail - write_ptr,
  200. write_ptr);
  201. }
  202. /* first write the number of bytes we have gathered */
  203. ret = copy_to_user(buffer, &avail, sizeof(avail));
  204. if (ret)
  205. return -EFAULT;
  206. /* Followed by the data itself */
  207. ret = copy_to_user(buffer + sizeof(avail), log_buffer, avail);
  208. if (ret)
  209. return -EFAULT;
  210. /* Update the host_read_ptr in the slot for this core */
  211. read_ptr += avail;
  212. if (read_ptr >= log_buffer_size)
  213. read_ptr -= log_buffer_size;
  214. sof_mailbox_write(sdev, core_data->slot_offset, &read_ptr, sizeof(read_ptr));
  215. /* Only update the host_read_ptr if mtrace is enabled */
  216. if (priv->mtrace_state != SOF_MTRACE_DISABLED)
  217. core_data->host_read_ptr = read_ptr;
  218. /*
  219. * Ask for a new buffer from user space for the next chunk, not
  220. * streaming due to the heading number of bytes value.
  221. */
  222. *ppos += count;
  223. return count;
  224. }
  225. static int sof_ipc4_mtrace_dfs_release(struct inode *inode, struct file *file)
  226. {
  227. struct sof_mtrace_core_data *core_data = inode->i_private;
  228. debugfs_file_put(file->f_path.dentry);
  229. scoped_guard(mutex, &core_data->buffer_lock) {
  230. kfree(core_data->log_buffer);
  231. core_data->log_buffer = NULL;
  232. }
  233. return 0;
  234. }
  235. static const struct file_operations sof_dfs_mtrace_fops = {
  236. .open = sof_ipc4_mtrace_dfs_open,
  237. .read = sof_ipc4_mtrace_dfs_read,
  238. .llseek = default_llseek,
  239. .release = sof_ipc4_mtrace_dfs_release,
  240. .owner = THIS_MODULE,
  241. };
  242. static ssize_t sof_ipc4_priority_mask_dfs_read(struct file *file, char __user *to,
  243. size_t count, loff_t *ppos)
  244. {
  245. struct sof_mtrace_priv *priv = file->private_data;
  246. int i, ret, offset, remaining;
  247. char *buf;
  248. /*
  249. * one entry (14 char + new line = 15):
  250. * " 0: 000001ef"
  251. *
  252. * 16 * 15 + 1 = 241
  253. */
  254. buf = kzalloc(241, GFP_KERNEL);
  255. if (!buf)
  256. return -ENOMEM;
  257. for (i = 0; i < MAX_ALLOWED_LIBRARIES; i++) {
  258. offset = strlen(buf);
  259. remaining = 241 - offset;
  260. snprintf(buf + offset, remaining, "%2d: 0x%08x\n", i,
  261. priv->state_info.logs_priorities_mask[i]);
  262. }
  263. ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
  264. kfree(buf);
  265. return ret;
  266. }
  267. static ssize_t sof_ipc4_priority_mask_dfs_write(struct file *file,
  268. const char __user *from,
  269. size_t count, loff_t *ppos)
  270. {
  271. struct sof_mtrace_priv *priv = file->private_data;
  272. unsigned int id;
  273. char *buf;
  274. u32 mask;
  275. int ret;
  276. /*
  277. * To update Nth mask entry, write:
  278. * "N,0x1234" or "N,1234" to the debugfs file
  279. * The mask will be interpreted as hexadecimal number
  280. */
  281. buf = memdup_user_nul(from, count);
  282. if (IS_ERR(buf))
  283. return PTR_ERR(buf);
  284. ret = sscanf(buf, "%u,0x%x", &id, &mask);
  285. if (ret != 2) {
  286. ret = sscanf(buf, "%u,%x", &id, &mask);
  287. if (ret != 2) {
  288. ret = -EINVAL;
  289. goto out;
  290. }
  291. }
  292. if (id >= MAX_ALLOWED_LIBRARIES) {
  293. ret = -EINVAL;
  294. goto out;
  295. }
  296. priv->state_info.logs_priorities_mask[id] = mask;
  297. ret = count;
  298. out:
  299. kfree(buf);
  300. return ret;
  301. }
  302. static const struct file_operations sof_dfs_priority_mask_fops = {
  303. .open = simple_open,
  304. .read = sof_ipc4_priority_mask_dfs_read,
  305. .write = sof_ipc4_priority_mask_dfs_write,
  306. .llseek = default_llseek,
  307. .owner = THIS_MODULE,
  308. };
  309. static int mtrace_debugfs_create(struct snd_sof_dev *sdev)
  310. {
  311. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  312. struct dentry *dfs_root;
  313. char dfs_name[100];
  314. int i;
  315. dfs_root = debugfs_create_dir("mtrace", sdev->debugfs_root);
  316. if (IS_ERR_OR_NULL(dfs_root))
  317. return 0;
  318. /* Create files for the logging parameters */
  319. debugfs_create_u32("aging_timer_period", 0644, dfs_root,
  320. &priv->state_info.aging_timer_period);
  321. debugfs_create_u32("fifo_full_timer_period", 0644, dfs_root,
  322. &priv->state_info.fifo_full_timer_period);
  323. debugfs_create_file("logs_priorities_mask", 0644, dfs_root, priv,
  324. &sof_dfs_priority_mask_fops);
  325. /* Separate log files per core */
  326. for (i = 0; i < sdev->num_cores; i++) {
  327. snprintf(dfs_name, sizeof(dfs_name), "core%d", i);
  328. debugfs_create_file(dfs_name, 0444, dfs_root, &priv->cores[i],
  329. &sof_dfs_mtrace_fops);
  330. }
  331. return 0;
  332. }
  333. static int ipc4_mtrace_enable(struct snd_sof_dev *sdev)
  334. {
  335. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  336. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  337. struct sof_ipc4_msg msg;
  338. u64 system_time;
  339. int ret;
  340. if (priv->mtrace_state != SOF_MTRACE_DISABLED)
  341. return 0;
  342. msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
  343. msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  344. msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
  345. msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
  346. msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_SYSTEM_TIME);
  347. /*
  348. * local_clock() is used to align with dmesg, so both kernel and firmware logs have
  349. * the same base and a minor delta due to the IPC. system time is in us format but
  350. * local_clock() returns the time in ns, so convert to ns.
  351. */
  352. system_time = div64_u64(local_clock(), NSEC_PER_USEC);
  353. msg.data_size = sizeof(system_time);
  354. msg.data_ptr = &system_time;
  355. ret = iops->set_get_data(sdev, &msg, msg.data_size, true);
  356. if (ret)
  357. return ret;
  358. msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_ENABLE_LOGS);
  359. priv->state_info.enable = 1;
  360. msg.data_size = sizeof(priv->state_info);
  361. msg.data_ptr = &priv->state_info;
  362. priv->mtrace_state = SOF_MTRACE_INITIALIZING;
  363. ret = iops->set_get_data(sdev, &msg, msg.data_size, true);
  364. if (ret) {
  365. priv->mtrace_state = SOF_MTRACE_DISABLED;
  366. return ret;
  367. }
  368. priv->mtrace_state = SOF_MTRACE_ENABLED;
  369. return 0;
  370. }
  371. static void ipc4_mtrace_disable(struct snd_sof_dev *sdev)
  372. {
  373. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  374. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  375. struct sof_ipc4_msg msg;
  376. int i;
  377. if (priv->mtrace_state == SOF_MTRACE_DISABLED)
  378. return;
  379. msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
  380. msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  381. msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
  382. msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
  383. msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_ENABLE_LOGS);
  384. priv->state_info.enable = 0;
  385. msg.data_size = sizeof(priv->state_info);
  386. msg.data_ptr = &priv->state_info;
  387. iops->set_get_data(sdev, &msg, msg.data_size, true);
  388. priv->mtrace_state = SOF_MTRACE_DISABLED;
  389. for (i = 0; i < sdev->num_cores; i++) {
  390. struct sof_mtrace_core_data *core_data = &priv->cores[i];
  391. core_data->host_read_ptr = 0;
  392. core_data->dsp_write_ptr = 0;
  393. wake_up(&core_data->trace_sleep);
  394. }
  395. }
  396. /*
  397. * Each DSP core logs to a dedicated slot.
  398. * Parse the slot descriptors at debug_box offset to find the debug log slots
  399. * and map them to cores.
  400. * There are 15 slots and therefore 15 descriptors to check (MAX_MTRACE_SLOTS)
  401. */
  402. static void sof_mtrace_find_core_slots(struct snd_sof_dev *sdev)
  403. {
  404. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  405. struct sof_mtrace_core_data *core_data;
  406. u32 slot_desc_type_offset, type, core;
  407. int i;
  408. for (i = 0; i < SOF_IPC4_MAX_DEBUG_SLOTS; i++) {
  409. /* The type is the second u32 in the slot descriptor */
  410. slot_desc_type_offset = sdev->debug_box.offset;
  411. slot_desc_type_offset += SOF_IPC4_DEBUG_DESCRIPTOR_SIZE * i + sizeof(u32);
  412. sof_mailbox_read(sdev, slot_desc_type_offset, &type, sizeof(type));
  413. if ((type & SOF_MTRACE_SLOT_TYPE_MASK) == SOF_IPC4_DEBUG_SLOT_DEBUG_LOG) {
  414. core = type & SOF_MTRACE_SLOT_CORE_MASK;
  415. if (core >= sdev->num_cores) {
  416. dev_dbg(sdev->dev, "core%u is invalid for slot%d\n",
  417. core, i);
  418. continue;
  419. }
  420. core_data = &priv->cores[core];
  421. /*
  422. * The area reserved for descriptors have the same size
  423. * as a slot.
  424. * In other words: slot0 starts at
  425. * debug_box + SOF_MTRACE_SLOT_SIZE offset
  426. */
  427. core_data->slot_offset = sdev->debug_box.offset;
  428. core_data->slot_offset += SOF_IPC4_DEBUG_SLOT_SIZE * (i + 1);
  429. dev_dbg(sdev->dev, "slot%d is used for core%u\n", i, core);
  430. if (core_data->delayed_pos_update) {
  431. sof_ipc4_mtrace_update_pos(sdev, core);
  432. core_data->delayed_pos_update = false;
  433. }
  434. } else if (type) {
  435. dev_dbg(sdev->dev, "slot%d is not a log slot (%#x)\n", i, type);
  436. }
  437. }
  438. }
  439. static int ipc4_mtrace_init(struct snd_sof_dev *sdev)
  440. {
  441. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  442. struct sof_mtrace_priv *priv;
  443. int i, ret;
  444. if (sdev->fw_trace_data) {
  445. dev_err(sdev->dev, "fw_trace_data has been already allocated\n");
  446. return -EBUSY;
  447. }
  448. if (!ipc4_data->mtrace_log_bytes ||
  449. ipc4_data->mtrace_type != SOF_IPC4_MTRACE_INTEL_CAVS_2) {
  450. sdev->fw_trace_is_supported = false;
  451. return 0;
  452. }
  453. priv = devm_kzalloc(sdev->dev, struct_size(priv, cores, sdev->num_cores),
  454. GFP_KERNEL);
  455. if (!priv)
  456. return -ENOMEM;
  457. sdev->fw_trace_data = priv;
  458. /* Set initial values for mtrace parameters */
  459. priv->state_info.aging_timer_period = DEFAULT_AGING_TIMER_PERIOD_MS;
  460. priv->state_info.fifo_full_timer_period = DEFAULT_FIFO_FULL_TIMER_PERIOD_MS;
  461. /* Only enable basefw logs initially (index 0 is always basefw) */
  462. priv->state_info.logs_priorities_mask[0] = DEFAULT_LOGS_PRIORITIES_MASK;
  463. for (i = 0; i < sdev->num_cores; i++) {
  464. struct sof_mtrace_core_data *core_data = &priv->cores[i];
  465. init_waitqueue_head(&core_data->trace_sleep);
  466. mutex_init(&core_data->buffer_lock);
  467. core_data->sdev = sdev;
  468. core_data->id = i;
  469. }
  470. ret = ipc4_mtrace_enable(sdev);
  471. if (ret) {
  472. /*
  473. * Mark firmware tracing as not supported and return 0 to not
  474. * block the whole audio stack
  475. */
  476. sdev->fw_trace_is_supported = false;
  477. dev_dbg(sdev->dev, "initialization failed, fw tracing is disabled\n");
  478. return 0;
  479. }
  480. sof_mtrace_find_core_slots(sdev);
  481. ret = mtrace_debugfs_create(sdev);
  482. if (ret)
  483. ipc4_mtrace_disable(sdev);
  484. return ret;
  485. }
  486. static void ipc4_mtrace_free(struct snd_sof_dev *sdev)
  487. {
  488. ipc4_mtrace_disable(sdev);
  489. }
  490. static int sof_ipc4_mtrace_update_pos_all_cores(struct snd_sof_dev *sdev)
  491. {
  492. int i;
  493. for (i = 0; i < sdev->num_cores; i++)
  494. sof_ipc4_mtrace_update_pos(sdev, i);
  495. return 0;
  496. }
  497. int sof_ipc4_mtrace_update_pos(struct snd_sof_dev *sdev, int core)
  498. {
  499. struct sof_mtrace_priv *priv = sdev->fw_trace_data;
  500. struct sof_mtrace_core_data *core_data;
  501. if (!sdev->fw_trace_is_supported ||
  502. priv->mtrace_state == SOF_MTRACE_DISABLED)
  503. return 0;
  504. if (core >= sdev->num_cores)
  505. return -EINVAL;
  506. core_data = &priv->cores[core];
  507. if (core_data->slot_offset == SOF_IPC4_INVALID_SLOT_OFFSET) {
  508. core_data->delayed_pos_update = true;
  509. return 0;
  510. }
  511. /* Read out the dsp_write_ptr from the slot for this core */
  512. sof_mailbox_read(sdev, core_data->slot_offset + sizeof(u32),
  513. &core_data->dsp_write_ptr, 4);
  514. core_data->dsp_write_ptr -= core_data->dsp_write_ptr % 4;
  515. if (sof_debug_check_flag(SOF_DBG_PRINT_DMA_POSITION_UPDATE_LOGS))
  516. dev_dbg(sdev->dev, "core%d, host read: %#x, dsp write: %#x",
  517. core, core_data->host_read_ptr, core_data->dsp_write_ptr);
  518. wake_up(&core_data->trace_sleep);
  519. return 0;
  520. }
  521. static void ipc4_mtrace_fw_crashed(struct snd_sof_dev *sdev)
  522. {
  523. /*
  524. * The DSP might not be able to send SOF_IPC4_NOTIFY_LOG_BUFFER_STATUS
  525. * messages anymore, so check the log buffer status on all
  526. * cores and process any pending messages.
  527. */
  528. sof_ipc4_mtrace_update_pos_all_cores(sdev);
  529. }
  530. static int ipc4_mtrace_resume(struct snd_sof_dev *sdev)
  531. {
  532. return ipc4_mtrace_enable(sdev);
  533. }
  534. static void ipc4_mtrace_suspend(struct snd_sof_dev *sdev, pm_message_t pm_state)
  535. {
  536. ipc4_mtrace_disable(sdev);
  537. }
  538. const struct sof_ipc_fw_tracing_ops ipc4_mtrace_ops = {
  539. .init = ipc4_mtrace_init,
  540. .free = ipc4_mtrace_free,
  541. .fw_crashed = ipc4_mtrace_fw_crashed,
  542. .suspend = ipc4_mtrace_suspend,
  543. .resume = ipc4_mtrace_resume,
  544. };