Merge tag 'loongarch-5.20' of git://git.kernel.org/pub/scm/linux/kernel/git/chenhuaca...
[platform/kernel/linux-starfive.git] / drivers / media / platform / mediatek / vcodec / vdec / vdec_h264_req_multi_if.c
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
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
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
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
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
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
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
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
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
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
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
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         share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
637                 inst->vsi->wdma_end_addr_offset;
638         share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
639         share_info->nal_info = inst->vsi->dec.nal_info;
640
641         if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
642                 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
643                        sizeof(share_info->h264_slice_params));
644                 vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
645         }
646
647         /* wait decoder done interrupt */
648         timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
649                                                WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
650         inst->vsi->dec.timeout = !!timeout;
651
652         err = vpu_dec_end(vpu);
653         if (err == SLICE_HEADER_FULL || timeout || err == TRANS_BUFFER_FULL) {
654                 err = -EINVAL;
655                 goto err_scp_decode;
656         }
657
658         share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
659                 inst->vsi->wdma_end_addr_offset;
660         vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
661
662         if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
663                 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
664                        sizeof(share_info->h264_slice_params));
665                 vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
666         }
667         mtk_vcodec_debug(inst, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
668                          inst->vsi->dec.crc[0], inst->vsi->dec.crc[1], inst->vsi->dec.crc[2]);
669
670         inst->slice_dec_num++;
671         return 0;
672
673 err_scp_decode:
674         if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
675                 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
676 err_free_fb_out:
677         vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
678         mtk_vcodec_err(inst, "slice dec number: %d err: %d", inst->slice_dec_num, err);
679         return err;
680 }
681
682 static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
683                                          struct vdec_fb *unused, bool *res_chg)
684 {
685         struct vdec_h264_slice_inst *inst = h_vdec;
686         struct vdec_vpu_inst *vpu = &inst->vpu;
687         struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
688         struct vdec_fb *fb;
689         unsigned char *buf;
690         unsigned int data[2], i;
691         u64 y_fb_dma, c_fb_dma;
692         struct mtk_vcodec_mem *mem;
693         int err, nal_start_idx;
694
695         /* bs NULL means flush decoder */
696         if (!bs)
697                 return vpu_dec_reset(vpu);
698
699         fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
700         src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
701         dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
702
703         y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
704         c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
705         mtk_vcodec_debug(inst, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
706                          inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
707
708         inst->vsi_ctx.dec.bs_buf_addr = (u64)bs->dma_addr;
709         inst->vsi_ctx.dec.bs_buf_size = bs->size;
710         inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
711         inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
712         inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
713
714         v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
715                                    &dst_buf_info->m2m_buf.vb, true);
716         err = get_vdec_sig_decode_parameters(inst);
717         if (err)
718                 goto err_free_fb_out;
719
720         buf = (unsigned char *)bs->va;
721         nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
722         if (nal_start_idx < 0) {
723                 err = -EINVAL;
724                 goto err_free_fb_out;
725         }
726         inst->vsi_ctx.dec.nal_info = buf[nal_start_idx];
727
728         *res_chg = inst->resolution_changed;
729         if (inst->resolution_changed) {
730                 mtk_vcodec_debug(inst, "- resolution changed -");
731                 if (inst->realloc_mv_buf) {
732                         err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
733                         inst->realloc_mv_buf = false;
734                         if (err)
735                                 goto err_free_fb_out;
736                 }
737                 inst->resolution_changed = false;
738
739                 for (i = 0; i < H264_MAX_MV_NUM; i++) {
740                         mem = &inst->mv_buf[i];
741                         inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
742                 }
743         }
744
745         memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
746         err = vpu_dec_start(vpu, data, 2);
747         if (err)
748                 goto err_free_fb_out;
749
750         /* wait decoder done interrupt */
751         err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
752                                            WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
753         if (err)
754                 mtk_vcodec_err(inst, "decode timeout: pic_%d",
755                                inst->ctx->decoded_frame_cnt);
756
757         inst->vsi->dec.timeout = !!err;
758         err = vpu_dec_end(vpu);
759         if (err)
760                 goto err_free_fb_out;
761
762         memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
763         mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
764                          inst->ctx->decoded_frame_cnt,
765                          inst->vsi_ctx.dec.crc[0], inst->vsi_ctx.dec.crc[1],
766                          inst->vsi_ctx.dec.crc[2], inst->vsi_ctx.dec.crc[3],
767                          inst->vsi_ctx.dec.crc[4], inst->vsi_ctx.dec.crc[5],
768                          inst->vsi_ctx.dec.crc[6], inst->vsi_ctx.dec.crc[7]);
769
770         inst->ctx->decoded_frame_cnt++;
771         return 0;
772
773 err_free_fb_out:
774         mtk_vcodec_err(inst, "dec frame number: %d err: %d",
775                        inst->ctx->decoded_frame_cnt, err);
776         return err;
777 }
778
779 static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
780                                   struct vdec_fb *unused, bool *res_chg)
781 {
782         struct vdec_h264_slice_inst *inst = h_vdec;
783         int ret;
784
785         if (!h_vdec)
786                 return -EINVAL;
787
788         if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
789                 ret = vdec_h264_slice_single_decode(h_vdec, bs, unused, res_chg);
790         else
791                 ret = vdec_h264_slice_lat_decode(h_vdec, bs, unused, res_chg);
792
793         return ret;
794 }
795
796 static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
797                                      void *out)
798 {
799         struct vdec_h264_slice_inst *inst = h_vdec;
800
801         switch (type) {
802         case GET_PARAM_PIC_INFO:
803                 vdec_h264_slice_get_pic_info(inst);
804                 break;
805         case GET_PARAM_DPB_SIZE:
806                 *(unsigned int *)out = 6;
807                 break;
808         case GET_PARAM_CROP_INFO:
809                 vdec_h264_slice_get_crop_info(inst, out);
810                 break;
811         default:
812                 mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
813                 return -EINVAL;
814         }
815         return 0;
816 }
817
818 const struct vdec_common_if vdec_h264_slice_multi_if = {
819         .init           = vdec_h264_slice_init,
820         .decode         = vdec_h264_slice_decode,
821         .get_param      = vdec_h264_slice_get_param,
822         .deinit         = vdec_h264_slice_deinit,
823 };