• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC);
415 	fps = (u64)USEC_PER_SEC;
416 	do_div(fps, 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 	}
737 
738 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
739 	    inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
740 		/* IDR periodicity, n:
741 		 * n = 0 - only the first I-frame is IDR frame
742 		 * n = 1 - all I-frames will be IDR frames
743 		 * n > 1 - every n-th I-frame will be IDR frame
744 		 */
745 		ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
746 		idrp.idr_period = 0;
747 		ret = hfi_session_set_property(inst, ptype, &idrp);
748 		if (ret)
749 			return ret;
750 	}
751 
752 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC &&
753 	    ctr->profile.hevc == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10) {
754 		struct hfi_hdr10_pq_sei hdr10;
755 		unsigned int c;
756 
757 		ptype = HFI_PROPERTY_PARAM_VENC_HDR10_PQ_SEI;
758 
759 		for (c = 0; c < 3; c++) {
760 			hdr10.mastering.display_primaries_x[c] =
761 				ctr->mastering.display_primaries_x[c];
762 			hdr10.mastering.display_primaries_y[c] =
763 				ctr->mastering.display_primaries_y[c];
764 		}
765 
766 		hdr10.mastering.white_point_x = ctr->mastering.white_point_x;
767 		hdr10.mastering.white_point_y = ctr->mastering.white_point_y;
768 		hdr10.mastering.max_display_mastering_luminance =
769 			ctr->mastering.max_display_mastering_luminance;
770 		hdr10.mastering.min_display_mastering_luminance =
771 			ctr->mastering.min_display_mastering_luminance;
772 
773 		hdr10.cll.max_content_light = ctr->cll.max_content_light_level;
774 		hdr10.cll.max_pic_average_light =
775 			ctr->cll.max_pic_average_light_level;
776 
777 		ret = hfi_session_set_property(inst, ptype, &hdr10);
778 		if (ret)
779 			return ret;
780 	}
781 
782 	if (ctr->num_b_frames) {
783 		u32 max_num_b_frames = NUM_B_FRAMES_MAX;
784 
785 		ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
786 		ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
787 		if (ret)
788 			return ret;
789 	}
790 
791 	ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
792 	intra_period.pframes = ctr->num_p_frames;
793 	intra_period.bframes = ctr->num_b_frames;
794 
795 	ret = hfi_session_set_property(inst, ptype, &intra_period);
796 	if (ret)
797 		return ret;
798 
799 	if (!ctr->rc_enable)
800 		rate_control = HFI_RATE_CONTROL_OFF;
801 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
802 		rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR :
803 						      HFI_RATE_CONTROL_VBR_CFR;
804 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
805 		rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR :
806 						      HFI_RATE_CONTROL_CBR_CFR;
807 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
808 		rate_control = HFI_RATE_CONTROL_CQ;
809 
810 	ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
811 	ret = hfi_session_set_property(inst, ptype, &rate_control);
812 	if (ret)
813 		return ret;
814 
815 	if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) {
816 		struct hfi_heic_frame_quality quality = {};
817 
818 		ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY;
819 		quality.frame_quality = ctr->const_quality;
820 		ret = hfi_session_set_property(inst, ptype, &quality);
821 		if (ret)
822 			return ret;
823 	}
824 
825 	if (!ctr->bitrate)
826 		bitrate = 64000;
827 	else
828 		bitrate = ctr->bitrate;
829 
830 	ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
831 	brate.bitrate = bitrate;
832 	brate.layer_id = 0;
833 
834 	ret = hfi_session_set_property(inst, ptype, &brate);
835 	if (ret)
836 		return ret;
837 
838 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
839 	    inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
840 		ptype = HFI_PROPERTY_CONFIG_VENC_SYNC_FRAME_SEQUENCE_HEADER;
841 		if (ctr->header_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)
842 			en.enable = 0;
843 		else
844 			en.enable = 1;
845 
846 		ret = hfi_session_set_property(inst, ptype, &en);
847 		if (ret)
848 			return ret;
849 	}
850 
851 	if (!ctr->bitrate_peak)
852 		bitrate *= 2;
853 	else
854 		bitrate = ctr->bitrate_peak;
855 
856 	ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
857 	brate.bitrate = bitrate;
858 	brate.layer_id = 0;
859 
860 	ret = hfi_session_set_property(inst, ptype, &brate);
861 	if (ret)
862 		return ret;
863 
864 	ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
865 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
866 		quant.qp_i = ctr->hevc_i_qp;
867 		quant.qp_p = ctr->hevc_p_qp;
868 		quant.qp_b = ctr->hevc_b_qp;
869 	} else {
870 		quant.qp_i = ctr->h264_i_qp;
871 		quant.qp_p = ctr->h264_p_qp;
872 		quant.qp_b = ctr->h264_b_qp;
873 	}
874 	quant.layer_id = 0;
875 	ret = hfi_session_set_property(inst, ptype, &quant);
876 	if (ret)
877 		return ret;
878 
879 	if (inst->core->res->hfi_version == HFI_VERSION_4XX ||
880 	    inst->core->res->hfi_version == HFI_VERSION_6XX) {
881 		ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE_V2;
882 
883 		if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
884 			quant_range_v2.min_qp.qp_packed = ctr->hevc_min_qp;
885 			quant_range_v2.max_qp.qp_packed = ctr->hevc_max_qp;
886 		} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
887 			quant_range_v2.min_qp.qp_packed = ctr->vp8_min_qp;
888 			quant_range_v2.max_qp.qp_packed = ctr->vp8_max_qp;
889 		} else {
890 			quant_range_v2.min_qp.qp_packed = ctr->h264_min_qp;
891 			quant_range_v2.max_qp.qp_packed = ctr->h264_max_qp;
892 		}
893 
894 		ret = hfi_session_set_property(inst, ptype, &quant_range_v2);
895 	} else {
896 		ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
897 
898 		if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
899 			quant_range.min_qp = ctr->hevc_min_qp;
900 			quant_range.max_qp = ctr->hevc_max_qp;
901 		} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
902 			quant_range.min_qp = ctr->vp8_min_qp;
903 			quant_range.max_qp = ctr->vp8_max_qp;
904 		} else {
905 			quant_range.min_qp = ctr->h264_min_qp;
906 			quant_range.max_qp = ctr->h264_max_qp;
907 		}
908 
909 		quant_range.layer_id = 0;
910 		ret = hfi_session_set_property(inst, ptype, &quant_range);
911 	}
912 
913 	if (ret)
914 		return ret;
915 
916 	ptype = HFI_PROPERTY_PARAM_VENC_LTRMODE;
917 	ltr_mode.ltr_count = ctr->ltr_count;
918 	ltr_mode.ltr_mode = HFI_LTR_MODE_MANUAL;
919 	ltr_mode.trust_mode = 1;
920 	ret = hfi_session_set_property(inst, ptype, &ltr_mode);
921 	if (ret)
922 		return ret;
923 
924 	switch (inst->hfi_codec) {
925 	case HFI_VIDEO_CODEC_H264:
926 		profile = ctr->profile.h264;
927 		level = ctr->level.h264;
928 		break;
929 	case HFI_VIDEO_CODEC_MPEG4:
930 		profile = ctr->profile.mpeg4;
931 		level = ctr->level.mpeg4;
932 		break;
933 	case HFI_VIDEO_CODEC_VP8:
934 		profile = ctr->profile.vp8;
935 		level = 0;
936 		break;
937 	case HFI_VIDEO_CODEC_VP9:
938 		profile = ctr->profile.vp9;
939 		level = ctr->level.vp9;
940 		break;
941 	case HFI_VIDEO_CODEC_HEVC:
942 		profile = ctr->profile.hevc;
943 		level = ctr->level.hevc;
944 		break;
945 	case HFI_VIDEO_CODEC_MPEG2:
946 	default:
947 		profile = 0;
948 		level = 0;
949 		break;
950 	}
951 
952 	ret = venus_helper_set_profile_level(inst, profile, level);
953 	if (ret)
954 		return ret;
955 
956 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
957 	    inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
958 		struct hfi_enable en = {};
959 
960 		ptype = HFI_PROPERTY_PARAM_VENC_H264_GENERATE_AUDNAL;
961 
962 		if (ctr->aud_enable)
963 			en.enable = 1;
964 
965 		ret = hfi_session_set_property(inst, ptype, &en);
966 	}
967 
968 	if ((inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 ||
969 	     inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) &&
970 	    (rate_control == HFI_RATE_CONTROL_CBR_VFR ||
971 	     rate_control == HFI_RATE_CONTROL_CBR_CFR)) {
972 		intra_refresh.mode = HFI_INTRA_REFRESH_NONE;
973 		intra_refresh.cir_mbs = 0;
974 
975 		if (ctr->intra_refresh_period) {
976 			u32 mbs;
977 
978 			mbs = ALIGN(inst->width, 16) * ALIGN(inst->height, 16);
979 			mbs /= 16 * 16;
980 			if (mbs % ctr->intra_refresh_period)
981 				mbs++;
982 			mbs /= ctr->intra_refresh_period;
983 
984 			intra_refresh.cir_mbs = mbs;
985 			if (ctr->intra_refresh_type ==
986 			    V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC)
987 				intra_refresh.mode = HFI_INTRA_REFRESH_CYCLIC;
988 			else
989 				intra_refresh.mode = HFI_INTRA_REFRESH_RANDOM;
990 		}
991 
992 		ptype = HFI_PROPERTY_PARAM_VENC_INTRA_REFRESH;
993 
994 		ret = hfi_session_set_property(inst, ptype, &intra_refresh);
995 		if (ret)
996 			return ret;
997 	}
998 
999 	return 0;
1000 }
1001 
venc_init_session(struct venus_inst * inst)1002 static int venc_init_session(struct venus_inst *inst)
1003 {
1004 	int ret;
1005 
1006 	ret = venus_helper_session_init(inst);
1007 	if (ret == -EALREADY)
1008 		return 0;
1009 	else if (ret)
1010 		return ret;
1011 
1012 	ret = venus_helper_set_stride(inst, inst->out_width,
1013 				      inst->out_height);
1014 	if (ret)
1015 		goto deinit;
1016 
1017 	ret = venus_helper_set_input_resolution(inst, inst->width,
1018 						inst->height);
1019 	if (ret)
1020 		goto deinit;
1021 
1022 	ret = venus_helper_set_output_resolution(inst, inst->width,
1023 						 inst->height,
1024 						 HFI_BUFFER_OUTPUT);
1025 	if (ret)
1026 		goto deinit;
1027 
1028 	ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
1029 	if (ret)
1030 		goto deinit;
1031 
1032 	ret = venc_set_properties(inst);
1033 	if (ret)
1034 		goto deinit;
1035 
1036 	return 0;
1037 deinit:
1038 	hfi_session_deinit(inst);
1039 	return ret;
1040 }
1041 
venc_out_num_buffers(struct venus_inst * inst,unsigned int * num)1042 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
1043 {
1044 	struct hfi_buffer_requirements bufreq;
1045 	int ret;
1046 
1047 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1048 	if (ret)
1049 		return ret;
1050 
1051 	*num = bufreq.count_actual;
1052 
1053 	return 0;
1054 }
1055 
venc_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])1056 static int venc_queue_setup(struct vb2_queue *q,
1057 			    unsigned int *num_buffers, unsigned int *num_planes,
1058 			    unsigned int sizes[], struct device *alloc_devs[])
1059 {
1060 	struct venus_inst *inst = vb2_get_drv_priv(q);
1061 	struct venus_core *core = inst->core;
1062 	unsigned int num, min = 4;
1063 	int ret;
1064 
1065 	if (*num_planes) {
1066 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1067 		    *num_planes != inst->fmt_out->num_planes)
1068 			return -EINVAL;
1069 
1070 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1071 		    *num_planes != inst->fmt_cap->num_planes)
1072 			return -EINVAL;
1073 
1074 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1075 		    sizes[0] < inst->input_buf_size)
1076 			return -EINVAL;
1077 
1078 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1079 		    sizes[0] < inst->output_buf_size)
1080 			return -EINVAL;
1081 
1082 		return 0;
1083 	}
1084 
1085 	if (test_bit(0, &core->sys_error)) {
1086 		if (inst->nonblock)
1087 			return -EAGAIN;
1088 
1089 		ret = wait_event_interruptible(core->sys_err_done,
1090 					       !test_bit(0, &core->sys_error));
1091 		if (ret)
1092 			return ret;
1093 	}
1094 
1095 	ret = venc_pm_get(inst);
1096 	if (ret)
1097 		return ret;
1098 
1099 	mutex_lock(&inst->lock);
1100 	ret = venc_init_session(inst);
1101 	mutex_unlock(&inst->lock);
1102 
1103 	if (ret)
1104 		goto put_power;
1105 
1106 	ret = venc_pm_put(inst, false);
1107 	if (ret)
1108 		return ret;
1109 
1110 	switch (q->type) {
1111 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1112 		*num_planes = inst->fmt_out->num_planes;
1113 
1114 		ret = venc_out_num_buffers(inst, &num);
1115 		if (ret)
1116 			break;
1117 
1118 		num = max(num, min);
1119 		*num_buffers = max(*num_buffers, num);
1120 		inst->num_input_bufs = *num_buffers;
1121 
1122 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
1123 						    inst->out_width,
1124 						    inst->out_height);
1125 		inst->input_buf_size = sizes[0];
1126 		break;
1127 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1128 		*num_planes = inst->fmt_cap->num_planes;
1129 		*num_buffers = max(*num_buffers, min);
1130 		inst->num_output_bufs = *num_buffers;
1131 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
1132 						    inst->width,
1133 						    inst->height);
1134 		sizes[0] = max(sizes[0], inst->output_buf_size);
1135 		inst->output_buf_size = sizes[0];
1136 		break;
1137 	default:
1138 		ret = -EINVAL;
1139 		break;
1140 	}
1141 
1142 	return ret;
1143 put_power:
1144 	venc_pm_put(inst, false);
1145 	return ret;
1146 }
1147 
venc_buf_init(struct vb2_buffer * vb)1148 static int venc_buf_init(struct vb2_buffer *vb)
1149 {
1150 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1151 
1152 	inst->buf_count++;
1153 
1154 	return venus_helper_vb2_buf_init(vb);
1155 }
1156 
venc_release_session(struct venus_inst * inst)1157 static void venc_release_session(struct venus_inst *inst)
1158 {
1159 	int ret;
1160 
1161 	venc_pm_get(inst);
1162 
1163 	mutex_lock(&inst->lock);
1164 
1165 	ret = hfi_session_deinit(inst);
1166 	if (ret || inst->session_error)
1167 		hfi_session_abort(inst);
1168 
1169 	mutex_unlock(&inst->lock);
1170 
1171 	venus_pm_load_scale(inst);
1172 	INIT_LIST_HEAD(&inst->registeredbufs);
1173 	venus_pm_release_core(inst);
1174 
1175 	venc_pm_put(inst, false);
1176 }
1177 
venc_buf_cleanup(struct vb2_buffer * vb)1178 static void venc_buf_cleanup(struct vb2_buffer *vb)
1179 {
1180 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1181 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1182 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1183 
1184 	mutex_lock(&inst->lock);
1185 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1186 		if (!list_empty(&inst->registeredbufs))
1187 			list_del_init(&buf->reg_list);
1188 	mutex_unlock(&inst->lock);
1189 
1190 	inst->buf_count--;
1191 	if (!inst->buf_count)
1192 		venc_release_session(inst);
1193 }
1194 
venc_verify_conf(struct venus_inst * inst)1195 static int venc_verify_conf(struct venus_inst *inst)
1196 {
1197 	enum hfi_version ver = inst->core->res->hfi_version;
1198 	struct hfi_buffer_requirements bufreq;
1199 	int ret;
1200 
1201 	if (!inst->num_input_bufs || !inst->num_output_bufs)
1202 		return -EINVAL;
1203 
1204 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
1205 	if (ret)
1206 		return ret;
1207 
1208 	if (inst->num_output_bufs < bufreq.count_actual ||
1209 	    inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1210 		return -EINVAL;
1211 
1212 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1213 	if (ret)
1214 		return ret;
1215 
1216 	if (inst->num_input_bufs < bufreq.count_actual ||
1217 	    inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1218 		return -EINVAL;
1219 
1220 	return 0;
1221 }
1222 
venc_start_streaming(struct vb2_queue * q,unsigned int count)1223 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
1224 {
1225 	struct venus_inst *inst = vb2_get_drv_priv(q);
1226 	int ret;
1227 
1228 	mutex_lock(&inst->lock);
1229 
1230 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1231 		inst->streamon_out = 1;
1232 	else
1233 		inst->streamon_cap = 1;
1234 
1235 	if (!(inst->streamon_out & inst->streamon_cap)) {
1236 		mutex_unlock(&inst->lock);
1237 		return 0;
1238 	}
1239 
1240 	venus_helper_init_instance(inst);
1241 
1242 	inst->sequence_cap = 0;
1243 	inst->sequence_out = 0;
1244 
1245 	ret = venc_pm_get(inst);
1246 	if (ret)
1247 		goto error;
1248 
1249 	ret = venus_pm_acquire_core(inst);
1250 	if (ret)
1251 		goto put_power;
1252 
1253 	ret = venc_pm_put(inst, true);
1254 	if (ret)
1255 		goto error;
1256 
1257 	ret = venc_set_properties(inst);
1258 	if (ret)
1259 		goto error;
1260 
1261 	ret = venc_verify_conf(inst);
1262 	if (ret)
1263 		goto error;
1264 
1265 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1266 					inst->num_output_bufs, 0);
1267 	if (ret)
1268 		goto error;
1269 
1270 	ret = venus_helper_vb2_start_streaming(inst);
1271 	if (ret)
1272 		goto error;
1273 
1274 	inst->enc_state = VENUS_ENC_STATE_ENCODING;
1275 
1276 	mutex_unlock(&inst->lock);
1277 
1278 	return 0;
1279 
1280 put_power:
1281 	venc_pm_put(inst, false);
1282 error:
1283 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1284 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1285 		inst->streamon_out = 0;
1286 	else
1287 		inst->streamon_cap = 0;
1288 	mutex_unlock(&inst->lock);
1289 	return ret;
1290 }
1291 
venc_vb2_buf_queue(struct vb2_buffer * vb)1292 static void venc_vb2_buf_queue(struct vb2_buffer *vb)
1293 {
1294 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1295 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1296 
1297 	venc_pm_get_put(inst);
1298 
1299 	mutex_lock(&inst->lock);
1300 
1301 	if (inst->enc_state == VENUS_ENC_STATE_STOPPED) {
1302 		vbuf->sequence = inst->sequence_cap++;
1303 		vbuf->field = V4L2_FIELD_NONE;
1304 		vb2_set_plane_payload(vb, 0, 0);
1305 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1306 		mutex_unlock(&inst->lock);
1307 		return;
1308 	}
1309 
1310 	venus_helper_vb2_buf_queue(vb);
1311 	mutex_unlock(&inst->lock);
1312 }
1313 
1314 static const struct vb2_ops venc_vb2_ops = {
1315 	.queue_setup = venc_queue_setup,
1316 	.buf_init = venc_buf_init,
1317 	.buf_cleanup = venc_buf_cleanup,
1318 	.buf_prepare = venus_helper_vb2_buf_prepare,
1319 	.start_streaming = venc_start_streaming,
1320 	.stop_streaming = venus_helper_vb2_stop_streaming,
1321 	.buf_queue = venc_vb2_buf_queue,
1322 };
1323 
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)1324 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1325 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1326 			  u32 hfi_flags, u64 timestamp_us)
1327 {
1328 	struct vb2_v4l2_buffer *vbuf;
1329 	struct vb2_buffer *vb;
1330 	unsigned int type;
1331 
1332 	venc_pm_touch(inst);
1333 
1334 	if (buf_type == HFI_BUFFER_INPUT)
1335 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1336 	else
1337 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1338 
1339 	vbuf = venus_helper_find_buf(inst, type, tag);
1340 	if (!vbuf)
1341 		return;
1342 
1343 	vbuf->flags = flags;
1344 
1345 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1346 		vb = &vbuf->vb2_buf;
1347 		vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1348 		vb->planes[0].data_offset = data_offset;
1349 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1350 		vbuf->sequence = inst->sequence_cap++;
1351 		if ((vbuf->flags & V4L2_BUF_FLAG_LAST) &&
1352 		    inst->enc_state == VENUS_ENC_STATE_DRAIN) {
1353 			inst->enc_state = VENUS_ENC_STATE_STOPPED;
1354 		}
1355 	} else {
1356 		vbuf->sequence = inst->sequence_out++;
1357 	}
1358 
1359 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1360 }
1361 
venc_event_notify(struct venus_inst * inst,u32 event,struct hfi_event_data * data)1362 static void venc_event_notify(struct venus_inst *inst, u32 event,
1363 			      struct hfi_event_data *data)
1364 {
1365 	struct device *dev = inst->core->dev_enc;
1366 
1367 	venc_pm_touch(inst);
1368 
1369 	if (event == EVT_SESSION_ERROR) {
1370 		inst->session_error = true;
1371 		venus_helper_vb2_queue_error(inst);
1372 		dev_err(dev, "enc: event session error %x\n", inst->error);
1373 	}
1374 }
1375 
1376 static const struct hfi_inst_ops venc_hfi_ops = {
1377 	.buf_done = venc_buf_done,
1378 	.event_notify = venc_event_notify,
1379 };
1380 
1381 static const struct v4l2_m2m_ops venc_m2m_ops = {
1382 	.device_run = venus_helper_m2m_device_run,
1383 	.job_abort = venus_helper_m2m_job_abort,
1384 };
1385 
m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1386 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1387 			  struct vb2_queue *dst_vq)
1388 {
1389 	struct venus_inst *inst = priv;
1390 	int ret;
1391 
1392 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1393 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1394 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1395 	src_vq->ops = &venc_vb2_ops;
1396 	src_vq->mem_ops = &vb2_dma_contig_memops;
1397 	src_vq->drv_priv = inst;
1398 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1399 	src_vq->allow_zero_bytesused = 1;
1400 	src_vq->min_queued_buffers = 1;
1401 	src_vq->dev = inst->core->dev;
1402 	src_vq->lock = &inst->ctx_q_lock;
1403 	if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1404 		src_vq->bidirectional = 1;
1405 	ret = vb2_queue_init(src_vq);
1406 	if (ret)
1407 		return ret;
1408 
1409 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1410 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1411 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1412 	dst_vq->ops = &venc_vb2_ops;
1413 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1414 	dst_vq->drv_priv = inst;
1415 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1416 	dst_vq->allow_zero_bytesused = 1;
1417 	dst_vq->min_queued_buffers = 1;
1418 	dst_vq->dev = inst->core->dev;
1419 	dst_vq->lock = &inst->ctx_q_lock;
1420 	return vb2_queue_init(dst_vq);
1421 }
1422 
venc_inst_init(struct venus_inst * inst)1423 static void venc_inst_init(struct venus_inst *inst)
1424 {
1425 	inst->fmt_cap = &venc_formats[VENUS_FMT_H264];
1426 	inst->fmt_out = &venc_formats[VENUS_FMT_NV12];
1427 	inst->width = 1280;
1428 	inst->height = ALIGN(720, 32);
1429 	inst->out_width = 1280;
1430 	inst->out_height = 720;
1431 	inst->fps = 15;
1432 	inst->timeperframe.numerator = 1;
1433 	inst->timeperframe.denominator = 15;
1434 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1435 }
1436 
venc_open(struct file * file)1437 static int venc_open(struct file *file)
1438 {
1439 	struct venus_core *core = video_drvdata(file);
1440 	struct venus_inst *inst;
1441 	int ret;
1442 
1443 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1444 	if (!inst)
1445 		return -ENOMEM;
1446 
1447 	INIT_LIST_HEAD(&inst->dpbbufs);
1448 	INIT_LIST_HEAD(&inst->registeredbufs);
1449 	INIT_LIST_HEAD(&inst->internalbufs);
1450 	INIT_LIST_HEAD(&inst->list);
1451 	mutex_init(&inst->lock);
1452 	mutex_init(&inst->ctx_q_lock);
1453 
1454 	inst->core = core;
1455 	inst->session_type = VIDC_SESSION_TYPE_ENC;
1456 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1457 	inst->core_acquired = false;
1458 	inst->nonblock = file->f_flags & O_NONBLOCK;
1459 
1460 	if (inst->enc_state == VENUS_ENC_STATE_DEINIT)
1461 		inst->enc_state = VENUS_ENC_STATE_INIT;
1462 
1463 	venus_helper_init_instance(inst);
1464 
1465 	ret = venc_ctrl_init(inst);
1466 	if (ret)
1467 		goto err_free;
1468 
1469 	ret = hfi_session_create(inst, &venc_hfi_ops);
1470 	if (ret)
1471 		goto err_ctrl_deinit;
1472 
1473 	venc_inst_init(inst);
1474 
1475 	/*
1476 	 * create m2m device for every instance, the m2m context scheduling
1477 	 * is made by firmware side so we do not need to care about.
1478 	 */
1479 	inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1480 	if (IS_ERR(inst->m2m_dev)) {
1481 		ret = PTR_ERR(inst->m2m_dev);
1482 		goto err_session_destroy;
1483 	}
1484 
1485 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1486 	if (IS_ERR(inst->m2m_ctx)) {
1487 		ret = PTR_ERR(inst->m2m_ctx);
1488 		goto err_m2m_release;
1489 	}
1490 
1491 	v4l2_fh_init(&inst->fh, core->vdev_enc);
1492 
1493 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1494 	v4l2_fh_add(&inst->fh);
1495 	inst->fh.m2m_ctx = inst->m2m_ctx;
1496 	file->private_data = &inst->fh;
1497 
1498 	return 0;
1499 
1500 err_m2m_release:
1501 	v4l2_m2m_release(inst->m2m_dev);
1502 err_session_destroy:
1503 	hfi_session_destroy(inst);
1504 err_ctrl_deinit:
1505 	venc_ctrl_deinit(inst);
1506 err_free:
1507 	kfree(inst);
1508 	return ret;
1509 }
1510 
venc_close(struct file * file)1511 static int venc_close(struct file *file)
1512 {
1513 	struct venus_inst *inst = to_inst(file);
1514 
1515 	venc_pm_get(inst);
1516 
1517 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1518 	v4l2_m2m_release(inst->m2m_dev);
1519 	venc_ctrl_deinit(inst);
1520 	hfi_session_destroy(inst);
1521 	mutex_destroy(&inst->lock);
1522 	mutex_destroy(&inst->ctx_q_lock);
1523 	v4l2_fh_del(&inst->fh);
1524 	v4l2_fh_exit(&inst->fh);
1525 
1526 	inst->enc_state = VENUS_ENC_STATE_DEINIT;
1527 
1528 	venc_pm_put(inst, false);
1529 
1530 	kfree(inst);
1531 	return 0;
1532 }
1533 
1534 static const struct v4l2_file_operations venc_fops = {
1535 	.owner = THIS_MODULE,
1536 	.open = venc_open,
1537 	.release = venc_close,
1538 	.unlocked_ioctl = video_ioctl2,
1539 	.poll = v4l2_m2m_fop_poll,
1540 	.mmap = v4l2_m2m_fop_mmap,
1541 };
1542 
venc_probe(struct platform_device * pdev)1543 static int venc_probe(struct platform_device *pdev)
1544 {
1545 	struct device *dev = &pdev->dev;
1546 	struct video_device *vdev;
1547 	struct venus_core *core;
1548 	int ret;
1549 
1550 	if (!dev->parent)
1551 		return -EPROBE_DEFER;
1552 
1553 	core = dev_get_drvdata(dev->parent);
1554 	if (!core)
1555 		return -EPROBE_DEFER;
1556 
1557 	platform_set_drvdata(pdev, core);
1558 
1559 	if (core->pm_ops->venc_get) {
1560 		ret = core->pm_ops->venc_get(dev);
1561 		if (ret)
1562 			return ret;
1563 	}
1564 
1565 	vdev = video_device_alloc();
1566 	if (!vdev)
1567 		return -ENOMEM;
1568 
1569 	strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1570 	vdev->release = video_device_release;
1571 	vdev->fops = &venc_fops;
1572 	vdev->ioctl_ops = &venc_ioctl_ops;
1573 	vdev->vfl_dir = VFL_DIR_M2M;
1574 	vdev->v4l2_dev = &core->v4l2_dev;
1575 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1576 
1577 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1578 	if (ret)
1579 		goto err_vdev_release;
1580 
1581 	core->vdev_enc = vdev;
1582 	core->dev_enc = dev;
1583 
1584 	video_set_drvdata(vdev, core);
1585 	pm_runtime_set_autosuspend_delay(dev, 2000);
1586 	pm_runtime_use_autosuspend(dev);
1587 	pm_runtime_enable(dev);
1588 
1589 	return 0;
1590 
1591 err_vdev_release:
1592 	video_device_release(vdev);
1593 	return ret;
1594 }
1595 
venc_remove(struct platform_device * pdev)1596 static void venc_remove(struct platform_device *pdev)
1597 {
1598 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1599 
1600 	video_unregister_device(core->vdev_enc);
1601 	pm_runtime_disable(core->dev_enc);
1602 
1603 	if (core->pm_ops->venc_put)
1604 		core->pm_ops->venc_put(core->dev_enc);
1605 }
1606 
venc_runtime_suspend(struct device * dev)1607 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1608 {
1609 	struct venus_core *core = dev_get_drvdata(dev);
1610 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1611 	int ret = 0;
1612 
1613 	if (pm_ops->venc_power)
1614 		ret = pm_ops->venc_power(dev, POWER_OFF);
1615 
1616 	return ret;
1617 }
1618 
venc_runtime_resume(struct device * dev)1619 static __maybe_unused int venc_runtime_resume(struct device *dev)
1620 {
1621 	struct venus_core *core = dev_get_drvdata(dev);
1622 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1623 	int ret = 0;
1624 
1625 	if (pm_ops->venc_power)
1626 		ret = pm_ops->venc_power(dev, POWER_ON);
1627 
1628 	return ret;
1629 }
1630 
1631 static const struct dev_pm_ops venc_pm_ops = {
1632 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1633 				pm_runtime_force_resume)
1634 	SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1635 };
1636 
1637 static const struct of_device_id venc_dt_match[] = {
1638 	{ .compatible = "venus-encoder" },
1639 	{ }
1640 };
1641 MODULE_DEVICE_TABLE(of, venc_dt_match);
1642 
1643 static struct platform_driver qcom_venus_enc_driver = {
1644 	.probe = venc_probe,
1645 	.remove_new = venc_remove,
1646 	.driver = {
1647 		.name = "qcom-venus-encoder",
1648 		.of_match_table = venc_dt_match,
1649 		.pm = &venc_pm_ops,
1650 	},
1651 };
1652 module_platform_driver(qcom_venus_enc_driver);
1653 
1654 MODULE_ALIAS("platform:qcom-venus-encoder");
1655 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1656 MODULE_LICENSE("GPL v2");
1657