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