public inbox for linux-rockchip@lists.infradead.org
 help / color / mirror / Atom feed
From: Benjamin Gaignard <benjamin.gaignard@collabora.com>
To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com,
	ming.qian@nxp.com, ezequiel@vanguardiasur.com.ar,
	p.zabel@pengutronix.de, gregkh@linuxfoundation.org,
	hverkuil-cisco@xs4all.nl, nicolas.dufresne@collabora.com
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org,
	linux-arm-msm@vger.kernel.org,
	linux-rockchip@lists.infradead.org,
	linux-staging@lists.linux.dev, kernel@collabora.com,
	Benjamin Gaignard <benjamin.gaignard@collabora.com>
Subject: [PATCH v3 05/11] media: verisilicon: Refactor postprocessor to store more buffers
Date: Thu, 22 Jun 2023 15:13:43 +0200	[thread overview]
Message-ID: <20230622131349.144160-6-benjamin.gaignard@collabora.com> (raw)
In-Reply-To: <20230622131349.144160-1-benjamin.gaignard@collabora.com>

Like for vb queues remove VB2_MAX_FRAME upper limit on postprocessor
buffers storage capacity.
If a buffer hasn't been allocated when setting up the queue make
possible to alloc it on the fly while streaming. This happens
when resolution change between keyframes.

Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
---
 drivers/media/platform/verisilicon/hantro.h   |   6 +-
 .../media/platform/verisilicon/hantro_hw.h    |   6 +-
 .../platform/verisilicon/hantro_postproc.c    | 109 ++++++++++++------
 .../media/platform/verisilicon/hantro_v4l2.c  |  12 +-
 4 files changed, 82 insertions(+), 51 deletions(-)

diff --git a/drivers/media/platform/verisilicon/hantro.h b/drivers/media/platform/verisilicon/hantro.h
index 2989ebc631cc..bd0dca11b90a 100644
--- a/drivers/media/platform/verisilicon/hantro.h
+++ b/drivers/media/platform/verisilicon/hantro.h
@@ -460,12 +460,14 @@ hantro_get_dst_buf(struct hantro_ctx *ctx)
 
 bool hantro_needs_postproc(const struct hantro_ctx *ctx,
 			   const struct hantro_fmt *fmt);
+dma_addr_t
+hantro_postproc_get_dec_buf_addr(struct hantro_ctx *ctx, int index);
 
 static inline dma_addr_t
 hantro_get_dec_buf_addr(struct hantro_ctx *ctx, struct vb2_buffer *vb)
 {
 	if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt))
-		return ctx->postproc.dec_q[vb->index].dma;
+		return hantro_postproc_get_dec_buf_addr(ctx, vb->index);
 	return vb2_dma_contig_plane_dma_addr(vb, 0);
 }
 
@@ -477,8 +479,8 @@ vb2_to_hantro_decoded_buf(struct vb2_buffer *buf)
 
 void hantro_postproc_disable(struct hantro_ctx *ctx);
 void hantro_postproc_enable(struct hantro_ctx *ctx);
+int hantro_postproc_init(struct hantro_ctx *ctx);
 void hantro_postproc_free(struct hantro_ctx *ctx);
-int hantro_postproc_alloc(struct hantro_ctx *ctx);
 int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx,
 				   struct v4l2_frmsizeenum *fsize);
 
diff --git a/drivers/media/platform/verisilicon/hantro_hw.h b/drivers/media/platform/verisilicon/hantro_hw.h
index 9e8faf7ba6fb..5c7b6e04aded 100644
--- a/drivers/media/platform/verisilicon/hantro_hw.h
+++ b/drivers/media/platform/verisilicon/hantro_hw.h
@@ -15,8 +15,6 @@
 #include <media/v4l2-vp9.h>
 #include <media/videobuf2-core.h>
 
-#define VB2_MAX_FRAME	32
-
 #define DEC_8190_ALIGN_MASK	0x07U
 
 #define MB_DIM			16
@@ -252,10 +250,10 @@ struct hantro_vp9_dec_hw_ctx {
 /**
  * struct hantro_postproc_ctx
  *
- * @dec_q:		References buffers, in decoder format.
+ * @dec_refs_xa:	References buffers, in decoder format.
  */
 struct hantro_postproc_ctx {
-	struct hantro_aux_buf dec_q[VB2_MAX_FRAME];
+	struct xarray dec_refs_xa;
 };
 
 /**
diff --git a/drivers/media/platform/verisilicon/hantro_postproc.c b/drivers/media/platform/verisilicon/hantro_postproc.c
index 6437423ccf3a..a6b67d0cc66c 100644
--- a/drivers/media/platform/verisilicon/hantro_postproc.c
+++ b/drivers/media/platform/verisilicon/hantro_postproc.c
@@ -170,36 +170,23 @@ static int hantro_postproc_g2_enum_framesizes(struct hantro_ctx *ctx,
 	return 0;
 }
 
-void hantro_postproc_free(struct hantro_ctx *ctx)
+static int hantro_postproc_alloc(struct hantro_ctx *ctx, int index)
 {
 	struct hantro_dev *vpu = ctx->dev;
-	unsigned int i;
-
-	for (i = 0; i < VB2_MAX_FRAME; ++i) {
-		struct hantro_aux_buf *priv = &ctx->postproc.dec_q[i];
-
-		if (priv->cpu) {
-			dma_free_attrs(vpu->dev, priv->size, priv->cpu,
-				       priv->dma, priv->attrs);
-			priv->cpu = NULL;
-		}
-	}
-}
-
-int hantro_postproc_alloc(struct hantro_ctx *ctx)
-{
-	struct hantro_dev *vpu = ctx->dev;
-	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
-	struct vb2_queue *cap_queue = &m2m_ctx->cap_q_ctx.q;
-	unsigned int num_buffers = cap_queue->num_buffers;
 	struct v4l2_pix_format_mplane pix_mp;
 	const struct hantro_fmt *fmt;
-	unsigned int i, buf_size;
+	struct hantro_aux_buf *priv;
+	unsigned int buf_size;
+
+	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
 
 	/* this should always pick native format */
 	fmt = hantro_get_default_fmt(ctx, false, ctx->bit_depth);
 	if (!fmt)
 		return -EINVAL;
+
 	v4l2_fill_pixfmt_mp(&pix_mp, fmt->fourcc, ctx->src_fmt.width,
 			    ctx->src_fmt.height);
 
@@ -214,20 +201,74 @@ int hantro_postproc_alloc(struct hantro_ctx *ctx)
 		buf_size += hantro_hevc_mv_size(pix_mp.width,
 						pix_mp.height);
 
-	for (i = 0; i < num_buffers; ++i) {
-		struct hantro_aux_buf *priv = &ctx->postproc.dec_q[i];
-
-		/*
-		 * The buffers on this queue are meant as intermediate
-		 * buffers for the decoder, so no mapping is needed.
-		 */
-		priv->attrs = DMA_ATTR_NO_KERNEL_MAPPING;
-		priv->cpu = dma_alloc_attrs(vpu->dev, buf_size, &priv->dma,
-					    GFP_KERNEL, priv->attrs);
-		if (!priv->cpu)
-			return -ENOMEM;
-		priv->size = buf_size;
+	/*
+	 * The buffers on this queue are meant as intermediate
+	 * buffers for the decoder, so no mapping is needed.
+	 */
+	priv->attrs = DMA_ATTR_NO_KERNEL_MAPPING;
+	priv->cpu = dma_alloc_attrs(vpu->dev, buf_size, &priv->dma, GFP_KERNEL, priv->attrs);
+	if (!priv->cpu)
+		return -ENOMEM;
+
+	priv->size = buf_size;
+
+	xa_store(&ctx->postproc.dec_refs_xa, index, priv, GFP_KERNEL);
+
+	return 0;
+}
+
+void hantro_postproc_free(struct hantro_ctx *ctx)
+{
+	struct hantro_dev *vpu = ctx->dev;
+	struct hantro_aux_buf *priv;
+	unsigned long i;
+
+	xa_for_each(&ctx->postproc.dec_refs_xa, i, priv) {
+		dma_free_attrs(vpu->dev, priv->size, priv->cpu, priv->dma, priv->attrs);
+		kfree(priv);
+	}
+
+	xa_destroy(&ctx->postproc.dec_refs_xa);
+}
+
+int hantro_postproc_init(struct hantro_ctx *ctx)
+{
+	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
+	struct vb2_queue *cap_queue = &m2m_ctx->cap_q_ctx.q;
+	unsigned int num_buffers = cap_queue->num_buffers;
+	int i, ret = 0;
+
+	xa_init_flags(&ctx->postproc.dec_refs_xa, XA_FLAGS_ALLOC);
+
+	for (i = 0; i < num_buffers; i++) {
+		ret = hantro_postproc_alloc(ctx, i);
+		if (ret < 0) {
+			hantro_postproc_free(ctx);
+			return ret;
+		}
 	}
+
+	return ret;
+}
+
+dma_addr_t
+hantro_postproc_get_dec_buf_addr(struct hantro_ctx *ctx, int index)
+{
+	struct hantro_aux_buf *priv = xa_load(&ctx->postproc.dec_refs_xa, index);
+	int ret;
+
+	if (priv)
+		return priv->dma;
+
+	/* buffer not already allocated, try getting a new one */
+	ret = hantro_postproc_alloc(ctx, index);
+	if (ret < 0)
+		return 0;
+
+	priv = xa_load(&ctx->postproc.dec_refs_xa, index);
+	if (priv)
+		return priv->dma;
+
 	return 0;
 }
 
diff --git a/drivers/media/platform/verisilicon/hantro_v4l2.c b/drivers/media/platform/verisilicon/hantro_v4l2.c
index 61cfaaf4e927..0f615c1001c0 100644
--- a/drivers/media/platform/verisilicon/hantro_v4l2.c
+++ b/drivers/media/platform/verisilicon/hantro_v4l2.c
@@ -894,20 +894,10 @@ static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
 			ret = ctx->codec_ops->init(ctx);
 			if (ret)
 				return ret;
-		}
-
-		if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
-			ret = hantro_postproc_alloc(ctx);
-			if (ret)
-				goto err_codec_exit;
+			ret = hantro_postproc_init(ctx);
 		}
 	}
 	return ret;
-
-err_codec_exit:
-	if (ctx->codec_ops->exit)
-		ctx->codec_ops->exit(ctx);
-	return ret;
 }
 
 static void
-- 
2.39.2


_______________________________________________
Linux-rockchip mailing list
Linux-rockchip@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-rockchip

  parent reply	other threads:[~2023-06-22 13:14 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-22 13:13 [PATCH v3 00/11] Add DELETE_BUF ioctl Benjamin Gaignard
2023-06-22 13:13 ` [PATCH v3 01/11] media: videobuf2: Access vb2_queue bufs array through helper functions Benjamin Gaignard
2023-06-23 15:10   ` kernel test robot
2023-06-24 23:34   ` kernel test robot
2023-06-22 13:13 ` [PATCH v3 02/11] media: videobuf2: Use Xarray instead of static buffers array Benjamin Gaignard
2023-06-22 13:13 ` [PATCH v3 03/11] media: videobuf2: Remove VB2_MAX_FRAME limit on buffer storage Benjamin Gaignard
2023-06-22 13:56   ` Dan Carpenter
2023-06-22 14:11   ` Dan Carpenter
2023-06-22 14:13     ` Benjamin Gaignard
2023-06-23  7:02       ` Hans Verkuil
2023-06-23  7:51         ` Benjamin Gaignard
2023-06-22 13:13 ` [PATCH v3 04/11] media: videobuf2: Stop define VB2_MAX_FRAME as global Benjamin Gaignard
2023-06-30  9:51   ` Hsia-Jun Li
2023-07-03  8:09     ` Benjamin Gaignard
2023-07-03  8:35       ` Hsia-Jun Li
2023-07-03 10:53         ` Benjamin Gaignard
2023-07-03 11:05           ` Hsia-Jun Li
2023-07-12 10:48         ` Tomasz Figa
2023-07-17  7:44           ` Hsia-Jun Li
2023-07-28  6:46             ` Tomasz Figa
2023-07-28  6:55               ` Hsia-Jun Li
2023-07-28  7:03                 ` Tomasz Figa
2023-07-28  7:24                   ` Hsia-Jun Li
2023-09-07  4:15                     ` Tomasz Figa
2023-09-07  6:54                       ` Hsia-Jun Li
2023-06-22 13:13 ` Benjamin Gaignard [this message]
2023-06-22 13:13 ` [PATCH v3 06/11] media: verisilicon: Store chroma and motion vectors offset Benjamin Gaignard
2023-07-02  7:48   ` Markus Elfring
2023-06-22 13:13 ` [PATCH v3 07/11] media: verisilicon: vp9: Use destination buffer height to compute chroma offset Benjamin Gaignard
2023-07-02  9:14   ` Markus Elfring
2023-06-22 13:13 ` [PATCH v3 08/11] media: verisilicon: postproc: Fix down scale test Benjamin Gaignard
2023-06-22 15:27   ` Benjamin Gaignard
2023-06-22 13:13 ` [PATCH v3 09/11] media: verisilicon: vp9: Allow to change resolution while streaming Benjamin Gaignard
2023-07-02  9:33   ` Markus Elfring
2023-06-22 13:13 ` [PATCH v3 10/11] media: v4l2: Add DELETE_BUF ioctl Benjamin Gaignard
2023-06-22 23:12   ` kernel test robot
2023-06-23  0:25   ` kernel test robot
2023-06-26  7:08   ` [EXT] " Ming Qian
2023-06-26  7:48     ` Benjamin Gaignard
2023-06-26  7:50       ` Benjamin Gaignard
2023-06-26  8:13         ` Ming Qian
2023-06-26  8:04       ` Ming Qian
2023-06-27  7:30   ` Hsia-Jun Li
2023-06-27  8:43     ` Benjamin Gaignard
2023-06-27  8:47       ` Hsia-Jun Li
2023-06-30  9:43         ` Hsia-Jun Li
2023-07-03  8:12           ` Benjamin Gaignard
2023-07-03  8:19             ` Hsia-Jun Li
2023-07-03  8:52               ` Benjamin Gaignard
2023-07-03  9:20                 ` Hsia-Jun Li
2023-07-03 10:35                   ` Benjamin Gaignard
2023-07-03 11:06                     ` Hsia-Jun Li
     [not found]                     ` <8ca2f66e-8ff9-e885-274f-51417b581b78@synaptics.com>
2023-07-03 11:17                       ` Benjamin Gaignard
2023-07-03 15:42                         ` Randy Li
2023-07-13  9:09           ` Tomasz Figa
2023-07-17  2:16             ` Hsia-Jun Li
2023-07-28  6:57               ` Tomasz Figa
2023-07-28  7:26                 ` Hsia-Jun Li
2023-07-02 10:20   ` Markus Elfring
2023-06-22 13:13 ` [PATCH v3 11/11] media: v4l2: Add mem2mem helpers for " Benjamin Gaignard
2023-06-27  7:40 ` [PATCH v3 00/11] Add " Hsia-Jun Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230622131349.144160-6-benjamin.gaignard@collabora.com \
    --to=benjamin.gaignard@collabora.com \
    --cc=ezequiel@vanguardiasur.com.ar \
    --cc=gregkh@linuxfoundation.org \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=kernel@collabora.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=m.szyprowski@samsung.com \
    --cc=mchehab@kernel.org \
    --cc=ming.qian@nxp.com \
    --cc=nicolas.dufresne@collabora.com \
    --cc=p.zabel@pengutronix.de \
    --cc=tfiga@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox