1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
7 #include <linux/debugfs.h>
8 #include <linux/device.h>
9 #include <linux/interrupt.h>
10 #include <linux/list.h>
11 #include <linux/mhi.h>
12 #include <linux/module.h>
15 static int mhi_debugfs_states_show(struct seq_file *m, void *d)
17 struct mhi_controller *mhi_cntrl = m->private;
20 seq_printf(m, "PM state: %s Device: %s MHI state: %s EE: %s wake: %s\n",
21 to_mhi_pm_state_str(mhi_cntrl->pm_state),
22 mhi_is_active(mhi_cntrl) ? "Active" : "Inactive",
23 TO_MHI_STATE_STR(mhi_cntrl->dev_state),
24 TO_MHI_EXEC_STR(mhi_cntrl->ee),
25 mhi_cntrl->wake_set ? "true" : "false");
28 seq_printf(m, "M0: %u M2: %u M3: %u", mhi_cntrl->M0, mhi_cntrl->M2,
31 seq_printf(m, " device wake: %u pending packets: %u\n",
32 atomic_read(&mhi_cntrl->dev_wake),
33 atomic_read(&mhi_cntrl->pending_pkts));
38 static int mhi_debugfs_events_show(struct seq_file *m, void *d)
40 struct mhi_controller *mhi_cntrl = m->private;
41 struct mhi_event *mhi_event;
42 struct mhi_event_ctxt *er_ctxt;
45 if (!mhi_is_active(mhi_cntrl)) {
46 seq_puts(m, "Device not ready\n");
50 er_ctxt = mhi_cntrl->mhi_ctxt->er_ctxt;
51 mhi_event = mhi_cntrl->mhi_event;
52 for (i = 0; i < mhi_cntrl->total_ev_rings;
53 i++, er_ctxt++, mhi_event++) {
54 struct mhi_ring *ring = &mhi_event->ring;
56 if (mhi_event->offload_ev) {
57 seq_printf(m, "Index: %d is an offload event ring\n",
62 seq_printf(m, "Index: %d intmod count: %lu time: %lu",
63 i, (er_ctxt->intmod & EV_CTX_INTMODC_MASK) >>
65 (er_ctxt->intmod & EV_CTX_INTMODT_MASK) >>
66 EV_CTX_INTMODT_SHIFT);
68 seq_printf(m, " base: 0x%0llx len: 0x%llx", er_ctxt->rbase,
71 seq_printf(m, " rp: 0x%llx wp: 0x%llx", er_ctxt->rp,
74 seq_printf(m, " local rp: 0x%pK db: 0x%pad\n", ring->rp,
75 &mhi_event->db_cfg.db_val);
81 static int mhi_debugfs_channels_show(struct seq_file *m, void *d)
83 struct mhi_controller *mhi_cntrl = m->private;
84 struct mhi_chan *mhi_chan;
85 struct mhi_chan_ctxt *chan_ctxt;
88 if (!mhi_is_active(mhi_cntrl)) {
89 seq_puts(m, "Device not ready\n");
93 mhi_chan = mhi_cntrl->mhi_chan;
94 chan_ctxt = mhi_cntrl->mhi_ctxt->chan_ctxt;
95 for (i = 0; i < mhi_cntrl->max_chan; i++, chan_ctxt++, mhi_chan++) {
96 struct mhi_ring *ring = &mhi_chan->tre_ring;
98 if (mhi_chan->offload_ch) {
99 seq_printf(m, "%s(%u) is an offload channel\n",
100 mhi_chan->name, mhi_chan->chan);
104 if (!mhi_chan->mhi_dev)
108 "%s(%u) state: 0x%lx brstmode: 0x%lx pollcfg: 0x%lx",
109 mhi_chan->name, mhi_chan->chan, (chan_ctxt->chcfg &
110 CHAN_CTX_CHSTATE_MASK) >> CHAN_CTX_CHSTATE_SHIFT,
111 (chan_ctxt->chcfg & CHAN_CTX_BRSTMODE_MASK) >>
112 CHAN_CTX_BRSTMODE_SHIFT, (chan_ctxt->chcfg &
113 CHAN_CTX_POLLCFG_MASK) >> CHAN_CTX_POLLCFG_SHIFT);
115 seq_printf(m, " type: 0x%x event ring: %u", chan_ctxt->chtype,
118 seq_printf(m, " base: 0x%llx len: 0x%llx rp: 0x%llx wp: 0x%llx",
119 chan_ctxt->rbase, chan_ctxt->rlen, chan_ctxt->rp,
122 seq_printf(m, " local rp: 0x%pK local wp: 0x%pK db: 0x%pad\n",
124 &mhi_chan->db_cfg.db_val);
130 static int mhi_device_info_show(struct device *dev, void *data)
132 struct mhi_device *mhi_dev;
134 if (dev->bus != &mhi_bus_type)
137 mhi_dev = to_mhi_device(dev);
139 seq_printf((struct seq_file *)data, "%s: type: %s dev_wake: %u",
140 mhi_dev->name, mhi_dev->dev_type ? "Controller" : "Transfer",
143 /* for transfer device types only */
144 if (mhi_dev->dev_type == MHI_DEVICE_XFER)
145 seq_printf((struct seq_file *)data, " channels: %u(UL)/%u(DL)",
146 mhi_dev->ul_chan_id, mhi_dev->dl_chan_id);
148 seq_puts((struct seq_file *)data, "\n");
153 static int mhi_debugfs_devices_show(struct seq_file *m, void *d)
155 struct mhi_controller *mhi_cntrl = m->private;
157 if (!mhi_is_active(mhi_cntrl)) {
158 seq_puts(m, "Device not ready\n");
162 device_for_each_child(mhi_cntrl->cntrl_dev, m, mhi_device_info_show);
167 static int mhi_debugfs_regdump_show(struct seq_file *m, void *d)
169 struct mhi_controller *mhi_cntrl = m->private;
170 enum mhi_state state;
174 void __iomem *mhi_base = mhi_cntrl->regs;
175 void __iomem *bhi_base = mhi_cntrl->bhi;
176 void __iomem *bhie_base = mhi_cntrl->bhie;
177 void __iomem *wake_db = mhi_cntrl->wake_db;
183 { "MHI_REGLEN", MHIREGLEN, mhi_base},
184 { "MHI_VER", MHIVER, mhi_base},
185 { "MHI_CFG", MHICFG, mhi_base},
186 { "MHI_CTRL", MHICTRL, mhi_base},
187 { "MHI_STATUS", MHISTATUS, mhi_base},
188 { "MHI_WAKE_DB", 0, wake_db},
189 { "BHI_EXECENV", BHI_EXECENV, bhi_base},
190 { "BHI_STATUS", BHI_STATUS, bhi_base},
191 { "BHI_ERRCODE", BHI_ERRCODE, bhi_base},
192 { "BHI_ERRDBG1", BHI_ERRDBG1, bhi_base},
193 { "BHI_ERRDBG2", BHI_ERRDBG2, bhi_base},
194 { "BHI_ERRDBG3", BHI_ERRDBG3, bhi_base},
195 { "BHIE_TXVEC_DB", BHIE_TXVECDB_OFFS, bhie_base},
196 { "BHIE_TXVEC_STATUS", BHIE_TXVECSTATUS_OFFS, bhie_base},
197 { "BHIE_RXVEC_DB", BHIE_RXVECDB_OFFS, bhie_base},
198 { "BHIE_RXVEC_STATUS", BHIE_RXVECSTATUS_OFFS, bhie_base},
202 if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state))
205 seq_printf(m, "Host PM state: %s Device state: %s EE: %s\n",
206 to_mhi_pm_state_str(mhi_cntrl->pm_state),
207 TO_MHI_STATE_STR(mhi_cntrl->dev_state),
208 TO_MHI_EXEC_STR(mhi_cntrl->ee));
210 state = mhi_get_mhi_state(mhi_cntrl);
211 ee = mhi_get_exec_env(mhi_cntrl);
212 seq_printf(m, "Device EE: %s state: %s\n", TO_MHI_EXEC_STR(ee),
213 TO_MHI_STATE_STR(state));
215 for (i = 0; regs[i].name; i++) {
218 ret = mhi_read_reg(mhi_cntrl, regs[i].base, regs[i].offset,
223 seq_printf(m, "%s: 0x%x\n", regs[i].name, val);
229 static int mhi_debugfs_device_wake_show(struct seq_file *m, void *d)
231 struct mhi_controller *mhi_cntrl = m->private;
232 struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
234 if (!mhi_is_active(mhi_cntrl)) {
235 seq_puts(m, "Device not ready\n");
240 "Wake count: %d\n%s\n", mhi_dev->dev_wake,
241 "Usage: echo get/put > device_wake to vote/unvote for M0");
246 static ssize_t mhi_debugfs_device_wake_write(struct file *file,
247 const char __user *ubuf,
248 size_t count, loff_t *ppos)
250 struct seq_file *m = file->private_data;
251 struct mhi_controller *mhi_cntrl = m->private;
252 struct mhi_device *mhi_dev = mhi_cntrl->mhi_dev;
256 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
259 if (!strncmp(buf, "get", 3)) {
260 ret = mhi_device_get_sync(mhi_dev);
261 } else if (!strncmp(buf, "put", 3)) {
262 mhi_device_put(mhi_dev);
266 return ret ? ret : count;
269 static int mhi_debugfs_timeout_ms_show(struct seq_file *m, void *d)
271 struct mhi_controller *mhi_cntrl = m->private;
273 seq_printf(m, "%u ms\n", mhi_cntrl->timeout_ms);
278 static ssize_t mhi_debugfs_timeout_ms_write(struct file *file,
279 const char __user *ubuf,
280 size_t count, loff_t *ppos)
282 struct seq_file *m = file->private_data;
283 struct mhi_controller *mhi_cntrl = m->private;
286 if (kstrtou32_from_user(ubuf, count, 0, &timeout_ms))
289 mhi_cntrl->timeout_ms = timeout_ms;
294 static int mhi_debugfs_states_open(struct inode *inode, struct file *fp)
296 return single_open(fp, mhi_debugfs_states_show, inode->i_private);
299 static int mhi_debugfs_events_open(struct inode *inode, struct file *fp)
301 return single_open(fp, mhi_debugfs_events_show, inode->i_private);
304 static int mhi_debugfs_channels_open(struct inode *inode, struct file *fp)
306 return single_open(fp, mhi_debugfs_channels_show, inode->i_private);
309 static int mhi_debugfs_devices_open(struct inode *inode, struct file *fp)
311 return single_open(fp, mhi_debugfs_devices_show, inode->i_private);
314 static int mhi_debugfs_regdump_open(struct inode *inode, struct file *fp)
316 return single_open(fp, mhi_debugfs_regdump_show, inode->i_private);
319 static int mhi_debugfs_device_wake_open(struct inode *inode, struct file *fp)
321 return single_open(fp, mhi_debugfs_device_wake_show, inode->i_private);
324 static int mhi_debugfs_timeout_ms_open(struct inode *inode, struct file *fp)
326 return single_open(fp, mhi_debugfs_timeout_ms_show, inode->i_private);
329 static const struct file_operations debugfs_states_fops = {
330 .open = mhi_debugfs_states_open,
331 .release = single_release,
335 static const struct file_operations debugfs_events_fops = {
336 .open = mhi_debugfs_events_open,
337 .release = single_release,
341 static const struct file_operations debugfs_channels_fops = {
342 .open = mhi_debugfs_channels_open,
343 .release = single_release,
347 static const struct file_operations debugfs_devices_fops = {
348 .open = mhi_debugfs_devices_open,
349 .release = single_release,
353 static const struct file_operations debugfs_regdump_fops = {
354 .open = mhi_debugfs_regdump_open,
355 .release = single_release,
359 static const struct file_operations debugfs_device_wake_fops = {
360 .open = mhi_debugfs_device_wake_open,
361 .write = mhi_debugfs_device_wake_write,
362 .release = single_release,
366 static const struct file_operations debugfs_timeout_ms_fops = {
367 .open = mhi_debugfs_timeout_ms_open,
368 .write = mhi_debugfs_timeout_ms_write,
369 .release = single_release,
373 static struct dentry *mhi_debugfs_root;
375 void mhi_create_debugfs(struct mhi_controller *mhi_cntrl)
377 mhi_cntrl->debugfs_dentry =
378 debugfs_create_dir(dev_name(mhi_cntrl->cntrl_dev),
381 debugfs_create_file("states", 0444, mhi_cntrl->debugfs_dentry,
382 mhi_cntrl, &debugfs_states_fops);
383 debugfs_create_file("events", 0444, mhi_cntrl->debugfs_dentry,
384 mhi_cntrl, &debugfs_events_fops);
385 debugfs_create_file("channels", 0444, mhi_cntrl->debugfs_dentry,
386 mhi_cntrl, &debugfs_channels_fops);
387 debugfs_create_file("devices", 0444, mhi_cntrl->debugfs_dentry,
388 mhi_cntrl, &debugfs_devices_fops);
389 debugfs_create_file("regdump", 0444, mhi_cntrl->debugfs_dentry,
390 mhi_cntrl, &debugfs_regdump_fops);
391 debugfs_create_file("device_wake", 0644, mhi_cntrl->debugfs_dentry,
392 mhi_cntrl, &debugfs_device_wake_fops);
393 debugfs_create_file("timeout_ms", 0644, mhi_cntrl->debugfs_dentry,
394 mhi_cntrl, &debugfs_timeout_ms_fops);
397 void mhi_destroy_debugfs(struct mhi_controller *mhi_cntrl)
399 debugfs_remove_recursive(mhi_cntrl->debugfs_dentry);
400 mhi_cntrl->debugfs_dentry = NULL;
403 void mhi_debugfs_init(void)
405 mhi_debugfs_root = debugfs_create_dir(mhi_bus_type.name, NULL);
408 void mhi_debugfs_exit(void)
410 debugfs_remove_recursive(mhi_debugfs_root);