18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  skl-debug.c - Debugfs for skl driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2016-17 Intel Corp
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/pci.h>
98c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
108c2ecf20Sopenharmony_ci#include <uapi/sound/skl-tplg-interface.h>
118c2ecf20Sopenharmony_ci#include "skl.h"
128c2ecf20Sopenharmony_ci#include "skl-sst-dsp.h"
138c2ecf20Sopenharmony_ci#include "skl-sst-ipc.h"
148c2ecf20Sopenharmony_ci#include "skl-topology.h"
158c2ecf20Sopenharmony_ci#include "../common/sst-dsp.h"
168c2ecf20Sopenharmony_ci#include "../common/sst-dsp-priv.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define MOD_BUF		PAGE_SIZE
198c2ecf20Sopenharmony_ci#define FW_REG_BUF	PAGE_SIZE
208c2ecf20Sopenharmony_ci#define FW_REG_SIZE	0x60
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct skl_debug {
238c2ecf20Sopenharmony_ci	struct skl_dev *skl;
248c2ecf20Sopenharmony_ci	struct device *dev;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	struct dentry *fs;
278c2ecf20Sopenharmony_ci	struct dentry *modules;
288c2ecf20Sopenharmony_ci	u8 fw_read_buff[FW_REG_BUF];
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf,
328c2ecf20Sopenharmony_ci				int max_pin, ssize_t size, bool direction)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	int i;
358c2ecf20Sopenharmony_ci	ssize_t ret = 0;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	for (i = 0; i < max_pin; i++) {
388c2ecf20Sopenharmony_ci		ret += scnprintf(buf + size, MOD_BUF - size,
398c2ecf20Sopenharmony_ci				"%s %d\n\tModule %d\n\tInstance %d\n\t"
408c2ecf20Sopenharmony_ci				"In-used %s\n\tType %s\n"
418c2ecf20Sopenharmony_ci				"\tState %d\n\tIndex %d\n",
428c2ecf20Sopenharmony_ci				direction ? "Input Pin:" : "Output Pin:",
438c2ecf20Sopenharmony_ci				i, m_pin[i].id.module_id,
448c2ecf20Sopenharmony_ci				m_pin[i].id.instance_id,
458c2ecf20Sopenharmony_ci				m_pin[i].in_use ? "Used" : "Unused",
468c2ecf20Sopenharmony_ci				m_pin[i].is_dynamic ? "Dynamic" : "Static",
478c2ecf20Sopenharmony_ci				m_pin[i].pin_state, i);
488c2ecf20Sopenharmony_ci		size += ret;
498c2ecf20Sopenharmony_ci	}
508c2ecf20Sopenharmony_ci	return ret;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic ssize_t skl_print_fmt(struct skl_module_fmt *fmt, char *buf,
548c2ecf20Sopenharmony_ci					ssize_t size, bool direction)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	return scnprintf(buf + size, MOD_BUF - size,
578c2ecf20Sopenharmony_ci			"%s\n\tCh %d\n\tFreq %d\n\tBit depth %d\n\t"
588c2ecf20Sopenharmony_ci			"Valid bit depth %d\n\tCh config %#x\n\tInterleaving %d\n\t"
598c2ecf20Sopenharmony_ci			"Sample Type %d\n\tCh Map %#x\n",
608c2ecf20Sopenharmony_ci			direction ? "Input Format:" : "Output Format:",
618c2ecf20Sopenharmony_ci			fmt->channels, fmt->s_freq, fmt->bit_depth,
628c2ecf20Sopenharmony_ci			fmt->valid_bit_depth, fmt->ch_cfg,
638c2ecf20Sopenharmony_ci			fmt->interleaving_style, fmt->sample_type,
648c2ecf20Sopenharmony_ci			fmt->ch_map);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic ssize_t module_read(struct file *file, char __user *user_buf,
688c2ecf20Sopenharmony_ci			   size_t count, loff_t *ppos)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct skl_module_cfg *mconfig = file->private_data;
718c2ecf20Sopenharmony_ci	struct skl_module *module = mconfig->module;
728c2ecf20Sopenharmony_ci	struct skl_module_res *res = &module->resources[mconfig->res_idx];
738c2ecf20Sopenharmony_ci	char *buf;
748c2ecf20Sopenharmony_ci	ssize_t ret;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	buf = kzalloc(MOD_BUF, GFP_KERNEL);
778c2ecf20Sopenharmony_ci	if (!buf)
788c2ecf20Sopenharmony_ci		return -ENOMEM;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	ret = scnprintf(buf, MOD_BUF, "Module:\n\tUUID %pUL\n\tModule id %d\n"
818c2ecf20Sopenharmony_ci			"\tInstance id %d\n\tPvt_id %d\n", mconfig->guid,
828c2ecf20Sopenharmony_ci			mconfig->id.module_id, mconfig->id.instance_id,
838c2ecf20Sopenharmony_ci			mconfig->id.pvt_id);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
868c2ecf20Sopenharmony_ci			"Resources:\n\tCPC %#x\n\tIBS %#x\n\tOBS %#x\t\n",
878c2ecf20Sopenharmony_ci			res->cpc, res->ibs, res->obs);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
908c2ecf20Sopenharmony_ci			"Module data:\n\tCore %d\n\tIn queue %d\n\t"
918c2ecf20Sopenharmony_ci			"Out queue %d\n\tType %s\n",
928c2ecf20Sopenharmony_ci			mconfig->core_id, mconfig->max_in_queue,
938c2ecf20Sopenharmony_ci			mconfig->max_out_queue,
948c2ecf20Sopenharmony_ci			mconfig->is_loadable ? "loadable" : "inbuilt");
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	ret += skl_print_fmt(mconfig->in_fmt, buf, ret, true);
978c2ecf20Sopenharmony_ci	ret += skl_print_fmt(mconfig->out_fmt, buf, ret, false);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1008c2ecf20Sopenharmony_ci			"Fixup:\n\tParams %#x\n\tConverter %#x\n",
1018c2ecf20Sopenharmony_ci			mconfig->params_fixup, mconfig->converter);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1048c2ecf20Sopenharmony_ci			"Module Gateway:\n\tType %#x\n\tVbus %#x\n\tHW conn %#x\n\tSlot %#x\n",
1058c2ecf20Sopenharmony_ci			mconfig->dev_type, mconfig->vbus_id,
1068c2ecf20Sopenharmony_ci			mconfig->hw_conn_type, mconfig->time_slot);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1098c2ecf20Sopenharmony_ci			"Pipeline:\n\tID %d\n\tPriority %d\n\tConn Type %d\n\t"
1108c2ecf20Sopenharmony_ci			"Pages %#x\n", mconfig->pipe->ppl_id,
1118c2ecf20Sopenharmony_ci			mconfig->pipe->pipe_priority, mconfig->pipe->conn_type,
1128c2ecf20Sopenharmony_ci			mconfig->pipe->memory_pages);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1158c2ecf20Sopenharmony_ci			"\tParams:\n\t\tHost DMA %d\n\t\tLink DMA %d\n",
1168c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->host_dma_id,
1178c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->link_dma_id);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1208c2ecf20Sopenharmony_ci			"\tPCM params:\n\t\tCh %d\n\t\tFreq %d\n\t\tFormat %d\n",
1218c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->ch,
1228c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->s_freq,
1238c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->s_fmt);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1268c2ecf20Sopenharmony_ci			"\tLink %#x\n\tStream %#x\n",
1278c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->linktype,
1288c2ecf20Sopenharmony_ci			mconfig->pipe->p_params->stream);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1318c2ecf20Sopenharmony_ci			"\tState %d\n\tPassthru %s\n",
1328c2ecf20Sopenharmony_ci			mconfig->pipe->state,
1338c2ecf20Sopenharmony_ci			mconfig->pipe->passthru ? "true" : "false");
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	ret += skl_print_pins(mconfig->m_in_pin, buf,
1368c2ecf20Sopenharmony_ci			mconfig->max_in_queue, ret, true);
1378c2ecf20Sopenharmony_ci	ret += skl_print_pins(mconfig->m_out_pin, buf,
1388c2ecf20Sopenharmony_ci			mconfig->max_out_queue, ret, false);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	ret += scnprintf(buf + ret, MOD_BUF - ret,
1418c2ecf20Sopenharmony_ci			"Other:\n\tDomain %d\n\tHomogeneous Input %s\n\t"
1428c2ecf20Sopenharmony_ci			"Homogeneous Output %s\n\tIn Queue Mask %d\n\t"
1438c2ecf20Sopenharmony_ci			"Out Queue Mask %d\n\tDMA ID %d\n\tMem Pages %d\n\t"
1448c2ecf20Sopenharmony_ci			"Module Type %d\n\tModule State %d\n",
1458c2ecf20Sopenharmony_ci			mconfig->domain,
1468c2ecf20Sopenharmony_ci			mconfig->homogenous_inputs ? "true" : "false",
1478c2ecf20Sopenharmony_ci			mconfig->homogenous_outputs ? "true" : "false",
1488c2ecf20Sopenharmony_ci			mconfig->in_queue_mask, mconfig->out_queue_mask,
1498c2ecf20Sopenharmony_ci			mconfig->dma_id, mconfig->mem_pages, mconfig->m_state,
1508c2ecf20Sopenharmony_ci			mconfig->m_type);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	kfree(buf);
1558c2ecf20Sopenharmony_ci	return ret;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic const struct file_operations mcfg_fops = {
1598c2ecf20Sopenharmony_ci	.open = simple_open,
1608c2ecf20Sopenharmony_ci	.read = module_read,
1618c2ecf20Sopenharmony_ci	.llseek = default_llseek,
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_civoid skl_debug_init_module(struct skl_debug *d,
1668c2ecf20Sopenharmony_ci			struct snd_soc_dapm_widget *w,
1678c2ecf20Sopenharmony_ci			struct skl_module_cfg *mconfig)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	debugfs_create_file(w->name, 0444, d->modules, mconfig,
1708c2ecf20Sopenharmony_ci			    &mcfg_fops);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic ssize_t fw_softreg_read(struct file *file, char __user *user_buf,
1748c2ecf20Sopenharmony_ci			       size_t count, loff_t *ppos)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct skl_debug *d = file->private_data;
1778c2ecf20Sopenharmony_ci	struct sst_dsp *sst = d->skl->dsp;
1788c2ecf20Sopenharmony_ci	size_t w0_stat_sz = sst->addr.w0_stat_sz;
1798c2ecf20Sopenharmony_ci	void __iomem *in_base = sst->mailbox.in_base;
1808c2ecf20Sopenharmony_ci	void __iomem *fw_reg_addr;
1818c2ecf20Sopenharmony_ci	unsigned int offset;
1828c2ecf20Sopenharmony_ci	char *tmp;
1838c2ecf20Sopenharmony_ci	ssize_t ret = 0;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	tmp = kzalloc(FW_REG_BUF, GFP_KERNEL);
1868c2ecf20Sopenharmony_ci	if (!tmp)
1878c2ecf20Sopenharmony_ci		return -ENOMEM;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	fw_reg_addr = in_base - w0_stat_sz;
1908c2ecf20Sopenharmony_ci	memset(d->fw_read_buff, 0, FW_REG_BUF);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (w0_stat_sz > 0)
1938c2ecf20Sopenharmony_ci		__ioread32_copy(d->fw_read_buff, fw_reg_addr, w0_stat_sz >> 2);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	for (offset = 0; offset < FW_REG_SIZE; offset += 16) {
1968c2ecf20Sopenharmony_ci		ret += scnprintf(tmp + ret, FW_REG_BUF - ret, "%#.4x: ", offset);
1978c2ecf20Sopenharmony_ci		hex_dump_to_buffer(d->fw_read_buff + offset, 16, 16, 4,
1988c2ecf20Sopenharmony_ci				   tmp + ret, FW_REG_BUF - ret, 0);
1998c2ecf20Sopenharmony_ci		ret += strlen(tmp + ret);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		/* print newline for each offset */
2028c2ecf20Sopenharmony_ci		if (FW_REG_BUF - ret > 0)
2038c2ecf20Sopenharmony_ci			tmp[ret++] = '\n';
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(user_buf, count, ppos, tmp, ret);
2078c2ecf20Sopenharmony_ci	kfree(tmp);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return ret;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic const struct file_operations soft_regs_ctrl_fops = {
2138c2ecf20Sopenharmony_ci	.open = simple_open,
2148c2ecf20Sopenharmony_ci	.read = fw_softreg_read,
2158c2ecf20Sopenharmony_ci	.llseek = default_llseek,
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistruct skl_debug *skl_debugfs_init(struct skl_dev *skl)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct skl_debug *d;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	d = devm_kzalloc(&skl->pci->dev, sizeof(*d), GFP_KERNEL);
2238c2ecf20Sopenharmony_ci	if (!d)
2248c2ecf20Sopenharmony_ci		return NULL;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* create the debugfs dir with platform component's debugfs as parent */
2278c2ecf20Sopenharmony_ci	d->fs = debugfs_create_dir("dsp", skl->component->debugfs_root);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	d->skl = skl;
2308c2ecf20Sopenharmony_ci	d->dev = &skl->pci->dev;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* now create the module dir */
2338c2ecf20Sopenharmony_ci	d->modules = debugfs_create_dir("modules", d->fs);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	debugfs_create_file("fw_soft_regs_rd", 0444, d->fs, d,
2368c2ecf20Sopenharmony_ci			    &soft_regs_ctrl_fops);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return d;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_civoid skl_debugfs_exit(struct skl_dev *skl)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct skl_debug *d = skl->debugfs;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	debugfs_remove_recursive(d->fs);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	d = NULL;
2488c2ecf20Sopenharmony_ci}
249