From a5af61d67c3ed25973c775b79a68db39c336d353 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sat, 28 Nov 2026 12:38:54 +0100 Subject: [PATCH] drm/apple: invalidate mode state on disconnected HPD event disconnected_hpd_event() clears connector->connected and schedules hotplug_wq to notify userspace of an unplug, but previously left dcp->mode_state.valid set until the firmware finished tearing down the link and delivered cb_hotplug(0) tens of milliseconds later. On firmware-27 external pipes, userspace can disable the CRTC from that hotplug notification during the window where mode_state.valid is still true. iomfb_poweroff() then bypasses its !mode_state.valid guard and submits a clear swap into an external pipe the firmware has already powered down (fControllerPowerState == 0). The firmware swallows that clear swap without completing it, leaving present_state_h17p.pending stuck until setPowerState(0) times out and iomfb_queue_timeout() latches dcp->crashed = true, wedging that external DCP until reboot. Call dcp_mode_invalidate(&dcp->mode_state) in disconnected_hpd_event() before notifying userspace so CRTC power-off leaves an already-unplugged pipe to the firmware while preserving DPMS-off pipe stop when the cable remains connected. Add KUnit coverage in apple-display-hotplug for both idle and in-flight modesets. Fixes: ad937dcfb227 ("drm/apple: leave an external pipe without a valid mode to the firmware") Signed-off-by: Thomas Glanzmann --- drivers/gpu/drm/apple/dcp-internal.h | 1 + drivers/gpu/drm/apple/dcp.c | 12 ++++++++-- drivers/gpu/drm/apple/hotplug-test.c | 34 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 81de9acc87461..2bec104e06138 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1444,6 +1444,7 @@ void dcp_platform_shutdown(struct platform_device *pdev); void dcp_disable_typec_work(struct apple_dcp *dcp, bool release_cable); void dcp_enable_typec_work(struct apple_dcp *dcp); void dcp_swap_watchdog(struct work_struct *work); +void disconnected_hpd_event(struct apple_connector *con); void iomfb_scanout_put_h17p(struct apple_dcp *dcp, struct iomfb_scanout_h17p *scanout); #endif diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 6abd5cc9fda60..b4226799366ba 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -852,7 +852,7 @@ static int dcp_desktop_shmem_setup(struct apple_dcp *dcp, return 0; } -static void disconnected_hpd_event(struct apple_connector *connector); +VISIBLE_IF_KUNIT void disconnected_hpd_event(struct apple_connector *con); bool dcp_is_external_h17p(struct apple_dcp *dcp) { @@ -4181,7 +4181,7 @@ static void dcp_typec_relink_work(struct work_struct *work) spin_unlock_irqrestore(&dcp->relink_lock, flags); } -static void disconnected_hpd_event(struct apple_connector *con) +VISIBLE_IF_KUNIT void disconnected_hpd_event(struct apple_connector *con) { struct platform_device *pdev; @@ -4192,6 +4192,14 @@ static void disconnected_hpd_event(struct apple_connector *con) if (pdev) { struct apple_dcp *dcp = platform_get_drvdata(pdev); + /* + * Userspace can disable the CRTC from the hotplug notification + * below before the firmware finishes tearing down the link and + * reports cb_hotplug(0). Invalidate the mode now so the CRTC + * power-off leaves the unplugged pipe to the firmware instead + * of submitting a clear swap that gets swallowed. + */ + dcp_mode_invalidate(&dcp->mode_state); WRITE_ONCE(dcp->ext_backlight, false); /* Nothing else reports an HDMI unplug to the firmware metadata. */ if (dcp_sink_metadata(dcp)) diff --git a/drivers/gpu/drm/apple/hotplug-test.c b/drivers/gpu/drm/apple/hotplug-test.c index 15ca6291bf495..5df27fc9cc271 100644 --- a/drivers/gpu/drm/apple/hotplug-test.c +++ b/drivers/gpu/drm/apple/hotplug-test.c @@ -273,11 +273,45 @@ static void hotplug_coalesced_ready(struct kunit *test) KUNIT_EXPECT_EQ(test, fixture->notifications, 2U); } +static void hotplug_disconnected_hpd_invalidates_mode(struct kunit *test) +{ + struct hotplug_fixture *fixture = test->priv; + + fixture->connector.base.status = connector_status_connected; + fixture->connector.connected = true; + fixture->dcp.mode_state.valid = true; + /* Hold hotplug_wq so we inspect state before userspace is notified. */ + disable_work_sync(&fixture->connector.hotplug_wq); + disconnected_hpd_event(&fixture->connector); + KUNIT_EXPECT_FALSE(test, READ_ONCE(fixture->connector.connected)); + KUNIT_EXPECT_FALSE(test, READ_ONCE(fixture->dcp.mode_state.valid)); + enable_work(&fixture->connector.hotplug_wq); + schedule_work(&fixture->connector.hotplug_wq); + flush_work(&fixture->connector.hotplug_wq); + hotplug_expect_notification(test, connector_status_disconnected); +} + +static void hotplug_disconnected_hpd_invalidates_inflight_modeset(struct kunit *test) +{ + struct hotplug_fixture *fixture = test->priv; + + fixture->connector.base.status = connector_status_connected; + fixture->connector.connected = true; + dcp_mode_begin(&fixture->dcp.mode_state); + disconnected_hpd_event(&fixture->connector); + dcp_mode_finish(&fixture->dcp.mode_state, true, &fixture->connector.connected); + KUNIT_EXPECT_FALSE(test, READ_ONCE(fixture->dcp.mode_state.valid)); + flush_work(&fixture->connector.hotplug_wq); + hotplug_expect_notification(test, connector_status_disconnected); +} + static struct kunit_case hotplug_test_cases[] = { KUNIT_CASE(hotplug_route_failure_notify), KUNIT_CASE(hotplug_coalesced_ready), KUNIT_CASE(hotplug_connected), KUNIT_CASE(hotplug_disconnected), + KUNIT_CASE(hotplug_disconnected_hpd_invalidates_mode), + KUNIT_CASE(hotplug_disconnected_hpd_invalidates_inflight_modeset), KUNIT_CASE(hotplug_retrain), KUNIT_CASE(hotplug_unrouted), KUNIT_CASE(hotplug_forced_on),