1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/vmalloc.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-mem2mem.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include "vpu.h"
22 #include "vpu_defs.h"
23 #include "vpu_core.h"
24 #include "vpu_helpers.h"
25 #include "vpu_v4l2.h"
26 #include "vpu_cmds.h"
27 #include "vpu_rpc.h"
28 
29 #define VDEC_FRAME_DEPTH		256
30 #define VDEC_MIN_BUFFER_CAP		8
31 
32 struct vdec_fs_info {
33 	char name[8];
34 	u32 type;
35 	u32 max_count;
36 	u32 req_count;
37 	u32 count;
38 	u32 index;
39 	u32 size;
40 	struct vpu_buffer buffer[32];
41 	u32 tag;
42 };
43 
44 struct vdec_t {
45 	u32 seq_hdr_found;
46 	struct vpu_buffer udata;
47 	struct vpu_decode_params params;
48 	struct vpu_dec_codec_info codec_info;
49 	enum vpu_codec_state state;
50 
51 	struct vpu_vb2_buffer *slots[VB2_MAX_FRAME];
52 	u32 req_frame_count;
53 	struct vdec_fs_info mbi;
54 	struct vdec_fs_info dcp;
55 	u32 seq_tag;
56 
57 	bool reset_codec;
58 	bool fixed_fmt;
59 	u32 decoded_frame_count;
60 	u32 display_frame_count;
61 	u32 sequence;
62 	u32 eos_received;
63 	bool is_source_changed;
64 	u32 source_change;
65 	u32 drain;
66 	u32 ts_pre_count;
67 	u32 frame_depth;
68 };
69 
70 static const struct vpu_format vdec_formats[] = {
71 	{
72 		.pixfmt = V4L2_PIX_FMT_NV12M_8L128,
73 		.num_planes = 2,
74 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
75 	},
76 	{
77 		.pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
78 		.num_planes = 2,
79 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
80 	},
81 	{
82 		.pixfmt = V4L2_PIX_FMT_H264,
83 		.num_planes = 1,
84 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
85 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
86 	},
87 	{
88 		.pixfmt = V4L2_PIX_FMT_H264_MVC,
89 		.num_planes = 1,
90 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
91 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
92 	},
93 	{
94 		.pixfmt = V4L2_PIX_FMT_HEVC,
95 		.num_planes = 1,
96 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
97 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
98 	},
99 	{
100 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
101 		.num_planes = 1,
102 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
103 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
104 	},
105 	{
106 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
107 		.num_planes = 1,
108 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
109 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
110 	},
111 	{
112 		.pixfmt = V4L2_PIX_FMT_MPEG2,
113 		.num_planes = 1,
114 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
115 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
116 	},
117 	{
118 		.pixfmt = V4L2_PIX_FMT_MPEG4,
119 		.num_planes = 1,
120 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
121 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
122 	},
123 	{
124 		.pixfmt = V4L2_PIX_FMT_XVID,
125 		.num_planes = 1,
126 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
127 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
128 	},
129 	{
130 		.pixfmt = V4L2_PIX_FMT_VP8,
131 		.num_planes = 1,
132 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
133 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
134 	},
135 	{
136 		.pixfmt = V4L2_PIX_FMT_H263,
137 		.num_planes = 1,
138 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
139 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION
140 	},
141 	{0, 0, 0, 0},
142 };
143 
144 static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
145 	.g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
146 };
147 
148 static int vdec_ctrl_init(struct vpu_inst *inst)
149 {
150 	struct v4l2_ctrl *ctrl;
151 	int ret;
152 
153 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
154 	if (ret)
155 		return ret;
156 
157 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
158 				 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
159 	if (ctrl)
160 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
161 
162 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
163 				 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
164 	if (ctrl)
165 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
166 
167 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
168 	if (ret) {
169 		dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
170 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
171 		return ret;
172 	}
173 
174 	return 0;
175 }
176 
177 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
178 {
179 	struct vdec_t *vdec = inst->priv;
180 
181 	if (vdec->eos_received) {
182 		if (!vpu_set_last_buffer_dequeued(inst))
183 			vdec->eos_received--;
184 	}
185 }
186 
187 static void vdec_handle_resolution_change(struct vpu_inst *inst)
188 {
189 	struct vdec_t *vdec = inst->priv;
190 	struct vb2_queue *q;
191 
192 	if (!inst->fh.m2m_ctx)
193 		return;
194 
195 	if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
196 		return;
197 	if (!vdec->source_change)
198 		return;
199 
200 	q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
201 	if (!list_empty(&q->done_list))
202 		return;
203 
204 	vdec->source_change--;
205 	vpu_notify_source_change(inst);
206 }
207 
208 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
209 {
210 	struct vdec_t *vdec = inst->priv;
211 	enum vpu_codec_state pre_state = inst->state;
212 
213 	if (state == VPU_CODEC_STATE_SEEK) {
214 		if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
215 			vdec->state = inst->state;
216 		else
217 			vdec->state = VPU_CODEC_STATE_ACTIVE;
218 	}
219 	if (inst->state != VPU_CODEC_STATE_SEEK || force)
220 		inst->state = state;
221 	else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
222 		vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
223 
224 	if (inst->state != pre_state)
225 		vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
226 
227 	if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
228 		vdec_handle_resolution_change(inst);
229 
230 	return 0;
231 }
232 
233 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
234 {
235 	strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
236 	strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
237 	strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
238 
239 	return 0;
240 }
241 
242 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
243 {
244 	struct vpu_inst *inst = to_inst(file);
245 	struct vdec_t *vdec = inst->priv;
246 	const struct vpu_format *fmt;
247 	int ret = -EINVAL;
248 
249 	vpu_inst_lock(inst);
250 	if (!V4L2_TYPE_IS_OUTPUT(f->type) && vdec->fixed_fmt) {
251 		if (f->index == 0) {
252 			f->pixelformat = inst->cap_format.pixfmt;
253 			f->flags = inst->cap_format.flags;
254 			ret = 0;
255 		}
256 	} else {
257 		fmt = vpu_helper_enum_format(inst, f->type, f->index);
258 		memset(f->reserved, 0, sizeof(f->reserved));
259 		if (!fmt)
260 			goto exit;
261 
262 		f->pixelformat = fmt->pixfmt;
263 		f->flags = fmt->flags;
264 		ret = 0;
265 	}
266 
267 exit:
268 	vpu_inst_unlock(inst);
269 	return ret;
270 }
271 
272 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
273 {
274 	struct vpu_inst *inst = to_inst(file);
275 	struct vdec_t *vdec = inst->priv;
276 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
277 	struct vpu_format *cur_fmt;
278 	int i;
279 
280 	cur_fmt = vpu_get_format(inst, f->type);
281 
282 	pixmp->pixelformat = cur_fmt->pixfmt;
283 	pixmp->num_planes = cur_fmt->num_planes;
284 	pixmp->width = cur_fmt->width;
285 	pixmp->height = cur_fmt->height;
286 	pixmp->field = cur_fmt->field;
287 	pixmp->flags = cur_fmt->flags;
288 	for (i = 0; i < pixmp->num_planes; i++) {
289 		pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
290 		pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
291 	}
292 
293 	f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
294 	f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
295 	f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
296 	f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
297 
298 	return 0;
299 }
300 
301 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
302 {
303 	struct vpu_inst *inst = to_inst(file);
304 	struct vdec_t *vdec = inst->priv;
305 
306 	vpu_try_fmt_common(inst, f);
307 
308 	vpu_inst_lock(inst);
309 	if (vdec->fixed_fmt) {
310 		f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
311 		f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
312 		f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
313 		f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
314 	} else {
315 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
316 		f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
317 		f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
318 		f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
319 	}
320 	vpu_inst_unlock(inst);
321 
322 	return 0;
323 }
324 
325 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
326 {
327 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
328 	const struct vpu_format *fmt;
329 	struct vpu_format *cur_fmt;
330 	struct vb2_queue *q;
331 	struct vdec_t *vdec = inst->priv;
332 	int i;
333 
334 	if (!inst->fh.m2m_ctx)
335 		return -EINVAL;
336 
337 	q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
338 	if (!q)
339 		return -EINVAL;
340 	if (vb2_is_busy(q))
341 		return -EBUSY;
342 
343 	fmt = vpu_try_fmt_common(inst, f);
344 	if (!fmt)
345 		return -EINVAL;
346 
347 	cur_fmt = vpu_get_format(inst, f->type);
348 	if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
349 		if (cur_fmt->pixfmt != fmt->pixfmt) {
350 			vdec->reset_codec = true;
351 			vdec->fixed_fmt = false;
352 		}
353 	}
354 	cur_fmt->pixfmt = fmt->pixfmt;
355 	if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
356 		cur_fmt->num_planes = fmt->num_planes;
357 		cur_fmt->flags = fmt->flags;
358 		cur_fmt->width = pixmp->width;
359 		cur_fmt->height = pixmp->height;
360 		for (i = 0; i < fmt->num_planes; i++) {
361 			cur_fmt->sizeimage[i] = pixmp->plane_fmt[i].sizeimage;
362 			cur_fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
363 		}
364 		if (pixmp->field != V4L2_FIELD_ANY)
365 			cur_fmt->field = pixmp->field;
366 	} else {
367 		pixmp->num_planes = cur_fmt->num_planes;
368 		pixmp->width = cur_fmt->width;
369 		pixmp->height = cur_fmt->height;
370 		for (i = 0; i < pixmp->num_planes; i++) {
371 			pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
372 			pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
373 		}
374 		pixmp->field = cur_fmt->field;
375 	}
376 
377 	if (!vdec->fixed_fmt) {
378 		if (V4L2_TYPE_IS_OUTPUT(f->type)) {
379 			vdec->params.codec_format = cur_fmt->pixfmt;
380 			vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
381 			vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
382 			vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
383 			vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
384 		} else {
385 			vdec->params.output_format = cur_fmt->pixfmt;
386 			inst->crop.left = 0;
387 			inst->crop.top = 0;
388 			inst->crop.width = cur_fmt->width;
389 			inst->crop.height = cur_fmt->height;
390 		}
391 	}
392 
393 	vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
394 		  f->fmt.pix_mp.pixelformat,
395 		  f->fmt.pix_mp.pixelformat >> 8,
396 		  f->fmt.pix_mp.pixelformat >> 16,
397 		  f->fmt.pix_mp.pixelformat >> 24,
398 		  f->fmt.pix_mp.width,
399 		  f->fmt.pix_mp.height);
400 
401 	return 0;
402 }
403 
404 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
405 {
406 	struct vpu_inst *inst = to_inst(file);
407 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
408 	struct vdec_t *vdec = inst->priv;
409 	int ret = 0;
410 
411 	vpu_inst_lock(inst);
412 	ret = vdec_s_fmt_common(inst, f);
413 	if (ret)
414 		goto exit;
415 
416 	if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
417 		struct v4l2_format fc;
418 
419 		memset(&fc, 0, sizeof(fc));
420 		fc.type = inst->cap_format.type;
421 		fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
422 		fc.fmt.pix_mp.width = pixmp->width;
423 		fc.fmt.pix_mp.height = pixmp->height;
424 		vdec_s_fmt_common(inst, &fc);
425 	}
426 
427 	f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
428 	f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
429 	f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
430 	f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
431 
432 exit:
433 	vpu_inst_unlock(inst);
434 	return ret;
435 }
436 
437 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
438 {
439 	struct vpu_inst *inst = to_inst(file);
440 
441 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
442 		return -EINVAL;
443 
444 	switch (s->target) {
445 	case V4L2_SEL_TGT_COMPOSE:
446 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
447 	case V4L2_SEL_TGT_COMPOSE_PADDED:
448 		s->r = inst->crop;
449 		break;
450 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
451 		s->r.left = 0;
452 		s->r.top = 0;
453 		s->r.width = inst->cap_format.width;
454 		s->r.height = inst->cap_format.height;
455 		break;
456 	default:
457 		return -EINVAL;
458 	}
459 
460 	return 0;
461 }
462 
463 static int vdec_drain(struct vpu_inst *inst)
464 {
465 	struct vdec_t *vdec = inst->priv;
466 
467 	if (!inst->fh.m2m_ctx)
468 		return 0;
469 
470 	if (!vdec->drain)
471 		return 0;
472 
473 	if (v4l2_m2m_num_src_bufs_ready(inst->fh.m2m_ctx))
474 		return 0;
475 
476 	if (!vdec->params.frame_count) {
477 		vpu_set_last_buffer_dequeued(inst);
478 		return 0;
479 	}
480 
481 	vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
482 	vdec->params.end_flag = 1;
483 	vpu_iface_set_decode_params(inst, &vdec->params, 1);
484 	vdec->drain = 0;
485 	vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
486 
487 	return 0;
488 }
489 
490 static int vdec_cmd_start(struct vpu_inst *inst)
491 {
492 	switch (inst->state) {
493 	case VPU_CODEC_STATE_STARTED:
494 	case VPU_CODEC_STATE_DRAIN:
495 	case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
496 		vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
497 		break;
498 	default:
499 		break;
500 	}
501 	vpu_process_capture_buffer(inst);
502 	return 0;
503 }
504 
505 static int vdec_cmd_stop(struct vpu_inst *inst)
506 {
507 	struct vdec_t *vdec = inst->priv;
508 
509 	vpu_trace(inst->dev, "[%d]\n", inst->id);
510 
511 	if (inst->state == VPU_CODEC_STATE_DEINIT) {
512 		vpu_set_last_buffer_dequeued(inst);
513 	} else {
514 		vdec->drain = 1;
515 		vdec_drain(inst);
516 	}
517 
518 	return 0;
519 }
520 
521 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
522 {
523 	struct vpu_inst *inst = to_inst(file);
524 	int ret;
525 
526 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
527 	if (ret)
528 		return ret;
529 
530 	vpu_inst_lock(inst);
531 	switch (cmd->cmd) {
532 	case V4L2_DEC_CMD_START:
533 		vdec_cmd_start(inst);
534 		break;
535 	case V4L2_DEC_CMD_STOP:
536 		vdec_cmd_stop(inst);
537 		break;
538 	default:
539 		break;
540 	}
541 	vpu_inst_unlock(inst);
542 
543 	return 0;
544 }
545 
546 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
547 {
548 	switch (sub->type) {
549 	case V4L2_EVENT_EOS:
550 		return v4l2_event_subscribe(fh, sub, 0, NULL);
551 	case V4L2_EVENT_SOURCE_CHANGE:
552 		return v4l2_src_change_event_subscribe(fh, sub);
553 	case V4L2_EVENT_CTRL:
554 		return v4l2_ctrl_subscribe_event(fh, sub);
555 	default:
556 		return -EINVAL;
557 	}
558 
559 	return 0;
560 }
561 
562 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
563 	.vidioc_querycap               = vdec_querycap,
564 	.vidioc_enum_fmt_vid_cap       = vdec_enum_fmt,
565 	.vidioc_enum_fmt_vid_out       = vdec_enum_fmt,
566 	.vidioc_g_fmt_vid_cap_mplane   = vdec_g_fmt,
567 	.vidioc_g_fmt_vid_out_mplane   = vdec_g_fmt,
568 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
569 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
570 	.vidioc_s_fmt_vid_cap_mplane   = vdec_s_fmt,
571 	.vidioc_s_fmt_vid_out_mplane   = vdec_s_fmt,
572 	.vidioc_g_selection            = vdec_g_selection,
573 	.vidioc_try_decoder_cmd        = v4l2_m2m_ioctl_try_decoder_cmd,
574 	.vidioc_decoder_cmd            = vdec_decoder_cmd,
575 	.vidioc_subscribe_event        = vdec_subscribe_event,
576 	.vidioc_unsubscribe_event      = v4l2_event_unsubscribe,
577 	.vidioc_reqbufs                = v4l2_m2m_ioctl_reqbufs,
578 	.vidioc_create_bufs	       = v4l2_m2m_ioctl_create_bufs,
579 	.vidioc_prepare_buf	       = v4l2_m2m_ioctl_prepare_buf,
580 	.vidioc_querybuf               = v4l2_m2m_ioctl_querybuf,
581 	.vidioc_qbuf                   = v4l2_m2m_ioctl_qbuf,
582 	.vidioc_expbuf                 = v4l2_m2m_ioctl_expbuf,
583 	.vidioc_dqbuf                  = v4l2_m2m_ioctl_dqbuf,
584 	.vidioc_streamon               = v4l2_m2m_ioctl_streamon,
585 	.vidioc_streamoff              = v4l2_m2m_ioctl_streamoff,
586 };
587 
588 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
589 {
590 	struct vdec_t *vdec = inst->priv;
591 
592 	if (V4L2_TYPE_IS_OUTPUT(type)) {
593 		if (vdec->ts_pre_count >= vdec->frame_depth)
594 			return false;
595 		return true;
596 	}
597 
598 	if (vdec->req_frame_count)
599 		return true;
600 
601 	return false;
602 }
603 
604 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
605 {
606 	struct vdec_t *vdec = inst->priv;
607 	struct vpu_dec_pic_info *info = arg;
608 	struct vpu_vb2_buffer *vpu_buf;
609 	struct vb2_v4l2_buffer *vbuf;
610 	int ret = 0;
611 
612 	if (!info || info->id >= ARRAY_SIZE(vdec->slots))
613 		return -EINVAL;
614 
615 	vpu_inst_lock(inst);
616 	vpu_buf = vdec->slots[info->id];
617 	if (!vpu_buf) {
618 		dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
619 		ret = -EINVAL;
620 		goto exit;
621 	}
622 	vbuf = &vpu_buf->m2m_buf.vb;
623 	if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
624 		dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
625 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
626 	vdec->decoded_frame_count++;
627 	if (vdec->ts_pre_count >= info->consumed_count)
628 		vdec->ts_pre_count -= info->consumed_count;
629 	else
630 		vdec->ts_pre_count = 0;
631 exit:
632 	vpu_inst_unlock(inst);
633 
634 	return ret;
635 }
636 
637 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
638 {
639 	struct vdec_t *vdec = inst->priv;
640 	int i;
641 
642 	for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
643 		if (!vdec->slots[i])
644 			continue;
645 		if (luma == vdec->slots[i]->luma)
646 			return vdec->slots[i];
647 	}
648 
649 	return NULL;
650 }
651 
652 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
653 {
654 	struct vdec_t *vdec = inst->priv;
655 	struct vpu_vb2_buffer *vpu_buf;
656 	struct vb2_v4l2_buffer *vbuf;
657 	u32 sequence;
658 
659 	if (!frame)
660 		return;
661 
662 	vpu_inst_lock(inst);
663 	sequence = vdec->sequence++;
664 	vpu_buf = vdec_find_buffer(inst, frame->luma);
665 	vpu_inst_unlock(inst);
666 	if (!vpu_buf) {
667 		dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
668 			inst->id, frame->id, frame->luma);
669 		return;
670 	}
671 	if (frame->skipped) {
672 		dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
673 		return;
674 	}
675 
676 	vbuf = &vpu_buf->m2m_buf.vb;
677 	if (vbuf->vb2_buf.index != frame->id)
678 		dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
679 			inst->id, vbuf->vb2_buf.index, frame->id);
680 
681 	if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
682 		dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
683 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
684 	vb2_set_plane_payload(&vbuf->vb2_buf, 0, inst->cap_format.sizeimage[0]);
685 	vb2_set_plane_payload(&vbuf->vb2_buf, 1, inst->cap_format.sizeimage[1]);
686 	vbuf->vb2_buf.timestamp = frame->timestamp;
687 	vbuf->field = inst->cap_format.field;
688 	vbuf->sequence = sequence;
689 	dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, frame->timestamp);
690 
691 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
692 	vpu_inst_lock(inst);
693 	vdec->display_frame_count++;
694 	vpu_inst_unlock(inst);
695 	dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
696 		inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
697 }
698 
699 static void vdec_stop_done(struct vpu_inst *inst)
700 {
701 	struct vdec_t *vdec = inst->priv;
702 
703 	vpu_inst_lock(inst);
704 	vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
705 	vdec->seq_hdr_found = 0;
706 	vdec->req_frame_count = 0;
707 	vdec->reset_codec = false;
708 	vdec->fixed_fmt = false;
709 	vdec->params.end_flag = 0;
710 	vdec->drain = 0;
711 	vdec->ts_pre_count = 0;
712 	vdec->params.frame_count = 0;
713 	vdec->decoded_frame_count = 0;
714 	vdec->display_frame_count = 0;
715 	vdec->sequence = 0;
716 	vdec->eos_received = 0;
717 	vdec->is_source_changed = false;
718 	vdec->source_change = 0;
719 	vpu_inst_unlock(inst);
720 }
721 
722 static bool vdec_check_source_change(struct vpu_inst *inst)
723 {
724 	struct vdec_t *vdec = inst->priv;
725 	const struct vpu_format *fmt;
726 	int i;
727 
728 	if (!inst->fh.m2m_ctx)
729 		return false;
730 
731 	if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
732 		return true;
733 	fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
734 	if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
735 		return true;
736 	if (inst->cap_format.width != vdec->codec_info.decoded_width)
737 		return true;
738 	if (inst->cap_format.height != vdec->codec_info.decoded_height)
739 		return true;
740 	if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
741 		return true;
742 	if (inst->crop.left != vdec->codec_info.offset_x)
743 		return true;
744 	if (inst->crop.top != vdec->codec_info.offset_y)
745 		return true;
746 	if (inst->crop.width != vdec->codec_info.width)
747 		return true;
748 	if (inst->crop.height != vdec->codec_info.height)
749 		return true;
750 	if (fmt && inst->cap_format.num_planes != fmt->num_planes)
751 		return true;
752 	for (i = 0; i < inst->cap_format.num_planes; i++) {
753 		if (inst->cap_format.bytesperline[i] != vdec->codec_info.bytesperline[i])
754 			return true;
755 		if (inst->cap_format.sizeimage[i] != vdec->codec_info.sizeimage[i])
756 			return true;
757 	}
758 
759 	return false;
760 }
761 
762 static void vdec_init_fmt(struct vpu_inst *inst)
763 {
764 	struct vdec_t *vdec = inst->priv;
765 	const struct vpu_format *fmt;
766 	int i;
767 
768 	fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt);
769 	inst->out_format.width = vdec->codec_info.width;
770 	inst->out_format.height = vdec->codec_info.height;
771 	inst->cap_format.width = vdec->codec_info.decoded_width;
772 	inst->cap_format.height = vdec->codec_info.decoded_height;
773 	inst->cap_format.pixfmt = vdec->codec_info.pixfmt;
774 	if (fmt) {
775 		inst->cap_format.num_planes = fmt->num_planes;
776 		inst->cap_format.flags = fmt->flags;
777 	}
778 	for (i = 0; i < inst->cap_format.num_planes; i++) {
779 		inst->cap_format.bytesperline[i] = vdec->codec_info.bytesperline[i];
780 		inst->cap_format.sizeimage[i] = vdec->codec_info.sizeimage[i];
781 	}
782 	if (vdec->codec_info.progressive)
783 		inst->cap_format.field = V4L2_FIELD_NONE;
784 	else
785 		inst->cap_format.field = V4L2_FIELD_SEQ_BT;
786 	if (vdec->codec_info.color_primaries == V4L2_COLORSPACE_DEFAULT)
787 		vdec->codec_info.color_primaries = V4L2_COLORSPACE_REC709;
788 	if (vdec->codec_info.transfer_chars == V4L2_XFER_FUNC_DEFAULT)
789 		vdec->codec_info.transfer_chars = V4L2_XFER_FUNC_709;
790 	if (vdec->codec_info.matrix_coeffs == V4L2_YCBCR_ENC_DEFAULT)
791 		vdec->codec_info.matrix_coeffs = V4L2_YCBCR_ENC_709;
792 	if (vdec->codec_info.full_range == V4L2_QUANTIZATION_DEFAULT)
793 		vdec->codec_info.full_range = V4L2_QUANTIZATION_LIM_RANGE;
794 }
795 
796 static void vdec_init_crop(struct vpu_inst *inst)
797 {
798 	struct vdec_t *vdec = inst->priv;
799 
800 	inst->crop.left = vdec->codec_info.offset_x;
801 	inst->crop.top = vdec->codec_info.offset_y;
802 	inst->crop.width = vdec->codec_info.width;
803 	inst->crop.height = vdec->codec_info.height;
804 }
805 
806 static void vdec_init_mbi(struct vpu_inst *inst)
807 {
808 	struct vdec_t *vdec = inst->priv;
809 
810 	vdec->mbi.size = vdec->codec_info.mbi_size;
811 	vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
812 	scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
813 	vdec->mbi.type = MEM_RES_MBI;
814 	vdec->mbi.tag = vdec->seq_tag;
815 }
816 
817 static void vdec_init_dcp(struct vpu_inst *inst)
818 {
819 	struct vdec_t *vdec = inst->priv;
820 
821 	vdec->dcp.size = vdec->codec_info.dcp_size;
822 	vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
823 	scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
824 	vdec->dcp.type = MEM_RES_DCP;
825 	vdec->dcp.tag = vdec->seq_tag;
826 }
827 
828 static void vdec_request_one_fs(struct vdec_fs_info *fs)
829 {
830 	fs->req_count++;
831 	if (fs->req_count > fs->max_count)
832 		fs->req_count = fs->max_count;
833 }
834 
835 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
836 {
837 	struct vpu_buffer *buffer;
838 
839 	if (!fs->size)
840 		return -EINVAL;
841 
842 	if (fs->count >= fs->req_count)
843 		return -EINVAL;
844 
845 	buffer = &fs->buffer[fs->count];
846 	if (buffer->virt && buffer->length >= fs->size)
847 		return 0;
848 
849 	vpu_free_dma(buffer);
850 	buffer->length = fs->size;
851 	return vpu_alloc_dma(inst->core, buffer);
852 }
853 
854 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
855 {
856 	int ret;
857 
858 	while (fs->count < fs->req_count) {
859 		ret = vdec_alloc_fs_buffer(inst, fs);
860 		if (ret)
861 			break;
862 		fs->count++;
863 	}
864 }
865 
866 static void vdec_clear_fs(struct vdec_fs_info *fs)
867 {
868 	u32 i;
869 
870 	if (!fs)
871 		return;
872 
873 	for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
874 		vpu_free_dma(&fs->buffer[i]);
875 	memset(fs, 0, sizeof(*fs));
876 }
877 
878 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
879 {
880 	struct vpu_fs_info info;
881 	int ret;
882 
883 	if (fs->index >= fs->count)
884 		return 0;
885 
886 	memset(&info, 0, sizeof(info));
887 	info.id = fs->index;
888 	info.type = fs->type;
889 	info.tag = fs->tag;
890 	info.luma_addr = fs->buffer[fs->index].phys;
891 	info.luma_size = fs->buffer[fs->index].length;
892 	ret = vpu_session_alloc_fs(inst, &info);
893 	if (ret)
894 		return ret;
895 
896 	fs->index++;
897 	return 0;
898 }
899 
900 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
901 {
902 	struct vdec_t *vdec = inst->priv;
903 	struct vpu_fs_info info;
904 
905 	if (!vdec->req_frame_count)
906 		return 0;
907 
908 	memset(&info, 0, sizeof(info));
909 	info.type = MEM_RES_FRAME;
910 	info.tag = vdec->seq_tag + 0xf0;
911 	vpu_session_alloc_fs(inst, &info);
912 	vdec->req_frame_count--;
913 
914 	return 0;
915 }
916 
917 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
918 {
919 	struct vdec_t *vdec = inst->priv;
920 	struct vpu_vb2_buffer *vpu_buf;
921 	struct vpu_fs_info info;
922 	int ret;
923 
924 	if (inst->state != VPU_CODEC_STATE_ACTIVE)
925 		return -EINVAL;
926 
927 	if (!vdec->req_frame_count)
928 		return -EINVAL;
929 
930 	if (!vbuf)
931 		return -EINVAL;
932 
933 	if (vdec->slots[vbuf->vb2_buf.index]) {
934 		dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
935 			inst->id, vbuf->vb2_buf.index);
936 		return -EINVAL;
937 	}
938 
939 	dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
940 		inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
941 	vpu_buf = to_vpu_vb2_buffer(vbuf);
942 
943 	memset(&info, 0, sizeof(info));
944 	info.id = vbuf->vb2_buf.index;
945 	info.type = MEM_RES_FRAME;
946 	info.tag = vdec->seq_tag;
947 	info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
948 	info.luma_size = inst->cap_format.sizeimage[0];
949 	info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
950 	info.chromau_size = inst->cap_format.sizeimage[1];
951 	info.bytesperline = inst->cap_format.bytesperline[0];
952 	ret = vpu_session_alloc_fs(inst, &info);
953 	if (ret)
954 		return ret;
955 
956 	vpu_buf->tag = info.tag;
957 	vpu_buf->luma = info.luma_addr;
958 	vpu_buf->chroma_u = info.chromau_size;
959 	vpu_buf->chroma_v = 0;
960 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
961 	vdec->slots[info.id] = vpu_buf;
962 	vdec->req_frame_count--;
963 
964 	return 0;
965 }
966 
967 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
968 {
969 	struct vdec_t *vdec = inst->priv;
970 	int i;
971 	int ret;
972 
973 	if (force) {
974 		for (i = vdec->req_frame_count; i > 0; i--)
975 			vdec_response_frame_abnormal(inst);
976 		return;
977 	}
978 
979 	for (i = vdec->req_frame_count; i > 0; i--) {
980 		ret = vpu_process_capture_buffer(inst);
981 		if (ret)
982 			break;
983 		if (vdec->eos_received)
984 			break;
985 	}
986 
987 	for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
988 		if (vdec_response_fs(inst, &vdec->mbi))
989 			break;
990 		if (vdec->eos_received)
991 			break;
992 	}
993 	for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
994 		if (vdec_response_fs(inst, &vdec->dcp))
995 			break;
996 		if (vdec->eos_received)
997 			break;
998 	}
999 }
1000 
1001 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1002 {
1003 	struct vpu_fs_info info;
1004 
1005 	memset(&info, 0, sizeof(info));
1006 	info.id = id;
1007 	info.tag = tag;
1008 	vpu_session_release_fs(inst, &info);
1009 }
1010 
1011 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1012 {
1013 	if (!inst->fh.m2m_ctx)
1014 		return;
1015 	if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1016 		return;
1017 	if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1018 		return;
1019 	v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1020 }
1021 
1022 static void vdec_clear_slots(struct vpu_inst *inst)
1023 {
1024 	struct vdec_t *vdec = inst->priv;
1025 	struct vpu_vb2_buffer *vpu_buf;
1026 	struct vb2_v4l2_buffer *vbuf;
1027 	int i;
1028 
1029 	for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1030 		if (!vdec->slots[i])
1031 			continue;
1032 
1033 		vpu_buf = vdec->slots[i];
1034 		vbuf = &vpu_buf->m2m_buf.vb;
1035 
1036 		vdec_recycle_buffer(inst, vbuf);
1037 		vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1038 		vdec->slots[i] = NULL;
1039 	}
1040 }
1041 
1042 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1043 {
1044 	struct vdec_t *vdec = inst->priv;
1045 
1046 	vpu_inst_lock(inst);
1047 	memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1048 
1049 	vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1050 		  inst->id,
1051 		  vdec->codec_info.decoded_width,
1052 		  vdec->codec_info.decoded_height,
1053 		  vdec->codec_info.offset_x,
1054 		  vdec->codec_info.offset_y,
1055 		  vdec->codec_info.width,
1056 		  vdec->codec_info.height,
1057 		  hdr->num_ref_frms,
1058 		  hdr->num_dpb_frms);
1059 	inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1060 	vdec->is_source_changed = vdec_check_source_change(inst);
1061 	vdec_init_fmt(inst);
1062 	vdec_init_crop(inst);
1063 	vdec_init_mbi(inst);
1064 	vdec_init_dcp(inst);
1065 	if (!vdec->seq_hdr_found) {
1066 		vdec->seq_tag = vdec->codec_info.tag;
1067 		if (vdec->is_source_changed) {
1068 			vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1069 			vpu_notify_source_change(inst);
1070 			vdec->is_source_changed = false;
1071 		}
1072 	}
1073 	if (vdec->seq_tag != vdec->codec_info.tag) {
1074 		vdec_response_fs_request(inst, true);
1075 		vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1076 			  inst->id, vdec->seq_tag, vdec->codec_info.tag);
1077 	}
1078 	vdec->seq_hdr_found++;
1079 	vdec->fixed_fmt = true;
1080 	vpu_inst_unlock(inst);
1081 }
1082 
1083 static void vdec_event_resolution_change(struct vpu_inst *inst)
1084 {
1085 	struct vdec_t *vdec = inst->priv;
1086 
1087 	vpu_trace(inst->dev, "[%d]\n", inst->id);
1088 	vpu_inst_lock(inst);
1089 	vdec->seq_tag = vdec->codec_info.tag;
1090 	vdec_clear_fs(&vdec->mbi);
1091 	vdec_clear_fs(&vdec->dcp);
1092 	vdec_clear_slots(inst);
1093 	vdec_init_mbi(inst);
1094 	vdec_init_dcp(inst);
1095 	if (vdec->is_source_changed) {
1096 		vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1097 		vdec->source_change++;
1098 		vdec_handle_resolution_change(inst);
1099 		vdec->is_source_changed = false;
1100 	}
1101 	vpu_inst_unlock(inst);
1102 }
1103 
1104 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1105 {
1106 	struct vdec_t *vdec = inst->priv;
1107 
1108 	if (!fs)
1109 		return;
1110 
1111 	vpu_inst_lock(inst);
1112 
1113 	switch (fs->type) {
1114 	case MEM_RES_FRAME:
1115 		vdec->req_frame_count++;
1116 		break;
1117 	case MEM_RES_MBI:
1118 		vdec_request_one_fs(&vdec->mbi);
1119 		break;
1120 	case MEM_RES_DCP:
1121 		vdec_request_one_fs(&vdec->dcp);
1122 		break;
1123 	default:
1124 		break;
1125 	}
1126 
1127 	vdec_alloc_fs(inst, &vdec->mbi);
1128 	vdec_alloc_fs(inst, &vdec->dcp);
1129 
1130 	vdec_response_fs_request(inst, false);
1131 
1132 	vpu_inst_unlock(inst);
1133 }
1134 
1135 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1136 {
1137 	struct vdec_t *vdec = inst->priv;
1138 	struct vpu_vb2_buffer *vpu_buf;
1139 	struct vb2_v4l2_buffer *vbuf;
1140 
1141 	if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1142 		return;
1143 	if (fs->type != MEM_RES_FRAME)
1144 		return;
1145 
1146 	if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1147 		dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1148 		return;
1149 	}
1150 
1151 	vpu_inst_lock(inst);
1152 	vpu_buf = vdec->slots[fs->id];
1153 	vdec->slots[fs->id] = NULL;
1154 
1155 	if (!vpu_buf) {
1156 		dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1157 		goto exit;
1158 	}
1159 
1160 	vbuf = &vpu_buf->m2m_buf.vb;
1161 	if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1162 		dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1163 		vdec->sequence++;
1164 	}
1165 
1166 	vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1167 	if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1168 		vdec_recycle_buffer(inst, vbuf);
1169 
1170 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1171 	vpu_process_capture_buffer(inst);
1172 
1173 exit:
1174 	vpu_inst_unlock(inst);
1175 }
1176 
1177 static void vdec_event_eos(struct vpu_inst *inst)
1178 {
1179 	struct vdec_t *vdec = inst->priv;
1180 
1181 	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1182 		  inst->id,
1183 		  vdec->params.frame_count,
1184 		  vdec->decoded_frame_count,
1185 		  vdec->display_frame_count,
1186 		  vdec->sequence);
1187 	vpu_inst_lock(inst);
1188 	vdec->eos_received++;
1189 	vdec->fixed_fmt = false;
1190 	inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1191 	vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
1192 	vdec_set_last_buffer_dequeued(inst);
1193 	vpu_inst_unlock(inst);
1194 }
1195 
1196 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1197 {
1198 	switch (event) {
1199 	case VPU_MSG_ID_SEQ_HDR_FOUND:
1200 		vdec_event_seq_hdr(inst, data);
1201 		break;
1202 	case VPU_MSG_ID_RES_CHANGE:
1203 		vdec_event_resolution_change(inst);
1204 		break;
1205 	case VPU_MSG_ID_FRAME_REQ:
1206 		vdec_event_req_fs(inst, data);
1207 		break;
1208 	case VPU_MSG_ID_FRAME_RELEASE:
1209 		vdec_evnet_rel_fs(inst, data);
1210 		break;
1211 	case VPU_MSG_ID_PIC_EOS:
1212 		vdec_event_eos(inst);
1213 		break;
1214 	default:
1215 		break;
1216 	}
1217 }
1218 
1219 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1220 {
1221 	struct vdec_t *vdec = inst->priv;
1222 	struct vb2_v4l2_buffer *vbuf;
1223 	struct vpu_rpc_buffer_desc desc;
1224 	u32 free_space;
1225 	int ret;
1226 
1227 	vbuf = to_vb2_v4l2_buffer(vb);
1228 	dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1229 		inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1230 
1231 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1232 		return -EINVAL;
1233 	if (vdec->reset_codec)
1234 		return -EINVAL;
1235 
1236 	if (inst->state == VPU_CODEC_STATE_STARTED)
1237 		vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1238 
1239 	ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1240 	if (ret)
1241 		return ret;
1242 
1243 	free_space = vpu_helper_get_free_space(inst);
1244 	if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1245 		return -ENOMEM;
1246 
1247 	ret = vpu_iface_input_frame(inst, vb);
1248 	if (ret < 0)
1249 		return -ENOMEM;
1250 
1251 	dev_dbg(inst->dev, "[%d][INPUT  TS]%32lld\n", inst->id, vb->timestamp);
1252 	vdec->ts_pre_count++;
1253 	vdec->params.frame_count++;
1254 
1255 	v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1256 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1257 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1258 
1259 	if (vdec->drain)
1260 		vdec_drain(inst);
1261 
1262 	return 0;
1263 }
1264 
1265 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1266 {
1267 	struct vdec_t *vdec = inst->priv;
1268 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1269 	int ret;
1270 
1271 	if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1272 		return -EINVAL;
1273 	if (vdec->reset_codec)
1274 		return -EINVAL;
1275 
1276 	ret = vdec_response_frame(inst, vbuf);
1277 	if (ret)
1278 		return ret;
1279 	v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1280 	return 0;
1281 }
1282 
1283 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1284 {
1285 	struct vdec_t *vdec = inst->priv;
1286 
1287 	if (V4L2_TYPE_IS_OUTPUT(type))
1288 		return;
1289 
1290 	vdec_handle_resolution_change(inst);
1291 	if (vdec->eos_received)
1292 		vdec_set_last_buffer_dequeued(inst);
1293 }
1294 
1295 static void vdec_abort(struct vpu_inst *inst)
1296 {
1297 	struct vdec_t *vdec = inst->priv;
1298 	struct vpu_rpc_buffer_desc desc;
1299 	int ret;
1300 
1301 	vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1302 	vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1303 	vdec->params.end_flag = 1;
1304 	vpu_iface_set_decode_params(inst, &vdec->params, 1);
1305 
1306 	vpu_session_abort(inst);
1307 
1308 	ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1309 	if (!ret)
1310 		vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1311 
1312 	vpu_session_rst_buf(inst);
1313 	vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1314 		  inst->id,
1315 		  vdec->params.frame_count,
1316 		  vdec->decoded_frame_count,
1317 		  vdec->display_frame_count,
1318 		  vdec->sequence);
1319 	vdec->params.end_flag = 0;
1320 	vdec->drain = 0;
1321 	vdec->ts_pre_count = 0;
1322 	vdec->params.frame_count = 0;
1323 	vdec->decoded_frame_count = 0;
1324 	vdec->display_frame_count = 0;
1325 	vdec->sequence = 0;
1326 }
1327 
1328 static void vdec_stop(struct vpu_inst *inst, bool free)
1329 {
1330 	struct vdec_t *vdec = inst->priv;
1331 
1332 	vdec_clear_slots(inst);
1333 	if (inst->state != VPU_CODEC_STATE_DEINIT)
1334 		vpu_session_stop(inst);
1335 	vdec_clear_fs(&vdec->mbi);
1336 	vdec_clear_fs(&vdec->dcp);
1337 	if (free) {
1338 		vpu_free_dma(&vdec->udata);
1339 		vpu_free_dma(&inst->stream_buffer);
1340 	}
1341 	vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1342 	vdec->reset_codec = false;
1343 }
1344 
1345 static void vdec_release(struct vpu_inst *inst)
1346 {
1347 	if (inst->id != VPU_INST_NULL_ID)
1348 		vpu_trace(inst->dev, "[%d]\n", inst->id);
1349 	vpu_inst_lock(inst);
1350 	vdec_stop(inst, true);
1351 	vpu_inst_unlock(inst);
1352 }
1353 
1354 static void vdec_cleanup(struct vpu_inst *inst)
1355 {
1356 	struct vdec_t *vdec;
1357 
1358 	if (!inst)
1359 		return;
1360 
1361 	vdec = inst->priv;
1362 	if (vdec)
1363 		vfree(vdec);
1364 	inst->priv = NULL;
1365 	vfree(inst);
1366 }
1367 
1368 static void vdec_init_params(struct vdec_t *vdec)
1369 {
1370 	vdec->params.frame_count = 0;
1371 	vdec->params.end_flag = 0;
1372 }
1373 
1374 static int vdec_start(struct vpu_inst *inst)
1375 {
1376 	struct vdec_t *vdec = inst->priv;
1377 	int stream_buffer_size;
1378 	int ret;
1379 
1380 	if (inst->state != VPU_CODEC_STATE_DEINIT)
1381 		return 0;
1382 
1383 	vpu_trace(inst->dev, "[%d]\n", inst->id);
1384 	if (!vdec->udata.virt) {
1385 		vdec->udata.length = 0x1000;
1386 		ret = vpu_alloc_dma(inst->core, &vdec->udata);
1387 		if (ret) {
1388 			dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1389 			goto error;
1390 		}
1391 	}
1392 
1393 	if (!inst->stream_buffer.virt) {
1394 		stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1395 		if (stream_buffer_size > 0) {
1396 			inst->stream_buffer.length = stream_buffer_size;
1397 			ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1398 			if (ret) {
1399 				dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1400 				goto error;
1401 			}
1402 			inst->use_stream_buffer = true;
1403 		}
1404 	}
1405 
1406 	if (inst->use_stream_buffer)
1407 		vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1408 	vpu_iface_init_instance(inst);
1409 	vdec->params.udata.base = vdec->udata.phys;
1410 	vdec->params.udata.size = vdec->udata.length;
1411 	ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1412 	if (ret) {
1413 		dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1414 		goto error;
1415 	}
1416 
1417 	vdec_init_params(vdec);
1418 	ret = vpu_session_start(inst);
1419 	if (ret) {
1420 		dev_err(inst->dev, "[%d] start fail\n", inst->id);
1421 		goto error;
1422 	}
1423 
1424 	vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1425 
1426 	return 0;
1427 error:
1428 	vpu_free_dma(&vdec->udata);
1429 	vpu_free_dma(&inst->stream_buffer);
1430 	return ret;
1431 }
1432 
1433 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1434 {
1435 	struct vdec_t *vdec = inst->priv;
1436 	int ret = 0;
1437 
1438 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1439 		if (vdec->reset_codec)
1440 			vdec_stop(inst, false);
1441 		if (inst->state == VPU_CODEC_STATE_DEINIT) {
1442 			ret = vdec_start(inst);
1443 			if (ret)
1444 				return ret;
1445 		}
1446 	}
1447 
1448 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1449 		if (inst->state == VPU_CODEC_STATE_SEEK)
1450 			vdec_update_state(inst, vdec->state, 1);
1451 		vdec->eos_received = 0;
1452 		vpu_process_output_buffer(inst);
1453 	} else {
1454 		vdec_cmd_start(inst);
1455 	}
1456 	if (inst->state == VPU_CODEC_STATE_ACTIVE)
1457 		vdec_response_fs_request(inst, false);
1458 
1459 	return ret;
1460 }
1461 
1462 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1463 {
1464 	struct vdec_t *vdec = inst->priv;
1465 
1466 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1467 		return 0;
1468 
1469 	if (V4L2_TYPE_IS_OUTPUT(type)) {
1470 		vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1471 		vdec->drain = 0;
1472 	} else {
1473 		if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1474 			vdec_abort(inst);
1475 
1476 		vdec->eos_received = 0;
1477 		vdec_clear_slots(inst);
1478 	}
1479 
1480 	return 0;
1481 }
1482 
1483 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1484 {
1485 	struct vdec_t *vdec = inst->priv;
1486 	int num = -1;
1487 
1488 	switch (i) {
1489 	case 0:
1490 		num = scnprintf(str, size,
1491 				"req_frame_count = %d\ninterlaced = %d\n",
1492 				vdec->req_frame_count,
1493 				vdec->codec_info.progressive ? 0 : 1);
1494 		break;
1495 	case 1:
1496 		num = scnprintf(str, size,
1497 				"mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1498 				vdec->mbi.size,
1499 				vdec->mbi.req_count,
1500 				vdec->mbi.count,
1501 				vdec->mbi.index);
1502 		break;
1503 	case 2:
1504 		num = scnprintf(str, size,
1505 				"dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1506 				vdec->dcp.size,
1507 				vdec->dcp.req_count,
1508 				vdec->dcp.count,
1509 				vdec->dcp.index);
1510 		break;
1511 	case 3:
1512 		num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1513 		break;
1514 	case 4:
1515 		num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1516 		break;
1517 	case 5:
1518 		num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1519 		break;
1520 	case 6:
1521 		num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1522 		break;
1523 	case 7:
1524 		num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1525 				vdec->drain, vdec->eos_received, vdec->source_change);
1526 		break;
1527 	case 8:
1528 		num = scnprintf(str, size, "ts_pre_count = %d, frame_depth = %d\n",
1529 				vdec->ts_pre_count, vdec->frame_depth);
1530 		break;
1531 	case 9:
1532 		num = scnprintf(str, size, "fps = %d/%d\n",
1533 				vdec->codec_info.frame_rate.numerator,
1534 				vdec->codec_info.frame_rate.denominator);
1535 		break;
1536 	default:
1537 		break;
1538 	}
1539 
1540 	return num;
1541 }
1542 
1543 static struct vpu_inst_ops vdec_inst_ops = {
1544 	.ctrl_init = vdec_ctrl_init,
1545 	.check_ready = vdec_check_ready,
1546 	.buf_done = vdec_buf_done,
1547 	.get_one_frame = vdec_frame_decoded,
1548 	.stop_done = vdec_stop_done,
1549 	.event_notify = vdec_event_notify,
1550 	.release = vdec_release,
1551 	.cleanup = vdec_cleanup,
1552 	.start = vdec_start_session,
1553 	.stop = vdec_stop_session,
1554 	.process_output = vdec_process_output,
1555 	.process_capture = vdec_process_capture,
1556 	.on_queue_empty = vdec_on_queue_empty,
1557 	.get_debug_info = vdec_get_debug_info,
1558 	.wait_prepare = vpu_inst_unlock,
1559 	.wait_finish = vpu_inst_lock,
1560 };
1561 
1562 static void vdec_init(struct file *file)
1563 {
1564 	struct vpu_inst *inst = to_inst(file);
1565 	struct vdec_t *vdec;
1566 	struct v4l2_format f;
1567 
1568 	vdec = inst->priv;
1569 	vdec->frame_depth = VDEC_FRAME_DEPTH;
1570 
1571 	memset(&f, 0, sizeof(f));
1572 	f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1573 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1574 	f.fmt.pix_mp.width = 1280;
1575 	f.fmt.pix_mp.height = 720;
1576 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1577 	vdec_s_fmt(file, &inst->fh, &f);
1578 
1579 	memset(&f, 0, sizeof(f));
1580 	f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1581 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1582 	f.fmt.pix_mp.width = 1280;
1583 	f.fmt.pix_mp.height = 720;
1584 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1585 	vdec_s_fmt(file, &inst->fh, &f);
1586 }
1587 
1588 static int vdec_open(struct file *file)
1589 {
1590 	struct vpu_inst *inst;
1591 	struct vdec_t *vdec;
1592 	int ret;
1593 
1594 	inst = vzalloc(sizeof(*inst));
1595 	if (!inst)
1596 		return -ENOMEM;
1597 
1598 	vdec = vzalloc(sizeof(*vdec));
1599 	if (!vdec) {
1600 		vfree(inst);
1601 		return -ENOMEM;
1602 	}
1603 
1604 	inst->ops = &vdec_inst_ops;
1605 	inst->formats = vdec_formats;
1606 	inst->type = VPU_CORE_TYPE_DEC;
1607 	inst->priv = vdec;
1608 
1609 	ret = vpu_v4l2_open(file, inst);
1610 	if (ret)
1611 		return ret;
1612 
1613 	vdec->fixed_fmt = false;
1614 	inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1615 	vdec_init(file);
1616 
1617 	return 0;
1618 }
1619 
1620 static __poll_t vdec_poll(struct file *file, poll_table *wait)
1621 {
1622 	struct vpu_inst *inst = to_inst(file);
1623 	struct vb2_queue *src_q, *dst_q;
1624 	__poll_t ret;
1625 
1626 	ret = v4l2_m2m_fop_poll(file, wait);
1627 	src_q = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
1628 	dst_q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
1629 	if (vb2_is_streaming(src_q) && !vb2_is_streaming(dst_q))
1630 		ret &= (~EPOLLERR);
1631 	if (!src_q->error && !dst_q->error &&
1632 	    (vb2_is_streaming(src_q) && list_empty(&src_q->queued_list)) &&
1633 	    (vb2_is_streaming(dst_q) && list_empty(&dst_q->queued_list)))
1634 		ret &= (~EPOLLERR);
1635 
1636 	return ret;
1637 }
1638 
1639 static const struct v4l2_file_operations vdec_fops = {
1640 	.owner = THIS_MODULE,
1641 	.open = vdec_open,
1642 	.release = vpu_v4l2_close,
1643 	.unlocked_ioctl = video_ioctl2,
1644 	.poll = vdec_poll,
1645 	.mmap = v4l2_m2m_fop_mmap,
1646 };
1647 
1648 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1649 {
1650 	return &vdec_ioctl_ops;
1651 }
1652 
1653 const struct v4l2_file_operations *vdec_get_fops(void)
1654 {
1655 	return &vdec_fops;
1656 }
1657