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