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