1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/platform_data/x86/apple.h>
19 #define TB_TIMEOUT 100 /* ms */
22 * struct tb_cm - Simple Thunderbolt connection manager
23 * @tunnel_list: List of active tunnels
24 * @dp_resources: List of available DP resources for DP tunneling
25 * @hotplug_active: tb_handle_hotplug will stop progressing plug
26 * events and exit if this is not set (it needs to
27 * acquire the lock one more time). Used to drain wq
28 * after cfg has been paused.
29 * @remove_work: Work used to remove any unplugged routers after
33 struct list_head tunnel_list;
34 struct list_head dp_resources;
36 struct delayed_work remove_work;
39 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
41 return ((void *)tcm - sizeof(struct tb));
44 struct tb_hotplug_event {
45 struct work_struct work;
52 static void tb_handle_hotplug(struct work_struct *work);
54 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
56 struct tb_hotplug_event *ev;
58 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
66 INIT_WORK(&ev->work, tb_handle_hotplug);
67 queue_work(tb->wq, &ev->work);
70 /* enumeration & hot plug handling */
72 static void tb_add_dp_resources(struct tb_switch *sw)
74 struct tb_cm *tcm = tb_priv(sw->tb);
77 tb_switch_for_each_port(sw, port) {
78 if (!tb_port_is_dpin(port))
81 if (!tb_switch_query_dp_resource(sw, port))
84 list_add_tail(&port->list, &tcm->dp_resources);
85 tb_port_dbg(port, "DP IN resource available\n");
89 static void tb_remove_dp_resources(struct tb_switch *sw)
91 struct tb_cm *tcm = tb_priv(sw->tb);
92 struct tb_port *port, *tmp;
94 /* Clear children resources first */
95 tb_switch_for_each_port(sw, port) {
96 if (tb_port_has_remote(port))
97 tb_remove_dp_resources(port->remote->sw);
100 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
101 if (port->sw == sw) {
102 tb_port_dbg(port, "DP OUT resource unavailable\n");
103 list_del_init(&port->list);
108 static void tb_switch_discover_tunnels(struct tb_switch *sw,
109 struct list_head *list,
112 struct tb *tb = sw->tb;
113 struct tb_port *port;
115 tb_switch_for_each_port(sw, port) {
116 struct tb_tunnel *tunnel = NULL;
118 switch (port->config.type) {
119 case TB_TYPE_DP_HDMI_IN:
120 tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
123 case TB_TYPE_PCIE_DOWN:
124 tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
127 case TB_TYPE_USB3_DOWN:
128 tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
136 list_add_tail(&tunnel->list, list);
139 tb_switch_for_each_port(sw, port) {
140 if (tb_port_has_remote(port)) {
141 tb_switch_discover_tunnels(port->remote->sw, list,
147 static void tb_discover_tunnels(struct tb *tb)
149 struct tb_cm *tcm = tb_priv(tb);
150 struct tb_tunnel *tunnel;
152 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
154 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
155 if (tb_tunnel_is_pci(tunnel)) {
156 struct tb_switch *parent = tunnel->dst_port->sw;
158 while (parent != tunnel->src_port->sw) {
160 parent = tb_switch_parent(parent);
162 } else if (tb_tunnel_is_dp(tunnel)) {
163 /* Keep the domain from powering down */
164 pm_runtime_get_sync(&tunnel->src_port->sw->dev);
165 pm_runtime_get_sync(&tunnel->dst_port->sw->dev);
170 static int tb_port_configure_xdomain(struct tb_port *port)
172 if (tb_switch_is_usb4(port->sw))
173 return usb4_port_configure_xdomain(port);
174 return tb_lc_configure_xdomain(port);
177 static void tb_port_unconfigure_xdomain(struct tb_port *port)
179 if (tb_switch_is_usb4(port->sw))
180 usb4_port_unconfigure_xdomain(port);
182 tb_lc_unconfigure_xdomain(port);
184 tb_port_enable(port->dual_link_port);
187 static void tb_scan_xdomain(struct tb_port *port)
189 struct tb_switch *sw = port->sw;
190 struct tb *tb = sw->tb;
191 struct tb_xdomain *xd;
194 if (!tb_is_xdomain_enabled())
197 route = tb_downstream_route(port);
198 xd = tb_xdomain_find_by_route(tb, route);
204 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
207 tb_port_at(route, sw)->xdomain = xd;
208 tb_port_configure_xdomain(port);
213 static int tb_enable_tmu(struct tb_switch *sw)
217 /* If it is already enabled in correct mode, don't touch it */
218 if (tb_switch_tmu_hifi_is_enabled(sw, sw->tmu.unidirectional_request))
221 ret = tb_switch_tmu_disable(sw);
225 ret = tb_switch_tmu_post_time(sw);
229 return tb_switch_tmu_enable(sw);
233 * tb_find_unused_port() - return the first inactive port on @sw
234 * @sw: Switch to find the port on
235 * @type: Port type to look for
237 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
238 enum tb_port_type type)
240 struct tb_port *port;
242 tb_switch_for_each_port(sw, port) {
243 if (tb_is_upstream_port(port))
245 if (port->config.type != type)
249 if (tb_port_is_enabled(port))
256 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
257 const struct tb_port *port)
259 struct tb_port *down;
261 down = usb4_switch_map_usb3_down(sw, port);
262 if (down && !tb_usb3_port_is_enabled(down))
267 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
268 struct tb_port *src_port,
269 struct tb_port *dst_port)
271 struct tb_cm *tcm = tb_priv(tb);
272 struct tb_tunnel *tunnel;
274 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
275 if (tunnel->type == type &&
276 ((src_port && src_port == tunnel->src_port) ||
277 (dst_port && dst_port == tunnel->dst_port))) {
285 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
286 struct tb_port *src_port,
287 struct tb_port *dst_port)
289 struct tb_port *port, *usb3_down;
290 struct tb_switch *sw;
292 /* Pick the router that is deepest in the topology */
293 if (dst_port->sw->config.depth > src_port->sw->config.depth)
298 /* Can't be the host router */
299 if (sw == tb->root_switch)
302 /* Find the downstream USB4 port that leads to this router */
303 port = tb_port_at(tb_route(sw), tb->root_switch);
304 /* Find the corresponding host router USB3 downstream port */
305 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
309 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
312 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
313 struct tb_port *dst_port, int *available_up, int *available_down)
315 int usb3_consumed_up, usb3_consumed_down, ret;
316 struct tb_cm *tcm = tb_priv(tb);
317 struct tb_tunnel *tunnel;
318 struct tb_port *port;
320 tb_port_dbg(dst_port, "calculating available bandwidth\n");
322 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
324 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
325 &usb3_consumed_down);
329 usb3_consumed_up = 0;
330 usb3_consumed_down = 0;
333 *available_up = *available_down = 40000;
335 /* Find the minimum available bandwidth over all links */
336 tb_for_each_port_on_path(src_port, dst_port, port) {
337 int link_speed, link_width, up_bw, down_bw;
339 if (!tb_port_is_null(port))
342 if (tb_is_upstream_port(port)) {
343 link_speed = port->sw->link_speed;
345 link_speed = tb_port_get_link_speed(port);
350 link_width = port->bonded ? 2 : 1;
352 up_bw = link_speed * link_width * 1000; /* Mb/s */
353 /* Leave 10% guard band */
357 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
360 * Find all DP tunnels that cross the port and reduce
361 * their consumed bandwidth from the available.
363 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
364 int dp_consumed_up, dp_consumed_down;
366 if (!tb_tunnel_is_dp(tunnel))
369 if (!tb_tunnel_port_on_path(tunnel, port))
372 ret = tb_tunnel_consumed_bandwidth(tunnel,
378 up_bw -= dp_consumed_up;
379 down_bw -= dp_consumed_down;
383 * If USB3 is tunneled from the host router down to the
384 * branch leading to port we need to take USB3 consumed
385 * bandwidth into account regardless whether it actually
388 up_bw -= usb3_consumed_up;
389 down_bw -= usb3_consumed_down;
391 if (up_bw < *available_up)
392 *available_up = up_bw;
393 if (down_bw < *available_down)
394 *available_down = down_bw;
397 if (*available_up < 0)
399 if (*available_down < 0)
405 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
406 struct tb_port *src_port,
407 struct tb_port *dst_port)
409 struct tb_tunnel *tunnel;
411 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
412 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
415 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
416 struct tb_port *dst_port)
418 int ret, available_up, available_down;
419 struct tb_tunnel *tunnel;
421 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
425 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
428 * Calculate available bandwidth for the first hop USB3 tunnel.
429 * That determines the whole USB3 bandwidth for this branch.
431 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
432 &available_up, &available_down);
434 tb_warn(tb, "failed to calculate available bandwidth\n");
438 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
439 available_up, available_down);
441 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
444 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
446 struct tb_switch *parent = tb_switch_parent(sw);
447 int ret, available_up, available_down;
448 struct tb_port *up, *down, *port;
449 struct tb_cm *tcm = tb_priv(tb);
450 struct tb_tunnel *tunnel;
452 if (!tb_acpi_may_tunnel_usb3()) {
453 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
457 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
465 * Look up available down port. Since we are chaining it should
466 * be found right above this switch.
468 port = tb_port_at(tb_route(sw), parent);
469 down = tb_find_usb3_down(parent, port);
473 if (tb_route(parent)) {
474 struct tb_port *parent_up;
476 * Check first that the parent switch has its upstream USB3
477 * port enabled. Otherwise the chain is not complete and
478 * there is no point setting up a new tunnel.
480 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
481 if (!parent_up || !tb_port_is_enabled(parent_up))
484 /* Make all unused bandwidth available for the new tunnel */
485 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
490 ret = tb_available_bandwidth(tb, down, up, &available_up,
495 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
496 available_up, available_down);
498 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
505 if (tb_tunnel_activate(tunnel)) {
507 "USB3 tunnel activation failed, aborting\n");
512 list_add_tail(&tunnel->list, &tcm->tunnel_list);
513 if (tb_route(parent))
514 tb_reclaim_usb3_bandwidth(tb, down, up);
519 tb_tunnel_free(tunnel);
521 if (tb_route(parent))
522 tb_reclaim_usb3_bandwidth(tb, down, up);
527 static int tb_create_usb3_tunnels(struct tb_switch *sw)
529 struct tb_port *port;
532 if (!tb_acpi_may_tunnel_usb3())
536 ret = tb_tunnel_usb3(sw->tb, sw);
541 tb_switch_for_each_port(sw, port) {
542 if (!tb_port_has_remote(port))
544 ret = tb_create_usb3_tunnels(port->remote->sw);
552 static void tb_scan_port(struct tb_port *port);
555 * tb_scan_switch() - scan for and initialize downstream switches
557 static void tb_scan_switch(struct tb_switch *sw)
559 struct tb_port *port;
561 pm_runtime_get_sync(&sw->dev);
563 tb_switch_for_each_port(sw, port)
566 pm_runtime_mark_last_busy(&sw->dev);
567 pm_runtime_put_autosuspend(&sw->dev);
571 * tb_scan_port() - check for and initialize switches below port
573 static void tb_scan_port(struct tb_port *port)
575 struct tb_cm *tcm = tb_priv(port->sw->tb);
576 struct tb_port *upstream_port;
577 struct tb_switch *sw;
579 if (tb_is_upstream_port(port))
582 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
583 !tb_dp_port_is_enabled(port)) {
584 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
585 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
590 if (port->config.type != TB_TYPE_PORT)
592 if (port->dual_link_port && port->link_nr)
594 * Downstream switch is reachable through two ports.
595 * Only scan on the primary port (link_nr == 0).
597 if (tb_wait_for_port(port, false) <= 0)
600 tb_port_dbg(port, "port already has a remote\n");
604 tb_retimer_scan(port, true);
606 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
607 tb_downstream_route(port));
610 * If there is an error accessing the connected switch
611 * it may be connected to another domain. Also we allow
612 * the other domain to be connected to a max depth switch.
614 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
615 tb_scan_xdomain(port);
619 if (tb_switch_configure(sw)) {
625 * If there was previously another domain connected remove it
629 tb_xdomain_remove(port->xdomain);
630 tb_port_unconfigure_xdomain(port);
631 port->xdomain = NULL;
635 * Do not send uevents until we have discovered all existing
636 * tunnels and know which switches were authorized already by
639 if (!tcm->hotplug_active)
640 dev_set_uevent_suppress(&sw->dev, true);
643 * At the moment Thunderbolt 2 and beyond (devices with LC) we
644 * can support runtime PM.
646 sw->rpm = sw->generation > 1;
648 if (tb_switch_add(sw)) {
653 /* Link the switches using both links if available */
654 upstream_port = tb_upstream_port(sw);
655 port->remote = upstream_port;
656 upstream_port->remote = port;
657 if (port->dual_link_port && upstream_port->dual_link_port) {
658 port->dual_link_port->remote = upstream_port->dual_link_port;
659 upstream_port->dual_link_port->remote = port->dual_link_port;
662 /* Enable lane bonding if supported */
663 tb_switch_lane_bonding_enable(sw);
664 /* Set the link configured */
665 tb_switch_configure_link(sw);
666 if (tb_switch_enable_clx(sw, TB_CL0S))
667 tb_sw_warn(sw, "failed to enable CLx on upstream port\n");
669 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI,
670 tb_switch_is_clx_enabled(sw));
672 if (tb_enable_tmu(sw))
673 tb_sw_warn(sw, "failed to enable TMU\n");
675 /* Scan upstream retimers */
676 tb_retimer_scan(upstream_port, true);
679 * Create USB 3.x tunnels only when the switch is plugged to the
680 * domain. This is because we scan the domain also during discovery
681 * and want to discover existing USB 3.x tunnels before we create
684 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
685 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
687 tb_add_dp_resources(sw);
691 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
693 struct tb_port *src_port, *dst_port;
699 tb_tunnel_deactivate(tunnel);
700 list_del(&tunnel->list);
703 src_port = tunnel->src_port;
704 dst_port = tunnel->dst_port;
706 switch (tunnel->type) {
709 * In case of DP tunnel make sure the DP IN resource is
710 * deallocated properly.
712 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
713 /* Now we can allow the domain to runtime suspend again */
714 pm_runtime_mark_last_busy(&dst_port->sw->dev);
715 pm_runtime_put_autosuspend(&dst_port->sw->dev);
716 pm_runtime_mark_last_busy(&src_port->sw->dev);
717 pm_runtime_put_autosuspend(&src_port->sw->dev);
721 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
726 * PCIe and DMA tunnels do not consume guaranteed
732 tb_tunnel_free(tunnel);
736 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
738 static void tb_free_invalid_tunnels(struct tb *tb)
740 struct tb_cm *tcm = tb_priv(tb);
741 struct tb_tunnel *tunnel;
744 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
745 if (tb_tunnel_is_invalid(tunnel))
746 tb_deactivate_and_free_tunnel(tunnel);
751 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
753 static void tb_free_unplugged_children(struct tb_switch *sw)
755 struct tb_port *port;
757 tb_switch_for_each_port(sw, port) {
758 if (!tb_port_has_remote(port))
761 if (port->remote->sw->is_unplugged) {
762 tb_retimer_remove_all(port);
763 tb_remove_dp_resources(port->remote->sw);
764 tb_switch_unconfigure_link(port->remote->sw);
765 tb_switch_lane_bonding_disable(port->remote->sw);
766 tb_switch_remove(port->remote->sw);
768 if (port->dual_link_port)
769 port->dual_link_port->remote = NULL;
771 tb_free_unplugged_children(port->remote->sw);
776 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
777 const struct tb_port *port)
779 struct tb_port *down = NULL;
782 * To keep plugging devices consistently in the same PCIe
783 * hierarchy, do mapping here for switch downstream PCIe ports.
785 if (tb_switch_is_usb4(sw)) {
786 down = usb4_switch_map_pcie_down(sw, port);
787 } else if (!tb_route(sw)) {
788 int phy_port = tb_phy_port_from_link(port->port);
792 * Hard-coded Thunderbolt port to PCIe down port mapping
795 if (tb_switch_is_cactus_ridge(sw) ||
796 tb_switch_is_alpine_ridge(sw))
797 index = !phy_port ? 6 : 7;
798 else if (tb_switch_is_falcon_ridge(sw))
799 index = !phy_port ? 6 : 8;
800 else if (tb_switch_is_titan_ridge(sw))
801 index = !phy_port ? 8 : 9;
805 /* Validate the hard-coding */
806 if (WARN_ON(index > sw->config.max_port_number))
809 down = &sw->ports[index];
813 if (WARN_ON(!tb_port_is_pcie_down(down)))
815 if (tb_pci_port_is_enabled(down))
822 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
825 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
827 struct tb_port *host_port, *port;
828 struct tb_cm *tcm = tb_priv(tb);
830 host_port = tb_route(in->sw) ?
831 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
833 list_for_each_entry(port, &tcm->dp_resources, list) {
834 if (!tb_port_is_dpout(port))
837 if (tb_port_is_enabled(port)) {
838 tb_port_dbg(port, "in use\n");
842 tb_port_dbg(port, "DP OUT available\n");
845 * Keep the DP tunnel under the topology starting from
846 * the same host router downstream port.
848 if (host_port && tb_route(port->sw)) {
851 p = tb_port_at(tb_route(port->sw), tb->root_switch);
862 static void tb_tunnel_dp(struct tb *tb)
864 int available_up, available_down, ret, link_nr;
865 struct tb_cm *tcm = tb_priv(tb);
866 struct tb_port *port, *in, *out;
867 struct tb_tunnel *tunnel;
869 if (!tb_acpi_may_tunnel_dp()) {
870 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
875 * Find pair of inactive DP IN and DP OUT adapters and then
876 * establish a DP tunnel between them.
878 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
882 list_for_each_entry(port, &tcm->dp_resources, list) {
883 if (!tb_port_is_dpin(port))
886 if (tb_port_is_enabled(port)) {
887 tb_port_dbg(port, "in use\n");
891 tb_port_dbg(port, "DP IN available\n");
893 out = tb_find_dp_out(tb, port);
901 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
905 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
910 * This is only applicable to links that are not bonded (so
911 * when Thunderbolt 1 hardware is involved somewhere in the
912 * topology). For these try to share the DP bandwidth between
916 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
917 if (tb_tunnel_is_dp(tunnel)) {
924 * DP stream needs the domain to be active so runtime resume
925 * both ends of the tunnel.
927 * This should bring the routers in the middle active as well
928 * and keeps the domain from runtime suspending while the DP
931 pm_runtime_get_sync(&in->sw->dev);
932 pm_runtime_get_sync(&out->sw->dev);
934 if (tb_switch_alloc_dp_resource(in->sw, in)) {
935 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
939 /* Make all unused USB3 bandwidth available for the new DP tunnel */
940 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
942 tb_warn(tb, "failed to release unused bandwidth\n");
946 ret = tb_available_bandwidth(tb, in, out, &available_up,
951 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
952 available_up, available_down);
954 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
957 tb_port_dbg(out, "could not allocate DP tunnel\n");
961 if (tb_tunnel_activate(tunnel)) {
962 tb_port_info(out, "DP tunnel activation failed, aborting\n");
966 list_add_tail(&tunnel->list, &tcm->tunnel_list);
967 tb_reclaim_usb3_bandwidth(tb, in, out);
971 tb_tunnel_free(tunnel);
973 tb_reclaim_usb3_bandwidth(tb, in, out);
975 tb_switch_dealloc_dp_resource(in->sw, in);
977 pm_runtime_mark_last_busy(&out->sw->dev);
978 pm_runtime_put_autosuspend(&out->sw->dev);
979 pm_runtime_mark_last_busy(&in->sw->dev);
980 pm_runtime_put_autosuspend(&in->sw->dev);
983 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
985 struct tb_port *in, *out;
986 struct tb_tunnel *tunnel;
988 if (tb_port_is_dpin(port)) {
989 tb_port_dbg(port, "DP IN resource unavailable\n");
993 tb_port_dbg(port, "DP OUT resource unavailable\n");
998 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
999 tb_deactivate_and_free_tunnel(tunnel);
1000 list_del_init(&port->list);
1003 * See if there is another DP OUT port that can be used for
1004 * to create another tunnel.
1009 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1011 struct tb_cm *tcm = tb_priv(tb);
1014 if (tb_port_is_enabled(port))
1017 list_for_each_entry(p, &tcm->dp_resources, list) {
1022 tb_port_dbg(port, "DP %s resource available\n",
1023 tb_port_is_dpin(port) ? "IN" : "OUT");
1024 list_add_tail(&port->list, &tcm->dp_resources);
1026 /* Look for suitable DP IN <-> DP OUT pairs now */
1030 static void tb_disconnect_and_release_dp(struct tb *tb)
1032 struct tb_cm *tcm = tb_priv(tb);
1033 struct tb_tunnel *tunnel, *n;
1036 * Tear down all DP tunnels and release their resources. They
1037 * will be re-established after resume based on plug events.
1039 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1040 if (tb_tunnel_is_dp(tunnel))
1041 tb_deactivate_and_free_tunnel(tunnel);
1044 while (!list_empty(&tcm->dp_resources)) {
1045 struct tb_port *port;
1047 port = list_first_entry(&tcm->dp_resources,
1048 struct tb_port, list);
1049 list_del_init(&port->list);
1053 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1055 struct tb_tunnel *tunnel;
1058 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1062 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1063 if (WARN_ON(!tunnel))
1066 tb_switch_xhci_disconnect(sw);
1068 tb_tunnel_deactivate(tunnel);
1069 list_del(&tunnel->list);
1070 tb_tunnel_free(tunnel);
1074 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1076 struct tb_port *up, *down, *port;
1077 struct tb_cm *tcm = tb_priv(tb);
1078 struct tb_switch *parent_sw;
1079 struct tb_tunnel *tunnel;
1081 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1086 * Look up available down port. Since we are chaining it should
1087 * be found right above this switch.
1089 parent_sw = tb_to_switch(sw->dev.parent);
1090 port = tb_port_at(tb_route(sw), parent_sw);
1091 down = tb_find_pcie_down(parent_sw, port);
1095 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1099 if (tb_tunnel_activate(tunnel)) {
1101 "PCIe tunnel activation failed, aborting\n");
1102 tb_tunnel_free(tunnel);
1107 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
1110 if (tb_switch_pcie_l1_enable(sw))
1111 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
1113 if (tb_switch_xhci_connect(sw))
1114 tb_sw_warn(sw, "failed to connect xHCI\n");
1116 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1120 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1121 int transmit_path, int transmit_ring,
1122 int receive_path, int receive_ring)
1124 struct tb_cm *tcm = tb_priv(tb);
1125 struct tb_port *nhi_port, *dst_port;
1126 struct tb_tunnel *tunnel;
1127 struct tb_switch *sw;
1129 sw = tb_to_switch(xd->dev.parent);
1130 dst_port = tb_port_at(xd->route, sw);
1131 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1133 mutex_lock(&tb->lock);
1134 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1135 transmit_ring, receive_path, receive_ring);
1137 mutex_unlock(&tb->lock);
1141 if (tb_tunnel_activate(tunnel)) {
1142 tb_port_info(nhi_port,
1143 "DMA tunnel activation failed, aborting\n");
1144 tb_tunnel_free(tunnel);
1145 mutex_unlock(&tb->lock);
1149 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1150 mutex_unlock(&tb->lock);
1154 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1155 int transmit_path, int transmit_ring,
1156 int receive_path, int receive_ring)
1158 struct tb_cm *tcm = tb_priv(tb);
1159 struct tb_port *nhi_port, *dst_port;
1160 struct tb_tunnel *tunnel, *n;
1161 struct tb_switch *sw;
1163 sw = tb_to_switch(xd->dev.parent);
1164 dst_port = tb_port_at(xd->route, sw);
1165 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1167 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1168 if (!tb_tunnel_is_dma(tunnel))
1170 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1173 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1174 receive_path, receive_ring))
1175 tb_deactivate_and_free_tunnel(tunnel);
1179 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1180 int transmit_path, int transmit_ring,
1181 int receive_path, int receive_ring)
1183 if (!xd->is_unplugged) {
1184 mutex_lock(&tb->lock);
1185 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1186 transmit_ring, receive_path,
1188 mutex_unlock(&tb->lock);
1193 /* hotplug handling */
1196 * tb_handle_hotplug() - handle hotplug event
1198 * Executes on tb->wq.
1200 static void tb_handle_hotplug(struct work_struct *work)
1202 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1203 struct tb *tb = ev->tb;
1204 struct tb_cm *tcm = tb_priv(tb);
1205 struct tb_switch *sw;
1206 struct tb_port *port;
1208 /* Bring the domain back from sleep if it was suspended */
1209 pm_runtime_get_sync(&tb->dev);
1211 mutex_lock(&tb->lock);
1212 if (!tcm->hotplug_active)
1213 goto out; /* during init, suspend or shutdown */
1215 sw = tb_switch_find_by_route(tb, ev->route);
1218 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1219 ev->route, ev->port, ev->unplug);
1222 if (ev->port > sw->config.max_port_number) {
1224 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1225 ev->route, ev->port, ev->unplug);
1228 port = &sw->ports[ev->port];
1229 if (tb_is_upstream_port(port)) {
1230 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1231 ev->route, ev->port, ev->unplug);
1235 pm_runtime_get_sync(&sw->dev);
1238 tb_retimer_remove_all(port);
1240 if (tb_port_has_remote(port)) {
1241 tb_port_dbg(port, "switch unplugged\n");
1242 tb_sw_set_unplugged(port->remote->sw);
1243 tb_free_invalid_tunnels(tb);
1244 tb_remove_dp_resources(port->remote->sw);
1245 tb_switch_tmu_disable(port->remote->sw);
1246 tb_switch_unconfigure_link(port->remote->sw);
1247 tb_switch_lane_bonding_disable(port->remote->sw);
1248 tb_switch_remove(port->remote->sw);
1249 port->remote = NULL;
1250 if (port->dual_link_port)
1251 port->dual_link_port->remote = NULL;
1252 /* Maybe we can create another DP tunnel */
1254 } else if (port->xdomain) {
1255 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1257 tb_port_dbg(port, "xdomain unplugged\n");
1259 * Service drivers are unbound during
1260 * tb_xdomain_remove() so setting XDomain as
1261 * unplugged here prevents deadlock if they call
1262 * tb_xdomain_disable_paths(). We will tear down
1263 * all the tunnels below.
1265 xd->is_unplugged = true;
1266 tb_xdomain_remove(xd);
1267 port->xdomain = NULL;
1268 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1270 tb_port_unconfigure_xdomain(port);
1271 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1272 tb_dp_resource_unavailable(tb, port);
1273 } else if (!port->port) {
1274 tb_sw_dbg(sw, "xHCI disconnect request\n");
1275 tb_switch_xhci_disconnect(sw);
1278 "got unplug event for disconnected port, ignoring\n");
1280 } else if (port->remote) {
1281 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1282 } else if (!port->port && sw->authorized) {
1283 tb_sw_dbg(sw, "xHCI connect request\n");
1284 tb_switch_xhci_connect(sw);
1286 if (tb_port_is_null(port)) {
1287 tb_port_dbg(port, "hotplug: scanning\n");
1290 tb_port_dbg(port, "hotplug: no switch found\n");
1291 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1292 tb_dp_resource_available(tb, port);
1296 pm_runtime_mark_last_busy(&sw->dev);
1297 pm_runtime_put_autosuspend(&sw->dev);
1302 mutex_unlock(&tb->lock);
1304 pm_runtime_mark_last_busy(&tb->dev);
1305 pm_runtime_put_autosuspend(&tb->dev);
1311 * tb_schedule_hotplug_handler() - callback function for the control channel
1313 * Delegates to tb_handle_hotplug.
1315 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1316 const void *buf, size_t size)
1318 const struct cfg_event_pkg *pkg = buf;
1321 if (type != TB_CFG_PKG_EVENT) {
1322 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1326 route = tb_cfg_get_route(&pkg->header);
1328 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1329 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1333 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1336 static void tb_stop(struct tb *tb)
1338 struct tb_cm *tcm = tb_priv(tb);
1339 struct tb_tunnel *tunnel;
1340 struct tb_tunnel *n;
1342 cancel_delayed_work(&tcm->remove_work);
1343 /* tunnels are only present after everything has been initialized */
1344 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1346 * DMA tunnels require the driver to be functional so we
1347 * tear them down. Other protocol tunnels can be left
1350 if (tb_tunnel_is_dma(tunnel))
1351 tb_tunnel_deactivate(tunnel);
1352 tb_tunnel_free(tunnel);
1354 tb_switch_remove(tb->root_switch);
1355 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1358 static int tb_scan_finalize_switch(struct device *dev, void *data)
1360 if (tb_is_switch(dev)) {
1361 struct tb_switch *sw = tb_to_switch(dev);
1364 * If we found that the switch was already setup by the
1365 * boot firmware, mark it as authorized now before we
1366 * send uevent to userspace.
1371 dev_set_uevent_suppress(dev, false);
1372 kobject_uevent(&dev->kobj, KOBJ_ADD);
1373 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1379 static int tb_start(struct tb *tb)
1381 struct tb_cm *tcm = tb_priv(tb);
1384 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1385 if (IS_ERR(tb->root_switch))
1386 return PTR_ERR(tb->root_switch);
1389 * ICM firmware upgrade needs running firmware and in native
1390 * mode that is not available so disable firmware upgrade of the
1393 tb->root_switch->no_nvm_upgrade = true;
1394 /* All USB4 routers support runtime PM */
1395 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1397 ret = tb_switch_configure(tb->root_switch);
1399 tb_switch_put(tb->root_switch);
1403 /* Announce the switch to the world */
1404 ret = tb_switch_add(tb->root_switch);
1406 tb_switch_put(tb->root_switch);
1410 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_RATE_HIFI, false);
1411 /* Enable TMU if it is off */
1412 tb_switch_tmu_enable(tb->root_switch);
1413 /* Full scan to discover devices added before the driver was loaded. */
1414 tb_scan_switch(tb->root_switch);
1415 /* Find out tunnels created by the boot firmware */
1416 tb_discover_tunnels(tb);
1418 * If the boot firmware did not create USB 3.x tunnels create them
1419 * now for the whole topology.
1421 tb_create_usb3_tunnels(tb->root_switch);
1422 /* Add DP IN resources for the root switch */
1423 tb_add_dp_resources(tb->root_switch);
1424 /* Make the discovered switches available to the userspace */
1425 device_for_each_child(&tb->root_switch->dev, NULL,
1426 tb_scan_finalize_switch);
1428 /* Allow tb_handle_hotplug to progress events */
1429 tcm->hotplug_active = true;
1433 static int tb_suspend_noirq(struct tb *tb)
1435 struct tb_cm *tcm = tb_priv(tb);
1437 tb_dbg(tb, "suspending...\n");
1438 tb_disconnect_and_release_dp(tb);
1439 tb_switch_suspend(tb->root_switch, false);
1440 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1441 tb_dbg(tb, "suspend finished\n");
1446 static void tb_restore_children(struct tb_switch *sw)
1448 struct tb_port *port;
1450 /* No need to restore if the router is already unplugged */
1451 if (sw->is_unplugged)
1454 if (tb_switch_enable_clx(sw, TB_CL0S))
1455 tb_sw_warn(sw, "failed to re-enable CLx on upstream port\n");
1458 * tb_switch_tmu_configure() was already called when the switch was
1459 * added before entering system sleep or runtime suspend,
1460 * so no need to call it again before enabling TMU.
1462 if (tb_enable_tmu(sw))
1463 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1465 tb_switch_for_each_port(sw, port) {
1466 if (!tb_port_has_remote(port) && !port->xdomain)
1470 tb_switch_lane_bonding_enable(port->remote->sw);
1471 tb_switch_configure_link(port->remote->sw);
1473 tb_restore_children(port->remote->sw);
1474 } else if (port->xdomain) {
1475 tb_port_configure_xdomain(port);
1480 static int tb_resume_noirq(struct tb *tb)
1482 struct tb_cm *tcm = tb_priv(tb);
1483 struct tb_tunnel *tunnel, *n;
1484 unsigned int usb3_delay = 0;
1487 tb_dbg(tb, "resuming...\n");
1489 /* remove any pci devices the firmware might have setup */
1490 tb_switch_reset(tb->root_switch);
1492 tb_switch_resume(tb->root_switch);
1493 tb_free_invalid_tunnels(tb);
1494 tb_free_unplugged_children(tb->root_switch);
1495 tb_restore_children(tb->root_switch);
1498 * If we get here from suspend to disk the boot firmware or the
1499 * restore kernel might have created tunnels of its own. Since
1500 * we cannot be sure they are usable for us we find and tear
1503 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
1504 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
1505 if (tb_tunnel_is_usb3(tunnel))
1507 tb_tunnel_deactivate(tunnel);
1508 tb_tunnel_free(tunnel);
1511 /* Re-create our tunnels now */
1512 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1513 /* USB3 requires delay before it can be re-activated */
1514 if (tb_tunnel_is_usb3(tunnel)) {
1516 /* Only need to do it once */
1519 tb_tunnel_restart(tunnel);
1521 if (!list_empty(&tcm->tunnel_list)) {
1523 * the pcie links need some time to get going.
1524 * 100ms works for me...
1526 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1529 /* Allow tb_handle_hotplug to progress events */
1530 tcm->hotplug_active = true;
1531 tb_dbg(tb, "resume finished\n");
1536 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1538 struct tb_port *port;
1541 tb_switch_for_each_port(sw, port) {
1542 if (tb_is_upstream_port(port))
1544 if (port->xdomain && port->xdomain->is_unplugged) {
1545 tb_retimer_remove_all(port);
1546 tb_xdomain_remove(port->xdomain);
1547 tb_port_unconfigure_xdomain(port);
1548 port->xdomain = NULL;
1550 } else if (port->remote) {
1551 ret += tb_free_unplugged_xdomains(port->remote->sw);
1558 static int tb_freeze_noirq(struct tb *tb)
1560 struct tb_cm *tcm = tb_priv(tb);
1562 tcm->hotplug_active = false;
1566 static int tb_thaw_noirq(struct tb *tb)
1568 struct tb_cm *tcm = tb_priv(tb);
1570 tcm->hotplug_active = true;
1574 static void tb_complete(struct tb *tb)
1577 * Release any unplugged XDomains and if there is a case where
1578 * another domain is swapped in place of unplugged XDomain we
1579 * need to run another rescan.
1581 mutex_lock(&tb->lock);
1582 if (tb_free_unplugged_xdomains(tb->root_switch))
1583 tb_scan_switch(tb->root_switch);
1584 mutex_unlock(&tb->lock);
1587 static int tb_runtime_suspend(struct tb *tb)
1589 struct tb_cm *tcm = tb_priv(tb);
1591 mutex_lock(&tb->lock);
1592 tb_switch_suspend(tb->root_switch, true);
1593 tcm->hotplug_active = false;
1594 mutex_unlock(&tb->lock);
1599 static void tb_remove_work(struct work_struct *work)
1601 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1602 struct tb *tb = tcm_to_tb(tcm);
1604 mutex_lock(&tb->lock);
1605 if (tb->root_switch) {
1606 tb_free_unplugged_children(tb->root_switch);
1607 tb_free_unplugged_xdomains(tb->root_switch);
1609 mutex_unlock(&tb->lock);
1612 static int tb_runtime_resume(struct tb *tb)
1614 struct tb_cm *tcm = tb_priv(tb);
1615 struct tb_tunnel *tunnel, *n;
1617 mutex_lock(&tb->lock);
1618 tb_switch_resume(tb->root_switch);
1619 tb_free_invalid_tunnels(tb);
1620 tb_restore_children(tb->root_switch);
1621 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1622 tb_tunnel_restart(tunnel);
1623 tcm->hotplug_active = true;
1624 mutex_unlock(&tb->lock);
1627 * Schedule cleanup of any unplugged devices. Run this in a
1628 * separate thread to avoid possible deadlock if the device
1629 * removal runtime resumes the unplugged device.
1631 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1635 static const struct tb_cm_ops tb_cm_ops = {
1638 .suspend_noirq = tb_suspend_noirq,
1639 .resume_noirq = tb_resume_noirq,
1640 .freeze_noirq = tb_freeze_noirq,
1641 .thaw_noirq = tb_thaw_noirq,
1642 .complete = tb_complete,
1643 .runtime_suspend = tb_runtime_suspend,
1644 .runtime_resume = tb_runtime_resume,
1645 .handle_event = tb_handle_event,
1646 .disapprove_switch = tb_disconnect_pci,
1647 .approve_switch = tb_tunnel_pci,
1648 .approve_xdomain_paths = tb_approve_xdomain_paths,
1649 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1653 * During suspend the Thunderbolt controller is reset and all PCIe
1654 * tunnels are lost. The NHI driver will try to reestablish all tunnels
1655 * during resume. This adds device links between the tunneled PCIe
1656 * downstream ports and the NHI so that the device core will make sure
1657 * NHI is resumed first before the rest.
1659 static void tb_apple_add_links(struct tb_nhi *nhi)
1661 struct pci_dev *upstream, *pdev;
1663 if (!x86_apple_machine)
1666 switch (nhi->pdev->device) {
1667 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1668 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1669 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1670 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1676 upstream = pci_upstream_bridge(nhi->pdev);
1678 if (!pci_is_pcie(upstream))
1680 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
1682 upstream = pci_upstream_bridge(upstream);
1689 * For each hotplug downstream port, create add device link
1690 * back to NHI so that PCIe tunnels can be re-established after
1693 for_each_pci_bridge(pdev, upstream->subordinate) {
1694 const struct device_link *link;
1696 if (!pci_is_pcie(pdev))
1698 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
1699 !pdev->is_hotplug_bridge)
1702 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
1703 DL_FLAG_AUTOREMOVE_SUPPLIER |
1704 DL_FLAG_PM_RUNTIME);
1706 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
1707 dev_name(&pdev->dev));
1709 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
1710 dev_name(&pdev->dev));
1715 struct tb *tb_probe(struct tb_nhi *nhi)
1720 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1724 if (tb_acpi_may_tunnel_pcie())
1725 tb->security_level = TB_SECURITY_USER;
1727 tb->security_level = TB_SECURITY_NOPCIE;
1729 tb->cm_ops = &tb_cm_ops;
1732 INIT_LIST_HEAD(&tcm->tunnel_list);
1733 INIT_LIST_HEAD(&tcm->dp_resources);
1734 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1736 tb_dbg(tb, "using software connection manager\n");
1738 tb_apple_add_links(nhi);
1739 tb_acpi_add_links(nhi);