// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license. When using or
// redistributing this file, you may do so under either license.
//
// Copyright(c) 2018 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
//
// Generic debug routines used to export DSP MMIO and memories to userspace
// for firmware debugging.
//
#include <linux/debugfs.h>
#include <linux/io.h>
#include <linux/pm_runtime.h>
#include <sound/sof/ext_manifest.h>
#include <sound/sof/debug.h>
#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