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