xhci-debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // 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. #include <linux/slab.h>
  10. #include "xhci.h"
  11. #include "xhci-debugfs.h"
  12. static const struct debugfs_reg32 xhci_cap_regs[] = {
  13. dump_register(CAPLENGTH),
  14. dump_register(HCSPARAMS1),
  15. dump_register(HCSPARAMS2),
  16. dump_register(HCSPARAMS3),
  17. dump_register(HCCPARAMS1),
  18. dump_register(DOORBELLOFF),
  19. dump_register(RUNTIMEOFF),
  20. dump_register(HCCPARAMS2),
  21. };
  22. static const struct debugfs_reg32 xhci_op_regs[] = {
  23. dump_register(USBCMD),
  24. dump_register(USBSTS),
  25. dump_register(PAGESIZE),
  26. dump_register(DNCTRL),
  27. dump_register(CRCR),
  28. dump_register(DCBAAP_LOW),
  29. dump_register(DCBAAP_HIGH),
  30. dump_register(CONFIG),
  31. };
  32. static const struct debugfs_reg32 xhci_runtime_regs[] = {
  33. dump_register(MFINDEX),
  34. dump_register(IR0_IMAN),
  35. dump_register(IR0_IMOD),
  36. dump_register(IR0_ERSTSZ),
  37. dump_register(IR0_ERSTBA_LOW),
  38. dump_register(IR0_ERSTBA_HIGH),
  39. dump_register(IR0_ERDP_LOW),
  40. dump_register(IR0_ERDP_HIGH),
  41. };
  42. static const struct debugfs_reg32 xhci_extcap_legsup[] = {
  43. dump_register(EXTCAP_USBLEGSUP),
  44. dump_register(EXTCAP_USBLEGCTLSTS),
  45. };
  46. static const struct debugfs_reg32 xhci_extcap_protocol[] = {
  47. dump_register(EXTCAP_REVISION),
  48. dump_register(EXTCAP_NAME),
  49. dump_register(EXTCAP_PORTINFO),
  50. dump_register(EXTCAP_PORTTYPE),
  51. dump_register(EXTCAP_MANTISSA1),
  52. dump_register(EXTCAP_MANTISSA2),
  53. dump_register(EXTCAP_MANTISSA3),
  54. dump_register(EXTCAP_MANTISSA4),
  55. dump_register(EXTCAP_MANTISSA5),
  56. dump_register(EXTCAP_MANTISSA6),
  57. };
  58. static const struct debugfs_reg32 xhci_extcap_dbc[] = {
  59. dump_register(EXTCAP_DBC_CAPABILITY),
  60. dump_register(EXTCAP_DBC_DOORBELL),
  61. dump_register(EXTCAP_DBC_ERSTSIZE),
  62. dump_register(EXTCAP_DBC_ERST_LOW),
  63. dump_register(EXTCAP_DBC_ERST_HIGH),
  64. dump_register(EXTCAP_DBC_ERDP_LOW),
  65. dump_register(EXTCAP_DBC_ERDP_HIGH),
  66. dump_register(EXTCAP_DBC_CONTROL),
  67. dump_register(EXTCAP_DBC_STATUS),
  68. dump_register(EXTCAP_DBC_PORTSC),
  69. dump_register(EXTCAP_DBC_CONT_LOW),
  70. dump_register(EXTCAP_DBC_CONT_HIGH),
  71. dump_register(EXTCAP_DBC_DEVINFO1),
  72. dump_register(EXTCAP_DBC_DEVINFO2),
  73. };
  74. static struct dentry *xhci_debugfs_root;
  75. static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
  76. {
  77. struct xhci_regset *regset;
  78. regset = kzalloc(sizeof(*regset), GFP_KERNEL);
  79. if (!regset)
  80. return NULL;
  81. /*
  82. * The allocation and free of regset are executed in order.
  83. * We needn't a lock here.
  84. */
  85. INIT_LIST_HEAD(&regset->list);
  86. list_add_tail(&regset->list, &xhci->regset_list);
  87. return regset;
  88. }
  89. static void xhci_debugfs_free_regset(struct xhci_regset *regset)
  90. {
  91. if (!regset)
  92. return;
  93. list_del(&regset->list);
  94. kfree(regset);
  95. }
  96. static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
  97. const struct debugfs_reg32 *regs,
  98. size_t nregs, struct dentry *parent,
  99. const char *fmt, ...)
  100. {
  101. struct xhci_regset *rgs;
  102. va_list args;
  103. struct debugfs_regset32 *regset;
  104. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  105. rgs = xhci_debugfs_alloc_regset(xhci);
  106. if (!rgs)
  107. return;
  108. va_start(args, fmt);
  109. vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
  110. va_end(args);
  111. regset = &rgs->regset;
  112. regset->regs = regs;
  113. regset->nregs = nregs;
  114. regset->base = hcd->regs + base;
  115. debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
  116. }
  117. static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
  118. const struct debugfs_reg32 *regs,
  119. size_t n, const char *cap_name)
  120. {
  121. u32 offset;
  122. int index = 0;
  123. size_t psic, nregs = n;
  124. void __iomem *base = &xhci->cap_regs->hc_capbase;
  125. offset = xhci_find_next_ext_cap(base, 0, cap_id);
  126. while (offset) {
  127. if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
  128. psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
  129. nregs = min(4 + psic, n);
  130. }
  131. xhci_debugfs_regset(xhci, offset, regs, nregs,
  132. xhci->debugfs_root, "%s:%02d",
  133. cap_name, index);
  134. offset = xhci_find_next_ext_cap(base, offset, cap_id);
  135. index++;
  136. }
  137. }
  138. static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
  139. {
  140. dma_addr_t dma;
  141. struct xhci_ring *ring = *(struct xhci_ring **)s->private;
  142. dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  143. seq_printf(s, "%pad\n", &dma);
  144. return 0;
  145. }
  146. static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
  147. {
  148. dma_addr_t dma;
  149. struct xhci_ring *ring = *(struct xhci_ring **)s->private;
  150. dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  151. seq_printf(s, "%pad\n", &dma);
  152. return 0;
  153. }
  154. static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
  155. {
  156. struct xhci_ring *ring = *(struct xhci_ring **)s->private;
  157. seq_printf(s, "%d\n", ring->cycle_state);
  158. return 0;
  159. }
  160. static void xhci_ring_dump_segment(struct seq_file *s,
  161. struct xhci_segment *seg)
  162. {
  163. int i;
  164. dma_addr_t dma;
  165. union xhci_trb *trb;
  166. for (i = 0; i < TRBS_PER_SEGMENT; i++) {
  167. trb = &seg->trbs[i];
  168. dma = seg->dma + i * sizeof(*trb);
  169. seq_printf(s, "%pad: %s\n", &dma,
  170. xhci_decode_trb(trb->generic.field[0],
  171. trb->generic.field[1],
  172. trb->generic.field[2],
  173. trb->generic.field[3]));
  174. }
  175. }
  176. static int xhci_ring_trb_show(struct seq_file *s, void *unused)
  177. {
  178. int i;
  179. struct xhci_ring *ring = s->private;
  180. struct xhci_segment *seg = ring->first_seg;
  181. for (i = 0; i < ring->num_segs; i++) {
  182. xhci_ring_dump_segment(s, seg);
  183. seg = seg->next;
  184. }
  185. return 0;
  186. }
  187. static struct xhci_file_map ring_files[] = {
  188. {"enqueue", xhci_ring_enqueue_show, },
  189. {"dequeue", xhci_ring_dequeue_show, },
  190. {"cycle", xhci_ring_cycle_show, },
  191. {"trbs", xhci_ring_trb_show, },
  192. };
  193. static int xhci_ring_open(struct inode *inode, struct file *file)
  194. {
  195. int i;
  196. struct xhci_file_map *f_map;
  197. const char *file_name = file_dentry(file)->d_iname;
  198. for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
  199. f_map = &ring_files[i];
  200. if (strcmp(f_map->name, file_name) == 0)
  201. break;
  202. }
  203. return single_open(file, f_map->show, inode->i_private);
  204. }
  205. static const struct file_operations xhci_ring_fops = {
  206. .open = xhci_ring_open,
  207. .read = seq_read,
  208. .llseek = seq_lseek,
  209. .release = single_release,
  210. };
  211. static int xhci_slot_context_show(struct seq_file *s, void *unused)
  212. {
  213. struct xhci_hcd *xhci;
  214. struct xhci_slot_ctx *slot_ctx;
  215. struct xhci_slot_priv *priv = s->private;
  216. struct xhci_virt_device *dev = priv->dev;
  217. xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
  218. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  219. seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
  220. xhci_decode_slot_context(slot_ctx->dev_info,
  221. slot_ctx->dev_info2,
  222. slot_ctx->tt_info,
  223. slot_ctx->dev_state));
  224. return 0;
  225. }
  226. static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
  227. {
  228. int dci;
  229. dma_addr_t dma;
  230. struct xhci_hcd *xhci;
  231. struct xhci_ep_ctx *ep_ctx;
  232. struct xhci_slot_priv *priv = s->private;
  233. struct xhci_virt_device *dev = priv->dev;
  234. xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
  235. for (dci = 1; dci < 32; dci++) {
  236. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
  237. dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
  238. seq_printf(s, "%pad: %s\n", &dma,
  239. xhci_decode_ep_context(ep_ctx->ep_info,
  240. ep_ctx->ep_info2,
  241. ep_ctx->deq,
  242. ep_ctx->tx_info));
  243. }
  244. return 0;
  245. }
  246. static int xhci_device_name_show(struct seq_file *s, void *unused)
  247. {
  248. struct xhci_slot_priv *priv = s->private;
  249. struct xhci_virt_device *dev = priv->dev;
  250. seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
  251. return 0;
  252. }
  253. static struct xhci_file_map context_files[] = {
  254. {"name", xhci_device_name_show, },
  255. {"slot-context", xhci_slot_context_show, },
  256. {"ep-context", xhci_endpoint_context_show, },
  257. };
  258. static int xhci_context_open(struct inode *inode, struct file *file)
  259. {
  260. int i;
  261. struct xhci_file_map *f_map;
  262. const char *file_name = file_dentry(file)->d_iname;
  263. for (i = 0; i < ARRAY_SIZE(context_files); i++) {
  264. f_map = &context_files[i];
  265. if (strcmp(f_map->name, file_name) == 0)
  266. break;
  267. }
  268. return single_open(file, f_map->show, inode->i_private);
  269. }
  270. static const struct file_operations xhci_context_fops = {
  271. .open = xhci_context_open,
  272. .read = seq_read,
  273. .llseek = seq_lseek,
  274. .release = single_release,
  275. };
  276. static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
  277. struct xhci_file_map *files,
  278. size_t nentries, void *data,
  279. struct dentry *parent,
  280. const struct file_operations *fops)
  281. {
  282. int i;
  283. for (i = 0; i < nentries; i++)
  284. debugfs_create_file(files[i].name, 0444, parent, data, fops);
  285. }
  286. static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
  287. struct xhci_ring **ring,
  288. const char *name,
  289. struct dentry *parent)
  290. {
  291. struct dentry *dir;
  292. dir = debugfs_create_dir(name, parent);
  293. xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
  294. ring, dir, &xhci_ring_fops);
  295. return dir;
  296. }
  297. static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
  298. struct dentry *parent,
  299. int slot_id)
  300. {
  301. struct xhci_virt_device *dev = xhci->devs[slot_id];
  302. xhci_debugfs_create_files(xhci, context_files,
  303. ARRAY_SIZE(context_files),
  304. dev->debugfs_private,
  305. parent, &xhci_context_fops);
  306. }
  307. void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
  308. struct xhci_virt_device *dev,
  309. int ep_index)
  310. {
  311. struct xhci_ep_priv *epriv;
  312. struct xhci_slot_priv *spriv = dev->debugfs_private;
  313. if (spriv->eps[ep_index])
  314. return;
  315. epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
  316. if (!epriv)
  317. return;
  318. snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
  319. epriv->root = xhci_debugfs_create_ring_dir(xhci,
  320. &dev->eps[ep_index].new_ring,
  321. epriv->name,
  322. spriv->root);
  323. spriv->eps[ep_index] = epriv;
  324. }
  325. void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
  326. struct xhci_virt_device *dev,
  327. int ep_index)
  328. {
  329. struct xhci_ep_priv *epriv;
  330. struct xhci_slot_priv *spriv = dev->debugfs_private;
  331. if (!spriv || !spriv->eps[ep_index])
  332. return;
  333. epriv = spriv->eps[ep_index];
  334. debugfs_remove_recursive(epriv->root);
  335. spriv->eps[ep_index] = NULL;
  336. kfree(epriv);
  337. }
  338. void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
  339. {
  340. struct xhci_slot_priv *priv;
  341. struct xhci_virt_device *dev = xhci->devs[slot_id];
  342. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  343. if (!priv)
  344. return;
  345. snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
  346. priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
  347. priv->dev = dev;
  348. dev->debugfs_private = priv;
  349. xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
  350. "ep00", priv->root);
  351. xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
  352. }
  353. void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
  354. {
  355. int i;
  356. struct xhci_slot_priv *priv;
  357. struct xhci_virt_device *dev = xhci->devs[slot_id];
  358. if (!dev || !dev->debugfs_private)
  359. return;
  360. priv = dev->debugfs_private;
  361. debugfs_remove_recursive(priv->root);
  362. for (i = 0; i < 31; i++)
  363. kfree(priv->eps[i]);
  364. kfree(priv);
  365. dev->debugfs_private = NULL;
  366. }
  367. void xhci_debugfs_init(struct xhci_hcd *xhci)
  368. {
  369. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  370. xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
  371. xhci_debugfs_root);
  372. INIT_LIST_HEAD(&xhci->regset_list);
  373. xhci_debugfs_regset(xhci,
  374. 0,
  375. xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
  376. xhci->debugfs_root, "reg-cap");
  377. xhci_debugfs_regset(xhci,
  378. HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
  379. xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
  380. xhci->debugfs_root, "reg-op");
  381. xhci_debugfs_regset(xhci,
  382. readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
  383. xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
  384. xhci->debugfs_root, "reg-runtime");
  385. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
  386. xhci_extcap_legsup,
  387. ARRAY_SIZE(xhci_extcap_legsup),
  388. "reg-ext-legsup");
  389. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
  390. xhci_extcap_protocol,
  391. ARRAY_SIZE(xhci_extcap_protocol),
  392. "reg-ext-protocol");
  393. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
  394. xhci_extcap_dbc,
  395. ARRAY_SIZE(xhci_extcap_dbc),
  396. "reg-ext-dbc");
  397. xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
  398. "command-ring",
  399. xhci->debugfs_root);
  400. xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
  401. "event-ring",
  402. xhci->debugfs_root);
  403. xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
  404. }
  405. void xhci_debugfs_exit(struct xhci_hcd *xhci)
  406. {
  407. struct xhci_regset *rgs, *tmp;
  408. debugfs_remove_recursive(xhci->debugfs_root);
  409. xhci->debugfs_root = NULL;
  410. xhci->debugfs_slots = NULL;
  411. list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
  412. xhci_debugfs_free_regset(rgs);
  413. }
  414. void __init xhci_debugfs_create_root(void)
  415. {
  416. xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
  417. }
  418. void __exit xhci_debugfs_remove_root(void)
  419. {
  420. debugfs_remove_recursive(xhci_debugfs_root);
  421. xhci_debugfs_root = NULL;
  422. }