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