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 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
355 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
357 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
358 struct hantro_ctx *ctx = fh_to_ctx(priv);
359 const struct hantro_fmt *formats;
360 unsigned int num_fmts;
361 struct vb2_queue *vq;
364 /* Change not allowed if queue is busy. */
365 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
369 if (!hantro_is_encoder_ctx(ctx)) {
370 struct vb2_queue *peer_vq;
373 * Since format change on the OUTPUT queue will reset
374 * the CAPTURE queue, we can't allow doing so
375 * when the CAPTURE queue has buffers allocated.
377 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
378 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
379 if (vb2_is_busy(peer_vq))
383 ret = vidioc_try_fmt_out_mplane(file, priv, f);
387 formats = hantro_get_formats(ctx, &num_fmts);
388 ctx->vpu_src_fmt = hantro_find_format(formats, num_fmts,
389 pix_mp->pixelformat);
390 ctx->src_fmt = *pix_mp;
393 * Current raw format might have become invalid with newly
394 * selected codec, so reset it to default just to be safe and
395 * keep internal driver state sane. User is mandated to set
396 * the raw format again after we return, so we don't need
398 * Note that hantro_reset_raw_fmt() also propagates size
399 * changes to the raw format.
401 if (!hantro_is_encoder_ctx(ctx))
402 hantro_reset_raw_fmt(ctx);
404 /* Colorimetry information are always propagated. */
405 ctx->dst_fmt.colorspace = pix_mp->colorspace;
406 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
407 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
408 ctx->dst_fmt.quantization = pix_mp->quantization;
410 hantro_update_requires_request(ctx, pix_mp->pixelformat);
412 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
413 vpu_debug(0, "fmt - w: %d, h: %d\n",
414 pix_mp->width, pix_mp->height);
418 static int vidioc_s_fmt_cap_mplane(struct file *file, void *priv,
419 struct v4l2_format *f)
421 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
422 struct hantro_ctx *ctx = fh_to_ctx(priv);
423 const struct hantro_fmt *formats;
424 struct vb2_queue *vq;
425 unsigned int num_fmts;
428 /* Change not allowed if queue is busy. */
429 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
433 if (hantro_is_encoder_ctx(ctx)) {
434 struct vb2_queue *peer_vq;
437 * Since format change on the CAPTURE queue will reset
438 * the OUTPUT queue, we can't allow doing so
439 * when the OUTPUT queue has buffers allocated.
441 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
442 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
443 if (vb2_is_busy(peer_vq) &&
444 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
445 pix_mp->height != ctx->dst_fmt.height ||
446 pix_mp->width != ctx->dst_fmt.width))
450 ret = vidioc_try_fmt_cap_mplane(file, priv, f);
454 formats = hantro_get_formats(ctx, &num_fmts);
455 ctx->vpu_dst_fmt = hantro_find_format(formats, num_fmts,
456 pix_mp->pixelformat);
457 ctx->dst_fmt = *pix_mp;
460 * Current raw format might have become invalid with newly
461 * selected codec, so reset it to default just to be safe and
462 * keep internal driver state sane. User is mandated to set
463 * the raw format again after we return, so we don't need
465 * Note that hantro_reset_raw_fmt() also propagates size
466 * changes to the raw format.
468 if (hantro_is_encoder_ctx(ctx))
469 hantro_reset_raw_fmt(ctx);
471 /* Colorimetry information are always propagated. */
472 ctx->src_fmt.colorspace = pix_mp->colorspace;
473 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
474 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
475 ctx->src_fmt.quantization = pix_mp->quantization;
477 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
478 vpu_debug(0, "fmt - w: %d, h: %d\n",
479 pix_mp->width, pix_mp->height);
481 hantro_update_requires_request(ctx, pix_mp->pixelformat);
486 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
487 .vidioc_querycap = vidioc_querycap,
488 .vidioc_enum_framesizes = vidioc_enum_framesizes,
490 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
491 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
492 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
493 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
494 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
495 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
496 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
497 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
499 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
500 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
501 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
502 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
503 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
504 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
505 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
507 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
508 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
510 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
511 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
515 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
516 unsigned int *num_planes, unsigned int sizes[],
517 struct device *alloc_devs[])
519 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
520 struct v4l2_pix_format_mplane *pixfmt;
524 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
525 pixfmt = &ctx->dst_fmt;
527 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
528 pixfmt = &ctx->src_fmt;
531 vpu_err("invalid queue type: %d\n", vq->type);
536 if (*num_planes != pixfmt->num_planes)
538 for (i = 0; i < pixfmt->num_planes; ++i)
539 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
544 *num_planes = pixfmt->num_planes;
545 for (i = 0; i < pixfmt->num_planes; ++i)
546 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
551 hantro_buf_plane_check(struct vb2_buffer *vb, const struct hantro_fmt *vpu_fmt,
552 struct v4l2_pix_format_mplane *pixfmt)
557 for (i = 0; i < pixfmt->num_planes; ++i) {
558 sz = pixfmt->plane_fmt[i].sizeimage;
559 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
560 i, vb2_plane_size(vb, i), sz);
561 if (vb2_plane_size(vb, i) < sz) {
562 vpu_err("plane %d is too small for output\n", i);
569 static int hantro_buf_prepare(struct vb2_buffer *vb)
571 struct vb2_queue *vq = vb->vb2_queue;
572 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
574 if (V4L2_TYPE_IS_OUTPUT(vq->type))
575 return hantro_buf_plane_check(vb, ctx->vpu_src_fmt,
578 return hantro_buf_plane_check(vb, ctx->vpu_dst_fmt, &ctx->dst_fmt);
581 static void hantro_buf_queue(struct vb2_buffer *vb)
583 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
584 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
586 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
589 static bool hantro_vq_is_coded(struct vb2_queue *q)
591 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
593 return hantro_is_encoder_ctx(ctx) != V4L2_TYPE_IS_OUTPUT(q->type);
596 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
598 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
601 if (V4L2_TYPE_IS_OUTPUT(q->type))
602 ctx->sequence_out = 0;
604 ctx->sequence_cap = 0;
606 if (hantro_vq_is_coded(q)) {
607 enum hantro_codec_mode codec_mode;
609 if (V4L2_TYPE_IS_OUTPUT(q->type))
610 codec_mode = ctx->vpu_src_fmt->codec_mode;
612 codec_mode = ctx->vpu_dst_fmt->codec_mode;
614 vpu_debug(4, "Codec mode = %d\n", codec_mode);
615 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
616 if (ctx->codec_ops->init)
617 ret = ctx->codec_ops->init(ctx);
624 hantro_return_bufs(struct vb2_queue *q,
625 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
627 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
630 struct vb2_v4l2_buffer *vbuf;
632 vbuf = buf_remove(ctx->fh.m2m_ctx);
635 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
637 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
641 static void hantro_stop_streaming(struct vb2_queue *q)
643 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
645 if (hantro_vq_is_coded(q)) {
646 if (ctx->codec_ops && ctx->codec_ops->exit)
647 ctx->codec_ops->exit(ctx);
651 * The mem2mem framework calls v4l2_m2m_cancel_job before
652 * .stop_streaming, so there isn't any job running and
653 * it is safe to return all the buffers.
655 if (V4L2_TYPE_IS_OUTPUT(q->type))
656 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
658 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
661 static void hantro_buf_request_complete(struct vb2_buffer *vb)
663 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
665 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
668 static int hantro_buf_out_validate(struct vb2_buffer *vb)
670 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
672 vbuf->field = V4L2_FIELD_NONE;
676 const struct vb2_ops hantro_queue_ops = {
677 .queue_setup = hantro_queue_setup,
678 .buf_prepare = hantro_buf_prepare,
679 .buf_queue = hantro_buf_queue,
680 .buf_out_validate = hantro_buf_out_validate,
681 .buf_request_complete = hantro_buf_request_complete,
682 .start_streaming = hantro_start_streaming,
683 .stop_streaming = hantro_stop_streaming,
684 .wait_prepare = vb2_ops_wait_prepare,
685 .wait_finish = vb2_ops_wait_finish,