1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Intel Corporation
6 * Authors: Gil Fine <gil.fine@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/debugfs.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/uaccess.h>
17 #define PORT_CAP_V1_PCIE_LEN 1
18 #define PORT_CAP_V2_PCIE_LEN 2
19 #define PORT_CAP_POWER_LEN 2
20 #define PORT_CAP_LANE_LEN 3
21 #define PORT_CAP_USB3_LEN 5
22 #define PORT_CAP_DP_V1_LEN 9
23 #define PORT_CAP_DP_V2_LEN 14
24 #define PORT_CAP_TMU_V1_LEN 8
25 #define PORT_CAP_TMU_V2_LEN 10
26 #define PORT_CAP_BASIC_LEN 9
27 #define PORT_CAP_USB4_LEN 20
29 #define SWITCH_CAP_TMU_LEN 26
30 #define SWITCH_CAP_BASIC_LEN 27
34 #define COUNTER_SET_LEN 3
36 #define DEBUGFS_ATTR(__space, __write) \
37 static int __space ## _open(struct inode *inode, struct file *file) \
39 return single_open(file, __space ## _show, inode->i_private); \
42 static const struct file_operations __space ## _fops = { \
43 .owner = THIS_MODULE, \
44 .open = __space ## _open, \
45 .release = single_release, \
48 .llseek = seq_lseek, \
51 #define DEBUGFS_ATTR_RO(__space) \
52 DEBUGFS_ATTR(__space, NULL)
54 #define DEBUGFS_ATTR_RW(__space) \
55 DEBUGFS_ATTR(__space, __space ## _write)
57 static struct dentry *tb_debugfs_root;
59 static void *validate_and_copy_from_user(const void __user *user_buf,
66 return ERR_PTR(-EINVAL);
68 if (!access_ok(user_buf, *count))
69 return ERR_PTR(-EFAULT);
71 buf = (void *)get_zeroed_page(GFP_KERNEL);
73 return ERR_PTR(-ENOMEM);
75 nbytes = min_t(size_t, *count, PAGE_SIZE);
76 if (copy_from_user(buf, user_buf, nbytes)) {
77 free_page((unsigned long)buf);
78 return ERR_PTR(-EFAULT);
85 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
92 token = strsep(line, "\n");
97 * For Adapter/Router configuration space:
98 * Short format is: offset value\n
100 * Long format as produced from the read side:
101 * offset relative_offset cap_id vs_cap_id value\n
102 * v[0] v[1] v[2] v[3] v[4]
104 * For Counter configuration space:
105 * Short format is: offset\n
107 * Long format as produced from the read side:
108 * offset relative_offset counter_id value\n
109 * v[0] v[1] v[2] v[3]
111 ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
112 /* In case of Counters, clear counter, "val" content is NA */
113 if (ret == short_fmt_len) {
115 *val = v[short_fmt_len - 1];
117 } else if (ret == long_fmt_len) {
119 *val = v[long_fmt_len - 1];
126 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
127 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
128 const char __user *user_buf, size_t count,
131 struct tb *tb = sw->tb;
136 buf = validate_and_copy_from_user(user_buf, &count);
140 pm_runtime_get_sync(&sw->dev);
142 if (mutex_lock_interruptible(&tb->lock)) {
147 /* User did hardware changes behind the driver's back */
148 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
151 while (parse_line(&line, &offset, &val, 2, 5)) {
153 ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
155 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
160 mutex_unlock(&tb->lock);
163 pm_runtime_mark_last_busy(&sw->dev);
164 pm_runtime_put_autosuspend(&sw->dev);
165 free_page((unsigned long)buf);
167 return ret < 0 ? ret : count;
170 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
171 size_t count, loff_t *ppos)
173 struct seq_file *s = file->private_data;
174 struct tb_port *port = s->private;
176 return regs_write(port->sw, port, user_buf, count, ppos);
179 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
180 size_t count, loff_t *ppos)
182 struct seq_file *s = file->private_data;
183 struct tb_switch *sw = s->private;
185 return regs_write(sw, NULL, user_buf, count, ppos);
187 #define DEBUGFS_MODE 0600
189 #define port_regs_write NULL
190 #define switch_regs_write NULL
191 #define DEBUGFS_MODE 0400
194 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
196 * struct tb_margining - Lane margining support
197 * @caps: Port lane margining capabilities
198 * @results: Last lane margining results
199 * @lanes: %0, %1 or %7 (all)
200 * @min_ber_level: Minimum supported BER level contour value
201 * @max_ber_level: Maximum supported BER level contour value
202 * @ber_level: Current BER level contour value
203 * @voltage_steps: Number of mandatory voltage steps
204 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
205 * @time_steps: Number of time margin steps
206 * @max_time_offset: Maximum time margin offset (in mUI)
207 * @software: %true if software margining is used instead of hardware
208 * @time: %true if time margining is used instead of voltage
209 * @right_high: %false if left/low margin test is performed, %true if
212 struct tb_margining {
216 unsigned int min_ber_level;
217 unsigned int max_ber_level;
218 unsigned int ber_level;
219 unsigned int voltage_steps;
220 unsigned int max_voltage_offset;
221 unsigned int time_steps;
222 unsigned int max_time_offset;
228 static bool supports_software(const struct usb4_port *usb4)
230 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
233 static bool supports_hardware(const struct usb4_port *usb4)
235 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
238 static bool both_lanes(const struct usb4_port *usb4)
240 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
243 static unsigned int independent_voltage_margins(const struct usb4_port *usb4)
245 return (usb4->margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK) >>
246 USB4_MARGIN_CAP_0_VOLTAGE_INDP_SHIFT;
249 static bool supports_time(const struct usb4_port *usb4)
251 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
254 /* Only applicable if supports_time() returns true */
255 static unsigned int independent_time_margins(const struct usb4_port *usb4)
257 return (usb4->margining->caps[1] & USB4_MARGIN_CAP_1_TIME_INDP_MASK) >>
258 USB4_MARGIN_CAP_1_TIME_INDP_SHIFT;
262 margining_ber_level_write(struct file *file, const char __user *user_buf,
263 size_t count, loff_t *ppos)
265 struct seq_file *s = file->private_data;
266 struct tb_port *port = s->private;
267 struct usb4_port *usb4 = port->usb4;
268 struct tb *tb = port->sw->tb;
273 if (mutex_lock_interruptible(&tb->lock))
276 if (usb4->margining->software) {
281 buf = validate_and_copy_from_user(user_buf, &count);
287 buf[count - 1] = '\0';
289 ret = kstrtouint(buf, 10, &val);
293 if (val < usb4->margining->min_ber_level ||
294 val > usb4->margining->max_ber_level) {
299 usb4->margining->ber_level = val;
302 free_page((unsigned long)buf);
304 mutex_unlock(&tb->lock);
306 return ret < 0 ? ret : count;
309 static void ber_level_show(struct seq_file *s, unsigned int val)
312 seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
314 seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
317 static int margining_ber_level_show(struct seq_file *s, void *not_used)
319 struct tb_port *port = s->private;
320 struct usb4_port *usb4 = port->usb4;
322 if (usb4->margining->software)
324 ber_level_show(s, usb4->margining->ber_level);
327 DEBUGFS_ATTR_RW(margining_ber_level);
329 static int margining_caps_show(struct seq_file *s, void *not_used)
331 struct tb_port *port = s->private;
332 struct usb4_port *usb4 = port->usb4;
333 struct tb *tb = port->sw->tb;
336 if (mutex_lock_interruptible(&tb->lock))
339 /* Dump the raw caps first */
340 cap0 = usb4->margining->caps[0];
341 seq_printf(s, "0x%08x\n", cap0);
342 cap1 = usb4->margining->caps[1];
343 seq_printf(s, "0x%08x\n", cap1);
345 seq_printf(s, "# software margining: %s\n",
346 supports_software(usb4) ? "yes" : "no");
347 if (supports_hardware(usb4)) {
348 seq_puts(s, "# hardware margining: yes\n");
349 seq_puts(s, "# minimum BER level contour: ");
350 ber_level_show(s, usb4->margining->min_ber_level);
351 seq_puts(s, "# maximum BER level contour: ");
352 ber_level_show(s, usb4->margining->max_ber_level);
354 seq_puts(s, "# hardware margining: no\n");
357 seq_printf(s, "# both lanes simultaneously: %s\n",
358 both_lanes(usb4) ? "yes" : "no");
359 seq_printf(s, "# voltage margin steps: %u\n",
360 usb4->margining->voltage_steps);
361 seq_printf(s, "# maximum voltage offset: %u mV\n",
362 usb4->margining->max_voltage_offset);
364 switch (independent_voltage_margins(usb4)) {
365 case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
366 seq_puts(s, "# returns minimum between high and low voltage margins\n");
368 case USB4_MARGIN_CAP_0_VOLTAGE_HL:
369 seq_puts(s, "# returns high or low voltage margin\n");
371 case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
372 seq_puts(s, "# returns both high and low margins\n");
376 if (supports_time(usb4)) {
377 seq_puts(s, "# time margining: yes\n");
378 seq_printf(s, "# time margining is destructive: %s\n",
379 cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
381 switch (independent_time_margins(usb4)) {
382 case USB4_MARGIN_CAP_1_TIME_MIN:
383 seq_puts(s, "# returns minimum between left and right time margins\n");
385 case USB4_MARGIN_CAP_1_TIME_LR:
386 seq_puts(s, "# returns left or right margin\n");
388 case USB4_MARGIN_CAP_1_TIME_BOTH:
389 seq_puts(s, "# returns both left and right margins\n");
393 seq_printf(s, "# time margin steps: %u\n",
394 usb4->margining->time_steps);
395 seq_printf(s, "# maximum time offset: %u mUI\n",
396 usb4->margining->max_time_offset);
398 seq_puts(s, "# time margining: no\n");
401 mutex_unlock(&tb->lock);
404 DEBUGFS_ATTR_RO(margining_caps);
407 margining_lanes_write(struct file *file, const char __user *user_buf,
408 size_t count, loff_t *ppos)
410 struct seq_file *s = file->private_data;
411 struct tb_port *port = s->private;
412 struct usb4_port *usb4 = port->usb4;
413 struct tb *tb = port->sw->tb;
417 buf = validate_and_copy_from_user(user_buf, &count);
421 buf[count - 1] = '\0';
423 if (mutex_lock_interruptible(&tb->lock)) {
428 if (!strcmp(buf, "0")) {
429 usb4->margining->lanes = 0;
430 } else if (!strcmp(buf, "1")) {
431 usb4->margining->lanes = 1;
432 } else if (!strcmp(buf, "all")) {
433 /* Needs to be supported */
434 if (both_lanes(usb4))
435 usb4->margining->lanes = 7;
442 mutex_unlock(&tb->lock);
445 free_page((unsigned long)buf);
446 return ret < 0 ? ret : count;
449 static int margining_lanes_show(struct seq_file *s, void *not_used)
451 struct tb_port *port = s->private;
452 struct usb4_port *usb4 = port->usb4;
453 struct tb *tb = port->sw->tb;
456 if (mutex_lock_interruptible(&tb->lock))
459 lanes = usb4->margining->lanes;
460 if (both_lanes(usb4)) {
462 seq_puts(s, "[0] 1 all\n");
464 seq_puts(s, "0 [1] all\n");
466 seq_puts(s, "0 1 [all]\n");
469 seq_puts(s, "[0] 1\n");
471 seq_puts(s, "0 [1]\n");
474 mutex_unlock(&tb->lock);
477 DEBUGFS_ATTR_RW(margining_lanes);
479 static ssize_t margining_mode_write(struct file *file,
480 const char __user *user_buf,
481 size_t count, loff_t *ppos)
483 struct seq_file *s = file->private_data;
484 struct tb_port *port = s->private;
485 struct usb4_port *usb4 = port->usb4;
486 struct tb *tb = port->sw->tb;
490 buf = validate_and_copy_from_user(user_buf, &count);
494 buf[count - 1] = '\0';
496 if (mutex_lock_interruptible(&tb->lock)) {
501 if (!strcmp(buf, "software")) {
502 if (supports_software(usb4))
503 usb4->margining->software = true;
506 } else if (!strcmp(buf, "hardware")) {
507 if (supports_hardware(usb4))
508 usb4->margining->software = false;
515 mutex_unlock(&tb->lock);
518 free_page((unsigned long)buf);
519 return ret ? ret : count;
522 static int margining_mode_show(struct seq_file *s, void *not_used)
524 const struct tb_port *port = s->private;
525 const struct usb4_port *usb4 = port->usb4;
526 struct tb *tb = port->sw->tb;
527 const char *space = "";
529 if (mutex_lock_interruptible(&tb->lock))
532 if (supports_software(usb4)) {
533 if (usb4->margining->software)
534 seq_puts(s, "[software]");
536 seq_puts(s, "software");
539 if (supports_hardware(usb4)) {
540 if (usb4->margining->software)
541 seq_printf(s, "%shardware", space);
543 seq_printf(s, "%s[hardware]", space);
546 mutex_unlock(&tb->lock);
551 DEBUGFS_ATTR_RW(margining_mode);
553 static int margining_run_write(void *data, u64 val)
555 struct tb_port *port = data;
556 struct usb4_port *usb4 = port->usb4;
557 struct tb_switch *sw = port->sw;
558 struct tb_margining *margining;
559 struct tb_switch *down_sw;
560 struct tb *tb = sw->tb;
566 pm_runtime_get_sync(&sw->dev);
568 if (mutex_lock_interruptible(&tb->lock)) {
573 if (tb_is_upstream_port(port))
575 else if (port->remote)
576 down_sw = port->remote->sw;
582 * CL states may interfere with lane margining so
583 * disable them temporarily now.
585 ret = tb_switch_clx_disable(down_sw);
587 tb_sw_warn(down_sw, "failed to disable CL states\n");
593 margining = usb4->margining;
595 if (margining->software) {
596 tb_port_dbg(port, "running software %s lane margining for lanes %u\n",
597 margining->time ? "time" : "voltage", margining->lanes);
598 ret = usb4_port_sw_margin(port, margining->lanes, margining->time,
599 margining->right_high,
600 USB4_MARGIN_SW_COUNTER_CLEAR);
604 ret = usb4_port_sw_margin_errors(port, &margining->results[0]);
606 tb_port_dbg(port, "running hardware %s lane margining for lanes %u\n",
607 margining->time ? "time" : "voltage", margining->lanes);
608 /* Clear the results */
609 margining->results[0] = 0;
610 margining->results[1] = 0;
611 ret = usb4_port_hw_margin(port, margining->lanes,
612 margining->ber_level, margining->time,
613 margining->right_high, margining->results);
618 tb_switch_clx_enable(down_sw, clx);
620 mutex_unlock(&tb->lock);
622 pm_runtime_mark_last_busy(&sw->dev);
623 pm_runtime_put_autosuspend(&sw->dev);
627 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
630 static ssize_t margining_results_write(struct file *file,
631 const char __user *user_buf,
632 size_t count, loff_t *ppos)
634 struct seq_file *s = file->private_data;
635 struct tb_port *port = s->private;
636 struct usb4_port *usb4 = port->usb4;
637 struct tb *tb = port->sw->tb;
639 if (mutex_lock_interruptible(&tb->lock))
642 /* Just clear the results */
643 usb4->margining->results[0] = 0;
644 usb4->margining->results[1] = 0;
646 mutex_unlock(&tb->lock);
650 static void voltage_margin_show(struct seq_file *s,
651 const struct tb_margining *margining, u8 val)
653 unsigned int tmp, voltage;
655 tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
656 voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
657 seq_printf(s, "%u mV (%u)", voltage, tmp);
658 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
659 seq_puts(s, " exceeds maximum");
663 static void time_margin_show(struct seq_file *s,
664 const struct tb_margining *margining, u8 val)
666 unsigned int tmp, interval;
668 tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
669 interval = tmp * margining->max_time_offset / margining->time_steps;
670 seq_printf(s, "%u mUI (%u)", interval, tmp);
671 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
672 seq_puts(s, " exceeds maximum");
676 static int margining_results_show(struct seq_file *s, void *not_used)
678 struct tb_port *port = s->private;
679 struct usb4_port *usb4 = port->usb4;
680 struct tb_margining *margining;
681 struct tb *tb = port->sw->tb;
683 if (mutex_lock_interruptible(&tb->lock))
686 margining = usb4->margining;
687 /* Dump the raw results first */
688 seq_printf(s, "0x%08x\n", margining->results[0]);
689 /* Only the hardware margining has two result dwords */
690 if (!margining->software) {
693 seq_printf(s, "0x%08x\n", margining->results[1]);
695 if (margining->time) {
696 if (!margining->lanes || margining->lanes == 7) {
697 val = margining->results[1];
698 seq_puts(s, "# lane 0 right time margin: ");
699 time_margin_show(s, margining, val);
700 val = margining->results[1] >>
701 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
702 seq_puts(s, "# lane 0 left time margin: ");
703 time_margin_show(s, margining, val);
705 if (margining->lanes == 1 || margining->lanes == 7) {
706 val = margining->results[1] >>
707 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
708 seq_puts(s, "# lane 1 right time margin: ");
709 time_margin_show(s, margining, val);
710 val = margining->results[1] >>
711 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
712 seq_puts(s, "# lane 1 left time margin: ");
713 time_margin_show(s, margining, val);
716 if (!margining->lanes || margining->lanes == 7) {
717 val = margining->results[1];
718 seq_puts(s, "# lane 0 high voltage margin: ");
719 voltage_margin_show(s, margining, val);
720 val = margining->results[1] >>
721 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
722 seq_puts(s, "# lane 0 low voltage margin: ");
723 voltage_margin_show(s, margining, val);
725 if (margining->lanes == 1 || margining->lanes == 7) {
726 val = margining->results[1] >>
727 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
728 seq_puts(s, "# lane 1 high voltage margin: ");
729 voltage_margin_show(s, margining, val);
730 val = margining->results[1] >>
731 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
732 seq_puts(s, "# lane 1 low voltage margin: ");
733 voltage_margin_show(s, margining, val);
738 mutex_unlock(&tb->lock);
741 DEBUGFS_ATTR_RW(margining_results);
743 static ssize_t margining_test_write(struct file *file,
744 const char __user *user_buf,
745 size_t count, loff_t *ppos)
747 struct seq_file *s = file->private_data;
748 struct tb_port *port = s->private;
749 struct usb4_port *usb4 = port->usb4;
750 struct tb *tb = port->sw->tb;
754 buf = validate_and_copy_from_user(user_buf, &count);
758 buf[count - 1] = '\0';
760 if (mutex_lock_interruptible(&tb->lock)) {
765 if (!strcmp(buf, "time") && supports_time(usb4))
766 usb4->margining->time = true;
767 else if (!strcmp(buf, "voltage"))
768 usb4->margining->time = false;
772 mutex_unlock(&tb->lock);
775 free_page((unsigned long)buf);
776 return ret ? ret : count;
779 static int margining_test_show(struct seq_file *s, void *not_used)
781 struct tb_port *port = s->private;
782 struct usb4_port *usb4 = port->usb4;
783 struct tb *tb = port->sw->tb;
785 if (mutex_lock_interruptible(&tb->lock))
788 if (supports_time(usb4)) {
789 if (usb4->margining->time)
790 seq_puts(s, "voltage [time]\n");
792 seq_puts(s, "[voltage] time\n");
794 seq_puts(s, "[voltage]\n");
797 mutex_unlock(&tb->lock);
800 DEBUGFS_ATTR_RW(margining_test);
802 static ssize_t margining_margin_write(struct file *file,
803 const char __user *user_buf,
804 size_t count, loff_t *ppos)
806 struct seq_file *s = file->private_data;
807 struct tb_port *port = s->private;
808 struct usb4_port *usb4 = port->usb4;
809 struct tb *tb = port->sw->tb;
813 buf = validate_and_copy_from_user(user_buf, &count);
817 buf[count - 1] = '\0';
819 if (mutex_lock_interruptible(&tb->lock)) {
824 if (usb4->margining->time) {
825 if (!strcmp(buf, "left"))
826 usb4->margining->right_high = false;
827 else if (!strcmp(buf, "right"))
828 usb4->margining->right_high = true;
832 if (!strcmp(buf, "low"))
833 usb4->margining->right_high = false;
834 else if (!strcmp(buf, "high"))
835 usb4->margining->right_high = true;
840 mutex_unlock(&tb->lock);
843 free_page((unsigned long)buf);
844 return ret ? ret : count;
847 static int margining_margin_show(struct seq_file *s, void *not_used)
849 struct tb_port *port = s->private;
850 struct usb4_port *usb4 = port->usb4;
851 struct tb *tb = port->sw->tb;
853 if (mutex_lock_interruptible(&tb->lock))
856 if (usb4->margining->time) {
857 if (usb4->margining->right_high)
858 seq_puts(s, "left [right]\n");
860 seq_puts(s, "[left] right\n");
862 if (usb4->margining->right_high)
863 seq_puts(s, "low [high]\n");
865 seq_puts(s, "[low] high\n");
868 mutex_unlock(&tb->lock);
871 DEBUGFS_ATTR_RW(margining_margin);
873 static void margining_port_init(struct tb_port *port)
875 struct tb_margining *margining;
876 struct dentry *dir, *parent;
877 struct usb4_port *usb4;
886 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
887 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
889 margining = kzalloc(sizeof(*margining), GFP_KERNEL);
893 ret = usb4_port_margining_caps(port, margining->caps);
899 usb4->margining = margining;
901 /* Set the initial mode */
902 if (supports_software(usb4))
903 margining->software = true;
905 val = (margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK) >>
906 USB4_MARGIN_CAP_0_VOLTAGE_STEPS_SHIFT;
907 margining->voltage_steps = val;
908 val = (margining->caps[0] & USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK) >>
909 USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_SHIFT;
910 margining->max_voltage_offset = 74 + val * 2;
912 if (supports_time(usb4)) {
913 val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_STEPS_MASK) >>
914 USB4_MARGIN_CAP_1_TIME_STEPS_SHIFT;
915 margining->time_steps = val;
916 val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_OFFSET_MASK) >>
917 USB4_MARGIN_CAP_1_TIME_OFFSET_SHIFT;
919 * Store it as mUI (milli Unit Interval) because we want
920 * to keep it as integer.
922 margining->max_time_offset = 200 + 10 * val;
925 dir = debugfs_create_dir("margining", parent);
926 if (supports_hardware(usb4)) {
927 val = (margining->caps[1] & USB4_MARGIN_CAP_1_MIN_BER_MASK) >>
928 USB4_MARGIN_CAP_1_MIN_BER_SHIFT;
929 margining->min_ber_level = val;
930 val = (margining->caps[1] & USB4_MARGIN_CAP_1_MAX_BER_MASK) >>
931 USB4_MARGIN_CAP_1_MAX_BER_SHIFT;
932 margining->max_ber_level = val;
934 /* Set the default to minimum */
935 margining->ber_level = margining->min_ber_level;
937 debugfs_create_file("ber_level_contour", 0400, dir, port,
938 &margining_ber_level_fops);
940 debugfs_create_file("caps", 0400, dir, port, &margining_caps_fops);
941 debugfs_create_file("lanes", 0600, dir, port, &margining_lanes_fops);
942 debugfs_create_file("mode", 0600, dir, port, &margining_mode_fops);
943 debugfs_create_file("run", 0600, dir, port, &margining_run_fops);
944 debugfs_create_file("results", 0600, dir, port, &margining_results_fops);
945 debugfs_create_file("test", 0600, dir, port, &margining_test_fops);
946 if (independent_voltage_margins(usb4) ||
947 (supports_time(usb4) && independent_time_margins(usb4)))
948 debugfs_create_file("margin", 0600, dir, port, &margining_margin_fops);
951 static void margining_port_remove(struct tb_port *port)
953 struct dentry *parent;
959 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
960 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
962 debugfs_lookup_and_remove("margining", parent);
964 kfree(port->usb4->margining);
965 port->usb4->margining = NULL;
968 static void margining_switch_init(struct tb_switch *sw)
970 struct tb_port *upstream, *downstream;
971 struct tb_switch *parent_sw;
972 u64 route = tb_route(sw);
977 upstream = tb_upstream_port(sw);
978 parent_sw = tb_switch_parent(sw);
979 downstream = tb_port_at(route, parent_sw);
981 margining_port_init(downstream);
982 margining_port_init(upstream);
985 static void margining_switch_remove(struct tb_switch *sw)
987 struct tb_port *upstream, *downstream;
988 struct tb_switch *parent_sw;
989 u64 route = tb_route(sw);
994 upstream = tb_upstream_port(sw);
995 parent_sw = tb_switch_parent(sw);
996 downstream = tb_port_at(route, parent_sw);
998 margining_port_remove(upstream);
999 margining_port_remove(downstream);
1002 static void margining_xdomain_init(struct tb_xdomain *xd)
1004 struct tb_switch *parent_sw;
1005 struct tb_port *downstream;
1007 parent_sw = tb_xdomain_parent(xd);
1008 downstream = tb_port_at(xd->route, parent_sw);
1010 margining_port_init(downstream);
1013 static void margining_xdomain_remove(struct tb_xdomain *xd)
1015 struct tb_switch *parent_sw;
1016 struct tb_port *downstream;
1018 parent_sw = tb_xdomain_parent(xd);
1019 downstream = tb_port_at(xd->route, parent_sw);
1020 margining_port_remove(downstream);
1023 static inline void margining_switch_init(struct tb_switch *sw) { }
1024 static inline void margining_switch_remove(struct tb_switch *sw) { }
1025 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1026 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1029 static int port_clear_all_counters(struct tb_port *port)
1034 buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1039 ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1040 COUNTER_SET_LEN * port->config.max_counters);
1046 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1047 size_t count, loff_t *ppos)
1049 struct seq_file *s = file->private_data;
1050 struct tb_port *port = s->private;
1051 struct tb_switch *sw = port->sw;
1052 struct tb *tb = port->sw->tb;
1056 buf = validate_and_copy_from_user(user_buf, &count);
1058 return PTR_ERR(buf);
1060 pm_runtime_get_sync(&sw->dev);
1062 if (mutex_lock_interruptible(&tb->lock)) {
1067 /* If written delimiter only, clear all counters in one shot */
1068 if (buf[0] == '\n') {
1069 ret = port_clear_all_counters(port);
1075 while (parse_line(&line, &offset, &val, 1, 4)) {
1076 ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1083 mutex_unlock(&tb->lock);
1086 pm_runtime_mark_last_busy(&sw->dev);
1087 pm_runtime_put_autosuspend(&sw->dev);
1088 free_page((unsigned long)buf);
1090 return ret < 0 ? ret : count;
1093 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1094 struct tb_port *port, unsigned int cap,
1095 unsigned int offset, u8 cap_id, u8 vsec_id,
1101 for (i = 0; i < dwords; i++) {
1103 ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1105 ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1107 seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1111 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1112 offset + i, cap_id, vsec_id, data);
1116 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1117 struct tb_port *port, unsigned int cap, u8 cap_id,
1118 u8 vsec_id, int length)
1120 int ret, offset = 0;
1122 while (length > 0) {
1123 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1124 u32 data[TB_MAX_CONFIG_RW_LENGTH];
1127 ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1130 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1132 cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1136 for (i = 0; i < dwords; i++) {
1137 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1138 cap + offset + i, offset + i,
1139 cap_id, vsec_id, data[i]);
1147 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1150 struct tb_cap_any header;
1155 ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1157 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1161 switch (header.basic.cap) {
1162 case TB_PORT_CAP_PHY:
1163 length = PORT_CAP_LANE_LEN;
1166 case TB_PORT_CAP_TIME1:
1167 if (usb4_switch_version(port->sw) < 2)
1168 length = PORT_CAP_TMU_V1_LEN;
1170 length = PORT_CAP_TMU_V2_LEN;
1173 case TB_PORT_CAP_POWER:
1174 length = PORT_CAP_POWER_LEN;
1177 case TB_PORT_CAP_ADAP:
1178 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1179 if (usb4_switch_version(port->sw) < 2)
1180 length = PORT_CAP_V1_PCIE_LEN;
1182 length = PORT_CAP_V2_PCIE_LEN;
1183 } else if (tb_port_is_dpin(port)) {
1184 if (usb4_switch_version(port->sw) < 2)
1185 length = PORT_CAP_DP_V1_LEN;
1187 length = PORT_CAP_DP_V2_LEN;
1188 } else if (tb_port_is_dpout(port)) {
1189 length = PORT_CAP_DP_V1_LEN;
1190 } else if (tb_port_is_usb3_down(port) ||
1191 tb_port_is_usb3_up(port)) {
1192 length = PORT_CAP_USB3_LEN;
1194 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1195 cap, header.basic.cap);
1200 case TB_PORT_CAP_VSE:
1201 if (!header.extended_short.length) {
1202 ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1205 seq_printf(s, "0x%04x <capability read failed>\n",
1209 length = header.extended_long.length;
1210 vsec_id = header.extended_short.vsec_id;
1212 length = header.extended_short.length;
1213 vsec_id = header.extended_short.vsec_id;
1217 case TB_PORT_CAP_USB4:
1218 length = PORT_CAP_USB4_LEN;
1222 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1223 cap, header.basic.cap);
1227 cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1230 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1234 cap = tb_port_next_cap(port, 0);
1236 port_cap_show(port, s, cap);
1237 cap = tb_port_next_cap(port, cap);
1241 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1243 u32 data[PORT_CAP_BASIC_LEN];
1246 ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1250 for (i = 0; i < ARRAY_SIZE(data); i++)
1251 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1256 static int port_regs_show(struct seq_file *s, void *not_used)
1258 struct tb_port *port = s->private;
1259 struct tb_switch *sw = port->sw;
1260 struct tb *tb = sw->tb;
1263 pm_runtime_get_sync(&sw->dev);
1265 if (mutex_lock_interruptible(&tb->lock)) {
1270 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1272 ret = port_basic_regs_show(port, s);
1276 port_caps_show(port, s);
1279 mutex_unlock(&tb->lock);
1281 pm_runtime_mark_last_busy(&sw->dev);
1282 pm_runtime_put_autosuspend(&sw->dev);
1286 DEBUGFS_ATTR_RW(port_regs);
1288 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1291 struct tb_cap_any header;
1295 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1297 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1301 if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1302 if (!header.extended_short.length) {
1303 ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1306 seq_printf(s, "0x%04x <capability read failed>\n",
1310 length = header.extended_long.length;
1312 length = header.extended_short.length;
1314 vsec_id = header.extended_short.vsec_id;
1316 if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1317 length = SWITCH_CAP_TMU_LEN;
1319 seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1320 cap, header.basic.cap);
1325 cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1328 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1332 cap = tb_switch_next_cap(sw, 0);
1334 switch_cap_show(sw, s, cap);
1335 cap = tb_switch_next_cap(sw, cap);
1339 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1341 u32 data[SWITCH_CAP_BASIC_LEN];
1345 /* Only USB4 has the additional registers */
1346 if (tb_switch_is_usb4(sw))
1347 dwords = ARRAY_SIZE(data);
1351 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1355 for (i = 0; i < dwords; i++)
1356 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1361 static int switch_regs_show(struct seq_file *s, void *not_used)
1363 struct tb_switch *sw = s->private;
1364 struct tb *tb = sw->tb;
1367 pm_runtime_get_sync(&sw->dev);
1369 if (mutex_lock_interruptible(&tb->lock)) {
1374 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1376 ret = switch_basic_regs_show(sw, s);
1380 switch_caps_show(sw, s);
1383 mutex_unlock(&tb->lock);
1385 pm_runtime_mark_last_busy(&sw->dev);
1386 pm_runtime_put_autosuspend(&sw->dev);
1390 DEBUGFS_ATTR_RW(switch_regs);
1392 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1397 ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1400 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1404 for (i = 0; i < ARRAY_SIZE(data); i++) {
1405 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1406 hopid * PATH_LEN + i, i, hopid, data[i]);
1412 static int path_show(struct seq_file *s, void *not_used)
1414 struct tb_port *port = s->private;
1415 struct tb_switch *sw = port->sw;
1416 struct tb *tb = sw->tb;
1417 int start, i, ret = 0;
1419 pm_runtime_get_sync(&sw->dev);
1421 if (mutex_lock_interruptible(&tb->lock)) {
1426 seq_puts(s, "# offset relative_offset in_hop_id value\n");
1428 /* NHI and lane adapters have entry for path 0 */
1429 if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1430 ret = path_show_one(port, s, 0);
1435 start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1437 for (i = start; i <= port->config.max_in_hop_id; i++) {
1438 ret = path_show_one(port, s, i);
1444 mutex_unlock(&tb->lock);
1446 pm_runtime_mark_last_busy(&sw->dev);
1447 pm_runtime_put_autosuspend(&sw->dev);
1451 DEBUGFS_ATTR_RO(path);
1453 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1456 u32 data[COUNTER_SET_LEN];
1459 ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1460 counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1462 seq_printf(s, "0x%04x <not accessible>\n",
1463 counter * COUNTER_SET_LEN);
1467 for (i = 0; i < ARRAY_SIZE(data); i++) {
1468 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1469 counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1475 static int counters_show(struct seq_file *s, void *not_used)
1477 struct tb_port *port = s->private;
1478 struct tb_switch *sw = port->sw;
1479 struct tb *tb = sw->tb;
1482 pm_runtime_get_sync(&sw->dev);
1484 if (mutex_lock_interruptible(&tb->lock)) {
1489 seq_puts(s, "# offset relative_offset counter_id value\n");
1491 for (i = 0; i < port->config.max_counters; i++) {
1492 ret = counter_set_regs_show(port, s, i);
1497 mutex_unlock(&tb->lock);
1500 pm_runtime_mark_last_busy(&sw->dev);
1501 pm_runtime_put_autosuspend(&sw->dev);
1505 DEBUGFS_ATTR_RW(counters);
1508 * tb_switch_debugfs_init() - Add debugfs entries for router
1509 * @sw: Pointer to the router
1511 * Adds debugfs directories and files for given router.
1513 void tb_switch_debugfs_init(struct tb_switch *sw)
1515 struct dentry *debugfs_dir;
1516 struct tb_port *port;
1518 debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1519 sw->debugfs_dir = debugfs_dir;
1520 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1523 tb_switch_for_each_port(sw, port) {
1524 struct dentry *debugfs_dir;
1529 if (port->config.type == TB_TYPE_INACTIVE)
1532 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1533 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1534 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1535 port, &port_regs_fops);
1536 debugfs_create_file("path", 0400, debugfs_dir, port,
1538 if (port->config.counters_support)
1539 debugfs_create_file("counters", 0600, debugfs_dir, port,
1543 margining_switch_init(sw);
1547 * tb_switch_debugfs_remove() - Remove all router debugfs entries
1548 * @sw: Pointer to the router
1550 * Removes all previously added debugfs entries under this router.
1552 void tb_switch_debugfs_remove(struct tb_switch *sw)
1554 margining_switch_remove(sw);
1555 debugfs_remove_recursive(sw->debugfs_dir);
1558 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1560 margining_xdomain_init(xd);
1563 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1565 margining_xdomain_remove(xd);
1569 * tb_service_debugfs_init() - Add debugfs directory for service
1570 * @svc: Thunderbolt service pointer
1572 * Adds debugfs directory for service.
1574 void tb_service_debugfs_init(struct tb_service *svc)
1576 svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1581 * tb_service_debugfs_remove() - Remove service debugfs directory
1582 * @svc: Thunderbolt service pointer
1584 * Removes the previously created debugfs directory for @svc.
1586 void tb_service_debugfs_remove(struct tb_service *svc)
1588 debugfs_remove_recursive(svc->debugfs_dir);
1589 svc->debugfs_dir = NULL;
1592 void tb_debugfs_init(void)
1594 tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1597 void tb_debugfs_exit(void)
1599 debugfs_remove_recursive(tb_debugfs_root);