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 */
20 #define MAX_GROUPS 7 /* max Group_ID is 7 */
23 * struct tb_cm - Simple Thunderbolt connection manager
24 * @tunnel_list: List of active tunnels
25 * @dp_resources: List of available DP resources for DP tunneling
26 * @hotplug_active: tb_handle_hotplug will stop progressing plug
27 * events and exit if this is not set (it needs to
28 * acquire the lock one more time). Used to drain wq
29 * after cfg has been paused.
30 * @remove_work: Work used to remove any unplugged routers after
32 * @groups: Bandwidth groups used in this domain.
35 struct list_head tunnel_list;
36 struct list_head dp_resources;
38 struct delayed_work remove_work;
39 struct tb_bandwidth_group groups[MAX_GROUPS];
42 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
44 return ((void *)tcm - sizeof(struct tb));
47 struct tb_hotplug_event {
48 struct work_struct work;
55 static void tb_init_bandwidth_groups(struct tb_cm *tcm)
59 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
60 struct tb_bandwidth_group *group = &tcm->groups[i];
62 group->tb = tcm_to_tb(tcm);
64 INIT_LIST_HEAD(&group->ports);
68 static void tb_bandwidth_group_attach_port(struct tb_bandwidth_group *group,
71 if (!group || WARN_ON(in->group))
75 list_add_tail(&in->group_list, &group->ports);
77 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index);
80 static struct tb_bandwidth_group *tb_find_free_bandwidth_group(struct tb_cm *tcm)
84 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
85 struct tb_bandwidth_group *group = &tcm->groups[i];
87 if (list_empty(&group->ports))
94 static struct tb_bandwidth_group *
95 tb_attach_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
98 struct tb_bandwidth_group *group;
99 struct tb_tunnel *tunnel;
102 * Find all DP tunnels that go through all the same USB4 links
103 * as this one. Because we always setup tunnels the same way we
104 * can just check for the routers at both ends of the tunnels
105 * and if they are the same we have a match.
107 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
108 if (!tb_tunnel_is_dp(tunnel))
111 if (tunnel->src_port->sw == in->sw &&
112 tunnel->dst_port->sw == out->sw) {
113 group = tunnel->src_port->group;
115 tb_bandwidth_group_attach_port(group, in);
121 /* Pick up next available group then */
122 group = tb_find_free_bandwidth_group(tcm);
124 tb_bandwidth_group_attach_port(group, in);
126 tb_port_warn(in, "no available bandwidth groups\n");
131 static void tb_discover_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
134 if (usb4_dp_port_bw_mode_enabled(in)) {
137 index = usb4_dp_port_group_id(in);
138 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
139 if (tcm->groups[i].index == index) {
140 tb_bandwidth_group_attach_port(&tcm->groups[i], in);
146 tb_attach_bandwidth_group(tcm, in, out);
149 static void tb_detach_bandwidth_group(struct tb_port *in)
151 struct tb_bandwidth_group *group = in->group;
155 list_del_init(&in->group_list);
157 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index);
161 static void tb_handle_hotplug(struct work_struct *work);
163 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
165 struct tb_hotplug_event *ev;
167 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
175 INIT_WORK(&ev->work, tb_handle_hotplug);
176 queue_work(tb->wq, &ev->work);
179 /* enumeration & hot plug handling */
181 static void tb_add_dp_resources(struct tb_switch *sw)
183 struct tb_cm *tcm = tb_priv(sw->tb);
184 struct tb_port *port;
186 tb_switch_for_each_port(sw, port) {
187 if (!tb_port_is_dpin(port))
190 if (!tb_switch_query_dp_resource(sw, port))
193 list_add_tail(&port->list, &tcm->dp_resources);
194 tb_port_dbg(port, "DP IN resource available\n");
198 static void tb_remove_dp_resources(struct tb_switch *sw)
200 struct tb_cm *tcm = tb_priv(sw->tb);
201 struct tb_port *port, *tmp;
203 /* Clear children resources first */
204 tb_switch_for_each_port(sw, port) {
205 if (tb_port_has_remote(port))
206 tb_remove_dp_resources(port->remote->sw);
209 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
210 if (port->sw == sw) {
211 tb_port_dbg(port, "DP OUT resource unavailable\n");
212 list_del_init(&port->list);
217 static void tb_discover_dp_resource(struct tb *tb, struct tb_port *port)
219 struct tb_cm *tcm = tb_priv(tb);
222 list_for_each_entry(p, &tcm->dp_resources, list) {
227 tb_port_dbg(port, "DP %s resource available discovered\n",
228 tb_port_is_dpin(port) ? "IN" : "OUT");
229 list_add_tail(&port->list, &tcm->dp_resources);
232 static void tb_discover_dp_resources(struct tb *tb)
234 struct tb_cm *tcm = tb_priv(tb);
235 struct tb_tunnel *tunnel;
237 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
238 if (tb_tunnel_is_dp(tunnel))
239 tb_discover_dp_resource(tb, tunnel->dst_port);
243 static void tb_switch_discover_tunnels(struct tb_switch *sw,
244 struct list_head *list,
247 struct tb *tb = sw->tb;
248 struct tb_port *port;
250 tb_switch_for_each_port(sw, port) {
251 struct tb_tunnel *tunnel = NULL;
253 switch (port->config.type) {
254 case TB_TYPE_DP_HDMI_IN:
255 tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
257 * In case of DP tunnel exists, change host router's
258 * 1st children TMU mode to HiFi for CL0s to work.
261 tb_switch_enable_tmu_1st_child(tb->root_switch,
262 TB_SWITCH_TMU_RATE_HIFI);
265 case TB_TYPE_PCIE_DOWN:
266 tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
269 case TB_TYPE_USB3_DOWN:
270 tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
278 list_add_tail(&tunnel->list, list);
281 tb_switch_for_each_port(sw, port) {
282 if (tb_port_has_remote(port)) {
283 tb_switch_discover_tunnels(port->remote->sw, list,
289 static void tb_discover_tunnels(struct tb *tb)
291 struct tb_cm *tcm = tb_priv(tb);
292 struct tb_tunnel *tunnel;
294 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
296 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
297 if (tb_tunnel_is_pci(tunnel)) {
298 struct tb_switch *parent = tunnel->dst_port->sw;
300 while (parent != tunnel->src_port->sw) {
302 parent = tb_switch_parent(parent);
304 } else if (tb_tunnel_is_dp(tunnel)) {
305 struct tb_port *in = tunnel->src_port;
306 struct tb_port *out = tunnel->dst_port;
308 /* Keep the domain from powering down */
309 pm_runtime_get_sync(&in->sw->dev);
310 pm_runtime_get_sync(&out->sw->dev);
312 tb_discover_bandwidth_group(tcm, in, out);
317 static int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
319 if (tb_switch_is_usb4(port->sw))
320 return usb4_port_configure_xdomain(port, xd);
321 return tb_lc_configure_xdomain(port);
324 static void tb_port_unconfigure_xdomain(struct tb_port *port)
326 if (tb_switch_is_usb4(port->sw))
327 usb4_port_unconfigure_xdomain(port);
329 tb_lc_unconfigure_xdomain(port);
331 tb_port_enable(port->dual_link_port);
334 static void tb_scan_xdomain(struct tb_port *port)
336 struct tb_switch *sw = port->sw;
337 struct tb *tb = sw->tb;
338 struct tb_xdomain *xd;
341 if (!tb_is_xdomain_enabled())
344 route = tb_downstream_route(port);
345 xd = tb_xdomain_find_by_route(tb, route);
351 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
354 tb_port_at(route, sw)->xdomain = xd;
355 tb_port_configure_xdomain(port, xd);
360 static int tb_enable_tmu(struct tb_switch *sw)
364 /* If it is already enabled in correct mode, don't touch it */
365 if (tb_switch_tmu_is_enabled(sw, sw->tmu.unidirectional_request))
368 ret = tb_switch_tmu_disable(sw);
372 ret = tb_switch_tmu_post_time(sw);
376 return tb_switch_tmu_enable(sw);
380 * tb_find_unused_port() - return the first inactive port on @sw
381 * @sw: Switch to find the port on
382 * @type: Port type to look for
384 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
385 enum tb_port_type type)
387 struct tb_port *port;
389 tb_switch_for_each_port(sw, port) {
390 if (tb_is_upstream_port(port))
392 if (port->config.type != type)
396 if (tb_port_is_enabled(port))
403 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
404 const struct tb_port *port)
406 struct tb_port *down;
408 down = usb4_switch_map_usb3_down(sw, port);
409 if (down && !tb_usb3_port_is_enabled(down))
414 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
415 struct tb_port *src_port,
416 struct tb_port *dst_port)
418 struct tb_cm *tcm = tb_priv(tb);
419 struct tb_tunnel *tunnel;
421 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
422 if (tunnel->type == type &&
423 ((src_port && src_port == tunnel->src_port) ||
424 (dst_port && dst_port == tunnel->dst_port))) {
432 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
433 struct tb_port *src_port,
434 struct tb_port *dst_port)
436 struct tb_port *port, *usb3_down;
437 struct tb_switch *sw;
439 /* Pick the router that is deepest in the topology */
440 if (dst_port->sw->config.depth > src_port->sw->config.depth)
445 /* Can't be the host router */
446 if (sw == tb->root_switch)
449 /* Find the downstream USB4 port that leads to this router */
450 port = tb_port_at(tb_route(sw), tb->root_switch);
451 /* Find the corresponding host router USB3 downstream port */
452 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
456 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
459 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
460 struct tb_port *dst_port, int *available_up, int *available_down)
462 int usb3_consumed_up, usb3_consumed_down, ret;
463 struct tb_cm *tcm = tb_priv(tb);
464 struct tb_tunnel *tunnel;
465 struct tb_port *port;
467 tb_dbg(tb, "calculating available bandwidth between %llx:%u <-> %llx:%u\n",
468 tb_route(src_port->sw), src_port->port, tb_route(dst_port->sw),
471 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
472 if (tunnel && tunnel->src_port != src_port &&
473 tunnel->dst_port != dst_port) {
474 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
475 &usb3_consumed_down);
479 usb3_consumed_up = 0;
480 usb3_consumed_down = 0;
483 *available_up = *available_down = 40000;
485 /* Find the minimum available bandwidth over all links */
486 tb_for_each_port_on_path(src_port, dst_port, port) {
487 int link_speed, link_width, up_bw, down_bw;
489 if (!tb_port_is_null(port))
492 if (tb_is_upstream_port(port)) {
493 link_speed = port->sw->link_speed;
495 link_speed = tb_port_get_link_speed(port);
500 link_width = port->bonded ? 2 : 1;
502 up_bw = link_speed * link_width * 1000; /* Mb/s */
503 /* Leave 10% guard band */
507 tb_port_dbg(port, "link total bandwidth %d/%d Mb/s\n", up_bw,
511 * Find all DP tunnels that cross the port and reduce
512 * their consumed bandwidth from the available.
514 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
515 int dp_consumed_up, dp_consumed_down;
517 if (tb_tunnel_is_invalid(tunnel))
520 if (!tb_tunnel_is_dp(tunnel))
523 if (!tb_tunnel_port_on_path(tunnel, port))
527 * Ignore the DP tunnel between src_port and
528 * dst_port because it is the same tunnel and we
529 * may be re-calculating estimated bandwidth.
531 if (tunnel->src_port == src_port &&
532 tunnel->dst_port == dst_port)
535 ret = tb_tunnel_consumed_bandwidth(tunnel,
541 up_bw -= dp_consumed_up;
542 down_bw -= dp_consumed_down;
546 * If USB3 is tunneled from the host router down to the
547 * branch leading to port we need to take USB3 consumed
548 * bandwidth into account regardless whether it actually
551 up_bw -= usb3_consumed_up;
552 down_bw -= usb3_consumed_down;
554 if (up_bw < *available_up)
555 *available_up = up_bw;
556 if (down_bw < *available_down)
557 *available_down = down_bw;
560 if (*available_up < 0)
562 if (*available_down < 0)
568 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
569 struct tb_port *src_port,
570 struct tb_port *dst_port)
572 struct tb_tunnel *tunnel;
574 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
575 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
578 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
579 struct tb_port *dst_port)
581 int ret, available_up, available_down;
582 struct tb_tunnel *tunnel;
584 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
588 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
591 * Calculate available bandwidth for the first hop USB3 tunnel.
592 * That determines the whole USB3 bandwidth for this branch.
594 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
595 &available_up, &available_down);
597 tb_warn(tb, "failed to calculate available bandwidth\n");
601 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
602 available_up, available_down);
604 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
607 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
609 struct tb_switch *parent = tb_switch_parent(sw);
610 int ret, available_up, available_down;
611 struct tb_port *up, *down, *port;
612 struct tb_cm *tcm = tb_priv(tb);
613 struct tb_tunnel *tunnel;
615 if (!tb_acpi_may_tunnel_usb3()) {
616 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
620 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
628 * Look up available down port. Since we are chaining it should
629 * be found right above this switch.
631 port = tb_port_at(tb_route(sw), parent);
632 down = tb_find_usb3_down(parent, port);
636 if (tb_route(parent)) {
637 struct tb_port *parent_up;
639 * Check first that the parent switch has its upstream USB3
640 * port enabled. Otherwise the chain is not complete and
641 * there is no point setting up a new tunnel.
643 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
644 if (!parent_up || !tb_port_is_enabled(parent_up))
647 /* Make all unused bandwidth available for the new tunnel */
648 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
653 ret = tb_available_bandwidth(tb, down, up, &available_up,
658 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
659 available_up, available_down);
661 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
668 if (tb_tunnel_activate(tunnel)) {
670 "USB3 tunnel activation failed, aborting\n");
675 list_add_tail(&tunnel->list, &tcm->tunnel_list);
676 if (tb_route(parent))
677 tb_reclaim_usb3_bandwidth(tb, down, up);
682 tb_tunnel_free(tunnel);
684 if (tb_route(parent))
685 tb_reclaim_usb3_bandwidth(tb, down, up);
690 static int tb_create_usb3_tunnels(struct tb_switch *sw)
692 struct tb_port *port;
695 if (!tb_acpi_may_tunnel_usb3())
699 ret = tb_tunnel_usb3(sw->tb, sw);
704 tb_switch_for_each_port(sw, port) {
705 if (!tb_port_has_remote(port))
707 ret = tb_create_usb3_tunnels(port->remote->sw);
715 static void tb_scan_port(struct tb_port *port);
718 * tb_scan_switch() - scan for and initialize downstream switches
720 static void tb_scan_switch(struct tb_switch *sw)
722 struct tb_port *port;
724 pm_runtime_get_sync(&sw->dev);
726 tb_switch_for_each_port(sw, port)
729 pm_runtime_mark_last_busy(&sw->dev);
730 pm_runtime_put_autosuspend(&sw->dev);
734 * tb_scan_port() - check for and initialize switches below port
736 static void tb_scan_port(struct tb_port *port)
738 struct tb_cm *tcm = tb_priv(port->sw->tb);
739 struct tb_port *upstream_port;
740 bool discovery = false;
741 struct tb_switch *sw;
744 if (tb_is_upstream_port(port))
747 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
748 !tb_dp_port_is_enabled(port)) {
749 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
750 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
755 if (port->config.type != TB_TYPE_PORT)
757 if (port->dual_link_port && port->link_nr)
759 * Downstream switch is reachable through two ports.
760 * Only scan on the primary port (link_nr == 0).
764 pm_runtime_get_sync(&port->usb4->dev);
766 if (tb_wait_for_port(port, false) <= 0)
769 tb_port_dbg(port, "port already has a remote\n");
773 tb_retimer_scan(port, true);
775 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
776 tb_downstream_route(port));
779 * If there is an error accessing the connected switch
780 * it may be connected to another domain. Also we allow
781 * the other domain to be connected to a max depth switch.
783 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
784 tb_scan_xdomain(port);
788 if (tb_switch_configure(sw)) {
794 * If there was previously another domain connected remove it
798 tb_xdomain_remove(port->xdomain);
799 tb_port_unconfigure_xdomain(port);
800 port->xdomain = NULL;
804 * Do not send uevents until we have discovered all existing
805 * tunnels and know which switches were authorized already by
808 if (!tcm->hotplug_active) {
809 dev_set_uevent_suppress(&sw->dev, true);
814 * At the moment Thunderbolt 2 and beyond (devices with LC) we
815 * can support runtime PM.
817 sw->rpm = sw->generation > 1;
819 if (tb_switch_add(sw)) {
824 /* Link the switches using both links if available */
825 upstream_port = tb_upstream_port(sw);
826 port->remote = upstream_port;
827 upstream_port->remote = port;
828 if (port->dual_link_port && upstream_port->dual_link_port) {
829 port->dual_link_port->remote = upstream_port->dual_link_port;
830 upstream_port->dual_link_port->remote = port->dual_link_port;
833 /* Enable lane bonding if supported */
834 tb_switch_lane_bonding_enable(sw);
835 /* Set the link configured */
836 tb_switch_configure_link(sw);
838 * CL0s and CL1 are enabled and supported together.
839 * Silently ignore CLx enabling in case CLx is not supported.
842 tb_sw_dbg(sw, "discovery, not touching CL states\n");
844 ret = tb_switch_enable_clx(sw, TB_CL1);
845 if (ret && ret != -EOPNOTSUPP)
846 tb_sw_warn(sw, "failed to enable %s on upstream port\n",
847 tb_switch_clx_name(TB_CL1));
850 if (tb_switch_is_clx_enabled(sw, TB_CL1))
852 * To support highest CLx state, we set router's TMU to
855 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true);
857 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/
858 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false);
860 if (tb_enable_tmu(sw))
861 tb_sw_warn(sw, "failed to enable TMU\n");
863 /* Scan upstream retimers */
864 tb_retimer_scan(upstream_port, true);
867 * Create USB 3.x tunnels only when the switch is plugged to the
868 * domain. This is because we scan the domain also during discovery
869 * and want to discover existing USB 3.x tunnels before we create
872 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
873 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
875 tb_add_dp_resources(sw);
880 pm_runtime_mark_last_busy(&port->usb4->dev);
881 pm_runtime_put_autosuspend(&port->usb4->dev);
885 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
887 struct tb_port *src_port, *dst_port;
893 tb_tunnel_deactivate(tunnel);
894 list_del(&tunnel->list);
897 src_port = tunnel->src_port;
898 dst_port = tunnel->dst_port;
900 switch (tunnel->type) {
902 tb_detach_bandwidth_group(src_port);
904 * In case of DP tunnel make sure the DP IN resource is
905 * deallocated properly.
907 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
908 /* Now we can allow the domain to runtime suspend again */
909 pm_runtime_mark_last_busy(&dst_port->sw->dev);
910 pm_runtime_put_autosuspend(&dst_port->sw->dev);
911 pm_runtime_mark_last_busy(&src_port->sw->dev);
912 pm_runtime_put_autosuspend(&src_port->sw->dev);
916 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
921 * PCIe and DMA tunnels do not consume guaranteed
927 tb_tunnel_free(tunnel);
931 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
933 static void tb_free_invalid_tunnels(struct tb *tb)
935 struct tb_cm *tcm = tb_priv(tb);
936 struct tb_tunnel *tunnel;
939 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
940 if (tb_tunnel_is_invalid(tunnel))
941 tb_deactivate_and_free_tunnel(tunnel);
946 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
948 static void tb_free_unplugged_children(struct tb_switch *sw)
950 struct tb_port *port;
952 tb_switch_for_each_port(sw, port) {
953 if (!tb_port_has_remote(port))
956 if (port->remote->sw->is_unplugged) {
957 tb_retimer_remove_all(port);
958 tb_remove_dp_resources(port->remote->sw);
959 tb_switch_unconfigure_link(port->remote->sw);
960 tb_switch_lane_bonding_disable(port->remote->sw);
961 tb_switch_remove(port->remote->sw);
963 if (port->dual_link_port)
964 port->dual_link_port->remote = NULL;
966 tb_free_unplugged_children(port->remote->sw);
971 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
972 const struct tb_port *port)
974 struct tb_port *down = NULL;
977 * To keep plugging devices consistently in the same PCIe
978 * hierarchy, do mapping here for switch downstream PCIe ports.
980 if (tb_switch_is_usb4(sw)) {
981 down = usb4_switch_map_pcie_down(sw, port);
982 } else if (!tb_route(sw)) {
983 int phy_port = tb_phy_port_from_link(port->port);
987 * Hard-coded Thunderbolt port to PCIe down port mapping
990 if (tb_switch_is_cactus_ridge(sw) ||
991 tb_switch_is_alpine_ridge(sw))
992 index = !phy_port ? 6 : 7;
993 else if (tb_switch_is_falcon_ridge(sw))
994 index = !phy_port ? 6 : 8;
995 else if (tb_switch_is_titan_ridge(sw))
996 index = !phy_port ? 8 : 9;
1000 /* Validate the hard-coding */
1001 if (WARN_ON(index > sw->config.max_port_number))
1004 down = &sw->ports[index];
1008 if (WARN_ON(!tb_port_is_pcie_down(down)))
1010 if (tb_pci_port_is_enabled(down))
1017 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
1021 tb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group *group)
1023 struct tb_tunnel *first_tunnel;
1024 struct tb *tb = group->tb;
1028 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n",
1031 first_tunnel = NULL;
1032 list_for_each_entry(in, &group->ports, group_list) {
1033 int estimated_bw, estimated_up, estimated_down;
1034 struct tb_tunnel *tunnel;
1035 struct tb_port *out;
1037 if (!usb4_dp_port_bw_mode_enabled(in))
1040 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
1041 if (WARN_ON(!tunnel))
1044 if (!first_tunnel) {
1046 * Since USB3 bandwidth is shared by all DP
1047 * tunnels under the host router USB4 port, even
1048 * if they do not begin from the host router, we
1049 * can release USB3 bandwidth just once and not
1050 * for each tunnel separately.
1052 first_tunnel = tunnel;
1053 ret = tb_release_unused_usb3_bandwidth(tb,
1054 first_tunnel->src_port, first_tunnel->dst_port);
1057 "failed to release unused bandwidth\n");
1062 out = tunnel->dst_port;
1063 ret = tb_available_bandwidth(tb, in, out, &estimated_up,
1067 "failed to re-calculate estimated bandwidth\n");
1072 * Estimated bandwidth includes:
1073 * - already allocated bandwidth for the DP tunnel
1074 * - available bandwidth along the path
1075 * - bandwidth allocated for USB 3.x but not used.
1077 tb_port_dbg(in, "re-calculated estimated bandwidth %u/%u Mb/s\n",
1078 estimated_up, estimated_down);
1080 if (in->sw->config.depth < out->sw->config.depth)
1081 estimated_bw = estimated_down;
1083 estimated_bw = estimated_up;
1085 if (usb4_dp_port_set_estimated_bw(in, estimated_bw))
1086 tb_port_warn(in, "failed to update estimated bandwidth\n");
1090 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port,
1091 first_tunnel->dst_port);
1093 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index);
1096 static void tb_recalc_estimated_bandwidth(struct tb *tb)
1098 struct tb_cm *tcm = tb_priv(tb);
1101 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n");
1103 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
1104 struct tb_bandwidth_group *group = &tcm->groups[i];
1106 if (!list_empty(&group->ports))
1107 tb_recalc_estimated_bandwidth_for_group(group);
1110 tb_dbg(tb, "bandwidth re-calculation done\n");
1113 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
1115 struct tb_port *host_port, *port;
1116 struct tb_cm *tcm = tb_priv(tb);
1118 host_port = tb_route(in->sw) ?
1119 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
1121 list_for_each_entry(port, &tcm->dp_resources, list) {
1122 if (!tb_port_is_dpout(port))
1125 if (tb_port_is_enabled(port)) {
1126 tb_port_dbg(port, "DP OUT in use\n");
1130 tb_port_dbg(port, "DP OUT available\n");
1133 * Keep the DP tunnel under the topology starting from
1134 * the same host router downstream port.
1136 if (host_port && tb_route(port->sw)) {
1139 p = tb_port_at(tb_route(port->sw), tb->root_switch);
1150 static void tb_tunnel_dp(struct tb *tb)
1152 int available_up, available_down, ret, link_nr;
1153 struct tb_cm *tcm = tb_priv(tb);
1154 struct tb_port *port, *in, *out;
1155 struct tb_tunnel *tunnel;
1157 if (!tb_acpi_may_tunnel_dp()) {
1158 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
1163 * Find pair of inactive DP IN and DP OUT adapters and then
1164 * establish a DP tunnel between them.
1166 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
1170 list_for_each_entry(port, &tcm->dp_resources, list) {
1171 if (!tb_port_is_dpin(port))
1174 if (tb_port_is_enabled(port)) {
1175 tb_port_dbg(port, "DP IN in use\n");
1179 tb_port_dbg(port, "DP IN available\n");
1181 out = tb_find_dp_out(tb, port);
1189 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
1193 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
1198 * This is only applicable to links that are not bonded (so
1199 * when Thunderbolt 1 hardware is involved somewhere in the
1200 * topology). For these try to share the DP bandwidth between
1204 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
1205 if (tb_tunnel_is_dp(tunnel)) {
1212 * DP stream needs the domain to be active so runtime resume
1213 * both ends of the tunnel.
1215 * This should bring the routers in the middle active as well
1216 * and keeps the domain from runtime suspending while the DP
1219 pm_runtime_get_sync(&in->sw->dev);
1220 pm_runtime_get_sync(&out->sw->dev);
1222 if (tb_switch_alloc_dp_resource(in->sw, in)) {
1223 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
1227 if (!tb_attach_bandwidth_group(tcm, in, out))
1228 goto err_dealloc_dp;
1230 /* Make all unused USB3 bandwidth available for the new DP tunnel */
1231 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
1233 tb_warn(tb, "failed to release unused bandwidth\n");
1234 goto err_detach_group;
1237 ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down);
1239 goto err_reclaim_usb;
1241 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
1242 available_up, available_down);
1244 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
1247 tb_port_dbg(out, "could not allocate DP tunnel\n");
1248 goto err_reclaim_usb;
1251 if (tb_tunnel_activate(tunnel)) {
1252 tb_port_info(out, "DP tunnel activation failed, aborting\n");
1256 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1257 tb_reclaim_usb3_bandwidth(tb, in, out);
1259 /* Update the domain with the new bandwidth estimation */
1260 tb_recalc_estimated_bandwidth(tb);
1263 * In case of DP tunnel exists, change host router's 1st children
1264 * TMU mode to HiFi for CL0s to work.
1266 tb_switch_enable_tmu_1st_child(tb->root_switch, TB_SWITCH_TMU_RATE_HIFI);
1271 tb_tunnel_free(tunnel);
1273 tb_reclaim_usb3_bandwidth(tb, in, out);
1275 tb_detach_bandwidth_group(in);
1277 tb_switch_dealloc_dp_resource(in->sw, in);
1279 pm_runtime_mark_last_busy(&out->sw->dev);
1280 pm_runtime_put_autosuspend(&out->sw->dev);
1281 pm_runtime_mark_last_busy(&in->sw->dev);
1282 pm_runtime_put_autosuspend(&in->sw->dev);
1285 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
1287 struct tb_port *in, *out;
1288 struct tb_tunnel *tunnel;
1290 if (tb_port_is_dpin(port)) {
1291 tb_port_dbg(port, "DP IN resource unavailable\n");
1295 tb_port_dbg(port, "DP OUT resource unavailable\n");
1300 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
1301 tb_deactivate_and_free_tunnel(tunnel);
1302 list_del_init(&port->list);
1305 * See if there is another DP OUT port that can be used for
1306 * to create another tunnel.
1308 tb_recalc_estimated_bandwidth(tb);
1312 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1314 struct tb_cm *tcm = tb_priv(tb);
1317 if (tb_port_is_enabled(port))
1320 list_for_each_entry(p, &tcm->dp_resources, list) {
1325 tb_port_dbg(port, "DP %s resource available\n",
1326 tb_port_is_dpin(port) ? "IN" : "OUT");
1327 list_add_tail(&port->list, &tcm->dp_resources);
1329 /* Look for suitable DP IN <-> DP OUT pairs now */
1333 static void tb_disconnect_and_release_dp(struct tb *tb)
1335 struct tb_cm *tcm = tb_priv(tb);
1336 struct tb_tunnel *tunnel, *n;
1339 * Tear down all DP tunnels and release their resources. They
1340 * will be re-established after resume based on plug events.
1342 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1343 if (tb_tunnel_is_dp(tunnel))
1344 tb_deactivate_and_free_tunnel(tunnel);
1347 while (!list_empty(&tcm->dp_resources)) {
1348 struct tb_port *port;
1350 port = list_first_entry(&tcm->dp_resources,
1351 struct tb_port, list);
1352 list_del_init(&port->list);
1356 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1358 struct tb_tunnel *tunnel;
1361 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1365 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1366 if (WARN_ON(!tunnel))
1369 tb_switch_xhci_disconnect(sw);
1371 tb_tunnel_deactivate(tunnel);
1372 list_del(&tunnel->list);
1373 tb_tunnel_free(tunnel);
1377 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1379 struct tb_port *up, *down, *port;
1380 struct tb_cm *tcm = tb_priv(tb);
1381 struct tb_switch *parent_sw;
1382 struct tb_tunnel *tunnel;
1384 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1389 * Look up available down port. Since we are chaining it should
1390 * be found right above this switch.
1392 parent_sw = tb_to_switch(sw->dev.parent);
1393 port = tb_port_at(tb_route(sw), parent_sw);
1394 down = tb_find_pcie_down(parent_sw, port);
1398 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1402 if (tb_tunnel_activate(tunnel)) {
1404 "PCIe tunnel activation failed, aborting\n");
1405 tb_tunnel_free(tunnel);
1410 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
1413 if (tb_switch_pcie_l1_enable(sw))
1414 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
1416 if (tb_switch_xhci_connect(sw))
1417 tb_sw_warn(sw, "failed to connect xHCI\n");
1419 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1423 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1424 int transmit_path, int transmit_ring,
1425 int receive_path, int receive_ring)
1427 struct tb_cm *tcm = tb_priv(tb);
1428 struct tb_port *nhi_port, *dst_port;
1429 struct tb_tunnel *tunnel;
1430 struct tb_switch *sw;
1432 sw = tb_to_switch(xd->dev.parent);
1433 dst_port = tb_port_at(xd->route, sw);
1434 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1436 mutex_lock(&tb->lock);
1437 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1438 transmit_ring, receive_path, receive_ring);
1440 mutex_unlock(&tb->lock);
1444 if (tb_tunnel_activate(tunnel)) {
1445 tb_port_info(nhi_port,
1446 "DMA tunnel activation failed, aborting\n");
1447 tb_tunnel_free(tunnel);
1448 mutex_unlock(&tb->lock);
1452 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1453 mutex_unlock(&tb->lock);
1457 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1458 int transmit_path, int transmit_ring,
1459 int receive_path, int receive_ring)
1461 struct tb_cm *tcm = tb_priv(tb);
1462 struct tb_port *nhi_port, *dst_port;
1463 struct tb_tunnel *tunnel, *n;
1464 struct tb_switch *sw;
1466 sw = tb_to_switch(xd->dev.parent);
1467 dst_port = tb_port_at(xd->route, sw);
1468 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1470 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1471 if (!tb_tunnel_is_dma(tunnel))
1473 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1476 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1477 receive_path, receive_ring))
1478 tb_deactivate_and_free_tunnel(tunnel);
1482 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1483 int transmit_path, int transmit_ring,
1484 int receive_path, int receive_ring)
1486 if (!xd->is_unplugged) {
1487 mutex_lock(&tb->lock);
1488 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1489 transmit_ring, receive_path,
1491 mutex_unlock(&tb->lock);
1496 /* hotplug handling */
1499 * tb_handle_hotplug() - handle hotplug event
1501 * Executes on tb->wq.
1503 static void tb_handle_hotplug(struct work_struct *work)
1505 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1506 struct tb *tb = ev->tb;
1507 struct tb_cm *tcm = tb_priv(tb);
1508 struct tb_switch *sw;
1509 struct tb_port *port;
1511 /* Bring the domain back from sleep if it was suspended */
1512 pm_runtime_get_sync(&tb->dev);
1514 mutex_lock(&tb->lock);
1515 if (!tcm->hotplug_active)
1516 goto out; /* during init, suspend or shutdown */
1518 sw = tb_switch_find_by_route(tb, ev->route);
1521 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1522 ev->route, ev->port, ev->unplug);
1525 if (ev->port > sw->config.max_port_number) {
1527 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1528 ev->route, ev->port, ev->unplug);
1531 port = &sw->ports[ev->port];
1532 if (tb_is_upstream_port(port)) {
1533 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1534 ev->route, ev->port, ev->unplug);
1538 pm_runtime_get_sync(&sw->dev);
1541 tb_retimer_remove_all(port);
1543 if (tb_port_has_remote(port)) {
1544 tb_port_dbg(port, "switch unplugged\n");
1545 tb_sw_set_unplugged(port->remote->sw);
1546 tb_free_invalid_tunnels(tb);
1547 tb_remove_dp_resources(port->remote->sw);
1548 tb_switch_tmu_disable(port->remote->sw);
1549 tb_switch_unconfigure_link(port->remote->sw);
1550 tb_switch_lane_bonding_disable(port->remote->sw);
1551 tb_switch_remove(port->remote->sw);
1552 port->remote = NULL;
1553 if (port->dual_link_port)
1554 port->dual_link_port->remote = NULL;
1555 /* Maybe we can create another DP tunnel */
1556 tb_recalc_estimated_bandwidth(tb);
1558 } else if (port->xdomain) {
1559 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1561 tb_port_dbg(port, "xdomain unplugged\n");
1563 * Service drivers are unbound during
1564 * tb_xdomain_remove() so setting XDomain as
1565 * unplugged here prevents deadlock if they call
1566 * tb_xdomain_disable_paths(). We will tear down
1567 * all the tunnels below.
1569 xd->is_unplugged = true;
1570 tb_xdomain_remove(xd);
1571 port->xdomain = NULL;
1572 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1574 tb_port_unconfigure_xdomain(port);
1575 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1576 tb_dp_resource_unavailable(tb, port);
1577 } else if (!port->port) {
1578 tb_sw_dbg(sw, "xHCI disconnect request\n");
1579 tb_switch_xhci_disconnect(sw);
1582 "got unplug event for disconnected port, ignoring\n");
1584 } else if (port->remote) {
1585 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1586 } else if (!port->port && sw->authorized) {
1587 tb_sw_dbg(sw, "xHCI connect request\n");
1588 tb_switch_xhci_connect(sw);
1590 if (tb_port_is_null(port)) {
1591 tb_port_dbg(port, "hotplug: scanning\n");
1594 tb_port_dbg(port, "hotplug: no switch found\n");
1595 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1596 tb_dp_resource_available(tb, port);
1600 pm_runtime_mark_last_busy(&sw->dev);
1601 pm_runtime_put_autosuspend(&sw->dev);
1606 mutex_unlock(&tb->lock);
1608 pm_runtime_mark_last_busy(&tb->dev);
1609 pm_runtime_put_autosuspend(&tb->dev);
1614 static int tb_alloc_dp_bandwidth(struct tb_tunnel *tunnel, int *requested_up,
1615 int *requested_down)
1617 int allocated_up, allocated_down, available_up, available_down, ret;
1618 int requested_up_corrected, requested_down_corrected, granularity;
1619 int max_up, max_down, max_up_rounded, max_down_rounded;
1620 struct tb *tb = tunnel->tb;
1621 struct tb_port *in, *out;
1623 ret = tb_tunnel_allocated_bandwidth(tunnel, &allocated_up, &allocated_down);
1627 in = tunnel->src_port;
1628 out = tunnel->dst_port;
1630 tb_port_dbg(in, "bandwidth allocated currently %d/%d Mb/s\n",
1631 allocated_up, allocated_down);
1634 * If we get rounded up request from graphics side, say HBR2 x 4
1635 * that is 17500 instead of 17280 (this is because of the
1636 * granularity), we allow it too. Here the graphics has already
1637 * negotiated with the DPRX the maximum possible rates (which is
1638 * 17280 in this case).
1640 * Since the link cannot go higher than 17280 we use that in our
1641 * calculations but the DP IN adapter Allocated BW write must be
1642 * the same value (17500) otherwise the adapter will mark it as
1643 * failed for graphics.
1645 ret = tb_tunnel_maximum_bandwidth(tunnel, &max_up, &max_down);
1649 ret = usb4_dp_port_granularity(in);
1654 max_up_rounded = roundup(max_up, granularity);
1655 max_down_rounded = roundup(max_down, granularity);
1658 * This will "fix" the request down to the maximum supported
1659 * rate * lanes if it is at the maximum rounded up level.
1661 requested_up_corrected = *requested_up;
1662 if (requested_up_corrected == max_up_rounded)
1663 requested_up_corrected = max_up;
1664 else if (requested_up_corrected < 0)
1665 requested_up_corrected = 0;
1666 requested_down_corrected = *requested_down;
1667 if (requested_down_corrected == max_down_rounded)
1668 requested_down_corrected = max_down;
1669 else if (requested_down_corrected < 0)
1670 requested_down_corrected = 0;
1672 tb_port_dbg(in, "corrected bandwidth request %d/%d Mb/s\n",
1673 requested_up_corrected, requested_down_corrected);
1675 if ((*requested_up >= 0 && requested_up_corrected > max_up_rounded) ||
1676 (*requested_down >= 0 && requested_down_corrected > max_down_rounded)) {
1677 tb_port_dbg(in, "bandwidth request too high (%d/%d Mb/s > %d/%d Mb/s)\n",
1678 requested_up_corrected, requested_down_corrected,
1679 max_up_rounded, max_down_rounded);
1683 if ((*requested_up >= 0 && requested_up_corrected <= allocated_up) ||
1684 (*requested_down >= 0 && requested_down_corrected <= allocated_down)) {
1686 * If requested bandwidth is less or equal than what is
1687 * currently allocated to that tunnel we simply change
1688 * the reservation of the tunnel. Since all the tunnels
1689 * going out from the same USB4 port are in the same
1690 * group the released bandwidth will be taken into
1691 * account for the other tunnels automatically below.
1693 return tb_tunnel_alloc_bandwidth(tunnel, requested_up,
1698 * More bandwidth is requested. Release all the potential
1699 * bandwidth from USB3 first.
1701 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
1706 * Then go over all tunnels that cross the same USB4 ports (they
1707 * are also in the same group but we use the same function here
1708 * that we use with the normal bandwidth allocation).
1710 ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down);
1714 tb_port_dbg(in, "bandwidth available for allocation %d/%d Mb/s\n",
1715 available_up, available_down);
1717 if ((*requested_up >= 0 && available_up >= requested_up_corrected) ||
1718 (*requested_down >= 0 && available_down >= requested_down_corrected)) {
1719 ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up,
1726 tb_reclaim_usb3_bandwidth(tb, in, out);
1730 static void tb_handle_dp_bandwidth_request(struct work_struct *work)
1732 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1733 int requested_bw, requested_up, requested_down, ret;
1734 struct tb_port *in, *out;
1735 struct tb_tunnel *tunnel;
1736 struct tb *tb = ev->tb;
1737 struct tb_cm *tcm = tb_priv(tb);
1738 struct tb_switch *sw;
1740 pm_runtime_get_sync(&tb->dev);
1742 mutex_lock(&tb->lock);
1743 if (!tcm->hotplug_active)
1746 sw = tb_switch_find_by_route(tb, ev->route);
1748 tb_warn(tb, "bandwidth request from non-existent router %llx\n",
1753 in = &sw->ports[ev->port];
1754 if (!tb_port_is_dpin(in)) {
1755 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n");
1759 tb_port_dbg(in, "handling bandwidth allocation request\n");
1761 if (!usb4_dp_port_bw_mode_enabled(in)) {
1762 tb_port_warn(in, "bandwidth allocation mode not enabled\n");
1766 ret = usb4_dp_port_requested_bw(in);
1768 if (ret == -ENODATA)
1769 tb_port_dbg(in, "no bandwidth request active\n");
1771 tb_port_warn(in, "failed to read requested bandwidth\n");
1776 tb_port_dbg(in, "requested bandwidth %d Mb/s\n", requested_bw);
1778 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
1780 tb_port_warn(in, "failed to find tunnel\n");
1784 out = tunnel->dst_port;
1786 if (in->sw->config.depth < out->sw->config.depth) {
1788 requested_down = requested_bw;
1790 requested_up = requested_bw;
1791 requested_down = -1;
1794 ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down);
1796 if (ret == -ENOBUFS)
1797 tb_port_warn(in, "not enough bandwidth available\n");
1799 tb_port_warn(in, "failed to change bandwidth allocation\n");
1801 tb_port_dbg(in, "bandwidth allocation changed to %d/%d Mb/s\n",
1802 requested_up, requested_down);
1804 /* Update other clients about the allocation change */
1805 tb_recalc_estimated_bandwidth(tb);
1809 mutex_unlock(&tb->lock);
1811 pm_runtime_mark_last_busy(&tb->dev);
1812 pm_runtime_put_autosuspend(&tb->dev);
1815 static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port)
1817 struct tb_hotplug_event *ev;
1819 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
1826 INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request);
1827 queue_work(tb->wq, &ev->work);
1830 static void tb_handle_notification(struct tb *tb, u64 route,
1831 const struct cfg_error_pkg *error)
1833 if (tb_cfg_ack_notification(tb->ctl, route, error))
1834 tb_warn(tb, "could not ack notification on %llx\n", route);
1836 switch (error->error) {
1837 case TB_CFG_ERROR_DP_BW:
1838 tb_queue_dp_bandwidth_request(tb, route, error->port);
1848 * tb_schedule_hotplug_handler() - callback function for the control channel
1850 * Delegates to tb_handle_hotplug.
1852 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1853 const void *buf, size_t size)
1855 const struct cfg_event_pkg *pkg = buf;
1856 u64 route = tb_cfg_get_route(&pkg->header);
1859 case TB_CFG_PKG_ERROR:
1860 tb_handle_notification(tb, route, (const struct cfg_error_pkg *)buf);
1862 case TB_CFG_PKG_EVENT:
1865 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1869 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1870 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1874 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1877 static void tb_stop(struct tb *tb)
1879 struct tb_cm *tcm = tb_priv(tb);
1880 struct tb_tunnel *tunnel;
1881 struct tb_tunnel *n;
1883 cancel_delayed_work(&tcm->remove_work);
1884 /* tunnels are only present after everything has been initialized */
1885 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1887 * DMA tunnels require the driver to be functional so we
1888 * tear them down. Other protocol tunnels can be left
1891 if (tb_tunnel_is_dma(tunnel))
1892 tb_tunnel_deactivate(tunnel);
1893 tb_tunnel_free(tunnel);
1895 tb_switch_remove(tb->root_switch);
1896 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1899 static int tb_scan_finalize_switch(struct device *dev, void *data)
1901 if (tb_is_switch(dev)) {
1902 struct tb_switch *sw = tb_to_switch(dev);
1905 * If we found that the switch was already setup by the
1906 * boot firmware, mark it as authorized now before we
1907 * send uevent to userspace.
1912 dev_set_uevent_suppress(dev, false);
1913 kobject_uevent(&dev->kobj, KOBJ_ADD);
1914 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1920 static int tb_start(struct tb *tb)
1922 struct tb_cm *tcm = tb_priv(tb);
1925 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1926 if (IS_ERR(tb->root_switch))
1927 return PTR_ERR(tb->root_switch);
1930 * ICM firmware upgrade needs running firmware and in native
1931 * mode that is not available so disable firmware upgrade of the
1934 * However, USB4 routers support NVM firmware upgrade if they
1935 * implement the necessary router operations.
1937 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch);
1938 /* All USB4 routers support runtime PM */
1939 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1941 ret = tb_switch_configure(tb->root_switch);
1943 tb_switch_put(tb->root_switch);
1947 /* Announce the switch to the world */
1948 ret = tb_switch_add(tb->root_switch);
1950 tb_switch_put(tb->root_switch);
1955 * To support highest CLx state, we set host router's TMU to
1958 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_RATE_NORMAL,
1960 /* Enable TMU if it is off */
1961 tb_switch_tmu_enable(tb->root_switch);
1962 /* Full scan to discover devices added before the driver was loaded. */
1963 tb_scan_switch(tb->root_switch);
1964 /* Find out tunnels created by the boot firmware */
1965 tb_discover_tunnels(tb);
1966 /* Add DP resources from the DP tunnels created by the boot firmware */
1967 tb_discover_dp_resources(tb);
1969 * If the boot firmware did not create USB 3.x tunnels create them
1970 * now for the whole topology.
1972 tb_create_usb3_tunnels(tb->root_switch);
1973 /* Add DP IN resources for the root switch */
1974 tb_add_dp_resources(tb->root_switch);
1975 /* Make the discovered switches available to the userspace */
1976 device_for_each_child(&tb->root_switch->dev, NULL,
1977 tb_scan_finalize_switch);
1979 /* Allow tb_handle_hotplug to progress events */
1980 tcm->hotplug_active = true;
1984 static int tb_suspend_noirq(struct tb *tb)
1986 struct tb_cm *tcm = tb_priv(tb);
1988 tb_dbg(tb, "suspending...\n");
1989 tb_disconnect_and_release_dp(tb);
1990 tb_switch_suspend(tb->root_switch, false);
1991 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1992 tb_dbg(tb, "suspend finished\n");
1997 static void tb_restore_children(struct tb_switch *sw)
1999 struct tb_port *port;
2002 /* No need to restore if the router is already unplugged */
2003 if (sw->is_unplugged)
2007 * CL0s and CL1 are enabled and supported together.
2008 * Silently ignore CLx re-enabling in case CLx is not supported.
2010 ret = tb_switch_enable_clx(sw, TB_CL1);
2011 if (ret && ret != -EOPNOTSUPP)
2012 tb_sw_warn(sw, "failed to re-enable %s on upstream port\n",
2013 tb_switch_clx_name(TB_CL1));
2015 if (tb_switch_is_clx_enabled(sw, TB_CL1))
2017 * To support highest CLx state, we set router's TMU to
2020 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true);
2022 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/
2023 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false);
2025 if (tb_enable_tmu(sw))
2026 tb_sw_warn(sw, "failed to restore TMU configuration\n");
2028 tb_switch_for_each_port(sw, port) {
2029 if (!tb_port_has_remote(port) && !port->xdomain)
2033 tb_switch_lane_bonding_enable(port->remote->sw);
2034 tb_switch_configure_link(port->remote->sw);
2036 tb_restore_children(port->remote->sw);
2037 } else if (port->xdomain) {
2038 tb_port_configure_xdomain(port, port->xdomain);
2043 static int tb_resume_noirq(struct tb *tb)
2045 struct tb_cm *tcm = tb_priv(tb);
2046 struct tb_tunnel *tunnel, *n;
2047 unsigned int usb3_delay = 0;
2050 tb_dbg(tb, "resuming...\n");
2052 /* remove any pci devices the firmware might have setup */
2053 tb_switch_reset(tb->root_switch);
2055 tb_switch_resume(tb->root_switch);
2056 tb_free_invalid_tunnels(tb);
2057 tb_free_unplugged_children(tb->root_switch);
2058 tb_restore_children(tb->root_switch);
2061 * If we get here from suspend to disk the boot firmware or the
2062 * restore kernel might have created tunnels of its own. Since
2063 * we cannot be sure they are usable for us we find and tear
2066 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
2067 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
2068 if (tb_tunnel_is_usb3(tunnel))
2070 tb_tunnel_deactivate(tunnel);
2071 tb_tunnel_free(tunnel);
2074 /* Re-create our tunnels now */
2075 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
2076 /* USB3 requires delay before it can be re-activated */
2077 if (tb_tunnel_is_usb3(tunnel)) {
2079 /* Only need to do it once */
2082 tb_tunnel_restart(tunnel);
2084 if (!list_empty(&tcm->tunnel_list)) {
2086 * the pcie links need some time to get going.
2087 * 100ms works for me...
2089 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
2092 /* Allow tb_handle_hotplug to progress events */
2093 tcm->hotplug_active = true;
2094 tb_dbg(tb, "resume finished\n");
2099 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
2101 struct tb_port *port;
2104 tb_switch_for_each_port(sw, port) {
2105 if (tb_is_upstream_port(port))
2107 if (port->xdomain && port->xdomain->is_unplugged) {
2108 tb_retimer_remove_all(port);
2109 tb_xdomain_remove(port->xdomain);
2110 tb_port_unconfigure_xdomain(port);
2111 port->xdomain = NULL;
2113 } else if (port->remote) {
2114 ret += tb_free_unplugged_xdomains(port->remote->sw);
2121 static int tb_freeze_noirq(struct tb *tb)
2123 struct tb_cm *tcm = tb_priv(tb);
2125 tcm->hotplug_active = false;
2129 static int tb_thaw_noirq(struct tb *tb)
2131 struct tb_cm *tcm = tb_priv(tb);
2133 tcm->hotplug_active = true;
2137 static void tb_complete(struct tb *tb)
2140 * Release any unplugged XDomains and if there is a case where
2141 * another domain is swapped in place of unplugged XDomain we
2142 * need to run another rescan.
2144 mutex_lock(&tb->lock);
2145 if (tb_free_unplugged_xdomains(tb->root_switch))
2146 tb_scan_switch(tb->root_switch);
2147 mutex_unlock(&tb->lock);
2150 static int tb_runtime_suspend(struct tb *tb)
2152 struct tb_cm *tcm = tb_priv(tb);
2154 mutex_lock(&tb->lock);
2155 tb_switch_suspend(tb->root_switch, true);
2156 tcm->hotplug_active = false;
2157 mutex_unlock(&tb->lock);
2162 static void tb_remove_work(struct work_struct *work)
2164 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
2165 struct tb *tb = tcm_to_tb(tcm);
2167 mutex_lock(&tb->lock);
2168 if (tb->root_switch) {
2169 tb_free_unplugged_children(tb->root_switch);
2170 tb_free_unplugged_xdomains(tb->root_switch);
2172 mutex_unlock(&tb->lock);
2175 static int tb_runtime_resume(struct tb *tb)
2177 struct tb_cm *tcm = tb_priv(tb);
2178 struct tb_tunnel *tunnel, *n;
2180 mutex_lock(&tb->lock);
2181 tb_switch_resume(tb->root_switch);
2182 tb_free_invalid_tunnels(tb);
2183 tb_restore_children(tb->root_switch);
2184 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
2185 tb_tunnel_restart(tunnel);
2186 tcm->hotplug_active = true;
2187 mutex_unlock(&tb->lock);
2190 * Schedule cleanup of any unplugged devices. Run this in a
2191 * separate thread to avoid possible deadlock if the device
2192 * removal runtime resumes the unplugged device.
2194 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
2198 static const struct tb_cm_ops tb_cm_ops = {
2201 .suspend_noirq = tb_suspend_noirq,
2202 .resume_noirq = tb_resume_noirq,
2203 .freeze_noirq = tb_freeze_noirq,
2204 .thaw_noirq = tb_thaw_noirq,
2205 .complete = tb_complete,
2206 .runtime_suspend = tb_runtime_suspend,
2207 .runtime_resume = tb_runtime_resume,
2208 .handle_event = tb_handle_event,
2209 .disapprove_switch = tb_disconnect_pci,
2210 .approve_switch = tb_tunnel_pci,
2211 .approve_xdomain_paths = tb_approve_xdomain_paths,
2212 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
2216 * During suspend the Thunderbolt controller is reset and all PCIe
2217 * tunnels are lost. The NHI driver will try to reestablish all tunnels
2218 * during resume. This adds device links between the tunneled PCIe
2219 * downstream ports and the NHI so that the device core will make sure
2220 * NHI is resumed first before the rest.
2222 static void tb_apple_add_links(struct tb_nhi *nhi)
2224 struct pci_dev *upstream, *pdev;
2226 if (!x86_apple_machine)
2229 switch (nhi->pdev->device) {
2230 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
2231 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
2232 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2233 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
2239 upstream = pci_upstream_bridge(nhi->pdev);
2241 if (!pci_is_pcie(upstream))
2243 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
2245 upstream = pci_upstream_bridge(upstream);
2252 * For each hotplug downstream port, create add device link
2253 * back to NHI so that PCIe tunnels can be re-established after
2256 for_each_pci_bridge(pdev, upstream->subordinate) {
2257 const struct device_link *link;
2259 if (!pci_is_pcie(pdev))
2261 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
2262 !pdev->is_hotplug_bridge)
2265 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
2266 DL_FLAG_AUTOREMOVE_SUPPLIER |
2267 DL_FLAG_PM_RUNTIME);
2269 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
2270 dev_name(&pdev->dev));
2272 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
2273 dev_name(&pdev->dev));
2278 struct tb *tb_probe(struct tb_nhi *nhi)
2283 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
2287 if (tb_acpi_may_tunnel_pcie())
2288 tb->security_level = TB_SECURITY_USER;
2290 tb->security_level = TB_SECURITY_NOPCIE;
2292 tb->cm_ops = &tb_cm_ops;
2295 INIT_LIST_HEAD(&tcm->tunnel_list);
2296 INIT_LIST_HEAD(&tcm->dp_resources);
2297 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
2298 tb_init_bandwidth_groups(tcm);
2300 tb_dbg(tb, "using software connection manager\n");
2302 tb_apple_add_links(nhi);
2303 tb_acpi_add_links(nhi);