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)
173 * XDomain paths currently only support single lane so we must
174 * disable the other lane according to USB4 spec.
176 tb_port_disable(port->dual_link_port);
178 if (tb_switch_is_usb4(port->sw))
179 return usb4_port_configure_xdomain(port);
180 return tb_lc_configure_xdomain(port);
183 static void tb_port_unconfigure_xdomain(struct tb_port *port)
185 if (tb_switch_is_usb4(port->sw))
186 usb4_port_unconfigure_xdomain(port);
188 tb_lc_unconfigure_xdomain(port);
190 tb_port_enable(port->dual_link_port);
193 static void tb_scan_xdomain(struct tb_port *port)
195 struct tb_switch *sw = port->sw;
196 struct tb *tb = sw->tb;
197 struct tb_xdomain *xd;
200 if (!tb_is_xdomain_enabled())
203 route = tb_downstream_route(port);
204 xd = tb_xdomain_find_by_route(tb, route);
210 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
213 tb_port_at(route, sw)->xdomain = xd;
214 tb_port_configure_xdomain(port);
219 static int tb_enable_tmu(struct tb_switch *sw)
223 /* If it is already enabled in correct mode, don't touch it */
224 if (tb_switch_tmu_hifi_is_enabled(sw, sw->tmu.unidirectional_request))
227 ret = tb_switch_tmu_disable(sw);
231 ret = tb_switch_tmu_post_time(sw);
235 return tb_switch_tmu_enable(sw);
239 * tb_find_unused_port() - return the first inactive port on @sw
240 * @sw: Switch to find the port on
241 * @type: Port type to look for
243 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
244 enum tb_port_type type)
246 struct tb_port *port;
248 tb_switch_for_each_port(sw, port) {
249 if (tb_is_upstream_port(port))
251 if (port->config.type != type)
255 if (tb_port_is_enabled(port))
262 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
263 const struct tb_port *port)
265 struct tb_port *down;
267 down = usb4_switch_map_usb3_down(sw, port);
268 if (down && !tb_usb3_port_is_enabled(down))
273 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
274 struct tb_port *src_port,
275 struct tb_port *dst_port)
277 struct tb_cm *tcm = tb_priv(tb);
278 struct tb_tunnel *tunnel;
280 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
281 if (tunnel->type == type &&
282 ((src_port && src_port == tunnel->src_port) ||
283 (dst_port && dst_port == tunnel->dst_port))) {
291 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
292 struct tb_port *src_port,
293 struct tb_port *dst_port)
295 struct tb_port *port, *usb3_down;
296 struct tb_switch *sw;
298 /* Pick the router that is deepest in the topology */
299 if (dst_port->sw->config.depth > src_port->sw->config.depth)
304 /* Can't be the host router */
305 if (sw == tb->root_switch)
308 /* Find the downstream USB4 port that leads to this router */
309 port = tb_port_at(tb_route(sw), tb->root_switch);
310 /* Find the corresponding host router USB3 downstream port */
311 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
315 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
318 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
319 struct tb_port *dst_port, int *available_up, int *available_down)
321 int usb3_consumed_up, usb3_consumed_down, ret;
322 struct tb_cm *tcm = tb_priv(tb);
323 struct tb_tunnel *tunnel;
324 struct tb_port *port;
326 tb_port_dbg(dst_port, "calculating available bandwidth\n");
328 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
330 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
331 &usb3_consumed_down);
335 usb3_consumed_up = 0;
336 usb3_consumed_down = 0;
339 *available_up = *available_down = 40000;
341 /* Find the minimum available bandwidth over all links */
342 tb_for_each_port_on_path(src_port, dst_port, port) {
343 int link_speed, link_width, up_bw, down_bw;
345 if (!tb_port_is_null(port))
348 if (tb_is_upstream_port(port)) {
349 link_speed = port->sw->link_speed;
351 link_speed = tb_port_get_link_speed(port);
356 link_width = port->bonded ? 2 : 1;
358 up_bw = link_speed * link_width * 1000; /* Mb/s */
359 /* Leave 10% guard band */
363 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
366 * Find all DP tunnels that cross the port and reduce
367 * their consumed bandwidth from the available.
369 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
370 int dp_consumed_up, dp_consumed_down;
372 if (!tb_tunnel_is_dp(tunnel))
375 if (!tb_tunnel_port_on_path(tunnel, port))
378 ret = tb_tunnel_consumed_bandwidth(tunnel,
384 up_bw -= dp_consumed_up;
385 down_bw -= dp_consumed_down;
389 * If USB3 is tunneled from the host router down to the
390 * branch leading to port we need to take USB3 consumed
391 * bandwidth into account regardless whether it actually
394 up_bw -= usb3_consumed_up;
395 down_bw -= usb3_consumed_down;
397 if (up_bw < *available_up)
398 *available_up = up_bw;
399 if (down_bw < *available_down)
400 *available_down = down_bw;
403 if (*available_up < 0)
405 if (*available_down < 0)
411 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
412 struct tb_port *src_port,
413 struct tb_port *dst_port)
415 struct tb_tunnel *tunnel;
417 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
418 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
421 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
422 struct tb_port *dst_port)
424 int ret, available_up, available_down;
425 struct tb_tunnel *tunnel;
427 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
431 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
434 * Calculate available bandwidth for the first hop USB3 tunnel.
435 * That determines the whole USB3 bandwidth for this branch.
437 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
438 &available_up, &available_down);
440 tb_warn(tb, "failed to calculate available bandwidth\n");
444 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
445 available_up, available_down);
447 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
450 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
452 struct tb_switch *parent = tb_switch_parent(sw);
453 int ret, available_up, available_down;
454 struct tb_port *up, *down, *port;
455 struct tb_cm *tcm = tb_priv(tb);
456 struct tb_tunnel *tunnel;
458 if (!tb_acpi_may_tunnel_usb3()) {
459 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
463 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
471 * Look up available down port. Since we are chaining it should
472 * be found right above this switch.
474 port = tb_port_at(tb_route(sw), parent);
475 down = tb_find_usb3_down(parent, port);
479 if (tb_route(parent)) {
480 struct tb_port *parent_up;
482 * Check first that the parent switch has its upstream USB3
483 * port enabled. Otherwise the chain is not complete and
484 * there is no point setting up a new tunnel.
486 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
487 if (!parent_up || !tb_port_is_enabled(parent_up))
490 /* Make all unused bandwidth available for the new tunnel */
491 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
496 ret = tb_available_bandwidth(tb, down, up, &available_up,
501 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
502 available_up, available_down);
504 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
511 if (tb_tunnel_activate(tunnel)) {
513 "USB3 tunnel activation failed, aborting\n");
518 list_add_tail(&tunnel->list, &tcm->tunnel_list);
519 if (tb_route(parent))
520 tb_reclaim_usb3_bandwidth(tb, down, up);
525 tb_tunnel_free(tunnel);
527 if (tb_route(parent))
528 tb_reclaim_usb3_bandwidth(tb, down, up);
533 static int tb_create_usb3_tunnels(struct tb_switch *sw)
535 struct tb_port *port;
538 if (!tb_acpi_may_tunnel_usb3())
542 ret = tb_tunnel_usb3(sw->tb, sw);
547 tb_switch_for_each_port(sw, port) {
548 if (!tb_port_has_remote(port))
550 ret = tb_create_usb3_tunnels(port->remote->sw);
558 static void tb_scan_port(struct tb_port *port);
561 * tb_scan_switch() - scan for and initialize downstream switches
563 static void tb_scan_switch(struct tb_switch *sw)
565 struct tb_port *port;
567 pm_runtime_get_sync(&sw->dev);
569 tb_switch_for_each_port(sw, port)
572 pm_runtime_mark_last_busy(&sw->dev);
573 pm_runtime_put_autosuspend(&sw->dev);
577 * tb_scan_port() - check for and initialize switches below port
579 static void tb_scan_port(struct tb_port *port)
581 struct tb_cm *tcm = tb_priv(port->sw->tb);
582 struct tb_port *upstream_port;
583 struct tb_switch *sw;
585 if (tb_is_upstream_port(port))
588 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
589 !tb_dp_port_is_enabled(port)) {
590 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
591 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
596 if (port->config.type != TB_TYPE_PORT)
598 if (port->dual_link_port && port->link_nr)
600 * Downstream switch is reachable through two ports.
601 * Only scan on the primary port (link_nr == 0).
603 if (tb_wait_for_port(port, false) <= 0)
606 tb_port_dbg(port, "port already has a remote\n");
610 tb_retimer_scan(port, true);
612 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
613 tb_downstream_route(port));
616 * If there is an error accessing the connected switch
617 * it may be connected to another domain. Also we allow
618 * the other domain to be connected to a max depth switch.
620 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
621 tb_scan_xdomain(port);
625 if (tb_switch_configure(sw)) {
631 * If there was previously another domain connected remove it
635 tb_xdomain_remove(port->xdomain);
636 tb_port_unconfigure_xdomain(port);
637 port->xdomain = NULL;
641 * Do not send uevents until we have discovered all existing
642 * tunnels and know which switches were authorized already by
645 if (!tcm->hotplug_active)
646 dev_set_uevent_suppress(&sw->dev, true);
649 * At the moment Thunderbolt 2 and beyond (devices with LC) we
650 * can support runtime PM.
652 sw->rpm = sw->generation > 1;
654 if (tb_switch_add(sw)) {
659 /* Link the switches using both links if available */
660 upstream_port = tb_upstream_port(sw);
661 port->remote = upstream_port;
662 upstream_port->remote = port;
663 if (port->dual_link_port && upstream_port->dual_link_port) {
664 port->dual_link_port->remote = upstream_port->dual_link_port;
665 upstream_port->dual_link_port->remote = port->dual_link_port;
668 /* Enable lane bonding if supported */
669 tb_switch_lane_bonding_enable(sw);
670 /* Set the link configured */
671 tb_switch_configure_link(sw);
672 if (tb_switch_enable_clx(sw, TB_CL0S))
673 tb_sw_warn(sw, "failed to enable CLx on upstream port\n");
675 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI,
676 tb_switch_is_clx_enabled(sw));
678 if (tb_enable_tmu(sw))
679 tb_sw_warn(sw, "failed to enable TMU\n");
681 /* Scan upstream retimers */
682 tb_retimer_scan(upstream_port, true);
685 * Create USB 3.x tunnels only when the switch is plugged to the
686 * domain. This is because we scan the domain also during discovery
687 * and want to discover existing USB 3.x tunnels before we create
690 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
691 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
693 tb_add_dp_resources(sw);
697 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
699 struct tb_port *src_port, *dst_port;
705 tb_tunnel_deactivate(tunnel);
706 list_del(&tunnel->list);
709 src_port = tunnel->src_port;
710 dst_port = tunnel->dst_port;
712 switch (tunnel->type) {
715 * In case of DP tunnel make sure the DP IN resource is
716 * deallocated properly.
718 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
719 /* Now we can allow the domain to runtime suspend again */
720 pm_runtime_mark_last_busy(&dst_port->sw->dev);
721 pm_runtime_put_autosuspend(&dst_port->sw->dev);
722 pm_runtime_mark_last_busy(&src_port->sw->dev);
723 pm_runtime_put_autosuspend(&src_port->sw->dev);
727 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
732 * PCIe and DMA tunnels do not consume guaranteed
738 tb_tunnel_free(tunnel);
742 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
744 static void tb_free_invalid_tunnels(struct tb *tb)
746 struct tb_cm *tcm = tb_priv(tb);
747 struct tb_tunnel *tunnel;
750 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
751 if (tb_tunnel_is_invalid(tunnel))
752 tb_deactivate_and_free_tunnel(tunnel);
757 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
759 static void tb_free_unplugged_children(struct tb_switch *sw)
761 struct tb_port *port;
763 tb_switch_for_each_port(sw, port) {
764 if (!tb_port_has_remote(port))
767 if (port->remote->sw->is_unplugged) {
768 tb_retimer_remove_all(port);
769 tb_remove_dp_resources(port->remote->sw);
770 tb_switch_unconfigure_link(port->remote->sw);
771 tb_switch_lane_bonding_disable(port->remote->sw);
772 tb_switch_remove(port->remote->sw);
774 if (port->dual_link_port)
775 port->dual_link_port->remote = NULL;
777 tb_free_unplugged_children(port->remote->sw);
782 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
783 const struct tb_port *port)
785 struct tb_port *down = NULL;
788 * To keep plugging devices consistently in the same PCIe
789 * hierarchy, do mapping here for switch downstream PCIe ports.
791 if (tb_switch_is_usb4(sw)) {
792 down = usb4_switch_map_pcie_down(sw, port);
793 } else if (!tb_route(sw)) {
794 int phy_port = tb_phy_port_from_link(port->port);
798 * Hard-coded Thunderbolt port to PCIe down port mapping
801 if (tb_switch_is_cactus_ridge(sw) ||
802 tb_switch_is_alpine_ridge(sw))
803 index = !phy_port ? 6 : 7;
804 else if (tb_switch_is_falcon_ridge(sw))
805 index = !phy_port ? 6 : 8;
806 else if (tb_switch_is_titan_ridge(sw))
807 index = !phy_port ? 8 : 9;
811 /* Validate the hard-coding */
812 if (WARN_ON(index > sw->config.max_port_number))
815 down = &sw->ports[index];
819 if (WARN_ON(!tb_port_is_pcie_down(down)))
821 if (tb_pci_port_is_enabled(down))
828 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
831 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
833 struct tb_port *host_port, *port;
834 struct tb_cm *tcm = tb_priv(tb);
836 host_port = tb_route(in->sw) ?
837 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
839 list_for_each_entry(port, &tcm->dp_resources, list) {
840 if (!tb_port_is_dpout(port))
843 if (tb_port_is_enabled(port)) {
844 tb_port_dbg(port, "in use\n");
848 tb_port_dbg(port, "DP OUT available\n");
851 * Keep the DP tunnel under the topology starting from
852 * the same host router downstream port.
854 if (host_port && tb_route(port->sw)) {
857 p = tb_port_at(tb_route(port->sw), tb->root_switch);
868 static void tb_tunnel_dp(struct tb *tb)
870 int available_up, available_down, ret;
871 struct tb_cm *tcm = tb_priv(tb);
872 struct tb_port *port, *in, *out;
873 struct tb_tunnel *tunnel;
875 if (!tb_acpi_may_tunnel_dp()) {
876 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
881 * Find pair of inactive DP IN and DP OUT adapters and then
882 * establish a DP tunnel between them.
884 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
888 list_for_each_entry(port, &tcm->dp_resources, list) {
889 if (!tb_port_is_dpin(port))
892 if (tb_port_is_enabled(port)) {
893 tb_port_dbg(port, "in use\n");
897 tb_port_dbg(port, "DP IN available\n");
899 out = tb_find_dp_out(tb, port);
907 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
911 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
916 * DP stream needs the domain to be active so runtime resume
917 * both ends of the tunnel.
919 * This should bring the routers in the middle active as well
920 * and keeps the domain from runtime suspending while the DP
923 pm_runtime_get_sync(&in->sw->dev);
924 pm_runtime_get_sync(&out->sw->dev);
926 if (tb_switch_alloc_dp_resource(in->sw, in)) {
927 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
931 /* Make all unused USB3 bandwidth available for the new DP tunnel */
932 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
934 tb_warn(tb, "failed to release unused bandwidth\n");
938 ret = tb_available_bandwidth(tb, in, out, &available_up,
943 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
944 available_up, available_down);
946 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
948 tb_port_dbg(out, "could not allocate DP tunnel\n");
952 if (tb_tunnel_activate(tunnel)) {
953 tb_port_info(out, "DP tunnel activation failed, aborting\n");
957 list_add_tail(&tunnel->list, &tcm->tunnel_list);
958 tb_reclaim_usb3_bandwidth(tb, in, out);
962 tb_tunnel_free(tunnel);
964 tb_reclaim_usb3_bandwidth(tb, in, out);
966 tb_switch_dealloc_dp_resource(in->sw, in);
968 pm_runtime_mark_last_busy(&out->sw->dev);
969 pm_runtime_put_autosuspend(&out->sw->dev);
970 pm_runtime_mark_last_busy(&in->sw->dev);
971 pm_runtime_put_autosuspend(&in->sw->dev);
974 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
976 struct tb_port *in, *out;
977 struct tb_tunnel *tunnel;
979 if (tb_port_is_dpin(port)) {
980 tb_port_dbg(port, "DP IN resource unavailable\n");
984 tb_port_dbg(port, "DP OUT resource unavailable\n");
989 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
990 tb_deactivate_and_free_tunnel(tunnel);
991 list_del_init(&port->list);
994 * See if there is another DP OUT port that can be used for
995 * to create another tunnel.
1000 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1002 struct tb_cm *tcm = tb_priv(tb);
1005 if (tb_port_is_enabled(port))
1008 list_for_each_entry(p, &tcm->dp_resources, list) {
1013 tb_port_dbg(port, "DP %s resource available\n",
1014 tb_port_is_dpin(port) ? "IN" : "OUT");
1015 list_add_tail(&port->list, &tcm->dp_resources);
1017 /* Look for suitable DP IN <-> DP OUT pairs now */
1021 static void tb_disconnect_and_release_dp(struct tb *tb)
1023 struct tb_cm *tcm = tb_priv(tb);
1024 struct tb_tunnel *tunnel, *n;
1027 * Tear down all DP tunnels and release their resources. They
1028 * will be re-established after resume based on plug events.
1030 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1031 if (tb_tunnel_is_dp(tunnel))
1032 tb_deactivate_and_free_tunnel(tunnel);
1035 while (!list_empty(&tcm->dp_resources)) {
1036 struct tb_port *port;
1038 port = list_first_entry(&tcm->dp_resources,
1039 struct tb_port, list);
1040 list_del_init(&port->list);
1044 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1046 struct tb_tunnel *tunnel;
1049 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1053 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1054 if (WARN_ON(!tunnel))
1057 tb_tunnel_deactivate(tunnel);
1058 list_del(&tunnel->list);
1059 tb_tunnel_free(tunnel);
1063 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1065 struct tb_port *up, *down, *port;
1066 struct tb_cm *tcm = tb_priv(tb);
1067 struct tb_switch *parent_sw;
1068 struct tb_tunnel *tunnel;
1070 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1075 * Look up available down port. Since we are chaining it should
1076 * be found right above this switch.
1078 parent_sw = tb_to_switch(sw->dev.parent);
1079 port = tb_port_at(tb_route(sw), parent_sw);
1080 down = tb_find_pcie_down(parent_sw, port);
1084 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1088 if (tb_tunnel_activate(tunnel)) {
1090 "PCIe tunnel activation failed, aborting\n");
1091 tb_tunnel_free(tunnel);
1096 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
1099 if (tb_switch_pcie_l1_enable(sw))
1100 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
1102 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1106 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1107 int transmit_path, int transmit_ring,
1108 int receive_path, int receive_ring)
1110 struct tb_cm *tcm = tb_priv(tb);
1111 struct tb_port *nhi_port, *dst_port;
1112 struct tb_tunnel *tunnel;
1113 struct tb_switch *sw;
1115 sw = tb_to_switch(xd->dev.parent);
1116 dst_port = tb_port_at(xd->route, sw);
1117 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1119 mutex_lock(&tb->lock);
1120 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1121 transmit_ring, receive_path, receive_ring);
1123 mutex_unlock(&tb->lock);
1127 if (tb_tunnel_activate(tunnel)) {
1128 tb_port_info(nhi_port,
1129 "DMA tunnel activation failed, aborting\n");
1130 tb_tunnel_free(tunnel);
1131 mutex_unlock(&tb->lock);
1135 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1136 mutex_unlock(&tb->lock);
1140 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1141 int transmit_path, int transmit_ring,
1142 int receive_path, int receive_ring)
1144 struct tb_cm *tcm = tb_priv(tb);
1145 struct tb_port *nhi_port, *dst_port;
1146 struct tb_tunnel *tunnel, *n;
1147 struct tb_switch *sw;
1149 sw = tb_to_switch(xd->dev.parent);
1150 dst_port = tb_port_at(xd->route, sw);
1151 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1153 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1154 if (!tb_tunnel_is_dma(tunnel))
1156 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1159 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1160 receive_path, receive_ring))
1161 tb_deactivate_and_free_tunnel(tunnel);
1165 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1166 int transmit_path, int transmit_ring,
1167 int receive_path, int receive_ring)
1169 if (!xd->is_unplugged) {
1170 mutex_lock(&tb->lock);
1171 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1172 transmit_ring, receive_path,
1174 mutex_unlock(&tb->lock);
1179 /* hotplug handling */
1182 * tb_handle_hotplug() - handle hotplug event
1184 * Executes on tb->wq.
1186 static void tb_handle_hotplug(struct work_struct *work)
1188 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1189 struct tb *tb = ev->tb;
1190 struct tb_cm *tcm = tb_priv(tb);
1191 struct tb_switch *sw;
1192 struct tb_port *port;
1194 /* Bring the domain back from sleep if it was suspended */
1195 pm_runtime_get_sync(&tb->dev);
1197 mutex_lock(&tb->lock);
1198 if (!tcm->hotplug_active)
1199 goto out; /* during init, suspend or shutdown */
1201 sw = tb_switch_find_by_route(tb, ev->route);
1204 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1205 ev->route, ev->port, ev->unplug);
1208 if (ev->port > sw->config.max_port_number) {
1210 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1211 ev->route, ev->port, ev->unplug);
1214 port = &sw->ports[ev->port];
1215 if (tb_is_upstream_port(port)) {
1216 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1217 ev->route, ev->port, ev->unplug);
1221 pm_runtime_get_sync(&sw->dev);
1224 tb_retimer_remove_all(port);
1226 if (tb_port_has_remote(port)) {
1227 tb_port_dbg(port, "switch unplugged\n");
1228 tb_sw_set_unplugged(port->remote->sw);
1229 tb_free_invalid_tunnels(tb);
1230 tb_remove_dp_resources(port->remote->sw);
1231 tb_switch_tmu_disable(port->remote->sw);
1232 tb_switch_unconfigure_link(port->remote->sw);
1233 tb_switch_lane_bonding_disable(port->remote->sw);
1234 tb_switch_remove(port->remote->sw);
1235 port->remote = NULL;
1236 if (port->dual_link_port)
1237 port->dual_link_port->remote = NULL;
1238 /* Maybe we can create another DP tunnel */
1240 } else if (port->xdomain) {
1241 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1243 tb_port_dbg(port, "xdomain unplugged\n");
1245 * Service drivers are unbound during
1246 * tb_xdomain_remove() so setting XDomain as
1247 * unplugged here prevents deadlock if they call
1248 * tb_xdomain_disable_paths(). We will tear down
1249 * all the tunnels below.
1251 xd->is_unplugged = true;
1252 tb_xdomain_remove(xd);
1253 port->xdomain = NULL;
1254 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1256 tb_port_unconfigure_xdomain(port);
1257 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1258 tb_dp_resource_unavailable(tb, port);
1261 "got unplug event for disconnected port, ignoring\n");
1263 } else if (port->remote) {
1264 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1266 if (tb_port_is_null(port)) {
1267 tb_port_dbg(port, "hotplug: scanning\n");
1270 tb_port_dbg(port, "hotplug: no switch found\n");
1271 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1272 tb_dp_resource_available(tb, port);
1276 pm_runtime_mark_last_busy(&sw->dev);
1277 pm_runtime_put_autosuspend(&sw->dev);
1282 mutex_unlock(&tb->lock);
1284 pm_runtime_mark_last_busy(&tb->dev);
1285 pm_runtime_put_autosuspend(&tb->dev);
1291 * tb_schedule_hotplug_handler() - callback function for the control channel
1293 * Delegates to tb_handle_hotplug.
1295 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1296 const void *buf, size_t size)
1298 const struct cfg_event_pkg *pkg = buf;
1301 if (type != TB_CFG_PKG_EVENT) {
1302 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1306 route = tb_cfg_get_route(&pkg->header);
1308 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1309 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1313 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1316 static void tb_stop(struct tb *tb)
1318 struct tb_cm *tcm = tb_priv(tb);
1319 struct tb_tunnel *tunnel;
1320 struct tb_tunnel *n;
1322 cancel_delayed_work(&tcm->remove_work);
1323 /* tunnels are only present after everything has been initialized */
1324 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1326 * DMA tunnels require the driver to be functional so we
1327 * tear them down. Other protocol tunnels can be left
1330 if (tb_tunnel_is_dma(tunnel))
1331 tb_tunnel_deactivate(tunnel);
1332 tb_tunnel_free(tunnel);
1334 tb_switch_remove(tb->root_switch);
1335 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1338 static int tb_scan_finalize_switch(struct device *dev, void *data)
1340 if (tb_is_switch(dev)) {
1341 struct tb_switch *sw = tb_to_switch(dev);
1344 * If we found that the switch was already setup by the
1345 * boot firmware, mark it as authorized now before we
1346 * send uevent to userspace.
1351 dev_set_uevent_suppress(dev, false);
1352 kobject_uevent(&dev->kobj, KOBJ_ADD);
1353 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1359 static int tb_start(struct tb *tb)
1361 struct tb_cm *tcm = tb_priv(tb);
1364 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1365 if (IS_ERR(tb->root_switch))
1366 return PTR_ERR(tb->root_switch);
1369 * ICM firmware upgrade needs running firmware and in native
1370 * mode that is not available so disable firmware upgrade of the
1373 tb->root_switch->no_nvm_upgrade = true;
1374 /* All USB4 routers support runtime PM */
1375 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1377 ret = tb_switch_configure(tb->root_switch);
1379 tb_switch_put(tb->root_switch);
1383 /* Announce the switch to the world */
1384 ret = tb_switch_add(tb->root_switch);
1386 tb_switch_put(tb->root_switch);
1390 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_RATE_HIFI, false);
1391 /* Enable TMU if it is off */
1392 tb_switch_tmu_enable(tb->root_switch);
1393 /* Full scan to discover devices added before the driver was loaded. */
1394 tb_scan_switch(tb->root_switch);
1395 /* Find out tunnels created by the boot firmware */
1396 tb_discover_tunnels(tb);
1398 * If the boot firmware did not create USB 3.x tunnels create them
1399 * now for the whole topology.
1401 tb_create_usb3_tunnels(tb->root_switch);
1402 /* Add DP IN resources for the root switch */
1403 tb_add_dp_resources(tb->root_switch);
1404 /* Make the discovered switches available to the userspace */
1405 device_for_each_child(&tb->root_switch->dev, NULL,
1406 tb_scan_finalize_switch);
1408 /* Allow tb_handle_hotplug to progress events */
1409 tcm->hotplug_active = true;
1413 static int tb_suspend_noirq(struct tb *tb)
1415 struct tb_cm *tcm = tb_priv(tb);
1417 tb_dbg(tb, "suspending...\n");
1418 tb_disconnect_and_release_dp(tb);
1419 tb_switch_suspend(tb->root_switch, false);
1420 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1421 tb_dbg(tb, "suspend finished\n");
1426 static void tb_restore_children(struct tb_switch *sw)
1428 struct tb_port *port;
1430 /* No need to restore if the router is already unplugged */
1431 if (sw->is_unplugged)
1434 if (tb_switch_enable_clx(sw, TB_CL0S))
1435 tb_sw_warn(sw, "failed to re-enable CLx on upstream port\n");
1438 * tb_switch_tmu_configure() was already called when the switch was
1439 * added before entering system sleep or runtime suspend,
1440 * so no need to call it again before enabling TMU.
1442 if (tb_enable_tmu(sw))
1443 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1445 tb_switch_for_each_port(sw, port) {
1446 if (!tb_port_has_remote(port) && !port->xdomain)
1450 tb_switch_lane_bonding_enable(port->remote->sw);
1451 tb_switch_configure_link(port->remote->sw);
1453 tb_restore_children(port->remote->sw);
1454 } else if (port->xdomain) {
1455 tb_port_configure_xdomain(port);
1460 static int tb_resume_noirq(struct tb *tb)
1462 struct tb_cm *tcm = tb_priv(tb);
1463 struct tb_tunnel *tunnel, *n;
1464 unsigned int usb3_delay = 0;
1467 tb_dbg(tb, "resuming...\n");
1469 /* remove any pci devices the firmware might have setup */
1470 tb_switch_reset(tb->root_switch);
1472 tb_switch_resume(tb->root_switch);
1473 tb_free_invalid_tunnels(tb);
1474 tb_free_unplugged_children(tb->root_switch);
1475 tb_restore_children(tb->root_switch);
1478 * If we get here from suspend to disk the boot firmware or the
1479 * restore kernel might have created tunnels of its own. Since
1480 * we cannot be sure they are usable for us we find and tear
1483 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
1484 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
1485 if (tb_tunnel_is_usb3(tunnel))
1487 tb_tunnel_deactivate(tunnel);
1488 tb_tunnel_free(tunnel);
1491 /* Re-create our tunnels now */
1492 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1493 /* USB3 requires delay before it can be re-activated */
1494 if (tb_tunnel_is_usb3(tunnel)) {
1496 /* Only need to do it once */
1499 tb_tunnel_restart(tunnel);
1501 if (!list_empty(&tcm->tunnel_list)) {
1503 * the pcie links need some time to get going.
1504 * 100ms works for me...
1506 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1509 /* Allow tb_handle_hotplug to progress events */
1510 tcm->hotplug_active = true;
1511 tb_dbg(tb, "resume finished\n");
1516 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1518 struct tb_port *port;
1521 tb_switch_for_each_port(sw, port) {
1522 if (tb_is_upstream_port(port))
1524 if (port->xdomain && port->xdomain->is_unplugged) {
1525 tb_retimer_remove_all(port);
1526 tb_xdomain_remove(port->xdomain);
1527 tb_port_unconfigure_xdomain(port);
1528 port->xdomain = NULL;
1530 } else if (port->remote) {
1531 ret += tb_free_unplugged_xdomains(port->remote->sw);
1538 static int tb_freeze_noirq(struct tb *tb)
1540 struct tb_cm *tcm = tb_priv(tb);
1542 tcm->hotplug_active = false;
1546 static int tb_thaw_noirq(struct tb *tb)
1548 struct tb_cm *tcm = tb_priv(tb);
1550 tcm->hotplug_active = true;
1554 static void tb_complete(struct tb *tb)
1557 * Release any unplugged XDomains and if there is a case where
1558 * another domain is swapped in place of unplugged XDomain we
1559 * need to run another rescan.
1561 mutex_lock(&tb->lock);
1562 if (tb_free_unplugged_xdomains(tb->root_switch))
1563 tb_scan_switch(tb->root_switch);
1564 mutex_unlock(&tb->lock);
1567 static int tb_runtime_suspend(struct tb *tb)
1569 struct tb_cm *tcm = tb_priv(tb);
1571 mutex_lock(&tb->lock);
1572 tb_switch_suspend(tb->root_switch, true);
1573 tcm->hotplug_active = false;
1574 mutex_unlock(&tb->lock);
1579 static void tb_remove_work(struct work_struct *work)
1581 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1582 struct tb *tb = tcm_to_tb(tcm);
1584 mutex_lock(&tb->lock);
1585 if (tb->root_switch) {
1586 tb_free_unplugged_children(tb->root_switch);
1587 tb_free_unplugged_xdomains(tb->root_switch);
1589 mutex_unlock(&tb->lock);
1592 static int tb_runtime_resume(struct tb *tb)
1594 struct tb_cm *tcm = tb_priv(tb);
1595 struct tb_tunnel *tunnel, *n;
1597 mutex_lock(&tb->lock);
1598 tb_switch_resume(tb->root_switch);
1599 tb_free_invalid_tunnels(tb);
1600 tb_restore_children(tb->root_switch);
1601 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1602 tb_tunnel_restart(tunnel);
1603 tcm->hotplug_active = true;
1604 mutex_unlock(&tb->lock);
1607 * Schedule cleanup of any unplugged devices. Run this in a
1608 * separate thread to avoid possible deadlock if the device
1609 * removal runtime resumes the unplugged device.
1611 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1615 static const struct tb_cm_ops tb_cm_ops = {
1618 .suspend_noirq = tb_suspend_noirq,
1619 .resume_noirq = tb_resume_noirq,
1620 .freeze_noirq = tb_freeze_noirq,
1621 .thaw_noirq = tb_thaw_noirq,
1622 .complete = tb_complete,
1623 .runtime_suspend = tb_runtime_suspend,
1624 .runtime_resume = tb_runtime_resume,
1625 .handle_event = tb_handle_event,
1626 .disapprove_switch = tb_disconnect_pci,
1627 .approve_switch = tb_tunnel_pci,
1628 .approve_xdomain_paths = tb_approve_xdomain_paths,
1629 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1633 * During suspend the Thunderbolt controller is reset and all PCIe
1634 * tunnels are lost. The NHI driver will try to reestablish all tunnels
1635 * during resume. This adds device links between the tunneled PCIe
1636 * downstream ports and the NHI so that the device core will make sure
1637 * NHI is resumed first before the rest.
1639 static void tb_apple_add_links(struct tb_nhi *nhi)
1641 struct pci_dev *upstream, *pdev;
1643 if (!x86_apple_machine)
1646 switch (nhi->pdev->device) {
1647 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1648 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1649 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1650 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1656 upstream = pci_upstream_bridge(nhi->pdev);
1658 if (!pci_is_pcie(upstream))
1660 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
1662 upstream = pci_upstream_bridge(upstream);
1669 * For each hotplug downstream port, create add device link
1670 * back to NHI so that PCIe tunnels can be re-established after
1673 for_each_pci_bridge(pdev, upstream->subordinate) {
1674 const struct device_link *link;
1676 if (!pci_is_pcie(pdev))
1678 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
1679 !pdev->is_hotplug_bridge)
1682 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
1683 DL_FLAG_AUTOREMOVE_SUPPLIER |
1684 DL_FLAG_PM_RUNTIME);
1686 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
1687 dev_name(&pdev->dev));
1689 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
1690 dev_name(&pdev->dev));
1695 struct tb *tb_probe(struct tb_nhi *nhi)
1700 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1704 if (tb_acpi_may_tunnel_pcie())
1705 tb->security_level = TB_SECURITY_USER;
1707 tb->security_level = TB_SECURITY_NOPCIE;
1709 tb->cm_ops = &tb_cm_ops;
1712 INIT_LIST_HEAD(&tcm->tunnel_list);
1713 INIT_LIST_HEAD(&tcm->dp_resources);
1714 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1716 tb_dbg(tb, "using software connection manager\n");
1718 tb_apple_add_links(nhi);
1719 tb_acpi_add_links(nhi);