xhci-debugfs.c 15 KB

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