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