WIP FPC-III support
[linux/fpc-iii.git] / drivers / usb / host / xhci-debugfs.c
blob2c0fda57869e4c3c2c08782b1dfefa0bba1114e9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xhci-debugfs.c - xHCI debugfs interface
5 * Copyright (C) 2017 Intel Corporation
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
13 #include "xhci.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),
32 dump_register(CRCR),
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);
91 if (!regset)
92 return NULL;
95 * The allocation and free of regset are executed in order.
96 * We needn't a lock here.
98 INIT_LIST_HEAD(&regset->list);
99 list_add_tail(&regset->list, &xhci->regset_list);
101 return regset;
104 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
106 if (!regset)
107 return;
109 list_del(&regset->list);
110 kfree(regset);
113 __printf(6, 7)
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;
120 va_list args;
121 struct debugfs_regset32 *regset;
122 struct usb_hcd *hcd = xhci_to_hcd(xhci);
124 rgs = xhci_debugfs_alloc_regset(xhci);
125 if (!rgs)
126 return;
128 va_start(args, fmt);
129 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
130 va_end(args);
132 regset = &rgs->regset;
133 regset->regs = regs;
134 regset->nregs = nregs;
135 regset->base = hcd->regs + base;
137 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
140 static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
141 const struct debugfs_reg32 *regs,
142 size_t n, const char *cap_name)
144 u32 offset;
145 int index = 0;
146 size_t psic, nregs = n;
147 void __iomem *base = &xhci->cap_regs->hc_capbase;
149 offset = xhci_find_next_ext_cap(base, 0, cap_id);
150 while (offset) {
151 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
152 psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
153 nregs = min(4 + psic, n);
156 xhci_debugfs_regset(xhci, offset, regs, nregs,
157 xhci->debugfs_root, "%s:%02d",
158 cap_name, index);
159 offset = xhci_find_next_ext_cap(base, offset, cap_id);
160 index++;
164 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
166 dma_addr_t dma;
167 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
169 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
170 seq_printf(s, "%pad\n", &dma);
172 return 0;
175 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
177 dma_addr_t dma;
178 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
180 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
181 seq_printf(s, "%pad\n", &dma);
183 return 0;
186 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
188 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
190 seq_printf(s, "%d\n", ring->cycle_state);
192 return 0;
195 static void xhci_ring_dump_segment(struct seq_file *s,
196 struct xhci_segment *seg)
198 int i;
199 dma_addr_t dma;
200 union xhci_trb *trb;
202 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
203 trb = &seg->trbs[i];
204 dma = seg->dma + i * sizeof(*trb);
205 seq_printf(s, "%pad: %s\n", &dma,
206 xhci_decode_trb(le32_to_cpu(trb->generic.field[0]),
207 le32_to_cpu(trb->generic.field[1]),
208 le32_to_cpu(trb->generic.field[2]),
209 le32_to_cpu(trb->generic.field[3])));
213 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
215 int i;
216 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
217 struct xhci_segment *seg = ring->first_seg;
219 for (i = 0; i < ring->num_segs; i++) {
220 xhci_ring_dump_segment(s, seg);
221 seg = seg->next;
224 return 0;
227 static struct xhci_file_map ring_files[] = {
228 {"enqueue", xhci_ring_enqueue_show, },
229 {"dequeue", xhci_ring_dequeue_show, },
230 {"cycle", xhci_ring_cycle_show, },
231 {"trbs", xhci_ring_trb_show, },
234 static int xhci_ring_open(struct inode *inode, struct file *file)
236 int i;
237 struct xhci_file_map *f_map;
238 const char *file_name = file_dentry(file)->d_iname;
240 for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
241 f_map = &ring_files[i];
243 if (strcmp(f_map->name, file_name) == 0)
244 break;
247 return single_open(file, f_map->show, inode->i_private);
250 static const struct file_operations xhci_ring_fops = {
251 .open = xhci_ring_open,
252 .read = seq_read,
253 .llseek = seq_lseek,
254 .release = single_release,
257 static int xhci_slot_context_show(struct seq_file *s, void *unused)
259 struct xhci_hcd *xhci;
260 struct xhci_slot_ctx *slot_ctx;
261 struct xhci_slot_priv *priv = s->private;
262 struct xhci_virt_device *dev = priv->dev;
264 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
265 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
266 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
267 xhci_decode_slot_context(le32_to_cpu(slot_ctx->dev_info),
268 le32_to_cpu(slot_ctx->dev_info2),
269 le32_to_cpu(slot_ctx->tt_info),
270 le32_to_cpu(slot_ctx->dev_state)));
272 return 0;
275 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
277 int ep_index;
278 dma_addr_t dma;
279 struct xhci_hcd *xhci;
280 struct xhci_ep_ctx *ep_ctx;
281 struct xhci_slot_priv *priv = s->private;
282 struct xhci_virt_device *dev = priv->dev;
284 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
286 for (ep_index = 0; ep_index < 31; ep_index++) {
287 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
288 dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
289 seq_printf(s, "%pad: %s\n", &dma,
290 xhci_decode_ep_context(le32_to_cpu(ep_ctx->ep_info),
291 le32_to_cpu(ep_ctx->ep_info2),
292 le64_to_cpu(ep_ctx->deq),
293 le32_to_cpu(ep_ctx->tx_info)));
296 return 0;
299 static int xhci_device_name_show(struct seq_file *s, void *unused)
301 struct xhci_slot_priv *priv = s->private;
302 struct xhci_virt_device *dev = priv->dev;
304 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
306 return 0;
309 static struct xhci_file_map context_files[] = {
310 {"name", xhci_device_name_show, },
311 {"slot-context", xhci_slot_context_show, },
312 {"ep-context", xhci_endpoint_context_show, },
315 static int xhci_context_open(struct inode *inode, struct file *file)
317 int i;
318 struct xhci_file_map *f_map;
319 const char *file_name = file_dentry(file)->d_iname;
321 for (i = 0; i < ARRAY_SIZE(context_files); i++) {
322 f_map = &context_files[i];
324 if (strcmp(f_map->name, file_name) == 0)
325 break;
328 return single_open(file, f_map->show, inode->i_private);
331 static const struct file_operations xhci_context_fops = {
332 .open = xhci_context_open,
333 .read = seq_read,
334 .llseek = seq_lseek,
335 .release = single_release,
340 static int xhci_portsc_show(struct seq_file *s, void *unused)
342 struct xhci_port *port = s->private;
343 u32 portsc;
345 portsc = readl(port->addr);
346 seq_printf(s, "%s\n", xhci_decode_portsc(portsc));
348 return 0;
351 static int xhci_port_open(struct inode *inode, struct file *file)
353 return single_open(file, xhci_portsc_show, inode->i_private);
356 static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
357 size_t count, loff_t *ppos)
359 struct seq_file *s = file->private_data;
360 struct xhci_port *port = s->private;
361 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
362 char buf[32];
363 u32 portsc;
364 unsigned long flags;
366 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
367 return -EFAULT;
369 if (!strncmp(buf, "compliance", 10)) {
370 /* If CTC is clear, compliance is enabled by default */
371 if (!HCC2_CTC(xhci->hcc_params2))
372 return count;
373 spin_lock_irqsave(&xhci->lock, flags);
374 /* compliance mode can only be enabled on ports in RxDetect */
375 portsc = readl(port->addr);
376 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
377 spin_unlock_irqrestore(&xhci->lock, flags);
378 return -EPERM;
380 portsc = xhci_port_state_to_neutral(portsc);
381 portsc &= ~PORT_PLS_MASK;
382 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
383 writel(portsc, port->addr);
384 spin_unlock_irqrestore(&xhci->lock, flags);
385 } else {
386 return -EINVAL;
388 return count;
391 static const struct file_operations port_fops = {
392 .open = xhci_port_open,
393 .write = xhci_port_write,
394 .read = seq_read,
395 .llseek = seq_lseek,
396 .release = single_release,
399 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
400 struct xhci_file_map *files,
401 size_t nentries, void *data,
402 struct dentry *parent,
403 const struct file_operations *fops)
405 int i;
407 for (i = 0; i < nentries; i++)
408 debugfs_create_file(files[i].name, 0444, parent, data, fops);
411 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
412 struct xhci_ring **ring,
413 const char *name,
414 struct dentry *parent)
416 struct dentry *dir;
418 dir = debugfs_create_dir(name, parent);
419 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
420 ring, dir, &xhci_ring_fops);
422 return dir;
425 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
426 struct dentry *parent,
427 int slot_id)
429 struct xhci_virt_device *dev = xhci->devs[slot_id];
431 xhci_debugfs_create_files(xhci, context_files,
432 ARRAY_SIZE(context_files),
433 dev->debugfs_private,
434 parent, &xhci_context_fops);
437 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
438 struct xhci_virt_device *dev,
439 int ep_index)
441 struct xhci_ep_priv *epriv;
442 struct xhci_slot_priv *spriv = dev->debugfs_private;
444 if (!spriv)
445 return;
447 if (spriv->eps[ep_index])
448 return;
450 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
451 if (!epriv)
452 return;
454 epriv->show_ring = dev->eps[ep_index].ring;
456 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
457 epriv->root = xhci_debugfs_create_ring_dir(xhci,
458 &epriv->show_ring,
459 epriv->name,
460 spriv->root);
461 spriv->eps[ep_index] = epriv;
464 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
465 struct xhci_virt_device *dev,
466 int ep_index)
468 struct xhci_ep_priv *epriv;
469 struct xhci_slot_priv *spriv = dev->debugfs_private;
471 if (!spriv || !spriv->eps[ep_index])
472 return;
474 epriv = spriv->eps[ep_index];
475 debugfs_remove_recursive(epriv->root);
476 spriv->eps[ep_index] = NULL;
477 kfree(epriv);
480 static int xhci_stream_id_show(struct seq_file *s, void *unused)
482 struct xhci_ep_priv *epriv = s->private;
484 if (!epriv->stream_info)
485 return -EPERM;
487 seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
488 epriv->stream_id, epriv->stream_info->num_streams - 1);
490 return 0;
493 static int xhci_stream_id_open(struct inode *inode, struct file *file)
495 return single_open(file, xhci_stream_id_show, inode->i_private);
498 static ssize_t xhci_stream_id_write(struct file *file, const char __user *ubuf,
499 size_t count, loff_t *ppos)
501 struct seq_file *s = file->private_data;
502 struct xhci_ep_priv *epriv = s->private;
503 int ret;
504 u16 stream_id; /* MaxPStreams + 1 <= 16 */
506 if (!epriv->stream_info)
507 return -EPERM;
509 /* Decimal number */
510 ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
511 if (ret)
512 return ret;
514 if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
515 return -EINVAL;
517 epriv->stream_id = stream_id;
518 epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
520 return count;
523 static const struct file_operations stream_id_fops = {
524 .open = xhci_stream_id_open,
525 .write = xhci_stream_id_write,
526 .read = seq_read,
527 .llseek = seq_lseek,
528 .release = single_release,
531 static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
533 struct xhci_ep_priv *epriv = s->private;
534 struct xhci_stream_ctx *stream_ctx;
535 dma_addr_t dma;
536 int id;
538 if (!epriv->stream_info)
539 return -EPERM;
541 seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
542 epriv->stream_info->num_streams,
543 epriv->stream_info->num_stream_ctxs);
545 for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
546 stream_ctx = epriv->stream_info->stream_ctx_array + id;
547 dma = epriv->stream_info->ctx_array_dma + id * 16;
548 if (id < epriv->stream_info->num_streams)
549 seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
550 id, le64_to_cpu(stream_ctx->stream_ring));
551 else
552 seq_printf(s, "%pad stream context entry not used deq %016llx\n",
553 &dma, le64_to_cpu(stream_ctx->stream_ring));
556 return 0;
558 DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
560 void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
561 struct xhci_virt_device *dev,
562 int ep_index)
564 struct xhci_slot_priv *spriv = dev->debugfs_private;
565 struct xhci_ep_priv *epriv;
567 if (!spriv || !spriv->eps[ep_index] ||
568 !dev->eps[ep_index].stream_info)
569 return;
571 epriv = spriv->eps[ep_index];
572 epriv->stream_info = dev->eps[ep_index].stream_info;
574 /* Show trb ring of stream ID 1 by default */
575 epriv->stream_id = 1;
576 epriv->show_ring = epriv->stream_info->stream_rings[1];
577 debugfs_create_file("stream_id", 0644,
578 epriv->root, epriv,
579 &stream_id_fops);
580 debugfs_create_file("stream_context_array", 0444,
581 epriv->root, epriv,
582 &xhci_stream_context_array_fops);
585 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
587 struct xhci_slot_priv *priv;
588 struct xhci_virt_device *dev = xhci->devs[slot_id];
590 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
591 if (!priv)
592 return;
594 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
595 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
596 priv->dev = dev;
597 dev->debugfs_private = priv;
599 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
600 "ep00", priv->root);
602 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
605 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
607 int i;
608 struct xhci_slot_priv *priv;
609 struct xhci_virt_device *dev = xhci->devs[slot_id];
611 if (!dev || !dev->debugfs_private)
612 return;
614 priv = dev->debugfs_private;
616 debugfs_remove_recursive(priv->root);
618 for (i = 0; i < 31; i++)
619 kfree(priv->eps[i]);
621 kfree(priv);
622 dev->debugfs_private = NULL;
625 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
626 struct dentry *parent)
628 unsigned int num_ports;
629 char port_name[8];
630 struct xhci_port *port;
631 struct dentry *dir;
633 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
635 parent = debugfs_create_dir("ports", parent);
637 while (num_ports--) {
638 scnprintf(port_name, sizeof(port_name), "port%02d",
639 num_ports + 1);
640 dir = debugfs_create_dir(port_name, parent);
641 port = &xhci->hw_ports[num_ports];
642 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
646 void xhci_debugfs_init(struct xhci_hcd *xhci)
648 struct device *dev = xhci_to_hcd(xhci)->self.controller;
650 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
651 xhci_debugfs_root);
653 INIT_LIST_HEAD(&xhci->regset_list);
655 xhci_debugfs_regset(xhci,
657 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
658 xhci->debugfs_root, "reg-cap");
660 xhci_debugfs_regset(xhci,
661 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
662 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
663 xhci->debugfs_root, "reg-op");
665 xhci_debugfs_regset(xhci,
666 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
667 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
668 xhci->debugfs_root, "reg-runtime");
670 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
671 xhci_extcap_legsup,
672 ARRAY_SIZE(xhci_extcap_legsup),
673 "reg-ext-legsup");
675 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
676 xhci_extcap_protocol,
677 ARRAY_SIZE(xhci_extcap_protocol),
678 "reg-ext-protocol");
680 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
681 xhci_extcap_dbc,
682 ARRAY_SIZE(xhci_extcap_dbc),
683 "reg-ext-dbc");
685 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
686 "command-ring",
687 xhci->debugfs_root);
689 xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
690 "event-ring",
691 xhci->debugfs_root);
693 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
695 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
698 void xhci_debugfs_exit(struct xhci_hcd *xhci)
700 struct xhci_regset *rgs, *tmp;
702 debugfs_remove_recursive(xhci->debugfs_root);
703 xhci->debugfs_root = NULL;
704 xhci->debugfs_slots = NULL;
706 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
707 xhci_debugfs_free_regset(rgs);
710 void __init xhci_debugfs_create_root(void)
712 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
715 void __exit xhci_debugfs_remove_root(void)
717 debugfs_remove_recursive(xhci_debugfs_root);
718 xhci_debugfs_root = NULL;