From 63e7faab60d3baa1bcfab3785b98b761499de78a Mon Sep 17 00:00:00 2001 From: Nabige Aala Date: Tue, 1 Dec 2026 16:29:02 +0530 Subject: [PATCH] BACKPORT: drm/msm: serialize cross-CRTC SSPP reassignment to fix underrun 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. Link: https://lore.kernel.org/all/20261201-underrun-fix-v1-1-23af33abd2d7@oss.qualcomm.com/ Fixes: 774bcfb731765 ("drm/msm/dpu: add support for virtual planes") Signed-off-by: Nabige Aala --- 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 | 15 ++++- drivers/gpu/drm/msm/msm_kms.h | 13 ++++ 4 files changed, 111 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 a03dc12ee3c79..4c3e1cd43057f 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); @@ -1110,6 +1190,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 e39831a397b16..92f7defeaf276 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 e70e5088cfe53..ae5dac4227a50 100644 --- a/drivers/gpu/drm/msm/msm_atomic.c +++ b/drivers/gpu/drm/msm/msm_atomic.c @@ -137,8 +137,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; @@ -163,6 +163,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; } @@ -222,7 +225,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); @@ -332,5 +335,11 @@ void msm_atomic_commit_tail(struct drm_atomic_commit *state) 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) + kms->funcs->atomic_commit_setup(kms, state); + return drm_dp_mst_atomic_setup_commit(state); } diff --git a/drivers/gpu/drm/msm/msm_kms.h b/drivers/gpu/drm/msm/msm_kms.h index f25b31e502d21..104145b41fe33 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.