• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * drivers/amlogic/amports/vav1.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/slab.h>
40 #include <linux/amlogic/tee.h>
41 #include "../../../stream_input/amports/amports_priv.h"
42 #include <linux/amlogic/media/codec_mm/codec_mm.h>
43 #include "../utils/decoder_mmu_box.h"
44 #include "../utils/decoder_bmmu_box.h"
45 
46 #define MEM_NAME "codec_av1"
47 /* #include <mach/am_regs.h> */
48 #include <linux/amlogic/media/utils/vdec_reg.h>
49 #include "../utils/vdec.h"
50 #include "../utils/amvdec.h"
51 #ifdef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
52 #include "../utils/vdec_profile.h"
53 #endif
54 
55 #include <linux/amlogic/media/video_sink/video.h>
56 #include <linux/amlogic/media/codec_mm/configs.h>
57 #include "../utils/config_parser.h"
58 #include "../utils/firmware.h"
59 #include "../../../common/chips/decoder_cpu_ver_info.h"
60 #include "../../../amvdec_ports/vdec_drv_base.h"
61 
62 #define SUPPORT_V4L2
63 //#define DEBUG_USE_VP9_DEVICE_NAME
64 //#define BUFMGR_ONLY_OLD_CHIP
65 
66 #ifdef SUPPORT_V4L2
67 #include "../utils/vdec_v4l2_buffer_ops.h"
68 #endif
69 
70 #define AML
71 #include "aom_av1_define.h"
72 #include "av1_global.h"
73 
74 #define DUMP_FILMGRAIN
75 #define MIX_STREAM_SUPPORT
76 //#define MV_USE_FIXED_BUF
77 //#define USE_SPEC_BUF_FOR_MMU_HEAD
78 
79 #define AOM_AV1_DBLK_INIT
80 #define AOM_AV1_UPSCALE_INIT
81 
82 #define USE_DEC_PIC_END
83 
84 
85 #include "vav1.h"
86 
87 #define FGS_TABLE_SIZE  (512 * 128 / 8)
88 
89 #define AV1_GMC_PARAM_BUFF_ADDR 	               0x316d
90 #define HEVCD_MPP_DECOMP_AXIURG_CTL                0x34c7
91 #define HEVC_FGS_IDX                               0x3660
92 #define HEVC_FGS_DATA                              0x3661
93 #define HEVC_FGS_CTRL                              0x3662
94 #define AV1_SKIP_MODE_INFO                         0x316c
95 #define AV1_QUANT_WR                               0x3146
96 #define   AV1_SEG_W_ADDR                           0x3165
97 #define   AV1_SEG_R_ADDR                           0x3166
98 #define AV1_REF_SEG_INFO                           0x3171
99 #define HEVC_ASSIST_PIC_SIZE_FB_READ               0x300d
100 #define PARSER_REF_SCALE_ENBL                      0x316b
101 #define HEVC_MPRED_MV_RPTR_1                       0x3263
102 #define HEVC_MPRED_MV_RPTR_2                       0x3264
103 #define HEVC_SAO_CTRL9                             0x362d
104 #define HEVC_FGS_TABLE_START                       0x3666
105 #define HEVC_FGS_TABLE_LENGTH                      0x3667
106 #define HEVC_DBLK_CDEF0                            0x3515
107 #define HEVC_DBLK_CDEF1                            0x3516
108 #define HEVC_DBLK_UPS1                             0x351c
109 #define HEVC_DBLK_UPS2                             0x351d
110 #define HEVC_DBLK_UPS3                             0x351e
111 #define HEVC_DBLK_UPS4                             0x351f
112 #define HEVC_DBLK_UPS5                             0x3520
113 #define AV1_UPSCALE_X0_QN                          0x316e
114 #define AV1_UPSCALE_STEP_QN                        0x316f
115 #define HEVC_DBLK_DBLK0                            0x3523
116 #define HEVC_DBLK_DBLK1                            0x3524
117 #define HEVC_DBLK_DBLK2                            0x3525
118 
119 #define HW_MASK_FRONT    0x1
120 #define HW_MASK_BACK     0x2
121 
122 #define AV1D_MPP_REFINFO_TBL_ACCCONFIG             0x3442
123 #define AV1D_MPP_REFINFO_DATA                      0x3443
124 #define AV1D_MPP_REF_SCALE_ENBL                    0x3441
125 #define HEVC_MPRED_CTRL4                           0x324c
126 #define HEVC_CM_HEADER_START_ADDR                  0x3628
127 #define HEVC_DBLK_CFGB                             0x350b
128 #define HEVCD_MPP_ANC2AXI_TBL_DATA                 0x3464
129 #define HEVC_SAO_MMU_VH1_ADDR                      0x363b
130 #define HEVC_SAO_MMU_VH0_ADDR                      0x363a
131 
132 #define HEVC_MV_INFO                               0x310d
133 #define HEVC_QP_INFO                               0x3137
134 #define HEVC_SKIP_INFO                             0x3136
135 
136 #define HEVC_CM_BODY_LENGTH2                       0x3663
137 #define HEVC_CM_HEADER_OFFSET2                     0x3664
138 #define HEVC_CM_HEADER_LENGTH2                     0x3665
139 
140 #define HEVC_CM_HEADER_START_ADDR2                 0x364a
141 #define HEVC_SAO_MMU_DMA_CTRL2                     0x364c
142 #define HEVC_SAO_MMU_VH0_ADDR2                     0x364d
143 #define HEVC_SAO_MMU_VH1_ADDR2                     0x364e
144 #define HEVC_SAO_MMU_STATUS2                       0x3650
145 #define HEVC_DW_VH0_ADDDR                          0x365e
146 #define HEVC_DW_VH1_ADDDR                          0x365f
147 
148 #ifdef BUFMGR_ONLY_OLD_CHIP
149 #undef AV1_SKIP_MODE_INFO
150 #define AV1_SKIP_MODE_INFO  HEVC_ASSIST_SCRATCH_B
151 #endif
152 
153 
154 #define AOM_AV1_DEC_IDLE                     0
155 #define AOM_AV1_DEC_FRAME_HEADER             1
156 #define AOM_AV1_DEC_TILE_END                 2
157 #define AOM_AV1_DEC_TG_END                   3
158 #define AOM_AV1_DEC_LCU_END                  4
159 #define AOM_AV1_DECODE_SLICE                 5
160 #define AOM_AV1_SEARCH_HEAD                  6
161 #define AOM_AV1_DUMP_LMEM                    7
162 #define AOM_AV1_FGS_PARAM_CONT               8
163 #define AOM_AV1_DISCARD_NAL                  0x10
164 
165 /*status*/
166 #define AOM_AV1_DEC_PIC_END                  0xe0
167 /*AOM_AV1_FGS_PARA:
168 Bit[11] - 0 Read, 1 - Write
169 Bit[10:8] - film_grain_params_ref_idx, For Write request
170 */
171 #define AOM_AV1_FGS_PARAM                    0xe1
172 #define AOM_AV1_DEC_PIC_END_PRE              0xe2
173 #define AOM_AV1_HEAD_PARSER_DONE          0xf0
174 #define AOM_AV1_HEAD_SEARCH_DONE          0xf1
175 #define AOM_AV1_SEQ_HEAD_PARSER_DONE      0xf2
176 #define AOM_AV1_FRAME_HEAD_PARSER_DONE    0xf3
177 #define AOM_AV1_FRAME_PARSER_DONE   0xf4
178 #define AOM_AV1_REDUNDANT_FRAME_HEAD_PARSER_DONE   0xf5
179 #define HEVC_ACTION_DONE            0xff
180 
181 #define AOM_DECODE_BUFEMPTY        0x20
182 #define AOM_DECODE_TIMEOUT         0x21
183 #define AOM_SEARCH_BUFEMPTY        0x22
184 #define AOM_DECODE_OVER_SIZE       0x23
185 #define AOM_EOS                     0x24
186 #define AOM_NAL_DECODE_DONE            0x25
187 
188 
189 #define VF_POOL_SIZE        32
190 
191 #undef pr_info
192 #define pr_info printk
193 
194 #define DECODE_MODE_SINGLE		((0x80 << 24) | 0)
195 #define DECODE_MODE_MULTI_STREAMBASE	((0x80 << 24) | 1)
196 #define DECODE_MODE_MULTI_FRAMEBASE	((0x80 << 24) | 2)
197 #define DECODE_MODE_SINGLE_LOW_LATENCY ((0x80 << 24) | 3)
198 #define DECODE_MODE_MULTI_FRAMEBASE_NOHEAD ((0x80 << 24) | 4)
199 
200 #define  AV1_TRIGGER_FRAME_DONE		0x100
201 #define  AV1_TRIGGER_FRAME_ENABLE	0x200
202 
203 #define MV_MEM_UNIT 0x240
204 /*---------------------------------------------------
205  * Include "parser_cmd.h"
206  *---------------------------------------------------
207  */
208 #define PARSER_CMD_SKIP_CFG_0 0x0000090b
209 
210 #define PARSER_CMD_SKIP_CFG_1 0x1b14140f
211 
212 #define PARSER_CMD_SKIP_CFG_2 0x001b1910
213 
214 #define PARSER_CMD_NUMBER 37
215 
216 /*#define HEVC_PIC_STRUCT_SUPPORT*/
217 /* to remove, fix build error */
218 
219 /*#define CODEC_MM_FLAGS_FOR_VDECODER  0*/
220 
221 #define MULTI_INSTANCE_SUPPORT
222 #define SUPPORT_10BIT
223 /* #define ERROR_HANDLE_DEBUG */
224 
225 #ifndef STAT_KTHREAD
226 #define STAT_KTHREAD 0x40
227 #endif
228 
229 #ifdef MULTI_INSTANCE_SUPPORT
230 #define MAX_DECODE_INSTANCE_NUM     9
231 
232 #ifdef DEBUG_USE_VP9_DEVICE_NAME
233 #define MULTI_DRIVER_NAME "ammvdec_vp9"
234 #else
235 #define MULTI_DRIVER_NAME "ammvdec_av1"
236 #endif
237 
238 #define AUX_BUF_ALIGN(adr) ((adr + 0xf) & (~0xf))
239 static u32 prefix_aux_buf_size = (16 * 1024);
240 static u32 suffix_aux_buf_size;
241 
242 static unsigned int max_decode_instance_num
243 				= MAX_DECODE_INSTANCE_NUM;
244 static unsigned int decode_frame_count[MAX_DECODE_INSTANCE_NUM];
245 static unsigned int display_frame_count[MAX_DECODE_INSTANCE_NUM];
246 static unsigned int max_process_time[MAX_DECODE_INSTANCE_NUM];
247 static unsigned int run_count[MAX_DECODE_INSTANCE_NUM];
248 static unsigned int input_empty[MAX_DECODE_INSTANCE_NUM];
249 static unsigned int not_run_ready[MAX_DECODE_INSTANCE_NUM];
250 #ifdef AOM_AV1_MMU_DW
251 static unsigned int dw_mmu_enable[MAX_DECODE_INSTANCE_NUM];
252 #endif
253 /* disable timeout for av1 mosaic JIRA SWPL-23326 */
254 static u32 decode_timeout_val = 0;
255 static int start_decode_buf_level = 0x8000;
256 static u32 work_buf_size;
257 static u32 force_pts_unstable;
258 static u32 mv_buf_margin = REF_FRAMES;
259 
260 /* DOUBLE_WRITE_MODE is enabled only when NV21 8 bit output is needed */
261 /* double_write_mode:
262  *	0, no double write;
263  *	1, 1:1 ratio;
264  *	2, (1/4):(1/4) ratio;
265  *	3, (1/4):(1/4) ratio, with both compressed frame included
266  *	4, (1/2):(1/2) ratio;
267  *  5, (1/2):(1/2) ratio, with both compressed frame included
268  *	0x10, double write only
269  *  0x20, mmu double write
270  *	0x100, if > 1080p,use mode 4,else use mode 1;
271  *	0x200, if > 1080p,use mode 2,else use mode 1;
272  *	0x300, if > 720p, use mode 4, else use mode 1;
273  */
274 static u32 double_write_mode;
275 
276 #ifdef DEBUG_USE_VP9_DEVICE_NAME
277 #define DRIVER_NAME "amvdec_vp9"
278 #define MODULE_NAME "amvdec_vp9"
279 #define DRIVER_HEADER_NAME "amvdec_vp9_header"
280 #else
281 #define DRIVER_NAME "amvdec_av1"
282 #define MODULE_NAME "amvdec_av1"
283 #define DRIVER_HEADER_NAME "amvdec_av1_header"
284 #endif
285 
286 #define PUT_INTERVAL        (HZ/100)
287 #define ERROR_SYSTEM_RESET_COUNT   200
288 
289 #define PTS_NORMAL                0
290 #define PTS_NONE_REF_USE_DURATION 1
291 
292 #define PTS_MODE_SWITCHING_THRESHOLD           3
293 #define PTS_MODE_SWITCHING_RECOVERY_THREASHOLD 3
294 
295 #define DUR2PTS(x) ((x)*90/96)
296 #define PTS2DUR(x) ((x)*96/90)
297 
298 
299 struct AV1HW_s;
300 static int vav1_vf_states(struct vframe_states *states, void *);
301 static struct vframe_s *vav1_vf_peek(void *);
302 static struct vframe_s *vav1_vf_get(void *);
303 static void vav1_vf_put(struct vframe_s *, void *);
304 static int vav1_event_cb(int type, void *data, void *private_data);
305 
306 static int vav1_stop(struct AV1HW_s *hw);
307 #ifdef MULTI_INSTANCE_SUPPORT
308 static s32 vav1_init(struct vdec_s *vdec);
309 #else
310 static s32 vav1_init(struct AV1HW_s *hw);
311 #endif
312 static void vav1_prot_init(struct AV1HW_s *hw, u32 mask);
313 static int vav1_local_init(struct AV1HW_s *hw);
314 static void vav1_put_timer_func(unsigned long arg);
315 static void dump_data(struct AV1HW_s *hw, int size);
316 static unsigned char get_data_check_sum
317 	(struct AV1HW_s *hw, int size);
318 static void dump_pic_list(struct AV1HW_s *hw);
319 static int vav1_mmu_map_alloc(struct AV1HW_s *hw);
320 static void vav1_mmu_map_free(struct AV1HW_s *hw);
321 static int av1_alloc_mmu(
322 		struct AV1HW_s *hw,
323 		int cur_buf_idx,
324 		int pic_width,
325 		int pic_height,
326 		unsigned short bit_depth,
327 		unsigned int *mmu_index_adr);
328 
329 #ifdef DEBUG_USE_VP9_DEVICE_NAME
330 static const char vav1_dec_id[] = "vvp9-dev";
331 
332 #define PROVIDER_NAME   "decoder.vp9"
333 #define MULTI_INSTANCE_PROVIDER_NAME    "vdec.vp9"
334 #else
335 static const char vav1_dec_id[] = "vav1-dev";
336 
337 #define PROVIDER_NAME   "decoder.av1"
338 #define MULTI_INSTANCE_PROVIDER_NAME    "vdec.av1"
339 #endif
340 #define DV_PROVIDER_NAME  "dvbldec"
341 
342 static const struct vframe_operations_s vav1_vf_provider = {
343 	.peek = vav1_vf_peek,
344 	.get = vav1_vf_get,
345 	.put = vav1_vf_put,
346 	.event_cb = vav1_event_cb,
347 	.vf_states = vav1_vf_states,
348 };
349 
350 static struct vframe_provider_s vav1_vf_prov;
351 
352 static u32 bit_depth_luma;
353 static u32 bit_depth_chroma;
354 static u32 frame_width;
355 static u32 frame_height;
356 static u32 video_signal_type;
357 static u32 on_no_keyframe_skiped;
358 static u32 without_display_mode;
359 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
360 static u32 force_dv_enable;
361 #endif
362 
363 #define PROB_SIZE    (496 * 2 * 4)
364 #define PROB_BUF_SIZE    (0x5000)
365 #define COUNT_BUF_SIZE   (0x300 * 4 * 4)
366 /*compute_losless_comp_body_size(4096, 2304, 1) = 18874368(0x1200000)*/
367 #define MAX_FRAME_4K_NUM 0x1200
368 #define MAX_FRAME_8K_NUM 0x4800
369 
370 #define HEVC_ASSIST_MMU_MAP_ADDR                   0x3009
371 
372 
373 /*USE_BUF_BLOCK*/
374 struct BUF_s {
375 	int index;
376 	unsigned int alloc_flag;
377 	/*buffer */
378 	unsigned int cma_page_count;
379 	unsigned long alloc_addr;
380 	unsigned long start_adr;
381 	unsigned int size;
382 
383 	unsigned int free_start_adr;
384 	ulong v4l_ref_buf_addr;
385 } /*BUF_t */;
386 
387 struct MVBUF_s {
388 	unsigned long start_adr;
389 	unsigned int size;
390 	int used_flag;
391 } /*MVBUF_t */;
392 
393 /*#define TEST_WR_PTR_INC*/
394 /*#define WR_PTR_INC_NUM 128*/
395 #define WR_PTR_INC_NUM 1
396 
397 //#define SIMULATION
398 #define DOS_PROJECT
399 #undef MEMORY_MAP_IN_REAL_CHIP
400 
401 /*#undef DOS_PROJECT*/
402 /*#define MEMORY_MAP_IN_REAL_CHIP*/
403 
404 /*#define CONFIG_HEVC_CLK_FORCED_ON*/
405 /*#define ENABLE_SWAP_TEST*/
406 #ifndef BUFMGR_ONLY_OLD_CHIP
407 #define   MCRCC_ENABLE
408 #endif
409 
410 #ifdef AV1_10B_NV21
411 #else
412 #define LOSLESS_COMPRESS_MODE
413 #endif
414 
415 typedef unsigned int u32;
416 typedef unsigned short u16;
417 
418 
419 static u32 get_picture_qos;
420 
421 static u32 debug;
422 
423 static bool is_reset;
424 /*for debug*/
425 /*
426 	udebug_flag:
427 	bit 0, enable ucode print
428 	bit 1, enable ucode detail print
429 	bit [31:16] not 0, pos to dump lmem
430 		bit 2, pop bits to lmem
431 		bit [11:8], pre-pop bits for alignment (when bit 2 is 1)
432 */
433 static u32 udebug_flag;
434 /*
435 	when udebug_flag[1:0] is not 0
436 	udebug_pause_pos not 0,
437 		pause position
438 */
439 static u32 udebug_pause_pos;
440 /*
441 	when udebug_flag[1:0] is not 0
442 	and udebug_pause_pos is not 0,
443 		pause only when DEBUG_REG2 is equal to this val
444 */
445 static u32 udebug_pause_val;
446 
447 static u32 udebug_pause_decode_idx;
448 
449 
450 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
451 static u32 dv_toggle_prov_name;
452 #endif
453 
454 
455 #define DEBUG_REG
456 #ifdef DEBUG_REG
AV1_WRITE_VREG_DBG2(unsigned int adr,unsigned int val,int line)457 void AV1_WRITE_VREG_DBG2(unsigned int adr, unsigned int val, int line)
458 {
459 	if (debug & AV1_DEBUG_REG)
460 		pr_info("%d:%s(%x, %x)\n", line, __func__, adr, val);
461 	if (adr != 0)
462 		WRITE_VREG(adr, val);
463 }
464 
465 #undef WRITE_VREG
466 #define WRITE_VREG(a,v) AV1_WRITE_VREG_DBG2(a,v,__LINE__)
467 #endif
468 
469 #define FRAME_CNT_WINDOW_SIZE 59
470 #define RATE_CORRECTION_THRESHOLD 5
471 /**************************************************
472 
473 AV1 buffer management start
474 
475 ***************************************************/
476 
477 #define MMU_COMPRESS_HEADER_SIZE  0x48000
478 #define MMU_COMPRESS_HEADER_SIZE_DW MMU_COMPRESS_HEADER_SIZE
479 #define MMU_COMPRESS_8K_HEADER_SIZE  (0x48000*4)
480 #define MAX_SIZE_8K (8192 * 4608)
481 #define MAX_SIZE_4K (4096 * 2304)
482 #define IS_8K_SIZE(w, h)	(((w) * (h)) > MAX_SIZE_4K)
483 
484 #define INVALID_IDX -1  /* Invalid buffer index.*/
485 
486 
487 /*4 scratch frames for the new frames to support a maximum of 4 cores decoding
488  *in parallel, 3 for scaled references on the encoder.
489  *TODO(hkuang): Add ondemand frame buffers instead of hardcoding the number
490  * // of framebuffers.
491  *TODO(jkoleszar): These 3 extra references could probably come from the
492  *normal reference pool.
493  */
494 //#define FRAME_BUFFERS (REF_FRAMES + 16)
495 //#define REF_FRAMES_4K (6)
496 #define REF_FRAMES_4K REF_FRAMES
497 
498 #ifdef USE_SPEC_BUF_FOR_MMU_HEAD
499 #define HEADER_FRAME_BUFFERS (0)
500 #elif (defined AOM_AV1_MMU_DW)
501 #define HEADER_FRAME_BUFFERS (2 * FRAME_BUFFERS)
502 #else
503 #define HEADER_FRAME_BUFFERS (FRAME_BUFFERS)
504 #endif
505 #define MAX_BUF_NUM (FRAME_BUFFERS)
506 #define MV_BUFFER_NUM	FRAME_BUFFERS
507 
508 //#define FRAME_CONTEXTS_LOG2 2
509 //#define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
510 /*buffer + header buffer + workspace*/
511 #ifdef MV_USE_FIXED_BUF
512 #define MAX_BMMU_BUFFER_NUM (FRAME_BUFFERS + HEADER_FRAME_BUFFERS + 1)
513 #define VF_BUFFER_IDX(n) (n)
514 #define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n)
515 #define WORK_SPACE_BUF_ID (FRAME_BUFFERS + HEADER_FRAME_BUFFERS)
516 #else
517 #define MAX_BMMU_BUFFER_NUM \
518 	(FRAME_BUFFERS + HEADER_FRAME_BUFFERS + MV_BUFFER_NUM + 1)
519 #define VF_BUFFER_IDX(n) (n)
520 #define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n)
521 #define MV_BUFFER_IDX(n) (FRAME_BUFFERS + HEADER_FRAME_BUFFERS + n)
522 #define WORK_SPACE_BUF_ID \
523 	(FRAME_BUFFERS + HEADER_FRAME_BUFFERS + MV_BUFFER_NUM)
524 #endif
525 #ifdef AOM_AV1_MMU_DW
526 #define DW_HEADER_BUFFER_IDX(n) (HEADER_BUFFER_IDX(HEADER_FRAME_BUFFERS/2) + n)
527 #endif
528 
529 
530 static void set_canvas(struct AV1HW_s *hw,
531 	struct PIC_BUFFER_CONFIG_s *pic_config);
532 
533 static void fill_frame_info(struct AV1HW_s *hw,
534 	struct PIC_BUFFER_CONFIG_s *frame,
535 	unsigned int framesize,
536 	unsigned int pts);
537 
538 
539 static int  compute_losless_comp_body_size(int width, int height,
540 				uint8_t is_bit_depth_10);
541 
542 
543 
544 #ifdef MULTI_INSTANCE_SUPPORT
545 #define DEC_RESULT_NONE             0
546 #define DEC_RESULT_DONE             1
547 #define DEC_RESULT_AGAIN            2
548 #define DEC_RESULT_CONFIG_PARAM     3
549 #define DEC_RESULT_ERROR            4
550 #define DEC_INIT_PICLIST			5
551 #define DEC_UNINIT_PICLIST			6
552 #define DEC_RESULT_GET_DATA         7
553 #define DEC_RESULT_GET_DATA_RETRY   8
554 #define DEC_RESULT_EOS              9
555 #define DEC_RESULT_FORCE_EXIT       10
556 
557 #define DEC_S1_RESULT_NONE          0
558 #define DEC_S1_RESULT_DONE          1
559 #define DEC_S1_RESULT_FORCE_EXIT       2
560 #define DEC_S1_RESULT_TEST_TRIGGER_DONE	0xf0
561 
562 #ifdef FB_DECODING_TEST_SCHEDULE
563 #define TEST_SET_NONE        0
564 #define TEST_SET_PIC_DONE    1
565 #define TEST_SET_S2_DONE     2
566 #endif
567 
568 static void av1_work(struct work_struct *work);
569 #endif
570 
571 #ifdef DUMP_FILMGRAIN
572 u32 fg_dump_index = 0xff;
573 #endif
574 
575 #ifdef AOM_AV1_DBLK_INIT
576 struct loop_filter_info_n_s;
577 struct loopfilter;
578 struct segmentation_lf;
579 #endif
580 struct AV1HW_s {
581 	AV1Decoder *pbi;
582 	union param_u aom_param;
583 	unsigned char frame_decoded;
584 	unsigned char one_compressed_data_done;
585 	unsigned char new_compressed_data;
586 #if 1
587 /*def CHECK_OBU_REDUNDANT_FRAME_HEADER*/
588 	int obu_frame_frame_head_come_after_tile;
589 #endif
590 	unsigned char index;
591 
592 	struct device *cma_dev;
593 	struct platform_device *platform_dev;
594 	void (*vdec_cb)(struct vdec_s *, void *);
595 	void *vdec_cb_arg;
596 	struct vframe_chunk_s *chunk;
597 	int dec_result;
598 	struct work_struct work;
599 	struct work_struct set_clk_work;
600 	u32 start_shift_bytes;
601 	u32 data_size;
602 
603 	struct BuffInfo_s work_space_buf_store;
604 	unsigned long buf_start;
605 	u32 buf_size;
606 	u32 cma_alloc_count;
607 	unsigned long cma_alloc_addr;
608 	uint8_t eos;
609 	unsigned long int start_process_time;
610 	unsigned last_lcu_idx;
611 	int decode_timeout_count;
612 	unsigned timeout_num;
613 	int save_buffer_mode;
614 
615 	int double_write_mode;
616 
617 	long used_4k_num;
618 
619 	unsigned char m_ins_flag;
620 	char *provider_name;
621 	union param_u param;
622 	int frame_count;
623 	int pic_count;
624 	u32 stat;
625 	struct timer_list timer;
626 	u32 frame_dur;
627 	u32 frame_ar;
628 	int fatal_error;
629 	uint8_t init_flag;
630 	uint8_t config_next_ref_info_flag;
631 	uint8_t first_sc_checked;
632 	uint8_t process_busy;
633 #define PROC_STATE_INIT			0
634 #define PROC_STATE_DECODESLICE	1
635 #define PROC_STATE_SENDAGAIN	2
636 	uint8_t process_state;
637 	u32 ucode_pause_pos;
638 
639 	int show_frame_num;
640 	struct buff_s mc_buf_spec;
641 	struct dec_sysinfo vav1_amstream_dec_info;
642 	void *rpm_addr;
643 	void *lmem_addr;
644 	dma_addr_t rpm_phy_addr;
645 	dma_addr_t lmem_phy_addr;
646 	unsigned short *lmem_ptr;
647 	unsigned short *debug_ptr;
648 #ifdef DUMP_FILMGRAIN
649 	dma_addr_t fg_phy_addr;
650 	unsigned char *fg_ptr;
651 	void *fg_addr;
652 #endif
653 	u32 fgs_valid;
654 
655 	u8 aux_data_dirty;
656 	u32 prefix_aux_size;
657 	u32 suffix_aux_size;
658 	void *aux_addr;
659 	dma_addr_t aux_phy_addr;
660 	char *dv_data_buf;
661 	int dv_data_size;
662 
663 	void *prob_buffer_addr;
664 	void *count_buffer_addr;
665 	dma_addr_t prob_buffer_phy_addr;
666 	dma_addr_t count_buffer_phy_addr;
667 
668 	void *frame_mmu_map_addr;
669 	dma_addr_t frame_mmu_map_phy_addr;
670 #ifdef AOM_AV1_MMU_DW
671 	void *dw_frame_mmu_map_addr;
672 	dma_addr_t dw_frame_mmu_map_phy_addr;
673 #endif
674 	unsigned int use_cma_flag;
675 
676 	struct BUF_s m_BUF[MAX_BUF_NUM];
677 	struct MVBUF_s m_mv_BUF[MV_BUFFER_NUM];
678 	u32 used_buf_num;
679 	DECLARE_KFIFO(newframe_q, struct vframe_s *, VF_POOL_SIZE);
680 	DECLARE_KFIFO(display_q, struct vframe_s *, VF_POOL_SIZE);
681 	DECLARE_KFIFO(pending_q, struct vframe_s *, VF_POOL_SIZE);
682 	struct vframe_s vfpool[VF_POOL_SIZE];
683 	u32 vf_pre_count;
684 	u32 vf_get_count;
685 	u32 vf_put_count;
686 	int buf_num;
687 	int pic_num;
688 	int lcu_size_log2;
689 	unsigned int losless_comp_body_size;
690 
691 	u32 video_signal_type;
692 
693 	u32 frame_mode_pts_save[FRAME_BUFFERS];
694 	u64 frame_mode_pts64_save[FRAME_BUFFERS];
695 
696 	int pts_mode;
697 	int last_lookup_pts;
698 	int last_pts;
699 	u64 last_lookup_pts_us64;
700 	u64 last_pts_us64;
701 	u64 shift_byte_count;
702 
703 	u32 pts_unstable;
704 	u32 frame_cnt_window;
705 	u32 pts1, pts2;
706 	u32 last_duration;
707 	u32 duration_from_pts_done;
708 	bool av1_first_pts_ready;
709 
710 	u32 shift_byte_count_lo;
711 	u32 shift_byte_count_hi;
712 	int pts_mode_switching_count;
713 	int pts_mode_recovery_count;
714 
715 	bool get_frame_dur;
716 	u32 saved_resolution;
717 
718 	/**/
719 	struct AV1_Common_s common;
720 	struct RefCntBuffer_s *cur_buf;
721 	int refresh_frame_flags;
722 	uint8_t need_resync;
723 	uint8_t hold_ref_buf;
724 	uint8_t ready_for_new_data;
725 	struct BufferPool_s av1_buffer_pool;
726 
727 	struct BuffInfo_s *work_space_buf;
728 
729 	struct buff_s *mc_buf;
730 
731 	unsigned int frame_width;
732 	unsigned int frame_height;
733 
734 	unsigned short *rpm_ptr;
735 	int     init_pic_w;
736 	int     init_pic_h;
737 	int     lcu_total;
738 
739 	int     current_lcu_size;
740 
741 	int     slice_type;
742 
743 	int skip_flag;
744 	int decode_idx;
745 	int result_done_count;
746 	uint8_t has_keyframe;
747 	uint8_t has_sequence;
748 	uint8_t wait_buf;
749 	uint8_t error_flag;
750 
751 	/* bit 0, for decoding; bit 1, for displaying */
752 	uint8_t ignore_bufmgr_error;
753 	int PB_skip_mode;
754 	int PB_skip_count_after_decoding;
755 	/*hw*/
756 
757 	/**/
758 	struct vdec_info *gvs;
759 
760 	u32 pre_stream_offset;
761 
762 	unsigned int dec_status;
763 	u32 last_put_idx;
764 	int new_frame_displayed;
765 	void *mmu_box;
766 	void *bmmu_box;
767 	int mmu_enable;
768 #ifdef AOM_AV1_MMU_DW
769 	void *mmu_box_dw;
770 	int dw_mmu_enable;
771 #endif
772 	struct vframe_master_display_colour_s vf_dp;
773 	struct firmware_s *fw;
774 	int max_pic_w;
775 	int max_pic_h;
776 
777 	int need_cache_size;
778 	u64 sc_start_time;
779 	bool postproc_done;
780 	int low_latency_flag;
781 	bool no_head;
782 	bool pic_list_init_done;
783 	bool pic_list_init_done2;
784 	bool is_used_v4l;
785 	void *v4l2_ctx;
786 	int frameinfo_enable;
787 	struct vframe_qos_s vframe_qos;
788 
789 #ifdef AOM_AV1_DBLK_INIT
790 	/*
791 	 * malloc may not work in real chip, please allocate memory for the following structures
792 	 */
793     struct loop_filter_info_n_s *lfi;
794     struct loopfilter *lf;
795     struct segmentation_lf *seg_4lf;
796 #endif
797 
798 };
799 static void av1_dump_state(struct vdec_s *vdec);
800 
av1_print(struct AV1HW_s * hw,int flag,const char * fmt,...)801 int av1_print(struct AV1HW_s *hw,
802 	int flag, const char *fmt, ...)
803 {
804 #define HEVC_PRINT_BUF		256
805 	unsigned char buf[HEVC_PRINT_BUF];
806 	int len = 0;
807 
808 	if (hw == NULL ||
809 		(flag == 0) ||
810 		(debug & flag)) {
811 		va_list args;
812 
813 		va_start(args, fmt);
814 		if (hw)
815 			len = sprintf(buf, "[%d]", hw->index);
816 		vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
817 		pr_info("%s", buf);
818 		va_end(args);
819 	}
820 	return 0;
821 }
822 
av1_is_debug(int flag)823 unsigned char av1_is_debug(int flag)
824 {
825 	if ((flag == 0) || (debug & flag))
826 		return 1;
827 
828 	return 0;
829 }
830 
av1_print2(int flag,const char * fmt,...)831 int av1_print2(int flag, const char *fmt, ...)
832 {
833 	unsigned char buf[HEVC_PRINT_BUF];
834 	int len = 0;
835 
836 	if ((flag == 0) ||
837 		(debug & flag)) {
838 		va_list args;
839 
840 		va_start(args, fmt);
841 		vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
842 		pr_info("%s", buf);
843 		va_end(args);
844 	}
845 	return 0;
846 
847 }
848 
is_oversize(int w,int h)849 static int is_oversize(int w, int h)
850 {
851 	int max = (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)?
852 		MAX_SIZE_8K : MAX_SIZE_4K;
853 
854 	if (w <= 0 || h <= 0)
855 		return true;
856 
857 	if (h != 0 && (w > max / h))
858 		return true;
859 
860 	return false;
861 }
862 
v4l_get_fb(struct aml_vcodec_ctx * ctx,struct vdec_fb ** out)863 static int v4l_get_fb(struct aml_vcodec_ctx *ctx, struct vdec_fb **out)
864 {
865 	int ret = 0;
866 
867 	ret = ctx->dec_if->get_param(ctx->drv_handle,
868 		GET_PARAM_FREE_FRAME_BUFFER, out);
869 
870 	return ret;
871 }
872 
873 
close_to(int a,int b,int m)874 static inline bool close_to(int a, int b, int m)
875 {
876 	return (abs(a - b) < m) ? true : false;
877 }
878 
879 #ifdef MULTI_INSTANCE_SUPPORT
av1_print_cont(struct AV1HW_s * hw,int flag,const char * fmt,...)880 static int av1_print_cont(struct AV1HW_s *hw,
881 	int flag, const char *fmt, ...)
882 {
883 	unsigned char buf[HEVC_PRINT_BUF];
884 	int len = 0;
885 
886 	if (hw == NULL ||
887 		(flag == 0) ||
888 		(debug & flag)) {
889 		va_list args;
890 
891 		va_start(args, fmt);
892 		vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
893 		pr_info("%s", buf);
894 		va_end(args);
895 	}
896 	return 0;
897 }
898 
trigger_schedule(struct AV1HW_s * hw)899 static void trigger_schedule(struct AV1HW_s *hw)
900 {
901 	if (hw->vdec_cb)
902 		hw->vdec_cb(hw_to_vdec(hw), hw->vdec_cb_arg);
903 }
904 
reset_process_time(struct AV1HW_s * hw)905 static void reset_process_time(struct AV1HW_s *hw)
906 {
907 	if (hw->start_process_time) {
908 		unsigned process_time =
909 			1000 * (jiffies - hw->start_process_time) / HZ;
910 		hw->start_process_time = 0;
911 		if (process_time > max_process_time[hw->index])
912 			max_process_time[hw->index] = process_time;
913 	}
914 }
915 
start_process_time(struct AV1HW_s * hw)916 static void start_process_time(struct AV1HW_s *hw)
917 {
918 	hw->start_process_time = jiffies;
919 	hw->decode_timeout_count = 0;
920 	hw->last_lcu_idx = 0;
921 }
922 
timeout_process(struct AV1HW_s * hw)923 static void timeout_process(struct AV1HW_s *hw)
924 {
925 	reset_process_time(hw);
926 	if (hw->process_busy) {
927 		av1_print(hw,
928 			0, "%s decoder timeout but process_busy\n", __func__);
929 		if (debug)
930 			av1_print(hw, 0, "debug disable timeout notify\n");
931 		return;
932 	}
933 	hw->timeout_num++;
934 	amhevc_stop();
935 	av1_print(hw,
936 		0, "%s decoder timeout\n", __func__);
937 
938 	hw->dec_result = DEC_RESULT_DONE;
939 	vdec_schedule_work(&hw->work);
940 }
941 
get_valid_double_write_mode(struct AV1HW_s * hw)942 static u32 get_valid_double_write_mode(struct AV1HW_s *hw)
943 {
944 	u32 dw = ((double_write_mode & 0x80000000) == 0) ?
945 		hw->double_write_mode :
946 		(double_write_mode & 0x7fffffff);
947 	if ((dw & 0x20) &&
948 		((dw & 0xf) == 2 || (dw & 0xf) == 3)) {
949 		pr_info("MMU doueble write 1:4 not supported !!!\n");
950 		dw = 0;
951 	}
952 	return dw;
953 }
954 
get_double_write_mode(struct AV1HW_s * hw)955 static int get_double_write_mode(struct AV1HW_s *hw)
956 {
957 	u32 valid_dw_mode = get_valid_double_write_mode(hw);
958 	u32 dw;
959 	int w, h;
960 	struct AV1_Common_s *cm = &hw->pbi->common;
961 	struct PIC_BUFFER_CONFIG_s *cur_pic_config;
962 
963 	if (!cm->cur_frame)
964 		return 1;/*no valid frame,*/
965 	cur_pic_config = &cm->cur_frame->buf;
966 	w = cur_pic_config->y_crop_width;
967 	h = cur_pic_config->y_crop_height;
968 
969 	dw = 0x1; /*1:1*/
970 	switch (valid_dw_mode) {
971 	case 0x100:
972 		if (w > 1920 && h > 1088)
973 			dw = 0x4; /*1:2*/
974 		break;
975 	case 0x200:
976 		if (w > 1920 && h > 1088)
977 			dw = 0x2; /*1:4*/
978 		break;
979 	case 0x300:
980 		if (w > 1280 && h > 720)
981 			dw = 0x4; /*1:2*/
982 		break;
983 	default:
984 		dw = valid_dw_mode;
985 		break;
986 	}
987 	return dw;
988 }
989 
990 /* for double write buf alloc */
get_double_write_mode_init(struct AV1HW_s * hw)991 static int get_double_write_mode_init(struct AV1HW_s *hw)
992 {
993 	u32 valid_dw_mode = get_valid_double_write_mode(hw);
994 	u32 dw;
995 	int w = hw->init_pic_w;
996 	int h = hw->init_pic_h;
997 
998 	dw = 0x1; /*1:1*/
999 	switch (valid_dw_mode) {
1000 	case 0x100:
1001 		if (w > 1920 && h > 1088)
1002 			dw = 0x4; /*1:2*/
1003 		break;
1004 	case 0x200:
1005 		if (w > 1920 && h > 1088)
1006 			dw = 0x2; /*1:4*/
1007 		break;
1008 	case 0x300:
1009 		if (w > 1280 && h > 720)
1010 			dw = 0x4; /*1:2*/
1011 		break;
1012 	default:
1013 		dw = valid_dw_mode;
1014 		break;
1015 	}
1016 	return dw;
1017 }
1018 #endif
1019 
get_double_write_ratio(struct AV1HW_s * hw,int dw_mode)1020 static int get_double_write_ratio(struct AV1HW_s *hw,
1021 	int dw_mode)
1022 {
1023 	int ratio = 1;
1024 	int dw_mode_ratio = dw_mode & 0xf;
1025 	if ((dw_mode_ratio == 2) ||
1026 			(dw_mode_ratio == 3))
1027 		ratio = 4;
1028 	else if (dw_mode_ratio == 4)
1029 		ratio = 2;
1030 	return ratio;
1031 }
1032 
1033 //#define	MAX_4K_NUM		0x1200
av1_alloc_mmu(struct AV1HW_s * hw,int cur_buf_idx,int pic_width,int pic_height,unsigned short bit_depth,unsigned int * mmu_index_adr)1034 int av1_alloc_mmu(
1035 	struct AV1HW_s *hw,
1036 	int cur_buf_idx,
1037 	int pic_width,
1038 	int pic_height,
1039 	unsigned short bit_depth,
1040 	unsigned int *mmu_index_adr)
1041 {
1042 	int ret = 0;
1043 	int bit_depth_10 = (bit_depth == AOM_BITS_10);
1044 	int picture_size;
1045 	int cur_mmu_4k_number, max_frame_num;
1046 	if (!hw->mmu_box) {
1047 		pr_err("error no mmu box!\n");
1048 		return -1;
1049 	}
1050 	if (hw->double_write_mode & 0x10)
1051 		return 0;
1052 	if (bit_depth >= AOM_BITS_12) {
1053 		hw->fatal_error = DECODER_FATAL_ERROR_SIZE_OVERFLOW;
1054 		pr_err("fatal_error, un support bit depth 12!\n\n");
1055 		return -1;
1056 	}
1057 	picture_size = compute_losless_comp_body_size(pic_width, pic_height,
1058 				   bit_depth_10);
1059 	cur_mmu_4k_number = ((picture_size + (1 << 12) - 1) >> 12);
1060 
1061 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
1062 		max_frame_num = MAX_FRAME_8K_NUM;
1063 	else
1064 		max_frame_num = MAX_FRAME_4K_NUM;
1065 
1066 	if (cur_mmu_4k_number > max_frame_num) {
1067 		pr_err("over max !! cur_mmu_4k_number 0x%x width %d height %d\n",
1068 			cur_mmu_4k_number, pic_width, pic_height);
1069 		return -1;
1070 	}
1071 	ret = decoder_mmu_box_alloc_idx(
1072 		hw->mmu_box,
1073 		cur_buf_idx,
1074 		cur_mmu_4k_number,
1075 		mmu_index_adr);
1076 	return ret;
1077 }
1078 
1079 #ifdef AOM_AV1_MMU_DW
1080 static int compute_losless_comp_body_size_dw(int width, int height,
1081 				uint8_t is_bit_depth_10);
1082 
av1_alloc_mmu_dw(struct AV1HW_s * hw,int cur_buf_idx,int pic_width,int pic_height,unsigned short bit_depth,unsigned int * mmu_index_adr)1083 int av1_alloc_mmu_dw(
1084 	struct AV1HW_s *hw,
1085 	int cur_buf_idx,
1086 	int pic_width,
1087 	int pic_height,
1088 	unsigned short bit_depth,
1089 	unsigned int *mmu_index_adr)
1090 {
1091 	int ret = 0;
1092 	int bit_depth_10 = (bit_depth == AOM_BITS_10);
1093 	int picture_size;
1094 	int cur_mmu_4k_number, max_frame_num;
1095 	if (!hw->mmu_box_dw) {
1096 		pr_err("error no mmu box!\n");
1097 		return -1;
1098 	}
1099 	if (hw->double_write_mode & 0x10)
1100 		return 0;
1101 	if (bit_depth >= AOM_BITS_12) {
1102 		hw->fatal_error = DECODER_FATAL_ERROR_SIZE_OVERFLOW;
1103 		pr_err("fatal_error, un support bit depth 12!\n\n");
1104 		return -1;
1105 	}
1106 	picture_size = compute_losless_comp_body_size_dw(pic_width, pic_height,
1107 				   bit_depth_10);
1108 	cur_mmu_4k_number = ((picture_size + (1 << 12) - 1) >> 12);
1109 
1110 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
1111 		max_frame_num = MAX_FRAME_8K_NUM;
1112 	else
1113 		max_frame_num = MAX_FRAME_4K_NUM;
1114 
1115 	if (cur_mmu_4k_number > max_frame_num) {
1116 		pr_err("over max !! cur_mmu_4k_number 0x%x width %d height %d\n",
1117 			cur_mmu_4k_number, pic_width, pic_height);
1118 		return -1;
1119 	}
1120 	ret = decoder_mmu_box_alloc_idx(
1121 		hw->mmu_box_dw,
1122 		cur_buf_idx,
1123 		cur_mmu_4k_number,
1124 		mmu_index_adr);
1125 	return ret;
1126 }
1127 #endif
1128 
1129 #ifndef MV_USE_FIXED_BUF
dealloc_mv_bufs(struct AV1HW_s * hw)1130 static void dealloc_mv_bufs(struct AV1HW_s *hw)
1131 {
1132 	int i;
1133 	for (i = 0; i < MV_BUFFER_NUM; i++) {
1134 		if (hw->m_mv_BUF[i].start_adr) {
1135 			if (debug)
1136 				pr_info(
1137 				"dealloc mv buf(%d) adr %ld size 0x%x used_flag %d\n",
1138 				i, hw->m_mv_BUF[i].start_adr,
1139 				hw->m_mv_BUF[i].size,
1140 				hw->m_mv_BUF[i].used_flag);
1141 			decoder_bmmu_box_free_idx(
1142 				hw->bmmu_box,
1143 				MV_BUFFER_IDX(i));
1144 			hw->m_mv_BUF[i].start_adr = 0;
1145 			hw->m_mv_BUF[i].size = 0;
1146 			hw->m_mv_BUF[i].used_flag = 0;
1147 		}
1148 	}
1149 }
1150 
alloc_mv_buf(struct AV1HW_s * hw,int i,int size)1151 static int alloc_mv_buf(struct AV1HW_s *hw,
1152 	int i, int size)
1153 {
1154 	int ret = 0;
1155 	if (decoder_bmmu_box_alloc_buf_phy
1156 		(hw->bmmu_box,
1157 		MV_BUFFER_IDX(i), size,
1158 		DRIVER_NAME,
1159 		&hw->m_mv_BUF[i].start_adr) < 0) {
1160 		hw->m_mv_BUF[i].start_adr = 0;
1161 		ret = -1;
1162 	} else {
1163 		hw->m_mv_BUF[i].size = size;
1164 		hw->m_mv_BUF[i].used_flag = 0;
1165 		ret = 0;
1166 		if (debug) {
1167 			pr_info(
1168 			"MV Buffer %d: start_adr %p size %x\n",
1169 			i,
1170 			(void *)hw->m_mv_BUF[i].start_adr,
1171 			hw->m_mv_BUF[i].size);
1172 		}
1173 	}
1174 	return ret;
1175 }
1176 
init_mv_buf_list(struct AV1HW_s * hw)1177 static int init_mv_buf_list(struct AV1HW_s *hw)
1178 {
1179 	int i;
1180 	int ret = 0;
1181 	int count = MV_BUFFER_NUM;
1182 	int pic_width = hw->init_pic_w;
1183 	int pic_height = hw->init_pic_h;
1184 	unsigned lcu_size = hw->current_lcu_size;
1185 	int extended_pic_width = (pic_width + lcu_size -1)
1186 				& (~(lcu_size - 1));
1187 	int extended_pic_height = (pic_height + lcu_size -1)
1188 				& (~(lcu_size - 1));
1189 	int size = (extended_pic_width / 64) *
1190 				(extended_pic_height / 64) * 19 * 16;
1191 #if 0
1192 	if (mv_buf_margin > 0)
1193 		count = REF_FRAMES + mv_buf_margin;
1194 	if (hw->init_pic_w > 2048 && hw->init_pic_h > 1088)
1195 		count = REF_FRAMES_4K + mv_buf_margin;
1196 #else
1197 	if (debug)
1198 		pr_info("%s, calculated mv size 0x%x\n",
1199 			__func__, size);
1200 	if (hw->init_pic_w > 4096 && hw->init_pic_h > 2048) {
1201 		count = REF_FRAMES_4K + mv_buf_margin;
1202 		if (debug & AOM_DEBUG_USE_FIXED_MV_BUF_SIZE)
1203 			size = 0x260000;
1204 	} else if (hw->init_pic_w > 2048 && hw->init_pic_h > 1088) {
1205 		count = REF_FRAMES_4K + mv_buf_margin;
1206 		if (debug & AOM_DEBUG_USE_FIXED_MV_BUF_SIZE)
1207 			size = 0x130000;
1208 	} else {
1209 		count = REF_FRAMES + mv_buf_margin;
1210 		if (debug & AOM_DEBUG_USE_FIXED_MV_BUF_SIZE)
1211 			size = 0x130000;
1212 	}
1213 #endif
1214 	if (debug) {
1215 		pr_info("%s w:%d, h:%d, lcu_size %d, count: %d, size 0x%x\n",
1216 		__func__, hw->init_pic_w, hw->init_pic_h,
1217 		lcu_size, count, size);
1218 	}
1219 
1220 	for (i = 0;
1221 		i < count && i < MV_BUFFER_NUM; i++) {
1222 		if (alloc_mv_buf(hw, i, size) < 0) {
1223 			ret = -1;
1224 			break;
1225 		}
1226 	}
1227 	return ret;
1228 }
1229 
get_mv_buf(struct AV1HW_s * hw,int * mv_buf_index,uint32_t * mpred_mv_wr_start_addr)1230 static int get_mv_buf(struct AV1HW_s *hw,
1231 		int *mv_buf_index,
1232 		uint32_t *mpred_mv_wr_start_addr)
1233 {
1234 	int i;
1235 	int ret = -1;
1236 	for (i = 0; i < MV_BUFFER_NUM; i++) {
1237 		if (hw->m_mv_BUF[i].start_adr &&
1238 			hw->m_mv_BUF[i].used_flag == 0) {
1239 			hw->m_mv_BUF[i].used_flag = 1;
1240 			ret = i;
1241 			break;
1242 		}
1243 	}
1244 
1245 	if (ret >= 0) {
1246 		*mv_buf_index = ret;
1247 		*mpred_mv_wr_start_addr =
1248 			(hw->m_mv_BUF[ret].start_adr + 0xffff) &
1249 			(~0xffff);
1250 		if (debug & AV1_DEBUG_BUFMGR_MORE)
1251 			pr_info(
1252 			"%s => %d (%d) size 0x%x\n",
1253 			__func__, ret,
1254 			*mpred_mv_wr_start_addr,
1255 			hw->m_mv_BUF[ret].size);
1256 	} else {
1257 		pr_info(
1258 		"%s: Error, mv buf is not enough\n",
1259 		__func__);
1260 	}
1261 	return ret;
1262 }
put_mv_buf(struct AV1HW_s * hw,int * mv_buf_index)1263 static void put_mv_buf(struct AV1HW_s *hw,
1264 				int *mv_buf_index)
1265 {
1266 	int i = *mv_buf_index;
1267 	if (i >= MV_BUFFER_NUM) {
1268 		if (debug & AV1_DEBUG_BUFMGR_MORE)
1269 			pr_info(
1270 			"%s: index %d beyond range\n",
1271 			__func__, i);
1272 		return;
1273 	}
1274 	if (debug & AV1_DEBUG_BUFMGR_MORE)
1275 		pr_info(
1276 		"%s(%d): used_flag(%d)\n",
1277 		__func__, i,
1278 		hw->m_mv_BUF[i].used_flag);
1279 
1280 	*mv_buf_index = -1;
1281 	if (hw->m_mv_BUF[i].start_adr &&
1282 		hw->m_mv_BUF[i].used_flag)
1283 		hw->m_mv_BUF[i].used_flag = 0;
1284 }
put_un_used_mv_bufs(struct AV1HW_s * hw)1285 static void	put_un_used_mv_bufs(struct AV1HW_s *hw)
1286 {
1287 	struct AV1_Common_s *const cm = &hw->pbi->common;
1288 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
1289 	int i;
1290 	for (i = 0; i < hw->used_buf_num; ++i) {
1291 		if ((frame_bufs[i].ref_count == 0) &&
1292 			(frame_bufs[i].buf.index != -1) &&
1293 			(frame_bufs[i].buf.mv_buf_index >= 0)
1294 			)
1295 			put_mv_buf(hw, &frame_bufs[i].buf.mv_buf_index);
1296 	}
1297 }
1298 #endif
1299 
1300 
get_free_buf_count(struct AV1HW_s * hw)1301 static int get_free_buf_count(struct AV1HW_s *hw)
1302 {
1303 	struct AV1_Common_s *const cm = &hw->pbi->common;
1304 	struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
1305 	int i;
1306 	int free_buf_count = 0;
1307 	for (i = 0; i < hw->used_buf_num; ++i)
1308 		if ((frame_bufs[i].ref_count == 0) &&
1309 			(frame_bufs[i].buf.vf_ref == 0) &&
1310 			(frame_bufs[i].buf.index != -1)
1311 			)
1312 			free_buf_count++;
1313 	return free_buf_count;
1314 }
1315 
aom_bufmgr_init(struct AV1HW_s * hw,struct BuffInfo_s * buf_spec_i,struct buff_s * mc_buf_i)1316 int aom_bufmgr_init(struct AV1HW_s *hw, struct BuffInfo_s *buf_spec_i,
1317 		struct buff_s *mc_buf_i) {
1318 	struct AV1_Common_s *cm = &hw->pbi->common;
1319 	if (debug)
1320 		pr_info("%s %d %p\n", __func__, __LINE__, hw->pbi);
1321 	hw->frame_count = 0;
1322 	hw->pic_count = 0;
1323 	hw->pre_stream_offset = 0;
1324 	spin_lock_init(&cm->buffer_pool->lock);
1325 	cm->prev_fb_idx = INVALID_IDX;
1326 	cm->new_fb_idx = INVALID_IDX;
1327 	hw->used_4k_num = -1;
1328 	cm->cur_fb_idx_mmu = INVALID_IDX;
1329 	pr_debug
1330 	("After aom_bufmgr_init, prev_fb_idx : %d, new_fb_idx : %d\r\n",
1331 		cm->prev_fb_idx, cm->new_fb_idx);
1332 	hw->need_resync = 1;
1333 
1334 	cm->current_video_frame = 0;
1335 	hw->ready_for_new_data = 1;
1336 
1337 	/* private init */
1338 	hw->work_space_buf = buf_spec_i;
1339 	if (!hw->mmu_enable)
1340 		hw->mc_buf = mc_buf_i;
1341 
1342 	hw->rpm_addr = NULL;
1343 	hw->lmem_addr = NULL;
1344 #ifdef DUMP_FILMGRAIN
1345 	hw->fg_addr = NULL;
1346 #endif
1347 	hw->use_cma_flag = 0;
1348 	hw->decode_idx = 0;
1349 	hw->result_done_count = 0;
1350 	/*int m_uiMaxCUWidth = 1<<7;*/
1351 	/*int m_uiMaxCUHeight = 1<<7;*/
1352 	hw->has_keyframe = 0;
1353 	hw->has_sequence = 0;
1354 	hw->skip_flag = 0;
1355 	hw->wait_buf = 0;
1356 	hw->error_flag = 0;
1357 
1358 	hw->pts_mode = PTS_NORMAL;
1359 	hw->last_pts = 0;
1360 	hw->last_lookup_pts = 0;
1361 	hw->last_pts_us64 = 0;
1362 	hw->last_lookup_pts_us64 = 0;
1363 	hw->shift_byte_count = 0;
1364 	hw->shift_byte_count_lo = 0;
1365 	hw->shift_byte_count_hi = 0;
1366 	hw->pts_mode_switching_count = 0;
1367 	hw->pts_mode_recovery_count = 0;
1368 
1369 	hw->buf_num = 0;
1370 	hw->pic_num = 0;
1371 
1372 	return 0;
1373 }
1374 
1375 /*
1376 struct AV1HW_s av1_decoder;
1377 union param_u av1_param;
1378 */
1379 /**************************************************
1380  *
1381  *AV1 buffer management end
1382  *
1383  ***************************************************
1384  */
1385 
1386 
1387 #define HEVC_CM_BODY_START_ADDR    			    0x3626
1388 #define HEVC_CM_BODY_LENGTH    				    0x3627
1389 #define HEVC_CM_HEADER_LENGTH    				  0x3629
1390 #define HEVC_CM_HEADER_OFFSET    				  0x362b
1391 
1392 #define LOSLESS_COMPRESS_MODE
1393 
1394 /*#define DECOMP_HEADR_SURGENT*/
1395 #ifdef AV1_10B_NV21
1396 static u32 mem_map_mode = 2  /* 0:linear 1:32x32 2:64x32*/
1397 #else
1398 static u32 mem_map_mode; /* 0:linear 1:32x32 2:64x32 ; m8baby test1902 */
1399 #endif
1400 static u32 enable_mem_saving = 1;
1401 static u32 force_w_h;
1402 
1403 static u32 force_fps;
1404 
1405 
1406 const u32 av1_version = 201602101;
1407 static u32 debug;
1408 static u32 radr;
1409 static u32 rval;
1410 static u32 pop_shorts;
1411 static u32 dbg_cmd;
1412 static u32 dbg_skip_decode_index;
1413 static u32 endian = 0xff0;
1414 static u32 multi_frames_in_one_pack = 1;
1415 #ifdef ERROR_HANDLE_DEBUG
1416 static u32 dbg_nal_skip_flag;
1417 		/* bit[0], skip vps; bit[1], skip sps; bit[2], skip pps */
1418 static u32 dbg_nal_skip_count;
1419 #endif
1420 /*for debug*/
1421 static u32 decode_pic_begin;
1422 static uint slice_parse_begin;
1423 static u32 step;
1424 #ifdef MIX_STREAM_SUPPORT
1425 static u32 buf_alloc_width = 4096;
1426 static u32 buf_alloc_height = 2304;
1427 static u32 av1_max_pic_w = 4096;
1428 static u32 av1_max_pic_h = 2304;
1429 
1430 static u32 dynamic_buf_num_margin;
1431 #else
1432 static u32 buf_alloc_width;
1433 static u32 buf_alloc_height;
1434 static u32 dynamic_buf_num_margin = 7;
1435 #endif
1436 static u32 buf_alloc_depth = 10;
1437 static u32 buf_alloc_size;
1438 /*
1439  *bit[0]: 0,
1440  *    bit[1]: 0, always release cma buffer when stop
1441  *    bit[1]: 1, never release cma buffer when stop
1442  *bit[0]: 1, when stop, release cma buffer if blackout is 1;
1443  *do not release cma buffer is blackout is not 1
1444  *
1445  *bit[2]: 0, when start decoding, check current displayed buffer
1446  *	 (only for buffer decoded by AV1) if blackout is 0
1447  *	 1, do not check current displayed buffer
1448  *
1449  *bit[3]: 1, if blackout is not 1, do not release current
1450  *			displayed cma buffer always.
1451  */
1452 /* set to 1 for fast play;
1453  *	set to 8 for other case of "keep last frame"
1454  */
1455 static u32 buffer_mode = 1;
1456 /* buffer_mode_dbg: debug only*/
1457 static u32 buffer_mode_dbg = 0xffff0000;
1458 /**/
1459 
1460 /*
1461  *bit 0, 1: only display I picture;
1462  *bit 1, 1: only decode I picture;
1463  */
1464 static u32 i_only_flag;
1465 
1466 static u32 low_latency_flag;
1467 
1468 static u32 no_head;
1469 
1470 static u32 max_decoding_time;
1471 /*
1472  *error handling
1473  */
1474 /*error_handle_policy:
1475  *bit 0: 0, auto skip error_skip_nal_count nals before error recovery;
1476  *1, skip error_skip_nal_count nals before error recovery;
1477  *bit 1 (valid only when bit0 == 1):
1478  *1, wait vps/sps/pps after error recovery;
1479  *bit 2 (valid only when bit0 == 0):
1480  *0, auto search after error recovery (av1_recover() called);
1481  *1, manual search after error recovery
1482  *(change to auto search after get IDR: WRITE_VREG(NAL_SEARCH_CTL, 0x2))
1483  *
1484  *bit 4: 0, set error_mark after reset/recover
1485  *    1, do not set error_mark after reset/recover
1486  *bit 5: 0, check total lcu for every picture
1487  *    1, do not check total lcu
1488  *
1489  */
1490 
1491 static u32 error_handle_policy;
1492 /*static u32 parser_sei_enable = 1;*/
1493 #define MAX_BUF_NUM_NORMAL     16
1494 #define MAX_BUF_NUM_LESS   12
1495 static u32 max_buf_num = MAX_BUF_NUM_NORMAL;
1496 #define MAX_BUF_NUM_SAVE_BUF  8
1497 
1498 static u32 run_ready_min_buf_num = 2;
1499 
1500 
1501 static DEFINE_MUTEX(vav1_mutex);
1502 #ifndef MULTI_INSTANCE_SUPPORT
1503 static struct device *cma_dev;
1504 #endif
1505 #define HEVC_DEC_STATUS_REG       HEVC_ASSIST_SCRATCH_0
1506 #define HEVC_FG_STATUS			HEVC_ASSIST_SCRATCH_B
1507 #define HEVC_RPM_BUFFER           HEVC_ASSIST_SCRATCH_1
1508 #define AOM_AV1_ADAPT_PROB_REG        HEVC_ASSIST_SCRATCH_3
1509 #define AOM_AV1_MMU_MAP_BUFFER        HEVC_ASSIST_SCRATCH_4 // changed to use HEVC_ASSIST_MMU_MAP_ADDR
1510 #define AOM_AV1_DAALA_TOP_BUFFER      HEVC_ASSIST_SCRATCH_5
1511 #define HEVC_SAO_UP               HEVC_ASSIST_SCRATCH_6
1512 //#define HEVC_STREAM_SWAP_BUFFER   HEVC_ASSIST_SCRATCH_7
1513 #define AOM_AV1_CDF_BUFFER_W      HEVC_ASSIST_SCRATCH_8
1514 #define AOM_AV1_CDF_BUFFER_R      HEVC_ASSIST_SCRATCH_9
1515 #define AOM_AV1_COUNT_SWAP_BUFFER     HEVC_ASSIST_SCRATCH_A
1516 #define AOM_AV1_SEG_MAP_BUFFER_W  AV1_SEG_W_ADDR  //    HEVC_ASSIST_SCRATCH_B
1517 #define AOM_AV1_SEG_MAP_BUFFER_R  AV1_SEG_R_ADDR  //    HEVC_ASSIST_SCRATCH_C
1518 //#define HEVC_sao_vb_size          HEVC_ASSIST_SCRATCH_B
1519 //#define HEVC_SAO_VB               HEVC_ASSIST_SCRATCH_C
1520 //#define HEVC_SCALELUT             HEVC_ASSIST_SCRATCH_D
1521 #define HEVC_WAIT_FLAG	          HEVC_ASSIST_SCRATCH_E
1522 #define RPM_CMD_REG               HEVC_ASSIST_SCRATCH_F
1523 //#define HEVC_STREAM_SWAP_TEST     HEVC_ASSIST_SCRATCH_L
1524 
1525 #ifdef MULTI_INSTANCE_SUPPORT
1526 #define HEVC_DECODE_COUNT       HEVC_ASSIST_SCRATCH_M
1527 #define HEVC_DECODE_SIZE		HEVC_ASSIST_SCRATCH_N
1528 #else
1529 #define HEVC_DECODE_PIC_BEGIN_REG HEVC_ASSIST_SCRATCH_M
1530 #define HEVC_DECODE_PIC_NUM_REG   HEVC_ASSIST_SCRATCH_N
1531 #endif
1532 #define AOM_AV1_SEGMENT_FEATURE   AV1_QUANT_WR
1533 
1534 #define DEBUG_REG1              HEVC_ASSIST_SCRATCH_G
1535 #define DEBUG_REG2              HEVC_ASSIST_SCRATCH_H
1536 
1537 #define LMEM_DUMP_ADR     	        HEVC_ASSIST_SCRATCH_I
1538 #define CUR_NAL_UNIT_TYPE       HEVC_ASSIST_SCRATCH_J
1539 #define DECODE_STOP_POS           HEVC_ASSIST_SCRATCH_K
1540 
1541 #define PIC_END_LCU_COUNT       HEVC_ASSIST_SCRATCH_2
1542 
1543 #define HEVC_AUX_ADR			HEVC_ASSIST_SCRATCH_L
1544 #define HEVC_AUX_DATA_SIZE		HEVC_ASSIST_SCRATCH_7
1545 
1546 /*
1547  *ucode parser/search control
1548  *bit 0:  0, header auto parse; 1, header manual parse
1549  *bit 1:  0, auto skip for noneseamless stream; 1, no skip
1550  *bit [3:2]: valid when bit1==0;
1551  *0, auto skip nal before first vps/sps/pps/idr;
1552  *1, auto skip nal before first vps/sps/pps
1553  *2, auto skip nal before first  vps/sps/pps,
1554  *	and not decode until the first I slice (with slice address of 0)
1555  *
1556  *3, auto skip before first I slice (nal_type >=16 && nal_type<=21)
1557  *bit [15:4] nal skip count (valid when bit0 == 1 (manual mode) )
1558  *bit [16]: for NAL_UNIT_EOS when bit0 is 0:
1559  *	0, send SEARCH_DONE to arm ;  1, do not send SEARCH_DONE to arm
1560  *bit [17]: for NAL_SEI when bit0 is 0:
1561  *	0, do not parse SEI in ucode; 1, parse SEI in ucode
1562  *bit [31:20]: used by ucode for debug purpose
1563  */
1564 #define NAL_SEARCH_CTL            HEVC_ASSIST_SCRATCH_I
1565 	/*[31:24] chip feature
1566 		31: 0, use MBOX1; 1, use MBOX0
1567 	  [24:16] debug
1568 	    0x1, bufmgr only
1569 	*/
1570 #define DECODE_MODE              HEVC_ASSIST_SCRATCH_J
1571 #define DECODE_STOP_POS         HEVC_ASSIST_SCRATCH_K
1572 
1573 #define RPM_BUF_SIZE ((RPM_END - RPM_BEGIN) * 2)
1574 #define LMEM_BUF_SIZE (0x600 * 2)
1575 
1576 #ifdef MAP_8K
1577 static u32 seg_map_size = 0xd8000;
1578 #else
1579 static u32 seg_map_size = 0x36000;
1580 #endif
1581 
1582 #define WORK_BUF_SPEC_NUM 2
1583 static struct BuffInfo_s  aom_workbuff_spec[WORK_BUF_SPEC_NUM]={
1584 	{ //8M bytes
1585 		.max_width = 1920,
1586 		.max_height = 1088,
1587 		.ipp = {
1588 			// IPP work space calculation : 4096 * (Y+CbCr+Flags) = 12k, round to 16k
1589 			.buf_size = 0x4000,
1590 		},
1591 		.sao_abv = {
1592 			.buf_size = 0x30000,
1593 		},
1594 		.sao_vb = {
1595 			.buf_size = 0x30000,
1596 		},
1597 		.short_term_rps = {
1598 			// SHORT_TERM_RPS - Max 64 set, 16 entry every set, total 64x16x2 = 2048 bytes (0x800)
1599 			.buf_size = 0x800,
1600 		},
1601 		.vps = {
1602 			// VPS STORE AREA - Max 16 VPS, each has 0x80 bytes, total 0x0800 bytes
1603 			.buf_size = 0x800,
1604 		},
1605 		.seg_map = {
1606 			// SEGMENT MAP AREA - 1920x1088/4/4 * 3 bits = 0xBF40 Bytes * 16 = 0xBF400
1607 			.buf_size = 0xBF400,
1608 		},
1609 		.daala_top = {
1610 			// DAALA TOP STORE AREA - 224 Bytes (use 256 Bytes for LPDDR4) per 128. Total 4096/128*256 = 0x2000
1611 			.buf_size = 0x2000,
1612 		},
1613 		.sao_up = {
1614 			// SAO UP STORE AREA - Max 640(10240/16) LCU, each has 16 bytes total 0x2800 bytes
1615 			.buf_size = 0x2800,
1616 		},
1617 		.swap_buf = {
1618 			// 256cyclex64bit = 2K bytes 0x800 (only 144 cycles valid)
1619 			.buf_size = 0x800,
1620 		},
1621 		.cdf_buf = {
1622 		// for context store/load 1024x256 x16 = 512K bytes 16*0x8000
1623 			.buf_size = 0x80000,
1624 		},
1625 		.gmc_buf = {
1626 		// for gmc_parameter store/load 128 x 16 = 2K bytes 0x800
1627 			.buf_size = 0x800,
1628 		},
1629 		.scalelut = {
1630 			// support up to 32 SCALELUT 1024x32 = 32Kbytes (0x8000)
1631 			.buf_size = 0x8000,
1632 		},
1633 		.dblk_para = {
1634 			// DBLK -> Max 256(4096/16) LCU, each para 1024bytes(total:0x40000), data 1024bytes(total:0x40000)
1635 			.buf_size = 0x80000,
1636 		},
1637 		.dblk_data = {
1638 			.buf_size = 0x80000,
1639 		},
1640 		.cdef_data = {
1641 			.buf_size = 0x40000,
1642 		},
1643 		.ups_data = {
1644 			.buf_size = 0x60000,
1645 		},
1646 		.fgs_table = {
1647 		  .buf_size = FGS_TABLE_SIZE * FRAME_BUFFERS, // 512x128bits
1648 		},
1649 #ifdef AOM_AV1_MMU
1650 #define VBH_BUF_SIZE (2 * 16 * 2304)
1651 #define VBH_BUF_COUNT 4
1652 		.mmu_vbh = {
1653 		  .buf_size = VBH_BUF_SIZE * VBH_BUF_COUNT,
1654 		  //.buf_size = 0x5000, //2*16*(more than 2304)/4, 4K
1655 		},
1656 		.cm_header = {
1657 		  //.buf_size = MMU_COMPRESS_HEADER_SIZE*8, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1658 		  .buf_size = MMU_COMPRESS_HEADER_SIZE*FRAME_BUFFERS, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1659 		},
1660 #endif
1661 #ifdef AOM_AV1_MMU_DW
1662 		.mmu_vbh_dw = {
1663 		  .buf_size = VBH_BUF_SIZE * VBH_BUF_COUNT,
1664 		  //.buf_size = 0x5000, //2*16*(more than 2304)/4, 4K
1665 		},
1666 		.cm_header_dw = {
1667 		  //.buf_size = MMU_COMPRESS_HEADER_SIZE*8, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1668 		  .buf_size = MMU_COMPRESS_HEADER_SIZE_DW*FRAME_BUFFERS, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1669 		},
1670 #endif
1671 		.mpred_above = {
1672 			.buf_size = 0x10000, /* 2 * size of hw*/
1673 		},
1674 #ifdef MV_USE_FIXED_BUF
1675 		.mpred_mv = {
1676 		   .buf_size = 0x40000*FRAME_BUFFERS, //1080p, 0x40000 per buffer
1677 		},
1678 #endif
1679 		.rpm = {
1680 		   .buf_size = 0x80*2,
1681 		},
1682 		.lmem = {
1683 			.buf_size = 0x400 * 2,
1684 		}
1685 	},
1686 	{
1687 #ifdef VPU_FILMGRAIN_DUMP
1688 		.max_width = 640,
1689 		.max_height = 480,
1690 #else
1691 		.max_width = 4096,
1692 		.max_height = 2304,
1693 #endif
1694 		.ipp = {
1695 			// IPP work space calculation : 4096 * (Y+CbCr+Flags) = 12k, round to 16k
1696 			.buf_size = 0x4000,
1697 		},
1698 		.sao_abv = {
1699 			.buf_size = 0x30000,
1700 		},
1701 		.sao_vb = {
1702 			.buf_size = 0x30000,
1703 		},
1704 		.short_term_rps = {
1705 			// SHORT_TERM_RPS - Max 64 set, 16 entry every set, total 64x16x2 = 2048 bytes (0x800)
1706 			.buf_size = 0x800,
1707 		},
1708 		.vps = {
1709 			// VPS STORE AREA - Max 16 VPS, each has 0x80 bytes, total 0x0800 bytes
1710 			.buf_size = 0x800,
1711 		},
1712 		.seg_map = {
1713 			// SEGMENT MAP AREA - 4096x2304/4/4 * 3 bits = 0x36000 Bytes * 16 = 0x360000
1714 			.buf_size = 0x360000,
1715 		},
1716 		.daala_top = {
1717 			// DAALA TOP STORE AREA - 224 Bytes (use 256 Bytes for LPDDR4) per 128. Total 4096/128*256 = 0x2000
1718 			.buf_size = 0x2000,
1719 		},
1720 		.sao_up = {
1721 			// SAO UP STORE AREA - Max 640(10240/16) LCU, each has 16 bytes total 0x2800 bytes
1722 			.buf_size = 0x2800,
1723 		},
1724 		.swap_buf = {
1725 			// 256cyclex64bit = 2K bytes 0x800 (only 144 cycles valid)
1726 			.buf_size = 0x800,
1727 		},
1728 		.cdf_buf = {
1729 		// for context store/load 1024x256 x16 = 512K bytes 16*0x8000
1730 			.buf_size = 0x80000,
1731 		},
1732 		.gmc_buf = {
1733 		// for gmc_parameter store/load 128 x 16 = 2K bytes 0x800
1734 			.buf_size = 0x800,
1735 		},
1736 		.scalelut = {
1737 			// support up to 32 SCALELUT 1024x32 = 32Kbytes (0x8000)
1738 			.buf_size = 0x8000,
1739 		},
1740 		.dblk_para = {
1741 			// DBLK -> Max 256(4096/16) LCU, each para 1024bytes(total:0x40000), data 1024bytes(total:0x40000)
1742 			.buf_size = 0x80000,
1743 		},
1744 		.dblk_data = {
1745 			.buf_size = 0x80000,
1746 		},
1747 		.cdef_data = {
1748 			.buf_size = 0x40000,
1749 		},
1750 		.ups_data = {
1751 			.buf_size = 0x60000,
1752 		},
1753 		.fgs_table = {
1754 		  .buf_size = FGS_TABLE_SIZE * FRAME_BUFFERS, // 512x128bits
1755 		},
1756 #ifdef AOM_AV1_MMU
1757 		.mmu_vbh = {
1758 		  .buf_size = VBH_BUF_SIZE * VBH_BUF_COUNT,
1759 		  //.buf_size = 0x5000, //2*16*(more than 2304)/4, 4K
1760 		},
1761 		.cm_header = {
1762 		  //.buf_size = MMU_COMPRESS_HEADER_SIZE*8, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1763 		  .buf_size = MMU_COMPRESS_HEADER_SIZE*FRAME_BUFFERS, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1764 		},
1765 #endif
1766 #ifdef AOM_AV1_MMU_DW
1767 		.mmu_vbh_dw = {
1768 		  .buf_size = VBH_BUF_SIZE * VBH_BUF_COUNT,
1769 		  //.buf_size = 0x5000, //2*16*(more than 2304)/4, 4K
1770 		},
1771 		.cm_header_dw = {
1772 		  //.buf_size = MMU_COMPRESS_HEADER_SIZE*8, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1773 		  .buf_size = MMU_COMPRESS_HEADER_SIZE_DW*FRAME_BUFFERS, // 0x44000 = ((1088*2*1024*4)/32/4)*(32/8)
1774 		},
1775 #endif
1776 		.mpred_above = {
1777 			.buf_size = 0x10000, /* 2 * size of hw*/
1778 		},
1779 #ifdef MV_USE_FIXED_BUF
1780 		.mpred_mv = {
1781 		  /* .buf_size = 0x100000*16,
1782 		  //4k2k , 0x100000 per buffer */
1783 		  /* 4096x2304 , 0x120000 per buffer */
1784 #if (defined MAP_8K)
1785 #define MAX_ONE_MV_BUFFER_SIZE   0x260000
1786 #else
1787 #define MAX_ONE_MV_BUFFER_SIZE   0x130000
1788 #endif
1789 		  .buf_size = MAX_ONE_MV_BUFFER_SIZE * FRAME_BUFFERS,
1790 		},
1791 #endif
1792 		.rpm = {
1793 		   .buf_size = 0x80*2,
1794 		},
1795 		.lmem = {
1796 			.buf_size = 0x400 * 2,
1797 		}
1798 
1799 	}
1800 };
1801 
1802 
1803 /*
1804 * AUX DATA Process
1805 */
1806 static u32 init_aux_size;
aux_data_is_avaible(struct AV1HW_s * hw)1807 static int aux_data_is_avaible(struct AV1HW_s *hw)
1808 {
1809 	u32 reg_val;
1810 
1811 	reg_val = READ_VREG(HEVC_AUX_DATA_SIZE);
1812 	if (reg_val != 0 && reg_val != init_aux_size)
1813 		return 1;
1814 	else
1815 		return 0;
1816 }
1817 
config_aux_buf(struct AV1HW_s * hw)1818 static void config_aux_buf(struct AV1HW_s *hw)
1819 {
1820 	WRITE_VREG(HEVC_AUX_ADR, hw->aux_phy_addr);
1821 	init_aux_size = ((hw->prefix_aux_size >> 4) << 16) |
1822 		(hw->suffix_aux_size >> 4);
1823 	WRITE_VREG(HEVC_AUX_DATA_SIZE, init_aux_size);
1824 }
1825 
1826 /*
1827 * dv_meta_flag: 1, dolby meta (T35) only; 2, not include dolby meta (T35)
1828 */
set_aux_data(struct AV1HW_s * hw,char ** aux_data_buf,int * aux_data_size,unsigned char suffix_flag,unsigned char dv_meta_flag)1829 static void set_aux_data(struct AV1HW_s *hw,
1830 	    char **aux_data_buf, int *aux_data_size,
1831 	unsigned char suffix_flag,
1832 	unsigned char dv_meta_flag)
1833 {
1834 	int i;
1835 	unsigned short *aux_adr;
1836 	unsigned int size_reg_val =
1837 		READ_VREG(HEVC_AUX_DATA_SIZE);
1838 	unsigned int aux_count = 0;
1839 	int aux_size = 0;
1840 	if (0 == aux_data_is_avaible(hw))
1841 		return;
1842 
1843 	if (hw->aux_data_dirty ||
1844 		hw->m_ins_flag == 0) {
1845 
1846 		hw->aux_data_dirty = 0;
1847 	}
1848 
1849 	if (suffix_flag) {
1850 		aux_adr = (unsigned short *)
1851 			(hw->aux_addr +
1852 			hw->prefix_aux_size);
1853 		aux_count =
1854 		((size_reg_val & 0xffff) << 4)
1855 			>> 1;
1856 		aux_size =
1857 			hw->suffix_aux_size;
1858 	} else {
1859 		aux_adr =
1860 		(unsigned short *)hw->aux_addr;
1861 		aux_count =
1862 		((size_reg_val >> 16) << 4)
1863 			>> 1;
1864 		aux_size =
1865 			hw->prefix_aux_size;
1866 	}
1867 	if (debug & AV1_DEBUG_BUFMGR_MORE) {
1868 		av1_print(hw, 0,
1869 			"%s:old size %d count %d,suf %d dv_flag %d\r\n",
1870 			__func__, *aux_data_size,
1871 			aux_count, suffix_flag, dv_meta_flag);
1872 	}
1873 	if (aux_size > 0 && aux_count > 0) {
1874 		int heads_size = 0;
1875 		int new_size;
1876 		char *new_buf;
1877 
1878 		for (i = 0; i < aux_count; i++) {
1879 			unsigned char tag = aux_adr[i] >> 8;
1880 			if (tag != 0 && tag != 0xff) {
1881 				if (dv_meta_flag == 0)
1882 					heads_size += 8;
1883 				else if (dv_meta_flag == 1 && tag == 0x14)
1884 					heads_size += 8;
1885 				else if (dv_meta_flag == 2 && tag != 0x14)
1886 					heads_size += 8;
1887 			}
1888 		}
1889 		new_size = *aux_data_size + aux_count + heads_size;
1890 		new_buf = vmalloc(new_size);
1891 		if (new_buf) {
1892 			unsigned char valid_tag = 0;
1893 			unsigned char *h =
1894 				new_buf +
1895 				*aux_data_size;
1896 			unsigned char *p = h + 8;
1897 			int len = 0;
1898 			int padding_len = 0;
1899 			if (*aux_data_buf) {
1900 				memcpy(new_buf, *aux_data_buf,  *aux_data_size);
1901 				vfree(*aux_data_buf);
1902 			}
1903 			*aux_data_buf = new_buf;
1904 			for (i = 0; i < aux_count; i += 4) {
1905 				int ii;
1906 				unsigned char tag = aux_adr[i + 3] >> 8;
1907 				if (tag != 0 && tag != 0xff) {
1908 					if (dv_meta_flag == 0)
1909 						valid_tag = 1;
1910 					else if (dv_meta_flag == 1
1911 						&& tag == 0x14)
1912 						valid_tag = 1;
1913 					else if (dv_meta_flag == 2
1914 						&& tag != 0x14)
1915 						valid_tag = 1;
1916 					else
1917 						valid_tag = 0;
1918 					if (valid_tag && len > 0) {
1919 						*aux_data_size +=
1920 						(len + 8);
1921 						h[0] = (len >> 24)
1922 						& 0xff;
1923 						h[1] = (len >> 16)
1924 						& 0xff;
1925 						h[2] = (len >> 8)
1926 						& 0xff;
1927 						h[3] = (len >> 0)
1928 						& 0xff;
1929 						h[6] =
1930 						(padding_len >> 8)
1931 						& 0xff;
1932 						h[7] = (padding_len)
1933 						& 0xff;
1934 						h += (len + 8);
1935 						p += 8;
1936 						len = 0;
1937 						padding_len = 0;
1938 					}
1939 					if (valid_tag) {
1940 						h[4] = tag;
1941 						h[5] = 0;
1942 						h[6] = 0;
1943 						h[7] = 0;
1944 					}
1945 				}
1946 				if (valid_tag) {
1947 					for (ii = 0; ii < 4; ii++) {
1948 						unsigned short aa =
1949 							aux_adr[i + 3
1950 							- ii];
1951 						*p = aa & 0xff;
1952 						p++;
1953 						len++;
1954 						if ((aa >> 8) == 0xff)
1955 							padding_len++;
1956 					}
1957 				}
1958 			}
1959 			if (len > 0) {
1960 				*aux_data_size += (len + 8);
1961 				h[0] = (len >> 24) & 0xff;
1962 				h[1] = (len >> 16) & 0xff;
1963 				h[2] = (len >> 8) & 0xff;
1964 				h[3] = (len >> 0) & 0xff;
1965 				h[6] = (padding_len >> 8) & 0xff;
1966 				h[7] = (padding_len) & 0xff;
1967 			}
1968 			if (debug & AV1_DEBUG_BUFMGR_MORE) {
1969 				av1_print(hw, 0,
1970 					"aux: (size %d) suffix_flag %d\n",
1971 					*aux_data_size, suffix_flag);
1972 				for (i = 0; i < *aux_data_size; i++) {
1973 					av1_print_cont(hw, 0,
1974 						"%02x ", (*aux_data_buf)[i]);
1975 					if (((i + 1) & 0xf) == 0)
1976 						av1_print_cont(hw, 0, "\n");
1977 				}
1978 				av1_print_cont(hw, 0, "\n");
1979 			}
1980 
1981 		} else {
1982 			av1_print(hw, 0, "new buf alloc failed\n");
1983 			if (*aux_data_buf)
1984 				vfree(*aux_data_buf);
1985 			*aux_data_buf = NULL;
1986 			*aux_data_size = 0;
1987 		}
1988 	}
1989 
1990 }
1991 
set_dv_data(struct AV1HW_s * hw)1992 static void set_dv_data(struct AV1HW_s *hw)
1993 {
1994 	set_aux_data(hw, &hw->dv_data_buf,
1995 		&hw->dv_data_size, 0, 1);
1996 
1997 }
1998 
set_pic_aux_data(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic,unsigned char suffix_flag,unsigned char dv_meta_flag)1999 static void set_pic_aux_data(struct AV1HW_s *hw,
2000 	struct PIC_BUFFER_CONFIG_s *pic, unsigned char suffix_flag,
2001 	unsigned char dv_meta_flag)
2002 {
2003 	if (pic == NULL)
2004 		return;
2005 	set_aux_data(hw, &pic->aux_data_buf,
2006 		&pic->aux_data_size, suffix_flag, dv_meta_flag);
2007 }
2008 
copy_dv_data(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic)2009 static void copy_dv_data(struct AV1HW_s *hw,
2010 	struct PIC_BUFFER_CONFIG_s *pic)
2011 {
2012 	char *new_buf;
2013 	int new_size;
2014 	new_size = pic->aux_data_size + hw->dv_data_size;
2015 	new_buf = vmalloc(new_size);
2016 	if (new_buf) {
2017 		if (debug & AV1_DEBUG_BUFMGR_MORE) {
2018 			av1_print(hw, 0,
2019 				"%s: (size %d) pic index %d\n",
2020 				__func__,
2021 				hw->dv_data_size, pic->index);
2022 		}
2023 		if (pic->aux_data_buf) {
2024 			memcpy(new_buf, pic->aux_data_buf,  pic->aux_data_size);
2025 			vfree(pic->aux_data_buf);
2026 		}
2027 		memcpy(new_buf + pic->aux_data_size, hw->dv_data_buf, hw->dv_data_size);
2028 		pic->aux_data_size += hw->dv_data_size;
2029 		pic->aux_data_buf = new_buf;
2030 		vfree(hw->dv_data_buf);
2031 		hw->dv_data_buf = NULL;
2032 		hw->dv_data_size = 0;
2033 	}
2034 
2035 }
2036 
release_aux_data(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic)2037 static void release_aux_data(struct AV1HW_s *hw,
2038 	struct PIC_BUFFER_CONFIG_s *pic)
2039 {
2040 	if (pic->aux_data_buf)
2041 		vfree(pic->aux_data_buf);
2042 	pic->aux_data_buf = NULL;
2043 	pic->aux_data_size = 0;
2044 }
2045 
dump_aux_buf(struct AV1HW_s * hw)2046 static void dump_aux_buf(struct AV1HW_s *hw)
2047 {
2048 	int i;
2049 	unsigned short *aux_adr =
2050 		(unsigned short *)
2051 		hw->aux_addr;
2052 	unsigned int aux_size =
2053 		(READ_VREG(HEVC_AUX_DATA_SIZE)
2054 		>> 16) << 4;
2055 
2056 	if (hw->prefix_aux_size > 0) {
2057 		av1_print(hw, 0,
2058 			"prefix aux: (size %d)\n",
2059 			aux_size);
2060 		for (i = 0; i <
2061 		(aux_size >> 1); i++) {
2062 			av1_print_cont(hw, 0,
2063 				"%04x ",
2064 				*(aux_adr + i));
2065 			if (((i + 1) & 0xf)
2066 				== 0)
2067 				av1_print_cont(hw,
2068 				0, "\n");
2069 		}
2070 	}
2071 	if (hw->suffix_aux_size > 0) {
2072 		aux_adr = (unsigned short *)
2073 			(hw->aux_addr +
2074 			hw->prefix_aux_size);
2075 		aux_size =
2076 		(READ_VREG(HEVC_AUX_DATA_SIZE) & 0xffff)
2077 			<< 4;
2078 		av1_print(hw, 0,
2079 			"suffix aux: (size %d)\n",
2080 			aux_size);
2081 		for (i = 0; i <
2082 		(aux_size >> 1); i++) {
2083 			av1_print_cont(hw, 0,
2084 				"%04x ", *(aux_adr + i));
2085 			if (((i + 1) & 0xf) == 0)
2086 				av1_print_cont(hw, 0, "\n");
2087 		}
2088 	}
2089 }
2090 
2091 /*
2092 *
2093 */
2094 
2095 /*Losless compression body buffer size 4K per 64x32 (jt)*/
compute_losless_comp_body_size(int width,int height,uint8_t is_bit_depth_10)2096 static int compute_losless_comp_body_size(int width, int height,
2097 				uint8_t is_bit_depth_10)
2098 {
2099 	int     width_x64;
2100 	int     height_x32;
2101 	int     bsize;
2102 
2103 	width_x64 = width + 63;
2104 	width_x64 >>= 6;
2105 	height_x32 = height + 31;
2106 	height_x32 >>= 5;
2107 	bsize = (is_bit_depth_10?4096:3200)*width_x64*height_x32;
2108 	if (debug & AV1_DEBUG_BUFMGR_MORE)
2109 		pr_info("%s(%d,%d,%d)=>%d\n",
2110 			__func__, width, height,
2111 			is_bit_depth_10, bsize);
2112 
2113 	return  bsize;
2114 }
2115 
2116 /* Losless compression header buffer size 32bytes per 128x64 (jt)*/
compute_losless_comp_header_size(int width,int height)2117 static  int  compute_losless_comp_header_size(int width, int height)
2118 {
2119 	int     width_x128;
2120 	int     height_x64;
2121 	int     hsize;
2122 
2123 	width_x128 = width + 127;
2124 	width_x128 >>= 7;
2125 	height_x64 = height + 63;
2126 	height_x64 >>= 6;
2127 
2128 	hsize = 32 * width_x128 * height_x64;
2129 	if (debug & AV1_DEBUG_BUFMGR_MORE)
2130 		pr_info("%s(%d,%d)=>%d\n",
2131 			__func__, width, height,
2132 			hsize);
2133 
2134 	return  hsize;
2135 }
2136 
2137 #ifdef AOM_AV1_MMU_DW
compute_losless_comp_body_size_dw(int width,int height,uint8_t is_bit_depth_10)2138 static int compute_losless_comp_body_size_dw(int width, int height,
2139 				uint8_t is_bit_depth_10)
2140 {
2141 
2142 	return compute_losless_comp_body_size(width, height, is_bit_depth_10);
2143 }
2144 
2145 /* Losless compression header buffer size 32bytes per 128x64 (jt)*/
compute_losless_comp_header_size_dw(int width,int height)2146 static int compute_losless_comp_header_size_dw(int width, int height)
2147 {
2148 	return compute_losless_comp_header_size(width, height);
2149 }
2150 #endif
2151 
init_buff_spec(struct AV1HW_s * hw,struct BuffInfo_s * buf_spec)2152 static void init_buff_spec(struct AV1HW_s *hw,
2153 	struct BuffInfo_s *buf_spec)
2154 {
2155 	void *mem_start_virt;
2156 
2157     buf_spec->ipp.buf_start =
2158 		buf_spec->start_adr;
2159     buf_spec->sao_abv.buf_start =
2160 		buf_spec->ipp.buf_start + buf_spec->ipp.buf_size;
2161 
2162     buf_spec->sao_vb.buf_start =
2163 		buf_spec->sao_abv.buf_start + buf_spec->sao_abv.buf_size;
2164     buf_spec->short_term_rps.buf_start =
2165 		buf_spec->sao_vb.buf_start + buf_spec->sao_vb.buf_size;
2166     buf_spec->vps.buf_start =
2167 		buf_spec->short_term_rps.buf_start + buf_spec->short_term_rps.buf_size;
2168     buf_spec->seg_map.buf_start =
2169 		buf_spec->vps.buf_start + buf_spec->vps.buf_size;
2170     buf_spec->daala_top.buf_start =
2171 		buf_spec->seg_map.buf_start + buf_spec->seg_map.buf_size;
2172     buf_spec->sao_up.buf_start =
2173 		buf_spec->daala_top.buf_start + buf_spec->daala_top.buf_size;
2174     buf_spec->swap_buf.buf_start =
2175 		buf_spec->sao_up.buf_start + buf_spec->sao_up.buf_size;
2176     buf_spec->cdf_buf.buf_start =
2177 		buf_spec->swap_buf.buf_start + buf_spec->swap_buf.buf_size;
2178     buf_spec->gmc_buf.buf_start =
2179 		buf_spec->cdf_buf.buf_start + buf_spec->cdf_buf.buf_size;
2180     buf_spec->scalelut.buf_start =
2181 		buf_spec->gmc_buf.buf_start + buf_spec->gmc_buf.buf_size;
2182     buf_spec->dblk_para.buf_start =
2183 		buf_spec->scalelut.buf_start + buf_spec->scalelut.buf_size;
2184     buf_spec->dblk_data.buf_start =
2185 		buf_spec->dblk_para.buf_start + buf_spec->dblk_para.buf_size;
2186     buf_spec->cdef_data.buf_start =
2187 		buf_spec->dblk_data.buf_start + buf_spec->dblk_data.buf_size;
2188     buf_spec->ups_data.buf_start =
2189 		buf_spec->cdef_data.buf_start + buf_spec->cdef_data.buf_size;
2190     buf_spec->fgs_table.buf_start =
2191 		buf_spec->ups_data.buf_start + buf_spec->ups_data.buf_size;
2192 #ifdef AOM_AV1_MMU
2193     buf_spec->mmu_vbh.buf_start =
2194 		buf_spec->fgs_table.buf_start + buf_spec->fgs_table.buf_size;
2195     buf_spec->cm_header.buf_start =
2196 		buf_spec->mmu_vbh.buf_start + buf_spec->mmu_vbh.buf_size;
2197 #ifdef AOM_AV1_MMU_DW
2198     buf_spec->mmu_vbh_dw.buf_start =
2199 		buf_spec->cm_header.buf_start + buf_spec->cm_header.buf_size;
2200     buf_spec->cm_header_dw.buf_start =
2201 		buf_spec->mmu_vbh_dw.buf_start + buf_spec->mmu_vbh_dw.buf_size;
2202     buf_spec->mpred_above.buf_start =
2203 		buf_spec->cm_header_dw.buf_start + buf_spec->cm_header_dw.buf_size;
2204 #else
2205     buf_spec->mpred_above.buf_start =
2206 		buf_spec->cm_header.buf_start + buf_spec->cm_header.buf_size;
2207 #endif
2208 #else
2209     buf_spec->mpred_above.buf_start =
2210 		buf_spec->fgs_table.buf_start + buf_spec->fgs_table.buf_size;
2211 #endif
2212 
2213 #ifdef MV_USE_FIXED_BUF
2214 	buf_spec->mpred_mv.buf_start =
2215 		buf_spec->mpred_above.buf_start +
2216 		buf_spec->mpred_above.buf_size;
2217 
2218 	buf_spec->rpm.buf_start =
2219 		buf_spec->mpred_mv.buf_start +
2220 		buf_spec->mpred_mv.buf_size;
2221 #else
2222 	buf_spec->rpm.buf_start =
2223 		buf_spec->mpred_above.buf_start +
2224 		buf_spec->mpred_above.buf_size;
2225 
2226 #endif
2227 	buf_spec->lmem.buf_start =
2228 		buf_spec->rpm.buf_start +
2229 		buf_spec->rpm.buf_size;
2230 	buf_spec->end_adr =
2231 		buf_spec->lmem.buf_start +
2232 		buf_spec->lmem.buf_size;
2233 
2234 	if (!hw)
2235 		return;
2236 
2237 	if (!vdec_secure(hw_to_vdec(hw))) {
2238 		mem_start_virt =
2239 			codec_mm_phys_to_virt(buf_spec->dblk_para.buf_start);
2240 		if (mem_start_virt) {
2241 			memset(mem_start_virt, 0,
2242 				buf_spec->dblk_para.buf_size);
2243 			codec_mm_dma_flush(mem_start_virt,
2244 				buf_spec->dblk_para.buf_size,
2245 				DMA_TO_DEVICE);
2246 		} else {
2247 			mem_start_virt = codec_mm_vmap(
2248 				buf_spec->dblk_para.buf_start,
2249 				buf_spec->dblk_para.buf_size);
2250 			if (mem_start_virt) {
2251 				memset(mem_start_virt, 0,
2252 					buf_spec->dblk_para.buf_size);
2253 				codec_mm_dma_flush(mem_start_virt,
2254 					buf_spec->dblk_para.buf_size,
2255 					DMA_TO_DEVICE);
2256 				codec_mm_unmap_phyaddr(mem_start_virt);
2257 			} else {
2258 				/*not virt for tvp playing,
2259 				may need clear on ucode.*/
2260 				pr_err("mem_start_virt failed\n");
2261 			}
2262 		}
2263 	}
2264 
2265 	if (debug) {
2266 		pr_info("%s workspace (%x %x) size = %x\n", __func__,
2267 			   buf_spec->start_adr, buf_spec->end_adr,
2268 			   buf_spec->end_adr - buf_spec->start_adr);
2269 	}
2270 
2271 	if (debug) {
2272 		pr_info("ipp.buf_start    		 :%x\n",
2273 			   buf_spec->ipp.buf_start);
2274 		pr_info("sao_abv.buf_start    	  :%x\n",
2275 			   buf_spec->sao_abv.buf_start);
2276 		pr_info("sao_vb.buf_start    	  :%x\n",
2277 			   buf_spec->sao_vb.buf_start);
2278 		pr_info("short_term_rps.buf_start  :%x\n",
2279 			   buf_spec->short_term_rps.buf_start);
2280 		pr_info("vps.buf_start    		 :%x\n",
2281 			   buf_spec->vps.buf_start);
2282 		pr_info("seg_map.buf_start    	  :%x\n",
2283 			   buf_spec->seg_map.buf_start);
2284 		pr_info("daala_top.buf_start    	  :%x\n",
2285 			   buf_spec->daala_top.buf_start);
2286 		pr_info("swap_buf.buf_start    	:%x\n",
2287 			   buf_spec->swap_buf.buf_start);
2288 		pr_info("cdf_buf.buf_start    	:%x\n",
2289 			   buf_spec->cdf_buf.buf_start);
2290 		pr_info("gmc_buf.buf_start    	:%x\n",
2291 			   buf_spec->gmc_buf.buf_start);
2292 		pr_info("scalelut.buf_start    	:%x\n",
2293 			   buf_spec->scalelut.buf_start);
2294 		pr_info("dblk_para.buf_start       :%x\n",
2295 			   buf_spec->dblk_para.buf_start);
2296 		pr_info("dblk_data.buf_start       :%x\n",
2297 			   buf_spec->dblk_data.buf_start);
2298 		pr_info("cdef_data.buf_start       :%x\n",
2299 				buf_spec->cdef_data.buf_start);
2300 		pr_info("ups_data.buf_start       :%x\n",
2301 				buf_spec->ups_data.buf_start);
2302 
2303 #ifdef AOM_AV1_MMU
2304 		pr_info("mmu_vbh.buf_start     :%x\n",
2305 			buf_spec->mmu_vbh.buf_start);
2306 #endif
2307 		pr_info("mpred_above.buf_start     :%x\n",
2308 			   buf_spec->mpred_above.buf_start);
2309 #ifdef MV_USE_FIXED_BUF
2310 		pr_info("mpred_mv.buf_start    	:%x\n",
2311 			   buf_spec->mpred_mv.buf_start);
2312 #endif
2313 		if ((debug & AOM_AV1_DEBUG_SEND_PARAM_WITH_REG) == 0) {
2314 			pr_info("rpm.buf_start    		 :%x\n",
2315 				   buf_spec->rpm.buf_start);
2316 		}
2317 	}
2318 }
2319 
2320 
2321 
uninit_mmu_buffers(struct AV1HW_s * hw)2322 static void uninit_mmu_buffers(struct AV1HW_s *hw)
2323 {
2324 #ifndef MV_USE_FIXED_BUF
2325 	dealloc_mv_bufs(hw);
2326 #endif
2327 	if (hw->mmu_box)
2328 		decoder_mmu_box_free(hw->mmu_box);
2329 	hw->mmu_box = NULL;
2330 
2331 #ifdef AOM_AV1_MMU_DW
2332 	if (hw->mmu_box_dw)
2333 		decoder_mmu_box_free(hw->mmu_box_dw);
2334 	hw->mmu_box_dw = NULL;
2335 #endif
2336 	if (hw->bmmu_box)
2337 		decoder_bmmu_box_free(hw->bmmu_box);
2338 	hw->bmmu_box = NULL;
2339 }
2340 
2341 
config_pic(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic_config)2342 static int config_pic(struct AV1HW_s *hw,
2343 				struct PIC_BUFFER_CONFIG_s *pic_config)
2344 {
2345 	int ret = -1;
2346 	int i;
2347 	int pic_width = hw->init_pic_w;
2348 	int pic_height = hw->init_pic_h;
2349 	//int lcu_size = ((params->p.seq_flags >> 6) & 0x1) ? 128 : 64;
2350     int lcu_size = hw->current_lcu_size;
2351 
2352 	int pic_width_64 = (pic_width + 63) & (~0x3f);
2353 	int pic_height_32 = (pic_height + 31) & (~0x1f);
2354 	int pic_width_lcu  = (pic_width_64 % lcu_size) ?
2355 				pic_width_64 / lcu_size  + 1
2356 				: pic_width_64 / lcu_size;
2357 	int pic_height_lcu = (pic_height_32 % lcu_size) ?
2358 				pic_height_32 / lcu_size + 1
2359 				: pic_height_32 / lcu_size;
2360 	int lcu_total       = pic_width_lcu * pic_height_lcu;
2361 #ifdef MV_USE_FIXED_BUF
2362 	u32 mpred_mv_end = hw->work_space_buf->mpred_mv.buf_start +
2363 			hw->work_space_buf->mpred_mv.buf_size;
2364 #ifdef USE_DYNAMIC_MV_BUFFER
2365   int32_t MV_MEM_UNIT = (lcu_size == 128) ? (19*4*16) : (19*16);
2366   int32_t mv_buffer_size = (lcu_total*MV_MEM_UNIT);
2367 #else
2368   int32_t mv_buffer_size = MAX_ONE_MV_BUFFER_SIZE;
2369 #endif
2370 
2371 #endif
2372 
2373 	u32 y_adr = 0;
2374 	int buf_size = 0;
2375 
2376 	int losless_comp_header_size =
2377 			compute_losless_comp_header_size(pic_width,
2378 			pic_height);
2379 	int losless_comp_body_size = compute_losless_comp_body_size(pic_width,
2380 			pic_height, buf_alloc_depth == 10);
2381 	int mc_buffer_size = losless_comp_header_size + losless_comp_body_size;
2382 	int mc_buffer_size_h = (mc_buffer_size + 0xffff) >> 16;
2383 	int mc_buffer_size_u_v = 0;
2384 	int mc_buffer_size_u_v_h = 0;
2385 	int dw_mode = get_double_write_mode_init(hw);
2386 	struct vdec_v4l2_buffer *fb = NULL;
2387 
2388 	hw->lcu_total = lcu_total;
2389 
2390 	if (dw_mode && (dw_mode & 0x20) == 0) {
2391 		int pic_width_dw = pic_width /
2392 			get_double_write_ratio(hw, dw_mode);
2393 		int pic_height_dw = pic_height /
2394 			get_double_write_ratio(hw, dw_mode);
2395 
2396 		int pic_width_64_dw = (pic_width_dw + 63) & (~0x3f);
2397 		int pic_height_32_dw = (pic_height_dw + 31) & (~0x1f);
2398 		int pic_width_lcu_dw  = (pic_width_64_dw % lcu_size) ?
2399 					pic_width_64_dw / lcu_size  + 1
2400 					: pic_width_64_dw / lcu_size;
2401 		int pic_height_lcu_dw = (pic_height_32_dw % lcu_size) ?
2402 					pic_height_32_dw / lcu_size + 1
2403 					: pic_height_32_dw / lcu_size;
2404 		int lcu_total_dw       = pic_width_lcu_dw * pic_height_lcu_dw;
2405 		mc_buffer_size_u_v = lcu_total_dw * lcu_size * lcu_size / 2;
2406 		mc_buffer_size_u_v_h = (mc_buffer_size_u_v + 0xffff) >> 16;
2407 		/*64k alignment*/
2408 		buf_size = ((mc_buffer_size_u_v_h << 16) * 3);
2409 		buf_size = ((buf_size + 0xffff) >> 16) << 16;
2410 	}
2411 
2412 	if (mc_buffer_size & 0xffff) /*64k alignment*/
2413 		mc_buffer_size_h += 1;
2414 	if ((!hw->mmu_enable) && ((dw_mode & 0x10) == 0))
2415 		buf_size += (mc_buffer_size_h << 16);
2416 
2417 #ifdef USE_SPEC_BUF_FOR_MMU_HEAD
2418 	if (hw->mmu_enable) {
2419 		pic_config->header_adr =
2420 			hw->work_space_buf->cm_header.buf_start +
2421 					(pic_config->index * MMU_COMPRESS_HEADER_SIZE);
2422 
2423 #ifdef AOM_AV1_MMU_DW
2424 		if (hw->dw_mmu_enable) {
2425 			pic_config->header_dw_adr =
2426 				hw->work_space_buf->cm_header_dw.buf_start +
2427 						(pic_config->index * MMU_COMPRESS_HEADER_SIZE_DW);
2428 
2429 		}
2430 #endif
2431 	}
2432 
2433 #else
2434 /*!USE_SPEC_BUF_FOR_MMU_HEAD*/
2435 	if (hw->mmu_enable) {
2436 		pic_config->header_adr = decoder_bmmu_box_get_phy_addr(
2437 			hw->bmmu_box, HEADER_BUFFER_IDX(pic_config->index));
2438 
2439 #ifdef AOM_AV1_MMU_DW
2440 		if (hw->dw_mmu_enable) {
2441 			pic_config->header_dw_adr = decoder_bmmu_box_get_phy_addr(
2442 				hw->bmmu_box, DW_HEADER_BUFFER_IDX(pic_config->index));
2443 
2444 		}
2445 		if (debug & AV1_DEBUG_BUFMGR_MORE) {
2446 			pr_info("MMU dw header_adr (%d, %d) %d: %d\n",
2447 				hw->dw_mmu_enable,
2448 				DW_HEADER_BUFFER_IDX(pic_config->index),
2449 				pic_config->index,
2450 				pic_config->header_dw_adr);
2451 		}
2452 #endif
2453 
2454 		if (debug & AV1_DEBUG_BUFMGR_MORE) {
2455 			pr_info("MMU header_adr %d: %d\n",
2456 				pic_config->index, pic_config->header_adr);
2457 		}
2458 	}
2459 #endif
2460 
2461 	i = pic_config->index;
2462 #ifdef MV_USE_FIXED_BUF
2463 	if ((hw->work_space_buf->mpred_mv.buf_start +
2464 		((i + 1) * mv_buffer_size))
2465 		<= mpred_mv_end
2466 	) {
2467 #endif
2468 		if (buf_size > 0) {
2469 			if (hw->is_used_v4l) {
2470 #ifdef SUPPORT_V4L2
2471 				ret = vdec_v4l_get_buffer(hw->v4l2_ctx, &fb);
2472 #endif
2473 				if (ret) {
2474 					av1_print(hw, PRINT_FLAG_ERROR,
2475 						"[%d] get fb fail.\n",
2476 						((struct aml_vcodec_ctx *)
2477 						(hw->v4l2_ctx))->id);
2478 					return ret;
2479 				}
2480 
2481 				hw->m_BUF[i].v4l_ref_buf_addr = (ulong)fb;
2482 #ifdef SUPPORT_V4L2
2483 				pic_config->cma_alloc_addr = fb->m.mem[0].addr;
2484 #endif
2485 				av1_print(hw, PRINT_FLAG_V4L_DETAIL,
2486 					"[%d] %s(), v4l ref buf addr: 0x%x\n",
2487 					((struct aml_vcodec_ctx *)
2488 					(hw->v4l2_ctx))->id, __func__, fb);
2489 			} else {
2490 				ret = decoder_bmmu_box_alloc_buf_phy(hw->bmmu_box,
2491 						VF_BUFFER_IDX(i),
2492 						buf_size, DRIVER_NAME,
2493 						&pic_config->cma_alloc_addr);
2494 				if (ret < 0) {
2495 					pr_info(
2496 						"decoder_bmmu_box_alloc_buf_phy idx %d size %d fail\n",
2497 						VF_BUFFER_IDX(i),
2498 						buf_size
2499 						);
2500 					return ret;
2501 				}
2502 			}
2503 
2504 			if (pic_config->cma_alloc_addr)
2505 				y_adr = pic_config->cma_alloc_addr;
2506 			else {
2507 				pr_info(
2508 					"decoder_bmmu_box_alloc_buf_phy idx %d size %d return null\n",
2509 					VF_BUFFER_IDX(i),
2510 					buf_size
2511 					);
2512 				return -1;
2513 			}
2514 		}
2515 		{
2516 			/*ensure get_pic_by_POC()
2517 			not get the buffer not decoded*/
2518 			pic_config->BUF_index = i;
2519 			pic_config->lcu_total = lcu_total;
2520 
2521 			pic_config->comp_body_size = losless_comp_body_size;
2522 			pic_config->buf_size = buf_size;
2523 
2524 			pic_config->mc_canvas_y = pic_config->index;
2525 			pic_config->mc_canvas_u_v = pic_config->index;
2526 			if (dw_mode & 0x10) {
2527 				pic_config->dw_y_adr = y_adr;
2528 				pic_config->dw_u_v_adr = y_adr +
2529 					((mc_buffer_size_u_v_h << 16) << 1);
2530 
2531 				pic_config->mc_canvas_y =
2532 					(pic_config->index << 1);
2533 				pic_config->mc_canvas_u_v =
2534 					(pic_config->index << 1) + 1;
2535 			} else if (dw_mode && (dw_mode & 0x20) == 0) {
2536 				pic_config->dw_y_adr = y_adr;
2537 				pic_config->dw_u_v_adr = pic_config->dw_y_adr +
2538 				((mc_buffer_size_u_v_h << 16) << 1);
2539 			}
2540 #ifdef MV_USE_FIXED_BUF
2541 			pic_config->mpred_mv_wr_start_addr =
2542 			hw->work_space_buf->mpred_mv.buf_start +
2543 					(pic_config->index * mv_buffer_size);
2544 #endif
2545 #ifdef DUMP_FILMGRAIN
2546 			if (pic_config->index == fg_dump_index) {
2547 				pic_config->fgs_table_adr = hw->fg_phy_addr;
2548 				pr_info("set buffer %d film grain table 0x%x\n",
2549 					pic_config->index, pic_config->fgs_table_adr);
2550 			} else
2551 #endif
2552 		    pic_config->fgs_table_adr =
2553 				hw->work_space_buf->fgs_table.buf_start +
2554 				(pic_config->index * FGS_TABLE_SIZE);
2555 
2556 			if (debug) {
2557 				pr_info
2558 				("%s index %d BUF_index %d ",
2559 				__func__, pic_config->index,
2560 				pic_config->BUF_index);
2561 				pr_info
2562 				("comp_body_size %x comp_buf_size %x ",
2563 				pic_config->comp_body_size,
2564 				pic_config->buf_size);
2565 				pr_info
2566 				("mpred_mv_wr_start_adr %d\n",
2567 				pic_config->mpred_mv_wr_start_addr);
2568 				pr_info("dw_y_adr %d, pic_config->dw_u_v_adr =%d\n",
2569 					pic_config->dw_y_adr,
2570 					pic_config->dw_u_v_adr);
2571 			}
2572 			ret = 0;
2573 		}
2574 #ifdef MV_USE_FIXED_BUF
2575 	}
2576 #endif
2577 	return ret;
2578 }
2579 
2580 #ifndef USE_SPEC_BUF_FOR_MMU_HEAD
vav1_mmu_compress_header_size(struct AV1HW_s * hw)2581 static int vav1_mmu_compress_header_size(struct AV1HW_s *hw)
2582 {
2583 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
2584 		IS_8K_SIZE(hw->max_pic_w, hw->max_pic_h))
2585 		return (MMU_COMPRESS_8K_HEADER_SIZE);
2586 
2587 	return (MMU_COMPRESS_HEADER_SIZE);
2588 }
2589 #endif
2590 /*#define FRAME_MMU_MAP_SIZE  (MAX_FRAME_4K_NUM * 4)*/
vav1_frame_mmu_map_size(struct AV1HW_s * hw)2591 static int vav1_frame_mmu_map_size(struct AV1HW_s *hw)
2592 {
2593 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
2594 		IS_8K_SIZE(hw->max_pic_w, hw->max_pic_h))
2595 		return (MAX_FRAME_8K_NUM * 4);
2596 
2597 	return (MAX_FRAME_4K_NUM * 4);
2598 }
2599 
2600 #ifdef AOM_AV1_MMU_DW
vaom_dw_frame_mmu_map_size(struct AV1HW_s * hw)2601 static int vaom_dw_frame_mmu_map_size(struct AV1HW_s *hw)
2602 {
2603 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
2604 		IS_8K_SIZE(hw->max_pic_w, hw->max_pic_h))
2605 		return (MAX_FRAME_8K_NUM * 4);
2606 
2607 	return (MAX_FRAME_4K_NUM * 4);
2608 }
2609 #endif
2610 
init_pic_list(struct AV1HW_s * hw)2611 static void init_pic_list(struct AV1HW_s *hw)
2612 {
2613 	int i;
2614 	struct AV1_Common_s *cm = &hw->pbi->common;
2615 	struct PIC_BUFFER_CONFIG_s *pic_config;
2616 	struct vdec_s *vdec = hw_to_vdec(hw);
2617 
2618 #ifndef USE_SPEC_BUF_FOR_MMU_HEAD
2619 	u32 header_size;
2620 	if (hw->mmu_enable && ((hw->double_write_mode & 0x10) == 0)) {
2621 		header_size = vav1_mmu_compress_header_size(hw);
2622 		/*alloc AV1 compress header first*/
2623 		for (i = 0; i < hw->used_buf_num; i++) {
2624 			unsigned long buf_addr;
2625 			if (decoder_bmmu_box_alloc_buf_phy
2626 				(hw->bmmu_box,
2627 				HEADER_BUFFER_IDX(i), header_size,
2628 				DRIVER_HEADER_NAME,
2629 				&buf_addr) < 0) {
2630 				av1_print(hw, 0, "%s malloc compress header failed %d\n",
2631 				DRIVER_HEADER_NAME, i);
2632 				hw->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
2633 				return;
2634 			}
2635 #ifdef AOM_AV1_MMU_DW
2636 			if (hw->dw_mmu_enable) {
2637 				if (decoder_bmmu_box_alloc_buf_phy
2638 					(hw->bmmu_box,
2639 					DW_HEADER_BUFFER_IDX(i), header_size,
2640 					DRIVER_HEADER_NAME,
2641 					&buf_addr) < 0) {
2642 					av1_print(hw, 0, "%s malloc compress dw header failed %d\n",
2643 					DRIVER_HEADER_NAME, i);
2644 					hw->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
2645 					return;
2646 				}
2647 			}
2648 #endif
2649 		}
2650 	}
2651 #endif
2652 	for (i = 0; i < hw->used_buf_num; i++) {
2653 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
2654 		pic_config->index = i;
2655 		pic_config->BUF_index = -1;
2656 		pic_config->mv_buf_index = -1;
2657 		if (vdec->parallel_dec == 1) {
2658 			pic_config->y_canvas_index = -1;
2659 			pic_config->uv_canvas_index = -1;
2660 		}
2661 		if (config_pic(hw, pic_config) < 0) {
2662 			if (debug)
2663 				av1_print(hw, 0, "Config_pic %d fail\n",
2664 					pic_config->index);
2665 			pic_config->index = -1;
2666 			break;
2667 		}
2668 		pic_config->y_crop_width = hw->init_pic_w;
2669 		pic_config->y_crop_height = hw->init_pic_h;
2670 		pic_config->double_write_mode = get_double_write_mode(hw);
2671 
2672 		if (pic_config->double_write_mode &&
2673 			(pic_config->double_write_mode & 0x20) == 0) {
2674 			set_canvas(hw, pic_config);
2675 		}
2676 	}
2677 	for (; i < hw->used_buf_num; i++) {
2678 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
2679 		pic_config->index = -1;
2680 		pic_config->BUF_index = -1;
2681 		pic_config->mv_buf_index = -1;
2682 		if (vdec->parallel_dec == 1) {
2683 			pic_config->y_canvas_index = -1;
2684 			pic_config->uv_canvas_index = -1;
2685 		}
2686 	}
2687 	av1_print(hw, AV1_DEBUG_BUFMGR, "%s ok, used_buf_num = %d\n",
2688 		__func__, hw->used_buf_num);
2689 
2690 }
2691 
init_pic_list_hw(struct AV1HW_s * hw)2692 static void init_pic_list_hw(struct AV1HW_s *hw)
2693 {
2694 	int i;
2695 	struct AV1_Common_s *cm = &hw->pbi->common;
2696 	struct PIC_BUFFER_CONFIG_s *pic_config;
2697 	/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x0);*/
2698 	WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2699 		(0x1 << 1) | (0x1 << 2));
2700 
2701 
2702 	for (i = 0; i < hw->used_buf_num; i++) {
2703 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
2704 		if (pic_config->index < 0)
2705 			break;
2706 
2707 		if (hw->mmu_enable && ((pic_config->double_write_mode & 0x10) == 0)) {
2708 
2709 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
2710 				pic_config->header_adr >> 5);
2711 		} else {
2712 			/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
2713 			 *	pic_config->mc_y_adr
2714 			 *	| (pic_config->mc_canvas_y << 8) | 0x1);
2715 			 */
2716 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
2717 				pic_config->dw_y_adr >> 5);
2718 		}
2719 #ifndef LOSLESS_COMPRESS_MODE
2720 		/*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
2721 		 *	pic_config->mc_u_v_adr
2722 		 *	| (pic_config->mc_canvas_u_v << 8)| 0x1);
2723 		 */
2724 			WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
2725 				pic_config->dw_u_v_adr >> 5);
2726 #else
2727 		if (pic_config->double_write_mode & 0x10) {
2728 				WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA,
2729 				pic_config->dw_u_v_adr >> 5);
2730 		}
2731 #endif
2732 	}
2733 	WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x1);
2734 
2735 #ifdef CHANGE_REMOVED
2736 	/*Zero out canvas registers in IPP -- avoid simulation X*/
2737 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2738 			(0 << 8) | (0 << 1) | 1);
2739 #else
2740 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2741 			(1 << 8) | (0 << 1) | 1);
2742 #endif
2743 	for (i = 0; i < 32; i++)
2744 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
2745 }
2746 
2747 
dump_pic_list(struct AV1HW_s * hw)2748 static void dump_pic_list(struct AV1HW_s *hw)
2749 {
2750 	struct AV1_Common_s *const cm = &hw->pbi->common;
2751 	struct PIC_BUFFER_CONFIG_s *pic_config;
2752 	int i;
2753 	for (i = 0; i < FRAME_BUFFERS; i++) {
2754 		pic_config = &cm->buffer_pool->frame_bufs[i].buf;
2755 		av1_print(hw, 0,
2756 			"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",
2757 			i,
2758 			pic_config->index,
2759 #ifndef MV_USE_FIXED_BUF
2760 			pic_config->mv_buf_index,
2761 #else
2762 			-1,
2763 #endif
2764 			cm->buffer_pool->
2765 			frame_bufs[i].ref_count,
2766 			pic_config->vf_ref,
2767 			pic_config->decode_idx,
2768 			pic_config->slice_type,
2769 			pic_config->y_crop_width,
2770 			pic_config->y_crop_height,
2771 			pic_config->cma_alloc_addr
2772 			);
2773 	}
2774 	return;
2775 }
2776 
av1_release_buf(AV1Decoder * pbi,RefCntBuffer * const buf)2777 void av1_release_buf(AV1Decoder *pbi, RefCntBuffer *const buf)
2778 {
2779 
2780 #if 0
2781 	//def CHANGE_DONE
2782 	struct AV1HW_s *hw = (struct AV1HW_s *)(pbi->private_data);
2783 	if (!hw->mmu_enable)
2784 		return;
2785 	//release_buffer_4k(&av1_mmumgr_m, buf->buf.index);
2786 	decoder_mmu_box_free_idx(hw->mmu_box, buf->buf.index);
2787 #ifdef AOM_AV1_MMU_DW
2788 	//release_buffer_4k(&av1_mmumgr_dw, buf->buf.index);
2789 	decoder_mmu_box_free_idx(hw->mmu_box_dw, buf->buf.index);
2790 #endif
2791 
2792 #endif
2793 }
2794 
av1_release_bufs(struct AV1HW_s * hw)2795 void av1_release_bufs(struct AV1HW_s *hw)
2796 {
2797 	/*struct AV1HW_s *hw = (struct AV1HW_s *)(pbi->private_data);*/
2798 	AV1_COMMON *cm = &hw->pbi->common;
2799 	RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
2800 	int i;
2801 	if (!hw->mmu_enable)
2802 		return;
2803 
2804 	for (i = 0; i < FRAME_BUFFERS; ++i) {
2805 		if (frame_bufs[i].buf.vf_ref == 0 &&
2806 			frame_bufs[i].ref_count == 0 &&
2807 			frame_bufs[i].buf.index >= 0) {
2808 			//release_buffer_4k(&av1_mmumgr_m, i);
2809 			decoder_mmu_box_free_idx(hw->mmu_box, i);
2810 #ifdef AOM_AV1_MMU_DW
2811 			//release_buffer_4k(&av1_mmumgr_dw, i);
2812 			if (hw->dw_mmu_enable)
2813 				decoder_mmu_box_free_idx(hw->mmu_box_dw, i);
2814 #endif
2815 			av1_print(hw, AOM_DEBUG_HW_MORE, "%s, index %d\n",
2816 				__func__, i);
2817 
2818 			if (frame_bufs[i].buf.aux_data_buf)
2819 				release_aux_data(hw, &frame_bufs[i].buf);
2820 		}
2821 	}
2822 }
2823 
config_pic_size(struct AV1HW_s * hw,unsigned short bit_depth)2824 static int config_pic_size(struct AV1HW_s *hw, unsigned short bit_depth)
2825 {
2826     uint32_t data32;
2827 	struct AV1_Common_s *cm = &hw->pbi->common;
2828 	struct PIC_BUFFER_CONFIG_s *cur_pic_config = &cm->cur_frame->buf;
2829 	int losless_comp_header_size, losless_comp_body_size;
2830 #ifdef AOM_AV1_MMU_DW
2831 	int losless_comp_header_size_dw, losless_comp_body_size_dw;
2832 #endif
2833     av1_print(hw, AOM_DEBUG_HW_MORE,
2834 		"	#### config_pic_size ####, bit_depth = %d\n", bit_depth);
2835 
2836 	frame_width = cur_pic_config->y_crop_width;
2837 	frame_height = cur_pic_config->y_crop_height;
2838 	cur_pic_config->bit_depth = bit_depth;
2839 	cur_pic_config->double_write_mode = get_double_write_mode(hw);
2840 
2841 	/* use fixed maximum size   // 128x128/4/4*3-bits = 384 Bytes
2842     seg_map_size =
2843 	((frame_width + 127) >> 7) * ((frame_height + 127) >> 7) * 384 ;
2844 	*/
2845 	WRITE_VREG(HEVC_PARSER_PICTURE_SIZE,
2846 		(frame_height << 16) | frame_width);
2847 #ifdef DUAL_DECODE
2848 #else
2849 	WRITE_VREG(HEVC_ASSIST_PIC_SIZE_FB_READ,
2850 		(frame_height << 16) | frame_width);
2851 #endif
2852 #ifdef AOM_AV1_MMU
2853 
2854   //alloc_mmu(&av1_mmumgr_m, cm->cur_frame->buf.index, frame_width, frame_height,  bit_depth);
2855 #endif
2856 #ifdef AOM_AV1_MMU_DW
2857 
2858   //alloc_mmu(&av1_mmumgr_dw, cm->cur_frame->buf.index, frame_width, frame_height,  bit_depth);
2859     losless_comp_header_size_dw =
2860 		compute_losless_comp_header_size_dw(frame_width, frame_height);
2861     losless_comp_body_size_dw =
2862 		compute_losless_comp_body_size_dw(frame_width, frame_height,
2863 			(bit_depth == AOM_BITS_10));
2864 #endif
2865 
2866     losless_comp_header_size =
2867 		compute_losless_comp_header_size
2868 		(frame_width, frame_height);
2869     losless_comp_body_size =
2870 		compute_losless_comp_body_size(frame_width,
2871 		frame_height, (bit_depth == AOM_BITS_10));
2872 
2873 	cur_pic_config->comp_body_size = losless_comp_body_size;
2874 
2875 	av1_print(hw, AOM_DEBUG_HW_MORE,
2876 		"%s: width %d height %d depth %d head_size 0x%x body_size 0x%x\r\n",
2877 		__func__, frame_width, frame_height, bit_depth,
2878 		losless_comp_header_size, losless_comp_body_size);
2879 #ifdef LOSLESS_COMPRESS_MODE
2880 	data32 = READ_VREG(HEVC_SAO_CTRL5);
2881 	if (bit_depth == AOM_BITS_10)
2882 		data32 &= ~(1<<9);
2883 	else
2884 		data32 |= (1<<9);
2885 
2886 	WRITE_VREG(HEVC_SAO_CTRL5, data32);
2887 
2888 	if (hw->mmu_enable) {
2889 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1,(0x1<< 4)); // bit[4] : paged_mem_mode
2890 	} else {
2891 		if (bit_depth == AOM_BITS_10)
2892 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0<<3)); // bit[3] smem mdoe
2893 		else
2894 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (1<<3)); // bit[3] smem mdoe
2895 	}
2896 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,
2897 		(losless_comp_body_size >> 5));
2898 	/*
2899 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,
2900 	(0xff<<20) | (0xff<<10) | 0xff); //8-bit mode
2901 	*/
2902 	WRITE_VREG(HEVC_CM_BODY_LENGTH,
2903 		losless_comp_body_size);
2904 	WRITE_VREG(HEVC_CM_HEADER_OFFSET,
2905 		losless_comp_body_size);
2906 	WRITE_VREG(HEVC_CM_HEADER_LENGTH,
2907 		losless_comp_header_size);
2908 
2909 	if (get_double_write_mode(hw) & 0x10)
2910 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
2911 
2912 #else
2913 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL1,0x1 << 31);
2914 #endif
2915 #ifdef AOM_AV1_MMU_DW
2916     if (hw->dw_mmu_enable) {
2917 		WRITE_VREG(HEVC_CM_BODY_LENGTH2, losless_comp_body_size_dw);
2918 		WRITE_VREG(HEVC_CM_HEADER_OFFSET2, losless_comp_body_size_dw);
2919 		WRITE_VREG(HEVC_CM_HEADER_LENGTH2, losless_comp_header_size_dw);
2920 	}
2921 #endif
2922     return 0;
2923 
2924 }
2925 
config_mc_buffer(struct AV1HW_s * hw,unsigned short bit_depth,unsigned char inter_flag)2926 static int config_mc_buffer(struct AV1HW_s *hw, unsigned short bit_depth, unsigned char inter_flag)
2927 {
2928 	int32_t i;
2929 	AV1_COMMON *cm = &hw->pbi->common;
2930 	PIC_BUFFER_CONFIG* cur_pic_config = &cm->cur_frame->buf;
2931 	uint8_t scale_enable = 0;
2932 
2933 	av1_print(hw, AOM_DEBUG_HW_MORE,
2934 		" #### config_mc_buffer %s ####\n",
2935 		inter_flag ? "inter" : "intra");
2936 
2937 #ifdef DEBUG_PRINT
2938 	if (debug&AOM_AV1_DEBUG_BUFMGR)
2939 		av1_print(hw, AOM_DEBUG_HW_MORE,
2940 			"config_mc_buffer entered .....\n");
2941 #endif
2942 
2943 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2944 		(0 << 8) | (0<<1) | 1);
2945 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2946 		(cur_pic_config->order_hint<<24) |
2947 		(cur_pic_config->mc_canvas_u_v<<16) |
2948 		(cur_pic_config->mc_canvas_u_v<<8)|
2949 		cur_pic_config->mc_canvas_y);
2950 	for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
2951 		PIC_BUFFER_CONFIG *pic_config; //cm->frame_refs[i].buf;
2952 		if (inter_flag)
2953 			pic_config = av1_get_ref_frame_spec_buf(cm, i);
2954 		else
2955 			pic_config = cur_pic_config;
2956 		if (pic_config) {
2957 			WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2958 				(pic_config->order_hint<<24) |
2959 				(pic_config->mc_canvas_u_v<<16) |
2960 				(pic_config->mc_canvas_u_v<<8) |
2961 				pic_config->mc_canvas_y);
2962 			if (inter_flag)
2963 				av1_print(hw, AOM_DEBUG_HW_MORE,
2964 				"refid 0x%x mc_canvas_u_v 0x%x mc_canvas_y 0x%x order_hint 0x%x\n",
2965 				i, pic_config->mc_canvas_u_v,
2966 				pic_config->mc_canvas_y, pic_config->order_hint);
2967 		} else {
2968 			WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
2969 		}
2970 	}
2971 
2972 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2973 		(16 << 8) | (0 << 1) | 1);
2974 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2975 		(cur_pic_config->order_hint << 24) |
2976 		(cur_pic_config->mc_canvas_u_v << 16) |
2977 		(cur_pic_config->mc_canvas_u_v << 8) |
2978 		cur_pic_config->mc_canvas_y);
2979 	for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
2980 		PIC_BUFFER_CONFIG *pic_config;
2981 		if (inter_flag)
2982 			pic_config = av1_get_ref_frame_spec_buf(cm, i);
2983 		else
2984 			pic_config = cur_pic_config;
2985 
2986 		if (pic_config) {
2987 			WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2988 			(pic_config->order_hint << 24)|
2989 			(pic_config->mc_canvas_u_v << 16) |
2990 			(pic_config->mc_canvas_u_v << 8) |
2991 			pic_config->mc_canvas_y);
2992 		} else {
2993 			WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
2994 		}
2995 	}
2996 
2997 	WRITE_VREG(AV1D_MPP_REFINFO_TBL_ACCCONFIG,
2998 		(0x1 << 2) | (0x0 <<3)); // auto_inc start index:0 field:0
2999 	for (i = 0; i <= ALTREF_FRAME; i++) {
3000 		int32_t ref_pic_body_size;
3001 		struct scale_factors * sf = NULL;
3002 		PIC_BUFFER_CONFIG *pic_config;
3003 
3004 		if (inter_flag && i >= LAST_FRAME)
3005 			pic_config = av1_get_ref_frame_spec_buf(cm, i);
3006 		else
3007 			pic_config = cur_pic_config;
3008 
3009 		if (pic_config) {
3010 			ref_pic_body_size =
3011 				compute_losless_comp_body_size(pic_config->y_crop_width,
3012 			pic_config->y_crop_height, (bit_depth == AOM_BITS_10));
3013 
3014 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, pic_config->y_crop_width);
3015 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, pic_config->y_crop_height);
3016 			if (inter_flag && i >= LAST_FRAME) {
3017 				av1_print(hw, AOM_DEBUG_HW_MORE,
3018 				"refid %d: ref width/height(%d,%d), cur width/height(%d,%d) ref_pic_body_size 0x%x\n",
3019 				i, pic_config->y_crop_width, pic_config->y_crop_height,
3020 				cur_pic_config->y_crop_width, cur_pic_config->y_crop_height,
3021 				ref_pic_body_size);
3022 			}
3023 		} else {
3024 			ref_pic_body_size = 0;
3025 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, 0);
3026 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, 0);
3027 		}
3028 
3029 		if (inter_flag && i >= LAST_FRAME)
3030 			sf = av1_get_ref_scale_factors(cm, i);
3031 
3032 		if ((sf != NULL) && av1_is_scaled(sf)) {
3033 			scale_enable |= (1 << i);
3034 		}
3035 
3036 		if (sf) {
3037 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, sf->x_scale_fp);
3038 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, sf->y_scale_fp);
3039 
3040 			av1_print(hw, AOM_DEBUG_HW_MORE,
3041 			"x_scale_fp %d, y_scale_fp %d\n",
3042 			sf->x_scale_fp, sf->y_scale_fp);
3043 		} else {
3044 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, REF_NO_SCALE); //1<<14
3045 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, REF_NO_SCALE);
3046 		}
3047 		if (hw->mmu_enable)
3048 			WRITE_VREG(AV1D_MPP_REFINFO_DATA, 0);
3049 		else
3050 			WRITE_VREG(AV1D_MPP_REFINFO_DATA,
3051 				ref_pic_body_size >> 5);
3052 	}
3053 	WRITE_VREG(AV1D_MPP_REF_SCALE_ENBL, scale_enable);
3054 	WRITE_VREG(PARSER_REF_SCALE_ENBL, scale_enable);
3055 	av1_print(hw, AOM_DEBUG_HW_MORE,
3056 		"WRITE_VREG(PARSER_REF_SCALE_ENBL, 0x%x)\n",
3057 		scale_enable);
3058 	return 0;
3059 }
3060 
clear_mpred_hw(struct AV1HW_s * hw)3061 static void clear_mpred_hw(struct AV1HW_s *hw)
3062 {
3063 	unsigned int data32;
3064 
3065 	data32 = READ_VREG(HEVC_MPRED_CTRL4);
3066 	data32 &=  (~(1 << 6));
3067 	WRITE_VREG(HEVC_MPRED_CTRL4, data32);
3068 }
3069 
config_mpred_hw(struct AV1HW_s * hw,unsigned char inter_flag)3070 static void config_mpred_hw(struct AV1HW_s *hw, unsigned char inter_flag)
3071 {
3072 	AV1_COMMON *cm = &hw->pbi->common;
3073 	PIC_BUFFER_CONFIG *cur_pic_config = &cm->cur_frame->buf;
3074 	//PIC_BUFFER_CONFIG *last_frame_pic_config = NULL;
3075 	int i, j, pos, reg_i;
3076 	int mv_cal_tpl_count = 0;
3077 	unsigned int mv_ref_id[MFMV_STACK_SIZE] = {0, 0, 0};
3078 	unsigned ref_offset_reg[] = {
3079 		HEVC_MPRED_L0_REF06_POC,
3080 		HEVC_MPRED_L0_REF07_POC,
3081 		HEVC_MPRED_L0_REF08_POC,
3082 		HEVC_MPRED_L0_REF09_POC,
3083 		HEVC_MPRED_L0_REF10_POC,
3084 		HEVC_MPRED_L0_REF11_POC,
3085 	};
3086 	unsigned ref_buf_reg[] = {
3087 		HEVC_MPRED_L0_REF03_POC,
3088 		HEVC_MPRED_L0_REF04_POC,
3089 		HEVC_MPRED_L0_REF05_POC
3090 	};
3091 	unsigned ref_offset_val[6] =
3092 		{0, 0, 0, 0, 0, 0};
3093 	unsigned ref_buf_val[3] = {0, 0, 0};
3094 
3095 	uint32_t data32;
3096 	int32_t  mpred_curr_lcu_x;
3097 	int32_t  mpred_curr_lcu_y;
3098 	//int32_t     mpred_mv_rd_end_addr;
3099 
3100 	av1_print(hw, AOM_DEBUG_HW_MORE,
3101 	" #### config_mpred_hw ####\n");
3102 
3103 	/*if (cm->prev_frame)
3104 	last_frame_pic_config = &cm->prev_frame->buf;
3105 	mpred_mv_rd_end_addr = last_frame_pic_config->mpred_mv_wr_start_addr
3106 	+ (last_frame_pic_config->lcu_total * MV_MEM_UNIT);
3107 	*/
3108 
3109 	data32 = READ_VREG(HEVC_MPRED_CURR_LCU);
3110 	mpred_curr_lcu_x   =data32 & 0xffff;
3111 	mpred_curr_lcu_y   =(data32>>16) & 0xffff;
3112 
3113 	av1_print(hw, AOM_DEBUG_HW_MORE,
3114 	"cur pic index %d\n", cur_pic_config->index);
3115 	/*printk("cur pic index %d  col pic index %d\n",
3116 	cur_pic_config->index, last_frame_pic_config->index);*/
3117 
3118 	//WRITE_VREG(HEVC_MPRED_CTRL3,0x24122412);
3119 	WRITE_VREG(HEVC_MPRED_CTRL3, 0x13151315); // 'd19, 'd21 for AV1
3120 	WRITE_VREG(HEVC_MPRED_ABV_START_ADDR,
3121 	hw->pbi->work_space_buf->mpred_above.buf_start);
3122 
3123 #if 0
3124 	data32 = READ_VREG(HEVC_MPRED_CTRL4);
3125 	data32 &=  (~(1<<6));
3126 	data32 |= (cm->use_prev_frame_mvs << 6);
3127 	WRITE_VREG(HEVC_MPRED_CTRL4, data32);
3128 #endif
3129 	if (inter_flag) {
3130 		/* config sign_bias */
3131 		//data32 = (cm->cur_frame_force_integer_mv & 0x1) << 9;
3132 		data32 = READ_VREG(HEVC_MPRED_CTRL4);
3133 		data32 &= (~(0xff << 12));
3134 		//for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
3135 		/* HEVC_MPRED_CTRL4[bit 12] is for cm->ref_frame_sign_bias[0]
3136 		instead of cm->ref_frame_sign_bias[LAST_FRAME] */
3137 		for (i = 0; i <= ALTREF_FRAME; i++) {
3138 			data32 |= ((cm->ref_frame_sign_bias[i] & 0x1) << (12 + i));
3139 		}
3140 		WRITE_VREG(HEVC_MPRED_CTRL4, data32);
3141 		av1_print(hw, AOM_DEBUG_HW_MORE,
3142 			"WRITE_VREG(HEVC_MPRED_CTRL4, 0x%x)\n", data32);
3143 	}
3144 #if 1
3145 	data32 = (
3146 		(cm->seq_params.order_hint_info.enable_order_hint << 27) |
3147 		(cm->seq_params.order_hint_info.order_hint_bits_minus_1 << 24) |
3148 		(cm->cur_frame->order_hint <<16 ) |
3149 		(0x13 << 8) | (0x13 << 0));
3150 #else
3151 	data32 = READ_VREG(HEVC_MPRED_L0_REF00_POC);
3152 	data32 &= (~(0xff << 16));
3153 	data32 |= (cm->cur_frame->order_hint & 0xff);
3154 	data32 &= (~(1 << 27));
3155 	data32 |= (cm->seq_params.order_hint_info.enable_order_hint << 27);
3156 #endif
3157 	WRITE_VREG(HEVC_MPRED_L0_REF00_POC, data32);
3158 	av1_print(hw, AOM_DEBUG_HW_MORE,
3159 	"WRITE_VREG(HEVC_MPRED_L0_REF00_POC, 0x%x)\n", data32);
3160 
3161 	if (inter_flag) {
3162 		/* config ref_buf id and order hint */
3163 		data32 = 0;
3164 		pos = 25;
3165 		reg_i = 0;
3166 		for (i = ALTREF_FRAME; i >= LAST_FRAME; i--) {
3167 				PIC_BUFFER_CONFIG *pic_config =
3168 					av1_get_ref_frame_spec_buf(cm, i);
3169 			if (pic_config) {
3170 				av1_print(hw, AOM_DEBUG_HW_MORE,
3171 					"pic_config for %d th ref: index %d, reg[%d] pos %d\n",
3172 					i, pic_config->index, reg_i, pos);
3173 				data32 |= ((pic_config->index < 0)? 0 : pic_config->index) << pos;
3174 			} else
3175 				av1_print(hw, AOM_DEBUG_HW_MORE,
3176 				"pic_config is null for %d th ref\n", i);
3177 			if (pos == 0) {
3178 				//WRITE_VREG(ref_buf_reg[reg_i], data32);
3179 				ref_buf_val[reg_i] = data32;
3180 				av1_print(hw, AOM_DEBUG_HW_MORE,
3181 				"ref_buf_reg[%d], WRITE_VREG(0x%x, 0x%x)\n",
3182 				reg_i, ref_buf_reg[reg_i], data32);
3183 				reg_i++;
3184 				data32 = 0;
3185 				pos = 24; //for P_HEVC_MPRED_L0_REF04_POC
3186 			} else {
3187 				if (pos == 24)
3188 				pos -= 8; //for P_HEVC_MPRED_L0_REF04_POC
3189 				else
3190 				pos -= 5; //for P_HEVC_MPRED_L0_REF03_POC
3191 			}
3192 		}
3193 		for (i = ALTREF_FRAME; i >= LAST_FRAME; i--) {
3194 			PIC_BUFFER_CONFIG *pic_config =
3195 				av1_get_ref_frame_spec_buf(cm, i);
3196 			if (pic_config) {
3197 				av1_print(hw, AOM_DEBUG_HW_MORE,
3198 					"pic_config for %d th ref: order_hint %d, reg[%d] pos %d\n",
3199 					i, pic_config->order_hint, reg_i, pos);
3200 				data32 |= ((pic_config->index < 0)? 0 : pic_config->order_hint) << pos;
3201 			} else
3202 				av1_print(hw, AOM_DEBUG_HW_MORE,
3203 				"pic_config is null for %d th ref\n", i);
3204 			if (pos == 0) {
3205 				//WRITE_VREG(ref_buf_reg[reg_i], data32);
3206 				ref_buf_val[reg_i] = data32;
3207 				av1_print(hw, AOM_DEBUG_HW_MORE,
3208 				"ref_buf_reg[%d], WRITE_VREG(0x%x, 0x%x)\n",
3209 				reg_i, ref_buf_reg[reg_i], data32);
3210 				reg_i++;
3211 				data32 = 0;
3212 				pos = 24;
3213 			} else
3214 				pos -= 8;
3215 		}
3216 		if (pos != 24) {
3217 			//WRITE_VREG(ref_buf_reg[reg_i], data32);
3218 			ref_buf_val[reg_i] = data32;
3219 			av1_print(hw, AOM_DEBUG_HW_MORE,
3220 				"ref_buf_reg[%d], WRITE_VREG(0x%x, 0x%x)\n",
3221 			reg_i, ref_buf_reg[reg_i], data32);
3222 		}
3223 		/* config ref_offset */
3224 		data32 = 0;
3225 		pos = 24;
3226 		mv_cal_tpl_count = 0;
3227 		reg_i = 0;
3228 		for (i = 0; i < cm->mv_ref_id_index; i++) {
3229 			if (cm->mv_cal_tpl_mvs[i]) {
3230 				mv_ref_id[mv_cal_tpl_count] = cm->mv_ref_id[i];
3231 				mv_cal_tpl_count++;
3232 				for (j = LAST_FRAME; j <= ALTREF_FRAME; j++) {
3233 					/*offset can be negative*/
3234 					unsigned char offval =
3235 						cm->mv_ref_offset[i][j] & 0xff;
3236 					data32 |= (offval << pos);
3237 					if (pos == 0) {
3238 						//WRITE_VREG(ref_offset_reg[reg_i], data32);
3239 						ref_offset_val[reg_i] = data32;
3240 						av1_print(hw, AOM_DEBUG_HW_MORE,
3241 						"ref_offset_reg[%d], WRITE_VREG(0x%x, 0x%x)\n",
3242 						reg_i, ref_offset_reg[reg_i], data32);
3243 						reg_i++;
3244 						data32 = 0;
3245 						pos = 24;
3246 					} else
3247 						pos -= 8;
3248 				}
3249 			}
3250 		}
3251 		if (pos != 24) {
3252 		//WRITE_VREG(ref_offset_reg[reg_i], data32);
3253 		ref_offset_val[reg_i] = data32;
3254 		av1_print(hw, AOM_DEBUG_HW_MORE,
3255 		"ref_offset_reg[%d], WRITE_VREG(0x%x, 0x%x)\n",
3256 		reg_i, ref_offset_reg[reg_i], data32);
3257 		}
3258 
3259 		data32 = ref_offset_val[5] | //READ_VREG(HEVC_MPRED_L0_REF11_POC) |
3260 			mv_cal_tpl_count | (mv_ref_id[0] << 2) |
3261 			(mv_ref_id[1] << 5) | (mv_ref_id[2] << 8);
3262 		ref_offset_val[5] = data32;
3263 		//WRITE_VREG(HEVC_MPRED_L0_REF11_POC, data32);
3264 		av1_print(hw, AOM_DEBUG_HW_MORE,
3265 			"WRITE_VREG(HEVC_MPRED_L0_REF11_POC 0x%x, 0x%x)\n",
3266 			HEVC_MPRED_L0_REF11_POC, data32);
3267 	}
3268 	for (i = 0; i < 3; i++)
3269 		WRITE_VREG(ref_buf_reg[i], ref_buf_val[i]);
3270 	for (i = 0; i < 6; i++)
3271 		WRITE_VREG(ref_offset_reg[i], ref_offset_val[i]);
3272 
3273 	WRITE_VREG(HEVC_MPRED_MV_WR_START_ADDR,
3274 		cur_pic_config->mpred_mv_wr_start_addr);
3275 	WRITE_VREG(HEVC_MPRED_MV_WPTR,
3276 		cur_pic_config->mpred_mv_wr_start_addr);
3277 
3278 	if (inter_flag) {
3279 		for (i = 0; i < mv_cal_tpl_count; i++) {
3280 			PIC_BUFFER_CONFIG *pic_config =
3281 			av1_get_ref_frame_spec_buf(cm, mv_ref_id[i]);
3282 			if (pic_config == NULL)
3283 				continue;
3284 			if (i == 0) {
3285 				WRITE_VREG(HEVC_MPRED_MV_RD_START_ADDR,
3286 					pic_config->mpred_mv_wr_start_addr);
3287 				WRITE_VREG(HEVC_MPRED_MV_RPTR,
3288 					pic_config->mpred_mv_wr_start_addr);
3289 			} else if (i == 1) {
3290 				WRITE_VREG(HEVC_MPRED_L0_REF01_POC,
3291 					pic_config->mpred_mv_wr_start_addr);
3292 				WRITE_VREG(HEVC_MPRED_MV_RPTR_1,
3293 					pic_config->mpred_mv_wr_start_addr);
3294 			} else if (i == 2) {
3295 				WRITE_VREG(HEVC_MPRED_L0_REF02_POC,
3296 					pic_config->mpred_mv_wr_start_addr);
3297 				WRITE_VREG(HEVC_MPRED_MV_RPTR_2,
3298 					pic_config->mpred_mv_wr_start_addr);
3299 			} else {
3300 				av1_print(hw, AOM_DEBUG_HW_MORE,
3301 					"%s: mv_ref_id error\n", __func__);
3302 			}
3303 		}
3304 	}
3305 	data32 = READ_VREG(HEVC_MPRED_CTRL0);
3306 	data32 &= ~((1 << 10) | (1 << 11));
3307 	data32 |= (1 << 10); /*write enable*/
3308 	av1_print(hw, AOM_DEBUG_HW_MORE,
3309 		"current_frame.frame_type=%d, cur_frame->frame_type=%d, allow_ref_frame_mvs=%d\n",
3310 		cm->current_frame.frame_type, cm->cur_frame->frame_type,
3311 		cm->allow_ref_frame_mvs);
3312 
3313 	if (av1_frame_is_inter(&hw->pbi->common)) {
3314 		if (cm->allow_ref_frame_mvs) {
3315 			data32 |= (1 << 11); /*read enable*/
3316 		}
3317 	}
3318 	av1_print(hw, AOM_DEBUG_HW_MORE,
3319 		"WRITE_VREG(HEVC_MPRED_CTRL0 0x%x, 0x%x)\n",
3320 		HEVC_MPRED_CTRL0, data32);
3321 	WRITE_VREG(HEVC_MPRED_CTRL0, data32);
3322 	/*
3323 	printk("config_mpred: (%x) wr_start_addr  %x from indx %d;
3324 		(%x) rd_start_addr %x from index %d\n",
3325 		cur_pic_config,  cur_pic_config->mpred_mv_wr_start_addr,  cur_pic_config->index,
3326 	last_frame_pic_config,  last_frame_pic_config->mpred_mv_wr_start_addr,  last_frame_pic_config->index);
3327 	data32 = ((pbi->lcu_x_num - pbi->tile_width_lcu)*MV_MEM_UNIT);
3328 	WRITE_VREG(HEVC_MPRED_MV_WR_ROW_JUMP,data32);
3329 	WRITE_VREG(HEVC_MPRED_MV_RD_ROW_JUMP,data32);
3330 
3331 	WRITE_VREG(HEVC_MPRED_MV_RD_END_ADDR, mpred_mv_rd_end_addr);
3332 	*/
3333 }
3334 
config_sao_hw(struct AV1HW_s * hw,union param_u * params)3335 static void config_sao_hw(struct AV1HW_s *hw, union param_u *params)
3336 {
3337 	/*
3338 	!!!!!!!!!!!!!!!!!!!!!!!!!TODO .... !!!!!!!!!!!
3339 	mem_map_mode, endian, get_double_write_mode
3340 	*/
3341     AV1_COMMON *cm = &hw->pbi->common;
3342     PIC_BUFFER_CONFIG* pic_config = &cm->cur_frame->buf;
3343     uint32_t data32;
3344     int32_t lcu_size =
3345 		((params->p.seq_flags >> 6) & 0x1) ? 128 : 64;
3346     int32_t mc_buffer_size_u_v =
3347 		pic_config->lcu_total*lcu_size*lcu_size/2;
3348     int32_t mc_buffer_size_u_v_h =
3349 		(mc_buffer_size_u_v + 0xffff)>>16; //64k alignment
3350     av1_print(hw, AOM_DEBUG_HW_MORE,
3351 		"[test.c] #### config_sao_hw ####, lcu_size %d\n", lcu_size);
3352     av1_print(hw, AOM_DEBUG_HW_MORE,
3353 		"[config_sao_hw] lcu_total : %d\n", pic_config->lcu_total);
3354     av1_print(hw, AOM_DEBUG_HW_MORE,
3355 		"[config_sao_hw] mc_y_adr : 0x%x\n", pic_config->mc_y_adr);
3356     av1_print(hw, AOM_DEBUG_HW_MORE,
3357 		"[config_sao_hw] mc_u_v_adr : 0x%x\n", pic_config->mc_u_v_adr);
3358     av1_print(hw, AOM_DEBUG_HW_MORE,
3359 		"[config_sao_hw] header_adr : 0x%x\n", pic_config->header_adr);
3360 #ifdef AOM_AV1_MMU_DW
3361     if (hw->dw_mmu_enable)
3362 		av1_print(hw, AOM_DEBUG_HW_MORE,
3363 		"[config_sao_hw] header_dw_adr : 0x%x\n", pic_config->header_dw_adr);
3364 #endif
3365     data32 = READ_VREG(HEVC_SAO_CTRL9) | (1 << 1);
3366     WRITE_VREG(HEVC_SAO_CTRL9, data32);
3367 
3368     data32 = READ_VREG(HEVC_SAO_CTRL5);
3369     data32 |= (0x1 << 14); /* av1 mode */
3370     data32 |= (0xff << 16); /* dw {v1,v0,h1,h0} ctrl_y_cbus */
3371     WRITE_VREG(HEVC_SAO_CTRL5, data32);
3372 
3373     WRITE_VREG(HEVC_SAO_CTRL0,
3374 		lcu_size == 128 ? 0x7 : 0x6); /*lcu_size_log2*/
3375 #ifdef LOSLESS_COMPRESS_MODE
3376     WRITE_VREG(HEVC_CM_BODY_START_ADDR, pic_config->mc_y_adr);
3377 #ifdef AOM_AV1_MMU
3378     WRITE_VREG(HEVC_CM_HEADER_START_ADDR, pic_config->header_adr);
3379 #endif
3380 #ifdef AOM_AV1_MMU_DW
3381     if (hw->dw_mmu_enable)
3382 		WRITE_VREG(HEVC_CM_HEADER_START_ADDR2, pic_config->header_dw_adr);
3383 #endif
3384 #else
3385 /*!LOSLESS_COMPRESS_MODE*/
3386     WRITE_VREG(HEVC_SAO_Y_START_ADDR, pic_config->mc_y_adr);
3387 #endif
3388 
3389     av1_print(hw, AOM_DEBUG_HW_MORE,
3390 		"[config_sao_hw] sao_body_addr:%x\n", pic_config->mc_y_adr);
3391 	//printk("[config_sao_hw] sao_header_addr:%x\n", pic_config->mc_y_adr + losless_comp_body_size );
3392 
3393 #ifdef VPU_FILMGRAIN_DUMP
3394 	// Let Microcode to increase
3395 	// WRITE_VREG(HEVC_FGS_TABLE_START, pic_config->fgs_table_adr);
3396 #else
3397     WRITE_VREG(HEVC_FGS_TABLE_START, pic_config->fgs_table_adr);
3398 #endif
3399     WRITE_VREG(HEVC_FGS_TABLE_LENGTH, FGS_TABLE_SIZE * 8);
3400     av1_print(hw, AOM_DEBUG_HW_MORE,
3401 		"[config_sao_hw] fgs_table adr:0x%x , length 0x%x bits\n",
3402 		pic_config->fgs_table_adr, FGS_TABLE_SIZE * 8);
3403 
3404     data32 = (mc_buffer_size_u_v_h<<16)<<1;
3405 	//printk("data32 = %x, mc_buffer_size_u_v_h = %x, lcu_total = %x\n", data32, mc_buffer_size_u_v_h, pic_config->lcu_total);
3406     WRITE_VREG(HEVC_SAO_Y_LENGTH ,data32);
3407 
3408 #ifndef LOSLESS_COMPRESS_MODE
3409     WRITE_VREG(HEVC_SAO_C_START_ADDR, pic_config->mc_u_v_adr);
3410 #else
3411 #endif
3412 
3413     data32 = (mc_buffer_size_u_v_h<<16);
3414     WRITE_VREG(HEVC_SAO_C_LENGTH  ,data32);
3415 
3416 #ifndef LOSLESS_COMPRESS_MODE
3417 	/* multi tile to do... */
3418     WRITE_VREG(HEVC_SAO_Y_WPTR, pic_config->mc_y_adr);
3419 
3420     WRITE_VREG(HEVC_SAO_C_WPTR, pic_config->mc_u_v_adr);
3421 #else
3422     if (get_double_write_mode(hw) &&
3423 		(get_double_write_mode(hw) & 0x20) == 0) {
3424 	    WRITE_VREG(HEVC_SAO_Y_START_ADDR, pic_config->dw_y_adr);
3425 	    WRITE_VREG(HEVC_SAO_C_START_ADDR, pic_config->dw_u_v_adr);
3426 	    WRITE_VREG(HEVC_SAO_Y_WPTR, pic_config->dw_y_adr);
3427 	    WRITE_VREG(HEVC_SAO_C_WPTR, pic_config->dw_u_v_adr);
3428 	} else {
3429 		//WRITE_VREG(HEVC_SAO_Y_START_ADDR, 0xffffffff);
3430 		//WRITE_VREG(HEVC_SAO_C_START_ADDR, 0xffffffff);
3431 	}
3432 #endif
3433 
3434 
3435 #ifndef AOM_AV1_NV21
3436 #ifdef AOM_AV1_MMU_DW
3437     if (hw->dw_mmu_enable) {
3438 	}
3439 #endif
3440 #endif
3441 
3442 #ifdef AOM_AV1_NV21
3443 #ifdef DOS_PROJECT
3444     data32 = READ_VREG(HEVC_SAO_CTRL1);
3445     data32 &= (~0x3000);
3446     data32 |= (MEM_MAP_MODE << 12); // [13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32
3447     data32 &= (~0x3);
3448     data32 |= 0x1; // [1]:dw_disable [0]:cm_disable
3449     WRITE_VREG(HEVC_SAO_CTRL1, data32);
3450 
3451     data32 = READ_VREG(HEVC_SAO_CTRL5); // [23:22] dw_v1_ctrl [21:20] dw_v0_ctrl [19:18] dw_h1_ctrl [17:16] dw_h0_ctrl
3452     data32 &= ~(0xff << 16);			   // set them all 0 for AOM_AV1_NV21 (no down-scale)
3453     WRITE_VREG(HEVC_SAO_CTRL5, data32);
3454 
3455     data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3456     data32 &= (~0x30);
3457     data32 |= (MEM_MAP_MODE << 4); // [5:4]	-- address_format 00:linear 01:32x32 10:64x32
3458     WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3459 #else
3460 // m8baby test1902
3461    data32 = READ_VREG(HEVC_SAO_CTRL1);
3462 	data32 &= (~0x3000);
3463 	data32 |= (MEM_MAP_MODE << 12); // [13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32
3464     data32 &= (~0xff0);
3465 	//data32 |= 0x670;  // Big-Endian per 64-bit
3466     data32 |= 0x880;  // Big-Endian per 64-bit
3467     data32 &= (~0x3);
3468     data32 |= 0x1; // [1]:dw_disable [0]:cm_disable
3469     WRITE_VREG(HEVC_SAO_CTRL1, data32);
3470 
3471     data32 = READ_VREG(HEVC_SAO_CTRL5); // [23:22] dw_v1_ctrl [21:20] dw_v0_ctrl [19:18] dw_h1_ctrl [17:16] dw_h0_ctrl
3472     data32 &= ~(0xff << 16);			   // set them all 0 for AOM_AV1_NV21 (no down-scale)
3473     WRITE_VREG(HEVC_SAO_CTRL5, data32);
3474 
3475     data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3476 	data32 &= (~0x30);
3477 	data32 |= (MEM_MAP_MODE << 4); // [5:4]	-- address_format 00:linear 01:32x32 10:64x32
3478 	data32 &= (~0xF);
3479     data32 |= 0x8;	// Big-Endian per 64-bit
3480     WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3481 #endif
3482 #else
3483 /*CHANGE_DONE nnn*/
3484 	data32 = READ_VREG(HEVC_SAO_CTRL1);
3485 	data32 &= (~0x3000);
3486 	data32 |= (mem_map_mode <<
3487 			   12);
3488 
3489 /*  [13:12] axi_aformat, 0-Linear,
3490  *				   1-32x32, 2-64x32
3491  */
3492 	data32 &= (~0xff0);
3493 	/* data32 |= 0x670;  // Big-Endian per 64-bit */
3494 #ifdef AOM_AV1_MMU_DW
3495 	if (hw->dw_mmu_enable == 0)
3496 		data32 |= endian;	/* Big-Endian per 64-bit */
3497 #else
3498 	data32 |= endian;	/* Big-Endian per 64-bit */
3499 #endif
3500 	data32 &= (~0x3); /*[1]:dw_disable [0]:cm_disable*/
3501 	if (get_double_write_mode(hw) == 0)
3502 		data32 |= 0x2; /*disable double write*/
3503 	else if (get_double_write_mode(hw) & 0x10)
3504 		data32 |= 0x1; /*disable cm*/
3505 	 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) { /* >= G12A dw write control */
3506 		unsigned int data;
3507 		data = READ_VREG(HEVC_DBLK_CFGB);
3508 		data &= (~0x300); /*[8]:first write enable (compress)  [9]:double write enable (uncompress)*/
3509 		if (get_double_write_mode(hw) == 0)
3510 			data |= (0x1 << 8); /*enable first write*/
3511 		else if (get_double_write_mode(hw) & 0x10)
3512 			data |= (0x1 << 9); /*double write only*/
3513 		else
3514 			data |= ((0x1 << 8)  |(0x1 << 9));
3515 		WRITE_VREG(HEVC_DBLK_CFGB, data);
3516 	}
3517 
3518 	WRITE_VREG(HEVC_SAO_CTRL1, data32);
3519 
3520 	if (get_double_write_mode(hw) & 0x10) {
3521 		/* [23:22] dw_v1_ctrl
3522 		 *[21:20] dw_v0_ctrl
3523 		 *[19:18] dw_h1_ctrl
3524 		 *[17:16] dw_h0_ctrl
3525 		 */
3526 		data32 = READ_VREG(HEVC_SAO_CTRL5);
3527 		/*set them all 0 for H265_NV21 (no down-scale)*/
3528 		data32 &= ~(0xff << 16);
3529 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
3530 	} else {
3531 		data32 = READ_VREG(HEVC_SAO_CTRL5);
3532 		data32 &= (~(0xff << 16));
3533 		if ((get_double_write_mode(hw) & 0xf) == 2 ||
3534 			(get_double_write_mode(hw) & 0xf) == 3)
3535 			data32 |= (0xff<<16);
3536 		else if ((get_double_write_mode(hw) & 0xf) == 4 ||
3537 			(get_double_write_mode(hw) & 0xf) == 5)
3538 			data32 |= (0x33<<16);
3539 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
3540 	}
3541 
3542 	data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3543 	data32 &= (~0x30);
3544 	/* [5:4]	-- address_format 00:linear 01:32x32 10:64x32 */
3545 	data32 |= (mem_map_mode <<
3546 			   4);
3547 	data32 &= (~0xF);
3548 	data32 |= 0xf;  /* valid only when double write only */
3549 		/*data32 |= 0x8;*/		/* Big-Endian per 64-bit */
3550 	WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3551 
3552 #endif
3553 
3554 }
3555 
3556 
3557 #ifdef AOM_AV1_DBLK_INIT
3558 /*
3559  * Defines, declarations, sub-functions for av1 de-block loop filter Thr/Lvl table update
3560  * - struct segmentation_lf is for loop filter only (removed something)
3561  * - function "av1_loop_filter_init" and "av1_loop_filter_frame_init" will be instantiated in C_Entry
3562  * - av1_loop_filter_init run once before decoding start
3563  * - av1_loop_filter_frame_init run before every frame decoding start
3564  * - set video format to AOM_AV1 is in av1_loop_filter_init
3565  */
3566 #define MAX_LOOP_FILTER 63
3567 #define MAX_MODE_LF_DELTAS 2
3568 #define MAX_SEGMENTS 8
3569 #define MAX_MB_PLANE 3
3570 
3571 typedef enum {
3572   SEG_LVL_ALT_Q,	   // Use alternate Quantizer ....
3573   SEG_LVL_ALT_LF_Y_V,  // Use alternate loop filter value on y plane vertical
3574   SEG_LVL_ALT_LF_Y_H,  // Use alternate loop filter value on y plane horizontal
3575   SEG_LVL_ALT_LF_U,	// Use alternate loop filter value on u plane
3576   SEG_LVL_ALT_LF_V,	// Use alternate loop filter value on v plane
3577   SEG_LVL_REF_FRAME,   // Optional Segment reference frame
3578   SEG_LVL_SKIP,		// Optional Segment (0,0) + skip mode
3579   SEG_LVL_GLOBALMV,
3580   SEG_LVL_MAX
3581 } SEG_LVL_FEATURES;
3582 
3583 static const SEG_LVL_FEATURES seg_lvl_lf_lut[MAX_MB_PLANE][2] = {
3584   { SEG_LVL_ALT_LF_Y_V, SEG_LVL_ALT_LF_Y_H },
3585   { SEG_LVL_ALT_LF_U, SEG_LVL_ALT_LF_U },
3586   { SEG_LVL_ALT_LF_V, SEG_LVL_ALT_LF_V }
3587 };
3588 
3589 struct segmentation_lf { // for loopfilter only
3590   uint8_t enabled;
3591 		  /*
3592 		   SEG_LVL_ALT_LF_Y_V feature_enable: seg_lf_info_y[bit7]
3593 		   SEG_LVL_ALT_LF_Y_V data: seg_lf_info_y[bit0~6]
3594 		   SEG_LVL_ALT_LF_Y_H feature enable: seg_lf_info_y[bit15]
3595 		   SEG_LVL_ALT_LF_Y_H data: seg_lf_info_y[bit8~14]
3596 		   */
3597   uint16_t seg_lf_info_y[8];
3598 		  /*
3599 		   SEG_LVL_ALT_LF_U feature_enable: seg_lf_info_c[bit7]
3600 		   SEG_LVL_ALT_LF_U data: seg_lf_info_c[bit0~6]
3601 		   SEG_LVL_ALT_LF_V feature enable: seg_lf_info_c[bit15]
3602 		   SEG_LVL_ALT_LF_V data: seg_lf_info_c[bit8~14]
3603 		   */
3604   uint16_t seg_lf_info_c[8];
3605 };
3606 
3607 typedef struct {
3608   uint8_t mblim;
3609   uint8_t lim;
3610   uint8_t hev_thr;
3611 } loop_filter_thresh;
3612 
3613 typedef struct loop_filter_info_n_s {
3614   loop_filter_thresh lfthr[MAX_LOOP_FILTER + 1];
3615   uint8_t lvl[MAX_MB_PLANE][MAX_SEGMENTS][2][REF_FRAMES][MAX_MODE_LF_DELTAS];
3616 } loop_filter_info_n;
3617 
3618 struct loopfilter {
3619   int32_t filter_level[2];
3620   int32_t filter_level_u;
3621   int32_t filter_level_v;
3622 
3623   int32_t sharpness_level;
3624 
3625   uint8_t mode_ref_delta_enabled;
3626   uint8_t mode_ref_delta_update;
3627 
3628   // 0 = Intra, Last, Last2+Last3,
3629   // GF, BRF, ARF2, ARF
3630   int8_t ref_deltas[REF_FRAMES];
3631 
3632   // 0 = ZERO_MV, MV
3633   int8_t mode_deltas[MAX_MODE_LF_DELTAS];
3634 
3635   int32_t combine_vert_horz_lf;
3636 
3637   int32_t lf_pic_cnt;
3638 
3639 //#if LOOP_FILTER_BITMASK
3640   //LoopFilterMask *lfm;
3641   //size_t lfm_num;
3642   //int lfm_stride;
3643   //LpfSuperblockInfo neighbor_sb_lpf_info;
3644 //#endif  // LOOP_FILTER_BITMASK
3645 };
3646 
myclamp(int32_t value,int32_t low,int32_t high)3647 static int32_t myclamp(int32_t value, int32_t low, int32_t high) {
3648   return value < low ? low : (value > high ? high : value);
3649 }
3650 
3651 /*static int8_t extend_sign_7bits(uint8_t value) {
3652   return (((value>>6) & 0x1)<<7) | (value&0x7f);
3653 }*/
3654 
3655 // convert data to int8_t variable
3656 // value : signed data (with any bitwidth<8) which is assigned to uint8_t variable as an input
3657 // bw    : bitwidth of signed data, (from 1 to 7)
conv2int8(uint8_t value,uint8_t bw)3658 static int8_t conv2int8 (uint8_t value, uint8_t bw) {
3659     if (bw<1 || bw>7) return (int8_t)value;
3660     else {
3661 	    const uint8_t data_bits = value & ((1<<bw)-1);
3662 	    const uint8_t sign_bit = (value>>(bw-1)) & 0x1;
3663 	    const uint8_t sign_bit_ext = sign_bit | sign_bit<<1 | sign_bit<<2 | sign_bit<<3 | sign_bit<<4 | sign_bit<<5 | sign_bit<<6 | sign_bit<<7;
3664 	    return (int8_t)((sign_bit_ext<<bw) | data_bits);
3665 	}
3666 }
3667 
av1_update_sharpness(loop_filter_info_n * lfi,int32_t sharpness_lvl)3668 static void av1_update_sharpness(loop_filter_info_n *lfi, int32_t sharpness_lvl) {
3669   int32_t lvl;
3670 
3671   // For each possible value for the loop filter fill out limits
3672   for (lvl = 0; lvl <= MAX_LOOP_FILTER; lvl++) {
3673 	// Set loop filter parameters that control sharpness.
3674     int32_t block_inside_limit =
3675 		lvl >> ((sharpness_lvl > 0) + (sharpness_lvl > 4));
3676 
3677     if (sharpness_lvl > 0) {
3678 	  if (block_inside_limit > (9 - sharpness_lvl))
3679 	    block_inside_limit = (9 - sharpness_lvl);
3680 	}
3681 
3682     if (block_inside_limit < 1)
3683 	  block_inside_limit = 1;
3684 
3685     lfi->lfthr[lvl].lim = (uint8_t)block_inside_limit;
3686     lfi->lfthr[lvl].mblim = (uint8_t)(2 * (lvl + 2) + block_inside_limit);
3687   }
3688 }
3689 
3690 // instantiate this function once when decode is started
av1_loop_filter_init(loop_filter_info_n * lfi,struct loopfilter * lf)3691 void av1_loop_filter_init(loop_filter_info_n *lfi, struct loopfilter *lf) {
3692 	int32_t i;
3693 	uint32_t data32;
3694 
3695 	// init limits for given sharpness
3696 	av1_update_sharpness(lfi, lf->sharpness_level);
3697 
3698 	// Write to register
3699 	for (i = 0; i < 32; i++) {
3700 		uint32_t thr;
3701 		thr = ((lfi->lfthr[i*2+1].lim & 0x3f)<<8) |
3702 			(lfi->lfthr[i*2+1].mblim & 0xff);
3703 		thr = (thr<<16) | ((lfi->lfthr[i*2].lim & 0x3f)<<8) |
3704 			(lfi->lfthr[i*2].mblim & 0xff);
3705 		WRITE_VREG(HEVC_DBLK_CFG9, thr);
3706 	}
3707 	// video format is AOM_AV1
3708 	data32 = (0x57 << 8) |  // 1st/2nd write both enable
3709 		(0x4  << 0);   // aom_av1 video format
3710 	WRITE_VREG(HEVC_DBLK_CFGB, data32);
3711 	av1_print2(AOM_DEBUG_HW_MORE,
3712 		"[DBLK DEBUG] CFGB : 0x%x\n", data32);
3713 }
3714 
3715 // perform this function per frame
av1_loop_filter_frame_init(AV1Decoder * pbi,struct segmentation_lf * seg,loop_filter_info_n * lfi,struct loopfilter * lf,int32_t pic_width)3716 void av1_loop_filter_frame_init(AV1Decoder* pbi, struct segmentation_lf *seg,
3717 	loop_filter_info_n *lfi,
3718 	struct loopfilter *lf,
3719 	int32_t pic_width) {
3720 	BuffInfo_t* buf_spec = pbi->work_space_buf;
3721 	int32_t i,dir;
3722 	int32_t filt_lvl[MAX_MB_PLANE], filt_lvl_r[MAX_MB_PLANE];
3723 	int32_t plane;
3724 	int32_t seg_id;
3725 	// n_shift is the multiplier for lf_deltas
3726 	// the multiplier is 1 for when filter_lvl is between 0 and 31;
3727 	// 2 when filter_lvl is between 32 and 63
3728 
3729 	// update limits if sharpness has changed
3730 	av1_update_sharpness(lfi, lf->sharpness_level);
3731 
3732 	// Write to register
3733 	for (i = 0; i < 32; i++) {
3734 		uint32_t thr;
3735 		thr = ((lfi->lfthr[i*2+1].lim & 0x3f)<<8)
3736 			| (lfi->lfthr[i*2+1].mblim & 0xff);
3737 		thr = (thr<<16) | ((lfi->lfthr[i*2].lim & 0x3f)<<8)
3738 			| (lfi->lfthr[i*2].mblim & 0xff);
3739 		WRITE_VREG(HEVC_DBLK_CFG9, thr);
3740 	}
3741 
3742 	filt_lvl[0] = lf->filter_level[0];
3743 	filt_lvl[1] = lf->filter_level_u;
3744 	filt_lvl[2] = lf->filter_level_v;
3745 
3746 	filt_lvl_r[0] = lf->filter_level[1];
3747 	filt_lvl_r[1] = lf->filter_level_u;
3748 	filt_lvl_r[2] = lf->filter_level_v;
3749 
3750 #ifdef DBG_LPF_PRINT
3751 	printk("LF_PRINT: pic_cnt(%d) base_filter_level(%d,%d,%d,%d)\n",
3752 		lf->lf_pic_cnt, lf->filter_level[0],
3753 		lf->filter_level[1], lf->filter_level_u, lf->filter_level_v);
3754 #endif
3755 
3756 	for (plane = 0; plane < 3; plane++) {
3757 		if (plane == 0 && !filt_lvl[0] && !filt_lvl_r[0])
3758 		  break;
3759 		else if (plane == 1 && !filt_lvl[1])
3760 		  continue;
3761 		else if (plane == 2 && !filt_lvl[2])
3762 		  continue;
3763 
3764 		for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) { // MAX_SEGMENTS==8
3765 			for (dir = 0; dir < 2; ++dir) {
3766 				int32_t lvl_seg = (dir == 0) ? filt_lvl[plane] : filt_lvl_r[plane];
3767 				//assert(plane >= 0 && plane <= 2);
3768 				const uint8_t seg_lf_info_y0 = seg->seg_lf_info_y[seg_id] & 0xff;
3769 				const uint8_t seg_lf_info_y1 = (seg->seg_lf_info_y[seg_id]>>8) & 0xff;
3770 				const uint8_t seg_lf_info_u = seg->seg_lf_info_c[seg_id] & 0xff;
3771 				const uint8_t seg_lf_info_v = (seg->seg_lf_info_c[seg_id]>>8) & 0xff;
3772 				const uint8_t seg_lf_info = (plane==2) ? seg_lf_info_v : (plane==1) ?
3773 					seg_lf_info_u : ((dir==0) ?  seg_lf_info_y0 : seg_lf_info_y1);
3774 				const int8_t seg_lf_active = ((seg->enabled) && ((seg_lf_info>>7) & 0x1));
3775 				const int8_t seg_lf_data = conv2int8(seg_lf_info,7);
3776 #ifdef DBG_LPF_PRINT
3777 				const int8_t seg_lf_data_clip = (seg_lf_data>63) ? 63 :
3778 					(seg_lf_data<-63) ? -63 : seg_lf_data;
3779 #endif
3780 				if (seg_lf_active) {
3781 				  lvl_seg = myclamp(lvl_seg + (int32_t)seg_lf_data, 0, MAX_LOOP_FILTER);
3782 				}
3783 
3784 #ifdef DBG_LPF_PRINT
3785 				printk("LF_PRINT:plane(%d) seg_id(%d) dir(%d) seg_lf_info(%d,0x%x),lvl_seg(0x%x)\n",
3786 					plane,seg_id,dir,seg_lf_active,seg_lf_data_clip,lvl_seg);
3787 #endif
3788 
3789 				if (!lf->mode_ref_delta_enabled) {
3790 				  // we could get rid of this if we assume that deltas are set to
3791 				  // zero when not in use; encoder always uses deltas
3792 				  memset(lfi->lvl[plane][seg_id][dir], lvl_seg,
3793 						 sizeof(lfi->lvl[plane][seg_id][dir]));
3794 				} else {
3795 					int32_t ref, mode;
3796 					const int32_t scale = 1 << (lvl_seg >> 5);
3797 					const int32_t intra_lvl = lvl_seg + lf->ref_deltas[INTRA_FRAME] * scale;
3798 					lfi->lvl[plane][seg_id][dir][INTRA_FRAME][0] =
3799 					  myclamp(intra_lvl, 0, MAX_LOOP_FILTER);
3800 #ifdef DBG_LPF_PRINT
3801 					printk("LF_PRINT:ref_deltas[INTRA_FRAME](%d)\n",lf->ref_deltas[INTRA_FRAME]);
3802 #endif
3803 					for (ref = LAST_FRAME; ref < REF_FRAMES; ++ref) {		 // LAST_FRAME==1 REF_FRAMES==8
3804 						for (mode = 0; mode < MAX_MODE_LF_DELTAS; ++mode) {	 // MAX_MODE_LF_DELTAS==2
3805 							const int32_t inter_lvl =
3806 								lvl_seg + lf->ref_deltas[ref] * scale +
3807 								lf->mode_deltas[mode] * scale;
3808 							lfi->lvl[plane][seg_id][dir][ref][mode] =
3809 								myclamp(inter_lvl, 0, MAX_LOOP_FILTER);
3810 #ifdef DBG_LPF_PRINT
3811 							printk("LF_PRINT:ref_deltas(%d) mode_deltas(%d)\n",
3812 							lf->ref_deltas[ref], lf->mode_deltas[mode]);
3813 #endif
3814 						}
3815 					}
3816 				}
3817 			}
3818 		}
3819 	}
3820 
3821 #ifdef DBG_LPF_PRINT
3822 	for (i = 0; i <= MAX_LOOP_FILTER; i++) {
3823 		printk("LF_PRINT:(%2d) thr=%d,blim=%3d,lim=%2d\n",
3824 			i, lfi->lfthr[i].hev_thr,
3825 			lfi->lfthr[i].mblim, lfi->lfthr[i].lim);
3826 	}
3827 	for (plane = 0; plane < 3; plane++) {
3828 		for (seg_id = 0; seg_id < MAX_SEGMENTS; seg_id++) { // MAX_SEGMENTS==8
3829 			for (dir = 0; dir < 2; ++dir) {
3830 				int32_t mode;
3831 				for (mode = 0; mode < 2; ++mode) {
3832 					printk("assign {lvl[%d][%d][%d][0][%d],lvl[%d][%d][%d][1][%d],lvl[%d][%d][%d][2][%d],lvl[%d][%d][%d][3][%d],lvl[%d][%d][%d][4][%d],lvl[%d][%d][%d][5][%d],lvl[%d][%d][%d][6][%d],lvl[%d][%d][%d][7][%d]}={6'd%2d,6'd%2d,6'd%2d,6'd%2d,6'd%2d,6'd%2d,6'd%2d,6'd%2d};\n",
3833 					plane, seg_id, dir, mode,
3834 					plane, seg_id, dir, mode,
3835 					plane, seg_id, dir, mode,
3836 					plane, seg_id, dir, mode,
3837 					plane, seg_id, dir, mode,
3838 					plane, seg_id, dir, mode,
3839 					plane, seg_id, dir, mode,
3840 					plane, seg_id, dir, mode,
3841 					lfi->lvl[plane][seg_id][dir][0][mode],
3842 					lfi->lvl[plane][seg_id][dir][1][mode],
3843 					lfi->lvl[plane][seg_id][dir][2][mode],
3844 					lfi->lvl[plane][seg_id][dir][3][mode],
3845 					lfi->lvl[plane][seg_id][dir][4][mode],
3846 					lfi->lvl[plane][seg_id][dir][5][mode],
3847 					lfi->lvl[plane][seg_id][dir][6][mode],
3848 					lfi->lvl[plane][seg_id][dir][7][mode]);
3849 				}
3850 			}
3851 		}
3852 	}
3853 #endif
3854 	// Write to register
3855 	for (i = 0; i < 192; i++) {
3856 		uint32_t level;
3857 		level = ((lfi->lvl[i>>6&3][i>>3&7][1][i&7][1] & 0x3f)<<24) |
3858 			((lfi->lvl[i>>6&3][i>>3&7][1][i&7][0] & 0x3f)<<16) |
3859 			((lfi->lvl[i>>6&3][i>>3&7][0][i&7][1] & 0x3f)<<8) |
3860 			(lfi->lvl[i>>6&3][i>>3&7][0][i&7][0] & 0x3f);
3861 		if (!lf->filter_level[0] && !lf->filter_level[1])
3862 			level = 0;
3863 		WRITE_VREG(HEVC_DBLK_CFGA, level);
3864 	}
3865 #ifdef DBG_LPF_DBLK_FORCED_OFF
3866 	if (lf->lf_pic_cnt == 2) {
3867 		printk("LF_PRINT: pic_cnt(%d) dblk forced off !!!\n", lf->lf_pic_cnt);
3868 		WRITE_VREG(HEVC_DBLK_DBLK0, 0);
3869 	} else
3870 		WRITE_VREG(HEVC_DBLK_DBLK0,
3871 			lf->filter_level[0] | lf->filter_level[1] << 6 |
3872 			lf->filter_level_u << 12 | lf->filter_level_v << 18);
3873 #else
3874 	WRITE_VREG(HEVC_DBLK_DBLK0,
3875 		lf->filter_level[0] | lf->filter_level[1]<<6 |
3876 		lf->filter_level_u<<12 | lf->filter_level_v<<18);
3877 #endif
3878 	for (i =0; i < 10; i++)
3879 		WRITE_VREG(HEVC_DBLK_DBLK1,
3880 			((i<2) ? lf->mode_deltas[i&1] : lf->ref_deltas[(i-2)&7]));
3881 	for (i = 0; i < 8; i++)
3882 		WRITE_VREG(HEVC_DBLK_DBLK2,
3883 			(uint32_t)(seg->seg_lf_info_y[i]) | (uint32_t)(seg->seg_lf_info_c[i]<<16));
3884 
3885 	// Set P_HEVC_DBLK_CFGB again
3886 	{
3887 		uint32_t lpf_data32 = READ_VREG(HEVC_DBLK_CFGB);
3888 		if (lf->mode_ref_delta_enabled)
3889 			lpf_data32 |=  (0x1<<28); // mode_ref_delta_enabled
3890 		else
3891 			lpf_data32 &= ~(0x1<<28);
3892 		if (seg->enabled)
3893 			lpf_data32 |=  (0x1<<29); // seg enable
3894 		else
3895 			lpf_data32 &= ~(0x1<<29);
3896 		if (pic_width >= 1280)
3897 			lpf_data32 |= (0x1 << 4); // dblk pipeline mode=1 for performance
3898 		else
3899 			lpf_data32 &= ~(0x3 << 4);
3900 		WRITE_VREG(HEVC_DBLK_CFGB, lpf_data32);
3901 	}
3902 	  // Set CDEF
3903 	WRITE_VREG(HEVC_DBLK_CDEF0, buf_spec->cdef_data.buf_start);
3904 	{
3905 		uint32_t cdef_data32 = (READ_VREG(HEVC_DBLK_CDEF1) & 0xffffff00);
3906 		cdef_data32 |= 17;	// TODO ERROR :: cdef temp dma address left offset
3907 		WRITE_VREG(HEVC_DBLK_CDEF1, cdef_data32);
3908 	}
3909 	// Picture count
3910 	lf->lf_pic_cnt++;
3911 }
3912 #endif  // #ifdef AOM_AV1_DBLK_INIT
3913 
3914 #ifdef AOM_AV1_UPSCALE_INIT
3915 /*
3916  * these functions here for upscaling updated in every picture
3917  */
3918 #define RS_SUBPEL_BITS 6
3919 #define RS_SUBPEL_MASK ((1 << RS_SUBPEL_BITS) - 1)
3920 #define RS_SCALE_SUBPEL_BITS 14
3921 #define RS_SCALE_SUBPEL_MASK ((1 << RS_SCALE_SUBPEL_BITS) - 1)
3922 #define RS_SCALE_EXTRA_BITS (RS_SCALE_SUBPEL_BITS - RS_SUBPEL_BITS)
3923 #define RS_SCALE_EXTRA_OFF (1 << (RS_SCALE_EXTRA_BITS - 1))
3924 
av1_get_upscale_convolve_step(int32_t in_length,int32_t out_length)3925 static int32_t av1_get_upscale_convolve_step(int32_t in_length, int32_t out_length) {
3926   return ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length;
3927 }
3928 
get_upscale_convolve_x0(int32_t in_length,int32_t out_length,int32_t x_step_qn)3929 static int32_t get_upscale_convolve_x0(int32_t in_length, int32_t out_length,
3930 									   int32_t x_step_qn) {
3931   const int32_t err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS);
3932   const int32_t x0 =
3933 	  (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
3934 	   out_length / 2) /
3935 		  out_length +
3936 	  RS_SCALE_EXTRA_OFF - err / 2;
3937   return (int32_t)((uint32_t)x0 & RS_SCALE_SUBPEL_MASK);
3938 }
3939 
av1_upscale_frame_init(AV1Decoder * pbi,AV1_COMMON * cm,param_t * params)3940 void av1_upscale_frame_init(AV1Decoder* pbi, AV1_COMMON *cm, param_t* params)
3941 {
3942 	BuffInfo_t* buf_spec = pbi->work_space_buf;
3943 	//uint32_t data32;
3944 	const int32_t width                   = cm->dec_width;
3945 	const int32_t superres_upscaled_width = cm->superres_upscaled_width;
3946 	const int32_t x_step_qn_luma          = av1_get_upscale_convolve_step(width, superres_upscaled_width);
3947 	const int32_t x0_qn_luma              = get_upscale_convolve_x0(width, superres_upscaled_width, x_step_qn_luma);
3948 	const int32_t x_step_qn_chroma        = av1_get_upscale_convolve_step((width+1)>>1, (superres_upscaled_width+1)>>1);
3949 	const int32_t x0_qn_chroma            = get_upscale_convolve_x0((width+1)>>1, (superres_upscaled_width+1)>>1, x_step_qn_chroma);
3950     av1_print2(AOM_DEBUG_HW_MORE,
3951 		"UPS_PRINT: width(%d -> %d)\n",
3952 		width, superres_upscaled_width);
3953     av1_print2(AOM_DEBUG_HW_MORE,
3954 		"UPS_PRINT: xstep(%d,%d)(0x%X, 0x%X) x0qn(%d,%d)(0x%X, 0x%X)\n",
3955 	  x_step_qn_luma,x_step_qn_chroma,
3956 	  x_step_qn_luma,x_step_qn_chroma,
3957 	  x0_qn_luma,x0_qn_chroma,
3958 	  x0_qn_luma,x0_qn_chroma);
3959     WRITE_VREG(HEVC_DBLK_UPS1, buf_spec->ups_data.buf_start);
3960     WRITE_VREG(HEVC_DBLK_UPS2, x0_qn_luma);		 // x0_qn y
3961     WRITE_VREG(HEVC_DBLK_UPS3, x0_qn_chroma);	   // x0_qn c
3962     WRITE_VREG(HEVC_DBLK_UPS4, x_step_qn_luma);	 // x_step y
3963     WRITE_VREG(HEVC_DBLK_UPS5, x_step_qn_chroma);   // x_step c
3964     WRITE_VREG(AV1_UPSCALE_X0_QN, (x0_qn_chroma<<16)|x0_qn_luma);
3965     WRITE_VREG(AV1_UPSCALE_STEP_QN, (x_step_qn_chroma<<16)|x_step_qn_luma);
3966 
3967 /*
3968  * TileR calculation here if cm needs an exactly accurate value
3969  */
3970 //#define AV1_UPSCALE_TILER_CALCULATION
3971 #ifdef AV1_UPSCALE_TILER_CALCULATION
3972     uint32_t upscl_enabled = 1; // 1 just for example, actually this is use_superres flag
3973     uint32_t tiler_x = 192; // 192 just for example, actually this is tile end
3974     uint32_t ux;
3975     uint32_t ux_tiler,ux_tiler_rnd32;
3976     uint32_t xqn_y;
3977     uint32_t xqn_c;
3978     uint32_t tiler_x_y = tiler_x     - 8 - 3; // dblk/cdef left-shift-8 plus upscaling extra-3
3979     uint32_t tiler_x_c = (tiler_x/2) - 4 - 3; // dblk/cdef left-shift-4 plus upscaling extra-3
3980 
3981     xqn_y = x0_qn_luma;
3982     xqn_c = x0_qn_chroma;
3983     ux_tiler = 0;
3984     ux_tiler_rnd32 = 0;
3985     for (ux=0; ux<16384; ux+=8) {
3986 	    uint32_t x1qn_y     = xqn_y + x_step_qn_luma  *(  7+3); // extra-3 is for lrf
3987 	    uint32_t x1qn_c     = xqn_c + x_step_qn_chroma*(  3+3); // extra-3 is for lrf
3988 	    uint32_t x1qn_y_nxt = xqn_y + x_step_qn_luma  *(8+7+3); // extra-3 is for lrf
3989 	    uint32_t x1qn_c_nxt = xqn_c + x_step_qn_chroma*(4+3+3); // extra-3 is for lrf
3990 
3991 	    uint32_t x1_y = upscl_enabled ? (x1qn_y>>14) : ux    +7+3;
3992 	    uint32_t x1_c = upscl_enabled ? (x1qn_c>>14) : (ux/2)+3+3;
3993 	    uint32_t x1_y_nxt = upscl_enabled ? (x1qn_y_nxt>>14) : ux    +8+7+3;
3994 	    uint32_t x1_c_nxt = upscl_enabled ? (x1qn_c_nxt>>14) : (ux/2)+4+3+3;
3995 
3996 	    if ((x1_y<tiler_x_y && x1_c<tiler_x_c) &&
3997 			(x1_y_nxt>=tiler_x_y || x1_c_nxt>=tiler_x_c)) {
3998 		    ux_tiler = ux;
3999 		    ux_tiler_rnd32 = (ux_tiler/32 + (ux_tiler%32 ? 1 : 0)) * 32;
4000 		    break;
4001 		}
4002 
4003 	    xqn_y += x_step_qn_luma*8;
4004 	    xqn_c += x_step_qn_chroma*4;
4005 	}
4006 
4007     av1_print(hw, AOM_DEBUG_HW_MORE,
4008 		"UPS_PRINT: xqn_y(0x%x), xqn_c(0x%x), x1qn_y(0x%x), x1qn_c(0x%x)\n",
4009 		xqn_y, xqn_c, x1qn_y, x1qn_c);
4010     av1_print(hw, AOM_DEBUG_HW_MORE,
4011 		"UPS_PRINT: ux_tiler(%d)(0x%x), ux_tiler_rnd32(%d)(0x%x)\n",
4012 		ux_tiler, ux_tiler, ux_tiler_rnd32, ux_tiler_rnd32);
4013 #endif
4014 
4015 	// TEMP write lrf register here
4016 	//WRITE_VREG(HEVC_DBLK_LRF0, 1<<0 | 1<<2); // LRF UNIT SIZE
4017 	//WRITE_VREG(HEVC_DBLK_LRF1, 3<<0 | 1<<8 | 1<<16 | 1<<24); // LRF UNIT NUMBER
4018 
4019 	// TEMP Global Enables write here
4020 	/*
4021     const uint32_t dblk_enable = (!cm->allow_intrabc && !cm->single_tile_decoding && (cm->lf.filter_level[0] || cm->lf.filter_level[1]));
4022     const uint32_t cdef_enable = (!cm->allow_intrabc && !cm->single_tile_decoding && !cm->skip_loop_filter && !cm->coded_lossless && (cm->cdef_bits || cm->cdef_strengths[0] || cm->cdef_uv_strengths[0]));
4023     printk("LPF_ENABLES : dblk(%d) cdef(%d)\n", dblk_enable, cdef_enable);
4024     data32 = READ_VREG(HEVC_DBLK_CFGB );
4025     data32 &= ~(0xf<<20);
4026     data32 |= (dblk_enable<<20);
4027     data32 |= (cdef_enable<<23);
4028     WRITE_VREG(HEVC_DBLK_CFGB, data32);
4029 	*/
4030 }
4031 
4032 #endif // #ifdef AOM_AV1_UPSCALE_INIT
4033 
release_dblk_struct(struct AV1HW_s * hw)4034 static void release_dblk_struct(struct AV1HW_s *hw)
4035 {
4036 #ifdef AOM_AV1_DBLK_INIT
4037 	if (hw->lfi)
4038 		vfree(hw->lfi);
4039 	if (hw->lf)
4040 		vfree(hw->lf);
4041 	if (hw->seg_4lf)
4042 		vfree(hw->seg_4lf);
4043 	hw->lfi = NULL;
4044 	hw->lf = NULL;
4045 	hw->seg_4lf = NULL;
4046 #endif
4047 }
4048 
init_dblk_struc(struct AV1HW_s * hw)4049 static int init_dblk_struc(struct AV1HW_s *hw)
4050 {
4051 #ifdef AOM_AV1_DBLK_INIT
4052     hw->lfi = vmalloc(sizeof(loop_filter_info_n));
4053     hw->lf = vmalloc(sizeof(struct loopfilter));
4054     hw->seg_4lf = vmalloc(sizeof(struct segmentation_lf));
4055 
4056     if (hw->lfi == NULL || hw->lf == NULL || hw->seg_4lf == NULL) {
4057 		printk("[test.c] aom_loop_filter init malloc error!!!\n");
4058 		release_dblk_struct(hw);
4059 		return -1;
4060 	}
4061 
4062     hw->lf->mode_ref_delta_enabled = 1; // set default here
4063     hw->lf->mode_ref_delta_update = 1; // set default here
4064     hw->lf->sharpness_level = 0; // init to 0
4065     hw->lf->lf_pic_cnt = 0; // init to 0
4066 #endif
4067     return 0;
4068 }
4069 
config_dblk_hw(struct AV1HW_s * hw)4070 static void config_dblk_hw(struct AV1HW_s *hw)
4071 {
4072     AV1Decoder *pbi = hw->pbi;
4073     AV1_COMMON *cm = &hw->pbi->common;
4074     loop_filter_info_n *lfi = hw->lfi;
4075     struct loopfilter *lf = hw->lf;
4076     struct segmentation_lf *seg_4lf = hw->seg_4lf;
4077     BuffInfo_t* buf_spec = pbi->work_space_buf;
4078 	PIC_BUFFER_CONFIG* cur_pic_config = &cm->cur_frame->buf;
4079 	PIC_BUFFER_CONFIG* prev_pic_config = &cm->prev_frame->buf;
4080     int i;
4081 
4082 #ifdef AOM_AV1_DBLK_INIT
4083 #ifdef DUAL_DECODE
4084 #else
4085 	av1_print(hw, AOM_DEBUG_HW_MORE,
4086 	"[test.c ref_delta] cur_frame : %p prev_frame : %p - %p \n",
4087 	cm->cur_frame, cm->prev_frame,
4088 	av1_get_primary_ref_frame_buf(cm));
4089 	// get lf parameters from parser
4090 	lf->mode_ref_delta_enabled =
4091 		(hw->aom_param.p.loop_filter_mode_ref_delta_enabled & 1);
4092 	lf->mode_ref_delta_update =
4093 		((hw->aom_param.p.loop_filter_mode_ref_delta_enabled >> 1) & 1);
4094 	lf->sharpness_level =
4095 		hw->aom_param.p.loop_filter_sharpness_level;
4096 	if (((hw->aom_param.p.loop_filter_mode_ref_delta_enabled)&3) == 3) { // enabled but and update
4097 		if (cm->prev_frame <= 0) {
4098 		  // already initialized in Microcode
4099 			lf->ref_deltas[0] = conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_0),7);
4100 			lf->ref_deltas[1]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_0>>8),7);
4101 			lf->ref_deltas[2]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_1),7);
4102 			lf->ref_deltas[3]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_1>>8),7);
4103 			lf->ref_deltas[4]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_2),7);
4104 			lf->ref_deltas[5]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_2>>8),7);
4105 			lf->ref_deltas[6]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_3),7);
4106 			lf->ref_deltas[7]	= conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_3>>8),7);
4107 			lf->mode_deltas[0] = conv2int8((uint8_t)(hw->aom_param.p.loop_filter_mode_deltas_0),7);
4108 			lf->mode_deltas[1] = conv2int8((uint8_t)(hw->aom_param.p.loop_filter_mode_deltas_0>>8),7);
4109 		} else {
4110 			lf->ref_deltas[0] = (hw->aom_param.p.loop_filter_ref_deltas_0 & 0x80) ?
4111 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_0),7) :
4112 			    cm->prev_frame->ref_deltas[0];
4113 			lf->ref_deltas[1]	= (hw->aom_param.p.loop_filter_ref_deltas_0 & 0x8000) ?
4114 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_0>>8),7) :
4115 			    cm->prev_frame->ref_deltas[1];
4116 			lf->ref_deltas[2]	= (hw->aom_param.p.loop_filter_ref_deltas_1 & 0x80) ?
4117 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_1),7) :
4118 			    cm->prev_frame->ref_deltas[2];
4119 			lf->ref_deltas[3]	= (hw->aom_param.p.loop_filter_ref_deltas_1 & 0x8000) ?
4120 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_1>>8),7) :
4121 			    cm->prev_frame->ref_deltas[3];
4122 			lf->ref_deltas[4]	= (hw->aom_param.p.loop_filter_ref_deltas_2 & 0x80) ?
4123 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_2),7) :
4124 			    cm->prev_frame->ref_deltas[4];
4125 			lf->ref_deltas[5]	= (hw->aom_param.p.loop_filter_ref_deltas_2 & 0x8000) ?
4126 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_2>>8),7) :
4127 			    cm->prev_frame->ref_deltas[5];
4128 			lf->ref_deltas[6] = (hw->aom_param.p.loop_filter_ref_deltas_3 & 0x80) ?
4129 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_3),7) :
4130 			    cm->prev_frame->ref_deltas[6];
4131 			lf->ref_deltas[7]	= (hw->aom_param.p.loop_filter_ref_deltas_3 & 0x8000) ?
4132 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_ref_deltas_3>>8),7) :
4133 			    cm->prev_frame->ref_deltas[7];
4134 			lf->mode_deltas[0] = (hw->aom_param.p.loop_filter_mode_deltas_0 & 0x80) ?
4135 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_mode_deltas_0),7) :
4136 			    cm->prev_frame->mode_deltas[0];
4137 			lf->mode_deltas[1] = (hw->aom_param.p.loop_filter_mode_deltas_0 & 0x8000) ?
4138 				conv2int8((uint8_t)(hw->aom_param.p.loop_filter_mode_deltas_0>>8),7) :
4139 			    cm->prev_frame->mode_deltas[1];
4140 		}
4141 	} //else if (hw->aom_param.p.loop_filter_mode_ref_delta_enabled == 1) { // enabled but no update
4142 	  else { // match c code -- not enabled, still need to copy prev to used for next
4143 		if ((cm->prev_frame <= 0) | (hw->aom_param.p.loop_filter_mode_ref_delta_enabled & 4)) {
4144 			av1_print(hw, AOM_DEBUG_HW_MORE,
4145 				"[test.c] mode_ref_delta set to default\n");
4146 			lf->ref_deltas[0] = conv2int8((uint8_t)1,7);
4147 			lf->ref_deltas[1] = conv2int8((uint8_t)0,7);
4148 			lf->ref_deltas[2] = conv2int8((uint8_t)0,7);
4149 			lf->ref_deltas[3] = conv2int8((uint8_t)0,7);
4150 			lf->ref_deltas[4] = conv2int8((uint8_t)0xff,7);
4151 			lf->ref_deltas[5] = conv2int8((uint8_t)0,7);
4152 			lf->ref_deltas[6] = conv2int8((uint8_t)0xff,7);
4153 			lf->ref_deltas[7] = conv2int8((uint8_t)0xff,7);
4154 			lf->mode_deltas[0] = conv2int8((uint8_t)0,7);
4155 			lf->mode_deltas[1] = conv2int8((uint8_t)0,7);
4156 		} else {
4157 			av1_print(hw, AOM_DEBUG_HW_MORE,
4158 				"[test.c] mode_ref_delta copy from prev_frame\n");
4159 			lf->ref_deltas[0]			   = cm->prev_frame->ref_deltas[0];
4160 			lf->ref_deltas[1]			   = cm->prev_frame->ref_deltas[1];
4161 			lf->ref_deltas[2]			   = cm->prev_frame->ref_deltas[2];
4162 			lf->ref_deltas[3]			   = cm->prev_frame->ref_deltas[3];
4163 			lf->ref_deltas[4]			   = cm->prev_frame->ref_deltas[4];
4164 			lf->ref_deltas[5]			   = cm->prev_frame->ref_deltas[5];
4165 			lf->ref_deltas[6]			   = cm->prev_frame->ref_deltas[6];
4166 			lf->ref_deltas[7]			   = cm->prev_frame->ref_deltas[7];
4167 			lf->mode_deltas[0]			  = cm->prev_frame->mode_deltas[0];
4168 			lf->mode_deltas[1]			  = cm->prev_frame->mode_deltas[1];
4169 		}
4170 	}
4171 	lf->filter_level[0]			 = hw->aom_param.p.loop_filter_level_0;
4172 	lf->filter_level[1]			 = hw->aom_param.p.loop_filter_level_1;
4173 	lf->filter_level_u    		  = hw->aom_param.p.loop_filter_level_u;
4174 	lf->filter_level_v    		  = hw->aom_param.p.loop_filter_level_v;
4175 
4176 	cm->cur_frame->ref_deltas[0] = lf->ref_deltas[0];
4177 	cm->cur_frame->ref_deltas[1] = lf->ref_deltas[1];
4178 	cm->cur_frame->ref_deltas[2] = lf->ref_deltas[2];
4179 	cm->cur_frame->ref_deltas[3] = lf->ref_deltas[3];
4180 	cm->cur_frame->ref_deltas[4] = lf->ref_deltas[4];
4181 	cm->cur_frame->ref_deltas[5] = lf->ref_deltas[5];
4182 	cm->cur_frame->ref_deltas[6] = lf->ref_deltas[6];
4183 	cm->cur_frame->ref_deltas[7] = lf->ref_deltas[7];
4184 	cm->cur_frame->mode_deltas[0] = lf->mode_deltas[0];
4185 	cm->cur_frame->mode_deltas[1] = lf->mode_deltas[1];
4186 
4187 	// get seg_4lf parameters from parser
4188 	seg_4lf->enabled = hw->aom_param.p.segmentation_enabled & 1;
4189 	cm->cur_frame->segmentation_enabled = hw->aom_param.p.segmentation_enabled & 1;
4190 	cm->cur_frame->intra_only = (hw->aom_param.p.segmentation_enabled >> 2) & 1;
4191 	cm->cur_frame->segmentation_update_map = (hw->aom_param.p.segmentation_enabled >> 3) & 1;
4192 
4193 	if (hw->aom_param.p.segmentation_enabled & 1) { // segmentation_enabled
4194 		if (hw->aom_param.p.segmentation_enabled & 2) { // segmentation_update_data
4195 			for (i = 0; i < MAX_SEGMENTS; i++) {
4196 				seg_4lf->seg_lf_info_y[i] = hw->aom_param.p.seg_lf_info_y[i];
4197 				seg_4lf->seg_lf_info_c[i] = hw->aom_param.p.seg_lf_info_c[i];
4198 				#ifdef DBG_LPF_PRINT
4199 					printk(" read seg_lf_info [%d] : 0x%x, 0x%x\n",
4200 					i, seg_4lf->seg_lf_info_y[i], seg_4lf->seg_lf_info_c[i]);
4201 				#endif
4202 				}
4203 			} // segmentation_update_data
4204 			else { // no segmentation_update_data
4205 				if (cm->prev_frame <= 0) {
4206 					for (i=0;i<MAX_SEGMENTS;i++) {
4207 						seg_4lf->seg_lf_info_y[i] = 0;
4208 						seg_4lf->seg_lf_info_c[i] = 0;
4209 					}
4210 				} else {
4211 					for (i = 0; i < MAX_SEGMENTS; i++) {
4212 						seg_4lf->seg_lf_info_y[i] = cm->prev_frame->seg_lf_info_y[i];
4213 						seg_4lf->seg_lf_info_c[i] = cm->prev_frame->seg_lf_info_c[i];
4214 		#ifdef DBG_LPF_PRINT
4215 					  printk(" Refrence seg_lf_info [%d] : 0x%x, 0x%x\n",
4216 					  i, seg_4lf->seg_lf_info_y[i], seg_4lf->seg_lf_info_c[i]);
4217 		#endif
4218 					}
4219 				}
4220 			} // no segmentation_update_data
4221 		} // segmentation_enabled
4222 		else {
4223 			for (i=0;i<MAX_SEGMENTS;i++) {
4224 				seg_4lf->seg_lf_info_y[i] = 0;
4225 				seg_4lf->seg_lf_info_c[i] = 0;
4226 			}
4227 		} // NOT segmentation_enabled
4228 		for (i=0;i<MAX_SEGMENTS;i++) {
4229 			cm->cur_frame->seg_lf_info_y[i] = seg_4lf->seg_lf_info_y[i];
4230 			cm->cur_frame->seg_lf_info_c[i] = seg_4lf->seg_lf_info_c[i];
4231 #ifdef DBG_LPF_PRINT
4232 			printk(" SAVE seg_lf_info [%d] : 0x%x, 0x%x\n",
4233 			i, cm->cur_frame->seg_lf_info_y[i],
4234 			cm->cur_frame->seg_lf_info_c[i]);
4235 #endif
4236 		}
4237 
4238 	/*
4239 	* Update loop filter Thr/Lvl table for every frame
4240 	*/
4241 	av1_print(hw, AOM_DEBUG_HW_MORE,
4242 		"[test.c] av1_loop_filter_frame_init (run before every frame decoding start)\n");
4243 	av1_loop_filter_frame_init(pbi, seg_4lf, lfi, lf, cm->dec_width);
4244 #endif // not DUAL_DECODE
4245 #endif
4246 
4247 #ifdef AOM_AV1_UPSCALE_INIT
4248 	/*
4249 	* init for upscaling
4250 	*/
4251 	av1_print(hw, AOM_DEBUG_HW_MORE,
4252 		"[test.c] av1_upscale_frame_init (run before every frame decoding start)\n");
4253 	av1_upscale_frame_init(pbi,
4254 		&pbi->common, &hw->aom_param);
4255 #endif // #ifdef AOM_AV1_UPSCALE_INIT
4256 
4257 	//BuffInfo_t* buf_spec = pbi->work_space_buf;
4258 	av1_print(hw, AOM_DEBUG_HW_MORE,
4259 		"[test.c] cur_frame : %p prev_frame : %p - %p \n",
4260 		cm->cur_frame, cm->prev_frame, av1_get_primary_ref_frame_buf(cm));
4261 	if (cm->cur_frame <= 0) {
4262 		WRITE_VREG(AOM_AV1_CDF_BUFFER_W, buf_spec->cdf_buf.buf_start);
4263 		WRITE_VREG(AOM_AV1_SEG_MAP_BUFFER_W, buf_spec->seg_map.buf_start);
4264 	}
4265 	else {
4266 		av1_print(hw, AOM_DEBUG_HW_MORE,
4267 			"[test.c] Config WRITE CDF_BUF/SEG_MAP_BUF  : %d\n",
4268 			cur_pic_config->index);
4269 		WRITE_VREG(AOM_AV1_CDF_BUFFER_W,
4270 			buf_spec->cdf_buf.buf_start + (0x8000*cur_pic_config->index));
4271 		WRITE_VREG(AOM_AV1_SEG_MAP_BUFFER_W,
4272 			buf_spec->seg_map.buf_start + (seg_map_size*cur_pic_config->index));
4273 	}
4274 	cm->cur_frame->seg_mi_rows = cm->cur_frame->mi_rows;
4275 	cm->cur_frame->seg_mi_cols = cm->cur_frame->mi_cols;
4276 	if (cm->prev_frame <= 0) {
4277 		WRITE_VREG(AOM_AV1_CDF_BUFFER_R, buf_spec->cdf_buf.buf_start);
4278 		WRITE_VREG(AOM_AV1_SEG_MAP_BUFFER_R, buf_spec->seg_map.buf_start);
4279 	} else {
4280 		av1_print(hw, AOM_DEBUG_HW_MORE,
4281 			"[test.c] Config READ  CDF_BUF/SEG_MAP_BUF  : %d\n",
4282 			prev_pic_config->index);
4283 		WRITE_VREG(AOM_AV1_CDF_BUFFER_R,
4284 			buf_spec->cdf_buf.buf_start + (0x8000*prev_pic_config->index));
4285 		WRITE_VREG(AOM_AV1_SEG_MAP_BUFFER_R,
4286 			buf_spec->seg_map.buf_start + (seg_map_size*prev_pic_config->index));
4287 
4288 		// segmentation_enabled but no segmentation_update_data
4289 		if ((hw->aom_param.p.segmentation_enabled & 3) == 1) {
4290 			av1_print(hw, AOM_DEBUG_HW_MORE,
4291 				"[test.c] segfeatures_copy from prev_frame\n");
4292 			for (i = 0; i < 8; i++) {
4293 				WRITE_VREG(AOM_AV1_SEGMENT_FEATURE,
4294 					cm->prev_frame->segment_feature[i]);
4295 			}
4296 		}
4297 		// segmentation_enabled but no segmentation_update_map
4298 		if ((hw->aom_param.p.segmentation_enabled & 9) == 1) {
4299 			av1_print(hw, AOM_DEBUG_HW_MORE,
4300 				"[test.c] seg_map_size copy from prev_frame\n");
4301 			cm->cur_frame->seg_mi_rows = cm->prev_frame->seg_mi_rows;
4302 			cm->cur_frame->seg_mi_cols = cm->prev_frame->seg_mi_cols;
4303 		}
4304 	}
4305 #ifdef PRINT_HEVC_DATA_PATH_MONITOR
4306 	{
4307 		uint32_t total_clk_count;
4308 		uint32_t path_transfer_count;
4309 		uint32_t path_wait_count;
4310 		float path_wait_ratio;
4311 		if (pbi->decode_idx > 1) {
4312 			WRITE_VREG(HEVC_PATH_MONITOR_CTRL, 0); // Disabble monitor and set rd_idx to 0
4313 			total_clk_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4314 
4315 			WRITE_VREG(HEVC_PATH_MONITOR_CTRL, (1<<4)); // Disabble monitor and set rd_idx to 0
4316 
4317 			// parser --> iqit
4318 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4319 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4320 			if (path_transfer_count == 0)
4321 				path_wait_ratio = 0.0;
4322 			else
4323 				path_wait_ratio =
4324 					(float)path_wait_count/(float)path_transfer_count;
4325 			printk("[P%d HEVC PATH] Parser/IQIT/IPP/DBLK/OW/DDR/CMD WAITING \% : %.2f",
4326 				pbi->decode_idx - 2,
4327 				path_wait_ratio);
4328 
4329 			// iqit --> ipp
4330 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4331 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4332 			if (path_transfer_count == 0)
4333 				path_wait_ratio = 0.0;
4334 			else
4335 				path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
4336 			printk(" %.2f", path_wait_ratio);
4337 
4338 			// dblk <-- ipp
4339 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4340 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4341 			if (path_transfer_count == 0)
4342 				path_wait_ratio = 0.0;
4343 			else
4344 				path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
4345 			printk(" %.2f", path_wait_ratio);
4346 
4347 			// dblk --> ow
4348 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4349 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4350 			if (path_transfer_count == 0)
4351 				path_wait_ratio = 0.0;
4352 			else path_wait_ratio =
4353 				(float)path_wait_count/(float)path_transfer_count;
4354 			printk(" %.2f", path_wait_ratio);
4355 
4356 			// <--> DDR
4357 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4358 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4359 			if (path_transfer_count == 0)
4360 				path_wait_ratio = 0.0;
4361 			else path_wait_ratio =
4362 				(float)path_wait_count/(float)path_transfer_count;
4363 			printk(" %.2f", path_wait_ratio);
4364 
4365 			// CMD
4366 			path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4367 			path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
4368 			if (path_transfer_count == 0)
4369 				path_wait_ratio = 0.0;
4370 			else
4371 				path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
4372 			printk(" %.2f\n", path_wait_ratio);
4373 		}
4374 	}
4375 
4376 #endif
4377 
4378 }
4379 
aom_config_work_space_hw(struct AV1HW_s * hw,u32 mask)4380 static void aom_config_work_space_hw(struct AV1HW_s *hw, u32 mask)
4381 {
4382 	struct BuffInfo_s *buf_spec = hw->work_space_buf;
4383 	unsigned int data32;
4384 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
4385 	if (debug && hw->init_flag == 0)
4386 		av1_print(hw, AOM_DEBUG_HW_MORE, "%s %x %x %x %x %x %x %x %x\n",
4387 			__func__,
4388 			buf_spec->ipp.buf_start,
4389 			buf_spec->start_adr,
4390 			buf_spec->short_term_rps.buf_start,
4391 			buf_spec->sao_up.buf_start,
4392 			buf_spec->swap_buf.buf_start,
4393 			buf_spec->scalelut.buf_start,
4394 			buf_spec->dblk_para.buf_start,
4395 			buf_spec->dblk_data.buf_start);
4396 	if (mask & HW_MASK_FRONT) {
4397 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
4398 		if ((debug & AOM_AV1_DEBUG_SEND_PARAM_WITH_REG) == 0)
4399 			WRITE_VREG(HEVC_RPM_BUFFER, (u32)hw->rpm_phy_addr);
4400 
4401 		/*WRITE_VREG(HEVC_STREAM_SWAP_BUFFER,
4402 			buf_spec->swap_buf.buf_start);*/
4403 		WRITE_VREG(LMEM_DUMP_ADR, (u32)hw->lmem_phy_addr);
4404 
4405 	}
4406 
4407 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
4408 
4409     WRITE_VREG(AOM_AV1_DAALA_TOP_BUFFER,
4410 		buf_spec->daala_top.buf_start);
4411     WRITE_VREG(AV1_GMC_PARAM_BUFF_ADDR,
4412 		buf_spec->gmc_buf.buf_start);
4413 
4414     WRITE_VREG(HEVC_DBLK_CFG4,
4415 		buf_spec->dblk_para.buf_start); // cfg_addr_cif
4416     WRITE_VREG(HEVC_DBLK_CFG5,
4417 		buf_spec->dblk_data.buf_start); // cfg_addr_xio
4418 
4419 	if (mask & HW_MASK_BACK) {
4420 #ifdef LOSLESS_COMPRESS_MODE
4421 		int losless_comp_header_size =
4422 			compute_losless_comp_header_size(hw->init_pic_w,
4423 			hw->init_pic_h);
4424 		int losless_comp_body_size =
4425 			compute_losless_comp_body_size(hw->init_pic_w,
4426 			hw->init_pic_h, buf_alloc_depth == 10);
4427 #endif
4428 #ifdef AOM_AV1_MMU_DW
4429 		int losless_comp_header_size_dw =
4430 			compute_losless_comp_header_size_dw(hw->init_pic_w,
4431 			hw->init_pic_h);
4432 		int losless_comp_body_size_dw =
4433 			compute_losless_comp_body_size_dw(hw->init_pic_w,
4434 			hw->init_pic_h, buf_alloc_depth == 10);
4435 #endif
4436 		WRITE_VREG(HEVCD_IPP_LINEBUFF_BASE,
4437 			buf_spec->ipp.buf_start);
4438 		WRITE_VREG(HEVC_SAO_UP, buf_spec->sao_up.buf_start);
4439 		//WRITE_VREG(HEVC_SCALELUT, buf_spec->scalelut.buf_start);
4440 #ifdef CHANGE_REMOVED
4441 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
4442 			/* cfg_addr_adp*/
4443 			WRITE_VREG(HEVC_DBLK_CFGE, buf_spec->dblk_para.buf_start);
4444 			if (debug & AV1_DEBUG_BUFMGR_MORE)
4445 				pr_info("Write HEVC_DBLK_CFGE\n");
4446 		}
4447 #endif
4448 		/* cfg_p_addr */
4449 		WRITE_VREG(HEVC_DBLK_CFG4, buf_spec->dblk_para.buf_start);
4450 		/* cfg_d_addr */
4451 		WRITE_VREG(HEVC_DBLK_CFG5, buf_spec->dblk_data.buf_start);
4452 
4453 #ifdef CHANGE_REMOVED
4454 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
4455 		 /*
4456 		 * data32 = (READ_VREG(HEVC_DBLK_CFG3)>>8) & 0xff; // xio left offset, default is 0x40
4457 		 * data32 = data32 * 2;
4458 		 * data32 = (READ_VREG(HEVC_DBLK_CFG3)>>16) & 0xff; // adp left offset, default is 0x040
4459 		 * data32 = data32 * 2;
4460 		 */
4461 		WRITE_VREG(HEVC_DBLK_CFG3, 0x808010); // make left storage 2 x 4k]
4462 	}
4463 #endif
4464 #ifdef LOSLESS_COMPRESS_MODE
4465 	if (hw->mmu_enable) {
4466 		/*bit[4] : paged_mem_mode*/
4467 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0x1 << 4));
4468 #ifdef CHANGE_REMOVED
4469 		if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_SM1)
4470 #endif
4471 			WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, 0);
4472 	} else {
4473 		/*if (cur_pic_config->bit_depth == AOM_BITS_10)
4474 		 *	WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0<<3));
4475 		 */
4476 		/*bit[3] smem mdoe*/
4477 		/*else WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (1<<3));*/
4478 		/*bit[3] smem mdoe*/
4479 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,
4480 			(losless_comp_body_size >> 5));
4481 	}
4482 	/*WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,
4483 		(losless_comp_body_size >> 5));*/
4484 	/*WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,
4485 		(0xff<<20) | (0xff<<10) | 0xff);*/
4486 	/*8-bit mode */
4487 	WRITE_VREG(HEVC_CM_BODY_LENGTH, losless_comp_body_size);
4488 	WRITE_VREG(HEVC_CM_HEADER_OFFSET, losless_comp_body_size);
4489 	WRITE_VREG(HEVC_CM_HEADER_LENGTH, losless_comp_header_size);
4490 	if (get_double_write_mode(hw) & 0x10)
4491 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
4492 #else
4493 	WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
4494 #endif
4495 
4496 	if (hw->mmu_enable) {
4497 		WRITE_VREG(HEVC_SAO_MMU_VH0_ADDR, buf_spec->mmu_vbh.buf_start);
4498 		WRITE_VREG(HEVC_SAO_MMU_VH1_ADDR, buf_spec->mmu_vbh.buf_start
4499 				+ VBH_BUF_SIZE);
4500 		/*data32 = READ_VREG(HEVC_SAO_CTRL9);*/
4501 		/*data32 |= 0x1;*/
4502 		/*WRITE_VREG(HEVC_SAO_CTRL9, data32);*/
4503 
4504 		/* use HEVC_CM_HEADER_START_ADDR */
4505 		data32 = READ_VREG(HEVC_SAO_CTRL5);
4506 		data32 |= (1<<10);
4507 		WRITE_VREG(HEVC_SAO_CTRL5, data32);
4508 	}
4509 #ifdef AOM_AV1_MMU_DW
4510     data32 = READ_VREG(HEVC_SAO_CTRL5);
4511 	if (hw->dw_mmu_enable) {
4512 		data32 = READ_VREG(HEVC_SAO_CTRL9);
4513 		data32 |= (1<<10);
4514 		WRITE_VREG(HEVC_SAO_CTRL9, data32);
4515 
4516 	    WRITE_VREG(HEVC_CM_BODY_LENGTH2,losless_comp_body_size_dw);
4517 	    WRITE_VREG(HEVC_CM_HEADER_OFFSET2,losless_comp_body_size_dw);
4518 	    WRITE_VREG(HEVC_CM_HEADER_LENGTH2,losless_comp_header_size_dw);
4519 
4520 	    WRITE_VREG(HEVC_SAO_MMU_VH0_ADDR2, buf_spec->mmu_vbh_dw.buf_start);
4521 	    WRITE_VREG(HEVC_SAO_MMU_VH1_ADDR2, buf_spec->mmu_vbh_dw.buf_start
4522 			+ VBH_BUF_SIZE);
4523 
4524 		WRITE_VREG(HEVC_DW_VH0_ADDDR, buf_spec->mmu_vbh_dw.buf_start
4525 			+ (2 * VBH_BUF_SIZE));
4526 		WRITE_VREG(HEVC_DW_VH1_ADDDR, buf_spec->mmu_vbh_dw.buf_start
4527 			+ (3 * VBH_BUF_SIZE));
4528 
4529 		/* use HEVC_CM_HEADER_START_ADDR */
4530 	    data32 |= (1<<15);
4531 	} else
4532 	    data32 &= ~(1<<15);
4533     WRITE_VREG(HEVC_SAO_CTRL5, data32);
4534 #endif
4535 
4536 	WRITE_VREG(LMEM_DUMP_ADR, (u32)hw->lmem_phy_addr);
4537 #ifdef CHANGE_REMOVED
4538 
4539 		WRITE_VREG(AV1_SEG_MAP_BUFFER, buf_spec->seg_map.buf_start);
4540 
4541 		 /**/
4542 		WRITE_VREG(AV1_PROB_SWAP_BUFFER, hw->prob_buffer_phy_addr);
4543 		WRITE_VREG(AV1_COUNT_SWAP_BUFFER, hw->count_buffer_phy_addr);
4544 		if (hw->mmu_enable) {
4545 			if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A)
4546 				WRITE_VREG(HEVC_ASSIST_MMU_MAP_ADDR, hw->frame_mmu_map_phy_addr);
4547 			else
4548 				WRITE_VREG(AV1_MMU_MAP_BUFFER, hw->frame_mmu_map_phy_addr);
4549 		}
4550 #else
4551 	if (hw->mmu_enable)
4552 	    WRITE_VREG(HEVC_SAO_MMU_DMA_CTRL, hw->frame_mmu_map_phy_addr);
4553 #ifdef AOM_AV1_MMU_DW
4554 	if (hw->dw_mmu_enable) {
4555 		WRITE_VREG(HEVC_SAO_MMU_DMA_CTRL2, hw->dw_frame_mmu_map_phy_addr);
4556 		//default of 0xffffffff will disable dw
4557 	    WRITE_VREG(HEVC_SAO_Y_START_ADDR, 0);
4558 	    WRITE_VREG(HEVC_SAO_C_START_ADDR, 0);
4559 	}
4560 #endif
4561 #endif
4562 	}
4563 
4564 	config_aux_buf(hw);
4565 }
4566 
4567 #ifdef MCRCC_ENABLE
4568 static u32 mcrcc_cache_alg_flag = 1;
4569 static void mcrcc_perfcount_reset(struct AV1HW_s *hw);
4570 static void decomp_perfcount_reset(struct AV1HW_s *hw);
4571 #endif
4572 
aom_init_decoder_hw(struct AV1HW_s * hw,u32 mask)4573 static void aom_init_decoder_hw(struct AV1HW_s *hw, u32 mask)
4574 {
4575 	unsigned int data32;
4576 	int i;
4577 	const unsigned short parser_cmd[PARSER_CMD_NUMBER] = {
4578 		0x0401, 0x8401, 0x0800, 0x0402, 0x9002, 0x1423,
4579 		0x8CC3, 0x1423, 0x8804, 0x9825, 0x0800, 0x04FE,
4580 		0x8406, 0x8411, 0x1800, 0x8408, 0x8409, 0x8C2A,
4581 		0x9C2B, 0x1C00, 0x840F, 0x8407, 0x8000, 0x8408,
4582 		0x2000, 0xA800, 0x8410, 0x04DE, 0x840C, 0x840D,
4583 		0xAC00, 0xA000, 0x08C0, 0x08E0, 0xA40E, 0xFC00,
4584 		0x7C00
4585 	};
4586 #if 0
4587 	if (get_cpu_major_id() >= MESON_CPU_MAJOR_ID_G12A) {
4588 		/* Set MCR fetch priorities*/
4589 		data32 = 0x1 | (0x1 << 2) | (0x1 <<3) |
4590 			(24 << 4) | (32 << 11) | (24 << 18) | (32 << 25);
4591 		WRITE_VREG(HEVCD_MPP_DECOMP_AXIURG_CTL, data32);
4592 	}
4593 #endif
4594 	/*if (debug & AV1_DEBUG_BUFMGR_MORE)
4595 		pr_info("%s\n", __func__);*/
4596 	if (mask & HW_MASK_FRONT) {
4597 		data32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
4598 #ifdef CHANGE_REMOVED
4599 #if 1
4600 		/* set bit 31~29 to 3 if HEVC_STREAM_FIFO_CTL[29] is 1 */
4601 		data32 &= ~(7 << 29);
4602 		data32 |= (3 << 29);
4603 #endif
4604 		data32 = data32 |
4605 		(1 << 24) |/*stream_buffer_empty_int_amrisc_enable*/
4606 		(1 << 22) |/*stream_fifo_empty_int_amrisc_enable*/
4607 		(1 << 7) |/*dec_done_int_cpu_enable*/
4608 		(1 << 4) |/*startcode_found_int_cpu_enable*/
4609 		(0 << 3) |/*startcode_found_int_amrisc_enable*/
4610 		(1 << 0)	/*parser_int_enable*/
4611 		;
4612 #else
4613 		data32 = data32 & 0x03ffffff;
4614 		data32 = data32 |
4615 			(3 << 29) |  // stream_buffer_empty_int_ctl ( 0x200 interrupt)
4616 			(3 << 26) |  // stream_fifo_empty_int_ctl ( 4 interrupt)
4617 			(1 << 24) |  // stream_buffer_empty_int_amrisc_enable
4618 			(1 << 22) |  // stream_fifo_empty_int_amrisc_enable
4619 #ifdef AOM_AV1_HED_FB
4620 #ifdef DUAL_DECODE
4621 		// For HALT CCPU test. Use Pull inside CCPU to generate interrupt
4622 		// (1 << 9) |  // fed_fb_slice_done_int_amrisc_enable
4623 #else
4624 			(1 << 10) |  // fed_fb_slice_done_int_cpu_enable
4625 #endif
4626 #endif
4627 			(1 << 7) |  // dec_done_int_cpu_enable
4628 			(1 << 4) |  // startcode_found_int_cpu_enable
4629 			(0 << 3) |  // startcode_found_int_amrisc_enable
4630 			(1 << 0)    // parser_int_enable
4631 			;
4632 #endif
4633 		WRITE_VREG(HEVC_PARSER_INT_CONTROL, data32);
4634 
4635 		data32 = READ_VREG(HEVC_SHIFT_STATUS);
4636 		data32 = data32 |
4637 		(0 << 1) |/*emulation_check_off AV1
4638 			do not have emulation*/
4639 		(1 << 0)/*startcode_check_on*/
4640 		;
4641 		WRITE_VREG(HEVC_SHIFT_STATUS, data32);
4642 		WRITE_VREG(HEVC_SHIFT_CONTROL,
4643 		(0 << 14) | /*disable_start_code_protect*/
4644 		(1 << 10) | /*length_zero_startcode_en for AV1*/
4645 		(1 << 9) | /*length_valid_startcode_en for AV1*/
4646 		(3 << 6) | /*sft_valid_wr_position*/
4647 		(2 << 4) | /*emulate_code_length_sub_1*/
4648 		(3 << 1) | /*start_code_length_sub_1
4649 		AV1 use 0x00000001 as startcode (4 Bytes)*/
4650 		(1 << 0)   /*stream_shift_enable*/
4651 		);
4652 
4653 		WRITE_VREG(HEVC_CABAC_CONTROL,
4654 			(1 << 0)/*cabac_enable*/
4655 		);
4656 
4657 		WRITE_VREG(HEVC_PARSER_CORE_CONTROL,
4658 			(1 << 0)/* hevc_parser_core_clk_en*/
4659 		);
4660 
4661 		WRITE_VREG(HEVC_DEC_STATUS_REG, 0);
4662 	}
4663 
4664 	if (mask & HW_MASK_BACK) {
4665 		/*Initial IQIT_SCALELUT memory
4666 		-- just to avoid X in simulation*/
4667 
4668 		WRITE_VREG(HEVC_IQIT_SCALELUT_WR_ADDR, 0);/*cfg_p_addr*/
4669 		for (i = 0; i < 1024; i++)
4670 			WRITE_VREG(HEVC_IQIT_SCALELUT_DATA, 0);
4671 	}
4672 
4673 	if (mask & HW_MASK_FRONT) {
4674 		u32 decode_mode;
4675 /*
4676 #ifdef ENABLE_SWAP_TEST
4677 	WRITE_VREG(HEVC_STREAM_SWAP_TEST, 100);
4678 #else
4679 	WRITE_VREG(HEVC_STREAM_SWAP_TEST, 0);
4680 #endif
4681 */
4682 #ifdef MULTI_INSTANCE_SUPPORT
4683 		if (!hw->m_ins_flag) {
4684 			if (hw->low_latency_flag)
4685 				decode_mode = DECODE_MODE_SINGLE_LOW_LATENCY;
4686 			else
4687 				decode_mode = DECODE_MODE_SINGLE;
4688 		} else if (vdec_frame_based(hw_to_vdec(hw)))
4689 			decode_mode = hw->no_head ?
4690 				DECODE_MODE_MULTI_FRAMEBASE_NOHEAD :
4691 				DECODE_MODE_MULTI_FRAMEBASE;
4692 		else
4693 			decode_mode = DECODE_MODE_MULTI_STREAMBASE;
4694 		if (debug & AOM_DEBUG_BUFMGR_ONLY)
4695 			decode_mode |= (1 << 16);
4696 		WRITE_VREG(DECODE_MODE, decode_mode);
4697 		WRITE_VREG(HEVC_DECODE_SIZE, 0);
4698 		WRITE_VREG(HEVC_DECODE_COUNT, 0);
4699 #else
4700 	WRITE_VREG(DECODE_MODE, DECODE_MODE_SINGLE);
4701 	WRITE_VREG(HEVC_DECODE_PIC_BEGIN_REG, 0);
4702 	WRITE_VREG(HEVC_DECODE_PIC_NUM_REG, 0x7fffffff); /*to remove*/
4703 #endif
4704 	/*Send parser_cmd*/
4705 	WRITE_VREG(HEVC_PARSER_CMD_WRITE, (1 << 16) | (0 << 0));
4706 	for (i = 0; i < PARSER_CMD_NUMBER; i++)
4707 		WRITE_VREG(HEVC_PARSER_CMD_WRITE, parser_cmd[i]);
4708 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_0, PARSER_CMD_SKIP_CFG_0);
4709 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_1, PARSER_CMD_SKIP_CFG_1);
4710 	WRITE_VREG(HEVC_PARSER_CMD_SKIP_2, PARSER_CMD_SKIP_CFG_2);
4711 
4712 
4713 		WRITE_VREG(HEVC_PARSER_IF_CONTROL,
4714 			/*  (1 << 8) |*/ /*sao_sw_pred_enable*/
4715 			(1 << 5) | /*parser_sao_if_en*/
4716 			(1 << 2) | /*parser_mpred_if_en*/
4717 			(1 << 0) /*parser_scaler_if_en*/
4718 		);
4719 	}
4720 
4721 	if (mask & HW_MASK_BACK) {
4722 		/*Changed to Start MPRED in microcode*/
4723 		/*
4724 		pr_info("[test.c] Start MPRED\n");
4725 		WRITE_VREG(HEVC_MPRED_INT_STATUS,
4726 		(1<<31)
4727 		);
4728 		*/
4729 		WRITE_VREG(HEVCD_IPP_TOP_CNTL,
4730 			(0 << 1) | /*enable ipp*/
4731 			(1 << 0)   /*software reset ipp and mpp*/
4732 		);
4733 #ifdef CHANGE_REMOVED
4734 		WRITE_VREG(HEVCD_IPP_TOP_CNTL,
4735 			(1 << 1) | /*enable ipp*/
4736 			(0 << 0)   /*software reset ipp and mpp*/
4737 		);
4738 #else
4739 		WRITE_VREG(HEVCD_IPP_TOP_CNTL,
4740 			(3 << 4) | // av1
4741 			(1 << 1) | /*enable ipp*/
4742 			(0 << 0)   /*software reset ipp and mpp*/
4743 		);
4744 #endif
4745 	if (get_double_write_mode(hw) & 0x10) {
4746 		/*Enable NV21 reference read mode for MC*/
4747 		WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
4748 	}
4749 #ifdef MCRCC_ENABLE
4750 		/*Initialize mcrcc and decomp perf counters*/
4751 		if (mcrcc_cache_alg_flag &&
4752 			hw->init_flag == 0) {
4753 			mcrcc_perfcount_reset(hw);
4754 			decomp_perfcount_reset(hw);
4755 		}
4756 #endif
4757 	}
4758 #ifdef CHANGE_REMOVED
4759 #else
4760 // Set MCR fetch priorities
4761     data32 = 0x1 | (0x1 << 2) | (0x1 <<3) |
4762 	(24 << 4) | (32 << 11) | (24 << 18) | (32 << 25);
4763     WRITE_VREG(HEVCD_MPP_DECOMP_AXIURG_CTL, data32);
4764 #endif
4765 	return;
4766 }
4767 
4768 
4769 #ifdef CONFIG_HEVC_CLK_FORCED_ON
config_av1_clk_forced_on(void)4770 static void config_av1_clk_forced_on(void)
4771 {
4772 	unsigned int rdata32;
4773 	/*IQIT*/
4774 	rdata32 = READ_VREG(HEVC_IQIT_CLK_RST_CTRL);
4775 	WRITE_VREG(HEVC_IQIT_CLK_RST_CTRL, rdata32 | (0x1 << 2));
4776 
4777 	/* DBLK*/
4778 	rdata32 = READ_VREG(HEVC_DBLK_CFG0);
4779 	WRITE_VREG(HEVC_DBLK_CFG0, rdata32 | (0x1 << 2));
4780 
4781 	/* SAO*/
4782 	rdata32 = READ_VREG(HEVC_SAO_CTRL1);
4783 	WRITE_VREG(HEVC_SAO_CTRL1, rdata32 | (0x1 << 2));
4784 
4785 	/*MPRED*/
4786 	rdata32 = READ_VREG(HEVC_MPRED_CTRL1);
4787 	WRITE_VREG(HEVC_MPRED_CTRL1, rdata32 | (0x1 << 24));
4788 
4789 	/* PARSER*/
4790 	rdata32 = READ_VREG(HEVC_STREAM_CONTROL);
4791 	WRITE_VREG(HEVC_STREAM_CONTROL, rdata32 | (0x1 << 15));
4792 	rdata32 = READ_VREG(HEVC_SHIFT_CONTROL);
4793 	WRITE_VREG(HEVC_SHIFT_CONTROL, rdata32 | (0x1 << 15));
4794 	rdata32 = READ_VREG(HEVC_CABAC_CONTROL);
4795 	WRITE_VREG(HEVC_CABAC_CONTROL, rdata32 | (0x1 << 13));
4796 	rdata32 = READ_VREG(HEVC_PARSER_CORE_CONTROL);
4797 	WRITE_VREG(HEVC_PARSER_CORE_CONTROL, rdata32 | (0x1 << 15));
4798 	rdata32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
4799 	WRITE_VREG(HEVC_PARSER_INT_CONTROL, rdata32 | (0x1 << 15));
4800 	rdata32 = READ_VREG(HEVC_PARSER_IF_CONTROL);
4801 	WRITE_VREG(HEVC_PARSER_IF_CONTROL,
4802 			rdata32 | (0x1 << 6) | (0x1 << 3) | (0x1 << 1));
4803 
4804 	/*IPP*/
4805 	rdata32 = READ_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG);
4806 	WRITE_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG, rdata32 | 0xffffffff);
4807 
4808 	/* MCRCC*/
4809 	rdata32 = READ_VREG(HEVCD_MCRCC_CTL1);
4810 	WRITE_VREG(HEVCD_MCRCC_CTL1, rdata32 | (0x1 << 3));
4811 }
4812 #endif
4813 
4814 
vav1_mmu_map_alloc(struct AV1HW_s * hw)4815 static int vav1_mmu_map_alloc(struct AV1HW_s *hw)
4816 {
4817 	if (hw->mmu_enable) {
4818 		u32 mmu_map_size = vav1_frame_mmu_map_size(hw);
4819 		hw->frame_mmu_map_addr =
4820 			dma_alloc_coherent(amports_get_dma_device(),
4821 				mmu_map_size,
4822 				&hw->frame_mmu_map_phy_addr, GFP_KERNEL);
4823 		if (hw->frame_mmu_map_addr == NULL) {
4824 			pr_err("%s: failed to alloc count_buffer\n", __func__);
4825 			return -1;
4826 		}
4827 		memset(hw->frame_mmu_map_addr, 0, mmu_map_size);
4828 	}
4829 #ifdef AOM_AV1_MMU_DW
4830 	if (hw->dw_mmu_enable) {
4831 		u32 mmu_map_size = vaom_dw_frame_mmu_map_size(hw);
4832 		hw->dw_frame_mmu_map_addr =
4833 			dma_alloc_coherent(amports_get_dma_device(),
4834 				mmu_map_size,
4835 				&hw->dw_frame_mmu_map_phy_addr, GFP_KERNEL);
4836 		if (hw->dw_frame_mmu_map_addr == NULL) {
4837 			pr_err("%s: failed to alloc count_buffer\n", __func__);
4838 			return -1;
4839 		}
4840 		memset(hw->dw_frame_mmu_map_addr, 0, mmu_map_size);
4841 	}
4842 #endif
4843 	return 0;
4844 }
4845 
4846 
vav1_mmu_map_free(struct AV1HW_s * hw)4847 static void vav1_mmu_map_free(struct AV1HW_s *hw)
4848 {
4849 	if (hw->mmu_enable) {
4850 		u32 mmu_map_size = vav1_frame_mmu_map_size(hw);
4851 		if (hw->frame_mmu_map_addr) {
4852 			if (hw->frame_mmu_map_phy_addr)
4853 				dma_free_coherent(amports_get_dma_device(),
4854 					mmu_map_size,
4855 					hw->frame_mmu_map_addr,
4856 					hw->frame_mmu_map_phy_addr);
4857 			hw->frame_mmu_map_addr = NULL;
4858 		}
4859 	}
4860 #ifdef AOM_AV1_MMU_DW
4861 	if (hw->dw_mmu_enable) {
4862 		u32 mmu_map_size = vaom_dw_frame_mmu_map_size(hw);
4863 		if (hw->dw_frame_mmu_map_addr) {
4864 			if (hw->dw_frame_mmu_map_phy_addr)
4865 				dma_free_coherent(amports_get_dma_device(),
4866 					mmu_map_size,
4867 					hw->dw_frame_mmu_map_addr,
4868 					hw->dw_frame_mmu_map_phy_addr);
4869 			hw->dw_frame_mmu_map_addr = NULL;
4870 		}
4871 	}
4872 #endif
4873 }
4874 
4875 
av1_local_uninit(struct AV1HW_s * hw)4876 static void av1_local_uninit(struct AV1HW_s *hw)
4877 {
4878 	hw->rpm_ptr = NULL;
4879 	hw->lmem_ptr = NULL;
4880 #ifdef DUMP_FILMGRAIN
4881 	hw->fg_ptr = NULL;
4882 	if (hw->fg_addr) {
4883 		if (hw->fg_phy_addr)
4884 			dma_free_coherent(amports_get_dma_device(),
4885 				FGS_TABLE_SIZE, hw->fg_addr,
4886 				hw->fg_phy_addr);
4887 		hw->fg_addr = NULL;
4888 	}
4889 #endif
4890 	if (hw->rpm_addr) {
4891 		dma_free_coherent(amports_get_dma_device(),
4892 					RPM_BUF_SIZE,
4893 					hw->rpm_addr,
4894 					hw->rpm_phy_addr);
4895 		hw->rpm_addr = NULL;
4896 	}
4897 	if (hw->aux_addr) {
4898 		dma_free_coherent(amports_get_dma_device(),
4899 				hw->prefix_aux_size + hw->suffix_aux_size, hw->aux_addr,
4900 					hw->aux_phy_addr);
4901 		hw->aux_addr = NULL;
4902 	}
4903 	if (hw->lmem_addr) {
4904 		if (hw->lmem_phy_addr)
4905 			dma_free_coherent(amports_get_dma_device(),
4906 				LMEM_BUF_SIZE, hw->lmem_addr,
4907 				hw->lmem_phy_addr);
4908 		hw->lmem_addr = NULL;
4909 	}
4910 	if (hw->prob_buffer_addr) {
4911 		if (hw->prob_buffer_phy_addr)
4912 			dma_free_coherent(amports_get_dma_device(),
4913 				PROB_BUF_SIZE, hw->prob_buffer_addr,
4914 				hw->prob_buffer_phy_addr);
4915 
4916 		hw->prob_buffer_addr = NULL;
4917 	}
4918 	if (hw->count_buffer_addr) {
4919 		if (hw->count_buffer_phy_addr)
4920 			dma_free_coherent(amports_get_dma_device(),
4921 				COUNT_BUF_SIZE, hw->count_buffer_addr,
4922 				hw->count_buffer_phy_addr);
4923 
4924 		hw->count_buffer_addr = NULL;
4925 	}
4926 
4927 	vav1_mmu_map_free(hw);
4928 
4929 	if (hw->gvs)
4930 		vfree(hw->gvs);
4931 	hw->gvs = NULL;
4932 }
4933 
av1_local_init(struct AV1HW_s * hw)4934 static int av1_local_init(struct AV1HW_s *hw)
4935 {
4936 	int ret = -1;
4937 	/*int losless_comp_header_size, losless_comp_body_size;*/
4938 
4939 	struct BuffInfo_s *cur_buf_info = NULL;
4940 
4941 	memset(&hw->param, 0, sizeof(union param_u));
4942 #ifdef MULTI_INSTANCE_SUPPORT
4943 	cur_buf_info = &hw->work_space_buf_store;
4944     hw->pbi->work_space_buf = cur_buf_info;
4945 
4946 	if (vdec_is_support_4k()) {
4947 		memcpy(cur_buf_info, &aom_workbuff_spec[1],	/* 8k */
4948 			sizeof(struct BuffInfo_s));
4949 	} else
4950 		memcpy(cur_buf_info, &aom_workbuff_spec[0],/* 1080p */
4951 		sizeof(struct BuffInfo_s));
4952 
4953 	cur_buf_info->start_adr = hw->buf_start;
4954 	if (!hw->mmu_enable)
4955 		hw->mc_buf_spec.buf_end = hw->buf_start + hw->buf_size;
4956 
4957 #else
4958 /*! MULTI_INSTANCE_SUPPORT*/
4959 	if (vdec_is_support_4k()) {
4960 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
4961 			cur_buf_info = &aom_workbuff_spec[1];/* 8k work space */
4962 		else
4963 			cur_buf_info = &aom_workbuff_spec[1];/* 4k2k work space */
4964 	} else
4965 		cur_buf_info = &aom_workbuff_spec[0];/* 1080p work space */
4966 
4967 #endif
4968 
4969 	init_buff_spec(hw, cur_buf_info);
4970 	aom_bufmgr_init(hw, cur_buf_info, NULL);
4971 
4972 	if (!vdec_is_support_4k()
4973 		&& (buf_alloc_width > 1920 &&  buf_alloc_height > 1088)) {
4974 		buf_alloc_width = 1920;
4975 		buf_alloc_height = 1088;
4976 		if (hw->max_pic_w > 1920 && hw->max_pic_h > 1088) {
4977 			hw->max_pic_w = 1920;
4978 			hw->max_pic_h = 1088;
4979 		}
4980 	} else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
4981 		buf_alloc_width = 8192;
4982 		buf_alloc_height = 4608;
4983 	}
4984 #if 0
4985 	hw->init_pic_w = hw->max_pic_w ? hw->max_pic_w :
4986 		(buf_alloc_width ? buf_alloc_width :
4987 		(hw->vav1_amstream_dec_info.width ?
4988 		hw->vav1_amstream_dec_info.width :
4989 		hw->work_space_buf->max_width));
4990 	hw->init_pic_h = hw->max_pic_h ? hw->max_pic_h :
4991 		(buf_alloc_height ? buf_alloc_height :
4992 		(hw->vav1_amstream_dec_info.height ?
4993 		hw->vav1_amstream_dec_info.height :
4994 		hw->work_space_buf->max_height));
4995 #else
4996 	hw->init_pic_w = hw->max_pic_w ? hw->max_pic_w :
4997 		(hw->vav1_amstream_dec_info.width ? hw->vav1_amstream_dec_info.width :
4998 		(buf_alloc_width ? buf_alloc_width : hw->work_space_buf->max_width));
4999 	hw->init_pic_h = hw->max_pic_h ? hw->max_pic_h :
5000 		(hw->vav1_amstream_dec_info.height ? hw->vav1_amstream_dec_info.height :
5001 		(buf_alloc_height ? buf_alloc_height : hw->work_space_buf->max_height));
5002 #endif
5003     hw->pbi->frame_width = hw->init_pic_w;
5004     hw->pbi->frame_height = hw->init_pic_h;
5005 
5006 	/* video is not support unaligned with 64 in tl1
5007 	** vdec canvas mode will be linear when dump yuv is set
5008 	*/
5009 	if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
5010 		(hw->double_write_mode != 0) &&
5011 		(((hw->max_pic_w % 64) != 0) ||
5012 		(hw->vav1_amstream_dec_info.width % 64) != 0)) {
5013 		if (hw_to_vdec(hw)->canvas_mode !=
5014 			CANVAS_BLKMODE_LINEAR)
5015 			mem_map_mode = 2;
5016 		else {
5017 			mem_map_mode = 0;
5018 			av1_print(hw, AOM_DEBUG_HW_MORE, "vdec blkmod linear, force mem_map_mode 0\n");
5019 		}
5020 	}
5021 
5022 #if 0
5023 //ndef MV_USE_FIXED_BUF
5024 	if (init_mv_buf_list(hw) < 0) {
5025 		pr_err("%s: init_mv_buf_list fail\n", __func__);
5026 		return -1;
5027 	}
5028 #endif
5029 	if (IS_8K_SIZE(hw->init_pic_w, hw->init_pic_h)) {
5030 		max_buf_num = MAX_BUF_NUM_LESS;
5031 		if (max_buf_num > REF_FRAMES_4K)
5032 			mv_buf_margin = max_buf_num - REF_FRAMES_4K + 1;
5033 	}
5034 	if (hw->save_buffer_mode)
5035 		hw->used_buf_num = MAX_BUF_NUM_SAVE_BUF;
5036 	else
5037 		hw->used_buf_num = max_buf_num;
5038 	if (hw->used_buf_num > MAX_BUF_NUM)
5039 		hw->used_buf_num = MAX_BUF_NUM;
5040 	if (hw->used_buf_num > FRAME_BUFFERS)
5041 		hw->used_buf_num = FRAME_BUFFERS;
5042 
5043 	hw->pts_unstable = ((unsigned long)(hw->vav1_amstream_dec_info.param)
5044 			& 0x40) >> 6;
5045 
5046 	if ((debug & AOM_AV1_DEBUG_SEND_PARAM_WITH_REG) == 0) {
5047 		hw->rpm_addr = dma_alloc_coherent(amports_get_dma_device(),
5048 				RPM_BUF_SIZE,
5049 				&hw->rpm_phy_addr, GFP_KERNEL);
5050 		if (hw->rpm_addr == NULL) {
5051 			pr_err("%s: failed to alloc rpm buffer\n", __func__);
5052 			return -1;
5053 		}
5054 		hw->rpm_ptr = hw->rpm_addr;
5055 	}
5056 
5057 	if (prefix_aux_buf_size > 0 ||
5058 		suffix_aux_buf_size > 0) {
5059 		u32 aux_buf_size;
5060 
5061 		hw->prefix_aux_size = AUX_BUF_ALIGN(prefix_aux_buf_size);
5062 		hw->suffix_aux_size = AUX_BUF_ALIGN(suffix_aux_buf_size);
5063 		aux_buf_size = hw->prefix_aux_size + hw->suffix_aux_size;
5064 		hw->aux_addr = dma_alloc_coherent(amports_get_dma_device(),
5065 				aux_buf_size, &hw->aux_phy_addr, GFP_KERNEL);
5066 		if (hw->aux_addr == NULL) {
5067 			pr_err("%s: failed to alloc rpm buffer\n", __func__);
5068 			goto dma_alloc_fail;
5069 		}
5070 	}
5071 #ifdef DUMP_FILMGRAIN
5072 	hw->fg_addr = dma_alloc_coherent(amports_get_dma_device(),
5073 			FGS_TABLE_SIZE,
5074 			&hw->fg_phy_addr, GFP_KERNEL);
5075 	if (hw->fg_addr == NULL) {
5076 		pr_err("%s: failed to alloc fg buffer\n", __func__);
5077 	}
5078 	hw->fg_ptr = hw->fg_addr;
5079 #endif
5080 	hw->lmem_addr = dma_alloc_coherent(amports_get_dma_device(),
5081 			LMEM_BUF_SIZE,
5082 			&hw->lmem_phy_addr, GFP_KERNEL);
5083 	if (hw->lmem_addr == NULL) {
5084 		pr_err("%s: failed to alloc lmem buffer\n", __func__);
5085 		goto dma_alloc_fail;
5086 	}
5087 	hw->lmem_ptr = hw->lmem_addr;
5088 
5089 	hw->prob_buffer_addr = dma_alloc_coherent(amports_get_dma_device(),
5090 				PROB_BUF_SIZE,
5091 				&hw->prob_buffer_phy_addr, GFP_KERNEL);
5092 	if (hw->prob_buffer_addr == NULL) {
5093 		pr_err("%s: failed to alloc prob_buffer\n", __func__);
5094 		goto dma_alloc_fail;
5095 	}
5096 	memset(hw->prob_buffer_addr, 0, PROB_BUF_SIZE);
5097 	hw->count_buffer_addr = dma_alloc_coherent(amports_get_dma_device(),
5098 				COUNT_BUF_SIZE,
5099 				&hw->count_buffer_phy_addr, GFP_KERNEL);
5100 	if (hw->count_buffer_addr == NULL) {
5101 		pr_err("%s: failed to alloc count_buffer\n", __func__);
5102 		goto dma_alloc_fail;
5103 	}
5104 	memset(hw->count_buffer_addr, 0, COUNT_BUF_SIZE);
5105 
5106 	ret = vav1_mmu_map_alloc(hw);
5107 	if (ret < 0)
5108 		goto dma_alloc_fail;
5109 
5110 	return ret;
5111 
5112 dma_alloc_fail:
5113 	av1_local_uninit(hw);
5114 	return -1;
5115 }
5116 
5117 
5118 #define spec2canvas(x)  \
5119 	(((x)->uv_canvas_index << 16) | \
5120 	 ((x)->uv_canvas_index << 8)  | \
5121 	 ((x)->y_canvas_index << 0))
5122 
5123 
set_canvas(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic_config)5124 static void set_canvas(struct AV1HW_s *hw,
5125 	struct PIC_BUFFER_CONFIG_s *pic_config)
5126 {
5127 	struct vdec_s *vdec = hw_to_vdec(hw);
5128 	int canvas_w = ALIGN(pic_config->y_crop_width, 64)/4;
5129 	int canvas_h = ALIGN(pic_config->y_crop_height, 32)/4;
5130 	int blkmode = mem_map_mode;
5131 	/*CANVAS_BLKMODE_64X32*/
5132 	if	(pic_config->double_write_mode) {
5133 		canvas_w = pic_config->y_crop_width	/
5134 				get_double_write_ratio(hw,
5135 					pic_config->double_write_mode);
5136 		canvas_h = pic_config->y_crop_height /
5137 				get_double_write_ratio(hw,
5138 					pic_config->double_write_mode);
5139 
5140 		if (mem_map_mode == 0)
5141 			canvas_w = ALIGN(canvas_w, 32);
5142 		else
5143 			canvas_w = ALIGN(canvas_w, 64);
5144 		canvas_h = ALIGN(canvas_h, 32);
5145 
5146 		if (vdec->parallel_dec == 1) {
5147 			if (pic_config->y_canvas_index == -1)
5148 				pic_config->y_canvas_index =
5149 					vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
5150 			if (pic_config->uv_canvas_index == -1)
5151 				pic_config->uv_canvas_index =
5152 					vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
5153 		} else {
5154 			pic_config->y_canvas_index = 128 + pic_config->index * 2;
5155 			pic_config->uv_canvas_index = 128 + pic_config->index * 2 + 1;
5156 		}
5157 
5158 		canvas_config_ex(pic_config->y_canvas_index,
5159 			pic_config->dw_y_adr, canvas_w, canvas_h,
5160 			CANVAS_ADDR_NOWRAP, blkmode, 0x7);
5161 		canvas_config_ex(pic_config->uv_canvas_index,
5162 			pic_config->dw_u_v_adr,	canvas_w, canvas_h,
5163 			CANVAS_ADDR_NOWRAP, blkmode, 0x7);
5164 
5165 #ifdef MULTI_INSTANCE_SUPPORT
5166 		pic_config->canvas_config[0].phy_addr =
5167 				pic_config->dw_y_adr;
5168 		pic_config->canvas_config[0].width =
5169 				canvas_w;
5170 		pic_config->canvas_config[0].height =
5171 				canvas_h;
5172 		pic_config->canvas_config[0].block_mode =
5173 				blkmode;
5174 		pic_config->canvas_config[0].endian = 7;
5175 
5176 		pic_config->canvas_config[1].phy_addr =
5177 				pic_config->dw_u_v_adr;
5178 		pic_config->canvas_config[1].width =
5179 				canvas_w;
5180 		pic_config->canvas_config[1].height =
5181 				canvas_h;
5182 		pic_config->canvas_config[1].block_mode =
5183 				blkmode;
5184 		pic_config->canvas_config[1].endian = 7;
5185 #endif
5186 	}
5187 }
5188 
set_frame_info(struct AV1HW_s * hw,struct vframe_s * vf)5189 static void set_frame_info(struct AV1HW_s *hw, struct vframe_s *vf)
5190 {
5191 	unsigned int ar;
5192 	vf->duration = hw->frame_dur;
5193 	vf->duration_pulldown = 0;
5194 	vf->flag = 0;
5195 	vf->prop.master_display_colour = hw->vf_dp;
5196 	vf->signal_type = hw->video_signal_type;
5197 	if (vf->compWidth && vf->compHeight)
5198 		hw->frame_ar = vf->compHeight * 0x100 / vf->compWidth;
5199 	ar = min_t(u32, hw->frame_ar, DISP_RATIO_ASPECT_RATIO_MAX);
5200 	vf->ratio_control = (ar << DISP_RATIO_ASPECT_RATIO_BIT);
5201 
5202 }
5203 
vav1_vf_states(struct vframe_states * states,void * op_arg)5204 static int vav1_vf_states(struct vframe_states *states, void *op_arg)
5205 {
5206 	struct AV1HW_s *hw = (struct AV1HW_s *)op_arg;
5207 
5208 	states->vf_pool_size = VF_POOL_SIZE;
5209 	states->buf_free_num = kfifo_len(&hw->newframe_q);
5210 	states->buf_avail_num = kfifo_len(&hw->display_q);
5211 
5212 	if (step == 2)
5213 		states->buf_avail_num = 0;
5214 	return 0;
5215 }
5216 
vav1_vf_peek(void * op_arg)5217 static struct vframe_s *vav1_vf_peek(void *op_arg)
5218 {
5219 	struct vframe_s *vf[2] = {0, 0};
5220 	struct AV1HW_s *hw = (struct AV1HW_s *)op_arg;
5221 
5222 	if (step == 2)
5223 		return NULL;
5224 
5225 	if (kfifo_out_peek(&hw->display_q, (void *)&vf, 2)) {
5226 		if (vf[1]) {
5227 			vf[0]->next_vf_pts_valid = true;
5228 			vf[0]->next_vf_pts = vf[1]->pts;
5229 		} else
5230 			vf[0]->next_vf_pts_valid = false;
5231 		return vf[0];
5232 	}
5233 
5234 	return NULL;
5235 }
5236 
vav1_vf_get(void * op_arg)5237 static struct vframe_s *vav1_vf_get(void *op_arg)
5238 {
5239 	struct vframe_s *vf;
5240 	struct AV1HW_s *hw = (struct AV1HW_s *)op_arg;
5241 
5242 	if (step == 2)
5243 		return NULL;
5244 	else if (step == 1)
5245 			step = 2;
5246 
5247 	if (kfifo_get(&hw->display_q, &vf)) {
5248 		struct vframe_s *next_vf;
5249 		uint8_t index = vf->index & 0xff;
5250 		if (index < hw->used_buf_num) {
5251 			hw->vf_get_count++;
5252 			if (debug & AOM_DEBUG_VFRAME) {
5253 				struct BufferPool_s *pool = hw->pbi->common.buffer_pool;
5254 				struct PIC_BUFFER_CONFIG_s *pic =
5255 					&pool->frame_bufs[index].buf;
5256 				unsigned long flags;
5257 				lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5258 				av1_print(hw, AOM_DEBUG_VFRAME, "%s index 0x%x type 0x%x w/h %d/%d, aux size %d, pts %d, %lld\n",
5259 					__func__, vf->index, vf->type,
5260 					vf->width, vf->height,
5261 					pic->aux_data_size,
5262 					vf->pts,
5263 					vf->pts_us64);
5264 				unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5265 			}
5266 
5267 			if (kfifo_peek(&hw->display_q, &next_vf)) {
5268 				vf->next_vf_pts_valid = true;
5269 				vf->next_vf_pts = next_vf->pts;
5270 			} else
5271 				vf->next_vf_pts_valid = false;
5272 #ifdef DUMP_FILMGRAIN
5273 			if (index == fg_dump_index) {
5274 				unsigned long flags;
5275 				int ii;
5276 				lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5277 				pr_info("FGS_TABLE for buffer %d:\n", index);
5278 				for (ii = 0; ii < FGS_TABLE_SIZE; ii++) {
5279 			        pr_info("%02x ", hw->fg_ptr[ii]);
5280 	        		if (((ii+ 1) & 0xf) == 0)
5281 						pr_info("\n");
5282 	    		}
5283 				unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5284 			}
5285 #endif
5286 
5287 			return vf;
5288 		}
5289 	}
5290 	return NULL;
5291 }
5292 
vav1_vf_put(struct vframe_s * vf,void * op_arg)5293 static void vav1_vf_put(struct vframe_s *vf, void *op_arg)
5294 {
5295 	struct AV1HW_s *hw = (struct AV1HW_s *)op_arg;
5296 	uint8_t index = vf->index & 0xff;
5297 	unsigned long flags;
5298 
5299 	if ((vf == NULL) || (hw == NULL))
5300 		return;
5301 
5302 	kfifo_put(&hw->newframe_q, (const struct vframe_s *)vf);
5303 	hw->vf_put_count++;
5304 	if (debug & AOM_DEBUG_VFRAME) {
5305 		lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5306 		av1_print(hw, AOM_DEBUG_VFRAME, "%s index 0x%x type 0x%x w/h %d/%d, pts %d, %lld\n",
5307 			__func__, vf->index, vf->type,
5308 			vf->width, vf->height,
5309 			vf->pts,
5310 			vf->pts_us64);
5311 		unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5312 	}
5313 
5314 	if (index < hw->used_buf_num) {
5315 		struct AV1_Common_s *cm = &hw->pbi->common;
5316 		struct BufferPool_s *pool = cm->buffer_pool;
5317 
5318 		lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5319 		if ((debug & AV1_DEBUG_IGNORE_VF_REF) == 0) {
5320 			if (pool->frame_bufs[index].buf.vf_ref > 0)
5321 				pool->frame_bufs[index].buf.vf_ref--;
5322 		}
5323 		if (hw->wait_buf)
5324 			WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG,
5325 						0x1);
5326 		hw->last_put_idx = index;
5327 		hw->new_frame_displayed++;
5328 		unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5329 	}
5330 
5331 }
5332 
vav1_event_cb(int type,void * data,void * op_arg)5333 static int vav1_event_cb(int type, void *data, void *op_arg)
5334 {
5335 	unsigned long flags;
5336 	struct AV1HW_s *hw = (struct AV1HW_s *)op_arg;
5337 	struct AV1_Common_s *cm = &hw->pbi->common;
5338 	struct BufferPool_s *pool = cm->buffer_pool;
5339 
5340 	if (type & VFRAME_EVENT_RECEIVER_RESET) {
5341 #if 0
5342 		unsigned long flags;
5343 
5344 		amhevc_stop();
5345 #ifndef CONFIG_AMLOGIC_POST_PROCESS_MANAGER
5346 		vf_light_unreg_provider(&vav1_vf_prov);
5347 #endif
5348 		spin_lock_irqsave(&hw->lock, flags);
5349 		vav1_local_init();
5350 		vav1_prot_init();
5351 		spin_unlock_irqrestore(&hw->lock, flags);
5352 #ifndef CONFIG_AMLOGIC_POST_PROCESS_MANAGER
5353 		vf_reg_provider(&vav1_vf_prov);
5354 #endif
5355 		amhevc_start();
5356 #endif
5357 	} else if (type & VFRAME_EVENT_RECEIVER_GET_AUX_DATA) {
5358 		struct provider_aux_req_s *req =
5359 			(struct provider_aux_req_s *)data;
5360 		unsigned char index;
5361 
5362 		lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5363 		index = req->vf->index & 0xff;
5364 		req->aux_buf = NULL;
5365 		req->aux_size = 0;
5366 		if (req->bot_flag)
5367 			index = (req->vf->index >> 8) & 0xff;
5368 		if (index != 0xff
5369 			&& index < hw->used_buf_num) {
5370 			struct PIC_BUFFER_CONFIG_s *pic_config =
5371 				&pool->frame_bufs[index].buf;
5372 			req->aux_buf = pic_config->aux_data_buf;
5373 			req->aux_size = pic_config->aux_data_size;
5374 #if 0
5375 //def CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
5376 			if (hw->bypass_dvenl && !dolby_meta_with_el)
5377 				req->dv_enhance_exist = false;
5378 			else
5379 				req->dv_enhance_exist =
5380 					pic_config->dv_enhance_exist;
5381 			av1_print(hw, AOM_DEBUG_VFRAME,
5382 			"query dv_enhance_exist for pic (vf 0x%p, poc %d index %d) flag => %d, aux sizd 0x%x\n",
5383 			req->vf,
5384 			pic_config->POC, index,
5385 			req->dv_enhance_exist, req->aux_size);
5386 #else
5387 			req->dv_enhance_exist = 0;
5388 #endif
5389 		}
5390 		unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
5391 
5392 		if (debug & AOM_DEBUG_AUX_DATA)
5393 			av1_print(hw, 0,
5394 			"%s(type 0x%x vf index 0x%x)=>size 0x%x\n",
5395 			__func__, type, index, req->aux_size);
5396 #if 0
5397 //def CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
5398 	} else if (type & VFRAME_EVENT_RECEIVER_DOLBY_BYPASS_EL) {
5399 		if ((force_bypass_dvenl & 0x80000000) == 0) {
5400 			av1_print(hw, 0,
5401 			"%s: VFRAME_EVENT_RECEIVER_DOLBY_BYPASS_EL\n",
5402 			__func__);
5403 			hw->bypass_dvenl_enable = 1;
5404 		}
5405 
5406 #endif
5407 	}
5408 	return 0;
5409 }
5410 
av1_inc_vf_ref(struct AV1HW_s * hw,int index)5411 void av1_inc_vf_ref(struct AV1HW_s *hw, int index)
5412 {
5413 	struct AV1_Common_s *cm = &hw->pbi->common;
5414 
5415 	if ((debug & AV1_DEBUG_IGNORE_VF_REF) == 0) {
5416 		cm->buffer_pool->frame_bufs[index].buf.vf_ref++;
5417 
5418 		av1_print(hw, AV1_DEBUG_BUFMGR_MORE, "%s index = %d new vf_ref = %d\r\n",
5419 			__func__, index,
5420 			cm->buffer_pool->frame_bufs[index].buf.vf_ref);
5421 	}
5422 }
5423 
frame_duration_adapt(struct AV1HW_s * hw,struct vframe_s * vf,u32 valid)5424 static int frame_duration_adapt(struct AV1HW_s *hw, struct vframe_s *vf, u32 valid)
5425 {
5426 	u32 old_duration, pts_duration = 0;
5427 	u32 pts = vf->pts;
5428 
5429 	if (hw->get_frame_dur == true)
5430 		return true;
5431 
5432 	hw->frame_cnt_window++;
5433 	if (!(hw->av1_first_pts_ready == 1)) {
5434 		if (valid) {
5435 			hw->pts1 = pts;
5436 			hw->frame_cnt_window = 0;
5437 			hw->duration_from_pts_done = 0;
5438 			hw->av1_first_pts_ready = 1;
5439 		} else {
5440 			return false;
5441 		}
5442 	} else {
5443 		if (pts < hw->pts1) {
5444 			if (hw->frame_cnt_window > FRAME_CNT_WINDOW_SIZE) {
5445 				hw->pts1 = pts;
5446 				hw->frame_cnt_window = 0;
5447 			}
5448 		}
5449 
5450 		if (valid && (hw->frame_cnt_window > FRAME_CNT_WINDOW_SIZE) &&
5451 			(pts > hw->pts1) && (hw->duration_from_pts_done == 0)) {
5452 				old_duration = hw->frame_dur;
5453 				hw->pts2 = pts;
5454 				pts_duration = (((hw->pts2 - hw->pts1) * 16) /
5455 					(hw->frame_cnt_window * 15));
5456 
5457 			if (close_to(pts_duration, old_duration, 2000)) {
5458 				hw->frame_dur = pts_duration;
5459 				av1_print(hw, AV1_DEBUG_OUT_PTS,
5460 					"use calc duration %d\n", pts_duration);
5461 			}
5462 
5463 			if (hw->duration_from_pts_done == 0) {
5464 				if (close_to(pts_duration, old_duration, RATE_CORRECTION_THRESHOLD)) {
5465 					hw->duration_from_pts_done = 1;
5466 				} else {
5467 					if (!close_to(pts_duration,
5468 						 old_duration, 1000) &&
5469 						!close_to(pts_duration,
5470 						hw->frame_dur, 1000) &&
5471 						close_to(pts_duration,
5472 						hw->last_duration, 200)) {
5473 						/* frame_dur must
5474 						 *  wrong,recover it.
5475 						 */
5476 						hw->frame_dur = pts_duration;
5477 					}
5478 					hw->pts1 = hw->pts2;
5479 					hw->frame_cnt_window = 0;
5480 					hw->duration_from_pts_done = 0;
5481 				}
5482 			}
5483 			hw->last_duration = pts_duration;
5484 		}
5485 	}
5486 	return true;
5487 }
5488 
update_vf_memhandle(struct AV1HW_s * hw,struct vframe_s * vf,struct PIC_BUFFER_CONFIG_s * pic)5489 static void update_vf_memhandle(struct AV1HW_s *hw,
5490 	struct vframe_s *vf, struct PIC_BUFFER_CONFIG_s *pic)
5491 {
5492 	if (pic->index < 0) {
5493 		vf->mem_handle = NULL;
5494 		vf->mem_head_handle = NULL;
5495 		vf->mem_dw_handle = NULL;
5496 	} else if (vf->type & VIDTYPE_SCATTER) {
5497 #ifdef AOM_AV1_MMU_DW
5498 		if (pic->double_write_mode & 0x20 &&
5499 			(debug & AOM_DEBUG_DW_DISP_MAIN) == 0) {
5500 			vf->mem_handle =
5501 				decoder_mmu_box_get_mem_handle(
5502 					hw->mmu_box_dw, pic->index);
5503 			vf->mem_head_handle =
5504 				decoder_bmmu_box_get_mem_handle(
5505 					hw->bmmu_box,
5506 					DW_HEADER_BUFFER_IDX(pic->BUF_index));
5507 			vf->mem_dw_handle = NULL;
5508 		} else
5509 #endif
5510 		{
5511 		vf->mem_handle =
5512 			decoder_mmu_box_get_mem_handle(
5513 				hw->mmu_box, pic->index);
5514 		vf->mem_head_handle =
5515 			decoder_bmmu_box_get_mem_handle(
5516 				hw->bmmu_box,
5517 				HEADER_BUFFER_IDX(pic->BUF_index));
5518 		if (hw->double_write_mode == 3)
5519 			vf->mem_dw_handle =
5520 				decoder_bmmu_box_get_mem_handle(
5521 					hw->bmmu_box,
5522 					VF_BUFFER_IDX(pic->BUF_index));
5523 		else
5524 			vf->mem_dw_handle = NULL;
5525 		}
5526 #ifdef USE_SPEC_BUF_FOR_MMU_HEAD
5527 		vf->mem_head_handle = NULL;
5528 #endif
5529 	} else {
5530 		vf->mem_handle =
5531 			decoder_bmmu_box_get_mem_handle(
5532 				hw->bmmu_box, VF_BUFFER_IDX(pic->BUF_index));
5533 		vf->mem_head_handle = NULL;
5534 		vf->mem_dw_handle = NULL;
5535 		/*vf->mem_head_handle =
5536 		 *decoder_bmmu_box_get_mem_handle(
5537 		 *hw->bmmu_box, VF_BUFFER_IDX(BUF_index));
5538 		 */
5539 	}
5540 }
5541 
prepare_display_buf(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * pic_config)5542 static int prepare_display_buf(struct AV1HW_s *hw,
5543 				struct PIC_BUFFER_CONFIG_s *pic_config)
5544 {
5545 	struct vframe_s *vf = NULL;
5546 	int stream_offset = pic_config->stream_offset;
5547 	unsigned short slice_type = pic_config->slice_type;
5548 	u32 pts_valid = 0, pts_us64_valid = 0;
5549 	u32 pts_save;
5550 	u64 pts_us64_save;
5551 	u32 frame_size;
5552 	int i;
5553 
5554 	av1_print(hw, AOM_DEBUG_VFRAME, "%s index = %d\r\n", __func__, pic_config->index);
5555 	if (kfifo_get(&hw->newframe_q, &vf) == 0) {
5556 		av1_print(hw, 0, "fatal error, no available buffer slot.");
5557 		return -1;
5558 	}
5559 
5560 	if (pic_config->double_write_mode &&
5561 		(pic_config->double_write_mode & 0x20) == 0)
5562 		set_canvas(hw, pic_config);
5563 
5564 	display_frame_count[hw->index]++;
5565 	if (vf) {
5566 		if (!force_pts_unstable) {
5567 			if ((pic_config->pts == 0) || (pic_config->pts <= hw->last_pts)) {
5568 				for (i = (FRAME_BUFFERS - 1); i > 0; i--) {
5569 					if ((hw->last_pts == hw->frame_mode_pts_save[i]) ||
5570 						(hw->last_pts_us64 == hw->frame_mode_pts64_save[i])) {
5571 						pic_config->pts = hw->frame_mode_pts_save[i - 1];
5572 						pic_config->pts64 = hw->frame_mode_pts64_save[i - 1];
5573 						break;
5574 					}
5575 				}
5576 				if ((i == 0) || (pic_config->pts <= hw->last_pts)) {
5577 					av1_print(hw, AV1_DEBUG_OUT_PTS,
5578 						"no found pts %d, set 0. %d, %d\n",
5579 						i, pic_config->pts, hw->last_pts);
5580 					pic_config->pts = 0;
5581 					pic_config->pts64 = 0;
5582 				}
5583 			}
5584 		}
5585 		if (hw->is_used_v4l) {
5586 			vf->v4l_mem_handle
5587 				= hw->m_BUF[pic_config->BUF_index].v4l_ref_buf_addr;
5588 			av1_print(hw, PRINT_FLAG_V4L_DETAIL,
5589 				"[%d] %s(), v4l mem handle: 0x%lx\n",
5590 				((struct aml_vcodec_ctx *)(hw->v4l2_ctx))->id,
5591 				__func__, vf->v4l_mem_handle);
5592 		}
5593 
5594 #ifdef MULTI_INSTANCE_SUPPORT
5595 		if (vdec_frame_based(hw_to_vdec(hw))) {
5596 			vf->pts = pic_config->pts;
5597 			vf->pts_us64 = pic_config->pts64;
5598 			if (vf->pts != 0 || vf->pts_us64 != 0) {
5599 				pts_valid = 1;
5600 				pts_us64_valid = 1;
5601 			} else {
5602 				pts_valid = 0;
5603 				pts_us64_valid = 0;
5604 			}
5605 		} else
5606 #endif
5607 		/* if (pts_lookup_offset(PTS_TYPE_VIDEO,
5608 		 *   stream_offset, &vf->pts, 0) != 0) {
5609 		 */
5610 		if (pts_lookup_offset_us64
5611 			(PTS_TYPE_VIDEO, stream_offset, &vf->pts,
5612 			&frame_size, 0,
5613 			&vf->pts_us64) != 0) {
5614 #ifdef DEBUG_PTS
5615 			hw->pts_missed++;
5616 #endif
5617 			vf->pts = 0;
5618 			vf->pts_us64 = 0;
5619 			pts_valid = 0;
5620 			pts_us64_valid = 0;
5621 		} else {
5622 #ifdef DEBUG_PTS
5623 			hw->pts_hit++;
5624 #endif
5625 			pts_valid = 1;
5626 			pts_us64_valid = 1;
5627 		}
5628 		/*
5629 		if (vdec_frame_based(hw_to_vdec(hw)) && (!hw->get_frame_dur) && hw->last_lookup_pts_us64 && hw->last_pts
5630 			&& ((vf->pts_us64 > hw->last_lookup_pts_us64)  ||(vf->pts > hw->last_pts) )) {
5631 			 if (vf->pts > hw->last_pts)
5632 				hw->frame_dur =   PTS2DUR(vf->pts -hw->last_pts);
5633 			av1_print(hw, 0,
5634 				"AV1 frame mode  dur=%d,pts(%d,%lld) last pts(%d,%lld)\n",
5635 				hw->frame_dur, vf->pts,
5636 			vf->pts_us64, hw->last_pts, hw->last_lookup_pts_us64);
5637 			hw->get_frame_dur = true;
5638 			hw->pts_mode = PTS_NONE_REF_USE_DURATION;
5639 			hw->duration_from_pts_done = 1;
5640 		}
5641 		*/
5642 		fill_frame_info(hw, pic_config, frame_size, vf->pts);
5643 
5644 		pts_save = vf->pts;
5645 		pts_us64_save = vf->pts_us64;
5646 		if (hw->pts_unstable) {
5647 			frame_duration_adapt(hw, vf, pts_valid);
5648 			if (hw->duration_from_pts_done) {
5649 				hw->pts_mode = PTS_NONE_REF_USE_DURATION;
5650 			} else {
5651 				if (pts_valid || pts_us64_valid)
5652 					hw->pts_mode = PTS_NORMAL;
5653 			}
5654 		}
5655 
5656 		if ((hw->pts_mode == PTS_NORMAL) && (vf->pts != 0)
5657 			&& hw->get_frame_dur) {
5658 			int pts_diff = (int)vf->pts - hw->last_lookup_pts;
5659 
5660 			if (pts_diff < 0) {
5661 				hw->pts_mode_switching_count++;
5662 				hw->pts_mode_recovery_count = 0;
5663 
5664 				if (hw->pts_mode_switching_count >=
5665 					PTS_MODE_SWITCHING_THRESHOLD) {
5666 					hw->pts_mode =
5667 						PTS_NONE_REF_USE_DURATION;
5668 					pr_info
5669 					("HEVC: switch to n_d mode.\n");
5670 				}
5671 
5672 			} else {
5673 				int p = PTS_MODE_SWITCHING_RECOVERY_THREASHOLD;
5674 
5675 				hw->pts_mode_recovery_count++;
5676 				if (hw->pts_mode_recovery_count > p) {
5677 					hw->pts_mode_switching_count = 0;
5678 					hw->pts_mode_recovery_count = 0;
5679 				}
5680 			}
5681 		}
5682 
5683 		if (vf->pts != 0)
5684 			hw->last_lookup_pts = vf->pts;
5685 		if (hw->frame_dur && ((vf->pts == 0) || (vf->pts_us64 == 0))) {
5686 			vf->pts = hw->last_pts + DUR2PTS(hw->frame_dur);
5687 			vf->pts_us64 = hw->last_pts_us64 +
5688 				(DUR2PTS(hw->frame_dur) * 100 / 9);
5689 		}
5690 
5691 		if ((hw->pts_mode == PTS_NONE_REF_USE_DURATION)
5692 			&& (slice_type != KEY_FRAME))
5693 			vf->pts = hw->last_pts + DUR2PTS(hw->frame_dur);
5694 		hw->last_pts = vf->pts;
5695 
5696 		if (vf->pts_us64 != 0)
5697 			hw->last_lookup_pts_us64 = vf->pts_us64;
5698 
5699 		if ((hw->pts_mode == PTS_NONE_REF_USE_DURATION)
5700 			&& (slice_type != KEY_FRAME)) {
5701 			vf->pts_us64 =
5702 				hw->last_pts_us64 +
5703 				(DUR2PTS(hw->frame_dur) * 100 / 9);
5704 		}
5705 		hw->last_pts_us64 = vf->pts_us64;
5706 
5707 		av1_print(hw, AV1_DEBUG_OUT_PTS,
5708 			"AV1 dec out slice_type %d pts: pts_mode=%d,dur=%d,pts(%d, %lld)(%d, %lld)\n",
5709 			slice_type, hw->pts_mode, hw->frame_dur, vf->pts,
5710 			vf->pts_us64, pts_save, pts_us64_save);
5711 
5712 
5713 		if (hw->pts_mode == PTS_NONE_REF_USE_DURATION) {
5714 			vf->disp_pts = vf->pts;
5715 			vf->disp_pts_us64 = vf->pts_us64;
5716 			vf->pts = pts_save;
5717 			vf->pts_us64 = pts_us64_save;
5718 		} else {
5719 			vf->disp_pts = 0;
5720 			vf->disp_pts_us64 = 0;
5721 		}
5722 
5723 		vf->index = 0xff00 | pic_config->index;
5724 
5725 		if (pic_config->double_write_mode & 0x10) {
5726 			/* double write only */
5727 			vf->compBodyAddr = 0;
5728 			vf->compHeadAddr = 0;
5729 #ifdef AOM_AV1_MMU_DW
5730 			vf->dwBodyAddr = 0;
5731 			vf->dwHeadAddr = 0;
5732 #endif
5733 		} else {
5734 			if (hw->mmu_enable) {
5735 				vf->compBodyAddr = 0;
5736 				vf->compHeadAddr = pic_config->header_adr;
5737 				vf->fgs_table_adr = pic_config->fgs_table_adr;
5738 				vf->fgs_valid = hw->fgs_valid;
5739 #ifdef AOM_AV1_MMU_DW
5740 				vf->dwBodyAddr = 0;
5741 				vf->dwHeadAddr = 0;
5742 				if (pic_config->double_write_mode & 0x20) {
5743 					u32 mode = pic_config->double_write_mode & 0xf;
5744 					if (mode == 5 || mode == 3)
5745 						vf->dwHeadAddr = pic_config->header_dw_adr;
5746 					else if ((mode == 1 || mode == 2 || mode == 4)
5747 					&& (debug & AOM_DEBUG_DW_DISP_MAIN) == 0) {
5748 					vf->compHeadAddr = pic_config->header_dw_adr;
5749 					vf->fgs_valid = 0;
5750 					av1_print(hw, AOM_DEBUG_VFRAME,
5751 						"Use dw mmu for display\n");
5752 					}
5753 				}
5754 #endif
5755 			} else {
5756 				/*vf->compBodyAddr = pic_config->mc_y_adr;
5757 				 *vf->compHeadAddr = pic_config->mc_y_adr +
5758 				 *pic_config->comp_body_size; */
5759 				/*head adr*/
5760 			}
5761 			vf->canvas0Addr = vf->canvas1Addr = 0;
5762 		}
5763 		if (pic_config->double_write_mode &&
5764 			(pic_config->double_write_mode & 0x20) == 0) {
5765 			vf->type = VIDTYPE_PROGRESSIVE |
5766 				VIDTYPE_VIU_FIELD;
5767 			vf->type |= VIDTYPE_VIU_NV21;
5768 			if ((pic_config->double_write_mode == 3 ||
5769 				pic_config->double_write_mode == 5) &&
5770 				(!IS_8K_SIZE(pic_config->y_crop_width,
5771 				pic_config->y_crop_height))) {
5772 				vf->type |= VIDTYPE_COMPRESS;
5773 				if (hw->mmu_enable)
5774 					vf->type |= VIDTYPE_SCATTER;
5775 			}
5776 #ifdef MULTI_INSTANCE_SUPPORT
5777 			if (hw->m_ins_flag) {
5778 				vf->canvas0Addr = vf->canvas1Addr = -1;
5779 				vf->plane_num = 2;
5780 				vf->canvas0_config[0] =
5781 					pic_config->canvas_config[0];
5782 				vf->canvas0_config[1] =
5783 					pic_config->canvas_config[1];
5784 				vf->canvas1_config[0] =
5785 					pic_config->canvas_config[0];
5786 				vf->canvas1_config[1] =
5787 					pic_config->canvas_config[1];
5788 
5789 			} else
5790 #endif
5791 				vf->canvas0Addr = vf->canvas1Addr =
5792 					spec2canvas(pic_config);
5793 		} else {
5794 			vf->canvas0Addr = vf->canvas1Addr = 0;
5795 			vf->type = VIDTYPE_COMPRESS | VIDTYPE_VIU_FIELD;
5796 			if (hw->mmu_enable)
5797 				vf->type |= VIDTYPE_SCATTER;
5798 		}
5799 
5800 		switch (pic_config->bit_depth) {
5801 		case AOM_BITS_8:
5802 			vf->bitdepth = BITDEPTH_Y8 |
5803 				BITDEPTH_U8 | BITDEPTH_V8;
5804 			break;
5805 		case AOM_BITS_10:
5806 		case AOM_BITS_12:
5807 			vf->bitdepth = BITDEPTH_Y10 |
5808 				BITDEPTH_U10 | BITDEPTH_V10;
5809 			break;
5810 		default:
5811 			vf->bitdepth = BITDEPTH_Y10 |
5812 				BITDEPTH_U10 | BITDEPTH_V10;
5813 			break;
5814 		}
5815 		if ((vf->type & VIDTYPE_COMPRESS) == 0)
5816 			vf->bitdepth =
5817 				BITDEPTH_Y8 | BITDEPTH_U8 | BITDEPTH_V8;
5818 		if (pic_config->bit_depth == AOM_BITS_8)
5819 			vf->bitdepth |= BITDEPTH_SAVING_MODE;
5820 
5821 		/* if ((vf->width!=pic_config->width)|
5822 		 *	(vf->height!=pic_config->height))
5823 		 */
5824 		/* pr_info("aaa: %d/%d, %d/%d\n",
5825 		   vf->width,vf->height, pic_config->width,
5826 			pic_config->height); */
5827 		vf->width = pic_config->y_crop_width /
5828 			get_double_write_ratio(hw,
5829 				pic_config->double_write_mode);
5830 		vf->height = pic_config->y_crop_height /
5831 			get_double_write_ratio(hw,
5832 				pic_config->double_write_mode);
5833 		if (force_w_h != 0) {
5834 			vf->width = (force_w_h >> 16) & 0xffff;
5835 			vf->height = force_w_h & 0xffff;
5836 		}
5837 		if ((pic_config->double_write_mode & 0x20) &&
5838 			((pic_config->double_write_mode & 0xf) == 2 ||
5839 			(pic_config->double_write_mode & 0xf) == 4)) {
5840 			vf->compWidth = pic_config->y_crop_width /
5841 				get_double_write_ratio(hw,
5842 					pic_config->double_write_mode);
5843 			vf->compHeight = pic_config->y_crop_height /
5844 				get_double_write_ratio(hw,
5845 					pic_config->double_write_mode);
5846 		} else {
5847 			vf->compWidth = pic_config->y_crop_width;
5848 			vf->compHeight = pic_config->y_crop_height;
5849 		}
5850 		set_frame_info(hw, vf);
5851 		if (force_fps & 0x100) {
5852 			u32 rate = force_fps & 0xff;
5853 
5854 			if (rate)
5855 				vf->duration = 96000/rate;
5856 			else
5857 				vf->duration = 0;
5858 		}
5859 		update_vf_memhandle(hw, vf, pic_config);
5860 
5861 		av1_inc_vf_ref(hw, pic_config->index);
5862 		decoder_do_frame_check(hw_to_vdec(hw), vf);
5863 		kfifo_put(&hw->display_q, (const struct vframe_s *)vf);
5864 		ATRACE_COUNTER(MODULE_NAME, vf->pts);
5865 		hw->vf_pre_count++;
5866 #ifndef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
5867 		/*count info*/
5868 		gvs->frame_dur = hw->frame_dur;
5869 		vdec_count_info(gvs, 0, stream_offset);
5870 #endif
5871 		hw_to_vdec(hw)->vdec_fps_detec(hw_to_vdec(hw)->id);
5872 		if (without_display_mode == 0) {
5873 			vf_notify_receiver(hw->provider_name,
5874 				VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL);
5875 		} else
5876 			vav1_vf_put(vav1_vf_get(hw), hw);
5877 	}
5878 
5879 	return 0;
5880 }
5881 
av1_raw_write_image(AV1Decoder * pbi,PIC_BUFFER_CONFIG * sd)5882 void av1_raw_write_image(AV1Decoder *pbi, PIC_BUFFER_CONFIG *sd)
5883 {
5884 	sd->stream_offset = pbi->pre_stream_offset;
5885 	prepare_display_buf((struct AV1HW_s *)(pbi->private_data), sd);
5886 	pbi->pre_stream_offset = READ_VREG(HEVC_SHIFT_BYTE_COUNT);
5887 }
5888 
notify_v4l_eos(struct vdec_s * vdec)5889 static int notify_v4l_eos(struct vdec_s *vdec)
5890 {
5891 	struct AV1HW_s *hw = (struct AV1HW_s *)vdec->private;
5892 	struct aml_vcodec_ctx *ctx = (struct aml_vcodec_ctx *)(hw->v4l2_ctx);
5893 	struct vframe_s *vf = NULL;
5894 	struct vdec_fb *fb = NULL;
5895 
5896 	if (hw->is_used_v4l && hw->eos) {
5897 		if (kfifo_get(&hw->newframe_q, &vf) == 0 || vf == NULL) {
5898 			av1_print(hw, 0,
5899 				"%s fatal error, no available buffer slot.\n",
5900 				__func__);
5901 			return -1;
5902 		}
5903 
5904 		if (v4l_get_fb(hw->v4l2_ctx, &fb)) {
5905 			pr_err("[%d] get fb fail.\n", ctx->id);
5906 			return -1;
5907 		}
5908 
5909 		vf->timestamp = ULONG_MAX;
5910 		vf->v4l_mem_handle = (unsigned long)fb;
5911 		vf->flag = VFRAME_FLAG_EMPTY_FRAME_V4L;
5912 
5913 		kfifo_put(&hw->display_q, (const struct vframe_s *)vf);
5914 		vf_notify_receiver(vdec->vf_provider_name,
5915 			VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL);
5916 
5917 		av1_print(hw, AOM_DEBUG_HW_MORE, "[%d] AV1 EOS notify.\n", ctx->id);
5918 	}
5919 
5920 	return 0;
5921 }
5922 
get_rpm_param(union param_u * params)5923 static void get_rpm_param(union param_u *params)
5924 {
5925 	int i;
5926 	unsigned int data32;
5927 
5928 	if (debug & AV1_DEBUG_BUFMGR)
5929 		pr_info("enter %s\r\n", __func__);
5930 	for (i = 0; i < 128; i++) {
5931 		do {
5932 			data32 = READ_VREG(RPM_CMD_REG);
5933 			/*pr_info("%x\n", data32);*/
5934 		} while ((data32 & 0x10000) == 0);
5935 		params->l.data[i] = data32&0xffff;
5936 		/*pr_info("%x\n", data32);*/
5937 		WRITE_VREG(RPM_CMD_REG, 0);
5938 	}
5939 	if (debug & AV1_DEBUG_BUFMGR)
5940 		pr_info("leave %s\r\n", __func__);
5941 }
5942 
5943 
dec_again_process(struct AV1HW_s * hw)5944 static void dec_again_process(struct AV1HW_s *hw)
5945 {
5946 #ifdef CHANGE_REMOVED
5947 	amhevc_stop();
5948 	hw->dec_result = DEC_RESULT_AGAIN;
5949 	if (hw->process_state ==
5950 		PROC_STATE_DECODESLICE) {
5951 		hw->process_state =
5952 		PROC_STATE_SENDAGAIN;
5953 		if (hw->mmu_enable)
5954 			av1_recycle_mmu_buf(hw);
5955 	}
5956 	reset_process_time(hw);
5957 	vdec_schedule_work(&hw->work);
5958 #endif
5959 }
5960 
read_film_grain_reg(struct AV1HW_s * hw)5961 static void read_film_grain_reg(struct AV1HW_s *hw)
5962 {
5963     AV1_COMMON *cm = &hw->pbi->common;
5964     int i;
5965     if (cm->cur_frame == NULL) {
5966 	    av1_print(hw, AOM_DEBUG_HW_MORE, "%s, cur_frame not exist!!!\n", __func__);
5967 	    return;
5968 	} else
5969 	    av1_print(hw, AOM_DEBUG_HW_MORE, "%s\n", __func__);
5970     WRITE_VREG(HEVC_FGS_IDX, 0);
5971     for (i = 0; i < FILM_GRAIN_REG_SIZE; i++) {
5972 	    cm->cur_frame->film_grain_reg[i] = READ_VREG(HEVC_FGS_DATA);
5973 	}
5974     cm->cur_frame->film_grain_reg_valid = 1;
5975 }
5976 
config_film_grain_reg(struct AV1HW_s * hw,int film_grain_params_ref_idx)5977 static void config_film_grain_reg(struct AV1HW_s *hw, int film_grain_params_ref_idx)
5978 {
5979 
5980     AV1_COMMON *cm = &hw->pbi->common;
5981     int i;
5982     unsigned char found = 0;
5983     RefCntBuffer *buf;
5984     av1_print(hw, AOM_DEBUG_HW_MORE,
5985 		" ## %s frome reference idx %d\n",
5986 		__func__, film_grain_params_ref_idx);
5987     for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
5988 	    if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
5989 		    found = 1;
5990 		    break;
5991 		}
5992 	}
5993     if (!found) {
5994 	    av1_print(hw, AOM_DEBUG_HW_MORE,
5995 			"%s Error, Invalid film grain reference idx %d\n",
5996 			__func__, film_grain_params_ref_idx);
5997 	    return;
5998 	}
5999     buf = cm->ref_frame_map[film_grain_params_ref_idx];
6000 
6001     if (buf->film_grain_reg_valid == 0) {
6002 	    av1_print(hw, AOM_DEBUG_HW_MORE,
6003 			"%s Error, film grain register data invalid for reference idx %d\n",
6004 			__func__, film_grain_params_ref_idx);
6005 	    return;
6006 	}
6007 
6008     if (cm->cur_frame == NULL) {
6009 	    av1_print(hw, AOM_DEBUG_HW_MORE,
6010 			"%s, cur_frame not exist!!!\n", __func__);
6011 	}
6012     WRITE_VREG(HEVC_FGS_IDX, 0);
6013     for (i = 0; i < FILM_GRAIN_REG_SIZE; i++) {
6014 	    WRITE_VREG(HEVC_FGS_DATA, buf->film_grain_reg[i]);
6015 	    if (cm->cur_frame)
6016 		    cm->cur_frame->film_grain_reg[i] = buf->film_grain_reg[i];
6017 	}
6018     if (cm->cur_frame)
6019 	    cm->cur_frame->film_grain_reg_valid = 1;
6020     WRITE_VREG(HEVC_FGS_CTRL, READ_VREG(HEVC_FGS_CTRL) | 1); // set fil_grain_start
6021 }
6022 
config_next_ref_info_hw(struct AV1HW_s * hw)6023 void config_next_ref_info_hw(struct AV1HW_s *hw)
6024 {
6025   int j;
6026   AV1_COMMON *const cm = &hw->pbi->common;
6027     av1_set_next_ref_frame_map(hw->pbi);
6028     WRITE_VREG(HEVC_PARSER_MEM_WR_ADDR, 0x1000);
6029     for (j = 0; j < 12; j++) {
6030 	  unsigned int info =
6031 		  av1_get_next_used_ref_info(cm, j);
6032 	  WRITE_VREG(HEVC_PARSER_MEM_RW_DATA, info);
6033 	  av1_print(hw, AOM_DEBUG_HW_MORE,
6034 		"config next ref info %d 0x%x\n", j, info);
6035 	}
6036 }
6037 
6038 
6039 
6040 #ifdef PRINT_HEVC_DATA_PATH_MONITOR
datapath_monitor(struct AV1HW_s * hw)6041 void datapath_monitor(struct AV1HW_s *hw)
6042 {
6043 		  uint32_t total_clk_count;
6044 		  uint32_t path_transfer_count;
6045 		  uint32_t path_wait_count;
6046   float path_wait_ratio;
6047   if (pbi->decode_idx > 1) {
6048     WRITE_VREG(HEVC_PATH_MONITOR_CTRL, 0); // Disabble monitor and set rd_idx to 0
6049     total_clk_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6050 
6051     WRITE_VREG(HEVC_PATH_MONITOR_CTRL, (1<<4)); // Disabble monitor and set rd_idx to 0
6052 
6053 // parser --> iqit
6054     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6055     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6056     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6057     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6058     printk("[P%d HEVC PATH] Parser/IQIT/IPP/DBLK/OW/DDR/CMD WAITING \% : %.2f",
6059         pbi->decode_idx - 2, path_wait_ratio);
6060 
6061 // iqit --> ipp
6062     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6063     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6064     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6065     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6066     printk(" %.2f", path_wait_ratio);
6067 
6068 // dblk <-- ipp
6069     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6070     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6071     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6072     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6073     printk(" %.2f", path_wait_ratio);
6074 
6075 // dblk --> ow
6076     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6077     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6078     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6079     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6080     printk(" %.2f", path_wait_ratio);
6081 
6082 // <--> DDR
6083     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6084     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6085     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6086     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6087     printk(" %.2f", path_wait_ratio);
6088 
6089 // CMD
6090     path_transfer_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6091     path_wait_count = READ_VREG(HEVC_PATH_MONITOR_DATA);
6092     if (path_transfer_count == 0) path_wait_ratio = 0.0;
6093     else path_wait_ratio = (float)path_wait_count/(float)path_transfer_count;
6094     printk(" %.2f\n", path_wait_ratio);
6095   }
6096 }
6097 
6098 #endif
6099 
6100 #ifdef MCRCC_ENABLE
6101 
6102 static int mcrcc_hit_rate;
6103 static int mcrcc_bypass_rate;
6104 
6105 #define C_Reg_Wr  WRITE_VREG
C_Reg_Rd(unsigned int adr,unsigned int * val)6106 static void C_Reg_Rd(unsigned int adr, unsigned int *val)
6107 {
6108 	*val = READ_VREG(adr);
6109 }
6110 
mcrcc_perfcount_reset(struct AV1HW_s * hw)6111 static void mcrcc_perfcount_reset(struct AV1HW_s *hw)
6112 {
6113 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE,
6114 		"[cache_util.c] Entered mcrcc_perfcount_reset...\n");
6115 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x1);
6116 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x0);
6117 	return;
6118 }
6119 
6120 static unsigned raw_mcr_cnt_total_prev;
6121 static unsigned hit_mcr_0_cnt_total_prev;
6122 static unsigned hit_mcr_1_cnt_total_prev;
6123 static unsigned byp_mcr_cnt_nchcanv_total_prev;
6124 static unsigned byp_mcr_cnt_nchoutwin_total_prev;
6125 
mcrcc_get_hitrate(struct AV1HW_s * hw,unsigned reset_pre)6126 static void mcrcc_get_hitrate(struct AV1HW_s *hw, unsigned reset_pre)
6127 {
6128 	unsigned  delta_hit_mcr_0_cnt;
6129 	unsigned  delta_hit_mcr_1_cnt;
6130 	unsigned  delta_raw_mcr_cnt;
6131 	unsigned  delta_mcr_cnt_nchcanv;
6132 	unsigned  delta_mcr_cnt_nchoutwin;
6133 
6134 	unsigned   tmp;
6135 	unsigned   raw_mcr_cnt;
6136 	unsigned   hit_mcr_cnt;
6137 	unsigned   byp_mcr_cnt_nchoutwin;
6138 	unsigned   byp_mcr_cnt_nchcanv;
6139 	int      hitrate;
6140 
6141 	if (reset_pre) {
6142 		raw_mcr_cnt_total_prev = 0;
6143 		hit_mcr_0_cnt_total_prev = 0;
6144 		hit_mcr_1_cnt_total_prev = 0;
6145 		byp_mcr_cnt_nchcanv_total_prev = 0;
6146 		byp_mcr_cnt_nchoutwin_total_prev = 0;
6147 	}
6148 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] Entered mcrcc_get_hitrate...\n");
6149 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x0<<1));
6150 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &raw_mcr_cnt);
6151 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x1<<1));
6152 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &hit_mcr_cnt);
6153 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x2<<1));
6154 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &byp_mcr_cnt_nchoutwin);
6155 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x3<<1));
6156 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &byp_mcr_cnt_nchcanv);
6157 
6158 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "raw_mcr_cnt_total: %d\n",raw_mcr_cnt);
6159 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "hit_mcr_cnt_total: %d\n",hit_mcr_cnt);
6160 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "byp_mcr_cnt_nchoutwin_total: %d\n",byp_mcr_cnt_nchoutwin);
6161 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "byp_mcr_cnt_nchcanv_total: %d\n",byp_mcr_cnt_nchcanv);
6162 
6163 	delta_raw_mcr_cnt = raw_mcr_cnt - raw_mcr_cnt_total_prev;
6164 	delta_mcr_cnt_nchcanv = byp_mcr_cnt_nchcanv - byp_mcr_cnt_nchcanv_total_prev;
6165 	delta_mcr_cnt_nchoutwin = byp_mcr_cnt_nchoutwin - byp_mcr_cnt_nchoutwin_total_prev;
6166 	raw_mcr_cnt_total_prev = raw_mcr_cnt;
6167 	byp_mcr_cnt_nchcanv_total_prev = byp_mcr_cnt_nchcanv;
6168 	byp_mcr_cnt_nchoutwin_total_prev = byp_mcr_cnt_nchoutwin;
6169 
6170 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x4<<1));
6171 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &tmp);
6172 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "miss_mcr_0_cnt_total: %d\n",tmp);
6173 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x5<<1));
6174 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &tmp);
6175 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "miss_mcr_1_cnt_total: %d\n",tmp);
6176 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x6<<1));
6177 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &tmp);
6178 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "hit_mcr_0_cnt_total: %d\n",tmp);
6179 	delta_hit_mcr_0_cnt = tmp - hit_mcr_0_cnt_total_prev;
6180 	hit_mcr_0_cnt_total_prev = tmp;
6181 	C_Reg_Wr(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x7<<1));
6182 	C_Reg_Rd(HEVCD_MCRCC_PERFMON_DATA, &tmp);
6183 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "hit_mcr_1_cnt_total: %d\n",tmp);
6184 	delta_hit_mcr_1_cnt = tmp - hit_mcr_1_cnt_total_prev;
6185 	hit_mcr_1_cnt_total_prev = tmp;
6186 
6187 	if ( delta_raw_mcr_cnt != 0 ) {
6188 		hitrate = 100 * delta_hit_mcr_0_cnt/ delta_raw_mcr_cnt;
6189 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "CANV0_HIT_RATE : %d\n", hitrate);
6190 		hitrate = 100 * delta_hit_mcr_1_cnt/delta_raw_mcr_cnt;
6191 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "CANV1_HIT_RATE : %d\n", hitrate);
6192 		hitrate = 100 * delta_mcr_cnt_nchcanv/delta_raw_mcr_cnt;
6193 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "NONCACH_CANV_BYP_RATE : %d\n", hitrate);
6194 		hitrate = 100 * delta_mcr_cnt_nchoutwin/delta_raw_mcr_cnt;
6195 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "CACHE_OUTWIN_BYP_RATE : %d\n", hitrate);
6196 	}
6197 
6198 	if (raw_mcr_cnt != 0)
6199 	{
6200 		hitrate = 100*hit_mcr_cnt/raw_mcr_cnt;
6201 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "MCRCC_HIT_RATE : %d\n", hitrate);
6202 		hitrate = 100*(byp_mcr_cnt_nchoutwin + byp_mcr_cnt_nchcanv)/raw_mcr_cnt;
6203 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "MCRCC_BYP_RATE : %d\n", hitrate);
6204 	} else {
6205 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "MCRCC_HIT_RATE : na\n");
6206 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "MCRCC_BYP_RATE : na\n");
6207 	}
6208 	mcrcc_hit_rate = 100*hit_mcr_cnt/raw_mcr_cnt;
6209 	mcrcc_bypass_rate = 100*(byp_mcr_cnt_nchoutwin + byp_mcr_cnt_nchcanv)/raw_mcr_cnt;
6210 
6211 	return;
6212 }
6213 
decomp_perfcount_reset(struct AV1HW_s * hw)6214 static void decomp_perfcount_reset(struct AV1HW_s *hw)
6215 {
6216 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] Entered decomp_perfcount_reset...\n");
6217 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x1);
6218 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x0);
6219 	return;
6220 }
6221 
decomp_get_hitrate(struct AV1HW_s * hw)6222 static void decomp_get_hitrate(struct AV1HW_s *hw)
6223 {
6224 	unsigned   raw_mcr_cnt;
6225 	unsigned   hit_mcr_cnt;
6226 	int  hitrate;
6227 
6228 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] Entered decomp_get_hitrate...\n");
6229 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x0<<1));
6230 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &raw_mcr_cnt);
6231 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x1<<1));
6232 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &hit_mcr_cnt);
6233 
6234 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "hcache_raw_cnt_total: %d\n",raw_mcr_cnt);
6235 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "hcache_hit_cnt_total: %d\n",hit_mcr_cnt);
6236 
6237 	if ( raw_mcr_cnt != 0 ) {
6238 		hitrate = 100*hit_mcr_cnt/raw_mcr_cnt;
6239 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_HCACHE_HIT_RATE : %.2f\%\n", hitrate);
6240 	} else {
6241 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_HCACHE_HIT_RATE : na\n");
6242 	}
6243 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x2<<1));
6244 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &raw_mcr_cnt);
6245 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x3<<1));
6246 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &hit_mcr_cnt);
6247 
6248 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "dcache_raw_cnt_total: %d\n",raw_mcr_cnt);
6249 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "dcache_hit_cnt_total: %d\n",hit_mcr_cnt);
6250 
6251 	if ( raw_mcr_cnt != 0 ) {
6252 		hitrate = 100*hit_mcr_cnt/raw_mcr_cnt;
6253 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_DCACHE_HIT_RATE : %d\n", hitrate);
6254 
6255 		//hitrate = ((float)hit_mcr_cnt/(float)raw_mcr_cnt);
6256 		//hitrate = (mcrcc_hit_rate + (mcrcc_bypass_rate * hitrate))*100;
6257 		hitrate = mcrcc_hit_rate + (mcrcc_bypass_rate * hit_mcr_cnt/raw_mcr_cnt);
6258 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "MCRCC_DECOMP_DCACHE_EFFECTIVE_HIT_RATE : %d\n", hitrate);
6259 
6260 	} else {
6261 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_DCACHE_HIT_RATE : na\n");
6262 	}
6263 
6264 	return;
6265 }
6266 
decomp_get_comprate(struct AV1HW_s * hw)6267 static void decomp_get_comprate(struct AV1HW_s *hw)
6268 {
6269 	unsigned   raw_ucomp_cnt;
6270 	unsigned   fast_comp_cnt;
6271 	unsigned   slow_comp_cnt;
6272 	int      comprate;
6273 
6274 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] Entered decomp_get_comprate...\n");
6275 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x4<<1));
6276 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &fast_comp_cnt);
6277 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x5<<1));
6278 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &slow_comp_cnt);
6279 	C_Reg_Wr(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x6<<1));
6280 	C_Reg_Rd(HEVCD_MPP_DECOMP_PERFMON_DATA, &raw_ucomp_cnt);
6281 
6282 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "decomp_fast_comp_total: %d\n",fast_comp_cnt);
6283 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "decomp_slow_comp_total: %d\n",slow_comp_cnt);
6284 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "decomp_raw_uncomp_total: %d\n",raw_ucomp_cnt);
6285 
6286 	if ( raw_ucomp_cnt != 0 )
6287 	{
6288 		comprate = 100*(fast_comp_cnt + slow_comp_cnt)/raw_ucomp_cnt;
6289 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_COMP_RATIO : %d\n", comprate);
6290 	} else
6291 	{
6292 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "DECOMP_COMP_RATIO : na\n");
6293 	}
6294 	return;
6295 }
6296 
dump_hit_rate(struct AV1HW_s * hw)6297 static void dump_hit_rate(struct AV1HW_s *hw)
6298 {
6299 	if (debug & AV1_DEBUG_CACHE_HIT_RATE) {
6300 		mcrcc_get_hitrate(hw, hw->m_ins_flag);
6301 		decomp_get_hitrate(hw);
6302 		decomp_get_comprate(hw);
6303 	}
6304 }
6305 
mcrcc_get_abs_frame_distance(struct AV1HW_s * hw,uint32_t refid,uint32_t ref_ohint,uint32_t curr_ohint,uint32_t ohint_bits_min1)6306 static  uint32_t  mcrcc_get_abs_frame_distance(struct AV1HW_s *hw, uint32_t refid, uint32_t ref_ohint, uint32_t curr_ohint, uint32_t ohint_bits_min1)
6307 {
6308 	int32_t diff_ohint0;
6309 	int32_t diff_ohint1;
6310 	uint32_t abs_dist;
6311 	uint32_t m;
6312 	uint32_t m_min1;
6313 
6314 	diff_ohint0 = ref_ohint - curr_ohint;
6315 
6316 	m = (1 << ohint_bits_min1);
6317 	m_min1 = m -1;
6318 
6319 	diff_ohint1 = (diff_ohint0 & m_min1 ) - (diff_ohint0 & m);
6320 
6321 	abs_dist = (diff_ohint1 < 0) ? -diff_ohint1 : diff_ohint1;
6322 
6323 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE,
6324 		"[cache_util.c] refid:%0x ref_orderhint:%0x curr_orderhint:%0x orderhint_bits_min1:%0x abd_dist:%0x\n",
6325 		refid, ref_ohint, curr_ohint, ohint_bits_min1,abs_dist);
6326 
6327 	return  abs_dist;
6328 }
6329 
config_mcrcc_axi_hw_nearest_ref(struct AV1HW_s * hw)6330 static void  config_mcrcc_axi_hw_nearest_ref(struct AV1HW_s *hw)
6331 {
6332 	uint32_t i;
6333 	uint32_t rdata32;
6334 	uint32_t dist_array[8];
6335 	uint32_t refcanvas_array[2];
6336 	uint32_t orderhint_bits;
6337 	unsigned char is_inter;
6338 	AV1_COMMON *cm = &hw->pbi->common;
6339 	PIC_BUFFER_CONFIG *curr_pic_config;
6340 	int32_t  curr_orderhint;
6341 	int cindex0 = LAST_FRAME;
6342 	uint32_t    last_ref_orderhint_dist = 1023; // large distance
6343 	uint32_t    curr_ref_orderhint_dist = 1023; // large distance
6344 	int cindex1;
6345 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE,
6346 		"[test.c] #### config_mcrcc_axi_hw ####\n");
6347 
6348 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0x2); // reset mcrcc
6349 
6350 	is_inter = av1_frame_is_inter(&hw->pbi->common); //((pbi->common.frame_type != KEY_FRAME) && (!pbi->common.intra_only)) ? 1 : 0;
6351 	if ( !is_inter ) { // I-PIC
6352 		//WRITE_VREG(HEVCD_MCRCC_CTL1, 0x1); // remove reset -- disables clock
6353 		WRITE_VREG(HEVCD_MCRCC_CTL2, 0xffffffff); // Replace with current-frame canvas
6354 		WRITE_VREG(HEVCD_MCRCC_CTL3, 0xffffffff); //
6355 		WRITE_VREG(HEVCD_MCRCC_CTL1, 0xff0); // enable mcrcc progressive-mode
6356 		return;
6357 	}
6358 
6359 #if 0
6360 	//printk("before call mcrcc_get_hitrate\r\n");
6361 	mcrcc_get_hitrate(hw);
6362 	decomp_get_hitrate(hw);
6363 	decomp_get_comprate(hw);
6364 #endif
6365 
6366 	// Find absolute orderhint delta
6367 	curr_pic_config =  &cm->cur_frame->buf;
6368 	curr_orderhint = curr_pic_config->order_hint;
6369 	orderhint_bits = cm->seq_params.order_hint_info.order_hint_bits_minus_1;
6370 	for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
6371 		int32_t  ref_orderhint = 0;
6372 		PIC_BUFFER_CONFIG *pic_config;
6373 		//int32_t  tmp;
6374 		pic_config = av1_get_ref_frame_spec_buf(cm,i);
6375 		if (pic_config)
6376 			ref_orderhint = pic_config->order_hint;
6377 		//tmp = curr_orderhint - ref_orderhint;
6378 		//dist_array[i] =  (tmp < 0) ? -tmp : tmp;
6379 		dist_array[i] =  mcrcc_get_abs_frame_distance(hw, i,ref_orderhint, curr_orderhint, orderhint_bits);
6380 	}
6381 	// Get smallest orderhint distance refid
6382 	for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
6383 		PIC_BUFFER_CONFIG *pic_config;
6384 		pic_config = av1_get_ref_frame_spec_buf(cm, i);
6385 		curr_ref_orderhint_dist = dist_array[i];
6386 		if ( curr_ref_orderhint_dist < last_ref_orderhint_dist) {
6387 			cindex0 = i;
6388 			last_ref_orderhint_dist = curr_ref_orderhint_dist;
6389 		}
6390 	}
6391 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR, (cindex0 << 8) | (1<<1) | 0);
6392 	refcanvas_array[0] = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR) & 0xffff;
6393 
6394 	last_ref_orderhint_dist = 1023; // large distance
6395 	curr_ref_orderhint_dist = 1023; // large distance
6396 	// Get 2nd smallest orderhint distance refid
6397 	cindex1 = LAST_FRAME;
6398 	for (i = LAST_FRAME; i <= ALTREF_FRAME; i++) {
6399 		PIC_BUFFER_CONFIG *pic_config;
6400 		pic_config = av1_get_ref_frame_spec_buf(cm, i);
6401 		curr_ref_orderhint_dist = dist_array[i];
6402 		WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR, (i << 8) | (1<<1) | 0);
6403 		refcanvas_array[1] = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR) & 0xffff;
6404 		av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] curr_ref_orderhint_dist:%x last_ref_orderhint_dist:%x refcanvas_array[0]:%x refcanvas_array[1]:%x\n",
6405 		curr_ref_orderhint_dist, last_ref_orderhint_dist, refcanvas_array[0],refcanvas_array[1]);
6406 		if ((curr_ref_orderhint_dist < last_ref_orderhint_dist) && (refcanvas_array[0] != refcanvas_array[1])) {
6407 			cindex1 = i;
6408 			last_ref_orderhint_dist = curr_ref_orderhint_dist;
6409 		}
6410 	}
6411 
6412 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR, (cindex0 << 8) | (1<<1) | 0);
6413 	refcanvas_array[0] = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6414 	WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR, (cindex1 << 8) | (1<<1) | 0);
6415 	refcanvas_array[1] = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
6416 
6417 	av1_print(hw, AV1_DEBUG_CACHE_HIT_RATE, "[cache_util.c] refcanvas_array[0](index %d):%x refcanvas_array[1](index %d):%x\n",
6418 	cindex0, refcanvas_array[0], cindex1, refcanvas_array[1]);
6419 
6420 	// lowest delta_picnum
6421 	rdata32 = refcanvas_array[0];
6422 	rdata32 = rdata32 & 0xffff;
6423 	rdata32 = rdata32 | ( rdata32 << 16);
6424 	WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
6425 
6426 	// 2nd-lowest delta_picnum
6427 	rdata32 = refcanvas_array[1];
6428 	rdata32 = rdata32 & 0xffff;
6429 	rdata32 = rdata32 | ( rdata32 << 16);
6430 	WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32);
6431 
6432 	WRITE_VREG(HEVCD_MCRCC_CTL1, 0xff0); // enable mcrcc progressive-mode
6433 	return;
6434 }
6435 
6436 
6437 #endif
6438 
av1_continue_decoding(struct AV1HW_s * hw,int obu_type)6439 int av1_continue_decoding(struct AV1HW_s *hw, int obu_type)
6440 {
6441 	int ret;
6442 #if 1
6443 	//def CHANGE_DONE
6444 	AV1Decoder *pbi = hw->pbi;
6445 	AV1_COMMON *const cm = &pbi->common;
6446 	int i;
6447 
6448 	av1_print(hw, AOM_DEBUG_HW_MORE,
6449 		"%s: pbi %p cm %p cur_frame %p %d has_seq %d has_keyframe %d\n",
6450 		__func__, pbi, cm, cm->cur_frame,
6451 		pbi->bufmgr_proc_count,
6452 		hw->has_sequence,
6453 		hw->has_keyframe);
6454 
6455 	if (hw->has_sequence == 0) {
6456 		av1_print(hw, 0,
6457 			"no sequence head, skip\n");
6458 		if (!hw->m_ins_flag)
6459 			WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
6460 		return -2;
6461 	} else if (hw->has_keyframe == 0 &&
6462 		hw->aom_param.p.frame_type != KEY_FRAME){
6463 		av1_print(hw, 0,
6464 			"no key frame, skip\n");
6465 		on_no_keyframe_skiped++;
6466 		if (!hw->m_ins_flag)
6467 			WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
6468 		return -2;
6469 	}
6470 	hw->has_keyframe = 1;
6471 	on_no_keyframe_skiped = 0;
6472 
6473 	//pre_decode_idx = pbi->decode_idx;
6474 	if (pbi->bufmgr_proc_count == 0 ||
6475 	hw->one_compressed_data_done) {
6476 		hw->new_compressed_data = 1;
6477 		hw->one_compressed_data_done = 0;
6478 	} else {
6479 		hw->new_compressed_data = 0;
6480 	}
6481 	ret = av1_bufmgr_process(pbi, &hw->aom_param,
6482 		hw->new_compressed_data, obu_type);
6483 	av1_print(hw, AOM_DEBUG_HW_MORE,
6484 		"%s: pbi %p cm %p cur_frame %p\n",
6485 		__func__, pbi, cm, cm->cur_frame);
6486 
6487 	av1_print(hw, AOM_DEBUG_HW_MORE,
6488 		"1+++++++++++++++++++++++++++++++++++%d %p\n",
6489 		ret, cm->cur_frame);
6490 	if (hw->new_compressed_data)
6491 		WRITE_VREG(PIC_END_LCU_COUNT, 0);
6492 
6493 	if (ret > 0) {
6494 		/* the case when cm->show_existing_frame is 1 */
6495 		/*case 3016*/
6496 		av1_print(hw, AOM_DEBUG_HW_MORE,
6497 			"Decoding done (index=%d, show_existing_frame = %d)\n",
6498 			cm->cur_frame? cm->cur_frame->buf.index:-1,
6499 			cm->show_existing_frame
6500 			);
6501 
6502 		if (cm->cur_frame) {
6503 			PIC_BUFFER_CONFIG* cur_pic_config = &cm->cur_frame->buf;
6504 			if (debug &
6505 				AV1_DEBUG_BUFMGR_MORE)
6506 				dump_aux_buf(hw);
6507 			set_pic_aux_data(hw,
6508 				cur_pic_config, 0, 0);
6509 		}
6510 		config_next_ref_info_hw(hw);
6511 
6512 		av1_print(hw, AOM_DEBUG_HW_MORE,
6513 			"aom_bufmgr_process=> %d,decode done, AOM_AV1_SEARCH_HEAD\r\n", ret);
6514 			WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
6515 		pbi->decode_idx++;
6516 		pbi->bufmgr_proc_count++;
6517 		hw->frame_decoded = 1;
6518 		return 0;
6519 	}
6520 	else if (ret < 0) {
6521 		hw->frame_decoded = 1;
6522 		av1_print(hw, AOM_DEBUG_HW_MORE,
6523 		"aom_bufmgr_process=> %d, bufmgr e.r.r.o.r., AOM_AV1_SEARCH_HEAD\r\n", ret);
6524 		WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
6525 		return 0;
6526 	}
6527 	else if (ret == 0) {
6528 		PIC_BUFFER_CONFIG* cur_pic_config = &cm->cur_frame->buf;
6529 		PIC_BUFFER_CONFIG* prev_pic_config = &cm->prev_frame->buf;
6530 		//struct segmentation_lf *seg_4lf = &hw->seg_4lf_store;
6531 		if (debug &
6532 			AV1_DEBUG_BUFMGR_MORE)
6533 			dump_aux_buf(hw);
6534 		set_dv_data(hw);
6535 		if (cm->show_frame &&
6536 			hw->dv_data_buf != NULL)
6537 			copy_dv_data(hw, cur_pic_config);
6538 		/* to do:..
6539 		set_pic_aux_data(hw,
6540 			cur_pic_config, 0, 2);*/
6541 		hw->frame_decoded = 0;
6542 		pbi->bufmgr_proc_count++;
6543 		if (hw->new_compressed_data == 0) {
6544 			WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_DECODE_SLICE);
6545 			return 0;
6546 		}
6547 		av1_print(hw, AOM_DEBUG_HW_MORE,
6548 		" [PICTURE %d] cm->cur_frame->mi_size : (%d X %d) y_crop_size :(%d X %d)\n",
6549 		hw->frame_count,
6550 		cm->cur_frame->mi_cols,
6551 		cm->cur_frame->mi_rows,
6552 		cur_pic_config->y_crop_width,
6553 		cur_pic_config->y_crop_height);
6554 		if (cm->prev_frame > 0) {
6555 			av1_print(hw, AOM_DEBUG_HW_MORE,
6556 			" [SEGMENT] cm->prev_frame->segmentation_enabled : %d\n",
6557 			cm->prev_frame->segmentation_enabled);
6558 			av1_print(hw, AOM_DEBUG_HW_MORE,
6559 			" [SEGMENT] cm->prev_frame->mi_size : (%d X %d)\n",
6560 			cm->prev_frame->mi_cols, cm->prev_frame->mi_rows);
6561 		}
6562 		cm->cur_frame->prev_segmentation_enabled = (cm->prev_frame > 0) ?
6563 		(cm->prev_frame->segmentation_enabled & (cm->prev_frame->segmentation_update_map
6564 		| cm->prev_frame->prev_segmentation_enabled) &
6565 		(cm->cur_frame->mi_rows == cm->prev_frame->mi_rows) &
6566 		(cm->cur_frame->mi_cols == cm->prev_frame->mi_cols)) : 0;
6567 		WRITE_VREG(AV1_SKIP_MODE_INFO,
6568 			(cm->cur_frame->prev_segmentation_enabled << 31) |
6569 			(((cm->prev_frame > 0) ? cm->prev_frame->intra_only : 0) << 30) |
6570 			(((cm->prev_frame > 0) ? prev_pic_config->index : 0x1f) << 24) |
6571 			(((cm->cur_frame > 0) ? cur_pic_config->index : 0x1f) << 16) |
6572 			(cm->current_frame.skip_mode_info.ref_frame_idx_0 & 0xf) |
6573 			((cm->current_frame.skip_mode_info.ref_frame_idx_1 & 0xf) << 4) |
6574 			(cm->current_frame.skip_mode_info.skip_mode_allowed << 8));
6575 		cur_pic_config->decode_idx = pbi->decode_idx;
6576 
6577 		av1_print(hw, AOM_DEBUG_HW_MORE,
6578 			"Decode Frame Data %d frame_type %d (%d) bufmgr_proc_count %d\n",
6579 		pbi->decode_idx,
6580 		cm->cur_frame->frame_type,
6581 		cm->current_frame.frame_type,
6582 		pbi->bufmgr_proc_count);
6583 		pbi->decode_idx++;
6584 		hw->frame_count++;
6585 		cur_pic_config->slice_type = cm->cur_frame->frame_type;
6586 		if (hw->chunk) {
6587 			cur_pic_config->pts = hw->chunk->pts;
6588 			cur_pic_config->pts64 = hw->chunk->pts64;
6589 			hw->chunk->pts = 0;
6590 			 hw->chunk->pts64 = 0;
6591 			//pr_info("continue_decode: pic %p, pts %d\n",cur_pic_config, hw->chunk->pts);
6592 		}
6593 #ifdef DUAL_DECODE
6594 #else
6595 		config_pic_size(hw, hw->aom_param.p.bit_depth);
6596 #endif
6597 		if (get_mv_buf(hw,
6598 			&cm->cur_frame->buf.mv_buf_index,
6599 			&cm->cur_frame->buf.mpred_mv_wr_start_addr
6600 			) < 0) {
6601 			av1_print(hw, 0,
6602 				"%s: Error get_mv_buf fail\n",
6603 				__func__);
6604 			ret = -1;
6605 		}
6606 
6607 		if (ret >= 0 && hw->mmu_enable && ((hw->double_write_mode & 0x10) == 0)) {
6608 			ret = av1_alloc_mmu(hw,
6609 				cm->cur_frame->buf.index,
6610 				cur_pic_config->y_crop_width,
6611 				cur_pic_config->y_crop_height,
6612 				hw->aom_param.p.bit_depth,
6613 				hw->frame_mmu_map_addr);
6614 			if (ret >= 0)
6615 				cm->cur_fb_idx_mmu = cm->cur_frame->buf.index;
6616 			else
6617 				pr_err("can't alloc need mmu1,idx %d ret =%d\n",
6618 				cm->cur_frame->buf.index, ret);
6619 #ifdef AOM_AV1_MMU_DW
6620 			if (hw->dw_mmu_enable) {
6621 				ret = av1_alloc_mmu_dw(hw,
6622 				cm->cur_frame->buf.index,
6623 				cur_pic_config->y_crop_width,
6624 				cur_pic_config->y_crop_height,
6625 				hw->aom_param.p.bit_depth,
6626 				hw->dw_frame_mmu_map_addr);
6627 				if (ret >= 0)
6628 					cm->cur_fb_idx_mmu_dw = cm->cur_frame->buf.index;
6629 				else
6630 					pr_err("can't alloc need dw mmu1,idx %d ret =%d\n",
6631 					cm->cur_frame->buf.index, ret);
6632 			}
6633 #endif
6634 		} else {
6635 			ret = 0;
6636 		}
6637 		if (av1_frame_is_inter(&hw->pbi->common)) {
6638 			//if ((pbi->common.frame_type != KEY_FRAME) && (!pbi->common.intra_only)) {
6639 #ifdef DUAL_DECODE
6640 #else
6641 			config_mc_buffer(hw, hw->aom_param.p.bit_depth, 1);
6642 #endif
6643 			config_mpred_hw(hw, 1);
6644 		}
6645 		else {
6646 			config_mc_buffer(hw, hw->aom_param.p.bit_depth, 0);
6647 			clear_mpred_hw(hw);
6648 			config_mpred_hw(hw, 0);
6649 		}
6650 #ifdef DUAL_DECODE
6651 #else
6652 #ifdef MCRCC_ENABLE
6653 		config_mcrcc_axi_hw_nearest_ref(hw);
6654 #endif
6655 		config_sao_hw(hw, &hw->aom_param);
6656 #endif
6657 
6658 		config_dblk_hw(hw);
6659 
6660 		av1_print(hw, AOM_DEBUG_HW_MORE, "HEVC_DEC_STATUS_REG <= AOM_AV1_DECODE_SLICE\n");
6661 		WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_DECODE_SLICE);
6662 
6663 		// Save segment_feature while hardware decoding
6664 		if (hw->seg_4lf->enabled) {
6665 			for (i = 0; i < 8; i++) {
6666 			cm->cur_frame->segment_feature[i] = READ_VREG(AOM_AV1_SEGMENT_FEATURE);
6667 			}
6668 		} else {
6669 			for (i = 0; i < 8; i++) {
6670 				cm->cur_frame->segment_feature[i] = (0x80000000 | (i << 22));
6671 			}
6672 		}
6673 		WRITE_VREG(HEVC_PARSER_MEM_WR_ADDR, 0x1010 + (cur_pic_config->index));
6674 		if (hw->aom_param.p.segmentation_enabled & 1) // segmentation_enabled
6675 			WRITE_VREG(HEVC_PARSER_MEM_RW_DATA, READ_VREG(AV1_REF_SEG_INFO));
6676 		else
6677 			WRITE_VREG(HEVC_PARSER_MEM_RW_DATA, 0);
6678 	} else {
6679 		av1_print(hw, AOM_DEBUG_HW_MORE, "Sequence head, Search next start code\n");
6680 		cm->prev_fb_idx = INVALID_IDX;
6681 		//skip, search next start code
6682 		WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_DECODE_SLICE);
6683 	}
6684 	return ret;
6685 
6686 #else
6687 
6688 	bit_depth_luma = av1_param.p.bit_depth;
6689 	bit_depth_chroma = av1_param.p.bit_depth;
6690 
6691 	if (hw->process_state != PROC_STATE_SENDAGAIN) {
6692 		ret = av1_bufmgr_process(hw, &av1_param);
6693 		if (!hw->m_ins_flag)
6694 			hw->result_done_count++;
6695 	} else {
6696 		union param_u *params = &av1_param;
6697 		if (hw->mmu_enable && ((hw->double_write_mode & 0x10) == 0)) {
6698 			ret = av1_alloc_mmu(hw,
6699 				cm->new_fb_idx,
6700 				params->p.width,
6701 				params->p.height,
6702 				params->p.bit_depth,
6703 				hw->frame_mmu_map_addr);
6704 			if (ret >= 0)
6705 				cm->cur_fb_idx_mmu = cm->new_fb_idx;
6706 			else
6707 				pr_err("can't alloc need mmu1,idx %d ret =%d\n",
6708 					cm->new_fb_idx, ret);
6709 		} else {
6710 			ret = 0;
6711 		}
6712 		WRITE_VREG(HEVC_PARSER_PICTURE_SIZE,
6713 			(params->p.height << 16) | params->p.width);
6714 	}
6715 	if (ret < 0) {
6716 			pr_info("av1_bufmgr_process=> %d, AV1_10B_DISCARD_NAL\r\n", ret);
6717 		WRITE_VREG(HEVC_DEC_STATUS_REG, AV1_10B_DISCARD_NAL);
6718 		cm->show_frame = 0;
6719 		if (hw->mmu_enable)
6720 			av1_recycle_mmu_buf(hw);
6721 
6722 		if (hw->m_ins_flag) {
6723 			hw->dec_result = DEC_RESULT_DONE;
6724 			amhevc_stop();
6725 			vdec_schedule_work(&hw->work);
6726 		}
6727 		return ret;
6728 	} else if (ret == 0) {
6729 		struct PIC_BUFFER_CONFIG_s *cur_pic_config
6730 			= &cm->cur_frame->buf;
6731 		cur_pic_config->decode_idx = hw->frame_count;
6732 
6733 		if (hw->process_state != PROC_STATE_SENDAGAIN) {
6734 			if (!hw->m_ins_flag) {
6735 				hw->frame_count++;
6736 				decode_frame_count[hw->index]
6737 					= hw->frame_count;
6738 			}
6739 			if (hw->chunk) {
6740 				cur_pic_config->pts = hw->chunk->pts;
6741 				cur_pic_config->pts64 = hw->chunk->pts64;
6742 			}
6743 		}
6744 		/*pr_info("Decode Frame Data %d\n", hw->frame_count);*/
6745 		config_pic_size(hw, av1_param.p.bit_depth);
6746 
6747 		if ((hw->common.frame_type != KEY_FRAME)
6748 			&& (!hw->common.intra_only)) {
6749 			config_mc_buffer(hw, av1_param.p.bit_depth);
6750 			config_mpred_hw(hw);
6751 		} else {
6752 			clear_mpred_hw(hw);
6753 		}
6754 #ifdef MCRCC_ENABLE
6755 		if (mcrcc_cache_alg_flag)
6756 			config_mcrcc_axi_hw_new(hw);
6757 		else
6758 			config_mcrcc_axi_hw(hw);
6759 #endif
6760 		config_sao_hw(hw, &av1_param);
6761 		/*pr_info("HEVC_DEC_STATUS_REG <= AV1_10B_DECODE_SLICE\n");*/
6762 		WRITE_VREG(HEVC_DEC_STATUS_REG, AV1_10B_DECODE_SLICE);
6763 	} else {
6764 		pr_info("Skip search next start code\n");
6765 		cm->prev_fb_idx = INVALID_IDX;
6766 		/*skip, search next start code*/
6767 		WRITE_VREG(HEVC_DEC_STATUS_REG, AV1_10B_DECODE_SLICE);
6768 	}
6769 	hw->process_state = PROC_STATE_DECODESLICE;
6770 	if (hw->mmu_enable && ((hw->double_write_mode & 0x10) == 0)) {
6771 		if (hw->last_put_idx < hw->used_buf_num) {
6772 			struct RefCntBuffer_s *frame_bufs =
6773 				cm->buffer_pool->frame_bufs;
6774 			int i = hw->last_put_idx;
6775 			/*free not used buffers.*/
6776 			if ((frame_bufs[i].ref_count == 0) &&
6777 			(frame_bufs[i].buf.vf_ref == 0) &&
6778 			(frame_bufs[i].buf.index != -1)) {
6779 				decoder_mmu_box_free_idx(hw->mmu_box, i);
6780 			}
6781 			hw->last_put_idx = -1;
6782 		}
6783 	}
6784 	return ret;
6785 #endif
6786 }
6787 
fill_frame_info(struct AV1HW_s * hw,struct PIC_BUFFER_CONFIG_s * frame,unsigned int framesize,unsigned int pts)6788 static void fill_frame_info(struct AV1HW_s *hw,
6789 	struct PIC_BUFFER_CONFIG_s *frame,
6790 	unsigned int framesize,
6791 	unsigned int pts)
6792 {
6793 	struct vframe_qos_s *vframe_qos = &hw->vframe_qos;
6794 
6795 	if (frame->slice_type == KEY_FRAME)
6796 		vframe_qos->type = 1;
6797 	else if (frame->slice_type == INTER_FRAME)
6798 		vframe_qos->type = 2;
6799 /*
6800 #define SHOW_QOS_INFO
6801 */
6802 	vframe_qos->size = framesize;
6803 	vframe_qos->pts = pts;
6804 #ifdef SHOW_QOS_INFO
6805 	av1_print(hw, 0, "slice:%d\n", frame->slice_type);
6806 #endif
6807 	vframe_qos->max_mv = frame->max_mv;
6808 	vframe_qos->avg_mv = frame->avg_mv;
6809 	vframe_qos->min_mv = frame->min_mv;
6810 #ifdef SHOW_QOS_INFO
6811 	av1_print(hw, 0, "mv: max:%d,  avg:%d, min:%d\n",
6812 			vframe_qos->max_mv,
6813 			vframe_qos->avg_mv,
6814 			vframe_qos->min_mv);
6815 #endif
6816 	vframe_qos->max_qp = frame->max_qp;
6817 	vframe_qos->avg_qp = frame->avg_qp;
6818 	vframe_qos->min_qp = frame->min_qp;
6819 #ifdef SHOW_QOS_INFO
6820 	av1_print(hw, 0, "qp: max:%d,  avg:%d, min:%d\n",
6821 			vframe_qos->max_qp,
6822 			vframe_qos->avg_qp,
6823 			vframe_qos->min_qp);
6824 #endif
6825 	vframe_qos->max_skip = frame->max_skip;
6826 	vframe_qos->avg_skip = frame->avg_skip;
6827 	vframe_qos->min_skip = frame->min_skip;
6828 #ifdef SHOW_QOS_INFO
6829 	av1_print(hw, 0, "skip: max:%d,	avg:%d, min:%d\n",
6830 			vframe_qos->max_skip,
6831 			vframe_qos->avg_skip,
6832 			vframe_qos->min_skip);
6833 #endif
6834 	vframe_qos->num++;
6835 	/*
6836 	if (hw->frameinfo_enable)
6837 		vdec_fill_frame_info(vframe_qos, 1);
6838 	*/
6839 }
6840 
6841 /* only when we decoded one field or one frame,
6842 we can call this function to get qos info*/
get_picture_qos_info(struct AV1HW_s * hw)6843 static void get_picture_qos_info(struct AV1HW_s *hw)
6844 {
6845 	struct PIC_BUFFER_CONFIG_s *frame = &hw->cur_buf->buf;
6846 
6847 	if (!frame)
6848 		return;
6849 
6850 	if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_G12A) {
6851 		unsigned char a[3];
6852 		unsigned char i, j, t;
6853 		unsigned long  data;
6854 
6855 		data = READ_VREG(HEVC_MV_INFO);
6856 		if (frame->slice_type == KEY_FRAME)
6857 			data = 0;
6858 		a[0] = data & 0xff;
6859 		a[1] = (data >> 8) & 0xff;
6860 		a[2] = (data >> 16) & 0xff;
6861 
6862 		for (i = 0; i < 3; i++) {
6863 			for (j = i+1; j < 3; j++) {
6864 				if (a[j] < a[i]) {
6865 					t = a[j];
6866 					a[j] = a[i];
6867 					a[i] = t;
6868 				} else if (a[j] == a[i]) {
6869 					a[i]++;
6870 					t = a[j];
6871 					a[j] = a[i];
6872 					a[i] = t;
6873 				}
6874 			}
6875 		}
6876 		frame->max_mv = a[2];
6877 		frame->avg_mv = a[1];
6878 		frame->min_mv = a[0];
6879 
6880 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6881 			"mv data %x  a[0]= %x a[1]= %x a[2]= %x\n",
6882 			data, a[0], a[1], a[2]);
6883 
6884 		data = READ_VREG(HEVC_QP_INFO);
6885 		a[0] = data & 0x1f;
6886 		a[1] = (data >> 8) & 0x3f;
6887 		a[2] = (data >> 16) & 0x7f;
6888 
6889 		for (i = 0; i < 3; i++) {
6890 			for (j = i+1; j < 3; j++) {
6891 				if (a[j] < a[i]) {
6892 					t = a[j];
6893 					a[j] = a[i];
6894 					a[i] = t;
6895 				} else if (a[j] == a[i]) {
6896 					a[i]++;
6897 					t = a[j];
6898 					a[j] = a[i];
6899 					a[i] = t;
6900 				}
6901 			}
6902 		}
6903 		frame->max_qp = a[2];
6904 		frame->avg_qp = a[1];
6905 		frame->min_qp = a[0];
6906 
6907 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6908 			"qp data %x  a[0]= %x a[1]= %x a[2]= %x\n",
6909 			data, a[0], a[1], a[2]);
6910 
6911 		data = READ_VREG(HEVC_SKIP_INFO);
6912 		a[0] = data & 0x1f;
6913 		a[1] = (data >> 8) & 0x3f;
6914 		a[2] = (data >> 16) & 0x7f;
6915 
6916 		for (i = 0; i < 3; i++) {
6917 			for (j = i+1; j < 3; j++) {
6918 				if (a[j] < a[i]) {
6919 					t = a[j];
6920 					a[j] = a[i];
6921 					a[i] = t;
6922 				} else if (a[j] == a[i]) {
6923 					a[i]++;
6924 					t = a[j];
6925 					a[j] = a[i];
6926 					a[i] = t;
6927 				}
6928 			}
6929 		}
6930 		frame->max_skip = a[2];
6931 		frame->avg_skip = a[1];
6932 		frame->min_skip = a[0];
6933 
6934 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6935 			"skip data %x  a[0]= %x a[1]= %x a[2]= %x\n",
6936 			data, a[0], a[1], a[2]);
6937 	} else {
6938 		uint32_t blk88_y_count;
6939 		uint32_t blk88_c_count;
6940 		uint32_t blk22_mv_count;
6941 		uint32_t rdata32;
6942 		int32_t mv_hi;
6943 		int32_t mv_lo;
6944 		uint32_t rdata32_l;
6945 		uint32_t mvx_L0_hi;
6946 		uint32_t mvy_L0_hi;
6947 		uint32_t mvx_L1_hi;
6948 		uint32_t mvy_L1_hi;
6949 		int64_t value;
6950 		uint64_t temp_value;
6951 		int pic_number = frame->decode_idx;
6952 
6953 		frame->max_mv = 0;
6954 		frame->avg_mv = 0;
6955 		frame->min_mv = 0;
6956 
6957 		frame->max_skip = 0;
6958 		frame->avg_skip = 0;
6959 		frame->min_skip = 0;
6960 
6961 		frame->max_qp = 0;
6962 		frame->avg_qp = 0;
6963 		frame->min_qp = 0;
6964 
6965 		av1_print(hw, AV1_DEBUG_QOS_INFO, "slice_type:%d, poc:%d\n",
6966 			frame->slice_type,
6967 			pic_number);
6968 
6969 		/* set rd_idx to 0 */
6970 		WRITE_VREG(HEVC_PIC_QUALITY_CTRL, 0);
6971 
6972 		blk88_y_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
6973 		if (blk88_y_count == 0) {
6974 
6975 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6976 			"[Picture %d Quality] NO Data yet.\n",
6977 			pic_number);
6978 
6979 			/* reset all counts */
6980 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
6981 			return;
6982 		}
6983 		/* qp_y_sum */
6984 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
6985 
6986 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6987 			"[Picture %d Quality] Y QP AVG : %d (%d/%d)\n",
6988 			pic_number, rdata32/blk88_y_count,
6989 			rdata32, blk88_y_count);
6990 
6991 		frame->avg_qp = rdata32/blk88_y_count;
6992 		/* intra_y_count */
6993 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
6994 
6995 		av1_print(hw, AV1_DEBUG_QOS_INFO,
6996 			"[Picture %d Quality] Y intra rate : %d%c (%d)\n",
6997 			pic_number, rdata32*100/blk88_y_count,
6998 			'%', rdata32);
6999 
7000 		/* skipped_y_count */
7001 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7002 
7003 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7004 			"[Picture %d Quality] Y skipped rate : %d%c (%d)\n",
7005 			pic_number, rdata32*100/blk88_y_count,
7006 			'%', rdata32);
7007 
7008 		frame->avg_skip = rdata32*100/blk88_y_count;
7009 		/* coeff_non_zero_y_count */
7010 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7011 
7012 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7013 			"[Picture %d Quality] Y ZERO_Coeff rate : %d%c (%d)\n",
7014 			pic_number, (100 - rdata32*100/(blk88_y_count*1)),
7015 			'%', rdata32);
7016 
7017 		/* blk66_c_count */
7018 		blk88_c_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
7019 		if (blk88_c_count == 0) {
7020 			av1_print(hw, AV1_DEBUG_QOS_INFO,
7021 				"[Picture %d Quality] NO Data yet.\n",
7022 				pic_number);
7023 			/* reset all counts */
7024 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
7025 			return;
7026 		}
7027 		/* qp_c_sum */
7028 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7029 
7030 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7031 				"[Picture %d Quality] C QP AVG : %d (%d/%d)\n",
7032 				pic_number, rdata32/blk88_c_count,
7033 				rdata32, blk88_c_count);
7034 
7035 		/* intra_c_count */
7036 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7037 
7038 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7039 			"[Picture %d Quality] C intra rate : %d%c (%d)\n",
7040 			pic_number, rdata32*100/blk88_c_count,
7041 			'%', rdata32);
7042 
7043 		/* skipped_cu_c_count */
7044 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7045 
7046 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7047 			"[Picture %d Quality] C skipped rate : %d%c (%d)\n",
7048 			pic_number, rdata32*100/blk88_c_count,
7049 			'%', rdata32);
7050 
7051 		/* coeff_non_zero_c_count */
7052 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7053 
7054 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7055 			"[Picture %d Quality] C ZERO_Coeff rate : %d%c (%d)\n",
7056 			pic_number, (100 - rdata32*100/(blk88_c_count*1)),
7057 			'%', rdata32);
7058 
7059 		/* 1'h0, qp_c_max[6:0], 1'h0, qp_c_min[6:0],
7060 		1'h0, qp_y_max[6:0], 1'h0, qp_y_min[6:0] */
7061 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7062 
7063 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7064 			"[Picture %d Quality] Y QP min : %d\n",
7065 			pic_number, (rdata32>>0)&0xff);
7066 
7067 		frame->min_qp = (rdata32>>0)&0xff;
7068 
7069 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7070 			"[Picture %d Quality] Y QP max : %d\n",
7071 			pic_number, (rdata32>>8)&0xff);
7072 
7073 		frame->max_qp = (rdata32>>8)&0xff;
7074 
7075 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7076 			"[Picture %d Quality] C QP min : %d\n",
7077 			pic_number, (rdata32>>16)&0xff);
7078 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7079 			"[Picture %d Quality] C QP max : %d\n",
7080 			pic_number, (rdata32>>24)&0xff);
7081 
7082 		/* blk22_mv_count */
7083 		blk22_mv_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
7084 		if (blk22_mv_count == 0) {
7085 			av1_print(hw, AV1_DEBUG_QOS_INFO,
7086 				"[Picture %d Quality] NO MV Data yet.\n",
7087 				pic_number);
7088 			/* reset all counts */
7089 			WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
7090 			return;
7091 		}
7092 		/* mvy_L1_count[39:32], mvx_L1_count[39:32],
7093 		mvy_L0_count[39:32], mvx_L0_count[39:32] */
7094 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7095 		/* should all be 0x00 or 0xff */
7096 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7097 			"[Picture %d Quality] MV AVG High Bits: 0x%X\n",
7098 			pic_number, rdata32);
7099 
7100 		mvx_L0_hi = ((rdata32>>0)&0xff);
7101 		mvy_L0_hi = ((rdata32>>8)&0xff);
7102 		mvx_L1_hi = ((rdata32>>16)&0xff);
7103 		mvy_L1_hi = ((rdata32>>24)&0xff);
7104 
7105 		/* mvx_L0_count[31:0] */
7106 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
7107 		temp_value = mvx_L0_hi;
7108 		temp_value = (temp_value << 32) | rdata32_l;
7109 
7110 		if (mvx_L0_hi & 0x80)
7111 			value = 0xFFFFFFF000000000 | temp_value;
7112 		else
7113 			value = temp_value;
7114 
7115 		value = div_s64(value, blk22_mv_count);
7116 
7117 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7118 			"[Picture %d Quality] MVX_L0 AVG : %d (%lld/%d)\n",
7119 			pic_number, (int)value,
7120 			value, blk22_mv_count);
7121 
7122 		frame->avg_mv = value;
7123 
7124 		/* mvy_L0_count[31:0] */
7125 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
7126 		temp_value = mvy_L0_hi;
7127 		temp_value = (temp_value << 32) | rdata32_l;
7128 
7129 		if (mvy_L0_hi & 0x80)
7130 			value = 0xFFFFFFF000000000 | temp_value;
7131 		else
7132 			value = temp_value;
7133 
7134 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7135 			"[Picture %d Quality] MVY_L0 AVG : %d (%lld/%d)\n",
7136 			pic_number, rdata32_l/blk22_mv_count,
7137 			value, blk22_mv_count);
7138 
7139 		/* mvx_L1_count[31:0] */
7140 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
7141 		temp_value = mvx_L1_hi;
7142 		temp_value = (temp_value << 32) | rdata32_l;
7143 		if (mvx_L1_hi & 0x80)
7144 			value = 0xFFFFFFF000000000 | temp_value;
7145 		else
7146 			value = temp_value;
7147 
7148 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7149 			"[Picture %d Quality] MVX_L1 AVG : %d (%lld/%d)\n",
7150 			pic_number, rdata32_l/blk22_mv_count,
7151 			value, blk22_mv_count);
7152 
7153 		/* mvy_L1_count[31:0] */
7154 		rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
7155 		temp_value = mvy_L1_hi;
7156 		temp_value = (temp_value << 32) | rdata32_l;
7157 		if (mvy_L1_hi & 0x80)
7158 			value = 0xFFFFFFF000000000 | temp_value;
7159 		else
7160 			value = temp_value;
7161 
7162 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7163 			"[Picture %d Quality] MVY_L1 AVG : %d (%lld/%d)\n",
7164 			pic_number, rdata32_l/blk22_mv_count,
7165 			value, blk22_mv_count);
7166 
7167 		/* {mvx_L0_max, mvx_L0_min} // format : {sign, abs[14:0]}  */
7168 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7169 		mv_hi = (rdata32>>16)&0xffff;
7170 		if (mv_hi & 0x8000)
7171 			mv_hi = 0x8000 - mv_hi;
7172 
7173 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7174 			"[Picture %d Quality] MVX_L0 MAX : %d\n",
7175 			pic_number, mv_hi);
7176 
7177 		frame->max_mv = mv_hi;
7178 
7179 		mv_lo = (rdata32>>0)&0xffff;
7180 		if (mv_lo & 0x8000)
7181 			mv_lo = 0x8000 - mv_lo;
7182 
7183 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7184 			"[Picture %d Quality] MVX_L0 MIN : %d\n",
7185 			pic_number, mv_lo);
7186 
7187 		frame->min_mv = mv_lo;
7188 
7189 		/* {mvy_L0_max, mvy_L0_min} */
7190 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7191 		mv_hi = (rdata32>>16)&0xffff;
7192 		if (mv_hi & 0x8000)
7193 			mv_hi = 0x8000 - mv_hi;
7194 
7195 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7196 			"[Picture %d Quality] MVY_L0 MAX : %d\n",
7197 			pic_number, mv_hi);
7198 
7199 		mv_lo = (rdata32>>0)&0xffff;
7200 		if (mv_lo & 0x8000)
7201 			mv_lo = 0x8000 - mv_lo;
7202 
7203 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7204 			"[Picture %d Quality] MVY_L0 MIN : %d\n",
7205 			pic_number, mv_lo);
7206 
7207 		/* {mvx_L1_max, mvx_L1_min} */
7208 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7209 		mv_hi = (rdata32>>16)&0xffff;
7210 		if (mv_hi & 0x8000)
7211 			mv_hi = 0x8000 - mv_hi;
7212 
7213 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7214 			"[Picture %d Quality] MVX_L1 MAX : %d\n",
7215 			pic_number, mv_hi);
7216 
7217 		mv_lo = (rdata32>>0)&0xffff;
7218 		if (mv_lo & 0x8000)
7219 			mv_lo = 0x8000 - mv_lo;
7220 
7221 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7222 			"[Picture %d Quality] MVX_L1 MIN : %d\n",
7223 			pic_number, mv_lo);
7224 
7225 		/* {mvy_L1_max, mvy_L1_min} */
7226 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
7227 		mv_hi = (rdata32>>16)&0xffff;
7228 		if (mv_hi & 0x8000)
7229 			mv_hi = 0x8000 - mv_hi;
7230 
7231 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7232 			"[Picture %d Quality] MVY_L1 MAX : %d\n",
7233 			pic_number, mv_hi);
7234 
7235 		mv_lo = (rdata32>>0)&0xffff;
7236 		if (mv_lo & 0x8000)
7237 			mv_lo = 0x8000 - mv_lo;
7238 
7239 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7240 			"[Picture %d Quality] MVY_L1 MIN : %d\n",
7241 			pic_number, mv_lo);
7242 
7243 		rdata32 = READ_VREG(HEVC_PIC_QUALITY_CTRL);
7244 
7245 		av1_print(hw, AV1_DEBUG_QOS_INFO,
7246 			"[Picture %d Quality] After Read : VDEC_PIC_QUALITY_CTRL : 0x%x\n",
7247 			pic_number, rdata32);
7248 
7249 		/* reset all counts */
7250 		WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
7251 	}
7252 }
7253 
load_param(struct AV1HW_s * hw,union param_u * params,uint32_t dec_status)7254 static int load_param(struct AV1HW_s *hw, union param_u *params, uint32_t dec_status)
7255 {
7256     int i;
7257     unsigned long flags;
7258     int head_type = 0;
7259     if (dec_status == AOM_AV1_SEQ_HEAD_PARSER_DONE)
7260 	  head_type = OBU_SEQUENCE_HEADER;
7261     else if (dec_status == AOM_AV1_FRAME_HEAD_PARSER_DONE)
7262 	  head_type = OBU_FRAME_HEADER;
7263     else if (dec_status == AOM_AV1_FRAME_PARSER_DONE)
7264 	  head_type = OBU_FRAME;
7265     else if (dec_status == AOM_AV1_REDUNDANT_FRAME_HEAD_PARSER_DONE)
7266 	  head_type = OBU_REDUNDANT_FRAME_HEADER;
7267     else {
7268 	  //printf("Error, dec_status of 0x%x, not supported!!!\n", dec_status);
7269 	  return -1;
7270 	}
7271     av1_print2(AOM_DEBUG_HW_MORE, "load_param: ret 0x%x\n", head_type);
7272 
7273 	    if (debug&AOM_AV1_DEBUG_SEND_PARAM_WITH_REG) {
7274 		    get_rpm_param(params);
7275 		}
7276 	    else {
7277 		    for (i = 0; i < (RPM_END-RPM_BEGIN); i += 4) {
7278 			    int32_t ii;
7279 			    for (ii = 0; ii < 4; ii++) {
7280 				    params->l.data[i+ii]=hw->rpm_ptr[i+3-ii];
7281 				}
7282 			}
7283 		}
7284 
7285     params->p.enable_ref_frame_mvs = (params->p.seq_flags >> 7) & 0x1;
7286     params->p.enable_superres = (params->p.seq_flags >> 15) & 0x1;
7287 
7288     if (debug & AV1_DEBUG_BUFMGR_MORE) {
7289 		lock_buffer_pool(hw->pbi->common.buffer_pool, flags);
7290 	    pr_info("aom_param: (%d)\n", hw->pbi->decode_idx);
7291 		//pbi->slice_idx++;
7292 	    for ( i = 0; i < (RPM_END-RPM_BEGIN); i++) {
7293 		    pr_info("%04x ", params->l.data[i]);
7294 		    if (((i + 1) & 0xf) == 0)
7295 			    pr_info("\n");
7296 		}
7297 		unlock_buffer_pool(hw->pbi->common.buffer_pool, flags);
7298 	}
7299   return head_type;
7300 }
7301 
av1_postproc(struct AV1HW_s * hw)7302 static int av1_postproc(struct AV1HW_s *hw)
7303 {
7304 	if (hw->postproc_done)
7305 		return 0;
7306 	hw->postproc_done = 1;
7307 	return av1_bufmgr_postproc(hw->pbi, hw->frame_decoded);
7308 }
7309 
vav1_isr_thread_fn(int irq,void * data)7310 static irqreturn_t vav1_isr_thread_fn(int irq, void *data)
7311 {
7312 	struct AV1HW_s *hw = (struct AV1HW_s *)data;
7313 	unsigned int dec_status = hw->dec_status;
7314 	int obu_type;
7315 	int ret;
7316 
7317 	/*if (hw->wait_buf)
7318 	 *	pr_info("set wait_buf to 0\r\n");
7319 	 */
7320 	if (hw->eos)
7321 		return IRQ_HANDLED;
7322 	hw->wait_buf = 0;
7323 	if ((dec_status == AOM_NAL_DECODE_DONE) ||
7324 			(dec_status == AOM_SEARCH_BUFEMPTY) ||
7325 			(dec_status == AOM_DECODE_BUFEMPTY)
7326 		) {
7327 		if (hw->m_ins_flag) {
7328 			reset_process_time(hw);
7329 			if (!vdec_frame_based(hw_to_vdec(hw)))
7330 				dec_again_process(hw);
7331 			else {
7332 				hw->dec_result = DEC_RESULT_DONE;
7333 				vdec_schedule_work(&hw->work);
7334 			}
7335 		}
7336 		hw->process_busy = 0;
7337 		return IRQ_HANDLED;
7338 	} else if (dec_status == AOM_AV1_DEC_PIC_END) {
7339 		struct AV1_Common_s *const cm = &hw->pbi->common;
7340 #if 1
7341 		u32 fg_reg0, fg_reg1, num_y_points, num_cb_points, num_cr_points;
7342 		WRITE_VREG(HEVC_FGS_IDX, 0);
7343 		fg_reg0 = READ_VREG(HEVC_FGS_DATA);
7344 		fg_reg1 = READ_VREG(HEVC_FGS_DATA);
7345 		num_y_points = fg_reg1 & 0xf;
7346 		num_cr_points = (fg_reg1 >> 8) & 0xf;
7347 		num_cb_points = (fg_reg1 >> 4) & 0xf;
7348 		if ((num_y_points > 0) ||
7349 		((num_cb_points > 0) | ((fg_reg0 >> 17) & 0x1)) ||
7350 		((num_cr_points > 0) | ((fg_reg0 >> 17) & 0x1)))
7351 			hw->fgs_valid = 1;
7352 		else
7353 			hw->fgs_valid = 0;
7354 		av1_print(hw, AOM_DEBUG_HW_MORE,
7355 			"fg_data0 0x%x fg_data1 0x%x fg_valid %d\n",
7356 			fg_reg0, fg_reg1, hw->fgs_valid);
7357 #else
7358 		if (READ_VREG(HEVC_FGS_CTRL) &
7359 				((1 << 4) | (1 << 5) | (1 << 6)))
7360 			hw->fgs_valid = 1;
7361 		else
7362 			hw->fgs_valid = 0;
7363 #endif
7364 		decode_frame_count[hw->index] = hw->frame_count;
7365 		if (hw->m_ins_flag) {
7366 #ifdef USE_DEC_PIC_END
7367 			if (READ_VREG(PIC_END_LCU_COUNT) != 0) {
7368 				hw->frame_decoded = 1;
7369 				/*
7370 				In c module, multi obus are put in one packet, which is decoded
7371 				with av1_receive_compressed_data().
7372 				For STREAM_MODE or SINGLE_MODE, there is no packet boundary,
7373 				we assume each packet must and only include one picture of data (LCUs)
7374 				 or cm->show_existing_frame is 1
7375 				*/
7376 				av1_print(hw, AOM_DEBUG_HW_MORE,
7377 					"Decoding done (index %d), fgs_valid %d data_size 0x%x shiftbyte 0x%x\n",
7378 					cm->cur_frame? cm->cur_frame->buf.index:-1,
7379 					hw->fgs_valid,
7380 					hw->data_size,
7381 					READ_VREG(HEVC_SHIFT_BYTE_COUNT));
7382 				hw->config_next_ref_info_flag = 1; /*to do: low_latency_flag  case*/
7383 				//config_next_ref_info_hw(hw);
7384 			}
7385 #endif
7386 
7387 			if (get_picture_qos)
7388 				get_picture_qos_info(hw);
7389 
7390 			reset_process_time(hw);
7391 
7392 			if (hw->m_ins_flag && hw->mmu_enable &&
7393 				(debug & AOM_DEBUG_DIS_RECYCLE_MMU_TAIL) == 0) {
7394 				long used_4k_num =
7395 				(READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
7396 				if (cm->cur_frame != NULL) {
7397 					hevc_mmu_dma_check(hw_to_vdec(hw));
7398 
7399 					av1_print(hw, AOM_DEBUG_HW_MORE, "mmu free tail, index %d used_num 0x%x\n",
7400 						cm->cur_frame->buf.index, used_4k_num);
7401 					decoder_mmu_box_free_idx_tail(hw->mmu_box,
7402 						cm->cur_frame->buf.index, used_4k_num);
7403 #ifdef AOM_AV1_MMU_DW
7404 					if (hw->dw_mmu_enable) {
7405 						used_4k_num =
7406 						(READ_VREG(HEVC_SAO_MMU_STATUS2) >> 16);
7407 						decoder_mmu_box_free_idx_tail(hw->mmu_box_dw,
7408 							cm->cur_frame->buf.index, used_4k_num);
7409 						av1_print(hw, AOM_DEBUG_HW_MORE, "dw mmu free tail, index %d used_num 0x%x\n",
7410 							cm->cur_frame->buf.index, used_4k_num);
7411 					}
7412 #endif
7413 				}
7414 
7415 
7416 			}
7417 			/*
7418 			if (debug &
7419 				AV1_DEBUG_BUFMGR_MORE)
7420 				dump_aux_buf(hw);
7421 			set_aux_data(hw,
7422 				&cm->cur_frame->buf, 0, 0);
7423 			*/
7424 			if (/*hw->vf_pre_count == 0 ||*/ hw->low_latency_flag)
7425 				av1_postproc(hw);
7426 
7427 			if (multi_frames_in_one_pack &&
7428 			hw->frame_decoded &&
7429 			READ_VREG(HEVC_SHIFT_BYTE_COUNT) < hw->data_size) {
7430 				WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
7431 				av1_print(hw, AOM_DEBUG_HW_MORE,
7432 				"PIC_END, fgs_valid %d search head ...\n",
7433 				hw->fgs_valid);
7434 				if (hw->config_next_ref_info_flag)
7435 					config_next_ref_info_hw(hw);
7436 			} else {
7437 				hw->dec_result = DEC_RESULT_DONE;
7438 				amhevc_stop();
7439 #ifdef MCRCC_ENABLE
7440 				if (mcrcc_cache_alg_flag)
7441 					dump_hit_rate(hw);
7442 #endif
7443 				vdec_schedule_work(&hw->work);
7444 			}
7445 		} else {
7446             av1_print(hw, AOM_DEBUG_HW_MORE,
7447             	"PIC_END, fgs_valid %d search head ...\n",
7448             	hw->fgs_valid);
7449             WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
7450 #ifdef USE_DEC_PIC_END
7451 		    if (READ_VREG(PIC_END_LCU_COUNT) != 0) {
7452 			    hw->frame_decoded = 1;
7453 					/*
7454 				    In c module, multi obus are put in one packet, which is decoded
7455 				    with av1_receive_compressed_data().
7456 				    For STREAM_MODE or SINGLE_MODE, there is no packet boundary,
7457 				    we assume each packet must and only include one picture of data (LCUs)
7458 					 or cm->show_existing_frame is 1
7459 					*/
7460 				av1_print(hw, AOM_DEBUG_HW_MORE, "Decoding done (index %d)\n",
7461 					cm->cur_frame? cm->cur_frame->buf.index:-1);
7462 				config_next_ref_info_hw(hw);
7463 			}
7464 #endif
7465 			/*
7466 			if (debug &
7467 				AV1_DEBUG_BUFMGR_MORE)
7468 				dump_aux_buf(hw);
7469 			set_aux_data(hw,
7470 				&cm->cur_frame->buf, 0, 0);
7471 			*/
7472 			if (hw->low_latency_flag) {
7473 				av1_postproc(hw);
7474 				vdec_profile(hw_to_vdec(hw), VDEC_PROFILE_EVENT_CB);
7475 				if (debug & PRINT_FLAG_VDEC_DETAIL)
7476 					pr_info("%s AV1 frame done \n", __func__);
7477 			}
7478 		}
7479 
7480 		hw->process_busy = 0;
7481 		return IRQ_HANDLED;
7482 	}
7483 
7484 	if (dec_status == AOM_EOS) {
7485 		if (hw->m_ins_flag)
7486 			reset_process_time(hw);
7487 
7488 		av1_print(hw, AOM_DEBUG_HW_MORE, "AV1_EOS, flush buffer\r\n");
7489 
7490 		av1_postproc(hw);
7491 
7492 		av1_print(hw, AOM_DEBUG_HW_MORE, "send AV1_10B_DISCARD_NAL\r\n");
7493 		WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_DISCARD_NAL);
7494 		hw->process_busy = 0;
7495 		if (hw->m_ins_flag) {
7496 			hw->dec_result = DEC_RESULT_DONE;
7497 			amhevc_stop();
7498 			vdec_schedule_work(&hw->work);
7499 		}
7500 		return IRQ_HANDLED;
7501 	} else if (dec_status == AOM_DECODE_OVER_SIZE) {
7502 		av1_print(hw, AOM_DEBUG_HW_MORE, "av1  decode oversize !!\n");
7503 		/*debug |= (AV1_DEBUG_DIS_LOC_ERROR_PROC |
7504 			AV1_DEBUG_DIS_SYS_ERROR_PROC);*/
7505 		hw->fatal_error |= DECODER_FATAL_ERROR_SIZE_OVERFLOW;
7506 		hw->process_busy = 0;
7507 		if (hw->m_ins_flag)
7508 			reset_process_time(hw);
7509 		return IRQ_HANDLED;
7510 	}
7511 
7512     obu_type = load_param(hw, &hw->aom_param, dec_status);
7513     if (obu_type < 0) {
7514 		hw->process_busy = 0;
7515 		return IRQ_HANDLED;
7516 	}
7517 
7518     if (obu_type == OBU_SEQUENCE_HEADER) {
7519 		int next_lcu_size;
7520 	    hw->has_sequence = 1;
7521 	    av1_bufmgr_process(hw->pbi, &hw->aom_param, 0, obu_type);
7522 
7523 		if ((hw->max_pic_w < hw->aom_param.p.max_frame_width) ||
7524 			(hw->max_pic_h < hw->aom_param.p.max_frame_height)) {
7525 			av1_print(hw, 0, "%s, max size change (%d, %d) -> (%d, %d)\n",
7526 				__func__, hw->max_pic_w, hw->max_pic_h,
7527 				hw->aom_param.p.max_frame_width, hw->aom_param.p.max_frame_height);
7528 			vav1_mmu_map_free(hw);
7529 			hw->max_pic_w = hw->aom_param.p.max_frame_width;
7530 			hw->max_pic_h = hw->aom_param.p.max_frame_height;
7531 			hw->init_pic_w = hw->max_pic_w;
7532 			hw->init_pic_h = hw->max_pic_h;
7533 			hw->pbi->frame_width = hw->init_pic_w;
7534 			hw->pbi->frame_height = hw->init_pic_h;
7535 			if (IS_8K_SIZE(hw->max_pic_w, hw->max_pic_h)) {
7536 				hw->double_write_mode = 4;
7537 				max_buf_num = MAX_BUF_NUM_LESS;
7538 				if (max_buf_num > REF_FRAMES_4K)
7539 					mv_buf_margin = max_buf_num - REF_FRAMES_4K + 1;
7540 				if (((hw->max_pic_w % 64) != 0) &&
7541 					(hw_to_vdec(hw)->canvas_mode != CANVAS_BLKMODE_LINEAR))
7542 					mem_map_mode = 2;
7543 				av1_print(hw, 0, "force 8k double write 4, mem_map_mode %d\n", mem_map_mode);
7544 			}
7545 			vav1_mmu_map_alloc(hw);
7546 			if (hw->mmu_enable)
7547 			    WRITE_VREG(HEVC_SAO_MMU_DMA_CTRL, hw->frame_mmu_map_phy_addr);
7548 #ifdef AOM_AV1_MMU_DW
7549 			if (hw->dw_mmu_enable) {
7550 				WRITE_VREG(HEVC_SAO_MMU_DMA_CTRL2, hw->dw_frame_mmu_map_phy_addr);
7551 				//default of 0xffffffff will disable dw
7552 			    WRITE_VREG(HEVC_SAO_Y_START_ADDR, 0);
7553 			    WRITE_VREG(HEVC_SAO_C_START_ADDR, 0);
7554 			}
7555 #endif
7556 		}
7557 		bit_depth_luma = hw->aom_param.p.bit_depth;
7558 		bit_depth_chroma = hw->aom_param.p.bit_depth;
7559 		next_lcu_size = ((hw->aom_param.p.seq_flags >> 6) & 0x1) ? 128 : 64;
7560 		hw->video_signal_type = (hw->aom_param.p.video_signal_type << 16
7561 			| hw->aom_param.p.color_description);
7562 
7563 	    if (next_lcu_size != hw->current_lcu_size) {
7564 			av1_print(hw, AOM_DEBUG_HW_MORE,
7565 				" ## lcu_size changed from %d to %d\n",
7566 				hw->current_lcu_size, next_lcu_size);
7567 			hw->current_lcu_size = next_lcu_size;
7568 		}
7569 
7570 	    if (!hw->pic_list_init_done) {
7571 #if 0
7572 			if (hw->m_ins_flag) {
7573 				/* picture list init.*/
7574 				hw->dec_result = DEC_INIT_PICLIST;
7575 				vdec_schedule_work(&hw->work);
7576 			} else
7577 #endif
7578 			{
7579 				init_pic_list(hw);
7580 				init_pic_list_hw(hw);
7581 #ifndef MV_USE_FIXED_BUF
7582 				if (init_mv_buf_list(hw) < 0) {
7583 					pr_err("%s: !!!!Error, init_mv_buf_list fail\n", __func__);
7584 				}
7585 #endif
7586 			}
7587 			hw->pic_list_init_done = true;
7588 		}
7589 	    av1_print(hw, AOM_DEBUG_HW_MORE,
7590 			"AOM_AV1_SEQ_HEAD_PARSER_DONE, search head ...\n");
7591 	    WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_SEARCH_HEAD);
7592 		hw->process_busy = 0;
7593 		return IRQ_HANDLED;
7594 	}
7595 #ifndef USE_DEC_PIC_END
7596 		//if (pbi->wait_buf) {
7597 	    if (pbi->bufmgr_proc_count > 0) {
7598 		    if (READ_VREG(PIC_END_LCU_COUNT) != 0) {
7599 			    hw->frame_decoded = 1;
7600 				/*
7601 			    In c module, multi obus are put in one packet, which is decoded
7602 			    with av1_receive_compressed_data().
7603 			    For STREAM_MODE or SINGLE_MODE, there is no packet boundary,
7604 			    we assume each packet must and only include one picture of data (LCUs)
7605 				 or cm->show_existing_frame is 1
7606 				*/
7607 				av1_print(hw, AOM_DEBUG_HW_MORE, "Decoding done (index %d)\n",
7608 					cm->cur_frame? cm->cur_frame->buf.index:-1);
7609 			}
7610 		}
7611 #endif
7612 #if 1
7613 /*def CHECK_OBU_REDUNDANT_FRAME_HEADER*/
7614 	    if (debug & AOM_DEBUG_BUFMGR_ONLY) {
7615 		    if (READ_VREG(PIC_END_LCU_COUNT) != 0)
7616 			    hw->obu_frame_frame_head_come_after_tile = 0;
7617 
7618 		    if (obu_type == OBU_FRAME_HEADER ||
7619 			  obu_type == OBU_FRAME) {
7620 			  hw->obu_frame_frame_head_come_after_tile = 1;
7621 			} else if (obu_type == OBU_REDUNDANT_FRAME_HEADER &&
7622 				hw->obu_frame_frame_head_come_after_tile == 0) {
7623 				if (hw->frame_decoded == 1) {
7624 					av1_print(hw, AOM_DEBUG_HW_MORE,
7625 						"Warning, OBU_REDUNDANT_FRAME_HEADER come without OBU_FRAME or OBU_FRAME_HEAD\n");
7626 					hw->frame_decoded = 0;
7627 				}
7628 			}
7629 		}
7630 #endif
7631 	if (hw->frame_decoded)
7632 		hw->one_compressed_data_done = 1;
7633 
7634 	if (hw->m_ins_flag)
7635 		reset_process_time(hw);
7636 
7637 
7638 	if (hw->process_state != PROC_STATE_SENDAGAIN
7639 		) {
7640 	    if (hw->one_compressed_data_done) {
7641 	        av1_postproc(hw);
7642 	        av1_release_bufs(hw);
7643 #ifndef MV_USE_FIXED_BUF
7644 	        put_un_used_mv_bufs(hw);
7645 #endif
7646 	    }
7647 	}
7648 
7649 	ret = av1_continue_decoding(hw, obu_type);
7650 	hw->postproc_done = 0;
7651 	hw->process_busy = 0;
7652 
7653 	if (hw->m_ins_flag) {
7654 		if (ret >= 0)
7655 			start_process_time(hw);
7656 		else {
7657 			hw->dec_result = DEC_RESULT_DONE;
7658 			amhevc_stop();
7659 			vdec_schedule_work(&hw->work);
7660 		}
7661 	}
7662 
7663 	return IRQ_HANDLED;
7664 }
7665 
vav1_isr(int irq,void * data)7666 static irqreturn_t vav1_isr(int irq, void *data)
7667 {
7668 	int i;
7669 	unsigned int dec_status;
7670 	struct AV1HW_s *hw = (struct AV1HW_s *)data;
7671 	//struct AV1_Common_s *const cm = &hw->pbi->common;
7672 	uint debug_tag;
7673 
7674 	WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
7675 
7676 	dec_status = READ_VREG(HEVC_DEC_STATUS_REG) & 0xff;
7677 	if (!hw)
7678 		return IRQ_HANDLED;
7679 	if (hw->init_flag == 0)
7680 		return IRQ_HANDLED;
7681 	if (hw->process_busy)/*on process.*/
7682 		return IRQ_HANDLED;
7683 	hw->dec_status = dec_status;
7684 	hw->process_busy = 1;
7685 	if (debug & AV1_DEBUG_BUFMGR)
7686 		av1_print(hw, AV1_DEBUG_BUFMGR,
7687 			"av1 isr (%d) dec status  = 0x%x, lcu 0x%x shiftbyte 0x%x (%x %x lev %x, wr %x, rd %x)\n",
7688 			irq,
7689 			dec_status, READ_VREG(HEVC_PARSER_LCU_START),
7690 			READ_VREG(HEVC_SHIFT_BYTE_COUNT),
7691 			READ_VREG(HEVC_STREAM_START_ADDR),
7692 			READ_VREG(HEVC_STREAM_END_ADDR),
7693 			READ_VREG(HEVC_STREAM_LEVEL),
7694 			READ_VREG(HEVC_STREAM_WR_PTR),
7695 			READ_VREG(HEVC_STREAM_RD_PTR)
7696 		);
7697 
7698 	debug_tag = READ_HREG(DEBUG_REG1);
7699 	if (debug_tag & 0x10000) {
7700 		pr_info("LMEM<tag %x>:\n", READ_HREG(DEBUG_REG1));
7701 		for (i = 0; i < 0x400; i += 4) {
7702 			int ii;
7703 			if ((i & 0xf) == 0)
7704 				pr_info("%03x: ", i);
7705 			for (ii = 0; ii < 4; ii++) {
7706 				pr_info("%04x ",
7707 					   hw->lmem_ptr[i + 3 - ii]);
7708 			}
7709 			if (((i + ii) & 0xf) == 0)
7710 				pr_info("\n");
7711 		}
7712 		if (((udebug_pause_pos & 0xffff)
7713 			== (debug_tag & 0xffff)) &&
7714 			(udebug_pause_decode_idx == 0 ||
7715 			udebug_pause_decode_idx == hw->result_done_count) &&
7716 			(udebug_pause_val == 0 ||
7717 			udebug_pause_val == READ_HREG(DEBUG_REG2))) {
7718 			udebug_pause_pos &= 0xffff;
7719 			hw->ucode_pause_pos = udebug_pause_pos;
7720 		}
7721 		else if (debug_tag & 0x20000)
7722 			hw->ucode_pause_pos = 0xffffffff;
7723 		if (hw->ucode_pause_pos)
7724 			reset_process_time(hw);
7725 		else
7726 			WRITE_HREG(DEBUG_REG1, 0);
7727 	} else if (debug_tag != 0) {
7728 		pr_info(
7729 			"dbg%x: %x lcu %x\n", READ_HREG(DEBUG_REG1),
7730 			   READ_HREG(DEBUG_REG2),
7731 			   READ_VREG(HEVC_PARSER_LCU_START));
7732 
7733 		if (((udebug_pause_pos & 0xffff)
7734 			== (debug_tag & 0xffff)) &&
7735 			(udebug_pause_decode_idx == 0 ||
7736 			udebug_pause_decode_idx == hw->result_done_count) &&
7737 			(udebug_pause_val == 0 ||
7738 			udebug_pause_val == READ_HREG(DEBUG_REG2))) {
7739 			udebug_pause_pos &= 0xffff;
7740 			hw->ucode_pause_pos = udebug_pause_pos;
7741 		}
7742 		if (hw->ucode_pause_pos)
7743 			reset_process_time(hw);
7744 		else
7745 			WRITE_HREG(DEBUG_REG1, 0);
7746 		hw->process_busy = 0;
7747 		return IRQ_HANDLED;
7748 	}
7749 
7750     //if (READ_VREG(HEVC_FG_STATUS) == AOM_AV1_FGS_PARAM) {
7751 	if (hw->dec_status == AOM_AV1_FGS_PARAM) {
7752 	    uint32_t status_val = READ_VREG(HEVC_FG_STATUS);
7753 	    WRITE_VREG(HEVC_FG_STATUS, AOM_AV1_FGS_PARAM_CONT);
7754 	    WRITE_VREG(HEVC_DEC_STATUS_REG, AOM_AV1_FGS_PARAM_CONT);
7755 			// Bit[11] - 0 Read, 1 - Write
7756 			// Bit[10:8] - film_grain_params_ref_idx // For Write request
7757 	    if ((status_val >> 11) & 0x1) {
7758 		    uint32_t film_grain_params_ref_idx = (status_val >> 8) & 0x7;
7759 		    config_film_grain_reg(hw, film_grain_params_ref_idx);
7760 		}
7761 	    else
7762 		    read_film_grain_reg(hw);
7763 		hw->process_busy = 0;
7764 	    return IRQ_HANDLED;
7765 	}
7766 
7767 	if (!hw->m_ins_flag) {
7768 		av1_print(hw, AV1_DEBUG_BUFMGR,
7769 			"error flag = %d\n", hw->error_flag);
7770 		if (hw->error_flag == 1) {
7771 			hw->error_flag = 2;
7772 			hw->process_busy = 0;
7773 			return IRQ_HANDLED;
7774 		} else if (hw->error_flag == 3) {
7775 			hw->process_busy = 0;
7776 			return IRQ_HANDLED;
7777 		}
7778 	}
7779 	if (get_free_buf_count(hw) <= 0) {
7780 		/*
7781 		if (hw->wait_buf == 0)
7782 			pr_info("set wait_buf to 1\r\n");
7783 		*/
7784 		hw->wait_buf = 1;
7785 		hw->process_busy = 0;
7786 		av1_print(hw, AV1_DEBUG_BUFMGR,
7787 			"free buf not enough = %d\n",
7788 			get_free_buf_count(hw));
7789 		return IRQ_HANDLED;
7790 	}
7791 	return IRQ_WAKE_THREAD;
7792 }
7793 
av1_set_clk(struct work_struct * work)7794 static void av1_set_clk(struct work_struct *work)
7795 {
7796 	struct AV1HW_s *hw = container_of(work,
7797 		struct AV1HW_s, set_clk_work);
7798 	int fps = 96000 / hw->frame_dur;
7799 
7800 	if (hevc_source_changed(VFORMAT_AV1,
7801 		frame_width, frame_height, fps) > 0)
7802 		hw->saved_resolution = frame_width *
7803 		frame_height * fps;
7804 }
7805 
vav1_put_timer_func(unsigned long arg)7806 static void vav1_put_timer_func(unsigned long arg)
7807 {
7808 	struct AV1HW_s *hw = (struct AV1HW_s *)arg;
7809 	struct timer_list *timer = &hw->timer;
7810 	uint8_t empty_flag;
7811 	unsigned int buf_level;
7812 
7813 	enum receviver_start_e state = RECEIVER_INACTIVE;
7814 
7815 	if (hw->m_ins_flag) {
7816 		if (hw_to_vdec(hw)->next_status
7817 			== VDEC_STATUS_DISCONNECTED) {
7818 			hw->dec_result = DEC_RESULT_FORCE_EXIT;
7819 			vdec_schedule_work(&hw->work);
7820 			pr_debug(
7821 			"vdec requested to be disconnected\n");
7822 			return;
7823 		}
7824 	}
7825 	if (hw->init_flag == 0) {
7826 		if (hw->stat & STAT_TIMER_ARM) {
7827 			timer->expires = jiffies + PUT_INTERVAL;
7828 			add_timer(&hw->timer);
7829 		}
7830 		return;
7831 	}
7832 	if (hw->m_ins_flag == 0) {
7833 		if (vf_get_receiver(hw->provider_name)) {
7834 			state =
7835 				vf_notify_receiver(hw->provider_name,
7836 					VFRAME_EVENT_PROVIDER_QUREY_STATE,
7837 					NULL);
7838 			if ((state == RECEIVER_STATE_NULL)
7839 				|| (state == RECEIVER_STATE_NONE))
7840 				state = RECEIVER_INACTIVE;
7841 		} else
7842 			state = RECEIVER_INACTIVE;
7843 
7844 		empty_flag = (READ_VREG(HEVC_PARSER_INT_STATUS) >> 6) & 0x1;
7845 		/* error watchdog */
7846 		if (empty_flag == 0) {
7847 			/* decoder has input */
7848 			if ((debug & AV1_DEBUG_DIS_LOC_ERROR_PROC) == 0) {
7849 
7850 				buf_level = READ_VREG(HEVC_STREAM_LEVEL);
7851 				/* receiver has no buffer to recycle */
7852 				if ((state == RECEIVER_INACTIVE) &&
7853 					(kfifo_is_empty(&hw->display_q) &&
7854 					 buf_level > 0x200)
7855 					) {
7856 						WRITE_VREG
7857 						(HEVC_ASSIST_MBOX0_IRQ_REG,
7858 						 0x1);
7859 				}
7860 			}
7861 
7862 		}
7863 	}
7864 #ifdef MULTI_INSTANCE_SUPPORT
7865 	else {
7866 		if (
7867 			(decode_timeout_val > 0) &&
7868 			(hw->start_process_time > 0) &&
7869 			((1000 * (jiffies - hw->start_process_time) / HZ)
7870 				> decode_timeout_val)
7871 		) {
7872 			int current_lcu_idx =
7873 				READ_VREG(HEVC_PARSER_LCU_START)
7874 				& 0xffffff;
7875 			if (hw->last_lcu_idx == current_lcu_idx) {
7876 				if (hw->decode_timeout_count > 0)
7877 					hw->decode_timeout_count--;
7878 				if (hw->decode_timeout_count == 0) {
7879 					if (input_frame_based(
7880 						hw_to_vdec(hw)) ||
7881 					(READ_VREG(HEVC_STREAM_LEVEL) > 0x200))
7882 						timeout_process(hw);
7883 					else {
7884 						av1_print(hw, 0,
7885 							"timeout & empty, again\n");
7886 						dec_again_process(hw);
7887 					}
7888 				}
7889 			} else {
7890 				start_process_time(hw);
7891 				hw->last_lcu_idx = current_lcu_idx;
7892 			}
7893 		}
7894 	}
7895 #endif
7896 
7897 	if ((hw->ucode_pause_pos != 0) &&
7898 		(hw->ucode_pause_pos != 0xffffffff) &&
7899 		udebug_pause_pos != hw->ucode_pause_pos) {
7900 		hw->ucode_pause_pos = 0;
7901 		WRITE_HREG(DEBUG_REG1, 0);
7902 	}
7903 #ifdef MULTI_INSTANCE_SUPPORT
7904 	if (debug & AV1_DEBUG_DUMP_DATA) {
7905 		debug &= ~AV1_DEBUG_DUMP_DATA;
7906 		av1_print(hw, 0,
7907 			"%s: chunk size 0x%x off 0x%x sum 0x%x\n",
7908 			__func__,
7909 			hw->chunk->size,
7910 			hw->chunk->offset,
7911 			get_data_check_sum(hw, hw->chunk->size)
7912 			);
7913 		dump_data(hw, hw->chunk->size);
7914 	}
7915 #endif
7916 	if (debug & AV1_DEBUG_DUMP_PIC_LIST) {
7917 		/*dump_pic_list(hw);*/
7918 		av1_dump_state(hw_to_vdec(hw));
7919 		debug &= ~AV1_DEBUG_DUMP_PIC_LIST;
7920 	}
7921 	if (debug & AV1_DEBUG_TRIG_SLICE_SEGMENT_PROC) {
7922 		WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG, 0x1);
7923 		debug &= ~AV1_DEBUG_TRIG_SLICE_SEGMENT_PROC;
7924 	}
7925 	/*if (debug & AV1_DEBUG_HW_RESET) {
7926 	}*/
7927 
7928 	if (radr != 0) {
7929 		if ((radr >> 24) != 0) {
7930 			int count = radr >> 24;
7931 			int adr = radr & 0xffffff;
7932 			int i;
7933 			for (i = 0; i < count; i++)
7934 				pr_info("READ_VREG(%x)=%x\n", adr+i, READ_VREG(adr+i));
7935 		} else if (rval != 0) {
7936 			WRITE_VREG(radr, rval);
7937 			pr_info("WRITE_VREG(%x,%x)\n", radr, rval);
7938 		} else
7939 			pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr));
7940 		rval = 0;
7941 		radr = 0;
7942 	}
7943 	if (pop_shorts != 0) {
7944 		int i;
7945 		u32 sum = 0;
7946 
7947 		pr_info("pop stream 0x%x shorts\r\n", pop_shorts);
7948 		for (i = 0; i < pop_shorts; i++) {
7949 			u32 data =
7950 			(READ_HREG(HEVC_SHIFTED_DATA) >> 16);
7951 			WRITE_HREG(HEVC_SHIFT_COMMAND,
7952 			(1<<7)|16);
7953 			if ((i & 0xf) == 0)
7954 				pr_info("%04x:", i);
7955 			pr_info("%04x ", data);
7956 			if (((i + 1) & 0xf) == 0)
7957 				pr_info("\r\n");
7958 			sum += data;
7959 		}
7960 		pr_info("\r\nsum = %x\r\n", sum);
7961 		pop_shorts = 0;
7962 	}
7963 	if (dbg_cmd != 0) {
7964 		if (dbg_cmd == 1) {
7965 			u32 disp_laddr;
7966 
7967 			if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_GXBB &&
7968 				get_double_write_mode(hw) == 0) {
7969 				disp_laddr =
7970 					READ_VCBUS_REG(AFBC_BODY_BADDR) << 4;
7971 			} else {
7972 				struct canvas_s cur_canvas;
7973 
7974 				canvas_read((READ_VCBUS_REG(VD1_IF0_CANVAS0)
7975 					& 0xff), &cur_canvas);
7976 				disp_laddr = cur_canvas.addr;
7977 			}
7978 			pr_info("current displayed buffer address %x\r\n",
7979 				disp_laddr);
7980 		}
7981 		dbg_cmd = 0;
7982 	}
7983 	/*don't changed at start.*/
7984 	if (hw->get_frame_dur && hw->show_frame_num > 60 &&
7985 		hw->frame_dur > 0 && hw->saved_resolution !=
7986 		frame_width * frame_height *
7987 			(96000 / hw->frame_dur))
7988 		vdec_schedule_work(&hw->set_clk_work);
7989 
7990 	timer->expires = jiffies + PUT_INTERVAL;
7991 	add_timer(timer);
7992 }
7993 
7994 
vav1_dec_status(struct vdec_s * vdec,struct vdec_info * vstatus)7995 int vav1_dec_status(struct vdec_s *vdec, struct vdec_info *vstatus)
7996 {
7997 	struct AV1HW_s *av1 =
7998 		(struct AV1HW_s *)vdec->private;
7999 
8000 	if (!av1)
8001 		return -1;
8002 
8003 	vstatus->frame_width = frame_width;
8004 	vstatus->frame_height = frame_height;
8005 	if (av1->frame_dur != 0)
8006 		vstatus->frame_rate = 96000 / av1->frame_dur;
8007 	else
8008 		vstatus->frame_rate = -1;
8009 	vstatus->error_count = 0;
8010 	vstatus->status = av1->stat | av1->fatal_error;
8011 	vstatus->frame_dur = av1->frame_dur;
8012 #ifndef CONFIG_AMLOGIC_MEDIA_MULTI_DEC
8013 	vstatus->bit_rate = gvs->bit_rate;
8014 	vstatus->frame_data = gvs->frame_data;
8015 	vstatus->total_data = gvs->total_data;
8016 	vstatus->frame_count = gvs->frame_count;
8017 	vstatus->error_frame_count = gvs->error_frame_count;
8018 	vstatus->drop_frame_count = gvs->drop_frame_count;
8019 	vstatus->total_data = gvs->total_data;
8020 	vstatus->samp_cnt = gvs->samp_cnt;
8021 	vstatus->offset = gvs->offset;
8022 	snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name),
8023 		"%s", DRIVER_NAME);
8024 #endif
8025 	return 0;
8026 }
8027 
vav1_set_isreset(struct vdec_s * vdec,int isreset)8028 int vav1_set_isreset(struct vdec_s *vdec, int isreset)
8029 {
8030 	is_reset = isreset;
8031 	return 0;
8032 }
8033 
8034 #if 0
8035 static void AV1_DECODE_INIT(void)
8036 {
8037 	/* enable av1 clocks */
8038 	WRITE_VREG(DOS_GCLK_EN3, 0xffffffff);
8039 	/* *************************************************************** */
8040 	/* Power ON HEVC */
8041 	/* *************************************************************** */
8042 	/* Powerup HEVC */
8043 	WRITE_VREG(AO_RTI_GEN_PWR_SLEEP0,
8044 			READ_VREG(AO_RTI_GEN_PWR_SLEEP0) & (~(0x3 << 6)));
8045 	WRITE_VREG(DOS_MEM_PD_HEVC, 0x0);
8046 	WRITE_VREG(DOS_SW_RESET3, READ_VREG(DOS_SW_RESET3) | (0x3ffff << 2));
8047 	WRITE_VREG(DOS_SW_RESET3, READ_VREG(DOS_SW_RESET3) & (~(0x3ffff << 2)));
8048 	/* remove isolations */
8049 	WRITE_VREG(AO_RTI_GEN_PWR_ISO0,
8050 			READ_VREG(AO_RTI_GEN_PWR_ISO0) & (~(0x3 << 10)));
8051 
8052 }
8053 #endif
8054 
vav1_prot_init(struct AV1HW_s * hw,u32 mask)8055 static void vav1_prot_init(struct AV1HW_s *hw, u32 mask)
8056 {
8057 	unsigned int data32;
8058 	/* AV1_DECODE_INIT(); */
8059 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
8060 
8061 	aom_config_work_space_hw(hw, mask);
8062 	if (mask & HW_MASK_BACK) {
8063 		//to do: .. for single instance, called after init_pic_list()
8064 		if (hw->m_ins_flag)
8065 			init_pic_list_hw(hw);
8066 	}
8067 
8068 	aom_init_decoder_hw(hw, mask);
8069 
8070 #ifdef AOM_AV1_DBLK_INIT
8071 	av1_print(hw, AOM_DEBUG_HW_MORE,
8072 	"[test.c] av1_loop_filter_init (run once before decoding start)\n");
8073     av1_loop_filter_init(hw->lfi, hw->lf);
8074 #endif
8075 	if ((mask & HW_MASK_FRONT) == 0)
8076 		return;
8077 #if 1
8078 	if (debug & AV1_DEBUG_BUFMGR_MORE)
8079 		pr_info("%s\n", __func__);
8080 	data32 = READ_VREG(HEVC_STREAM_CONTROL);
8081 	data32 = data32 |
8082 		(1 << 0)/*stream_fetch_enable*/
8083 		;
8084 	WRITE_VREG(HEVC_STREAM_CONTROL, data32);
8085 
8086 	if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
8087 	    if (debug & AV1_DEBUG_BUFMGR)
8088 		    pr_info("[test.c] Config STREAM_FIFO_CTL\n");
8089 	    data32 = READ_VREG(HEVC_STREAM_FIFO_CTL);
8090 	    data32 = data32 |
8091 				 (1 << 29) // stream_fifo_hole
8092 				 ;
8093 	    WRITE_VREG(HEVC_STREAM_FIFO_CTL, data32);
8094 	}
8095 #if 0
8096 	data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
8097 	if (data32 != 0x00000100) {
8098 		pr_info("av1 prot init error %d\n", __LINE__);
8099 		return;
8100 	}
8101 	data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
8102 	if (data32 != 0x00000300) {
8103 		pr_info("av1 prot init error %d\n", __LINE__);
8104 		return;
8105 	}
8106 	WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x12345678);
8107 	WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x9abcdef0);
8108 	data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
8109 	if (data32 != 0x12345678) {
8110 		pr_info("av1 prot init error %d\n", __LINE__);
8111 		return;
8112 	}
8113 	data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
8114 	if (data32 != 0x9abcdef0) {
8115 		pr_info("av1 prot init error %d\n", __LINE__);
8116 		return;
8117 	}
8118 #endif
8119 	WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x000000001);
8120 	WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x00000300);
8121 #endif
8122 
8123 
8124 
8125 	WRITE_VREG(HEVC_WAIT_FLAG, 1);
8126 
8127 	/* WRITE_VREG(HEVC_MPSR, 1); */
8128 
8129 	/* clear mailbox interrupt */
8130 	WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
8131 
8132 	/* enable mailbox interrupt */
8133 	WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 1);
8134 
8135 	/* disable PSCALE for hardware sharing */
8136 	WRITE_VREG(HEVC_PSCALE_CTRL, 0);
8137 
8138 	WRITE_VREG(DEBUG_REG1, 0x0);
8139 	/*check vps/sps/pps/i-slice in ucode*/
8140 	WRITE_VREG(NAL_SEARCH_CTL, 0x8);
8141 
8142 	WRITE_VREG(DECODE_STOP_POS, udebug_flag);
8143 }
8144 
vav1_local_init(struct AV1HW_s * hw)8145 static int vav1_local_init(struct AV1HW_s *hw)
8146 {
8147 	int i;
8148 	int ret;
8149 	int width, height;
8150 
8151 	hw->gvs = vzalloc(sizeof(struct vdec_info));
8152 	if (NULL == hw->gvs) {
8153 		pr_info("the struct of vdec status malloc failed.\n");
8154 		return -1;
8155 	}
8156 #ifdef DEBUG_PTS
8157 	hw->pts_missed = 0;
8158 	hw->pts_hit = 0;
8159 #endif
8160 	hw->new_frame_displayed = 0;
8161 	hw->last_put_idx = -1;
8162 	hw->saved_resolution = 0;
8163 	hw->get_frame_dur = false;
8164 	on_no_keyframe_skiped = 0;
8165 	hw->duration_from_pts_done = 0;
8166 	hw->av1_first_pts_ready = 0;
8167 	hw->frame_cnt_window = 0;
8168 	width = hw->vav1_amstream_dec_info.width;
8169 	height = hw->vav1_amstream_dec_info.height;
8170 	hw->frame_dur =
8171 		(hw->vav1_amstream_dec_info.rate ==
8172 		 0) ? 3200 : hw->vav1_amstream_dec_info.rate;
8173 	if (width && height)
8174 		hw->frame_ar = height * 0x100 / width;
8175 /*
8176  *TODO:FOR VERSION
8177  */
8178 	pr_info("av1: ver (%d,%d) decinfo: %dx%d rate=%d\n", av1_version,
8179 		   0, width, height, hw->frame_dur);
8180 
8181 	if (hw->frame_dur == 0)
8182 		hw->frame_dur = 96000 / 24;
8183 
8184 	INIT_KFIFO(hw->display_q);
8185 	INIT_KFIFO(hw->newframe_q);
8186 
8187 
8188 	for (i = 0; i < VF_POOL_SIZE; i++) {
8189 		const struct vframe_s *vf = &hw->vfpool[i];
8190 
8191 		hw->vfpool[i].index = -1;
8192 		kfifo_put(&hw->newframe_q, vf);
8193 	}
8194 
8195 	ret = av1_local_init(hw);
8196 
8197 	if (force_pts_unstable) {
8198 		if (!hw->pts_unstable) {
8199 			hw->pts_unstable =
8200 			(hw->vav1_amstream_dec_info.rate == 0)?1:0;
8201 			pr_info("set pts unstable\n");
8202 		}
8203 	}
8204 	return ret;
8205 }
8206 
8207 
8208 #ifdef MULTI_INSTANCE_SUPPORT
vav1_init(struct vdec_s * vdec)8209 static s32 vav1_init(struct vdec_s *vdec)
8210 {
8211 	struct AV1HW_s *hw = (struct AV1HW_s *)vdec->private;
8212 #else
8213 static s32 vav1_init(struct AV1HW_s *hw)
8214 {
8215 #endif
8216 	int ret;
8217 	int fw_size = 0x1000 * 16;
8218 	struct firmware_s *fw = NULL;
8219 
8220 	hw->stat |= STAT_TIMER_INIT;
8221 
8222 	if (vav1_local_init(hw) < 0)
8223 		return -EBUSY;
8224 
8225 	fw = vmalloc(sizeof(struct firmware_s) + fw_size);
8226 	if (IS_ERR_OR_NULL(fw))
8227 		return -ENOMEM;
8228 
8229 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
8230 #ifdef DEBUG_USE_VP9_DEVICE_NAME
8231 	if (get_firmware_data(VIDEO_DEC_VP9_MMU, fw->data) < 0) {
8232 #else
8233 	if (get_firmware_data(VIDEO_DEC_AV1_MMU, fw->data) < 0) {
8234 #endif
8235 		pr_err("get firmware fail.\n");
8236 		printk("%s %d\n", __func__, __LINE__);
8237 		vfree(fw);
8238 		return -1;
8239 	}
8240 	av1_print(hw, AOM_DEBUG_HW_MORE, "%s %d\n", __func__, __LINE__);
8241 	fw->len = fw_size;
8242 
8243 	INIT_WORK(&hw->set_clk_work, av1_set_clk);
8244 	init_timer(&hw->timer);
8245 
8246 #ifdef MULTI_INSTANCE_SUPPORT
8247 	if (hw->m_ins_flag) {
8248 		hw->timer.data = (ulong) hw;
8249 		hw->timer.function = vav1_put_timer_func;
8250 		hw->timer.expires = jiffies + PUT_INTERVAL;
8251 
8252 		/*add_timer(&hw->timer);
8253 
8254 		hw->stat |= STAT_TIMER_ARM;
8255 		hw->stat |= STAT_ISR_REG;*/
8256 
8257 		INIT_WORK(&hw->work, av1_work);
8258 		hw->fw = fw;
8259 
8260 		return 0;	/*multi instance return */
8261 	}
8262 #endif
8263 	amhevc_enable();
8264 
8265 	ret = amhevc_loadmc_ex(VFORMAT_AV1, NULL, fw->data);
8266 	if (ret < 0) {
8267 		amhevc_disable();
8268 		vfree(fw);
8269 		pr_err("AV1: the %s fw loading failed, err: %x\n",
8270 			tee_enabled() ? "TEE" : "local", ret);
8271 		return -EBUSY;
8272 	}
8273 
8274 	vfree(fw);
8275 
8276 	hw->stat |= STAT_MC_LOAD;
8277 
8278 	/* enable AMRISC side protocol */
8279 	vav1_prot_init(hw, HW_MASK_FRONT | HW_MASK_BACK);
8280 
8281 	if (vdec_request_threaded_irq(VDEC_IRQ_0,
8282 				vav1_isr,
8283 				vav1_isr_thread_fn,
8284 				IRQF_ONESHOT,/*run thread on this irq disabled*/
8285 				"vav1-irq", (void *)hw)) {
8286 		pr_info("vav1 irq register error.\n");
8287 		amhevc_disable();
8288 		return -ENOENT;
8289 	}
8290 
8291 	hw->stat |= STAT_ISR_REG;
8292 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
8293 	if (force_dv_enable)
8294 		hw->provider_name = DV_PROVIDER_NAME;
8295 	else
8296 #endif
8297 		hw->provider_name = PROVIDER_NAME;
8298 #ifdef MULTI_INSTANCE_SUPPORT
8299 	vf_provider_init(&vav1_vf_prov, hw->provider_name,
8300 				&vav1_vf_provider, hw);
8301 	vf_reg_provider(&vav1_vf_prov);
8302 	vf_notify_receiver(hw->provider_name, VFRAME_EVENT_PROVIDER_START, NULL);
8303 	if (hw->frame_dur != 0) {
8304 		if (!is_reset)
8305 			vf_notify_receiver(hw->provider_name,
8306 					VFRAME_EVENT_PROVIDER_FR_HINT,
8307 					(void *)
8308 					((unsigned long)hw->frame_dur));
8309 	}
8310 #else
8311 	vf_provider_init(&vav1_vf_prov, hw->provider_name, &vav1_vf_provider,
8312 					 hw);
8313 	vf_reg_provider(&vav1_vf_prov);
8314 	vf_notify_receiver(hw->provider_name, VFRAME_EVENT_PROVIDER_START, NULL);
8315 	if (!is_reset)
8316 		vf_notify_receiver(hw->provider_name, VFRAME_EVENT_PROVIDER_FR_HINT,
8317 		(void *)((unsigned long)hw->frame_dur));
8318 #endif
8319 	hw->stat |= STAT_VF_HOOK;
8320 
8321 	hw->timer.data = (ulong)hw;
8322 	hw->timer.function = vav1_put_timer_func;
8323 	hw->timer.expires = jiffies + PUT_INTERVAL;
8324 
8325 	hw->stat |= STAT_VDEC_RUN;
8326 
8327 	add_timer(&hw->timer);
8328 	hw->stat |= STAT_TIMER_ARM;
8329 
8330 	amhevc_start();
8331 
8332 	hw->init_flag = 1;
8333 	hw->process_busy = 0;
8334 	pr_info("%d, vav1_init, RP=0x%x\n",
8335 		__LINE__, READ_VREG(HEVC_STREAM_RD_PTR));
8336 	return 0;
8337 }
8338 
8339 static int vmav1_stop(struct AV1HW_s *hw)
8340 {
8341 	hw->init_flag = 0;
8342 
8343 	if (hw->stat & STAT_VDEC_RUN) {
8344 		amhevc_stop();
8345 		hw->stat &= ~STAT_VDEC_RUN;
8346 	}
8347 	if (hw->stat & STAT_ISR_REG) {
8348 		vdec_free_irq(VDEC_IRQ_0, (void *)hw);
8349 		hw->stat &= ~STAT_ISR_REG;
8350 	}
8351 	if (hw->stat & STAT_TIMER_ARM) {
8352 		del_timer_sync(&hw->timer);
8353 		hw->stat &= ~STAT_TIMER_ARM;
8354 	}
8355 
8356 	if (hw->stat & STAT_VF_HOOK) {
8357 		if (!is_reset)
8358 			vf_notify_receiver(hw->provider_name,
8359 					VFRAME_EVENT_PROVIDER_FR_END_HINT,
8360 					NULL);
8361 
8362 		vf_unreg_provider(&vav1_vf_prov);
8363 		hw->stat &= ~STAT_VF_HOOK;
8364 	}
8365 	av1_local_uninit(hw);
8366 	reset_process_time(hw);
8367 	cancel_work_sync(&hw->work);
8368 	cancel_work_sync(&hw->set_clk_work);
8369 	uninit_mmu_buffers(hw);
8370 	if (hw->fw)
8371 		vfree(hw->fw);
8372 	hw->fw = NULL;
8373 	return 0;
8374 }
8375 
8376 static int vav1_stop(struct AV1HW_s *hw)
8377 {
8378 
8379 	hw->init_flag = 0;
8380 	hw->first_sc_checked = 0;
8381 	if (hw->stat & STAT_VDEC_RUN) {
8382 		amhevc_stop();
8383 		hw->stat &= ~STAT_VDEC_RUN;
8384 	}
8385 
8386 	if (hw->stat & STAT_ISR_REG) {
8387 #ifdef MULTI_INSTANCE_SUPPORT
8388 		if (!hw->m_ins_flag)
8389 #endif
8390 			WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
8391 		vdec_free_irq(VDEC_IRQ_0, (void *)hw);
8392 		hw->stat &= ~STAT_ISR_REG;
8393 	}
8394 
8395 	if (hw->stat & STAT_TIMER_ARM) {
8396 		del_timer_sync(&hw->timer);
8397 		hw->stat &= ~STAT_TIMER_ARM;
8398 	}
8399 
8400 	if (hw->stat & STAT_VF_HOOK) {
8401 		if (!is_reset)
8402 			vf_notify_receiver(hw->provider_name,
8403 					VFRAME_EVENT_PROVIDER_FR_END_HINT,
8404 					NULL);
8405 
8406 		vf_unreg_provider(&vav1_vf_prov);
8407 		hw->stat &= ~STAT_VF_HOOK;
8408 	}
8409 	av1_local_uninit(hw);
8410 
8411 	cancel_work_sync(&hw->set_clk_work);
8412 #ifdef MULTI_INSTANCE_SUPPORT
8413 	if (hw->m_ins_flag) {
8414 		cancel_work_sync(&hw->work);
8415 	} else
8416 		amhevc_disable();
8417 #else
8418 	amhevc_disable();
8419 #endif
8420 	uninit_mmu_buffers(hw);
8421 
8422 	vfree(hw->fw);
8423 	hw->fw = NULL;
8424 	return 0;
8425 }
8426 static int amvdec_av1_mmu_init(struct AV1HW_s *hw)
8427 {
8428 	int tvp_flag = vdec_secure(hw_to_vdec(hw)) ?
8429 		CODEC_MM_FLAGS_TVP : 0;
8430 	int buf_size = 48;
8431 
8432 	if ((hw->max_pic_w * hw->max_pic_h > 1280*736) &&
8433 		(hw->max_pic_w * hw->max_pic_h <= 1920*1088)) {
8434 		buf_size = 12;
8435 	} else if ((hw->max_pic_w * hw->max_pic_h > 0) &&
8436 		(hw->max_pic_w * hw->max_pic_h <= 1280*736)) {
8437 		buf_size = 4;
8438 	}
8439 	hw->need_cache_size = buf_size * SZ_1M;
8440 	hw->sc_start_time = get_jiffies_64();
8441 	if (hw->mmu_enable && ((hw->double_write_mode & 0x10) == 0)) {
8442 		int count = FRAME_BUFFERS;
8443 		hw->mmu_box = decoder_mmu_box_alloc_box(DRIVER_NAME,
8444 			hw->index /* * 2*/, count,
8445 			hw->need_cache_size,
8446 			tvp_flag
8447 			);
8448 		if (!hw->mmu_box) {
8449 			pr_err("av1 alloc mmu box failed!!\n");
8450 			return -1;
8451 		}
8452 #ifdef AOM_AV1_MMU_DW
8453 		if (hw->dw_mmu_enable) {
8454 			hw->mmu_box_dw = decoder_mmu_box_alloc_box(DRIVER_NAME,
8455 				hw->index /** 2 + 1*/, count,
8456 				hw->need_cache_size,
8457 				tvp_flag
8458 				);
8459 			if (!hw->mmu_box_dw) {
8460 				pr_err("av1 alloc dw mmu box failed!!\n");
8461 				return -1;
8462 			}
8463 		}
8464 #endif
8465 
8466 	}
8467 	hw->bmmu_box = decoder_bmmu_box_alloc_box(
8468 			DRIVER_NAME,
8469 			hw->index,
8470 			MAX_BMMU_BUFFER_NUM,
8471 			4 + PAGE_SHIFT,
8472 			CODEC_MM_FLAGS_CMA_CLEAR |
8473 			CODEC_MM_FLAGS_FOR_VDECODER |
8474 			tvp_flag);
8475 	av1_print(hw, AV1_DEBUG_BUFMGR,
8476 		"%s, MAX_BMMU_BUFFER_NUM = %d\n",
8477 		__func__,
8478 		MAX_BMMU_BUFFER_NUM);
8479 	if (!hw->bmmu_box) {
8480 		pr_err("av1 alloc bmmu box failed!!\n");
8481 		return -1;
8482 	}
8483 	return 0;
8484 }
8485 
8486 static struct AV1HW_s *gHevc;
8487 
8488 
8489 static int amvdec_av1_probe(struct platform_device *pdev)
8490 {
8491 	struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
8492 
8493 	struct AV1HW_s *hw;
8494 	AV1Decoder *pbi;
8495 	int ret;
8496 #ifndef MULTI_INSTANCE_SUPPORT
8497 	int i;
8498 #endif
8499 	pr_debug("%s\n", __func__);
8500 
8501 	if (!(is_cpu_tm2_revb() ||
8502 	(get_cpu_major_id() > AM_MESON_CPU_MAJOR_ID_TM2))) {
8503 		pr_err("unsupport av1, cpu %d, is_tm2_revb %d\n",
8504 			get_cpu_major_id(), is_cpu_tm2_revb());
8505 		return -EFAULT;
8506 	}
8507 
8508 	mutex_lock(&vav1_mutex);
8509 	hw = vzalloc(sizeof(struct AV1HW_s));
8510 	if (hw == NULL) {
8511 		av1_print(hw, 0, "\namvdec_av1 device data allocation failed\n");
8512 		mutex_unlock(&vav1_mutex);
8513 		return -ENOMEM;
8514 	}
8515 	gHevc = hw;
8516 	/*
8517 	memcpy(&BUF[0], &hw->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
8518 	memset(hw, 0, sizeof(struct AV1HW_s));
8519 	memcpy(&hw->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
8520 	*/
8521 	if (init_dblk_struc(hw) < 0) {
8522 		av1_print(hw, 0, "\nammvdec_av1 device data allocation failed\n");
8523 		vfree(hw);
8524 		return -ENOMEM;
8525 	}
8526 
8527 	pbi = av1_decoder_create(&hw->av1_buffer_pool); //&aom_decoder;
8528 	hw->pbi = pbi;
8529 	if (hw->pbi == NULL) {
8530 		pr_info("\nammvdec_av1 device data allocation failed\n");
8531 		release_dblk_struct(hw);
8532 		vfree(hw);
8533 		return -ENOMEM;
8534 	}
8535 	//hw->pbi->common.buffer_pool = &hw->av1_buffer_pool; //????
8536 	hw->pbi->private_data = hw;
8537 
8538 	hw->init_flag = 0;
8539 	hw->first_sc_checked= 0;
8540 
8541 #ifdef MULTI_INSTANCE_SUPPORT
8542 	hw->eos = 0;
8543 	hw->start_process_time = 0;
8544 	hw->timeout_num = 0;
8545 #endif
8546 	hw->fatal_error = 0;
8547 	hw->show_frame_num = 0;
8548 	if (pdata == NULL) {
8549 		av1_print(hw, 0, "\namvdec_av1 memory resource undefined.\n");
8550 		vfree(hw);
8551 		mutex_unlock(&vav1_mutex);
8552 		return -EFAULT;
8553 	}
8554 
8555 	if (pdata->sys_info) {
8556 		hw->vav1_amstream_dec_info = *pdata->sys_info;
8557 		av1_max_pic_w = (hw->vav1_amstream_dec_info.width) ?
8558 			(hw->vav1_amstream_dec_info.width) : 8192;
8559 
8560 		av1_max_pic_h = (hw->vav1_amstream_dec_info.height) ?
8561 			(hw->vav1_amstream_dec_info.height) : 4608;
8562 	} else {
8563 		hw->vav1_amstream_dec_info.width = 0;
8564 		hw->vav1_amstream_dec_info.height = 0;
8565 		hw->vav1_amstream_dec_info.rate = 30;
8566 		av1_max_pic_w = 8192;
8567 		av1_max_pic_h = 4608;
8568 	}
8569 	hw->max_pic_w = av1_max_pic_w;
8570 	hw->max_pic_h = av1_max_pic_h;
8571 
8572 	hw->m_ins_flag = 0;
8573 #ifdef MULTI_INSTANCE_SUPPORT
8574 	hw->platform_dev = pdev;
8575 	platform_set_drvdata(pdev, pdata);
8576 #endif
8577 	hw->double_write_mode = double_write_mode;
8578 	hw->mmu_enable = 1;
8579 #ifdef AOM_AV1_MMU_DW
8580 	hw->dw_mmu_enable =
8581 		get_double_write_mode_init(hw) & 0x20 ? 1 : 0;
8582 #endif
8583 	if (amvdec_av1_mmu_init(hw) < 0) {
8584 		vfree(hw);
8585 		mutex_unlock(&vav1_mutex);
8586 		pr_err("av1 alloc bmmu box failed!!\n");
8587 		return -1;
8588 	}
8589 
8590 	ret = decoder_bmmu_box_alloc_buf_phy(hw->bmmu_box, WORK_SPACE_BUF_ID,
8591 			work_buf_size, DRIVER_NAME, &pdata->mem_start);
8592 	if (ret < 0) {
8593 		uninit_mmu_buffers(hw);
8594 		vfree(hw);
8595 		mutex_unlock(&vav1_mutex);
8596 		return ret;
8597 	}
8598 	hw->buf_size = work_buf_size;
8599 
8600 #ifdef MULTI_INSTANCE_SUPPORT
8601 	hw->buf_start = pdata->mem_start;
8602 #else
8603 	if (!hw->mmu_enable)
8604 		hw->mc_buf_spec.buf_end = pdata->mem_start + hw->buf_size;
8605 
8606 	for (i = 0; i < WORK_BUF_SPEC_NUM; i++)
8607 		aom_workbuff_spec[i].start_adr = pdata->mem_start;
8608 #endif
8609 
8610 	if (debug) {
8611 		av1_print(hw, AOM_DEBUG_HW_MORE, "===AV1 decoder mem resource 0x%lx size 0x%x\n",
8612 			   pdata->mem_start, hw->buf_size);
8613 	}
8614 
8615 	hw->no_head = no_head;
8616 #ifdef MULTI_INSTANCE_SUPPORT
8617 	hw->cma_dev = pdata->cma_dev;
8618 #else
8619 	cma_dev = pdata->cma_dev;
8620 #endif
8621 
8622 #ifdef MULTI_INSTANCE_SUPPORT
8623 	pdata->private = hw;
8624 	pdata->dec_status = vav1_dec_status;
8625 	pdata->set_isreset = vav1_set_isreset;
8626 	is_reset = 0;
8627 	if (vav1_init(pdata) < 0) {
8628 #else
8629 	if (vav1_init(hw) < 0) {
8630 #endif
8631 		av1_print(hw, 0, "\namvdec_av1 init failed.\n");
8632 		av1_local_uninit(hw);
8633 		uninit_mmu_buffers(hw);
8634 		vfree(hw);
8635 		pdata->dec_status = NULL;
8636 		mutex_unlock(&vav1_mutex);
8637 		return -ENODEV;
8638 	}
8639 	/*set the max clk for smooth playing...*/
8640 	hevc_source_changed(VFORMAT_AV1, 4096, 2048, 60);
8641 	mutex_unlock(&vav1_mutex);
8642 
8643 	return 0;
8644 }
8645 
8646 static int amvdec_av1_remove(struct platform_device *pdev)
8647 {
8648 	struct AV1HW_s *hw = gHevc;
8649 	struct vdec_s *vdec = hw_to_vdec(hw);
8650 	int i;
8651 
8652 	if (debug)
8653 		av1_print(hw, AOM_DEBUG_HW_MORE, "amvdec_av1_remove\n");
8654 
8655 	mutex_lock(&vav1_mutex);
8656 
8657 	vav1_stop(hw);
8658 
8659 	hevc_source_changed(VFORMAT_AV1, 0, 0, 0);
8660 
8661 	if (vdec->parallel_dec == 1) {
8662 		for (i = 0; i < FRAME_BUFFERS; i++) {
8663 			vdec->free_canvas_ex(hw->pbi->common.buffer_pool->
8664 				frame_bufs[i].buf.y_canvas_index, vdec->id);
8665 			vdec->free_canvas_ex(hw->pbi->common.buffer_pool->
8666 				frame_bufs[i].buf.uv_canvas_index, vdec->id);
8667 		}
8668 	}
8669 
8670 #ifdef DEBUG_PTS
8671 	pr_info("pts missed %ld, pts hit %ld, duration %d\n",
8672 		   hw->pts_missed, hw->pts_hit, hw->frame_dur);
8673 #endif
8674 	vfree(hw->pbi);
8675 	release_dblk_struct(hw);
8676 	vfree(hw);
8677 	mutex_unlock(&vav1_mutex);
8678 
8679 	return 0;
8680 }
8681 
8682 /****************************************/
8683 #ifdef CONFIG_PM
8684 static int av1_suspend(struct device *dev)
8685 {
8686 	amhevc_suspend(to_platform_device(dev), dev->power.power_state);
8687 	return 0;
8688 }
8689 
8690 static int av1_resume(struct device *dev)
8691 {
8692 	amhevc_resume(to_platform_device(dev));
8693 	return 0;
8694 }
8695 
8696 static const struct dev_pm_ops av1_pm_ops = {
8697 	SET_SYSTEM_SLEEP_PM_OPS(av1_suspend, av1_resume)
8698 };
8699 #endif
8700 
8701 static struct platform_driver amvdec_av1_driver = {
8702 	.probe = amvdec_av1_probe,
8703 	.remove = amvdec_av1_remove,
8704 	.driver = {
8705 		.name = DRIVER_NAME,
8706 #ifdef CONFIG_PM
8707 		.pm = &av1_pm_ops,
8708 #endif
8709 	}
8710 };
8711 
8712 static struct codec_profile_t amvdec_av1_profile = {
8713 #ifdef DEBUG_USE_VP9_DEVICE_NAME
8714 	.name = "vp9",
8715 #else
8716 	.name = "av1",
8717 #endif
8718 	.profile = ""
8719 };
8720 
8721 static struct codec_profile_t amvdec_av1_profile_mult;
8722 
8723 static unsigned char get_data_check_sum
8724 	(struct AV1HW_s *hw, int size)
8725 {
8726 	int jj;
8727 	int sum = 0;
8728 	u8 *data = NULL;
8729 
8730 	if (!hw->chunk->block->is_mapped)
8731 		data = codec_mm_vmap(hw->chunk->block->start +
8732 			hw->chunk->offset, size);
8733 	else
8734 		data = ((u8 *)hw->chunk->block->start_virt) +
8735 			hw->chunk->offset;
8736 
8737 	for (jj = 0; jj < size; jj++)
8738 		sum += data[jj];
8739 
8740 	if (!hw->chunk->block->is_mapped)
8741 		codec_mm_unmap_phyaddr(data);
8742 	return sum;
8743 }
8744 
8745 static void dump_data(struct AV1HW_s *hw, int size)
8746 {
8747 	int jj;
8748 	u8 *data = NULL;
8749 	int padding_size = hw->chunk->offset &
8750 		(VDEC_FIFO_ALIGN - 1);
8751 
8752 	if (!hw->chunk->block->is_mapped)
8753 		data = codec_mm_vmap(hw->chunk->block->start +
8754 			hw->chunk->offset, size);
8755 	else
8756 		data = ((u8 *)hw->chunk->block->start_virt) +
8757 			hw->chunk->offset;
8758 
8759 	av1_print(hw, 0, "padding: ");
8760 	for (jj = padding_size; jj > 0; jj--)
8761 		av1_print_cont(hw,
8762 			0,
8763 			"%02x ", *(data - jj));
8764 	av1_print_cont(hw, 0, "data adr %p\n",
8765 		data);
8766 
8767 	for (jj = 0; jj < size; jj++) {
8768 		if ((jj & 0xf) == 0)
8769 			av1_print(hw,
8770 				0,
8771 				"%06x:", jj);
8772 		av1_print_cont(hw,
8773 			0,
8774 			"%02x ", data[jj]);
8775 		if (((jj + 1) & 0xf) == 0)
8776 			av1_print(hw,
8777 			 0,
8778 				"\n");
8779 	}
8780 	av1_print(hw,
8781 	 0,
8782 		"\n");
8783 
8784 	if (!hw->chunk->block->is_mapped)
8785 		codec_mm_unmap_phyaddr(data);
8786 }
8787 
8788 static void av1_work(struct work_struct *work)
8789 {
8790 	struct AV1HW_s *hw = container_of(work,
8791 		struct AV1HW_s, work);
8792 	struct vdec_s *vdec = hw_to_vdec(hw);
8793 	/* finished decoding one frame or error,
8794 	 * notify vdec core to switch context
8795 	 */
8796 	av1_print(hw, PRINT_FLAG_VDEC_DETAIL,
8797 		"%s dec_result %d %x %x %x\n",
8798 		__func__,
8799 		hw->dec_result,
8800 		READ_VREG(HEVC_STREAM_LEVEL),
8801 		READ_VREG(HEVC_STREAM_WR_PTR),
8802 		READ_VREG(HEVC_STREAM_RD_PTR));
8803 	if (((hw->dec_result == DEC_RESULT_GET_DATA) ||
8804 		(hw->dec_result == DEC_RESULT_GET_DATA_RETRY))
8805 		&& (hw_to_vdec(hw)->next_status !=
8806 		VDEC_STATUS_DISCONNECTED)) {
8807 		if (!vdec_has_more_input(vdec)) {
8808 			hw->dec_result = DEC_RESULT_EOS;
8809 			vdec_schedule_work(&hw->work);
8810 			return;
8811 		}
8812 
8813 		if (hw->dec_result == DEC_RESULT_GET_DATA) {
8814 			av1_print(hw, PRINT_FLAG_VDEC_STATUS,
8815 				"%s DEC_RESULT_GET_DATA %x %x %x\n",
8816 				__func__,
8817 				READ_VREG(HEVC_STREAM_LEVEL),
8818 				READ_VREG(HEVC_STREAM_WR_PTR),
8819 				READ_VREG(HEVC_STREAM_RD_PTR));
8820 			vdec_vframe_dirty(vdec, hw->chunk);
8821 			vdec_clean_input(vdec);
8822 		}
8823 
8824 		if (get_free_buf_count(hw) >=
8825 			run_ready_min_buf_num) {
8826 			int r;
8827 			int decode_size;
8828 			r = vdec_prepare_input(vdec, &hw->chunk);
8829 			if (r < 0) {
8830 				hw->dec_result = DEC_RESULT_GET_DATA_RETRY;
8831 
8832 				av1_print(hw,
8833 					PRINT_FLAG_VDEC_DETAIL,
8834 					"amvdec_vh265: Insufficient data\n");
8835 
8836 				vdec_schedule_work(&hw->work);
8837 				return;
8838 			}
8839 			hw->dec_result = DEC_RESULT_NONE;
8840 			av1_print(hw, PRINT_FLAG_VDEC_STATUS,
8841 				"%s: chunk size 0x%x sum 0x%x\n",
8842 				__func__, r,
8843 				(debug & PRINT_FLAG_VDEC_STATUS) ?
8844 				get_data_check_sum(hw, r) : 0
8845 				);
8846 
8847 			if (debug & PRINT_FLAG_VDEC_DATA)
8848 				dump_data(hw, hw->chunk->size);
8849 
8850 			decode_size = hw->chunk->size +
8851 				(hw->chunk->offset & (VDEC_FIFO_ALIGN - 1));
8852 
8853 			WRITE_VREG(HEVC_DECODE_SIZE,
8854 				READ_VREG(HEVC_DECODE_SIZE) + decode_size);
8855 
8856 			vdec_enable_input(vdec);
8857 
8858 			WRITE_VREG(HEVC_DEC_STATUS_REG, HEVC_ACTION_DONE);
8859 
8860 			start_process_time(hw);
8861 
8862 		} else {
8863 			hw->dec_result = DEC_RESULT_GET_DATA_RETRY;
8864 
8865 			av1_print(hw, PRINT_FLAG_VDEC_DETAIL,
8866 				"amvdec_vh265: Insufficient data\n");
8867 
8868 			vdec_schedule_work(&hw->work);
8869 		}
8870 		return;
8871 	} else if (hw->dec_result == DEC_RESULT_DONE) {
8872 		/* if (!hw->ctx_valid)
8873 			hw->ctx_valid = 1; */
8874 		hw->result_done_count++;
8875 		hw->process_state = PROC_STATE_INIT;
8876 
8877 		av1_print(hw, PRINT_FLAG_VDEC_STATUS,
8878 			"%s (===> %d) dec_result %d (%d) %x %x %x shiftbytes 0x%x decbytes 0x%x\n",
8879 			__func__,
8880 			hw->frame_count,
8881 			hw->dec_result,
8882 			hw->result_done_count,
8883 			READ_VREG(HEVC_STREAM_LEVEL),
8884 			READ_VREG(HEVC_STREAM_WR_PTR),
8885 			READ_VREG(HEVC_STREAM_RD_PTR),
8886 			READ_VREG(HEVC_SHIFT_BYTE_COUNT),
8887 			READ_VREG(HEVC_SHIFT_BYTE_COUNT) -
8888 			hw->start_shift_bytes
8889 			);
8890 		vdec_vframe_dirty(hw_to_vdec(hw), hw->chunk);
8891 	} else if (hw->dec_result == DEC_RESULT_AGAIN) {
8892 		/*
8893 			stream base: stream buf empty or timeout
8894 			frame base: vdec_prepare_input fail
8895 		*/
8896 		if (!vdec_has_more_input(vdec)) {
8897 			hw->dec_result = DEC_RESULT_EOS;
8898 			vdec_schedule_work(&hw->work);
8899 			return;
8900 		}
8901 	} else if (hw->dec_result == DEC_RESULT_EOS) {
8902 		av1_print(hw, PRINT_FLAG_VDEC_STATUS,
8903 			"%s: end of stream\n",
8904 			__func__);
8905 		hw->eos = 1;
8906 		av1_postproc(hw);
8907 
8908 		if (hw->is_used_v4l)
8909 			notify_v4l_eos(hw_to_vdec(hw));
8910 
8911 		vdec_vframe_dirty(hw_to_vdec(hw), hw->chunk);
8912 	} else if (hw->dec_result == DEC_RESULT_FORCE_EXIT) {
8913 		av1_print(hw, PRINT_FLAG_VDEC_STATUS,
8914 			"%s: force exit\n",
8915 			__func__);
8916 		if (hw->stat & STAT_VDEC_RUN) {
8917 			amhevc_stop();
8918 			hw->stat &= ~STAT_VDEC_RUN;
8919 		}
8920 
8921 		if (hw->stat & STAT_ISR_REG) {
8922 #ifdef MULTI_INSTANCE_SUPPORT
8923 			if (!hw->m_ins_flag)
8924 #endif
8925 				WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
8926 			vdec_free_irq(VDEC_IRQ_0, (void *)hw);
8927 			hw->stat &= ~STAT_ISR_REG;
8928 		}
8929 	}
8930 	if (hw->stat & STAT_VDEC_RUN) {
8931 		amhevc_stop();
8932 		hw->stat &= ~STAT_VDEC_RUN;
8933 	}
8934 
8935 	if (hw->stat & STAT_TIMER_ARM) {
8936 		del_timer_sync(&hw->timer);
8937 		hw->stat &= ~STAT_TIMER_ARM;
8938 	}
8939 	/* mark itself has all HW resource released and input released */
8940 	if (vdec->parallel_dec == 1)
8941 		vdec_core_finish_run(vdec, CORE_MASK_HEVC);
8942 	else
8943 		vdec_core_finish_run(hw_to_vdec(hw), CORE_MASK_VDEC_1
8944 					| CORE_MASK_HEVC);
8945 	trigger_schedule(hw);
8946 }
8947 
8948 static int av1_hw_ctx_restore(struct AV1HW_s *hw)
8949 {
8950 	vav1_prot_init(hw, HW_MASK_FRONT | HW_MASK_BACK);
8951 	return 0;
8952 }
8953 static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask)
8954 {
8955 	struct AV1HW_s *hw =
8956 		(struct AV1HW_s *)vdec->private;
8957 	int tvp = vdec_secure(hw_to_vdec(hw)) ?
8958 		CODEC_MM_FLAGS_TVP : 0;
8959 	unsigned long ret = 0;
8960 
8961 	if (!hw->pic_list_init_done2 || hw->eos)
8962 		return ret;
8963 	if (!hw->first_sc_checked && hw->mmu_enable) {
8964 		int size = decoder_mmu_box_sc_check(hw->mmu_box, tvp);
8965 		hw->first_sc_checked = 1;
8966 		av1_print(hw, 0, "av1 cached=%d  need_size=%d speed= %d ms\n",
8967 			size, (hw->need_cache_size >> PAGE_SHIFT),
8968 			(int)(get_jiffies_64() - hw->sc_start_time) * 1000/HZ);
8969 #ifdef AOM_AV1_MMU_DW
8970 		/*!!!!!! To do ... */
8971 		if (hw->dw_mmu_enable) {
8972 
8973 		}
8974 #endif
8975 	}
8976 
8977 	if (get_free_buf_count(hw) >=
8978 		run_ready_min_buf_num) {
8979 		if (vdec->parallel_dec == 1)
8980 			ret = CORE_MASK_HEVC;
8981 		else
8982 			ret = CORE_MASK_VDEC_1 | CORE_MASK_HEVC;
8983 		}
8984 	if (ret)
8985 		not_run_ready[hw->index] = 0;
8986 	else
8987 		not_run_ready[hw->index]++;
8988 
8989 	/*av1_print(hw,
8990 		PRINT_FLAG_VDEC_DETAIL, "%s mask %lx=>%lx\r\n",
8991 		__func__, mask, ret);*/
8992 	return ret;
8993 }
8994 
8995 static void av1_frame_mode_pts_save(struct AV1HW_s *hw)
8996 {
8997 	int i;
8998 
8999 	if (hw->chunk == NULL)
9000 		return;
9001 	av1_print(hw, AV1_DEBUG_OUT_PTS,
9002 		"run front: pts %d, pts64 %lld\n", hw->chunk->pts, hw->chunk->pts64);
9003 	for (i = (FRAME_BUFFERS - 1); i > 0; i--) {
9004 		hw->frame_mode_pts_save[i] = hw->frame_mode_pts_save[i - 1];
9005 		hw->frame_mode_pts64_save[i] = hw->frame_mode_pts64_save[i - 1];
9006 	}
9007 	hw->frame_mode_pts_save[0] = hw->chunk->pts;
9008 	hw->frame_mode_pts64_save[0] = hw->chunk->pts64;
9009 }
9010 
9011 static void run_front(struct vdec_s *vdec)
9012 {
9013 	struct AV1HW_s *hw =
9014 		(struct AV1HW_s *)vdec->private;
9015 	int ret, size;
9016 
9017 	run_count[hw->index]++;
9018 	/* hw->chunk = vdec_prepare_input(vdec); */
9019 	hevc_reset_core(vdec);
9020 
9021 	size = vdec_prepare_input(vdec, &hw->chunk);
9022 	if (size < 0) {
9023 		input_empty[hw->index]++;
9024 
9025 		hw->dec_result = DEC_RESULT_AGAIN;
9026 
9027 		av1_print(hw, PRINT_FLAG_VDEC_DETAIL,
9028 			"ammvdec_av1: Insufficient data\n");
9029 
9030 		vdec_schedule_work(&hw->work);
9031 		return;
9032 	}
9033 	input_empty[hw->index] = 0;
9034 	hw->dec_result = DEC_RESULT_NONE;
9035 	hw->start_shift_bytes = READ_VREG(HEVC_SHIFT_BYTE_COUNT);
9036 
9037 	av1_frame_mode_pts_save(hw);
9038 	if (debug & PRINT_FLAG_VDEC_STATUS) {
9039 		/*int ii;*/
9040 		av1_print(hw, 0,
9041 			"%s (%d): size 0x%x (0x%x 0x%x) sum 0x%x (%x %x %x %x %x) bytes 0x%x\n",
9042 			__func__,
9043 			hw->frame_count, size,
9044 			hw->chunk ? hw->chunk->size : 0,
9045 			hw->chunk ? hw->chunk->offset : 0,
9046 			hw->chunk ? ((vdec_frame_based(vdec) &&
9047 			(debug & PRINT_FLAG_VDEC_STATUS)) ?
9048 			get_data_check_sum(hw, size) : 0) : 0,
9049 		READ_VREG(HEVC_STREAM_START_ADDR),
9050 		READ_VREG(HEVC_STREAM_END_ADDR),
9051 		READ_VREG(HEVC_STREAM_LEVEL),
9052 		READ_VREG(HEVC_STREAM_WR_PTR),
9053 		READ_VREG(HEVC_STREAM_RD_PTR),
9054 		hw->start_shift_bytes);
9055 #if 0
9056 		if (vdec_frame_based(vdec) && hw->chunk) {
9057 			u8 *data = NULL;
9058 
9059 			if (!hw->chunk->block->is_mapped)
9060 				data = codec_mm_vmap(hw->chunk->block->start +
9061 					hw->chunk->offset, 8);
9062 			else
9063 				data = ((u8 *)hw->chunk->block->start_virt) +
9064 					hw->chunk->offset;
9065 
9066 			av1_print_cont(hw, 0, "data adr %p:",
9067 				data);
9068 			for (ii = 0; ii < 8; ii++)
9069 				av1_print_cont(hw, 0, "%02x ",
9070 					data[ii]);
9071 
9072 			if (!hw->chunk->block->is_mapped)
9073 				codec_mm_unmap_phyaddr(data);
9074 		}
9075 		av1_print_cont(hw, 0, "\r\n");
9076 #endif
9077 	}
9078 	if (vdec->mc_loaded) {
9079 	/*firmware have load before,
9080 	  and not changes to another.
9081 	  ignore reload.
9082 	*/
9083 	} else {
9084 #ifdef DEBUG_USE_VP9_DEVICE_NAME
9085 			ret = amhevc_loadmc_ex(VFORMAT_VP9, NULL, hw->fw->data);
9086 #else
9087 			ret = amhevc_loadmc_ex(VFORMAT_AV1, NULL, hw->fw->data);
9088 #endif
9089 			if (ret < 0) {
9090 			amhevc_disable();
9091 			av1_print(hw, PRINT_FLAG_ERROR,
9092 				"AV1: the %s fw loading failed, err: %x\n",
9093 				tee_enabled() ? "TEE" : "local", ret);
9094 			hw->dec_result = DEC_RESULT_FORCE_EXIT;
9095 			vdec_schedule_work(&hw->work);
9096 			return;
9097 		}
9098 		vdec->mc_loaded = 1;
9099 #ifdef DEBUG_USE_VP9_DEVICE_NAME
9100 		vdec->mc_type = VFORMAT_VP9;
9101 #else
9102 		vdec->mc_type = VFORMAT_AV1;
9103 #endif
9104 	}
9105 
9106 	if (av1_hw_ctx_restore(hw) < 0) {
9107 		vdec_schedule_work(&hw->work);
9108 		return;
9109 	}
9110 
9111 	vdec_enable_input(vdec);
9112 
9113 	WRITE_VREG(HEVC_DEC_STATUS_REG, HEVC_ACTION_DONE);
9114 
9115 	if (vdec_frame_based(vdec)) {
9116 		if (debug & PRINT_FLAG_VDEC_DATA)
9117 			dump_data(hw, hw->chunk->size);
9118 
9119 		WRITE_VREG(HEVC_SHIFT_BYTE_COUNT, 0);
9120 		size = hw->chunk->size +
9121 			(hw->chunk->offset & (VDEC_FIFO_ALIGN - 1));
9122 	}
9123 	hw->data_size = size;
9124 	WRITE_VREG(HEVC_DECODE_SIZE, size);
9125 	WRITE_VREG(HEVC_DECODE_COUNT, hw->result_done_count);
9126 	WRITE_VREG(LMEM_DUMP_ADR, (u32)hw->lmem_phy_addr);
9127 	if (hw->config_next_ref_info_flag)
9128 	    config_next_ref_info_hw(hw);
9129 	hw->config_next_ref_info_flag = 0;
9130 	hw->init_flag = 1;
9131 
9132 	av1_print(hw, PRINT_FLAG_VDEC_DETAIL,
9133 		"%s: start hw (%x %x %x) HEVC_DECODE_SIZE 0x%x\n",
9134 		__func__,
9135 		READ_VREG(HEVC_DEC_STATUS_REG),
9136 		READ_VREG(HEVC_MPC_E),
9137 		READ_VREG(HEVC_MPSR),
9138 		READ_VREG(HEVC_DECODE_SIZE));
9139 
9140 	start_process_time(hw);
9141 	mod_timer(&hw->timer, jiffies);
9142 	hw->stat |= STAT_TIMER_ARM;
9143 	hw->stat |= STAT_ISR_REG;
9144 	amhevc_start();
9145 	hw->stat |= STAT_VDEC_RUN;
9146 }
9147 
9148 static void run(struct vdec_s *vdec, unsigned long mask,
9149 	void (*callback)(struct vdec_s *, void *), void *arg)
9150 {
9151 	struct AV1HW_s *hw =
9152 		(struct AV1HW_s *)vdec->private;
9153 
9154 	av1_print(hw,
9155 		PRINT_FLAG_VDEC_DETAIL, "%s mask %lx\r\n",
9156 		__func__, mask);
9157 
9158 	run_count[hw->index]++;
9159 	hw->vdec_cb_arg = arg;
9160 	hw->vdec_cb = callback;
9161 		run_front(vdec);
9162 }
9163 
9164 static void reset(struct vdec_s *vdec)
9165 {
9166 
9167 	struct AV1HW_s *hw =
9168 		(struct AV1HW_s *)vdec->private;
9169 
9170 	av1_print(hw,
9171 		PRINT_FLAG_VDEC_DETAIL, "%s\r\n", __func__);
9172 
9173 }
9174 
9175 static irqreturn_t av1_irq_cb(struct vdec_s *vdec, int irq)
9176 {
9177 	struct AV1HW_s *hw =
9178 		(struct AV1HW_s *)vdec->private;
9179 	return vav1_isr(0, hw);
9180 }
9181 
9182 static irqreturn_t av1_threaded_irq_cb(struct vdec_s *vdec, int irq)
9183 {
9184 	struct AV1HW_s *hw =
9185 		(struct AV1HW_s *)vdec->private;
9186 	return vav1_isr_thread_fn(0, hw);
9187 }
9188 
9189 static void av1_dump_state(struct vdec_s *vdec)
9190 {
9191 	struct AV1HW_s *hw =
9192 		(struct AV1HW_s *)vdec->private;
9193 	struct AV1_Common_s *const cm = &hw->pbi->common;
9194 	int i;
9195 	av1_print(hw, 0, "====== %s\n", __func__);
9196 
9197 	av1_print(hw, 0,
9198 		"width/height (%d/%d), used_buf_num %d\n",
9199 		cm->width,
9200 		cm->height,
9201 		hw->used_buf_num
9202 		);
9203 
9204 	av1_print(hw, 0,
9205 		"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",
9206 		input_frame_based(vdec),
9207 		hw->eos,
9208 		hw->dec_result,
9209 		decode_frame_count[hw->index],
9210 		display_frame_count[hw->index],
9211 		run_count[hw->index],
9212 		not_run_ready[hw->index],
9213 		input_empty[hw->index],
9214 		hw->low_latency_flag,
9215 		hw->no_head
9216 		);
9217 
9218 	if (vf_get_receiver(vdec->vf_provider_name)) {
9219 		enum receviver_start_e state =
9220 		vf_notify_receiver(vdec->vf_provider_name,
9221 			VFRAME_EVENT_PROVIDER_QUREY_STATE,
9222 			NULL);
9223 		av1_print(hw, 0,
9224 			"\nreceiver(%s) state %d\n",
9225 			vdec->vf_provider_name,
9226 			state);
9227 	}
9228 
9229 	av1_print(hw, 0,
9230 	"%s, newq(%d/%d), dispq(%d/%d), vf prepare/get/put (%d/%d/%d), free_buf_count %d (min %d for run_ready)\n",
9231 	__func__,
9232 	kfifo_len(&hw->newframe_q),
9233 	VF_POOL_SIZE,
9234 	kfifo_len(&hw->display_q),
9235 	VF_POOL_SIZE,
9236 	hw->vf_pre_count,
9237 	hw->vf_get_count,
9238 	hw->vf_put_count,
9239 	get_free_buf_count(hw),
9240 	run_ready_min_buf_num
9241 	);
9242 
9243 	dump_pic_list(hw);
9244 
9245 	for (i = 0; i < MAX_BUF_NUM; i++) {
9246 		av1_print(hw, 0,
9247 			"mv_Buf(%d) start_adr 0x%x size 0x%x used %d\n",
9248 			i,
9249 			hw->m_mv_BUF[i].start_adr,
9250 			hw->m_mv_BUF[i].size,
9251 			hw->m_mv_BUF[i].used_flag);
9252 	}
9253 
9254 	av1_print(hw, 0,
9255 		"HEVC_DEC_STATUS_REG=0x%x\n",
9256 		READ_VREG(HEVC_DEC_STATUS_REG));
9257 	av1_print(hw, 0,
9258 		"HEVC_MPC_E=0x%x\n",
9259 		READ_VREG(HEVC_MPC_E));
9260 	av1_print(hw, 0,
9261 		"DECODE_MODE=0x%x\n",
9262 		READ_VREG(DECODE_MODE));
9263 	av1_print(hw, 0,
9264 		"NAL_SEARCH_CTL=0x%x\n",
9265 		READ_VREG(NAL_SEARCH_CTL));
9266 	av1_print(hw, 0,
9267 		"HEVC_PARSER_LCU_START=0x%x\n",
9268 		READ_VREG(HEVC_PARSER_LCU_START));
9269 	av1_print(hw, 0,
9270 		"HEVC_DECODE_SIZE=0x%x\n",
9271 		READ_VREG(HEVC_DECODE_SIZE));
9272 	av1_print(hw, 0,
9273 		"HEVC_SHIFT_BYTE_COUNT=0x%x\n",
9274 		READ_VREG(HEVC_SHIFT_BYTE_COUNT));
9275 	av1_print(hw, 0,
9276 		"HEVC_STREAM_START_ADDR=0x%x\n",
9277 		READ_VREG(HEVC_STREAM_START_ADDR));
9278 	av1_print(hw, 0,
9279 		"HEVC_STREAM_END_ADDR=0x%x\n",
9280 		READ_VREG(HEVC_STREAM_END_ADDR));
9281 	av1_print(hw, 0,
9282 		"HEVC_STREAM_LEVEL=0x%x\n",
9283 		READ_VREG(HEVC_STREAM_LEVEL));
9284 	av1_print(hw, 0,
9285 		"HEVC_STREAM_WR_PTR=0x%x\n",
9286 		READ_VREG(HEVC_STREAM_WR_PTR));
9287 	av1_print(hw, 0,
9288 		"HEVC_STREAM_RD_PTR=0x%x\n",
9289 		READ_VREG(HEVC_STREAM_RD_PTR));
9290 	av1_print(hw, 0,
9291 		"PARSER_VIDEO_RP=0x%x\n",
9292 		STBUF_READ(&vdec->vbuf, get_rp));
9293 	av1_print(hw, 0,
9294 		"PARSER_VIDEO_WP=0x%x\n",
9295 		STBUF_READ(&vdec->vbuf, get_wp));
9296 
9297 	if (input_frame_based(vdec) &&
9298 		(debug & PRINT_FLAG_VDEC_DATA)
9299 		) {
9300 		int jj;
9301 		if (hw->chunk && hw->chunk->block &&
9302 			hw->chunk->size > 0) {
9303 			u8 *data = NULL;
9304 
9305 			if (!hw->chunk->block->is_mapped)
9306 				data = codec_mm_vmap(
9307 					hw->chunk->block->start +
9308 					hw->chunk->offset,
9309 					hw->chunk->size);
9310 			else
9311 				data = ((u8 *)hw->chunk->block->start_virt)
9312 					+ hw->chunk->offset;
9313 			av1_print(hw, 0,
9314 				"frame data size 0x%x\n",
9315 				hw->chunk->size);
9316 			for (jj = 0; jj < hw->chunk->size; jj++) {
9317 				if ((jj & 0xf) == 0)
9318 					av1_print(hw, 0,
9319 						"%06x:", jj);
9320 				av1_print_cont(hw, 0,
9321 					"%02x ", data[jj]);
9322 				if (((jj + 1) & 0xf) == 0)
9323 					av1_print_cont(hw, 0,
9324 						"\n");
9325 			}
9326 
9327 			if (!hw->chunk->block->is_mapped)
9328 				codec_mm_unmap_phyaddr(data);
9329 		}
9330 	}
9331 
9332 }
9333 
9334 static int ammvdec_av1_probe(struct platform_device *pdev)
9335 {
9336 	struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
9337 	int ret;
9338 	int config_val;
9339 	struct vframe_content_light_level_s content_light_level;
9340 	struct vframe_master_display_colour_s vf_dp;
9341 
9342 	struct BUF_s BUF[MAX_BUF_NUM];
9343 	struct AV1HW_s *hw = NULL;
9344 	pr_debug("%s\n", __func__);
9345 
9346 	if (!(is_cpu_tm2_revb() ||
9347 	(get_cpu_major_id() > AM_MESON_CPU_MAJOR_ID_TM2))) {
9348 		pr_err("unsupport av1, cpu %d, is_tm2_revb %d\n",
9349 			get_cpu_major_id(), is_cpu_tm2_revb());
9350 		return -EFAULT;
9351 	}
9352 
9353 	if (pdata == NULL) {
9354 		av1_print(hw, 0, "\nammvdec_av1 memory resource undefined.\n");
9355 		return -EFAULT;
9356 	}
9357 	memset(&vf_dp, 0, sizeof(struct vframe_master_display_colour_s));
9358 
9359 	hw = vzalloc(sizeof(struct AV1HW_s));
9360 	if (hw == NULL) {
9361 		av1_print(hw, 0, "\nammvdec_av1 device data allocation failed\n");
9362 		return -ENOMEM;
9363 	}
9364 
9365 	if (init_dblk_struc(hw) < 0) {
9366 		av1_print(hw, 0, "\nammvdec_av1 device data allocation failed\n");
9367 		vfree(hw);
9368 		return -ENOMEM;
9369 	}
9370 
9371 	hw->pbi = av1_decoder_create(&hw->av1_buffer_pool); //&aom_decoder;
9372 	if (hw->pbi == NULL) {
9373 		av1_print(hw, 0, "\nammvdec_av1 device data allocation failed\n");
9374 		release_dblk_struct(hw);
9375 		vfree(hw);
9376 		return -ENOMEM;
9377 	}
9378 	hw->pbi->private_data = hw;
9379 	/* the ctx from v4l2 driver. */
9380 	hw->v4l2_ctx = pdata->private;
9381 
9382 	pdata->private = hw;
9383 	pdata->dec_status = vav1_dec_status;
9384 	/* pdata->set_trickmode = set_trickmode; */
9385 	pdata->run_ready = run_ready;
9386 	pdata->run = run;
9387 	pdata->reset = reset;
9388 	pdata->irq_handler = av1_irq_cb;
9389 	pdata->threaded_irq_handler = av1_threaded_irq_cb;
9390 	pdata->dump_state = av1_dump_state;
9391 
9392 	memcpy(&BUF[0], &hw->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
9393 	memcpy(&hw->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
9394 
9395 	hw->index = pdev->id;
9396 
9397 	if (pdata->use_vfm_path)
9398 		snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
9399 			VFM_DEC_PROVIDER_NAME);
9400 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
9401 	else if (vdec_dual(pdata)) {
9402 		struct AV1HW_s *hevc_pair = NULL;
9403 
9404 		if (dv_toggle_prov_name) /*debug purpose*/
9405 			snprintf(pdata->vf_provider_name,
9406 			VDEC_PROVIDER_NAME_SIZE,
9407 				(pdata->master) ? VFM_DEC_DVBL_PROVIDER_NAME :
9408 				VFM_DEC_DVEL_PROVIDER_NAME);
9409 		else
9410 			snprintf(pdata->vf_provider_name,
9411 			VDEC_PROVIDER_NAME_SIZE,
9412 				(pdata->master) ? VFM_DEC_DVEL_PROVIDER_NAME :
9413 				VFM_DEC_DVBL_PROVIDER_NAME);
9414 		if (pdata->master)
9415 			hevc_pair = (struct AV1HW_s *)pdata->master->private;
9416 		else if (pdata->slave)
9417 			hevc_pair = (struct AV1HW_s *)pdata->slave->private;
9418 
9419 		if (hevc_pair)
9420 			hw->shift_byte_count_lo = hevc_pair->shift_byte_count_lo;
9421 	}
9422 #endif
9423 	else
9424 		snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
9425 			MULTI_INSTANCE_PROVIDER_NAME ".%02x", pdev->id & 0xff);
9426 
9427 	vf_provider_init(&pdata->vframe_provider, pdata->vf_provider_name,
9428 		&vav1_vf_provider, hw);
9429 
9430 	hw->provider_name = pdata->vf_provider_name;
9431 	platform_set_drvdata(pdev, pdata);
9432 
9433 	hw->platform_dev = pdev;
9434 	hw->video_signal_type = 0;
9435 	hw->m_ins_flag = 1;
9436 
9437 	if (pdata->sys_info) {
9438 		hw->vav1_amstream_dec_info = *pdata->sys_info;
9439 		if ((unsigned long) hw->vav1_amstream_dec_info.param
9440 				& 0x08) {
9441 				hw->low_latency_flag = 1;
9442 			} else
9443 				hw->low_latency_flag = 0;
9444 	} else {
9445 		hw->vav1_amstream_dec_info.width = 0;
9446 		hw->vav1_amstream_dec_info.height = 0;
9447 		hw->vav1_amstream_dec_info.rate = 30;
9448 	}
9449 
9450 	if ((debug & IGNORE_PARAM_FROM_CONFIG) == 0 &&
9451 			pdata->config_len) {
9452 #ifdef MULTI_INSTANCE_SUPPORT
9453 		int av1_buf_width = 0;
9454 		int av1_buf_height = 0;
9455 		/*use ptr config for doubel_write_mode, etc*/
9456 		av1_print(hw, 0, "pdata->config=%s\n", pdata->config);
9457 		if (get_config_int(pdata->config, "av1_double_write_mode",
9458 				&config_val) == 0)
9459 			hw->double_write_mode = config_val;
9460 		else
9461 			hw->double_write_mode = double_write_mode;
9462 
9463 		if (get_config_int(pdata->config, "save_buffer_mode",
9464 				&config_val) == 0)
9465 			hw->save_buffer_mode = config_val;
9466 		else
9467 			hw->save_buffer_mode = 0;
9468 		if (get_config_int(pdata->config, "av1_buf_width",
9469 				&config_val) == 0) {
9470 			av1_buf_width = config_val;
9471 		}
9472 		if (get_config_int(pdata->config, "av1_buf_height",
9473 				&config_val) == 0) {
9474 			av1_buf_height = config_val;
9475 		}
9476 
9477 		if (get_config_int(pdata->config, "no_head",
9478 				&config_val) == 0)
9479 			hw->no_head = config_val;
9480 		else
9481 			hw->no_head = no_head;
9482 
9483 		/*use ptr config for max_pic_w, etc*/
9484 		if (get_config_int(pdata->config, "av1_max_pic_w",
9485 				&config_val) == 0) {
9486 				hw->max_pic_w = config_val;
9487 		}
9488 		if (get_config_int(pdata->config, "av1_max_pic_h",
9489 				&config_val) == 0) {
9490 				hw->max_pic_h = config_val;
9491 		}
9492 		if ((hw->max_pic_w * hw->max_pic_h)
9493 			< (av1_buf_width * av1_buf_height)) {
9494 			hw->max_pic_w = av1_buf_width;
9495 			hw->max_pic_h = av1_buf_height;
9496 			av1_print(hw, 0, "use buf resolution\n");
9497 		}
9498 #endif
9499 		if (get_config_int(pdata->config, "HDRStaticInfo",
9500 				&vf_dp.present_flag) == 0
9501 				&& vf_dp.present_flag == 1) {
9502 			get_config_int(pdata->config, "mG.x",
9503 					&vf_dp.primaries[0][0]);
9504 			get_config_int(pdata->config, "mG.y",
9505 					&vf_dp.primaries[0][1]);
9506 			get_config_int(pdata->config, "mB.x",
9507 					&vf_dp.primaries[1][0]);
9508 			get_config_int(pdata->config, "mB.y",
9509 					&vf_dp.primaries[1][1]);
9510 			get_config_int(pdata->config, "mR.x",
9511 					&vf_dp.primaries[2][0]);
9512 			get_config_int(pdata->config, "mR.y",
9513 					&vf_dp.primaries[2][1]);
9514 			get_config_int(pdata->config, "mW.x",
9515 					&vf_dp.white_point[0]);
9516 			get_config_int(pdata->config, "mW.y",
9517 					&vf_dp.white_point[1]);
9518 			get_config_int(pdata->config, "mMaxDL",
9519 					&vf_dp.luminance[0]);
9520 			get_config_int(pdata->config, "mMinDL",
9521 					&vf_dp.luminance[1]);
9522 			vf_dp.content_light_level.present_flag = 1;
9523 			get_config_int(pdata->config, "mMaxCLL",
9524 					&content_light_level.max_content);
9525 			get_config_int(pdata->config, "mMaxFALL",
9526 					&content_light_level.max_pic_average);
9527 			vf_dp.content_light_level = content_light_level;
9528 			hw->video_signal_type = (1 << 29)
9529 					| (5 << 26)	/* unspecified */
9530 					| (0 << 25)	/* limit */
9531 					| (1 << 24)	/* color available */
9532 					| (9 << 16)	/* 2020 */
9533 					| (16 << 8)	/* 2084 */
9534 					| (9 << 0);	/* 2020 */
9535 		}
9536 		hw->vf_dp = vf_dp;
9537 	} else {
9538 		/*hw->vav1_amstream_dec_info.width = 0;
9539 		hw->vav1_amstream_dec_info.height = 0;
9540 		hw->vav1_amstream_dec_info.rate = 30;*/
9541 		if (hw->vav1_amstream_dec_info.width)
9542 			hw->max_pic_w = hw->vav1_amstream_dec_info.width;
9543 		else
9544 			hw->max_pic_w = 8192;
9545 
9546 		if (hw->vav1_amstream_dec_info.height)
9547 			hw->max_pic_h = hw->vav1_amstream_dec_info.height;
9548 		else
9549 			hw->max_pic_h = 4608;
9550 		hw->double_write_mode = double_write_mode;
9551 	}
9552 	if (is_oversize(hw->max_pic_w, hw->max_pic_h)) {
9553 		pr_err("over size: %dx%d, probe failed\n",
9554 			hw->max_pic_w, hw->max_pic_h);
9555 		return -1;
9556 	}
9557 
9558 	hw->mmu_enable = 1;
9559 	video_signal_type = hw->video_signal_type;
9560 
9561 	hw->is_used_v4l = (((unsigned long)
9562 		hw->vav1_amstream_dec_info.param & 0x80) >> 7);
9563 	if (hw->is_used_v4l) {
9564 		hw->double_write_mode = 0x10;
9565 		hw->mmu_enable = 0;
9566 		hw->max_pic_w = 1920;
9567 		hw->max_pic_h = 1080;
9568 	}
9569 #ifdef AOM_AV1_MMU_DW
9570 	hw->dw_mmu_enable =
9571 		get_double_write_mode_init(hw) & 0x20 ? 1 : 0;
9572 
9573 #endif
9574 	av1_print(hw, 0,
9575 			"no_head %d  low_latency %d\n",
9576 			hw->no_head, hw->low_latency_flag);
9577 #if 0
9578 	hw->buf_start = pdata->mem_start;
9579 	hw->buf_size = pdata->mem_end - pdata->mem_start + 1;
9580 #else
9581 	if (amvdec_av1_mmu_init(hw) < 0) {
9582 		pr_err("av1 alloc bmmu box failed!!\n");
9583 		/* devm_kfree(&pdev->dev, (void *)hw); */
9584 		vfree((void *)hw);
9585 		pdata->dec_status = NULL;
9586 		return -1;
9587 	}
9588 
9589 	hw->cma_alloc_count = PAGE_ALIGN(work_buf_size) / PAGE_SIZE;
9590 	ret = decoder_bmmu_box_alloc_buf_phy(hw->bmmu_box, WORK_SPACE_BUF_ID,
9591 			hw->cma_alloc_count * PAGE_SIZE, DRIVER_NAME,
9592 			&hw->cma_alloc_addr);
9593 	if (ret < 0) {
9594 		uninit_mmu_buffers(hw);
9595 		/* devm_kfree(&pdev->dev, (void *)hw); */
9596 		vfree((void *)hw);
9597 		pdata->dec_status = NULL;
9598 		return ret;
9599 	}
9600 	hw->buf_start = hw->cma_alloc_addr;
9601 	hw->buf_size = work_buf_size;
9602 #endif
9603 
9604 	hw->init_flag = 0;
9605 	hw->first_sc_checked = 0;
9606 	hw->fatal_error = 0;
9607 	hw->show_frame_num = 0;
9608 
9609 	if (debug) {
9610 		av1_print(hw, AOM_DEBUG_HW_MORE, "===AV1 decoder mem resource 0x%lx size 0x%x\n",
9611 			   hw->buf_start,
9612 			   hw->buf_size);
9613 	}
9614 
9615 	hw->cma_dev = pdata->cma_dev;
9616 	if (vav1_init(pdata) < 0) {
9617 		av1_print(hw, 0, "\namvdec_av1 init failed.\n");
9618 		av1_local_uninit(hw);
9619 		uninit_mmu_buffers(hw);
9620 		/* devm_kfree(&pdev->dev, (void *)hw); */
9621 		vfree((void *)hw);
9622 		pdata->dec_status = NULL;
9623 		return -ENODEV;
9624 	}
9625 	vdec_set_prepare_level(pdata, start_decode_buf_level);
9626 	hevc_source_changed(VFORMAT_AV1, 4096, 2048, 60);
9627 
9628 	if (pdata->parallel_dec == 1)
9629 		vdec_core_request(pdata, CORE_MASK_HEVC);
9630 	else
9631 		vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
9632 					| CORE_MASK_COMBINE);
9633 
9634 	hw->pic_list_init_done2 = true;
9635 	return 0;
9636 }
9637 
9638 static int ammvdec_av1_remove(struct platform_device *pdev)
9639 {
9640 	struct AV1HW_s *hw = (struct AV1HW_s *)
9641 		(((struct vdec_s *)(platform_get_drvdata(pdev)))->private);
9642 	struct vdec_s *vdec = hw_to_vdec(hw);
9643 	int i;
9644 	if (debug)
9645 		av1_print(hw, AOM_DEBUG_HW_MORE, "amvdec_av1_remove\n");
9646 
9647 	vmav1_stop(hw);
9648 
9649 	if (vdec->parallel_dec == 1)
9650 		vdec_core_release(hw_to_vdec(hw), CORE_MASK_HEVC);
9651 	else
9652 		vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
9653 
9654 	vdec_set_status(hw_to_vdec(hw), VDEC_STATUS_DISCONNECTED);
9655 
9656 	if (vdec->parallel_dec == 1) {
9657 		for (i = 0; i < FRAME_BUFFERS; i++) {
9658 			vdec->free_canvas_ex
9659 				(hw->pbi->common.buffer_pool->frame_bufs[i].buf.y_canvas_index,
9660 				vdec->id);
9661 			vdec->free_canvas_ex
9662 				(hw->pbi->common.buffer_pool->frame_bufs[i].buf.uv_canvas_index,
9663 				vdec->id);
9664 		}
9665 	}
9666 
9667 
9668 #ifdef DEBUG_PTS
9669 	pr_info("pts missed %ld, pts hit %ld, duration %d\n",
9670 		   hw->pts_missed, hw->pts_hit, hw->frame_dur);
9671 #endif
9672 	/* devm_kfree(&pdev->dev, (void *)hw); */
9673 	vfree(hw->pbi);
9674 	release_dblk_struct(hw);
9675 	vfree((void *)hw);
9676 	return 0;
9677 }
9678 
9679 static struct platform_driver ammvdec_av1_driver = {
9680 	.probe = ammvdec_av1_probe,
9681 	.remove = ammvdec_av1_remove,
9682 	.driver = {
9683 		.name = MULTI_DRIVER_NAME,
9684 #ifdef CONFIG_PM
9685 		.pm = &av1_pm_ops,
9686 #endif
9687 	}
9688 };
9689 #endif
9690 static struct mconfig av1_configs[] = {
9691 	MC_PU32("bit_depth_luma", &bit_depth_luma),
9692 	MC_PU32("bit_depth_chroma", &bit_depth_chroma),
9693 	MC_PU32("frame_width", &frame_width),
9694 	MC_PU32("frame_height", &frame_height),
9695 	MC_PU32("debug", &debug),
9696 	MC_PU32("radr", &radr),
9697 	MC_PU32("rval", &rval),
9698 	MC_PU32("pop_shorts", &pop_shorts),
9699 	MC_PU32("dbg_cmd", &dbg_cmd),
9700 	MC_PU32("dbg_skip_decode_index", &dbg_skip_decode_index),
9701 	MC_PU32("endian", &endian),
9702 	MC_PU32("step", &step),
9703 	MC_PU32("udebug_flag", &udebug_flag),
9704 	MC_PU32("decode_pic_begin", &decode_pic_begin),
9705 	MC_PU32("slice_parse_begin", &slice_parse_begin),
9706 	MC_PU32("i_only_flag", &i_only_flag),
9707 	MC_PU32("error_handle_policy", &error_handle_policy),
9708 	MC_PU32("buf_alloc_width", &buf_alloc_width),
9709 	MC_PU32("buf_alloc_height", &buf_alloc_height),
9710 	MC_PU32("buf_alloc_depth", &buf_alloc_depth),
9711 	MC_PU32("buf_alloc_size", &buf_alloc_size),
9712 	MC_PU32("buffer_mode", &buffer_mode),
9713 	MC_PU32("buffer_mode_dbg", &buffer_mode_dbg),
9714 	MC_PU32("max_buf_num", &max_buf_num),
9715 	MC_PU32("dynamic_buf_num_margin", &dynamic_buf_num_margin),
9716 	MC_PU32("mem_map_mode", &mem_map_mode),
9717 	MC_PU32("double_write_mode", &double_write_mode),
9718 	MC_PU32("enable_mem_saving", &enable_mem_saving),
9719 	MC_PU32("force_w_h", &force_w_h),
9720 	MC_PU32("force_fps", &force_fps),
9721 	MC_PU32("max_decoding_time", &max_decoding_time),
9722 	MC_PU32("on_no_keyframe_skiped", &on_no_keyframe_skiped),
9723 	MC_PU32("start_decode_buf_level", &start_decode_buf_level),
9724 	MC_PU32("decode_timeout_val", &decode_timeout_val),
9725 	MC_PU32("av1_max_pic_w", &av1_max_pic_w),
9726 	MC_PU32("av1_max_pic_h", &av1_max_pic_h),
9727 };
9728 static struct mconfig_node av1_node;
9729 
9730 static int __init amvdec_av1_driver_init_module(void)
9731 {
9732 
9733 	struct BuffInfo_s *p_buf_info;
9734 #ifdef BUFMGR_ONLY_OLD_CHIP
9735 	debug |= AOM_DEBUG_BUFMGR_ONLY;
9736 #endif
9737 
9738 	if (vdec_is_support_4k()) {
9739 		if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
9740 			p_buf_info = &aom_workbuff_spec[1];
9741 		else
9742 			p_buf_info = &aom_workbuff_spec[1];
9743 	} else
9744 		p_buf_info = &aom_workbuff_spec[0];
9745 
9746 	init_buff_spec(NULL, p_buf_info);
9747 	work_buf_size =
9748 		(p_buf_info->end_adr - p_buf_info->start_adr
9749 		 + 0xffff) & (~0xffff);
9750 
9751 	pr_debug("amvdec_av1 module init\n");
9752 
9753 	error_handle_policy = 0;
9754 
9755 #ifdef ERROR_HANDLE_DEBUG
9756 	dbg_nal_skip_flag = 0;
9757 	dbg_nal_skip_count = 0;
9758 #endif
9759 	udebug_flag = 0;
9760 	decode_pic_begin = 0;
9761 	slice_parse_begin = 0;
9762 	step = 0;
9763 	buf_alloc_size = 0;
9764 #ifdef MULTI_INSTANCE_SUPPORT
9765 	if (platform_driver_register(&ammvdec_av1_driver))
9766 		pr_err("failed to register ammvdec_av1 driver\n");
9767 
9768 #endif
9769 	if (platform_driver_register(&amvdec_av1_driver)) {
9770 		pr_err("failed to register amvdec_av1 driver\n");
9771 		return -ENODEV;
9772 	}
9773 
9774 	if ((get_cpu_major_id() > AM_MESON_CPU_MAJOR_ID_TM2) || is_cpu_tm2_revb()) {
9775 		amvdec_av1_profile.profile =
9776 				"8k, 10bit, dwrite, compressed, no_head";
9777 	} else {
9778 		amvdec_av1_profile.name = "av1_unsupport";
9779 	}
9780 
9781 	vcodec_profile_register(&amvdec_av1_profile);
9782 	amvdec_av1_profile_mult = amvdec_av1_profile;
9783 #ifdef DEBUG_USE_VP9_DEVICE_NAME
9784 
9785 	amvdec_av1_profile_mult.name = "mvp9";
9786 	vcodec_profile_register(&amvdec_av1_profile_mult);
9787 	INIT_REG_NODE_CONFIGS("media.decoder", &av1_node,
9788 		"vp9", av1_configs, CONFIG_FOR_RW);
9789 
9790 #else
9791 	amvdec_av1_profile_mult.name = "mav1";
9792 	vcodec_profile_register(&amvdec_av1_profile_mult);
9793 	INIT_REG_NODE_CONFIGS("media.decoder", &av1_node,
9794 		"av1", av1_configs, CONFIG_FOR_RW);
9795 #endif
9796 
9797 	return 0;
9798 }
9799 
9800 static void __exit amvdec_av1_driver_remove_module(void)
9801 {
9802 	pr_debug("amvdec_av1 module remove.\n");
9803 #ifdef MULTI_INSTANCE_SUPPORT
9804 	platform_driver_unregister(&ammvdec_av1_driver);
9805 #endif
9806 	platform_driver_unregister(&amvdec_av1_driver);
9807 }
9808 
9809 /****************************************/
9810 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
9811 module_param(force_dv_enable, uint, 0664);
9812 MODULE_PARM_DESC(force_dv_enable, "\n amvdec_av1 force_dv_enable\n");
9813 #endif
9814 
9815 module_param(bit_depth_luma, uint, 0664);
9816 MODULE_PARM_DESC(bit_depth_luma, "\n amvdec_av1 bit_depth_luma\n");
9817 
9818 module_param(bit_depth_chroma, uint, 0664);
9819 MODULE_PARM_DESC(bit_depth_chroma, "\n amvdec_av1 bit_depth_chroma\n");
9820 
9821 module_param(frame_width, uint, 0664);
9822 MODULE_PARM_DESC(frame_width, "\n amvdec_av1 frame_width\n");
9823 
9824 module_param(frame_height, uint, 0664);
9825 MODULE_PARM_DESC(frame_height, "\n amvdec_av1 frame_height\n");
9826 
9827 module_param(multi_frames_in_one_pack, uint, 0664);
9828 MODULE_PARM_DESC(multi_frames_in_one_pack, "\n multi_frames_in_one_pack\n");
9829 
9830 module_param(debug, uint, 0664);
9831 MODULE_PARM_DESC(debug, "\n amvdec_av1 debug\n");
9832 
9833 module_param(radr, uint, 0664);
9834 MODULE_PARM_DESC(radr, "\n radr\n");
9835 
9836 module_param(rval, uint, 0664);
9837 MODULE_PARM_DESC(rval, "\n rval\n");
9838 
9839 module_param(pop_shorts, uint, 0664);
9840 MODULE_PARM_DESC(pop_shorts, "\n rval\n");
9841 
9842 module_param(dbg_cmd, uint, 0664);
9843 MODULE_PARM_DESC(dbg_cmd, "\n dbg_cmd\n");
9844 
9845 module_param(dbg_skip_decode_index, uint, 0664);
9846 MODULE_PARM_DESC(dbg_skip_decode_index, "\n dbg_skip_decode_index\n");
9847 
9848 module_param(endian, uint, 0664);
9849 MODULE_PARM_DESC(endian, "\n rval\n");
9850 
9851 module_param(step, uint, 0664);
9852 MODULE_PARM_DESC(step, "\n amvdec_av1 step\n");
9853 
9854 module_param(decode_pic_begin, uint, 0664);
9855 MODULE_PARM_DESC(decode_pic_begin, "\n amvdec_av1 decode_pic_begin\n");
9856 
9857 module_param(slice_parse_begin, uint, 0664);
9858 MODULE_PARM_DESC(slice_parse_begin, "\n amvdec_av1 slice_parse_begin\n");
9859 
9860 module_param(i_only_flag, uint, 0664);
9861 MODULE_PARM_DESC(i_only_flag, "\n amvdec_av1 i_only_flag\n");
9862 
9863 module_param(low_latency_flag, uint, 0664);
9864 MODULE_PARM_DESC(low_latency_flag, "\n amvdec_av1 low_latency_flag\n");
9865 
9866 module_param(no_head, uint, 0664);
9867 MODULE_PARM_DESC(no_head, "\n amvdec_av1 no_head\n");
9868 
9869 module_param(error_handle_policy, uint, 0664);
9870 MODULE_PARM_DESC(error_handle_policy, "\n amvdec_av1 error_handle_policy\n");
9871 
9872 module_param(buf_alloc_width, uint, 0664);
9873 MODULE_PARM_DESC(buf_alloc_width, "\n buf_alloc_width\n");
9874 
9875 module_param(buf_alloc_height, uint, 0664);
9876 MODULE_PARM_DESC(buf_alloc_height, "\n buf_alloc_height\n");
9877 
9878 module_param(buf_alloc_depth, uint, 0664);
9879 MODULE_PARM_DESC(buf_alloc_depth, "\n buf_alloc_depth\n");
9880 
9881 module_param(buf_alloc_size, uint, 0664);
9882 MODULE_PARM_DESC(buf_alloc_size, "\n buf_alloc_size\n");
9883 
9884 module_param(buffer_mode, uint, 0664);
9885 MODULE_PARM_DESC(buffer_mode, "\n buffer_mode\n");
9886 
9887 module_param(buffer_mode_dbg, uint, 0664);
9888 MODULE_PARM_DESC(buffer_mode_dbg, "\n buffer_mode_dbg\n");
9889 /*USE_BUF_BLOCK*/
9890 module_param(max_buf_num, uint, 0664);
9891 MODULE_PARM_DESC(max_buf_num, "\n max_buf_num\n");
9892 
9893 module_param(dynamic_buf_num_margin, uint, 0664);
9894 MODULE_PARM_DESC(dynamic_buf_num_margin, "\n dynamic_buf_num_margin\n");
9895 
9896 module_param(mv_buf_margin, uint, 0664);
9897 MODULE_PARM_DESC(mv_buf_margin, "\n mv_buf_margin\n");
9898 
9899 module_param(run_ready_min_buf_num, uint, 0664);
9900 MODULE_PARM_DESC(run_ready_min_buf_num, "\n run_ready_min_buf_num\n");
9901 
9902 /**/
9903 
9904 module_param(mem_map_mode, uint, 0664);
9905 MODULE_PARM_DESC(mem_map_mode, "\n mem_map_mode\n");
9906 
9907 #ifdef SUPPORT_10BIT
9908 module_param(double_write_mode, uint, 0664);
9909 MODULE_PARM_DESC(double_write_mode, "\n double_write_mode\n");
9910 
9911 module_param(enable_mem_saving, uint, 0664);
9912 MODULE_PARM_DESC(enable_mem_saving, "\n enable_mem_saving\n");
9913 
9914 module_param(force_w_h, uint, 0664);
9915 MODULE_PARM_DESC(force_w_h, "\n force_w_h\n");
9916 #endif
9917 
9918 module_param(force_fps, uint, 0664);
9919 MODULE_PARM_DESC(force_fps, "\n force_fps\n");
9920 
9921 module_param(max_decoding_time, uint, 0664);
9922 MODULE_PARM_DESC(max_decoding_time, "\n max_decoding_time\n");
9923 
9924 module_param(on_no_keyframe_skiped, uint, 0664);
9925 MODULE_PARM_DESC(on_no_keyframe_skiped, "\n on_no_keyframe_skiped\n");
9926 
9927 #ifdef MCRCC_ENABLE
9928 module_param(mcrcc_cache_alg_flag, uint, 0664);
9929 MODULE_PARM_DESC(mcrcc_cache_alg_flag, "\n mcrcc_cache_alg_flag\n");
9930 #endif
9931 
9932 #ifdef MULTI_INSTANCE_SUPPORT
9933 module_param(start_decode_buf_level, int, 0664);
9934 MODULE_PARM_DESC(start_decode_buf_level,
9935 		"\n av1 start_decode_buf_level\n");
9936 
9937 module_param(decode_timeout_val, uint, 0664);
9938 MODULE_PARM_DESC(decode_timeout_val,
9939 	"\n av1 decode_timeout_val\n");
9940 
9941 module_param(av1_max_pic_w, uint, 0664);
9942 MODULE_PARM_DESC(av1_max_pic_w, "\n av1_max_pic_w\n");
9943 
9944 module_param(av1_max_pic_h, uint, 0664);
9945 MODULE_PARM_DESC(av1_max_pic_h, "\n av1_max_pic_h\n");
9946 
9947 module_param_array(decode_frame_count, uint,
9948 	&max_decode_instance_num, 0664);
9949 
9950 module_param_array(display_frame_count, uint,
9951 	&max_decode_instance_num, 0664);
9952 
9953 module_param_array(max_process_time, uint,
9954 	&max_decode_instance_num, 0664);
9955 
9956 module_param_array(run_count, uint,
9957 	&max_decode_instance_num, 0664);
9958 
9959 module_param_array(input_empty, uint,
9960 	&max_decode_instance_num, 0664);
9961 
9962 module_param_array(not_run_ready, uint,
9963 	&max_decode_instance_num, 0664);
9964 
9965 #ifdef AOM_AV1_MMU_DW
9966 module_param_array(dw_mmu_enable, uint,
9967 	&max_decode_instance_num, 0664);
9968 #endif
9969 
9970 module_param(prefix_aux_buf_size, uint, 0664);
9971 MODULE_PARM_DESC(prefix_aux_buf_size, "\n prefix_aux_buf_size\n");
9972 
9973 module_param(suffix_aux_buf_size, uint, 0664);
9974 MODULE_PARM_DESC(suffix_aux_buf_size, "\n suffix_aux_buf_size\n");
9975 
9976 #endif
9977 
9978 #ifdef DUMP_FILMGRAIN
9979 module_param(fg_dump_index, uint, 0664);
9980 MODULE_PARM_DESC(fg_dump_index, "\n fg_dump_index\n");
9981 #endif
9982 
9983 module_param(get_picture_qos, uint, 0664);
9984 MODULE_PARM_DESC(get_picture_qos, "\n amvdec_av1 get_picture_qos\n");
9985 
9986 module_param(udebug_flag, uint, 0664);
9987 MODULE_PARM_DESC(udebug_flag, "\n amvdec_h265 udebug_flag\n");
9988 
9989 #ifdef CONFIG_AMLOGIC_MEDIA_ENHANCEMENT_DOLBYVISION
9990 module_param(dv_toggle_prov_name, uint, 0664);
9991 MODULE_PARM_DESC(dv_toggle_prov_name, "\n dv_toggle_prov_name\n");
9992 #endif
9993 
9994 module_param(udebug_pause_pos, uint, 0664);
9995 MODULE_PARM_DESC(udebug_pause_pos, "\n udebug_pause_pos\n");
9996 
9997 module_param(udebug_pause_val, uint, 0664);
9998 MODULE_PARM_DESC(udebug_pause_val, "\n udebug_pause_val\n");
9999 
10000 module_param(udebug_pause_decode_idx, uint, 0664);
10001 MODULE_PARM_DESC(udebug_pause_decode_idx, "\n udebug_pause_decode_idx\n");
10002 
10003 module_param(force_pts_unstable, uint, 0664);
10004 MODULE_PARM_DESC(force_pts_unstable, "\n force_pts_unstable\n");
10005 
10006 module_param(without_display_mode, uint, 0664);
10007 MODULE_PARM_DESC(without_display_mode, "\n without_display_mode\n");
10008 
10009 module_init(amvdec_av1_driver_init_module);
10010 module_exit(amvdec_av1_driver_remove_module);
10011 
10012 MODULE_DESCRIPTION("AMLOGIC av1 Video Decoder Driver");
10013 MODULE_LICENSE("GPL");
10014 
10015