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