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 	bool capture = V4L2_TYPE_IS_CAPTURE(type);
301 	bool coded;
302 
303 	coded = capture == ctx->is_encoder;
304 
305 	vpu_debug(4, "trying format %c%c%c%c\n",
306 		  (pix_mp->pixelformat & 0x7f),
307 		  (pix_mp->pixelformat >> 8) & 0x7f,
308 		  (pix_mp->pixelformat >> 16) & 0x7f,
309 		  (pix_mp->pixelformat >> 24) & 0x7f);
310 
311 	fmt = hantro_find_format(ctx, pix_mp->pixelformat);
312 	if (!fmt) {
313 		fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
314 		pix_mp->pixelformat = fmt->fourcc;
315 	}
316 
317 	if (coded) {
318 		pix_mp->num_planes = 1;
319 	} else if (!ctx->is_encoder) {
320 		/*
321 		 * Width/height on the CAPTURE end of a decoder are ignored and
322 		 * replaced by the OUTPUT ones.
323 		 */
324 		pix_mp->width = ctx->src_fmt.width;
325 		pix_mp->height = ctx->src_fmt.height;
326 	}
327 
328 	pix_mp->field = V4L2_FIELD_NONE;
329 
330 	v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
331 				       &fmt->frmsize);
332 
333 	if (!coded) {
334 		/* Fill remaining fields */
335 		v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
336 				    pix_mp->height);
337 		if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
338 		    !hantro_needs_postproc(ctx, fmt))
339 			pix_mp->plane_fmt[0].sizeimage +=
340 				hantro_h264_mv_size(pix_mp->width,
341 						    pix_mp->height);
342 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
343 			 !hantro_needs_postproc(ctx, fmt))
344 			pix_mp->plane_fmt[0].sizeimage +=
345 				hantro_vp9_mv_size(pix_mp->width,
346 						   pix_mp->height);
347 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
348 			 !hantro_needs_postproc(ctx, fmt))
349 			pix_mp->plane_fmt[0].sizeimage +=
350 				hantro_hevc_mv_size(pix_mp->width,
351 						    pix_mp->height);
352 		else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_AV1_FRAME &&
353 			 !hantro_needs_postproc(ctx, fmt))
354 			pix_mp->plane_fmt[0].sizeimage +=
355 				hantro_av1_mv_size(pix_mp->width,
356 						   pix_mp->height);
357 	} else if (!pix_mp->plane_fmt[0].sizeimage) {
358 		/*
359 		 * For coded formats the application can specify
360 		 * sizeimage. If the application passes a zero sizeimage,
361 		 * let's default to the maximum frame size.
362 		 */
363 		pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
364 			pix_mp->width * pix_mp->height * fmt->max_depth;
365 	}
366 
367 	return 0;
368 }
369 
370 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
371 				     struct v4l2_format *f)
372 {
373 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
374 }
375 
376 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
377 				     struct v4l2_format *f)
378 {
379 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
380 }
381 
382 static void
383 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
384 		 const struct hantro_fmt *vpu_fmt)
385 {
386 	memset(fmt, 0, sizeof(*fmt));
387 
388 	fmt->pixelformat = vpu_fmt->fourcc;
389 	fmt->field = V4L2_FIELD_NONE;
390 	fmt->colorspace = V4L2_COLORSPACE_JPEG;
391 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
392 	fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
393 	fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
394 }
395 
396 static void
397 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
398 {
399 	const struct hantro_fmt *vpu_fmt;
400 	struct v4l2_pix_format_mplane fmt;
401 
402 	vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
403 	if (!vpu_fmt)
404 		return;
405 
406 	hantro_reset_fmt(&fmt, vpu_fmt);
407 	fmt.width = vpu_fmt->frmsize.min_width;
408 	fmt.height = vpu_fmt->frmsize.min_height;
409 	if (ctx->is_encoder)
410 		hantro_set_fmt_cap(ctx, &fmt);
411 	else
412 		hantro_set_fmt_out(ctx, &fmt, HANTRO_AUTO_POSTPROC);
413 }
414 
415 int
416 hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
417 {
418 	const struct hantro_fmt *raw_vpu_fmt;
419 	struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
420 	int ret;
421 
422 	raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
423 	if (!raw_vpu_fmt)
424 		return -EINVAL;
425 
426 	if (ctx->is_encoder) {
427 		encoded_fmt = &ctx->dst_fmt;
428 		ctx->vpu_src_fmt = raw_vpu_fmt;
429 	} else {
430 		encoded_fmt = &ctx->src_fmt;
431 	}
432 
433 	hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
434 	raw_fmt.width = encoded_fmt->width;
435 	raw_fmt.height = encoded_fmt->height;
436 	if (ctx->is_encoder)
437 		ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
438 	else
439 		ret = hantro_set_fmt_cap(ctx, &raw_fmt);
440 
441 	if (!ret) {
442 		ctx->bit_depth = bit_depth;
443 		ctx->need_postproc = need_postproc;
444 	}
445 
446 	return ret;
447 }
448 
449 void hantro_reset_fmts(struct hantro_ctx *ctx)
450 {
451 	hantro_reset_encoded_fmt(ctx);
452 	hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
453 }
454 
455 static void
456 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
457 {
458 	switch (fourcc) {
459 	case V4L2_PIX_FMT_JPEG:
460 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
461 		break;
462 	case V4L2_PIX_FMT_MPEG2_SLICE:
463 	case V4L2_PIX_FMT_VP8_FRAME:
464 	case V4L2_PIX_FMT_H264_SLICE:
465 	case V4L2_PIX_FMT_HEVC_SLICE:
466 	case V4L2_PIX_FMT_VP9_FRAME:
467 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
468 		break;
469 	default:
470 		break;
471 	}
472 }
473 
474 static void
475 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
476 {
477 	struct vb2_queue *vq;
478 
479 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
480 			     V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
481 
482 	switch (fourcc) {
483 	case V4L2_PIX_FMT_JPEG:
484 	case V4L2_PIX_FMT_MPEG2_SLICE:
485 	case V4L2_PIX_FMT_VP8_FRAME:
486 	case V4L2_PIX_FMT_HEVC_SLICE:
487 	case V4L2_PIX_FMT_VP9_FRAME:
488 		vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
489 		break;
490 	case V4L2_PIX_FMT_H264_SLICE:
491 		vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
492 		break;
493 	default:
494 		break;
495 	}
496 }
497 
498 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
499 			      struct v4l2_pix_format_mplane *pix_mp,
500 			      bool need_postproc)
501 {
502 	struct vb2_queue *vq;
503 	int ret;
504 
505 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
506 			     V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
507 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
508 	if (ret)
509 		return ret;
510 
511 	if (!ctx->is_encoder) {
512 		struct vb2_queue *peer_vq;
513 
514 		/*
515 		 * In order to support dynamic resolution change,
516 		 * the decoder admits a resolution change, as long
517 		 * as the pixelformat remains. Can't be done if streaming.
518 		 */
519 		if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
520 		    pix_mp->pixelformat != ctx->src_fmt.pixelformat))
521 			return -EBUSY;
522 		/*
523 		 * Since format change on the OUTPUT queue will reset
524 		 * the CAPTURE queue, we can't allow doing so
525 		 * when the CAPTURE queue has buffers allocated.
526 		 */
527 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
528 					  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
529 		if (vb2_is_busy(peer_vq))
530 			return -EBUSY;
531 	} else {
532 		/*
533 		 * The encoder doesn't admit a format change if
534 		 * there are OUTPUT buffers allocated.
535 		 */
536 		if (vb2_is_busy(vq))
537 			return -EBUSY;
538 	}
539 
540 	ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
541 	ctx->src_fmt = *pix_mp;
542 
543 	/*
544 	 * Current raw format might have become invalid with newly
545 	 * selected codec, so reset it to default just to be safe and
546 	 * keep internal driver state sane. User is mandated to set
547 	 * the raw format again after we return, so we don't need
548 	 * anything smarter.
549 	 * Note that hantro_reset_raw_fmt() also propagates size
550 	 * changes to the raw format.
551 	 */
552 	if (!ctx->is_encoder)
553 		hantro_reset_raw_fmt(ctx,
554 				     hantro_get_format_depth(pix_mp->pixelformat),
555 				     need_postproc);
556 
557 	/* Colorimetry information are always propagated. */
558 	ctx->dst_fmt.colorspace = pix_mp->colorspace;
559 	ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
560 	ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
561 	ctx->dst_fmt.quantization = pix_mp->quantization;
562 
563 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
564 	hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
565 
566 	vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
567 	vpu_debug(0, "fmt - w: %d, h: %d\n",
568 		  pix_mp->width, pix_mp->height);
569 	return 0;
570 }
571 
572 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
573 			      struct v4l2_pix_format_mplane *pix_mp)
574 {
575 	struct vb2_queue *vq;
576 	int ret;
577 
578 	/* Change not allowed if queue is busy. */
579 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
580 			     V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
581 	if (vb2_is_busy(vq))
582 		return -EBUSY;
583 
584 	if (ctx->is_encoder) {
585 		struct vb2_queue *peer_vq;
586 
587 		/*
588 		 * Since format change on the CAPTURE queue will reset
589 		 * the OUTPUT queue, we can't allow doing so
590 		 * when the OUTPUT queue has buffers allocated.
591 		 */
592 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
593 					  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
594 		if (vb2_is_busy(peer_vq) &&
595 		    (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
596 		     pix_mp->height != ctx->dst_fmt.height ||
597 		     pix_mp->width != ctx->dst_fmt.width))
598 			return -EBUSY;
599 	}
600 
601 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
602 	if (ret)
603 		return ret;
604 
605 	ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
606 	ctx->dst_fmt = *pix_mp;
607 
608 	/*
609 	 * Current raw format might have become invalid with newly
610 	 * selected codec, so reset it to default just to be safe and
611 	 * keep internal driver state sane. User is mandated to set
612 	 * the raw format again after we return, so we don't need
613 	 * anything smarter.
614 	 * Note that hantro_reset_raw_fmt() also propagates size
615 	 * changes to the raw format.
616 	 */
617 	if (ctx->is_encoder)
618 		hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
619 
620 	/* Colorimetry information are always propagated. */
621 	ctx->src_fmt.colorspace = pix_mp->colorspace;
622 	ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
623 	ctx->src_fmt.xfer_func = pix_mp->xfer_func;
624 	ctx->src_fmt.quantization = pix_mp->quantization;
625 
626 	vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
627 	vpu_debug(0, "fmt - w: %d, h: %d\n",
628 		  pix_mp->width, pix_mp->height);
629 
630 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
631 
632 	return 0;
633 }
634 
635 static int
636 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
637 {
638 	return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp, HANTRO_AUTO_POSTPROC);
639 }
640 
641 static int
642 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
643 {
644 	return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
645 }
646 
647 static int vidioc_g_selection(struct file *file, void *priv,
648 			      struct v4l2_selection *sel)
649 {
650 	struct hantro_ctx *ctx = fh_to_ctx(priv);
651 
652 	/* Crop only supported on source. */
653 	if (!ctx->is_encoder ||
654 	    sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
655 		return -EINVAL;
656 
657 	switch (sel->target) {
658 	case V4L2_SEL_TGT_CROP_DEFAULT:
659 	case V4L2_SEL_TGT_CROP_BOUNDS:
660 		sel->r.top = 0;
661 		sel->r.left = 0;
662 		sel->r.width = ctx->src_fmt.width;
663 		sel->r.height = ctx->src_fmt.height;
664 		break;
665 	case V4L2_SEL_TGT_CROP:
666 		sel->r.top = 0;
667 		sel->r.left = 0;
668 		sel->r.width = ctx->dst_fmt.width;
669 		sel->r.height = ctx->dst_fmt.height;
670 		break;
671 	default:
672 		return -EINVAL;
673 	}
674 
675 	return 0;
676 }
677 
678 static int vidioc_s_selection(struct file *file, void *priv,
679 			      struct v4l2_selection *sel)
680 {
681 	struct hantro_ctx *ctx = fh_to_ctx(priv);
682 	struct v4l2_rect *rect = &sel->r;
683 	struct vb2_queue *vq;
684 
685 	/* Crop only supported on source. */
686 	if (!ctx->is_encoder ||
687 	    sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
688 		return -EINVAL;
689 
690 	/* Change not allowed if the queue is streaming. */
691 	vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
692 	if (vb2_is_streaming(vq))
693 		return -EBUSY;
694 
695 	if (sel->target != V4L2_SEL_TGT_CROP)
696 		return -EINVAL;
697 
698 	/*
699 	 * We do not support offsets, and we can crop only inside
700 	 * right-most or bottom-most macroblocks.
701 	 */
702 	if (rect->left != 0 || rect->top != 0 ||
703 	    round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
704 	    round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
705 		/* Default to full frame for incorrect settings. */
706 		rect->left = 0;
707 		rect->top = 0;
708 		rect->width = ctx->src_fmt.width;
709 		rect->height = ctx->src_fmt.height;
710 	} else {
711 		/* We support widths aligned to 4 pixels and arbitrary heights. */
712 		rect->width = round_up(rect->width, 4);
713 	}
714 
715 	ctx->dst_fmt.width = rect->width;
716 	ctx->dst_fmt.height = rect->height;
717 
718 	return 0;
719 }
720 
721 static const struct v4l2_event hantro_eos_event = {
722 	.type = V4L2_EVENT_EOS
723 };
724 
725 static int vidioc_encoder_cmd(struct file *file, void *priv,
726 			      struct v4l2_encoder_cmd *ec)
727 {
728 	struct hantro_ctx *ctx = fh_to_ctx(priv);
729 	int ret;
730 
731 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
732 	if (ret < 0)
733 		return ret;
734 
735 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
736 	    !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
737 		return 0;
738 
739 	ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
740 	if (ret < 0)
741 		return ret;
742 
743 	if (ec->cmd == V4L2_ENC_CMD_STOP &&
744 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
745 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
746 
747 	if (ec->cmd == V4L2_ENC_CMD_START)
748 		vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
749 
750 	return 0;
751 }
752 
753 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
754 	.vidioc_querycap = vidioc_querycap,
755 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
756 
757 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
758 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
759 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
760 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
761 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
762 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
763 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
764 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
765 
766 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
767 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
768 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
769 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
770 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
771 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
772 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
773 
774 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
775 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
776 
777 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
778 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
779 
780 	.vidioc_g_selection = vidioc_g_selection,
781 	.vidioc_s_selection = vidioc_s_selection,
782 
783 	.vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
784 	.vidioc_encoder_cmd = vidioc_encoder_cmd,
785 };
786 
787 static int
788 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
789 		   unsigned int *num_planes, unsigned int sizes[],
790 		   struct device *alloc_devs[])
791 {
792 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
793 	struct v4l2_pix_format_mplane *pixfmt;
794 	int i;
795 
796 	switch (vq->type) {
797 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
798 		pixfmt = &ctx->dst_fmt;
799 		break;
800 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
801 		pixfmt = &ctx->src_fmt;
802 		break;
803 	default:
804 		vpu_err("invalid queue type: %d\n", vq->type);
805 		return -EINVAL;
806 	}
807 
808 	if (*num_planes) {
809 		if (*num_planes != pixfmt->num_planes)
810 			return -EINVAL;
811 		for (i = 0; i < pixfmt->num_planes; ++i)
812 			if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
813 				return -EINVAL;
814 		return 0;
815 	}
816 
817 	*num_planes = pixfmt->num_planes;
818 	for (i = 0; i < pixfmt->num_planes; ++i)
819 		sizes[i] = pixfmt->plane_fmt[i].sizeimage;
820 	return 0;
821 }
822 
823 static int
824 hantro_buf_plane_check(struct vb2_buffer *vb,
825 		       struct v4l2_pix_format_mplane *pixfmt)
826 {
827 	unsigned int sz;
828 	int i;
829 
830 	for (i = 0; i < pixfmt->num_planes; ++i) {
831 		sz = pixfmt->plane_fmt[i].sizeimage;
832 		vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
833 			  i, vb2_plane_size(vb, i), sz);
834 		if (vb2_plane_size(vb, i) < sz) {
835 			vpu_err("plane %d is too small for output\n", i);
836 			return -EINVAL;
837 		}
838 	}
839 	return 0;
840 }
841 
842 static int hantro_buf_prepare(struct vb2_buffer *vb)
843 {
844 	struct vb2_queue *vq = vb->vb2_queue;
845 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
846 	struct v4l2_pix_format_mplane *pix_fmt;
847 	int ret;
848 
849 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
850 		pix_fmt = &ctx->src_fmt;
851 	else
852 		pix_fmt = &ctx->dst_fmt;
853 	ret = hantro_buf_plane_check(vb, pix_fmt);
854 	if (ret)
855 		return ret;
856 	/*
857 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
858 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
859 	 * it to buffer length).
860 	 */
861 	if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
862 		if (ctx->is_encoder)
863 			vb2_set_plane_payload(vb, 0, 0);
864 		else
865 			vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
866 	}
867 
868 	return 0;
869 }
870 
871 static void hantro_buf_queue(struct vb2_buffer *vb)
872 {
873 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
874 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
875 
876 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
877 	    vb2_is_streaming(vb->vb2_queue) &&
878 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
879 		unsigned int i;
880 
881 		for (i = 0; i < vb->num_planes; i++)
882 			vb2_set_plane_payload(vb, i, 0);
883 
884 		vbuf->field = V4L2_FIELD_NONE;
885 		vbuf->sequence = ctx->sequence_cap++;
886 
887 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
888 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
889 		return;
890 	}
891 
892 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
893 }
894 
895 static bool hantro_vq_is_coded(struct vb2_queue *q)
896 {
897 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
898 
899 	return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
900 }
901 
902 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
903 {
904 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
905 	int ret = 0;
906 
907 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
908 
909 	if (V4L2_TYPE_IS_OUTPUT(q->type))
910 		ctx->sequence_out = 0;
911 	else
912 		ctx->sequence_cap = 0;
913 
914 	if (hantro_vq_is_coded(q)) {
915 		enum hantro_codec_mode codec_mode;
916 
917 		if (V4L2_TYPE_IS_OUTPUT(q->type))
918 			codec_mode = ctx->vpu_src_fmt->codec_mode;
919 		else
920 			codec_mode = ctx->vpu_dst_fmt->codec_mode;
921 
922 		vpu_debug(4, "Codec mode = %d\n", codec_mode);
923 		ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
924 		if (ctx->codec_ops->init) {
925 			ret = ctx->codec_ops->init(ctx);
926 			if (ret)
927 				return ret;
928 		}
929 
930 		if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
931 			ret = hantro_postproc_alloc(ctx);
932 			if (ret)
933 				goto err_codec_exit;
934 		}
935 	}
936 	return ret;
937 
938 err_codec_exit:
939 	if (ctx->codec_ops->exit)
940 		ctx->codec_ops->exit(ctx);
941 	return ret;
942 }
943 
944 static void
945 hantro_return_bufs(struct vb2_queue *q,
946 		   struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
947 {
948 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
949 
950 	for (;;) {
951 		struct vb2_v4l2_buffer *vbuf;
952 
953 		vbuf = buf_remove(ctx->fh.m2m_ctx);
954 		if (!vbuf)
955 			break;
956 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
957 					   &ctx->ctrl_handler);
958 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
959 	}
960 }
961 
962 static void hantro_stop_streaming(struct vb2_queue *q)
963 {
964 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
965 
966 	if (hantro_vq_is_coded(q)) {
967 		hantro_postproc_free(ctx);
968 		if (ctx->codec_ops && ctx->codec_ops->exit)
969 			ctx->codec_ops->exit(ctx);
970 	}
971 
972 	/*
973 	 * The mem2mem framework calls v4l2_m2m_cancel_job before
974 	 * .stop_streaming, so there isn't any job running and
975 	 * it is safe to return all the buffers.
976 	 */
977 	if (V4L2_TYPE_IS_OUTPUT(q->type))
978 		hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
979 	else
980 		hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
981 
982 	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
983 
984 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
985 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
986 		v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
987 }
988 
989 static void hantro_buf_request_complete(struct vb2_buffer *vb)
990 {
991 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
992 
993 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
994 }
995 
996 static int hantro_buf_out_validate(struct vb2_buffer *vb)
997 {
998 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
999 
1000 	vbuf->field = V4L2_FIELD_NONE;
1001 	return 0;
1002 }
1003 
1004 const struct vb2_ops hantro_queue_ops = {
1005 	.queue_setup = hantro_queue_setup,
1006 	.buf_prepare = hantro_buf_prepare,
1007 	.buf_queue = hantro_buf_queue,
1008 	.buf_out_validate = hantro_buf_out_validate,
1009 	.buf_request_complete = hantro_buf_request_complete,
1010 	.start_streaming = hantro_start_streaming,
1011 	.stop_streaming = hantro_stop_streaming,
1012 	.wait_prepare = vb2_ops_wait_prepare,
1013 	.wait_finish = vb2_ops_wait_finish,
1014 };
1015