Re: [RFC PATCH 07/28] media: mali-vpu: implement TRY_FMT and S_FMT ioctls

From: Charvi Mehta

Date: Mon Nov 23 2026 - 12:26:27 EST



On 11/19/26 20:44, Nicolas Dufresne wrote:
+int mali_vpu_fill_pixmp_fmt(const struct mali_vpu_format_desc *fmt,
+ u32 width, u32 height,
+ struct v4l2_pix_format_mplane *pix_mp)
+{
+ u32 sizeimage;
+ int ret;
+
+ memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
+ memset(pix_mp->plane_fmt, 0, sizeof(pix_mp->plane_fmt));
+ pix_mp->flags = 0;
+
+ if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+ pix_mp->width = width;
+ pix_mp->height = height;
+ pix_mp->pixelformat = fmt->fourcc;
+ pix_mp->num_planes = 1;
+ pix_mp->plane_fmt[0].bytesperline = 0;
+
+ /*
+ * Use one byte per pixel as the default compressed sizeimage,
+ * with a minimum of one page, aligned to a page boundary.
+ */
+ if (check_mul_overflow(width, height, &sizeimage))
+ return -EOVERFLOW;
+
+ sizeimage = max_t(u32, PAGE_SIZE, sizeimage);
+
+ if (sizeimage > U32_MAX - (PAGE_SIZE - 1))
+ return -EOVERFLOW;
The "moved" code is not exactly identical, and this is not said in the commit
message. Depending on you max_width/height, this might all be exaggerated.
The checks were intended as defensive handling in a common helper for future
variants. However, given the frame-size constraints applied before this
calculation, they are overly defensive. I’ll remove them and preserve the
original calculation.

- switch (type) {
- case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
- fmt_list = sess->port_src.supported_fmts.fmts;
- num_fmts = sess->port_src.supported_fmts.num_fmts;
- break;
- case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
- fmt_list = sess->port_dst.supported_fmts.fmts;
- num_fmts = sess->port_dst.supported_fmts.num_fmts;
- break;
- default:
- return NULL;
+ vpu_fmt = mali_vpu_find_format(sess, pix_mp->pixelformat, fmt->type);
+ if (!vpu_fmt) {
+ port_fmt = mali_vpu_get_port_fmt(sess, fmt->type);
Maybe it would be better called get_default_port_fmt() ?
This helper actually returns the current port format, and it is also used by
`G_FMT`. On a fresh session that contains the defaults, but it subsequently
contains the negotiated format. I’ll rename it to
`mali_vpu_get_current_port_fmt()` to make that clear.

+ if (!port_fmt)
+ return -EINVAL;
+
+ *pix_mp = port_fmt->fmt.pix_mp;
+ return 0;
+ }
+
+ /* Requested format found, validate other fields */
+ w = pix_mp->width ? pix_mp->width : fmsz->width_default;
+ h = pix_mp->height ? pix_mp->height : fmsz->height_default;
No, width = 0 should return min_width, same for height, just drop, and let the
below function do that. The default width/height are what you set on a fresh
session.
Agreed. In v2, I’ll pass the requested width and height directly to
`v4l2_apply_frmsize_constraints()`, and let it adjust zero to the supported
minimum.

+
+ v4l2_apply_frmsize_constraints(&w, &h, &fmsz->stepwise);
+
+ /* Decode CAPTURE queue: lock geometry to input if streaming */
+ if (sess->role == MALI_VPU_DECODER && fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
+ src_q = v4l2_m2m_get_src_vq(sess->m2m_ctx);
+ if (vb2_is_streaming(src_q)) {
+ port_fmt_dec = &sess->port_src.curr_fmt;
+ w = port_fmt_dec->fmt.pix_mp.width;
+ h = port_fmt_dec->fmt.pix_mp.height;
+ }
This is try_fmt, it should not care if you are streaming or not.
The intention was to use the parsed CAPTURE dimensions once available, but
OUTPUT streaming does not guarantee that parsing has completed. I’ll track
whether firmware has supplied the parameters instead I'll keep the adjustment
logic shared with `S_FMT`, so both return the same format for the same
request and known stream parameters.

+ }
+
+ ret = mali_vpu_fill_pixmp_fmt(vpu_fmt, w, h, pix_mp);
+ if (ret)
+ return ret;
+
+ if (pix_mp->field == V4L2_FIELD_ANY)
+ pix_mp->field = V4L2_FIELD_NONE;
Well, only if you support interlaced.
Since I’m adding interlaced decoding support in v2, I’ll normalize `field` to
`V4L2_FIELD_NONE` on the compressed OUTPUT queue and derive the CAPTURE field
layout from the stream information reported by firmware.

+static int mali_vpu_try_fmt(struct file *filp, void *fh, struct v4l2_format *fmt)
+{
+ struct mali_vpu_session *sess = mali_vpu_get_session(filp);
+ int ret;
+
+ mutex_lock(&sess->vq_lock);
This is suspicious, the vb queue already do some locking in most of the vidioc
call.
Both queues assign `&sess->vq_lock` to `vb2_queue::lock`, but `video_device::lock`
is unset. The V4L2 core acquires it for queue-management ioctls marked
INFO_FL_QUEUE, but not for G_FMT, TRY_FMT or S_FMT.

video_device::lock is unset, as this driver uses per-session locking and
format ioctls are not required to be serialized among different sessions.
The explicit locks therefore protect format access against queue operations
and asynchronous firmware updates, which use the same mutex. They do not
reacquire a lock already held by the ioctl core.


Regards,
Charvi