SSPPs are shared hardware resources. When a virtual plane moves its SSPP
from one CRTC to another, the reassigning commit can take the async
(legacy cursor) path. On that path, hw_done and flip_done are signalled
before the hardware is actually programmed. The destination CRTC's next
commit then calls wait_flush(), which only guards its own CRTC mask and
is blind to the in-flight reassignment on the source CRTC. Both commits
end up racing to program the same SSPP, causing an underrun.

The standard per-CRTC locking cannot catch this because the two commits
operate on different CRTC masks, so the cross-CRTC handoff is invisible
to wait_flush().

Fix this with two cooperating mechanisms:

1. Veto async when SSPP ownership changes: compare sspp_to_crtc_id[]
   between old and new global state in a new can_do_async() hook. If any
   SSPP has moved to a different CRTC, force the commit onto the
   synchronous path so hw_done only fires after hardware is fully
   programmed.

2. Cross-CRTC barrier in prepare_commit: snapshot the drm_crtc_commit
   handle for each affected CRTC into dpu_global_state->pending_commits[]
   at commit setup time. On the next commit, drm_crtc_commit_wait() on
   those handles blocks hardware programming until the prior
   SSPP-reassigning commit has truly completed, regardless of which CRTCs
   are involved.

Fixes: 774bcfb731765 ("drm/msm/dpu: add support for virtual planes")
Signed-off-by: Nabige Aala <[email protected]>
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c | 83 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h |  3 ++
 drivers/gpu/drm/msm/msm_atomic.c        | 20 ++++++--
 drivers/gpu/drm/msm/msm_drv.h           |  1 +
 drivers/gpu/drm/msm/msm_kms.c           |  1 +
 drivers/gpu/drm/msm/msm_kms.h           | 13 ++++++
 6 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c 
b/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c
index cc86e06790c4..0950983c2911 100644
--- a/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c
+++ b/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c
@@ -356,6 +356,9 @@ dpu_kms_global_duplicate_state(struct drm_private_obj *obj)
 
        __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
 
+       /* The new state will track its own commits during commit setup. */
+       memset(state->pending_commits, 0, sizeof(state->pending_commits));
+
        return &state->base;
 }
 
@@ -363,6 +366,12 @@ static void dpu_kms_global_destroy_state(struct 
drm_private_obj *obj,
                                      struct drm_private_state *state)
 {
        struct dpu_global_state *dpu_state = to_dpu_global_state(state);
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(dpu_state->pending_commits); i++) {
+               if (dpu_state->pending_commits[i])
+                       drm_crtc_commit_put(dpu_state->pending_commits[i]);
+       }
 
        kfree(dpu_state);
 }
@@ -462,6 +471,77 @@ static int dpu_kms_check_mode_changed(struct msm_kms *kms, 
struct drm_atomic_com
        return 0;
 }
 
+static int dpu_kms_atomic_commit_setup(struct msm_kms *kms,
+                                      struct drm_atomic_commit *state)
+{
+       struct dpu_kms *dpu_kms = to_dpu_kms(kms);
+       struct drm_private_state *priv_state;
+       struct dpu_global_state *global_state;
+       struct drm_crtc_state *crtc_state;
+       struct drm_crtc *crtc;
+       int i;
+
+       priv_state = drm_atomic_get_new_private_obj_state(state, 
&dpu_kms->global_state);
+       if (!priv_state)
+               return 0;
+
+       global_state = to_dpu_global_state(priv_state);
+       for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
+               if (crtc_state->commit)
+                       global_state->pending_commits[drm_crtc_index(crtc)] =
+                               drm_crtc_commit_get(crtc_state->commit);
+       }
+
+       return 0;
+}
+
+static void dpu_kms_prepare_commit(struct msm_kms *kms, struct 
drm_atomic_commit *state)
+{
+       struct dpu_kms *dpu_kms = to_dpu_kms(kms);
+       struct drm_private_state *priv_state;
+       struct dpu_global_state *global_state;
+       unsigned int i;
+       int ret;
+
+       priv_state = drm_atomic_get_old_private_obj_state(state, 
&dpu_kms->global_state);
+       if (!priv_state)
+               return;
+
+       global_state = to_dpu_global_state(priv_state);
+       /*
+        * Virtual planes on different CRTCs can reuse the same SSPP. The
+        * per-CRTC and per-plane dependencies do not cover that handoff.
+        * Wait for the previous resource update before programming hardware.
+        */
+       for (i = 0; i < ARRAY_SIZE(global_state->pending_commits); i++) {
+               if (!global_state->pending_commits[i])
+                       continue;
+
+               ret = drm_crtc_commit_wait(global_state->pending_commits[i]);
+               if (ret)
+                       drm_err(kms->dev, "CRTC %u resource commit wait timed 
out\n", i);
+       }
+}
+
+static bool dpu_kms_can_do_async(struct msm_kms *kms, struct drm_atomic_commit 
*state)
+{
+       struct dpu_kms *dpu_kms = to_dpu_kms(kms);
+       struct drm_private_state *old_priv_state, *new_priv_state;
+       const struct dpu_global_state *old_global_state, *new_global_state;
+
+       new_priv_state = drm_atomic_get_new_private_obj_state(state, 
&dpu_kms->global_state);
+       if (!new_priv_state)
+               return true;
+
+       old_priv_state = drm_atomic_get_old_private_obj_state(state, 
&dpu_kms->global_state);
+       old_global_state = to_dpu_global_state(old_priv_state);
+       new_global_state = to_dpu_global_state(new_priv_state);
+
+       return !memcmp(old_global_state->sspp_to_crtc_id,
+                      new_global_state->sspp_to_crtc_id,
+                      sizeof(new_global_state->sspp_to_crtc_id));
+}
+
 static void dpu_kms_flush_commit(struct msm_kms *kms, unsigned crtc_mask)
 {
        struct dpu_kms *dpu_kms = to_dpu_kms(kms);
@@ -1075,6 +1155,9 @@ static const struct msm_kms_funcs kms_funcs = {
        .enable_commit   = dpu_kms_enable_commit,
        .disable_commit  = dpu_kms_disable_commit,
        .check_mode_changed = dpu_kms_check_mode_changed,
+       .atomic_commit_setup = dpu_kms_atomic_commit_setup,
+       .prepare_commit  = dpu_kms_prepare_commit,
+       .can_do_async    = dpu_kms_can_do_async,
        .flush_commit    = dpu_kms_flush_commit,
        .wait_flush      = dpu_kms_wait_flush,
        .complete_commit = dpu_kms_complete_commit,
diff --git a/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h 
b/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h
index e39831a397b1..92f7defeaf27 100644
--- a/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h
+++ b/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.h
@@ -124,6 +124,9 @@ struct dpu_global_state {
 
        struct dpu_rm *rm;
 
+       /* Commits updating this resource state, held until it is destroyed. */
+       struct drm_crtc_commit *pending_commits[MAX_CRTCS];
+
        uint32_t pingpong_to_crtc_id[PINGPONG_MAX - PINGPONG_0];
        uint32_t mixer_to_crtc_id[LM_MAX - LM_0];
        uint32_t ctl_to_crtc_id[CTL_MAX - CTL_0];
diff --git a/drivers/gpu/drm/msm/msm_atomic.c b/drivers/gpu/drm/msm/msm_atomic.c
index a8babf1dbe0d..e59b4cc7077d 100644
--- a/drivers/gpu/drm/msm/msm_atomic.c
+++ b/drivers/gpu/drm/msm/msm_atomic.c
@@ -136,8 +136,8 @@ void msm_atomic_destroy_pending_timer(struct 
msm_pending_timer *timer)
                kthread_destroy_worker(timer->worker);
 }
 
-static bool can_do_async(struct drm_atomic_commit *state,
-               struct drm_crtc **async_crtc)
+static bool can_do_async(struct msm_kms *kms, struct drm_atomic_commit *state,
+                                struct drm_crtc **async_crtc)
 {
        struct drm_connector_state *connector_state;
        struct drm_connector *connector;
@@ -162,6 +162,9 @@ static bool can_do_async(struct drm_atomic_commit *state,
                *async_crtc = crtc;
        }
 
+       if (kms->funcs->can_do_async && !kms->funcs->can_do_async(kms, state))
+               return false;
+
        return true;
 }
 
@@ -217,7 +220,7 @@ void msm_atomic_commit_tail(struct drm_atomic_commit *state)
        struct msm_kms *kms = priv->kms;
        struct drm_crtc *async_crtc = NULL;
        unsigned crtc_mask = get_crtc_mask(state);
-       bool async = can_do_async(state, &async_crtc);
+       bool async = can_do_async(kms, state, &async_crtc);
 
        trace_msm_atomic_commit_tail_start(async, crtc_mask);
 
@@ -322,3 +325,14 @@ void msm_atomic_commit_tail(struct drm_atomic_commit 
*state)
 
        trace_msm_atomic_commit_tail_finish(async, crtc_mask);
 }
+
+int msm_atomic_commit_setup(struct drm_atomic_commit *state)
+{
+       struct msm_drm_private *priv = state->dev->dev_private;
+       struct msm_kms *kms = priv->kms;
+
+       if (kms->funcs->atomic_commit_setup)
+               return kms->funcs->atomic_commit_setup(kms, state);
+
+       return 0;
+}
diff --git a/drivers/gpu/drm/msm/msm_drv.h b/drivers/gpu/drm/msm/msm_drv.h
index b65702ac0140..2d95326b191c 100644
--- a/drivers/gpu/drm/msm/msm_drv.h
+++ b/drivers/gpu/drm/msm/msm_drv.h
@@ -212,6 +212,7 @@ int msm_atomic_init_pending_timer(struct msm_pending_timer 
*timer,
 void msm_atomic_destroy_pending_timer(struct msm_pending_timer *timer);
 void msm_atomic_commit_tail(struct drm_atomic_commit *state);
 int msm_atomic_check(struct drm_device *dev, struct drm_atomic_commit *state);
+int msm_atomic_commit_setup(struct drm_atomic_commit *state);
 struct drm_atomic_commit *msm_atomic_state_alloc(struct drm_device *dev);
 
 int msm_crtc_enable_vblank(struct drm_crtc *crtc);
diff --git a/drivers/gpu/drm/msm/msm_kms.c b/drivers/gpu/drm/msm/msm_kms.c
index 4c7d9a06cc22..0310af0e986b 100644
--- a/drivers/gpu/drm/msm/msm_kms.c
+++ b/drivers/gpu/drm/msm/msm_kms.c
@@ -29,6 +29,7 @@ static const struct drm_mode_config_funcs mode_config_funcs = 
{
 
 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
        .atomic_commit_tail = msm_atomic_commit_tail,
+       .atomic_commit_setup = msm_atomic_commit_setup,
 };
 
 static irqreturn_t msm_irq(int irq, void *arg)
diff --git a/drivers/gpu/drm/msm/msm_kms.h b/drivers/gpu/drm/msm/msm_kms.h
index f25b31e502d2..104145b41fe3 100644
--- a/drivers/gpu/drm/msm/msm_kms.h
+++ b/drivers/gpu/drm/msm/msm_kms.h
@@ -68,6 +68,19 @@ struct msm_kms_funcs {
         */
        int (*check_mode_changed)(struct msm_kms *kms, struct drm_atomic_commit 
*state);
 
+       /**
+        * Track dependencies on shared resources before swapping the atomic
+        * state. The CRTC commit objects have already been allocated.
+        */
+       int (*atomic_commit_setup)(struct msm_kms *kms, struct 
drm_atomic_commit *state);
+
+       /**
+        * Return whether this update may defer its flush until the pending
+        * timer runs. Resource changes that require hardware completion before
+        * another commit can reuse them must not defer the flush.
+        */
+       bool (*can_do_async)(struct msm_kms *kms, struct drm_atomic_commit 
*state);
+
        /**
         * Prepare for atomic commit.  This is called after any previous
         * (async or otherwise) commit has completed.

---
base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
change-id: 20261201-underrun-fix-7c884e487666

Best regards,
--  
Nabige Aala <[email protected]>

Reply via email to