1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2017 Linaro Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-dma-sg.h>
25 
26 #include "hfi_venus_io.h"
27 #include "core.h"
28 #include "helpers.h"
29 #include "vdec.h"
30 
31 static u32 get_framesize_uncompressed(unsigned int plane, u32 width, u32 height)
32 {
33 	u32 y_stride, uv_stride, y_plane;
34 	u32 y_sclines, uv_sclines, uv_plane;
35 	u32 size;
36 
37 	y_stride = ALIGN(width, 128);
38 	uv_stride = ALIGN(width, 128);
39 	y_sclines = ALIGN(height, 32);
40 	uv_sclines = ALIGN(((height + 1) >> 1), 16);
41 
42 	y_plane = y_stride * y_sclines;
43 	uv_plane = uv_stride * uv_sclines + SZ_4K;
44 	size = y_plane + uv_plane + SZ_8K;
45 
46 	return ALIGN(size, SZ_4K);
47 }
48 
49 static u32 get_framesize_compressed(unsigned int width, unsigned int height)
50 {
51 	return ((width * height * 3 / 2) / 2) + 128;
52 }
53 
54 /*
55  * Three resons to keep MPLANE formats (despite that the number of planes
56  * currently is one):
57  * - the MPLANE formats allow only one plane to be used
58  * - the downstream driver use MPLANE formats too
59  * - future firmware versions could add support for >1 planes
60  */
61 static const struct venus_format vdec_formats[] = {
62 	{
63 		.pixfmt = V4L2_PIX_FMT_NV12,
64 		.num_planes = 1,
65 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
66 	}, {
67 		.pixfmt = V4L2_PIX_FMT_MPEG4,
68 		.num_planes = 1,
69 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
70 	}, {
71 		.pixfmt = V4L2_PIX_FMT_MPEG2,
72 		.num_planes = 1,
73 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
74 	}, {
75 		.pixfmt = V4L2_PIX_FMT_H263,
76 		.num_planes = 1,
77 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
78 	}, {
79 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
80 		.num_planes = 1,
81 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
82 	}, {
83 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
84 		.num_planes = 1,
85 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
86 	}, {
87 		.pixfmt = V4L2_PIX_FMT_H264,
88 		.num_planes = 1,
89 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
90 	}, {
91 		.pixfmt = V4L2_PIX_FMT_VP8,
92 		.num_planes = 1,
93 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
94 	}, {
95 		.pixfmt = V4L2_PIX_FMT_VP9,
96 		.num_planes = 1,
97 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
98 	}, {
99 		.pixfmt = V4L2_PIX_FMT_XVID,
100 		.num_planes = 1,
101 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
102 	},
103 };
104 
105 static const struct venus_format *
106 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
107 {
108 	const struct venus_format *fmt = vdec_formats;
109 	unsigned int size = ARRAY_SIZE(vdec_formats);
110 	unsigned int i;
111 
112 	for (i = 0; i < size; i++) {
113 		if (fmt[i].pixfmt == pixfmt)
114 			break;
115 	}
116 
117 	if (i == size || fmt[i].type != type)
118 		return NULL;
119 
120 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
121 	    !venus_helper_check_codec(inst, fmt[i].pixfmt))
122 		return NULL;
123 
124 	return &fmt[i];
125 }
126 
127 static const struct venus_format *
128 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
129 {
130 	const struct venus_format *fmt = vdec_formats;
131 	unsigned int size = ARRAY_SIZE(vdec_formats);
132 	unsigned int i, k = 0;
133 
134 	if (index > size)
135 		return NULL;
136 
137 	for (i = 0; i < size; i++) {
138 		if (fmt[i].type != type)
139 			continue;
140 		if (k == index)
141 			break;
142 		k++;
143 	}
144 
145 	if (i == size)
146 		return NULL;
147 
148 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
149 	    !venus_helper_check_codec(inst, fmt[i].pixfmt))
150 		return NULL;
151 
152 	return &fmt[i];
153 }
154 
155 static const struct venus_format *
156 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
157 {
158 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
159 	struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
160 	const struct venus_format *fmt;
161 	unsigned int p;
162 
163 	memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
164 	memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
165 
166 	fmt = find_format(inst, pixmp->pixelformat, f->type);
167 	if (!fmt) {
168 		if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
169 			pixmp->pixelformat = V4L2_PIX_FMT_NV12;
170 		else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
171 			pixmp->pixelformat = V4L2_PIX_FMT_H264;
172 		else
173 			return NULL;
174 		fmt = find_format(inst, pixmp->pixelformat, f->type);
175 		pixmp->width = 1280;
176 		pixmp->height = 720;
177 	}
178 
179 	pixmp->width = clamp(pixmp->width, inst->cap_width.min,
180 			     inst->cap_width.max);
181 	pixmp->height = clamp(pixmp->height, inst->cap_height.min,
182 			      inst->cap_height.max);
183 
184 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
185 		pixmp->height = ALIGN(pixmp->height, 32);
186 
187 	if (pixmp->field == V4L2_FIELD_ANY)
188 		pixmp->field = V4L2_FIELD_NONE;
189 	pixmp->num_planes = fmt->num_planes;
190 	pixmp->flags = 0;
191 
192 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
193 		for (p = 0; p < pixmp->num_planes; p++) {
194 			pfmt[p].sizeimage =
195 				get_framesize_uncompressed(p, pixmp->width,
196 							   pixmp->height);
197 			pfmt[p].bytesperline = ALIGN(pixmp->width, 128);
198 		}
199 	} else {
200 		pfmt[0].sizeimage = get_framesize_compressed(pixmp->width,
201 							     pixmp->height);
202 		pfmt[0].bytesperline = 0;
203 	}
204 
205 	return fmt;
206 }
207 
208 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
209 {
210 	struct venus_inst *inst = to_inst(file);
211 
212 	vdec_try_fmt_common(inst, f);
213 
214 	return 0;
215 }
216 
217 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
218 {
219 	struct venus_inst *inst = to_inst(file);
220 	const struct venus_format *fmt = NULL;
221 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
222 
223 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
224 		fmt = inst->fmt_cap;
225 	else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
226 		fmt = inst->fmt_out;
227 
228 	if (inst->reconfig) {
229 		struct v4l2_format format = {};
230 
231 		inst->out_width = inst->reconfig_width;
232 		inst->out_height = inst->reconfig_height;
233 		inst->reconfig = false;
234 
235 		format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
236 		format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
237 		format.fmt.pix_mp.width = inst->out_width;
238 		format.fmt.pix_mp.height = inst->out_height;
239 
240 		vdec_try_fmt_common(inst, &format);
241 
242 		inst->width = format.fmt.pix_mp.width;
243 		inst->height = format.fmt.pix_mp.height;
244 	}
245 
246 	pixmp->pixelformat = fmt->pixfmt;
247 
248 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
249 		pixmp->width = inst->width;
250 		pixmp->height = inst->height;
251 		pixmp->colorspace = inst->colorspace;
252 		pixmp->ycbcr_enc = inst->ycbcr_enc;
253 		pixmp->quantization = inst->quantization;
254 		pixmp->xfer_func = inst->xfer_func;
255 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
256 		pixmp->width = inst->out_width;
257 		pixmp->height = inst->out_height;
258 	}
259 
260 	vdec_try_fmt_common(inst, f);
261 
262 	return 0;
263 }
264 
265 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
266 {
267 	struct venus_inst *inst = to_inst(file);
268 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
269 	struct v4l2_pix_format_mplane orig_pixmp;
270 	const struct venus_format *fmt;
271 	struct v4l2_format format;
272 	u32 pixfmt_out = 0, pixfmt_cap = 0;
273 
274 	orig_pixmp = *pixmp;
275 
276 	fmt = vdec_try_fmt_common(inst, f);
277 
278 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
279 		pixfmt_out = pixmp->pixelformat;
280 		pixfmt_cap = inst->fmt_cap->pixfmt;
281 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
282 		pixfmt_cap = pixmp->pixelformat;
283 		pixfmt_out = inst->fmt_out->pixfmt;
284 	}
285 
286 	memset(&format, 0, sizeof(format));
287 
288 	format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
289 	format.fmt.pix_mp.pixelformat = pixfmt_out;
290 	format.fmt.pix_mp.width = orig_pixmp.width;
291 	format.fmt.pix_mp.height = orig_pixmp.height;
292 	vdec_try_fmt_common(inst, &format);
293 
294 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
295 		inst->out_width = format.fmt.pix_mp.width;
296 		inst->out_height = format.fmt.pix_mp.height;
297 		inst->colorspace = pixmp->colorspace;
298 		inst->ycbcr_enc = pixmp->ycbcr_enc;
299 		inst->quantization = pixmp->quantization;
300 		inst->xfer_func = pixmp->xfer_func;
301 	}
302 
303 	memset(&format, 0, sizeof(format));
304 
305 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
306 	format.fmt.pix_mp.pixelformat = pixfmt_cap;
307 	format.fmt.pix_mp.width = orig_pixmp.width;
308 	format.fmt.pix_mp.height = orig_pixmp.height;
309 	vdec_try_fmt_common(inst, &format);
310 
311 	inst->width = format.fmt.pix_mp.width;
312 	inst->height = format.fmt.pix_mp.height;
313 
314 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
315 		inst->fmt_out = fmt;
316 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
317 		inst->fmt_cap = fmt;
318 
319 	return 0;
320 }
321 
322 static int
323 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
324 {
325 	struct venus_inst *inst = to_inst(file);
326 
327 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
328 	    s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
329 		return -EINVAL;
330 
331 	switch (s->target) {
332 	case V4L2_SEL_TGT_CROP_BOUNDS:
333 	case V4L2_SEL_TGT_CROP_DEFAULT:
334 	case V4L2_SEL_TGT_CROP:
335 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
336 			return -EINVAL;
337 		s->r.width = inst->out_width;
338 		s->r.height = inst->out_height;
339 		break;
340 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
341 	case V4L2_SEL_TGT_COMPOSE_PADDED:
342 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
343 			return -EINVAL;
344 		s->r.width = inst->width;
345 		s->r.height = inst->height;
346 		break;
347 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
348 	case V4L2_SEL_TGT_COMPOSE:
349 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
350 			return -EINVAL;
351 		s->r.width = inst->out_width;
352 		s->r.height = inst->out_height;
353 		break;
354 	default:
355 		return -EINVAL;
356 	}
357 
358 	s->r.top = 0;
359 	s->r.left = 0;
360 
361 	return 0;
362 }
363 
364 static int
365 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
366 {
367 	strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
368 	strlcpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
369 	strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
370 
371 	return 0;
372 }
373 
374 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
375 {
376 	struct venus_inst *inst = to_inst(file);
377 	const struct venus_format *fmt;
378 
379 	memset(f->reserved, 0, sizeof(f->reserved));
380 
381 	fmt = find_format_by_index(inst, f->index, f->type);
382 	if (!fmt)
383 		return -EINVAL;
384 
385 	f->pixelformat = fmt->pixfmt;
386 
387 	return 0;
388 }
389 
390 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
391 {
392 	struct venus_inst *inst = to_inst(file);
393 	struct v4l2_captureparm *cap = &a->parm.capture;
394 	struct v4l2_fract *timeperframe = &cap->timeperframe;
395 	u64 us_per_frame, fps;
396 
397 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
398 	    a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
399 		return -EINVAL;
400 
401 	memset(cap->reserved, 0, sizeof(cap->reserved));
402 	if (!timeperframe->denominator)
403 		timeperframe->denominator = inst->timeperframe.denominator;
404 	if (!timeperframe->numerator)
405 		timeperframe->numerator = inst->timeperframe.numerator;
406 	cap->readbuffers = 0;
407 	cap->extendedmode = 0;
408 	cap->capability = V4L2_CAP_TIMEPERFRAME;
409 	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
410 	do_div(us_per_frame, timeperframe->denominator);
411 
412 	if (!us_per_frame)
413 		return -EINVAL;
414 
415 	fps = (u64)USEC_PER_SEC;
416 	do_div(fps, us_per_frame);
417 
418 	inst->fps = fps;
419 	inst->timeperframe = *timeperframe;
420 
421 	return 0;
422 }
423 
424 static int vdec_enum_framesizes(struct file *file, void *fh,
425 				struct v4l2_frmsizeenum *fsize)
426 {
427 	struct venus_inst *inst = to_inst(file);
428 	const struct venus_format *fmt;
429 
430 	fmt = find_format(inst, fsize->pixel_format,
431 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
432 	if (!fmt) {
433 		fmt = find_format(inst, fsize->pixel_format,
434 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
435 		if (!fmt)
436 			return -EINVAL;
437 	}
438 
439 	if (fsize->index)
440 		return -EINVAL;
441 
442 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
443 
444 	fsize->stepwise.min_width = inst->cap_width.min;
445 	fsize->stepwise.max_width = inst->cap_width.max;
446 	fsize->stepwise.step_width = inst->cap_width.step_size;
447 	fsize->stepwise.min_height = inst->cap_height.min;
448 	fsize->stepwise.max_height = inst->cap_height.max;
449 	fsize->stepwise.step_height = inst->cap_height.step_size;
450 
451 	return 0;
452 }
453 
454 static int vdec_subscribe_event(struct v4l2_fh *fh,
455 				const struct v4l2_event_subscription *sub)
456 {
457 	switch (sub->type) {
458 	case V4L2_EVENT_EOS:
459 		return v4l2_event_subscribe(fh, sub, 2, NULL);
460 	case V4L2_EVENT_SOURCE_CHANGE:
461 		return v4l2_src_change_event_subscribe(fh, sub);
462 	case V4L2_EVENT_CTRL:
463 		return v4l2_ctrl_subscribe_event(fh, sub);
464 	default:
465 		return -EINVAL;
466 	}
467 }
468 
469 static int
470 vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
471 {
472 	if (cmd->cmd != V4L2_DEC_CMD_STOP)
473 		return -EINVAL;
474 
475 	return 0;
476 }
477 
478 static int
479 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
480 {
481 	struct venus_inst *inst = to_inst(file);
482 	int ret;
483 
484 	ret = vdec_try_decoder_cmd(file, fh, cmd);
485 	if (ret)
486 		return ret;
487 
488 	mutex_lock(&inst->lock);
489 	inst->cmd_stop = true;
490 	mutex_unlock(&inst->lock);
491 
492 	hfi_session_flush(inst);
493 
494 	return 0;
495 }
496 
497 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
498 	.vidioc_querycap = vdec_querycap,
499 	.vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt,
500 	.vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt,
501 	.vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
502 	.vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
503 	.vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
504 	.vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
505 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
506 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
507 	.vidioc_g_selection = vdec_g_selection,
508 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
509 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
510 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
511 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
512 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
513 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
514 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
515 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
516 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
517 	.vidioc_s_parm = vdec_s_parm,
518 	.vidioc_enum_framesizes = vdec_enum_framesizes,
519 	.vidioc_subscribe_event = vdec_subscribe_event,
520 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
521 	.vidioc_try_decoder_cmd = vdec_try_decoder_cmd,
522 	.vidioc_decoder_cmd = vdec_decoder_cmd,
523 };
524 
525 static int vdec_set_properties(struct venus_inst *inst)
526 {
527 	struct vdec_controls *ctr = &inst->controls.dec;
528 	struct venus_core *core = inst->core;
529 	struct hfi_enable en = { .enable = 1 };
530 	u32 ptype;
531 	int ret;
532 
533 	if (core->res->hfi_version == HFI_VERSION_1XX) {
534 		ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
535 		ret = hfi_session_set_property(inst, ptype, &en);
536 		if (ret)
537 			return ret;
538 	}
539 
540 	if (core->res->hfi_version == HFI_VERSION_3XX ||
541 	    inst->cap_bufs_mode_dynamic) {
542 		struct hfi_buffer_alloc_mode mode;
543 
544 		ptype = HFI_PROPERTY_PARAM_BUFFER_ALLOC_MODE;
545 		mode.type = HFI_BUFFER_OUTPUT;
546 		mode.mode = HFI_BUFFER_MODE_DYNAMIC;
547 
548 		ret = hfi_session_set_property(inst, ptype, &mode);
549 		if (ret)
550 			return ret;
551 	}
552 
553 	if (ctr->post_loop_deb_mode) {
554 		ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
555 		en.enable = 1;
556 		ret = hfi_session_set_property(inst, ptype, &en);
557 		if (ret)
558 			return ret;
559 	}
560 
561 	return 0;
562 }
563 
564 static int vdec_init_session(struct venus_inst *inst)
565 {
566 	int ret;
567 
568 	ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
569 	if (ret)
570 		return ret;
571 
572 	ret = venus_helper_set_input_resolution(inst, inst->out_width,
573 						inst->out_height);
574 	if (ret)
575 		goto deinit;
576 
577 	ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt);
578 	if (ret)
579 		goto deinit;
580 
581 	return 0;
582 deinit:
583 	hfi_session_deinit(inst);
584 	return ret;
585 }
586 
587 static int vdec_cap_num_buffers(struct venus_inst *inst, unsigned int *num)
588 {
589 	struct hfi_buffer_requirements bufreq;
590 	int ret;
591 
592 	ret = vdec_init_session(inst);
593 	if (ret)
594 		return ret;
595 
596 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
597 
598 	*num = bufreq.count_actual;
599 
600 	hfi_session_deinit(inst);
601 
602 	return ret;
603 }
604 
605 static int vdec_queue_setup(struct vb2_queue *q,
606 			    unsigned int *num_buffers, unsigned int *num_planes,
607 			    unsigned int sizes[], struct device *alloc_devs[])
608 {
609 	struct venus_inst *inst = vb2_get_drv_priv(q);
610 	unsigned int p, num;
611 	int ret = 0;
612 
613 	if (*num_planes) {
614 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
615 		    *num_planes != inst->fmt_out->num_planes)
616 			return -EINVAL;
617 
618 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
619 		    *num_planes != inst->fmt_cap->num_planes)
620 			return -EINVAL;
621 
622 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
623 		    sizes[0] < inst->input_buf_size)
624 			return -EINVAL;
625 
626 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
627 		    sizes[0] < inst->output_buf_size)
628 			return -EINVAL;
629 
630 		return 0;
631 	}
632 
633 	switch (q->type) {
634 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
635 		*num_planes = inst->fmt_out->num_planes;
636 		sizes[0] = get_framesize_compressed(inst->out_width,
637 						    inst->out_height);
638 		inst->input_buf_size = sizes[0];
639 		inst->num_input_bufs = *num_buffers;
640 
641 		ret = vdec_cap_num_buffers(inst, &num);
642 		if (ret)
643 			break;
644 
645 		inst->num_output_bufs = num;
646 		break;
647 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
648 		*num_planes = inst->fmt_cap->num_planes;
649 
650 		ret = vdec_cap_num_buffers(inst, &num);
651 		if (ret)
652 			break;
653 
654 		*num_buffers = max(*num_buffers, num);
655 
656 		for (p = 0; p < *num_planes; p++)
657 			sizes[p] = get_framesize_uncompressed(p, inst->width,
658 							      inst->height);
659 
660 		inst->num_output_bufs = *num_buffers;
661 		inst->output_buf_size = sizes[0];
662 		break;
663 	default:
664 		ret = -EINVAL;
665 		break;
666 	}
667 
668 	return ret;
669 }
670 
671 static int vdec_verify_conf(struct venus_inst *inst)
672 {
673 	struct hfi_buffer_requirements bufreq;
674 	int ret;
675 
676 	if (!inst->num_input_bufs || !inst->num_output_bufs)
677 		return -EINVAL;
678 
679 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
680 	if (ret)
681 		return ret;
682 
683 	if (inst->num_output_bufs < bufreq.count_actual ||
684 	    inst->num_output_bufs < bufreq.count_min)
685 		return -EINVAL;
686 
687 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
688 	if (ret)
689 		return ret;
690 
691 	if (inst->num_input_bufs < bufreq.count_min)
692 		return -EINVAL;
693 
694 	return 0;
695 }
696 
697 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
698 {
699 	struct venus_inst *inst = vb2_get_drv_priv(q);
700 	struct venus_core *core = inst->core;
701 	u32 ptype;
702 	int ret;
703 
704 	mutex_lock(&inst->lock);
705 
706 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
707 		inst->streamon_out = 1;
708 	else
709 		inst->streamon_cap = 1;
710 
711 	if (!(inst->streamon_out & inst->streamon_cap)) {
712 		mutex_unlock(&inst->lock);
713 		return 0;
714 	}
715 
716 	venus_helper_init_instance(inst);
717 
718 	inst->reconfig = false;
719 	inst->sequence_cap = 0;
720 	inst->sequence_out = 0;
721 	inst->cmd_stop = false;
722 
723 	ret = vdec_init_session(inst);
724 	if (ret)
725 		goto bufs_done;
726 
727 	ret = vdec_set_properties(inst);
728 	if (ret)
729 		goto deinit_sess;
730 
731 	if (core->res->hfi_version == HFI_VERSION_3XX) {
732 		struct hfi_buffer_size_actual buf_sz;
733 
734 		ptype = HFI_PROPERTY_PARAM_BUFFER_SIZE_ACTUAL;
735 		buf_sz.type = HFI_BUFFER_OUTPUT;
736 		buf_sz.size = inst->output_buf_size;
737 
738 		ret = hfi_session_set_property(inst, ptype, &buf_sz);
739 		if (ret)
740 			goto deinit_sess;
741 	}
742 
743 	ret = vdec_verify_conf(inst);
744 	if (ret)
745 		goto deinit_sess;
746 
747 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
748 					VB2_MAX_FRAME);
749 	if (ret)
750 		goto deinit_sess;
751 
752 	ret = venus_helper_vb2_start_streaming(inst);
753 	if (ret)
754 		goto deinit_sess;
755 
756 	mutex_unlock(&inst->lock);
757 
758 	return 0;
759 
760 deinit_sess:
761 	hfi_session_deinit(inst);
762 bufs_done:
763 	venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
764 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
765 		inst->streamon_out = 0;
766 	else
767 		inst->streamon_cap = 0;
768 	mutex_unlock(&inst->lock);
769 	return ret;
770 }
771 
772 static const struct vb2_ops vdec_vb2_ops = {
773 	.queue_setup = vdec_queue_setup,
774 	.buf_init = venus_helper_vb2_buf_init,
775 	.buf_prepare = venus_helper_vb2_buf_prepare,
776 	.start_streaming = vdec_start_streaming,
777 	.stop_streaming = venus_helper_vb2_stop_streaming,
778 	.buf_queue = venus_helper_vb2_buf_queue,
779 };
780 
781 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
782 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
783 			  u32 hfi_flags, u64 timestamp_us)
784 {
785 	enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
786 	struct vb2_v4l2_buffer *vbuf;
787 	struct vb2_buffer *vb;
788 	unsigned int type;
789 
790 	if (buf_type == HFI_BUFFER_INPUT)
791 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
792 	else
793 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
794 
795 	vbuf = venus_helper_find_buf(inst, type, tag);
796 	if (!vbuf)
797 		return;
798 
799 	vbuf->flags = flags;
800 	vbuf->field = V4L2_FIELD_NONE;
801 
802 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
803 		vb = &vbuf->vb2_buf;
804 		vb->planes[0].bytesused =
805 			max_t(unsigned int, inst->output_buf_size, bytesused);
806 		vb->planes[0].data_offset = data_offset;
807 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
808 		vbuf->sequence = inst->sequence_cap++;
809 
810 		if (inst->cmd_stop) {
811 			vbuf->flags |= V4L2_BUF_FLAG_LAST;
812 			inst->cmd_stop = false;
813 		}
814 
815 		if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
816 			const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
817 
818 			v4l2_event_queue_fh(&inst->fh, &ev);
819 		}
820 	} else {
821 		vbuf->sequence = inst->sequence_out++;
822 	}
823 
824 	if (hfi_flags & HFI_BUFFERFLAG_READONLY)
825 		venus_helper_acquire_buf_ref(vbuf);
826 
827 	if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
828 		state = VB2_BUF_STATE_ERROR;
829 
830 	v4l2_m2m_buf_done(vbuf, state);
831 }
832 
833 static void vdec_event_notify(struct venus_inst *inst, u32 event,
834 			      struct hfi_event_data *data)
835 {
836 	struct venus_core *core = inst->core;
837 	struct device *dev = core->dev_dec;
838 	static const struct v4l2_event ev = {
839 		.type = V4L2_EVENT_SOURCE_CHANGE,
840 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
841 
842 	switch (event) {
843 	case EVT_SESSION_ERROR:
844 		inst->session_error = true;
845 		dev_err(dev, "dec: event session error %x\n", inst->error);
846 		break;
847 	case EVT_SYS_EVENT_CHANGE:
848 		switch (data->event_type) {
849 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
850 			hfi_session_continue(inst);
851 			dev_dbg(dev, "event sufficient resources\n");
852 			break;
853 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
854 			inst->reconfig_height = data->height;
855 			inst->reconfig_width = data->width;
856 			inst->reconfig = true;
857 
858 			v4l2_event_queue_fh(&inst->fh, &ev);
859 
860 			dev_dbg(dev, "event not sufficient resources (%ux%u)\n",
861 				data->width, data->height);
862 			break;
863 		case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
864 			venus_helper_release_buf_ref(inst, data->tag);
865 			break;
866 		default:
867 			break;
868 		}
869 		break;
870 	default:
871 		break;
872 	}
873 }
874 
875 static const struct hfi_inst_ops vdec_hfi_ops = {
876 	.buf_done = vdec_buf_done,
877 	.event_notify = vdec_event_notify,
878 };
879 
880 static void vdec_inst_init(struct venus_inst *inst)
881 {
882 	inst->fmt_out = &vdec_formats[6];
883 	inst->fmt_cap = &vdec_formats[0];
884 	inst->width = 1280;
885 	inst->height = ALIGN(720, 32);
886 	inst->out_width = 1280;
887 	inst->out_height = 720;
888 	inst->fps = 30;
889 	inst->timeperframe.numerator = 1;
890 	inst->timeperframe.denominator = 30;
891 
892 	inst->cap_width.min = 64;
893 	inst->cap_width.max = 1920;
894 	if (inst->core->res->hfi_version == HFI_VERSION_3XX)
895 		inst->cap_width.max = 3840;
896 	inst->cap_width.step_size = 1;
897 	inst->cap_height.min = 64;
898 	inst->cap_height.max = ALIGN(1080, 32);
899 	if (inst->core->res->hfi_version == HFI_VERSION_3XX)
900 		inst->cap_height.max = ALIGN(2160, 32);
901 	inst->cap_height.step_size = 1;
902 	inst->cap_framerate.min = 1;
903 	inst->cap_framerate.max = 30;
904 	inst->cap_framerate.step_size = 1;
905 	inst->cap_mbs_per_frame.min = 16;
906 	inst->cap_mbs_per_frame.max = 8160;
907 }
908 
909 static const struct v4l2_m2m_ops vdec_m2m_ops = {
910 	.device_run = venus_helper_m2m_device_run,
911 	.job_abort = venus_helper_m2m_job_abort,
912 };
913 
914 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
915 			  struct vb2_queue *dst_vq)
916 {
917 	struct venus_inst *inst = priv;
918 	int ret;
919 
920 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
921 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
922 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
923 	src_vq->ops = &vdec_vb2_ops;
924 	src_vq->mem_ops = &vb2_dma_sg_memops;
925 	src_vq->drv_priv = inst;
926 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
927 	src_vq->allow_zero_bytesused = 1;
928 	src_vq->min_buffers_needed = 1;
929 	src_vq->dev = inst->core->dev;
930 	ret = vb2_queue_init(src_vq);
931 	if (ret)
932 		return ret;
933 
934 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
935 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
936 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
937 	dst_vq->ops = &vdec_vb2_ops;
938 	dst_vq->mem_ops = &vb2_dma_sg_memops;
939 	dst_vq->drv_priv = inst;
940 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
941 	dst_vq->allow_zero_bytesused = 1;
942 	dst_vq->min_buffers_needed = 1;
943 	dst_vq->dev = inst->core->dev;
944 	ret = vb2_queue_init(dst_vq);
945 	if (ret) {
946 		vb2_queue_release(src_vq);
947 		return ret;
948 	}
949 
950 	return 0;
951 }
952 
953 static int vdec_open(struct file *file)
954 {
955 	struct venus_core *core = video_drvdata(file);
956 	struct venus_inst *inst;
957 	int ret;
958 
959 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
960 	if (!inst)
961 		return -ENOMEM;
962 
963 	INIT_LIST_HEAD(&inst->registeredbufs);
964 	INIT_LIST_HEAD(&inst->internalbufs);
965 	INIT_LIST_HEAD(&inst->list);
966 	mutex_init(&inst->lock);
967 
968 	inst->core = core;
969 	inst->session_type = VIDC_SESSION_TYPE_DEC;
970 	inst->num_output_bufs = 1;
971 
972 	venus_helper_init_instance(inst);
973 
974 	ret = pm_runtime_get_sync(core->dev_dec);
975 	if (ret < 0)
976 		goto err_free_inst;
977 
978 	ret = vdec_ctrl_init(inst);
979 	if (ret)
980 		goto err_put_sync;
981 
982 	ret = hfi_session_create(inst, &vdec_hfi_ops);
983 	if (ret)
984 		goto err_ctrl_deinit;
985 
986 	vdec_inst_init(inst);
987 
988 	/*
989 	 * create m2m device for every instance, the m2m context scheduling
990 	 * is made by firmware side so we do not need to care about.
991 	 */
992 	inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
993 	if (IS_ERR(inst->m2m_dev)) {
994 		ret = PTR_ERR(inst->m2m_dev);
995 		goto err_session_destroy;
996 	}
997 
998 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
999 	if (IS_ERR(inst->m2m_ctx)) {
1000 		ret = PTR_ERR(inst->m2m_ctx);
1001 		goto err_m2m_release;
1002 	}
1003 
1004 	v4l2_fh_init(&inst->fh, core->vdev_dec);
1005 
1006 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1007 	v4l2_fh_add(&inst->fh);
1008 	inst->fh.m2m_ctx = inst->m2m_ctx;
1009 	file->private_data = &inst->fh;
1010 
1011 	return 0;
1012 
1013 err_m2m_release:
1014 	v4l2_m2m_release(inst->m2m_dev);
1015 err_session_destroy:
1016 	hfi_session_destroy(inst);
1017 err_ctrl_deinit:
1018 	vdec_ctrl_deinit(inst);
1019 err_put_sync:
1020 	pm_runtime_put_sync(core->dev_dec);
1021 err_free_inst:
1022 	kfree(inst);
1023 	return ret;
1024 }
1025 
1026 static int vdec_close(struct file *file)
1027 {
1028 	struct venus_inst *inst = to_inst(file);
1029 
1030 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1031 	v4l2_m2m_release(inst->m2m_dev);
1032 	vdec_ctrl_deinit(inst);
1033 	hfi_session_destroy(inst);
1034 	mutex_destroy(&inst->lock);
1035 	v4l2_fh_del(&inst->fh);
1036 	v4l2_fh_exit(&inst->fh);
1037 
1038 	pm_runtime_put_sync(inst->core->dev_dec);
1039 
1040 	kfree(inst);
1041 	return 0;
1042 }
1043 
1044 static const struct v4l2_file_operations vdec_fops = {
1045 	.owner = THIS_MODULE,
1046 	.open = vdec_open,
1047 	.release = vdec_close,
1048 	.unlocked_ioctl = video_ioctl2,
1049 	.poll = v4l2_m2m_fop_poll,
1050 	.mmap = v4l2_m2m_fop_mmap,
1051 #ifdef CONFIG_COMPAT
1052 	.compat_ioctl32 = v4l2_compat_ioctl32,
1053 #endif
1054 };
1055 
1056 static int vdec_probe(struct platform_device *pdev)
1057 {
1058 	struct device *dev = &pdev->dev;
1059 	struct video_device *vdev;
1060 	struct venus_core *core;
1061 	int ret;
1062 
1063 	if (!dev->parent)
1064 		return -EPROBE_DEFER;
1065 
1066 	core = dev_get_drvdata(dev->parent);
1067 	if (!core)
1068 		return -EPROBE_DEFER;
1069 
1070 	if (core->res->hfi_version == HFI_VERSION_3XX) {
1071 		core->core0_clk = devm_clk_get(dev, "core");
1072 		if (IS_ERR(core->core0_clk))
1073 			return PTR_ERR(core->core0_clk);
1074 	}
1075 
1076 	platform_set_drvdata(pdev, core);
1077 
1078 	vdev = video_device_alloc();
1079 	if (!vdev)
1080 		return -ENOMEM;
1081 
1082 	strlcpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1083 	vdev->release = video_device_release;
1084 	vdev->fops = &vdec_fops;
1085 	vdev->ioctl_ops = &vdec_ioctl_ops;
1086 	vdev->vfl_dir = VFL_DIR_M2M;
1087 	vdev->v4l2_dev = &core->v4l2_dev;
1088 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1089 
1090 	ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1091 	if (ret)
1092 		goto err_vdev_release;
1093 
1094 	core->vdev_dec = vdev;
1095 	core->dev_dec = dev;
1096 
1097 	video_set_drvdata(vdev, core);
1098 	pm_runtime_enable(dev);
1099 
1100 	return 0;
1101 
1102 err_vdev_release:
1103 	video_device_release(vdev);
1104 	return ret;
1105 }
1106 
1107 static int vdec_remove(struct platform_device *pdev)
1108 {
1109 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1110 
1111 	video_unregister_device(core->vdev_dec);
1112 	pm_runtime_disable(core->dev_dec);
1113 
1114 	return 0;
1115 }
1116 
1117 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1118 {
1119 	struct venus_core *core = dev_get_drvdata(dev);
1120 
1121 	if (core->res->hfi_version == HFI_VERSION_1XX)
1122 		return 0;
1123 
1124 	writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1125 	clk_disable_unprepare(core->core0_clk);
1126 	writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1127 
1128 	return 0;
1129 }
1130 
1131 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1132 {
1133 	struct venus_core *core = dev_get_drvdata(dev);
1134 	int ret;
1135 
1136 	if (core->res->hfi_version == HFI_VERSION_1XX)
1137 		return 0;
1138 
1139 	writel(0, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1140 	ret = clk_prepare_enable(core->core0_clk);
1141 	writel(1, core->base + WRAPPER_VDEC_VCODEC_POWER_CONTROL);
1142 
1143 	return ret;
1144 }
1145 
1146 static const struct dev_pm_ops vdec_pm_ops = {
1147 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1148 				pm_runtime_force_resume)
1149 	SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1150 };
1151 
1152 static const struct of_device_id vdec_dt_match[] = {
1153 	{ .compatible = "venus-decoder" },
1154 	{ }
1155 };
1156 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1157 
1158 static struct platform_driver qcom_venus_dec_driver = {
1159 	.probe = vdec_probe,
1160 	.remove = vdec_remove,
1161 	.driver = {
1162 		.name = "qcom-venus-decoder",
1163 		.of_match_table = vdec_dt_match,
1164 		.pm = &vdec_pm_ops,
1165 	},
1166 };
1167 module_platform_driver(qcom_venus_dec_driver);
1168 
1169 MODULE_ALIAS("platform:qcom-venus-decoder");
1170 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1171 MODULE_LICENSE("GPL v2");
1172