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