From 85d90fe19eedff93a584ffe1eefda10b151df206 Mon Sep 17 00:00:00 2001 From: Bartosz Nitka Date: Tue, 17 Nov 2026 23:26:54 +0100 Subject: [PATCH 1/2] drm: apple: dptxep: let a dcpext take its link down A dcpext's firmware sets its link rate to 0 when its HPD goes low, from within the set_hpd(0) call. In DP alt mode the rate goes to the ATC PHY, whose configure may refuse it (-EOPNOTSUPP) once the PHY has left DP mode, as the T8132 ATC PHY does when the display is released for a deep sleep. The refusal goes back to the firmware, which then waits on its own call and answers nothing else: the set_hpd(0) and the release_display after it time out, and the next connect does too. A link going down on a PHY that is out of DP mode is down already: answer the rate 0 as done, whichever DCP asks. No other PHY driver refuses a rate 0 with -EOPNOTSUPP: they take the link down, or fail with another error that goes back as before. Assisted-by: LLM Signed-off-by: Bartosz Nitka --- drivers/gpu/drm/apple/dptxep.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/gpu/drm/apple/dptxep.c b/drivers/gpu/drm/apple/dptxep.c index c75079dced540..91658b17be883 100644 --- a/drivers/gpu/drm/apple/dptxep.c +++ b/drivers/gpu/drm/apple/dptxep.c @@ -681,6 +681,14 @@ static int dptxport_call_set_link_rate(struct apple_epic_service *service, dptx->phy_ops.dp.link_rate = phy_link_rate; dptx->phy_ops.dp.set_rate = 1; ret = phy_configure(dptx->atcphy, &dptx->phy_ops); + /* + * A dcpext takes its link down (rate 0) when its HPD goes + * low, and its ATC PHY may have left DP mode already: the + * link is down then. Refused, the firmware waits on this + * call and answers nothing else, the HPD low among them. + */ + if (ret == -EOPNOTSUPP && !phy_link_rate) + ret = 0; if (ret) return ret; } From 3e2fe315f8b23e5ea3cb91260baf733a6e9d1ada Mon Sep 17 00:00:00 2001 From: Bartosz Nitka Date: Wed, 18 Nov 2026 15:02:25 +0100 Subject: [PATCH 2/2] drm: apple: t8132: keep an external display across a deep sleep A deep sleep powers a T8132 dcpext down whole (iomfb_m4_ext_stop()), and its firmware starts again from reset after it. Firmware that starts with its DPTX port still connected brings the link up on hardware the sleep reset, and crashes (an SError) with any display attached. So in its deep-sleep suspend, before A500 while its firmware still runs, the display is released as an unplug releases it (HPD low, then dcp_dptx_disconnect()) and its crossbar output is parked. The cable stays connected: after the restart the route is unparked and the type-C work connects the display again, as for a plug. A suspend that fails after the release connects it again at once. A release call that timed out is never answered by the firmware that starts from reset: it is dropped once the firmware is stopped, so that the connect after the wake does not wait for it. DRM does not see that release and reconnect. From the release until the replay of its state at PM_POST_SUSPEND, the connector is held: it stays connected, its modes are those of the link it had (the link's capacity is kept at the release; its rate is 0 meanwhile), and its hotplugs wait. Only an output that took the hold at PM_SUSPEND_PREPARE is held, as its PM_POST_SUSPEND is what ends the hold. The replays wait for their displays to be connected again (each one's D575 and its sink's timing catalog), together and 3 s at most, before any takes the modeset locks, so that each modeset finds its link up. A display unplugged during the sleep ends its wait as soon as that is known (no cable at the reconnect, or the type-C unplug after it). When the replay is done (for an output that was not replayed, when the wait is over), the hotplug held meanwhile goes out. The release's own HPD low and rate 0 are among the hotplugs held, so one goes out after every such sleep, and the compositor finds what the sleep changed: a display gone, also one whose CRTC was off, or a new link or sink. A replay that fails sets the link status BAD and sends the hotplug as well. Assisted-by: LLM Signed-off-by: Bartosz Nitka --- drivers/gpu/drm/apple/dcp-internal.h | 5 + drivers/gpu/drm/apple/dcp.c | 123 ++++++++++++++--- drivers/gpu/drm/apple/iomfb_m4.c | 183 ++++++++++++++++++++++++++ drivers/gpu/drm/apple/iomfb_m4.h | 16 +++ drivers/gpu/drm/apple/t8132_ext_drm.c | 125 +++++++++++++++--- 5 files changed, 416 insertions(+), 36 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index a964be895b6ad..7f3d851ceb511 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -937,6 +937,11 @@ struct apple_dcp { * Activate/Deactivate belong to it until its answer is in. */ bool t8132_release_late; + /* + * T8132 dcpext: its display was released and its route parked for a + * deep sleep (dcp_t8132_sleep_release()), to be connected again after it. + */ + bool t8132_sleep_released; /* DPTX feeds a Thunderbolt DP IN adapter, not the Type-C PHY lanes */ bool dptx_tunnel; /* DFP port in the DPTX target: 0 = dpphy, 1 = dpin0, 2 = dpin1 */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index a95a285af3a85..c26d4863a55b4 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -3765,6 +3765,24 @@ static void dcp_t8132_dptx_teardown_log(struct apple_dcp *dcp, u32 port, le32_to_cpu(READ_ONCE(*ep->txbfr.rptr)), le32_to_cpu(READ_ONCE(*ep->txbfr.wptr))); } +/* + * A T8132 dcpext lowers only the HPD its connect raised. An answer that comes + * after the call timed out is the call's orphan's (see + * dcp_dptx_release_locked()). + */ +static void dcp_t8132_hpd_low(struct apple_dcp *dcp, u32 port) +{ + int ret; + + if (!dptx_service_available(&dcp->dptxport[port]) || + !READ_ONCE(dcp->dptxport[port].connected)) + return; + ret = dptxport_set_hpd(dcp->dptxport[port].service, false); + dcp_t8132_dptx_teardown_log(dcp, port, "set_hpd(0)", ret); + if (ret == -ETIMEDOUT) + WRITE_ONCE(dcp->t8132_release_late, true); +} + static int dcp_dptx_release_locked(struct apple_dcp *dcp, u32 port) { int ret; @@ -3904,6 +3922,12 @@ int dcp_dptx_disconnect_oob(struct platform_device *pdev, u32 port) /* Nothing left to retry for the display that went. */ if (dcp->external_native) cancel_delayed_work(&dcp->external_retry_wq); + /* + * A T8132 dcpext's replay after a deep sleep stops + * waiting for it (see dcp_t8132_sleep_reconnect()). + */ + if (dcp_is_t8132_ext(dcp)) + iomfb_m4_ext_relink_lost(dcp); } WRITE_ONCE(dcp->typec_crtc_off, false); reinit_completion(&dcp->typec_iomfb_hpd_ready); @@ -3930,20 +3954,12 @@ int dcp_dptx_disconnect_oob(struct platform_device *pdev, u32 port) av_service_disconnect(dcp); /* - * A T8132 dcpext lowers only the HPD its connect raised, and, as - * desktop 26.x compact DPTX does, still releases the display when the - * firmware refuses: a refusal must not keep a released tunnel's route. - * An answer that comes after the call timed out is the call's orphan's - * (see dcp_dptx_release_locked()). + * A T8132 dcpext, as desktop 26.x compact DPTX does, still releases the + * display when the firmware refuses its HPD low: a refusal must not keep + * a released tunnel's route. */ if (dcp_is_t8132_ext(dcp)) { - if (dptx_service_available(&dcp->dptxport[port]) && - READ_ONCE(dcp->dptxport[port].connected)) { - ret = dptxport_set_hpd(dcp->dptxport[port].service, false); - dcp_t8132_dptx_teardown_log(dcp, port, "set_hpd(0)", ret); - if (ret == -ETIMEDOUT) - WRITE_ONCE(dcp->t8132_release_late, true); - } + dcp_t8132_hpd_low(dcp, port); } else if (dptx_service_available(&dcp->dptxport[port])) { ret = dptxport_set_hpd(dcp->dptxport[port].service, false); if (ret) @@ -9227,6 +9243,67 @@ bool dcp_s2r_firmware_stopped(struct platform_device *pdev) #include "dcp-v27-sleep.inc" +/* + * A deep sleep powers a T8132 dcpext down whole (iomfb_m4_ext_stop()), and its + * firmware starts again from reset after it. Firmware that starts with its + * DPTX port still connected brings the link up on hardware the sleep reset, + * and crashes (an SError) with any display attached. So, in its deep-sleep + * suspend, before A500 while its firmware still runs, its display is released + * as an unplug releases it, HPD low first, and its crossbar output is parked. + * The cable stays connected: dcp_t8132_sleep_reconnect() connects the display + * again. + */ +static void dcp_t8132_sleep_release(struct apple_dcp *dcp) +{ + if (!dcp_is_t8132_ext(dcp) || pm_suspend_target_state != PM_SUSPEND_MEM) + return; + dcp_disable_typec_work(dcp, false); + /* + * Connected again after the wake: the DRM replay waits for it, its + * connector held from before the HPD low. + */ + if (dptx_service_available(&dcp->dptxport[0]) && + READ_ONCE(dcp->dptxport[0].connected)) + iomfb_m4_ext_relink_expect(dcp); + dcp_t8132_hpd_low(dcp, 0); + /* Released whatever the firmware answers (dcp_dptx_release_locked()). */ + dcp_dptx_disconnect(dcp, 0); + dcp_typec_route_park(dcp); + dcp->t8132_sleep_released = true; +} + +/* After the sleep, or a suspend that failed: the route back, then the link. */ +static void dcp_t8132_sleep_reconnect(struct apple_dcp *dcp) +{ + if (!dcp->t8132_sleep_released) + return; + dcp->t8132_sleep_released = false; + dcp_typec_route_unpark(dcp); + /* + * A display unplugged from now on (or already) is not waited for: an + * unplug clears the cable and reports the display lost under hpd_mutex + * (dcp_dptx_disconnect_oob()), so one of the two sees the other. + */ + scoped_guard(mutex, &dcp->hpd_mutex) + iomfb_m4_ext_relink_arm(dcp, READ_ONCE(dcp->typec_cable_connected)); + /* Queues the connect while the cable is still there. */ + dcp_enable_typec_work(dcp); +} + +/* + * Its firmware stopped for the sleep (iomfb_m4_ext_stop()) starts again from + * reset: the calls of an earlier teardown that timed out are never answered + * now, and its connect after the wake does not wait for them + * (dcp_t8132_wait_teardown_answers()). + */ +static void dcp_t8132_sleep_stopped(struct apple_dcp *dcp) +{ + if (!dcp_is_t8132_ext(dcp) || !dcp->dptxep || !iomfb_m4_ext_stopped(dcp)) + return; + afk_compact_orphans_drop(dcp->dptxep); + WRITE_ONCE(dcp->t8132_release_late, false); +} + static int dcp_platform_suspend(struct device *dev) { struct apple_dcp *dcp = dev_get_drvdata(dev); @@ -9271,11 +9348,17 @@ static int dcp_platform_suspend(struct device *dev) * (iomfb_m4_suspend()). */ if (iomfb_is_m4(dcp)) { - int ret = iomfb_m4_suspend(dcp); + int ret; - if (!ret) - device_set_wakeup_path(dev); - return ret; + dcp_t8132_sleep_release(dcp); + ret = iomfb_m4_suspend(dcp); + if (ret) { + dcp_t8132_sleep_reconnect(dcp); + return ret; + } + dcp_t8132_sleep_stopped(dcp); + device_set_wakeup_path(dev); + return 0; } if (dcp_is_ext26(dcp) && pm_suspend_target_state != PM_SUSPEND_TO_IDLE) { dev_warn(dev, "native26 refuses non-s2idle target %d\n", pm_suspend_target_state); @@ -9382,8 +9465,12 @@ static int dcp_platform_resume(struct device *dev) if (dcp->s2r_stopped) return dcp_s2r_resume(dcp); - if (iomfb_is_m4(dcp)) - return iomfb_m4_resume(dcp); + if (iomfb_is_m4(dcp)) { + int ret = iomfb_m4_resume(dcp); + + dcp_t8132_sleep_reconnect(dcp); + return ret; + } if (READ_ONCE(dcp->ext_dead)) return 0; diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index 3ae666b093ea7..1d61f083ff60f 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -222,6 +222,28 @@ struct iomfb_m4 { bool ext_dart_down; /* ... since a system sleep (no D572(1) since) */ bool ext_dart_since_sleep; + /* + * Its display was released for a deep sleep with the cable left in + * (dcp.c), and is connected again after the wake in the background: + * the DRM replay waits for that (iomfb_m4_ext_relink_wait()). + * Meanwhile, until iomfb_m4_ext_relink_end(), its connector stays as + * it was: connected, its hotplugs held, its modes those of the link it + * had (ext_hold_capacity). + */ + bool ext_relink; + u64 ext_hold_capacity; + /* + * Its reconnect under way after the wake (armed), and the display + * found gone meanwhile (lost): the replay stops waiting for it. Both + * set under the owner's hpd_mutex, as its cable state is. + */ + bool ext_relink_armed, ext_relink_lost; + /* + * From PM_SUSPEND_PREPARE, the DRM layer ends a hold for this sleep + * at PM_POST_SUSPEND (iomfb_m4_ext_relink_own()); without that, no + * hold starts. + */ + bool ext_relink_owned; /* The external PIODMA child, kept once created; ->piodma once usable */ struct platform_device *ext_piodma; /* The display DART's state was found lost after a D572(1) */ @@ -1814,6 +1836,9 @@ static int dcp_m4_external_callback(struct dcp_m4_runtime *r, u32 tag, bool remo WRITE_ONCE(r->dcp->ext_sink, !!connected); t8132_ext_drm_hotplug(r->dcp->dev); } + /* A replay waiting for the link (iomfb_m4_ext_relink_wait()). */ + if (connected) + wake_up_all(&r->dcp->ext_settle_wq); return 0; } @@ -2999,6 +3024,9 @@ int iomfb_m4_ext_detect(struct apple_dcp *owner) if (!dcp || !dcp->external) return -ENODEV; + /* Released for a deep sleep: connected as it was until it is back. */ + if (READ_ONCE(dcp->ext_relink)) + return 1; /* A tunnel whose pixel clock was refused cannot show. */ return iomfb_m4_external_ready(owner) && READ_ONCE(dcp->ext_sink) && !READ_ONCE(owner->tb_clock_refused); @@ -3017,9 +3045,13 @@ int iomfb_m4_ext_detect(struct apple_dcp *owner) /* The DPTX link's capacity in kbit/s at 8b/10b, 0 while not known. */ static u64 dcp_m4_ext_capacity(struct apple_dcp *owner) { + struct iomfb_m4 *dcp = owner->v26_6; u32 lanes = READ_ONCE(owner->dptxport[0].lane_count); u32 rate = READ_ONCE(owner->dptxport[0].link_rate); + /* Released for a deep sleep: the link it had until it is back. */ + if (dcp && READ_ONCE(dcp->ext_relink)) + return READ_ONCE(dcp->ext_hold_capacity); return (u64)lanes * rate * 270 * 1000 * 8 / 10; } @@ -3861,6 +3893,8 @@ static bool dcp_m4_ext_cache_modes(struct iomfb_m4 *dcp, const void *blob, u32 s dcp->ext_pref_count = 0; mutex_unlock(&dcp->ext_modes_lock); kfree(old); + /* A replay waiting for the sink's modes (iomfb_m4_ext_relink_wait()). */ + wake_up_all(&dcp->ext_settle_wq); dev_dbg(dcp->dev, "%u of %u timing(s) with an 8-bpc RGB SDR colour: %d\n", kept, count, ret); return true; @@ -5149,6 +5183,141 @@ void iomfb_m4_ext_sleep_wake(struct apple_dcp *owner) mutex_unlock(&dcp->lock); } +/* + * PM_SUSPEND_PREPARE: the DRM layer ends a hold of this output's connector at + * PM_POST_SUSPEND (iomfb_m4_ext_relink_end()), if this sleep starts one. + */ +void iomfb_m4_ext_relink_own(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + if (dcp && dcp->external) + WRITE_ONCE(dcp->ext_relink_owned, true); +} + +/* + * A dcpext's display released for a deep sleep with the cable left in: it is + * connected again after the wake, by the type-C work, in the background. Its + * connector is held meanwhile only for a DRM layer that ends the hold + * (iomfb_m4_ext_relink_own()). + */ +void iomfb_m4_ext_relink_expect(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + if (!dcp || !dcp->external || !READ_ONCE(dcp->ext_relink_owned)) + return; + /* The link's capacity before it goes down: its modes meanwhile. */ + WRITE_ONCE(dcp->ext_hold_capacity, dcp_m4_ext_capacity(owner)); + WRITE_ONCE(dcp->ext_relink_armed, false); + WRITE_ONCE(dcp->ext_relink_lost, false); + WRITE_ONCE(dcp->ext_relink, true); +} + +/* + * After the wake, its reconnect queued, under the owner's hpd_mutex with + * @cable read under it: from now on, the display found gone (the cable + * already gone, or iomfb_m4_ext_relink_lost()) ends the replay's wait for it. + */ +void iomfb_m4_ext_relink_arm(struct apple_dcp *owner, bool cable) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + lockdep_assert_held(&owner->hpd_mutex); + if (!dcp || !dcp->external || !READ_ONCE(dcp->ext_relink)) + return; + WRITE_ONCE(dcp->ext_relink_armed, true); + if (!cable) { + WRITE_ONCE(dcp->ext_relink_lost, true); + wake_up_all(&dcp->ext_settle_wq); + } +} + +/* + * Its display unplugged after the wake, under the owner's hpd_mutex as its + * cable goes: nothing comes back to wait for. + */ +void iomfb_m4_ext_relink_lost(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + lockdep_assert_held(&owner->hpd_mutex); + if (!dcp || !dcp->external || !READ_ONCE(dcp->ext_relink) || + !READ_ONCE(dcp->ext_relink_armed)) + return; + WRITE_ONCE(dcp->ext_relink_lost, true); + wake_up_all(&dcp->ext_settle_wq); +} + +/* Whether its connector is held for a reconnect after a deep sleep. */ +bool iomfb_m4_ext_relinking(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + + return dcp && dcp->external && READ_ONCE(dcp->ext_relink); +} + +/* + * Its display connected again, the sink's modes in; or found gone; or the + * session failed. + */ +static bool dcp_m4_ext_relinked(struct iomfb_m4 *dcp) +{ + /* Pairs with the release store of dcp->runtime in startup. */ + struct dcp_m4_runtime *r = smp_load_acquire(&dcp->runtime); + + if (READ_ONCE(dcp->error) || READ_ONCE(dcp->ext_relink_lost)) + return true; + /* Pairs with the release store of r->done in startup. */ + return r && smp_load_acquire(&r->done) && READ_ONCE(r->ext_connected) && + READ_ONCE(dcp->ext_sink) && READ_ONCE(dcp->ext_catalog_open); +} + +/* + * PM_POST_SUSPEND, before the DRM replay: after iomfb_m4_ext_relink_expect(), + * the display connected again (until @deadline at most, in jiffies), so that + * the replay's enable finds a link instead of failing on one not up yet (and + * leaving the compositor a hotplug to set the mode again); or found gone + * (unplugged during the sleep), at once; or the session failed. Its + * connector stays held: iomfb_m4_ext_relink_end() ends that. + */ +void iomfb_m4_ext_relink_wait(struct apple_dcp *owner, unsigned long deadline) +{ + struct iomfb_m4 *dcp = owner->v26_6; + long left = (long)(deadline - jiffies); + + if (!dcp || !dcp->external || !READ_ONCE(dcp->ext_relink) || left <= 0) + return; + wait_event_timeout(dcp->ext_settle_wq, dcp_m4_ext_relinked(dcp), left); +} + +/* + * PM_POST_SUSPEND after iomfb_m4_ext_relink_wait(), or a sleep refused at + * PM_SUSPEND_PREPARE, or the DRM device going: the hold of its connector ends, + * and the DRM layer's part in one with it. 0 if none was kept or the display + * is back; -ENOLINK if it was found gone; the session's error if it failed; + * -ETIMEDOUT if it is not back otherwise. The DRM layer sends the hotplug + * held meanwhile once its replay is done (t8132_ext_drm.c). + */ +int iomfb_m4_ext_relink_end(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + int ret; + + if (!dcp || !dcp->external) + return 0; + WRITE_ONCE(dcp->ext_relink_owned, false); + if (!READ_ONCE(dcp->ext_relink)) + return 0; + WRITE_ONCE(dcp->ext_relink, false); + if (READ_ONCE(dcp->ext_relink_lost)) + return -ENOLINK; + ret = READ_ONCE(dcp->error); + if (ret) + return ret; + return dcp_m4_ext_relinked(dcp) ? 0 : -ETIMEDOUT; +} + /* A replay ends unchecked (it failed, or nothing to replay). */ void iomfb_m4_ext_restore_end(struct apple_dcp *owner) { @@ -5463,6 +5632,20 @@ static int iomfb_m4_ext_start(struct iomfb_m4 *dcp) return 0; } +/* An external DCP stopped for a deep sleep, its CPU to start from reset after it. */ +bool iomfb_m4_ext_stopped(struct apple_dcp *owner) +{ + struct iomfb_m4 *dcp = owner->v26_6; + bool stopped; + + if (!dcp || !dcp->external) + return false; + mutex_lock(&dcp->lock); + stopped = dcp->ext_stopped; + mutex_unlock(&dcp->lock); + return stopped; +} + /* apple-drm, a consumer of this device, has blanked the panel before this. */ int iomfb_m4_suspend(struct apple_dcp *owner) { diff --git a/drivers/gpu/drm/apple/iomfb_m4.h b/drivers/gpu/drm/apple/iomfb_m4.h index c45d737b1d726..2ec7f0eeeb723 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.h +++ b/drivers/gpu/drm/apple/iomfb_m4.h @@ -70,6 +70,14 @@ void iomfb_m4_ext_sleep_wake(struct apple_dcp *dcp); bool iomfb_m4_ext_stale(struct apple_dcp *dcp); int iomfb_m4_ext_restore_result(struct apple_dcp *dcp, unsigned int timeout_ms); void iomfb_m4_ext_restore_end(struct apple_dcp *dcp); +void iomfb_m4_ext_relink_own(struct apple_dcp *dcp); +void iomfb_m4_ext_relink_expect(struct apple_dcp *dcp); +bool iomfb_m4_ext_relinking(struct apple_dcp *dcp); +void iomfb_m4_ext_relink_arm(struct apple_dcp *dcp, bool cable); +void iomfb_m4_ext_relink_lost(struct apple_dcp *dcp); +void iomfb_m4_ext_relink_wait(struct apple_dcp *dcp, unsigned long deadline); +int iomfb_m4_ext_relink_end(struct apple_dcp *dcp); +bool iomfb_m4_ext_stopped(struct apple_dcp *dcp); /* The caller has waited for the producer fence before publishing the DMA GEM. */ int iomfb_m4_present(struct apple_dcp *dcp, struct drm_framebuffer *fb, @@ -140,6 +148,14 @@ static inline int iomfb_m4_ext_restore_result(struct apple_dcp *dcp, unsigned in } static inline void iomfb_m4_ext_restore_end(struct apple_dcp *dcp) { } +static inline void iomfb_m4_ext_relink_own(struct apple_dcp *dcp) { } +static inline void iomfb_m4_ext_relink_expect(struct apple_dcp *dcp) { } +static inline bool iomfb_m4_ext_relinking(struct apple_dcp *dcp) { return false; } +static inline void iomfb_m4_ext_relink_arm(struct apple_dcp *dcp, bool cable) { } +static inline void iomfb_m4_ext_relink_lost(struct apple_dcp *dcp) { } +static inline void iomfb_m4_ext_relink_wait(struct apple_dcp *dcp, unsigned long deadline) { } +static inline int iomfb_m4_ext_relink_end(struct apple_dcp *dcp) { return 0; } +static inline bool iomfb_m4_ext_stopped(struct apple_dcp *dcp) { return false; } static inline int iomfb_m4_present(struct apple_dcp *dcp, struct drm_framebuffer *fb, dma_addr_t dva, size_t mapped) diff --git a/drivers/gpu/drm/apple/t8132_ext_drm.c b/drivers/gpu/drm/apple/t8132_ext_drm.c index 911f011ae400b..c1fef32983069 100644 --- a/drivers/gpu/drm/apple/t8132_ext_drm.c +++ b/drivers/gpu/drm/apple/t8132_ext_drm.c @@ -92,6 +92,11 @@ struct t8132_ext_out { * (t8132_ext_lock to set; read anywhere) */ bool dying; + /* + * A hotplug came while the connector was held for a display released + * for a deep sleep: sent when the hold ends (t8132_ext_lock) + */ + bool hotplug_held; bool first_frame; /* A frame was not shown, so the next one goes whole */ bool frame_lost; @@ -763,10 +768,24 @@ void t8132_ext_drm_hotplug(struct device *dcpext) unsigned long flags; spin_lock_irqsave(&t8132_ext_lock, flags); - for (unsigned int i = 0; i < T8132_EXT_MAX; i++) - if (t8132_ext_live[i] && &t8132_ext_live[i]->pdev->dev == dcpext && - !t8132_ext_live[i]->dying) - schedule_work(&t8132_ext_live[i]->hotplug_work); + for (unsigned int i = 0; i < T8132_EXT_MAX; i++) { + struct t8132_ext_out *out = t8132_ext_live[i]; + struct apple_dcp *owner; + + if (!out || &out->pdev->dev != dcpext || out->dying) + continue; + /* + * Released for a deep sleep and connected again after it: the + * connector stays as it was meanwhile, and its hotplug waits for + * the end of the hold (t8132_ext_hold_end()). + */ + owner = t8132_ext_owner(out); + if (owner && iomfb_m4_ext_relinking(owner)) { + out->hotplug_held = true; + continue; + } + schedule_work(&out->hotplug_work); + } spin_unlock_irqrestore(&t8132_ext_lock, flags); } @@ -797,7 +816,8 @@ static int t8132_ext_wait_commit(struct drm_crtc *crtc, unsigned long timeout) * the output enters the sleep (its frames refused, its enables and disables * no-ops), then its display goes down with every worker running * (iomfb_m4_ext_sleep_down()). A commit after this one finds the output - * asleep; none can be under way inside it. + * asleep; none can be under way inside it. A hold of its connector for a + * display released for the sleep (dcp.c) is this layer's to end from now on. */ static int t8132_ext_sleep_prepare(struct t8132_ext_out *out) { @@ -822,9 +842,51 @@ static int t8132_ext_sleep_prepare(struct t8132_ext_out *out) out->sleep_entered = entered; if (entered) iomfb_m4_ext_sleep_down(owner); + iomfb_m4_ext_relink_own(owner); return 0; } +/* How long the replays wait, together, for displays released for the sleep (dcp.c) */ +#define T8132_EXT_RELINK_MS 3000 + +/* + * The hold of a display released for the sleep is over, and its replay, if + * any, done: the hotplug held meanwhile goes out, or @hotplug's. The + * release's own HPD low and link rate 0 are among those held, so one goes + * out after every such sleep: the compositor probes the connector again and + * finds what the sleep changed, if anything (a display gone, also from a + * CRTC that is off, or a new link or sink). + */ +static void t8132_ext_hold_end(struct t8132_ext_out *out, bool hotplug) +{ + unsigned long flags; + + spin_lock_irqsave(&t8132_ext_lock, flags); + if ((hotplug || out->hotplug_held) && !out->dying) + schedule_work(&out->hotplug_work); + out->hotplug_held = false; + spin_unlock_irqrestore(&t8132_ext_lock, flags); +} + +/* A display released for the sleep connected again, until @deadline at most. */ +static void t8132_ext_relink_wait(struct t8132_ext_out *out, unsigned long deadline) +{ + struct apple_dcp *owner = t8132_ext_owner(out); + + if (owner) + iomfb_m4_ext_relink_wait(owner, deadline); +} + +/* Not replayed: the hold of a display released for the sleep ends. */ +static void t8132_ext_relink_end(struct t8132_ext_out *out) +{ + struct apple_dcp *owner = t8132_ext_owner(out); + + if (owner) + iomfb_m4_ext_relink_end(owner); + t8132_ext_hold_end(out, false); +} + /* * PM_POST_SUSPEND (the tasks thawed, every device resumed, the display * DARTs included; also after an aborted sleep): the @@ -837,7 +899,9 @@ static int t8132_ext_sleep_prepare(struct t8132_ext_out *out) * (then read under its lock), or that is not confirmed afterwards (the * display DART and a frame shown, iomfb_m4_ext_restore_result()) sets the * connector's link status BAD and sends a hotplug event: the compositor has - * to set the mode again. + * to set the mode again. A display released for the sleep was waited for + * before (t8132_ext_pm_notify()); its connector's hold ends here, and the + * hotplug held meanwhile goes out at the end. */ static void t8132_ext_sleep_restore(struct t8132_ext_out *out) { @@ -846,12 +910,17 @@ static void t8132_ext_sleep_restore(struct t8132_ext_out *out) struct drm_modeset_acquire_ctx ctx; struct drm_atomic_commit *state; struct drm_crtc_state *cs; - bool active = false, known = false; + bool active = false, known = false, bad; int ret, restore = 0; out->sleep_entered = false; if (!owner) return; + ret = iomfb_m4_ext_relink_end(owner); + if (ret) + dev_info(&out->pdev->dev, "resume: %s: the display not connected again (%s): %d\n", + out->crtc.name, ret == -ENOLINK ? "unplugged" : + ret == -ETIMEDOUT ? "timed out" : "session failed", ret); drm_modeset_acquire_init(&ctx, 0); state = drm_atomic_commit_alloc(drm); if (!state) { @@ -902,13 +971,25 @@ static void t8132_ext_sleep_restore(struct t8132_ext_out *out) restore = iomfb_m4_ext_restore_result(owner, 1000); else iomfb_m4_ext_restore_end(owner); - if (active && (ret || restore || iomfb_m4_ext_stale(owner))) { + bad = active && (ret || restore || iomfb_m4_ext_stale(owner)); + if (bad) { drm_connector_set_link_status_property(&out->connector, DRM_MODE_LINK_STATUS_BAD); dev_warn(&out->pdev->dev, "resume: %s not restored (replay %d, restore %d): link status BAD, hotplug sent\n", out->crtc.name, ret, restore); - if (!READ_ONCE(out->dying)) - schedule_work(&out->hotplug_work); } + t8132_ext_hold_end(out, bad); +} + +/* + * The sleep is over for @out: back if it entered it; if not, its connector's + * hold, if any, ends, and it reports whatever it has now. + */ +static void t8132_ext_sleep_end(struct t8132_ext_out *out) +{ + if (out->sleep_entered) + t8132_ext_sleep_restore(out); + else + t8132_ext_relink_end(out); } /* @@ -918,6 +999,7 @@ static void t8132_ext_sleep_restore(struct t8132_ext_out *out) */ static int t8132_ext_pm_notify(struct notifier_block *nb, unsigned long event, void *unused) { + unsigned long deadline; unsigned int i; int ret = 0; @@ -926,16 +1008,21 @@ static int t8132_ext_pm_notify(struct notifier_block *nb, unsigned long event, v for (i = 0; i < t8132_ext_nouts && !ret; i++) ret = t8132_ext_sleep_prepare(t8132_ext_outs[i]); for (i = 0; ret && i < t8132_ext_nouts; i++) - if (t8132_ext_outs[i]->sleep_entered) - t8132_ext_sleep_restore(t8132_ext_outs[i]); + t8132_ext_sleep_end(t8132_ext_outs[i]); mutex_unlock(&t8132_ext_reg_lock); return notifier_from_errno(ret); } - /* An output that entered the sleep comes back. */ - if (event == PM_POST_SUSPEND) + /* + * Displays released for the sleep are connected again side by side: + * waited for together, against one deadline, before any replay. + */ + if (event == PM_POST_SUSPEND) { + deadline = jiffies + msecs_to_jiffies(T8132_EXT_RELINK_MS); for (i = 0; i < t8132_ext_nouts; i++) - if (t8132_ext_outs[i]->sleep_entered) - t8132_ext_sleep_restore(t8132_ext_outs[i]); + t8132_ext_relink_wait(t8132_ext_outs[i], deadline); + for (i = 0; i < t8132_ext_nouts; i++) + t8132_ext_sleep_end(t8132_ext_outs[i]); + } mutex_unlock(&t8132_ext_reg_lock); return NOTIFY_DONE; } @@ -949,8 +1036,8 @@ static struct notifier_block t8132_ext_pm_nb = { * before drm_dev_unregister() and any DRM object is torn down: the PM * notifier unregistered (a call of it under way finishes first), then every * output dying, its work cancelled, and one left in a sleep (no - * PM_POST_SUSPEND will come) out of it. Only for the DRM device whose - * outputs these are; called again, nothing. + * PM_POST_SUSPEND will come) out of it, its connector's hold ended. Only for + * the DRM device whose outputs these are; called again, nothing. */ void t8132_ext_drm_fini(struct drm_device *drm) { @@ -991,6 +1078,8 @@ void t8132_ext_drm_fini(struct drm_device *drm) iomfb_m4_ext_sleep_wake(owner); iomfb_m4_ext_restore_end(owner); } + if (owner) + iomfb_m4_ext_relink_end(owner); out->sleep_entered = false; } }