From 2c63a63bf65739763051dc4ce7ce2ffaf2d514c4 Mon Sep 17 00:00:00 2001 In-Reply-To: <20260429195306.239666-1-mfritsche@reauktion.de> References: <20260429195306.239666-1-mfritsche@reauktion.de> From: Markus Fritsche Date: Sat, 9 May 2026 16:50:51 +0200 Subject: [PATCH RFC v2] media: rockchip-rga: attach dma_resv release fence at device_run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Opt the rockchip-rga driver into the new vb2 release-fence helper. Same shape as the hantro patch: attach a producer fence on the CAPTURE-side dmabuf at m2m device_run, signalled by vb2_buffer_done() when RGA completes the m2m operation. Differs from hantro in one mechanical detail: rga's device_run wraps the entire body in spin_lock_irqsave(&rga->ctrl_lock). Our helper calls dma_resv_lock(), which is sleepable, so the buffer-fetch + fence-attach sequence has to run above the spinlock. Restructure device_run so: - v4l2_m2m_next_src_buf / next_dst_buf, - src->sequence increment, - vb2_buffer_attach_release_fence(&dst->vb2_buf) run before spin_lock_irqsave; only the rga->curr assignment and rga_hw_start() (the actual HW kick) remain inside the spinlock. This is safe under the m2m-job ownership model: by the time device_run is called, the m2m core has selected this context and serializes one device_run per context, so v4l2_m2m_next_*_buf returns stable pointers until the corresponding *_buf_remove in rga_isr. ctrl_lock was previously protecting per-device state (rga->curr) and the HW register access, neither of which depends on the buffer-fetch happening inside the lock. The CAPTURE queue is opted in with supports_release_fences=true at queue_init. Userspace consumers of RGA-produced dmabufs (image-processing pipelines, screen-rotation servers, gstreamer flows on Rockchip boards) get spec-clean implicit-sync semantics, matching what hantro does in the previous patch in this series. Sven Püschel's ongoing "media: platform: rga: Add RGA3 support" v5 series (linux-rockchip 2026-04-28) restructures rga.c substantially. If that lands first, the device_run restructure here will need a rebase against the new shape; the locking story itself is invariant. Cc: Jacob Chen Cc: Ezequiel Garcia Cc: Sven Püschel Cc: Heiko Stuebner Cc: Hans Verkuil Cc: linux-media@vger.kernel.org Cc: linux-rockchip@lists.infradead.org Signed-off-by: Markus Fritsche --- drivers/media/platform/rockchip/rga/rga.c | 27 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/rockchip/rga/rga.c b/drivers/media/platform/rockchip/rga/rga.c index fea63b94c..03030c7ea 100644 --- a/drivers/media/platform/rockchip/rga/rga.c +++ b/drivers/media/platform/rockchip/rga/rga.c @@ -38,15 +38,28 @@ static void device_run(void *prv) struct vb2_v4l2_buffer *src, *dst; unsigned long flags; - spin_lock_irqsave(&rga->ctrl_lock, flags); - - rga->curr = ctx; - + /* + * Fetch the next-job buffers and (best-effort) attach a producer + * fence on CAPTURE before taking ctrl_lock below. + * vb2_buffer_attach_release_fence() takes dma_resv_lock, which is + * sleepable; ctrl_lock is taken with spin_lock_irqsave so any + * sleepable call must happen above it. Buffer ownership is + * already committed at this point: the m2m core has selected + * this context for device_run and serializes one device_run per + * context, so v4l2_m2m_next_*_buf returns stable pointers until + * the corresponding *_buf_remove in rga_isr. + */ src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); src->sequence = ctx->osequence++; dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); + (void)vb2_buffer_attach_release_fence(&dst->vb2_buf); + + spin_lock_irqsave(&rga->ctrl_lock, flags); + + rga->curr = ctx; + rga_hw_start(rga, vb_to_rga(src), vb_to_rga(dst)); spin_unlock_irqrestore(&rga->ctrl_lock, flags); @@ -123,6 +136,12 @@ queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) dst_vq->lock = &ctx->rga->mutex; dst_vq->dev = ctx->rga->v4l2_dev.dev; + /* + * Opt the CAPTURE queue into vb2 release-fence publishing. + * Compile-time gated by CONFIG_VIDEOBUF2_RELEASE_FENCES. + */ + dst_vq->supports_release_fences = true; + return vb2_queue_init(dst_vq); } -- 2.53.0