1 // SPDX-License-Identifier: GPL-2.0
3 * Rockchip 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>
29 #include "rockchip_vpu.h"
30 #include "rockchip_vpu_hw.h"
31 #include "rockchip_vpu_common.h"
33 static const struct rockchip_vpu_fmt *
34 rockchip_vpu_find_format(struct rockchip_vpu_ctx *ctx, u32 fourcc)
36 struct rockchip_vpu_dev *dev = ctx->dev;
37 const struct rockchip_vpu_fmt *formats;
38 unsigned int num_fmts, i;
40 formats = dev->variant->enc_fmts;
41 num_fmts = dev->variant->num_enc_fmts;
42 for (i = 0; i < num_fmts; i++)
43 if (formats[i].fourcc == fourcc)
48 static const struct rockchip_vpu_fmt *
49 rockchip_vpu_get_default_fmt(struct rockchip_vpu_ctx *ctx, bool bitstream)
51 struct rockchip_vpu_dev *dev = ctx->dev;
52 const struct rockchip_vpu_fmt *formats;
53 unsigned int num_fmts, i;
55 formats = dev->variant->enc_fmts;
56 num_fmts = dev->variant->num_enc_fmts;
57 for (i = 0; i < num_fmts; i++) {
58 if (bitstream == (formats[i].codec_mode != RK_VPU_MODE_NONE))
64 static int vidioc_querycap(struct file *file, void *priv,
65 struct v4l2_capability *cap)
67 struct rockchip_vpu_dev *vpu = video_drvdata(file);
68 struct video_device *vdev = video_devdata(file);
70 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
71 strscpy(cap->card, vdev->name, sizeof(cap->card));
72 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
73 vpu->dev->driver->name);
77 static int vidioc_enum_framesizes(struct file *file, void *priv,
78 struct v4l2_frmsizeenum *fsize)
80 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
81 const struct rockchip_vpu_fmt *fmt;
83 if (fsize->index != 0) {
84 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
89 fmt = rockchip_vpu_find_format(ctx, fsize->pixel_format);
91 vpu_debug(0, "unsupported bitstream format (%08x)\n",
96 /* This only makes sense for coded formats */
97 if (fmt->codec_mode == RK_VPU_MODE_NONE)
100 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
101 fsize->stepwise = fmt->frmsize;
106 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
107 struct v4l2_fmtdesc *f)
109 struct rockchip_vpu_dev *dev = video_drvdata(file);
110 const struct rockchip_vpu_fmt *fmt;
111 const struct rockchip_vpu_fmt *formats;
112 int num_fmts, i, j = 0;
114 formats = dev->variant->enc_fmts;
115 num_fmts = dev->variant->num_enc_fmts;
116 for (i = 0; i < num_fmts; i++) {
117 /* Skip uncompressed formats */
118 if (formats[i].codec_mode == RK_VPU_MODE_NONE)
122 f->pixelformat = fmt->fourcc;
130 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
131 struct v4l2_fmtdesc *f)
133 struct rockchip_vpu_dev *dev = video_drvdata(file);
134 const struct rockchip_vpu_fmt *formats;
135 const struct rockchip_vpu_fmt *fmt;
136 int num_fmts, i, j = 0;
138 formats = dev->variant->enc_fmts;
139 num_fmts = dev->variant->num_enc_fmts;
140 for (i = 0; i < num_fmts; i++) {
141 if (formats[i].codec_mode != RK_VPU_MODE_NONE)
145 f->pixelformat = fmt->fourcc;
153 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
154 struct v4l2_format *f)
156 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
157 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
159 vpu_debug(4, "f->type = %d\n", f->type);
161 *pix_mp = ctx->src_fmt;
166 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
167 struct v4l2_format *f)
169 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
170 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
172 vpu_debug(4, "f->type = %d\n", f->type);
174 *pix_mp = ctx->dst_fmt;
180 vidioc_try_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
182 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
183 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
184 const struct rockchip_vpu_fmt *fmt;
186 vpu_debug(4, "%c%c%c%c\n",
187 (pix_mp->pixelformat & 0x7f),
188 (pix_mp->pixelformat >> 8) & 0x7f,
189 (pix_mp->pixelformat >> 16) & 0x7f,
190 (pix_mp->pixelformat >> 24) & 0x7f);
192 fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
194 fmt = rockchip_vpu_get_default_fmt(ctx, true);
195 f->fmt.pix.pixelformat = fmt->fourcc;
198 pix_mp->num_planes = 1;
199 pix_mp->field = V4L2_FIELD_NONE;
201 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
205 * For compressed formats the application can specify
206 * sizeimage. If the application passes a zero sizeimage,
207 * let's default to the maximum frame size.
209 if (!pix_mp->plane_fmt[0].sizeimage)
210 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
211 pix_mp->width * pix_mp->height * fmt->max_depth;
212 memset(pix_mp->plane_fmt[0].reserved, 0,
213 sizeof(pix_mp->plane_fmt[0].reserved));
218 vidioc_try_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
220 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
221 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
222 const struct rockchip_vpu_fmt *fmt;
225 vpu_debug(4, "%c%c%c%c\n",
226 (pix_mp->pixelformat & 0x7f),
227 (pix_mp->pixelformat >> 8) & 0x7f,
228 (pix_mp->pixelformat >> 16) & 0x7f,
229 (pix_mp->pixelformat >> 24) & 0x7f);
231 fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
233 fmt = rockchip_vpu_get_default_fmt(ctx, false);
234 f->fmt.pix.pixelformat = fmt->fourcc;
237 pix_mp->field = V4L2_FIELD_NONE;
239 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
240 &ctx->vpu_dst_fmt->frmsize);
242 /* Fill remaining fields */
243 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
246 for (i = 0; i < pix_mp->num_planes; i++) {
247 memset(pix_mp->plane_fmt[i].reserved, 0,
248 sizeof(pix_mp->plane_fmt[i].reserved));
253 void rockchip_vpu_enc_reset_dst_fmt(struct rockchip_vpu_dev *vpu,
254 struct rockchip_vpu_ctx *ctx)
256 struct v4l2_pix_format_mplane *fmt = &ctx->dst_fmt;
258 ctx->vpu_dst_fmt = rockchip_vpu_get_default_fmt(ctx, true);
260 memset(fmt, 0, sizeof(*fmt));
263 v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
264 &ctx->vpu_dst_fmt->frmsize);
265 fmt->pixelformat = ctx->vpu_dst_fmt->fourcc;
266 fmt->field = V4L2_FIELD_NONE;
267 fmt->colorspace = V4L2_COLORSPACE_JPEG,
268 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
269 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
270 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
272 fmt->plane_fmt[0].sizeimage = ctx->vpu_dst_fmt->header_size +
273 fmt->width * fmt->height * ctx->vpu_dst_fmt->max_depth;
276 void rockchip_vpu_enc_reset_src_fmt(struct rockchip_vpu_dev *vpu,
277 struct rockchip_vpu_ctx *ctx)
279 struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
281 ctx->vpu_src_fmt = rockchip_vpu_get_default_fmt(ctx, false);
283 memset(fmt, 0, sizeof(*fmt));
285 v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
286 &ctx->vpu_src_fmt->frmsize);
287 fmt->field = V4L2_FIELD_NONE;
288 fmt->colorspace = V4L2_COLORSPACE_JPEG,
289 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
290 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
291 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
293 v4l2_fill_pixfmt_mp(fmt, ctx->vpu_src_fmt->fourcc, fmt->width,
298 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
300 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
301 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
302 struct vb2_queue *vq;
305 /* Change not allowed if queue is streaming. */
306 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
307 if (vb2_is_streaming(vq))
310 ret = vidioc_try_fmt_out_mplane(file, priv, f);
314 ctx->vpu_src_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
315 ctx->src_fmt = *pix_mp;
317 /* Propagate to the CAPTURE format */
318 ctx->dst_fmt.colorspace = pix_mp->colorspace;
319 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
320 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
321 ctx->dst_fmt.quantization = pix_mp->quantization;
322 ctx->dst_fmt.width = pix_mp->width;
323 ctx->dst_fmt.height = pix_mp->height;
325 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
326 vpu_debug(0, "fmt - w: %d, h: %d\n",
327 pix_mp->width, pix_mp->height);
332 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
334 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
335 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
336 struct rockchip_vpu_dev *vpu = ctx->dev;
337 struct vb2_queue *vq, *peer_vq;
340 /* Change not allowed if queue is streaming. */
341 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
342 if (vb2_is_streaming(vq))
346 * Since format change on the CAPTURE queue will reset
347 * the OUTPUT queue, we can't allow doing so
348 * when the OUTPUT queue has buffers allocated.
350 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
351 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
352 if (vb2_is_busy(peer_vq) &&
353 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
354 pix_mp->height != ctx->dst_fmt.height ||
355 pix_mp->width != ctx->dst_fmt.width))
358 ret = vidioc_try_fmt_cap_mplane(file, priv, f);
362 ctx->vpu_dst_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
363 ctx->dst_fmt = *pix_mp;
365 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
366 vpu_debug(0, "fmt - w: %d, h: %d\n",
367 pix_mp->width, pix_mp->height);
370 * Current raw format might have become invalid with newly
371 * selected codec, so reset it to default just to be safe and
372 * keep internal driver state sane. User is mandated to set
373 * the raw format again after we return, so we don't need
376 rockchip_vpu_enc_reset_src_fmt(vpu, ctx);
380 const struct v4l2_ioctl_ops rockchip_vpu_enc_ioctl_ops = {
381 .vidioc_querycap = vidioc_querycap,
382 .vidioc_enum_framesizes = vidioc_enum_framesizes,
384 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
385 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
386 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
387 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
388 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
389 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
390 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
391 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
393 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
394 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
395 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
396 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
397 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
398 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
399 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
401 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
402 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
404 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
405 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
409 rockchip_vpu_queue_setup(struct vb2_queue *vq,
410 unsigned int *num_buffers,
411 unsigned int *num_planes,
412 unsigned int sizes[],
413 struct device *alloc_devs[])
415 struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
416 struct v4l2_pix_format_mplane *pixfmt;
420 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
421 pixfmt = &ctx->dst_fmt;
423 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
424 pixfmt = &ctx->src_fmt;
427 vpu_err("invalid queue type: %d\n", vq->type);
432 if (*num_planes != pixfmt->num_planes)
434 for (i = 0; i < pixfmt->num_planes; ++i)
435 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
440 *num_planes = pixfmt->num_planes;
441 for (i = 0; i < pixfmt->num_planes; ++i)
442 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
446 static int rockchip_vpu_buf_prepare(struct vb2_buffer *vb)
448 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
449 struct vb2_queue *vq = vb->vb2_queue;
450 struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
451 struct v4l2_pix_format_mplane *pixfmt;
457 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
458 pixfmt = &ctx->dst_fmt;
460 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
461 pixfmt = &ctx->src_fmt;
463 if (vbuf->field == V4L2_FIELD_ANY)
464 vbuf->field = V4L2_FIELD_NONE;
465 if (vbuf->field != V4L2_FIELD_NONE) {
466 vpu_debug(4, "field %d not supported\n",
472 vpu_err("invalid queue type: %d\n", vq->type);
476 for (i = 0; i < pixfmt->num_planes; ++i) {
477 sz = pixfmt->plane_fmt[i].sizeimage;
478 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
479 i, vb2_plane_size(vb, i), sz);
480 if (vb2_plane_size(vb, i) < sz) {
481 vpu_err("plane %d is too small\n", i);
490 static void rockchip_vpu_buf_queue(struct vb2_buffer *vb)
492 struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
493 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
495 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
498 static int rockchip_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
500 struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
501 enum rockchip_vpu_codec_mode codec_mode;
504 if (V4L2_TYPE_IS_OUTPUT(q->type))
505 ctx->sequence_out = 0;
507 ctx->sequence_cap = 0;
509 /* Set codec_ops for the chosen destination format */
510 codec_mode = ctx->vpu_dst_fmt->codec_mode;
512 vpu_debug(4, "Codec mode = %d\n", codec_mode);
513 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
515 if (!V4L2_TYPE_IS_OUTPUT(q->type))
516 if (ctx->codec_ops && ctx->codec_ops->init)
517 ret = ctx->codec_ops->init(ctx);
521 static void rockchip_vpu_stop_streaming(struct vb2_queue *q)
523 struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
525 if (!V4L2_TYPE_IS_OUTPUT(q->type))
526 if (ctx->codec_ops && ctx->codec_ops->exit)
527 ctx->codec_ops->exit(ctx);
530 * The mem2mem framework calls v4l2_m2m_cancel_job before
531 * .stop_streaming, so there isn't any job running and
532 * it is safe to return all the buffers.
535 struct vb2_v4l2_buffer *vbuf;
537 if (V4L2_TYPE_IS_OUTPUT(q->type))
538 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
540 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
543 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
547 const struct vb2_ops rockchip_vpu_enc_queue_ops = {
548 .queue_setup = rockchip_vpu_queue_setup,
549 .buf_prepare = rockchip_vpu_buf_prepare,
550 .buf_queue = rockchip_vpu_buf_queue,
551 .start_streaming = rockchip_vpu_start_streaming,
552 .stop_streaming = rockchip_vpu_stop_streaming,
553 .wait_prepare = vb2_ops_wait_prepare,
554 .wait_finish = vb2_ops_wait_finish,