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); 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. */ 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: