diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c index 57a2a93e1abb14..2d80fd6f9cb6f3 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; @@ -190,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, }; @@ -215,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; @@ -227,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); @@ -236,10 +270,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); @@ -298,15 +329,18 @@ 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); err_v4l2: - mutex_destroy(&dev->dev_mutex); v4l2_device_unregister(&dev->v4l2_dev); err_hw: hevc_d_hw_remove(dev); +err_free: + kfree(dev); return ret; } @@ -317,14 +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); - mutex_destroy(&dev->dev_mutex); - v4l2_device_unregister(&dev->v4l2_dev); - - hevc_d_hw_remove(dev); + hevc_d_unregister(dev); } static int hevc_d_runtime_suspend(struct device *d) 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_h265.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_h265.c index e6a035e99ca46d..9a9f9444d7e0ba 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 @@ -1078,8 +1079,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 +1097,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 */ @@ -1567,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; @@ -1587,10 +1589,19 @@ 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)) { - 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 */ @@ -1602,7 +1613,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 */ @@ -1621,8 +1632,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 +1642,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 +1651,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; } @@ -1653,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; } @@ -1683,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 (run->dst->planes[0].length < ctx->dst_fmt.plane_fmt[0].sizeimage || - run->dst->planes[1].length < 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, - ctx->dst_fmt.plane_fmt[0].sizeimage, - 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) { + 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 (run->dst->planes[0].length < ctx->dst_fmt.plane_fmt[0].sizeimage) { - v4l2_warn(&dev->v4l2_dev, - "Capture planes length (%d) < sizeimage (%d)\n", - 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) { + 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; @@ -1729,7 +1734,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; @@ -1760,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 @@ -1822,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) { + 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; 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 */ @@ -1873,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; } @@ -1883,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; @@ -1999,6 +2010,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) @@ -2250,8 +2265,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; }; @@ -2259,8 +2273,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) @@ -2283,11 +2296,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) @@ -2392,11 +2445,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) @@ -2404,8 +2464,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; @@ -2413,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; } @@ -2439,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; } @@ -2454,7 +2515,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; @@ -2471,6 +2541,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; } @@ -2488,17 +2575,17 @@ 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; } 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"); + hevc_d_dbg(1, &dev->v4l2_dev, "Tiles cols/rows too big\n"); return -EINVAL; } @@ -2524,18 +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) { + 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) { + 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; } @@ -2564,7 +2663,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); @@ -2584,10 +2684,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, @@ -2595,7 +2693,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 */ 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 d0af990278cf40..e728397b53b798 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -314,17 +314,28 @@ 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 */ 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; } @@ -334,6 +345,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 +356,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; } @@ -360,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 @@ -368,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; } @@ -387,8 +403,10 @@ 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)) + if (dst_fmt_locked(vq)) return -EBUSY; ret = hevc_d_try_fmt_vid_cap(file, priv, f); @@ -408,6 +426,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; @@ -485,6 +505,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)) @@ -495,9 +516,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); } } @@ -529,6 +554,35 @@ 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; + + 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 +591,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 +617,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); } @@ -585,13 +645,21 @@ 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(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); - v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); + media_request_manual_complete(req); } 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, @@ -629,7 +697,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;