10934d375SYunfei Dong // SPDX-License-Identifier: GPL-2.0
20934d375SYunfei Dong /*
30934d375SYunfei Dong  * Copyright (c) 2023 MediaTek Inc.
40934d375SYunfei Dong  * Author: Yunfei Dong <yunfei.dong@mediatek.com>
50934d375SYunfei Dong  */
60934d375SYunfei Dong 
70934d375SYunfei Dong #include <linux/module.h>
80934d375SYunfei Dong #include <linux/slab.h>
90934d375SYunfei Dong #include <media/videobuf2-dma-contig.h>
100934d375SYunfei Dong 
110934d375SYunfei Dong #include "../mtk_vcodec_dec.h"
120934d375SYunfei Dong #include "../../common/mtk_vcodec_intr.h"
130934d375SYunfei Dong #include "../vdec_drv_base.h"
140934d375SYunfei Dong #include "../vdec_drv_if.h"
150934d375SYunfei Dong #include "../vdec_vpu_if.h"
160934d375SYunfei Dong 
170934d375SYunfei Dong /* the size used to store hevc wrap information */
180934d375SYunfei Dong #define VDEC_HEVC_WRAP_SZ (532 * SZ_1K)
190934d375SYunfei Dong 
200934d375SYunfei Dong #define HEVC_MAX_MV_NUM 32
210934d375SYunfei Dong 
220934d375SYunfei Dong /* get used parameters for sps/pps */
230934d375SYunfei Dong #define GET_HEVC_VDEC_FLAG(cond, flag) \
240934d375SYunfei Dong 	{ dst_param->cond = ((src_param->flags & (flag)) ? (1) : (0)); }
250934d375SYunfei Dong #define GET_HEVC_VDEC_PARAM(param) \
260934d375SYunfei Dong 	{ dst_param->param = src_param->param; }
270934d375SYunfei Dong 
280934d375SYunfei Dong /**
290934d375SYunfei Dong  * enum vdec_hevc_core_dec_err_type  - core decode error type
300934d375SYunfei Dong  *
310934d375SYunfei Dong  * @TRANS_BUFFER_FULL: trans buffer is full
320934d375SYunfei Dong  * @SLICE_HEADER_FULL: slice header buffer is full
330934d375SYunfei Dong  */
340934d375SYunfei Dong enum vdec_hevc_core_dec_err_type {
350934d375SYunfei Dong 	TRANS_BUFFER_FULL = 1,
360934d375SYunfei Dong 	SLICE_HEADER_FULL,
370934d375SYunfei Dong };
380934d375SYunfei Dong 
390934d375SYunfei Dong /**
400934d375SYunfei Dong  * struct mtk_hevc_dpb_info  - hevc dpb information
410934d375SYunfei Dong  *
420934d375SYunfei Dong  * @y_dma_addr:     Y plane physical address
430934d375SYunfei Dong  * @c_dma_addr:     CbCr plane physical address
440934d375SYunfei Dong  * @reference_flag: reference picture flag (short/long term reference picture)
450934d375SYunfei Dong  * @field:          field picture flag
460934d375SYunfei Dong  */
470934d375SYunfei Dong struct mtk_hevc_dpb_info {
480934d375SYunfei Dong 	dma_addr_t y_dma_addr;
490934d375SYunfei Dong 	dma_addr_t c_dma_addr;
500934d375SYunfei Dong 	int reference_flag;
510934d375SYunfei Dong 	int field;
520934d375SYunfei Dong };
530934d375SYunfei Dong 
540934d375SYunfei Dong /*
550934d375SYunfei Dong  * struct mtk_hevc_sps_param  - parameters for sps
560934d375SYunfei Dong  */
570934d375SYunfei Dong struct mtk_hevc_sps_param {
580934d375SYunfei Dong 	unsigned char video_parameter_set_id;
590934d375SYunfei Dong 	unsigned char seq_parameter_set_id;
600934d375SYunfei Dong 	unsigned short pic_width_in_luma_samples;
610934d375SYunfei Dong 	unsigned short pic_height_in_luma_samples;
620934d375SYunfei Dong 	unsigned char bit_depth_luma_minus8;
630934d375SYunfei Dong 	unsigned char bit_depth_chroma_minus8;
640934d375SYunfei Dong 	unsigned char log2_max_pic_order_cnt_lsb_minus4;
650934d375SYunfei Dong 	unsigned char sps_max_dec_pic_buffering_minus1;
660934d375SYunfei Dong 	unsigned char sps_max_num_reorder_pics;
670934d375SYunfei Dong 	unsigned char sps_max_latency_increase_plus1;
680934d375SYunfei Dong 	unsigned char log2_min_luma_coding_block_size_minus3;
690934d375SYunfei Dong 	unsigned char log2_diff_max_min_luma_coding_block_size;
700934d375SYunfei Dong 	unsigned char log2_min_luma_transform_block_size_minus2;
710934d375SYunfei Dong 	unsigned char log2_diff_max_min_luma_transform_block_size;
720934d375SYunfei Dong 	unsigned char max_transform_hierarchy_depth_inter;
730934d375SYunfei Dong 	unsigned char max_transform_hierarchy_depth_intra;
740934d375SYunfei Dong 	unsigned char pcm_sample_bit_depth_luma_minus1;
750934d375SYunfei Dong 	unsigned char pcm_sample_bit_depth_chroma_minus1;
760934d375SYunfei Dong 	unsigned char log2_min_pcm_luma_coding_block_size_minus3;
770934d375SYunfei Dong 	unsigned char log2_diff_max_min_pcm_luma_coding_block_size;
780934d375SYunfei Dong 	unsigned char num_short_term_ref_pic_sets;
790934d375SYunfei Dong 	unsigned char num_long_term_ref_pics_sps;
800934d375SYunfei Dong 	unsigned char chroma_format_idc;
810934d375SYunfei Dong 	unsigned char sps_max_sub_layers_minus1;
820934d375SYunfei Dong 	unsigned char separate_colour_plane;
830934d375SYunfei Dong 	unsigned char scaling_list_enabled;
840934d375SYunfei Dong 	unsigned char amp_enabled;
850934d375SYunfei Dong 	unsigned char sample_adaptive_offset;
860934d375SYunfei Dong 	unsigned char pcm_enabled;
870934d375SYunfei Dong 	unsigned char pcm_loop_filter_disabled;
880934d375SYunfei Dong 	unsigned char long_term_ref_pics_enabled;
890934d375SYunfei Dong 	unsigned char sps_temporal_mvp_enabled;
900934d375SYunfei Dong 	unsigned char strong_intra_smoothing_enabled;
910934d375SYunfei Dong 	unsigned char reserved[5];
920934d375SYunfei Dong };
930934d375SYunfei Dong 
940934d375SYunfei Dong /*
950934d375SYunfei Dong  * struct mtk_hevc_pps_param  - parameters for pps
960934d375SYunfei Dong  */
970934d375SYunfei Dong struct mtk_hevc_pps_param {
980934d375SYunfei Dong 	unsigned char pic_parameter_set_id;
990934d375SYunfei Dong 	unsigned char num_extra_slice_header_bits;
1000934d375SYunfei Dong 	unsigned char num_ref_idx_l0_default_active_minus1;
1010934d375SYunfei Dong 	unsigned char num_ref_idx_l1_default_active_minus1;
1020934d375SYunfei Dong 	char init_qp_minus26;
1030934d375SYunfei Dong 	unsigned char diff_cu_qp_delta_depth;
1040934d375SYunfei Dong 	char pps_cb_qp_offset;
1050934d375SYunfei Dong 	char pps_cr_qp_offset;
1060934d375SYunfei Dong 	unsigned char num_tile_columns_minus1;
1070934d375SYunfei Dong 	unsigned char num_tile_rows_minus1;
1080934d375SYunfei Dong 	unsigned char column_width_minus1[20];
1090934d375SYunfei Dong 	unsigned char row_height_minus1[22];
1100934d375SYunfei Dong 	char pps_beta_offset_div2;
1110934d375SYunfei Dong 	char pps_tc_offset_div2;
1120934d375SYunfei Dong 	unsigned char log2_parallel_merge_level_minus2;
1130934d375SYunfei Dong 	char dependent_slice_segment_enabled;
1140934d375SYunfei Dong 	char output_flag_present;
1150934d375SYunfei Dong 	char sign_data_hiding_enabled;
1160934d375SYunfei Dong 	char cabac_init_present;
1170934d375SYunfei Dong 	char constrained_intra_pred;
1180934d375SYunfei Dong 	char transform_skip_enabled;
1190934d375SYunfei Dong 	char cu_qp_delta_enabled;
1200934d375SYunfei Dong 	char pps_slice_chroma_qp_offsets_present;
1210934d375SYunfei Dong 	char weighted_pred;
1220934d375SYunfei Dong 	char weighted_bipred;
1230934d375SYunfei Dong 	char transquant_bypass_enabled;
1240934d375SYunfei Dong 	char pps_flag_tiles_enabled;
1250934d375SYunfei Dong 	char entropy_coding_sync_enabled;
1260934d375SYunfei Dong 	char loop_filter_across_tiles_enabled;
1270934d375SYunfei Dong 	char pps_loop_filter_across_slices_enabled;
1280934d375SYunfei Dong 	char deblocking_filter_override_enabled;
1290934d375SYunfei Dong 	char pps_disable_deflocking_filter;
1300934d375SYunfei Dong 	char lists_modification_present;
1310934d375SYunfei Dong 	char slice_segment_header_extersion_present;
1320934d375SYunfei Dong 	char deblocking_filter_control_present;
1330934d375SYunfei Dong 	char uniform_spacing;
1340934d375SYunfei Dong 	char reserved[6];
1350934d375SYunfei Dong };
1360934d375SYunfei Dong 
1370934d375SYunfei Dong /*
1380934d375SYunfei Dong  * struct mtk_hevc_slice_header_param  - parameters for slice header
1390934d375SYunfei Dong  */
1400934d375SYunfei Dong struct mtk_hevc_slice_header_param {
1410934d375SYunfei Dong 	unsigned int	slice_type;
1420934d375SYunfei Dong 	unsigned int	num_active_ref_layer_pics;
1430934d375SYunfei Dong 	int		slice_qp;
1440934d375SYunfei Dong 	int		slice_qp_delta_cb;
1450934d375SYunfei Dong 	int		slice_qp_delta_cr;
1460934d375SYunfei Dong 	int		num_ref_idx[3];
1470934d375SYunfei Dong 	unsigned int	col_ref_idx;
1480934d375SYunfei Dong 	unsigned int	five_minus_max_num_merge_cand;
1490934d375SYunfei Dong 	int		slice_deblocking_filter_beta_offset_div2;
1500934d375SYunfei Dong 	int		slice_deblocking_filter_tc_offset_div2;
1510934d375SYunfei Dong 	unsigned char	sao_enable_flag;
1520934d375SYunfei Dong 	unsigned char	sao_enable_flag_chroma;
1530934d375SYunfei Dong 	unsigned char	cabac_init_flag;
1540934d375SYunfei Dong 	unsigned char	slice_tmvp_flags_present;
1550934d375SYunfei Dong 	unsigned char	col_from_l0_flag;
1560934d375SYunfei Dong 	unsigned char	mvd_l1_zero_flag;
1570934d375SYunfei Dong 	unsigned char	slice_loop_filter_across_slices_enabled_flag;
1580934d375SYunfei Dong 	unsigned char	deblocking_filter_disable_flag;
1590934d375SYunfei Dong 	unsigned int	slice_reg0;
1600934d375SYunfei Dong 	unsigned int	slice_reg1;
1610934d375SYunfei Dong 	unsigned int	slice_reg2;
1620934d375SYunfei Dong 	unsigned int	num_rps_curr_temp_list;
1630934d375SYunfei Dong 	unsigned int	ref_list_mode;
1640934d375SYunfei Dong 	int		str_num_delta_pocs;
1650934d375SYunfei Dong 	int		str_num_negtive_pos_pics;
1660934d375SYunfei Dong 	int		num_long_term;
1670934d375SYunfei Dong 	int		num_long_term_sps;
1680934d375SYunfei Dong 	unsigned int	max_cu_width;
1690934d375SYunfei Dong 	unsigned int	max_cu_height;
1700934d375SYunfei Dong 	unsigned int	num_entry_point_offsets;
1710934d375SYunfei Dong 	unsigned int    last_lcu_x_in_tile[17];
1720934d375SYunfei Dong 	unsigned int    last_lcu_y_in_tile[17];
1730934d375SYunfei Dong 	unsigned char   nal_unit_type;
1740934d375SYunfei Dong };
1750934d375SYunfei Dong 
1760934d375SYunfei Dong /*
1770934d375SYunfei Dong  * struct slice_api_hevc_scaling_matrix  - parameters for scaling list
1780934d375SYunfei Dong  */
1790934d375SYunfei Dong struct slice_api_hevc_scaling_matrix {
1800934d375SYunfei Dong 	unsigned char scaling_list_4x4[6][16];
1810934d375SYunfei Dong 	unsigned char scaling_list_8x8[6][64];
1820934d375SYunfei Dong 	unsigned char scaling_list_16x16[6][64];
1830934d375SYunfei Dong 	unsigned char scaling_list_32x32[2][64];
1840934d375SYunfei Dong 	unsigned char scaling_list_dc_coef_16x16[6];
1850934d375SYunfei Dong 	unsigned char scaling_list_dc_coef_32x32[2];
1860934d375SYunfei Dong };
1870934d375SYunfei Dong 
1880934d375SYunfei Dong /*
1890934d375SYunfei Dong  * struct slice_hevc_dpb_entry  - each dpb information
1900934d375SYunfei Dong  */
1910934d375SYunfei Dong struct slice_hevc_dpb_entry {
1920934d375SYunfei Dong 	u64 timestamp;
1930934d375SYunfei Dong 	unsigned char flags;
1940934d375SYunfei Dong 	unsigned char field_pic;
1950934d375SYunfei Dong 	int pic_order_cnt_val;
1960934d375SYunfei Dong };
1970934d375SYunfei Dong 
1980934d375SYunfei Dong /*
1990934d375SYunfei Dong  * struct slice_api_hevc_decode_param - parameters for decode.
2000934d375SYunfei Dong  */
2010934d375SYunfei Dong struct slice_api_hevc_decode_param {
2020934d375SYunfei Dong 	struct slice_hevc_dpb_entry dpb[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
2030934d375SYunfei Dong 	int pic_order_cnt_val;
2040934d375SYunfei Dong 	unsigned short short_term_ref_pic_set_size;
2050934d375SYunfei Dong 	unsigned short long_term_ref_pic_set_size;
2060934d375SYunfei Dong 	unsigned char num_active_dpb_entries;
2070934d375SYunfei Dong 	unsigned char num_poc_st_curr_before;
2080934d375SYunfei Dong 	unsigned char num_poc_st_curr_after;
2090934d375SYunfei Dong 	unsigned char num_poc_lt_curr;
2100934d375SYunfei Dong 	unsigned char poc_st_curr_before[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
2110934d375SYunfei Dong 	unsigned char poc_st_curr_after[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
2120934d375SYunfei Dong 	unsigned char poc_lt_curr[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
2130934d375SYunfei Dong 	unsigned char num_delta_pocs_of_ref_rps_idx;
2140934d375SYunfei Dong 	int flags;
2150934d375SYunfei Dong };
2160934d375SYunfei Dong 
2170934d375SYunfei Dong /**
2180934d375SYunfei Dong  * struct hevc_fb - hevc decode frame buffer information
2190934d375SYunfei Dong  *
2200934d375SYunfei Dong  * @vdec_fb_va: virtual address of struct vdec_fb
2210934d375SYunfei Dong  * @y_fb_dma:   dma address of Y frame buffer (luma)
2220934d375SYunfei Dong  * @c_fb_dma:   dma address of C frame buffer (chroma)
2230934d375SYunfei Dong  * @poc:        picture order count of frame buffer
2240934d375SYunfei Dong  * @reserved:   for 8 bytes alignment
2250934d375SYunfei Dong  */
2260934d375SYunfei Dong struct hevc_fb {
2270934d375SYunfei Dong 	u64 vdec_fb_va;
2280934d375SYunfei Dong 	u64 y_fb_dma;
2290934d375SYunfei Dong 	u64 c_fb_dma;
2300934d375SYunfei Dong 	s32 poc;
2310934d375SYunfei Dong 	u32 reserved;
2320934d375SYunfei Dong };
2330934d375SYunfei Dong 
2340934d375SYunfei Dong /**
2350934d375SYunfei Dong  * struct vdec_hevc_slice_lat_dec_param  - parameters for decode current frame
2360934d375SYunfei Dong  *
2370934d375SYunfei Dong  * @sps:            hevc sps syntax parameters
2380934d375SYunfei Dong  * @pps:            hevc pps syntax parameters
2390934d375SYunfei Dong  * @slice_header:   hevc slice header syntax parameters
2400934d375SYunfei Dong  * @scaling_matrix: hevc scaling list parameters
2410934d375SYunfei Dong  * @decode_params:  decoder parameters of each frame used for hardware decode
2420934d375SYunfei Dong  * @hevc_dpb_info:  dpb reference list
2430934d375SYunfei Dong  */
2440934d375SYunfei Dong struct vdec_hevc_slice_lat_dec_param {
2450934d375SYunfei Dong 	struct mtk_hevc_sps_param sps;
2460934d375SYunfei Dong 	struct mtk_hevc_pps_param pps;
2470934d375SYunfei Dong 	struct mtk_hevc_slice_header_param slice_header;
2480934d375SYunfei Dong 	struct slice_api_hevc_scaling_matrix scaling_matrix;
2490934d375SYunfei Dong 	struct slice_api_hevc_decode_param decode_params;
2500934d375SYunfei Dong 	struct mtk_hevc_dpb_info hevc_dpb_info[V4L2_HEVC_DPB_ENTRIES_NUM_MAX];
2510934d375SYunfei Dong };
2520934d375SYunfei Dong 
2530934d375SYunfei Dong /**
2540934d375SYunfei Dong  * struct vdec_hevc_slice_info - decode information
2550934d375SYunfei Dong  *
2560934d375SYunfei Dong  * @wdma_end_addr_offset: wdma end address offset
2570934d375SYunfei Dong  * @timeout:              Decode timeout: 1 timeout, 0 no timeount
2580934d375SYunfei Dong  * @vdec_fb_va:           VDEC frame buffer struct virtual address
2590934d375SYunfei Dong  * @crc:                  Used to check whether hardware's status is right
2600934d375SYunfei Dong  */
2610934d375SYunfei Dong struct vdec_hevc_slice_info {
2620934d375SYunfei Dong 	u64 wdma_end_addr_offset;
2630934d375SYunfei Dong 	u64 timeout;
2640934d375SYunfei Dong 	u64 vdec_fb_va;
2650934d375SYunfei Dong 	u32 crc[8];
2660934d375SYunfei Dong };
2670934d375SYunfei Dong 
2680934d375SYunfei Dong /*
2690934d375SYunfei Dong  * struct vdec_hevc_slice_mem - memory address and size
2700934d375SYunfei Dong  */
2710934d375SYunfei Dong struct vdec_hevc_slice_mem {
2720934d375SYunfei Dong 	union {
2730934d375SYunfei Dong 		u64 buf;
2740934d375SYunfei Dong 		dma_addr_t dma_addr;
2750934d375SYunfei Dong 	};
2760934d375SYunfei Dong 	union {
2770934d375SYunfei Dong 		size_t size;
2780934d375SYunfei Dong 		dma_addr_t dma_addr_end;
2790934d375SYunfei Dong 		u64 padding;
2800934d375SYunfei Dong 	};
2810934d375SYunfei Dong };
2820934d375SYunfei Dong 
2830934d375SYunfei Dong /**
2840934d375SYunfei Dong  * struct vdec_hevc_slice_fb - frame buffer for decoding
2850934d375SYunfei Dong  * @y:  current y buffer address info
2860934d375SYunfei Dong  * @c:  current c buffer address info
2870934d375SYunfei Dong  */
2880934d375SYunfei Dong struct vdec_hevc_slice_fb {
2890934d375SYunfei Dong 	struct vdec_hevc_slice_mem y;
2900934d375SYunfei Dong 	struct vdec_hevc_slice_mem c;
2910934d375SYunfei Dong };
2920934d375SYunfei Dong 
2930934d375SYunfei Dong /**
2940934d375SYunfei Dong  * struct vdec_hevc_slice_vsi - shared memory for decode information exchange
2950934d375SYunfei Dong  *        between SCP and Host.
2960934d375SYunfei Dong  *
2970934d375SYunfei Dong  * @bs:                input buffer info
2980934d375SYunfei Dong  *
2990934d375SYunfei Dong  * @ube:               ube buffer
3000934d375SYunfei Dong  * @trans:             transcoded buffer
3010934d375SYunfei Dong  * @err_map:           err map buffer
3020934d375SYunfei Dong  * @slice_bc:          slice bc buffer
3030934d375SYunfei Dong  * @wrap:              temp buffer
3040934d375SYunfei Dong  *
3050934d375SYunfei Dong  * @fb:                current y/c buffer
3060934d375SYunfei Dong  * @mv_buf_dma:        HW working motion vector buffer
3070934d375SYunfei Dong  * @dec:               decode information (AP-R, VPU-W)
3080934d375SYunfei Dong  * @hevc_slice_params: decode parameters for hw used
3090934d375SYunfei Dong  */
3100934d375SYunfei Dong struct vdec_hevc_slice_vsi {
3110934d375SYunfei Dong 	/* used in LAT stage */
3120934d375SYunfei Dong 	struct vdec_hevc_slice_mem bs;
3130934d375SYunfei Dong 
3140934d375SYunfei Dong 	struct vdec_hevc_slice_mem ube;
3150934d375SYunfei Dong 	struct vdec_hevc_slice_mem trans;
3160934d375SYunfei Dong 	struct vdec_hevc_slice_mem err_map;
3170934d375SYunfei Dong 	struct vdec_hevc_slice_mem slice_bc;
3180934d375SYunfei Dong 	struct vdec_hevc_slice_mem wrap;
3190934d375SYunfei Dong 
3200934d375SYunfei Dong 	struct vdec_hevc_slice_fb fb;
3210934d375SYunfei Dong 	struct vdec_hevc_slice_mem mv_buf_dma[HEVC_MAX_MV_NUM];
3220934d375SYunfei Dong 	struct vdec_hevc_slice_info dec;
3230934d375SYunfei Dong 	struct vdec_hevc_slice_lat_dec_param hevc_slice_params;
3240934d375SYunfei Dong };
3250934d375SYunfei Dong 
3260934d375SYunfei Dong /**
3270934d375SYunfei Dong  * struct vdec_hevc_slice_share_info - shared information used to exchange
3280934d375SYunfei Dong  *                                     message between lat and core
3290934d375SYunfei Dong  *
3300934d375SYunfei Dong  * @sps:               sequence header information from user space
3310934d375SYunfei Dong  * @dec_params:        decoder params from user space
3320934d375SYunfei Dong  * @hevc_slice_params: decoder params used for hardware
3330934d375SYunfei Dong  * @trans:             trans buffer dma address
3340934d375SYunfei Dong  */
3350934d375SYunfei Dong struct vdec_hevc_slice_share_info {
3360934d375SYunfei Dong 	struct v4l2_ctrl_hevc_sps sps;
3370934d375SYunfei Dong 	struct v4l2_ctrl_hevc_decode_params dec_params;
3380934d375SYunfei Dong 	struct vdec_hevc_slice_lat_dec_param hevc_slice_params;
3390934d375SYunfei Dong 	struct vdec_hevc_slice_mem trans;
3400934d375SYunfei Dong };
3410934d375SYunfei Dong 
3420934d375SYunfei Dong /**
3430934d375SYunfei Dong  * struct vdec_hevc_slice_inst - hevc decoder instance
3440934d375SYunfei Dong  *
3450934d375SYunfei Dong  * @slice_dec_num:      how many picture be decoded
3460934d375SYunfei Dong  * @ctx:                point to mtk_vcodec_dec_ctx
3470934d375SYunfei Dong  * @mv_buf:             HW working motion vector buffer
3480934d375SYunfei Dong  * @vpu:                VPU instance
3490934d375SYunfei Dong  * @vsi:                vsi used for lat
3500934d375SYunfei Dong  * @vsi_core:           vsi used for core
3510934d375SYunfei Dong  * @wrap_addr:          wrap address used for hevc
3520934d375SYunfei Dong  *
3530934d375SYunfei Dong  * @hevc_slice_param:   the parameters that hardware use to decode
3540934d375SYunfei Dong  *
3550934d375SYunfei Dong  * @resolution_changed: resolution changed
3560934d375SYunfei Dong  * @realloc_mv_buf:     reallocate mv buffer
3570934d375SYunfei Dong  * @cap_num_planes:     number of capture queue plane
3580934d375SYunfei Dong  */
3590934d375SYunfei Dong struct vdec_hevc_slice_inst {
3600934d375SYunfei Dong 	unsigned int slice_dec_num;
3610934d375SYunfei Dong 	struct mtk_vcodec_dec_ctx *ctx;
3620934d375SYunfei Dong 	struct mtk_vcodec_mem mv_buf[HEVC_MAX_MV_NUM];
3630934d375SYunfei Dong 	struct vdec_vpu_inst vpu;
3640934d375SYunfei Dong 	struct vdec_hevc_slice_vsi *vsi;
3650934d375SYunfei Dong 	struct vdec_hevc_slice_vsi *vsi_core;
3660934d375SYunfei Dong 	struct mtk_vcodec_mem wrap_addr;
3670934d375SYunfei Dong 
3680934d375SYunfei Dong 	struct vdec_hevc_slice_lat_dec_param hevc_slice_param;
3690934d375SYunfei Dong 
3700934d375SYunfei Dong 	unsigned int resolution_changed;
3710934d375SYunfei Dong 	unsigned int realloc_mv_buf;
3720934d375SYunfei Dong 	unsigned int cap_num_planes;
3730934d375SYunfei Dong };
3740934d375SYunfei Dong 
vdec_hevc_get_mv_buf_size(unsigned int width,unsigned int height)3750934d375SYunfei Dong static unsigned int vdec_hevc_get_mv_buf_size(unsigned int width, unsigned int height)
3760934d375SYunfei Dong {
3770934d375SYunfei Dong 	const unsigned int unit_size = (width / 16) * (height / 16) + 8;
3780934d375SYunfei Dong 
3790934d375SYunfei Dong 	return 64 * unit_size;
3800934d375SYunfei Dong }
3810934d375SYunfei Dong 
vdec_hevc_get_ctrl_ptr(struct mtk_vcodec_dec_ctx * ctx,int id)3820934d375SYunfei Dong static void *vdec_hevc_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id)
3830934d375SYunfei Dong {
3840934d375SYunfei Dong 	struct v4l2_ctrl *ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, id);
3850934d375SYunfei Dong 
3860934d375SYunfei Dong 	if (!ctrl)
3870934d375SYunfei Dong 		return ERR_PTR(-EINVAL);
3880934d375SYunfei Dong 
3890934d375SYunfei Dong 	return ctrl->p_cur.p;
3900934d375SYunfei Dong }
3910934d375SYunfei Dong 
vdec_hevc_fill_dpb_info(struct mtk_vcodec_dec_ctx * ctx,struct slice_api_hevc_decode_param * decode_params,struct mtk_hevc_dpb_info * hevc_dpb_info)3920934d375SYunfei Dong static void vdec_hevc_fill_dpb_info(struct mtk_vcodec_dec_ctx *ctx,
3930934d375SYunfei Dong 				    struct slice_api_hevc_decode_param *decode_params,
3940934d375SYunfei Dong 				    struct mtk_hevc_dpb_info *hevc_dpb_info)
3950934d375SYunfei Dong {
3960934d375SYunfei Dong 	const struct slice_hevc_dpb_entry *dpb;
3970934d375SYunfei Dong 	struct vb2_queue *vq;
3980934d375SYunfei Dong 	struct vb2_buffer *vb;
3990934d375SYunfei Dong 	int index;
4000934d375SYunfei Dong 
4010934d375SYunfei Dong 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
4020934d375SYunfei Dong 	for (index = 0; index < V4L2_HEVC_DPB_ENTRIES_NUM_MAX; index++) {
4030934d375SYunfei Dong 		dpb = &decode_params->dpb[index];
4040934d375SYunfei Dong 		if (index >= decode_params->num_active_dpb_entries)
4050934d375SYunfei Dong 			continue;
4060934d375SYunfei Dong 
4070934d375SYunfei Dong 		vb = vb2_find_buffer(vq, dpb->timestamp);
4080934d375SYunfei Dong 		if (!vb) {
4090934d375SYunfei Dong 			dev_err(&ctx->dev->plat_dev->dev,
4100934d375SYunfei Dong 				"Reference invalid: dpb_index(%d) timestamp(%lld)",
4110934d375SYunfei Dong 				index, dpb->timestamp);
4120934d375SYunfei Dong 			continue;
4130934d375SYunfei Dong 		}
4140934d375SYunfei Dong 
4150934d375SYunfei Dong 		hevc_dpb_info[index].field = dpb->field_pic;
4160934d375SYunfei Dong 
4170934d375SYunfei Dong 		hevc_dpb_info[index].y_dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
4180934d375SYunfei Dong 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
4190934d375SYunfei Dong 			hevc_dpb_info[index].c_dma_addr = vb2_dma_contig_plane_dma_addr(vb, 1);
4200934d375SYunfei Dong 		else
4210934d375SYunfei Dong 			hevc_dpb_info[index].c_dma_addr =
4220934d375SYunfei Dong 				hevc_dpb_info[index].y_dma_addr + ctx->picinfo.fb_sz[0];
4230934d375SYunfei Dong 	}
4240934d375SYunfei Dong }
4250934d375SYunfei Dong 
vdec_hevc_copy_sps_params(struct mtk_hevc_sps_param * dst_param,const struct v4l2_ctrl_hevc_sps * src_param)4260934d375SYunfei Dong static void vdec_hevc_copy_sps_params(struct mtk_hevc_sps_param *dst_param,
4270934d375SYunfei Dong 				      const struct v4l2_ctrl_hevc_sps *src_param)
4280934d375SYunfei Dong {
4290934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(video_parameter_set_id);
4300934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(seq_parameter_set_id);
4310934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pic_width_in_luma_samples);
4320934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pic_height_in_luma_samples);
4330934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(bit_depth_luma_minus8);
4340934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(bit_depth_chroma_minus8);
4350934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_max_pic_order_cnt_lsb_minus4);
4360934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(sps_max_dec_pic_buffering_minus1);
4370934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(sps_max_num_reorder_pics);
4380934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(sps_max_latency_increase_plus1);
4390934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_min_luma_coding_block_size_minus3);
4400934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_diff_max_min_luma_coding_block_size);
4410934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_min_luma_transform_block_size_minus2);
4420934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_diff_max_min_luma_transform_block_size);
4430934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(max_transform_hierarchy_depth_inter);
4440934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(max_transform_hierarchy_depth_intra);
4450934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pcm_sample_bit_depth_luma_minus1);
4460934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pcm_sample_bit_depth_chroma_minus1);
4470934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_min_pcm_luma_coding_block_size_minus3);
4480934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_diff_max_min_pcm_luma_coding_block_size);
4490934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_short_term_ref_pic_sets);
4500934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_long_term_ref_pics_sps);
4510934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(chroma_format_idc);
4520934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(sps_max_sub_layers_minus1);
4530934d375SYunfei Dong 
4540934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(separate_colour_plane,
4550934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_SEPARATE_COLOUR_PLANE);
4560934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(scaling_list_enabled,
4570934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED);
4580934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(amp_enabled,
4590934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_AMP_ENABLED);
4600934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(sample_adaptive_offset,
4610934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_SAMPLE_ADAPTIVE_OFFSET);
4620934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pcm_enabled,
4630934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_PCM_ENABLED);
4640934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pcm_loop_filter_disabled,
4650934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_PCM_LOOP_FILTER_DISABLED);
4660934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(long_term_ref_pics_enabled,
4670934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT);
4680934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(sps_temporal_mvp_enabled,
4690934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED);
4700934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(strong_intra_smoothing_enabled,
4710934d375SYunfei Dong 			   V4L2_HEVC_SPS_FLAG_STRONG_INTRA_SMOOTHING_ENABLED);
4720934d375SYunfei Dong }
4730934d375SYunfei Dong 
vdec_hevc_copy_pps_params(struct mtk_hevc_pps_param * dst_param,const struct v4l2_ctrl_hevc_pps * src_param)4740934d375SYunfei Dong static void vdec_hevc_copy_pps_params(struct mtk_hevc_pps_param *dst_param,
4750934d375SYunfei Dong 				      const struct v4l2_ctrl_hevc_pps *src_param)
4760934d375SYunfei Dong {
4770934d375SYunfei Dong 	int i;
4780934d375SYunfei Dong 
4790934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pic_parameter_set_id);
4800934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_extra_slice_header_bits);
4810934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_ref_idx_l0_default_active_minus1);
4820934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_ref_idx_l1_default_active_minus1);
4830934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(init_qp_minus26);
4840934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(diff_cu_qp_delta_depth);
4850934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pps_cb_qp_offset);
4860934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pps_cr_qp_offset);
4870934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_tile_columns_minus1);
4880934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_tile_rows_minus1);
4890934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(init_qp_minus26);
4900934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(diff_cu_qp_delta_depth);
4910934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pic_parameter_set_id);
4920934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_extra_slice_header_bits);
4930934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_ref_idx_l0_default_active_minus1);
4940934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_ref_idx_l1_default_active_minus1);
4950934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pps_beta_offset_div2);
4960934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pps_tc_offset_div2);
4970934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(log2_parallel_merge_level_minus2);
4980934d375SYunfei Dong 
4990934d375SYunfei Dong 	for (i = 0; i < ARRAY_SIZE(src_param->column_width_minus1); i++)
5000934d375SYunfei Dong 		GET_HEVC_VDEC_PARAM(column_width_minus1[i]);
5010934d375SYunfei Dong 	for (i = 0; i < ARRAY_SIZE(src_param->row_height_minus1); i++)
5020934d375SYunfei Dong 		GET_HEVC_VDEC_PARAM(row_height_minus1[i]);
5030934d375SYunfei Dong 
5040934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(dependent_slice_segment_enabled,
5050934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_DEPENDENT_SLICE_SEGMENT_ENABLED);
5060934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(output_flag_present,
5070934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_OUTPUT_FLAG_PRESENT);
5080934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(sign_data_hiding_enabled,
5090934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_SIGN_DATA_HIDING_ENABLED);
5100934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(cabac_init_present,
5110934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_CABAC_INIT_PRESENT);
5120934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(constrained_intra_pred,
5130934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_CONSTRAINED_INTRA_PRED);
5140934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(transform_skip_enabled,
5150934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_TRANSFORM_SKIP_ENABLED);
5160934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(cu_qp_delta_enabled,
5170934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED);
5180934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pps_slice_chroma_qp_offsets_present,
5190934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_PPS_SLICE_CHROMA_QP_OFFSETS_PRESENT);
5200934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(weighted_pred,
5210934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED);
5220934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(weighted_bipred,
5230934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_WEIGHTED_BIPRED);
5240934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(transquant_bypass_enabled,
5250934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_TRANSQUANT_BYPASS_ENABLED);
5260934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pps_flag_tiles_enabled,
5270934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_TILES_ENABLED);
5280934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(entropy_coding_sync_enabled,
5290934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED);
5300934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(loop_filter_across_tiles_enabled,
5310934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED);
5320934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pps_loop_filter_across_slices_enabled,
5330934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_PPS_LOOP_FILTER_ACROSS_SLICES_ENABLED);
5340934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(deblocking_filter_override_enabled,
5350934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_OVERRIDE_ENABLED);
5360934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(pps_disable_deflocking_filter,
5370934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER);
5380934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(lists_modification_present,
5390934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_LISTS_MODIFICATION_PRESENT);
5400934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(slice_segment_header_extersion_present,
5410934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_SLICE_SEGMENT_HEADER_EXTENSION_PRESENT);
5420934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(deblocking_filter_control_present,
5430934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT);
5440934d375SYunfei Dong 	GET_HEVC_VDEC_FLAG(uniform_spacing,
5450934d375SYunfei Dong 			   V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING);
5460934d375SYunfei Dong }
5470934d375SYunfei Dong 
vdec_hevc_copy_scaling_matrix(struct slice_api_hevc_scaling_matrix * dst_matrix,const struct v4l2_ctrl_hevc_scaling_matrix * src_matrix)5480934d375SYunfei Dong static void vdec_hevc_copy_scaling_matrix(struct slice_api_hevc_scaling_matrix *dst_matrix,
5490934d375SYunfei Dong 					  const struct v4l2_ctrl_hevc_scaling_matrix *src_matrix)
5500934d375SYunfei Dong {
5510934d375SYunfei Dong 	memcpy(dst_matrix, src_matrix, sizeof(*src_matrix));
5520934d375SYunfei Dong }
5530934d375SYunfei Dong 
5540934d375SYunfei Dong static void
vdec_hevc_copy_decode_params(struct slice_api_hevc_decode_param * dst_param,const struct v4l2_ctrl_hevc_decode_params * src_param,const struct v4l2_hevc_dpb_entry dpb[V4L2_HEVC_DPB_ENTRIES_NUM_MAX])5550934d375SYunfei Dong vdec_hevc_copy_decode_params(struct slice_api_hevc_decode_param *dst_param,
5560934d375SYunfei Dong 			     const struct v4l2_ctrl_hevc_decode_params *src_param,
5570934d375SYunfei Dong 			     const struct v4l2_hevc_dpb_entry dpb[V4L2_HEVC_DPB_ENTRIES_NUM_MAX])
5580934d375SYunfei Dong {
5590934d375SYunfei Dong 	struct slice_hevc_dpb_entry *dst_entry;
5600934d375SYunfei Dong 	const struct v4l2_hevc_dpb_entry *src_entry;
5610934d375SYunfei Dong 	int i;
5620934d375SYunfei Dong 
5630934d375SYunfei Dong 	for (i = 0; i < ARRAY_SIZE(dst_param->dpb); i++) {
5640934d375SYunfei Dong 		dst_entry = &dst_param->dpb[i];
5650934d375SYunfei Dong 		src_entry = &dpb[i];
5660934d375SYunfei Dong 
5670934d375SYunfei Dong 		dst_entry->timestamp = src_entry->timestamp;
5680934d375SYunfei Dong 		dst_entry->flags = src_entry->flags;
5690934d375SYunfei Dong 		dst_entry->field_pic = src_entry->field_pic;
5700934d375SYunfei Dong 		dst_entry->pic_order_cnt_val = src_entry->pic_order_cnt_val;
5710934d375SYunfei Dong 
5720934d375SYunfei Dong 		GET_HEVC_VDEC_PARAM(poc_st_curr_before[i]);
5730934d375SYunfei Dong 		GET_HEVC_VDEC_PARAM(poc_st_curr_after[i]);
5740934d375SYunfei Dong 		GET_HEVC_VDEC_PARAM(poc_lt_curr[i]);
5750934d375SYunfei Dong 	}
5760934d375SYunfei Dong 
5770934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(pic_order_cnt_val);
5780934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(short_term_ref_pic_set_size);
5790934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(long_term_ref_pic_set_size);
5800934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_active_dpb_entries);
5810934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_poc_st_curr_before);
5820934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_poc_st_curr_after);
5830934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_delta_pocs_of_ref_rps_idx);
5840934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(num_poc_lt_curr);
5850934d375SYunfei Dong 	GET_HEVC_VDEC_PARAM(flags);
5860934d375SYunfei Dong }
5870934d375SYunfei Dong 
vdec_hevc_slice_fill_decode_parameters(struct vdec_hevc_slice_inst * inst,struct vdec_hevc_slice_share_info * share_info)5880934d375SYunfei Dong static int vdec_hevc_slice_fill_decode_parameters(struct vdec_hevc_slice_inst *inst,
5890934d375SYunfei Dong 						  struct vdec_hevc_slice_share_info *share_info)
5900934d375SYunfei Dong {
5910934d375SYunfei Dong 	struct vdec_hevc_slice_lat_dec_param *slice_param = &inst->vsi->hevc_slice_params;
5920934d375SYunfei Dong 	const struct v4l2_ctrl_hevc_decode_params *dec_params;
5930934d375SYunfei Dong 	const struct v4l2_ctrl_hevc_scaling_matrix *src_matrix;
5940934d375SYunfei Dong 	const struct v4l2_ctrl_hevc_sps *sps;
5950934d375SYunfei Dong 	const struct v4l2_ctrl_hevc_pps *pps;
5960934d375SYunfei Dong 
5970934d375SYunfei Dong 	dec_params =
5980934d375SYunfei Dong 		vdec_hevc_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_HEVC_DECODE_PARAMS);
5990934d375SYunfei Dong 	if (IS_ERR(dec_params))
6000934d375SYunfei Dong 		return PTR_ERR(dec_params);
6010934d375SYunfei Dong 
6020934d375SYunfei Dong 	src_matrix =
6030934d375SYunfei Dong 		vdec_hevc_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_HEVC_SCALING_MATRIX);
6040934d375SYunfei Dong 	if (IS_ERR(src_matrix))
6050934d375SYunfei Dong 		return PTR_ERR(src_matrix);
6060934d375SYunfei Dong 
6070934d375SYunfei Dong 	sps = vdec_hevc_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_HEVC_SPS);
6080934d375SYunfei Dong 	if (IS_ERR(sps))
6090934d375SYunfei Dong 		return PTR_ERR(sps);
6100934d375SYunfei Dong 
6110934d375SYunfei Dong 	pps = vdec_hevc_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_HEVC_PPS);
6120934d375SYunfei Dong 	if (IS_ERR(pps))
6130934d375SYunfei Dong 		return PTR_ERR(pps);
6140934d375SYunfei Dong 
6150934d375SYunfei Dong 	vdec_hevc_copy_sps_params(&slice_param->sps, sps);
6160934d375SYunfei Dong 	vdec_hevc_copy_pps_params(&slice_param->pps, pps);
6170934d375SYunfei Dong 	vdec_hevc_copy_scaling_matrix(&slice_param->scaling_matrix, src_matrix);
6180934d375SYunfei Dong 
6190934d375SYunfei Dong 	memcpy(&share_info->sps, sps, sizeof(*sps));
6200934d375SYunfei Dong 	memcpy(&share_info->dec_params, dec_params, sizeof(*dec_params));
6210934d375SYunfei Dong 
6220934d375SYunfei Dong 	slice_param->decode_params.num_poc_st_curr_before = dec_params->num_poc_st_curr_before;
6230934d375SYunfei Dong 	slice_param->decode_params.num_poc_st_curr_after = dec_params->num_poc_st_curr_after;
6240934d375SYunfei Dong 	slice_param->decode_params.num_poc_lt_curr = dec_params->num_poc_lt_curr;
6250934d375SYunfei Dong 	slice_param->decode_params.num_delta_pocs_of_ref_rps_idx =
6260934d375SYunfei Dong 		dec_params->num_delta_pocs_of_ref_rps_idx;
6270934d375SYunfei Dong 
6280934d375SYunfei Dong 	return 0;
6290934d375SYunfei Dong }
6300934d375SYunfei Dong 
vdec_hevc_slice_fill_decode_reflist(struct vdec_hevc_slice_inst * inst,struct vdec_hevc_slice_lat_dec_param * slice_param,struct vdec_hevc_slice_share_info * share_info)6310934d375SYunfei Dong static void vdec_hevc_slice_fill_decode_reflist(struct vdec_hevc_slice_inst *inst,
6320934d375SYunfei Dong 						struct vdec_hevc_slice_lat_dec_param *slice_param,
6330934d375SYunfei Dong 						struct vdec_hevc_slice_share_info *share_info)
6340934d375SYunfei Dong {
6350934d375SYunfei Dong 	struct v4l2_ctrl_hevc_decode_params *dec_params = &share_info->dec_params;
6360934d375SYunfei Dong 
6370934d375SYunfei Dong 	vdec_hevc_copy_decode_params(&slice_param->decode_params, dec_params,
6380934d375SYunfei Dong 				     share_info->dec_params.dpb);
6390934d375SYunfei Dong 
6400934d375SYunfei Dong 	vdec_hevc_fill_dpb_info(inst->ctx, &slice_param->decode_params,
6410934d375SYunfei Dong 				slice_param->hevc_dpb_info);
6420934d375SYunfei Dong }
6430934d375SYunfei Dong 
vdec_hevc_slice_alloc_mv_buf(struct vdec_hevc_slice_inst * inst,struct vdec_pic_info * pic)6440934d375SYunfei Dong static int vdec_hevc_slice_alloc_mv_buf(struct vdec_hevc_slice_inst *inst,
6450934d375SYunfei Dong 					struct vdec_pic_info *pic)
6460934d375SYunfei Dong {
6470934d375SYunfei Dong 	unsigned int buf_sz = vdec_hevc_get_mv_buf_size(pic->buf_w, pic->buf_h);
6480934d375SYunfei Dong 	struct mtk_vcodec_mem *mem;
6490934d375SYunfei Dong 	int i, err;
6500934d375SYunfei Dong 
6510934d375SYunfei Dong 	mtk_v4l2_vdec_dbg(3, inst->ctx, "allocate mv buffer size = 0x%x", buf_sz);
6520934d375SYunfei Dong 	for (i = 0; i < HEVC_MAX_MV_NUM; i++) {
6530934d375SYunfei Dong 		mem = &inst->mv_buf[i];
6540934d375SYunfei Dong 		if (mem->va)
6550934d375SYunfei Dong 			mtk_vcodec_mem_free(inst->ctx, mem);
6560934d375SYunfei Dong 		mem->size = buf_sz;
6570934d375SYunfei Dong 		err = mtk_vcodec_mem_alloc(inst->ctx, mem);
6580934d375SYunfei Dong 		if (err) {
6590934d375SYunfei Dong 			mtk_vdec_err(inst->ctx, "failed to allocate mv buf");
6600934d375SYunfei Dong 			return err;
6610934d375SYunfei Dong 		}
6620934d375SYunfei Dong 	}
6630934d375SYunfei Dong 
6640934d375SYunfei Dong 	return 0;
6650934d375SYunfei Dong }
6660934d375SYunfei Dong 
vdec_hevc_slice_free_mv_buf(struct vdec_hevc_slice_inst * inst)6670934d375SYunfei Dong static void vdec_hevc_slice_free_mv_buf(struct vdec_hevc_slice_inst *inst)
6680934d375SYunfei Dong {
6690934d375SYunfei Dong 	int i;
6700934d375SYunfei Dong 	struct mtk_vcodec_mem *mem;
6710934d375SYunfei Dong 
6720934d375SYunfei Dong 	for (i = 0; i < HEVC_MAX_MV_NUM; i++) {
6730934d375SYunfei Dong 		mem = &inst->mv_buf[i];
6740934d375SYunfei Dong 		if (mem->va)
6750934d375SYunfei Dong 			mtk_vcodec_mem_free(inst->ctx, mem);
6760934d375SYunfei Dong 	}
6770934d375SYunfei Dong }
6780934d375SYunfei Dong 
vdec_hevc_slice_get_pic_info(struct vdec_hevc_slice_inst * inst)6790934d375SYunfei Dong static void vdec_hevc_slice_get_pic_info(struct vdec_hevc_slice_inst *inst)
6800934d375SYunfei Dong {
6810934d375SYunfei Dong 	struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
6820934d375SYunfei Dong 	u32 data[3];
6830934d375SYunfei Dong 
6840934d375SYunfei Dong 	data[0] = ctx->picinfo.pic_w;
6850934d375SYunfei Dong 	data[1] = ctx->picinfo.pic_h;
6860934d375SYunfei Dong 	data[2] = ctx->capture_fourcc;
6870934d375SYunfei Dong 	vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
6880934d375SYunfei Dong 
6890934d375SYunfei Dong 	ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, VCODEC_DEC_ALIGNED_64);
6900934d375SYunfei Dong 	ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, VCODEC_DEC_ALIGNED_64);
6910934d375SYunfei Dong 	ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
6920934d375SYunfei Dong 	ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
6930934d375SYunfei Dong 	inst->cap_num_planes =
6940934d375SYunfei Dong 		ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
6950934d375SYunfei Dong 
6960934d375SYunfei Dong 	mtk_vdec_debug(ctx, "pic(%d, %d), buf(%d, %d)",
6970934d375SYunfei Dong 		       ctx->picinfo.pic_w, ctx->picinfo.pic_h,
6980934d375SYunfei Dong 		       ctx->picinfo.buf_w, ctx->picinfo.buf_h);
6990934d375SYunfei Dong 	mtk_vdec_debug(ctx, "Y/C(%d, %d)", ctx->picinfo.fb_sz[0],
7000934d375SYunfei Dong 		       ctx->picinfo.fb_sz[1]);
7010934d375SYunfei Dong 
7020934d375SYunfei Dong 	if (ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w ||
7030934d375SYunfei Dong 	    ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h) {
7040934d375SYunfei Dong 		inst->resolution_changed = true;
7050934d375SYunfei Dong 		if (ctx->last_decoded_picinfo.buf_w != ctx->picinfo.buf_w ||
7060934d375SYunfei Dong 		    ctx->last_decoded_picinfo.buf_h != ctx->picinfo.buf_h)
7070934d375SYunfei Dong 			inst->realloc_mv_buf = true;
7080934d375SYunfei Dong 
7090934d375SYunfei Dong 		mtk_v4l2_vdec_dbg(1, inst->ctx, "resChg: (%d %d) : old(%d, %d) -> new(%d, %d)",
7100934d375SYunfei Dong 				  inst->resolution_changed,
7110934d375SYunfei Dong 				  inst->realloc_mv_buf,
7120934d375SYunfei Dong 				  ctx->last_decoded_picinfo.pic_w,
7130934d375SYunfei Dong 				  ctx->last_decoded_picinfo.pic_h,
7140934d375SYunfei Dong 				  ctx->picinfo.pic_w, ctx->picinfo.pic_h);
7150934d375SYunfei Dong 	}
7160934d375SYunfei Dong }
7170934d375SYunfei Dong 
vdec_hevc_slice_get_crop_info(struct vdec_hevc_slice_inst * inst,struct v4l2_rect * cr)7180934d375SYunfei Dong static void vdec_hevc_slice_get_crop_info(struct vdec_hevc_slice_inst *inst,
7190934d375SYunfei Dong 					  struct v4l2_rect *cr)
7200934d375SYunfei Dong {
7210934d375SYunfei Dong 	cr->left = 0;
7220934d375SYunfei Dong 	cr->top = 0;
7230934d375SYunfei Dong 	cr->width = inst->ctx->picinfo.pic_w;
7240934d375SYunfei Dong 	cr->height = inst->ctx->picinfo.pic_h;
7250934d375SYunfei Dong 
7260934d375SYunfei Dong 	mtk_vdec_debug(inst->ctx, "l=%d, t=%d, w=%d, h=%d",
7270934d375SYunfei Dong 		       cr->left, cr->top, cr->width, cr->height);
7280934d375SYunfei Dong }
7290934d375SYunfei Dong 
vdec_hevc_slice_setup_lat_buffer(struct vdec_hevc_slice_inst * inst,struct mtk_vcodec_mem * bs,struct vdec_lat_buf * lat_buf,bool * res_chg)7300934d375SYunfei Dong static int vdec_hevc_slice_setup_lat_buffer(struct vdec_hevc_slice_inst *inst,
7310934d375SYunfei Dong 					    struct mtk_vcodec_mem *bs,
7320934d375SYunfei Dong 					    struct vdec_lat_buf *lat_buf,
7330934d375SYunfei Dong 					    bool *res_chg)
7340934d375SYunfei Dong {
7350934d375SYunfei Dong 	struct mtk_vcodec_mem *mem;
7360934d375SYunfei Dong 	struct mtk_video_dec_buf *src_buf_info;
7370934d375SYunfei Dong 	struct vdec_hevc_slice_share_info *share_info;
7380934d375SYunfei Dong 	int i, err;
7390934d375SYunfei Dong 
7400934d375SYunfei Dong 	inst->vsi->bs.dma_addr = (u64)bs->dma_addr;
7410934d375SYunfei Dong 	inst->vsi->bs.size = bs->size;
7420934d375SYunfei Dong 
7430934d375SYunfei Dong 	src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
7440934d375SYunfei Dong 	lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
7450934d375SYunfei Dong 	v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
7460934d375SYunfei Dong 
7470934d375SYunfei Dong 	*res_chg = inst->resolution_changed;
7480934d375SYunfei Dong 	if (inst->resolution_changed) {
7490934d375SYunfei Dong 		mtk_vdec_debug(inst->ctx, "- resolution changed -");
7500934d375SYunfei Dong 		if (inst->realloc_mv_buf) {
7510934d375SYunfei Dong 			err = vdec_hevc_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
7520934d375SYunfei Dong 			inst->realloc_mv_buf = false;
7530934d375SYunfei Dong 			if (err)
7540934d375SYunfei Dong 				return err;
7550934d375SYunfei Dong 		}
7560934d375SYunfei Dong 		inst->resolution_changed = false;
7570934d375SYunfei Dong 	}
7580934d375SYunfei Dong 
7590934d375SYunfei Dong 	for (i = 0; i < HEVC_MAX_MV_NUM; i++) {
7600934d375SYunfei Dong 		mem = &inst->mv_buf[i];
7610934d375SYunfei Dong 		inst->vsi->mv_buf_dma[i].dma_addr = mem->dma_addr;
7620934d375SYunfei Dong 		inst->vsi->mv_buf_dma[i].size = mem->size;
7630934d375SYunfei Dong 	}
7640934d375SYunfei Dong 
7650934d375SYunfei Dong 	inst->vsi->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
7660934d375SYunfei Dong 	inst->vsi->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
7670934d375SYunfei Dong 
7680934d375SYunfei Dong 	inst->vsi->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
7690934d375SYunfei Dong 	inst->vsi->err_map.size = lat_buf->wdma_err_addr.size;
7700934d375SYunfei Dong 
7710934d375SYunfei Dong 	inst->vsi->slice_bc.dma_addr = lat_buf->slice_bc_addr.dma_addr;
7720934d375SYunfei Dong 	inst->vsi->slice_bc.size = lat_buf->slice_bc_addr.size;
7730934d375SYunfei Dong 
7740934d375SYunfei Dong 	inst->vsi->trans.dma_addr_end = inst->ctx->msg_queue.wdma_rptr_addr;
7750934d375SYunfei Dong 	inst->vsi->trans.dma_addr = inst->ctx->msg_queue.wdma_wptr_addr;
7760934d375SYunfei Dong 
7770934d375SYunfei Dong 	share_info = lat_buf->private_data;
7780934d375SYunfei Dong 	share_info->trans.dma_addr = inst->vsi->trans.dma_addr;
7790934d375SYunfei Dong 	share_info->trans.dma_addr_end = inst->vsi->trans.dma_addr_end;
7800934d375SYunfei Dong 
7810934d375SYunfei Dong 	mtk_vdec_debug(inst->ctx, "lat: ube addr/size(0x%llx 0x%llx) err:0x%llx",
7820934d375SYunfei Dong 		       inst->vsi->ube.buf,
7830934d375SYunfei Dong 		       inst->vsi->ube.padding,
7840934d375SYunfei Dong 		       inst->vsi->err_map.buf);
7850934d375SYunfei Dong 
7860934d375SYunfei Dong 	mtk_vdec_debug(inst->ctx, "slice addr/size(0x%llx 0x%llx) trans start/end((0x%llx 0x%llx))",
7870934d375SYunfei Dong 		       inst->vsi->slice_bc.buf,
7880934d375SYunfei Dong 		       inst->vsi->slice_bc.padding,
7890934d375SYunfei Dong 		       inst->vsi->trans.buf,
7900934d375SYunfei Dong 		       inst->vsi->trans.padding);
7910934d375SYunfei Dong 
7920934d375SYunfei Dong 	return 0;
7930934d375SYunfei Dong }
7940934d375SYunfei Dong 
vdec_hevc_slice_setup_core_buffer(struct vdec_hevc_slice_inst * inst,struct vdec_hevc_slice_share_info * share_info,struct vdec_lat_buf * lat_buf)7950934d375SYunfei Dong static int vdec_hevc_slice_setup_core_buffer(struct vdec_hevc_slice_inst *inst,
7960934d375SYunfei Dong 					     struct vdec_hevc_slice_share_info *share_info,
7970934d375SYunfei Dong 					     struct vdec_lat_buf *lat_buf)
7980934d375SYunfei Dong {
7990934d375SYunfei Dong 	struct mtk_vcodec_mem *mem;
8000934d375SYunfei Dong 	struct mtk_vcodec_dec_ctx *ctx = inst->ctx;
8010934d375SYunfei Dong 	struct vb2_v4l2_buffer *vb2_v4l2;
8020934d375SYunfei Dong 	struct vdec_fb *fb;
8030934d375SYunfei Dong 	u64 y_fb_dma, c_fb_dma;
8040934d375SYunfei Dong 	int i;
8050934d375SYunfei Dong 
8060934d375SYunfei Dong 	fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
8070934d375SYunfei Dong 	if (!fb) {
8080934d375SYunfei Dong 		mtk_vdec_err(inst->ctx, "fb buffer is NULL");
8090934d375SYunfei Dong 		return -EBUSY;
8100934d375SYunfei Dong 	}
8110934d375SYunfei Dong 
8120934d375SYunfei Dong 	y_fb_dma = (u64)fb->base_y.dma_addr;
8130934d375SYunfei Dong 	if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
8140934d375SYunfei Dong 		c_fb_dma =
8150934d375SYunfei Dong 			y_fb_dma + inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
8160934d375SYunfei Dong 	else
8170934d375SYunfei Dong 		c_fb_dma = (u64)fb->base_c.dma_addr;
8180934d375SYunfei Dong 
8190934d375SYunfei Dong 	mtk_vdec_debug(inst->ctx, "[hevc-core] y/c addr = 0x%llx 0x%llx", y_fb_dma, c_fb_dma);
8200934d375SYunfei Dong 
8210934d375SYunfei Dong 	inst->vsi_core->fb.y.dma_addr = y_fb_dma;
8220934d375SYunfei Dong 	inst->vsi_core->fb.y.size = ctx->picinfo.fb_sz[0];
8230934d375SYunfei Dong 	inst->vsi_core->fb.c.dma_addr = c_fb_dma;
8240934d375SYunfei Dong 	inst->vsi_core->fb.y.size = ctx->picinfo.fb_sz[1];
8250934d375SYunfei Dong 
8260934d375SYunfei Dong 	inst->vsi_core->dec.vdec_fb_va = (unsigned long)fb;
8270934d375SYunfei Dong 
8280934d375SYunfei Dong 	inst->vsi_core->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
8290934d375SYunfei Dong 	inst->vsi_core->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
8300934d375SYunfei Dong 
8310934d375SYunfei Dong 	inst->vsi_core->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
8320934d375SYunfei Dong 	inst->vsi_core->err_map.size = lat_buf->wdma_err_addr.size;
8330934d375SYunfei Dong 
8340934d375SYunfei Dong 	inst->vsi_core->slice_bc.dma_addr = lat_buf->slice_bc_addr.dma_addr;
8350934d375SYunfei Dong 	inst->vsi_core->slice_bc.size = lat_buf->slice_bc_addr.size;
8360934d375SYunfei Dong 
8370934d375SYunfei Dong 	inst->vsi_core->trans.dma_addr = share_info->trans.dma_addr;
8380934d375SYunfei Dong 	inst->vsi_core->trans.dma_addr_end = share_info->trans.dma_addr_end;
8390934d375SYunfei Dong 
8400934d375SYunfei Dong 	inst->vsi_core->wrap.dma_addr = inst->wrap_addr.dma_addr;
8410934d375SYunfei Dong 	inst->vsi_core->wrap.size = inst->wrap_addr.size;
8420934d375SYunfei Dong 
8430934d375SYunfei Dong 	for (i = 0; i < HEVC_MAX_MV_NUM; i++) {
8440934d375SYunfei Dong 		mem = &inst->mv_buf[i];
8450934d375SYunfei Dong 		inst->vsi_core->mv_buf_dma[i].dma_addr = mem->dma_addr;
8460934d375SYunfei Dong 		inst->vsi_core->mv_buf_dma[i].size = mem->size;
8470934d375SYunfei Dong 	}
8480934d375SYunfei Dong 
8490934d375SYunfei Dong 	vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
8500934d375SYunfei Dong 	v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
8510934d375SYunfei Dong 
8520934d375SYunfei Dong 	return 0;
8530934d375SYunfei Dong }
8540934d375SYunfei Dong 
vdec_hevc_slice_init(struct mtk_vcodec_dec_ctx * ctx)8550934d375SYunfei Dong static int vdec_hevc_slice_init(struct mtk_vcodec_dec_ctx *ctx)
8560934d375SYunfei Dong {
8570934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst;
8580934d375SYunfei Dong 	int err, vsi_size;
8590934d375SYunfei Dong 
8600934d375SYunfei Dong 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
8610934d375SYunfei Dong 	if (!inst)
8620934d375SYunfei Dong 		return -ENOMEM;
8630934d375SYunfei Dong 
8640934d375SYunfei Dong 	inst->ctx = ctx;
8650934d375SYunfei Dong 
8660934d375SYunfei Dong 	inst->vpu.id = SCP_IPI_VDEC_LAT;
8670934d375SYunfei Dong 	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
8680934d375SYunfei Dong 	inst->vpu.ctx = ctx;
8690934d375SYunfei Dong 	inst->vpu.codec_type = ctx->current_codec;
8700934d375SYunfei Dong 	inst->vpu.capture_type = ctx->capture_fourcc;
8710934d375SYunfei Dong 
8720934d375SYunfei Dong 	err = vpu_dec_init(&inst->vpu);
8730934d375SYunfei Dong 	if (err) {
8740934d375SYunfei Dong 		mtk_vdec_err(ctx, "vdec_hevc init err=%d", err);
8750934d375SYunfei Dong 		goto error_free_inst;
8760934d375SYunfei Dong 	}
8770934d375SYunfei Dong 
8780934d375SYunfei Dong 	vsi_size = round_up(sizeof(struct vdec_hevc_slice_vsi), VCODEC_DEC_ALIGNED_64);
8790934d375SYunfei Dong 	inst->vsi = inst->vpu.vsi;
8800934d375SYunfei Dong 	inst->vsi_core =
8810934d375SYunfei Dong 		(struct vdec_hevc_slice_vsi *)(((char *)inst->vpu.vsi) + vsi_size);
8820934d375SYunfei Dong 
8830934d375SYunfei Dong 	inst->resolution_changed = true;
8840934d375SYunfei Dong 	inst->realloc_mv_buf = true;
8850934d375SYunfei Dong 
8860934d375SYunfei Dong 	inst->wrap_addr.size = VDEC_HEVC_WRAP_SZ;
8870934d375SYunfei Dong 	err = mtk_vcodec_mem_alloc(ctx, &inst->wrap_addr);
8880934d375SYunfei Dong 	if (err)
8890934d375SYunfei Dong 		goto error_free_inst;
8900934d375SYunfei Dong 
8910934d375SYunfei Dong 	mtk_vdec_debug(ctx, "lat struct size = %d,%d,%d,%d vsi: %d\n",
8920934d375SYunfei Dong 		       (int)sizeof(struct mtk_hevc_sps_param),
8930934d375SYunfei Dong 		       (int)sizeof(struct mtk_hevc_pps_param),
8940934d375SYunfei Dong 		       (int)sizeof(struct vdec_hevc_slice_lat_dec_param),
8950934d375SYunfei Dong 		       (int)sizeof(struct mtk_hevc_dpb_info),
8960934d375SYunfei Dong 			 vsi_size);
8970934d375SYunfei Dong 	mtk_vdec_debug(ctx, "lat hevc instance >> %p, codec_type = 0x%x",
8980934d375SYunfei Dong 		       inst, inst->vpu.codec_type);
8990934d375SYunfei Dong 
900ec25fc3cSNicolas Dufresne 	ctx->drv_handle = inst;
9010934d375SYunfei Dong 	return 0;
9020934d375SYunfei Dong error_free_inst:
9030934d375SYunfei Dong 	kfree(inst);
9040934d375SYunfei Dong 	return err;
9050934d375SYunfei Dong }
9060934d375SYunfei Dong 
vdec_hevc_slice_deinit(void * h_vdec)9070934d375SYunfei Dong static void vdec_hevc_slice_deinit(void *h_vdec)
9080934d375SYunfei Dong {
9090934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst = h_vdec;
9100934d375SYunfei Dong 	struct mtk_vcodec_mem *mem;
9110934d375SYunfei Dong 
9120934d375SYunfei Dong 	vpu_dec_deinit(&inst->vpu);
9130934d375SYunfei Dong 	vdec_hevc_slice_free_mv_buf(inst);
9140934d375SYunfei Dong 
9150934d375SYunfei Dong 	mem = &inst->wrap_addr;
9160934d375SYunfei Dong 	if (mem->va)
9170934d375SYunfei Dong 		mtk_vcodec_mem_free(inst->ctx, mem);
9180934d375SYunfei Dong 
9190934d375SYunfei Dong 	vdec_msg_queue_deinit(&inst->ctx->msg_queue, inst->ctx);
9200934d375SYunfei Dong 	kfree(inst);
9210934d375SYunfei Dong }
9220934d375SYunfei Dong 
vdec_hevc_slice_core_decode(struct vdec_lat_buf * lat_buf)9230934d375SYunfei Dong static int vdec_hevc_slice_core_decode(struct vdec_lat_buf *lat_buf)
9240934d375SYunfei Dong {
9250934d375SYunfei Dong 	int err, timeout;
9260934d375SYunfei Dong 	struct mtk_vcodec_dec_ctx *ctx = lat_buf->ctx;
9270934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst = ctx->drv_handle;
9280934d375SYunfei Dong 	struct vdec_hevc_slice_share_info *share_info = lat_buf->private_data;
9290934d375SYunfei Dong 	struct vdec_vpu_inst *vpu = &inst->vpu;
9300934d375SYunfei Dong 
9310934d375SYunfei Dong 	mtk_vdec_debug(ctx, "[hevc-core] vdec_hevc core decode");
9320934d375SYunfei Dong 	memcpy(&inst->vsi_core->hevc_slice_params, &share_info->hevc_slice_params,
9330934d375SYunfei Dong 	       sizeof(share_info->hevc_slice_params));
9340934d375SYunfei Dong 
9350934d375SYunfei Dong 	err = vdec_hevc_slice_setup_core_buffer(inst, share_info, lat_buf);
9360934d375SYunfei Dong 	if (err)
9370934d375SYunfei Dong 		goto vdec_dec_end;
9380934d375SYunfei Dong 
9390934d375SYunfei Dong 	vdec_hevc_slice_fill_decode_reflist(inst, &inst->vsi_core->hevc_slice_params,
9400934d375SYunfei Dong 					    share_info);
9410934d375SYunfei Dong 	err = vpu_dec_core(vpu);
9420934d375SYunfei Dong 	if (err) {
9430934d375SYunfei Dong 		mtk_vdec_err(ctx, "core decode err=%d", err);
9440934d375SYunfei Dong 		goto vdec_dec_end;
9450934d375SYunfei Dong 	}
9460934d375SYunfei Dong 
9470934d375SYunfei Dong 	/* wait decoder done interrupt */
9480934d375SYunfei Dong 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
9490934d375SYunfei Dong 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
9500934d375SYunfei Dong 	if (timeout)
9510934d375SYunfei Dong 		mtk_vdec_err(ctx, "core decode timeout: pic_%d", ctx->decoded_frame_cnt);
9520934d375SYunfei Dong 	inst->vsi_core->dec.timeout = !!timeout;
9530934d375SYunfei Dong 
9540934d375SYunfei Dong 	vpu_dec_core_end(vpu);
9550934d375SYunfei Dong 	mtk_vdec_debug(ctx, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
9560934d375SYunfei Dong 		       ctx->decoded_frame_cnt,
9570934d375SYunfei Dong 		       inst->vsi_core->dec.crc[0], inst->vsi_core->dec.crc[1],
9580934d375SYunfei Dong 		       inst->vsi_core->dec.crc[2], inst->vsi_core->dec.crc[3],
9590934d375SYunfei Dong 		       inst->vsi_core->dec.crc[4], inst->vsi_core->dec.crc[5],
9600934d375SYunfei Dong 		       inst->vsi_core->dec.crc[6], inst->vsi_core->dec.crc[7]);
9610934d375SYunfei Dong 
9620934d375SYunfei Dong vdec_dec_end:
9630934d375SYunfei Dong 	vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans.dma_addr_end);
9640934d375SYunfei Dong 	ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
9650934d375SYunfei Dong 	mtk_vdec_debug(ctx, "core decode done err=%d", err);
9660934d375SYunfei Dong 	ctx->decoded_frame_cnt++;
9670934d375SYunfei Dong 	return 0;
9680934d375SYunfei Dong }
9690934d375SYunfei Dong 
vdec_hevc_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)9700934d375SYunfei Dong static int vdec_hevc_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
9710934d375SYunfei Dong 				      struct vdec_fb *fb, bool *res_chg)
9720934d375SYunfei Dong {
9730934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst = h_vdec;
9740934d375SYunfei Dong 	struct vdec_vpu_inst *vpu = &inst->vpu;
9750934d375SYunfei Dong 	int err, timeout = 0;
9760934d375SYunfei Dong 	unsigned int data[2];
9770934d375SYunfei Dong 	struct vdec_lat_buf *lat_buf;
9780934d375SYunfei Dong 	struct vdec_hevc_slice_share_info *share_info;
9790934d375SYunfei Dong 
9800934d375SYunfei Dong 	if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
9810934d375SYunfei Dong 				vdec_hevc_slice_core_decode,
9820934d375SYunfei Dong 				sizeof(*share_info)))
9830934d375SYunfei Dong 		return -ENOMEM;
9840934d375SYunfei Dong 
9850934d375SYunfei Dong 	/* bs NULL means flush decoder */
9860934d375SYunfei Dong 	if (!bs) {
9870934d375SYunfei Dong 		vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
9880934d375SYunfei Dong 		return vpu_dec_reset(vpu);
9890934d375SYunfei Dong 	}
9900934d375SYunfei Dong 
9910934d375SYunfei Dong 	lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
9920934d375SYunfei Dong 	if (!lat_buf) {
9930934d375SYunfei Dong 		mtk_vdec_debug(inst->ctx, "failed to get lat buffer");
9940934d375SYunfei Dong 		return -EAGAIN;
9950934d375SYunfei Dong 	}
9960934d375SYunfei Dong 
9970934d375SYunfei Dong 	share_info = lat_buf->private_data;
9980934d375SYunfei Dong 	err = vdec_hevc_slice_fill_decode_parameters(inst, share_info);
9990934d375SYunfei Dong 	if (err)
10000934d375SYunfei Dong 		goto err_free_fb_out;
10010934d375SYunfei Dong 
10020934d375SYunfei Dong 	err = vdec_hevc_slice_setup_lat_buffer(inst, bs, lat_buf, res_chg);
10030934d375SYunfei Dong 	if (err)
10040934d375SYunfei Dong 		goto err_free_fb_out;
10050934d375SYunfei Dong 
10060934d375SYunfei Dong 	err = vpu_dec_start(vpu, data, 2);
10070934d375SYunfei Dong 	if (err) {
10080934d375SYunfei Dong 		mtk_vdec_debug(inst->ctx, "lat decode err: %d", err);
10090934d375SYunfei Dong 		goto err_free_fb_out;
10100934d375SYunfei Dong 	}
10110934d375SYunfei Dong 
10120934d375SYunfei Dong 	if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
10130934d375SYunfei Dong 		memcpy(&share_info->hevc_slice_params, &inst->vsi->hevc_slice_params,
10140934d375SYunfei Dong 		       sizeof(share_info->hevc_slice_params));
10150934d375SYunfei Dong 		vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
10160934d375SYunfei Dong 	}
10170934d375SYunfei Dong 
10180934d375SYunfei Dong 	/* wait decoder done interrupt */
10190934d375SYunfei Dong 	timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
10200934d375SYunfei Dong 					       WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
10210934d375SYunfei Dong 	if (timeout)
10220934d375SYunfei Dong 		mtk_vdec_err(inst->ctx, "lat decode timeout: pic_%d", inst->slice_dec_num);
10230934d375SYunfei Dong 	inst->vsi->dec.timeout = !!timeout;
10240934d375SYunfei Dong 
10250934d375SYunfei Dong 	err = vpu_dec_end(vpu);
10260934d375SYunfei Dong 	if (err == SLICE_HEADER_FULL || err == TRANS_BUFFER_FULL) {
10270934d375SYunfei Dong 		if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
10280934d375SYunfei Dong 			vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
10290934d375SYunfei Dong 		inst->slice_dec_num++;
10300934d375SYunfei Dong 		mtk_vdec_err(inst->ctx, "lat dec fail: pic_%d err:%d", inst->slice_dec_num, err);
10310934d375SYunfei Dong 		return -EINVAL;
10320934d375SYunfei Dong 	}
10330934d375SYunfei Dong 
10340934d375SYunfei Dong 	share_info->trans.dma_addr_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
10350934d375SYunfei Dong 		inst->vsi->dec.wdma_end_addr_offset;
10360934d375SYunfei Dong 	vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans.dma_addr_end);
10370934d375SYunfei Dong 
10380934d375SYunfei Dong 	if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
10390934d375SYunfei Dong 		memcpy(&share_info->hevc_slice_params, &inst->vsi->hevc_slice_params,
10400934d375SYunfei Dong 		       sizeof(share_info->hevc_slice_params));
10410934d375SYunfei Dong 		vdec_msg_queue_qbuf(&inst->ctx->msg_queue.core_ctx, lat_buf);
10420934d375SYunfei Dong 	}
10430934d375SYunfei Dong 	mtk_vdec_debug(inst->ctx, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
10440934d375SYunfei Dong 		       inst->vsi->dec.crc[0], inst->vsi->dec.crc[1], inst->vsi->dec.crc[2]);
10450934d375SYunfei Dong 
10460934d375SYunfei Dong 	inst->slice_dec_num++;
10470934d375SYunfei Dong 	return 0;
10480934d375SYunfei Dong err_free_fb_out:
10490934d375SYunfei Dong 	vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
10500934d375SYunfei Dong 	mtk_vdec_err(inst->ctx, "slice dec number: %d err: %d", inst->slice_dec_num, err);
10510934d375SYunfei Dong 	return err;
10520934d375SYunfei Dong }
10530934d375SYunfei Dong 
vdec_hevc_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * unused,bool * res_chg)10540934d375SYunfei Dong static int vdec_hevc_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
10550934d375SYunfei Dong 				  struct vdec_fb *unused, bool *res_chg)
10560934d375SYunfei Dong {
10570934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst = h_vdec;
10580934d375SYunfei Dong 
10590934d375SYunfei Dong 	if (!h_vdec || inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
10600934d375SYunfei Dong 		return -EINVAL;
10610934d375SYunfei Dong 
10620934d375SYunfei Dong 	return vdec_hevc_slice_lat_decode(h_vdec, bs, unused, res_chg);
10630934d375SYunfei Dong }
10640934d375SYunfei Dong 
vdec_hevc_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)10650934d375SYunfei Dong static int vdec_hevc_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
10660934d375SYunfei Dong 				     void *out)
10670934d375SYunfei Dong {
10680934d375SYunfei Dong 	struct vdec_hevc_slice_inst *inst = h_vdec;
10690934d375SYunfei Dong 
10700934d375SYunfei Dong 	switch (type) {
10710934d375SYunfei Dong 	case GET_PARAM_PIC_INFO:
10720934d375SYunfei Dong 		vdec_hevc_slice_get_pic_info(inst);
10730934d375SYunfei Dong 		break;
10740934d375SYunfei Dong 	case GET_PARAM_DPB_SIZE:
10750934d375SYunfei Dong 		*(unsigned int *)out = 6;
10760934d375SYunfei Dong 		break;
10770934d375SYunfei Dong 	case GET_PARAM_CROP_INFO:
10780934d375SYunfei Dong 		vdec_hevc_slice_get_crop_info(inst, out);
10790934d375SYunfei Dong 		break;
10800934d375SYunfei Dong 	default:
10810934d375SYunfei Dong 		mtk_vdec_err(inst->ctx, "invalid get parameter type=%d", type);
10820934d375SYunfei Dong 		return -EINVAL;
10830934d375SYunfei Dong 	}
10840934d375SYunfei Dong 	return 0;
10850934d375SYunfei Dong }
10860934d375SYunfei Dong 
10870934d375SYunfei Dong const struct vdec_common_if vdec_hevc_slice_multi_if = {
10880934d375SYunfei Dong 	.init		= vdec_hevc_slice_init,
10890934d375SYunfei Dong 	.decode		= vdec_hevc_slice_decode,
10900934d375SYunfei Dong 	.get_param	= vdec_hevc_slice_get_param,
10910934d375SYunfei Dong 	.deinit		= vdec_hevc_slice_deinit,
10920934d375SYunfei Dong };
1093