Skip to contentSkip to conversation

drm/amd/display: send the flip event right away if vblank is off

PATCH New Prike Liang 1 participant 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amd/display: send the flip event right away if vblank is off

dm_arm_vblank_event_pre_programming() takes a vblank reference for a page flip or cursor update that carries an event, and the handler that later sends the event drops that reference again. The return value of drm_crtc_vblank_get() is ignored, though. If vblank is off, the event is armed anyway, and the handler that eventually sends it drops a reference that was never taken, which unbalances the vblank refcount.

That can't happen today: DM only turns vblank off for a CRTC that it disables, and a disabled CRTC has no active planes. The next patch also turns vblank off while a GPU reset has the streams stopped, though. Page flips that don't modify the DC state skip amdgpu_dm_commit_streams() and only take dc_lock after this point, so they still get here while the reset holds it.

Send the event right away when no vblank reference can be taken, like drm_crtc_vblank_off() does for queued vblank events. No vblank interrupt could deliver it in that case anyway.

Add a KUnit test for it.

Cc Harry Wentland <[email protected]>
Cc Leo Li <[email protected]>
Assisted-by Copilot:claude-opus-5.5
---
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 15 ++++++++--
 .../display/amdgpu_dm/tests/amdgpu_dm_test.c  | 29 +++++++++++++++++++
 2 files changed, 42 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +13 -2
index b0310612425f..9cee9104112c 100644
@@ -3924,6 +3924,11 @@ EXPORT_IF_KUNIT(dm_arm_vblank_event);
  * Grab a reference on the vblank counter if a page flip or cursor update is to
  * be programmed. Do this before programming so the HW is not in any
  * idle-optimized state (such as PSR).
+ *
+ * If vblank is off, no vblank interrupt can deliver the event, and the
+ * handlers that send it would drop a reference that was never taken. Send
+ * the event right away instead, like drm_crtc_vblank_off() does for queued
+ * vblank events.
  */
 STATIC_IFN_KUNIT void dm_arm_vblank_event_pre_programming(struct amdgpu_crtc *acrtc,
 							  struct dm_crtc_state *acrtc_state,
@@ -3935,8 +3940,14 @@ STATIC_IFN_KUNIT void dm_arm_vblank_event_pre_programming(struct amdgpu_crtc *ac
 	if (!acrtc->base.state->event || acrtc_state->active_planes == 0)
 		return;
 
-	if (pflip_update || cursor_update)
-		drm_crtc_vblank_get(&acrtc->base);
+	if (!pflip_update && !cursor_update)
+		return;
+
+	if (drm_crtc_vblank_get(&acrtc->base)) {
+		drm_crtc_send_vblank_event(&acrtc->base,
+					   acrtc->base.state->event);
+		acrtc->base.state->event = NULL;
+	}
 }
 EXPORT_IF_KUNIT(dm_arm_vblank_event_pre_programming);
 
drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c +29 -0
index 28ea2f8fbaf6..317e1f13655c 100644
@@ -1819,6 +1819,34 @@ static void dm_test_arm_vblank_pre_programming_update(struct kunit *test)
 	drm_crtc_vblank_put(&ctx->acrtc->base);
 }
 
+/**
+ * dm_test_arm_vblank_pre_programming_vblank_off - Test the event is sent with vblank off
+ * @test: The KUnit test context
+ *
+ * No vblank interrupt can deliver the event, and the handlers that send it
+ * must not drop a vblank reference that was never taken.
+ */
+static void dm_test_arm_vblank_pre_programming_vblank_off(struct kunit *test)
+{
+	struct dm_test_vblank_ctx *ctx = dm_test_vblank_ctx_alloc(test);
+	struct drm_vblank_crtc *vblank;
+
+	KUNIT_ASSERT_EQ(test, drm_vblank_init(&ctx->adev->ddev, 1), 0);
+	vblank = drm_crtc_vblank_crtc(&ctx->acrtc->base);
+	drm_crtc_vblank_off(&ctx->acrtc->base);
+
+	/* Sending an event that has no file frees it. */
+	ctx->acrtc_state->base.event = kzalloc_obj(*ctx->event, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ctx->acrtc_state->base.event);
+
+	dm_test_arm_vblank_pre_programming(ctx, true, false);
+
+	KUNIT_EXPECT_NULL(test, ctx->acrtc_state->base.event);
+	/* Only drm_crtc_vblank_off() holds a reference. */
+	KUNIT_EXPECT_EQ(test, atomic_read(&vblank->refcount), 1);
+	kfree(ctx->acrtc_state->base.event);
+}
+
 /**
  * dm_test_arm_vblank_event_no_event - Test a commit without an event is a no-op
  * @test: The KUnit test context
@@ -5555,6 +5583,7 @@ static struct kunit_case amdgpu_dm_tests[] = {
 	KUNIT_CASE(dm_test_arm_vblank_pre_programming_no_event),
 	KUNIT_CASE(dm_test_arm_vblank_pre_programming_no_planes),
 	KUNIT_CASE(dm_test_arm_vblank_pre_programming_update),
+	KUNIT_CASE(dm_test_arm_vblank_pre_programming_vblank_off),
 	/* dm_update_pflip_irq_state */
 	KUNIT_CASE(dm_test_update_pflip_irq_state_dcn),
 	KUNIT_CASE(dm_test_update_pflip_irq_state_dce),
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amd/display: turn vblank off in DRM across GPU reset

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. Save the timestamping mode before vblank is turned off and set the constants from it again after the reset; otherwise the vblank timestamp query in drm_crtc_vblank_on_config() warns about an uninitialized mode. The CRTC state can't be used for this: commits swap it without dc_lock, and a commit that never takes dc_lock can free the old state during the reset.

Commits call manage_dm_interrupts() without dc_lock so far, so they could turn vblank off or on between dm_suspend() and dm_resume(). Call it under dc_lock, which the reset holds for that whole time. A commit that already has vblank off when the reset starts turns it on again itself, if at all, so leave such a CRTC alone and only turn vblank back on for the CRTCs that the reset turned it off for.

A modeset that waits for dc_lock during the reset may also 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.

dm_resume() doesn't run if amdgpu_dm_commit_zero_streams() fails in dm_suspend(), so turn the interrupts and vblank back on there.

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() queues vblank_control_work without waiting for it. 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 |  91 +++++++++++-
 .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h |   5 +
 .../display/amdgpu_dm/tests/amdgpu_dm_test.c  | 138 ++++++++++++++++++
 3 files changed, 232 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +89 -2
index 9cee9104112c..d0d712352a2a 100644
@@ -1599,6 +1599,84 @@ 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.
+ *
+ * The caller holds dc_lock from the off call to the on call, and commits call
+ * manage_dm_interrupts() under dc_lock, so only this function changes vblank
+ * in between. It only touches CRTCs whose vblank is on: where a commit has
+ * turned vblank off, that commit also turns it on again, if at all.
+ */
+STATIC_IFN_KUNIT void dm_gpureset_toggle_vblank(struct amdgpu_device *adev,
+						struct dc_state *state, bool enable)
+{
+	struct amdgpu_display_manager *dm = &adev->dm;
+	struct drm_vblank_crtc_config config;
+	struct drm_vblank_crtc *vblank;
+	struct drm_display_mode *mode;
+	struct amdgpu_crtc *acrtc;
+	struct drm_crtc *crtc;
+	int i;
+
+	if (!enable) {
+		dm->reset_vblank_crtcs = 0;
+
+		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;
+
+			crtc = &acrtc->base;
+			vblank = drm_crtc_vblank_crtc(crtc);
+			if (vblank->inmodeset)
+				continue;
+
+			/* drm_crtc_vblank_off() clears the timestamping constants. */
+			mode = &dm->reset_vblank_modes[drm_crtc_index(crtc)];
+			drm_mode_copy(mode, &vblank->hwmode);
+			dm->reset_vblank_crtcs |= drm_crtc_mask(crtc);
+
+			drm_crtc_vblank_off(crtc);
+		}
+
+		return;
+	}
+
+	drm_for_each_crtc(crtc, adev_to_drm(adev)) {
+		if (!(dm->reset_vblank_crtcs & drm_crtc_mask(crtc)))
+			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 (!crtc->enabled)
+			continue;
+
+		mode = &dm->reset_vblank_modes[drm_crtc_index(crtc)];
+		drm_calc_timestamping_constants(crtc, mode);
+
+		/* Keep the off delay that manage_dm_interrupts() configured. */
+		config = drm_crtc_vblank_crtc(crtc)->config;
+		drm_crtc_vblank_on_config(crtc, &config);
+	}
+
+	dm->reset_vblank_crtcs = 0;
+}
+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)
@@ -1733,11 +1811,15 @@ 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);
 		if (res != DC_OK) {
 			drm_err(adev_to_drm(adev), "Failed to commit zero streams: %d\n", res);
+			/* dm_resume() doesn't run after an error, undo the above */
+			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;
 			mutex_unlock(&dm->dc_lock);
@@ -2072,6 +2154,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;
@@ -4871,7 +4954,9 @@ static void amdgpu_dm_commit_streams(struct drm_atomic_commit *state,
 		if (old_crtc_state->active &&
 		    (!new_crtc_state->active ||
 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
-			manage_dm_interrupts(adev, acrtc, NULL);
+			/* Under dc_lock, see dm_gpureset_toggle_vblank() */
+			scoped_guard(mutex, &dm->dc_lock)
+				manage_dm_interrupts(adev, acrtc, NULL);
 			/*
 			 * ISM hysteresis lives on system_dfl_wq, not the
 			 * vblank workqueue. Wait it out so a timer armed while
@@ -5567,7 +5652,9 @@ static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_commit *state)
 		     drm_atomic_crtc_needs_modeset(new_crtc_state))) {
 			dc_stream_retain(dm_new_crtc_state->stream);
 			acrtc->dm_irq_params.stream = dm_new_crtc_state->stream;
-			manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
+			/* Under dc_lock, see dm_gpureset_toggle_vblank() */
+			scoped_guard(mutex, &dm->dc_lock)
+				manage_dm_interrupts(adev, acrtc, dm_new_crtc_state);
 		}
 		/* Handle vrr on->off / off->on transitions */
 		amdgpu_dm_handle_vrr_transition(dm, dm_old_crtc_state, dm_new_crtc_state);
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.h +5 -0
index 971ac1804c03..afc1e9781f55 100644
@@ -362,6 +362,8 @@ struct dm_otg_sw_counter {
  * @soc_bounding_box: SOC bounding box values provided by gpu_info FW
  * @cached_state: Caches device atomic state for suspend/resume
  * @cached_dc_state: Cached state of content streams
+ * @reset_vblank_crtcs: Mask of the CRTCs whose vblank a GPU reset turned off
+ * @reset_vblank_modes: Vblank timestamping modes of those CRTCs, by CRTC index
  * @compressor: Frame buffer compression buffer. See &struct dm_compressor_info
  * @force_timing_sync: set via debugfs. When set, indicates that all connected
  *		       displays will be forced to synchronize.
@@ -663,6 +665,8 @@ struct amdgpu_display_manager {
 
 	struct drm_atomic_commit *cached_state;
 	struct dc_state *cached_dc_state;
+	u32 reset_vblank_crtcs;
+	struct drm_display_mode reset_vblank_modes[AMDGPU_MAX_CRTCS];
 
 	struct dm_compressor_info compressor;
 
@@ -1406,6 +1410,7 @@ void amdgpu_dm_fill_power_nits_range(const struct amdgpu_dm_backlight_caps *caps
 				     struct mod_power_init_params *init_data);
 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);
drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_test.c +138 -0

150 lines not on this page · raw message

Signature · 1 line
2.34.1