usb: xhci: Add debugfs interface for xHCI driver
[linux-2.6-microblaze.git] / drivers / usb / host / xhci-debugfs.c
1 /*
2  * xhci-debugfs.c - xHCI debugfs interface
3  *
4  * Copyright (C) 2017 Intel Corporation
5  *
6  * Author: Lu Baolu <baolu.lu@linux.intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/slab.h>
14
15 #include "xhci.h"
16 #include "xhci-debugfs.h"
17
18 static const struct debugfs_reg32 xhci_cap_regs[] = {
19         dump_register(CAPLENGTH),
20         dump_register(HCSPARAMS1),
21         dump_register(HCSPARAMS2),
22         dump_register(HCSPARAMS3),
23         dump_register(HCCPARAMS1),
24         dump_register(DOORBELLOFF),
25         dump_register(RUNTIMEOFF),
26         dump_register(HCCPARAMS2),
27 };
28
29 static const struct debugfs_reg32 xhci_op_regs[] = {
30         dump_register(USBCMD),
31         dump_register(USBSTS),
32         dump_register(PAGESIZE),
33         dump_register(DNCTRL),
34         dump_register(CRCR),
35         dump_register(DCBAAP_LOW),
36         dump_register(DCBAAP_HIGH),
37         dump_register(CONFIG),
38 };
39
40 static const struct debugfs_reg32 xhci_runtime_regs[] = {
41         dump_register(MFINDEX),
42         dump_register(IR0_IMAN),
43         dump_register(IR0_IMOD),
44         dump_register(IR0_ERSTSZ),
45         dump_register(IR0_ERSTBA_LOW),
46         dump_register(IR0_ERSTBA_HIGH),
47         dump_register(IR0_ERDP_LOW),
48         dump_register(IR0_ERDP_HIGH),
49 };
50
51 static const struct debugfs_reg32 xhci_extcap_legsup[] = {
52         dump_register(EXTCAP_USBLEGSUP),
53         dump_register(EXTCAP_USBLEGCTLSTS),
54 };
55
56 static const struct debugfs_reg32 xhci_extcap_protocol[] = {
57         dump_register(EXTCAP_REVISION),
58         dump_register(EXTCAP_NAME),
59         dump_register(EXTCAP_PORTINFO),
60         dump_register(EXTCAP_PORTTYPE),
61         dump_register(EXTCAP_MANTISSA1),
62         dump_register(EXTCAP_MANTISSA2),
63         dump_register(EXTCAP_MANTISSA3),
64         dump_register(EXTCAP_MANTISSA4),
65         dump_register(EXTCAP_MANTISSA5),
66         dump_register(EXTCAP_MANTISSA6),
67 };
68
69 static const struct debugfs_reg32 xhci_extcap_dbc[] = {
70         dump_register(EXTCAP_DBC_CAPABILITY),
71         dump_register(EXTCAP_DBC_DOORBELL),
72         dump_register(EXTCAP_DBC_ERSTSIZE),
73         dump_register(EXTCAP_DBC_ERST_LOW),
74         dump_register(EXTCAP_DBC_ERST_HIGH),
75         dump_register(EXTCAP_DBC_ERDP_LOW),
76         dump_register(EXTCAP_DBC_ERDP_HIGH),
77         dump_register(EXTCAP_DBC_CONTROL),
78         dump_register(EXTCAP_DBC_STATUS),
79         dump_register(EXTCAP_DBC_PORTSC),
80         dump_register(EXTCAP_DBC_CONT_LOW),
81         dump_register(EXTCAP_DBC_CONT_HIGH),
82         dump_register(EXTCAP_DBC_DEVINFO1),
83         dump_register(EXTCAP_DBC_DEVINFO2),
84 };
85
86 static struct dentry *xhci_debugfs_root;
87
88 static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
89 {
90         struct xhci_regset      *regset;
91
92         regset = kzalloc(sizeof(*regset), GFP_KERNEL);
93         if (!regset)
94                 return NULL;
95
96         /*
97          * The allocation and free of regset are executed in order.
98          * We needn't a lock here.
99          */
100         INIT_LIST_HEAD(&regset->list);
101         list_add_tail(&regset->list, &xhci->regset_list);
102
103         return regset;
104 }
105
106 static void xhci_debugfs_free_regset(struct xhci_regset *regset)
107 {
108         if (!regset)
109                 return;
110
111         list_del(&regset->list);
112         kfree(regset);
113 }
114
115 static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
116                                 const struct debugfs_reg32 *regs,
117                                 size_t nregs, struct dentry *parent,
118                                 const char *fmt, ...)
119 {
120         struct xhci_regset      *rgs;
121         va_list                 args;
122         struct debugfs_regset32 *regset;
123         struct usb_hcd          *hcd = xhci_to_hcd(xhci);
124
125         rgs = xhci_debugfs_alloc_regset(xhci);
126         if (!rgs)
127                 return;
128
129         va_start(args, fmt);
130         vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
131         va_end(args);
132
133         regset = &rgs->regset;
134         regset->regs = regs;
135         regset->nregs = nregs;
136         regset->base = hcd->regs + base;
137
138         debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
139 }
140
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)
144 {
145         u32                     offset;
146         int                     index = 0;
147         size_t                  psic, nregs = n;
148         void __iomem            *base = &xhci->cap_regs->hc_capbase;
149
150         offset = xhci_find_next_ext_cap(base, 0, cap_id);
151         while (offset) {
152                 if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
153                         psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
154                         nregs = min(4 + psic, n);
155                 }
156
157                 xhci_debugfs_regset(xhci, offset, regs, nregs,
158                                     xhci->debugfs_root, "%s:%02d",
159                                     cap_name, index);
160                 offset = xhci_find_next_ext_cap(base, offset, cap_id);
161                 index++;
162         }
163 }
164
165 static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
166 {
167         dma_addr_t              dma;
168         struct xhci_ring        *ring = s->private;
169
170         dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
171         seq_printf(s, "%pad\n", &dma);
172
173         return 0;
174 }
175
176 static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
177 {
178         dma_addr_t              dma;
179         struct xhci_ring        *ring = s->private;
180
181         dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
182         seq_printf(s, "%pad\n", &dma);
183
184         return 0;
185 }
186
187 static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
188 {
189         struct xhci_ring        *ring = s->private;
190
191         seq_printf(s, "%d\n", ring->cycle_state);
192
193         return 0;
194 }
195
196 static void xhci_ring_dump_segment(struct seq_file *s,
197                                    struct xhci_segment *seg)
198 {
199         int                     i;
200         dma_addr_t              dma;
201         union xhci_trb          *trb;
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(trb->generic.field[0],
208                                            trb->generic.field[1],
209                                            trb->generic.field[2],
210                                            trb->generic.field[3]));
211         }
212 }
213
214 static int xhci_ring_trb_show(struct seq_file *s, void *unused)
215 {
216         int                     i;
217         struct xhci_ring        *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
228 static 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
235 static 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
251 static 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
258 static 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(slot_ctx->dev_info,
269                                             slot_ctx->dev_info2,
270                                             slot_ctx->tt_info,
271                                             slot_ctx->dev_state));
272
273         return 0;
274 }
275
276 static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
277 {
278         int                     dci;
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 (dci = 1; dci < 32; dci++) {
288                 ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
289                 dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
290                 seq_printf(s, "%pad: %s\n", &dma,
291                            xhci_decode_ep_context(ep_ctx->ep_info,
292                                                   ep_ctx->ep_info2,
293                                                   ep_ctx->deq,
294                                                   ep_ctx->tx_info));
295         }
296
297         return 0;
298 }
299
300 static 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
310 static 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
316 static 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
332 static 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 static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
340                                       struct xhci_file_map *files,
341                                       size_t nentries, void *data,
342                                       struct dentry *parent,
343                                       const struct file_operations *fops)
344 {
345         int                     i;
346
347         for (i = 0; i < nentries; i++)
348                 debugfs_create_file(files[i].name, 0444, parent, data, fops);
349 }
350
351 static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
352                                                    struct xhci_ring *ring,
353                                                    const char *name,
354                                                    struct dentry *parent)
355 {
356         struct dentry           *dir;
357
358         dir = debugfs_create_dir(name, parent);
359         xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
360                                   ring, dir, &xhci_ring_fops);
361
362         return dir;
363 }
364
365 static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
366                                               struct dentry *parent,
367                                               int slot_id)
368 {
369         struct xhci_virt_device *dev = xhci->devs[slot_id];
370
371         xhci_debugfs_create_files(xhci, context_files,
372                                   ARRAY_SIZE(context_files),
373                                   dev->debugfs_private,
374                                   parent, &xhci_context_fops);
375 }
376
377 void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
378                                   struct xhci_virt_device *dev,
379                                   int ep_index)
380 {
381         struct xhci_ep_priv     *epriv;
382         struct xhci_slot_priv   *spriv = dev->debugfs_private;
383
384         if (spriv->eps[ep_index])
385                 return;
386
387         epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
388         if (!epriv)
389                 return;
390
391         snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
392         epriv->root = xhci_debugfs_create_ring_dir(xhci,
393                                                    dev->eps[ep_index].new_ring,
394                                                    epriv->name,
395                                                    spriv->root);
396         spriv->eps[ep_index] = epriv;
397 }
398
399 void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
400                                   struct xhci_virt_device *dev,
401                                   int ep_index)
402 {
403         struct xhci_ep_priv     *epriv;
404         struct xhci_slot_priv   *spriv = dev->debugfs_private;
405
406         if (!spriv || !spriv->eps[ep_index])
407                 return;
408
409         epriv = spriv->eps[ep_index];
410         debugfs_remove_recursive(epriv->root);
411         spriv->eps[ep_index] = NULL;
412         kfree(epriv);
413 }
414
415 void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
416 {
417         struct xhci_slot_priv   *priv;
418         struct xhci_virt_device *dev = xhci->devs[slot_id];
419
420         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
421         if (!priv)
422                 return;
423
424         snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
425         priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
426         priv->dev = dev;
427         dev->debugfs_private = priv;
428
429         xhci_debugfs_create_ring_dir(xhci, dev->eps[0].ring,
430                                      "ep00", priv->root);
431
432         xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
433 }
434
435 void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
436 {
437         int                     i;
438         struct xhci_slot_priv   *priv;
439         struct xhci_virt_device *dev = xhci->devs[slot_id];
440
441         if (!dev || !dev->debugfs_private)
442                 return;
443
444         priv = dev->debugfs_private;
445
446         debugfs_remove_recursive(priv->root);
447
448         for (i = 0; i < 31; i++)
449                 kfree(priv->eps[i]);
450
451         kfree(priv);
452         dev->debugfs_private = NULL;
453 }
454
455 void xhci_debugfs_init(struct xhci_hcd *xhci)
456 {
457         struct device           *dev = xhci_to_hcd(xhci)->self.controller;
458
459         xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
460                                                 xhci_debugfs_root);
461
462         INIT_LIST_HEAD(&xhci->regset_list);
463
464         xhci_debugfs_regset(xhci,
465                             0,
466                             xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
467                             xhci->debugfs_root, "reg-cap");
468
469         xhci_debugfs_regset(xhci,
470                             HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
471                             xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
472                             xhci->debugfs_root, "reg-op");
473
474         xhci_debugfs_regset(xhci,
475                             readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
476                             xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
477                             xhci->debugfs_root, "reg-runtime");
478
479         xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
480                                    xhci_extcap_legsup,
481                                    ARRAY_SIZE(xhci_extcap_legsup),
482                                    "reg-ext-legsup");
483
484         xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
485                                    xhci_extcap_protocol,
486                                    ARRAY_SIZE(xhci_extcap_protocol),
487                                    "reg-ext-protocol");
488
489         xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
490                                    xhci_extcap_dbc,
491                                    ARRAY_SIZE(xhci_extcap_dbc),
492                                    "reg-ext-dbc");
493
494         xhci_debugfs_create_ring_dir(xhci, xhci->cmd_ring,
495                                      "command-ring",
496                                      xhci->debugfs_root);
497
498         xhci_debugfs_create_ring_dir(xhci, xhci->event_ring,
499                                      "event-ring",
500                                      xhci->debugfs_root);
501
502         xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
503 }
504
505 void xhci_debugfs_exit(struct xhci_hcd *xhci)
506 {
507         struct xhci_regset      *rgs, *tmp;
508
509         debugfs_remove_recursive(xhci->debugfs_root);
510         xhci->debugfs_root = NULL;
511         xhci->debugfs_slots = NULL;
512
513         list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
514                 xhci_debugfs_free_regset(rgs);
515 }
516
517 void __init xhci_debugfs_create_root(void)
518 {
519         xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
520 }
521
522 void __exit xhci_debugfs_remove_root(void)
523 {
524         debugfs_remove_recursive(xhci_debugfs_root);
525         xhci_debugfs_root = NULL;
526 }