1*671e0b90SPeter Ujfalusi // SPDX-License-Identifier: GPL-2.0-only 2*671e0b90SPeter Ujfalusi // 3*671e0b90SPeter Ujfalusi // Copyright(c) 2022 Intel Corporation. All rights reserved. 4*671e0b90SPeter Ujfalusi // 5*671e0b90SPeter Ujfalusi // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 6*671e0b90SPeter Ujfalusi 7*671e0b90SPeter Ujfalusi #include <linux/debugfs.h> 8*671e0b90SPeter Ujfalusi #include <linux/sched/signal.h> 9*671e0b90SPeter Ujfalusi #include "sof-priv.h" 10*671e0b90SPeter Ujfalusi #include "sof-audio.h" 11*671e0b90SPeter Ujfalusi #include "ops.h" 12*671e0b90SPeter Ujfalusi #include "sof-utils.h" 13*671e0b90SPeter Ujfalusi #include "ipc3-priv.h" 14*671e0b90SPeter Ujfalusi 15*671e0b90SPeter Ujfalusi #define TRACE_FILTER_ELEMENTS_PER_ENTRY 4 16*671e0b90SPeter Ujfalusi #define TRACE_FILTER_MAX_CONFIG_STRING_LENGTH 1024 17*671e0b90SPeter Ujfalusi 18*671e0b90SPeter Ujfalusi static int trace_filter_append_elem(struct snd_sof_dev *sdev, u32 key, u32 value, 19*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter_elem *elem_list, 20*671e0b90SPeter Ujfalusi int capacity, int *counter) 21*671e0b90SPeter Ujfalusi { 22*671e0b90SPeter Ujfalusi if (*counter >= capacity) 23*671e0b90SPeter Ujfalusi return -ENOMEM; 24*671e0b90SPeter Ujfalusi 25*671e0b90SPeter Ujfalusi elem_list[*counter].key = key; 26*671e0b90SPeter Ujfalusi elem_list[*counter].value = value; 27*671e0b90SPeter Ujfalusi ++*counter; 28*671e0b90SPeter Ujfalusi 29*671e0b90SPeter Ujfalusi return 0; 30*671e0b90SPeter Ujfalusi } 31*671e0b90SPeter Ujfalusi 32*671e0b90SPeter Ujfalusi static int trace_filter_parse_entry(struct snd_sof_dev *sdev, const char *line, 33*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter_elem *elem, 34*671e0b90SPeter Ujfalusi int capacity, int *counter) 35*671e0b90SPeter Ujfalusi { 36*671e0b90SPeter Ujfalusi int log_level, pipe_id, comp_id, read, ret; 37*671e0b90SPeter Ujfalusi int len = strlen(line); 38*671e0b90SPeter Ujfalusi int cnt = *counter; 39*671e0b90SPeter Ujfalusi u32 uuid_id; 40*671e0b90SPeter Ujfalusi 41*671e0b90SPeter Ujfalusi /* ignore empty content */ 42*671e0b90SPeter Ujfalusi ret = sscanf(line, " %n", &read); 43*671e0b90SPeter Ujfalusi if (!ret && read == len) 44*671e0b90SPeter Ujfalusi return len; 45*671e0b90SPeter Ujfalusi 46*671e0b90SPeter Ujfalusi ret = sscanf(line, " %d %x %d %d %n", &log_level, &uuid_id, &pipe_id, &comp_id, &read); 47*671e0b90SPeter Ujfalusi if (ret != TRACE_FILTER_ELEMENTS_PER_ENTRY || read != len) { 48*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Invalid trace filter entry '%s'\n", line); 49*671e0b90SPeter Ujfalusi return -EINVAL; 50*671e0b90SPeter Ujfalusi } 51*671e0b90SPeter Ujfalusi 52*671e0b90SPeter Ujfalusi if (uuid_id > 0) { 53*671e0b90SPeter Ujfalusi ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_UUID, 54*671e0b90SPeter Ujfalusi uuid_id, elem, capacity, &cnt); 55*671e0b90SPeter Ujfalusi if (ret) 56*671e0b90SPeter Ujfalusi return ret; 57*671e0b90SPeter Ujfalusi } 58*671e0b90SPeter Ujfalusi if (pipe_id >= 0) { 59*671e0b90SPeter Ujfalusi ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_PIPE, 60*671e0b90SPeter Ujfalusi pipe_id, elem, capacity, &cnt); 61*671e0b90SPeter Ujfalusi if (ret) 62*671e0b90SPeter Ujfalusi return ret; 63*671e0b90SPeter Ujfalusi } 64*671e0b90SPeter Ujfalusi if (comp_id >= 0) { 65*671e0b90SPeter Ujfalusi ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_COMP, 66*671e0b90SPeter Ujfalusi comp_id, elem, capacity, &cnt); 67*671e0b90SPeter Ujfalusi if (ret) 68*671e0b90SPeter Ujfalusi return ret; 69*671e0b90SPeter Ujfalusi } 70*671e0b90SPeter Ujfalusi 71*671e0b90SPeter Ujfalusi ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_SET_LEVEL | 72*671e0b90SPeter Ujfalusi SOF_IPC_TRACE_FILTER_ELEM_FIN, 73*671e0b90SPeter Ujfalusi log_level, elem, capacity, &cnt); 74*671e0b90SPeter Ujfalusi if (ret) 75*671e0b90SPeter Ujfalusi return ret; 76*671e0b90SPeter Ujfalusi 77*671e0b90SPeter Ujfalusi /* update counter only when parsing whole entry passed */ 78*671e0b90SPeter Ujfalusi *counter = cnt; 79*671e0b90SPeter Ujfalusi 80*671e0b90SPeter Ujfalusi return len; 81*671e0b90SPeter Ujfalusi } 82*671e0b90SPeter Ujfalusi 83*671e0b90SPeter Ujfalusi static int trace_filter_parse(struct snd_sof_dev *sdev, char *string, 84*671e0b90SPeter Ujfalusi int *out_elem_cnt, 85*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter_elem **out) 86*671e0b90SPeter Ujfalusi { 87*671e0b90SPeter Ujfalusi static const char entry_delimiter[] = ";"; 88*671e0b90SPeter Ujfalusi char *entry = string; 89*671e0b90SPeter Ujfalusi int capacity = 0; 90*671e0b90SPeter Ujfalusi int entry_len; 91*671e0b90SPeter Ujfalusi int cnt = 0; 92*671e0b90SPeter Ujfalusi 93*671e0b90SPeter Ujfalusi /* 94*671e0b90SPeter Ujfalusi * Each entry contains at least 1, up to TRACE_FILTER_ELEMENTS_PER_ENTRY 95*671e0b90SPeter Ujfalusi * IPC elements, depending on content. Calculate IPC elements capacity 96*671e0b90SPeter Ujfalusi * for the input string where each element is set. 97*671e0b90SPeter Ujfalusi */ 98*671e0b90SPeter Ujfalusi while (entry) { 99*671e0b90SPeter Ujfalusi capacity += TRACE_FILTER_ELEMENTS_PER_ENTRY; 100*671e0b90SPeter Ujfalusi entry = strchr(entry + 1, entry_delimiter[0]); 101*671e0b90SPeter Ujfalusi } 102*671e0b90SPeter Ujfalusi *out = kmalloc(capacity * sizeof(**out), GFP_KERNEL); 103*671e0b90SPeter Ujfalusi if (!*out) 104*671e0b90SPeter Ujfalusi return -ENOMEM; 105*671e0b90SPeter Ujfalusi 106*671e0b90SPeter Ujfalusi /* split input string by ';', and parse each entry separately in trace_filter_parse_entry */ 107*671e0b90SPeter Ujfalusi while ((entry = strsep(&string, entry_delimiter))) { 108*671e0b90SPeter Ujfalusi entry_len = trace_filter_parse_entry(sdev, entry, *out, capacity, &cnt); 109*671e0b90SPeter Ujfalusi if (entry_len < 0) { 110*671e0b90SPeter Ujfalusi dev_err(sdev->dev, 111*671e0b90SPeter Ujfalusi "Parsing filter entry '%s' failed with %d\n", 112*671e0b90SPeter Ujfalusi entry, entry_len); 113*671e0b90SPeter Ujfalusi return -EINVAL; 114*671e0b90SPeter Ujfalusi } 115*671e0b90SPeter Ujfalusi } 116*671e0b90SPeter Ujfalusi 117*671e0b90SPeter Ujfalusi *out_elem_cnt = cnt; 118*671e0b90SPeter Ujfalusi 119*671e0b90SPeter Ujfalusi return 0; 120*671e0b90SPeter Ujfalusi } 121*671e0b90SPeter Ujfalusi 122*671e0b90SPeter Ujfalusi static int ipc3_trace_update_filter(struct snd_sof_dev *sdev, int num_elems, 123*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter_elem *elems) 124*671e0b90SPeter Ujfalusi { 125*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter *msg; 126*671e0b90SPeter Ujfalusi struct sof_ipc_reply reply; 127*671e0b90SPeter Ujfalusi size_t size; 128*671e0b90SPeter Ujfalusi int ret; 129*671e0b90SPeter Ujfalusi 130*671e0b90SPeter Ujfalusi size = struct_size(msg, elems, num_elems); 131*671e0b90SPeter Ujfalusi if (size > SOF_IPC_MSG_MAX_SIZE) 132*671e0b90SPeter Ujfalusi return -EINVAL; 133*671e0b90SPeter Ujfalusi 134*671e0b90SPeter Ujfalusi msg = kmalloc(size, GFP_KERNEL); 135*671e0b90SPeter Ujfalusi if (!msg) 136*671e0b90SPeter Ujfalusi return -ENOMEM; 137*671e0b90SPeter Ujfalusi 138*671e0b90SPeter Ujfalusi msg->hdr.size = size; 139*671e0b90SPeter Ujfalusi msg->hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_FILTER_UPDATE; 140*671e0b90SPeter Ujfalusi msg->elem_cnt = num_elems; 141*671e0b90SPeter Ujfalusi memcpy(&msg->elems[0], elems, num_elems * sizeof(*elems)); 142*671e0b90SPeter Ujfalusi 143*671e0b90SPeter Ujfalusi ret = pm_runtime_get_sync(sdev->dev); 144*671e0b90SPeter Ujfalusi if (ret < 0 && ret != -EACCES) { 145*671e0b90SPeter Ujfalusi pm_runtime_put_noidle(sdev->dev); 146*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "enabling device failed: %d\n", ret); 147*671e0b90SPeter Ujfalusi goto error; 148*671e0b90SPeter Ujfalusi } 149*671e0b90SPeter Ujfalusi ret = sof_ipc_tx_message(sdev->ipc, msg, msg->hdr.size, &reply, sizeof(reply)); 150*671e0b90SPeter Ujfalusi pm_runtime_mark_last_busy(sdev->dev); 151*671e0b90SPeter Ujfalusi pm_runtime_put_autosuspend(sdev->dev); 152*671e0b90SPeter Ujfalusi 153*671e0b90SPeter Ujfalusi error: 154*671e0b90SPeter Ujfalusi kfree(msg); 155*671e0b90SPeter Ujfalusi return ret ? ret : reply.error; 156*671e0b90SPeter Ujfalusi } 157*671e0b90SPeter Ujfalusi 158*671e0b90SPeter Ujfalusi static ssize_t dfsentry_trace_filter_write(struct file *file, const char __user *from, 159*671e0b90SPeter Ujfalusi size_t count, loff_t *ppos) 160*671e0b90SPeter Ujfalusi { 161*671e0b90SPeter Ujfalusi struct snd_sof_dfsentry *dfse = file->private_data; 162*671e0b90SPeter Ujfalusi struct sof_ipc_trace_filter_elem *elems = NULL; 163*671e0b90SPeter Ujfalusi struct snd_sof_dev *sdev = dfse->sdev; 164*671e0b90SPeter Ujfalusi loff_t pos = 0; 165*671e0b90SPeter Ujfalusi int num_elems; 166*671e0b90SPeter Ujfalusi char *string; 167*671e0b90SPeter Ujfalusi int ret; 168*671e0b90SPeter Ujfalusi 169*671e0b90SPeter Ujfalusi if (count > TRACE_FILTER_MAX_CONFIG_STRING_LENGTH) { 170*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "%s too long input, %zu > %d\n", __func__, count, 171*671e0b90SPeter Ujfalusi TRACE_FILTER_MAX_CONFIG_STRING_LENGTH); 172*671e0b90SPeter Ujfalusi return -EINVAL; 173*671e0b90SPeter Ujfalusi } 174*671e0b90SPeter Ujfalusi 175*671e0b90SPeter Ujfalusi string = kmalloc(count + 1, GFP_KERNEL); 176*671e0b90SPeter Ujfalusi if (!string) 177*671e0b90SPeter Ujfalusi return -ENOMEM; 178*671e0b90SPeter Ujfalusi 179*671e0b90SPeter Ujfalusi /* assert null termination */ 180*671e0b90SPeter Ujfalusi string[count] = 0; 181*671e0b90SPeter Ujfalusi ret = simple_write_to_buffer(string, count, &pos, from, count); 182*671e0b90SPeter Ujfalusi if (ret < 0) 183*671e0b90SPeter Ujfalusi goto error; 184*671e0b90SPeter Ujfalusi 185*671e0b90SPeter Ujfalusi ret = trace_filter_parse(sdev, string, &num_elems, &elems); 186*671e0b90SPeter Ujfalusi if (ret < 0) 187*671e0b90SPeter Ujfalusi goto error; 188*671e0b90SPeter Ujfalusi 189*671e0b90SPeter Ujfalusi if (num_elems) { 190*671e0b90SPeter Ujfalusi ret = ipc3_trace_update_filter(sdev, num_elems, elems); 191*671e0b90SPeter Ujfalusi if (ret < 0) { 192*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Filter update failed: %d\n", ret); 193*671e0b90SPeter Ujfalusi goto error; 194*671e0b90SPeter Ujfalusi } 195*671e0b90SPeter Ujfalusi } 196*671e0b90SPeter Ujfalusi ret = count; 197*671e0b90SPeter Ujfalusi error: 198*671e0b90SPeter Ujfalusi kfree(string); 199*671e0b90SPeter Ujfalusi kfree(elems); 200*671e0b90SPeter Ujfalusi return ret; 201*671e0b90SPeter Ujfalusi } 202*671e0b90SPeter Ujfalusi 203*671e0b90SPeter Ujfalusi static const struct file_operations sof_dfs_trace_filter_fops = { 204*671e0b90SPeter Ujfalusi .open = simple_open, 205*671e0b90SPeter Ujfalusi .write = dfsentry_trace_filter_write, 206*671e0b90SPeter Ujfalusi .llseek = default_llseek, 207*671e0b90SPeter Ujfalusi }; 208*671e0b90SPeter Ujfalusi 209*671e0b90SPeter Ujfalusi static int debugfs_create_trace_filter(struct snd_sof_dev *sdev) 210*671e0b90SPeter Ujfalusi { 211*671e0b90SPeter Ujfalusi struct snd_sof_dfsentry *dfse; 212*671e0b90SPeter Ujfalusi 213*671e0b90SPeter Ujfalusi dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL); 214*671e0b90SPeter Ujfalusi if (!dfse) 215*671e0b90SPeter Ujfalusi return -ENOMEM; 216*671e0b90SPeter Ujfalusi 217*671e0b90SPeter Ujfalusi dfse->sdev = sdev; 218*671e0b90SPeter Ujfalusi dfse->type = SOF_DFSENTRY_TYPE_BUF; 219*671e0b90SPeter Ujfalusi 220*671e0b90SPeter Ujfalusi debugfs_create_file("filter", 0200, sdev->debugfs_root, dfse, 221*671e0b90SPeter Ujfalusi &sof_dfs_trace_filter_fops); 222*671e0b90SPeter Ujfalusi /* add to dfsentry list */ 223*671e0b90SPeter Ujfalusi list_add(&dfse->list, &sdev->dfsentry_list); 224*671e0b90SPeter Ujfalusi 225*671e0b90SPeter Ujfalusi return 0; 226*671e0b90SPeter Ujfalusi } 227*671e0b90SPeter Ujfalusi 228*671e0b90SPeter Ujfalusi static size_t sof_dtrace_avail(struct snd_sof_dev *sdev, 229*671e0b90SPeter Ujfalusi loff_t pos, size_t buffer_size) 230*671e0b90SPeter Ujfalusi { 231*671e0b90SPeter Ujfalusi loff_t host_offset = READ_ONCE(sdev->host_offset); 232*671e0b90SPeter Ujfalusi 233*671e0b90SPeter Ujfalusi /* 234*671e0b90SPeter Ujfalusi * If host offset is less than local pos, it means write pointer of 235*671e0b90SPeter Ujfalusi * host DMA buffer has been wrapped. We should output the trace data 236*671e0b90SPeter Ujfalusi * at the end of host DMA buffer at first. 237*671e0b90SPeter Ujfalusi */ 238*671e0b90SPeter Ujfalusi if (host_offset < pos) 239*671e0b90SPeter Ujfalusi return buffer_size - pos; 240*671e0b90SPeter Ujfalusi 241*671e0b90SPeter Ujfalusi /* If there is available trace data now, it is unnecessary to wait. */ 242*671e0b90SPeter Ujfalusi if (host_offset > pos) 243*671e0b90SPeter Ujfalusi return host_offset - pos; 244*671e0b90SPeter Ujfalusi 245*671e0b90SPeter Ujfalusi return 0; 246*671e0b90SPeter Ujfalusi } 247*671e0b90SPeter Ujfalusi 248*671e0b90SPeter Ujfalusi static size_t sof_wait_dtrace_avail(struct snd_sof_dev *sdev, loff_t pos, 249*671e0b90SPeter Ujfalusi size_t buffer_size) 250*671e0b90SPeter Ujfalusi { 251*671e0b90SPeter Ujfalusi wait_queue_entry_t wait; 252*671e0b90SPeter Ujfalusi size_t ret = sof_dtrace_avail(sdev, pos, buffer_size); 253*671e0b90SPeter Ujfalusi 254*671e0b90SPeter Ujfalusi /* data immediately available */ 255*671e0b90SPeter Ujfalusi if (ret) 256*671e0b90SPeter Ujfalusi return ret; 257*671e0b90SPeter Ujfalusi 258*671e0b90SPeter Ujfalusi if (sdev->dtrace_state != SOF_DTRACE_ENABLED && sdev->dtrace_draining) { 259*671e0b90SPeter Ujfalusi /* 260*671e0b90SPeter Ujfalusi * tracing has ended and all traces have been 261*671e0b90SPeter Ujfalusi * read by client, return EOF 262*671e0b90SPeter Ujfalusi */ 263*671e0b90SPeter Ujfalusi sdev->dtrace_draining = false; 264*671e0b90SPeter Ujfalusi return 0; 265*671e0b90SPeter Ujfalusi } 266*671e0b90SPeter Ujfalusi 267*671e0b90SPeter Ujfalusi /* wait for available trace data from FW */ 268*671e0b90SPeter Ujfalusi init_waitqueue_entry(&wait, current); 269*671e0b90SPeter Ujfalusi set_current_state(TASK_INTERRUPTIBLE); 270*671e0b90SPeter Ujfalusi add_wait_queue(&sdev->trace_sleep, &wait); 271*671e0b90SPeter Ujfalusi 272*671e0b90SPeter Ujfalusi if (!signal_pending(current)) { 273*671e0b90SPeter Ujfalusi /* set timeout to max value, no error code */ 274*671e0b90SPeter Ujfalusi schedule_timeout(MAX_SCHEDULE_TIMEOUT); 275*671e0b90SPeter Ujfalusi } 276*671e0b90SPeter Ujfalusi remove_wait_queue(&sdev->trace_sleep, &wait); 277*671e0b90SPeter Ujfalusi 278*671e0b90SPeter Ujfalusi return sof_dtrace_avail(sdev, pos, buffer_size); 279*671e0b90SPeter Ujfalusi } 280*671e0b90SPeter Ujfalusi 281*671e0b90SPeter Ujfalusi static ssize_t dfsentry_dtrace_read(struct file *file, char __user *buffer, 282*671e0b90SPeter Ujfalusi size_t count, loff_t *ppos) 283*671e0b90SPeter Ujfalusi { 284*671e0b90SPeter Ujfalusi struct snd_sof_dfsentry *dfse = file->private_data; 285*671e0b90SPeter Ujfalusi struct snd_sof_dev *sdev = dfse->sdev; 286*671e0b90SPeter Ujfalusi unsigned long rem; 287*671e0b90SPeter Ujfalusi loff_t lpos = *ppos; 288*671e0b90SPeter Ujfalusi size_t avail, buffer_size = dfse->size; 289*671e0b90SPeter Ujfalusi u64 lpos_64; 290*671e0b90SPeter Ujfalusi 291*671e0b90SPeter Ujfalusi /* make sure we know about any failures on the DSP side */ 292*671e0b90SPeter Ujfalusi sdev->dtrace_error = false; 293*671e0b90SPeter Ujfalusi 294*671e0b90SPeter Ujfalusi /* check pos and count */ 295*671e0b90SPeter Ujfalusi if (lpos < 0) 296*671e0b90SPeter Ujfalusi return -EINVAL; 297*671e0b90SPeter Ujfalusi if (!count) 298*671e0b90SPeter Ujfalusi return 0; 299*671e0b90SPeter Ujfalusi 300*671e0b90SPeter Ujfalusi /* check for buffer wrap and count overflow */ 301*671e0b90SPeter Ujfalusi lpos_64 = lpos; 302*671e0b90SPeter Ujfalusi lpos = do_div(lpos_64, buffer_size); 303*671e0b90SPeter Ujfalusi 304*671e0b90SPeter Ujfalusi /* get available count based on current host offset */ 305*671e0b90SPeter Ujfalusi avail = sof_wait_dtrace_avail(sdev, lpos, buffer_size); 306*671e0b90SPeter Ujfalusi if (sdev->dtrace_error) { 307*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "trace IO error\n"); 308*671e0b90SPeter Ujfalusi return -EIO; 309*671e0b90SPeter Ujfalusi } 310*671e0b90SPeter Ujfalusi 311*671e0b90SPeter Ujfalusi /* make sure count is <= avail */ 312*671e0b90SPeter Ujfalusi if (count > avail) 313*671e0b90SPeter Ujfalusi count = avail; 314*671e0b90SPeter Ujfalusi 315*671e0b90SPeter Ujfalusi /* 316*671e0b90SPeter Ujfalusi * make sure that all trace data is available for the CPU as the trace 317*671e0b90SPeter Ujfalusi * data buffer might be allocated from non consistent memory. 318*671e0b90SPeter Ujfalusi * Note: snd_dma_buffer_sync() is called for normal audio playback and 319*671e0b90SPeter Ujfalusi * capture streams also. 320*671e0b90SPeter Ujfalusi */ 321*671e0b90SPeter Ujfalusi snd_dma_buffer_sync(&sdev->dmatb, SNDRV_DMA_SYNC_CPU); 322*671e0b90SPeter Ujfalusi /* copy available trace data to debugfs */ 323*671e0b90SPeter Ujfalusi rem = copy_to_user(buffer, ((u8 *)(dfse->buf) + lpos), count); 324*671e0b90SPeter Ujfalusi if (rem) 325*671e0b90SPeter Ujfalusi return -EFAULT; 326*671e0b90SPeter Ujfalusi 327*671e0b90SPeter Ujfalusi *ppos += count; 328*671e0b90SPeter Ujfalusi 329*671e0b90SPeter Ujfalusi /* move debugfs reading position */ 330*671e0b90SPeter Ujfalusi return count; 331*671e0b90SPeter Ujfalusi } 332*671e0b90SPeter Ujfalusi 333*671e0b90SPeter Ujfalusi static int dfsentry_dtrace_release(struct inode *inode, struct file *file) 334*671e0b90SPeter Ujfalusi { 335*671e0b90SPeter Ujfalusi struct snd_sof_dfsentry *dfse = inode->i_private; 336*671e0b90SPeter Ujfalusi struct snd_sof_dev *sdev = dfse->sdev; 337*671e0b90SPeter Ujfalusi 338*671e0b90SPeter Ujfalusi /* avoid duplicate traces at next open */ 339*671e0b90SPeter Ujfalusi if (sdev->dtrace_state != SOF_DTRACE_ENABLED) 340*671e0b90SPeter Ujfalusi sdev->host_offset = 0; 341*671e0b90SPeter Ujfalusi 342*671e0b90SPeter Ujfalusi return 0; 343*671e0b90SPeter Ujfalusi } 344*671e0b90SPeter Ujfalusi 345*671e0b90SPeter Ujfalusi static const struct file_operations sof_dfs_dtrace_fops = { 346*671e0b90SPeter Ujfalusi .open = simple_open, 347*671e0b90SPeter Ujfalusi .read = dfsentry_dtrace_read, 348*671e0b90SPeter Ujfalusi .llseek = default_llseek, 349*671e0b90SPeter Ujfalusi .release = dfsentry_dtrace_release, 350*671e0b90SPeter Ujfalusi }; 351*671e0b90SPeter Ujfalusi 352*671e0b90SPeter Ujfalusi static int debugfs_create_dtrace(struct snd_sof_dev *sdev) 353*671e0b90SPeter Ujfalusi { 354*671e0b90SPeter Ujfalusi struct snd_sof_dfsentry *dfse; 355*671e0b90SPeter Ujfalusi int ret; 356*671e0b90SPeter Ujfalusi 357*671e0b90SPeter Ujfalusi if (!sdev) 358*671e0b90SPeter Ujfalusi return -EINVAL; 359*671e0b90SPeter Ujfalusi 360*671e0b90SPeter Ujfalusi ret = debugfs_create_trace_filter(sdev); 361*671e0b90SPeter Ujfalusi if (ret < 0) 362*671e0b90SPeter Ujfalusi dev_warn(sdev->dev, "failed to create filter debugfs file: %d", ret); 363*671e0b90SPeter Ujfalusi 364*671e0b90SPeter Ujfalusi dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL); 365*671e0b90SPeter Ujfalusi if (!dfse) 366*671e0b90SPeter Ujfalusi return -ENOMEM; 367*671e0b90SPeter Ujfalusi 368*671e0b90SPeter Ujfalusi dfse->type = SOF_DFSENTRY_TYPE_BUF; 369*671e0b90SPeter Ujfalusi dfse->buf = sdev->dmatb.area; 370*671e0b90SPeter Ujfalusi dfse->size = sdev->dmatb.bytes; 371*671e0b90SPeter Ujfalusi dfse->sdev = sdev; 372*671e0b90SPeter Ujfalusi 373*671e0b90SPeter Ujfalusi debugfs_create_file("trace", 0444, sdev->debugfs_root, dfse, 374*671e0b90SPeter Ujfalusi &sof_dfs_dtrace_fops); 375*671e0b90SPeter Ujfalusi 376*671e0b90SPeter Ujfalusi return 0; 377*671e0b90SPeter Ujfalusi } 378*671e0b90SPeter Ujfalusi 379*671e0b90SPeter Ujfalusi static int ipc3_dtrace_enable(struct snd_sof_dev *sdev) 380*671e0b90SPeter Ujfalusi { 381*671e0b90SPeter Ujfalusi struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 382*671e0b90SPeter Ujfalusi struct sof_ipc_fw_version *v = &ready->version; 383*671e0b90SPeter Ujfalusi struct sof_ipc_dma_trace_params_ext params; 384*671e0b90SPeter Ujfalusi struct sof_ipc_reply ipc_reply; 385*671e0b90SPeter Ujfalusi int ret; 386*671e0b90SPeter Ujfalusi 387*671e0b90SPeter Ujfalusi if (!sdev->fw_trace_is_supported) 388*671e0b90SPeter Ujfalusi return 0; 389*671e0b90SPeter Ujfalusi 390*671e0b90SPeter Ujfalusi if (sdev->dtrace_state == SOF_DTRACE_ENABLED || !sdev->dma_trace_pages) 391*671e0b90SPeter Ujfalusi return -EINVAL; 392*671e0b90SPeter Ujfalusi 393*671e0b90SPeter Ujfalusi if (sdev->dtrace_state == SOF_DTRACE_STOPPED) 394*671e0b90SPeter Ujfalusi goto start; 395*671e0b90SPeter Ujfalusi 396*671e0b90SPeter Ujfalusi /* set IPC parameters */ 397*671e0b90SPeter Ujfalusi params.hdr.cmd = SOF_IPC_GLB_TRACE_MSG; 398*671e0b90SPeter Ujfalusi /* PARAMS_EXT is only supported from ABI 3.7.0 onwards */ 399*671e0b90SPeter Ujfalusi if (v->abi_version >= SOF_ABI_VER(3, 7, 0)) { 400*671e0b90SPeter Ujfalusi params.hdr.size = sizeof(struct sof_ipc_dma_trace_params_ext); 401*671e0b90SPeter Ujfalusi params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS_EXT; 402*671e0b90SPeter Ujfalusi params.timestamp_ns = ktime_get(); /* in nanosecond */ 403*671e0b90SPeter Ujfalusi } else { 404*671e0b90SPeter Ujfalusi params.hdr.size = sizeof(struct sof_ipc_dma_trace_params); 405*671e0b90SPeter Ujfalusi params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS; 406*671e0b90SPeter Ujfalusi } 407*671e0b90SPeter Ujfalusi params.buffer.phy_addr = sdev->dmatp.addr; 408*671e0b90SPeter Ujfalusi params.buffer.size = sdev->dmatb.bytes; 409*671e0b90SPeter Ujfalusi params.buffer.pages = sdev->dma_trace_pages; 410*671e0b90SPeter Ujfalusi params.stream_tag = 0; 411*671e0b90SPeter Ujfalusi 412*671e0b90SPeter Ujfalusi sdev->host_offset = 0; 413*671e0b90SPeter Ujfalusi sdev->dtrace_draining = false; 414*671e0b90SPeter Ujfalusi 415*671e0b90SPeter Ujfalusi ret = snd_sof_dma_trace_init(sdev, ¶ms); 416*671e0b90SPeter Ujfalusi if (ret < 0) { 417*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Host dtrace init failed: %d\n", ret); 418*671e0b90SPeter Ujfalusi return ret; 419*671e0b90SPeter Ujfalusi } 420*671e0b90SPeter Ujfalusi dev_dbg(sdev->dev, "%s: stream_tag: %d\n", __func__, params.stream_tag); 421*671e0b90SPeter Ujfalusi 422*671e0b90SPeter Ujfalusi /* send IPC to the DSP */ 423*671e0b90SPeter Ujfalusi ret = sof_ipc_tx_message(sdev->ipc, ¶ms, sizeof(params), &ipc_reply, sizeof(ipc_reply)); 424*671e0b90SPeter Ujfalusi if (ret < 0) { 425*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "can't set params for DMA for trace %d\n", ret); 426*671e0b90SPeter Ujfalusi goto trace_release; 427*671e0b90SPeter Ujfalusi } 428*671e0b90SPeter Ujfalusi 429*671e0b90SPeter Ujfalusi start: 430*671e0b90SPeter Ujfalusi ret = snd_sof_dma_trace_trigger(sdev, SNDRV_PCM_TRIGGER_START); 431*671e0b90SPeter Ujfalusi if (ret < 0) { 432*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Host dtrace trigger start failed: %d\n", ret); 433*671e0b90SPeter Ujfalusi goto trace_release; 434*671e0b90SPeter Ujfalusi } 435*671e0b90SPeter Ujfalusi 436*671e0b90SPeter Ujfalusi sdev->dtrace_state = SOF_DTRACE_ENABLED; 437*671e0b90SPeter Ujfalusi 438*671e0b90SPeter Ujfalusi return 0; 439*671e0b90SPeter Ujfalusi 440*671e0b90SPeter Ujfalusi trace_release: 441*671e0b90SPeter Ujfalusi snd_sof_dma_trace_release(sdev); 442*671e0b90SPeter Ujfalusi return ret; 443*671e0b90SPeter Ujfalusi } 444*671e0b90SPeter Ujfalusi 445*671e0b90SPeter Ujfalusi static int ipc3_dtrace_init(struct snd_sof_dev *sdev) 446*671e0b90SPeter Ujfalusi { 447*671e0b90SPeter Ujfalusi int ret; 448*671e0b90SPeter Ujfalusi 449*671e0b90SPeter Ujfalusi /* dtrace is only supported with SOF_IPC */ 450*671e0b90SPeter Ujfalusi if (sdev->pdata->ipc_type != SOF_IPC) 451*671e0b90SPeter Ujfalusi return -EOPNOTSUPP; 452*671e0b90SPeter Ujfalusi 453*671e0b90SPeter Ujfalusi /* set false before start initialization */ 454*671e0b90SPeter Ujfalusi sdev->dtrace_state = SOF_DTRACE_DISABLED; 455*671e0b90SPeter Ujfalusi 456*671e0b90SPeter Ujfalusi /* allocate trace page table buffer */ 457*671e0b90SPeter Ujfalusi ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev, 458*671e0b90SPeter Ujfalusi PAGE_SIZE, &sdev->dmatp); 459*671e0b90SPeter Ujfalusi if (ret < 0) { 460*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "can't alloc page table for trace %d\n", ret); 461*671e0b90SPeter Ujfalusi return ret; 462*671e0b90SPeter Ujfalusi } 463*671e0b90SPeter Ujfalusi 464*671e0b90SPeter Ujfalusi /* allocate trace data buffer */ 465*671e0b90SPeter Ujfalusi ret = snd_dma_alloc_dir_pages(SNDRV_DMA_TYPE_DEV_SG, sdev->dev, 466*671e0b90SPeter Ujfalusi DMA_FROM_DEVICE, DMA_BUF_SIZE_FOR_TRACE, 467*671e0b90SPeter Ujfalusi &sdev->dmatb); 468*671e0b90SPeter Ujfalusi if (ret < 0) { 469*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "can't alloc buffer for trace %d\n", ret); 470*671e0b90SPeter Ujfalusi goto page_err; 471*671e0b90SPeter Ujfalusi } 472*671e0b90SPeter Ujfalusi 473*671e0b90SPeter Ujfalusi /* create compressed page table for audio firmware */ 474*671e0b90SPeter Ujfalusi ret = snd_sof_create_page_table(sdev->dev, &sdev->dmatb, 475*671e0b90SPeter Ujfalusi sdev->dmatp.area, sdev->dmatb.bytes); 476*671e0b90SPeter Ujfalusi if (ret < 0) 477*671e0b90SPeter Ujfalusi goto table_err; 478*671e0b90SPeter Ujfalusi 479*671e0b90SPeter Ujfalusi sdev->dma_trace_pages = ret; 480*671e0b90SPeter Ujfalusi dev_dbg(sdev->dev, "%s: dma_trace_pages: %d\n", __func__, 481*671e0b90SPeter Ujfalusi sdev->dma_trace_pages); 482*671e0b90SPeter Ujfalusi 483*671e0b90SPeter Ujfalusi if (sdev->first_boot) { 484*671e0b90SPeter Ujfalusi ret = debugfs_create_dtrace(sdev); 485*671e0b90SPeter Ujfalusi if (ret < 0) 486*671e0b90SPeter Ujfalusi goto table_err; 487*671e0b90SPeter Ujfalusi } 488*671e0b90SPeter Ujfalusi 489*671e0b90SPeter Ujfalusi init_waitqueue_head(&sdev->trace_sleep); 490*671e0b90SPeter Ujfalusi 491*671e0b90SPeter Ujfalusi ret = ipc3_dtrace_enable(sdev); 492*671e0b90SPeter Ujfalusi if (ret < 0) 493*671e0b90SPeter Ujfalusi goto table_err; 494*671e0b90SPeter Ujfalusi 495*671e0b90SPeter Ujfalusi return 0; 496*671e0b90SPeter Ujfalusi table_err: 497*671e0b90SPeter Ujfalusi sdev->dma_trace_pages = 0; 498*671e0b90SPeter Ujfalusi snd_dma_free_pages(&sdev->dmatb); 499*671e0b90SPeter Ujfalusi page_err: 500*671e0b90SPeter Ujfalusi snd_dma_free_pages(&sdev->dmatp); 501*671e0b90SPeter Ujfalusi return ret; 502*671e0b90SPeter Ujfalusi } 503*671e0b90SPeter Ujfalusi 504*671e0b90SPeter Ujfalusi int ipc3_dtrace_posn_update(struct snd_sof_dev *sdev, 505*671e0b90SPeter Ujfalusi struct sof_ipc_dma_trace_posn *posn) 506*671e0b90SPeter Ujfalusi { 507*671e0b90SPeter Ujfalusi if (!sdev->fw_trace_is_supported) 508*671e0b90SPeter Ujfalusi return 0; 509*671e0b90SPeter Ujfalusi 510*671e0b90SPeter Ujfalusi if (sdev->dtrace_state == SOF_DTRACE_ENABLED && 511*671e0b90SPeter Ujfalusi sdev->host_offset != posn->host_offset) { 512*671e0b90SPeter Ujfalusi sdev->host_offset = posn->host_offset; 513*671e0b90SPeter Ujfalusi wake_up(&sdev->trace_sleep); 514*671e0b90SPeter Ujfalusi } 515*671e0b90SPeter Ujfalusi 516*671e0b90SPeter Ujfalusi if (posn->overflow != 0) 517*671e0b90SPeter Ujfalusi dev_err(sdev->dev, 518*671e0b90SPeter Ujfalusi "DSP trace buffer overflow %u bytes. Total messages %d\n", 519*671e0b90SPeter Ujfalusi posn->overflow, posn->messages); 520*671e0b90SPeter Ujfalusi 521*671e0b90SPeter Ujfalusi return 0; 522*671e0b90SPeter Ujfalusi } 523*671e0b90SPeter Ujfalusi 524*671e0b90SPeter Ujfalusi /* an error has occurred within the DSP that prevents further trace */ 525*671e0b90SPeter Ujfalusi static void ipc3_dtrace_fw_crashed(struct snd_sof_dev *sdev) 526*671e0b90SPeter Ujfalusi { 527*671e0b90SPeter Ujfalusi if (sdev->dtrace_state == SOF_DTRACE_ENABLED) { 528*671e0b90SPeter Ujfalusi sdev->dtrace_error = true; 529*671e0b90SPeter Ujfalusi wake_up(&sdev->trace_sleep); 530*671e0b90SPeter Ujfalusi } 531*671e0b90SPeter Ujfalusi } 532*671e0b90SPeter Ujfalusi 533*671e0b90SPeter Ujfalusi static void ipc3_dtrace_release(struct snd_sof_dev *sdev, bool only_stop) 534*671e0b90SPeter Ujfalusi { 535*671e0b90SPeter Ujfalusi struct sof_ipc_fw_ready *ready = &sdev->fw_ready; 536*671e0b90SPeter Ujfalusi struct sof_ipc_fw_version *v = &ready->version; 537*671e0b90SPeter Ujfalusi struct sof_ipc_cmd_hdr hdr; 538*671e0b90SPeter Ujfalusi struct sof_ipc_reply ipc_reply; 539*671e0b90SPeter Ujfalusi int ret; 540*671e0b90SPeter Ujfalusi 541*671e0b90SPeter Ujfalusi if (!sdev->fw_trace_is_supported || sdev->dtrace_state == SOF_DTRACE_DISABLED) 542*671e0b90SPeter Ujfalusi return; 543*671e0b90SPeter Ujfalusi 544*671e0b90SPeter Ujfalusi ret = snd_sof_dma_trace_trigger(sdev, SNDRV_PCM_TRIGGER_STOP); 545*671e0b90SPeter Ujfalusi if (ret < 0) 546*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Host dtrace trigger stop failed: %d\n", ret); 547*671e0b90SPeter Ujfalusi sdev->dtrace_state = SOF_DTRACE_STOPPED; 548*671e0b90SPeter Ujfalusi 549*671e0b90SPeter Ujfalusi /* 550*671e0b90SPeter Ujfalusi * stop and free trace DMA in the DSP. TRACE_DMA_FREE is only supported from 551*671e0b90SPeter Ujfalusi * ABI 3.20.0 onwards 552*671e0b90SPeter Ujfalusi */ 553*671e0b90SPeter Ujfalusi if (v->abi_version >= SOF_ABI_VER(3, 20, 0)) { 554*671e0b90SPeter Ujfalusi hdr.size = sizeof(hdr); 555*671e0b90SPeter Ujfalusi hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_DMA_FREE; 556*671e0b90SPeter Ujfalusi 557*671e0b90SPeter Ujfalusi ret = sof_ipc_tx_message(sdev->ipc, &hdr, hdr.size, 558*671e0b90SPeter Ujfalusi &ipc_reply, sizeof(ipc_reply)); 559*671e0b90SPeter Ujfalusi if (ret < 0) 560*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "DMA_TRACE_FREE failed with error: %d\n", ret); 561*671e0b90SPeter Ujfalusi } 562*671e0b90SPeter Ujfalusi 563*671e0b90SPeter Ujfalusi if (only_stop) 564*671e0b90SPeter Ujfalusi goto out; 565*671e0b90SPeter Ujfalusi 566*671e0b90SPeter Ujfalusi ret = snd_sof_dma_trace_release(sdev); 567*671e0b90SPeter Ujfalusi if (ret < 0) 568*671e0b90SPeter Ujfalusi dev_err(sdev->dev, "Host dtrace release failed %d\n", ret); 569*671e0b90SPeter Ujfalusi 570*671e0b90SPeter Ujfalusi sdev->dtrace_state = SOF_DTRACE_DISABLED; 571*671e0b90SPeter Ujfalusi 572*671e0b90SPeter Ujfalusi out: 573*671e0b90SPeter Ujfalusi sdev->dtrace_draining = true; 574*671e0b90SPeter Ujfalusi wake_up(&sdev->trace_sleep); 575*671e0b90SPeter Ujfalusi } 576*671e0b90SPeter Ujfalusi 577*671e0b90SPeter Ujfalusi static void ipc3_dtrace_suspend(struct snd_sof_dev *sdev, pm_message_t pm_state) 578*671e0b90SPeter Ujfalusi { 579*671e0b90SPeter Ujfalusi ipc3_dtrace_release(sdev, pm_state.event == SOF_DSP_PM_D0); 580*671e0b90SPeter Ujfalusi } 581*671e0b90SPeter Ujfalusi 582*671e0b90SPeter Ujfalusi static int ipc3_dtrace_resume(struct snd_sof_dev *sdev) 583*671e0b90SPeter Ujfalusi { 584*671e0b90SPeter Ujfalusi return ipc3_dtrace_enable(sdev); 585*671e0b90SPeter Ujfalusi } 586*671e0b90SPeter Ujfalusi 587*671e0b90SPeter Ujfalusi static void ipc3_dtrace_free(struct snd_sof_dev *sdev) 588*671e0b90SPeter Ujfalusi { 589*671e0b90SPeter Ujfalusi /* release trace */ 590*671e0b90SPeter Ujfalusi ipc3_dtrace_release(sdev, false); 591*671e0b90SPeter Ujfalusi 592*671e0b90SPeter Ujfalusi if (sdev->dma_trace_pages) { 593*671e0b90SPeter Ujfalusi snd_dma_free_pages(&sdev->dmatb); 594*671e0b90SPeter Ujfalusi snd_dma_free_pages(&sdev->dmatp); 595*671e0b90SPeter Ujfalusi sdev->dma_trace_pages = 0; 596*671e0b90SPeter Ujfalusi } 597*671e0b90SPeter Ujfalusi } 598*671e0b90SPeter Ujfalusi 599*671e0b90SPeter Ujfalusi const struct sof_ipc_fw_tracing_ops ipc3_dtrace_ops = { 600*671e0b90SPeter Ujfalusi .init = ipc3_dtrace_init, 601*671e0b90SPeter Ujfalusi .free = ipc3_dtrace_free, 602*671e0b90SPeter Ujfalusi .fw_crashed = ipc3_dtrace_fw_crashed, 603*671e0b90SPeter Ujfalusi .suspend = ipc3_dtrace_suspend, 604*671e0b90SPeter Ujfalusi .resume = ipc3_dtrace_resume, 605*671e0b90SPeter Ujfalusi }; 606