CHROMIUM: [media] rockchip-vpu: rename rk3288-vpu to rockchip-vpu
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / rockchip-vpu / rockchip_vpu_dec.c
1 /*
2  * Rockchip VPU codec driver
3  *
4  * Copyright (C) 2014 Rockchip Electronics Co., Ltd.
5  *      Hertz Wong <hertz.wong@rock-chips.com>
6  *
7  * Copyright (C) 2014 Google, Inc.
8  *      Tomasz Figa <tfiga@chromium.org>
9  *
10  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
11  *
12  * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
13  *
14  * This software is licensed under the terms of the GNU General Public
15  * License version 2, as published by the Free Software Foundation, and
16  * may be copied, distributed, and modified under those terms.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  */
23
24 #include "rockchip_vpu_common.h"
25
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/videodev2.h>
29
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include <media/videobuf2-core.h>
33 #include <media/videobuf2-dma-sg.h>
34
35 #include "rockchip_vpu_dec.h"
36 #include "rockchip_vpu_hw.h"
37
38 #define DEF_SRC_FMT_DEC                         V4L2_PIX_FMT_H264_SLICE
39 #define DEF_DST_FMT_DEC                         V4L2_PIX_FMT_NV12
40
41 #define ROCKCHIP_DEC_MIN_WIDTH                  48U
42 #define ROCKCHIP_DEC_MAX_WIDTH                  3840U
43 #define ROCKCHIP_DEC_MIN_HEIGHT                 48U
44 #define ROCKCHIP_DEC_MAX_HEIGHT                 2160U
45
46 #define ROCKCHIP_H264_MAX_SLICES_PER_FRAME      16
47
48 static struct rockchip_vpu_fmt formats[] = {
49         {
50                 .name = "4:2:0 1 plane Y/CbCr",
51                 .fourcc = V4L2_PIX_FMT_NV12,
52                 .codec_mode = RK_VPU_CODEC_NONE,
53                 .num_planes = 1,
54                 .depth = { 12 },
55         },
56         {
57                 .name = "Slices of H264 Encoded Stream",
58                 .fourcc = V4L2_PIX_FMT_H264_SLICE,
59                 .codec_mode = RK3288_VPU_CODEC_H264D,
60                 .num_planes = 1,
61         },
62         {
63                 .name = "Frames of VP8 Encoded Stream",
64                 .fourcc = V4L2_PIX_FMT_VP8_FRAME,
65                 .codec_mode = RK3288_VPU_CODEC_VP8D,
66                 .num_planes = 1,
67         },
68 };
69
70 static struct rockchip_vpu_fmt *find_format(u32 fourcc, bool bitstream)
71 {
72         unsigned int i;
73
74         vpu_debug_enter();
75
76         for (i = 0; i < ARRAY_SIZE(formats); i++) {
77                 if (formats[i].fourcc == fourcc &&
78                     !!bitstream == (formats[i].codec_mode != RK_VPU_CODEC_NONE))
79                         return &formats[i];
80         }
81
82         return NULL;
83 }
84
85 /* Indices of controls that need to be accessed directly. */
86 enum {
87         ROCKCHIP_VPU_DEC_CTRL_H264_SPS,
88         ROCKCHIP_VPU_DEC_CTRL_H264_PPS,
89         ROCKCHIP_VPU_DEC_CTRL_H264_SCALING_MATRIX,
90         ROCKCHIP_VPU_DEC_CTRL_H264_SLICE_PARAM,
91         ROCKCHIP_VPU_DEC_CTRL_H264_DECODE_PARAM,
92         ROCKCHIP_VPU_DEC_CTRL_VP8_FRAME_HDR,
93 };
94
95 static struct rockchip_vpu_control controls[] = {
96         /* H264 slice-based interface. */
97         [ROCKCHIP_VPU_DEC_CTRL_H264_SPS] = {
98                 .id = V4L2_CID_MPEG_VIDEO_H264_SPS,
99                 .type = V4L2_CTRL_TYPE_PRIVATE,
100                 .name = "H264 SPS Parameters",
101                 .elem_size = sizeof(struct v4l2_ctrl_h264_sps),
102                 .max_stores = VIDEO_MAX_FRAME,
103                 .can_store = true,
104         },
105         [ROCKCHIP_VPU_DEC_CTRL_H264_PPS] = {
106                 .id = V4L2_CID_MPEG_VIDEO_H264_PPS,
107                 .type = V4L2_CTRL_TYPE_PRIVATE,
108                 .name = "H264 PPS Parameters",
109                 .elem_size = sizeof(struct v4l2_ctrl_h264_pps),
110                 .max_stores = VIDEO_MAX_FRAME,
111                 .can_store = true,
112         },
113         [ROCKCHIP_VPU_DEC_CTRL_H264_SCALING_MATRIX] = {
114                 .id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
115                 .type = V4L2_CTRL_TYPE_PRIVATE,
116                 .name = "H264 Scaling Matrix",
117                 .elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix),
118                 .max_stores = VIDEO_MAX_FRAME,
119                 .can_store = true,
120         },
121         [ROCKCHIP_VPU_DEC_CTRL_H264_SLICE_PARAM] = {
122                 .id = V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM,
123                 .type = V4L2_CTRL_TYPE_PRIVATE,
124                 .name = "H264 Slice Parameters",
125                 .max_stores = VIDEO_MAX_FRAME,
126                 .elem_size = sizeof(struct v4l2_ctrl_h264_slice_param),
127                 .dims = { ROCKCHIP_H264_MAX_SLICES_PER_FRAME, },
128                 .can_store = true,
129         },
130         [ROCKCHIP_VPU_DEC_CTRL_H264_DECODE_PARAM] = {
131                 .id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM,
132                 .type = V4L2_CTRL_TYPE_PRIVATE,
133                 .name = "H264 Decode Parameters",
134                 .max_stores = VIDEO_MAX_FRAME,
135                 .elem_size = sizeof(struct v4l2_ctrl_h264_decode_param),
136                 .can_store = true,
137         },
138         [ROCKCHIP_VPU_DEC_CTRL_VP8_FRAME_HDR] = {
139                 .id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR,
140                 .type = V4L2_CTRL_TYPE_PRIVATE,
141                 .name = "VP8 Frame Header Parameters",
142                 .max_stores = VIDEO_MAX_FRAME,
143                 .elem_size = sizeof(struct v4l2_ctrl_vp8_frame_hdr),
144                 .can_store = true,
145         },
146 };
147
148 static inline const void *get_ctrl_ptr(struct rockchip_vpu_ctx *ctx, unsigned id)
149 {
150         struct v4l2_ctrl *ctrl = ctx->ctrls[id];
151
152         return ctrl->p_cur.p;
153 }
154
155 /* Query capabilities of the device */
156 static int vidioc_querycap(struct file *file, void *priv,
157                            struct v4l2_capability *cap)
158 {
159         struct rockchip_vpu_dev *dev = video_drvdata(file);
160
161         vpu_debug_enter();
162
163         strlcpy(cap->driver, ROCKCHIP_VPU_DEC_NAME, sizeof(cap->driver));
164         strlcpy(cap->card, dev->pdev->name, sizeof(cap->card));
165         strlcpy(cap->bus_info, "platform:" ROCKCHIP_VPU_NAME,
166                 sizeof(cap->bus_info));
167
168         /*
169          * This is only a mem-to-mem video device. The capture and output
170          * device capability flags are left only for backward compatibility
171          * and are scheduled for removal.
172          */
173         cap->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
174             V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
175         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
176
177         vpu_debug_leave();
178
179         return 0;
180 }
181
182 static int vidioc_enum_framesizes(struct file *file, void *prov,
183                                   struct v4l2_frmsizeenum *fsize)
184 {
185         struct v4l2_frmsize_stepwise *s = &fsize->stepwise;
186         struct rockchip_vpu_fmt *fmt;
187
188         if (fsize->index != 0) {
189                 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
190                                 fsize->index);
191                 return -EINVAL;
192         }
193
194         fmt = find_format(fsize->pixel_format, true);
195         if (!fmt) {
196                 vpu_debug(0, "unsupported bitstream format (%08x)\n",
197                                 fsize->pixel_format);
198                 return -EINVAL;
199         }
200
201         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
202
203         s->min_width = ROCKCHIP_DEC_MIN_WIDTH;
204         s->max_width = ROCKCHIP_DEC_MAX_WIDTH;
205         s->step_width = MB_DIM;
206         s->min_height = ROCKCHIP_DEC_MIN_HEIGHT;
207         s->max_height = ROCKCHIP_DEC_MAX_HEIGHT;
208         s->step_height = MB_DIM;
209
210         return 0;
211 }
212
213 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool out)
214 {
215         struct rockchip_vpu_fmt *fmt;
216         int i, j = 0;
217
218         vpu_debug_enter();
219
220         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
221                 if (out && formats[i].codec_mode == RK_VPU_CODEC_NONE)
222                         continue;
223                 else if (!out && (formats[i].codec_mode != RK_VPU_CODEC_NONE))
224                         continue;
225
226                 if (j == f->index) {
227                         fmt = &formats[i];
228                         strlcpy(f->description, fmt->name,
229                                 sizeof(f->description));
230                         f->pixelformat = fmt->fourcc;
231
232                         f->flags = 0;
233                         if (formats[i].codec_mode != RK_VPU_CODEC_NONE)
234                                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
235
236                         vpu_debug_leave();
237
238                         return 0;
239                 }
240
241                 ++j;
242         }
243
244         vpu_debug_leave();
245
246         return -EINVAL;
247 }
248
249 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
250                                           struct v4l2_fmtdesc *f)
251 {
252         return vidioc_enum_fmt(f, false);
253 }
254
255 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
256                                           struct v4l2_fmtdesc *f)
257 {
258         return vidioc_enum_fmt(f, true);
259 }
260
261 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
262 {
263         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
264
265         vpu_debug_enter();
266
267         vpu_debug(4, "f->type = %d\n", f->type);
268
269         switch (f->type) {
270         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
271                 f->fmt.pix_mp = ctx->dst_fmt;
272                 break;
273
274         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
275                 f->fmt.pix_mp = ctx->src_fmt;
276                 break;
277
278         default:
279                 vpu_err("invalid buf type\n");
280                 return -EINVAL;
281         }
282
283         vpu_debug_leave();
284
285         return 0;
286 }
287
288 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
289 {
290         struct rockchip_vpu_fmt *fmt;
291         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
292         char str[5];
293
294         vpu_debug_enter();
295
296         switch (f->type) {
297         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
298                 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
299
300                 fmt = find_format(pix_fmt_mp->pixelformat, true);
301                 if (!fmt) {
302                         vpu_err("failed to try output format\n");
303                         return -EINVAL;
304                 }
305
306                 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
307                         vpu_err("sizeimage of output format must be given\n");
308                         return -EINVAL;
309                 }
310
311                 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
312                 break;
313
314         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
315                 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
316
317                 fmt = find_format(pix_fmt_mp->pixelformat, false);
318                 if (!fmt) {
319                         vpu_err("failed to try capture format\n");
320                         return -EINVAL;
321                 }
322
323                 if (fmt->num_planes != pix_fmt_mp->num_planes) {
324                         vpu_err("plane number mismatches on capture format\n");
325                         return -EINVAL;
326                 }
327
328                 /* Limit to hardware min/max. */
329                 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
330                                 ROCKCHIP_DEC_MIN_WIDTH, ROCKCHIP_DEC_MAX_WIDTH);
331                 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
332                                 ROCKCHIP_DEC_MIN_HEIGHT, ROCKCHIP_DEC_MAX_HEIGHT);
333
334                 /* Round up to macroblocks. */
335                 pix_fmt_mp->width = round_up(pix_fmt_mp->width, MB_DIM);
336                 pix_fmt_mp->height = round_up(pix_fmt_mp->height, MB_DIM);
337                 break;
338
339         default:
340                 vpu_err("invalid buf type\n");
341                 return -EINVAL;
342         }
343
344         vpu_debug_leave();
345
346         return 0;
347 }
348
349 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
350 {
351         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
352         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
353         unsigned int mb_width, mb_height;
354         struct rockchip_vpu_fmt *fmt;
355         int ret = 0;
356         int i;
357
358         vpu_debug_enter();
359
360         switch (f->type) {
361         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
362                 /* Change not allowed if any queue is streaming. */
363                 if (vb2_is_streaming(&ctx->vq_src)
364                     || vb2_is_streaming(&ctx->vq_dst)) {
365                         ret = -EBUSY;
366                         goto out;
367                 }
368                 /*
369                  * Pixel format change is not allowed when the other queue has
370                  * buffers allocated.
371                  */
372                 if (vb2_is_busy(&ctx->vq_dst)
373                     && pix_fmt_mp->pixelformat != ctx->src_fmt.pixelformat) {
374                         ret = -EBUSY;
375                         goto out;
376                 }
377
378                 ret = vidioc_try_fmt(file, priv, f);
379                 if (ret)
380                         goto out;
381
382                 ctx->vpu_src_fmt = find_format(pix_fmt_mp->pixelformat, true);
383                 ctx->src_fmt = *pix_fmt_mp;
384                 break;
385
386         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
387                 /*
388                  * Change not allowed if this queue is streaming.
389                  *
390                  * NOTE: We allow changes with source queue streaming
391                  * to support resolution change in decoded stream.
392                  */
393                 if (vb2_is_streaming(&ctx->vq_dst)) {
394                         ret = -EBUSY;
395                         goto out;
396                 }
397                 /*
398                  * Pixel format change is not allowed when the other queue has
399                  * buffers allocated.
400                  */
401                 if (vb2_is_busy(&ctx->vq_src)
402                     && pix_fmt_mp->pixelformat != ctx->dst_fmt.pixelformat) {
403                         ret = -EBUSY;
404                         goto out;
405                 }
406
407                 ret = vidioc_try_fmt(file, priv, f);
408                 if (ret)
409                         goto out;
410
411                 fmt = find_format(pix_fmt_mp->pixelformat, false);
412                 ctx->vpu_dst_fmt = fmt;
413
414                 mb_width = MB_WIDTH(pix_fmt_mp->width);
415                 mb_height = MB_HEIGHT(pix_fmt_mp->height);
416
417                 vpu_debug(0, "CAPTURE codec mode: %d\n", fmt->codec_mode);
418                 vpu_debug(0, "fmt - w: %d, h: %d, mb - w: %d, h: %d\n",
419                           pix_fmt_mp->width, pix_fmt_mp->height,
420                           mb_width, mb_height);
421
422                 for (i = 0; i < fmt->num_planes; ++i) {
423                         pix_fmt_mp->plane_fmt[i].bytesperline =
424                                 mb_width * MB_DIM * fmt->depth[i] / 8;
425                         pix_fmt_mp->plane_fmt[i].sizeimage =
426                                 pix_fmt_mp->plane_fmt[i].bytesperline
427                                 * mb_height * MB_DIM;
428                         /*
429                          * All of multiplanar formats we support have chroma
430                          * planes subsampled by 2.
431                          */
432                         if (i != 0)
433                                 pix_fmt_mp->plane_fmt[i].sizeimage /= 2;
434                 }
435
436                 ctx->dst_fmt = *pix_fmt_mp;
437                 break;
438
439         default:
440                 vpu_err("invalid buf type\n");
441                 return -EINVAL;
442         }
443
444 out:
445         vpu_debug_leave();
446
447         return ret;
448 }
449
450 static int vidioc_reqbufs(struct file *file, void *priv,
451                           struct v4l2_requestbuffers *reqbufs)
452 {
453         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
454         int ret;
455
456         vpu_debug_enter();
457
458         switch (reqbufs->type) {
459         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
460                 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
461                 if (ret != 0) {
462                         vpu_err("error in vb2_reqbufs() for E(S)\n");
463                         goto out;
464                 }
465                 break;
466
467         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
468                 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
469                 if (ret != 0) {
470                         vpu_err("error in vb2_reqbufs() for E(D)\n");
471                         goto out;
472                 }
473                 break;
474
475         default:
476                 vpu_err("invalid buf type\n");
477                 ret = -EINVAL;
478                 goto out;
479         }
480
481 out:
482         vpu_debug_leave();
483
484         return ret;
485 }
486
487 static int vidioc_querybuf(struct file *file, void *priv,
488                            struct v4l2_buffer *buf)
489 {
490         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
491         int ret;
492
493         vpu_debug_enter();
494
495         switch (buf->type) {
496         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
497                 ret = vb2_querybuf(&ctx->vq_dst, buf);
498                 if (ret != 0) {
499                         vpu_err("error in vb2_querybuf() for E(D)\n");
500                         goto out;
501                 }
502
503                 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
504                 break;
505
506         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
507                 ret = vb2_querybuf(&ctx->vq_src, buf);
508                 if (ret != 0) {
509                         vpu_err("error in vb2_querybuf() for E(S)\n");
510                         goto out;
511                 }
512                 break;
513
514         default:
515                 vpu_err("invalid buf type\n");
516                 ret = -EINVAL;
517                 goto out;
518         }
519
520 out:
521         vpu_debug_leave();
522
523         return ret;
524 }
525
526 /* Queue a buffer */
527 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
528 {
529         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
530         int ret;
531         int i;
532
533         vpu_debug_enter();
534
535         for (i = 0; i < buf->length; i++)
536                 vpu_debug(4, "plane[%d]->length %d bytesused %d\n",
537                                 i, buf->m.planes[i].length,
538                                 buf->m.planes[i].bytesused);
539
540         switch (buf->type) {
541         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
542                 ret = vb2_qbuf(&ctx->vq_src, buf);
543                 vpu_debug(4, "OUTPUT_MPLANE : vb2_qbuf return %d\n", ret);
544                 break;
545
546         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
547                 ret = vb2_qbuf(&ctx->vq_dst, buf);
548                 vpu_debug(4, "CAPTURE_MPLANE: vb2_qbuf return %d\n", ret);
549                 break;
550
551         default:
552                 ret = -EINVAL;
553         }
554
555         vpu_debug_leave();
556
557         return ret;
558 }
559
560 /* Dequeue a buffer */
561 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
562 {
563         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
564         int ret;
565
566         vpu_debug_enter();
567
568         switch (buf->type) {
569         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
570                 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
571                 break;
572
573         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
574                 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
575                 break;
576
577         default:
578                 ret = -EINVAL;
579         }
580
581         vpu_debug_leave();
582
583         return ret;
584 }
585
586 /* Export DMA buffer */
587 static int vidioc_expbuf(struct file *file, void *priv,
588                          struct v4l2_exportbuffer *eb)
589 {
590         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
591         int ret;
592
593         vpu_debug_enter();
594
595         switch (eb->type) {
596         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
597                 ret = vb2_expbuf(&ctx->vq_src, eb);
598                 break;
599
600         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
601                 ret = vb2_expbuf(&ctx->vq_dst, eb);
602                 break;
603
604         default:
605                 ret = -EINVAL;
606         }
607
608         vpu_debug_leave();
609
610         return ret;
611 }
612
613 /* Stream on */
614 static int vidioc_streamon(struct file *file, void *priv,
615                            enum v4l2_buf_type type)
616 {
617         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
618         int ret;
619
620         vpu_debug_enter();
621
622         switch (type) {
623         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
624                 ret = vb2_streamon(&ctx->vq_src, type);
625                 break;
626
627         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
628                 ret = vb2_streamon(&ctx->vq_dst, type);
629                 break;
630
631         default:
632                 ret = -EINVAL;
633         }
634
635         vpu_debug_leave();
636
637         return ret;
638 }
639
640 /* Stream off, which equals to a pause */
641 static int vidioc_streamoff(struct file *file, void *priv,
642                             enum v4l2_buf_type type)
643 {
644         struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
645         int ret;
646
647         vpu_debug_enter();
648
649         switch (type) {
650         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
651                 ret = vb2_streamoff(&ctx->vq_src, type);
652                 break;
653
654         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
655                 ret = vb2_streamoff(&ctx->vq_dst, type);
656                 break;
657
658         default:
659                 ret = -EINVAL;
660         }
661
662         vpu_debug_leave();
663
664         return ret;
665 }
666
667 static void rockchip_vpu_dec_set_dpb(struct rockchip_vpu_ctx *ctx,
668                                             struct v4l2_ctrl *ctrl)
669 {
670         struct v4l2_ctrl_h264_decode_param *dec_param = ctrl->p_new.p;
671         const struct v4l2_h264_dpb_entry *new_dpb_entry;
672         u8 *dpb_map = ctx->run.h264d.dpb_map;
673         struct v4l2_h264_dpb_entry *cur_dpb_entry;
674         DECLARE_BITMAP(used, ARRAY_SIZE(ctx->run.h264d.dpb)) = { 0, };
675         DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
676         int i, j;
677
678         BUILD_BUG_ON(ARRAY_SIZE(ctx->run.h264d.dpb) !=
679                                                 ARRAY_SIZE(dec_param->dpb));
680
681         /* Disable all entries by default. */
682         for (j = 0; j < ARRAY_SIZE(ctx->run.h264d.dpb); ++j) {
683                 cur_dpb_entry = &ctx->run.h264d.dpb[j];
684
685                 cur_dpb_entry->flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
686         }
687
688         /* Try to match new DPB entries with existing ones by their POCs. */
689         for (i = 0; i < ARRAY_SIZE(dec_param->dpb); ++i) {
690                 new_dpb_entry = &dec_param->dpb[i];
691
692                 if (!(new_dpb_entry->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
693                         continue;
694
695                 /*
696                  * To cut off some comparisons, iterate only on target DPB
697                  * entries which are not used yet.
698                  */
699                 for_each_clear_bit(j, used, ARRAY_SIZE(ctx->run.h264d.dpb)) {
700                         cur_dpb_entry = &ctx->run.h264d.dpb[j];
701
702                         if (new_dpb_entry->top_field_order_cnt ==
703                                         cur_dpb_entry->top_field_order_cnt &&
704                             new_dpb_entry->bottom_field_order_cnt ==
705                                         cur_dpb_entry->bottom_field_order_cnt) {
706                                 memcpy(cur_dpb_entry, new_dpb_entry,
707                                         sizeof(*cur_dpb_entry));
708                                 set_bit(j, used);
709                                 dpb_map[i] = j;
710                                 break;
711                         }
712                 }
713
714                 if (j == ARRAY_SIZE(ctx->run.h264d.dpb))
715                         set_bit(i, new);
716         }
717
718         /* For entries that could not be matched, use remaining free slots. */
719         for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
720                 new_dpb_entry = &dec_param->dpb[i];
721
722                 j = find_first_zero_bit(used, ARRAY_SIZE(ctx->run.h264d.dpb));
723                 /*
724                  * Both arrays are of the same sizes, so there is no way
725                  * we can end up with no space in target array, unless
726                  * something is buggy.
727                  */
728                 if (WARN_ON(j >= ARRAY_SIZE(ctx->run.h264d.dpb)))
729                         return;
730
731                 cur_dpb_entry = &ctx->run.h264d.dpb[j];
732                 memcpy(cur_dpb_entry, new_dpb_entry, sizeof(*cur_dpb_entry));
733                 set_bit(j, used);
734                 dpb_map[i] = j;
735         }
736
737         /*
738          * Verify that reference picture lists are in range, since they
739          * will be indexing dpb_map[] when programming the hardware.
740          *
741          * Fallback to 0 should be safe, as we will get at most corrupt
742          * decoding result, without any serious side effects. Moreover,
743          * even if entry 0 is unused, the hardware programming code will
744          * handle this properly.
745          */
746         for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b0); ++i)
747                 if (dec_param->ref_pic_list_b0[i]
748                     >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
749                         dec_param->ref_pic_list_b0[i] = 0;
750         for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b1); ++i)
751                 if (dec_param->ref_pic_list_b1[i]
752                     >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
753                         dec_param->ref_pic_list_b1[i] = 0;
754         for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_p0); ++i)
755                 if (dec_param->ref_pic_list_p0[i]
756                     >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
757                         dec_param->ref_pic_list_p0[i] = 0;
758 }
759
760 static int rockchip_vpu_dec_s_ctrl(struct v4l2_ctrl *ctrl)
761 {
762         struct rockchip_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
763         struct rockchip_vpu_dev *dev = ctx->dev;
764         int ret = 0;
765
766         vpu_debug_enter();
767
768         vpu_debug(4, "ctrl id %d\n", ctrl->id);
769
770         switch (ctrl->id) {
771         case V4L2_CID_MPEG_VIDEO_H264_SPS:
772         case V4L2_CID_MPEG_VIDEO_H264_PPS:
773         case V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX:
774         case V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM:
775         case V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR:
776                 /* These controls are used directly. */
777                 break;
778
779         case V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM:
780                 if (ctrl->store)
781                         break;
782                 rockchip_vpu_dec_set_dpb(ctx, ctrl);
783                 break;
784
785         default:
786                 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
787                          ctrl->id, ctrl->val);
788                 ret = -EINVAL;
789         }
790
791         vpu_debug_leave();
792
793         return ret;
794 }
795
796 static const struct v4l2_ctrl_ops rockchip_vpu_dec_ctrl_ops = {
797         .s_ctrl = rockchip_vpu_dec_s_ctrl,
798 };
799
800 static const struct v4l2_ioctl_ops rockchip_vpu_dec_ioctl_ops = {
801         .vidioc_querycap = vidioc_querycap,
802         .vidioc_enum_framesizes = vidioc_enum_framesizes,
803         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
804         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
805         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
806         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
807         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
808         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
809         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
810         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
811         .vidioc_reqbufs = vidioc_reqbufs,
812         .vidioc_querybuf = vidioc_querybuf,
813         .vidioc_qbuf = vidioc_qbuf,
814         .vidioc_dqbuf = vidioc_dqbuf,
815         .vidioc_expbuf = vidioc_expbuf,
816         .vidioc_streamon = vidioc_streamon,
817         .vidioc_streamoff = vidioc_streamoff,
818 };
819
820 static int rockchip_vpu_queue_setup(struct vb2_queue *vq,
821                                   const void *parg,
822                                   unsigned int *buf_count,
823                                   unsigned int *plane_count,
824                                   unsigned int psize[], void *allocators[])
825 {
826         struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
827         int ret = 0;
828
829         vpu_debug_enter();
830
831         switch (vq->type) {
832         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
833                 *plane_count = ctx->vpu_src_fmt->num_planes;
834
835                 if (*buf_count < 1)
836                         *buf_count = 1;
837
838                 if (*buf_count > VIDEO_MAX_FRAME)
839                         *buf_count = VIDEO_MAX_FRAME;
840
841                 psize[0] = ctx->src_fmt.plane_fmt[0].sizeimage;
842                 allocators[0] = ctx->dev->alloc_ctx;
843                 vpu_debug(0, "output psize[%d]: %d\n", 0, psize[0]);
844                 break;
845
846         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
847                 *plane_count = ctx->vpu_dst_fmt->num_planes;
848
849                 if (*buf_count < 1)
850                         *buf_count = 1;
851
852                 if (*buf_count > VIDEO_MAX_FRAME)
853                         *buf_count = VIDEO_MAX_FRAME;
854
855                 psize[0] = round_up(ctx->dst_fmt.plane_fmt[0].sizeimage, 8);
856                 allocators[0] = ctx->dev->alloc_ctx;
857
858                 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE)
859                         /* Add space for appended motion vectors. */
860                         psize[0] += 64 * MB_WIDTH(ctx->dst_fmt.width)
861                                         * MB_HEIGHT(ctx->dst_fmt.height);
862
863                 vpu_debug(0, "capture psize[%d]: %d\n", 0, psize[0]);
864                 break;
865
866         default:
867                 vpu_err("invalid queue type: %d\n", vq->type);
868                 ret = -EINVAL;
869         }
870
871         vpu_debug_leave();
872
873         return ret;
874 }
875
876 static int rockchip_vpu_buf_init(struct vb2_buffer *vb)
877 {
878         struct vb2_queue *vq = vb->vb2_queue;
879         struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
880
881         vpu_debug_enter();
882
883         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
884                 ctx->dst_bufs[vb->index] = vb;
885
886         vpu_debug_leave();
887
888         return 0;
889 }
890
891 static void rockchip_vpu_buf_cleanup(struct vb2_buffer *vb)
892 {
893         struct vb2_queue *vq = vb->vb2_queue;
894         struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
895
896         vpu_debug_enter();
897
898         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
899                 ctx->dst_bufs[vb->index] = NULL;
900
901         vpu_debug_leave();
902 }
903
904 static int rockchip_vpu_buf_prepare(struct vb2_buffer *vb)
905 {
906         struct vb2_queue *vq = vb->vb2_queue;
907         struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
908         int ret = 0;
909         int i;
910
911         vpu_debug_enter();
912
913         switch (vq->type) {
914         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
915                 vpu_debug(4, "plane size: %ld, dst size: %d\n",
916                                 vb2_plane_size(vb, 0),
917                                 ctx->src_fmt.plane_fmt[0].sizeimage);
918
919                 if (vb2_plane_size(vb, 0)
920                     < ctx->src_fmt.plane_fmt[0].sizeimage) {
921                         vpu_err("plane size is too small for output\n");
922                         ret = -EINVAL;
923                 }
924                 break;
925
926         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
927                 for (i = 0; i < ctx->vpu_dst_fmt->num_planes; ++i) {
928                         vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", i,
929                                         vb2_plane_size(vb, i),
930                                         ctx->dst_fmt.plane_fmt[i].sizeimage);
931
932                         if (vb2_plane_size(vb, i)
933                             < ctx->dst_fmt.plane_fmt[i].sizeimage) {
934                                 vpu_err("size of plane %d is too small for capture\n",
935                                         i);
936                                 break;
937                         }
938                 }
939
940                 if (i != ctx->vpu_dst_fmt->num_planes)
941                         ret = -EINVAL;
942                 break;
943
944         default:
945                 vpu_err("invalid queue type: %d\n", vq->type);
946                 ret = -EINVAL;
947         }
948
949         vpu_debug_leave();
950
951         return ret;
952 }
953
954 static int rockchip_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
955 {
956         int ret = 0;
957         struct rockchip_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
958         struct rockchip_vpu_dev *dev = ctx->dev;
959         bool ready = false;
960
961         vpu_debug_enter();
962
963         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
964                 ret = rockchip_vpu_init(ctx);
965                 if (ret < 0) {
966                         vpu_err("rockchip_vpu_init failed\n");
967                         return ret;
968                 }
969
970                 ready = vb2_is_streaming(&ctx->vq_src);
971         } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
972                 ready = vb2_is_streaming(&ctx->vq_dst);
973         }
974
975         if (ready)
976                 rockchip_vpu_try_context(dev, ctx);
977
978         vpu_debug_leave();
979
980         return 0;
981 }
982
983 static void rockchip_vpu_stop_streaming(struct vb2_queue *q)
984 {
985         unsigned long flags;
986         struct rockchip_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
987         struct rockchip_vpu_dev *dev = ctx->dev;
988         struct rockchip_vpu_buf *b;
989         LIST_HEAD(queue);
990         int i;
991
992         vpu_debug_enter();
993
994         spin_lock_irqsave(&dev->irqlock, flags);
995
996         list_del_init(&ctx->list);
997
998         switch (q->type) {
999         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1000                 list_splice_init(&ctx->dst_queue, &queue);
1001                 break;
1002
1003         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1004                 list_splice_init(&ctx->src_queue, &queue);
1005                 break;
1006
1007         default:
1008                 break;
1009         }
1010
1011         spin_unlock_irqrestore(&dev->irqlock, flags);
1012
1013         wait_event(dev->run_wq, dev->current_ctx != ctx);
1014
1015         while (!list_empty(&queue)) {
1016                 b = list_first_entry(&queue, struct rockchip_vpu_buf, list);
1017                 for (i = 0; i < b->vb.vb2_buf.num_planes; i++)
1018                         vb2_set_plane_payload(&b->vb.vb2_buf, i, 0);
1019                 vb2_buffer_done(&b->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1020                 list_del(&b->list);
1021         }
1022
1023         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1024                 rockchip_vpu_deinit(ctx);
1025
1026         vpu_debug_leave();
1027 }
1028
1029 static void rockchip_vpu_buf_queue(struct vb2_buffer *vb)
1030 {
1031         struct vb2_queue *vq = vb->vb2_queue;
1032         struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1033         struct rockchip_vpu_dev *dev = ctx->dev;
1034         struct rockchip_vpu_buf *vpu_buf;
1035         unsigned long flags;
1036
1037         vpu_debug_enter();
1038
1039         switch (vq->type) {
1040         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1041                 vpu_buf = vb_to_buf(vb);
1042
1043                 /* Mark destination as available for use by VPU */
1044                 spin_lock_irqsave(&dev->irqlock, flags);
1045
1046                 list_add_tail(&vpu_buf->list, &ctx->dst_queue);
1047
1048                 spin_unlock_irqrestore(&dev->irqlock, flags);
1049                 break;
1050
1051         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1052                 vpu_buf = vb_to_buf(vb);
1053
1054                 spin_lock_irqsave(&dev->irqlock, flags);
1055
1056                 list_add_tail(&vpu_buf->list, &ctx->src_queue);
1057
1058                 spin_unlock_irqrestore(&dev->irqlock, flags);
1059                 break;
1060
1061         default:
1062                 vpu_err("unsupported buffer type (%d)\n", vq->type);
1063         }
1064
1065         if (vb2_is_streaming(&ctx->vq_src) && vb2_is_streaming(&ctx->vq_dst))
1066                 rockchip_vpu_try_context(dev, ctx);
1067
1068         vpu_debug_leave();
1069 }
1070
1071 static struct vb2_ops rockchip_vpu_dec_qops = {
1072         .queue_setup = rockchip_vpu_queue_setup,
1073         .wait_prepare = vb2_ops_wait_prepare,
1074         .wait_finish = vb2_ops_wait_finish,
1075         .buf_init = rockchip_vpu_buf_init,
1076         .buf_prepare = rockchip_vpu_buf_prepare,
1077         .buf_cleanup = rockchip_vpu_buf_cleanup,
1078         .start_streaming = rockchip_vpu_start_streaming,
1079         .stop_streaming = rockchip_vpu_stop_streaming,
1080         .buf_queue = rockchip_vpu_buf_queue,
1081 };
1082
1083 struct vb2_ops *get_dec_queue_ops(void)
1084 {
1085         return &rockchip_vpu_dec_qops;
1086 }
1087
1088 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1089 {
1090         return &rockchip_vpu_dec_ioctl_ops;
1091 }
1092
1093 static void rockchip_vpu_dec_prepare_run(struct rockchip_vpu_ctx *ctx)
1094 {
1095         struct vb2_v4l2_buffer *src = to_vb2_v4l2_buffer(&ctx->run.src->vb.vb2_buf);
1096
1097         v4l2_ctrl_apply_store(&ctx->ctrl_handler, src->config_store);
1098
1099         if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE) {
1100                 ctx->run.h264d.sps = get_ctrl_ptr(ctx,
1101                                                                                   ROCKCHIP_VPU_DEC_CTRL_H264_SPS);
1102                 ctx->run.h264d.pps = get_ctrl_ptr(ctx,
1103                                                                                   ROCKCHIP_VPU_DEC_CTRL_H264_PPS);
1104                 ctx->run.h264d.scaling_matrix = get_ctrl_ptr(ctx,
1105                                                                                 ROCKCHIP_VPU_DEC_CTRL_H264_SCALING_MATRIX);
1106                 ctx->run.h264d.slice_param = get_ctrl_ptr(ctx,
1107                                                                          ROCKCHIP_VPU_DEC_CTRL_H264_SLICE_PARAM);
1108                 ctx->run.h264d.decode_param = get_ctrl_ptr(ctx,
1109                                                                           ROCKCHIP_VPU_DEC_CTRL_H264_DECODE_PARAM);
1110         } else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP8_FRAME) {
1111                 ctx->run.vp8d.frame_hdr = get_ctrl_ptr(ctx,
1112                                                                                            ROCKCHIP_VPU_DEC_CTRL_VP8_FRAME_HDR);
1113         }
1114 }
1115
1116 static void rockchip_vpu_dec_run_done(struct rockchip_vpu_ctx *ctx,
1117                                     enum vb2_buffer_state result)
1118 {
1119         struct v4l2_plane_pix_format *plane_fmts = ctx->dst_fmt.plane_fmt;
1120         struct vb2_buffer *dst = &ctx->run.dst->vb.vb2_buf;
1121         int i;
1122
1123         if (result != VB2_BUF_STATE_DONE) {
1124                 /* Assume no payload after failed run. */
1125                 for (i = 0; i < dst->num_planes; ++i)
1126                         vb2_set_plane_payload(dst, i, 0);
1127                 return;
1128         }
1129
1130         for (i = 0; i < dst->num_planes; ++i)
1131                 vb2_set_plane_payload(dst, i, plane_fmts[i].sizeimage);
1132 }
1133
1134 static const struct rockchip_vpu_run_ops rockchip_vpu_dec_run_ops = {
1135         .prepare_run = rockchip_vpu_dec_prepare_run,
1136         .run_done = rockchip_vpu_dec_run_done,
1137 };
1138
1139 int rockchip_vpu_dec_init(struct rockchip_vpu_ctx *ctx)
1140 {
1141         ctx->vpu_src_fmt = find_format(DEF_SRC_FMT_DEC, false);
1142         ctx->vpu_dst_fmt = find_format(DEF_DST_FMT_DEC, true);
1143
1144         ctx->run_ops = &rockchip_vpu_dec_run_ops;
1145
1146         return rockchip_vpu_ctrls_setup(ctx, &rockchip_vpu_dec_ctrl_ops,
1147                                         controls, ARRAY_SIZE(controls), NULL);
1148 }
1149
1150 void rockchip_vpu_dec_exit(struct rockchip_vpu_ctx *ctx)
1151 {
1152         rockchip_vpu_ctrls_delete(ctx);
1153 }