ipc4-mtrace.c 18 KB

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