For a GPU reset, dm_suspend() stops all streams with
amdgpu_dm_commit_zero_streams() and toggles the OTG interrupts
directly, and dm_resume() restores the cached DC state the same way.
Neither goes through manage_dm_interrupts(), so DRM is never told that
vblank stops: drm_crtc_vblank_get() keeps succeeding while no vblank
interrupt can arrive until the streams are back.

Since commit ebc3351cc4a9 ("drm/amdgpu: remove suspend/resume DRM
clients during GPU recovery"), the fbdev client keeps running during
recovery, and its damage worker rate-limits display updates with
drm_client_modeset_wait_for_vblank(). Every display update during the
reset then waits for a vblank that never comes, until the wait times
out and warns:

  amdgpu 0000:03:00.0: [drm] vblank wait timed out on crtc 0
  WARNING: drivers/gpu/drm/drm_vblank.c:1320 at 
drm_crtc_wait_one_vblank+0x194/0x200
  Workqueue: events drm_fb_helper_damage_work
  Call Trace:
   drm_client_modeset_wait_for_vblank+0x63/0x80
   drm_fb_helper_damage_work+0x89/0x1a0

FBIO_WAITFORVSYNC runs into the same timeout, and DRM_IOCTL_WAIT_VBLANK
sleeps until its own 3 second timeout.

Do what a regular CRTC disable does: call drm_crtc_vblank_off() for the
CRTCs of the cached DC state before their streams are stopped, and
drm_crtc_vblank_on_config() with their existing configuration once the
streams and interrupts are restored. drm_crtc_vblank_get() then fails
for the duration of the reset, so drm_client_modeset_wait_for_vblank()
doesn't wait at all, and DRM_IOCTL_WAIT_VBLANK waiters are woken up.
Page flips that get to amdgpu_dm_commit_planes() meanwhile have their
event sent right away, see the previous patch.

drm_crtc_vblank_off() also clears the vblank timestamping constants,
which a modeset sets again before it turns vblank back on. Recompute
them from the CRTC state after the reset as well; otherwise the vblank
timestamp query in drm_crtc_vblank_on_config() warns about an
uninitialized mode.

A modeset that waits for dc_lock during the reset may be turning one of
these CRTCs off. Leave vblank off for a CRTC that is no longer enabled;
amdgpu_dm_crtc_set_vblank() would refuse to enable vblank on it anyway.

Neither call can block on the reset: drm_crtc_vblank_off() only takes
spinlocks and flushes the DRM vblank worker, which DM doesn't use, and
amdgpu_dm_crtc_set_vblank() doesn't queue vblank_control_work during a
reset. Unlike suspending the DRM clients, this doesn't make recovery
wait for anything that may itself depend on the reset.

Add KUnit tests for the new helper.

Fixes: ebc3351cc4a9 ("drm/amdgpu: remove suspend/resume DRM clients during GPU 
recovery")
Cc: Harry Wentland <[email protected]>
Cc: Leo Li <[email protected]>
Cc: Christian König <[email protected]>
Cc: Alex Deucher <[email protected]>
Assisted-by: Copilot:claude-opus-5.5
---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |  52 ++++++++
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h |   1 +
 .../display/amdgpu_dm/tests/amdgpu_dm_test.c  | 112 ++++++++++++++++++
 3 files changed, 165 insertions(+)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
index d3882ffcb5d4..007968e892c8 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
@@ -1496,6 +1496,56 @@ STATIC_IFN_KUNIT void 
dm_gpureset_toggle_interrupts(struct amdgpu_device *adev,
 }
 EXPORT_IF_KUNIT(dm_gpureset_toggle_interrupts);
 
+/**
+ * dm_gpureset_toggle_vblank() - Turn DRM vblank off or on around a GPU reset
+ * @adev: amdgpu device
+ * @state: DC state whose streams the reset stops and restores
+ * @enable: false before the streams are stopped, true once they are restored
+ *
+ * The GPU reset path stops and restores the streams through DC directly and
+ * bypasses manage_dm_interrupts(). Turn vblank off in DRM like a CRTC
+ * disable does, so that drm_crtc_vblank_get() fails while no vblank
+ * interrupt can arrive, instead of letting vblank waiters such as the fbdev
+ * damage worker sleep until they time out.
+ */
+STATIC_IFN_KUNIT void dm_gpureset_toggle_vblank(struct amdgpu_device *adev,
+                                               struct dc_state *state, bool 
enable)
+{
+       struct drm_vblank_crtc_config config;
+       struct amdgpu_crtc *acrtc;
+       int i;
+
+       for (i = 0; i < state->stream_count; i++) {
+               int otg_inst = state->stream_status[i].primary_otg_inst;
+
+               acrtc = amdgpu_dm_get_crtc_by_otg_inst(adev, otg_inst);
+               if (!acrtc)
+                       continue;
+
+               if (!enable) {
+                       drm_crtc_vblank_off(&acrtc->base);
+                       continue;
+               }
+
+               /*
+                * A modeset waiting for dc_lock may be turning the CRTC off,
+                * and amdgpu_dm_crtc_set_vblank() refuses to enable vblank on
+                * it then. Leave its vblank off.
+                */
+               if (!acrtc->base.enabled)
+                       continue;
+
+               /* drm_crtc_vblank_off() cleared the timestamping constants. */
+               drm_calc_timestamping_constants(&acrtc->base,
+                                               
&acrtc->base.state->adjusted_mode);
+
+               /* Keep the off delay that manage_dm_interrupts() configured. */
+               config = drm_crtc_vblank_crtc(&acrtc->base)->config;
+               drm_crtc_vblank_on_config(&acrtc->base, &config);
+       }
+}
+EXPORT_IF_KUNIT(dm_gpureset_toggle_vblank);
+
 DEFINE_FREE(state_release, struct dc_state *, dc_state_release(_T))
 
 STATIC_IFN_KUNIT enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc)
@@ -1630,6 +1680,7 @@ static int dm_suspend(struct amdgpu_ip_block *ip_block)
                        return -ENOMEM;
                }
 
+               dm_gpureset_toggle_vblank(adev, dm->cached_dc_state, false);
                dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, false);
 
                res = amdgpu_dm_commit_zero_streams(dm->dc);
@@ -1969,6 +2020,7 @@ static int dm_resume(struct amdgpu_ip_block *ip_block)
                dm_gpureset_commit_state(dm->cached_dc_state, dm);
 
                dm_gpureset_toggle_interrupts(adev, dm->cached_dc_state, true);
+               dm_gpureset_toggle_vblank(adev, dm->cached_dc_state, true);
 
                dc_state_release(dm->cached_dc_state);
                dm->cached_dc_state = NULL;
diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h
index f8503ebf28f4..303e9a57e7b8 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h
@@ -1351,6 +1351,7 @@ void mmhub_read_system_context(struct amdgpu_device *adev,
                               struct dc_phy_addr_space_config *pa_config);
 int amdgpu_dm_init_power_module(struct amdgpu_display_manager *dm);
 void dm_gpureset_toggle_interrupts(struct amdgpu_device *adev, struct dc_state 
*state, bool enable);
+void dm_gpureset_toggle_vblank(struct amdgpu_device *adev, struct dc_state 
*state, bool enable);
 enum dc_status amdgpu_dm_commit_zero_streams(struct dc *dc);
 int dm_cache_state(struct amdgpu_device *adev);
 void dm_destroy_cached_state(struct amdgpu_device *adev);
diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
index ebec1260c510..ed2e65bad674 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c
@@ -5232,6 +5232,113 @@ static void 
dm_test_gpureset_toggle_interrupts_dcn(struct kunit *test)
        dm_gpureset_toggle_interrupts(adev, state, true);
 }
 
+/* Tests for dm_gpureset_toggle_vblank() */
+
+/*
+ * One stream on the OTG of the CRTC from dm_test_irq_mgmt_ctx_alloc(), which
+ * is enabled with a 1080p60 mode and has its vblank set up like on a regular
+ * CRTC enable: timestamping constants first, then manage_dm_interrupts().
+ */
+static struct dc_state *dm_test_gpureset_vblank_state(struct kunit *test,
+                                                     struct 
dm_test_irq_mgmt_ctx *ctx)
+{
+       struct drm_display_mode *mode = &ctx->acrtc_state->base.adjusted_mode;
+       struct dc_state *state = dm_kunit_alloc_dc_state(test);
+
+       mode->crtc_clock = 148500;
+       mode->crtc_htotal = 2200;
+       mode->crtc_vtotal = 1125;
+       ctx->acrtc->base.state = &ctx->acrtc_state->base;
+       ctx->acrtc->base.enabled = true;
+       drm_calc_timestamping_constants(&ctx->acrtc->base, mode);
+       ctx->adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 1, 2);
+       manage_dm_interrupts(ctx->adev, ctx->acrtc, ctx->acrtc_state);
+
+       state->stream_count = 1;
+       state->stream_status[0].primary_otg_inst = ctx->acrtc->otg_inst;
+
+       return state;
+}
+
+/**
+ * dm_test_gpureset_toggle_vblank_off - Test vblank is off while streams are 
down
+ * @test: The KUnit test context
+ *
+ * drm_crtc_vblank_get() has to fail, or vblank waiters such as the fbdev
+ * damage worker sleep until they time out.
+ */
+static void dm_test_gpureset_toggle_vblank_off(struct kunit *test)
+{
+       struct dm_test_irq_mgmt_ctx *ctx = dm_test_irq_mgmt_ctx_alloc(test);
+       struct dc_state *state = dm_test_gpureset_vblank_state(test, ctx);
+       struct drm_crtc *crtc = &ctx->acrtc->base;
+
+       KUNIT_ASSERT_EQ(test, drm_crtc_vblank_get(crtc), 0);
+       drm_crtc_vblank_put(crtc);
+
+       dm_gpureset_toggle_vblank(ctx->adev, state, false);
+
+       KUNIT_EXPECT_FALSE(test, ctx->adev->ddev.vblank[0].enabled);
+       KUNIT_EXPECT_EQ(test, drm_crtc_vblank_get(crtc), -EINVAL);
+}
+
+/**
+ * dm_test_gpureset_toggle_vblank_on - Test vblank comes back with its config 
and mode
+ * @test: The KUnit test context
+ */
+static void dm_test_gpureset_toggle_vblank_on(struct kunit *test)
+{
+       struct dm_test_irq_mgmt_ctx *ctx = dm_test_irq_mgmt_ctx_alloc(test);
+       struct dc_state *state = dm_test_gpureset_vblank_state(test, ctx);
+       struct drm_crtc *crtc = &ctx->acrtc->base;
+
+       dm_gpureset_toggle_vblank(ctx->adev, state, false);
+       dm_gpureset_toggle_vblank(ctx->adev, state, true);
+
+       /* manage_dm_interrupts() derived this off delay from the timing. */
+       KUNIT_EXPECT_EQ(test, ctx->adev->ddev.vblank[0].config.offdelay_ms, 34);
+       /* Vblank timestamps need the mode that drm_crtc_vblank_off() cleared. 
*/
+       KUNIT_EXPECT_EQ(test, ctx->adev->ddev.vblank[0].hwmode.crtc_clock, 
148500);
+       KUNIT_ASSERT_EQ(test, drm_crtc_vblank_get(crtc), 0);
+       drm_crtc_vblank_put(crtc);
+}
+
+/**
+ * dm_test_gpureset_toggle_vblank_no_crtc - Test streams without a CRTC are 
skipped
+ * @test: The KUnit test context
+ */
+static void dm_test_gpureset_toggle_vblank_no_crtc(struct kunit *test)
+{
+       struct dm_test_irq_mgmt_ctx *ctx = dm_test_irq_mgmt_ctx_alloc(test);
+       struct dc_state *state = dm_test_gpureset_vblank_state(test, ctx);
+       struct drm_crtc *crtc = &ctx->acrtc->base;
+
+       state->stream_status[0].primary_otg_inst = ctx->acrtc->otg_inst + 1;
+
+       dm_gpureset_toggle_vblank(ctx->adev, state, false);
+
+       KUNIT_ASSERT_EQ(test, drm_crtc_vblank_get(crtc), 0);
+       drm_crtc_vblank_put(crtc);
+}
+
+/**
+ * dm_test_gpureset_toggle_vblank_crtc_disabled - Test a disabled CRTC stays 
off
+ * @test: The KUnit test context
+ *
+ * A modeset that waits for the reset to finish may be turning the CRTC off.
+ */
+static void dm_test_gpureset_toggle_vblank_crtc_disabled(struct kunit *test)
+{
+       struct dm_test_irq_mgmt_ctx *ctx = dm_test_irq_mgmt_ctx_alloc(test);
+       struct dc_state *state = dm_test_gpureset_vblank_state(test, ctx);
+
+       dm_gpureset_toggle_vblank(ctx->adev, state, false);
+       ctx->acrtc->base.enabled = false;
+       dm_gpureset_toggle_vblank(ctx->adev, state, true);
+
+       KUNIT_EXPECT_EQ(test, drm_crtc_vblank_get(&ctx->acrtc->base), -EINVAL);
+}
+
 static struct kunit_case amdgpu_dm_tests[] = {
        /* Simple DM callbacks */
        KUNIT_CASE(dm_test_wait_for_idle),
@@ -5481,6 +5588,11 @@ static struct kunit_case amdgpu_dm_tests[] = {
        KUNIT_CASE(dm_test_initialize_plane_overlay),
        /* dm_gpureset_toggle_interrupts */
        KUNIT_CASE(dm_test_gpureset_toggle_interrupts_dcn),
+       /* dm_gpureset_toggle_vblank */
+       KUNIT_CASE(dm_test_gpureset_toggle_vblank_off),
+       KUNIT_CASE(dm_test_gpureset_toggle_vblank_on),
+       KUNIT_CASE(dm_test_gpureset_toggle_vblank_no_crtc),
+       KUNIT_CASE(dm_test_gpureset_toggle_vblank_crtc_disabled),
        {}
 };
 
-- 
2.34.1

Reply via email to