From 54f659b583506a87cd22c75688b546c012bda69c Mon Sep 17 00:00:00 2001 From: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:23:08 -0700 Subject: [PATCH 1/3] thunderbolt: apple: tell appledrm whether a DP tunnel is still requested appledrm connects a DP tunnel's display from the DP IN adapter's ordered worker and waits for DCP to configure the link. A tunnel that is taken down meanwhile is detached only after that wait: the detach is queued behind it. The adapter knows at once, when the tunnel is deactivated, that the tunnel is no longer requested. Export apple_dp_tunnel_binding_live() so that appledrm can ask, for the binding it was handed, whether its tunnel is still requested. It takes the adapter's lock, as the binding's set_active callback does. Signed-off-by: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> --- drivers/thunderbolt/apple.c | 11 +++++++++++ include/linux/soc/apple/dp-tunnel.h | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 3aa62de12e91b..ab5fcec02a606 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -839,6 +839,17 @@ static int apple_dpin_dcp_set_active(void *data, bool active) return ret; } +bool apple_dp_tunnel_binding_live(void *data) +{ + const struct apple_dpin_binding *binding = data; + struct apple_dpin_ctx *c = binding->ctx; + + guard(mutex)(&c->lock); + return c->state.alive && + apple_dpin_token_access(&c->tokens, binding->generation); +} +EXPORT_SYMBOL_GPL(apple_dp_tunnel_binding_live); + static bool apple_dpin_provider_get(void *data) { struct apple_dpin_binding *binding = data; diff --git a/include/linux/soc/apple/dp-tunnel.h b/include/linux/soc/apple/dp-tunnel.h index 034a74b852ebc..09afceeda7000 100644 --- a/include/linux/soc/apple/dp-tunnel.h +++ b/include/linux/soc/apple/dp-tunnel.h @@ -38,6 +38,14 @@ int apple_dcp_tb_dp_tunnel(struct device_node *connector_np, unsigned int dpin, u64 generation, bool active, int (*set_active)(void *binding, bool active), void *binding); +/* + * Thunderbolt glue: is the tunnel handed to appledrm with @binding still + * requested? It is not from the moment the tunnel is deactivated, before + * the detach that follows reaches appledrm. Takes the DP IN adapter's lock, + * as @set_active does; may sleep. + */ +bool apple_dp_tunnel_binding_live(void *binding); + /* The sink raised IRQ_HPD on the tunnel's DP IN adapter. */ #define APPLE_DP_TUNNEL_IRQ_HPD BIT(0) /* The HPD level of the tunnel's DP IN adapter changed to @hpd. */ From 56793eff52dcb158a31d97e2e78f9c760e2d91e4 Mon Sep 17 00:00:00 2001 From: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:23:08 -0700 Subject: [PATCH 2/3] drm/apple: stop waiting for a link on a DP tunnel that went A Thunderbolt 5 dock (Ugreen 10-in-1) on a J714s withdraws its first DP tunnel about 190 ms after it is requested, right after DCP starts on it, in about half of its first connects after boot or plug-in, and re-advertises it about 1.4 s later. The connect then waits the full 2 s for a link configuration that cannot come, while DCP retries the link about every 500 ms. The release that follows the timeout can land in the first milliseconds of such a retry, and then DCP never answers it: apple-dcp 2b2e00000.dcp: dcp_dptx_connect: timed out waiting for port 0 link configuration apple-dcp 2b2e00000.dcp: AFK[ep:2c]: AppleDCPDPTXRemotePort (chan:1) service call group 0 cmd 7 failed to complete: -110 thunderbolt-apple-acio 381ac0000.acio: dpin0: display teardown failed: -110 thunderbolt-apple-acio 381ac0000.acio: dpin0: DP tunnel setup failed: -110 The display stays dark, its pipeline held, until the dock is plugged in again. Over 11 such connects, 4 of the 5 releases sent within 26 ms of a retry went unanswered, and none of the 6 sent 97 ms or more after one. Wait for the link in 50 ms slices, and ask the Thunderbolt glue between them whether the tunnel feeding the port is still requested. Once it is not, release the display at once, well before DCP's first retry. This makes an unanswered release rare, not impossible: DCP can still leave an early release unanswered, and then the re-advertised tunnel's connect recovers the display. Only T6050 watches the tunnel for now; elsewhere the wait is unchanged until it is tested there. Tested on J714s with the dock on the right port: over 8 boots the dock withdrew the first tunnel each time, the wait ended 17 to 48 ms later, and all three displays came up on the re-advertised tunnels, with no timeout and no -110. Over a later day of use (8 boots, 33 withdrawals of DP IN 0, 1 and 2), one early release still timed out: DP IN 2's tunnel went at 13:02:16.596, DCP retried at 17.066, the release failed with -110 at 17.658, and the re-route at 19.570 brought the display up. No display stayed dark. Signed-off-by: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> --- drivers/gpu/drm/apple/dcp.c | 87 +++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 3b474f1565f31..da221dd196524 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2054,6 +2054,77 @@ static int dcp_t8132_wait_teardown_answers(struct apple_dcp *dcp, u32 port, return 0; } +/* + * Has the Thunderbolt DP tunnel feeding DPTX port @port been taken down? + * Port 0 carries the tunnel this pipeline was handed; port 1 carries only + * the second tile of a tiled split, fed by its own tunnel. The tunnel's + * provider knows at once, while the detach that follows waits for the + * connect in progress on this pipeline. + */ +static bool dcp_tb_tunnel_withdrawn(struct apple_dcp *dcp, u32 port) +{ + typeof(apple_dp_tunnel_binding_live) *live; + bool gone = false; + void *binding; + + guard(mutex)(&dcp->tb_lock); + binding = port ? dcp->split.binding : dcp->tb_dpin_ctx; + if (!binding) + return false; + live = symbol_get(apple_dp_tunnel_binding_live); + if (live) { + gone = !live(binding); + symbol_put(apple_dp_tunnel_binding_live); + } + return gone; +} + +/* + * Only T6050 is known to need the tunnel watched: a Thunderbolt 5 dock + * there withdraws a fresh tunnel right after DCP starts on it. Elsewhere + * the wait stays as it was until the watch is tested there. + */ +static bool dcp_watches_tunnel_withdrawal(void) +{ + return of_machine_is_compatible("apple,t6050"); +} + +#define DCP_LINKCFG_POLL_MS 50 + +/* + * Wait up to @timeout for DCP's link configuration of @port, returning what + * wait_for_completion_timeout() would. With @watch_tunnel, a Thunderbolt + * tunnel taken down meanwhile ends the wait at once and sets *@withdrawn: + * no link comes on it, and DCP keeps retrying it until released. A release + * sent late, after the full timeout, is more likely to go unanswered and + * leave the pipeline held, so release it as soon as the tunnel goes. + */ +static unsigned long dcp_dptx_wait_linkcfg(struct apple_dcp *dcp, u32 port, + unsigned long timeout, bool watch_tunnel, + bool *withdrawn) +{ + unsigned long deadline = jiffies + timeout; + long left; + + *withdrawn = false; + if (!watch_tunnel) + return wait_for_completion_timeout(&dcp->dptxport[port].linkcfg_completion, + timeout); + for (;;) { + left = (long)(deadline - jiffies); + if (left <= 0) + return 0; + if (wait_for_completion_timeout(&dcp->dptxport[port].linkcfg_completion, + min_t(unsigned long, left, + msecs_to_jiffies(DCP_LINKCFG_POLL_MS)))) + return max_t(long, (long)(deadline - jiffies), 1); + if (dcp_tb_tunnel_withdrawn(dcp, port)) { + *withdrawn = true; + return 0; + } + } +} + /* * Connect DPTX port @port. On failure, @retryable (if given) tells whether * the attempt left nothing outstanding in the firmware, so that a later @@ -2069,7 +2140,7 @@ static int dcp_dptx_connect_raw(struct apple_dcp *dcp, u32 port, bool *retryable /* T8122 dcpext: HPD only after the link wait, see dcp_ext_dptx_607() */ bool late_hpd = dcp_ext_dptx_607(dcp); bool t8132 = dcp_is_t8132_ext(dcp); - bool compact; + bool compact, withdrawn; unsigned long timeout; u64 generation; u8 dfp_port; @@ -2217,12 +2288,20 @@ static int dcp_dptx_connect_raw(struct apple_dcp *dcp, u32 port, bool *retryable } timeout = dcp_uses_t6020_tunnel_flow(dcp) ? DPTX_TUNNEL_CONNECT_TIMEOUT : DPTX_CONNECT_TIMEOUT; - ret = wait_for_completion_timeout(&dcp->dptxport[port].linkcfg_completion, - timeout); + ret = dcp_dptx_wait_linkcfg(dcp, port, timeout, + !compact && !late_hpd && + dcp_watches_tunnel_withdrawal(), + &withdrawn); /* A revoked wait must not disconnect or publish readiness for a new session. */ if (!dcp_connect_session_valid(dcp, session, cable, recovery)) return -ESTALE; - if (!ret && compact) { + if (!ret && withdrawn) { + dev_info(dcp->dev, + "dcp_dptx_connect: port %u: the DP tunnel went before link configuration\n", + port); + ret = -ENOLINK; + goto out_withdraw; + } else if (!ret && compact) { /* Without a sink the link is configured once one arrives. */ dev_dbg(dcp->dev, "dcp_dptx_connect: no link configuration on port %u yet\n", port); From 2865b4e77f81d6b0e90782ee5f3b1f926ec802dc Mon Sep 17 00:00:00 2001 From: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:28:53 -0700 Subject: [PATCH 3/3] selftests: apple-dcp: give fast-replug-bounce the link-config wait dcp_dptx_connect_raw() now waits through dcp_dptx_wait_linkcfg(), so the bounce scenario pastes it too. The scenario is an H17P link: the tunnel-withdrawal watch is off and reaching it is a failure. Signed-off-by: Isaac Duarte <31712685+Isaac-Duarte@users.noreply.github.com> --- .../selftests/drivers/gpu/apple-dcp/lifecycle.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py index 86a95e0f4178e..1e648c4e8e3eb 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py @@ -737,6 +737,16 @@ def swap(old, new): ' assert(!"T8132 dcpext teardown in an H17P scenario"); return -ENODEV; }\n') if 'static unsigned long jiffies;' not in unit: firmware += '\nstatic unsigned long jiffies;\n' + if 'dcp_dptx_wait_linkcfg(' in raw: + # Only T6050 watches the tunnel: this H17P link waits as before. + firmware += ('#define DCP_LINKCFG_POLL_MS 50\n' + '#ifndef min_t\n#define min_t(t, a, b) ((t)(a) < (t)(b) ? (t)(a) : (t)(b))\n#endif\n' + '#ifndef max_t\n#define max_t(t, a, b) ((t)(a) > (t)(b) ? (t)(a) : (t)(b))\n#endif\n' + '#ifndef msecs_to_jiffies\n#define msecs_to_jiffies(x) (x)\n#endif\n' + 'static bool dcp_watches_tunnel_withdrawal(void) { return false; }\n' + 'static bool dcp_tb_tunnel_withdrawn(struct apple_dcp *d, u32 port)\n' + '{ (void)d; (void)port; assert(!"tunnel watch in an H17P scenario"); return false; }\n' + + apple_function(root, 'dcp_dptx_wait_linkcfg')) # unless the update work already needed the clock clock = 'static unsigned jiffies_to_msecs(unsigned long t) { return t; }\n' if clock in unit: