1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt driver - bus logic (NHI independent) 4 * 5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 6 * Copyright (C) 2019, Intel Corporation 7 */ 8 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> 14 15 #include "tb.h" 16 #include "tb_regs.h" 17 #include "tunnel.h" 18 19 #define TB_TIMEOUT 100 /* ms */ 20 #define TB_RELEASE_BW_TIMEOUT 10000 /* ms */ 21 22 /* 23 * How many time bandwidth allocation request from graphics driver is 24 * retried if the DP tunnel is still activating. 25 */ 26 #define TB_BW_ALLOC_RETRIES 3 27 28 /* 29 * Minimum bandwidth (in Mb/s) that is needed in the single transmitter/receiver 30 * direction. This is 40G - 10% guard band bandwidth. 31 */ 32 #define TB_ASYM_MIN (40000 * 90 / 100) 33 34 /* 35 * Threshold bandwidth (in Mb/s) that is used to switch the links to 36 * asymmetric and back. This is selected as 45G which means when the 37 * request is higher than this, we switch the link to asymmetric, and 38 * when it is less than this we switch it back. The 45G is selected so 39 * that we still have 27G (of the total 72G) for bulk PCIe traffic when 40 * switching back to symmetric. 41 */ 42 #define TB_ASYM_THRESHOLD 45000 43 44 #define MAX_GROUPS 7 /* max Group_ID is 7 */ 45 46 static unsigned int asym_threshold = TB_ASYM_THRESHOLD; 47 module_param_named(asym_threshold, asym_threshold, uint, 0444); 48 MODULE_PARM_DESC(asym_threshold, 49 "threshold (Mb/s) when to Gen 4 switch link symmetry. 0 disables. (default: " 50 __MODULE_STRING(TB_ASYM_THRESHOLD) ")"); 51 52 /** 53 * struct tb_cm - Simple Thunderbolt connection manager 54 * @tunnel_list: List of active tunnels 55 * @dp_resources: List of available DP resources for DP tunneling 56 * @hotplug_active: tb_handle_hotplug will stop progressing plug 57 * events and exit if this is not set (it needs to 58 * acquire the lock one more time). Used to drain wq 59 * after cfg has been paused. 60 * @remove_work: Work used to remove any unplugged routers after 61 * runtime resume 62 * @groups: Bandwidth groups used in this domain. 63 */ 64 struct tb_cm { 65 struct list_head tunnel_list; 66 struct list_head dp_resources; 67 bool hotplug_active; 68 struct delayed_work remove_work; 69 struct tb_bandwidth_group groups[MAX_GROUPS]; 70 }; 71 72 static inline struct tb *tcm_to_tb(struct tb_cm *tcm) 73 { 74 return ((void *)tcm - sizeof(struct tb)); 75 } 76 77 struct tb_hotplug_event { 78 struct delayed_work work; 79 struct tb *tb; 80 u64 route; 81 u8 port; 82 bool unplug; 83 int retry; 84 }; 85 86 static void tb_scan_port(struct tb_port *port); 87 static void tb_handle_hotplug(struct work_struct *work); 88 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port, 89 const char *reason); 90 static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port, 91 int retry, unsigned long delay); 92 93 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug) 94 { 95 struct tb_hotplug_event *ev; 96 97 ev = kmalloc_obj(*ev); 98 if (!ev) 99 return; 100 101 ev->tb = tb_domain_get(tb); 102 ev->route = route; 103 ev->port = port; 104 ev->unplug = unplug; 105 INIT_DELAYED_WORK(&ev->work, tb_handle_hotplug); 106 queue_delayed_work(tb->wq, &ev->work, 0); 107 } 108 109 /* enumeration & hot plug handling */ 110 111 static void tb_add_dp_resources(struct tb_switch *sw) 112 { 113 struct tb_cm *tcm = tb_priv(sw->tb); 114 struct tb_port *port; 115 116 tb_switch_for_each_port(sw, port) { 117 if (!tb_port_is_dpin(port)) 118 continue; 119 120 if (!tb_switch_query_dp_resource(sw, port)) 121 continue; 122 123 /* 124 * If DP IN on device router exist, position it at the 125 * beginning of the DP resources list, so that it is used 126 * before DP IN of the host router. This way external GPU(s) 127 * will be prioritized when pairing DP IN to a DP OUT. 128 */ 129 if (tb_route(sw)) 130 list_add(&port->list, &tcm->dp_resources); 131 else 132 list_add_tail(&port->list, &tcm->dp_resources); 133 134 tb_port_dbg(port, "DP IN resource available\n"); 135 } 136 } 137 138 static void tb_remove_dp_resources(struct tb_switch *sw) 139 { 140 struct tb_cm *tcm = tb_priv(sw->tb); 141 struct tb_port *port, *tmp; 142 143 /* Clear children resources first */ 144 tb_switch_for_each_port(sw, port) { 145 if (tb_port_has_remote(port)) 146 tb_remove_dp_resources(port->remote->sw); 147 } 148 149 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { 150 if (port->sw == sw) { 151 tb_port_dbg(port, "DP OUT resource unavailable\n"); 152 list_del_init(&port->list); 153 } 154 } 155 } 156 157 static void tb_discover_dp_resource(struct tb *tb, struct tb_port *port) 158 { 159 struct tb_cm *tcm = tb_priv(tb); 160 struct tb_port *p; 161 162 list_for_each_entry(p, &tcm->dp_resources, list) { 163 if (p == port) 164 return; 165 } 166 167 tb_port_dbg(port, "DP %s resource available discovered\n", 168 tb_port_is_dpin(port) ? "IN" : "OUT"); 169 list_add_tail(&port->list, &tcm->dp_resources); 170 } 171 172 static void tb_discover_dp_resources(struct tb *tb) 173 { 174 struct tb_cm *tcm = tb_priv(tb); 175 struct tb_tunnel *tunnel; 176 177 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 178 if (tb_tunnel_is_dp(tunnel)) 179 tb_discover_dp_resource(tb, tunnel->dst_port); 180 } 181 } 182 183 /* Enables CL states up to host router */ 184 static int tb_enable_clx(struct tb_switch *sw) 185 { 186 struct tb_cm *tcm = tb_priv(sw->tb); 187 unsigned int clx = TB_CL0S | TB_CL1; 188 const struct tb_tunnel *tunnel; 189 int ret; 190 191 /* 192 * Currently only enable CLx for the first link. This is enough 193 * to allow the CPU to save energy at least on Intel hardware 194 * and makes it slightly simpler to implement. We may change 195 * this in the future to cover the whole topology if it turns 196 * out to be beneficial. 197 */ 198 while (sw && tb_switch_depth(sw) > 1) 199 sw = tb_switch_parent(sw); 200 201 if (!sw) 202 return 0; 203 204 if (tb_switch_depth(sw) != 1) 205 return 0; 206 207 /* 208 * If we are re-enabling then check if there is an active DMA 209 * tunnel and in that case bail out. 210 */ 211 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 212 if (tb_tunnel_is_dma(tunnel)) { 213 if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw))) 214 return 0; 215 } 216 } 217 218 /* 219 * Initially try with CL2. If that's not supported by the 220 * topology try with CL0s and CL1 and then give up. 221 */ 222 ret = tb_switch_clx_enable(sw, clx | TB_CL2); 223 if (ret == -EOPNOTSUPP) 224 ret = tb_switch_clx_enable(sw, clx); 225 return ret == -EOPNOTSUPP ? 0 : ret; 226 } 227 228 /* 229 * Disables CL states from @sw up to the host router. 230 * 231 * This can be used to figure out whether the link was setup by us or the 232 * boot firmware so we don't accidentally enable them if they were not 233 * enabled during discovery. 234 */ 235 static bool tb_disable_clx(struct tb_switch *sw) 236 { 237 bool disabled = false; 238 239 do { 240 int ret; 241 242 ret = tb_switch_clx_disable(sw); 243 if (ret > 0) 244 disabled = true; 245 else if (ret < 0) 246 tb_sw_warn(sw, "failed to disable CL states\n"); 247 248 sw = tb_switch_parent(sw); 249 } while (sw); 250 251 return disabled; 252 } 253 254 static int tb_increase_switch_tmu_accuracy(struct device *dev, void *data) 255 { 256 struct tb_switch *sw; 257 258 sw = tb_to_switch(dev); 259 if (!sw) 260 return 0; 261 262 if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) { 263 enum tb_switch_tmu_mode mode; 264 int ret; 265 266 if (tb_switch_clx_is_enabled(sw, TB_CL1)) 267 mode = TB_SWITCH_TMU_MODE_HIFI_UNI; 268 else 269 mode = TB_SWITCH_TMU_MODE_HIFI_BI; 270 271 ret = tb_switch_tmu_configure(sw, mode); 272 if (ret) 273 return ret; 274 275 return tb_switch_tmu_enable(sw); 276 } 277 278 return 0; 279 } 280 281 static void tb_increase_tmu_accuracy(struct tb_tunnel *tunnel) 282 { 283 struct tb_switch *sw; 284 285 if (!tunnel) 286 return; 287 288 /* 289 * Once first DP tunnel is established we change the TMU 290 * accuracy of first depth child routers (and the host router) 291 * to the highest. This is needed for the DP tunneling to work 292 * but also allows CL0s. 293 * 294 * If both routers are v2 then we don't need to do anything as 295 * they are using enhanced TMU mode that allows all CLx. 296 */ 297 sw = tunnel->tb->root_switch; 298 device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy); 299 } 300 301 static int tb_switch_tmu_hifi_uni_required(struct device *dev, void *not_used) 302 { 303 struct tb_switch *sw = tb_to_switch(dev); 304 305 if (sw && tb_switch_tmu_is_enabled(sw) && 306 tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_HIFI_UNI)) 307 return 1; 308 309 return device_for_each_child(dev, NULL, 310 tb_switch_tmu_hifi_uni_required); 311 } 312 313 static bool tb_tmu_hifi_uni_required(struct tb *tb) 314 { 315 return device_for_each_child(&tb->dev, NULL, 316 tb_switch_tmu_hifi_uni_required) == 1; 317 } 318 319 static int tb_enable_tmu(struct tb_switch *sw) 320 { 321 int ret; 322 323 /* 324 * If both routers at the end of the link are v2 we simply 325 * enable the enhanced uni-directional mode. That covers all 326 * the CL states. For v1 and before we need to use the normal 327 * rate to allow CL1 (when supported). Otherwise we keep the TMU 328 * running at the highest accuracy. 329 */ 330 ret = tb_switch_tmu_configure(sw, 331 TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI); 332 if (ret == -EOPNOTSUPP) { 333 if (tb_switch_clx_is_enabled(sw, TB_CL1)) { 334 /* 335 * Figure out uni-directional HiFi TMU requirements 336 * currently in the domain. If there are no 337 * uni-directional HiFi requirements we can put the TMU 338 * into LowRes mode. 339 * 340 * Deliberately skip bi-directional HiFi links 341 * as these work independently of other links 342 * (and they do not allow any CL states anyway). 343 */ 344 if (tb_tmu_hifi_uni_required(sw->tb)) 345 ret = tb_switch_tmu_configure(sw, 346 TB_SWITCH_TMU_MODE_HIFI_UNI); 347 else 348 ret = tb_switch_tmu_configure(sw, 349 TB_SWITCH_TMU_MODE_LOWRES); 350 } else { 351 ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI); 352 } 353 354 /* If not supported, fallback to bi-directional HiFi */ 355 if (ret == -EOPNOTSUPP) 356 ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI); 357 } 358 if (ret) 359 return ret; 360 361 /* If it is already enabled in correct mode, don't touch it */ 362 if (tb_switch_tmu_is_enabled(sw)) 363 return 0; 364 365 ret = tb_switch_tmu_disable(sw); 366 if (ret) 367 return ret; 368 369 ret = tb_switch_tmu_post_time(sw); 370 if (ret) 371 return ret; 372 373 return tb_switch_tmu_enable(sw); 374 } 375 376 static void tb_switch_discover_tunnels(struct tb_switch *sw, 377 struct list_head *list, 378 bool alloc_hopids) 379 { 380 struct tb *tb = sw->tb; 381 struct tb_port *port; 382 383 tb_switch_for_each_port(sw, port) { 384 struct tb_tunnel *tunnel = NULL; 385 386 switch (port->config.type) { 387 case TB_TYPE_DP_HDMI_IN: 388 tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids); 389 tb_increase_tmu_accuracy(tunnel); 390 break; 391 392 case TB_TYPE_PCIE_DOWN: 393 tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids); 394 break; 395 396 case TB_TYPE_USB3_DOWN: 397 tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids); 398 break; 399 400 default: 401 break; 402 } 403 404 if (tunnel) 405 list_add_tail(&tunnel->list, list); 406 } 407 408 tb_switch_for_each_port(sw, port) { 409 if (tb_port_has_remote(port)) { 410 tb_switch_discover_tunnels(port->remote->sw, list, 411 alloc_hopids); 412 } 413 } 414 } 415 416 static int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd) 417 { 418 if (tb_switch_is_usb4(port->sw)) 419 return usb4_port_configure_xdomain(port, xd); 420 return tb_lc_configure_xdomain(port); 421 } 422 423 static void tb_port_unconfigure_xdomain(struct tb_port *port) 424 { 425 if (tb_switch_is_usb4(port->sw)) 426 usb4_port_unconfigure_xdomain(port); 427 else 428 tb_lc_unconfigure_xdomain(port); 429 } 430 431 static void tb_scan_xdomain(struct tb_port *port) 432 { 433 struct tb_switch *sw = port->sw; 434 struct tb *tb = sw->tb; 435 struct tb_xdomain *xd; 436 u64 route; 437 438 if (!tb_is_xdomain_enabled()) 439 return; 440 441 route = tb_downstream_route(port); 442 xd = tb_xdomain_find_by_route(tb, route); 443 if (xd) { 444 tb_xdomain_put(xd); 445 return; 446 } 447 448 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, 449 NULL); 450 if (xd) { 451 tb_port_at(route, sw)->xdomain = xd; 452 tb_port_configure_xdomain(port, xd); 453 tb_xdomain_add(xd); 454 } 455 } 456 457 /* 458 * Returns the first inactive port on @sw. 459 */ 460 static struct tb_port *tb_find_unused_port(struct tb_switch *sw, 461 enum tb_port_type type) 462 { 463 struct tb_port *port; 464 465 tb_switch_for_each_port(sw, port) { 466 if (tb_is_upstream_port(port)) 467 continue; 468 if (port->config.type != type) 469 continue; 470 if (!port->cap_adap) 471 continue; 472 if (tb_port_is_enabled(port)) 473 continue; 474 return port; 475 } 476 return NULL; 477 } 478 479 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw, 480 const struct tb_port *port) 481 { 482 struct tb_port *down; 483 484 down = usb4_switch_map_usb3_down(sw, port); 485 if (down && !tb_usb3_port_is_enabled(down)) 486 return down; 487 return NULL; 488 } 489 490 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type, 491 struct tb_port *src_port, 492 struct tb_port *dst_port) 493 { 494 struct tb_cm *tcm = tb_priv(tb); 495 struct tb_tunnel *tunnel; 496 497 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 498 if (tunnel->type == type && 499 ((src_port && src_port == tunnel->src_port) || 500 (dst_port && dst_port == tunnel->dst_port))) { 501 return tunnel; 502 } 503 } 504 505 return NULL; 506 } 507 508 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb, 509 struct tb_port *src_port, 510 struct tb_port *dst_port) 511 { 512 struct tb_port *port, *usb3_down; 513 struct tb_switch *sw; 514 515 /* Pick the router that is deepest in the topology */ 516 if (tb_port_path_direction_downstream(src_port, dst_port)) 517 sw = dst_port->sw; 518 else 519 sw = src_port->sw; 520 521 /* Can't be the host router */ 522 if (sw == tb->root_switch) 523 return NULL; 524 525 /* Find the downstream USB4 port that leads to this router */ 526 port = tb_port_at(tb_route(sw), tb->root_switch); 527 /* Find the corresponding host router USB3 downstream port */ 528 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); 529 if (!usb3_down) 530 return NULL; 531 532 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL); 533 } 534 535 /** 536 * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link 537 * @tb: Domain structure 538 * @src_port: Source protocol adapter 539 * @dst_port: Destination protocol adapter 540 * @port: USB4 port the consumed bandwidth is calculated 541 * @consumed_up: Consumed upstream bandwidth (Mb/s) 542 * @consumed_down: Consumed downstream bandwidth (Mb/s) 543 * 544 * Calculates consumed USB3 and PCIe bandwidth at @port between path 545 * from @src_port to @dst_port. Does not take USB3 tunnel starting from 546 * @src_port and ending on @src_port into account because that bandwidth is 547 * already included in as part of the "first hop" USB3 tunnel. 548 * 549 * Return: %0 on success, negative errno otherwise. 550 */ 551 static int tb_consumed_usb3_pcie_bandwidth(struct tb *tb, 552 struct tb_port *src_port, 553 struct tb_port *dst_port, 554 struct tb_port *port, 555 int *consumed_up, 556 int *consumed_down) 557 { 558 int pci_consumed_up, pci_consumed_down; 559 struct tb_tunnel *tunnel; 560 561 *consumed_up = *consumed_down = 0; 562 563 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); 564 if (tunnel && !tb_port_is_usb3_down(src_port) && 565 !tb_port_is_usb3_up(dst_port)) { 566 int ret; 567 568 ret = tb_tunnel_consumed_bandwidth(tunnel, consumed_up, 569 consumed_down); 570 if (ret) 571 return ret; 572 } 573 574 /* 575 * If there is anything reserved for PCIe bulk traffic take it 576 * into account here too. 577 */ 578 if (tb_tunnel_reserved_pci(port, &pci_consumed_up, &pci_consumed_down)) { 579 *consumed_up += pci_consumed_up; 580 *consumed_down += pci_consumed_down; 581 } 582 583 return 0; 584 } 585 586 /** 587 * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link 588 * @tb: Domain structure 589 * @src_port: Source protocol adapter 590 * @dst_port: Destination protocol adapter 591 * @port: USB4 port the consumed bandwidth is calculated 592 * @consumed_up: Consumed upstream bandwidth (Mb/s) 593 * @consumed_down: Consumed downstream bandwidth (Mb/s) 594 * 595 * Calculates consumed DP bandwidth at @port between path from @src_port 596 * to @dst_port. Does not take tunnel starting from @src_port and ending 597 * from @src_port into account. 598 * 599 * If there is bandwidth reserved for any of the groups between 600 * @src_port and @dst_port (but not yet used) that is also taken into 601 * account in the returned consumed bandwidth. 602 * 603 * Return: %0 on success, negative errno otherwise. 604 */ 605 static int tb_consumed_dp_bandwidth(struct tb *tb, 606 struct tb_port *src_port, 607 struct tb_port *dst_port, 608 struct tb_port *port, 609 int *consumed_up, 610 int *consumed_down) 611 { 612 int group_reserved[MAX_GROUPS] = {}; 613 struct tb_cm *tcm = tb_priv(tb); 614 struct tb_tunnel *tunnel; 615 bool downstream; 616 int i, ret; 617 618 *consumed_up = *consumed_down = 0; 619 620 /* 621 * Find all DP tunnels that cross the port and reduce 622 * their consumed bandwidth from the available. 623 */ 624 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 625 const struct tb_bandwidth_group *group; 626 int dp_consumed_up, dp_consumed_down; 627 628 if (tb_tunnel_is_invalid(tunnel)) 629 continue; 630 631 if (!tb_tunnel_is_dp(tunnel)) 632 continue; 633 634 if (!tb_tunnel_port_on_path(tunnel, port)) 635 continue; 636 637 /* 638 * Calculate what is reserved for groups crossing the 639 * same ports only once (as that is reserved for all the 640 * tunnels in the group). 641 */ 642 group = tunnel->src_port->group; 643 if (group && group->reserved && !group_reserved[group->index]) 644 group_reserved[group->index] = group->reserved; 645 646 /* 647 * Ignore the DP tunnel between src_port and dst_port 648 * because it is the same tunnel and we may be 649 * re-calculating estimated bandwidth. 650 */ 651 if (tunnel->src_port == src_port && 652 tunnel->dst_port == dst_port) 653 continue; 654 655 ret = tb_tunnel_consumed_bandwidth(tunnel, &dp_consumed_up, 656 &dp_consumed_down); 657 if (ret) 658 return ret; 659 660 *consumed_up += dp_consumed_up; 661 *consumed_down += dp_consumed_down; 662 } 663 664 downstream = tb_port_path_direction_downstream(src_port, dst_port); 665 for (i = 0; i < ARRAY_SIZE(group_reserved); i++) { 666 if (downstream) 667 *consumed_down += group_reserved[i]; 668 else 669 *consumed_up += group_reserved[i]; 670 } 671 672 return 0; 673 } 674 675 static bool tb_asym_supported(struct tb_port *src_port, struct tb_port *dst_port, 676 struct tb_port *port) 677 { 678 bool downstream = tb_port_path_direction_downstream(src_port, dst_port); 679 enum tb_link_width width; 680 681 if (tb_is_upstream_port(port)) 682 width = downstream ? TB_LINK_WIDTH_ASYM_RX : TB_LINK_WIDTH_ASYM_TX; 683 else 684 width = downstream ? TB_LINK_WIDTH_ASYM_TX : TB_LINK_WIDTH_ASYM_RX; 685 686 return tb_port_width_supported(port, width); 687 } 688 689 /** 690 * tb_maximum_bandwidth() - Maximum bandwidth over a single link 691 * @tb: Domain structure 692 * @src_port: Source protocol adapter 693 * @dst_port: Destination protocol adapter 694 * @port: USB4 port the total bandwidth is calculated 695 * @max_up: Maximum upstream bandwidth (Mb/s) 696 * @max_down: Maximum downstream bandwidth (Mb/s) 697 * @include_asym: Include bandwidth if the link is switched from 698 * symmetric to asymmetric 699 * 700 * Returns maximum possible bandwidth in @max_up and @max_down over a 701 * single link at @port. If @include_asym is set then includes the 702 * additional banwdith if the links are transitioned into asymmetric to 703 * direction from @src_port to @dst_port. 704 * 705 * Return: %0 on success, negative errno otherwise. 706 */ 707 static int tb_maximum_bandwidth(struct tb *tb, struct tb_port *src_port, 708 struct tb_port *dst_port, struct tb_port *port, 709 int *max_up, int *max_down, bool include_asym) 710 { 711 bool downstream = tb_port_path_direction_downstream(src_port, dst_port); 712 int link_speed, link_width, up_bw, down_bw; 713 714 /* 715 * Can include asymmetric, only if it is actually supported by 716 * the lane adapter. 717 */ 718 if (!tb_asym_supported(src_port, dst_port, port)) 719 include_asym = false; 720 721 if (tb_is_upstream_port(port)) { 722 link_speed = port->sw->link_speed; 723 /* 724 * sw->link_width is from upstream perspective so we use 725 * the opposite for downstream of the host router. 726 */ 727 if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) { 728 up_bw = link_speed * 3 * 1000; 729 down_bw = link_speed * 1 * 1000; 730 } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) { 731 up_bw = link_speed * 1 * 1000; 732 down_bw = link_speed * 3 * 1000; 733 } else if (include_asym) { 734 /* 735 * The link is symmetric at the moment but we 736 * can switch it to asymmetric as needed. Report 737 * this bandwidth as available (even though it 738 * is not yet enabled). 739 */ 740 if (downstream) { 741 up_bw = link_speed * 1 * 1000; 742 down_bw = link_speed * 3 * 1000; 743 } else { 744 up_bw = link_speed * 3 * 1000; 745 down_bw = link_speed * 1 * 1000; 746 } 747 } else { 748 up_bw = link_speed * port->sw->link_width * 1000; 749 down_bw = up_bw; 750 } 751 } else { 752 link_speed = tb_port_get_link_speed(port); 753 if (link_speed < 0) 754 return link_speed; 755 756 link_width = tb_port_get_link_width(port); 757 if (link_width < 0) 758 return link_width; 759 760 if (link_width == TB_LINK_WIDTH_ASYM_TX) { 761 up_bw = link_speed * 1 * 1000; 762 down_bw = link_speed * 3 * 1000; 763 } else if (link_width == TB_LINK_WIDTH_ASYM_RX) { 764 up_bw = link_speed * 3 * 1000; 765 down_bw = link_speed * 1 * 1000; 766 } else if (include_asym) { 767 /* 768 * The link is symmetric at the moment but we 769 * can switch it to asymmetric as needed. Report 770 * this bandwidth as available (even though it 771 * is not yet enabled). 772 */ 773 if (downstream) { 774 up_bw = link_speed * 1 * 1000; 775 down_bw = link_speed * 3 * 1000; 776 } else { 777 up_bw = link_speed * 3 * 1000; 778 down_bw = link_speed * 1 * 1000; 779 } 780 } else { 781 up_bw = link_speed * link_width * 1000; 782 down_bw = up_bw; 783 } 784 } 785 786 /* Leave 10% guard band */ 787 *max_up = up_bw - up_bw / 10; 788 *max_down = down_bw - down_bw / 10; 789 790 tb_port_dbg(port, "link maximum bandwidth %d/%d Mb/s\n", *max_up, *max_down); 791 return 0; 792 } 793 794 /** 795 * tb_available_bandwidth() - Available bandwidth for tunneling 796 * @tb: Domain structure 797 * @src_port: Source protocol adapter 798 * @dst_port: Destination protocol adapter 799 * @available_up: Available bandwidth upstream (Mb/s) 800 * @available_down: Available bandwidth downstream (Mb/s) 801 * @include_asym: Include bandwidth if the link is switched from 802 * symmetric to asymmetric 803 * 804 * Calculates maximum available bandwidth for protocol tunneling between 805 * @src_port and @dst_port at the moment. This is minimum of maximum 806 * link bandwidth across all links reduced by currently consumed 807 * bandwidth on that link. 808 * 809 * If @include_asym is true then includes also bandwidth that can be 810 * added when the links are transitioned into asymmetric (but does not 811 * transition the links). 812 * 813 * Return: %0 on success, negative errno otherwise. 814 */ 815 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port, 816 struct tb_port *dst_port, int *available_up, 817 int *available_down, bool include_asym) 818 { 819 struct tb_port *port; 820 int ret; 821 822 /* Maximum possible bandwidth asymmetric Gen 4 link is 120 Gb/s */ 823 *available_up = *available_down = 120000; 824 825 /* Find the minimum available bandwidth over all links */ 826 tb_for_each_port_on_path(src_port, dst_port, port) { 827 int max_up, max_down, consumed_up, consumed_down; 828 829 if (!tb_port_is_null(port)) 830 continue; 831 832 ret = tb_maximum_bandwidth(tb, src_port, dst_port, port, 833 &max_up, &max_down, include_asym); 834 if (ret) 835 return ret; 836 837 ret = tb_consumed_usb3_pcie_bandwidth(tb, src_port, dst_port, 838 port, &consumed_up, 839 &consumed_down); 840 if (ret) 841 return ret; 842 max_up -= consumed_up; 843 max_down -= consumed_down; 844 845 ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, port, 846 &consumed_up, &consumed_down); 847 if (ret) 848 return ret; 849 max_up -= consumed_up; 850 max_down -= consumed_down; 851 852 if (max_up < *available_up) 853 *available_up = max_up; 854 if (max_down < *available_down) 855 *available_down = max_down; 856 } 857 858 if (*available_up < 0) 859 *available_up = 0; 860 if (*available_down < 0) 861 *available_down = 0; 862 863 return 0; 864 } 865 866 static int tb_release_unused_usb3_bandwidth(struct tb *tb, 867 struct tb_port *src_port, 868 struct tb_port *dst_port) 869 { 870 struct tb_tunnel *tunnel; 871 872 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); 873 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0; 874 } 875 876 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port, 877 struct tb_port *dst_port) 878 { 879 int ret, available_up, available_down; 880 struct tb_tunnel *tunnel; 881 882 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); 883 if (!tunnel) 884 return; 885 886 tb_tunnel_dbg(tunnel, "reclaiming unused bandwidth\n"); 887 888 /* 889 * Calculate available bandwidth for the first hop USB3 tunnel. 890 * That determines the whole USB3 bandwidth for this branch. 891 */ 892 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, 893 &available_up, &available_down, false); 894 if (ret) { 895 tb_tunnel_warn(tunnel, "failed to calculate available bandwidth\n"); 896 return; 897 } 898 899 tb_tunnel_dbg(tunnel, "available bandwidth %d/%d Mb/s\n", available_up, 900 available_down); 901 902 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down); 903 } 904 905 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) 906 { 907 struct tb_switch *parent = tb_switch_parent(sw); 908 int ret, available_up, available_down; 909 struct tb_port *up, *down, *port; 910 struct tb_cm *tcm = tb_priv(tb); 911 struct tb_tunnel *tunnel; 912 913 if (!tb_acpi_may_tunnel_usb3()) { 914 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n"); 915 return 0; 916 } 917 918 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP); 919 if (!up) 920 return 0; 921 922 if (!sw->link_usb4) 923 return 0; 924 925 /* 926 * Look up available down port. Since we are chaining it should 927 * be found right above this switch. 928 */ 929 port = tb_switch_downstream_port(sw); 930 down = tb_find_usb3_down(parent, port); 931 if (!down) 932 return 0; 933 934 if (tb_route(parent)) { 935 struct tb_port *parent_up; 936 /* 937 * Check first that the parent switch has its upstream USB3 938 * port enabled. Otherwise the chain is not complete and 939 * there is no point setting up a new tunnel. 940 */ 941 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP); 942 if (!parent_up || !tb_port_is_enabled(parent_up)) 943 return 0; 944 945 /* Make all unused bandwidth available for the new tunnel */ 946 ret = tb_release_unused_usb3_bandwidth(tb, down, up); 947 if (ret) 948 return ret; 949 } 950 951 ret = tb_available_bandwidth(tb, down, up, &available_up, &available_down, 952 false); 953 if (ret) 954 goto err_reclaim; 955 956 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n", 957 available_up, available_down); 958 959 /* 960 * If the available bandwidth is less than 1.5 Gb/s notify 961 * userspace that the connected isochronous device may not work 962 * properly. 963 */ 964 if (available_up < 1500 || available_down < 1500) 965 tb_tunnel_event(tb, TB_TUNNEL_LOW_BANDWIDTH, TB_TUNNEL_USB3, 966 down, up); 967 968 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up, 969 available_down); 970 if (!tunnel) { 971 ret = -ENOMEM; 972 goto err_reclaim; 973 } 974 975 if (tb_tunnel_activate(tunnel)) { 976 tb_port_info(up, 977 "USB3 tunnel activation failed, aborting\n"); 978 ret = -EIO; 979 goto err_free; 980 } 981 982 list_add_tail(&tunnel->list, &tcm->tunnel_list); 983 if (tb_route(parent)) 984 tb_reclaim_usb3_bandwidth(tb, down, up); 985 986 return 0; 987 988 err_free: 989 tb_tunnel_put(tunnel); 990 err_reclaim: 991 if (tb_route(parent)) 992 tb_reclaim_usb3_bandwidth(tb, down, up); 993 994 return ret; 995 } 996 997 static int tb_create_usb3_tunnels(struct tb_switch *sw) 998 { 999 struct tb_port *port; 1000 int ret; 1001 1002 if (!tb_acpi_may_tunnel_usb3()) 1003 return 0; 1004 1005 if (tb_route(sw)) { 1006 ret = tb_tunnel_usb3(sw->tb, sw); 1007 if (ret) 1008 return ret; 1009 } 1010 1011 tb_switch_for_each_port(sw, port) { 1012 if (!tb_port_has_remote(port)) 1013 continue; 1014 ret = tb_create_usb3_tunnels(port->remote->sw); 1015 if (ret) 1016 return ret; 1017 } 1018 1019 return 0; 1020 } 1021 1022 /** 1023 * tb_configure_asym() - Transition links to asymmetric if needed 1024 * @tb: Domain structure 1025 * @src_port: Source adapter to start the transition 1026 * @dst_port: Destination adapter 1027 * @requested_up: Additional bandwidth (Mb/s) required upstream 1028 * @requested_down: Additional bandwidth (Mb/s) required downstream 1029 * 1030 * Transition links between @src_port and @dst_port into asymmetric, with 1031 * three lanes in the direction from @src_port towards @dst_port and one lane 1032 * in the opposite direction, if the bandwidth requirements 1033 * (requested + currently consumed) on that link exceed @asym_threshold. 1034 * 1035 * Must be called with available >= requested over all links. 1036 * 1037 * Return: %0 on success, negative errno otherwise. 1038 */ 1039 static int tb_configure_asym(struct tb *tb, struct tb_port *src_port, 1040 struct tb_port *dst_port, int requested_up, 1041 int requested_down) 1042 { 1043 bool clx = false, clx_disabled = false, downstream; 1044 struct tb_switch *sw; 1045 struct tb_port *up; 1046 int ret = 0; 1047 1048 if (!asym_threshold) 1049 return 0; 1050 1051 downstream = tb_port_path_direction_downstream(src_port, dst_port); 1052 /* Pick up router deepest in the hierarchy */ 1053 if (downstream) 1054 sw = dst_port->sw; 1055 else 1056 sw = src_port->sw; 1057 1058 tb_for_each_upstream_port_on_path(src_port, dst_port, up) { 1059 struct tb_port *down = tb_switch_downstream_port(up->sw); 1060 enum tb_link_width width_up, width_down; 1061 int consumed_up, consumed_down; 1062 1063 ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up, 1064 &consumed_up, &consumed_down); 1065 if (ret) 1066 break; 1067 1068 if (downstream) { 1069 /* 1070 * Downstream so make sure upstream is within the 36G 1071 * (40G - guard band 10%), and the requested is above 1072 * what the threshold is. 1073 */ 1074 if (consumed_up + requested_up >= TB_ASYM_MIN) { 1075 ret = -ENOBUFS; 1076 break; 1077 } 1078 /* Does consumed + requested exceed the threshold */ 1079 if (consumed_down + requested_down < asym_threshold) 1080 continue; 1081 1082 width_up = TB_LINK_WIDTH_ASYM_RX; 1083 width_down = TB_LINK_WIDTH_ASYM_TX; 1084 } else { 1085 /* Upstream, the opposite of above */ 1086 if (consumed_down + requested_down >= TB_ASYM_MIN) { 1087 ret = -ENOBUFS; 1088 break; 1089 } 1090 if (consumed_up + requested_up < asym_threshold) 1091 continue; 1092 1093 width_up = TB_LINK_WIDTH_ASYM_TX; 1094 width_down = TB_LINK_WIDTH_ASYM_RX; 1095 } 1096 1097 if (up->sw->link_width == width_up) 1098 continue; 1099 1100 if (!tb_port_width_supported(up, width_up) || 1101 !tb_port_width_supported(down, width_down)) 1102 continue; 1103 1104 /* 1105 * Disable CL states before doing any transitions. We 1106 * delayed it until now that we know there is a real 1107 * transition taking place. 1108 */ 1109 if (!clx_disabled) { 1110 clx = tb_disable_clx(sw); 1111 clx_disabled = true; 1112 } 1113 1114 tb_sw_dbg(up->sw, "configuring asymmetric link\n"); 1115 1116 /* 1117 * Here requested + consumed > threshold so we need to 1118 * transition the link into asymmetric now. 1119 */ 1120 ret = tb_switch_set_link_width(up->sw, width_up); 1121 if (ret) { 1122 tb_sw_warn(up->sw, "failed to set link width\n"); 1123 break; 1124 } 1125 } 1126 1127 /* Re-enable CL states if they were previosly enabled */ 1128 if (clx) 1129 tb_enable_clx(sw); 1130 1131 return ret; 1132 } 1133 1134 /** 1135 * tb_configure_sym() - Transition links to symmetric if possible 1136 * @tb: Domain structure 1137 * @src_port: Source adapter to start the transition 1138 * @dst_port: Destination adapter 1139 * @keep_asym: Keep asymmetric link if preferred 1140 * 1141 * Goes over each link from @src_port to @dst_port and tries to 1142 * transition the link to symmetric if the currently consumed bandwidth 1143 * allows and link asymmetric preference is ignored (if @keep_asym is %false). 1144 * 1145 * Return: %0 on success, negative errno otherwise. 1146 */ 1147 static int tb_configure_sym(struct tb *tb, struct tb_port *src_port, 1148 struct tb_port *dst_port, bool keep_asym) 1149 { 1150 bool clx = false, clx_disabled = false, downstream; 1151 struct tb_switch *sw; 1152 struct tb_port *up; 1153 int ret = 0; 1154 1155 if (!asym_threshold) 1156 return 0; 1157 1158 downstream = tb_port_path_direction_downstream(src_port, dst_port); 1159 /* Pick up router deepest in the hierarchy */ 1160 if (downstream) 1161 sw = dst_port->sw; 1162 else 1163 sw = src_port->sw; 1164 1165 tb_for_each_upstream_port_on_path(src_port, dst_port, up) { 1166 int consumed_up, consumed_down; 1167 1168 /* Already symmetric */ 1169 if (up->sw->link_width <= TB_LINK_WIDTH_DUAL) 1170 continue; 1171 /* Unplugged, no need to switch */ 1172 if (up->sw->is_unplugged) 1173 continue; 1174 1175 ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up, 1176 &consumed_up, &consumed_down); 1177 if (ret) 1178 break; 1179 1180 if (downstream) { 1181 /* 1182 * Downstream so we want the consumed_down < threshold. 1183 * Upstream traffic should be less than 36G (40G 1184 * guard band 10%) as the link was configured asymmetric 1185 * already. 1186 */ 1187 if (consumed_down >= asym_threshold) 1188 continue; 1189 } else { 1190 if (consumed_up >= asym_threshold) 1191 continue; 1192 } 1193 1194 if (up->sw->link_width == TB_LINK_WIDTH_DUAL) 1195 continue; 1196 1197 /* 1198 * Here consumed < threshold so we can transition the 1199 * link to symmetric. 1200 * 1201 * However, if the router prefers asymmetric link we 1202 * honor that (unless @keep_asym is %false). 1203 */ 1204 if (keep_asym && 1205 up->sw->preferred_link_width > TB_LINK_WIDTH_DUAL) { 1206 tb_sw_dbg(up->sw, "keeping preferred asymmetric link\n"); 1207 continue; 1208 } 1209 1210 /* Disable CL states before doing any transitions */ 1211 if (!clx_disabled) { 1212 clx = tb_disable_clx(sw); 1213 clx_disabled = true; 1214 } 1215 1216 tb_sw_dbg(up->sw, "configuring symmetric link\n"); 1217 1218 ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL); 1219 if (ret) { 1220 tb_sw_warn(up->sw, "failed to set link width\n"); 1221 break; 1222 } 1223 } 1224 1225 /* Re-enable CL states if they were previosly enabled */ 1226 if (clx) 1227 tb_enable_clx(sw); 1228 1229 return ret; 1230 } 1231 1232 static void tb_configure_link(struct tb_port *down, struct tb_port *up, 1233 struct tb_switch *sw) 1234 { 1235 struct tb *tb = sw->tb; 1236 1237 /* Link the routers using both links if available */ 1238 down->remote = up; 1239 up->remote = down; 1240 if (down->dual_link_port && up->dual_link_port) { 1241 down->dual_link_port->remote = up->dual_link_port; 1242 up->dual_link_port->remote = down->dual_link_port; 1243 } 1244 1245 /* 1246 * Enable lane bonding if the link is currently two single lane 1247 * links. 1248 */ 1249 if (sw->link_width < TB_LINK_WIDTH_DUAL) 1250 tb_switch_set_link_width(sw, TB_LINK_WIDTH_DUAL); 1251 1252 /* 1253 * Device router that comes up as symmetric link is 1254 * connected deeper in the hierarchy, we transition the links 1255 * above into symmetric if bandwidth allows. 1256 */ 1257 if (tb_switch_depth(sw) > 1 && 1258 tb_port_get_link_generation(up) >= 4 && 1259 up->sw->link_width == TB_LINK_WIDTH_DUAL) { 1260 struct tb_port *host_port; 1261 1262 host_port = tb_port_at(tb_route(sw), tb->root_switch); 1263 tb_configure_sym(tb, host_port, up, false); 1264 } 1265 1266 /* Set the link configured */ 1267 tb_switch_configure_link(sw); 1268 } 1269 1270 /* 1271 * tb_scan_switch() - scan for and initialize downstream switches 1272 */ 1273 static void tb_scan_switch(struct tb_switch *sw) 1274 { 1275 struct tb_port *port; 1276 1277 pm_runtime_get_sync(&sw->dev); 1278 1279 tb_switch_for_each_port(sw, port) 1280 tb_scan_port(port); 1281 1282 pm_runtime_mark_last_busy(&sw->dev); 1283 pm_runtime_put_autosuspend(&sw->dev); 1284 } 1285 1286 /* 1287 * tb_scan_port() - check for and initialize switches below port 1288 */ 1289 static void tb_scan_port(struct tb_port *port) 1290 { 1291 struct tb_cm *tcm = tb_priv(port->sw->tb); 1292 struct tb_port *upstream_port; 1293 bool discovery = false; 1294 struct tb_switch *sw; 1295 1296 if (tb_is_upstream_port(port)) 1297 return; 1298 1299 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 && 1300 !tb_dp_port_is_enabled(port)) { 1301 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n"); 1302 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, 1303 false); 1304 return; 1305 } 1306 1307 if (port->config.type != TB_TYPE_PORT) 1308 return; 1309 if (port->dual_link_port && port->link_nr) 1310 return; /* 1311 * Downstream switch is reachable through two ports. 1312 * Only scan on the primary port (link_nr == 0). 1313 */ 1314 1315 if (port->usb4) 1316 pm_runtime_get_sync(&port->usb4->dev); 1317 1318 if (tb_wait_for_port(port, false) <= 0) 1319 goto out_rpm_put; 1320 if (port->remote) { 1321 tb_port_dbg(port, "port already has a remote\n"); 1322 goto out_rpm_put; 1323 } 1324 1325 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, 1326 tb_downstream_route(port)); 1327 if (IS_ERR(sw)) { 1328 /* 1329 * Make the downstream retimers available even if there 1330 * is no router connected. 1331 */ 1332 tb_retimer_scan(port, true); 1333 1334 /* 1335 * If there is an error accessing the connected switch 1336 * it may be connected to another domain. Also we allow 1337 * the other domain to be connected to a max depth switch. 1338 */ 1339 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) 1340 tb_scan_xdomain(port); 1341 goto out_rpm_put; 1342 } 1343 1344 if (tb_switch_configure(sw)) { 1345 tb_switch_put(sw); 1346 goto out_rpm_put; 1347 } 1348 1349 /* 1350 * If there was previously another domain connected remove it 1351 * first. 1352 */ 1353 if (port->xdomain) { 1354 tb_xdomain_remove(port->xdomain); 1355 tb_port_unconfigure_xdomain(port); 1356 port->xdomain = NULL; 1357 } 1358 1359 /* 1360 * Do not send uevents until we have discovered all existing 1361 * tunnels and know which switches were authorized already by 1362 * the boot firmware. 1363 */ 1364 if (!tcm->hotplug_active) { 1365 dev_set_uevent_suppress(&sw->dev, true); 1366 discovery = true; 1367 } 1368 1369 /* 1370 * At the moment Thunderbolt 2 and beyond (devices with LC) we 1371 * can support runtime PM. 1372 */ 1373 sw->rpm = sw->generation > 1; 1374 1375 if (tb_switch_add(sw)) { 1376 tb_switch_put(sw); 1377 goto out_rpm_put; 1378 } 1379 1380 upstream_port = tb_upstream_port(sw); 1381 tb_configure_link(port, upstream_port, sw); 1382 1383 /* 1384 * Scan for downstream retimers. We only scan them after the 1385 * router has been enumerated to avoid issues with certain 1386 * Pluggable devices that expect the host to enumerate them 1387 * within certain timeout. 1388 */ 1389 tb_retimer_scan(port, true); 1390 1391 /* 1392 * CL0s and CL1 are enabled and supported together. 1393 * Silently ignore CLx enabling in case CLx is not supported. 1394 */ 1395 if (discovery) 1396 tb_sw_dbg(sw, "discovery, not touching CL states\n"); 1397 else if (tb_enable_clx(sw)) 1398 tb_sw_warn(sw, "failed to enable CL states\n"); 1399 1400 if (tb_enable_tmu(sw)) 1401 tb_sw_warn(sw, "failed to enable TMU\n"); 1402 1403 /* 1404 * Configuration valid needs to be set after the TMU has been 1405 * enabled for the upstream port of the router so we do it here. 1406 */ 1407 tb_switch_configuration_valid(sw); 1408 1409 /* Scan upstream retimers */ 1410 tb_retimer_scan(upstream_port, true); 1411 1412 /* 1413 * Create USB 3.x tunnels only when the switch is plugged to the 1414 * domain. This is because we scan the domain also during discovery 1415 * and want to discover existing USB 3.x tunnels before we create 1416 * any new. 1417 */ 1418 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) 1419 tb_sw_warn(sw, "USB3 tunnel creation failed\n"); 1420 1421 tb_add_dp_resources(sw); 1422 tb_scan_switch(sw); 1423 1424 out_rpm_put: 1425 if (port->usb4) { 1426 pm_runtime_mark_last_busy(&port->usb4->dev); 1427 pm_runtime_put_autosuspend(&port->usb4->dev); 1428 } 1429 } 1430 1431 static void 1432 tb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group *group) 1433 { 1434 struct tb_tunnel *first_tunnel; 1435 struct tb *tb = group->tb; 1436 struct tb_port *in; 1437 int ret; 1438 1439 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n", 1440 group->index); 1441 1442 first_tunnel = NULL; 1443 list_for_each_entry(in, &group->ports, group_list) { 1444 int estimated_bw, estimated_up, estimated_down; 1445 struct tb_tunnel *tunnel; 1446 struct tb_port *out; 1447 1448 if (!usb4_dp_port_bandwidth_mode_enabled(in)) 1449 continue; 1450 1451 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL); 1452 if (WARN_ON(!tunnel)) 1453 break; 1454 1455 if (!first_tunnel) { 1456 /* 1457 * Since USB3 bandwidth is shared by all DP 1458 * tunnels under the host router USB4 port, even 1459 * if they do not begin from the host router, we 1460 * can release USB3 bandwidth just once and not 1461 * for each tunnel separately. 1462 */ 1463 first_tunnel = tunnel; 1464 ret = tb_release_unused_usb3_bandwidth(tb, 1465 first_tunnel->src_port, first_tunnel->dst_port); 1466 if (ret) { 1467 tb_tunnel_warn(tunnel, 1468 "failed to release unused bandwidth\n"); 1469 break; 1470 } 1471 } 1472 1473 out = tunnel->dst_port; 1474 ret = tb_available_bandwidth(tb, in, out, &estimated_up, 1475 &estimated_down, true); 1476 if (ret) { 1477 tb_tunnel_warn(tunnel, 1478 "failed to re-calculate estimated bandwidth\n"); 1479 break; 1480 } 1481 1482 /* 1483 * Estimated bandwidth includes: 1484 * - already allocated bandwidth for the DP tunnel 1485 * - available bandwidth along the path 1486 * - bandwidth allocated for USB 3.x but not used. 1487 */ 1488 if (tb_tunnel_direction_downstream(tunnel)) 1489 estimated_bw = estimated_down; 1490 else 1491 estimated_bw = estimated_up; 1492 1493 /* 1494 * If there is reserved bandwidth for the group that is 1495 * not yet released we report that too. 1496 */ 1497 tb_tunnel_dbg(tunnel, 1498 "re-calculated estimated bandwidth %u (+ %u reserved) = %u Mb/s\n", 1499 estimated_bw, group->reserved, 1500 estimated_bw + group->reserved); 1501 1502 if (usb4_dp_port_set_estimated_bandwidth(in, 1503 estimated_bw + group->reserved)) 1504 tb_tunnel_warn(tunnel, 1505 "failed to update estimated bandwidth\n"); 1506 } 1507 1508 if (first_tunnel) 1509 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port, 1510 first_tunnel->dst_port); 1511 1512 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index); 1513 } 1514 1515 static void tb_recalc_estimated_bandwidth(struct tb *tb) 1516 { 1517 struct tb_cm *tcm = tb_priv(tb); 1518 int i; 1519 1520 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n"); 1521 1522 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { 1523 struct tb_bandwidth_group *group = &tcm->groups[i]; 1524 1525 if (!list_empty(&group->ports)) 1526 tb_recalc_estimated_bandwidth_for_group(group); 1527 } 1528 1529 tb_dbg(tb, "bandwidth re-calculation done\n"); 1530 } 1531 1532 static bool __release_group_bandwidth(struct tb_bandwidth_group *group) 1533 { 1534 if (group->reserved) { 1535 tb_dbg(group->tb, "group %d released total %d Mb/s\n", group->index, 1536 group->reserved); 1537 group->reserved = 0; 1538 return true; 1539 } 1540 return false; 1541 } 1542 1543 static void __configure_group_sym(struct tb_bandwidth_group *group) 1544 { 1545 struct tb_tunnel *tunnel; 1546 struct tb_port *in; 1547 1548 if (list_empty(&group->ports)) 1549 return; 1550 1551 /* 1552 * All the tunnels in the group go through the same USB4 links 1553 * so we find the first one here and pass the IN and OUT 1554 * adapters to tb_configure_sym() which now transitions the 1555 * links back to symmetric if bandwidth requirement < asym_threshold. 1556 * 1557 * We do this here to avoid unnecessary transitions (for example 1558 * if the graphics released bandwidth for other tunnel in the 1559 * same group). 1560 */ 1561 in = list_first_entry(&group->ports, struct tb_port, group_list); 1562 tunnel = tb_find_tunnel(group->tb, TB_TUNNEL_DP, in, NULL); 1563 if (tunnel) 1564 tb_configure_sym(group->tb, in, tunnel->dst_port, true); 1565 } 1566 1567 static void tb_bandwidth_group_release_work(struct work_struct *work) 1568 { 1569 struct tb_bandwidth_group *group = 1570 container_of(work, typeof(*group), release_work.work); 1571 struct tb *tb = group->tb; 1572 1573 mutex_lock(&tb->lock); 1574 if (__release_group_bandwidth(group)) 1575 tb_recalc_estimated_bandwidth(tb); 1576 __configure_group_sym(group); 1577 mutex_unlock(&tb->lock); 1578 } 1579 1580 static void tb_init_bandwidth_groups(struct tb_cm *tcm) 1581 { 1582 int i; 1583 1584 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { 1585 struct tb_bandwidth_group *group = &tcm->groups[i]; 1586 1587 group->tb = tcm_to_tb(tcm); 1588 group->index = i + 1; 1589 INIT_LIST_HEAD(&group->ports); 1590 INIT_DELAYED_WORK(&group->release_work, 1591 tb_bandwidth_group_release_work); 1592 } 1593 } 1594 1595 static void tb_bandwidth_group_attach_port(struct tb_bandwidth_group *group, 1596 struct tb_port *in) 1597 { 1598 if (!group || WARN_ON(in->group)) 1599 return; 1600 1601 in->group = group; 1602 list_add_tail(&in->group_list, &group->ports); 1603 1604 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index); 1605 } 1606 1607 static struct tb_bandwidth_group *tb_find_free_bandwidth_group(struct tb_cm *tcm) 1608 { 1609 int i; 1610 1611 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { 1612 struct tb_bandwidth_group *group = &tcm->groups[i]; 1613 1614 if (list_empty(&group->ports)) 1615 return group; 1616 } 1617 1618 return NULL; 1619 } 1620 1621 static struct tb_bandwidth_group * 1622 tb_attach_bandwidth_group(struct tb_cm *tcm, struct tb_port *in, 1623 struct tb_port *out) 1624 { 1625 struct tb_bandwidth_group *group; 1626 struct tb_tunnel *tunnel; 1627 1628 /* 1629 * Find all DP tunnels that go through all the same USB4 links 1630 * as this one. Because we always setup tunnels the same way we 1631 * can just check for the routers at both ends of the tunnels 1632 * and if they are the same we have a match. 1633 */ 1634 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 1635 if (!tb_tunnel_is_dp(tunnel)) 1636 continue; 1637 1638 if (tunnel->src_port->sw == in->sw && 1639 tunnel->dst_port->sw == out->sw) { 1640 group = tunnel->src_port->group; 1641 if (group) { 1642 tb_bandwidth_group_attach_port(group, in); 1643 return group; 1644 } 1645 } 1646 } 1647 1648 /* Pick up next available group then */ 1649 group = tb_find_free_bandwidth_group(tcm); 1650 if (group) 1651 tb_bandwidth_group_attach_port(group, in); 1652 else 1653 tb_port_warn(in, "no available bandwidth groups\n"); 1654 1655 return group; 1656 } 1657 1658 static void tb_discover_bandwidth_group(struct tb_cm *tcm, struct tb_port *in, 1659 struct tb_port *out) 1660 { 1661 if (usb4_dp_port_bandwidth_mode_enabled(in)) { 1662 int index, i; 1663 1664 index = usb4_dp_port_group_id(in); 1665 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { 1666 if (tcm->groups[i].index == index) { 1667 tb_bandwidth_group_attach_port(&tcm->groups[i], in); 1668 return; 1669 } 1670 } 1671 } 1672 1673 tb_attach_bandwidth_group(tcm, in, out); 1674 } 1675 1676 static void tb_detach_bandwidth_group(struct tb_port *in) 1677 { 1678 struct tb_bandwidth_group *group = in->group; 1679 1680 if (group) { 1681 in->group = NULL; 1682 list_del_init(&in->group_list); 1683 1684 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index); 1685 1686 /* No more tunnels so release the reserved bandwidth if any */ 1687 if (list_empty(&group->ports)) { 1688 cancel_delayed_work(&group->release_work); 1689 __release_group_bandwidth(group); 1690 } 1691 } 1692 } 1693 1694 static void tb_discover_tunnels(struct tb *tb) 1695 { 1696 struct tb_cm *tcm = tb_priv(tb); 1697 struct tb_tunnel *tunnel; 1698 1699 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); 1700 1701 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 1702 if (tb_tunnel_is_pci(tunnel)) { 1703 struct tb_switch *parent = tunnel->dst_port->sw; 1704 1705 while (parent != tunnel->src_port->sw) { 1706 parent->boot = true; 1707 parent = tb_switch_parent(parent); 1708 } 1709 } else if (tb_tunnel_is_dp(tunnel)) { 1710 struct tb_port *in = tunnel->src_port; 1711 struct tb_port *out = tunnel->dst_port; 1712 1713 /* Keep the domain from powering down */ 1714 pm_runtime_get_sync(&in->sw->dev); 1715 pm_runtime_get_sync(&out->sw->dev); 1716 1717 tb_discover_bandwidth_group(tcm, in, out); 1718 } 1719 } 1720 } 1721 1722 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel) 1723 { 1724 struct tb_port *src_port, *dst_port; 1725 struct tb *tb; 1726 1727 if (!tunnel) 1728 return; 1729 1730 tb_tunnel_deactivate(tunnel); 1731 list_del(&tunnel->list); 1732 1733 tb = tunnel->tb; 1734 src_port = tunnel->src_port; 1735 dst_port = tunnel->dst_port; 1736 1737 switch (tunnel->type) { 1738 case TB_TUNNEL_DP: 1739 tb_detach_bandwidth_group(src_port); 1740 /* 1741 * In case of DP tunnel make sure the DP IN resource is 1742 * deallocated properly. 1743 */ 1744 tb_switch_dealloc_dp_resource(src_port->sw, src_port); 1745 /* 1746 * If bandwidth on a link is < asym_threshold 1747 * transition the link to symmetric. 1748 */ 1749 tb_configure_sym(tb, src_port, dst_port, true); 1750 /* Now we can allow the domain to runtime suspend again */ 1751 pm_runtime_mark_last_busy(&dst_port->sw->dev); 1752 pm_runtime_put_autosuspend(&dst_port->sw->dev); 1753 pm_runtime_mark_last_busy(&src_port->sw->dev); 1754 pm_runtime_put_autosuspend(&src_port->sw->dev); 1755 fallthrough; 1756 1757 case TB_TUNNEL_USB3: 1758 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port); 1759 break; 1760 1761 default: 1762 /* 1763 * PCIe and DMA tunnels do not consume guaranteed 1764 * bandwidth. 1765 */ 1766 break; 1767 } 1768 1769 tb_tunnel_put(tunnel); 1770 } 1771 1772 /* 1773 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away 1774 */ 1775 static void tb_free_invalid_tunnels(struct tb *tb) 1776 { 1777 struct tb_cm *tcm = tb_priv(tb); 1778 struct tb_tunnel *tunnel; 1779 struct tb_tunnel *n; 1780 1781 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { 1782 if (tb_tunnel_is_invalid(tunnel)) 1783 tb_deactivate_and_free_tunnel(tunnel); 1784 } 1785 } 1786 1787 /* 1788 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches 1789 */ 1790 static void tb_free_unplugged_children(struct tb_switch *sw) 1791 { 1792 struct tb_port *port; 1793 1794 tb_switch_for_each_port(sw, port) { 1795 if (!tb_port_has_remote(port)) 1796 continue; 1797 1798 if (port->remote->sw->is_unplugged) { 1799 tb_retimer_remove_all(port); 1800 tb_remove_dp_resources(port->remote->sw); 1801 tb_switch_unconfigure_link(port->remote->sw); 1802 tb_switch_set_link_width(port->remote->sw, 1803 TB_LINK_WIDTH_SINGLE); 1804 tb_switch_remove(port->remote->sw); 1805 port->remote = NULL; 1806 if (port->dual_link_port) 1807 port->dual_link_port->remote = NULL; 1808 } else { 1809 tb_free_unplugged_children(port->remote->sw); 1810 } 1811 } 1812 } 1813 1814 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, 1815 const struct tb_port *port) 1816 { 1817 struct tb_port *down = NULL; 1818 1819 /* 1820 * To keep plugging devices consistently in the same PCIe 1821 * hierarchy, do mapping here for switch downstream PCIe ports. 1822 */ 1823 if (tb_switch_is_usb4(sw)) { 1824 down = usb4_switch_map_pcie_down(sw, port); 1825 } else if (!tb_route(sw)) { 1826 int phy_port = tb_phy_port_from_link(port->port); 1827 int index; 1828 1829 /* 1830 * Hard-coded Thunderbolt port to PCIe down port mapping 1831 * per controller. 1832 */ 1833 if (tb_switch_is_cactus_ridge(sw) || 1834 tb_switch_is_alpine_ridge(sw)) 1835 index = !phy_port ? 6 : 7; 1836 else if (tb_switch_is_falcon_ridge(sw)) 1837 index = !phy_port ? 6 : 8; 1838 else if (tb_switch_is_titan_ridge(sw)) 1839 index = !phy_port ? 8 : 9; 1840 else 1841 goto out; 1842 1843 /* Validate the hard-coding */ 1844 if (WARN_ON(index > sw->config.max_port_number)) 1845 goto out; 1846 1847 down = &sw->ports[index]; 1848 } 1849 1850 if (down) { 1851 if (WARN_ON(!tb_port_is_pcie_down(down))) 1852 goto out; 1853 if (tb_pci_port_is_enabled(down)) 1854 goto out; 1855 1856 return down; 1857 } 1858 1859 out: 1860 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); 1861 } 1862 1863 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in) 1864 { 1865 struct tb_port *host_port, *port; 1866 struct tb_cm *tcm = tb_priv(tb); 1867 1868 host_port = tb_route(in->sw) ? 1869 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; 1870 1871 list_for_each_entry(port, &tcm->dp_resources, list) { 1872 if (!tb_port_is_dpout(port)) 1873 continue; 1874 1875 if (tb_port_is_enabled(port)) { 1876 tb_port_dbg(port, "DP OUT in use\n"); 1877 continue; 1878 } 1879 1880 /* Needs to be on different routers */ 1881 if (in->sw == port->sw) { 1882 tb_port_dbg(port, "skipping DP OUT on same router\n"); 1883 continue; 1884 } 1885 1886 tb_port_dbg(port, "DP OUT available\n"); 1887 1888 /* 1889 * Keep the DP tunnel under the topology starting from 1890 * the same host router downstream port. 1891 */ 1892 if (host_port && tb_route(port->sw)) { 1893 struct tb_port *p; 1894 1895 p = tb_port_at(tb_route(port->sw), tb->root_switch); 1896 if (p != host_port) 1897 continue; 1898 } 1899 1900 return port; 1901 } 1902 1903 return NULL; 1904 } 1905 1906 static void tb_dp_tunnel_active(struct tb_tunnel *tunnel, void *data) 1907 { 1908 struct tb_port *in = tunnel->src_port; 1909 struct tb_port *out = tunnel->dst_port; 1910 struct tb *tb = data; 1911 1912 mutex_lock(&tb->lock); 1913 if (tb_tunnel_is_active(tunnel)) { 1914 int consumed_up, consumed_down, ret; 1915 1916 tb_tunnel_dbg(tunnel, "DPRX capabilities read completed\n"); 1917 1918 /* If fail reading tunnel's consumed bandwidth, tear it down */ 1919 ret = tb_tunnel_consumed_bandwidth(tunnel, &consumed_up, 1920 &consumed_down); 1921 if (ret) { 1922 tb_tunnel_warn(tunnel, 1923 "failed to read consumed bandwidth, tearing down\n"); 1924 tb_deactivate_and_free_tunnel(tunnel); 1925 } else { 1926 tb_reclaim_usb3_bandwidth(tb, in, out); 1927 /* 1928 * Transition the links to asymmetric if the 1929 * consumption exceeds the threshold. 1930 */ 1931 tb_configure_asym(tb, in, out, consumed_up, 1932 consumed_down); 1933 /* 1934 * Update the domain with the new bandwidth 1935 * estimation. 1936 */ 1937 tb_recalc_estimated_bandwidth(tb); 1938 /* 1939 * In case DP tunnel exists, change host 1940 * router's 1st children TMU mode to HiFi for 1941 * CL0s to work. 1942 */ 1943 tb_increase_tmu_accuracy(tunnel); 1944 } 1945 } else { 1946 struct tb_port *in = tunnel->src_port; 1947 1948 /* 1949 * This tunnel failed to establish. This means DPRX 1950 * negotiation most likely did not complete which 1951 * happens either because there is no graphics driver 1952 * loaded or not all DP cables where connected to the 1953 * discrete router. 1954 * 1955 * In both cases we remove the DP IN adapter from the 1956 * available resources as it is not usable. This will 1957 * also tear down the tunnel and try to re-use the 1958 * released DP OUT. 1959 * 1960 * It will be added back only if there is hotplug for 1961 * the DP IN again. 1962 */ 1963 tb_tunnel_warn(tunnel, "not active, tearing down\n"); 1964 tb_dp_resource_unavailable(tb, in, "DPRX negotiation failed"); 1965 } 1966 mutex_unlock(&tb->lock); 1967 1968 tb_domain_put(tb); 1969 } 1970 1971 static void tb_tunnel_one_dp(struct tb *tb, struct tb_port *in, 1972 struct tb_port *out) 1973 { 1974 int available_up, available_down, ret, link_nr; 1975 struct tb_cm *tcm = tb_priv(tb); 1976 struct tb_tunnel *tunnel; 1977 1978 /* 1979 * This is only applicable to links that are not bonded (so 1980 * when Thunderbolt 1 hardware is involved somewhere in the 1981 * topology). For these try to share the DP bandwidth between 1982 * the two lanes. 1983 */ 1984 link_nr = 1; 1985 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { 1986 if (tb_tunnel_is_dp(tunnel)) { 1987 link_nr = 0; 1988 break; 1989 } 1990 } 1991 1992 /* 1993 * DP stream needs the domain to be active so runtime resume 1994 * both ends of the tunnel. 1995 * 1996 * This should bring the routers in the middle active as well 1997 * and keeps the domain from runtime suspending while the DP 1998 * tunnel is active. 1999 */ 2000 pm_runtime_get_sync(&in->sw->dev); 2001 pm_runtime_get_sync(&out->sw->dev); 2002 2003 if (tb_switch_alloc_dp_resource(in->sw, in)) { 2004 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n"); 2005 goto err_rpm_put; 2006 } 2007 2008 if (!tb_attach_bandwidth_group(tcm, in, out)) 2009 goto err_dealloc_dp; 2010 2011 /* Make all unused USB3 bandwidth available for the new DP tunnel */ 2012 ret = tb_release_unused_usb3_bandwidth(tb, in, out); 2013 if (ret) { 2014 tb_warn(tb, "failed to release unused bandwidth\n"); 2015 goto err_detach_group; 2016 } 2017 2018 ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down, 2019 true); 2020 if (ret) { 2021 tb_tunnel_event(tb, TB_TUNNEL_NO_BANDWIDTH, TB_TUNNEL_DP, in, out); 2022 goto err_reclaim_usb; 2023 } 2024 2025 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n", 2026 available_up, available_down); 2027 2028 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up, 2029 available_down, tb_dp_tunnel_active, 2030 tb_domain_get(tb)); 2031 if (!tunnel) { 2032 tb_port_dbg(out, "could not allocate DP tunnel\n"); 2033 goto err_reclaim_usb; 2034 } 2035 2036 list_add_tail(&tunnel->list, &tcm->tunnel_list); 2037 2038 ret = tb_tunnel_activate(tunnel); 2039 if (ret && ret != -EINPROGRESS) { 2040 tb_port_info(out, "DP tunnel activation failed, aborting\n"); 2041 list_del(&tunnel->list); 2042 goto err_free; 2043 } 2044 2045 return; 2046 2047 err_free: 2048 tb_tunnel_put(tunnel); 2049 err_reclaim_usb: 2050 tb_reclaim_usb3_bandwidth(tb, in, out); 2051 tb_domain_put(tb); 2052 err_detach_group: 2053 tb_detach_bandwidth_group(in); 2054 err_dealloc_dp: 2055 tb_switch_dealloc_dp_resource(in->sw, in); 2056 err_rpm_put: 2057 pm_runtime_mark_last_busy(&out->sw->dev); 2058 pm_runtime_put_autosuspend(&out->sw->dev); 2059 pm_runtime_mark_last_busy(&in->sw->dev); 2060 pm_runtime_put_autosuspend(&in->sw->dev); 2061 } 2062 2063 static void tb_tunnel_dp(struct tb *tb) 2064 { 2065 struct tb_cm *tcm = tb_priv(tb); 2066 struct tb_port *port, *in, *out; 2067 2068 if (!tb_acpi_may_tunnel_dp()) { 2069 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n"); 2070 return; 2071 } 2072 2073 /* 2074 * Find pair of inactive DP IN and DP OUT adapters and then 2075 * establish a DP tunnel between them. 2076 */ 2077 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); 2078 2079 in = NULL; 2080 out = NULL; 2081 list_for_each_entry(port, &tcm->dp_resources, list) { 2082 if (!tb_port_is_dpin(port)) 2083 continue; 2084 2085 if (tb_port_is_enabled(port)) { 2086 tb_port_dbg(port, "DP IN in use\n"); 2087 continue; 2088 } 2089 2090 in = port; 2091 tb_port_dbg(in, "DP IN available\n"); 2092 2093 out = tb_find_dp_out(tb, port); 2094 if (out) 2095 tb_tunnel_one_dp(tb, in, out); 2096 else 2097 tb_port_dbg(in, "no suitable DP OUT adapter available, not tunneling\n"); 2098 } 2099 2100 if (!in) 2101 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n"); 2102 } 2103 2104 static void tb_enter_redrive(struct tb_port *port) 2105 { 2106 struct tb_switch *sw = port->sw; 2107 2108 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) 2109 return; 2110 2111 /* 2112 * If we get hot-unplug for the DP IN port of the host router 2113 * and the DP resource is not available anymore it means there 2114 * is a monitor connected directly to the Type-C port and we are 2115 * in "redrive" mode. For this to work we cannot enter RTD3 so 2116 * we bump up the runtime PM reference count here. 2117 */ 2118 if (!tb_port_is_dpin(port)) 2119 return; 2120 if (tb_route(sw)) 2121 return; 2122 if (!tb_switch_query_dp_resource(sw, port)) { 2123 port->redrive = true; 2124 pm_runtime_get(&sw->dev); 2125 tb_port_dbg(port, "enter redrive mode, keeping powered\n"); 2126 } 2127 } 2128 2129 static void tb_exit_redrive(struct tb_port *port) 2130 { 2131 struct tb_switch *sw = port->sw; 2132 2133 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) 2134 return; 2135 2136 if (!tb_port_is_dpin(port)) 2137 return; 2138 if (tb_route(sw)) 2139 return; 2140 if (port->redrive && tb_switch_query_dp_resource(sw, port)) { 2141 port->redrive = false; 2142 pm_runtime_put(&sw->dev); 2143 tb_port_dbg(port, "exit redrive mode\n"); 2144 } 2145 } 2146 2147 static void tb_switch_enter_redrive(struct tb_switch *sw) 2148 { 2149 struct tb_port *port; 2150 2151 tb_switch_for_each_port(sw, port) 2152 tb_enter_redrive(port); 2153 } 2154 2155 /* 2156 * Called during system and runtime suspend to forcefully exit redrive 2157 * mode without querying whether the resource is available. 2158 */ 2159 static void tb_switch_exit_redrive(struct tb_switch *sw) 2160 { 2161 struct tb_port *port; 2162 2163 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) 2164 return; 2165 2166 tb_switch_for_each_port(sw, port) { 2167 if (!tb_port_is_dpin(port)) 2168 continue; 2169 2170 if (port->redrive) { 2171 port->redrive = false; 2172 pm_runtime_put(&sw->dev); 2173 tb_port_dbg(port, "exit redrive mode\n"); 2174 } 2175 } 2176 } 2177 2178 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port, 2179 const char *reason) 2180 { 2181 struct tb_port *in, *out; 2182 struct tb_tunnel *tunnel; 2183 2184 if (tb_port_is_dpin(port)) { 2185 tb_port_dbg(port, "DP IN resource unavailable: %s\n", reason); 2186 in = port; 2187 out = NULL; 2188 } else { 2189 tb_port_dbg(port, "DP OUT resource unavailable: %s\n", reason); 2190 in = NULL; 2191 out = port; 2192 } 2193 2194 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out); 2195 if (tunnel) 2196 tb_deactivate_and_free_tunnel(tunnel); 2197 else 2198 tb_enter_redrive(port); 2199 list_del_init(&port->list); 2200 2201 /* 2202 * See if there is another DP OUT port that can be used for 2203 * to create another tunnel. 2204 */ 2205 tb_recalc_estimated_bandwidth(tb); 2206 tb_tunnel_dp(tb); 2207 } 2208 2209 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port) 2210 { 2211 struct tb_cm *tcm = tb_priv(tb); 2212 struct tb_port *p; 2213 2214 if (tb_port_is_enabled(port)) 2215 return; 2216 2217 list_for_each_entry(p, &tcm->dp_resources, list) { 2218 if (p == port) 2219 return; 2220 } 2221 2222 tb_port_dbg(port, "DP %s resource available after hotplug\n", 2223 tb_port_is_dpin(port) ? "IN" : "OUT"); 2224 list_add_tail(&port->list, &tcm->dp_resources); 2225 tb_exit_redrive(port); 2226 2227 /* Look for suitable DP IN <-> DP OUT pairs now */ 2228 tb_tunnel_dp(tb); 2229 } 2230 2231 static void tb_disconnect_and_release_dp(struct tb *tb) 2232 { 2233 struct tb_cm *tcm = tb_priv(tb); 2234 struct tb_tunnel *tunnel, *n; 2235 2236 /* 2237 * Tear down all DP tunnels and release their resources. They 2238 * will be re-established after resume based on plug events. 2239 */ 2240 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { 2241 if (tb_tunnel_is_dp(tunnel)) 2242 tb_deactivate_and_free_tunnel(tunnel); 2243 } 2244 2245 while (!list_empty(&tcm->dp_resources)) { 2246 struct tb_port *port; 2247 2248 port = list_first_entry(&tcm->dp_resources, 2249 struct tb_port, list); 2250 list_del_init(&port->list); 2251 } 2252 } 2253 2254 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw) 2255 { 2256 struct tb_tunnel *tunnel; 2257 struct tb_port *up; 2258 2259 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); 2260 if (WARN_ON(!up)) 2261 return -ENODEV; 2262 2263 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up); 2264 if (WARN_ON(!tunnel)) 2265 return -ENODEV; 2266 2267 tb_switch_xhci_disconnect(sw); 2268 2269 tb_tunnel_deactivate(tunnel); 2270 list_del(&tunnel->list); 2271 tb_tunnel_put(tunnel); 2272 return 0; 2273 } 2274 2275 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) 2276 { 2277 struct tb_port *up, *down, *port; 2278 struct tb_cm *tcm = tb_priv(tb); 2279 struct tb_tunnel *tunnel; 2280 2281 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); 2282 if (!up) 2283 return 0; 2284 2285 /* 2286 * Look up available down port. Since we are chaining it should 2287 * be found right above this switch. 2288 */ 2289 port = tb_switch_downstream_port(sw); 2290 down = tb_find_pcie_down(tb_switch_parent(sw), port); 2291 if (!down) 2292 return 0; 2293 2294 tunnel = tb_tunnel_alloc_pci(tb, up, down); 2295 if (!tunnel) 2296 return -ENOMEM; 2297 2298 if (tb_tunnel_activate(tunnel)) { 2299 tb_port_info(up, 2300 "PCIe tunnel activation failed, aborting\n"); 2301 tb_tunnel_put(tunnel); 2302 return -EIO; 2303 } 2304 2305 /* 2306 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it 2307 * here. 2308 */ 2309 if (tb_switch_pcie_l1_enable(sw)) 2310 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n"); 2311 2312 if (tb_switch_xhci_connect(sw)) 2313 tb_sw_warn(sw, "failed to connect xHCI\n"); 2314 2315 list_add_tail(&tunnel->list, &tcm->tunnel_list); 2316 return 0; 2317 } 2318 2319 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, 2320 int transmit_path, int transmit_ring, 2321 int receive_path, int receive_ring) 2322 { 2323 struct tb_cm *tcm = tb_priv(tb); 2324 struct tb_port *nhi_port, *dst_port; 2325 struct tb_tunnel *tunnel; 2326 struct tb_switch *sw; 2327 int ret; 2328 2329 sw = tb_to_switch(xd->dev.parent); 2330 dst_port = tb_port_at(xd->route, sw); 2331 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); 2332 2333 mutex_lock(&tb->lock); 2334 2335 /* 2336 * When tunneling DMA paths the link should not enter CL states 2337 * so disable them now. 2338 */ 2339 tb_disable_clx(sw); 2340 2341 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path, 2342 transmit_ring, receive_path, receive_ring); 2343 if (!tunnel) { 2344 ret = -ENOMEM; 2345 goto err_clx; 2346 } 2347 2348 if (tb_tunnel_activate(tunnel)) { 2349 tb_port_info(nhi_port, 2350 "DMA tunnel activation failed, aborting\n"); 2351 ret = -EIO; 2352 goto err_free; 2353 } 2354 2355 list_add_tail(&tunnel->list, &tcm->tunnel_list); 2356 mutex_unlock(&tb->lock); 2357 return 0; 2358 2359 err_free: 2360 tb_tunnel_put(tunnel); 2361 err_clx: 2362 tb_enable_clx(sw); 2363 mutex_unlock(&tb->lock); 2364 2365 return ret; 2366 } 2367 2368 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, 2369 int transmit_path, int transmit_ring, 2370 int receive_path, int receive_ring) 2371 { 2372 struct tb_cm *tcm = tb_priv(tb); 2373 struct tb_port *nhi_port, *dst_port; 2374 struct tb_tunnel *tunnel, *n; 2375 struct tb_switch *sw; 2376 2377 sw = tb_to_switch(xd->dev.parent); 2378 dst_port = tb_port_at(xd->route, sw); 2379 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); 2380 2381 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { 2382 if (!tb_tunnel_is_dma(tunnel)) 2383 continue; 2384 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) 2385 continue; 2386 2387 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring, 2388 receive_path, receive_ring)) 2389 tb_deactivate_and_free_tunnel(tunnel); 2390 } 2391 2392 /* 2393 * Try to re-enable CL states now, it is OK if this fails 2394 * because we may still have another DMA tunnel active through 2395 * the same host router USB4 downstream port. 2396 */ 2397 tb_enable_clx(sw); 2398 } 2399 2400 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, 2401 int transmit_path, int transmit_ring, 2402 int receive_path, int receive_ring) 2403 { 2404 if (!xd->is_unplugged) { 2405 mutex_lock(&tb->lock); 2406 __tb_disconnect_xdomain_paths(tb, xd, transmit_path, 2407 transmit_ring, receive_path, 2408 receive_ring); 2409 mutex_unlock(&tb->lock); 2410 } 2411 return 0; 2412 } 2413 2414 /* hotplug handling */ 2415 2416 /* 2417 * tb_handle_hotplug() - handle hotplug event 2418 * 2419 * Executes on tb->wq. 2420 */ 2421 static void tb_handle_hotplug(struct work_struct *work) 2422 { 2423 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work); 2424 struct tb *tb = ev->tb; 2425 struct tb_cm *tcm = tb_priv(tb); 2426 struct tb_switch *sw; 2427 struct tb_port *port; 2428 2429 /* Bring the domain back from sleep if it was suspended */ 2430 pm_runtime_get_sync(&tb->dev); 2431 2432 mutex_lock(&tb->lock); 2433 if (!tcm->hotplug_active) 2434 goto out; /* during init, suspend or shutdown */ 2435 2436 sw = tb_switch_find_by_route(tb, ev->route); 2437 if (!sw) { 2438 tb_warn(tb, 2439 "hotplug event from non existent switch %llx:%x (unplug: %d)\n", 2440 ev->route, ev->port, ev->unplug); 2441 goto out; 2442 } 2443 if (ev->port > sw->config.max_port_number) { 2444 tb_warn(tb, 2445 "hotplug event from non existent port %llx:%x (unplug: %d)\n", 2446 ev->route, ev->port, ev->unplug); 2447 goto put_sw; 2448 } 2449 port = &sw->ports[ev->port]; 2450 if (tb_is_upstream_port(port)) { 2451 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n", 2452 ev->route, ev->port, ev->unplug); 2453 goto put_sw; 2454 } 2455 2456 pm_runtime_get_sync(&sw->dev); 2457 2458 if (ev->unplug) { 2459 tb_retimer_remove_all(port); 2460 2461 if (tb_port_has_remote(port)) { 2462 tb_port_dbg(port, "switch unplugged\n"); 2463 tb_sw_set_unplugged(port->remote->sw); 2464 tb_free_invalid_tunnels(tb); 2465 tb_remove_dp_resources(port->remote->sw); 2466 tb_switch_tmu_disable(port->remote->sw); 2467 tb_switch_unconfigure_link(port->remote->sw); 2468 tb_switch_set_link_width(port->remote->sw, 2469 TB_LINK_WIDTH_SINGLE); 2470 tb_switch_remove(port->remote->sw); 2471 port->remote = NULL; 2472 if (port->dual_link_port) 2473 port->dual_link_port->remote = NULL; 2474 /* Maybe we can create another DP tunnel */ 2475 tb_recalc_estimated_bandwidth(tb); 2476 tb_tunnel_dp(tb); 2477 } else if (port->xdomain) { 2478 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); 2479 2480 tb_port_dbg(port, "xdomain unplugged\n"); 2481 /* 2482 * Service drivers are unbound during 2483 * tb_xdomain_remove() so setting XDomain as 2484 * unplugged here prevents deadlock if they call 2485 * tb_xdomain_disable_paths(). We will tear down 2486 * all the tunnels below. 2487 */ 2488 xd->is_unplugged = true; 2489 tb_xdomain_remove(xd); 2490 port->xdomain = NULL; 2491 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); 2492 tb_xdomain_put(xd); 2493 tb_port_unconfigure_xdomain(port); 2494 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) { 2495 tb_dp_resource_unavailable(tb, port, "adapter unplug"); 2496 } else if (!port->port) { 2497 tb_sw_dbg(sw, "xHCI disconnect request\n"); 2498 tb_switch_xhci_disconnect(sw); 2499 } else { 2500 tb_port_dbg(port, 2501 "got unplug event for disconnected port, ignoring\n"); 2502 } 2503 } else if (port->remote) { 2504 tb_port_dbg(port, "got plug event for connected port, ignoring\n"); 2505 } else if (!port->port && sw->authorized) { 2506 tb_sw_dbg(sw, "xHCI connect request\n"); 2507 tb_switch_xhci_connect(sw); 2508 } else { 2509 if (tb_port_is_null(port)) { 2510 tb_port_dbg(port, "hotplug: scanning\n"); 2511 tb_scan_port(port); 2512 if (!port->remote) 2513 tb_port_dbg(port, "hotplug: no switch found\n"); 2514 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) { 2515 tb_dp_resource_available(tb, port); 2516 } 2517 } 2518 2519 pm_runtime_mark_last_busy(&sw->dev); 2520 pm_runtime_put_autosuspend(&sw->dev); 2521 2522 put_sw: 2523 tb_switch_put(sw); 2524 out: 2525 mutex_unlock(&tb->lock); 2526 2527 tb_domain_unregister_unplugged_xdomains(tb); 2528 2529 pm_runtime_mark_last_busy(&tb->dev); 2530 pm_runtime_put_autosuspend(&tb->dev); 2531 2532 /* Undo the refcount increased in tb_queue_hotplug() */ 2533 tb_domain_put(tb); 2534 2535 kfree(ev); 2536 } 2537 2538 static int tb_alloc_dp_bandwidth(struct tb_tunnel *tunnel, int *requested_up, 2539 int *requested_down) 2540 { 2541 int allocated_up, allocated_down, available_up, available_down, ret; 2542 int requested_up_corrected, requested_down_corrected, granularity; 2543 int max_up, max_down, max_up_rounded, max_down_rounded; 2544 struct tb_bandwidth_group *group; 2545 struct tb *tb = tunnel->tb; 2546 struct tb_port *in, *out; 2547 bool downstream; 2548 2549 ret = tb_tunnel_allocated_bandwidth(tunnel, &allocated_up, &allocated_down); 2550 if (ret) 2551 return ret; 2552 2553 in = tunnel->src_port; 2554 out = tunnel->dst_port; 2555 2556 tb_tunnel_dbg(tunnel, "bandwidth allocated currently %d/%d Mb/s\n", 2557 allocated_up, allocated_down); 2558 2559 /* 2560 * If we get rounded up request from graphics side, say HBR2 x 4 2561 * that is 17500 instead of 17280 (this is because of the 2562 * granularity), we allow it too. Here the graphics has already 2563 * negotiated with the DPRX the maximum possible rates (which is 2564 * 17280 in this case). 2565 * 2566 * Since the link cannot go higher than 17280 we use that in our 2567 * calculations but the DP IN adapter Allocated BW write must be 2568 * the same value (17500) otherwise the adapter will mark it as 2569 * failed for graphics. 2570 */ 2571 ret = tb_tunnel_maximum_bandwidth(tunnel, &max_up, &max_down); 2572 if (ret) 2573 goto fail; 2574 2575 ret = usb4_dp_port_granularity(in); 2576 if (ret < 0) 2577 goto fail; 2578 granularity = ret; 2579 2580 max_up_rounded = roundup(max_up, granularity); 2581 max_down_rounded = roundup(max_down, granularity); 2582 2583 /* 2584 * This will "fix" the request down to the maximum supported 2585 * rate * lanes if it is at the maximum rounded up level. 2586 */ 2587 requested_up_corrected = *requested_up; 2588 if (requested_up_corrected == max_up_rounded) 2589 requested_up_corrected = max_up; 2590 else if (requested_up_corrected < 0) 2591 requested_up_corrected = 0; 2592 requested_down_corrected = *requested_down; 2593 if (requested_down_corrected == max_down_rounded) 2594 requested_down_corrected = max_down; 2595 else if (requested_down_corrected < 0) 2596 requested_down_corrected = 0; 2597 2598 tb_tunnel_dbg(tunnel, "corrected bandwidth request %d/%d Mb/s\n", 2599 requested_up_corrected, requested_down_corrected); 2600 2601 if ((*requested_up >= 0 && requested_up_corrected > max_up_rounded) || 2602 (*requested_down >= 0 && requested_down_corrected > max_down_rounded)) { 2603 tb_tunnel_dbg(tunnel, 2604 "bandwidth request too high (%d/%d Mb/s > %d/%d Mb/s)\n", 2605 requested_up_corrected, requested_down_corrected, 2606 max_up_rounded, max_down_rounded); 2607 ret = -ENOBUFS; 2608 goto fail; 2609 } 2610 2611 downstream = tb_tunnel_direction_downstream(tunnel); 2612 group = in->group; 2613 2614 if ((*requested_up >= 0 && requested_up_corrected <= allocated_up) || 2615 (*requested_down >= 0 && requested_down_corrected <= allocated_down)) { 2616 if (tunnel->bw_mode) { 2617 int reserved; 2618 /* 2619 * If requested bandwidth is less or equal than 2620 * what is currently allocated to that tunnel we 2621 * simply change the reservation of the tunnel 2622 * and add the released bandwidth for the group 2623 * for the next 10s. Then we release it for 2624 * others to use. 2625 */ 2626 if (downstream) 2627 reserved = allocated_down - *requested_down; 2628 else 2629 reserved = allocated_up - *requested_up; 2630 2631 if (reserved > 0) { 2632 group->reserved += reserved; 2633 tb_dbg(tb, "group %d reserved %d total %d Mb/s\n", 2634 group->index, reserved, group->reserved); 2635 2636 /* 2637 * If it was not already pending, 2638 * schedule release now. If it is then 2639 * postpone it for the next 10s (unless 2640 * it is already running in which case 2641 * the 10s already expired and we should 2642 * give the reserved back to others). 2643 */ 2644 mod_delayed_work(system_percpu_wq, &group->release_work, 2645 msecs_to_jiffies(TB_RELEASE_BW_TIMEOUT)); 2646 } 2647 } 2648 2649 ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up, 2650 requested_down); 2651 if (ret) 2652 goto fail; 2653 2654 return 0; 2655 } 2656 2657 /* 2658 * More bandwidth is requested. Release all the potential 2659 * bandwidth from USB3 first. 2660 */ 2661 ret = tb_release_unused_usb3_bandwidth(tb, in, out); 2662 if (ret) 2663 goto fail; 2664 2665 /* 2666 * Then go over all tunnels that cross the same USB4 ports (they 2667 * are also in the same group but we use the same function here 2668 * that we use with the normal bandwidth allocation). 2669 */ 2670 ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down, 2671 true); 2672 if (ret) 2673 goto reclaim; 2674 2675 tb_tunnel_dbg(tunnel, "bandwidth available for allocation %d/%d (+ %u reserved) Mb/s\n", 2676 available_up, available_down, group->reserved); 2677 2678 if ((*requested_up >= 0 && 2679 available_up + group->reserved >= requested_up_corrected) || 2680 (*requested_down >= 0 && 2681 available_down + group->reserved >= requested_down_corrected)) { 2682 int released = 0; 2683 2684 /* 2685 * If bandwidth on a link is >= asym_threshold 2686 * transition the link to asymmetric. 2687 */ 2688 ret = tb_configure_asym(tb, in, out, *requested_up, 2689 *requested_down); 2690 if (ret) { 2691 tb_configure_sym(tb, in, out, true); 2692 goto fail; 2693 } 2694 2695 ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up, 2696 requested_down); 2697 if (ret) { 2698 tb_tunnel_warn(tunnel, "failed to allocate bandwidth\n"); 2699 tb_configure_sym(tb, in, out, true); 2700 } 2701 2702 if (downstream) { 2703 if (*requested_down > available_down) 2704 released = *requested_down - available_down; 2705 } else { 2706 if (*requested_up > available_up) 2707 released = *requested_up - available_up; 2708 } 2709 if (released) { 2710 group->reserved -= released; 2711 tb_dbg(tb, "group %d released %d total %d Mb/s\n", 2712 group->index, released, group->reserved); 2713 } 2714 } else { 2715 ret = -ENOBUFS; 2716 } 2717 2718 reclaim: 2719 tb_reclaim_usb3_bandwidth(tb, in, out); 2720 fail: 2721 if (ret && ret != -ENODEV) { 2722 /* 2723 * Write back the same allocated (so no change), this 2724 * makes the DPTX request fail on graphics side. 2725 */ 2726 tb_tunnel_dbg(tunnel, 2727 "failing the request by rewriting allocated %d/%d Mb/s\n", 2728 allocated_up, allocated_down); 2729 tb_tunnel_alloc_bandwidth(tunnel, &allocated_up, &allocated_down); 2730 tb_tunnel_event(tb, TB_TUNNEL_NO_BANDWIDTH, TB_TUNNEL_DP, in, out); 2731 } 2732 2733 return ret; 2734 } 2735 2736 static void tb_handle_dp_bandwidth_request(struct work_struct *work) 2737 { 2738 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work); 2739 int requested_bw, requested_up, requested_down, ret; 2740 struct tb_tunnel *tunnel; 2741 struct tb *tb = ev->tb; 2742 struct tb_cm *tcm = tb_priv(tb); 2743 struct tb_switch *sw; 2744 struct tb_port *in; 2745 2746 pm_runtime_get_sync(&tb->dev); 2747 2748 mutex_lock(&tb->lock); 2749 if (!tcm->hotplug_active) 2750 goto unlock; 2751 2752 sw = tb_switch_find_by_route(tb, ev->route); 2753 if (!sw) { 2754 tb_warn(tb, "bandwidth request from non-existent router %llx\n", 2755 ev->route); 2756 goto unlock; 2757 } 2758 2759 in = &sw->ports[ev->port]; 2760 if (!tb_port_is_dpin(in)) { 2761 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); 2762 goto put_sw; 2763 } 2764 2765 tb_port_dbg(in, "handling bandwidth allocation request, retry %d\n", ev->retry); 2766 2767 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL); 2768 if (!tunnel) { 2769 tb_port_warn(in, "failed to find tunnel\n"); 2770 goto put_sw; 2771 } 2772 2773 if (!usb4_dp_port_bandwidth_mode_enabled(in)) { 2774 if (tunnel->bw_mode) { 2775 /* 2776 * Reset the tunnel back to use the legacy 2777 * allocation. 2778 */ 2779 tunnel->bw_mode = false; 2780 tb_port_dbg(in, "DPTX disabled bandwidth allocation mode\n"); 2781 } else { 2782 tb_port_warn(in, "bandwidth allocation mode not enabled\n"); 2783 } 2784 goto put_sw; 2785 } 2786 2787 ret = usb4_dp_port_requested_bandwidth(in); 2788 if (ret < 0) { 2789 if (ret == -ENODATA) { 2790 /* 2791 * There is no request active so this means the 2792 * BW allocation mode was enabled from graphics 2793 * side. At this point we know that the graphics 2794 * driver has read the DPRX capabilities so we 2795 * can offer better bandwidth estimation. 2796 */ 2797 tb_port_dbg(in, "DPTX enabled bandwidth allocation mode, updating estimated bandwidth\n"); 2798 tb_recalc_estimated_bandwidth(tb); 2799 } else { 2800 tb_port_warn(in, "failed to read requested bandwidth\n"); 2801 } 2802 goto put_sw; 2803 } 2804 requested_bw = ret; 2805 2806 tb_port_dbg(in, "requested bandwidth %d Mb/s\n", requested_bw); 2807 2808 if (tb_tunnel_direction_downstream(tunnel)) { 2809 requested_up = -1; 2810 requested_down = requested_bw; 2811 } else { 2812 requested_up = requested_bw; 2813 requested_down = -1; 2814 } 2815 2816 ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down); 2817 if (ret) { 2818 if (ret == -ENOBUFS) { 2819 tb_tunnel_warn(tunnel, 2820 "not enough bandwidth available\n"); 2821 } else if (ret == -ENOTCONN) { 2822 tb_tunnel_dbg(tunnel, "not active yet\n"); 2823 /* 2824 * We got bandwidth allocation request but the 2825 * tunnel is not yet active. This means that 2826 * tb_dp_tunnel_active() is not yet called for 2827 * this tunnel. Allow it some time and retry 2828 * this request a couple of times. 2829 */ 2830 if (ev->retry < TB_BW_ALLOC_RETRIES) { 2831 tb_tunnel_dbg(tunnel, 2832 "retrying bandwidth allocation request\n"); 2833 tb_queue_dp_bandwidth_request(tb, ev->route, 2834 ev->port, 2835 ev->retry + 1, 2836 msecs_to_jiffies(50)); 2837 } else { 2838 tb_tunnel_dbg(tunnel, 2839 "run out of retries, failing the request"); 2840 } 2841 } else { 2842 tb_tunnel_warn(tunnel, 2843 "failed to change bandwidth allocation\n"); 2844 } 2845 } else { 2846 tb_tunnel_dbg(tunnel, 2847 "bandwidth allocation changed to %d/%d Mb/s\n", 2848 requested_up, requested_down); 2849 2850 /* Update other clients about the allocation change */ 2851 tb_recalc_estimated_bandwidth(tb); 2852 2853 tb_dbg(tb, "checking if more DP tunnels can be established now\n"); 2854 tb_tunnel_dp(tb); 2855 } 2856 2857 put_sw: 2858 tb_switch_put(sw); 2859 unlock: 2860 mutex_unlock(&tb->lock); 2861 2862 pm_runtime_mark_last_busy(&tb->dev); 2863 pm_runtime_put_autosuspend(&tb->dev); 2864 2865 kfree(ev); 2866 } 2867 2868 static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port, 2869 int retry, unsigned long delay) 2870 { 2871 struct tb_hotplug_event *ev; 2872 2873 ev = kmalloc_obj(*ev); 2874 if (!ev) 2875 return; 2876 2877 ev->tb = tb; 2878 ev->route = route; 2879 ev->port = port; 2880 ev->retry = retry; 2881 INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request); 2882 queue_delayed_work(tb->wq, &ev->work, delay); 2883 } 2884 2885 static void tb_handle_notification(struct tb *tb, u64 route, 2886 const struct cfg_error_pkg *error) 2887 { 2888 2889 switch (error->error) { 2890 case TB_CFG_ERROR_PCIE_WAKE: 2891 case TB_CFG_ERROR_DP_CON_CHANGE: 2892 case TB_CFG_ERROR_DPTX_DISCOVERY: 2893 if (tb_cfg_ack_notification(tb->ctl, route, error)) 2894 tb_warn(tb, "could not ack notification on %llx\n", 2895 route); 2896 break; 2897 2898 case TB_CFG_ERROR_DP_BW: 2899 if (tb_cfg_ack_notification(tb->ctl, route, error)) 2900 tb_warn(tb, "could not ack notification on %llx\n", 2901 route); 2902 tb_queue_dp_bandwidth_request(tb, route, error->port, 0, 0); 2903 break; 2904 2905 default: 2906 /* Ignore for now */ 2907 break; 2908 } 2909 } 2910 2911 /* 2912 * tb_schedule_hotplug_handler() - callback function for the control channel 2913 * 2914 * Delegates to tb_handle_hotplug. 2915 */ 2916 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, 2917 const void *buf, size_t size) 2918 { 2919 const struct cfg_event_pkg *pkg = buf; 2920 u64 route = tb_cfg_get_route(&pkg->header); 2921 2922 switch (type) { 2923 case TB_CFG_PKG_ERROR: 2924 tb_handle_notification(tb, route, (const struct cfg_error_pkg *)buf); 2925 return; 2926 case TB_CFG_PKG_EVENT: 2927 break; 2928 default: 2929 tb_warn(tb, "unexpected event %#x, ignoring\n", type); 2930 return; 2931 } 2932 2933 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { 2934 tb_warn(tb, "could not ack plug event on %llx:%x\n", route, 2935 pkg->port); 2936 } 2937 2938 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); 2939 } 2940 2941 static void tb_stop(struct tb *tb) 2942 { 2943 struct tb_cm *tcm = tb_priv(tb); 2944 struct tb_tunnel *tunnel; 2945 struct tb_tunnel *n; 2946 2947 cancel_delayed_work(&tcm->remove_work); 2948 /* tunnels are only present after everything has been initialized */ 2949 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { 2950 /* 2951 * DMA tunnels require the driver to be functional so we 2952 * tear them down. Other protocol tunnels can be left 2953 * intact. 2954 */ 2955 if (tb_tunnel_is_dma(tunnel)) 2956 tb_tunnel_deactivate(tunnel); 2957 tb_tunnel_put(tunnel); 2958 } 2959 tb_switch_remove(tb->root_switch); 2960 tb->root_switch = NULL; 2961 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ 2962 } 2963 2964 static void tb_deinit(struct tb *tb) 2965 { 2966 struct tb_cm *tcm = tb_priv(tb); 2967 int i; 2968 2969 /* Cancel all the release bandwidth workers */ 2970 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) 2971 cancel_delayed_work_sync(&tcm->groups[i].release_work); 2972 } 2973 2974 static int tb_scan_finalize_switch(struct device *dev, void *data) 2975 { 2976 if (tb_is_switch(dev)) { 2977 struct tb_switch *sw = tb_to_switch(dev); 2978 2979 /* 2980 * If we found that the switch was already setup by the 2981 * boot firmware, mark it as authorized now before we 2982 * send uevent to userspace. 2983 */ 2984 if (sw->boot) 2985 sw->authorized = 1; 2986 2987 dev_set_uevent_suppress(dev, false); 2988 kobject_uevent(&dev->kobj, KOBJ_ADD); 2989 device_for_each_child(dev, NULL, tb_scan_finalize_switch); 2990 } 2991 2992 return 0; 2993 } 2994 2995 static int tb_start(struct tb *tb, bool reset) 2996 { 2997 struct tb_cm *tcm = tb_priv(tb); 2998 bool discover = true; 2999 int ret; 3000 3001 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); 3002 if (IS_ERR(tb->root_switch)) 3003 return dev_err_probe(tb->nhi->dev, PTR_ERR(tb->root_switch), 3004 "failed to allocate host router\n"); 3005 3006 /* 3007 * ICM firmware upgrade needs running firmware and in native 3008 * mode that is not available so disable firmware upgrade of the 3009 * root switch. 3010 * 3011 * However, USB4 routers support NVM firmware upgrade if they 3012 * implement the necessary router operations. 3013 */ 3014 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); 3015 /* All USB4 routers support runtime PM */ 3016 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); 3017 3018 ret = tb_switch_configure(tb->root_switch); 3019 if (ret) { 3020 tb_switch_put(tb->root_switch); 3021 return dev_err_probe(tb->nhi->dev, ret, "failed to configure host router\n"); 3022 } 3023 3024 /* Announce the switch to the world */ 3025 ret = tb_switch_add(tb->root_switch); 3026 if (ret) { 3027 tb_switch_put(tb->root_switch); 3028 return dev_err_probe(tb->nhi->dev, ret, "failed to add host router\n"); 3029 } 3030 3031 /* 3032 * To support highest CLx state, we set host router's TMU to 3033 * Normal mode. 3034 */ 3035 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); 3036 /* Enable TMU if it is off */ 3037 tb_switch_tmu_enable(tb->root_switch); 3038 3039 /* 3040 * Boot firmware might have created tunnels of its own. Since we 3041 * cannot be sure they are usable for us, tear them down and 3042 * reset the ports to handle it as new hotplug for USB4 v1 3043 * routers (for USB4 v2 and beyond we already do host reset). 3044 */ 3045 if (reset && tb_switch_is_usb4(tb->root_switch)) { 3046 discover = false; 3047 if (usb4_switch_version(tb->root_switch) == 1) 3048 tb_switch_reset(tb->root_switch); 3049 } 3050 3051 if (discover) { 3052 /* Full scan to discover devices added before the driver was loaded. */ 3053 tb_scan_switch(tb->root_switch); 3054 /* Find out tunnels created by the boot firmware */ 3055 tb_discover_tunnels(tb); 3056 /* Add DP resources from the DP tunnels created by the boot firmware */ 3057 tb_discover_dp_resources(tb); 3058 } 3059 3060 /* 3061 * If the boot firmware did not create USB 3.x tunnels create them 3062 * now for the whole topology. 3063 */ 3064 tb_create_usb3_tunnels(tb->root_switch); 3065 /* Add DP IN resources for the root switch */ 3066 tb_add_dp_resources(tb->root_switch); 3067 tb_switch_enter_redrive(tb->root_switch); 3068 /* Make the discovered switches available to the userspace */ 3069 device_for_each_child(&tb->root_switch->dev, NULL, 3070 tb_scan_finalize_switch); 3071 3072 /* Allow tb_handle_hotplug to progress events */ 3073 tcm->hotplug_active = true; 3074 return 0; 3075 } 3076 3077 static int tb_suspend_noirq(struct tb *tb) 3078 { 3079 struct tb_cm *tcm = tb_priv(tb); 3080 3081 tb_dbg(tb, "suspending...\n"); 3082 tb_disconnect_and_release_dp(tb); 3083 tb_switch_exit_redrive(tb->root_switch); 3084 tb_switch_suspend(tb->root_switch, false); 3085 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ 3086 tb_dbg(tb, "suspend finished\n"); 3087 3088 return 0; 3089 } 3090 3091 static void tb_restore_children(struct tb_switch *sw) 3092 { 3093 struct tb_port *port; 3094 3095 /* No need to restore if the router is already unplugged */ 3096 if (sw->is_unplugged) 3097 return; 3098 3099 if (tb_enable_clx(sw)) 3100 tb_sw_warn(sw, "failed to re-enable CL states\n"); 3101 3102 if (tb_enable_tmu(sw)) 3103 tb_sw_warn(sw, "failed to restore TMU configuration\n"); 3104 3105 tb_switch_configuration_valid(sw); 3106 3107 tb_switch_for_each_port(sw, port) { 3108 if (!tb_port_has_remote(port) && !port->xdomain) 3109 continue; 3110 3111 if (port->remote) { 3112 tb_switch_set_link_width(port->remote->sw, 3113 port->remote->sw->link_width); 3114 tb_switch_configure_link(port->remote->sw); 3115 3116 tb_restore_children(port->remote->sw); 3117 } else if (port->xdomain) { 3118 tb_port_configure_xdomain(port, port->xdomain); 3119 } 3120 } 3121 } 3122 3123 static void tb_free_unplugged_xdomains(struct tb_switch *sw) 3124 { 3125 struct tb_port *port; 3126 3127 tb_switch_for_each_port(sw, port) { 3128 if (tb_is_upstream_port(port)) 3129 continue; 3130 if (port->xdomain && port->xdomain->is_unplugged) { 3131 tb_retimer_remove_all(port); 3132 tb_xdomain_remove(port->xdomain); 3133 tb_port_unconfigure_xdomain(port); 3134 port->xdomain = NULL; 3135 } else if (port->remote) { 3136 tb_free_unplugged_xdomains(port->remote->sw); 3137 } 3138 } 3139 } 3140 3141 static int tb_resume_noirq(struct tb *tb) 3142 { 3143 struct tb_cm *tcm = tb_priv(tb); 3144 struct tb_tunnel *tunnel, *n; 3145 unsigned int usb3_delay = 0; 3146 LIST_HEAD(tunnels); 3147 3148 tb_dbg(tb, "resuming...\n"); 3149 3150 /* 3151 * For non-USB4 hosts (Apple systems) remove any PCIe devices 3152 * the firmware might have setup. 3153 */ 3154 if (!tb_switch_is_usb4(tb->root_switch)) 3155 tb_switch_reset(tb->root_switch); 3156 3157 tb_switch_resume(tb->root_switch, false); 3158 tb_free_invalid_tunnels(tb); 3159 tb_free_unplugged_children(tb->root_switch); 3160 tb_free_unplugged_xdomains(tb->root_switch); 3161 tb_restore_children(tb->root_switch); 3162 3163 /* 3164 * If we get here from suspend to disk the boot firmware or the 3165 * restore kernel might have created tunnels of its own. Since 3166 * we cannot be sure they are usable for us we find and tear 3167 * them down. 3168 */ 3169 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); 3170 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) { 3171 if (tb_tunnel_is_usb3(tunnel)) 3172 usb3_delay = 500; 3173 tb_tunnel_deactivate(tunnel); 3174 tb_tunnel_put(tunnel); 3175 } 3176 3177 /* Re-create our tunnels now */ 3178 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { 3179 /* USB3 requires delay before it can be re-activated */ 3180 if (tb_tunnel_is_usb3(tunnel)) { 3181 msleep(usb3_delay); 3182 /* Only need to do it once */ 3183 usb3_delay = 0; 3184 } 3185 tb_tunnel_activate(tunnel); 3186 } 3187 if (!list_empty(&tcm->tunnel_list)) { 3188 /* 3189 * the pcie links need some time to get going. 3190 * 100ms works for me... 3191 */ 3192 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n"); 3193 msleep(100); 3194 } 3195 tb_switch_enter_redrive(tb->root_switch); 3196 /* Allow tb_handle_hotplug to progress events */ 3197 tcm->hotplug_active = true; 3198 tb_dbg(tb, "resume finished\n"); 3199 3200 return 0; 3201 } 3202 3203 static int tb_freeze_noirq(struct tb *tb) 3204 { 3205 struct tb_cm *tcm = tb_priv(tb); 3206 3207 tcm->hotplug_active = false; 3208 return 0; 3209 } 3210 3211 static int tb_thaw_noirq(struct tb *tb) 3212 { 3213 struct tb_cm *tcm = tb_priv(tb); 3214 3215 tcm->hotplug_active = true; 3216 return 0; 3217 } 3218 3219 static void tb_complete(struct tb *tb) 3220 { 3221 /* 3222 * Unregister unplugged XDomains and if there is a case where 3223 * another domain is swapped in place of unplugged XDomain we 3224 * need to run another rescan. 3225 */ 3226 if (tb_domain_unregister_unplugged_xdomains(tb)) { 3227 scoped_guard(mutex, &tb->lock) 3228 tb_scan_switch(tb->root_switch); 3229 } 3230 } 3231 3232 static int tb_runtime_suspend(struct tb *tb) 3233 { 3234 struct tb_cm *tcm = tb_priv(tb); 3235 3236 mutex_lock(&tb->lock); 3237 /* 3238 * The below call only releases DP resources to allow exiting and 3239 * re-entering redrive mode. 3240 */ 3241 tb_disconnect_and_release_dp(tb); 3242 tb_switch_exit_redrive(tb->root_switch); 3243 tb_switch_suspend(tb->root_switch, true); 3244 tcm->hotplug_active = false; 3245 mutex_unlock(&tb->lock); 3246 3247 return 0; 3248 } 3249 3250 static void tb_remove_work(struct work_struct *work) 3251 { 3252 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work); 3253 struct tb *tb = tcm_to_tb(tcm); 3254 3255 mutex_lock(&tb->lock); 3256 if (tb->root_switch) 3257 tb_free_unplugged_children(tb->root_switch); 3258 mutex_unlock(&tb->lock); 3259 3260 tb_free_unplugged_xdomains(tb->root_switch); 3261 } 3262 3263 static int tb_runtime_resume(struct tb *tb) 3264 { 3265 struct tb_cm *tcm = tb_priv(tb); 3266 struct tb_tunnel *tunnel, *n; 3267 3268 mutex_lock(&tb->lock); 3269 tb_switch_resume(tb->root_switch, true); 3270 tb_free_invalid_tunnels(tb); 3271 tb_restore_children(tb->root_switch); 3272 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) 3273 tb_tunnel_activate(tunnel); 3274 tb_switch_enter_redrive(tb->root_switch); 3275 tcm->hotplug_active = true; 3276 mutex_unlock(&tb->lock); 3277 3278 /* 3279 * Schedule cleanup of any unplugged devices. Run this in a 3280 * separate thread to avoid possible deadlock if the device 3281 * removal runtime resumes the unplugged device. 3282 */ 3283 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); 3284 return 0; 3285 } 3286 3287 static const struct tb_cm_ops tb_cm_ops = { 3288 .start = tb_start, 3289 .stop = tb_stop, 3290 .deinit = tb_deinit, 3291 .suspend_noirq = tb_suspend_noirq, 3292 .resume_noirq = tb_resume_noirq, 3293 .freeze_noirq = tb_freeze_noirq, 3294 .thaw_noirq = tb_thaw_noirq, 3295 .complete = tb_complete, 3296 .runtime_suspend = tb_runtime_suspend, 3297 .runtime_resume = tb_runtime_resume, 3298 .handle_event = tb_handle_event, 3299 .disapprove_switch = tb_disconnect_pci, 3300 .approve_switch = tb_tunnel_pci, 3301 .approve_xdomain_paths = tb_approve_xdomain_paths, 3302 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths, 3303 }; 3304 3305 /* 3306 * During suspend the Thunderbolt controller is reset and all PCIe 3307 * tunnels are lost. The NHI driver will try to reestablish all tunnels 3308 * during resume. This adds device links between the tunneled PCIe 3309 * downstream ports and the NHI so that the device core will make sure 3310 * NHI is resumed first before the rest. 3311 */ 3312 static bool tb_apple_add_links(struct tb_nhi *nhi) 3313 { 3314 struct pci_dev *nhi_pdev = to_pci_dev(nhi->dev); 3315 struct pci_dev *upstream, *pdev; 3316 bool ret; 3317 3318 if (!x86_apple_machine) 3319 return false; 3320 3321 switch (nhi_pdev->device) { 3322 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: 3323 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: 3324 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI: 3325 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI: 3326 break; 3327 default: 3328 return false; 3329 } 3330 3331 upstream = pci_upstream_bridge(nhi_pdev); 3332 while (upstream) { 3333 if (!pci_is_pcie(upstream)) 3334 return false; 3335 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM) 3336 break; 3337 upstream = pci_upstream_bridge(upstream); 3338 } 3339 3340 if (!upstream) 3341 return false; 3342 3343 /* 3344 * For each hotplug downstream port, create add device link 3345 * back to NHI so that PCIe tunnels can be re-established after 3346 * sleep. 3347 */ 3348 ret = false; 3349 for_each_pci_bridge(pdev, upstream->subordinate) { 3350 const struct device_link *link; 3351 3352 if (!pci_is_pcie(pdev)) 3353 continue; 3354 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM || 3355 !pdev->is_pciehp) 3356 continue; 3357 3358 link = device_link_add(&pdev->dev, nhi->dev, 3359 DL_FLAG_AUTOREMOVE_SUPPLIER | 3360 DL_FLAG_PM_RUNTIME); 3361 if (link) { 3362 dev_dbg(nhi->dev, "created link from %s\n", 3363 dev_name(&pdev->dev)); 3364 ret = true; 3365 } else { 3366 dev_warn(nhi->dev, "device link creation from %s failed\n", 3367 dev_name(&pdev->dev)); 3368 } 3369 } 3370 3371 return ret; 3372 } 3373 3374 struct tb *tb_probe(struct tb_nhi *nhi) 3375 { 3376 struct tb_cm *tcm; 3377 struct tb *tb; 3378 3379 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm)); 3380 if (!tb) 3381 return NULL; 3382 3383 if (tb_acpi_may_tunnel_pcie()) 3384 tb->security_level = TB_SECURITY_USER; 3385 else 3386 tb->security_level = TB_SECURITY_NOPCIE; 3387 3388 tb->cm_ops = &tb_cm_ops; 3389 3390 tcm = tb_priv(tb); 3391 INIT_LIST_HEAD(&tcm->tunnel_list); 3392 INIT_LIST_HEAD(&tcm->dp_resources); 3393 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); 3394 tb_init_bandwidth_groups(tcm); 3395 3396 tb_dbg(tb, "using software connection manager\n"); 3397 3398 /* 3399 * Device links are needed to make sure we establish tunnels 3400 * before the PCIe/USB stack is resumed so complain here if we 3401 * found them missing. 3402 */ 3403 if (!tb_apple_add_links(nhi) && !tb_acpi_add_links(nhi)) 3404 tb_warn(tb, "device links to tunneled native ports are missing!\n"); 3405 3406 return tb; 3407 } 3408