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