1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 MediaTek Inc.
4  * Author: Yunfei Dong <yunfei.dong@mediatek.com>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <media/v4l2-h264.h>
10 #include <media/v4l2-mem2mem.h>
11 #include <media/videobuf2-dma-contig.h>
12 
13 #include "../mtk_vcodec_util.h"
14 #include "../mtk_vcodec_dec.h"
15 #include "../mtk_vcodec_intr.h"
16 #include "../vdec_drv_base.h"
17 #include "../vdec_drv_if.h"
18 #include "../vdec_vpu_if.h"
19 #include "vdec_h264_req_common.h"
20 
21 /**
22  * enum vdec_h264_core_dec_err_type  - core decode error type
23  *
24  * @TRANS_BUFFER_FULL: trans buffer is full
25  * @SLICE_HEADER_FULL: slice header buffer is full
26  */
27 enum vdec_h264_core_dec_err_type {
28 	TRANS_BUFFER_FULL = 1,
29 	SLICE_HEADER_FULL,
30 };
31 
32 /**
33  * struct vdec_h264_slice_lat_dec_param  - parameters for decode current frame
34  *
35  * @sps:		h264 sps syntax parameters
36  * @pps:		h264 pps syntax parameters
37  * @slice_header:	h264 slice header syntax parameters
38  * @scaling_matrix:	h264 scaling list parameters
39  * @decode_params:	decoder parameters of each frame used for hardware decode
40  * @h264_dpb_info:	dpb reference list
41  */
42 struct vdec_h264_slice_lat_dec_param {
43 	struct mtk_h264_sps_param sps;
44 	struct mtk_h264_pps_param pps;
45 	struct mtk_h264_slice_hd_param slice_header;
46 	struct slice_api_h264_scaling_matrix scaling_matrix;
47 	struct slice_api_h264_decode_param decode_params;
48 	struct mtk_h264_dpb_info h264_dpb_info[V4L2_H264_NUM_DPB_ENTRIES];
49 };
50 
51 /**
52  * struct vdec_h264_slice_info - decode information
53  *
54  * @nal_info:		nal info of current picture
55  * @timeout:		Decode timeout: 1 timeout, 0 no timeount
56  * @bs_buf_size:	bitstream size
57  * @bs_buf_addr:	bitstream buffer dma address
58  * @y_fb_dma:		Y frame buffer dma address
59  * @c_fb_dma:		C frame buffer dma address
60  * @vdec_fb_va:	VDEC frame buffer struct virtual address
61  * @crc:		Used to check whether hardware's status is right
62  */
63 struct vdec_h264_slice_info {
64 	u16 nal_info;
65 	u16 timeout;
66 	u32 bs_buf_size;
67 	u64 bs_buf_addr;
68 	u64 y_fb_dma;
69 	u64 c_fb_dma;
70 	u64 vdec_fb_va;
71 	u32 crc[8];
72 };
73 
74 /**
75  * struct vdec_h264_slice_vsi - shared memory for decode information exchange
76  *        between SCP and Host.
77  *
78  * @wdma_err_addr:		wdma error dma address
79  * @wdma_start_addr:		wdma start dma address
80  * @wdma_end_addr:		wdma end dma address
81  * @slice_bc_start_addr:	slice bc start dma address
82  * @slice_bc_end_addr:		slice bc end dma address
83  * @row_info_start_addr:	row info start dma address
84  * @row_info_end_addr:		row info end dma address
85  * @trans_start:		trans start dma address
86  * @trans_end:			trans end dma address
87  * @wdma_end_addr_offset:	wdma end address offset
88  *
89  * @mv_buf_dma:		HW working motion vector buffer
90  *				dma address (AP-W, VPU-R)
91  * @dec:			decode information (AP-R, VPU-W)
92  * @h264_slice_params:		decode parameters for hw used
93  */
94 struct vdec_h264_slice_vsi {
95 	/* LAT dec addr */
96 	u64 wdma_err_addr;
97 	u64 wdma_start_addr;
98 	u64 wdma_end_addr;
99 	u64 slice_bc_start_addr;
100 	u64 slice_bc_end_addr;
101 	u64 row_info_start_addr;
102 	u64 row_info_end_addr;
103 	u64 trans_start;
104 	u64 trans_end;
105 	u64 wdma_end_addr_offset;
106 
107 	u64 mv_buf_dma[H264_MAX_MV_NUM];
108 	struct vdec_h264_slice_info dec;
109 	struct vdec_h264_slice_lat_dec_param h264_slice_params;
110 };
111 
112 /**
113  * struct vdec_h264_slice_share_info - shared information used to exchange
114  *                                     message between lat and core
115  *
116  * @sps:		sequence header information from user space
117  * @dec_params:	decoder params from user space
118  * @h264_slice_params:	decoder params used for hardware
119  * @trans_start:	trans start dma address
120  * @trans_end:		trans end dma address
121  * @nal_info:		nal info of current picture
122  */
123 struct vdec_h264_slice_share_info {
124 	struct v4l2_ctrl_h264_sps sps;
125 	struct v4l2_ctrl_h264_decode_params dec_params;
126 	struct vdec_h264_slice_lat_dec_param h264_slice_params;
127 	u64 trans_start;
128 	u64 trans_end;
129 	u16 nal_info;
130 };
131 
132 /**
133  * struct vdec_h264_slice_inst - h264 decoder instance
134  *
135  * @slice_dec_num:	how many picture be decoded
136  * @ctx:		point to mtk_vcodec_ctx
137  * @pred_buf:		HW working predication buffer
138  * @mv_buf:		HW working motion vector buffer
139  * @vpu:		VPU instance
140  * @vsi:		vsi used for lat
141  * @vsi_core:		vsi used for core
142  *
143  * @vsi_ctx:		Local VSI data for this decoding context
144  * @h264_slice_param:	the parameters that hardware use to decode
145  *
146  * @resolution_changed:resolution changed
147  * @realloc_mv_buf:	reallocate mv buffer
148  * @cap_num_planes:	number of capture queue plane
149  *
150  * @dpb:		decoded picture buffer used to store reference
151  *			buffer information
152  *@is_field_bitstream:	is field bitstream
153  */
154 struct vdec_h264_slice_inst {
155 	unsigned int slice_dec_num;
156 	struct mtk_vcodec_ctx *ctx;
157 	struct mtk_vcodec_mem pred_buf;
158 	struct mtk_vcodec_mem mv_buf[H264_MAX_MV_NUM];
159 	struct vdec_vpu_inst vpu;
160 	struct vdec_h264_slice_vsi *vsi;
161 	struct vdec_h264_slice_vsi *vsi_core;
162 
163 	struct vdec_h264_slice_vsi vsi_ctx;
164 	struct vdec_h264_slice_lat_dec_param h264_slice_param;
165 
166 	unsigned int resolution_changed;
167 	unsigned int realloc_mv_buf;
168 	unsigned int cap_num_planes;
169 
170 	struct v4l2_h264_dpb_entry dpb[16];
171 	bool is_field_bitstream;
172 };
173 
vdec_h264_slice_fill_decode_parameters(struct vdec_h264_slice_inst * inst,struct vdec_h264_slice_share_info * share_info)174 static int vdec_h264_slice_fill_decode_parameters(struct vdec_h264_slice_inst *inst,
175 						  struct vdec_h264_slice_share_info *share_info)
176 {
177 	struct vdec_h264_slice_lat_dec_param *slice_param = &inst->vsi->h264_slice_params;
178 	const struct v4l2_ctrl_h264_decode_params *dec_params;
179 	const struct v4l2_ctrl_h264_scaling_matrix *src_matrix;
180 	const struct v4l2_ctrl_h264_sps *sps;
181 	const struct v4l2_ctrl_h264_pps *pps;
182 
183 	dec_params =
184 		mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
185 	if (IS_ERR(dec_params))
186 		return PTR_ERR(dec_params);
187 
188 	src_matrix =
189 		mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
190 	if (IS_ERR(src_matrix))
191 		return PTR_ERR(src_matrix);
192 
193 	sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
194 	if (IS_ERR(sps))
195 		return PTR_ERR(sps);
196 
197 	pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
198 	if (IS_ERR(pps))
199 		return PTR_ERR(pps);
200 
201 	if (dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC) {
202 		mtk_vcodec_err(inst, "No support for H.264 field decoding.");
203 		inst->is_field_bitstream = true;
204 		return -EINVAL;
205 	}
206 
207 	mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
208 	mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
209 	mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, src_matrix);
210 
211 	memcpy(&share_info->sps, sps, sizeof(*sps));
212 	memcpy(&share_info->dec_params, dec_params, sizeof(*dec_params));
213 
214 	return 0;
215 }
216 
get_vdec_sig_decode_parameters(struct vdec_h264_slice_inst * inst)217 static int get_vdec_sig_decode_parameters(struct vdec_h264_slice_inst *inst)
218 {
219 	const struct v4l2_ctrl_h264_decode_params *dec_params;
220 	const struct v4l2_ctrl_h264_sps *sps;
221 	const struct v4l2_ctrl_h264_pps *pps;
222 	const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
223 	struct vdec_h264_slice_lat_dec_param *slice_param = &inst->h264_slice_param;
224 	struct v4l2_h264_reflist_builder reflist_builder;
225 	struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
226 	struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
227 	struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
228 	u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
229 	u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
230 	u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
231 
232 	dec_params =
233 		mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
234 	if (IS_ERR(dec_params))
235 		return PTR_ERR(dec_params);
236 
237 	sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
238 	if (IS_ERR(sps))
239 		return PTR_ERR(sps);
240 
241 	pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
242 	if (IS_ERR(pps))
243 		return PTR_ERR(pps);
244 
245 	scaling_matrix =
246 		mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
247 	if (IS_ERR(scaling_matrix))
248 		return PTR_ERR(scaling_matrix);
249 
250 	mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
251 
252 	mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
253 	mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
254 	mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, scaling_matrix);
255 
256 	mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params, inst->dpb);
257 	mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
258 				    slice_param->h264_dpb_info);
259 
260 	/* Build the reference lists */
261 	v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps, inst->dpb);
262 	v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
263 	v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
264 
265 	/* Adapt the built lists to the firmware's expectations */
266 	mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
267 	mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
268 	mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
269 
270 	memcpy(&inst->vsi_ctx.h264_slice_params, slice_param,
271 	       sizeof(inst->vsi_ctx.h264_slice_params));
272 
273 	return 0;
274 }
275 
vdec_h264_slice_fill_decode_reflist(struct vdec_h264_slice_inst * inst,struct vdec_h264_slice_lat_dec_param * slice_param,struct vdec_h264_slice_share_info * share_info)276 static void vdec_h264_slice_fill_decode_reflist(struct vdec_h264_slice_inst *inst,
277 						struct vdec_h264_slice_lat_dec_param *slice_param,
278 						struct vdec_h264_slice_share_info *share_info)
279 {
280 	struct v4l2_ctrl_h264_decode_params *dec_params = &share_info->dec_params;
281 	struct v4l2_ctrl_h264_sps *sps = &share_info->sps;
282 	struct v4l2_h264_reflist_builder reflist_builder;
283 	struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
284 	struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
285 	struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
286 	u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
287 	u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
288 	u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
289 
290 	mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
291 
292 	mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params,
293 					 inst->dpb);
294 	mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
295 				    slice_param->h264_dpb_info);
296 
297 	mtk_v4l2_debug(3, "cur poc = %d\n", dec_params->bottom_field_order_cnt);
298 	/* Build the reference lists */
299 	v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps,
300 				       inst->dpb);
301 	v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
302 	v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
303 
304 	/* Adapt the built lists to the firmware's expectations */
305 	mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
306 	mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
307 	mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
308 }
309 
vdec_h264_slice_alloc_mv_buf(struct vdec_h264_slice_inst * inst,struct vdec_pic_info * pic)310 static int vdec_h264_slice_alloc_mv_buf(struct vdec_h264_slice_inst *inst,
311 					struct vdec_pic_info *pic)
312 {
313 	unsigned int buf_sz = mtk_vdec_h264_get_mv_buf_size(pic->buf_w, pic->buf_h);
314 	struct mtk_vcodec_mem *mem;
315 	int i, err;
316 
317 	mtk_v4l2_debug(3, "size = 0x%x", buf_sz);
318 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
319 		mem = &inst->mv_buf[i];
320 		if (mem->va)
321 			mtk_vcodec_mem_free(inst->ctx, mem);
322 		mem->size = buf_sz;
323 		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
324 		if (err) {
325 			mtk_vcodec_err(inst, "failed to allocate mv buf");
326 			return err;
327 		}
328 	}
329 
330 	return 0;
331 }
332 
vdec_h264_slice_free_mv_buf(struct vdec_h264_slice_inst * inst)333 static void vdec_h264_slice_free_mv_buf(struct vdec_h264_slice_inst *inst)
334 {
335 	int i;
336 	struct mtk_vcodec_mem *mem;
337 
338 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
339 		mem = &inst->mv_buf[i];
340 		if (mem->va)
341 			mtk_vcodec_mem_free(inst->ctx, mem);
342 	}
343 }
344 
vdec_h264_slice_get_pic_info(struct vdec_h264_slice_inst * inst)345 static void vdec_h264_slice_get_pic_info(struct vdec_h264_slice_inst *inst)
346 {
347 	struct mtk_vcodec_ctx *ctx = inst->ctx;
348 	u32 data[3];
349 
350 	data[0] = ctx->picinfo.pic_w;
351 	data[1] = ctx->picinfo.pic_h;
352 	data[2] = ctx->capture_fourcc;
353 	vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
354 
355 	ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, VCODEC_DEC_ALIGNED_64);
356 	ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, VCODEC_DEC_ALIGNED_64);
357 	ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
358 	ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
359 	inst->cap_num_planes =
360 		ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
361 
362 	mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
363 			 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
364 			 ctx->picinfo.buf_w, ctx->picinfo.buf_h);
365 	mtk_vcodec_debug(inst, "Y/C(%d, %d)", ctx->picinfo.fb_sz[0],
366 			 ctx->picinfo.fb_sz[1]);
367 
368 	if (ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w ||
369 	    ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h) {
370 		inst->resolution_changed = true;
371 		if (ctx->last_decoded_picinfo.buf_w != ctx->picinfo.buf_w ||
372 		    ctx->last_decoded_picinfo.buf_h != ctx->picinfo.buf_h)
373 			inst->realloc_mv_buf = true;
374 
375 		mtk_v4l2_debug(1, "resChg: (%d %d) : old(%d, %d) -> new(%d, %d)",
376 			       inst->resolution_changed,
377 			       inst->realloc_mv_buf,
378 			       ctx->last_decoded_picinfo.pic_w,
379 			       ctx->last_decoded_picinfo.pic_h,
380 			       ctx->picinfo.pic_w, ctx->picinfo.pic_h);
381 	}
382 }
383 
vdec_h264_slice_get_crop_info(struct vdec_h264_slice_inst * inst,struct v4l2_rect * cr)384 static void vdec_h264_slice_get_crop_info(struct vdec_h264_slice_inst *inst,
385 					  struct v4l2_rect *cr)
386 {
387 	cr->left = 0;
388 	cr->top = 0;
389 	cr->width = inst->ctx->picinfo.pic_w;
390 	cr->height = inst->ctx->picinfo.pic_h;
391 
392 	mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
393 			 cr->left, cr->top, cr->width, cr->height);
394 }
395 
vdec_h264_slice_init(struct mtk_vcodec_ctx * ctx)396 static int vdec_h264_slice_init(struct mtk_vcodec_ctx *ctx)
397 {
398 	struct vdec_h264_slice_inst *inst;
399 	int err, vsi_size;
400 
401 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
402 	if (!inst)
403 		return -ENOMEM;
404 
405 	inst->ctx = ctx;
406 
407 	inst->vpu.id = SCP_IPI_VDEC_LAT;
408 	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
409 	inst->vpu.ctx = ctx;
410 	inst->vpu.codec_type = ctx->current_codec;
411 	inst->vpu.capture_type = ctx->capture_fourcc;
412 
413 	err = vpu_dec_init(&inst->vpu);
414 	if (err) {
415 		mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
416 		goto error_free_inst;
417 	}
418 
419 	vsi_size = round_up(sizeof(struct vdec_h264_slice_vsi), VCODEC_DEC_ALIGNED_64);
420 	inst->vsi = inst->vpu.vsi;
421 	inst->vsi_core =
422 		(struct vdec_h264_slice_vsi *)(((char *)inst->vpu.vsi) + vsi_size);
423 	inst->resolution_changed = true;
424 	inst->realloc_mv_buf = true;
425 
426 	mtk_vcodec_debug(inst, "lat struct size = %d,%d,%d,%d vsi: %d\n",
427 			 (int)sizeof(struct mtk_h264_sps_param),
428 			 (int)sizeof(struct mtk_h264_pps_param),
429 			 (int)sizeof(struct vdec_h264_slice_lat_dec_param),
430 			 (int)sizeof(struct mtk_h264_dpb_info),
431 			 vsi_size);
432 	mtk_vcodec_debug(inst, "lat H264 instance >> %p, codec_type = 0x%x",
433 			 inst, inst->vpu.codec_type);
434 
435 	ctx->drv_handle = inst;
436 	return 0;
437 
438 error_free_inst:
439 	kfree(inst);
440 	return err;
441 }
442 
vdec_h264_slice_deinit(void * h_vdec)443 static void vdec_h264_slice_deinit(void *h_vdec)
444 {
445 	struct vdec_h264_slice_inst *inst = h_vdec;
446 
447 	mtk_vcodec_debug_enter(inst);
448 
449 	vpu_dec_deinit(&inst->vpu);
450 	vdec_h264_slice_free_mv_buf(inst);
451 	vdec_msg_queue_deinit(&inst->ctx->msg_queue, inst->ctx);
452 
453 	kfree(inst);
454 }
455 
vdec_h264_slice_core_decode(struct vdec_lat_buf * lat_buf)456 static int vdec_h264_slice_core_decode(struct vdec_lat_buf *lat_buf)
457 {
458 	struct vdec_fb *fb;
459 	u64 vdec_fb_va;
460 	u64 y_fb_dma, c_fb_dma;
461 	int err, timeout, i;
462 	struct mtk_vcodec_ctx *ctx = lat_buf->ctx;
463 	struct vdec_h264_slice_inst *inst = ctx->drv_handle;
464 	struct vb2_v4l2_buffer *vb2_v4l2;
465 	struct vdec_h264_slice_share_info *share_info = lat_buf->private_data;
466 	struct mtk_vcodec_mem *mem;
467 	struct vdec_vpu_inst *vpu = &inst->vpu;
468 
469 	mtk_vcodec_debug(inst, "[h264-core] vdec_h264 core decode");
470 	memcpy(&inst->vsi_core->h264_slice_params, &share_info->h264_slice_params,
471 	       sizeof(share_info->h264_slice_params));
472 
473 	fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
474 	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
475 	vdec_fb_va = (unsigned long)fb;
476 
477 	if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
478 		c_fb_dma =
479 			y_fb_dma + inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
480 	else
481 		c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
482 
483 	mtk_vcodec_debug(inst, "[h264-core] y/c addr = 0x%llx 0x%llx", y_fb_dma,
484 			 c_fb_dma);
485 
486 	inst->vsi_core->dec.y_fb_dma = y_fb_dma;
487 	inst->vsi_core->dec.c_fb_dma = c_fb_dma;
488 	inst->vsi_core->dec.vdec_fb_va = vdec_fb_va;
489 	inst->vsi_core->dec.nal_info = share_info->nal_info;
490 	inst->vsi_core->wdma_start_addr =
491 		lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
492 	inst->vsi_core->wdma_end_addr =
493 		lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
494 		lat_buf->ctx->msg_queue.wdma_addr.size;
495 	inst->vsi_core->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
496 	inst->vsi_core->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
497 	inst->vsi_core->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
498 		lat_buf->slice_bc_addr.size;
499 	inst->vsi_core->trans_start = share_info->trans_start;
500 	inst->vsi_core->trans_end = share_info->trans_end;
501 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
502 		mem = &inst->mv_buf[i];
503 		inst->vsi_core->mv_buf_dma[i] = mem->dma_addr;
504 	}
505 
506 	vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
507 	v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
508 
509 	vdec_h264_slice_fill_decode_reflist(inst, &inst->vsi_core->h264_slice_params,
510 					    share_info);
511 
512 	err = vpu_dec_core(vpu);
513 	if (err) {
514 		mtk_vcodec_err(inst, "core decode err=%d", err);
515 		goto vdec_dec_end;
516 	}
517 
518 	/* wait decoder done interrupt */
519 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
520 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
521 	if (timeout)
522 		mtk_vcodec_err(inst, "core decode timeout: pic_%d",
523 			       ctx->decoded_frame_cnt);
524 	inst->vsi_core->dec.timeout = !!timeout;
525 
526 	vpu_dec_core_end(vpu);
527 	mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
528 			 ctx->decoded_frame_cnt,
529 			 inst->vsi_core->dec.crc[0], inst->vsi_core->dec.crc[1],
530 			 inst->vsi_core->dec.crc[2], inst->vsi_core->dec.crc[3],
531 			 inst->vsi_core->dec.crc[4], inst->vsi_core->dec.crc[5],
532 			 inst->vsi_core->dec.crc[6], inst->vsi_core->dec.crc[7]);
533 
534 vdec_dec_end:
535 	vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
536 	ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
537 	mtk_vcodec_debug(inst, "core decode done err=%d", err);
538 	ctx->decoded_frame_cnt++;
539 	return 0;
540 }
541 
vdec_h264_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)542 static int vdec_h264_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
543 				      struct vdec_fb *fb, bool *res_chg)
544 {
545 	struct vdec_h264_slice_inst *inst = h_vdec;
546 	struct vdec_vpu_inst *vpu = &inst->vpu;
547 	struct mtk_video_dec_buf *src_buf_info;
548 	int nal_start_idx, err, timeout = 0, i;
549 	unsigned int data[2];
550 	struct vdec_lat_buf *lat_buf;
551 	struct vdec_h264_slice_share_info *share_info;
552 	unsigned char *buf;
553 	struct mtk_vcodec_mem *mem;
554 
555 	if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
556 				vdec_h264_slice_core_decode,
557 				sizeof(*share_info)))
558 		return -ENOMEM;
559 
560 	/* bs NULL means flush decoder */
561 	if (!bs) {
562 		vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
563 		return vpu_dec_reset(vpu);
564 	}
565 
566 	if (inst->is_field_bitstream)
567 		return -EINVAL;
568 
569 	lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
570 	if (!lat_buf) {
571 		mtk_vcodec_err(inst, "failed to get lat buffer");
572 		return -EINVAL;
573 	}
574 	share_info = lat_buf->private_data;
575 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
576 
577 	buf = (unsigned char *)bs->va;
578 	nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
579 	if (nal_start_idx < 0) {
580 		err = -EINVAL;
581 		goto err_free_fb_out;
582 	}
583 
584 	inst->vsi->dec.nal_info = buf[nal_start_idx];
585 	inst->vsi->dec.bs_buf_addr = (u64)bs->dma_addr;
586 	inst->vsi->dec.bs_buf_size = bs->size;
587 
588 	lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
589 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
590 
591 	err = vdec_h264_slice_fill_decode_parameters(inst, share_info);
592 	if (err)
593 		goto err_free_fb_out;
594 
595 	*res_chg = inst->resolution_changed;
596 	if (inst->resolution_changed) {
597 		mtk_vcodec_debug(inst, "- resolution changed -");
598 		if (inst->realloc_mv_buf) {
599 			err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
600 			inst->realloc_mv_buf = false;
601 			if (err)
602 				goto err_free_fb_out;
603 		}
604 		inst->resolution_changed = false;
605 	}
606 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
607 		mem = &inst->mv_buf[i];
608 		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
609 	}
610 	inst->vsi->wdma_start_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
611 	inst->vsi->wdma_end_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
612 		lat_buf->ctx->msg_queue.wdma_addr.size;
613 	inst->vsi->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
614 	inst->vsi->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
615 	inst->vsi->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
616 		lat_buf->slice_bc_addr.size;
617 
618 	inst->vsi->trans_end = inst->ctx->msg_queue.wdma_rptr_addr;
619 	inst->vsi->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
620 	mtk_vcodec_debug(inst, "lat:trans(0x%llx 0x%llx) err:0x%llx",
621 			 inst->vsi->wdma_start_addr,
622 			 inst->vsi->wdma_end_addr,
623 			 inst->vsi->wdma_err_addr);
624 
625 	mtk_vcodec_debug(inst, "slice(0x%llx 0x%llx) rprt((0x%llx 0x%llx))",
626 			 inst->vsi->slice_bc_start_addr,
627 			 inst->vsi->slice_bc_end_addr,
628 			 inst->vsi->trans_start,
629 			 inst->vsi->trans_end);
630 	err = vpu_dec_start(vpu, data, 2);
631 	if (err) {
632 		mtk_vcodec_debug(inst, "lat decode err: %d", err);
633 		goto err_scp_decode;
634 	}
635 
636 	/* wait decoder done interrupt */
637 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
638 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
639 	inst->vsi->dec.timeout = !!timeout;
640 
641 	err = vpu_dec_end(vpu);
642 	if (err == SLICE_HEADER_FULL || timeout || err == TRANS_BUFFER_FULL) {
643 		err = -EINVAL;
644 		goto err_scp_decode;
645 	}
646 
647 	share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
648 		inst->vsi->wdma_end_addr_offset;
649 	share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
650 	share_info->nal_info = inst->vsi->dec.nal_info;
651 	vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
652 
653 	memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
654 	       sizeof(share_info->h264_slice_params));
655 	vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
656 
657 	inst->slice_dec_num++;
658 	return 0;
659 
660 err_scp_decode:
661 err_free_fb_out:
662 	vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
663 	mtk_vcodec_err(inst, "slice dec number: %d err: %d", inst->slice_dec_num, err);
664 	return err;
665 }
666 
vdec_h264_slice_single_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)667 static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
668 					 struct vdec_fb *unused, bool *res_chg)
669 {
670 	struct vdec_h264_slice_inst *inst = h_vdec;
671 	struct vdec_vpu_inst *vpu = &inst->vpu;
672 	struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
673 	struct vdec_fb *fb;
674 	unsigned char *buf;
675 	unsigned int data[2], i;
676 	u64 y_fb_dma, c_fb_dma;
677 	struct mtk_vcodec_mem *mem;
678 	int err, nal_start_idx;
679 
680 	/* bs NULL means flush decoder */
681 	if (!bs)
682 		return vpu_dec_reset(vpu);
683 
684 	fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
685 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
686 	dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
687 
688 	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
689 	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
690 	mtk_vcodec_debug(inst, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
691 			 inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
692 
693 	inst->vsi_ctx.dec.bs_buf_addr = (u64)bs->dma_addr;
694 	inst->vsi_ctx.dec.bs_buf_size = bs->size;
695 	inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
696 	inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
697 	inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
698 
699 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
700 				   &dst_buf_info->m2m_buf.vb, true);
701 	err = get_vdec_sig_decode_parameters(inst);
702 	if (err)
703 		goto err_free_fb_out;
704 
705 	buf = (unsigned char *)bs->va;
706 	nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
707 	if (nal_start_idx < 0) {
708 		err = -EINVAL;
709 		goto err_free_fb_out;
710 	}
711 	inst->vsi_ctx.dec.nal_info = buf[nal_start_idx];
712 
713 	*res_chg = inst->resolution_changed;
714 	if (inst->resolution_changed) {
715 		mtk_vcodec_debug(inst, "- resolution changed -");
716 		if (inst->realloc_mv_buf) {
717 			err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
718 			inst->realloc_mv_buf = false;
719 			if (err)
720 				goto err_free_fb_out;
721 		}
722 		inst->resolution_changed = false;
723 
724 		for (i = 0; i < H264_MAX_MV_NUM; i++) {
725 			mem = &inst->mv_buf[i];
726 			inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
727 		}
728 	}
729 
730 	memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
731 	err = vpu_dec_start(vpu, data, 2);
732 	if (err)
733 		goto err_free_fb_out;
734 
735 	/* wait decoder done interrupt */
736 	err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
737 					   WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
738 	if (err)
739 		mtk_vcodec_err(inst, "decode timeout: pic_%d",
740 			       inst->ctx->decoded_frame_cnt);
741 
742 	inst->vsi->dec.timeout = !!err;
743 	err = vpu_dec_end(vpu);
744 	if (err)
745 		goto err_free_fb_out;
746 
747 	memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
748 	mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
749 			 inst->ctx->decoded_frame_cnt,
750 			 inst->vsi_ctx.dec.crc[0], inst->vsi_ctx.dec.crc[1],
751 			 inst->vsi_ctx.dec.crc[2], inst->vsi_ctx.dec.crc[3],
752 			 inst->vsi_ctx.dec.crc[4], inst->vsi_ctx.dec.crc[5],
753 			 inst->vsi_ctx.dec.crc[6], inst->vsi_ctx.dec.crc[7]);
754 
755 	inst->ctx->decoded_frame_cnt++;
756 	return 0;
757 
758 err_free_fb_out:
759 	mtk_vcodec_err(inst, "dec frame number: %d err: %d",
760 		       inst->ctx->decoded_frame_cnt, err);
761 	return err;
762 }
763 
vdec_h264_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)764 static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
765 				  struct vdec_fb *unused, bool *res_chg)
766 {
767 	struct vdec_h264_slice_inst *inst = h_vdec;
768 	int ret;
769 
770 	if (!h_vdec)
771 		return -EINVAL;
772 
773 	if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
774 		ret = vdec_h264_slice_single_decode(h_vdec, bs, unused, res_chg);
775 	else
776 		ret = vdec_h264_slice_lat_decode(h_vdec, bs, unused, res_chg);
777 
778 	return ret;
779 }
780 
vdec_h264_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)781 static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
782 				     void *out)
783 {
784 	struct vdec_h264_slice_inst *inst = h_vdec;
785 
786 	switch (type) {
787 	case GET_PARAM_PIC_INFO:
788 		vdec_h264_slice_get_pic_info(inst);
789 		break;
790 	case GET_PARAM_DPB_SIZE:
791 		*(unsigned int *)out = 6;
792 		break;
793 	case GET_PARAM_CROP_INFO:
794 		vdec_h264_slice_get_crop_info(inst, out);
795 		break;
796 	default:
797 		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
798 		return -EINVAL;
799 	}
800 	return 0;
801 }
802 
803 const struct vdec_common_if vdec_h264_slice_multi_if = {
804 	.init		= vdec_h264_slice_init,
805 	.decode		= vdec_h264_slice_decode,
806 	.get_param	= vdec_h264_slice_get_param,
807 	.deinit		= vdec_h264_slice_deinit,
808 };
809