ASoC: SOF: Provide probe debugfs support
authorCezary Rojewski <cezary.rojewski@intel.com>
Tue, 18 Feb 2020 14:39:23 +0000 (15:39 +0100)
committerMark Brown <broonie@kernel.org>
Tue, 18 Feb 2020 21:52:10 +0000 (21:52 +0000)
Define debugfs subdirectory delegated for IPC communication with DSP.
Input format: uint,uint,(...) which are later translated into DWORDS
sequence and further into instances of struct of interest given the IPC
type.

For Extractor probes, following have been enabled:
- PROBE_POINT_ADD (echo <..> probe_points)
- PROBE_POINT_REMOVE (echo <..> probe_points_remove)
- PROBE_POINT_INFO (cat probe_points)

Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
Acked-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Link: https://lore.kernel.org/r/20200218143924.10565-9-cezary.rojewski@intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/sof/debug.c

index d2b3b99..b5c0d6c 100644 (file)
 #include "sof-priv.h"
 #include "ops.h"
 
+#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
+#include "probe.h"
+
+/**
+ * strsplit_u32 - Split string into sequence of u32 tokens
+ * @buf:       String to split into tokens.
+ * @delim:     String containing delimiter characters.
+ * @tkns:      Returned u32 sequence pointer.
+ * @num_tkns:  Returned number of tokens obtained.
+ */
+static int
+strsplit_u32(char **buf, const char *delim, u32 **tkns, size_t *num_tkns)
+{
+       char *s;
+       u32 *data, *tmp;
+       size_t count = 0;
+       size_t cap = 32;
+       int ret = 0;
+
+       *tkns = NULL;
+       *num_tkns = 0;
+       data = kcalloc(cap, sizeof(*data), GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
+
+       while ((s = strsep(buf, delim)) != NULL) {
+               ret = kstrtouint(s, 0, data + count);
+               if (ret)
+                       goto exit;
+               if (++count >= cap) {
+                       cap *= 2;
+                       tmp = krealloc(data, cap * sizeof(*data), GFP_KERNEL);
+                       if (!tmp) {
+                               ret = -ENOMEM;
+                               goto exit;
+                       }
+                       data = tmp;
+               }
+       }
+
+       if (!count)
+               goto exit;
+       *tkns = kmemdup(data, count * sizeof(*data), GFP_KERNEL);
+       if (*tkns == NULL) {
+               ret = -ENOMEM;
+               goto exit;
+       }
+       *num_tkns = count;
+
+exit:
+       kfree(data);
+       return ret;
+}
+
+static int tokenize_input(const char __user *from, size_t count,
+               loff_t *ppos, u32 **tkns, size_t *num_tkns)
+{
+       char *buf;
+       int ret;
+
+       buf = kmalloc(count + 1, GFP_KERNEL);
+       if (!buf)
+               return -ENOMEM;
+
+       ret = simple_write_to_buffer(buf, count, ppos, from, count);
+       if (ret != count) {
+               ret = ret >= 0 ? -EIO : ret;
+               goto exit;
+       }
+
+       buf[count] = '\0';
+       ret = strsplit_u32((char **)&buf, ",", tkns, num_tkns);
+exit:
+       kfree(buf);
+       return ret;
+}
+
+static ssize_t probe_points_read(struct file *file,
+               char __user *to, size_t count, loff_t *ppos)
+{
+       struct snd_sof_dfsentry *dfse = file->private_data;
+       struct snd_sof_dev *sdev = dfse->sdev;
+       struct sof_probe_point_desc *desc;
+       size_t num_desc, len = 0;
+       char *buf;
+       int i, ret;
+
+       if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
+               dev_warn(sdev->dev, "no extractor stream running\n");
+               return -ENOENT;
+       }
+
+       buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+       if (!buf)
+               return -ENOMEM;
+
+       ret = sof_ipc_probe_points_info(sdev, &desc, &num_desc);
+       if (ret < 0)
+               goto exit;
+
+       for (i = 0; i < num_desc; i++) {
+               ret = snprintf(buf + len, PAGE_SIZE - len,
+                       "Id: %#010x  Purpose: %d  Node id: %#x\n",
+                       desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
+               if (ret < 0)
+                       goto free_desc;
+               len += ret;
+       }
+
+       ret = simple_read_from_buffer(to, count, ppos, buf, len);
+free_desc:
+       kfree(desc);
+exit:
+       kfree(buf);
+       return ret;
+}
+
+static ssize_t probe_points_write(struct file *file,
+               const char __user *from, size_t count, loff_t *ppos)
+{
+       struct snd_sof_dfsentry *dfse = file->private_data;
+       struct snd_sof_dev *sdev = dfse->sdev;
+       struct sof_probe_point_desc *desc;
+       size_t num_tkns, bytes;
+       u32 *tkns;
+       int ret;
+
+       if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
+               dev_warn(sdev->dev, "no extractor stream running\n");
+               return -ENOENT;
+       }
+
+       ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
+       if (ret < 0)
+               return ret;
+       bytes = sizeof(*tkns) * num_tkns;
+       if (!num_tkns || (bytes % sizeof(*desc))) {
+               ret = -EINVAL;
+               goto exit;
+       }
+
+       desc = (struct sof_probe_point_desc *)tkns;
+       ret = sof_ipc_probe_points_add(sdev,
+                       desc, bytes / sizeof(*desc));
+       if (!ret)
+               ret = count;
+exit:
+       kfree(tkns);
+       return ret;
+}
+
+static const struct file_operations probe_points_fops = {
+       .open = simple_open,
+       .read = probe_points_read,
+       .write = probe_points_write,
+       .llseek = default_llseek,
+};
+
+static ssize_t probe_points_remove_write(struct file *file,
+               const char __user *from, size_t count, loff_t *ppos)
+{
+       struct snd_sof_dfsentry *dfse = file->private_data;
+       struct snd_sof_dev *sdev = dfse->sdev;
+       size_t num_tkns;
+       u32 *tkns;
+       int ret;
+
+       if (sdev->extractor_stream_tag == SOF_PROBE_INVALID_NODE_ID) {
+               dev_warn(sdev->dev, "no extractor stream running\n");
+               return -ENOENT;
+       }
+
+       ret = tokenize_input(from, count, ppos, &tkns, &num_tkns);
+       if (ret < 0)
+               return ret;
+       if (!num_tkns) {
+               ret = -EINVAL;
+               goto exit;
+       }
+
+       ret = sof_ipc_probe_points_remove(sdev, tkns, num_tkns);
+       if (!ret)
+               ret = count;
+exit:
+       kfree(tkns);
+       return ret;
+}
+
+static const struct file_operations probe_points_remove_fops = {
+       .open = simple_open,
+       .write = probe_points_remove_write,
+       .llseek = default_llseek,
+};
+
+static int snd_sof_debugfs_probe_item(struct snd_sof_dev *sdev,
+                                const char *name, mode_t mode,
+                                const struct file_operations *fops)
+{
+       struct snd_sof_dfsentry *dfse;
+
+       dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
+       if (!dfse)
+               return -ENOMEM;
+
+       dfse->type = SOF_DFSENTRY_TYPE_BUF;
+       dfse->sdev = sdev;
+
+       debugfs_create_file(name, mode, sdev->debugfs_root, dfse, fops);
+       /* add to dfsentry list */
+       list_add(&dfse->list, &sdev->dfsentry_list);
+
+       return 0;
+}
+#endif
+
 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
 #define MAX_IPC_FLOOD_DURATION_MS 1000
 #define MAX_IPC_FLOOD_COUNT 10000
@@ -436,6 +651,17 @@ int snd_sof_dbg_init(struct snd_sof_dev *sdev)
                        return err;
        }
 
+#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
+       err = snd_sof_debugfs_probe_item(sdev, "probe_points",
+                       0644, &probe_points_fops);
+       if (err < 0)
+               return err;
+       err = snd_sof_debugfs_probe_item(sdev, "probe_points_remove",
+                       0200, &probe_points_remove_fops);
+       if (err < 0)
+               return err;
+#endif
+
 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST)
        /* create read-write ipc_flood_count debugfs entry */
        err = snd_sof_debugfs_buf_item(sdev, NULL, 0,