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/platform_device.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-mem2mem.h>
12 #include <media/videobuf2-dma-contig.h>
13 #include <media/v4l2-ioctl.h>
14 #include <media/v4l2-event.h>
15 #include <media/v4l2-ctrls.h>
16
17 #include "hfi_venus_io.h"
18 #include "hfi_parser.h"
19 #include "core.h"
20 #include "helpers.h"
21 #include "venc.h"
22 #include "pm_helpers.h"
23
24 #define NUM_B_FRAMES_MAX 4
25
26 /*
27 * Three resons to keep MPLANE formats (despite that the number of planes
28 * currently is one):
29 * - the MPLANE formats allow only one plane to be used
30 * - the downstream driver use MPLANE formats too
31 * - future firmware versions could add support for >1 planes
32 */
33 static const struct venus_format venc_formats[] = {
34 [VENUS_FMT_NV12] = {
35 .pixfmt = V4L2_PIX_FMT_NV12,
36 .num_planes = 1,
37 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
38 },
39 [VENUS_FMT_H264] = {
40 .pixfmt = V4L2_PIX_FMT_H264,
41 .num_planes = 1,
42 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
43 },
44 [VENUS_FMT_VP8] = {
45 .pixfmt = V4L2_PIX_FMT_VP8,
46 .num_planes = 1,
47 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
48 },
49 [VENUS_FMT_HEVC] = {
50 .pixfmt = V4L2_PIX_FMT_HEVC,
51 .num_planes = 1,
52 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
53 },
54 [VENUS_FMT_MPEG4] = {
55 .pixfmt = V4L2_PIX_FMT_MPEG4,
56 .num_planes = 1,
57 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
58 },
59 [VENUS_FMT_H263] = {
60 .pixfmt = V4L2_PIX_FMT_H263,
61 .num_planes = 1,
62 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
63 },
64 };
65
66 static const struct venus_format *
find_format(struct venus_inst * inst,u32 pixfmt,u32 type)67 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
68 {
69 const struct venus_format *fmt = venc_formats;
70 unsigned int size = ARRAY_SIZE(venc_formats);
71 unsigned int i;
72
73 for (i = 0; i < size; i++) {
74 if (fmt[i].pixfmt == pixfmt)
75 break;
76 }
77
78 if (i == size || fmt[i].type != type)
79 return NULL;
80
81 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
82 !venus_helper_check_codec(inst, fmt[i].pixfmt))
83 return NULL;
84
85 return &fmt[i];
86 }
87
88 static const struct venus_format *
find_format_by_index(struct venus_inst * inst,unsigned int index,u32 type)89 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
90 {
91 const struct venus_format *fmt = venc_formats;
92 unsigned int size = ARRAY_SIZE(venc_formats);
93 unsigned int i, k = 0;
94
95 if (index > size)
96 return NULL;
97
98 for (i = 0; i < size; i++) {
99 bool valid;
100
101 if (fmt[i].type != type)
102 continue;
103 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
104 venus_helper_check_codec(inst, fmt[i].pixfmt);
105 if (k == index && valid)
106 break;
107 if (valid)
108 k++;
109 }
110
111 if (i == size)
112 return NULL;
113
114 return &fmt[i];
115 }
116
venc_v4l2_to_hfi(int id,int value)117 static int venc_v4l2_to_hfi(int id, int value)
118 {
119 switch (id) {
120 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
121 switch (value) {
122 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
123 default:
124 return HFI_H264_ENTROPY_CAVLC;
125 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
126 return HFI_H264_ENTROPY_CABAC;
127 }
128 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
129 switch (value) {
130 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
131 default:
132 return HFI_H264_DB_MODE_ALL_BOUNDARY;
133 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
134 return HFI_H264_DB_MODE_DISABLE;
135 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
136 return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
137 }
138 }
139
140 return 0;
141 }
142
143 static int
venc_querycap(struct file * file,void * fh,struct v4l2_capability * cap)144 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
145 {
146 struct venus_inst *inst = to_inst(file);
147 struct venus_core *core = inst->core;
148
149 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
150 strscpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
151 snprintf(cap->bus_info, sizeof(cap->bus_info),
152 "plat:%s:enc", dev_name(core->dev));
153
154 return 0;
155 }
156
venc_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)157 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
158 {
159 struct venus_inst *inst = to_inst(file);
160 const struct venus_format *fmt;
161
162 fmt = find_format_by_index(inst, f->index, f->type);
163
164 memset(f->reserved, 0, sizeof(f->reserved));
165
166 if (!fmt)
167 return -EINVAL;
168
169 f->pixelformat = fmt->pixfmt;
170
171 return 0;
172 }
173
174 static const struct venus_format *
venc_try_fmt_common(struct venus_inst * inst,struct v4l2_format * f)175 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
176 {
177 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
178 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
179 const struct venus_format *fmt;
180 u32 sizeimage;
181
182 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
183 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
184
185 fmt = find_format(inst, pixmp->pixelformat, f->type);
186 if (!fmt) {
187 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
188 pixmp->pixelformat = V4L2_PIX_FMT_H264;
189 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
190 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
191 else
192 return NULL;
193 fmt = find_format(inst, pixmp->pixelformat, f->type);
194 if (!fmt)
195 return NULL;
196 }
197
198 pixmp->width = clamp(pixmp->width, frame_width_min(inst),
199 frame_width_max(inst));
200 pixmp->height = clamp(pixmp->height, frame_height_min(inst),
201 frame_height_max(inst));
202
203 pixmp->width = ALIGN(pixmp->width, 128);
204 pixmp->height = ALIGN(pixmp->height, 32);
205
206 pixmp->width = ALIGN(pixmp->width, 2);
207 pixmp->height = ALIGN(pixmp->height, 2);
208
209 if (pixmp->field == V4L2_FIELD_ANY)
210 pixmp->field = V4L2_FIELD_NONE;
211 pixmp->num_planes = fmt->num_planes;
212 pixmp->flags = 0;
213
214 sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
215 pixmp->width,
216 pixmp->height);
217 pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage);
218
219 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
220 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
221 else
222 pfmt[0].bytesperline = 0;
223
224 return fmt;
225 }
226
venc_try_fmt(struct file * file,void * fh,struct v4l2_format * f)227 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
228 {
229 struct venus_inst *inst = to_inst(file);
230
231 venc_try_fmt_common(inst, f);
232
233 return 0;
234 }
235
venc_s_fmt(struct file * file,void * fh,struct v4l2_format * f)236 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
237 {
238 struct venus_inst *inst = to_inst(file);
239 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
240 struct v4l2_pix_format_mplane orig_pixmp;
241 const struct venus_format *fmt;
242 struct v4l2_format format;
243 u32 pixfmt_out = 0, pixfmt_cap = 0;
244 struct vb2_queue *q;
245
246 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
247
248 if (vb2_is_busy(q))
249 return -EBUSY;
250
251 orig_pixmp = *pixmp;
252
253 fmt = venc_try_fmt_common(inst, f);
254 if (!fmt)
255 return -EINVAL;
256
257 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
258 pixfmt_out = pixmp->pixelformat;
259 pixfmt_cap = inst->fmt_cap->pixfmt;
260 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
261 pixfmt_cap = pixmp->pixelformat;
262 pixfmt_out = inst->fmt_out->pixfmt;
263 }
264
265 memset(&format, 0, sizeof(format));
266
267 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
268 format.fmt.pix_mp.pixelformat = pixfmt_out;
269 format.fmt.pix_mp.width = orig_pixmp.width;
270 format.fmt.pix_mp.height = orig_pixmp.height;
271 venc_try_fmt_common(inst, &format);
272
273 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
274 inst->out_width = format.fmt.pix_mp.width;
275 inst->out_height = format.fmt.pix_mp.height;
276 inst->colorspace = pixmp->colorspace;
277 inst->ycbcr_enc = pixmp->ycbcr_enc;
278 inst->quantization = pixmp->quantization;
279 inst->xfer_func = pixmp->xfer_func;
280 }
281
282 memset(&format, 0, sizeof(format));
283
284 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
285 format.fmt.pix_mp.pixelformat = pixfmt_cap;
286 format.fmt.pix_mp.width = orig_pixmp.width;
287 format.fmt.pix_mp.height = orig_pixmp.height;
288 venc_try_fmt_common(inst, &format);
289
290 inst->width = format.fmt.pix_mp.width;
291 inst->height = format.fmt.pix_mp.height;
292
293 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
294 inst->fmt_out = fmt;
295 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
296 inst->fmt_cap = fmt;
297 inst->output_buf_size = pixmp->plane_fmt[0].sizeimage;
298 }
299
300 return 0;
301 }
302
venc_g_fmt(struct file * file,void * fh,struct v4l2_format * f)303 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
304 {
305 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
306 struct venus_inst *inst = to_inst(file);
307 const struct venus_format *fmt;
308
309 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
310 fmt = inst->fmt_cap;
311 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
312 fmt = inst->fmt_out;
313 else
314 return -EINVAL;
315
316 pixmp->pixelformat = fmt->pixfmt;
317
318 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
319 pixmp->width = inst->width;
320 pixmp->height = inst->height;
321 pixmp->colorspace = inst->colorspace;
322 pixmp->ycbcr_enc = inst->ycbcr_enc;
323 pixmp->quantization = inst->quantization;
324 pixmp->xfer_func = inst->xfer_func;
325 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
326 pixmp->width = inst->out_width;
327 pixmp->height = inst->out_height;
328 }
329
330 venc_try_fmt_common(inst, f);
331
332 return 0;
333 }
334
335 static int
venc_g_selection(struct file * file,void * fh,struct v4l2_selection * s)336 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
337 {
338 struct venus_inst *inst = to_inst(file);
339
340 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
341 return -EINVAL;
342
343 switch (s->target) {
344 case V4L2_SEL_TGT_CROP_DEFAULT:
345 case V4L2_SEL_TGT_CROP_BOUNDS:
346 s->r.width = inst->out_width;
347 s->r.height = inst->out_height;
348 break;
349 case V4L2_SEL_TGT_CROP:
350 s->r.width = inst->width;
351 s->r.height = inst->height;
352 break;
353 default:
354 return -EINVAL;
355 }
356
357 s->r.top = 0;
358 s->r.left = 0;
359
360 return 0;
361 }
362
363 static int
venc_s_selection(struct file * file,void * fh,struct v4l2_selection * s)364 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
365 {
366 struct venus_inst *inst = to_inst(file);
367
368 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
369 return -EINVAL;
370
371 if (s->r.width > inst->out_width ||
372 s->r.height > inst->out_height)
373 return -EINVAL;
374
375 s->r.width = ALIGN(s->r.width, 2);
376 s->r.height = ALIGN(s->r.height, 2);
377
378 switch (s->target) {
379 case V4L2_SEL_TGT_CROP:
380 s->r.top = 0;
381 s->r.left = 0;
382 inst->width = s->r.width;
383 inst->height = s->r.height;
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 return 0;
390 }
391
venc_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)392 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
393 {
394 struct venus_inst *inst = to_inst(file);
395 struct v4l2_outputparm *out = &a->parm.output;
396 struct v4l2_fract *timeperframe = &out->timeperframe;
397 u64 us_per_frame, fps;
398
399 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
400 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
401 return -EINVAL;
402
403 memset(out->reserved, 0, sizeof(out->reserved));
404
405 if (!timeperframe->denominator)
406 timeperframe->denominator = inst->timeperframe.denominator;
407 if (!timeperframe->numerator)
408 timeperframe->numerator = inst->timeperframe.numerator;
409
410 out->capability = V4L2_CAP_TIMEPERFRAME;
411
412 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
413 do_div(us_per_frame, timeperframe->denominator);
414
415 us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC);
416 fps = USEC_PER_SEC / (u32)us_per_frame;
417 fps = min(VENUS_MAX_FPS, fps);
418
419 inst->timeperframe = *timeperframe;
420 inst->fps = fps;
421
422 return 0;
423 }
424
venc_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)425 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
426 {
427 struct venus_inst *inst = to_inst(file);
428
429 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
430 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
431 return -EINVAL;
432
433 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
434 a->parm.output.timeperframe = inst->timeperframe;
435
436 return 0;
437 }
438
venc_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)439 static int venc_enum_framesizes(struct file *file, void *fh,
440 struct v4l2_frmsizeenum *fsize)
441 {
442 struct venus_inst *inst = to_inst(file);
443 const struct venus_format *fmt;
444
445 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
446
447 fmt = find_format(inst, fsize->pixel_format,
448 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
449 if (!fmt) {
450 fmt = find_format(inst, fsize->pixel_format,
451 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
452 if (!fmt)
453 return -EINVAL;
454 }
455
456 if (fsize->index)
457 return -EINVAL;
458
459 fsize->stepwise.min_width = frame_width_min(inst);
460 fsize->stepwise.max_width = frame_width_max(inst);
461 fsize->stepwise.step_width = frame_width_step(inst);
462 fsize->stepwise.min_height = frame_height_min(inst);
463 fsize->stepwise.max_height = frame_height_max(inst);
464 fsize->stepwise.step_height = frame_height_step(inst);
465
466 return 0;
467 }
468
venc_enum_frameintervals(struct file * file,void * fh,struct v4l2_frmivalenum * fival)469 static int venc_enum_frameintervals(struct file *file, void *fh,
470 struct v4l2_frmivalenum *fival)
471 {
472 struct venus_inst *inst = to_inst(file);
473 const struct venus_format *fmt;
474 unsigned int framerate_factor = 1;
475
476 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
477
478 fmt = find_format(inst, fival->pixel_format,
479 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
480 if (!fmt) {
481 fmt = find_format(inst, fival->pixel_format,
482 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
483 if (!fmt)
484 return -EINVAL;
485 }
486
487 if (fival->index)
488 return -EINVAL;
489
490 if (!fival->width || !fival->height)
491 return -EINVAL;
492
493 if (fival->width > frame_width_max(inst) ||
494 fival->width < frame_width_min(inst) ||
495 fival->height > frame_height_max(inst) ||
496 fival->height < frame_height_min(inst))
497 return -EINVAL;
498
499 if (IS_V1(inst->core)) {
500 /* framerate is reported in 1/65535 fps unit */
501 framerate_factor = (1 << 16);
502 }
503
504 fival->stepwise.min.numerator = 1;
505 fival->stepwise.min.denominator = frate_max(inst) / framerate_factor;
506 fival->stepwise.max.numerator = 1;
507 fival->stepwise.max.denominator = frate_min(inst) / framerate_factor;
508 fival->stepwise.step.numerator = 1;
509 fival->stepwise.step.denominator = frate_max(inst) / framerate_factor;
510
511 return 0;
512 }
513
venc_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)514 static int venc_subscribe_event(struct v4l2_fh *fh,
515 const struct v4l2_event_subscription *sub)
516 {
517 switch (sub->type) {
518 case V4L2_EVENT_EOS:
519 return v4l2_event_subscribe(fh, sub, 2, NULL);
520 case V4L2_EVENT_CTRL:
521 return v4l2_ctrl_subscribe_event(fh, sub);
522 default:
523 return -EINVAL;
524 }
525 }
526
527 static int
venc_encoder_cmd(struct file * file,void * fh,struct v4l2_encoder_cmd * cmd)528 venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
529 {
530 struct venus_inst *inst = to_inst(file);
531 struct hfi_frame_data fdata = {0};
532 int ret = 0;
533
534 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
535 if (ret)
536 return ret;
537
538 mutex_lock(&inst->lock);
539
540 if (cmd->cmd == V4L2_ENC_CMD_STOP &&
541 inst->enc_state == VENUS_ENC_STATE_ENCODING) {
542 /*
543 * Implement V4L2_ENC_CMD_STOP by enqueue an empty buffer on
544 * encoder input to signal EOS.
545 */
546 if (!(inst->streamon_out && inst->streamon_cap))
547 goto unlock;
548
549 fdata.buffer_type = HFI_BUFFER_INPUT;
550 fdata.flags |= HFI_BUFFERFLAG_EOS;
551 fdata.device_addr = 0xdeadb000;
552
553 ret = hfi_session_process_buf(inst, &fdata);
554
555 inst->enc_state = VENUS_ENC_STATE_DRAIN;
556 } else if (cmd->cmd == V4L2_ENC_CMD_START) {
557 if (inst->enc_state == VENUS_ENC_STATE_DRAIN) {
558 ret = -EBUSY;
559 goto unlock;
560 }
561 if (inst->enc_state == VENUS_ENC_STATE_STOPPED) {
562 vb2_clear_last_buffer_dequeued(&inst->fh.m2m_ctx->cap_q_ctx.q);
563 inst->enc_state = VENUS_ENC_STATE_ENCODING;
564 }
565 }
566
567 unlock:
568 mutex_unlock(&inst->lock);
569 return ret;
570 }
571
572 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
573 .vidioc_querycap = venc_querycap,
574 .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
575 .vidioc_enum_fmt_vid_out = venc_enum_fmt,
576 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
577 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
578 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
579 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
580 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
581 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
582 .vidioc_g_selection = venc_g_selection,
583 .vidioc_s_selection = venc_s_selection,
584 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
585 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
586 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
587 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
588 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
589 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
590 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
591 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
592 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
593 .vidioc_s_parm = venc_s_parm,
594 .vidioc_g_parm = venc_g_parm,
595 .vidioc_enum_framesizes = venc_enum_framesizes,
596 .vidioc_enum_frameintervals = venc_enum_frameintervals,
597 .vidioc_subscribe_event = venc_subscribe_event,
598 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
599 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
600 .vidioc_encoder_cmd = venc_encoder_cmd,
601 };
602
venc_pm_get(struct venus_inst * inst)603 static int venc_pm_get(struct venus_inst *inst)
604 {
605 struct venus_core *core = inst->core;
606 struct device *dev = core->dev_enc;
607 int ret;
608
609 mutex_lock(&core->pm_lock);
610 ret = pm_runtime_resume_and_get(dev);
611 mutex_unlock(&core->pm_lock);
612
613 return ret < 0 ? ret : 0;
614 }
615
venc_pm_put(struct venus_inst * inst,bool autosuspend)616 static int venc_pm_put(struct venus_inst *inst, bool autosuspend)
617 {
618 struct venus_core *core = inst->core;
619 struct device *dev = core->dev_enc;
620 int ret;
621
622 mutex_lock(&core->pm_lock);
623
624 if (autosuspend)
625 ret = pm_runtime_put_autosuspend(dev);
626 else
627 ret = pm_runtime_put_sync(dev);
628
629 mutex_unlock(&core->pm_lock);
630
631 return ret < 0 ? ret : 0;
632 }
633
venc_pm_get_put(struct venus_inst * inst)634 static int venc_pm_get_put(struct venus_inst *inst)
635 {
636 struct venus_core *core = inst->core;
637 struct device *dev = core->dev_enc;
638 int ret = 0;
639
640 mutex_lock(&core->pm_lock);
641
642 if (pm_runtime_suspended(dev)) {
643 ret = pm_runtime_resume_and_get(dev);
644 if (ret < 0)
645 goto error;
646
647 ret = pm_runtime_put_autosuspend(dev);
648 }
649
650 error:
651 mutex_unlock(&core->pm_lock);
652
653 return ret < 0 ? ret : 0;
654 }
655
venc_pm_touch(struct venus_inst * inst)656 static void venc_pm_touch(struct venus_inst *inst)
657 {
658 pm_runtime_mark_last_busy(inst->core->dev_enc);
659 }
660
venc_set_properties(struct venus_inst * inst)661 static int venc_set_properties(struct venus_inst *inst)
662 {
663 struct venc_controls *ctr = &inst->controls.enc;
664 struct hfi_intra_period intra_period;
665 struct hfi_framerate frate;
666 struct hfi_bitrate brate;
667 struct hfi_idr_period idrp;
668 struct hfi_quantization quant;
669 struct hfi_quantization_range quant_range;
670 struct hfi_quantization_range_v2 quant_range_v2;
671 struct hfi_enable en;
672 struct hfi_ltr_mode ltr_mode;
673 struct hfi_intra_refresh intra_refresh = {};
674 u32 ptype, rate_control, bitrate;
675 u32 profile, level;
676 int ret;
677
678 ret = venus_helper_set_work_mode(inst);
679 if (ret)
680 return ret;
681
682 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
683 frate.buffer_type = HFI_BUFFER_OUTPUT;
684 frate.framerate = inst->fps * (1 << 16);
685
686 ret = hfi_session_set_property(inst, ptype, &frate);
687 if (ret)
688 return ret;
689
690 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
691 struct hfi_h264_vui_timing_info info;
692 struct hfi_h264_entropy_control entropy;
693 struct hfi_h264_db_control deblock;
694 struct hfi_h264_8x8_transform h264_transform;
695
696 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
697 info.enable = 1;
698 info.fixed_framerate = 1;
699 info.time_scale = NSEC_PER_SEC;
700
701 ret = hfi_session_set_property(inst, ptype, &info);
702 if (ret)
703 return ret;
704
705 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
706 entropy.entropy_mode = venc_v4l2_to_hfi(
707 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
708 ctr->h264_entropy_mode);
709 entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
710
711 ret = hfi_session_set_property(inst, ptype, &entropy);
712 if (ret)
713 return ret;
714
715 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
716 deblock.mode = venc_v4l2_to_hfi(
717 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
718 ctr->h264_loop_filter_mode);
719 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
720 deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
721
722 ret = hfi_session_set_property(inst, ptype, &deblock);
723 if (ret)
724 return ret;
725
726 ptype = HFI_PROPERTY_PARAM_VENC_H264_TRANSFORM_8X8;
727 h264_transform.enable_type = 0;
728 if (ctr->profile.h264 == V4L2_MPEG_VIDEO_H264_PROFILE_HIGH ||
729 ctr->profile.h264 == V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH)
730 h264_transform.enable_type = ctr->h264_8x8_transform;
731
732 ret = hfi_session_set_property(inst, ptype, &h264_transform);
733 if (ret)
734 return ret;
735
736 if (ctr->layer_bitrate) {
737 unsigned int i;
738
739 ptype = HFI_PROPERTY_PARAM_VENC_HIER_P_MAX_NUM_ENH_LAYER;
740 ret = hfi_session_set_property(inst, ptype, &ctr->h264_hier_layers);
741 if (ret)
742 return ret;
743
744 ptype = HFI_PROPERTY_CONFIG_VENC_HIER_P_ENH_LAYER;
745 ret = hfi_session_set_property(inst, ptype, &ctr->layer_bitrate);
746 if (ret)
747 return ret;
748
749 for (i = 0; i < ctr->h264_hier_layers; ++i) {
750 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
751 brate.bitrate = ctr->h264_hier_layer_bitrate[i];
752 brate.layer_id = i;
753
754 ret = hfi_session_set_property(inst, ptype, &brate);
755 if (ret)
756 return ret;
757 }
758 }
759 }
760
761 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
762 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
763 /* IDR periodicity, n:
764 * n = 0 - only the first I-frame is IDR frame
765 * n = 1 - all I-frames will be IDR frames
766 * n > 1 - every n-th I-frame will be IDR frame
767 */
768 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
769 idrp.idr_period = 0;
770 ret = hfi_session_set_property(inst, ptype, &idrp);
771 if (ret)
772 return ret;
773 }
774
775 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC &&
776 ctr->profile.hevc == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10) {
777 struct hfi_hdr10_pq_sei hdr10;
778 unsigned int c;
779
780 ptype = HFI_PROPERTY_PARAM_VENC_HDR10_PQ_SEI;
781
782 for (c = 0; c < 3; c++) {
783 hdr10.mastering.display_primaries_x[c] =
784 ctr->mastering.display_primaries_x[c];
785 hdr10.mastering.display_primaries_y[c] =
786 ctr->mastering.display_primaries_y[c];
787 }
788
789 hdr10.mastering.white_point_x = ctr->mastering.white_point_x;
790 hdr10.mastering.white_point_y = ctr->mastering.white_point_y;
791 hdr10.mastering.max_display_mastering_luminance =
792 ctr->mastering.max_display_mastering_luminance;
793 hdr10.mastering.min_display_mastering_luminance =
794 ctr->mastering.min_display_mastering_luminance;
795
796 hdr10.cll.max_content_light = ctr->cll.max_content_light_level;
797 hdr10.cll.max_pic_average_light =
798 ctr->cll.max_pic_average_light_level;
799
800 ret = hfi_session_set_property(inst, ptype, &hdr10);
801 if (ret)
802 return ret;
803 }
804
805 if (ctr->num_b_frames) {
806 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
807
808 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
809 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
810 if (ret)
811 return ret;
812 }
813
814 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
815 intra_period.pframes = ctr->num_p_frames;
816 intra_period.bframes = ctr->num_b_frames;
817
818 ret = hfi_session_set_property(inst, ptype, &intra_period);
819 if (ret)
820 return ret;
821
822 if (!ctr->rc_enable)
823 rate_control = HFI_RATE_CONTROL_OFF;
824 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
825 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR :
826 HFI_RATE_CONTROL_VBR_CFR;
827 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
828 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR :
829 HFI_RATE_CONTROL_CBR_CFR;
830 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
831 rate_control = HFI_RATE_CONTROL_CQ;
832
833 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
834 ret = hfi_session_set_property(inst, ptype, &rate_control);
835 if (ret)
836 return ret;
837
838 if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) {
839 struct hfi_heic_frame_quality quality = {};
840
841 ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY;
842 quality.frame_quality = ctr->const_quality;
843 ret = hfi_session_set_property(inst, ptype, &quality);
844 if (ret)
845 return ret;
846 }
847
848 if (!ctr->layer_bitrate) {
849 if (!ctr->bitrate)
850 bitrate = 64000;
851 else
852 bitrate = ctr->bitrate;
853
854 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
855 brate.bitrate = bitrate;
856 brate.layer_id = 0;
857
858 ret = hfi_session_set_property(inst, ptype, &brate);
859 if (ret)
860 return ret;
861
862 if (!ctr->bitrate_peak)
863 bitrate *= 2;
864 else
865 bitrate = ctr->bitrate_peak;
866
867 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
868 brate.bitrate = bitrate;
869 brate.layer_id = 0;
870
871 ret = hfi_session_set_property(inst, ptype, &brate);
872 if (ret)
873 return ret;
874 }
875
876 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
877 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
878 ptype = HFI_PROPERTY_CONFIG_VENC_SYNC_FRAME_SEQUENCE_HEADER;
879 if (ctr->header_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)
880 en.enable = 0;
881 else
882 en.enable = 1;
883
884 ret = hfi_session_set_property(inst, ptype, &en);
885 if (ret)
886 return ret;
887 }
888
889 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
890 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
891 quant.qp_i = ctr->hevc_i_qp;
892 quant.qp_p = ctr->hevc_p_qp;
893 quant.qp_b = ctr->hevc_b_qp;
894 } else {
895 quant.qp_i = ctr->h264_i_qp;
896 quant.qp_p = ctr->h264_p_qp;
897 quant.qp_b = ctr->h264_b_qp;
898 }
899 quant.layer_id = 0;
900 ret = hfi_session_set_property(inst, ptype, &quant);
901 if (ret)
902 return ret;
903
904 if (inst->core->res->hfi_version == HFI_VERSION_4XX ||
905 inst->core->res->hfi_version == HFI_VERSION_6XX) {
906 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE_V2;
907
908 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
909 quant_range_v2.min_qp.qp_packed = ctr->hevc_min_qp;
910 quant_range_v2.max_qp.qp_packed = ctr->hevc_max_qp;
911 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
912 quant_range_v2.min_qp.qp_packed = ctr->vp8_min_qp;
913 quant_range_v2.max_qp.qp_packed = ctr->vp8_max_qp;
914 } else {
915 quant_range_v2.min_qp.qp_packed = ctr->h264_min_qp;
916 quant_range_v2.max_qp.qp_packed = ctr->h264_max_qp;
917 }
918
919 ret = hfi_session_set_property(inst, ptype, &quant_range_v2);
920 } else {
921 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
922
923 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
924 quant_range.min_qp = ctr->hevc_min_qp;
925 quant_range.max_qp = ctr->hevc_max_qp;
926 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
927 quant_range.min_qp = ctr->vp8_min_qp;
928 quant_range.max_qp = ctr->vp8_max_qp;
929 } else {
930 quant_range.min_qp = ctr->h264_min_qp;
931 quant_range.max_qp = ctr->h264_max_qp;
932 }
933
934 quant_range.layer_id = 0;
935 ret = hfi_session_set_property(inst, ptype, &quant_range);
936 }
937
938 if (ret)
939 return ret;
940
941 ptype = HFI_PROPERTY_PARAM_VENC_LTRMODE;
942 ltr_mode.ltr_count = ctr->ltr_count;
943 ltr_mode.ltr_mode = HFI_LTR_MODE_MANUAL;
944 ltr_mode.trust_mode = 1;
945 ret = hfi_session_set_property(inst, ptype, <r_mode);
946 if (ret)
947 return ret;
948
949 switch (inst->hfi_codec) {
950 case HFI_VIDEO_CODEC_H264:
951 profile = ctr->profile.h264;
952 level = ctr->level.h264;
953 break;
954 case HFI_VIDEO_CODEC_MPEG4:
955 profile = ctr->profile.mpeg4;
956 level = ctr->level.mpeg4;
957 break;
958 case HFI_VIDEO_CODEC_VP8:
959 profile = ctr->profile.vp8;
960 level = 0;
961 break;
962 case HFI_VIDEO_CODEC_VP9:
963 profile = ctr->profile.vp9;
964 level = ctr->level.vp9;
965 break;
966 case HFI_VIDEO_CODEC_HEVC:
967 profile = ctr->profile.hevc;
968 level = ctr->level.hevc;
969 break;
970 case HFI_VIDEO_CODEC_MPEG2:
971 default:
972 profile = 0;
973 level = 0;
974 break;
975 }
976
977 ret = venus_helper_set_profile_level(inst, profile, level);
978 if (ret)
979 return ret;
980
981 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
982 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
983 struct hfi_enable en = {};
984
985 ptype = HFI_PROPERTY_PARAM_VENC_H264_GENERATE_AUDNAL;
986
987 if (ctr->aud_enable)
988 en.enable = 1;
989
990 ret = hfi_session_set_property(inst, ptype, &en);
991 }
992
993 if ((inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
994 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) &&
995 (rate_control == HFI_RATE_CONTROL_CBR_VFR ||
996 rate_control == HFI_RATE_CONTROL_CBR_CFR)) {
997 intra_refresh.mode = HFI_INTRA_REFRESH_NONE;
998 intra_refresh.cir_mbs = 0;
999
1000 if (ctr->intra_refresh_period) {
1001 u32 mbs;
1002
1003 mbs = ALIGN(inst->width, 16) * ALIGN(inst->height, 16);
1004 mbs /= 16 * 16;
1005 if (mbs % ctr->intra_refresh_period)
1006 mbs++;
1007 mbs /= ctr->intra_refresh_period;
1008
1009 intra_refresh.cir_mbs = mbs;
1010 if (ctr->intra_refresh_type ==
1011 V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC)
1012 intra_refresh.mode = HFI_INTRA_REFRESH_CYCLIC;
1013 else
1014 intra_refresh.mode = HFI_INTRA_REFRESH_RANDOM;
1015 }
1016
1017 ptype = HFI_PROPERTY_PARAM_VENC_INTRA_REFRESH;
1018
1019 ret = hfi_session_set_property(inst, ptype, &intra_refresh);
1020 if (ret)
1021 return ret;
1022 }
1023
1024 return 0;
1025 }
1026
venc_init_session(struct venus_inst * inst)1027 static int venc_init_session(struct venus_inst *inst)
1028 {
1029 int ret;
1030
1031 ret = venus_helper_session_init(inst);
1032 if (ret == -EALREADY)
1033 return 0;
1034 else if (ret)
1035 return ret;
1036
1037 ret = venus_helper_set_stride(inst, inst->out_width,
1038 inst->out_height);
1039 if (ret)
1040 goto deinit;
1041
1042 ret = venus_helper_set_input_resolution(inst, inst->width,
1043 inst->height);
1044 if (ret)
1045 goto deinit;
1046
1047 ret = venus_helper_set_output_resolution(inst, inst->width,
1048 inst->height,
1049 HFI_BUFFER_OUTPUT);
1050 if (ret)
1051 goto deinit;
1052
1053 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
1054 if (ret)
1055 goto deinit;
1056
1057 ret = venc_set_properties(inst);
1058 if (ret)
1059 goto deinit;
1060
1061 return 0;
1062 deinit:
1063 hfi_session_deinit(inst);
1064 return ret;
1065 }
1066
venc_out_num_buffers(struct venus_inst * inst,unsigned int * num)1067 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
1068 {
1069 struct hfi_buffer_requirements bufreq;
1070 int ret;
1071
1072 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1073 if (ret)
1074 return ret;
1075
1076 *num = bufreq.count_actual;
1077
1078 return 0;
1079 }
1080
venc_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])1081 static int venc_queue_setup(struct vb2_queue *q,
1082 unsigned int *num_buffers, unsigned int *num_planes,
1083 unsigned int sizes[], struct device *alloc_devs[])
1084 {
1085 struct venus_inst *inst = vb2_get_drv_priv(q);
1086 struct venus_core *core = inst->core;
1087 unsigned int num, min = 4;
1088 int ret;
1089
1090 if (*num_planes) {
1091 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1092 *num_planes != inst->fmt_out->num_planes)
1093 return -EINVAL;
1094
1095 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1096 *num_planes != inst->fmt_cap->num_planes)
1097 return -EINVAL;
1098
1099 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1100 sizes[0] < inst->input_buf_size)
1101 return -EINVAL;
1102
1103 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1104 sizes[0] < inst->output_buf_size)
1105 return -EINVAL;
1106
1107 return 0;
1108 }
1109
1110 if (test_bit(0, &core->sys_error)) {
1111 if (inst->nonblock)
1112 return -EAGAIN;
1113
1114 ret = wait_event_interruptible(core->sys_err_done,
1115 !test_bit(0, &core->sys_error));
1116 if (ret)
1117 return ret;
1118 }
1119
1120 ret = venc_pm_get(inst);
1121 if (ret)
1122 return ret;
1123
1124 mutex_lock(&inst->lock);
1125 ret = venc_init_session(inst);
1126 mutex_unlock(&inst->lock);
1127
1128 if (ret)
1129 goto put_power;
1130
1131 ret = venc_pm_put(inst, false);
1132 if (ret)
1133 return ret;
1134
1135 switch (q->type) {
1136 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1137 *num_planes = inst->fmt_out->num_planes;
1138
1139 ret = venc_out_num_buffers(inst, &num);
1140 if (ret)
1141 break;
1142
1143 num = max(num, min);
1144 *num_buffers = max(*num_buffers, num);
1145 inst->num_input_bufs = *num_buffers;
1146
1147 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
1148 inst->out_width,
1149 inst->out_height);
1150 inst->input_buf_size = sizes[0];
1151 break;
1152 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1153 *num_planes = inst->fmt_cap->num_planes;
1154 *num_buffers = max(*num_buffers, min);
1155 inst->num_output_bufs = *num_buffers;
1156 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
1157 inst->width,
1158 inst->height);
1159 sizes[0] = max(sizes[0], inst->output_buf_size);
1160 inst->output_buf_size = sizes[0];
1161 break;
1162 default:
1163 ret = -EINVAL;
1164 break;
1165 }
1166
1167 return ret;
1168 put_power:
1169 venc_pm_put(inst, false);
1170 return ret;
1171 }
1172
venc_buf_init(struct vb2_buffer * vb)1173 static int venc_buf_init(struct vb2_buffer *vb)
1174 {
1175 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1176
1177 inst->buf_count++;
1178
1179 return venus_helper_vb2_buf_init(vb);
1180 }
1181
venc_release_session(struct venus_inst * inst)1182 static void venc_release_session(struct venus_inst *inst)
1183 {
1184 int ret;
1185
1186 venc_pm_get(inst);
1187
1188 mutex_lock(&inst->lock);
1189
1190 ret = hfi_session_deinit(inst);
1191 if (ret || inst->session_error)
1192 hfi_session_abort(inst);
1193
1194 mutex_unlock(&inst->lock);
1195
1196 venus_pm_load_scale(inst);
1197 INIT_LIST_HEAD(&inst->registeredbufs);
1198 venus_pm_release_core(inst);
1199
1200 venc_pm_put(inst, false);
1201 }
1202
venc_buf_cleanup(struct vb2_buffer * vb)1203 static void venc_buf_cleanup(struct vb2_buffer *vb)
1204 {
1205 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1206 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1207 struct venus_buffer *buf = to_venus_buffer(vbuf);
1208
1209 mutex_lock(&inst->lock);
1210 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1211 if (!list_empty(&inst->registeredbufs))
1212 list_del_init(&buf->reg_list);
1213 mutex_unlock(&inst->lock);
1214
1215 inst->buf_count--;
1216 if (!inst->buf_count)
1217 venc_release_session(inst);
1218 }
1219
venc_verify_conf(struct venus_inst * inst)1220 static int venc_verify_conf(struct venus_inst *inst)
1221 {
1222 enum hfi_version ver = inst->core->res->hfi_version;
1223 struct hfi_buffer_requirements bufreq;
1224 int ret;
1225
1226 if (!inst->num_input_bufs || !inst->num_output_bufs)
1227 return -EINVAL;
1228
1229 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
1230 if (ret)
1231 return ret;
1232
1233 if (inst->num_output_bufs < bufreq.count_actual ||
1234 inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1235 return -EINVAL;
1236
1237 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1238 if (ret)
1239 return ret;
1240
1241 if (inst->num_input_bufs < bufreq.count_actual ||
1242 inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1243 return -EINVAL;
1244
1245 return 0;
1246 }
1247
venc_start_streaming(struct vb2_queue * q,unsigned int count)1248 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
1249 {
1250 struct venus_inst *inst = vb2_get_drv_priv(q);
1251 int ret;
1252
1253 mutex_lock(&inst->lock);
1254
1255 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1256 inst->streamon_out = 1;
1257 else
1258 inst->streamon_cap = 1;
1259
1260 if (!(inst->streamon_out & inst->streamon_cap)) {
1261 mutex_unlock(&inst->lock);
1262 return 0;
1263 }
1264
1265 venus_helper_init_instance(inst);
1266
1267 inst->sequence_cap = 0;
1268 inst->sequence_out = 0;
1269
1270 ret = venc_pm_get(inst);
1271 if (ret)
1272 goto error;
1273
1274 ret = venus_pm_acquire_core(inst);
1275 if (ret)
1276 goto put_power;
1277
1278 ret = venc_pm_put(inst, true);
1279 if (ret)
1280 goto error;
1281
1282 ret = venc_set_properties(inst);
1283 if (ret)
1284 goto error;
1285
1286 ret = venc_verify_conf(inst);
1287 if (ret)
1288 goto error;
1289
1290 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1291 inst->num_output_bufs, 0);
1292 if (ret)
1293 goto error;
1294
1295 ret = venus_helper_vb2_start_streaming(inst);
1296 if (ret)
1297 goto error;
1298
1299 inst->enc_state = VENUS_ENC_STATE_ENCODING;
1300
1301 mutex_unlock(&inst->lock);
1302
1303 return 0;
1304
1305 put_power:
1306 venc_pm_put(inst, false);
1307 error:
1308 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1309 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1310 inst->streamon_out = 0;
1311 else
1312 inst->streamon_cap = 0;
1313 mutex_unlock(&inst->lock);
1314 return ret;
1315 }
1316
venc_vb2_buf_queue(struct vb2_buffer * vb)1317 static void venc_vb2_buf_queue(struct vb2_buffer *vb)
1318 {
1319 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1320 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1321
1322 venc_pm_get_put(inst);
1323
1324 mutex_lock(&inst->lock);
1325
1326 if (inst->enc_state == VENUS_ENC_STATE_STOPPED) {
1327 vbuf->sequence = inst->sequence_cap++;
1328 vbuf->field = V4L2_FIELD_NONE;
1329 vb2_set_plane_payload(vb, 0, 0);
1330 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1331 mutex_unlock(&inst->lock);
1332 return;
1333 }
1334
1335 venus_helper_vb2_buf_queue(vb);
1336 mutex_unlock(&inst->lock);
1337 }
1338
1339 static const struct vb2_ops venc_vb2_ops = {
1340 .queue_setup = venc_queue_setup,
1341 .buf_init = venc_buf_init,
1342 .buf_cleanup = venc_buf_cleanup,
1343 .buf_prepare = venus_helper_vb2_buf_prepare,
1344 .start_streaming = venc_start_streaming,
1345 .stop_streaming = venus_helper_vb2_stop_streaming,
1346 .buf_queue = venc_vb2_buf_queue,
1347 };
1348
venc_buf_done(struct venus_inst * inst,unsigned int buf_type,u32 tag,u32 bytesused,u32 data_offset,u32 flags,u32 hfi_flags,u64 timestamp_us)1349 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1350 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1351 u32 hfi_flags, u64 timestamp_us)
1352 {
1353 struct vb2_v4l2_buffer *vbuf;
1354 struct vb2_buffer *vb;
1355 unsigned int type;
1356
1357 venc_pm_touch(inst);
1358
1359 if (buf_type == HFI_BUFFER_INPUT)
1360 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1361 else
1362 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1363
1364 vbuf = venus_helper_find_buf(inst, type, tag);
1365 if (!vbuf)
1366 return;
1367
1368 vbuf->flags = flags;
1369
1370 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1371 vb = &vbuf->vb2_buf;
1372 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1373 vb->planes[0].data_offset = data_offset;
1374 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1375 vbuf->sequence = inst->sequence_cap++;
1376 if ((vbuf->flags & V4L2_BUF_FLAG_LAST) &&
1377 inst->enc_state == VENUS_ENC_STATE_DRAIN) {
1378 inst->enc_state = VENUS_ENC_STATE_STOPPED;
1379 }
1380 } else {
1381 vbuf->sequence = inst->sequence_out++;
1382 }
1383
1384 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1385 }
1386
venc_event_notify(struct venus_inst * inst,u32 event,struct hfi_event_data * data)1387 static void venc_event_notify(struct venus_inst *inst, u32 event,
1388 struct hfi_event_data *data)
1389 {
1390 struct device *dev = inst->core->dev_enc;
1391
1392 venc_pm_touch(inst);
1393
1394 if (event == EVT_SESSION_ERROR) {
1395 inst->session_error = true;
1396 venus_helper_vb2_queue_error(inst);
1397 dev_err(dev, "enc: event session error %x\n", inst->error);
1398 }
1399 }
1400
1401 static const struct hfi_inst_ops venc_hfi_ops = {
1402 .buf_done = venc_buf_done,
1403 .event_notify = venc_event_notify,
1404 };
1405
1406 static const struct v4l2_m2m_ops venc_m2m_ops = {
1407 .device_run = venus_helper_m2m_device_run,
1408 .job_abort = venus_helper_m2m_job_abort,
1409 };
1410
m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1411 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1412 struct vb2_queue *dst_vq)
1413 {
1414 struct venus_inst *inst = priv;
1415 int ret;
1416
1417 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1418 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1419 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1420 src_vq->ops = &venc_vb2_ops;
1421 src_vq->mem_ops = &vb2_dma_contig_memops;
1422 src_vq->drv_priv = inst;
1423 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1424 src_vq->allow_zero_bytesused = 1;
1425 src_vq->min_queued_buffers = 1;
1426 src_vq->dev = inst->core->dev;
1427 src_vq->lock = &inst->ctx_q_lock;
1428 if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1429 src_vq->bidirectional = 1;
1430 ret = vb2_queue_init(src_vq);
1431 if (ret)
1432 return ret;
1433
1434 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1435 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1436 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1437 dst_vq->ops = &venc_vb2_ops;
1438 dst_vq->mem_ops = &vb2_dma_contig_memops;
1439 dst_vq->drv_priv = inst;
1440 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1441 dst_vq->allow_zero_bytesused = 1;
1442 dst_vq->min_queued_buffers = 1;
1443 dst_vq->dev = inst->core->dev;
1444 dst_vq->lock = &inst->ctx_q_lock;
1445 return vb2_queue_init(dst_vq);
1446 }
1447
venc_inst_init(struct venus_inst * inst)1448 static void venc_inst_init(struct venus_inst *inst)
1449 {
1450 inst->fmt_cap = &venc_formats[VENUS_FMT_H264];
1451 inst->fmt_out = &venc_formats[VENUS_FMT_NV12];
1452 inst->width = 1280;
1453 inst->height = ALIGN(720, 32);
1454 inst->out_width = 1280;
1455 inst->out_height = 720;
1456 inst->fps = 15;
1457 inst->timeperframe.numerator = 1;
1458 inst->timeperframe.denominator = 15;
1459 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1460 }
1461
venc_open(struct file * file)1462 static int venc_open(struct file *file)
1463 {
1464 struct venus_core *core = video_drvdata(file);
1465 struct venus_inst *inst;
1466 int ret;
1467
1468 inst = kzalloc_obj(*inst);
1469 if (!inst)
1470 return -ENOMEM;
1471
1472 INIT_LIST_HEAD(&inst->dpbbufs);
1473 INIT_LIST_HEAD(&inst->registeredbufs);
1474 INIT_LIST_HEAD(&inst->internalbufs);
1475 INIT_LIST_HEAD(&inst->list);
1476 mutex_init(&inst->lock);
1477 mutex_init(&inst->ctx_q_lock);
1478
1479 inst->core = core;
1480 inst->session_type = VIDC_SESSION_TYPE_ENC;
1481 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1482 inst->core_acquired = false;
1483 inst->nonblock = file->f_flags & O_NONBLOCK;
1484
1485 if (inst->enc_state == VENUS_ENC_STATE_DEINIT)
1486 inst->enc_state = VENUS_ENC_STATE_INIT;
1487
1488 venus_helper_init_instance(inst);
1489
1490 ret = venc_ctrl_init(inst);
1491 if (ret)
1492 goto err_free;
1493
1494 venc_inst_init(inst);
1495
1496 /*
1497 * create m2m device for every instance, the m2m context scheduling
1498 * is made by firmware side so we do not need to care about.
1499 */
1500 inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1501 if (IS_ERR(inst->m2m_dev)) {
1502 ret = PTR_ERR(inst->m2m_dev);
1503 goto err_ctrl_deinit;
1504 }
1505
1506 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1507 if (IS_ERR(inst->m2m_ctx)) {
1508 ret = PTR_ERR(inst->m2m_ctx);
1509 goto err_m2m_dev_release;
1510 }
1511
1512 ret = hfi_session_create(inst, &venc_hfi_ops);
1513 if (ret)
1514 goto err_m2m_ctx_release;
1515
1516 v4l2_fh_init(&inst->fh, core->vdev_enc);
1517
1518 inst->fh.ctrl_handler = &inst->ctrl_handler;
1519 v4l2_fh_add(&inst->fh, file);
1520 inst->fh.m2m_ctx = inst->m2m_ctx;
1521
1522 return 0;
1523
1524 err_m2m_ctx_release:
1525 v4l2_m2m_ctx_release(inst->m2m_ctx);
1526 err_m2m_dev_release:
1527 v4l2_m2m_release(inst->m2m_dev);
1528 err_ctrl_deinit:
1529 v4l2_ctrl_handler_free(&inst->ctrl_handler);
1530 err_free:
1531 kfree(inst);
1532 return ret;
1533 }
1534
venc_close(struct file * file)1535 static int venc_close(struct file *file)
1536 {
1537 struct venus_inst *inst = to_inst(file);
1538
1539 venc_pm_get(inst);
1540 venus_close_common(inst, file);
1541 inst->enc_state = VENUS_ENC_STATE_DEINIT;
1542 venc_pm_put(inst, false);
1543
1544 kfree(inst);
1545 return 0;
1546 }
1547
1548 static const struct v4l2_file_operations venc_fops = {
1549 .owner = THIS_MODULE,
1550 .open = venc_open,
1551 .release = venc_close,
1552 .unlocked_ioctl = video_ioctl2,
1553 .poll = v4l2_m2m_fop_poll,
1554 .mmap = v4l2_m2m_fop_mmap,
1555 };
1556
venc_probe(struct platform_device * pdev)1557 static int venc_probe(struct platform_device *pdev)
1558 {
1559 struct device *dev = &pdev->dev;
1560 struct video_device *vdev;
1561 struct venus_core *core;
1562 int ret;
1563
1564 core = dev_get_drvdata(dev->parent);
1565 if (!core)
1566 return -EINVAL;
1567
1568 platform_set_drvdata(pdev, core);
1569
1570 if (core->pm_ops->venc_get) {
1571 ret = core->pm_ops->venc_get(dev);
1572 if (ret)
1573 return ret;
1574 }
1575
1576 vdev = video_device_alloc();
1577 if (!vdev)
1578 return -ENOMEM;
1579
1580 strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1581 vdev->release = video_device_release;
1582 vdev->fops = &venc_fops;
1583 vdev->ioctl_ops = &venc_ioctl_ops;
1584 vdev->vfl_dir = VFL_DIR_M2M;
1585 vdev->v4l2_dev = &core->v4l2_dev;
1586 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1587
1588 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1589 if (ret)
1590 goto err_vdev_release;
1591
1592 core->vdev_enc = vdev;
1593 core->dev_enc = dev;
1594
1595 video_set_drvdata(vdev, core);
1596 pm_runtime_set_autosuspend_delay(dev, 2000);
1597 pm_runtime_use_autosuspend(dev);
1598 pm_runtime_enable(dev);
1599
1600 return 0;
1601
1602 err_vdev_release:
1603 video_device_release(vdev);
1604 return ret;
1605 }
1606
venc_remove(struct platform_device * pdev)1607 static void venc_remove(struct platform_device *pdev)
1608 {
1609 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1610
1611 video_unregister_device(core->vdev_enc);
1612 pm_runtime_disable(core->dev_enc);
1613
1614 if (core->pm_ops->venc_put)
1615 core->pm_ops->venc_put(core->dev_enc);
1616 }
1617
venc_runtime_suspend(struct device * dev)1618 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1619 {
1620 struct venus_core *core = dev_get_drvdata(dev);
1621 const struct venus_pm_ops *pm_ops = core->pm_ops;
1622 int ret = 0;
1623
1624 if (pm_ops->venc_power)
1625 ret = pm_ops->venc_power(dev, POWER_OFF);
1626
1627 return ret;
1628 }
1629
venc_runtime_resume(struct device * dev)1630 static __maybe_unused int venc_runtime_resume(struct device *dev)
1631 {
1632 struct venus_core *core = dev_get_drvdata(dev);
1633 const struct venus_pm_ops *pm_ops = core->pm_ops;
1634 int ret = 0;
1635
1636 if (pm_ops->venc_power)
1637 ret = pm_ops->venc_power(dev, POWER_ON);
1638
1639 return ret;
1640 }
1641
1642 static const struct dev_pm_ops venc_pm_ops = {
1643 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1644 pm_runtime_force_resume)
1645 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1646 };
1647
1648 static const struct of_device_id venc_dt_match[] = {
1649 { .compatible = "venus-encoder" },
1650 { }
1651 };
1652 MODULE_DEVICE_TABLE(of, venc_dt_match);
1653
1654 static struct platform_driver qcom_venus_enc_driver = {
1655 .probe = venc_probe,
1656 .remove = venc_remove,
1657 .driver = {
1658 .name = "qcom-venus-encoder",
1659 .of_match_table = venc_dt_match,
1660 .pm = &venc_pm_ops,
1661 },
1662 };
1663 module_platform_driver(qcom_venus_enc_driver);
1664
1665 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1666 MODULE_LICENSE("GPL v2");
1667