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