18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25cdf6c09SVinod Koul /*
35cdf6c09SVinod Koul  *  skl-debug.c - Debugfs for skl driver
45cdf6c09SVinod Koul  *
55cdf6c09SVinod Koul  *  Copyright (C) 2016-17 Intel Corp
65cdf6c09SVinod Koul  */
75cdf6c09SVinod Koul 
85cdf6c09SVinod Koul #include <linux/pci.h>
95cdf6c09SVinod Koul #include <linux/debugfs.h>
100c24fdc0SGuenter Roeck #include <uapi/sound/skl-tplg-interface.h>
115cdf6c09SVinod Koul #include "skl.h"
12bdd0384aSVunny Sodhi #include "skl-sst-dsp.h"
13bdd0384aSVunny Sodhi #include "skl-sst-ipc.h"
14d14700a0SVinod Koul #include "skl-topology.h"
1558be7753SVinod Koul #include "../common/sst-dsp.h"
16bdd0384aSVunny Sodhi #include "../common/sst-dsp-priv.h"
17d14700a0SVinod Koul 
18d14700a0SVinod Koul #define MOD_BUF		PAGE_SIZE
19bdd0384aSVunny Sodhi #define FW_REG_BUF	PAGE_SIZE
20bdd0384aSVunny Sodhi #define FW_REG_SIZE	0x60
215cdf6c09SVinod Koul 
225cdf6c09SVinod Koul struct skl_debug {
23bcc2a2dcSCezary Rojewski 	struct skl_dev *skl;
245cdf6c09SVinod Koul 	struct device *dev;
255cdf6c09SVinod Koul 
265cdf6c09SVinod Koul 	struct dentry *fs;
27d14700a0SVinod Koul 	struct dentry *modules;
28bdd0384aSVunny Sodhi 	u8 fw_read_buff[FW_REG_BUF];
295cdf6c09SVinod Koul };
305cdf6c09SVinod Koul 
skl_print_pins(struct skl_module_pin * m_pin,char * buf,int max_pin,ssize_t size,bool direction)31d14700a0SVinod Koul static ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf,
32d14700a0SVinod Koul 				int max_pin, ssize_t size, bool direction)
33d14700a0SVinod Koul {
34d14700a0SVinod Koul 	int i;
35d14700a0SVinod Koul 	ssize_t ret = 0;
36d14700a0SVinod Koul 
3764bbacc5STakashi Iwai 	for (i = 0; i < max_pin; i++) {
38549cd0baSTakashi Iwai 		ret += scnprintf(buf + size, MOD_BUF - size,
39d14700a0SVinod Koul 				"%s %d\n\tModule %d\n\tInstance %d\n\t"
40d14700a0SVinod Koul 				"In-used %s\n\tType %s\n"
41d14700a0SVinod Koul 				"\tState %d\n\tIndex %d\n",
42d14700a0SVinod Koul 				direction ? "Input Pin:" : "Output Pin:",
43d14700a0SVinod Koul 				i, m_pin[i].id.module_id,
44d14700a0SVinod Koul 				m_pin[i].id.instance_id,
45d14700a0SVinod Koul 				m_pin[i].in_use ? "Used" : "Unused",
46d14700a0SVinod Koul 				m_pin[i].is_dynamic ? "Dynamic" : "Static",
47d14700a0SVinod Koul 				m_pin[i].pin_state, i);
4864bbacc5STakashi Iwai 		size += ret;
4964bbacc5STakashi Iwai 	}
50d14700a0SVinod Koul 	return ret;
51d14700a0SVinod Koul }
52d14700a0SVinod Koul 
skl_print_fmt(struct skl_module_fmt * fmt,char * buf,ssize_t size,bool direction)53d14700a0SVinod Koul static ssize_t skl_print_fmt(struct skl_module_fmt *fmt, char *buf,
54d14700a0SVinod Koul 					ssize_t size, bool direction)
55d14700a0SVinod Koul {
56549cd0baSTakashi Iwai 	return scnprintf(buf + size, MOD_BUF - size,
57d14700a0SVinod Koul 			"%s\n\tCh %d\n\tFreq %d\n\tBit depth %d\n\t"
58d14700a0SVinod Koul 			"Valid bit depth %d\n\tCh config %#x\n\tInterleaving %d\n\t"
59d14700a0SVinod Koul 			"Sample Type %d\n\tCh Map %#x\n",
60d14700a0SVinod Koul 			direction ? "Input Format:" : "Output Format:",
61d14700a0SVinod Koul 			fmt->channels, fmt->s_freq, fmt->bit_depth,
62d14700a0SVinod Koul 			fmt->valid_bit_depth, fmt->ch_cfg,
63d14700a0SVinod Koul 			fmt->interleaving_style, fmt->sample_type,
64d14700a0SVinod Koul 			fmt->ch_map);
65d14700a0SVinod Koul }
66d14700a0SVinod Koul 
module_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)67d14700a0SVinod Koul static ssize_t module_read(struct file *file, char __user *user_buf,
68d14700a0SVinod Koul 			   size_t count, loff_t *ppos)
69d14700a0SVinod Koul {
70d14700a0SVinod Koul 	struct skl_module_cfg *mconfig = file->private_data;
7184b71067SCezary Rojewski 	struct skl_module *module = mconfig->module;
7284b71067SCezary Rojewski 	struct skl_module_res *res = &module->resources[mconfig->res_idx];
73d14700a0SVinod Koul 	char *buf;
74d14700a0SVinod Koul 	ssize_t ret;
75d14700a0SVinod Koul 
76d14700a0SVinod Koul 	buf = kzalloc(MOD_BUF, GFP_KERNEL);
77d14700a0SVinod Koul 	if (!buf)
78d14700a0SVinod Koul 		return -ENOMEM;
79d14700a0SVinod Koul 
80549cd0baSTakashi Iwai 	ret = scnprintf(buf, MOD_BUF, "Module:\n\tUUID %pUL\n\tModule id %d\n"
81d14700a0SVinod Koul 			"\tInstance id %d\n\tPvt_id %d\n", mconfig->guid,
82d14700a0SVinod Koul 			mconfig->id.module_id, mconfig->id.instance_id,
83d14700a0SVinod Koul 			mconfig->id.pvt_id);
84d14700a0SVinod Koul 
85549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
8684b71067SCezary Rojewski 			"Resources:\n\tCPC %#x\n\tIBS %#x\n\tOBS %#x\t\n",
8784b71067SCezary Rojewski 			res->cpc, res->ibs, res->obs);
88d14700a0SVinod Koul 
89549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
90d14700a0SVinod Koul 			"Module data:\n\tCore %d\n\tIn queue %d\n\t"
91d14700a0SVinod Koul 			"Out queue %d\n\tType %s\n",
92d14700a0SVinod Koul 			mconfig->core_id, mconfig->max_in_queue,
93d14700a0SVinod Koul 			mconfig->max_out_queue,
94d14700a0SVinod Koul 			mconfig->is_loadable ? "loadable" : "inbuilt");
95d14700a0SVinod Koul 
96d14700a0SVinod Koul 	ret += skl_print_fmt(mconfig->in_fmt, buf, ret, true);
97d14700a0SVinod Koul 	ret += skl_print_fmt(mconfig->out_fmt, buf, ret, false);
98d14700a0SVinod Koul 
99549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
100d14700a0SVinod Koul 			"Fixup:\n\tParams %#x\n\tConverter %#x\n",
101d14700a0SVinod Koul 			mconfig->params_fixup, mconfig->converter);
102d14700a0SVinod Koul 
103549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
104d14700a0SVinod Koul 			"Module Gateway:\n\tType %#x\n\tVbus %#x\n\tHW conn %#x\n\tSlot %#x\n",
105d14700a0SVinod Koul 			mconfig->dev_type, mconfig->vbus_id,
106d14700a0SVinod Koul 			mconfig->hw_conn_type, mconfig->time_slot);
107d14700a0SVinod Koul 
108549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
109d14700a0SVinod Koul 			"Pipeline:\n\tID %d\n\tPriority %d\n\tConn Type %d\n\t"
110d14700a0SVinod Koul 			"Pages %#x\n", mconfig->pipe->ppl_id,
111d14700a0SVinod Koul 			mconfig->pipe->pipe_priority, mconfig->pipe->conn_type,
112d14700a0SVinod Koul 			mconfig->pipe->memory_pages);
113d14700a0SVinod Koul 
114549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
115d14700a0SVinod Koul 			"\tParams:\n\t\tHost DMA %d\n\t\tLink DMA %d\n",
116d14700a0SVinod Koul 			mconfig->pipe->p_params->host_dma_id,
117d14700a0SVinod Koul 			mconfig->pipe->p_params->link_dma_id);
118d14700a0SVinod Koul 
119549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
120d14700a0SVinod Koul 			"\tPCM params:\n\t\tCh %d\n\t\tFreq %d\n\t\tFormat %d\n",
121d14700a0SVinod Koul 			mconfig->pipe->p_params->ch,
122d14700a0SVinod Koul 			mconfig->pipe->p_params->s_freq,
123d14700a0SVinod Koul 			mconfig->pipe->p_params->s_fmt);
124d14700a0SVinod Koul 
125549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
126d14700a0SVinod Koul 			"\tLink %#x\n\tStream %#x\n",
127d14700a0SVinod Koul 			mconfig->pipe->p_params->linktype,
128d14700a0SVinod Koul 			mconfig->pipe->p_params->stream);
129d14700a0SVinod Koul 
130549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
131d14700a0SVinod Koul 			"\tState %d\n\tPassthru %s\n",
132d14700a0SVinod Koul 			mconfig->pipe->state,
133d14700a0SVinod Koul 			mconfig->pipe->passthru ? "true" : "false");
134d14700a0SVinod Koul 
135d14700a0SVinod Koul 	ret += skl_print_pins(mconfig->m_in_pin, buf,
136d14700a0SVinod Koul 			mconfig->max_in_queue, ret, true);
137d14700a0SVinod Koul 	ret += skl_print_pins(mconfig->m_out_pin, buf,
138d14700a0SVinod Koul 			mconfig->max_out_queue, ret, false);
139d14700a0SVinod Koul 
140549cd0baSTakashi Iwai 	ret += scnprintf(buf + ret, MOD_BUF - ret,
1417608102eSColin Ian King 			"Other:\n\tDomain %d\n\tHomogeneous Input %s\n\t"
1427608102eSColin Ian King 			"Homogeneous Output %s\n\tIn Queue Mask %d\n\t"
143d14700a0SVinod Koul 			"Out Queue Mask %d\n\tDMA ID %d\n\tMem Pages %d\n\t"
144d14700a0SVinod Koul 			"Module Type %d\n\tModule State %d\n",
145d14700a0SVinod Koul 			mconfig->domain,
146d14700a0SVinod Koul 			mconfig->homogenous_inputs ? "true" : "false",
147d14700a0SVinod Koul 			mconfig->homogenous_outputs ? "true" : "false",
148d14700a0SVinod Koul 			mconfig->in_queue_mask, mconfig->out_queue_mask,
149d14700a0SVinod Koul 			mconfig->dma_id, mconfig->mem_pages, mconfig->m_state,
150d14700a0SVinod Koul 			mconfig->m_type);
151d14700a0SVinod Koul 
152d14700a0SVinod Koul 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
153d14700a0SVinod Koul 
154d14700a0SVinod Koul 	kfree(buf);
155d14700a0SVinod Koul 	return ret;
156d14700a0SVinod Koul }
157d14700a0SVinod Koul 
158d14700a0SVinod Koul static const struct file_operations mcfg_fops = {
159d14700a0SVinod Koul 	.open = simple_open,
160d14700a0SVinod Koul 	.read = module_read,
161d14700a0SVinod Koul 	.llseek = default_llseek,
162d14700a0SVinod Koul };
163d14700a0SVinod Koul 
164d14700a0SVinod Koul 
skl_debug_init_module(struct skl_debug * d,struct snd_soc_dapm_widget * w,struct skl_module_cfg * mconfig)165d14700a0SVinod Koul void skl_debug_init_module(struct skl_debug *d,
166d14700a0SVinod Koul 			struct snd_soc_dapm_widget *w,
167d14700a0SVinod Koul 			struct skl_module_cfg *mconfig)
168d14700a0SVinod Koul {
169d8701faeSGreg Kroah-Hartman 	debugfs_create_file(w->name, 0444, d->modules, mconfig,
170d8701faeSGreg Kroah-Hartman 			    &mcfg_fops);
171d14700a0SVinod Koul }
172d14700a0SVinod Koul 
fw_softreg_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)173bdd0384aSVunny Sodhi static ssize_t fw_softreg_read(struct file *file, char __user *user_buf,
174bdd0384aSVunny Sodhi 			       size_t count, loff_t *ppos)
175bdd0384aSVunny Sodhi {
176bdd0384aSVunny Sodhi 	struct skl_debug *d = file->private_data;
177bcc2a2dcSCezary Rojewski 	struct sst_dsp *sst = d->skl->dsp;
178bdd0384aSVunny Sodhi 	size_t w0_stat_sz = sst->addr.w0_stat_sz;
179bdd0384aSVunny Sodhi 	void __iomem *in_base = sst->mailbox.in_base;
180bdd0384aSVunny Sodhi 	void __iomem *fw_reg_addr;
181bdd0384aSVunny Sodhi 	unsigned int offset;
182bdd0384aSVunny Sodhi 	char *tmp;
183bdd0384aSVunny Sodhi 	ssize_t ret = 0;
184bdd0384aSVunny Sodhi 
185bdd0384aSVunny Sodhi 	tmp = kzalloc(FW_REG_BUF, GFP_KERNEL);
186bdd0384aSVunny Sodhi 	if (!tmp)
187bdd0384aSVunny Sodhi 		return -ENOMEM;
188bdd0384aSVunny Sodhi 
189bdd0384aSVunny Sodhi 	fw_reg_addr = in_base - w0_stat_sz;
190bdd0384aSVunny Sodhi 	memset(d->fw_read_buff, 0, FW_REG_BUF);
191bdd0384aSVunny Sodhi 
192bdd0384aSVunny Sodhi 	if (w0_stat_sz > 0)
19317d29ff9SAmadeusz Sławiński 		__ioread32_copy(d->fw_read_buff, fw_reg_addr, w0_stat_sz >> 2);
194bdd0384aSVunny Sodhi 
195bdd0384aSVunny Sodhi 	for (offset = 0; offset < FW_REG_SIZE; offset += 16) {
196549cd0baSTakashi Iwai 		ret += scnprintf(tmp + ret, FW_REG_BUF - ret, "%#.4x: ", offset);
197bdd0384aSVunny Sodhi 		hex_dump_to_buffer(d->fw_read_buff + offset, 16, 16, 4,
198bdd0384aSVunny Sodhi 				   tmp + ret, FW_REG_BUF - ret, 0);
199bdd0384aSVunny Sodhi 		ret += strlen(tmp + ret);
200bdd0384aSVunny Sodhi 
201bdd0384aSVunny Sodhi 		/* print newline for each offset */
202bdd0384aSVunny Sodhi 		if (FW_REG_BUF - ret > 0)
203bdd0384aSVunny Sodhi 			tmp[ret++] = '\n';
204bdd0384aSVunny Sodhi 	}
205bdd0384aSVunny Sodhi 
206bdd0384aSVunny Sodhi 	ret = simple_read_from_buffer(user_buf, count, ppos, tmp, ret);
207bdd0384aSVunny Sodhi 	kfree(tmp);
208bdd0384aSVunny Sodhi 
209bdd0384aSVunny Sodhi 	return ret;
210bdd0384aSVunny Sodhi }
211bdd0384aSVunny Sodhi 
212bdd0384aSVunny Sodhi static const struct file_operations soft_regs_ctrl_fops = {
213bdd0384aSVunny Sodhi 	.open = simple_open,
214bdd0384aSVunny Sodhi 	.read = fw_softreg_read,
215bdd0384aSVunny Sodhi 	.llseek = default_llseek,
216bdd0384aSVunny Sodhi };
217bdd0384aSVunny Sodhi 
skl_debugfs_init(struct skl_dev * skl)218bcc2a2dcSCezary Rojewski struct skl_debug *skl_debugfs_init(struct skl_dev *skl)
2195cdf6c09SVinod Koul {
2205cdf6c09SVinod Koul 	struct skl_debug *d;
2215cdf6c09SVinod Koul 
2225cdf6c09SVinod Koul 	d = devm_kzalloc(&skl->pci->dev, sizeof(*d), GFP_KERNEL);
2235cdf6c09SVinod Koul 	if (!d)
2245cdf6c09SVinod Koul 		return NULL;
2255cdf6c09SVinod Koul 
2265cdf6c09SVinod Koul 	/* create the debugfs dir with platform component's debugfs as parent */
227d8701faeSGreg Kroah-Hartman 	d->fs = debugfs_create_dir("dsp", skl->component->debugfs_root);
2285cdf6c09SVinod Koul 
2295cdf6c09SVinod Koul 	d->skl = skl;
2305cdf6c09SVinod Koul 	d->dev = &skl->pci->dev;
2315cdf6c09SVinod Koul 
232d14700a0SVinod Koul 	/* now create the module dir */
233d14700a0SVinod Koul 	d->modules = debugfs_create_dir("modules", d->fs);
234d14700a0SVinod Koul 
235d8701faeSGreg Kroah-Hartman 	debugfs_create_file("fw_soft_regs_rd", 0444, d->fs, d,
236d8701faeSGreg Kroah-Hartman 			    &soft_regs_ctrl_fops);
237bdd0384aSVunny Sodhi 
2385cdf6c09SVinod Koul 	return d;
2395cdf6c09SVinod Koul }
2405b8e4c1cSAmadeusz Sławiński 
skl_debugfs_exit(struct skl_dev * skl)241bcc2a2dcSCezary Rojewski void skl_debugfs_exit(struct skl_dev *skl)
2425b8e4c1cSAmadeusz Sławiński {
2435b8e4c1cSAmadeusz Sławiński 	struct skl_debug *d = skl->debugfs;
2445b8e4c1cSAmadeusz Sławiński 
2455b8e4c1cSAmadeusz Sławiński 	debugfs_remove_recursive(d->fs);
2465b8e4c1cSAmadeusz Sławiński 
2475b8e4c1cSAmadeusz Sławiński 	d = NULL;
2485b8e4c1cSAmadeusz Sławiński }
249