summaryrefslogtreecommitdiff
path: root/drivers/frame_provider/decoder/avs2/vavs2.c (plain)
blob: 9da22f92afcdb223e1babc833f8dd0b7f87bc28c
1 /*
2 * drivers/amlogic/amports/avs2.c
3 *
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16*/
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/interrupt.h>
22#include <linux/semaphore.h>
23#include <linux/delay.h>
24#include <linux/timer.h>
25#include <linux/kfifo.h>
26#include <linux/kthread.h>
27#include <linux/spinlock.h>
28#include <linux/platform_device.h>
29#include <linux/amlogic/media/vfm/vframe.h>
30#include <linux/amlogic/media/utils/amstream.h>
31#include <linux/amlogic/media/utils/vformat.h>
32#include <linux/amlogic/media/frame_sync/ptsserv.h>
33#include <linux/amlogic/media/frame_sync/tsync.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>
40#include <linux/amlogic/tee.h>
41#include "../../../stream_input/amports/amports_priv.h"
42#include <linux/amlogic/media/codec_mm/codec_mm.h>
43#include "../utils/decoder_mmu_box.h"
44#include "../utils/decoder_bmmu_box.h"
45#include "avs2_global.h"
46
47#define MEM_NAME "codec_avs2"
48/* #include <mach/am_regs.h> */
49#include <linux/amlogic/media/utils/vdec_reg.h>
50#include "../utils/vdec.h"
51#include "../utils/amvdec.h"
52
53#include <linux/amlogic/media/video_sink/video.h>
54#include <linux/amlogic/media/codec_mm/configs.h>
55#include "../utils/config_parser.h"
56#include "../utils/firmware.h"
57#include "../../../common/chips/decoder_cpu_ver_info.h"
58#include <linux/amlogic/tee.h>
59
60
61#define I_ONLY_SUPPORT
62#define MIX_STREAM_SUPPORT
63#define G12A_BRINGUP_DEBUG
64#define CONSTRAIN_MAX_BUF_NUM
65
66#include "vavs2.h"
67#define HEVC_SHIFT_LENGTH_PROTECT 0x313a
68#define HEVC_MPRED_CTRL9 0x325b
69#define HEVC_DBLK_CFGD 0x350d
70
71
72#define HEVC_CM_HEADER_START_ADDR 0x3628
73#define HEVC_DBLK_CFGB 0x350b
74#define HEVCD_MPP_ANC2AXI_TBL_DATA 0x3464
75#define HEVC_SAO_MMU_VH1_ADDR 0x363b
76#define HEVC_SAO_MMU_VH0_ADDR 0x363a
77
78
79/*
80 * AVS2_DEC_STATUS define
81*/
82/*internal*/
83#define AVS2_DEC_IDLE 0
84#define AVS2_SEQUENCE 1
85#define AVS2_I_PICTURE 2
86#define AVS2_PB_PICTURE 3
87#define AVS2_DISCARD_STARTCODE 4
88#define AVS2_DISCARD_NAL 4
89
90#define AVS2_SLICE_DECODING 6
91
92#define SWAP_IN_CMD 0x10
93#define SWAP_OUT_CMD 0x11
94#define SWAP_OUTIN_CMD 0x12
95#define SWAP_DONE 0x13
96#define SWAP_POST_INIT 0x14
97
98/*head*/
99#define AVS2_HEAD_SEQ_READY 0x21
100#define AVS2_HEAD_PIC_I_READY 0x22
101#define AVS2_HEAD_PIC_PB_READY 0x23
102#define AVS2_HEAD_SEQ_END_READY 0x24
103#define AVS2_STARTCODE_SEARCH_DONE 0x25
104
105/*pic done*/
106#define HEVC_DECPIC_DATA_DONE 0x30
107#define HEVC_DECPIC_DATA_ERROR 0x31
108#define HEVC_NAL_DECODE_DONE 0x32
109#define AVS2_DECODE_BUFEMPTY 0x33
110#define AVS2_DECODE_TIMEOUT 0x34
111#define AVS2_DECODE_OVER_SIZE 0x35
112#define AVS2_EOS 0x36
113
114/*cmd*/
115#define AVS2_10B_DISCARD_NAL 0xf0
116#define AVS2_SEARCH_NEW_PIC 0xf1
117#define AVS2_ACTION_ERROR 0xfe
118#define HEVC_ACTION_ERROR 0xfe
119#define AVS2_ACTION_DONE 0xff
120/*AVS2_DEC_STATUS end*/
121
122
123#define VF_POOL_SIZE 32
124
125#undef pr_info
126#define pr_info printk
127
128#define DECODE_MODE_SINGLE (0 | (0x80 << 24))
129#define DECODE_MODE_MULTI_STREAMBASE (1 | (0x80 << 24))
130#define DECODE_MODE_MULTI_FRAMEBASE (2 | (0x80 << 24))
131
132
133#define VP9_TRIGGER_FRAME_DONE 0x100
134#define VP9_TRIGGER_FRAME_ENABLE 0x200
135
136/*#define MV_MEM_UNIT 0x240*/
137#define MV_MEM_UNIT 0x200
138/*---------------------------------------------------
139 Include "parser_cmd.h"
140---------------------------------------------------*/
141#define PARSER_CMD_SKIP_CFG_0 0x0000090b
142
143#define PARSER_CMD_SKIP_CFG_1 0x1b14140f
144
145#define PARSER_CMD_SKIP_CFG_2 0x001b1910
146
147
148#define PARSER_CMD_NUMBER 37
149
150static unsigned short parser_cmd[PARSER_CMD_NUMBER] = {
1510x0401,
1520x8401,
1530x0800,
1540x0402,
1550x9002,
1560x1423,
1570x8CC3,
1580x1423,
1590x8804,
1600x9825,
1610x0800,
1620x04FE,
1630x8406,
1640x8411,
1650x1800,
1660x8408,
1670x8409,
1680x8C2A,
1690x9C2B,
1700x1C00,
1710x840F,
1720x8407,
1730x8000,
1740x8408,
1750x2000,
1760xA800,
1770x8410,
1780x04DE,
1790x840C,
1800x840D,
1810xAC00,
1820xA000,
1830x08C0,
1840x08E0,
1850xA40E,
1860xFC00,
1870x7C00
188};
189
190static int32_t g_WqMDefault4x4[16] = {
191 64, 64, 64, 68,
192 64, 64, 68, 72,
193 64, 68, 76, 80,
194 72, 76, 84, 96
195};
196
197
198static int32_t g_WqMDefault8x8[64] = {
199 64, 64, 64, 64, 68, 68, 72, 76,
200 64, 64, 64, 68, 72, 76, 84, 92,
201 64, 64, 68, 72, 76, 80, 88, 100,
202 64, 68, 72, 80, 84, 92, 100, 112,
203 68, 72, 80, 84, 92, 104, 112, 128,
204 76, 80, 84, 92, 104, 116, 132, 152,
205 96, 100, 104, 116, 124, 140, 164, 188,
206 104, 108, 116, 128, 152, 172, 192, 216
207};
208/*#define HEVC_PIC_STRUCT_SUPPORT*/
209/* to remove, fix build error */
210
211/*#define CODEC_MM_FLAGS_FOR_VDECODER 0*/
212
213#define MULTI_INSTANCE_SUPPORT
214/* #define ERROR_HANDLE_DEBUG */
215
216#ifndef STAT_KTHREAD
217#define STAT_KTHREAD 0x40
218#endif
219
220#ifdef MULTI_INSTANCE_SUPPORT
221#define MAX_DECODE_INSTANCE_NUM 12
222#define MULTI_DRIVER_NAME "ammvdec_avs2"
223
224#define lock_buffer(dec, flags) \
225 spin_lock_irqsave(&dec->buffer_lock, flags)
226
227#define unlock_buffer(dec, flags) \
228 spin_unlock_irqrestore(&dec->buffer_lock, flags)
229
230static unsigned int max_decode_instance_num
231 = MAX_DECODE_INSTANCE_NUM;
232static unsigned int decode_frame_count[MAX_DECODE_INSTANCE_NUM];
233static unsigned int display_frame_count[MAX_DECODE_INSTANCE_NUM];
234static unsigned int max_process_time[MAX_DECODE_INSTANCE_NUM];
235static unsigned int run_count[MAX_DECODE_INSTANCE_NUM];
236static unsigned int input_empty[MAX_DECODE_INSTANCE_NUM];
237static unsigned int not_run_ready[MAX_DECODE_INSTANCE_NUM];
238
239#ifdef G12A_BRINGUP_DEBUG
240static u32 decode_timeout_val = 200;
241#else
242static u32 decode_timeout_val = 200;
243#endif
244static int start_decode_buf_level = 0x8000;
245#ifdef AVS2_10B_MMU
246static u32 work_buf_size; /* = 24 * 1024 * 1024*/;
247#else
248static u32 work_buf_size = 32 * 1024 * 1024;
249#endif
250
251static u32 mv_buf_margin;
252static int pre_decode_buf_level = 0x1000;
253static u32 again_threshold;
254
255
256/* DOUBLE_WRITE_MODE is enabled only when NV21 8 bit output is needed */
257/* double_write_mode:
258 * 0, no double write;
259 * 1, 1:1 ratio;
260 * 2, (1/4):(1/4) ratio;
261 * 3, (1/4):(1/4) ratio, with both compressed frame included
262 * 4, (1/2):(1/2) ratio;
263 * 0x10, double write only
264 * 0x100, if > 1080p,use mode 4,else use mode 1;
265 * 0x200, if > 1080p,use mode 2,else use mode 1;
266 * 0x300, if > 720p, use mode 4, else use mode 1;
267 */
268static u32 double_write_mode;
269static u32 without_display_mode;
270
271#define DRIVER_NAME "amvdec_avs2"
272#define MODULE_NAME "amvdec_avs2"
273#define DRIVER_HEADER_NAME "amvdec_avs2_header"
274
275
276#define PUT_INTERVAL (HZ/100)
277#define ERROR_SYSTEM_RESET_COUNT 200
278
279#define PTS_NORMAL 0
280#define PTS_NONE_REF_USE_DURATION 1
281
282#define PTS_MODE_SWITCHING_THRESHOLD 3
283#define PTS_MODE_SWITCHING_RECOVERY_THREASHOLD 3
284
285#define DUR2PTS(x) ((x)*90/96)
286
287struct AVS2Decoder_s;
288static int vavs2_vf_states(struct vframe_states *states, void *);
289static struct vframe_s *vavs2_vf_peek(void *);
290static struct vframe_s *vavs2_vf_get(void *);
291static void vavs2_vf_put(struct vframe_s *, void *);
292static int vavs2_event_cb(int type, void *data, void *private_data);
293static void set_vframe(struct AVS2Decoder_s *dec,
294 struct vframe_s *vf, struct avs2_frame_s *pic, u8 dummy);
295
296static int vavs2_stop(struct AVS2Decoder_s *dec);
297static s32 vavs2_init(struct vdec_s *vdec);
298static void vavs2_prot_init(struct AVS2Decoder_s *dec);
299static int vavs2_local_init(struct AVS2Decoder_s *dec);
300static void vavs2_put_timer_func(unsigned long arg);
301static void dump_data(struct AVS2Decoder_s *dec, int size);
302static unsigned char get_data_check_sum
303 (struct AVS2Decoder_s *dec, int size);
304static void dump_pic_list(struct AVS2Decoder_s *dec);
305
306static const char vavs2_dec_id[] = "vavs2-dev";
307
308#define PROVIDER_NAME "decoder.avs2"
309#define MULTI_INSTANCE_PROVIDER_NAME "vdec.avs2"
310
311static const struct vframe_operations_s vavs2_vf_provider = {
312 .peek = vavs2_vf_peek,
313 .get = vavs2_vf_get,
314 .put = vavs2_vf_put,
315 .event_cb = vavs2_event_cb,
316 .vf_states = vavs2_vf_states,
317};
318
319static struct vframe_provider_s vavs2_vf_prov;
320
321static u32 bit_depth_luma;
322static u32 bit_depth_chroma;
323static u32 frame_width;
324static u32 frame_height;
325static u32 video_signal_type;
326static u32 pts_unstable;
327static u32 on_no_keyframe_skiped;
328
329static u32 force_video_signal_type;
330static u32 enable_force_video_signal_type;
331#define VIDEO_SIGNAL_TYPE_AVAILABLE_MASK 0x20000000
332
333static const char * const video_format_names[] = {
334 "component", "PAL", "NTSC", "SECAM",
335 "MAC", "unspecified", "Reserved", "Reserved"
336};
337
338static inline int div_r32(int64_t m, int n)
339{
340/*
341return (int)(m/n)
342*/
343#ifndef CONFIG_ARM64
344 int64_t qu = 0;
345 qu = div_s64(m, n);
346 return (int)qu;
347#else
348 return (int)(m/n);
349#endif
350}
351
352enum vpx_bit_depth_t {
353 AVS2_BITS_8 = 8, /**< 8 bits */
354 AVS2_BITS_10 = 10, /**< 10 bits */
355 AVS2_BITS_12 = 12, /**< 12 bits */
356};
357
358/*USE_BUF_BLOCK*/
359struct BUF_s {
360 int index;
361 unsigned int alloc_flag;
362 /*buffer */
363 unsigned int cma_page_count;
364 unsigned long alloc_addr;
365 unsigned long start_adr;
366 unsigned int size;
367
368 unsigned int free_start_adr;
369} /*BUF_t */;
370
371struct MVBUF_s {
372 unsigned long start_adr;
373 unsigned int size;
374 int used_flag;
375} /*MVBUF_t */;
376
377 /* #undef BUFMGR_ONLY to enable hardware configuration */
378
379/*#define TEST_WR_PTR_INC*/
380#define WR_PTR_INC_NUM 128
381
382#define SIMULATION
383#define DOS_PROJECT
384#undef MEMORY_MAP_IN_REAL_CHIP
385
386/*#undef DOS_PROJECT*/
387/*#define MEMORY_MAP_IN_REAL_CHIP*/
388
389/*#define BUFFER_MGR_ONLY*/
390/*#define CONFIG_HEVC_CLK_FORCED_ON*/
391/*#define ENABLE_SWAP_TEST*/
392
393#ifdef AVS2_10B_NV21
394#define MEM_MAP_MODE 2 /* 0:linear 1:32x32 2:64x32*/
395#else
396#define MEM_MAP_MODE 0 /* 0:linear 1:32x32 2:64x32*/
397#endif
398
399#ifdef AVS2_10B_NV21
400#else
401#define LOSLESS_COMPRESS_MODE
402#endif
403
404#define DOUBLE_WRITE_YSTART_TEMP 0x02000000
405#define DOUBLE_WRITE_CSTART_TEMP 0x02900000
406
407
408
409typedef unsigned int u32;
410typedef unsigned short u16;
411
412#define AVS2_DBG_BUFMGR 0x01
413#define AVS2_DBG_BUFMGR_MORE 0x02
414#define AVS2_DBG_BUFMGR_DETAIL 0x04
415#define AVS2_DBG_IRQ_EVENT 0x08
416#define AVS2_DBG_OUT_PTS 0x10
417#define AVS2_DBG_PRINT_SOURCE_LINE 0x20
418#define AVS2_DBG_PRINT_PARAM 0x40
419#define AVS2_DBG_PRINT_PIC_LIST 0x80
420#define AVS2_DBG_SEND_PARAM_WITH_REG 0x100
421#define AVS2_DBG_MERGE 0x200
422#define AVS2_DBG_DBG_LF_PRINT 0x400
423#define AVS2_DBG_REG 0x800
424#define AVS2_DBG_PIC_LEAK 0x1000
425#define AVS2_DBG_PIC_LEAK_WAIT 0x2000
426#define AVS2_DBG_HDR_INFO 0x4000
427#define AVS2_DBG_DIS_LOC_ERROR_PROC 0x10000
428#define AVS2_DBG_DIS_SYS_ERROR_PROC 0x20000
429#define AVS2_DBG_DUMP_PIC_LIST 0x40000
430#define AVS2_DBG_TRIG_SLICE_SEGMENT_PROC 0x80000
431#define AVS2_DBG_FORCE_UNCOMPRESS 0x100000
432#define AVS2_DBG_LOAD_UCODE_FROM_FILE 0x200000
433#define AVS2_DBG_FORCE_SEND_AGAIN 0x400000
434#define AVS2_DBG_DUMP_DATA 0x800000
435#define AVS2_DBG_DUMP_LMEM_BUF 0x1000000
436#define AVS2_DBG_DUMP_RPM_BUF 0x2000000
437#define AVS2_DBG_CACHE 0x4000000
438#define IGNORE_PARAM_FROM_CONFIG 0x8000000
439/*MULTI_INSTANCE_SUPPORT*/
440#define PRINT_FLAG_ERROR 0
441#define PRINT_FLAG_VDEC_STATUS 0x20000000
442#define PRINT_FLAG_VDEC_DETAIL 0x40000000
443#define PRINT_FLAG_VDEC_DATA 0x80000000
444
445#define PRINT_LINE() \
446 do { \
447 if (debug & AVS2_DBG_PRINT_SOURCE_LINE)\
448 pr_info("%s line %d\n", __func__, __LINE__);\
449 } while (0)
450
451static u32 debug;
452
453static u32 debug_again;
454
455bool is_avs2_print_param(void)
456{
457 bool ret = false;
458 if (debug & AVS2_DBG_PRINT_PARAM)
459 ret = true;
460 return ret;
461}
462
463bool is_avs2_print_bufmgr_detail(void)
464{
465 bool ret = false;
466 if (debug & AVS2_DBG_BUFMGR_DETAIL)
467 ret = true;
468 return ret;
469}
470static bool is_reset;
471/*for debug*/
472/*
473 udebug_flag:
474 bit 0, enable ucode print
475 bit 1, enable ucode detail print
476 bit [31:16] not 0, pos to dump lmem
477 bit 2, pop bits to lmem
478 bit [11:8], pre-pop bits for alignment (when bit 2 is 1)
479*/
480static u32 udebug_flag;
481/*
482 when udebug_flag[1:0] is not 0
483 udebug_pause_pos not 0,
484 pause position
485*/
486static u32 udebug_pause_pos;
487/*
488 when udebug_flag[1:0] is not 0
489 and udebug_pause_pos is not 0,
490 pause only when DEBUG_REG2 is equal to this val
491*/
492static u32 udebug_pause_val;
493
494static u32 udebug_pause_decode_idx;
495
496static u32 force_disp_pic_index;
497
498#define DEBUG_REG
499#ifdef DEBUG_REG
500static void WRITE_VREG_DBG2(unsigned adr, unsigned val)
501{
502 if (debug & AVS2_DBG_REG)
503 pr_info("%s(%x, %x)\n", __func__, adr, val);
504 if (adr != 0)
505 WRITE_VREG(adr, val);
506}
507
508#undef WRITE_VREG
509#define WRITE_VREG WRITE_VREG_DBG2
510#endif
511
512
513#ifdef AVS2_10B_MMU
514#define MMU_COMPRESS_HEADER_SIZE 0x48000
515#define MMU_COMPRESS_8K_HEADER_SIZE 0x48000*4
516#endif
517
518#define INVALID_IDX -1 /* Invalid buffer index.*/
519
520
521#define FRAME_BUFFERS (AVS2_MAX_BUFFER_NUM)
522#define HEADER_FRAME_BUFFERS (FRAME_BUFFERS)
523#define MAX_BUF_NUM (FRAME_BUFFERS)
524
525#define FRAME_CONTEXTS_LOG2 2
526#define FRAME_CONTEXTS (1 << FRAME_CONTEXTS_LOG2)
527/*buffer + header buffer + workspace*/
528#ifdef MV_USE_FIXED_BUF
529#define MAX_BMMU_BUFFER_NUM ((FRAME_BUFFERS + HEADER_FRAME_BUFFERS + 1)+1)
530#define VF_BUFFER_IDX(n) (n)
531#define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n+1)
532#define WORK_SPACE_BUF_ID (FRAME_BUFFERS + HEADER_FRAME_BUFFERS+1)
533#else
534#define MAX_BMMU_BUFFER_NUM (((FRAME_BUFFERS*2)+HEADER_FRAME_BUFFERS+1)+1)
535#define VF_BUFFER_IDX(n) (n)
536#define HEADER_BUFFER_IDX(n) (FRAME_BUFFERS + n+1)
537#define MV_BUFFER_IDX(n) ((FRAME_BUFFERS * 2) + n+1)
538#define WORK_SPACE_BUF_ID ((FRAME_BUFFERS * 2) + HEADER_FRAME_BUFFERS+1)
539#endif
540/*
541static void set_canvas(struct AVS2Decoder_s *dec,
542 struct avs2_frame_s *pic);
543int avs2_prepare_display_buf(struct AVS2Decoder_s *dec,
544 int pos);
545*/
546
547
548struct buff_s {
549 u32 buf_start;
550 u32 buf_size;
551 u32 buf_end;
552};
553
554struct BuffInfo_s {
555 u32 max_width;
556 u32 max_height;
557 u32 start_adr;
558 u32 end_adr;
559 struct buff_s ipp;
560 struct buff_s sao_abv;
561 struct buff_s sao_vb;
562 struct buff_s short_term_rps;
563 struct buff_s rcs;
564 struct buff_s sps;
565 struct buff_s pps;
566 struct buff_s sao_up;
567 struct buff_s swap_buf;
568 struct buff_s swap_buf2;
569 struct buff_s scalelut;
570 struct buff_s dblk_para;
571 struct buff_s dblk_data;
572 struct buff_s dblk_data2;
573#ifdef AVS2_10B_MMU
574 struct buff_s mmu_vbh;
575 struct buff_s cm_header;
576#endif
577 struct buff_s mpred_above;
578#ifdef MV_USE_FIXED_BUF
579 struct buff_s mpred_mv;
580#endif
581 struct buff_s rpm;
582 struct buff_s lmem;
583};
584
585#define DEC_RESULT_NONE 0
586#define DEC_RESULT_DONE 1
587#define DEC_RESULT_AGAIN 2
588#define DEC_RESULT_CONFIG_PARAM 3
589#define DEC_RESULT_ERROR 4
590#define DEC_INIT_PICLIST 5
591#define DEC_UNINIT_PICLIST 6
592#define DEC_RESULT_GET_DATA 7
593#define DEC_RESULT_GET_DATA_RETRY 8
594#define DEC_RESULT_EOS 9
595#define DEC_RESULT_FORCE_EXIT 10
596
597static void avs2_work(struct work_struct *work);
598struct loop_filter_info_n;
599struct loopfilter;
600struct segmentation;
601
602struct AVS2Decoder_s {
603 int pic_list_init_flag;
604 unsigned char index;
605 spinlock_t buffer_lock;
606 struct device *cma_dev;
607 struct platform_device *platform_dev;
608 void (*vdec_cb)(struct vdec_s *, void *);
609 void *vdec_cb_arg;
610 struct vframe_chunk_s *chunk;
611 int dec_result;
612 struct work_struct work;
613 u32 start_shift_bytes;
614
615 struct BuffInfo_s work_space_buf_store;
616 unsigned long buf_start;
617 u32 buf_size;
618 u32 cma_alloc_count;
619 unsigned long cma_alloc_addr;
620 uint8_t eos;
621 unsigned long int start_process_time;
622 unsigned last_lcu_idx;
623 int decode_timeout_count;
624 unsigned timeout_num;
625
626 int double_write_mode;
627
628 unsigned char m_ins_flag;
629 char *provider_name;
630 int frame_count;
631 u32 stat;
632 struct timer_list timer;
633 u32 frame_dur;
634 u32 frame_ar;
635 int fatal_error;
636 uint8_t init_flag;
637 uint8_t first_sc_checked;
638 uint8_t process_busy;
639#define PROC_STATE_INIT 0
640#define PROC_STATE_HEAD_DONE 1
641#define PROC_STATE_DECODING 2
642#define PROC_STATE_HEAD_AGAIN 3
643#define PROC_STATE_DECODE_AGAIN 4
644#define PROC_STATE_TEST1 5
645 uint8_t process_state;
646 u32 ucode_pause_pos;
647
648 int show_frame_num;
649#ifndef AVS2_10B_MMU
650 struct buff_s mc_buf_spec;
651#endif
652 struct dec_sysinfo vavs2_amstream_dec_info;
653 void *rpm_addr;
654 void *lmem_addr;
655 dma_addr_t rpm_phy_addr;
656 dma_addr_t lmem_phy_addr;
657 unsigned short *lmem_ptr;
658 unsigned short *debug_ptr;
659
660#if 1
661 /*AVS2_10B_MMU*/
662 void *frame_mmu_map_addr;
663 dma_addr_t frame_mmu_map_phy_addr;
664#endif
665 unsigned int use_cma_flag;
666
667 struct BUF_s m_BUF[MAX_BUF_NUM];
668 struct MVBUF_s m_mv_BUF[MAX_BUF_NUM];
669 u32 used_buf_num;
670 DECLARE_KFIFO(newframe_q, struct vframe_s *, VF_POOL_SIZE);
671 DECLARE_KFIFO(display_q, struct vframe_s *, VF_POOL_SIZE);
672 DECLARE_KFIFO(pending_q, struct vframe_s *, VF_POOL_SIZE);
673 struct vframe_s vfpool[VF_POOL_SIZE];
674 u32 vf_pre_count;
675 u32 vf_get_count;
676 u32 vf_put_count;
677 int buf_num;
678 unsigned int losless_comp_body_size;
679
680 u32 video_signal_type;
681 u32 video_ori_signal_type;
682
683 int pts_mode;
684 int last_lookup_pts;
685 int last_pts;
686 u64 last_lookup_pts_us64;
687 u64 last_pts_us64;
688 u64 shift_byte_count;
689 u32 shift_byte_count_lo;
690 u32 shift_byte_count_hi;
691 int pts_mode_switching_count;
692 int pts_mode_recovery_count;
693
694 bool get_frame_dur;
695 u32 saved_resolution;
696
697 /**/
698 int refresh_frame_flags;
699 uint8_t hold_ref_buf;
700 struct BuffInfo_s *work_space_buf;
701#ifndef AVS2_10B_MMU
702 struct buff_s *mc_buf;
703#endif
704 unsigned int frame_width;
705 unsigned int frame_height;
706
707 unsigned short *rpm_ptr;
708 int init_pic_w;
709 int init_pic_h;
710
711 int slice_type;
712
713 int decode_idx;
714 int slice_idx;
715 uint8_t wait_buf;
716 uint8_t error_flag;
717 unsigned int bufmgr_error_count;
718
719 /* bit 0, for decoding; bit 1, for displaying */
720 uint8_t ignore_bufmgr_error;
721 uint8_t skip_PB_before_I;
722 int PB_skip_mode;
723 int PB_skip_count_after_decoding;
724 /*hw*/
725
726 /**/
727 struct vdec_info *gvs;
728
729
730 unsigned int dec_status;
731 u32 last_put_idx;
732 int new_frame_displayed;
733 void *mmu_box;
734 void *bmmu_box;
735 struct vframe_master_display_colour_s vf_dp;
736 struct firmware_s *fw;
737#ifdef AVS2_10B_MMU
738 int cur_fb_idx_mmu;
739 long used_4k_num;
740#endif
741 struct avs2_decoder avs2_dec;
742#define ALF_NUM_BIT_SHIFT 6
743#define NO_VAR_BINS 16
744 int32_t m_filterCoeffSym[16][9];
745 int32_t m_varIndTab[NO_VAR_BINS];
746
747 struct vframe_s vframe_dummy;
748 /* start_decoding_flag,
749 bit 0, SEQ ready
750 bit 1, I ready
751 */
752 unsigned char start_decoding_flag;
753 uint32_t mpred_abv_start_addr;
754 uint32_t mpred_abv_start_addr_bak;
755 u8 next_again_flag;
756 u32 pre_parser_wr_ptr;
757 int need_cache_size;
758 u64 sc_start_time;
759#ifdef I_ONLY_SUPPORT
760 u32 i_only;
761#endif
762 int frameinfo_enable;
763 struct vframe_qos_s vframe_qos;
764 u32 dynamic_buf_margin;
765};
766
767static int compute_losless_comp_body_size(
768 struct AVS2Decoder_s *dec, int width, int height,
769 uint8_t is_bit_depth_10);
770
771static int avs2_print(struct AVS2Decoder_s *dec,
772 int flag, const char *fmt, ...)
773{
774#define HEVC_PRINT_BUF 256
775 unsigned char buf[HEVC_PRINT_BUF];
776 int len = 0;
777 if (dec == NULL ||
778 (flag == 0) ||
779 (debug & flag)) {
780 va_list args;
781 va_start(args, fmt);
782 if (dec)
783 len = sprintf(buf, "[%d]", dec->index);
784 vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
785 pr_info("%s", buf);
786 va_end(args);
787 }
788 return 0;
789}
790
791static int avs2_print_cont(struct AVS2Decoder_s *dec,
792 int flag, const char *fmt, ...)
793{
794 unsigned char buf[HEVC_PRINT_BUF];
795 int len = 0;
796 if (dec == NULL ||
797 (flag == 0) ||
798 (debug & flag)) {
799 va_list args;
800 va_start(args, fmt);
801 vsnprintf(buf + len, HEVC_PRINT_BUF - len, fmt, args);
802 pr_info("%s", buf);
803 va_end(args);
804 }
805 return 0;
806}
807
808#define PROB_SIZE (496 * 2 * 4)
809#define PROB_BUF_SIZE (0x5000)
810#define COUNT_BUF_SIZE (0x300 * 4 * 4)
811/*compute_losless_comp_body_size(4096, 2304, 1) = 18874368(0x1200000)*/
812#define MAX_FRAME_4K_NUM 0x1200
813#define MAX_FRAME_8K_NUM 0x4800
814#define MAX_SIZE_4K (4096 * 2304)
815#define IS_8K_SIZE(w, h) (((w) * (h)) > MAX_SIZE_4K)
816
817static int get_frame_mmu_map_size(struct AVS2Decoder_s *dec)
818{
819 if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
820 (IS_8K_SIZE(dec->init_pic_w, dec->init_pic_h)))
821 return (MAX_FRAME_8K_NUM * 4);
822 return (MAX_FRAME_4K_NUM * 4);
823}
824
825static int get_compress_header_size(struct AVS2Decoder_s *dec)
826{
827 if ((get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) &&
828 (IS_8K_SIZE(dec->init_pic_w, dec->init_pic_h)))
829 return MMU_COMPRESS_8K_HEADER_SIZE;
830 return MMU_COMPRESS_HEADER_SIZE;
831}
832
833static void reset_process_time(struct AVS2Decoder_s *dec)
834{
835 if (dec->start_process_time) {
836 unsigned process_time =
837 1000 * (jiffies - dec->start_process_time) / HZ;
838 dec->start_process_time = 0;
839 if (process_time > max_process_time[dec->index])
840 max_process_time[dec->index] = process_time;
841 }
842}
843
844static void start_process_time(struct AVS2Decoder_s *dec)
845{
846 dec->start_process_time = jiffies;
847 dec->decode_timeout_count = 0;
848 dec->last_lcu_idx = 0;
849}
850
851static void update_decoded_pic(struct AVS2Decoder_s *dec);
852
853static void timeout_process(struct AVS2Decoder_s *dec)
854{
855 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
856 struct avs2_frame_s *cur_pic = avs2_dec->hc.cur_pic;
857 dec->timeout_num++;
858 amhevc_stop();
859 avs2_print(dec,
860 0, "%s decoder timeout\n", __func__);
861 if (cur_pic)
862 cur_pic->error_mark = 1;
863 dec->dec_result = DEC_RESULT_DONE;
864 update_decoded_pic(dec);
865 reset_process_time(dec);
866 vdec_schedule_work(&dec->work);
867}
868
869static u32 get_valid_double_write_mode(struct AVS2Decoder_s *dec)
870{
871 return (dec->m_ins_flag &&
872 ((double_write_mode & 0x80000000) == 0)) ?
873 dec->double_write_mode :
874 (double_write_mode & 0x7fffffff);
875}
876
877static int get_double_write_mode(struct AVS2Decoder_s *dec)
878{
879 u32 valid_dw_mode = get_valid_double_write_mode(dec);
880 int w = dec->avs2_dec.img.width;
881 int h = dec->avs2_dec.img.height;
882 u32 dw = 0x1; /*1:1*/
883 switch (valid_dw_mode) {
884 case 0x100:
885 if (w > 1920 && h > 1088)
886 dw = 0x4; /*1:2*/
887 break;
888 case 0x200:
889 if (w > 1920 && h > 1088)
890 dw = 0x2; /*1:4*/
891 break;
892 case 0x300:
893 if (w > 1280 && h > 720)
894 dw = 0x4; /*1:2*/
895 break;
896 default:
897 dw = valid_dw_mode;
898 break;
899 }
900 return dw;
901}
902
903/* for double write buf alloc */
904static int get_double_write_mode_init(struct AVS2Decoder_s *dec)
905{
906 u32 valid_dw_mode = get_valid_double_write_mode(dec);
907 u32 dw;
908 int w = dec->init_pic_w;
909 int h = dec->init_pic_h;
910
911 dw = 0x1; /*1:1*/
912 switch (valid_dw_mode) {
913 case 0x100:
914 if (w > 1920 && h > 1088)
915 dw = 0x4; /*1:2*/
916 break;
917 case 0x200:
918 if (w > 1920 && h > 1088)
919 dw = 0x2; /*1:4*/
920 break;
921 case 0x300:
922 if (w > 1280 && h > 720)
923 dw = 0x4; /*1:2*/
924 break;
925 default:
926 dw = valid_dw_mode;
927 break;
928 }
929 return dw;
930}
931
932static int get_double_write_ratio(struct AVS2Decoder_s *dec,
933 int dw_mode)
934{
935 int ratio = 1;
936 if ((dw_mode == 2) ||
937 (dw_mode == 3))
938 ratio = 4;
939 else if (dw_mode == 4)
940 ratio = 2;
941 return ratio;
942}
943
944//#define MAX_4K_NUM 0x1200
945#ifdef AVS2_10B_MMU
946int avs2_alloc_mmu(
947 struct AVS2Decoder_s *dec,
948 int cur_buf_idx,
949 int pic_width,
950 int pic_height,
951 unsigned short bit_depth,
952 unsigned int *mmu_index_adr)
953{
954 int bit_depth_10 = (bit_depth == AVS2_BITS_10);
955 int picture_size;
956 int cur_mmu_4k_number, max_frame_num;
957#ifdef DYNAMIC_ALLOC_HEAD
958 unsigned long buf_addr;
959 struct avs2_frame_s *pic = dec->avs2_dec.hc.cur_pic;
960 if (pic->header_adr == 0) {
961 if (decoder_bmmu_box_alloc_buf_phy
962 (dec->bmmu_box,
963 HEADER_BUFFER_IDX(cur_buf_idx),
964 MMU_COMPRESS_HEADER_SIZE,
965 DRIVER_HEADER_NAME,
966 &buf_addr) < 0){
967 avs2_print(dec, 0,
968 "%s malloc compress header failed %d\n",
969 DRIVER_HEADER_NAME, cur_buf_idx);
970 dec->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
971 return -1;
972 } else
973 pic->header_adr = buf_addr;
974 }
975#endif
976
977 picture_size = compute_losless_comp_body_size(
978 dec, pic_width, pic_height,
979 bit_depth_10);
980 cur_mmu_4k_number = ((picture_size + (1 << 12) - 1) >> 12);
981 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
982 max_frame_num = MAX_FRAME_8K_NUM;
983 else
984 max_frame_num = MAX_FRAME_4K_NUM;
985 if (cur_mmu_4k_number > max_frame_num) {
986 pr_err("over max !! cur_mmu_4k_number 0x%x width %d height %d\n",
987 cur_mmu_4k_number, pic_width, pic_height);
988 return -1;
989 }
990 return decoder_mmu_box_alloc_idx(
991 dec->mmu_box,
992 cur_buf_idx,
993 cur_mmu_4k_number,
994 mmu_index_adr);
995}
996#endif
997
998#if 0
999/*ndef MV_USE_FIXED_BUF*/
1000static void dealloc_mv_bufs(struct AVS2Decoder_s *dec)
1001{
1002 int i;
1003 for (i = 0; i < FRAME_BUFFERS; i++) {
1004 if (dec->m_mv_BUF[i].start_adr) {
1005 if (debug)
1006 pr_info(
1007 "dealloc mv buf(%d) adr %ld size 0x%x used_flag %d\n",
1008 i, dec->m_mv_BUF[i].start_adr,
1009 dec->m_mv_BUF[i].size,
1010 dec->m_mv_BUF[i].used_flag);
1011 decoder_bmmu_box_free_idx(
1012 dec->bmmu_box,
1013 MV_BUFFER_IDX(i));
1014 dec->m_mv_BUF[i].start_adr = 0;
1015 dec->m_mv_BUF[i].size = 0;
1016 dec->m_mv_BUF[i].used_flag = 0;
1017 }
1018 }
1019}
1020
1021static int alloc_mv_buf(struct AVS2Decoder_s *dec,
1022 int i, int size)
1023{
1024 int ret = 0;
1025 if (decoder_bmmu_box_alloc_buf_phy
1026 (dec->bmmu_box,
1027 MV_BUFFER_IDX(i), size,
1028 DRIVER_NAME,
1029 &dec->m_mv_BUF[i].start_adr) < 0) {
1030 dec->m_mv_BUF[i].start_adr = 0;
1031 ret = -1;
1032 } else {
1033 dec->m_mv_BUF[i].size = size;
1034 dec->m_mv_BUF[i].used_flag = 0;
1035 ret = 0;
1036 if (debug) {
1037 pr_info(
1038 "MV Buffer %d: start_adr %p size %x\n",
1039 i,
1040 (void *)dec->m_mv_BUF[i].start_adr,
1041 dec->m_mv_BUF[i].size);
1042 }
1043 }
1044 return ret;
1045}
1046
1047static int init_mv_buf_list(struct AVS2Decoder_s *dec)
1048{
1049 int i;
1050 int ret = 0;
1051 int count = FRAME_BUFFERS;
1052 int pic_width = dec->init_pic_w;
1053 int pic_height = dec->init_pic_h;
1054 int lcu_size = 64; /*fixed 64*/
1055 int pic_width_64 = (pic_width + 63) & (~0x3f);
1056 int pic_height_32 = (pic_height + 31) & (~0x1f);
1057 int pic_width_lcu = (pic_width_64 % lcu_size) ?
1058 pic_width_64 / lcu_size + 1
1059 : pic_width_64 / lcu_size;
1060 int pic_height_lcu = (pic_height_32 % lcu_size) ?
1061 pic_height_32 / lcu_size + 1
1062 : pic_height_32 / lcu_size;
1063 int lcu_total = pic_width_lcu * pic_height_lcu;
1064 int size = ((lcu_total * MV_MEM_UNIT) + 0xffff) &
1065 (~0xffff);
1066 if (mv_buf_margin > 0)
1067 count = dec->avs2_dec.ref_maxbuffer + mv_buf_margin;
1068 for (i = 0; i < count; i++) {
1069 if (alloc_mv_buf(dec, i, size) < 0) {
1070 ret = -1;
1071 break;
1072 }
1073 }
1074 return ret;
1075}
1076#if 0
1077
1078static int get_mv_buf(struct AVS2Decoder_s *dec,
1079 struct avs2_frame_s *pic)
1080{
1081 int i;
1082 int ret = -1;
1083 for (i = 0; i < FRAME_BUFFERS; i++) {
1084 if (dec->m_mv_BUF[i].start_adr &&
1085 dec->m_mv_BUF[i].used_flag == 0) {
1086 dec->m_mv_BUF[i].used_flag = 1;
1087 ret = i;
1088 break;
1089 }
1090 }
1091
1092 if (ret >= 0) {
1093 pic->mv_buf_index = ret;
1094 pic->mpred_mv_wr_start_addr =
1095 (dec->m_mv_BUF[ret].start_adr + 0xffff) &
1096 (~0xffff);
1097 if (debug & AVS2_DBG_BUFMGR_MORE)
1098 pr_info(
1099 "%s => %d (%d) size 0x%x\n",
1100 __func__, ret,
1101 pic->mpred_mv_wr_start_addr,
1102 dec->m_mv_BUF[ret].size);
1103 } else {
1104 pr_info(
1105 "%s: Error, mv buf is not enough\n",
1106 __func__);
1107 }
1108 return ret;
1109}
1110
1111static void put_mv_buf(struct AVS2Decoder_s *dec,
1112 struct avs2_frame_s *pic)
1113{
1114 int i = pic->mv_buf_index;
1115 if (i >= FRAME_BUFFERS) {
1116 if (debug & AVS2_DBG_BUFMGR_MORE)
1117 pr_info(
1118 "%s: index %d beyond range\n",
1119 __func__, i);
1120 return;
1121 }
1122 if (debug & AVS2_DBG_BUFMGR_MORE)
1123 pr_info(
1124 "%s(%d): used_flag(%d)\n",
1125 __func__, i,
1126 dec->m_mv_BUF[i].used_flag);
1127
1128 pic->mv_buf_index = -1;
1129 if (dec->m_mv_BUF[i].start_adr &&
1130 dec->m_mv_BUF[i].used_flag)
1131 dec->m_mv_BUF[i].used_flag = 0;
1132}
1133
1134static void put_un_used_mv_bufs(struct AVS2Decoder_s *dec)
1135{
1136 struct VP9_Common_s *const cm = &dec->common;
1137 struct RefCntBuffer_s *const frame_bufs = cm->buffer_pool->frame_bufs;
1138 int i;
1139 for (i = 0; i < dec->used_buf_num; ++i) {
1140 if ((frame_bufs[i].ref_count == 0) &&
1141 (frame_bufs[i].buf.index != -1) &&
1142 (frame_bufs[i].buf.mv_buf_index >= 0)
1143 )
1144 put_mv_buf(dec, &frame_bufs[i].buf);
1145 }
1146}
1147#endif
1148
1149#endif
1150
1151static int get_free_buf_count(struct AVS2Decoder_s *dec)
1152{
1153 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
1154 int i;
1155 int count = 0;
1156 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
1157 if ((avs2_dec->fref[i]->imgcoi_ref < -256
1158#if 0
1159 || abs(avs2_dec->fref[i]->
1160 imgtr_fwRefDistance - img->tr) >= 128
1161#endif
1162 ) && avs2_dec->fref[i]->is_output == -1
1163 && avs2_dec->fref[i]->bg_flag == 0
1164#ifndef NO_DISPLAY
1165 && avs2_dec->fref[i]->vf_ref == 0
1166 && avs2_dec->fref[i]->to_prepare_disp == 0
1167#endif
1168 ) {
1169 count++;
1170 }
1171 }
1172
1173 return count;
1174}
1175
1176#ifdef CONSTRAIN_MAX_BUF_NUM
1177static int get_vf_ref_only_buf_count(struct AVS2Decoder_s *dec)
1178{
1179 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
1180 int i;
1181 int count = 0;
1182 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
1183 if ((avs2_dec->fref[i]->imgcoi_ref < -256
1184#if 0
1185 || abs(avs2_dec->fref[i]->
1186 imgtr_fwRefDistance - img->tr) >= 128
1187#endif
1188 ) && avs2_dec->fref[i]->is_output == -1
1189 && avs2_dec->fref[i]->bg_flag == 0
1190#ifndef NO_DISPLAY
1191 && avs2_dec->fref[i]->vf_ref > 0
1192 && avs2_dec->fref[i]->to_prepare_disp == 0
1193#endif
1194 ) {
1195 count++;
1196 }
1197 }
1198
1199 return count;
1200}
1201
1202static int get_used_buf_count(struct AVS2Decoder_s *dec)
1203{
1204 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
1205 int i;
1206 int count = 0;
1207 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
1208 if ((avs2_dec->fref[i]->imgcoi_ref >= -256
1209#if 0
1210 || abs(avs2_dec->fref[i]->
1211 imgtr_fwRefDistance - img->tr) >= 128
1212#endif
1213 ) || avs2_dec->fref[i]->is_output != -1
1214 || avs2_dec->fref[i]->bg_flag != 0
1215#ifndef NO_DISPLAY
1216 || avs2_dec->fref[i]->vf_ref != 0
1217 || avs2_dec->fref[i]->to_prepare_disp != 0
1218#endif
1219 ) {
1220 count++;
1221 }
1222 }
1223
1224 return count;
1225}
1226#endif
1227
1228int avs2_bufmgr_init(struct AVS2Decoder_s *dec, struct BuffInfo_s *buf_spec_i,
1229 struct buff_s *mc_buf_i) {
1230
1231 dec->frame_count = 0;
1232#ifdef AVS2_10B_MMU
1233 dec->used_4k_num = -1;
1234 dec->cur_fb_idx_mmu = INVALID_IDX;
1235#endif
1236
1237
1238 /* private init */
1239 dec->work_space_buf = buf_spec_i;
1240#ifndef AVS2_10B_MMU
1241 dec->mc_buf = mc_buf_i;
1242#endif
1243 dec->rpm_addr = NULL;
1244 dec->lmem_addr = NULL;
1245
1246 dec->use_cma_flag = 0;
1247 dec->decode_idx = 0;
1248 dec->slice_idx = 0;
1249 /*int m_uiMaxCUWidth = 1<<7;*/
1250 /*int m_uiMaxCUHeight = 1<<7;*/
1251 dec->wait_buf = 0;
1252 dec->error_flag = 0;
1253 dec->skip_PB_before_I = 0;
1254
1255 dec->pts_mode = PTS_NORMAL;
1256 dec->last_pts = 0;
1257 dec->last_lookup_pts = 0;
1258 dec->last_pts_us64 = 0;
1259 dec->last_lookup_pts_us64 = 0;
1260 dec->shift_byte_count = 0;
1261 dec->shift_byte_count_lo = 0;
1262 dec->shift_byte_count_hi = 0;
1263 dec->pts_mode_switching_count = 0;
1264 dec->pts_mode_recovery_count = 0;
1265
1266 dec->buf_num = 0;
1267
1268 dec->bufmgr_error_count = 0;
1269 return 0;
1270}
1271
1272
1273
1274#define HEVC_CM_BODY_START_ADDR 0x3626
1275#define HEVC_CM_BODY_LENGTH 0x3627
1276#define HEVC_CM_HEADER_LENGTH 0x3629
1277#define HEVC_CM_HEADER_OFFSET 0x362b
1278
1279#define LOSLESS_COMPRESS_MODE
1280
1281/*#define DECOMP_HEADR_SURGENT*/
1282
1283static u32 mem_map_mode; /* 0:linear 1:32x32 2:64x32 ; m8baby test1902 */
1284static u32 enable_mem_saving = 1;
1285static u32 force_w_h;
1286
1287static u32 force_fps;
1288
1289
1290const u32 avs2_version = 201602101;
1291static u32 debug;
1292static u32 radr;
1293static u32 rval;
1294static u32 pop_shorts;
1295static u32 dbg_cmd;
1296static u32 dbg_skip_decode_index;
1297static u32 endian = 0xff0;
1298#ifdef ERROR_HANDLE_DEBUG
1299static u32 dbg_nal_skip_flag;
1300 /* bit[0], skip vps; bit[1], skip sps; bit[2], skip pps */
1301static u32 dbg_nal_skip_count;
1302#endif
1303/*for debug*/
1304static u32 decode_pic_begin;
1305static uint slice_parse_begin;
1306static u32 step;
1307#ifdef MIX_STREAM_SUPPORT
1308static u32 buf_alloc_width = 4096;
1309static u32 buf_alloc_height = 2304;
1310
1311static u32 dynamic_buf_num_margin;
1312#else
1313static u32 buf_alloc_width;
1314static u32 buf_alloc_height;
1315static u32 dynamic_buf_num_margin = 7;
1316#endif
1317#ifdef CONSTRAIN_MAX_BUF_NUM
1318static u32 run_ready_max_vf_only_num;
1319static u32 run_ready_display_q_num;
1320 /*0: not check
1321 0xff: avs2_dec.ref_maxbuffer
1322 */
1323static u32 run_ready_max_buf_num = 0xff;
1324#endif
1325static u32 buf_alloc_depth = 10;
1326static u32 buf_alloc_size;
1327/*
1328bit[0]: 0,
1329 bit[1]: 0, always release cma buffer when stop
1330 bit[1]: 1, never release cma buffer when stop
1331bit[0]: 1, when stop, release cma buffer if blackout is 1;
1332do not release cma buffer is blackout is not 1
1333
1334bit[2]: 0, when start decoding, check current displayed buffer
1335 (only for buffer decoded by vp9) if blackout is 0
1336 1, do not check current displayed buffer
1337
1338bit[3]: 1, if blackout is not 1, do not release current
1339 displayed cma buffer always.
1340*/
1341/* set to 1 for fast play;
1342 set to 8 for other case of "keep last frame"
1343*/
1344static u32 buffer_mode = 1;
1345/* buffer_mode_dbg: debug only*/
1346static u32 buffer_mode_dbg = 0xffff0000;
1347/**/
1348
1349/*
1350bit 0, 1: only display I picture;
1351bit 1, 1: only decode I picture;
1352*/
1353static u32 i_only_flag;
1354
1355
1356static u32 max_decoding_time;
1357/*
1358error handling
1359*/
1360/*error_handle_policy:
1361bit 0: search seq again if buffer mgr error occur
1362 (buffer mgr error count need big than
1363 re_search_seq_threshold)
1364bit 1: 1, display from I picture;
1365 0, display from any correct pic
1366*/
1367
1368static u32 error_handle_policy = 1;
1369/*
1370re_search_seq_threshold:
1371 bit 7~0: buffer mgr error research seq count
1372 bit 15~8: frame count threshold
1373*/
1374static u32 re_search_seq_threshold = 0x800; /*0x8;*/
1375/*static u32 parser_sei_enable = 1;*/
1376
1377static u32 max_buf_num = (REF_BUFFER + 1);
1378
1379static u32 run_ready_min_buf_num = 2;
1380
1381static DEFINE_MUTEX(vavs2_mutex);
1382
1383#define HEVC_DEC_STATUS_REG HEVC_ASSIST_SCRATCH_0
1384#define HEVC_RPM_BUFFER HEVC_ASSIST_SCRATCH_1
1385#define AVS2_ALF_SWAP_BUFFER HEVC_ASSIST_SCRATCH_2
1386#define HEVC_RCS_BUFFER HEVC_ASSIST_SCRATCH_3
1387#define HEVC_SPS_BUFFER HEVC_ASSIST_SCRATCH_4
1388#define HEVC_PPS_BUFFER HEVC_ASSIST_SCRATCH_5
1389#define HEVC_SAO_UP HEVC_ASSIST_SCRATCH_6
1390#ifdef AVS2_10B_MMU
1391#define AVS2_MMU_MAP_BUFFER HEVC_ASSIST_SCRATCH_7
1392#else
1393#define HEVC_STREAM_SWAP_BUFFER HEVC_ASSIST_SCRATCH_7
1394#endif
1395#define HEVC_STREAM_SWAP_BUFFER2 HEVC_ASSIST_SCRATCH_8
1396/*
1397#define VP9_PROB_SWAP_BUFFER HEVC_ASSIST_SCRATCH_9
1398#define VP9_COUNT_SWAP_BUFFER HEVC_ASSIST_SCRATCH_A
1399#define VP9_SEG_MAP_BUFFER HEVC_ASSIST_SCRATCH_B
1400*/
1401#define HEVC_SCALELUT HEVC_ASSIST_SCRATCH_D
1402#define HEVC_WAIT_FLAG HEVC_ASSIST_SCRATCH_E
1403#define RPM_CMD_REG HEVC_ASSIST_SCRATCH_F
1404#define LMEM_DUMP_ADR HEVC_ASSIST_SCRATCH_9
1405#define HEVC_STREAM_SWAP_TEST HEVC_ASSIST_SCRATCH_L
1406/*!!!*/
1407#define HEVC_DECODE_COUNT HEVC_ASSIST_SCRATCH_M
1408#define HEVC_DECODE_SIZE HEVC_ASSIST_SCRATCH_N
1409#define DEBUG_REG1 HEVC_ASSIST_SCRATCH_G
1410#define DEBUG_REG2 HEVC_ASSIST_SCRATCH_H
1411
1412
1413/*
1414ucode parser/search control
1415bit 0: 0, header auto parse; 1, header manual parse
1416bit 1: 0, auto skip for noneseamless stream; 1, no skip
1417bit [3:2]: valid when bit1==0;
14180, auto skip nal before first vps/sps/pps/idr;
14191, auto skip nal before first vps/sps/pps
14202, auto skip nal before first vps/sps/pps,
1421 and not decode until the first I slice (with slice address of 0)
1422
14233, auto skip before first I slice (nal_type >=16 && nal_type<=21)
1424bit [15:4] nal skip count (valid when bit0 == 1 (manual mode) )
1425bit [16]: for NAL_UNIT_EOS when bit0 is 0:
1426 0, send SEARCH_DONE to arm ; 1, do not send SEARCH_DONE to arm
1427bit [17]: for NAL_SEI when bit0 is 0:
1428 0, do not parse SEI in ucode; 1, parse SEI in ucode
1429bit [31:20]: used by ucode for debug purpose
1430*/
1431#define NAL_SEARCH_CTL HEVC_ASSIST_SCRATCH_I
1432 /*DECODE_MODE: set before start decoder
1433 bit 7~0: decode mode
1434 bit 23~16: start_decoding_flag
1435 bit [0] - SEQ_ready
1436 bit [2:1] - I Picture Count
1437 bit 31~24: chip feature
1438 */
1439#define DECODE_MODE HEVC_ASSIST_SCRATCH_J
1440#define DECODE_STOP_POS HEVC_ASSIST_SCRATCH_K
1441 /*read only*/
1442#define CUR_NAL_UNIT_TYPE HEVC_ASSIST_SCRATCH_J
1443
1444#define RPM_BUF_SIZE (0x600 * 2)
1445#define LMEM_BUF_SIZE (0x600 * 2)
1446
1447#define WORK_BUF_SPEC_NUM 3
1448static struct BuffInfo_s amvavs2_workbuff_spec[WORK_BUF_SPEC_NUM] = {
1449 {
1450 /* 8M bytes */
1451 .max_width = 1920,
1452 .max_height = 1088,
1453 .ipp = {
1454 /* IPP work space calculation :
1455 4096 * (Y+CbCr+Flags) = 12k, round to 16k */
1456 .buf_size = 0x4000,
1457 },
1458 .sao_abv = {
1459 .buf_size = 0x30000,
1460 },
1461 .sao_vb = {
1462 .buf_size = 0x30000,
1463 },
1464 .short_term_rps = {
1465 /* SHORT_TERM_RPS - Max 64 set, 16 entry every set,
1466 total 64x16x2 = 2048 bytes (0x800) */
1467 .buf_size = 0x800,
1468 },
1469 .rcs = {
1470 /* RCS STORE AREA - Max 32 RCS, each has 32 bytes,
1471 total 0x0400 bytes */
1472 .buf_size = 0x400,
1473 },
1474 .sps = {
1475 /* SPS STORE AREA - Max 16 SPS, each has 0x80 bytes,
1476 total 0x0800 bytes*/
1477 .buf_size = 0x800,
1478 },
1479 .pps = {
1480 /*PPS STORE AREA - Max 64 PPS, each has 0x80 bytes,
1481 total 0x2000 bytes*/
1482 .buf_size = 0x2000,
1483 },
1484 .sao_up = {
1485 /* SAO UP STORE AREA - Max 640(10240/16) LCU,
1486 each has 16 bytes total 0x2800 bytes */
1487 .buf_size = 0x2800,
1488 },
1489 .swap_buf = {
1490 /* 256cyclex64bit = 2K bytes 0x800
1491 (only 144 cycles valid) */
1492 .buf_size = 0x800,
1493 },
1494 .swap_buf2 = {
1495 .buf_size = 0x800,
1496 },
1497 .scalelut = {
1498 /* support up to 32 SCALELUT 1024x32 =
1499 32Kbytes (0x8000) */
1500 .buf_size = 0x8000,
1501 },
1502 .dblk_para = {
1503 /* DBLK -> Max 256(4096/16) LCU,
1504 each para 1024bytes(total:0x40000),
1505 data 1024bytes(total:0x40000)*/
1506 .buf_size = 0x40000,
1507 },
1508 .dblk_data = {
1509 .buf_size = 0x40000,
1510 },
1511 .dblk_data2 = {
1512 .buf_size = 0x40000,
1513 },
1514#ifdef AVS2_10B_MMU
1515 .mmu_vbh = {
1516 .buf_size = 0x5000, /*2*16*(more than 2304)/4, 4K*/
1517 },
1518#if 0
1519 .cm_header = {
1520 /*add one for keeper.*/
1521 .buf_size = MMU_COMPRESS_HEADER_SIZE *
1522 (FRAME_BUFFERS + 1),
1523 /* 0x44000 = ((1088*2*1024*4)/32/4)*(32/8) */
1524 },
1525#endif
1526#endif
1527 .mpred_above = {
1528 .buf_size = 0x8000, /* 2 * size of hevc*/
1529 },
1530#ifdef MV_USE_FIXED_BUF
1531 .mpred_mv = {/* 1080p, 0x40000 per buffer */
1532 .buf_size = 0x40000 * FRAME_BUFFERS,
1533 },
1534#endif
1535 .rpm = {
1536 .buf_size = RPM_BUF_SIZE,
1537 },
1538 .lmem = {
1539 .buf_size = 0x400 * 2,
1540 }
1541 },
1542 {
1543 .max_width = 4096,
1544 .max_height = 2304,
1545 .ipp = {
1546 /* IPP work space calculation :
1547 4096 * (Y+CbCr+Flags) = 12k, round to 16k */
1548 .buf_size = 0x4000,
1549 },
1550 .sao_abv = {
1551 .buf_size = 0x30000,
1552 },
1553 .sao_vb = {
1554 .buf_size = 0x30000,
1555 },
1556 .short_term_rps = {
1557 /* SHORT_TERM_RPS - Max 64 set, 16 entry every set,
1558 total 64x16x2 = 2048 bytes (0x800) */
1559 .buf_size = 0x800,
1560 },
1561 .rcs = {
1562 /* RCS STORE AREA - Max 16 RCS, each has 32 bytes,
1563 total 0x0400 bytes */
1564 .buf_size = 0x400,
1565 },
1566 .sps = {
1567 /* SPS STORE AREA - Max 16 SPS, each has 0x80 bytes,
1568 total 0x0800 bytes */
1569 .buf_size = 0x800,
1570 },
1571 .pps = {
1572 /* PPS STORE AREA - Max 64 PPS, each has 0x80 bytes,
1573 total 0x2000 bytes */
1574 .buf_size = 0x2000,
1575 },
1576 .sao_up = {
1577 /* SAO UP STORE AREA - Max 640(10240/16) LCU,
1578 each has 16 bytes total 0x2800 bytes */
1579 .buf_size = 0x2800,
1580 },
1581 .swap_buf = {
1582 /* 256cyclex64bit = 2K bytes 0x800
1583 (only 144 cycles valid) */
1584 .buf_size = 0x800,
1585 },
1586 .swap_buf2 = {
1587 .buf_size = 0x800,
1588 },
1589 .scalelut = {
1590 /* support up to 32 SCALELUT 1024x32 = 32Kbytes
1591 (0x8000) */
1592 .buf_size = 0x8000,
1593 },
1594 .dblk_para = {
1595 /* DBLK -> Max 256(4096/16) LCU,
1596 each para 1024bytes(total:0x40000),
1597 data 1024bytes(total:0x40000)*/
1598 .buf_size = 0x80000,
1599 },
1600 .dblk_data = {
1601 /*DBLK -> Max 256(4096/16) LCU,
1602 each para 1024bytes(total:0x40000),
1603 data 1024bytes(total:0x40000)*/
1604 .buf_size = 0x80000,
1605 },
1606 .dblk_data2 = {
1607 .buf_size = 0x80000,
1608 },
1609#ifdef AVS2_10B_MMU
1610 .mmu_vbh = {
1611 .buf_size = 0x5000,/*2*16*(more than 2304)/4, 4K*/
1612 },
1613#if 0
1614 .cm_header = {
1615 /*add one for keeper.*/
1616 .buf_size = MMU_COMPRESS_HEADER_SIZE *
1617 (FRAME_BUFFERS + 1),
1618 /* 0x44000 = ((1088*2*1024*4)/32/4)*(32/8) */
1619 },
1620#endif
1621#endif
1622 .mpred_above = {
1623 .buf_size = 0x10000, /* 2 * size of hevc*/
1624 },
1625#ifdef MV_USE_FIXED_BUF
1626 .mpred_mv = {
1627 /* .buf_size = 0x100000*16,
1628 //4k2k , 0x100000 per buffer */
1629 /* 4096x2304 , 0x120000 per buffer */
1630 .buf_size = 0x120000 * FRAME_BUFFERS,
1631 },
1632#endif
1633 .rpm = {
1634 .buf_size = RPM_BUF_SIZE,
1635 },
1636 .lmem = {
1637 .buf_size = 0x400 * 2,
1638 }
1639 },
1640 {
1641 .max_width = 4096 * 2,
1642 .max_height = 2304 * 2,
1643 .ipp = {
1644 /*IPP work space calculation : 4096 * (Y+CbCr+Flags) = 12k,
1645 round to 16k*/
1646 .buf_size = 0x4000 * 2,
1647 },
1648 .sao_abv = {
1649 .buf_size = 0x30000 * 2,
1650 },
1651 .sao_vb = {
1652 .buf_size = 0x30000 * 2,
1653 },
1654 .short_term_rps = {
1655 /*SHORT_TERM_RPS - Max 64 set, 16 entry every set,
1656 total 64x16x2 = 2048 bytes (0x800)*/
1657 .buf_size = 0x800,
1658 },
1659 .rcs = {
1660 /*RCS STORE AREA - Max 16 RCS, each has 32 bytes,
1661 total 0x0400 bytes*/
1662 .buf_size = 0x400,
1663 },
1664 .sps = {
1665 /*SPS STORE AREA - Max 16 SPS, each has 0x80 bytes,
1666 total 0x0800 bytes*/
1667 .buf_size = 0x800,
1668 },
1669 .pps = {
1670 /*PPS STORE AREA - Max 64 PPS, each has 0x80 bytes, total
1671 0x2000 bytes*/
1672 .buf_size = 0x2000,
1673 },
1674 .sao_up = {
1675 /*SAO UP STORE AREA - Max 640(10240/16) LCU, each has 16 bytes i
1676 total 0x2800 bytes*/
1677 .buf_size = 0x2800 * 2,
1678 },
1679 .swap_buf = {
1680 /*256cyclex64bit = 2K bytes 0x800 (only 144 cycles valid)*/
1681 .buf_size = 0x800,
1682 },
1683 .swap_buf2 = {
1684 .buf_size = 0x800,
1685 },
1686 .scalelut = {
1687 /*support up to 32 SCALELUT 1024x32 = 32Kbytes (0x8000)*/
1688 .buf_size = 0x8000 * 2,
1689 },
1690 .dblk_para = {
1691 .buf_size = 0x40000 * 2,
1692 },
1693 .dblk_data = {
1694 .buf_size = 0x80000 * 2,
1695 },
1696 .dblk_data2 = {
1697 .buf_size = 0x80000 * 2,
1698 },
1699#ifdef AVS2_10B_MMU
1700 .mmu_vbh = {
1701 .buf_size = 0x5000 * 2, /*2*16*2304/4, 4K*/
1702 },
1703#if 0
1704 .cm_header = {
1705 /*0x44000 = ((1088*2*1024*4)/32/4)*(32/8)*/
1706 .buf_size = MMU_COMPRESS_8K_HEADER_SIZE * 17,
1707 },
1708#endif
1709#endif
1710 .mpred_above = {
1711 .buf_size = 0x8000 * 2,
1712 },
1713#ifdef MV_USE_FIXED_BUF
1714 .mpred_mv = {
1715 /*4k2k , 0x100000 per buffer*/
1716 .buf_size = 0x120000 * FRAME_BUFFERS * 4,
1717 },
1718#endif
1719 .rpm = {
1720 .buf_size = RPM_BUF_SIZE,
1721 },
1722 .lmem = {
1723 .buf_size = 0x400 * 2,
1724 }
1725 }
1726};
1727
1728
1729/*Losless compression body buffer size 4K per 64x32 (jt)*/
1730static int compute_losless_comp_body_size(struct AVS2Decoder_s *dec,
1731 int width, int height,
1732 uint8_t is_bit_depth_10)
1733{
1734 int width_x64;
1735 int height_x32;
1736 int bsize;
1737 width_x64 = width + 63;
1738 width_x64 >>= 6;
1739 height_x32 = height + 31;
1740 height_x32 >>= 5;
1741#ifdef AVS2_10B_MMU
1742 bsize = (is_bit_depth_10 ? 4096 : 3200)
1743 * width_x64 * height_x32;
1744#else
1745 bsize = (is_bit_depth_10 ? 4096 : 3072)
1746 * width_x64 * height_x32;
1747#endif
1748 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
1749 "%s(%d,%d,%d)=>%d\n",
1750 __func__, width, height,
1751 is_bit_depth_10, bsize);
1752
1753 return bsize;
1754}
1755
1756/* Losless compression header buffer size 32bytes per 128x64 (jt)*/
1757static int compute_losless_comp_header_size(struct AVS2Decoder_s *dec,
1758 int width, int height)
1759{
1760 int width_x128;
1761 int height_x64;
1762 int hsize;
1763 width_x128 = width + 127;
1764 width_x128 >>= 7;
1765 height_x64 = height + 63;
1766 height_x64 >>= 6;
1767
1768 hsize = 32 * width_x128 * height_x64;
1769 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
1770 "%s(%d,%d)=>%d\n",
1771 __func__, width, height,
1772 hsize);
1773
1774 return hsize;
1775}
1776
1777static void init_buff_spec(struct AVS2Decoder_s *dec,
1778 struct BuffInfo_s *buf_spec)
1779{
1780 void *mem_start_virt;
1781 buf_spec->ipp.buf_start = buf_spec->start_adr;
1782 buf_spec->sao_abv.buf_start =
1783 buf_spec->ipp.buf_start + buf_spec->ipp.buf_size;
1784
1785 buf_spec->sao_vb.buf_start =
1786 buf_spec->sao_abv.buf_start + buf_spec->sao_abv.buf_size;
1787 buf_spec->short_term_rps.buf_start =
1788 buf_spec->sao_vb.buf_start + buf_spec->sao_vb.buf_size;
1789 buf_spec->rcs.buf_start =
1790 buf_spec->short_term_rps.buf_start +
1791 buf_spec->short_term_rps.buf_size;
1792 buf_spec->sps.buf_start =
1793 buf_spec->rcs.buf_start + buf_spec->rcs.buf_size;
1794 buf_spec->pps.buf_start =
1795 buf_spec->sps.buf_start + buf_spec->sps.buf_size;
1796 buf_spec->sao_up.buf_start =
1797 buf_spec->pps.buf_start + buf_spec->pps.buf_size;
1798 buf_spec->swap_buf.buf_start =
1799 buf_spec->sao_up.buf_start + buf_spec->sao_up.buf_size;
1800 buf_spec->swap_buf2.buf_start =
1801 buf_spec->swap_buf.buf_start + buf_spec->swap_buf.buf_size;
1802 buf_spec->scalelut.buf_start =
1803 buf_spec->swap_buf2.buf_start + buf_spec->swap_buf2.buf_size;
1804 buf_spec->dblk_para.buf_start =
1805 buf_spec->scalelut.buf_start + buf_spec->scalelut.buf_size;
1806 buf_spec->dblk_data.buf_start =
1807 buf_spec->dblk_para.buf_start + buf_spec->dblk_para.buf_size;
1808 buf_spec->dblk_data2.buf_start =
1809 buf_spec->dblk_data.buf_start + buf_spec->dblk_data.buf_size;
1810#ifdef AVS2_10B_MMU
1811 buf_spec->mmu_vbh.buf_start =
1812 buf_spec->dblk_data2.buf_start + buf_spec->dblk_data2.buf_size;
1813 buf_spec->mpred_above.buf_start =
1814 buf_spec->mmu_vbh.buf_start + buf_spec->mmu_vbh.buf_size;
1815#else
1816 buf_spec->mpred_above.buf_start =
1817 buf_spec->dblk_data2.buf_start + buf_spec->dblk_data2.buf_size;
1818#endif
1819#ifdef MV_USE_FIXED_BUF
1820 buf_spec->mpred_mv.buf_start =
1821 buf_spec->mpred_above.buf_start +
1822 buf_spec->mpred_above.buf_size;
1823
1824 buf_spec->rpm.buf_start =
1825 buf_spec->mpred_mv.buf_start +
1826 buf_spec->mpred_mv.buf_size;
1827#else
1828 buf_spec->rpm.buf_start =
1829 buf_spec->mpred_above.buf_start +
1830 buf_spec->mpred_above.buf_size;
1831
1832#endif
1833 buf_spec->lmem.buf_start =
1834 buf_spec->rpm.buf_start +
1835 buf_spec->rpm.buf_size;
1836 buf_spec->end_adr =
1837 buf_spec->lmem.buf_start +
1838 buf_spec->lmem.buf_size;
1839
1840 if (dec) {
1841 mem_start_virt =
1842 codec_mm_phys_to_virt(buf_spec->dblk_para.buf_start);
1843 if (mem_start_virt) {
1844 memset(mem_start_virt, 0, buf_spec->dblk_para.buf_size);
1845 codec_mm_dma_flush(mem_start_virt,
1846 buf_spec->dblk_para.buf_size,
1847 DMA_TO_DEVICE);
1848 } else {
1849 /*not virt for tvp playing,
1850 may need clear on ucode.*/
1851 pr_err("mem_start_virt failed\n");
1852 }
1853 if (debug) {
1854 pr_info("%s workspace (%x %x) size = %x\n", __func__,
1855 buf_spec->start_adr, buf_spec->end_adr,
1856 buf_spec->end_adr - buf_spec->start_adr);
1857 }
1858 if (debug) {
1859 pr_info("ipp.buf_start :%x\n",
1860 buf_spec->ipp.buf_start);
1861 pr_info("sao_abv.buf_start :%x\n",
1862 buf_spec->sao_abv.buf_start);
1863 pr_info("sao_vb.buf_start :%x\n",
1864 buf_spec->sao_vb.buf_start);
1865 pr_info("short_term_rps.buf_start :%x\n",
1866 buf_spec->short_term_rps.buf_start);
1867 pr_info("rcs.buf_start :%x\n",
1868 buf_spec->rcs.buf_start);
1869 pr_info("sps.buf_start :%x\n",
1870 buf_spec->sps.buf_start);
1871 pr_info("pps.buf_start :%x\n",
1872 buf_spec->pps.buf_start);
1873 pr_info("sao_up.buf_start :%x\n",
1874 buf_spec->sao_up.buf_start);
1875 pr_info("swap_buf.buf_start :%x\n",
1876 buf_spec->swap_buf.buf_start);
1877 pr_info("swap_buf2.buf_start :%x\n",
1878 buf_spec->swap_buf2.buf_start);
1879 pr_info("scalelut.buf_start :%x\n",
1880 buf_spec->scalelut.buf_start);
1881 pr_info("dblk_para.buf_start :%x\n",
1882 buf_spec->dblk_para.buf_start);
1883 pr_info("dblk_data.buf_start :%x\n",
1884 buf_spec->dblk_data.buf_start);
1885 pr_info("dblk_data2.buf_start :%x\n",
1886 buf_spec->dblk_data2.buf_start);
1887 #ifdef AVS2_10B_MMU
1888 pr_info("mmu_vbh.buf_start :%x\n",
1889 buf_spec->mmu_vbh.buf_start);
1890 #endif
1891 pr_info("mpred_above.buf_start :%x\n",
1892 buf_spec->mpred_above.buf_start);
1893#ifdef MV_USE_FIXED_BUF
1894 pr_info("mpred_mv.buf_start :%x\n",
1895 buf_spec->mpred_mv.buf_start);
1896#endif
1897 if ((debug & AVS2_DBG_SEND_PARAM_WITH_REG) == 0) {
1898 pr_info("rpm.buf_start :%x\n",
1899 buf_spec->rpm.buf_start);
1900 }
1901 }
1902 }
1903
1904}
1905
1906static void uninit_mmu_buffers(struct AVS2Decoder_s *dec)
1907{
1908#if 0
1909/*ndef MV_USE_FIXED_BUF*/
1910 dealloc_mv_bufs(dec);
1911#endif
1912 decoder_mmu_box_free(dec->mmu_box);
1913 dec->mmu_box = NULL;
1914
1915 if (dec->bmmu_box)
1916 decoder_bmmu_box_free(dec->bmmu_box);
1917 dec->bmmu_box = NULL;
1918}
1919
1920#ifndef AVS2_10B_MMU
1921static void init_buf_list(struct AVS2Decoder_s *dec)
1922{
1923 int i;
1924 int buf_size;
1925 int mc_buffer_end = dec->mc_buf->buf_start + dec->mc_buf->buf_size;
1926 dec->used_buf_num = max_buf_num;
1927
1928 if (dec->used_buf_num > MAX_BUF_NUM)
1929 dec->used_buf_num = MAX_BUF_NUM;
1930 if (buf_alloc_size > 0) {
1931 buf_size = buf_alloc_size;
1932 avs2_print(dec, AVS2_DBG_BUFMGR,
1933 "[Buffer Management] init_buf_list:\n");
1934 } else {
1935 int pic_width = dec->init_pic_w;
1936 int pic_height = dec->init_pic_h;
1937
1938 /*SUPPORT_10BIT*/
1939 int losless_comp_header_size = compute_losless_comp_header_size
1940 (dec, pic_width, pic_height);
1941 int losless_comp_body_size = compute_losless_comp_body_size
1942 (dec, pic_width, pic_height, buf_alloc_depth == 10);
1943 int mc_buffer_size = losless_comp_header_size
1944 + losless_comp_body_size;
1945 int mc_buffer_size_h = (mc_buffer_size + 0xffff)>>16;
1946
1947 int dw_mode = get_double_write_mode_init(dec);
1948
1949 if (dw_mode) {
1950 int pic_width_dw = pic_width /
1951 get_double_write_ratio(dec, dw_mode);
1952 int pic_height_dw = pic_height /
1953 get_double_write_ratio(dec, dw_mode);
1954 int lcu_size = 64; /*fixed 64*/
1955 int pic_width_64 = (pic_width_dw + 63) & (~0x3f);
1956 int pic_height_32 = (pic_height_dw + 31) & (~0x1f);
1957 int pic_width_lcu =
1958 (pic_width_64 % lcu_size) ? pic_width_64 / lcu_size
1959 + 1 : pic_width_64 / lcu_size;
1960 int pic_height_lcu =
1961 (pic_height_32 % lcu_size) ? pic_height_32 / lcu_size
1962 + 1 : pic_height_32 / lcu_size;
1963 int lcu_total = pic_width_lcu * pic_height_lcu;
1964 int mc_buffer_size_u_v = lcu_total * lcu_size * lcu_size / 2;
1965 int mc_buffer_size_u_v_h = (mc_buffer_size_u_v + 0xffff) >> 16;
1966 /*64k alignment*/
1967 buf_size = ((mc_buffer_size_u_v_h << 16) * 3);
1968 } else
1969 buf_size = 0;
1970
1971 if (mc_buffer_size & 0xffff) { /*64k alignment*/
1972 mc_buffer_size_h += 1;
1973 }
1974 if ((dw_mode & 0x10) == 0)
1975 buf_size += (mc_buffer_size_h << 16);
1976 avs2_print(dec, AVS2_DBG_BUFMGR,
1977 "init_buf_list num %d (width %d height %d):\n",
1978 dec->used_buf_num, pic_width, pic_height);
1979 }
1980
1981 for (i = 0; i < dec->used_buf_num; i++) {
1982 if (((i + 1) * buf_size) > dec->mc_buf->buf_size)
1983 dec->use_cma_flag = 1;
1984#ifndef AVS2_10B_MMU
1985 dec->m_BUF[i].alloc_flag = 0;
1986 dec->m_BUF[i].index = i;
1987
1988 dec->use_cma_flag = 1;
1989 if (dec->use_cma_flag) {
1990 dec->m_BUF[i].cma_page_count =
1991 PAGE_ALIGN(buf_size) / PAGE_SIZE;
1992 if (decoder_bmmu_box_alloc_buf_phy(dec->bmmu_box,
1993 VF_BUFFER_IDX(i), buf_size, DRIVER_NAME,
1994 &dec->m_BUF[i].alloc_addr) < 0) {
1995 dec->m_BUF[i].cma_page_count = 0;
1996 if (i <= 5) {
1997 dec->fatal_error |=
1998 DECODER_FATAL_ERROR_NO_MEM;
1999 }
2000 break;
2001 }
2002 dec->m_BUF[i].start_adr = dec->m_BUF[i].alloc_addr;
2003 } else {
2004 dec->m_BUF[i].cma_page_count = 0;
2005 dec->m_BUF[i].alloc_addr = 0;
2006 dec->m_BUF[i].start_adr =
2007 dec->mc_buf->buf_start + i * buf_size;
2008 }
2009 dec->m_BUF[i].size = buf_size;
2010 dec->m_BUF[i].free_start_adr = dec->m_BUF[i].start_adr;
2011
2012 if (((dec->m_BUF[i].start_adr + buf_size) > mc_buffer_end)
2013 && (dec->m_BUF[i].alloc_addr == 0)) {
2014 if (debug) {
2015 avs2_print(dec, 0,
2016 "Max mc buffer or mpred_mv buffer is used\n");
2017 }
2018 break;
2019 }
2020
2021 avs2_print(dec, AVS2_DBG_BUFMGR,
2022 "Buffer %d: start_adr %p size %x\n", i,
2023 (void *)dec->m_BUF[i].start_adr,
2024 dec->m_BUF[i].size);
2025#endif
2026 }
2027 dec->buf_num = i;
2028}
2029#endif
2030
2031static int config_pic(struct AVS2Decoder_s *dec,
2032 struct avs2_frame_s *pic, int32_t lcu_size_log2)
2033{
2034 int ret = -1;
2035 int i;
2036 int pic_width = dec->init_pic_w;
2037 int pic_height = dec->init_pic_h;
2038 /*struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2039 int32_t lcu_size_log2 = avs2_dec->lcu_size_log2;*/
2040 int32_t lcu_size = 1 << lcu_size_log2;
2041 int pic_width_64 = (pic_width + 63) & (~0x3f);
2042 int pic_height_32 = (pic_height + 31) & (~0x1f);
2043 int pic_width_lcu = (pic_width_64 % lcu_size) ?
2044 pic_width_64 / lcu_size + 1
2045 : pic_width_64 / lcu_size;
2046 int pic_height_lcu = (pic_height_32 % lcu_size) ?
2047 pic_height_32 / lcu_size + 1
2048 : pic_height_32 / lcu_size;
2049 int lcu_total = pic_width_lcu * pic_height_lcu;
2050#if 0
2051 int32_t MV_MEM_UNIT =
2052 (lcu_size_log2 == 6) ? 0x200 :
2053 ((lcu_size_log2 == 5) ? 0x80 : 0x20);
2054#endif
2055#ifdef MV_USE_FIXED_BUF
2056 u32 mpred_mv_end = dec->work_space_buf->mpred_mv.buf_start +
2057 dec->work_space_buf->mpred_mv.buf_size;
2058#endif
2059 u32 y_adr = 0;
2060 int buf_size = 0;
2061
2062 int losless_comp_header_size =
2063 compute_losless_comp_header_size(
2064 dec, pic_width, pic_height);
2065 int losless_comp_body_size = compute_losless_comp_body_size(
2066 dec, pic_width,
2067 pic_height, buf_alloc_depth == 10);
2068 int mc_buffer_size = losless_comp_header_size + losless_comp_body_size;
2069 int mc_buffer_size_h = (mc_buffer_size + 0xffff) >> 16;
2070 int mc_buffer_size_u_v = 0;
2071 int mc_buffer_size_u_v_h = 0;
2072 int dw_mode = get_double_write_mode_init(dec);
2073
2074 if (dw_mode) {
2075 int pic_width_dw = pic_width /
2076 get_double_write_ratio(dec, dw_mode);
2077 int pic_height_dw = pic_height /
2078 get_double_write_ratio(dec, dw_mode);
2079 int pic_width_64_dw = (pic_width_dw + 63) & (~0x3f);
2080 int pic_height_32_dw = (pic_height_dw + 31) & (~0x1f);
2081 int pic_width_lcu_dw = (pic_width_64_dw % lcu_size) ?
2082 pic_width_64_dw / lcu_size + 1
2083 : pic_width_64_dw / lcu_size;
2084 int pic_height_lcu_dw = (pic_height_32_dw % lcu_size) ?
2085 pic_height_32_dw / lcu_size + 1
2086 : pic_height_32_dw / lcu_size;
2087 int lcu_total_dw = pic_width_lcu_dw * pic_height_lcu_dw;
2088
2089 mc_buffer_size_u_v = lcu_total_dw * lcu_size * lcu_size / 2;
2090 mc_buffer_size_u_v_h = (mc_buffer_size_u_v + 0xffff) >> 16;
2091 /*64k alignment*/
2092 buf_size = ((mc_buffer_size_u_v_h << 16) * 3);
2093 buf_size = ((buf_size + 0xffff) >> 16) << 16;
2094 }
2095 if (mc_buffer_size & 0xffff) /*64k alignment*/
2096 mc_buffer_size_h += 1;
2097#ifndef AVS2_10B_MMU
2098 if ((dw_mode & 0x10) == 0)
2099 buf_size += (mc_buffer_size_h << 16);
2100#endif
2101
2102#ifdef AVS2_10B_MMU
2103#ifndef DYNAMIC_ALLOC_HEAD
2104 pic->header_adr = decoder_bmmu_box_get_phy_addr(
2105 dec->bmmu_box, HEADER_BUFFER_IDX(pic->index));
2106
2107 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
2108 "buf_size %d, MMU header_adr %d: %ld\n",
2109 buf_size, pic->index, pic->header_adr);
2110#endif
2111#endif
2112
2113 i = pic->index;
2114#ifdef MV_USE_FIXED_BUF
2115#ifdef G12A_BRINGUP_DEBUG
2116 if (1) {
2117#else
2118 if ((dec->work_space_buf->mpred_mv.buf_start +
2119 (((i + 1) * lcu_total) * MV_MEM_UNIT))
2120 <= mpred_mv_end
2121 ) {
2122#endif
2123#endif
2124#ifndef AVS2_10B_MMU
2125 if (debug) {
2126 pr_err("start %x .size=%d\n",
2127 dec->mc_buf_spec.buf_start + i * buf_size,
2128 buf_size);
2129 }
2130#endif
2131#ifndef AVS2_10B_MMU
2132 for (i = 0; i < dec->buf_num; i++) {
2133 y_adr = ((dec->m_BUF[i].free_start_adr
2134 + 0xffff) >> 16) << 16;
2135 /*64k alignment*/
2136 if ((y_adr+buf_size) <= (dec->m_BUF[i].start_adr+
2137 dec->m_BUF[i].size)) {
2138 dec->m_BUF[i].free_start_adr =
2139 y_adr + buf_size;
2140 break;
2141 }
2142 }
2143 if (i < dec->buf_num)
2144#else
2145 /*if ((dec->mc_buf->buf_start + (i + 1) * buf_size) <
2146 dec->mc_buf->buf_end)
2147 y_adr = dec->mc_buf->buf_start + i * buf_size;
2148 else {*/
2149 if (buf_size > 0 && pic->cma_alloc_addr == 0) {
2150 ret = decoder_bmmu_box_alloc_buf_phy(dec->bmmu_box,
2151 VF_BUFFER_IDX(i),
2152 buf_size, DRIVER_NAME,
2153 &pic->cma_alloc_addr);
2154 if (ret < 0) {
2155 avs2_print(dec, 0,
2156 "decoder_bmmu_box_alloc_buf_phy idx %d size %d fail\n",
2157 VF_BUFFER_IDX(i),
2158 buf_size
2159 );
2160 return ret;
2161 }
2162
2163 if (pic->cma_alloc_addr)
2164 y_adr = pic->cma_alloc_addr;
2165 else {
2166 avs2_print(dec, 0,
2167 "decoder_bmmu_box_alloc_buf_phy idx %d size %d return null\n",
2168 VF_BUFFER_IDX(i),
2169 buf_size
2170 );
2171 return -1;
2172 }
2173 }
2174#endif
2175 {
2176 /*ensure get_pic_by_POC()
2177 not get the buffer not decoded*/
2178 pic->BUF_index = i;
2179 pic->lcu_total = lcu_total;
2180
2181 pic->comp_body_size = losless_comp_body_size;
2182 pic->buf_size = buf_size;
2183#ifndef AVS2_10B_MMU
2184 pic->mc_y_adr = y_adr;
2185#endif
2186 pic->mc_canvas_y = pic->index;
2187 pic->mc_canvas_u_v = pic->index;
2188#ifndef AVS2_10B_MMU
2189 if (dw_mode & 0x10) {
2190 pic->mc_u_v_adr = y_adr +
2191 ((mc_buffer_size_u_v_h << 16) << 1);
2192
2193 pic->mc_canvas_y =
2194 (pic->index << 1);
2195 pic->mc_canvas_u_v =
2196 (pic->index << 1) + 1;
2197
2198 pic->dw_y_adr = y_adr;
2199 pic->dw_u_v_adr = pic->mc_u_v_adr;
2200 } else
2201#endif
2202 if (dw_mode) {
2203 pic->dw_y_adr = y_adr
2204#ifndef AVS2_10B_MMU
2205 + (mc_buffer_size_h << 16)
2206#endif
2207 ;
2208 pic->dw_u_v_adr = pic->dw_y_adr +
2209 ((mc_buffer_size_u_v_h << 16) << 1);
2210#ifdef AVS2_10B_MMU
2211 pic->mc_y_adr = pic->dw_y_adr;
2212 pic->mc_u_v_adr = pic->dw_u_v_adr;
2213#endif
2214 }
2215#ifdef MV_USE_FIXED_BUF
2216#ifdef G12A_BRINGUP_DEBUG
2217 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
2218 pic->mpred_mv_wr_start_addr =
2219 dec->work_space_buf->mpred_mv.buf_start +
2220 (pic->index * 0x120000 * 4);
2221 } else {
2222 pic->mpred_mv_wr_start_addr =
2223 dec->work_space_buf->mpred_mv.buf_start +
2224 (pic->index * 0x120000);
2225 }
2226#else
2227 pic->mpred_mv_wr_start_addr =
2228 dec->work_space_buf->mpred_mv.buf_start +
2229 ((pic->index * lcu_total)
2230 * MV_MEM_UNIT);
2231#endif
2232#endif
2233 if (debug) {
2234 avs2_print(dec, AVS2_DBG_BUFMGR,
2235 "%s index %d BUF_index %d mc_y_adr %x ",
2236 __func__, pic->index,
2237 pic->BUF_index,
2238 pic->mc_y_adr);
2239 avs2_print_cont(dec, AVS2_DBG_BUFMGR,
2240 "comp_body_size %x comp_buf_size %x ",
2241 pic->comp_body_size,
2242 pic->buf_size);
2243 avs2_print_cont(dec, AVS2_DBG_BUFMGR,
2244 "mpred_mv_wr_start_adr %d\n",
2245 pic->mpred_mv_wr_start_addr);
2246 avs2_print_cont(dec, AVS2_DBG_BUFMGR,
2247 "dw_y_adr %d, pic->dw_u_v_adr =%d\n",
2248 pic->dw_y_adr,
2249 pic->dw_u_v_adr);
2250 }
2251 ret = 0;
2252 }
2253#ifdef MV_USE_FIXED_BUF
2254 } else {
2255 avs2_print(dec, 0,
2256 "mv buffer alloc fail %x > %x\n",
2257 dec->work_space_buf->mpred_mv.buf_start +
2258 (((i + 1) * lcu_total) * MV_MEM_UNIT),
2259 mpred_mv_end);
2260 }
2261#endif
2262 return ret;
2263}
2264
2265static void init_pic_list(struct AVS2Decoder_s *dec,
2266 int32_t lcu_size_log2)
2267{
2268 int i;
2269 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2270 struct avs2_frame_s *pic;
2271#ifdef AVS2_10B_MMU
2272 unsigned long buf_addr1;
2273 /*alloc AVS2 compress header first*/
2274 if (decoder_bmmu_box_alloc_buf_phy
2275 (dec->bmmu_box,
2276 HEADER_BUFFER_IDX(-1), get_compress_header_size(dec),
2277 DRIVER_HEADER_NAME,
2278 &buf_addr1) < 0){
2279 avs2_print(dec, 0,
2280 "%s malloc compress header failed %d\n",
2281 DRIVER_HEADER_NAME, -1);
2282 dec->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
2283 return;
2284 }
2285#ifndef DYNAMIC_ALLOC_HEAD
2286 for (i = 0; i < dec->used_buf_num; i++) {
2287 unsigned long buf_addr;
2288 if (decoder_bmmu_box_alloc_buf_phy
2289 (dec->bmmu_box,
2290 HEADER_BUFFER_IDX(i), get_compress_header_size(dec),
2291 DRIVER_HEADER_NAME,
2292 &buf_addr) < 0){
2293 avs2_print(dec, 0,
2294 "%s malloc compress header failed %d\n",
2295 DRIVER_HEADER_NAME, i);
2296 dec->fatal_error |= DECODER_FATAL_ERROR_NO_MEM;
2297 return;
2298 }
2299 }
2300#endif
2301#endif
2302 dec->frame_height = avs2_dec->img.height;
2303 dec->frame_width = avs2_dec->img.width;
2304
2305 for (i = 0; i < dec->used_buf_num; i++) {
2306 if (i == (dec->used_buf_num - 1))
2307 pic = avs2_dec->m_bg;
2308 else
2309 pic = avs2_dec->fref[i];
2310 pic->index = i;
2311 pic->BUF_index = -1;
2312 pic->mv_buf_index = -1;
2313 if (config_pic(dec, pic, lcu_size_log2) < 0) {
2314 if (debug)
2315 avs2_print(dec, 0,
2316 "Config_pic %d fail\n",
2317 pic->index);
2318 pic->index = -1;
2319 break;
2320 }
2321 pic->pic_w = avs2_dec->img.width;
2322 pic->pic_h = avs2_dec->img.height;
2323 }
2324 for (; i < dec->used_buf_num; i++) {
2325 if (i == (dec->used_buf_num - 1))
2326 pic = avs2_dec->m_bg;
2327 else
2328 pic = avs2_dec->fref[i];
2329 pic->index = -1;
2330 pic->BUF_index = -1;
2331 pic->mv_buf_index = -1;
2332 }
2333 avs2_print(dec, AVS2_DBG_BUFMGR,
2334 "%s ok, used_buf_num = %d\n",
2335 __func__, dec->used_buf_num);
2336 dec->pic_list_init_flag = 1;
2337}
2338
2339
2340static void init_pic_list_hw(struct AVS2Decoder_s *dec)
2341{
2342 int i;
2343 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2344 struct avs2_frame_s *pic;
2345 /*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x0);*/
2346#if 0
2347 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2348 (0x1 << 1) | (0x1 << 2));
2349
2350#ifdef DUAL_CORE_64
2351 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2352 (0x1 << 1) | (0x1 << 2));
2353#endif
2354#endif
2355 for (i = 0; i < dec->used_buf_num; i++) {
2356 if (i == (dec->used_buf_num - 1))
2357 pic = avs2_dec->m_bg;
2358 else
2359 pic = avs2_dec->fref[i];
2360 if (pic->index < 0)
2361 break;
2362#ifdef AVS2_10B_MMU
2363 /*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
2364 pic->header_adr
2365 | (pic->mc_canvas_y << 8)|0x1);*/
2366 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2367 (0x1 << 1) | (pic->index << 8));
2368
2369#ifdef DUAL_CORE_64
2370 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_GXLX2)
2371 WRITE_VREG(HEVC2_MPP_ANC2AXI_TBL_CONF_ADDR,
2372 (0x1 << 1) | (pic->index << 8));
2373 else
2374 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2375 (0x1 << 1) | (pic->index << 8));
2376#endif
2377 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA, pic->header_adr >> 5);
2378#else
2379 /*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
2380 pic->mc_y_adr
2381 | (pic->mc_canvas_y << 8) | 0x1);*/
2382 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA, pic->mc_y_adr >> 5);
2383#endif
2384#ifndef LOSLESS_COMPRESS_MODE
2385 /*WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CMD_ADDR,
2386 pic->mc_u_v_adr
2387 | (pic->mc_canvas_u_v << 8)| 0x1);*/
2388 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_DATA, pic->mc_u_v_adr >> 5);
2389#endif
2390#ifdef DUAL_CORE_64
2391#ifdef AVS2_10B_MMU
2392 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_DATA,
2393 pic->header_adr >> 5);
2394#else
2395 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_DATA,
2396 pic->mc_y_adr >> 5);
2397#endif
2398#ifndef LOSLESS_COMPRESS_MODE
2399 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_DATA,
2400 pic->mc_u_v_adr >> 5);
2401#endif
2402/*DUAL_CORE_64*/
2403#endif
2404 }
2405 WRITE_VREG(HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR, 0x1);
2406#ifdef DUAL_CORE_64
2407 WRITE_VREG(HEVC2_HEVCD_MPP_ANC2AXI_TBL_CONF_ADDR,
2408 0x1);
2409#endif
2410 /*Zero out canvas registers in IPP -- avoid simulation X*/
2411 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2412 (0 << 8) | (0 << 1) | 1);
2413 for (i = 0; i < 32; i++) {
2414 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
2415#ifdef DUAL_CORE_64
2416 WRITE_VREG(HEVC2_HEVCD_MPP_ANC_CANVAS_DATA_ADDR, 0);
2417#endif
2418 }
2419}
2420
2421
2422static void dump_pic_list(struct AVS2Decoder_s *dec)
2423{
2424 int ii;
2425 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2426 for (ii = 0; ii < avs2_dec->ref_maxbuffer; ii++) {
2427 avs2_print(dec, 0,
2428 "fref[%d]: index %d decode_id %d mvbuf %d imgcoi_ref %d imgtr_fwRefDistance %d refered %d, pre %d is_out %d, bg %d, vf_ref %d error %d lcu %d ref_pos(%d,%d,%d,%d,%d,%d,%d)\n",
2429 ii, avs2_dec->fref[ii]->index,
2430 avs2_dec->fref[ii]->decode_idx,
2431 avs2_dec->fref[ii]->mv_buf_index,
2432 avs2_dec->fref[ii]->imgcoi_ref,
2433 avs2_dec->fref[ii]->imgtr_fwRefDistance,
2434 avs2_dec->fref[ii]->refered_by_others,
2435 avs2_dec->fref[ii]->to_prepare_disp,
2436 avs2_dec->fref[ii]->is_output,
2437 avs2_dec->fref[ii]->bg_flag,
2438 avs2_dec->fref[ii]->vf_ref,
2439 avs2_dec->fref[ii]->error_mark,
2440 avs2_dec->fref[ii]->decoded_lcu,
2441 avs2_dec->fref[ii]->ref_poc[0],
2442 avs2_dec->fref[ii]->ref_poc[1],
2443 avs2_dec->fref[ii]->ref_poc[2],
2444 avs2_dec->fref[ii]->ref_poc[3],
2445 avs2_dec->fref[ii]->ref_poc[4],
2446 avs2_dec->fref[ii]->ref_poc[5],
2447 avs2_dec->fref[ii]->ref_poc[6]
2448 );
2449 }
2450 return;
2451}
2452
2453static int config_mc_buffer(struct AVS2Decoder_s *dec)
2454{
2455 int32_t i;
2456 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2457 struct avs2_frame_s *pic;
2458 struct avs2_frame_s *cur_pic = avs2_dec->hc.cur_pic;
2459
2460 /*if (avs2_dec->img.type == I_IMG)
2461 return 0;
2462 */
2463 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2464 "Entered config_mc_buffer....\n");
2465 if (avs2_dec->f_bg != NULL) {
2466 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2467 "config_mc_buffer for background (canvas_y %d, canvas_u_v %d)\n",
2468 avs2_dec->f_bg->mc_canvas_y, avs2_dec->f_bg->mc_canvas_u_v);
2469 /*WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2470 (7 << 8) | (0<<1) | 1); L0:BG */
2471 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2472 (15 << 8) | (0<<1) | 1); /* L0:BG*/
2473 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2474 (avs2_dec->f_bg->mc_canvas_u_v << 16) |
2475 (avs2_dec->f_bg->mc_canvas_u_v << 8) |
2476 avs2_dec->f_bg->mc_canvas_y);
2477 /*WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2478 (23 << 8) | (0<<1) | 1); L1:BG*/
2479 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2480 (31 << 8) | (0<<1) | 1); /* L1:BG*/
2481 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2482 (avs2_dec->f_bg->mc_canvas_u_v << 16) |
2483 (avs2_dec->f_bg->mc_canvas_u_v << 8) |
2484 avs2_dec->f_bg->mc_canvas_y);
2485 }
2486
2487 if (avs2_dec->img.type == I_IMG)
2488 return 0;
2489
2490 if (avs2_dec->img.type == P_IMG) {
2491 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2492 "config_mc_buffer for P_IMG, img type %d\n",
2493 avs2_dec->img.type);
2494 /*refer to prepare_RefInfo()*/
2495 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2496 (0 << 8) | (0<<1) | 1);
2497 for (i = 0; i < avs2_dec->img.num_of_references; i++) {
2498 pic = avs2_dec->fref[i];
2499 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2500 (pic->mc_canvas_u_v << 16) |
2501 (pic->mc_canvas_u_v << 8) |
2502 pic->mc_canvas_y);
2503
2504 if (pic->error_mark)
2505 cur_pic->error_mark = 1;
2506
2507 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2508 "refid %x mc_canvas_u_v %x mc_canvas_y %x error_mark %x\n",
2509 i, pic->mc_canvas_u_v, pic->mc_canvas_y,
2510 pic->error_mark);
2511 }
2512 } else if (avs2_dec->img.type == F_IMG) {
2513 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2514 "config_mc_buffer for F_IMG, img type %d\n",
2515 avs2_dec->img.type);
2516 /*refer to prepare_RefInfo()*/
2517 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2518 (0 << 8) | (0<<1) | 1);
2519 for (i = 0; i < avs2_dec->img.num_of_references; i++) {
2520 pic = avs2_dec->fref[i];
2521 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2522 (pic->mc_canvas_u_v << 16) |
2523 (pic->mc_canvas_u_v << 8) |
2524 pic->mc_canvas_y);
2525
2526 if (pic->error_mark)
2527 cur_pic->error_mark = 1;
2528
2529 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2530 "refid %x mc_canvas_u_v %x mc_canvas_y %x error_mark %x\n",
2531 i, pic->mc_canvas_u_v, pic->mc_canvas_y,
2532 pic->error_mark);
2533 }
2534 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2535 (16 << 8) | (0<<1) | 1);
2536 for (i = 0; i < avs2_dec->img.num_of_references; i++) {
2537 pic = avs2_dec->fref[i];
2538 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2539 (pic->mc_canvas_u_v << 16) |
2540 (pic->mc_canvas_u_v << 8) |
2541 pic->mc_canvas_y);
2542 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2543 "refid %x mc_canvas_u_v %x mc_canvas_y %x\n",
2544 i, pic->mc_canvas_u_v, pic->mc_canvas_y);
2545 }
2546 } else {
2547 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2548 "config_mc_buffer for B_IMG\n");
2549 /*refer to prepare_RefInfo()*/
2550 pic = avs2_dec->fref[1];
2551 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2552 (0 << 8) | (0<<1) | 1);
2553 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2554 (pic->mc_canvas_u_v << 16) |
2555 (pic->mc_canvas_u_v << 8) |
2556 pic->mc_canvas_y);
2557
2558 if (pic->error_mark)
2559 cur_pic->error_mark = 1;
2560
2561 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2562 "refid %x mc_canvas_u_v %x mc_canvas_y %x error_mark %x\n",
2563 1, pic->mc_canvas_u_v, pic->mc_canvas_y,
2564 pic->error_mark);
2565
2566 pic = avs2_dec->fref[0];
2567 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2568 (16 << 8) | (0<<1) | 1);
2569 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR,
2570 (pic->mc_canvas_u_v<<16) |
2571 (pic->mc_canvas_u_v<<8) |
2572 pic->mc_canvas_y);
2573
2574 if (pic->error_mark)
2575 cur_pic->error_mark = 1;
2576
2577 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2578 "refid %x mc_canvas_u_v %x mc_canvas_y %x error_mark %x\n",
2579 0, pic->mc_canvas_u_v, pic->mc_canvas_y,
2580 pic->error_mark);
2581 }
2582 return 0;
2583}
2584#if 0
2585static void mcrcc_get_hitrate(void)
2586{
2587 u32 tmp;
2588 u32 raw_mcr_cnt;
2589 u32 hit_mcr_cnt;
2590 u32 byp_mcr_cnt_nchoutwin;
2591 u32 byp_mcr_cnt_nchcanv;
2592 int hitrate;
2593
2594 if (debug & AVS2_DBG_CACHE)
2595 pr_info("[cache_util.c] Entered mcrcc_get_hitrate...\n");
2596 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x0<<1));
2597 raw_mcr_cnt = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2598 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x1<<1));
2599 hit_mcr_cnt = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2600 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x2<<1));
2601 byp_mcr_cnt_nchoutwin = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2602 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x3<<1));
2603 byp_mcr_cnt_nchcanv = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2604
2605 if (debug & AVS2_DBG_CACHE) {
2606 pr_info("raw_mcr_cnt_total: %d\n",raw_mcr_cnt);
2607 pr_info("hit_mcr_cnt_total: %d\n",hit_mcr_cnt);
2608 pr_info("byp_mcr_cnt_nchoutwin_total: %d\n",byp_mcr_cnt_nchoutwin);
2609 pr_info("byp_mcr_cnt_nchcanv_total: %d\n",byp_mcr_cnt_nchcanv);
2610 }
2611 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x4<<1));
2612 tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2613 if (debug & AVS2_DBG_CACHE)
2614 pr_info("miss_mcr_0_cnt_total: %d\n", tmp);
2615
2616 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x5<<1));
2617 tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2618 if (debug & AVS2_DBG_CACHE)
2619 pr_info("miss_mcr_1_cnt_total: %d\n", tmp);
2620
2621 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x6<<1));
2622 tmp = READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2623 if (debug & AVS2_DBG_CACHE)
2624 pr_info("hit_mcr_0_cnt_total: %d\n",tmp);
2625
2626 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)(0x7<<1));
2627 tmp= READ_VREG(HEVCD_MCRCC_PERFMON_DATA);
2628 if (debug & AVS2_DBG_CACHE)
2629 pr_info("hit_mcr_1_cnt_total: %d\n",tmp);
2630
2631 if (raw_mcr_cnt != 0) {
2632 hitrate = (hit_mcr_cnt / raw_mcr_cnt) * 100;
2633 if (debug & AVS2_DBG_CACHE)
2634 pr_info("MCRCC_HIT_RATE : %d\n", hitrate);
2635 hitrate = ((byp_mcr_cnt_nchoutwin + byp_mcr_cnt_nchcanv)
2636 /raw_mcr_cnt) * 100;
2637 if (debug & AVS2_DBG_CACHE)
2638 pr_info("MCRCC_BYP_RATE : %d\n", hitrate);
2639 } else if (debug & AVS2_DBG_CACHE) {
2640 pr_info("MCRCC_HIT_RATE : na\n");
2641 pr_info("MCRCC_BYP_RATE : na\n");
2642 }
2643 return;
2644}
2645
2646
2647static void decomp_get_hitrate(void)
2648{
2649 u32 raw_mcr_cnt;
2650 u32 hit_mcr_cnt;
2651 int hitrate;
2652
2653 if (debug & AVS2_DBG_CACHE)
2654 pr_info("[cache_util.c] Entered decomp_get_hitrate...\n");
2655 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x0<<1));
2656 raw_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2657 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x1<<1));
2658 hit_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2659
2660 if (debug & AVS2_DBG_CACHE) {
2661 pr_info("hcache_raw_cnt_total: %d\n",raw_mcr_cnt);
2662 pr_info("hcache_hit_cnt_total: %d\n",hit_mcr_cnt);
2663 }
2664 if (raw_mcr_cnt != 0) {
2665 hitrate = (hit_mcr_cnt / raw_mcr_cnt) * 100;
2666 if (debug & AVS2_DBG_CACHE)
2667 pr_info("DECOMP_HCACHE_HIT_RATE : %d\n", hitrate);
2668 } else {
2669 if (debug & AVS2_DBG_CACHE)
2670 pr_info("DECOMP_HCACHE_HIT_RATE : na\n");
2671 }
2672 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x2<<1));
2673 raw_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2674 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x3<<1));
2675 hit_mcr_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2676
2677 if (debug & AVS2_DBG_CACHE) {
2678 pr_info("dcache_raw_cnt_total: %d\n", raw_mcr_cnt);
2679 pr_info("dcache_hit_cnt_total: %d\n", hit_mcr_cnt);
2680 }
2681 if (raw_mcr_cnt != 0) {
2682 hitrate = (hit_mcr_cnt / raw_mcr_cnt) * 100;
2683 if (debug & AVS2_DBG_CACHE)
2684 pr_info("DECOMP_DCACHE_HIT_RATE : %d\n", hitrate);
2685 } else if (debug & AVS2_DBG_CACHE) {
2686 pr_info("DECOMP_DCACHE_HIT_RATE : na\n");
2687 }
2688return;
2689}
2690
2691static void decomp_get_comprate(void)
2692{
2693 u32 raw_ucomp_cnt;
2694 u32 fast_comp_cnt;
2695 u32 slow_comp_cnt;
2696 int comprate;
2697
2698 if (debug & AVS2_DBG_CACHE)
2699 pr_info("[cache_util.c] Entered decomp_get_comprate...\n");
2700 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x4<<1));
2701 fast_comp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2702 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x5<<1));
2703 slow_comp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2704 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)(0x6<<1));
2705 raw_ucomp_cnt = READ_VREG(HEVCD_MPP_DECOMP_PERFMON_DATA);
2706 if (debug & AVS2_DBG_CACHE) {
2707 pr_info("decomp_fast_comp_total: %d\n", fast_comp_cnt);
2708 pr_info("decomp_slow_comp_total: %d\n", slow_comp_cnt);
2709 pr_info("decomp_raw_uncomp_total: %d\n", raw_ucomp_cnt);
2710 }
2711
2712 if (raw_ucomp_cnt != 0) {
2713 comprate = ((fast_comp_cnt + slow_comp_cnt)
2714 / raw_ucomp_cnt) * 100;
2715 if (debug & AVS2_DBG_CACHE)
2716 pr_info("DECOMP_COMP_RATIO : %d\n", comprate);
2717 } else if (debug & AVS2_DBG_CACHE) {
2718 pr_info("DECOMP_COMP_RATIO : na\n");
2719 }
2720 return;
2721}
2722#endif
2723
2724static void config_mcrcc_axi_hw(struct AVS2Decoder_s *dec)
2725{
2726 uint32_t rdata32;
2727 uint32_t rdata32_2;
2728 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2729
2730 WRITE_VREG(HEVCD_MCRCC_CTL1, 0x2); /* reset mcrcc*/
2731
2732 if (avs2_dec->img.type == I_IMG) { /* I-PIC*/
2733 /* remove reset -- disables clock */
2734 WRITE_VREG(HEVCD_MCRCC_CTL1, 0x0);
2735 return;
2736 }
2737/*
2738 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
2739 mcrcc_get_hitrate();
2740 decomp_get_hitrate();
2741 decomp_get_comprate();
2742 }
2743*/
2744 if ((avs2_dec->img.type == B_IMG) ||
2745 (avs2_dec->img.type == F_IMG)) { /*B-PIC or F_PIC*/
2746 /*Programme canvas0 */
2747 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2748 (0 << 8) | (0 << 1) | 0);
2749 rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
2750 rdata32 = rdata32 & 0xffff;
2751 rdata32 = rdata32 | (rdata32 << 16);
2752 WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
2753
2754 /*Programme canvas1 */
2755 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2756 (16 << 8) | (1 << 1) | 0);
2757 rdata32_2 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
2758 rdata32_2 = rdata32_2 & 0xffff;
2759 rdata32_2 = rdata32_2 | (rdata32_2 << 16);
2760 if (rdata32 == rdata32_2) {
2761 rdata32_2 =
2762 READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
2763 rdata32_2 = rdata32_2 & 0xffff;
2764 rdata32_2 = rdata32_2 | (rdata32_2 << 16);
2765 }
2766 WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32_2);
2767 } else { /* P-PIC */
2768 WRITE_VREG(HEVCD_MPP_ANC_CANVAS_ACCCONFIG_ADDR,
2769 (0 << 8) | (1 << 1) | 0);
2770 rdata32 = READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
2771 rdata32 = rdata32 & 0xffff;
2772 rdata32 = rdata32 | (rdata32 << 16);
2773 WRITE_VREG(HEVCD_MCRCC_CTL2, rdata32);
2774
2775 /*Programme canvas1*/
2776 rdata32 =
2777 READ_VREG(HEVCD_MPP_ANC_CANVAS_DATA_ADDR);
2778 rdata32 = rdata32 & 0xffff;
2779 rdata32 = rdata32 | (rdata32 << 16);
2780 WRITE_VREG(HEVCD_MCRCC_CTL3, rdata32);
2781 }
2782 /*enable mcrcc progressive-mode */
2783 WRITE_VREG(HEVCD_MCRCC_CTL1, 0xff0);
2784 return;
2785}
2786
2787static void config_mpred_hw(struct AVS2Decoder_s *dec)
2788{
2789 uint32_t data32;
2790 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2791 struct avs2_frame_s *cur_pic = avs2_dec->hc.cur_pic;
2792 struct avs2_frame_s *col_pic = avs2_dec->fref[0];
2793 int32_t mpred_mv_rd_start_addr;
2794 int32_t mpred_curr_lcu_x;
2795 int32_t mpred_curr_lcu_y;
2796 int32_t mpred_mv_rd_end_addr;
2797 int32_t above_en;
2798 int32_t mv_wr_en;
2799 int32_t mv_rd_en;
2800 int32_t col_isIntra;
2801 int mv_mem_unit;
2802 if (avs2_dec->img.type != I_IMG) {
2803 above_en = 1;
2804 mv_wr_en = 1;
2805 mv_rd_en = 1;
2806 col_isIntra = 0;
2807 } else {
2808 above_en = 1;
2809 mv_wr_en = 1;
2810 mv_rd_en = 0;
2811 col_isIntra = 0;
2812 }
2813
2814 mpred_mv_rd_start_addr =
2815 col_pic->mpred_mv_wr_start_addr;
2816 data32 = READ_VREG(HEVC_MPRED_CURR_LCU);
2817 mpred_curr_lcu_x = data32 & 0xffff;
2818 mpred_curr_lcu_y = (data32 >> 16) & 0xffff;
2819
2820 mv_mem_unit = avs2_dec->lcu_size_log2 == 6 ?
2821 0x200 : (avs2_dec->lcu_size_log2 == 5 ?
2822 0x80 : 0x20);
2823
2824 mpred_mv_rd_end_addr =
2825 mpred_mv_rd_start_addr +
2826 ((avs2_dec->lcu_x_num *
2827 avs2_dec->lcu_y_num) * mv_mem_unit);
2828
2829 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2830 "cur pic index %d col pic index %d\n",
2831 cur_pic->index, col_pic->index);
2832
2833
2834 WRITE_VREG(HEVC_MPRED_MV_WR_START_ADDR,
2835 cur_pic->mpred_mv_wr_start_addr);
2836 WRITE_VREG(HEVC_MPRED_MV_RD_START_ADDR,
2837 col_pic->mpred_mv_wr_start_addr);
2838 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2839 "[MPRED CO_MV] write 0x%x read 0x%x\n",
2840 cur_pic->mpred_mv_wr_start_addr,
2841 col_pic->mpred_mv_wr_start_addr);
2842
2843 data32 =
2844 ((avs2_dec->bk_img_is_top_field) << 13) |
2845 ((avs2_dec->hd.background_picture_enable & 1) << 12) |
2846 ((avs2_dec->hd.curr_RPS.num_of_ref & 7) << 8) |
2847 ((avs2_dec->hd.b_pmvr_enabled & 1) << 6) |
2848 ((avs2_dec->img.is_top_field & 1) << 5) |
2849 ((avs2_dec->img.is_field_sequence & 1) << 4) |
2850 ((avs2_dec->img.typeb & 7) << 1) |
2851 (avs2_dec->hd.background_reference_enable & 0x1);
2852 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2853 "HEVC_MPRED_CTRL9 <= 0x%x(num of ref %d)\n",
2854 data32, avs2_dec->hd.curr_RPS.num_of_ref);
2855 WRITE_VREG(HEVC_MPRED_CTRL9, data32);
2856
2857 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2858 "%s: dis %d %d %d %d %d %d %d fref0_ref_poc %d %d %d %d %d %d %d\n",
2859 __func__,
2860 avs2_dec->fref[0]->imgtr_fwRefDistance,
2861 avs2_dec->fref[1]->imgtr_fwRefDistance,
2862 avs2_dec->fref[2]->imgtr_fwRefDistance,
2863 avs2_dec->fref[3]->imgtr_fwRefDistance,
2864 avs2_dec->fref[4]->imgtr_fwRefDistance,
2865 avs2_dec->fref[5]->imgtr_fwRefDistance,
2866 avs2_dec->fref[6]->imgtr_fwRefDistance,
2867 avs2_dec->fref[0]->ref_poc[0],
2868 avs2_dec->fref[0]->ref_poc[1],
2869 avs2_dec->fref[0]->ref_poc[2],
2870 avs2_dec->fref[0]->ref_poc[3],
2871 avs2_dec->fref[0]->ref_poc[4],
2872 avs2_dec->fref[0]->ref_poc[5],
2873 avs2_dec->fref[0]->ref_poc[6]
2874 );
2875 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2876 "pic_distance %d, imgtr_next_P %d\n",
2877 avs2_dec->img.pic_distance, avs2_dec->img.imgtr_next_P);
2878
2879
2880 WRITE_VREG(HEVC_MPRED_CUR_POC, avs2_dec->img.pic_distance);
2881 WRITE_VREG(HEVC_MPRED_COL_POC, avs2_dec->img.imgtr_next_P);
2882
2883 /*below MPRED Ref_POC_xx_Lx registers
2884 must follow Ref_POC_xx_L0 ->
2885 Ref_POC_xx_L1 in pair write order!!!*/
2886 WRITE_VREG(HEVC_MPRED_L0_REF00_POC,
2887 avs2_dec->fref[0]->imgtr_fwRefDistance);
2888 WRITE_VREG(HEVC_MPRED_L1_REF00_POC,
2889 avs2_dec->fref[0]->ref_poc[0]);
2890
2891 WRITE_VREG(HEVC_MPRED_L0_REF01_POC,
2892 avs2_dec->fref[1]->imgtr_fwRefDistance);
2893 WRITE_VREG(HEVC_MPRED_L1_REF01_POC,
2894 avs2_dec->fref[0]->ref_poc[1]);
2895
2896 WRITE_VREG(HEVC_MPRED_L0_REF02_POC,
2897 avs2_dec->fref[2]->imgtr_fwRefDistance);
2898 WRITE_VREG(HEVC_MPRED_L1_REF02_POC,
2899 avs2_dec->fref[0]->ref_poc[2]);
2900
2901 WRITE_VREG(HEVC_MPRED_L0_REF03_POC,
2902 avs2_dec->fref[3]->imgtr_fwRefDistance);
2903 WRITE_VREG(HEVC_MPRED_L1_REF03_POC,
2904 avs2_dec->fref[0]->ref_poc[3]);
2905
2906 WRITE_VREG(HEVC_MPRED_L0_REF04_POC,
2907 avs2_dec->fref[4]->imgtr_fwRefDistance);
2908 WRITE_VREG(HEVC_MPRED_L1_REF04_POC,
2909 avs2_dec->fref[0]->ref_poc[4]);
2910
2911 WRITE_VREG(HEVC_MPRED_L0_REF05_POC,
2912 avs2_dec->fref[5]->imgtr_fwRefDistance);
2913 WRITE_VREG(HEVC_MPRED_L1_REF05_POC,
2914 avs2_dec->fref[0]->ref_poc[5]);
2915
2916 WRITE_VREG(HEVC_MPRED_L0_REF06_POC,
2917 avs2_dec->fref[6]->imgtr_fwRefDistance);
2918 WRITE_VREG(HEVC_MPRED_L1_REF06_POC,
2919 avs2_dec->fref[0]->ref_poc[6]);
2920
2921
2922 WRITE_VREG(HEVC_MPRED_MV_RD_END_ADDR,
2923 mpred_mv_rd_end_addr);
2924}
2925
2926static void config_dblk_hw(struct AVS2Decoder_s *dec)
2927{
2928 /*
2929 * Picture level de-block parameter configuration here
2930 */
2931 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2932 union param_u *rpm_param = &avs2_dec->param;
2933 uint32_t data32;
2934
2935 data32 = READ_VREG(HEVC_DBLK_CFG1);
2936 data32 = (((data32 >> 20) & 0xfff) << 20) |
2937 (((avs2_dec->input.sample_bit_depth == 10)
2938 ? 0xa : 0x0) << 16) | /*[16 +: 4]: {luma_bd[1:0],
2939 chroma_bd[1:0]}*/
2940 (((data32 >> 2) & 0x3fff) << 2) |
2941 (((rpm_param->p.lcu_size == 6)
2942 ? 0 : (rpm_param->p.lcu_size == 5)
2943 ? 1 : 2) << 0);/*[ 0 +: 2]: lcu_size*/
2944 WRITE_VREG(HEVC_DBLK_CFG1, data32);
2945
2946 data32 = (avs2_dec->img.height << 16) |
2947 avs2_dec->img.width;
2948 WRITE_VREG(HEVC_DBLK_CFG2, data32);
2949 /*
2950 [27 +: 1]: cross_slice_loopfilter_enable_flag
2951 [26 +: 1]: loop_filter_disable
2952 [25 +: 1]: useNSQT
2953 [22 +: 3]: imgtype
2954 [17 +: 5]: alpha_c_offset (-8~8)
2955 [12 +: 5]: beta_offset (-8~8)
2956 [ 6 +: 6]: chroma_quant_param_delta_u (-16~16)
2957 [ 0 +: 6]: chroma_quant_param_delta_v (-16~16)
2958 */
2959 data32 = ((avs2_dec->input.crossSliceLoopFilter
2960 & 0x1) << 27) |
2961 ((rpm_param->p.loop_filter_disable & 0x1) << 26) |
2962 ((avs2_dec->input.useNSQT & 0x1) << 25) |
2963 ((avs2_dec->img.type & 0x7) << 22) |
2964 ((rpm_param->p.alpha_c_offset & 0x1f) << 17) |
2965 ((rpm_param->p.beta_offset & 0x1f) << 12) |
2966 ((rpm_param->p.chroma_quant_param_delta_cb & 0x3f) << 6) |
2967 ((rpm_param->p.chroma_quant_param_delta_cr & 0x3f) << 0);
2968
2969 WRITE_VREG(HEVC_DBLK_CFG9, data32);
2970 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2971 "[c] cfgDBLK: crossslice(%d),lfdisable(%d),bitDepth(%d),lcuSize(%d),NSQT(%d)\n",
2972 avs2_dec->input.crossSliceLoopFilter,
2973 rpm_param->p.loop_filter_disable,
2974 avs2_dec->input.sample_bit_depth,
2975 avs2_dec->lcu_size,
2976 avs2_dec->input.useNSQT);
2977 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2978 "[c] cfgDBLK: alphaCOffset(%d),betaOffset(%d),quantDeltaCb(%d),quantDeltaCr(%d)\n",
2979 rpm_param->p.alpha_c_offset,
2980 rpm_param->p.beta_offset,
2981 rpm_param->p.chroma_quant_param_delta_cb,
2982 rpm_param->p.chroma_quant_param_delta_cr);
2983 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
2984 "[c] cfgDBLK: .done.\n");
2985}
2986
2987static void config_sao_hw(struct AVS2Decoder_s *dec)
2988{
2989 uint32_t data32;
2990 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
2991 struct avs2_frame_s *cur_pic = avs2_dec->hc.cur_pic;
2992
2993 int lcu_size = 64;
2994 int mc_buffer_size_u_v =
2995 cur_pic->lcu_total * lcu_size*lcu_size/2;
2996 int mc_buffer_size_u_v_h =
2997 (mc_buffer_size_u_v + 0xffff) >> 16;/*64k alignment*/
2998
2999 data32 = READ_VREG(HEVC_SAO_CTRL0);
3000 data32 &= (~0xf);
3001 data32 |= avs2_dec->lcu_size_log2;
3002 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3003 "%s, lcu_size_log2 = %d, config HEVC_SAO_CTRL0 0x%x\n",
3004 __func__,
3005 avs2_dec->lcu_size_log2,
3006 data32);
3007
3008 WRITE_VREG(HEVC_SAO_CTRL0, data32);
3009
3010#ifndef AVS2_10B_MMU
3011 if ((get_double_write_mode(dec) & 0x10) == 0)
3012 WRITE_VREG(HEVC_CM_BODY_START_ADDR, cur_pic->mc_y_adr);
3013#endif
3014 if (get_double_write_mode(dec)) {
3015 WRITE_VREG(HEVC_SAO_Y_START_ADDR, cur_pic->dw_y_adr);
3016 WRITE_VREG(HEVC_SAO_C_START_ADDR, cur_pic->dw_u_v_adr);
3017 WRITE_VREG(HEVC_SAO_Y_WPTR, cur_pic->dw_y_adr);
3018 WRITE_VREG(HEVC_SAO_C_WPTR, cur_pic->dw_u_v_adr);
3019 } else {
3020 WRITE_VREG(HEVC_SAO_Y_START_ADDR, 0xffffffff);
3021 WRITE_VREG(HEVC_SAO_C_START_ADDR, 0xffffffff);
3022 }
3023#ifdef AVS2_10B_MMU
3024 WRITE_VREG(HEVC_CM_HEADER_START_ADDR, cur_pic->header_adr);
3025#endif
3026 data32 = (mc_buffer_size_u_v_h << 16) << 1;
3027 /*pr_info("data32=%x,mc_buffer_size_u_v_h=%x,lcu_total=%x\n",
3028 data32, mc_buffer_size_u_v_h, cur_pic->lcu_total);*/
3029 WRITE_VREG(HEVC_SAO_Y_LENGTH, data32);
3030
3031 data32 = (mc_buffer_size_u_v_h << 16);
3032 WRITE_VREG(HEVC_SAO_C_LENGTH, data32);
3033
3034#ifdef AVS2_10B_NV21
3035#ifdef DOS_PROJECT
3036 data32 = READ_VREG(HEVC_SAO_CTRL1);
3037 data32 &= (~0x3000);
3038 /*[13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32*/
3039 data32 |= (MEM_MAP_MODE << 12);
3040 data32 &= (~0x3);
3041 data32 |= 0x1; /* [1]:dw_disable [0]:cm_disable*/
3042 WRITE_VREG(HEVC_SAO_CTRL1, data32);
3043 /*[23:22] dw_v1_ctrl [21:20] dw_v0_ctrl [19:18] dw_h1_ctrl
3044 [17:16] dw_h0_ctrl*/
3045 data32 = READ_VREG(HEVC_SAO_CTRL5);
3046 /*set them all 0 for H265_NV21 (no down-scale)*/
3047 data32 &= ~(0xff << 16);
3048 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3049 ata32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3050 data32 &= (~0x30);
3051 /*[5:4] address_format 00:linear 01:32x32 10:64x32*/
3052 data32 |= (MEM_MAP_MODE << 4);
3053 WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3054#else
3055 /*m8baby test1902*/
3056 data32 = READ_VREG(HEVC_SAO_CTRL1);
3057 data32 &= (~0x3000);
3058 /*[13:12] axi_aformat, 0-Linear, 1-32x32, 2-64x32*/
3059 data32 |= (MEM_MAP_MODE << 12);
3060 data32 &= (~0xff0);
3061 /*data32 |= 0x670;*/ /*Big-Endian per 64-bit*/
3062 data32 |= 0x880; /*.Big-Endian per 64-bit */
3063 data32 &= (~0x3);
3064 data32 |= 0x1; /*[1]:dw_disable [0]:cm_disable*/
3065 WRITE_VREG(HEVC_SAO_CTRL1, data32);
3066 /* [23:22] dw_v1_ctrl [21:20] dw_v0_ctrl
3067 [19:18] dw_h1_ctrl [17:16] dw_h0_ctrl*/
3068 data32 = READ_VREG(HEVC_SAO_CTRL5);
3069 /* set them all 0 for H265_NV21 (no down-scale)*/
3070 data32 &= ~(0xff << 16);
3071 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3072
3073 data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3074 data32 &= (~0x30);
3075 /*[5:4] address_format 00:linear 01:32x32 10:64x32*/
3076 data32 |= (MEM_MAP_MODE << 4);
3077 data32 &= (~0xF);
3078 data32 |= 0x8; /*Big-Endian per 64-bit*/
3079 WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3080#endif
3081#else
3082 data32 = READ_VREG(HEVC_SAO_CTRL1);
3083 data32 &= (~0x3000);
3084 data32 |= (MEM_MAP_MODE <<
3085 12); /* [13:12] axi_aformat, 0-Linear,
3086 1-32x32, 2-64x32 */
3087 data32 &= (~0xff0);
3088 /* data32 |= 0x670; // Big-Endian per 64-bit */
3089 data32 |= endian; /* Big-Endian per 64-bit */
3090 data32 &= (~0x3); /*[1]:dw_disable [0]:cm_disable*/
3091#if 0
3092 if (get_cpu_major_id() < MESON_CPU_MAJOR_ID_G12A) {
3093 if (get_double_write_mode(dec) == 0)
3094 data32 |= 0x2; /*disable double write*/
3095#ifndef AVS2_10B_MMU
3096 else
3097 if (get_double_write_mode(dec) & 0x10)
3098 data32 |= 0x1; /*disable cm*/
3099#endif
3100 }
3101#endif
3102 WRITE_VREG(HEVC_SAO_CTRL1, data32);
3103
3104 if (get_double_write_mode(dec) & 0x10) {
3105 /* [23:22] dw_v1_ctrl
3106 [21:20] dw_v0_ctrl
3107 [19:18] dw_h1_ctrl
3108 [17:16] dw_h0_ctrl
3109 */
3110 data32 = READ_VREG(HEVC_SAO_CTRL5);
3111 /*set them all 0 for H265_NV21 (no down-scale)*/
3112 data32 &= ~(0xff << 16);
3113 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3114 } else {
3115 data32 = READ_VREG(HEVC_SAO_CTRL5);
3116 data32 &= (~(0xff << 16));
3117 if (get_double_write_mode(dec) == 2 ||
3118 get_double_write_mode(dec) == 3)
3119 data32 |= (0xff<<16);
3120 else if (get_double_write_mode(dec) == 4)
3121 data32 |= (0x33<<16);
3122 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3123 }
3124
3125 data32 = READ_VREG(HEVCD_IPP_AXIIF_CONFIG);
3126 data32 &= (~0x30);
3127 /* [5:4] -- address_format 00:linear 01:32x32 10:64x32 */
3128 data32 |= (mem_map_mode <<
3129 4);
3130 data32 &= (~0xF);
3131 data32 |= 0xf; /* valid only when double write only */
3132 /*data32 |= 0x8;*/ /* Big-Endian per 64-bit */
3133 WRITE_VREG(HEVCD_IPP_AXIIF_CONFIG, data32);
3134#endif
3135}
3136
3137static void reconstructCoefficients(struct AVS2Decoder_s *dec,
3138 struct ALFParam_s *alfParam)
3139{
3140 int32_t g, sum, i, coeffPred;
3141 for (g = 0; g < alfParam->filters_per_group; g++) {
3142 sum = 0;
3143 for (i = 0; i < alfParam->num_coeff - 1; i++) {
3144 sum += (2 * alfParam->coeffmulti[g][i]);
3145 dec->m_filterCoeffSym[g][i] =
3146 alfParam->coeffmulti[g][i];
3147 /*pr_info("[t] dec->m_filterCoeffSym[%d][%d]=0x%x\n",
3148 g, i, dec->m_filterCoeffSym[g][i]);*/
3149 }
3150 coeffPred = (1 << ALF_NUM_BIT_SHIFT) - sum;
3151 dec->m_filterCoeffSym[g][alfParam->num_coeff - 1]
3152 = coeffPred +
3153 alfParam->coeffmulti[g][alfParam->num_coeff - 1];
3154 /*pr_info("[t] dec->m_filterCoeffSym[%d][%d]=0x%x\n",
3155 g, (alfParam->num_coeff - 1),
3156 dec->m_filterCoeffSym[g][alfParam->num_coeff - 1]);*/
3157 }
3158}
3159
3160static void reconstructCoefInfo(struct AVS2Decoder_s *dec,
3161 int32_t compIdx, struct ALFParam_s *alfParam)
3162{
3163 int32_t i;
3164 if (compIdx == ALF_Y) {
3165 if (alfParam->filters_per_group > 1) {
3166 for (i = 1; i < NO_VAR_BINS; ++i) {
3167 if (alfParam->filterPattern[i])
3168 dec->m_varIndTab[i] =
3169 dec->m_varIndTab[i - 1] + 1;
3170 else
3171 dec->m_varIndTab[i] =
3172 dec->m_varIndTab[i - 1];
3173 }
3174 }
3175 }
3176 reconstructCoefficients(dec, alfParam);
3177}
3178
3179static void config_alf_hw(struct AVS2Decoder_s *dec)
3180{
3181 /*
3182 * Picture level ALF parameter configuration here
3183 */
3184 uint32_t data32;
3185 int32_t i, j;
3186 int32_t m_filters_per_group;
3187 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
3188 struct ALFParam_s *m_alfPictureParam_y =
3189 &avs2_dec->m_alfPictureParam[0];
3190 struct ALFParam_s *m_alfPictureParam_cb =
3191 &avs2_dec->m_alfPictureParam[1];
3192 struct ALFParam_s *m_alfPictureParam_cr =
3193 &avs2_dec->m_alfPictureParam[2];
3194
3195 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3196 "[t]alfy,cidx(%d),flag(%d),filters_per_group(%d),filterPattern[0]=0x%x,[15]=0x%x\n",
3197 m_alfPictureParam_y->componentID,
3198 m_alfPictureParam_y->alf_flag,
3199 m_alfPictureParam_y->filters_per_group,
3200 m_alfPictureParam_y->filterPattern[0],
3201 m_alfPictureParam_y->filterPattern[15]);
3202 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3203 "[t]alfy,num_coeff(%d),coeffmulti[0][0]=0x%x,[0][1]=0x%x,[1][0]=0x%x,[1][1]=0x%x\n",
3204 m_alfPictureParam_y->num_coeff,
3205 m_alfPictureParam_y->coeffmulti[0][0],
3206 m_alfPictureParam_y->coeffmulti[0][1],
3207 m_alfPictureParam_y->coeffmulti[1][0],
3208 m_alfPictureParam_y->coeffmulti[1][1]);
3209
3210 /*Cr*/
3211 for (i = 0; i < 16; i++)
3212 dec->m_varIndTab[i] = 0;
3213 for (j = 0; j < 16; j++)
3214 for (i = 0; i < 9; i++)
3215 dec->m_filterCoeffSym[j][i] = 0;
3216 reconstructCoefInfo(dec, 2, m_alfPictureParam_cr);
3217 data32 =
3218 ((dec->m_filterCoeffSym[0][4] & 0xf) << 28) |
3219 ((dec->m_filterCoeffSym[0][3] & 0x7f) << 21) |
3220 ((dec->m_filterCoeffSym[0][2] & 0x7f) << 14) |
3221 ((dec->m_filterCoeffSym[0][1] & 0x7f) << 7) |
3222 ((dec->m_filterCoeffSym[0][0] & 0x7f) << 0);
3223 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3224 data32 =
3225 ((dec->m_filterCoeffSym[0][8] & 0x7f) << 24) |
3226 ((dec->m_filterCoeffSym[0][7] & 0x7f) << 17) |
3227 ((dec->m_filterCoeffSym[0][6] & 0x7f) << 10) |
3228 ((dec->m_filterCoeffSym[0][5] & 0x7f) << 3) |
3229 (((dec->m_filterCoeffSym[0][4] >> 4) & 0x7) << 0);
3230 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3231 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3232 "[c] pic_alf_on_cr(%d), alf_cr_coef(%d %d %d %d %d %d %d %d %d)\n",
3233 m_alfPictureParam_cr->alf_flag,
3234 dec->m_filterCoeffSym[0][0],
3235 dec->m_filterCoeffSym[0][1],
3236 dec->m_filterCoeffSym[0][2],
3237 dec->m_filterCoeffSym[0][3],
3238 dec->m_filterCoeffSym[0][4],
3239 dec->m_filterCoeffSym[0][5],
3240 dec->m_filterCoeffSym[0][6],
3241 dec->m_filterCoeffSym[0][7],
3242 dec->m_filterCoeffSym[0][8]);
3243
3244 /* Cb*/
3245 for (j = 0; j < 16; j++)
3246 for (i = 0; i < 9; i++)
3247 dec->m_filterCoeffSym[j][i] = 0;
3248 reconstructCoefInfo(dec, 1, m_alfPictureParam_cb);
3249 data32 =
3250 ((dec->m_filterCoeffSym[0][4] & 0xf) << 28) |
3251 ((dec->m_filterCoeffSym[0][3] & 0x7f) << 21) |
3252 ((dec->m_filterCoeffSym[0][2] & 0x7f) << 14) |
3253 ((dec->m_filterCoeffSym[0][1] & 0x7f) << 7) |
3254 ((dec->m_filterCoeffSym[0][0] & 0x7f) << 0);
3255 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3256 data32 =
3257 ((dec->m_filterCoeffSym[0][8] & 0x7f) << 24) |
3258 ((dec->m_filterCoeffSym[0][7] & 0x7f) << 17) |
3259 ((dec->m_filterCoeffSym[0][6] & 0x7f) << 10) |
3260 ((dec->m_filterCoeffSym[0][5] & 0x7f) << 3) |
3261 (((dec->m_filterCoeffSym[0][4] >> 4) & 0x7) << 0);
3262 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3263 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3264 "[c] pic_alf_on_cb(%d), alf_cb_coef(%d %d %d %d %d %d %d %d %d)\n",
3265 m_alfPictureParam_cb->alf_flag,
3266 dec->m_filterCoeffSym[0][0],
3267 dec->m_filterCoeffSym[0][1],
3268 dec->m_filterCoeffSym[0][2],
3269 dec->m_filterCoeffSym[0][3],
3270 dec->m_filterCoeffSym[0][4],
3271 dec->m_filterCoeffSym[0][5],
3272 dec->m_filterCoeffSym[0][6],
3273 dec->m_filterCoeffSym[0][7],
3274 dec->m_filterCoeffSym[0][8]);
3275
3276 /* Y*/
3277 for (j = 0; j < 16; j++)
3278 for (i = 0; i < 9; i++)
3279 dec->m_filterCoeffSym[j][i] = 0;
3280 reconstructCoefInfo(dec, 0, m_alfPictureParam_y);
3281 data32 =
3282 ((dec->m_varIndTab[7] & 0xf) << 28) |
3283 ((dec->m_varIndTab[6] & 0xf) << 24) |
3284 ((dec->m_varIndTab[5] & 0xf) << 20) |
3285 ((dec->m_varIndTab[4] & 0xf) << 16) |
3286 ((dec->m_varIndTab[3] & 0xf) << 12) |
3287 ((dec->m_varIndTab[2] & 0xf) << 8) |
3288 ((dec->m_varIndTab[1] & 0xf) << 4) |
3289 ((dec->m_varIndTab[0] & 0xf) << 0);
3290 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3291 data32 = ((dec->m_varIndTab[15] & 0xf) << 28) |
3292 ((dec->m_varIndTab[14] & 0xf) << 24) |
3293 ((dec->m_varIndTab[13] & 0xf) << 20) |
3294 ((dec->m_varIndTab[12] & 0xf) << 16) |
3295 ((dec->m_varIndTab[11] & 0xf) << 12) |
3296 ((dec->m_varIndTab[10] & 0xf) << 8) |
3297 ((dec->m_varIndTab[9] & 0xf) << 4) |
3298 ((dec->m_varIndTab[8] & 0xf) << 0);
3299 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3300 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3301 "[c] pic_alf_on_y(%d), alf_y_tab(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)\n",
3302 m_alfPictureParam_y->alf_flag,
3303 dec->m_varIndTab[0],
3304 dec->m_varIndTab[1],
3305 dec->m_varIndTab[2],
3306 dec->m_varIndTab[3],
3307 dec->m_varIndTab[4],
3308 dec->m_varIndTab[5],
3309 dec->m_varIndTab[6],
3310 dec->m_varIndTab[7],
3311 dec->m_varIndTab[8],
3312 dec->m_varIndTab[9],
3313 dec->m_varIndTab[10],
3314 dec->m_varIndTab[11],
3315 dec->m_varIndTab[12],
3316 dec->m_varIndTab[13],
3317 dec->m_varIndTab[14],
3318 dec->m_varIndTab[15]);
3319
3320 m_filters_per_group =
3321 (m_alfPictureParam_y->alf_flag == 0) ?
3322 1 : m_alfPictureParam_y->filters_per_group;
3323 for (i = 0; i < m_filters_per_group; i++) {
3324 data32 =
3325 ((dec->m_filterCoeffSym[i][4] & 0xf) << 28) |
3326 ((dec->m_filterCoeffSym[i][3] & 0x7f) << 21) |
3327 ((dec->m_filterCoeffSym[i][2] & 0x7f) << 14) |
3328 ((dec->m_filterCoeffSym[i][1] & 0x7f) << 7) |
3329 ((dec->m_filterCoeffSym[i][0] & 0x7f) << 0);
3330 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3331 data32 =
3332 /*[31] last indication*/
3333 ((i == m_filters_per_group-1) << 31) |
3334 ((dec->m_filterCoeffSym[i][8] & 0x7f) << 24) |
3335 ((dec->m_filterCoeffSym[i][7] & 0x7f) << 17) |
3336 ((dec->m_filterCoeffSym[i][6] & 0x7f) << 10) |
3337 ((dec->m_filterCoeffSym[i][5] & 0x7f) << 3) |
3338 (((dec->m_filterCoeffSym[i][4] >> 4) & 0x7) << 0);
3339 WRITE_VREG(HEVC_DBLK_CFGD, data32);
3340 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3341 "[c] alf_y_coef[%d](%d %d %d %d %d %d %d %d %d)\n",
3342 i, dec->m_filterCoeffSym[i][0],
3343 dec->m_filterCoeffSym[i][1],
3344 dec->m_filterCoeffSym[i][2],
3345 dec->m_filterCoeffSym[i][3],
3346 dec->m_filterCoeffSym[i][4],
3347 dec->m_filterCoeffSym[i][5],
3348 dec->m_filterCoeffSym[i][6],
3349 dec->m_filterCoeffSym[i][7],
3350 dec->m_filterCoeffSym[i][8]);
3351 }
3352 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
3353 "[c] cfgALF .done.\n");
3354}
3355
3356static void config_other_hw(struct AVS2Decoder_s *dec)
3357{
3358 uint32_t data32;
3359 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
3360 struct avs2_frame_s *cur_pic = avs2_dec->hc.cur_pic;
3361 int bit_depth = cur_pic->bit_depth;
3362 int losless_comp_header_size =
3363 compute_losless_comp_header_size(
3364 dec, cur_pic->pic_w,
3365 cur_pic->pic_h);
3366 int losless_comp_body_size =
3367 compute_losless_comp_body_size(
3368 dec, cur_pic->pic_w,
3369 cur_pic->pic_h, (bit_depth == AVS2_BITS_10));
3370 cur_pic->comp_body_size = losless_comp_body_size;
3371
3372#ifdef LOSLESS_COMPRESS_MODE
3373 data32 = READ_VREG(HEVC_SAO_CTRL5);
3374 if (bit_depth == AVS2_BITS_10)
3375 data32 &= ~(1 << 9);
3376 else
3377 data32 |= (1 << 9);
3378
3379 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3380
3381#ifdef AVS2_10B_MMU
3382 /*bit[4] : paged_mem_mode*/
3383 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0x1 << 4));
3384#else
3385 /*bit[3] smem mdoe*/
3386 if (bit_depth == AVS2_BITS_10)
3387 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0 << 3));
3388 else
3389 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (1 << 3));
3390#endif
3391 WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, (losless_comp_body_size >> 5));
3392 /*WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,(0xff<<20) | (0xff<<10) | 0xff);*/
3393 WRITE_VREG(HEVC_CM_BODY_LENGTH, losless_comp_body_size);
3394 WRITE_VREG(HEVC_CM_HEADER_OFFSET, losless_comp_body_size);
3395 WRITE_VREG(HEVC_CM_HEADER_LENGTH, losless_comp_header_size);
3396#else
3397 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
3398#endif
3399}
3400
3401static void avs2_config_work_space_hw(struct AVS2Decoder_s *dec)
3402{
3403 struct BuffInfo_s *buf_spec = dec->work_space_buf;
3404#ifdef LOSLESS_COMPRESS_MODE
3405 int losless_comp_header_size =
3406 compute_losless_comp_header_size(
3407 dec, dec->init_pic_w,
3408 dec->init_pic_h);
3409 int losless_comp_body_size =
3410 compute_losless_comp_body_size(dec,
3411 dec->init_pic_w,
3412 dec->init_pic_h, buf_alloc_depth == 10);
3413#endif
3414#ifdef AVS2_10B_MMU
3415 unsigned int data32;
3416#endif
3417 if (debug && dec->init_flag == 0)
3418 avs2_print(dec, 0,
3419 "%s %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
3420 __func__,
3421 buf_spec->ipp.buf_start,
3422 buf_spec->start_adr,
3423 buf_spec->short_term_rps.buf_start,
3424 buf_spec->rcs.buf_start,
3425 buf_spec->sps.buf_start,
3426 buf_spec->pps.buf_start,
3427 buf_spec->sao_up.buf_start,
3428 buf_spec->swap_buf.buf_start,
3429 buf_spec->swap_buf2.buf_start,
3430 buf_spec->scalelut.buf_start,
3431 buf_spec->dblk_para.buf_start,
3432 buf_spec->dblk_data.buf_start,
3433 buf_spec->dblk_data2.buf_start);
3434 WRITE_VREG(HEVCD_IPP_LINEBUFF_BASE, buf_spec->ipp.buf_start);
3435 if ((debug & AVS2_DBG_SEND_PARAM_WITH_REG) == 0)
3436 WRITE_VREG(HEVC_RPM_BUFFER, (u32)dec->rpm_phy_addr);
3437 WRITE_VREG(AVS2_ALF_SWAP_BUFFER, buf_spec->short_term_rps.buf_start);
3438 WRITE_VREG(HEVC_RCS_BUFFER, buf_spec->rcs.buf_start);
3439 WRITE_VREG(HEVC_SPS_BUFFER, buf_spec->sps.buf_start);
3440 WRITE_VREG(HEVC_PPS_BUFFER, buf_spec->pps.buf_start);
3441 WRITE_VREG(HEVC_SAO_UP, buf_spec->sao_up.buf_start);
3442#ifdef AVS2_10B_MMU
3443 WRITE_VREG(AVS2_MMU_MAP_BUFFER, dec->frame_mmu_map_phy_addr);
3444#else
3445 WRITE_VREG(HEVC_STREAM_SWAP_BUFFER, buf_spec->swap_buf.buf_start);
3446#endif
3447 WRITE_VREG(HEVC_STREAM_SWAP_BUFFER2, buf_spec->swap_buf2.buf_start);
3448 WRITE_VREG(HEVC_SCALELUT, buf_spec->scalelut.buf_start);
3449
3450 /* cfg_p_addr */
3451 WRITE_VREG(HEVC_DBLK_CFG4, buf_spec->dblk_para.buf_start);
3452 /* cfg_d_addr */
3453 WRITE_VREG(HEVC_DBLK_CFG5, buf_spec->dblk_data.buf_start);
3454
3455 WRITE_VREG(HEVC_DBLK_CFGE, buf_spec->dblk_data2.buf_start);
3456
3457#ifdef LOSLESS_COMPRESS_MODE
3458 data32 = READ_VREG(HEVC_SAO_CTRL5);
3459#if 1
3460 data32 &= ~(1<<9);
3461#else
3462 if (params->p.bit_depth != 0x00)
3463 data32 &= ~(1<<9);
3464 else
3465 data32 |= (1<<9);
3466#endif
3467 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3468#ifdef AVS2_10B_MMU
3469 /*bit[4] : paged_mem_mode*/
3470 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0x1 << 4));
3471 WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, 0);
3472#else
3473 /* bit[3] smem mode*/
3474 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, (0<<3));
3475
3476 WRITE_VREG(HEVCD_MPP_DECOMP_CTL2, (losless_comp_body_size >> 5));
3477#endif
3478 /*WRITE_VREG(HEVCD_MPP_DECOMP_CTL2,(losless_comp_body_size >> 5));*/
3479 /*WRITE_VREG(HEVCD_MPP_DECOMP_CTL3,(0xff<<20) | (0xff<<10) | 0xff);*/
3480/*8-bit mode */
3481 WRITE_VREG(HEVC_CM_BODY_LENGTH, losless_comp_body_size);
3482 WRITE_VREG(HEVC_CM_HEADER_OFFSET, losless_comp_body_size);
3483 WRITE_VREG(HEVC_CM_HEADER_LENGTH, losless_comp_header_size);
3484#else
3485 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
3486#endif
3487
3488#ifdef AVS2_10B_MMU
3489 WRITE_VREG(HEVC_SAO_MMU_VH0_ADDR, buf_spec->mmu_vbh.buf_start);
3490 WRITE_VREG(HEVC_SAO_MMU_VH1_ADDR, buf_spec->mmu_vbh.buf_start
3491 + buf_spec->mmu_vbh.buf_size/2);
3492 /*data32 = READ_VREG(HEVC_SAO_CTRL9);*/
3493 /*data32 |= 0x1;*/
3494 /*WRITE_VREG(HEVC_SAO_CTRL9, data32);*/
3495
3496 /* use HEVC_CM_HEADER_START_ADDR */
3497 data32 = READ_VREG(HEVC_SAO_CTRL5);
3498 data32 |= (1<<10);
3499#if 1
3500 if (debug & AVS2_DBG_FORCE_UNCOMPRESS)
3501 data32 |= 0x80;
3502#endif
3503 WRITE_VREG(HEVC_SAO_CTRL5, data32);
3504
3505#endif
3506
3507 WRITE_VREG(LMEM_DUMP_ADR, (u32)dec->lmem_phy_addr);
3508#if 1
3509/*MULTI_INSTANCE_SUPPORT*/
3510 /*new added in simulation???*/
3511 WRITE_VREG(HEVC_MPRED_ABV_START_ADDR, buf_spec->mpred_above.buf_start);
3512#endif
3513}
3514
3515static void decomp_perfcount_reset(void)
3516{
3517 if (debug & AVS2_DBG_CACHE)
3518 pr_info("[cache_util.c] Entered decomp_perfcount_reset...\n");
3519 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x1);
3520 WRITE_VREG(HEVCD_MPP_DECOMP_PERFMON_CTL, (unsigned int)0x0);
3521 return;
3522}
3523
3524static void mcrcc_perfcount_reset(void)
3525{
3526 if (debug & AVS2_DBG_CACHE)
3527 pr_info("[cache_util.c] Entered mcrcc_perfcount_reset...\n");
3528 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x1);
3529 WRITE_VREG(HEVCD_MCRCC_PERFMON_CTL, (unsigned int)0x0);
3530 return;
3531}
3532
3533static void avs2_init_decoder_hw(struct AVS2Decoder_s *dec)
3534{
3535 unsigned int data32;
3536 unsigned int decode_mode;
3537 int i;
3538
3539 /*if (debug & AVS2_DBG_BUFMGR_MORE)
3540 pr_info("%s\n", __func__);*/
3541 data32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
3542#if 1
3543 /* set bit 31~29 to 3 if HEVC_STREAM_FIFO_CTL[29] is 1 */
3544 data32 &= ~(7 << 29);
3545 data32 |= (3 << 29);
3546#endif
3547 data32 = data32 |
3548 (1 << 24) |/*stream_buffer_empty_int_amrisc_enable*/
3549 (1 << 22) |/*stream_fifo_empty_int_amrisc_enable*/
3550 (1 << 7) |/*dec_done_int_cpu_enable*/
3551 (1 << 4) |/*startcode_found_int_cpu_enable*/
3552 (0 << 3) |/*startcode_found_int_amrisc_enable*/
3553 (1 << 0) /*parser_int_enable*/
3554 ;
3555 WRITE_VREG(HEVC_PARSER_INT_CONTROL, data32);
3556
3557 data32 = READ_VREG(HEVC_SHIFT_STATUS);
3558 data32 = data32 |
3559 (0 << 1) |/*emulation_check_off VP9
3560 do not have emulation*/
3561 (1 << 0)/*startcode_check_on*/
3562 ;
3563 WRITE_VREG(HEVC_SHIFT_STATUS, data32);
3564 WRITE_VREG(HEVC_SHIFT_CONTROL,
3565 (6 << 20) | /* emu_push_bits (6-bits for AVS2)*/
3566 (0 << 19) | /* emu_3_enable, maybe turned on in microcode*/
3567 (0 << 18) | /* emu_2_enable, maybe turned on in microcode*/
3568 (0 << 17) | /* emu_1_enable, maybe turned on in microcode*/
3569 (0 << 16) | /* emu_0_enable, maybe turned on in microcode*/
3570 (0 << 14) | /*disable_start_code_protect*/
3571 (3 << 6) | /* sft_valid_wr_position*/
3572 (2 << 4) | /* emulate_code_length_sub_1*/
3573 (2 << 1) | /* start_code_length_sub_1*/
3574 (1 << 0) /* stream_shift_enable*/
3575 );
3576
3577 WRITE_VREG(HEVC_SHIFT_LENGTH_PROTECT,
3578 (0 << 30) | /*data_protect_fill_00_enable*/
3579 (1 << 29) /*data_protect_fill_ff_enable*/
3580 );
3581 WRITE_VREG(HEVC_CABAC_CONTROL,
3582 (1 << 0)/*cabac_enable*/
3583 );
3584
3585 WRITE_VREG(HEVC_PARSER_CORE_CONTROL,
3586 (1 << 0)/* hevc_parser_core_clk_en*/
3587 );
3588
3589
3590 WRITE_VREG(HEVC_DEC_STATUS_REG, 0);
3591
3592 /*Initial IQIT_SCALELUT memory -- just to avoid X in simulation*/
3593
3594 WRITE_VREG(HEVC_IQIT_SCALELUT_WR_ADDR, 0);/*cfg_p_addr*/
3595 for (i = 0; i < 1024; i++)
3596 WRITE_VREG(HEVC_IQIT_SCALELUT_DATA, 0);
3597
3598
3599#ifdef ENABLE_SWAP_TEST
3600 WRITE_VREG(HEVC_STREAM_SWAP_TEST, 100);
3601#else
3602 WRITE_VREG(HEVC_STREAM_SWAP_TEST, 0);
3603#endif
3604 if (!dec->m_ins_flag)
3605 decode_mode = DECODE_MODE_SINGLE;
3606 else if (vdec_frame_based(hw_to_vdec(dec)))
3607 decode_mode = DECODE_MODE_MULTI_FRAMEBASE;
3608 else
3609 decode_mode = DECODE_MODE_MULTI_STREAMBASE;
3610 if (dec->avs2_dec.bufmgr_error_flag &&
3611 (error_handle_policy & 0x1)) {
3612 dec->bufmgr_error_count++;
3613 dec->avs2_dec.bufmgr_error_flag = 0;
3614 if (dec->bufmgr_error_count >
3615 (re_search_seq_threshold & 0xff)
3616 && dec->frame_count >
3617 ((re_search_seq_threshold >> 8) & 0xff)) {
3618 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
3619 dec->start_decoding_flag = 0;
3620 avs2_dec->hd.vec_flag = 1;
3621 dec->skip_PB_before_I = 1;
3622 avs2_print(dec, 0,
3623 "!!Bufmgr error, search seq again (0x%x %d %d)\n",
3624 error_handle_policy,
3625 dec->frame_count,
3626 dec->bufmgr_error_count);
3627 dec->bufmgr_error_count = 0;
3628 }
3629 }
3630 decode_mode |= (dec->start_decoding_flag << 16);
3631
3632 WRITE_VREG(DECODE_MODE, decode_mode);
3633 WRITE_VREG(HEVC_DECODE_SIZE, 0);
3634 WRITE_VREG(HEVC_DECODE_COUNT, 0);
3635
3636 /*Send parser_cmd*/
3637 WRITE_VREG(HEVC_PARSER_CMD_WRITE, (1 << 16) | (0 << 0));
3638 for (i = 0; i < PARSER_CMD_NUMBER; i++)
3639 WRITE_VREG(HEVC_PARSER_CMD_WRITE, parser_cmd[i]);
3640 WRITE_VREG(HEVC_PARSER_CMD_SKIP_0, PARSER_CMD_SKIP_CFG_0);
3641 WRITE_VREG(HEVC_PARSER_CMD_SKIP_1, PARSER_CMD_SKIP_CFG_1);
3642 WRITE_VREG(HEVC_PARSER_CMD_SKIP_2, PARSER_CMD_SKIP_CFG_2);
3643
3644
3645 WRITE_VREG(HEVC_PARSER_IF_CONTROL,
3646 (1 << 9) | /* parser_alf_if_en*/
3647 /* (1 << 8) |*/ /*sao_sw_pred_enable*/
3648 (1 << 5) | /*parser_sao_if_en*/
3649 (1 << 2) | /*parser_mpred_if_en*/
3650 (1 << 0) /*parser_scaler_if_en*/
3651 );
3652
3653#ifdef MULTI_INSTANCE_SUPPORT
3654 WRITE_VREG(HEVC_MPRED_INT_STATUS, (1<<31));
3655
3656 WRITE_VREG(HEVC_PARSER_RESULT_3, 0xffffffff);
3657
3658 for (i = 0; i < 8; i++)
3659 data32 = READ_VREG(HEVC_MPRED_ABV_START_ADDR);
3660
3661 WRITE_VREG(DOS_SW_RESET3, (1<<18)); /* reset mpred */
3662 WRITE_VREG(DOS_SW_RESET3, 0);
3663 WRITE_VREG(HEVC_MPRED_ABV_START_ADDR, data32);
3664 WRITE_VREG(HEVC_MPRED_ABV_START_ADDR, data32);
3665 WRITE_VREG(HEVC_MPRED_ABV_START_ADDR, data32);
3666#endif
3667 /*End of Multi-instance*/
3668 /*Changed to Start MPRED in microcode*/
3669 /*
3670 pr_info("[test.c] Start MPRED\n");
3671 WRITE_VREG(HEVC_MPRED_INT_STATUS,
3672 (1<<31)
3673 );
3674 */
3675
3676 /*AVS2 default seq_wq_matrix config*/
3677
3678 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
3679 "Config AVS2 default seq_wq_matrix ...\n");
3680 /*4x4*/
3681 /* default seq_wq_matrix_4x4 begin address*/
3682 WRITE_VREG(HEVC_IQIT_SCALELUT_WR_ADDR, 64);
3683 for (i = 0; i < 16; i++)
3684 WRITE_VREG(HEVC_IQIT_SCALELUT_DATA, g_WqMDefault4x4[i]);
3685
3686 /*8x8*/
3687 /*default seq_wq_matrix_8x8 begin address*/
3688 WRITE_VREG(HEVC_IQIT_SCALELUT_WR_ADDR, 0);
3689 for (i = 0; i < 64; i++)
3690 WRITE_VREG(HEVC_IQIT_SCALELUT_DATA, g_WqMDefault8x8[i]);
3691
3692
3693 WRITE_VREG(HEVCD_IPP_TOP_CNTL,
3694 (0 << 1) | /*enable ipp*/
3695 (1 << 0) /*software reset ipp and mpp*/
3696 );
3697 WRITE_VREG(HEVCD_IPP_TOP_CNTL,
3698 (1 << 1) | /*enable ipp*/
3699 (0 << 0) /*software reset ipp and mpp*/
3700 );
3701#if 0
3702/*AVS2_10B_NV21*/
3703 /*Enable NV21 reference read mode for MC*/
3704 WRITE_VREG(HEVCD_MPP_DECOMP_CTL1, 0x1 << 31);
3705#endif
3706 /* Init dblk*/
3707 data32 = READ_VREG(HEVC_DBLK_CFGB);
3708 data32 |= (2 << 0);
3709 /* [3:0] cfg_video_type -> AVS2*/
3710
3711 data32 &= (~0x300); /*[8]:first write enable (compress)
3712 [9]:double write enable (uncompress)*/
3713 if (get_double_write_mode(dec) == 0)
3714 data32 |= (0x1 << 8); /*enable first write*/
3715 else if (get_double_write_mode(dec) == 0x10)
3716 data32 |= (0x1 << 9); /*double write only*/
3717 else
3718 data32 |= ((0x1 << 8) | (0x1 << 9));
3719 WRITE_VREG(HEVC_DBLK_CFGB, data32);
3720
3721 WRITE_VREG(HEVC_DBLK_CFG0, (1 << 0)); /* [0] rst_sync*/
3722 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
3723 "Bitstream level Init for DBLK .Done.\n");
3724
3725 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
3726 mcrcc_perfcount_reset();
3727 decomp_perfcount_reset();
3728 }
3729
3730 return;
3731}
3732
3733
3734#ifdef CONFIG_HEVC_CLK_FORCED_ON
3735static void config_avs2_clk_forced_on(void)
3736{
3737 unsigned int rdata32;
3738 /*IQIT*/
3739 rdata32 = READ_VREG(HEVC_IQIT_CLK_RST_CTRL);
3740 WRITE_VREG(HEVC_IQIT_CLK_RST_CTRL, rdata32 | (0x1 << 2));
3741
3742 /* DBLK*/
3743 rdata32 = READ_VREG(HEVC_DBLK_CFG0);
3744 WRITE_VREG(HEVC_DBLK_CFG0, rdata32 | (0x1 << 2));
3745
3746 /* SAO*/
3747 rdata32 = READ_VREG(HEVC_SAO_CTRL1);
3748 WRITE_VREG(HEVC_SAO_CTRL1, rdata32 | (0x1 << 2));
3749
3750 /*MPRED*/
3751 rdata32 = READ_VREG(HEVC_MPRED_CTRL1);
3752 WRITE_VREG(HEVC_MPRED_CTRL1, rdata32 | (0x1 << 24));
3753
3754 /* PARSER*/
3755 rdata32 = READ_VREG(HEVC_STREAM_CONTROL);
3756 WRITE_VREG(HEVC_STREAM_CONTROL, rdata32 | (0x1 << 15));
3757 rdata32 = READ_VREG(HEVC_SHIFT_CONTROL);
3758 WRITE_VREG(HEVC_SHIFT_CONTROL, rdata32 | (0x1 << 15));
3759 rdata32 = READ_VREG(HEVC_CABAC_CONTROL);
3760 WRITE_VREG(HEVC_CABAC_CONTROL, rdata32 | (0x1 << 13));
3761 rdata32 = READ_VREG(HEVC_PARSER_CORE_CONTROL);
3762 WRITE_VREG(HEVC_PARSER_CORE_CONTROL, rdata32 | (0x1 << 15));
3763 rdata32 = READ_VREG(HEVC_PARSER_INT_CONTROL);
3764 WRITE_VREG(HEVC_PARSER_INT_CONTROL, rdata32 | (0x1 << 15));
3765 rdata32 = READ_VREG(HEVC_PARSER_IF_CONTROL);
3766 WRITE_VREG(HEVC_PARSER_IF_CONTROL,
3767 rdata32 | (0x1 << 6) | (0x1 << 3) | (0x1 << 1));
3768
3769 /*IPP*/
3770 rdata32 = READ_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG);
3771 WRITE_VREG(HEVCD_IPP_DYNCLKGATE_CONFIG, rdata32 | 0xffffffff);
3772
3773 /* MCRCC*/
3774 rdata32 = READ_VREG(HEVCD_MCRCC_CTL1);
3775 WRITE_VREG(HEVCD_MCRCC_CTL1, rdata32 | (0x1 << 3));
3776}
3777#endif
3778
3779
3780
3781
3782
3783static struct AVS2Decoder_s gAVS2Decoder;
3784
3785static void avs2_local_uninit(struct AVS2Decoder_s *dec)
3786{
3787 dec->rpm_ptr = NULL;
3788 dec->lmem_ptr = NULL;
3789 if (dec->rpm_addr) {
3790 dma_free_coherent(amports_get_dma_device(),
3791 RPM_BUF_SIZE, dec->rpm_addr,
3792 dec->rpm_phy_addr);
3793 dec->rpm_addr = NULL;
3794 }
3795 if (dec->lmem_addr) {
3796 if (dec->lmem_phy_addr)
3797 dma_free_coherent(amports_get_dma_device(),
3798 LMEM_BUF_SIZE, dec->lmem_addr,
3799 dec->lmem_phy_addr);
3800 dec->lmem_addr = NULL;
3801 }
3802
3803#ifdef AVS2_10B_MMU
3804 if (dec->frame_mmu_map_addr) {
3805 if (dec->frame_mmu_map_phy_addr)
3806 dma_free_coherent(amports_get_dma_device(),
3807 get_frame_mmu_map_size(dec), dec->frame_mmu_map_addr,
3808 dec->frame_mmu_map_phy_addr);
3809 dec->frame_mmu_map_addr = NULL;
3810 }
3811#endif
3812 if (dec->gvs)
3813 vfree(dec->gvs);
3814 dec->gvs = NULL;
3815}
3816
3817static int avs2_local_init(struct AVS2Decoder_s *dec)
3818{
3819 int ret = -1;
3820 /*int losless_comp_header_size, losless_comp_body_size;*/
3821
3822 struct BuffInfo_s *cur_buf_info = NULL;
3823
3824 cur_buf_info = &dec->work_space_buf_store;
3825
3826 if (vdec_is_support_4k()) {
3827 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
3828 memcpy(cur_buf_info, &amvavs2_workbuff_spec[2], /* 8k */
3829 sizeof(struct BuffInfo_s));
3830 else
3831 memcpy(cur_buf_info, &amvavs2_workbuff_spec[1], /* 4k */
3832 sizeof(struct BuffInfo_s));
3833 } else
3834 memcpy(cur_buf_info, &amvavs2_workbuff_spec[0],/* 1080p */
3835 sizeof(struct BuffInfo_s));
3836
3837 cur_buf_info->start_adr = dec->buf_start;
3838#ifndef AVS2_10B_MMU
3839 dec->mc_buf_spec.buf_end = dec->buf_start + dec->buf_size;
3840#endif
3841
3842 init_buff_spec(dec, cur_buf_info);
3843
3844 init_avs2_decoder(&dec->avs2_dec);
3845
3846#ifdef AVS2_10B_MMU
3847 avs2_bufmgr_init(dec, cur_buf_info, NULL);
3848#else
3849 dec->mc_buf_spec.buf_start = (cur_buf_info->end_adr + 0xffff)
3850 & (~0xffff);
3851 dec->mc_buf_spec.buf_size = (dec->mc_buf_spec.buf_end
3852 - dec->mc_buf_spec.buf_start);
3853 if (debug) {
3854 pr_err("dec->mc_buf_spec.buf_start %x-%x\n",
3855 dec->mc_buf_spec.buf_start,
3856 dec->mc_buf_spec.buf_start +
3857 dec->mc_buf_spec.buf_size);
3858 }
3859 avs2_bufmgr_init(dec, cur_buf_info, &dec->mc_buf_spec);
3860#endif
3861
3862 if (!vdec_is_support_4k()
3863 && (buf_alloc_width > 1920 && buf_alloc_height > 1088)) {
3864 buf_alloc_width = 1920;
3865 buf_alloc_height = 1088;
3866 } else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
3867 buf_alloc_width = 8192;
3868 buf_alloc_height = 4608;
3869 }
3870 dec->init_pic_w = buf_alloc_width ? buf_alloc_width :
3871 (dec->vavs2_amstream_dec_info.width ?
3872 dec->vavs2_amstream_dec_info.width :
3873 dec->work_space_buf->max_width);
3874 dec->init_pic_h = buf_alloc_height ? buf_alloc_height :
3875 (dec->vavs2_amstream_dec_info.height ?
3876 dec->vavs2_amstream_dec_info.height :
3877 dec->work_space_buf->max_height);
3878#if 0
3879/*ndef MV_USE_FIXED_BUF*/
3880 if (init_mv_buf_list(dec) < 0) {
3881 pr_err("%s: init_mv_buf_list fail\n", __func__);
3882 return -1;
3883 }
3884#endif
3885
3886#ifndef AVS2_10B_MMU
3887 init_buf_list(dec);
3888#else
3889 dec->used_buf_num = max_buf_num + dec->dynamic_buf_margin;
3890 if (dec->used_buf_num > MAX_BUF_NUM)
3891 dec->used_buf_num = MAX_BUF_NUM;
3892 if (dec->used_buf_num > FRAME_BUFFERS)
3893 dec->used_buf_num = FRAME_BUFFERS;
3894#endif
3895 dec->avs2_dec.ref_maxbuffer = dec->used_buf_num - 1;
3896 /*init_pic_list(dec);*/
3897
3898 pts_unstable = ((unsigned long)(dec->vavs2_amstream_dec_info.param)
3899 & 0x40) >> 6;
3900
3901 if ((debug & AVS2_DBG_SEND_PARAM_WITH_REG) == 0) {
3902 dec->rpm_addr = dma_alloc_coherent(amports_get_dma_device(),
3903 RPM_BUF_SIZE,
3904 &dec->rpm_phy_addr, GFP_KERNEL);
3905 if (dec->rpm_addr == NULL) {
3906 pr_err("%s: failed to alloc rpm buffer\n", __func__);
3907 return -1;
3908 }
3909 avs2_print(dec, AVS2_DBG_BUFMGR,
3910 "rpm_phy_addr %x\n", (u32) dec->rpm_phy_addr);
3911 dec->rpm_ptr = dec->rpm_addr;
3912 }
3913
3914 dec->lmem_addr = dma_alloc_coherent(amports_get_dma_device(),
3915 LMEM_BUF_SIZE,
3916 &dec->lmem_phy_addr, GFP_KERNEL);
3917 if (dec->lmem_addr == NULL) {
3918 pr_err("%s: failed to alloc lmem buffer\n", __func__);
3919 return -1;
3920 } else
3921 avs2_print(dec, AVS2_DBG_BUFMGR,
3922 "%s, lmem_phy_addr %x\n",
3923 __func__, (u32)dec->lmem_phy_addr);
3924
3925 dec->lmem_ptr = dec->lmem_addr;
3926
3927
3928#ifdef AVS2_10B_MMU
3929 dec->frame_mmu_map_addr = dma_alloc_coherent(amports_get_dma_device(),
3930 get_frame_mmu_map_size(dec),
3931 &dec->frame_mmu_map_phy_addr, GFP_KERNEL);
3932 if (dec->frame_mmu_map_addr == NULL) {
3933 pr_err("%s: failed to alloc count_buffer\n", __func__);
3934 return -1;
3935 }
3936 memset(dec->frame_mmu_map_addr, 0, get_frame_mmu_map_size(dec));
3937#endif
3938
3939 ret = 0;
3940 return ret;
3941}
3942
3943/********************************************
3944 * Mailbox command
3945 ********************************************/
3946#define CMD_FINISHED 0
3947#define CMD_ALLOC_VIEW 1
3948#define CMD_FRAME_DISPLAY 3
3949#define CMD_DEBUG 10
3950
3951
3952#define DECODE_BUFFER_NUM_MAX 32
3953#define DISPLAY_BUFFER_NUM 6
3954
3955#define video_domain_addr(adr) (adr&0x7fffffff)
3956#define DECODER_WORK_SPACE_SIZE 0x800000
3957
3958#define spec2canvas(x) \
3959 (((x)->uv_canvas_index << 16) | \
3960 ((x)->uv_canvas_index << 8) | \
3961 ((x)->y_canvas_index << 0))
3962
3963
3964static void set_canvas(struct AVS2Decoder_s *dec,
3965 struct avs2_frame_s *pic)
3966{
3967 int canvas_w = ALIGN(pic->pic_w, 64)/4;
3968 int canvas_h = ALIGN(pic->pic_h, 32)/4;
3969 int blkmode = mem_map_mode;
3970 struct vdec_s *vdec = hw_to_vdec(dec);
3971 /*CANVAS_BLKMODE_64X32*/
3972 if (pic->double_write_mode) {
3973 canvas_w = pic->pic_w /
3974 get_double_write_ratio(dec,
3975 pic->double_write_mode);
3976 canvas_h = pic->pic_h /
3977 get_double_write_ratio(dec,
3978 pic->double_write_mode);
3979
3980 if (mem_map_mode == 0)
3981 canvas_w = ALIGN(canvas_w, 32);
3982 else
3983 canvas_w = ALIGN(canvas_w, 64);
3984 canvas_h = ALIGN(canvas_h, 32);
3985
3986 if (vdec->parallel_dec == 1) {
3987 if (pic->y_canvas_index == -1)
3988 pic->y_canvas_index = vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
3989 if (pic->uv_canvas_index == -1)
3990 pic->uv_canvas_index = vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
3991 } else {
3992 pic->y_canvas_index = 128 + pic->index * 2;
3993 pic->uv_canvas_index = 128 + pic->index * 2 + 1;
3994 }
3995
3996 canvas_config_ex(pic->y_canvas_index,
3997 pic->dw_y_adr, canvas_w, canvas_h,
3998 CANVAS_ADDR_NOWRAP, blkmode, 0x7);
3999 canvas_config_ex(pic->uv_canvas_index,
4000 pic->dw_u_v_adr, canvas_w, canvas_h,
4001 CANVAS_ADDR_NOWRAP, blkmode, 0x7);
4002#ifdef MULTI_INSTANCE_SUPPORT
4003 pic->canvas_config[0].phy_addr =
4004 pic->dw_y_adr;
4005 pic->canvas_config[0].width =
4006 canvas_w;
4007 pic->canvas_config[0].height =
4008 canvas_h;
4009 pic->canvas_config[0].block_mode =
4010 blkmode;
4011 pic->canvas_config[0].endian = 7;
4012
4013 pic->canvas_config[1].phy_addr =
4014 pic->dw_u_v_adr;
4015 pic->canvas_config[1].width =
4016 canvas_w;
4017 pic->canvas_config[1].height =
4018 canvas_h;
4019 pic->canvas_config[1].block_mode =
4020 blkmode;
4021 pic->canvas_config[1].endian = 7;
4022#endif
4023 } else {
4024 #ifndef AVS2_10B_MMU
4025 if (vdec->parallel_dec == 1) {
4026 if (pic->y_canvas_index == -1)
4027 pic->y_canvas_index = vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
4028 if (pic->uv_canvas_index == -1)
4029 pic->uv_canvas_index = vdec->get_canvas_ex(CORE_MASK_HEVC, vdec->id);
4030 } else {
4031 pic->y_canvas_index = 128 + pic->index;
4032 pic->uv_canvas_index = 128 + pic->index;
4033 }
4034
4035 canvas_config_ex(pic->y_canvas_index,
4036 pic->mc_y_adr, canvas_w, canvas_h,
4037 CANVAS_ADDR_NOWRAP, blkmode, 0x7);
4038 canvas_config_ex(pic->uv_canvas_index,
4039 pic->mc_u_v_adr, canvas_w, canvas_h,
4040 CANVAS_ADDR_NOWRAP, blkmode, 0x7);
4041 #endif
4042 }
4043}
4044
4045static void set_frame_info(struct AVS2Decoder_s *dec, struct vframe_s *vf)
4046{
4047 unsigned int ar;
4048
4049 vf->duration = dec->frame_dur;
4050 vf->duration_pulldown = 0;
4051 vf->flag = 0;
4052 vf->prop.master_display_colour = dec->vf_dp;
4053 vf->signal_type = dec->video_signal_type;
4054
4055 ar = min_t(u32, dec->frame_ar, DISP_RATIO_ASPECT_RATIO_MAX);
4056 vf->ratio_control = (ar << DISP_RATIO_ASPECT_RATIO_BIT);
4057
4058 return;
4059}
4060
4061static int vavs2_vf_states(struct vframe_states *states, void *op_arg)
4062{
4063 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)op_arg;
4064
4065 states->vf_pool_size = VF_POOL_SIZE;
4066 states->buf_free_num = kfifo_len(&dec->newframe_q);
4067 states->buf_avail_num = kfifo_len(&dec->display_q);
4068
4069 if (step == 2)
4070 states->buf_avail_num = 0;
4071 return 0;
4072}
4073
4074static struct vframe_s *vavs2_vf_peek(void *op_arg)
4075{
4076 struct vframe_s *vf;
4077 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)op_arg;
4078 if (step == 2)
4079 return NULL;
4080
4081 if (force_disp_pic_index & 0x100) {
4082 if (force_disp_pic_index & 0x200)
4083 return NULL;
4084 return &dec->vframe_dummy;
4085 }
4086
4087 if (kfifo_peek(&dec->display_q, &vf))
4088 return vf;
4089
4090 return NULL;
4091}
4092
4093static struct avs2_frame_s *get_pic_by_index(
4094 struct AVS2Decoder_s *dec, int index)
4095{
4096 int i;
4097 struct avs2_frame_s *pic = NULL;
4098 if (index == (dec->used_buf_num - 1))
4099 pic = dec->avs2_dec.m_bg;
4100 else if (index >= 0 && index < dec->used_buf_num) {
4101 for (i = 0; i < dec->used_buf_num; i++) {
4102 if (dec->avs2_dec.fref[i]->index == index)
4103 pic = dec->avs2_dec.fref[i];
4104 }
4105 }
4106 return pic;
4107}
4108
4109static struct vframe_s *vavs2_vf_get(void *op_arg)
4110{
4111 struct vframe_s *vf;
4112 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)op_arg;
4113 if (step == 2)
4114 return NULL;
4115 else if (step == 1)
4116 step = 2;
4117
4118 if (force_disp_pic_index & 0x100) {
4119 int idx = force_disp_pic_index & 0xff;
4120 struct avs2_frame_s *pic = NULL;
4121 if (idx >= 0
4122 && idx < dec->avs2_dec.ref_maxbuffer)
4123 pic = get_pic_by_index(dec, idx);
4124 if (pic == NULL)
4125 return NULL;
4126 if (force_disp_pic_index & 0x200)
4127 return NULL;
4128
4129 vf = &dec->vframe_dummy;
4130
4131 set_vframe(dec, vf, pic, 1);
4132
4133 force_disp_pic_index |= 0x200;
4134 return vf;
4135 }
4136
4137 if (kfifo_get(&dec->display_q, &vf)) {
4138 uint8_t index = vf->index & 0xff;
4139 if (index < dec->used_buf_num) {
4140 struct avs2_frame_s *pic = get_pic_by_index(dec, index);
4141 if (pic == NULL &&
4142 (debug & AVS2_DBG_PIC_LEAK)) {
4143 int i;
4144 avs2_print(dec, 0,
4145 "%s error index 0x%x pic not exist\n",
4146 __func__, index);
4147 dump_pic_list(dec);
4148 for (i = 0; i < 10; i++) {
4149 pic = get_pic_by_index(dec, index);
4150 pr_info("pic = %p\n", pic);
4151 }
4152
4153 if (debug & AVS2_DBG_PIC_LEAK)
4154 debug |= AVS2_DBG_PIC_LEAK_WAIT;
4155 return NULL;
4156 }
4157 dec->vf_get_count++;
4158 if (pic)
4159 avs2_print(dec, AVS2_DBG_BUFMGR,
4160 "%s index 0x%x pos %d getcount %d type 0x%x w/h %d/%d, pts %d, %lld\n",
4161 __func__, index,
4162 pic->imgtr_fwRefDistance_bak,
4163 dec->vf_get_count,
4164 vf->type,
4165 vf->width, vf->height,
4166 vf->pts,
4167 vf->pts_us64);
4168 return vf;
4169 }
4170 }
4171 return NULL;
4172}
4173
4174static void vavs2_vf_put(struct vframe_s *vf, void *op_arg)
4175{
4176 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)op_arg;
4177 uint8_t index = vf->index & 0xff;
4178
4179 if (vf == (&dec->vframe_dummy))
4180 return;
4181
4182 kfifo_put(&dec->newframe_q, (const struct vframe_s *)vf);
4183 dec->vf_put_count++;
4184 avs2_print(dec, AVS2_DBG_BUFMGR,
4185 "%s index putcount 0x%x %d\n",
4186 __func__, vf->index,
4187 dec->vf_put_count);
4188
4189 if (index < dec->used_buf_num) {
4190 unsigned long flags;
4191 struct avs2_frame_s *pic;
4192
4193 lock_buffer(dec, flags);
4194 pic = get_pic_by_index(dec, index);
4195 if (pic && pic->vf_ref > 0)
4196 pic->vf_ref--;
4197 else {
4198 if (pic)
4199 avs2_print(dec, 0,
4200 "%s, error pic (index %d) vf_ref is %d\n",
4201 __func__, index, pic->vf_ref);
4202 else
4203 avs2_print(dec, 0,
4204 "%s, error pic (index %d) is NULL\n",
4205 __func__, index);
4206 }
4207 if (dec->wait_buf)
4208 WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG,
4209 0x1);
4210 dec->last_put_idx = index;
4211 dec->new_frame_displayed++;
4212 unlock_buffer(dec, flags);
4213 }
4214
4215}
4216
4217static int vavs2_event_cb(int type, void *data, void *private_data)
4218{
4219 return 0;
4220}
4221
4222static struct avs2_frame_s *get_disp_pic(struct AVS2Decoder_s *dec)
4223{
4224 struct avs2_decoder *avs2_dec = &dec->avs2_dec;
4225 struct avs2_frame_s *pic = NULL;
4226 int32_t j;
4227 int32_t pre_disp_count_min = 0x7fffffff;
4228 for (j = 0; j < avs2_dec->ref_maxbuffer; j++) {
4229 if (avs2_dec->fref[j]->to_prepare_disp &&
4230 avs2_dec->fref[j]->to_prepare_disp <
4231 pre_disp_count_min) {
4232 pre_disp_count_min =
4233 avs2_dec->fref[j]->to_prepare_disp;
4234 pic = avs2_dec->fref[j];
4235 }
4236 }
4237 if (pic)
4238 pic->to_prepare_disp = 0;
4239
4240 return pic;
4241
4242}
4243
4244
4245
4246static void fill_frame_info(struct AVS2Decoder_s *dec,
4247 struct avs2_frame_s *pic, unsigned int framesize, unsigned int pts)
4248{
4249 struct vframe_qos_s *vframe_qos = &dec->vframe_qos;
4250
4251 if (pic->slice_type == I_IMG)
4252 vframe_qos->type = 1;
4253 else if (pic->slice_type == P_IMG)
4254 vframe_qos->type = 2;
4255 else if (pic->slice_type == B_IMG)
4256 vframe_qos->type = 3;
4257/*
4258#define SHOW_QOS_INFO
4259*/
4260 if (input_frame_based(hw_to_vdec(dec)))
4261 vframe_qos->size = pic->frame_size;
4262 else
4263 vframe_qos->size = framesize;
4264 vframe_qos->pts = pts;
4265#ifdef SHOW_QOS_INFO
4266 avs2_print(dec, 0, "slice:%d\n", pic->slice_type);
4267#endif
4268
4269
4270 vframe_qos->max_mv = pic->max_mv;
4271 vframe_qos->avg_mv = pic->avg_mv;
4272 vframe_qos->min_mv = pic->min_mv;
4273#ifdef SHOW_QOS_INFO
4274 avs2_print(dec, 0, "mv: max:%d, avg:%d, min:%d\n",
4275 vframe_qos->max_mv,
4276 vframe_qos->avg_mv,
4277 vframe_qos->min_mv);
4278#endif
4279
4280 vframe_qos->max_qp = pic->max_qp;
4281 vframe_qos->avg_qp = pic->avg_qp;
4282 vframe_qos->min_qp = pic->min_qp;
4283#ifdef SHOW_QOS_INFO
4284 avs2_print(dec, 0, "qp: max:%d, avg:%d, min:%d\n",
4285 vframe_qos->max_qp,
4286 vframe_qos->avg_qp,
4287 vframe_qos->min_qp);
4288#endif
4289
4290 vframe_qos->max_skip = pic->max_skip;
4291 vframe_qos->avg_skip = pic->avg_skip;
4292 vframe_qos->min_skip = pic->min_skip;
4293#ifdef SHOW_QOS_INFO
4294 avs2_print(dec, 0, "skip: max:%d, avg:%d, min:%d\n",
4295 vframe_qos->max_skip,
4296 vframe_qos->avg_skip,
4297 vframe_qos->min_skip);
4298#endif
4299
4300 vframe_qos->num++;
4301
4302}
4303
4304static void set_vframe(struct AVS2Decoder_s *dec,
4305 struct vframe_s *vf, struct avs2_frame_s *pic, u8 dummy)
4306{
4307 unsigned long flags;
4308 int stream_offset;
4309 unsigned int frame_size = 0;
4310 int pts_discontinue;
4311 stream_offset = pic->stream_offset;
4312 avs2_print(dec, AVS2_DBG_BUFMGR,
4313 "%s index = %d pos = %d\r\n",
4314 __func__, pic->index,
4315 pic->imgtr_fwRefDistance);
4316
4317 if (pic->double_write_mode)
4318 set_canvas(dec, pic);
4319
4320 display_frame_count[dec->index]++;
4321
4322 if (!dummy) {
4323#ifdef MULTI_INSTANCE_SUPPORT
4324 if (vdec_frame_based(hw_to_vdec(dec))) {
4325 vf->pts = pic->pts;
4326 vf->pts_us64 = pic->pts64;
4327 } else
4328#endif
4329 /* if (pts_lookup_offset(PTS_TYPE_VIDEO,
4330 stream_offset, &vf->pts, 0) != 0) { */
4331 if (pts_lookup_offset_us64
4332 (PTS_TYPE_VIDEO, stream_offset,
4333 &vf->pts, &frame_size, 0,
4334 &vf->pts_us64) != 0) {
4335#ifdef DEBUG_PTS
4336 dec->pts_missed++;
4337#endif
4338 vf->pts = 0;
4339 vf->pts_us64 = 0;
4340 }
4341#ifdef DEBUG_PTS
4342 else
4343 dec->pts_hit++;
4344#endif
4345 if (pts_unstable)
4346 dec->pts_mode = PTS_NONE_REF_USE_DURATION;
4347
4348 fill_frame_info(dec, pic, frame_size, vf->pts);
4349
4350 if ((dec->pts_mode == PTS_NORMAL) && (vf->pts != 0)
4351 && dec->get_frame_dur) {
4352 int pts_diff = (int)vf->pts - dec->last_lookup_pts;
4353
4354 if (pts_diff < 0) {
4355 dec->pts_mode_switching_count++;
4356 dec->pts_mode_recovery_count = 0;
4357
4358 if (dec->pts_mode_switching_count >=
4359 PTS_MODE_SWITCHING_THRESHOLD) {
4360 dec->pts_mode =
4361 PTS_NONE_REF_USE_DURATION;
4362 pr_info
4363 ("HEVC: switch to n_d mode.\n");
4364 }
4365
4366 } else {
4367 int p = PTS_MODE_SWITCHING_RECOVERY_THREASHOLD;
4368 dec->pts_mode_recovery_count++;
4369 if (dec->pts_mode_recovery_count > p) {
4370 dec->pts_mode_switching_count = 0;
4371 dec->pts_mode_recovery_count = 0;
4372 }
4373 }
4374 }
4375
4376 pts_discontinue =
4377 (abs(dec->last_pts - vf->pts) >=
4378 tsync_vpts_discontinuity_margin());
4379
4380 if (vf->pts != 0)
4381 dec->last_lookup_pts = vf->pts;
4382#if 1
4383 if ((dec->pts_mode == PTS_NONE_REF_USE_DURATION)
4384 && ((pic->slice_type != I_IMG) || (!pts_discontinue &&
4385 !first_pts_checkin_complete(PTS_TYPE_AUDIO))))
4386 vf->pts = dec->last_pts + DUR2PTS(dec->frame_dur);
4387#endif
4388 dec->last_pts = vf->pts;
4389
4390 if (vf->pts_us64 != 0)
4391 dec->last_lookup_pts_us64 = vf->pts_us64;
4392
4393#if 1
4394 if ((dec->pts_mode == PTS_NONE_REF_USE_DURATION)
4395 && ((pic->slice_type != I_IMG) || (!pts_discontinue &&
4396 !first_pts_checkin_complete(PTS_TYPE_AUDIO)))) {
4397 vf->pts_us64 =
4398 dec->last_pts_us64 +
4399 (DUR2PTS(dec->frame_dur) * 100 / 9);
4400 }
4401#endif
4402 dec->last_pts_us64 = vf->pts_us64;
4403 avs2_print(dec, AVS2_DBG_OUT_PTS,
4404 "avs2 dec out pts: vf->pts=%d, vf->pts_us64 = %lld\n",
4405 vf->pts, vf->pts_us64);
4406 }
4407
4408 vf->index = 0xff00 | pic->index;
4409
4410 if (pic->double_write_mode & 0x10) {
4411 /* double write only */
4412 vf->compBodyAddr = 0;
4413 vf->compHeadAddr = 0;
4414 } else {
4415#ifdef AVS2_10B_MMU
4416 vf->compBodyAddr = 0;
4417 vf->compHeadAddr = pic->header_adr;
4418#else
4419 vf->compBodyAddr = pic->mc_y_adr; /*body adr*/
4420 vf->compHeadAddr = pic->mc_y_adr +
4421 pic->comp_body_size;
4422 /*head adr*/
4423#endif
4424 }
4425 if (pic->double_write_mode) {
4426 vf->type = VIDTYPE_PROGRESSIVE |
4427 VIDTYPE_VIU_FIELD;
4428 vf->type |= VIDTYPE_VIU_NV21;
4429 if (pic->double_write_mode == 3) {
4430 vf->type |= VIDTYPE_COMPRESS;
4431#ifdef AVS2_10B_MMU
4432 vf->type |= VIDTYPE_SCATTER;
4433#endif
4434 }
4435#ifdef MULTI_INSTANCE_SUPPORT
4436 if (dec->m_ins_flag) {
4437 vf->canvas0Addr = vf->canvas1Addr = -1;
4438 vf->plane_num = 2;
4439 vf->canvas0_config[0] =
4440 pic->canvas_config[0];
4441 vf->canvas0_config[1] =
4442 pic->canvas_config[1];
4443
4444 vf->canvas1_config[0] =
4445 pic->canvas_config[0];
4446 vf->canvas1_config[1] =
4447 pic->canvas_config[1];
4448
4449 } else
4450#endif
4451 vf->canvas0Addr = vf->canvas1Addr =
4452 spec2canvas(pic);
4453 } else {
4454 vf->canvas0Addr = vf->canvas1Addr = 0;
4455 vf->type = VIDTYPE_COMPRESS | VIDTYPE_VIU_FIELD;
4456#ifdef AVS2_10B_MMU
4457 vf->type |= VIDTYPE_SCATTER;
4458#endif
4459 }
4460
4461 switch (pic->bit_depth) {
4462 case AVS2_BITS_8:
4463 vf->bitdepth = BITDEPTH_Y8 |
4464 BITDEPTH_U8 | BITDEPTH_V8;
4465 break;
4466 case AVS2_BITS_10:
4467 case AVS2_BITS_12:
4468 vf->bitdepth = BITDEPTH_Y10 |
4469 BITDEPTH_U10 | BITDEPTH_V10;
4470 break;
4471 default:
4472 vf->bitdepth = BITDEPTH_Y10 |
4473 BITDEPTH_U10 | BITDEPTH_V10;
4474 break;
4475 }
4476 if ((vf->type & VIDTYPE_COMPRESS) == 0)
4477 vf->bitdepth =
4478 BITDEPTH_Y8 | BITDEPTH_U8 | BITDEPTH_V8;
4479 if (pic->bit_depth == AVS2_BITS_8)
4480 vf->bitdepth |= BITDEPTH_SAVING_MODE;
4481
4482 set_frame_info(dec, vf);
4483 /* if((vf->width!=pic->width)|
4484 (vf->height!=pic->height)) */
4485 /* pr_info("aaa: %d/%d, %d/%d\n",
4486 vf->width,vf->height, pic->width,
4487 pic->height); */
4488 vf->width = pic->pic_w /
4489 get_double_write_ratio(dec,
4490 pic->double_write_mode);
4491 vf->height = pic->pic_h /
4492 get_double_write_ratio(dec,
4493 pic->double_write_mode);
4494 if (force_w_h != 0) {
4495 vf->width = (force_w_h >> 16) & 0xffff;
4496 vf->height = force_w_h & 0xffff;
4497 }
4498 vf->compWidth = pic->pic_w;
4499 vf->compHeight = pic->pic_h;
4500 if (force_fps & 0x100) {
4501 u32 rate = force_fps & 0xff;
4502 if (rate)
4503 vf->duration = 96000/rate;
4504 else
4505 vf->duration = 0;
4506 }
4507#ifdef AVS2_10B_MMU
4508 if (vf->type & VIDTYPE_SCATTER) {
4509 vf->mem_handle = decoder_mmu_box_get_mem_handle(
4510 dec->mmu_box,
4511 pic->index);
4512 vf->mem_head_handle = decoder_bmmu_box_get_mem_handle(
4513 dec->bmmu_box,
4514 HEADER_BUFFER_IDX(pic->index));
4515 } else {
4516 vf->mem_handle = decoder_bmmu_box_get_mem_handle(
4517 dec->bmmu_box,
4518 VF_BUFFER_IDX(pic->index));
4519 vf->mem_head_handle = decoder_bmmu_box_get_mem_handle(
4520 dec->bmmu_box,
4521 HEADER_BUFFER_IDX(pic->index));
4522 }
4523#else
4524 vf->mem_handle = decoder_bmmu_box_get_mem_handle(
4525 dec->bmmu_box,
4526 VF_BUFFER_IDX(pic->index));
4527#endif
4528
4529 if (!dummy) {
4530 lock_buffer(dec, flags);
4531 pic->vf_ref = 1;
4532 unlock_buffer(dec, flags);
4533 }
4534 dec->vf_pre_count++;
4535}
4536
4537static inline void dec_update_gvs(struct AVS2Decoder_s *dec)
4538{
4539 if (dec->gvs->frame_height != dec->frame_height) {
4540 dec->gvs->frame_width = dec->frame_width;
4541 dec->gvs->frame_height = dec->frame_height;
4542 }
4543 if (dec->gvs->frame_dur != dec->frame_dur) {
4544 dec->gvs->frame_dur = dec->frame_dur;
4545 if (dec->frame_dur != 0)
4546 dec->gvs->frame_rate = 96000 / dec->frame_dur;
4547 else
4548 dec->gvs->frame_rate = -1;
4549 }
4550 dec->gvs->status = dec->stat | dec->fatal_error;
4551}
4552
4553
4554static int avs2_prepare_display_buf(struct AVS2Decoder_s *dec)
4555{
4556#ifndef NO_DISPLAY
4557 struct vframe_s *vf = NULL;
4558 /*unsigned short slice_type;*/
4559 struct avs2_frame_s *pic;
4560 struct vdec_s *pvdec = hw_to_vdec(dec);
4561 while (1) {
4562 pic = get_disp_pic(dec);
4563 if (pic == NULL)
4564 break;
4565
4566 if (force_disp_pic_index & 0x100) {
4567 /*recycle directly*/
4568 continue;
4569 }
4570
4571 if (pic->error_mark) {
4572 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
4573 "!!!error pic, skip\n",
4574 0);
4575 continue;
4576 }
4577
4578 if (dec->start_decoding_flag != 0) {
4579 if (dec->skip_PB_before_I &&
4580 pic->slice_type != I_IMG) {
4581 avs2_print(dec, AVS2_DBG_BUFMGR_DETAIL,
4582 "!!!slice type %d (not I) skip\n",
4583 0, pic->slice_type);
4584 continue;
4585 }
4586 dec->skip_PB_before_I = 0;
4587 }
4588
4589 if (kfifo_get(&dec->newframe_q, &vf) == 0) {
4590 pr_info("fatal error, no available buffer slot.");
4591 return -1;
4592 }
4593
4594 if (vf) {
4595 struct vdec_info tmp4x;
4596 int stream_offset = pic->stream_offset;
4597 set_vframe(dec, vf, pic, 0);
4598 decoder_do_frame_check(pvdec, vf);
4599 kfifo_put(&dec->display_q, (const struct vframe_s *)vf);
4600 ATRACE_COUNTER(MODULE_NAME, vf->pts);
4601
4602 dec_update_gvs(dec);
4603 /*count info*/
4604 vdec_count_info(dec->gvs, 0, stream_offset);
4605 memcpy(&tmp4x, dec->gvs, sizeof(struct vdec_info));
4606 tmp4x.bit_depth_luma = bit_depth_luma;
4607 tmp4x.bit_depth_chroma = bit_depth_chroma;
4608 tmp4x.double_write_mode = get_double_write_mode(dec);
4609 vdec_fill_vdec_frame(pvdec, &dec->vframe_qos, &tmp4x, vf, pic->hw_decode_time);
4610 pvdec->vdec_fps_detec(pvdec->id);
4611 if (without_display_mode == 0) {
4612 vf_notify_receiver(dec->provider_name,
4613 VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL);
4614 } else
4615 vavs2_vf_put(vavs2_vf_get(dec), dec);
4616 }
4617 }
4618/*!NO_DISPLAY*/
4619#endif
4620 return 0;
4621}
4622
4623static void get_rpm_param(union param_u *params)
4624{
4625 int i;
4626 unsigned int data32;
4627 if (debug & AVS2_DBG_BUFMGR)
4628 pr_info("enter %s\r\n", __func__);
4629 for (i = 0; i < (RPM_END - RPM_BEGIN); i++) {
4630 do {
4631 data32 = READ_VREG(RPM_CMD_REG);
4632 /*pr_info("%x\n", data32);*/
4633 } while ((data32 & 0x10000) == 0);
4634 params->l.data[i] = data32&0xffff;
4635 /*pr_info("%x\n", data32);*/
4636 WRITE_VREG(RPM_CMD_REG, 0);
4637 }
4638 if (debug & AVS2_DBG_BUFMGR)
4639 pr_info("leave %s\r\n", __func__);
4640}
4641static void debug_buffer_mgr_more(struct AVS2Decoder_s *dec)
4642{
4643 int i;
4644 if (!(debug & AVS2_DBG_BUFMGR_MORE))
4645 return;
4646 pr_info("avs2_param: (%d)\n", dec->avs2_dec.img.number);
4647 for (i = 0; i < (RPM_END-RPM_BEGIN); i++) {
4648 pr_info("%04x ", dec->avs2_dec.param.l.data[i]);
4649 if (((i + 1) & 0xf) == 0)
4650 pr_info("\n");
4651 }
4652}
4653
4654#ifdef AVS2_10B_MMU
4655static void avs2_recycle_mmu_buf_tail(struct AVS2Decoder_s *dec)
4656{
4657 if (dec->cur_fb_idx_mmu != INVALID_IDX) {
4658 if (dec->used_4k_num == -1) {
4659 dec->used_4k_num =
4660 (READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
4661 if (dec->m_ins_flag)
4662 hevc_mmu_dma_check(hw_to_vdec(dec));
4663 decoder_mmu_box_free_idx_tail(dec->mmu_box,
4664 dec->cur_fb_idx_mmu, dec->used_4k_num);
4665 }
4666 dec->cur_fb_idx_mmu = INVALID_IDX;
4667 dec->used_4k_num = -1;
4668 }
4669}
4670
4671static void avs2_recycle_mmu_buf(struct AVS2Decoder_s *dec)
4672{
4673 if (dec->cur_fb_idx_mmu != INVALID_IDX) {
4674 decoder_mmu_box_free_idx(dec->mmu_box,
4675 dec->cur_fb_idx_mmu);
4676
4677 dec->cur_fb_idx_mmu = INVALID_IDX;
4678 dec->used_4k_num = -1;
4679 }
4680}
4681#endif
4682
4683static void dec_again_process(struct AVS2Decoder_s *dec)
4684{
4685 amhevc_stop();
4686 dec->dec_result = DEC_RESULT_AGAIN;
4687 if (dec->process_state ==
4688 PROC_STATE_DECODING) {
4689 dec->process_state =
4690 PROC_STATE_DECODE_AGAIN;
4691 } else if (dec->process_state ==
4692 PROC_STATE_HEAD_DONE) {
4693 dec->process_state =
4694 PROC_STATE_HEAD_AGAIN;
4695 }
4696 dec->next_again_flag = 1;
4697 reset_process_time(dec);
4698 vdec_schedule_work(&dec->work);
4699}
4700
4701static uint32_t log2i(uint32_t val)
4702{
4703 uint32_t ret = -1;
4704 while (val != 0) {
4705 val >>= 1;
4706 ret++;
4707 }
4708 return ret;
4709}
4710
4711static void check_pic_error(struct AVS2Decoder_s *dec,
4712 struct avs2_frame_s *pic)
4713{
4714 if (pic->decoded_lcu == 0) {
4715 pic->decoded_lcu =
4716 (READ_VREG(HEVC_PARSER_LCU_START)
4717 & 0xffffff) + 1;
4718 }
4719 if (pic->decoded_lcu != dec->avs2_dec.lcu_total) {
4720 avs2_print(dec, AVS2_DBG_BUFMGR,
4721 "%s error pic(index %d imgtr_fwRefDistance %d) decoded lcu %d (total %d)\n",
4722 __func__, pic->index, pic->imgtr_fwRefDistance,
4723 pic->decoded_lcu, dec->avs2_dec.lcu_total);
4724 pic->error_mark = 1;
4725 } else {
4726 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
4727 "%s pic(index %d imgtr_fwRefDistance %d) decoded lcu %d (total %d)\n",
4728 __func__, pic->index, pic->imgtr_fwRefDistance,
4729 pic->decoded_lcu, dec->avs2_dec.lcu_total);
4730
4731 }
4732}
4733static void update_decoded_pic(struct AVS2Decoder_s *dec)
4734{
4735 struct avs2_frame_s *pic = dec->avs2_dec.hc.cur_pic;
4736 if (pic) {
4737 dec->avs2_dec.hc.cur_pic->decoded_lcu =
4738 (READ_VREG(HEVC_PARSER_LCU_START)
4739 & 0xffffff) + 1;
4740 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
4741 "%s pic(index %d imgtr_fwRefDistance %d) decoded lcu %d (total %d)\n",
4742 __func__, pic->index, pic->imgtr_fwRefDistance,
4743 pic->decoded_lcu, dec->avs2_dec.lcu_total);
4744 }
4745}
4746/* +[SE] [BUG][BUG-171463][chuanqi.wang]: get frame rate by video sequeue*/
4747static int get_frame_rate(union param_u *params, struct AVS2Decoder_s *dec)
4748{
4749 int tmp = 0;
4750
4751 switch (params->p.frame_rate_code) {
4752 case 1:
4753 case 2:
4754 tmp = 24;
4755 break;
4756 case 3:
4757 tmp = 25;
4758 break;
4759 case 4:
4760 case 5:
4761 tmp = 30;
4762 break;
4763 case 6:
4764 tmp = 50;
4765 break;
4766 case 7:
4767 case 8:
4768 tmp = 60;
4769 break;
4770 case 9:
4771 tmp = 100;
4772 break;
4773 case 10:
4774 tmp = 120;
4775 break;
4776 default:
4777 tmp = 25;
4778 break;
4779 }
4780
4781 if (!params->p.progressive_sequence)
4782 tmp = tmp / 2;
4783 dec->frame_dur = div_u64(96000ULL, tmp);
4784 dec->get_frame_dur = true;
4785 /*avs2_print(dec, 0, "avs2 frame_dur:%d,progressive:%d\n", dec->frame_dur, params->p.progressive_sequence);*/
4786 return 0;
4787}
4788
4789
4790#define HEVC_MV_INFO 0x310d
4791#define HEVC_QP_INFO 0x3137
4792#define HEVC_SKIP_INFO 0x3136
4793
4794/* only when we decoded one field or one frame,
4795we can call this function to get qos info*/
4796static void get_picture_qos_info(struct AVS2Decoder_s *dec)
4797{
4798 struct avs2_frame_s *picture = dec->avs2_dec.hc.cur_pic;
4799 struct vdec_s *vdec = hw_to_vdec(dec);
4800 if (!picture) {
4801 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
4802 "%s decode picture is none exist\n");
4803
4804 return;
4805 }
4806 if (vdec->mvfrm) {
4807 picture->frame_size = vdec->mvfrm->frame_size;
4808 picture->hw_decode_time =
4809 local_clock() - vdec->mvfrm->hw_decode_start;
4810 }
4811
4812/*
4813#define DEBUG_QOS
4814*/
4815
4816 if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_G12A) {
4817 unsigned char a[3];
4818 unsigned char i, j, t;
4819 unsigned long data;
4820
4821 data = READ_VREG(HEVC_MV_INFO);
4822 if (picture->slice_type == I_IMG)
4823 data = 0;
4824 a[0] = data & 0xff;
4825 a[1] = (data >> 8) & 0xff;
4826 a[2] = (data >> 16) & 0xff;
4827
4828 for (i = 0; i < 3; i++)
4829 for (j = i+1; j < 3; j++) {
4830 if (a[j] < a[i]) {
4831 t = a[j];
4832 a[j] = a[i];
4833 a[i] = t;
4834 } else if (a[j] == a[i]) {
4835 a[i]++;
4836 t = a[j];
4837 a[j] = a[i];
4838 a[i] = t;
4839 }
4840 }
4841 picture->max_mv = a[2];
4842 picture->avg_mv = a[1];
4843 picture->min_mv = a[0];
4844#ifdef DEBUG_QOS
4845 avs2_print(dec, 0, "mv data %x a[0]= %x a[1]= %x a[2]= %x\n",
4846 data, a[0], a[1], a[2]);
4847#endif
4848
4849 data = READ_VREG(HEVC_QP_INFO);
4850 a[0] = data & 0x1f;
4851 a[1] = (data >> 8) & 0x3f;
4852 a[2] = (data >> 16) & 0x7f;
4853
4854 for (i = 0; i < 3; i++)
4855 for (j = i+1; j < 3; j++) {
4856 if (a[j] < a[i]) {
4857 t = a[j];
4858 a[j] = a[i];
4859 a[i] = t;
4860 } else if (a[j] == a[i]) {
4861 a[i]++;
4862 t = a[j];
4863 a[j] = a[i];
4864 a[i] = t;
4865 }
4866 }
4867 picture->max_qp = a[2];
4868 picture->avg_qp = a[1];
4869 picture->min_qp = a[0];
4870#ifdef DEBUG_QOS
4871 avs2_print(dec, 0, "qp data %x a[0]= %x a[1]= %x a[2]= %x\n",
4872 data, a[0], a[1], a[2]);
4873#endif
4874
4875 data = READ_VREG(HEVC_SKIP_INFO);
4876 a[0] = data & 0x1f;
4877 a[1] = (data >> 8) & 0x3f;
4878 a[2] = (data >> 16) & 0x7f;
4879
4880 for (i = 0; i < 3; i++)
4881 for (j = i+1; j < 3; j++) {
4882 if (a[j] < a[i]) {
4883 t = a[j];
4884 a[j] = a[i];
4885 a[i] = t;
4886 } else if (a[j] == a[i]) {
4887 a[i]++;
4888 t = a[j];
4889 a[j] = a[i];
4890 a[i] = t;
4891 }
4892 }
4893 picture->max_skip = a[2];
4894 picture->avg_skip = a[1];
4895 picture->min_skip = a[0];
4896
4897#ifdef DEBUG_QOS
4898 avs2_print(dec, 0,
4899 "skip data %x a[0]= %x a[1]= %x a[2]= %x\n",
4900 data, a[0], a[1], a[2]);
4901#endif
4902 } else {
4903 uint32_t blk88_y_count;
4904 uint32_t blk88_c_count;
4905 uint32_t blk22_mv_count;
4906 uint32_t rdata32;
4907 int32_t mv_hi;
4908 int32_t mv_lo;
4909 uint32_t rdata32_l;
4910 uint32_t mvx_L0_hi;
4911 uint32_t mvy_L0_hi;
4912 uint32_t mvx_L1_hi;
4913 uint32_t mvy_L1_hi;
4914 int64_t value;
4915 uint64_t temp_value;
4916#ifdef DEBUG_QOS
4917 int pic_number = 0;
4918#endif
4919
4920 picture->max_mv = 0;
4921 picture->avg_mv = 0;
4922 picture->min_mv = 0;
4923
4924 picture->max_skip = 0;
4925 picture->avg_skip = 0;
4926 picture->min_skip = 0;
4927
4928 picture->max_qp = 0;
4929 picture->avg_qp = 0;
4930 picture->min_qp = 0;
4931
4932
4933
4934#ifdef DEBUG_QOS
4935 avs2_print(dec, 0, "slice_type:%d, poc:%d\n",
4936 picture->slice_type,
4937 pic_number);
4938#endif
4939 /* set rd_idx to 0 */
4940 WRITE_VREG(HEVC_PIC_QUALITY_CTRL, 0);
4941
4942 blk88_y_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
4943 if (blk88_y_count == 0) {
4944#ifdef DEBUG_QOS
4945 avs2_print(dec, 0,
4946 "[Picture %d Quality] NO Data yet.\n",
4947 pic_number);
4948#endif
4949 /* reset all counts */
4950 WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
4951 return;
4952 }
4953 /* qp_y_sum */
4954 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
4955#ifdef DEBUG_QOS
4956 avs2_print(dec, 0,
4957 "[Picture %d Quality] Y QP AVG : %d (%d/%d)\n",
4958 pic_number, rdata32/blk88_y_count,
4959 rdata32, blk88_y_count);
4960#endif
4961 picture->avg_qp = rdata32/blk88_y_count;
4962 /* intra_y_count */
4963 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
4964#ifdef DEBUG_QOS
4965 avs2_print(dec, 0,
4966 "[Picture %d Quality] Y intra rate : %d%c (%d)\n",
4967 pic_number, rdata32*100/blk88_y_count,
4968 '%', rdata32);
4969#endif
4970 /* skipped_y_count */
4971 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
4972#ifdef DEBUG_QOS
4973 avs2_print(dec, 0,
4974 "[Picture %d Quality] Y skipped rate : %d%c (%d)\n",
4975 pic_number, rdata32*100/blk88_y_count,
4976 '%', rdata32);
4977#endif
4978 picture->avg_skip = rdata32*100/blk88_y_count;
4979 /* coeff_non_zero_y_count */
4980 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
4981#ifdef DEBUG_QOS
4982 avs2_print(dec, 0,
4983 "[Picture %d Quality] Y ZERO_Coeff rate : %d%c (%d)\n",
4984 pic_number, (100 - rdata32*100/(blk88_y_count*1)),
4985 '%', rdata32);
4986#endif
4987 /* blk66_c_count */
4988 blk88_c_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
4989 if (blk88_c_count == 0) {
4990#ifdef DEBUG_QOS
4991 avs2_print(dec, 0,
4992 "[Picture %d Quality] NO Data yet.\n",
4993 pic_number);
4994#endif
4995 /* reset all counts */
4996 WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
4997 return;
4998 }
4999 /* qp_c_sum */
5000 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5001#ifdef DEBUG_QOS
5002 avs2_print(dec, 0,
5003 "[Picture %d Quality] C QP AVG : %d (%d/%d)\n",
5004 pic_number, rdata32/blk88_c_count,
5005 rdata32, blk88_c_count);
5006#endif
5007 /* intra_c_count */
5008 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5009#ifdef DEBUG_QOS
5010 avs2_print(dec, 0,
5011 "[Picture %d Quality] C intra rate : %d%c (%d)\n",
5012 pic_number, rdata32*100/blk88_c_count,
5013 '%', rdata32);
5014#endif
5015 /* skipped_cu_c_count */
5016 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5017#ifdef DEBUG_QOS
5018 avs2_print(dec, 0,
5019 "[Picture %d Quality] C skipped rate : %d%c (%d)\n",
5020 pic_number, rdata32*100/blk88_c_count,
5021 '%', rdata32);
5022#endif
5023 /* coeff_non_zero_c_count */
5024 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5025#ifdef DEBUG_QOS
5026 avs2_print(dec, 0,
5027 "[Picture %d Quality] C ZERO_Coeff rate : %d%c (%d)\n",
5028 pic_number, (100 - rdata32*100/(blk88_c_count*1)),
5029 '%', rdata32);
5030#endif
5031
5032 /* 1'h0, qp_c_max[6:0], 1'h0, qp_c_min[6:0],
5033 1'h0, qp_y_max[6:0], 1'h0, qp_y_min[6:0] */
5034 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5035#ifdef DEBUG_QOS
5036 avs2_print(dec, 0, "[Picture %d Quality] Y QP min : %d\n",
5037 pic_number, (rdata32>>0)&0xff);
5038#endif
5039 picture->min_qp = (rdata32>>0)&0xff;
5040
5041#ifdef DEBUG_QOS
5042 avs2_print(dec, 0, "[Picture %d Quality] Y QP max : %d\n",
5043 pic_number, (rdata32>>8)&0xff);
5044#endif
5045 picture->max_qp = (rdata32>>8)&0xff;
5046
5047#ifdef DEBUG_QOS
5048 avs2_print(dec, 0, "[Picture %d Quality] C QP min : %d\n",
5049 pic_number, (rdata32>>16)&0xff);
5050 avs2_print(dec, 0, "[Picture %d Quality] C QP max : %d\n",
5051 pic_number, (rdata32>>24)&0xff);
5052#endif
5053
5054 /* blk22_mv_count */
5055 blk22_mv_count = READ_VREG(HEVC_PIC_QUALITY_DATA);
5056 if (blk22_mv_count == 0) {
5057#ifdef DEBUG_QOS
5058 avs2_print(dec, 0,
5059 "[Picture %d Quality] NO MV Data yet.\n",
5060 pic_number);
5061#endif
5062 /* reset all counts */
5063 WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
5064 return;
5065 }
5066 /* mvy_L1_count[39:32], mvx_L1_count[39:32],
5067 mvy_L0_count[39:32], mvx_L0_count[39:32] */
5068 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5069 /* should all be 0x00 or 0xff */
5070#ifdef DEBUG_QOS
5071 avs2_print(dec, 0,
5072 "[Picture %d Quality] MV AVG High Bits: 0x%X\n",
5073 pic_number, rdata32);
5074#endif
5075 mvx_L0_hi = ((rdata32>>0)&0xff);
5076 mvy_L0_hi = ((rdata32>>8)&0xff);
5077 mvx_L1_hi = ((rdata32>>16)&0xff);
5078 mvy_L1_hi = ((rdata32>>24)&0xff);
5079
5080 /* mvx_L0_count[31:0] */
5081 rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
5082 temp_value = mvx_L0_hi;
5083 temp_value = (temp_value << 32) | rdata32_l;
5084
5085 if (mvx_L0_hi & 0x80)
5086 value = 0xFFFFFFF000000000 | temp_value;
5087 else
5088 value = temp_value;
5089 value = div_s64(value, blk22_mv_count);
5090#ifdef DEBUG_QOS
5091 avs2_print(dec, 0,
5092 "[Picture %d Quality] MVX_L0 AVG : %d (%lld/%d)\n",
5093 pic_number, (int)value,
5094 value, blk22_mv_count);
5095#endif
5096 picture->avg_mv = value;
5097
5098 /* mvy_L0_count[31:0] */
5099 rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
5100 temp_value = mvy_L0_hi;
5101 temp_value = (temp_value << 32) | rdata32_l;
5102
5103 if (mvy_L0_hi & 0x80)
5104 value = 0xFFFFFFF000000000 | temp_value;
5105 else
5106 value = temp_value;
5107#ifdef DEBUG_QOS
5108 avs2_print(dec, 0,
5109 "[Picture %d Quality] MVY_L0 AVG : %d (%lld/%d)\n",
5110 pic_number, rdata32_l/blk22_mv_count,
5111 value, blk22_mv_count);
5112#endif
5113
5114 /* mvx_L1_count[31:0] */
5115 rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
5116 temp_value = mvx_L1_hi;
5117 temp_value = (temp_value << 32) | rdata32_l;
5118 if (mvx_L1_hi & 0x80)
5119 value = 0xFFFFFFF000000000 | temp_value;
5120 else
5121 value = temp_value;
5122#ifdef DEBUG_QOS
5123 avs2_print(dec, 0,
5124 "[Picture %d Quality] MVX_L1 AVG : %d (%lld/%d)\n",
5125 pic_number, rdata32_l/blk22_mv_count,
5126 value, blk22_mv_count);
5127#endif
5128
5129 /* mvy_L1_count[31:0] */
5130 rdata32_l = READ_VREG(HEVC_PIC_QUALITY_DATA);
5131 temp_value = mvy_L1_hi;
5132 temp_value = (temp_value << 32) | rdata32_l;
5133 if (mvy_L1_hi & 0x80)
5134 value = 0xFFFFFFF000000000 | temp_value;
5135 else
5136 value = temp_value;
5137#ifdef DEBUG_QOS
5138 avs2_print(dec, 0,
5139 "[Picture %d Quality] MVY_L1 AVG : %d (%lld/%d)\n",
5140 pic_number, rdata32_l/blk22_mv_count,
5141 value, blk22_mv_count);
5142#endif
5143
5144 /* {mvx_L0_max, mvx_L0_min} // format : {sign, abs[14:0]} */
5145 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5146 mv_hi = (rdata32>>16)&0xffff;
5147 if (mv_hi & 0x8000)
5148 mv_hi = 0x8000 - mv_hi;
5149#ifdef DEBUG_QOS
5150 avs2_print(dec, 0, "[Picture %d Quality] MVX_L0 MAX : %d\n",
5151 pic_number, mv_hi);
5152#endif
5153 picture->max_mv = mv_hi;
5154
5155 mv_lo = (rdata32>>0)&0xffff;
5156 if (mv_lo & 0x8000)
5157 mv_lo = 0x8000 - mv_lo;
5158#ifdef DEBUG_QOS
5159 avs2_print(dec, 0, "[Picture %d Quality] MVX_L0 MIN : %d\n",
5160 pic_number, mv_lo);
5161#endif
5162 picture->min_mv = mv_lo;
5163
5164#ifdef DEBUG_QOS
5165 /* {mvy_L0_max, mvy_L0_min} */
5166 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5167 mv_hi = (rdata32>>16)&0xffff;
5168 if (mv_hi & 0x8000)
5169 mv_hi = 0x8000 - mv_hi;
5170 avs2_print(dec, 0, "[Picture %d Quality] MVY_L0 MAX : %d\n",
5171 pic_number, mv_hi);
5172
5173
5174 mv_lo = (rdata32>>0)&0xffff;
5175 if (mv_lo & 0x8000)
5176 mv_lo = 0x8000 - mv_lo;
5177
5178 avs2_print(dec, 0, "[Picture %d Quality] MVY_L0 MIN : %d\n",
5179 pic_number, mv_lo);
5180
5181
5182 /* {mvx_L1_max, mvx_L1_min} */
5183 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5184 mv_hi = (rdata32>>16)&0xffff;
5185 if (mv_hi & 0x8000)
5186 mv_hi = 0x8000 - mv_hi;
5187
5188 avs2_print(dec, 0, "[Picture %d Quality] MVX_L1 MAX : %d\n",
5189 pic_number, mv_hi);
5190
5191
5192 mv_lo = (rdata32>>0)&0xffff;
5193 if (mv_lo & 0x8000)
5194 mv_lo = 0x8000 - mv_lo;
5195
5196 avs2_print(dec, 0, "[Picture %d Quality] MVX_L1 MIN : %d\n",
5197 pic_number, mv_lo);
5198
5199
5200 /* {mvy_L1_max, mvy_L1_min} */
5201 rdata32 = READ_VREG(HEVC_PIC_QUALITY_DATA);
5202 mv_hi = (rdata32>>16)&0xffff;
5203 if (mv_hi & 0x8000)
5204 mv_hi = 0x8000 - mv_hi;
5205
5206 avs2_print(dec, 0, "[Picture %d Quality] MVY_L1 MAX : %d\n",
5207 pic_number, mv_hi);
5208
5209 mv_lo = (rdata32>>0)&0xffff;
5210 if (mv_lo & 0x8000)
5211 mv_lo = 0x8000 - mv_lo;
5212
5213 avs2_print(dec, 0, "[Picture %d Quality] MVY_L1 MIN : %d\n",
5214 pic_number, mv_lo);
5215#endif
5216
5217 rdata32 = READ_VREG(HEVC_PIC_QUALITY_CTRL);
5218#ifdef DEBUG_QOS
5219 avs2_print(dec, 0,
5220 "[Picture %d Quality] After Read : VDEC_PIC_QUALITY_CTRL : 0x%x\n",
5221 pic_number, rdata32);
5222#endif
5223 /* reset all counts */
5224 WRITE_VREG(HEVC_PIC_QUALITY_CTRL, (1<<8));
5225 }
5226}
5227
5228static irqreturn_t vavs2_isr_thread_fn(int irq, void *data)
5229{
5230 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)data;
5231 unsigned int dec_status = dec->dec_status;
5232 int i, ret;
5233 int32_t start_code = 0;
5234
5235 /*if (dec->wait_buf)
5236 pr_info("set wait_buf to 0\r\n");
5237 */
5238
5239 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
5240 "%s decode_status 0x%x process_state %d lcu 0x%x\n",
5241 __func__, dec_status, dec->process_state,
5242 READ_VREG(HEVC_PARSER_LCU_START));
5243
5244#ifndef G12A_BRINGUP_DEBUG
5245 if (dec->eos) {
5246 PRINT_LINE();
5247 goto irq_handled_exit;
5248 }
5249#endif
5250 dec->wait_buf = 0;
5251 if (dec_status == AVS2_DECODE_BUFEMPTY) {
5252 PRINT_LINE();
5253 if (dec->m_ins_flag) {
5254 reset_process_time(dec);
5255 if (!vdec_frame_based(hw_to_vdec(dec)))
5256 dec_again_process(dec);
5257 else {
5258 dec->dec_result = DEC_RESULT_DONE;
5259 reset_process_time(dec);
5260 amhevc_stop();
5261 vdec_schedule_work(&dec->work);
5262 }
5263 }
5264 goto irq_handled_exit;
5265 } else if (dec_status == HEVC_DECPIC_DATA_DONE) {
5266 PRINT_LINE();
5267 dec->start_decoding_flag |= 0x3;
5268 if (dec->m_ins_flag) {
5269 update_decoded_pic(dec);
5270 get_picture_qos_info(dec);
5271 reset_process_time(dec);
5272 dec->dec_result = DEC_RESULT_DONE;
5273 amhevc_stop();
5274#if 0 /*def AVS2_10B_MMU*/
5275 if (dec->m_ins_flag) {
5276 /*avs2_recycle_mmu_buf_tail(dec);*/
5277 dec->used_4k_num =
5278 (READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
5279 }
5280#endif
5281
5282#if 0
5283 /*keep hardware state*/
5284 WRITE_VREG(HEVC_MPRED_INT_STATUS, (1<<31));
5285 WRITE_VREG(HEVC_PARSER_RESULT_3, 0xffffffff);
5286 dec->mpred_abv_start_addr =
5287 READ_VREG(HEVC_MPRED_ABV_START_ADDR);
5288 /**/
5289#endif
5290 vdec_schedule_work(&dec->work);
5291 }
5292 goto irq_handled_exit;
5293 }
5294 PRINT_LINE();
5295#if 0
5296 if (dec_status == AVS2_EOS) {
5297 if (dec->m_ins_flag)
5298 reset_process_time(dec);
5299
5300 avs2_print(dec, AVS2_DBG_BUFMGR,
5301 "AVS2_EOS, flush buffer\r\n");
5302
5303 avs2_post_process(&dec->avs2_dec);
5304 avs2_prepare_display_buf(dec);
5305
5306 avs2_print(dec, AVS2_DBG_BUFMGR,
5307 "send AVS2_10B_DISCARD_NAL\r\n");
5308 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_10B_DISCARD_NAL);
5309 if (dec->m_ins_flag) {
5310 update_decoded_pic(dec);
5311 dec->dec_result = DEC_RESULT_DONE;
5312 amhevc_stop();
5313 vdec_schedule_work(&dec->work);
5314 }
5315 goto irq_handled_exit;
5316 } else
5317#endif
5318 if (dec_status == AVS2_DECODE_OVER_SIZE) {
5319 avs2_print(dec, 0,
5320 "avs2 decode oversize !!\n");
5321 debug |= (AVS2_DBG_DIS_LOC_ERROR_PROC |
5322 AVS2_DBG_DIS_SYS_ERROR_PROC);
5323 dec->fatal_error |= DECODER_FATAL_ERROR_SIZE_OVERFLOW;
5324 if (dec->m_ins_flag)
5325 reset_process_time(dec);
5326 goto irq_handled_exit;
5327 }
5328 PRINT_LINE();
5329
5330 if (dec->m_ins_flag)
5331 reset_process_time(dec);
5332
5333 if (dec_status == AVS2_HEAD_SEQ_READY)
5334 start_code = SEQUENCE_HEADER_CODE;
5335 else if (dec_status == AVS2_HEAD_PIC_I_READY)
5336 start_code = I_PICTURE_START_CODE;
5337 else if (dec_status == AVS2_HEAD_PIC_PB_READY)
5338 start_code = PB_PICTURE_START_CODE;
5339 else if (dec_status == AVS2_STARTCODE_SEARCH_DONE)
5340 /*SEQUENCE_END_CODE, VIDEO_EDIT_CODE*/
5341 start_code = READ_VREG(CUR_NAL_UNIT_TYPE);
5342
5343 if (dec->process_state ==
5344 PROC_STATE_HEAD_AGAIN
5345 ) {
5346 if ((start_code == I_PICTURE_START_CODE)
5347 || (start_code == PB_PICTURE_START_CODE)) {
5348 avs2_print(dec, 0,
5349 "PROC_STATE_HEAD_AGAIN error, start_code 0x%x!!!\r\n",
5350 start_code);
5351 goto irq_handled_exit;
5352 } else {
5353 avs2_print(dec, AVS2_DBG_BUFMGR,
5354 "PROC_STATE_HEAD_AGAIN, start_code 0x%x\r\n",
5355 start_code);
5356 dec->process_state = PROC_STATE_HEAD_DONE;
5357 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_ACTION_DONE);
5358 goto irq_handled_exit;
5359 }
5360 } else if (dec->process_state ==
5361 PROC_STATE_DECODE_AGAIN) {
5362 if ((start_code == I_PICTURE_START_CODE)
5363 || (start_code == PB_PICTURE_START_CODE)) {
5364 avs2_print(dec, AVS2_DBG_BUFMGR,
5365 "PROC_STATE_DECODE_AGAIN=> decode_slice, start_code 0x%x\r\n",
5366 start_code);
5367 goto decode_slice;
5368 } else {
5369 avs2_print(dec, 0,
5370 "PROC_STATE_DECODE_AGAIN, start_code 0x%x!!!\r\n",
5371 start_code);
5372 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_ACTION_DONE);
5373 goto irq_handled_exit;
5374 }
5375 }
5376
5377 if ((start_code == I_PICTURE_START_CODE)
5378 || (start_code == PB_PICTURE_START_CODE)
5379 || (start_code == SEQUENCE_END_CODE)
5380 || (start_code == VIDEO_EDIT_CODE)) {
5381 PRINT_LINE();
5382
5383 if (dec->avs2_dec.hc.cur_pic != NULL) {
5384 int32_t ii;
5385#ifdef AVS2_10B_MMU
5386 avs2_recycle_mmu_buf_tail(dec);
5387#endif
5388 check_pic_error(dec, dec->avs2_dec.hc.cur_pic);
5389 avs2_post_process(&dec->avs2_dec);
5390
5391 if (debug & AVS2_DBG_PRINT_PIC_LIST)
5392 dump_pic_list(dec);
5393
5394 avs2_prepare_display_buf(dec);
5395 dec->avs2_dec.hc.cur_pic = NULL;
5396 for (ii = 0; ii < dec->avs2_dec.ref_maxbuffer;
5397 ii++) {
5398 if (dec->avs2_dec.fref[ii]->
5399 bg_flag == 0 &&
5400 dec->avs2_dec.fref[ii]->
5401 is_output == -1 &&
5402 dec->avs2_dec.fref[ii]->
5403 mmu_alloc_flag &&
5404 dec->avs2_dec.fref[ii]->
5405 vf_ref == 0) {
5406 struct avs2_frame_s *pic =
5407 dec->avs2_dec.fref[ii];
5408 if (dec->avs2_dec.fref[ii]->
5409 refered_by_others == 0) {
5410#ifdef AVS2_10B_MMU
5411 dec->avs2_dec.fref[ii]->
5412 mmu_alloc_flag = 0;
5413 /*release_buffer_4k(
5414 dec->avs2_dec.fref[ii]->index);*/
5415 decoder_mmu_box_free_idx(dec->mmu_box,
5416 dec->avs2_dec.fref[ii]->index);
5417#ifdef DYNAMIC_ALLOC_HEAD
5418 decoder_bmmu_box_free_idx(
5419 dec->bmmu_box,
5420 HEADER_BUFFER_IDX(pic->index));
5421 pic->header_adr = 0;
5422#endif
5423#endif
5424#ifndef MV_USE_FIXED_BUF
5425 decoder_bmmu_box_free_idx(
5426 dec->bmmu_box,
5427 MV_BUFFER_IDX(pic->index));
5428 pic->mpred_mv_wr_start_addr = 0;
5429#endif
5430 }
5431 decoder_bmmu_box_free_idx(
5432 dec->bmmu_box,
5433 VF_BUFFER_IDX(pic->index));
5434 dec->cma_alloc_addr = 0;
5435 }
5436 }
5437 }
5438 }
5439
5440 if ((dec_status == AVS2_HEAD_PIC_I_READY)
5441 || (dec_status == AVS2_HEAD_PIC_PB_READY)) {
5442 PRINT_LINE();
5443
5444 if (debug & AVS2_DBG_SEND_PARAM_WITH_REG) {
5445 get_rpm_param(
5446 &dec->avs2_dec.param);
5447 } else {
5448
5449 for (i = 0; i < (RPM_END - RPM_BEGIN); i += 4) {
5450 int ii;
5451 for (ii = 0; ii < 4; ii++)
5452 dec->avs2_dec.param.l.data[i + ii] =
5453 dec->rpm_ptr[i + 3 - ii];
5454 }
5455 }
5456#ifdef SANITY_CHECK
5457 if (dec->avs2_dec.param.p.num_of_ref_cur >
5458 dec->avs2_dec.ref_maxbuffer) {
5459 pr_info("Warning: Wrong num_of_ref_cur %d, force to %d\n",
5460 dec->avs2_dec.param.p.num_of_ref_cur,
5461 dec->avs2_dec.ref_maxbuffer);
5462 dec->avs2_dec.param.p.num_of_ref_cur =
5463 dec->avs2_dec.ref_maxbuffer;
5464 }
5465#endif
5466 PRINT_LINE();
5467
5468 debug_buffer_mgr_more(dec);
5469 get_frame_rate(&dec->avs2_dec.param, dec);
5470
5471#if 0 // The video_signal_type is type of uint16_t and result false, so comment it out.
5472 if (dec->avs2_dec.param.p.video_signal_type
5473 & (1<<30)) {
5474 union param_u *pPara;
5475
5476 avs2_print(dec, 0,
5477 "avs2 HDR meta data present\n");
5478 pPara = &dec->avs2_dec.param;
5479
5480 /*clean this flag*/
5481 pPara->p.video_signal_type
5482 &= ~(1<<30);
5483
5484 dec->vf_dp.present_flag = 1;
5485
5486 dec->vf_dp.white_point[0]
5487 = pPara->p.white_point_x;
5488 avs2_print(dec, AVS2_DBG_HDR_INFO,
5489 "white_point[0]:0x%x\n",
5490 dec->vf_dp.white_point[0]);
5491
5492 dec->vf_dp.white_point[1]
5493 = pPara->p.white_point_y;
5494 avs2_print(dec, AVS2_DBG_HDR_INFO,
5495 "white_point[1]:0x%x\n",
5496 dec->vf_dp.white_point[1]);
5497
5498 for (i = 0; i < 3; i++) {
5499 dec->vf_dp.primaries[i][0]
5500 = pPara->p.display_primaries_x[i];
5501 avs2_print(dec, AVS2_DBG_HDR_INFO,
5502 "primaries[%d][0]:0x%x\n",
5503 i,
5504 dec->vf_dp.primaries[i][0]);
5505 }
5506
5507 for (i = 0; i < 3; i++) {
5508 dec->vf_dp.primaries[i][1]
5509 = pPara->p.display_primaries_y[i];
5510 avs2_print(dec, AVS2_DBG_HDR_INFO,
5511 "primaries[%d][1]:0x%x\n",
5512 i,
5513 dec->vf_dp.primaries[i][1]);
5514 }
5515
5516 dec->vf_dp.luminance[0]
5517 = pPara->p.max_display_mastering_luminance;
5518 avs2_print(dec, AVS2_DBG_HDR_INFO,
5519 "luminance[0]:0x%x\n",
5520 dec->vf_dp.luminance[0]);
5521
5522 dec->vf_dp.luminance[1]
5523 = pPara->p.min_display_mastering_luminance;
5524 avs2_print(dec, AVS2_DBG_HDR_INFO,
5525 "luminance[1]:0x%x\n",
5526 dec->vf_dp.luminance[1]);
5527
5528
5529 dec->vf_dp.content_light_level.present_flag
5530 = 1;
5531 dec->vf_dp.content_light_level.max_content
5532 = pPara->p.max_content_light_level;
5533 avs2_print(dec, AVS2_DBG_HDR_INFO,
5534 "max_content:0x%x\n",
5535 dec->vf_dp.content_light_level.max_content);
5536
5537 dec->vf_dp.content_light_level.max_pic_average
5538 = pPara->p.max_picture_average_light_level;
5539
5540 avs2_print(dec, AVS2_DBG_HDR_INFO,
5541 "max_pic_average:0x%x\n",
5542 dec->vf_dp.content_light_level.max_pic_average);
5543 }
5544#endif
5545
5546
5547 if (dec->video_ori_signal_type !=
5548 ((dec->avs2_dec.param.p.video_signal_type << 16)
5549 | dec->avs2_dec.param.p.color_description)) {
5550 u32 v = dec->avs2_dec.param.p.video_signal_type;
5551 u32 c = dec->avs2_dec.param.p.color_description;
5552 u32 convert_c = c;
5553
5554 if (v & 0x2000) {
5555 avs2_print(dec, 0,
5556 "video_signal_type present:\n");
5557 avs2_print(dec, 0,
5558 " %s %s\n",
5559 video_format_names[(v >> 10) & 7],
5560 ((v >> 9) & 1) ?
5561 "full_range" : "limited");
5562 if (v & 0x100) {
5563 u32 transfer;
5564 u32 maxtrix;
5565
5566 avs2_print(dec, 0,
5567 "color_description present:\n");
5568 avs2_print(dec, 0,
5569 "color_primarie = %d\n",
5570 v & 0xff);
5571 avs2_print(dec, 0,
5572 "transfer_characteristic = %d\n",
5573 (c >> 8) & 0xff);
5574 avs2_print(dec, 0,
5575 " matrix_coefficient = %d\n",
5576 c & 0xff);
5577
5578 transfer = (c >> 8) & 0xFF;
5579 if (transfer >= 15)
5580 avs2_print(dec, 0,
5581 "unsupport transfer_characteristic\n");
5582 else if (transfer == 14)
5583 transfer = 18; /* HLG */
5584 else if (transfer == 13)
5585 transfer = 32;
5586 else if (transfer == 12)
5587 transfer = 16;
5588 else if (transfer == 11)
5589 transfer = 15;
5590
5591 maxtrix = c & 0xFF;
5592 if (maxtrix >= 10)
5593 avs2_print(dec, 0,
5594 "unsupport matrix_coefficient\n");
5595 else if (maxtrix == 9)
5596 maxtrix = 10;
5597 else if (maxtrix == 8)
5598 maxtrix = 9;
5599
5600 convert_c = (transfer << 8) | (maxtrix);
5601
5602 avs2_print(dec, 0,
5603 " convered c:0x%x\n",
5604 convert_c);
5605 }
5606 }
5607
5608 if (enable_force_video_signal_type)
5609 dec->video_signal_type
5610 = force_video_signal_type;
5611 else {
5612 dec->video_signal_type
5613 = (v << 16) | convert_c;
5614
5615 dec->video_ori_signal_type
5616 = (v << 16) | c;
5617 }
5618
5619 video_signal_type = dec->video_signal_type;
5620 }
5621 }
5622#if 0
5623 if ((debug_again & 0x4) &&
5624 dec->process_state ==
5625 PROC_STATE_INIT) {
5626 if (start_code == PB_PICTURE_START_CODE) {
5627 dec->process_state = PROC_STATE_TEST1;
5628 dec_again_process(dec);
5629 goto irq_handled_exit;
5630 }
5631 }
5632#endif
5633 PRINT_LINE();
5634 avs2_prepare_header(&dec->avs2_dec, start_code);
5635
5636 if (start_code == SEQUENCE_HEADER_CODE ||
5637 start_code == VIDEO_EDIT_CODE ||
5638 start_code == SEQUENCE_END_CODE) {
5639 if (dec->m_ins_flag &&
5640 vdec_frame_based(hw_to_vdec(dec)))
5641 dec->start_decoding_flag |= 0x1;
5642 dec->process_state = PROC_STATE_HEAD_DONE;
5643 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_ACTION_DONE);
5644 } else if (start_code == I_PICTURE_START_CODE ||
5645 start_code == PB_PICTURE_START_CODE) {
5646 ret = 0;
5647 if (dec->pic_list_init_flag == 0) {
5648 int32_t lcu_size_log2 =
5649 log2i(dec->avs2_dec.param.p.lcu_size);
5650
5651 avs2_init_global_buffers(&dec->avs2_dec);
5652 /*avs2_dec->m_bg->index is
5653 set to dec->used_buf_num - 1*/
5654 init_pic_list(dec, lcu_size_log2);
5655 init_pic_list_hw(dec);
5656 }
5657 ret = avs2_process_header(&dec->avs2_dec);
5658 if (!dec->m_ins_flag)
5659 dec->slice_idx++;
5660
5661 PRINT_LINE();
5662#ifdef I_ONLY_SUPPORT
5663 if ((start_code == PB_PICTURE_START_CODE) &&
5664 (dec->i_only & 0x2))
5665 ret = -2;
5666#endif
5667#ifdef AVS2_10B_MMU
5668 if (ret >= 0) {
5669 ret = avs2_alloc_mmu(dec,
5670 dec->avs2_dec.hc.cur_pic->index,
5671 dec->avs2_dec.img.width,
5672 dec->avs2_dec.img.height,
5673 dec->avs2_dec.input.sample_bit_depth,
5674 dec->frame_mmu_map_addr);
5675 if (ret >= 0) {
5676 dec->cur_fb_idx_mmu =
5677 dec->avs2_dec.hc.cur_pic->index;
5678 dec->avs2_dec.hc.cur_pic->mmu_alloc_flag = 1;
5679 } else
5680 pr_err("can't alloc need mmu1,idx %d ret =%d\n",
5681 dec->avs2_dec.hc.cur_pic->index,
5682 ret);
5683 }
5684#endif
5685
5686#ifndef MV_USE_FIXED_BUF
5687 if (ret >= 0 &&
5688 dec->avs2_dec.hc.cur_pic->
5689 mpred_mv_wr_start_addr == 0) {
5690 unsigned long buf_addr;
5691 unsigned mv_buf_size = 0x120000;
5692 int i = dec->avs2_dec.hc.cur_pic->index;
5693 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
5694 mv_buf_size = 0x120000 * 4;
5695 if (decoder_bmmu_box_alloc_buf_phy
5696 (dec->bmmu_box,
5697 MV_BUFFER_IDX(i),
5698 mv_buf_size,
5699 DRIVER_NAME,
5700 &buf_addr) < 0)
5701 ret = -1;
5702 else
5703 dec->avs2_dec.hc.cur_pic->
5704 mpred_mv_wr_start_addr
5705 = buf_addr;
5706 }
5707#endif
5708 if (ret < 0) {
5709 avs2_print(dec, AVS2_DBG_BUFMGR,
5710 "avs2_bufmgr_process=> %d, AVS2_10B_DISCARD_NAL\r\n",
5711 ret);
5712 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_10B_DISCARD_NAL);
5713 #ifdef AVS2_10B_MMU
5714 avs2_recycle_mmu_buf(dec);
5715 #endif
5716 if (dec->m_ins_flag) {
5717 dec->dec_result = DEC_RESULT_DONE;
5718 amhevc_stop();
5719 vdec_schedule_work(&dec->work);
5720 }
5721
5722 goto irq_handled_exit;
5723 } else {
5724 PRINT_LINE();
5725 dec->avs2_dec.hc.cur_pic->stream_offset =
5726 READ_VREG(HEVC_SHIFT_BYTE_COUNT);
5727 /*
5728 struct PIC_BUFFER_CONFIG_s *cur_pic
5729 = &cm->cur_frame->buf;
5730 cur_pic->decode_idx = dec->frame_count;
5731 */
5732 if (!dec->m_ins_flag) {
5733 dec->frame_count++;
5734 decode_frame_count[dec->index]
5735 = dec->frame_count;
5736 }
5737 /*MULTI_INSTANCE_SUPPORT*/
5738 if (dec->chunk) {
5739 dec->avs2_dec.hc.cur_pic->pts =
5740 dec->chunk->pts;
5741 dec->avs2_dec.hc.cur_pic->pts64 =
5742 dec->chunk->pts64;
5743 }
5744 /**/
5745 dec->avs2_dec.hc.cur_pic->bit_depth
5746 = dec->avs2_dec.input.sample_bit_depth;
5747 dec->avs2_dec.hc.cur_pic->double_write_mode
5748 = get_double_write_mode(dec);
5749decode_slice:
5750 PRINT_LINE();
5751
5752 config_mc_buffer(dec);
5753 config_mcrcc_axi_hw(dec);
5754 config_mpred_hw(dec);
5755 config_dblk_hw(dec);
5756 config_sao_hw(dec);
5757 config_alf_hw(dec);
5758 config_other_hw(dec);
5759
5760 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
5761 "=>fref0 imgtr_fwRefDistance %d, fref1 imgtr_fwRefDistance %d, dis2/dis3/dis4 %d %d %d img->tr %d\n",
5762 dec->avs2_dec.fref[0]->imgtr_fwRefDistance,
5763 dec->avs2_dec.fref[1]->imgtr_fwRefDistance,
5764 dec->avs2_dec.fref[2]->imgtr_fwRefDistance,
5765 dec->avs2_dec.fref[3]->imgtr_fwRefDistance,
5766 dec->avs2_dec.fref[4]->imgtr_fwRefDistance,
5767 dec->avs2_dec.img.tr);
5768
5769 if ((debug_again & 0x2) &&
5770 dec->process_state ==
5771 PROC_STATE_INIT) {
5772 dec->process_state = PROC_STATE_DECODING;
5773 dec_again_process(dec);
5774 goto irq_handled_exit;
5775 }
5776
5777 dec->process_state = PROC_STATE_DECODING;
5778
5779 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_ACTION_DONE);
5780
5781 }
5782
5783 if (dec->m_ins_flag)
5784 start_process_time(dec);
5785 }
5786irq_handled_exit:
5787 PRINT_LINE();
5788 dec->process_busy = 0;
5789 return IRQ_HANDLED;
5790}
5791
5792static irqreturn_t vavs2_isr(int irq, void *data)
5793{
5794 int i;
5795 unsigned int dec_status;
5796 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)data;
5797 uint debug_tag;
5798
5799 WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
5800
5801 dec_status = READ_VREG(HEVC_DEC_STATUS_REG);
5802
5803 if (!dec)
5804 return IRQ_HANDLED;
5805 if (dec->init_flag == 0)
5806 return IRQ_HANDLED;
5807 if (dec->process_busy)/*on process.*/
5808 return IRQ_HANDLED;
5809 dec->dec_status = dec_status;
5810 dec->process_busy = 1;
5811 if (debug & AVS2_DBG_IRQ_EVENT)
5812 avs2_print(dec, 0,
5813 "avs2 isr dec status = 0x%x, lcu 0x%x shiftbyte 0x%x (%x %x lev %x, wr %x, rd %x)\n",
5814 dec_status, READ_VREG(HEVC_PARSER_LCU_START),
5815 READ_VREG(HEVC_SHIFT_BYTE_COUNT),
5816 READ_VREG(HEVC_STREAM_START_ADDR),
5817 READ_VREG(HEVC_STREAM_END_ADDR),
5818 READ_VREG(HEVC_STREAM_LEVEL),
5819 READ_VREG(HEVC_STREAM_WR_PTR),
5820 READ_VREG(HEVC_STREAM_RD_PTR)
5821 );
5822
5823 debug_tag = READ_HREG(DEBUG_REG1);
5824 if (debug_tag & 0x10000) {
5825 dma_sync_single_for_cpu(
5826 amports_get_dma_device(),
5827 dec->lmem_phy_addr,
5828 LMEM_BUF_SIZE,
5829 DMA_FROM_DEVICE);
5830
5831 pr_info("LMEM<tag %x>:\n", READ_HREG(DEBUG_REG1));
5832 for (i = 0; i < 0x400; i += 4) {
5833 int ii;
5834 if ((i & 0xf) == 0)
5835 pr_info("%03x: ", i);
5836 for (ii = 0; ii < 4; ii++) {
5837 pr_info("%04x ",
5838 dec->lmem_ptr[i + 3 - ii]);
5839 }
5840 if (((i + ii) & 0xf) == 0)
5841 pr_info("\n");
5842 }
5843
5844 if (((udebug_pause_pos & 0xffff)
5845 == (debug_tag & 0xffff)) &&
5846 (udebug_pause_decode_idx == 0 ||
5847 udebug_pause_decode_idx == dec->decode_idx) &&
5848 (udebug_pause_val == 0 ||
5849 udebug_pause_val == READ_HREG(DEBUG_REG2))) {
5850 udebug_pause_pos &= 0xffff;
5851 dec->ucode_pause_pos = udebug_pause_pos;
5852 } else if (debug_tag & 0x20000)
5853 dec->ucode_pause_pos = 0xffffffff;
5854 if (dec->ucode_pause_pos)
5855 reset_process_time(dec);
5856 else
5857 WRITE_HREG(DEBUG_REG1, 0);
5858 } else if (debug_tag != 0) {
5859 pr_info(
5860 "dbg%x: %x lcu %x\n", READ_HREG(DEBUG_REG1),
5861 READ_HREG(DEBUG_REG2),
5862 READ_VREG(HEVC_PARSER_LCU_START));
5863 if (((udebug_pause_pos & 0xffff)
5864 == (debug_tag & 0xffff)) &&
5865 (udebug_pause_decode_idx == 0 ||
5866 udebug_pause_decode_idx == dec->decode_idx) &&
5867 (udebug_pause_val == 0 ||
5868 udebug_pause_val == READ_HREG(DEBUG_REG2))) {
5869 udebug_pause_pos &= 0xffff;
5870 dec->ucode_pause_pos = udebug_pause_pos;
5871 }
5872 if (dec->ucode_pause_pos)
5873 reset_process_time(dec);
5874 else
5875 WRITE_HREG(DEBUG_REG1, 0);
5876 dec->process_busy = 0;
5877 return IRQ_HANDLED;
5878 }
5879
5880 if (!dec->m_ins_flag) {
5881 if (dec->error_flag == 1) {
5882 dec->error_flag = 2;
5883 dec->process_busy = 0;
5884 return IRQ_HANDLED;
5885 } else if (dec->error_flag == 3) {
5886 dec->process_busy = 0;
5887 return IRQ_HANDLED;
5888 }
5889
5890 if ((dec->pic_list_init_flag) &&
5891 get_free_buf_count(dec) <= 0) {
5892 /*
5893 if (dec->wait_buf == 0)
5894 pr_info("set wait_buf to 1\r\n");
5895 */
5896 dec->wait_buf = 1;
5897 dec->process_busy = 0;
5898 if (debug & AVS2_DBG_IRQ_EVENT)
5899 avs2_print(dec, 0, "wait_buf\n");
5900 return IRQ_HANDLED;
5901 } else if (force_disp_pic_index) {
5902 dec->process_busy = 0;
5903 return IRQ_HANDLED;
5904 }
5905 }
5906 return IRQ_WAKE_THREAD;
5907}
5908
5909static void vavs2_put_timer_func(unsigned long arg)
5910{
5911 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)arg;
5912 struct timer_list *timer = &dec->timer;
5913 uint8_t empty_flag;
5914 unsigned int buf_level;
5915
5916 enum receviver_start_e state = RECEIVER_INACTIVE;
5917 if (dec->m_ins_flag) {
5918 if (hw_to_vdec(dec)->next_status
5919 == VDEC_STATUS_DISCONNECTED) {
5920 dec->dec_result = DEC_RESULT_FORCE_EXIT;
5921 vdec_schedule_work(&dec->work);
5922 avs2_print(dec, AVS2_DBG_BUFMGR,
5923 "vdec requested to be disconnected\n");
5924 return;
5925 }
5926 }
5927 if (dec->init_flag == 0) {
5928 if (dec->stat & STAT_TIMER_ARM) {
5929 timer->expires = jiffies + PUT_INTERVAL;
5930 add_timer(&dec->timer);
5931 }
5932 return;
5933 }
5934 if (dec->m_ins_flag == 0) {
5935 if (vf_get_receiver(dec->provider_name)) {
5936 state =
5937 vf_notify_receiver(dec->provider_name,
5938 VFRAME_EVENT_PROVIDER_QUREY_STATE,
5939 NULL);
5940 if ((state == RECEIVER_STATE_NULL)
5941 || (state == RECEIVER_STATE_NONE))
5942 state = RECEIVER_INACTIVE;
5943 } else
5944 state = RECEIVER_INACTIVE;
5945
5946 empty_flag = (READ_VREG(HEVC_PARSER_INT_STATUS) >> 6) & 0x1;
5947 /* error watchdog */
5948 if (empty_flag == 0) {
5949 /* decoder has input */
5950 if ((debug & AVS2_DBG_DIS_LOC_ERROR_PROC) == 0) {
5951
5952 buf_level = READ_VREG(HEVC_STREAM_LEVEL);
5953 /* receiver has no buffer to recycle */
5954 if ((state == RECEIVER_INACTIVE) &&
5955 (kfifo_is_empty(&dec->display_q) &&
5956 buf_level > 0x200)
5957 ) {
5958 WRITE_VREG
5959 (HEVC_ASSIST_MBOX0_IRQ_REG,
5960 0x1);
5961 }
5962 }
5963
5964 if ((debug & AVS2_DBG_DIS_SYS_ERROR_PROC) == 0) {
5965 /* receiver has no buffer to recycle */
5966 /*if ((state == RECEIVER_INACTIVE) &&
5967 (kfifo_is_empty(&dec->display_q))) {
5968 pr_info("avs2 something error,need reset\n");
5969 }*/
5970 }
5971 }
5972 } else {
5973 if (
5974 (decode_timeout_val > 0) &&
5975 (dec->start_process_time > 0) &&
5976 ((1000 * (jiffies - dec->start_process_time) / HZ)
5977 > decode_timeout_val)
5978 ) {
5979 int current_lcu_idx =
5980 READ_VREG(HEVC_PARSER_LCU_START)
5981 & 0xffffff;
5982 if (dec->last_lcu_idx == current_lcu_idx) {
5983 if (dec->decode_timeout_count > 0)
5984 dec->decode_timeout_count--;
5985 if (dec->decode_timeout_count == 0) {
5986 if (input_frame_based(
5987 hw_to_vdec(dec)) ||
5988 (READ_VREG(HEVC_STREAM_LEVEL) > 0x200))
5989 timeout_process(dec);
5990 else {
5991 avs2_print(dec, 0,
5992 "timeout & empty, again\n");
5993 dec_again_process(dec);
5994 }
5995 }
5996 } else {
5997 start_process_time(dec);
5998 dec->last_lcu_idx = current_lcu_idx;
5999 }
6000 }
6001 }
6002
6003 if ((dec->ucode_pause_pos != 0) &&
6004 (dec->ucode_pause_pos != 0xffffffff) &&
6005 udebug_pause_pos != dec->ucode_pause_pos) {
6006 dec->ucode_pause_pos = 0;
6007 WRITE_HREG(DEBUG_REG1, 0);
6008 }
6009 if (debug & AVS2_DBG_DUMP_DATA) {
6010 debug &= ~AVS2_DBG_DUMP_DATA;
6011 avs2_print(dec, 0,
6012 "%s: chunk size 0x%x off 0x%x sum 0x%x\n",
6013 __func__,
6014 dec->chunk->size,
6015 dec->chunk->offset,
6016 get_data_check_sum(dec, dec->chunk->size)
6017 );
6018 dump_data(dec, dec->chunk->size);
6019 }
6020 if (debug & AVS2_DBG_DUMP_PIC_LIST) {
6021 dump_pic_list(dec);
6022 debug &= ~AVS2_DBG_DUMP_PIC_LIST;
6023 }
6024 if (debug & AVS2_DBG_TRIG_SLICE_SEGMENT_PROC) {
6025 WRITE_VREG(HEVC_ASSIST_MBOX0_IRQ_REG, 0x1);
6026 debug &= ~AVS2_DBG_TRIG_SLICE_SEGMENT_PROC;
6027 }
6028 if (debug & AVS2_DBG_DUMP_RPM_BUF) {
6029 int i;
6030 pr_info("RPM:\n");
6031 for (i = 0; i < RPM_BUF_SIZE; i += 4) {
6032 int ii;
6033 if ((i & 0xf) == 0)
6034 pr_info("%03x: ", i);
6035 for (ii = 0; ii < 4; ii++) {
6036 pr_info("%04x ",
6037 dec->lmem_ptr[i + 3 - ii]);
6038 }
6039 if (((i + ii) & 0xf) == 0)
6040 pr_info("\n");
6041 }
6042 debug &= ~AVS2_DBG_DUMP_RPM_BUF;
6043 }
6044 if (debug & AVS2_DBG_DUMP_LMEM_BUF) {
6045 int i;
6046 pr_info("LMEM:\n");
6047 for (i = 0; i < LMEM_BUF_SIZE; i += 4) {
6048 int ii;
6049 if ((i & 0xf) == 0)
6050 pr_info("%03x: ", i);
6051 for (ii = 0; ii < 4; ii++) {
6052 pr_info("%04x ",
6053 dec->lmem_ptr[i + 3 - ii]);
6054 }
6055 if (((i + ii) & 0xf) == 0)
6056 pr_info("\n");
6057 }
6058 debug &= ~AVS2_DBG_DUMP_LMEM_BUF;
6059 }
6060 /*if (debug & AVS2_DBG_HW_RESET) {
6061 }*/
6062
6063 if (radr != 0) {
6064 if (rval != 0) {
6065 WRITE_VREG(radr, rval);
6066 pr_info("WRITE_VREG(%x,%x)\n", radr, rval);
6067 } else
6068 pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr));
6069 rval = 0;
6070 radr = 0;
6071 }
6072 if (pop_shorts != 0) {
6073 int i;
6074 u32 sum = 0;
6075 pr_info("pop stream 0x%x shorts\r\n", pop_shorts);
6076 for (i = 0; i < pop_shorts; i++) {
6077 u32 data =
6078 (READ_HREG(HEVC_SHIFTED_DATA) >> 16);
6079 WRITE_HREG(HEVC_SHIFT_COMMAND,
6080 (1<<7)|16);
6081 if ((i & 0xf) == 0)
6082 pr_info("%04x:", i);
6083 pr_info("%04x ", data);
6084 if (((i + 1) & 0xf) == 0)
6085 pr_info("\r\n");
6086 sum += data;
6087 }
6088 pr_info("\r\nsum = %x\r\n", sum);
6089 pop_shorts = 0;
6090 }
6091 if (dbg_cmd != 0) {
6092 if (dbg_cmd == 1) {
6093 u32 disp_laddr;
6094 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_GXBB &&
6095 get_double_write_mode(dec) == 0) {
6096 disp_laddr =
6097 READ_VCBUS_REG(AFBC_BODY_BADDR) << 4;
6098 } else {
6099 struct canvas_s cur_canvas;
6100 canvas_read((READ_VCBUS_REG(VD1_IF0_CANVAS0)
6101 & 0xff), &cur_canvas);
6102 disp_laddr = cur_canvas.addr;
6103 }
6104 pr_info("current displayed buffer address %x\r\n",
6105 disp_laddr);
6106 }
6107 dbg_cmd = 0;
6108 }
6109 /*don't changed at start.*/
6110 if (dec->get_frame_dur && dec->show_frame_num > 60 &&
6111 dec->frame_dur > 0 && dec->saved_resolution !=
6112 frame_width * frame_height *
6113 (96000 / dec->frame_dur)) {
6114 int fps = 96000 / dec->frame_dur;
6115 if (hevc_source_changed(VFORMAT_AVS2,
6116 frame_width, frame_height, fps) > 0)
6117 dec->saved_resolution = frame_width *
6118 frame_height * fps;
6119 }
6120
6121 timer->expires = jiffies + PUT_INTERVAL;
6122 add_timer(timer);
6123}
6124
6125
6126int vavs2_dec_status(struct vdec_s *vdec, struct vdec_info *vstatus)
6127{
6128 struct AVS2Decoder_s *dec =
6129 (struct AVS2Decoder_s *)vdec->private;
6130
6131 if (!dec)
6132 return -1;
6133
6134 vstatus->frame_width = dec->frame_width;
6135 vstatus->frame_height = dec->frame_height;
6136
6137 if (dec->frame_dur != 0)
6138 vstatus->frame_rate = 96000 / dec->frame_dur;
6139 else
6140 vstatus->frame_rate = -1;
6141 vstatus->error_count = 0;
6142 vstatus->status = dec->stat | dec->fatal_error;
6143 vstatus->frame_dur = dec->frame_dur;
6144 vstatus->bit_rate = dec->gvs->bit_rate;
6145 vstatus->frame_data = dec->gvs->frame_data;
6146 vstatus->total_data = dec->gvs->total_data;
6147 vstatus->frame_count = dec->gvs->frame_count;
6148 vstatus->error_frame_count = dec->gvs->error_frame_count;
6149 vstatus->drop_frame_count = dec->gvs->drop_frame_count;
6150 vstatus->total_data = dec->gvs->total_data;
6151 vstatus->samp_cnt = dec->gvs->samp_cnt;
6152 vstatus->offset = dec->gvs->offset;
6153 snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name),
6154 "%s", DRIVER_NAME);
6155 return 0;
6156}
6157
6158int vavs2_set_isreset(struct vdec_s *vdec, int isreset)
6159{
6160 is_reset = isreset;
6161 return 0;
6162}
6163
6164static void vavs2_prot_init(struct AVS2Decoder_s *dec)
6165{
6166 unsigned int data32;
6167
6168 avs2_config_work_space_hw(dec);
6169 if (dec->pic_list_init_flag)
6170 init_pic_list_hw(dec);
6171
6172 avs2_init_decoder_hw(dec);
6173
6174#if 1
6175 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
6176 "%s\n", __func__);
6177 data32 = READ_VREG(HEVC_STREAM_CONTROL);
6178 data32 = data32 |
6179 (1 << 0)/*stream_fetch_enable*/
6180 ;
6181 WRITE_VREG(HEVC_STREAM_CONTROL, data32);
6182#if 0
6183 data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
6184 if (data32 != 0x00000100) {
6185 pr_info("avs2 prot init error %d\n", __LINE__);
6186 return;
6187 }
6188 data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
6189 if (data32 != 0x00000300) {
6190 pr_info("avs2 prot init error %d\n", __LINE__);
6191 return;
6192 }
6193 WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x12345678);
6194 WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x9abcdef0);
6195 data32 = READ_VREG(HEVC_SHIFT_STARTCODE);
6196 if (data32 != 0x12345678) {
6197 pr_info("avs2 prot init error %d\n", __LINE__);
6198 return;
6199 }
6200 data32 = READ_VREG(HEVC_SHIFT_EMULATECODE);
6201 if (data32 != 0x9abcdef0) {
6202 pr_info("avs2 prot init error %d\n", __LINE__);
6203 return;
6204 }
6205#endif
6206 WRITE_VREG(HEVC_SHIFT_STARTCODE, 0x00000100);
6207 WRITE_VREG(HEVC_SHIFT_EMULATECODE, 0x00000000);
6208#endif
6209
6210
6211
6212 WRITE_VREG(HEVC_WAIT_FLAG, 1);
6213
6214 /* WRITE_VREG(HEVC_MPSR, 1); */
6215
6216 /* clear mailbox interrupt */
6217 WRITE_VREG(HEVC_ASSIST_MBOX0_CLR_REG, 1);
6218
6219 /* enable mailbox interrupt */
6220 WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 1);
6221
6222 /* disable PSCALE for hardware sharing */
6223 WRITE_VREG(HEVC_PSCALE_CTRL, 0);
6224
6225 WRITE_VREG(DEBUG_REG1, 0x0);
6226 /*check vps/sps/pps/i-slice in ucode*/
6227 WRITE_VREG(NAL_SEARCH_CTL, 0x8);
6228
6229 WRITE_VREG(DECODE_STOP_POS, udebug_flag);
6230
6231}
6232
6233#ifdef I_ONLY_SUPPORT
6234static int vavs2_set_trickmode(struct vdec_s *vdec, unsigned long trickmode)
6235{
6236 struct AVS2Decoder_s *dec =
6237 (struct AVS2Decoder_s *)vdec->private;
6238 if (i_only_flag & 0x100)
6239 return 0;
6240 if (trickmode == TRICKMODE_I || trickmode == TRICKMODE_I_HEVC)
6241 dec->i_only = 0x3;
6242 else if (trickmode == TRICKMODE_NONE)
6243 dec->i_only = 0x0;
6244 return 0;
6245}
6246#endif
6247
6248static int vavs2_local_init(struct AVS2Decoder_s *dec)
6249{
6250 int i;
6251 int ret;
6252 int width, height;
6253
6254 dec->gvs = vzalloc(sizeof(struct vdec_info));
6255 if (NULL == dec->gvs) {
6256 avs2_print(dec, 0,
6257 "the struct of vdec status malloc failed.\n");
6258 return -1;
6259 }
6260#ifdef DEBUG_PTS
6261 dec->pts_missed = 0;
6262 dec->pts_hit = 0;
6263#endif
6264 dec->new_frame_displayed = 0;
6265 dec->last_put_idx = -1;
6266 dec->saved_resolution = 0;
6267 dec->get_frame_dur = false;
6268 on_no_keyframe_skiped = 0;
6269 width = dec->vavs2_amstream_dec_info.width;
6270 height = dec->vavs2_amstream_dec_info.height;
6271 dec->frame_dur =
6272 (dec->vavs2_amstream_dec_info.rate ==
6273 0) ? 3600 : dec->vavs2_amstream_dec_info.rate;
6274 if (width && height)
6275 dec->frame_ar = height * 0x100 / width;
6276/*
6277TODO:FOR VERSION
6278*/
6279 avs2_print(dec, AVS2_DBG_BUFMGR,
6280 "avs2: ver (%d,%d) decinfo: %dx%d rate=%d\n", avs2_version,
6281 0, width, height, dec->frame_dur);
6282
6283 if (dec->frame_dur == 0)
6284 dec->frame_dur = 96000 / 24;
6285#ifdef I_ONLY_SUPPORT
6286 if (i_only_flag & 0x100)
6287 dec->i_only = i_only_flag & 0xff;
6288 else if ((unsigned long) dec->vavs2_amstream_dec_info.param
6289 & 0x08)
6290 dec->i_only = 0x7;
6291 else
6292 dec->i_only = 0x0;
6293#endif
6294 INIT_KFIFO(dec->display_q);
6295 INIT_KFIFO(dec->newframe_q);
6296
6297
6298 for (i = 0; i < VF_POOL_SIZE; i++) {
6299 const struct vframe_s *vf = &dec->vfpool[i];
6300 dec->vfpool[i].index = -1;
6301 kfifo_put(&dec->newframe_q, vf);
6302 }
6303
6304
6305 ret = avs2_local_init(dec);
6306
6307 return ret;
6308}
6309
6310
6311static s32 vavs2_init(struct vdec_s *vdec)
6312{
6313 int ret = -1, size = -1;
6314 int fw_size = 0x1000 * 16;
6315 struct firmware_s *fw = NULL;
6316 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)vdec->private;
6317 init_timer(&dec->timer);
6318
6319 dec->stat |= STAT_TIMER_INIT;
6320 if (vavs2_local_init(dec) < 0)
6321 return -EBUSY;
6322
6323 vdec_set_vframe_comm(vdec, DRIVER_NAME);
6324
6325 fw = vmalloc(sizeof(struct firmware_s) + fw_size);
6326 if (IS_ERR_OR_NULL(fw))
6327 return -ENOMEM;
6328
6329 size = get_firmware_data(VIDEO_DEC_AVS2_MMU, fw->data);
6330 if (size < 0) {
6331 pr_err("get firmware fail.\n");
6332 vfree(fw);
6333 return -1;
6334 }
6335
6336 fw->len = fw_size;
6337
6338 if (dec->m_ins_flag) {
6339 dec->timer.data = (ulong) dec;
6340 dec->timer.function = vavs2_put_timer_func;
6341 dec->timer.expires = jiffies + PUT_INTERVAL;
6342
6343 /*add_timer(&dec->timer);
6344
6345 dec->stat |= STAT_TIMER_ARM;
6346 dec->stat |= STAT_ISR_REG;*/
6347
6348 INIT_WORK(&dec->work, avs2_work);
6349 dec->fw = fw;
6350
6351 return 0;
6352 }
6353 amhevc_enable();
6354 ret = amhevc_loadmc_ex(VFORMAT_AVS2, NULL, fw->data);
6355 if (ret < 0) {
6356 amhevc_disable();
6357 vfree(fw);
6358 pr_err("AVS2: the %s fw loading failed, err: %x\n",
6359 tee_enabled() ? "TEE" : "local", ret);
6360 return -EBUSY;
6361 }
6362
6363 vfree(fw);
6364
6365 dec->stat |= STAT_MC_LOAD;
6366
6367 /* enable AMRISC side protocol */
6368 vavs2_prot_init(dec);
6369
6370 if (vdec_request_threaded_irq(VDEC_IRQ_0,
6371 vavs2_isr,
6372 vavs2_isr_thread_fn,
6373 IRQF_ONESHOT,/*run thread on this irq disabled*/
6374 "vavs2-irq", (void *)dec)) {
6375 pr_info("vavs2 irq register error.\n");
6376 amhevc_disable();
6377 return -ENOENT;
6378 }
6379
6380 dec->stat |= STAT_ISR_REG;
6381
6382 dec->provider_name = PROVIDER_NAME;
6383 vf_provider_init(&vavs2_vf_prov, PROVIDER_NAME,
6384 &vavs2_vf_provider, dec);
6385 vf_reg_provider(&vavs2_vf_prov);
6386 vf_notify_receiver(PROVIDER_NAME, VFRAME_EVENT_PROVIDER_START, NULL);
6387 if (dec->frame_dur != 0) {
6388 if (!is_reset)
6389 vf_notify_receiver(dec->provider_name,
6390 VFRAME_EVENT_PROVIDER_FR_HINT,
6391 (void *)
6392 ((unsigned long)dec->frame_dur));
6393 }
6394 dec->stat |= STAT_VF_HOOK;
6395
6396 dec->timer.data = (ulong)dec;
6397 dec->timer.function = vavs2_put_timer_func;
6398 dec->timer.expires = jiffies + PUT_INTERVAL;
6399
6400
6401 add_timer(&dec->timer);
6402
6403 dec->stat |= STAT_TIMER_ARM;
6404
6405 /* dec->stat |= STAT_KTHREAD; */
6406 dec->process_busy = 0;
6407 avs2_print(dec, AVS2_DBG_BUFMGR_MORE,
6408 "%d, vavs2_init, RP=0x%x\n",
6409 __LINE__, READ_VREG(HEVC_STREAM_RD_PTR));
6410 return 0;
6411}
6412
6413static int vmavs2_stop(struct AVS2Decoder_s *dec)
6414{
6415 dec->init_flag = 0;
6416 dec->first_sc_checked = 0;
6417 if (dec->stat & STAT_TIMER_ARM) {
6418 del_timer_sync(&dec->timer);
6419 dec->stat &= ~STAT_TIMER_ARM;
6420 }
6421
6422 if (dec->stat & STAT_VF_HOOK) {
6423 if (!is_reset)
6424 vf_notify_receiver(dec->provider_name,
6425 VFRAME_EVENT_PROVIDER_FR_END_HINT,
6426 NULL);
6427
6428 vf_unreg_provider(&vavs2_vf_prov);
6429 dec->stat &= ~STAT_VF_HOOK;
6430 }
6431 avs2_local_uninit(dec);
6432 reset_process_time(dec);
6433 cancel_work_sync(&dec->work);
6434 uninit_mmu_buffers(dec);
6435 if (dec->fw) {
6436 vfree(dec->fw);
6437 dec->fw = NULL;
6438 }
6439
6440 return 0;
6441}
6442
6443
6444static int vavs2_stop(struct AVS2Decoder_s *dec)
6445{
6446
6447 dec->init_flag = 0;
6448 dec->first_sc_checked = 0;
6449 if (dec->stat & STAT_VDEC_RUN) {
6450 amhevc_stop();
6451 dec->stat &= ~STAT_VDEC_RUN;
6452 }
6453
6454 if (dec->stat & STAT_ISR_REG) {
6455 if (!dec->m_ins_flag)
6456 WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
6457 vdec_free_irq(VDEC_IRQ_0, (void *)dec);
6458 dec->stat &= ~STAT_ISR_REG;
6459 }
6460
6461 if (dec->stat & STAT_TIMER_ARM) {
6462 del_timer_sync(&dec->timer);
6463 dec->stat &= ~STAT_TIMER_ARM;
6464 }
6465
6466 if (dec->stat & STAT_VF_HOOK) {
6467 if (!is_reset)
6468 vf_notify_receiver(dec->provider_name,
6469 VFRAME_EVENT_PROVIDER_FR_END_HINT,
6470 NULL);
6471
6472 vf_unreg_provider(&vavs2_vf_prov);
6473 dec->stat &= ~STAT_VF_HOOK;
6474 }
6475 avs2_local_uninit(dec);
6476
6477 if (dec->m_ins_flag)
6478 cancel_work_sync(&dec->work);
6479 else
6480 amhevc_disable();
6481 uninit_mmu_buffers(dec);
6482
6483 return 0;
6484}
6485
6486static int amvdec_avs2_mmu_init(struct AVS2Decoder_s *dec)
6487{
6488 int tvp_flag = vdec_secure(hw_to_vdec(dec)) ?
6489 CODEC_MM_FLAGS_TVP : 0;
6490 int buf_size = 48;
6491
6492#ifdef AVS2_10B_MMU
6493 dec->need_cache_size = buf_size * SZ_1M;
6494 dec->sc_start_time = get_jiffies_64();
6495 dec->mmu_box = decoder_mmu_box_alloc_box(DRIVER_NAME,
6496 dec->index, FRAME_BUFFERS,
6497 dec->need_cache_size,
6498 tvp_flag
6499 );
6500 if (!dec->mmu_box) {
6501 pr_err("avs2 alloc mmu box failed!!\n");
6502 return -1;
6503 }
6504#endif
6505 dec->bmmu_box = decoder_bmmu_box_alloc_box(
6506 DRIVER_NAME,
6507 dec->index,
6508 MAX_BMMU_BUFFER_NUM,
6509 4 + PAGE_SHIFT,
6510 CODEC_MM_FLAGS_CMA_CLEAR |
6511 CODEC_MM_FLAGS_FOR_VDECODER |
6512 tvp_flag);
6513 if (!dec->bmmu_box) {
6514 pr_err("avs2 alloc bmmu box failed!!\n");
6515 return -1;
6516 }
6517 return 0;
6518}
6519
6520static int amvdec_avs2_probe(struct platform_device *pdev)
6521{
6522 struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
6523 struct BUF_s BUF[MAX_BUF_NUM];
6524 struct AVS2Decoder_s *dec = &gAVS2Decoder;
6525 int ret;
6526 pr_info("%s\n", __func__);
6527 mutex_lock(&vavs2_mutex);
6528
6529 memcpy(&BUF[0], &dec->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
6530 memset(dec, 0, sizeof(struct AVS2Decoder_s));
6531 memcpy(&dec->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
6532
6533 dec->init_flag = 0;
6534 dec->first_sc_checked = 0;
6535 dec->eos = 0;
6536 dec->start_process_time = 0;
6537 dec->timeout_num = 0;
6538 dec->fatal_error = 0;
6539 dec->show_frame_num = 0;
6540 if (pdata == NULL) {
6541 avs2_print(dec, 0,
6542 "\namvdec_avs2 memory resource undefined.\n");
6543 mutex_unlock(&vavs2_mutex);
6544 return -EFAULT;
6545 }
6546 dec->m_ins_flag = 0;
6547 dec->platform_dev = pdev;
6548 platform_set_drvdata(pdev, pdata);
6549
6550 if (amvdec_avs2_mmu_init(dec) < 0) {
6551 mutex_unlock(&vavs2_mutex);
6552 pr_err("avs2 alloc bmmu box failed!!\n");
6553 return -1;
6554 }
6555
6556 ret = decoder_bmmu_box_alloc_buf_phy(dec->bmmu_box, WORK_SPACE_BUF_ID,
6557 work_buf_size, DRIVER_NAME, &pdata->mem_start);
6558 if (ret < 0) {
6559 uninit_mmu_buffers(dec);
6560 mutex_unlock(&vavs2_mutex);
6561 return ret;
6562 }
6563 dec->buf_size = work_buf_size;
6564
6565 dec->buf_start = pdata->mem_start;
6566
6567
6568 if (debug) {
6569 avs2_print(dec, 0,
6570 "===AVS2 decoder mem resource 0x%lx size 0x%x\n",
6571 pdata->mem_start, dec->buf_size);
6572 }
6573
6574 if (pdata->sys_info) {
6575 dec->vavs2_amstream_dec_info = *pdata->sys_info;
6576 dec->frame_width = dec->vavs2_amstream_dec_info.width;
6577 dec->frame_height = dec->vavs2_amstream_dec_info.height;
6578 } else {
6579 dec->vavs2_amstream_dec_info.width = 0;
6580 dec->vavs2_amstream_dec_info.height = 0;
6581 dec->vavs2_amstream_dec_info.rate = 30;
6582 }
6583 dec->cma_dev = pdata->cma_dev;
6584
6585 pdata->private = dec;
6586 pdata->dec_status = vavs2_dec_status;
6587 /*pdata->set_isreset = vavs2_set_isreset;*/
6588 is_reset = 0;
6589 if (vavs2_init(pdata) < 0) {
6590 pr_info("\namvdec_avs2 init failed.\n");
6591 avs2_local_uninit(dec);
6592 uninit_mmu_buffers(dec);
6593 pdata->dec_status = NULL;
6594 mutex_unlock(&vavs2_mutex);
6595 return -ENODEV;
6596 }
6597 /*set the max clk for smooth playing...*/
6598 hevc_source_changed(VFORMAT_AVS2,
6599 4096, 2048, 60);
6600 mutex_unlock(&vavs2_mutex);
6601
6602 return 0;
6603}
6604
6605static int amvdec_avs2_remove(struct platform_device *pdev)
6606{
6607 struct AVS2Decoder_s *dec = &gAVS2Decoder;
6608 if (debug)
6609 pr_info("amvdec_avs2_remove\n");
6610
6611 mutex_lock(&vavs2_mutex);
6612
6613 vavs2_stop(dec);
6614
6615
6616 hevc_source_changed(VFORMAT_AVS2, 0, 0, 0);
6617
6618
6619#ifdef DEBUG_PTS
6620 pr_info("pts missed %ld, pts hit %ld, duration %d\n",
6621 dec->pts_missed, dec->pts_hit, dec->frame_dur);
6622#endif
6623
6624 mutex_unlock(&vavs2_mutex);
6625
6626 return 0;
6627}
6628
6629/****************************************/
6630#ifdef CONFIG_PM
6631static int avs2_suspend(struct device *dev)
6632{
6633 amhevc_suspend(to_platform_device(dev), dev->power.power_state);
6634 return 0;
6635}
6636
6637static int avs2_resume(struct device *dev)
6638{
6639 amhevc_resume(to_platform_device(dev));
6640 return 0;
6641}
6642
6643static const struct dev_pm_ops avs2_pm_ops = {
6644 SET_SYSTEM_SLEEP_PM_OPS(avs2_suspend, avs2_resume)
6645};
6646#endif
6647
6648static struct platform_driver amvdec_avs2_driver = {
6649 .probe = amvdec_avs2_probe,
6650 .remove = amvdec_avs2_remove,
6651 .driver = {
6652 .name = DRIVER_NAME,
6653#ifdef CONFIG_PM
6654 .pm = &avs2_pm_ops,
6655#endif
6656 }
6657};
6658
6659static struct codec_profile_t amvdec_avs2_profile = {
6660 .name = "avs2",
6661 .profile = ""
6662};
6663
6664static struct codec_profile_t amvdec_avs2_profile_mult;
6665
6666static unsigned char get_data_check_sum
6667 (struct AVS2Decoder_s *dec, int size)
6668{
6669 int jj;
6670 int sum = 0;
6671 u8 *data = NULL;
6672
6673 if (!dec->chunk->block->is_mapped)
6674 data = codec_mm_vmap(dec->chunk->block->start +
6675 dec->chunk->offset, size);
6676 else
6677 data = ((u8 *)dec->chunk->block->start_virt) +
6678 dec->chunk->offset;
6679
6680 for (jj = 0; jj < size; jj++)
6681 sum += data[jj];
6682
6683 if (!dec->chunk->block->is_mapped)
6684 codec_mm_unmap_phyaddr(data);
6685 return sum;
6686}
6687
6688static void dump_data(struct AVS2Decoder_s *dec, int size)
6689{
6690 int jj;
6691 u8 *data = NULL;
6692 int padding_size = dec->chunk->offset &
6693 (VDEC_FIFO_ALIGN - 1);
6694
6695 if (!dec->chunk->block->is_mapped)
6696 data = codec_mm_vmap(dec->chunk->block->start +
6697 dec->chunk->offset, size);
6698 else
6699 data = ((u8 *)dec->chunk->block->start_virt) +
6700 dec->chunk->offset;
6701
6702 avs2_print(dec, 0, "padding: ");
6703 for (jj = padding_size; jj > 0; jj--)
6704 avs2_print_cont(dec,
6705 0,
6706 "%02x ", *(data - jj));
6707 avs2_print_cont(dec, 0, "data adr %p\n",
6708 data);
6709
6710 for (jj = 0; jj < size; jj++) {
6711 if ((jj & 0xf) == 0)
6712 avs2_print(dec,
6713 0,
6714 "%06x:", jj);
6715 avs2_print_cont(dec,
6716 0,
6717 "%02x ", data[jj]);
6718 if (((jj + 1) & 0xf) == 0)
6719 avs2_print(dec,
6720 0,
6721 "\n");
6722 }
6723 avs2_print(dec,
6724 0,
6725 "\n");
6726
6727 if (!dec->chunk->block->is_mapped)
6728 codec_mm_unmap_phyaddr(data);
6729}
6730
6731static void avs2_work(struct work_struct *work)
6732{
6733 struct AVS2Decoder_s *dec = container_of(work,
6734 struct AVS2Decoder_s, work);
6735 struct vdec_s *vdec = hw_to_vdec(dec);
6736 /* finished decoding one frame or error,
6737 * notify vdec core to switch context
6738 */
6739 avs2_print(dec, PRINT_FLAG_VDEC_DETAIL,
6740 "%s dec_result %d %x %x %x\n",
6741 __func__,
6742 dec->dec_result,
6743 READ_VREG(HEVC_STREAM_LEVEL),
6744 READ_VREG(HEVC_STREAM_WR_PTR),
6745 READ_VREG(HEVC_STREAM_RD_PTR));
6746
6747 if (((dec->dec_result == DEC_RESULT_GET_DATA) ||
6748 (dec->dec_result == DEC_RESULT_GET_DATA_RETRY))
6749 && (hw_to_vdec(dec)->next_status !=
6750 VDEC_STATUS_DISCONNECTED)) {
6751 if (!vdec_has_more_input(vdec)) {
6752 dec->dec_result = DEC_RESULT_EOS;
6753 vdec_schedule_work(&dec->work);
6754 return;
6755 }
6756
6757 if (dec->dec_result == DEC_RESULT_GET_DATA) {
6758 avs2_print(dec, PRINT_FLAG_VDEC_STATUS,
6759 "%s DEC_RESULT_GET_DATA %x %x %x\n",
6760 __func__,
6761 READ_VREG(HEVC_STREAM_LEVEL),
6762 READ_VREG(HEVC_STREAM_WR_PTR),
6763 READ_VREG(HEVC_STREAM_RD_PTR));
6764 vdec_vframe_dirty(vdec, dec->chunk);
6765 vdec_clean_input(vdec);
6766 }
6767
6768 if (get_free_buf_count(dec) >=
6769 run_ready_min_buf_num) {
6770 int r;
6771 int decode_size;
6772 r = vdec_prepare_input(vdec, &dec->chunk);
6773 if (r < 0) {
6774 dec->dec_result = DEC_RESULT_GET_DATA_RETRY;
6775
6776 avs2_print(dec,
6777 PRINT_FLAG_VDEC_DETAIL,
6778 "amvdec_vh265: Insufficient data\n");
6779
6780 vdec_schedule_work(&dec->work);
6781 return;
6782 }
6783 dec->dec_result = DEC_RESULT_NONE;
6784 avs2_print(dec, PRINT_FLAG_VDEC_STATUS,
6785 "%s: chunk size 0x%x sum 0x%x\n",
6786 __func__, r,
6787 (debug & PRINT_FLAG_VDEC_STATUS) ?
6788 get_data_check_sum(dec, r) : 0
6789 );
6790 if (debug & PRINT_FLAG_VDEC_DATA)
6791 dump_data(dec, dec->chunk->size);
6792
6793 decode_size = dec->chunk->size +
6794 (dec->chunk->offset & (VDEC_FIFO_ALIGN - 1));
6795
6796 WRITE_VREG(HEVC_DECODE_SIZE,
6797 READ_VREG(HEVC_DECODE_SIZE) + decode_size);
6798
6799 vdec_enable_input(vdec);
6800
6801 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_ACTION_DONE);
6802
6803 start_process_time(dec);
6804
6805 } else{
6806 dec->dec_result = DEC_RESULT_GET_DATA_RETRY;
6807
6808 avs2_print(dec, PRINT_FLAG_VDEC_DETAIL,
6809 "amvdec_vh265: Insufficient data\n");
6810
6811 vdec_schedule_work(&dec->work);
6812 }
6813 return;
6814 } else if (dec->dec_result == DEC_RESULT_DONE) {
6815 /* if (!dec->ctx_valid)
6816 dec->ctx_valid = 1; */
6817 dec->slice_idx++;
6818 dec->frame_count++;
6819 dec->process_state = PROC_STATE_INIT;
6820 decode_frame_count[dec->index] = dec->frame_count;
6821
6822#ifdef AVS2_10B_MMU
6823 dec->used_4k_num =
6824 (READ_VREG(HEVC_SAO_MMU_STATUS) >> 16);
6825#endif
6826 avs2_print(dec, PRINT_FLAG_VDEC_STATUS,
6827 "%s (===> %d) dec_result %d %x %x %x shiftbytes 0x%x decbytes 0x%x\n",
6828 __func__,
6829 dec->frame_count,
6830 dec->dec_result,
6831 READ_VREG(HEVC_STREAM_LEVEL),
6832 READ_VREG(HEVC_STREAM_WR_PTR),
6833 READ_VREG(HEVC_STREAM_RD_PTR),
6834 READ_VREG(HEVC_SHIFT_BYTE_COUNT),
6835 READ_VREG(HEVC_SHIFT_BYTE_COUNT) -
6836 dec->start_shift_bytes
6837 );
6838 vdec_vframe_dirty(hw_to_vdec(dec), dec->chunk);
6839 } else if (dec->dec_result == DEC_RESULT_AGAIN) {
6840 /*
6841 stream base: stream buf empty or timeout
6842 frame base: vdec_prepare_input fail
6843 */
6844 if (!vdec_has_more_input(vdec)) {
6845 dec->dec_result = DEC_RESULT_EOS;
6846 vdec_schedule_work(&dec->work);
6847 return;
6848 }
6849 } else if (dec->dec_result == DEC_RESULT_EOS) {
6850 avs2_print(dec, PRINT_FLAG_VDEC_STATUS,
6851 "%s: end of stream\n",
6852 __func__);
6853 dec->eos = 1;
6854 check_pic_error(dec, dec->avs2_dec.hc.cur_pic);
6855 avs2_post_process(&dec->avs2_dec);
6856 avs2_prepare_display_buf(dec);
6857 vdec_vframe_dirty(hw_to_vdec(dec), dec->chunk);
6858 } else if (dec->dec_result == DEC_RESULT_FORCE_EXIT) {
6859 avs2_print(dec, PRINT_FLAG_VDEC_STATUS,
6860 "%s: force exit\n",
6861 __func__);
6862 if (dec->stat & STAT_VDEC_RUN) {
6863 amhevc_stop();
6864 dec->stat &= ~STAT_VDEC_RUN;
6865 }
6866
6867 if (dec->stat & STAT_ISR_REG) {
6868 if (!dec->m_ins_flag)
6869 WRITE_VREG(HEVC_ASSIST_MBOX0_MASK, 0);
6870 vdec_free_irq(VDEC_IRQ_0, (void *)dec);
6871 dec->stat &= ~STAT_ISR_REG;
6872 }
6873 }
6874
6875 if (dec->stat & STAT_TIMER_ARM) {
6876 del_timer_sync(&dec->timer);
6877 dec->stat &= ~STAT_TIMER_ARM;
6878 }
6879 /* mark itself has all HW resource released and input released */
6880 if (vdec->parallel_dec ==1)
6881 vdec_core_finish_run(vdec, CORE_MASK_HEVC);
6882 else
6883 vdec_core_finish_run(vdec, CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
6884
6885 if (dec->vdec_cb)
6886 dec->vdec_cb(hw_to_vdec(dec), dec->vdec_cb_arg);
6887}
6888
6889static int avs2_hw_ctx_restore(struct AVS2Decoder_s *dec)
6890{
6891 /* new to do ... */
6892 vavs2_prot_init(dec);
6893 return 0;
6894}
6895
6896static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask)
6897{
6898 struct AVS2Decoder_s *dec =
6899 (struct AVS2Decoder_s *)vdec->private;
6900 int tvp = vdec_secure(hw_to_vdec(dec)) ?
6901 CODEC_MM_FLAGS_TVP : 0;
6902 unsigned long ret = 0;
6903 avs2_print(dec,
6904 PRINT_FLAG_VDEC_DETAIL, "%s\r\n", __func__);
6905 if (debug & AVS2_DBG_PIC_LEAK_WAIT)
6906 return ret;
6907
6908 if (dec->eos)
6909 return ret;
6910 if (!dec->first_sc_checked) {
6911 int size = decoder_mmu_box_sc_check(dec->mmu_box, tvp);
6912 dec->first_sc_checked = 1;
6913 avs2_print(dec, 0, "vavs2 cached=%d need_size=%d speed= %d ms\n",
6914 size, (dec->need_cache_size >> PAGE_SHIFT),
6915 (int)(get_jiffies_64() - dec->sc_start_time) * 1000/HZ);
6916 }
6917
6918 if (dec->next_again_flag &&
6919 (!vdec_frame_based(vdec))) {
6920 u32 parser_wr_ptr =
6921 READ_PARSER_REG(PARSER_VIDEO_WP);
6922 if (parser_wr_ptr >= dec->pre_parser_wr_ptr &&
6923 (parser_wr_ptr - dec->pre_parser_wr_ptr) <
6924 again_threshold) {
6925 int r = vdec_sync_input(vdec);
6926 avs2_print(dec,
6927 PRINT_FLAG_VDEC_DETAIL, "%s buf lelvel:%x\n", __func__, r);
6928 return 0;
6929 }
6930 }
6931/*
6932 if (vdec_stream_based(vdec) && (dec->pic_list_init_flag == 0)
6933 && pre_decode_buf_level != 0) {
6934 u32 rp, wp, level;
6935
6936 rp = READ_PARSER_REG(PARSER_VIDEO_RP);
6937 wp = READ_PARSER_REG(PARSER_VIDEO_WP);
6938 if (wp < rp)
6939 level = vdec->input.size + wp - rp;
6940 else
6941 level = wp - rp;
6942
6943 if (level < pre_decode_buf_level)
6944 return 0;
6945 }
6946*/
6947
6948 if ((dec->pic_list_init_flag == 0) ||
6949 get_free_buf_count(dec) >=
6950 run_ready_min_buf_num)
6951 ret = 1;
6952#ifdef CONSTRAIN_MAX_BUF_NUM
6953 if (dec->pic_list_init_flag) {
6954 if (run_ready_max_vf_only_num > 0 &&
6955 get_vf_ref_only_buf_count(dec) >=
6956 run_ready_max_vf_only_num
6957 )
6958 ret = 0;
6959 if (run_ready_display_q_num > 0 &&
6960 kfifo_len(&dec->display_q) >=
6961 run_ready_display_q_num)
6962 ret = 0;
6963
6964 if (run_ready_max_buf_num == 0xff &&
6965 get_used_buf_count(dec) >=
6966 dec->avs2_dec.ref_maxbuffer)
6967 ret = 0;
6968 else if (run_ready_max_buf_num &&
6969 get_used_buf_count(dec) >=
6970 run_ready_max_buf_num)
6971 ret = 0;
6972 }
6973#endif
6974 if (ret)
6975 not_run_ready[dec->index] = 0;
6976 else
6977 not_run_ready[dec->index]++;
6978
6979 if (vdec->parallel_dec == 1)
6980 return ret ? CORE_MASK_HEVC : 0;
6981 else
6982 return ret ? (CORE_MASK_VDEC_1 | CORE_MASK_HEVC) : 0;
6983}
6984
6985static void run(struct vdec_s *vdec, unsigned long mask,
6986 void (*callback)(struct vdec_s *, void *), void *arg)
6987{
6988 struct AVS2Decoder_s *dec =
6989 (struct AVS2Decoder_s *)vdec->private;
6990 int r;
6991
6992 run_count[dec->index]++;
6993 dec->vdec_cb_arg = arg;
6994 dec->vdec_cb = callback;
6995 /* dec->chunk = vdec_prepare_input(vdec); */
6996 hevc_reset_core(vdec);
6997 dec->pre_parser_wr_ptr =
6998 READ_PARSER_REG(PARSER_VIDEO_WP);
6999 dec->next_again_flag = 0;
7000
7001 r = vdec_prepare_input(vdec, &dec->chunk);
7002 if (r < 0) {
7003 input_empty[dec->index]++;
7004
7005 dec->dec_result = DEC_RESULT_AGAIN;
7006
7007 avs2_print(dec, PRINT_FLAG_VDEC_DETAIL,
7008 "ammvdec_vh265: Insufficient data\n");
7009
7010 vdec_schedule_work(&dec->work);
7011 return;
7012 }
7013 input_empty[dec->index] = 0;
7014 dec->dec_result = DEC_RESULT_NONE;
7015 dec->start_shift_bytes = READ_VREG(HEVC_SHIFT_BYTE_COUNT);
7016
7017 if (debug & PRINT_FLAG_VDEC_STATUS) {
7018 int ii;
7019 avs2_print(dec, 0,
7020 "%s (%d): size 0x%x (0x%x 0x%x) sum 0x%x (%x %x %x %x %x) bytes 0x%x",
7021 __func__,
7022 dec->frame_count, r,
7023 dec->chunk ? dec->chunk->size : 0,
7024 dec->chunk ? dec->chunk->offset : 0,
7025 dec->chunk ? ((vdec_frame_based(vdec) &&
7026 (debug & PRINT_FLAG_VDEC_STATUS)) ?
7027 get_data_check_sum(dec, r) : 0) : 0,
7028 READ_VREG(HEVC_STREAM_START_ADDR),
7029 READ_VREG(HEVC_STREAM_END_ADDR),
7030 READ_VREG(HEVC_STREAM_LEVEL),
7031 READ_VREG(HEVC_STREAM_WR_PTR),
7032 READ_VREG(HEVC_STREAM_RD_PTR),
7033 dec->start_shift_bytes);
7034 if (vdec_frame_based(vdec) && dec->chunk) {
7035 u8 *data = NULL;
7036 if (!dec->chunk->block->is_mapped)
7037 data = codec_mm_vmap(dec->chunk->block->start +
7038 dec->chunk->offset, 8);
7039 else
7040 data = ((u8 *)dec->chunk->block->start_virt) +
7041 dec->chunk->offset;
7042
7043 avs2_print_cont(dec, 0, "data adr %p:",
7044 data);
7045 for (ii = 0; ii < 8; ii++)
7046 avs2_print_cont(dec, 0, "%02x ",
7047 data[ii]);
7048 if (!dec->chunk->block->is_mapped)
7049 codec_mm_unmap_phyaddr(data);
7050 }
7051 avs2_print_cont(dec, 0, "\r\n");
7052 }
7053 if (vdec->mc_loaded) {
7054 /*firmware have load before,
7055 and not changes to another.
7056 ignore reload.
7057 */
7058 } else if (amhevc_loadmc_ex(VFORMAT_AVS2, NULL, dec->fw->data) < 0) {
7059 vdec->mc_loaded = 0;
7060 amhevc_disable();
7061 avs2_print(dec, 0,
7062 "%s: Error amvdec_loadmc fail\n", __func__);
7063 dec->dec_result = DEC_RESULT_FORCE_EXIT;
7064 vdec_schedule_work(&dec->work);
7065 return;
7066 } else {
7067 vdec->mc_loaded = 1;
7068 vdec->mc_type = VFORMAT_AVS2;
7069 }
7070
7071
7072 if (avs2_hw_ctx_restore(dec) < 0) {
7073 vdec_schedule_work(&dec->work);
7074 return;
7075 }
7076
7077 vdec_enable_input(vdec);
7078
7079 WRITE_VREG(HEVC_DEC_STATUS_REG, AVS2_SEARCH_NEW_PIC);
7080
7081 if (vdec_frame_based(vdec) && dec->chunk) {
7082 if (debug & PRINT_FLAG_VDEC_DATA)
7083 dump_data(dec, dec->chunk->size);
7084
7085 WRITE_VREG(HEVC_SHIFT_BYTE_COUNT, 0);
7086 r = dec->chunk->size +
7087 (dec->chunk->offset & (VDEC_FIFO_ALIGN - 1));
7088 if (vdec->mvfrm)
7089 vdec->mvfrm->frame_size = dec->chunk->size;
7090 }
7091
7092 WRITE_VREG(HEVC_DECODE_SIZE, r);
7093 WRITE_VREG(HEVC_DECODE_COUNT, dec->slice_idx);
7094 dec->init_flag = 1;
7095
7096 avs2_print(dec, PRINT_FLAG_VDEC_DETAIL,
7097 "%s: start hevc (%x %x %x)\n",
7098 __func__,
7099 READ_VREG(HEVC_DEC_STATUS_REG),
7100 READ_VREG(HEVC_MPC_E),
7101 READ_VREG(HEVC_MPSR));
7102
7103 start_process_time(dec);
7104 mod_timer(&dec->timer, jiffies);
7105 dec->stat |= STAT_TIMER_ARM;
7106 dec->stat |= STAT_ISR_REG;
7107 if (vdec->mvfrm)
7108 vdec->mvfrm->hw_decode_start = local_clock();
7109 amhevc_start();
7110 dec->stat |= STAT_VDEC_RUN;
7111}
7112
7113static void reset(struct vdec_s *vdec)
7114{
7115
7116 struct AVS2Decoder_s *dec =
7117 (struct AVS2Decoder_s *)vdec->private;
7118
7119 avs2_print(dec,
7120 PRINT_FLAG_VDEC_DETAIL, "%s\r\n", __func__);
7121
7122}
7123
7124static irqreturn_t avs2_irq_cb(struct vdec_s *vdec, int irq)
7125{
7126 struct AVS2Decoder_s *dec =
7127 (struct AVS2Decoder_s *)vdec->private;
7128 return vavs2_isr(0, dec);
7129}
7130
7131static irqreturn_t avs2_threaded_irq_cb(struct vdec_s *vdec, int irq)
7132{
7133 struct AVS2Decoder_s *dec =
7134 (struct AVS2Decoder_s *)vdec->private;
7135 return vavs2_isr_thread_fn(0, dec);
7136}
7137
7138static void avs2_dump_state(struct vdec_s *vdec)
7139{
7140 struct AVS2Decoder_s *dec =
7141 (struct AVS2Decoder_s *)vdec->private;
7142 int i;
7143 avs2_print(dec, 0, "====== %s\n", __func__);
7144
7145 avs2_print(dec, 0,
7146 "width/height (%d/%d), used_buf_num %d\n",
7147 dec->avs2_dec.img.width,
7148 dec->avs2_dec.img.height,
7149 dec->used_buf_num
7150 );
7151
7152 avs2_print(dec, 0,
7153 "is_framebase(%d), eos %d, dec_result 0x%x dec_frm %d disp_frm %d run %d not_run_ready %d input_empty %d\n",
7154 input_frame_based(vdec),
7155 dec->eos,
7156 dec->dec_result,
7157 decode_frame_count[dec->index],
7158 display_frame_count[dec->index],
7159 run_count[dec->index],
7160 not_run_ready[dec->index],
7161 input_empty[dec->index]
7162 );
7163
7164 if (vf_get_receiver(vdec->vf_provider_name)) {
7165 enum receviver_start_e state =
7166 vf_notify_receiver(vdec->vf_provider_name,
7167 VFRAME_EVENT_PROVIDER_QUREY_STATE,
7168 NULL);
7169 avs2_print(dec, 0,
7170 "\nreceiver(%s) state %d\n",
7171 vdec->vf_provider_name,
7172 state);
7173 }
7174
7175 avs2_print(dec, 0,
7176 "%s, newq(%d/%d), dispq(%d/%d), vf prepare/get/put (%d/%d/%d), free_buf_count %d (min %d for run_ready)\n",
7177 __func__,
7178 kfifo_len(&dec->newframe_q),
7179 VF_POOL_SIZE,
7180 kfifo_len(&dec->display_q),
7181 VF_POOL_SIZE,
7182 dec->vf_pre_count,
7183 dec->vf_get_count,
7184 dec->vf_put_count,
7185 get_free_buf_count(dec),
7186 run_ready_min_buf_num
7187 );
7188
7189 dump_pic_list(dec);
7190
7191 for (i = 0; i < MAX_BUF_NUM; i++) {
7192 avs2_print(dec, 0,
7193 "mv_Buf(%d) start_adr 0x%x size 0x%x used %d\n",
7194 i,
7195 dec->m_mv_BUF[i].start_adr,
7196 dec->m_mv_BUF[i].size,
7197 dec->m_mv_BUF[i].used_flag);
7198 }
7199
7200 avs2_print(dec, 0,
7201 "HEVC_DEC_STATUS_REG=0x%x\n",
7202 READ_VREG(HEVC_DEC_STATUS_REG));
7203 avs2_print(dec, 0,
7204 "HEVC_MPC_E=0x%x\n",
7205 READ_VREG(HEVC_MPC_E));
7206 avs2_print(dec, 0,
7207 "DECODE_MODE=0x%x\n",
7208 READ_VREG(DECODE_MODE));
7209 avs2_print(dec, 0,
7210 "NAL_SEARCH_CTL=0x%x\n",
7211 READ_VREG(NAL_SEARCH_CTL));
7212 avs2_print(dec, 0,
7213 "HEVC_PARSER_LCU_START=0x%x\n",
7214 READ_VREG(HEVC_PARSER_LCU_START));
7215 avs2_print(dec, 0,
7216 "HEVC_DECODE_SIZE=0x%x\n",
7217 READ_VREG(HEVC_DECODE_SIZE));
7218 avs2_print(dec, 0,
7219 "HEVC_SHIFT_BYTE_COUNT=0x%x\n",
7220 READ_VREG(HEVC_SHIFT_BYTE_COUNT));
7221 avs2_print(dec, 0,
7222 "HEVC_STREAM_START_ADDR=0x%x\n",
7223 READ_VREG(HEVC_STREAM_START_ADDR));
7224 avs2_print(dec, 0,
7225 "HEVC_STREAM_END_ADDR=0x%x\n",
7226 READ_VREG(HEVC_STREAM_END_ADDR));
7227 avs2_print(dec, 0,
7228 "HEVC_STREAM_LEVEL=0x%x\n",
7229 READ_VREG(HEVC_STREAM_LEVEL));
7230 avs2_print(dec, 0,
7231 "HEVC_STREAM_WR_PTR=0x%x\n",
7232 READ_VREG(HEVC_STREAM_WR_PTR));
7233 avs2_print(dec, 0,
7234 "HEVC_STREAM_RD_PTR=0x%x\n",
7235 READ_VREG(HEVC_STREAM_RD_PTR));
7236 avs2_print(dec, 0,
7237 "PARSER_VIDEO_RP=0x%x\n",
7238 READ_PARSER_REG(PARSER_VIDEO_RP));
7239 avs2_print(dec, 0,
7240 "PARSER_VIDEO_WP=0x%x\n",
7241 READ_PARSER_REG(PARSER_VIDEO_WP));
7242
7243 if (input_frame_based(vdec) &&
7244 (debug & PRINT_FLAG_VDEC_DATA)
7245 ) {
7246 int jj;
7247 if (dec->chunk && dec->chunk->block &&
7248 dec->chunk->size > 0) {
7249 u8 *data = NULL;
7250 if (!dec->chunk->block->is_mapped)
7251 data = codec_mm_vmap(dec->chunk->block->start +
7252 dec->chunk->offset, dec->chunk->size);
7253 else
7254 data = ((u8 *)dec->chunk->block->start_virt) +
7255 dec->chunk->offset;
7256 avs2_print(dec, 0,
7257 "frame data size 0x%x\n",
7258 dec->chunk->size);
7259 for (jj = 0; jj < dec->chunk->size; jj++) {
7260 if ((jj & 0xf) == 0)
7261 avs2_print(dec, 0,
7262 "%06x:", jj);
7263 avs2_print_cont(dec, 0,
7264 "%02x ", data[jj]);
7265 if (((jj + 1) & 0xf) == 0)
7266 avs2_print_cont(dec, 0,
7267 "\n");
7268 }
7269
7270 if (!dec->chunk->block->is_mapped)
7271 codec_mm_unmap_phyaddr(data);
7272 }
7273 }
7274
7275}
7276
7277static int ammvdec_avs2_probe(struct platform_device *pdev)
7278{
7279 struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
7280 int ret;
7281 int config_val;
7282 struct vframe_content_light_level_s content_light_level;
7283 struct vframe_master_display_colour_s vf_dp;
7284
7285 struct BUF_s BUF[MAX_BUF_NUM];
7286 struct AVS2Decoder_s *dec = NULL;
7287 pr_info("%s\n", __func__);
7288 if (pdata == NULL) {
7289 pr_info("\nammvdec_avs2 memory resource undefined.\n");
7290 return -EFAULT;
7291 }
7292 /*dec = (struct AVS2Decoder_s *)devm_kzalloc(&pdev->dev,
7293 sizeof(struct AVS2Decoder_s), GFP_KERNEL);*/
7294 memset(&vf_dp, 0, sizeof(struct vframe_master_display_colour_s));
7295 dec = vmalloc(sizeof(struct AVS2Decoder_s));
7296 memset(dec, 0, sizeof(struct AVS2Decoder_s));
7297 if (dec == NULL) {
7298 pr_info("\nammvdec_avs2 device data allocation failed\n");
7299 return -ENOMEM;
7300 }
7301 if (pdata->parallel_dec == 1) {
7302 int i;
7303 for (i = 0; i < AVS2_MAX_BUFFER_NUM; i++) {
7304 dec->avs2_dec.frm_pool[i].y_canvas_index = -1;
7305 dec->avs2_dec.frm_pool[i].uv_canvas_index = -1;
7306 }
7307 }
7308 pdata->private = dec;
7309 pdata->dec_status = vavs2_dec_status;
7310#ifdef I_ONLY_SUPPORT
7311 pdata->set_trickmode = vavs2_set_trickmode;
7312#endif
7313 pdata->run_ready = run_ready;
7314 pdata->run = run;
7315 pdata->reset = reset;
7316 pdata->irq_handler = avs2_irq_cb;
7317 pdata->threaded_irq_handler = avs2_threaded_irq_cb;
7318 pdata->dump_state = avs2_dump_state;
7319
7320 memcpy(&BUF[0], &dec->m_BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
7321 memset(dec, 0, sizeof(struct AVS2Decoder_s));
7322 memcpy(&dec->m_BUF[0], &BUF[0], sizeof(struct BUF_s) * MAX_BUF_NUM);
7323
7324 dec->index = pdev->id;
7325 dec->m_ins_flag = 1;
7326
7327 if (pdata->use_vfm_path) {
7328 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
7329 VFM_DEC_PROVIDER_NAME);
7330 dec->frameinfo_enable = 1;
7331 } else
7332 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
7333 MULTI_INSTANCE_PROVIDER_NAME ".%02x", pdev->id & 0xff);
7334
7335 vf_provider_init(&pdata->vframe_provider, pdata->vf_provider_name,
7336 &vavs2_vf_provider, dec);
7337
7338 dec->provider_name = pdata->vf_provider_name;
7339 platform_set_drvdata(pdev, pdata);
7340
7341 dec->platform_dev = pdev;
7342 dec->video_signal_type = 0;
7343 dec->video_ori_signal_type = 0;
7344 if (get_cpu_major_id() < AM_MESON_CPU_MAJOR_ID_TXLX)
7345 dec->stat |= VP9_TRIGGER_FRAME_ENABLE;
7346#if 1
7347 if ((debug & IGNORE_PARAM_FROM_CONFIG) == 0 &&
7348 pdata->config_len) {
7349 /*use ptr config for doubel_write_mode, etc*/
7350 avs2_print(dec, 0, "pdata->config=%s\n", pdata->config);
7351 if (get_config_int(pdata->config, "avs2_double_write_mode",
7352 &config_val) == 0)
7353 dec->double_write_mode = config_val;
7354 else
7355 dec->double_write_mode = double_write_mode;
7356
7357 if (get_config_int(pdata->config, "parm_v4l_buffer_margin",
7358 &config_val) == 0)
7359 dec->dynamic_buf_margin = config_val;
7360 else
7361 dec->dynamic_buf_margin = 0;
7362
7363 if (get_config_int(pdata->config, "HDRStaticInfo",
7364 &vf_dp.present_flag) == 0
7365 && vf_dp.present_flag == 1) {
7366 get_config_int(pdata->config, "mG.x",
7367 &vf_dp.primaries[0][0]);
7368 get_config_int(pdata->config, "mG.y",
7369 &vf_dp.primaries[0][1]);
7370 get_config_int(pdata->config, "mB.x",
7371 &vf_dp.primaries[1][0]);
7372 get_config_int(pdata->config, "mB.y",
7373 &vf_dp.primaries[1][1]);
7374 get_config_int(pdata->config, "mR.x",
7375 &vf_dp.primaries[2][0]);
7376 get_config_int(pdata->config, "mR.y",
7377 &vf_dp.primaries[2][1]);
7378 get_config_int(pdata->config, "mW.x",
7379 &vf_dp.white_point[0]);
7380 get_config_int(pdata->config, "mW.y",
7381 &vf_dp.white_point[1]);
7382 get_config_int(pdata->config, "mMaxDL",
7383 &vf_dp.luminance[0]);
7384 get_config_int(pdata->config, "mMinDL",
7385 &vf_dp.luminance[1]);
7386 vf_dp.content_light_level.present_flag = 1;
7387 get_config_int(pdata->config, "mMaxCLL",
7388 &content_light_level.max_content);
7389 get_config_int(pdata->config, "mMaxFALL",
7390 &content_light_level.max_pic_average);
7391 vf_dp.content_light_level = content_light_level;
7392 dec->video_signal_type = (1 << 29)
7393 | (5 << 26) /* unspecified */
7394 | (0 << 25) /* limit */
7395 | (1 << 24) /* color available */
7396 | (9 << 16) /* 2020 */
7397 | (16 << 8) /* 2084 */
7398 | (9 << 0); /* 2020 */
7399 }
7400 dec->vf_dp = vf_dp;
7401 } else
7402#endif
7403 {
7404 /*dec->vavs2_amstream_dec_info.width = 0;
7405 dec->vavs2_amstream_dec_info.height = 0;
7406 dec->vavs2_amstream_dec_info.rate = 30;*/
7407 dec->double_write_mode = double_write_mode;
7408 dec->dynamic_buf_margin = dynamic_buf_num_margin;
7409 }
7410 video_signal_type = dec->video_signal_type;
7411
7412#if 0
7413 dec->buf_start = pdata->mem_start;
7414 dec->buf_size = pdata->mem_end - pdata->mem_start + 1;
7415#else
7416 if (amvdec_avs2_mmu_init(dec) < 0) {
7417 pr_err("avs2 alloc bmmu box failed!!\n");
7418 /* devm_kfree(&pdev->dev, (void *)dec); */
7419 vfree((void *)dec);
7420 return -1;
7421 }
7422 dec->cma_alloc_count = PAGE_ALIGN(work_buf_size) / PAGE_SIZE;
7423 ret = decoder_bmmu_box_alloc_buf_phy(dec->bmmu_box, WORK_SPACE_BUF_ID,
7424 dec->cma_alloc_count * PAGE_SIZE, DRIVER_NAME,
7425 &dec->cma_alloc_addr);
7426 if (ret < 0) {
7427 uninit_mmu_buffers(dec);
7428 /* devm_kfree(&pdev->dev, (void *)dec); */
7429 vfree((void *)dec);
7430 return ret;
7431 }
7432 dec->buf_start = dec->cma_alloc_addr;
7433 dec->buf_size = work_buf_size;
7434#endif
7435 dec->init_flag = 0;
7436 dec->first_sc_checked = 0;
7437 dec->fatal_error = 0;
7438 dec->show_frame_num = 0;
7439
7440 if (debug) {
7441 pr_info("===AVS2 decoder mem resource 0x%lx size 0x%x\n",
7442 dec->buf_start,
7443 dec->buf_size);
7444 }
7445
7446 if (pdata->sys_info) {
7447 dec->vavs2_amstream_dec_info = *pdata->sys_info;
7448 dec->frame_width = dec->vavs2_amstream_dec_info.width;
7449 dec->frame_height = dec->vavs2_amstream_dec_info.height;
7450 } else {
7451 dec->vavs2_amstream_dec_info.width = 0;
7452 dec->vavs2_amstream_dec_info.height = 0;
7453 dec->vavs2_amstream_dec_info.rate = 30;
7454 }
7455
7456 dec->cma_dev = pdata->cma_dev;
7457 if (vavs2_init(pdata) < 0) {
7458 pr_info("\namvdec_avs2 init failed.\n");
7459 avs2_local_uninit(dec);
7460 uninit_mmu_buffers(dec);
7461 /* devm_kfree(&pdev->dev, (void *)dec); */
7462 vfree((void *)dec);
7463 pdata->dec_status = NULL;
7464 return -ENODEV;
7465 }
7466 vdec_set_prepare_level(pdata, start_decode_buf_level);
7467 hevc_source_changed(VFORMAT_AVS2,
7468 4096, 2048, 60);
7469 if (pdata->parallel_dec == 1)
7470 vdec_core_request(pdata, CORE_MASK_HEVC);
7471 else {
7472 vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
7473 | CORE_MASK_COMBINE);
7474 }
7475
7476 return 0;
7477}
7478
7479static int ammvdec_avs2_remove(struct platform_device *pdev)
7480{
7481 struct AVS2Decoder_s *dec = (struct AVS2Decoder_s *)
7482 (((struct vdec_s *)(platform_get_drvdata(pdev)))->private);
7483 struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
7484 int i;
7485
7486 if (debug)
7487 pr_info("amvdec_avs2_remove\n");
7488
7489 vmavs2_stop(dec);
7490
7491 if (pdata->parallel_dec == 1)
7492 vdec_core_release(hw_to_vdec(dec), CORE_MASK_HEVC);
7493 else
7494 vdec_core_release(hw_to_vdec(dec), CORE_MASK_HEVC);
7495
7496 vdec_set_status(hw_to_vdec(dec), VDEC_STATUS_DISCONNECTED);
7497 if (pdata->parallel_dec == 1) {
7498 for (i = 0; i < AVS2_MAX_BUFFER_NUM; i++) {
7499 pdata->free_canvas_ex(dec->avs2_dec.frm_pool[i].y_canvas_index, pdata->id);
7500 pdata->free_canvas_ex(dec->avs2_dec.frm_pool[i].uv_canvas_index, pdata->id);
7501 }
7502 }
7503
7504
7505#ifdef DEBUG_PTS
7506 pr_info("pts missed %ld, pts hit %ld, duration %d\n",
7507 dec->pts_missed, dec->pts_hit, dec->frame_dur);
7508#endif
7509 /* devm_kfree(&pdev->dev, (void *)dec); */
7510 vfree((void *)dec);
7511 return 0;
7512}
7513
7514static struct platform_driver ammvdec_avs2_driver = {
7515 .probe = ammvdec_avs2_probe,
7516 .remove = ammvdec_avs2_remove,
7517 .driver = {
7518 .name = MULTI_DRIVER_NAME,
7519#ifdef CONFIG_PM
7520 .pm = &avs2_pm_ops,
7521#endif
7522 }
7523};
7524#endif
7525static struct mconfig avs2_configs[] = {
7526 MC_PU32("bit_depth_luma", &bit_depth_luma),
7527 MC_PU32("bit_depth_chroma", &bit_depth_chroma),
7528 MC_PU32("frame_width", &frame_width),
7529 MC_PU32("frame_height", &frame_height),
7530 MC_PU32("debug", &debug),
7531 MC_PU32("radr", &radr),
7532 MC_PU32("rval", &rval),
7533 MC_PU32("pop_shorts", &pop_shorts),
7534 MC_PU32("dbg_cmd", &dbg_cmd),
7535 MC_PU32("dbg_skip_decode_index", &dbg_skip_decode_index),
7536 MC_PU32("endian", &endian),
7537 MC_PU32("step", &step),
7538 MC_PU32("udebug_flag", &udebug_flag),
7539 MC_PU32("decode_pic_begin", &decode_pic_begin),
7540 MC_PU32("slice_parse_begin", &slice_parse_begin),
7541 MC_PU32("i_only_flag", &i_only_flag),
7542 MC_PU32("error_handle_policy", &error_handle_policy),
7543 MC_PU32("buf_alloc_width", &buf_alloc_width),
7544 MC_PU32("buf_alloc_height", &buf_alloc_height),
7545 MC_PU32("buf_alloc_depth", &buf_alloc_depth),
7546 MC_PU32("buf_alloc_size", &buf_alloc_size),
7547 MC_PU32("buffer_mode", &buffer_mode),
7548 MC_PU32("buffer_mode_dbg", &buffer_mode_dbg),
7549 MC_PU32("max_buf_num", &max_buf_num),
7550 MC_PU32("dynamic_buf_num_margin", &dynamic_buf_num_margin),
7551 MC_PU32("mem_map_mode", &mem_map_mode),
7552 MC_PU32("double_write_mode", &double_write_mode),
7553 MC_PU32("enable_mem_saving", &enable_mem_saving),
7554 MC_PU32("force_w_h", &force_w_h),
7555 MC_PU32("force_fps", &force_fps),
7556 MC_PU32("max_decoding_time", &max_decoding_time),
7557 MC_PU32("on_no_keyframe_skiped", &on_no_keyframe_skiped),
7558 MC_PU32("start_decode_buf_level", &start_decode_buf_level),
7559 MC_PU32("decode_timeout_val", &decode_timeout_val),
7560};
7561static struct mconfig_node avs2_node;
7562
7563static int __init amvdec_avs2_driver_init_module(void)
7564{
7565
7566#ifdef AVS2_10B_MMU
7567
7568 struct BuffInfo_s *p_buf_info;
7569
7570 if (vdec_is_support_4k()) {
7571 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1)
7572 p_buf_info = &amvavs2_workbuff_spec[2];
7573 else
7574 p_buf_info = &amvavs2_workbuff_spec[1];
7575 } else
7576 p_buf_info = &amvavs2_workbuff_spec[0];
7577
7578 init_buff_spec(NULL, p_buf_info);
7579 work_buf_size =
7580 (p_buf_info->end_adr - p_buf_info->start_adr
7581 + 0xffff) & (~0xffff);
7582
7583#endif
7584 pr_debug("amvdec_avs2 module init\n");
7585
7586#ifdef ERROR_HANDLE_DEBUG
7587 dbg_nal_skip_flag = 0;
7588 dbg_nal_skip_count = 0;
7589#endif
7590 udebug_flag = 0;
7591 decode_pic_begin = 0;
7592 slice_parse_begin = 0;
7593 step = 0;
7594 buf_alloc_size = 0;
7595 if (platform_driver_register(&ammvdec_avs2_driver))
7596 pr_err("failed to register ammvdec_avs2 driver\n");
7597
7598 if (platform_driver_register(&amvdec_avs2_driver)) {
7599 pr_err("failed to register amvdec_avs2 driver\n");
7600 return -ENODEV;
7601 }
7602
7603 if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_SM1) {
7604 amvdec_avs2_profile.profile =
7605 "8k, 10bit, dwrite, compressed";
7606 } else if (get_cpu_major_id() >= AM_MESON_CPU_MAJOR_ID_G12A) {
7607 if (vdec_is_support_4k())
7608 amvdec_avs2_profile.profile =
7609 "4k, 10bit, dwrite, compressed";
7610 else
7611 amvdec_avs2_profile.profile =
7612 "10bit, dwrite, compressed";
7613 } else {
7614 amvdec_avs2_profile.name = "avs2_unsupport";
7615 }
7616
7617 vcodec_profile_register(&amvdec_avs2_profile);
7618 amvdec_avs2_profile_mult = amvdec_avs2_profile;
7619 amvdec_avs2_profile_mult.name = "mavs2";
7620 vcodec_profile_register(&amvdec_avs2_profile_mult);
7621
7622 INIT_REG_NODE_CONFIGS("media.decoder", &avs2_node,
7623 "avs2", avs2_configs, CONFIG_FOR_RW);
7624
7625 return 0;
7626}
7627
7628static void __exit amvdec_avs2_driver_remove_module(void)
7629{
7630 pr_debug("amvdec_avs2 module remove.\n");
7631 platform_driver_unregister(&ammvdec_avs2_driver);
7632 platform_driver_unregister(&amvdec_avs2_driver);
7633}
7634
7635/****************************************/
7636
7637module_param(bit_depth_luma, uint, 0664);
7638MODULE_PARM_DESC(bit_depth_luma, "\n amvdec_avs2 bit_depth_luma\n");
7639
7640module_param(bit_depth_chroma, uint, 0664);
7641MODULE_PARM_DESC(bit_depth_chroma, "\n amvdec_avs2 bit_depth_chroma\n");
7642
7643module_param(frame_width, uint, 0664);
7644MODULE_PARM_DESC(frame_width, "\n amvdec_avs2 frame_width\n");
7645
7646module_param(frame_height, uint, 0664);
7647MODULE_PARM_DESC(frame_height, "\n amvdec_avs2 frame_height\n");
7648
7649module_param(debug, uint, 0664);
7650MODULE_PARM_DESC(debug, "\n amvdec_avs2 debug\n");
7651
7652module_param(debug_again, uint, 0664);
7653MODULE_PARM_DESC(debug_again, "\n amvdec_avs2 debug_again\n");
7654
7655module_param(radr, uint, 0664);
7656MODULE_PARM_DESC(radr, "\nradr\n");
7657
7658module_param(rval, uint, 0664);
7659MODULE_PARM_DESC(rval, "\nrval\n");
7660
7661module_param(pop_shorts, uint, 0664);
7662MODULE_PARM_DESC(pop_shorts, "\nrval\n");
7663
7664module_param(dbg_cmd, uint, 0664);
7665MODULE_PARM_DESC(dbg_cmd, "\ndbg_cmd\n");
7666
7667module_param(dbg_skip_decode_index, uint, 0664);
7668MODULE_PARM_DESC(dbg_skip_decode_index, "\ndbg_skip_decode_index\n");
7669
7670module_param(endian, uint, 0664);
7671MODULE_PARM_DESC(endian, "\nrval\n");
7672
7673module_param(step, uint, 0664);
7674MODULE_PARM_DESC(step, "\n amvdec_avs2 step\n");
7675
7676module_param(decode_pic_begin, uint, 0664);
7677MODULE_PARM_DESC(decode_pic_begin, "\n amvdec_avs2 decode_pic_begin\n");
7678
7679module_param(slice_parse_begin, uint, 0664);
7680MODULE_PARM_DESC(slice_parse_begin, "\n amvdec_avs2 slice_parse_begin\n");
7681
7682module_param(i_only_flag, uint, 0664);
7683MODULE_PARM_DESC(i_only_flag, "\n amvdec_avs2 i_only_flag\n");
7684
7685module_param(error_handle_policy, uint, 0664);
7686MODULE_PARM_DESC(error_handle_policy, "\n amvdec_avs2 error_handle_policy\n");
7687
7688module_param(re_search_seq_threshold, uint, 0664);
7689MODULE_PARM_DESC(re_search_seq_threshold, "\n amvdec_avs2 re_search_seq_threshold\n");
7690
7691module_param(buf_alloc_width, uint, 0664);
7692MODULE_PARM_DESC(buf_alloc_width, "\n buf_alloc_width\n");
7693
7694module_param(buf_alloc_height, uint, 0664);
7695MODULE_PARM_DESC(buf_alloc_height, "\n buf_alloc_height\n");
7696
7697module_param(buf_alloc_depth, uint, 0664);
7698MODULE_PARM_DESC(buf_alloc_depth, "\n buf_alloc_depth\n");
7699
7700module_param(buf_alloc_size, uint, 0664);
7701MODULE_PARM_DESC(buf_alloc_size, "\n buf_alloc_size\n");
7702
7703module_param(buffer_mode, uint, 0664);
7704MODULE_PARM_DESC(buffer_mode, "\n buffer_mode\n");
7705
7706module_param(buffer_mode_dbg, uint, 0664);
7707MODULE_PARM_DESC(buffer_mode_dbg, "\n buffer_mode_dbg\n");
7708/*USE_BUF_BLOCK*/
7709module_param(max_buf_num, uint, 0664);
7710MODULE_PARM_DESC(max_buf_num, "\n max_buf_num\n");
7711
7712module_param(dynamic_buf_num_margin, uint, 0664);
7713MODULE_PARM_DESC(dynamic_buf_num_margin, "\n dynamic_buf_num_margin\n");
7714
7715#ifdef CONSTRAIN_MAX_BUF_NUM
7716module_param(run_ready_max_vf_only_num, uint, 0664);
7717MODULE_PARM_DESC(run_ready_max_vf_only_num, "\n run_ready_max_vf_only_num\n");
7718
7719module_param(run_ready_display_q_num, uint, 0664);
7720MODULE_PARM_DESC(run_ready_display_q_num, "\n run_ready_display_q_num\n");
7721
7722module_param(run_ready_max_buf_num, uint, 0664);
7723MODULE_PARM_DESC(run_ready_max_buf_num, "\n run_ready_max_buf_num\n");
7724#endif
7725
7726module_param(mv_buf_margin, uint, 0664);
7727MODULE_PARM_DESC(mv_buf_margin, "\n mv_buf_margin\n");
7728
7729module_param(run_ready_min_buf_num, uint, 0664);
7730MODULE_PARM_DESC(run_ready_min_buf_num, "\n run_ready_min_buf_num\n");
7731
7732/**/
7733
7734module_param(mem_map_mode, uint, 0664);
7735MODULE_PARM_DESC(mem_map_mode, "\n mem_map_mode\n");
7736
7737module_param(double_write_mode, uint, 0664);
7738MODULE_PARM_DESC(double_write_mode, "\n double_write_mode\n");
7739
7740module_param(enable_mem_saving, uint, 0664);
7741MODULE_PARM_DESC(enable_mem_saving, "\n enable_mem_saving\n");
7742
7743module_param(force_w_h, uint, 0664);
7744MODULE_PARM_DESC(force_w_h, "\n force_w_h\n");
7745
7746module_param(force_fps, uint, 0664);
7747MODULE_PARM_DESC(force_fps, "\n force_fps\n");
7748
7749module_param(max_decoding_time, uint, 0664);
7750MODULE_PARM_DESC(max_decoding_time, "\n max_decoding_time\n");
7751
7752module_param(on_no_keyframe_skiped, uint, 0664);
7753MODULE_PARM_DESC(on_no_keyframe_skiped, "\n on_no_keyframe_skiped\n");
7754
7755
7756module_param(start_decode_buf_level, int, 0664);
7757MODULE_PARM_DESC(start_decode_buf_level,
7758 "\n avs2 start_decode_buf_level\n");
7759
7760module_param(decode_timeout_val, uint, 0664);
7761MODULE_PARM_DESC(decode_timeout_val,
7762 "\n avs2 decode_timeout_val\n");
7763
7764module_param_array(decode_frame_count, uint,
7765 &max_decode_instance_num, 0664);
7766
7767module_param_array(display_frame_count, uint,
7768 &max_decode_instance_num, 0664);
7769
7770module_param_array(max_process_time, uint,
7771 &max_decode_instance_num, 0664);
7772
7773module_param_array(run_count, uint,
7774 &max_decode_instance_num, 0664);
7775
7776module_param_array(input_empty, uint,
7777 &max_decode_instance_num, 0664);
7778
7779module_param_array(not_run_ready, uint,
7780 &max_decode_instance_num, 0664);
7781
7782module_param(video_signal_type, uint, 0664);
7783MODULE_PARM_DESC(video_signal_type, "\n amvdec_avs2 video_signal_type\n");
7784
7785module_param(force_video_signal_type, uint, 0664);
7786MODULE_PARM_DESC(force_video_signal_type, "\n amvdec_avs2 force_video_signal_type\n");
7787
7788module_param(enable_force_video_signal_type, uint, 0664);
7789MODULE_PARM_DESC(enable_force_video_signal_type, "\n amvdec_avs2 enable_force_video_signal_type\n");
7790
7791
7792module_param(udebug_flag, uint, 0664);
7793MODULE_PARM_DESC(udebug_flag, "\n amvdec_h265 udebug_flag\n");
7794
7795module_param(udebug_pause_pos, uint, 0664);
7796MODULE_PARM_DESC(udebug_pause_pos, "\n udebug_pause_pos\n");
7797
7798module_param(udebug_pause_val, uint, 0664);
7799MODULE_PARM_DESC(udebug_pause_val, "\n udebug_pause_val\n");
7800
7801module_param(udebug_pause_decode_idx, uint, 0664);
7802MODULE_PARM_DESC(udebug_pause_decode_idx, "\n udebug_pause_decode_idx\n");
7803
7804module_param(pre_decode_buf_level, int, 0664);
7805MODULE_PARM_DESC(pre_decode_buf_level,
7806 "\n amvdec_avs2 pre_decode_buf_level\n");
7807
7808module_param(again_threshold, uint, 0664);
7809MODULE_PARM_DESC(again_threshold, "\n again_threshold\n");
7810
7811
7812module_param(force_disp_pic_index, int, 0664);
7813MODULE_PARM_DESC(force_disp_pic_index,
7814 "\n amvdec_h265 force_disp_pic_index\n");
7815
7816module_param(without_display_mode, uint, 0664);
7817MODULE_PARM_DESC(without_display_mode, "\n without_display_mode\n");
7818
7819module_init(amvdec_avs2_driver_init_module);
7820module_exit(amvdec_avs2_driver_remove_module);
7821
7822MODULE_DESCRIPTION("AMLOGIC avs2 Video Decoder Driver");
7823MODULE_LICENSE("GPL");
7824MODULE_AUTHOR("Tim Yao <tim.yao@amlogic.com>");
7825