xref: /linux/drivers/media/platform/qcom/venus/vdec.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
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 	struct venus_core *core = inst->core;
850 	unsigned int in_num, out_num;
851 	int ret = 0;
852 
853 	if (*num_planes) {
854 		unsigned int output_buf_size = venus_helper_get_opb_size(inst);
855 
856 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
857 		    *num_planes != inst->fmt_out->num_planes)
858 			return -EINVAL;
859 
860 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
861 		    *num_planes != inst->fmt_cap->num_planes)
862 			return -EINVAL;
863 
864 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
865 		    sizes[0] < inst->input_buf_size)
866 			return -EINVAL;
867 
868 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
869 		    sizes[0] < output_buf_size)
870 			return -EINVAL;
871 
872 		return 0;
873 	}
874 
875 	if (test_bit(0, &core->sys_error)) {
876 		if (inst->nonblock)
877 			return -EAGAIN;
878 
879 		ret = wait_event_interruptible(core->sys_err_done,
880 					       !test_bit(0, &core->sys_error));
881 		if (ret)
882 			return ret;
883 	}
884 
885 	ret = vdec_pm_get(inst);
886 	if (ret)
887 		return ret;
888 
889 	ret = vdec_session_init(inst);
890 	if (ret)
891 		goto put_power;
892 
893 	ret = vdec_num_buffers(inst, &in_num, &out_num);
894 	if (ret)
895 		goto put_power;
896 
897 	ret = vdec_pm_put(inst, false);
898 	if (ret)
899 		return ret;
900 
901 	switch (q->type) {
902 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
903 		*num_planes = inst->fmt_out->num_planes;
904 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
905 						    inst->out_width,
906 						    inst->out_height);
907 		sizes[0] = max(sizes[0], inst->input_buf_size);
908 		inst->input_buf_size = sizes[0];
909 		*num_buffers = max(*num_buffers, in_num);
910 		inst->num_input_bufs = *num_buffers;
911 		inst->num_output_bufs = out_num;
912 		break;
913 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
914 		*num_planes = inst->fmt_cap->num_planes;
915 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
916 						    inst->width,
917 						    inst->height);
918 		inst->output_buf_size = sizes[0];
919 		*num_buffers = max(*num_buffers, out_num);
920 		inst->num_output_bufs = *num_buffers;
921 
922 		mutex_lock(&inst->lock);
923 		if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
924 			inst->codec_state = VENUS_DEC_STATE_STOPPED;
925 		mutex_unlock(&inst->lock);
926 		break;
927 	default:
928 		ret = -EINVAL;
929 		break;
930 	}
931 
932 	return ret;
933 
934 put_power:
935 	vdec_pm_put(inst, false);
936 	return ret;
937 }
938 
939 static int vdec_verify_conf(struct venus_inst *inst)
940 {
941 	enum hfi_version ver = inst->core->res->hfi_version;
942 	struct hfi_buffer_requirements bufreq;
943 	int ret;
944 
945 	if (!inst->num_input_bufs || !inst->num_output_bufs)
946 		return -EINVAL;
947 
948 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
949 	if (ret)
950 		return ret;
951 
952 	if (inst->num_output_bufs < bufreq.count_actual ||
953 	    inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
954 		return -EINVAL;
955 
956 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
957 	if (ret)
958 		return ret;
959 
960 	if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
961 		return -EINVAL;
962 
963 	return 0;
964 }
965 
966 static int vdec_start_capture(struct venus_inst *inst)
967 {
968 	int ret;
969 
970 	if (!inst->streamon_out)
971 		return 0;
972 
973 	if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
974 		if (inst->reconfig)
975 			goto reconfigure;
976 
977 		venus_helper_queue_dpb_bufs(inst);
978 		venus_helper_process_initial_cap_bufs(inst);
979 		inst->streamon_cap = 1;
980 		return 0;
981 	}
982 
983 	if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
984 		return 0;
985 
986 reconfigure:
987 	ret = vdec_output_conf(inst);
988 	if (ret)
989 		return ret;
990 
991 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
992 					VB2_MAX_FRAME, VB2_MAX_FRAME);
993 	if (ret)
994 		return ret;
995 
996 	ret = venus_helper_intbufs_realloc(inst);
997 	if (ret)
998 		goto err;
999 
1000 	venus_pm_load_scale(inst);
1001 
1002 	inst->next_buf_last = false;
1003 
1004 	ret = venus_helper_alloc_dpb_bufs(inst);
1005 	if (ret)
1006 		goto err;
1007 
1008 	ret = hfi_session_continue(inst);
1009 	if (ret)
1010 		goto free_dpb_bufs;
1011 
1012 	ret = venus_helper_queue_dpb_bufs(inst);
1013 	if (ret)
1014 		goto free_dpb_bufs;
1015 
1016 	ret = venus_helper_process_initial_cap_bufs(inst);
1017 	if (ret)
1018 		goto free_dpb_bufs;
1019 
1020 	inst->codec_state = VENUS_DEC_STATE_DECODING;
1021 
1022 	if (inst->drain_active)
1023 		inst->codec_state = VENUS_DEC_STATE_DRAIN;
1024 
1025 	inst->streamon_cap = 1;
1026 	inst->sequence_cap = 0;
1027 	inst->reconfig = false;
1028 	inst->drain_active = false;
1029 
1030 	return 0;
1031 
1032 free_dpb_bufs:
1033 	venus_helper_free_dpb_bufs(inst);
1034 err:
1035 	return ret;
1036 }
1037 
1038 static int vdec_start_output(struct venus_inst *inst)
1039 {
1040 	int ret;
1041 
1042 	if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
1043 		ret = venus_helper_process_initial_out_bufs(inst);
1044 		if (inst->next_buf_last)
1045 			inst->codec_state = VENUS_DEC_STATE_DRC;
1046 		else
1047 			inst->codec_state = VENUS_DEC_STATE_DECODING;
1048 		goto done;
1049 	}
1050 
1051 	if (inst->codec_state == VENUS_DEC_STATE_INIT ||
1052 	    inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
1053 		ret = venus_helper_process_initial_out_bufs(inst);
1054 		goto done;
1055 	}
1056 
1057 	if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
1058 		return -EINVAL;
1059 
1060 	venus_helper_init_instance(inst);
1061 	inst->sequence_out = 0;
1062 	inst->reconfig = false;
1063 	inst->next_buf_last = false;
1064 
1065 	ret = vdec_set_properties(inst);
1066 	if (ret)
1067 		return ret;
1068 
1069 	ret = vdec_set_work_route(inst);
1070 	if (ret)
1071 		return ret;
1072 
1073 	ret = vdec_output_conf(inst);
1074 	if (ret)
1075 		return ret;
1076 
1077 	ret = vdec_verify_conf(inst);
1078 	if (ret)
1079 		return ret;
1080 
1081 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1082 					VB2_MAX_FRAME, VB2_MAX_FRAME);
1083 	if (ret)
1084 		return ret;
1085 
1086 	ret = venus_helper_vb2_start_streaming(inst);
1087 	if (ret)
1088 		return ret;
1089 
1090 	ret = venus_helper_process_initial_out_bufs(inst);
1091 	if (ret)
1092 		return ret;
1093 
1094 	inst->codec_state = VENUS_DEC_STATE_INIT;
1095 
1096 done:
1097 	inst->streamon_out = 1;
1098 	return ret;
1099 }
1100 
1101 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1102 {
1103 	struct venus_inst *inst = vb2_get_drv_priv(q);
1104 	int ret;
1105 
1106 	mutex_lock(&inst->lock);
1107 
1108 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1109 		ret = vdec_start_capture(inst);
1110 	} else {
1111 		ret = vdec_pm_get(inst);
1112 		if (ret)
1113 			goto error;
1114 
1115 		ret = venus_pm_acquire_core(inst);
1116 		if (ret)
1117 			goto put_power;
1118 
1119 		ret = vdec_pm_put(inst, true);
1120 		if (ret)
1121 			goto error;
1122 
1123 		ret = vdec_start_output(inst);
1124 	}
1125 
1126 	if (ret)
1127 		goto error;
1128 
1129 	mutex_unlock(&inst->lock);
1130 	return 0;
1131 
1132 put_power:
1133 	vdec_pm_put(inst, false);
1134 error:
1135 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1136 	mutex_unlock(&inst->lock);
1137 	return ret;
1138 }
1139 
1140 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1141 {
1142 	struct vb2_v4l2_buffer *buf;
1143 
1144 	while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1145 		v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1146 }
1147 
1148 static int vdec_stop_capture(struct venus_inst *inst)
1149 {
1150 	int ret = 0;
1151 
1152 	switch (inst->codec_state) {
1153 	case VENUS_DEC_STATE_DECODING:
1154 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1155 		fallthrough;
1156 	case VENUS_DEC_STATE_DRAIN:
1157 		inst->codec_state = VENUS_DEC_STATE_STOPPED;
1158 		inst->drain_active = false;
1159 		fallthrough;
1160 	case VENUS_DEC_STATE_SEEK:
1161 		vdec_cancel_dst_buffers(inst);
1162 		break;
1163 	case VENUS_DEC_STATE_DRC:
1164 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
1165 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1166 		venus_helper_free_dpb_bufs(inst);
1167 		break;
1168 	default:
1169 		break;
1170 	}
1171 
1172 	return ret;
1173 }
1174 
1175 static int vdec_stop_output(struct venus_inst *inst)
1176 {
1177 	int ret = 0;
1178 
1179 	switch (inst->codec_state) {
1180 	case VENUS_DEC_STATE_DECODING:
1181 	case VENUS_DEC_STATE_DRAIN:
1182 	case VENUS_DEC_STATE_STOPPED:
1183 	case VENUS_DEC_STATE_DRC:
1184 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1185 		inst->codec_state = VENUS_DEC_STATE_SEEK;
1186 		break;
1187 	case VENUS_DEC_STATE_INIT:
1188 	case VENUS_DEC_STATE_CAPTURE_SETUP:
1189 		ret = hfi_session_flush(inst, HFI_FLUSH_INPUT, true);
1190 		break;
1191 	default:
1192 		break;
1193 	}
1194 
1195 	return ret;
1196 }
1197 
1198 static void vdec_stop_streaming(struct vb2_queue *q)
1199 {
1200 	struct venus_inst *inst = vb2_get_drv_priv(q);
1201 	int ret = -EINVAL;
1202 
1203 	mutex_lock(&inst->lock);
1204 
1205 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1206 		ret = vdec_stop_capture(inst);
1207 	else
1208 		ret = vdec_stop_output(inst);
1209 
1210 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1211 
1212 	inst->session_error = 0;
1213 
1214 	if (ret)
1215 		goto unlock;
1216 
1217 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1218 		inst->streamon_out = 0;
1219 	else
1220 		inst->streamon_cap = 0;
1221 
1222 unlock:
1223 	mutex_unlock(&inst->lock);
1224 }
1225 
1226 static void vdec_session_release(struct venus_inst *inst)
1227 {
1228 	struct venus_core *core = inst->core;
1229 	int ret, abort = 0;
1230 
1231 	vdec_pm_get(inst);
1232 
1233 	mutex_lock(&inst->lock);
1234 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1235 
1236 	ret = hfi_session_stop(inst);
1237 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1238 	ret = hfi_session_unload_res(inst);
1239 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1240 	ret = venus_helper_unregister_bufs(inst);
1241 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1242 	ret = venus_helper_intbufs_free(inst);
1243 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1244 	ret = hfi_session_deinit(inst);
1245 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1246 
1247 	if (inst->session_error || test_bit(0, &core->sys_error))
1248 		abort = 1;
1249 
1250 	if (abort)
1251 		hfi_session_abort(inst);
1252 
1253 	venus_helper_free_dpb_bufs(inst);
1254 	venus_pm_load_scale(inst);
1255 	INIT_LIST_HEAD(&inst->registeredbufs);
1256 	mutex_unlock(&inst->lock);
1257 
1258 	venus_pm_release_core(inst);
1259 	vdec_pm_put(inst, false);
1260 }
1261 
1262 static int vdec_buf_init(struct vb2_buffer *vb)
1263 {
1264 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1265 
1266 	inst->buf_count++;
1267 
1268 	return venus_helper_vb2_buf_init(vb);
1269 }
1270 
1271 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1272 {
1273 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1274 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1275 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1276 
1277 	mutex_lock(&inst->lock);
1278 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1279 		if (!list_empty(&inst->registeredbufs))
1280 			list_del_init(&buf->reg_list);
1281 	mutex_unlock(&inst->lock);
1282 
1283 	inst->buf_count--;
1284 	if (!inst->buf_count)
1285 		vdec_session_release(inst);
1286 }
1287 
1288 static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1289 {
1290 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1291 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1292 	static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS };
1293 
1294 	vdec_pm_get_put(inst);
1295 
1296 	mutex_lock(&inst->lock);
1297 
1298 	if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1299 	    inst->codec_state == VENUS_DEC_STATE_DRC) {
1300 		vbuf->flags |= V4L2_BUF_FLAG_LAST;
1301 		vbuf->sequence = inst->sequence_cap++;
1302 		vbuf->field = V4L2_FIELD_NONE;
1303 		vb2_set_plane_payload(vb, 0, 0);
1304 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1305 		v4l2_event_queue_fh(&inst->fh, &eos);
1306 		inst->next_buf_last = false;
1307 		mutex_unlock(&inst->lock);
1308 		return;
1309 	}
1310 
1311 	venus_helper_vb2_buf_queue(vb);
1312 	mutex_unlock(&inst->lock);
1313 }
1314 
1315 static const struct vb2_ops vdec_vb2_ops = {
1316 	.queue_setup = vdec_queue_setup,
1317 	.buf_init = vdec_buf_init,
1318 	.buf_cleanup = vdec_buf_cleanup,
1319 	.buf_prepare = venus_helper_vb2_buf_prepare,
1320 	.start_streaming = vdec_start_streaming,
1321 	.stop_streaming = vdec_stop_streaming,
1322 	.buf_queue = vdec_vb2_buf_queue,
1323 };
1324 
1325 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1326 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1327 			  u32 hfi_flags, u64 timestamp_us)
1328 {
1329 	enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1330 	struct vb2_v4l2_buffer *vbuf;
1331 	struct vb2_buffer *vb;
1332 	unsigned int type;
1333 
1334 	vdec_pm_touch(inst);
1335 
1336 	if (buf_type == HFI_BUFFER_INPUT)
1337 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1338 	else
1339 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1340 
1341 	vbuf = venus_helper_find_buf(inst, type, tag);
1342 	if (!vbuf) {
1343 		venus_helper_change_dpb_owner(inst, vbuf, type, buf_type, tag);
1344 		return;
1345 	}
1346 
1347 	vbuf->flags = flags;
1348 	vbuf->field = V4L2_FIELD_NONE;
1349 	vb = &vbuf->vb2_buf;
1350 
1351 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1352 		vb2_set_plane_payload(vb, 0, bytesused);
1353 		vb->planes[0].data_offset = data_offset;
1354 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1355 		vbuf->sequence = inst->sequence_cap++;
1356 
1357 		if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1358 			const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1359 
1360 			v4l2_event_queue_fh(&inst->fh, &ev);
1361 
1362 			if (inst->codec_state == VENUS_DEC_STATE_DRAIN) {
1363 				inst->drain_active = false;
1364 				inst->codec_state = VENUS_DEC_STATE_STOPPED;
1365 			}
1366 		}
1367 
1368 		if (!bytesused)
1369 			state = VB2_BUF_STATE_ERROR;
1370 	} else {
1371 		vbuf->sequence = inst->sequence_out++;
1372 	}
1373 
1374 	venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1375 
1376 	if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1377 		venus_helper_acquire_buf_ref(vbuf);
1378 
1379 	if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1380 		state = VB2_BUF_STATE_ERROR;
1381 
1382 	if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1383 		state = VB2_BUF_STATE_ERROR;
1384 		vb2_set_plane_payload(vb, 0, 0);
1385 		vb->timestamp = 0;
1386 	}
1387 
1388 	v4l2_m2m_buf_done(vbuf, state);
1389 }
1390 
1391 static void vdec_event_change(struct venus_inst *inst,
1392 			      struct hfi_event_data *ev_data, bool sufficient)
1393 {
1394 	static const struct v4l2_event ev = {
1395 		.type = V4L2_EVENT_SOURCE_CHANGE,
1396 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1397 	struct device *dev = inst->core->dev_dec;
1398 	struct v4l2_format format = {};
1399 
1400 	mutex_lock(&inst->lock);
1401 
1402 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1403 	format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1404 	format.fmt.pix_mp.width = ev_data->width;
1405 	format.fmt.pix_mp.height = ev_data->height;
1406 
1407 	vdec_try_fmt_common(inst, &format);
1408 
1409 	inst->width = format.fmt.pix_mp.width;
1410 	inst->height = format.fmt.pix_mp.height;
1411 	/*
1412 	 * Some versions of the firmware do not report crop information for
1413 	 * all codecs. For these cases, set the crop to the coded resolution.
1414 	 */
1415 	if (ev_data->input_crop.width > 0 && ev_data->input_crop.height > 0) {
1416 		inst->crop.left = ev_data->input_crop.left;
1417 		inst->crop.top = ev_data->input_crop.top;
1418 		inst->crop.width = ev_data->input_crop.width;
1419 		inst->crop.height = ev_data->input_crop.height;
1420 	} else {
1421 		inst->crop.left = 0;
1422 		inst->crop.top = 0;
1423 		inst->crop.width = ev_data->width;
1424 		inst->crop.height = ev_data->height;
1425 	}
1426 
1427 	inst->fw_min_cnt = ev_data->buf_count;
1428 	/* overwriting this to 11 for vp9 due to fw bug */
1429 	if (inst->hfi_codec == HFI_VIDEO_CODEC_VP9)
1430 		inst->fw_min_cnt = 11;
1431 
1432 	inst->out_width = ev_data->width;
1433 	inst->out_height = ev_data->height;
1434 
1435 	if (inst->bit_depth != ev_data->bit_depth)
1436 		inst->bit_depth = ev_data->bit_depth;
1437 
1438 	if (inst->pic_struct != ev_data->pic_struct)
1439 		inst->pic_struct = ev_data->pic_struct;
1440 
1441 	dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n",
1442 		sufficient ? "" : "not", ev_data->width, ev_data->height);
1443 
1444 	switch (inst->codec_state) {
1445 	case VENUS_DEC_STATE_INIT:
1446 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1447 		break;
1448 	case VENUS_DEC_STATE_DECODING:
1449 	case VENUS_DEC_STATE_DRAIN:
1450 		inst->codec_state = VENUS_DEC_STATE_DRC;
1451 		break;
1452 	default:
1453 		break;
1454 	}
1455 
1456 	/*
1457 	 * The assumption is that the firmware have to return the last buffer
1458 	 * before this event is received in the v4l2 driver. Also the firmware
1459 	 * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1460 	 */
1461 
1462 	if (inst->codec_state == VENUS_DEC_STATE_DRC) {
1463 		int ret;
1464 
1465 		inst->next_buf_last = true;
1466 
1467 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1468 		if (ret)
1469 			dev_dbg(dev, VDBGH "flush output error %d\n", ret);
1470 	}
1471 
1472 	inst->next_buf_last = true;
1473 	inst->reconfig = true;
1474 	v4l2_event_queue_fh(&inst->fh, &ev);
1475 	wake_up(&inst->reconf_wait);
1476 
1477 	mutex_unlock(&inst->lock);
1478 }
1479 
1480 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1481 			      struct hfi_event_data *data)
1482 {
1483 	struct venus_core *core = inst->core;
1484 	struct device *dev = core->dev_dec;
1485 
1486 	vdec_pm_touch(inst);
1487 
1488 	switch (event) {
1489 	case EVT_SESSION_ERROR:
1490 		inst->session_error = true;
1491 		venus_helper_vb2_queue_error(inst);
1492 		dev_err(dev, "dec: event session error %x\n", inst->error);
1493 		break;
1494 	case EVT_SYS_EVENT_CHANGE:
1495 		switch (data->event_type) {
1496 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1497 			vdec_event_change(inst, data, true);
1498 			break;
1499 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1500 			vdec_event_change(inst, data, false);
1501 			break;
1502 		case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1503 			venus_helper_release_buf_ref(inst, data->tag);
1504 			break;
1505 		default:
1506 			break;
1507 		}
1508 		break;
1509 	default:
1510 		break;
1511 	}
1512 }
1513 
1514 static void vdec_flush_done(struct venus_inst *inst)
1515 {
1516 	dev_dbg(inst->core->dev_dec, VDBGH "flush done\n");
1517 }
1518 
1519 static const struct hfi_inst_ops vdec_hfi_ops = {
1520 	.buf_done = vdec_buf_done,
1521 	.event_notify = vdec_event_notify,
1522 	.flush_done = vdec_flush_done,
1523 };
1524 
1525 static void vdec_inst_init(struct venus_inst *inst)
1526 {
1527 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1528 	inst->fmt_out = &vdec_formats[6];
1529 	inst->fmt_cap = &vdec_formats[0];
1530 	inst->width = frame_width_min(inst);
1531 	inst->height = ALIGN(frame_height_min(inst), 32);
1532 	inst->crop.left = 0;
1533 	inst->crop.top = 0;
1534 	inst->crop.width = inst->width;
1535 	inst->crop.height = inst->height;
1536 	inst->fw_min_cnt = 8;
1537 	inst->out_width = frame_width_min(inst);
1538 	inst->out_height = frame_height_min(inst);
1539 	inst->fps = 30;
1540 	inst->timeperframe.numerator = 1;
1541 	inst->timeperframe.denominator = 30;
1542 	inst->opb_buftype = HFI_BUFFER_OUTPUT;
1543 }
1544 
1545 static void vdec_m2m_device_run(void *priv)
1546 {
1547 }
1548 
1549 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1550 	.device_run = vdec_m2m_device_run,
1551 	.job_abort = venus_helper_m2m_job_abort,
1552 };
1553 
1554 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1555 			  struct vb2_queue *dst_vq)
1556 {
1557 	struct venus_inst *inst = priv;
1558 	int ret;
1559 
1560 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1561 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1562 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1563 	src_vq->ops = &vdec_vb2_ops;
1564 	src_vq->mem_ops = &vb2_dma_contig_memops;
1565 	src_vq->drv_priv = inst;
1566 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1567 	src_vq->allow_zero_bytesused = 1;
1568 	src_vq->min_buffers_needed = 0;
1569 	src_vq->dev = inst->core->dev;
1570 	ret = vb2_queue_init(src_vq);
1571 	if (ret)
1572 		return ret;
1573 
1574 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1575 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1576 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1577 	dst_vq->ops = &vdec_vb2_ops;
1578 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1579 	dst_vq->drv_priv = inst;
1580 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1581 	dst_vq->allow_zero_bytesused = 1;
1582 	dst_vq->min_buffers_needed = 0;
1583 	dst_vq->dev = inst->core->dev;
1584 	return vb2_queue_init(dst_vq);
1585 }
1586 
1587 static int vdec_open(struct file *file)
1588 {
1589 	struct venus_core *core = video_drvdata(file);
1590 	struct venus_inst *inst;
1591 	int ret;
1592 
1593 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1594 	if (!inst)
1595 		return -ENOMEM;
1596 
1597 	INIT_LIST_HEAD(&inst->dpbbufs);
1598 	INIT_LIST_HEAD(&inst->registeredbufs);
1599 	INIT_LIST_HEAD(&inst->internalbufs);
1600 	INIT_LIST_HEAD(&inst->list);
1601 	mutex_init(&inst->lock);
1602 
1603 	inst->core = core;
1604 	inst->session_type = VIDC_SESSION_TYPE_DEC;
1605 	inst->num_output_bufs = 1;
1606 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1607 	inst->buf_count = 0;
1608 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1609 	inst->core_acquired = false;
1610 	inst->bit_depth = VIDC_BITDEPTH_8;
1611 	inst->pic_struct = HFI_INTERLACE_FRAME_PROGRESSIVE;
1612 	init_waitqueue_head(&inst->reconf_wait);
1613 	inst->nonblock = file->f_flags & O_NONBLOCK;
1614 
1615 	venus_helper_init_instance(inst);
1616 
1617 	ret = vdec_ctrl_init(inst);
1618 	if (ret)
1619 		goto err_free;
1620 
1621 	ret = hfi_session_create(inst, &vdec_hfi_ops);
1622 	if (ret)
1623 		goto err_ctrl_deinit;
1624 
1625 	vdec_inst_init(inst);
1626 
1627 	ida_init(&inst->dpb_ids);
1628 
1629 	/*
1630 	 * create m2m device for every instance, the m2m context scheduling
1631 	 * is made by firmware side so we do not need to care about.
1632 	 */
1633 	inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1634 	if (IS_ERR(inst->m2m_dev)) {
1635 		ret = PTR_ERR(inst->m2m_dev);
1636 		goto err_session_destroy;
1637 	}
1638 
1639 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1640 	if (IS_ERR(inst->m2m_ctx)) {
1641 		ret = PTR_ERR(inst->m2m_ctx);
1642 		goto err_m2m_release;
1643 	}
1644 
1645 	v4l2_fh_init(&inst->fh, core->vdev_dec);
1646 
1647 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1648 	v4l2_fh_add(&inst->fh);
1649 	inst->fh.m2m_ctx = inst->m2m_ctx;
1650 	file->private_data = &inst->fh;
1651 
1652 	return 0;
1653 
1654 err_m2m_release:
1655 	v4l2_m2m_release(inst->m2m_dev);
1656 err_session_destroy:
1657 	hfi_session_destroy(inst);
1658 err_ctrl_deinit:
1659 	vdec_ctrl_deinit(inst);
1660 err_free:
1661 	kfree(inst);
1662 	return ret;
1663 }
1664 
1665 static int vdec_close(struct file *file)
1666 {
1667 	struct venus_inst *inst = to_inst(file);
1668 
1669 	vdec_pm_get(inst);
1670 
1671 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1672 	v4l2_m2m_release(inst->m2m_dev);
1673 	vdec_ctrl_deinit(inst);
1674 	ida_destroy(&inst->dpb_ids);
1675 	hfi_session_destroy(inst);
1676 	mutex_destroy(&inst->lock);
1677 	v4l2_fh_del(&inst->fh);
1678 	v4l2_fh_exit(&inst->fh);
1679 
1680 	vdec_pm_put(inst, false);
1681 
1682 	kfree(inst);
1683 	return 0;
1684 }
1685 
1686 static const struct v4l2_file_operations vdec_fops = {
1687 	.owner = THIS_MODULE,
1688 	.open = vdec_open,
1689 	.release = vdec_close,
1690 	.unlocked_ioctl = video_ioctl2,
1691 	.poll = v4l2_m2m_fop_poll,
1692 	.mmap = v4l2_m2m_fop_mmap,
1693 };
1694 
1695 static int vdec_probe(struct platform_device *pdev)
1696 {
1697 	struct device *dev = &pdev->dev;
1698 	struct video_device *vdev;
1699 	struct venus_core *core;
1700 	int ret;
1701 
1702 	if (!dev->parent)
1703 		return -EPROBE_DEFER;
1704 
1705 	core = dev_get_drvdata(dev->parent);
1706 	if (!core)
1707 		return -EPROBE_DEFER;
1708 
1709 	platform_set_drvdata(pdev, core);
1710 
1711 	if (core->pm_ops->vdec_get) {
1712 		ret = core->pm_ops->vdec_get(dev);
1713 		if (ret)
1714 			return ret;
1715 	}
1716 
1717 	vdev = video_device_alloc();
1718 	if (!vdev)
1719 		return -ENOMEM;
1720 
1721 	strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1722 	vdev->release = video_device_release;
1723 	vdev->fops = &vdec_fops;
1724 	vdev->ioctl_ops = &vdec_ioctl_ops;
1725 	vdev->vfl_dir = VFL_DIR_M2M;
1726 	vdev->v4l2_dev = &core->v4l2_dev;
1727 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1728 
1729 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1730 	if (ret)
1731 		goto err_vdev_release;
1732 
1733 	core->vdev_dec = vdev;
1734 	core->dev_dec = dev;
1735 
1736 	video_set_drvdata(vdev, core);
1737 	pm_runtime_set_autosuspend_delay(dev, 2000);
1738 	pm_runtime_use_autosuspend(dev);
1739 	pm_runtime_enable(dev);
1740 
1741 	return 0;
1742 
1743 err_vdev_release:
1744 	video_device_release(vdev);
1745 	return ret;
1746 }
1747 
1748 static int vdec_remove(struct platform_device *pdev)
1749 {
1750 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1751 
1752 	video_unregister_device(core->vdev_dec);
1753 	pm_runtime_disable(core->dev_dec);
1754 
1755 	if (core->pm_ops->vdec_put)
1756 		core->pm_ops->vdec_put(core->dev_dec);
1757 
1758 	return 0;
1759 }
1760 
1761 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1762 {
1763 	struct venus_core *core = dev_get_drvdata(dev);
1764 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1765 	int ret = 0;
1766 
1767 	if (pm_ops->vdec_power)
1768 		ret = pm_ops->vdec_power(dev, POWER_OFF);
1769 
1770 	return ret;
1771 }
1772 
1773 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1774 {
1775 	struct venus_core *core = dev_get_drvdata(dev);
1776 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1777 	int ret = 0;
1778 
1779 	if (pm_ops->vdec_power)
1780 		ret = pm_ops->vdec_power(dev, POWER_ON);
1781 
1782 	return ret;
1783 }
1784 
1785 static const struct dev_pm_ops vdec_pm_ops = {
1786 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1787 				pm_runtime_force_resume)
1788 	SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1789 };
1790 
1791 static const struct of_device_id vdec_dt_match[] = {
1792 	{ .compatible = "venus-decoder" },
1793 	{ }
1794 };
1795 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1796 
1797 static struct platform_driver qcom_venus_dec_driver = {
1798 	.probe = vdec_probe,
1799 	.remove = vdec_remove,
1800 	.driver = {
1801 		.name = "qcom-venus-decoder",
1802 		.of_match_table = vdec_dt_match,
1803 		.pm = &vdec_pm_ops,
1804 	},
1805 };
1806 module_platform_driver(qcom_venus_dec_driver);
1807 
1808 MODULE_ALIAS("platform:qcom-venus-decoder");
1809 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1810 MODULE_LICENSE("GPL v2");
1811