EDAC/igen6: ecclog_llist can be static
[sfrench/cifs-2.6.git] / drivers / media / platform / qcom / venus / venc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 #include <linux/clk.h>
7 #include <linux/module.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/slab.h>
12 #include <media/v4l2-mem2mem.h>
13 #include <media/videobuf2-dma-sg.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-ctrls.h>
17
18 #include "hfi_venus_io.h"
19 #include "hfi_parser.h"
20 #include "core.h"
21 #include "helpers.h"
22 #include "venc.h"
23 #include "pm_helpers.h"
24
25 #define NUM_B_FRAMES_MAX        4
26
27 /*
28  * Three resons to keep MPLANE formats (despite that the number of planes
29  * currently is one):
30  * - the MPLANE formats allow only one plane to be used
31  * - the downstream driver use MPLANE formats too
32  * - future firmware versions could add support for >1 planes
33  */
34 static const struct venus_format venc_formats[] = {
35         {
36                 .pixfmt = V4L2_PIX_FMT_NV12,
37                 .num_planes = 1,
38                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
39         }, {
40                 .pixfmt = V4L2_PIX_FMT_MPEG4,
41                 .num_planes = 1,
42                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
43         }, {
44                 .pixfmt = V4L2_PIX_FMT_H263,
45                 .num_planes = 1,
46                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
47         }, {
48                 .pixfmt = V4L2_PIX_FMT_H264,
49                 .num_planes = 1,
50                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
51         }, {
52                 .pixfmt = V4L2_PIX_FMT_VP8,
53                 .num_planes = 1,
54                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
55         }, {
56                 .pixfmt = V4L2_PIX_FMT_HEVC,
57                 .num_planes = 1,
58                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
59         },
60 };
61
62 static const struct venus_format *
63 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
64 {
65         const struct venus_format *fmt = venc_formats;
66         unsigned int size = ARRAY_SIZE(venc_formats);
67         unsigned int i;
68
69         for (i = 0; i < size; i++) {
70                 if (fmt[i].pixfmt == pixfmt)
71                         break;
72         }
73
74         if (i == size || fmt[i].type != type)
75                 return NULL;
76
77         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
78             !venus_helper_check_codec(inst, fmt[i].pixfmt))
79                 return NULL;
80
81         return &fmt[i];
82 }
83
84 static const struct venus_format *
85 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
86 {
87         const struct venus_format *fmt = venc_formats;
88         unsigned int size = ARRAY_SIZE(venc_formats);
89         unsigned int i, k = 0;
90
91         if (index > size)
92                 return NULL;
93
94         for (i = 0; i < size; i++) {
95                 bool valid;
96
97                 if (fmt[i].type != type)
98                         continue;
99                 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
100                         venus_helper_check_codec(inst, fmt[i].pixfmt);
101                 if (k == index && valid)
102                         break;
103                 if (valid)
104                         k++;
105         }
106
107         if (i == size)
108                 return NULL;
109
110         return &fmt[i];
111 }
112
113 static int venc_v4l2_to_hfi(int id, int value)
114 {
115         switch (id) {
116         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
117                 switch (value) {
118                 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
119                 default:
120                         return HFI_H264_ENTROPY_CAVLC;
121                 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
122                         return HFI_H264_ENTROPY_CABAC;
123                 }
124         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
125                 switch (value) {
126                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
127                 default:
128                         return HFI_H264_DB_MODE_ALL_BOUNDARY;
129                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
130                         return HFI_H264_DB_MODE_DISABLE;
131                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
132                         return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
133                 }
134         }
135
136         return 0;
137 }
138
139 static int
140 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
141 {
142         strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
143         strscpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
144         strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
145
146         return 0;
147 }
148
149 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
150 {
151         struct venus_inst *inst = to_inst(file);
152         const struct venus_format *fmt;
153
154         fmt = find_format_by_index(inst, f->index, f->type);
155
156         memset(f->reserved, 0, sizeof(f->reserved));
157
158         if (!fmt)
159                 return -EINVAL;
160
161         f->pixelformat = fmt->pixfmt;
162
163         return 0;
164 }
165
166 static const struct venus_format *
167 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
168 {
169         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
170         struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
171         const struct venus_format *fmt;
172         u32 sizeimage;
173
174         memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
175         memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
176
177         fmt = find_format(inst, pixmp->pixelformat, f->type);
178         if (!fmt) {
179                 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
180                         pixmp->pixelformat = V4L2_PIX_FMT_H264;
181                 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
182                         pixmp->pixelformat = V4L2_PIX_FMT_NV12;
183                 else
184                         return NULL;
185                 fmt = find_format(inst, pixmp->pixelformat, f->type);
186         }
187
188         pixmp->width = clamp(pixmp->width, frame_width_min(inst),
189                              frame_width_max(inst));
190         pixmp->height = clamp(pixmp->height, frame_height_min(inst),
191                               frame_height_max(inst));
192
193         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
194                 pixmp->height = ALIGN(pixmp->height, 32);
195
196         pixmp->width = ALIGN(pixmp->width, 2);
197         pixmp->height = ALIGN(pixmp->height, 2);
198
199         if (pixmp->field == V4L2_FIELD_ANY)
200                 pixmp->field = V4L2_FIELD_NONE;
201         pixmp->num_planes = fmt->num_planes;
202         pixmp->flags = 0;
203
204         sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
205                                              pixmp->width,
206                                              pixmp->height);
207         pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage);
208
209         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
210                 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
211         else
212                 pfmt[0].bytesperline = 0;
213
214         return fmt;
215 }
216
217 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
218 {
219         struct venus_inst *inst = to_inst(file);
220
221         venc_try_fmt_common(inst, f);
222
223         return 0;
224 }
225
226 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
227 {
228         struct venus_inst *inst = to_inst(file);
229         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
230         struct v4l2_pix_format_mplane orig_pixmp;
231         const struct venus_format *fmt;
232         struct v4l2_format format;
233         u32 pixfmt_out = 0, pixfmt_cap = 0;
234         struct vb2_queue *q;
235
236         q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
237         if (!q)
238                 return -EINVAL;
239
240         if (vb2_is_busy(q))
241                 return -EBUSY;
242
243         orig_pixmp = *pixmp;
244
245         fmt = venc_try_fmt_common(inst, f);
246         if (!fmt)
247                 return -EINVAL;
248
249         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
250                 pixfmt_out = pixmp->pixelformat;
251                 pixfmt_cap = inst->fmt_cap->pixfmt;
252         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
253                 pixfmt_cap = pixmp->pixelformat;
254                 pixfmt_out = inst->fmt_out->pixfmt;
255         }
256
257         memset(&format, 0, sizeof(format));
258
259         format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
260         format.fmt.pix_mp.pixelformat = pixfmt_out;
261         format.fmt.pix_mp.width = orig_pixmp.width;
262         format.fmt.pix_mp.height = orig_pixmp.height;
263         venc_try_fmt_common(inst, &format);
264
265         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
266                 inst->out_width = format.fmt.pix_mp.width;
267                 inst->out_height = format.fmt.pix_mp.height;
268                 inst->colorspace = pixmp->colorspace;
269                 inst->ycbcr_enc = pixmp->ycbcr_enc;
270                 inst->quantization = pixmp->quantization;
271                 inst->xfer_func = pixmp->xfer_func;
272         }
273
274         memset(&format, 0, sizeof(format));
275
276         format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
277         format.fmt.pix_mp.pixelformat = pixfmt_cap;
278         format.fmt.pix_mp.width = orig_pixmp.width;
279         format.fmt.pix_mp.height = orig_pixmp.height;
280         venc_try_fmt_common(inst, &format);
281
282         inst->width = format.fmt.pix_mp.width;
283         inst->height = format.fmt.pix_mp.height;
284
285         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
286                 inst->fmt_out = fmt;
287         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
288                 inst->fmt_cap = fmt;
289                 inst->output_buf_size = pixmp->plane_fmt[0].sizeimage;
290         }
291
292         return 0;
293 }
294
295 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
296 {
297         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
298         struct venus_inst *inst = to_inst(file);
299         const struct venus_format *fmt;
300
301         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
302                 fmt = inst->fmt_cap;
303         else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
304                 fmt = inst->fmt_out;
305         else
306                 return -EINVAL;
307
308         pixmp->pixelformat = fmt->pixfmt;
309
310         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
311                 pixmp->width = inst->width;
312                 pixmp->height = inst->height;
313                 pixmp->colorspace = inst->colorspace;
314                 pixmp->ycbcr_enc = inst->ycbcr_enc;
315                 pixmp->quantization = inst->quantization;
316                 pixmp->xfer_func = inst->xfer_func;
317         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
318                 pixmp->width = inst->out_width;
319                 pixmp->height = inst->out_height;
320         }
321
322         venc_try_fmt_common(inst, f);
323
324         return 0;
325 }
326
327 static int
328 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
329 {
330         struct venus_inst *inst = to_inst(file);
331
332         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
333                 return -EINVAL;
334
335         switch (s->target) {
336         case V4L2_SEL_TGT_CROP_DEFAULT:
337         case V4L2_SEL_TGT_CROP_BOUNDS:
338                 s->r.width = inst->width;
339                 s->r.height = inst->height;
340                 break;
341         case V4L2_SEL_TGT_CROP:
342                 s->r.width = inst->out_width;
343                 s->r.height = inst->out_height;
344                 break;
345         default:
346                 return -EINVAL;
347         }
348
349         s->r.top = 0;
350         s->r.left = 0;
351
352         return 0;
353 }
354
355 static int
356 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
357 {
358         struct venus_inst *inst = to_inst(file);
359
360         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
361                 return -EINVAL;
362
363         switch (s->target) {
364         case V4L2_SEL_TGT_CROP:
365                 if (s->r.width != inst->out_width ||
366                     s->r.height != inst->out_height ||
367                     s->r.top != 0 || s->r.left != 0)
368                         return -EINVAL;
369                 break;
370         default:
371                 return -EINVAL;
372         }
373
374         return 0;
375 }
376
377 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
378 {
379         struct venus_inst *inst = to_inst(file);
380         struct v4l2_outputparm *out = &a->parm.output;
381         struct v4l2_fract *timeperframe = &out->timeperframe;
382         u64 us_per_frame, fps;
383
384         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
385             a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
386                 return -EINVAL;
387
388         memset(out->reserved, 0, sizeof(out->reserved));
389
390         if (!timeperframe->denominator)
391                 timeperframe->denominator = inst->timeperframe.denominator;
392         if (!timeperframe->numerator)
393                 timeperframe->numerator = inst->timeperframe.numerator;
394
395         out->capability = V4L2_CAP_TIMEPERFRAME;
396
397         us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
398         do_div(us_per_frame, timeperframe->denominator);
399
400         if (!us_per_frame)
401                 return -EINVAL;
402
403         fps = (u64)USEC_PER_SEC;
404         do_div(fps, us_per_frame);
405
406         inst->timeperframe = *timeperframe;
407         inst->fps = fps;
408
409         return 0;
410 }
411
412 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
413 {
414         struct venus_inst *inst = to_inst(file);
415
416         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
417             a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
418                 return -EINVAL;
419
420         a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
421         a->parm.output.timeperframe = inst->timeperframe;
422
423         return 0;
424 }
425
426 static int venc_enum_framesizes(struct file *file, void *fh,
427                                 struct v4l2_frmsizeenum *fsize)
428 {
429         struct venus_inst *inst = to_inst(file);
430         const struct venus_format *fmt;
431
432         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
433
434         fmt = find_format(inst, fsize->pixel_format,
435                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
436         if (!fmt) {
437                 fmt = find_format(inst, fsize->pixel_format,
438                                   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
439                 if (!fmt)
440                         return -EINVAL;
441         }
442
443         if (fsize->index)
444                 return -EINVAL;
445
446         fsize->stepwise.min_width = frame_width_min(inst);
447         fsize->stepwise.max_width = frame_width_max(inst);
448         fsize->stepwise.step_width = frame_width_step(inst);
449         fsize->stepwise.min_height = frame_height_min(inst);
450         fsize->stepwise.max_height = frame_height_max(inst);
451         fsize->stepwise.step_height = frame_height_step(inst);
452
453         return 0;
454 }
455
456 static int venc_enum_frameintervals(struct file *file, void *fh,
457                                     struct v4l2_frmivalenum *fival)
458 {
459         struct venus_inst *inst = to_inst(file);
460         const struct venus_format *fmt;
461         unsigned int framerate_factor = 1;
462
463         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
464
465         fmt = find_format(inst, fival->pixel_format,
466                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
467         if (!fmt) {
468                 fmt = find_format(inst, fival->pixel_format,
469                                   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
470                 if (!fmt)
471                         return -EINVAL;
472         }
473
474         if (fival->index)
475                 return -EINVAL;
476
477         if (!fival->width || !fival->height)
478                 return -EINVAL;
479
480         if (fival->width > frame_width_max(inst) ||
481             fival->width < frame_width_min(inst) ||
482             fival->height > frame_height_max(inst) ||
483             fival->height < frame_height_min(inst))
484                 return -EINVAL;
485
486         if (IS_V1(inst->core)) {
487                 /* framerate is reported in 1/65535 fps unit */
488                 framerate_factor = (1 << 16);
489         }
490
491         fival->stepwise.min.numerator = 1;
492         fival->stepwise.min.denominator = frate_max(inst) / framerate_factor;
493         fival->stepwise.max.numerator = 1;
494         fival->stepwise.max.denominator = frate_min(inst) / framerate_factor;
495         fival->stepwise.step.numerator = 1;
496         fival->stepwise.step.denominator = frate_max(inst) / framerate_factor;
497
498         return 0;
499 }
500
501 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
502         .vidioc_querycap = venc_querycap,
503         .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
504         .vidioc_enum_fmt_vid_out = venc_enum_fmt,
505         .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
506         .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
507         .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
508         .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
509         .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
510         .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
511         .vidioc_g_selection = venc_g_selection,
512         .vidioc_s_selection = venc_s_selection,
513         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
514         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
515         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
516         .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
517         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
518         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
519         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
520         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
521         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
522         .vidioc_s_parm = venc_s_parm,
523         .vidioc_g_parm = venc_g_parm,
524         .vidioc_enum_framesizes = venc_enum_framesizes,
525         .vidioc_enum_frameintervals = venc_enum_frameintervals,
526         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
527         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
528 };
529
530 static int venc_set_properties(struct venus_inst *inst)
531 {
532         struct venc_controls *ctr = &inst->controls.enc;
533         struct hfi_intra_period intra_period;
534         struct hfi_framerate frate;
535         struct hfi_bitrate brate;
536         struct hfi_idr_period idrp;
537         struct hfi_quantization quant;
538         struct hfi_quantization_range quant_range;
539         u32 ptype, rate_control, bitrate;
540         int ret;
541
542         ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
543         if (ret)
544                 return ret;
545
546         ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
547         frate.buffer_type = HFI_BUFFER_OUTPUT;
548         frate.framerate = inst->fps * (1 << 16);
549
550         ret = hfi_session_set_property(inst, ptype, &frate);
551         if (ret)
552                 return ret;
553
554         if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
555                 struct hfi_h264_vui_timing_info info;
556                 struct hfi_h264_entropy_control entropy;
557                 struct hfi_h264_db_control deblock;
558
559                 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
560                 info.enable = 1;
561                 info.fixed_framerate = 1;
562                 info.time_scale = NSEC_PER_SEC;
563
564                 ret = hfi_session_set_property(inst, ptype, &info);
565                 if (ret)
566                         return ret;
567
568                 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
569                 entropy.entropy_mode = venc_v4l2_to_hfi(
570                                           V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
571                                           ctr->h264_entropy_mode);
572                 entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
573
574                 ret = hfi_session_set_property(inst, ptype, &entropy);
575                 if (ret)
576                         return ret;
577
578                 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
579                 deblock.mode = venc_v4l2_to_hfi(
580                                       V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
581                                       ctr->h264_loop_filter_mode);
582                 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
583                 deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
584
585                 ret = hfi_session_set_property(inst, ptype, &deblock);
586                 if (ret)
587                         return ret;
588         }
589
590         /* IDR periodicity, n:
591          * n = 0 - only the first I-frame is IDR frame
592          * n = 1 - all I-frames will be IDR frames
593          * n > 1 - every n-th I-frame will be IDR frame
594          */
595         ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
596         idrp.idr_period = 0;
597         ret = hfi_session_set_property(inst, ptype, &idrp);
598         if (ret)
599                 return ret;
600
601         if (ctr->num_b_frames) {
602                 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
603
604                 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
605                 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
606                 if (ret)
607                         return ret;
608         }
609
610         ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
611         intra_period.pframes = ctr->num_p_frames;
612         intra_period.bframes = ctr->num_b_frames;
613
614         ret = hfi_session_set_property(inst, ptype, &intra_period);
615         if (ret)
616                 return ret;
617
618         if (!ctr->rc_enable)
619                 rate_control = HFI_RATE_CONTROL_OFF;
620         else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
621                 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR :
622                                                       HFI_RATE_CONTROL_VBR_CFR;
623         else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
624                 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR :
625                                                       HFI_RATE_CONTROL_CBR_CFR;
626         else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
627                 rate_control = HFI_RATE_CONTROL_CQ;
628
629         ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
630         ret = hfi_session_set_property(inst, ptype, &rate_control);
631         if (ret)
632                 return ret;
633
634         if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) {
635                 struct hfi_heic_frame_quality quality = {};
636
637                 ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY;
638                 quality.frame_quality = ctr->const_quality;
639                 ret = hfi_session_set_property(inst, ptype, &quality);
640                 if (ret)
641                         return ret;
642         }
643
644         if (!ctr->bitrate)
645                 bitrate = 64000;
646         else
647                 bitrate = ctr->bitrate;
648
649         ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
650         brate.bitrate = bitrate;
651         brate.layer_id = 0;
652
653         ret = hfi_session_set_property(inst, ptype, &brate);
654         if (ret)
655                 return ret;
656
657         if (!ctr->bitrate_peak)
658                 bitrate *= 2;
659         else
660                 bitrate = ctr->bitrate_peak;
661
662         ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
663         brate.bitrate = bitrate;
664         brate.layer_id = 0;
665
666         ret = hfi_session_set_property(inst, ptype, &brate);
667         if (ret)
668                 return ret;
669
670         ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
671         quant.qp_i = ctr->h264_i_qp;
672         quant.qp_p = ctr->h264_p_qp;
673         quant.qp_b = ctr->h264_b_qp;
674         quant.layer_id = 0;
675         ret = hfi_session_set_property(inst, ptype, &quant);
676         if (ret)
677                 return ret;
678
679         ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
680         quant_range.min_qp = ctr->h264_min_qp;
681         quant_range.max_qp = ctr->h264_max_qp;
682         quant_range.layer_id = 0;
683         ret = hfi_session_set_property(inst, ptype, &quant_range);
684         if (ret)
685                 return ret;
686
687         ret = venus_helper_set_profile_level(inst, ctr->profile, ctr->level);
688         if (ret)
689                 return ret;
690
691         return 0;
692 }
693
694 static int venc_init_session(struct venus_inst *inst)
695 {
696         int ret;
697
698         ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
699         if (ret)
700                 return ret;
701
702         ret = venus_helper_set_input_resolution(inst, inst->width,
703                                                 inst->height);
704         if (ret)
705                 goto deinit;
706
707         ret = venus_helper_set_output_resolution(inst, inst->width,
708                                                  inst->height,
709                                                  HFI_BUFFER_OUTPUT);
710         if (ret)
711                 goto deinit;
712
713         ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
714         if (ret)
715                 goto deinit;
716
717         ret = venus_helper_init_codec_freq_data(inst);
718         if (ret)
719                 goto deinit;
720
721         ret = venc_set_properties(inst);
722         if (ret)
723                 goto deinit;
724
725         return 0;
726 deinit:
727         hfi_session_deinit(inst);
728         return ret;
729 }
730
731 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
732 {
733         struct hfi_buffer_requirements bufreq;
734         int ret;
735
736         ret = venc_init_session(inst);
737         if (ret)
738                 return ret;
739
740         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
741
742         *num = bufreq.count_actual;
743
744         hfi_session_deinit(inst);
745
746         return ret;
747 }
748
749 static int venc_queue_setup(struct vb2_queue *q,
750                             unsigned int *num_buffers, unsigned int *num_planes,
751                             unsigned int sizes[], struct device *alloc_devs[])
752 {
753         struct venus_inst *inst = vb2_get_drv_priv(q);
754         unsigned int num, min = 4;
755         int ret = 0;
756
757         if (*num_planes) {
758                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
759                     *num_planes != inst->fmt_out->num_planes)
760                         return -EINVAL;
761
762                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
763                     *num_planes != inst->fmt_cap->num_planes)
764                         return -EINVAL;
765
766                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
767                     sizes[0] < inst->input_buf_size)
768                         return -EINVAL;
769
770                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
771                     sizes[0] < inst->output_buf_size)
772                         return -EINVAL;
773
774                 return 0;
775         }
776
777         switch (q->type) {
778         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
779                 *num_planes = inst->fmt_out->num_planes;
780
781                 ret = venc_out_num_buffers(inst, &num);
782                 if (ret)
783                         break;
784
785                 num = max(num, min);
786                 *num_buffers = max(*num_buffers, num);
787                 inst->num_input_bufs = *num_buffers;
788
789                 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
790                                                     inst->width,
791                                                     inst->height);
792                 inst->input_buf_size = sizes[0];
793                 break;
794         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
795                 *num_planes = inst->fmt_cap->num_planes;
796                 *num_buffers = max(*num_buffers, min);
797                 inst->num_output_bufs = *num_buffers;
798                 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
799                                                     inst->width,
800                                                     inst->height);
801                 sizes[0] = max(sizes[0], inst->output_buf_size);
802                 inst->output_buf_size = sizes[0];
803                 break;
804         default:
805                 ret = -EINVAL;
806                 break;
807         }
808
809         return ret;
810 }
811
812 static int venc_verify_conf(struct venus_inst *inst)
813 {
814         enum hfi_version ver = inst->core->res->hfi_version;
815         struct hfi_buffer_requirements bufreq;
816         int ret;
817
818         if (!inst->num_input_bufs || !inst->num_output_bufs)
819                 return -EINVAL;
820
821         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
822         if (ret)
823                 return ret;
824
825         if (inst->num_output_bufs < bufreq.count_actual ||
826             inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
827                 return -EINVAL;
828
829         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
830         if (ret)
831                 return ret;
832
833         if (inst->num_input_bufs < bufreq.count_actual ||
834             inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
835                 return -EINVAL;
836
837         return 0;
838 }
839
840 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
841 {
842         struct venus_inst *inst = vb2_get_drv_priv(q);
843         int ret;
844
845         mutex_lock(&inst->lock);
846
847         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
848                 inst->streamon_out = 1;
849         else
850                 inst->streamon_cap = 1;
851
852         if (!(inst->streamon_out & inst->streamon_cap)) {
853                 mutex_unlock(&inst->lock);
854                 return 0;
855         }
856
857         venus_helper_init_instance(inst);
858
859         inst->sequence_cap = 0;
860         inst->sequence_out = 0;
861
862         ret = venc_init_session(inst);
863         if (ret)
864                 goto bufs_done;
865
866         ret = venus_pm_acquire_core(inst);
867         if (ret)
868                 goto deinit_sess;
869
870         ret = venc_set_properties(inst);
871         if (ret)
872                 goto deinit_sess;
873
874         ret = venc_verify_conf(inst);
875         if (ret)
876                 goto deinit_sess;
877
878         ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
879                                         inst->num_output_bufs, 0);
880         if (ret)
881                 goto deinit_sess;
882
883         ret = venus_helper_vb2_start_streaming(inst);
884         if (ret)
885                 goto deinit_sess;
886
887         mutex_unlock(&inst->lock);
888
889         return 0;
890
891 deinit_sess:
892         hfi_session_deinit(inst);
893 bufs_done:
894         venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
895         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
896                 inst->streamon_out = 0;
897         else
898                 inst->streamon_cap = 0;
899         mutex_unlock(&inst->lock);
900         return ret;
901 }
902
903 static const struct vb2_ops venc_vb2_ops = {
904         .queue_setup = venc_queue_setup,
905         .buf_init = venus_helper_vb2_buf_init,
906         .buf_prepare = venus_helper_vb2_buf_prepare,
907         .start_streaming = venc_start_streaming,
908         .stop_streaming = venus_helper_vb2_stop_streaming,
909         .buf_queue = venus_helper_vb2_buf_queue,
910 };
911
912 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
913                           u32 tag, u32 bytesused, u32 data_offset, u32 flags,
914                           u32 hfi_flags, u64 timestamp_us)
915 {
916         struct vb2_v4l2_buffer *vbuf;
917         struct vb2_buffer *vb;
918         unsigned int type;
919
920         if (buf_type == HFI_BUFFER_INPUT)
921                 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
922         else
923                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
924
925         vbuf = venus_helper_find_buf(inst, type, tag);
926         if (!vbuf)
927                 return;
928
929         vbuf->flags = flags;
930
931         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
932                 vb = &vbuf->vb2_buf;
933                 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
934                 vb->planes[0].data_offset = data_offset;
935                 vb->timestamp = timestamp_us * NSEC_PER_USEC;
936                 vbuf->sequence = inst->sequence_cap++;
937         } else {
938                 vbuf->sequence = inst->sequence_out++;
939         }
940
941         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
942 }
943
944 static void venc_event_notify(struct venus_inst *inst, u32 event,
945                               struct hfi_event_data *data)
946 {
947         struct device *dev = inst->core->dev_enc;
948
949         if (event == EVT_SESSION_ERROR) {
950                 inst->session_error = true;
951                 dev_err(dev, "enc: event session error %x\n", inst->error);
952         }
953 }
954
955 static const struct hfi_inst_ops venc_hfi_ops = {
956         .buf_done = venc_buf_done,
957         .event_notify = venc_event_notify,
958 };
959
960 static const struct v4l2_m2m_ops venc_m2m_ops = {
961         .device_run = venus_helper_m2m_device_run,
962         .job_abort = venus_helper_m2m_job_abort,
963 };
964
965 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
966                           struct vb2_queue *dst_vq)
967 {
968         struct venus_inst *inst = priv;
969         int ret;
970
971         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
972         src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
973         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
974         src_vq->ops = &venc_vb2_ops;
975         src_vq->mem_ops = &vb2_dma_sg_memops;
976         src_vq->drv_priv = inst;
977         src_vq->buf_struct_size = sizeof(struct venus_buffer);
978         src_vq->allow_zero_bytesused = 1;
979         src_vq->min_buffers_needed = 1;
980         src_vq->dev = inst->core->dev;
981         if (inst->core->res->hfi_version == HFI_VERSION_1XX)
982                 src_vq->bidirectional = 1;
983         ret = vb2_queue_init(src_vq);
984         if (ret)
985                 return ret;
986
987         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
988         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
989         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
990         dst_vq->ops = &venc_vb2_ops;
991         dst_vq->mem_ops = &vb2_dma_sg_memops;
992         dst_vq->drv_priv = inst;
993         dst_vq->buf_struct_size = sizeof(struct venus_buffer);
994         dst_vq->allow_zero_bytesused = 1;
995         dst_vq->min_buffers_needed = 1;
996         dst_vq->dev = inst->core->dev;
997         return vb2_queue_init(dst_vq);
998 }
999
1000 static void venc_inst_init(struct venus_inst *inst)
1001 {
1002         inst->fmt_cap = &venc_formats[2];
1003         inst->fmt_out = &venc_formats[0];
1004         inst->width = 1280;
1005         inst->height = ALIGN(720, 32);
1006         inst->out_width = 1280;
1007         inst->out_height = 720;
1008         inst->fps = 15;
1009         inst->timeperframe.numerator = 1;
1010         inst->timeperframe.denominator = 15;
1011         inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1012 }
1013
1014 static int venc_open(struct file *file)
1015 {
1016         struct venus_core *core = video_drvdata(file);
1017         struct venus_inst *inst;
1018         int ret;
1019
1020         inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1021         if (!inst)
1022                 return -ENOMEM;
1023
1024         INIT_LIST_HEAD(&inst->dpbbufs);
1025         INIT_LIST_HEAD(&inst->registeredbufs);
1026         INIT_LIST_HEAD(&inst->internalbufs);
1027         INIT_LIST_HEAD(&inst->list);
1028         mutex_init(&inst->lock);
1029
1030         inst->core = core;
1031         inst->session_type = VIDC_SESSION_TYPE_ENC;
1032         inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1033         inst->core_acquired = false;
1034
1035         venus_helper_init_instance(inst);
1036
1037         ret = pm_runtime_get_sync(core->dev_enc);
1038         if (ret < 0)
1039                 goto err_put_sync;
1040
1041         ret = venc_ctrl_init(inst);
1042         if (ret)
1043                 goto err_put_sync;
1044
1045         ret = hfi_session_create(inst, &venc_hfi_ops);
1046         if (ret)
1047                 goto err_ctrl_deinit;
1048
1049         venc_inst_init(inst);
1050
1051         /*
1052          * create m2m device for every instance, the m2m context scheduling
1053          * is made by firmware side so we do not need to care about.
1054          */
1055         inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1056         if (IS_ERR(inst->m2m_dev)) {
1057                 ret = PTR_ERR(inst->m2m_dev);
1058                 goto err_session_destroy;
1059         }
1060
1061         inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1062         if (IS_ERR(inst->m2m_ctx)) {
1063                 ret = PTR_ERR(inst->m2m_ctx);
1064                 goto err_m2m_release;
1065         }
1066
1067         v4l2_fh_init(&inst->fh, core->vdev_enc);
1068
1069         inst->fh.ctrl_handler = &inst->ctrl_handler;
1070         v4l2_fh_add(&inst->fh);
1071         inst->fh.m2m_ctx = inst->m2m_ctx;
1072         file->private_data = &inst->fh;
1073
1074         return 0;
1075
1076 err_m2m_release:
1077         v4l2_m2m_release(inst->m2m_dev);
1078 err_session_destroy:
1079         hfi_session_destroy(inst);
1080 err_ctrl_deinit:
1081         venc_ctrl_deinit(inst);
1082 err_put_sync:
1083         pm_runtime_put_sync(core->dev_enc);
1084         kfree(inst);
1085         return ret;
1086 }
1087
1088 static int venc_close(struct file *file)
1089 {
1090         struct venus_inst *inst = to_inst(file);
1091
1092         v4l2_m2m_ctx_release(inst->m2m_ctx);
1093         v4l2_m2m_release(inst->m2m_dev);
1094         venc_ctrl_deinit(inst);
1095         hfi_session_destroy(inst);
1096         mutex_destroy(&inst->lock);
1097         v4l2_fh_del(&inst->fh);
1098         v4l2_fh_exit(&inst->fh);
1099
1100         pm_runtime_put_sync(inst->core->dev_enc);
1101
1102         kfree(inst);
1103         return 0;
1104 }
1105
1106 static const struct v4l2_file_operations venc_fops = {
1107         .owner = THIS_MODULE,
1108         .open = venc_open,
1109         .release = venc_close,
1110         .unlocked_ioctl = video_ioctl2,
1111         .poll = v4l2_m2m_fop_poll,
1112         .mmap = v4l2_m2m_fop_mmap,
1113 };
1114
1115 static int venc_probe(struct platform_device *pdev)
1116 {
1117         struct device *dev = &pdev->dev;
1118         struct video_device *vdev;
1119         struct venus_core *core;
1120         int ret;
1121
1122         if (!dev->parent)
1123                 return -EPROBE_DEFER;
1124
1125         core = dev_get_drvdata(dev->parent);
1126         if (!core)
1127                 return -EPROBE_DEFER;
1128
1129         platform_set_drvdata(pdev, core);
1130
1131         if (core->pm_ops->venc_get) {
1132                 ret = core->pm_ops->venc_get(dev);
1133                 if (ret)
1134                         return ret;
1135         }
1136
1137         vdev = video_device_alloc();
1138         if (!vdev)
1139                 return -ENOMEM;
1140
1141         strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1142         vdev->release = video_device_release;
1143         vdev->fops = &venc_fops;
1144         vdev->ioctl_ops = &venc_ioctl_ops;
1145         vdev->vfl_dir = VFL_DIR_M2M;
1146         vdev->v4l2_dev = &core->v4l2_dev;
1147         vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1148
1149         ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1150         if (ret)
1151                 goto err_vdev_release;
1152
1153         core->vdev_enc = vdev;
1154         core->dev_enc = dev;
1155
1156         video_set_drvdata(vdev, core);
1157         pm_runtime_enable(dev);
1158
1159         return 0;
1160
1161 err_vdev_release:
1162         video_device_release(vdev);
1163         return ret;
1164 }
1165
1166 static int venc_remove(struct platform_device *pdev)
1167 {
1168         struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1169
1170         video_unregister_device(core->vdev_enc);
1171         pm_runtime_disable(core->dev_enc);
1172
1173         if (core->pm_ops->venc_put)
1174                 core->pm_ops->venc_put(core->dev_enc);
1175
1176         return 0;
1177 }
1178
1179 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1180 {
1181         struct venus_core *core = dev_get_drvdata(dev);
1182         const struct venus_pm_ops *pm_ops = core->pm_ops;
1183         int ret = 0;
1184
1185         if (pm_ops->venc_power)
1186                 ret = pm_ops->venc_power(dev, POWER_OFF);
1187
1188         return ret;
1189 }
1190
1191 static __maybe_unused int venc_runtime_resume(struct device *dev)
1192 {
1193         struct venus_core *core = dev_get_drvdata(dev);
1194         const struct venus_pm_ops *pm_ops = core->pm_ops;
1195         int ret = 0;
1196
1197         if (pm_ops->venc_power)
1198                 ret = pm_ops->venc_power(dev, POWER_ON);
1199
1200         return ret;
1201 }
1202
1203 static const struct dev_pm_ops venc_pm_ops = {
1204         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1205                                 pm_runtime_force_resume)
1206         SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1207 };
1208
1209 static const struct of_device_id venc_dt_match[] = {
1210         { .compatible = "venus-encoder" },
1211         { }
1212 };
1213 MODULE_DEVICE_TABLE(of, venc_dt_match);
1214
1215 static struct platform_driver qcom_venus_enc_driver = {
1216         .probe = venc_probe,
1217         .remove = venc_remove,
1218         .driver = {
1219                 .name = "qcom-venus-encoder",
1220                 .of_match_table = venc_dt_match,
1221                 .pm = &venc_pm_ops,
1222         },
1223 };
1224 module_platform_driver(qcom_venus_enc_driver);
1225
1226 MODULE_ALIAS("platform:qcom-venus-encoder");
1227 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1228 MODULE_LICENSE("GPL v2");