From 718d7136285d8f16fa9e010361c61a9e0fec15ac Mon Sep 17 00:00:00 2001 From: John Cox Date: Tue, 17 Nov 2026 16:24:02 +0000 Subject: [PATCH 01/18] media/hevc_d: Fix off by one worst size cmd buffer checks tile_entry_fill will not fulfil its stated guarantee to leave one slot free if its loop never runs. Fix that. Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index e6a035e99ca46d..9b68af4b870fae 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1078,8 +1078,7 @@ static int tile_entry_fill(struct hevc_d_dec_env *const de, const unsigned int last_x = s->col_bd[t_x + 1] - 1; const unsigned int last_y = s->row_bd[t_y + 1] - 1; - /* One more than needed here */ - rv = cmds_check_space(de, CMDS_NEW_ENTRY_POINT + 3); + rv = cmds_check_space(de, CMDS_NEW_ENTRY_POINT + 2); if (rv) return rv; @@ -1097,7 +1096,8 @@ static int tile_entry_fill(struct hevc_d_dec_env *const de, t_x, t_y, s->col_bd[t_x], s->row_bd[t_y], de->entry_qp, de->entry_slice); } - return 0; + /* Loop above may not run once so must check here */ + return cmds_check_space(de, 1); } /* Write STATUS register with expected end CTU address of previous slice */ From 1ea622e77cf2f0e7774914a2e7d03e851b0f1cf3 Mon Sep 17 00:00:00 2001 From: John Cox Date: Tue, 17 Nov 2026 16:57:08 +0000 Subject: [PATCH 02/18] media/hevc_d: Avoid unlikely race between decode & streamoff If there is a concurrent decode and streamoff request there is a theoretical window in which the streamoff can complete before the decode. Close it. Signed-off-by: John Cox --- .../media/platform/raspberrypi/hevc_dec/hevc_d_h265.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index 9b68af4b870fae..bed575e6012fc7 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -2392,11 +2392,18 @@ void hevc_d_h265_trigger(struct hevc_d_ctx *ctx) de->frame_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); de->request = req; + /* + * Once job_finish is called it is possible to get a streamoff. By + * that point the irq must have been claimed or sync will not give + * the correct ordering. + * This means p1out may go -ve it if the irq runs before we inc it + * but that is OK + */ + hevc_d_hw_irq_active1_claim(dev, &de->irq_ent, p1_cb, de); + /* Enable the next setup if our Q isn't too big */ if (atomic_add_return(1, &ctx->p1out) < HEVC_D_P1BUF_COUNT) v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx); - - hevc_d_hw_irq_active1_claim(dev, &de->irq_ent, p1_cb, de); } static int try_ctrl_sps(struct v4l2_ctrl *ctrl) From 78565271f01eb8c118aca1457d873eab607d42c5 Mon Sep 17 00:00:00 2001 From: John Cox Date: Tue, 17 Nov 2026 17:22:33 +0000 Subject: [PATCH 03/18] media/hevc_d: Fix setup order so error path is always valid frame_slot and rpi_currpoc are used in the error path before they are set. Fix this. Signed-off-by: John Cox --- .../platform/raspberrypi/hevc_dec/hevc_d_h265.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index bed575e6012fc7..d5495bc7c4f5d4 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1587,6 +1587,15 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) s->slice_temporal_mvp = (sh0->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED); + /* + * Set frame_slot and currpoc early as they are used in the error path + */ + de->mvbase = 0; + de->cmd_len = 0; + de->dpbno_col = ~0U; + de->frame_slot = run->dst->vb2_buf.index; + de->rpi_currpoc = sh0->slice_pic_order_cnt; + /* Frame start */ if (!is_sps_set(run->h265.sps)) { @@ -1621,8 +1630,6 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) de->pic_height_in_ctbs_y = (s->sps.pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y; /* 7-17 */ - de->cmd_len = 0; - de->dpbno_col = ~0U; switch (ctx->dst_fmt.pixelformat) { case V4L2_PIX_FMT_NV12MT_COL128: @@ -1633,8 +1640,6 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) de->chroma_stride = de->luma_stride / 2; de->frame_chroma_addr = vb2_dma_contig_plane_dma_addr(&run->dst->vb2_buf, 1); - de->mvbase = 0; - de->frame_slot = run->dst->vb2_buf.index; break; case V4L2_PIX_FMT_NV12_COL128: case V4L2_PIX_FMT_NV12_10_COL128: @@ -1644,8 +1649,6 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) de->chroma_stride = de->luma_stride; de->frame_chroma_addr = de->frame_luma_addr + (ctx->dst_fmt.height * 128); - de->mvbase = 0; - de->frame_slot = run->dst->vb2_buf.index; old_bits = true; break; } @@ -1729,7 +1732,6 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) de->rpi_config2 = mk_config2(s); de->rpi_framesize = (s->sps.pic_height_in_luma_samples << 16) | s->sps.pic_width_in_luma_samples; - de->rpi_currpoc = sh0->slice_pic_order_cnt; s->slice_idx = 0; From 8871699a1d27174dd985649d80ef7039a597ef86 Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 15:46:49 +0000 Subject: [PATCH 04/18] media/hevc_d: Fix PPS/SPS typo in comment Typo after checking if PPS has changed. Fix it Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index d5495bc7c4f5d4..b3573c30242ffc 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1611,7 +1611,7 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) } if (sps_changed || memcmp(&s->pps, run->h265.pps, sizeof(s->pps)) != 0) { - /* SPS changed */ + /* PPS changed */ memcpy(&s->pps, run->h265.pps, sizeof(s->pps)); /* Recalc stuff as required */ From 6eeaa833449c47c3b10380f880b2c9450aab4fee Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 15:50:52 +0000 Subject: [PATCH 05/18] media/hevc_d: Only start decode h/w after both CAPTURE and OUTPUT have started Starting the h/w and therefore keeping decode context based only on OUTPUT streaming state allows CAPTURE formats to change without the context changing. This allows for missized buffers. Fix so the h/w is shutdown if either stream is off. Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_video.c | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index d0af990278cf40..8227547c21e7e3 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -529,6 +529,17 @@ static int hevc_d_buf_prepare(struct vb2_buffer *vb) return 0; } +static bool other_q_started(struct hevc_d_ctx *ctx, struct vb2_queue *vq) +{ + struct vb2_queue *other_vq; + + other_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, + V4L2_TYPE_IS_OUTPUT(vq->type) ? + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : + V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); + return vb2_start_streaming_called(other_vq); +} + static int hevc_d_start_streaming(struct vb2_queue *vq, unsigned int count) { struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); @@ -537,7 +548,8 @@ static int hevc_d_start_streaming(struct vb2_queue *vq, unsigned int count) v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, vq); - if (V4L2_TYPE_IS_OUTPUT(vq->type)) { + /* Only start if we have both OUTPUT and CAPTURE streaming */ + if (other_q_started(ctx, vq)) { ret = hevc_d_hw_pm_get(dev); if (ret) goto fail_cleanup; @@ -562,7 +574,12 @@ static void hevc_d_stop_streaming(struct vb2_queue *vq) struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); struct hevc_d_dev *dev = ctx->dev; - if (V4L2_TYPE_IS_OUTPUT(vq->type)) { + /* + * Stop if we stop any stream + * Some decoder resource allocation is based off source and destination + * formats and this prevents old badly sized buffers being used. + */ + if (other_q_started(ctx, vq)) { hevc_d_h265_stop(ctx); hevc_d_hw_pm_put(dev); } From b4494d88a59993cc4e516626f7d4bbf9f300ae3b Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 16:30:46 +0000 Subject: [PATCH 06/18] media/hevc_d: Remove dev_mutex and replace usage with ctx_mutex Remove device serialization of G_FMT and S_FMT and use the per context lock instead for these ops. Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c | 5 ----- drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h | 5 +---- .../media/platform/raspberrypi/hevc_dec/hevc_d_video.c | 9 +++++++++ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c index 57a2a93e1abb14..21e04d43d95fc9 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c @@ -236,10 +236,7 @@ static int hevc_d_probe(struct platform_device *pdev) goto err_hw; } - mutex_init(&dev->dev_mutex); - vfd = &dev->vfd; - vfd->lock = &dev->dev_mutex; vfd->v4l2_dev = &dev->v4l2_dev; snprintf(vfd->name, sizeof(vfd->name), "%s", hevc_d_video_device.name); @@ -303,7 +300,6 @@ static int hevc_d_probe(struct platform_device *pdev) media_device_cleanup(&dev->mdev); v4l2_m2m_release(dev->m2m_dev); err_v4l2: - mutex_destroy(&dev->dev_mutex); v4l2_device_unregister(&dev->v4l2_dev); err_hw: hevc_d_hw_remove(dev); @@ -321,7 +317,6 @@ static void hevc_d_remove(struct platform_device *pdev) v4l2_m2m_release(dev->m2m_dev); video_unregister_device(&dev->vfd); - mutex_destroy(&dev->dev_mutex); v4l2_device_unregister(&dev->v4l2_dev); hevc_d_hw_remove(dev); diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h index 6d40d91229a6b5..e3092af645c808 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h @@ -110,7 +110,7 @@ struct hevc_d_ctx { */ int fatal_err; - /* Lock for queue operations */ + /* Lock for queue operations and src_fmt / dst_fmt */ struct mutex ctx_mutex; struct v4l2_ctrl_handler hdl; @@ -180,9 +180,6 @@ struct hevc_d_dev { struct device *dev; struct v4l2_m2m_dev *m2m_dev; - /* Video device file (vfd) mutex */ - struct mutex dev_mutex; - void __iomem *base_irq; void __iomem *base_h265; diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index 8227547c21e7e3..6993e60d695d82 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -319,6 +319,7 @@ static int hevc_d_enum_fmt_vid_cap(struct file *file, void *priv, * default is not updated whilst CAPTURE buffers exist as they were sized * from it * returns a pointer to the struct as a convenience + * Must be called with ctx_mutex held */ static struct v4l2_pix_format_mplane *get_dst_fmt(struct hevc_d_ctx *const ctx) { @@ -334,6 +335,8 @@ static int hevc_d_g_fmt_vid_cap(struct file *file, void *priv, { struct hevc_d_ctx *ctx = hevc_d_file2ctx(file); + guard(mutex)(&ctx->ctx_mutex); + f->fmt.pix_mp = *get_dst_fmt(ctx); return 0; } @@ -343,6 +346,8 @@ static int hevc_d_g_fmt_vid_out(struct file *file, void *priv, { struct hevc_d_ctx *ctx = hevc_d_file2ctx(file); + guard(mutex)(&ctx->ctx_mutex); + f->fmt.pix_mp = ctx->src_fmt; return 0; } @@ -387,6 +392,8 @@ static int hevc_d_s_fmt_vid_cap(struct file *file, void *priv, struct vb2_queue *vq; int ret; + guard(mutex)(&ctx->ctx_mutex); + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); if (vb2_is_busy(vq)) return -EBUSY; @@ -408,6 +415,8 @@ static int hevc_d_s_fmt_vid_out(struct file *file, void *priv, struct vb2_queue *vq; int ret; + guard(mutex)(&ctx->ctx_mutex); + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); if (vb2_is_streaming(vq)) return -EBUSY; From 1160b3e0c78a28259a8cba7ac2b23abf2fd6b430 Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 16:56:30 +0000 Subject: [PATCH 07/18] media/hevc_d: Ensure that a removed buffer is not in use Add code to invalidate a capture buffer slot if the buffer is removed Also change sync to use wait_for_completion which is both neater and safer than the existing wait_queue logic Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_h265.c | 57 ++++++++++++++++--- .../raspberrypi/hevc_dec/hevc_d_h265.h | 1 + .../raspberrypi/hevc_dec/hevc_d_video.c | 19 +++++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index b3573c30242ffc..7baee9140ec489 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -11,6 +11,7 @@ * Copyright (C) 2018 Bootlin */ +#include #include #include @@ -2001,6 +2002,10 @@ static void phase2_err_claimed(struct hevc_d_dev *const dev, void *v) phase2_done(dev, de, VB2_BUF_STATE_ERROR); } +/* + * Find the dpb slot, if the slot is empty then fall back to the slot + * with the nearest POC + */ static const struct hevc_d_slot *find_slot(struct hevc_d_dec_env *const de, const struct hevc_d_ctx *ctx, unsigned int dpb_no) @@ -2252,8 +2257,7 @@ static void dec_state_delete(struct hevc_d_ctx *const ctx) } struct irq_sync { - atomic_t done; - wait_queue_head_t wq; + struct completion done; struct hevc_d_hw_irq_ent irq_ent; }; @@ -2261,8 +2265,7 @@ static void phase2_sync_claimed(struct hevc_d_dev *const dev, void *v) { struct irq_sync *const sync = v; - atomic_set(&sync->done, 1); - wake_up(&sync->wq); + complete(&sync->done); } static void phase1_sync_claimed(struct hevc_d_dev *const dev, void *v) @@ -2285,11 +2288,51 @@ void hevc_d_h265_sync(struct hevc_d_dev *dev) { struct irq_sync sync; - atomic_set(&sync.done, 0); - init_waitqueue_head(&sync.wq); + init_completion(&sync.done); hevc_d_hw_irq_active1_claim(dev, &sync.irq_ent, phase1_sync_claimed, &sync); - wait_event(sync.wq, atomic_read(&sync.done)); + wait_for_completion(&sync.done); +} + +struct slot_invalidate { + struct completion done; + struct hevc_d_slot *slot; + struct hevc_d_hw_irq_ent irq_ent; +}; + +static void slot_invalidate_claimed(struct hevc_d_dev *const dev, void *v) +{ + struct slot_invalidate *const si = v; + struct hevc_d_slot *const slot = si->slot; + + /* Invalidate slot but leave POC intact for fallback */ + slot->refybase = 0; + slot->refcbase = 0; + slot->colbase = 0; + complete(&si->done); +} + +/* + * Forget the reference addresses cached for a CAPTURE buffer + * Syncs with phase 2 so it is safe to clobber and only returns + * once the buffer is no longer in use by h/w. + * + * If the buffer was still in use, then an alternative will be selected by + * find_slot at the start of phase2 + */ +void hevc_d_h265_invalidate_slot(struct hevc_d_ctx *ctx, unsigned int index) +{ + struct slot_invalidate si; + + if (WARN_ON(index >= ARRAY_SIZE(ctx->slots))) + return; + + init_completion(&si.done); + si.slot = ctx->slots + index; + + hevc_d_hw_irq_active2_claim(ctx->dev, &si.irq_ent, + slot_invalidate_claimed, &si); + wait_for_completion(&si.done); } static void h265_ctx_uninit(struct hevc_d_dev *const dev, struct hevc_d_ctx *ctx) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h index 75c248c76da668..7abcd0667a3a6e 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.h @@ -18,6 +18,7 @@ int hevc_d_h265_start(struct hevc_d_ctx *ctx); void hevc_d_h265_stop(struct hevc_d_ctx *ctx); void hevc_d_h265_trigger(struct hevc_d_ctx *ctx); void hevc_d_h265_sync(struct hevc_d_dev *dev); +void hevc_d_h265_invalidate_slot(struct hevc_d_ctx *ctx, unsigned int index); void hevc_d_device_run(void *priv); diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index 6993e60d695d82..a55cd3ab2f5996 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -538,6 +538,24 @@ static int hevc_d_buf_prepare(struct vb2_buffer *vb) return 0; } +/* + * Called before buffer memory is released: on free, VIDIOC_REMOVE_BUFS or + * when a dmabuf is replaced on QBUF. + * A CAPTURE buffer that userspace has dequeued may still be in use as a + * reference by frames in the decode pipeline so make sure the decoder has + * let go of it first. + */ +static void hevc_d_buf_cleanup(struct vb2_buffer *vb) +{ + struct vb2_queue *vq = vb->vb2_queue; + struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); + + if (V4L2_TYPE_IS_OUTPUT(vq->type)) + return; + + hevc_d_h265_invalidate_slot(ctx, vb->index); +} + static bool other_q_started(struct hevc_d_ctx *ctx, struct vb2_queue *vq) { struct vb2_queue *other_vq; @@ -618,6 +636,7 @@ static void hevc_d_buf_request_complete(struct vb2_buffer *vb) static const struct vb2_ops hevc_d_qops = { .queue_setup = hevc_d_queue_setup, .buf_prepare = hevc_d_buf_prepare, + .buf_cleanup = hevc_d_buf_cleanup, .buf_queue = hevc_d_buf_queue, .buf_out_validate = hevc_d_buf_out_validate, .buf_request_complete = hevc_d_buf_request_complete, From 4d8517a9753de5a30afdafda6f1308a743e60aa4 Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 17:43:06 +0000 Subject: [PATCH 08/18] media/hevc_d: Remove min_queued_buffers = 1 from CAPTURE queue min_queued_buffers = 1 is unneeded and Claude asserts that it can cause a (unlikely) race such that a job can be started before _start has completed so remove it. (Hantro doesn't have this line) Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index a55cd3ab2f5996..aa010408439aa0 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -674,7 +674,6 @@ int hevc_d_queue_init(void *priv, struct vb2_queue *src_vq, dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING; dst_vq->drv_priv = ctx; dst_vq->buf_struct_size = sizeof(struct hevc_d_buffer); - dst_vq->min_queued_buffers = 1; dst_vq->ops = &hevc_d_qops; dst_vq->mem_ops = &vb2_dma_contig_memops; dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; From 9a58cc51f91bc17d068f76ce5e56d643940dc69a Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 18:04:45 +0000 Subject: [PATCH 09/18] media/hevc_d: Ensure media requests are cleaned up correctly Requests are manually completed. Fix cleanup code to complete them. Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_video.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index aa010408439aa0..686657822279f7 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -494,6 +494,7 @@ static void hevc_d_queue_cleanup(struct vb2_queue *vq, u32 state) { struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); struct vb2_v4l2_buffer *vbuf; + struct media_request *req; for (;;) { if (V4L2_TYPE_IS_OUTPUT(vq->type)) @@ -504,9 +505,13 @@ static void hevc_d_queue_cleanup(struct vb2_queue *vq, u32 state) if (!vbuf) return; - v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, - &ctx->hdl); + /* Ensure requests are dealt with */ + req = state == VB2_BUF_STATE_QUEUED ? NULL : vbuf->vb2_buf.req_obj.req; + if (req) + v4l2_ctrl_request_complete(req, &ctx->hdl); v4l2_m2m_buf_done(vbuf, state); + if (req) + media_request_manual_complete(req); } } @@ -629,8 +634,15 @@ static void hevc_d_buf_queue(struct vb2_buffer *vb) static void hevc_d_buf_request_complete(struct vb2_buffer *vb) { struct hevc_d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); + struct media_request *req = vb->req_obj.req; - v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); + v4l2_ctrl_request_complete(req, &ctx->hdl); + + /* Must do manual completion, to avoid WARN we must unbind first */ + media_request_object_unbind(&vb->req_obj); + media_request_object_put(&vb->req_obj); + + media_request_manual_complete(req); } static const struct vb2_ops hevc_d_qops = { From ca34115de93b6ddf52fe249abf9711fd40e779ae Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 18:31:24 +0000 Subject: [PATCH 10/18] media/hevc_d: Lock controls against change in phase 0 Take the control lock whilst h265_setup is in progress to avoid inconsistent values whilst in use. Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_h265.c | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index 7baee9140ec489..d9e4b7c9daf832 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -2616,7 +2616,8 @@ void hevc_d_device_run(void *priv) struct hevc_d_dev *const dev = ctx->dev; struct hevc_d_run run = {}; struct media_request *src_req; - const struct v4l2_ctrl *ctrl; + const struct v4l2_ctrl *slice_ctrl; + int err; run.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); run.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); @@ -2636,10 +2637,8 @@ void hevc_d_device_run(void *priv) hevc_d_find_control_data(ctx, V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); - ctrl = v4l2_ctrl_find(ctx->fh.ctrl_handler, + slice_ctrl = v4l2_ctrl_find(ctx->fh.ctrl_handler, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); - run.h265.slice_ents = ctrl->elems; /* Framework ensures >= 1 */ - run.h265.slice_params = ctrl->p_cur.p; run.h265.scaling_matrix = hevc_d_find_control_data(ctx, @@ -2647,7 +2646,22 @@ void hevc_d_device_run(void *priv) v4l2_m2m_buf_copy_metadata(run.src, run.dst, true); - if (hevc_d_h265_setup(ctx, &run) == -1) + /* + * Ensure controls cannot be changed by non-request sets whilst we + * are using them. slice params must be read inside the lock as the + * array might be realloced. + * SPS, PPS etc. might be overwritten by new values between setup and + * here if they are not attached to the request but that doesn't + * matter as they will be fixed from here until we have finished with + * them. + */ + mutex_lock(ctx->hdl.lock); + run.h265.slice_ents = slice_ctrl->elems; /* Framework ensures >= 1 */ + run.h265.slice_params = slice_ctrl->p_cur.p; + + err = hevc_d_h265_setup(ctx, &run); + mutex_unlock(ctx->hdl.lock); + if (err) goto fail; /* Complete request(s) controls */ From 4395b190f1529671691ab2977bafc709db1865b1 Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 18:47:09 +0000 Subject: [PATCH 11/18] media/hevc_d: Fix up hevc_d_open error handling Control setup errors ignored, v4l2_fh_exit missing on error path. Fix these. Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c index 21e04d43d95fc9..b716b90070e436 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c @@ -91,6 +91,7 @@ static int hevc_d_init_ctrls(struct hevc_d_dev *dev, struct hevc_d_ctx *ctx) { struct v4l2_ctrl_handler *hdl = &ctx->hdl; unsigned int i; + int ret; v4l2_ctrl_handler_init(hdl, ARRAY_SIZE(hevc_d_ctrls)); if (hdl->error) { @@ -111,8 +112,14 @@ static int hevc_d_init_ctrls(struct hevc_d_dev *dev, struct hevc_d_ctx *ctx) } } + ret = v4l2_ctrl_handler_setup(hdl); + if (ret) { + v4l2_err(&dev->v4l2_dev, "Failed to set up control handler\n"); + v4l2_ctrl_handler_free(hdl); + return ret; + } + ctx->fh.ctrl_handler = hdl; - v4l2_ctrl_handler_setup(hdl); return 0; } @@ -155,6 +162,7 @@ static int hevc_d_open(struct file *file) err_ctx: v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); err_free: + v4l2_fh_exit(&ctx->fh); mutex_destroy(&ctx->ctx_mutex); kfree(ctx); return ret; From 63bd036374f3eed7af2a64ab6226bc603d47a4ad Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 19:05:36 +0000 Subject: [PATCH 12/18] media/hevc_d: Tighten up SPS/PPS/SH value checking Tighten up value checking so we cannot program wrong values into the h/w Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_h265.c | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index d9e4b7c9daf832..32ceaa9ef564e0 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -2456,8 +2456,9 @@ static int try_ctrl_sps(struct v4l2_ctrl *ctrl) const struct v4l2_ctrl_hevc_sps *const sps = ctrl->p_new.p_hevc_sps; struct hevc_d_ctx *const ctx = ctrl->priv; struct hevc_d_dev *const dev = ctx->dev; - const unsigned int ctb_log2_size_y = - sps->log2_min_luma_coding_block_size_minus3 + 3 + + const unsigned int min_cb_log2_size_y = + sps->log2_min_luma_coding_block_size_minus3 + 3; + const unsigned int ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size; const unsigned int min_tb_log2_size_y = sps->log2_min_luma_transform_block_size_minus2 + 2; @@ -2506,7 +2507,16 @@ static int try_ctrl_sps(struct v4l2_ctrl *ctrl) if (sps->sps_max_num_reorder_pics > sps->sps_max_dec_pic_buffering_minus1) return -EINVAL; - if (ctb_log2_size_y > 6) + if (sps->sps_max_sub_layers_minus1 > 6) + return -EINVAL; + if (ctb_log2_size_y < 4 || ctb_log2_size_y > 6) + return -EINVAL; + if (min_tb_log2_size_y >= min_cb_log2_size_y) + return -EINVAL; + /* Width & height must be a multiple of the min coding block size */ + if ((sps->pic_width_in_luma_samples | + sps->pic_height_in_luma_samples) & + ((1U << min_cb_log2_size_y) - 1)) return -EINVAL; if (max_tb_log2_size_y > 5) return -EINVAL; @@ -2523,6 +2533,23 @@ static int try_ctrl_sps(struct v4l2_ctrl *ctrl) if (sps->num_long_term_ref_pics_sps > 32) return -EINVAL; + /* The core zeros the PCM fields if PCM is not enabled */ + if (sps->flags & V4L2_HEVC_SPS_FLAG_PCM_ENABLED) { + const unsigned int bit_depth = sps->bit_depth_luma_minus8 + 8; + const unsigned int min_pcm_log2_size_y = + sps->log2_min_pcm_luma_coding_block_size_minus3 + 3; + const unsigned int max_pcm_log2_size_y = min_pcm_log2_size_y + + sps->log2_diff_max_min_pcm_luma_coding_block_size; + + /* Luma & chroma depths have been checked to be the same */ + if (sps->pcm_sample_bit_depth_luma_minus1 + 1U > bit_depth || + sps->pcm_sample_bit_depth_chroma_minus1 + 1U > bit_depth) + return -EINVAL; + if (min_pcm_log2_size_y < min(min_cb_log2_size_y, 5U) || + max_pcm_log2_size_y > min(ctb_log2_size_y, 5U)) + return -EINVAL; + } + return 0; } @@ -2546,9 +2573,9 @@ static int try_ctrl_pps(struct v4l2_ctrl *ctrl) } if ((pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED) && - (pps->num_tile_columns_minus1 > + (pps->num_tile_columns_minus1 >= ARRAY_SIZE(pps->column_width_minus1) || - pps->num_tile_rows_minus1 > + pps->num_tile_rows_minus1 >= ARRAY_SIZE(pps->row_height_minus1))) { v4l2_warn(&dev->v4l2_dev, "Tiles cols/rows too big\n"); return -EINVAL; @@ -2582,6 +2609,11 @@ static int try_ctrl_slice_params(struct v4l2_ctrl *ctrl) } for (i = 0; i != ctrl->new_elems; ++i, ++sh) { + if (!sh->bit_size) { + v4l2_warn(&dev->v4l2_dev, "Slice %u: bit size zero\n", i); + return -EINVAL; + } + if (sh->slice_type != HEVC_SLICE_B && sh->slice_type != HEVC_SLICE_P && sh->slice_type != HEVC_SLICE_I) { From 703451059c050dbd4c2d35fbd84842cfe70daef0 Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 19 Nov 2026 19:17:21 +0000 Subject: [PATCH 13/18] media/hevc_d: Ensure tiles are given in order Tiles must turn up in the correct order. Enforce this. Signed-off-by: John Cox --- .../platform/raspberrypi/hevc_dec/hevc_d_h265.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index 32ceaa9ef564e0..3e79fb4e763eae 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1568,6 +1568,7 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) struct vb2_queue *vq; struct hevc_d_dec_env *de; unsigned int prev_rs; + unsigned int prev_start_ts = 0; unsigned int i; int rv; unsigned int ctb_size_y; @@ -1825,6 +1826,13 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) /* Calc all the random coord info to avoid repeated conversion in/out */ s->start_ts = s->ctb_addr_rs_to_ts[sh->slice_segment_addr]; + if (i != 0 && s->start_ts <= prev_start_ts) { + v4l2_warn(&dev->v4l2_dev, + "Slice %u: segment addr %u not after previous slice\n", + i, sh->slice_segment_addr); + goto fail; + } + prev_start_ts = s->start_ts; s->start_ctb_x = sh->slice_segment_addr % de->pic_width_in_ctbs_y; s->start_ctb_y = sh->slice_segment_addr / de->pic_width_in_ctbs_y; /* Last CTB of previous slice */ @@ -2608,6 +2616,13 @@ static int try_ctrl_slice_params(struct v4l2_ctrl *ctrl) return -EINVAL; } + /* The first slice segment of a picture cannot be dependent */ + if (sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT) { + v4l2_warn(&dev->v4l2_dev, + "New frame but dependent slice segment\n"); + return -EINVAL; + } + for (i = 0; i != ctrl->new_elems; ++i, ++sh) { if (!sh->bit_size) { v4l2_warn(&dev->v4l2_dev, "Slice %u: bit size zero\n", i); From 529b6e35e196e6ac613e7aa48a28f43de88c4b96 Mon Sep 17 00:00:00 2001 From: John Cox Date: Fri, 20 Nov 2026 10:19:53 +0000 Subject: [PATCH 14/18] media/hevc_d: Prevent dst format change whilst streaming It is possible to remove all CAPTURE buffers whilst streaming. This allows format change as that makes it no longer busy. Check for streaming too. Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_video.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index 686657822279f7..78376aff7a5d36 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -314,10 +314,20 @@ static int hevc_d_enum_fmt_vid_cap(struct file *file, void *priv, return 0; } +/* + * The dst format may not change whilst CAPTURE buffers exist as they were + * sized from it, or whilst CAPTURE is streaming as decoder resources are + * sized from it at start. Buffers existing is not sufficient as + * VIDIOC_REMOVE_BUFS can remove them all whilst streaming. + */ +static bool dst_fmt_locked(struct vb2_queue *vq) +{ + return vb2_is_busy(vq) || vb2_is_streaming(vq); +} + /* * get dst format - sets it to default if otherwise unset - * default is not updated whilst CAPTURE buffers exist as they were sized - * from it + * default is not updated whilst the dst format is locked * returns a pointer to the struct as a convenience * Must be called with ctx_mutex held */ @@ -325,7 +335,7 @@ static struct v4l2_pix_format_mplane *get_dst_fmt(struct hevc_d_ctx *const ctx) { struct vb2_queue *vq = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx); - if (!ctx->dst_fmt_set && !vb2_is_busy(vq)) + if (!ctx->dst_fmt_set && !dst_fmt_locked(vq)) ctx->dst_fmt = hevc_d_hevc_default_dst_fmt(ctx); return &ctx->dst_fmt; } @@ -395,7 +405,7 @@ static int hevc_d_s_fmt_vid_cap(struct file *file, void *priv, guard(mutex)(&ctx->ctx_mutex); vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); - if (vb2_is_busy(vq)) + if (dst_fmt_locked(vq)) return -EBUSY; ret = hevc_d_try_fmt_vid_cap(file, priv, f); From 8a3dddb67d81e30214dbf10889bfba039469d39c Mon Sep 17 00:00:00 2001 From: John Cox Date: Fri, 20 Nov 2026 10:38:00 +0000 Subject: [PATCH 15/18] media/hevc_d: Use vb2_plane_size when checking plane sizes Setup checks the vb2_v4l2_buffer plane length member which may not be correct for dmabufs as that is just what the user passed not the actual size - use vb2_plane_size on the vb2_buf instead Signed-off-by: John Cox --- .../platform/raspberrypi/hevc_dec/hevc_d_h265.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index 3e79fb4e763eae..f4141a82f43a0b 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1692,12 +1692,12 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) run->dst->vb2_buf.num_planes); goto fail; } - if (run->dst->planes[0].length < ctx->dst_fmt.plane_fmt[0].sizeimage || - run->dst->planes[1].length < ctx->dst_fmt.plane_fmt[1].sizeimage) { + if (vb2_plane_size(&run->dst->vb2_buf, 0) < ctx->dst_fmt.plane_fmt[0].sizeimage || + vb2_plane_size(&run->dst->vb2_buf, 1) < ctx->dst_fmt.plane_fmt[1].sizeimage) { v4l2_warn(&dev->v4l2_dev, - "Capture planes length (%d/%d) < sizeimage (%d/%d)\n", - run->dst->planes[0].length, - run->dst->planes[1].length, + "Capture planes length (%lu/%lu) < sizeimage (%d/%d)\n", + vb2_plane_size(&run->dst->vb2_buf, 0), + vb2_plane_size(&run->dst->vb2_buf, 1), ctx->dst_fmt.plane_fmt[0].sizeimage, ctx->dst_fmt.plane_fmt[1].sizeimage); goto fail; @@ -1710,10 +1710,10 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) run->dst->vb2_buf.num_planes); goto fail; } - if (run->dst->planes[0].length < ctx->dst_fmt.plane_fmt[0].sizeimage) { + if (vb2_plane_size(&run->dst->vb2_buf, 0) < ctx->dst_fmt.plane_fmt[0].sizeimage) { v4l2_warn(&dev->v4l2_dev, - "Capture planes length (%d) < sizeimage (%d)\n", - run->dst->planes[0].length, + "Capture planes length (%lu) < sizeimage (%d)\n", + vb2_plane_size(&run->dst->vb2_buf, 0), ctx->dst_fmt.plane_fmt[0].sizeimage); goto fail; } From eb4e21bb028c49efe877f5c6259655e2165e6a0b Mon Sep 17 00:00:00 2001 From: John Cox Date: Fri, 20 Nov 2026 11:03:57 +0000 Subject: [PATCH 16/18] media/hevc_d: Fix behaviour if unbound whilst running Separate unregister and device release functions so we behave correctly if unbound whilst running. Signed-off-by: John Cox --- .../platform/raspberrypi/hevc_dec/hevc_d.c | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c index b716b90070e436..2d80fd6f9cb6f3 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c @@ -198,13 +198,39 @@ static const struct v4l2_file_operations hevc_d_fops = { .mmap = v4l2_m2m_fop_mmap, }; +/* + * Separate device release from unregister as unbind doesn't shut everything + * down. On unbind this may happen some time after unregister. + */ +static void hevc_d_video_device_release(struct video_device *vfd) +{ + struct hevc_d_dev *dev = container_of(vfd, struct hevc_d_dev, vfd); + + v4l2_device_unregister(&dev->v4l2_dev); + v4l2_m2m_release(dev->m2m_dev); + media_device_cleanup(&dev->mdev); + kfree(dev); +} + +static void hevc_d_unregister(struct hevc_d_dev *dev) +{ + /* Stop new jobs being started - never resumed */ + v4l2_m2m_suspend(dev->m2m_dev); + /* Wait for any work still in flight */ + hevc_d_h265_sync(dev); + /* H/w now idle so we can shut it down */ + hevc_d_hw_remove(dev); + /* Must be last - drops what may be the final ref to dev */ + video_unregister_device(&dev->vfd); +} + static const struct video_device hevc_d_video_device = { .name = HEVC_D_NAME, .vfl_dir = VFL_DIR_M2M, .fops = &hevc_d_fops, .ioctl_ops = &hevc_d_ioctl_ops, .minor = -1, - .release = video_device_release_empty, + .release = hevc_d_video_device_release, .device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING, }; @@ -223,7 +249,7 @@ static int hevc_d_probe(struct platform_device *pdev) struct video_device *vfd; int ret; - dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); + dev = kzalloc_obj(*dev); if (!dev) return -ENOMEM; @@ -235,7 +261,7 @@ static int hevc_d_probe(struct platform_device *pdev) ret = hevc_d_hw_probe(dev); if (ret) { dev_err_probe(&pdev->dev, ret, "Failed to probe hardware\n"); - return ret; + goto err_free; } ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); @@ -303,7 +329,9 @@ static int hevc_d_probe(struct platform_device *pdev) err_m2m_mc: v4l2_m2m_unregister_media_controller(dev->m2m_dev); err_video: - video_unregister_device(&dev->vfd); + hevc_d_unregister(dev); + return ret; + err_m2m: media_device_cleanup(&dev->mdev); v4l2_m2m_release(dev->m2m_dev); @@ -311,6 +339,8 @@ static int hevc_d_probe(struct platform_device *pdev) v4l2_device_unregister(&dev->v4l2_dev); err_hw: hevc_d_hw_remove(dev); +err_free: + kfree(dev); return ret; } @@ -321,13 +351,7 @@ static void hevc_d_remove(struct platform_device *pdev) media_device_unregister(&dev->mdev); v4l2_m2m_unregister_media_controller(dev->m2m_dev); - media_device_cleanup(&dev->mdev); - - v4l2_m2m_release(dev->m2m_dev); - video_unregister_device(&dev->vfd); - v4l2_device_unregister(&dev->v4l2_dev); - - hevc_d_hw_remove(dev); + hevc_d_unregister(dev); } static int hevc_d_runtime_suspend(struct device *d) From 9255d04104b459ad678756ca1115ad6741335e79 Mon Sep 17 00:00:00 2001 From: John Cox Date: Fri, 20 Nov 2026 11:40:13 +0000 Subject: [PATCH 17/18] media/hevc_d: try_fmt_vid_cap uses fmt from SPS if unmatched Pick a format that matches the SPS (if set) in hevc_d_try_fmt_vid_cap if what the user set is unsupported or bad rather than leaving bad and always fixing up to NV12MT_COL128 Signed-off-by: John Cox --- drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c index 78376aff7a5d36..e728397b53b798 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -375,6 +375,8 @@ static int hevc_d_try_fmt_vid_cap(struct file *file, void *priv, if (f->fmt.pix_mp.pixelformat == pixelformat) break; } + if (!pixelformat) + f->fmt.pix_mp.pixelformat = pixelformat_from_sps(sps, 0); /* * We don't have any way of finding out colourspace so believe @@ -383,7 +385,6 @@ static int hevc_d_try_fmt_vid_cap(struct file *file, void *priv, if (f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_DEFAULT) copy_color(&f->fmt.pix_mp, &ctx->src_fmt); - f->fmt.pix_mp.pixelformat = pixelformat; hevc_d_prepare_dst_format(&f->fmt.pix_mp); return 0; } From 253fe73ac79f657de24963b3aa0286fc20c3ee6d Mon Sep 17 00:00:00 2001 From: John Cox Date: Wed, 25 Nov 2026 18:16:17 +0000 Subject: [PATCH 18/18] media/hevc_d: Change warning messages to debug for control misconfiguration It is possible to flood the log with warning messages by setting bad values in SPS, PPS etc. It is valuable to have this info when debugging so reduce the level to debug. Reported-by: Roy Closes: https://github.com/raspberrypi/linux/pull/7796 Signed-off-by: John Cox --- .../raspberrypi/hevc_dec/hevc_d_h265.c | 144 +++++++++--------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index f4141a82f43a0b..9a9f9444d7e0ba 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c @@ -1601,7 +1601,7 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) /* Frame start */ if (!is_sps_set(run->h265.sps)) { - v4l2_warn(&dev->v4l2_dev, "SPS never set\n"); + hevc_d_dbg(1, &dev->v4l2_dev, "SPS never set\n"); goto fail; } /* Can't check for PPS easily as all 0's looks valid */ @@ -1658,29 +1658,29 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) if (s->sps.bit_depth_luma_minus8 == 0) { if (ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12MT_COL128 && ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12_COL128) { - v4l2_err(&dev->v4l2_dev, - "Pixel format %#x != NV12MT_COL128 for 8-bit output", - ctx->dst_fmt.pixelformat); + hevc_d_dbg(1, &dev->v4l2_dev, + "Pixel format %#x != NV12MT_COL128 for 8-bit output\n", + ctx->dst_fmt.pixelformat); goto fail; } } else { if (ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12MT_10_COL128 && ctx->dst_fmt.pixelformat != V4L2_PIX_FMT_NV12_10_COL128) { - v4l2_err(&dev->v4l2_dev, - "Pixel format %#x != NV12MT_10_COL128 for 10-bit output", - ctx->dst_fmt.pixelformat); + hevc_d_dbg(1, &dev->v4l2_dev, + "Pixel format %#x != NV12MT_10_COL128 for 10-bit output\n", + ctx->dst_fmt.pixelformat); goto fail; } } if (s->sps.pic_width_in_luma_samples > ctx->dst_fmt.width || s->sps.pic_height_in_luma_samples > ctx->dst_fmt.height) { - v4l2_warn(&dev->v4l2_dev, - "SPS size (%dx%d) > capture size (%d,%d)\n", - s->sps.pic_width_in_luma_samples, - s->sps.pic_height_in_luma_samples, - ctx->dst_fmt.width, - ctx->dst_fmt.height); + hevc_d_dbg(1, &dev->v4l2_dev, + "SPS size (%dx%d) > capture size (%d,%d)\n", + s->sps.pic_width_in_luma_samples, + s->sps.pic_height_in_luma_samples, + ctx->dst_fmt.width, + ctx->dst_fmt.height); goto fail; } @@ -1688,33 +1688,33 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) case V4L2_PIX_FMT_NV12MT_COL128: case V4L2_PIX_FMT_NV12MT_10_COL128: if (run->dst->vb2_buf.num_planes != 2) { - v4l2_warn(&dev->v4l2_dev, "Capture planes (%d) != 2\n", - run->dst->vb2_buf.num_planes); + hevc_d_dbg(1, &dev->v4l2_dev, "Capture planes (%d) != 2\n", + run->dst->vb2_buf.num_planes); goto fail; } if (vb2_plane_size(&run->dst->vb2_buf, 0) < ctx->dst_fmt.plane_fmt[0].sizeimage || vb2_plane_size(&run->dst->vb2_buf, 1) < ctx->dst_fmt.plane_fmt[1].sizeimage) { - v4l2_warn(&dev->v4l2_dev, - "Capture planes length (%lu/%lu) < sizeimage (%d/%d)\n", - vb2_plane_size(&run->dst->vb2_buf, 0), - vb2_plane_size(&run->dst->vb2_buf, 1), - ctx->dst_fmt.plane_fmt[0].sizeimage, - ctx->dst_fmt.plane_fmt[1].sizeimage); + hevc_d_dbg(1, &dev->v4l2_dev, + "Capture planes length (%lu/%lu) < sizeimage (%d/%d)\n", + vb2_plane_size(&run->dst->vb2_buf, 0), + vb2_plane_size(&run->dst->vb2_buf, 1), + ctx->dst_fmt.plane_fmt[0].sizeimage, + ctx->dst_fmt.plane_fmt[1].sizeimage); goto fail; } break; case V4L2_PIX_FMT_NV12_COL128: case V4L2_PIX_FMT_NV12_10_COL128: if (run->dst->vb2_buf.num_planes != 1) { - v4l2_warn(&dev->v4l2_dev, "Capture planes (%d) != 1\n", - run->dst->vb2_buf.num_planes); + hevc_d_dbg(1, &dev->v4l2_dev, "Capture planes (%d) != 1\n", + run->dst->vb2_buf.num_planes); goto fail; } if (vb2_plane_size(&run->dst->vb2_buf, 0) < ctx->dst_fmt.plane_fmt[0].sizeimage) { - v4l2_warn(&dev->v4l2_dev, - "Capture planes length (%lu) < sizeimage (%d)\n", - vb2_plane_size(&run->dst->vb2_buf, 0), - ctx->dst_fmt.plane_fmt[0].sizeimage); + hevc_d_dbg(1, &dev->v4l2_dev, + "Capture planes length (%lu) < sizeimage (%d)\n", + vb2_plane_size(&run->dst->vb2_buf, 0), + ctx->dst_fmt.plane_fmt[0].sizeimage); goto fail; } break; @@ -1764,24 +1764,24 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) * checked before use. */ if (sh->slice_segment_addr >= s->ctb_size) { - v4l2_warn(&dev->v4l2_dev, - "Slice %u: segment addr %u >= pic size in CTBs %u\n", - i, sh->slice_segment_addr, s->ctb_size); + hevc_d_dbg(1, &dev->v4l2_dev, + "Slice %u: segment addr %u >= pic size in CTBs %u\n", + i, sh->slice_segment_addr, s->ctb_size); goto fail; } if (old_bits && sh->bit_size <= 8 * (u64)sh->data_byte_offset) { - v4l2_warn(&dev->v4l2_dev, - "data_byte_offset %u * 8 >= bits %u\n", - sh->data_byte_offset, sh->bit_size); + hevc_d_dbg(1, &dev->v4l2_dev, + "data_byte_offset %u * 8 >= bits %u\n", + sh->data_byte_offset, sh->bit_size); goto fail; } if ((u64)sh->data_byte_offset + byte_size > run->src->planes[0].bytesused) { - v4l2_warn(&dev->v4l2_dev, - "data_byte_offset %u + bits %u (= %u bytes) > bytesused %u\n", - sh->data_byte_offset, bit_size, byte_size, - run->src->planes[0].bytesused); + hevc_d_dbg(1, &dev->v4l2_dev, + "data_byte_offset %u + bits %u (= %u bytes) > bytesused %u\n", + sh->data_byte_offset, bit_size, byte_size, + run->src->planes[0].bytesused); goto fail; } /* BFNUM (data_len) includes the byte with rbsp_stop_one_bit which is not @@ -1827,9 +1827,9 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) /* Calc all the random coord info to avoid repeated conversion in/out */ s->start_ts = s->ctb_addr_rs_to_ts[sh->slice_segment_addr]; if (i != 0 && s->start_ts <= prev_start_ts) { - v4l2_warn(&dev->v4l2_dev, - "Slice %u: segment addr %u not after previous slice\n", - i, sh->slice_segment_addr); + hevc_d_dbg(1, &dev->v4l2_dev, + "Slice %u: segment addr %u not after previous slice\n", + i, sh->slice_segment_addr); goto fail; } prev_start_ts = s->start_ts; @@ -1884,9 +1884,9 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) "Missing unused DPB ent %d, timestamp=%lld\n", i, (long long)dec->dpb[i].timestamp); else - v4l2_warn(&dev->v4l2_dev, - "Missing inuse DPB ent %d, timestamp=%lld\n", - i, (long long)dec->dpb[i].timestamp); + hevc_d_dbg(1, &dev->v4l2_dev, + "Missing inuse DPB ent %d, timestamp=%lld\n", + i, (long long)dec->dpb[i].timestamp); de->ref_slots[i] = lkg_slot; continue; } @@ -1894,10 +1894,10 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) if (s->use_aux) { dpb_q_aux[i] = aux_q_ref_idx(ctx, buf->index); if (!dpb_q_aux[i]) - v4l2_warn(&dev->v4l2_dev, - "Missing DPB AUX ent %d, timestamp=%lld, index=%d\n", - i, (long long)dec->dpb[i].timestamp, - buf->index); + hevc_d_dbg(1, &dev->v4l2_dev, + "Missing DPB AUX ent %d, timestamp=%lld, index=%d\n", + i, (long long)dec->dpb[i].timestamp, + buf->index); } de->ref_slots[i] = buf->index; @@ -2474,25 +2474,25 @@ static int try_ctrl_sps(struct v4l2_ctrl *ctrl) sps->log2_diff_max_min_luma_transform_block_size; if (sps->chroma_format_idc != 1) { - v4l2_warn(&dev->v4l2_dev, - "Chroma format (%d) unsupported\n", - sps->chroma_format_idc); + hevc_d_dbg(1, &dev->v4l2_dev, + "Chroma format (%d) unsupported\n", + sps->chroma_format_idc); return -EINVAL; } if (sps->bit_depth_luma_minus8 != 0 && sps->bit_depth_luma_minus8 != 2) { - v4l2_warn(&dev->v4l2_dev, - "Luma depth (%d) unsupported\n", - sps->bit_depth_luma_minus8 + 8); + hevc_d_dbg(1, &dev->v4l2_dev, + "Luma depth (%d) unsupported\n", + sps->bit_depth_luma_minus8 + 8); return -EINVAL; } if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8) { - v4l2_warn(&dev->v4l2_dev, - "Chroma depth (%d) != Luma depth (%d)\n", - sps->bit_depth_chroma_minus8 + 8, - sps->bit_depth_luma_minus8 + 8); + hevc_d_dbg(1, &dev->v4l2_dev, + "Chroma depth (%d) != Luma depth (%d)\n", + sps->bit_depth_chroma_minus8 + 8, + sps->bit_depth_luma_minus8 + 8); return -EINVAL; } @@ -2500,10 +2500,10 @@ static int try_ctrl_sps(struct v4l2_ctrl *ctrl) sps->pic_height_in_luma_samples < HEVC_D_MIN_HEIGHT || sps->pic_width_in_luma_samples > HEVC_D_MAX_WIDTH || sps->pic_height_in_luma_samples > HEVC_D_MAX_HEIGHT) { - v4l2_warn(&dev->v4l2_dev, - "Bad sps width (%u) x height (%u)\n", - sps->pic_width_in_luma_samples, - sps->pic_height_in_luma_samples); + hevc_d_dbg(1, &dev->v4l2_dev, + "Bad sps width (%u) x height (%u)\n", + sps->pic_width_in_luma_samples, + sps->pic_height_in_luma_samples); return -EINVAL; } @@ -2575,8 +2575,8 @@ static int try_ctrl_pps(struct v4l2_ctrl *ctrl) V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED) && (pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED) && (pps->num_tile_columns_minus1 || pps->num_tile_rows_minus1)) { - v4l2_warn(&dev->v4l2_dev, - "WPP + Tiles not supported\n"); + hevc_d_dbg(1, &dev->v4l2_dev, + "WPP + Tiles not supported\n"); return -EINVAL; } @@ -2585,7 +2585,7 @@ static int try_ctrl_pps(struct v4l2_ctrl *ctrl) ARRAY_SIZE(pps->column_width_minus1) || pps->num_tile_rows_minus1 >= ARRAY_SIZE(pps->row_height_minus1))) { - v4l2_warn(&dev->v4l2_dev, "Tiles cols/rows too big\n"); + hevc_d_dbg(1, &dev->v4l2_dev, "Tiles cols/rows too big\n"); return -EINVAL; } @@ -2611,30 +2611,30 @@ static int try_ctrl_slice_params(struct v4l2_ctrl *ctrl) * slice_segment_address for slice 0 */ if (sh->slice_segment_addr != 0) { - v4l2_warn(&dev->v4l2_dev, "New frame but segment_addr=%d\n", - sh->slice_segment_addr); + hevc_d_dbg(1, &dev->v4l2_dev, "New frame but segment_addr=%d\n", + sh->slice_segment_addr); return -EINVAL; } /* The first slice segment of a picture cannot be dependent */ if (sh->flags & V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT) { - v4l2_warn(&dev->v4l2_dev, - "New frame but dependent slice segment\n"); + hevc_d_dbg(1, &dev->v4l2_dev, + "New frame but dependent slice segment\n"); return -EINVAL; } for (i = 0; i != ctrl->new_elems; ++i, ++sh) { if (!sh->bit_size) { - v4l2_warn(&dev->v4l2_dev, "Slice %u: bit size zero\n", i); + hevc_d_dbg(1, &dev->v4l2_dev, "Slice %u: bit size zero\n", i); return -EINVAL; } if (sh->slice_type != HEVC_SLICE_B && sh->slice_type != HEVC_SLICE_P && sh->slice_type != HEVC_SLICE_I) { - v4l2_warn(&dev->v4l2_dev, - "Slice %u: bad slice type %u\n", - i, sh->slice_type); + hevc_d_dbg(1, &dev->v4l2_dev, + "Slice %u: bad slice type %u\n", + i, sh->slice_type); return -EINVAL; }