From fab82c8247db0469c30ed13c632b68073c180956 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:50:37 +1100 Subject: [PATCH] thunderbolt: apple: request USB4 Gen4 wherever the host router has it The M4 Pro and M4 Max (T6040, T6041) ports are Thunderbolt 5 / USB4 v2 ports, but only the T6050 description asked its router for Gen4: the cable word for a Gen4 cable said Gen4 (0x801) on T6050 and 20 Gb/s (0x11) everywhere else, so T6040 links never got past Gen3, whatever the cable and the partner. Whether a host router can run Gen4 is in its own config space: USB4 v2 lane adapters report Gen4 in the Supported Link Speeds field of LANE_ADP_CS_0, USB4 v1 routers leave that bit clear. The J616s (T6040) host router reports it: its lane adapter target speed is 0xe (Gen2, Gen3 and Gen4) where the M2's is 0xc. Select Gen4 by that bit instead of by SoC: - The Type-C switch records a Gen4 cable as Gen4 for every router. - When the cable word is written (first start, and the wakes in apple-cio-sleep.c), a router whose lane adapters do not report Gen4 gets the 20 Gb/s word it got before. - Gen4 link recovery is requested on every Apple host. The core only touches links that trained at Gen4, so nothing changes on Gen2/Gen3 links. T6050 keeps the word it had. USB4 v1 routers keep the 20 Gb/s word (0x11). T6040/T6041 now send 0x801 on a Gen4 cable. The speed itself is still negotiated by the two routers: a Gen3 partner on a Gen4 cable is expected to train Gen3. Asymmetric links are left as they were: allowed by the core capability checks, and only reached through DP tunnels, which are off on T6040; T6050 keeps QUIRK_NO_ASYM for its documented link drop. Signed-off-by: Ryan Murray --- drivers/thunderbolt/apple-cio-sleep.c | 14 +++--- drivers/thunderbolt/apple.c | 72 ++++++++++++++++++++++----- drivers/thunderbolt/tb_regs.h | 1 + 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/drivers/thunderbolt/apple-cio-sleep.c b/drivers/thunderbolt/apple-cio-sleep.c index d6ce8599a1ff5..3d1665d45b0c7 100644 --- a/drivers/thunderbolt/apple-cio-sleep.c +++ b/drivers/thunderbolt/apple-cio-sleep.c @@ -48,6 +48,7 @@ static int apple_nhi_cio_router_wake(struct tb_nhi *nhi) struct apple_nhi *anhi = nhi_to_anhi(nhi); struct tb_switch *sw = anhi->tb->root_switch; struct tb_port *port; + u32 cable_info; int cap, ret; cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_APPLE); @@ -60,10 +61,10 @@ static int apple_nhi_cio_router_wake(struct tb_nhi *nhi) if (ret) dev_warn(anhi->dev, "CIO port %u unlock failed: %d\n", port->port, ret); } - ret = tb_sw_write(sw, &anhi->acio->current_cable_info, TB_CFG_SWITCH, + cable_info = apple_nhi_cable_info(anhi, anhi->acio->current_cable_info); + ret = tb_sw_write(sw, &cable_info, TB_CFG_SWITCH, cap + TB_VSE_CAP_APPLE_CABLE_INFO, 1); - dev_info(anhi->dev, "CIO cable word %#x written: %d\n", anhi->acio->current_cable_info, - ret); + dev_info(anhi->dev, "CIO cable word %#x written: %d\n", cable_info, ret); return ret; } @@ -285,6 +286,7 @@ static int apple_nhi_cio_router_wake_t6050(struct tb_nhi *nhi) { struct apple_nhi *anhi = nhi_to_anhi(nhi); struct tb_switch *sw = anhi->tb->root_switch; + u32 cable_info; int cap, ret; apple_nhi_cio_wake_op_t6050(anhi, APPLE_CIO_T6050_WAKE_OP_A, 0); @@ -292,10 +294,10 @@ static int apple_nhi_cio_router_wake_t6050(struct tb_nhi *nhi) cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_APPLE); if (cap < 0) return cap; - ret = tb_sw_write(sw, &anhi->acio->current_cable_info, TB_CFG_SWITCH, + cable_info = apple_nhi_cable_info(anhi, anhi->acio->current_cable_info); + ret = tb_sw_write(sw, &cable_info, TB_CFG_SWITCH, cap + TB_VSE_CAP_APPLE_CABLE_INFO, 1); - dev_info(anhi->dev, "CIO cable word %#x written: %d\n", anhi->acio->current_cable_info, - ret); + dev_info(anhi->dev, "CIO cable word %#x written: %d\n", cable_info, ret); return ret; } diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index ac8e1a4205098..7e7f52ad502de 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -165,10 +165,6 @@ struct apple_cio_hw { bool run_store; /* Hold LSTX low as a GPIO from probe until the router first takes it. */ bool lstx_hold_from_probe; - /* The router trains Gen4 only on a cable word that says Gen4. */ - bool gen4_cable; - /* Run Gen4 links with link recovery enabled at both ends. */ - bool gen4_link_recovery; /* Leave the host router's TMU to its firmware; host TMU constants. */ bool host_tmu; const struct apple_dpin_policy *dp; @@ -220,6 +216,8 @@ static const struct apple_cio_hw apple_cio_t8142_hw = { * the M5 policy, see apple_dpin_m5. A Gen4 link times out CL training once CL * states are enabled, then drops; it also dropped in the middle of a * transition to an asymmetric width. Its links stay in CL0 and symmetric. + * Gen4 itself is not a property of this description: see + * apple_nhi_host_gen4(). */ static const struct apple_cio_hw apple_cio_t6050_hw = { .dpin_count = 4, @@ -229,10 +227,8 @@ static const struct apple_cio_hw apple_cio_t6050_hw = { .lstx_after_boot = true, .lstx_hold_from_probe = true, .strict_fw_ready = true, - .gen4_cable = true, .no_host_clx = true, .no_asym = true, - .gen4_link_recovery = true, .host_tmu = true, .dp = &apple_dpin_m5, }; @@ -2578,6 +2574,47 @@ static const struct tb_nhi_ring_layout apple_nhi_ring_layout = { .options_stride = APPLE_CIO_NHI_RING_STRIDE, }; +/* + * A host router that can run USB4 Gen4 says so in the Supported Link Speeds + * field of its lane adapters (USB4 v2, LANE_ADP_CS_0). The field is the + * same on every Apple USB4 router, so this, not the SoC, decides whether a + * Gen4 cable word is handed to the router. + */ +static bool apple_nhi_host_gen4(struct tb_switch *sw) +{ + struct tb_port *port; + u32 val; + + tb_switch_for_each_port(sw, port) { + if (!tb_port_is_null(port) || !port->cap_phy) + continue; + if (tb_port_read(port, &val, TB_CFG_PORT, + port->cap_phy + LANE_ADP_CS_0, 1)) + continue; + if (FIELD_GET(LANE_ADP_CS_0_SUPPORTED_SPEED_MASK, val) & + LANE_ADP_CS_0_SUPPORTED_SPEED_GEN4) + return true; + } + return false; +} + +/* + * The cable word as the host router gets it: a Gen4 cable on a router + * without Gen4 is a 20 Gb/s cable to it. + */ +static u32 apple_nhi_cable_info(struct apple_nhi *anhi, u32 info) +{ + if (!(info & TB_VSE_CAP_APPLE_CABLE_INFO_GEN4)) + return info; + if (apple_nhi_host_gen4(anhi->tb->root_switch)) { + dev_dbg(anhi->dev, "Gen4 cable, host router supports Gen4\n"); + return info; + } + dev_dbg(anhi->dev, "Gen4 cable, host router is Gen3: 20 Gb/s cable word\n"); + return (info & ~TB_VSE_CAP_APPLE_CABLE_INFO_GEN4) | + TB_VSE_CAP_APPLE_CABLE_INFO_20_GBPS; +} + static int apple_nhi_probe(struct platform_device *pdev) { struct apple_cio *acio = dev_get_drvdata(pdev->dev.parent); @@ -2586,6 +2623,7 @@ static int apple_nhi_probe(struct platform_device *pdev) struct resource *res; struct tb_port *port; enum apple_dpin_flow dp_hooks; + u32 cable_info; int cap_apple; int ret = 0; @@ -2702,8 +2740,13 @@ static int apple_nhi_probe(struct platform_device *pdev) anhi->nhi.quirks |= QUIRK_NO_HOST_CLX; if (acio->hw->no_asym) anhi->nhi.quirks |= QUIRK_NO_ASYM; - if (acio->hw->gen4_link_recovery) - anhi->nhi.quirks |= QUIRK_GEN4_LINK_RECOVERY; + /* + * The router firmware has no way back from a Gen4 link error but to + * disconnect the link unless link recovery is on at both ends. The core + * only touches links that trained at Gen4, so this is a no-op on routers + * and cables without Gen4. + */ + anhi->nhi.quirks |= QUIRK_GEN4_LINK_RECOVERY; if (acio->hw->host_tmu) anhi->nhi.quirks |= QUIRK_HOST_TMU; @@ -2791,7 +2834,8 @@ static int apple_nhi_probe(struct platform_device *pdev) port->port, ret); } - ret = tb_sw_write(anhi->tb->root_switch, &acio->target_cable_info, TB_CFG_SWITCH, + cable_info = apple_nhi_cable_info(anhi, acio->target_cable_info); + ret = tb_sw_write(anhi->tb->root_switch, &cable_info, TB_CFG_SWITCH, cap_apple + TB_VSE_CAP_APPLE_CABLE_INFO, 1); if (ret) { dev_warn(anhi->dev, "Setting VSE Apple cable info failed: %d\n", ret); @@ -3900,11 +3944,13 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, if (FIELD_GET(EUDO_CABLE_TYPE_MASK, data->usb4.eudo) != EUDO_CABLE_TYPE_PASSIVE) acio->target_cable_info |= TB_VSE_CAP_APPLE_CABLE_INFO_ACTIVE_CABLE; /* - * The T6050 router firmware trains Gen4 only on a Gen4 cable, and - * takes Gen4 alone: no 20 Gb/s bit beside it. + * The router firmware trains Gen4 only on a Gen4 cable, and takes + * Gen4 alone: no 20 Gb/s bit beside it. Whether the host router + * can run Gen4 at all is only known once its config space is up: + * apple_nhi_cable_info() turns this into the 20 Gb/s word there + * for a router without Gen4. */ - if (acio->hw->gen4_cable && - FIELD_GET(EUDO_CABLE_SPEED_MASK, data->usb4.eudo) >= + if (FIELD_GET(EUDO_CABLE_SPEED_MASK, data->usb4.eudo) >= APPLE_EUDO_CABLE_SPEED_USB4_GEN4) acio->target_cable_info |= TB_VSE_CAP_APPLE_CABLE_INFO_GEN4; else if (FIELD_GET(EUDO_CABLE_SPEED_MASK, data->usb4.eudo) >= diff --git a/drivers/thunderbolt/tb_regs.h b/drivers/thunderbolt/tb_regs.h index a5e0d622db139..abc2f8a14fef9 100644 --- a/drivers/thunderbolt/tb_regs.h +++ b/drivers/thunderbolt/tb_regs.h @@ -339,6 +339,7 @@ struct tb_regs_port_header { #define LANE_ADP_CS_0 0x00 #define LANE_ADP_CS_0_SUPPORTED_SPEED_MASK GENMASK(19, 16) #define LANE_ADP_CS_0_SUPPORTED_SPEED_SHIFT 16 +#define LANE_ADP_CS_0_SUPPORTED_SPEED_GEN4 0x2 #define LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK GENMASK(25, 20) #define LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT 20 #define LANE_ADP_CS_0_SUPPORTED_WIDTH_DUAL 0x2