From: Hugo Sales <[email protected]>

Since commit ad40065dc8bd ("drm/amd/display: Hold the VRR vblank
reference only on active CRTCs"), disabling a CRTC with VRR on goes
through the VRR on->off branch of amdgpu_dm_handle_vrr_transition(). That
branch calls dc_exit_ips_for_hw_access() before clearing the PSR and
Replay events

When the last display goes off while VRR is active,
amdgpu_dm_commit_streams() allows IPS (idle power states), since no
display is left, but the IPS exit in the VRR transition that follows
disallows it. DCN 3.5 and later APUs would then stay out of IPS with all
displays off

A disabled CRTC has no stream, so there are no PSR or Replay events to
clear. Return after dropping the vblank reference when the new state has
no stream, which skips the IPS exit as well

The test helper is split into setup and run steps so the new IPS case can
set up the dc before the transition runs

Fixes: ad40065dc8bd ("drm/amd/display: Hold the VRR vblank reference only on 
active CRTCs")
Link: 
https://sashiko.dev/#/patchset/[email protected]?part=1
Cc: [email protected] # 7.3.x: ad40065dc8bd: drm/amd/display: Hold the VRR 
vblank reference only on active CRTCs
Signed-off-by: Hugo Sales <[email protected]>
---
sashiko-bot found this on v1 of "Hold the VRR vblank reference only on
active CRTCs" [1], but v1 was applied before I got around to sending the
v2

I couldn't test it on hardware, since my laptop is DCN 2.1 and has no
IPS. dm_test_vrr_transition_crtc_off_keeps_ips fails without this patch
and passes with it. The amdgpu_dm_freesync KUnit suite passes 42/42 on
v7.4-rc5. Other amdgpu_dm suites fail or panic in amdgpu_dm_hpd_init()
the same way with and without the patch

Could someone with a DCN 3.5 or later APU confirm that it stays out of
IPS when the last display goes off with VRR on, and that this patch
fixes it?

For stable, this only applies on top of ad40065dc8bd, so on 7.3.y it has
to go in after that commit's backport. The LTS trees don't have the IPS
exit in amdgpu_dm_handle_vrr_transition(), so they don't need it

[1] https://lore.kernel.org/r/[email protected]
---
 .../drm/amd/display/amdgpu_dm/amdgpu_dm_freesync.c |   8 ++
 .../amdgpu_dm/tests/amdgpu_dm_freesync_test.c      | 107 +++++++++++++++++----
 2 files changed, 94 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_freesync.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_freesync.c
index f2539bcf824fc..001992111d80b 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_freesync.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_freesync.c
@@ -453,6 +453,14 @@ void amdgpu_dm_handle_vrr_transition(struct 
amdgpu_display_manager *dm,
                drm_dbg_driver(new_state->base.crtc->dev, "%s: crtc=%u VRR 
on->off: Drop vblank ref\n",
                                 __func__, new_state->base.crtc->base.id);
 
+               /*
+                * A disabled CRTC has no stream, so there are no PSR or Replay
+                * events to clear. Leave IPS as amdgpu_dm_commit_streams() set
+                * it, which allows it once the last display is gone
+                */
+               if (!new_state->stream)
+                       return;
+
                scoped_guard(mutex, &dm->dc_lock) {
                        dc_exit_ips_for_hw_access(dm->dc);
                        amdgpu_dm_psr_set_event(dm, new_state->stream, false,
diff --git 
a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_freesync_test.c 
b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_freesync_test.c
index 8f219f08cd55b..a8b3c1febca1c 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_freesync_test.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_freesync_test.c
@@ -939,19 +939,19 @@ static void 
dm_test_handle_vrr_transition_round_trip(struct kunit *test)
 }
 
 /*
- * Runs a transition on a CRTC whose vblank is on with two references held,
- * so a get or put never has to enable or disable it, and returns the
- * refcount afterwards
+ * Sets up a transition on a CRTC whose vblank is on with two references
+ * held, so a get or put never has to enable or disable it
  */
-static int dm_test_vrr_transition_refcount(struct kunit *test,
-                                          bool old_active, enum mod_vrr_state 
old_vrr,
-                                          bool new_active, enum mod_vrr_state 
new_vrr)
+static struct amdgpu_display_manager *
+dm_test_vrr_transition_setup(struct kunit *test,
+                            bool old_active, enum mod_vrr_state old_vrr,
+                            bool new_active, enum mod_vrr_state new_vrr,
+                            struct dm_crtc_state **old_state,
+                            struct dm_crtc_state **new_state)
 {
        struct amdgpu_device *adev = dm_kunit_alloc_adev(test);
        struct amdgpu_display_manager *dm = dm_kunit_alloc_dm(test);
-       struct dm_crtc_state *old_state, *new_state;
        struct drm_crtc *crtc;
-       int refcount;
 
        KUNIT_ASSERT_EQ(test, drm_vblank_init(&adev->ddev, 1), 0);
        adev->ip_versions[DCE_HWIP][0] = IP_VERSION(2, 1, 0);
@@ -962,30 +962,55 @@ static int dm_test_vrr_transition_refcount(struct kunit 
*test,
        KUNIT_ASSERT_NOT_NULL(test, crtc);
        crtc->dev = &adev->ddev;
 
-       old_state = kunit_kzalloc(test, sizeof(*old_state), GFP_KERNEL);
-       new_state = kunit_kzalloc(test, sizeof(*new_state), GFP_KERNEL);
-       KUNIT_ASSERT_NOT_NULL(test, old_state);
-       KUNIT_ASSERT_NOT_NULL(test, new_state);
-       old_state->base.crtc = crtc;
-       old_state->base.active = old_active;
-       old_state->freesync_config.state = old_vrr;
-       new_state->base.crtc = crtc;
-       new_state->base.active = new_active;
-       new_state->freesync_config.state = new_vrr;
+       *old_state = kunit_kzalloc(test, sizeof(**old_state), GFP_KERNEL);
+       *new_state = kunit_kzalloc(test, sizeof(**new_state), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, *old_state);
+       KUNIT_ASSERT_NOT_NULL(test, *new_state);
+       (*old_state)->base.crtc = crtc;
+       (*old_state)->base.active = old_active;
+       (*old_state)->freesync_config.state = old_vrr;
+       (*new_state)->base.crtc = crtc;
+       (*new_state)->base.active = new_active;
+       (*new_state)->freesync_config.state = new_vrr;
 
        adev->ddev.vblank[0].enabled = true;
        atomic_set(&adev->ddev.vblank[0].refcount, 2);
 
+       return dm;
+}
+
+/* Returns the refcount after the transition */
+static int dm_test_vrr_transition_run(struct amdgpu_display_manager *dm,
+                                     struct dm_crtc_state *old_state,
+                                     struct dm_crtc_state *new_state)
+{
+       struct drm_vblank_crtc *vblank = &dm->adev->ddev.vblank[0];
+       int refcount;
+
        amdgpu_dm_handle_vrr_transition(dm, old_state, new_state);
-       refcount = atomic_read(&adev->ddev.vblank[0].refcount);
+       refcount = atomic_read(&vblank->refcount);
 
        /* drm_vblank_init_release() warns if vblank is still enabled */
-       adev->ddev.vblank[0].enabled = false;
-       atomic_set(&adev->ddev.vblank[0].refcount, 0);
+       vblank->enabled = false;
+       atomic_set(&vblank->refcount, 0);
 
        return refcount;
 }
 
+static int dm_test_vrr_transition_refcount(struct kunit *test,
+                                          bool old_active, enum mod_vrr_state 
old_vrr,
+                                          bool new_active, enum mod_vrr_state 
new_vrr)
+{
+       struct dm_crtc_state *old_state, *new_state;
+       struct amdgpu_display_manager *dm;
+
+       dm = dm_test_vrr_transition_setup(test, old_active, old_vrr,
+                                         new_active, new_vrr,
+                                         &old_state, &new_state);
+
+       return dm_test_vrr_transition_run(dm, old_state, new_state);
+}
+
 /**
  * dm_test_vrr_transition_crtc_off_drops_ref - Test disabling the CRTC drops 
the ref
  * @test: The KUnit test context
@@ -1031,6 +1056,45 @@ static void 
dm_test_vrr_transition_crtc_off_keeps_ref(struct kunit *test)
                        2);
 }
 
+static int dm_test_vrr_idle_calls;
+
+static bool dm_test_vrr_apply_idle(struct dc *dc, bool allow)
+{
+       dm_test_vrr_idle_calls++;
+
+       /* Not applied, so dc_allow_idle_optimizations() skips its logging */
+       return false;
+}
+
+/**
+ * dm_test_vrr_transition_crtc_off_keeps_ips - Test disabling the CRTC leaves 
IPS alone
+ * @test: The KUnit test context
+ *
+ * amdgpu_dm_commit_streams() allows IPS once the last display is gone. The
+ * disabled CRTC has no stream, so there is nothing for the VRR transition
+ * to program, and it must not disallow IPS again
+ */
+static void dm_test_vrr_transition_crtc_off_keeps_ips(struct kunit *test)
+{
+       struct dm_crtc_state *old_state, *new_state;
+       struct amdgpu_display_manager *dm;
+       struct dc *dc;
+
+       dm = dm_test_vrr_transition_setup(test, true, VRR_STATE_ACTIVE_VARIABLE,
+                                         false, VRR_STATE_ACTIVE_VARIABLE,
+                                         &old_state, &new_state);
+       dc = dm->dc;
+       dc->caps.ips_support = true;
+       dc->caps.is_apu = true;
+       dc->idle_optimizations_allowed = true;
+       dc->clk_mgr = dm_kunit_alloc_clk_mgr(test);
+       dc->hwss.apply_idle_power_optimizations = dm_test_vrr_apply_idle;
+       dm_test_vrr_idle_calls = 0;
+
+       KUNIT_EXPECT_EQ(test, dm_test_vrr_transition_run(dm, old_state, 
new_state), 1);
+       KUNIT_EXPECT_EQ(test, dm_test_vrr_idle_calls, 0);
+}
+
 static struct kunit_case amdgpu_dm_freesync_tests[] = {
        /* amdgpu_dm_is_timing_unchanged_for_freesync */
        KUNIT_CASE(dm_test_timing_unchanged_null_args),
@@ -1081,6 +1145,7 @@ static struct kunit_case amdgpu_dm_freesync_tests[] = {
        KUNIT_CASE(dm_test_vrr_transition_crtc_off_drops_ref),
        KUNIT_CASE(dm_test_vrr_transition_crtc_on_takes_ref),
        KUNIT_CASE(dm_test_vrr_transition_crtc_off_keeps_ref),
+       KUNIT_CASE(dm_test_vrr_transition_crtc_off_keeps_ips),
        {}
 };
 

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261202-amdgpu-vrr-ips-followup-bd16272d9d58

Best regards,
--  
Hugo Sales <[email protected]>


Reply via email to