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 #include <media/videobuf2-core.h>
27 #include <media/videobuf2-dma-sg.h>
28 
29 #include "hantro.h"
30 #include "hantro_hw.h"
31 #include "hantro_v4l2.h"
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 *
hantro_get_formats(const struct hantro_ctx * ctx,unsigned int * num_fmts)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 *
hantro_get_postproc_formats(const struct hantro_ctx * ctx,unsigned int * num_fmts)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 static const struct hantro_fmt *
hantro_find_format(const struct hantro_ctx * ctx,u32 fourcc)70 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
71 {
72 	const struct hantro_fmt *formats;
73 	unsigned int i, num_fmts;
74 
75 	formats = hantro_get_formats(ctx, &num_fmts);
76 	for (i = 0; i < num_fmts; i++)
77 		if (formats[i].fourcc == fourcc)
78 			return &formats[i];
79 
80 	formats = hantro_get_postproc_formats(ctx, &num_fmts);
81 	for (i = 0; i < num_fmts; i++)
82 		if (formats[i].fourcc == fourcc)
83 			return &formats[i];
84 	return NULL;
85 }
86 
87 static const struct hantro_fmt *
hantro_get_default_fmt(const struct hantro_ctx * ctx,bool bitstream)88 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream)
89 {
90 	const struct hantro_fmt *formats;
91 	unsigned int i, num_fmts;
92 
93 	formats = hantro_get_formats(ctx, &num_fmts);
94 	for (i = 0; i < num_fmts; i++) {
95 		if (bitstream == (formats[i].codec_mode !=
96 				  HANTRO_MODE_NONE))
97 			return &formats[i];
98 	}
99 	return NULL;
100 }
101 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)102 static int vidioc_querycap(struct file *file, void *priv,
103 			   struct v4l2_capability *cap)
104 {
105 	struct hantro_dev *vpu = video_drvdata(file);
106 	struct video_device *vdev = video_devdata(file);
107 
108 	strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
109 	strscpy(cap->card, vdev->name, sizeof(cap->card));
110 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
111 		 vpu->dev->driver->name);
112 	return 0;
113 }
114 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)115 static int vidioc_enum_framesizes(struct file *file, void *priv,
116 				  struct v4l2_frmsizeenum *fsize)
117 {
118 	struct hantro_ctx *ctx = fh_to_ctx(priv);
119 	const struct hantro_fmt *fmt;
120 
121 	if (fsize->index != 0) {
122 		vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
123 			  fsize->index);
124 		return -EINVAL;
125 	}
126 
127 	fmt = hantro_find_format(ctx, fsize->pixel_format);
128 	if (!fmt) {
129 		vpu_debug(0, "unsupported bitstream format (%08x)\n",
130 			  fsize->pixel_format);
131 		return -EINVAL;
132 	}
133 
134 	/* This only makes sense for coded formats */
135 	if (fmt->codec_mode == HANTRO_MODE_NONE)
136 		return -EINVAL;
137 
138 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
139 	fsize->stepwise = fmt->frmsize;
140 
141 	return 0;
142 }
143 
vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f,bool capture)144 static int vidioc_enum_fmt(struct file *file, void *priv,
145 			   struct v4l2_fmtdesc *f, bool capture)
146 
147 {
148 	struct hantro_ctx *ctx = fh_to_ctx(priv);
149 	const struct hantro_fmt *fmt, *formats;
150 	unsigned int num_fmts, i, j = 0;
151 	bool skip_mode_none;
152 
153 	/*
154 	 * When dealing with an encoder:
155 	 *  - on the capture side we want to filter out all MODE_NONE formats.
156 	 *  - on the output side we want to filter out all formats that are
157 	 *    not MODE_NONE.
158 	 * When dealing with a decoder:
159 	 *  - on the capture side we want to filter out all formats that are
160 	 *    not MODE_NONE.
161 	 *  - on the output side we want to filter out all MODE_NONE formats.
162 	 */
163 	skip_mode_none = capture == ctx->is_encoder;
164 
165 	formats = hantro_get_formats(ctx, &num_fmts);
166 	for (i = 0; i < num_fmts; i++) {
167 		bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
168 
169 		if (skip_mode_none == mode_none)
170 			continue;
171 		if (j == f->index) {
172 			fmt = &formats[i];
173 			f->pixelformat = fmt->fourcc;
174 			return 0;
175 		}
176 		++j;
177 	}
178 
179 	/*
180 	 * Enumerate post-processed formats. As per the specification,
181 	 * we enumerated these formats after natively decoded formats
182 	 * as a hint for applications on what's the preferred fomat.
183 	 */
184 	if (!capture)
185 		return -EINVAL;
186 	formats = hantro_get_postproc_formats(ctx, &num_fmts);
187 	for (i = 0; i < num_fmts; i++) {
188 		if (j == f->index) {
189 			fmt = &formats[i];
190 			f->pixelformat = fmt->fourcc;
191 			return 0;
192 		}
193 		++j;
194 	}
195 
196 	return -EINVAL;
197 }
198 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)199 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
200 				   struct v4l2_fmtdesc *f)
201 {
202 	return vidioc_enum_fmt(file, priv, f, true);
203 }
204 
vidioc_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)205 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
206 				   struct v4l2_fmtdesc *f)
207 {
208 	return vidioc_enum_fmt(file, priv, f, false);
209 }
210 
vidioc_g_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)211 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
212 				   struct v4l2_format *f)
213 {
214 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
215 	struct hantro_ctx *ctx = fh_to_ctx(priv);
216 
217 	vpu_debug(4, "f->type = %d\n", f->type);
218 
219 	*pix_mp = ctx->src_fmt;
220 
221 	return 0;
222 }
223 
vidioc_g_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)224 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
225 				   struct v4l2_format *f)
226 {
227 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
228 	struct hantro_ctx *ctx = fh_to_ctx(priv);
229 
230 	vpu_debug(4, "f->type = %d\n", f->type);
231 
232 	*pix_mp = ctx->dst_fmt;
233 
234 	return 0;
235 }
236 
hantro_try_fmt(const struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp,enum v4l2_buf_type type)237 static int hantro_try_fmt(const struct hantro_ctx *ctx,
238 			  struct v4l2_pix_format_mplane *pix_mp,
239 			  enum v4l2_buf_type type)
240 {
241 	const struct hantro_fmt *fmt, *vpu_fmt;
242 	bool capture = V4L2_TYPE_IS_CAPTURE(type);
243 	bool coded;
244 
245 	coded = capture == ctx->is_encoder;
246 
247 	vpu_debug(4, "trying format %c%c%c%c\n",
248 		  (pix_mp->pixelformat & 0x7f),
249 		  (pix_mp->pixelformat >> 8) & 0x7f,
250 		  (pix_mp->pixelformat >> 16) & 0x7f,
251 		  (pix_mp->pixelformat >> 24) & 0x7f);
252 
253 	fmt = hantro_find_format(ctx, pix_mp->pixelformat);
254 	if (!fmt) {
255 		fmt = hantro_get_default_fmt(ctx, coded);
256 		pix_mp->pixelformat = fmt->fourcc;
257 	}
258 
259 	if (coded) {
260 		pix_mp->num_planes = 1;
261 		vpu_fmt = fmt;
262 	} else if (ctx->is_encoder) {
263 		vpu_fmt = ctx->vpu_dst_fmt;
264 	} else {
265 		vpu_fmt = ctx->vpu_src_fmt;
266 		/*
267 		 * Width/height on the CAPTURE end of a decoder are ignored and
268 		 * replaced by the OUTPUT ones.
269 		 */
270 		pix_mp->width = ctx->src_fmt.width;
271 		pix_mp->height = ctx->src_fmt.height;
272 	}
273 
274 	pix_mp->field = V4L2_FIELD_NONE;
275 
276 	v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
277 				       &vpu_fmt->frmsize);
278 
279 	if (!coded) {
280 		/* Fill remaining fields */
281 		v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
282 				    pix_mp->height);
283 		if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
284 		    !hantro_needs_postproc(ctx, fmt))
285 			pix_mp->plane_fmt[0].sizeimage +=
286 				hantro_h264_mv_size(pix_mp->width,
287 						    pix_mp->height);
288 	} else if (!pix_mp->plane_fmt[0].sizeimage) {
289 		/*
290 		 * For coded formats the application can specify
291 		 * sizeimage. If the application passes a zero sizeimage,
292 		 * let's default to the maximum frame size.
293 		 */
294 		pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
295 			pix_mp->width * pix_mp->height * fmt->max_depth;
296 	}
297 
298 	return 0;
299 }
300 
vidioc_try_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)301 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
302 				     struct v4l2_format *f)
303 {
304 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
305 }
306 
vidioc_try_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)307 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
308 				     struct v4l2_format *f)
309 {
310 	return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
311 }
312 
313 static void
hantro_reset_fmt(struct v4l2_pix_format_mplane * fmt,const struct hantro_fmt * vpu_fmt)314 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
315 		 const struct hantro_fmt *vpu_fmt)
316 {
317 	memset(fmt, 0, sizeof(*fmt));
318 
319 	fmt->pixelformat = vpu_fmt->fourcc;
320 	fmt->field = V4L2_FIELD_NONE;
321 	fmt->colorspace = V4L2_COLORSPACE_JPEG;
322 	fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
323 	fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
324 	fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
325 }
326 
327 static void
hantro_reset_encoded_fmt(struct hantro_ctx * ctx)328 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
329 {
330 	const struct hantro_fmt *vpu_fmt;
331 	struct v4l2_pix_format_mplane *fmt;
332 
333 	vpu_fmt = hantro_get_default_fmt(ctx, true);
334 
335 	if (ctx->is_encoder) {
336 		ctx->vpu_dst_fmt = vpu_fmt;
337 		fmt = &ctx->dst_fmt;
338 	} else {
339 		ctx->vpu_src_fmt = vpu_fmt;
340 		fmt = &ctx->src_fmt;
341 	}
342 
343 	hantro_reset_fmt(fmt, vpu_fmt);
344 	fmt->width = vpu_fmt->frmsize.min_width;
345 	fmt->height = vpu_fmt->frmsize.min_height;
346 	if (ctx->is_encoder)
347 		hantro_set_fmt_cap(ctx, fmt);
348 	else
349 		hantro_set_fmt_out(ctx, fmt);
350 }
351 
352 static void
hantro_reset_raw_fmt(struct hantro_ctx * ctx)353 hantro_reset_raw_fmt(struct hantro_ctx *ctx)
354 {
355 	const struct hantro_fmt *raw_vpu_fmt;
356 	struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt;
357 
358 	raw_vpu_fmt = hantro_get_default_fmt(ctx, false);
359 
360 	if (ctx->is_encoder) {
361 		ctx->vpu_src_fmt = raw_vpu_fmt;
362 		raw_fmt = &ctx->src_fmt;
363 		encoded_fmt = &ctx->dst_fmt;
364 	} else {
365 		ctx->vpu_dst_fmt = raw_vpu_fmt;
366 		raw_fmt = &ctx->dst_fmt;
367 		encoded_fmt = &ctx->src_fmt;
368 	}
369 
370 	hantro_reset_fmt(raw_fmt, raw_vpu_fmt);
371 	raw_fmt->width = encoded_fmt->width;
372 	raw_fmt->height = encoded_fmt->height;
373 	if (ctx->is_encoder)
374 		hantro_set_fmt_out(ctx, raw_fmt);
375 	else
376 		hantro_set_fmt_cap(ctx, raw_fmt);
377 }
378 
hantro_reset_fmts(struct hantro_ctx * ctx)379 void hantro_reset_fmts(struct hantro_ctx *ctx)
380 {
381 	hantro_reset_encoded_fmt(ctx);
382 	hantro_reset_raw_fmt(ctx);
383 }
384 
385 static void
hantro_update_requires_request(struct hantro_ctx * ctx,u32 fourcc)386 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
387 {
388 	switch (fourcc) {
389 	case V4L2_PIX_FMT_JPEG:
390 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
391 		break;
392 	case V4L2_PIX_FMT_MPEG2_SLICE:
393 	case V4L2_PIX_FMT_VP8_FRAME:
394 	case V4L2_PIX_FMT_H264_SLICE:
395 	case V4L2_PIX_FMT_HEVC_SLICE:
396 		ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
397 		break;
398 	default:
399 		break;
400 	}
401 }
402 
hantro_set_fmt_out(struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp)403 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
404 			      struct v4l2_pix_format_mplane *pix_mp)
405 {
406 	struct vb2_queue *vq;
407 	int ret;
408 
409 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
410 			     V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
411 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
412 	if (ret)
413 		return ret;
414 
415 	if (!ctx->is_encoder) {
416 		struct vb2_queue *peer_vq;
417 
418 		/*
419 		 * In order to support dynamic resolution change,
420 		 * the decoder admits a resolution change, as long
421 		 * as the pixelformat remains. Can't be done if streaming.
422 		 */
423 		if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
424 		    pix_mp->pixelformat != ctx->src_fmt.pixelformat))
425 			return -EBUSY;
426 		/*
427 		 * Since format change on the OUTPUT queue will reset
428 		 * the CAPTURE queue, we can't allow doing so
429 		 * when the CAPTURE queue has buffers allocated.
430 		 */
431 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
432 					  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
433 		if (vb2_is_busy(peer_vq))
434 			return -EBUSY;
435 	} else {
436 		/*
437 		 * The encoder doesn't admit a format change if
438 		 * there are OUTPUT buffers allocated.
439 		 */
440 		if (vb2_is_busy(vq))
441 			return -EBUSY;
442 	}
443 
444 	ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
445 	ctx->src_fmt = *pix_mp;
446 
447 	/*
448 	 * Current raw format might have become invalid with newly
449 	 * selected codec, so reset it to default just to be safe and
450 	 * keep internal driver state sane. User is mandated to set
451 	 * the raw format again after we return, so we don't need
452 	 * anything smarter.
453 	 * Note that hantro_reset_raw_fmt() also propagates size
454 	 * changes to the raw format.
455 	 */
456 	if (!ctx->is_encoder)
457 		hantro_reset_raw_fmt(ctx);
458 
459 	/* Colorimetry information are always propagated. */
460 	ctx->dst_fmt.colorspace = pix_mp->colorspace;
461 	ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
462 	ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
463 	ctx->dst_fmt.quantization = pix_mp->quantization;
464 
465 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
466 
467 	vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
468 	vpu_debug(0, "fmt - w: %d, h: %d\n",
469 		  pix_mp->width, pix_mp->height);
470 	return 0;
471 }
472 
hantro_set_fmt_cap(struct hantro_ctx * ctx,struct v4l2_pix_format_mplane * pix_mp)473 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
474 			      struct v4l2_pix_format_mplane *pix_mp)
475 {
476 	struct vb2_queue *vq;
477 	int ret;
478 
479 	/* Change not allowed if queue is busy. */
480 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
481 			     V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
482 	if (vb2_is_busy(vq))
483 		return -EBUSY;
484 
485 	if (ctx->is_encoder) {
486 		struct vb2_queue *peer_vq;
487 
488 		/*
489 		 * Since format change on the CAPTURE queue will reset
490 		 * the OUTPUT queue, we can't allow doing so
491 		 * when the OUTPUT queue has buffers allocated.
492 		 */
493 		peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
494 					  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
495 		if (vb2_is_busy(peer_vq) &&
496 		    (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
497 		     pix_mp->height != ctx->dst_fmt.height ||
498 		     pix_mp->width != ctx->dst_fmt.width))
499 			return -EBUSY;
500 	}
501 
502 	ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
503 	if (ret)
504 		return ret;
505 
506 	ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
507 	ctx->dst_fmt = *pix_mp;
508 
509 	/*
510 	 * Current raw format might have become invalid with newly
511 	 * selected codec, so reset it to default just to be safe and
512 	 * keep internal driver state sane. User is mandated to set
513 	 * the raw format again after we return, so we don't need
514 	 * anything smarter.
515 	 * Note that hantro_reset_raw_fmt() also propagates size
516 	 * changes to the raw format.
517 	 */
518 	if (ctx->is_encoder)
519 		hantro_reset_raw_fmt(ctx);
520 
521 	/* Colorimetry information are always propagated. */
522 	ctx->src_fmt.colorspace = pix_mp->colorspace;
523 	ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
524 	ctx->src_fmt.xfer_func = pix_mp->xfer_func;
525 	ctx->src_fmt.quantization = pix_mp->quantization;
526 
527 	vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
528 	vpu_debug(0, "fmt - w: %d, h: %d\n",
529 		  pix_mp->width, pix_mp->height);
530 
531 	hantro_update_requires_request(ctx, pix_mp->pixelformat);
532 
533 	return 0;
534 }
535 
536 static int
vidioc_s_fmt_out_mplane(struct file * file,void * priv,struct v4l2_format * f)537 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
538 {
539 	return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp);
540 }
541 
542 static int
vidioc_s_fmt_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)543 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
544 {
545 	return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
546 }
547 
548 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
549 	.vidioc_querycap = vidioc_querycap,
550 	.vidioc_enum_framesizes = vidioc_enum_framesizes,
551 
552 	.vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
553 	.vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
554 	.vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
555 	.vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
556 	.vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
557 	.vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
558 	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
559 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
560 
561 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
562 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
563 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
564 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
565 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
566 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
567 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
568 
569 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
570 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
571 
572 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
573 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
574 };
575 
576 static int
hantro_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])577 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
578 		   unsigned int *num_planes, unsigned int sizes[],
579 		   struct device *alloc_devs[])
580 {
581 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
582 	struct v4l2_pix_format_mplane *pixfmt;
583 	int i;
584 
585 	switch (vq->type) {
586 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
587 		pixfmt = &ctx->dst_fmt;
588 		break;
589 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
590 		pixfmt = &ctx->src_fmt;
591 		break;
592 	default:
593 		vpu_err("invalid queue type: %d\n", vq->type);
594 		return -EINVAL;
595 	}
596 
597 	if (*num_planes) {
598 		if (*num_planes != pixfmt->num_planes)
599 			return -EINVAL;
600 		for (i = 0; i < pixfmt->num_planes; ++i)
601 			if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
602 				return -EINVAL;
603 		return 0;
604 	}
605 
606 	*num_planes = pixfmt->num_planes;
607 	for (i = 0; i < pixfmt->num_planes; ++i)
608 		sizes[i] = pixfmt->plane_fmt[i].sizeimage;
609 	return 0;
610 }
611 
612 static int
hantro_buf_plane_check(struct vb2_buffer * vb,struct v4l2_pix_format_mplane * pixfmt)613 hantro_buf_plane_check(struct vb2_buffer *vb,
614 		       struct v4l2_pix_format_mplane *pixfmt)
615 {
616 	unsigned int sz;
617 	int i;
618 
619 	for (i = 0; i < pixfmt->num_planes; ++i) {
620 		sz = pixfmt->plane_fmt[i].sizeimage;
621 		vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
622 			  i, vb2_plane_size(vb, i), sz);
623 		if (vb2_plane_size(vb, i) < sz) {
624 			vpu_err("plane %d is too small for output\n", i);
625 			return -EINVAL;
626 		}
627 	}
628 	return 0;
629 }
630 
hantro_buf_prepare(struct vb2_buffer * vb)631 static int hantro_buf_prepare(struct vb2_buffer *vb)
632 {
633 	struct vb2_queue *vq = vb->vb2_queue;
634 	struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
635 	struct v4l2_pix_format_mplane *pix_fmt;
636 	int ret;
637 
638 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
639 		pix_fmt = &ctx->src_fmt;
640 	else
641 		pix_fmt = &ctx->dst_fmt;
642 	ret = hantro_buf_plane_check(vb, pix_fmt);
643 	if (ret)
644 		return ret;
645 	/*
646 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
647 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
648 	 * it to buffer length).
649 	 */
650 	if (V4L2_TYPE_IS_CAPTURE(vq->type))
651 		vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
652 
653 	return 0;
654 }
655 
hantro_buf_queue(struct vb2_buffer * vb)656 static void hantro_buf_queue(struct vb2_buffer *vb)
657 {
658 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
659 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
660 
661 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
662 }
663 
hantro_vq_is_coded(struct vb2_queue * q)664 static bool hantro_vq_is_coded(struct vb2_queue *q)
665 {
666 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
667 
668 	return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
669 }
670 
hantro_start_streaming(struct vb2_queue * q,unsigned int count)671 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
672 {
673 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
674 	int ret = 0;
675 
676 	if (V4L2_TYPE_IS_OUTPUT(q->type))
677 		ctx->sequence_out = 0;
678 	else
679 		ctx->sequence_cap = 0;
680 
681 	if (hantro_vq_is_coded(q)) {
682 		enum hantro_codec_mode codec_mode;
683 
684 		if (V4L2_TYPE_IS_OUTPUT(q->type))
685 			codec_mode = ctx->vpu_src_fmt->codec_mode;
686 		else
687 			codec_mode = ctx->vpu_dst_fmt->codec_mode;
688 
689 		vpu_debug(4, "Codec mode = %d\n", codec_mode);
690 		ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
691 		if (ctx->codec_ops->init) {
692 			ret = ctx->codec_ops->init(ctx);
693 			if (ret)
694 				return ret;
695 		}
696 
697 		if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
698 			ret = hantro_postproc_alloc(ctx);
699 			if (ret)
700 				goto err_codec_exit;
701 		}
702 	}
703 	return ret;
704 
705 err_codec_exit:
706 	if (ctx->codec_ops->exit)
707 		ctx->codec_ops->exit(ctx);
708 	return ret;
709 }
710 
711 static void
hantro_return_bufs(struct vb2_queue * q,struct vb2_v4l2_buffer * (* buf_remove)(struct v4l2_m2m_ctx *))712 hantro_return_bufs(struct vb2_queue *q,
713 		   struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
714 {
715 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
716 
717 	for (;;) {
718 		struct vb2_v4l2_buffer *vbuf;
719 
720 		vbuf = buf_remove(ctx->fh.m2m_ctx);
721 		if (!vbuf)
722 			break;
723 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
724 					   &ctx->ctrl_handler);
725 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
726 	}
727 }
728 
hantro_stop_streaming(struct vb2_queue * q)729 static void hantro_stop_streaming(struct vb2_queue *q)
730 {
731 	struct hantro_ctx *ctx = vb2_get_drv_priv(q);
732 
733 	if (hantro_vq_is_coded(q)) {
734 		hantro_postproc_free(ctx);
735 		if (ctx->codec_ops && ctx->codec_ops->exit)
736 			ctx->codec_ops->exit(ctx);
737 	}
738 
739 	/*
740 	 * The mem2mem framework calls v4l2_m2m_cancel_job before
741 	 * .stop_streaming, so there isn't any job running and
742 	 * it is safe to return all the buffers.
743 	 */
744 	if (V4L2_TYPE_IS_OUTPUT(q->type))
745 		hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
746 	else
747 		hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
748 }
749 
hantro_buf_request_complete(struct vb2_buffer * vb)750 static void hantro_buf_request_complete(struct vb2_buffer *vb)
751 {
752 	struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
753 
754 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
755 }
756 
hantro_buf_out_validate(struct vb2_buffer * vb)757 static int hantro_buf_out_validate(struct vb2_buffer *vb)
758 {
759 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
760 
761 	vbuf->field = V4L2_FIELD_NONE;
762 	return 0;
763 }
764 
765 const struct vb2_ops hantro_queue_ops = {
766 	.queue_setup = hantro_queue_setup,
767 	.buf_prepare = hantro_buf_prepare,
768 	.buf_queue = hantro_buf_queue,
769 	.buf_out_validate = hantro_buf_out_validate,
770 	.buf_request_complete = hantro_buf_request_complete,
771 	.start_streaming = hantro_start_streaming,
772 	.stop_streaming = hantro_stop_streaming,
773 	.wait_prepare = vb2_ops_wait_prepare,
774 	.wait_finish = vb2_ops_wait_finish,
775 };
776