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