blob: dc332f5800ad93fbaf226adc9ea036d44f2727e5 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * xhci-debugfs.c - xHCI debugfs interface
4 *
5 * Copyright (C) 2017 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9
10#include <linux/slab.h>
11#include <linux/uaccess.h>
12
13#include "xhci.h"
14#include "xhci-debugfs.h"
15
16static 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),
25};
26
27static 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),
36};
37
38static 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),
47};
48
49static const struct debugfs_reg32 xhci_extcap_legsup[] = {
50 dump_register(EXTCAP_USBLEGSUP),
51 dump_register(EXTCAP_USBLEGCTLSTS),
52};
53
54static 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),
65};
66
67static 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),
82};
83
84static struct dentry *xhci_debugfs_root;
85
86static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
87{
88 struct xhci_regset *regset;
89
90 regset = kzalloc(sizeof(*regset), GFP_KERNEL);
91 if (!regset)
92 return NULL;
93
94 /*
95 * The allocation and free of regset are executed in order.
96 * We needn't a lock here.
97 */
98 INIT_LIST_HEAD(&regset->list);
99 list_add_tail(&regset->list, &xhci->regset_list);
100
101 return regset;
102}
103
104static void xhci_debugfs_free_regset(struct xhci_regset *regset)
105{
106 if (!regset)
107 return;
108
109 list_del(&regset->list);
110 kfree(regset);
111}
112
113static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
114 const struct debugfs_reg32 *regs,
115 size_t nregs, struct dentry *parent,
116 const char *fmt, ...)
117{
118 struct xhci_regset *rgs;
119 va_list args;
120 struct debugfs_regset32 *regset;
121 struct usb_hcd *hcd = xhci_to_hcd(xhci);
122
123 rgs = xhci_debugfs_alloc_regset(xhci);
124 if (!rgs)
125 return;
126
127 va_start(args, fmt);
128 vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
129 va_end(args);
130
131 regset = &rgs->regset;
132 regset->regs = regs;
133 regset->nregs = nregs;
134 regset->base = hcd->regs + base;
135 regset->dev = hcd->self.controller;
136
137 debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
138}
139
140static 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)
143{
144 u32 offset;
145 int index = 0;
146 size_t psic, nregs = n;
147 void __iomem *base = &xhci->cap_regs->hc_capbase;
148
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);
154 }
155
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++;
161 }
162}
163
164static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
165{
166 dma_addr_t dma;
167 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
168
169 dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
170 seq_printf(s, "%pad\n", &dma);
171
172 return 0;
173}
174
175static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
176{
177 dma_addr_t dma;
178 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
179
180 dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
181 seq_printf(s, "%pad\n", &dma);
182
183 return 0;
184}
185
186static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
187{
188 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
189
190 seq_printf(s, "%d\n", ring->cycle_state);
191
192 return 0;
193}
194
195static void xhci_ring_dump_segment(struct seq_file *s,
196 struct xhci_segment *seg)
197{
198 int i;
199 dma_addr_t dma;
200 union xhci_trb *trb;
201 char str[XHCI_MSG_MAX];
202
203 for (i = 0; i < TRBS_PER_SEGMENT; i++) {
204 trb = &seg->trbs[i];
205 dma = seg->dma + i * sizeof(*trb);
206 seq_printf(s, "%pad: %s\n", &dma,
207 xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
208 le32_to_cpu(trb->generic.field[1]),
209 le32_to_cpu(trb->generic.field[2]),
210 le32_to_cpu(trb->generic.field[3])));
211 }
212}
213
214static int xhci_ring_trb_show(struct seq_file *s, void *unused)
215{
216 int i;
217 struct xhci_ring *ring = *(struct xhci_ring **)s->private;
218 struct xhci_segment *seg = ring->first_seg;
219
220 for (i = 0; i < ring->num_segs; i++) {
221 xhci_ring_dump_segment(s, seg);
222 seg = seg->next;
223 }
224
225 return 0;
226}
227
228static struct xhci_file_map ring_files[] = {
229 {"enqueue", xhci_ring_enqueue_show, },
230 {"dequeue", xhci_ring_dequeue_show, },
231 {"cycle", xhci_ring_cycle_show, },
232 {"trbs", xhci_ring_trb_show, },
233};
234
235static int xhci_ring_open(struct inode *inode, struct file *file)
236{
237 int i;
238 struct xhci_file_map *f_map;
239 const char *file_name = file_dentry(file)->d_iname;
240
241 for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
242 f_map = &ring_files[i];
243
244 if (strcmp(f_map->name, file_name) == 0)
245 break;
246 }
247
248 return single_open(file, f_map->show, inode->i_private);
249}
250
251static const struct file_operations xhci_ring_fops = {
252 .open = xhci_ring_open,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
256};
257
258static int xhci_slot_context_show(struct seq_file *s, void *unused)
259{
260 struct xhci_hcd *xhci;
261 struct xhci_slot_ctx *slot_ctx;
262 struct xhci_slot_priv *priv = s->private;
263 struct xhci_virt_device *dev = priv->dev;
264
265 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
266 slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
267 seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
268 xhci_decode_slot_context(le32_to_cpu(slot_ctx->dev_info),
269 le32_to_cpu(slot_ctx->dev_info2),
270 le32_to_cpu(slot_ctx->tt_info),
271 le32_to_cpu(slot_ctx->dev_state)));
272
273 return 0;
274}
275
276static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
277{
278 int ep_index;
279 dma_addr_t dma;
280 struct xhci_hcd *xhci;
281 struct xhci_ep_ctx *ep_ctx;
282 struct xhci_slot_priv *priv = s->private;
283 struct xhci_virt_device *dev = priv->dev;
284
285 xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
286
287 for (ep_index = 0; ep_index < 31; ep_index++) {
288 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
289 dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
290 seq_printf(s, "%pad: %s\n", &dma,
291 xhci_decode_ep_context(le32_to_cpu(ep_ctx->ep_info),
292 le32_to_cpu(ep_ctx->ep_info2),
293 le64_to_cpu(ep_ctx->deq),
294 le32_to_cpu(ep_ctx->tx_info)));
295 }
296
297 return 0;
298}
299
300static int xhci_device_name_show(struct seq_file *s, void *unused)
301{
302 struct xhci_slot_priv *priv = s->private;
303 struct xhci_virt_device *dev = priv->dev;
304
305 seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
306
307 return 0;
308}
309
310static struct xhci_file_map context_files[] = {
311 {"name", xhci_device_name_show, },
312 {"slot-context", xhci_slot_context_show, },
313 {"ep-context", xhci_endpoint_context_show, },
314};
315
316static int xhci_context_open(struct inode *inode, struct file *file)
317{
318 int i;
319 struct xhci_file_map *f_map;
320 const char *file_name = file_dentry(file)->d_iname;
321
322 for (i = 0; i < ARRAY_SIZE(context_files); i++) {
323 f_map = &context_files[i];
324
325 if (strcmp(f_map->name, file_name) == 0)
326 break;
327 }
328
329 return single_open(file, f_map->show, inode->i_private);
330}
331
332static const struct file_operations xhci_context_fops = {
333 .open = xhci_context_open,
334 .read = seq_read,
335 .llseek = seq_lseek,
336 .release = single_release,
337};
338
339
340
341static int xhci_portsc_show(struct seq_file *s, void *unused)
342{
343 struct xhci_port *port = s->private;
344 u32 portsc;
345 char str[XHCI_MSG_MAX];
346
347 portsc = readl(port->addr);
348 seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
349
350 return 0;
351}
352
353static int xhci_port_open(struct inode *inode, struct file *file)
354{
355 return single_open(file, xhci_portsc_show, inode->i_private);
356}
357
358static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
359 size_t count, loff_t *ppos)
360{
361 struct seq_file *s = file->private_data;
362 struct xhci_port *port = s->private;
363 struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
364 char buf[32];
365 u32 portsc;
366 unsigned long flags;
367
368 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
369 return -EFAULT;
370
371 if (!strncmp(buf, "compliance", 10)) {
372 /* If CTC is clear, compliance is enabled by default */
373 if (!HCC2_CTC(xhci->hcc_params2))
374 return count;
375 spin_lock_irqsave(&xhci->lock, flags);
376 /* compliance mode can only be enabled on ports in RxDetect */
377 portsc = readl(port->addr);
378 if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
379 spin_unlock_irqrestore(&xhci->lock, flags);
380 return -EPERM;
381 }
382 portsc = xhci_port_state_to_neutral(portsc);
383 portsc &= ~PORT_PLS_MASK;
384 portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
385 writel(portsc, port->addr);
386 spin_unlock_irqrestore(&xhci->lock, flags);
387 } else {
388 return -EINVAL;
389 }
390 return count;
391}
392
393static const struct file_operations port_fops = {
394 .open = xhci_port_open,
395 .write = xhci_port_write,
396 .read = seq_read,
397 .llseek = seq_lseek,
398 .release = single_release,
399};
400
401static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
402 struct xhci_file_map *files,
403 size_t nentries, void *data,
404 struct dentry *parent,
405 const struct file_operations *fops)
406{
407 int i;
408
409 for (i = 0; i < nentries; i++)
410 debugfs_create_file(files[i].name, 0444, parent, data, fops);
411}
412
413static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
414 struct xhci_ring **ring,
415 const char *name,
416 struct dentry *parent)
417{
418 struct dentry *dir;
419
420 dir = debugfs_create_dir(name, parent);
421 xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
422 ring, dir, &xhci_ring_fops);
423
424 return dir;
425}
426
427static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
428 struct dentry *parent,
429 int slot_id)
430{
431 struct xhci_virt_device *dev = xhci->devs[slot_id];
432
433 xhci_debugfs_create_files(xhci, context_files,
434 ARRAY_SIZE(context_files),
435 dev->debugfs_private,
436 parent, &xhci_context_fops);
437}
438
439void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
440 struct xhci_virt_device *dev,
441 int ep_index)
442{
443 struct xhci_ep_priv *epriv;
444 struct xhci_slot_priv *spriv = dev->debugfs_private;
445
446 if (!spriv)
447 return;
448
449 if (spriv->eps[ep_index])
450 return;
451
452 epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
453 if (!epriv)
454 return;
455
456 snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
457 epriv->root = xhci_debugfs_create_ring_dir(xhci,
458 &dev->eps[ep_index].ring,
459 epriv->name,
460 spriv->root);
461 spriv->eps[ep_index] = epriv;
462}
463
464void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
465 struct xhci_virt_device *dev,
466 int ep_index)
467{
468 struct xhci_ep_priv *epriv;
469 struct xhci_slot_priv *spriv = dev->debugfs_private;
470
471 if (!spriv || !spriv->eps[ep_index])
472 return;
473
474 epriv = spriv->eps[ep_index];
475 debugfs_remove_recursive(epriv->root);
476 spriv->eps[ep_index] = NULL;
477 kfree(epriv);
478}
479
480void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
481{
482 struct xhci_slot_priv *priv;
483 struct xhci_virt_device *dev = xhci->devs[slot_id];
484
485 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
486 if (!priv)
487 return;
488
489 snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
490 priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
491 priv->dev = dev;
492 dev->debugfs_private = priv;
493
494 xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
495 "ep00", priv->root);
496
497 xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
498}
499
500void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
501{
502 int i;
503 struct xhci_slot_priv *priv;
504 struct xhci_virt_device *dev = xhci->devs[slot_id];
505
506 if (!dev || !dev->debugfs_private)
507 return;
508
509 priv = dev->debugfs_private;
510
511 debugfs_remove_recursive(priv->root);
512
513 for (i = 0; i < 31; i++)
514 kfree(priv->eps[i]);
515
516 kfree(priv);
517 dev->debugfs_private = NULL;
518}
519
520static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
521 struct dentry *parent)
522{
523 unsigned int num_ports;
524 char port_name[8];
525 struct xhci_port *port;
526 struct dentry *dir;
527
528 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
529
530 parent = debugfs_create_dir("ports", parent);
531
532 while (num_ports--) {
533 scnprintf(port_name, sizeof(port_name), "port%02d",
534 num_ports + 1);
535 dir = debugfs_create_dir(port_name, parent);
536 port = &xhci->hw_ports[num_ports];
537 debugfs_create_file("portsc", 0644, dir, port, &port_fops);
538 }
539}
540
541void xhci_debugfs_init(struct xhci_hcd *xhci)
542{
543 struct device *dev = xhci_to_hcd(xhci)->self.controller;
544
545 xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
546 xhci_debugfs_root);
547
548 INIT_LIST_HEAD(&xhci->regset_list);
549
550 xhci_debugfs_regset(xhci,
551 0,
552 xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
553 xhci->debugfs_root, "reg-cap");
554
555 xhci_debugfs_regset(xhci,
556 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
557 xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
558 xhci->debugfs_root, "reg-op");
559
560 xhci_debugfs_regset(xhci,
561 readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
562 xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
563 xhci->debugfs_root, "reg-runtime");
564
565 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
566 xhci_extcap_legsup,
567 ARRAY_SIZE(xhci_extcap_legsup),
568 "reg-ext-legsup");
569
570 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
571 xhci_extcap_protocol,
572 ARRAY_SIZE(xhci_extcap_protocol),
573 "reg-ext-protocol");
574
575 xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
576 xhci_extcap_dbc,
577 ARRAY_SIZE(xhci_extcap_dbc),
578 "reg-ext-dbc");
579
580 xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
581 "command-ring",
582 xhci->debugfs_root);
583
584 xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
585 "event-ring",
586 xhci->debugfs_root);
587
588 xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
589
590 xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
591}
592
593void xhci_debugfs_exit(struct xhci_hcd *xhci)
594{
595 struct xhci_regset *rgs, *tmp;
596
597 debugfs_remove_recursive(xhci->debugfs_root);
598 xhci->debugfs_root = NULL;
599 xhci->debugfs_slots = NULL;
600
601 list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
602 xhci_debugfs_free_regset(rgs);
603}
604
605void __init xhci_debugfs_create_root(void)
606{
607 xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
608}
609
610void __exit xhci_debugfs_remove_root(void)
611{
612 debugfs_remove_recursive(xhci_debugfs_root);
613 xhci_debugfs_root = NULL;
614}