xref: /linux/drivers/media/platform/qcom/venus/vdec.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 #include <linux/clk.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-ioctl.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-mem2mem.h>
15 #include <media/videobuf2-dma-contig.h>
16 
17 #include "hfi_venus_io.h"
18 #include "hfi_parser.h"
19 #include "core.h"
20 #include "helpers.h"
21 #include "vdec.h"
22 #include "pm_helpers.h"
23 
24 /*
25  * Three resons to keep MPLANE formats (despite that the number of planes
26  * currently is one):
27  * - the MPLANE formats allow only one plane to be used
28  * - the downstream driver use MPLANE formats too
29  * - future firmware versions could add support for >1 planes
30  */
31 static const struct venus_format vdec_formats[] = {
32 	[VENUS_FMT_NV12] = {
33 		.pixfmt = V4L2_PIX_FMT_NV12,
34 		.num_planes = 1,
35 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
36 	},
37 	[VENUS_FMT_QC08C] = {
38 		.pixfmt = V4L2_PIX_FMT_QC08C,
39 		.num_planes = 1,
40 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
41 	},
42 	[VENUS_FMT_QC10C] = {
43 		.pixfmt = V4L2_PIX_FMT_QC10C,
44 		.num_planes = 1,
45 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
46 	},
47 	[VENUS_FMT_P010] = {
48 		.pixfmt = V4L2_PIX_FMT_P010,
49 		.num_planes = 1,
50 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
51 	},
52 	[VENUS_FMT_H264] = {
53 		.pixfmt = V4L2_PIX_FMT_H264,
54 		.num_planes = 1,
55 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
56 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
57 	},
58 	[VENUS_FMT_VP8] = {
59 		.pixfmt = V4L2_PIX_FMT_VP8,
60 		.num_planes = 1,
61 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
62 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
63 	},
64 	[VENUS_FMT_VP9] = {
65 		.pixfmt = V4L2_PIX_FMT_VP9,
66 		.num_planes = 1,
67 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
68 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
69 	},
70 	[VENUS_FMT_HEVC] = {
71 		.pixfmt = V4L2_PIX_FMT_HEVC,
72 		.num_planes = 1,
73 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
74 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
75 	},
76 	[VENUS_FMT_VC1_ANNEX_G] = {
77 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
78 		.num_planes = 1,
79 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
80 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
81 	},
82 	[VENUS_FMT_VC1_ANNEX_L] = {
83 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
84 		.num_planes = 1,
85 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
86 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
87 	},
88 	[VENUS_FMT_MPEG4] = {
89 		.pixfmt = V4L2_PIX_FMT_MPEG4,
90 		.num_planes = 1,
91 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
92 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
93 	},
94 	[VENUS_FMT_MPEG2] = {
95 		.pixfmt = V4L2_PIX_FMT_MPEG2,
96 		.num_planes = 1,
97 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
98 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
99 	},
100 	[VENUS_FMT_H263] = {
101 		.pixfmt = V4L2_PIX_FMT_H263,
102 		.num_planes = 1,
103 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
104 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
105 	},
106 	[VENUS_FMT_XVID] = {
107 		.pixfmt = V4L2_PIX_FMT_XVID,
108 		.num_planes = 1,
109 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
110 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
111 	},
112 
113 };
114 
115 static const struct venus_format *
find_format(struct venus_inst * inst,u32 pixfmt,u32 type)116 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
117 {
118 	const struct venus_format *fmt = vdec_formats;
119 	unsigned int size = ARRAY_SIZE(vdec_formats);
120 	unsigned int i;
121 
122 	for (i = 0; i < size; i++) {
123 		if (fmt[i].pixfmt == pixfmt)
124 			break;
125 	}
126 
127 	if (i == size || fmt[i].type != type)
128 		return NULL;
129 
130 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
131 	    !venus_helper_check_codec(inst, fmt[i].pixfmt))
132 		return NULL;
133 
134 	if (V4L2_TYPE_IS_CAPTURE(type) &&
135 	    !venus_helper_check_format(inst, fmt[i].pixfmt))
136 		return NULL;
137 
138 	if (V4L2_TYPE_IS_CAPTURE(type) && fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
139 	    !(inst->bit_depth == VIDC_BITDEPTH_10))
140 		return NULL;
141 
142 	return &fmt[i];
143 }
144 
145 static const struct venus_format *
find_format_by_index(struct venus_inst * inst,unsigned int index,u32 type)146 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
147 {
148 	const struct venus_format *fmt = vdec_formats;
149 	unsigned int size = ARRAY_SIZE(vdec_formats);
150 	unsigned int i, k = 0;
151 
152 	if (index > size)
153 		return NULL;
154 
155 	for (i = 0; i < size; i++) {
156 		bool valid = false;
157 
158 		if (fmt[i].type != type)
159 			continue;
160 
161 		if (V4L2_TYPE_IS_OUTPUT(type)) {
162 			valid = venus_helper_check_codec(inst, fmt[i].pixfmt);
163 		} else {
164 			valid = venus_helper_check_format(inst, fmt[i].pixfmt);
165 
166 			if (fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
167 			    !(inst->bit_depth == VIDC_BITDEPTH_10))
168 				valid = false;
169 		}
170 
171 		if (k == index && valid)
172 			break;
173 		if (valid)
174 			k++;
175 	}
176 
177 	if (i == size)
178 		return NULL;
179 
180 	return &fmt[i];
181 }
182 
183 static const struct venus_format *
vdec_try_fmt_common(struct venus_inst * inst,struct v4l2_format * f)184 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
185 {
186 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
187 	struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
188 	const struct venus_format *fmt;
189 	u32 szimage;
190 
191 	memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
192 	memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
193 
194 	fmt = find_format(inst, pixmp->pixelformat, f->type);
195 	if (!fmt) {
196 		if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
197 			pixmp->pixelformat = V4L2_PIX_FMT_NV12;
198 		else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
199 			pixmp->pixelformat = V4L2_PIX_FMT_H264;
200 		else
201 			return NULL;
202 		fmt = find_format(inst, pixmp->pixelformat, f->type);
203 		if (!fmt)
204 			return NULL;
205 	}
206 
207 	pixmp->width = clamp(pixmp->width, frame_width_min(inst),
208 			     frame_width_max(inst));
209 	pixmp->height = clamp(pixmp->height, frame_height_min(inst),
210 			      frame_height_max(inst));
211 
212 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
213 		pixmp->height = ALIGN(pixmp->height, 32);
214 
215 	if (pixmp->field == V4L2_FIELD_ANY)
216 		pixmp->field = V4L2_FIELD_NONE;
217 	pixmp->num_planes = fmt->num_planes;
218 	pixmp->flags = 0;
219 
220 	szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
221 					   pixmp->height);
222 
223 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
224 		unsigned int stride = pixmp->width;
225 
226 		if (pixmp->pixelformat == V4L2_PIX_FMT_P010)
227 			stride *= 2;
228 
229 		pfmt[0].sizeimage = szimage;
230 		pfmt[0].bytesperline = ALIGN(stride, 128);
231 	} else {
232 		pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M);
233 		pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage);
234 		pfmt[0].bytesperline = 0;
235 	}
236 
237 	return fmt;
238 }
239 
vdec_try_fmt(struct file * file,void * fh,struct v4l2_format * f)240 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
241 {
242 	struct venus_inst *inst = to_inst(file);
243 
244 	vdec_try_fmt_common(inst, f);
245 
246 	return 0;
247 }
248 
vdec_check_src_change(struct venus_inst * inst)249 static int vdec_check_src_change(struct venus_inst *inst)
250 {
251 	int ret;
252 
253 	if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
254 	    inst->codec_state == VENUS_DEC_STATE_INIT &&
255 	    !inst->reconfig)
256 		return -EINVAL;
257 
258 	if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
259 		return 0;
260 
261 	/*
262 	 * The code snippet below is a workaround for backward compatibility
263 	 * with applications which doesn't support V4L2 events. It will be
264 	 * dropped in future once those applications are fixed.
265 	 */
266 
267 	if (inst->codec_state != VENUS_DEC_STATE_INIT)
268 		goto done;
269 
270 	ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
271 				 msecs_to_jiffies(100));
272 	if (!ret)
273 		return -EINVAL;
274 
275 	if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
276 	    !inst->reconfig)
277 		dev_dbg(inst->core->dev, VDBGH "wrong state\n");
278 
279 done:
280 	return 0;
281 }
282 
vdec_g_fmt(struct file * file,void * fh,struct v4l2_format * f)283 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
284 {
285 	struct venus_inst *inst = to_inst(file);
286 	const struct venus_format *fmt = NULL;
287 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
288 	int ret;
289 
290 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
291 		fmt = inst->fmt_cap;
292 	else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
293 		fmt = inst->fmt_out;
294 
295 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
296 		ret = vdec_check_src_change(inst);
297 		if (ret)
298 			return ret;
299 	}
300 
301 	pixmp->pixelformat = fmt->pixfmt;
302 
303 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
304 		pixmp->width = inst->width;
305 		pixmp->height = inst->height;
306 		pixmp->colorspace = inst->colorspace;
307 		pixmp->ycbcr_enc = inst->ycbcr_enc;
308 		pixmp->quantization = inst->quantization;
309 		pixmp->xfer_func = inst->xfer_func;
310 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
311 		pixmp->width = inst->out_width;
312 		pixmp->height = inst->out_height;
313 	}
314 
315 	vdec_try_fmt_common(inst, f);
316 
317 	return 0;
318 }
319 
vdec_s_fmt(struct file * file,void * fh,struct v4l2_format * f)320 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
321 {
322 	struct venus_inst *inst = to_inst(file);
323 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
324 	struct v4l2_pix_format_mplane orig_pixmp;
325 	const struct venus_format *fmt;
326 	struct v4l2_format format;
327 	u32 pixfmt_out = 0, pixfmt_cap = 0;
328 	struct vb2_queue *q;
329 
330 	q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
331 
332 	if (vb2_is_busy(q))
333 		return -EBUSY;
334 
335 	orig_pixmp = *pixmp;
336 
337 	fmt = vdec_try_fmt_common(inst, f);
338 
339 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
340 		pixfmt_out = pixmp->pixelformat;
341 		pixfmt_cap = inst->fmt_cap->pixfmt;
342 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
343 		pixfmt_cap = pixmp->pixelformat;
344 		pixfmt_out = inst->fmt_out->pixfmt;
345 	}
346 
347 	memset(&format, 0, sizeof(format));
348 
349 	format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
350 	format.fmt.pix_mp.pixelformat = pixfmt_out;
351 	format.fmt.pix_mp.width = orig_pixmp.width;
352 	format.fmt.pix_mp.height = orig_pixmp.height;
353 	vdec_try_fmt_common(inst, &format);
354 
355 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
356 		inst->out_width = format.fmt.pix_mp.width;
357 		inst->out_height = format.fmt.pix_mp.height;
358 		inst->colorspace = pixmp->colorspace;
359 		inst->ycbcr_enc = pixmp->ycbcr_enc;
360 		inst->quantization = pixmp->quantization;
361 		inst->xfer_func = pixmp->xfer_func;
362 		inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
363 	}
364 
365 	memset(&format, 0, sizeof(format));
366 
367 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
368 	format.fmt.pix_mp.pixelformat = pixfmt_cap;
369 	format.fmt.pix_mp.width = orig_pixmp.width;
370 	format.fmt.pix_mp.height = orig_pixmp.height;
371 	vdec_try_fmt_common(inst, &format);
372 
373 	inst->width = format.fmt.pix_mp.width;
374 	inst->height = format.fmt.pix_mp.height;
375 	inst->crop.top = 0;
376 	inst->crop.left = 0;
377 	inst->crop.width = inst->width;
378 	inst->crop.height = inst->height;
379 
380 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
381 		inst->fmt_out = fmt;
382 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
383 		inst->fmt_cap = fmt;
384 		inst->output2_buf_size =
385 			venus_helper_get_framesz(pixfmt_cap, orig_pixmp.width, orig_pixmp.height);
386 	}
387 
388 	return 0;
389 }
390 
391 static int
vdec_g_selection(struct file * file,void * fh,struct v4l2_selection * s)392 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
393 {
394 	struct venus_inst *inst = to_inst(file);
395 
396 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
397 	    s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
398 		return -EINVAL;
399 
400 	s->r.top = 0;
401 	s->r.left = 0;
402 
403 	switch (s->target) {
404 	case V4L2_SEL_TGT_CROP_BOUNDS:
405 	case V4L2_SEL_TGT_CROP_DEFAULT:
406 	case V4L2_SEL_TGT_CROP:
407 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
408 			return -EINVAL;
409 		s->r.width = inst->out_width;
410 		s->r.height = inst->out_height;
411 		break;
412 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
413 	case V4L2_SEL_TGT_COMPOSE_PADDED:
414 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
415 			return -EINVAL;
416 		s->r.width = inst->width;
417 		s->r.height = inst->height;
418 		break;
419 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
420 	case V4L2_SEL_TGT_COMPOSE:
421 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
422 			return -EINVAL;
423 		s->r = inst->crop;
424 		break;
425 	default:
426 		return -EINVAL;
427 	}
428 
429 	return 0;
430 }
431 
432 static int
vdec_querycap(struct file * file,void * fh,struct v4l2_capability * cap)433 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
434 {
435 	struct venus_inst *inst = to_inst(file);
436 	struct venus_core *core = inst->core;
437 
438 	strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
439 	strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
440 	snprintf(cap->bus_info, sizeof(cap->bus_info),
441 		 "plat:%s:dec", dev_name(core->dev));
442 
443 	return 0;
444 }
445 
vdec_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)446 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
447 {
448 	struct venus_inst *inst = to_inst(file);
449 	const struct venus_format *fmt;
450 
451 	memset(f->reserved, 0, sizeof(f->reserved));
452 
453 	fmt = find_format_by_index(inst, f->index, f->type);
454 	if (!fmt)
455 		return -EINVAL;
456 
457 	f->pixelformat = fmt->pixfmt;
458 	f->flags = fmt->flags;
459 
460 	return 0;
461 }
462 
vdec_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)463 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
464 {
465 	struct venus_inst *inst = to_inst(file);
466 	struct v4l2_captureparm *cap = &a->parm.capture;
467 	struct v4l2_fract *timeperframe = &cap->timeperframe;
468 	u64 us_per_frame, fps;
469 
470 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
471 	    a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
472 		return -EINVAL;
473 
474 	memset(cap->reserved, 0, sizeof(cap->reserved));
475 	if (!timeperframe->denominator)
476 		timeperframe->denominator = inst->timeperframe.denominator;
477 	if (!timeperframe->numerator)
478 		timeperframe->numerator = inst->timeperframe.numerator;
479 	cap->readbuffers = 0;
480 	cap->extendedmode = 0;
481 	cap->capability = V4L2_CAP_TIMEPERFRAME;
482 	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
483 	do_div(us_per_frame, timeperframe->denominator);
484 
485 	us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC);
486 	fps = USEC_PER_SEC / (u32)us_per_frame;
487 	fps = min(VENUS_MAX_FPS, fps);
488 
489 	inst->fps = fps;
490 	inst->timeperframe = *timeperframe;
491 
492 	return 0;
493 }
494 
vdec_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)495 static int vdec_enum_framesizes(struct file *file, void *fh,
496 				struct v4l2_frmsizeenum *fsize)
497 {
498 	struct venus_inst *inst = to_inst(file);
499 	const struct venus_format *fmt;
500 
501 	fmt = find_format(inst, fsize->pixel_format,
502 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
503 	if (!fmt) {
504 		fmt = find_format(inst, fsize->pixel_format,
505 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
506 		if (!fmt)
507 			return -EINVAL;
508 	}
509 
510 	if (fsize->index)
511 		return -EINVAL;
512 
513 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
514 
515 	fsize->stepwise.min_width = frame_width_min(inst);
516 	fsize->stepwise.max_width = frame_width_max(inst);
517 	fsize->stepwise.step_width = frame_width_step(inst);
518 	fsize->stepwise.min_height = frame_height_min(inst);
519 	fsize->stepwise.max_height = frame_height_max(inst);
520 	fsize->stepwise.step_height = frame_height_step(inst);
521 
522 	return 0;
523 }
524 
vdec_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)525 static int vdec_subscribe_event(struct v4l2_fh *fh,
526 				const struct v4l2_event_subscription *sub)
527 {
528 	struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
529 	int ret;
530 
531 	switch (sub->type) {
532 	case V4L2_EVENT_EOS:
533 		return v4l2_event_subscribe(fh, sub, 2, NULL);
534 	case V4L2_EVENT_SOURCE_CHANGE:
535 		ret = v4l2_src_change_event_subscribe(fh, sub);
536 		if (ret)
537 			return ret;
538 		inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
539 		return 0;
540 	case V4L2_EVENT_CTRL:
541 		return v4l2_ctrl_subscribe_event(fh, sub);
542 	default:
543 		return -EINVAL;
544 	}
545 }
546 
547 static int
vdec_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * cmd)548 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
549 {
550 	struct venus_inst *inst = to_inst(file);
551 	struct vb2_queue *dst_vq;
552 	struct hfi_frame_data fdata = {0};
553 	int ret;
554 
555 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
556 	if (ret)
557 		return ret;
558 
559 	mutex_lock(&inst->lock);
560 
561 	if (cmd->cmd == V4L2_DEC_CMD_STOP) {
562 		/*
563 		 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
564 		 * decoder input to signal EOS.
565 		 */
566 		if (!(inst->streamon_out && inst->streamon_cap))
567 			goto unlock;
568 
569 		fdata.buffer_type = HFI_BUFFER_INPUT;
570 		fdata.flags |= HFI_BUFFERFLAG_EOS;
571 
572 		/* Send NULL EOS addr for only IRIS2 (SM8250),for firmware <= 1.0.87.
573 		 * SC7280 also reports "1.0.<hash>" parsed as 1.0.0; restricting to IRIS2
574 		 * avoids misapplying this quirk and breaking VP9 decode on SC7280.
575 		 */
576 
577 		if (IS_IRIS2(inst->core) && is_fw_rev_or_older(inst->core, 1, 0, 87))
578 			fdata.device_addr = 0;
579 		else
580 			fdata.device_addr = 0xdeadb000;
581 
582 		ret = hfi_session_process_buf(inst, &fdata);
583 
584 		if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING) {
585 			inst->codec_state = VENUS_DEC_STATE_DRAIN;
586 			inst->drain_active = true;
587 		}
588 	} else if (cmd->cmd == V4L2_DEC_CMD_START &&
589 		   inst->codec_state == VENUS_DEC_STATE_STOPPED) {
590 		dst_vq = v4l2_m2m_get_vq(inst->fh.m2m_ctx,
591 					 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
592 		vb2_clear_last_buffer_dequeued(dst_vq);
593 
594 		inst->codec_state = VENUS_DEC_STATE_DECODING;
595 	}
596 
597 unlock:
598 	mutex_unlock(&inst->lock);
599 	return ret;
600 }
601 
602 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
603 	.vidioc_querycap = vdec_querycap,
604 	.vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
605 	.vidioc_enum_fmt_vid_out = vdec_enum_fmt,
606 	.vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
607 	.vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
608 	.vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
609 	.vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
610 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
611 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
612 	.vidioc_g_selection = vdec_g_selection,
613 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
614 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
615 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
616 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
617 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
618 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
619 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
620 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
621 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
622 	.vidioc_s_parm = vdec_s_parm,
623 	.vidioc_enum_framesizes = vdec_enum_framesizes,
624 	.vidioc_subscribe_event = vdec_subscribe_event,
625 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
626 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
627 	.vidioc_decoder_cmd = vdec_decoder_cmd,
628 };
629 
vdec_pm_get(struct venus_inst * inst)630 static int vdec_pm_get(struct venus_inst *inst)
631 {
632 	struct venus_core *core = inst->core;
633 	struct device *dev = core->dev_dec;
634 	int ret;
635 
636 	mutex_lock(&core->pm_lock);
637 	ret = pm_runtime_resume_and_get(dev);
638 	mutex_unlock(&core->pm_lock);
639 
640 	return ret;
641 }
642 
vdec_pm_put(struct venus_inst * inst,bool autosuspend)643 static int vdec_pm_put(struct venus_inst *inst, bool autosuspend)
644 {
645 	struct venus_core *core = inst->core;
646 	struct device *dev = core->dev_dec;
647 	int ret;
648 
649 	mutex_lock(&core->pm_lock);
650 
651 	if (autosuspend)
652 		ret = pm_runtime_put_autosuspend(dev);
653 	else
654 		ret = pm_runtime_put_sync(dev);
655 
656 	mutex_unlock(&core->pm_lock);
657 
658 	return ret < 0 ? ret : 0;
659 }
660 
vdec_pm_get_put(struct venus_inst * inst)661 static int vdec_pm_get_put(struct venus_inst *inst)
662 {
663 	struct venus_core *core = inst->core;
664 	struct device *dev = core->dev_dec;
665 	int ret = 0;
666 
667 	mutex_lock(&core->pm_lock);
668 
669 	if (pm_runtime_suspended(dev)) {
670 		ret = pm_runtime_resume_and_get(dev);
671 		if (ret < 0)
672 			goto error;
673 
674 		ret = pm_runtime_put_autosuspend(dev);
675 	}
676 
677 error:
678 	mutex_unlock(&core->pm_lock);
679 
680 	return ret < 0 ? ret : 0;
681 }
682 
vdec_pm_touch(struct venus_inst * inst)683 static void vdec_pm_touch(struct venus_inst *inst)
684 {
685 	pm_runtime_mark_last_busy(inst->core->dev_dec);
686 }
687 
vdec_set_properties(struct venus_inst * inst)688 static int vdec_set_properties(struct venus_inst *inst)
689 {
690 	struct vdec_controls *ctr = &inst->controls.dec;
691 	struct hfi_enable en = { .enable = 1 };
692 	u32 ptype, decode_order, conceal;
693 	int ret;
694 
695 	if (ctr->post_loop_deb_mode) {
696 		ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
697 		ret = hfi_session_set_property(inst, ptype, &en);
698 		if (ret)
699 			return ret;
700 	}
701 
702 	if (ctr->display_delay_enable && ctr->display_delay == 0) {
703 		ptype = HFI_PROPERTY_PARAM_VDEC_OUTPUT_ORDER;
704 		decode_order = HFI_OUTPUT_ORDER_DECODE;
705 		ret = hfi_session_set_property(inst, ptype, &decode_order);
706 		if (ret)
707 			return ret;
708 	}
709 
710 	/* Enabling sufficient sequence change support for VP9 */
711 	if (is_fw_rev_or_newer(inst->core, 5, 4, 51)) {
712 		ptype = HFI_PROPERTY_PARAM_VDEC_ENABLE_SUFFICIENT_SEQCHANGE_EVENT;
713 		ret = hfi_session_set_property(inst, ptype, &en);
714 		if (ret)
715 			return ret;
716 	}
717 
718 	ptype = HFI_PROPERTY_PARAM_VDEC_CONCEAL_COLOR;
719 	conceal = ctr->conceal_color & 0xffff;
720 	conceal |= ((ctr->conceal_color >> 16) & 0xffff) << 10;
721 	conceal |= ((ctr->conceal_color >> 32) & 0xffff) << 20;
722 
723 	ret = hfi_session_set_property(inst, ptype, &conceal);
724 	if (ret)
725 		return ret;
726 
727 	return 0;
728 }
729 
vdec_set_work_route(struct venus_inst * inst)730 static int vdec_set_work_route(struct venus_inst *inst)
731 {
732 	u32 ptype = HFI_PROPERTY_PARAM_WORK_ROUTE;
733 	struct hfi_video_work_route wr;
734 
735 	if (!(IS_IRIS2(inst->core) || IS_IRIS2_1(inst->core)))
736 		return 0;
737 
738 	wr.video_work_route = inst->core->res->num_vpp_pipes;
739 
740 	return hfi_session_set_property(inst, ptype, &wr);
741 }
742 
743 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
744 #define is_10bit_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_10_BIT_BASE & \
745 				 HFI_COLOR_FORMAT_UBWC_BASE))
746 
747 
vdec_output_conf(struct venus_inst * inst)748 static int vdec_output_conf(struct venus_inst *inst)
749 {
750 	struct venus_core *core = inst->core;
751 	struct hfi_enable en = { .enable = 1 };
752 	struct hfi_buffer_requirements bufreq;
753 	u32 width = inst->width;
754 	u32 height = inst->height;
755 	u32 out_fmt, out2_fmt;
756 	bool ubwc = false;
757 	u32 ptype;
758 	int ret;
759 
760 	ret = venus_helper_set_work_mode(inst);
761 	if (ret)
762 		return ret;
763 
764 	if (core->res->hfi_version == HFI_VERSION_1XX) {
765 		ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
766 		ret = hfi_session_set_property(inst, ptype, &en);
767 		if (ret)
768 			return ret;
769 	}
770 
771 	/* Force searching UBWC formats for bigger then HD resolutions */
772 	if (width > 1920 && height > ALIGN(1080, 32))
773 		ubwc = true;
774 
775 	/* For Venus v4/v6 UBWC format is mandatory */
776 	if (IS_V4(core) || IS_V6(core))
777 		ubwc = true;
778 
779 	ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
780 					&out2_fmt, ubwc);
781 	if (ret)
782 		return ret;
783 
784 	inst->output_buf_size =
785 			venus_helper_get_framesz_raw(out_fmt, width, height);
786 	inst->output2_buf_size =
787 			venus_helper_get_framesz_raw(out2_fmt, width, height);
788 
789 	if (is_ubwc_fmt(out_fmt)) {
790 		inst->opb_buftype = HFI_BUFFER_OUTPUT2;
791 		inst->opb_fmt = out2_fmt;
792 		inst->dpb_buftype = HFI_BUFFER_OUTPUT;
793 		inst->dpb_fmt = out_fmt;
794 	} else if (is_ubwc_fmt(out2_fmt) || is_10bit_ubwc_fmt(out_fmt)) {
795 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
796 		inst->opb_fmt = out_fmt;
797 		inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
798 		inst->dpb_fmt = out2_fmt;
799 	} else {
800 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
801 		inst->opb_fmt = out_fmt;
802 		inst->dpb_buftype = 0;
803 		inst->dpb_fmt = 0;
804 	}
805 
806 	ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
807 					  inst->opb_buftype);
808 	if (ret)
809 		return ret;
810 
811 	ret = venus_helper_set_format_constraints(inst);
812 	if (ret)
813 		return ret;
814 
815 	if (inst->dpb_fmt) {
816 		ret = venus_helper_set_multistream(inst, false, true);
817 		if (ret)
818 			return ret;
819 
820 		ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
821 						  inst->dpb_buftype);
822 		if (ret)
823 			return ret;
824 
825 		ret = venus_helper_set_output_resolution(inst, width, height,
826 							 HFI_BUFFER_OUTPUT2);
827 		if (ret)
828 			return ret;
829 	}
830 
831 	if (IS_V3(core) || IS_V4(core) || IS_V6(core)) {
832 		ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
833 		if (ret)
834 			return ret;
835 
836 		if (bufreq.size > inst->output_buf_size)
837 			return -EINVAL;
838 
839 		if (inst->dpb_fmt) {
840 			ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT2,
841 						      &bufreq);
842 			if (ret)
843 				return ret;
844 
845 			if (bufreq.size > inst->output2_buf_size)
846 				return -EINVAL;
847 		}
848 
849 		if (inst->output2_buf_size) {
850 			ret = venus_helper_set_bufsize(inst,
851 						       inst->output2_buf_size,
852 						       HFI_BUFFER_OUTPUT2);
853 			if (ret)
854 				return ret;
855 		}
856 
857 		if (inst->output_buf_size) {
858 			ret = venus_helper_set_bufsize(inst,
859 						       inst->output_buf_size,
860 						       HFI_BUFFER_OUTPUT);
861 			if (ret)
862 				return ret;
863 		}
864 	}
865 
866 	ret = venus_helper_set_dyn_bufmode(inst);
867 	if (ret)
868 		return ret;
869 
870 	return 0;
871 }
872 
vdec_session_init(struct venus_inst * inst)873 static int vdec_session_init(struct venus_inst *inst)
874 {
875 	int ret;
876 
877 	ret = venus_helper_session_init(inst);
878 	if (ret == -EALREADY)
879 		return 0;
880 	else if (ret)
881 		return ret;
882 
883 	ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
884 						frame_height_min(inst));
885 	if (ret)
886 		goto deinit;
887 
888 	return 0;
889 deinit:
890 	hfi_session_deinit(inst);
891 	return ret;
892 }
893 
vdec_num_buffers(struct venus_inst * inst,unsigned int * in_num,unsigned int * out_num)894 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
895 			    unsigned int *out_num)
896 {
897 	enum hfi_version ver = inst->core->res->hfi_version;
898 	struct hfi_buffer_requirements bufreq;
899 	int ret;
900 
901 	*in_num = *out_num = 0;
902 
903 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
904 	if (ret)
905 		return ret;
906 
907 	*in_num = hfi_bufreq_get_count_min(&bufreq, ver);
908 
909 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
910 	if (ret)
911 		return ret;
912 
913 	*out_num = hfi_bufreq_get_count_min(&bufreq, ver);
914 
915 	return 0;
916 }
917 
vdec_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])918 static int vdec_queue_setup(struct vb2_queue *q,
919 			    unsigned int *num_buffers, unsigned int *num_planes,
920 			    unsigned int sizes[], struct device *alloc_devs[])
921 {
922 	struct venus_inst *inst = vb2_get_drv_priv(q);
923 	struct venus_core *core = inst->core;
924 	unsigned int in_num, out_num;
925 	int ret = 0;
926 
927 	if (*num_planes) {
928 		unsigned int output_buf_size = venus_helper_get_opb_size(inst);
929 
930 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
931 		    *num_planes != inst->fmt_out->num_planes)
932 			return -EINVAL;
933 
934 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
935 		    *num_planes != inst->fmt_cap->num_planes)
936 			return -EINVAL;
937 
938 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
939 		    sizes[0] < inst->input_buf_size)
940 			return -EINVAL;
941 
942 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
943 		    sizes[0] < output_buf_size)
944 			return -EINVAL;
945 
946 		return 0;
947 	}
948 
949 	if (test_bit(0, &core->sys_error)) {
950 		if (inst->nonblock)
951 			return -EAGAIN;
952 
953 		ret = wait_event_interruptible(core->sys_err_done,
954 					       !test_bit(0, &core->sys_error));
955 		if (ret)
956 			return ret;
957 	}
958 
959 	ret = vdec_pm_get(inst);
960 	if (ret)
961 		return ret;
962 
963 	ret = vdec_session_init(inst);
964 	if (ret)
965 		goto put_power;
966 
967 	ret = vdec_num_buffers(inst, &in_num, &out_num);
968 	if (ret)
969 		goto put_power;
970 
971 	ret = vdec_pm_put(inst, false);
972 	if (ret)
973 		return ret;
974 
975 	switch (q->type) {
976 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
977 		*num_planes = inst->fmt_out->num_planes;
978 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
979 						    inst->out_width,
980 						    inst->out_height);
981 		sizes[0] = max(sizes[0], inst->input_buf_size);
982 		inst->input_buf_size = sizes[0];
983 		*num_buffers = max(*num_buffers, in_num);
984 		inst->num_input_bufs = *num_buffers;
985 		inst->num_output_bufs = out_num;
986 		break;
987 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
988 		*num_planes = inst->fmt_cap->num_planes;
989 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
990 						    inst->width,
991 						    inst->height);
992 		inst->output_buf_size = sizes[0];
993 		*num_buffers = max(*num_buffers, out_num);
994 		inst->num_output_bufs = *num_buffers;
995 
996 		mutex_lock(&inst->lock);
997 		if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
998 			inst->codec_state = VENUS_DEC_STATE_STOPPED;
999 		mutex_unlock(&inst->lock);
1000 		break;
1001 	default:
1002 		ret = -EINVAL;
1003 		break;
1004 	}
1005 
1006 	return ret;
1007 
1008 put_power:
1009 	vdec_pm_put(inst, false);
1010 	return ret;
1011 }
1012 
vdec_verify_conf(struct venus_inst * inst)1013 static int vdec_verify_conf(struct venus_inst *inst)
1014 {
1015 	enum hfi_version ver = inst->core->res->hfi_version;
1016 	struct hfi_buffer_requirements bufreq;
1017 	int ret;
1018 
1019 	if (!inst->num_input_bufs || !inst->num_output_bufs)
1020 		return -EINVAL;
1021 
1022 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
1023 	if (ret)
1024 		return ret;
1025 
1026 	if (inst->num_output_bufs < bufreq.count_actual ||
1027 	    inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1028 		return -EINVAL;
1029 
1030 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1031 	if (ret)
1032 		return ret;
1033 
1034 	if (inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1035 		return -EINVAL;
1036 
1037 	return 0;
1038 }
1039 
vdec_start_capture(struct venus_inst * inst)1040 static int vdec_start_capture(struct venus_inst *inst)
1041 {
1042 	int ret;
1043 
1044 	if (!inst->streamon_out)
1045 		return 0;
1046 
1047 	if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
1048 		if (inst->reconfig)
1049 			goto reconfigure;
1050 
1051 		venus_helper_queue_dpb_bufs(inst);
1052 		venus_helper_process_initial_cap_bufs(inst);
1053 		inst->streamon_cap = 1;
1054 		return 0;
1055 	}
1056 
1057 	if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
1058 		return 0;
1059 
1060 reconfigure:
1061 	ret = vdec_output_conf(inst);
1062 	if (ret)
1063 		return ret;
1064 
1065 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1066 					VB2_MAX_FRAME, VB2_MAX_FRAME);
1067 	if (ret)
1068 		return ret;
1069 
1070 	ret = venus_helper_intbufs_realloc(inst);
1071 	if (ret)
1072 		goto err;
1073 
1074 	venus_pm_load_scale(inst);
1075 
1076 	inst->next_buf_last = false;
1077 
1078 	ret = venus_helper_alloc_dpb_bufs(inst);
1079 	if (ret)
1080 		goto err;
1081 
1082 	ret = hfi_session_continue(inst);
1083 	if (ret)
1084 		goto free_dpb_bufs;
1085 
1086 	ret = venus_helper_queue_dpb_bufs(inst);
1087 	if (ret)
1088 		goto free_dpb_bufs;
1089 
1090 	ret = venus_helper_process_initial_cap_bufs(inst);
1091 	if (ret)
1092 		goto free_dpb_bufs;
1093 
1094 	inst->codec_state = VENUS_DEC_STATE_DECODING;
1095 
1096 	if (inst->drain_active)
1097 		inst->codec_state = VENUS_DEC_STATE_DRAIN;
1098 
1099 	inst->streamon_cap = 1;
1100 	inst->sequence_cap = 0;
1101 	inst->reconfig = false;
1102 	inst->drain_active = false;
1103 
1104 	return 0;
1105 
1106 free_dpb_bufs:
1107 	venus_helper_free_dpb_bufs(inst);
1108 err:
1109 	return ret;
1110 }
1111 
vdec_start_output(struct venus_inst * inst)1112 static int vdec_start_output(struct venus_inst *inst)
1113 {
1114 	int ret;
1115 
1116 	if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
1117 		ret = venus_helper_process_initial_out_bufs(inst);
1118 		if (ret)
1119 			return ret;
1120 
1121 		if (inst->next_buf_last) {
1122 			inst->codec_state = VENUS_DEC_STATE_DRC;
1123 		} else {
1124 			inst->codec_state = VENUS_DEC_STATE_DECODING;
1125 
1126 			if (inst->streamon_cap) {
1127 				ret = venus_helper_queue_dpb_bufs(inst);
1128 				if (ret)
1129 					return ret;
1130 			}
1131 		}
1132 		goto done;
1133 	}
1134 
1135 	if (inst->codec_state == VENUS_DEC_STATE_INIT ||
1136 	    inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
1137 		ret = venus_helper_process_initial_out_bufs(inst);
1138 		goto done;
1139 	}
1140 
1141 	if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
1142 		return -EINVAL;
1143 
1144 	venus_helper_init_instance(inst);
1145 	inst->sequence_out = 0;
1146 	inst->reconfig = false;
1147 	inst->next_buf_last = false;
1148 
1149 	ret = vdec_set_properties(inst);
1150 	if (ret)
1151 		return ret;
1152 
1153 	ret = vdec_set_work_route(inst);
1154 	if (ret)
1155 		return ret;
1156 
1157 	ret = vdec_output_conf(inst);
1158 	if (ret)
1159 		return ret;
1160 
1161 	ret = vdec_verify_conf(inst);
1162 	if (ret)
1163 		return ret;
1164 
1165 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1166 					VB2_MAX_FRAME, VB2_MAX_FRAME);
1167 	if (ret)
1168 		return ret;
1169 
1170 	ret = venus_helper_vb2_start_streaming(inst);
1171 	if (ret)
1172 		return ret;
1173 
1174 	ret = venus_helper_process_initial_out_bufs(inst);
1175 	if (ret)
1176 		return ret;
1177 
1178 	inst->codec_state = VENUS_DEC_STATE_INIT;
1179 
1180 done:
1181 	inst->streamon_out = 1;
1182 	return ret;
1183 }
1184 
vdec_start_streaming(struct vb2_queue * q,unsigned int count)1185 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1186 {
1187 	struct venus_inst *inst = vb2_get_drv_priv(q);
1188 	int ret;
1189 
1190 	mutex_lock(&inst->lock);
1191 
1192 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1193 		ret = vdec_start_capture(inst);
1194 	} else {
1195 		ret = vdec_pm_get(inst);
1196 		if (ret)
1197 			goto error;
1198 
1199 		ret = venus_pm_acquire_core(inst);
1200 		if (ret)
1201 			goto put_power;
1202 
1203 		ret = vdec_pm_put(inst, true);
1204 		if (ret)
1205 			goto error;
1206 
1207 		ret = vdec_start_output(inst);
1208 	}
1209 
1210 	if (ret)
1211 		goto error;
1212 
1213 	mutex_unlock(&inst->lock);
1214 	return 0;
1215 
1216 put_power:
1217 	vdec_pm_put(inst, false);
1218 error:
1219 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1220 	mutex_unlock(&inst->lock);
1221 	return ret;
1222 }
1223 
vdec_cancel_dst_buffers(struct venus_inst * inst)1224 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1225 {
1226 	struct vb2_v4l2_buffer *buf;
1227 
1228 	while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1229 		v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1230 }
1231 
vdec_stop_capture(struct venus_inst * inst)1232 static int vdec_stop_capture(struct venus_inst *inst)
1233 {
1234 	int ret = 0;
1235 
1236 	switch (inst->codec_state) {
1237 	case VENUS_DEC_STATE_DECODING:
1238 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1239 		fallthrough;
1240 	case VENUS_DEC_STATE_DRAIN:
1241 		inst->codec_state = VENUS_DEC_STATE_STOPPED;
1242 		inst->drain_active = false;
1243 		fallthrough;
1244 	case VENUS_DEC_STATE_SEEK:
1245 		vdec_cancel_dst_buffers(inst);
1246 		break;
1247 	case VENUS_DEC_STATE_DRC:
1248 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
1249 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1250 		venus_helper_free_dpb_bufs(inst);
1251 		break;
1252 	default:
1253 		break;
1254 	}
1255 
1256 	return ret;
1257 }
1258 
vdec_stop_output(struct venus_inst * inst)1259 static int vdec_stop_output(struct venus_inst *inst)
1260 {
1261 	int ret = 0;
1262 
1263 	switch (inst->codec_state) {
1264 	case VENUS_DEC_STATE_DECODING:
1265 	case VENUS_DEC_STATE_DRAIN:
1266 	case VENUS_DEC_STATE_STOPPED:
1267 	case VENUS_DEC_STATE_DRC:
1268 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1269 		inst->codec_state = VENUS_DEC_STATE_SEEK;
1270 		break;
1271 	case VENUS_DEC_STATE_INIT:
1272 	case VENUS_DEC_STATE_CAPTURE_SETUP:
1273 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1274 		break;
1275 	default:
1276 		break;
1277 	}
1278 
1279 	return ret;
1280 }
1281 
vdec_stop_streaming(struct vb2_queue * q)1282 static void vdec_stop_streaming(struct vb2_queue *q)
1283 {
1284 	struct venus_inst *inst = vb2_get_drv_priv(q);
1285 	int ret = -EINVAL;
1286 
1287 	vdec_pm_get_put(inst);
1288 
1289 	mutex_lock(&inst->lock);
1290 
1291 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1292 		ret = vdec_stop_capture(inst);
1293 	else
1294 		ret = vdec_stop_output(inst);
1295 
1296 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1297 
1298 	inst->session_error = 0;
1299 
1300 	if (ret)
1301 		goto unlock;
1302 
1303 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1304 		inst->streamon_out = 0;
1305 	else
1306 		inst->streamon_cap = 0;
1307 
1308 unlock:
1309 	mutex_unlock(&inst->lock);
1310 }
1311 
vdec_session_release(struct venus_inst * inst)1312 static void vdec_session_release(struct venus_inst *inst)
1313 {
1314 	struct venus_core *core = inst->core;
1315 	int ret, abort = 0;
1316 
1317 	vdec_pm_get(inst);
1318 
1319 	mutex_lock(&inst->lock);
1320 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1321 
1322 	ret = hfi_session_stop(inst);
1323 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1324 	ret = hfi_session_unload_res(inst);
1325 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1326 	ret = venus_helper_unregister_bufs(inst);
1327 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1328 	ret = venus_helper_intbufs_free(inst);
1329 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1330 	ret = hfi_session_deinit(inst);
1331 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1332 
1333 	if (inst->session_error || test_bit(0, &core->sys_error))
1334 		abort = 1;
1335 
1336 	if (abort)
1337 		hfi_session_abort(inst);
1338 
1339 	venus_helper_free_dpb_bufs(inst);
1340 	venus_pm_load_scale(inst);
1341 	INIT_LIST_HEAD(&inst->registeredbufs);
1342 	mutex_unlock(&inst->lock);
1343 
1344 	venus_pm_release_core(inst);
1345 	vdec_pm_put(inst, false);
1346 }
1347 
vdec_buf_init(struct vb2_buffer * vb)1348 static int vdec_buf_init(struct vb2_buffer *vb)
1349 {
1350 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1351 
1352 	inst->buf_count++;
1353 
1354 	return venus_helper_vb2_buf_init(vb);
1355 }
1356 
vdec_buf_cleanup(struct vb2_buffer * vb)1357 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1358 {
1359 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1360 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1361 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1362 
1363 	mutex_lock(&inst->lock);
1364 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1365 		if (!list_empty(&inst->registeredbufs))
1366 			list_del_init(&buf->reg_list);
1367 	mutex_unlock(&inst->lock);
1368 
1369 	inst->buf_count--;
1370 	if (!inst->buf_count)
1371 		vdec_session_release(inst);
1372 }
1373 
vdec_vb2_buf_queue(struct vb2_buffer * vb)1374 static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1375 {
1376 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1377 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1378 	static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS };
1379 
1380 	vdec_pm_get_put(inst);
1381 
1382 	mutex_lock(&inst->lock);
1383 
1384 	if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1385 	    inst->codec_state == VENUS_DEC_STATE_DRC) {
1386 		vbuf->flags |= V4L2_BUF_FLAG_LAST;
1387 		vbuf->sequence = inst->sequence_cap++;
1388 		vbuf->field = V4L2_FIELD_NONE;
1389 		vb2_set_plane_payload(vb, 0, 0);
1390 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1391 		v4l2_event_queue_fh(&inst->fh, &eos);
1392 		inst->next_buf_last = false;
1393 		mutex_unlock(&inst->lock);
1394 		return;
1395 	}
1396 
1397 	venus_helper_vb2_buf_queue(vb);
1398 	mutex_unlock(&inst->lock);
1399 }
1400 
1401 static const struct vb2_ops vdec_vb2_ops = {
1402 	.queue_setup = vdec_queue_setup,
1403 	.buf_init = vdec_buf_init,
1404 	.buf_cleanup = vdec_buf_cleanup,
1405 	.buf_prepare = venus_helper_vb2_buf_prepare,
1406 	.start_streaming = vdec_start_streaming,
1407 	.stop_streaming = vdec_stop_streaming,
1408 	.buf_queue = vdec_vb2_buf_queue,
1409 };
1410 
vdec_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)1411 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1412 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1413 			  u32 hfi_flags, u64 timestamp_us)
1414 {
1415 	enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1416 	struct vb2_v4l2_buffer *vbuf;
1417 	struct vb2_buffer *vb;
1418 	unsigned int type;
1419 
1420 	vdec_pm_touch(inst);
1421 
1422 	if (buf_type == HFI_BUFFER_INPUT)
1423 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1424 	else
1425 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1426 
1427 	vbuf = venus_helper_find_buf(inst, type, tag);
1428 	if (!vbuf) {
1429 		venus_helper_change_dpb_owner(inst, vbuf, type, buf_type, tag);
1430 		return;
1431 	}
1432 
1433 	vbuf->flags = flags;
1434 	vbuf->field = V4L2_FIELD_NONE;
1435 	vb = &vbuf->vb2_buf;
1436 
1437 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1438 		vb2_set_plane_payload(vb, 0, bytesused);
1439 		vb->planes[0].data_offset = data_offset;
1440 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1441 		vbuf->sequence = inst->sequence_cap++;
1442 
1443 		if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1444 			const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1445 
1446 			v4l2_event_queue_fh(&inst->fh, &ev);
1447 
1448 			if (inst->codec_state == VENUS_DEC_STATE_DRAIN) {
1449 				inst->drain_active = false;
1450 				inst->codec_state = VENUS_DEC_STATE_STOPPED;
1451 			}
1452 		} else {
1453 			if (!bytesused)
1454 				state = VB2_BUF_STATE_ERROR;
1455 		}
1456 	} else {
1457 		vbuf->sequence = inst->sequence_out++;
1458 	}
1459 
1460 	venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1461 
1462 	if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1463 		venus_helper_acquire_buf_ref(vbuf);
1464 
1465 	if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1466 		state = VB2_BUF_STATE_ERROR;
1467 
1468 	if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1469 		state = VB2_BUF_STATE_ERROR;
1470 		vb2_set_plane_payload(vb, 0, 0);
1471 		vb->timestamp = 0;
1472 	}
1473 
1474 	v4l2_m2m_buf_done(vbuf, state);
1475 }
1476 
vdec_event_change(struct venus_inst * inst,struct hfi_event_data * ev_data,bool sufficient)1477 static void vdec_event_change(struct venus_inst *inst,
1478 			      struct hfi_event_data *ev_data, bool sufficient)
1479 {
1480 	static const struct v4l2_event ev = {
1481 		.type = V4L2_EVENT_SOURCE_CHANGE,
1482 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1483 	struct device *dev = inst->core->dev_dec;
1484 	struct v4l2_format format = {};
1485 
1486 	mutex_lock(&inst->lock);
1487 
1488 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1489 	format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1490 	format.fmt.pix_mp.width = ev_data->width;
1491 	format.fmt.pix_mp.height = ev_data->height;
1492 
1493 	vdec_try_fmt_common(inst, &format);
1494 
1495 	inst->width = format.fmt.pix_mp.width;
1496 	inst->height = format.fmt.pix_mp.height;
1497 	/*
1498 	 * Some versions of the firmware do not report crop information for
1499 	 * all codecs. For these cases, set the crop to the coded resolution.
1500 	 */
1501 	if (ev_data->input_crop.width > 0 && ev_data->input_crop.height > 0) {
1502 		inst->crop.left = ev_data->input_crop.left;
1503 		inst->crop.top = ev_data->input_crop.top;
1504 		inst->crop.width = ev_data->input_crop.width;
1505 		inst->crop.height = ev_data->input_crop.height;
1506 	} else {
1507 		inst->crop.left = 0;
1508 		inst->crop.top = 0;
1509 		inst->crop.width = ev_data->width;
1510 		inst->crop.height = ev_data->height;
1511 	}
1512 
1513 	inst->fw_min_cnt = ev_data->buf_count;
1514 	/* overwriting this to 11 for vp9 due to fw bug */
1515 	if (inst->hfi_codec == HFI_VIDEO_CODEC_VP9)
1516 		inst->fw_min_cnt = 11;
1517 
1518 	inst->out_width = ev_data->width;
1519 	inst->out_height = ev_data->height;
1520 
1521 	if (inst->bit_depth != ev_data->bit_depth) {
1522 		inst->bit_depth = ev_data->bit_depth;
1523 		if (inst->bit_depth == VIDC_BITDEPTH_10)
1524 			inst->fmt_cap = &vdec_formats[VENUS_FMT_P010];
1525 		else
1526 			inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12];
1527 	}
1528 
1529 	if (inst->pic_struct != ev_data->pic_struct)
1530 		inst->pic_struct = ev_data->pic_struct;
1531 
1532 	dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n",
1533 		sufficient ? "" : "not", ev_data->width, ev_data->height);
1534 
1535 	switch (inst->codec_state) {
1536 	case VENUS_DEC_STATE_INIT:
1537 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1538 		break;
1539 	case VENUS_DEC_STATE_DECODING:
1540 	case VENUS_DEC_STATE_DRAIN:
1541 		inst->codec_state = VENUS_DEC_STATE_DRC;
1542 		break;
1543 	default:
1544 		break;
1545 	}
1546 
1547 	/*
1548 	 * The assumption is that the firmware have to return the last buffer
1549 	 * before this event is received in the v4l2 driver. Also the firmware
1550 	 * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1551 	 */
1552 
1553 	if (inst->codec_state == VENUS_DEC_STATE_DRC) {
1554 		int ret;
1555 
1556 		inst->next_buf_last = true;
1557 
1558 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1559 		if (ret)
1560 			dev_dbg(dev, VDBGH "flush output error %d\n", ret);
1561 	}
1562 
1563 	inst->next_buf_last = true;
1564 	inst->reconfig = true;
1565 	v4l2_event_queue_fh(&inst->fh, &ev);
1566 	wake_up(&inst->reconf_wait);
1567 
1568 	mutex_unlock(&inst->lock);
1569 }
1570 
vdec_event_notify(struct venus_inst * inst,u32 event,struct hfi_event_data * data)1571 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1572 			      struct hfi_event_data *data)
1573 {
1574 	struct venus_core *core = inst->core;
1575 	struct device *dev = core->dev_dec;
1576 
1577 	vdec_pm_touch(inst);
1578 
1579 	switch (event) {
1580 	case EVT_SESSION_ERROR:
1581 		inst->session_error = true;
1582 		venus_helper_vb2_queue_error(inst);
1583 		dev_err(dev, "dec: event session error %x\n", inst->error);
1584 		break;
1585 	case EVT_SYS_EVENT_CHANGE:
1586 		switch (data->event_type) {
1587 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1588 			vdec_event_change(inst, data, true);
1589 			break;
1590 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1591 			vdec_event_change(inst, data, false);
1592 			break;
1593 		case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1594 			venus_helper_release_buf_ref(inst, data->tag);
1595 			break;
1596 		default:
1597 			break;
1598 		}
1599 		break;
1600 	default:
1601 		break;
1602 	}
1603 }
1604 
vdec_flush_done(struct venus_inst * inst)1605 static void vdec_flush_done(struct venus_inst *inst)
1606 {
1607 	dev_dbg(inst->core->dev_dec, VDBGH "flush done\n");
1608 }
1609 
1610 static const struct hfi_inst_ops vdec_hfi_ops = {
1611 	.buf_done = vdec_buf_done,
1612 	.event_notify = vdec_event_notify,
1613 	.flush_done = vdec_flush_done,
1614 };
1615 
vdec_inst_init(struct venus_inst * inst)1616 static void vdec_inst_init(struct venus_inst *inst)
1617 {
1618 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1619 	inst->fmt_out = &vdec_formats[VENUS_FMT_H264];
1620 	inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12];
1621 	inst->width = frame_width_min(inst);
1622 	inst->height = ALIGN(frame_height_min(inst), 32);
1623 	inst->crop.left = 0;
1624 	inst->crop.top = 0;
1625 	inst->crop.width = inst->width;
1626 	inst->crop.height = inst->height;
1627 	inst->fw_min_cnt = 8;
1628 	inst->out_width = frame_width_min(inst);
1629 	inst->out_height = frame_height_min(inst);
1630 	inst->fps = 30;
1631 	inst->timeperframe.numerator = 1;
1632 	inst->timeperframe.denominator = 30;
1633 	inst->opb_buftype = HFI_BUFFER_OUTPUT;
1634 }
1635 
vdec_m2m_device_run(void * priv)1636 static void vdec_m2m_device_run(void *priv)
1637 {
1638 }
1639 
1640 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1641 	.device_run = vdec_m2m_device_run,
1642 	.job_abort = venus_helper_m2m_job_abort,
1643 };
1644 
m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1645 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1646 			  struct vb2_queue *dst_vq)
1647 {
1648 	struct venus_inst *inst = priv;
1649 	int ret;
1650 
1651 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1652 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1653 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1654 	src_vq->ops = &vdec_vb2_ops;
1655 	src_vq->mem_ops = &vb2_dma_contig_memops;
1656 	src_vq->drv_priv = inst;
1657 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1658 	src_vq->allow_zero_bytesused = 1;
1659 	src_vq->min_queued_buffers = 0;
1660 	src_vq->dev = inst->core->dev;
1661 	src_vq->lock = &inst->ctx_q_lock;
1662 	ret = vb2_queue_init(src_vq);
1663 	if (ret)
1664 		return ret;
1665 
1666 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1667 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1668 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1669 	dst_vq->ops = &vdec_vb2_ops;
1670 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1671 	dst_vq->drv_priv = inst;
1672 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1673 	dst_vq->allow_zero_bytesused = 1;
1674 	dst_vq->min_queued_buffers = 0;
1675 	dst_vq->dev = inst->core->dev;
1676 	dst_vq->lock = &inst->ctx_q_lock;
1677 	return vb2_queue_init(dst_vq);
1678 }
1679 
vdec_open(struct file * file)1680 static int vdec_open(struct file *file)
1681 {
1682 	struct venus_core *core = video_drvdata(file);
1683 	struct venus_inst *inst;
1684 	int ret;
1685 
1686 	inst = kzalloc_obj(*inst);
1687 	if (!inst)
1688 		return -ENOMEM;
1689 
1690 	INIT_LIST_HEAD(&inst->dpbbufs);
1691 	INIT_LIST_HEAD(&inst->registeredbufs);
1692 	INIT_LIST_HEAD(&inst->internalbufs);
1693 	INIT_LIST_HEAD(&inst->list);
1694 	mutex_init(&inst->lock);
1695 	mutex_init(&inst->ctx_q_lock);
1696 
1697 	inst->core = core;
1698 	inst->session_type = VIDC_SESSION_TYPE_DEC;
1699 	inst->num_output_bufs = 1;
1700 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1701 	inst->buf_count = 0;
1702 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1703 	inst->core_acquired = false;
1704 	inst->bit_depth = VIDC_BITDEPTH_8;
1705 	inst->pic_struct = HFI_INTERLACE_FRAME_PROGRESSIVE;
1706 	init_waitqueue_head(&inst->reconf_wait);
1707 	inst->nonblock = file->f_flags & O_NONBLOCK;
1708 
1709 	venus_helper_init_instance(inst);
1710 
1711 	ret = vdec_ctrl_init(inst);
1712 	if (ret)
1713 		goto err_free;
1714 
1715 	vdec_inst_init(inst);
1716 
1717 	ida_init(&inst->dpb_ids);
1718 
1719 	/*
1720 	 * create m2m device for every instance, the m2m context scheduling
1721 	 * is made by firmware side so we do not need to care about.
1722 	 */
1723 	inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1724 	if (IS_ERR(inst->m2m_dev)) {
1725 		ret = PTR_ERR(inst->m2m_dev);
1726 		goto err_ctrl_deinit;
1727 	}
1728 
1729 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1730 	if (IS_ERR(inst->m2m_ctx)) {
1731 		ret = PTR_ERR(inst->m2m_ctx);
1732 		goto err_m2m_dev_release;
1733 	}
1734 
1735 	ret = hfi_session_create(inst, &vdec_hfi_ops);
1736 	if (ret)
1737 		goto err_m2m_ctx_release;
1738 
1739 	v4l2_fh_init(&inst->fh, core->vdev_dec);
1740 
1741 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1742 	v4l2_fh_add(&inst->fh, file);
1743 	inst->fh.m2m_ctx = inst->m2m_ctx;
1744 
1745 	return 0;
1746 
1747 err_m2m_ctx_release:
1748 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1749 err_m2m_dev_release:
1750 	v4l2_m2m_release(inst->m2m_dev);
1751 err_ctrl_deinit:
1752 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
1753 err_free:
1754 	kfree(inst);
1755 	return ret;
1756 }
1757 
vdec_close(struct file * file)1758 static int vdec_close(struct file *file)
1759 {
1760 	struct venus_inst *inst = to_inst(file);
1761 
1762 	vdec_pm_get(inst);
1763 	cancel_work_sync(&inst->delayed_process_work);
1764 	venus_close_common(inst, file);
1765 	ida_destroy(&inst->dpb_ids);
1766 	vdec_pm_put(inst, false);
1767 
1768 	kfree(inst);
1769 	return 0;
1770 }
1771 
1772 static const struct v4l2_file_operations vdec_fops = {
1773 	.owner = THIS_MODULE,
1774 	.open = vdec_open,
1775 	.release = vdec_close,
1776 	.unlocked_ioctl = video_ioctl2,
1777 	.poll = v4l2_m2m_fop_poll,
1778 	.mmap = v4l2_m2m_fop_mmap,
1779 };
1780 
vdec_probe(struct platform_device * pdev)1781 static int vdec_probe(struct platform_device *pdev)
1782 {
1783 	struct device *dev = &pdev->dev;
1784 	struct video_device *vdev;
1785 	struct venus_core *core;
1786 	int ret;
1787 
1788 	core = dev_get_drvdata(dev->parent);
1789 	if (!core)
1790 		return -EINVAL;
1791 
1792 	platform_set_drvdata(pdev, core);
1793 
1794 	if (core->pm_ops->vdec_get) {
1795 		ret = core->pm_ops->vdec_get(dev);
1796 		if (ret)
1797 			return ret;
1798 	}
1799 
1800 	vdev = video_device_alloc();
1801 	if (!vdev)
1802 		return -ENOMEM;
1803 
1804 	strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1805 	vdev->release = video_device_release;
1806 	vdev->fops = &vdec_fops;
1807 	vdev->ioctl_ops = &vdec_ioctl_ops;
1808 	vdev->vfl_dir = VFL_DIR_M2M;
1809 	vdev->v4l2_dev = &core->v4l2_dev;
1810 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1811 
1812 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1813 	if (ret)
1814 		goto err_vdev_release;
1815 
1816 	core->vdev_dec = vdev;
1817 	core->dev_dec = dev;
1818 
1819 	video_set_drvdata(vdev, core);
1820 	pm_runtime_set_autosuspend_delay(dev, 2000);
1821 	pm_runtime_use_autosuspend(dev);
1822 	pm_runtime_enable(dev);
1823 
1824 	return 0;
1825 
1826 err_vdev_release:
1827 	video_device_release(vdev);
1828 	return ret;
1829 }
1830 
vdec_remove(struct platform_device * pdev)1831 static void vdec_remove(struct platform_device *pdev)
1832 {
1833 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1834 
1835 	video_unregister_device(core->vdev_dec);
1836 	pm_runtime_disable(core->dev_dec);
1837 
1838 	if (core->pm_ops->vdec_put)
1839 		core->pm_ops->vdec_put(core->dev_dec);
1840 }
1841 
vdec_runtime_suspend(struct device * dev)1842 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1843 {
1844 	struct venus_core *core = dev_get_drvdata(dev);
1845 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1846 	int ret = 0;
1847 
1848 	if (pm_ops->vdec_power)
1849 		ret = pm_ops->vdec_power(dev, POWER_OFF);
1850 
1851 	return ret;
1852 }
1853 
vdec_runtime_resume(struct device * dev)1854 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1855 {
1856 	struct venus_core *core = dev_get_drvdata(dev);
1857 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1858 	int ret = 0;
1859 
1860 	if (pm_ops->vdec_power)
1861 		ret = pm_ops->vdec_power(dev, POWER_ON);
1862 
1863 	return ret;
1864 }
1865 
1866 static const struct dev_pm_ops vdec_pm_ops = {
1867 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1868 				pm_runtime_force_resume)
1869 	SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1870 };
1871 
1872 static const struct of_device_id vdec_dt_match[] = {
1873 	{ .compatible = "venus-decoder" },
1874 	{ }
1875 };
1876 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1877 
1878 static struct platform_driver qcom_venus_dec_driver = {
1879 	.probe = vdec_probe,
1880 	.remove = vdec_remove,
1881 	.driver = {
1882 		.name = "qcom-venus-decoder",
1883 		.of_match_table = vdec_dt_match,
1884 		.pm = &vdec_pm_ops,
1885 	},
1886 };
1887 module_platform_driver(qcom_venus_dec_driver);
1888 
1889 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1890 MODULE_LICENSE("GPL v2");
1891