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