2 * xhci-debugfs.c - xHCI debugfs interface
4 * Copyright (C) 2017 Intel Corporation
6 * Author: Lu Baolu <baolu.lu@linux.intel.com>
9 #include <linux/slab.h>
12 #include "xhci-debugfs.h"
14 static const struct debugfs_reg32 xhci_cap_regs[] = {
15 dump_register(CAPLENGTH),
16 dump_register(HCSPARAMS1),
17 dump_register(HCSPARAMS2),
18 dump_register(HCSPARAMS3),
19 dump_register(HCCPARAMS1),
20 dump_register(DOORBELLOFF),
21 dump_register(RUNTIMEOFF),
22 dump_register(HCCPARAMS2),
25 static const struct debugfs_reg32 xhci_op_regs[] = {
26 dump_register(USBCMD),
27 dump_register(USBSTS),
28 dump_register(PAGESIZE),
29 dump_register(DNCTRL),
31 dump_register(DCBAAP_LOW),
32 dump_register(DCBAAP_HIGH),
33 dump_register(CONFIG),
36 static const struct debugfs_reg32 xhci_runtime_regs[] = {
37 dump_register(MFINDEX),
38 dump_register(IR0_IMAN),
39 dump_register(IR0_IMOD),
40 dump_register(IR0_ERSTSZ),
41 dump_register(IR0_ERSTBA_LOW),
42 dump_register(IR0_ERSTBA_HIGH),
43 dump_register(IR0_ERDP_LOW),
44 dump_register(IR0_ERDP_HIGH),
47 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
48 dump_register(EXTCAP_USBLEGSUP),
49 dump_register(EXTCAP_USBLEGCTLSTS),
52 static const struct debugfs_reg32 xhci_extcap_protocol[] = {
53 dump_register(EXTCAP_REVISION),
54 dump_register(EXTCAP_NAME),
55 dump_register(EXTCAP_PORTINFO),
56 dump_register(EXTCAP_PORTTYPE),
57 dump_register(EXTCAP_MANTISSA1),
58 dump_register(EXTCAP_MANTISSA2),
59 dump_register(EXTCAP_MANTISSA3),
60 dump_register(EXTCAP_MANTISSA4),
61 dump_register(EXTCAP_MANTISSA5),
62 dump_register(EXTCAP_MANTISSA6),
65 static const struct debugfs_reg32 xhci_extcap_dbc[] = {
66 dump_register(EXTCAP_DBC_CAPABILITY),
67 dump_register(EXTCAP_DBC_DOORBELL),
68 dump_register(EXTCAP_DBC_ERSTSIZE),
69 dump_register(EXTCAP_DBC_ERST_LOW),
70 dump_register(EXTCAP_DBC_ERST_HIGH),
71 dump_register(EXTCAP_DBC_ERDP_LOW),
72 dump_register(EXTCAP_DBC_ERDP_HIGH),
73 dump_register(EXTCAP_DBC_CONTROL),
74 dump_register(EXTCAP_DBC_STATUS),
75 dump_register(EXTCAP_DBC_PORTSC),
76 dump_register(EXTCAP_DBC_CONT_LOW),
77 dump_register(EXTCAP_DBC_CONT_HIGH),
78 dump_register(EXTCAP_DBC_DEVINFO1),
79 dump_register(EXTCAP_DBC_DEVINFO2),
82 static struct dentry *xhci_debugfs_root;
84 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
86 struct xhci_regset *regset;
88 regset = kzalloc(sizeof(*regset), GFP_KERNEL);
93 * The allocation and free of regset are executed in order.
94 * We needn't a lock here.
96 INIT_LIST_HEAD(®set->list);
97 list_add_tail(®set->list, &xhci->regset_list);
102 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
107 list_del(®set->list);
111 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
112 const struct debugfs_reg32 *regs,
113 size_t nregs, struct dentry *parent,
114 const char *fmt, ...)
116 struct xhci_regset *rgs;
118 struct debugfs_regset32 *regset;
119 struct usb_hcd *hcd = xhci_to_hcd(xhci);
121 rgs = xhci_debugfs_alloc_regset(xhci);
126 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
129 regset = &rgs->regset;
131 regset->nregs = nregs;
132 regset->base = hcd->regs + base;
134 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
137 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
138 const struct debugfs_reg32 *regs,
139 size_t n, const char *cap_name)
143 size_t psic, nregs = n;
144 void __iomem *base = &xhci->cap_regs->hc_capbase;
146 offset = xhci_find_next_ext_cap(base, 0, cap_id);
148 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
149 psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
150 nregs = min(4 + psic, n);
153 xhci_debugfs_regset(xhci, offset, regs, nregs,
154 xhci->debugfs_root, "%s:%02d",
156 offset = xhci_find_next_ext_cap(base, offset, cap_id);
161 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
164 struct xhci_ring *ring = s->private;
166 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
167 seq_printf(s, "%pad\n", &dma);
172 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
175 struct xhci_ring *ring = s->private;
177 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
178 seq_printf(s, "%pad\n", &dma);
183 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
185 struct xhci_ring *ring = s->private;
187 seq_printf(s, "%d\n", ring->cycle_state);
192 static void xhci_ring_dump_segment(struct seq_file *s,
193 struct xhci_segment *seg)
199 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
201 dma = seg->dma + i * sizeof(*trb);
202 seq_printf(s, "%pad: %s\n", &dma,
203 xhci_decode_trb(trb->generic.field[0],
204 trb->generic.field[1],
205 trb->generic.field[2],
206 trb->generic.field[3]));
210 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
213 struct xhci_ring *ring = s->private;
214 struct xhci_segment *seg = ring->first_seg;
216 for (i = 0; i < ring->num_segs; i++) {
217 xhci_ring_dump_segment(s, seg);
224 static struct xhci_file_map ring_files[] = {
225 {"enqueue", xhci_ring_enqueue_show, },
226 {"dequeue", xhci_ring_dequeue_show, },
227 {"cycle", xhci_ring_cycle_show, },
228 {"trbs", xhci_ring_trb_show, },
231 static int xhci_ring_open(struct inode *inode, struct file *file)
234 struct xhci_file_map *f_map;
235 const char *file_name = file_dentry(file)->d_iname;
237 for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
238 f_map = &ring_files[i];
240 if (strcmp(f_map->name, file_name) == 0)
244 return single_open(file, f_map->show, inode->i_private);
247 static const struct file_operations xhci_ring_fops = {
248 .open = xhci_ring_open,
251 .release = single_release,
254 static int xhci_slot_context_show(struct seq_file *s, void *unused)
256 struct xhci_hcd *xhci;
257 struct xhci_slot_ctx *slot_ctx;
258 struct xhci_slot_priv *priv = s->private;
259 struct xhci_virt_device *dev = priv->dev;
261 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
262 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
263 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
264 xhci_decode_slot_context(slot_ctx->dev_info,
267 slot_ctx->dev_state));
272 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
276 struct xhci_hcd *xhci;
277 struct xhci_ep_ctx *ep_ctx;
278 struct xhci_slot_priv *priv = s->private;
279 struct xhci_virt_device *dev = priv->dev;
281 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
283 for (dci = 1; dci < 32; dci++) {
284 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
285 dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
286 seq_printf(s, "%pad: %s\n", &dma,
287 xhci_decode_ep_context(ep_ctx->ep_info,
296 static int xhci_device_name_show(struct seq_file *s, void *unused)
298 struct xhci_slot_priv *priv = s->private;
299 struct xhci_virt_device *dev = priv->dev;
301 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
306 static struct xhci_file_map context_files[] = {
307 {"name", xhci_device_name_show, },
308 {"slot-context", xhci_slot_context_show, },
309 {"ep-context", xhci_endpoint_context_show, },
312 static int xhci_context_open(struct inode *inode, struct file *file)
315 struct xhci_file_map *f_map;
316 const char *file_name = file_dentry(file)->d_iname;
318 for (i = 0; i < ARRAY_SIZE(context_files); i++) {
319 f_map = &context_files[i];
321 if (strcmp(f_map->name, file_name) == 0)
325 return single_open(file, f_map->show, inode->i_private);
328 static const struct file_operations xhci_context_fops = {
329 .open = xhci_context_open,
332 .release = single_release,
335 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
336 struct xhci_file_map *files,
337 size_t nentries, void *data,
338 struct dentry *parent,
339 const struct file_operations *fops)
343 for (i = 0; i < nentries; i++)
344 debugfs_create_file(files[i].name, 0444, parent, data, fops);
347 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
348 struct xhci_ring *ring,
350 struct dentry *parent)
354 dir = debugfs_create_dir(name, parent);
355 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
356 ring, dir, &xhci_ring_fops);
361 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
362 struct dentry *parent,
365 struct xhci_virt_device *dev = xhci->devs[slot_id];
367 xhci_debugfs_create_files(xhci, context_files,
368 ARRAY_SIZE(context_files),
369 dev->debugfs_private,
370 parent, &xhci_context_fops);
373 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
374 struct xhci_virt_device *dev,
377 struct xhci_ep_priv *epriv;
378 struct xhci_slot_priv *spriv = dev->debugfs_private;
380 if (spriv->eps[ep_index])
383 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
387 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
388 epriv->root = xhci_debugfs_create_ring_dir(xhci,
389 dev->eps[ep_index].new_ring,
392 spriv->eps[ep_index] = epriv;
395 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
396 struct xhci_virt_device *dev,
399 struct xhci_ep_priv *epriv;
400 struct xhci_slot_priv *spriv = dev->debugfs_private;
402 if (!spriv || !spriv->eps[ep_index])
405 epriv = spriv->eps[ep_index];
406 debugfs_remove_recursive(epriv->root);
407 spriv->eps[ep_index] = NULL;
411 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
413 struct xhci_slot_priv *priv;
414 struct xhci_virt_device *dev = xhci->devs[slot_id];
416 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
420 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
421 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
423 dev->debugfs_private = priv;
425 xhci_debugfs_create_ring_dir(xhci, dev->eps[0].ring,
428 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
431 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
434 struct xhci_slot_priv *priv;
435 struct xhci_virt_device *dev = xhci->devs[slot_id];
437 if (!dev || !dev->debugfs_private)
440 priv = dev->debugfs_private;
442 debugfs_remove_recursive(priv->root);
444 for (i = 0; i < 31; i++)
448 dev->debugfs_private = NULL;
451 void xhci_debugfs_init(struct xhci_hcd *xhci)
453 struct device *dev = xhci_to_hcd(xhci)->self.controller;
455 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
458 INIT_LIST_HEAD(&xhci->regset_list);
460 xhci_debugfs_regset(xhci,
462 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
463 xhci->debugfs_root, "reg-cap");
465 xhci_debugfs_regset(xhci,
466 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
467 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
468 xhci->debugfs_root, "reg-op");
470 xhci_debugfs_regset(xhci,
471 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
472 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
473 xhci->debugfs_root, "reg-runtime");
475 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
477 ARRAY_SIZE(xhci_extcap_legsup),
480 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
481 xhci_extcap_protocol,
482 ARRAY_SIZE(xhci_extcap_protocol),
485 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
487 ARRAY_SIZE(xhci_extcap_dbc),
490 xhci_debugfs_create_ring_dir(xhci, xhci->cmd_ring,
494 xhci_debugfs_create_ring_dir(xhci, xhci->event_ring,
498 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
501 void xhci_debugfs_exit(struct xhci_hcd *xhci)
503 struct xhci_regset *rgs, *tmp;
505 debugfs_remove_recursive(xhci->debugfs_root);
506 xhci->debugfs_root = NULL;
507 xhci->debugfs_slots = NULL;
509 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
510 xhci_debugfs_free_regset(rgs);
513 void __init xhci_debugfs_create_root(void)
515 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
518 void __exit xhci_debugfs_remove_root(void)
520 debugfs_remove_recursive(xhci_debugfs_root);
521 xhci_debugfs_root = NULL;