1 // SPDX-License-Identifier: GPL-2.0
3 * Hantro VPU codec driver
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>
10 * Copyright 2018 Google LLC.
11 * Tomasz Figa <tfiga@chromium.org>
13 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
14 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
26 #include <media/videobuf2-core.h>
27 #include <media/videobuf2-dma-sg.h>
30 #include "hantro_hw.h"
31 #include "hantro_v4l2.h"
33 static const struct hantro_fmt *
34 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts)
36 const struct hantro_fmt *formats;
38 if (hantro_is_encoder_ctx(ctx)) {
39 formats = ctx->dev->variant->enc_fmts;
40 *num_fmts = ctx->dev->variant->num_enc_fmts;
42 formats = ctx->dev->variant->dec_fmts;
43 *num_fmts = ctx->dev->variant->num_dec_fmts;
49 static const struct hantro_fmt *
50 hantro_find_format(const struct hantro_fmt *formats, unsigned int num_fmts,
55 for (i = 0; i < num_fmts; i++)
56 if (formats[i].fourcc == fourcc)
61 static const struct hantro_fmt *
62 hantro_get_default_fmt(const struct hantro_fmt *formats, unsigned int num_fmts,
67 for (i = 0; i < num_fmts; i++) {
68 if (bitstream == (formats[i].codec_mode !=
75 static int vidioc_querycap(struct file *file, void *priv,
76 struct v4l2_capability *cap)
78 struct hantro_dev *vpu = video_drvdata(file);
79 struct video_device *vdev = video_devdata(file);
81 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
82 strscpy(cap->card, vdev->name, sizeof(cap->card));
83 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
84 vpu->dev->driver->name);
88 static int vidioc_enum_framesizes(struct file *file, void *priv,
89 struct v4l2_frmsizeenum *fsize)
91 struct hantro_ctx *ctx = fh_to_ctx(priv);
92 const struct hantro_fmt *formats, *fmt;
93 unsigned int num_fmts;
95 if (fsize->index != 0) {
96 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
101 formats = hantro_get_formats(ctx, &num_fmts);
102 fmt = hantro_find_format(formats, num_fmts, fsize->pixel_format);
104 vpu_debug(0, "unsupported bitstream format (%08x)\n",
105 fsize->pixel_format);
109 /* This only makes sense for coded formats */
110 if (fmt->codec_mode == HANTRO_MODE_NONE)
113 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
114 fsize->stepwise = fmt->frmsize;
119 static int vidioc_enum_fmt(struct file *file, void *priv,
120 struct v4l2_fmtdesc *f, bool capture)
123 struct hantro_ctx *ctx = fh_to_ctx(priv);
124 const struct hantro_fmt *fmt, *formats;
125 unsigned int num_fmts, i, j = 0;
129 * When dealing with an encoder:
130 * - on the capture side we want to filter out all MODE_NONE formats.
131 * - on the output side we want to filter out all formats that are
133 * When dealing with a decoder:
134 * - on the capture side we want to filter out all formats that are
136 * - on the output side we want to filter out all MODE_NONE formats.
138 skip_mode_none = capture == hantro_is_encoder_ctx(ctx);
140 formats = hantro_get_formats(ctx, &num_fmts);
141 for (i = 0; i < num_fmts; i++) {
142 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
144 if (skip_mode_none == mode_none)
148 f->pixelformat = fmt->fourcc;
156 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
157 struct v4l2_fmtdesc *f)
159 return vidioc_enum_fmt(file, priv, f, true);
162 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
163 struct v4l2_fmtdesc *f)
165 return vidioc_enum_fmt(file, priv, f, false);
168 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
169 struct v4l2_format *f)
171 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
172 struct hantro_ctx *ctx = fh_to_ctx(priv);
174 vpu_debug(4, "f->type = %d\n", f->type);
176 *pix_mp = ctx->src_fmt;
181 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
182 struct v4l2_format *f)
184 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
185 struct hantro_ctx *ctx = fh_to_ctx(priv);
187 vpu_debug(4, "f->type = %d\n", f->type);
189 *pix_mp = ctx->dst_fmt;
194 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f,
197 struct hantro_ctx *ctx = fh_to_ctx(priv);
198 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
199 const struct hantro_fmt *formats, *fmt, *vpu_fmt;
200 unsigned int num_fmts;
203 coded = capture == hantro_is_encoder_ctx(ctx);
205 vpu_debug(4, "trying format %c%c%c%c\n",
206 (pix_mp->pixelformat & 0x7f),
207 (pix_mp->pixelformat >> 8) & 0x7f,
208 (pix_mp->pixelformat >> 16) & 0x7f,
209 (pix_mp->pixelformat >> 24) & 0x7f);
211 formats = hantro_get_formats(ctx, &num_fmts);
212 fmt = hantro_find_format(formats, num_fmts, pix_mp->pixelformat);
214 fmt = hantro_get_default_fmt(formats, num_fmts, coded);
215 f->fmt.pix_mp.pixelformat = fmt->fourcc;
219 pix_mp->num_planes = 1;
221 } else if (hantro_is_encoder_ctx(ctx)) {
222 vpu_fmt = ctx->vpu_dst_fmt;
224 vpu_fmt = ctx->vpu_src_fmt;
226 * Width/height on the CAPTURE end of a decoder are ignored and
227 * replaced by the OUTPUT ones.
229 pix_mp->width = ctx->src_fmt.width;
230 pix_mp->height = ctx->src_fmt.height;
233 pix_mp->field = V4L2_FIELD_NONE;
235 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
239 /* Fill remaining fields */
240 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
242 } else if (!pix_mp->plane_fmt[0].sizeimage) {
244 * For coded formats the application can specify
245 * sizeimage. If the application passes a zero sizeimage,
246 * let's default to the maximum frame size.
248 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
249 pix_mp->width * pix_mp->height * fmt->max_depth;
255 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
256 struct v4l2_format *f)
258 return vidioc_try_fmt(file, priv, f, true);
261 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
262 struct v4l2_format *f)
264 return vidioc_try_fmt(file, priv, f, false);
268 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
269 const struct hantro_fmt *vpu_fmt)
271 memset(fmt, 0, sizeof(*fmt));
273 fmt->pixelformat = vpu_fmt->fourcc;
274 fmt->field = V4L2_FIELD_NONE;
275 fmt->colorspace = V4L2_COLORSPACE_JPEG,
276 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
277 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
278 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
282 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
284 const struct hantro_fmt *vpu_fmt, *formats;
285 struct v4l2_pix_format_mplane *fmt;
286 unsigned int num_fmts;
288 formats = hantro_get_formats(ctx, &num_fmts);
289 vpu_fmt = hantro_get_default_fmt(formats, num_fmts, true);
291 if (hantro_is_encoder_ctx(ctx)) {
292 ctx->vpu_dst_fmt = vpu_fmt;
295 ctx->vpu_src_fmt = vpu_fmt;
299 hantro_reset_fmt(fmt, vpu_fmt);
301 fmt->width = vpu_fmt->frmsize.min_width;
302 fmt->height = vpu_fmt->frmsize.min_height;
303 fmt->plane_fmt[0].sizeimage = vpu_fmt->header_size +
304 fmt->width * fmt->height * vpu_fmt->max_depth;
308 hantro_reset_raw_fmt(struct hantro_ctx *ctx)
310 const struct hantro_fmt *raw_vpu_fmt, *formats;
311 struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt;
312 unsigned int num_fmts;
314 formats = hantro_get_formats(ctx, &num_fmts);
315 raw_vpu_fmt = hantro_get_default_fmt(formats, num_fmts, false);
317 if (hantro_is_encoder_ctx(ctx)) {
318 ctx->vpu_src_fmt = raw_vpu_fmt;
319 raw_fmt = &ctx->src_fmt;
320 encoded_fmt = &ctx->dst_fmt;
322 ctx->vpu_dst_fmt = raw_vpu_fmt;
323 raw_fmt = &ctx->dst_fmt;
324 encoded_fmt = &ctx->src_fmt;
327 hantro_reset_fmt(raw_fmt, raw_vpu_fmt);
328 v4l2_fill_pixfmt_mp(raw_fmt, raw_vpu_fmt->fourcc,
330 encoded_fmt->height);
333 void hantro_reset_fmts(struct hantro_ctx *ctx)
335 hantro_reset_encoded_fmt(ctx);
336 hantro_reset_raw_fmt(ctx);
340 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
343 case V4L2_PIX_FMT_JPEG:
344 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
346 case V4L2_PIX_FMT_MPEG2_SLICE:
347 case V4L2_PIX_FMT_VP8_FRAME:
348 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
356 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
358 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
359 struct hantro_ctx *ctx = fh_to_ctx(priv);
360 const struct hantro_fmt *formats;
361 unsigned int num_fmts;
362 struct vb2_queue *vq;
365 /* Change not allowed if queue is busy. */
366 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
370 if (!hantro_is_encoder_ctx(ctx)) {
371 struct vb2_queue *peer_vq;
374 * Since format change on the OUTPUT queue will reset
375 * the CAPTURE queue, we can't allow doing so
376 * when the CAPTURE queue has buffers allocated.
378 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
379 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
380 if (vb2_is_busy(peer_vq))
384 ret = vidioc_try_fmt_out_mplane(file, priv, f);
388 formats = hantro_get_formats(ctx, &num_fmts);
389 ctx->vpu_src_fmt = hantro_find_format(formats, num_fmts,
390 pix_mp->pixelformat);
391 ctx->src_fmt = *pix_mp;
394 * Current raw format might have become invalid with newly
395 * selected codec, so reset it to default just to be safe and
396 * keep internal driver state sane. User is mandated to set
397 * the raw format again after we return, so we don't need
399 * Note that hantro_reset_raw_fmt() also propagates size
400 * changes to the raw format.
402 if (!hantro_is_encoder_ctx(ctx))
403 hantro_reset_raw_fmt(ctx);
405 /* Colorimetry information are always propagated. */
406 ctx->dst_fmt.colorspace = pix_mp->colorspace;
407 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
408 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
409 ctx->dst_fmt.quantization = pix_mp->quantization;
411 hantro_update_requires_request(ctx, pix_mp->pixelformat);
413 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
414 vpu_debug(0, "fmt - w: %d, h: %d\n",
415 pix_mp->width, pix_mp->height);
419 static int vidioc_s_fmt_cap_mplane(struct file *file, void *priv,
420 struct v4l2_format *f)
422 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
423 struct hantro_ctx *ctx = fh_to_ctx(priv);
424 const struct hantro_fmt *formats;
425 struct vb2_queue *vq;
426 unsigned int num_fmts;
429 /* Change not allowed if queue is busy. */
430 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
434 if (hantro_is_encoder_ctx(ctx)) {
435 struct vb2_queue *peer_vq;
438 * Since format change on the CAPTURE queue will reset
439 * the OUTPUT queue, we can't allow doing so
440 * when the OUTPUT queue has buffers allocated.
442 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
443 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
444 if (vb2_is_busy(peer_vq) &&
445 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
446 pix_mp->height != ctx->dst_fmt.height ||
447 pix_mp->width != ctx->dst_fmt.width))
451 ret = vidioc_try_fmt_cap_mplane(file, priv, f);
455 formats = hantro_get_formats(ctx, &num_fmts);
456 ctx->vpu_dst_fmt = hantro_find_format(formats, num_fmts,
457 pix_mp->pixelformat);
458 ctx->dst_fmt = *pix_mp;
461 * Current raw format might have become invalid with newly
462 * selected codec, so reset it to default just to be safe and
463 * keep internal driver state sane. User is mandated to set
464 * the raw format again after we return, so we don't need
466 * Note that hantro_reset_raw_fmt() also propagates size
467 * changes to the raw format.
469 if (hantro_is_encoder_ctx(ctx))
470 hantro_reset_raw_fmt(ctx);
472 /* Colorimetry information are always propagated. */
473 ctx->src_fmt.colorspace = pix_mp->colorspace;
474 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
475 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
476 ctx->src_fmt.quantization = pix_mp->quantization;
478 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
479 vpu_debug(0, "fmt - w: %d, h: %d\n",
480 pix_mp->width, pix_mp->height);
482 hantro_update_requires_request(ctx, pix_mp->pixelformat);
487 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
488 .vidioc_querycap = vidioc_querycap,
489 .vidioc_enum_framesizes = vidioc_enum_framesizes,
491 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
492 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
493 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
494 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
495 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
496 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
497 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
498 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
500 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
501 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
502 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
503 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
504 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
505 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
506 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
508 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
509 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
511 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
512 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
516 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
517 unsigned int *num_planes, unsigned int sizes[],
518 struct device *alloc_devs[])
520 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
521 struct v4l2_pix_format_mplane *pixfmt;
525 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
526 pixfmt = &ctx->dst_fmt;
528 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
529 pixfmt = &ctx->src_fmt;
532 vpu_err("invalid queue type: %d\n", vq->type);
537 if (*num_planes != pixfmt->num_planes)
539 for (i = 0; i < pixfmt->num_planes; ++i)
540 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
545 *num_planes = pixfmt->num_planes;
546 for (i = 0; i < pixfmt->num_planes; ++i)
547 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
552 hantro_buf_plane_check(struct vb2_buffer *vb, const struct hantro_fmt *vpu_fmt,
553 struct v4l2_pix_format_mplane *pixfmt)
558 for (i = 0; i < pixfmt->num_planes; ++i) {
559 sz = pixfmt->plane_fmt[i].sizeimage;
560 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
561 i, vb2_plane_size(vb, i), sz);
562 if (vb2_plane_size(vb, i) < sz) {
563 vpu_err("plane %d is too small for output\n", i);
570 static int hantro_buf_prepare(struct vb2_buffer *vb)
572 struct vb2_queue *vq = vb->vb2_queue;
573 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
575 if (V4L2_TYPE_IS_OUTPUT(vq->type))
576 return hantro_buf_plane_check(vb, ctx->vpu_src_fmt,
579 return hantro_buf_plane_check(vb, ctx->vpu_dst_fmt, &ctx->dst_fmt);
582 static void hantro_buf_queue(struct vb2_buffer *vb)
584 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
585 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
587 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
590 static bool hantro_vq_is_coded(struct vb2_queue *q)
592 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
594 return hantro_is_encoder_ctx(ctx) != V4L2_TYPE_IS_OUTPUT(q->type);
597 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
599 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
602 if (V4L2_TYPE_IS_OUTPUT(q->type))
603 ctx->sequence_out = 0;
605 ctx->sequence_cap = 0;
607 if (hantro_vq_is_coded(q)) {
608 enum hantro_codec_mode codec_mode;
610 if (V4L2_TYPE_IS_OUTPUT(q->type))
611 codec_mode = ctx->vpu_src_fmt->codec_mode;
613 codec_mode = ctx->vpu_dst_fmt->codec_mode;
615 vpu_debug(4, "Codec mode = %d\n", codec_mode);
616 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
617 if (ctx->codec_ops->init)
618 ret = ctx->codec_ops->init(ctx);
625 hantro_return_bufs(struct vb2_queue *q,
626 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
628 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
631 struct vb2_v4l2_buffer *vbuf;
633 vbuf = buf_remove(ctx->fh.m2m_ctx);
636 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
638 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
642 static void hantro_stop_streaming(struct vb2_queue *q)
644 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
646 if (hantro_vq_is_coded(q)) {
647 if (ctx->codec_ops && ctx->codec_ops->exit)
648 ctx->codec_ops->exit(ctx);
652 * The mem2mem framework calls v4l2_m2m_cancel_job before
653 * .stop_streaming, so there isn't any job running and
654 * it is safe to return all the buffers.
656 if (V4L2_TYPE_IS_OUTPUT(q->type))
657 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
659 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
662 static void hantro_buf_request_complete(struct vb2_buffer *vb)
664 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
666 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
669 static int hantro_buf_out_validate(struct vb2_buffer *vb)
671 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
673 vbuf->field = V4L2_FIELD_NONE;
677 const struct vb2_ops hantro_queue_ops = {
678 .queue_setup = hantro_queue_setup,
679 .buf_prepare = hantro_buf_prepare,
680 .buf_queue = hantro_buf_queue,
681 .buf_out_validate = hantro_buf_out_validate,
682 .buf_request_complete = hantro_buf_request_complete,
683 .start_streaming = hantro_start_streaming,
684 .stop_streaming = hantro_stop_streaming,
685 .wait_prepare = vb2_ops_wait_prepare,
686 .wait_finish = vb2_ops_wait_finish,