1 // SPDX-License-Identifier: GPL-2.0
3 * xhci-debugfs.c - xHCI debugfs interface
5 * Copyright (C) 2017 Intel Corporation
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
14 #include "xhci-debugfs.h"
16 static const struct debugfs_reg32 xhci_cap_regs[] = {
17 dump_register(CAPLENGTH),
18 dump_register(HCSPARAMS1),
19 dump_register(HCSPARAMS2),
20 dump_register(HCSPARAMS3),
21 dump_register(HCCPARAMS1),
22 dump_register(DOORBELLOFF),
23 dump_register(RUNTIMEOFF),
24 dump_register(HCCPARAMS2),
27 static const struct debugfs_reg32 xhci_op_regs[] = {
28 dump_register(USBCMD),
29 dump_register(USBSTS),
30 dump_register(PAGESIZE),
31 dump_register(DNCTRL),
33 dump_register(DCBAAP_LOW),
34 dump_register(DCBAAP_HIGH),
35 dump_register(CONFIG),
38 static const struct debugfs_reg32 xhci_runtime_regs[] = {
39 dump_register(MFINDEX),
40 dump_register(IR0_IMAN),
41 dump_register(IR0_IMOD),
42 dump_register(IR0_ERSTSZ),
43 dump_register(IR0_ERSTBA_LOW),
44 dump_register(IR0_ERSTBA_HIGH),
45 dump_register(IR0_ERDP_LOW),
46 dump_register(IR0_ERDP_HIGH),
49 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
50 dump_register(EXTCAP_USBLEGSUP),
51 dump_register(EXTCAP_USBLEGCTLSTS),
54 static const struct debugfs_reg32 xhci_extcap_protocol[] = {
55 dump_register(EXTCAP_REVISION),
56 dump_register(EXTCAP_NAME),
57 dump_register(EXTCAP_PORTINFO),
58 dump_register(EXTCAP_PORTTYPE),
59 dump_register(EXTCAP_MANTISSA1),
60 dump_register(EXTCAP_MANTISSA2),
61 dump_register(EXTCAP_MANTISSA3),
62 dump_register(EXTCAP_MANTISSA4),
63 dump_register(EXTCAP_MANTISSA5),
64 dump_register(EXTCAP_MANTISSA6),
67 static const struct debugfs_reg32 xhci_extcap_dbc[] = {
68 dump_register(EXTCAP_DBC_CAPABILITY),
69 dump_register(EXTCAP_DBC_DOORBELL),
70 dump_register(EXTCAP_DBC_ERSTSIZE),
71 dump_register(EXTCAP_DBC_ERST_LOW),
72 dump_register(EXTCAP_DBC_ERST_HIGH),
73 dump_register(EXTCAP_DBC_ERDP_LOW),
74 dump_register(EXTCAP_DBC_ERDP_HIGH),
75 dump_register(EXTCAP_DBC_CONTROL),
76 dump_register(EXTCAP_DBC_STATUS),
77 dump_register(EXTCAP_DBC_PORTSC),
78 dump_register(EXTCAP_DBC_CONT_LOW),
79 dump_register(EXTCAP_DBC_CONT_HIGH),
80 dump_register(EXTCAP_DBC_DEVINFO1),
81 dump_register(EXTCAP_DBC_DEVINFO2),
84 static struct dentry *xhci_debugfs_root;
86 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
88 struct xhci_regset *regset;
90 regset = kzalloc(sizeof(*regset), GFP_KERNEL);
95 * The allocation and free of regset are executed in order.
96 * We needn't a lock here.
98 INIT_LIST_HEAD(®set->list);
99 list_add_tail(®set->list, &xhci->regset_list);
104 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
109 list_del(®set->list);
114 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
115 const struct debugfs_reg32 *regs,
116 size_t nregs, struct dentry *parent,
117 const char *fmt, ...)
119 struct xhci_regset *rgs;
121 struct debugfs_regset32 *regset;
122 struct usb_hcd *hcd = xhci_to_hcd(xhci);
124 rgs = xhci_debugfs_alloc_regset(xhci);
129 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
132 regset = &rgs->regset;
134 regset->nregs = nregs;
135 regset->base = hcd->regs + base;
136 regset->dev = hcd->self.controller;
138 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
141 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
142 const struct debugfs_reg32 *regs,
143 size_t n, const char *cap_name)
147 size_t psic, nregs = n;
148 void __iomem *base = &xhci->cap_regs->hc_capbase;
150 offset = xhci_find_next_ext_cap(base, 0, cap_id);
152 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
153 psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
154 nregs = min(4 + psic, n);
157 xhci_debugfs_regset(xhci, offset, regs, nregs,
158 xhci->debugfs_root, "%s:%02d",
160 offset = xhci_find_next_ext_cap(base, offset, cap_id);
165 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
168 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
170 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
171 seq_printf(s, "%pad\n", &dma);
176 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
179 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
181 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
182 seq_printf(s, "%pad\n", &dma);
187 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
189 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
191 seq_printf(s, "%d\n", ring->cycle_state);
196 static void xhci_ring_dump_segment(struct seq_file *s,
197 struct xhci_segment *seg)
202 char str[XHCI_MSG_MAX];
204 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
206 dma = seg->dma + i * sizeof(*trb);
207 seq_printf(s, "%2u %pad: %s\n", seg->num, &dma,
208 xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
209 le32_to_cpu(trb->generic.field[1]),
210 le32_to_cpu(trb->generic.field[2]),
211 le32_to_cpu(trb->generic.field[3])));
215 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
217 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
218 struct xhci_segment *seg = ring->first_seg;
220 xhci_for_each_ring_seg(ring->first_seg, seg)
221 xhci_ring_dump_segment(s, seg);
226 static struct xhci_file_map ring_files[] = {
227 {"enqueue", xhci_ring_enqueue_show, },
228 {"dequeue", xhci_ring_dequeue_show, },
229 {"cycle", xhci_ring_cycle_show, },
230 {"trbs", xhci_ring_trb_show, },
233 static int xhci_ring_open(struct inode *inode, struct file *file)
235 const struct xhci_file_map *f_map = debugfs_get_aux(file);
237 return single_open(file, f_map->show, inode->i_private);
240 static const struct file_operations xhci_ring_fops = {
241 .open = xhci_ring_open,
244 .release = single_release,
247 static int xhci_slot_context_show(struct seq_file *s, void *unused)
249 struct xhci_hcd *xhci;
250 struct xhci_slot_ctx *slot_ctx;
251 struct xhci_slot_priv *priv = s->private;
252 struct xhci_virt_device *dev = priv->dev;
253 char str[XHCI_MSG_MAX];
255 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
256 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
257 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
258 xhci_decode_slot_context(str,
259 le32_to_cpu(slot_ctx->dev_info),
260 le32_to_cpu(slot_ctx->dev_info2),
261 le32_to_cpu(slot_ctx->tt_info),
262 le32_to_cpu(slot_ctx->dev_state)));
267 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
271 struct xhci_hcd *xhci;
272 struct xhci_ep_ctx *ep_ctx;
273 struct xhci_slot_priv *priv = s->private;
274 struct xhci_virt_device *dev = priv->dev;
275 char str[XHCI_MSG_MAX];
277 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
279 for (ep_index = 0; ep_index < 31; ep_index++) {
280 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
281 dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
282 seq_printf(s, "%pad: %s, virt_state:%#x\n", &dma,
283 xhci_decode_ep_context(str,
284 le32_to_cpu(ep_ctx->ep_info),
285 le32_to_cpu(ep_ctx->ep_info2),
286 le64_to_cpu(ep_ctx->deq),
287 le32_to_cpu(ep_ctx->tx_info)),
288 dev->eps[ep_index].ep_state);
294 static int xhci_device_name_show(struct seq_file *s, void *unused)
296 struct xhci_slot_priv *priv = s->private;
297 struct xhci_virt_device *dev = priv->dev;
299 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
304 static struct xhci_file_map context_files[] = {
305 {"name", xhci_device_name_show, },
306 {"slot-context", xhci_slot_context_show, },
307 {"ep-context", xhci_endpoint_context_show, },
310 static int xhci_context_open(struct inode *inode, struct file *file)
312 const struct xhci_file_map *f_map = debugfs_get_aux(file);
314 return single_open(file, f_map->show, inode->i_private);
317 static const struct file_operations xhci_context_fops = {
318 .open = xhci_context_open,
321 .release = single_release,
326 static int xhci_portsc_show(struct seq_file *s, void *unused)
328 struct xhci_port *port = s->private;
330 char str[XHCI_MSG_MAX];
332 portsc = readl(port->addr);
333 seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
338 static int xhci_port_open(struct inode *inode, struct file *file)
340 return single_open(file, xhci_portsc_show, inode->i_private);
343 static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
344 size_t count, loff_t *ppos)
346 struct seq_file *s = file->private_data;
347 struct xhci_port *port = s->private;
348 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
353 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
356 if (!strncmp(buf, "compliance", 10)) {
357 /* If CTC is clear, compliance is enabled by default */
358 if (!HCC2_CTC(xhci->hcc_params2))
360 spin_lock_irqsave(&xhci->lock, flags);
361 /* compliance mode can only be enabled on ports in RxDetect */
362 portsc = readl(port->addr);
363 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
364 spin_unlock_irqrestore(&xhci->lock, flags);
367 portsc = xhci_port_state_to_neutral(portsc);
368 portsc &= ~PORT_PLS_MASK;
369 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
370 writel(portsc, port->addr);
371 spin_unlock_irqrestore(&xhci->lock, flags);
378 static const struct file_operations port_fops = {
379 .open = xhci_port_open,
380 .write = xhci_port_write,
383 .release = single_release,
386 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
387 struct xhci_file_map *files,
388 size_t nentries, void *data,
389 struct dentry *parent,
390 const struct file_operations *fops)
394 for (i = 0; i < nentries; i++)
395 debugfs_create_file_aux(files[i].name, 0444, parent,
396 data, &files[i], fops);
399 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
400 struct xhci_ring **ring,
402 struct dentry *parent)
406 dir = debugfs_create_dir(name, parent);
407 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
408 ring, dir, &xhci_ring_fops);
413 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
414 struct dentry *parent,
417 struct xhci_virt_device *dev = xhci->devs[slot_id];
419 xhci_debugfs_create_files(xhci, context_files,
420 ARRAY_SIZE(context_files),
421 dev->debugfs_private,
422 parent, &xhci_context_fops);
425 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
426 struct xhci_virt_device *dev,
429 struct xhci_ep_priv *epriv;
430 struct xhci_slot_priv *spriv = dev->debugfs_private;
435 if (spriv->eps[ep_index])
438 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
442 epriv->show_ring = dev->eps[ep_index].ring;
444 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
445 epriv->root = xhci_debugfs_create_ring_dir(xhci,
449 spriv->eps[ep_index] = epriv;
452 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
453 struct xhci_virt_device *dev,
456 struct xhci_ep_priv *epriv;
457 struct xhci_slot_priv *spriv = dev->debugfs_private;
459 if (!spriv || !spriv->eps[ep_index])
462 epriv = spriv->eps[ep_index];
463 debugfs_remove_recursive(epriv->root);
464 spriv->eps[ep_index] = NULL;
468 static int xhci_stream_id_show(struct seq_file *s, void *unused)
470 struct xhci_ep_priv *epriv = s->private;
472 if (!epriv->stream_info)
475 seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
476 epriv->stream_id, epriv->stream_info->num_streams - 1);
481 static int xhci_stream_id_open(struct inode *inode, struct file *file)
483 return single_open(file, xhci_stream_id_show, inode->i_private);
486 static ssize_t xhci_stream_id_write(struct file *file, const char __user *ubuf,
487 size_t count, loff_t *ppos)
489 struct seq_file *s = file->private_data;
490 struct xhci_ep_priv *epriv = s->private;
492 u16 stream_id; /* MaxPStreams + 1 <= 16 */
494 if (!epriv->stream_info)
498 ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
502 if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
505 epriv->stream_id = stream_id;
506 epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
511 static const struct file_operations stream_id_fops = {
512 .open = xhci_stream_id_open,
513 .write = xhci_stream_id_write,
516 .release = single_release,
519 static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
521 struct xhci_ep_priv *epriv = s->private;
522 struct xhci_stream_ctx *stream_ctx;
526 if (!epriv->stream_info)
529 seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
530 epriv->stream_info->num_streams,
531 epriv->stream_info->num_stream_ctxs);
533 for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
534 stream_ctx = epriv->stream_info->stream_ctx_array + id;
535 dma = epriv->stream_info->ctx_array_dma + id * 16;
536 if (id < epriv->stream_info->num_streams)
537 seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
538 id, le64_to_cpu(stream_ctx->stream_ring));
540 seq_printf(s, "%pad stream context entry not used deq %016llx\n",
541 &dma, le64_to_cpu(stream_ctx->stream_ring));
546 DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
548 void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
549 struct xhci_virt_device *dev,
552 struct xhci_slot_priv *spriv = dev->debugfs_private;
553 struct xhci_ep_priv *epriv;
555 if (!spriv || !spriv->eps[ep_index] ||
556 !dev->eps[ep_index].stream_info)
559 epriv = spriv->eps[ep_index];
560 epriv->stream_info = dev->eps[ep_index].stream_info;
562 /* Show trb ring of stream ID 1 by default */
563 epriv->stream_id = 1;
564 epriv->show_ring = epriv->stream_info->stream_rings[1];
565 debugfs_create_file("stream_id", 0644,
568 debugfs_create_file("stream_context_array", 0444,
570 &xhci_stream_context_array_fops);
573 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
575 struct xhci_slot_priv *priv;
576 struct xhci_virt_device *dev = xhci->devs[slot_id];
578 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
582 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
583 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
585 dev->debugfs_private = priv;
587 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
590 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
593 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
596 struct xhci_slot_priv *priv;
597 struct xhci_virt_device *dev = xhci->devs[slot_id];
599 if (!dev || !dev->debugfs_private)
602 priv = dev->debugfs_private;
604 debugfs_remove_recursive(priv->root);
606 for (i = 0; i < 31; i++)
610 dev->debugfs_private = NULL;
613 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
614 struct dentry *parent)
616 unsigned int num_ports;
618 struct xhci_port *port;
621 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
623 parent = debugfs_create_dir("ports", parent);
625 while (num_ports--) {
626 scnprintf(port_name, sizeof(port_name), "port%02d",
628 dir = debugfs_create_dir(port_name, parent);
629 port = &xhci->hw_ports[num_ports];
630 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
634 void xhci_debugfs_init(struct xhci_hcd *xhci)
636 struct device *dev = xhci_to_hcd(xhci)->self.controller;
638 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
641 INIT_LIST_HEAD(&xhci->regset_list);
643 xhci_debugfs_regset(xhci,
645 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
646 xhci->debugfs_root, "reg-cap");
648 xhci_debugfs_regset(xhci,
649 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
650 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
651 xhci->debugfs_root, "reg-op");
653 xhci_debugfs_regset(xhci,
654 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
655 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
656 xhci->debugfs_root, "reg-runtime");
658 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
660 ARRAY_SIZE(xhci_extcap_legsup),
663 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
664 xhci_extcap_protocol,
665 ARRAY_SIZE(xhci_extcap_protocol),
668 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
670 ARRAY_SIZE(xhci_extcap_dbc),
673 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
677 xhci_debugfs_create_ring_dir(xhci, &xhci->interrupters[0]->event_ring,
681 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
683 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
686 void xhci_debugfs_exit(struct xhci_hcd *xhci)
688 struct xhci_regset *rgs, *tmp;
690 debugfs_remove_recursive(xhci->debugfs_root);
691 xhci->debugfs_root = NULL;
692 xhci->debugfs_slots = NULL;
694 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
695 xhci_debugfs_free_regset(rgs);
698 void __init xhci_debugfs_create_root(void)
700 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
703 void __exit xhci_debugfs_remove_root(void)
705 debugfs_remove_recursive(xhci_debugfs_root);
706 xhci_debugfs_root = NULL;