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 vb2_queue *dst_vq;
499 	struct hfi_frame_data fdata = {0};
500 	int ret;
501 
502 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
503 	if (ret)
504 		return ret;
505 
506 	mutex_lock(&inst->lock);
507 
508 	if (cmd->cmd == V4L2_DEC_CMD_STOP) {
509 		/*
510 		 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
511 		 * decoder input to signal EOS.
512 		 */
513 		if (!(inst->streamon_out && inst->streamon_cap))
514 			goto unlock;
515 
516 		fdata.buffer_type = HFI_BUFFER_INPUT;
517 		fdata.flags |= HFI_BUFFERFLAG_EOS;
518 		fdata.device_addr = 0xdeadb000;
519 
520 		ret = hfi_session_process_buf(inst, &fdata);
521 
522 		if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING) {
523 			inst->codec_state = VENUS_DEC_STATE_DRAIN;
524 			inst->drain_active = true;
525 		}
526 	} else if (cmd->cmd == V4L2_DEC_CMD_START &&
527 		   inst->codec_state == VENUS_DEC_STATE_STOPPED) {
528 		dst_vq = v4l2_m2m_get_vq(inst->fh.m2m_ctx,
529 					 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
530 		vb2_clear_last_buffer_dequeued(dst_vq);
531 
532 		inst->codec_state = VENUS_DEC_STATE_DECODING;
533 	}
534 
535 unlock:
536 	mutex_unlock(&inst->lock);
537 	return ret;
538 }
539 
540 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
541 	.vidioc_querycap = vdec_querycap,
542 	.vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
543 	.vidioc_enum_fmt_vid_out = vdec_enum_fmt,
544 	.vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
545 	.vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
546 	.vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
547 	.vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
548 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
549 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
550 	.vidioc_g_selection = vdec_g_selection,
551 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
552 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
553 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
554 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
555 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
556 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
557 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
558 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
559 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
560 	.vidioc_s_parm = vdec_s_parm,
561 	.vidioc_enum_framesizes = vdec_enum_framesizes,
562 	.vidioc_subscribe_event = vdec_subscribe_event,
563 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
564 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
565 	.vidioc_decoder_cmd = vdec_decoder_cmd,
566 };
567 
vdec_pm_get(struct venus_inst *inst)568 static int vdec_pm_get(struct venus_inst *inst)
569 {
570 	struct venus_core *core = inst->core;
571 	struct device *dev = core->dev_dec;
572 	int ret;
573 
574 	mutex_lock(&core->pm_lock);
575 	ret = pm_runtime_get_sync(dev);
576 	mutex_unlock(&core->pm_lock);
577 
578 	return ret < 0 ? ret : 0;
579 }
580 
vdec_pm_put(struct venus_inst *inst, bool autosuspend)581 static int vdec_pm_put(struct venus_inst *inst, bool autosuspend)
582 {
583 	struct venus_core *core = inst->core;
584 	struct device *dev = core->dev_dec;
585 	int ret;
586 
587 	mutex_lock(&core->pm_lock);
588 
589 	if (autosuspend)
590 		ret = pm_runtime_put_autosuspend(dev);
591 	else
592 		ret = pm_runtime_put_sync(dev);
593 
594 	mutex_unlock(&core->pm_lock);
595 
596 	return ret < 0 ? ret : 0;
597 }
598 
vdec_pm_get_put(struct venus_inst *inst)599 static int vdec_pm_get_put(struct venus_inst *inst)
600 {
601 	struct venus_core *core = inst->core;
602 	struct device *dev = core->dev_dec;
603 	int ret = 0;
604 
605 	mutex_lock(&core->pm_lock);
606 
607 	if (pm_runtime_suspended(dev)) {
608 		ret = pm_runtime_get_sync(dev);
609 		if (ret < 0)
610 			goto error;
611 
612 		ret = pm_runtime_put_autosuspend(dev);
613 	}
614 
615 error:
616 	mutex_unlock(&core->pm_lock);
617 
618 	return ret < 0 ? ret : 0;
619 }
620 
vdec_pm_touch(struct venus_inst *inst)621 static void vdec_pm_touch(struct venus_inst *inst)
622 {
623 	pm_runtime_mark_last_busy(inst->core->dev_dec);
624 }
625 
vdec_set_properties(struct venus_inst *inst)626 static int vdec_set_properties(struct venus_inst *inst)
627 {
628 	struct vdec_controls *ctr = &inst->controls.dec;
629 	struct hfi_enable en = { .enable = 1 };
630 	u32 ptype;
631 	int ret;
632 
633 	if (ctr->post_loop_deb_mode) {
634 		ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
635 		ret = hfi_session_set_property(inst, ptype, &en);
636 		if (ret)
637 			return ret;
638 	}
639 
640 	return 0;
641 }
642 
643 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
644 
vdec_output_conf(struct venus_inst *inst)645 static int vdec_output_conf(struct venus_inst *inst)
646 {
647 	struct venus_core *core = inst->core;
648 	struct hfi_enable en = { .enable = 1 };
649 	struct hfi_buffer_requirements bufreq;
650 	u32 width = inst->out_width;
651 	u32 height = inst->out_height;
652 	u32 out_fmt, out2_fmt;
653 	bool ubwc = false;
654 	u32 ptype;
655 	int ret;
656 
657 	ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
658 	if (ret)
659 		return ret;
660 
661 	if (core->res->hfi_version == HFI_VERSION_1XX) {
662 		ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
663 		ret = hfi_session_set_property(inst, ptype, &en);
664 		if (ret)
665 			return ret;
666 	}
667 
668 	/* Force searching UBWC formats for bigger then HD resolutions */
669 	if (width > 1920 && height > ALIGN(1080, 32))
670 		ubwc = true;
671 
672 	/* For Venus v4 UBWC format is mandatory */
673 	if (IS_V4(core))
674 		ubwc = true;
675 
676 	ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
677 					&out2_fmt, ubwc);
678 	if (ret)
679 		return ret;
680 
681 	inst->output_buf_size =
682 			venus_helper_get_framesz_raw(out_fmt, width, height);
683 	inst->output2_buf_size =
684 			venus_helper_get_framesz_raw(out2_fmt, width, height);
685 
686 	if (is_ubwc_fmt(out_fmt)) {
687 		inst->opb_buftype = HFI_BUFFER_OUTPUT2;
688 		inst->opb_fmt = out2_fmt;
689 		inst->dpb_buftype = HFI_BUFFER_OUTPUT;
690 		inst->dpb_fmt = out_fmt;
691 	} else if (is_ubwc_fmt(out2_fmt)) {
692 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
693 		inst->opb_fmt = out_fmt;
694 		inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
695 		inst->dpb_fmt = out2_fmt;
696 	} else {
697 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
698 		inst->opb_fmt = out_fmt;
699 		inst->dpb_buftype = 0;
700 		inst->dpb_fmt = 0;
701 	}
702 
703 	ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
704 					  inst->opb_buftype);
705 	if (ret)
706 		return ret;
707 
708 	if (inst->dpb_fmt) {
709 		ret = venus_helper_set_multistream(inst, false, true);
710 		if (ret)
711 			return ret;
712 
713 		ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
714 						  inst->dpb_buftype);
715 		if (ret)
716 			return ret;
717 
718 		ret = venus_helper_set_output_resolution(inst, width, height,
719 							 HFI_BUFFER_OUTPUT2);
720 		if (ret)
721 			return ret;
722 	}
723 
724 	if (IS_V3(core) || IS_V4(core)) {
725 		ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
726 		if (ret)
727 			return ret;
728 
729 		if (bufreq.size > inst->output_buf_size)
730 			return -EINVAL;
731 
732 		if (inst->dpb_fmt) {
733 			ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT2,
734 						      &bufreq);
735 			if (ret)
736 				return ret;
737 
738 			if (bufreq.size > inst->output2_buf_size)
739 				return -EINVAL;
740 		}
741 
742 		if (inst->output2_buf_size) {
743 			ret = venus_helper_set_bufsize(inst,
744 						       inst->output2_buf_size,
745 						       HFI_BUFFER_OUTPUT2);
746 			if (ret)
747 				return ret;
748 		}
749 
750 		if (inst->output_buf_size) {
751 			ret = venus_helper_set_bufsize(inst,
752 						       inst->output_buf_size,
753 						       HFI_BUFFER_OUTPUT);
754 			if (ret)
755 				return ret;
756 		}
757 	}
758 
759 	ret = venus_helper_set_dyn_bufmode(inst);
760 	if (ret)
761 		return ret;
762 
763 	return 0;
764 }
765 
vdec_session_init(struct venus_inst *inst)766 static int vdec_session_init(struct venus_inst *inst)
767 {
768 	int ret;
769 
770 	ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
771 	if (ret == -EINVAL)
772 		return 0;
773 	else if (ret)
774 		return ret;
775 
776 	ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
777 						frame_height_min(inst));
778 	if (ret)
779 		goto deinit;
780 
781 	ret = venus_helper_init_codec_freq_data(inst);
782 	if (ret)
783 		goto deinit;
784 
785 	return 0;
786 deinit:
787 	hfi_session_deinit(inst);
788 	return ret;
789 }
790 
vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num, unsigned int *out_num)791 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
792 			    unsigned int *out_num)
793 {
794 	enum hfi_version ver = inst->core->res->hfi_version;
795 	struct hfi_buffer_requirements bufreq;
796 	int ret;
797 
798 	*in_num = *out_num = 0;
799 
800 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
801 	if (ret)
802 		return ret;
803 
804 	*in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
805 
806 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
807 	if (ret)
808 		return ret;
809 
810 	*out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
811 
812 	return 0;
813 }
814 
vdec_queue_setup(struct vb2_queue *q, unsigned int *num_buffers, unsigned int *num_planes, unsigned int sizes[], struct device *alloc_devs[])815 static int vdec_queue_setup(struct vb2_queue *q,
816 			    unsigned int *num_buffers, unsigned int *num_planes,
817 			    unsigned int sizes[], struct device *alloc_devs[])
818 {
819 	struct venus_inst *inst = vb2_get_drv_priv(q);
820 	unsigned int in_num, out_num;
821 	int ret = 0;
822 
823 	if (*num_planes) {
824 		unsigned int output_buf_size = venus_helper_get_opb_size(inst);
825 
826 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
827 		    *num_planes != inst->fmt_out->num_planes)
828 			return -EINVAL;
829 
830 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
831 		    *num_planes != inst->fmt_cap->num_planes)
832 			return -EINVAL;
833 
834 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
835 		    sizes[0] < inst->input_buf_size)
836 			return -EINVAL;
837 
838 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
839 		    sizes[0] < output_buf_size)
840 			return -EINVAL;
841 
842 		return 0;
843 	}
844 
845 	ret = vdec_pm_get(inst);
846 	if (ret)
847 		return ret;
848 
849 	ret = vdec_session_init(inst);
850 	if (ret)
851 		goto put_power;
852 
853 	ret = vdec_num_buffers(inst, &in_num, &out_num);
854 	if (ret)
855 		goto put_power;
856 
857 	ret = vdec_pm_put(inst, false);
858 	if (ret)
859 		return ret;
860 
861 	switch (q->type) {
862 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
863 		*num_planes = inst->fmt_out->num_planes;
864 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
865 						    inst->out_width,
866 						    inst->out_height);
867 		sizes[0] = max(sizes[0], inst->input_buf_size);
868 		inst->input_buf_size = sizes[0];
869 		*num_buffers = max(*num_buffers, in_num);
870 		inst->num_input_bufs = *num_buffers;
871 		inst->num_output_bufs = out_num;
872 		break;
873 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
874 		*num_planes = inst->fmt_cap->num_planes;
875 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
876 						    inst->width,
877 						    inst->height);
878 		inst->output_buf_size = sizes[0];
879 		*num_buffers = max(*num_buffers, out_num);
880 		inst->num_output_bufs = *num_buffers;
881 
882 		mutex_lock(&inst->lock);
883 		if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
884 			inst->codec_state = VENUS_DEC_STATE_STOPPED;
885 		mutex_unlock(&inst->lock);
886 		break;
887 	default:
888 		ret = -EINVAL;
889 		break;
890 	}
891 
892 	return ret;
893 
894 put_power:
895 	vdec_pm_put(inst, false);
896 	return ret;
897 }
898 
vdec_verify_conf(struct venus_inst *inst)899 static int vdec_verify_conf(struct venus_inst *inst)
900 {
901 	enum hfi_version ver = inst->core->res->hfi_version;
902 	struct hfi_buffer_requirements bufreq;
903 	int ret;
904 
905 	if (!inst->num_input_bufs || !inst->num_output_bufs)
906 		return -EINVAL;
907 
908 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
909 	if (ret)
910 		return ret;
911 
912 	if (inst->num_output_bufs < bufreq.count_actual ||
913 	    inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
914 		return -EINVAL;
915 
916 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
917 	if (ret)
918 		return ret;
919 
920 	if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
921 		return -EINVAL;
922 
923 	return 0;
924 }
925 
vdec_start_capture(struct venus_inst *inst)926 static int vdec_start_capture(struct venus_inst *inst)
927 {
928 	int ret;
929 
930 	if (!inst->streamon_out)
931 		return 0;
932 
933 	if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
934 		if (inst->reconfig)
935 			goto reconfigure;
936 
937 		venus_helper_queue_dpb_bufs(inst);
938 		venus_helper_process_initial_cap_bufs(inst);
939 		inst->streamon_cap = 1;
940 		return 0;
941 	}
942 
943 	if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
944 		return 0;
945 
946 reconfigure:
947 	ret = vdec_output_conf(inst);
948 	if (ret)
949 		return ret;
950 
951 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
952 					VB2_MAX_FRAME, VB2_MAX_FRAME);
953 	if (ret)
954 		return ret;
955 
956 	ret = venus_helper_intbufs_realloc(inst);
957 	if (ret)
958 		goto err;
959 
960 	ret = venus_helper_alloc_dpb_bufs(inst);
961 	if (ret)
962 		goto err;
963 
964 	ret = venus_helper_queue_dpb_bufs(inst);
965 	if (ret)
966 		goto free_dpb_bufs;
967 
968 	ret = venus_helper_process_initial_cap_bufs(inst);
969 	if (ret)
970 		goto free_dpb_bufs;
971 
972 	venus_pm_load_scale(inst);
973 
974 	inst->next_buf_last = false;
975 
976 	ret = hfi_session_continue(inst);
977 	if (ret)
978 		goto free_dpb_bufs;
979 
980 	inst->codec_state = VENUS_DEC_STATE_DECODING;
981 
982 	if (inst->drain_active)
983 		inst->codec_state = VENUS_DEC_STATE_DRAIN;
984 
985 	inst->streamon_cap = 1;
986 	inst->sequence_cap = 0;
987 	inst->reconfig = false;
988 	inst->drain_active = false;
989 
990 	return 0;
991 
992 free_dpb_bufs:
993 	venus_helper_free_dpb_bufs(inst);
994 err:
995 	return ret;
996 }
997 
vdec_start_output(struct venus_inst *inst)998 static int vdec_start_output(struct venus_inst *inst)
999 {
1000 	int ret;
1001 
1002 	if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
1003 		ret = venus_helper_process_initial_out_bufs(inst);
1004 		if (inst->next_buf_last)
1005 			inst->codec_state = VENUS_DEC_STATE_DRC;
1006 		else
1007 			inst->codec_state = VENUS_DEC_STATE_DECODING;
1008 		goto done;
1009 	}
1010 
1011 	if (inst->codec_state == VENUS_DEC_STATE_INIT ||
1012 	    inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
1013 		ret = venus_helper_process_initial_out_bufs(inst);
1014 		goto done;
1015 	}
1016 
1017 	if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
1018 		return -EINVAL;
1019 
1020 	venus_helper_init_instance(inst);
1021 	inst->sequence_out = 0;
1022 	inst->reconfig = false;
1023 	inst->next_buf_last = false;
1024 
1025 	ret = vdec_set_properties(inst);
1026 	if (ret)
1027 		return ret;
1028 
1029 	ret = vdec_output_conf(inst);
1030 	if (ret)
1031 		return ret;
1032 
1033 	ret = vdec_verify_conf(inst);
1034 	if (ret)
1035 		return ret;
1036 
1037 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1038 					VB2_MAX_FRAME, VB2_MAX_FRAME);
1039 	if (ret)
1040 		return ret;
1041 
1042 	ret = venus_helper_vb2_start_streaming(inst);
1043 	if (ret)
1044 		return ret;
1045 
1046 	ret = venus_helper_process_initial_out_bufs(inst);
1047 	if (ret)
1048 		return ret;
1049 
1050 	inst->codec_state = VENUS_DEC_STATE_INIT;
1051 
1052 done:
1053 	inst->streamon_out = 1;
1054 	return ret;
1055 }
1056 
vdec_start_streaming(struct vb2_queue *q, unsigned int count)1057 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1058 {
1059 	struct venus_inst *inst = vb2_get_drv_priv(q);
1060 	int ret;
1061 
1062 	mutex_lock(&inst->lock);
1063 
1064 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1065 		ret = vdec_start_capture(inst);
1066 	} else {
1067 		ret = vdec_pm_get(inst);
1068 		if (ret)
1069 			goto error;
1070 
1071 		ret = venus_pm_acquire_core(inst);
1072 		if (ret)
1073 			goto put_power;
1074 
1075 		ret = vdec_pm_put(inst, true);
1076 		if (ret)
1077 			goto error;
1078 
1079 		ret = vdec_start_output(inst);
1080 	}
1081 
1082 	if (ret)
1083 		goto error;
1084 
1085 	mutex_unlock(&inst->lock);
1086 	return 0;
1087 
1088 put_power:
1089 	vdec_pm_put(inst, false);
1090 error:
1091 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1092 	mutex_unlock(&inst->lock);
1093 	return ret;
1094 }
1095 
vdec_cancel_dst_buffers(struct venus_inst *inst)1096 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1097 {
1098 	struct vb2_v4l2_buffer *buf;
1099 
1100 	while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1101 		v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1102 }
1103 
vdec_stop_capture(struct venus_inst *inst)1104 static int vdec_stop_capture(struct venus_inst *inst)
1105 {
1106 	int ret = 0;
1107 
1108 	switch (inst->codec_state) {
1109 	case VENUS_DEC_STATE_DECODING:
1110 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1111 		fallthrough;
1112 	case VENUS_DEC_STATE_DRAIN:
1113 		inst->codec_state = VENUS_DEC_STATE_STOPPED;
1114 		inst->drain_active = false;
1115 		fallthrough;
1116 	case VENUS_DEC_STATE_SEEK:
1117 		vdec_cancel_dst_buffers(inst);
1118 		break;
1119 	case VENUS_DEC_STATE_DRC:
1120 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
1121 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1122 		venus_helper_free_dpb_bufs(inst);
1123 		break;
1124 	default:
1125 		break;
1126 	}
1127 
1128 	return ret;
1129 }
1130 
vdec_stop_output(struct venus_inst *inst)1131 static int vdec_stop_output(struct venus_inst *inst)
1132 {
1133 	int ret = 0;
1134 
1135 	switch (inst->codec_state) {
1136 	case VENUS_DEC_STATE_DECODING:
1137 	case VENUS_DEC_STATE_DRAIN:
1138 	case VENUS_DEC_STATE_STOPPED:
1139 	case VENUS_DEC_STATE_DRC:
1140 		ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1141 		inst->codec_state = VENUS_DEC_STATE_SEEK;
1142 		break;
1143 	case VENUS_DEC_STATE_INIT:
1144 	case VENUS_DEC_STATE_CAPTURE_SETUP:
1145 		ret = hfi_session_flush(inst, HFI_FLUSH_INPUT, true);
1146 		break;
1147 	default:
1148 		break;
1149 	}
1150 
1151 	return ret;
1152 }
1153 
vdec_stop_streaming(struct vb2_queue *q)1154 static void vdec_stop_streaming(struct vb2_queue *q)
1155 {
1156 	struct venus_inst *inst = vb2_get_drv_priv(q);
1157 	int ret = -EINVAL;
1158 
1159 	mutex_lock(&inst->lock);
1160 
1161 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1162 		ret = vdec_stop_capture(inst);
1163 	else
1164 		ret = vdec_stop_output(inst);
1165 
1166 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1167 
1168 	if (ret)
1169 		goto unlock;
1170 
1171 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1172 		inst->streamon_out = 0;
1173 	else
1174 		inst->streamon_cap = 0;
1175 
1176 unlock:
1177 	mutex_unlock(&inst->lock);
1178 }
1179 
vdec_session_release(struct venus_inst *inst)1180 static void vdec_session_release(struct venus_inst *inst)
1181 {
1182 	struct venus_core *core = inst->core;
1183 	int ret, abort = 0;
1184 
1185 	vdec_pm_get(inst);
1186 
1187 	mutex_lock(&inst->lock);
1188 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1189 
1190 	ret = hfi_session_stop(inst);
1191 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1192 	ret = hfi_session_unload_res(inst);
1193 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1194 	ret = venus_helper_unregister_bufs(inst);
1195 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1196 	ret = venus_helper_intbufs_free(inst);
1197 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1198 	ret = hfi_session_deinit(inst);
1199 	abort = (ret && ret != -EINVAL) ? 1 : 0;
1200 
1201 	if (inst->session_error || core->sys_error)
1202 		abort = 1;
1203 
1204 	if (abort)
1205 		hfi_session_abort(inst);
1206 
1207 	venus_helper_free_dpb_bufs(inst);
1208 	venus_pm_load_scale(inst);
1209 	INIT_LIST_HEAD(&inst->registeredbufs);
1210 	mutex_unlock(&inst->lock);
1211 
1212 	venus_pm_release_core(inst);
1213 	vdec_pm_put(inst, false);
1214 }
1215 
vdec_buf_init(struct vb2_buffer *vb)1216 static int vdec_buf_init(struct vb2_buffer *vb)
1217 {
1218 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1219 
1220 	inst->buf_count++;
1221 
1222 	return venus_helper_vb2_buf_init(vb);
1223 }
1224 
vdec_buf_cleanup(struct vb2_buffer *vb)1225 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1226 {
1227 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1228 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1229 	struct venus_buffer *buf = to_venus_buffer(vbuf);
1230 
1231 	mutex_lock(&inst->lock);
1232 	if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1233 		if (!list_empty(&inst->registeredbufs))
1234 			list_del_init(&buf->reg_list);
1235 	mutex_unlock(&inst->lock);
1236 
1237 	inst->buf_count--;
1238 	if (!inst->buf_count)
1239 		vdec_session_release(inst);
1240 }
1241 
vdec_vb2_buf_queue(struct vb2_buffer *vb)1242 static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1243 {
1244 	struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1245 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1246 	static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS };
1247 
1248 	vdec_pm_get_put(inst);
1249 
1250 	mutex_lock(&inst->lock);
1251 
1252 	if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1253 	    inst->codec_state == VENUS_DEC_STATE_DRC) {
1254 		vbuf->flags |= V4L2_BUF_FLAG_LAST;
1255 		vbuf->sequence = inst->sequence_cap++;
1256 		vbuf->field = V4L2_FIELD_NONE;
1257 		vb2_set_plane_payload(vb, 0, 0);
1258 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1259 		v4l2_event_queue_fh(&inst->fh, &eos);
1260 		inst->next_buf_last = false;
1261 		mutex_unlock(&inst->lock);
1262 		return;
1263 	}
1264 
1265 	mutex_unlock(&inst->lock);
1266 
1267 	venus_helper_vb2_buf_queue(vb);
1268 }
1269 
1270 static const struct vb2_ops vdec_vb2_ops = {
1271 	.queue_setup = vdec_queue_setup,
1272 	.buf_init = vdec_buf_init,
1273 	.buf_cleanup = vdec_buf_cleanup,
1274 	.buf_prepare = venus_helper_vb2_buf_prepare,
1275 	.start_streaming = vdec_start_streaming,
1276 	.stop_streaming = vdec_stop_streaming,
1277 	.buf_queue = vdec_vb2_buf_queue,
1278 };
1279 
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)1280 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1281 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1282 			  u32 hfi_flags, u64 timestamp_us)
1283 {
1284 	enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1285 	struct vb2_v4l2_buffer *vbuf;
1286 	struct vb2_buffer *vb;
1287 	unsigned int type;
1288 
1289 	vdec_pm_touch(inst);
1290 
1291 	if (buf_type == HFI_BUFFER_INPUT)
1292 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1293 	else
1294 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1295 
1296 	vbuf = venus_helper_find_buf(inst, type, tag);
1297 	if (!vbuf)
1298 		return;
1299 
1300 	vbuf->flags = flags;
1301 	vbuf->field = V4L2_FIELD_NONE;
1302 	vb = &vbuf->vb2_buf;
1303 
1304 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1305 		vb2_set_plane_payload(vb, 0, bytesused);
1306 		vb->planes[0].data_offset = data_offset;
1307 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1308 		vbuf->sequence = inst->sequence_cap++;
1309 
1310 		if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1311 			const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1312 
1313 			v4l2_event_queue_fh(&inst->fh, &ev);
1314 
1315 			if (inst->codec_state == VENUS_DEC_STATE_DRAIN) {
1316 				inst->drain_active = false;
1317 				inst->codec_state = VENUS_DEC_STATE_STOPPED;
1318 			}
1319 		}
1320 
1321 		if (!bytesused)
1322 			state = VB2_BUF_STATE_ERROR;
1323 	} else {
1324 		vbuf->sequence = inst->sequence_out++;
1325 	}
1326 
1327 	venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1328 
1329 	if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1330 		venus_helper_acquire_buf_ref(vbuf);
1331 
1332 	if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1333 		state = VB2_BUF_STATE_ERROR;
1334 
1335 	if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1336 		state = VB2_BUF_STATE_ERROR;
1337 		vb2_set_plane_payload(vb, 0, 0);
1338 		vb->timestamp = 0;
1339 	}
1340 
1341 	v4l2_m2m_buf_done(vbuf, state);
1342 }
1343 
vdec_event_change(struct venus_inst *inst, struct hfi_event_data *ev_data, bool sufficient)1344 static void vdec_event_change(struct venus_inst *inst,
1345 			      struct hfi_event_data *ev_data, bool sufficient)
1346 {
1347 	static const struct v4l2_event ev = {
1348 		.type = V4L2_EVENT_SOURCE_CHANGE,
1349 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1350 	struct device *dev = inst->core->dev_dec;
1351 	struct v4l2_format format = {};
1352 
1353 	mutex_lock(&inst->lock);
1354 
1355 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1356 	format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1357 	format.fmt.pix_mp.width = ev_data->width;
1358 	format.fmt.pix_mp.height = ev_data->height;
1359 
1360 	vdec_try_fmt_common(inst, &format);
1361 
1362 	inst->width = format.fmt.pix_mp.width;
1363 	inst->height = format.fmt.pix_mp.height;
1364 
1365 	inst->out_width = ev_data->width;
1366 	inst->out_height = ev_data->height;
1367 
1368 	if (inst->bit_depth != ev_data->bit_depth)
1369 		inst->bit_depth = ev_data->bit_depth;
1370 
1371 	dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n",
1372 		sufficient ? "" : "not", ev_data->width, ev_data->height);
1373 
1374 	switch (inst->codec_state) {
1375 	case VENUS_DEC_STATE_INIT:
1376 		inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1377 		break;
1378 	case VENUS_DEC_STATE_DECODING:
1379 	case VENUS_DEC_STATE_DRAIN:
1380 		inst->codec_state = VENUS_DEC_STATE_DRC;
1381 		break;
1382 	default:
1383 		break;
1384 	}
1385 
1386 	/*
1387 	 * The assumption is that the firmware have to return the last buffer
1388 	 * before this event is received in the v4l2 driver. Also the firmware
1389 	 * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1390 	 */
1391 
1392 	if (inst->codec_state == VENUS_DEC_STATE_DRC) {
1393 		int ret;
1394 
1395 		inst->next_buf_last = true;
1396 
1397 		ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1398 		if (ret)
1399 			dev_dbg(dev, VDBGH "flush output error %d\n", ret);
1400 	}
1401 
1402 	inst->next_buf_last = true;
1403 	inst->reconfig = true;
1404 	v4l2_event_queue_fh(&inst->fh, &ev);
1405 	wake_up(&inst->reconf_wait);
1406 
1407 	mutex_unlock(&inst->lock);
1408 }
1409 
vdec_event_notify(struct venus_inst *inst, u32 event, struct hfi_event_data *data)1410 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1411 			      struct hfi_event_data *data)
1412 {
1413 	struct venus_core *core = inst->core;
1414 	struct device *dev = core->dev_dec;
1415 
1416 	vdec_pm_touch(inst);
1417 
1418 	switch (event) {
1419 	case EVT_SESSION_ERROR:
1420 		inst->session_error = true;
1421 		dev_err(dev, "dec: event session error %x\n", inst->error);
1422 		break;
1423 	case EVT_SYS_EVENT_CHANGE:
1424 		switch (data->event_type) {
1425 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1426 			vdec_event_change(inst, data, true);
1427 			break;
1428 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1429 			vdec_event_change(inst, data, false);
1430 			break;
1431 		case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1432 			venus_helper_release_buf_ref(inst, data->tag);
1433 			break;
1434 		default:
1435 			break;
1436 		}
1437 		break;
1438 	default:
1439 		break;
1440 	}
1441 }
1442 
vdec_flush_done(struct venus_inst *inst)1443 static void vdec_flush_done(struct venus_inst *inst)
1444 {
1445 	dev_dbg(inst->core->dev_dec, VDBGH "flush done\n");
1446 }
1447 
1448 static const struct hfi_inst_ops vdec_hfi_ops = {
1449 	.buf_done = vdec_buf_done,
1450 	.event_notify = vdec_event_notify,
1451 	.flush_done = vdec_flush_done,
1452 };
1453 
vdec_inst_init(struct venus_inst *inst)1454 static void vdec_inst_init(struct venus_inst *inst)
1455 {
1456 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1457 	inst->fmt_out = &vdec_formats[6];
1458 	inst->fmt_cap = &vdec_formats[0];
1459 	inst->width = frame_width_min(inst);
1460 	inst->height = ALIGN(frame_height_min(inst), 32);
1461 	inst->out_width = frame_width_min(inst);
1462 	inst->out_height = frame_height_min(inst);
1463 	inst->fps = 30;
1464 	inst->timeperframe.numerator = 1;
1465 	inst->timeperframe.denominator = 30;
1466 	inst->opb_buftype = HFI_BUFFER_OUTPUT;
1467 }
1468 
vdec_m2m_device_run(void *priv)1469 static void vdec_m2m_device_run(void *priv)
1470 {
1471 }
1472 
1473 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1474 	.device_run = vdec_m2m_device_run,
1475 	.job_abort = venus_helper_m2m_job_abort,
1476 };
1477 
m2m_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)1478 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1479 			  struct vb2_queue *dst_vq)
1480 {
1481 	struct venus_inst *inst = priv;
1482 	int ret;
1483 
1484 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1485 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1486 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1487 	src_vq->ops = &vdec_vb2_ops;
1488 	src_vq->mem_ops = &vb2_dma_sg_memops;
1489 	src_vq->drv_priv = inst;
1490 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1491 	src_vq->allow_zero_bytesused = 1;
1492 	src_vq->min_buffers_needed = 0;
1493 	src_vq->dev = inst->core->dev;
1494 	ret = vb2_queue_init(src_vq);
1495 	if (ret)
1496 		return ret;
1497 
1498 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1499 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1500 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1501 	dst_vq->ops = &vdec_vb2_ops;
1502 	dst_vq->mem_ops = &vb2_dma_sg_memops;
1503 	dst_vq->drv_priv = inst;
1504 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1505 	dst_vq->allow_zero_bytesused = 1;
1506 	dst_vq->min_buffers_needed = 0;
1507 	dst_vq->dev = inst->core->dev;
1508 	return vb2_queue_init(dst_vq);
1509 }
1510 
vdec_open(struct file *file)1511 static int vdec_open(struct file *file)
1512 {
1513 	struct venus_core *core = video_drvdata(file);
1514 	struct venus_inst *inst;
1515 	int ret;
1516 
1517 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1518 	if (!inst)
1519 		return -ENOMEM;
1520 
1521 	INIT_LIST_HEAD(&inst->dpbbufs);
1522 	INIT_LIST_HEAD(&inst->registeredbufs);
1523 	INIT_LIST_HEAD(&inst->internalbufs);
1524 	INIT_LIST_HEAD(&inst->list);
1525 	mutex_init(&inst->lock);
1526 
1527 	inst->core = core;
1528 	inst->session_type = VIDC_SESSION_TYPE_DEC;
1529 	inst->num_output_bufs = 1;
1530 	inst->codec_state = VENUS_DEC_STATE_DEINIT;
1531 	inst->buf_count = 0;
1532 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1533 	inst->core_acquired = false;
1534 	inst->bit_depth = VIDC_BITDEPTH_8;
1535 	init_waitqueue_head(&inst->reconf_wait);
1536 	venus_helper_init_instance(inst);
1537 
1538 	ret = vdec_ctrl_init(inst);
1539 	if (ret)
1540 		goto err_free;
1541 
1542 	ret = hfi_session_create(inst, &vdec_hfi_ops);
1543 	if (ret)
1544 		goto err_ctrl_deinit;
1545 
1546 	vdec_inst_init(inst);
1547 
1548 	/*
1549 	 * create m2m device for every instance, the m2m context scheduling
1550 	 * is made by firmware side so we do not need to care about.
1551 	 */
1552 	inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1553 	if (IS_ERR(inst->m2m_dev)) {
1554 		ret = PTR_ERR(inst->m2m_dev);
1555 		goto err_session_destroy;
1556 	}
1557 
1558 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1559 	if (IS_ERR(inst->m2m_ctx)) {
1560 		ret = PTR_ERR(inst->m2m_ctx);
1561 		goto err_m2m_release;
1562 	}
1563 
1564 	v4l2_fh_init(&inst->fh, core->vdev_dec);
1565 
1566 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1567 	v4l2_fh_add(&inst->fh);
1568 	inst->fh.m2m_ctx = inst->m2m_ctx;
1569 	file->private_data = &inst->fh;
1570 
1571 	return 0;
1572 
1573 err_m2m_release:
1574 	v4l2_m2m_release(inst->m2m_dev);
1575 err_session_destroy:
1576 	hfi_session_destroy(inst);
1577 err_ctrl_deinit:
1578 	vdec_ctrl_deinit(inst);
1579 err_free:
1580 	kfree(inst);
1581 	return ret;
1582 }
1583 
vdec_close(struct file *file)1584 static int vdec_close(struct file *file)
1585 {
1586 	struct venus_inst *inst = to_inst(file);
1587 
1588 	vdec_pm_get(inst);
1589 
1590 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1591 	v4l2_m2m_release(inst->m2m_dev);
1592 	vdec_ctrl_deinit(inst);
1593 	hfi_session_destroy(inst);
1594 	mutex_destroy(&inst->lock);
1595 	v4l2_fh_del(&inst->fh);
1596 	v4l2_fh_exit(&inst->fh);
1597 
1598 	vdec_pm_put(inst, false);
1599 
1600 	kfree(inst);
1601 	return 0;
1602 }
1603 
1604 static const struct v4l2_file_operations vdec_fops = {
1605 	.owner = THIS_MODULE,
1606 	.open = vdec_open,
1607 	.release = vdec_close,
1608 	.unlocked_ioctl = video_ioctl2,
1609 	.poll = v4l2_m2m_fop_poll,
1610 	.mmap = v4l2_m2m_fop_mmap,
1611 };
1612 
vdec_probe(struct platform_device *pdev)1613 static int vdec_probe(struct platform_device *pdev)
1614 {
1615 	struct device *dev = &pdev->dev;
1616 	struct video_device *vdev;
1617 	struct venus_core *core;
1618 	int ret;
1619 
1620 	if (!dev->parent)
1621 		return -EPROBE_DEFER;
1622 
1623 	core = dev_get_drvdata(dev->parent);
1624 	if (!core)
1625 		return -EPROBE_DEFER;
1626 
1627 	platform_set_drvdata(pdev, core);
1628 
1629 	if (core->pm_ops->vdec_get) {
1630 		ret = core->pm_ops->vdec_get(dev);
1631 		if (ret)
1632 			return ret;
1633 	}
1634 
1635 	vdev = video_device_alloc();
1636 	if (!vdev)
1637 		return -ENOMEM;
1638 
1639 	strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1640 	vdev->release = video_device_release;
1641 	vdev->fops = &vdec_fops;
1642 	vdev->ioctl_ops = &vdec_ioctl_ops;
1643 	vdev->vfl_dir = VFL_DIR_M2M;
1644 	vdev->v4l2_dev = &core->v4l2_dev;
1645 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1646 
1647 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1648 	if (ret)
1649 		goto err_vdev_release;
1650 
1651 	core->vdev_dec = vdev;
1652 	core->dev_dec = dev;
1653 
1654 	video_set_drvdata(vdev, core);
1655 	pm_runtime_set_autosuspend_delay(dev, 2000);
1656 	pm_runtime_use_autosuspend(dev);
1657 	pm_runtime_enable(dev);
1658 
1659 	return 0;
1660 
1661 err_vdev_release:
1662 	video_device_release(vdev);
1663 	return ret;
1664 }
1665 
vdec_remove(struct platform_device *pdev)1666 static int vdec_remove(struct platform_device *pdev)
1667 {
1668 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1669 
1670 	video_unregister_device(core->vdev_dec);
1671 	pm_runtime_disable(core->dev_dec);
1672 
1673 	if (core->pm_ops->vdec_put)
1674 		core->pm_ops->vdec_put(core->dev_dec);
1675 
1676 	return 0;
1677 }
1678 
vdec_runtime_suspend(struct device *dev)1679 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1680 {
1681 	struct venus_core *core = dev_get_drvdata(dev);
1682 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1683 	int ret = 0;
1684 
1685 	if (pm_ops->vdec_power)
1686 		ret = pm_ops->vdec_power(dev, POWER_OFF);
1687 
1688 	return ret;
1689 }
1690 
vdec_runtime_resume(struct device *dev)1691 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1692 {
1693 	struct venus_core *core = dev_get_drvdata(dev);
1694 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1695 	int ret = 0;
1696 
1697 	if (pm_ops->vdec_power)
1698 		ret = pm_ops->vdec_power(dev, POWER_ON);
1699 
1700 	return ret;
1701 }
1702 
1703 static const struct dev_pm_ops vdec_pm_ops = {
1704 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1705 				pm_runtime_force_resume)
1706 	SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1707 };
1708 
1709 static const struct of_device_id vdec_dt_match[] = {
1710 	{ .compatible = "venus-decoder" },
1711 	{ }
1712 };
1713 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1714 
1715 static struct platform_driver qcom_venus_dec_driver = {
1716 	.probe = vdec_probe,
1717 	.remove = vdec_remove,
1718 	.driver = {
1719 		.name = "qcom-venus-decoder",
1720 		.of_match_table = vdec_dt_match,
1721 		.pm = &vdec_pm_ops,
1722 	},
1723 };
1724 module_platform_driver(qcom_venus_dec_driver);
1725 
1726 MODULE_ALIAS("platform:qcom-venus-decoder");
1727 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1728 MODULE_LICENSE("GPL v2");
1729