GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / media / platform / qcom / venus / vdec.c
1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2017 Linaro Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-dma-sg.h>
25
26 #include "hfi_venus_io.h"
27 #include "core.h"
28 #include "helpers.h"
29 #include "vdec.h"
30
31 static u32 get_framesize_uncompressed(unsigned int plane, u32 width, u32 height)
32 {
33         u32 y_stride, uv_stride, y_plane;
34         u32 y_sclines, uv_sclines, uv_plane;
35         u32 size;
36
37         y_stride = ALIGN(width, 128);
38         uv_stride = ALIGN(width, 128);
39         y_sclines = ALIGN(height, 32);
40         uv_sclines = ALIGN(((height + 1) >> 1), 16);
41
42         y_plane = y_stride * y_sclines;
43         uv_plane = uv_stride * uv_sclines + SZ_4K;
44         size = y_plane + uv_plane + SZ_8K;
45
46         return ALIGN(size, SZ_4K);
47 }
48
49 static u32 get_framesize_compressed(unsigned int width, unsigned int height)
50 {
51         return ((width * height * 3 / 2) / 2) + 128;
52 }
53
54 /*
55  * Three resons to keep MPLANE formats (despite that the number of planes
56  * currently is one):
57  * - the MPLANE formats allow only one plane to be used
58  * - the downstream driver use MPLANE formats too
59  * - future firmware versions could add support for >1 planes
60  */
61 static const struct venus_format vdec_formats[] = {
62         {
63                 .pixfmt = V4L2_PIX_FMT_NV12,
64                 .num_planes = 1,
65                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
66         }, {
67                 .pixfmt = V4L2_PIX_FMT_MPEG4,
68                 .num_planes = 1,
69                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
70         }, {
71                 .pixfmt = V4L2_PIX_FMT_MPEG2,
72                 .num_planes = 1,
73                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
74         }, {
75                 .pixfmt = V4L2_PIX_FMT_H263,
76                 .num_planes = 1,
77                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
78         }, {
79                 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
80                 .num_planes = 1,
81                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
82         }, {
83                 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
84                 .num_planes = 1,
85                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
86         }, {
87                 .pixfmt = V4L2_PIX_FMT_H264,
88                 .num_planes = 1,
89                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
90         }, {
91                 .pixfmt = V4L2_PIX_FMT_VP8,
92                 .num_planes = 1,
93                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
94         }, {
95                 .pixfmt = V4L2_PIX_FMT_VP9,
96                 .num_planes = 1,
97                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
98         }, {
99                 .pixfmt = V4L2_PIX_FMT_XVID,
100                 .num_planes = 1,
101                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
102         },
103 };
104
105 static const struct venus_format *
106 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
107 {
108         const struct venus_format *fmt = vdec_formats;
109         unsigned int size = ARRAY_SIZE(vdec_formats);
110         unsigned int i;
111
112         for (i = 0; i < size; i++) {
113                 if (fmt[i].pixfmt == pixfmt)
114                         break;
115         }
116
117         if (i == size || fmt[i].type != type)
118                 return NULL;
119
120         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
121             !venus_helper_check_codec(inst, fmt[i].pixfmt))
122                 return NULL;
123
124         return &fmt[i];
125 }
126
127 static const struct venus_format *
128 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
129 {
130         const struct venus_format *fmt = vdec_formats;
131         unsigned int size = ARRAY_SIZE(vdec_formats);
132         unsigned int i, k = 0;
133
134         if (index > size)
135                 return NULL;
136
137         for (i = 0; i < size; i++) {
138                 if (fmt[i].type != type)
139                         continue;
140                 if (k == index)
141                         break;
142                 k++;
143         }
144
145         if (i == size)
146                 return NULL;
147
148         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
149             !venus_helper_check_codec(inst, fmt[i].pixfmt))
150                 return NULL;
151
152         return &fmt[i];
153 }
154
155 static const struct venus_format *
156 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
157 {
158         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
159         struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
160         const struct venus_format *fmt;
161         unsigned int p;
162
163         memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
164         memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
165
166         fmt = find_format(inst, pixmp->pixelformat, f->type);
167         if (!fmt) {
168                 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
169                         pixmp->pixelformat = V4L2_PIX_FMT_NV12;
170                 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
171                         pixmp->pixelformat = V4L2_PIX_FMT_H264;
172                 else
173                         return NULL;
174                 fmt = find_format(inst, pixmp->pixelformat, f->type);
175                 pixmp->width = 1280;
176                 pixmp->height = 720;
177         }
178
179         pixmp->width = clamp(pixmp->width, inst->cap_width.min,
180                              inst->cap_width.max);
181         pixmp->height = clamp(pixmp->height, inst->cap_height.min,
182                               inst->cap_height.max);
183
184         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
185                 pixmp->height = ALIGN(pixmp->height, 32);
186
187         if (pixmp->field == V4L2_FIELD_ANY)
188                 pixmp->field = V4L2_FIELD_NONE;
189         pixmp->num_planes = fmt->num_planes;
190         pixmp->flags = 0;
191
192         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
193                 for (p = 0; p < pixmp->num_planes; p++) {
194                         pfmt[p].sizeimage =
195                                 get_framesize_uncompressed(p, pixmp->width,
196                                                            pixmp->height);
197                         pfmt[p].bytesperline = ALIGN(pixmp->width, 128);
198                 }
199         } else {
200                 pfmt[0].sizeimage = get_framesize_compressed(pixmp->width,
201                                                              pixmp->height);
202                 pfmt[0].bytesperline = 0;
203         }
204
205         return fmt;
206 }
207
208 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
209 {
210         struct venus_inst *inst = to_inst(file);
211
212         vdec_try_fmt_common(inst, f);
213
214         return 0;
215 }
216
217 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
218 {
219         struct venus_inst *inst = to_inst(file);
220         const struct venus_format *fmt = NULL;
221         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
222
223         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
224                 fmt = inst->fmt_cap;
225         else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
226                 fmt = inst->fmt_out;
227
228         if (inst->reconfig) {
229                 struct v4l2_format format = {};
230
231                 inst->out_width = inst->reconfig_width;
232                 inst->out_height = inst->reconfig_height;
233                 inst->reconfig = false;
234
235                 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
236                 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
237                 format.fmt.pix_mp.width = inst->out_width;
238                 format.fmt.pix_mp.height = inst->out_height;
239
240                 vdec_try_fmt_common(inst, &format);
241
242                 inst->width = format.fmt.pix_mp.width;
243                 inst->height = format.fmt.pix_mp.height;
244         }
245
246         pixmp->pixelformat = fmt->pixfmt;
247
248         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
249                 pixmp->width = inst->width;
250                 pixmp->height = inst->height;
251                 pixmp->colorspace = inst->colorspace;
252                 pixmp->ycbcr_enc = inst->ycbcr_enc;
253                 pixmp->quantization = inst->quantization;
254                 pixmp->xfer_func = inst->xfer_func;
255         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
256                 pixmp->width = inst->out_width;
257                 pixmp->height = inst->out_height;
258         }
259
260         vdec_try_fmt_common(inst, f);
261
262         return 0;
263 }
264
265 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
266 {
267         struct venus_inst *inst = to_inst(file);
268         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
269         struct v4l2_pix_format_mplane orig_pixmp;
270         const struct venus_format *fmt;
271         struct v4l2_format format;
272         u32 pixfmt_out = 0, pixfmt_cap = 0;
273
274         orig_pixmp = *pixmp;
275
276         fmt = vdec_try_fmt_common(inst, f);
277
278         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
279                 pixfmt_out = pixmp->pixelformat;
280                 pixfmt_cap = inst->fmt_cap->pixfmt;
281         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
282                 pixfmt_cap = pixmp->pixelformat;
283                 pixfmt_out = inst->fmt_out->pixfmt;
284         }
285
286         memset(&format, 0, sizeof(format));
287
288         format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
289         format.fmt.pix_mp.pixelformat = pixfmt_out;
290         format.fmt.pix_mp.width = orig_pixmp.width;
291         format.fmt.pix_mp.height = orig_pixmp.height;
292         vdec_try_fmt_common(inst, &format);
293
294         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
295                 inst->out_width = format.fmt.pix_mp.width;
296                 inst->out_height = format.fmt.pix_mp.height;
297                 inst->colorspace = pixmp->colorspace;
298                 inst->ycbcr_enc = pixmp->ycbcr_enc;
299                 inst->quantization = pixmp->quantization;
300                 inst->xfer_func = pixmp->xfer_func;
301         }
302
303         memset(&format, 0, sizeof(format));
304
305         format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
306         format.fmt.pix_mp.pixelformat = pixfmt_cap;
307         format.fmt.pix_mp.width = orig_pixmp.width;
308         format.fmt.pix_mp.height = orig_pixmp.height;
309         vdec_try_fmt_common(inst, &format);
310
311         inst->width = format.fmt.pix_mp.width;
312         inst->height = format.fmt.pix_mp.height;
313
314         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
315                 inst->fmt_out = fmt;
316         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
317                 inst->fmt_cap = fmt;
318
319         return 0;
320 }
321
322 static int
323 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
324 {
325         struct venus_inst *inst = to_inst(file);
326
327         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
328             s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
329                 return -EINVAL;
330
331         switch (s->target) {
332         case V4L2_SEL_TGT_CROP_BOUNDS:
333         case V4L2_SEL_TGT_CROP_DEFAULT:
334         case V4L2_SEL_TGT_CROP:
335                 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
336                         return -EINVAL;
337                 s->r.width = inst->out_width;
338                 s->r.height = inst->out_height;
339                 break;
340         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
341         case V4L2_SEL_TGT_COMPOSE_PADDED:
342                 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
343                         return -EINVAL;
344                 s->r.width = inst->width;
345                 s->r.height = inst->height;
346                 break;
347         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
348         case V4L2_SEL_TGT_COMPOSE:
349                 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
350                         return -EINVAL;
351                 s->r.width = inst->out_width;
352                 s->r.height = inst->out_height;
353                 break;
354         default:
355                 return -EINVAL;
356         }
357
358         s->r.top = 0;
359         s->r.left = 0;
360
361         return 0;
362 }
363
364 static int
365 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
366 {
367         strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
368         strlcpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
369         strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
370
371         return 0;
372 }
373
374 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
375 {
376         struct venus_inst *inst = to_inst(file);
377         const struct venus_format *fmt;
378
379         memset(f->reserved, 0, sizeof(f->reserved));
380
381         fmt = find_format_by_index(inst, f->index, f->type);
382         if (!fmt)
383                 return -EINVAL;
384
385         f->pixelformat = fmt->pixfmt;
386
387         return 0;
388 }
389
390 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
391 {
392         struct venus_inst *inst = to_inst(file);
393         struct v4l2_captureparm *cap = &a->parm.capture;
394         struct v4l2_fract *timeperframe = &cap->timeperframe;
395         u64 us_per_frame, fps;
396
397         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
398             a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
399                 return -EINVAL;
400
401         memset(cap->reserved, 0, sizeof(cap->reserved));
402         if (!timeperframe->denominator)
403                 timeperframe->denominator = inst->timeperframe.denominator;
404         if (!timeperframe->numerator)
405                 timeperframe->numerator = inst->timeperframe.numerator;
406         cap->readbuffers = 0;
407         cap->extendedmode = 0;
408         cap->capability = V4L2_CAP_TIMEPERFRAME;
409         us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
410         do_div(us_per_frame, timeperframe->denominator);
411
412         if (!us_per_frame)
413                 return -EINVAL;
414
415         fps = (u64)USEC_PER_SEC;
416         do_div(fps, us_per_frame);
417
418         inst->fps = fps;
419         inst->timeperframe = *timeperframe;
420
421         return 0;
422 }
423
424 static int vdec_enum_framesizes(struct file *file, void *fh,
425                                 struct v4l2_frmsizeenum *fsize)
426 {
427         struct venus_inst *inst = to_inst(file);
428         const struct venus_format *fmt;
429
430         fmt = find_format(inst, fsize->pixel_format,
431                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
432         if (!fmt) {
433                 fmt = find_format(inst, fsize->pixel_format,
434                                   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
435                 if (!fmt)
436                         return -EINVAL;
437         }
438
439         if (fsize->index)
440                 return -EINVAL;
441
442         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
443
444         fsize->stepwise.min_width = inst->cap_width.min;
445         fsize->stepwise.max_width = inst->cap_width.max;
446         fsize->stepwise.step_width = inst->cap_width.step_size;
447         fsize->stepwise.min_height = inst->cap_height.min;
448         fsize->stepwise.max_height = inst->cap_height.max;
449         fsize->stepwise.step_height = inst->cap_height.step_size;
450
451         return 0;
452 }
453
454 static int vdec_subscribe_event(struct v4l2_fh *fh,
455                                 const struct v4l2_event_subscription *sub)
456 {
457         switch (sub->type) {
458         case V4L2_EVENT_EOS:
459                 return v4l2_event_subscribe(fh, sub, 2, NULL);
460         case V4L2_EVENT_SOURCE_CHANGE:
461                 return v4l2_src_change_event_subscribe(fh, sub);
462         case V4L2_EVENT_CTRL:
463                 return v4l2_ctrl_subscribe_event(fh, sub);
464         default:
465                 return -EINVAL;
466         }
467 }
468
469 static int
470 vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
471 {
472         switch (cmd->cmd) {
473         case V4L2_DEC_CMD_STOP:
474                 if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
475                         return -EINVAL;
476                 break;
477         default:
478                 return -EINVAL;
479         }
480
481         return 0;
482 }
483
484 static int
485 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
486 {
487         struct venus_inst *inst = to_inst(file);
488         struct hfi_frame_data fdata = {0};
489         int ret;
490
491         ret = vdec_try_decoder_cmd(file, fh, cmd);
492         if (ret)
493                 return ret;
494
495         mutex_lock(&inst->lock);
496
497         /*
498          * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on decoder
499          * input to signal EOS.
500          */
501         if (!(inst->streamon_out & inst->streamon_cap))
502                 goto unlock;
503
504         fdata.buffer_type = HFI_BUFFER_INPUT;
505         fdata.flags |= HFI_BUFFERFLAG_EOS;
506         fdata.device_addr = 0xdeadbeef;
507
508         ret = hfi_session_process_buf(inst, &fdata);
509
510 unlock:
511         mutex_unlock(&inst->lock);
512         return ret;
513 }
514
515 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
516         .vidioc_querycap = vdec_querycap,
517         .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt,
518         .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt,
519         .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
520         .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
521         .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
522         .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
523         .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
524         .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
525         .vidioc_g_selection = vdec_g_selection,
526         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
527         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
528         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
529         .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
530         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
531         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
532         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
533         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
534         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
535         .vidioc_s_parm = vdec_s_parm,
536         .vidioc_enum_framesizes = vdec_enum_framesizes,
537         .vidioc_subscribe_event = vdec_subscribe_event,
538         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
539         .vidioc_try_decoder_cmd = vdec_try_decoder_cmd,
540         .vidioc_decoder_cmd = vdec_decoder_cmd,
541 };
542
543 static int vdec_set_properties(struct venus_inst *inst)
544 {
545         struct vdec_controls *ctr = &inst->controls.dec;
546         struct venus_core *core = inst->core;
547         struct hfi_enable en = { .enable = 1 };
548         u32 ptype;
549         int ret;
550
551         if (core->res->hfi_version == HFI_VERSION_1XX) {
552                 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
553                 ret = hfi_session_set_property(inst, ptype, &en);
554                 if (ret)
555                         return ret;
556         }
557
558         if (core->res->hfi_version == HFI_VERSION_3XX ||
559             inst->cap_bufs_mode_dynamic) {
560                 struct hfi_buffer_alloc_mode mode;
561
562                 ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
563                 mode.type = HFI_BUFFER_OUTPUT;
564                 mode.mode = HFI_BUFFER_MODE_DYNAMIC;
565
566                 ret = hfi_session_set_property(inst, ptype, &mode);
567                 if (ret)
568                         return ret;
569         }
570
571         if (ctr->post_loop_deb_mode) {
572                 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
573                 en.enable = 1;
574                 ret = hfi_session_set_property(inst, ptype, &en);
575                 if (ret)
576                         return ret;
577         }
578
579         return 0;
580 }
581
582 static int vdec_init_session(struct venus_inst *inst)
583 {
584         int ret;
585
586         ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
587         if (ret)
588                 return ret;
589
590         ret = venus_helper_set_input_resolution(inst, inst->out_width,
591                                                 inst->out_height);
592         if (ret)
593                 goto deinit;
594
595         ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt);
596         if (ret)
597                 goto deinit;
598
599         return 0;
600 deinit:
601         hfi_session_deinit(inst);
602         return ret;
603 }
604
605 static int vdec_cap_num_buffers(struct venus_inst *inst, unsigned int *num)
606 {
607         struct hfi_buffer_requirements bufreq;
608         int ret;
609
610         ret = vdec_init_session(inst);
611         if (ret)
612                 return ret;
613
614         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
615
616         *num = bufreq.count_actual;
617
618         hfi_session_deinit(inst);
619
620         return ret;
621 }
622
623 static int vdec_queue_setup(struct vb2_queue *q,
624                             unsigned int *num_buffers, unsigned int *num_planes,
625                             unsigned int sizes[], struct device *alloc_devs[])
626 {
627         struct venus_inst *inst = vb2_get_drv_priv(q);
628         unsigned int p, num;
629         int ret = 0;
630
631         if (*num_planes) {
632                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
633                     *num_planes != inst->fmt_out->num_planes)
634                         return -EINVAL;
635
636                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
637                     *num_planes != inst->fmt_cap->num_planes)
638                         return -EINVAL;
639
640                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
641                     sizes[0] < inst->input_buf_size)
642                         return -EINVAL;
643
644                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
645                     sizes[0] < inst->output_buf_size)
646                         return -EINVAL;
647
648                 return 0;
649         }
650
651         switch (q->type) {
652         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
653                 *num_planes = inst->fmt_out->num_planes;
654                 sizes[0] = get_framesize_compressed(inst->out_width,
655                                                     inst->out_height);
656                 inst->input_buf_size = sizes[0];
657                 inst->num_input_bufs = *num_buffers;
658
659                 ret = vdec_cap_num_buffers(inst, &num);
660                 if (ret)
661                         break;
662
663                 inst->num_output_bufs = num;
664                 break;
665         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
666                 *num_planes = inst->fmt_cap->num_planes;
667
668                 ret = vdec_cap_num_buffers(inst, &num);
669                 if (ret)
670                         break;
671
672                 *num_buffers = max(*num_buffers, num);
673
674                 for (p = 0; p < *num_planes; p++)
675                         sizes[p] = get_framesize_uncompressed(p, inst->width,
676                                                               inst->height);
677
678                 inst->num_output_bufs = *num_buffers;
679                 inst->output_buf_size = sizes[0];
680                 break;
681         default:
682                 ret = -EINVAL;
683                 break;
684         }
685
686         return ret;
687 }
688
689 static int vdec_verify_conf(struct venus_inst *inst)
690 {
691         struct hfi_buffer_requirements bufreq;
692         int ret;
693
694         if (!inst->num_input_bufs || !inst->num_output_bufs)
695                 return -EINVAL;
696
697         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
698         if (ret)
699                 return ret;
700
701         if (inst->num_output_bufs < bufreq.count_actual ||
702             inst->num_output_bufs < bufreq.count_min)
703                 return -EINVAL;
704
705         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
706         if (ret)
707                 return ret;
708
709         if (inst->num_input_bufs < bufreq.count_min)
710                 return -EINVAL;
711
712         return 0;
713 }
714
715 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
716 {
717         struct venus_inst *inst = vb2_get_drv_priv(q);
718         struct venus_core *core = inst->core;
719         u32 ptype;
720         int ret;
721
722         mutex_lock(&inst->lock);
723
724         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
725                 inst->streamon_out = 1;
726         else
727                 inst->streamon_cap = 1;
728
729         if (!(inst->streamon_out & inst->streamon_cap)) {
730                 mutex_unlock(&inst->lock);
731                 return 0;
732         }
733
734         venus_helper_init_instance(inst);
735
736         inst->reconfig = false;
737         inst->sequence_cap = 0;
738         inst->sequence_out = 0;
739
740         ret = vdec_init_session(inst);
741         if (ret)
742                 goto bufs_done;
743
744         ret = vdec_set_properties(inst);
745         if (ret)
746                 goto deinit_sess;
747
748         if (core->res->hfi_version == HFI_VERSION_3XX) {
749                 struct hfi_buffer_size_actual buf_sz;
750
751                 ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
752                 buf_sz.type = HFI_BUFFER_OUTPUT;
753                 buf_sz.size = inst->output_buf_size;
754
755                 ret = hfi_session_set_property(inst, ptype, &buf_sz);
756                 if (ret)
757                         goto deinit_sess;
758         }
759
760         ret = vdec_verify_conf(inst);
761         if (ret)
762                 goto deinit_sess;
763
764         ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
765                                         VB2_MAX_FRAME);
766         if (ret)
767                 goto deinit_sess;
768
769         ret = venus_helper_vb2_start_streaming(inst);
770         if (ret)
771                 goto deinit_sess;
772
773         mutex_unlock(&inst->lock);
774
775         return 0;
776
777 deinit_sess:
778         hfi_session_deinit(inst);
779 bufs_done:
780         venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
781         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
782                 inst->streamon_out = 0;
783         else
784                 inst->streamon_cap = 0;
785         mutex_unlock(&inst->lock);
786         return ret;
787 }
788
789 static const struct vb2_ops vdec_vb2_ops = {
790         .queue_setup = vdec_queue_setup,
791         .buf_init = venus_helper_vb2_buf_init,
792         .buf_prepare = venus_helper_vb2_buf_prepare,
793         .start_streaming = vdec_start_streaming,
794         .stop_streaming = venus_helper_vb2_stop_streaming,
795         .buf_queue = venus_helper_vb2_buf_queue,
796 };
797
798 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
799                           u32 tag, u32 bytesused, u32 data_offset, u32 flags,
800                           u32 hfi_flags, u64 timestamp_us)
801 {
802         enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
803         struct vb2_v4l2_buffer *vbuf;
804         struct vb2_buffer *vb;
805         unsigned int type;
806
807         if (buf_type == HFI_BUFFER_INPUT)
808                 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
809         else
810                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
811
812         vbuf = venus_helper_find_buf(inst, type, tag);
813         if (!vbuf)
814                 return;
815
816         vbuf->flags = flags;
817         vbuf->field = V4L2_FIELD_NONE;
818
819         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
820                 vb = &vbuf->vb2_buf;
821                 vb->planes[0].bytesused =
822                         max_t(unsigned int, inst->output_buf_size, bytesused);
823                 vb->planes[0].data_offset = data_offset;
824                 vb->timestamp = timestamp_us * NSEC_PER_USEC;
825                 vbuf->sequence = inst->sequence_cap++;
826
827                 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
828                         const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
829
830                         v4l2_event_queue_fh(&inst->fh, &ev);
831                 }
832         } else {
833                 vbuf->sequence = inst->sequence_out++;
834         }
835
836         if (hfi_flags & HFI_BUFFERFLAG_READONLY)
837                 venus_helper_acquire_buf_ref(vbuf);
838
839         if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
840                 state = VB2_BUF_STATE_ERROR;
841
842         v4l2_m2m_buf_done(vbuf, state);
843 }
844
845 static void vdec_event_notify(struct venus_inst *inst, u32 event,
846                               struct hfi_event_data *data)
847 {
848         struct venus_core *core = inst->core;
849         struct device *dev = core->dev_dec;
850         static const struct v4l2_event ev = {
851                 .type = V4L2_EVENT_SOURCE_CHANGE,
852                 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
853
854         switch (event) {
855         case EVT_SESSION_ERROR:
856                 inst->session_error = true;
857                 dev_err(dev, "dec: event session error %x\n", inst->error);
858                 break;
859         case EVT_SYS_EVENT_CHANGE:
860                 switch (data->event_type) {
861                 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
862                         hfi_session_continue(inst);
863                         dev_dbg(dev, "event sufficient resources\n");
864                         break;
865                 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
866                         inst->reconfig_height = data->height;
867                         inst->reconfig_width = data->width;
868                         inst->reconfig = true;
869
870                         v4l2_event_queue_fh(&inst->fh, &ev);
871
872                         dev_dbg(dev, "event not sufficient resources (%ux%u)\n",
873                                 data->width, data->height);
874                         break;
875                 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
876                         venus_helper_release_buf_ref(inst, data->tag);
877                         break;
878                 default:
879                         break;
880                 }
881                 break;
882         default:
883                 break;
884         }
885 }
886
887 static const struct hfi_inst_ops vdec_hfi_ops = {
888         .buf_done = vdec_buf_done,
889         .event_notify = vdec_event_notify,
890 };
891
892 static void vdec_inst_init(struct venus_inst *inst)
893 {
894         inst->fmt_out = &vdec_formats[6];
895         inst->fmt_cap = &vdec_formats[0];
896         inst->width = 1280;
897         inst->height = ALIGN(720, 32);
898         inst->out_width = 1280;
899         inst->out_height = 720;
900         inst->fps = 30;
901         inst->timeperframe.numerator = 1;
902         inst->timeperframe.denominator = 30;
903
904         inst->cap_width.min = 64;
905         inst->cap_width.max = 1920;
906         if (inst->core->res->hfi_version == HFI_VERSION_3XX)
907                 inst->cap_width.max = 3840;
908         inst->cap_width.step_size = 1;
909         inst->cap_height.min = 64;
910         inst->cap_height.max = ALIGN(1080, 32);
911         if (inst->core->res->hfi_version == HFI_VERSION_3XX)
912                 inst->cap_height.max = ALIGN(2160, 32);
913         inst->cap_height.step_size = 1;
914         inst->cap_framerate.min = 1;
915         inst->cap_framerate.max = 30;
916         inst->cap_framerate.step_size = 1;
917         inst->cap_mbs_per_frame.min = 16;
918         inst->cap_mbs_per_frame.max = 8160;
919 }
920
921 static const struct v4l2_m2m_ops vdec_m2m_ops = {
922         .device_run = venus_helper_m2m_device_run,
923         .job_abort = venus_helper_m2m_job_abort,
924 };
925
926 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
927                           struct vb2_queue *dst_vq)
928 {
929         struct venus_inst *inst = priv;
930         int ret;
931
932         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
933         src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
934         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
935         src_vq->ops = &vdec_vb2_ops;
936         src_vq->mem_ops = &vb2_dma_sg_memops;
937         src_vq->drv_priv = inst;
938         src_vq->buf_struct_size = sizeof(struct venus_buffer);
939         src_vq->allow_zero_bytesused = 1;
940         src_vq->min_buffers_needed = 1;
941         src_vq->dev = inst->core->dev;
942         ret = vb2_queue_init(src_vq);
943         if (ret)
944                 return ret;
945
946         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
947         dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
948         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
949         dst_vq->ops = &vdec_vb2_ops;
950         dst_vq->mem_ops = &vb2_dma_sg_memops;
951         dst_vq->drv_priv = inst;
952         dst_vq->buf_struct_size = sizeof(struct venus_buffer);
953         dst_vq->allow_zero_bytesused = 1;
954         dst_vq->min_buffers_needed = 1;
955         dst_vq->dev = inst->core->dev;
956         ret = vb2_queue_init(dst_vq);
957         if (ret) {
958                 vb2_queue_release(src_vq);
959                 return ret;
960         }
961
962         return 0;
963 }
964
965 static int vdec_open(struct file *file)
966 {
967         struct venus_core *core = video_drvdata(file);
968         struct venus_inst *inst;
969         int ret;
970
971         inst = kzalloc(sizeof(*inst), GFP_KERNEL);
972         if (!inst)
973                 return -ENOMEM;
974
975         INIT_LIST_HEAD(&inst->registeredbufs);
976         INIT_LIST_HEAD(&inst->internalbufs);
977         INIT_LIST_HEAD(&inst->list);
978         mutex_init(&inst->lock);
979
980         inst->core = core;
981         inst->session_type = VIDC_SESSION_TYPE_DEC;
982         inst->num_output_bufs = 1;
983
984         venus_helper_init_instance(inst);
985
986         ret = pm_runtime_get_sync(core->dev_dec);
987         if (ret < 0)
988                 goto err_free_inst;
989
990         ret = vdec_ctrl_init(inst);
991         if (ret)
992                 goto err_put_sync;
993
994         ret = hfi_session_create(inst, &vdec_hfi_ops);
995         if (ret)
996                 goto err_ctrl_deinit;
997
998         vdec_inst_init(inst);
999
1000         /*
1001          * create m2m device for every instance, the m2m context scheduling
1002          * is made by firmware side so we do not need to care about.
1003          */
1004         inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1005         if (IS_ERR(inst->m2m_dev)) {
1006                 ret = PTR_ERR(inst->m2m_dev);
1007                 goto err_session_destroy;
1008         }
1009
1010         inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1011         if (IS_ERR(inst->m2m_ctx)) {
1012                 ret = PTR_ERR(inst->m2m_ctx);
1013                 goto err_m2m_release;
1014         }
1015
1016         v4l2_fh_init(&inst->fh, core->vdev_dec);
1017
1018         inst->fh.ctrl_handler = &inst->ctrl_handler;
1019         v4l2_fh_add(&inst->fh);
1020         inst->fh.m2m_ctx = inst->m2m_ctx;
1021         file->private_data = &inst->fh;
1022
1023         return 0;
1024
1025 err_m2m_release:
1026         v4l2_m2m_release(inst->m2m_dev);
1027 err_session_destroy:
1028         hfi_session_destroy(inst);
1029 err_ctrl_deinit:
1030         vdec_ctrl_deinit(inst);
1031 err_put_sync:
1032         pm_runtime_put_sync(core->dev_dec);
1033 err_free_inst:
1034         kfree(inst);
1035         return ret;
1036 }
1037
1038 static int vdec_close(struct file *file)
1039 {
1040         struct venus_inst *inst = to_inst(file);
1041
1042         v4l2_m2m_ctx_release(inst->m2m_ctx);
1043         v4l2_m2m_release(inst->m2m_dev);
1044         vdec_ctrl_deinit(inst);
1045         hfi_session_destroy(inst);
1046         mutex_destroy(&inst->lock);
1047         v4l2_fh_del(&inst->fh);
1048         v4l2_fh_exit(&inst->fh);
1049
1050         pm_runtime_put_sync(inst->core->dev_dec);
1051
1052         kfree(inst);
1053         return 0;
1054 }
1055
1056 static const struct v4l2_file_operations vdec_fops = {
1057         .owner = THIS_MODULE,
1058         .open = vdec_open,
1059         .release = vdec_close,
1060         .unlocked_ioctl = video_ioctl2,
1061         .poll = v4l2_m2m_fop_poll,
1062         .mmap = v4l2_m2m_fop_mmap,
1063 };
1064
1065 static int vdec_probe(struct platform_device *pdev)
1066 {
1067         struct device *dev = &pdev->dev;
1068         struct video_device *vdev;
1069         struct venus_core *core;
1070         int ret;
1071
1072         if (!dev->parent)
1073                 return -EPROBE_DEFER;
1074
1075         core = dev_get_drvdata(dev->parent);
1076         if (!core)
1077                 return -EPROBE_DEFER;
1078
1079         if (core->res->hfi_version == HFI_VERSION_3XX) {
1080                 core->core0_clk = devm_clk_get(dev, "core");
1081                 if (IS_ERR(core->core0_clk))
1082                         return PTR_ERR(core->core0_clk);
1083         }
1084
1085         platform_set_drvdata(pdev, core);
1086
1087         vdev = video_device_alloc();
1088         if (!vdev)
1089                 return -ENOMEM;
1090
1091         strlcpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1092         vdev->release = video_device_release;
1093         vdev->fops = &vdec_fops;
1094         vdev->ioctl_ops = &vdec_ioctl_ops;
1095         vdev->vfl_dir = VFL_DIR_M2M;
1096         vdev->v4l2_dev = &core->v4l2_dev;
1097         vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1098
1099         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1100         if (ret)
1101                 goto err_vdev_release;
1102
1103         core->vdev_dec = vdev;
1104         core->dev_dec = dev;
1105
1106         video_set_drvdata(vdev, core);
1107         pm_runtime_enable(dev);
1108
1109         return 0;
1110
1111 err_vdev_release:
1112         video_device_release(vdev);
1113         return ret;
1114 }
1115
1116 static int vdec_remove(struct platform_device *pdev)
1117 {
1118         struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1119
1120         video_unregister_device(core->vdev_dec);
1121         pm_runtime_disable(core->dev_dec);
1122
1123         return 0;
1124 }
1125
1126 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1127 {
1128         struct venus_core *core = dev_get_drvdata(dev);
1129
1130         if (core->res->hfi_version == HFI_VERSION_1XX)
1131                 return 0;
1132
1133         writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1134         clk_disable_unprepare(core->core0_clk);
1135         writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1136
1137         return 0;
1138 }
1139
1140 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1141 {
1142         struct venus_core *core = dev_get_drvdata(dev);
1143         int ret;
1144
1145         if (core->res->hfi_version == HFI_VERSION_1XX)
1146                 return 0;
1147
1148         writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1149         ret = clk_prepare_enable(core->core0_clk);
1150         writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1151
1152         return ret;
1153 }
1154
1155 static const struct dev_pm_ops vdec_pm_ops = {
1156         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1157                                 pm_runtime_force_resume)
1158         SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1159 };
1160
1161 static const struct of_device_id vdec_dt_match[] = {
1162         { .compatible = "venus-decoder" },
1163         { }
1164 };
1165 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1166
1167 static struct platform_driver qcom_venus_dec_driver = {
1168         .probe = vdec_probe,
1169         .remove = vdec_remove,
1170         .driver = {
1171                 .name = "qcom-venus-decoder",
1172                 .of_match_table = vdec_dt_match,
1173                 .pm = &vdec_pm_ops,
1174         },
1175 };
1176 module_platform_driver(qcom_venus_dec_driver);
1177
1178 MODULE_ALIAS("platform:qcom-venus-decoder");
1179 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1180 MODULE_LICENSE("GPL v2");