1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  skl-debug.c - Debugfs for skl driver
4  *
5  *  Copyright (C) 2016-17 Intel Corp
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/debugfs.h>
10 #include <uapi/sound/skl-tplg-interface.h>
11 #include "skl.h"
12 #include "skl-sst-dsp.h"
13 #include "skl-sst-ipc.h"
14 #include "skl-topology.h"
15 #include "../common/sst-dsp.h"
16 #include "../common/sst-dsp-priv.h"
17 
18 #define MOD_BUF		PAGE_SIZE
19 #define FW_REG_BUF	PAGE_SIZE
20 #define FW_REG_SIZE	0x60
21 
22 struct skl_debug {
23 	struct skl *skl;
24 	struct device *dev;
25 
26 	struct dentry *fs;
27 	struct dentry *modules;
28 	u8 fw_read_buff[FW_REG_BUF];
29 };
30 
31 static ssize_t skl_print_pins(struct skl_module_pin *m_pin, char *buf,
32 				int max_pin, ssize_t size, bool direction)
33 {
34 	int i;
35 	ssize_t ret = 0;
36 
37 	for (i = 0; i < max_pin; i++)
38 		ret += snprintf(buf + size, MOD_BUF - size,
39 				"%s %d\n\tModule %d\n\tInstance %d\n\t"
40 				"In-used %s\n\tType %s\n"
41 				"\tState %d\n\tIndex %d\n",
42 				direction ? "Input Pin:" : "Output Pin:",
43 				i, m_pin[i].id.module_id,
44 				m_pin[i].id.instance_id,
45 				m_pin[i].in_use ? "Used" : "Unused",
46 				m_pin[i].is_dynamic ? "Dynamic" : "Static",
47 				m_pin[i].pin_state, i);
48 	return ret;
49 }
50 
51 static ssize_t skl_print_fmt(struct skl_module_fmt *fmt, char *buf,
52 					ssize_t size, bool direction)
53 {
54 	return snprintf(buf + size, MOD_BUF - size,
55 			"%s\n\tCh %d\n\tFreq %d\n\tBit depth %d\n\t"
56 			"Valid bit depth %d\n\tCh config %#x\n\tInterleaving %d\n\t"
57 			"Sample Type %d\n\tCh Map %#x\n",
58 			direction ? "Input Format:" : "Output Format:",
59 			fmt->channels, fmt->s_freq, fmt->bit_depth,
60 			fmt->valid_bit_depth, fmt->ch_cfg,
61 			fmt->interleaving_style, fmt->sample_type,
62 			fmt->ch_map);
63 }
64 
65 static ssize_t module_read(struct file *file, char __user *user_buf,
66 			   size_t count, loff_t *ppos)
67 {
68 	struct skl_module_cfg *mconfig = file->private_data;
69 	char *buf;
70 	ssize_t ret;
71 
72 	buf = kzalloc(MOD_BUF, GFP_KERNEL);
73 	if (!buf)
74 		return -ENOMEM;
75 
76 	ret = snprintf(buf, MOD_BUF, "Module:\n\tUUID %pUL\n\tModule id %d\n"
77 			"\tInstance id %d\n\tPvt_id %d\n", mconfig->guid,
78 			mconfig->id.module_id, mconfig->id.instance_id,
79 			mconfig->id.pvt_id);
80 
81 	ret += snprintf(buf + ret, MOD_BUF - ret,
82 			"Resources:\n\tMCPS %#x\n\tIBS %#x\n\tOBS %#x\t\n",
83 			mconfig->mcps, mconfig->ibs, mconfig->obs);
84 
85 	ret += snprintf(buf + ret, MOD_BUF - ret,
86 			"Module data:\n\tCore %d\n\tIn queue %d\n\t"
87 			"Out queue %d\n\tType %s\n",
88 			mconfig->core_id, mconfig->max_in_queue,
89 			mconfig->max_out_queue,
90 			mconfig->is_loadable ? "loadable" : "inbuilt");
91 
92 	ret += skl_print_fmt(mconfig->in_fmt, buf, ret, true);
93 	ret += skl_print_fmt(mconfig->out_fmt, buf, ret, false);
94 
95 	ret += snprintf(buf + ret, MOD_BUF - ret,
96 			"Fixup:\n\tParams %#x\n\tConverter %#x\n",
97 			mconfig->params_fixup, mconfig->converter);
98 
99 	ret += snprintf(buf + ret, MOD_BUF - ret,
100 			"Module Gateway:\n\tType %#x\n\tVbus %#x\n\tHW conn %#x\n\tSlot %#x\n",
101 			mconfig->dev_type, mconfig->vbus_id,
102 			mconfig->hw_conn_type, mconfig->time_slot);
103 
104 	ret += snprintf(buf + ret, MOD_BUF - ret,
105 			"Pipeline:\n\tID %d\n\tPriority %d\n\tConn Type %d\n\t"
106 			"Pages %#x\n", mconfig->pipe->ppl_id,
107 			mconfig->pipe->pipe_priority, mconfig->pipe->conn_type,
108 			mconfig->pipe->memory_pages);
109 
110 	ret += snprintf(buf + ret, MOD_BUF - ret,
111 			"\tParams:\n\t\tHost DMA %d\n\t\tLink DMA %d\n",
112 			mconfig->pipe->p_params->host_dma_id,
113 			mconfig->pipe->p_params->link_dma_id);
114 
115 	ret += snprintf(buf + ret, MOD_BUF - ret,
116 			"\tPCM params:\n\t\tCh %d\n\t\tFreq %d\n\t\tFormat %d\n",
117 			mconfig->pipe->p_params->ch,
118 			mconfig->pipe->p_params->s_freq,
119 			mconfig->pipe->p_params->s_fmt);
120 
121 	ret += snprintf(buf + ret, MOD_BUF - ret,
122 			"\tLink %#x\n\tStream %#x\n",
123 			mconfig->pipe->p_params->linktype,
124 			mconfig->pipe->p_params->stream);
125 
126 	ret += snprintf(buf + ret, MOD_BUF - ret,
127 			"\tState %d\n\tPassthru %s\n",
128 			mconfig->pipe->state,
129 			mconfig->pipe->passthru ? "true" : "false");
130 
131 	ret += skl_print_pins(mconfig->m_in_pin, buf,
132 			mconfig->max_in_queue, ret, true);
133 	ret += skl_print_pins(mconfig->m_out_pin, buf,
134 			mconfig->max_out_queue, ret, false);
135 
136 	ret += snprintf(buf + ret, MOD_BUF - ret,
137 			"Other:\n\tDomain %d\n\tHomogeneous Input %s\n\t"
138 			"Homogeneous Output %s\n\tIn Queue Mask %d\n\t"
139 			"Out Queue Mask %d\n\tDMA ID %d\n\tMem Pages %d\n\t"
140 			"Module Type %d\n\tModule State %d\n",
141 			mconfig->domain,
142 			mconfig->homogenous_inputs ? "true" : "false",
143 			mconfig->homogenous_outputs ? "true" : "false",
144 			mconfig->in_queue_mask, mconfig->out_queue_mask,
145 			mconfig->dma_id, mconfig->mem_pages, mconfig->m_state,
146 			mconfig->m_type);
147 
148 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
149 
150 	kfree(buf);
151 	return ret;
152 }
153 
154 static const struct file_operations mcfg_fops = {
155 	.open = simple_open,
156 	.read = module_read,
157 	.llseek = default_llseek,
158 };
159 
160 
161 void skl_debug_init_module(struct skl_debug *d,
162 			struct snd_soc_dapm_widget *w,
163 			struct skl_module_cfg *mconfig)
164 {
165 	if (!debugfs_create_file(w->name, 0444,
166 				d->modules, mconfig,
167 				&mcfg_fops))
168 		dev_err(d->dev, "%s: module debugfs init failed\n", w->name);
169 }
170 
171 static ssize_t fw_softreg_read(struct file *file, char __user *user_buf,
172 			       size_t count, loff_t *ppos)
173 {
174 	struct skl_debug *d = file->private_data;
175 	struct sst_dsp *sst = d->skl->skl_sst->dsp;
176 	size_t w0_stat_sz = sst->addr.w0_stat_sz;
177 	void __iomem *in_base = sst->mailbox.in_base;
178 	void __iomem *fw_reg_addr;
179 	unsigned int offset;
180 	char *tmp;
181 	ssize_t ret = 0;
182 
183 	tmp = kzalloc(FW_REG_BUF, GFP_KERNEL);
184 	if (!tmp)
185 		return -ENOMEM;
186 
187 	fw_reg_addr = in_base - w0_stat_sz;
188 	memset(d->fw_read_buff, 0, FW_REG_BUF);
189 
190 	if (w0_stat_sz > 0)
191 		__iowrite32_copy(d->fw_read_buff, fw_reg_addr, w0_stat_sz >> 2);
192 
193 	for (offset = 0; offset < FW_REG_SIZE; offset += 16) {
194 		ret += snprintf(tmp + ret, FW_REG_BUF - ret, "%#.4x: ", offset);
195 		hex_dump_to_buffer(d->fw_read_buff + offset, 16, 16, 4,
196 				   tmp + ret, FW_REG_BUF - ret, 0);
197 		ret += strlen(tmp + ret);
198 
199 		/* print newline for each offset */
200 		if (FW_REG_BUF - ret > 0)
201 			tmp[ret++] = '\n';
202 	}
203 
204 	ret = simple_read_from_buffer(user_buf, count, ppos, tmp, ret);
205 	kfree(tmp);
206 
207 	return ret;
208 }
209 
210 static const struct file_operations soft_regs_ctrl_fops = {
211 	.open = simple_open,
212 	.read = fw_softreg_read,
213 	.llseek = default_llseek,
214 };
215 
216 struct skl_debug *skl_debugfs_init(struct skl *skl)
217 {
218 	struct skl_debug *d;
219 
220 	d = devm_kzalloc(&skl->pci->dev, sizeof(*d), GFP_KERNEL);
221 	if (!d)
222 		return NULL;
223 
224 	/* create the debugfs dir with platform component's debugfs as parent */
225 	d->fs = debugfs_create_dir("dsp",
226 				   skl->component->debugfs_root);
227 	if (IS_ERR(d->fs) || !d->fs) {
228 		dev_err(&skl->pci->dev, "debugfs root creation failed\n");
229 		return NULL;
230 	}
231 
232 	d->skl = skl;
233 	d->dev = &skl->pci->dev;
234 
235 	/* now create the module dir */
236 	d->modules = debugfs_create_dir("modules", d->fs);
237 	if (IS_ERR(d->modules) || !d->modules) {
238 		dev_err(&skl->pci->dev, "modules debugfs create failed\n");
239 		goto err;
240 	}
241 
242 	if (!debugfs_create_file("fw_soft_regs_rd", 0444, d->fs, d,
243 				 &soft_regs_ctrl_fops)) {
244 		dev_err(d->dev, "fw soft regs control debugfs init failed\n");
245 		goto err;
246 	}
247 
248 	return d;
249 
250 err:
251 	debugfs_remove_recursive(d->fs);
252 	return NULL;
253 }
254 
255 void skl_debugfs_exit(struct skl *skl)
256 {
257 	struct skl_debug *d = skl->debugfs;
258 
259 	debugfs_remove_recursive(d->fs);
260 
261 	d = NULL;
262 }
263