Re: [PATCH v12 5/7] media: chips-media: wave5: Add the v4l2 layer

From: Nicolas Dufresne
Date: Tue Sep 26 2023 - 20:13:58 EST


Le vendredi 22 septembre 2023 à 09:33 +0200, Hans Verkuil a écrit :
> On 21/09/2023 21:11, Nicolas Dufresne wrote:
> > Le mercredi 20 septembre 2023 à 17:13 +0200, Hans Verkuil a écrit :
> > > On 15/09/2023 23:11, Sebastian Fricke wrote:
> > > > From: Nas Chung <nas.chung@xxxxxxxxxxxxxxx>
> > > >
> > > > Add the decoder and encoder implementing the v4l2
> > > > API. This patch also adds the Makefile and the VIDEO_WAVE_VPU config
> > > >
> > > > Signed-off-by: Sebastian Fricke <sebastian.fricke@xxxxxxxxxxxxx>
> > > > Signed-off-by: Nicolas Dufresne <nicolas.dufresne@xxxxxxxxxxxxx>
> > > > Signed-off-by: Robert Beckett <bob.beckett@xxxxxxxxxxxxx>
> > > > Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@xxxxxxxxxxxxx>
> > > > Signed-off-by: Nas Chung <nas.chung@xxxxxxxxxxxxxxx>
> > > > ---
> > > > drivers/media/platform/chips-media/Kconfig | 1 +
> > > > drivers/media/platform/chips-media/Makefile | 1 +
> > > > drivers/media/platform/chips-media/wave5/Kconfig | 12 +
> > > > drivers/media/platform/chips-media/wave5/Makefile | 10 +
> > > > .../platform/chips-media/wave5/wave5-helper.c | 196 ++
> > > > .../platform/chips-media/wave5/wave5-helper.h | 30 +
> > > > .../platform/chips-media/wave5/wave5-vpu-dec.c | 1965 ++++++++++++++++++++
> > > > .../platform/chips-media/wave5/wave5-vpu-enc.c | 1825 ++++++++++++++++++
> > > > .../media/platform/chips-media/wave5/wave5-vpu.c | 331 ++++
> > > > .../media/platform/chips-media/wave5/wave5-vpu.h | 83 +
> > > > 10 files changed, 4454 insertions(+)
> > > >
>
> <snip>
>
> > > > +static int wave5_vpu_dec_set_eos_on_firmware(struct vpu_instance *inst)
> > > > +{
> > > > + int ret;
> > > > +
> > > > + ret = wave5_vpu_dec_update_bitstream_buffer(inst, 0);
> > > > + if (ret) {
> > > > + dev_err(inst->dev->dev,
> > > > + "Setting EOS for the bitstream, fail: %d\n", ret);
> > >
> > > Is this an error due to a driver problem, or because a bad bitstream is
> > > fed from userspace? In the first case, dev_err would be right, in the
> > > second dev_dbg would be more appropriate. Bad userspace input should not
> > > spam the kernel log in general.
> >
> > Its the first. To set the EOS flag, a command is sent to the firmware. That
> > command may never return (timeout) or may report an error. For this specific
> > command, if that happens we are likely facing firmware of driver problem (or
> > both).
>
> OK, I'd add that as a comment here as this is unexpected behavior.
>
> >
> > >
> > > > + return ret;
> > > > + }
> > > > + return 0;
> > > > +}
>
> <snip>
>
> > > > +static int wave5_vpu_dec_create_bufs(struct file *file, void *priv,
> > > > + struct v4l2_create_buffers *create)
> > > > +{
> > > > + struct v4l2_format *f = &create->format;
> > > > +
> > > > + if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
> > > > + return -ENOTTY;
> > >
> > > Huh? Why is this needed?
> >
> > Minimally a comment should be added. The why is that we support CREATE_BUF for
> > OUTPUT queue (bitstream) but not for CAPTURE queues. This is simply not
> > supported by Wave5 firmware. Do you have any suggestion how this asymmetry can
> > be implemented better ?
>
> Certainly not with ENOTTY: the ioctl exists, it is just not supported for
> CAPTURE queues.
>
> How about -EPERM? And document this error as well in the VIDIOC_CREATE_BUFS
> documentation. And you want a dev_dbg here too.

The suggestion cannot be used since there is documentation for that one already,
and it does not match "unsupported".

"Permission denied. Can be returned if the device needs write permission, or
some special capabilities is needed (e. g. root)"

What about using the most logical error code, which name is actually obvious,
like ENOTSUP ?

#define ENOTSUPP 524 /* Operation is not supported */

>
> So I would propose that EPERM is returned if CREATE_BUFS is only supported
> for for one of the two queues of an M2M device.

Note that userspace does not care of the difference between an ioctl not being
implemented at all or not being implement for one queue. GStreamer have been
testing with both queue type for couple of years now. Adding this distinction is
just leaking an implementation details to userspace. I'm fine to just do what
you'd like, just stating the obvious that while it may look logical inside the
kernel, its a bit of a non-sense for our users.

regards,
Nicolas

>
> >
> > >
> > > > +
> > > > + return v4l2_m2m_ioctl_create_bufs(file, priv, create);
> > > > +}
>
> <snip>
>
> > > > +static int wave5_vpu_dec_queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
> > > > + unsigned int *num_planes, unsigned int sizes[],
> > > > + struct device *alloc_devs[])
> > > > +{
> > > > + struct vpu_instance *inst = vb2_get_drv_priv(q);
> > > > + struct v4l2_pix_format_mplane inst_format =
> > > > + (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ? inst->src_fmt : inst->dst_fmt;
> > > > + unsigned int i;
> > > > +
> > > > + dev_dbg(inst->dev->dev, "%s: num_buffers: %u | num_planes: %u | type: %u\n", __func__,
> > > > + *num_buffers, *num_planes, q->type);
> > > > +
> > > > + /* the CREATE_BUFS case */
> > > > + if (*num_planes) {
> > > > + if (inst_format.num_planes != *num_planes)
> > > > + return -EINVAL;
> > > > +
> > > > + for (i = 0; i < *num_planes; i++) {
> > > > + if (sizes[i] < inst_format.plane_fmt[i].sizeimage)
> > > > + return -EINVAL;
> > > > + }
> > > > + /* the REQBUFS case */
> > > > + } else {
> > > > + *num_planes = inst_format.num_planes;
> > > > +
> > > > + if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
> > > > + sizes[0] = inst_format.plane_fmt[0].sizeimage;
> > > > + dev_dbg(inst->dev->dev, "%s: size[0]: %u\n", __func__, sizes[0]);
> > > > + } else if (*num_planes == 1) {
> > > > + if (inst->output_format == FORMAT_422)
> > > > + sizes[0] = inst_format.width * inst_format.height * 2;
> > > > + else
> > > > + sizes[0] = inst_format.width * inst_format.height * 3 / 2;
> > > > + dev_dbg(inst->dev->dev, "%s: size[0]: %u\n", __func__, sizes[0]);
> > > > + } else if (*num_planes == 2) {
> > > > + sizes[0] = inst_format.width * inst_format.height;
> > > > + if (inst->output_format == FORMAT_422)
> > > > + sizes[1] = inst_format.width * inst_format.height;
> > > > + else
> > > > + sizes[1] = inst_format.width * inst_format.height / 2;
> > > > + dev_dbg(inst->dev->dev, "%s: size[0]: %u | size[1]: %u\n",
> > > > + __func__, sizes[0], sizes[1]);
> > > > + } else if (*num_planes == 3) {
> > > > + sizes[0] = inst_format.width * inst_format.height;
> > > > + if (inst->output_format == FORMAT_422) {
> > > > + sizes[1] = inst_format.width * inst_format.height / 2;
> > > > + sizes[2] = inst_format.width * inst_format.height / 2;
> > > > + } else {
> > > > + sizes[1] = inst_format.width * inst_format.height / 4;
> > > > + sizes[2] = inst_format.width * inst_format.height / 4;
> > > > + }
> > > > + dev_dbg(inst->dev->dev, "%s: size[0]: %u | size[1]: %u | size[2]: %u\n",
> > > > + __func__, sizes[0], sizes[1], sizes[2]);
> > > > + }
> > > > + }
> > > > +
> > > > + if (inst->state == VPU_INST_STATE_INIT_SEQ &&
> > > > + q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
> > > > + if (*num_buffers > inst->dst_buf_count &&
> > > > + *num_buffers < WAVE5_MAX_FBS)
> > > > + inst->dst_buf_count = *num_buffers;
> > >
> > > In the create_bufs case, *num_buffers is the number of buffers you are
> > > adding to the already existing buffers. Frankly, the logic here is
> > > dubious. I'm not sure what the intent is. Why do you need to keep track
> > > of the buf count at all when the vb2_queue already does that?
> >
> > This needs to be cleaned up. CREATE_BUFS case is not supported for capture, and
> > so there is no such weirdo case second calls for that queue at least. Meanwhile,
> > inst->dst_buf_count is used a MIN_BUFFERS_FOR_CAPTURE initially, and the number
> > of allocated buffer later. I think it would be better to simply store the min,
> > and use the queue to track the number of allocated buffers.
> >
> > In this diver, the reference frame are stored separately, and compressed. The
> > capture queue contains the display frame. There is a gap when comes time to
> > transfer timestamp, and for this reason we had to keep the two fbc_count equal.
> > We classified this as hardware issue and moved on.
> >
> > I think the dst_buf_count can be dropped now and the "*num_buffers > inst-
> > > dst_buf_count" not longer make any sense.
> >
> > >
> > > WAVE5_MAX_FBS == 32, which is VIDEO_MAX_FRAMES. You can just drop the check
> > > against WAVE5_MAX_FBS since the core ensures already it will never allocate
> > > more than VIDEO_MAX_FRAMES.
> > >
> > > I'm not sure why WAVE5_MAX_FBS is defined at all, when it is just equal to
> > > VIDEO_MAX_FRAMES. Perhaps it can be replaced everywhere with VIDEO_MAX_FRAMES?
> >
> > That is more challenging changes, since VIDEO_MAX_FRAMES is a software
> > limitation, but WAVE5_MAX_FBS is a hardware limitation. Buffer index only have 4
> > bits on this hardware. And the marking of frame being used for display is using
> > a 32bit flag. Considering there is effort to lift that software limitation, it
> > seems ill advised to completely stop ensuring this HW limit is respected.
>
> If there are only 4 bits for the buffer index, shouldn't WAVE5_MAX_FBS be 16? Or
> did you mean '5 bits'? Assuming that you meant '5 bits', then that makes
> WAVE5_MAX_FBS identical to VIDEO_MAX_FRAMES, but that is just luck, really.
>
> In any case, you should document at the place where WAVE5_MAX_FBS is defined that
> this is a hardware limitation, and not a random software limit.
>
> I also think that the DELETE_BUFS series should allow drivers to set max_num_buffers
> to a value less than 32 (currently the requirement is that it is at least 32).
>
> I saw a few more drivers that limit the number of buffers, usually based on the
> format (and so the buffer size) and some HW memory limitation. It might be interesting
> to allow drivers to change max_num_buffers on the fly (provided no buffers are
> allocated, of course). Limit checking is really something that vb2 should do, and
> not the driver.
>
> >
> > I'm open for suggestions.
> >
> > >
> > > > +
> > > > + *num_buffers = inst->dst_buf_count;
> > > > + }
> > > > +
> > > > + return 0;
> > > > +}
>
> Regards,
>
> Hans