Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/sound/sof/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
#define SOF_IPC_TRACE_DMA_PARAMS SOF_CMD_TYPE(0x001)
#define SOF_IPC_TRACE_DMA_POSITION SOF_CMD_TYPE(0x002)
#define SOF_IPC_TRACE_DMA_PARAMS_EXT SOF_CMD_TYPE(0x003)
#define SOF_IPC_TRACE_FILTER_UPDATE SOF_CMD_TYPE(0x004) /**< ABI3.17 */

/* debug */
#define SOF_IPC_TEST_IPC_FLOOD SOF_CMD_TYPE(0x001)
Expand Down
28 changes: 28 additions & 0 deletions include/sound/sof/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ struct sof_ipc_dma_trace_posn {
uint32_t messages; /* total trace messages */
} __packed;

/* Values used in sof_ipc_trace_filter_elem: */

/* bits 6..0 */
#define SOF_IPC_TRACE_FILTER_ELEM_SET_LEVEL 0x01 /**< trace level for selected components */
#define SOF_IPC_TRACE_FILTER_ELEM_BY_UUID 0x02 /**< filter by uuid key */
#define SOF_IPC_TRACE_FILTER_ELEM_BY_PIPE 0x03 /**< filter by pipeline */
#define SOF_IPC_TRACE_FILTER_ELEM_BY_COMP 0x04 /**< filter by component id */

/* bit 7 */
#define SOF_IPC_TRACE_FILTER_ELEM_FIN 0x80 /**< mark last filter in set */

/* bits 31..8: Unused */

/** part of sof_ipc_trace_filter, ABI3.17 */
struct sof_ipc_trace_filter_elem {
uint32_t key; /**< SOF_IPC_TRACE_FILTER_ELEM_ {LEVEL, UUID, COMP, PIPE} */
uint32_t value; /**< element value */
} __packed;

/** Runtime tracing filtration data - SOF_IPC_TRACE_FILTER_UPDATE, ABI3.17 */
struct sof_ipc_trace_filter {
struct sof_ipc_cmd_hdr hdr; /**< IPC command header */
uint32_t elem_cnt; /**< number of entries in elems[] array */
uint32_t reserved[8]; /**< reserved for future usage */
/** variable size array with new filtering settings */
struct sof_ipc_trace_filter_elem elems[];
} __packed;

/*
* Commom debug
*/
Expand Down
223 changes: 223 additions & 0 deletions sound/soc/sof/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,224 @@
#include "sof-priv.h"
#include "ops.h"

#define TRACE_FILTER_ELEMENTS_PER_ENTRY 4
#define TRACE_FILTER_MAX_CONFIG_STRING_LENGTH 1024

static int trace_filter_append_elem(struct snd_sof_dev *sdev, uint32_t key, uint32_t value,
struct sof_ipc_trace_filter_elem *elem_list,
int capacity, int *counter)
{
if (*counter >= capacity)
return -ENOMEM;

elem_list[*counter].key = key;
elem_list[*counter].value = value;
++*counter;

return 0;
}

static int trace_filter_parse_entry(struct snd_sof_dev *sdev, const char *line,
struct sof_ipc_trace_filter_elem *elem,
int capacity, int *counter)
{
int len = strlen(line);
int cnt = *counter;
uint32_t uuid_id;
int log_level;
int pipe_id;
int comp_id;
int read;
int ret;

/* ignore empty content */
if (!line[0])
return 0;

ret = sscanf(line, " %d %x %d %d %n", &log_level, &uuid_id, &pipe_id, &comp_id, &read);
if (ret != TRACE_FILTER_ELEMENTS_PER_ENTRY || read != len) {
dev_err(sdev->dev, "error: invalid trace filter entry '%s'\n", line);
return -EINVAL;
}

if (uuid_id > 0) {
ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_UUID,
uuid_id, elem, capacity, &cnt);
if (ret)
return ret;
}
if (pipe_id >= 0) {
ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_PIPE,
pipe_id, elem, capacity, &cnt);
if (ret)
return ret;
}
if (comp_id >= 0) {
ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_COMP,
comp_id, elem, capacity, &cnt);
if (ret)
return ret;
}

ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_SET_LEVEL |
SOF_IPC_TRACE_FILTER_ELEM_FIN,
log_level, elem, capacity, &cnt);
if (ret)
return ret;

/* update counter only when parsing whole entry passed */
*counter = cnt;

return len;
}

static int trace_filter_parse(struct snd_sof_dev *sdev, char *string,
int *out_elem_cnt,
struct sof_ipc_trace_filter_elem **out)
{
static const char entry_delimiter[] = ";";
char *entry = string;
int capacity = 0;
int entry_len;
int cnt = 0;

/*
* Each entry contains at least 1, up to TRACE_FILTER_ELEMENTS_PER_ENTRY
* IPC elements, depending on content. Calculate IPC elements capacity
* for the input string where each element is set.
*/
while (entry) {
capacity += TRACE_FILTER_ELEMENTS_PER_ENTRY;
entry = strchr(entry + 1, entry_delimiter[0]);
}
*out = kmalloc(capacity * sizeof(**out), GFP_KERNEL);
if (!*out)
return -ENOMEM;

/* split input string by ';', and parse each entry separately in trace_filter_parse_entry */
while ((entry = strsep(&string, entry_delimiter))) {
entry_len = trace_filter_parse_entry(sdev, entry, *out, capacity, &cnt);
if (entry_len < 0) {
dev_err(sdev->dev, "error: %s failed for '%s', %d\n", __func__, entry,
entry_len);
return -EINVAL;
}
}

*out_elem_cnt = cnt;

return 0;
}

static int sof_ipc_trace_update_filter(struct snd_sof_dev *sdev, int num_elems,
struct sof_ipc_trace_filter_elem *elems)
{
struct sof_ipc_trace_filter *msg;
struct sof_ipc_reply reply;
size_t size;
int ret;

size = struct_size(msg, elems, num_elems);
if (size > SOF_IPC_MSG_MAX_SIZE)
return -EINVAL;

msg = kmalloc(size, GFP_KERNEL);
if (!msg)
return -ENOMEM;

msg->hdr.size = size;
msg->hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_FILTER_UPDATE;
msg->elem_cnt = num_elems;
memcpy(&msg->elems[0], elems, num_elems * sizeof(*elems));

ret = pm_runtime_get_sync(sdev->dev);
if (ret < 0 && ret != -EACCES) {
pm_runtime_put_noidle(sdev->dev);
dev_err(sdev->dev, "error: enabling device failed: %d\n", ret);
goto error;
}
ret = sof_ipc_tx_message(sdev->ipc, msg->hdr.cmd, msg, msg->hdr.size,
&reply, sizeof(reply));
pm_runtime_mark_last_busy(sdev->dev);
pm_runtime_put_autosuspend(sdev->dev);

error:
kfree(msg);
return ret ? ret : reply.error;
}

static ssize_t sof_dfsentry_trace_filter_write(struct file *file, const char __user *from,
size_t count, loff_t *ppos)
{
struct snd_sof_dfsentry *dfse = file->private_data;
struct sof_ipc_trace_filter_elem *elems = NULL;
struct snd_sof_dev *sdev = dfse->sdev;
loff_t pos = 0;
int num_elems;
char *string;
int ret;

if (count > TRACE_FILTER_MAX_CONFIG_STRING_LENGTH) {
dev_err(sdev->dev, "%s too long input, %zu > %d\n", __func__, count,
TRACE_FILTER_MAX_CONFIG_STRING_LENGTH);
return -EINVAL;
}

string = kmalloc(count + 1, GFP_KERNEL);
if (!string)
return -ENOMEM;

/* assert null termination */
string[count] = 0;
ret = simple_write_to_buffer(string, count, &pos, from, count);
if (ret < 0)
goto error;

ret = trace_filter_parse(sdev, string, &num_elems, &elems);
if (ret < 0) {
dev_err(sdev->dev, "error: fail in trace_filter_parse, %d\n", ret);
goto error;
}

if (num_elems) {
ret = sof_ipc_trace_update_filter(sdev, num_elems, elems);
if (ret < 0) {
dev_err(sdev->dev, "error: fail in sof_ipc_trace_update_filter %d\n", ret);
goto error;
}
}
ret = count;
error:
kfree(string);
kfree(elems);
return ret;
}

static const struct file_operations sof_dfs_trace_filter_fops = {
.open = simple_open,
.write = sof_dfsentry_trace_filter_write,
.llseek = default_llseek,
};

static int trace_debugfs_filter_create(struct snd_sof_dev *sdev)
{
struct snd_sof_dfsentry *dfse;

dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
if (!dfse)
return -ENOMEM;

dfse->sdev = sdev;
dfse->type = SOF_DFSENTRY_TYPE_BUF;

debugfs_create_file("filter", 0200, sdev->debugfs_root, dfse,
&sof_dfs_trace_filter_fops);
/* add to dfsentry list */
list_add(&dfse->list, &sdev->dfsentry_list);

return 0;
}

static size_t sof_trace_avail(struct snd_sof_dev *sdev,
loff_t pos, size_t buffer_size)
{
Expand Down Expand Up @@ -135,10 +353,15 @@ static const struct file_operations sof_dfs_trace_fops = {
static int trace_debugfs_create(struct snd_sof_dev *sdev)
{
struct snd_sof_dfsentry *dfse;
int ret;

if (!sdev)
return -EINVAL;

ret = trace_debugfs_filter_create(sdev);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we return an error code from trace_debugfs_create(), snd_sof_init_trace() will fail too and leave the system with no SOF tracing. This isn't fatal for SOF probing / functionality, but maybe it's better to fail filter initialisation "silently." In fact I'd rather change snd_sof_init_trace() to not fail if trace_debugfs_create() fails, but that's something for a different PR. And anyway, if the system fails such functions, which in this case means it's really low on memory, many other things will fail too...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lyakh as you have spotted, possibility of failure trace_debugfs_filter_create() in fully functional system in quite low, and even if it will be 'silent failure', then following line is going to fail too (dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL)), so system will be without SOF tracing at all. In my opinion it's not an argument to drop return code check and relay on other things fails. I can replace return ret with dev_err, but because of low fail probability, it shouldn't change much for end-user.

if (ret < 0)
dev_err(sdev->dev, "error: fail in %s, %d", __func__, ret);

dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
if (!dfse)
return -ENOMEM;
Expand Down