• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * drivers/amlogic/amports/vvp9.c
3   *
4   * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5   *
6   * This program is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14   * more details.
15   *
16   */
17 #define DEBUG
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/interrupt.h>
23 #include <linux/semaphore.h>
24 #include <linux/delay.h>
25 #include <linux/timer.h>
26 #include <linux/kfifo.h>
27 #include <linux/kthread.h>
28 #include <linux/spinlock.h>
29 #include <linux/platform_device.h>
30 #include <linux/amlogic/media/vfm/vframe.h>
31 #include <linux/amlogic/media/utils/amstream.h>
32 #include <linux/amlogic/media/utils/vformat.h>
33 #include <linux/amlogic/media/frame_sync/ptsserv.h>
34 #include <linux/amlogic/media/canvas/canvas.h>
35 #include <linux/amlogic/media/vfm/vframe_provider.h>
36 #include <linux/amlogic/media/vfm/vframe_receiver.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/dma-map-ops.h>
39 #include <linux/sched/clock.h>
40 #include <linux/slab.h>
41 #include <linux/amlogic/tee.h>
42 #include "../../../stream_input/amports/amports_priv.h"
43 #include <linux/amlogic/media/codec_mm/codec_mm.h>
44 #include "../utils/decoder_mmu_box.h"
45 #include "../utils/decoder_bmmu_box.h"
46 
47 #define MEM_NAME "codec_vp9"
48 /* #include <mach/am_regs.h> */
49 #include <linux/amlogic/media/utils/vdec_reg.h>
50 #include "../utils/vdec.h"
51 #include "../utils/amvdec.h"
52 #ifdef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
53 #include "../utils/vdec_profile.h"
54 #endif
55 
56 #include <linux/amlogic/media/video_sink/video.h>
57 #include <linux/amlogic/media/codec_mm/configs.h>
58 #include "../utils/config_parser.h"
59 #include "../utils/firmware.h"
60 #include "../../../common/chips/decoder_cpu_ver_info.h"
61 #include "../utils/vdec_v4l2_buffer_ops.h"
62 #include <media/v4l2-mem2mem.h>
63 
64 #define MIX_STREAM_SUPPORT
65 
66 #include "vvp9.h"
67 
68 
69 /*#define SUPPORT_FB_DECODING*/
70 /*#define FB_DECODING_TEST_SCHEDULE*/
71 
72 
73 #define HW_MASK_FRONT    0x1
74 #define HW_MASK_BACK     0x2
75 
76 #define VP9D_MPP_REFINFO_TBL_ACCCONFIG             0x3442
77 #define VP9D_MPP_REFINFO_DATA                      0x3443
78 #define VP9D_MPP_REF_SCALE_ENBL                    0x3441
79 #define HEVC_MPRED_CTRL4                           0x324c
80 #define HEVC_CM_HEADER_START_ADDR                  0x3628
81 #define HEVC_DBLK_CFGB                             0x350b
82 #define HEVCD_MPP_ANC2AXI_TBL_DATA                 0x3464
83 #define HEVC_SAO_MMU_VH1_ADDR                      0x363b
84 #define HEVC_SAO_MMU_VH0_ADDR                      0x363a
85 
86 #define HEVC_MV_INFO                               0x310d
87 #define HEVC_QP_INFO                               0x3137
88 #define HEVC_SKIP_INFO                             0x3136
89 
90 #define VP9_10B_DEC_IDLE                           0
91 #define VP9_10B_DEC_FRAME_HEADER                   1
92 #define VP9_10B_DEC_SLICE_SEGMENT                  2
93 #define VP9_10B_DECODE_SLICE                     5
94 #define VP9_10B_DISCARD_NAL                  6
95 #define VP9_DUMP_LMEM                7
96 #define HEVC_DECPIC_DATA_DONE            0xa
97 #define HEVC_DECPIC_DATA_ERROR            0xb
98 #define HEVC_NAL_DECODE_DONE            0xe
99 #define HEVC_DECODE_BUFEMPTY        0x20
100 #define HEVC_DECODE_TIMEOUT         0x21
101 #define HEVC_SEARCH_BUFEMPTY        0x22
102 #define HEVC_DECODE_OVER_SIZE       0x23
103 #define HEVC_S2_DECODING_DONE       0x50
104 #define VP9_HEAD_PARSER_DONE            0xf0
105 #define VP9_HEAD_SEARCH_DONE          0xf1
106 #define VP9_EOS                        0xf2
107 #define HEVC_ACTION_DONE                0xff
108 
109 #define VF_POOL_SIZE        32
110 
111 #undef pr_info
112 #define pr_info printk
113 
114 #define DECODE_MODE_SINGLE		((0x80 << 24) | 0)
115 #define DECODE_MODE_MULTI_STREAMBASE	((0x80 << 24) | 1)
116 #define DECODE_MODE_MULTI_FRAMEBASE	((0x80 << 24) | 2)
117 #define DECODE_MODE_SINGLE_LOW_LATENCY ((0x80 << 24) | 3)
118 #define DECODE_MODE_MULTI_FRAMEBASE_NOHEAD ((0x80 << 24) | 4)
119 
120 #define  VP9_TRIGGER_FRAME_DONE		0x100
121 #define  VP9_TRIGGER_FRAME_ENABLE	0x200
122 
123 #define MV_MEM_UNIT 0x240
124 /*---------------------------------------------------
125  * Include "parser_cmd.h"
126  *---------------------------------------------------
127  */
128 #define PARSER_CMD_SKIP_CFG_0 0x0000090b
129 
130 #define PARSER_CMD_SKIP_CFG_1 0x1b14140f
131 
132 #define PARSER_CMD_SKIP_CFG_2 0x001b1910
133 
134 #define PARSER_CMD_NUMBER 37
135 
136 /*#define HEVC_PIC_STRUCT_SUPPORT*/
137 /* to remove, fix build error */
138 
139 /*#define CODEC_MM_FLAGS_FOR_VDECODER  0*/
140 
141 #define MULTI_INSTANCE_SUPPORT
142 #define SUPPORT_10BIT
143 /* #define ERROR_HANDLE_DEBUG */
144 
145 #ifndef STAT_KTHREAD
146 #define STAT_KTHREAD 0x40
147 #endif
148 
149 #ifdef MULTI_INSTANCE_SUPPORT
150 #define MAX_DECODE_INSTANCE_NUM     9
151 #define MULTI_DRIVER_NAME "ammvdec_vp9"
152 static unsigned int max_decode_instance_num
153 				= MAX_DECODE_INSTANCE_NUM;
154 static unsigned int decode_frame_count[MAX_DECODE_INSTANCE_NUM];
155 static unsigned int display_frame_count[MAX_DECODE_INSTANCE_NUM];
156 static unsigned int max_process_time[MAX_DECODE_INSTANCE_NUM];
157 static unsigned int run_count[MAX_DECODE_INSTANCE_NUM];
158 static unsigned int input_empty[MAX_DECODE_INSTANCE_NUM];
159 static unsigned int not_run_ready[MAX_DECODE_INSTANCE_NUM];
160 
161 static u32 decode_timeout_val = 200;
162 static int start_decode_buf_level = 0x8000;
163 static u32 work_buf_size;
164 
165 static u32 mv_buf_margin;
166 
167 /* DOUBLE_WRITE_MODE is enabled only when NV21 8 bit output is needed */
168 /* double_write_mode:
169  *	0, no double write;
170  *	1, 1:1 ratio;
171  *	2, (1/4):(1/4) ratio;
172  *	3, (1/4):(1/4) ratio, with both compressed frame included
173  *	4, (1/2):(1/2) ratio;
174  *	0x10, double write only
175  *	0x100, if > 1080p,use mode 4,else use mode 1;
176  *	0x200, if > 1080p,use mode 2,else use mode 1;
177  *	0x300, if > 720p, use mode 4, else use mode 1;
178  */
179 static u32 double_write_mode;
180 
181 #define DRIVER_NAME "amvdec_vp9"
182 #define MODULE_NAME "amvdec_vp9"
183 #define DRIVER_HEADER_NAME "amvdec_vp9_header"
184 
185 
186 #define PUT_INTERVAL        (HZ/100)
187 #define ERROR_SYSTEM_RESET_COUNT   200
188 
189 #define PTS_NORMAL                0
190 #define PTS_NONE_REF_USE_DURATION 1
191 
192 #define PTS_MODE_SWITCHING_THRESHOLD           3
193 #define PTS_MODE_SWITCHING_RECOVERY_THREASHOLD 3
194 
195 #define DUR2PTS(x) ((x)*90/96)
196 
197 struct VP9Decoder_s;
198 static int vvp9_vf_states(struct vframe_states *states, void *);
199 static struct vframe_s *vvp9_vf_peek(void *);
200 static struct vframe_s *vvp9_vf_get(void *);
201 static void vvp9_vf_put(struct vframe_s *, void *);
202 static int vvp9_event_cb(int type, void *data, void *private_data);
203 
204 static int vvp9_stop(struct VP9Decoder_s *pbi);
205 #ifdef MULTI_INSTANCE_SUPPORT
206 static s32 vvp9_init(struct vdec_s *vdec);
207 #else
208 static s32 vvp9_init(struct VP9Decoder_s *pbi);
209 #endif
210 static void vvp9_prot_init(struct VP9Decoder_s *pbi, u32 mask);
211 static int vvp9_local_init(struct VP9Decoder_s *pbi);
212 static void vvp9_put_timer_func(struct timer_list *timer);
213 static void dump_data(struct VP9Decoder_s *pbi, int size);
214 static unsigned char get_data_check_sum
215 	(struct VP9Decoder_s *pbi, int size);
216 static void dump_pic_list(struct VP9Decoder_s *pbi);
217 static int vp9_alloc_mmu(
218 		struct VP9Decoder_s *pbi,
219 		int cur_buf_idx,
220 		int pic_width,
221 		int pic_height,
222 		unsigned short bit_depth,
223 		unsigned int *mmu_index_adr);
224 
225 
226 static const char vvp9_dec_id[] = "vvp9-dev";
227 
228 #define PROVIDER_NAME   "decoder.vp9"
229 #define MULTI_INSTANCE_PROVIDER_NAME    "vdec.vp9"
230 
231 static const struct vframe_operations_s vvp9_vf_provider = {
232 	.peek = vvp9_vf_peek,
233 	.get = vvp9_vf_get,
234 	.put = vvp9_vf_put,
235 	.event_cb = vvp9_event_cb,
236 	.vf_states = vvp9_vf_states,
237 };
238 
239 static struct vframe_provider_s vvp9_vf_prov;
240 
241 static u32 bit_depth_luma;
242 static u32 bit_depth_chroma;
243 static u32 frame_width;
244 static u32 frame_height;
245 static u32 video_signal_type;
246 
247 static u32 on_no_keyframe_skiped;
248 
249 #define PROB_SIZE    (496 * 2 * 4)
250 #define PROB_BUF_SIZE    (0x5000)
251 #define COUNT_BUF_SIZE   (0x300 * 4 * 4)
252 /*compute_losless_comp_body_size(4096, 2304, 1) = 18874368(0x1200000)*/
253 #define MAX_FRAME_4K_NUM 0x1200
254 #define MAX_FRAME_8K_NUM 0x4800
255 
256 #define HEVC_ASSIST_MMU_MAP_ADDR                   0x3009
257 
258 #ifdef SUPPORT_FB_DECODING
259 /* register define */
260 #define HEVC_ASSIST_HED_FB_W_CTL                   0x3006
261 #define HEVC_ASSIST_HED_FB_R_CTL                   0x3007
262 #define HEVC_ASSIST_HED_FB_ADDR                    0x3008
263 #define HEVC_ASSIST_FB_MMU_MAP_ADDR                0x300a
264 #define HEVC_ASSIST_FBD_MMU_MAP_ADDR               0x300b
265 
266 
267 #define MAX_STAGE_PAGE_NUM 0x1200
268 #define STAGE_MMU_MAP_SIZE (MAX_STAGE_PAGE_NUM * 4)
269 #endif
div_r32(int64_t m,int n)270 static inline int div_r32(int64_t m, int n)
271 {
272 /*
273  *return (int)(m/n)
274  */
275 #ifndef CONFIG_ARM64
276 	int64_t qu = 0;
277 	qu = div_s64(m, n);
278 	return (int)qu;
279 #else
280 	return (int)(m/n);
281 #endif
282 }
283 
284 /*USE_BUF_BLOCK*/
285 struct BUF_s {
286 	int index;
287 	unsigned int alloc_flag;
288 	/*buffer */
289 	unsigned int cma_page_count;
290 	unsigned long alloc_addr;
291 	unsigned long start_adr;
292 	unsigned int size;
293 
294 	unsigned int free_start_adr;
295 	ulong v4l_ref_buf_addr;
296 	ulong	header_addr;
297 	u32 	header_size;
298 	u32	luma_size;
299 	ulong	chroma_addr;
300 	u32	chroma_size;
301 } /*BUF_t */;
302 
303 struct MVBUF_s {
304 	unsigned long start_adr;
305 	unsigned int size;
306 	int used_flag;
307 } /*MVBUF_t */;
308 
309 	/* #undef BUFMGR_ONLY to enable hardware configuration */
310 
311 /*#define TEST_WR_PTR_INC*/
312 /*#define WR_PTR_INC_NUM 128*/
313 #define WR_PTR_INC_NUM 1
314 
315 #define SIMULATION
316 #define DOS_PROJECT
317 #undef MEMORY_MAP_IN_REAL_CHIP
318 
319 /*#undef DOS_PROJECT*/
320 /*#define MEMORY_MAP_IN_REAL_CHIP*/
321 
322 /*#define BUFFER_MGR_ONLY*/
323 /*#define CONFIG_HEVC_CLK_FORCED_ON*/
324 /*#define ENABLE_SWAP_TEST*/
325 #define   MCRCC_ENABLE
326 
327 #define VP9_LPF_LVL_UPDATE
328 /*#define DBG_LF_PRINT*/
329 
330 #ifdef VP9_10B_NV21
331 #else
332 #define LOSLESS_COMPRESS_MODE
333 #endif
334 
335 #define DOUBLE_WRITE_YSTART_TEMP 0x02000000
336 #define DOUBLE_WRITE_CSTART_TEMP 0x02900000
337 
338 
339 
340 typedef unsigned int u32;
341 typedef unsigned short u16;
342 
343 #define VP9_DEBUG_BUFMGR                   0x01
344 #define VP9_DEBUG_BUFMGR_MORE              0x02
345 #define VP9_DEBUG_BUFMGR_DETAIL            0x04
346 #define VP9_DEBUG_OUT_PTS                  0x10
347 #define VP9_DEBUG_SEND_PARAM_WITH_REG      0x100
348 #define VP9_DEBUG_MERGE                    0x200
349 #define VP9_DEBUG_DBG_LF_PRINT             0x400
350 #define VP9_DEBUG_REG                      0x800
351 #define VP9_DEBUG_2_STAGE                  0x1000
352 #define VP9_DEBUG_2_STAGE_MORE             0x2000
353 #define VP9_DEBUG_QOS_INFO                 0x4000
354 #define VP9_DEBUG_DIS_LOC_ERROR_PROC       0x10000
355 #define VP9_DEBUG_DIS_SYS_ERROR_PROC   0x20000
356 #define VP9_DEBUG_DUMP_PIC_LIST       0x40000
357 #define VP9_DEBUG_TRIG_SLICE_SEGMENT_PROC 0x80000
358 #define VP9_DEBUG_NO_TRIGGER_FRAME       0x100000
359 #define VP9_DEBUG_LOAD_UCODE_FROM_FILE   0x200000
360 #define VP9_DEBUG_FORCE_SEND_AGAIN       0x400000
361 #define VP9_DEBUG_DUMP_DATA              0x800000
362 #define VP9_DEBUG_CACHE                  0x1000000
363 #define VP9_DEBUG_CACHE_HIT_RATE         0x2000000
364 #define IGNORE_PARAM_FROM_CONFIG         0x8000000
365 #ifdef MULTI_INSTANCE_SUPPORT
366 #define PRINT_FLAG_ERROR		0x0
367 #define PRINT_FLAG_V4L_DETAIL		0x10000000
368 #define PRINT_FLAG_VDEC_STATUS		0x20000000
369 #define PRINT_FLAG_VDEC_DETAIL		0x40000000
370 #define PRINT_FLAG_VDEC_DATA		0x80000000
371 #endif
372 
373 static u32 debug;
374 static bool is_reset;
375 /*for debug*/
376 /*
377 	udebug_flag:
378 	bit 0, enable ucode print
379 	bit 1, enable ucode detail print
380 	bit [31:16] not 0, pos to dump lmem
381 		bit 2, pop bits to lmem
382 		bit [11:8], pre-pop bits for alignment (when bit 2 is 1)
383 */
384 static u32 udebug_flag;
385 /*
386 	when udebug_flag[1:0] is not 0
387 	udebug_pause_pos not 0,
388 		pause position
389 */
390 static u32 udebug_pause_pos;
391 /*
392 	when udebug_flag[1:0] is not 0
393 	and udebug_pause_pos is not 0,
394 		pause only when DEBUG_REG2 is equal to this val
395 */
396 static u32 udebug_pause_val;
397 
398 static u32 udebug_pause_decode_idx;
399 
400 static u32 without_display_mode;
401 
402 #define DEBUG_REG
403 #ifdef DEBUG_REG
WRITE_VREG_DBG2(unsigned int adr,unsigned int val)404 void WRITE_VREG_DBG2(unsigned int adr, unsigned int val)
405 {
406 	if (debug & VP9_DEBUG_REG)
407 		pr_info("%s(%x, %x)\n", __func__, adr, val);
408 	if (adr != 0)
409 		WRITE_VREG(adr, val);
410 }
411 
412 #undef WRITE_VREG
413 #define WRITE_VREG WRITE_VREG_DBG2
414 #endif
415 
416 #define FRAME_CNT_WINDOW_SIZE 59
417 #define RATE_CORRECTION_THRESHOLD 5
418 /**************************************************
419 
420 VP9 buffer management start
421 
422 ***************************************************/
423 
424 #define MMU_COMPRESS_HEADER_SIZE  0x48000
425 #define MMU_COMPRESS_8K_HEADER_SIZE  (0x48000*4)
426 #define MAX_SIZE_8K (8192 * 4608)
427 #define MAX_SIZE_4K (4096 * 2304)
428 #define IS_8K_SIZE(w, h)	(((w) * (h)) > MAX_SIZE_4K)
429 
430 #define INVALID_IDX -1  /* Invalid buffer index.*/
431 
432 #define RPM_BEGIN                                              0x200
433 #define RPM_END                                                0x280
434 
435 union param_u {
436 	struct {
437 		unsigned short data[RPM_END - RPM_BEGIN];
438 	} l;
439 	struct {
440 		/* from ucode lmem, do not change this struct */
441 		unsigned short profile;
442 		unsigned short show_existing_frame;
443 		unsigned short frame_to_show_idx;
444 		unsigned short frame_type; /*1 bit*/
445 		unsigned short show_frame; /*1 bit*/
446 		unsigned short error_resilient_mode; /*1 bit*/
447 		unsigned short intra_only; /*1 bit*/
448 		unsigned short display_size_present; /*1 bit*/
449 		unsigned short reset_frame_context;
450 		unsigned short refresh_frame_flags;
451 		unsigned short width;
452 		unsigned short height;
453 		unsigned short display_width;
454 		unsigned short display_height;
455 	/*
456 	 *bit[11:8] - ref_frame_info_0 (ref(3-bits), ref_frame_sign_bias(1-bit))
457 	 *bit[7:4]  - ref_frame_info_1 (ref(3-bits), ref_frame_sign_bias(1-bit))
458 	 *bit[3:0]  - ref_frame_info_2 (ref(3-bits), ref_frame_sign_bias(1-bit))
459 	 */
460 		unsigned short ref_info;
461 		/*
462 		 *bit[2]: same_frame_size0
463 		 *bit[1]: same_frame_size1
464 		 *bit[0]: same_frame_size2
465 		 */
466 		unsigned short same_frame_size;
467 
468 		unsigned short mode_ref_delta_enabled;
469 		unsigned short ref_deltas[4];
470 		unsigned short mode_deltas[2];
471 		unsigned short filter_level;
472 		unsigned short sharpness_level;
473 		unsigned short bit_depth;
474 		unsigned short seg_quant_info[8];
475 		unsigned short seg_enabled;
476 		unsigned short seg_abs_delta;
477 		/* bit 15: feature enabled; bit 8, sign; bit[5:0], data */
478 		unsigned short seg_lf_info[8];
479 	} p;
480 };
481 
482 
483 struct vpx_codec_frame_buffer_s {
484 	uint8_t *data;  /**< Pointer to the data buffer */
485 	size_t size;  /**< Size of data in bytes */
486 	void *priv;  /**< Frame's private data */
487 };
488 
489 enum vpx_color_space_t {
490 	VPX_CS_UNKNOWN    = 0,  /**< Unknown */
491 	VPX_CS_BT_601     = 1,  /**< BT.601 */
492 	VPX_CS_BT_709     = 2,  /**< BT.709 */
493 	VPX_CS_SMPTE_170  = 3,  /**< SMPTE.170 */
494 	VPX_CS_SMPTE_240  = 4,  /**< SMPTE.240 */
495 	VPX_CS_BT_2020    = 5,  /**< BT.2020 */
496 	VPX_CS_RESERVED   = 6,  /**< Reserved */
497 	VPX_CS_SRGB       = 7   /**< sRGB */
498 }; /**< alias for enum vpx_color_space */
499 
500 enum vpx_bit_depth_t {
501 	VPX_BITS_8  =  8,  /**<  8 bits */
502 	VPX_BITS_10 = 10,  /**< 10 bits */
503 	VPX_BITS_12 = 12,  /**< 12 bits */
504 };
505 
506 #define MAX_SLICE_NUM 1024
507 struct PIC_BUFFER_CONFIG_s {
508 	int index;
509 	int BUF_index;
510 	int mv_buf_index;
511 	int comp_body_size;
512 	int buf_size;
513 	int vf_ref;
514 	int y_canvas_index;
515 	int uv_canvas_index;
516 #ifdef MULTI_INSTANCE_SUPPORT
517 	struct canvas_config_s canvas_config[2];
518 #endif
519 	int decode_idx;
520 	int slice_type;
521 	int stream_offset;
522 	u32 pts;
523 	u64 pts64;
524 	u64 timestamp;
525 	uint8_t error_mark;
526 	/**/
527 	int slice_idx;
528 	/*buffer*/
529 	unsigned long header_adr;
530 	unsigned long mpred_mv_wr_start_addr;
531 	/*unsigned long mc_y_adr;
532 	 *unsigned long mc_u_v_adr;
533 	 */
534 	unsigned int dw_y_adr;
535 	unsigned int dw_u_v_adr;
536 	int mc_canvas_y;
537 	int mc_canvas_u_v;
538 
539 	int lcu_total;
540 	/**/
541 	int   y_width;
542 	int   y_height;
543 	int   y_crop_width;
544 	int   y_crop_height;
545 	int   y_stride;
546 
547 	int   uv_width;
548 	int   uv_height;
549 	int   uv_crop_width;
550 	int   uv_crop_height;
551 	int   uv_stride;
552 
553 	int   alpha_width;
554 	int   alpha_height;
555 	int   alpha_stride;
556 
557 	uint8_t *y_buffer;
558 	uint8_t *u_buffer;
559 	uint8_t *v_buffer;
560 	uint8_t *alpha_buffer;
561 
562 	uint8_t *buffer_alloc;
563 	int buffer_alloc_sz;
564 	int border;
565 	int frame_size;
566 	int subsampling_x;
567 	int subsampling_y;
568 	unsigned int bit_depth;
569 	enum vpx_color_space_t color_space;
570 
571 	int corrupted;
572 	int flags;
573 	unsigned long cma_alloc_addr;
574 
575 	int double_write_mode;
576 
577 	/* picture qos infomation*/
578 	int max_qp;
579 	int avg_qp;
580 	int min_qp;
581 	int max_skip;
582 	int avg_skip;
583 	int min_skip;
584 	int max_mv;
585 	int min_mv;
586 	int avg_mv;
587 
588 	u32 hw_decode_time;
589 	u32 frame_size2; // For frame base mode
590 	bool vframe_bound;
591 } PIC_BUFFER_CONFIG;
592 
593 enum BITSTREAM_PROFILE {
594 	PROFILE_0,
595 	PROFILE_1,
596 	PROFILE_2,
597 	PROFILE_3,
598 	MAX_PROFILES
599 };
600 
601 enum FRAME_TYPE {
602 	KEY_FRAME = 0,
603 	INTER_FRAME = 1,
604 	FRAME_TYPES,
605 };
606 
607 enum REFERENCE_MODE {
608 	SINGLE_REFERENCE      = 0,
609 	COMPOUND_REFERENCE    = 1,
610 	REFERENCE_MODE_SELECT = 2,
611 	REFERENCE_MODES       = 3,
612 };
613 
614 #define NONE           -1
615 #define INTRA_FRAME     0
616 #define LAST_FRAME      1
617 #define GOLDEN_FRAME    2
618 #define ALTREF_FRAME    3
619 #define MAX_REF_FRAMES  4
620 
621 #define REFS_PER_FRAME 3
622 
623 #define REF_FRAMES_LOG2 3
624 #define REF_FRAMES (1 << REF_FRAMES_LOG2)
625 #define REF_FRAMES_4K (6)
626 
627 /*4 scratch frames for the new frames to support a maximum of 4 cores decoding
628  *in parallel, 3 for scaled references on the encoder.
629  *TODO(hkuang): Add ondemand frame buffers instead of hardcoding the number
630  * // of framebuffers.
631  *TODO(jkoleszar): These 3 extra references could probably come from the
632  *normal reference pool.
633  */
634 #define FRAME_BUFFERS (REF_FRAMES + 16)
635 #define HEADER_FRAME_BUFFERS (FRAME_BUFFERS)
636 #define MAX_BUF_NUM (FRAME_BUFFERS)
637 #define MV_BUFFER_NUM	FRAME_BUFFERS
638 #ifdef SUPPORT_FB_DECODING
639 #define STAGE_MAX_BUFFERS		16
640 #else
641 #define STAGE_MAX_BUFFERS		0
642 #endif
643 
644 #define FRAME_CONTEXTS_LOG2 2
645 #define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
646 /*buffer + header buffer + workspace*/
647 #ifdef MV_USE_FIXED_BUF
648 #define MAX_BMMU_BUFFER_NUM (FRAME_BUFFERS + HEADER_FRAME_BUFFERS + 1)
649 #define VF_BUFFER_IDX(n) (n)
650 #define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n)
651 #define WORK_SPACE_BUF_ID (FRAME_BUFFERS + HEADER_FRAME_BUFFERS)
652 #else
653 #define MAX_BMMU_BUFFER_NUM \
654 	(FRAME_BUFFERS + HEADER_FRAME_BUFFERS + MV_BUFFER_NUM + 1)
655 #define VF_BUFFER_IDX(n) (n)
656 #define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n)
657 #define MV_BUFFER_IDX(n) (FRAME_BUFFERS + HEADER_FRAME_BUFFERS + n)
658 #define WORK_SPACE_BUF_ID \
659 	(FRAME_BUFFERS + HEADER_FRAME_BUFFERS + MV_BUFFER_NUM)
660 #endif
661 
662 struct RefCntBuffer_s {
663 	int ref_count;
664 	/*MV_REF *mvs;*/
665 	int mi_rows;
666 	int mi_cols;
667 	struct vpx_codec_frame_buffer_s raw_frame_buffer;
668 	struct PIC_BUFFER_CONFIG_s buf;
669 
670 /*The Following variables will only be used in frame parallel decode.
671  *
672  *frame_worker_owner indicates which FrameWorker owns this buffer. NULL means
673  *that no FrameWorker owns, or is decoding, this buffer.
674  *VP9Worker *frame_worker_owner;
675  *
676  *row and col indicate which position frame has been decoded to in real
677  *pixel unit. They are reset to -1 when decoding begins and set to INT_MAX
678  *when the frame is fully decoded.
679  */
680 	int row;
681 	int col;
682 } RefCntBuffer;
683 
684 struct RefBuffer_s {
685 /*TODO(dkovalev): idx is not really required and should be removed, now it
686  *is used in vp9_onyxd_if.c
687  */
688 	int idx;
689 	struct PIC_BUFFER_CONFIG_s *buf;
690 	/*struct scale_factors sf;*/
691 } RefBuffer;
692 
693 struct InternalFrameBuffer_s {
694 	uint8_t *data;
695 	size_t size;
696 	int in_use;
697 } InternalFrameBuffer;
698 
699 struct InternalFrameBufferList_s {
700 	int num_internal_frame_buffers;
701 	struct InternalFrameBuffer_s *int_fb;
702 } InternalFrameBufferList;
703 
704 struct BufferPool_s {
705 /*Protect BufferPool from being accessed by several FrameWorkers at
706  *the same time during frame parallel decode.
707  *TODO(hkuang): Try to use atomic variable instead of locking the whole pool.
708  *
709  *Private data associated with the frame buffer callbacks.
710  *void *cb_priv;
711  *
712  *vpx_get_frame_buffer_cb_fn_t get_fb_cb;
713  *vpx_release_frame_buffer_cb_fn_t release_fb_cb;
714  */
715 
716 	struct RefCntBuffer_s frame_bufs[FRAME_BUFFERS];
717 
718 /*Frame buffers allocated internally by the codec.*/
719 	struct InternalFrameBufferList_s int_frame_buffers;
720 	unsigned long flags;
721 	spinlock_t lock;
722 
723 } BufferPool;
724 
725 #define lock_buffer_pool(pool, flags) \
726 		spin_lock_irqsave(&pool->lock, flags)
727 
728 #define unlock_buffer_pool(pool, flags) \
729 		spin_unlock_irqrestore(&pool->lock, flags)
730 
731 struct VP9_Common_s {
732 	enum vpx_color_space_t color_space;
733 	int width;
734 	int height;
735 	int display_width;
736 	int display_height;
737 	int last_width;
738 	int last_height;
739 
740 	int subsampling_x;
741 	int subsampling_y;
742 
743 	int use_highbitdepth;/*Marks if we need to use 16bit frame buffers.*/
744 
745 	struct PIC_BUFFER_CONFIG_s *frame_to_show;
746 	struct RefCntBuffer_s *prev_frame;
747 
748 	/*TODO(hkuang): Combine this with cur_buf in macroblockd.*/
749 	struct RefCntBuffer_s *cur_frame;
750 
751 	int ref_frame_map[REF_FRAMES]; /* maps fb_idx to reference slot */
752 
753 	/*Prepare ref_frame_map for the next frame.
754 	 *Only used in frame parallel decode.
755 	 */
756 	int next_ref_frame_map[REF_FRAMES];
757 
758 	/* TODO(jkoleszar): could expand active_ref_idx to 4,
759 	 *with 0 as intra, and roll new_fb_idx into it.
760 	 */
761 
762 	/*Each frame can reference REFS_PER_FRAME buffers*/
763 	struct RefBuffer_s frame_refs[REFS_PER_FRAME];
764 
765 	int prev_fb_idx;
766 	int new_fb_idx;
767 	int cur_fb_idx_mmu;
768 	/*last frame's frame type for motion search*/
769 	enum FRAME_TYPE last_frame_type;
770 	enum FRAME_TYPE frame_type;
771 
772 	int show_frame;
773 	int last_show_frame;
774 	int show_existing_frame;
775 
776 	/*Flag signaling that the frame is encoded using only INTRA modes.*/
777 	uint8_t intra_only;
778 	uint8_t last_intra_only;
779 
780 	int allow_high_precision_mv;
781 
782 	/*Flag signaling that the frame context should be reset to default
783 	 *values. 0 or 1 implies don't reset, 2 reset just the context
784 	 *specified in the  frame header, 3 reset all contexts.
785 	 */
786 	int reset_frame_context;
787 
788 	/*MBs, mb_rows/cols is in 16-pixel units; mi_rows/cols is in
789 	 *	MODE_INFO (8-pixel) units.
790 	 */
791 	int MBs;
792 	int mb_rows, mi_rows;
793 	int mb_cols, mi_cols;
794 	int mi_stride;
795 
796 	/*Whether to use previous frame's motion vectors for prediction.*/
797 	int use_prev_frame_mvs;
798 
799 	int refresh_frame_context;    /* Two state 0 = NO, 1 = YES */
800 
801 	int ref_frame_sign_bias[MAX_REF_FRAMES];    /* Two state 0, 1 */
802 
803 	/*struct loopfilter lf;*/
804 	/*struct segmentation seg;*/
805 
806 	/*TODO(hkuang):Remove this as it is the same as frame_parallel_decode*/
807 	/* in pbi.*/
808 	int frame_parallel_decode;  /* frame-based threading.*/
809 
810 	/*Context probabilities for reference frame prediction*/
811 	/*MV_REFERENCE_FRAME comp_fixed_ref;*/
812 	/*MV_REFERENCE_FRAME comp_var_ref[2];*/
813 	enum REFERENCE_MODE reference_mode;
814 
815 	/*FRAME_CONTEXT *fc; */ /* this frame entropy */
816 	/*FRAME_CONTEXT *frame_contexts; */  /*FRAME_CONTEXTS*/
817 	/*unsigned int  frame_context_idx; *//* Context to use/update */
818 	/*FRAME_COUNTS counts;*/
819 
820 	unsigned int current_video_frame;
821 	enum BITSTREAM_PROFILE profile;
822 
823 	enum vpx_bit_depth_t bit_depth;
824 
825 	int error_resilient_mode;
826 	int frame_parallel_decoding_mode;
827 
828 	int byte_alignment;
829 	int skip_loop_filter;
830 
831 	/*External BufferPool passed from outside.*/
832 	struct BufferPool_s *buffer_pool;
833 
834 	int above_context_alloc_cols;
835 
836 };
837 
838 static void set_canvas(struct VP9Decoder_s *pbi,
839 	struct PIC_BUFFER_CONFIG_s *pic_config);
840 static int prepare_display_buf(struct VP9Decoder_s *pbi,
841 					struct PIC_BUFFER_CONFIG_s *pic_config);
842 
843 static void fill_frame_info(struct VP9Decoder_s *pbi,
844 	struct PIC_BUFFER_CONFIG_s *frame,
845 	unsigned int framesize,
846 	unsigned int pts);
847 
get_frame_new_buffer(struct VP9_Common_s * cm)848 static struct PIC_BUFFER_CONFIG_s *get_frame_new_buffer(struct VP9_Common_s *cm)
849 {
850 	return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
851 }
852 
ref_cnt_fb(struct RefCntBuffer_s * bufs,int * idx,int new_idx)853 static void ref_cnt_fb(struct RefCntBuffer_s *bufs, int *idx, int new_idx)
854 {
855 	const int ref_index = *idx;
856 
857 	if (ref_index >= 0 && bufs[ref_index].ref_count > 0) {
858 		bufs[ref_index].ref_count--;
859 		/*pr_info("[MMU DEBUG 2] dec ref_count[%d] : %d\r\n",
860 		 *		ref_index, bufs[ref_index].ref_count);
861 		 */
862 	}
863 
864 	*idx = new_idx;
865 
866 	bufs[new_idx].ref_count++;
867 	/*pr_info("[MMU DEBUG 3] inc ref_count[%d] : %d\r\n",
868 	 *			new_idx, bufs[new_idx].ref_count);
869 	 */
870 }
871 
vp9_release_frame_buffer(struct vpx_codec_frame_buffer_s * fb)872 int vp9_release_frame_buffer(struct vpx_codec_frame_buffer_s *fb)
873 {
874 	struct InternalFrameBuffer_s *const int_fb =
875 				(struct InternalFrameBuffer_s *)fb->priv;
876 	if (int_fb)
877 		int_fb->in_use = 0;
878 	return 0;
879 }
880 
881 static int  compute_losless_comp_body_size(int width, int height,
882 				uint8_t is_bit_depth_10);
883 
setup_display_size(struct VP9_Common_s * cm,union param_u * params,int print_header_info)884 static void setup_display_size(struct VP9_Common_s *cm, union param_u *params,
885 						int print_header_info)
886 {
887 	cm->display_width = cm->width;
888 	cm->display_height = cm->height;
889 	if (params->p.display_size_present) {
890 		if (print_header_info)
891 			pr_info(" * 1-bit display_size_present read : 1\n");
892 		cm->display_width = params->p.display_width;
893 		cm->display_height = params->p.display_height;
894 		/*vp9_read_frame_size(rb, &cm->display_width,
895 		 *					&cm->display_height);
896 		 */
897 	} else {
898 		if (print_header_info)
899 			pr_info(" * 1-bit display_size_present read : 0\n");
900 	}
901 }
902 
903 
904 uint8_t print_header_info = 0;
905 
906 struct buff_s {
907 	u32 buf_start;
908 	u32 buf_size;
909 	u32 buf_end;
910 } buff_t;
911 
912 struct BuffInfo_s {
913 	u32 max_width;
914 	u32 max_height;
915 	u32 start_adr;
916 	u32 end_adr;
917 	struct buff_s ipp;
918 	struct buff_s sao_abv;
919 	struct buff_s sao_vb;
920 	struct buff_s short_term_rps;
921 	struct buff_s vps;
922 	struct buff_s sps;
923 	struct buff_s pps;
924 	struct buff_s sao_up;
925 	struct buff_s swap_buf;
926 	struct buff_s swap_buf2;
927 	struct buff_s scalelut;
928 	struct buff_s dblk_para;
929 	struct buff_s dblk_data;
930 	struct buff_s seg_map;
931 	struct buff_s mmu_vbh;
932 	struct buff_s cm_header;
933 	struct buff_s mpred_above;
934 #ifdef MV_USE_FIXED_BUF
935 	struct buff_s mpred_mv;
936 #endif
937 	struct buff_s rpm;
938 	struct buff_s lmem;
939 } BuffInfo_t;
940 #ifdef MULTI_INSTANCE_SUPPORT
941 #define DEC_RESULT_NONE             0
942 #define DEC_RESULT_DONE             1
943 #define DEC_RESULT_AGAIN            2
944 #define DEC_RESULT_CONFIG_PARAM     3
945 #define DEC_RESULT_ERROR            4
946 #define DEC_INIT_PICLIST			5
947 #define DEC_UNINIT_PICLIST			6
948 #define DEC_RESULT_GET_DATA         7
949 #define DEC_RESULT_GET_DATA_RETRY   8
950 #define DEC_RESULT_EOS              9
951 #define DEC_RESULT_FORCE_EXIT       10
952 #define DEC_V4L2_CONTINUE_DECODING  18
953 
954 #define DEC_S1_RESULT_NONE          0
955 #define DEC_S1_RESULT_DONE          1
956 #define DEC_S1_RESULT_FORCE_EXIT       2
957 #define DEC_S1_RESULT_TEST_TRIGGER_DONE	0xf0
958 
959 #ifdef FB_DECODING_TEST_SCHEDULE
960 #define TEST_SET_NONE        0
961 #define TEST_SET_PIC_DONE    1
962 #define TEST_SET_S2_DONE     2
963 #endif
964 
965 static void vp9_work(struct work_struct *work);
966 #endif
967 struct loop_filter_info_n;
968 struct loopfilter;
969 struct segmentation;
970 
971 #ifdef SUPPORT_FB_DECODING
972 static void mpred_process(struct VP9Decoder_s *pbi);
973 static void vp9_s1_work(struct work_struct *work);
974 
975 struct stage_buf_s {
976 	int index;
977 	unsigned short rpm[RPM_END - RPM_BEGIN];
978 };
979 
980 static unsigned int not_run2_ready[MAX_DECODE_INSTANCE_NUM];
981 
982 static unsigned int run2_count[MAX_DECODE_INSTANCE_NUM];
983 
984 #ifdef FB_DECODING_TEST_SCHEDULE
985 u32 stage_buf_num; /* = 16;*/
986 #else
987 u32 stage_buf_num;
988 #endif
989 #endif
990 
991 struct VP9Decoder_s {
992 #ifdef MULTI_INSTANCE_SUPPORT
993 	unsigned char index;
994 
995 	struct device *cma_dev;
996 	struct platform_device *platform_dev;
997 	void (*vdec_cb)(struct vdec_s *, void *);
998 	void *vdec_cb_arg;
999 	struct vframe_chunk_s *chunk;
1000 	int dec_result;
1001 	struct work_struct work;
1002 	struct work_struct recycle_mmu_work;
1003 	struct work_struct set_clk_work;
1004 	u32 start_shift_bytes;
1005 
1006 	struct BuffInfo_s work_space_buf_store;
1007 	unsigned long buf_start;
1008 	u32 buf_size;
1009 	u32 cma_alloc_count;
1010 	unsigned long cma_alloc_addr;
1011 	uint8_t eos;
1012 	unsigned long int start_process_time;
1013 	unsigned last_lcu_idx;
1014 	int decode_timeout_count;
1015 	unsigned timeout_num;
1016 	int save_buffer_mode;
1017 
1018 	int double_write_mode;
1019 #endif
1020 	long used_4k_num;
1021 
1022 	unsigned char m_ins_flag;
1023 	char *provider_name;
1024 	union param_u param;
1025 	int frame_count;
1026 	int pic_count;
1027 	u32 stat;
1028 	struct timer_list timer;
1029 	u32 frame_dur;
1030 	u32 frame_ar;
1031 	int fatal_error;
1032 	uint8_t init_flag;
1033 	uint8_t first_sc_checked;
1034 	uint8_t process_busy;
1035 #define PROC_STATE_INIT			0
1036 #define PROC_STATE_DECODESLICE	1
1037 #define PROC_STATE_SENDAGAIN	2
1038 	uint8_t process_state;
1039 	u32 ucode_pause_pos;
1040 
1041 	int show_frame_num;
1042 	struct buff_s mc_buf_spec;
1043 	struct dec_sysinfo vvp9_amstream_dec_info;
1044 	void *rpm_addr;
1045 	void *lmem_addr;
1046 	dma_addr_t rpm_phy_addr;
1047 	dma_addr_t lmem_phy_addr;
1048 	unsigned short *lmem_ptr;
1049 	unsigned short *debug_ptr;
1050 
1051 	void *prob_buffer_addr;
1052 	void *count_buffer_addr;
1053 	dma_addr_t prob_buffer_phy_addr;
1054 	dma_addr_t count_buffer_phy_addr;
1055 
1056 	void *frame_mmu_map_addr;
1057 	dma_addr_t frame_mmu_map_phy_addr;
1058 
1059 	unsigned int use_cma_flag;
1060 
1061 	struct BUF_s m_BUF[MAX_BUF_NUM];
1062 	struct MVBUF_s m_mv_BUF[MV_BUFFER_NUM];
1063 	u32 used_buf_num;
1064 	DECLARE_KFIFO(newframe_q, struct vframe_s *, VF_POOL_SIZE);
1065 	DECLARE_KFIFO(display_q, struct vframe_s *, VF_POOL_SIZE);
1066 	DECLARE_KFIFO(pending_q, struct vframe_s *, VF_POOL_SIZE);
1067 	struct vframe_s vfpool[VF_POOL_SIZE];
1068 	u32 vf_pre_count;
1069 	u32 vf_get_count;
1070 	u32 vf_put_count;
1071 	int buf_num;
1072 	int pic_num;
1073 	int lcu_size_log2;
1074 	unsigned int losless_comp_body_size;
1075 
1076 	u32 video_signal_type;
1077 
1078 	int pts_mode;
1079 	int last_lookup_pts;
1080 	int last_pts;
1081 	u64 last_lookup_pts_us64;
1082 	u64 last_pts_us64;
1083 	u64 shift_byte_count;
1084 
1085 	u32 pts_unstable;
1086 	u32 frame_cnt_window;
1087 	u32 pts1, pts2;
1088 	u32 last_duration;
1089 	u32 duration_from_pts_done;
1090 	bool vp9_first_pts_ready;
1091 
1092 	u32 shift_byte_count_lo;
1093 	u32 shift_byte_count_hi;
1094 	int pts_mode_switching_count;
1095 	int pts_mode_recovery_count;
1096 
1097 	bool get_frame_dur;
1098 	u32 saved_resolution;
1099 
1100 	/**/
1101 	struct VP9_Common_s common;
1102 	struct RefCntBuffer_s *cur_buf;
1103 	int refresh_frame_flags;
1104 	uint8_t need_resync;
1105 	uint8_t hold_ref_buf;
1106 	uint8_t ready_for_new_data;
1107 	struct BufferPool_s vp9_buffer_pool;
1108 
1109 	struct BuffInfo_s *work_space_buf;
1110 
1111 	struct buff_s *mc_buf;
1112 
1113 	unsigned int frame_width;
1114 	unsigned int frame_height;
1115 
1116 	unsigned short *rpm_ptr;
1117 	int     init_pic_w;
1118 	int     init_pic_h;
1119 	int     lcu_total;
1120 	int     lcu_size;
1121 
1122 	int     slice_type;
1123 
1124 	int skip_flag;
1125 	int decode_idx;
1126 	int slice_idx;
1127 	uint8_t has_keyframe;
1128 	uint8_t wait_buf;
1129 	uint8_t error_flag;
1130 
1131 	/* bit 0, for decoding; bit 1, for displaying */
1132 	uint8_t ignore_bufmgr_error;
1133 	int PB_skip_mode;
1134 	int PB_skip_count_after_decoding;
1135 	/*hw*/
1136 
1137 	/*lf*/
1138 	int default_filt_lvl;
1139 	struct loop_filter_info_n *lfi;
1140 	struct loopfilter *lf;
1141 	struct segmentation *seg_4lf;
1142 	/**/
1143 	struct vdec_info *gvs;
1144 
1145 	u32 pre_stream_offset;
1146 
1147 	unsigned int dec_status;
1148 	u32 last_put_idx;
1149 	int new_frame_displayed;
1150 	void *mmu_box;
1151 	void *bmmu_box;
1152 	int mmu_enable;
1153 	struct vframe_master_display_colour_s vf_dp;
1154 	struct firmware_s *fw;
1155 	int max_pic_w;
1156 	int max_pic_h;
1157 #ifdef SUPPORT_FB_DECODING
1158 	int dec_s1_result;
1159 	int s1_test_cmd;
1160 	struct work_struct s1_work;
1161 	int used_stage_buf_num;
1162 	int s1_pos;
1163 	int s2_pos;
1164 	void *stage_mmu_map_addr;
1165 	dma_addr_t stage_mmu_map_phy_addr;
1166 	struct stage_buf_s *s1_buf;
1167 	struct stage_buf_s *s2_buf;
1168 	struct stage_buf_s *stage_bufs
1169 		[STAGE_MAX_BUFFERS];
1170 	unsigned char run2_busy;
1171 
1172 	int s1_mv_buf_index;
1173 	int s1_mv_buf_index_pre;
1174 	int s1_mv_buf_index_pre_pre;
1175 	unsigned long s1_mpred_mv_wr_start_addr;
1176 	unsigned long s1_mpred_mv_wr_start_addr_pre;
1177 	unsigned short s1_intra_only;
1178 	unsigned short s1_frame_type;
1179 	unsigned short s1_width;
1180 	unsigned short s1_height;
1181 	unsigned short s1_last_show_frame;
1182 	union param_u s1_param;
1183 	u8 back_not_run_ready;
1184 #endif
1185 	int need_cache_size;
1186 	u64 sc_start_time;
1187 	bool postproc_done;
1188 	int low_latency_flag;
1189 	bool no_head;
1190 	bool pic_list_init_done;
1191 	bool pic_list_init_done2;
1192 	bool is_used_v4l;
1193 	void *v4l2_ctx;
1194 	bool v4l_params_parsed;
1195 	int frameinfo_enable;
1196 	struct vframe_qos_s vframe_qos;
1197 	u32 mem_map_mode;
1198 	u32 dynamic_buf_num_margin;
1199 	struct vframe_s vframe_dummy;
1200 	unsigned int res_ch_flag;
1201 	/*struct VP9Decoder_s vp9_decoder;*/
1202 	union param_u vp9_param;
1203 	int sidebind_type;
1204 	int sidebind_channel_id;
1205 };
1206 
vp9_print(struct VP9Decoder_s * pbi,int flag,const char * fmt,...)1207 static int vp9_print(struct VP9Decoder_s *pbi,
1208 	int flag, const char *fmt, ...)
1209 {
1210 #define HEVC_PRINT_BUF		256
1211 	unsigned char buf[HEVC_PRINT_BUF];
1212 	int len = 0;
1213 
1214 	if (pbi == NULL ||
1215 		(flag == 0) ||
1216 		(debug & flag)) {
1217 		va_list args;
1218 
1219 		va_start(args, fmt);
1220 		if (pbi)
1221 			len = sprintf(buf, "[%d]", pbi->index);
1222 		vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
1223 		pr_debug("%s", buf);
1224 		va_end(args);
1225 	}
1226 	return 0;
1227 }
1228 
is_oversize(int w,int h)1229 static int is_oversize(int w, int h)
1230 {
1231 	int max = (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)?
1232 		MAX_SIZE_8K : MAX_SIZE_4K;
1233 
1234 	if (w <= 0 || h <= 0)
1235 		return true;
1236 
1237 	if (h != 0 && (w > max / h))
1238 		return true;
1239 
1240 	return false;
1241 }
1242 
1243 static int v4l_alloc_and_config_pic(struct VP9Decoder_s *pbi,
1244 	struct PIC_BUFFER_CONFIG_s *pic);
1245 
resize_context_buffers(struct VP9Decoder_s * pbi,struct VP9_Common_s * cm,int width,int height)1246 static void resize_context_buffers(struct VP9Decoder_s *pbi,
1247 	struct VP9_Common_s *cm, int width, int height)
1248 {
1249 	if (cm->width != width || cm->height != height) {
1250 		/* to do ..*/
1251 		if (pbi != NULL) {
1252 			pbi->vp9_first_pts_ready = 0;
1253 			pbi->duration_from_pts_done = 0;
1254 		}
1255 		pr_info("%s (%d,%d)=>(%d,%d)\r\n", __func__, cm->width,
1256 			cm->height, width, height);
1257 		cm->width = width;
1258 		cm->height = height;
1259 	}
1260 	/*
1261 	 *if (cm->cur_frame->mvs == NULL ||
1262 	 *	cm->mi_rows > cm->cur_frame->mi_rows ||
1263 	 *	cm->mi_cols > cm->cur_frame->mi_cols) {
1264 	 *	resize_mv_buffer(cm);
1265 	 *}
1266 	 */
1267 }
1268 
valid_ref_frame_size(int ref_width,int ref_height,int this_width,int this_height)1269 static int valid_ref_frame_size(int ref_width, int ref_height,
1270 				int this_width, int this_height) {
1271 	return 2 * this_width >= ref_width &&
1272 		2 * this_height >= ref_height &&
1273 		this_width <= 16 * ref_width &&
1274 		this_height <= 16 * ref_height;
1275 }
1276 
1277 /*
1278  *static int valid_ref_frame_img_fmt(enum vpx_bit_depth_t ref_bit_depth,
1279  *					int ref_xss, int ref_yss,
1280  *					enum vpx_bit_depth_t this_bit_depth,
1281  *					int this_xss, int this_yss) {
1282  *	return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1283  *		ref_yss == this_yss;
1284  *}
1285  */
1286 
1287 
setup_frame_size(struct VP9Decoder_s * pbi,struct VP9_Common_s * cm,union param_u * params,unsigned int * mmu_index_adr,int print_header_info)1288 static int setup_frame_size(
1289 		struct VP9Decoder_s *pbi,
1290 		struct VP9_Common_s *cm, union param_u *params,
1291 		unsigned int *mmu_index_adr,
1292 		int print_header_info) {
1293 	int width, height;
1294 	struct BufferPool_s * const pool = cm->buffer_pool;
1295 	struct PIC_BUFFER_CONFIG_s *ybf;
1296 	int ret = 0;
1297 
1298 	width = params->p.width;
1299 	height = params->p.height;
1300 	if (is_oversize(width, height)) {
1301 		vp9_print(pbi, 0, "%s, Error: Invalid frame size\n", __func__);
1302 		return -1;
1303 	}
1304 
1305 	/*vp9_read_frame_size(rb, &width, &height);*/
1306 	if (print_header_info)
1307 		pr_info(" * 16-bits w read : %d (width : %d)\n", width, height);
1308 	if (print_header_info)
1309 		pr_info
1310 		(" * 16-bits h read : %d (height : %d)\n", width, height);
1311 
1312 	WRITE_VREG(HEVC_PARSER_PICTURE_SIZE, (height << 16) | width);
1313 #ifdef VP9_10B_HED_FB
1314 	WRITE_VREG(HEVC_ASSIST_PIC_SIZE_FB_READ, (height << 16) | width);
1315 #endif
1316 	if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
1317 		ret = vp9_alloc_mmu(pbi,
1318 			cm->new_fb_idx,
1319 			params->p.width,
1320 			params->p.height,
1321 			params->p.bit_depth,
1322 			mmu_index_adr);
1323 		if (ret != 0) {
1324 			pr_err("can't alloc need mmu1,idx %d ret =%d\n",
1325 				cm->new_fb_idx,
1326 				ret);
1327 			return ret;
1328 		}
1329 		cm->cur_fb_idx_mmu = cm->new_fb_idx;
1330 	}
1331 
1332 	resize_context_buffers(pbi, cm, width, height);
1333 	setup_display_size(cm, params, print_header_info);
1334 #if 0
1335 	lock_buffer_pool(pool);
1336 	if (vp9_realloc_frame_buffer(
1337 		get_frame_new_buffer(cm), cm->width, cm->height,
1338 		cm->subsampling_x, cm->subsampling_y,
1339 #if CONFIG_VP9_HIGHBITDEPTH
1340 		cm->use_highbitdepth,
1341 #endif
1342 		VP9_DEC_BORDER_IN_PIXELS,
1343 		cm->byte_alignment,
1344 		&pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer,
1345 		pool->get_fb_cb, pool->cb_priv)) {
1346 		unlock_buffer_pool(pool);
1347 		vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1348 			"Failed to allocate frame buffer");
1349 	}
1350 	unlock_buffer_pool(pool);
1351 #else
1352 	/* porting */
1353 	ybf = get_frame_new_buffer(cm);
1354 	if (!ybf)
1355 		return -1;
1356 
1357 	ybf->y_crop_width = width;
1358 	ybf->y_crop_height = height;
1359 	ybf->bit_depth = params->p.bit_depth;
1360 #endif
1361 	pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1362 	pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1363 	pool->frame_bufs[cm->new_fb_idx].buf.bit_depth =
1364 						(unsigned int)cm->bit_depth;
1365 	pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1366 	return ret;
1367 }
1368 
setup_frame_size_with_refs(struct VP9Decoder_s * pbi,struct VP9_Common_s * cm,union param_u * params,unsigned int * mmu_index_adr,int print_header_info)1369 static int setup_frame_size_with_refs(
1370 		struct VP9Decoder_s *pbi,
1371 		struct VP9_Common_s *cm,
1372 		union param_u *params,
1373 		unsigned int *mmu_index_adr,
1374 		int print_header_info) {
1375 
1376 	int width, height;
1377 	int found = 0, i;
1378 	int has_valid_ref_frame = 0;
1379 	struct PIC_BUFFER_CONFIG_s *ybf;
1380 	struct BufferPool_s * const pool = cm->buffer_pool;
1381 	int ret = 0;
1382 
1383 	for (i = 0; i < REFS_PER_FRAME; ++i) {
1384 		if ((params->p.same_frame_size >>
1385 				(REFS_PER_FRAME - i - 1)) & 0x1) {
1386 			struct PIC_BUFFER_CONFIG_s *const buf =
1387 							cm->frame_refs[i].buf;
1388 			/*if (print_header_info)
1389 			 *	pr_info
1390 			 *	("1-bit same_frame_size[%d] read : 1\n", i);
1391 			 */
1392 				width = buf->y_crop_width;
1393 				height = buf->y_crop_height;
1394 			/*if (print_header_info)
1395 			 *	pr_info
1396 			 *	(" - same_frame_size width : %d\n", width);
1397 			 */
1398 			/*if (print_header_info)
1399 			 *	pr_info
1400 			 *	(" - same_frame_size height : %d\n", height);
1401 			 */
1402 			found = 1;
1403 			break;
1404 		} else {
1405 			/*if (print_header_info)
1406 			 *	pr_info
1407 			 *	("1-bit same_frame_size[%d] read : 0\n", i);
1408 			 */
1409 		}
1410 	}
1411 
1412 	if (!found) {
1413 		/*vp9_read_frame_size(rb, &width, &height);*/
1414 		width = params->p.width;
1415 		height = params->p.height;
1416 		/*if (print_header_info)
1417 		 *	pr_info
1418 		 *	(" * 16-bits w read : %d (width : %d)\n",
1419 		 *		width, height);
1420 		 *if (print_header_info)
1421 		 *	pr_info
1422 		 *	(" * 16-bits h read : %d (height : %d)\n",
1423 		 *		width, height);
1424 		 */
1425 	}
1426 
1427 	if (is_oversize(width, height)) {
1428 		vp9_print(pbi, 0, "%s, Error: Invalid frame size\n", __func__);
1429 		return -1;
1430 	}
1431 
1432 	params->p.width = width;
1433 	params->p.height = height;
1434 
1435 	WRITE_VREG(HEVC_PARSER_PICTURE_SIZE, (height << 16) | width);
1436 	if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
1437 		/*if(cm->prev_fb_idx >= 0) release_unused_4k(cm->prev_fb_idx);
1438 		 *cm->prev_fb_idx = cm->new_fb_idx;
1439 		 */
1440 	/*	pr_info
1441 	 *	("[DEBUG DEBUG]Before alloc_mmu,
1442 	 *	prev_fb_idx : %d, new_fb_idx : %d\r\n",
1443 	 *	cm->prev_fb_idx, cm->new_fb_idx);
1444 	 */
1445 		ret = vp9_alloc_mmu(pbi, cm->new_fb_idx,
1446 				params->p.width, params->p.height,
1447 		params->p.bit_depth, mmu_index_adr);
1448 		if (ret != 0) {
1449 			pr_err("can't alloc need mmu,idx %d\r\n",
1450 				cm->new_fb_idx);
1451 			return ret;
1452 		}
1453 		cm->cur_fb_idx_mmu = cm->new_fb_idx;
1454 	}
1455 
1456 	/*Check to make sure at least one of frames that this frame references
1457 	 *has valid dimensions.
1458 	 */
1459 	for (i = 0; i < REFS_PER_FRAME; ++i) {
1460 		struct RefBuffer_s * const ref_frame = &cm->frame_refs[i];
1461 
1462 		has_valid_ref_frame |=
1463 			valid_ref_frame_size(ref_frame->buf->y_crop_width,
1464 			ref_frame->buf->y_crop_height,
1465 			width, height);
1466 	}
1467 	if (!has_valid_ref_frame) {
1468 		pr_err("Error: Referenced frame has invalid size\r\n");
1469 		return -1;
1470 	}
1471 #if 0
1472 	for (i = 0; i < REFS_PER_FRAME; ++i) {
1473 			struct RefBuffer_s * const ref_frame =
1474 							&cm->frame_refs[i];
1475 			if (!valid_ref_frame_img_fmt(
1476 				ref_frame->buf->bit_depth,
1477 				ref_frame->buf->subsampling_x,
1478 				ref_frame->buf->subsampling_y,
1479 				cm->bit_depth,
1480 				cm->subsampling_x,
1481 				cm->subsampling_y))
1482 				pr_err
1483 				("Referenced frame incompatible color fmt\r\n");
1484 				return -1;
1485 	}
1486 #endif
1487 	resize_context_buffers(pbi, cm, width, height);
1488 	setup_display_size(cm, params, print_header_info);
1489 
1490 #if 0
1491 	lock_buffer_pool(pool);
1492 	if (vp9_realloc_frame_buffer(
1493 		get_frame_new_buffer(cm), cm->width, cm->height,
1494 		cm->subsampling_x, cm->subsampling_y,
1495 #if CONFIG_VP9_HIGHBITDEPTH
1496 		cm->use_highbitdepth,
1497 #endif
1498 		VP9_DEC_BORDER_IN_PIXELS,
1499 		cm->byte_alignment,
1500 		&pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer,
1501 		pool->get_fb_cb,
1502 		pool->cb_priv)) {
1503 		unlock_buffer_pool(pool);
1504 		vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1505 		"Failed to allocate frame buffer");
1506 	}
1507 	unlock_buffer_pool(pool);
1508 #else
1509 	/* porting */
1510 	ybf = get_frame_new_buffer(cm);
1511 	if (!ybf)
1512 		return -1;
1513 
1514 	ybf->y_crop_width = width;
1515 	ybf->y_crop_height = height;
1516 	ybf->bit_depth = params->p.bit_depth;
1517 #endif
1518 	pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1519 	pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1520 	pool->frame_bufs[cm->new_fb_idx].buf.bit_depth =
1521 						(unsigned int)cm->bit_depth;
1522 	pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1523 	return ret;
1524 }
1525 
close_to(int a,int b,int m)1526 static inline bool close_to(int a, int b, int m)
1527 {
1528 	return (abs(a - b) < m) ? true : false;
1529 }
1530 
1531 #ifdef MULTI_INSTANCE_SUPPORT
vp9_print_cont(struct VP9Decoder_s * pbi,int flag,const char * fmt,...)1532 static int vp9_print_cont(struct VP9Decoder_s *pbi,
1533 	int flag, const char *fmt, ...)
1534 {
1535 	unsigned char buf[HEVC_PRINT_BUF];
1536 	int len = 0;
1537 
1538 	if (pbi == NULL ||
1539 		(flag == 0) ||
1540 		(debug & flag)) {
1541 		va_list args;
1542 
1543 		va_start(args, fmt);
1544 		vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
1545 		pr_debug("%s", buf);
1546 		va_end(args);
1547 	}
1548 	return 0;
1549 }
1550 
trigger_schedule(struct VP9Decoder_s * pbi)1551 static void trigger_schedule(struct VP9Decoder_s *pbi)
1552 {
1553 	if (pbi->is_used_v4l) {
1554 		struct aml_vcodec_ctx *ctx =
1555 			(struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
1556 
1557 		if (ctx->param_sets_from_ucode &&
1558 			!pbi->v4l_params_parsed)
1559 			vdec_v4l_write_frame_sync(ctx);
1560 	}
1561 
1562 	if (pbi->vdec_cb)
1563 		pbi->vdec_cb(hw_to_vdec(pbi), pbi->vdec_cb_arg);
1564 }
1565 
reset_process_time(struct VP9Decoder_s * pbi)1566 static void reset_process_time(struct VP9Decoder_s *pbi)
1567 {
1568 	if (pbi->start_process_time) {
1569 		unsigned process_time =
1570 			1000 * (jiffies - pbi->start_process_time) / HZ;
1571 		pbi->start_process_time = 0;
1572 		if (process_time > max_process_time[pbi->index])
1573 			max_process_time[pbi->index] = process_time;
1574 	}
1575 }
1576 
start_process_time(struct VP9Decoder_s * pbi)1577 static void start_process_time(struct VP9Decoder_s *pbi)
1578 {
1579 	pbi->start_process_time = jiffies;
1580 	pbi->decode_timeout_count = 0;
1581 	pbi->last_lcu_idx = 0;
1582 }
1583 
timeout_process(struct VP9Decoder_s * pbi)1584 static void timeout_process(struct VP9Decoder_s *pbi)
1585 {
1586 	pbi->timeout_num++;
1587 	amhevc_stop();
1588 	vp9_print(pbi,
1589 		0, "%s decoder timeout\n", __func__);
1590 
1591 	pbi->dec_result = DEC_RESULT_DONE;
1592 	reset_process_time(pbi);
1593 	vdec_schedule_work(&pbi->work);
1594 }
1595 
get_valid_double_write_mode(struct VP9Decoder_s * pbi)1596 static u32 get_valid_double_write_mode(struct VP9Decoder_s *pbi)
1597 {
1598 	return ((double_write_mode & 0x80000000) == 0) ?
1599 		pbi->double_write_mode :
1600 		(double_write_mode & 0x7fffffff);
1601 }
1602 
v4l_parser_get_double_write_mode(struct VP9Decoder_s * pbi)1603 static int v4l_parser_get_double_write_mode(struct VP9Decoder_s *pbi)
1604 {
1605 	u32 valid_dw_mode = get_valid_double_write_mode(pbi);
1606 	u32 dw;
1607 	int w, h;
1608 
1609 	/* mask for supporting double write value bigger than 0x100 */
1610 	if (valid_dw_mode & 0xffffff00) {
1611 		w = pbi->frame_width;
1612 		h = pbi->frame_height;
1613 
1614 		dw = 0x1; /*1:1*/
1615 		switch (valid_dw_mode) {
1616 		case 0x100:
1617 			if (w > 1920 && h > 1088)
1618 				dw = 0x4; /*1:2*/
1619 			break;
1620 		case 0x200:
1621 			if (w > 1920 && h > 1088)
1622 				dw = 0x2; /*1:4*/
1623 			break;
1624 		case 0x300:
1625 			if (w > 1280 && h > 720)
1626 				dw = 0x4; /*1:2*/
1627 			break;
1628 		default:
1629 			break;
1630 		}
1631 		return dw;
1632 	}
1633 
1634 	return valid_dw_mode;
1635 }
1636 
1637 
get_double_write_mode(struct VP9Decoder_s * pbi)1638 static int get_double_write_mode(struct VP9Decoder_s *pbi)
1639 {
1640 	u32 valid_dw_mode = get_valid_double_write_mode(pbi);
1641 	u32 dw;
1642 	int w, h;
1643 	struct VP9_Common_s *cm = &pbi->common;
1644 	struct PIC_BUFFER_CONFIG_s *cur_pic_config;
1645 
1646 	/* mask for supporting double write value bigger than 0x100 */
1647 	if (valid_dw_mode & 0xffffff00) {
1648 		if (!cm->cur_frame)
1649 			return 1;/*no valid frame,*/
1650 		cur_pic_config = &cm->cur_frame->buf;
1651 		w = cur_pic_config->y_crop_width;
1652 		h = cur_pic_config->y_crop_height;
1653 
1654 		dw = 0x1; /*1:1*/
1655 		switch (valid_dw_mode) {
1656 		case 0x100:
1657 			if (w > 1920 && h > 1088)
1658 				dw = 0x4; /*1:2*/
1659 			break;
1660 		case 0x200:
1661 			if (w > 1920 && h > 1088)
1662 				dw = 0x2; /*1:4*/
1663 			break;
1664 		case 0x300:
1665 			if (w > 1280 && h > 720)
1666 				dw = 0x4; /*1:2*/
1667 			break;
1668 		default:
1669 			break;
1670 		}
1671 		return dw;
1672 	}
1673 
1674 	return valid_dw_mode;
1675 }
1676 
1677 /* for double write buf alloc */
get_double_write_mode_init(struct VP9Decoder_s * pbi)1678 static int get_double_write_mode_init(struct VP9Decoder_s *pbi)
1679 {
1680 	u32 valid_dw_mode = get_valid_double_write_mode(pbi);
1681 	u32 dw;
1682 	int w = pbi->init_pic_w;
1683 	int h = pbi->init_pic_h;
1684 
1685 	dw = 0x1; /*1:1*/
1686 	switch (valid_dw_mode) {
1687 	case 0x100:
1688 		if (w > 1920 && h > 1088)
1689 			dw = 0x4; /*1:2*/
1690 		break;
1691 	case 0x200:
1692 		if (w > 1920 && h > 1088)
1693 			dw = 0x2; /*1:4*/
1694 		break;
1695 	case 0x300:
1696 		if (w > 1280 && h > 720)
1697 			dw = 0x4; /*1:2*/
1698 		break;
1699 	default:
1700 		dw = valid_dw_mode;
1701 		break;
1702 	}
1703 	return dw;
1704 }
1705 #endif
1706 
get_double_write_ratio(struct VP9Decoder_s * pbi,int dw_mode)1707 static int get_double_write_ratio(struct VP9Decoder_s *pbi,
1708 	int dw_mode)
1709 {
1710 	int ratio = 1;
1711 	if ((dw_mode == 2) ||
1712 			(dw_mode == 3))
1713 		ratio = 4;
1714 	else if (dw_mode == 4)
1715 		ratio = 2;
1716 	return ratio;
1717 }
1718 
1719 //#define	MAX_4K_NUM		0x1200
1720 
vp9_alloc_mmu(struct VP9Decoder_s * pbi,int cur_buf_idx,int pic_width,int pic_height,unsigned short bit_depth,unsigned int * mmu_index_adr)1721 int vp9_alloc_mmu(
1722 	struct VP9Decoder_s *pbi,
1723 	int cur_buf_idx,
1724 	int pic_width,
1725 	int pic_height,
1726 	unsigned short bit_depth,
1727 	unsigned int *mmu_index_adr)
1728 {
1729 	int bit_depth_10 = (bit_depth == VPX_BITS_10);
1730 	int picture_size;
1731 	int cur_mmu_4k_number, max_frame_num;
1732 	if (!pbi->mmu_box) {
1733 		pr_err("error no mmu box!\n");
1734 		return -1;
1735 	}
1736 	if (get_double_write_mode(pbi) == 0x10)
1737 		return 0;
1738 	if (bit_depth >= VPX_BITS_12) {
1739 		pbi->fatal_error = DECODER_FATAL_ERROR_SIZE_OVERFLOW;
1740 		pr_err("fatal_error, un support bit depth 12!\n\n");
1741 		return -1;
1742 	}
1743 	picture_size = compute_losless_comp_body_size(pic_width, pic_height,
1744 				   bit_depth_10);
1745 	cur_mmu_4k_number = ((picture_size + (1 << 12) - 1) >> 12);
1746 
1747 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
1748 		max_frame_num = MAX_FRAME_8K_NUM;
1749 	else
1750 		max_frame_num = MAX_FRAME_4K_NUM;
1751 
1752 	if (cur_mmu_4k_number > max_frame_num) {
1753 		pr_err("over max !! cur_mmu_4k_number 0x%x width %d height %d\n",
1754 			cur_mmu_4k_number, pic_width, pic_height);
1755 		return -1;
1756 	}
1757 	return decoder_mmu_box_alloc_idx(
1758 		pbi->mmu_box,
1759 		cur_buf_idx,
1760 		cur_mmu_4k_number,
1761 		mmu_index_adr);
1762 }
1763 
1764 
1765 #ifndef MV_USE_FIXED_BUF
dealloc_mv_bufs(struct VP9Decoder_s * pbi)1766 static void dealloc_mv_bufs(struct VP9Decoder_s *pbi)
1767 {
1768 	int i;
1769 	for (i = 0; i < MV_BUFFER_NUM; i++) {
1770 		if (pbi->m_mv_BUF[i].start_adr) {
1771 			if (debug)
1772 				pr_info(
1773 				"dealloc mv buf(%d) adr %ld size 0x%x used_flag %d\n",
1774 				i, pbi->m_mv_BUF[i].start_adr,
1775 				pbi->m_mv_BUF[i].size,
1776 				pbi->m_mv_BUF[i].used_flag);
1777 			decoder_bmmu_box_free_idx(
1778 				pbi->bmmu_box,
1779 				MV_BUFFER_IDX(i));
1780 			pbi->m_mv_BUF[i].start_adr = 0;
1781 			pbi->m_mv_BUF[i].size = 0;
1782 			pbi->m_mv_BUF[i].used_flag = 0;
1783 		}
1784 	}
1785 }
1786 
alloc_mv_buf(struct VP9Decoder_s * pbi,int i,int size)1787 static int alloc_mv_buf(struct VP9Decoder_s *pbi,
1788 	int i, int size)
1789 {
1790 	int ret = 0;
1791 
1792 	if (pbi->m_mv_BUF[i].start_adr &&
1793 		size > pbi->m_mv_BUF[i].size) {
1794 		dealloc_mv_bufs(pbi);
1795 	} else if (pbi->m_mv_BUF[i].start_adr)
1796 		return 0;
1797 
1798 	if (decoder_bmmu_box_alloc_buf_phy
1799 		(pbi->bmmu_box,
1800 		MV_BUFFER_IDX(i), size,
1801 		DRIVER_NAME,
1802 		&pbi->m_mv_BUF[i].start_adr) < 0) {
1803 		pbi->m_mv_BUF[i].start_adr = 0;
1804 		ret = -1;
1805 	} else {
1806 		pbi->m_mv_BUF[i].size = size;
1807 		pbi->m_mv_BUF[i].used_flag = 0;
1808 		ret = 0;
1809 		if (debug) {
1810 			pr_info(
1811 			"MV Buffer %d: start_adr %p size %x\n",
1812 			i,
1813 			(void *)pbi->m_mv_BUF[i].start_adr,
1814 			pbi->m_mv_BUF[i].size);
1815 		}
1816 	}
1817 	return ret;
1818 }
1819 
init_mv_buf_list(struct VP9Decoder_s * pbi)1820 static int init_mv_buf_list(struct VP9Decoder_s *pbi)
1821 {
1822 	int i;
1823 	int ret = 0;
1824 	int count = MV_BUFFER_NUM;
1825 	int pic_width = pbi->init_pic_w;
1826 	int pic_height = pbi->init_pic_h;
1827 	int lcu_size = 64; /*fixed 64*/
1828 	int pic_width_64 = (pic_width + 63) & (~0x3f);
1829 	int pic_height_32 = (pic_height + 31) & (~0x1f);
1830 	int pic_width_lcu  = (pic_width_64 % lcu_size) ?
1831 				pic_width_64 / lcu_size  + 1
1832 				: pic_width_64 / lcu_size;
1833 	int pic_height_lcu = (pic_height_32 % lcu_size) ?
1834 				pic_height_32 / lcu_size + 1
1835 				: pic_height_32 / lcu_size;
1836 	int lcu_total       = pic_width_lcu * pic_height_lcu;
1837 	int size = ((lcu_total * MV_MEM_UNIT) + 0xffff) &
1838 		(~0xffff);
1839 	if (mv_buf_margin > 0)
1840 		count = REF_FRAMES + mv_buf_margin;
1841 
1842 	if (pbi->init_pic_w > 2048 && pbi->init_pic_h > 1088)
1843 		count = REF_FRAMES_4K + mv_buf_margin;
1844 
1845 	if (debug) {
1846 		pr_info("%s w:%d, h:%d, count: %d\n",
1847 		__func__, pbi->init_pic_w, pbi->init_pic_h, count);
1848 	}
1849 
1850 	for (i = 0;
1851 		i < count && i < MV_BUFFER_NUM; i++) {
1852 		if (alloc_mv_buf(pbi, i, size) < 0) {
1853 			ret = -1;
1854 			break;
1855 		}
1856 	}
1857 	return ret;
1858 }
1859 
get_mv_buf(struct VP9Decoder_s * pbi,int * mv_buf_index,unsigned long * mpred_mv_wr_start_addr)1860 static int get_mv_buf(struct VP9Decoder_s *pbi,
1861 		int *mv_buf_index,
1862 		unsigned long *mpred_mv_wr_start_addr)
1863 {
1864 	int i;
1865 	int ret = -1;
1866 	for (i = 0; i < MV_BUFFER_NUM; i++) {
1867 		if (pbi->m_mv_BUF[i].start_adr &&
1868 			pbi->m_mv_BUF[i].used_flag == 0) {
1869 			pbi->m_mv_BUF[i].used_flag = 1;
1870 			ret = i;
1871 			break;
1872 		}
1873 	}
1874 
1875 	if (ret >= 0) {
1876 		*mv_buf_index = ret;
1877 		*mpred_mv_wr_start_addr =
1878 			(pbi->m_mv_BUF[ret].start_adr + 0xffff) &
1879 			(~0xffff);
1880 		if (debug & VP9_DEBUG_BUFMGR_MORE)
1881 			pr_info(
1882 			"%s => %d (%ld) size 0x%x\n",
1883 			__func__, ret,
1884 			*mpred_mv_wr_start_addr,
1885 			pbi->m_mv_BUF[ret].size);
1886 	} else {
1887 		pr_info(
1888 		"%s: Error, mv buf is not enough\n",
1889 		__func__);
1890 	}
1891 	return ret;
1892 }
1893 
put_mv_buf(struct VP9Decoder_s * pbi,int * mv_buf_index)1894 static void put_mv_buf(struct VP9Decoder_s *pbi,
1895 				int *mv_buf_index)
1896 {
1897 	int i = *mv_buf_index;
1898 	if (i >= MV_BUFFER_NUM) {
1899 		if (debug & VP9_DEBUG_BUFMGR_MORE)
1900 			pr_info(
1901 			"%s: index %d beyond range\n",
1902 			__func__, i);
1903 		return;
1904 	}
1905 	if (debug & VP9_DEBUG_BUFMGR_MORE)
1906 		pr_info(
1907 		"%s(%d): used_flag(%d)\n",
1908 		__func__, i,
1909 		pbi->m_mv_BUF[i].used_flag);
1910 
1911 	*mv_buf_index = -1;
1912 	if (pbi->m_mv_BUF[i].start_adr &&
1913 		pbi->m_mv_BUF[i].used_flag)
1914 		pbi->m_mv_BUF[i].used_flag = 0;
1915 }
1916 
put_un_used_mv_bufs(struct VP9Decoder_s * pbi)1917 static void	put_un_used_mv_bufs(struct VP9Decoder_s *pbi)
1918 {
1919 	struct VP9_Common_s *const cm = &pbi->common;
1920 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
1921 	int i;
1922 	for (i = 0; i < pbi->used_buf_num; ++i) {
1923 		if ((frame_bufs[i].ref_count == 0) &&
1924 			(frame_bufs[i].buf.index != -1) &&
1925 			(frame_bufs[i].buf.mv_buf_index >= 0)
1926 			)
1927 			put_mv_buf(pbi, &frame_bufs[i].buf.mv_buf_index);
1928 	}
1929 }
1930 
1931 #ifdef SUPPORT_FB_DECODING
mv_buf_available(struct VP9Decoder_s * pbi)1932 static bool mv_buf_available(struct VP9Decoder_s *pbi)
1933 {
1934 	int i;
1935 	bool ret = 0;
1936 	for (i = 0; i < MV_BUFFER_NUM; i++) {
1937 		if (pbi->m_mv_BUF[i].start_adr &&
1938 			pbi->m_mv_BUF[i].used_flag == 0) {
1939 			ret = 1;
1940 			break;
1941 		}
1942 	}
1943 	return ret;
1944 }
1945 #endif
1946 #endif
1947 
1948 #ifdef SUPPORT_FB_DECODING
init_stage_buf(struct VP9Decoder_s * pbi)1949 static void init_stage_buf(struct VP9Decoder_s *pbi)
1950 {
1951 	uint i;
1952 	for (i = 0; i < STAGE_MAX_BUFFERS
1953 		&& i < stage_buf_num; i++) {
1954 		pbi->stage_bufs[i] =
1955 			vmalloc(sizeof(struct stage_buf_s));
1956 		if (pbi->stage_bufs[i] == NULL) {
1957 			vp9_print(pbi,
1958 				0, "%s vmalloc fail\n", __func__);
1959 			break;
1960 		}
1961 		pbi->stage_bufs[i]->index = i;
1962 	}
1963 	pbi->used_stage_buf_num = i;
1964 	pbi->s1_pos = 0;
1965 	pbi->s2_pos = 0;
1966 	pbi->s1_buf = NULL;
1967 	pbi->s2_buf = NULL;
1968 	pbi->s1_mv_buf_index = FRAME_BUFFERS;
1969 	pbi->s1_mv_buf_index_pre = FRAME_BUFFERS;
1970 	pbi->s1_mv_buf_index_pre_pre = FRAME_BUFFERS;
1971 
1972 	if (pbi->used_stage_buf_num > 0)
1973 		vp9_print(pbi,
1974 			0, "%s 2 stage decoding buf %d\n",
1975 			__func__,
1976 			pbi->used_stage_buf_num);
1977 }
1978 
uninit_stage_buf(struct VP9Decoder_s * pbi)1979 static void uninit_stage_buf(struct VP9Decoder_s *pbi)
1980 {
1981 	int i;
1982 	for (i = 0; i < pbi->used_stage_buf_num; i++) {
1983 		if (pbi->stage_bufs[i])
1984 			vfree(pbi->stage_bufs[i]);
1985 		pbi->stage_bufs[i] = NULL;
1986 	}
1987 	pbi->used_stage_buf_num = 0;
1988 	pbi->s1_pos = 0;
1989 	pbi->s2_pos = 0;
1990 	pbi->s1_buf = NULL;
1991 	pbi->s2_buf = NULL;
1992 }
1993 
get_s1_buf(struct VP9Decoder_s * pbi)1994 static int get_s1_buf(
1995 	struct VP9Decoder_s *pbi)
1996 {
1997 	struct stage_buf_s *buf = NULL;
1998 	int ret = -1;
1999 	int buf_page_num = MAX_STAGE_PAGE_NUM;
2000 	int next_s1_pos = pbi->s1_pos + 1;
2001 
2002 	if (next_s1_pos >= pbi->used_stage_buf_num)
2003 		next_s1_pos = 0;
2004 	if (next_s1_pos == pbi->s2_pos) {
2005 		pbi->s1_buf = NULL;
2006 		return ret;
2007 	}
2008 
2009 	buf = pbi->stage_bufs[pbi->s1_pos];
2010 	ret = decoder_mmu_box_alloc_idx(
2011 		pbi->mmu_box,
2012 		buf->index,
2013 		buf_page_num,
2014 		pbi->stage_mmu_map_addr);
2015 	if (ret < 0) {
2016 		vp9_print(pbi, 0,
2017 			"%s decoder_mmu_box_alloc fail for index %d (s1_pos %d s2_pos %d)\n",
2018 			__func__, buf->index,
2019 			pbi->s1_pos, pbi->s2_pos);
2020 		buf = NULL;
2021 	} else {
2022 		vp9_print(pbi, VP9_DEBUG_2_STAGE,
2023 			"%s decoder_mmu_box_alloc %d page for index %d (s1_pos %d s2_pos %d)\n",
2024 			__func__, buf_page_num, buf->index,
2025 			pbi->s1_pos, pbi->s2_pos);
2026 	}
2027 	pbi->s1_buf = buf;
2028 	return ret;
2029 }
2030 
inc_s1_pos(struct VP9Decoder_s * pbi)2031 static void inc_s1_pos(struct VP9Decoder_s *pbi)
2032 {
2033 	struct stage_buf_s *buf =
2034 		pbi->stage_bufs[pbi->s1_pos];
2035 
2036 	int used_page_num =
2037 #ifdef FB_DECODING_TEST_SCHEDULE
2038 		MAX_STAGE_PAGE_NUM/2;
2039 #else
2040 		(READ_VREG(HEVC_ASSIST_HED_FB_W_CTL) >> 16);
2041 #endif
2042 	decoder_mmu_box_free_idx_tail(pbi->mmu_box,
2043 		FRAME_BUFFERS + buf->index, used_page_num);
2044 
2045 	pbi->s1_pos++;
2046 	if (pbi->s1_pos >= pbi->used_stage_buf_num)
2047 		pbi->s1_pos = 0;
2048 
2049 	vp9_print(pbi, VP9_DEBUG_2_STAGE,
2050 		"%s (used_page_num %d) for index %d (s1_pos %d s2_pos %d)\n",
2051 		__func__, used_page_num, buf->index,
2052 		pbi->s1_pos, pbi->s2_pos);
2053 }
2054 
2055 #define s2_buf_available(pbi) (pbi->s1_pos != pbi->s2_pos)
2056 
get_s2_buf(struct VP9Decoder_s * pbi)2057 static int get_s2_buf(
2058 	struct VP9Decoder_s *pbi)
2059 {
2060 	int ret = -1;
2061 	struct stage_buf_s *buf = NULL;
2062 	if (s2_buf_available(pbi)) {
2063 		buf = pbi->stage_bufs[pbi->s2_pos];
2064 		vp9_print(pbi, VP9_DEBUG_2_STAGE,
2065 			"%s for index %d (s1_pos %d s2_pos %d)\n",
2066 			__func__, buf->index,
2067 			pbi->s1_pos, pbi->s2_pos);
2068 		pbi->s2_buf = buf;
2069 		ret = 0;
2070 	}
2071 	return ret;
2072 }
2073 
inc_s2_pos(struct VP9Decoder_s * pbi)2074 static void inc_s2_pos(struct VP9Decoder_s *pbi)
2075 {
2076 	struct stage_buf_s *buf =
2077 		pbi->stage_bufs[pbi->s2_pos];
2078 	decoder_mmu_box_free_idx(pbi->mmu_box,
2079 		FRAME_BUFFERS + buf->index);
2080 	pbi->s2_pos++;
2081 	if (pbi->s2_pos >= pbi->used_stage_buf_num)
2082 		pbi->s2_pos = 0;
2083 	vp9_print(pbi, VP9_DEBUG_2_STAGE,
2084 		"%s for index %d (s1_pos %d s2_pos %d)\n",
2085 		__func__, buf->index,
2086 		pbi->s1_pos, pbi->s2_pos);
2087 }
2088 
get_free_stage_buf_num(struct VP9Decoder_s * pbi)2089 static int get_free_stage_buf_num(struct VP9Decoder_s *pbi)
2090 {
2091 	int num;
2092 	if (pbi->s1_pos >= pbi->s2_pos)
2093 		num = pbi->used_stage_buf_num -
2094 			(pbi->s1_pos - pbi->s2_pos) - 1;
2095 	else
2096 		num = (pbi->s2_pos - pbi->s1_pos) - 1;
2097 	return num;
2098 }
2099 
2100 #ifndef FB_DECODING_TEST_SCHEDULE
2101 static DEFINE_SPINLOCK(fb_core_spin_lock);
2102 
is_s2_decoding_finished(struct VP9Decoder_s * pbi)2103 static u8 is_s2_decoding_finished(struct VP9Decoder_s *pbi)
2104 {
2105 	/* to do: VLSI review
2106 		completion of last LCU decoding in BACK
2107 	 */
2108 	return 1;
2109 }
2110 
start_s1_decoding(struct VP9Decoder_s * pbi)2111 static void start_s1_decoding(struct VP9Decoder_s *pbi)
2112 {
2113 	/* to do: VLSI review
2114 	 after parser, how to start LCU decoding in BACK
2115 	*/
2116 }
2117 
fb_reset_core(struct vdec_s * vdec,u32 mask)2118 static void fb_reset_core(struct vdec_s *vdec, u32 mask)
2119 {
2120 	/* to do: VLSI review
2121 		1. how to disconnect DMC for FRONT and BACK
2122 		2. reset bit 13, 24, FRONT or BACK ??
2123 	*/
2124 
2125 	unsigned long flags;
2126 	u32 reset_bits = 0;
2127 	if (mask & HW_MASK_FRONT)
2128 		WRITE_VREG(HEVC_STREAM_CONTROL, 0);
2129 	spin_lock_irqsave(&fb_core_spin_lock, flags);
2130 	codec_dmcbus_write(DMC_REQ_CTRL,
2131 		codec_dmcbus_read(DMC_REQ_CTRL) & (~(1 << 4)));
2132 	spin_unlock_irqrestore(&fb_core_spin_lock, flags);
2133 
2134 	while (!(codec_dmcbus_read(DMC_CHAN_STS)
2135 		& (1 << 4)))
2136 		;
2137 
2138 	if ((mask & HW_MASK_FRONT) &&
2139 		input_frame_based(vdec))
2140 		WRITE_VREG(HEVC_STREAM_CONTROL, 0);
2141 
2142 		/*
2143 	 * 2: assist
2144 	 * 3: parser
2145 	 * 4: parser_state
2146 	 * 8: dblk
2147 	 * 11:mcpu
2148 	 * 12:ccpu
2149 	 * 13:ddr
2150 	 * 14:iqit
2151 	 * 15:ipp
2152 	 * 17:qdct
2153 	 * 18:mpred
2154 	 * 19:sao
2155 	 * 24:hevc_afifo
2156 	 */
2157 	if (mask & HW_MASK_FRONT) {
2158 		reset_bits =
2159 		(1<<3)|(1<<4)|(1<<11)|
2160 		(1<<12)|(1<<18);
2161 	}
2162 	if (mask & HW_MASK_BACK) {
2163 		reset_bits =
2164 		(1<<8)|(1<<13)|(1<<14)|(1<<15)|
2165 		(1<<17)|(1<<19)|(1<<24);
2166 	}
2167 	WRITE_VREG(DOS_SW_RESET3, reset_bits);
2168 #if 0
2169 		(1<<3)|(1<<4)|(1<<8)|(1<<11)|
2170 		(1<<12)|(1<<13)|(1<<14)|(1<<15)|
2171 		(1<<17)|(1<<18)|(1<<19)|(1<<24);
2172 #endif
2173 	WRITE_VREG(DOS_SW_RESET3, 0);
2174 
2175 
2176 	spin_lock_irqsave(&fb_core_spin_lock, flags);
2177 	codec_dmcbus_write(DMC_REQ_CTRL,
2178 		codec_dmcbus_read(DMC_REQ_CTRL) | (1 << 4));
2179 	spin_unlock_irqrestore(&fb_core_spin_lock, flags);
2180 
2181 }
2182 #endif
2183 
2184 #endif
2185 
2186 static void init_pic_list_hw(struct VP9Decoder_s *pbi);
2187 
get_free_fb(struct VP9Decoder_s * pbi)2188 static int get_free_fb(struct VP9Decoder_s *pbi)
2189 {
2190 	struct VP9_Common_s *const cm = &pbi->common;
2191 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
2192 	int i;
2193 	unsigned long flags;
2194 
2195 	lock_buffer_pool(cm->buffer_pool, flags);
2196 	if (debug & VP9_DEBUG_BUFMGR_MORE) {
2197 		for (i = 0; i < pbi->used_buf_num; ++i) {
2198 			pr_info("%s:%d, ref_count %d vf_ref %d index %d\r\n",
2199 			__func__, i, frame_bufs[i].ref_count,
2200 			frame_bufs[i].buf.vf_ref,
2201 			frame_bufs[i].buf.index);
2202 		}
2203 	}
2204 	for (i = 0; i < pbi->used_buf_num; ++i) {
2205 		if ((frame_bufs[i].ref_count == 0) &&
2206 			(frame_bufs[i].buf.vf_ref == 0) &&
2207 			(frame_bufs[i].buf.index != -1)
2208 			)
2209 			break;
2210 	}
2211 	if (i != pbi->used_buf_num) {
2212 		frame_bufs[i].ref_count = 1;
2213 		/*pr_info("[MMU DEBUG 1] set ref_count[%d] : %d\r\n",
2214 					i, frame_bufs[i].ref_count);*/
2215 	} else {
2216 		/* Reset i to be INVALID_IDX to indicate
2217 			no free buffer found*/
2218 		i = INVALID_IDX;
2219 	}
2220 
2221 	unlock_buffer_pool(cm->buffer_pool, flags);
2222 	return i;
2223 }
2224 
v4l_get_free_fb(struct VP9Decoder_s * pbi)2225 static int v4l_get_free_fb(struct VP9Decoder_s *pbi)
2226 {
2227 	struct VP9_Common_s *const cm = &pbi->common;
2228 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
2229 	struct aml_vcodec_ctx * v4l = pbi->v4l2_ctx;
2230 	struct v4l_buff_pool *pool = &v4l->cap_pool;
2231 	struct PIC_BUFFER_CONFIG_s *pic = NULL;
2232 	int i, idx = INVALID_IDX;
2233 	ulong flags;
2234 
2235 	lock_buffer_pool(cm->buffer_pool, flags);
2236 
2237 	for (i = 0; i < pool->in; ++i) {
2238 		u32 state = (pool->seq[i] >> 16);
2239 		u32 index = (pool->seq[i] & 0xffff);
2240 
2241 		switch (state) {
2242 		case V4L_CAP_BUFF_IN_DEC:
2243 			pic = &frame_bufs[i].buf;
2244 			if ((frame_bufs[i].ref_count == 0) &&
2245 				(pic->vf_ref == 0) &&
2246 				(pic->index != -1) &&
2247 				pic->cma_alloc_addr) {
2248 				idx = i;
2249 			}
2250 			break;
2251 		case V4L_CAP_BUFF_IN_M2M:
2252 			pic = &frame_bufs[index].buf;
2253 			pic->y_crop_width = pbi->frame_width;
2254 			pic->y_crop_height = pbi->frame_height;
2255 			if (!v4l_alloc_and_config_pic(pbi, pic)) {
2256 				set_canvas(pbi, pic);
2257 				init_pic_list_hw(pbi);
2258 				idx = index;
2259 			}
2260 			break;
2261 		default:
2262 			pr_err("v4l buffer state err %d.\n", state);
2263 			break;
2264 		}
2265 
2266 		if (idx != INVALID_IDX) {
2267 			frame_bufs[idx].ref_count = 1;
2268 			break;
2269 		}
2270 	}
2271 
2272 	unlock_buffer_pool(cm->buffer_pool, flags);
2273 
2274 	return idx;
2275 }
2276 
get_free_buf_count(struct VP9Decoder_s * pbi)2277 static int get_free_buf_count(struct VP9Decoder_s *pbi)
2278 {
2279 	struct VP9_Common_s *const cm = &pbi->common;
2280 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
2281 	int i;
2282 	int free_buf_count = 0;
2283 	for (i = 0; i < pbi->used_buf_num; ++i)
2284 		if ((frame_bufs[i].ref_count == 0) &&
2285 			(frame_bufs[i].buf.vf_ref == 0) &&
2286 			(frame_bufs[i].buf.index != -1)
2287 			)
2288 			free_buf_count++;
2289 	return free_buf_count;
2290 }
2291 
decrease_ref_count(int idx,struct RefCntBuffer_s * const frame_bufs,struct BufferPool_s * const pool)2292 static void decrease_ref_count(int idx, struct RefCntBuffer_s *const frame_bufs,
2293 					struct BufferPool_s *const pool)
2294 {
2295 	if (idx >= 0) {
2296 		--frame_bufs[idx].ref_count;
2297 		/*pr_info("[MMU DEBUG 7] dec ref_count[%d] : %d\r\n", idx,
2298 		 *	frame_bufs[idx].ref_count);
2299 		 */
2300 		/*A worker may only get a free framebuffer index when
2301 		 *calling get_free_fb. But the private buffer is not set up
2302 		 *until finish decoding header. So any error happens during
2303 		 *decoding header, the frame_bufs will not have valid priv
2304 		 *buffer.
2305 		 */
2306 
2307 		if (frame_bufs[idx].ref_count == 0 &&
2308 			frame_bufs[idx].raw_frame_buffer.priv)
2309 			vp9_release_frame_buffer
2310 			(&frame_bufs[idx].raw_frame_buffer);
2311 	}
2312 }
2313 
generate_next_ref_frames(struct VP9Decoder_s * pbi)2314 static void generate_next_ref_frames(struct VP9Decoder_s *pbi)
2315 {
2316 	struct VP9_Common_s *const cm = &pbi->common;
2317 	struct RefCntBuffer_s *frame_bufs = cm->buffer_pool->frame_bufs;
2318 	struct BufferPool_s *const pool = cm->buffer_pool;
2319 	int mask, ref_index = 0;
2320 	unsigned long flags;
2321 
2322 	/* Generate next_ref_frame_map.*/
2323 	lock_buffer_pool(pool, flags);
2324 	for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2325 		if (mask & 1) {
2326 			cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
2327 			++frame_bufs[cm->new_fb_idx].ref_count;
2328 			/*pr_info("[MMU DEBUG 4] inc ref_count[%d] : %d\r\n",
2329 			 *cm->new_fb_idx, frame_bufs[cm->new_fb_idx].ref_count);
2330 			 */
2331 		} else
2332 			cm->next_ref_frame_map[ref_index] =
2333 				cm->ref_frame_map[ref_index];
2334 		/* Current thread holds the reference frame.*/
2335 		if (cm->ref_frame_map[ref_index] >= 0) {
2336 			++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2337 			/*pr_info
2338 			 *("[MMU DEBUG 5] inc ref_count[%d] : %d\r\n",
2339 			 *cm->ref_frame_map[ref_index],
2340 			 *frame_bufs[cm->ref_frame_map[ref_index]].ref_count);
2341 			 */
2342 		}
2343 		++ref_index;
2344 	}
2345 
2346 	for (; ref_index < REF_FRAMES; ++ref_index) {
2347 		cm->next_ref_frame_map[ref_index] =
2348 			cm->ref_frame_map[ref_index];
2349 		/* Current thread holds the reference frame.*/
2350 		if (cm->ref_frame_map[ref_index] >= 0) {
2351 			++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
2352 			/*pr_info("[MMU DEBUG 6] inc ref_count[%d] : %d\r\n",
2353 			 *cm->ref_frame_map[ref_index],
2354 			 *frame_bufs[cm->ref_frame_map[ref_index]].ref_count);
2355 			 */
2356 		}
2357 	}
2358 	unlock_buffer_pool(pool, flags);
2359 	return;
2360 }
2361 
refresh_ref_frames(struct VP9Decoder_s * pbi)2362 static void refresh_ref_frames(struct VP9Decoder_s *pbi)
2363 
2364 {
2365 	struct VP9_Common_s *const cm = &pbi->common;
2366 	struct BufferPool_s *pool = cm->buffer_pool;
2367 	struct RefCntBuffer_s *frame_bufs = cm->buffer_pool->frame_bufs;
2368 	int mask, ref_index = 0;
2369 	unsigned long flags;
2370 
2371 	lock_buffer_pool(pool, flags);
2372 	for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
2373 		const int old_idx = cm->ref_frame_map[ref_index];
2374 		/*Current thread releases the holding of reference frame.*/
2375 		decrease_ref_count(old_idx, frame_bufs, pool);
2376 
2377 		/*Release the reference frame in reference map.*/
2378 		if ((mask & 1) && old_idx >= 0)
2379 			decrease_ref_count(old_idx, frame_bufs, pool);
2380 		cm->ref_frame_map[ref_index] =
2381 			cm->next_ref_frame_map[ref_index];
2382 		++ref_index;
2383 	}
2384 
2385 	/*Current thread releases the holding of reference frame.*/
2386 	for (; ref_index < REF_FRAMES && !cm->show_existing_frame;
2387 		++ref_index) {
2388 		const int old_idx = cm->ref_frame_map[ref_index];
2389 
2390 		decrease_ref_count(old_idx, frame_bufs, pool);
2391 		cm->ref_frame_map[ref_index] =
2392 			cm->next_ref_frame_map[ref_index];
2393 	}
2394 	unlock_buffer_pool(pool, flags);
2395 	return;
2396 }
2397 
vp9_bufmgr_process(struct VP9Decoder_s * pbi,union param_u * params)2398 int vp9_bufmgr_process(struct VP9Decoder_s *pbi, union param_u *params)
2399 {
2400 	struct VP9_Common_s *const cm = &pbi->common;
2401 	struct BufferPool_s *pool = cm->buffer_pool;
2402 	struct RefCntBuffer_s *frame_bufs = cm->buffer_pool->frame_bufs;
2403 	struct PIC_BUFFER_CONFIG_s *pic = NULL;
2404 	int i;
2405 	int ret;
2406 
2407 	pbi->ready_for_new_data = 0;
2408 
2409 	if (pbi->has_keyframe == 0 &&
2410 		params->p.frame_type != KEY_FRAME){
2411 		on_no_keyframe_skiped++;
2412 		return -2;
2413 	}
2414 	pbi->has_keyframe = 1;
2415 	on_no_keyframe_skiped = 0;
2416 #if 0
2417 	if (pbi->mmu_enable) {
2418 		if (!pbi->m_ins_flag)
2419 			pbi->used_4k_num = (READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
2420 		if (cm->prev_fb_idx >= 0) {
2421 			decoder_mmu_box_free_idx_tail(pbi->mmu_box,
2422 			cm->prev_fb_idx, pbi->used_4k_num);
2423 		}
2424 	}
2425 #endif
2426 	if (cm->new_fb_idx >= 0
2427 		&& frame_bufs[cm->new_fb_idx].ref_count == 0){
2428 		vp9_release_frame_buffer
2429 			(&frame_bufs[cm->new_fb_idx].raw_frame_buffer);
2430 	}
2431 	/*pr_info("Before get_free_fb, prev_fb_idx : %d, new_fb_idx : %d\r\n",
2432 		cm->prev_fb_idx, cm->new_fb_idx);*/
2433 #ifndef MV_USE_FIXED_BUF
2434 	put_un_used_mv_bufs(pbi);
2435 	if (debug & VP9_DEBUG_BUFMGR_DETAIL)
2436 		dump_pic_list(pbi);
2437 #endif
2438 	cm->new_fb_idx = pbi->is_used_v4l ?
2439 		v4l_get_free_fb(pbi) :
2440 		get_free_fb(pbi);
2441 	if (cm->new_fb_idx == INVALID_IDX) {
2442 		pr_info("get_free_fb error\r\n");
2443 		return -1;
2444 	}
2445 #ifndef MV_USE_FIXED_BUF
2446 #ifdef SUPPORT_FB_DECODING
2447 	if (pbi->used_stage_buf_num == 0) {
2448 #endif
2449 		if (get_mv_buf(pbi,
2450 			&pool->frame_bufs[cm->new_fb_idx].
2451 			buf.mv_buf_index,
2452 			&pool->frame_bufs[cm->new_fb_idx].
2453 			buf.mpred_mv_wr_start_addr
2454 			) < 0) {
2455 			pr_info("get_mv_buf fail\r\n");
2456 			return -1;
2457 		}
2458 		if (debug & VP9_DEBUG_BUFMGR_DETAIL)
2459 			dump_pic_list(pbi);
2460 #ifdef SUPPORT_FB_DECODING
2461 	}
2462 #endif
2463 #endif
2464 	cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
2465 	/*if (debug & VP9_DEBUG_BUFMGR)
2466 		pr_info("[VP9 DEBUG]%s(get_free_fb): %d\r\n", __func__,
2467 				cm->new_fb_idx);*/
2468 
2469 	pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
2470 	if (pbi->mmu_enable) {
2471 		/* moved to after picture size ready
2472 		 *alloc_mmu(cm, params->p.width, params->p.height,
2473 		 *params->p.bit_depth, pbi->frame_mmu_map_addr);
2474 		 */
2475 		cm->prev_fb_idx = cm->new_fb_idx;
2476 	}
2477 	/*read_uncompressed_header()*/
2478 	cm->last_frame_type = cm->frame_type;
2479 	cm->last_intra_only = cm->intra_only;
2480 	cm->profile = params->p.profile;
2481 	if (cm->profile >= MAX_PROFILES) {
2482 		pr_err("Error: Unsupported profile %d\r\n", cm->profile);
2483 		return -1;
2484 	}
2485 	cm->show_existing_frame = params->p.show_existing_frame;
2486 	if (cm->show_existing_frame) {
2487 		/* Show an existing frame directly.*/
2488 		int frame_to_show_idx = params->p.frame_to_show_idx;
2489 		int frame_to_show;
2490 		unsigned long flags;
2491 		if (frame_to_show_idx >= REF_FRAMES) {
2492 			pr_info("frame_to_show_idx %d exceed max index\r\n",
2493 					frame_to_show_idx);
2494 			return -1;
2495 		}
2496 
2497 		frame_to_show = cm->ref_frame_map[frame_to_show_idx];
2498 		/*pr_info("frame_to_show %d\r\n", frame_to_show);*/
2499 		lock_buffer_pool(pool, flags);
2500 		if (frame_to_show < 0 ||
2501 			frame_bufs[frame_to_show].ref_count < 1) {
2502 			unlock_buffer_pool(pool, flags);
2503 			pr_err
2504 			("Error:Buffer %d does not contain a decoded frame",
2505 			frame_to_show);
2506 			return -1;
2507 		}
2508 
2509 		ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
2510 		unlock_buffer_pool(pool, flags);
2511 		pbi->refresh_frame_flags = 0;
2512 		/*cm->lf.filter_level = 0;*/
2513 		cm->show_frame = 1;
2514 
2515 		/*
2516 		 *if (pbi->frame_parallel_decode) {
2517 		 *	for (i = 0; i < REF_FRAMES; ++i)
2518 		 *		cm->next_ref_frame_map[i] =
2519 		 *		cm->ref_frame_map[i];
2520 		 *}
2521 		 */
2522 		/* do not decode, search next start code */
2523 		return 1;
2524 	}
2525 	cm->frame_type = params->p.frame_type;
2526 	cm->show_frame = params->p.show_frame;
2527 	cm->error_resilient_mode = params->p.error_resilient_mode;
2528 
2529 
2530 	if (cm->frame_type == KEY_FRAME) {
2531 		pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
2532 
2533 		for (i = 0; i < REFS_PER_FRAME; ++i) {
2534 			cm->frame_refs[i].idx = INVALID_IDX;
2535 			cm->frame_refs[i].buf = NULL;
2536 		}
2537 
2538 		ret = setup_frame_size(pbi,
2539 			cm, params,  pbi->frame_mmu_map_addr,
2540 				print_header_info);
2541 		if (ret)
2542 			return -1;
2543 		if (pbi->need_resync) {
2544 			memset(&cm->ref_frame_map, -1,
2545 				sizeof(cm->ref_frame_map));
2546 			pbi->need_resync = 0;
2547 		}
2548 	} else {
2549 		cm->intra_only = cm->show_frame ? 0 : params->p.intra_only;
2550 		/*if (print_header_info) {
2551 		 *	if (cm->show_frame)
2552 		 *		pr_info
2553 		 *		("intra_only set to 0 because of show_frame\n");
2554 		 *	else
2555 		 *		pr_info
2556 		 *		("1-bit intra_only read: %d\n", cm->intra_only);
2557 		 *}
2558 		 */
2559 
2560 
2561 		cm->reset_frame_context = cm->error_resilient_mode ?
2562 			0 : params->p.reset_frame_context;
2563 		if (print_header_info) {
2564 			if (cm->error_resilient_mode)
2565 				pr_info
2566 				("reset to 0 error_resilient_mode\n");
2567 		else
2568 			pr_info
2569 				(" * 2-bits reset_frame_context read : %d\n",
2570 				cm->reset_frame_context);
2571 		}
2572 
2573 		if (cm->intra_only) {
2574 			if (cm->profile > PROFILE_0) {
2575 				/*read_bitdepth_colorspace_sampling(cm,
2576 				 *	rb, print_header_info);
2577 				 */
2578 			} else {
2579 				/*NOTE: The intra-only frame header
2580 				 *does not include the specification
2581 				 *of either the color format or
2582 				 *color sub-sampling
2583 				 *in profile 0. VP9 specifies that the default
2584 				 *color format should be YUV 4:2:0 in this
2585 				 *case (normative).
2586 				 */
2587 				cm->color_space = VPX_CS_BT_601;
2588 				cm->subsampling_y = cm->subsampling_x = 1;
2589 				cm->bit_depth = VPX_BITS_8;
2590 				cm->use_highbitdepth = 0;
2591 			}
2592 
2593 			pbi->refresh_frame_flags =
2594 				params->p.refresh_frame_flags;
2595 			/*if (print_header_info)
2596 			 *	pr_info("*%d-bits refresh_frame read:0x%x\n",
2597 			 *	REF_FRAMES, pbi->refresh_frame_flags);
2598 			 */
2599 			ret = setup_frame_size(pbi,
2600 				cm,
2601 				params,
2602 				pbi->frame_mmu_map_addr,
2603 				print_header_info);
2604 			if (ret)
2605 				return -1;
2606 			if (pbi->need_resync) {
2607 				memset(&cm->ref_frame_map, -1,
2608 					sizeof(cm->ref_frame_map));
2609 				pbi->need_resync = 0;
2610 			}
2611 		} else if (pbi->need_resync != 1) {  /* Skip if need resync */
2612 			pbi->refresh_frame_flags =
2613 					params->p.refresh_frame_flags;
2614 			if (print_header_info)
2615 				pr_info
2616 				("*%d-bits refresh_frame read:0x%x\n",
2617 				REF_FRAMES, pbi->refresh_frame_flags);
2618 			for (i = 0; i < REFS_PER_FRAME; ++i) {
2619 				const int ref =
2620 					(params->p.ref_info >>
2621 					(((REFS_PER_FRAME-i-1)*4)+1))
2622 					& 0x7;
2623 				const int idx =
2624 					cm->ref_frame_map[ref];
2625 				struct RefBuffer_s * const ref_frame =
2626 					&cm->frame_refs[i];
2627 				if (print_header_info)
2628 					pr_info("*%d-bits ref[%d]read:%d\n",
2629 						REF_FRAMES_LOG2, i, ref);
2630 				ref_frame->idx = idx;
2631 				ref_frame->buf = &frame_bufs[idx].buf;
2632 				cm->ref_frame_sign_bias[LAST_FRAME + i]
2633 				= (params->p.ref_info >>
2634 				((REFS_PER_FRAME-i-1)*4)) & 0x1;
2635 				if (print_header_info)
2636 					pr_info("1bit ref_frame_sign_bias");
2637 				/*pr_info
2638 				 *("%dread: %d\n",
2639 				 *LAST_FRAME+i,
2640 				 *cm->ref_frame_sign_bias
2641 				 *[LAST_FRAME + i]);
2642 				 */
2643 				/*pr_info
2644 				 *("[VP9 DEBUG]%s(get ref):%d\r\n",
2645 				 *__func__, ref_frame->idx);
2646 				 */
2647 
2648 			}
2649 
2650 			ret = setup_frame_size_with_refs(
2651 				pbi,
2652 				cm,
2653 				params,
2654 				pbi->frame_mmu_map_addr,
2655 				print_header_info);
2656 			if (ret)
2657 				return -1;
2658 			for (i = 0; i < REFS_PER_FRAME; ++i) {
2659 				/*struct RefBuffer_s *const ref_buf =
2660 				 *&cm->frame_refs[i];
2661 				 */
2662 				/* to do:
2663 				 *vp9_setup_scale_factors_for_frame
2664 				 */
2665 			}
2666 		}
2667 	}
2668 
2669 	pic = get_frame_new_buffer(cm);
2670 	if (!pic)
2671 		return -1;
2672 
2673 	pic->bit_depth = cm->bit_depth;
2674 	pic->color_space = cm->color_space;
2675 	pic->slice_type = cm->frame_type;
2676 
2677 	if (pbi->need_resync) {
2678 		pr_err
2679 		("Error: Keyframe/intra-only frame required to reset\r\n");
2680 		return -1;
2681 	}
2682 	generate_next_ref_frames(pbi);
2683 	pbi->hold_ref_buf = 1;
2684 
2685 #if 0
2686 	if (frame_is_intra_only(cm) || cm->error_resilient_mode)
2687 		vp9_setup_past_independence(cm);
2688 	setup_loopfilter(&cm->lf, rb, print_header_info);
2689 	setup_quantization(cm, &pbi->mb, rb, print_header_info);
2690 	setup_segmentation(&cm->seg, rb, print_header_info);
2691 	setup_segmentation_dequant(cm, print_header_info);
2692 
2693 	setup_tile_info(cm, rb, print_header_info);
2694 	sz = vp9_rb_read_literal(rb, 16);
2695 	if (print_header_info)
2696 		pr_info(" * 16-bits size read : %d (0x%x)\n", sz, sz);
2697 
2698 	if (sz == 0)
2699 		vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2700 		"Invalid header size");
2701 #endif
2702 	/*end read_uncompressed_header()*/
2703 	cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
2704 			cm->width == cm->last_width &&
2705 			cm->height == cm->last_height &&
2706 			!cm->last_intra_only &&
2707 			cm->last_show_frame &&
2708 			(cm->last_frame_type != KEY_FRAME);
2709 
2710 	/*pr_info
2711 	 *("set use_prev_frame_mvs to %d (last_width %d last_height %d",
2712 	 *cm->use_prev_frame_mvs, cm->last_width, cm->last_height);
2713 	 *pr_info
2714 	 *(" last_intra_only %d last_show_frame %d last_frame_type %d)\n",
2715 	 *cm->last_intra_only, cm->last_show_frame, cm->last_frame_type);
2716 	 */
2717 	return 0;
2718 }
2719 
2720 
swap_frame_buffers(struct VP9Decoder_s * pbi)2721 void swap_frame_buffers(struct VP9Decoder_s *pbi)
2722 {
2723 	int ref_index = 0;
2724 	struct VP9_Common_s *const cm = &pbi->common;
2725 	struct BufferPool_s *const pool = cm->buffer_pool;
2726 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
2727 	unsigned long flags;
2728 	refresh_ref_frames(pbi);
2729 	pbi->hold_ref_buf = 0;
2730 	cm->frame_to_show = get_frame_new_buffer(cm);
2731 
2732 	if (cm->frame_to_show) {
2733 		/*if (!pbi->frame_parallel_decode || !cm->show_frame) {*/
2734 		lock_buffer_pool(pool, flags);
2735 		--frame_bufs[cm->new_fb_idx].ref_count;
2736 		/*pr_info("[MMU DEBUG 8] dec ref_count[%d] : %d\r\n", cm->new_fb_idx,
2737 		 *	frame_bufs[cm->new_fb_idx].ref_count);
2738 		 */
2739 		unlock_buffer_pool(pool, flags);
2740 		/*}*/
2741 	}
2742 
2743 	/*Invalidate these references until the next frame starts.*/
2744 	for (ref_index = 0; ref_index < 3; ref_index++)
2745 		cm->frame_refs[ref_index].idx = -1;
2746 }
2747 
2748 #if 0
2749 static void check_resync(vpx_codec_alg_priv_t *const ctx,
2750 				const struct VP9Decoder_s *const pbi)
2751 {
2752 	/* Clear resync flag if worker got a key frame or intra only frame.*/
2753 	if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
2754 		(pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
2755 		ctx->need_resync = 0;
2756 }
2757 #endif
2758 
vp9_get_raw_frame(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * sd)2759 int vp9_get_raw_frame(struct VP9Decoder_s *pbi, struct PIC_BUFFER_CONFIG_s *sd)
2760 {
2761 	struct VP9_Common_s *const cm = &pbi->common;
2762 	int ret = -1;
2763 
2764 	if (pbi->ready_for_new_data == 1)
2765 		return ret;
2766 
2767 	pbi->ready_for_new_data = 1;
2768 
2769 	/* no raw frame to show!!! */
2770 	if (!cm->show_frame)
2771 		return ret;
2772 
2773 	/* may not be get buff in v4l2 */
2774 	if (!cm->frame_to_show)
2775 		return ret;
2776 
2777 	pbi->ready_for_new_data = 1;
2778 
2779 	*sd = *cm->frame_to_show;
2780 	ret = 0;
2781 
2782 	return ret;
2783 }
2784 
vp9_bufmgr_init(struct VP9Decoder_s * pbi,struct BuffInfo_s * buf_spec_i,struct buff_s * mc_buf_i)2785 int vp9_bufmgr_init(struct VP9Decoder_s *pbi, struct BuffInfo_s *buf_spec_i,
2786 		struct buff_s *mc_buf_i) {
2787 	struct VP9_Common_s *cm = &pbi->common;
2788 
2789 	/*memset(pbi, 0, sizeof(struct VP9Decoder_s));*/
2790 	pbi->frame_count = 0;
2791 	pbi->pic_count = 0;
2792 	pbi->pre_stream_offset = 0;
2793 	cm->buffer_pool = &pbi->vp9_buffer_pool;
2794 	spin_lock_init(&cm->buffer_pool->lock);
2795 	cm->prev_fb_idx = INVALID_IDX;
2796 	cm->new_fb_idx = INVALID_IDX;
2797 	pbi->used_4k_num = -1;
2798 	cm->cur_fb_idx_mmu = INVALID_IDX;
2799 	pr_debug
2800 	("After vp9_bufmgr_init, prev_fb_idx : %d, new_fb_idx : %d\r\n",
2801 		cm->prev_fb_idx, cm->new_fb_idx);
2802 	pbi->need_resync = 1;
2803 	/* Initialize the references to not point to any frame buffers.*/
2804 	memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
2805 	memset(&cm->next_ref_frame_map, -1, sizeof(cm->next_ref_frame_map));
2806 	cm->current_video_frame = 0;
2807 	pbi->ready_for_new_data = 1;
2808 
2809 	/* private init */
2810 	pbi->work_space_buf = buf_spec_i;
2811 	if (!pbi->mmu_enable)
2812 		pbi->mc_buf = mc_buf_i;
2813 
2814 	pbi->rpm_addr = NULL;
2815 	pbi->lmem_addr = NULL;
2816 
2817 	pbi->use_cma_flag = 0;
2818 	pbi->decode_idx = 0;
2819 	pbi->slice_idx = 0;
2820 	/*int m_uiMaxCUWidth = 1<<7;*/
2821 	/*int m_uiMaxCUHeight = 1<<7;*/
2822 	pbi->has_keyframe = 0;
2823 	pbi->skip_flag = 0;
2824 	pbi->wait_buf = 0;
2825 	pbi->error_flag = 0;
2826 
2827 	pbi->pts_mode = PTS_NORMAL;
2828 	pbi->last_pts = 0;
2829 	pbi->last_lookup_pts = 0;
2830 	pbi->last_pts_us64 = 0;
2831 	pbi->last_lookup_pts_us64 = 0;
2832 	pbi->shift_byte_count = 0;
2833 	pbi->shift_byte_count_lo = 0;
2834 	pbi->shift_byte_count_hi = 0;
2835 	pbi->pts_mode_switching_count = 0;
2836 	pbi->pts_mode_recovery_count = 0;
2837 
2838 	pbi->buf_num = 0;
2839 	pbi->pic_num = 0;
2840 
2841 	return 0;
2842 }
2843 
vp9_bufmgr_postproc(struct VP9Decoder_s * pbi)2844 int vp9_bufmgr_postproc(struct VP9Decoder_s *pbi)
2845 {
2846 	struct VP9_Common_s *cm = &pbi->common;
2847 	struct PIC_BUFFER_CONFIG_s sd;
2848 
2849 	if (pbi->postproc_done)
2850 		return 0;
2851 	pbi->postproc_done = 1;
2852 	swap_frame_buffers(pbi);
2853 	if (!cm->show_existing_frame) {
2854 		cm->last_show_frame = cm->show_frame;
2855 		cm->prev_frame = cm->cur_frame;
2856 #if 0
2857 	if (cm->seg.enabled && !pbi->frame_parallel_decode)
2858 		vp9_swap_current_and_last_seg_map(cm);
2859 #endif
2860 	}
2861 	cm->last_width = cm->width;
2862 	cm->last_height = cm->height;
2863 	if (cm->show_frame)
2864 		cm->current_video_frame++;
2865 
2866 	if (vp9_get_raw_frame(pbi, &sd) == 0) {
2867 		/*pr_info("Display frame index %d\r\n", sd.index);*/
2868 		sd.stream_offset = pbi->pre_stream_offset;
2869 		prepare_display_buf(pbi, &sd);
2870 		pbi->pre_stream_offset = READ_VREG(HEVC_SHIFT_BYTE_COUNT);
2871 	}
2872 
2873 /* else
2874  *		pr_info
2875  *		("Not display this frame,ready_for_new_data%d show_frame%d\r\n",
2876  *		pbi->ready_for_new_data, cm->show_frame);
2877  */
2878 	return 0;
2879 }
2880 
2881 /**************************************************
2882  *
2883  *VP9 buffer management end
2884  *
2885  ***************************************************
2886  */
2887 
2888 
2889 #define HEVC_CM_BODY_START_ADDR                    0x3626
2890 #define HEVC_CM_BODY_LENGTH                        0x3627
2891 #define HEVC_CM_HEADER_LENGTH                      0x3629
2892 #define HEVC_CM_HEADER_OFFSET                      0x362b
2893 
2894 #define LOSLESS_COMPRESS_MODE
2895 
2896 /*#define DECOMP_HEADR_SURGENT*/
2897 #ifdef VP9_10B_NV21
2898 static u32 mem_map_mode = 2  /* 0:linear 1:32x32 2:64x32*/
2899 #else
2900 static u32 mem_map_mode; /* 0:linear 1:32x32 2:64x32 ; m8baby test1902 */
2901 #endif
2902 static u32 enable_mem_saving = 1;
2903 static u32 force_w_h;
2904 
2905 static u32 force_fps;
2906 
2907 
2908 const u32 vp9_version = 201602101;
2909 static u32 debug;
2910 static u32 radr;
2911 static u32 rval;
2912 static u32 pop_shorts;
2913 static u32 dbg_cmd;
2914 static u32 dbg_skip_decode_index;
2915 static u32 endian = 0xff0;
2916 #ifdef ERROR_HANDLE_DEBUG
2917 static u32 dbg_nal_skip_flag;
2918 		/* bit[0], skip vps; bit[1], skip sps; bit[2], skip pps */
2919 static u32 dbg_nal_skip_count;
2920 #endif
2921 /*for debug*/
2922 static u32 decode_pic_begin;
2923 static uint slice_parse_begin;
2924 static u32 step;
2925 #ifdef MIX_STREAM_SUPPORT
2926 static u32 buf_alloc_width = 4096;
2927 static u32 buf_alloc_height = 2304;
2928 static u32 vp9_max_pic_w = 4096;
2929 static u32 vp9_max_pic_h = 2304;
2930 
2931 static u32 dynamic_buf_num_margin;
2932 #else
2933 static u32 buf_alloc_width;
2934 static u32 buf_alloc_height;
2935 static u32 dynamic_buf_num_margin = 7;
2936 #endif
2937 static u32 buf_alloc_depth = 10;
2938 static u32 buf_alloc_size;
2939 /*
2940  *bit[0]: 0,
2941  *    bit[1]: 0, always release cma buffer when stop
2942  *    bit[1]: 1, never release cma buffer when stop
2943  *bit[0]: 1, when stop, release cma buffer if blackout is 1;
2944  *do not release cma buffer is blackout is not 1
2945  *
2946  *bit[2]: 0, when start decoding, check current displayed buffer
2947  *	 (only for buffer decoded by vp9) if blackout is 0
2948  *	 1, do not check current displayed buffer
2949  *
2950  *bit[3]: 1, if blackout is not 1, do not release current
2951  *			displayed cma buffer always.
2952  */
2953 /* set to 1 for fast play;
2954  *	set to 8 for other case of "keep last frame"
2955  */
2956 static u32 buffer_mode = 1;
2957 /* buffer_mode_dbg: debug only*/
2958 static u32 buffer_mode_dbg = 0xffff0000;
2959 /**/
2960 
2961 /*
2962  *bit 0, 1: only display I picture;
2963  *bit 1, 1: only decode I picture;
2964  */
2965 static u32 i_only_flag;
2966 
2967 static u32 low_latency_flag;
2968 
2969 static u32 no_head;
2970 
2971 static u32 max_decoding_time;
2972 /*
2973  *error handling
2974  */
2975 /*error_handle_policy:
2976  *bit 0: 0, auto skip error_skip_nal_count nals before error recovery;
2977  *1, skip error_skip_nal_count nals before error recovery;
2978  *bit 1 (valid only when bit0 == 1):
2979  *1, wait vps/sps/pps after error recovery;
2980  *bit 2 (valid only when bit0 == 0):
2981  *0, auto search after error recovery (vp9_recover() called);
2982  *1, manual search after error recovery
2983  *(change to auto search after get IDR: WRITE_VREG(NAL_SEARCH_CTL, 0x2))
2984  *
2985  *bit 4: 0, set error_mark after reset/recover
2986  *    1, do not set error_mark after reset/recover
2987  *bit 5: 0, check total lcu for every picture
2988  *    1, do not check total lcu
2989  *
2990  */
2991 
2992 static u32 error_handle_policy;
2993 /*static u32 parser_sei_enable = 1;*/
2994 #define MAX_BUF_NUM_NORMAL     12
2995 #define MAX_BUF_NUM_LESS   10
2996 static u32 max_buf_num = MAX_BUF_NUM_NORMAL;
2997 #define MAX_BUF_NUM_SAVE_BUF  8
2998 
2999 static u32 run_ready_min_buf_num = 2;
3000 
3001 
3002 static DEFINE_MUTEX(vvp9_mutex);
3003 #ifndef MULTI_INSTANCE_SUPPORT
3004 static struct device *cma_dev;
3005 #endif
3006 
3007 #define HEVC_DEC_STATUS_REG       HEVC_ASSIST_SCRATCH_0
3008 #define HEVC_RPM_BUFFER           HEVC_ASSIST_SCRATCH_1
3009 #define HEVC_SHORT_TERM_RPS       HEVC_ASSIST_SCRATCH_2
3010 #define VP9_ADAPT_PROB_REG        HEVC_ASSIST_SCRATCH_3
3011 #define VP9_MMU_MAP_BUFFER        HEVC_ASSIST_SCRATCH_4
3012 #define HEVC_PPS_BUFFER           HEVC_ASSIST_SCRATCH_5
3013 #define HEVC_SAO_UP               HEVC_ASSIST_SCRATCH_6
3014 #define HEVC_STREAM_SWAP_BUFFER   HEVC_ASSIST_SCRATCH_7
3015 #define HEVC_STREAM_SWAP_BUFFER2  HEVC_ASSIST_SCRATCH_8
3016 #define VP9_PROB_SWAP_BUFFER      HEVC_ASSIST_SCRATCH_9
3017 #define VP9_COUNT_SWAP_BUFFER     HEVC_ASSIST_SCRATCH_A
3018 #define VP9_SEG_MAP_BUFFER        HEVC_ASSIST_SCRATCH_B
3019 #define HEVC_SCALELUT             HEVC_ASSIST_SCRATCH_D
3020 #define HEVC_WAIT_FLAG            HEVC_ASSIST_SCRATCH_E
3021 #define RPM_CMD_REG               HEVC_ASSIST_SCRATCH_F
3022 #define LMEM_DUMP_ADR                 HEVC_ASSIST_SCRATCH_F
3023 #define HEVC_STREAM_SWAP_TEST     HEVC_ASSIST_SCRATCH_L
3024 #ifdef MULTI_INSTANCE_SUPPORT
3025 #define HEVC_DECODE_COUNT       HEVC_ASSIST_SCRATCH_M
3026 #define HEVC_DECODE_SIZE		HEVC_ASSIST_SCRATCH_N
3027 #else
3028 #define HEVC_DECODE_PIC_BEGIN_REG HEVC_ASSIST_SCRATCH_M
3029 #define HEVC_DECODE_PIC_NUM_REG   HEVC_ASSIST_SCRATCH_N
3030 #endif
3031 #define DEBUG_REG1              HEVC_ASSIST_SCRATCH_G
3032 #define DEBUG_REG2              HEVC_ASSIST_SCRATCH_H
3033 
3034 
3035 /*
3036  *ucode parser/search control
3037  *bit 0:  0, header auto parse; 1, header manual parse
3038  *bit 1:  0, auto skip for noneseamless stream; 1, no skip
3039  *bit [3:2]: valid when bit1==0;
3040  *0, auto skip nal before first vps/sps/pps/idr;
3041  *1, auto skip nal before first vps/sps/pps
3042  *2, auto skip nal before first  vps/sps/pps,
3043  *	and not decode until the first I slice (with slice address of 0)
3044  *
3045  *3, auto skip before first I slice (nal_type >=16 && nal_type<=21)
3046  *bit [15:4] nal skip count (valid when bit0 == 1 (manual mode) )
3047  *bit [16]: for NAL_UNIT_EOS when bit0 is 0:
3048  *	0, send SEARCH_DONE to arm ;  1, do not send SEARCH_DONE to arm
3049  *bit [17]: for NAL_SEI when bit0 is 0:
3050  *	0, do not parse SEI in ucode; 1, parse SEI in ucode
3051  *bit [31:20]: used by ucode for debug purpose
3052  */
3053 #define NAL_SEARCH_CTL            HEVC_ASSIST_SCRATCH_I
3054 	/*[31:24] chip feature
3055 		31: 0, use MBOX1; 1, use MBOX0
3056 	*/
3057 #define DECODE_MODE              HEVC_ASSIST_SCRATCH_J
3058 #define DECODE_STOP_POS         HEVC_ASSIST_SCRATCH_K
3059 
3060 #ifdef MULTI_INSTANCE_SUPPORT
3061 #define RPM_BUF_SIZE (0x400 * 2)
3062 #else
3063 #define RPM_BUF_SIZE (0x80*2)
3064 #endif
3065 #define LMEM_BUF_SIZE (0x400 * 2)
3066 
3067 #define WORK_BUF_SPEC_NUM 3
3068 static struct BuffInfo_s amvvp9_workbuff_spec[WORK_BUF_SPEC_NUM] = {
3069 	{
3070 		/* 8M bytes */
3071 		.max_width = 1920,
3072 		.max_height = 1088,
3073 		.ipp = {
3074 			/* IPP work space calculation :
3075 			 *   4096 * (Y+CbCr+Flags) = 12k, round to 16k
3076 			 */
3077 			.buf_size = 0x4000,
3078 		},
3079 		.sao_abv = {
3080 			.buf_size = 0x30000,
3081 		},
3082 		.sao_vb = {
3083 			.buf_size = 0x30000,
3084 		},
3085 		.short_term_rps = {
3086 			/* SHORT_TERM_RPS - Max 64 set, 16 entry every set,
3087 			 *   total 64x16x2 = 2048 bytes (0x800)
3088 			 */
3089 			.buf_size = 0x800,
3090 		},
3091 		.vps = {
3092 			/* VPS STORE AREA - Max 16 VPS, each has 0x80 bytes,
3093 			 *   total 0x0800 bytes
3094 			 */
3095 			.buf_size = 0x800,
3096 		},
3097 		.sps = {
3098 			/* SPS STORE AREA - Max 16 SPS, each has 0x80 bytes,
3099 			 *   total 0x0800 bytes
3100 			 */
3101 			.buf_size = 0x800,
3102 		},
3103 		.pps = {
3104 			/* PPS STORE AREA - Max 64 PPS, each has 0x80 bytes,
3105 			 *   total 0x2000 bytes
3106 			 */
3107 			.buf_size = 0x2000,
3108 		},
3109 		.sao_up = {
3110 			/* SAO UP STORE AREA - Max 640(10240/16) LCU,
3111 			 *   each has 16 bytes total 0x2800 bytes
3112 			 */
3113 			.buf_size = 0x2800,
3114 		},
3115 		.swap_buf = {
3116 			/* 256cyclex64bit = 2K bytes 0x800
3117 			 *   (only 144 cycles valid)
3118 			 */
3119 			.buf_size = 0x800,
3120 		},
3121 		.swap_buf2 = {
3122 			.buf_size = 0x800,
3123 		},
3124 		.scalelut = {
3125 			/* support up to 32 SCALELUT 1024x32 =
3126 			 *   32Kbytes (0x8000)
3127 			 */
3128 			.buf_size = 0x8000,
3129 		},
3130 		.dblk_para = {
3131 			/* DBLK -> Max 256(4096/16) LCU,
3132 			 *each para 1024bytes(total:0x40000),
3133 			 *data 1024bytes(total:0x40000)
3134 			 */
3135 			.buf_size = 0x80000,
3136 		},
3137 		.dblk_data = {
3138 			.buf_size = 0x80000,
3139 		},
3140 		.seg_map = {
3141 		/*4096x2304/64/64 *24 = 0xd800 Bytes*/
3142 			.buf_size = 0xd800,
3143 		},
3144 		.mmu_vbh = {
3145 			.buf_size = 0x5000, /*2*16*(more than 2304)/4, 4K*/
3146 		},
3147 #if 0
3148 		.cm_header = {
3149 			/*add one for keeper.*/
3150 			.buf_size = MMU_COMPRESS_HEADER_SIZE *
3151 						(FRAME_BUFFERS + 1),
3152 			/* 0x44000 = ((1088*2*1024*4)/32/4)*(32/8) */
3153 		},
3154 #endif
3155 		.mpred_above = {
3156 			.buf_size = 0x10000, /* 2 * size of hevc*/
3157 		},
3158 #ifdef MV_USE_FIXED_BUF
3159 		.mpred_mv = {/* 1080p, 0x40000 per buffer */
3160 			.buf_size = 0x40000 * FRAME_BUFFERS,
3161 		},
3162 #endif
3163 		.rpm = {
3164 			.buf_size = RPM_BUF_SIZE,
3165 		},
3166 		.lmem = {
3167 			.buf_size = 0x400 * 2,
3168 		}
3169 	},
3170 	{
3171 		.max_width = 4096,
3172 		.max_height = 2304,
3173 		.ipp = {
3174 			/* IPP work space calculation :
3175 			 *   4096 * (Y+CbCr+Flags) = 12k, round to 16k
3176 			 */
3177 			.buf_size = 0x4000,
3178 		},
3179 		.sao_abv = {
3180 			.buf_size = 0x30000,
3181 		},
3182 		.sao_vb = {
3183 			.buf_size = 0x30000,
3184 		},
3185 		.short_term_rps = {
3186 			/* SHORT_TERM_RPS - Max 64 set, 16 entry every set,
3187 			 *   total 64x16x2 = 2048 bytes (0x800)
3188 			 */
3189 			.buf_size = 0x800,
3190 		},
3191 		.vps = {
3192 			/* VPS STORE AREA - Max 16 VPS, each has 0x80 bytes,
3193 			 *   total 0x0800 bytes
3194 			 */
3195 			.buf_size = 0x800,
3196 		},
3197 		.sps = {
3198 			/* SPS STORE AREA - Max 16 SPS, each has 0x80 bytes,
3199 			 *   total 0x0800 bytes
3200 			 */
3201 			.buf_size = 0x800,
3202 		},
3203 		.pps = {
3204 			/* PPS STORE AREA - Max 64 PPS, each has 0x80 bytes,
3205 			 *   total 0x2000 bytes
3206 			 */
3207 			.buf_size = 0x2000,
3208 		},
3209 		.sao_up = {
3210 			/* SAO UP STORE AREA - Max 640(10240/16) LCU,
3211 			 *   each has 16 bytes total 0x2800 bytes
3212 			 */
3213 			.buf_size = 0x2800,
3214 		},
3215 		.swap_buf = {
3216 			/* 256cyclex64bit = 2K bytes 0x800
3217 			 *   (only 144 cycles valid)
3218 			 */
3219 			.buf_size = 0x800,
3220 		},
3221 		.swap_buf2 = {
3222 			.buf_size = 0x800,
3223 		},
3224 		.scalelut = {
3225 			/* support up to 32 SCALELUT 1024x32 = 32Kbytes
3226 			 *   (0x8000)
3227 			 */
3228 			.buf_size = 0x8000,
3229 		},
3230 		.dblk_para = {
3231 			/* DBLK -> Max 256(4096/16) LCU,
3232 			 *each para 1024bytes(total:0x40000),
3233 			 *data 1024bytes(total:0x40000)
3234 			 */
3235 			.buf_size = 0x80000,
3236 		},
3237 		.dblk_data = {
3238 			.buf_size = 0x80000,
3239 		},
3240 		.seg_map = {
3241 			/*4096x2304/64/64 *24 = 0xd800 Bytes*/
3242 			.buf_size = 0xd800,
3243 		},
3244 		.mmu_vbh = {
3245 			.buf_size = 0x5000,/*2*16*(more than 2304)/4, 4K*/
3246 		},
3247 #if 0
3248 		.cm_header = {
3249 			/*add one for keeper.*/
3250 			.buf_size = MMU_COMPRESS_HEADER_SIZE *
3251 						(FRAME_BUFFERS + 1),
3252 			/* 0x44000 = ((1088*2*1024*4)/32/4)*(32/8) */
3253 		},
3254 #endif
3255 		.mpred_above = {
3256 			.buf_size = 0x10000, /* 2 * size of hevc*/
3257 		},
3258 #ifdef MV_USE_FIXED_BUF
3259 		.mpred_mv = {
3260 			/* .buf_size = 0x100000*16,
3261 			 * //4k2k , 0x100000 per buffer
3262 			 */
3263 			/* 4096x2304 , 0x120000 per buffer */
3264 			.buf_size = 0x120000 * FRAME_BUFFERS,
3265 		},
3266 #endif
3267 		.rpm = {
3268 			.buf_size = RPM_BUF_SIZE,
3269 		},
3270 		.lmem = {
3271 			.buf_size = 0x400 * 2,
3272 		}
3273 	},
3274 	{
3275 		.max_width = 4096*2,
3276 		.max_height = 2304*2,
3277 		.ipp = {
3278 			// IPP work space calculation : 4096 * (Y+CbCr+Flags) = 12k, round to 16k
3279 			.buf_size = 0x4000*2,
3280 		},
3281 		.sao_abv = {
3282 			.buf_size = 0x30000*2,
3283 		},
3284 		.sao_vb = {
3285 			.buf_size = 0x30000*2,
3286 		},
3287 		.short_term_rps = {
3288 			// SHORT_TERM_RPS - Max 64 set, 16 entry every set, total 64x16x2 = 2048 bytes (0x800)
3289 			.buf_size = 0x800,
3290 		},
3291 		.vps = {
3292 			// VPS STORE AREA - Max 16 VPS, each has 0x80 bytes, total 0x0800 bytes
3293 			.buf_size = 0x800,
3294 		},
3295 		.sps = {
3296 			// SPS STORE AREA - Max 16 SPS, each has 0x80 bytes, total 0x0800 bytes
3297 			.buf_size = 0x800,
3298 		},
3299 		.pps = {
3300 			// PPS STORE AREA - Max 64 PPS, each has 0x80 bytes, total 0x2000 bytes
3301 			.buf_size = 0x2000,
3302 		},
3303 		.sao_up = {
3304 			// SAO UP STORE AREA - Max 640(10240/16) LCU, each has 16 bytes total 0x2800 bytes
3305 			.buf_size = 0x2800*2,
3306 		},
3307 		.swap_buf = {
3308 			// 256cyclex64bit = 2K bytes 0x800 (only 144 cycles valid)
3309 			.buf_size = 0x800,
3310 		},
3311 		.swap_buf2 = {
3312 			.buf_size = 0x800,
3313 		},
3314 		.scalelut = {
3315 			// support up to 32 SCALELUT 1024x32 = 32Kbytes (0x8000)
3316 			.buf_size = 0x8000*2,
3317 		},
3318 		.dblk_para = {
3319 			// DBLK -> Max 256(4096/16) LCU, each para 1024bytes(total:0x40000), data 1024bytes(total:0x40000)
3320 			.buf_size = 0x80000*2,
3321 		},
3322 		.dblk_data = {
3323 			.buf_size = 0x80000*2,
3324 		},
3325 		.seg_map = {
3326 			/*4096x2304/64/64 *24 = 0xd800 Bytes*/
3327 			.buf_size = 0xd800*4,
3328 		},
3329 		.mmu_vbh = {
3330 			.buf_size = 0x5000*2, //2*16*(more than 2304)/4, 4K
3331 		},
3332 #if 0
3333 		.cm_header = {
3334 			//.buf_size = MMU_COMPRESS_HEADER_SIZE*8, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
3335 			.buf_size = MMU_COMPRESS_HEADER_SIZE*16, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
3336 		},
3337 #endif
3338 		.mpred_above = {
3339 			.buf_size = 0x10000*2, /* 2 * size of hevc*/
3340 		},
3341 #ifdef MV_USE_FIXED_BUF
3342 		.mpred_mv = {
3343 			//4k2k , 0x100000 per buffer */
3344 			/* 4096x2304 , 0x120000 per buffer */
3345 			.buf_size = 0x120000 * FRAME_BUFFERS * 4,
3346 		},
3347 #endif
3348 		.rpm = {
3349 			.buf_size = RPM_BUF_SIZE,
3350 		},
3351 		.lmem = {
3352 			.buf_size = 0x400 * 2,
3353 		}
3354 	}
3355 };
3356 
3357 
3358 /*Losless compression body buffer size 4K per 64x32 (jt)*/
compute_losless_comp_body_size(int width,int height,uint8_t is_bit_depth_10)3359 int  compute_losless_comp_body_size(int width, int height,
3360 				uint8_t is_bit_depth_10)
3361 {
3362 	int     width_x64;
3363 	int     height_x32;
3364 	int     bsize;
3365 
3366 	width_x64 = width + 63;
3367 	width_x64 >>= 6;
3368 	height_x32 = height + 31;
3369 	height_x32 >>= 5;
3370 	bsize = (is_bit_depth_10?4096:3200)*width_x64*height_x32;
3371 	if (debug & VP9_DEBUG_BUFMGR_MORE)
3372 		pr_info("%s(%d,%d,%d)=>%d\n",
3373 			__func__, width, height,
3374 			is_bit_depth_10, bsize);
3375 
3376 	return  bsize;
3377 }
3378 
3379 /* Losless compression header buffer size 32bytes per 128x64 (jt)*/
compute_losless_comp_header_size(int width,int height)3380 static  int  compute_losless_comp_header_size(int width, int height)
3381 {
3382 	int     width_x128;
3383 	int     height_x64;
3384 	int     hsize;
3385 
3386 	width_x128 = width + 127;
3387 	width_x128 >>= 7;
3388 	height_x64 = height + 63;
3389 	height_x64 >>= 6;
3390 
3391 	hsize = 32 * width_x128 * height_x64;
3392 	if (debug & VP9_DEBUG_BUFMGR_MORE)
3393 		pr_info("%s(%d,%d)=>%d\n",
3394 			__func__, width, height,
3395 			hsize);
3396 
3397 	return  hsize;
3398 }
3399 
init_buff_spec(struct VP9Decoder_s * pbi,struct BuffInfo_s * buf_spec)3400 static void init_buff_spec(struct VP9Decoder_s *pbi,
3401 	struct BuffInfo_s *buf_spec)
3402 {
3403 	void *mem_start_virt;
3404 
3405 	buf_spec->ipp.buf_start = buf_spec->start_adr;
3406 	buf_spec->sao_abv.buf_start =
3407 		buf_spec->ipp.buf_start + buf_spec->ipp.buf_size;
3408 
3409 	buf_spec->sao_vb.buf_start =
3410 		buf_spec->sao_abv.buf_start + buf_spec->sao_abv.buf_size;
3411 	buf_spec->short_term_rps.buf_start =
3412 		buf_spec->sao_vb.buf_start + buf_spec->sao_vb.buf_size;
3413 	buf_spec->vps.buf_start =
3414 		buf_spec->short_term_rps.buf_start +
3415 		buf_spec->short_term_rps.buf_size;
3416 	buf_spec->sps.buf_start =
3417 		buf_spec->vps.buf_start + buf_spec->vps.buf_size;
3418 	buf_spec->pps.buf_start =
3419 		buf_spec->sps.buf_start + buf_spec->sps.buf_size;
3420 	buf_spec->sao_up.buf_start =
3421 		buf_spec->pps.buf_start + buf_spec->pps.buf_size;
3422 	buf_spec->swap_buf.buf_start =
3423 		buf_spec->sao_up.buf_start + buf_spec->sao_up.buf_size;
3424 	buf_spec->swap_buf2.buf_start =
3425 		buf_spec->swap_buf.buf_start + buf_spec->swap_buf.buf_size;
3426 	buf_spec->scalelut.buf_start =
3427 		buf_spec->swap_buf2.buf_start + buf_spec->swap_buf2.buf_size;
3428 	buf_spec->dblk_para.buf_start =
3429 		buf_spec->scalelut.buf_start + buf_spec->scalelut.buf_size;
3430 	buf_spec->dblk_data.buf_start =
3431 		buf_spec->dblk_para.buf_start + buf_spec->dblk_para.buf_size;
3432 	buf_spec->seg_map.buf_start =
3433 	buf_spec->dblk_data.buf_start + buf_spec->dblk_data.buf_size;
3434 	if (pbi == NULL || pbi->mmu_enable) {
3435 		buf_spec->mmu_vbh.buf_start  =
3436 			buf_spec->seg_map.buf_start +
3437 			buf_spec->seg_map.buf_size;
3438 		buf_spec->mpred_above.buf_start =
3439 			buf_spec->mmu_vbh.buf_start +
3440 			buf_spec->mmu_vbh.buf_size;
3441 	} else {
3442 		buf_spec->mpred_above.buf_start =
3443 		buf_spec->seg_map.buf_start + buf_spec->seg_map.buf_size;
3444 	}
3445 #ifdef MV_USE_FIXED_BUF
3446 	buf_spec->mpred_mv.buf_start =
3447 		buf_spec->mpred_above.buf_start +
3448 		buf_spec->mpred_above.buf_size;
3449 
3450 	buf_spec->rpm.buf_start =
3451 		buf_spec->mpred_mv.buf_start +
3452 		buf_spec->mpred_mv.buf_size;
3453 #else
3454 	buf_spec->rpm.buf_start =
3455 		buf_spec->mpred_above.buf_start +
3456 		buf_spec->mpred_above.buf_size;
3457 
3458 #endif
3459 	buf_spec->lmem.buf_start =
3460 		buf_spec->rpm.buf_start +
3461 		buf_spec->rpm.buf_size;
3462 	buf_spec->end_adr =
3463 		buf_spec->lmem.buf_start +
3464 		buf_spec->lmem.buf_size;
3465 
3466 	if (!pbi)
3467 		return;
3468 
3469 	if (!vdec_secure(hw_to_vdec(pbi))) {
3470 		mem_start_virt =
3471 			codec_mm_phys_to_virt(buf_spec->dblk_para.buf_start);
3472 		if (mem_start_virt) {
3473 			memset(mem_start_virt, 0,
3474 				buf_spec->dblk_para.buf_size);
3475 			codec_mm_dma_flush(mem_start_virt,
3476 				buf_spec->dblk_para.buf_size,
3477 				DMA_TO_DEVICE);
3478 		} else {
3479 			mem_start_virt = codec_mm_vmap(
3480 				buf_spec->dblk_para.buf_start,
3481 				buf_spec->dblk_para.buf_size);
3482 			if (mem_start_virt) {
3483 				memset(mem_start_virt, 0,
3484 					buf_spec->dblk_para.buf_size);
3485 				codec_mm_dma_flush(mem_start_virt,
3486 					buf_spec->dblk_para.buf_size,
3487 					DMA_TO_DEVICE);
3488 				codec_mm_unmap_phyaddr(mem_start_virt);
3489 			} else {
3490 				/*not virt for tvp playing,
3491 				may need clear on ucode.*/
3492 				pr_err("mem_start_virt failed\n");
3493 			}
3494 		}
3495 	}
3496 
3497 	if (debug) {
3498 		pr_info("%s workspace (%x %x) size = %x\n", __func__,
3499 			   buf_spec->start_adr, buf_spec->end_adr,
3500 			   buf_spec->end_adr - buf_spec->start_adr);
3501 	}
3502 
3503 	if (debug) {
3504 		pr_info("ipp.buf_start             :%x\n",
3505 			   buf_spec->ipp.buf_start);
3506 		pr_info("sao_abv.buf_start          :%x\n",
3507 			   buf_spec->sao_abv.buf_start);
3508 		pr_info("sao_vb.buf_start          :%x\n",
3509 			   buf_spec->sao_vb.buf_start);
3510 		pr_info("short_term_rps.buf_start  :%x\n",
3511 			   buf_spec->short_term_rps.buf_start);
3512 		pr_info("vps.buf_start             :%x\n",
3513 			   buf_spec->vps.buf_start);
3514 		pr_info("sps.buf_start             :%x\n",
3515 			   buf_spec->sps.buf_start);
3516 		pr_info("pps.buf_start             :%x\n",
3517 			   buf_spec->pps.buf_start);
3518 		pr_info("sao_up.buf_start          :%x\n",
3519 			   buf_spec->sao_up.buf_start);
3520 		pr_info("swap_buf.buf_start        :%x\n",
3521 			   buf_spec->swap_buf.buf_start);
3522 		pr_info("swap_buf2.buf_start       :%x\n",
3523 			   buf_spec->swap_buf2.buf_start);
3524 		pr_info("scalelut.buf_start        :%x\n",
3525 			   buf_spec->scalelut.buf_start);
3526 		pr_info("dblk_para.buf_start       :%x\n",
3527 			   buf_spec->dblk_para.buf_start);
3528 		pr_info("dblk_data.buf_start       :%x\n",
3529 			   buf_spec->dblk_data.buf_start);
3530 		pr_info("seg_map.buf_start       :%x\n",
3531 			buf_spec->seg_map.buf_start);
3532 	if (pbi->mmu_enable) {
3533 		pr_info("mmu_vbh.buf_start     :%x\n",
3534 			buf_spec->mmu_vbh.buf_start);
3535 	}
3536 		pr_info("mpred_above.buf_start     :%x\n",
3537 			   buf_spec->mpred_above.buf_start);
3538 #ifdef MV_USE_FIXED_BUF
3539 		pr_info("mpred_mv.buf_start        :%x\n",
3540 			   buf_spec->mpred_mv.buf_start);
3541 #endif
3542 		if ((debug & VP9_DEBUG_SEND_PARAM_WITH_REG) == 0) {
3543 			pr_info("rpm.buf_start             :%x\n",
3544 				   buf_spec->rpm.buf_start);
3545 		}
3546 	}
3547 }
3548 
3549 /* cache_util.c */
3550 #define   THODIYIL_MCRCC_CANVAS_ALGX    4
3551 
3552 static u32 mcrcc_cache_alg_flag = THODIYIL_MCRCC_CANVAS_ALGX;
3553 
mcrcc_perfcount_reset(void)3554 static void mcrcc_perfcount_reset(void)
3555 {
3556 	if (debug & VP9_DEBUG_CACHE)
3557 		pr_info("[cache_util.c] Entered mcrcc_perfcount_reset...\n");
3558 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x1);
3559 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x0);
3560 	return;
3561 }
3562 
3563 static unsigned raw_mcr_cnt_total_prev;
3564 static unsigned hit_mcr_0_cnt_total_prev;
3565 static unsigned hit_mcr_1_cnt_total_prev;
3566 static unsigned byp_mcr_cnt_nchcanv_total_prev;
3567 static unsigned byp_mcr_cnt_nchoutwin_total_prev;
3568 
mcrcc_get_hitrate(unsigned reset_pre)3569 static void mcrcc_get_hitrate(unsigned reset_pre)
3570 {
3571 	unsigned delta_hit_mcr_0_cnt;
3572 	unsigned delta_hit_mcr_1_cnt;
3573 	unsigned delta_raw_mcr_cnt;
3574 	unsigned delta_mcr_cnt_nchcanv;
3575 	unsigned delta_mcr_cnt_nchoutwin;
3576 
3577 	unsigned tmp;
3578 	unsigned raw_mcr_cnt;
3579 	unsigned hit_mcr_cnt;
3580 	unsigned byp_mcr_cnt_nchoutwin;
3581 	unsigned byp_mcr_cnt_nchcanv;
3582 	int hitrate;
3583 	if (reset_pre) {
3584 		raw_mcr_cnt_total_prev = 0;
3585 		hit_mcr_0_cnt_total_prev = 0;
3586 		hit_mcr_1_cnt_total_prev = 0;
3587 		byp_mcr_cnt_nchcanv_total_prev = 0;
3588 		byp_mcr_cnt_nchoutwin_total_prev = 0;
3589 	}
3590 	if (debug & VP9_DEBUG_CACHE)
3591 		pr_info("[cache_util.c] Entered mcrcc_get_hitrate...\n");
3592 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x0<<1));
3593 	raw_mcr_cnt = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3594 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x1<<1));
3595 	hit_mcr_cnt = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3596 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x2<<1));
3597 	byp_mcr_cnt_nchoutwin = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3598 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x3<<1));
3599 	byp_mcr_cnt_nchcanv = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3600 
3601 	if (debug & VP9_DEBUG_CACHE)
3602 		pr_info("raw_mcr_cnt_total: %d\n",
3603 			raw_mcr_cnt);
3604 	if (debug & VP9_DEBUG_CACHE)
3605 		pr_info("hit_mcr_cnt_total: %d\n",
3606 			hit_mcr_cnt);
3607 	if (debug & VP9_DEBUG_CACHE)
3608 		pr_info("byp_mcr_cnt_nchoutwin_total: %d\n",
3609 			byp_mcr_cnt_nchoutwin);
3610 	if (debug & VP9_DEBUG_CACHE)
3611 		pr_info("byp_mcr_cnt_nchcanv_total: %d\n",
3612 			byp_mcr_cnt_nchcanv);
3613 
3614 	delta_raw_mcr_cnt = raw_mcr_cnt -
3615 		raw_mcr_cnt_total_prev;
3616 	delta_mcr_cnt_nchcanv = byp_mcr_cnt_nchcanv -
3617 		byp_mcr_cnt_nchcanv_total_prev;
3618 	delta_mcr_cnt_nchoutwin = byp_mcr_cnt_nchoutwin -
3619 		byp_mcr_cnt_nchoutwin_total_prev;
3620 	raw_mcr_cnt_total_prev = raw_mcr_cnt;
3621 	byp_mcr_cnt_nchcanv_total_prev = byp_mcr_cnt_nchcanv;
3622 	byp_mcr_cnt_nchoutwin_total_prev = byp_mcr_cnt_nchoutwin;
3623 
3624 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x4<<1));
3625 	tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3626 	if (debug & VP9_DEBUG_CACHE)
3627 		pr_info("miss_mcr_0_cnt_total: %d\n", tmp);
3628 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x5<<1));
3629 	tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3630 	if (debug & VP9_DEBUG_CACHE)
3631 		pr_info("miss_mcr_1_cnt_total: %d\n", tmp);
3632 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x6<<1));
3633 	tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3634 	if (debug & VP9_DEBUG_CACHE)
3635 		pr_info("hit_mcr_0_cnt_total: %d\n", tmp);
3636 	delta_hit_mcr_0_cnt = tmp - hit_mcr_0_cnt_total_prev;
3637 	hit_mcr_0_cnt_total_prev = tmp;
3638 	WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x7<<1));
3639 	tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
3640 	if (debug & VP9_DEBUG_CACHE)
3641 		pr_info("hit_mcr_1_cnt_total: %d\n", tmp);
3642 	delta_hit_mcr_1_cnt = tmp - hit_mcr_1_cnt_total_prev;
3643 	hit_mcr_1_cnt_total_prev = tmp;
3644 
3645 	if (delta_raw_mcr_cnt != 0) {
3646 		hitrate = 100 * delta_hit_mcr_0_cnt
3647 			/ delta_raw_mcr_cnt;
3648 		if (debug & VP9_DEBUG_CACHE)
3649 			pr_info("CANV0_HIT_RATE : %d\n", hitrate);
3650 		hitrate = 100 * delta_hit_mcr_1_cnt
3651 			/ delta_raw_mcr_cnt;
3652 		if (debug & VP9_DEBUG_CACHE)
3653 			pr_info("CANV1_HIT_RATE : %d\n", hitrate);
3654 		hitrate = 100 * delta_mcr_cnt_nchcanv
3655 			/ delta_raw_mcr_cnt;
3656 		if (debug & VP9_DEBUG_CACHE)
3657 			pr_info("NONCACH_CANV_BYP_RATE : %d\n", hitrate);
3658 		hitrate = 100 * delta_mcr_cnt_nchoutwin
3659 			/ delta_raw_mcr_cnt;
3660 		if (debug & VP9_DEBUG_CACHE)
3661 			pr_info("CACHE_OUTWIN_BYP_RATE : %d\n", hitrate);
3662 	}
3663 
3664 
3665 	if (raw_mcr_cnt != 0) {
3666 		hitrate = 100 * hit_mcr_cnt / raw_mcr_cnt;
3667 		if (debug & VP9_DEBUG_CACHE)
3668 			pr_info("MCRCC_HIT_RATE : %d\n", hitrate);
3669 		hitrate = 100 * (byp_mcr_cnt_nchoutwin + byp_mcr_cnt_nchcanv)
3670 			/ raw_mcr_cnt;
3671 		if (debug & VP9_DEBUG_CACHE)
3672 			pr_info("MCRCC_BYP_RATE : %d\n", hitrate);
3673 	} else {
3674 		if (debug & VP9_DEBUG_CACHE)
3675 			pr_info("MCRCC_HIT_RATE : na\n");
3676 		if (debug & VP9_DEBUG_CACHE)
3677 			pr_info("MCRCC_BYP_RATE : na\n");
3678 	}
3679 	return;
3680 }
3681 
3682 
decomp_perfcount_reset(void)3683 static void decomp_perfcount_reset(void)
3684 {
3685 	if (debug & VP9_DEBUG_CACHE)
3686 		pr_info("[cache_util.c] Entered decomp_perfcount_reset...\n");
3687 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x1);
3688 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x0);
3689 	return;
3690 }
3691 
decomp_get_hitrate(void)3692 static void decomp_get_hitrate(void)
3693 {
3694 	unsigned   raw_mcr_cnt;
3695 	unsigned   hit_mcr_cnt;
3696 	int      hitrate;
3697 	if (debug & VP9_DEBUG_CACHE)
3698 		pr_info("[cache_util.c] Entered decomp_get_hitrate...\n");
3699 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x0<<1));
3700 	raw_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3701 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x1<<1));
3702 	hit_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3703 
3704 	if (debug & VP9_DEBUG_CACHE)
3705 		pr_info("hcache_raw_cnt_total: %d\n", raw_mcr_cnt);
3706 	if (debug & VP9_DEBUG_CACHE)
3707 		pr_info("hcache_hit_cnt_total: %d\n", hit_mcr_cnt);
3708 
3709 	if (raw_mcr_cnt != 0) {
3710 		hitrate = hit_mcr_cnt * 100 / raw_mcr_cnt;
3711 		if (debug & VP9_DEBUG_CACHE)
3712 			pr_info("DECOMP_HCACHE_HIT_RATE : %d\n", hitrate);
3713 	} else {
3714 		if (debug & VP9_DEBUG_CACHE)
3715 			pr_info("DECOMP_HCACHE_HIT_RATE : na\n");
3716 	}
3717 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x2<<1));
3718 	raw_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3719 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x3<<1));
3720 	hit_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3721 
3722 	if (debug & VP9_DEBUG_CACHE)
3723 		pr_info("dcache_raw_cnt_total: %d\n", raw_mcr_cnt);
3724 	if (debug & VP9_DEBUG_CACHE)
3725 		pr_info("dcache_hit_cnt_total: %d\n", hit_mcr_cnt);
3726 
3727 	if (raw_mcr_cnt != 0) {
3728 		hitrate = hit_mcr_cnt * 100 / raw_mcr_cnt;
3729 		if (debug & VP9_DEBUG_CACHE)
3730 			pr_info("DECOMP_DCACHE_HIT_RATE : %d\n", hitrate);
3731 	} else {
3732 		if (debug & VP9_DEBUG_CACHE)
3733 			pr_info("DECOMP_DCACHE_HIT_RATE : na\n");
3734 	}
3735 	return;
3736 }
3737 
decomp_get_comprate(void)3738 static void decomp_get_comprate(void)
3739 {
3740 	unsigned   raw_ucomp_cnt;
3741 	unsigned   fast_comp_cnt;
3742 	unsigned   slow_comp_cnt;
3743 	int      comprate;
3744 
3745 	if (debug & VP9_DEBUG_CACHE)
3746 		pr_info("[cache_util.c] Entered decomp_get_comprate...\n");
3747 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x4<<1));
3748 	fast_comp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3749 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x5<<1));
3750 	slow_comp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3751 	WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x6<<1));
3752 	raw_ucomp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
3753 
3754 	if (debug & VP9_DEBUG_CACHE)
3755 		pr_info("decomp_fast_comp_total: %d\n", fast_comp_cnt);
3756 	if (debug & VP9_DEBUG_CACHE)
3757 		pr_info("decomp_slow_comp_total: %d\n", slow_comp_cnt);
3758 	if (debug & VP9_DEBUG_CACHE)
3759 		pr_info("decomp_raw_uncomp_total: %d\n", raw_ucomp_cnt);
3760 
3761 	if (raw_ucomp_cnt != 0) {
3762 		comprate = (fast_comp_cnt + slow_comp_cnt)
3763 		* 100 / raw_ucomp_cnt;
3764 		if (debug & VP9_DEBUG_CACHE)
3765 			pr_info("DECOMP_COMP_RATIO : %d\n", comprate);
3766 	} else {
3767 		if (debug & VP9_DEBUG_CACHE)
3768 			pr_info("DECOMP_COMP_RATIO : na\n");
3769 	}
3770 	return;
3771 }
3772 /* cache_util.c end */
3773 
3774 /*====================================================
3775  *========================================================================
3776  *vp9_prob define
3777  *========================================================================
3778  */
3779 #define VP9_PARTITION_START      0
3780 #define VP9_PARTITION_SIZE_STEP  (3 * 4)
3781 #define VP9_PARTITION_ONE_SIZE   (4 * VP9_PARTITION_SIZE_STEP)
3782 #define VP9_PARTITION_KEY_START  0
3783 #define VP9_PARTITION_P_START    VP9_PARTITION_ONE_SIZE
3784 #define VP9_PARTITION_SIZE       (2 * VP9_PARTITION_ONE_SIZE)
3785 #define VP9_SKIP_START           (VP9_PARTITION_START + VP9_PARTITION_SIZE)
3786 #define VP9_SKIP_SIZE            4 /* only use 3*/
3787 #define VP9_TX_MODE_START        (VP9_SKIP_START+VP9_SKIP_SIZE)
3788 #define VP9_TX_MODE_8_0_OFFSET   0
3789 #define VP9_TX_MODE_8_1_OFFSET   1
3790 #define VP9_TX_MODE_16_0_OFFSET  2
3791 #define VP9_TX_MODE_16_1_OFFSET  4
3792 #define VP9_TX_MODE_32_0_OFFSET  6
3793 #define VP9_TX_MODE_32_1_OFFSET  9
3794 #define VP9_TX_MODE_SIZE         12
3795 #define VP9_COEF_START           (VP9_TX_MODE_START+VP9_TX_MODE_SIZE)
3796 #define VP9_COEF_BAND_0_OFFSET   0
3797 #define VP9_COEF_BAND_1_OFFSET   (VP9_COEF_BAND_0_OFFSET + 3 * 3 + 1)
3798 #define VP9_COEF_BAND_2_OFFSET   (VP9_COEF_BAND_1_OFFSET + 6 * 3)
3799 #define VP9_COEF_BAND_3_OFFSET   (VP9_COEF_BAND_2_OFFSET + 6 * 3)
3800 #define VP9_COEF_BAND_4_OFFSET   (VP9_COEF_BAND_3_OFFSET + 6 * 3)
3801 #define VP9_COEF_BAND_5_OFFSET   (VP9_COEF_BAND_4_OFFSET + 6 * 3)
3802 #define VP9_COEF_SIZE_ONE_SET    100 /* ((3 +5*6)*3 + 1 padding)*/
3803 #define VP9_COEF_4X4_START       (VP9_COEF_START + 0 * VP9_COEF_SIZE_ONE_SET)
3804 #define VP9_COEF_8X8_START       (VP9_COEF_START + 4 * VP9_COEF_SIZE_ONE_SET)
3805 #define VP9_COEF_16X16_START     (VP9_COEF_START + 8 * VP9_COEF_SIZE_ONE_SET)
3806 #define VP9_COEF_32X32_START     (VP9_COEF_START + 12 * VP9_COEF_SIZE_ONE_SET)
3807 #define VP9_COEF_SIZE_PLANE      (2 * VP9_COEF_SIZE_ONE_SET)
3808 #define VP9_COEF_SIZE            (4 * 2 * 2 * VP9_COEF_SIZE_ONE_SET)
3809 #define VP9_INTER_MODE_START     (VP9_COEF_START+VP9_COEF_SIZE)
3810 #define VP9_INTER_MODE_SIZE      24 /* only use 21 ( #*7)*/
3811 #define VP9_INTERP_START         (VP9_INTER_MODE_START+VP9_INTER_MODE_SIZE)
3812 #define VP9_INTERP_SIZE          8
3813 #define VP9_INTRA_INTER_START    (VP9_INTERP_START+VP9_INTERP_SIZE)
3814 #define VP9_INTRA_INTER_SIZE     4
3815 #define VP9_INTERP_INTRA_INTER_START  VP9_INTERP_START
3816 #define VP9_INTERP_INTRA_INTER_SIZE   (VP9_INTERP_SIZE + VP9_INTRA_INTER_SIZE)
3817 #define VP9_COMP_INTER_START     \
3818 		(VP9_INTERP_INTRA_INTER_START+VP9_INTERP_INTRA_INTER_SIZE)
3819 #define VP9_COMP_INTER_SIZE      5
3820 #define VP9_COMP_REF_START       (VP9_COMP_INTER_START+VP9_COMP_INTER_SIZE)
3821 #define VP9_COMP_REF_SIZE        5
3822 #define VP9_SINGLE_REF_START     (VP9_COMP_REF_START+VP9_COMP_REF_SIZE)
3823 #define VP9_SINGLE_REF_SIZE      10
3824 #define VP9_REF_MODE_START       VP9_COMP_INTER_START
3825 #define VP9_REF_MODE_SIZE        \
3826 		(VP9_COMP_INTER_SIZE+VP9_COMP_REF_SIZE+VP9_SINGLE_REF_SIZE)
3827 #define VP9_IF_Y_MODE_START      (VP9_REF_MODE_START+VP9_REF_MODE_SIZE)
3828 #define VP9_IF_Y_MODE_SIZE       36
3829 #define VP9_IF_UV_MODE_START     (VP9_IF_Y_MODE_START+VP9_IF_Y_MODE_SIZE)
3830 #define VP9_IF_UV_MODE_SIZE      92 /* only use 90*/
3831 #define VP9_MV_JOINTS_START      (VP9_IF_UV_MODE_START+VP9_IF_UV_MODE_SIZE)
3832 #define VP9_MV_JOINTS_SIZE       3
3833 #define VP9_MV_SIGN_0_START      (VP9_MV_JOINTS_START+VP9_MV_JOINTS_SIZE)
3834 #define VP9_MV_SIGN_0_SIZE       1
3835 #define VP9_MV_CLASSES_0_START   (VP9_MV_SIGN_0_START+VP9_MV_SIGN_0_SIZE)
3836 #define VP9_MV_CLASSES_0_SIZE    10
3837 #define VP9_MV_CLASS0_0_START    (VP9_MV_CLASSES_0_START+VP9_MV_CLASSES_0_SIZE)
3838 #define VP9_MV_CLASS0_0_SIZE     1
3839 #define VP9_MV_BITS_0_START      (VP9_MV_CLASS0_0_START+VP9_MV_CLASS0_0_SIZE)
3840 #define VP9_MV_BITS_0_SIZE       10
3841 #define VP9_MV_SIGN_1_START      (VP9_MV_BITS_0_START+VP9_MV_BITS_0_SIZE)
3842 #define VP9_MV_SIGN_1_SIZE       1
3843 #define VP9_MV_CLASSES_1_START   \
3844 			(VP9_MV_SIGN_1_START+VP9_MV_SIGN_1_SIZE)
3845 #define VP9_MV_CLASSES_1_SIZE    10
3846 #define VP9_MV_CLASS0_1_START    \
3847 			(VP9_MV_CLASSES_1_START+VP9_MV_CLASSES_1_SIZE)
3848 #define VP9_MV_CLASS0_1_SIZE     1
3849 #define VP9_MV_BITS_1_START      \
3850 			(VP9_MV_CLASS0_1_START+VP9_MV_CLASS0_1_SIZE)
3851 #define VP9_MV_BITS_1_SIZE       10
3852 #define VP9_MV_CLASS0_FP_0_START \
3853 			(VP9_MV_BITS_1_START+VP9_MV_BITS_1_SIZE)
3854 #define VP9_MV_CLASS0_FP_0_SIZE  9
3855 #define VP9_MV_CLASS0_FP_1_START \
3856 			(VP9_MV_CLASS0_FP_0_START+VP9_MV_CLASS0_FP_0_SIZE)
3857 #define VP9_MV_CLASS0_FP_1_SIZE  9
3858 #define VP9_MV_CLASS0_HP_0_START \
3859 			(VP9_MV_CLASS0_FP_1_START+VP9_MV_CLASS0_FP_1_SIZE)
3860 #define VP9_MV_CLASS0_HP_0_SIZE  2
3861 #define VP9_MV_CLASS0_HP_1_START \
3862 			(VP9_MV_CLASS0_HP_0_START+VP9_MV_CLASS0_HP_0_SIZE)
3863 #define VP9_MV_CLASS0_HP_1_SIZE  2
3864 #define VP9_MV_START             VP9_MV_JOINTS_START
3865 #define VP9_MV_SIZE              72 /*only use 69*/
3866 
3867 #define VP9_TOTAL_SIZE           (VP9_MV_START + VP9_MV_SIZE)
3868 
3869 
3870 /*========================================================================
3871  *	vp9_count_mem define
3872  *========================================================================
3873  */
3874 #define VP9_COEF_COUNT_START           0
3875 #define VP9_COEF_COUNT_BAND_0_OFFSET   0
3876 #define VP9_COEF_COUNT_BAND_1_OFFSET   \
3877 			(VP9_COEF_COUNT_BAND_0_OFFSET + 3*5)
3878 #define VP9_COEF_COUNT_BAND_2_OFFSET   \
3879 			(VP9_COEF_COUNT_BAND_1_OFFSET + 6*5)
3880 #define VP9_COEF_COUNT_BAND_3_OFFSET   \
3881 			(VP9_COEF_COUNT_BAND_2_OFFSET + 6*5)
3882 #define VP9_COEF_COUNT_BAND_4_OFFSET   \
3883 			(VP9_COEF_COUNT_BAND_3_OFFSET + 6*5)
3884 #define VP9_COEF_COUNT_BAND_5_OFFSET   \
3885 			(VP9_COEF_COUNT_BAND_4_OFFSET + 6*5)
3886 #define VP9_COEF_COUNT_SIZE_ONE_SET    165 /* ((3 +5*6)*5 */
3887 #define VP9_COEF_COUNT_4X4_START       \
3888 	(VP9_COEF_COUNT_START + 0*VP9_COEF_COUNT_SIZE_ONE_SET)
3889 #define VP9_COEF_COUNT_8X8_START       \
3890 	(VP9_COEF_COUNT_START + 4*VP9_COEF_COUNT_SIZE_ONE_SET)
3891 #define VP9_COEF_COUNT_16X16_START     \
3892 	(VP9_COEF_COUNT_START + 8*VP9_COEF_COUNT_SIZE_ONE_SET)
3893 #define VP9_COEF_COUNT_32X32_START     \
3894 	(VP9_COEF_COUNT_START + 12*VP9_COEF_COUNT_SIZE_ONE_SET)
3895 #define VP9_COEF_COUNT_SIZE_PLANE      (2 * VP9_COEF_COUNT_SIZE_ONE_SET)
3896 #define VP9_COEF_COUNT_SIZE            (4 * 2 * 2 * VP9_COEF_COUNT_SIZE_ONE_SET)
3897 
3898 #define VP9_INTRA_INTER_COUNT_START    \
3899 	(VP9_COEF_COUNT_START+VP9_COEF_COUNT_SIZE)
3900 #define VP9_INTRA_INTER_COUNT_SIZE     (4*2)
3901 #define VP9_COMP_INTER_COUNT_START     \
3902 	(VP9_INTRA_INTER_COUNT_START+VP9_INTRA_INTER_COUNT_SIZE)
3903 #define VP9_COMP_INTER_COUNT_SIZE      (5*2)
3904 #define VP9_COMP_REF_COUNT_START       \
3905 	(VP9_COMP_INTER_COUNT_START+VP9_COMP_INTER_COUNT_SIZE)
3906 #define VP9_COMP_REF_COUNT_SIZE        (5*2)
3907 #define VP9_SINGLE_REF_COUNT_START     \
3908 	(VP9_COMP_REF_COUNT_START+VP9_COMP_REF_COUNT_SIZE)
3909 #define VP9_SINGLE_REF_COUNT_SIZE      (10*2)
3910 #define VP9_TX_MODE_COUNT_START        \
3911 	(VP9_SINGLE_REF_COUNT_START+VP9_SINGLE_REF_COUNT_SIZE)
3912 #define VP9_TX_MODE_COUNT_SIZE         (12*2)
3913 #define VP9_SKIP_COUNT_START           \
3914 	(VP9_TX_MODE_COUNT_START+VP9_TX_MODE_COUNT_SIZE)
3915 #define VP9_SKIP_COUNT_SIZE            (3*2)
3916 #define VP9_MV_SIGN_0_COUNT_START      \
3917 	(VP9_SKIP_COUNT_START+VP9_SKIP_COUNT_SIZE)
3918 #define VP9_MV_SIGN_0_COUNT_SIZE       (1*2)
3919 #define VP9_MV_SIGN_1_COUNT_START      \
3920 	(VP9_MV_SIGN_0_COUNT_START+VP9_MV_SIGN_0_COUNT_SIZE)
3921 #define VP9_MV_SIGN_1_COUNT_SIZE       (1*2)
3922 #define VP9_MV_BITS_0_COUNT_START      \
3923 	(VP9_MV_SIGN_1_COUNT_START+VP9_MV_SIGN_1_COUNT_SIZE)
3924 #define VP9_MV_BITS_0_COUNT_SIZE       (10*2)
3925 #define VP9_MV_BITS_1_COUNT_START      \
3926 	(VP9_MV_BITS_0_COUNT_START+VP9_MV_BITS_0_COUNT_SIZE)
3927 #define VP9_MV_BITS_1_COUNT_SIZE       (10*2)
3928 #define VP9_MV_CLASS0_HP_0_COUNT_START \
3929 	(VP9_MV_BITS_1_COUNT_START+VP9_MV_BITS_1_COUNT_SIZE)
3930 #define VP9_MV_CLASS0_HP_0_COUNT_SIZE  (2*2)
3931 #define VP9_MV_CLASS0_HP_1_COUNT_START \
3932 	(VP9_MV_CLASS0_HP_0_COUNT_START+VP9_MV_CLASS0_HP_0_COUNT_SIZE)
3933 #define VP9_MV_CLASS0_HP_1_COUNT_SIZE  (2*2)
3934 /* Start merge_tree*/
3935 #define VP9_INTER_MODE_COUNT_START     \
3936 	(VP9_MV_CLASS0_HP_1_COUNT_START+VP9_MV_CLASS0_HP_1_COUNT_SIZE)
3937 #define VP9_INTER_MODE_COUNT_SIZE      (7*4)
3938 #define VP9_IF_Y_MODE_COUNT_START      \
3939 	(VP9_INTER_MODE_COUNT_START+VP9_INTER_MODE_COUNT_SIZE)
3940 #define VP9_IF_Y_MODE_COUNT_SIZE       (10*4)
3941 #define VP9_IF_UV_MODE_COUNT_START     \
3942 	(VP9_IF_Y_MODE_COUNT_START+VP9_IF_Y_MODE_COUNT_SIZE)
3943 #define VP9_IF_UV_MODE_COUNT_SIZE      (10*10)
3944 #define VP9_PARTITION_P_COUNT_START    \
3945 	(VP9_IF_UV_MODE_COUNT_START+VP9_IF_UV_MODE_COUNT_SIZE)
3946 #define VP9_PARTITION_P_COUNT_SIZE     (4*4*4)
3947 #define VP9_INTERP_COUNT_START         \
3948 	(VP9_PARTITION_P_COUNT_START+VP9_PARTITION_P_COUNT_SIZE)
3949 #define VP9_INTERP_COUNT_SIZE          (4*3)
3950 #define VP9_MV_JOINTS_COUNT_START      \
3951 	(VP9_INTERP_COUNT_START+VP9_INTERP_COUNT_SIZE)
3952 #define VP9_MV_JOINTS_COUNT_SIZE       (1 * 4)
3953 #define VP9_MV_CLASSES_0_COUNT_START   \
3954 	(VP9_MV_JOINTS_COUNT_START+VP9_MV_JOINTS_COUNT_SIZE)
3955 #define VP9_MV_CLASSES_0_COUNT_SIZE    (1*11)
3956 #define VP9_MV_CLASS0_0_COUNT_START    \
3957 	(VP9_MV_CLASSES_0_COUNT_START+VP9_MV_CLASSES_0_COUNT_SIZE)
3958 #define VP9_MV_CLASS0_0_COUNT_SIZE     (1*2)
3959 #define VP9_MV_CLASSES_1_COUNT_START   \
3960 	(VP9_MV_CLASS0_0_COUNT_START+VP9_MV_CLASS0_0_COUNT_SIZE)
3961 #define VP9_MV_CLASSES_1_COUNT_SIZE    (1*11)
3962 #define VP9_MV_CLASS0_1_COUNT_START    \
3963 	(VP9_MV_CLASSES_1_COUNT_START+VP9_MV_CLASSES_1_COUNT_SIZE)
3964 #define VP9_MV_CLASS0_1_COUNT_SIZE     (1*2)
3965 #define VP9_MV_CLASS0_FP_0_COUNT_START \
3966 	(VP9_MV_CLASS0_1_COUNT_START+VP9_MV_CLASS0_1_COUNT_SIZE)
3967 #define VP9_MV_CLASS0_FP_0_COUNT_SIZE  (3*4)
3968 #define VP9_MV_CLASS0_FP_1_COUNT_START \
3969 	(VP9_MV_CLASS0_FP_0_COUNT_START+VP9_MV_CLASS0_FP_0_COUNT_SIZE)
3970 #define VP9_MV_CLASS0_FP_1_COUNT_SIZE  (3*4)
3971 
3972 
3973 #define DC_PRED    0       /* Average of above and left pixels*/
3974 #define V_PRED     1       /* Vertical*/
3975 #define H_PRED     2       /* Horizontal*/
3976 #define D45_PRED   3       /*Directional 45 deg = round(arctan(1/1) * 180/pi)*/
3977 #define D135_PRED  4       /* Directional 135 deg = 180 - 45*/
3978 #define D117_PRED  5       /* Directional 117 deg = 180 - 63*/
3979 #define D153_PRED  6       /* Directional 153 deg = 180 - 27*/
3980 #define D207_PRED  7       /* Directional 207 deg = 180 + 27*/
3981 #define D63_PRED   8       /*Directional 63 deg = round(arctan(2/1) * 180/pi)*/
3982 #define TM_PRED    9       /*True-motion*/
3983 
clip_prob(int p)3984 int clip_prob(int p)
3985 {
3986 	return (p > 255) ? 255 : (p < 1) ? 1 : p;
3987 }
3988 
3989 #define ROUND_POWER_OF_TWO(value, n) \
3990 	(((value) + (1 << ((n) - 1))) >> (n))
3991 
3992 #define MODE_MV_COUNT_SAT 20
3993 static const int count_to_update_factor[MODE_MV_COUNT_SAT + 1] = {
3994 	0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64,
3995 	70, 76, 83, 89, 96, 102, 108, 115, 121, 128
3996 };
3997 
vp9_tree_merge_probs(unsigned int * prev_prob,unsigned int * cur_prob,int coef_node_start,int tree_left,int tree_right,int tree_i,int node)3998 void   vp9_tree_merge_probs(unsigned int *prev_prob, unsigned int *cur_prob,
3999 	int coef_node_start, int tree_left, int tree_right, int tree_i,
4000 	int node) {
4001 
4002 	int prob_32, prob_res, prob_shift;
4003 	int pre_prob, new_prob;
4004 	int den, m_count, get_prob, factor;
4005 
4006 	prob_32 = prev_prob[coef_node_start / 4 * 2];
4007 	prob_res = coef_node_start & 3;
4008 	prob_shift = prob_res * 8;
4009 	pre_prob = (prob_32 >> prob_shift) & 0xff;
4010 
4011 	den = tree_left + tree_right;
4012 
4013 	if (den == 0)
4014 		new_prob = pre_prob;
4015 	else {
4016 		m_count = (den < MODE_MV_COUNT_SAT) ?
4017 			den : MODE_MV_COUNT_SAT;
4018 		get_prob = clip_prob(
4019 				div_r32(((int64_t)tree_left * 256 + (den >> 1)),
4020 				den));
4021 		/*weighted_prob*/
4022 		factor = count_to_update_factor[m_count];
4023 		new_prob = ROUND_POWER_OF_TWO(pre_prob * (256 - factor)
4024 				+ get_prob * factor, 8);
4025 	}
4026 	cur_prob[coef_node_start / 4 * 2] = (cur_prob[coef_node_start / 4 * 2]
4027 			& (~(0xff << prob_shift))) | (new_prob << prob_shift);
4028 
4029 	/*pr_info(" - [%d][%d] 0x%02X --> 0x%02X (0x%X 0x%X) (%X)\n",
4030 	 *tree_i, node, pre_prob, new_prob, tree_left, tree_right,
4031 	 *cur_prob[coef_node_start/4*2]);
4032 	 */
4033 }
4034 
4035 
4036 /*void adapt_coef_probs(void)*/
adapt_coef_probs(int pic_count,int prev_kf,int cur_kf,int pre_fc,unsigned int * prev_prob,unsigned int * cur_prob,unsigned int * count)4037 void adapt_coef_probs(int pic_count, int prev_kf, int cur_kf, int pre_fc,
4038 	unsigned int *prev_prob, unsigned int *cur_prob, unsigned int *count)
4039 {
4040 	/* 80 * 64bits = 0xF00 ( use 0x1000 4K bytes)
4041 	 *unsigned int prev_prob[496*2];
4042 	 *unsigned int cur_prob[496*2];
4043 	 *0x300 * 128bits = 0x3000 (32K Bytes)
4044 	 *unsigned int count[0x300*4];
4045 	 */
4046 
4047 	int tx_size, coef_tx_size_start, coef_count_tx_size_start;
4048 	int plane, coef_plane_start, coef_count_plane_start;
4049 	int type, coef_type_start, coef_count_type_start;
4050 	int band, coef_band_start, coef_count_band_start;
4051 	int cxt_num;
4052 	int cxt, coef_cxt_start, coef_count_cxt_start;
4053 	int node, coef_node_start, coef_count_node_start;
4054 
4055 	int tree_i, tree_left, tree_right;
4056 	int mvd_i;
4057 
4058 	int count_sat = 24;
4059 	/*int update_factor = 112;*/ /*If COEF_MAX_UPDATE_FACTOR_AFTER_KEY,
4060 	 *use 128
4061 	 */
4062 	/* If COEF_MAX_UPDATE_FACTOR_AFTER_KEY, use 128*/
4063 	/*int update_factor = (pic_count == 1) ? 128 : 112;*/
4064 	int update_factor =   cur_kf ? 112 :
4065 			prev_kf ? 128 : 112;
4066 
4067 	int prob_32;
4068 	int prob_res;
4069 	int prob_shift;
4070 	int pre_prob;
4071 
4072 	int num, den;
4073 	int get_prob;
4074 	int m_count;
4075 	int factor;
4076 
4077 	int new_prob;
4078 
4079 	if (debug & VP9_DEBUG_MERGE)
4080 		pr_info
4081 	("\n ##adapt_coef_probs (pre_fc : %d ,prev_kf : %d,cur_kf : %d)##\n\n",
4082 	pre_fc, prev_kf, cur_kf);
4083 
4084 	/*adapt_coef_probs*/
4085 	for (tx_size = 0; tx_size < 4; tx_size++) {
4086 		coef_tx_size_start = VP9_COEF_START
4087 			+ tx_size * 4 * VP9_COEF_SIZE_ONE_SET;
4088 		coef_count_tx_size_start = VP9_COEF_COUNT_START
4089 			+ tx_size * 4 * VP9_COEF_COUNT_SIZE_ONE_SET;
4090 		coef_plane_start = coef_tx_size_start;
4091 		coef_count_plane_start = coef_count_tx_size_start;
4092 		for (plane = 0; plane < 2; plane++) {
4093 			coef_type_start = coef_plane_start;
4094 			coef_count_type_start = coef_count_plane_start;
4095 			for (type = 0; type < 2; type++) {
4096 				coef_band_start = coef_type_start;
4097 				coef_count_band_start = coef_count_type_start;
4098 				for (band = 0; band < 6; band++) {
4099 					if (band == 0)
4100 						cxt_num = 3;
4101 					else
4102 						cxt_num = 6;
4103 					coef_cxt_start = coef_band_start;
4104 					coef_count_cxt_start =
4105 						coef_count_band_start;
4106 					for (cxt = 0; cxt < cxt_num; cxt++) {
4107 						const int n0 =
4108 						count[coef_count_cxt_start];
4109 						const int n1 =
4110 						count[coef_count_cxt_start + 1];
4111 						const int n2 =
4112 						count[coef_count_cxt_start + 2];
4113 						const int neob =
4114 						count[coef_count_cxt_start + 3];
4115 						const int nneob =
4116 						count[coef_count_cxt_start + 4];
4117 						const unsigned int
4118 						branch_ct[3][2] = {
4119 						{ neob, nneob },
4120 						{ n0, n1 + n2 },
4121 						{ n1, n2 }
4122 						};
4123 						coef_node_start =
4124 							coef_cxt_start;
4125 						for
4126 						(node = 0; node < 3; node++) {
4127 							prob_32 =
4128 							prev_prob[
4129 							coef_node_start
4130 							/ 4 * 2];
4131 							prob_res =
4132 							coef_node_start & 3;
4133 							prob_shift =
4134 							prob_res * 8;
4135 							pre_prob =
4136 							(prob_32 >> prob_shift)
4137 							& 0xff;
4138 
4139 							/*get_binary_prob*/
4140 							num =
4141 							branch_ct[node][0];
4142 							den =
4143 							branch_ct[node][0] +
4144 							 branch_ct[node][1];
4145 							m_count = (den <
4146 							count_sat)
4147 							? den : count_sat;
4148 
4149 							get_prob =
4150 							(den == 0) ? 128u :
4151 							clip_prob(
4152 							div_r32(((int64_t)
4153 							num * 256
4154 							+ (den >> 1)),
4155 							den));
4156 
4157 							factor =
4158 							update_factor * m_count
4159 							/ count_sat;
4160 							new_prob =
4161 							ROUND_POWER_OF_TWO
4162 							(pre_prob *
4163 							(256 - factor) +
4164 							get_prob * factor, 8);
4165 
4166 							cur_prob[coef_node_start
4167 							/ 4 * 2] =
4168 							(cur_prob
4169 							[coef_node_start
4170 							/ 4 * 2] & (~(0xff <<
4171 							prob_shift))) |
4172 							(new_prob <<
4173 							prob_shift);
4174 
4175 							coef_node_start += 1;
4176 						}
4177 
4178 						coef_cxt_start =
4179 							coef_cxt_start + 3;
4180 						coef_count_cxt_start =
4181 							coef_count_cxt_start
4182 							+ 5;
4183 					}
4184 					if (band == 0) {
4185 						coef_band_start += 10;
4186 						coef_count_band_start += 15;
4187 					} else {
4188 						coef_band_start += 18;
4189 						coef_count_band_start += 30;
4190 					}
4191 				}
4192 				coef_type_start += VP9_COEF_SIZE_ONE_SET;
4193 				coef_count_type_start +=
4194 					VP9_COEF_COUNT_SIZE_ONE_SET;
4195 			}
4196 			coef_plane_start += 2 * VP9_COEF_SIZE_ONE_SET;
4197 			coef_count_plane_start +=
4198 					2 * VP9_COEF_COUNT_SIZE_ONE_SET;
4199 		}
4200 	}
4201 
4202 	if (cur_kf == 0) {
4203 		/*mode_mv_merge_probs - merge_intra_inter_prob*/
4204 		for (coef_count_node_start = VP9_INTRA_INTER_COUNT_START;
4205 		coef_count_node_start < (VP9_MV_CLASS0_HP_1_COUNT_START +
4206 		VP9_MV_CLASS0_HP_1_COUNT_SIZE);	coef_count_node_start += 2) {
4207 
4208 			if (coef_count_node_start ==
4209 				VP9_INTRA_INTER_COUNT_START) {
4210 				if (debug & VP9_DEBUG_MERGE)
4211 					pr_info(" # merge_intra_inter_prob\n");
4212 				coef_node_start = VP9_INTRA_INTER_START;
4213 			} else if (coef_count_node_start ==
4214 				VP9_COMP_INTER_COUNT_START) {
4215 				if (debug & VP9_DEBUG_MERGE)
4216 					pr_info(" # merge_comp_inter_prob\n");
4217 				coef_node_start = VP9_COMP_INTER_START;
4218 			}
4219 			/*
4220 			 *else if (coef_count_node_start ==
4221 			 *	VP9_COMP_REF_COUNT_START) {
4222 			 *	pr_info(" # merge_comp_inter_prob\n");
4223 			 *	coef_node_start = VP9_COMP_REF_START;
4224 			 *}
4225 			 *else if (coef_count_node_start ==
4226 			 *	VP9_SINGLE_REF_COUNT_START) {
4227 			 *	pr_info(" # merge_comp_inter_prob\n");
4228 			 *	coef_node_start = VP9_SINGLE_REF_START;
4229 			 *}
4230 			 */
4231 			else if (coef_count_node_start ==
4232 				VP9_TX_MODE_COUNT_START) {
4233 				if (debug & VP9_DEBUG_MERGE)
4234 					pr_info(" # merge_tx_mode_probs\n");
4235 				coef_node_start = VP9_TX_MODE_START;
4236 			} else if (coef_count_node_start ==
4237 				VP9_SKIP_COUNT_START) {
4238 				if (debug & VP9_DEBUG_MERGE)
4239 					pr_info(" # merge_skip_probs\n");
4240 				coef_node_start = VP9_SKIP_START;
4241 			} else if (coef_count_node_start ==
4242 				VP9_MV_SIGN_0_COUNT_START) {
4243 				if (debug & VP9_DEBUG_MERGE)
4244 					pr_info(" # merge_sign_0\n");
4245 				coef_node_start = VP9_MV_SIGN_0_START;
4246 			} else if (coef_count_node_start ==
4247 				VP9_MV_SIGN_1_COUNT_START) {
4248 				if (debug & VP9_DEBUG_MERGE)
4249 					pr_info(" # merge_sign_1\n");
4250 				coef_node_start = VP9_MV_SIGN_1_START;
4251 			} else if (coef_count_node_start ==
4252 				VP9_MV_BITS_0_COUNT_START) {
4253 				if (debug & VP9_DEBUG_MERGE)
4254 					pr_info(" # merge_bits_0\n");
4255 				coef_node_start = VP9_MV_BITS_0_START;
4256 			} else if (coef_count_node_start ==
4257 				VP9_MV_BITS_1_COUNT_START) {
4258 				if (debug & VP9_DEBUG_MERGE)
4259 					pr_info(" # merge_bits_1\n");
4260 				coef_node_start = VP9_MV_BITS_1_START;
4261 			} else if (coef_count_node_start ==
4262 					VP9_MV_CLASS0_HP_0_COUNT_START) {
4263 				if (debug & VP9_DEBUG_MERGE)
4264 					pr_info(" # merge_class0_hp\n");
4265 				coef_node_start = VP9_MV_CLASS0_HP_0_START;
4266 			}
4267 
4268 
4269 		den = count[coef_count_node_start] +
4270 			count[coef_count_node_start + 1];
4271 
4272 		prob_32 = prev_prob[coef_node_start / 4 * 2];
4273 		prob_res = coef_node_start & 3;
4274 		prob_shift = prob_res * 8;
4275 		pre_prob = (prob_32 >> prob_shift) & 0xff;
4276 
4277 		if (den == 0)
4278 			new_prob = pre_prob;
4279 		else {
4280 			m_count = (den < MODE_MV_COUNT_SAT) ?
4281 				den : MODE_MV_COUNT_SAT;
4282 			get_prob =
4283 				clip_prob(
4284 				div_r32(((int64_t)count[coef_count_node_start]
4285 				* 256 + (den >> 1)),
4286 				den));
4287 			/*weighted_prob*/
4288 			factor = count_to_update_factor[m_count];
4289 			new_prob =
4290 				ROUND_POWER_OF_TWO(pre_prob * (256 - factor)
4291 				+ get_prob * factor, 8);
4292 		}
4293 		cur_prob[coef_node_start / 4 * 2] =
4294 			(cur_prob[coef_node_start / 4 * 2] &
4295 			(~(0xff << prob_shift)))
4296 			| (new_prob << prob_shift);
4297 
4298 		coef_node_start = coef_node_start + 1;
4299 	}
4300 	if (debug & VP9_DEBUG_MERGE)
4301 		pr_info(" # merge_vp9_inter_mode_tree\n");
4302 	coef_node_start = VP9_INTER_MODE_START;
4303 	coef_count_node_start = VP9_INTER_MODE_COUNT_START;
4304 	for (tree_i = 0; tree_i < 7; tree_i++) {
4305 		for (node = 0; node < 3; node++) {
4306 			switch (node) {
4307 			case 2:
4308 				tree_left =
4309 				count[coef_count_node_start + 1];
4310 				tree_right =
4311 				count[coef_count_node_start + 3];
4312 				break;
4313 			case 1:
4314 				tree_left =
4315 				count[coef_count_node_start + 0];
4316 				tree_right =
4317 				count[coef_count_node_start + 1]
4318 				+ count[coef_count_node_start + 3];
4319 				break;
4320 			default:
4321 				tree_left =
4322 				count[coef_count_node_start + 2];
4323 				tree_right =
4324 				count[coef_count_node_start + 0]
4325 				+ count[coef_count_node_start + 1]
4326 				+ count[coef_count_node_start + 3];
4327 				break;
4328 
4329 			}
4330 
4331 			vp9_tree_merge_probs(prev_prob, cur_prob,
4332 				coef_node_start, tree_left, tree_right,
4333 				tree_i, node);
4334 
4335 			coef_node_start = coef_node_start + 1;
4336 		}
4337 		coef_count_node_start = coef_count_node_start + 4;
4338 	}
4339 	if (debug & VP9_DEBUG_MERGE)
4340 		pr_info(" # merge_vp9_intra_mode_tree\n");
4341 	coef_node_start = VP9_IF_Y_MODE_START;
4342 	coef_count_node_start = VP9_IF_Y_MODE_COUNT_START;
4343 	for (tree_i = 0; tree_i < 14; tree_i++) {
4344 		for (node = 0; node < 9; node++) {
4345 			switch (node) {
4346 			case 8:
4347 				tree_left =
4348 				count[coef_count_node_start+D153_PRED];
4349 				tree_right =
4350 				count[coef_count_node_start+D207_PRED];
4351 				break;
4352 			case 7:
4353 				tree_left =
4354 				count[coef_count_node_start+D63_PRED];
4355 				tree_right =
4356 				count[coef_count_node_start+D207_PRED] +
4357 				count[coef_count_node_start+D153_PRED];
4358 				break;
4359 			case 6:
4360 				tree_left =
4361 				count[coef_count_node_start + D45_PRED];
4362 				tree_right =
4363 				count[coef_count_node_start+D207_PRED] +
4364 				count[coef_count_node_start+D153_PRED] +
4365 				count[coef_count_node_start+D63_PRED];
4366 				break;
4367 			case 5:
4368 				tree_left =
4369 				count[coef_count_node_start+D135_PRED];
4370 				tree_right =
4371 				count[coef_count_node_start+D117_PRED];
4372 				break;
4373 			case 4:
4374 				tree_left =
4375 				count[coef_count_node_start+H_PRED];
4376 				tree_right =
4377 				count[coef_count_node_start+D117_PRED] +
4378 				count[coef_count_node_start+D135_PRED];
4379 				break;
4380 			case 3:
4381 				tree_left =
4382 				count[coef_count_node_start+H_PRED] +
4383 				count[coef_count_node_start+D117_PRED] +
4384 				count[coef_count_node_start+D135_PRED];
4385 				tree_right =
4386 				count[coef_count_node_start+D45_PRED] +
4387 				count[coef_count_node_start+D207_PRED] +
4388 				count[coef_count_node_start+D153_PRED] +
4389 				count[coef_count_node_start+D63_PRED];
4390 				break;
4391 			case 2:
4392 				tree_left =
4393 				count[coef_count_node_start+V_PRED];
4394 				tree_right =
4395 				count[coef_count_node_start+H_PRED] +
4396 				count[coef_count_node_start+D117_PRED] +
4397 				count[coef_count_node_start+D135_PRED] +
4398 				count[coef_count_node_start+D45_PRED] +
4399 				count[coef_count_node_start+D207_PRED] +
4400 				count[coef_count_node_start+D153_PRED] +
4401 				count[coef_count_node_start+D63_PRED];
4402 				break;
4403 			case 1:
4404 				tree_left =
4405 				count[coef_count_node_start+TM_PRED];
4406 				tree_right =
4407 				count[coef_count_node_start+V_PRED] +
4408 				count[coef_count_node_start+H_PRED] +
4409 				count[coef_count_node_start+D117_PRED] +
4410 				count[coef_count_node_start+D135_PRED] +
4411 				count[coef_count_node_start+D45_PRED] +
4412 				count[coef_count_node_start+D207_PRED] +
4413 				count[coef_count_node_start+D153_PRED] +
4414 				count[coef_count_node_start+D63_PRED];
4415 				break;
4416 			default:
4417 				tree_left =
4418 				count[coef_count_node_start+DC_PRED];
4419 				tree_right =
4420 				count[coef_count_node_start+TM_PRED] +
4421 				count[coef_count_node_start+V_PRED] +
4422 				count[coef_count_node_start+H_PRED] +
4423 				count[coef_count_node_start+D117_PRED] +
4424 				count[coef_count_node_start+D135_PRED] +
4425 				count[coef_count_node_start+D45_PRED] +
4426 				count[coef_count_node_start+D207_PRED] +
4427 				count[coef_count_node_start+D153_PRED] +
4428 				count[coef_count_node_start+D63_PRED];
4429 				break;
4430 
4431 				}
4432 
4433 			vp9_tree_merge_probs(prev_prob, cur_prob,
4434 				coef_node_start, tree_left, tree_right,
4435 				tree_i, node);
4436 
4437 			coef_node_start = coef_node_start + 1;
4438 		}
4439 		coef_count_node_start = coef_count_node_start + 10;
4440 	}
4441 
4442 	if (debug & VP9_DEBUG_MERGE)
4443 		pr_info(" # merge_vp9_partition_tree\n");
4444 	coef_node_start = VP9_PARTITION_P_START;
4445 	coef_count_node_start = VP9_PARTITION_P_COUNT_START;
4446 	for (tree_i = 0; tree_i < 16; tree_i++) {
4447 		for (node = 0; node < 3; node++) {
4448 			switch (node) {
4449 			case 2:
4450 				tree_left =
4451 				count[coef_count_node_start + 2];
4452 				tree_right =
4453 				count[coef_count_node_start + 3];
4454 				break;
4455 			case 1:
4456 				tree_left =
4457 				count[coef_count_node_start + 1];
4458 				tree_right =
4459 				count[coef_count_node_start + 2] +
4460 				count[coef_count_node_start + 3];
4461 				break;
4462 			default:
4463 				tree_left =
4464 				count[coef_count_node_start + 0];
4465 				tree_right =
4466 				count[coef_count_node_start + 1] +
4467 				count[coef_count_node_start + 2] +
4468 				count[coef_count_node_start + 3];
4469 				break;
4470 
4471 			}
4472 
4473 			vp9_tree_merge_probs(prev_prob, cur_prob,
4474 				coef_node_start,
4475 				tree_left, tree_right, tree_i, node);
4476 
4477 			coef_node_start = coef_node_start + 1;
4478 		}
4479 		coef_count_node_start = coef_count_node_start + 4;
4480 	}
4481 
4482 	if (debug & VP9_DEBUG_MERGE)
4483 		pr_info(" # merge_vp9_switchable_interp_tree\n");
4484 	coef_node_start = VP9_INTERP_START;
4485 	coef_count_node_start = VP9_INTERP_COUNT_START;
4486 	for (tree_i = 0; tree_i < 4; tree_i++) {
4487 		for (node = 0; node < 2; node++) {
4488 			switch (node) {
4489 			case 1:
4490 				tree_left =
4491 				count[coef_count_node_start + 1];
4492 				tree_right =
4493 				count[coef_count_node_start + 2];
4494 				break;
4495 			default:
4496 				tree_left =
4497 				count[coef_count_node_start + 0];
4498 				tree_right =
4499 				count[coef_count_node_start + 1] +
4500 				count[coef_count_node_start + 2];
4501 				break;
4502 
4503 			}
4504 
4505 			vp9_tree_merge_probs(prev_prob, cur_prob,
4506 				coef_node_start,
4507 				tree_left, tree_right, tree_i, node);
4508 
4509 			coef_node_start = coef_node_start + 1;
4510 		}
4511 		coef_count_node_start = coef_count_node_start + 3;
4512 	}
4513 
4514 	if (debug & VP9_DEBUG_MERGE)
4515 		pr_info("# merge_vp9_mv_joint_tree\n");
4516 	coef_node_start = VP9_MV_JOINTS_START;
4517 	coef_count_node_start = VP9_MV_JOINTS_COUNT_START;
4518 	for (tree_i = 0; tree_i < 1; tree_i++) {
4519 		for (node = 0; node < 3; node++) {
4520 			switch (node) {
4521 			case 2:
4522 				tree_left =
4523 				count[coef_count_node_start + 2];
4524 				tree_right =
4525 				count[coef_count_node_start + 3];
4526 				break;
4527 			case 1:
4528 				tree_left =
4529 				count[coef_count_node_start + 1];
4530 				tree_right =
4531 				count[coef_count_node_start + 2] +
4532 				count[coef_count_node_start + 3];
4533 				break;
4534 			default:
4535 				tree_left =
4536 				count[coef_count_node_start + 0];
4537 				tree_right =
4538 				count[coef_count_node_start + 1] +
4539 				count[coef_count_node_start + 2] +
4540 				count[coef_count_node_start + 3];
4541 				break;
4542 			}
4543 
4544 			vp9_tree_merge_probs(prev_prob, cur_prob,
4545 				coef_node_start,
4546 				tree_left, tree_right, tree_i, node);
4547 
4548 			coef_node_start = coef_node_start + 1;
4549 		}
4550 		coef_count_node_start = coef_count_node_start + 4;
4551 	}
4552 
4553 	for (mvd_i = 0; mvd_i < 2; mvd_i++) {
4554 		if (debug & VP9_DEBUG_MERGE)
4555 			pr_info(" # merge_vp9_mv_class_tree [%d]  -\n", mvd_i);
4556 		coef_node_start =
4557 			mvd_i ? VP9_MV_CLASSES_1_START : VP9_MV_CLASSES_0_START;
4558 		coef_count_node_start =
4559 			mvd_i ? VP9_MV_CLASSES_1_COUNT_START
4560 			: VP9_MV_CLASSES_0_COUNT_START;
4561 		tree_i = 0;
4562 		for (node = 0; node < 10; node++) {
4563 			switch (node) {
4564 			case 9:
4565 				tree_left =
4566 				count[coef_count_node_start + 9];
4567 				tree_right =
4568 				count[coef_count_node_start + 10];
4569 				break;
4570 			case 8:
4571 				tree_left =
4572 				count[coef_count_node_start + 7];
4573 				tree_right =
4574 				count[coef_count_node_start + 8];
4575 				break;
4576 			case 7:
4577 				tree_left =
4578 				count[coef_count_node_start + 7] +
4579 				count[coef_count_node_start + 8];
4580 				tree_right =
4581 				count[coef_count_node_start + 9] +
4582 				count[coef_count_node_start + 10];
4583 				break;
4584 			case 6:
4585 				tree_left =
4586 				count[coef_count_node_start + 6];
4587 				tree_right =
4588 				count[coef_count_node_start + 7] +
4589 				count[coef_count_node_start + 8] +
4590 				count[coef_count_node_start + 9] +
4591 				count[coef_count_node_start + 10];
4592 				break;
4593 			case 5:
4594 				tree_left =
4595 				count[coef_count_node_start + 4];
4596 				tree_right =
4597 				count[coef_count_node_start + 5];
4598 				break;
4599 			case 4:
4600 				tree_left =
4601 				count[coef_count_node_start + 4] +
4602 				count[coef_count_node_start + 5];
4603 				tree_right =
4604 				count[coef_count_node_start + 6] +
4605 				count[coef_count_node_start + 7] +
4606 				count[coef_count_node_start + 8] +
4607 				count[coef_count_node_start + 9] +
4608 				count[coef_count_node_start + 10];
4609 				break;
4610 			case 3:
4611 				tree_left =
4612 				count[coef_count_node_start + 2];
4613 				tree_right =
4614 				count[coef_count_node_start + 3];
4615 				break;
4616 			case 2:
4617 				tree_left =
4618 				count[coef_count_node_start + 2] +
4619 				count[coef_count_node_start + 3];
4620 				tree_right =
4621 				count[coef_count_node_start + 4] +
4622 				count[coef_count_node_start + 5] +
4623 				count[coef_count_node_start + 6] +
4624 				count[coef_count_node_start + 7] +
4625 				count[coef_count_node_start + 8] +
4626 				count[coef_count_node_start + 9] +
4627 				count[coef_count_node_start + 10];
4628 				break;
4629 			case 1:
4630 				tree_left =
4631 				count[coef_count_node_start + 1];
4632 				tree_right =
4633 				count[coef_count_node_start + 2] +
4634 				count[coef_count_node_start + 3] +
4635 				count[coef_count_node_start + 4] +
4636 				count[coef_count_node_start + 5] +
4637 				count[coef_count_node_start + 6] +
4638 				count[coef_count_node_start + 7] +
4639 				count[coef_count_node_start + 8] +
4640 				count[coef_count_node_start + 9] +
4641 				count[coef_count_node_start + 10];
4642 				break;
4643 			default:
4644 				tree_left =
4645 				count[coef_count_node_start + 0];
4646 				tree_right =
4647 				count[coef_count_node_start + 1] +
4648 				count[coef_count_node_start + 2] +
4649 				count[coef_count_node_start + 3] +
4650 				count[coef_count_node_start + 4] +
4651 				count[coef_count_node_start + 5] +
4652 				count[coef_count_node_start + 6] +
4653 				count[coef_count_node_start + 7] +
4654 				count[coef_count_node_start + 8] +
4655 				count[coef_count_node_start + 9] +
4656 				count[coef_count_node_start + 10];
4657 				break;
4658 
4659 			}
4660 
4661 			vp9_tree_merge_probs(prev_prob, cur_prob,
4662 				coef_node_start, tree_left, tree_right,
4663 				tree_i, node);
4664 
4665 			coef_node_start = coef_node_start + 1;
4666 		}
4667 
4668 		if (debug & VP9_DEBUG_MERGE)
4669 			pr_info(" # merge_vp9_mv_class0_tree [%d]  -\n", mvd_i);
4670 		coef_node_start =
4671 			mvd_i ? VP9_MV_CLASS0_1_START : VP9_MV_CLASS0_0_START;
4672 		coef_count_node_start =
4673 			mvd_i ? VP9_MV_CLASS0_1_COUNT_START :
4674 			VP9_MV_CLASS0_0_COUNT_START;
4675 		tree_i = 0;
4676 		node = 0;
4677 		tree_left = count[coef_count_node_start + 0];
4678 		tree_right = count[coef_count_node_start + 1];
4679 
4680 		vp9_tree_merge_probs(prev_prob, cur_prob, coef_node_start,
4681 			tree_left, tree_right, tree_i, node);
4682 		if (debug & VP9_DEBUG_MERGE)
4683 			pr_info(" # merge_vp9_mv_fp_tree_class0_fp [%d]  -\n",
4684 				mvd_i);
4685 		coef_node_start =
4686 			mvd_i ? VP9_MV_CLASS0_FP_1_START :
4687 			VP9_MV_CLASS0_FP_0_START;
4688 		coef_count_node_start =
4689 			mvd_i ? VP9_MV_CLASS0_FP_1_COUNT_START :
4690 			VP9_MV_CLASS0_FP_0_COUNT_START;
4691 		for (tree_i = 0; tree_i < 3; tree_i++) {
4692 			for (node = 0; node < 3; node++) {
4693 				switch (node) {
4694 				case 2:
4695 					tree_left =
4696 					count[coef_count_node_start + 2];
4697 					tree_right =
4698 					count[coef_count_node_start + 3];
4699 					break;
4700 				case 1:
4701 					tree_left =
4702 					count[coef_count_node_start + 1];
4703 					tree_right =
4704 					count[coef_count_node_start + 2]
4705 					+ count[coef_count_node_start + 3];
4706 					break;
4707 				default:
4708 					tree_left =
4709 					count[coef_count_node_start + 0];
4710 					tree_right =
4711 					count[coef_count_node_start + 1]
4712 					+ count[coef_count_node_start + 2]
4713 					+ count[coef_count_node_start + 3];
4714 					break;
4715 
4716 				}
4717 
4718 				vp9_tree_merge_probs(prev_prob, cur_prob,
4719 					coef_node_start, tree_left, tree_right,
4720 					tree_i, node);
4721 
4722 				coef_node_start = coef_node_start + 1;
4723 			}
4724 			coef_count_node_start = coef_count_node_start + 4;
4725 		}
4726 
4727 	} /* for mvd_i (mvd_y or mvd_x)*/
4728 }
4729 
4730 }
4731 
v4l_is_there_vframe_bound(struct VP9Decoder_s * pbi)4732 static bool v4l_is_there_vframe_bound(struct VP9Decoder_s *pbi)
4733 {
4734 	int i;
4735 	struct VP9_Common_s *const cm = &pbi->common;
4736 	struct RefCntBuffer_s *frame_bufs = cm->buffer_pool->frame_bufs;
4737 
4738 	for (i = 0; i < pbi->used_buf_num; ++i) {
4739 		if (frame_bufs[i].buf.vframe_bound)
4740 			return true;
4741 	}
4742 
4743 	return false;
4744 }
4745 
v4l_mmu_buffer_release(struct VP9Decoder_s * pbi)4746 static void v4l_mmu_buffer_release(struct VP9Decoder_s *pbi)
4747 {
4748 	struct VP9_Common_s *const cm = &pbi->common;
4749 	struct RefCntBuffer_s *frame_bufs = cm->buffer_pool->frame_bufs;
4750 	int i;
4751 
4752 	/* release workspace */
4753 	if (pbi->bmmu_box)
4754 		decoder_bmmu_box_free_idx(pbi->bmmu_box,
4755 			WORK_SPACE_BUF_ID);
4756 	/*
4757 	 * it's only when vframe get back to driver, right now we can be sure
4758 	 * that vframe and fd are related. if the playback exits, the capture
4759 	 * requires the upper app to release when the fd is closed, and others
4760 	 * buffers drivers are released by driver.
4761 	 */
4762 	for (i = 0; i < pbi->used_buf_num; ++i) {
4763 		if (!frame_bufs[i].buf.vframe_bound) {
4764 			if (pbi->bmmu_box)
4765 				decoder_bmmu_box_free_idx(pbi->bmmu_box,
4766 					HEADER_BUFFER_IDX(i));
4767 			if (pbi->mmu_box)
4768 				decoder_mmu_box_free_idx(pbi->mmu_box, i);
4769 
4770 			vp9_print(pbi, PRINT_FLAG_V4L_DETAIL,
4771 				"%s free buffer[%d], bmmu_box: %p, mmu_box: %p\n",
4772 				__func__, i, pbi->bmmu_box, pbi->mmu_box);
4773 		}
4774 	}
4775 }
4776 
uninit_mmu_buffers(struct VP9Decoder_s * pbi)4777 static void uninit_mmu_buffers(struct VP9Decoder_s *pbi)
4778 {
4779 #ifndef MV_USE_FIXED_BUF
4780 	dealloc_mv_bufs(pbi);
4781 #endif
4782 	if (pbi->is_used_v4l &&
4783 		v4l_is_there_vframe_bound(pbi)) {
4784 		if (get_double_write_mode(pbi) != 0x10) {
4785 			v4l_mmu_buffer_release(pbi);
4786 			return;
4787 		}
4788 	}
4789 
4790 	if (pbi->mmu_box)
4791 		decoder_mmu_box_free(pbi->mmu_box);
4792 	pbi->mmu_box = NULL;
4793 
4794 	if (pbi->bmmu_box)
4795 		decoder_bmmu_box_free(pbi->bmmu_box);
4796 	pbi->bmmu_box = NULL;
4797 }
4798 
calc_luc_quantity(u32 w,u32 h)4799 static int calc_luc_quantity(u32 w, u32 h)
4800 {
4801 	int lcu_size = 64; /*fixed 64*/
4802 	int pic_width_64 = (w + 63) & (~0x3f);
4803 	int pic_height_32 = (h + 31) & (~0x1f);
4804 	int pic_width_lcu  = (pic_width_64 % lcu_size) ?
4805 		pic_width_64 / lcu_size  + 1 : pic_width_64 / lcu_size;
4806 	int pic_height_lcu = (pic_height_32 % lcu_size) ?
4807 		pic_height_32 / lcu_size + 1 : pic_height_32 / lcu_size;
4808 
4809 	return pic_width_lcu * pic_height_lcu;
4810 }
4811 
v4l_alloc_and_config_pic(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * pic)4812 static int v4l_alloc_and_config_pic(struct VP9Decoder_s *pbi,
4813 	struct PIC_BUFFER_CONFIG_s *pic)
4814 {
4815 	int ret = -1;
4816 	int i = pic->index;
4817 	int dw_mode = get_double_write_mode_init(pbi);
4818 	int lcu_total = calc_luc_quantity(pbi->frame_width, pbi->frame_height);
4819 #ifdef MV_USE_FIXED_BUF
4820 	u32 mpred_mv_end = pbi->work_space_buf->mpred_mv.buf_start +
4821 		pbi->work_space_buf->mpred_mv.buf_size;
4822 #endif
4823 	struct vdec_v4l2_buffer *fb = NULL;
4824 
4825 	if (i < 0)
4826 		return ret;
4827 
4828 	ret = vdec_v4l_get_buffer(pbi->v4l2_ctx, &fb);
4829 	if (ret < 0) {
4830 		vp9_print(pbi, 0, "[%d] VP9 get buffer fail.\n",
4831 			((struct aml_vcodec_ctx *) (pbi->v4l2_ctx))->id);
4832 		return ret;
4833 	}
4834 
4835 	if (pbi->mmu_enable) {
4836 		pbi->m_BUF[i].header_addr = decoder_bmmu_box_get_phy_addr(
4837 			pbi->bmmu_box, HEADER_BUFFER_IDX(i));
4838 		if (debug & VP9_DEBUG_BUFMGR_MORE) {
4839 			pr_info("MMU header_adr %d: %ld\n",
4840 				i, pbi->m_BUF[i].header_addr);
4841 		}
4842 	}
4843 
4844 #ifdef MV_USE_FIXED_BUF
4845 	if ((pbi->work_space_buf->mpred_mv.buf_start +
4846 		(((i + 1) * lcu_total) * MV_MEM_UNIT))
4847 		<= mpred_mv_end) {
4848 #endif
4849 	pbi->m_BUF[i].v4l_ref_buf_addr = (ulong)fb;
4850 	pic->cma_alloc_addr = fb->m.mem[0].addr;
4851 	if (fb->num_planes == 1) {
4852 		pbi->m_BUF[i].start_adr = fb->m.mem[0].addr;
4853 		pbi->m_BUF[i].luma_size = fb->m.mem[0].offset;
4854 		pbi->m_BUF[i].size = fb->m.mem[0].size;
4855 		fb->m.mem[0].bytes_used = fb->m.mem[0].size;
4856 		pic->dw_y_adr = pbi->m_BUF[i].start_adr;
4857 		pic->dw_u_v_adr = pic->dw_y_adr + pbi->m_BUF[i].luma_size;
4858 	} else if (fb->num_planes == 2) {
4859 		pbi->m_BUF[i].start_adr = fb->m.mem[0].addr;
4860 		pbi->m_BUF[i].size = fb->m.mem[0].size;
4861 		pbi->m_BUF[i].chroma_addr = fb->m.mem[1].addr;
4862 		pbi->m_BUF[i].chroma_size = fb->m.mem[1].size;
4863 		fb->m.mem[0].bytes_used = fb->m.mem[0].size;
4864 		fb->m.mem[1].bytes_used = fb->m.mem[1].size;
4865 		pic->dw_y_adr = pbi->m_BUF[i].start_adr;
4866 		pic->dw_u_v_adr = pbi->m_BUF[i].chroma_addr;
4867 	}
4868 
4869 	/* config frame buffer */
4870 	if (pbi->mmu_enable)
4871 		pic->header_adr = pbi->m_BUF[i].header_addr;
4872 
4873 	pic->BUF_index		= i;
4874 	pic->lcu_total		= lcu_total;
4875 	pic->mc_canvas_y	= pic->index;
4876 	pic->mc_canvas_u_v	= pic->index;
4877 
4878 	if (dw_mode & 0x10) {
4879 		pic->mc_canvas_y = (pic->index << 1);
4880 		pic->mc_canvas_u_v = (pic->index << 1) + 1;
4881 	}
4882 
4883 #ifdef MV_USE_FIXED_BUF
4884 	pic->mpred_mv_wr_start_addr =
4885 		pbi->work_space_buf->mpred_mv.buf_start +
4886 		((pic->index * lcu_total) * MV_MEM_UNIT);
4887 #endif
4888 	if (debug) {
4889 		pr_info("%s index %d BUF_index %d ",
4890 			__func__, pic->index,
4891 			pic->BUF_index);
4892 		pr_info("comp_body_size %x comp_buf_size %x ",
4893 			pic->comp_body_size,
4894 			pic->buf_size);
4895 		pr_info("mpred_mv_wr_start_adr %ld\n",
4896 			pic->mpred_mv_wr_start_addr);
4897 		pr_info("dw_y_adr %d, pic_config->dw_u_v_adr =%d\n",
4898 			pic->dw_y_adr,
4899 			pic->dw_u_v_adr);
4900 	}
4901 #ifdef MV_USE_FIXED_BUF
4902 	}
4903 #endif
4904 	return ret;
4905 }
4906 
config_pic(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * pic_config)4907 static int config_pic(struct VP9Decoder_s *pbi,
4908 				struct PIC_BUFFER_CONFIG_s *pic_config)
4909 {
4910 	int ret = -1;
4911 	int i;
4912 	int pic_width = pbi->init_pic_w;
4913 	int pic_height = pbi->init_pic_h;
4914 	int lcu_size = 64; /*fixed 64*/
4915 	int pic_width_64 = (pic_width + 63) & (~0x3f);
4916 	int pic_height_32 = (pic_height + 31) & (~0x1f);
4917 	int pic_width_lcu  = (pic_width_64 % lcu_size) ?
4918 				pic_width_64 / lcu_size  + 1
4919 				: pic_width_64 / lcu_size;
4920 	int pic_height_lcu = (pic_height_32 % lcu_size) ?
4921 				pic_height_32 / lcu_size + 1
4922 				: pic_height_32 / lcu_size;
4923 	int lcu_total       = pic_width_lcu * pic_height_lcu;
4924 #ifdef MV_USE_FIXED_BUF
4925 	u32 mpred_mv_end = pbi->work_space_buf->mpred_mv.buf_start +
4926 			pbi->work_space_buf->mpred_mv.buf_size;
4927 #endif
4928 	u32 y_adr = 0;
4929 	int buf_size = 0;
4930 
4931 	int losless_comp_header_size =
4932 			compute_losless_comp_header_size(pic_width,
4933 			pic_height);
4934 	int losless_comp_body_size = compute_losless_comp_body_size(pic_width,
4935 			pic_height, buf_alloc_depth == 10);
4936 	int mc_buffer_size = losless_comp_header_size + losless_comp_body_size;
4937 	int mc_buffer_size_h = (mc_buffer_size + 0xffff) >> 16;
4938 	int mc_buffer_size_u_v = 0;
4939 	int mc_buffer_size_u_v_h = 0;
4940 	int dw_mode = get_double_write_mode_init(pbi);
4941 
4942 	pbi->lcu_total = lcu_total;
4943 
4944 	if (dw_mode) {
4945 		int pic_width_dw = pic_width /
4946 			get_double_write_ratio(pbi, dw_mode);
4947 		int pic_height_dw = pic_height /
4948 			get_double_write_ratio(pbi, dw_mode);
4949 
4950 		int pic_width_64_dw = (pic_width_dw + 63) & (~0x3f);
4951 		int pic_height_32_dw = (pic_height_dw + 31) & (~0x1f);
4952 		int pic_width_lcu_dw  = (pic_width_64_dw % lcu_size) ?
4953 					pic_width_64_dw / lcu_size  + 1
4954 					: pic_width_64_dw / lcu_size;
4955 		int pic_height_lcu_dw = (pic_height_32_dw % lcu_size) ?
4956 					pic_height_32_dw / lcu_size + 1
4957 					: pic_height_32_dw / lcu_size;
4958 		int lcu_total_dw       = pic_width_lcu_dw * pic_height_lcu_dw;
4959 		mc_buffer_size_u_v = lcu_total_dw * lcu_size * lcu_size / 2;
4960 		mc_buffer_size_u_v_h = (mc_buffer_size_u_v + 0xffff) >> 16;
4961 		/*64k alignment*/
4962 		buf_size = ((mc_buffer_size_u_v_h << 16) * 3);
4963 		buf_size = ((buf_size + 0xffff) >> 16) << 16;
4964 	}
4965 
4966 	if (mc_buffer_size & 0xffff) /*64k alignment*/
4967 		mc_buffer_size_h += 1;
4968 	if ((!pbi->mmu_enable) && ((dw_mode & 0x10) == 0))
4969 		buf_size += (mc_buffer_size_h << 16);
4970 
4971 	if (pbi->mmu_enable) {
4972 		pic_config->header_adr = decoder_bmmu_box_get_phy_addr(
4973 			pbi->bmmu_box, HEADER_BUFFER_IDX(pic_config->index));
4974 
4975 		if (debug & VP9_DEBUG_BUFMGR_MORE) {
4976 			pr_info("MMU header_adr %d: %ld\n",
4977 				pic_config->index, pic_config->header_adr);
4978 		}
4979 	}
4980 
4981 	i = pic_config->index;
4982 #ifdef MV_USE_FIXED_BUF
4983 	if ((pbi->work_space_buf->mpred_mv.buf_start +
4984 		(((i + 1) * lcu_total) * MV_MEM_UNIT))
4985 		<= mpred_mv_end
4986 	) {
4987 #endif
4988 		if (buf_size > 0) {
4989 			ret = decoder_bmmu_box_alloc_buf_phy(pbi->bmmu_box,
4990 					VF_BUFFER_IDX(i),
4991 					buf_size, DRIVER_NAME,
4992 					&pic_config->cma_alloc_addr);
4993 			if (ret < 0) {
4994 				pr_info(
4995 					"decoder_bmmu_box_alloc_buf_phy idx %d size %d fail\n",
4996 					VF_BUFFER_IDX(i),
4997 					buf_size
4998 					);
4999 				return ret;
5000 			}
5001 
5002 			if (pic_config->cma_alloc_addr)
5003 				y_adr = pic_config->cma_alloc_addr;
5004 			else {
5005 				pr_info(
5006 					"decoder_bmmu_box_alloc_buf_phy idx %d size %d return null\n",
5007 					VF_BUFFER_IDX(i),
5008 					buf_size
5009 					);
5010 				return -1;
5011 			}
5012 		}
5013 		{
5014 			/*ensure get_pic_by_POC()
5015 			not get the buffer not decoded*/
5016 			pic_config->BUF_index = i;
5017 			pic_config->lcu_total = lcu_total;
5018 
5019 			pic_config->comp_body_size = losless_comp_body_size;
5020 			pic_config->buf_size = buf_size;
5021 
5022 			pic_config->mc_canvas_y = pic_config->index;
5023 			pic_config->mc_canvas_u_v = pic_config->index;
5024 			if (dw_mode & 0x10) {
5025 				pic_config->dw_y_adr = y_adr;
5026 				pic_config->dw_u_v_adr = y_adr +
5027 					((mc_buffer_size_u_v_h << 16) << 1);
5028 
5029 				pic_config->mc_canvas_y =
5030 					(pic_config->index << 1);
5031 				pic_config->mc_canvas_u_v =
5032 					(pic_config->index << 1) + 1;
5033 			} else if (dw_mode) {
5034 				pic_config->dw_y_adr = y_adr;
5035 				pic_config->dw_u_v_adr = pic_config->dw_y_adr +
5036 				((mc_buffer_size_u_v_h << 16) << 1);
5037 			}
5038 #ifdef MV_USE_FIXED_BUF
5039 			pic_config->mpred_mv_wr_start_addr =
5040 			pbi->work_space_buf->mpred_mv.buf_start +
5041 					((pic_config->index * lcu_total)
5042 					* MV_MEM_UNIT);
5043 #endif
5044 			if (debug) {
5045 				pr_info
5046 				("%s index %d BUF_index %d ",
5047 				__func__, pic_config->index,
5048 				pic_config->BUF_index);
5049 				pr_info
5050 				("comp_body_size %x comp_buf_size %x ",
5051 				pic_config->comp_body_size,
5052 				pic_config->buf_size);
5053 				pr_info
5054 				("mpred_mv_wr_start_adr %ld\n",
5055 				pic_config->mpred_mv_wr_start_addr);
5056 				pr_info("dw_y_adr %d, pic_config->dw_u_v_adr =%d\n",
5057 					pic_config->dw_y_adr,
5058 					pic_config->dw_u_v_adr);
5059 			}
5060 			ret = 0;
5061 		}
5062 #ifdef MV_USE_FIXED_BUF
5063 	}
5064 #endif
5065 	return ret;
5066 }
5067 
vvp9_mmu_compress_header_size(struct VP9Decoder_s * pbi)5068 static int vvp9_mmu_compress_header_size(struct VP9Decoder_s *pbi)
5069 {
5070 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
5071 		IS_8K_SIZE(pbi->max_pic_w, pbi->max_pic_h))
5072 		return (MMU_COMPRESS_8K_HEADER_SIZE);
5073 
5074 	return (MMU_COMPRESS_HEADER_SIZE);
5075 }
5076 
5077 /*#define FRAME_MMU_MAP_SIZE  (MAX_FRAME_4K_NUM * 4)*/
vvp9_frame_mmu_map_size(struct VP9Decoder_s * pbi)5078 static int vvp9_frame_mmu_map_size(struct VP9Decoder_s *pbi)
5079 {
5080 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
5081 		IS_8K_SIZE(pbi->max_pic_w, pbi->max_pic_h))
5082 		return (MAX_FRAME_8K_NUM * 4);
5083 
5084 	return (MAX_FRAME_4K_NUM * 4);
5085 }
5086 
init_pic_list(struct VP9Decoder_s * pbi)5087 static void init_pic_list(struct VP9Decoder_s *pbi)
5088 {
5089 	int i;
5090 	struct VP9_Common_s *cm = &pbi->common;
5091 	struct PIC_BUFFER_CONFIG_s *pic_config;
5092 	u32 header_size;
5093 	struct vdec_s *vdec = hw_to_vdec(pbi);
5094 
5095 	if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
5096 		header_size = vvp9_mmu_compress_header_size(pbi);
5097 		/*alloc VP9 compress header first*/
5098 		for (i = 0; i < pbi->used_buf_num; i++) {
5099 			unsigned long buf_addr;
5100 			if (decoder_bmmu_box_alloc_buf_phy
5101 				(pbi->bmmu_box,
5102 				HEADER_BUFFER_IDX(i), header_size,
5103 				DRIVER_HEADER_NAME,
5104 				&buf_addr) < 0) {
5105 				pr_info("%s malloc compress header failed %d\n",
5106 				DRIVER_HEADER_NAME, i);
5107 				pbi->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
5108 				return;
5109 			}
5110 		}
5111 	}
5112 	for (i = 0; i < pbi->used_buf_num; i++) {
5113 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
5114 		pic_config->index = i;
5115 		pic_config->BUF_index = -1;
5116 		pic_config->mv_buf_index = -1;
5117 		if (vdec->parallel_dec == 1) {
5118 			pic_config->y_canvas_index = -1;
5119 			pic_config->uv_canvas_index = -1;
5120 		}
5121 		pic_config->y_crop_width = pbi->init_pic_w;
5122 		pic_config->y_crop_height = pbi->init_pic_h;
5123 		pic_config->double_write_mode = get_double_write_mode(pbi);
5124 
5125 		if (!pbi->is_used_v4l) {
5126 			if (config_pic(pbi, pic_config) < 0) {
5127 				if (debug)
5128 					pr_info("Config_pic %d fail\n",
5129 						pic_config->index);
5130 				pic_config->index = -1;
5131 				break;
5132 			}
5133 
5134 			if (pic_config->double_write_mode) {
5135 				set_canvas(pbi, pic_config);
5136 			}
5137 		}
5138 	}
5139 	for (; i < pbi->used_buf_num; i++) {
5140 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
5141 		pic_config->index = -1;
5142 		pic_config->BUF_index = -1;
5143 		pic_config->mv_buf_index = -1;
5144 		if (vdec->parallel_dec == 1) {
5145 			pic_config->y_canvas_index = -1;
5146 			pic_config->uv_canvas_index = -1;
5147 		}
5148 	}
5149 	pr_info("%s ok, used_buf_num = %d\n",
5150 		__func__, pbi->used_buf_num);
5151 }
5152 
init_pic_list_hw(struct VP9Decoder_s * pbi)5153 static void init_pic_list_hw(struct VP9Decoder_s *pbi)
5154 {
5155 	int i;
5156 	struct VP9_Common_s *cm = &pbi->common;
5157 	struct PIC_BUFFER_CONFIG_s *pic_config;
5158 	/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x0);*/
5159 	WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
5160 		(0x1 << 1) | (0x1 << 2));
5161 
5162 
5163 	for (i = 0; i < pbi->used_buf_num; i++) {
5164 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
5165 		if (pic_config->index < 0)
5166 			break;
5167 
5168 		if (pbi->mmu_enable && ((pic_config->double_write_mode & 0x10) == 0)) {
5169 
5170 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
5171 				pic_config->header_adr >> 5);
5172 		} else {
5173 			/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
5174 			 *	pic_config->mc_y_adr
5175 			 *	| (pic_config->mc_canvas_y << 8) | 0x1);
5176 			 */
5177 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
5178 				pic_config->dw_y_adr >> 5);
5179 		}
5180 #ifndef LOSLESS_COMPRESS_MODE
5181 		/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
5182 		 *	pic_config->mc_u_v_adr
5183 		 *	| (pic_config->mc_canvas_u_v << 8)| 0x1);
5184 		 */
5185 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
5186 				pic_config->header_adr >> 5);
5187 #else
5188 		if (pic_config->double_write_mode & 0x10) {
5189 				WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
5190 				pic_config->dw_u_v_adr >> 5);
5191 		}
5192 #endif
5193 	}
5194 	WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x1);
5195 
5196 	/*Zero out canvas registers in IPP -- avoid simulation X*/
5197 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
5198 			(0 << 8) | (0 << 1) | 1);
5199 	for (i = 0; i < 32; i++)
5200 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
5201 }
5202 
5203 
dump_pic_list(struct VP9Decoder_s * pbi)5204 static void dump_pic_list(struct VP9Decoder_s *pbi)
5205 {
5206 	struct VP9_Common_s *const cm = &pbi->common;
5207 	struct PIC_BUFFER_CONFIG_s *pic_config;
5208 	int i;
5209 	for (i = 0; i < FRAME_BUFFERS; i++) {
5210 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
5211 		vp9_print(pbi, 0,
5212 			"Buf(%d) index %d mv_buf_index %d ref_count %d vf_ref %d dec_idx %d slice_type %d w/h %d/%d adr%ld\n",
5213 			i,
5214 			pic_config->index,
5215 #ifndef MV_USE_FIXED_BUF
5216 			pic_config->mv_buf_index,
5217 #else
5218 			-1,
5219 #endif
5220 			cm->buffer_pool->
5221 			frame_bufs[i].ref_count,
5222 			pic_config->vf_ref,
5223 			pic_config->decode_idx,
5224 			pic_config->slice_type,
5225 			pic_config->y_crop_width,
5226 			pic_config->y_crop_height,
5227 			pic_config->cma_alloc_addr
5228 			);
5229 	}
5230 	return;
5231 }
5232 
config_pic_size(struct VP9Decoder_s * pbi,unsigned short bit_depth)5233 static int config_pic_size(struct VP9Decoder_s *pbi, unsigned short bit_depth)
5234 {
5235 #ifdef LOSLESS_COMPRESS_MODE
5236 	unsigned int data32;
5237 #endif
5238 	int losless_comp_header_size, losless_comp_body_size;
5239 	struct VP9_Common_s *cm = &pbi->common;
5240 	struct PIC_BUFFER_CONFIG_s *cur_pic_config = &cm->cur_frame->buf;
5241 
5242 	frame_width = cur_pic_config->y_crop_width;
5243 	frame_height = cur_pic_config->y_crop_height;
5244 	cur_pic_config->bit_depth = bit_depth;
5245 	cur_pic_config->double_write_mode = get_double_write_mode(pbi);
5246 	losless_comp_header_size =
5247 		compute_losless_comp_header_size(cur_pic_config->y_crop_width,
5248 		cur_pic_config->y_crop_height);
5249 	losless_comp_body_size =
5250 		compute_losless_comp_body_size(cur_pic_config->y_crop_width,
5251 		cur_pic_config->y_crop_height, (bit_depth == VPX_BITS_10));
5252 	cur_pic_config->comp_body_size = losless_comp_body_size;
5253 #ifdef LOSLESS_COMPRESS_MODE
5254 	data32 = READ_VREG(HEVC_SAO_CTRL5);
5255 	if (bit_depth == VPX_BITS_10)
5256 		data32 &= ~(1 << 9);
5257 	else
5258 		data32 |= (1 << 9);
5259 
5260 	WRITE_VREG(HEVC_SAO_CTRL5, data32);
5261 
5262 	if (pbi->mmu_enable) {
5263 		/*bit[4] : paged_mem_mode*/
5264 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0x1 << 4));
5265 	} else {
5266 	/*bit[3] smem mdoe*/
5267 		if (bit_depth == VPX_BITS_10)
5268 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0 << 3));
5269 		else
5270 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (1 << 3));
5271 	}
5272 	if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_SM1)
5273 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, (losless_comp_body_size >> 5));
5274 	/*WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,(0xff<<20) | (0xff<<10) | 0xff);*/
5275 	WRITE_VREG(HEVC_CM_BODY_LENGTH, losless_comp_body_size);
5276 	WRITE_VREG(HEVC_CM_HEADER_OFFSET, losless_comp_body_size);
5277 	WRITE_VREG(HEVC_CM_HEADER_LENGTH, losless_comp_header_size);
5278 	if (get_double_write_mode(pbi) & 0x10)
5279 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
5280 #else
5281 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
5282 #endif
5283 	return 0;
5284 }
5285 
config_mc_buffer(struct VP9Decoder_s * pbi,unsigned short bit_depth)5286 static int config_mc_buffer(struct VP9Decoder_s *pbi, unsigned short bit_depth)
5287 {
5288 	int i;
5289 	struct VP9_Common_s *cm = &pbi->common;
5290 	struct PIC_BUFFER_CONFIG_s *cur_pic_config = &cm->cur_frame->buf;
5291 	uint8_t scale_enable = 0;
5292 
5293 	if (debug&VP9_DEBUG_BUFMGR_MORE)
5294 		pr_info("config_mc_buffer entered .....\n");
5295 
5296 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
5297 			(0 << 8) | (0 << 1) | 1);
5298 	for (i = 0; i < REFS_PER_FRAME; ++i) {
5299 		struct PIC_BUFFER_CONFIG_s *pic_config = cm->frame_refs[i].buf;
5300 		if (!pic_config)
5301 			continue;
5302 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
5303 		(pic_config->mc_canvas_u_v << 16)
5304 		| (pic_config->mc_canvas_u_v << 8)
5305 		| pic_config->mc_canvas_y);
5306 		if (debug & VP9_DEBUG_BUFMGR_MORE)
5307 			pr_info("refid %x mc_canvas_u_v %x mc_canvas_y %x\n",
5308 				i, pic_config->mc_canvas_u_v,
5309 				pic_config->mc_canvas_y);
5310 	}
5311 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
5312 			(16 << 8) | (0 << 1) | 1);
5313 	for (i = 0; i < REFS_PER_FRAME; ++i) {
5314 		struct PIC_BUFFER_CONFIG_s *pic_config = cm->frame_refs[i].buf;
5315 		if (!pic_config)
5316 			continue;
5317 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
5318 			(pic_config->mc_canvas_u_v << 16)
5319 			| (pic_config->mc_canvas_u_v << 8)
5320 			| pic_config->mc_canvas_y);
5321 	}
5322 
5323 	/*auto_inc start index:0 field:0*/
5324 	WRITE_VREG(VP9D_MPP_REFINFO_TBL_ACCCONFIG, 0x1 << 2);
5325 	/*index 0:last 1:golden 2:altref*/
5326 	for (i = 0; i < REFS_PER_FRAME; i++) {
5327 		int ref_pic_body_size;
5328 		struct PIC_BUFFER_CONFIG_s *pic_config = cm->frame_refs[i].buf;
5329 		if (!pic_config)
5330 			continue;
5331 		WRITE_VREG(VP9D_MPP_REFINFO_DATA, pic_config->y_crop_width);
5332 		WRITE_VREG(VP9D_MPP_REFINFO_DATA, pic_config->y_crop_height);
5333 
5334 	if (pic_config->y_crop_width != cur_pic_config->y_crop_width ||
5335 		pic_config->y_crop_height != cur_pic_config->y_crop_height) {
5336 		scale_enable |= (1 << i);
5337 	}
5338 	ref_pic_body_size =
5339 		compute_losless_comp_body_size(pic_config->y_crop_width,
5340 		pic_config->y_crop_height, (bit_depth == VPX_BITS_10));
5341 	WRITE_VREG(VP9D_MPP_REFINFO_DATA,
5342 		(pic_config->y_crop_width << 14)
5343 		/ cur_pic_config->y_crop_width);
5344 	WRITE_VREG(VP9D_MPP_REFINFO_DATA,
5345 		(pic_config->y_crop_height << 14)
5346 		/ cur_pic_config->y_crop_height);
5347 	if (pbi->mmu_enable)
5348 		WRITE_VREG(VP9D_MPP_REFINFO_DATA, 0);
5349 	else
5350 		WRITE_VREG(VP9D_MPP_REFINFO_DATA, ref_pic_body_size >> 5);
5351 	}
5352 	WRITE_VREG(VP9D_MPP_REF_SCALE_ENBL, scale_enable);
5353 	return 0;
5354 }
5355 
clear_mpred_hw(struct VP9Decoder_s * pbi)5356 static void clear_mpred_hw(struct VP9Decoder_s *pbi)
5357 {
5358 	unsigned int data32;
5359 
5360 	data32 = READ_VREG(HEVC_MPRED_CTRL4);
5361 	data32 &=  (~(1 << 6));
5362 	WRITE_VREG(HEVC_MPRED_CTRL4, data32);
5363 }
5364 
config_mpred_hw(struct VP9Decoder_s * pbi)5365 static void config_mpred_hw(struct VP9Decoder_s *pbi)
5366 {
5367 	struct VP9_Common_s *cm = &pbi->common;
5368 	struct PIC_BUFFER_CONFIG_s *cur_pic_config = &cm->cur_frame->buf;
5369 	struct PIC_BUFFER_CONFIG_s *last_frame_pic_config =
5370 						&cm->prev_frame->buf;
5371 
5372 	unsigned int data32;
5373 	int     mpred_curr_lcu_x;
5374 	int     mpred_curr_lcu_y;
5375 	int     mpred_mv_rd_end_addr;
5376 
5377 
5378 	mpred_mv_rd_end_addr = last_frame_pic_config->mpred_mv_wr_start_addr
5379 			+ (last_frame_pic_config->lcu_total * MV_MEM_UNIT);
5380 
5381 	data32 = READ_VREG(HEVC_MPRED_CURR_LCU);
5382 	mpred_curr_lcu_x   = data32 & 0xffff;
5383 	mpred_curr_lcu_y   = (data32 >> 16) & 0xffff;
5384 
5385 	if (debug & VP9_DEBUG_BUFMGR)
5386 		pr_info("cur pic_config index %d  col pic_config index %d\n",
5387 			cur_pic_config->index, last_frame_pic_config->index);
5388 	WRITE_VREG(HEVC_MPRED_CTRL3, 0x24122412);
5389 	WRITE_VREG(HEVC_MPRED_ABV_START_ADDR,
5390 			pbi->work_space_buf->mpred_above.buf_start);
5391 
5392 	data32 = READ_VREG(HEVC_MPRED_CTRL4);
5393 
5394 	data32 &=  (~(1 << 6));
5395 	data32 |= (cm->use_prev_frame_mvs << 6);
5396 	WRITE_VREG(HEVC_MPRED_CTRL4, data32);
5397 
5398 	WRITE_VREG(HEVC_MPRED_MV_WR_START_ADDR,
5399 			cur_pic_config->mpred_mv_wr_start_addr);
5400 	WRITE_VREG(HEVC_MPRED_MV_WPTR, cur_pic_config->mpred_mv_wr_start_addr);
5401 
5402 	WRITE_VREG(HEVC_MPRED_MV_RD_START_ADDR,
5403 			last_frame_pic_config->mpred_mv_wr_start_addr);
5404 	WRITE_VREG(HEVC_MPRED_MV_RPTR,
5405 			last_frame_pic_config->mpred_mv_wr_start_addr);
5406 	/*data32 = ((pbi->lcu_x_num - pbi->tile_width_lcu)*MV_MEM_UNIT);*/
5407 	/*WRITE_VREG(HEVC_MPRED_MV_WR_ROW_JUMP,data32);*/
5408 	/*WRITE_VREG(HEVC_MPRED_MV_RD_ROW_JUMP,data32);*/
5409 	WRITE_VREG(HEVC_MPRED_MV_RD_END_ADDR, mpred_mv_rd_end_addr);
5410 
5411 }
5412 
config_sao_hw(struct VP9Decoder_s * pbi,union param_u * params)5413 static void config_sao_hw(struct VP9Decoder_s *pbi, union param_u *params)
5414 {
5415 	struct VP9_Common_s *cm = &pbi->common;
5416 	struct PIC_BUFFER_CONFIG_s *pic_config = &cm->cur_frame->buf;
5417 
5418 	unsigned int data32;
5419 	int lcu_size = 64;
5420 	int mc_buffer_size_u_v =
5421 		pic_config->lcu_total * lcu_size*lcu_size/2;
5422 	int mc_buffer_size_u_v_h =
5423 		(mc_buffer_size_u_v + 0xffff) >> 16;/*64k alignment*/
5424 	struct aml_vcodec_ctx * v4l2_ctx = pbi->v4l2_ctx;
5425 
5426 	if (get_double_write_mode(pbi)) {
5427 		WRITE_VREG(HEVC_SAO_Y_START_ADDR, pic_config->dw_y_adr);
5428 		WRITE_VREG(HEVC_SAO_C_START_ADDR, pic_config->dw_u_v_adr);
5429 		WRITE_VREG(HEVC_SAO_Y_WPTR, pic_config->dw_y_adr);
5430 		WRITE_VREG(HEVC_SAO_C_WPTR, pic_config->dw_u_v_adr);
5431 	} else {
5432 		WRITE_VREG(HEVC_SAO_Y_START_ADDR, 0xffffffff);
5433 		WRITE_VREG(HEVC_SAO_C_START_ADDR, 0xffffffff);
5434 	}
5435 	if (pbi->mmu_enable)
5436 		WRITE_VREG(HEVC_CM_HEADER_START_ADDR, pic_config->header_adr);
5437 
5438 	data32 = (mc_buffer_size_u_v_h << 16) << 1;
5439 	/*pr_info("data32=%x,mc_buffer_size_u_v_h=%x,lcu_total=%x\n",
5440 	 *	data32, mc_buffer_size_u_v_h, pic_config->lcu_total);
5441 	 */
5442 	WRITE_VREG(HEVC_SAO_Y_LENGTH, data32);
5443 
5444 	data32 = (mc_buffer_size_u_v_h << 16);
5445 	WRITE_VREG(HEVC_SAO_C_LENGTH, data32);
5446 
5447 #ifdef VP9_10B_NV21
5448 #ifdef DOS_PROJECT
5449 	data32 = READ_VREG(HEVC_SAO_CTRL1);
5450 	data32 &= (~0x3000);
5451 	/*[13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32*/
5452 	data32 |= (pbi->mem_map_mode << 12);
5453 	data32 &= (~0x3);
5454 	data32 |= 0x1; /* [1]:dw_disable [0]:cm_disable*/
5455 	WRITE_VREG(HEVC_SAO_CTRL1, data32);
5456 	/*[23:22] dw_v1_ctrl [21:20] dw_v0_ctrl [19:18] dw_h1_ctrl
5457 	 *	[17:16] dw_h0_ctrl
5458 	 */
5459 	data32 = READ_VREG(HEVC_SAO_CTRL5);
5460 	/*set them all 0 for H265_NV21 (no down-scale)*/
5461 	data32 &= ~(0xff << 16);
5462 	WRITE_VREG(HEVC_SAO_CTRL5, data32);
5463 	data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
5464 	data32 &= (~0x30);
5465 	/*[5:4] address_format 00:linear 01:32x32 10:64x32*/
5466 	data32 |= (pbi->mem_map_mode << 4);
5467 	WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
5468 #else
5469 	/*m8baby test1902*/
5470 	data32 = READ_VREG(HEVC_SAO_CTRL1);
5471 	data32 &= (~0x3000);
5472 	/*[13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32*/
5473 	data32 |= (pbi->mem_map_mode << 12);
5474 	data32 &= (~0xff0);
5475 	/*data32 |= 0x670;*/ /*Big-Endian per 64-bit*/
5476 	data32 |= 0x880;  /*.Big-Endian per 64-bit */
5477 	data32 &= (~0x3);
5478 	data32 |= 0x1; /*[1]:dw_disable [0]:cm_disable*/
5479 	WRITE_VREG(HEVC_SAO_CTRL1, data32);
5480 	/* [23:22] dw_v1_ctrl [21:20] dw_v0_ctrl
5481 	 *[19:18] dw_h1_ctrl [17:16] dw_h0_ctrl
5482 	 */
5483 	data32 = READ_VREG(HEVC_SAO_CTRL5);
5484 	/* set them all 0 for H265_NV21 (no down-scale)*/
5485 	data32 &= ~(0xff << 16);
5486 	WRITE_VREG(HEVC_SAO_CTRL5, data32);
5487 
5488 	data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
5489 	data32 &= (~0x30);
5490 	/*[5:4] address_format 00:linear 01:32x32 10:64x32*/
5491 	data32 |= (pbi->mem_map_mode << 4);
5492 	data32 &= (~0xF);
5493 	data32 |= 0x8; /*Big-Endian per 64-bit*/
5494 	WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
5495 #endif
5496 #else
5497 	data32 = READ_VREG(HEVC_SAO_CTRL1);
5498 	data32 &= (~0x3000);
5499 	data32 |= (pbi->mem_map_mode <<
5500 			   12);
5501 
5502 /*  [13:12] axi_aformat, 0-Linear,
5503  *				   1-32x32, 2-64x32
5504  */
5505 	data32 &= (~0xff0);
5506 	/* data32 |= 0x670;  // Big-Endian per 64-bit */
5507 	data32 |= endian;	/* Big-Endian per 64-bit */
5508 	data32 &= (~0x3); /*[1]:dw_disable [0]:cm_disable*/
5509 	if (get_double_write_mode(pbi) == 0)
5510 		data32 |= 0x2; /*disable double write*/
5511 	else if (get_double_write_mode(pbi) & 0x10)
5512 		data32 |= 0x1; /*disable cm*/
5513 	 if  (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) { /* >= G12A dw write control */
5514 		unsigned int data;
5515 		data = READ_VREG(HEVC_DBLK_CFGB);
5516 		data &= (~0x300); /*[8]:first write enable (compress)  [9]:double write enable (uncompress)*/
5517 		if (get_double_write_mode(pbi) == 0)
5518 			data |= (0x1 << 8); /*enable first write*/
5519 		else if (get_double_write_mode(pbi) & 0x10)
5520 			data |= (0x1 << 9); /*double write only*/
5521 		else
5522 			data |= ((0x1 << 8)  |(0x1 << 9));
5523 		WRITE_VREG(HEVC_DBLK_CFGB, data);
5524 	}
5525 
5526 	/* swap uv */
5527 	if (pbi->is_used_v4l) {
5528 		if ((v4l2_ctx->q_data[AML_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_NV21) ||
5529 			(v4l2_ctx->q_data[AML_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_NV21M))
5530 			data32 &= ~(1 << 8); /* NV21 */
5531 		else
5532 			data32 |= (1 << 8); /* NV12 */
5533 	}
5534 
5535 	/*
5536 	*  [31:24] ar_fifo1_axi_thred
5537 	*  [23:16] ar_fifo0_axi_thred
5538 	*  [15:14] axi_linealign, 0-16bytes, 1-32bytes, 2-64bytes
5539 	*  [13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32
5540 	*  [11:08] axi_lendian_C
5541 	*  [07:04] axi_lendian_Y
5542 	*  [3]     reserved
5543 	*  [2]     clk_forceon
5544 	*  [1]     dw_disable:disable double write output
5545 	*  [0]     cm_disable:disable compress output
5546 	*/
5547 	WRITE_VREG(HEVC_SAO_CTRL1, data32);
5548 
5549 	if (get_double_write_mode(pbi) & 0x10) {
5550 		/* [23:22] dw_v1_ctrl
5551 		 *[21:20] dw_v0_ctrl
5552 		 *[19:18] dw_h1_ctrl
5553 		 *[17:16] dw_h0_ctrl
5554 		 */
5555 		data32 = READ_VREG(HEVC_SAO_CTRL5);
5556 		/*set them all 0 for H265_NV21 (no down-scale)*/
5557 		data32 &= ~(0xff << 16);
5558 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
5559 	} else {
5560 		data32 = READ_VREG(HEVC_SAO_CTRL5);
5561 		data32 &= (~(0xff << 16));
5562 		if (get_double_write_mode(pbi) == 2 ||
5563 			get_double_write_mode(pbi) == 3)
5564 			data32 |= (0xff<<16);
5565 		else if (get_double_write_mode(pbi) == 4)
5566 			data32 |= (0x33<<16);
5567 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
5568 	}
5569 
5570 	data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
5571 	data32 &= (~0x30);
5572 	/* [5:4]    -- address_format 00:linear 01:32x32 10:64x32 */
5573 	data32 |= (pbi->mem_map_mode <<
5574 			   4);
5575 	data32 &= (~0xF);
5576 	data32 |= 0xf;  /* valid only when double write only */
5577 		/*data32 |= 0x8;*/		/* Big-Endian per 64-bit */
5578 
5579 	/* swap uv */
5580 	if (pbi->is_used_v4l) {
5581 		if ((v4l2_ctx->q_data[AML_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_NV21) ||
5582 			(v4l2_ctx->q_data[AML_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_NV21M))
5583 			data32 |= (1 << 12); /* NV21 */
5584 		else
5585 			data32 &= ~(1 << 12); /* NV12 */
5586 	}
5587 
5588 	/*
5589 	* [3:0]   little_endian
5590 	* [5:4]   address_format 00:linear 01:32x32 10:64x32
5591 	* [7:6]   reserved
5592 	* [9:8]   Linear_LineAlignment 00:16byte 01:32byte 10:64byte
5593 	* [11:10] reserved
5594 	* [12]    CbCr_byte_swap
5595 	* [31:13] reserved
5596 	*/
5597 	WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
5598 #endif
5599 }
5600 
vp9_config_work_space_hw(struct VP9Decoder_s * pbi,u32 mask)5601 static void vp9_config_work_space_hw(struct VP9Decoder_s *pbi, u32 mask)
5602 {
5603 	struct BuffInfo_s *buf_spec = pbi->work_space_buf;
5604 	unsigned int data32;
5605 
5606 	if (debug && pbi->init_flag == 0)
5607 		pr_info("%s %x %x %x %x %x %x %x %x %x %x %x %x\n",
5608 			__func__,
5609 			buf_spec->ipp.buf_start,
5610 			buf_spec->start_adr,
5611 			buf_spec->short_term_rps.buf_start,
5612 			buf_spec->vps.buf_start,
5613 			buf_spec->sps.buf_start,
5614 			buf_spec->pps.buf_start,
5615 			buf_spec->sao_up.buf_start,
5616 			buf_spec->swap_buf.buf_start,
5617 			buf_spec->swap_buf2.buf_start,
5618 			buf_spec->scalelut.buf_start,
5619 			buf_spec->dblk_para.buf_start,
5620 			buf_spec->dblk_data.buf_start);
5621 
5622 	if (mask & HW_MASK_FRONT) {
5623 		if ((debug & VP9_DEBUG_SEND_PARAM_WITH_REG) == 0)
5624 			WRITE_VREG(HEVC_RPM_BUFFER, (u32)pbi->rpm_phy_addr);
5625 
5626 		WRITE_VREG(HEVC_SHORT_TERM_RPS,
5627 			buf_spec->short_term_rps.buf_start);
5628 		/*WRITE_VREG(HEVC_VPS_BUFFER, buf_spec->vps.buf_start);*/
5629 		/*WRITE_VREG(HEVC_SPS_BUFFER, buf_spec->sps.buf_start);*/
5630 		WRITE_VREG(HEVC_PPS_BUFFER, buf_spec->pps.buf_start);
5631 		WRITE_VREG(HEVC_STREAM_SWAP_BUFFER,
5632 			buf_spec->swap_buf.buf_start);
5633 		WRITE_VREG(HEVC_STREAM_SWAP_BUFFER2,
5634 			buf_spec->swap_buf2.buf_start);
5635 		WRITE_VREG(LMEM_DUMP_ADR, (u32)pbi->lmem_phy_addr);
5636 
5637 	}
5638 
5639 	if (mask & HW_MASK_BACK) {
5640 #ifdef LOSLESS_COMPRESS_MODE
5641 		int losless_comp_header_size =
5642 			compute_losless_comp_header_size(pbi->init_pic_w,
5643 			pbi->init_pic_h);
5644 		int losless_comp_body_size =
5645 			compute_losless_comp_body_size(pbi->init_pic_w,
5646 			pbi->init_pic_h, buf_alloc_depth == 10);
5647 #endif
5648 		WRITE_VREG(HEVCD_IPP_LINEBUFF_BASE,
5649 			buf_spec->ipp.buf_start);
5650 		WRITE_VREG(HEVC_SAO_UP, buf_spec->sao_up.buf_start);
5651 		WRITE_VREG(HEVC_SCALELUT, buf_spec->scalelut.buf_start);
5652 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
5653 			/* cfg_addr_adp*/
5654 			WRITE_VREG(HEVC_DBLK_CFGE, buf_spec->dblk_para.buf_start);
5655 			if (debug & VP9_DEBUG_BUFMGR_MORE)
5656 				pr_info("Write HEVC_DBLK_CFGE\n");
5657 		}
5658 		/* cfg_p_addr */
5659 		WRITE_VREG(HEVC_DBLK_CFG4, buf_spec->dblk_para.buf_start);
5660 		/* cfg_d_addr */
5661 		WRITE_VREG(HEVC_DBLK_CFG5, buf_spec->dblk_data.buf_start);
5662 
5663 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
5664 		 /*
5665 	     * data32 = (READ_VREG(P_HEVC_DBLK_CFG3)>>8) & 0xff; // xio left offset, default is 0x40
5666 	     * data32 = data32 * 2;
5667 	     * data32 = (READ_VREG(P_HEVC_DBLK_CFG3)>>16) & 0xff; // adp left offset, default is 0x040
5668 	     * data32 = data32 * 2;
5669 	     */
5670 		WRITE_VREG(HEVC_DBLK_CFG3, 0x808010); // make left storage 2 x 4k]
5671 	}
5672 #ifdef LOSLESS_COMPRESS_MODE
5673 	if (pbi->mmu_enable) {
5674 		/*bit[4] : paged_mem_mode*/
5675 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0x1 << 4));
5676 		if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_SM1)
5677 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, 0);
5678 	} else {
5679 		/*if(cur_pic_config->bit_depth == VPX_BITS_10)
5680 		 *	WRITE_VREG(P_HEVCD_MPP_DECOMP_CTL1, (0<<3));
5681 		 */
5682 		/*bit[3] smem mdoe*/
5683 		/*else WRITE_VREG(P_HEVCD_MPP_DECOMP_CTL1, (1<<3));*/
5684 		/*bit[3] smem mdoe*/
5685 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,
5686 			(losless_comp_body_size >> 5));
5687 	}
5688 	/*WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,
5689 		(losless_comp_body_size >> 5));*/
5690 	/*WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,
5691 		(0xff<<20) | (0xff<<10) | 0xff);*/
5692 	/*8-bit mode */
5693 	WRITE_VREG(HEVC_CM_BODY_LENGTH, losless_comp_body_size);
5694 	WRITE_VREG(HEVC_CM_HEADER_OFFSET, losless_comp_body_size);
5695 	WRITE_VREG(HEVC_CM_HEADER_LENGTH, losless_comp_header_size);
5696 	if (get_double_write_mode(pbi) & 0x10)
5697 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
5698 #else
5699 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
5700 #endif
5701 
5702 	if (pbi->mmu_enable) {
5703 		WRITE_VREG(HEVC_SAO_MMU_VH0_ADDR, buf_spec->mmu_vbh.buf_start);
5704 		WRITE_VREG(HEVC_SAO_MMU_VH1_ADDR, buf_spec->mmu_vbh.buf_start
5705 				+ buf_spec->mmu_vbh.buf_size/2);
5706 		/*data32 = READ_VREG(P_HEVC_SAO_CTRL9);*/
5707 		/*data32 |= 0x1;*/
5708 		/*WRITE_VREG(P_HEVC_SAO_CTRL9, data32);*/
5709 
5710 		/* use HEVC_CM_HEADER_START_ADDR */
5711 		data32 = READ_VREG(HEVC_SAO_CTRL5);
5712 		data32 |= (1<<10);
5713 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
5714 	}
5715 
5716 		WRITE_VREG(VP9_SEG_MAP_BUFFER, buf_spec->seg_map.buf_start);
5717 
5718 	WRITE_VREG(LMEM_DUMP_ADR, (u32)pbi->lmem_phy_addr);
5719 		 /**/
5720 		WRITE_VREG(VP9_PROB_SWAP_BUFFER, pbi->prob_buffer_phy_addr);
5721 		WRITE_VREG(VP9_COUNT_SWAP_BUFFER, pbi->count_buffer_phy_addr);
5722 		if (pbi->mmu_enable) {
5723 			if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A)
5724 				WRITE_VREG(HEVC_ASSIST_MMU_MAP_ADDR, pbi->frame_mmu_map_phy_addr);
5725 			else
5726 				WRITE_VREG(VP9_MMU_MAP_BUFFER, pbi->frame_mmu_map_phy_addr);
5727 		}
5728 	}
5729 }
5730 
5731 
5732 #ifdef VP9_LPF_LVL_UPDATE
5733 /*
5734  * Defines, declarations, sub-functions for vp9 de-block loop
5735 	filter Thr/Lvl table update
5736  * - struct segmentation is for loop filter only (removed something)
5737  * - function "vp9_loop_filter_init" and "vp9_loop_filter_frame_init" will
5738 	be instantiated in C_Entry
5739  * - vp9_loop_filter_init run once before decoding start
5740  * - vp9_loop_filter_frame_init run before every frame decoding start
5741  * - set video format to VP9 is in vp9_loop_filter_init
5742  */
5743 #define MAX_LOOP_FILTER 63
5744 #define MAX_REF_LF_DELTAS 4
5745 #define MAX_MODE_LF_DELTAS 2
5746 /*#define INTRA_FRAME 0*/
5747 /*#define LAST_FRAME 1*/
5748 /*#define MAX_REF_FRAMES 4*/
5749 #define SEGMENT_DELTADATA   0
5750 #define SEGMENT_ABSDATA     1
5751 #define MAX_SEGMENTS     8
5752 /*.#define SEG_TREE_PROBS   (MAX_SEGMENTS-1)*/
5753 /*no use for loop filter, if this struct for common use, pls add it back*/
5754 /*#define PREDICTION_PROBS 3*/
5755 /* no use for loop filter, if this struct for common use, pls add it back*/
5756 
5757 enum SEG_LVL_FEATURES {
5758 	SEG_LVL_ALT_Q = 0, /*Use alternate Quantizer ....*/
5759 	SEG_LVL_ALT_LF = 1, /*Use alternate loop filter value...*/
5760 	SEG_LVL_REF_FRAME = 2, /*Optional Segment reference frame*/
5761 	SEG_LVL_SKIP = 3,  /*Optional Segment (0,0) + skip mode*/
5762 	SEG_LVL_MAX = 4    /*Number of features supported*/
5763 };
5764 
5765 struct segmentation {
5766 	uint8_t enabled;
5767 	uint8_t update_map;
5768 	uint8_t update_data;
5769 	uint8_t abs_delta;
5770 	uint8_t temporal_update;
5771 
5772 	/*no use for loop filter, if this struct
5773 	 *for common use, pls add it back
5774 	 */
5775 	/*vp9_prob tree_probs[SEG_TREE_PROBS]; */
5776 	/* no use for loop filter, if this struct
5777 	 *	for common use, pls add it back
5778 	 */
5779 	/*vp9_prob pred_probs[PREDICTION_PROBS];*/
5780 
5781 	int16_t feature_data[MAX_SEGMENTS][SEG_LVL_MAX];
5782 	unsigned int feature_mask[MAX_SEGMENTS];
5783 };
5784 
5785 struct loop_filter_thresh {
5786 	uint8_t mblim;
5787 	uint8_t lim;
5788 	uint8_t hev_thr;
5789 };
5790 
5791 struct loop_filter_info_n {
5792 	struct loop_filter_thresh lfthr[MAX_LOOP_FILTER + 1];
5793 	uint8_t lvl[MAX_SEGMENTS][MAX_REF_FRAMES][MAX_MODE_LF_DELTAS];
5794 };
5795 
5796 struct loopfilter {
5797 	int filter_level;
5798 
5799 	int sharpness_level;
5800 	int last_sharpness_level;
5801 
5802 	uint8_t mode_ref_delta_enabled;
5803 	uint8_t mode_ref_delta_update;
5804 
5805 	/*0 = Intra, Last, GF, ARF*/
5806 	signed char ref_deltas[MAX_REF_LF_DELTAS];
5807 	signed char last_ref_deltas[MAX_REF_LF_DELTAS];
5808 
5809 	/*0 = ZERO_MV, MV*/
5810 	signed char mode_deltas[MAX_MODE_LF_DELTAS];
5811 	signed char last_mode_deltas[MAX_MODE_LF_DELTAS];
5812 };
5813 
vp9_clamp(int value,int low,int high)5814 static int vp9_clamp(int value, int low, int high)
5815 {
5816 	return value < low ? low : (value > high ? high : value);
5817 }
5818 
segfeature_active(struct segmentation * seg,int segment_id,enum SEG_LVL_FEATURES feature_id)5819 int segfeature_active(struct segmentation *seg,
5820 			int segment_id,
5821 			enum SEG_LVL_FEATURES feature_id) {
5822 	return seg->enabled &&
5823 		(seg->feature_mask[segment_id] & (1 << feature_id));
5824 }
5825 
get_segdata(struct segmentation * seg,int segment_id,enum SEG_LVL_FEATURES feature_id)5826 int get_segdata(struct segmentation *seg, int segment_id,
5827 				enum SEG_LVL_FEATURES feature_id) {
5828 	return seg->feature_data[segment_id][feature_id];
5829 }
5830 
vp9_update_sharpness(struct loop_filter_info_n * lfi,int sharpness_lvl)5831 static void vp9_update_sharpness(struct loop_filter_info_n *lfi,
5832 					int sharpness_lvl)
5833 {
5834 	int lvl;
5835 	/*For each possible value for the loop filter fill out limits*/
5836 	for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
5837 		/*Set loop filter parameters that control sharpness.*/
5838 		int block_inside_limit = lvl >> ((sharpness_lvl > 0) +
5839 					(sharpness_lvl > 4));
5840 
5841 		if (sharpness_lvl > 0) {
5842 			if (block_inside_limit > (9 - sharpness_lvl))
5843 				block_inside_limit = (9 - sharpness_lvl);
5844 		}
5845 
5846 		if (block_inside_limit < 1)
5847 			block_inside_limit = 1;
5848 
5849 		lfi->lfthr[lvl].lim = (uint8_t)block_inside_limit;
5850 		lfi->lfthr[lvl].mblim = (uint8_t)(2 * (lvl + 2) +
5851 				block_inside_limit);
5852 	}
5853 }
5854 
5855 /*instantiate this function once when decode is started*/
vp9_loop_filter_init(struct VP9Decoder_s * pbi)5856 void vp9_loop_filter_init(struct VP9Decoder_s *pbi)
5857 {
5858 	struct loop_filter_info_n *lfi = pbi->lfi;
5859 	struct loopfilter *lf = pbi->lf;
5860 	struct segmentation *seg_4lf = pbi->seg_4lf;
5861 	int i;
5862 	unsigned int data32;
5863 
5864 	memset(lfi, 0, sizeof(struct loop_filter_info_n));
5865 	memset(lf, 0, sizeof(struct loopfilter));
5866 	memset(seg_4lf, 0, sizeof(struct segmentation));
5867 	lf->sharpness_level = 0; /*init to 0 */
5868 	/*init limits for given sharpness*/
5869 	vp9_update_sharpness(lfi, lf->sharpness_level);
5870 	lf->last_sharpness_level = lf->sharpness_level;
5871 	/*init hev threshold const vectors (actually no use)
5872 	 *for (i = 0; i <= MAX_LOOP_FILTER; i++)
5873 	 *	lfi->lfthr[i].hev_thr = (uint8_t)(i >> 4);
5874 	 */
5875 
5876 	/*Write to register*/
5877 	for (i = 0; i < 32; i++) {
5878 		unsigned int thr;
5879 
5880 		thr = ((lfi->lfthr[i * 2 + 1].lim & 0x3f)<<8) |
5881 			(lfi->lfthr[i * 2 + 1].mblim & 0xff);
5882 		thr = (thr<<16) | ((lfi->lfthr[i*2].lim & 0x3f)<<8) |
5883 			(lfi->lfthr[i * 2].mblim & 0xff);
5884 		WRITE_VREG(HEVC_DBLK_CFG9, thr);
5885 	}
5886 
5887 	/*video format is VP9*/
5888 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
5889 		data32 = (0x3 << 14) | // (dw fifo thres r and b)
5890 		(0x3 << 12) | // (dw fifo thres r or b)
5891 		(0x3 << 10) | // (dw fifo thres not r/b)
5892 		(0x3 << 8) | // 1st/2nd write both enable
5893 		(0x1 << 0); // vp9 video format
5894 		if (get_double_write_mode(pbi) == 0x10)
5895 			 data32 &= (~0x100);
5896 	} else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
5897 		data32 = (0x57 << 8) |  /*1st/2nd write both enable*/
5898 			(0x1  << 0); /*vp9 video format*/
5899 		if (get_double_write_mode(pbi) == 0x10)
5900 			 data32 &= (~0x100);
5901 	} else
5902 		data32 = 0x40400001;
5903 
5904 	WRITE_VREG(HEVC_DBLK_CFGB, data32);
5905 	if (debug & VP9_DEBUG_BUFMGR_MORE)
5906 		pr_info("[DBLK DEBUG] CFGB : 0x%x\n", data32);
5907 }
5908 	/* perform this function per frame*/
vp9_loop_filter_frame_init(struct segmentation * seg,struct loop_filter_info_n * lfi,struct loopfilter * lf,int default_filt_lvl)5909 void vp9_loop_filter_frame_init(struct segmentation *seg,
5910 		struct loop_filter_info_n *lfi, struct loopfilter *lf,
5911 		int default_filt_lvl) {
5912 	int i;
5913 	int seg_id;
5914 	/*n_shift is the multiplier for lf_deltas
5915 	 *the multiplier is 1 for when filter_lvl is between 0 and 31;
5916 	 *2 when filter_lvl is between 32 and 63
5917 	 */
5918 	const int scale = 1 << (default_filt_lvl >> 5);
5919 
5920 	/*update limits if sharpness has changed*/
5921 	if (lf->last_sharpness_level != lf->sharpness_level) {
5922 		vp9_update_sharpness(lfi, lf->sharpness_level);
5923 		lf->last_sharpness_level = lf->sharpness_level;
5924 
5925 	/*Write to register*/
5926 	for (i = 0; i < 32; i++) {
5927 		unsigned int thr;
5928 
5929 		thr = ((lfi->lfthr[i * 2 + 1].lim & 0x3f) << 8)
5930 			| (lfi->lfthr[i * 2 + 1].mblim & 0xff);
5931 		thr = (thr << 16) | ((lfi->lfthr[i * 2].lim & 0x3f) << 8)
5932 			| (lfi->lfthr[i * 2].mblim & 0xff);
5933 		WRITE_VREG(HEVC_DBLK_CFG9, thr);
5934 		}
5935 	}
5936 
5937 	for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {/*MAX_SEGMENTS = 8*/
5938 		int lvl_seg = default_filt_lvl;
5939 
5940 		if (segfeature_active(seg, seg_id, SEG_LVL_ALT_LF)) {
5941 			const int data = get_segdata(seg, seg_id,
5942 						SEG_LVL_ALT_LF);
5943 			lvl_seg = vp9_clamp(seg->abs_delta == SEGMENT_ABSDATA ?
5944 				data : default_filt_lvl + data,
5945 				0, MAX_LOOP_FILTER);
5946 #ifdef DBG_LF_PRINT
5947 	pr_info("segfeature_active!!!seg_id=%d,lvl_seg=%d\n", seg_id, lvl_seg);
5948 #endif
5949 	}
5950 
5951 	if (!lf->mode_ref_delta_enabled) {
5952 		/*we could get rid of this if we assume that deltas are set to
5953 		 *zero when not in use; encoder always uses deltas
5954 		 */
5955 		memset(lfi->lvl[seg_id], lvl_seg, sizeof(lfi->lvl[seg_id]));
5956 	} else {
5957 		int ref, mode;
5958 		const int intra_lvl = lvl_seg +	lf->ref_deltas[INTRA_FRAME]
5959 					* scale;
5960 #ifdef DBG_LF_PRINT
5961 	pr_info("LF_PRINT:vp9_loop_filter_frame_init,seg_id=%d\n", seg_id);
5962 	pr_info("ref_deltas[INTRA_FRAME]=%d\n", lf->ref_deltas[INTRA_FRAME]);
5963 #endif
5964 		lfi->lvl[seg_id][INTRA_FRAME][0] =
5965 				vp9_clamp(intra_lvl, 0, MAX_LOOP_FILTER);
5966 
5967 		for (ref = LAST_FRAME; ref < MAX_REF_FRAMES; ++ref) {
5968 			/* LAST_FRAME = 1, MAX_REF_FRAMES = 4*/
5969 			for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {
5970 				/*MAX_MODE_LF_DELTAS = 2*/
5971 				const int inter_lvl =
5972 					lvl_seg + lf->ref_deltas[ref] * scale
5973 					+ lf->mode_deltas[mode] * scale;
5974 #ifdef DBG_LF_PRINT
5975 #endif
5976 				lfi->lvl[seg_id][ref][mode] =
5977 					vp9_clamp(inter_lvl, 0,
5978 					MAX_LOOP_FILTER);
5979 				}
5980 			}
5981 		}
5982 	}
5983 
5984 #ifdef DBG_LF_PRINT
5985 	/*print out thr/lvl table per frame*/
5986 	for (i = 0; i <= MAX_LOOP_FILTER; i++) {
5987 		pr_info("LF_PRINT:(%d)thr=%d,blim=%d,lim=%d\n",
5988 			i, lfi->lfthr[i].hev_thr, lfi->lfthr[i].mblim,
5989 			lfi->lfthr[i].lim);
5990 	}
5991 	for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) {
5992 		pr_info("LF_PRINT:lvl(seg_id=%d)(mode=0,%d,%d,%d,%d)\n",
5993 			seg_id, lfi->lvl[seg_id][0][0],
5994 			lfi->lvl[seg_id][1][0], lfi->lvl[seg_id][2][0],
5995 			lfi->lvl[seg_id][3][0]);
5996 		pr_info("i(mode=1,%d,%d,%d,%d)\n", lfi->lvl[seg_id][0][1],
5997 			lfi->lvl[seg_id][1][1], lfi->lvl[seg_id][2][1],
5998 			lfi->lvl[seg_id][3][1]);
5999 	}
6000 #endif
6001 
6002 	/*Write to register */
6003 	for (i = 0; i < 16; i++) {
6004 		unsigned int level;
6005 
6006 		level = ((lfi->lvl[i >> 1][3][i & 1] & 0x3f) << 24) |
6007 			((lfi->lvl[i >> 1][2][i & 1] & 0x3f) << 16) |
6008 			((lfi->lvl[i >> 1][1][i & 1] & 0x3f) << 8) |
6009 			(lfi->lvl[i >> 1][0][i & 1] & 0x3f);
6010 		if (!default_filt_lvl)
6011 			level = 0;
6012 		WRITE_VREG(HEVC_DBLK_CFGA, level);
6013 	}
6014 }
6015 /* VP9_LPF_LVL_UPDATE */
6016 #endif
6017 
vp9_init_decoder_hw(struct VP9Decoder_s * pbi,u32 mask)6018 static void vp9_init_decoder_hw(struct VP9Decoder_s *pbi, u32 mask)
6019 {
6020 	unsigned int data32;
6021 	int i;
6022 	const unsigned short parser_cmd[PARSER_CMD_NUMBER] = {
6023 		0x0401, 0x8401, 0x0800, 0x0402, 0x9002, 0x1423,
6024 		0x8CC3, 0x1423, 0x8804, 0x9825, 0x0800, 0x04FE,
6025 		0x8406, 0x8411, 0x1800, 0x8408, 0x8409, 0x8C2A,
6026 		0x9C2B, 0x1C00, 0x840F, 0x8407, 0x8000, 0x8408,
6027 		0x2000, 0xA800, 0x8410, 0x04DE, 0x840C, 0x840D,
6028 		0xAC00, 0xA000, 0x08C0, 0x08E0, 0xA40E, 0xFC00,
6029 		0x7C00
6030 	};
6031 #if 0
6032 	if (get_cpu_major_id() >= MESON_CPU_MAJOR_ID_G12A) {
6033 		/* Set MCR fetch priorities*/
6034 		data32 = 0x1 | (0x1 << 2) | (0x1 <<3) |
6035 			(24 << 4) | (32 << 11) | (24 << 18) | (32 << 25);
6036 		WRITE_VREG(HEVCD_MPP_DECOMP_AXIURG_CTL, data32);
6037 	}
6038 #endif
6039 	/*if (debug & VP9_DEBUG_BUFMGR_MORE)
6040 		pr_info("%s\n", __func__);*/
6041 	if (mask & HW_MASK_FRONT) {
6042 		data32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
6043 #if 1
6044 		/* set bit 31~29 to 3 if HEVC_STREAM_FIFO_CTL[29] is 1 */
6045 		data32 &= ~(7 << 29);
6046 		data32 |= (3 << 29);
6047 #endif
6048 		data32 = data32 |
6049 		(1 << 24) |/*stream_buffer_empty_int_amrisc_enable*/
6050 		(1 << 22) |/*stream_fifo_empty_int_amrisc_enable*/
6051 		(1 << 7) |/*dec_done_int_cpu_enable*/
6052 		(1 << 4) |/*startcode_found_int_cpu_enable*/
6053 		(0 << 3) |/*startcode_found_int_amrisc_enable*/
6054 		(1 << 0)    /*parser_int_enable*/
6055 		;
6056 #ifdef SUPPORT_FB_DECODING
6057 #ifndef FB_DECODING_TEST_SCHEDULE
6058 		/*fed_fb_slice_done_int_cpu_enable*/
6059 		if (pbi->used_stage_buf_num > 0)
6060 			data32 |= (1 << 10);
6061 #endif
6062 #endif
6063 		WRITE_VREG(HEVC_PARSER_INT_CONTROL, data32);
6064 
6065 	data32 = READ_VREG(HEVC_SHIFT_STATUS);
6066 	data32 = data32 |
6067 	(0 << 1) |/*emulation_check_off VP9
6068 		do not have emulation*/
6069 	(1 << 0)/*startcode_check_on*/
6070 	;
6071 	WRITE_VREG(HEVC_SHIFT_STATUS, data32);
6072 	WRITE_VREG(HEVC_SHIFT_CONTROL,
6073 	(0 << 14) | /*disable_start_code_protect*/
6074 	(1 << 10) | /*length_zero_startcode_en for VP9*/
6075 	(1 << 9) | /*length_valid_startcode_en for VP9*/
6076 	(3 << 6) | /*sft_valid_wr_position*/
6077 	(2 << 4) | /*emulate_code_length_sub_1*/
6078 	(3 << 1) | /*start_code_length_sub_1
6079 	VP9 use 0x00000001 as startcode (4 Bytes)*/
6080 	(1 << 0)   /*stream_shift_enable*/
6081 	);
6082 
6083 	WRITE_VREG(HEVC_CABAC_CONTROL,
6084 		(1 << 0)/*cabac_enable*/
6085 	);
6086 
6087 	WRITE_VREG(HEVC_PARSER_CORE_CONTROL,
6088 		(1 << 0)/* hevc_parser_core_clk_en*/
6089 	);
6090 
6091 
6092 	WRITE_VREG(HEVC_DEC_STATUS_REG, 0);
6093 
6094 	}
6095 
6096 	if (mask & HW_MASK_BACK) {
6097 		/*Initial IQIT_SCALELUT memory
6098 		-- just to avoid X in simulation*/
6099 
6100 		WRITE_VREG(HEVC_IQIT_SCALELUT_WR_ADDR, 0);/*cfg_p_addr*/
6101 		for (i = 0; i < 1024; i++)
6102 			WRITE_VREG(HEVC_IQIT_SCALELUT_DATA, 0);
6103 	}
6104 
6105 	if (mask & HW_MASK_FRONT) {
6106 		u32 decode_mode;
6107 #ifdef ENABLE_SWAP_TEST
6108 	WRITE_VREG(HEVC_STREAM_SWAP_TEST, 100);
6109 #else
6110 	WRITE_VREG(HEVC_STREAM_SWAP_TEST, 0);
6111 #endif
6112 #ifdef MULTI_INSTANCE_SUPPORT
6113 		if (!pbi->m_ins_flag) {
6114 			if (pbi->low_latency_flag)
6115 				decode_mode = DECODE_MODE_SINGLE_LOW_LATENCY;
6116 			else
6117 				decode_mode = DECODE_MODE_SINGLE;
6118 		} else if (vdec_frame_based(hw_to_vdec(pbi)))
6119 			decode_mode = pbi->no_head ?
6120 				DECODE_MODE_MULTI_FRAMEBASE_NOHEAD :
6121 				DECODE_MODE_MULTI_FRAMEBASE;
6122 		else
6123 			decode_mode = DECODE_MODE_MULTI_STREAMBASE;
6124 #ifdef SUPPORT_FB_DECODING
6125 #ifndef FB_DECODING_TEST_SCHEDULE
6126 		if (pbi->used_stage_buf_num > 0)
6127 			decode_mode |= (0x01 << 24);
6128 #endif
6129 #endif
6130 		WRITE_VREG(DECODE_MODE, decode_mode);
6131 		WRITE_VREG(HEVC_DECODE_SIZE, 0);
6132 		WRITE_VREG(HEVC_DECODE_COUNT, 0);
6133 #else
6134 	WRITE_VREG(DECODE_MODE, DECODE_MODE_SINGLE);
6135 	WRITE_VREG(HEVC_DECODE_PIC_BEGIN_REG, 0);
6136 	WRITE_VREG(HEVC_DECODE_PIC_NUM_REG, 0x7fffffff); /*to remove*/
6137 #endif
6138 	/*Send parser_cmd*/
6139 	WRITE_VREG(HEVC_PARSER_CMD_WRITE, (1 << 16) | (0 << 0));
6140 	for (i = 0; i < PARSER_CMD_NUMBER; i++)
6141 		WRITE_VREG(HEVC_PARSER_CMD_WRITE, parser_cmd[i]);
6142 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_0, PARSER_CMD_SKIP_CFG_0);
6143 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_1, PARSER_CMD_SKIP_CFG_1);
6144 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_2, PARSER_CMD_SKIP_CFG_2);
6145 
6146 
6147 		WRITE_VREG(HEVC_PARSER_IF_CONTROL,
6148 			/*  (1 << 8) |*/ /*sao_sw_pred_enable*/
6149 			(1 << 5) | /*parser_sao_if_en*/
6150 			(1 << 2) | /*parser_mpred_if_en*/
6151 			(1 << 0) /*parser_scaler_if_en*/
6152 		);
6153 	}
6154 
6155 	if (mask & HW_MASK_BACK) {
6156 		/*Changed to Start MPRED in microcode*/
6157 		/*
6158 		pr_info("[test.c] Start MPRED\n");
6159 		WRITE_VREG(HEVC_MPRED_INT_STATUS,
6160 		(1<<31)
6161 		);
6162 		*/
6163 		WRITE_VREG(HEVCD_IPP_TOP_CNTL,
6164 			(0 << 1) | /*enable ipp*/
6165 			(1 << 0)   /*software reset ipp and mpp*/
6166 		);
6167 		WRITE_VREG(HEVCD_IPP_TOP_CNTL,
6168 			(1 << 1) | /*enable ipp*/
6169 			(0 << 0)   /*software reset ipp and mpp*/
6170 		);
6171 	if (get_double_write_mode(pbi) & 0x10) {
6172 		/*Enable NV21 reference read mode for MC*/
6173 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
6174 	}
6175 
6176 		/*Initialize mcrcc and decomp perf counters*/
6177 		if (mcrcc_cache_alg_flag &&
6178 			pbi->init_flag == 0) {
6179 			mcrcc_perfcount_reset();
6180 			decomp_perfcount_reset();
6181 		}
6182 	}
6183 	return;
6184 }
6185 
6186 
6187 #ifdef CONFIG_HEVC_CLK_FORCED_ON
config_vp9_clk_forced_on(void)6188 static void config_vp9_clk_forced_on(void)
6189 {
6190 	unsigned int rdata32;
6191 	/*IQIT*/
6192 	rdata32 = READ_VREG(HEVC_IQIT_CLK_RST_CTRL);
6193 	WRITE_VREG(HEVC_IQIT_CLK_RST_CTRL, rdata32 | (0x1 << 2));
6194 
6195 	/* DBLK*/
6196 	rdata32 = READ_VREG(HEVC_DBLK_CFG0);
6197 	WRITE_VREG(HEVC_DBLK_CFG0, rdata32 | (0x1 << 2));
6198 
6199 	/* SAO*/
6200 	rdata32 = READ_VREG(HEVC_SAO_CTRL1);
6201 	WRITE_VREG(HEVC_SAO_CTRL1, rdata32 | (0x1 << 2));
6202 
6203 	/*MPRED*/
6204 	rdata32 = READ_VREG(HEVC_MPRED_CTRL1);
6205 	WRITE_VREG(HEVC_MPRED_CTRL1, rdata32 | (0x1 << 24));
6206 
6207 	/* PARSER*/
6208 	rdata32 = READ_VREG(HEVC_STREAM_CONTROL);
6209 	WRITE_VREG(HEVC_STREAM_CONTROL, rdata32 | (0x1 << 15));
6210 	rdata32 = READ_VREG(HEVC_SHIFT_CONTROL);
6211 	WRITE_VREG(HEVC_SHIFT_CONTROL, rdata32 | (0x1 << 15));
6212 	rdata32 = READ_VREG(HEVC_CABAC_CONTROL);
6213 	WRITE_VREG(HEVC_CABAC_CONTROL, rdata32 | (0x1 << 13));
6214 	rdata32 = READ_VREG(HEVC_PARSER_CORE_CONTROL);
6215 	WRITE_VREG(HEVC_PARSER_CORE_CONTROL, rdata32 | (0x1 << 15));
6216 	rdata32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
6217 	WRITE_VREG(HEVC_PARSER_INT_CONTROL, rdata32 | (0x1 << 15));
6218 	rdata32 = READ_VREG(HEVC_PARSER_IF_CONTROL);
6219 	WRITE_VREG(HEVC_PARSER_IF_CONTROL,
6220 			rdata32 | (0x1 << 6) | (0x1 << 3) | (0x1 << 1));
6221 
6222 	/*IPP*/
6223 	rdata32 = READ_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG);
6224 	WRITE_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG, rdata32 | 0xffffffff);
6225 
6226 	/* MCRCC*/
6227 	rdata32 = READ_VREG(HEVCD_MCRCC_CTL1);
6228 	WRITE_VREG(HEVCD_MCRCC_CTL1, rdata32 | (0x1 << 3));
6229 }
6230 #endif
6231 
6232 
6233 #ifdef MCRCC_ENABLE
dump_hit_rate(struct VP9Decoder_s * pbi)6234 static void dump_hit_rate(struct VP9Decoder_s *pbi)
6235 {
6236 	if (debug & VP9_DEBUG_CACHE_HIT_RATE) {
6237 		mcrcc_get_hitrate(pbi->m_ins_flag);
6238 		decomp_get_hitrate();
6239 		decomp_get_comprate();
6240 	}
6241 }
6242 
config_mcrcc_axi_hw(struct VP9Decoder_s * pbi)6243 static void  config_mcrcc_axi_hw(struct VP9Decoder_s *pbi)
6244 {
6245 	unsigned int rdata32;
6246 	unsigned short is_inter;
6247 	/*pr_info("Entered config_mcrcc_axi_hw...\n");*/
6248 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0x2);/* reset mcrcc*/
6249 	is_inter = ((pbi->common.frame_type != KEY_FRAME) &&
6250 			(!pbi->common.intra_only)) ? 1 : 0;
6251 	if (!is_inter) { /* I-PIC*/
6252 		/*remove reset -- disables clock*/
6253 		WRITE_VREG(HEVCD_MCRCC_CTL1, 0x0);
6254 		return;
6255 	}
6256 
6257 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
6258 		mcrcc_get_hitrate(pbi->m_ins_flag);
6259 		decomp_get_hitrate();
6260 		decomp_get_comprate();
6261 	}
6262 
6263 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
6264 			(0 << 8) | (1 << 1) | 0);
6265 	rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6266 	rdata32 = rdata32 & 0xffff;
6267 	rdata32 = rdata32 | (rdata32 << 16);
6268 	WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
6269 	/*Programme canvas1 */
6270 	rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6271 	rdata32 = rdata32 & 0xffff;
6272 	rdata32 = rdata32 | (rdata32 << 16);
6273 	WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32);
6274 	/*enable mcrcc progressive-mode*/
6275 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0xff0);
6276 }
6277 
config_mcrcc_axi_hw_new(struct VP9Decoder_s * pbi)6278 static void  config_mcrcc_axi_hw_new(struct VP9Decoder_s *pbi)
6279 {
6280 	u32 curr_picnum = -1;
6281 	u32 lastref_picnum = -1;
6282 	u32 goldenref_picnum = -1;
6283 	u32 altref_picnum = -1;
6284 
6285 	u32 lastref_delta_picnum;
6286 	u32 goldenref_delta_picnum;
6287 	u32 altref_delta_picnum;
6288 
6289 	u32 rdata32;
6290 
6291 	u32 lastcanvas;
6292 	u32 goldencanvas;
6293 	u32 altrefcanvas;
6294 
6295 	u16 is_inter;
6296 	u16 lastref_inref;
6297 	u16 goldenref_inref;
6298 	u16 altref_inref;
6299 
6300 	u32 refcanvas_array[3], utmp;
6301 	int deltapicnum_array[3], tmp;
6302 
6303 	struct VP9_Common_s *cm = &pbi->common;
6304 	struct PIC_BUFFER_CONFIG_s *cur_pic_config
6305 		= &cm->cur_frame->buf;
6306 	curr_picnum = cur_pic_config->decode_idx;
6307 	if (cm->frame_refs[0].buf)
6308 		lastref_picnum = cm->frame_refs[0].buf->decode_idx;
6309 	if (cm->frame_refs[1].buf)
6310 		goldenref_picnum = cm->frame_refs[1].buf->decode_idx;
6311 	if (cm->frame_refs[2].buf)
6312 		altref_picnum = cm->frame_refs[2].buf->decode_idx;
6313 
6314 	lastref_delta_picnum = (lastref_picnum >= curr_picnum) ?
6315 		(lastref_picnum - curr_picnum) : (curr_picnum - lastref_picnum);
6316 	goldenref_delta_picnum = (goldenref_picnum >= curr_picnum) ?
6317 		(goldenref_picnum - curr_picnum) :
6318 		(curr_picnum - goldenref_picnum);
6319 	altref_delta_picnum =
6320 		(altref_picnum >= curr_picnum) ?
6321 		(altref_picnum - curr_picnum) : (curr_picnum - altref_picnum);
6322 
6323 	lastref_inref = (cm->frame_refs[0].idx != INVALID_IDX) ? 1 : 0;
6324 	goldenref_inref = (cm->frame_refs[1].idx != INVALID_IDX) ? 1 : 0;
6325 	altref_inref = (cm->frame_refs[2].idx != INVALID_IDX) ? 1 : 0;
6326 
6327 	if (debug & VP9_DEBUG_CACHE)
6328 		pr_info("%s--0--lastref_inref:%d goldenref_inref:%d altref_inref:%d\n",
6329 			__func__, lastref_inref, goldenref_inref, altref_inref);
6330 
6331 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0x2); /* reset mcrcc */
6332 
6333 	is_inter = ((pbi->common.frame_type != KEY_FRAME)
6334 		&& (!pbi->common.intra_only)) ? 1 : 0;
6335 
6336 	if (!is_inter) { /* I-PIC */
6337 		/* remove reset -- disables clock */
6338 		WRITE_VREG(HEVCD_MCRCC_CTL1, 0x0);
6339 		return;
6340 	}
6341 
6342 	if (!pbi->m_ins_flag)
6343 		dump_hit_rate(pbi);
6344 
6345 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR, (0 << 8) | (1<<1) | 0);
6346 	lastcanvas = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6347 	goldencanvas = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6348 	altrefcanvas = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6349 
6350 	if (debug & VP9_DEBUG_CACHE)
6351 		pr_info("[test.c] lastref_canv:%x goldenref_canv:%x altref_canv:%x\n",
6352 			lastcanvas, goldencanvas, altrefcanvas);
6353 
6354 	altref_inref = ((altref_inref == 1) &&
6355 			(altrefcanvas != (goldenref_inref
6356 			? goldencanvas : 0xffffffff)) &&
6357 			(altrefcanvas != (lastref_inref ?
6358 			lastcanvas : 0xffffffff))) ? 1 : 0;
6359 	goldenref_inref = ((goldenref_inref == 1) &&
6360 		(goldencanvas != (lastref_inref ?
6361 		lastcanvas : 0xffffffff))) ? 1 : 0;
6362 	if (debug & VP9_DEBUG_CACHE)
6363 		pr_info("[test.c]--1--lastref_inref:%d goldenref_inref:%d altref_inref:%d\n",
6364 			lastref_inref, goldenref_inref, altref_inref);
6365 
6366 	altref_delta_picnum = altref_inref ? altref_delta_picnum : 0x7fffffff;
6367 	goldenref_delta_picnum = goldenref_inref ?
6368 		goldenref_delta_picnum : 0x7fffffff;
6369 	lastref_delta_picnum = lastref_inref ?
6370 		lastref_delta_picnum : 0x7fffffff;
6371 	if (debug & VP9_DEBUG_CACHE)
6372 		pr_info("[test.c]--1--lastref_delta_picnum:%d goldenref_delta_picnum:%d altref_delta_picnum:%d\n",
6373 			lastref_delta_picnum, goldenref_delta_picnum,
6374 			altref_delta_picnum);
6375 	/*ARRAY SORT HERE DELTA/CANVAS ARRAY SORT -- use DELTA*/
6376 
6377 	refcanvas_array[0] = lastcanvas;
6378 	refcanvas_array[1] = goldencanvas;
6379 	refcanvas_array[2] = altrefcanvas;
6380 
6381 	deltapicnum_array[0] = lastref_delta_picnum;
6382 	deltapicnum_array[1] = goldenref_delta_picnum;
6383 	deltapicnum_array[2] = altref_delta_picnum;
6384 
6385 	/* sort0 : 2-to-1 */
6386 	if (deltapicnum_array[2] < deltapicnum_array[1]) {
6387 		utmp = refcanvas_array[2];
6388 		refcanvas_array[2] = refcanvas_array[1];
6389 		refcanvas_array[1] = utmp;
6390 		tmp = deltapicnum_array[2];
6391 		deltapicnum_array[2] = deltapicnum_array[1];
6392 		deltapicnum_array[1] = tmp;
6393 	}
6394 	/* sort1 : 1-to-0 */
6395 	if (deltapicnum_array[1] < deltapicnum_array[0]) {
6396 		utmp = refcanvas_array[1];
6397 		refcanvas_array[1] = refcanvas_array[0];
6398 		refcanvas_array[0] = utmp;
6399 		tmp = deltapicnum_array[1];
6400 		deltapicnum_array[1] = deltapicnum_array[0];
6401 		deltapicnum_array[0] = tmp;
6402 	}
6403 	/* sort2 : 2-to-1 */
6404 	if (deltapicnum_array[2] < deltapicnum_array[1]) {
6405 		utmp = refcanvas_array[2];  refcanvas_array[2] =
6406 			refcanvas_array[1]; refcanvas_array[1] =  utmp;
6407 		tmp  = deltapicnum_array[2]; deltapicnum_array[2] =
6408 			deltapicnum_array[1]; deltapicnum_array[1] = tmp;
6409 	}
6410 	if (mcrcc_cache_alg_flag ==
6411 		THODIYIL_MCRCC_CANVAS_ALGX) { /*09/15/2017*/
6412 		/* lowest delta_picnum */
6413 		rdata32 = refcanvas_array[0];
6414 		rdata32 = rdata32 & 0xffff;
6415 		rdata32 = rdata32 | (rdata32 << 16);
6416 		WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
6417 
6418 		/* 2nd-lowest delta_picnum */
6419 		rdata32 = refcanvas_array[1];
6420 		rdata32 = rdata32 & 0xffff;
6421 		rdata32 = rdata32 | (rdata32 << 16);
6422 		WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32);
6423 	} else {
6424 		/* previous version -- LAST/GOLDEN ALWAYS -- before 09/13/2017*/
6425 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
6426 			(0 << 8) | (1<<1) | 0);
6427 		rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6428 		rdata32 = rdata32 & 0xffff;
6429 		rdata32 = rdata32 | (rdata32 << 16);
6430 		WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
6431 
6432 		/* Programme canvas1 */
6433 		rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6434 		rdata32 = rdata32 & 0xffff;
6435 		rdata32 = rdata32 | (rdata32 << 16);
6436 		WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32);
6437 	}
6438 
6439 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0xff0); /* enable mcrcc progressive-mode */
6440 	return;
6441 }
6442 
6443 #endif
6444 
6445 
free_lf_buf(struct VP9Decoder_s * pbi)6446 static void free_lf_buf(struct VP9Decoder_s *pbi)
6447 {
6448 	if (pbi->lfi)
6449 		vfree(pbi->lfi);
6450 	if (pbi->lf)
6451 		vfree(pbi->lf);
6452 	if (pbi->seg_4lf)
6453 		vfree(pbi->seg_4lf);
6454 	pbi->lfi = NULL;
6455 	pbi->lf = NULL;
6456 	pbi->seg_4lf = NULL;
6457 }
6458 
alloc_lf_buf(struct VP9Decoder_s * pbi)6459 static int alloc_lf_buf(struct VP9Decoder_s *pbi)
6460 {
6461 	pbi->lfi = vmalloc(sizeof(struct loop_filter_info_n));
6462 	pbi->lf = vmalloc(sizeof(struct loopfilter));
6463 	pbi->seg_4lf = vmalloc(sizeof(struct segmentation));
6464 	if (pbi->lfi == NULL || pbi->lf == NULL || pbi->seg_4lf == NULL) {
6465 		free_lf_buf(pbi);
6466 		pr_err("[test.c] vp9_loop_filter init malloc error!!!\n");
6467 		return -1;
6468 	}
6469 	return 0;
6470 }
6471 
vp9_local_uninit(struct VP9Decoder_s * pbi)6472 static void vp9_local_uninit(struct VP9Decoder_s *pbi)
6473 {
6474 	pbi->rpm_ptr = NULL;
6475 	pbi->lmem_ptr = NULL;
6476 	if (pbi->rpm_addr) {
6477 		dma_free_coherent(amports_get_dma_device(),
6478 					RPM_BUF_SIZE,
6479 					pbi->rpm_addr,
6480 					pbi->rpm_phy_addr);
6481 		pbi->rpm_addr = NULL;
6482 	}
6483 	if (pbi->lmem_addr) {
6484 		if (pbi->lmem_phy_addr)
6485 			dma_free_coherent(amports_get_dma_device(),
6486 				LMEM_BUF_SIZE, pbi->lmem_addr,
6487 				pbi->lmem_phy_addr);
6488 		pbi->lmem_addr = NULL;
6489 	}
6490 	if (pbi->prob_buffer_addr) {
6491 		if (pbi->prob_buffer_phy_addr)
6492 			dma_free_coherent(amports_get_dma_device(),
6493 				PROB_BUF_SIZE, pbi->prob_buffer_addr,
6494 				pbi->prob_buffer_phy_addr);
6495 
6496 		pbi->prob_buffer_addr = NULL;
6497 	}
6498 	if (pbi->count_buffer_addr) {
6499 		if (pbi->count_buffer_phy_addr)
6500 			dma_free_coherent(amports_get_dma_device(),
6501 				COUNT_BUF_SIZE, pbi->count_buffer_addr,
6502 				pbi->count_buffer_phy_addr);
6503 
6504 		pbi->count_buffer_addr = NULL;
6505 	}
6506 	if (pbi->mmu_enable) {
6507 		u32 mmu_map_size = vvp9_frame_mmu_map_size(pbi);
6508 		if (pbi->frame_mmu_map_addr) {
6509 			if (pbi->frame_mmu_map_phy_addr)
6510 				dma_free_coherent(amports_get_dma_device(),
6511 					mmu_map_size,
6512 					pbi->frame_mmu_map_addr,
6513 					pbi->frame_mmu_map_phy_addr);
6514 			pbi->frame_mmu_map_addr = NULL;
6515 		}
6516 	}
6517 #ifdef SUPPORT_FB_DECODING
6518 	if (pbi->stage_mmu_map_addr) {
6519 		if (pbi->stage_mmu_map_phy_addr)
6520 			dma_free_coherent(amports_get_dma_device(),
6521 				STAGE_MMU_MAP_SIZE * STAGE_MAX_BUFFERS,
6522 				pbi->stage_mmu_map_addr,
6523 					pbi->stage_mmu_map_phy_addr);
6524 		pbi->stage_mmu_map_addr = NULL;
6525 	}
6526 
6527 	uninit_stage_buf(pbi);
6528 #endif
6529 
6530 #ifdef VP9_LPF_LVL_UPDATE
6531 	free_lf_buf(pbi);
6532 #endif
6533 	if (pbi->gvs)
6534 		vfree(pbi->gvs);
6535 	pbi->gvs = NULL;
6536 }
6537 
vp9_local_init(struct VP9Decoder_s * pbi)6538 static int vp9_local_init(struct VP9Decoder_s *pbi)
6539 {
6540 	int ret = -1;
6541 	/*int losless_comp_header_size, losless_comp_body_size;*/
6542 
6543 	struct BuffInfo_s *cur_buf_info = NULL;
6544 
6545 	memset(&pbi->param, 0, sizeof(union param_u));
6546 	memset(&pbi->common, 0, sizeof(struct VP9_Common_s));
6547 #ifdef MULTI_INSTANCE_SUPPORT
6548 	cur_buf_info = &pbi->work_space_buf_store;
6549 
6550 	if (vdec_is_support_4k()) {
6551 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
6552 			memcpy(cur_buf_info, &amvvp9_workbuff_spec[2],	/* 8k */
6553 			sizeof(struct BuffInfo_s));
6554 		} else
6555 			memcpy(cur_buf_info, &amvvp9_workbuff_spec[1],	/* 4k */
6556 			sizeof(struct BuffInfo_s));
6557 	} else
6558 		memcpy(cur_buf_info, &amvvp9_workbuff_spec[0],/* 1080p */
6559 		sizeof(struct BuffInfo_s));
6560 
6561 	cur_buf_info->start_adr = pbi->buf_start;
6562 	if (!pbi->mmu_enable)
6563 		pbi->mc_buf_spec.buf_end = pbi->buf_start + pbi->buf_size;
6564 
6565 #else
6566 /*! MULTI_INSTANCE_SUPPORT*/
6567 	if (vdec_is_support_4k()) {
6568 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
6569 			cur_buf_info = &amvvp9_workbuff_spec[2];/* 8k work space */
6570 		else
6571 			cur_buf_info = &amvvp9_workbuff_spec[1];/* 4k2k work space */
6572 	} else
6573 		cur_buf_info = &amvvp9_workbuff_spec[0];/* 1080p work space */
6574 
6575 #endif
6576 
6577 	init_buff_spec(pbi, cur_buf_info);
6578 	vp9_bufmgr_init(pbi, cur_buf_info, NULL);
6579 
6580 	if (!vdec_is_support_4k()
6581 		&& (buf_alloc_width > 1920 &&  buf_alloc_height > 1088)) {
6582 		buf_alloc_width = 1920;
6583 		buf_alloc_height = 1088;
6584 		if (pbi->max_pic_w > 1920 && pbi->max_pic_h > 1088) {
6585 			pbi->max_pic_w = 1920;
6586 			pbi->max_pic_h = 1088;
6587 		}
6588 	} else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
6589 		buf_alloc_width = 8192;
6590 		buf_alloc_height = 4608;
6591 	}
6592 	pbi->init_pic_w = pbi->max_pic_w ? pbi->max_pic_w :
6593 		(buf_alloc_width ? buf_alloc_width :
6594 		(pbi->vvp9_amstream_dec_info.width ?
6595 		pbi->vvp9_amstream_dec_info.width :
6596 		pbi->work_space_buf->max_width));
6597 	pbi->init_pic_h = pbi->max_pic_h ? pbi->max_pic_h :
6598 		(buf_alloc_height ? buf_alloc_height :
6599 		(pbi->vvp9_amstream_dec_info.height ?
6600 		pbi->vvp9_amstream_dec_info.height :
6601 		pbi->work_space_buf->max_height));
6602 
6603 	/* video is not support unaligned with 64 in tl1
6604 	** vdec canvas mode will be linear when dump yuv is set
6605 	*/
6606 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) &&
6607 		(pbi->double_write_mode != 0) &&
6608 		(((pbi->max_pic_w % 64) != 0) ||
6609 		(pbi->vvp9_amstream_dec_info.width % 64) != 0)) {
6610 		if (hw_to_vdec(pbi)->canvas_mode !=
6611 			CANVAS_BLKMODE_LINEAR)
6612 			pbi->mem_map_mode = 2;
6613 		else {
6614 			pbi->mem_map_mode = 0;
6615 			pr_info("vdec blkmod linear, force mem_map_mode 0\n");
6616 		}
6617 	}
6618 
6619 #ifndef MV_USE_FIXED_BUF
6620 	if (init_mv_buf_list(pbi) < 0) {
6621 		pr_err("%s: init_mv_buf_list fail\n", __func__);
6622 		return -1;
6623 	}
6624 #endif
6625 	if (pbi->save_buffer_mode)
6626 		pbi->used_buf_num = MAX_BUF_NUM_SAVE_BUF;
6627 	else {
6628 		if (pbi->is_used_v4l)
6629 			pbi->used_buf_num = 5 + pbi->dynamic_buf_num_margin;
6630 		else
6631 			pbi->used_buf_num = max_buf_num;
6632 	}
6633 
6634 	if (pbi->used_buf_num > MAX_BUF_NUM)
6635 		pbi->used_buf_num = MAX_BUF_NUM;
6636 	if (pbi->used_buf_num > FRAME_BUFFERS)
6637 		pbi->used_buf_num = FRAME_BUFFERS;
6638 
6639 	pbi->pts_unstable = ((unsigned long)(pbi->vvp9_amstream_dec_info.param)
6640 			& 0x40) >> 6;
6641 
6642 	if ((debug & VP9_DEBUG_SEND_PARAM_WITH_REG) == 0) {
6643 		pbi->rpm_addr = dma_alloc_coherent(amports_get_dma_device(),
6644 				RPM_BUF_SIZE,
6645 				&pbi->rpm_phy_addr, GFP_KERNEL);
6646 		if (pbi->rpm_addr == NULL) {
6647 			pr_err("%s: failed to alloc rpm buffer\n", __func__);
6648 			return -1;
6649 		}
6650 
6651 		pbi->rpm_ptr = pbi->rpm_addr;
6652 	}
6653 
6654 	pbi->lmem_addr = dma_alloc_coherent(amports_get_dma_device(),
6655 			LMEM_BUF_SIZE,
6656 			&pbi->lmem_phy_addr, GFP_KERNEL);
6657 	if (pbi->lmem_addr == NULL) {
6658 		pr_err("%s: failed to alloc lmem buffer\n", __func__);
6659 		return -1;
6660 	}
6661 		pbi->lmem_ptr = pbi->lmem_addr;
6662 
6663 	pbi->prob_buffer_addr = dma_alloc_coherent(amports_get_dma_device(),
6664 				PROB_BUF_SIZE,
6665 				&pbi->prob_buffer_phy_addr, GFP_KERNEL);
6666 	if (pbi->prob_buffer_addr == NULL) {
6667 		pr_err("%s: failed to alloc prob_buffer\n", __func__);
6668 		return -1;
6669 	}
6670 	memset(pbi->prob_buffer_addr, 0, PROB_BUF_SIZE);
6671 	pbi->count_buffer_addr = dma_alloc_coherent(amports_get_dma_device(),
6672 				COUNT_BUF_SIZE,
6673 				&pbi->count_buffer_phy_addr, GFP_KERNEL);
6674 	if (pbi->count_buffer_addr == NULL) {
6675 		pr_err("%s: failed to alloc count_buffer\n", __func__);
6676 		return -1;
6677 	}
6678 	memset(pbi->count_buffer_addr, 0, COUNT_BUF_SIZE);
6679 
6680 	if (pbi->mmu_enable) {
6681 		u32 mmu_map_size = vvp9_frame_mmu_map_size(pbi);
6682 		pbi->frame_mmu_map_addr =
6683 			dma_alloc_coherent(amports_get_dma_device(),
6684 				mmu_map_size,
6685 				&pbi->frame_mmu_map_phy_addr, GFP_KERNEL);
6686 		if (pbi->frame_mmu_map_addr == NULL) {
6687 			pr_err("%s: failed to alloc count_buffer\n", __func__);
6688 			return -1;
6689 		}
6690 		memset(pbi->frame_mmu_map_addr, 0, COUNT_BUF_SIZE);
6691 	}
6692 #ifdef SUPPORT_FB_DECODING
6693 	if (pbi->m_ins_flag && stage_buf_num > 0) {
6694 		pbi->stage_mmu_map_addr =
6695 			dma_alloc_coherent(amports_get_dma_device(),
6696 				STAGE_MMU_MAP_SIZE * STAGE_MAX_BUFFERS,
6697 				&pbi->stage_mmu_map_phy_addr, GFP_KERNEL);
6698 		if (pbi->stage_mmu_map_addr == NULL) {
6699 			pr_err("%s: failed to alloc count_buffer\n", __func__);
6700 			return -1;
6701 		}
6702 		memset(pbi->stage_mmu_map_addr,
6703 			0, STAGE_MMU_MAP_SIZE * STAGE_MAX_BUFFERS);
6704 
6705 		init_stage_buf(pbi);
6706 	}
6707 #endif
6708 
6709 	ret = 0;
6710 	return ret;
6711 }
6712 
6713 /********************************************
6714  *  Mailbox command
6715  ********************************************/
6716 #define CMD_FINISHED               0
6717 #define CMD_ALLOC_VIEW             1
6718 #define CMD_FRAME_DISPLAY          3
6719 #define CMD_DEBUG                  10
6720 
6721 
6722 #define DECODE_BUFFER_NUM_MAX    32
6723 #define DISPLAY_BUFFER_NUM       6
6724 
6725 #define video_domain_addr(adr) (adr&0x7fffffff)
6726 #define DECODER_WORK_SPACE_SIZE 0x800000
6727 
6728 #define spec2canvas(x)  \
6729 	(((x)->uv_canvas_index << 16) | \
6730 	 ((x)->uv_canvas_index << 8)  | \
6731 	 ((x)->y_canvas_index << 0))
6732 
6733 
set_canvas(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * pic_config)6734 static void set_canvas(struct VP9Decoder_s *pbi,
6735 	struct PIC_BUFFER_CONFIG_s *pic_config)
6736 {
6737 	struct vdec_s *vdec = hw_to_vdec(pbi);
6738 	int canvas_w = ALIGN(pic_config->y_crop_width, 64)/4;
6739 	int canvas_h = ALIGN(pic_config->y_crop_height, 32)/4;
6740 	int blkmode = pbi->mem_map_mode;
6741 	/*CANVAS_BLKMODE_64X32*/
6742 	if	(pic_config->double_write_mode) {
6743 		canvas_w = pic_config->y_crop_width	/
6744 				get_double_write_ratio(pbi,
6745 					pic_config->double_write_mode);
6746 		canvas_h = pic_config->y_crop_height /
6747 				get_double_write_ratio(pbi,
6748 					pic_config->double_write_mode);
6749 
6750 		if (pbi->mem_map_mode == 0)
6751 			canvas_w = ALIGN(canvas_w, 32);
6752 		else
6753 			canvas_w = ALIGN(canvas_w, 64);
6754 		canvas_h = ALIGN(canvas_h, 32);
6755 
6756 		if (vdec->parallel_dec == 1) {
6757 			if (pic_config->y_canvas_index == -1)
6758 				pic_config->y_canvas_index =
6759 					vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
6760 			if (pic_config->uv_canvas_index == -1)
6761 				pic_config->uv_canvas_index =
6762 					vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
6763 		} else {
6764 			pic_config->y_canvas_index = 128 + pic_config->index * 2;
6765 			pic_config->uv_canvas_index = 128 + pic_config->index * 2 + 1;
6766 		}
6767 
6768 		canvas_config_ex(pic_config->y_canvas_index,
6769 			pic_config->dw_y_adr, canvas_w, canvas_h,
6770 			CANVAS_ADDR_NOWRAP, blkmode, pbi->is_used_v4l ? 0 : 7);
6771 		canvas_config_ex(pic_config->uv_canvas_index,
6772 			pic_config->dw_u_v_adr,	canvas_w, canvas_h,
6773 			CANVAS_ADDR_NOWRAP, blkmode, pbi->is_used_v4l ? 0 : 7);
6774 
6775 #ifdef MULTI_INSTANCE_SUPPORT
6776 		pic_config->canvas_config[0].phy_addr =
6777 				pic_config->dw_y_adr;
6778 		pic_config->canvas_config[0].width =
6779 				canvas_w;
6780 		pic_config->canvas_config[0].height =
6781 				canvas_h;
6782 		pic_config->canvas_config[0].block_mode =
6783 				blkmode;
6784 		pic_config->canvas_config[0].endian = pbi->is_used_v4l ? 0 : 7;
6785 
6786 		pic_config->canvas_config[1].phy_addr =
6787 				pic_config->dw_u_v_adr;
6788 		pic_config->canvas_config[1].width =
6789 				canvas_w;
6790 		pic_config->canvas_config[1].height =
6791 				canvas_h;
6792 		pic_config->canvas_config[1].block_mode =
6793 				blkmode;
6794 		pic_config->canvas_config[1].endian = pbi->is_used_v4l ? 0 : 7;
6795 #endif
6796 	}
6797 }
6798 
6799 
set_frame_info(struct VP9Decoder_s * pbi,struct vframe_s * vf)6800 static void set_frame_info(struct VP9Decoder_s *pbi, struct vframe_s *vf)
6801 {
6802 	unsigned int ar;
6803 	vf->duration = pbi->frame_dur;
6804 	vf->duration_pulldown = 0;
6805 	vf->flag = 0;
6806 	vf->prop.master_display_colour = pbi->vf_dp;
6807 	vf->signal_type = pbi->video_signal_type;
6808 	if (vf->compWidth && vf->compHeight)
6809 		pbi->frame_ar = vf->compHeight * 0x100 / vf->compWidth;
6810 	ar = min_t(u32, pbi->frame_ar, DISP_RATIO_ASPECT_RATIO_MAX);
6811 	vf->ratio_control = (ar << DISP_RATIO_ASPECT_RATIO_BIT);
6812 
6813 	if (pbi->is_used_v4l && pbi->vf_dp.present_flag) {
6814 		struct aml_vdec_hdr_infos hdr;
6815 		struct aml_vcodec_ctx *ctx =
6816 			(struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
6817 
6818 		memset(&hdr, 0, sizeof(hdr));
6819 		hdr.signal_type = vf->signal_type;
6820 		hdr.color_parms = pbi->vf_dp;
6821 		vdec_v4l_set_hdr_infos(ctx, &hdr);
6822 	}
6823 
6824 	vf->sidebind_type = pbi->sidebind_type;
6825 	vf->sidebind_channel_id = pbi->sidebind_channel_id;
6826 }
6827 
vvp9_vf_states(struct vframe_states * states,void * op_arg)6828 static int vvp9_vf_states(struct vframe_states *states, void *op_arg)
6829 {
6830 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)op_arg;
6831 
6832 	states->vf_pool_size = VF_POOL_SIZE;
6833 	states->buf_free_num = kfifo_len(&pbi->newframe_q);
6834 	states->buf_avail_num = kfifo_len(&pbi->display_q);
6835 
6836 	if (step == 2)
6837 		states->buf_avail_num = 0;
6838 	return 0;
6839 }
6840 
vvp9_vf_peek(void * op_arg)6841 static struct vframe_s *vvp9_vf_peek(void *op_arg)
6842 {
6843 	struct vframe_s *vf[2] = {0, 0};
6844 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)op_arg;
6845 
6846 	if (step == 2)
6847 		return NULL;
6848 
6849 	if (kfifo_out_peek(&pbi->display_q, (void *)&vf, 2)) {
6850 		if (vf[1]) {
6851 			vf[0]->next_vf_pts_valid = true;
6852 			vf[0]->next_vf_pts = vf[1]->pts;
6853 		} else
6854 			vf[0]->next_vf_pts_valid = false;
6855 		return vf[0];
6856 	}
6857 
6858 	return NULL;
6859 }
6860 
vvp9_vf_get(void * op_arg)6861 static struct vframe_s *vvp9_vf_get(void *op_arg)
6862 {
6863 	struct vframe_s *vf;
6864 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)op_arg;
6865 
6866 	if (step == 2)
6867 		return NULL;
6868 	else if (step == 1)
6869 			step = 2;
6870 
6871 	if (kfifo_get(&pbi->display_q, &vf)) {
6872 		struct vframe_s *next_vf;
6873 		uint8_t index = vf->index & 0xff;
6874 		if (index < pbi->used_buf_num ||
6875 			(vf->type & VIDTYPE_V4L_EOS)) {
6876 			vf->index_disp = pbi->vf_get_count;
6877 			pbi->vf_get_count++;
6878 			if (debug & VP9_DEBUG_BUFMGR)
6879 				pr_info("%s type 0x%x w/h %d/%d, pts %d, %lld\n",
6880 					__func__, vf->type,
6881 					vf->width, vf->height,
6882 					vf->pts,
6883 					vf->pts_us64);
6884 
6885 			if (kfifo_peek(&pbi->display_q, &next_vf)) {
6886 				vf->next_vf_pts_valid = true;
6887 				vf->next_vf_pts = next_vf->pts;
6888 			} else
6889 				vf->next_vf_pts_valid = false;
6890 
6891 			return vf;
6892 		}
6893 	}
6894 	return NULL;
6895 }
6896 
vvp9_vf_put(struct vframe_s * vf,void * op_arg)6897 static void vvp9_vf_put(struct vframe_s *vf, void *op_arg)
6898 {
6899 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)op_arg;
6900 	uint8_t index = vf->index & 0xff;
6901 
6902 	if (vf == (&pbi->vframe_dummy))
6903 		return;
6904 
6905 	kfifo_put(&pbi->newframe_q, (const struct vframe_s *)vf);
6906 	pbi->vf_put_count++;
6907 	if (index < pbi->used_buf_num) {
6908 		struct VP9_Common_s *cm = &pbi->common;
6909 		struct BufferPool_s *pool = cm->buffer_pool;
6910 		unsigned long flags;
6911 
6912 		lock_buffer_pool(pool, flags);
6913 		if (pool->frame_bufs[index].buf.vf_ref > 0)
6914 			pool->frame_bufs[index].buf.vf_ref--;
6915 
6916 		if (pbi->is_used_v4l)
6917 			pool->frame_bufs[index].buf.vframe_bound = true;
6918 
6919 		if (pbi->wait_buf)
6920 			WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG,
6921 						0x1);
6922 		pbi->last_put_idx = index;
6923 		pbi->new_frame_displayed++;
6924 		unlock_buffer_pool(pool, flags);
6925 #ifdef SUPPORT_FB_DECODING
6926 		if (pbi->used_stage_buf_num > 0 &&
6927 			pbi->back_not_run_ready)
6928 			trigger_schedule(pbi);
6929 #endif
6930 	}
6931 
6932 }
6933 
vvp9_event_cb(int type,void * data,void * private_data)6934 static int vvp9_event_cb(int type, void *data, void *private_data)
6935 {
6936 	if (type & VFRAME_EVENT_RECEIVER_RESET) {
6937 #if 0
6938 		unsigned long flags;
6939 
6940 		amhevc_stop();
6941 #ifndef CONFIG_AMLOGIC_POST_PROCESS_MANAGER
6942 		vf_light_unreg_provider(&vvp9_vf_prov);
6943 #endif
6944 		spin_lock_irqsave(&pbi->lock, flags);
6945 		vvp9_local_init();
6946 		vvp9_prot_init();
6947 		spin_unlock_irqrestore(&pbi->lock, flags);
6948 #ifndef CONFIG_AMLOGIC_POST_PROCESS_MANAGER
6949 		vf_reg_provider(&vvp9_vf_prov);
6950 #endif
6951 		amhevc_start();
6952 #endif
6953 	}
6954 
6955 	return 0;
6956 }
6957 
inc_vf_ref(struct VP9Decoder_s * pbi,int index)6958 void inc_vf_ref(struct VP9Decoder_s *pbi, int index)
6959 {
6960 	struct VP9_Common_s *cm = &pbi->common;
6961 
6962 	cm->buffer_pool->frame_bufs[index].buf.vf_ref++;
6963 
6964 	if (debug & VP9_DEBUG_BUFMGR_MORE)
6965 		pr_info("%s index = %d new vf_ref = %d\r\n",
6966 			__func__, index,
6967 			cm->buffer_pool->frame_bufs[index].buf.vf_ref);
6968 }
6969 
frame_duration_adapt(struct VP9Decoder_s * pbi,struct vframe_s * vf,u32 valid)6970 static int frame_duration_adapt(struct VP9Decoder_s *pbi, struct vframe_s *vf, u32 valid)
6971 {
6972 	u32 old_duration, pts_duration = 0;
6973 	u32 pts = vf->pts;
6974 
6975 	if (pbi->get_frame_dur == true)
6976 		return true;
6977 
6978 	pbi->frame_cnt_window++;
6979 	if (!(pbi->vp9_first_pts_ready == 1)) {
6980 		if (valid) {
6981 			pbi->pts1 = pts;
6982 			pbi->frame_cnt_window = 0;
6983 			pbi->duration_from_pts_done = 0;
6984 			pbi->vp9_first_pts_ready = 1;
6985 		} else {
6986 			return false;
6987 		}
6988 	} else {
6989 		if (pts < pbi->pts1) {
6990 			if (pbi->frame_cnt_window > FRAME_CNT_WINDOW_SIZE) {
6991 				pbi->pts1 = pts;
6992 				pbi->frame_cnt_window = 0;
6993 			}
6994 		}
6995 
6996 		if (valid && (pbi->frame_cnt_window > FRAME_CNT_WINDOW_SIZE) &&
6997 			(pts > pbi->pts1) && (pbi->duration_from_pts_done == 0)) {
6998 				old_duration = pbi->frame_dur;
6999 				pbi->pts2 = pts;
7000 				pts_duration = (((pbi->pts2 - pbi->pts1) * 16) /
7001 					(pbi->frame_cnt_window * 15));
7002 
7003 			if (close_to(pts_duration, old_duration, 2000)) {
7004 				pbi->frame_dur = pts_duration;
7005 				if ((debug & VP9_DEBUG_OUT_PTS) != 0)
7006 					pr_info("use calc duration %d\n", pts_duration);
7007 			}
7008 
7009 			if (pbi->duration_from_pts_done == 0) {
7010 				if (close_to(pts_duration, old_duration, RATE_CORRECTION_THRESHOLD)) {
7011 					pbi->duration_from_pts_done = 1;
7012 				} else {
7013 					if (!close_to(pts_duration,
7014 						 old_duration, 1000) &&
7015 						!close_to(pts_duration,
7016 						pbi->frame_dur, 1000) &&
7017 						close_to(pts_duration,
7018 						pbi->last_duration, 200)) {
7019 						/* frame_dur must
7020 						 *  wrong,recover it.
7021 						 */
7022 						pbi->frame_dur = pts_duration;
7023 					}
7024 					pbi->pts1 = pbi->pts2;
7025 					pbi->frame_cnt_window = 0;
7026 					pbi->duration_from_pts_done = 0;
7027 				}
7028 			}
7029 			pbi->last_duration = pts_duration;
7030 		}
7031 	}
7032 	return true;
7033 }
7034 
update_vf_memhandle(struct VP9Decoder_s * pbi,struct vframe_s * vf,struct PIC_BUFFER_CONFIG_s * pic)7035 static void update_vf_memhandle(struct VP9Decoder_s *pbi,
7036 	struct vframe_s *vf, struct PIC_BUFFER_CONFIG_s *pic)
7037 {
7038 	if (pic->index < 0) {
7039 		vf->mem_handle = NULL;
7040 		vf->mem_head_handle = NULL;
7041 		vf->mem_dw_handle = NULL;
7042 	} else if (vf->type & VIDTYPE_SCATTER) {
7043 		vf->mem_handle =
7044 			decoder_mmu_box_get_mem_handle(
7045 				pbi->mmu_box, pic->index);
7046 		vf->mem_head_handle =
7047 			decoder_bmmu_box_get_mem_handle(
7048 				pbi->bmmu_box,
7049 				HEADER_BUFFER_IDX(pic->BUF_index));
7050 		if (pbi->double_write_mode == 3)
7051 			vf->mem_dw_handle =
7052 				decoder_bmmu_box_get_mem_handle(
7053 					pbi->bmmu_box,
7054 					VF_BUFFER_IDX(pic->BUF_index));
7055 		else
7056 			vf->mem_dw_handle = NULL;
7057 	} else {
7058 		vf->mem_handle =
7059 			decoder_bmmu_box_get_mem_handle(
7060 				pbi->bmmu_box, VF_BUFFER_IDX(pic->BUF_index));
7061 		vf->mem_head_handle = NULL;
7062 		vf->mem_dw_handle = NULL;
7063 		/*vf->mem_head_handle =
7064 		 *decoder_bmmu_box_get_mem_handle(
7065 		 *hevc->bmmu_box, VF_BUFFER_IDX(BUF_index));
7066 		 */
7067 	}
7068 }
7069 
pbi_update_gvs(struct VP9Decoder_s * pbi)7070 static inline void pbi_update_gvs(struct VP9Decoder_s *pbi)
7071 {
7072 	if (pbi->gvs->frame_height != frame_height) {
7073 		pbi->gvs->frame_width = frame_width;
7074 		pbi->gvs->frame_height = frame_height;
7075 	}
7076 	if (pbi->gvs->frame_dur != pbi->frame_dur) {
7077 		pbi->gvs->frame_dur = pbi->frame_dur;
7078 		if (pbi->frame_dur != 0)
7079 			pbi->gvs->frame_rate = 96000 / pbi->frame_dur;
7080 		else
7081 			pbi->gvs->frame_rate = -1;
7082 	}
7083 	pbi->gvs->status = pbi->stat | pbi->fatal_error;
7084 }
7085 
prepare_display_buf(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * pic_config)7086 static int prepare_display_buf(struct VP9Decoder_s *pbi,
7087 				struct PIC_BUFFER_CONFIG_s *pic_config)
7088 {
7089 	struct vframe_s *vf = NULL;
7090 	struct vdec_s *pvdec = hw_to_vdec(pbi);
7091 	int stream_offset = pic_config->stream_offset;
7092 	unsigned short slice_type = pic_config->slice_type;
7093 	struct aml_vcodec_ctx * v4l2_ctx = pbi->v4l2_ctx;
7094 	ulong nv_order = VIDTYPE_VIU_NV21;
7095 	u32 pts_valid = 0, pts_us64_valid = 0;
7096 	u32 pts_save;
7097 	u64 pts_us64_save;
7098 	u32 frame_size = 0;
7099 
7100 
7101 	if (debug & VP9_DEBUG_BUFMGR)
7102 		pr_info("%s index = %d\r\n", __func__, pic_config->index);
7103 	if (kfifo_get(&pbi->newframe_q, &vf) == 0) {
7104 		pr_info("fatal error, no available buffer slot.");
7105 		return -1;
7106 	}
7107 
7108 	/* swap uv */
7109 	if (pbi->is_used_v4l) {
7110 		if ((v4l2_ctx->cap_pix_fmt == V4L2_PIX_FMT_NV12) ||
7111 			(v4l2_ctx->cap_pix_fmt == V4L2_PIX_FMT_NV12M))
7112 			nv_order = VIDTYPE_VIU_NV12;
7113 	}
7114 
7115 	if (pic_config->double_write_mode)
7116 		set_canvas(pbi, pic_config);
7117 
7118 	display_frame_count[pbi->index]++;
7119 	if (vf) {
7120 		if (pbi->is_used_v4l) {
7121 			vf->v4l_mem_handle
7122 				= pbi->m_BUF[pic_config->BUF_index].v4l_ref_buf_addr;
7123 			if (pbi->mmu_enable) {
7124 				vf->mm_box.bmmu_box	= pbi->bmmu_box;
7125 				vf->mm_box.bmmu_idx	= HEADER_BUFFER_IDX(pic_config->BUF_index);
7126 				vf->mm_box.mmu_box	= pbi->mmu_box;
7127 				vf->mm_box.mmu_idx	= pic_config->index;
7128 			}
7129 		}
7130 
7131 #ifdef MULTI_INSTANCE_SUPPORT
7132 		if (vdec_frame_based(pvdec)) {
7133 			vf->pts = pic_config->pts;
7134 			vf->pts_us64 = pic_config->pts64;
7135 			vf->timestamp = pic_config->timestamp;
7136 			if (vf->pts != 0 || vf->pts_us64 != 0) {
7137 				pts_valid = 1;
7138 				pts_us64_valid = 1;
7139 			} else {
7140 				pts_valid = 0;
7141 				pts_us64_valid = 0;
7142 			}
7143 		} else
7144 #endif
7145 		/* if (pts_lookup_offset(PTS_TYPE_VIDEO,
7146 		 *   stream_offset, &vf->pts, 0) != 0) {
7147 		 */
7148 		if (pts_lookup_offset_us64
7149 			(PTS_TYPE_VIDEO, stream_offset, &vf->pts,
7150 			&frame_size, 0,
7151 			&vf->pts_us64) != 0) {
7152 #ifdef DEBUG_PTS
7153 			pbi->pts_missed++;
7154 #endif
7155 			vf->pts = 0;
7156 			vf->pts_us64 = 0;
7157 			pts_valid = 0;
7158 			pts_us64_valid = 0;
7159 		} else {
7160 #ifdef DEBUG_PTS
7161 			pbi->pts_hit++;
7162 #endif
7163 			pts_valid = 1;
7164 			pts_us64_valid = 1;
7165 		}
7166 
7167 		fill_frame_info(pbi, pic_config, frame_size, vf->pts);
7168 
7169 		pts_save = vf->pts;
7170 		pts_us64_save = vf->pts_us64;
7171 		if (pbi->pts_unstable) {
7172 			frame_duration_adapt(pbi, vf, pts_valid);
7173 			if (pbi->duration_from_pts_done) {
7174 				pbi->pts_mode = PTS_NONE_REF_USE_DURATION;
7175 			} else {
7176 				if (pts_valid || pts_us64_valid)
7177 					pbi->pts_mode = PTS_NORMAL;
7178 			}
7179 		}
7180 
7181 		if ((pbi->pts_mode == PTS_NORMAL) && (vf->pts != 0)
7182 			&& pbi->get_frame_dur) {
7183 			int pts_diff = (int)vf->pts - pbi->last_lookup_pts;
7184 
7185 			if (pts_diff < 0) {
7186 				pbi->pts_mode_switching_count++;
7187 				pbi->pts_mode_recovery_count = 0;
7188 
7189 				if (pbi->pts_mode_switching_count >=
7190 					PTS_MODE_SWITCHING_THRESHOLD) {
7191 					pbi->pts_mode =
7192 						PTS_NONE_REF_USE_DURATION;
7193 					pr_info
7194 					("HEVC: switch to n_d mode.\n");
7195 				}
7196 
7197 			} else {
7198 				int p = PTS_MODE_SWITCHING_RECOVERY_THREASHOLD;
7199 
7200 				pbi->pts_mode_recovery_count++;
7201 				if (pbi->pts_mode_recovery_count > p) {
7202 					pbi->pts_mode_switching_count = 0;
7203 					pbi->pts_mode_recovery_count = 0;
7204 				}
7205 			}
7206 		}
7207 
7208 		if (vf->pts != 0)
7209 			pbi->last_lookup_pts = vf->pts;
7210 
7211 		if ((pbi->pts_mode == PTS_NONE_REF_USE_DURATION)
7212 			&& (slice_type != KEY_FRAME))
7213 			vf->pts = pbi->last_pts + DUR2PTS(pbi->frame_dur);
7214 		pbi->last_pts = vf->pts;
7215 
7216 		if (vf->pts_us64 != 0)
7217 			pbi->last_lookup_pts_us64 = vf->pts_us64;
7218 
7219 		if ((pbi->pts_mode == PTS_NONE_REF_USE_DURATION)
7220 			&& (slice_type != KEY_FRAME)) {
7221 			vf->pts_us64 =
7222 				pbi->last_pts_us64 +
7223 				(DUR2PTS(pbi->frame_dur) * 100 / 9);
7224 		}
7225 		pbi->last_pts_us64 = vf->pts_us64;
7226 		if ((debug & VP9_DEBUG_OUT_PTS) != 0) {
7227 			pr_info
7228 			("VP9 dec out pts: pts_mode=%d,dur=%d,pts(%d,%lld)(%d,%lld)\n",
7229 			pbi->pts_mode, pbi->frame_dur, vf->pts,
7230 			vf->pts_us64, pts_save, pts_us64_save);
7231 		}
7232 
7233 		if (pbi->pts_mode == PTS_NONE_REF_USE_DURATION) {
7234 			vf->disp_pts = vf->pts;
7235 			vf->disp_pts_us64 = vf->pts_us64;
7236 			vf->pts = pts_save;
7237 			vf->pts_us64 = pts_us64_save;
7238 		} else {
7239 			vf->disp_pts = 0;
7240 			vf->disp_pts_us64 = 0;
7241 		}
7242 
7243 		vf->index = 0xff00 | pic_config->index;
7244 
7245 		if (pic_config->double_write_mode & 0x10) {
7246 			/* double write only */
7247 			vf->compBodyAddr = 0;
7248 			vf->compHeadAddr = 0;
7249 		} else {
7250 			if (pbi->mmu_enable) {
7251 				vf->compBodyAddr = 0;
7252 				vf->compHeadAddr = pic_config->header_adr;
7253 			} else {
7254 				/*vf->compBodyAddr = pic_config->mc_y_adr;
7255 				 *vf->compHeadAddr = pic_config->mc_y_adr +
7256 				 *pic_config->comp_body_size; */
7257 				/*head adr*/
7258 			}
7259 			vf->canvas0Addr = vf->canvas1Addr = 0;
7260 		}
7261 		if (pic_config->double_write_mode) {
7262 			vf->type = VIDTYPE_PROGRESSIVE |
7263 				VIDTYPE_VIU_FIELD;
7264 			vf->type |= nv_order;
7265 			if ((pic_config->double_write_mode == 3) &&
7266 				(!IS_8K_SIZE(pic_config->y_crop_width,
7267 				pic_config->y_crop_height))) {
7268 				vf->type |= VIDTYPE_COMPRESS;
7269 				if (pbi->mmu_enable)
7270 					vf->type |= VIDTYPE_SCATTER;
7271 			}
7272 #ifdef MULTI_INSTANCE_SUPPORT
7273 			if (pbi->m_ins_flag) {
7274 				vf->canvas0Addr = vf->canvas1Addr = -1;
7275 				vf->plane_num = 2;
7276 				vf->canvas0_config[0] =
7277 					pic_config->canvas_config[0];
7278 				vf->canvas0_config[1] =
7279 					pic_config->canvas_config[1];
7280 				vf->canvas1_config[0] =
7281 					pic_config->canvas_config[0];
7282 				vf->canvas1_config[1] =
7283 					pic_config->canvas_config[1];
7284 
7285 			} else
7286 #endif
7287 				vf->canvas0Addr = vf->canvas1Addr =
7288 					spec2canvas(pic_config);
7289 		} else {
7290 			vf->canvas0Addr = vf->canvas1Addr = 0;
7291 			vf->type = VIDTYPE_COMPRESS | VIDTYPE_VIU_FIELD;
7292 			if (pbi->mmu_enable)
7293 				vf->type |= VIDTYPE_SCATTER;
7294 		}
7295 
7296 		switch (pic_config->bit_depth) {
7297 		case VPX_BITS_8:
7298 			vf->bitdepth = BITDEPTH_Y8 |
7299 				BITDEPTH_U8 | BITDEPTH_V8;
7300 			break;
7301 		case VPX_BITS_10:
7302 		case VPX_BITS_12:
7303 			vf->bitdepth = BITDEPTH_Y10 |
7304 				BITDEPTH_U10 | BITDEPTH_V10;
7305 			break;
7306 		default:
7307 			vf->bitdepth = BITDEPTH_Y10 |
7308 				BITDEPTH_U10 | BITDEPTH_V10;
7309 			break;
7310 		}
7311 		if ((vf->type & VIDTYPE_COMPRESS) == 0)
7312 			vf->bitdepth =
7313 				BITDEPTH_Y8 | BITDEPTH_U8 | BITDEPTH_V8;
7314 		if (pic_config->bit_depth == VPX_BITS_8)
7315 			vf->bitdepth |= BITDEPTH_SAVING_MODE;
7316 
7317 		/* if((vf->width!=pic_config->width)|
7318 		 *	(vf->height!=pic_config->height))
7319 		 */
7320 		/* pr_info("aaa: %d/%d, %d/%d\n",
7321 		   vf->width,vf->height, pic_config->width,
7322 			pic_config->height); */
7323 		vf->width = pic_config->y_crop_width /
7324 			get_double_write_ratio(pbi,
7325 				pic_config->double_write_mode);
7326 		vf->height = pic_config->y_crop_height /
7327 			get_double_write_ratio(pbi,
7328 				pic_config->double_write_mode);
7329 		if (force_w_h != 0) {
7330 			vf->width = (force_w_h >> 16) & 0xffff;
7331 			vf->height = force_w_h & 0xffff;
7332 		}
7333 		vf->compWidth = pic_config->y_crop_width;
7334 		vf->compHeight = pic_config->y_crop_height;
7335 		set_frame_info(pbi, vf);
7336 		if (force_fps & 0x100) {
7337 			u32 rate = force_fps & 0xff;
7338 
7339 			if (rate)
7340 				vf->duration = 96000/rate;
7341 			else
7342 				vf->duration = 0;
7343 		}
7344 		update_vf_memhandle(pbi, vf, pic_config);
7345 		if (!(pic_config->y_crop_width == 196
7346 		&& pic_config->y_crop_height == 196
7347 		&& (debug & VP9_DEBUG_NO_TRIGGER_FRAME) == 0
7348 		&& (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_TXLX))) {
7349 			struct vdec_info tmp4x;
7350 
7351 			inc_vf_ref(pbi, pic_config->index);
7352 			decoder_do_frame_check(pvdec, vf);
7353 			kfifo_put(&pbi->display_q, (const struct vframe_s *)vf);
7354 			ATRACE_COUNTER(MODULE_NAME, vf->pts);
7355 			pbi->vf_pre_count++;
7356 			pbi_update_gvs(pbi);
7357 			/*count info*/
7358 			vdec_count_info(pbi->gvs, 0, stream_offset);
7359 			memcpy(&tmp4x, pbi->gvs, sizeof(struct vdec_info));
7360 			tmp4x.bit_depth_luma = pbi->vp9_param.p.bit_depth;
7361 			tmp4x.bit_depth_chroma = pbi->vp9_param.p.bit_depth;
7362 			tmp4x.double_write_mode = get_double_write_mode(pbi);
7363 			vdec_fill_vdec_frame(pvdec, &pbi->vframe_qos, &tmp4x,
7364 				vf, pic_config->hw_decode_time);
7365 			pvdec->vdec_fps_detec(pvdec->id);
7366 			if (without_display_mode == 0) {
7367 				vf_notify_receiver(pbi->provider_name,
7368 						VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL);
7369 			} else
7370 				vvp9_vf_put(vvp9_vf_get(pbi), pbi);
7371 		} else {
7372 			pbi->stat |= VP9_TRIGGER_FRAME_DONE;
7373 			hevc_source_changed(VFORMAT_VP9, 196, 196, 30);
7374 			pr_debug("[%s %d] drop trigger frame width %d height %d  state 0x%x\n",
7375 				__func__, __LINE__, vf->width,
7376 				vf->height, pbi->stat);
7377 		}
7378 	}
7379 
7380 	return 0;
7381 }
7382 
notify_v4l_eos(struct vdec_s * vdec)7383 static int notify_v4l_eos(struct vdec_s *vdec)
7384 {
7385 	struct VP9Decoder_s *hw = (struct VP9Decoder_s *)vdec->private;
7386 	struct aml_vcodec_ctx *ctx = (struct aml_vcodec_ctx *)(hw->v4l2_ctx);
7387 	struct vframe_s *vf = &hw->vframe_dummy;
7388 	struct vdec_v4l2_buffer *fb = NULL;
7389 	int index = INVALID_IDX;
7390 	ulong expires;
7391 
7392 	if (hw->is_used_v4l && hw->eos) {
7393 		expires = jiffies + msecs_to_jiffies(2000);
7394 		while (INVALID_IDX == (index = v4l_get_free_fb(hw))) {
7395 			if (time_after(jiffies, expires) ||
7396 				v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx))
7397 				break;
7398 		}
7399 
7400 		if (index == INVALID_IDX) {
7401 			if (vdec_v4l_get_buffer(hw->v4l2_ctx, &fb) < 0) {
7402 				pr_err("[%d] EOS get free buff fail.\n", ctx->id);
7403 				return -1;
7404 			}
7405 		}
7406 
7407 		vf->type		|= VIDTYPE_V4L_EOS;
7408 		vf->timestamp		= ULONG_MAX;
7409 		vf->flag		= VFRAME_FLAG_EMPTY_FRAME_V4L;
7410 		vf->v4l_mem_handle	= (index == INVALID_IDX) ? (ulong)fb :
7411 					hw->m_BUF[index].v4l_ref_buf_addr;
7412 
7413 		kfifo_put(&hw->display_q, (const struct vframe_s *)vf);
7414 		vf_notify_receiver(vdec->vf_provider_name,
7415 			VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL);
7416 
7417 		pr_info("[%d] VP9 EOS notify.\n", ctx->id);
7418 	}
7419 
7420 	return 0;
7421 }
7422 
get_rpm_param(union param_u * params)7423 static void get_rpm_param(union param_u *params)
7424 {
7425 	int i;
7426 	unsigned int data32;
7427 
7428 	if (debug & VP9_DEBUG_BUFMGR)
7429 		pr_info("enter %s\r\n", __func__);
7430 	for (i = 0; i < 128; i++) {
7431 		do {
7432 			data32 = READ_VREG(RPM_CMD_REG);
7433 			/*pr_info("%x\n", data32);*/
7434 		} while ((data32 & 0x10000) == 0);
7435 		params->l.data[i] = data32&0xffff;
7436 		/*pr_info("%x\n", data32);*/
7437 		WRITE_VREG(RPM_CMD_REG, 0);
7438 	}
7439 	if (debug & VP9_DEBUG_BUFMGR)
7440 		pr_info("leave %s\r\n", __func__);
7441 }
debug_buffer_mgr_more(struct VP9Decoder_s * pbi)7442 static void debug_buffer_mgr_more(struct VP9Decoder_s *pbi)
7443 {
7444 	int i;
7445 
7446 	if (!(debug & VP9_DEBUG_BUFMGR_MORE))
7447 		return;
7448 	pr_info("vp9_param: (%d)\n", pbi->slice_idx);
7449 	for (i = 0; i < (RPM_END-RPM_BEGIN); i++) {
7450 		pr_info("%04x ", pbi->vp9_param.l.data[i]);
7451 		if (((i + 1) & 0xf) == 0)
7452 			pr_info("\n");
7453 	}
7454 	pr_info("=============param==========\r\n");
7455 	pr_info("profile               %x\r\n", pbi->vp9_param.p.profile);
7456 	pr_info("show_existing_frame   %x\r\n",
7457 	pbi->vp9_param.p.show_existing_frame);
7458 	pr_info("frame_to_show_idx     %x\r\n",
7459 	pbi->vp9_param.p.frame_to_show_idx);
7460 	pr_info("frame_type            %x\r\n", pbi->vp9_param.p.frame_type);
7461 	pr_info("show_frame            %x\r\n", pbi->vp9_param.p.show_frame);
7462 	pr_info("e.r.r.o.r_resilient_mode  %x\r\n",
7463 	pbi->vp9_param.p.error_resilient_mode);
7464 	pr_info("intra_only            %x\r\n", pbi->vp9_param.p.intra_only);
7465 	pr_info("display_size_present  %x\r\n",
7466 	pbi->vp9_param.p.display_size_present);
7467 	pr_info("reset_frame_context   %x\r\n",
7468 	pbi->vp9_param.p.reset_frame_context);
7469 	pr_info("refresh_frame_flags   %x\r\n",
7470 	pbi->vp9_param.p.refresh_frame_flags);
7471 	pr_info("bit_depth             %x\r\n", pbi->vp9_param.p.bit_depth);
7472 	pr_info("width                 %x\r\n", pbi->vp9_param.p.width);
7473 	pr_info("height                %x\r\n", pbi->vp9_param.p.height);
7474 	pr_info("display_width         %x\r\n", pbi->vp9_param.p.display_width);
7475 	pr_info("display_height        %x\r\n", pbi->vp9_param.p.display_height);
7476 	pr_info("ref_info              %x\r\n", pbi->vp9_param.p.ref_info);
7477 	pr_info("same_frame_size       %x\r\n", pbi->vp9_param.p.same_frame_size);
7478 	if (!(debug & VP9_DEBUG_DBG_LF_PRINT))
7479 		return;
7480 	pr_info("mode_ref_delta_enabled: 0x%x\r\n",
7481 	pbi->vp9_param.p.mode_ref_delta_enabled);
7482 	pr_info("sharpness_level: 0x%x\r\n",
7483 	pbi->vp9_param.p.sharpness_level);
7484 	pr_info("ref_deltas: 0x%x, 0x%x, 0x%x, 0x%x\r\n",
7485 	pbi->vp9_param.p.ref_deltas[0], pbi->vp9_param.p.ref_deltas[1],
7486 	pbi->vp9_param.p.ref_deltas[2], pbi->vp9_param.p.ref_deltas[3]);
7487 	pr_info("mode_deltas: 0x%x, 0x%x\r\n", pbi->vp9_param.p.mode_deltas[0],
7488 	pbi->vp9_param.p.mode_deltas[1]);
7489 	pr_info("filter_level: 0x%x\r\n", pbi->vp9_param.p.filter_level);
7490 	pr_info("seg_enabled: 0x%x\r\n", pbi->vp9_param.p.seg_enabled);
7491 	pr_info("seg_abs_delta: 0x%x\r\n", pbi->vp9_param.p.seg_abs_delta);
7492 	pr_info("seg_lf_feature_enabled: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n",
7493 	(pbi->vp9_param.p.seg_lf_info[0]>>15 & 1),
7494 	(pbi->vp9_param.p.seg_lf_info[1]>>15 & 1),
7495 	(pbi->vp9_param.p.seg_lf_info[2]>>15 & 1),
7496 	(pbi->vp9_param.p.seg_lf_info[3]>>15 & 1),
7497 	(pbi->vp9_param.p.seg_lf_info[4]>>15 & 1),
7498 	(pbi->vp9_param.p.seg_lf_info[5]>>15 & 1),
7499 	(pbi->vp9_param.p.seg_lf_info[6]>>15 & 1),
7500 	(pbi->vp9_param.p.seg_lf_info[7]>>15 & 1));
7501 	pr_info("seg_lf_feature_data: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n",
7502 	(pbi->vp9_param.p.seg_lf_info[0] & 0x13f),
7503 	(pbi->vp9_param.p.seg_lf_info[1] & 0x13f),
7504 	(pbi->vp9_param.p.seg_lf_info[2] & 0x13f),
7505 	(pbi->vp9_param.p.seg_lf_info[3] & 0x13f),
7506 	(pbi->vp9_param.p.seg_lf_info[4] & 0x13f),
7507 	(pbi->vp9_param.p.seg_lf_info[5] & 0x13f),
7508 	(pbi->vp9_param.p.seg_lf_info[6] & 0x13f),
7509 	(pbi->vp9_param.p.seg_lf_info[7] & 0x13f));
7510 
7511 }
7512 
7513 
vp9_recycle_mmu_buf_tail(struct VP9Decoder_s * pbi)7514 static void vp9_recycle_mmu_buf_tail(struct VP9Decoder_s *pbi)
7515 {
7516 	struct VP9_Common_s *const cm = &pbi->common;
7517 	if (pbi->double_write_mode & 0x10)
7518 		return;
7519 	if (cm->cur_fb_idx_mmu != INVALID_IDX) {
7520 		if (pbi->used_4k_num == -1) {
7521 			pbi->used_4k_num =
7522 			(READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
7523 			if (pbi->m_ins_flag)
7524 				hevc_mmu_dma_check(hw_to_vdec(pbi));
7525 		}
7526 		decoder_mmu_box_free_idx_tail(pbi->mmu_box,
7527 			cm->cur_fb_idx_mmu, pbi->used_4k_num);
7528 		cm->cur_fb_idx_mmu = INVALID_IDX;
7529 		pbi->used_4k_num = -1;
7530 	}
7531 }
7532 
7533 #ifdef MULTI_INSTANCE_SUPPORT
vp9_recycle_mmu_buf(struct VP9Decoder_s * pbi)7534 static void vp9_recycle_mmu_buf(struct VP9Decoder_s *pbi)
7535 {
7536 	struct VP9_Common_s *const cm = &pbi->common;
7537 	if (pbi->double_write_mode & 0x10)
7538 		return;
7539 	if (cm->cur_fb_idx_mmu != INVALID_IDX) {
7540 		decoder_mmu_box_free_idx(pbi->mmu_box,
7541 			cm->cur_fb_idx_mmu);
7542 
7543 		cm->cur_fb_idx_mmu = INVALID_IDX;
7544 		pbi->used_4k_num = -1;
7545 	}
7546 }
7547 
vp9_recycle_mmu_work(struct work_struct * work)7548 void vp9_recycle_mmu_work(struct work_struct *work)
7549 {
7550 	struct VP9Decoder_s *pbi = container_of(work,
7551 		struct VP9Decoder_s, recycle_mmu_work);
7552 
7553 	vp9_recycle_mmu_buf(pbi);
7554 }
7555 #endif
7556 
7557 
dec_again_process(struct VP9Decoder_s * pbi)7558 static void dec_again_process(struct VP9Decoder_s *pbi)
7559 {
7560 	amhevc_stop();
7561 	pbi->dec_result = DEC_RESULT_AGAIN;
7562 	if (pbi->process_state ==
7563 		PROC_STATE_DECODESLICE) {
7564 		pbi->process_state =
7565 		PROC_STATE_SENDAGAIN;
7566 		if (pbi->mmu_enable) {
7567 			/*
7568 			 * Because vp9_recycle_mmu_buf has sleep function,we can't
7569 			 * call it directly. Use a recycle_mmu_work to substitude it.
7570 			 */
7571 			vdec_schedule_work(&pbi->recycle_mmu_work);
7572 		}
7573 	}
7574 	reset_process_time(pbi);
7575 	vdec_schedule_work(&pbi->work);
7576 }
7577 
continue_decoding(struct VP9Decoder_s * pbi)7578 int continue_decoding(struct VP9Decoder_s *pbi)
7579 {
7580 	int ret;
7581 	int i;
7582 	struct VP9_Common_s *const cm = &pbi->common;
7583 	struct aml_vcodec_ctx *ctx = (struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
7584 	debug_buffer_mgr_more(pbi);
7585 
7586 	if (pbi->is_used_v4l && ctx->param_sets_from_ucode)
7587 		pbi->res_ch_flag = 0;
7588 	bit_depth_luma = pbi->vp9_param.p.bit_depth;
7589 	bit_depth_chroma = pbi->vp9_param.p.bit_depth;
7590 
7591 	if ((pbi->vp9_param.p.bit_depth >= VPX_BITS_10) &&
7592 		(get_double_write_mode(pbi) == 0x10)) {
7593 		pbi->fatal_error |= DECODER_FATAL_ERROR_SIZE_OVERFLOW;
7594 		pr_err("fatal err, bit_depth %d, unsupport dw 0x10\n",
7595 			pbi->vp9_param.p.bit_depth);
7596 		return -1;
7597 	}
7598 
7599 	if (pbi->process_state != PROC_STATE_SENDAGAIN) {
7600 		ret = vp9_bufmgr_process(pbi, &pbi->vp9_param);
7601 		if (!pbi->m_ins_flag)
7602 			pbi->slice_idx++;
7603 	} else {
7604 		union param_u *params = &pbi->vp9_param;
7605 		if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
7606 			ret = vp9_alloc_mmu(pbi,
7607 				cm->new_fb_idx,
7608 				params->p.width,
7609 				params->p.height,
7610 				params->p.bit_depth,
7611 				pbi->frame_mmu_map_addr);
7612 			if (ret >= 0)
7613 				cm->cur_fb_idx_mmu = cm->new_fb_idx;
7614 			else
7615 				pr_err("can't alloc need mmu1,idx %d ret =%d\n",
7616 					cm->new_fb_idx,
7617 					ret);
7618 		} else {
7619 			ret = 0;
7620 		}
7621 		WRITE_VREG(HEVC_PARSER_PICTURE_SIZE,
7622 		(params->p.height << 16) | params->p.width);
7623 	}
7624 	if (ret < 0) {
7625 		pr_info("vp9_bufmgr_process=> %d, VP9_10B_DISCARD_NAL\r\n",
7626 		 ret);
7627 		WRITE_VREG(HEVC_DEC_STATUS_REG, VP9_10B_DISCARD_NAL);
7628 		cm->show_frame = 0;
7629 		if (pbi->mmu_enable)
7630 			vp9_recycle_mmu_buf(pbi);
7631 #ifdef MULTI_INSTANCE_SUPPORT
7632 		if (pbi->m_ins_flag) {
7633 			pbi->dec_result = DEC_RESULT_DONE;
7634 #ifdef SUPPORT_FB_DECODING
7635 			if (pbi->used_stage_buf_num == 0)
7636 #endif
7637 				amhevc_stop();
7638 			vdec_schedule_work(&pbi->work);
7639 		}
7640 #endif
7641 		return ret;
7642 	} else if (ret == 0) {
7643 		struct PIC_BUFFER_CONFIG_s *cur_pic_config
7644 			= &cm->cur_frame->buf;
7645 		cur_pic_config->decode_idx = pbi->frame_count;
7646 
7647 		if (pbi->process_state != PROC_STATE_SENDAGAIN) {
7648 			if (!pbi->m_ins_flag) {
7649 				pbi->frame_count++;
7650 				decode_frame_count[pbi->index]
7651 					= pbi->frame_count;
7652 			}
7653 #ifdef MULTI_INSTANCE_SUPPORT
7654 			if (pbi->chunk) {
7655 				cur_pic_config->pts = pbi->chunk->pts;
7656 				cur_pic_config->pts64 = pbi->chunk->pts64;
7657 				cur_pic_config->timestamp = pbi->chunk->timestamp;
7658 			}
7659 #endif
7660 		}
7661 		/*pr_info("Decode Frame Data %d\n", pbi->frame_count);*/
7662 		config_pic_size(pbi, pbi->vp9_param.p.bit_depth);
7663 
7664 		if ((pbi->common.frame_type != KEY_FRAME)
7665 			&& (!pbi->common.intra_only)) {
7666 			config_mc_buffer(pbi, pbi->vp9_param.p.bit_depth);
7667 #ifdef SUPPORT_FB_DECODING
7668 			if (pbi->used_stage_buf_num == 0)
7669 #endif
7670 				config_mpred_hw(pbi);
7671 		} else {
7672 #ifdef SUPPORT_FB_DECODING
7673 			if (pbi->used_stage_buf_num == 0)
7674 #endif
7675 				clear_mpred_hw(pbi);
7676 		}
7677 #ifdef MCRCC_ENABLE
7678 		if (mcrcc_cache_alg_flag)
7679 			config_mcrcc_axi_hw_new(pbi);
7680 		else
7681 			config_mcrcc_axi_hw(pbi);
7682 #endif
7683 		config_sao_hw(pbi, &pbi->vp9_param);
7684 
7685 #ifdef VP9_LPF_LVL_UPDATE
7686 		/*
7687 		* Get loop filter related picture level parameters from Parser
7688 		*/
7689 		pbi->lf->mode_ref_delta_enabled = pbi->vp9_param.p.mode_ref_delta_enabled;
7690 		pbi->lf->sharpness_level = pbi->vp9_param.p.sharpness_level;
7691 		for (i = 0; i < 4; i++)
7692 			pbi->lf->ref_deltas[i] = pbi->vp9_param.p.ref_deltas[i];
7693 		for (i = 0; i < 2; i++)
7694 			pbi->lf->mode_deltas[i] = pbi->vp9_param.p.mode_deltas[i];
7695 		pbi->default_filt_lvl = pbi->vp9_param.p.filter_level;
7696 		pbi->seg_4lf->enabled = pbi->vp9_param.p.seg_enabled;
7697 		pbi->seg_4lf->abs_delta = pbi->vp9_param.p.seg_abs_delta;
7698 		for (i = 0; i < MAX_SEGMENTS; i++)
7699 			pbi->seg_4lf->feature_mask[i] = (pbi->vp9_param.p.seg_lf_info[i] &
7700 			0x8000) ? (1 << SEG_LVL_ALT_LF) : 0;
7701 		for (i = 0; i < MAX_SEGMENTS; i++)
7702 			pbi->seg_4lf->feature_data[i][SEG_LVL_ALT_LF]
7703 			= (pbi->vp9_param.p.seg_lf_info[i]
7704 			& 0x100) ? -(pbi->vp9_param.p.seg_lf_info[i]
7705 			& 0x3f) : (pbi->vp9_param.p.seg_lf_info[i] & 0x3f);
7706 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
7707 			/*Set pipeline mode*/
7708 			uint32_t lpf_data32 = READ_VREG(HEVC_DBLK_CFGB);
7709 			/*dblk pipeline mode=1 for performance*/
7710 			if (pbi->vp9_param.p.width >= 1280)
7711 				lpf_data32 |= (0x1 << 4);
7712 			else
7713 				lpf_data32 &= ~(0x3 << 4);
7714 			WRITE_VREG(HEVC_DBLK_CFGB, lpf_data32);
7715 		}
7716 		/*
7717 		* Update loop filter Thr/Lvl table for every frame
7718 		*/
7719 		/*pr_info
7720 		("vp9_loop_filter (run before every frame decoding start)\n");*/
7721 		vp9_loop_filter_frame_init(pbi->seg_4lf,
7722 			pbi->lfi, pbi->lf, pbi->default_filt_lvl);
7723 #endif
7724 		/*pr_info("HEVC_DEC_STATUS_REG <= VP9_10B_DECODE_SLICE\n");*/
7725 		WRITE_VREG(HEVC_DEC_STATUS_REG, VP9_10B_DECODE_SLICE);
7726 	} else {
7727 		pr_info("Skip search next start code\n");
7728 		cm->prev_fb_idx = INVALID_IDX;
7729 		/*skip, search next start code*/
7730 		WRITE_VREG(HEVC_DEC_STATUS_REG, VP9_10B_DECODE_SLICE);
7731 	}
7732 	pbi->process_state = PROC_STATE_DECODESLICE;
7733 	if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
7734 		if (pbi->last_put_idx < pbi->used_buf_num) {
7735 			struct RefCntBuffer_s *frame_bufs =
7736 				cm->buffer_pool->frame_bufs;
7737 			int i = pbi->last_put_idx;
7738 			/*free not used buffers.*/
7739 			if ((frame_bufs[i].ref_count == 0) &&
7740 				(frame_bufs[i].buf.vf_ref == 0) &&
7741 				(frame_bufs[i].buf.index != -1)) {
7742 				decoder_mmu_box_free_idx(pbi->mmu_box, i);
7743 			}
7744 			pbi->last_put_idx = -1;
7745 		}
7746 	}
7747 	return ret;
7748 }
7749 
fill_frame_info(struct VP9Decoder_s * pbi,struct PIC_BUFFER_CONFIG_s * frame,unsigned int framesize,unsigned int pts)7750 static void fill_frame_info(struct VP9Decoder_s *pbi,
7751 	struct PIC_BUFFER_CONFIG_s *frame,
7752 	unsigned int framesize,
7753 	unsigned int pts)
7754 {
7755 	struct vframe_qos_s *vframe_qos = &pbi->vframe_qos;
7756 
7757 	if (frame->slice_type == KEY_FRAME)
7758 		vframe_qos->type = 1;
7759 	else if (frame->slice_type == INTER_FRAME)
7760 		vframe_qos->type = 2;
7761 /*
7762 #define SHOW_QOS_INFO
7763 */
7764 	if (input_frame_based(hw_to_vdec(pbi)))
7765 		vframe_qos->size = frame->frame_size2;
7766 	else
7767 		vframe_qos->size = framesize;
7768 	vframe_qos->pts = pts;
7769 #ifdef SHOW_QOS_INFO
7770 	vp9_print(pbi, 0, "slice:%d\n", frame->slice_type);
7771 #endif
7772 	vframe_qos->max_mv = frame->max_mv;
7773 	vframe_qos->avg_mv = frame->avg_mv;
7774 	vframe_qos->min_mv = frame->min_mv;
7775 #ifdef SHOW_QOS_INFO
7776 	vp9_print(pbi, 0, "mv: max:%d,  avg:%d, min:%d\n",
7777 			vframe_qos->max_mv,
7778 			vframe_qos->avg_mv,
7779 			vframe_qos->min_mv);
7780 #endif
7781 	vframe_qos->max_qp = frame->max_qp;
7782 	vframe_qos->avg_qp = frame->avg_qp;
7783 	vframe_qos->min_qp = frame->min_qp;
7784 #ifdef SHOW_QOS_INFO
7785 	vp9_print(pbi, 0, "qp: max:%d,  avg:%d, min:%d\n",
7786 			vframe_qos->max_qp,
7787 			vframe_qos->avg_qp,
7788 			vframe_qos->min_qp);
7789 #endif
7790 	vframe_qos->max_skip = frame->max_skip;
7791 	vframe_qos->avg_skip = frame->avg_skip;
7792 	vframe_qos->min_skip = frame->min_skip;
7793 #ifdef SHOW_QOS_INFO
7794 	vp9_print(pbi, 0, "skip: max:%d,	avg:%d, min:%d\n",
7795 			vframe_qos->max_skip,
7796 			vframe_qos->avg_skip,
7797 			vframe_qos->min_skip);
7798 #endif
7799 	vframe_qos->num++;
7800 }
7801 
7802 /* only when we decoded one field or one frame,
7803 we can call this function to get qos info*/
get_picture_qos_info(struct VP9Decoder_s * pbi)7804 static void get_picture_qos_info(struct VP9Decoder_s *pbi)
7805 {
7806 	struct PIC_BUFFER_CONFIG_s *frame = &pbi->cur_buf->buf;
7807 	struct vdec_s *vdec = hw_to_vdec(pbi);
7808 
7809 	if (!frame)
7810 		return;
7811 	if (vdec->mvfrm) {
7812 		frame->frame_size2 = vdec->mvfrm->frame_size;
7813 		frame->hw_decode_time =
7814 		local_clock() - vdec->mvfrm->hw_decode_start;
7815 	}
7816 
7817 	if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_G12A) {
7818 		unsigned char a[3];
7819 		unsigned char i, j, t;
7820 		unsigned long  data;
7821 
7822 		data = READ_VREG(HEVC_MV_INFO);
7823 		if (frame->slice_type == KEY_FRAME)
7824 			data = 0;
7825 		a[0] = data & 0xff;
7826 		a[1] = (data >> 8) & 0xff;
7827 		a[2] = (data >> 16) & 0xff;
7828 
7829 		for (i = 0; i < 3; i++) {
7830 			for (j = i+1; j < 3; j++) {
7831 				if (a[j] < a[i]) {
7832 					t = a[j];
7833 					a[j] = a[i];
7834 					a[i] = t;
7835 				} else if (a[j] == a[i]) {
7836 					a[i]++;
7837 					t = a[j];
7838 					a[j] = a[i];
7839 					a[i] = t;
7840 				}
7841 			}
7842 		}
7843 		frame->max_mv = a[2];
7844 		frame->avg_mv = a[1];
7845 		frame->min_mv = a[0];
7846 
7847 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7848 			"mv data %x  a[0]= %x a[1]= %x a[2]= %x\n",
7849 			data, a[0], a[1], a[2]);
7850 
7851 		data = READ_VREG(HEVC_QP_INFO);
7852 		a[0] = data & 0x1f;
7853 		a[1] = (data >> 8) & 0x3f;
7854 		a[2] = (data >> 16) & 0x7f;
7855 
7856 		for (i = 0; i < 3; i++) {
7857 			for (j = i+1; j < 3; j++) {
7858 				if (a[j] < a[i]) {
7859 					t = a[j];
7860 					a[j] = a[i];
7861 					a[i] = t;
7862 				} else if (a[j] == a[i]) {
7863 					a[i]++;
7864 					t = a[j];
7865 					a[j] = a[i];
7866 					a[i] = t;
7867 				}
7868 			}
7869 		}
7870 		frame->max_qp = a[2];
7871 		frame->avg_qp = a[1];
7872 		frame->min_qp = a[0];
7873 
7874 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7875 			"qp data %x  a[0]= %x a[1]= %x a[2]= %x\n",
7876 			data, a[0], a[1], a[2]);
7877 
7878 		data = READ_VREG(HEVC_SKIP_INFO);
7879 		a[0] = data & 0x1f;
7880 		a[1] = (data >> 8) & 0x3f;
7881 		a[2] = (data >> 16) & 0x7f;
7882 
7883 		for (i = 0; i < 3; i++) {
7884 			for (j = i+1; j < 3; j++) {
7885 				if (a[j] < a[i]) {
7886 					t = a[j];
7887 					a[j] = a[i];
7888 					a[i] = t;
7889 				} else if (a[j] == a[i]) {
7890 					a[i]++;
7891 					t = a[j];
7892 					a[j] = a[i];
7893 					a[i] = t;
7894 				}
7895 			}
7896 		}
7897 		frame->max_skip = a[2];
7898 		frame->avg_skip = a[1];
7899 		frame->min_skip = a[0];
7900 
7901 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7902 			"skip data %x  a[0]= %x a[1]= %x a[2]= %x\n",
7903 			data, a[0], a[1], a[2]);
7904 	} else {
7905 		uint32_t blk88_y_count;
7906 		uint32_t blk88_c_count;
7907 		uint32_t blk22_mv_count;
7908 		uint32_t rdata32;
7909 		int32_t mv_hi;
7910 		int32_t mv_lo;
7911 		uint32_t rdata32_l;
7912 		uint32_t mvx_L0_hi;
7913 		uint32_t mvy_L0_hi;
7914 		uint32_t mvx_L1_hi;
7915 		uint32_t mvy_L1_hi;
7916 		int64_t value;
7917 		uint64_t temp_value;
7918 		int pic_number = frame->decode_idx;
7919 
7920 		frame->max_mv = 0;
7921 		frame->avg_mv = 0;
7922 		frame->min_mv = 0;
7923 
7924 		frame->max_skip = 0;
7925 		frame->avg_skip = 0;
7926 		frame->min_skip = 0;
7927 
7928 		frame->max_qp = 0;
7929 		frame->avg_qp = 0;
7930 		frame->min_qp = 0;
7931 
7932 		vp9_print(pbi, VP9_DEBUG_QOS_INFO, "slice_type:%d, poc:%d\n",
7933 			frame->slice_type,
7934 			pic_number);
7935 
7936 		/* set rd_idx to 0 */
7937 		WRITE_VREG(HEVC_PIC_QUALITY_CTRL, 0);
7938 
7939 		blk88_y_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
7940 		if (blk88_y_count == 0) {
7941 
7942 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7943 			"[Picture %d Quality] NO Data yet.\n",
7944 			pic_number);
7945 
7946 			/* reset all counts */
7947 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
7948 			return;
7949 		}
7950 		/* qp_y_sum */
7951 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7952 
7953 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7954 			"[Picture %d Quality] Y QP AVG : %d (%d/%d)\n",
7955 			pic_number, rdata32/blk88_y_count,
7956 			rdata32, blk88_y_count);
7957 
7958 		frame->avg_qp = rdata32/blk88_y_count;
7959 		/* intra_y_count */
7960 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7961 
7962 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7963 			"[Picture %d Quality] Y intra rate : %d%c (%d)\n",
7964 			pic_number, rdata32*100/blk88_y_count,
7965 			'%', rdata32);
7966 
7967 		/* skipped_y_count */
7968 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7969 
7970 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7971 			"[Picture %d Quality] Y skipped rate : %d%c (%d)\n",
7972 			pic_number, rdata32*100/blk88_y_count,
7973 			'%', rdata32);
7974 
7975 		frame->avg_skip = rdata32*100/blk88_y_count;
7976 		/* coeff_non_zero_y_count */
7977 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7978 
7979 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7980 			"[Picture %d Quality] Y ZERO_Coeff rate : %d%c (%d)\n",
7981 			pic_number, (100 - rdata32*100/(blk88_y_count*1)),
7982 			'%', rdata32);
7983 
7984 		/* blk66_c_count */
7985 		blk88_c_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
7986 		if (blk88_c_count == 0) {
7987 			vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7988 				"[Picture %d Quality] NO Data yet.\n",
7989 				pic_number);
7990 			/* reset all counts */
7991 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
7992 			return;
7993 		}
7994 		/* qp_c_sum */
7995 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7996 
7997 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
7998 				"[Picture %d Quality] C QP AVG : %d (%d/%d)\n",
7999 				pic_number, rdata32/blk88_c_count,
8000 				rdata32, blk88_c_count);
8001 
8002 		/* intra_c_count */
8003 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8004 
8005 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8006 			"[Picture %d Quality] C intra rate : %d%c (%d)\n",
8007 			pic_number, rdata32*100/blk88_c_count,
8008 			'%', rdata32);
8009 
8010 		/* skipped_cu_c_count */
8011 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8012 
8013 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8014 			"[Picture %d Quality] C skipped rate : %d%c (%d)\n",
8015 			pic_number, rdata32*100/blk88_c_count,
8016 			'%', rdata32);
8017 
8018 		/* coeff_non_zero_c_count */
8019 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8020 
8021 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8022 			"[Picture %d Quality] C ZERO_Coeff rate : %d%c (%d)\n",
8023 			pic_number, (100 - rdata32*100/(blk88_c_count*1)),
8024 			'%', rdata32);
8025 
8026 		/* 1'h0, qp_c_max[6:0], 1'h0, qp_c_min[6:0],
8027 		1'h0, qp_y_max[6:0], 1'h0, qp_y_min[6:0] */
8028 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8029 
8030 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8031 			"[Picture %d Quality] Y QP min : %d\n",
8032 			pic_number, (rdata32>>0)&0xff);
8033 
8034 		frame->min_qp = (rdata32>>0)&0xff;
8035 
8036 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8037 			"[Picture %d Quality] Y QP max : %d\n",
8038 			pic_number, (rdata32>>8)&0xff);
8039 
8040 		frame->max_qp = (rdata32>>8)&0xff;
8041 
8042 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8043 			"[Picture %d Quality] C QP min : %d\n",
8044 			pic_number, (rdata32>>16)&0xff);
8045 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8046 			"[Picture %d Quality] C QP max : %d\n",
8047 			pic_number, (rdata32>>24)&0xff);
8048 
8049 		/* blk22_mv_count */
8050 		blk22_mv_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
8051 		if (blk22_mv_count == 0) {
8052 			vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8053 				"[Picture %d Quality] NO MV Data yet.\n",
8054 				pic_number);
8055 			/* reset all counts */
8056 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
8057 			return;
8058 		}
8059 		/* mvy_L1_count[39:32], mvx_L1_count[39:32],
8060 		mvy_L0_count[39:32], mvx_L0_count[39:32] */
8061 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8062 		/* should all be 0x00 or 0xff */
8063 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8064 			"[Picture %d Quality] MV AVG High Bits: 0x%X\n",
8065 			pic_number, rdata32);
8066 
8067 		mvx_L0_hi = ((rdata32>>0)&0xff);
8068 		mvy_L0_hi = ((rdata32>>8)&0xff);
8069 		mvx_L1_hi = ((rdata32>>16)&0xff);
8070 		mvy_L1_hi = ((rdata32>>24)&0xff);
8071 
8072 		/* mvx_L0_count[31:0] */
8073 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
8074 		temp_value = mvx_L0_hi;
8075 		temp_value = (temp_value << 32) | rdata32_l;
8076 
8077 		if (mvx_L0_hi & 0x80)
8078 			value = 0xFFFFFFF000000000 | temp_value;
8079 		else
8080 			value = temp_value;
8081 
8082 		value = div_s64(value, blk22_mv_count);
8083 
8084 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8085 			"[Picture %d Quality] MVX_L0 AVG : %d (%lld/%d)\n",
8086 			pic_number, (int)value,
8087 			value, blk22_mv_count);
8088 
8089 		frame->avg_mv = value;
8090 
8091 		/* mvy_L0_count[31:0] */
8092 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
8093 		temp_value = mvy_L0_hi;
8094 		temp_value = (temp_value << 32) | rdata32_l;
8095 
8096 		if (mvy_L0_hi & 0x80)
8097 			value = 0xFFFFFFF000000000 | temp_value;
8098 		else
8099 			value = temp_value;
8100 
8101 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8102 			"[Picture %d Quality] MVY_L0 AVG : %d (%lld/%d)\n",
8103 			pic_number, rdata32_l/blk22_mv_count,
8104 			value, blk22_mv_count);
8105 
8106 		/* mvx_L1_count[31:0] */
8107 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
8108 		temp_value = mvx_L1_hi;
8109 		temp_value = (temp_value << 32) | rdata32_l;
8110 		if (mvx_L1_hi & 0x80)
8111 			value = 0xFFFFFFF000000000 | temp_value;
8112 		else
8113 			value = temp_value;
8114 
8115 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8116 			"[Picture %d Quality] MVX_L1 AVG : %d (%lld/%d)\n",
8117 			pic_number, rdata32_l/blk22_mv_count,
8118 			value, blk22_mv_count);
8119 
8120 		/* mvy_L1_count[31:0] */
8121 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
8122 		temp_value = mvy_L1_hi;
8123 		temp_value = (temp_value << 32) | rdata32_l;
8124 		if (mvy_L1_hi & 0x80)
8125 			value = 0xFFFFFFF000000000 | temp_value;
8126 		else
8127 			value = temp_value;
8128 
8129 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8130 			"[Picture %d Quality] MVY_L1 AVG : %d (%lld/%d)\n",
8131 			pic_number, rdata32_l/blk22_mv_count,
8132 			value, blk22_mv_count);
8133 
8134 		/* {mvx_L0_max, mvx_L0_min} // format : {sign, abs[14:0]}  */
8135 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8136 		mv_hi = (rdata32>>16)&0xffff;
8137 		if (mv_hi & 0x8000)
8138 			mv_hi = 0x8000 - mv_hi;
8139 
8140 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8141 			"[Picture %d Quality] MVX_L0 MAX : %d\n",
8142 			pic_number, mv_hi);
8143 
8144 		frame->max_mv = mv_hi;
8145 
8146 		mv_lo = (rdata32>>0)&0xffff;
8147 		if (mv_lo & 0x8000)
8148 			mv_lo = 0x8000 - mv_lo;
8149 
8150 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8151 			"[Picture %d Quality] MVX_L0 MIN : %d\n",
8152 			pic_number, mv_lo);
8153 
8154 		frame->min_mv = mv_lo;
8155 
8156 		/* {mvy_L0_max, mvy_L0_min} */
8157 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8158 		mv_hi = (rdata32>>16)&0xffff;
8159 		if (mv_hi & 0x8000)
8160 			mv_hi = 0x8000 - mv_hi;
8161 
8162 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8163 			"[Picture %d Quality] MVY_L0 MAX : %d\n",
8164 			pic_number, mv_hi);
8165 
8166 		mv_lo = (rdata32>>0)&0xffff;
8167 		if (mv_lo & 0x8000)
8168 			mv_lo = 0x8000 - mv_lo;
8169 
8170 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8171 			"[Picture %d Quality] MVY_L0 MIN : %d\n",
8172 			pic_number, mv_lo);
8173 
8174 		/* {mvx_L1_max, mvx_L1_min} */
8175 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8176 		mv_hi = (rdata32>>16)&0xffff;
8177 		if (mv_hi & 0x8000)
8178 			mv_hi = 0x8000 - mv_hi;
8179 
8180 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8181 			"[Picture %d Quality] MVX_L1 MAX : %d\n",
8182 			pic_number, mv_hi);
8183 
8184 		mv_lo = (rdata32>>0)&0xffff;
8185 		if (mv_lo & 0x8000)
8186 			mv_lo = 0x8000 - mv_lo;
8187 
8188 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8189 			"[Picture %d Quality] MVX_L1 MIN : %d\n",
8190 			pic_number, mv_lo);
8191 
8192 		/* {mvy_L1_max, mvy_L1_min} */
8193 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
8194 		mv_hi = (rdata32>>16)&0xffff;
8195 		if (mv_hi & 0x8000)
8196 			mv_hi = 0x8000 - mv_hi;
8197 
8198 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8199 			"[Picture %d Quality] MVY_L1 MAX : %d\n",
8200 			pic_number, mv_hi);
8201 
8202 		mv_lo = (rdata32>>0)&0xffff;
8203 		if (mv_lo & 0x8000)
8204 			mv_lo = 0x8000 - mv_lo;
8205 
8206 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8207 			"[Picture %d Quality] MVY_L1 MIN : %d\n",
8208 			pic_number, mv_lo);
8209 
8210 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_CTRL);
8211 
8212 		vp9_print(pbi, VP9_DEBUG_QOS_INFO,
8213 			"[Picture %d Quality] After Read : VDEC_PIC_QUALITY_CTRL : 0x%x\n",
8214 			pic_number, rdata32);
8215 
8216 		/* reset all counts */
8217 		WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
8218 	}
8219 }
8220 
vvp9_get_ps_info(struct VP9Decoder_s * pbi,struct aml_vdec_ps_infos * ps)8221 static int vvp9_get_ps_info(struct VP9Decoder_s *pbi, struct aml_vdec_ps_infos *ps)
8222 {
8223 	int dw_mode = v4l_parser_get_double_write_mode(pbi);
8224 
8225 	ps->visible_width 	= pbi->frame_width / get_double_write_ratio(pbi, dw_mode);
8226 	ps->visible_height 	= pbi->frame_height / get_double_write_ratio(pbi, dw_mode);
8227 	ps->coded_width 	= ALIGN(pbi->frame_width, 32) / get_double_write_ratio(pbi, dw_mode);
8228 	ps->coded_height 	= ALIGN(pbi->frame_height, 32) / get_double_write_ratio(pbi, dw_mode);
8229 	ps->dpb_size 		= pbi->used_buf_num;
8230 
8231 	return 0;
8232 }
8233 
8234 
v4l_res_change(struct VP9Decoder_s * pbi)8235 static int v4l_res_change(struct VP9Decoder_s *pbi)
8236 {
8237 	struct aml_vcodec_ctx *ctx =
8238 		(struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
8239 	struct VP9_Common_s *const cm = &pbi->common;
8240 	int ret = 0;
8241 
8242 	if (ctx->param_sets_from_ucode &&
8243 		pbi->res_ch_flag == 0) {
8244 		struct aml_vdec_ps_infos ps;
8245 		if ((cm->width != 0 &&
8246 			cm->height != 0) &&
8247 			(pbi->frame_width != cm->width ||
8248 			pbi->frame_height != cm->height)) {
8249 
8250 			vp9_print(pbi, 0, "%s (%d,%d)=>(%d,%d)\r\n", __func__, cm->width,
8251 				cm->height, pbi->frame_width, pbi->frame_height);
8252 			vvp9_get_ps_info(pbi, &ps);
8253 			vdec_v4l_set_ps_infos(ctx, &ps);
8254 			vdec_v4l_res_ch_event(ctx);
8255 			pbi->v4l_params_parsed = false;
8256 			pbi->res_ch_flag = 1;
8257 			pbi->eos = 1;
8258 			vp9_bufmgr_postproc(pbi);
8259 			//del_timer_sync(&pbi->timer);
8260 			notify_v4l_eos(hw_to_vdec(pbi));
8261 			ret = 1;
8262 		}
8263 	}
8264 
8265 	return ret;
8266 }
8267 
8268 
vvp9_isr_thread_fn(int irq,void * data)8269 static irqreturn_t vvp9_isr_thread_fn(int irq, void *data)
8270 {
8271 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)data;
8272 	unsigned int dec_status = pbi->dec_status;
8273 	int i;
8274 
8275 	/*if (pbi->wait_buf)
8276 	 *	pr_info("set wait_buf to 0\r\n");
8277 	 */
8278 	if (pbi->eos)
8279 		return IRQ_HANDLED;
8280 	pbi->wait_buf = 0;
8281 #ifdef MULTI_INSTANCE_SUPPORT
8282 #ifdef SUPPORT_FB_DECODING
8283 #ifdef FB_DECODING_TEST_SCHEDULE
8284 	if (pbi->s1_test_cmd == TEST_SET_PIC_DONE)
8285 		dec_status = HEVC_DECPIC_DATA_DONE;
8286 	else if (pbi->s1_test_cmd == TEST_SET_S2_DONE
8287 		&& dec_status == HEVC_DECPIC_DATA_DONE)
8288 		dec_status = HEVC_S2_DECODING_DONE;
8289 	pbi->s1_test_cmd = TEST_SET_NONE;
8290 #else
8291 	/*if (irq != VDEC_IRQ_0)
8292 		dec_status = HEVC_S2_DECODING_DONE;*/
8293 #endif
8294 	if (dec_status == HEVC_S2_DECODING_DONE) {
8295 		pbi->dec_result = DEC_RESULT_DONE;
8296 		vdec_schedule_work(&pbi->work);
8297 #ifdef FB_DECODING_TEST_SCHEDULE
8298 		amhevc_stop();
8299 		pbi->dec_s1_result = DEC_S1_RESULT_DONE;
8300 		vdec_schedule_work(&pbi->s1_work);
8301 #endif
8302 	} else
8303 #endif
8304 	if ((dec_status == HEVC_NAL_DECODE_DONE) ||
8305 			(dec_status == HEVC_SEARCH_BUFEMPTY) ||
8306 			(dec_status == HEVC_DECODE_BUFEMPTY)
8307 		) {
8308 		if (pbi->m_ins_flag) {
8309 			reset_process_time(pbi);
8310 			if (!vdec_frame_based(hw_to_vdec(pbi)))
8311 				dec_again_process(pbi);
8312 			else {
8313 				pbi->dec_result = DEC_RESULT_GET_DATA;
8314 				vdec_schedule_work(&pbi->work);
8315 			}
8316 		}
8317 		pbi->process_busy = 0;
8318 		return IRQ_HANDLED;
8319 	} else if (dec_status == HEVC_DECPIC_DATA_DONE) {
8320 		if (pbi->m_ins_flag) {
8321 			get_picture_qos_info(pbi);
8322 #ifdef SUPPORT_FB_DECODING
8323 			if (pbi->used_stage_buf_num > 0) {
8324 				reset_process_time(pbi);
8325 				inc_s1_pos(pbi);
8326 				trigger_schedule(pbi);
8327 #ifdef FB_DECODING_TEST_SCHEDULE
8328 				pbi->s1_test_cmd = TEST_SET_S2_DONE;
8329 #else
8330 				amhevc_stop();
8331 				pbi->dec_s1_result = DEC_S1_RESULT_DONE;
8332 				vdec_schedule_work(&pbi->s1_work);
8333 #endif
8334 			} else
8335 #endif
8336 			{
8337 				reset_process_time(pbi);
8338 				if (pbi->vf_pre_count == 0 || pbi->low_latency_flag)
8339 					vp9_bufmgr_postproc(pbi);
8340 
8341 				pbi->dec_result = DEC_RESULT_DONE;
8342 				amhevc_stop();
8343 				if (mcrcc_cache_alg_flag)
8344 					dump_hit_rate(pbi);
8345 				vdec_schedule_work(&pbi->work);
8346 			}
8347 		} else {
8348 			if (pbi->low_latency_flag) {
8349 				vp9_bufmgr_postproc(pbi);
8350 				WRITE_VREG(HEVC_DEC_STATUS_REG, HEVC_ACTION_DONE);
8351 #ifdef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
8352 				vdec_profile(hw_to_vdec(pbi), VDEC_PROFILE_EVENT_CB);
8353 				if (debug & PRINT_FLAG_VDEC_DETAIL)
8354 					pr_info("%s VP9 frame done \n", __func__);
8355 #endif
8356 			}
8357 		}
8358 
8359 		pbi->process_busy = 0;
8360 		return IRQ_HANDLED;
8361 	}
8362 #endif
8363 
8364 	if (dec_status == VP9_EOS) {
8365 #ifdef MULTI_INSTANCE_SUPPORT
8366 		if (pbi->m_ins_flag)
8367 			reset_process_time(pbi);
8368 #endif
8369 
8370 		pr_info("VP9_EOS, flush buffer\r\n");
8371 
8372 		vp9_bufmgr_postproc(pbi);
8373 
8374 		pr_info("send VP9_10B_DISCARD_NAL\r\n");
8375 		WRITE_VREG(HEVC_DEC_STATUS_REG, VP9_10B_DISCARD_NAL);
8376 		pbi->process_busy = 0;
8377 #ifdef MULTI_INSTANCE_SUPPORT
8378 		if (pbi->m_ins_flag) {
8379 			pbi->dec_result = DEC_RESULT_DONE;
8380 			amhevc_stop();
8381 			vdec_schedule_work(&pbi->work);
8382 		}
8383 #endif
8384 		return IRQ_HANDLED;
8385 	} else if (dec_status == HEVC_DECODE_OVER_SIZE) {
8386 		pr_info("vp9  decode oversize !!\n");
8387 		debug |= (VP9_DEBUG_DIS_LOC_ERROR_PROC |
8388 			VP9_DEBUG_DIS_SYS_ERROR_PROC);
8389 		pbi->fatal_error |= DECODER_FATAL_ERROR_SIZE_OVERFLOW;
8390 #ifdef MULTI_INSTANCE_SUPPORT
8391 	if (pbi->m_ins_flag)
8392 		reset_process_time(pbi);
8393 #endif
8394 		return IRQ_HANDLED;
8395 	}
8396 
8397 	if (dec_status != VP9_HEAD_PARSER_DONE) {
8398 		pbi->process_busy = 0;
8399 		return IRQ_HANDLED;
8400 	}
8401 
8402 
8403 #ifdef MULTI_INSTANCE_SUPPORT
8404 #ifdef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
8405 	if (pbi->m_ins_flag ==0 && pbi->low_latency_flag) {
8406 		vdec_profile(hw_to_vdec(pbi), VDEC_PROFILE_EVENT_RUN);
8407 		if (debug & PRINT_FLAG_VDEC_DETAIL)
8408 			pr_info("%s VP9 frame header found \n", __func__);
8409 	}
8410 #endif
8411 	if (pbi->m_ins_flag)
8412 		reset_process_time(pbi);
8413 #endif
8414 	if (pbi->process_state != PROC_STATE_SENDAGAIN
8415 #ifdef SUPPORT_FB_DECODING
8416 		&& pbi->used_stage_buf_num == 0
8417 #endif
8418 		) {
8419 		if (pbi->mmu_enable)
8420 			vp9_recycle_mmu_buf_tail(pbi);
8421 
8422 
8423 		if (pbi->frame_count > 0)
8424 			vp9_bufmgr_postproc(pbi);
8425 	}
8426 
8427 	if (debug & VP9_DEBUG_SEND_PARAM_WITH_REG) {
8428 		get_rpm_param(&pbi->vp9_param);
8429 	} else {
8430 #ifdef SUPPORT_FB_DECODING
8431 		if (pbi->used_stage_buf_num > 0) {
8432 			reset_process_time(pbi);
8433 			get_s1_buf(pbi);
8434 
8435 			if (get_mv_buf(pbi,
8436 				&pbi->s1_mv_buf_index,
8437 				&pbi->s1_mpred_mv_wr_start_addr
8438 				) < 0) {
8439 				vp9_print(pbi, 0,
8440 					"%s: Error get_mv_buf fail\n",
8441 					__func__);
8442 			}
8443 
8444 			if (pbi->s1_buf == NULL) {
8445 				vp9_print(pbi, 0,
8446 					"%s: Error get_s1_buf fail\n",
8447 					__func__);
8448 				pbi->process_busy = 0;
8449 				return IRQ_HANDLED;
8450 			}
8451 
8452 			for (i = 0; i < (RPM_END - RPM_BEGIN); i += 4) {
8453 				int ii;
8454 				for (ii = 0; ii < 4; ii++) {
8455 					pbi->s1_buf->rpm[i + 3 - ii] =
8456 						pbi->rpm_ptr[i + 3 - ii];
8457 					pbi->s1_param.l.data[i + ii] =
8458 						pbi->rpm_ptr[i + 3 - ii];
8459 				}
8460 			}
8461 
8462 			mpred_process(pbi);
8463 #ifdef FB_DECODING_TEST_SCHEDULE
8464 			pbi->dec_s1_result =
8465 				DEC_S1_RESULT_TEST_TRIGGER_DONE;
8466 			vdec_schedule_work(&pbi->s1_work);
8467 #else
8468 			WRITE_VREG(HEVC_ASSIST_FB_MMU_MAP_ADDR,
8469 				pbi->stage_mmu_map_phy_addr +
8470 				pbi->s1_buf->index * STAGE_MMU_MAP_SIZE);
8471 
8472 			start_s1_decoding(pbi);
8473 #endif
8474 			start_process_time(pbi);
8475 			pbi->process_busy = 0;
8476 			return IRQ_HANDLED;
8477 		} else
8478 #endif
8479 		{
8480 			for (i = 0; i < (RPM_END - RPM_BEGIN); i += 4) {
8481 				int ii;
8482 				for (ii = 0; ii < 4; ii++)
8483 					pbi->vp9_param.l.data[i + ii] =
8484 						pbi->rpm_ptr[i + 3 - ii];
8485 			}
8486 		}
8487 	}
8488 
8489 	if (pbi->is_used_v4l) {
8490 		struct aml_vcodec_ctx *ctx =
8491 			(struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
8492 
8493 		pbi->frame_width = pbi->vp9_param.p.width;
8494 		pbi->frame_height = pbi->vp9_param.p.height;
8495 
8496 		if (!v4l_res_change(pbi)) {
8497 			if (ctx->param_sets_from_ucode && !pbi->v4l_params_parsed) {
8498 				struct aml_vdec_ps_infos ps;
8499 
8500 				pr_debug("set ucode parse\n");
8501 				vvp9_get_ps_info(pbi, &ps);
8502 				/*notice the v4l2 codec.*/
8503 				vdec_v4l_set_ps_infos(ctx, &ps);
8504 				pbi->v4l_params_parsed	= true;
8505 				pbi->postproc_done = 0;
8506 				pbi->process_busy = 0;
8507 				dec_again_process(pbi);
8508 				return IRQ_HANDLED;
8509 			}
8510 		} else {
8511 			pbi->postproc_done = 0;
8512 			pbi->process_busy = 0;
8513 			dec_again_process(pbi);
8514 			return IRQ_HANDLED;
8515 		}
8516 	}
8517 
8518 	continue_decoding(pbi);
8519 	pbi->postproc_done = 0;
8520 	pbi->process_busy = 0;
8521 
8522 #ifdef MULTI_INSTANCE_SUPPORT
8523 	if (pbi->m_ins_flag)
8524 		start_process_time(pbi);
8525 #endif
8526 
8527 	return IRQ_HANDLED;
8528 }
8529 
vvp9_isr(int irq,void * data)8530 static irqreturn_t vvp9_isr(int irq, void *data)
8531 {
8532 	int i;
8533 	unsigned int dec_status;
8534 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)data;
8535 	unsigned int adapt_prob_status;
8536 	struct VP9_Common_s *const cm = &pbi->common;
8537 	uint debug_tag;
8538 
8539 	WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
8540 
8541 	dec_status = READ_VREG(HEVC_DEC_STATUS_REG);
8542 	adapt_prob_status = READ_VREG(VP9_ADAPT_PROB_REG);
8543 	if (!pbi)
8544 		return IRQ_HANDLED;
8545 	if (pbi->init_flag == 0)
8546 		return IRQ_HANDLED;
8547 	if (pbi->process_busy)/*on process.*/
8548 		return IRQ_HANDLED;
8549 	pbi->dec_status = dec_status;
8550 	pbi->process_busy = 1;
8551 	if (debug & VP9_DEBUG_BUFMGR)
8552 		pr_info("vp9 isr (%d) dec status  = 0x%x, lcu 0x%x shiftbyte 0x%x (%x %x lev %x, wr %x, rd %x)\n",
8553 			irq,
8554 			dec_status, READ_VREG(HEVC_PARSER_LCU_START),
8555 			READ_VREG(HEVC_SHIFT_BYTE_COUNT),
8556 			READ_VREG(HEVC_STREAM_START_ADDR),
8557 			READ_VREG(HEVC_STREAM_END_ADDR),
8558 			READ_VREG(HEVC_STREAM_LEVEL),
8559 			READ_VREG(HEVC_STREAM_WR_PTR),
8560 			READ_VREG(HEVC_STREAM_RD_PTR)
8561 		);
8562 #ifdef SUPPORT_FB_DECODING
8563 	/*if (irq != VDEC_IRQ_0)
8564 		return IRQ_WAKE_THREAD;*/
8565 #endif
8566 
8567 	debug_tag = READ_HREG(DEBUG_REG1);
8568 	if (debug_tag & 0x10000) {
8569 		pr_info("LMEM<tag %x>:\n", READ_HREG(DEBUG_REG1));
8570 		for (i = 0; i < 0x400; i += 4) {
8571 			int ii;
8572 			if ((i & 0xf) == 0)
8573 				pr_info("%03x: ", i);
8574 			for (ii = 0; ii < 4; ii++) {
8575 				pr_info("%04x ",
8576 					   pbi->lmem_ptr[i + 3 - ii]);
8577 			}
8578 			if (((i + ii) & 0xf) == 0)
8579 				pr_info("\n");
8580 		}
8581 
8582 		if ((udebug_pause_pos == (debug_tag & 0xffff)) &&
8583 			(udebug_pause_decode_idx == 0 ||
8584 			udebug_pause_decode_idx == pbi->slice_idx) &&
8585 			(udebug_pause_val == 0 ||
8586 			udebug_pause_val == READ_HREG(DEBUG_REG2)))
8587 			pbi->ucode_pause_pos = udebug_pause_pos;
8588 		else if (debug_tag & 0x20000)
8589 			pbi->ucode_pause_pos = 0xffffffff;
8590 		if (pbi->ucode_pause_pos)
8591 			reset_process_time(pbi);
8592 		else
8593 			WRITE_HREG(DEBUG_REG1, 0);
8594 	} else if (debug_tag != 0) {
8595 		pr_info(
8596 			"dbg%x: %x lcu %x\n", READ_HREG(DEBUG_REG1),
8597 			   READ_HREG(DEBUG_REG2),
8598 			   READ_VREG(HEVC_PARSER_LCU_START));
8599 		if ((udebug_pause_pos == (debug_tag & 0xffff)) &&
8600 			(udebug_pause_decode_idx == 0 ||
8601 			udebug_pause_decode_idx == pbi->slice_idx) &&
8602 			(udebug_pause_val == 0 ||
8603 			udebug_pause_val == READ_HREG(DEBUG_REG2)))
8604 			pbi->ucode_pause_pos = udebug_pause_pos;
8605 		if (pbi->ucode_pause_pos)
8606 			reset_process_time(pbi);
8607 		else
8608 			WRITE_HREG(DEBUG_REG1, 0);
8609 		pbi->process_busy = 0;
8610 		return IRQ_HANDLED;
8611 	}
8612 
8613 #ifdef MULTI_INSTANCE_SUPPORT
8614 	if (!pbi->m_ins_flag) {
8615 #endif
8616 		if (pbi->error_flag == 1) {
8617 			pbi->error_flag = 2;
8618 			pbi->process_busy = 0;
8619 			return IRQ_HANDLED;
8620 		} else if (pbi->error_flag == 3) {
8621 			pbi->process_busy = 0;
8622 			return IRQ_HANDLED;
8623 		}
8624 
8625 		if (get_free_buf_count(pbi) <= 0) {
8626 			/*
8627 			if (pbi->wait_buf == 0)
8628 				pr_info("set wait_buf to 1\r\n");
8629 			*/
8630 			pbi->wait_buf = 1;
8631 			pbi->process_busy = 0;
8632 			return IRQ_HANDLED;
8633 		}
8634 #ifdef MULTI_INSTANCE_SUPPORT
8635 	}
8636 #endif
8637 	if ((adapt_prob_status & 0xff) == 0xfd) {
8638 		/*VP9_REQ_ADAPT_PROB*/
8639 		int pre_fc = (cm->frame_type == KEY_FRAME) ? 1 : 0;
8640 		uint8_t *prev_prob_b =
8641 		((uint8_t *)pbi->prob_buffer_addr) +
8642 		((adapt_prob_status >> 8) * 0x1000);
8643 		uint8_t *cur_prob_b =
8644 		((uint8_t *)pbi->prob_buffer_addr) + 0x4000;
8645 		uint8_t *count_b = (uint8_t *)pbi->count_buffer_addr;
8646 #ifdef MULTI_INSTANCE_SUPPORT
8647 		if (pbi->m_ins_flag)
8648 			reset_process_time(pbi);
8649 #endif
8650 		adapt_coef_probs(pbi->pic_count,
8651 			(cm->last_frame_type == KEY_FRAME),
8652 			pre_fc, (adapt_prob_status >> 8),
8653 			(unsigned int *)prev_prob_b,
8654 			(unsigned int *)cur_prob_b, (unsigned int *)count_b);
8655 
8656 		memcpy(prev_prob_b, cur_prob_b, PROB_SIZE);
8657 		WRITE_VREG(VP9_ADAPT_PROB_REG, 0);
8658 		pbi->pic_count += 1;
8659 #ifdef MULTI_INSTANCE_SUPPORT
8660 		if (pbi->m_ins_flag)
8661 			start_process_time(pbi);
8662 #endif
8663 
8664 		/*return IRQ_HANDLED;*/
8665 	}
8666 	return IRQ_WAKE_THREAD;
8667 }
8668 
vp9_set_clk(struct work_struct * work)8669 static void vp9_set_clk(struct work_struct *work)
8670 {
8671 	struct VP9Decoder_s *pbi = container_of(work,
8672 		struct VP9Decoder_s, set_clk_work);
8673 	int fps = 96000 / pbi->frame_dur;
8674 
8675 	if (hevc_source_changed(VFORMAT_VP9,
8676 		frame_width, frame_height, fps) > 0)
8677 		pbi->saved_resolution = frame_width *
8678 		frame_height * fps;
8679 }
8680 
vvp9_put_timer_func(struct timer_list * timer)8681 static void vvp9_put_timer_func(struct timer_list *timer)
8682 {
8683 	struct VP9Decoder_s *pbi = container_of(timer, struct VP9Decoder_s, timer);
8684 	uint8_t empty_flag;
8685 	unsigned int buf_level;
8686 
8687 	enum receviver_start_e state = RECEIVER_INACTIVE;
8688 
8689 	if (pbi->m_ins_flag) {
8690 		if (hw_to_vdec(pbi)->next_status
8691 			== VDEC_STATUS_DISCONNECTED &&
8692 			!pbi->is_used_v4l) {
8693 #ifdef SUPPORT_FB_DECODING
8694 			if (pbi->run2_busy)
8695 				return;
8696 
8697 			pbi->dec_s1_result = DEC_S1_RESULT_FORCE_EXIT;
8698 			vdec_schedule_work(&pbi->s1_work);
8699 #endif
8700 			pbi->dec_result = DEC_RESULT_FORCE_EXIT;
8701 			vdec_schedule_work(&pbi->work);
8702 			pr_debug(
8703 			"vdec requested to be disconnected\n");
8704 			return;
8705 		}
8706 	}
8707 	if (pbi->init_flag == 0) {
8708 		if (pbi->stat & STAT_TIMER_ARM) {
8709 			timer->expires = jiffies + PUT_INTERVAL;
8710 			add_timer(&pbi->timer);
8711 		}
8712 		return;
8713 	}
8714 	if (pbi->m_ins_flag == 0) {
8715 		if (vf_get_receiver(pbi->provider_name)) {
8716 			state =
8717 				vf_notify_receiver(pbi->provider_name,
8718 					VFRAME_EVENT_PROVIDER_QUREY_STATE,
8719 					NULL);
8720 			if ((state == RECEIVER_STATE_NULL)
8721 				|| (state == RECEIVER_STATE_NONE))
8722 				state = RECEIVER_INACTIVE;
8723 		} else
8724 			state = RECEIVER_INACTIVE;
8725 
8726 		empty_flag = (READ_VREG(HEVC_PARSER_INT_STATUS) >> 6) & 0x1;
8727 		/* error watchdog */
8728 		if (empty_flag == 0) {
8729 			/* decoder has input */
8730 			if ((debug & VP9_DEBUG_DIS_LOC_ERROR_PROC) == 0) {
8731 
8732 				buf_level = READ_VREG(HEVC_STREAM_LEVEL);
8733 				/* receiver has no buffer to recycle */
8734 				if ((state == RECEIVER_INACTIVE) &&
8735 					(kfifo_is_empty(&pbi->display_q) &&
8736 					 buf_level > 0x200)
8737 					) {
8738 						WRITE_VREG
8739 						(HEVC_ASSIST_MBOX0_IRQ_REG,
8740 						 0x1);
8741 				}
8742 			}
8743 
8744 			if ((debug & VP9_DEBUG_DIS_SYS_ERROR_PROC) == 0) {
8745 				/* receiver has no buffer to recycle */
8746 				/*if ((state == RECEIVER_INACTIVE) &&
8747 				 *	(kfifo_is_empty(&pbi->display_q))) {
8748 				 *pr_info("vp9 something error,need reset\n");
8749 				 *}
8750 				 */
8751 			}
8752 		}
8753 	}
8754 #ifdef MULTI_INSTANCE_SUPPORT
8755 	else {
8756 		if (
8757 			(decode_timeout_val > 0) &&
8758 			(pbi->start_process_time > 0) &&
8759 			((1000 * (jiffies - pbi->start_process_time) / HZ)
8760 				> decode_timeout_val)
8761 		) {
8762 			int current_lcu_idx =
8763 				READ_VREG(HEVC_PARSER_LCU_START)
8764 				& 0xffffff;
8765 			if (pbi->last_lcu_idx == current_lcu_idx) {
8766 				if (pbi->decode_timeout_count > 0)
8767 					pbi->decode_timeout_count--;
8768 				if (pbi->decode_timeout_count == 0) {
8769 					if (input_frame_based(
8770 						hw_to_vdec(pbi)) ||
8771 					(READ_VREG(HEVC_STREAM_LEVEL) > 0x200))
8772 						timeout_process(pbi);
8773 					else {
8774 						vp9_print(pbi, 0,
8775 							"timeout & empty, again\n");
8776 						dec_again_process(pbi);
8777 					}
8778 				}
8779 			} else {
8780 				start_process_time(pbi);
8781 				pbi->last_lcu_idx = current_lcu_idx;
8782 			}
8783 		}
8784 	}
8785 #endif
8786 
8787 	if ((pbi->ucode_pause_pos != 0) &&
8788 		(pbi->ucode_pause_pos != 0xffffffff) &&
8789 		udebug_pause_pos != pbi->ucode_pause_pos) {
8790 		pbi->ucode_pause_pos = 0;
8791 		WRITE_HREG(DEBUG_REG1, 0);
8792 	}
8793 #ifdef MULTI_INSTANCE_SUPPORT
8794 	if (debug & VP9_DEBUG_FORCE_SEND_AGAIN) {
8795 		pr_info(
8796 		"Force Send Again\r\n");
8797 		debug &= ~VP9_DEBUG_FORCE_SEND_AGAIN;
8798 		reset_process_time(pbi);
8799 		pbi->dec_result = DEC_RESULT_AGAIN;
8800 		if (pbi->process_state ==
8801 			PROC_STATE_DECODESLICE) {
8802 			if (pbi->mmu_enable)
8803 				vp9_recycle_mmu_buf(pbi);
8804 			pbi->process_state =
8805 			PROC_STATE_SENDAGAIN;
8806 		}
8807 		amhevc_stop();
8808 
8809 		vdec_schedule_work(&pbi->work);
8810 	}
8811 
8812 	if (debug & VP9_DEBUG_DUMP_DATA) {
8813 		debug &= ~VP9_DEBUG_DUMP_DATA;
8814 		vp9_print(pbi, 0,
8815 			"%s: chunk size 0x%x off 0x%x sum 0x%x\n",
8816 			__func__,
8817 			pbi->chunk->size,
8818 			pbi->chunk->offset,
8819 			get_data_check_sum(pbi, pbi->chunk->size)
8820 			);
8821 		dump_data(pbi, pbi->chunk->size);
8822 	}
8823 #endif
8824 	if (debug & VP9_DEBUG_DUMP_PIC_LIST) {
8825 		dump_pic_list(pbi);
8826 		debug &= ~VP9_DEBUG_DUMP_PIC_LIST;
8827 	}
8828 	if (debug & VP9_DEBUG_TRIG_SLICE_SEGMENT_PROC) {
8829 		WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG, 0x1);
8830 		debug &= ~VP9_DEBUG_TRIG_SLICE_SEGMENT_PROC;
8831 	}
8832 	/*if (debug & VP9_DEBUG_HW_RESET) {
8833 	}*/
8834 
8835 	if (radr != 0) {
8836 		if (rval != 0) {
8837 			WRITE_VREG(radr, rval);
8838 			pr_info("WRITE_VREG(%x,%x)\n", radr, rval);
8839 		} else
8840 			pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr));
8841 		rval = 0;
8842 		radr = 0;
8843 	}
8844 	if (pop_shorts != 0) {
8845 		int i;
8846 		u32 sum = 0;
8847 
8848 		pr_info("pop stream 0x%x shorts\r\n", pop_shorts);
8849 		for (i = 0; i < pop_shorts; i++) {
8850 			u32 data =
8851 			(READ_HREG(HEVC_SHIFTED_DATA) >> 16);
8852 			WRITE_HREG(HEVC_SHIFT_COMMAND,
8853 			(1<<7)|16);
8854 			if ((i & 0xf) == 0)
8855 				pr_info("%04x:", i);
8856 			pr_info("%04x ", data);
8857 			if (((i + 1) & 0xf) == 0)
8858 				pr_info("\r\n");
8859 			sum += data;
8860 		}
8861 		pr_info("\r\nsum = %x\r\n", sum);
8862 		pop_shorts = 0;
8863 	}
8864 	if (dbg_cmd != 0) {
8865 		if (dbg_cmd == 1) {
8866 			u32 disp_laddr;
8867 
8868 			if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_GXBB &&
8869 				get_double_write_mode(pbi) == 0) {
8870 				disp_laddr =
8871 					READ_VCBUS_REG(AFBC_BODY_BADDR) << 4;
8872 			} else {
8873 				struct canvas_s cur_canvas;
8874 
8875 				canvas_read((READ_VCBUS_REG(VD1_IF0_CANVAS0)
8876 					& 0xff), &cur_canvas);
8877 				disp_laddr = cur_canvas.addr;
8878 			}
8879 			pr_info("current displayed buffer address %x\r\n",
8880 				disp_laddr);
8881 		}
8882 		dbg_cmd = 0;
8883 	}
8884 	/*don't changed at start.*/
8885 	if (pbi->get_frame_dur && pbi->show_frame_num > 60 &&
8886 		pbi->frame_dur > 0 && pbi->saved_resolution !=
8887 		frame_width * frame_height *
8888 			(96000 / pbi->frame_dur))
8889 		vdec_schedule_work(&pbi->set_clk_work);
8890 
8891 	timer->expires = jiffies + PUT_INTERVAL;
8892 	add_timer(timer);
8893 }
8894 
8895 
vvp9_dec_status(struct vdec_s * vdec,struct vdec_info * vstatus)8896 int vvp9_dec_status(struct vdec_s *vdec, struct vdec_info *vstatus)
8897 {
8898 	struct VP9Decoder_s *vp9 =
8899 		(struct VP9Decoder_s *)vdec->private;
8900 
8901 	if (!vp9)
8902 		return -1;
8903 
8904 	vstatus->frame_width = frame_width;
8905 	vstatus->frame_height = frame_height;
8906 	if (vp9->frame_dur != 0)
8907 		vstatus->frame_rate = 96000 / vp9->frame_dur;
8908 	else
8909 		vstatus->frame_rate = -1;
8910 	vstatus->error_count = 0;
8911 	vstatus->status = vp9->stat | vp9->fatal_error;
8912 	vstatus->frame_dur = vp9->frame_dur;
8913 	vstatus->bit_rate = vp9->gvs->bit_rate;
8914 	vstatus->frame_data = vp9->gvs->frame_data;
8915 	vstatus->total_data = vp9->gvs->total_data;
8916 	vstatus->frame_count = vp9->gvs->frame_count;
8917 	vstatus->error_frame_count = vp9->gvs->error_frame_count;
8918 	vstatus->drop_frame_count = vp9->gvs->drop_frame_count;
8919 	vstatus->total_data = vp9->gvs->total_data;
8920 	vstatus->samp_cnt = vp9->gvs->samp_cnt;
8921 	vstatus->offset = vp9->gvs->offset;
8922 	snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name),
8923 		"%s", DRIVER_NAME);
8924 	return 0;
8925 }
8926 
vvp9_set_isreset(struct vdec_s * vdec,int isreset)8927 int vvp9_set_isreset(struct vdec_s *vdec, int isreset)
8928 {
8929 	is_reset = isreset;
8930 	return 0;
8931 }
8932 
8933 #if 0
8934 static void VP9_DECODE_INIT(void)
8935 {
8936 	/* enable vp9 clocks */
8937 	WRITE_VREG(DOS_GCLK_EN3, 0xffffffff);
8938 	/* *************************************************************** */
8939 	/* Power ON HEVC */
8940 	/* *************************************************************** */
8941 	/* Powerup HEVC */
8942 	WRITE_VREG(AO_RTI_GEN_PWR_SLEEP0,
8943 			READ_VREG(AO_RTI_GEN_PWR_SLEEP0) & (~(0x3 << 6)));
8944 	WRITE_VREG(DOS_MEM_PD_HEVC, 0x0);
8945 	WRITE_VREG(DOS_SW_RESET3, READ_VREG(DOS_SW_RESET3) | (0x3ffff << 2));
8946 	WRITE_VREG(DOS_SW_RESET3, READ_VREG(DOS_SW_RESET3) & (~(0x3ffff << 2)));
8947 	/* remove isolations */
8948 	WRITE_VREG(AO_RTI_GEN_PWR_ISO0,
8949 			READ_VREG(AO_RTI_GEN_PWR_ISO0) & (~(0x3 << 10)));
8950 
8951 }
8952 #endif
8953 
vvp9_prot_init(struct VP9Decoder_s * pbi,u32 mask)8954 static void vvp9_prot_init(struct VP9Decoder_s *pbi, u32 mask)
8955 {
8956 	unsigned int data32;
8957 	/* VP9_DECODE_INIT(); */
8958 	vp9_config_work_space_hw(pbi, mask);
8959 	if (mask & HW_MASK_BACK)
8960 		init_pic_list_hw(pbi);
8961 
8962 	vp9_init_decoder_hw(pbi, mask);
8963 
8964 #ifdef VP9_LPF_LVL_UPDATE
8965 	if (mask & HW_MASK_BACK)
8966 		vp9_loop_filter_init(pbi);
8967 #endif
8968 
8969 	if ((mask & HW_MASK_FRONT) == 0)
8970 		return;
8971 #if 1
8972 	if (debug & VP9_DEBUG_BUFMGR_MORE)
8973 		pr_info("%s\n", __func__);
8974 	data32 = READ_VREG(HEVC_STREAM_CONTROL);
8975 	data32 = data32 |
8976 		(1 << 0)/*stream_fetch_enable*/
8977 		;
8978 	WRITE_VREG(HEVC_STREAM_CONTROL, data32);
8979 
8980 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
8981 	    if (debug & VP9_DEBUG_BUFMGR)
8982 	        pr_info("[test.c] Config STREAM_FIFO_CTL\n");
8983 	    data32 = READ_VREG(HEVC_STREAM_FIFO_CTL);
8984 	    data32 = data32 |
8985 	             (1 << 29) // stream_fifo_hole
8986 	             ;
8987 	    WRITE_VREG(HEVC_STREAM_FIFO_CTL, data32);
8988 	}
8989 #if 0
8990 	data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
8991 	if (data32 != 0x00000100) {
8992 		pr_info("vp9 prot init error %d\n", __LINE__);
8993 		return;
8994 	}
8995 	data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
8996 	if (data32 != 0x00000300) {
8997 		pr_info("vp9 prot init error %d\n", __LINE__);
8998 		return;
8999 	}
9000 	WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x12345678);
9001 	WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x9abcdef0);
9002 	data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
9003 	if (data32 != 0x12345678) {
9004 		pr_info("vp9 prot init error %d\n", __LINE__);
9005 		return;
9006 	}
9007 	data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
9008 	if (data32 != 0x9abcdef0) {
9009 		pr_info("vp9 prot init error %d\n", __LINE__);
9010 		return;
9011 	}
9012 #endif
9013 	WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x000000001);
9014 	WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x00000300);
9015 #endif
9016 
9017 
9018 
9019 	WRITE_VREG(HEVC_WAIT_FLAG, 1);
9020 
9021 	/* WRITE_VREG(HEVC_MPSR, 1); */
9022 
9023 	/* clear mailbox interrupt */
9024 	WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
9025 
9026 	/* enable mailbox interrupt */
9027 	WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 1);
9028 
9029 	/* disable PSCALE for hardware sharing */
9030 	WRITE_VREG(HEVC_PSCALE_CTRL, 0);
9031 
9032 	WRITE_VREG(DEBUG_REG1, 0x0);
9033 	/*check vps/sps/pps/i-slice in ucode*/
9034 	WRITE_VREG(NAL_SEARCH_CTL, 0x8);
9035 
9036 	WRITE_VREG(DECODE_STOP_POS, udebug_flag);
9037 #ifdef SUPPORT_FB_DECODING
9038 #ifndef FB_DECODING_TEST_SCHEDULE
9039 	if (pbi->used_stage_buf_num > 0) {
9040 		if (mask & HW_MASK_FRONT) {
9041 			data32 = READ_VREG(
9042 				HEVC_ASSIST_HED_FB_W_CTL);
9043 			data32 = data32 |
9044 				(1 << 0) /*hed_fb_wr_en*/
9045 				;
9046 			WRITE_VREG(HEVC_ASSIST_HED_FB_W_CTL,
9047 				data32);
9048 		}
9049 		if (mask & HW_MASK_BACK) {
9050 			data32 = READ_VREG(
9051 				HEVC_ASSIST_HED_FB_R_CTL);
9052 			while (data32 & (1 << 7)) {
9053 				/*wait finish*/
9054 				data32 = READ_VREG(
9055 					HEVC_ASSIST_HED_FB_R_CTL);
9056 			}
9057 			data32 &= (~(0x1 << 0));
9058 			/*hed_fb_rd_addr_auto_rd*/
9059 			data32 &= (~(0x1 << 1));
9060 			/*rd_id = 0, hed_rd_map_auto_halt_num,
9061 			after wr 2 ready, then start reading*/
9062 			data32 |= (0x2 << 16);
9063 			WRITE_VREG(HEVC_ASSIST_HED_FB_R_CTL,
9064 				data32);
9065 
9066 			data32 |= (0x1 << 11); /*hed_rd_map_auto_halt_en*/
9067 			data32 |= (0x1 << 1); /*hed_fb_rd_addr_auto_rd*/
9068 			data32 |= (0x1 << 0); /*hed_fb_rd_en*/
9069 			WRITE_VREG(HEVC_ASSIST_HED_FB_R_CTL,
9070 				data32);
9071 		}
9072 
9073 	}
9074 #endif
9075 #endif
9076 }
9077 
vvp9_local_init(struct VP9Decoder_s * pbi)9078 static int vvp9_local_init(struct VP9Decoder_s *pbi)
9079 {
9080 	int i;
9081 	int ret;
9082 	int width, height;
9083 	if (alloc_lf_buf(pbi) < 0)
9084 		return -1;
9085 
9086 	pbi->gvs = vzalloc(sizeof(struct vdec_info));
9087 	if (NULL == pbi->gvs) {
9088 		pr_info("the struct of vdec status malloc failed.\n");
9089 		return -1;
9090 	}
9091 	vdec_set_vframe_comm(hw_to_vdec(pbi), DRIVER_NAME);
9092 #ifdef DEBUG_PTS
9093 	pbi->pts_missed = 0;
9094 	pbi->pts_hit = 0;
9095 #endif
9096 	pbi->new_frame_displayed = 0;
9097 	pbi->last_put_idx = -1;
9098 	pbi->saved_resolution = 0;
9099 	pbi->get_frame_dur = false;
9100 	on_no_keyframe_skiped = 0;
9101 	pbi->duration_from_pts_done = 0;
9102 	pbi->vp9_first_pts_ready = 0;
9103 	pbi->frame_cnt_window = 0;
9104 	width = pbi->vvp9_amstream_dec_info.width;
9105 	height = pbi->vvp9_amstream_dec_info.height;
9106 	pbi->frame_dur =
9107 		(pbi->vvp9_amstream_dec_info.rate ==
9108 		 0) ? 3200 : pbi->vvp9_amstream_dec_info.rate;
9109 	if (width && height)
9110 		pbi->frame_ar = height * 0x100 / width;
9111 /*
9112  *TODO:FOR VERSION
9113  */
9114 	pr_info("vp9: ver (%d,%d) decinfo: %dx%d rate=%d\n", vp9_version,
9115 		   0, width, height, pbi->frame_dur);
9116 
9117 	if (pbi->frame_dur == 0)
9118 		pbi->frame_dur = 96000 / 24;
9119 
9120 	INIT_KFIFO(pbi->display_q);
9121 	INIT_KFIFO(pbi->newframe_q);
9122 
9123 
9124 	for (i = 0; i < VF_POOL_SIZE; i++) {
9125 		const struct vframe_s *vf = &pbi->vfpool[i];
9126 
9127 		pbi->vfpool[i].index = -1;
9128 		kfifo_put(&pbi->newframe_q, vf);
9129 	}
9130 
9131 
9132 	ret = vp9_local_init(pbi);
9133 
9134 	if (!pbi->pts_unstable) {
9135 		pbi->pts_unstable =
9136 		(pbi->vvp9_amstream_dec_info.rate == 0)?1:0;
9137 		pr_info("set pts unstable\n");
9138 	}
9139 
9140 	return ret;
9141 }
9142 
9143 
9144 #ifdef MULTI_INSTANCE_SUPPORT
vvp9_init(struct vdec_s * vdec)9145 static s32 vvp9_init(struct vdec_s *vdec)
9146 {
9147 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)vdec->private;
9148 #else
9149 static s32 vvp9_init(struct VP9Decoder_s *pbi)
9150 {
9151 #endif
9152 	int ret;
9153 	int fw_size = 0x1000 * 16;
9154 	struct firmware_s *fw = NULL;
9155 
9156 	pbi->stat |= STAT_TIMER_INIT;
9157 
9158 	if (vvp9_local_init(pbi) < 0)
9159 		return -EBUSY;
9160 
9161 	fw = vmalloc(sizeof(struct firmware_s) + fw_size);
9162 	if (IS_ERR_OR_NULL(fw))
9163 		return -ENOMEM;
9164 
9165 	if (get_firmware_data(VIDEO_DEC_VP9_MMU, fw->data) < 0) {
9166 		pr_err("get firmware fail.\n");
9167 		vfree(fw);
9168 		return -1;
9169 	}
9170 
9171 	fw->len = fw_size;
9172 
9173 	INIT_WORK(&pbi->set_clk_work, vp9_set_clk);
9174 	timer_setup(&pbi->timer, NULL, 0);
9175 
9176 #ifdef MULTI_INSTANCE_SUPPORT
9177 	if (pbi->m_ins_flag) {
9178 		//pbi->timer.data = (ulong) pbi;
9179 		pbi->timer.function = vvp9_put_timer_func;
9180 		pbi->timer.expires = jiffies + PUT_INTERVAL;
9181 
9182 		/*add_timer(&pbi->timer);
9183 
9184 		pbi->stat |= STAT_TIMER_ARM;
9185 		pbi->stat |= STAT_ISR_REG;*/
9186 
9187 		INIT_WORK(&pbi->work, vp9_work);
9188 		INIT_WORK(&pbi->recycle_mmu_work, vp9_recycle_mmu_work);
9189 #ifdef SUPPORT_FB_DECODING
9190 		if (pbi->used_stage_buf_num > 0)
9191 			INIT_WORK(&pbi->s1_work, vp9_s1_work);
9192 #endif
9193 		pbi->fw = fw;
9194 
9195 		/* picture list init.*/
9196 		pbi->dec_result = DEC_INIT_PICLIST;
9197 		vdec_schedule_work(&pbi->work);
9198 
9199 		return 0;
9200 	}
9201 #endif
9202 	amhevc_enable();
9203 
9204 	init_pic_list(pbi);
9205 
9206 	ret = amhevc_loadmc_ex(VFORMAT_VP9, NULL, fw->data);
9207 	if (ret < 0) {
9208 		amhevc_disable();
9209 		vfree(fw);
9210 		pr_err("VP9: the %s fw loading failed, err: %x\n",
9211 			tee_enabled() ? "TEE" : "local", ret);
9212 		return -EBUSY;
9213 	}
9214 
9215 	vfree(fw);
9216 
9217 	pbi->stat |= STAT_MC_LOAD;
9218 
9219 	/* enable AMRISC side protocol */
9220 	vvp9_prot_init(pbi, HW_MASK_FRONT | HW_MASK_BACK);
9221 
9222 	if (vdec_request_threaded_irq(VDEC_IRQ_0,
9223 				vvp9_isr,
9224 				vvp9_isr_thread_fn,
9225 				IRQF_ONESHOT,/*run thread on this irq disabled*/
9226 				"vvp9-irq", (void *)pbi)) {
9227 		pr_info("vvp9 irq register error.\n");
9228 		amhevc_disable();
9229 		return -ENOENT;
9230 	}
9231 
9232 	pbi->stat |= STAT_ISR_REG;
9233 
9234 	pbi->provider_name = PROVIDER_NAME;
9235 #ifdef MULTI_INSTANCE_SUPPORT
9236 	vf_provider_init(&vvp9_vf_prov, PROVIDER_NAME,
9237 				&vvp9_vf_provider, pbi);
9238 	vf_reg_provider(&vvp9_vf_prov);
9239 	vf_notify_receiver(PROVIDER_NAME, VFRAME_EVENT_PROVIDER_START, NULL);
9240 	if (pbi->frame_dur != 0) {
9241 		if (!is_reset)
9242 			vf_notify_receiver(pbi->provider_name,
9243 					VFRAME_EVENT_PROVIDER_FR_HINT,
9244 					(void *)
9245 					((unsigned long)pbi->frame_dur));
9246 	}
9247 #else
9248 	vf_provider_init(&vvp9_vf_prov, PROVIDER_NAME, &vvp9_vf_provider,
9249 					 pbi);
9250 	vf_reg_provider(&vvp9_vf_prov);
9251 	vf_notify_receiver(PROVIDER_NAME, VFRAME_EVENT_PROVIDER_START, NULL);
9252 	if (!is_reset)
9253 		vf_notify_receiver(PROVIDER_NAME, VFRAME_EVENT_PROVIDER_FR_HINT,
9254 		(void *)((unsigned long)pbi->frame_dur));
9255 #endif
9256 	pbi->stat |= STAT_VF_HOOK;
9257 
9258 	//pbi->timer.data = (ulong)pbi;
9259 	pbi->timer.function = vvp9_put_timer_func;
9260 	pbi->timer.expires = jiffies + PUT_INTERVAL;
9261 
9262 	pbi->stat |= STAT_VDEC_RUN;
9263 
9264 	add_timer(&pbi->timer);
9265 
9266 	pbi->stat |= STAT_TIMER_ARM;
9267 
9268 	amhevc_start();
9269 
9270 	pbi->init_flag = 1;
9271 	pbi->process_busy = 0;
9272 	pr_info("%d, vvp9_init, RP=0x%x\n",
9273 		__LINE__, READ_VREG(HEVC_STREAM_RD_PTR));
9274 	return 0;
9275 }
9276 
9277 static int vmvp9_stop(struct VP9Decoder_s *pbi)
9278 {
9279 	pbi->init_flag = 0;
9280 
9281 	if (pbi->stat & STAT_VDEC_RUN) {
9282 		amhevc_stop();
9283 		pbi->stat &= ~STAT_VDEC_RUN;
9284 	}
9285 	if (pbi->stat & STAT_ISR_REG) {
9286 		vdec_free_irq(VDEC_IRQ_0, (void *)pbi);
9287 		pbi->stat &= ~STAT_ISR_REG;
9288 	}
9289 	if (pbi->stat & STAT_TIMER_ARM) {
9290 		del_timer_sync(&pbi->timer);
9291 		pbi->stat &= ~STAT_TIMER_ARM;
9292 	}
9293 
9294 	if (pbi->stat & STAT_VF_HOOK) {
9295 		if (!is_reset)
9296 			vf_notify_receiver(pbi->provider_name,
9297 					VFRAME_EVENT_PROVIDER_FR_END_HINT,
9298 					NULL);
9299 
9300 		vf_unreg_provider(&vvp9_vf_prov);
9301 		pbi->stat &= ~STAT_VF_HOOK;
9302 	}
9303 	vp9_local_uninit(pbi);
9304 	reset_process_time(pbi);
9305 	cancel_work_sync(&pbi->work);
9306 	cancel_work_sync(&pbi->recycle_mmu_work);
9307 #ifdef SUPPORT_FB_DECODING
9308 	if (pbi->used_stage_buf_num > 0)
9309 		cancel_work_sync(&pbi->s1_work);
9310 #endif
9311 	cancel_work_sync(&pbi->set_clk_work);
9312 	uninit_mmu_buffers(pbi);
9313 	if (pbi->fw)
9314 		vfree(pbi->fw);
9315 	pbi->fw = NULL;
9316 	return 0;
9317 }
9318 
9319 static int vvp9_stop(struct VP9Decoder_s *pbi)
9320 {
9321 
9322 	pbi->init_flag = 0;
9323 	pbi->first_sc_checked = 0;
9324 	if (pbi->stat & STAT_VDEC_RUN) {
9325 		amhevc_stop();
9326 		pbi->stat &= ~STAT_VDEC_RUN;
9327 	}
9328 
9329 	if (pbi->stat & STAT_ISR_REG) {
9330 #ifdef MULTI_INSTANCE_SUPPORT
9331 		if (!pbi->m_ins_flag)
9332 #endif
9333 			WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
9334 		vdec_free_irq(VDEC_IRQ_0, (void *)pbi);
9335 		pbi->stat &= ~STAT_ISR_REG;
9336 	}
9337 
9338 	if (pbi->stat & STAT_TIMER_ARM) {
9339 		del_timer_sync(&pbi->timer);
9340 		pbi->stat &= ~STAT_TIMER_ARM;
9341 	}
9342 
9343 	if (pbi->stat & STAT_VF_HOOK) {
9344 		if (!is_reset)
9345 			vf_notify_receiver(pbi->provider_name,
9346 					VFRAME_EVENT_PROVIDER_FR_END_HINT,
9347 					NULL);
9348 
9349 		vf_unreg_provider(&vvp9_vf_prov);
9350 		pbi->stat &= ~STAT_VF_HOOK;
9351 	}
9352 	vp9_local_uninit(pbi);
9353 
9354 	cancel_work_sync(&pbi->set_clk_work);
9355 #ifdef MULTI_INSTANCE_SUPPORT
9356 	if (pbi->m_ins_flag) {
9357 #ifdef SUPPORT_FB_DECODING
9358 		if (pbi->used_stage_buf_num > 0)
9359 			cancel_work_sync(&pbi->s1_work);
9360 #endif
9361 		cancel_work_sync(&pbi->work);
9362 		cancel_work_sync(&pbi->recycle_mmu_work);
9363 	} else
9364 		amhevc_disable();
9365 #else
9366 	amhevc_disable();
9367 #endif
9368 	uninit_mmu_buffers(pbi);
9369 
9370 	vfree(pbi->fw);
9371 	pbi->fw = NULL;
9372 	return 0;
9373 }
9374 static int amvdec_vp9_mmu_init(struct VP9Decoder_s *pbi)
9375 {
9376 	int tvp_flag = vdec_secure(hw_to_vdec(pbi)) ?
9377 		CODEC_MM_FLAGS_TVP : 0;
9378 	int buf_size = 48;
9379 
9380 	if ((pbi->max_pic_w * pbi->max_pic_h > 1280*736) &&
9381 		(pbi->max_pic_w * pbi->max_pic_h <= 1920*1088)) {
9382 		buf_size = 12;
9383 	} else if ((pbi->max_pic_w * pbi->max_pic_h > 0) &&
9384 		(pbi->max_pic_w * pbi->max_pic_h <= 1280*736)) {
9385 		buf_size = 4;
9386 	}
9387 	pbi->need_cache_size = buf_size * SZ_1M;
9388 	pbi->sc_start_time = get_jiffies_64();
9389 	if (pbi->mmu_enable && ((pbi->double_write_mode & 0x10) == 0)) {
9390 		pbi->mmu_box = decoder_mmu_box_alloc_box(DRIVER_NAME,
9391 			pbi->index, FRAME_BUFFERS,
9392 			pbi->need_cache_size,
9393 			tvp_flag
9394 			);
9395 		if (!pbi->mmu_box) {
9396 			pr_err("vp9 alloc mmu box failed!!\n");
9397 			return -1;
9398 		}
9399 	}
9400 	pbi->bmmu_box = decoder_bmmu_box_alloc_box(
9401 			DRIVER_NAME,
9402 			pbi->index,
9403 			MAX_BMMU_BUFFER_NUM,
9404 			4 + PAGE_SHIFT,
9405 			CODEC_MM_FLAGS_CMA_CLEAR |
9406 			CODEC_MM_FLAGS_FOR_VDECODER |
9407 			tvp_flag);
9408 	if (!pbi->bmmu_box) {
9409 		pr_err("vp9 alloc bmmu box failed!!\n");
9410 		return -1;
9411 	}
9412 	return 0;
9413 }
9414 
9415 static struct VP9Decoder_s *gHevc;
9416 
9417 static int amvdec_vp9_probe(struct platform_device *pdev)
9418 {
9419 	struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
9420 	struct BUF_s BUF[MAX_BUF_NUM];
9421 	struct VP9Decoder_s *pbi;
9422 	int ret;
9423 #ifndef MULTI_INSTANCE_SUPPORT
9424 	int i;
9425 #endif
9426 	pr_debug("%s\n", __func__);
9427 
9428 	mutex_lock(&vvp9_mutex);
9429 	pbi = vmalloc(sizeof(struct VP9Decoder_s));
9430 	if (pbi == NULL) {
9431 		pr_info("\namvdec_vp9 device data allocation failed\n");
9432 		mutex_unlock(&vvp9_mutex);
9433 		return -ENOMEM;
9434 	}
9435 
9436 	gHevc = pbi;
9437 	memcpy(&BUF[0], &pbi->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
9438 	memset(pbi, 0, sizeof(struct VP9Decoder_s));
9439 	memcpy(&pbi->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
9440 
9441 	pbi->init_flag = 0;
9442 	pbi->first_sc_checked= 0;
9443 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
9444 		vp9_max_pic_w = 8192;
9445 		vp9_max_pic_h = 4608;
9446 	}
9447 	pbi->max_pic_w = vp9_max_pic_w;
9448 	pbi->max_pic_h = vp9_max_pic_h;
9449 
9450 #ifdef MULTI_INSTANCE_SUPPORT
9451 	pbi->eos = 0;
9452 	pbi->start_process_time = 0;
9453 	pbi->timeout_num = 0;
9454 #endif
9455 	pbi->fatal_error = 0;
9456 	pbi->show_frame_num = 0;
9457 	if (pdata == NULL) {
9458 		pr_info("\namvdec_vp9 memory resource undefined.\n");
9459 		vfree(pbi);
9460 		mutex_unlock(&vvp9_mutex);
9461 		return -EFAULT;
9462 	}
9463 	pbi->m_ins_flag = 0;
9464 #ifdef MULTI_INSTANCE_SUPPORT
9465 	pbi->platform_dev = pdev;
9466 	platform_set_drvdata(pdev, pdata);
9467 #endif
9468 	pbi->double_write_mode = double_write_mode;
9469 	pbi->mmu_enable = 1;
9470 	if (amvdec_vp9_mmu_init(pbi) < 0) {
9471 		vfree(pbi);
9472 		mutex_unlock(&vvp9_mutex);
9473 		pr_err("vp9 alloc bmmu box failed!!\n");
9474 		return -1;
9475 	}
9476 
9477 	ret = decoder_bmmu_box_alloc_buf_phy(pbi->bmmu_box, WORK_SPACE_BUF_ID,
9478 			work_buf_size, DRIVER_NAME, &pdata->mem_start);
9479 	if (ret < 0) {
9480 		uninit_mmu_buffers(pbi);
9481 		vfree(pbi);
9482 		mutex_unlock(&vvp9_mutex);
9483 		return ret;
9484 	}
9485 	pbi->buf_size = work_buf_size;
9486 
9487 #ifdef MULTI_INSTANCE_SUPPORT
9488 	pbi->buf_start = pdata->mem_start;
9489 #else
9490 	if (!pbi->mmu_enable)
9491 		pbi->mc_buf_spec.buf_end = pdata->mem_start + pbi->buf_size;
9492 
9493 	for (i = 0; i < WORK_BUF_SPEC_NUM; i++)
9494 		amvvp9_workbuff_spec[i].start_adr = pdata->mem_start;
9495 #endif
9496 
9497 
9498 	if (debug) {
9499 		pr_info("===VP9 decoder mem resource 0x%lx size 0x%x\n",
9500 			   pdata->mem_start, pbi->buf_size);
9501 	}
9502 
9503 	if (pdata->sys_info)
9504 		pbi->vvp9_amstream_dec_info = *pdata->sys_info;
9505 	else {
9506 		pbi->vvp9_amstream_dec_info.width = 0;
9507 		pbi->vvp9_amstream_dec_info.height = 0;
9508 		pbi->vvp9_amstream_dec_info.rate = 30;
9509 	}
9510 	pbi->no_head = no_head;
9511 #ifdef MULTI_INSTANCE_SUPPORT
9512 	pbi->cma_dev = pdata->cma_dev;
9513 #else
9514 	cma_dev = pdata->cma_dev;
9515 #endif
9516 
9517 #ifdef MULTI_INSTANCE_SUPPORT
9518 	pdata->private = pbi;
9519 	pdata->dec_status = vvp9_dec_status;
9520 	pdata->set_isreset = vvp9_set_isreset;
9521 	is_reset = 0;
9522 	if (vvp9_init(pdata) < 0) {
9523 #else
9524 	if (vvp9_init(pbi) < 0) {
9525 #endif
9526 		pr_info("\namvdec_vp9 init failed.\n");
9527 		vp9_local_uninit(pbi);
9528 		uninit_mmu_buffers(pbi);
9529 		vfree(pbi);
9530 		pdata->dec_status = NULL;
9531 		mutex_unlock(&vvp9_mutex);
9532 		return -ENODEV;
9533 	}
9534 	/*set the max clk for smooth playing...*/
9535 	hevc_source_changed(VFORMAT_VP9,
9536 			4096, 2048, 60);
9537 	mutex_unlock(&vvp9_mutex);
9538 
9539 	return 0;
9540 }
9541 
9542 static int amvdec_vp9_remove(struct platform_device *pdev)
9543 {
9544 	struct VP9Decoder_s *pbi = gHevc;
9545 	struct vdec_s *vdec = hw_to_vdec(pbi);
9546 	int i;
9547 
9548 	if (debug)
9549 		pr_info("amvdec_vp9_remove\n");
9550 
9551 	mutex_lock(&vvp9_mutex);
9552 
9553 	vvp9_stop(pbi);
9554 
9555 	hevc_source_changed(VFORMAT_VP9, 0, 0, 0);
9556 
9557 	if (vdec->parallel_dec == 1) {
9558 		for (i = 0; i < FRAME_BUFFERS; i++) {
9559 			vdec->free_canvas_ex(pbi->common.buffer_pool->
9560 				frame_bufs[i].buf.y_canvas_index, vdec->id);
9561 			vdec->free_canvas_ex(pbi->common.buffer_pool->
9562 				frame_bufs[i].buf.uv_canvas_index, vdec->id);
9563 		}
9564 	}
9565 
9566 #ifdef DEBUG_PTS
9567 	pr_info("pts missed %ld, pts hit %ld, duration %d\n",
9568 		   pbi->pts_missed, pbi->pts_hit, pbi->frame_dur);
9569 #endif
9570 	mem_map_mode = 0;
9571 
9572 	vfree(pbi);
9573 	mutex_unlock(&vvp9_mutex);
9574 
9575 	return 0;
9576 }
9577 
9578 /****************************************/
9579 #ifdef CONFIG_PM
9580 static int vp9_suspend(struct device *dev)
9581 {
9582 	amhevc_suspend(to_platform_device(dev), dev->power.power_state);
9583 	return 0;
9584 }
9585 
9586 static int vp9_resume(struct device *dev)
9587 {
9588 	amhevc_resume(to_platform_device(dev));
9589 	return 0;
9590 }
9591 
9592 static const struct dev_pm_ops vp9_pm_ops = {
9593 	SET_SYSTEM_SLEEP_PM_OPS(vp9_suspend, vp9_resume)
9594 };
9595 #endif
9596 
9597 static struct platform_driver amvdec_vp9_driver = {
9598 	.probe = amvdec_vp9_probe,
9599 	.remove = amvdec_vp9_remove,
9600 	.driver = {
9601 		.name = DRIVER_NAME,
9602 #ifdef CONFIG_PM
9603 		.pm = &vp9_pm_ops,
9604 #endif
9605 	}
9606 };
9607 
9608 static struct codec_profile_t amvdec_vp9_profile = {
9609 	.name = "vp9",
9610 	.profile = ""
9611 };
9612 
9613 static struct codec_profile_t amvdec_vp9_profile_mult;
9614 
9615 static unsigned char get_data_check_sum
9616 	(struct VP9Decoder_s *pbi, int size)
9617 {
9618 	int jj;
9619 	int sum = 0;
9620 	u8 *data = NULL;
9621 
9622 	if (!pbi->chunk->block->is_mapped)
9623 		data = codec_mm_vmap(pbi->chunk->block->start +
9624 			pbi->chunk->offset, size);
9625 	else
9626 		data = ((u8 *)pbi->chunk->block->start_virt) +
9627 			pbi->chunk->offset;
9628 
9629 	for (jj = 0; jj < size; jj++)
9630 		sum += data[jj];
9631 
9632 	if (!pbi->chunk->block->is_mapped)
9633 		codec_mm_unmap_phyaddr(data);
9634 	return sum;
9635 }
9636 
9637 static void dump_data(struct VP9Decoder_s *pbi, int size)
9638 {
9639 	int jj;
9640 	u8 *data = NULL;
9641 	int padding_size = pbi->chunk->offset &
9642 		(VDEC_FIFO_ALIGN - 1);
9643 
9644 	if (!pbi->chunk->block->is_mapped)
9645 		data = codec_mm_vmap(pbi->chunk->block->start +
9646 			pbi->chunk->offset, size);
9647 	else
9648 		data = ((u8 *)pbi->chunk->block->start_virt) +
9649 			pbi->chunk->offset;
9650 
9651 	vp9_print(pbi, 0, "padding: ");
9652 	for (jj = padding_size; jj > 0; jj--)
9653 		vp9_print_cont(pbi,
9654 			0,
9655 			"%02x ", *(data - jj));
9656 	vp9_print_cont(pbi, 0, "data adr %p\n",
9657 		data);
9658 
9659 	for (jj = 0; jj < size; jj++) {
9660 		if ((jj & 0xf) == 0)
9661 			vp9_print(pbi,
9662 				0,
9663 				"%06x:", jj);
9664 		vp9_print_cont(pbi,
9665 			0,
9666 			"%02x ", data[jj]);
9667 		if (((jj + 1) & 0xf) == 0)
9668 			vp9_print(pbi,
9669 			 0,
9670 				"\n");
9671 	}
9672 	vp9_print(pbi,
9673 	 0,
9674 		"\n");
9675 
9676 	if (!pbi->chunk->block->is_mapped)
9677 		codec_mm_unmap_phyaddr(data);
9678 }
9679 
9680 static void vp9_work(struct work_struct *work)
9681 {
9682 	struct VP9Decoder_s *pbi = container_of(work,
9683 		struct VP9Decoder_s, work);
9684 	struct vdec_s *vdec = hw_to_vdec(pbi);
9685 	/* finished decoding one frame or error,
9686 	 * notify vdec core to switch context
9687 	 */
9688 	vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
9689 		"%s dec_result %d %x %x %x\n",
9690 		__func__,
9691 		pbi->dec_result,
9692 		READ_VREG(HEVC_STREAM_LEVEL),
9693 		READ_VREG(HEVC_STREAM_WR_PTR),
9694 		READ_VREG(HEVC_STREAM_RD_PTR));
9695 
9696 	if (pbi->dec_result == DEC_INIT_PICLIST) {
9697 		init_pic_list(pbi);
9698 		pbi->pic_list_init_done = true;
9699 		return;
9700 	}
9701 
9702 	if (((pbi->dec_result == DEC_RESULT_GET_DATA) ||
9703 		(pbi->dec_result == DEC_RESULT_GET_DATA_RETRY))
9704 		&& (hw_to_vdec(pbi)->next_status !=
9705 		VDEC_STATUS_DISCONNECTED)) {
9706 		if (!vdec_has_more_input(vdec)) {
9707 			pbi->dec_result = DEC_RESULT_EOS;
9708 			vdec_schedule_work(&pbi->work);
9709 			return;
9710 		}
9711 
9712 		if (pbi->dec_result == DEC_RESULT_GET_DATA) {
9713 			vp9_print(pbi, PRINT_FLAG_VDEC_STATUS,
9714 				"%s DEC_RESULT_GET_DATA %x %x %x\n",
9715 				__func__,
9716 				READ_VREG(HEVC_STREAM_LEVEL),
9717 				READ_VREG(HEVC_STREAM_WR_PTR),
9718 				READ_VREG(HEVC_STREAM_RD_PTR));
9719 			vdec_vframe_dirty(vdec, pbi->chunk);
9720 			vdec_clean_input(vdec);
9721 		}
9722 
9723 		if (get_free_buf_count(pbi) >=
9724 			run_ready_min_buf_num) {
9725 			int r;
9726 			int decode_size;
9727 			r = vdec_prepare_input(vdec, &pbi->chunk);
9728 			if (r < 0) {
9729 				pbi->dec_result = DEC_RESULT_GET_DATA_RETRY;
9730 
9731 				vp9_print(pbi,
9732 					PRINT_FLAG_VDEC_DETAIL,
9733 					"amvdec_vh265: Insufficient data\n");
9734 
9735 				vdec_schedule_work(&pbi->work);
9736 				return;
9737 			}
9738 			pbi->dec_result = DEC_RESULT_NONE;
9739 			vp9_print(pbi, PRINT_FLAG_VDEC_STATUS,
9740 				"%s: chunk size 0x%x sum 0x%x\n",
9741 				__func__, r,
9742 				(debug & PRINT_FLAG_VDEC_STATUS) ?
9743 				get_data_check_sum(pbi, r) : 0
9744 				);
9745 
9746 			if (debug & PRINT_FLAG_VDEC_DATA)
9747 				dump_data(pbi, pbi->chunk->size);
9748 
9749 			decode_size = pbi->chunk->size +
9750 				(pbi->chunk->offset & (VDEC_FIFO_ALIGN - 1));
9751 
9752 			WRITE_VREG(HEVC_DECODE_SIZE,
9753 				READ_VREG(HEVC_DECODE_SIZE) + decode_size);
9754 
9755 			vdec_enable_input(vdec);
9756 
9757 			WRITE_VREG(HEVC_DEC_STATUS_REG, HEVC_ACTION_DONE);
9758 
9759 			start_process_time(pbi);
9760 
9761 		} else{
9762 			pbi->dec_result = DEC_RESULT_GET_DATA_RETRY;
9763 
9764 			vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
9765 				"amvdec_vh265: Insufficient data\n");
9766 
9767 			vdec_schedule_work(&pbi->work);
9768 		}
9769 		return;
9770 	} else if (pbi->dec_result == DEC_RESULT_DONE) {
9771 #ifdef SUPPORT_FB_DECODING
9772 		if (pbi->used_stage_buf_num > 0) {
9773 #ifndef FB_DECODING_TEST_SCHEDULE
9774 			if (!is_s2_decoding_finished(pbi)) {
9775 				vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
9776 					"s2 decoding not done, check again later\n");
9777 				vdec_schedule_work(&pbi->work);
9778 			}
9779 #endif
9780 			inc_s2_pos(pbi);
9781 			if (mcrcc_cache_alg_flag)
9782 				dump_hit_rate(pbi);
9783 		}
9784 #endif
9785 		/* if (!pbi->ctx_valid)
9786 			pbi->ctx_valid = 1; */
9787 		pbi->slice_idx++;
9788 		pbi->frame_count++;
9789 		pbi->process_state = PROC_STATE_INIT;
9790 		decode_frame_count[pbi->index] = pbi->frame_count;
9791 
9792 		if (pbi->mmu_enable)
9793 			pbi->used_4k_num =
9794 				(READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
9795 		vp9_print(pbi, PRINT_FLAG_VDEC_STATUS,
9796 			"%s (===> %d) dec_result %d %x %x %x shiftbytes 0x%x decbytes 0x%x\n",
9797 			__func__,
9798 			pbi->frame_count,
9799 			pbi->dec_result,
9800 			READ_VREG(HEVC_STREAM_LEVEL),
9801 			READ_VREG(HEVC_STREAM_WR_PTR),
9802 			READ_VREG(HEVC_STREAM_RD_PTR),
9803 			READ_VREG(HEVC_SHIFT_BYTE_COUNT),
9804 			READ_VREG(HEVC_SHIFT_BYTE_COUNT) -
9805 			pbi->start_shift_bytes
9806 			);
9807 		vdec_vframe_dirty(hw_to_vdec(pbi), pbi->chunk);
9808 	} else if (pbi->dec_result == DEC_RESULT_AGAIN) {
9809 		/*
9810 			stream base: stream buf empty or timeout
9811 			frame base: vdec_prepare_input fail
9812 		*/
9813 		if (!vdec_has_more_input(vdec)) {
9814 			pbi->dec_result = DEC_RESULT_EOS;
9815 			vdec_schedule_work(&pbi->work);
9816 			return;
9817 		}
9818 	} else if (pbi->dec_result == DEC_RESULT_EOS) {
9819 		vp9_print(pbi, PRINT_FLAG_VDEC_STATUS,
9820 			"%s: end of stream\n",
9821 			__func__);
9822 		pbi->eos = 1;
9823 		vp9_bufmgr_postproc(pbi);
9824 
9825 		if (pbi->is_used_v4l)
9826 			notify_v4l_eos(hw_to_vdec(pbi));
9827 
9828 		vdec_vframe_dirty(hw_to_vdec(pbi), pbi->chunk);
9829 	} else if (pbi->dec_result == DEC_RESULT_FORCE_EXIT) {
9830 		vp9_print(pbi, PRINT_FLAG_VDEC_STATUS,
9831 			"%s: force exit\n",
9832 			__func__);
9833 		if (pbi->stat & STAT_VDEC_RUN) {
9834 			amhevc_stop();
9835 			pbi->stat &= ~STAT_VDEC_RUN;
9836 		}
9837 
9838 		if (pbi->stat & STAT_ISR_REG) {
9839 #ifdef MULTI_INSTANCE_SUPPORT
9840 			if (!pbi->m_ins_flag)
9841 #endif
9842 				WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
9843 			vdec_free_irq(VDEC_IRQ_0, (void *)pbi);
9844 			pbi->stat &= ~STAT_ISR_REG;
9845 		}
9846 	}
9847 	if (pbi->stat & STAT_VDEC_RUN) {
9848 		amhevc_stop();
9849 		pbi->stat &= ~STAT_VDEC_RUN;
9850 	}
9851 
9852 	if (pbi->stat & STAT_TIMER_ARM) {
9853 		del_timer_sync(&pbi->timer);
9854 		pbi->stat &= ~STAT_TIMER_ARM;
9855 	}
9856 	/* mark itself has all HW resource released and input released */
9857 #ifdef SUPPORT_FB_DECODING
9858 	if (pbi->used_stage_buf_num > 0)
9859 		vdec_core_finish_run(hw_to_vdec(pbi), CORE_MASK_HEVC_BACK);
9860 	else
9861 		vdec_core_finish_run(hw_to_vdec(pbi), CORE_MASK_VDEC_1
9862 				| CORE_MASK_HEVC
9863 				| CORE_MASK_HEVC_FRONT
9864 				| CORE_MASK_HEVC_BACK
9865 				);
9866 #else
9867 	if (vdec->parallel_dec == 1)
9868 		vdec_core_finish_run(vdec, CORE_MASK_HEVC);
9869 	else
9870 		vdec_core_finish_run(hw_to_vdec(pbi), CORE_MASK_VDEC_1
9871 					| CORE_MASK_HEVC);
9872 #endif
9873 	trigger_schedule(pbi);
9874 }
9875 
9876 static int vp9_hw_ctx_restore(struct VP9Decoder_s *pbi)
9877 {
9878 	/* new to do ... */
9879 #if (!defined SUPPORT_FB_DECODING)
9880 	vvp9_prot_init(pbi, HW_MASK_FRONT | HW_MASK_BACK);
9881 #elif (defined FB_DECODING_TEST_SCHEDULE)
9882 	vvp9_prot_init(pbi, HW_MASK_FRONT | HW_MASK_BACK);
9883 #else
9884 	if (pbi->used_stage_buf_num > 0)
9885 		vvp9_prot_init(pbi, HW_MASK_FRONT);
9886 	else
9887 		vvp9_prot_init(pbi, HW_MASK_FRONT | HW_MASK_BACK);
9888 #endif
9889 	return 0;
9890 }
9891 static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask)
9892 {
9893 	struct VP9Decoder_s *pbi =
9894 		(struct VP9Decoder_s *)vdec->private;
9895 	int tvp = vdec_secure(hw_to_vdec(pbi)) ?
9896 		CODEC_MM_FLAGS_TVP : 0;
9897 	unsigned long ret = 0;
9898 
9899 	if (!(pbi->pic_list_init_done && pbi->pic_list_init_done2) || pbi->eos)
9900 		return ret;
9901 	if (!pbi->first_sc_checked && pbi->mmu_enable) {
9902 		int size = decoder_mmu_box_sc_check(pbi->mmu_box, tvp);
9903 		pbi->first_sc_checked = 1;
9904 		vp9_print(pbi, 0, "vp9 cached=%d  need_size=%d speed= %d ms\n",
9905 			size, (pbi->need_cache_size >> PAGE_SHIFT),
9906 			(int)(get_jiffies_64() - pbi->sc_start_time) * 1000/HZ);
9907 	}
9908 
9909 #ifdef SUPPORT_FB_DECODING
9910 	if (pbi->used_stage_buf_num > 0) {
9911 		if (mask & CORE_MASK_HEVC_FRONT) {
9912 			if (get_free_stage_buf_num(pbi) > 0
9913 				&& mv_buf_available(pbi))
9914 				ret |= CORE_MASK_HEVC_FRONT;
9915 		}
9916 		if (mask & CORE_MASK_HEVC_BACK) {
9917 			if (s2_buf_available(pbi) &&
9918 				(get_free_buf_count(pbi) >=
9919 				run_ready_min_buf_num)) {
9920 				ret |= CORE_MASK_HEVC_BACK;
9921 				pbi->back_not_run_ready = 0;
9922 			} else
9923 				pbi->back_not_run_ready = 1;
9924 #if 0
9925 			if (get_free_buf_count(pbi) <
9926 				run_ready_min_buf_num)
9927 				dump_pic_list(pbi);
9928 #endif
9929 		}
9930 	} else if (get_free_buf_count(pbi) >=
9931 		run_ready_min_buf_num)
9932 		ret = CORE_MASK_VDEC_1 | CORE_MASK_HEVC
9933 			| CORE_MASK_HEVC_FRONT
9934 			| CORE_MASK_HEVC_BACK;
9935 
9936 	if (ret & CORE_MASK_HEVC_FRONT)
9937 		not_run_ready[pbi->index] = 0;
9938 	else
9939 		not_run_ready[pbi->index]++;
9940 
9941 	if (ret & CORE_MASK_HEVC_BACK)
9942 		not_run2_ready[pbi->index] = 0;
9943 	else
9944 		not_run2_ready[pbi->index]++;
9945 
9946 	vp9_print(pbi,
9947 		PRINT_FLAG_VDEC_DETAIL, "%s mask %lx=>%lx (%d %d %d %d)\r\n",
9948 		__func__, mask, ret,
9949 		get_free_stage_buf_num(pbi),
9950 		mv_buf_available(pbi),
9951 		s2_buf_available(pbi),
9952 		get_free_buf_count(pbi)
9953 		);
9954 
9955 	return ret;
9956 
9957 #else
9958 	if (get_free_buf_count(pbi) >=
9959 		run_ready_min_buf_num) {
9960 		if (vdec->parallel_dec == 1)
9961 			ret = CORE_MASK_HEVC;
9962 		else
9963 			ret = CORE_MASK_VDEC_1 | CORE_MASK_HEVC;
9964 	}
9965 
9966 	if (pbi->is_used_v4l) {
9967 		struct aml_vcodec_ctx *ctx =
9968 			(struct aml_vcodec_ctx *)(pbi->v4l2_ctx);
9969 
9970 		if (ctx->param_sets_from_ucode) {
9971 			if (pbi->v4l_params_parsed) {
9972 				if ((ctx->cap_pool.in < pbi->used_buf_num) &&
9973 				v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) <
9974 				run_ready_min_buf_num)
9975 					ret = 0;
9976 			} else {
9977 				if ((pbi->res_ch_flag == 1) &&
9978 				((ctx->state <= AML_STATE_INIT) ||
9979 				(ctx->state >= AML_STATE_FLUSHING)))
9980 					ret = 0;
9981 			}
9982 		} else if (ctx->cap_pool.in < ctx->dpb_size) {
9983 			if (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) <
9984 				run_ready_min_buf_num)
9985 				ret = 0;
9986 		}
9987 	}
9988 
9989 	if (ret)
9990 		not_run_ready[pbi->index] = 0;
9991 	else
9992 		not_run_ready[pbi->index]++;
9993 
9994 	vp9_print(pbi,
9995 		PRINT_FLAG_VDEC_DETAIL, "%s mask %lx=>%lx\r\n",
9996 		__func__, mask, ret);
9997 	return ret;
9998 #endif
9999 }
10000 
10001 static void run_front(struct vdec_s *vdec)
10002 {
10003 	struct VP9Decoder_s *pbi =
10004 		(struct VP9Decoder_s *)vdec->private;
10005 	int ret, size;
10006 
10007 	run_count[pbi->index]++;
10008 	/* pbi->chunk = vdec_prepare_input(vdec); */
10009 #if (!defined SUPPORT_FB_DECODING)
10010 	hevc_reset_core(vdec);
10011 #elif (defined FB_DECODING_TEST_SCHEDULE)
10012 	hevc_reset_core(vdec);
10013 #else
10014 	if (pbi->used_stage_buf_num > 0)
10015 		fb_reset_core(vdec, HW_MASK_FRONT);
10016 	else
10017 		hevc_reset_core(vdec);
10018 #endif
10019 
10020 	size = vdec_prepare_input(vdec, &pbi->chunk);
10021 	if (size < 0) {
10022 		input_empty[pbi->index]++;
10023 
10024 		pbi->dec_result = DEC_RESULT_AGAIN;
10025 
10026 		vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
10027 			"ammvdec_vh265: Insufficient data\n");
10028 
10029 		vdec_schedule_work(&pbi->work);
10030 		return;
10031 	}
10032 
10033 	input_empty[pbi->index] = 0;
10034 	pbi->dec_result = DEC_RESULT_NONE;
10035 	pbi->start_shift_bytes = READ_VREG(HEVC_SHIFT_BYTE_COUNT);
10036 
10037 	if (debug & PRINT_FLAG_VDEC_STATUS) {
10038 		int ii;
10039 		vp9_print(pbi, 0,
10040 			"%s (%d): size 0x%x (0x%x 0x%x) sum 0x%x (%x %x %x %x %x) bytes 0x%x",
10041 			__func__,
10042 			pbi->frame_count, size,
10043 			pbi->chunk ? pbi->chunk->size : 0,
10044 			pbi->chunk ? pbi->chunk->offset : 0,
10045 			pbi->chunk ? ((vdec_frame_based(vdec) &&
10046 			(debug & PRINT_FLAG_VDEC_STATUS)) ?
10047 			get_data_check_sum(pbi, size) : 0) : 0,
10048 		READ_VREG(HEVC_STREAM_START_ADDR),
10049 		READ_VREG(HEVC_STREAM_END_ADDR),
10050 		READ_VREG(HEVC_STREAM_LEVEL),
10051 		READ_VREG(HEVC_STREAM_WR_PTR),
10052 		READ_VREG(HEVC_STREAM_RD_PTR),
10053 		pbi->start_shift_bytes);
10054 		if (vdec_frame_based(vdec) && pbi->chunk) {
10055 			u8 *data = NULL;
10056 
10057 			if (!pbi->chunk->block->is_mapped)
10058 				data = codec_mm_vmap(pbi->chunk->block->start +
10059 					pbi->chunk->offset, 8);
10060 			else
10061 				data = ((u8 *)pbi->chunk->block->start_virt) +
10062 					pbi->chunk->offset;
10063 
10064 			vp9_print_cont(pbi, 0, "data adr %p:",
10065 				data);
10066 			for (ii = 0; ii < 8; ii++)
10067 				vp9_print_cont(pbi, 0, "%02x ",
10068 					data[ii]);
10069 
10070 			if (!pbi->chunk->block->is_mapped)
10071 				codec_mm_unmap_phyaddr(data);
10072 		}
10073 		vp9_print_cont(pbi, 0, "\r\n");
10074 	}
10075 	if (vdec->mc_loaded) {
10076 	/*firmware have load before,
10077 	  and not changes to another.
10078 	  ignore reload.
10079 	*/
10080 	} else {
10081 			ret = amhevc_loadmc_ex(VFORMAT_VP9, NULL, pbi->fw->data);
10082 			if (ret < 0) {
10083 			amhevc_disable();
10084 			vp9_print(pbi, PRINT_FLAG_ERROR,
10085 				"VP9: the %s fw loading failed, err: %x\n",
10086 				tee_enabled() ? "TEE" : "local", ret);
10087 			pbi->dec_result = DEC_RESULT_FORCE_EXIT;
10088 			vdec_schedule_work(&pbi->work);
10089 			return;
10090 		}
10091 		vdec->mc_loaded = 1;
10092 		vdec->mc_type = VFORMAT_VP9;
10093 	}
10094 
10095 	if (vp9_hw_ctx_restore(pbi) < 0) {
10096 		vdec_schedule_work(&pbi->work);
10097 		return;
10098 	}
10099 
10100 	vdec_enable_input(vdec);
10101 
10102 	WRITE_VREG(HEVC_DEC_STATUS_REG, HEVC_ACTION_DONE);
10103 
10104 	if (vdec_frame_based(vdec)) {
10105 		if (debug & PRINT_FLAG_VDEC_DATA)
10106 			dump_data(pbi, pbi->chunk->size);
10107 
10108 		WRITE_VREG(HEVC_SHIFT_BYTE_COUNT, 0);
10109 		size = pbi->chunk->size +
10110 			(pbi->chunk->offset & (VDEC_FIFO_ALIGN - 1));
10111 		if (vdec->mvfrm)
10112 			vdec->mvfrm->frame_size = pbi->chunk->size;
10113 	}
10114 	WRITE_VREG(HEVC_DECODE_SIZE, size);
10115 	WRITE_VREG(HEVC_DECODE_COUNT, pbi->slice_idx);
10116 	pbi->init_flag = 1;
10117 
10118 	vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
10119 		"%s: start hevc (%x %x %x)\n",
10120 		__func__,
10121 		READ_VREG(HEVC_DEC_STATUS_REG),
10122 		READ_VREG(HEVC_MPC_E),
10123 		READ_VREG(HEVC_MPSR));
10124 
10125 	start_process_time(pbi);
10126 	mod_timer(&pbi->timer, jiffies);
10127 	pbi->stat |= STAT_TIMER_ARM;
10128 	pbi->stat |= STAT_ISR_REG;
10129 	amhevc_start();
10130 	pbi->stat |= STAT_VDEC_RUN;
10131 }
10132 
10133 #ifdef SUPPORT_FB_DECODING
10134 static void mpred_process(struct VP9Decoder_s *pbi)
10135 {
10136 	union param_u *params = &pbi->s1_param;
10137 	unsigned char use_prev_frame_mvs =
10138 		!params->p.error_resilient_mode &&
10139 		params->p.width == pbi->s1_width &&
10140 		params->p.height == pbi->s1_height &&
10141 		!pbi->s1_intra_only &&
10142 		pbi->s1_last_show_frame &&
10143 		(pbi->s1_frame_type != KEY_FRAME);
10144 	pbi->s1_width = params->p.width;
10145 	pbi->s1_height = params->p.height;
10146 	pbi->s1_frame_type = params->p.frame_type;
10147 	pbi->s1_intra_only =
10148 		(params->p.show_frame ||
10149 		params->p.show_existing_frame)
10150 		? 0 : params->p.intra_only;
10151 	if ((pbi->s1_frame_type != KEY_FRAME)
10152 		&& (!pbi->s1_intra_only)) {
10153 		unsigned int data32;
10154 		int mpred_mv_rd_end_addr;
10155 
10156 		mpred_mv_rd_end_addr =
10157 			pbi->s1_mpred_mv_wr_start_addr_pre
10158 			+ (pbi->lcu_total * MV_MEM_UNIT);
10159 
10160 		WRITE_VREG(HEVC_MPRED_CTRL3, 0x24122412);
10161 		WRITE_VREG(HEVC_MPRED_ABV_START_ADDR,
10162 			pbi->work_space_buf->
10163 			mpred_above.buf_start);
10164 
10165 		data32 = READ_VREG(HEVC_MPRED_CTRL4);
10166 
10167 		data32 &=  (~(1 << 6));
10168 		data32 |= (use_prev_frame_mvs << 6);
10169 		WRITE_VREG(HEVC_MPRED_CTRL4, data32);
10170 
10171 		WRITE_VREG(HEVC_MPRED_MV_WR_START_ADDR,
10172 				pbi->s1_mpred_mv_wr_start_addr);
10173 		WRITE_VREG(HEVC_MPRED_MV_WPTR,
10174 			pbi->s1_mpred_mv_wr_start_addr);
10175 
10176 		WRITE_VREG(HEVC_MPRED_MV_RD_START_ADDR,
10177 			pbi->s1_mpred_mv_wr_start_addr_pre);
10178 		WRITE_VREG(HEVC_MPRED_MV_RPTR,
10179 			pbi->s1_mpred_mv_wr_start_addr_pre);
10180 
10181 		WRITE_VREG(HEVC_MPRED_MV_RD_END_ADDR,
10182 			mpred_mv_rd_end_addr);
10183 
10184 	} else
10185 		clear_mpred_hw(pbi);
10186 
10187 	if (!params->p.show_existing_frame) {
10188 		pbi->s1_mpred_mv_wr_start_addr_pre =
10189 			pbi->s1_mpred_mv_wr_start_addr;
10190 		pbi->s1_last_show_frame =
10191 			params->p.show_frame;
10192 		if (pbi->s1_mv_buf_index_pre_pre != MV_BUFFER_NUM)
10193 			put_mv_buf(pbi, &pbi->s1_mv_buf_index_pre_pre);
10194 		pbi->s1_mv_buf_index_pre_pre =
10195 			pbi->s1_mv_buf_index_pre;
10196 		pbi->s1_mv_buf_index_pre = pbi->s1_mv_buf_index;
10197 	} else
10198 		put_mv_buf(pbi, &pbi->s1_mv_buf_index);
10199 }
10200 
10201 static void vp9_s1_work(struct work_struct *s1_work)
10202 {
10203 	struct VP9Decoder_s *pbi = container_of(s1_work,
10204 		struct VP9Decoder_s, s1_work);
10205 	vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL,
10206 		"%s dec_s1_result %d\n",
10207 		__func__,
10208 		pbi->dec_s1_result);
10209 
10210 #ifdef FB_DECODING_TEST_SCHEDULE
10211 	if (pbi->dec_s1_result ==
10212 		DEC_S1_RESULT_TEST_TRIGGER_DONE) {
10213 		pbi->s1_test_cmd = TEST_SET_PIC_DONE;
10214 		WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG, 0x1);
10215 	}
10216 #endif
10217 	if (pbi->dec_s1_result == DEC_S1_RESULT_DONE ||
10218 		pbi->dec_s1_result == DEC_S1_RESULT_FORCE_EXIT) {
10219 
10220 		vdec_core_finish_run(hw_to_vdec(pbi),
10221 			CORE_MASK_HEVC_FRONT);
10222 
10223 		trigger_schedule(pbi);
10224 		/*pbi->dec_s1_result = DEC_S1_RESULT_NONE;*/
10225 	}
10226 
10227 }
10228 
10229 static void run_back(struct vdec_s *vdec)
10230 {
10231 	struct VP9Decoder_s *pbi =
10232 		(struct VP9Decoder_s *)vdec->private;
10233 	int i;
10234 	run2_count[pbi->index]++;
10235 	if (debug & PRINT_FLAG_VDEC_STATUS) {
10236 		vp9_print(pbi, 0,
10237 			"%s", __func__);
10238 	}
10239 	pbi->run2_busy = 1;
10240 #ifndef FB_DECODING_TEST_SCHEDULE
10241 	fb_reset_core(vdec, HW_MASK_BACK);
10242 
10243 	vvp9_prot_init(pbi, HW_MASK_BACK);
10244 #endif
10245 	vp9_recycle_mmu_buf_tail(pbi);
10246 
10247 	if (pbi->frame_count > 0)
10248 		vp9_bufmgr_postproc(pbi);
10249 
10250 	if (get_s2_buf(pbi) >= 0) {
10251 		for (i = 0; i < (RPM_END - RPM_BEGIN); i += 4) {
10252 			int ii;
10253 			for (ii = 0; ii < 4; ii++)
10254 				pbi->vp9_param.l.data[i + ii] =
10255 					pbi->s2_buf->rpm[i + 3 - ii];
10256 		}
10257 #ifndef FB_DECODING_TEST_SCHEDULE
10258 		WRITE_VREG(HEVC_ASSIST_FBD_MMU_MAP_ADDR,
10259 			pbi->stage_mmu_map_phy_addr +
10260 			pbi->s2_buf->index * STAGE_MMU_MAP_SIZE);
10261 #endif
10262 		continue_decoding(pbi);
10263 	}
10264 	pbi->run2_busy = 0;
10265 }
10266 #endif
10267 
10268 static void run(struct vdec_s *vdec, unsigned long mask,
10269 	void (*callback)(struct vdec_s *, void *), void *arg)
10270 {
10271 	struct VP9Decoder_s *pbi =
10272 		(struct VP9Decoder_s *)vdec->private;
10273 
10274 	vp9_print(pbi,
10275 		PRINT_FLAG_VDEC_DETAIL, "%s mask %lx\r\n",
10276 		__func__, mask);
10277 
10278 	if (vdec->mvfrm)
10279 		vdec->mvfrm->hw_decode_start = local_clock();
10280 	run_count[pbi->index]++;
10281 	pbi->vdec_cb_arg = arg;
10282 	pbi->vdec_cb = callback;
10283 #ifdef SUPPORT_FB_DECODING
10284 	if ((mask & CORE_MASK_HEVC) ||
10285 		(mask & CORE_MASK_HEVC_FRONT))
10286 		run_front(vdec);
10287 
10288 	if ((pbi->used_stage_buf_num > 0)
10289 		&& (mask & CORE_MASK_HEVC_BACK))
10290 		run_back(vdec);
10291 #else
10292 	run_front(vdec);
10293 #endif
10294 
10295 }
10296 
10297 static void  init_frame_bufs(struct VP9Decoder_s *pbi)
10298 {
10299 	struct vdec_s *vdec = hw_to_vdec(pbi);
10300 	struct VP9_Common_s *const cm = &pbi->common;
10301 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
10302 	int i;
10303 
10304 	for (i = 0; i < pbi->used_buf_num; ++i) {
10305 		frame_bufs[i].ref_count = 0;
10306 		frame_bufs[i].buf.vf_ref = 0;
10307 		frame_bufs[i].buf.decode_idx = 0;
10308 		frame_bufs[i].buf.cma_alloc_addr = 0;
10309 		frame_bufs[i].buf.index = i;
10310 		frame_bufs[i].buf.vframe_bound = 0;
10311 	}
10312 
10313 	if (vdec->parallel_dec == 1) {
10314 		for (i = 0; i < FRAME_BUFFERS; i++) {
10315 			vdec->free_canvas_ex
10316 				(pbi->common.buffer_pool->frame_bufs[i].buf.y_canvas_index,
10317 				vdec->id);
10318 			vdec->free_canvas_ex
10319 				(pbi->common.buffer_pool->frame_bufs[i].buf.uv_canvas_index,
10320 				vdec->id);
10321 		}
10322 	}
10323 }
10324 
10325 static void reset(struct vdec_s *vdec)
10326 {
10327 	struct VP9Decoder_s *pbi =
10328 		(struct VP9Decoder_s *)vdec->private;
10329 
10330 	cancel_work_sync(&pbi->work);
10331 	if (pbi->stat & STAT_VDEC_RUN) {
10332 		amhevc_stop();
10333 		pbi->stat &= ~STAT_VDEC_RUN;
10334 	}
10335 
10336 	if (pbi->stat & STAT_TIMER_ARM) {
10337 		del_timer_sync(&pbi->timer);
10338 		pbi->stat &= ~STAT_TIMER_ARM;
10339 	}
10340 	pbi->dec_result = DEC_RESULT_NONE;
10341 	reset_process_time(pbi);
10342 	vp9_local_uninit(pbi);
10343 	if (vvp9_local_init(pbi) < 0)
10344 		vp9_print(pbi, 0, "%s	local_init failed \r\n", __func__);
10345 	init_frame_bufs(pbi);
10346 
10347 	pbi->eos = 0;
10348 
10349 	vp9_print(pbi, PRINT_FLAG_VDEC_DETAIL, "%s\r\n", __func__);
10350 }
10351 
10352 static irqreturn_t vp9_irq_cb(struct vdec_s *vdec, int irq)
10353 {
10354 	struct VP9Decoder_s *pbi =
10355 		(struct VP9Decoder_s *)vdec->private;
10356 	return vvp9_isr(0, pbi);
10357 }
10358 
10359 static irqreturn_t vp9_threaded_irq_cb(struct vdec_s *vdec, int irq)
10360 {
10361 	struct VP9Decoder_s *pbi =
10362 		(struct VP9Decoder_s *)vdec->private;
10363 	return vvp9_isr_thread_fn(0, pbi);
10364 }
10365 
10366 static void vp9_dump_state(struct vdec_s *vdec)
10367 {
10368 	struct VP9Decoder_s *pbi =
10369 		(struct VP9Decoder_s *)vdec->private;
10370 	struct VP9_Common_s *const cm = &pbi->common;
10371 	int i;
10372 	vp9_print(pbi, 0, "====== %s\n", __func__);
10373 
10374 	vp9_print(pbi, 0,
10375 		"width/height (%d/%d), used_buf_num %d\n",
10376 		cm->width,
10377 		cm->height,
10378 		pbi->used_buf_num
10379 		);
10380 
10381 	vp9_print(pbi, 0,
10382 		"is_framebase(%d), eos %d, dec_result 0x%x dec_frm %d disp_frm %d run %d not_run_ready %d input_empty %d low_latency %d no_head %d \n",
10383 		input_frame_based(vdec),
10384 		pbi->eos,
10385 		pbi->dec_result,
10386 		decode_frame_count[pbi->index],
10387 		display_frame_count[pbi->index],
10388 		run_count[pbi->index],
10389 		not_run_ready[pbi->index],
10390 		input_empty[pbi->index],
10391 		pbi->low_latency_flag,
10392 		pbi->no_head
10393 		);
10394 
10395 	if (vf_get_receiver(vdec->vf_provider_name)) {
10396 		enum receviver_start_e state =
10397 		vf_notify_receiver(vdec->vf_provider_name,
10398 			VFRAME_EVENT_PROVIDER_QUREY_STATE,
10399 			NULL);
10400 		vp9_print(pbi, 0,
10401 			"\nreceiver(%s) state %d\n",
10402 			vdec->vf_provider_name,
10403 			state);
10404 	}
10405 
10406 	vp9_print(pbi, 0,
10407 	"%s, newq(%d/%d), dispq(%d/%d), vf prepare/get/put (%d/%d/%d), free_buf_count %d (min %d for run_ready)\n",
10408 	__func__,
10409 	kfifo_len(&pbi->newframe_q),
10410 	VF_POOL_SIZE,
10411 	kfifo_len(&pbi->display_q),
10412 	VF_POOL_SIZE,
10413 	pbi->vf_pre_count,
10414 	pbi->vf_get_count,
10415 	pbi->vf_put_count,
10416 	get_free_buf_count(pbi),
10417 	run_ready_min_buf_num
10418 	);
10419 
10420 	dump_pic_list(pbi);
10421 
10422 	for (i = 0; i < MAX_BUF_NUM; i++) {
10423 		vp9_print(pbi, 0,
10424 			"mv_Buf(%d) start_adr 0x%x size 0x%x used %d\n",
10425 			i,
10426 			pbi->m_mv_BUF[i].start_adr,
10427 			pbi->m_mv_BUF[i].size,
10428 			pbi->m_mv_BUF[i].used_flag);
10429 	}
10430 
10431 	vp9_print(pbi, 0,
10432 		"HEVC_DEC_STATUS_REG=0x%x\n",
10433 		READ_VREG(HEVC_DEC_STATUS_REG));
10434 	vp9_print(pbi, 0,
10435 		"HEVC_MPC_E=0x%x\n",
10436 		READ_VREG(HEVC_MPC_E));
10437 	vp9_print(pbi, 0,
10438 		"DECODE_MODE=0x%x\n",
10439 		READ_VREG(DECODE_MODE));
10440 	vp9_print(pbi, 0,
10441 		"NAL_SEARCH_CTL=0x%x\n",
10442 		READ_VREG(NAL_SEARCH_CTL));
10443 	vp9_print(pbi, 0,
10444 		"HEVC_PARSER_LCU_START=0x%x\n",
10445 		READ_VREG(HEVC_PARSER_LCU_START));
10446 	vp9_print(pbi, 0,
10447 		"HEVC_DECODE_SIZE=0x%x\n",
10448 		READ_VREG(HEVC_DECODE_SIZE));
10449 	vp9_print(pbi, 0,
10450 		"HEVC_SHIFT_BYTE_COUNT=0x%x\n",
10451 		READ_VREG(HEVC_SHIFT_BYTE_COUNT));
10452 	vp9_print(pbi, 0,
10453 		"HEVC_STREAM_START_ADDR=0x%x\n",
10454 		READ_VREG(HEVC_STREAM_START_ADDR));
10455 	vp9_print(pbi, 0,
10456 		"HEVC_STREAM_END_ADDR=0x%x\n",
10457 		READ_VREG(HEVC_STREAM_END_ADDR));
10458 	vp9_print(pbi, 0,
10459 		"HEVC_STREAM_LEVEL=0x%x\n",
10460 		READ_VREG(HEVC_STREAM_LEVEL));
10461 	vp9_print(pbi, 0,
10462 		"HEVC_STREAM_WR_PTR=0x%x\n",
10463 		READ_VREG(HEVC_STREAM_WR_PTR));
10464 	vp9_print(pbi, 0,
10465 		"HEVC_STREAM_RD_PTR=0x%x\n",
10466 		READ_VREG(HEVC_STREAM_RD_PTR));
10467 	vp9_print(pbi, 0,
10468 		"PARSER_VIDEO_RP=0x%x\n",
10469 		STBUF_READ(&vdec->vbuf, get_rp));
10470 	vp9_print(pbi, 0,
10471 		"PARSER_VIDEO_WP=0x%x\n",
10472 		STBUF_READ(&vdec->vbuf, get_wp));
10473 
10474 	if (input_frame_based(vdec) &&
10475 		(debug & PRINT_FLAG_VDEC_DATA)
10476 		) {
10477 		int jj;
10478 		if (pbi->chunk && pbi->chunk->block &&
10479 			pbi->chunk->size > 0) {
10480 			u8 *data = NULL;
10481 
10482 			if (!pbi->chunk->block->is_mapped)
10483 				data = codec_mm_vmap(
10484 					pbi->chunk->block->start +
10485 					pbi->chunk->offset,
10486 					pbi->chunk->size);
10487 			else
10488 				data = ((u8 *)pbi->chunk->block->start_virt)
10489 					+ pbi->chunk->offset;
10490 			vp9_print(pbi, 0,
10491 				"frame data size 0x%x\n",
10492 				pbi->chunk->size);
10493 			for (jj = 0; jj < pbi->chunk->size; jj++) {
10494 				if ((jj & 0xf) == 0)
10495 					vp9_print(pbi, 0,
10496 						"%06x:", jj);
10497 				vp9_print_cont(pbi, 0,
10498 					"%02x ", data[jj]);
10499 				if (((jj + 1) & 0xf) == 0)
10500 					vp9_print_cont(pbi, 0,
10501 						"\n");
10502 			}
10503 
10504 			if (!pbi->chunk->block->is_mapped)
10505 				codec_mm_unmap_phyaddr(data);
10506 		}
10507 	}
10508 
10509 }
10510 
10511 static int ammvdec_vp9_probe(struct platform_device *pdev)
10512 {
10513 	struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
10514 	int ret;
10515 	int config_val;
10516 	struct vframe_content_light_level_s content_light_level;
10517 	struct vframe_master_display_colour_s vf_dp;
10518 
10519 	struct BUF_s BUF[MAX_BUF_NUM];
10520 	struct VP9Decoder_s *pbi = NULL;
10521 	pr_debug("%s\n", __func__);
10522 
10523 	if (pdata == NULL) {
10524 		pr_info("\nammvdec_vp9 memory resource undefined.\n");
10525 		return -EFAULT;
10526 	}
10527 	/*pbi = (struct VP9Decoder_s *)devm_kzalloc(&pdev->dev,
10528 		sizeof(struct VP9Decoder_s), GFP_KERNEL);*/
10529 	memset(&vf_dp, 0, sizeof(struct vframe_master_display_colour_s));
10530 	pbi = vmalloc(sizeof(struct VP9Decoder_s));
10531 	if (pbi == NULL) {
10532 		pr_info("\nammvdec_vp9 device data allocation failed\n");
10533 		return -ENOMEM;
10534 	}
10535 	memset(pbi, 0, sizeof(struct VP9Decoder_s));
10536 
10537 	/* the ctx from v4l2 driver. */
10538 	pbi->v4l2_ctx = pdata->private;
10539 
10540 	pdata->private = pbi;
10541 	pdata->dec_status = vvp9_dec_status;
10542 	/* pdata->set_trickmode = set_trickmode; */
10543 	pdata->run_ready = run_ready;
10544 	pdata->run = run;
10545 	pdata->reset = reset;
10546 	pdata->irq_handler = vp9_irq_cb;
10547 	pdata->threaded_irq_handler = vp9_threaded_irq_cb;
10548 	pdata->dump_state = vp9_dump_state;
10549 
10550 	memcpy(&BUF[0], &pbi->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
10551 	memcpy(&pbi->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
10552 
10553 	pbi->index = pdev->id;
10554 
10555 	if (pdata->use_vfm_path)
10556 		snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
10557 			VFM_DEC_PROVIDER_NAME);
10558 	else
10559 		snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
10560 			MULTI_INSTANCE_PROVIDER_NAME ".%02x", pdev->id & 0xff);
10561 
10562 	vf_provider_init(&pdata->vframe_provider, pdata->vf_provider_name,
10563 		&vvp9_vf_provider, pbi);
10564 
10565 	pbi->provider_name = pdata->vf_provider_name;
10566 	platform_set_drvdata(pdev, pdata);
10567 
10568 	pbi->platform_dev = pdev;
10569 	pbi->video_signal_type = 0;
10570 	pbi->m_ins_flag = 1;
10571 	if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_TXLX)
10572 		pbi->stat |= VP9_TRIGGER_FRAME_ENABLE;
10573 
10574 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
10575 		pbi->max_pic_w = 8192;
10576 		pbi->max_pic_h = 4608;
10577 	} else {
10578 		pbi->max_pic_w = 4096;
10579 		pbi->max_pic_h = 2304;
10580 	}
10581 #if 1
10582 	if ((debug & IGNORE_PARAM_FROM_CONFIG) == 0 &&
10583 			pdata->config_len) {
10584 #ifdef MULTI_INSTANCE_SUPPORT
10585 		int vp9_buf_width = 0;
10586 		int vp9_buf_height = 0;
10587 		/*use ptr config for doubel_write_mode, etc*/
10588 		vp9_print(pbi, 0, "pdata->config=%s\n", pdata->config);
10589 		if (get_config_int(pdata->config, "vp9_double_write_mode",
10590 				&config_val) == 0)
10591 			pbi->double_write_mode = config_val;
10592 		else
10593 			pbi->double_write_mode = double_write_mode;
10594 
10595 		if (get_config_int(pdata->config, "save_buffer_mode",
10596 				&config_val) == 0)
10597 			pbi->save_buffer_mode = config_val;
10598 		else
10599 			pbi->save_buffer_mode = 0;
10600 		if (get_config_int(pdata->config, "vp9_buf_width",
10601 				&config_val) == 0) {
10602 			vp9_buf_width = config_val;
10603 		}
10604 		if (get_config_int(pdata->config, "vp9_buf_height",
10605 				&config_val) == 0) {
10606 			vp9_buf_height = config_val;
10607 		}
10608 
10609 		if (get_config_int(pdata->config, "no_head",
10610 				&config_val) == 0)
10611 			pbi->no_head = config_val;
10612 		else
10613 			pbi->no_head = no_head;
10614 
10615 		/*use ptr config for max_pic_w, etc*/
10616 		if (get_config_int(pdata->config, "vp9_max_pic_w",
10617 				&config_val) == 0) {
10618 				pbi->max_pic_w = config_val;
10619 		}
10620 		if (get_config_int(pdata->config, "vp9_max_pic_h",
10621 				&config_val) == 0) {
10622 				pbi->max_pic_h = config_val;
10623 		}
10624 		if ((pbi->max_pic_w * pbi->max_pic_h)
10625 			< (vp9_buf_width * vp9_buf_height)) {
10626 			pbi->max_pic_w = vp9_buf_width;
10627 			pbi->max_pic_h = vp9_buf_height;
10628 			vp9_print(pbi, 0, "use buf resolution\n");
10629 		}
10630 
10631 		if (get_config_int(pdata->config, "sidebind_type",
10632 				&config_val) == 0)
10633 			pbi->sidebind_type = config_val;
10634 
10635 		if (get_config_int(pdata->config, "sidebind_channel_id",
10636 				&config_val) == 0)
10637 			pbi->sidebind_channel_id = config_val;
10638 
10639 		if (get_config_int(pdata->config,
10640 			"parm_v4l_codec_enable",
10641 			&config_val) == 0)
10642 			pbi->is_used_v4l = config_val;
10643 
10644 		if (get_config_int(pdata->config,
10645 			"parm_v4l_buffer_margin",
10646 			&config_val) == 0)
10647 			pbi->dynamic_buf_num_margin = config_val;
10648 
10649 		if (get_config_int(pdata->config,
10650 			"parm_v4l_canvas_mem_mode",
10651 			&config_val) == 0)
10652 			pbi->mem_map_mode = config_val;
10653 #endif
10654 		if (get_config_int(pdata->config, "HDRStaticInfo",
10655 				&vf_dp.present_flag) == 0
10656 				&& vf_dp.present_flag == 1) {
10657 			get_config_int(pdata->config, "mG.x",
10658 					&vf_dp.primaries[0][0]);
10659 			get_config_int(pdata->config, "mG.y",
10660 					&vf_dp.primaries[0][1]);
10661 			get_config_int(pdata->config, "mB.x",
10662 					&vf_dp.primaries[1][0]);
10663 			get_config_int(pdata->config, "mB.y",
10664 					&vf_dp.primaries[1][1]);
10665 			get_config_int(pdata->config, "mR.x",
10666 					&vf_dp.primaries[2][0]);
10667 			get_config_int(pdata->config, "mR.y",
10668 					&vf_dp.primaries[2][1]);
10669 			get_config_int(pdata->config, "mW.x",
10670 					&vf_dp.white_point[0]);
10671 			get_config_int(pdata->config, "mW.y",
10672 					&vf_dp.white_point[1]);
10673 			get_config_int(pdata->config, "mMaxDL",
10674 					&vf_dp.luminance[0]);
10675 			get_config_int(pdata->config, "mMinDL",
10676 					&vf_dp.luminance[1]);
10677 			vf_dp.content_light_level.present_flag = 1;
10678 			get_config_int(pdata->config, "mMaxCLL",
10679 					&content_light_level.max_content);
10680 			get_config_int(pdata->config, "mMaxFALL",
10681 					&content_light_level.max_pic_average);
10682 			vf_dp.content_light_level = content_light_level;
10683 			pbi->video_signal_type = (1 << 29)
10684 					| (5 << 26)	/* unspecified */
10685 					| (0 << 25)	/* limit */
10686 					| (1 << 24)	/* color available */
10687 					| (9 << 16)	/* 2020 */
10688 					| (16 << 8)	/* 2084 */
10689 					| (9 << 0);	/* 2020 */
10690 		}
10691 		pbi->vf_dp = vf_dp;
10692 	} else
10693 #endif
10694 	{
10695 		/*pbi->vvp9_amstream_dec_info.width = 0;
10696 		pbi->vvp9_amstream_dec_info.height = 0;
10697 		pbi->vvp9_amstream_dec_info.rate = 30;*/
10698 		pbi->double_write_mode = double_write_mode;
10699 	}
10700 
10701 	if (!pbi->is_used_v4l) {
10702 		pbi->mem_map_mode = mem_map_mode;
10703 	}
10704 
10705 	if (is_oversize(pbi->max_pic_w, pbi->max_pic_h)) {
10706 		pr_err("over size: %dx%d, probe failed\n",
10707 			pbi->max_pic_w, pbi->max_pic_h);
10708 		return -1;
10709 	}
10710 	pbi->mmu_enable = 1;
10711 	video_signal_type = pbi->video_signal_type;
10712 
10713 	if (pdata->sys_info) {
10714 		pbi->vvp9_amstream_dec_info = *pdata->sys_info;
10715 	} else {
10716 		pbi->vvp9_amstream_dec_info.width = 0;
10717 		pbi->vvp9_amstream_dec_info.height = 0;
10718 		pbi->vvp9_amstream_dec_info.rate = 30;
10719 	}
10720 	pbi->low_latency_flag = 1;
10721 
10722 	vp9_print(pbi, 0,
10723 			"no_head %d  low_latency %d\n",
10724 			pbi->no_head, pbi->low_latency_flag);
10725 #if 0
10726 	pbi->buf_start = pdata->mem_start;
10727 	pbi->buf_size = pdata->mem_end - pdata->mem_start + 1;
10728 #else
10729 	if (amvdec_vp9_mmu_init(pbi) < 0) {
10730 		pr_err("vp9 alloc bmmu box failed!!\n");
10731 		/* devm_kfree(&pdev->dev, (void *)pbi); */
10732 		vfree((void *)pbi);
10733 		pdata->dec_status = NULL;
10734 		return -1;
10735 	}
10736 
10737 	pbi->cma_alloc_count = PAGE_ALIGN(work_buf_size) / PAGE_SIZE;
10738 	ret = decoder_bmmu_box_alloc_buf_phy(pbi->bmmu_box, WORK_SPACE_BUF_ID,
10739 			pbi->cma_alloc_count * PAGE_SIZE, DRIVER_NAME,
10740 			&pbi->cma_alloc_addr);
10741 	if (ret < 0) {
10742 		uninit_mmu_buffers(pbi);
10743 		/* devm_kfree(&pdev->dev, (void *)pbi); */
10744 		vfree((void *)pbi);
10745 		pdata->dec_status = NULL;
10746 		return ret;
10747 	}
10748 	pbi->buf_start = pbi->cma_alloc_addr;
10749 	pbi->buf_size = work_buf_size;
10750 #endif
10751 
10752 	pbi->init_flag = 0;
10753 	pbi->first_sc_checked = 0;
10754 	pbi->fatal_error = 0;
10755 	pbi->show_frame_num = 0;
10756 
10757 	if (debug) {
10758 		pr_info("===VP9 decoder mem resource 0x%lx size 0x%x\n",
10759 			   pbi->buf_start,
10760 			   pbi->buf_size);
10761 	}
10762 
10763 	pbi->cma_dev = pdata->cma_dev;
10764 	if (vvp9_init(pdata) < 0) {
10765 		pr_info("\namvdec_vp9 init failed.\n");
10766 		vp9_local_uninit(pbi);
10767 		uninit_mmu_buffers(pbi);
10768 		/* devm_kfree(&pdev->dev, (void *)pbi); */
10769 		vfree((void *)pbi);
10770 		pdata->dec_status = NULL;
10771 		return -ENODEV;
10772 	}
10773 	vdec_set_prepare_level(pdata, start_decode_buf_level);
10774 	hevc_source_changed(VFORMAT_VP9,
10775 			4096, 2048, 60);
10776 #ifdef SUPPORT_FB_DECODING
10777 	if (pbi->used_stage_buf_num > 0)
10778 		vdec_core_request(pdata,
10779 			CORE_MASK_HEVC_FRONT | CORE_MASK_HEVC_BACK);
10780 	else
10781 		vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
10782 			| CORE_MASK_HEVC_FRONT | CORE_MASK_HEVC_BACK
10783 					| CORE_MASK_COMBINE);
10784 #else
10785 	if (pdata->parallel_dec == 1)
10786 		vdec_core_request(pdata, CORE_MASK_HEVC);
10787 	else
10788 		vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
10789 					| CORE_MASK_COMBINE);
10790 #endif
10791 	pbi->pic_list_init_done2 = true;
10792 	return 0;
10793 }
10794 
10795 static int ammvdec_vp9_remove(struct platform_device *pdev)
10796 {
10797 	struct VP9Decoder_s *pbi = (struct VP9Decoder_s *)
10798 		(((struct vdec_s *)(platform_get_drvdata(pdev)))->private);
10799 	struct vdec_s *vdec = hw_to_vdec(pbi);
10800 	int i;
10801 	if (debug)
10802 		pr_info("amvdec_vp9_remove\n");
10803 
10804 	vmvp9_stop(pbi);
10805 
10806 #ifdef SUPPORT_FB_DECODING
10807 	vdec_core_release(hw_to_vdec(pbi), CORE_MASK_VDEC_1 | CORE_MASK_HEVC
10808 		| CORE_MASK_HEVC_FRONT | CORE_MASK_HEVC_BACK
10809 		);
10810 #else
10811 	if (vdec->parallel_dec == 1)
10812 		vdec_core_release(hw_to_vdec(pbi), CORE_MASK_HEVC);
10813 	else
10814 		vdec_core_release(hw_to_vdec(pbi), CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
10815 #endif
10816 	vdec_set_status(hw_to_vdec(pbi), VDEC_STATUS_DISCONNECTED);
10817 
10818 	if (vdec->parallel_dec == 1) {
10819 		for (i = 0; i < FRAME_BUFFERS; i++) {
10820 			vdec->free_canvas_ex
10821 				(pbi->common.buffer_pool->frame_bufs[i].buf.y_canvas_index,
10822 				vdec->id);
10823 			vdec->free_canvas_ex
10824 				(pbi->common.buffer_pool->frame_bufs[i].buf.uv_canvas_index,
10825 				vdec->id);
10826 		}
10827 	}
10828 
10829 
10830 #ifdef DEBUG_PTS
10831 	pr_info("pts missed %ld, pts hit %ld, duration %d\n",
10832 		   pbi->pts_missed, pbi->pts_hit, pbi->frame_dur);
10833 #endif
10834 	mem_map_mode = 0;
10835 
10836 	/* devm_kfree(&pdev->dev, (void *)pbi); */
10837 	vfree((void *)pbi);
10838 	return 0;
10839 }
10840 
10841 static struct platform_driver ammvdec_vp9_driver = {
10842 	.probe = ammvdec_vp9_probe,
10843 	.remove = ammvdec_vp9_remove,
10844 	.driver = {
10845 		.name = MULTI_DRIVER_NAME,
10846 #ifdef CONFIG_PM
10847 		.pm = &vp9_pm_ops,
10848 #endif
10849 	}
10850 };
10851 #endif
10852 static struct mconfig vp9_configs[] = {
10853 	MC_PU32("bit_depth_luma", &bit_depth_luma),
10854 	MC_PU32("bit_depth_chroma", &bit_depth_chroma),
10855 	MC_PU32("frame_width", &frame_width),
10856 	MC_PU32("frame_height", &frame_height),
10857 	MC_PU32("debug", &debug),
10858 	MC_PU32("radr", &radr),
10859 	MC_PU32("rval", &rval),
10860 	MC_PU32("pop_shorts", &pop_shorts),
10861 	MC_PU32("dbg_cmd", &dbg_cmd),
10862 	MC_PU32("dbg_skip_decode_index", &dbg_skip_decode_index),
10863 	MC_PU32("endian", &endian),
10864 	MC_PU32("step", &step),
10865 	MC_PU32("udebug_flag", &udebug_flag),
10866 	MC_PU32("decode_pic_begin", &decode_pic_begin),
10867 	MC_PU32("slice_parse_begin", &slice_parse_begin),
10868 	MC_PU32("i_only_flag", &i_only_flag),
10869 	MC_PU32("error_handle_policy", &error_handle_policy),
10870 	MC_PU32("buf_alloc_width", &buf_alloc_width),
10871 	MC_PU32("buf_alloc_height", &buf_alloc_height),
10872 	MC_PU32("buf_alloc_depth", &buf_alloc_depth),
10873 	MC_PU32("buf_alloc_size", &buf_alloc_size),
10874 	MC_PU32("buffer_mode", &buffer_mode),
10875 	MC_PU32("buffer_mode_dbg", &buffer_mode_dbg),
10876 	MC_PU32("max_buf_num", &max_buf_num),
10877 	MC_PU32("dynamic_buf_num_margin", &dynamic_buf_num_margin),
10878 	MC_PU32("mem_map_mode", &mem_map_mode),
10879 	MC_PU32("double_write_mode", &double_write_mode),
10880 	MC_PU32("enable_mem_saving", &enable_mem_saving),
10881 	MC_PU32("force_w_h", &force_w_h),
10882 	MC_PU32("force_fps", &force_fps),
10883 	MC_PU32("max_decoding_time", &max_decoding_time),
10884 	MC_PU32("on_no_keyframe_skiped", &on_no_keyframe_skiped),
10885 	MC_PU32("start_decode_buf_level", &start_decode_buf_level),
10886 	MC_PU32("decode_timeout_val", &decode_timeout_val),
10887 	MC_PU32("vp9_max_pic_w", &vp9_max_pic_w),
10888 	MC_PU32("vp9_max_pic_h", &vp9_max_pic_h),
10889 };
10890 static struct mconfig_node vp9_node;
10891 
10892 static int __init amvdec_vp9_driver_init_module(void)
10893 {
10894 
10895 	struct BuffInfo_s *p_buf_info;
10896 
10897 	if (vdec_is_support_4k()) {
10898 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
10899 			p_buf_info = &amvvp9_workbuff_spec[2];
10900 		else
10901 			p_buf_info = &amvvp9_workbuff_spec[1];
10902 	} else
10903 		p_buf_info = &amvvp9_workbuff_spec[0];
10904 
10905 	init_buff_spec(NULL, p_buf_info);
10906 	work_buf_size =
10907 		(p_buf_info->end_adr - p_buf_info->start_adr
10908 		 + 0xffff) & (~0xffff);
10909 
10910 	pr_debug("amvdec_vp9 module init\n");
10911 
10912 	error_handle_policy = 0;
10913 
10914 #ifdef ERROR_HANDLE_DEBUG
10915 	dbg_nal_skip_flag = 0;
10916 	dbg_nal_skip_count = 0;
10917 #endif
10918 	udebug_flag = 0;
10919 	decode_pic_begin = 0;
10920 	slice_parse_begin = 0;
10921 	step = 0;
10922 	buf_alloc_size = 0;
10923 #ifdef MULTI_INSTANCE_SUPPORT
10924 	if (platform_driver_register(&ammvdec_vp9_driver))
10925 		pr_err("failed to register ammvdec_vp9 driver\n");
10926 
10927 #endif
10928 	if (platform_driver_register(&amvdec_vp9_driver)) {
10929 		pr_err("failed to register amvdec_vp9 driver\n");
10930 		return -ENODEV;
10931 	}
10932 
10933 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
10934 		amvdec_vp9_profile.profile =
10935 				"8k, 10bit, dwrite, compressed, no_head";
10936 	} else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_GXL
10937 		/*&& get_cpu_major_id() != MESON_CPU_MAJOR_ID_GXLX*/
10938 		&& get_cpu_major_id() != AM_MESON_CPU_MAJOR_ID_TXL) {
10939 			if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_TXLX) {
10940 				if (vdec_is_support_4k())
10941 					amvdec_vp9_profile.profile =
10942 						"4k, 10bit, dwrite, compressed";
10943 				else
10944 					amvdec_vp9_profile.profile =
10945 						"10bit, dwrite, compressed";
10946 			} else {
10947 				if (vdec_is_support_4k())
10948 					amvdec_vp9_profile.profile =
10949 						"4k, 10bit, dwrite, compressed, no_head";
10950 				else
10951 					amvdec_vp9_profile.profile =
10952 						"10bit, dwrite, compressed, no_head";
10953 			}
10954 
10955 	} else {
10956 		amvdec_vp9_profile.name = "vp9_unsupport";
10957 	}
10958 
10959 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A)
10960 		max_buf_num = MAX_BUF_NUM_LESS;
10961 
10962 	vcodec_profile_register(&amvdec_vp9_profile);
10963 	amvdec_vp9_profile_mult = amvdec_vp9_profile;
10964 	amvdec_vp9_profile_mult.name = "mvp9";
10965 	vcodec_profile_register(&amvdec_vp9_profile_mult);
10966 	INIT_REG_NODE_CONFIGS("media.decoder", &vp9_node,
10967 		"vp9", vp9_configs, CONFIG_FOR_RW);
10968 
10969 	return 0;
10970 }
10971 
10972 static void __exit amvdec_vp9_driver_remove_module(void)
10973 {
10974 	pr_debug("amvdec_vp9 module remove.\n");
10975 #ifdef MULTI_INSTANCE_SUPPORT
10976 	platform_driver_unregister(&ammvdec_vp9_driver);
10977 #endif
10978 	platform_driver_unregister(&amvdec_vp9_driver);
10979 }
10980 
10981 /****************************************/
10982 
10983 module_param(bit_depth_luma, uint, 0664);
10984 MODULE_PARM_DESC(bit_depth_luma, "\n amvdec_vp9 bit_depth_luma\n");
10985 
10986 module_param(bit_depth_chroma, uint, 0664);
10987 MODULE_PARM_DESC(bit_depth_chroma, "\n amvdec_vp9 bit_depth_chroma\n");
10988 
10989 module_param(frame_width, uint, 0664);
10990 MODULE_PARM_DESC(frame_width, "\n amvdec_vp9 frame_width\n");
10991 
10992 module_param(frame_height, uint, 0664);
10993 MODULE_PARM_DESC(frame_height, "\n amvdec_vp9 frame_height\n");
10994 
10995 module_param(debug, uint, 0664);
10996 MODULE_PARM_DESC(debug, "\n amvdec_vp9 debug\n");
10997 
10998 module_param(radr, uint, 0664);
10999 MODULE_PARM_DESC(radr, "\n radr\n");
11000 
11001 module_param(rval, uint, 0664);
11002 MODULE_PARM_DESC(rval, "\n rval\n");
11003 
11004 module_param(pop_shorts, uint, 0664);
11005 MODULE_PARM_DESC(pop_shorts, "\n rval\n");
11006 
11007 module_param(dbg_cmd, uint, 0664);
11008 MODULE_PARM_DESC(dbg_cmd, "\n dbg_cmd\n");
11009 
11010 module_param(dbg_skip_decode_index, uint, 0664);
11011 MODULE_PARM_DESC(dbg_skip_decode_index, "\n dbg_skip_decode_index\n");
11012 
11013 module_param(endian, uint, 0664);
11014 MODULE_PARM_DESC(endian, "\n rval\n");
11015 
11016 module_param(step, uint, 0664);
11017 MODULE_PARM_DESC(step, "\n amvdec_vp9 step\n");
11018 
11019 module_param(decode_pic_begin, uint, 0664);
11020 MODULE_PARM_DESC(decode_pic_begin, "\n amvdec_vp9 decode_pic_begin\n");
11021 
11022 module_param(slice_parse_begin, uint, 0664);
11023 MODULE_PARM_DESC(slice_parse_begin, "\n amvdec_vp9 slice_parse_begin\n");
11024 
11025 module_param(i_only_flag, uint, 0664);
11026 MODULE_PARM_DESC(i_only_flag, "\n amvdec_vp9 i_only_flag\n");
11027 
11028 module_param(low_latency_flag, uint, 0664);
11029 MODULE_PARM_DESC(low_latency_flag, "\n amvdec_vp9 low_latency_flag\n");
11030 
11031 module_param(no_head, uint, 0664);
11032 MODULE_PARM_DESC(no_head, "\n amvdec_vp9 no_head\n");
11033 
11034 module_param(error_handle_policy, uint, 0664);
11035 MODULE_PARM_DESC(error_handle_policy, "\n amvdec_vp9 error_handle_policy\n");
11036 
11037 module_param(buf_alloc_width, uint, 0664);
11038 MODULE_PARM_DESC(buf_alloc_width, "\n buf_alloc_width\n");
11039 
11040 module_param(buf_alloc_height, uint, 0664);
11041 MODULE_PARM_DESC(buf_alloc_height, "\n buf_alloc_height\n");
11042 
11043 module_param(buf_alloc_depth, uint, 0664);
11044 MODULE_PARM_DESC(buf_alloc_depth, "\n buf_alloc_depth\n");
11045 
11046 module_param(buf_alloc_size, uint, 0664);
11047 MODULE_PARM_DESC(buf_alloc_size, "\n buf_alloc_size\n");
11048 
11049 module_param(buffer_mode, uint, 0664);
11050 MODULE_PARM_DESC(buffer_mode, "\n buffer_mode\n");
11051 
11052 module_param(buffer_mode_dbg, uint, 0664);
11053 MODULE_PARM_DESC(buffer_mode_dbg, "\n buffer_mode_dbg\n");
11054 /*USE_BUF_BLOCK*/
11055 module_param(max_buf_num, uint, 0664);
11056 MODULE_PARM_DESC(max_buf_num, "\n max_buf_num\n");
11057 
11058 module_param(dynamic_buf_num_margin, uint, 0664);
11059 MODULE_PARM_DESC(dynamic_buf_num_margin, "\n dynamic_buf_num_margin\n");
11060 
11061 module_param(mv_buf_margin, uint, 0664);
11062 MODULE_PARM_DESC(mv_buf_margin, "\n mv_buf_margin\n");
11063 
11064 module_param(run_ready_min_buf_num, uint, 0664);
11065 MODULE_PARM_DESC(run_ready_min_buf_num, "\n run_ready_min_buf_num\n");
11066 
11067 /**/
11068 
11069 module_param(mem_map_mode, uint, 0664);
11070 MODULE_PARM_DESC(mem_map_mode, "\n mem_map_mode\n");
11071 
11072 #ifdef SUPPORT_10BIT
11073 module_param(double_write_mode, uint, 0664);
11074 MODULE_PARM_DESC(double_write_mode, "\n double_write_mode\n");
11075 
11076 module_param(enable_mem_saving, uint, 0664);
11077 MODULE_PARM_DESC(enable_mem_saving, "\n enable_mem_saving\n");
11078 
11079 module_param(force_w_h, uint, 0664);
11080 MODULE_PARM_DESC(force_w_h, "\n force_w_h\n");
11081 #endif
11082 
11083 module_param(force_fps, uint, 0664);
11084 MODULE_PARM_DESC(force_fps, "\n force_fps\n");
11085 
11086 module_param(max_decoding_time, uint, 0664);
11087 MODULE_PARM_DESC(max_decoding_time, "\n max_decoding_time\n");
11088 
11089 module_param(on_no_keyframe_skiped, uint, 0664);
11090 MODULE_PARM_DESC(on_no_keyframe_skiped, "\n on_no_keyframe_skiped\n");
11091 
11092 module_param(mcrcc_cache_alg_flag, uint, 0664);
11093 MODULE_PARM_DESC(mcrcc_cache_alg_flag, "\n mcrcc_cache_alg_flag\n");
11094 
11095 #ifdef MULTI_INSTANCE_SUPPORT
11096 module_param(start_decode_buf_level, int, 0664);
11097 MODULE_PARM_DESC(start_decode_buf_level,
11098 		"\n vp9 start_decode_buf_level\n");
11099 
11100 module_param(decode_timeout_val, uint, 0664);
11101 MODULE_PARM_DESC(decode_timeout_val,
11102 	"\n vp9 decode_timeout_val\n");
11103 
11104 module_param(vp9_max_pic_w, uint, 0664);
11105 MODULE_PARM_DESC(vp9_max_pic_w, "\n vp9_max_pic_w\n");
11106 
11107 module_param(vp9_max_pic_h, uint, 0664);
11108 MODULE_PARM_DESC(vp9_max_pic_h, "\n vp9_max_pic_h\n");
11109 
11110 module_param_array(decode_frame_count, uint,
11111 	&max_decode_instance_num, 0664);
11112 
11113 module_param_array(display_frame_count, uint,
11114 	&max_decode_instance_num, 0664);
11115 
11116 module_param_array(max_process_time, uint,
11117 	&max_decode_instance_num, 0664);
11118 
11119 module_param_array(run_count, uint,
11120 	&max_decode_instance_num, 0664);
11121 
11122 module_param_array(input_empty, uint,
11123 	&max_decode_instance_num, 0664);
11124 
11125 module_param_array(not_run_ready, uint,
11126 	&max_decode_instance_num, 0664);
11127 #endif
11128 
11129 #ifdef SUPPORT_FB_DECODING
11130 module_param_array(not_run2_ready, uint,
11131 	&max_decode_instance_num, 0664);
11132 
11133 module_param_array(run2_count, uint,
11134 	&max_decode_instance_num, 0664);
11135 
11136 module_param(stage_buf_num, uint, 0664);
11137 MODULE_PARM_DESC(stage_buf_num, "\n amvdec_h265 stage_buf_num\n");
11138 #endif
11139 
11140 module_param(udebug_flag, uint, 0664);
11141 MODULE_PARM_DESC(udebug_flag, "\n amvdec_h265 udebug_flag\n");
11142 
11143 module_param(udebug_pause_pos, uint, 0664);
11144 MODULE_PARM_DESC(udebug_pause_pos, "\n udebug_pause_pos\n");
11145 
11146 module_param(udebug_pause_val, uint, 0664);
11147 MODULE_PARM_DESC(udebug_pause_val, "\n udebug_pause_val\n");
11148 
11149 module_param(udebug_pause_decode_idx, uint, 0664);
11150 MODULE_PARM_DESC(udebug_pause_decode_idx, "\n udebug_pause_decode_idx\n");
11151 
11152 module_param(without_display_mode, uint, 0664);
11153 MODULE_PARM_DESC(without_display_mode, "\n without_display_mode\n");
11154 
11155 module_init(amvdec_vp9_driver_init_module);
11156 module_exit(amvdec_vp9_driver_remove_module);
11157 
11158 MODULE_DESCRIPTION("AMLOGIC vp9 Video Decoder Driver");
11159 MODULE_LICENSE("GPL");
11160 
11161