Linux Audio

Check our new training course

Loading...
v6.8
  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
 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
113__printf(6, 7)
114static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
115				const struct debugfs_reg32 *regs,
116				size_t nregs, struct dentry *parent,
117				const char *fmt, ...)
118{
119	struct xhci_regset	*rgs;
120	va_list			args;
121	struct debugfs_regset32	*regset;
122	struct usb_hcd		*hcd = xhci_to_hcd(xhci);
123
124	rgs = xhci_debugfs_alloc_regset(xhci);
125	if (!rgs)
126		return;
127
128	va_start(args, fmt);
129	vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
130	va_end(args);
131
132	regset = &rgs->regset;
133	regset->regs = regs;
134	regset->nregs = nregs;
135	regset->base = hcd->regs + base;
136	regset->dev = hcd->self.controller;
137
138	debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
139}
140
141static 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
165static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
166{
167	dma_addr_t		dma;
168	struct xhci_ring	*ring = *(struct xhci_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
176static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
177{
178	dma_addr_t		dma;
179	struct xhci_ring	*ring = *(struct xhci_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
187static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
188{
189	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
190
191	seq_printf(s, "%d\n", ring->cycle_state);
192
193	return 0;
194}
195
196static 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	char			str[XHCI_MSG_MAX];
203
204	for (i = 0; i < TRBS_PER_SEGMENT; i++) {
205		trb = &seg->trbs[i];
206		dma = seg->dma + i * sizeof(*trb);
207		seq_printf(s, "%2u %pad: %s\n", seg->num, &dma,
208			   xhci_decode_trb(str, XHCI_MSG_MAX, le32_to_cpu(trb->generic.field[0]),
209					   le32_to_cpu(trb->generic.field[1]),
210					   le32_to_cpu(trb->generic.field[2]),
211					   le32_to_cpu(trb->generic.field[3])));
212	}
213}
214
215static int xhci_ring_trb_show(struct seq_file *s, void *unused)
216{
217	int			i;
218	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
219	struct xhci_segment	*seg = ring->first_seg;
220
221	for (i = 0; i < ring->num_segs; i++) {
222		xhci_ring_dump_segment(s, seg);
223		seg = seg->next;
224	}
225
226	return 0;
227}
228
229static struct xhci_file_map ring_files[] = {
230	{"enqueue",		xhci_ring_enqueue_show, },
231	{"dequeue",		xhci_ring_dequeue_show, },
232	{"cycle",		xhci_ring_cycle_show, },
233	{"trbs",		xhci_ring_trb_show, },
234};
235
236static int xhci_ring_open(struct inode *inode, struct file *file)
237{
238	int			i;
239	struct xhci_file_map	*f_map;
240	const char		*file_name = file_dentry(file)->d_iname;
241
242	for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
243		f_map = &ring_files[i];
244
245		if (strcmp(f_map->name, file_name) == 0)
246			break;
247	}
248
249	return single_open(file, f_map->show, inode->i_private);
250}
251
252static const struct file_operations xhci_ring_fops = {
253	.open			= xhci_ring_open,
254	.read			= seq_read,
255	.llseek			= seq_lseek,
256	.release		= single_release,
257};
258
259static int xhci_slot_context_show(struct seq_file *s, void *unused)
260{
261	struct xhci_hcd		*xhci;
262	struct xhci_slot_ctx	*slot_ctx;
263	struct xhci_slot_priv	*priv = s->private;
264	struct xhci_virt_device	*dev = priv->dev;
265	char			str[XHCI_MSG_MAX];
266
267	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
268	slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
269	seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
270		   xhci_decode_slot_context(str,
271					    le32_to_cpu(slot_ctx->dev_info),
272					    le32_to_cpu(slot_ctx->dev_info2),
273					    le32_to_cpu(slot_ctx->tt_info),
274					    le32_to_cpu(slot_ctx->dev_state)));
275
276	return 0;
277}
278
279static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
280{
281	int			ep_index;
282	dma_addr_t		dma;
283	struct xhci_hcd		*xhci;
284	struct xhci_ep_ctx	*ep_ctx;
285	struct xhci_slot_priv	*priv = s->private;
286	struct xhci_virt_device	*dev = priv->dev;
287	char			str[XHCI_MSG_MAX];
288
289	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
290
291	for (ep_index = 0; ep_index < 31; ep_index++) {
292		ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
293		dma = dev->out_ctx->dma + (ep_index + 1) * CTX_SIZE(xhci->hcc_params);
294		seq_printf(s, "%pad: %s\n", &dma,
295			   xhci_decode_ep_context(str,
296						  le32_to_cpu(ep_ctx->ep_info),
297						  le32_to_cpu(ep_ctx->ep_info2),
298						  le64_to_cpu(ep_ctx->deq),
299						  le32_to_cpu(ep_ctx->tx_info)));
300	}
301
302	return 0;
303}
304
305static int xhci_device_name_show(struct seq_file *s, void *unused)
306{
307	struct xhci_slot_priv	*priv = s->private;
308	struct xhci_virt_device	*dev = priv->dev;
309
310	seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
311
312	return 0;
313}
314
315static struct xhci_file_map context_files[] = {
316	{"name",		xhci_device_name_show, },
317	{"slot-context",	xhci_slot_context_show, },
318	{"ep-context",		xhci_endpoint_context_show, },
319};
320
321static int xhci_context_open(struct inode *inode, struct file *file)
322{
323	int			i;
324	struct xhci_file_map	*f_map;
325	const char		*file_name = file_dentry(file)->d_iname;
326
327	for (i = 0; i < ARRAY_SIZE(context_files); i++) {
328		f_map = &context_files[i];
329
330		if (strcmp(f_map->name, file_name) == 0)
331			break;
332	}
333
334	return single_open(file, f_map->show, inode->i_private);
335}
336
337static const struct file_operations xhci_context_fops = {
338	.open			= xhci_context_open,
339	.read			= seq_read,
340	.llseek			= seq_lseek,
341	.release		= single_release,
342};
343
344
345
346static int xhci_portsc_show(struct seq_file *s, void *unused)
347{
348	struct xhci_port	*port = s->private;
349	u32			portsc;
350	char			str[XHCI_MSG_MAX];
351
352	portsc = readl(port->addr);
353	seq_printf(s, "%s\n", xhci_decode_portsc(str, portsc));
354
355	return 0;
356}
357
358static int xhci_port_open(struct inode *inode, struct file *file)
359{
360	return single_open(file, xhci_portsc_show, inode->i_private);
361}
362
363static ssize_t xhci_port_write(struct file *file,  const char __user *ubuf,
364			       size_t count, loff_t *ppos)
365{
366	struct seq_file         *s = file->private_data;
367	struct xhci_port	*port = s->private;
368	struct xhci_hcd		*xhci = hcd_to_xhci(port->rhub->hcd);
369	char                    buf[32];
370	u32			portsc;
371	unsigned long		flags;
372
373	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
374		return -EFAULT;
375
376	if (!strncmp(buf, "compliance", 10)) {
377		/* If CTC is clear, compliance is enabled by default */
378		if (!HCC2_CTC(xhci->hcc_params2))
379			return count;
380		spin_lock_irqsave(&xhci->lock, flags);
381		/* compliance mode can only be enabled on ports in RxDetect */
382		portsc = readl(port->addr);
383		if ((portsc & PORT_PLS_MASK) != XDEV_RXDETECT) {
384			spin_unlock_irqrestore(&xhci->lock, flags);
385			return -EPERM;
386		}
387		portsc = xhci_port_state_to_neutral(portsc);
388		portsc &= ~PORT_PLS_MASK;
389		portsc |= PORT_LINK_STROBE | XDEV_COMP_MODE;
390		writel(portsc, port->addr);
391		spin_unlock_irqrestore(&xhci->lock, flags);
392	} else {
393		return -EINVAL;
394	}
395	return count;
396}
397
398static const struct file_operations port_fops = {
399	.open			= xhci_port_open,
400	.write                  = xhci_port_write,
401	.read			= seq_read,
402	.llseek			= seq_lseek,
403	.release		= single_release,
404};
405
406static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
407				      struct xhci_file_map *files,
408				      size_t nentries, void *data,
409				      struct dentry *parent,
410				      const struct file_operations *fops)
411{
412	int			i;
413
414	for (i = 0; i < nentries; i++)
415		debugfs_create_file(files[i].name, 0444, parent, data, fops);
416}
417
418static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
419						   struct xhci_ring **ring,
420						   const char *name,
421						   struct dentry *parent)
422{
423	struct dentry		*dir;
424
425	dir = debugfs_create_dir(name, parent);
426	xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
427				  ring, dir, &xhci_ring_fops);
428
429	return dir;
430}
431
432static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
433					      struct dentry *parent,
434					      int slot_id)
435{
436	struct xhci_virt_device	*dev = xhci->devs[slot_id];
437
438	xhci_debugfs_create_files(xhci, context_files,
439				  ARRAY_SIZE(context_files),
440				  dev->debugfs_private,
441				  parent, &xhci_context_fops);
442}
443
444void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
445				  struct xhci_virt_device *dev,
446				  int ep_index)
447{
448	struct xhci_ep_priv	*epriv;
449	struct xhci_slot_priv	*spriv = dev->debugfs_private;
450
451	if (!spriv)
452		return;
453
454	if (spriv->eps[ep_index])
455		return;
456
457	epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
458	if (!epriv)
459		return;
460
461	epriv->show_ring = dev->eps[ep_index].ring;
462
463	snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
464	epriv->root = xhci_debugfs_create_ring_dir(xhci,
465						   &epriv->show_ring,
466						   epriv->name,
467						   spriv->root);
468	spriv->eps[ep_index] = epriv;
469}
470
471void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
472				  struct xhci_virt_device *dev,
473				  int ep_index)
474{
475	struct xhci_ep_priv	*epriv;
476	struct xhci_slot_priv	*spriv = dev->debugfs_private;
477
478	if (!spriv || !spriv->eps[ep_index])
479		return;
480
481	epriv = spriv->eps[ep_index];
482	debugfs_remove_recursive(epriv->root);
483	spriv->eps[ep_index] = NULL;
484	kfree(epriv);
485}
486
487static int xhci_stream_id_show(struct seq_file *s, void *unused)
488{
489	struct xhci_ep_priv	*epriv = s->private;
490
491	if (!epriv->stream_info)
492		return -EPERM;
493
494	seq_printf(s, "Show stream ID %d trb ring, supported [1 - %d]\n",
495		   epriv->stream_id, epriv->stream_info->num_streams - 1);
496
497	return 0;
498}
499
500static int xhci_stream_id_open(struct inode *inode, struct file *file)
501{
502	return single_open(file, xhci_stream_id_show, inode->i_private);
503}
504
505static ssize_t xhci_stream_id_write(struct file *file,  const char __user *ubuf,
506			       size_t count, loff_t *ppos)
507{
508	struct seq_file         *s = file->private_data;
509	struct xhci_ep_priv	*epriv = s->private;
510	int			ret;
511	u16			stream_id; /* MaxPStreams + 1 <= 16 */
512
513	if (!epriv->stream_info)
514		return -EPERM;
515
516	/* Decimal number */
517	ret = kstrtou16_from_user(ubuf, count, 10, &stream_id);
518	if (ret)
519		return ret;
520
521	if (stream_id == 0 || stream_id >= epriv->stream_info->num_streams)
522		return -EINVAL;
523
524	epriv->stream_id = stream_id;
525	epriv->show_ring = epriv->stream_info->stream_rings[stream_id];
526
527	return count;
528}
529
530static const struct file_operations stream_id_fops = {
531	.open			= xhci_stream_id_open,
532	.write                  = xhci_stream_id_write,
533	.read			= seq_read,
534	.llseek			= seq_lseek,
535	.release		= single_release,
536};
537
538static int xhci_stream_context_array_show(struct seq_file *s, void *unused)
539{
540	struct xhci_ep_priv	*epriv = s->private;
541	struct xhci_stream_ctx	*stream_ctx;
542	dma_addr_t		dma;
543	int			id;
544
545	if (!epriv->stream_info)
546		return -EPERM;
547
548	seq_printf(s, "Allocated %d streams and %d stream context array entries\n",
549			epriv->stream_info->num_streams,
550			epriv->stream_info->num_stream_ctxs);
551
552	for (id = 0; id < epriv->stream_info->num_stream_ctxs; id++) {
553		stream_ctx = epriv->stream_info->stream_ctx_array + id;
554		dma = epriv->stream_info->ctx_array_dma + id * 16;
555		if (id < epriv->stream_info->num_streams)
556			seq_printf(s, "%pad stream id %d deq %016llx\n", &dma,
557				   id, le64_to_cpu(stream_ctx->stream_ring));
558		else
559			seq_printf(s, "%pad stream context entry not used deq %016llx\n",
560				   &dma, le64_to_cpu(stream_ctx->stream_ring));
561	}
562
563	return 0;
564}
565DEFINE_SHOW_ATTRIBUTE(xhci_stream_context_array);
566
567void xhci_debugfs_create_stream_files(struct xhci_hcd *xhci,
568				      struct xhci_virt_device *dev,
569				      int ep_index)
570{
571	struct xhci_slot_priv	*spriv = dev->debugfs_private;
572	struct xhci_ep_priv	*epriv;
573
574	if (!spriv || !spriv->eps[ep_index] ||
575	    !dev->eps[ep_index].stream_info)
576		return;
577
578	epriv = spriv->eps[ep_index];
579	epriv->stream_info = dev->eps[ep_index].stream_info;
580
581	/* Show trb ring of stream ID 1 by default */
582	epriv->stream_id = 1;
583	epriv->show_ring = epriv->stream_info->stream_rings[1];
584	debugfs_create_file("stream_id", 0644,
585			    epriv->root, epriv,
586			    &stream_id_fops);
587	debugfs_create_file("stream_context_array", 0444,
588			    epriv->root, epriv,
589			    &xhci_stream_context_array_fops);
590}
591
592void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
593{
594	struct xhci_slot_priv	*priv;
595	struct xhci_virt_device	*dev = xhci->devs[slot_id];
596
597	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
598	if (!priv)
599		return;
600
601	snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
602	priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
603	priv->dev = dev;
604	dev->debugfs_private = priv;
605
606	xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
607				     "ep00", priv->root);
608
609	xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
610}
611
612void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
613{
614	int			i;
615	struct xhci_slot_priv	*priv;
616	struct xhci_virt_device	*dev = xhci->devs[slot_id];
617
618	if (!dev || !dev->debugfs_private)
619		return;
620
621	priv = dev->debugfs_private;
622
623	debugfs_remove_recursive(priv->root);
624
625	for (i = 0; i < 31; i++)
626		kfree(priv->eps[i]);
627
628	kfree(priv);
629	dev->debugfs_private = NULL;
630}
631
632static void xhci_debugfs_create_ports(struct xhci_hcd *xhci,
633				      struct dentry *parent)
634{
635	unsigned int		num_ports;
636	char			port_name[8];
637	struct xhci_port	*port;
638	struct dentry		*dir;
639
640	num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
641
642	parent = debugfs_create_dir("ports", parent);
643
644	while (num_ports--) {
645		scnprintf(port_name, sizeof(port_name), "port%02d",
646			  num_ports + 1);
647		dir = debugfs_create_dir(port_name, parent);
648		port = &xhci->hw_ports[num_ports];
649		debugfs_create_file("portsc", 0644, dir, port, &port_fops);
650	}
651}
652
653void xhci_debugfs_init(struct xhci_hcd *xhci)
654{
655	struct device		*dev = xhci_to_hcd(xhci)->self.controller;
656
657	xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
658						xhci_debugfs_root);
659
660	INIT_LIST_HEAD(&xhci->regset_list);
661
662	xhci_debugfs_regset(xhci,
663			    0,
664			    xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
665			    xhci->debugfs_root, "reg-cap");
666
667	xhci_debugfs_regset(xhci,
668			    HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
669			    xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
670			    xhci->debugfs_root, "reg-op");
671
672	xhci_debugfs_regset(xhci,
673			    readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
674			    xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
675			    xhci->debugfs_root, "reg-runtime");
676
677	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
678				   xhci_extcap_legsup,
679				   ARRAY_SIZE(xhci_extcap_legsup),
680				   "reg-ext-legsup");
681
682	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
683				   xhci_extcap_protocol,
684				   ARRAY_SIZE(xhci_extcap_protocol),
685				   "reg-ext-protocol");
686
687	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
688				   xhci_extcap_dbc,
689				   ARRAY_SIZE(xhci_extcap_dbc),
690				   "reg-ext-dbc");
691
692	xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
693				     "command-ring",
694				     xhci->debugfs_root);
695
696	xhci_debugfs_create_ring_dir(xhci, &xhci->interrupters[0]->event_ring,
697				     "event-ring",
698				     xhci->debugfs_root);
699
700	xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
701
702	xhci_debugfs_create_ports(xhci, xhci->debugfs_root);
703}
704
705void xhci_debugfs_exit(struct xhci_hcd *xhci)
706{
707	struct xhci_regset	*rgs, *tmp;
708
709	debugfs_remove_recursive(xhci->debugfs_root);
710	xhci->debugfs_root = NULL;
711	xhci->debugfs_slots = NULL;
712
713	list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
714		xhci_debugfs_free_regset(rgs);
715}
716
717void __init xhci_debugfs_create_root(void)
718{
719	xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
720}
721
722void __exit xhci_debugfs_remove_root(void)
723{
724	debugfs_remove_recursive(xhci_debugfs_root);
725	xhci_debugfs_root = NULL;
726}
v4.17
  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
 10#include <linux/slab.h>
 
 11
 12#include "xhci.h"
 13#include "xhci-debugfs.h"
 14
 15static const struct debugfs_reg32 xhci_cap_regs[] = {
 16	dump_register(CAPLENGTH),
 17	dump_register(HCSPARAMS1),
 18	dump_register(HCSPARAMS2),
 19	dump_register(HCSPARAMS3),
 20	dump_register(HCCPARAMS1),
 21	dump_register(DOORBELLOFF),
 22	dump_register(RUNTIMEOFF),
 23	dump_register(HCCPARAMS2),
 24};
 25
 26static const struct debugfs_reg32 xhci_op_regs[] = {
 27	dump_register(USBCMD),
 28	dump_register(USBSTS),
 29	dump_register(PAGESIZE),
 30	dump_register(DNCTRL),
 31	dump_register(CRCR),
 32	dump_register(DCBAAP_LOW),
 33	dump_register(DCBAAP_HIGH),
 34	dump_register(CONFIG),
 35};
 36
 37static const struct debugfs_reg32 xhci_runtime_regs[] = {
 38	dump_register(MFINDEX),
 39	dump_register(IR0_IMAN),
 40	dump_register(IR0_IMOD),
 41	dump_register(IR0_ERSTSZ),
 42	dump_register(IR0_ERSTBA_LOW),
 43	dump_register(IR0_ERSTBA_HIGH),
 44	dump_register(IR0_ERDP_LOW),
 45	dump_register(IR0_ERDP_HIGH),
 46};
 47
 48static const struct debugfs_reg32 xhci_extcap_legsup[] = {
 49	dump_register(EXTCAP_USBLEGSUP),
 50	dump_register(EXTCAP_USBLEGCTLSTS),
 51};
 52
 53static const struct debugfs_reg32 xhci_extcap_protocol[] = {
 54	dump_register(EXTCAP_REVISION),
 55	dump_register(EXTCAP_NAME),
 56	dump_register(EXTCAP_PORTINFO),
 57	dump_register(EXTCAP_PORTTYPE),
 58	dump_register(EXTCAP_MANTISSA1),
 59	dump_register(EXTCAP_MANTISSA2),
 60	dump_register(EXTCAP_MANTISSA3),
 61	dump_register(EXTCAP_MANTISSA4),
 62	dump_register(EXTCAP_MANTISSA5),
 63	dump_register(EXTCAP_MANTISSA6),
 64};
 65
 66static const struct debugfs_reg32 xhci_extcap_dbc[] = {
 67	dump_register(EXTCAP_DBC_CAPABILITY),
 68	dump_register(EXTCAP_DBC_DOORBELL),
 69	dump_register(EXTCAP_DBC_ERSTSIZE),
 70	dump_register(EXTCAP_DBC_ERST_LOW),
 71	dump_register(EXTCAP_DBC_ERST_HIGH),
 72	dump_register(EXTCAP_DBC_ERDP_LOW),
 73	dump_register(EXTCAP_DBC_ERDP_HIGH),
 74	dump_register(EXTCAP_DBC_CONTROL),
 75	dump_register(EXTCAP_DBC_STATUS),
 76	dump_register(EXTCAP_DBC_PORTSC),
 77	dump_register(EXTCAP_DBC_CONT_LOW),
 78	dump_register(EXTCAP_DBC_CONT_HIGH),
 79	dump_register(EXTCAP_DBC_DEVINFO1),
 80	dump_register(EXTCAP_DBC_DEVINFO2),
 81};
 82
 83static struct dentry *xhci_debugfs_root;
 84
 85static struct xhci_regset *xhci_debugfs_alloc_regset(struct xhci_hcd *xhci)
 86{
 87	struct xhci_regset	*regset;
 88
 89	regset = kzalloc(sizeof(*regset), GFP_KERNEL);
 90	if (!regset)
 91		return NULL;
 92
 93	/*
 94	 * The allocation and free of regset are executed in order.
 95	 * We needn't a lock here.
 96	 */
 97	INIT_LIST_HEAD(&regset->list);
 98	list_add_tail(&regset->list, &xhci->regset_list);
 99
100	return regset;
101}
102
103static void xhci_debugfs_free_regset(struct xhci_regset *regset)
104{
105	if (!regset)
106		return;
107
108	list_del(&regset->list);
109	kfree(regset);
110}
111
 
112static void xhci_debugfs_regset(struct xhci_hcd *xhci, u32 base,
113				const struct debugfs_reg32 *regs,
114				size_t nregs, struct dentry *parent,
115				const char *fmt, ...)
116{
117	struct xhci_regset	*rgs;
118	va_list			args;
119	struct debugfs_regset32	*regset;
120	struct usb_hcd		*hcd = xhci_to_hcd(xhci);
121
122	rgs = xhci_debugfs_alloc_regset(xhci);
123	if (!rgs)
124		return;
125
126	va_start(args, fmt);
127	vsnprintf(rgs->name, sizeof(rgs->name), fmt, args);
128	va_end(args);
129
130	regset = &rgs->regset;
131	regset->regs = regs;
132	regset->nregs = nregs;
133	regset->base = hcd->regs + base;
 
134
135	debugfs_create_regset32((const char *)rgs->name, 0444, parent, regset);
136}
137
138static void xhci_debugfs_extcap_regset(struct xhci_hcd *xhci, int cap_id,
139				       const struct debugfs_reg32 *regs,
140				       size_t n, const char *cap_name)
141{
142	u32			offset;
143	int			index = 0;
144	size_t			psic, nregs = n;
145	void __iomem		*base = &xhci->cap_regs->hc_capbase;
146
147	offset = xhci_find_next_ext_cap(base, 0, cap_id);
148	while (offset) {
149		if (cap_id == XHCI_EXT_CAPS_PROTOCOL) {
150			psic = XHCI_EXT_PORT_PSIC(readl(base + offset + 8));
151			nregs = min(4 + psic, n);
152		}
153
154		xhci_debugfs_regset(xhci, offset, regs, nregs,
155				    xhci->debugfs_root, "%s:%02d",
156				    cap_name, index);
157		offset = xhci_find_next_ext_cap(base, offset, cap_id);
158		index++;
159	}
160}
161
162static int xhci_ring_enqueue_show(struct seq_file *s, void *unused)
163{
164	dma_addr_t		dma;
165	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
166
167	dma = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
168	seq_printf(s, "%pad\n", &dma);
169
170	return 0;
171}
172
173static int xhci_ring_dequeue_show(struct seq_file *s, void *unused)
174{
175	dma_addr_t		dma;
176	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
177
178	dma = xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
179	seq_printf(s, "%pad\n", &dma);
180
181	return 0;
182}
183
184static int xhci_ring_cycle_show(struct seq_file *s, void *unused)
185{
186	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
187
188	seq_printf(s, "%d\n", ring->cycle_state);
189
190	return 0;
191}
192
193static void xhci_ring_dump_segment(struct seq_file *s,
194				   struct xhci_segment *seg)
195{
196	int			i;
197	dma_addr_t		dma;
198	union xhci_trb		*trb;
 
199
200	for (i = 0; i < TRBS_PER_SEGMENT; i++) {
201		trb = &seg->trbs[i];
202		dma = seg->dma + i * sizeof(*trb);
203		seq_printf(s, "%pad: %s\n", &dma,
204			   xhci_decode_trb(trb->generic.field[0],
205					   trb->generic.field[1],
206					   trb->generic.field[2],
207					   trb->generic.field[3]));
208	}
209}
210
211static int xhci_ring_trb_show(struct seq_file *s, void *unused)
212{
213	int			i;
214	struct xhci_ring	*ring = *(struct xhci_ring **)s->private;
215	struct xhci_segment	*seg = ring->first_seg;
216
217	for (i = 0; i < ring->num_segs; i++) {
218		xhci_ring_dump_segment(s, seg);
219		seg = seg->next;
220	}
221
222	return 0;
223}
224
225static struct xhci_file_map ring_files[] = {
226	{"enqueue",		xhci_ring_enqueue_show, },
227	{"dequeue",		xhci_ring_dequeue_show, },
228	{"cycle",		xhci_ring_cycle_show, },
229	{"trbs",		xhci_ring_trb_show, },
230};
231
232static int xhci_ring_open(struct inode *inode, struct file *file)
233{
234	int			i;
235	struct xhci_file_map	*f_map;
236	const char		*file_name = file_dentry(file)->d_iname;
237
238	for (i = 0; i < ARRAY_SIZE(ring_files); i++) {
239		f_map = &ring_files[i];
240
241		if (strcmp(f_map->name, file_name) == 0)
242			break;
243	}
244
245	return single_open(file, f_map->show, inode->i_private);
246}
247
248static const struct file_operations xhci_ring_fops = {
249	.open			= xhci_ring_open,
250	.read			= seq_read,
251	.llseek			= seq_lseek,
252	.release		= single_release,
253};
254
255static int xhci_slot_context_show(struct seq_file *s, void *unused)
256{
257	struct xhci_hcd		*xhci;
258	struct xhci_slot_ctx	*slot_ctx;
259	struct xhci_slot_priv	*priv = s->private;
260	struct xhci_virt_device	*dev = priv->dev;
 
261
262	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
263	slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
264	seq_printf(s, "%pad: %s\n", &dev->out_ctx->dma,
265		   xhci_decode_slot_context(slot_ctx->dev_info,
266					    slot_ctx->dev_info2,
267					    slot_ctx->tt_info,
268					    slot_ctx->dev_state));
 
269
270	return 0;
271}
272
273static int xhci_endpoint_context_show(struct seq_file *s, void *unused)
274{
275	int			dci;
276	dma_addr_t		dma;
277	struct xhci_hcd		*xhci;
278	struct xhci_ep_ctx	*ep_ctx;
279	struct xhci_slot_priv	*priv = s->private;
280	struct xhci_virt_device	*dev = priv->dev;
 
281
282	xhci = hcd_to_xhci(bus_to_hcd(dev->udev->bus));
283
284	for (dci = 1; dci < 32; dci++) {
285		ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, dci);
286		dma = dev->out_ctx->dma + dci * CTX_SIZE(xhci->hcc_params);
287		seq_printf(s, "%pad: %s\n", &dma,
288			   xhci_decode_ep_context(ep_ctx->ep_info,
289						  ep_ctx->ep_info2,
290						  ep_ctx->deq,
291						  ep_ctx->tx_info));
 
292	}
293
294	return 0;
295}
296
297static int xhci_device_name_show(struct seq_file *s, void *unused)
298{
299	struct xhci_slot_priv	*priv = s->private;
300	struct xhci_virt_device	*dev = priv->dev;
301
302	seq_printf(s, "%s\n", dev_name(&dev->udev->dev));
303
304	return 0;
305}
306
307static struct xhci_file_map context_files[] = {
308	{"name",		xhci_device_name_show, },
309	{"slot-context",	xhci_slot_context_show, },
310	{"ep-context",		xhci_endpoint_context_show, },
311};
312
313static int xhci_context_open(struct inode *inode, struct file *file)
314{
315	int			i;
316	struct xhci_file_map	*f_map;
317	const char		*file_name = file_dentry(file)->d_iname;
318
319	for (i = 0; i < ARRAY_SIZE(context_files); i++) {
320		f_map = &context_files[i];
321
322		if (strcmp(f_map->name, file_name) == 0)
323			break;
324	}
325
326	return single_open(file, f_map->show, inode->i_private);
327}
328
329static const struct file_operations xhci_context_fops = {
330	.open			= xhci_context_open,
331	.read			= seq_read,
332	.llseek			= seq_lseek,
333	.release		= single_release,
334};
335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336static void xhci_debugfs_create_files(struct xhci_hcd *xhci,
337				      struct xhci_file_map *files,
338				      size_t nentries, void *data,
339				      struct dentry *parent,
340				      const struct file_operations *fops)
341{
342	int			i;
343
344	for (i = 0; i < nentries; i++)
345		debugfs_create_file(files[i].name, 0444, parent, data, fops);
346}
347
348static struct dentry *xhci_debugfs_create_ring_dir(struct xhci_hcd *xhci,
349						   struct xhci_ring **ring,
350						   const char *name,
351						   struct dentry *parent)
352{
353	struct dentry		*dir;
354
355	dir = debugfs_create_dir(name, parent);
356	xhci_debugfs_create_files(xhci, ring_files, ARRAY_SIZE(ring_files),
357				  ring, dir, &xhci_ring_fops);
358
359	return dir;
360}
361
362static void xhci_debugfs_create_context_files(struct xhci_hcd *xhci,
363					      struct dentry *parent,
364					      int slot_id)
365{
366	struct xhci_virt_device	*dev = xhci->devs[slot_id];
367
368	xhci_debugfs_create_files(xhci, context_files,
369				  ARRAY_SIZE(context_files),
370				  dev->debugfs_private,
371				  parent, &xhci_context_fops);
372}
373
374void xhci_debugfs_create_endpoint(struct xhci_hcd *xhci,
375				  struct xhci_virt_device *dev,
376				  int ep_index)
377{
378	struct xhci_ep_priv	*epriv;
379	struct xhci_slot_priv	*spriv = dev->debugfs_private;
380
 
 
 
381	if (spriv->eps[ep_index])
382		return;
383
384	epriv = kzalloc(sizeof(*epriv), GFP_KERNEL);
385	if (!epriv)
386		return;
387
 
 
388	snprintf(epriv->name, sizeof(epriv->name), "ep%02d", ep_index);
389	epriv->root = xhci_debugfs_create_ring_dir(xhci,
390						   &dev->eps[ep_index].ring,
391						   epriv->name,
392						   spriv->root);
393	spriv->eps[ep_index] = epriv;
394}
395
396void xhci_debugfs_remove_endpoint(struct xhci_hcd *xhci,
397				  struct xhci_virt_device *dev,
398				  int ep_index)
399{
400	struct xhci_ep_priv	*epriv;
401	struct xhci_slot_priv	*spriv = dev->debugfs_private;
402
403	if (!spriv || !spriv->eps[ep_index])
404		return;
405
406	epriv = spriv->eps[ep_index];
407	debugfs_remove_recursive(epriv->root);
408	spriv->eps[ep_index] = NULL;
409	kfree(epriv);
410}
411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412void xhci_debugfs_create_slot(struct xhci_hcd *xhci, int slot_id)
413{
414	struct xhci_slot_priv	*priv;
415	struct xhci_virt_device	*dev = xhci->devs[slot_id];
416
417	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
418	if (!priv)
419		return;
420
421	snprintf(priv->name, sizeof(priv->name), "%02d", slot_id);
422	priv->root = debugfs_create_dir(priv->name, xhci->debugfs_slots);
423	priv->dev = dev;
424	dev->debugfs_private = priv;
425
426	xhci_debugfs_create_ring_dir(xhci, &dev->eps[0].ring,
427				     "ep00", priv->root);
428
429	xhci_debugfs_create_context_files(xhci, priv->root, slot_id);
430}
431
432void xhci_debugfs_remove_slot(struct xhci_hcd *xhci, int slot_id)
433{
434	int			i;
435	struct xhci_slot_priv	*priv;
436	struct xhci_virt_device	*dev = xhci->devs[slot_id];
437
438	if (!dev || !dev->debugfs_private)
439		return;
440
441	priv = dev->debugfs_private;
442
443	debugfs_remove_recursive(priv->root);
444
445	for (i = 0; i < 31; i++)
446		kfree(priv->eps[i]);
447
448	kfree(priv);
449	dev->debugfs_private = NULL;
450}
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452void xhci_debugfs_init(struct xhci_hcd *xhci)
453{
454	struct device		*dev = xhci_to_hcd(xhci)->self.controller;
455
456	xhci->debugfs_root = debugfs_create_dir(dev_name(dev),
457						xhci_debugfs_root);
458
459	INIT_LIST_HEAD(&xhci->regset_list);
460
461	xhci_debugfs_regset(xhci,
462			    0,
463			    xhci_cap_regs, ARRAY_SIZE(xhci_cap_regs),
464			    xhci->debugfs_root, "reg-cap");
465
466	xhci_debugfs_regset(xhci,
467			    HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)),
468			    xhci_op_regs, ARRAY_SIZE(xhci_op_regs),
469			    xhci->debugfs_root, "reg-op");
470
471	xhci_debugfs_regset(xhci,
472			    readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK,
473			    xhci_runtime_regs, ARRAY_SIZE(xhci_runtime_regs),
474			    xhci->debugfs_root, "reg-runtime");
475
476	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_LEGACY,
477				   xhci_extcap_legsup,
478				   ARRAY_SIZE(xhci_extcap_legsup),
479				   "reg-ext-legsup");
480
481	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_PROTOCOL,
482				   xhci_extcap_protocol,
483				   ARRAY_SIZE(xhci_extcap_protocol),
484				   "reg-ext-protocol");
485
486	xhci_debugfs_extcap_regset(xhci, XHCI_EXT_CAPS_DEBUG,
487				   xhci_extcap_dbc,
488				   ARRAY_SIZE(xhci_extcap_dbc),
489				   "reg-ext-dbc");
490
491	xhci_debugfs_create_ring_dir(xhci, &xhci->cmd_ring,
492				     "command-ring",
493				     xhci->debugfs_root);
494
495	xhci_debugfs_create_ring_dir(xhci, &xhci->event_ring,
496				     "event-ring",
497				     xhci->debugfs_root);
498
499	xhci->debugfs_slots = debugfs_create_dir("devices", xhci->debugfs_root);
 
 
500}
501
502void xhci_debugfs_exit(struct xhci_hcd *xhci)
503{
504	struct xhci_regset	*rgs, *tmp;
505
506	debugfs_remove_recursive(xhci->debugfs_root);
507	xhci->debugfs_root = NULL;
508	xhci->debugfs_slots = NULL;
509
510	list_for_each_entry_safe(rgs, tmp, &xhci->regset_list, list)
511		xhci_debugfs_free_regset(rgs);
512}
513
514void __init xhci_debugfs_create_root(void)
515{
516	xhci_debugfs_root = debugfs_create_dir("xhci", usb_debug_root);
517}
518
519void __exit xhci_debugfs_remove_root(void)
520{
521	debugfs_remove_recursive(xhci_debugfs_root);
522	xhci_debugfs_root = NULL;
523}