[PATCH 3/4] media: tegra-vde: clean only the used part of the bitstream buffer

From: Jerome Berclaz via B4 Relay

Date: Thu Nov 26 2026 - 19:56:16 EST


From: Jerome Berclaz <jerome.berclaz@xxxxxxxxx>

When the decoder sits behind the SMMU, buffers use vb2_dma_sg_memops,
i.e. cached pages, and vb2 cleans the whole bitstream (OUTPUT) buffer
from the CPU caches on every QBUF. FFmpeg's V4L2 request decoder makes
these buffers width * height * 3 / 2 bytes, 1.4 MB at 720p, while a coded
frame is usually a few KB, so most of that work is wasted: it was the
largest remaining kernel cost of hardware H.264 playback on a Tegra30.

The hardware only reads the coded data and the zero padding that
tegra_buf_prepare() writes after it. Clean that range from
tegra_buf_prepare() and let vb2 skip its whole-buffer sync. OUTPUT
buffers are never synced for the CPU on DQBUF already (DMA_TO_DEVICE).

Signed-off-by: Jerome Berclaz <jerome.berclaz@xxxxxxxxx>
---
drivers/media/platform/nvidia/tegra-vde/v4l2.c | 33 ++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

diff --git a/drivers/media/platform/nvidia/tegra-vde/v4l2.c b/drivers/media/platform/nvidia/tegra-vde/v4l2.c
index cd25307ad9db..3269ca75593f 100644
--- a/drivers/media/platform/nvidia/tegra-vde/v4l2.c
+++ b/drivers/media/platform/nvidia/tegra-vde/v4l2.c
@@ -12,6 +12,7 @@
* Copyright (C) 2019 Boris Brezillon <boris.brezillon@xxxxxxxxxxxxx>
*/

+#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/slab.h>

@@ -214,6 +215,30 @@ static void tegra_buf_cleanup(struct vb2_buffer *vb)
__tegra_buf_cleanup(vb, vb->num_planes);
}

+/*
+ * With the SMMU the buffers are cached pages (vb2_dma_sg), and vb2 would
+ * clean the whole bitstream buffer on every QBUF, although the hardware
+ * only reads the coded data and its padding. Clean just that range.
+ */
+static void tegra_sync_coded_data(struct vb2_buffer *vb, unsigned int plane,
+ size_t size)
+{
+ struct device *dev = vb->vb2_queue->dev;
+ struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, plane);
+ struct scatterlist *sg;
+ unsigned int i;
+
+ for_each_sgtable_dma_sg(sgt, sg, i) {
+ size_t len = min_t(size_t, sg_dma_len(sg), size);
+
+ dma_sync_single_for_device(dev, sg_dma_address(sg), len,
+ DMA_TO_DEVICE);
+ size -= len;
+ if (!size)
+ break;
+ }
+}
+
static int tegra_buf_prepare(struct vb2_buffer *vb)
{
struct vb2_queue *vq = vb->vb2_queue;
@@ -264,11 +289,19 @@ static int tegra_buf_prepare(struct vb2_buffer *vb)
if (vb_data)
memset(vb_data + offset + size, 0,
hw_size - offset - size);
+
+ if (ctx->vde->domain && vb->memory == VB2_MEMORY_MMAP)
+ tegra_sync_coded_data(vb, i, hw_size);
}

tb->dma_addr[i] = tb->dma_base[i] + offset;
}

+ /* the range in use was synced above, skip vb2's whole-buffer sync */
+ if (V4L2_TYPE_IS_OUTPUT(vq->type) && ctx->vde->domain &&
+ vb->memory == VB2_MEMORY_MMAP)
+ vb->skip_cache_sync_on_prepare = 1;
+
switch (pixfmt->pixelformat) {
case V4L2_PIX_FMT_YVU420M:
swap(tb->dma_addr[1], tb->dma_addr[2]);

--
2.53.0