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