xhci-debugfs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. for (i = 0; i < TRBS_PER_SEGMENT; i++) {
  168. trb = &seg->trbs[i];
  169. dma = seg->dma + i * sizeof(*trb);
  170. seq_printf(s, "%pad: %s\n", &dma,
  171. xhci_decode_trb(le32_to_cpu(trb->generic.field[0]),
  172. le32_to_cpu(trb->generic.field[1]),
  173. le32_to_cpu(trb->generic.field[2]),
  174. le32_to_cpu(trb->generic.field[3])));
  175. }
  176. }
  177. static int xhci_ring_trb_show(struct seq_file *s, void *unused)
  178. {
  179. int i;
  180. struct xhci_ring *ring = *(struct xhci_ring **)s->private;
  181. struct xhci_segment *seg = ring->first_seg;
  182. for (i = 0; i < ring->num_segs; i++) {
  183. xhci_ring_dump_segment(s, seg);
  184. seg = seg->next;
  185. }
  186. return 0;
  187. }
  188. static struct xhci_file_map ring_files[] = {
  189. {"enqueue", xhci_ring_enqueue_show, },
  190. {"dequeue", xhci_ring_dequeue_show, },
  191. {"cycle", xhci_ring_cycle_show, },
  192. {"trbs", xhci_ring_trb_show, },
  193. };
  194. static int xhci_ring_open(struct inode *inode, struct file *file)
  195. {
  196. int i;
  197. struct xhci_file_map *f_map;
  198. const char *file_name = file_dentry(file)->d_iname;
  199. for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
  200. f_map = &ring_files[i];
  201. if (strcmp(f_map->name, file_name) == 0)
  202. break;
  203. }
  204. return single_open(file, f_map->show, inode->i_private);
  205. }
  206. static const struct file_operations xhci_ring_fops = {
  207. .open = xhci_ring_open,
  208. .read = seq_read,
  209. .llseek = seq_lseek,
  210. .release = single_release,
  211. };
  212. static int xhci_slot_context_show(struct seq_file *s, void *unused)
  213. {
  214. struct xhci_hcd *xhci;
  215. struct xhci_slot_ctx *slot_ctx;
  216. struct xhci_slot_priv *priv = s->private;
  217. struct xhci_virt_device *dev = priv->dev;
  218. xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
  219. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  220. seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
  221. xhci_decode_slot_context(le32_to_cpu(slot_ctx->dev_info),
  222. le32_to_cpu(slot_ctx->dev_info2),
  223. le32_to_cpu(slot_ctx->tt_info),
  224. le32_to_cpu(slot_ctx->dev_state)));
  225. return 0;
  226. }
  227. static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
  228. {
  229. int dci;
  230. dma_addr_t dma;
  231. struct xhci_hcd *xhci;
  232. struct xhci_ep_ctx *ep_ctx;
  233. struct xhci_slot_priv *priv = s->private;
  234. struct xhci_virt_device *dev = priv->dev;
  235. xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
  236. for (dci = 1; dci < 32; dci++) {
  237. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
  238. dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
  239. seq_printf(s, "%pad: %s\n", &dma,
  240. xhci_decode_ep_context(le32_to_cpu(ep_ctx->ep_info),
  241. le32_to_cpu(ep_ctx->ep_info2),
  242. le64_to_cpu(ep_ctx->deq),
  243. le32_to_cpu(ep_ctx->tx_info)));
  244. }
  245. return 0;
  246. }
  247. static int xhci_device_name_show(struct seq_file *s, void *unused)
  248. {
  249. struct xhci_slot_priv *priv = s->private;
  250. struct xhci_virt_device *dev = priv->dev;
  251. seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
  252. return 0;
  253. }
  254. static struct xhci_file_map context_files[] = {
  255. {"name", xhci_device_name_show, },
  256. {"slot-context", xhci_slot_context_show, },
  257. {"ep-context", xhci_endpoint_context_show, },
  258. };
  259. static int xhci_context_open(struct inode *inode, struct file *file)
  260. {
  261. int i;
  262. struct xhci_file_map *f_map;
  263. const char *file_name = file_dentry(file)->d_iname;
  264. for (i = 0; i < ARRAY_SIZE(context_files); i++) {
  265. f_map = &context_files[i];
  266. if (strcmp(f_map->name, file_name) == 0)
  267. break;
  268. }
  269. return single_open(file, f_map->show, inode->i_private);
  270. }
  271. static const struct file_operations xhci_context_fops = {
  272. .open = xhci_context_open,
  273. .read = seq_read,
  274. .llseek = seq_lseek,
  275. .release = single_release,
  276. };
  277. static int xhci_portsc_show(struct seq_file *s, void *unused)
  278. {
  279. struct xhci_port *port = s->private;
  280. u32 portsc;
  281. portsc = readl(port->addr);
  282. seq_printf(s, "%s\n", xhci_decode_portsc(portsc));
  283. return 0;
  284. }
  285. static int xhci_port_open(struct inode *inode, struct file *file)
  286. {
  287. return single_open(file, xhci_portsc_show, inode->i_private);
  288. }
  289. static ssize_t xhci_port_write(struct file *file, const char __user *ubuf,
  290. size_t count, loff_t *ppos)
  291. {
  292. struct seq_file *s = file->private_data;
  293. struct xhci_port *port = s->private;
  294. struct xhci_hcd *xhci = hcd_to_xhci(port->rhub->hcd);
  295. char buf[32];
  296. u32 portsc;
  297. unsigned long flags;
  298. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  299. return -EFAULT;
  300. if (!strncmp(buf, "compliance", 10)) {
  301. /* If CTC is clear, compliance is enabled by default */
  302. if (!HCC2_CTC(xhci->hcc_params2))
  303. return count;
  304. spin_lock_irqsave(&xhci->lock, flags);
  305. /* compliance mode can only be enabled on ports in RxDetect */
  306. portsc = readl(port->addr);
  307. if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
  308. spin_unlock_irqrestore(&xhci->lock, flags);
  309. return -EPERM;
  310. }
  311. portsc = xhci_port_state_to_neutral(portsc);
  312. portsc &= ~PORT_PLS_MASK;
  313. portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
  314. writel(portsc, port->addr);
  315. spin_unlock_irqrestore(&xhci->lock, flags);
  316. } else {
  317. return -EINVAL;
  318. }
  319. return count;
  320. }
  321. static const struct file_operations port_fops = {
  322. .open = xhci_port_open,
  323. .write = xhci_port_write,
  324. .read = seq_read,
  325. .llseek = seq_lseek,
  326. .release = single_release,
  327. };
  328. static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
  329. struct xhci_file_map *files,
  330. size_t nentries, void *data,
  331. struct dentry *parent,
  332. const struct file_operations *fops)
  333. {
  334. int i;
  335. for (i = 0; i < nentries; i++)
  336. debugfs_create_file(files[i].name, 0444, parent, data, fops);
  337. }
  338. static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
  339. struct xhci_ring **ring,
  340. const char *name,
  341. struct dentry *parent)
  342. {
  343. struct dentry *dir;
  344. dir = debugfs_create_dir(name, parent);
  345. xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
  346. ring, dir, &xhci_ring_fops);
  347. return dir;
  348. }
  349. static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
  350. struct dentry *parent,
  351. int slot_id)
  352. {
  353. struct xhci_virt_device *dev = xhci->devs[slot_id];
  354. xhci_debugfs_create_files(xhci, context_files,
  355. ARRAY_SIZE(context_files),
  356. dev->debugfs_private,
  357. parent, &xhci_context_fops);
  358. }
  359. void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
  360. struct xhci_virt_device *dev,
  361. int ep_index)
  362. {
  363. struct xhci_ep_priv *epriv;
  364. struct xhci_slot_priv *spriv = dev->debugfs_private;
  365. if (!spriv)
  366. return;
  367. if (spriv->eps[ep_index])
  368. return;
  369. epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
  370. if (!epriv)
  371. return;
  372. snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
  373. epriv->root = xhci_debugfs_create_ring_dir(xhci,
  374. &dev->eps[ep_index].ring,
  375. epriv->name,
  376. spriv->root);
  377. spriv->eps[ep_index] = epriv;
  378. }
  379. void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
  380. struct xhci_virt_device *dev,
  381. int ep_index)
  382. {
  383. struct xhci_ep_priv *epriv;
  384. struct xhci_slot_priv *spriv = dev->debugfs_private;
  385. if (!spriv || !spriv->eps[ep_index])
  386. return;
  387. epriv = spriv->eps[ep_index];
  388. debugfs_remove_recursive(epriv->root);
  389. spriv->eps[ep_index] = NULL;
  390. kfree(epriv);
  391. }
  392. void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
  393. {
  394. struct xhci_slot_priv *priv;
  395. struct xhci_virt_device *dev = xhci->devs[slot_id];
  396. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  397. if (!priv)
  398. return;
  399. snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
  400. priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
  401. priv->dev = dev;
  402. dev->debugfs_private = priv;
  403. xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
  404. "ep00", priv->root);
  405. xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
  406. }
  407. void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
  408. {
  409. int i;
  410. struct xhci_slot_priv *priv;
  411. struct xhci_virt_device *dev = xhci->devs[slot_id];
  412. if (!dev || !dev->debugfs_private)
  413. return;
  414. priv = dev->debugfs_private;
  415. debugfs_remove_recursive(priv->root);
  416. for (i = 0; i < 31; i++)
  417. kfree(priv->eps[i]);
  418. kfree(priv);
  419. dev->debugfs_private = NULL;
  420. }
  421. static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
  422. struct dentry *parent)
  423. {
  424. unsigned int num_ports;
  425. char port_name[8];
  426. struct xhci_port *port;
  427. struct dentry *dir;
  428. num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  429. parent = debugfs_create_dir("ports", parent);
  430. while (num_ports--) {
  431. scnprintf(port_name, sizeof(port_name), "port%02d",
  432. num_ports + 1);
  433. dir = debugfs_create_dir(port_name, parent);
  434. port = &xhci->hw_ports[num_ports];
  435. debugfs_create_file("portsc", 0644, dir, port, &port_fops);
  436. }
  437. }
  438. void xhci_debugfs_init(struct xhci_hcd *xhci)
  439. {
  440. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  441. xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
  442. xhci_debugfs_root);
  443. INIT_LIST_HEAD(&xhci->regset_list);
  444. xhci_debugfs_regset(xhci,
  445. 0,
  446. xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
  447. xhci->debugfs_root, "reg-cap");
  448. xhci_debugfs_regset(xhci,
  449. HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
  450. xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
  451. xhci->debugfs_root, "reg-op");
  452. xhci_debugfs_regset(xhci,
  453. readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
  454. xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
  455. xhci->debugfs_root, "reg-runtime");
  456. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
  457. xhci_extcap_legsup,
  458. ARRAY_SIZE(xhci_extcap_legsup),
  459. "reg-ext-legsup");
  460. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
  461. xhci_extcap_protocol,
  462. ARRAY_SIZE(xhci_extcap_protocol),
  463. "reg-ext-protocol");
  464. xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
  465. xhci_extcap_dbc,
  466. ARRAY_SIZE(xhci_extcap_dbc),
  467. "reg-ext-dbc");
  468. xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
  469. "command-ring",
  470. xhci->debugfs_root);
  471. xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
  472. "event-ring",
  473. xhci->debugfs_root);
  474. xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
  475. xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
  476. }
  477. void xhci_debugfs_exit(struct xhci_hcd *xhci)
  478. {
  479. struct xhci_regset *rgs, *tmp;
  480. debugfs_remove_recursive(xhci->debugfs_root);
  481. xhci->debugfs_root = NULL;
  482. xhci->debugfs_slots = NULL;
  483. list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
  484. xhci_debugfs_free_regset(rgs);
  485. }
  486. void __init xhci_debugfs_create_root(void)
  487. {
  488. xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
  489. }
  490. void __exit xhci_debugfs_remove_root(void)
  491. {
  492. debugfs_remove_recursive(xhci_debugfs_root);
  493. xhci_debugfs_root = NULL;
  494. }