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 	if (!fb) {
475 		err = -EBUSY;
476 		mtk_vcodec_err(inst, "fb buffer is NULL");
477 		goto vdec_dec_end;
478 	}
479 
480 	vdec_fb_va = (unsigned long)fb;
481 	y_fb_dma = (u64)fb->base_y.dma_addr;
482 	if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
483 		c_fb_dma =
484 			y_fb_dma + inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
485 	else
486 		c_fb_dma = (u64)fb->base_c.dma_addr;
487 
488 	mtk_vcodec_debug(inst, "[h264-core] y/c addr = 0x%llx 0x%llx", y_fb_dma,
489 			 c_fb_dma);
490 
491 	inst->vsi_core->dec.y_fb_dma = y_fb_dma;
492 	inst->vsi_core->dec.c_fb_dma = c_fb_dma;
493 	inst->vsi_core->dec.vdec_fb_va = vdec_fb_va;
494 	inst->vsi_core->dec.nal_info = share_info->nal_info;
495 	inst->vsi_core->wdma_start_addr =
496 		lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
497 	inst->vsi_core->wdma_end_addr =
498 		lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
499 		lat_buf->ctx->msg_queue.wdma_addr.size;
500 	inst->vsi_core->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
501 	inst->vsi_core->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
502 	inst->vsi_core->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
503 		lat_buf->slice_bc_addr.size;
504 	inst->vsi_core->trans_start = share_info->trans_start;
505 	inst->vsi_core->trans_end = share_info->trans_end;
506 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
507 		mem = &inst->mv_buf[i];
508 		inst->vsi_core->mv_buf_dma[i] = mem->dma_addr;
509 	}
510 
511 	vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
512 	v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
513 
514 	vdec_h264_slice_fill_decode_reflist(inst, &inst->vsi_core->h264_slice_params,
515 					    share_info);
516 
517 	err = vpu_dec_core(vpu);
518 	if (err) {
519 		mtk_vcodec_err(inst, "core decode err=%d", err);
520 		goto vdec_dec_end;
521 	}
522 
523 	/* wait decoder done interrupt */
524 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
525 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
526 	if (timeout)
527 		mtk_vcodec_err(inst, "core decode timeout: pic_%d",
528 			       ctx->decoded_frame_cnt);
529 	inst->vsi_core->dec.timeout = !!timeout;
530 
531 	vpu_dec_core_end(vpu);
532 	mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
533 			 ctx->decoded_frame_cnt,
534 			 inst->vsi_core->dec.crc[0], inst->vsi_core->dec.crc[1],
535 			 inst->vsi_core->dec.crc[2], inst->vsi_core->dec.crc[3],
536 			 inst->vsi_core->dec.crc[4], inst->vsi_core->dec.crc[5],
537 			 inst->vsi_core->dec.crc[6], inst->vsi_core->dec.crc[7]);
538 
539 vdec_dec_end:
540 	vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
541 	ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
542 	mtk_vcodec_debug(inst, "core decode done err=%d", err);
543 	ctx->decoded_frame_cnt++;
544 	return 0;
545 }
546 
vdec_h264_insert_startcode(struct mtk_vcodec_dev * vcodec_dev,unsigned char * buf,size_t * bs_size,struct mtk_h264_pps_param * pps)547 static void vdec_h264_insert_startcode(struct mtk_vcodec_dev *vcodec_dev, unsigned char *buf,
548 				       size_t *bs_size, struct mtk_h264_pps_param *pps)
549 {
550 	struct device *dev = &vcodec_dev->plat_dev->dev;
551 
552 	/* Need to add pending data at the end of bitstream when bs_sz is small than
553 	 * 20 bytes for cavlc bitstream, or lat will decode fail. This pending data is
554 	 * useful for mt8192 and mt8195 platform.
555 	 *
556 	 * cavlc bitstream when entropy_coding_mode_flag is false.
557 	 */
558 	if (pps->entropy_coding_mode_flag || *bs_size > 20 ||
559 	    !(of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-dec") ||
560 	    of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-dec")))
561 		return;
562 
563 	buf[*bs_size] = 0;
564 	buf[*bs_size + 1] = 0;
565 	buf[*bs_size + 2] = 1;
566 	buf[*bs_size + 3] = 0xff;
567 	(*bs_size) += 4;
568 }
569 
vdec_h264_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)570 static int vdec_h264_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
571 				      struct vdec_fb *fb, bool *res_chg)
572 {
573 	struct vdec_h264_slice_inst *inst = h_vdec;
574 	struct vdec_vpu_inst *vpu = &inst->vpu;
575 	struct mtk_video_dec_buf *src_buf_info;
576 	int nal_start_idx, err, timeout = 0, i;
577 	unsigned int data[2];
578 	struct vdec_lat_buf *lat_buf;
579 	struct vdec_h264_slice_share_info *share_info;
580 	unsigned char *buf;
581 	struct mtk_vcodec_mem *mem;
582 
583 	if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
584 				vdec_h264_slice_core_decode,
585 				sizeof(*share_info)))
586 		return -ENOMEM;
587 
588 	/* bs NULL means flush decoder */
589 	if (!bs) {
590 		vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
591 		return vpu_dec_reset(vpu);
592 	}
593 
594 	if (inst->is_field_bitstream)
595 		return -EINVAL;
596 
597 	lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
598 	if (!lat_buf) {
599 		mtk_vcodec_err(inst, "failed to get lat buffer");
600 		return -EINVAL;
601 	}
602 	share_info = lat_buf->private_data;
603 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
604 
605 	buf = (unsigned char *)bs->va;
606 	nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
607 	if (nal_start_idx < 0) {
608 		err = -EINVAL;
609 		goto err_free_fb_out;
610 	}
611 
612 	inst->vsi->dec.nal_info = buf[nal_start_idx];
613 	lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
614 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
615 
616 	err = vdec_h264_slice_fill_decode_parameters(inst, share_info);
617 	if (err)
618 		goto err_free_fb_out;
619 
620 	vdec_h264_insert_startcode(inst->ctx->dev, buf, &bs->size,
621 				   &share_info->h264_slice_params.pps);
622 
623 	inst->vsi->dec.bs_buf_addr = (uint64_t)bs->dma_addr;
624 	inst->vsi->dec.bs_buf_size = bs->size;
625 
626 	*res_chg = inst->resolution_changed;
627 	if (inst->resolution_changed) {
628 		mtk_vcodec_debug(inst, "- resolution changed -");
629 		if (inst->realloc_mv_buf) {
630 			err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
631 			inst->realloc_mv_buf = false;
632 			if (err)
633 				goto err_free_fb_out;
634 		}
635 		inst->resolution_changed = false;
636 	}
637 	for (i = 0; i < H264_MAX_MV_NUM; i++) {
638 		mem = &inst->mv_buf[i];
639 		inst->vsi->mv_buf_dma[i] = mem->dma_addr;
640 	}
641 	inst->vsi->wdma_start_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
642 	inst->vsi->wdma_end_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
643 		lat_buf->ctx->msg_queue.wdma_addr.size;
644 	inst->vsi->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
645 	inst->vsi->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
646 	inst->vsi->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
647 		lat_buf->slice_bc_addr.size;
648 
649 	inst->vsi->trans_end = inst->ctx->msg_queue.wdma_rptr_addr;
650 	inst->vsi->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
651 	mtk_vcodec_debug(inst, "lat:trans(0x%llx 0x%llx) err:0x%llx",
652 			 inst->vsi->wdma_start_addr,
653 			 inst->vsi->wdma_end_addr,
654 			 inst->vsi->wdma_err_addr);
655 
656 	mtk_vcodec_debug(inst, "slice(0x%llx 0x%llx) rprt((0x%llx 0x%llx))",
657 			 inst->vsi->slice_bc_start_addr,
658 			 inst->vsi->slice_bc_end_addr,
659 			 inst->vsi->trans_start,
660 			 inst->vsi->trans_end);
661 	err = vpu_dec_start(vpu, data, 2);
662 	if (err) {
663 		mtk_vcodec_debug(inst, "lat decode err: %d", err);
664 		goto err_free_fb_out;
665 	}
666 
667 	share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
668 		inst->vsi->wdma_end_addr_offset;
669 	share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
670 	share_info->nal_info = inst->vsi->dec.nal_info;
671 
672 	if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
673 		memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
674 		       sizeof(share_info->h264_slice_params));
675 		vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
676 	}
677 
678 	/* wait decoder done interrupt */
679 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
680 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
681 	if (timeout)
682 		mtk_vcodec_err(inst, "lat decode timeout: pic_%d", inst->slice_dec_num);
683 	inst->vsi->dec.timeout = !!timeout;
684 
685 	err = vpu_dec_end(vpu);
686 	if (err == SLICE_HEADER_FULL || err == TRANS_BUFFER_FULL) {
687 		if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
688 			vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
689 		inst->slice_dec_num++;
690 		mtk_vcodec_err(inst, "lat dec fail: pic_%d err:%d", inst->slice_dec_num, err);
691 		return -EINVAL;
692 	}
693 
694 	share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
695 		inst->vsi->wdma_end_addr_offset;
696 	vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
697 
698 	if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
699 		memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
700 		       sizeof(share_info->h264_slice_params));
701 		vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
702 	}
703 	mtk_vcodec_debug(inst, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
704 			 inst->vsi->dec.crc[0], inst->vsi->dec.crc[1], inst->vsi->dec.crc[2]);
705 
706 	inst->slice_dec_num++;
707 	return 0;
708 err_free_fb_out:
709 	vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
710 	mtk_vcodec_err(inst, "slice dec number: %d err: %d", inst->slice_dec_num, err);
711 	return err;
712 }
713 
vdec_h264_slice_single_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)714 static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
715 					 struct vdec_fb *unused, bool *res_chg)
716 {
717 	struct vdec_h264_slice_inst *inst = h_vdec;
718 	struct vdec_vpu_inst *vpu = &inst->vpu;
719 	struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
720 	struct vdec_fb *fb;
721 	unsigned char *buf;
722 	unsigned int data[2], i;
723 	u64 y_fb_dma, c_fb_dma;
724 	struct mtk_vcodec_mem *mem;
725 	int err, nal_start_idx;
726 
727 	/* bs NULL means flush decoder */
728 	if (!bs)
729 		return vpu_dec_reset(vpu);
730 
731 	fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
732 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
733 	dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
734 
735 	y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
736 	c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
737 	mtk_vcodec_debug(inst, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
738 			 inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
739 
740 	inst->vsi_ctx.dec.bs_buf_addr = (u64)bs->dma_addr;
741 	inst->vsi_ctx.dec.bs_buf_size = bs->size;
742 	inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
743 	inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
744 	inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
745 
746 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
747 				   &dst_buf_info->m2m_buf.vb, true);
748 	err = get_vdec_sig_decode_parameters(inst);
749 	if (err)
750 		goto err_free_fb_out;
751 
752 	buf = (unsigned char *)bs->va;
753 	nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
754 	if (nal_start_idx < 0) {
755 		err = -EINVAL;
756 		goto err_free_fb_out;
757 	}
758 	inst->vsi_ctx.dec.nal_info = buf[nal_start_idx];
759 
760 	*res_chg = inst->resolution_changed;
761 	if (inst->resolution_changed) {
762 		mtk_vcodec_debug(inst, "- resolution changed -");
763 		if (inst->realloc_mv_buf) {
764 			err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
765 			inst->realloc_mv_buf = false;
766 			if (err)
767 				goto err_free_fb_out;
768 		}
769 		inst->resolution_changed = false;
770 
771 		for (i = 0; i < H264_MAX_MV_NUM; i++) {
772 			mem = &inst->mv_buf[i];
773 			inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
774 		}
775 	}
776 
777 	memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
778 	err = vpu_dec_start(vpu, data, 2);
779 	if (err)
780 		goto err_free_fb_out;
781 
782 	/* wait decoder done interrupt */
783 	err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
784 					   WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
785 	if (err)
786 		mtk_vcodec_err(inst, "decode timeout: pic_%d",
787 			       inst->ctx->decoded_frame_cnt);
788 
789 	inst->vsi->dec.timeout = !!err;
790 	err = vpu_dec_end(vpu);
791 	if (err)
792 		goto err_free_fb_out;
793 
794 	memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
795 	mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
796 			 inst->ctx->decoded_frame_cnt,
797 			 inst->vsi_ctx.dec.crc[0], inst->vsi_ctx.dec.crc[1],
798 			 inst->vsi_ctx.dec.crc[2], inst->vsi_ctx.dec.crc[3],
799 			 inst->vsi_ctx.dec.crc[4], inst->vsi_ctx.dec.crc[5],
800 			 inst->vsi_ctx.dec.crc[6], inst->vsi_ctx.dec.crc[7]);
801 
802 	inst->ctx->decoded_frame_cnt++;
803 	return 0;
804 
805 err_free_fb_out:
806 	mtk_vcodec_err(inst, "dec frame number: %d err: %d",
807 		       inst->ctx->decoded_frame_cnt, err);
808 	return err;
809 }
810 
vdec_h264_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)811 static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
812 				  struct vdec_fb *unused, bool *res_chg)
813 {
814 	struct vdec_h264_slice_inst *inst = h_vdec;
815 	int ret;
816 
817 	if (!h_vdec)
818 		return -EINVAL;
819 
820 	if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
821 		ret = vdec_h264_slice_single_decode(h_vdec, bs, unused, res_chg);
822 	else
823 		ret = vdec_h264_slice_lat_decode(h_vdec, bs, unused, res_chg);
824 
825 	return ret;
826 }
827 
vdec_h264_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)828 static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
829 				     void *out)
830 {
831 	struct vdec_h264_slice_inst *inst = h_vdec;
832 
833 	switch (type) {
834 	case GET_PARAM_PIC_INFO:
835 		vdec_h264_slice_get_pic_info(inst);
836 		break;
837 	case GET_PARAM_DPB_SIZE:
838 		*(unsigned int *)out = 6;
839 		break;
840 	case GET_PARAM_CROP_INFO:
841 		vdec_h264_slice_get_crop_info(inst, out);
842 		break;
843 	default:
844 		mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
845 		return -EINVAL;
846 	}
847 	return 0;
848 }
849 
850 const struct vdec_common_if vdec_h264_slice_multi_if = {
851 	.init		= vdec_h264_slice_init,
852 	.decode		= vdec_h264_slice_decode,
853 	.get_param	= vdec_h264_slice_get_param,
854 	.deinit		= vdec_h264_slice_deinit,
855 };
856