rockchip: rkvenc: retry a frame whose bitstream overflowed

A job whose bitstream overflows the capture buffer counts as a
hardware fault: two in a row put both queues into error and end the
stream. The rate controller reaches that state on its own whenever a
few dark frames drive the QP to the floor and the next bright frame
no longer fits, which a start into a sunlit scene does on every other
attempt even with the step limiter fixed.

Handle the overflow as the rate-control event it is: force clear, feed
the controller the buffer size as the frame size, restart that frame
type from its control QP and run the same frame again while the job
stays running for the mem2mem framework, so user space sees one late
frame and no dropped one. An IDR that carried the joined parameter
sets carries them again on the retry. A frame that overflows twice is
a fault as before, and the consecutive error count stays with the bus
errors, the hardware watchdog and the job timeout. A timed out job
clears the latched status before it starts so that it is never taken
for an overflow.

Fixes: 64129c4282 ("rockchip: add the RKVENC H.264 and HEVC encoder")
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This commit is contained in:
Daniel Golle committed 2026-10-31 10:52:53 +00:00
1 parent feeb032824
commit bf610ebb84
2 files changed
+62 -16

No files matched your search

@@ -1,4 +1,4 @@
From 50505095537aed9ee3a852b5c7c7f7b40c60d5f1 Mon Sep 17 00:00:00 2001
From cc48f7cd0faa378a48af7fabd6ad7207c64b0cc5 Mon Sep 17 00:00:00 2001
From: Daniel Golle <daniel@makrotopia.org>
Date: Fri, 16 Oct 2026 23:57:02 +0100
Subject: [PATCH] media: platform: rockchip: Add RKVENC v1 encoder driver
@@ -27,7 +27,9 @@ a V4L2 front end:
keyframe), buffer queueing, the device_run to hardware job mapping
and the hard-IRQ/threaded-IRQ completion model with claim-under-lock
job ownership and a watchdog. A failed job is cleared with a force
clear and resets the block only when that does not stop it.
clear and resets the block only when that does not stop it. A
bitstream overflow is a rate-control event: the controller restarts
that frame type from its control QP and the frame is run once more.
Both V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE and JOINED_WITH_1ST_FRAME
(with PREPEND_SPSPPS_TO_IDR) are supported. Hardware row-level rate
@@ -51,7 +53,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
.../platform/rockchip/rkvenc/rkvenc-hevc.c | 63 +
.../platform/rockchip/rkvenc/rkvenc-nal.c | 353 +++++
.../platform/rockchip/rkvenc/rkvenc-nal.h | 12 +
.../platform/rockchip/rkvenc/rkvenc-rc.c | 180 +++
.../platform/rockchip/rkvenc/rkvenc-rc.c | 195 +++
.../platform/rockchip/rkvenc/rkvenc-ref.c | 127 ++
.../platform/rockchip/rkvenc/rkvenc-v4l2.c | 1150 +++++++++++++++++
.../rockchip/rkvenc/rkvenc-vepu541-h264.c | 207 +++
@@ -60,9 +62,9 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
.../rockchip/rkvenc/rkvenc-vepu541-tables.c | 278 ++++
.../platform/rockchip/rkvenc/rkvenc-vepu541.c | 218 ++++
.../platform/rockchip/rkvenc/rkvenc-vepu541.h | 201 +++
.../media/platform/rockchip/rkvenc/rkvenc.c | 533 ++++++++
.../media/platform/rockchip/rkvenc/rkvenc.h | 249 ++++
20 files changed, 4244 insertions(+)
.../media/platform/rockchip/rkvenc/rkvenc.c | 552 ++++++++
.../media/platform/rockchip/rkvenc/rkvenc.h | 251 ++++
20 files changed, 4280 insertions(+)
create mode 100644 drivers/media/platform/rockchip/rkvenc/Kconfig
create mode 100644 drivers/media/platform/rockchip/rkvenc/Makefile
create mode 100644 drivers/media/platform/rockchip/rkvenc/rkvenc-gop.c
@@ -751,7 +753,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+#endif
--- /dev/null
+++ b/drivers/media/platform/rockchip/rkvenc/rkvenc-rc.c
@@ -0,0 +1,180 @@
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Frame-level integer rate controller for the RKVENC v1 encoder
@@ -932,6 +934,21 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+
+ ctx->rc_first[t] = false;
+}
+
+/*
+ * A frame of type t overflowed its capture buffer of bytes at the picked
+ * QP. The retry restarts from the control QP; the step limit alone would
+ * need frames that the overflow keeps dropping.
+ */
+void rkvenc_rc_overflow(struct rkvenc_ctx *ctx, u32 bytes, u8 frame_type)
+{
+ s8 start = frame_type ? ctx->c.p_frame_qp : ctx->c.i_frame_qp;
+ unsigned int t = frame_type ? 1 : 0;
+
+ rkvenc_rc_update(ctx, bytes, ctx->cur_qp, frame_type);
+ if (ctx->rc_qp[t] < start)
+ ctx->rc_qp[t] = start;
+}
--- /dev/null
+++ b/drivers/media/platform/rockchip/rkvenc/rkvenc-ref.c
@@ -0,0 +1,127 @@
@@ -3607,7 +3624,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+#endif
--- /dev/null
+++ b/drivers/media/platform/rockchip/rkvenc/rkvenc.c
@@ -0,0 +1,533 @@
@@ -0,0 +1,552 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Rockchip RKVENC v1 platform driver: probe/remove, clocks, resets, power
@@ -3703,6 +3720,8 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+ if (rkvenc_vepu541_l1_written(a))
+ writel(img->l1[a / 4], regs + a);
+
+ /* A timed out job must not be judged by the previous status. */
+ dev->irq_sta = 0;
+ /* Order every relaxed write before the start (spec/01 5.4). */
+ writel(img->enc_strt, regs + RKVENC_ENC_STRT);
+
@@ -3825,14 +3844,31 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+ rkvenc_rc_update(ctx, payload, qp_avg, frame_type);
+ rkvenc_gop_commit(ctx);
+ ctx->consecutive_errors = 0;
+ ctx->overflow_retried = false;
+
+ avg_qp = v4l2_ctrl_find(&ctx->ctrl_hdl,
+ V4L2_CID_MPEG_VIDEO_AVERAGE_QP);
+ if (avg_qp)
+ v4l2_ctrl_s_ctrl(avg_qp, qp_avg);
+ } else if (ctx->rc_active && !ctx->overflow_retried &&
+ (int_sta & RKVENC_INT_ERRORS) == RKVENC_INT_OFLW) {
+ /* The job stays running for the framework: the frame is retried */
+ dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
+ rkvenc_hw_recover(dev);
+ rkvenc_rc_overflow(ctx,
+ dst ? vb2_plane_size(&dst->vb2_buf, 0) : 0,
+ ctx->cur_intra ? 0 : 1);
+ ctx->overflow_retried = true;
+ if (ctx->cur_prepend)
+ ctx->joined_header_done = false;
+ dev->job_ctx = NULL;
+ pm_runtime_put(dev->dev);
+ rkvenc_m2m_ops.device_run(ctx);
+ return;
+ } else {
+ rkvenc_hw_recover(dev);
+ rkvenc_gop_rollback(ctx);
+ ctx->overflow_retried = false;
+ ctx->consecutive_errors++;
+ if (ctx->consecutive_errors >= RKVENC_MAX_CONSEC_ERRORS) {
+ vb2_queue_error(&ctx->m2m_ctx->out_q_ctx.q);
@@ -4143,7 +4179,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+MODULE_LICENSE("GPL");
--- /dev/null
+++ b/drivers/media/platform/rockchip/rkvenc/rkvenc.h
@@ -0,0 +1,249 @@
@@ -0,0 +1,251 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Rockchip RKVENC v1 H.264 / HEVC encoder driver.
@@ -4313,6 +4349,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+ bool last_good_valid;
+ int last_good;
+ u32 consecutive_errors;
+ bool overflow_retried;
+
+ /* OUTPUT (raw) format details. */
+ u32 src_num_planes;
@@ -4380,6 +4417,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
+void rkvenc_rc_pick(struct rkvenc_ctx *ctx, u8 *qp, u8 frame_type);
+void rkvenc_rc_update(struct rkvenc_ctx *ctx, u32 bytes, u32 qp_avg,
+ u8 frame_type);
+void rkvenc_rc_overflow(struct rkvenc_ctx *ctx, u32 bytes, u8 frame_type);
+
+/* rkvenc-h264.c / rkvenc-hevc.c */
+int rkvenc_h264_setup(struct rkvenc_ctx *ctx);
@@ -1,4 +1,4 @@
From bcf5c3598fd14420c900f14c96ef9b554d5ee6a8 Mon Sep 17 00:00:00 2001
From 37c56aa3b70a0eda2d0d2ef705921890b2264ece Mon Sep 17 00:00:00 2001
From: Daniel Golle <daniel@makrotopia.org>
Date: Fri, 16 Oct 2026 23:57:17 +0100
Subject: [PATCH] media: rockchip: rkvenc: Add frequency scaling and cooling
@@ -31,9 +31,9 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
.../media/platform/rockchip/rkvenc/Makefile | 1 +
.../platform/rockchip/rkvenc/rkvenc-dvfs.c | 370 ++++++++++++++++++
.../platform/rockchip/rkvenc/rkvenc-v4l2.c | 20 +-
.../media/platform/rockchip/rkvenc/rkvenc.c | 10 +-
.../media/platform/rockchip/rkvenc/rkvenc.c | 11 +-
.../media/platform/rockchip/rkvenc/rkvenc.h | 47 +++
6 files changed, 448 insertions(+), 3 deletions(-)
6 files changed, 449 insertions(+), 3 deletions(-)
create mode 100644 drivers/media/platform/rockchip/rkvenc/rkvenc-dvfs.c
--- a/drivers/media/platform/rockchip/rkvenc/Kconfig
@@ -501,7 +501,15 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
v4l2_fh_del(&ctx->fh, filp);
--- a/drivers/media/platform/rockchip/rkvenc/rkvenc.c
+++ b/drivers/media/platform/rockchip/rkvenc/rkvenc.c
@@ -240,6 +240,7 @@ static void rkvenc_finish_job(struct rkv
@@ -236,6 +236,7 @@ static void rkvenc_finish_job(struct rkv
ctx->joined_header_done = false;
dev->job_ctx = NULL;
pm_runtime_put(dev->dev);
+ rkvenc_dvfs_job_end(dev, ctx);
rkvenc_m2m_ops.device_run(ctx);
return;
} else {
@@ -259,6 +260,7 @@ static void rkvenc_finish_job(struct rkv
dev->job_ctx = NULL;
pm_runtime_put(dev->dev);
@@ -509,7 +517,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev, ctx->m2m_ctx, state);
}
@@ -466,12 +467,18 @@ static int rkvenc_probe(struct platform_
@@ -485,12 +487,18 @@ static int rkvenc_probe(struct platform_
goto err_unreg_v4l2;
}
@@ -529,7 +537,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
err_release_m2m:
v4l2_m2m_release(rkvenc->m2m_dev);
err_unreg_v4l2:
@@ -486,6 +493,7 @@ static void rkvenc_remove(struct platfor
@@ -505,6 +513,7 @@ static void rkvenc_remove(struct platfor
struct rkvenc_dev *rkvenc = platform_get_drvdata(pdev);
cancel_delayed_work_sync(&rkvenc->watchdog);
@@ -613,7 +621,7 @@ Signed-off-by: Daniel Golle <daniel@makrotopia.org>
u32 src_fourcc;
u32 coded_fourcc;
@@ -215,6 +252,16 @@ void rkvenc_hw_recover(struct rkvenc_dev
@@ -216,6 +253,16 @@ void rkvenc_hw_recover(struct rkvenc_dev
int rkvenc_v4l2_init(struct rkvenc_dev *dev);
void rkvenc_v4l2_cleanup(struct rkvenc_dev *dev);