1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
5 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
7 #include <linux/debugfs.h>
8 #include <linux/sched/signal.h>
10 #include "sof-audio.h"
12 #include "sof-utils.h"
13 #include "ipc3-priv.h"
15 #define TRACE_FILTER_ELEMENTS_PER_ENTRY 4
16 #define TRACE_FILTER_MAX_CONFIG_STRING_LENGTH 1024
18 enum sof_dtrace_state {
21 SOF_DTRACE_INITIALIZING,
25 struct sof_dtrace_priv {
26 struct snd_dma_buffer dmatb;
27 struct snd_dma_buffer dmatp;
29 wait_queue_head_t trace_sleep;
33 enum sof_dtrace_state dtrace_state;
36 static bool trace_pos_update_expected(struct sof_dtrace_priv *priv)
38 if (priv->dtrace_state == SOF_DTRACE_ENABLED ||
39 priv->dtrace_state == SOF_DTRACE_INITIALIZING)
45 static int trace_filter_append_elem(struct snd_sof_dev *sdev, u32 key, u32 value,
46 struct sof_ipc_trace_filter_elem *elem_list,
47 int capacity, int *counter)
49 if (*counter >= capacity)
52 elem_list[*counter].key = key;
53 elem_list[*counter].value = value;
59 static int trace_filter_parse_entry(struct snd_sof_dev *sdev, const char *line,
60 struct sof_ipc_trace_filter_elem *elem,
61 int capacity, int *counter)
63 int log_level, pipe_id, comp_id, read, ret;
64 int len = strlen(line);
68 /* ignore empty content */
69 ret = sscanf(line, " %n", &read);
70 if (!ret && read == len)
73 ret = sscanf(line, " %d %x %d %d %n", &log_level, &uuid_id, &pipe_id, &comp_id, &read);
74 if (ret != TRACE_FILTER_ELEMENTS_PER_ENTRY || read != len) {
75 dev_err(sdev->dev, "Invalid trace filter entry '%s'\n", line);
80 ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_UUID,
81 uuid_id, elem, capacity, &cnt);
86 ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_PIPE,
87 pipe_id, elem, capacity, &cnt);
92 ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_COMP,
93 comp_id, elem, capacity, &cnt);
98 ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_SET_LEVEL |
99 SOF_IPC_TRACE_FILTER_ELEM_FIN,
100 log_level, elem, capacity, &cnt);
104 /* update counter only when parsing whole entry passed */
110 static int trace_filter_parse(struct snd_sof_dev *sdev, char *string,
112 struct sof_ipc_trace_filter_elem **out)
114 static const char entry_delimiter[] = ";";
115 char *entry = string;
121 * Each entry contains at least 1, up to TRACE_FILTER_ELEMENTS_PER_ENTRY
122 * IPC elements, depending on content. Calculate IPC elements capacity
123 * for the input string where each element is set.
126 capacity += TRACE_FILTER_ELEMENTS_PER_ENTRY;
127 entry = strchr(entry + 1, entry_delimiter[0]);
129 *out = kmalloc(capacity * sizeof(**out), GFP_KERNEL);
133 /* split input string by ';', and parse each entry separately in trace_filter_parse_entry */
134 while ((entry = strsep(&string, entry_delimiter))) {
135 entry_len = trace_filter_parse_entry(sdev, entry, *out, capacity, &cnt);
138 "Parsing filter entry '%s' failed with %d\n",
149 static int ipc3_trace_update_filter(struct snd_sof_dev *sdev, int num_elems,
150 struct sof_ipc_trace_filter_elem *elems)
152 struct sof_ipc_trace_filter *msg;
153 struct sof_ipc_reply reply;
157 size = struct_size(msg, elems, num_elems);
158 if (size > SOF_IPC_MSG_MAX_SIZE)
161 msg = kmalloc(size, GFP_KERNEL);
165 msg->hdr.size = size;
166 msg->hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_FILTER_UPDATE;
167 msg->elem_cnt = num_elems;
168 memcpy(&msg->elems[0], elems, num_elems * sizeof(*elems));
170 ret = pm_runtime_resume_and_get(sdev->dev);
171 if (ret < 0 && ret != -EACCES) {
172 dev_err(sdev->dev, "enabling device failed: %d\n", ret);
175 ret = sof_ipc_tx_message(sdev->ipc, msg, msg->hdr.size, &reply, sizeof(reply));
176 pm_runtime_mark_last_busy(sdev->dev);
177 pm_runtime_put_autosuspend(sdev->dev);
181 return ret ? ret : reply.error;
184 static ssize_t dfsentry_trace_filter_write(struct file *file, const char __user *from,
185 size_t count, loff_t *ppos)
187 struct snd_sof_dfsentry *dfse = file->private_data;
188 struct sof_ipc_trace_filter_elem *elems = NULL;
189 struct snd_sof_dev *sdev = dfse->sdev;
195 if (count > TRACE_FILTER_MAX_CONFIG_STRING_LENGTH) {
196 dev_err(sdev->dev, "%s too long input, %zu > %d\n", __func__, count,
197 TRACE_FILTER_MAX_CONFIG_STRING_LENGTH);
201 string = kmalloc(count + 1, GFP_KERNEL);
205 /* assert null termination */
207 ret = simple_write_to_buffer(string, count, &pos, from, count);
211 ret = trace_filter_parse(sdev, string, &num_elems, &elems);
216 ret = ipc3_trace_update_filter(sdev, num_elems, elems);
218 dev_err(sdev->dev, "Filter update failed: %d\n", ret);
229 static const struct file_operations sof_dfs_trace_filter_fops = {
231 .write = dfsentry_trace_filter_write,
232 .llseek = default_llseek,
235 static int debugfs_create_trace_filter(struct snd_sof_dev *sdev)
237 struct snd_sof_dfsentry *dfse;
239 dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
244 dfse->type = SOF_DFSENTRY_TYPE_BUF;
246 debugfs_create_file("filter", 0200, sdev->debugfs_root, dfse,
247 &sof_dfs_trace_filter_fops);
248 /* add to dfsentry list */
249 list_add(&dfse->list, &sdev->dfsentry_list);
254 static bool sof_dtrace_set_host_offset(struct sof_dtrace_priv *priv, u32 new_offset)
256 u32 host_offset = READ_ONCE(priv->host_offset);
258 if (host_offset != new_offset) {
259 /* This is a bit paranoid and unlikely that it is needed */
260 u32 ret = cmpxchg(&priv->host_offset, host_offset, new_offset);
262 if (ret == host_offset)
269 static size_t sof_dtrace_avail(struct snd_sof_dev *sdev,
270 loff_t pos, size_t buffer_size)
272 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
273 loff_t host_offset = READ_ONCE(priv->host_offset);
276 * If host offset is less than local pos, it means write pointer of
277 * host DMA buffer has been wrapped. We should output the trace data
278 * at the end of host DMA buffer at first.
280 if (host_offset < pos)
281 return buffer_size - pos;
283 /* If there is available trace data now, it is unnecessary to wait. */
284 if (host_offset > pos)
285 return host_offset - pos;
290 static size_t sof_wait_dtrace_avail(struct snd_sof_dev *sdev, loff_t pos,
293 size_t ret = sof_dtrace_avail(sdev, pos, buffer_size);
294 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
295 wait_queue_entry_t wait;
297 /* data immediately available */
301 if (priv->dtrace_draining && !trace_pos_update_expected(priv)) {
303 * tracing has ended and all traces have been
304 * read by client, return EOF
306 priv->dtrace_draining = false;
310 /* wait for available trace data from FW */
311 init_waitqueue_entry(&wait, current);
312 set_current_state(TASK_INTERRUPTIBLE);
313 add_wait_queue(&priv->trace_sleep, &wait);
315 if (!signal_pending(current)) {
316 /* set timeout to max value, no error code */
317 schedule_timeout(MAX_SCHEDULE_TIMEOUT);
319 remove_wait_queue(&priv->trace_sleep, &wait);
321 return sof_dtrace_avail(sdev, pos, buffer_size);
324 static ssize_t dfsentry_dtrace_read(struct file *file, char __user *buffer,
325 size_t count, loff_t *ppos)
327 struct snd_sof_dfsentry *dfse = file->private_data;
328 struct snd_sof_dev *sdev = dfse->sdev;
329 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
332 size_t avail, buffer_size = dfse->size;
335 /* make sure we know about any failures on the DSP side */
336 priv->dtrace_error = false;
338 /* check pos and count */
344 /* check for buffer wrap and count overflow */
346 lpos = do_div(lpos_64, buffer_size);
348 /* get available count based on current host offset */
349 avail = sof_wait_dtrace_avail(sdev, lpos, buffer_size);
350 if (priv->dtrace_error) {
351 dev_err(sdev->dev, "trace IO error\n");
355 /* no new trace data */
359 /* make sure count is <= avail */
364 * make sure that all trace data is available for the CPU as the trace
365 * data buffer might be allocated from non consistent memory.
366 * Note: snd_dma_buffer_sync() is called for normal audio playback and
367 * capture streams also.
369 snd_dma_buffer_sync(&priv->dmatb, SNDRV_DMA_SYNC_CPU);
370 /* copy available trace data to debugfs */
371 rem = copy_to_user(buffer, ((u8 *)(dfse->buf) + lpos), count);
377 /* move debugfs reading position */
381 static int dfsentry_dtrace_release(struct inode *inode, struct file *file)
383 struct snd_sof_dfsentry *dfse = inode->i_private;
384 struct snd_sof_dev *sdev = dfse->sdev;
385 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
387 /* avoid duplicate traces at next open */
388 if (priv->dtrace_state != SOF_DTRACE_ENABLED)
389 sof_dtrace_set_host_offset(priv, 0);
394 static const struct file_operations sof_dfs_dtrace_fops = {
396 .read = dfsentry_dtrace_read,
397 .llseek = default_llseek,
398 .release = dfsentry_dtrace_release,
401 static int debugfs_create_dtrace(struct snd_sof_dev *sdev)
403 struct sof_dtrace_priv *priv;
404 struct snd_sof_dfsentry *dfse;
410 priv = sdev->fw_trace_data;
412 ret = debugfs_create_trace_filter(sdev);
414 dev_warn(sdev->dev, "failed to create filter debugfs file: %d", ret);
416 dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
420 dfse->type = SOF_DFSENTRY_TYPE_BUF;
421 dfse->buf = priv->dmatb.area;
422 dfse->size = priv->dmatb.bytes;
425 debugfs_create_file("trace", 0444, sdev->debugfs_root, dfse,
426 &sof_dfs_dtrace_fops);
431 static int ipc3_dtrace_enable(struct snd_sof_dev *sdev)
433 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
434 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
435 struct sof_ipc_fw_version *v = &ready->version;
436 struct sof_ipc_dma_trace_params_ext params;
437 struct sof_ipc_reply ipc_reply;
440 if (!sdev->fw_trace_is_supported)
443 if (priv->dtrace_state == SOF_DTRACE_ENABLED || !priv->dma_trace_pages)
446 if (priv->dtrace_state == SOF_DTRACE_STOPPED)
449 /* set IPC parameters */
450 params.hdr.cmd = SOF_IPC_GLB_TRACE_MSG;
451 /* PARAMS_EXT is only supported from ABI 3.7.0 onwards */
452 if (v->abi_version >= SOF_ABI_VER(3, 7, 0)) {
453 params.hdr.size = sizeof(struct sof_ipc_dma_trace_params_ext);
454 params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS_EXT;
455 params.timestamp_ns = ktime_get(); /* in nanosecond */
457 params.hdr.size = sizeof(struct sof_ipc_dma_trace_params);
458 params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS;
460 params.buffer.phy_addr = priv->dmatp.addr;
461 params.buffer.size = priv->dmatb.bytes;
462 params.buffer.pages = priv->dma_trace_pages;
463 params.stream_tag = 0;
465 sof_dtrace_set_host_offset(priv, 0);
466 priv->dtrace_draining = false;
468 ret = sof_dtrace_host_init(sdev, &priv->dmatb, ¶ms);
470 dev_err(sdev->dev, "Host dtrace init failed: %d\n", ret);
473 dev_dbg(sdev->dev, "stream_tag: %d\n", params.stream_tag);
475 /* send IPC to the DSP */
476 priv->dtrace_state = SOF_DTRACE_INITIALIZING;
477 ret = sof_ipc_tx_message(sdev->ipc, ¶ms, sizeof(params), &ipc_reply, sizeof(ipc_reply));
479 dev_err(sdev->dev, "can't set params for DMA for trace %d\n", ret);
484 priv->dtrace_state = SOF_DTRACE_ENABLED;
486 ret = sof_dtrace_host_trigger(sdev, SNDRV_PCM_TRIGGER_START);
488 dev_err(sdev->dev, "Host dtrace trigger start failed: %d\n", ret);
495 priv->dtrace_state = SOF_DTRACE_DISABLED;
496 sof_dtrace_host_release(sdev);
500 static int ipc3_dtrace_init(struct snd_sof_dev *sdev)
502 struct sof_dtrace_priv *priv;
505 /* dtrace is only supported with SOF_IPC */
506 if (sdev->pdata->ipc_type != SOF_IPC)
509 if (sdev->fw_trace_data) {
510 dev_err(sdev->dev, "fw_trace_data has been already allocated\n");
514 priv = devm_kzalloc(sdev->dev, sizeof(*priv), GFP_KERNEL);
518 sdev->fw_trace_data = priv;
520 /* set false before start initialization */
521 priv->dtrace_state = SOF_DTRACE_DISABLED;
523 /* allocate trace page table buffer */
524 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
525 PAGE_SIZE, &priv->dmatp);
527 dev_err(sdev->dev, "can't alloc page table for trace %d\n", ret);
531 /* allocate trace data buffer */
532 ret = snd_dma_alloc_dir_pages(SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
533 DMA_FROM_DEVICE, DMA_BUF_SIZE_FOR_TRACE,
536 dev_err(sdev->dev, "can't alloc buffer for trace %d\n", ret);
540 /* create compressed page table for audio firmware */
541 ret = snd_sof_create_page_table(sdev->dev, &priv->dmatb,
542 priv->dmatp.area, priv->dmatb.bytes);
546 priv->dma_trace_pages = ret;
547 dev_dbg(sdev->dev, "dma_trace_pages: %d\n", priv->dma_trace_pages);
549 if (sdev->first_boot) {
550 ret = debugfs_create_dtrace(sdev);
555 init_waitqueue_head(&priv->trace_sleep);
557 ret = ipc3_dtrace_enable(sdev);
563 priv->dma_trace_pages = 0;
564 snd_dma_free_pages(&priv->dmatb);
566 snd_dma_free_pages(&priv->dmatp);
570 int ipc3_dtrace_posn_update(struct snd_sof_dev *sdev,
571 struct sof_ipc_dma_trace_posn *posn)
573 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
575 if (!sdev->fw_trace_is_supported)
578 if (trace_pos_update_expected(priv) &&
579 sof_dtrace_set_host_offset(priv, posn->host_offset))
580 wake_up(&priv->trace_sleep);
582 if (posn->overflow != 0)
584 "DSP trace buffer overflow %u bytes. Total messages %d\n",
585 posn->overflow, posn->messages);
590 /* an error has occurred within the DSP that prevents further trace */
591 static void ipc3_dtrace_fw_crashed(struct snd_sof_dev *sdev)
593 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
595 if (priv->dtrace_state == SOF_DTRACE_ENABLED) {
596 priv->dtrace_error = true;
597 wake_up(&priv->trace_sleep);
601 static void ipc3_dtrace_release(struct snd_sof_dev *sdev, bool only_stop)
603 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
604 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
605 struct sof_ipc_fw_version *v = &ready->version;
606 struct sof_ipc_cmd_hdr hdr;
607 struct sof_ipc_reply ipc_reply;
610 if (!sdev->fw_trace_is_supported || priv->dtrace_state == SOF_DTRACE_DISABLED)
613 ret = sof_dtrace_host_trigger(sdev, SNDRV_PCM_TRIGGER_STOP);
615 dev_err(sdev->dev, "Host dtrace trigger stop failed: %d\n", ret);
616 priv->dtrace_state = SOF_DTRACE_STOPPED;
619 * stop and free trace DMA in the DSP. TRACE_DMA_FREE is only supported from
622 if (v->abi_version >= SOF_ABI_VER(3, 20, 0)) {
623 hdr.size = sizeof(hdr);
624 hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_DMA_FREE;
626 ret = sof_ipc_tx_message(sdev->ipc, &hdr, hdr.size,
627 &ipc_reply, sizeof(ipc_reply));
629 dev_err(sdev->dev, "DMA_TRACE_FREE failed with error: %d\n", ret);
635 ret = sof_dtrace_host_release(sdev);
637 dev_err(sdev->dev, "Host dtrace release failed %d\n", ret);
639 priv->dtrace_state = SOF_DTRACE_DISABLED;
642 priv->dtrace_draining = true;
643 wake_up(&priv->trace_sleep);
646 static void ipc3_dtrace_suspend(struct snd_sof_dev *sdev, pm_message_t pm_state)
648 ipc3_dtrace_release(sdev, pm_state.event == SOF_DSP_PM_D0);
651 static int ipc3_dtrace_resume(struct snd_sof_dev *sdev)
653 return ipc3_dtrace_enable(sdev);
656 static void ipc3_dtrace_free(struct snd_sof_dev *sdev)
658 struct sof_dtrace_priv *priv = sdev->fw_trace_data;
661 ipc3_dtrace_release(sdev, false);
663 if (priv->dma_trace_pages) {
664 snd_dma_free_pages(&priv->dmatb);
665 snd_dma_free_pages(&priv->dmatp);
666 priv->dma_trace_pages = 0;
670 const struct sof_ipc_fw_tracing_ops ipc3_dtrace_ops = {
671 .init = ipc3_dtrace_init,
672 .free = ipc3_dtrace_free,
673 .fw_crashed = ipc3_dtrace_fw_crashed,
674 .suspend = ipc3_dtrace_suspend,
675 .resume = ipc3_dtrace_resume,