1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hantro VPU codec driver
4  *
5  * Copyright (C) 2018 Collabora, Ltd.
6  * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
7  *	Alpha Lin <Alpha.Lin@rock-chips.com>
8  *	Jeffy Chen <jeffy.chen@rock-chips.com>
9  *
10  * Copyright 2018 Google LLC.
11  *	Tomasz Figa <tfiga@chromium.org>
12  *
13  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
14  * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
15  */
16 
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
26 
27 #include "hantro.h"
28 #include "hantro_hw.h"
29 #include "hantro_v4l2.h"
30 
31 #define  HANTRO_DEFAULT_BIT_DEPTH 8
32 
33 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
34 			      struct v4l2_pix_format_mplane *pix_mp,
35 			      bool need_postproc);
36 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
37 			      struct v4l2_pix_format_mplane *pix_mp);
38 
39 static const struct hantro_fmt *
40 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts, bool need_postproc)
41 {
42 	const struct hantro_fmt *formats;
43 
44 	if (need_postproc) {
45 		*num_fmts = 0;
46 		return NULL;
47 	}
48 
49 	if (ctx->is_encoder) {
50 		formats = ctx->dev->variant->enc_fmts;
51 		*num_fmts = ctx->dev->variant->num_enc_fmts;
52 	} else {
53 		formats = ctx->dev->variant->dec_fmts;
54 		*num_fmts = ctx->dev->variant->num_dec_fmts;
55 	}
56 
57 	return formats;
58 }
59 
60 static const struct hantro_fmt *
61 hantro_get_postproc_formats(const struct hantro_ctx *ctx,
62 			    unsigned int *num_fmts)
63 {
64 	struct hantro_dev *vpu = ctx->dev;
65 
66 	if (ctx->is_encoder || !vpu->variant->postproc_fmts) {
67 		*num_fmts = 0;
68 		return NULL;
69 	}
70 
71 	*num_fmts = ctx->dev->variant->num_postproc_fmts;
72 	return ctx->dev->variant->postproc_fmts;
73 }
74 
75 int hantro_get_format_depth(u32 fourcc)
76 {
77 	switch (fourcc) {
78 	case V4L2_PIX_FMT_P010:
79 	case V4L2_PIX_FMT_P010_4L4:
80 	case V4L2_PIX_FMT_NV15_4L4:
81 		return 10;
82 	default:
83 		return 8;
84 	}
85 }
86 
87 static bool
88 hantro_check_depth_match(const struct hantro_fmt *fmt, int bit_depth)
89 {
90 	int fmt_depth;
91 
92 	if (!fmt->match_depth && !fmt->postprocessed)
93 		return true;
94 
95 	/* 0 means default depth, which is 8 */
96 	if (!bit_depth)
97 		bit_depth = HANTRO_DEFAULT_BIT_DEPTH;
98 
99 	fmt_depth = hantro_get_format_depth(fmt->fourcc);
100 
101 	/*
102 	 * Allow only downconversion for postproc formats for now.
103 	 * It may be possible to relax that on some HW.
104 	 */
105 	if (!fmt->match_depth)
106 		return fmt_depth <= bit_depth;
107 
108 	return fmt_depth == bit_depth;
109 }
110 
111 static const struct hantro_fmt *
112 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
113 {
114 	const struct hantro_fmt *formats;
115 	unsigned int i, num_fmts;
116 
117 	formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
118 	for (i = 0; i < num_fmts; i++)
119 		if (formats[i].fourcc == fourcc)
120 			return &formats[i];
121 
122 	formats = hantro_get_postproc_formats(ctx, &num_fmts);
123 	for (i = 0; i < num_fmts; i++)
124 		if (formats[i].fourcc == fourcc)
125 			return &formats[i];
126 	return NULL;
127 }
128 
129 const struct hantro_fmt *
130 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream,
131 		       int bit_depth, bool need_postproc)
132 {
133 	const struct hantro_fmt *formats;
134 	unsigned int i, num_fmts;
135 
136 	formats = hantro_get_formats(ctx, &num_fmts, need_postproc);
137 	for (i = 0; i < num_fmts; i++) {
138 		if (bitstream == (formats[i].codec_mode !=
139 				  HANTRO_MODE_NONE) &&
140 		    hantro_check_depth_match(&formats[i], bit_depth))
141 			return &formats[i];
142 	}
143 
144 	formats = hantro_get_postproc_formats(ctx, &num_fmts);
145 	for (i = 0; i < num_fmts; i++) {
146 		if (bitstream == (formats[i].codec_mode !=
147 				  HANTRO_MODE_NONE) &&
148 		    hantro_check_depth_match(&formats[i], bit_depth))
149 			return &formats[i];
150 	}
151 
152 	return NULL;
153 }
154 
155 static int vidioc_querycap(struct file *file, void *priv,
156 			   struct v4l2_capability *cap)
157 {
158 	struct hantro_dev *vpu = video_drvdata(file);
159 	struct video_device *vdev = video_devdata(file);
160 
161 	strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
162 	strscpy(cap->card, vdev->name, sizeof(cap->card));
163 	return 0;
164 }
165 
166 static int vidioc_enum_framesizes(struct file *file, void *priv,
167 				  struct v4l2_frmsizeenum *fsize)
168 {
169 	struct hantro_ctx *ctx = fh_to_ctx(priv);
170 	const struct hantro_fmt *fmt;
171 
172 	fmt = hantro_find_format(ctx, fsize->pixel_format);
173 	if (!fmt) {
174 		vpu_debug(0, "unsupported bitstream format (%08x)\n",
175 			  fsize->pixel_format);
176 		return -EINVAL;
177 	}
178 
179 	/* For non-coded formats check if postprocessing scaling is possible */
180 	if (fmt->codec_mode == HANTRO_MODE_NONE) {
181 		if (hantro_needs_postproc(ctx, fmt))
182 			return hanto_postproc_enum_framesizes(ctx, fsize);
183 		else
184 			return -ENOTTY;
185 	} else if (fsize->index != 0) {
186 		vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
187 			  fsize->index);
188 		return -EINVAL;
189 	}
190 
191 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
192 	fsize->stepwise = fmt->frmsize;
193 
194 	return 0;
195 }
196 
197 static int vidioc_enum_fmt(struct file *file, void *priv,
198 			   struct v4l2_fmtdesc *f, bool capture)
199 
200 {
201 	struct hantro_ctx *ctx = fh_to_ctx(priv);
202 	const struct hantro_fmt *fmt, *formats;
203 	unsigned int num_fmts, i, j = 0;
204 	bool skip_mode_none;
205 
206 	/*
207 	 * When dealing with an encoder:
208 	 *  - on the capture side we want to filter out all MODE_NONE formats.
209 	 *  - on the output side we want to filter out all formats that are
210 	 *    not MODE_NONE.
211 	 * When dealing with a decoder:
212 	 *  - on the capture side we want to filter out all formats that are
213 	 *    not MODE_NONE.
214 	 *  - on the output side we want to filter out all MODE_NONE formats.
215 	 */
216 	skip_mode_none = capture == ctx->is_encoder;
217 
218 	formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
219 	for (i = 0; i < num_fmts; i++) {
220 		bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
221 		fmt = &formats[i];
222 
223 		if (skip_mode_none == mode_none)
224 			continue;
225 		if (!hantro_check_depth_match(fmt, ctx->bit_depth))
226 			continue;
227 		if (j == f->index) {
228 			f->pixelformat = fmt->fourcc;
229 			return 0;
230 		}
231 		++j;
232 	}
233 
234 	/*
235 	 * Enumerate post-processed formats. As per the specification,
236 	 * we enumerated these formats after natively decoded formats
237 	 * as a hint for applications on what's the preferred fomat.
238 	 */
239 	if (!capture)
240 		return -EINVAL;
241 	formats = hantro_get_postproc_formats(ctx, &num_fmts);
242 	for (i = 0; i < num_fmts; i++) {
243 		fmt = &formats[i];
244 
245 		if (!hantro_check_depth_match(fmt, ctx->bit_depth))
246 			continue;
247 		if (j == f->index) {
248 			f->pixelformat = fmt->fourcc;
249 			return 0;
250 		}
251 		++j;
252 	}
253 
254 	return -EINVAL;
255 }
256 
257 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
258 				   struct v4l2_fmtdesc *f)
259 {
260 	return vidioc_enum_fmt(file, priv, f, true);
261 }
262 
263 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
264 				   struct v4l2_fmtdesc *f)
265 {
266 	return vidioc_enum_fmt(file, priv, f, false);
267 }
268 
269 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
270 				   struct v4l2_format *f)
271 {
272 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
273 	struct hantro_ctx *ctx = fh_to_ctx(priv);
274 
275 	vpu_debug(4, "f->type = %d\n", f->type);
276 
277 	*pix_mp = ctx->src_fmt;
278 
279 	return 0;
280 }
281 
282 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
283 				   struct v4l2_format *f)
284 {
285 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
286 	struct hantro_ctx *ctx = fh_to_ctx(priv);
287 
288 	vpu_debug(4, "f->type = %d\n", f->type);
289 
290 	*pix_mp = ctx->dst_fmt;
291 
292 	return 0;
293 }
294 
295 static int hantro_try_fmt(const struct hantro_ctx *ctx,
296 			  struct v4l2_pix_format_mplane *pix_mp,
297 			  enum v4l2_buf_type type)
298 {
299 	const struct hantro_fmt *fmt;
300 	const struct hantro_fmt *vpu_fmt;
301 	bool capture = V4L2_TYPE_IS_CAPTURE(type);
302 	bool coded;
303 
304 	coded = capture == ctx->is_encoder;
305 
306 	vpu_debug(4, "trying format %c%c%c%c\n",
307 		  (pix_mp->pixelformat & 0x7f),
308 		  (pix_mp->pixelformat >> 8) & 0x7f,
309 		  (pix_mp->pixelformat >> 16) & 0x7f,
310 		  (pix_mp->pixelformat >> 24) & 0x7f);
311 
312 	fmt = hantro_find_format(ctx, pix_mp->pixelformat);
313 	if (!fmt) {
314 		fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
315 		pix_mp->pixelformat = fmt->fourcc;
316 	}
317 
318 	if (coded) {
319 		pix_mp->num_planes = 1;
320 		vpu_fmt = fmt;
321 	} else if (ctx->is_encoder) {
322 		vpu_fmt = hantro_find_format(ctx, ctx->dst_fmt.pixelformat);
323 	} else {
324 		/*
325 		 * Width/height on the CAPTURE end of a decoder are ignored and
326 		 * replaced by the OUTPUT ones.
327 		 */
328 		pix_mp->width = ctx->src_fmt.width;
329 		pix_mp->height = ctx->src_fmt.height;
330 		vpu_fmt = fmt;
331 	}
332 
333 	pix_mp->field = V4L2_FIELD_NONE;
334 
335 	v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
336 				       &vpu_fmt->frmsize);
337 
338 	if (!coded) {
339 		/* Fill remaining fields */
340 		v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
341 				    pix_mp->height);
342 		if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
343 		    !hantro_needs_postproc(ctx, fmt))
344 			pix_mp->plane_fmt[0].sizeimage +=
345 				hantro_h264_mv_size(pix_mp->width,
346 						    pix_mp->height);
347 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
348 			 !hantro_needs_postproc(ctx, fmt))
349 			pix_mp->plane_fmt[0].sizeimage +=
350 				hantro_vp9_mv_size(pix_mp->width,
351 						   pix_mp->height);
352 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
353 			 !hantro_needs_postproc(ctx, fmt))
354 			pix_mp->plane_fmt[0].sizeimage +=
355 				hantro_hevc_mv_size(pix_mp->width,
356 						    pix_mp->height);
357 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_AV1_FRAME &&
358 			 !hantro_needs_postproc(ctx, fmt))
359 			pix_mp->plane_fmt[0].sizeimage +=
360 				hantro_av1_mv_size(pix_mp->width,
361 						   pix_mp->height);
362 	} else if (!pix_mp->plane_fmt[0].sizeimage) {
363 		/*
364 		 * For coded formats the application can specify
365 		 * sizeimage. If the application passes a zero sizeimage,
366 		 * let's default to the maximum frame size.
367 		 */
368 		pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
369 			pix_mp->width * pix_mp->height * fmt->max_depth;
370 	}
371 
372 	return 0;
373 }
374 
375 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
376 				     struct v4l2_format *f)
377 {
378 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
379 }
380 
381 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
382 				     struct v4l2_format *f)
383 {
384 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
385 }
386 
387 static void
388 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
389 		 const struct hantro_fmt *vpu_fmt)
390 {
391 	memset(fmt, 0, sizeof(*fmt));
392 
393 	fmt->pixelformat = vpu_fmt->fourcc;
394 	fmt->field = V4L2_FIELD_NONE;
395 	fmt->colorspace = V4L2_COLORSPACE_JPEG;
396 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
397 	fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
398 	fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
399 }
400 
401 static void
402 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
403 {
404 	const struct hantro_fmt *vpu_fmt;
405 	struct v4l2_pix_format_mplane fmt;
406 
407 	vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
408 	if (!vpu_fmt)
409 		return;
410 
411 	hantro_reset_fmt(&fmt, vpu_fmt);
412 	fmt.width = vpu_fmt->frmsize.min_width;
413 	fmt.height = vpu_fmt->frmsize.min_height;
414 	if (ctx->is_encoder)
415 		hantro_set_fmt_cap(ctx, &fmt);
416 	else
417 		hantro_set_fmt_out(ctx, &fmt, HANTRO_AUTO_POSTPROC);
418 }
419 
420 int
421 hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
422 {
423 	const struct hantro_fmt *raw_vpu_fmt;
424 	struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
425 	int ret;
426 
427 	raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
428 	if (!raw_vpu_fmt)
429 		return -EINVAL;
430 
431 	if (ctx->is_encoder) {
432 		encoded_fmt = &ctx->dst_fmt;
433 		ctx->vpu_src_fmt = raw_vpu_fmt;
434 	} else {
435 		encoded_fmt = &ctx->src_fmt;
436 	}
437 
438 	hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
439 	raw_fmt.width = encoded_fmt->width;
440 	raw_fmt.height = encoded_fmt->height;
441 	if (ctx->is_encoder)
442 		ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
443 	else
444 		ret = hantro_set_fmt_cap(ctx, &raw_fmt);
445 
446 	if (!ret) {
447 		ctx->bit_depth = bit_depth;
448 		ctx->need_postproc = need_postproc;
449 	}
450 
451 	return ret;
452 }
453 
454 void hantro_reset_fmts(struct hantro_ctx *ctx)
455 {
456 	hantro_reset_encoded_fmt(ctx);
457 	hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
458 }
459 
460 static void
461 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
462 {
463 	switch (fourcc) {
464 	case V4L2_PIX_FMT_JPEG:
465 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
466 		break;
467 	case V4L2_PIX_FMT_MPEG2_SLICE:
468 	case V4L2_PIX_FMT_VP8_FRAME:
469 	case V4L2_PIX_FMT_H264_SLICE:
470 	case V4L2_PIX_FMT_HEVC_SLICE:
471 	case V4L2_PIX_FMT_VP9_FRAME:
472 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
473 		break;
474 	default:
475 		break;
476 	}
477 }
478 
479 static void
480 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
481 {
482 	struct vb2_queue *vq;
483 
484 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
485 			     V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
486 
487 	switch (fourcc) {
488 	case V4L2_PIX_FMT_JPEG:
489 	case V4L2_PIX_FMT_MPEG2_SLICE:
490 	case V4L2_PIX_FMT_VP8_FRAME:
491 	case V4L2_PIX_FMT_HEVC_SLICE:
492 	case V4L2_PIX_FMT_VP9_FRAME:
493 		vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
494 		break;
495 	case V4L2_PIX_FMT_H264_SLICE:
496 		vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
497 		break;
498 	default:
499 		break;
500 	}
501 }
502 
503 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
504 			      struct v4l2_pix_format_mplane *pix_mp,
505 			      bool need_postproc)
506 {
507 	struct vb2_queue *vq;
508 	int ret;
509 
510 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
511 			     V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
512 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
513 	if (ret)
514 		return ret;
515 
516 	if (!ctx->is_encoder) {
517 		struct vb2_queue *peer_vq;
518 
519 		/*
520 		 * In order to support dynamic resolution change,
521 		 * the decoder admits a resolution change, as long
522 		 * as the pixelformat remains. Can't be done if streaming.
523 		 */
524 		if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
525 		    pix_mp->pixelformat != ctx->src_fmt.pixelformat))
526 			return -EBUSY;
527 		/*
528 		 * Since format change on the OUTPUT queue will reset
529 		 * the CAPTURE queue, we can't allow doing so
530 		 * when the CAPTURE queue has buffers allocated.
531 		 */
532 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
533 					  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
534 		if (vb2_is_busy(peer_vq))
535 			return -EBUSY;
536 	} else {
537 		/*
538 		 * The encoder doesn't admit a format change if
539 		 * there are OUTPUT buffers allocated.
540 		 */
541 		if (vb2_is_busy(vq))
542 			return -EBUSY;
543 	}
544 
545 	ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
546 	ctx->src_fmt = *pix_mp;
547 
548 	/*
549 	 * Current raw format might have become invalid with newly
550 	 * selected codec, so reset it to default just to be safe and
551 	 * keep internal driver state sane. User is mandated to set
552 	 * the raw format again after we return, so we don't need
553 	 * anything smarter.
554 	 * Note that hantro_reset_raw_fmt() also propagates size
555 	 * changes to the raw format.
556 	 */
557 	if (!ctx->is_encoder)
558 		hantro_reset_raw_fmt(ctx,
559 				     hantro_get_format_depth(pix_mp->pixelformat),
560 				     need_postproc);
561 
562 	/* Colorimetry information are always propagated. */
563 	ctx->dst_fmt.colorspace = pix_mp->colorspace;
564 	ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
565 	ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
566 	ctx->dst_fmt.quantization = pix_mp->quantization;
567 
568 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
569 	hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
570 
571 	vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
572 	vpu_debug(0, "fmt - w: %d, h: %d\n",
573 		  pix_mp->width, pix_mp->height);
574 	return 0;
575 }
576 
577 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
578 			      struct v4l2_pix_format_mplane *pix_mp)
579 {
580 	struct vb2_queue *vq;
581 	int ret;
582 
583 	/* Change not allowed if queue is busy. */
584 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
585 			     V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
586 	if (vb2_is_busy(vq))
587 		return -EBUSY;
588 
589 	if (ctx->is_encoder) {
590 		struct vb2_queue *peer_vq;
591 
592 		/*
593 		 * Since format change on the CAPTURE queue will reset
594 		 * the OUTPUT queue, we can't allow doing so
595 		 * when the OUTPUT queue has buffers allocated.
596 		 */
597 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
598 					  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
599 		if (vb2_is_busy(peer_vq) &&
600 		    (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
601 		     pix_mp->height != ctx->dst_fmt.height ||
602 		     pix_mp->width != ctx->dst_fmt.width))
603 			return -EBUSY;
604 	}
605 
606 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
607 	if (ret)
608 		return ret;
609 
610 	ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
611 	ctx->dst_fmt = *pix_mp;
612 
613 	/*
614 	 * Current raw format might have become invalid with newly
615 	 * selected codec, so reset it to default just to be safe and
616 	 * keep internal driver state sane. User is mandated to set
617 	 * the raw format again after we return, so we don't need
618 	 * anything smarter.
619 	 * Note that hantro_reset_raw_fmt() also propagates size
620 	 * changes to the raw format.
621 	 */
622 	if (ctx->is_encoder)
623 		hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
624 
625 	/* Colorimetry information are always propagated. */
626 	ctx->src_fmt.colorspace = pix_mp->colorspace;
627 	ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
628 	ctx->src_fmt.xfer_func = pix_mp->xfer_func;
629 	ctx->src_fmt.quantization = pix_mp->quantization;
630 
631 	vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
632 	vpu_debug(0, "fmt - w: %d, h: %d\n",
633 		  pix_mp->width, pix_mp->height);
634 
635 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
636 
637 	return 0;
638 }
639 
640 static int
641 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
642 {
643 	return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp, HANTRO_AUTO_POSTPROC);
644 }
645 
646 static int
647 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
648 {
649 	return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
650 }
651 
652 static int vidioc_g_selection(struct file *file, void *priv,
653 			      struct v4l2_selection *sel)
654 {
655 	struct hantro_ctx *ctx = fh_to_ctx(priv);
656 
657 	/* Crop only supported on source. */
658 	if (!ctx->is_encoder ||
659 	    sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
660 		return -EINVAL;
661 
662 	switch (sel->target) {
663 	case V4L2_SEL_TGT_CROP_DEFAULT:
664 	case V4L2_SEL_TGT_CROP_BOUNDS:
665 		sel->r.top = 0;
666 		sel->r.left = 0;
667 		sel->r.width = ctx->src_fmt.width;
668 		sel->r.height = ctx->src_fmt.height;
669 		break;
670 	case V4L2_SEL_TGT_CROP:
671 		sel->r.top = 0;
672 		sel->r.left = 0;
673 		sel->r.width = ctx->dst_fmt.width;
674 		sel->r.height = ctx->dst_fmt.height;
675 		break;
676 	default:
677 		return -EINVAL;
678 	}
679 
680 	return 0;
681 }
682 
683 static int vidioc_s_selection(struct file *file, void *priv,
684 			      struct v4l2_selection *sel)
685 {
686 	struct hantro_ctx *ctx = fh_to_ctx(priv);
687 	struct v4l2_rect *rect = &sel->r;
688 	struct vb2_queue *vq;
689 
690 	/* Crop only supported on source. */
691 	if (!ctx->is_encoder ||
692 	    sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
693 		return -EINVAL;
694 
695 	/* Change not allowed if the queue is streaming. */
696 	vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
697 	if (vb2_is_streaming(vq))
698 		return -EBUSY;
699 
700 	if (sel->target != V4L2_SEL_TGT_CROP)
701 		return -EINVAL;
702 
703 	/*
704 	 * We do not support offsets, and we can crop only inside
705 	 * right-most or bottom-most macroblocks.
706 	 */
707 	if (rect->left != 0 || rect->top != 0 ||
708 	    round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
709 	    round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
710 		/* Default to full frame for incorrect settings. */
711 		rect->left = 0;
712 		rect->top = 0;
713 		rect->width = ctx->src_fmt.width;
714 		rect->height = ctx->src_fmt.height;
715 	} else {
716 		/* We support widths aligned to 4 pixels and arbitrary heights. */
717 		rect->width = round_up(rect->width, 4);
718 	}
719 
720 	ctx->dst_fmt.width = rect->width;
721 	ctx->dst_fmt.height = rect->height;
722 
723 	return 0;
724 }
725 
726 static const struct v4l2_event hantro_eos_event = {
727 	.type = V4L2_EVENT_EOS
728 };
729 
730 static int vidioc_encoder_cmd(struct file *file, void *priv,
731 			      struct v4l2_encoder_cmd *ec)
732 {
733 	struct hantro_ctx *ctx = fh_to_ctx(priv);
734 	int ret;
735 
736 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
737 	if (ret < 0)
738 		return ret;
739 
740 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
741 	    !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
742 		return 0;
743 
744 	ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
745 	if (ret < 0)
746 		return ret;
747 
748 	if (ec->cmd == V4L2_ENC_CMD_STOP &&
749 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
750 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
751 
752 	if (ec->cmd == V4L2_ENC_CMD_START)
753 		vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
754 
755 	return 0;
756 }
757 
758 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
759 	.vidioc_querycap = vidioc_querycap,
760 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
761 
762 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
763 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
764 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
765 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
766 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
767 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
768 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
769 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
770 
771 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
772 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
773 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
774 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
775 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
776 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
777 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
778 
779 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
780 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
781 
782 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
783 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
784 
785 	.vidioc_g_selection = vidioc_g_selection,
786 	.vidioc_s_selection = vidioc_s_selection,
787 
788 	.vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
789 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
790 
791 	.vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
792 	.vidioc_encoder_cmd = vidioc_encoder_cmd,
793 };
794 
795 static int
796 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
797 		   unsigned int *num_planes, unsigned int sizes[],
798 		   struct device *alloc_devs[])
799 {
800 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
801 	struct v4l2_pix_format_mplane *pixfmt;
802 	int i;
803 
804 	switch (vq->type) {
805 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
806 		pixfmt = &ctx->dst_fmt;
807 		break;
808 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
809 		pixfmt = &ctx->src_fmt;
810 		break;
811 	default:
812 		vpu_err("invalid queue type: %d\n", vq->type);
813 		return -EINVAL;
814 	}
815 
816 	if (*num_planes) {
817 		if (*num_planes != pixfmt->num_planes)
818 			return -EINVAL;
819 		for (i = 0; i < pixfmt->num_planes; ++i)
820 			if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
821 				return -EINVAL;
822 		return 0;
823 	}
824 
825 	*num_planes = pixfmt->num_planes;
826 	for (i = 0; i < pixfmt->num_planes; ++i)
827 		sizes[i] = pixfmt->plane_fmt[i].sizeimage;
828 	return 0;
829 }
830 
831 static int
832 hantro_buf_plane_check(struct vb2_buffer *vb,
833 		       struct v4l2_pix_format_mplane *pixfmt)
834 {
835 	unsigned int sz;
836 	int i;
837 
838 	for (i = 0; i < pixfmt->num_planes; ++i) {
839 		sz = pixfmt->plane_fmt[i].sizeimage;
840 		vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
841 			  i, vb2_plane_size(vb, i), sz);
842 		if (vb2_plane_size(vb, i) < sz) {
843 			vpu_err("plane %d is too small for output\n", i);
844 			return -EINVAL;
845 		}
846 	}
847 	return 0;
848 }
849 
850 static int hantro_buf_prepare(struct vb2_buffer *vb)
851 {
852 	struct vb2_queue *vq = vb->vb2_queue;
853 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
854 	struct v4l2_pix_format_mplane *pix_fmt;
855 	int ret;
856 
857 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
858 		pix_fmt = &ctx->src_fmt;
859 	else
860 		pix_fmt = &ctx->dst_fmt;
861 	ret = hantro_buf_plane_check(vb, pix_fmt);
862 	if (ret)
863 		return ret;
864 	/*
865 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
866 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
867 	 * it to buffer length).
868 	 */
869 	if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
870 		if (ctx->is_encoder)
871 			vb2_set_plane_payload(vb, 0, 0);
872 		else
873 			vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
874 	}
875 
876 	return 0;
877 }
878 
879 static void hantro_buf_queue(struct vb2_buffer *vb)
880 {
881 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
882 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
883 
884 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
885 	    vb2_is_streaming(vb->vb2_queue) &&
886 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
887 		unsigned int i;
888 
889 		for (i = 0; i < vb->num_planes; i++)
890 			vb2_set_plane_payload(vb, i, 0);
891 
892 		vbuf->field = V4L2_FIELD_NONE;
893 		vbuf->sequence = ctx->sequence_cap++;
894 
895 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
896 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
897 		return;
898 	}
899 
900 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
901 }
902 
903 static bool hantro_vq_is_coded(struct vb2_queue *q)
904 {
905 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
906 
907 	return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
908 }
909 
910 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
911 {
912 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
913 	int ret = 0;
914 
915 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
916 
917 	if (V4L2_TYPE_IS_OUTPUT(q->type))
918 		ctx->sequence_out = 0;
919 	else
920 		ctx->sequence_cap = 0;
921 
922 	if (hantro_vq_is_coded(q)) {
923 		enum hantro_codec_mode codec_mode;
924 
925 		if (V4L2_TYPE_IS_OUTPUT(q->type))
926 			codec_mode = ctx->vpu_src_fmt->codec_mode;
927 		else
928 			codec_mode = ctx->vpu_dst_fmt->codec_mode;
929 
930 		vpu_debug(4, "Codec mode = %d\n", codec_mode);
931 		ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
932 		if (ctx->codec_ops->init) {
933 			ret = ctx->codec_ops->init(ctx);
934 			if (ret)
935 				return ret;
936 		}
937 
938 		if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
939 			ret = hantro_postproc_alloc(ctx);
940 			if (ret)
941 				goto err_codec_exit;
942 		}
943 	}
944 	return ret;
945 
946 err_codec_exit:
947 	if (ctx->codec_ops->exit)
948 		ctx->codec_ops->exit(ctx);
949 	return ret;
950 }
951 
952 static void
953 hantro_return_bufs(struct vb2_queue *q,
954 		   struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
955 {
956 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
957 
958 	for (;;) {
959 		struct vb2_v4l2_buffer *vbuf;
960 
961 		vbuf = buf_remove(ctx->fh.m2m_ctx);
962 		if (!vbuf)
963 			break;
964 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
965 					   &ctx->ctrl_handler);
966 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
967 	}
968 }
969 
970 static void hantro_stop_streaming(struct vb2_queue *q)
971 {
972 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
973 
974 	if (hantro_vq_is_coded(q)) {
975 		hantro_postproc_free(ctx);
976 		if (ctx->codec_ops && ctx->codec_ops->exit)
977 			ctx->codec_ops->exit(ctx);
978 	}
979 
980 	/*
981 	 * The mem2mem framework calls v4l2_m2m_cancel_job before
982 	 * .stop_streaming, so there isn't any job running and
983 	 * it is safe to return all the buffers.
984 	 */
985 	if (V4L2_TYPE_IS_OUTPUT(q->type))
986 		hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
987 	else
988 		hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
989 
990 	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
991 
992 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
993 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
994 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
995 }
996 
997 static void hantro_buf_request_complete(struct vb2_buffer *vb)
998 {
999 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1000 
1001 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
1002 }
1003 
1004 static int hantro_buf_out_validate(struct vb2_buffer *vb)
1005 {
1006 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1007 
1008 	vbuf->field = V4L2_FIELD_NONE;
1009 	return 0;
1010 }
1011 
1012 const struct vb2_ops hantro_queue_ops = {
1013 	.queue_setup = hantro_queue_setup,
1014 	.buf_prepare = hantro_buf_prepare,
1015 	.buf_queue = hantro_buf_queue,
1016 	.buf_out_validate = hantro_buf_out_validate,
1017 	.buf_request_complete = hantro_buf_request_complete,
1018 	.start_streaming = hantro_start_streaming,
1019 	.stop_streaming = hantro_stop_streaming,
1020 	.wait_prepare = vb2_ops_wait_prepare,
1021 	.wait_finish = vb2_ops_wait_finish,
1022 };
1023