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_dec.h"
14 #include "../../common/mtk_vcodec_intr.h"
15 #include "../vdec_drv_base.h"
16 #include "../vdec_drv_if.h"
17 #include "../vdec_vpu_if.h"
18 #include "vdec_h264_req_common.h"
19
20 /**
21 * enum vdec_h264_core_dec_err_type - core decode error type
22 *
23 * @TRANS_BUFFER_FULL: trans buffer is full
24 * @SLICE_HEADER_FULL: slice header buffer is full
25 */
26 enum vdec_h264_core_dec_err_type {
27 TRANS_BUFFER_FULL = 1,
28 SLICE_HEADER_FULL,
29 };
30
31 /**
32 * struct vdec_h264_slice_lat_dec_param - parameters for decode current frame
33 * (shared data between host and firmware)
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 (shared data between host and firmware)
53 *
54 * @nal_info: nal info of current picture
55 * @timeout: Decode timeout: 1 timeout, 0 no timeout
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 (shared data between host and firmware).
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 * @dec: decode information (AP-R, VPU-W)
91 * @h264_slice_params: decode parameters for hw used
92 */
93 struct vdec_h264_slice_vsi {
94 /* LAT dec addr */
95 u64 wdma_err_addr;
96 u64 wdma_start_addr;
97 u64 wdma_end_addr;
98 u64 slice_bc_start_addr;
99 u64 slice_bc_end_addr;
100 u64 row_info_start_addr;
101 u64 row_info_end_addr;
102 u64 trans_start;
103 u64 trans_end;
104 u64 wdma_end_addr_offset;
105
106 u64 mv_buf_dma[H264_MAX_MV_NUM];
107 struct vdec_h264_slice_info dec;
108 struct vdec_h264_slice_lat_dec_param h264_slice_params;
109 };
110
111 /**
112 * struct vdec_h264_slice_share_info - shared information used to exchange
113 * message between lat and core
114 *
115 * @sps: sequence header information from user space
116 * @dec_params: decoder params from user space
117 * @h264_slice_params: decoder params used for hardware
118 * @trans_start: trans start dma address
119 * @trans_end: trans end dma address
120 * @nal_info: nal info of current picture
121 */
122 struct vdec_h264_slice_share_info {
123 struct v4l2_ctrl_h264_sps sps;
124 struct v4l2_ctrl_h264_decode_params dec_params;
125 struct vdec_h264_slice_lat_dec_param h264_slice_params;
126 u64 trans_start;
127 u64 trans_end;
128 u16 nal_info;
129 };
130
131 /*
132 * struct vdec_h264_slice_mem - memory address and size
133 * (shared data between host and firmware)
134 */
135 struct vdec_h264_slice_mem {
136 union {
137 u64 buf;
138 u64 dma_addr;
139 };
140 union {
141 size_t size;
142 u64 dma_addr_end;
143 };
144 };
145
146 /**
147 * struct vdec_h264_slice_fb - frame buffer for decoding
148 * (shared data between host and firmware)
149 *
150 * @y: current luma buffer address info
151 * @c: current chroma buffer address info
152 */
153 struct vdec_h264_slice_fb {
154 struct vdec_h264_slice_mem y;
155 struct vdec_h264_slice_mem c;
156 };
157
158 /**
159 * struct vdec_h264_slice_info_ext - extend decode information
160 * (shared data between host and firmware)
161 *
162 * @wdma_end_addr_offset: offset from buffer start
163 * @nal_info: nal info of current picture
164 * @timeout: toggles whether a decode operation is timeout
165 * @reserved: reserved
166 * @vdec_fb_va: vdec frame buffer struct virtual address
167 * @crc: displays the hardware status
168 */
169 struct vdec_h264_slice_info_ext {
170 u64 wdma_end_addr_offset;
171 u16 nal_info;
172 u16 timeout;
173 u32 reserved;
174 u64 vdec_fb_va;
175 u32 crc[8];
176 };
177
178 /**
179 * struct vdec_h264_slice_vsi_ext - extend shared memory for decode information exchange
180 * between SCP and Host (shared data between host and firmware).
181 *
182 * @bs: input buffer info
183 * @fb: current y/c buffer
184 *
185 * @ube: buffer used to share date between lat and core
186 * @trans: transcoded buffer used for core decode
187 * @row_info: row info buffer
188 * @err_map: error map buffer
189 * @slice_bc: slice buffer
190 *
191 * @mv_buf_dma: store hardware motion vector data
192 * @dec: decode information (AP-R, VPU-W)
193 * @h264_slice_params: decode parameters used for the hw
194 */
195 struct vdec_h264_slice_vsi_ext {
196 /* LAT dec addr */
197 struct vdec_h264_slice_mem bs;
198 struct vdec_h264_slice_fb fb;
199
200 struct vdec_h264_slice_mem ube;
201 struct vdec_h264_slice_mem trans;
202 struct vdec_h264_slice_mem row_info;
203 struct vdec_h264_slice_mem err_map;
204 struct vdec_h264_slice_mem slice_bc;
205
206 struct vdec_h264_slice_mem mv_buf_dma[H264_MAX_MV_NUM];
207 struct vdec_h264_slice_info_ext dec;
208 struct vdec_h264_slice_lat_dec_param h264_slice_params;
209 };
210
211 /**
212 * struct vdec_h264_slice_inst - h264 decoder instance
213 *
214 * @slice_dec_num: Number of frames to be decoded
215 * @ctx: point to mtk_vcodec_dec_ctx
216 * @pred_buf: HW working prediction buffer
217 * @mv_buf: HW working motion vector buffer
218 * @vpu: VPU instance
219 * @vsi: vsi used for lat
220 * @vsi_core: vsi used for core
221 * @vsi_ctx: vsi data for this decoding context
222 * @vsi_ext: extended vsi used for lat
223 * @vsi_core_ext: extended vsi used for core
224 * @vsi_ctx_ext: extended vsi data for this decoding context
225 * @h264_slice_param: the parameters that hardware use to decode
226 *
227 * @resolution_changed: resolution changed
228 * @realloc_mv_buf: reallocate mv buffer
229 * @cap_num_planes: number of capture queue plane
230 *
231 * @dpb: decoded picture buffer used to store reference
232 * buffer information
233 * @is_field_bitstream: not support field bitstream, only support frame
234 *
235 * @decode: lat decoder pointer for different architectures
236 */
237 struct vdec_h264_slice_inst {
238 unsigned int slice_dec_num;
239 struct mtk_vcodec_dec_ctx *ctx;
240 struct mtk_vcodec_mem pred_buf;
241 struct mtk_vcodec_mem mv_buf[H264_MAX_MV_NUM];
242 struct vdec_vpu_inst vpu;
243 union {
244 struct {
245 struct vdec_h264_slice_vsi *vsi;
246 struct vdec_h264_slice_vsi *vsi_core;
247 struct vdec_h264_slice_vsi vsi_ctx;
248 };
249 struct {
250 struct vdec_h264_slice_vsi_ext *vsi_ext;
251 struct vdec_h264_slice_vsi_ext *vsi_core_ext;
252 struct vdec_h264_slice_vsi_ext vsi_ctx_ext;
253 };
254 };
255 struct vdec_h264_slice_lat_dec_param h264_slice_param;
256
257 unsigned int resolution_changed;
258 unsigned int realloc_mv_buf;
259 unsigned int cap_num_planes;
260
261 struct v4l2_h264_dpb_entry dpb[16];
262 bool is_field_bitstream;
263
264 int (*decode)(void *h_vdec, struct mtk_vcodec_mem *bs,
265 struct vdec_fb *unused, bool *res_chg);
266 };
267
vdec_h264_slice_fill_decode_parameters(struct vdec_h264_slice_inst * inst,struct vdec_h264_slice_share_info * share_info,struct vdec_h264_slice_lat_dec_param * slice_param)268 static int vdec_h264_slice_fill_decode_parameters(struct vdec_h264_slice_inst *inst,
269 struct vdec_h264_slice_share_info *share_info,
270 struct vdec_h264_slice_lat_dec_param *slice_param)
271 {
272 const struct v4l2_ctrl_h264_decode_params *dec_params;
273 const struct v4l2_ctrl_h264_scaling_matrix *src_matrix;
274 const struct v4l2_ctrl_h264_sps *sps;
275 const struct v4l2_ctrl_h264_pps *pps;
276
277 dec_params =
278 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
279 if (IS_ERR(dec_params))
280 return PTR_ERR(dec_params);
281
282 src_matrix =
283 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
284 if (IS_ERR(src_matrix))
285 return PTR_ERR(src_matrix);
286
287 sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
288 if (IS_ERR(sps))
289 return PTR_ERR(sps);
290
291 pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
292 if (IS_ERR(pps))
293 return PTR_ERR(pps);
294
295 if (dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC) {
296 mtk_vdec_err(inst->ctx, "No support for H.264 field decoding.");
297 inst->is_field_bitstream = true;
298 return -EINVAL;
299 }
300
301 mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
302 mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
303 mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, src_matrix);
304
305 memcpy(&share_info->sps, sps, sizeof(*sps));
306 memcpy(&share_info->dec_params, dec_params, sizeof(*dec_params));
307
308 return 0;
309 }
310
get_vdec_sig_decode_parameters(struct vdec_h264_slice_inst * inst)311 static int get_vdec_sig_decode_parameters(struct vdec_h264_slice_inst *inst)
312 {
313 const struct v4l2_ctrl_h264_decode_params *dec_params;
314 const struct v4l2_ctrl_h264_sps *sps;
315 const struct v4l2_ctrl_h264_pps *pps;
316 const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
317 struct vdec_h264_slice_lat_dec_param *slice_param = &inst->h264_slice_param;
318 struct v4l2_h264_reflist_builder reflist_builder;
319 struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
320 struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
321 struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
322 u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
323 u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
324 u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
325
326 dec_params =
327 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
328 if (IS_ERR(dec_params))
329 return PTR_ERR(dec_params);
330
331 sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
332 if (IS_ERR(sps))
333 return PTR_ERR(sps);
334
335 pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
336 if (IS_ERR(pps))
337 return PTR_ERR(pps);
338
339 scaling_matrix =
340 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
341 if (IS_ERR(scaling_matrix))
342 return PTR_ERR(scaling_matrix);
343
344 mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
345
346 mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
347 mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
348 mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, scaling_matrix);
349
350 mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params, inst->dpb);
351 mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
352 slice_param->h264_dpb_info);
353
354 /* Build the reference lists */
355 v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps, inst->dpb);
356 v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
357 v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
358
359 /* Adapt the built lists to the firmware's expectations */
360 mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
361 mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
362 mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
363
364 return 0;
365 }
366
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)367 static void vdec_h264_slice_fill_decode_reflist(struct vdec_h264_slice_inst *inst,
368 struct vdec_h264_slice_lat_dec_param *slice_param,
369 struct vdec_h264_slice_share_info *share_info)
370 {
371 struct v4l2_ctrl_h264_decode_params *dec_params = &share_info->dec_params;
372 struct v4l2_ctrl_h264_sps *sps = &share_info->sps;
373 struct v4l2_h264_reflist_builder reflist_builder;
374 struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
375 struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
376 struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
377 u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
378 u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
379 u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
380
381 mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
382
383 mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params,
384 inst->dpb);
385 mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
386 slice_param->h264_dpb_info);
387
388 mtk_v4l2_vdec_dbg(3, inst->ctx, "cur poc = %d\n", dec_params->bottom_field_order_cnt);
389 /* Build the reference lists */
390 v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps,
391 inst->dpb);
392 v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
393 v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
394
395 /* Adapt the built lists to the firmware's expectations */
396 mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
397 mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
398 mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
399 }
400
vdec_h264_slice_alloc_mv_buf(struct vdec_h264_slice_inst * inst,struct vdec_pic_info * pic)401 static int vdec_h264_slice_alloc_mv_buf(struct vdec_h264_slice_inst *inst,
402 struct vdec_pic_info *pic)
403 {
404 unsigned int buf_sz = mtk_vdec_h264_get_mv_buf_size(pic->buf_w, pic->buf_h);
405 struct mtk_vcodec_mem *mem;
406 int i, err;
407
408 mtk_v4l2_vdec_dbg(3, inst->ctx, "size = 0x%x", buf_sz);
409 for (i = 0; i < H264_MAX_MV_NUM; i++) {
410 mem = &inst->mv_buf[i];
411 if (mem->va)
412 mtk_vcodec_mem_free(inst->ctx, mem);
413 mem->size = buf_sz;
414 err = mtk_vcodec_mem_alloc(inst->ctx, mem);
415 if (err) {
416 mtk_vdec_err(inst->ctx, "failed to allocate mv buf");
417 return err;
418 }
419 }
420
421 return 0;
422 }
423
vdec_h264_slice_free_mv_buf(struct vdec_h264_slice_inst * inst)424 static void vdec_h264_slice_free_mv_buf(struct vdec_h264_slice_inst *inst)
425 {
426 int i;
427 struct mtk_vcodec_mem *mem;
428
429 for (i = 0; i < H264_MAX_MV_NUM; i++) {
430 mem = &inst->mv_buf[i];
431 if (mem->va)
432 mtk_vcodec_mem_free(inst->ctx, mem);
433 }
434 }
435
vdec_h264_slice_get_pic_info(struct vdec_h264_slice_inst * inst)436 static void vdec_h264_slice_get_pic_info(struct vdec_h264_slice_inst *inst)
437 {
438 struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
439 u32 data[3];
440
441 data[0] = ctx->picinfo.pic_w;
442 data[1] = ctx->picinfo.pic_h;
443 data[2] = ctx->capture_fourcc;
444 vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
445
446 ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, VCODEC_DEC_ALIGNED_64);
447 ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, VCODEC_DEC_ALIGNED_64);
448 ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
449 ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
450 inst->cap_num_planes =
451 ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
452
453 mtk_vdec_debug(ctx, "pic(%d, %d), buf(%d, %d)",
454 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
455 ctx->picinfo.buf_w, ctx->picinfo.buf_h);
456 mtk_vdec_debug(ctx, "Y/C(%d, %d)", ctx->picinfo.fb_sz[0],
457 ctx->picinfo.fb_sz[1]);
458
459 if (ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w ||
460 ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h) {
461 inst->resolution_changed = true;
462 if (ctx->last_decoded_picinfo.buf_w != ctx->picinfo.buf_w ||
463 ctx->last_decoded_picinfo.buf_h != ctx->picinfo.buf_h)
464 inst->realloc_mv_buf = true;
465
466 mtk_v4l2_vdec_dbg(1, inst->ctx, "resChg: (%d %d) : old(%d, %d) -> new(%d, %d)",
467 inst->resolution_changed,
468 inst->realloc_mv_buf,
469 ctx->last_decoded_picinfo.pic_w,
470 ctx->last_decoded_picinfo.pic_h,
471 ctx->picinfo.pic_w, ctx->picinfo.pic_h);
472 }
473 }
474
vdec_h264_slice_get_crop_info(struct vdec_h264_slice_inst * inst,struct v4l2_rect * cr)475 static void vdec_h264_slice_get_crop_info(struct vdec_h264_slice_inst *inst,
476 struct v4l2_rect *cr)
477 {
478 cr->left = 0;
479 cr->top = 0;
480 cr->width = inst->ctx->picinfo.pic_w;
481 cr->height = inst->ctx->picinfo.pic_h;
482
483 mtk_vdec_debug(inst->ctx, "l=%d, t=%d, w=%d, h=%d",
484 cr->left, cr->top, cr->width, cr->height);
485 }
486
vdec_h264_slice_setup_lat_buffer_ext(struct vdec_h264_slice_inst * inst,struct mtk_vcodec_mem * bs,struct vdec_lat_buf * lat_buf)487 static void vdec_h264_slice_setup_lat_buffer_ext(struct vdec_h264_slice_inst *inst,
488 struct mtk_vcodec_mem *bs,
489 struct vdec_lat_buf *lat_buf)
490 {
491 struct mtk_vcodec_mem *mem;
492 int i;
493
494 inst->vsi_ext->bs.dma_addr = (u64)bs->dma_addr;
495 inst->vsi_ext->bs.size = bs->size;
496
497 for (i = 0; i < H264_MAX_MV_NUM; i++) {
498 mem = &inst->mv_buf[i];
499 inst->vsi_ext->mv_buf_dma[i].dma_addr = mem->dma_addr;
500 inst->vsi_ext->mv_buf_dma[i].size = mem->size;
501 }
502 inst->vsi_ext->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
503 inst->vsi_ext->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
504
505 inst->vsi_ext->row_info.dma_addr = 0;
506 inst->vsi_ext->row_info.size = 0;
507
508 inst->vsi_ext->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
509 inst->vsi_ext->err_map.size = lat_buf->wdma_err_addr.size;
510
511 inst->vsi_ext->slice_bc.dma_addr = lat_buf->slice_bc_addr.dma_addr;
512 inst->vsi_ext->slice_bc.size = lat_buf->slice_bc_addr.size;
513
514 inst->vsi_ext->trans.dma_addr_end = inst->ctx->msg_queue.wdma_rptr_addr;
515 inst->vsi_ext->trans.dma_addr = inst->ctx->msg_queue.wdma_wptr_addr;
516 }
517
vdec_h264_slice_setup_core_buffer_ext(struct vdec_h264_slice_inst * inst,struct vdec_h264_slice_share_info * share_info,struct vdec_lat_buf * lat_buf)518 static int vdec_h264_slice_setup_core_buffer_ext(struct vdec_h264_slice_inst *inst,
519 struct vdec_h264_slice_share_info *share_info,
520 struct vdec_lat_buf *lat_buf)
521 {
522 struct mtk_vcodec_mem *mem;
523 struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
524 struct vb2_v4l2_buffer *vb2_v4l2;
525 struct vdec_fb *fb;
526 u64 y_fb_dma, c_fb_dma = 0;
527 int i;
528
529 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
530 if (!fb) {
531 mtk_vdec_err(ctx, "Unable to get a CAPTURE buffer for CAPTURE queue is empty.");
532 return -EBUSY;
533 }
534
535 y_fb_dma = (u64)fb->base_y.dma_addr;
536 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
537 c_fb_dma = y_fb_dma + ctx->picinfo.fb_sz[0];
538 else
539 c_fb_dma = (u64)fb->base_c.dma_addr;
540
541 mtk_vdec_debug(ctx, "[h264-core] y/c addr = 0x%llx 0x%llx", y_fb_dma, c_fb_dma);
542
543 inst->vsi_core_ext->fb.y.dma_addr = y_fb_dma;
544 inst->vsi_core_ext->fb.y.size = ctx->picinfo.fb_sz[0];
545 inst->vsi_core_ext->fb.c.dma_addr = c_fb_dma;
546 inst->vsi_core_ext->fb.c.size = ctx->picinfo.fb_sz[1];
547
548 inst->vsi_core_ext->dec.vdec_fb_va = (unsigned long)fb;
549 inst->vsi_core_ext->dec.nal_info = share_info->nal_info;
550
551 inst->vsi_core_ext->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
552 inst->vsi_core_ext->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
553
554 inst->vsi_core_ext->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
555 inst->vsi_core_ext->err_map.size = lat_buf->wdma_err_addr.size;
556
557 inst->vsi_core_ext->slice_bc.dma_addr = lat_buf->slice_bc_addr.dma_addr;
558 inst->vsi_core_ext->slice_bc.size = lat_buf->slice_bc_addr.size;
559
560 inst->vsi_core_ext->row_info.dma_addr = 0;
561 inst->vsi_core_ext->row_info.size = 0;
562
563 inst->vsi_core_ext->trans.dma_addr = share_info->trans_start;
564 inst->vsi_core_ext->trans.dma_addr_end = share_info->trans_end;
565
566 for (i = 0; i < H264_MAX_MV_NUM; i++) {
567 mem = &inst->mv_buf[i];
568 inst->vsi_core_ext->mv_buf_dma[i].dma_addr = mem->dma_addr;
569 inst->vsi_core_ext->mv_buf_dma[i].size = mem->size;
570 }
571
572 vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
573 v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
574
575 return 0;
576 }
577
vdec_h264_slice_core_decode_ext(struct vdec_lat_buf * lat_buf)578 static int vdec_h264_slice_core_decode_ext(struct vdec_lat_buf *lat_buf)
579 {
580 int err, timeout;
581 struct mtk_vcodec_dec_ctx *ctx = lat_buf->ctx;
582 struct vdec_h264_slice_inst *inst = ctx->drv_handle;
583 struct vdec_h264_slice_share_info *share_info = lat_buf->private_data;
584 struct vdec_vpu_inst *vpu = &inst->vpu;
585
586 memcpy(&inst->vsi_core_ext->h264_slice_params, &share_info->h264_slice_params,
587 sizeof(share_info->h264_slice_params));
588
589 err = vdec_h264_slice_setup_core_buffer_ext(inst, share_info, lat_buf);
590 if (err)
591 goto vdec_dec_end;
592
593 vdec_h264_slice_fill_decode_reflist(inst, &inst->vsi_core_ext->h264_slice_params,
594 share_info);
595 err = vpu_dec_core(vpu);
596 if (err) {
597 mtk_vdec_err(ctx, "core decode err=%d", err);
598 goto vdec_dec_end;
599 }
600
601 /* wait decoder done interrupt */
602 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
603 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
604 if (timeout)
605 mtk_vdec_err(ctx, "core decode timeout: pic_%d", ctx->decoded_frame_cnt);
606 inst->vsi_core_ext->dec.timeout = !!timeout;
607
608 vpu_dec_core_end(vpu);
609
610 /* crc is hardware checksum, can be used to check whether the decoder result is right.*/
611 mtk_vdec_debug(ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
612 ctx->decoded_frame_cnt,
613 inst->vsi_core_ext->dec.crc[0], inst->vsi_core_ext->dec.crc[1],
614 inst->vsi_core_ext->dec.crc[2], inst->vsi_core_ext->dec.crc[3],
615 inst->vsi_core_ext->dec.crc[4], inst->vsi_core_ext->dec.crc[5],
616 inst->vsi_core_ext->dec.crc[6], inst->vsi_core_ext->dec.crc[7]);
617
618 vdec_dec_end:
619 vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
620 ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
621 mtk_vdec_debug(ctx, "core decode done err=%d", err);
622 ctx->decoded_frame_cnt++;
623 return 0;
624 }
625
vdec_h264_slice_core_decode(struct vdec_lat_buf * lat_buf)626 static int vdec_h264_slice_core_decode(struct vdec_lat_buf *lat_buf)
627 {
628 struct vdec_fb *fb;
629 u64 y_fb_dma, c_fb_dma;
630 int err, timeout, i;
631 struct mtk_vcodec_dec_ctx *ctx = lat_buf->ctx;
632 struct vdec_h264_slice_inst *inst = ctx->drv_handle;
633 struct vb2_v4l2_buffer *vb2_v4l2;
634 struct vdec_h264_slice_share_info *share_info = lat_buf->private_data;
635 struct mtk_vcodec_mem *mem;
636 struct vdec_vpu_inst *vpu = &inst->vpu;
637
638 memcpy(&inst->vsi_core->h264_slice_params, &share_info->h264_slice_params,
639 sizeof(share_info->h264_slice_params));
640
641 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
642 if (!fb) {
643 err = -EBUSY;
644 mtk_vdec_err(ctx, "Unable to get a CAPTURE buffer for CAPTURE queue is empty.");
645 goto vdec_dec_end;
646 }
647
648 y_fb_dma = (u64)fb->base_y.dma_addr;
649 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
650 c_fb_dma = y_fb_dma + ctx->picinfo.fb_sz[0];
651 else
652 c_fb_dma = (u64)fb->base_c.dma_addr;
653
654 mtk_vdec_debug(ctx, "[h264-core] y/c addr = 0x%llx 0x%llx", y_fb_dma, c_fb_dma);
655
656 inst->vsi_core->dec.y_fb_dma = y_fb_dma;
657 inst->vsi_core->dec.c_fb_dma = c_fb_dma;
658 inst->vsi_core->dec.vdec_fb_va = (unsigned long)fb;
659 inst->vsi_core->dec.nal_info = share_info->nal_info;
660 inst->vsi_core->wdma_start_addr =
661 lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
662 inst->vsi_core->wdma_end_addr =
663 lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
664 lat_buf->ctx->msg_queue.wdma_addr.size;
665 inst->vsi_core->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
666 inst->vsi_core->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
667 inst->vsi_core->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
668 lat_buf->slice_bc_addr.size;
669 inst->vsi_core->trans_start = share_info->trans_start;
670 inst->vsi_core->trans_end = share_info->trans_end;
671 for (i = 0; i < H264_MAX_MV_NUM; i++) {
672 mem = &inst->mv_buf[i];
673 inst->vsi_core->mv_buf_dma[i] = mem->dma_addr;
674 }
675
676 vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
677 v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
678
679 vdec_h264_slice_fill_decode_reflist(inst, &inst->vsi_core->h264_slice_params,
680 share_info);
681
682 err = vpu_dec_core(vpu);
683 if (err) {
684 mtk_vdec_err(ctx, "core decode err=%d", err);
685 goto vdec_dec_end;
686 }
687
688 /* wait decoder done interrupt */
689 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
690 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
691 if (timeout)
692 mtk_vdec_err(ctx, "core decode timeout: pic_%d", ctx->decoded_frame_cnt);
693 inst->vsi_core->dec.timeout = !!timeout;
694
695 vpu_dec_core_end(vpu);
696
697 /* crc is hardware checksum, can be used to check whether the decoder result is right.*/
698 mtk_vdec_debug(ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
699 ctx->decoded_frame_cnt,
700 inst->vsi_core->dec.crc[0], inst->vsi_core->dec.crc[1],
701 inst->vsi_core->dec.crc[2], inst->vsi_core->dec.crc[3],
702 inst->vsi_core->dec.crc[4], inst->vsi_core->dec.crc[5],
703 inst->vsi_core->dec.crc[6], inst->vsi_core->dec.crc[7]);
704
705 vdec_dec_end:
706 vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
707 ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
708 mtk_vdec_debug(ctx, "core decode done err=%d", err);
709 ctx->decoded_frame_cnt++;
710
711 return 0;
712 }
713
vdec_h264_insert_startcode(struct mtk_vcodec_dec_dev * vcodec_dev,unsigned char * buf,size_t * bs_size,struct mtk_h264_pps_param * pps)714 static void vdec_h264_insert_startcode(struct mtk_vcodec_dec_dev *vcodec_dev, unsigned char *buf,
715 size_t *bs_size, struct mtk_h264_pps_param *pps)
716 {
717 struct device *dev = &vcodec_dev->plat_dev->dev;
718
719 /* Need to add pending data at the end of bitstream when bs_sz is small than
720 * 20 bytes for cavlc bitstream, or lat will decode fail. This pending data is
721 * useful for mt8192 and mt8195 platform.
722 *
723 * cavlc bitstream when entropy_coding_mode_flag is false.
724 */
725 if (pps->entropy_coding_mode_flag || *bs_size > 20 ||
726 !(of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-dec") ||
727 of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-dec")))
728 return;
729
730 buf[*bs_size] = 0;
731 buf[*bs_size + 1] = 0;
732 buf[*bs_size + 2] = 1;
733 buf[*bs_size + 3] = 0xff;
734 (*bs_size) += 4;
735 }
736
vdec_h264_slice_lat_decode_ext(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)737 static int vdec_h264_slice_lat_decode_ext(void *h_vdec, struct mtk_vcodec_mem *bs,
738 struct vdec_fb *fb, bool *res_chg)
739 {
740 struct vdec_h264_slice_inst *inst = h_vdec;
741 struct vdec_vpu_inst *vpu = &inst->vpu;
742 struct mtk_video_dec_buf *src_buf_info;
743 int err, timeout = 0;
744 unsigned int data[2];
745 struct vdec_lat_buf *lat_buf;
746 struct vdec_h264_slice_share_info *share_info;
747
748 if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
749 vdec_h264_slice_core_decode_ext,
750 sizeof(*share_info)))
751 return -ENOMEM;
752
753 /* bs NULL means flush decoder */
754 if (!bs) {
755 vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
756 return vpu_dec_reset(vpu);
757 }
758
759 if (inst->is_field_bitstream)
760 return -EINVAL;
761
762 lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
763 if (!lat_buf) {
764 mtk_vdec_debug(inst->ctx, "failed to get lat buffer");
765 return -EAGAIN;
766 }
767 share_info = lat_buf->private_data;
768 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
769
770 lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
771 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
772
773 err = vdec_h264_slice_fill_decode_parameters(inst, share_info,
774 &inst->vsi_ext->h264_slice_params);
775 if (err)
776 goto err_free_fb_out;
777
778 vdec_h264_insert_startcode(inst->ctx->dev, bs->va, &bs->size,
779 &share_info->h264_slice_params.pps);
780
781 *res_chg = inst->resolution_changed;
782 if (inst->resolution_changed) {
783 mtk_vdec_debug(inst->ctx, "- resolution changed -");
784 if (inst->realloc_mv_buf) {
785 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
786 inst->realloc_mv_buf = false;
787 if (err)
788 goto err_free_fb_out;
789 }
790 inst->resolution_changed = false;
791 }
792
793 vdec_h264_slice_setup_lat_buffer_ext(inst, bs, lat_buf);
794 mtk_vdec_debug(inst->ctx, "lat:trans(0x%llx 0x%lx) err:0x%llx",
795 inst->vsi_ext->ube.dma_addr, (unsigned long)inst->vsi_ext->ube.size,
796 inst->vsi_ext->err_map.dma_addr);
797
798 mtk_vdec_debug(inst->ctx, "slice(0x%llx 0x%lx) rprt((0x%llx 0x%llx))",
799 inst->vsi_ext->slice_bc.dma_addr,
800 (unsigned long)inst->vsi_ext->slice_bc.size,
801 inst->vsi_ext->trans.dma_addr, inst->vsi_ext->trans.dma_addr_end);
802
803 err = vpu_dec_start(vpu, data, 2);
804 if (err) {
805 mtk_vdec_debug(inst->ctx, "lat decode err: %d", err);
806 goto err_free_fb_out;
807 }
808
809 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
810 inst->vsi_ext->dec.wdma_end_addr_offset;
811
812 share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
813 share_info->nal_info = inst->vsi_ext->dec.nal_info;
814
815 if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
816 memcpy(&share_info->h264_slice_params, &inst->vsi_ext->h264_slice_params,
817 sizeof(share_info->h264_slice_params));
818 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
819 }
820
821 /* wait decoder done interrupt */
822 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
823 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
824 if (timeout)
825 mtk_vdec_err(inst->ctx, "lat decode timeout: pic_%d", inst->slice_dec_num);
826 inst->vsi_ext->dec.timeout = !!timeout;
827
828 err = vpu_dec_end(vpu);
829 if (err == SLICE_HEADER_FULL || err == TRANS_BUFFER_FULL) {
830 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
831 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
832 inst->slice_dec_num++;
833 mtk_vdec_err(inst->ctx, "lat dec fail: pic_%d err:%d", inst->slice_dec_num, err);
834 return -EINVAL;
835 }
836
837 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
838 inst->vsi_ext->dec.wdma_end_addr_offset;
839
840 vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
841
842 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
843 memcpy(&share_info->h264_slice_params, &inst->vsi_ext->h264_slice_params,
844 sizeof(share_info->h264_slice_params));
845 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
846 }
847 mtk_vdec_debug(inst->ctx, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
848 inst->vsi_ext->dec.crc[0], inst->vsi_ext->dec.crc[1],
849 inst->vsi_ext->dec.crc[2]);
850
851 inst->slice_dec_num++;
852 return 0;
853 err_free_fb_out:
854 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
855 mtk_vdec_err(inst->ctx, "slice dec number: %d err: %d", inst->slice_dec_num, err);
856 return err;
857 }
858
vdec_h264_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)859 static int vdec_h264_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
860 struct vdec_fb *fb, bool *res_chg)
861 {
862 struct vdec_h264_slice_inst *inst = h_vdec;
863 struct vdec_vpu_inst *vpu = &inst->vpu;
864 struct mtk_video_dec_buf *src_buf_info;
865 int nal_start_idx, err, timeout = 0, i;
866 unsigned int data[2];
867 struct vdec_lat_buf *lat_buf;
868 struct vdec_h264_slice_share_info *share_info;
869 unsigned char *buf;
870 struct mtk_vcodec_mem *mem;
871
872 if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
873 vdec_h264_slice_core_decode,
874 sizeof(*share_info)))
875 return -ENOMEM;
876
877 /* bs NULL means flush decoder */
878 if (!bs) {
879 vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
880 return vpu_dec_reset(vpu);
881 }
882
883 if (inst->is_field_bitstream)
884 return -EINVAL;
885
886 lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
887 if (!lat_buf) {
888 mtk_vdec_debug(inst->ctx, "failed to get lat buffer");
889 return -EAGAIN;
890 }
891 share_info = lat_buf->private_data;
892 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
893
894 buf = (unsigned char *)bs->va;
895 nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
896 if (nal_start_idx < 0) {
897 err = -EINVAL;
898 goto err_free_fb_out;
899 }
900
901 inst->vsi->dec.nal_info = buf[nal_start_idx];
902 lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
903 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
904
905 err = vdec_h264_slice_fill_decode_parameters(inst, share_info,
906 &inst->vsi->h264_slice_params);
907 if (err)
908 goto err_free_fb_out;
909
910 vdec_h264_insert_startcode(inst->ctx->dev, buf, &bs->size,
911 &share_info->h264_slice_params.pps);
912
913 inst->vsi->dec.bs_buf_addr = (uint64_t)bs->dma_addr;
914 inst->vsi->dec.bs_buf_size = bs->size;
915
916 *res_chg = inst->resolution_changed;
917 if (inst->resolution_changed) {
918 mtk_vdec_debug(inst->ctx, "- resolution changed -");
919 if (inst->realloc_mv_buf) {
920 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
921 inst->realloc_mv_buf = false;
922 if (err)
923 goto err_free_fb_out;
924 }
925 inst->resolution_changed = false;
926 }
927 for (i = 0; i < H264_MAX_MV_NUM; i++) {
928 mem = &inst->mv_buf[i];
929 inst->vsi->mv_buf_dma[i] = mem->dma_addr;
930 }
931 inst->vsi->wdma_start_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
932 inst->vsi->wdma_end_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
933 lat_buf->ctx->msg_queue.wdma_addr.size;
934 inst->vsi->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
935 inst->vsi->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
936 inst->vsi->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
937 lat_buf->slice_bc_addr.size;
938
939 inst->vsi->trans_end = inst->ctx->msg_queue.wdma_rptr_addr;
940 inst->vsi->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
941 mtk_vdec_debug(inst->ctx, "lat:trans(0x%llx 0x%llx) err:0x%llx",
942 inst->vsi->wdma_start_addr,
943 inst->vsi->wdma_end_addr,
944 inst->vsi->wdma_err_addr);
945
946 mtk_vdec_debug(inst->ctx, "slice(0x%llx 0x%llx) rprt((0x%llx 0x%llx))",
947 inst->vsi->slice_bc_start_addr,
948 inst->vsi->slice_bc_end_addr,
949 inst->vsi->trans_start,
950 inst->vsi->trans_end);
951 err = vpu_dec_start(vpu, data, 2);
952 if (err) {
953 mtk_vdec_debug(inst->ctx, "lat decode err: %d", err);
954 goto err_free_fb_out;
955 }
956
957 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
958 inst->vsi->wdma_end_addr_offset;
959 share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
960 share_info->nal_info = inst->vsi->dec.nal_info;
961
962 if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
963 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
964 sizeof(share_info->h264_slice_params));
965 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
966 }
967
968 /* wait decoder done interrupt */
969 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
970 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
971 if (timeout)
972 mtk_vdec_err(inst->ctx, "lat decode timeout: pic_%d", inst->slice_dec_num);
973 inst->vsi->dec.timeout = !!timeout;
974
975 err = vpu_dec_end(vpu);
976 if (err == SLICE_HEADER_FULL || err == TRANS_BUFFER_FULL) {
977 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
978 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
979 inst->slice_dec_num++;
980 mtk_vdec_err(inst->ctx, "lat dec fail: pic_%d err:%d", inst->slice_dec_num, err);
981 return -EINVAL;
982 }
983
984 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
985 inst->vsi->wdma_end_addr_offset;
986 vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
987
988 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
989 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
990 sizeof(share_info->h264_slice_params));
991 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
992 }
993 mtk_vdec_debug(inst->ctx, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
994 inst->vsi->dec.crc[0], inst->vsi->dec.crc[1], inst->vsi->dec.crc[2]);
995
996 inst->slice_dec_num++;
997 return 0;
998 err_free_fb_out:
999 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
1000 mtk_vdec_err(inst->ctx, "slice dec number: %d err: %d", inst->slice_dec_num, err);
1001 return err;
1002 }
1003
vdec_h264_slice_single_decode_ext(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)1004 static int vdec_h264_slice_single_decode_ext(void *h_vdec, struct mtk_vcodec_mem *bs,
1005 struct vdec_fb *unused, bool *res_chg)
1006 {
1007 struct vdec_h264_slice_inst *inst = h_vdec;
1008 struct vdec_vpu_inst *vpu = &inst->vpu;
1009 struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
1010 struct vdec_fb *fb;
1011 unsigned int data[2], i;
1012 u64 y_fb_dma, c_fb_dma;
1013 struct mtk_vcodec_mem *mem;
1014 int err;
1015
1016 /* bs NULL means flush decoder */
1017 if (!bs)
1018 return vpu_dec_reset(vpu);
1019
1020 fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
1021 if (!fb) {
1022 mtk_vdec_err(inst->ctx,
1023 "Unable to get a CAPTURE buffer for CAPTURE queue is empty.");
1024 return -ENOMEM;
1025 }
1026
1027 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
1028 dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
1029
1030 y_fb_dma = fb->base_y.dma_addr;
1031 c_fb_dma = fb->base_c.dma_addr;
1032 mtk_vdec_debug(inst->ctx, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
1033 inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
1034
1035 inst->vsi_ctx_ext.bs.dma_addr = (u64)bs->dma_addr;
1036 inst->vsi_ctx_ext.bs.size = bs->size;
1037 inst->vsi_ctx_ext.fb.y.dma_addr = y_fb_dma;
1038 inst->vsi_ctx_ext.fb.c.dma_addr = c_fb_dma;
1039 inst->vsi_ctx_ext.dec.vdec_fb_va = (u64)(uintptr_t)fb;
1040
1041 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
1042 &dst_buf_info->m2m_buf.vb, true);
1043 err = get_vdec_sig_decode_parameters(inst);
1044 if (err)
1045 goto err_free_fb_out;
1046
1047 memcpy(&inst->vsi_ctx_ext.h264_slice_params, &inst->h264_slice_param,
1048 sizeof(inst->vsi_ctx_ext.h264_slice_params));
1049
1050 *res_chg = inst->resolution_changed;
1051 if (inst->resolution_changed) {
1052 mtk_vdec_debug(inst->ctx, "- resolution changed -");
1053 if (inst->realloc_mv_buf) {
1054 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
1055 inst->realloc_mv_buf = false;
1056 if (err)
1057 goto err_free_fb_out;
1058 }
1059 inst->resolution_changed = false;
1060
1061 for (i = 0; i < H264_MAX_MV_NUM; i++) {
1062 mem = &inst->mv_buf[i];
1063 inst->vsi_ctx_ext.mv_buf_dma[i].dma_addr = mem->dma_addr;
1064 }
1065 }
1066
1067 memcpy(inst->vpu.vsi, &inst->vsi_ctx_ext, sizeof(inst->vsi_ctx_ext));
1068 err = vpu_dec_start(vpu, data, 2);
1069 if (err)
1070 goto err_free_fb_out;
1071
1072 /* wait decoder done interrupt */
1073 err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
1074 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
1075 if (err)
1076 mtk_vdec_err(inst->ctx, "decode timeout: pic_%d", inst->ctx->decoded_frame_cnt);
1077
1078 inst->vsi_ext->dec.timeout = !!err;
1079 err = vpu_dec_end(vpu);
1080 if (err)
1081 goto err_free_fb_out;
1082
1083 memcpy(&inst->vsi_ctx_ext, inst->vpu.vsi, sizeof(inst->vsi_ctx_ext));
1084 mtk_vdec_debug(inst->ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
1085 inst->ctx->decoded_frame_cnt,
1086 inst->vsi_ctx_ext.dec.crc[0], inst->vsi_ctx_ext.dec.crc[1],
1087 inst->vsi_ctx_ext.dec.crc[2], inst->vsi_ctx_ext.dec.crc[3],
1088 inst->vsi_ctx_ext.dec.crc[4], inst->vsi_ctx_ext.dec.crc[5],
1089 inst->vsi_ctx_ext.dec.crc[6], inst->vsi_ctx_ext.dec.crc[7]);
1090
1091 inst->ctx->decoded_frame_cnt++;
1092 return 0;
1093
1094 err_free_fb_out:
1095 mtk_vdec_err(inst->ctx, "dec frame number: %d err: %d", inst->ctx->decoded_frame_cnt, err);
1096 return err;
1097 }
1098
vdec_h264_slice_single_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)1099 static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
1100 struct vdec_fb *unused, bool *res_chg)
1101 {
1102 struct vdec_h264_slice_inst *inst = h_vdec;
1103 struct vdec_vpu_inst *vpu = &inst->vpu;
1104 struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
1105 struct vdec_fb *fb;
1106 unsigned char *buf;
1107 unsigned int data[2], i;
1108 u64 y_fb_dma, c_fb_dma;
1109 struct mtk_vcodec_mem *mem;
1110 int err, nal_start_idx;
1111
1112 /* bs NULL means flush decoder */
1113 if (!bs)
1114 return vpu_dec_reset(vpu);
1115
1116 fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
1117 if (!fb) {
1118 mtk_vdec_err(inst->ctx,
1119 "Unable to get a CAPTURE buffer for CAPTURE queue is empty.");
1120 return -ENOMEM;
1121 }
1122
1123 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
1124 dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
1125
1126 y_fb_dma = fb->base_y.dma_addr;
1127 c_fb_dma = fb->base_c.dma_addr;
1128 mtk_vdec_debug(inst->ctx, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
1129 inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
1130
1131 inst->vsi_ctx.dec.bs_buf_addr = (u64)bs->dma_addr;
1132 inst->vsi_ctx.dec.bs_buf_size = bs->size;
1133 inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
1134 inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
1135 inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
1136
1137 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
1138 &dst_buf_info->m2m_buf.vb, true);
1139 err = get_vdec_sig_decode_parameters(inst);
1140 if (err)
1141 goto err_free_fb_out;
1142
1143 memcpy(&inst->vsi_ctx.h264_slice_params, &inst->h264_slice_param,
1144 sizeof(inst->vsi_ctx.h264_slice_params));
1145
1146 buf = (unsigned char *)bs->va;
1147 nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
1148 if (nal_start_idx < 0) {
1149 err = -EINVAL;
1150 goto err_free_fb_out;
1151 }
1152 inst->vsi_ctx.dec.nal_info = buf[nal_start_idx];
1153
1154 *res_chg = inst->resolution_changed;
1155 if (inst->resolution_changed) {
1156 mtk_vdec_debug(inst->ctx, "- resolution changed -");
1157 if (inst->realloc_mv_buf) {
1158 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
1159 inst->realloc_mv_buf = false;
1160 if (err)
1161 goto err_free_fb_out;
1162 }
1163 inst->resolution_changed = false;
1164
1165 for (i = 0; i < H264_MAX_MV_NUM; i++) {
1166 mem = &inst->mv_buf[i];
1167 inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
1168 }
1169 }
1170
1171 memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
1172 err = vpu_dec_start(vpu, data, 2);
1173 if (err)
1174 goto err_free_fb_out;
1175
1176 /* wait decoder done interrupt */
1177 err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
1178 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
1179 if (err)
1180 mtk_vdec_err(inst->ctx, "decode timeout: pic_%d", inst->ctx->decoded_frame_cnt);
1181
1182 inst->vsi->dec.timeout = !!err;
1183 err = vpu_dec_end(vpu);
1184 if (err)
1185 goto err_free_fb_out;
1186
1187 memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
1188 mtk_vdec_debug(inst->ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
1189 inst->ctx->decoded_frame_cnt,
1190 inst->vsi_ctx.dec.crc[0], inst->vsi_ctx.dec.crc[1],
1191 inst->vsi_ctx.dec.crc[2], inst->vsi_ctx.dec.crc[3],
1192 inst->vsi_ctx.dec.crc[4], inst->vsi_ctx.dec.crc[5],
1193 inst->vsi_ctx.dec.crc[6], inst->vsi_ctx.dec.crc[7]);
1194
1195 inst->ctx->decoded_frame_cnt++;
1196 return 0;
1197
1198 err_free_fb_out:
1199 mtk_vdec_err(inst->ctx, "dec frame number: %d err: %d", inst->ctx->decoded_frame_cnt, err);
1200 return err;
1201 }
1202
vdec_h264_slice_init(struct mtk_vcodec_dec_ctx * ctx)1203 static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
1204 {
1205 struct vdec_h264_slice_inst *inst;
1206 int err, vsi_size;
1207 unsigned char *temp;
1208
1209 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1210 if (!inst)
1211 return -ENOMEM;
1212
1213 inst->ctx = ctx;
1214
1215 inst->vpu.id = SCP_IPI_VDEC_LAT;
1216 inst->vpu.core_id = SCP_IPI_VDEC_CORE;
1217 inst->vpu.ctx = ctx;
1218 inst->vpu.codec_type = ctx->current_codec;
1219 inst->vpu.capture_type = ctx->capture_fourcc;
1220
1221 err = vpu_dec_init(&inst->vpu);
1222 if (err) {
1223 mtk_vdec_err(ctx, "vdec_h264 init err=%d", err);
1224 goto error_free_inst;
1225 }
1226
1227 if (IS_VDEC_SUPPORT_EXT(ctx->dev->dec_capability)) {
1228 vsi_size = sizeof(struct vdec_h264_slice_vsi_ext);
1229
1230 vsi_size = round_up(vsi_size, VCODEC_DEC_ALIGNED_64);
1231 inst->vsi_ext = inst->vpu.vsi;
1232 temp = (unsigned char *)inst->vsi_ext;
1233 inst->vsi_core_ext = (struct vdec_h264_slice_vsi_ext *)(temp + vsi_size);
1234
1235 if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
1236 inst->decode = vdec_h264_slice_single_decode_ext;
1237 else
1238 inst->decode = vdec_h264_slice_lat_decode_ext;
1239 } else {
1240 vsi_size = sizeof(struct vdec_h264_slice_vsi);
1241
1242 vsi_size = round_up(vsi_size, VCODEC_DEC_ALIGNED_64);
1243 inst->vsi = inst->vpu.vsi;
1244 temp = (unsigned char *)inst->vsi;
1245 inst->vsi_core = (struct vdec_h264_slice_vsi *)(temp + vsi_size);
1246
1247 if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
1248 inst->decode = vdec_h264_slice_single_decode;
1249 else
1250 inst->decode = vdec_h264_slice_lat_decode;
1251 }
1252 inst->resolution_changed = true;
1253 inst->realloc_mv_buf = true;
1254
1255 mtk_vdec_debug(ctx, "lat struct size = %d,%d,%d,%d vsi: %d\n",
1256 (int)sizeof(struct mtk_h264_sps_param),
1257 (int)sizeof(struct mtk_h264_pps_param),
1258 (int)sizeof(struct vdec_h264_slice_lat_dec_param),
1259 (int)sizeof(struct mtk_h264_dpb_info),
1260 vsi_size);
1261 mtk_vdec_debug(ctx, "lat H264 instance >> %p, codec_type = 0x%x",
1262 inst, inst->vpu.codec_type);
1263
1264 ctx->drv_handle = inst;
1265 return 0;
1266
1267 error_free_inst:
1268 kfree(inst);
1269 return err;
1270 }
1271
vdec_h264_slice_deinit(void * h_vdec)1272 static void vdec_h264_slice_deinit(void *h_vdec)
1273 {
1274 struct vdec_h264_slice_inst *inst = h_vdec;
1275
1276 vpu_dec_deinit(&inst->vpu);
1277 vdec_h264_slice_free_mv_buf(inst);
1278 vdec_msg_queue_deinit(&inst->ctx->msg_queue, inst->ctx);
1279
1280 kfree(inst);
1281 }
1282
vdec_h264_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)1283 static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
1284 struct vdec_fb *unused, bool *res_chg)
1285 {
1286 struct vdec_h264_slice_inst *inst = h_vdec;
1287
1288 if (!h_vdec)
1289 return -EINVAL;
1290
1291 return inst->decode(h_vdec, bs, unused, res_chg);
1292 }
1293
vdec_h264_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)1294 static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
1295 void *out)
1296 {
1297 struct vdec_h264_slice_inst *inst = h_vdec;
1298
1299 switch (type) {
1300 case GET_PARAM_PIC_INFO:
1301 vdec_h264_slice_get_pic_info(inst);
1302 break;
1303 case GET_PARAM_DPB_SIZE:
1304 *(unsigned int *)out = 6;
1305 break;
1306 case GET_PARAM_CROP_INFO:
1307 vdec_h264_slice_get_crop_info(inst, out);
1308 break;
1309 default:
1310 mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
1311 return -EINVAL;
1312 }
1313 return 0;
1314 }
1315
1316 const struct vdec_common_if vdec_h264_slice_multi_if = {
1317 .init = vdec_h264_slice_init,
1318 .decode = vdec_h264_slice_decode,
1319 .get_param = vdec_h264_slice_get_param,
1320 .deinit = vdec_h264_slice_deinit,
1321 };
1322