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_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
789 	.vidioc_encoder_cmd = vidioc_encoder_cmd,
790 };
791 
792 static int
793 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
794 		   unsigned int *num_planes, unsigned int sizes[],
795 		   struct device *alloc_devs[])
796 {
797 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
798 	struct v4l2_pix_format_mplane *pixfmt;
799 	int i;
800 
801 	switch (vq->type) {
802 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
803 		pixfmt = &ctx->dst_fmt;
804 		break;
805 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
806 		pixfmt = &ctx->src_fmt;
807 		break;
808 	default:
809 		vpu_err("invalid queue type: %d\n", vq->type);
810 		return -EINVAL;
811 	}
812 
813 	if (*num_planes) {
814 		if (*num_planes != pixfmt->num_planes)
815 			return -EINVAL;
816 		for (i = 0; i < pixfmt->num_planes; ++i)
817 			if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
818 				return -EINVAL;
819 		return 0;
820 	}
821 
822 	*num_planes = pixfmt->num_planes;
823 	for (i = 0; i < pixfmt->num_planes; ++i)
824 		sizes[i] = pixfmt->plane_fmt[i].sizeimage;
825 	return 0;
826 }
827 
828 static int
829 hantro_buf_plane_check(struct vb2_buffer *vb,
830 		       struct v4l2_pix_format_mplane *pixfmt)
831 {
832 	unsigned int sz;
833 	int i;
834 
835 	for (i = 0; i < pixfmt->num_planes; ++i) {
836 		sz = pixfmt->plane_fmt[i].sizeimage;
837 		vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
838 			  i, vb2_plane_size(vb, i), sz);
839 		if (vb2_plane_size(vb, i) < sz) {
840 			vpu_err("plane %d is too small for output\n", i);
841 			return -EINVAL;
842 		}
843 	}
844 	return 0;
845 }
846 
847 static int hantro_buf_prepare(struct vb2_buffer *vb)
848 {
849 	struct vb2_queue *vq = vb->vb2_queue;
850 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
851 	struct v4l2_pix_format_mplane *pix_fmt;
852 	int ret;
853 
854 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
855 		pix_fmt = &ctx->src_fmt;
856 	else
857 		pix_fmt = &ctx->dst_fmt;
858 	ret = hantro_buf_plane_check(vb, pix_fmt);
859 	if (ret)
860 		return ret;
861 	/*
862 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
863 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
864 	 * it to buffer length).
865 	 */
866 	if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
867 		if (ctx->is_encoder)
868 			vb2_set_plane_payload(vb, 0, 0);
869 		else
870 			vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
871 	}
872 
873 	return 0;
874 }
875 
876 static void hantro_buf_queue(struct vb2_buffer *vb)
877 {
878 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
879 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
880 
881 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
882 	    vb2_is_streaming(vb->vb2_queue) &&
883 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
884 		unsigned int i;
885 
886 		for (i = 0; i < vb->num_planes; i++)
887 			vb2_set_plane_payload(vb, i, 0);
888 
889 		vbuf->field = V4L2_FIELD_NONE;
890 		vbuf->sequence = ctx->sequence_cap++;
891 
892 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
893 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
894 		return;
895 	}
896 
897 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
898 }
899 
900 static bool hantro_vq_is_coded(struct vb2_queue *q)
901 {
902 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
903 
904 	return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
905 }
906 
907 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
908 {
909 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
910 	int ret = 0;
911 
912 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
913 
914 	if (V4L2_TYPE_IS_OUTPUT(q->type))
915 		ctx->sequence_out = 0;
916 	else
917 		ctx->sequence_cap = 0;
918 
919 	if (hantro_vq_is_coded(q)) {
920 		enum hantro_codec_mode codec_mode;
921 
922 		if (V4L2_TYPE_IS_OUTPUT(q->type))
923 			codec_mode = ctx->vpu_src_fmt->codec_mode;
924 		else
925 			codec_mode = ctx->vpu_dst_fmt->codec_mode;
926 
927 		vpu_debug(4, "Codec mode = %d\n", codec_mode);
928 		ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
929 		if (ctx->codec_ops->init) {
930 			ret = ctx->codec_ops->init(ctx);
931 			if (ret)
932 				return ret;
933 		}
934 
935 		if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
936 			ret = hantro_postproc_alloc(ctx);
937 			if (ret)
938 				goto err_codec_exit;
939 		}
940 	}
941 	return ret;
942 
943 err_codec_exit:
944 	if (ctx->codec_ops->exit)
945 		ctx->codec_ops->exit(ctx);
946 	return ret;
947 }
948 
949 static void
950 hantro_return_bufs(struct vb2_queue *q,
951 		   struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
952 {
953 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
954 
955 	for (;;) {
956 		struct vb2_v4l2_buffer *vbuf;
957 
958 		vbuf = buf_remove(ctx->fh.m2m_ctx);
959 		if (!vbuf)
960 			break;
961 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
962 					   &ctx->ctrl_handler);
963 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
964 	}
965 }
966 
967 static void hantro_stop_streaming(struct vb2_queue *q)
968 {
969 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
970 
971 	if (hantro_vq_is_coded(q)) {
972 		hantro_postproc_free(ctx);
973 		if (ctx->codec_ops && ctx->codec_ops->exit)
974 			ctx->codec_ops->exit(ctx);
975 	}
976 
977 	/*
978 	 * The mem2mem framework calls v4l2_m2m_cancel_job before
979 	 * .stop_streaming, so there isn't any job running and
980 	 * it is safe to return all the buffers.
981 	 */
982 	if (V4L2_TYPE_IS_OUTPUT(q->type))
983 		hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
984 	else
985 		hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
986 
987 	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
988 
989 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
990 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
991 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
992 }
993 
994 static void hantro_buf_request_complete(struct vb2_buffer *vb)
995 {
996 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
997 
998 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
999 }
1000 
1001 static int hantro_buf_out_validate(struct vb2_buffer *vb)
1002 {
1003 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1004 
1005 	vbuf->field = V4L2_FIELD_NONE;
1006 	return 0;
1007 }
1008 
1009 const struct vb2_ops hantro_queue_ops = {
1010 	.queue_setup = hantro_queue_setup,
1011 	.buf_prepare = hantro_buf_prepare,
1012 	.buf_queue = hantro_buf_queue,
1013 	.buf_out_validate = hantro_buf_out_validate,
1014 	.buf_request_complete = hantro_buf_request_complete,
1015 	.start_streaming = hantro_start_streaming,
1016 	.stop_streaming = hantro_stop_streaming,
1017 	.wait_prepare = vb2_ops_wait_prepare,
1018 	.wait_finish = vb2_ops_wait_finish,
1019 };
1020