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