diff --git a/drivers/gpu/drm/bridge/tc358762.c b/drivers/gpu/drm/bridge/tc358762.c index 941d0b7a14dbe0..2fb44253df23b6 100644 --- a/drivers/gpu/drm/bridge/tc358762.c +++ b/drivers/gpu/drm/bridge/tc358762.c @@ -204,6 +204,8 @@ static void tc358762_pre_enable(struct drm_bridge *bridge, ret = tc358762_init(ctx); if (ret < 0) dev_err(ctx->dev, "error initializing bridge (%d)\n", ret); + + ctx->pre_enabled = true; } static int tc358762_attach(struct drm_bridge *bridge, diff --git a/drivers/gpu/drm/vc4/vc4_plane.c b/drivers/gpu/drm/vc4/vc4_plane.c index b4a8ff3445450e..6879b249bd07ad 100644 --- a/drivers/gpu/drm/vc4/vc4_plane.c +++ b/drivers/gpu/drm/vc4/vc4_plane.c @@ -2977,7 +2977,7 @@ int vc4_plane_create_additional_planes(struct drm_device *drm) * 1 cursor plane on a crtc. */ drm_plane_create_zpos_property(plane, i + 1, 1, - VC4_NUM_OVERLAY_PLANES + 1); + VC4_NUM_TXP_OVERLAY_PLANES + 1); } drm_for_each_crtc(crtc, drm) { diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c index 70fd83a5330e5e..2d80fd6f9cb6f3 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.c @@ -11,10 +11,10 @@ * Copyright (C) 2018 Bootlin */ +#include #include #include #include -#include #include #include @@ -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; @@ -226,8 +260,8 @@ static int hevc_d_probe(struct platform_device *pdev) ret = hevc_d_hw_probe(dev); if (ret) { - dev_err(&pdev->dev, "Failed to probe hardware - %d\n", ret); - return ret; + dev_err_probe(&pdev->dev, ret, "Failed to probe hardware\n"); + 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) @@ -392,7 +419,7 @@ static struct platform_driver hevc_d_driver = { .remove = hevc_d_remove, .driver = { .name = HEVC_D_NAME, - .of_match_table = of_match_ptr(hevc_d_dt_match), + .of_match_table = hevc_d_dt_match, .pm = pm_ptr(&hevc_d_pm_ops), }, }; diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d.h index 5729b04475871d..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; @@ -137,12 +137,17 @@ struct hevc_d_ctx { size_t pu_size_max; size_t coeff_size_max; - /* Spinlock protecting aux_free */ - spinlock_t aux_lock; + /* Aux structures only used in setup so no locking needed */ struct hevc_d_q_aux *aux_free; - struct hevc_d_q_aux *aux_ents[HEVC_D_AUX_ENT_COUNT]; + struct hevc_d_slot { + u32 refybase; + u32 refcbase; + u32 colbase; + u32 poc; + } slots[HEVC_D_AUX_ENT_COUNT]; + unsigned int colmv_stride; unsigned int colmv_picsize; }; @@ -175,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 a902860ac655d0..595136fb2b7bc3 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 @@ -136,6 +137,8 @@ static size_t next_size(const size_t x) struct hevc_d_q_aux { unsigned int refcount; unsigned int q_index; + /* Is this aux entry set correctly? Only set on release or in P2 */ + bool good; struct hevc_d_q_aux *next; struct hevc_d_hwbuf col; }; @@ -191,12 +194,13 @@ struct hevc_d_dec_env { unsigned int luma_stride; dma_addr_t frame_chroma_addr; unsigned int chroma_stride; - dma_addr_t ref_addrs[16][2]; - struct hevc_d_q_aux *frame_aux; - struct hevc_d_q_aux *col_aux; + u32 mvbase; + + unsigned int frame_slot; + unsigned int ref_slots[16]; - dma_addr_t pu_base_vc; - dma_addr_t coeff_base_vc; + dma_addr_t pu_base_addr; + dma_addr_t coeff_base_addr; u32 pu_stride; u32 coeff_stride; @@ -350,10 +354,6 @@ static struct hevc_d_q_aux *aux_q_alloc(struct hevc_d_ctx *const ctx, DMA_ATTR_FORCE_CONTIGUOUS | DMA_ATTR_NO_KERNEL_MAPPING)) goto fail; - /* - * Spinlock not required as called in P0 only and - * aux checks done by _new - */ aq->refcount = 1; aq->q_index = q_index; ctx->aux_ents[q_index] = aq; @@ -368,9 +368,7 @@ static struct hevc_d_q_aux *aux_q_new(struct hevc_d_ctx *const ctx, const unsigned int q_index) { struct hevc_d_q_aux *aq; - unsigned long lockflags; - spin_lock_irqsave(&ctx->aux_lock, lockflags); /* * If we already have this allocated to a slot then use that * and assume that it will all work itself out in the pipeline @@ -388,7 +386,6 @@ static struct hevc_d_q_aux *aux_q_new(struct hevc_d_ctx *const ctx, ctx->aux_ents[q_index] = aq; } } - spin_unlock_irqrestore(&ctx->aux_lock, lockflags); if (!aq) aq = aux_q_alloc(ctx, q_index); @@ -397,30 +394,14 @@ static struct hevc_d_q_aux *aux_q_new(struct hevc_d_ctx *const ctx, } static struct hevc_d_q_aux *aux_q_ref_idx(struct hevc_d_ctx *const ctx, - const int q_index) + const unsigned int q_index) { - unsigned long lockflags; struct hevc_d_q_aux *aq; - spin_lock_irqsave(&ctx->aux_lock, lockflags); aq = ctx->aux_ents[q_index]; if (aq) ++aq->refcount; - spin_unlock_irqrestore(&ctx->aux_lock, lockflags); - - return aq; -} - -static struct hevc_d_q_aux *aux_q_ref(struct hevc_d_ctx *const ctx, - struct hevc_d_q_aux *const aq) -{ - unsigned long lockflags; - if (aq) { - spin_lock_irqsave(&ctx->aux_lock, lockflags); - ++aq->refcount; - spin_unlock_irqrestore(&ctx->aux_lock, lockflags); - } return aq; } @@ -428,26 +409,23 @@ static void aux_q_release(struct hevc_d_ctx *const ctx, struct hevc_d_q_aux **const paq) { struct hevc_d_q_aux *const aq = *paq; - unsigned long lockflags; if (!aq) return; *paq = NULL; - spin_lock_irqsave(&ctx->aux_lock, lockflags); if (--aq->refcount == 0) { aq->next = ctx->aux_free; ctx->aux_free = aq; ctx->aux_ents[aq->q_index] = NULL; aq->q_index = ~0U; + aq->good = false; } - spin_unlock_irqrestore(&ctx->aux_lock, lockflags); } static void aux_q_init(struct hevc_d_ctx *const ctx) { - spin_lock_init(&ctx->aux_lock); ctx->aux_free = NULL; } @@ -1101,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; @@ -1120,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 */ @@ -1443,9 +1421,6 @@ static void dec_env_delete(struct hevc_d_dec_env *const de) struct hevc_d_ctx * const ctx = de->ctx; unsigned long lock_flags; - aux_q_release(ctx, &de->frame_aux); - aux_q_release(ctx, &de->col_aux); - spin_lock_irqsave(&ctx->dec_lock, lock_flags); de->state = HEVC_D_DECODE_END; @@ -1476,7 +1451,7 @@ static int dec_env_init(struct hevc_d_ctx *const ctx) ctx->dec_pool = kzalloc_objs(*ctx->dec_pool, HEVC_D_DEC_ENV_COUNT); if (!ctx->dec_pool) - return -1; + return -ENOMEM; spin_lock_init(&ctx->dec_lock); @@ -1499,7 +1474,7 @@ static int dec_env_init(struct hevc_d_ctx *const ctx) fail: dec_env_uninit(ctx); - return -1; + return -ENOMEM; } /* @@ -1593,13 +1568,14 @@ 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; - bool slice_temporal_mvp; unsigned int ctb_size_y; bool sps_changed = false; /* Old (downstream only) bit size meanings */ bool old_bits = false; + unsigned int lkg_slot; de = dec_env_new(ctx); if (!de) { @@ -1610,13 +1586,22 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) s->sh = NULL; /* Avoid use until in the slice loop */ - slice_temporal_mvp = (sh0->flags & + 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 */ @@ -1628,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 */ @@ -1647,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: @@ -1672,34 +1655,32 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) break; } - de->frame_aux = NULL; - 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; } @@ -1707,53 +1688,38 @@ 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; } - /* - * Fill in ref planes with our address s.t. if we mess up refs - * somehow then we still have a valid address entry - */ - for (i = 0; i != 16; ++i) { - de->ref_addrs[i][0] = de->frame_luma_addr; - de->ref_addrs[i][1] = de->frame_chroma_addr; - } - - /* - * Stash initial temporal_mvp flag - * This must be the same for all pic slices (7.4.7.1) - */ - s->slice_temporal_mvp = slice_temporal_mvp; - /* * Need Aux ents for all (ref) DPB ents if temporal MV could * be enabled for any pic @@ -1768,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; @@ -1799,29 +1764,29 @@ 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 * part of slice_segment_data but is all but certain to be in the input - * stream so add that when calulating the value we need, but limit to the + * stream so add that when calculating the value we need, but limit to the * actual size of the buffer (which may well be what is used to set * bit_size if the caller isn't being very pedantic). */ @@ -1861,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 */ @@ -1896,6 +1868,13 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) goto fail; } + /* + * Find all the references. If any are missing the fill with the + * nearest valid entry. If nothing valid then use the buffer we + * are decoding into as a last resort. Ensure everything contains + * something valid so no checking is required in P2 + */ + lkg_slot = de->frame_slot; for (i = 0; i < dec->num_active_dpb_entries; ++i) { struct vb2_buffer *buf = vb2_find_buffer(vq, dec->dpb[i].timestamp); @@ -1905,33 +1884,33 @@ 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; } if (s->use_aux) { - int buffer_index = buf->index; - - dpb_q_aux[i] = aux_q_ref_idx(ctx, buffer_index); + 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, - buffer_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_addrs[i][0] = - vb2_dma_contig_plane_dma_addr(buf, 0); - if (ctx->dst_fmt.pixelformat == V4L2_PIX_FMT_NV12MT_COL128 || - ctx->dst_fmt.pixelformat == V4L2_PIX_FMT_NV12MT_10_COL128) - de->ref_addrs[i][1] = - vb2_dma_contig_plane_dma_addr(buf, 1); - else - de->ref_addrs[i][1] = de->ref_addrs[i][0] + - (ctx->dst_fmt.height * 128); + de->ref_slots[i] = buf->index; + if (lkg_slot == de->frame_slot) { + unsigned int j; + + for (j = 0; j != i; ++j) + de->ref_slots[j] = buf->index; + } + lkg_slot = buf->index; } + for (; i != 16; ++i) + de->ref_slots[i] = lkg_slot; /* Move DPB from temp */ for (i = 0; i != V4L2_HEVC_DPB_ENTRIES_NUM_MAX; ++i) { @@ -1951,34 +1930,7 @@ static int hevc_d_h265_setup(struct hevc_d_ctx *ctx, struct hevc_d_run *run) goto fail; } - de->frame_aux = aux_q_ref(ctx, s->frame_aux); - } - - if (de->dpbno_col != ~0U) { - if (de->dpbno_col >= dec->num_active_dpb_entries) { - v4l2_err(&dev->v4l2_dev, - "Col ref index %d >= %d\n", - de->dpbno_col, - dec->num_active_dpb_entries); - } else { - /* Standard requires that the col pic is constant for - * the duration of the pic (text of collocated_ref_idx - * in H265-2 2018 7.4.7.1) - */ - - /* Spot the collocated ref in passing */ - de->col_aux = aux_q_ref(ctx, - dpb_q_aux[de->dpbno_col]); - - if (!de->col_aux) { - v4l2_warn(&dev->v4l2_dev, - "Missing DPB ent for col\n"); - /* Need to abort if this fails as P2 may - * explode on bad data - */ - goto fail; - } - } + de->mvbase = VC_ADDR(s->frame_aux->col.addr); } de->state = HEVC_D_DECODE_PHASE1; @@ -2044,31 +1996,86 @@ static void phase2_cb(struct hevc_d_dev *const dev, void *v) phase2_done(dev, v, VB2_BUF_STATE_DONE); } +static void phase2_err_claimed(struct hevc_d_dev *const dev, void *v) +{ + struct hevc_d_dec_env *const de = v; + struct hevc_d_ctx *const ctx = de->ctx; + struct hevc_d_slot *const slot = ctx->slots + de->frame_slot; + + slot->refybase = 0; + slot->refcbase = 0; + slot->colbase = 0; + slot->poc = de->rpi_currpoc; + + 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) +{ + const struct hevc_d_slot *slot = ctx->slots + de->ref_slots[dpb_no]; + const u32 poc = slot->poc; + u32 pocdiff = 0xffffffff; + unsigned int i; + + if (likely(slot->refybase)) + return slot; + + slot = NULL; + for (i = 0; i != 16; ++i) { + const struct hevc_d_slot *t = ctx->slots + de->ref_slots[i]; + u32 d = abs((s32)(poc - t->poc)); + + if (t->refybase && d < pocdiff) { + slot = t; + pocdiff = d; + } + } + return slot; +} + static void phase2_claimed(struct hevc_d_dev *const dev, void *v) { struct hevc_d_dec_env *const de = v; + struct hevc_d_ctx *const ctx = de->ctx; + struct hevc_d_slot *const slot = ctx->slots + de->frame_slot; + u32 colbase = 0; unsigned int i; - apb_write_vc_addr(dev, RPI_PURBASE, de->pu_base_vc); + slot->refybase = VC_ADDR(de->frame_luma_addr); + slot->refcbase = VC_ADDR(de->frame_chroma_addr); + slot->colbase = de->mvbase; + slot->poc = de->rpi_currpoc; + + apb_write_vc_addr(dev, RPI_PURBASE, de->pu_base_addr); apb_write_vc_len(dev, RPI_PURSTRIDE, de->pu_stride); - apb_write_vc_addr(dev, RPI_COEFFRBASE, de->coeff_base_vc); + apb_write_vc_addr(dev, RPI_COEFFRBASE, de->coeff_base_addr); apb_write_vc_len(dev, RPI_COEFFRSTRIDE, de->coeff_stride); apb_write_vc_addr(dev, RPI_OUTYBASE, de->frame_luma_addr); - apb_write_vc_addr(dev, RPI_OUTCBASE, de->frame_chroma_addr); apb_write_vc_len(dev, RPI_OUTYSTRIDE, de->luma_stride); + apb_write_vc_addr(dev, RPI_OUTCBASE, de->frame_chroma_addr); apb_write_vc_len(dev, RPI_OUTCSTRIDE, de->chroma_stride); for (i = 0; i < 16; i++) { /* Strides are in fact unused but fill in anyway */ unsigned int roff = i * RPI_REFREGS_SIZE; + const struct hevc_d_slot *ref = find_slot(de, ctx, i); + + if (!ref) + goto fail; + if (i == de->dpbno_col) + colbase = ref->colbase; - apb_write_vc_addr(dev, RPI_REFYBASE0 + roff, - de->ref_addrs[i][0]); + apb_write(dev, RPI_REFYBASE0 + roff, ref->refybase); apb_write_vc_len(dev, RPI_REFYSTRIDE0 + roff, de->luma_stride); - apb_write_vc_addr(dev, RPI_REFCBASE0 + roff, - de->ref_addrs[i][1]); + apb_write(dev, RPI_REFCBASE0 + roff, ref->refcbase); apb_write_vc_len(dev, RPI_REFCSTRIDE0 + roff, de->chroma_stride); } @@ -2078,23 +2085,21 @@ static void phase2_claimed(struct hevc_d_dev *const dev, void *v) apb_write(dev, RPI_CURRPOC, de->rpi_currpoc); /* collocated reads/writes */ - apb_write_vc_len(dev, RPI_COLSTRIDE, - de->ctx->colmv_stride); - apb_write_vc_len(dev, RPI_MVSTRIDE, - de->ctx->colmv_stride); - apb_write_vc_addr(dev, RPI_MVBASE, - !de->frame_aux ? 0 : de->frame_aux->col.addr); - apb_write_vc_addr(dev, RPI_COLBASE, - !de->col_aux ? 0 : de->col_aux->col.addr); + if (!colbase && de->dpbno_col != ~0U) + goto fail; + + apb_write_vc_len(dev, RPI_COLSTRIDE, ctx->colmv_stride); + apb_write(dev, RPI_COLBASE, colbase); + apb_write_vc_len(dev, RPI_MVSTRIDE, ctx->colmv_stride); + apb_write(dev, RPI_MVBASE, de->mvbase); hevc_d_hw_irq_active2_irq(dev, &de->irq_ent, phase2_cb, de); apb_write_final(dev, RPI_NUMROWS, de->pic_height_in_ctbs_y); -} + return; -static void phase2_err_claimed(struct hevc_d_dev *const dev, void *v) -{ - phase2_done(dev, v, VB2_BUF_STATE_ERROR); +fail: + phase2_err_claimed(dev, de); } static void phase1_claimed(struct hevc_d_dev *const dev, void *v); @@ -2205,11 +2210,11 @@ static void phase1_claimed(struct hevc_d_dev *const dev, void *v) if (ctx->fatal_err) goto fail; - de->pu_base_vc = pu_hwbuf->addr; + de->pu_base_addr = pu_hwbuf->addr; de->pu_stride = ALIGN_DOWN(pu_hwbuf->size / de->pic_height_in_ctbs_y, 64); - de->coeff_base_vc = coeff_hwbuf->addr; + de->coeff_base_addr = coeff_hwbuf->addr; de->coeff_stride = ALIGN_DOWN(coeff_hwbuf->size / de->pic_height_in_ctbs_y, 64); @@ -2217,9 +2222,9 @@ static void phase1_claimed(struct hevc_d_dev *const dev, void *v) * in cb_phase1 after error detection */ - apb_write_vc_addr(dev, RPI_PUWBASE, de->pu_base_vc); + apb_write_vc_addr(dev, RPI_PUWBASE, de->pu_base_addr); apb_write_vc_len(dev, RPI_PUWSTRIDE, de->pu_stride); - apb_write_vc_addr(dev, RPI_COEFFWBASE, de->coeff_base_vc); + apb_write_vc_addr(dev, RPI_COEFFWBASE, de->coeff_base_addr); apb_write_vc_len(dev, RPI_COEFFWSTRIDE, de->coeff_stride); /* Trigger command FIFO */ @@ -2260,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; }; @@ -2269,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) @@ -2293,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) @@ -2401,11 +2444,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) @@ -2413,8 +2463,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; @@ -2422,25 +2473,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; } @@ -2448,10 +2499,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; } @@ -2463,7 +2514,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; @@ -2480,6 +2540,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; } @@ -2497,17 +2574,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; } @@ -2533,18 +2610,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; } @@ -2573,7 +2662,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); @@ -2593,10 +2683,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, @@ -2604,7 +2692,22 @@ void hevc_d_device_run(void *priv) v4l2_m2m_buf_copy_metadata(run.src, run.dst); - 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_hw.c b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.c index b37c11c308a248..a65292af581f5b 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.c @@ -11,17 +11,9 @@ * Copyright (C) 2018 Bootlin */ #include -#include -#include #include -#include -#include -#include -#include #include #include -#include -#include #include #include diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h index 2c1e089e699c79..fae4474d00a3b1 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_hw.h @@ -14,6 +14,8 @@ #ifndef _HEVC_D_HW_H_ #define _HEVC_D_HW_H_ +#include + struct hevc_d_hw_irq_ent { struct hevc_d_hw_irq_ent *next; hevc_d_irq_callback cb; @@ -84,6 +86,8 @@ struct hevc_d_hw_irq_ent { /* Offset to get from REFYBASEn to REFYBASEn+1 */ #define RPI_REFREGS_SIZE 16 +#define VC_ADDR(x) ((u32)((x) >> 6)) + /* * Write a general register value * Order is unimportant @@ -123,14 +127,14 @@ static inline void apb_write_vc_addr(const struct hevc_d_dev * const dev, const unsigned int offset, const dma_addr_t a) { - apb_write(dev, offset, (u32)(a >> 6)); + apb_write(dev, offset, VC_ADDR(a)); } static inline void apb_write_vc_addr_final(const struct hevc_d_dev * const dev, const unsigned int offset, const dma_addr_t a) { - apb_write_final(dev, offset, (u32)(a >> 6)); + apb_write_final(dev, offset, VC_ADDR(a)); } static inline void apb_write_vc_len(const struct hevc_d_dev * const dev, 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 122d58ad55c001..391d31612f0da8 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.c @@ -11,7 +11,6 @@ * Copyright (C) 2018 Bootlin */ -#include #include #include #include @@ -43,7 +42,7 @@ size_t hevc_d_round_up_size(const size_t x) return x >= ((size_t)3 << n) ? (size_t)4 << n : ((size_t)3 << n); } -size_t hevc_d_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8) +static u32 bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8) { const size_t wxh = w * h; size_t bits_alloc; @@ -59,42 +58,39 @@ size_t hevc_d_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_min wxh * 3 / 8; /* Allow for bit depth */ bits_alloc += (bits_alloc * bits_minus8) / 8; - return hevc_d_round_up_size(bits_alloc); + return (u32)hevc_d_round_up_size(bits_alloc); } void hevc_d_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt) { - size_t size; - u32 w; - u32 h; + unsigned int width = pix_fmt->width; + unsigned int height = pix_fmt->height; + unsigned int sizeimage = pix_fmt->plane_fmt[0].sizeimage; + + if (!width) + width = HEVC_D_DEFAULT_WIDTH; + else + width = clamp(width, HEVC_D_MIN_WIDTH, HEVC_D_MAX_WIDTH); + if (!height) + height = HEVC_D_DEFAULT_HEIGHT; + else + height = clamp(height, HEVC_D_MIN_HEIGHT, HEVC_D_MAX_HEIGHT); + + /* If unspecified or way too big - pick max for size */ + if (!sizeimage || sizeimage > SZ_32M) + sizeimage = bit_buf_size(width, height, 2); - w = pix_fmt->width; - h = pix_fmt->height; - if (!w || !h) { - w = HEVC_D_DEFAULT_WIDTH; - h = HEVC_D_DEFAULT_HEIGHT; - } - if (w > HEVC_D_MAX_WIDTH) - w = HEVC_D_MAX_WIDTH; - if (h > HEVC_D_MAX_HEIGHT) - h = HEVC_D_MAX_HEIGHT; - - if (!pix_fmt->plane_fmt[0].sizeimage || - pix_fmt->plane_fmt[0].sizeimage > SZ_32M) { - /* Unspecified or way too big - pick max for size */ - size = hevc_d_bit_buf_size(w, h, 2); - } /* Set a minimum */ - size = max_t(u32, SZ_4K, pix_fmt->plane_fmt[0].sizeimage); + sizeimage = max(SZ_4K, sizeimage); pix_fmt->pixelformat = V4L2_PIX_FMT_HEVC_SLICE; - pix_fmt->width = w; - pix_fmt->height = h; + pix_fmt->width = width; + pix_fmt->height = height; pix_fmt->num_planes = 1; pix_fmt->field = V4L2_FIELD_NONE; /* Zero bytes per line for encoded source. */ pix_fmt->plane_fmt[0].bytesperline = 0; - pix_fmt->plane_fmt[0].sizeimage = size; + pix_fmt->plane_fmt[0].sizeimage = sizeimage; /* Zero all unused planes */ memset(pix_fmt->plane_fmt + pix_fmt->num_planes, 0, @@ -332,17 +328,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; } @@ -352,6 +359,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; } @@ -361,6 +370,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; } @@ -378,6 +389,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 @@ -386,7 +399,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; } @@ -405,8 +417,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); @@ -426,6 +440,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; @@ -479,41 +495,21 @@ static int hevc_d_queue_setup(struct vb2_queue *vq, unsigned int *nbufs, struct device *alloc_devs[]) { struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); - struct v4l2_pix_format_mplane *pix_fmt; - int expected_nplanes; + const struct v4l2_pix_format_mplane *pix_fmt; + unsigned int i; - if (V4L2_TYPE_IS_OUTPUT(vq->type)) { - pix_fmt = &ctx->src_fmt; - expected_nplanes = 1; - } else { - pix_fmt = get_dst_fmt(ctx); - expected_nplanes = 2; - } + pix_fmt = V4L2_TYPE_IS_OUTPUT(vq->type) ? &ctx->src_fmt : get_dst_fmt(ctx); if (*nplanes) { - if (pix_fmt->pixelformat == V4L2_PIX_FMT_NV12MT_COL128 || - pix_fmt->pixelformat == V4L2_PIX_FMT_NV12MT_10_COL128) { - if (*nplanes != expected_nplanes || - sizes[0] < pix_fmt->plane_fmt[0].sizeimage || - sizes[1] < pix_fmt->plane_fmt[1].sizeimage) - return -EINVAL; - } else { - if (sizes[0] < pix_fmt->plane_fmt[0].sizeimage) + if (*nplanes != pix_fmt->num_planes) + return -EINVAL; + for (i = 0; i != pix_fmt->num_planes; ++i) + if (sizes[i] < pix_fmt->plane_fmt[i].sizeimage) return -EINVAL; - } } else { - sizes[0] = pix_fmt->plane_fmt[0].sizeimage; - if (V4L2_TYPE_IS_OUTPUT(vq->type)) { - *nplanes = 1; - } else { - if (pix_fmt->pixelformat == V4L2_PIX_FMT_NV12MT_COL128 || - pix_fmt->pixelformat == V4L2_PIX_FMT_NV12MT_10_COL128) { - sizes[1] = pix_fmt->plane_fmt[1].sizeimage; - *nplanes = 2; - } else { - *nplanes = 1; - } - } + *nplanes = pix_fmt->num_planes; + for (i = 0; i != pix_fmt->num_planes; ++i) + sizes[i] = pix_fmt->plane_fmt[i].sizeimage; } return 0; @@ -523,6 +519,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)) @@ -533,9 +530,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); } } @@ -551,12 +552,11 @@ static int hevc_d_buf_prepare(struct vb2_buffer *vb) { struct vb2_queue *vq = vb->vb2_queue; struct hevc_d_ctx *ctx = vb2_get_drv_priv(vq); - struct v4l2_pix_format_mplane *pix_fmt; + struct v4l2_pix_format_mplane *pix_fmt = &ctx->dst_fmt; + /* If OUTPUT then we don't care about actual buffer size */ if (V4L2_TYPE_IS_OUTPUT(vq->type)) - pix_fmt = &ctx->src_fmt; - else - pix_fmt = &ctx->dst_fmt; + return 0; if (vb2_plane_size(vb, 0) < pix_fmt->plane_fmt[0].sizeimage || vb2_plane_size(vb, 1) < pix_fmt->plane_fmt[1].sizeimage) @@ -568,6 +568,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); @@ -576,7 +605,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; @@ -601,7 +631,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); } @@ -624,13 +659,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, @@ -666,7 +709,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; diff --git a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h index b0e4b2762b8e14..5a1ca7e4f06d46 100644 --- a/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h +++ b/drivers/media/platform/raspberrypi/hevc_dec/hevc_d_video.h @@ -24,7 +24,6 @@ extern const struct v4l2_ioctl_ops hevc_d_ioctl_ops; int hevc_d_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq); -size_t hevc_d_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8); size_t hevc_d_round_up_size(const size_t x); void hevc_d_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt);