xref: /utopia/UTPA2-700.0.x/modules/vdec_v2/hal/mustang/vpu_ex/halVPU_EX.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 
96 //-------------------------------------------------------------------------------------------------
97 //  Include Files
98 //-------------------------------------------------------------------------------------------------
99 // Common Definition
100 #include <string.h>
101 
102 #if defined(REDLION_LINUX_KERNEL_ENVI)
103 #include "drvHVD_Common.h"
104 #else
105 #include "MsCommon.h"
106 #endif
107 
108 #include "MsOS.h"
109 #include "asmCPU.h"
110 
111 
112 #ifndef MSOS_TYPE_NUTTX
113 
114 // Internal Definition
115 #include "regVPU_EX.h"
116 #include "halVPU_EX.h"
117 #include "halCHIP.h"
118 #include "../../../drv/hvd_ex/drvHVD_def.h"
119 #include "controller.h"
120 
121 #include "../hvd_ex/fwHVD_if.h"
122 #include "../mvd_ex/mvd4_interface.h"
123 #include "../hvd_ex/halHVD_EX.h"
124 #if (ENABLE_DECOMPRESS_FUNCTION == TRUE)
125 #include "ms_decompress.h"
126 #include "ms_decompress_priv.h"
127 #endif
128 #include "../../drv/mbx/apiMBX_St.h"
129 #include "../../drv/mbx/apiMBX.h"
130 
131 #if VPU_ENABLE_BDMA_FW_FLASH_2_SDRAM
132 #include "drvSERFLASH.h"
133 #define HVD_FLASHcpy(DESTADDR, SRCADDR, LEN, Flag)  MDrv_SERFLASH_CopyHnd((MS_PHYADDR)(SRCADDR), (MS_PHYADDR)(DESTADDR), (LEN), (Flag), SPIDMA_OPCFG_DEF)
134 #endif
135 
136 //-------------------------------------------------------------------------------------------------
137 //  Driver Compiler Options
138 //-------------------------------------------------------------------------------------------------
139 
140 
141 //-------------------------------------------------------------------------------------------------
142 //  Local Defines
143 //-------------------------------------------------------------------------------------------------
144 #define ENABLE_TEE 0
145 #define VPU_CTL_INTERFACE_VER   0x00000001  //the interface version of VPU driver
146 
147 #define VPU_MIU1BASE_ADDR    0x40000000     //Notice: this define must be comfirm with designer
148 #define MAX_SUPPORT_DECODER_NUM 2
149 typedef enum
150 {
151     E_VDEC_EX_REE_TO_TEE_MBX_MSG_NULL,
152     E_VDEC_EX_REE_TO_TEE_MBX_MSG_FW_LoadCode,
153     E_VDEC_EX_REE_TO_TEE_MBX_MSG_GETSHMBASEADDR,
154 } VDEC_REE_TO_TEE_MBX_MSG_TYPE;
155 
156 
157 typedef enum
158 {
159     E_VDEC_EX_TEE_TO_REE_MBX_MSG_NULL,
160     E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_INVALID,
161     E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_NO_TEE,
162     E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS,
163     E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_FAIL
164 } VDEC_TEE_TO_REE_MBX_ACK_TYPE;
165 
166 typedef enum
167 {
168     E_VPU_UART_CTRL_DISABLE = BIT(4),
169     E_VPU_UART_CTRL_ERR     = BIT(0),
170     E_VPU_UART_CTRL_INFO    = BIT(1),
171     E_VPU_UART_CTRL_DBG     = BIT(2),
172     E_VPU_UART_CTRL_FW      = BIT(3),
173     E_VPU_UART_CTRL_MUST    = BIT(4),
174     E_VPU_UART_CTRL_TRACE   = BIT(5),
175 } VPU_EX_UartCtrl;
176 
177 typedef struct
178 {
179     HAL_VPU_StreamId eStreamId;
180     VPU_EX_DecoderType eDecodertype;
181 } VPU_EX_Stream;
182 
183 
184 #ifndef ANDROID
185 #define VPU_PRINT  printf
186 #define VPU_ERR printf
187 #else
188 #include <sys/mman.h>
189 #include <cutils/ashmem.h>
190 #include <cutils/log.h>
191 #ifndef LOGI // android 4.1 rename LOGx to ALOGx
192 #define VPU_PRINT ALOGI
193 #else
194 #define VPU_PRINT LOGI
195 #endif
196 #ifndef LOGE // android 4.1 rename LOGx to ALOGx
197 #define VPU_ERR ALOGE
198 #else
199 #define VPU_ERR LOGE
200 #endif
201 #endif
202 
203 
204 #define VPU_MSG_ERR(format, args...)                \
205     do                                              \
206     {                                               \
207         if (u32VpuUartCtrl & E_VPU_UART_CTRL_ERR)  \
208         {                                           \
209             VPU_ERR("[VPU][ERR]%s:", __FUNCTION__);  \
210             VPU_ERR(format, ##args);                 \
211         }                                           \
212     } while (0)
213 
214 #define VPU_MSG_DBG(format, args...)                \
215     do                                              \
216     {                                               \
217         if (u32VpuUartCtrl & E_VPU_UART_CTRL_DBG)  \
218         {                                           \
219             VPU_ERR("[VPU][DBG]%s:", __FUNCTION__);  \
220             VPU_ERR(format, ##args);                 \
221         }                                           \
222     } while (0)
223 
224 #define VPU_MSG_INFO(format, args...)               \
225     do                                              \
226     {                                               \
227         if (u32VpuUartCtrl & E_VPU_UART_CTRL_INFO) \
228         {                                           \
229             VPU_ERR("[VPU][INF]%s:", __FUNCTION__);  \
230             VPU_ERR(format, ##args);                 \
231         }                                           \
232     } while (0)
233 
234 //------------------------------ MIU SETTINGS ----------------------------------
235 #define _MaskMiuReq_VPU_D_RW(m)     _VPU_WriteRegBit(MIU0_REG_RQ0_MASK, m, BIT(6))
236 #define _MaskMiuReq_VPU_Q_RW(m)     _VPU_WriteRegBit(MIU0_REG_RQ0_MASK, m, BIT(6))
237 #define _MaskMiuReq_VPU_I_R(m)      _VPU_WriteRegBit(MIU0_REG_RQ0_MASK+1, m, BIT(0))
238 
239 #define _MaskMiu1Req_VPU_D_RW(m)    _VPU_WriteRegBit(MIU1_REG_RQ0_MASK, m, BIT(6))
240 #define _MaskMiu1Req_VPU_Q_RW(m)    _VPU_WriteRegBit(MIU1_REG_RQ0_MASK, m, BIT(6))
241 #define _MaskMiu1Req_VPU_I_R(m)     _VPU_WriteRegBit(MIU1_REG_RQ0_MASK+1, m, BIT(0))
242 
243 #define VPU_D_RW_ON_MIU1            ((_VPU_ReadByte(MIU0_REG_SEL0) & BIT(6)) == BIT(6))
244 #define VPU_Q_RW_ON_MIU1            ((_VPU_ReadByte(MIU0_REG_SEL0) & BIT(6)) == BIT(6))
245 #define VPU_I_R_ON_MIU1             ((_VPU_ReadByte(MIU0_REG_SEL0+1) & BIT(0)) == BIT(0)) //g08
246 
247 #define _VPU_MIU_SetReqMask(miu_clients, mask)  \
248    do {                                         \
249        if (miu_clients##_ON_MIU1 == 0)          \
250            _MaskMiuReq_##miu_clients(mask);     \
251        else                                     \
252            _MaskMiu1Req_##miu_clients(mask);    \
253    } while(0)
254 
255 #if ENABLE_VPU_MUTEX_PROTECTION
256 static MS_S32 s32VPUMutexID = -1;
257 static const MS_U8 _u8VPU_Mutex[] = { "VPU_Mutex" };
258 
259 #define _HAL_VPU_MutexCreate()  \
260     if (s32VPUMutexID < 0)      \
261     {                           \
262         s32VPUMutexID = MsOS_CreateMutex(E_MSOS_FIFO,(char*)_u8VPU_Mutex, MSOS_PROCESS_SHARED); \
263     }
264 
265 #define _HAL_VPU_MutexDelete()              \
266     if (s32VPUMutexID >= 0)                 \
267     {                                       \
268         MsOS_DeleteMutex(s32VPUMutexID);    \
269         s32VPUMutexID = -1;                 \
270     }
271 
272 #define _HAL_VPU_Entry()                                                \
273     if (s32VPUMutexID >= 0)                                             \
274     {                                                                   \
275         if (!MsOS_ObtainMutex(s32VPUMutexID, VPU_DEFAULT_MUTEX_TIMEOUT))       \
276         {                                                               \
277             VPU_PRINT("[HAL VPU][%06d] Mutex taking timeout\n", __LINE__); \
278         }                                                               \
279     }
280 
281 #define _HAL_VPU_Return(_ret)                   \
282     {                                           \
283         if (s32VPUMutexID >= 0)                 \
284         {                                       \
285             MsOS_ReleaseMutex(s32VPUMutexID);   \
286         }                                       \
287         return _ret;                            \
288     }
289 
290 #define _HAL_VPU_Release()                      \
291     {                                           \
292         if (s32VPUMutexID >= 0)                 \
293         {                                       \
294             MsOS_ReleaseMutex(s32VPUMutexID);   \
295         }                                       \
296     }
297 #else
298 #define _HAL_VPU_MutexCreate()
299 #define _HAL_VPU_MutexDelete()
300 #define _HAL_VPU_Entry()
301 #define _HAL_VPU_Return(_ret)       {return _ret;}
302 #define _HAL_VPU_Release()
303 #endif
304 
305 #define VPU_FW_MEM_OFFSET   0x100000UL  // 1M
306 #define VPU_CMD_TIMEOUT     1000 // 1 sec
307 
308 //-------------------------------------------------------------------------------------------------
309 //  Local Structures
310 //-------------------------------------------------------------------------------------------------
311 typedef struct _VPU_HWInitFunc
312 {
313     MS_BOOL (*pfMVDHW_Init)(void);
314     MS_BOOL (*pfMVDHW_Deinit)(void);
315     MS_BOOL (*pfHVDHW_Init)(MS_U32 u32Arg);
316     MS_BOOL (*pfHVDHW_Deinit)(void);
317 } VPU_HWInitFunc;
318 
319 typedef struct
320 {
321     MS_U32  u32ApiHW_Version;   //<Version of current structure>
322     MS_U16  u16ApiHW_Length;    //<Length of this structure>
323 
324     MS_U8   u8Cap_Support_Decoder_Num;
325 
326     MS_BOOL bCap_Support_MPEG2;
327     MS_BOOL bCap_Support_H263;
328     MS_BOOL bCap_Support_MPEG4;
329     MS_BOOL bCap_Support_DIVX311;
330     MS_BOOL bCap_Support_DIVX412;
331     MS_BOOL bCap_Support_FLV;
332     MS_BOOL bCap_Support_VC1ADV;
333     MS_BOOL bCap_Support_VC1MAIN;
334 
335     MS_BOOL bCap_Support_RV8;
336     MS_BOOL bCap_Support_RV9;
337     MS_BOOL bCap_Support_H264;
338     MS_BOOL bCap_Support_AVS;
339     MS_BOOL bCap_Support_AVS_PLUS;
340     MS_BOOL bCap_Support_MJPEG;
341     MS_BOOL bCap_Support_MVC;
342     MS_BOOL bCap_Support_VP8;
343     MS_BOOL bCap_Support_VP9;
344     MS_BOOL bCap_Support_HEVC;
345 
346     /*New HW Cap and Feature add in struct at the end*/
347 }VDEC_HwCap;
348 
349 //-------------------------------------------------------------------------------------------------
350 //  Local Functions Prototype
351 //-------------------------------------------------------------------------------------------------
352 static MS_BOOL          _VPU_EX_LoadVLCTable(VPU_EX_VLCTblCfg *pVlcCfg, MS_U8 u8FwSrcType);
353 static MS_U8            _VPU_EX_GetOffsetIdx(MS_U32 u32Id);
354 static HVD_User_Cmd     _VPU_EX_MapCtrlCmd(VPU_EX_TaskInfo *pTaskInfo);
355 
356 //-------------------------------------------------------------------------------------------------
357 //  Global Variables
358 //-------------------------------------------------------------------------------------------------
359 extern HVD_Return HAL_HVD_EX_SetCmd(MS_U32 u32Id, HVD_User_Cmd eUsrCmd, MS_U32 u32CmdArg);
360 extern MS_BOOL HAL_MVD_InitHW(void);
361 extern MS_BOOL HAL_MVD_DeinitHW(void);
362 extern MS_BOOL HAL_HVD_EX_InitHW(MS_U32 u32Id,VPU_EX_DecoderType DecoderType);
363 extern MS_BOOL HAL_HVD_EX_DeinitHW(void);
364 extern void    HAL_HVD_EX_SetBufferAddr(MS_U32 u32Id);
365 
366 #if defined (__aeon__)
367 static MS_U32 u32VPURegOSBase = 0xA0000000;
368 #else
369 static MS_U32 u32VPURegOSBase = 0xBF200000;
370 #endif
371 
372 //-------------------------------------------------------------------------------------------------
373 //  Local Variables
374 //-------------------------------------------------------------------------------------------------
375 #if 0
376 
377 static MS_BOOL _bVPUPowered = FALSE;
378 static MS_BOOL _bVPURsted = FALSE;
379 static MS_BOOL _bVPUSingleMode = FALSE;
380 static VPU_EX_DecModCfg _stVPUDecMode;
381 
382 static MS_U8 u8TaskCnt = 0;
383 
384 static MS_U32 u32VpuUartCtrl = (E_VPU_UART_CTRL_ERR | E_VPU_UART_CTRL_MUST);
385 
386 //Notice: this function must be consistent with _VPU_EX_GetOffsetIdx()
387 static VPU_EX_Stream _stVPUStream[] =
388 {
389     {E_HAL_VPU_MAIN_STREAM0, E_VPU_EX_DECODER_NONE},
390     {E_HAL_VPU_SUB_STREAM0, E_VPU_EX_DECODER_NONE},
391 };
392 static VPU_HWInitFunc stHWInitFunc =
393 {
394     &HAL_MVD_InitHW,
395     &HAL_MVD_DeinitHW,
396     &HAL_HVD_EX_InitHW,
397     &HAL_HVD_EX_DeinitHW,
398 };
399 
400 #endif
401 
402 #if VPU_ENABLE_EMBEDDED_FW_BINARY
403 static const MS_U8 u8HVD_FW_Binary[] = {
404     #include "fwVPU.dat"
405 };
406 
407 #if HVD_ENABLE_RV_FEATURE
408 static const MS_U8 u8HVD_VLC_Binary[] = {
409     #include "fwVPU_VLC.dat"
410 };
411 #endif
412 #endif
413 
414 typedef struct
415 {
416     MS_BOOL _bVPUPowered;
417     MS_BOOL _bVPURsted;
418     MS_BOOL _bVPUSingleMode;
419     VPU_EX_DecModCfg _stVPUDecMode;
420     MS_U8 u8TaskCnt;
421     //Notice: this function must be consistent with _VPU_EX_GetOffsetIdx()
422     VPU_EX_Stream _stVPUStream[2];
423 
424     VPU_HWInitFunc stHWInitFunc;
425 
426     MS_BOOL bVpuExReloadFW;
427     MS_BOOL bVpuExLoadFWRlt;
428     MS_U32  u32VPUSHMAddr;    //PA
429     MS_BOOL bEnableVPUSecureMode;
430 
431     MS_U32  u32FWShareInfoAddr[4];
432 } VPU_Hal_CTX;
433 
434 //global variables
435 VPU_Hal_CTX* pVPUHalContext = NULL;
436 VPU_Hal_CTX gVPUHalContext;
437 MS_U32 u32VpuUartCtrl = (E_VPU_UART_CTRL_ERR | E_VPU_UART_CTRL_MUST);
438 #if ENABLE_TEE
439 MS_BOOL bVPUMbxInitFlag = 0;
440 MS_U8 u8VPUMbxMsgClass = 0;
441 MBX_Msg VPUReeToTeeMbxMsg;
442 MBX_Msg VPUTeeToReeMbxMsg;
443 #endif
444 //-------------------------------------------------------------------------------------------------
445 //  Debug Functions
446 //-------------------------------------------------------------------------------------------------
447 
448 
449 //-------------------------------------------------------------------------------------------------
450 //  Local Functions
451 //-------------------------------------------------------------------------------------------------
452 
_VPU_EX_LoadVLCTable(VPU_EX_VLCTblCfg * pVlcCfg,MS_U8 u8FwSrcType)453 static MS_BOOL _VPU_EX_LoadVLCTable(VPU_EX_VLCTblCfg *pVlcCfg, MS_U8 u8FwSrcType)
454 {
455 #if HVD_ENABLE_RV_FEATURE
456     if (E_HVD_FW_INPUT_SOURCE_FLASH == u8FwSrcType)
457     {
458 #if VPU_ENABLE_BDMA_FW_FLASH_2_SDRAM
459         VPU_MSG_DBG("Load VLC outF2D: dest:0x%lx source:%lx size:%lx\n",
460             pVlcCfg->u32DstAddr, pVlcCfg->u32BinAddr, pVlcCfg->u32BinSize);
461 
462         if (pVlcCfg->u32BinSize)
463         {
464             SPIDMA_Dev cpyflag = E_SPIDMA_DEV_MIU1;
465 
466             if (HAL_MIU1_BASE <= MsOS_VA2PA(pVlcCfg->u32FrameBufAddr))
467             {
468                 cpyflag = E_SPIDMA_DEV_MIU1;
469             }
470             else
471             {
472                 cpyflag = E_SPIDMA_DEV_MIU0;
473             }
474 
475             if (!HVD_FLASHcpy(MsOS_VA2PA(pVlcCfg->u32DstAddr), MsOS_VA2PA(pVlcCfg->u32BinAddr), pVlcCfg->u32BinSize, cpyflag))
476             {
477                 VPU_MSG_ERR("HVD_BDMAcpy VLC table Flash 2 DRAM failed: dest:0x%lx src:0x%lx size:0x%lx flag:%lu\n",
478                      pVlcCfg->u32DstAddr, pVlcCfg->u32BinAddr, pVlcCfg->u32BinSize, (MS_U32) cpyflag);
479 
480                 return FALSE;
481             }
482         }
483         else
484         {
485             VPU_MSG_ERR("During copy VLC from Flash to Dram, the source size of FW is zero\n");
486             return FALSE;
487         }
488 #else
489         VPU_MSG_ERR("driver not enable to use BDMA copy VLC from flash 2 sdram.\n");
490         return FALSE;
491 #endif
492     }
493     else
494     {
495         if (E_HVD_FW_INPUT_SOURCE_DRAM == u8FwSrcType)
496         {
497             if ((pVlcCfg->u32BinAddr != 0) && (pVlcCfg->u32BinSize != 0))
498             {
499                 VPU_MSG_INFO("Load VLC outD2D: dest:0x%lx source:%lx size:%lx\n",
500                             pVlcCfg->u32DstAddr, pVlcCfg->u32BinAddr, pVlcCfg->u32BinSize);
501 
502 #if HVD_ENABLE_BDMA_2_BITSTREAMBUF
503                 BDMA_Result bdmaRlt;
504                 MS_U32 u32DstAdd = 0, u32SrcAdd = 0, u32tabsize = 0;
505 
506                 u32DstAdd   = pVlcCfg->u32FrameBufAddr + pVlcCfg->u32VLCTableOffset;
507                 u32SrcAdd   = pVlcCfg->u32BinAddr;
508                 u32tabsize  = pVlcCfg->u32BinSize;
509                 //bdmaRlt = MDrv_BDMA_MemCopy(u32SrcAdd, u32DstAdd, SLQ_TBL_SIZE);
510                 MsOS_FlushMemory();
511                 bdmaRlt = HVD_dmacpy(u32DstAdd, u32SrcAdd, u32tabsize);
512 
513                 if (E_BDMA_OK != bdmaRlt)
514                 {
515                     VPU_MSG_ERR("MDrv_BDMA_MemCopy fail in %s(), ret=%x!\n", __FUNCTION__, bdmaRlt);
516                 }
517 #else
518                 HVD_memcpy(pVlcCfg->u32DstAddr, pVlcCfg->u32BinAddr, pVlcCfg->u32BinSize);
519 #endif
520             }
521             else
522             {
523                 VPU_MSG_ERR
524                     ("During copy VLC from out Dram to Dram, the source size or virtual address of VLC is zero\n");
525                 return FALSE;
526             }
527         }
528         else
529         {
530 #if VPU_ENABLE_EMBEDDED_FW_BINARY
531 #ifdef HVD_CACHE_TO_UNCACHE_CONVERT
532             MS_U8 *pu8HVD_VLC_Binary;
533 
534             pu8HVD_VLC_Binary = (MS_U8 *) ((MS_U32) u8HVD_VLC_Binary | 0xA0000000);
535 
536             VPU_MSG_DBG("Load VLC inD2D: dest:0x%lx source:%lx size:%lx\n",
537                         pVlcCfg->u32FrameBufAddr + pVlcCfg->u32VLCTableOffset, ((MS_U32) pu8HVD_VLC_Binary),
538                         (MS_U32) sizeof(u8HVD_VLC_Binary));
539 
540             HVD_memcpy((void *) (pVlcCfg->u32FrameBufAddr + pVlcCfg->u32VLCTableOffset),
541                        (void *) ((MS_U32) pu8HVD_VLC_Binary), sizeof(u8HVD_VLC_Binary));
542 #else
543             VPU_MSG_INFO("Load VLC inD2D: dest:0x%lx source:%lx size:%lx\n",
544                         MsOS_VA2PA(pVlcCfg->u32DstAddr), (MS_U32) u8HVD_VLC_Binary,
545                         (MS_U32) sizeof(u8HVD_VLC_Binary));
546 
547             HVD_memcpy(pVlcCfg->u32DstAddr, ((MS_U32) u8HVD_VLC_Binary), sizeof(u8HVD_VLC_Binary));
548 #endif
549 #else
550             VPU_MSG_ERR("driver not enable to use embedded VLC binary.\n");
551             return FALSE;
552 #endif
553         }
554     }
555 #endif
556 
557     return TRUE;
558 }
559 
560 //Notice: this function must be consistent with _stVPUStream[]
_VPU_EX_GetOffsetIdx(MS_U32 u32Id)561 static MS_U8 _VPU_EX_GetOffsetIdx(MS_U32 u32Id)
562 {
563     MS_U8 u8OffsetIdx = 0;
564     MS_U8 u8VSidBaseMask = 0xF0;
565     HAL_VPU_StreamId eVSidBase = (HAL_VPU_StreamId)(u32Id & u8VSidBaseMask);
566 
567     switch (eVSidBase)
568     {
569         case E_HAL_VPU_MAIN_STREAM_BASE:
570         {
571             u8OffsetIdx = 0;
572             break;
573         }
574         case E_HAL_VPU_SUB_STREAM_BASE:
575         {
576             u8OffsetIdx = 1;
577             break;
578         }
579         case E_HAL_VPU_MVC_STREAM_BASE:
580         {
581             u8OffsetIdx = 0;
582             break;
583         }
584         default:
585         {
586             u8OffsetIdx = 0;
587             break;
588         }
589     }
590 
591     /*
592     VPU_MSG_DBG("u32Id=0x%lx, eVSidBase=0x%x, u8OffsetIdx=0x%x\n",
593         u32Id, eVSidBase, u8OffsetIdx);
594         */
595     return u8OffsetIdx;
596 }
597 
_VPU_EX_Context_Init(void)598 static void _VPU_EX_Context_Init(void)
599 {
600     pVPUHalContext->_stVPUStream[0].eStreamId = E_HAL_VPU_MAIN_STREAM0;
601     pVPUHalContext->_stVPUStream[1].eStreamId = E_HAL_VPU_SUB_STREAM0;
602     pVPUHalContext->bVpuExReloadFW = TRUE;
603 
604     pVPUHalContext->u32FWShareInfoAddr[0] = 0xFFFFFFFF;
605     pVPUHalContext->u32FWShareInfoAddr[1] = 0xFFFFFFFF;
606     pVPUHalContext->u32FWShareInfoAddr[2] = 0xFFFFFFFF;
607     pVPUHalContext->u32FWShareInfoAddr[3] = 0xFFFFFFFF;
608 }
609 
610 
_VPU_EX_MapCtrlCmd(VPU_EX_TaskInfo * pTaskInfo)611 static HVD_User_Cmd _VPU_EX_MapCtrlCmd(VPU_EX_TaskInfo *pTaskInfo)
612 {
613     HVD_User_Cmd eCmd = E_HVD_CMD_INVALID_CMD;
614     MS_U8 u8OffsetIdx = 0;
615 
616     if (NULL == pTaskInfo)
617     {
618         return eCmd;
619     }
620 
621     u8OffsetIdx = _VPU_EX_GetOffsetIdx(pTaskInfo->u32Id);
622 
623     VPU_MSG_INFO("input TaskInfo u32Id=0x%08lx eVpuId=0x%x src=0x%x dec=0x%x\n",
624          pTaskInfo->u32Id, pTaskInfo->eVpuId, pTaskInfo->eSrcType, pTaskInfo->eDecType);
625 
626     if (E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType)
627     {
628         if (E_VPU_EX_INPUT_TSP == pTaskInfo->eSrcType)
629         {
630             eCmd = (u8OffsetIdx == 0) ? E_DUAL_CMD_TASK0_MVD_TSP : E_DUAL_CMD_TASK1_MVD_TSP;
631         }
632         else if (E_VPU_EX_INPUT_FILE == pTaskInfo->eSrcType)
633         {
634             eCmd = (u8OffsetIdx == 0) ? E_DUAL_CMD_TASK0_MVD_SLQ : E_DUAL_CMD_TASK1_MVD_SLQ;
635         }
636     }
637     else if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType)
638     {
639         if (E_VPU_EX_INPUT_TSP == pTaskInfo->eSrcType)
640         {
641             eCmd = (u8OffsetIdx == 0) ? E_DUAL_CMD_TASK0_HVD_TSP : E_DUAL_CMD_TASK1_HVD_TSP;
642         }
643         else if (E_VPU_EX_INPUT_FILE == pTaskInfo->eSrcType)
644         {
645             eCmd = (u8OffsetIdx == 0) ? E_DUAL_CMD_TASK0_HVD_BBU : E_DUAL_CMD_TASK1_HVD_BBU;
646         }
647     }
648 
649     VPU_MSG_INFO("output: eCmd=0x%x offsetIdx=0x%x\n", eCmd, u8OffsetIdx);
650     return eCmd;
651 }
652 
_VPU_EX_InitHW(VPU_EX_TaskInfo * pTaskInfo)653 static MS_BOOL _VPU_EX_InitHW(VPU_EX_TaskInfo *pTaskInfo)
654 {
655     if (!pTaskInfo)
656     {
657         VPU_MSG_ERR("null input\n");
658         return FALSE;
659     }
660 
661     //Check if we need to init MVD HW
662     if ((E_VPU_EX_INPUT_TSP == pTaskInfo->eSrcType) ||
663         (E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType))
664     {
665         //Init HW
666         if (FALSE == HAL_VPU_EX_MVDInUsed())
667         {
668             if (TRUE != HAL_MVD_InitHW())
669             {
670                 VPU_MSG_ERR("(%d):HAL_MVD_InitHW failed\n", __LINE__);
671                 return FALSE;
672             }
673         }
674         else
675         {
676             VPU_MSG_ERR("(%d): do nothing\n", __LINE__);
677         }
678     }
679 
680     //MVD use sub mvop
681     if((E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType) &&
682         (E_HAL_VPU_SUB_STREAM0 == pTaskInfo->eVpuId) &&
683         (E_VPU_DEC_MODE_DUAL_INDIE ==pVPUHalContext->_stVPUDecMode.u8DecMod))
684     {
685         VPU_MSG_ERR("Force turn on HVD\n");
686         if(!HAL_VPU_EX_HVDInUsed())
687         {
688             if (!HAL_HVD_EX_InitHW(pTaskInfo->u32Id,pTaskInfo->eDecType))
689             {
690                  VPU_MSG_ERR("(%d):HAL_HVD_EX_InitHW failed\n", __LINE__);
691                  return FALSE;
692             }
693         }
694         else
695         {
696             VPU_MSG_ERR("(%d): do nothing, HVD already init\n", __LINE__);
697         }
698     }
699 
700     //Check if we need to init HVD HW
701     if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType)
702     {
703         if (!HAL_VPU_EX_MVDInUsed())
704         {
705             if (!HAL_MVD_InitHW())
706             {
707                 VPU_MSG_ERR("(%d):HAL_MVD_InitHW failed\n", __LINE__);
708                 return FALSE;
709             }
710         }
711 
712         if (!HAL_HVD_EX_InitHW(pTaskInfo->u32Id,pTaskInfo->eDecType))
713         {
714             VPU_MSG_ERR("(%d):HAL_HVD_EX_InitHW failed\n", __LINE__);
715             return FALSE;
716         }
717     }
718 
719     return TRUE;
720 }
721 
_VPU_EX_InClock(MS_U32 u32type)722 static MS_U32 _VPU_EX_InClock(MS_U32 u32type)
723 {
724     switch (u32type)
725     {
726         case VPU_CLOCK_240MHZ:
727             return 240000000UL;
728         case VPU_CLOCK_216MHZ:
729             return 216000000UL;
730         case VPU_CLOCK_192MHZ:
731             return 192000000UL;
732         case VPU_CLOCK_320MHZ:
733             return 320000000UL;
734         case VPU_CLOCK_288MHZ:
735             return 288000000UL;
736         default:
737             return 240000000UL;
738     }
739 }
740 
741 #if defined(MSOS_TYPE_LINUX)
742 //For REE
HAL_VPU_EX_REE_RegisterMBX(void)743 MS_BOOL HAL_VPU_EX_REE_RegisterMBX(void)
744 {
745 
746 #if ENABLE_TEE
747     MS_U8 ClassNum = 0;
748     MBX_Result result;
749 
750     if (bVPUMbxInitFlag == TRUE)
751     {
752         return TRUE;
753     }
754 
755     if (E_MBX_SUCCESS != MApi_MBX_Init(E_MBX_CPU_MIPS,E_MBX_ROLE_HK,1000))
756     {
757         VPU_MSG_ERR("VDEC_TEE MApi_MBX_Init fail\n");
758         return FALSE;
759     }
760     else
761     {
762         MApi_MBX_Enable(TRUE);
763     }
764 
765     if (E_MBX_SUCCESS != MApi_MBX_QueryDynamicClass(E_MBX_CPU_MIPS_VPE1, "VDEC_TEE", (MS_U8 *)&ClassNum))
766     {
767         VPU_MSG_ERR("VDEC_TEE MApi_MBX_QueryDynamicClass fail\n");
768         return FALSE;
769     }
770 
771     result = MApi_MBX_RegisterMSG(ClassNum, 10);
772 
773     if (( E_MBX_SUCCESS != result) && ( E_MBX_ERR_SLOT_AREADY_OPENNED != result ))
774     {
775         VPU_MSG_ERR("%s fail\n",__FUNCTION__);
776         return FALSE;
777     }
778     else
779     {
780         bVPUMbxInitFlag = TRUE;
781         u8VPUMbxMsgClass = ClassNum;
782         return TRUE;
783     }
784 #else
785     return FALSE;
786 #endif
787 }
788 #if ENABLE_TEE
_VPU_EX_REE_SendMBXMsg(VDEC_REE_TO_TEE_MBX_MSG_TYPE msg_type)789 VDEC_TEE_TO_REE_MBX_ACK_TYPE _VPU_EX_REE_SendMBXMsg(VDEC_REE_TO_TEE_MBX_MSG_TYPE msg_type)
790 {
791     MBX_Result result;
792     MS_U8 u8Index;
793 
794     if (pVPUHalContext->bEnableVPUSecureMode == FALSE)
795     {
796         return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_NO_TEE;
797     }
798 
799     if (bVPUMbxInitFlag == FALSE)
800     {
801         return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_INVALID;
802     }
803 
804     VPUReeToTeeMbxMsg.eRoleID = E_MBX_CPU_MIPS_VPE1;
805     VPUReeToTeeMbxMsg.u8Ctrl = 0;
806     VPUReeToTeeMbxMsg.eMsgType = E_MBX_MSG_TYPE_INSTANT;
807     VPUReeToTeeMbxMsg.u8MsgClass = u8VPUMbxMsgClass;
808     VPUReeToTeeMbxMsg.u8Index = msg_type;
809 
810     result = MApi_MBX_SendMsg(&VPUReeToTeeMbxMsg);
811     if (E_MBX_SUCCESS != result)
812     {
813         return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_FAIL;
814     }
815 
816     // Receive Reply ACK from TEE side.
817     memset(&VPUTeeToReeMbxMsg, 0, sizeof(MBX_Msg));
818 
819     VPUTeeToReeMbxMsg.u8MsgClass = u8VPUMbxMsgClass;
820 
821 #if 0 // marked temperarily, wait kernel team to fix MApi_MBX_RecvMsg.
822     if(E_MBX_SUCCESS != MApi_MBX_RecvMsg(TEE_MBX_MSG_CLASS, &(TEE_TO_REE_MBX_MSG), 20, MBX_CHECK_INSTANT_MSG))
823     {
824         VPU_MSG_ERR("VDEC get Secure world ACK fail\n");
825         return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_FAIL;
826     }
827     else
828 #else
829     do
830     {
831         result = MApi_MBX_RecvMsg(u8VPUMbxMsgClass, &VPUTeeToReeMbxMsg, 2000, MBX_CHECK_INSTANT_MSG);
832     } while(E_MBX_SUCCESS != result);
833 #endif
834     {
835         u8Index = VPUTeeToReeMbxMsg.u8Index;
836         VPU_MSG_DBG("VDEC get ACK cmd:%x\n", u8Index);
837 
838         if (E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_FAIL == u8Index)
839         {
840             return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_FAIL;
841         }
842     }
843 
844     return E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS;
845 }
846 #endif
847 
848 
HAL_VPU_EX_REE_SetSHMBaseAddr(void)849 MS_BOOL HAL_VPU_EX_REE_SetSHMBaseAddr(void)
850 {
851 #if ENABLE_TEE
852     if(_VPU_EX_REE_SendMBXMsg(E_VDEC_EX_REE_TO_TEE_MBX_MSG_GETSHMBASEADDR) != E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
853     {
854         VPU_MSG_ERR("[Error] VDEC load code in Secure world fail!\n");
855         return FALSE;
856     }
857     else
858     {
859         MS_U32 u32VPUSHMoffset = (VPUTeeToReeMbxMsg.u8Parameters[0]&0xff) |
860                                  ((VPUTeeToReeMbxMsg.u8Parameters[1]<<8)&0xff00)|
861                                  ((VPUTeeToReeMbxMsg.u8Parameters[2]<<16)&0xff0000)|
862                                  ((VPUTeeToReeMbxMsg.u8Parameters[3]<<24)&0xff000000);
863         MS_U32 u32VPUSHMsize =   (VPUTeeToReeMbxMsg.u8Parameters[4]&0xff) |
864                                  ((VPUTeeToReeMbxMsg.u8Parameters[5]<<8)&0xff00)|
865                                  ((VPUTeeToReeMbxMsg.u8Parameters[6]<<16)&0xff0000)|
866                                  ((VPUTeeToReeMbxMsg.u8Parameters[7]<<24)&0xff000000);
867 
868         VPU_MSG_INFO("u32VPUSHMoffset %x,u32VPUSHMsize %x,miu %d\n",(unsigned int)u32VPUSHMoffset,(unsigned int)u32VPUSHMsize,VPUTeeToReeMbxMsg.u8Parameters[8]);
869         if(VPUTeeToReeMbxMsg.u8Parameters[8] == 1)
870         {
871             pVPUHalContext->u32VPUSHMAddr = u32VPUSHMoffset+HAL_MIU1_BASE;
872         }
873         else
874         {
875             pVPUHalContext->u32VPUSHMAddr = u32VPUSHMoffset;
876         }
877     }
878     return TRUE;
879 #else
880     return FALSE;
881 #endif
882 }
883 
884 #endif
885 
HAL_VPU_EX_GetFWReload(void)886 MS_BOOL HAL_VPU_EX_GetFWReload(void)
887 {
888     return pVPUHalContext->bVpuExReloadFW;
889 }
890 
_VPU_EX_IsNeedDecompress(MS_U32 u32SrcAddr)891 static MS_BOOL _VPU_EX_IsNeedDecompress(MS_U32 u32SrcAddr)
892 {
893     if(*((MS_U8*)(u32SrcAddr))=='V' && *((MS_U8*)(u32SrcAddr+1))=='D'
894         && *((MS_U8*)(u32SrcAddr+2))=='E' && *((MS_U8*)(u32SrcAddr+3))=='C'
895         && *((MS_U8*)(u32SrcAddr+4))=='2' && *((MS_U8*)(u32SrcAddr+5))=='0'
896         && *((MS_U8*)(u32SrcAddr+0x88))=='V' && *((MS_U8*)(u32SrcAddr+0x89))=='D'
897         && *((MS_U8*)(u32SrcAddr+0x8a))=='E' && *((MS_U8*)(u32SrcAddr+0x8b))=='C'
898         && *((MS_U8*)(u32SrcAddr+0x8c))=='2' && *((MS_U8*)(u32SrcAddr+0x8d))=='0'
899         )
900     {
901         return FALSE;
902     }
903     else
904     {
905         return TRUE;
906     }
907 }
908 
_VPU_EX_InitAll(VPU_EX_NDecInitPara * pInitPara)909 static MS_BOOL _VPU_EX_InitAll(VPU_EX_NDecInitPara *pInitPara)
910 {
911     MS_U32 u32fwPA = NULL;  //physical address
912     VPU_EX_ClockSpeed eClkSpeed = E_VPU_EX_CLOCK_240MHZ;
913 
914     if (TRUE == HAL_VPU_EX_IsPowered())
915     {
916         VPU_MSG_DBG("IsPowered\n");
917         return TRUE;
918     }
919     else
920     {
921         //VPU hold
922         HAL_VPU_EX_SwRst(FALSE);
923 
924         //VPU clock on
925         VPU_EX_InitParam VPUInitParams = {eClkSpeed, FALSE, -1, VPU_DEFAULT_MUTEX_TIMEOUT, TRUE};
926         VPUInitParams.bInMIU1 = VPU_I_R_ON_MIU1;
927 
928         HAL_VPU_EX_Init(&VPUInitParams);
929     }
930 
931     VPU_EX_FWCodeCfg *pFWCodeCfg   = NULL;
932     VPU_EX_TaskInfo  *pTaskInfo    = NULL;
933     VPU_EX_VLCTblCfg *pVlcCfg      = NULL;
934 
935     if (pInitPara)
936     {
937         pFWCodeCfg  = pInitPara->pFWCodeCfg;
938         pTaskInfo   = pInitPara->pTaskInfo;
939         pVlcCfg     = pInitPara->pVLCCfg;
940     }
941     else
942     {
943         VPU_MSG_DBG("(%d) NULL para\n", __LINE__);
944         return FALSE;
945     }
946 
947     u32fwPA = MsOS_VA2PA(pFWCodeCfg->u32DstAddr);
948 #if ENABLE_TEE
949 #if defined(MSOS_TYPE_LINUX)
950     if(pVPUHalContext->bEnableVPUSecureMode == TRUE)
951     {
952         VPU_MSG_INFO("Load VDEC f/w code in Secure World\n");
953 
954         if (FALSE == HAL_VPU_EX_GetFWReload())
955         {
956             if (FALSE == pVPUHalContext->bVpuExLoadFWRlt)
957             {
958                 VPU_MSG_INFO("Never load fw successfully, load it anyway!\n");
959                 if(_VPU_EX_REE_SendMBXMsg(E_VDEC_EX_REE_TO_TEE_MBX_MSG_FW_LoadCode) != E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
960                 {
961                     VPU_MSG_ERR("[Error] VDEC load code in Secure world fail!\n");
962                     return FALSE;
963                 }
964                 pVPUHalContext->bVpuExLoadFWRlt = TRUE;
965             }
966             else
967             {
968                 //Check f/w prefix "VDEC20"
969                 if (_VPU_EX_IsNeedDecompress(pFWCodeCfg->u32DstAddr) != FALSE)
970                 {
971                     VPU_MSG_ERR("Wrong prefix: reload fw!\n");
972                     if(_VPU_EX_REE_SendMBXMsg(E_VDEC_EX_REE_TO_TEE_MBX_MSG_FW_LoadCode) != E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
973                     {
974                         VPU_MSG_ERR("[Error] VDEC load code in Secure world fail!\n");
975                         pVPUHalContext->bVpuExLoadFWRlt = FALSE;
976                         return FALSE;
977                     }
978                 }
979                 else
980                 {
981                     VPU_MSG_INFO("Skip loading fw this time!!!\n");
982                 }
983             }
984         }
985         else
986         {
987             if(_VPU_EX_REE_SendMBXMsg(E_VDEC_EX_REE_TO_TEE_MBX_MSG_FW_LoadCode) != E_VDEC_EX_TEE_TO_REE_MBX_ACK_MSG_ACTION_SUCCESS)
988             {
989                 VPU_MSG_ERR("[Error] VDEC load code in Secure world fail!\n");
990                 pVPUHalContext->bVpuExLoadFWRlt = FALSE;
991                 return FALSE;
992             }
993             pVPUHalContext->bVpuExLoadFWRlt = TRUE;
994         }
995     }
996     else
997 #endif
998 #endif
999     {
1000         VPU_MSG_INFO("Load VDEC f/w code in Normal World\n");
1001 
1002         if (!HAL_VPU_EX_LoadCode(pFWCodeCfg))
1003         {
1004             VPU_MSG_ERR("HAL_VPU_EX_LoadCode fail!\n");
1005             return FALSE;
1006         }
1007     }
1008 
1009     if (pVlcCfg)
1010     {
1011         if (!_VPU_EX_LoadVLCTable(pVlcCfg, pFWCodeCfg->u8SrcType))
1012         {
1013             VPU_MSG_ERR("HAL_VPU_LoadVLCTable fail!\n");
1014             return FALSE;
1015         }
1016     }
1017 
1018     if (!HAL_VPU_EX_CPUSetting(u32fwPA))
1019     {
1020         VPU_MSG_ERR("HAL_VPU_EX_CPUSetting fail!\n");
1021         return FALSE;
1022     }
1023 
1024     //Init HW
1025     if (FALSE == _VPU_EX_InitHW(pTaskInfo))
1026     {
1027         VPU_MSG_ERR("(%d): InitHW failed\n", __LINE__);
1028         //_MVD_INIT_FAIL_RET();
1029         return FALSE;
1030     }
1031     else
1032     {
1033         VPU_MSG_DBG("(%d): InitHW success\n", __LINE__);
1034     }
1035 
1036     //set vpu clock to FW
1037     struct _ctl_info *ctl_ptr = (struct _ctl_info *)
1038                     MsOS_PA2KSEG1(MsOS_VA2PA(pInitPara->pFWCodeCfg->u32DstAddr) + CTL_INFO_ADDR);
1039 
1040     ctl_ptr->statue = CTL_STU_NONE;
1041     //notify controller the interface version of VPU driver.
1042     ctl_ptr->ctl_interface = VPU_CTL_INTERFACE_VER;
1043     ctl_ptr->vpu_clk = _VPU_EX_InClock(eClkSpeed);
1044     MsOS_FlushMemory();
1045     VPU_MSG_DBG("clock speed=0x%x\n", ctl_ptr->vpu_clk);
1046 
1047     //Release VPU: For dual decoder, we only release VPU if it is not released yet.
1048     if (TRUE == HAL_VPU_EX_IsRsted())
1049     {
1050         VPU_MSG_DBG("VPU_IsRsted\n");
1051         return TRUE;
1052     }
1053     else
1054     {
1055         HAL_VPU_EX_SwRstRelse();
1056     }
1057 
1058     return TRUE;
1059 }
1060 
_VPU_EX_DeinitHW(void)1061 static MS_BOOL _VPU_EX_DeinitHW(void)
1062 {
1063     MS_BOOL bRet = FALSE;
1064 
1065     if (FALSE == HAL_VPU_EX_MVDInUsed())
1066     {
1067         bRet = HAL_MVD_DeinitHW();
1068     }
1069 
1070     if (FALSE == HAL_VPU_EX_HVDInUsed())
1071     {
1072         bRet = HAL_HVD_EX_DeinitHW();
1073     }
1074 
1075     return bRet;
1076 }
1077 
_VPU_EX_DeinitAll(void)1078 static MS_BOOL _VPU_EX_DeinitAll(void)
1079 {
1080     HAL_VPU_EX_SwRst(TRUE);
1081     _VPU_EX_DeinitHW();
1082     HAL_VPU_EX_DeInit();
1083 
1084     return TRUE;
1085 }
1086 
_VPU_EX_GetActiveCodecCnt(void)1087 static MS_U8 _VPU_EX_GetActiveCodecCnt(void)
1088 {
1089     MS_U32 i;
1090     MS_U8  u8ActiveCnt = 0;
1091     for (i = 0; i < sizeof(pVPUHalContext->_stVPUStream) / sizeof(pVPUHalContext->_stVPUStream[0]); i++)
1092     {
1093         if (E_VPU_EX_DECODER_NONE != pVPUHalContext->_stVPUStream[i].eDecodertype)
1094         {
1095             u8ActiveCnt++;
1096         }
1097     }
1098     if (pVPUHalContext->u8TaskCnt != u8ActiveCnt)
1099     {
1100         VPU_MSG_ERR("Err u8TaskCnt(%d) != u8ActiveCnt(%d)\n", pVPUHalContext->u8TaskCnt, u8ActiveCnt);
1101     }
1102     VPU_MSG_DBG(" = %d\n", u8ActiveCnt);
1103     return u8ActiveCnt;
1104 }
_VPU_EX_ClockInv(MS_BOOL bEnable)1105 static void _VPU_EX_ClockInv(MS_BOOL bEnable)
1106 {
1107     if (TRUE)
1108     {
1109         _VPU_WriteWordMask(REG_TOP_VPU, 0, TOP_CKG_VPU_INV);
1110     }
1111     else
1112     {
1113         _VPU_WriteWordMask(REG_TOP_VPU, TOP_CKG_VPU_INV, TOP_CKG_VPU_INV);
1114     }
1115 }
1116 
_VPU_EX_ClockSpeed(MS_U32 u32type)1117 static void _VPU_EX_ClockSpeed(MS_U32 u32type)
1118 {
1119     switch (u32type)
1120     {
1121         case VPU_CLOCK_240MHZ:
1122         case VPU_CLOCK_216MHZ:
1123         case VPU_CLOCK_192MHZ:
1124         case VPU_CLOCK_320MHZ:
1125         case VPU_CLOCK_288MHZ:
1126             _VPU_WriteWordMask(REG_TOP_VPU, u32type, TOP_CKG_VPU_CLK_MASK);
1127             break;
1128         default:
1129             _VPU_WriteWordMask(REG_TOP_VPU, VPU_CLOCK_240MHZ, TOP_CKG_VPU_CLK_MASK);
1130             break;
1131     }
1132 }
1133 
_VPU_EX_MAU_IDLE(void)1134 static MS_BOOL _VPU_EX_MAU_IDLE(void)
1135 {
1136     if (((_VPU_Read2Byte(MAU1_ARB0_DBG0) & MAU1_FSM_CS_MASK) == MAU1_FSM_CS_IDLE)
1137         && ((_VPU_Read2Byte(MAU1_ARB1_DBG0) & MAU1_FSM_CS_MASK) == MAU1_FSM_CS_IDLE))
1138     {
1139         return TRUE;
1140     }
1141     return FALSE;
1142 }
1143 
1144 
1145 #if (ENABLE_DECOMPRESS_FUNCTION==TRUE)
_VPU_EX_DecompressBin(MS_U32 u32SrcAddr,MS_U32 u32SrcSize,MS_U32 u32DestAddr,MS_U32 u32SlidingAddr)1146 static MS_BOOL _VPU_EX_DecompressBin(MS_U32 u32SrcAddr, MS_U32 u32SrcSize, MS_U32 u32DestAddr, MS_U32 u32SlidingAddr)
1147 {
1148     if(_VPU_EX_IsNeedDecompress(u32SrcAddr))
1149     {
1150         ms_VDECDecompressInit((MS_U8*)u32SlidingAddr, (MS_U8*)u32DestAddr);
1151         ms_VDECDecompress((MS_U8*)u32SrcAddr, u32SrcSize);
1152         ms_VDECDecompressDeInit();
1153         return TRUE;
1154     }
1155     else
1156     {
1157         return FALSE;
1158     }
1159 }
1160 #endif
1161 
HAL_VPU_EX_SetSingleDecodeMode(MS_BOOL bEnable)1162 MS_BOOL HAL_VPU_EX_SetSingleDecodeMode(MS_BOOL bEnable)
1163 {
1164     MS_BOOL bRet = TRUE;
1165     pVPUHalContext->_bVPUSingleMode = bEnable;
1166     return bRet;
1167 }
1168 
HAL_VPU_EX_SetDecodeMode(VPU_EX_DecModCfg * pstCfg)1169 MS_BOOL HAL_VPU_EX_SetDecodeMode(VPU_EX_DecModCfg *pstCfg)
1170 {
1171     MS_BOOL bRet = TRUE;
1172     MS_U8 i=0;
1173     if (pstCfg != NULL)
1174     {
1175         pVPUHalContext->_stVPUDecMode.u8DecMod = pstCfg->u8DecMod;
1176         pVPUHalContext->_stVPUDecMode.u8CodecCnt = pstCfg->u8CodecCnt;
1177         for (i=0; ((i<pstCfg->u8CodecCnt)&&(i<VPU_MAX_DEC_NUM)); i++)
1178         {
1179             pVPUHalContext->_stVPUDecMode.u8CodecType[i] = pstCfg->u8CodecType[i];
1180         }
1181         pVPUHalContext->_stVPUDecMode.u8ArgSize = pstCfg->u8ArgSize;
1182         pVPUHalContext->_stVPUDecMode.u32Arg    = pstCfg->u32Arg;
1183     }
1184     else
1185     {
1186         bRet = FALSE;
1187     }
1188     return bRet;
1189 }
1190 
1191 //static MS_BOOL bVpuExReloadFW = TRUE;
1192 //static MS_BOOL bVpuExLoadFWRlt = FALSE;
HAL_VPU_EX_SetFWReload(MS_BOOL bReload)1193 MS_BOOL HAL_VPU_EX_SetFWReload(MS_BOOL bReload)
1194 {
1195     pVPUHalContext->bVpuExReloadFW = bReload;
1196     //VPU_PRINT("%s bVpuExReloadFW = %x\n", __FUNCTION__, bVpuExReloadFW);
1197     return TRUE;
1198 }
1199 
1200 
1201 //-------------------------------------------------------------------------------------------------
1202 //  Global Functions
1203 //-------------------------------------------------------------------------------------------------
HAL_VPU_EX_TaskCreate(MS_U32 u32Id,VPU_EX_NDecInitPara * pInitPara)1204 MS_BOOL HAL_VPU_EX_TaskCreate(MS_U32 u32Id, VPU_EX_NDecInitPara *pInitPara)
1205 {
1206     VPU_EX_TaskInfo *pTaskInfo  = pInitPara->pTaskInfo;
1207     MS_U8 u8Offset              = _VPU_EX_GetOffsetIdx(u32Id);
1208     HVD_User_Cmd eCmd           = E_HVD_CMD_INVALID_CMD;
1209     VPU_EX_DecoderType eDecType = E_VPU_EX_DECODER_NONE;
1210     MS_U32 u32Arg = 0xFFFFFFFF;
1211     MS_U32 u32Timeout = 0;
1212     HVD_Return eCtrlRet = E_HVD_RETURN_FAIL;
1213     MS_U32 u32CmdArg = 0;
1214     struct _ctl_info *ctl_ptr = (struct _ctl_info *)
1215                     MsOS_PA2KSEG1(MsOS_VA2PA(pInitPara->pFWCodeCfg->u32DstAddr) + CTL_INFO_ADDR);
1216 
1217     _HAL_VPU_Entry();
1218     //Check FW buffer size
1219     if (1 == u8Offset)
1220     {
1221         MS_U32 u32MinFWBuffSize = (u8Offset + 1) * VPU_FW_MEM_OFFSET;
1222         MS_U32 u32CurFWBuffSize = pInitPara->pFWCodeCfg->u32DstSize;
1223 
1224         if (u32CurFWBuffSize < u32MinFWBuffSize)
1225         {
1226             VPU_MSG_ERR("FW BuffSize(0x%lx < 0x%lx) is too small!\n", u32CurFWBuffSize, u32MinFWBuffSize);
1227             _HAL_VPU_Release();
1228             return FALSE;
1229         }
1230     }
1231 
1232     if (0 == pVPUHalContext->u8TaskCnt)
1233     {
1234         //No task is created, need to load f/w, etc.
1235         VPU_MSG_DBG("u8TaskCnt=%d\n", pVPUHalContext->u8TaskCnt);
1236 
1237         if (!_VPU_EX_InitAll(pInitPara))
1238         {
1239             VPU_MSG_DBG("(%d) fail to InitAll\n", __LINE__);
1240             _HAL_VPU_Release();
1241             return FALSE;
1242         }
1243 
1244         //Check if controller finish initialization: clear mailbox, etc.
1245         //Need to check it before sending any controller commands!
1246         u32Timeout = HVD_GetSysTime_ms() + VPU_CMD_TIMEOUT;
1247         while (CTL_STU_NONE == ctl_ptr->statue)
1248         {
1249             if (HVD_GetSysTime_ms() > u32Timeout)
1250             {
1251                 VPU_MSG_ERR("Ctl init timeout, st=%x\n", ctl_ptr->statue);
1252                 VPU_MSG_ERR("version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1253                      ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1254                 MS_U32 t=0;
1255                 for (t=0; t<30; t++)
1256                 {
1257                     VPU_MSG_DBG("_pc=0x%lx\n", HAL_VPU_EX_GetProgCnt());
1258                 }
1259                 _HAL_VPU_Release();
1260                 return FALSE;
1261             }
1262 
1263             MsOS_ReadMemory();
1264         }
1265 
1266         VPU_MSG_INFO("ctl_init_done: version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1267              ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1268 
1269     }
1270     else
1271     {
1272         if (pVPUHalContext->_bVPUSingleMode)
1273         {
1274             //Show error message
1275             VPU_PRINT("This task will use dram instead of sram!!!\n");
1276             VPU_MSG_INFO("VDEC warn: this task will use dram instead of sram!!!\n");
1277         }
1278 
1279         if (!_VPU_EX_InitHW(pInitPara->pTaskInfo))
1280         {
1281             VPU_MSG_DBG("(%d) fail to InitHW\n", __LINE__);
1282             _HAL_VPU_Release();
1283             return FALSE;
1284         }
1285         if (pInitPara->pVLCCfg)
1286         {
1287             if (!_VPU_EX_LoadVLCTable(pInitPara->pVLCCfg, pInitPara->pFWCodeCfg->u8SrcType))
1288             {
1289                 VPU_MSG_ERR("HAL_VPU_LoadVLCTable fail!\n");
1290                 _HAL_VPU_Release();
1291                 return FALSE;
1292             }
1293         }
1294     }
1295 
1296     #if 1  // For TEE
1297     if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType || E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType)
1298     {
1299         MS_U32 u32FWPhyAddr = MsOS_VA2PA(pInitPara->pFWCodeCfg->u32DstAddr);
1300 
1301         if (pVPUHalContext->u32FWShareInfoAddr[u8Offset] == 0xFFFFFFFF)
1302         {
1303             ctl_ptr->u32TaskShareInfoAddr[u8Offset] = 0xFFFFFFFF;
1304         }
1305         else
1306         {
1307             ctl_ptr->u32TaskShareInfoAddr[u8Offset] = pVPUHalContext->u32FWShareInfoAddr[u8Offset] - u32FWPhyAddr;
1308         }
1309 
1310         MsOS_FlushMemory();
1311         VPU_MSG_DBG("task share info offset = 0x%x\n", ctl_ptr->u32TaskShareInfoAddr[u8Offset]);
1312 
1313         ///printf("DRV side,      share info offset = 0x%lx\n", pVPUHalContext->u32FWShareInfoAddr[u8Offset]);
1314         ///printf("FW side,  task share info offset = 0x%x\n", ctl_ptr->u32TaskShareInfoAddr[u8Offset]);
1315     }
1316     #endif
1317 
1318     if ((TRUE==pVPUHalContext->_bVPUSingleMode) || (E_VPU_DEC_MODE_SINGLE==pVPUHalContext->_stVPUDecMode.u8DecMod))
1319     {
1320         //Issue E_DUAL_CMD_SINGLE_TASK to FW controller
1321         //arg=1 to get better performance for single task
1322         u32CmdArg = (pVPUHalContext->_bVPUSingleMode) ? 1 : 0;
1323         VPU_MSG_DBG("Issue E_DUAL_CMD_SINGLE_TASK to FW controller arg=%lx\n", u32CmdArg);
1324         eCtrlRet = HAL_HVD_EX_SetCmd(u32Id, E_DUAL_CMD_SINGLE_TASK, u32CmdArg);
1325         if (E_HVD_RETURN_SUCCESS != eCtrlRet)
1326         {
1327             VPU_MSG_ERR("E_DUAL_CMD_SINGLE_TASK NG eCtrlRet=%x\n", eCtrlRet);
1328         }
1329     }
1330     else if (E_VPU_DEC_MODE_DUAL_3D==pVPUHalContext->_stVPUDecMode.u8DecMod)
1331     {
1332         if(pVPUHalContext->_stVPUDecMode.u8CodecType[0] != pVPUHalContext->_stVPUDecMode.u8CodecType[1])
1333         {
1334             switch (pVPUHalContext->_stVPUDecMode.u32Arg)
1335             {
1336                 case E_VPU_CMD_MODE_KR3D_INTERLACE:
1337                     u32CmdArg = CTL_MODE_3DTV;
1338                     break;
1339                 case E_VPU_CMD_MODE_KR3D_FORCE_P:
1340                     u32CmdArg = CTL_MODE_3DTV_PROG;
1341                     break;
1342                 case E_VPU_CMD_MODE_KR3D_INTERLACE_TWO_PITCH:
1343                     u32CmdArg = CTL_MODE_3DTV_TWO_PITCH;
1344                     break;
1345                 case E_VPU_CMD_MODE_KR3D_FORCE_P_TWO_PITCH:
1346                     u32CmdArg = CTL_MODE_3DTV_PROG_TWO_PITCH;
1347                     break;
1348                 default:
1349                     u32CmdArg = CTL_MODE_3DTV;
1350                     VPU_MSG_INFO("%lx not defined, use CTL_MODE_3DTV for KR3D\n", pVPUHalContext->_stVPUDecMode.u32Arg);
1351                     break;
1352             }
1353         }
1354         else
1355         {
1356             u32CmdArg = CTL_MODE_3DWMV;
1357         }
1358         VPU_MSG_DBG("Issue E_DUAL_CMD_MODE to FW controller arg=%lx\n", u32CmdArg);
1359         eCtrlRet = HAL_HVD_EX_SetCmd(u32Id, E_DUAL_CMD_MODE, u32CmdArg);
1360         if (E_HVD_RETURN_SUCCESS != eCtrlRet)
1361         {
1362             VPU_MSG_ERR("E_DUAL_CMD_MODE NG eCtrlRet=%x\n", eCtrlRet);
1363         }
1364     }
1365     else if(E_VPU_DEC_MODE_DUAL_INDIE == pVPUHalContext->_stVPUDecMode.u8DecMod)
1366     {
1367         if(E_VPU_CMD_MODE_PIP_SYNC_MAIN_STC == pVPUHalContext->_stVPUDecMode.u32Arg)
1368         {
1369             u32CmdArg = CTL_MODE_ONE_STC;
1370         }
1371         else
1372         {
1373             u32CmdArg = (pVPUHalContext->_stVPUDecMode.u32Arg==E_VPU_CMD_MODE_PIP_SYNC_SWITCH) ? CTL_MODE_SWITCH_STC : CTL_MODE_NORMAL;
1374         }
1375         VPU_MSG_DBG("Issue E_DUAL_CMD_MODE to FW controller arg=%lx\n", u32CmdArg);
1376         eCtrlRet = HAL_HVD_EX_SetCmd(u32Id, E_DUAL_CMD_MODE, u32CmdArg);
1377         if (E_HVD_RETURN_SUCCESS != eCtrlRet)
1378         {
1379             VPU_MSG_ERR("E_DUAL_CMD_MODE NG eCtrlRet=%x\n", eCtrlRet);
1380         }
1381     }
1382     // Set heap size for current task
1383     ctl_ptr->heap_size[u8Offset] = pInitPara->pTaskInfo->u32HeapSize;
1384     MsOS_FlushMemory();
1385 
1386     eCmd = _VPU_EX_MapCtrlCmd(pTaskInfo);
1387 #if defined(SUPPORT_NEW_MEM_LAYOUT)
1388     if (E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType)
1389         u32Arg = u8Offset * VPU_FW_MEM_OFFSET + OFFSET_BASE;
1390     else if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType)
1391         u32Arg = u8Offset * VPU_FW_MEM_OFFSET + HVD_SHARE_MEM_ST_OFFSET;
1392     else
1393     {
1394         VPU_MSG_ERR("Can't find eDecType! %d\n", pTaskInfo->eDecType);
1395         _HAL_VPU_Release();
1396         return FALSE;
1397     }
1398 #else
1399     u32Arg = u8Offset * VPU_FW_MEM_OFFSET;
1400 #endif
1401 
1402     HAL_HVD_EX_SetCmd(u32Id, eCmd, u32Arg);
1403 
1404     MsOS_ReadMemory();
1405     VPU_MSG_INFO("before: version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1406          ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1407 
1408     u32Timeout = HVD_GetSysTime_ms() + VPU_CMD_TIMEOUT;
1409     while (CTL_TASK_CMDRDY != ctl_ptr->task_statue[u8Offset])
1410     {
1411         if (HVD_GetSysTime_ms() > u32Timeout)
1412         {
1413             VPU_MSG_ERR("Task %d creation timeout\n", u8Offset);
1414             MS_U32 t=0;
1415             for (t=0; t<30; t++)
1416             {
1417                 VPU_MSG_DBG("_pc=0x%lx\n", HAL_VPU_EX_GetProgCnt());
1418             }
1419 
1420             pVPUHalContext->bVpuExLoadFWRlt = FALSE; ///error handling
1421             VPU_MSG_ERR("set bVpuExLoadFWRlt as FALSE\n\n");
1422             _HAL_VPU_Release();
1423             return FALSE;
1424         }
1425 
1426         MsOS_ReadMemory();
1427     }
1428 
1429     VPU_MSG_INFO("after: version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1430          ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1431 
1432 
1433     if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType)
1434     {
1435         HAL_HVD_EX_SetBufferAddr(u32Id);
1436     }
1437 
1438     if (E_VPU_EX_DECODER_MVD == pTaskInfo->eDecType)
1439     {
1440         eDecType = E_VPU_EX_DECODER_MVD;
1441     }
1442     else if (E_VPU_EX_DECODER_HVD == pTaskInfo->eDecType)
1443     {
1444         eDecType = E_VPU_EX_DECODER_HVD;
1445     }
1446     else
1447     {
1448         VPU_MSG_ERR("Can't find eDecType! %d\n", pTaskInfo->eDecType);
1449         _HAL_VPU_Release();
1450         return FALSE;
1451     }
1452 
1453     if (pTaskInfo->eDecType != eDecType)
1454     {
1455         VPU_MSG_ERR("warning pTaskInfo->eDecType=%x not %x\n",
1456             pTaskInfo->eDecType, eDecType);
1457     }
1458     goto _SAVE_DEC_TYPE;
1459 
1460 _SAVE_DEC_TYPE:
1461     if (pVPUHalContext->_stVPUStream[u8Offset].eStreamId == (u32Id & 0xFF))
1462     {
1463         pVPUHalContext->_stVPUStream[u8Offset].eDecodertype = eDecType;
1464     }
1465     else
1466     {
1467         VPU_MSG_ERR("Cannot save eDecType!!\n");
1468     }
1469 
1470     (pVPUHalContext->u8TaskCnt)++;
1471     _HAL_VPU_Release();
1472     return TRUE;
1473 }
1474 
HAL_VPU_EX_TaskDelete(MS_U32 u32Id,VPU_EX_NDecInitPara * pInitPara)1475 MS_BOOL HAL_VPU_EX_TaskDelete(MS_U32 u32Id, VPU_EX_NDecInitPara *pInitPara)
1476 {
1477     HVD_Return eRet;
1478     HVD_User_Cmd eCmd = E_DUAL_CMD_DEL_TASK;
1479     MS_U8 u8OffsetIdx = _VPU_EX_GetOffsetIdx(u32Id);
1480     MS_U32 u32Timeout       = HVD_GetSysTime_ms() + 3000;
1481 
1482     _HAL_VPU_Entry();
1483     VPU_MSG_DBG("DecType=%d\n", pVPUHalContext->_stVPUStream[u8OffsetIdx].eDecodertype);
1484 
1485     eRet = HAL_HVD_EX_SetCmd(u32Id, eCmd, u8OffsetIdx);
1486     if(eRet != E_HVD_RETURN_SUCCESS)
1487     {
1488         VPU_MSG_ERR("VPU fail to DEL Task %d\n", eRet);
1489     }
1490 
1491     {
1492         struct _ctl_info *ctl_ptr = (struct _ctl_info *)
1493             MsOS_PA2KSEG1(MsOS_VA2PA(pInitPara->pFWCodeCfg->u32DstAddr) + CTL_INFO_ADDR);
1494         u32Timeout = HVD_GetSysTime_ms() + VPU_CMD_TIMEOUT;
1495 
1496         MsOS_ReadMemory();
1497 
1498         VPU_MSG_DBG("before: version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1499             ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1500 
1501         while (CTL_TASK_NONE != ctl_ptr->task_statue[u8OffsetIdx])
1502         {
1503             if (HVD_GetSysTime_ms() > u32Timeout)
1504             {
1505                 VPU_MSG_ERR("Task %u deletion timeout\n", u8OffsetIdx);
1506                 pVPUHalContext->bVpuExLoadFWRlt = FALSE; ///error handling
1507                 VPU_MSG_ERR("Set bVpuExLoadFWRlt as FALSE\n");
1508 
1509                 if(pVPUHalContext->u8TaskCnt == 1)
1510                 {
1511                     VPU_MSG_ERR("Due to one task remain, driver can force delete task\n");
1512                     break;
1513                 }
1514                 else if(pVPUHalContext->u8TaskCnt == 2)
1515                 {
1516                     VPU_MSG_ERR("Due to two tasks remain, driver can't force delete task\n");
1517                     _HAL_VPU_Release();
1518                     return FALSE;
1519                 }
1520                 else
1521                 {
1522                     VPU_MSG_ERR("Task number is not correct\n");
1523                     _HAL_VPU_Release();
1524                     return FALSE;
1525                 }
1526             }
1527 
1528             MsOS_ReadMemory();
1529         }
1530 
1531         VPU_MSG_DBG("after: version=0x%x, statue=0x%x, last_ctl_cmd=0x%x, last_ctl_arg=0x%x, t0=%d, t1=%d\n",
1532             ctl_ptr->verion, ctl_ptr->statue, ctl_ptr->last_ctl_cmd, ctl_ptr->last_ctl_arg, ctl_ptr->task_statue[0], ctl_ptr->task_statue[1]);
1533     }
1534 
1535     pVPUHalContext->_stVPUStream[u8OffsetIdx].eDecodertype = E_VPU_EX_DECODER_NONE;
1536     if( (u8OffsetIdx == 0) && (pVPUHalContext->_stVPUStream[u8OffsetIdx].eStreamId == E_HAL_VPU_MVC_MAIN_VIEW))
1537     {
1538         pVPUHalContext->_stVPUStream[u8OffsetIdx].eStreamId = E_HAL_VPU_MAIN_STREAM0;
1539     }
1540 
1541     if (pVPUHalContext->u8TaskCnt)
1542     {
1543         (pVPUHalContext->u8TaskCnt)--;
1544     }
1545     else
1546     {
1547         VPU_MSG_DBG("Warning: u8TaskCnt=0\n");
1548     }
1549 
1550     if (0 == pVPUHalContext->u8TaskCnt)
1551     {
1552         VPU_MSG_DBG("u8TaskCnt=%d time to terminate\n", pVPUHalContext->u8TaskCnt);
1553         _VPU_EX_DeinitAll();
1554         HAL_VPU_EX_SetSingleDecodeMode(FALSE);
1555         pVPUHalContext->u32VPUSHMAddr = 0;
1556         pVPUHalContext->u32FWShareInfoAddr[0] = 0xFFFFFFFF;
1557         pVPUHalContext->u32FWShareInfoAddr[1] = 0xFFFFFFFF;
1558         pVPUHalContext->u32FWShareInfoAddr[2] = 0xFFFFFFFF;
1559         pVPUHalContext->u32FWShareInfoAddr[3] = 0xFFFFFFFF;
1560     }
1561     else
1562     {
1563         pVPUHalContext->u32FWShareInfoAddr[u8OffsetIdx] = 0xFFFFFFFF;
1564         _VPU_EX_DeinitHW();
1565     }
1566 
1567     _HAL_VPU_Release();
1568     return TRUE;
1569 }
1570 
HAL_VPU_EX_LoadCode(VPU_EX_FWCodeCfg * pFWCodeCfg)1571 MS_BOOL HAL_VPU_EX_LoadCode(VPU_EX_FWCodeCfg *pFWCodeCfg)
1572 {
1573     MS_U32 u32DestAddr  = pFWCodeCfg->u32DstAddr;
1574     MS_U32 u32BinAddr   = pFWCodeCfg->u32BinAddr;
1575     MS_U32 u32Size      = pFWCodeCfg->u32BinSize;
1576 #if (ENABLE_DECOMPRESS_FUNCTION==TRUE)
1577     MS_U32 u32DestSize  = pFWCodeCfg->u32DstSize;
1578 #endif
1579 
1580     if (FALSE == HAL_VPU_EX_GetFWReload())
1581     {
1582         //VPU_PRINT("%s bFWReload FALSE!!!\n", __FUNCTION__);
1583         if (FALSE == pVPUHalContext->bVpuExLoadFWRlt)
1584         {
1585             VPU_MSG_INFO("Never load fw successfully, load it anyway!\n");
1586         }
1587         else
1588         {
1589             //Check f/w prefix "VDEC20"
1590             if (_VPU_EX_IsNeedDecompress(u32DestAddr)!=FALSE)
1591             {
1592                 VPU_MSG_ERR("Wrong prefix: reload fw!\n");
1593             }
1594             else
1595             {
1596                 VPU_MSG_INFO("Skip loading fw this time!!!\n");
1597                 return TRUE;
1598             }
1599         }
1600     }
1601 
1602     if (E_HVD_FW_INPUT_SOURCE_FLASH == pFWCodeCfg->u8SrcType)
1603     {
1604 #if VPU_ENABLE_BDMA_FW_FLASH_2_SDRAM
1605         if (u32Size != 0)
1606         {
1607             SPIDMA_Dev cpyflag = E_SPIDMA_DEV_MIU1;
1608 
1609             if (HAL_MIU1_BASE <= MsOS_VA2PA(u32DestAddr))
1610             {
1611                 cpyflag = E_SPIDMA_DEV_MIU1;
1612             }
1613             else
1614             {
1615                 cpyflag = E_SPIDMA_DEV_MIU0;
1616             }
1617 
1618             if (!HVD_FLASHcpy(MsOS_VA2PA(u32DestAddr), MsOS_VA2PA(u32BinAddr), u32Size, cpyflag))
1619             {
1620                 goto _load_code_fail;
1621             }
1622         }
1623         else
1624         {
1625             goto _load_code_fail;
1626         }
1627 #else
1628         goto _load_code_fail;
1629 #endif
1630     }
1631     else if (E_HVD_FW_INPUT_SOURCE_DRAM == pFWCodeCfg->u8SrcType)
1632     {
1633         if (u32BinAddr != 0 && u32Size != 0)
1634         {
1635 #if (ENABLE_DECOMPRESS_FUNCTION==TRUE)
1636             if(_VPU_EX_DecompressBin(u32BinAddr, u32Size, u32DestAddr, u32DestAddr+u32DestSize-WINDOW_SIZE)==TRUE)
1637             {
1638                 if(_VPU_EX_IsNeedDecompress(u32DestAddr)==FALSE)
1639                 {
1640                     VPU_MSG_INFO("Decompress ok!!!\n");
1641                 }
1642                 else
1643                 {
1644                     VPU_MSG_INFO("Decompress fail!!!\n");
1645                 }
1646             }
1647             else
1648 #endif
1649             {
1650                 HVD_memcpy(u32DestAddr, u32BinAddr, u32Size);
1651             }
1652         }
1653         else
1654         {
1655             goto _load_code_fail;
1656         }
1657     }
1658     else
1659     {
1660 #if VPU_ENABLE_EMBEDDED_FW_BINARY
1661         VPU_MSG_INFO("Load FW inD2D: dest=0x%lx, source=0x%lx, size=%ld\n",
1662                     u32DestAddr, ((MS_U32) u8HVD_FW_Binary),
1663                     (MS_U32) sizeof(u8HVD_FW_Binary));
1664 
1665 #if (ENABLE_DECOMPRESS_FUNCTION==TRUE)
1666         if(_VPU_EX_DecompressBin((MS_U32)u8HVD_FW_Binary, (MS_U32)sizeof(u8HVD_FW_Binary), u32DestAddr, u32DestAddr+u32DestSize-WINDOW_SIZE)==TRUE)
1667         {
1668             if(_VPU_EX_IsNeedDecompress(u32DestAddr)==FALSE)
1669             {
1670                 VPU_MSG_INFO("Decompress ok!!!\n");
1671             }
1672             else
1673             {
1674                 VPU_MSG_INFO("Decompress fail!!!\n");
1675             }
1676         }
1677         else
1678 #endif
1679         {
1680             HVD_memcpy(u32DestAddr, (MS_U32)u8HVD_FW_Binary, sizeof(u8HVD_FW_Binary));
1681         }
1682 #else
1683         goto _load_code_fail;
1684 #endif
1685     }
1686 
1687     MAsm_CPU_Sync();
1688     MsOS_FlushMemory();
1689 
1690     if (FALSE == (*((MS_U8*)(u32DestAddr+6))=='R' && *((MS_U8*)(u32DestAddr+7))=='2'))
1691     {
1692         VPU_MSG_ERR("FW is not R2 version! _%x_ _%x_\n", *(MS_U8*)(u32DestAddr+6), *(MS_U8*)(u32DestAddr+7));
1693         goto _load_code_fail;
1694     }
1695 
1696     pVPUHalContext->bVpuExLoadFWRlt = TRUE;
1697     return TRUE;
1698 
1699 _load_code_fail:
1700     pVPUHalContext->bVpuExLoadFWRlt = FALSE;
1701     return FALSE;
1702 }
1703 
HAL_VPU_EX_InitRegBase(MS_U32 u32RegBase)1704 void HAL_VPU_EX_InitRegBase(MS_U32 u32RegBase)
1705 {
1706     u32VPURegOSBase = u32RegBase;
1707 }
1708 
HAL_VPU_EX_Init_Share_Mem(void)1709 MS_BOOL HAL_VPU_EX_Init_Share_Mem(void)
1710 {
1711 #if ((defined(MSOS_TYPE_LINUX) || defined(MSOS_TYPE_ECOS)) && (!defined(SUPPORT_X_MODEL_FEATURE)))
1712 
1713     MS_U32 u32ShmId;
1714     MS_U32 u32Addr;
1715     MS_U32 u32BufSize;
1716 
1717 
1718     if (FALSE == MsOS_SHM_GetId( (MS_U8*)"Linux HAL VPU",
1719                                           sizeof(VPU_Hal_CTX),
1720                                           &u32ShmId,
1721                                           &u32Addr,
1722                                           &u32BufSize,
1723                                           MSOS_SHM_QUERY))
1724     {
1725         if (FALSE == MsOS_SHM_GetId((MS_U8*)"Linux HAL VPU",
1726                                              sizeof(VPU_Hal_CTX),
1727                                              &u32ShmId,
1728                                              &u32Addr,
1729                                              &u32BufSize,
1730                                              MSOS_SHM_CREATE))
1731         {
1732             VPU_MSG_ERR("[%s]SHM allocation failed!!!use global structure instead!!!\n",__FUNCTION__);
1733             if(pVPUHalContext == NULL)
1734             {
1735                 pVPUHalContext = &gVPUHalContext;
1736                 memset(pVPUHalContext,0,sizeof(VPU_Hal_CTX));
1737                 _VPU_EX_Context_Init();
1738                 VPU_PRINT("[%s]Global structure init Success!!!\n",__FUNCTION__);
1739             }
1740             else
1741             {
1742                 VPU_PRINT("[%s]Global structure exists!!!\n",__FUNCTION__);
1743             }
1744             //return FALSE;
1745         }
1746         else
1747         {
1748             memset((MS_U8*)u32Addr,0,sizeof(VPU_Hal_CTX));
1749             pVPUHalContext = (VPU_Hal_CTX*)u32Addr; // for one process
1750             _VPU_EX_Context_Init();
1751         }
1752     }
1753     else
1754     {
1755         pVPUHalContext = (VPU_Hal_CTX*)u32Addr; // for another process
1756     }
1757 #else
1758     if(pVPUHalContext == NULL)
1759     {
1760         pVPUHalContext = &gVPUHalContext;
1761         memset(pVPUHalContext,0,sizeof(VPU_Hal_CTX));
1762         _VPU_EX_Context_Init();
1763     }
1764 #endif
1765 
1766     return TRUE;
1767 
1768 }
1769 
HAL_VPU_EX_GetFreeStream(HAL_VPU_StreamType eStreamType)1770 HAL_VPU_StreamId HAL_VPU_EX_GetFreeStream(HAL_VPU_StreamType eStreamType)
1771 {
1772     MS_U32 i = 0;
1773 
1774     if(eStreamType == E_HAL_VPU_SUB_STREAM)
1775     {
1776         return E_HAL_VPU_STREAM_NONE;
1777     }
1778 
1779     _HAL_VPU_MutexCreate();
1780 
1781     if (E_HAL_VPU_MVC_STREAM == eStreamType)
1782     {
1783         if((E_VPU_EX_DECODER_NONE == pVPUHalContext->_stVPUStream[0].eDecodertype) && (E_VPU_EX_DECODER_NONE == pVPUHalContext->_stVPUStream[1].eDecodertype))
1784         {
1785             pVPUHalContext->_stVPUStream[0].eStreamId = E_HAL_VPU_MVC_MAIN_VIEW;
1786             return pVPUHalContext->_stVPUStream[0].eStreamId;       /// Need to check
1787         }
1788     }
1789     else if (E_HAL_VPU_MAIN_STREAM == eStreamType)
1790     {
1791         for (i = 0;i < MAX_SUPPORT_DECODER_NUM; i++)
1792         {
1793             if ((E_HAL_VPU_MAIN_STREAM_BASE & pVPUHalContext->_stVPUStream[i].eStreamId)
1794                 && (E_VPU_EX_DECODER_NONE == pVPUHalContext->_stVPUStream[i].eDecodertype))
1795             {
1796                 return pVPUHalContext->_stVPUStream[i].eStreamId;
1797             }
1798         }
1799     }
1800     else if (E_HAL_VPU_SUB_STREAM == eStreamType)
1801     {
1802         for (i = 0;i < MAX_SUPPORT_DECODER_NUM; i++)
1803         {
1804             if ((E_HAL_VPU_SUB_STREAM_BASE & pVPUHalContext->_stVPUStream[i].eStreamId)
1805                 && (E_VPU_EX_DECODER_NONE == pVPUHalContext->_stVPUStream[i].eDecodertype))
1806             {
1807                 return pVPUHalContext->_stVPUStream[i].eStreamId;
1808             }
1809         }
1810     }
1811 
1812     return E_HAL_VPU_STREAM_NONE;
1813 }
1814 
HAL_VPU_EX_Init(VPU_EX_InitParam * InitParams)1815 MS_BOOL HAL_VPU_EX_Init(VPU_EX_InitParam *InitParams)
1816 {
1817     VPU_MSG_DBG("Inv=%d, clk=%d\n", InitParams->bClockInv, InitParams->eClockSpeed);
1818 
1819     // enable module
1820     _VPU_EX_ClockInv(InitParams->bClockInv);
1821     _VPU_EX_ClockSpeed(InitParams->eClockSpeed);
1822     HAL_VPU_EX_PowerCtrl(TRUE);
1823 
1824 #if 1                           //Create VPU's own mutex
1825     //_HAL_VPU_MutexCreate();
1826 #else
1827     pVPUHalContext->s32VPUMutexID = InitParams->s32VPUMutexID;
1828     pVPUHalContext->u32VPUMutexTimeOut = InitParams->u32VPUMutexTimeout;
1829 #endif
1830 
1831     return TRUE;
1832 }
1833 
HAL_VPU_EX_DeInit(void)1834 MS_BOOL HAL_VPU_EX_DeInit(void)
1835 {
1836     if (0 != _VPU_EX_GetActiveCodecCnt())
1837     {
1838         VPU_MSG_DBG("do nothing since codec is active.\n");
1839         return TRUE;
1840     }
1841 
1842     memset(&(pVPUHalContext->_stVPUDecMode),0,sizeof(VPU_EX_DecModCfg));
1843 
1844     HAL_VPU_EX_PowerCtrl(FALSE);
1845     HAL_VPU_EX_SwRelseMAU();
1846     //_HAL_VPU_MutexDelete();
1847 
1848     return TRUE;
1849 }
1850 
HAL_VPU_EX_PowerCtrl(MS_BOOL bEnable)1851 void HAL_VPU_EX_PowerCtrl(MS_BOOL bEnable)
1852 {
1853     if (bEnable)
1854     {
1855         _VPU_WriteWordMask(REG_TOP_VPU, 0, TOP_CKG_VPU_DIS);
1856         pVPUHalContext->_bVPUPowered = TRUE;
1857     }
1858     else
1859     {
1860         _VPU_WriteWordMask(REG_TOP_VPU, TOP_CKG_VPU_DIS, TOP_CKG_VPU_DIS);
1861         pVPUHalContext->_bVPUPowered = FALSE;
1862     }
1863 }
1864 
HAL_VPU_EX_MIU_RW_Protect(MS_BOOL bEnable)1865 void HAL_VPU_EX_MIU_RW_Protect(MS_BOOL bEnable)
1866 {
1867     _VPU_MIU_SetReqMask(VPU_D_RW, bEnable);
1868     _VPU_MIU_SetReqMask(VPU_Q_RW, bEnable);
1869     _VPU_MIU_SetReqMask(VPU_I_R, bEnable);
1870     VPU_EX_TimerDelayMS(1);
1871 }
1872 
1873 ///-----------------------------------------------------------------------------
1874 /// config AVCH264 CPU
1875 /// @param u32StAddr \b IN: CPU binary code base address in DRAM.
1876 /// @param u8dlend_en \b IN: endian
1877 ///     - 1, little endian
1878 ///     - 0, big endian
1879 ///-----------------------------------------------------------------------------
HAL_VPU_EX_CPUSetting(MS_U32 u32StAddr)1880 MS_BOOL HAL_VPU_EX_CPUSetting(MS_U32 u32StAddr)
1881 {
1882     MS_BOOL bRet = TRUE;
1883     MS_U32 u32Offset = 0;
1884     MS_U16 tempreg = 0;
1885 
1886     u32Offset = (u32StAddr >= HAL_MIU1_BASE) ? (u32StAddr-HAL_MIU1_BASE) : u32StAddr ;
1887     _VPU_Write2Byte(VPU_REG_SPI_BASE,  0xC000);
1888     _VPU_WriteWordMask( VPU_REG_CPU_SETTING , 0 , VPU_REG_CPU_SPI_BOOT );
1889     _VPU_WriteWordMask( VPU_REG_CPU_SETTING , 0 , VPU_REG_CPU_SDRAM_BOOT );
1890     _VPU_Write2Byte(VPU_REG_DQMEM_MASK_L,  0xc000);
1891     _VPU_Write2Byte(VPU_REG_DQMEM_MASK_H,  0xffff);
1892     _VPU_Write2Byte(VPU_REG_IO2_BASE,  0x8000);
1893     _VPU_Write2Byte(VPU_REG_DQMEM_BASE_L,  0x0000);
1894     _VPU_Write2Byte(VPU_REG_DQMEM_BASE_H, 0x2000);
1895 
1896 
1897 
1898 
1899 
1900 
1901     #if (VPU_ENABLE_MOBF_TEST)
1902     MS_U32 u32BSAddr = 0x61C00000;
1903     MS_U32 u32BSSize = 0x400000;
1904     MS_U8 u8MobfRidx=0x0;
1905     MS_U8 u8MobfWidx=0x1;
1906     MS_U32 u32BsVpuAddr;
1907 
1908     if(u32BSAddr >= HAL_MIU1_BASE)
1909     {
1910         u32BsVpuAddr= (u32BSAddr - HAL_MIU1_BASE) + VPU_MIU1BASE_ADDR;
1911     }
1912     else
1913     {
1914         u32BsVpuAddr= u32BSAddr;
1915     }
1916     MS_U32 u32BSStartDiv8 = (u32BsVpuAddr >> 3)&0x1fffffff;
1917     MS_U32 u32BSEndDiv8 = ((u32BsVpuAddr+u32BSSize) >> 3)&0x1fffffff;
1918 
1919     //set MOBF region 0 to bistream buffer
1920     _VPU_Write2Byte(MAU1_REG_REGION_MASK0_L, (MS_U16)0xffff);
1921     _VPU_Write2Byte(MAU1_REG_REGION_MASK0_H, (MS_U16)0xffff);
1922     _VPU_Write2Byte(MAU1_REG_REGION_START0_L, (MS_U16)(u32BSStartDiv8 & 0xffff));
1923     _VPU_Write2Byte(MAU1_REG_REGION_START0_H, (MS_U16)((u32BSStartDiv8 >> 16) & 0xffff));
1924     _VPU_Write2Byte(MAU1_REG_REGION_END0_L, (MS_U16)(u32BSEndDiv8 & 0xffff));
1925     _VPU_Write2Byte(MAU1_REG_REGION_END0_H, (MS_U16)((u32BSEndDiv8 >> 16) & 0xffff));
1926     _VPU_Write2Byte(MAU1_REG_REGION_EN, (MS_U16)0x0001);
1927 
1928 
1929     _VPU_Write2Byte(MAU1_REG_MIU_RW_TAG1, (((MS_U16)u8MobfWidx)<<8)|((MS_U16)u8MobfRidx));
1930     #endif
1931 
1932     if(u32StAddr >= HAL_MIU1_BASE)
1933     {
1934         // Data sram base Unit: byte address
1935         _VPU_Write2Byte(VPU_REG_DCU_SDR_BASE_L, (MS_U16)(u32Offset  & 0x0000ffff)) ;
1936         _VPU_Write2Byte(VPU_REG_DCU_SDR_BASE_H, (MS_U16)((u32Offset >>16) & 0xffff));
1937         // Instruction sram base Unit: byte address
1938         _VPU_Write2Byte(VPU_REG_ICU_SDR_BASE_L, (MS_U16)(u32Offset & 0x0000ffff)) ;
1939         _VPU_Write2Byte(VPU_REG_ICU_SDR_BASE_H, (MS_U16)((u32Offset >>16) & 0xffff));
1940 
1941         _VPU_Write2Byte(MAU1_MIU_SEL, 0x8b00);
1942     }
1943     else
1944     {
1945         // Data sram base Unit: byte address
1946         _VPU_Write2Byte(VPU_REG_DCU_SDR_BASE_L, (MS_U16)(u32Offset & 0x0000ffff)) ;
1947         _VPU_Write2Byte(VPU_REG_DCU_SDR_BASE_H, (MS_U16)((u32Offset>>16) & 0xffff));
1948         // Instruction sram base Unit: byte address
1949         _VPU_Write2Byte(VPU_REG_ICU_SDR_BASE_L, (MS_U16)(u32Offset & 0x0000ffff)) ;
1950         _VPU_Write2Byte(VPU_REG_ICU_SDR_BASE_H, (MS_U16)((u32Offset>>16) & 0xffff));
1951 
1952         _VPU_Write2Byte(MAU1_MIU_SEL, 0x8900);
1953     }
1954 
1955     tempreg = _VPU_Read2Byte(VPU_REG_CONTROL_SET);
1956     tempreg |= VPU_REG_IO2_EN;
1957     tempreg |= VPU_REG_QMEM_SPACE_EN;
1958     _VPU_Write2Byte(VPU_REG_CONTROL_SET, tempreg);
1959 
1960     return bRet;
1961 }
1962 
1963 ///-----------------------------------------------------------------------------
1964 /// Set IQMem data access mode or instruction fetch mode
1965 /// @param u8dlend_en \b IN: endian
1966 ///     - 1, switch to data access mode
1967 ///     - 0, switch to instruction fetch mode
1968 ///-----------------------------------------------------------------------------
HAL_VPU_EX_IQMemSetDAMode(MS_BOOL bEnable)1969 void HAL_VPU_EX_IQMemSetDAMode(MS_BOOL bEnable)
1970 {
1971 
1972     if(bEnable){
1973 
1974         _VPU_Write2Byte(VPU_REG_IQMEM_SETTING, _VPU_Read2Byte(VPU_REG_IQMEM_SETTING)|0x10);
1975         _VPU_Write2Byte(VPU_REG_QMEM_OWNER, _VPU_Read2Byte(VPU_REG_QMEM_OWNER)&0xFFDE);
1976 
1977     }
1978     else{
1979 
1980         _VPU_Write2Byte(VPU_REG_IQMEM_SETTING, _VPU_Read2Byte(VPU_REG_IQMEM_SETTING)& 0xFFEF);
1981 
1982     }
1983 }
1984 
1985 ///-----------------------------------------------------------------------------
1986 /// H.264 SW reset
1987 /// @return TRUE or FALSE
1988 ///     - TRUE, Success
1989 ///     - FALSE, Failed
1990 ///-----------------------------------------------------------------------------
HAL_VPU_EX_SwRst(MS_BOOL bCheckMauIdle)1991 MS_BOOL HAL_VPU_EX_SwRst(MS_BOOL bCheckMauIdle)
1992 {
1993     MS_U16 tempreg = 0, tempreg1 = 0;
1994 
1995     //From T4, need to check MAU idle before reset VPU
1996     if (bCheckMauIdle)
1997     {
1998         MS_U32 mau_idle_cnt = 100;// ms
1999         while (mau_idle_cnt)
2000         {
2001             if (TRUE == _VPU_EX_MAU_IDLE())
2002             {
2003                 break;
2004             }
2005             mau_idle_cnt--;
2006             MsOS_DelayTask(1);
2007         }
2008 
2009         if (mau_idle_cnt == 0)
2010         {
2011             VPU_PRINT("MAU idle time out~~~~~\n");
2012         }
2013     }
2014 
2015 
2016     HAL_VPU_EX_MIU_RW_Protect(TRUE);
2017 
2018     tempreg1 = _VPU_Read2Byte(MAU1_CPU_RST);
2019     tempreg1 |= MAU1_REG_SW_RESET;
2020     _VPU_Write2Byte(MAU1_CPU_RST, tempreg1);
2021 
2022 #if defined(UDMA_FPGA_ENVI)
2023     tempreg = _VPU_Read2Byte(VPU_REG_RESET);
2024    _VPU_Write2Byte(VPU_REG_RESET, (tempreg& 0xfffd));
2025 #endif
2026 
2027     tempreg = _VPU_Read2Byte(VPU_REG_CPU_SETTING);
2028     tempreg &= ~VPU_REG_CPU_R2_EN;
2029     tempreg &= ~VPU_REG_CPU_SW_RSTZ;
2030     tempreg &= ~VPU_REG_CPU_MIU_SW_RSTZ;
2031     _VPU_Write2Byte(VPU_REG_CPU_SETTING, tempreg);
2032 
2033     VPU_EX_TimerDelayMS(1);
2034     HAL_VPU_EX_MIU_RW_Protect(FALSE);
2035 
2036     pVPUHalContext->_bVPURsted = FALSE;
2037     return TRUE;
2038 }
2039 
2040 ///-----------------------------------------------------------------------------
2041 /// CPU reset release
2042 ///-----------------------------------------------------------------------------
HAL_VPU_EX_SwRstRelse(void)2043 void HAL_VPU_EX_SwRstRelse(void)
2044 {
2045     MS_U16 tempreg = 0, tempreg1 = 0;
2046 
2047     tempreg = _VPU_Read2Byte(VPU_REG_CPU_SETTING);
2048     tempreg |= VPU_REG_CPU_R2_EN;
2049     tempreg |= VPU_REG_CPU_SW_RSTZ;
2050     tempreg |= VPU_REG_CPU_MIU_SW_RSTZ;
2051     _VPU_Write2Byte(VPU_REG_CPU_SETTING, tempreg);
2052 
2053     tempreg1 = _VPU_Read2Byte(MAU1_CPU_RST);
2054     tempreg1 &= ~MAU1_REG_SW_RESET;
2055     _VPU_Write2Byte(MAU1_CPU_RST, tempreg1);
2056 
2057     pVPUHalContext->_bVPURsted = TRUE;
2058 }
2059 
HAL_VPU_EX_SwRelseMAU(void)2060 void HAL_VPU_EX_SwRelseMAU(void)
2061 {
2062     MS_U16 tempreg = 0;
2063 
2064     tempreg = _VPU_Read2Byte(MAU1_CPU_RST);
2065     tempreg &= ~MAU1_REG_SW_RESET;
2066     _VPU_Write2Byte(MAU1_CPU_RST, tempreg);
2067 }
2068 
HAL_VPU_EX_MemRead(MS_U32 u32Addr)2069 MS_U32 HAL_VPU_EX_MemRead(MS_U32 u32Addr)
2070 {
2071     MS_U32 u32value = 0;
2072 
2073     return u32value;
2074 }
2075 
HAL_VPU_EX_MemWrite(MS_U32 u32Addr,MS_U32 u32value)2076 MS_BOOL HAL_VPU_EX_MemWrite(MS_U32 u32Addr, MS_U32 u32value)
2077 {
2078     MS_BOOL bRet = TRUE;
2079 
2080     return bRet;
2081 }
2082 
2083 ///-----------------------------------------------------------------------------
2084 /// Check AVCH264 Ready or not
2085 /// @return TRUE or FALSE
2086 ///     - TRUE, MailBox is free
2087 ///     - FALSE, MailBox is busy
2088 /// @param u8MBox \b IN: MailBox to check
2089 ///     - AVCH264_HI_MBOX0,
2090 ///     - AVCH264_HI_MBOX1,
2091 ///     - AVCH264_RISC_MBOX0,
2092 ///     - AVCH264_RISC_MBOX1,
2093 ///-----------------------------------------------------------------------------
HAL_VPU_EX_MBoxRdy(MS_U32 u32type)2094 MS_BOOL HAL_VPU_EX_MBoxRdy(MS_U32 u32type)
2095 {
2096     MS_BOOL bResult = FALSE;
2097 
2098     switch (u32type)
2099     {
2100         case VPU_HI_MBOX0:
2101             bResult = (_VPU_Read2Byte(VPU_REG_HI_MBOX_RDY) & VPU_REG_HI_MBOX0_RDY) ? FALSE : TRUE;
2102             break;
2103         case VPU_HI_MBOX1:
2104             bResult = (_VPU_Read2Byte(VPU_REG_HI_MBOX_RDY) & VPU_REG_HI_MBOX1_RDY) ? FALSE : TRUE;
2105             break;
2106         case VPU_RISC_MBOX0:
2107             bResult = (_VPU_Read2Byte(VPU_REG_RISC_MBOX_RDY) & VPU_REG_RISC_MBOX0_RDY) ? TRUE : FALSE;
2108             break;
2109         case VPU_RISC_MBOX1:
2110             bResult = (_VPU_Read2Byte(VPU_REG_RISC_MBOX_RDY) & VPU_REG_RISC_MBOX1_RDY) ? TRUE : FALSE;
2111             break;
2112         default:
2113             break;
2114     }
2115     return bResult;
2116 }
2117 
2118 
2119 ///-----------------------------------------------------------------------------
2120 /// Read message from AVCH264
2121 /// @return TRUE or FALSE
2122 ///     - TRUE, success
2123 ///     - FALSE, failed
2124 /// @param u8MBox \b IN: MailBox to read
2125 ///     - AVCH264_RISC_MBOX0
2126 ///     - AVCH264_RISC_MBOX1
2127 /// @param u32Msg \b OUT: message read
2128 ///-----------------------------------------------------------------------------
HAL_VPU_EX_MBoxRead(MS_U32 u32type,MS_U32 * u32Msg)2129 MS_BOOL HAL_VPU_EX_MBoxRead(MS_U32 u32type, MS_U32 * u32Msg)
2130 {
2131     MS_BOOL bResult = TRUE;
2132 
2133     switch (u32type)
2134     {
2135         case VPU_HI_MBOX0:
2136             *u32Msg = ((MS_U32) (_VPU_Read2Byte(VPU_REG_HI_MBOX0_H)) << 16) |
2137                 ((MS_U32) (_VPU_Read2Byte(VPU_REG_HI_MBOX0_L)));
2138             break;
2139         case VPU_HI_MBOX1:
2140             *u32Msg = ((MS_U32) (_VPU_Read2Byte(VPU_REG_HI_MBOX1_H)) << 16) |
2141                 ((MS_U32) (_VPU_Read2Byte(VPU_REG_HI_MBOX1_L)));
2142             break;
2143         case VPU_RISC_MBOX0:
2144             *u32Msg = ((MS_U32) (_VPU_Read2Byte(VPU_REG_RISC_MBOX0_H)) << 16) |
2145                 ((MS_U32) (_VPU_Read2Byte(VPU_REG_RISC_MBOX0_L)));
2146             break;
2147         case VPU_RISC_MBOX1:
2148             *u32Msg = ((MS_U32) (_VPU_Read2Byte(VPU_REG_RISC_MBOX1_H)) << 16) |
2149                 ((MS_U32) (_VPU_Read2Byte(VPU_REG_RISC_MBOX1_L)));
2150             break;
2151         default:
2152             *u32Msg = 0;
2153             bResult = FALSE;
2154             break;
2155     }
2156     return bResult;
2157 }
2158 
2159 ///-----------------------------------------------------------------------------
2160 /// Mailbox from AVCH264 clear bit resest
2161 ///-----------------------------------------------------------------------------
HAL_VPU_EX_MBoxClear(MS_U32 u32type)2162 void HAL_VPU_EX_MBoxClear(MS_U32 u32type)
2163 {
2164     switch (u32type)
2165     {
2166         case VPU_RISC_MBOX0:
2167             _VPU_WriteWordMask(VPU_REG_RISC_MBOX_CLR, VPU_REG_RISC_MBOX0_CLR, VPU_REG_RISC_MBOX0_CLR);
2168             break;
2169         case VPU_RISC_MBOX1:
2170             _VPU_WriteWordMask(VPU_REG_RISC_MBOX_CLR, VPU_REG_RISC_MBOX1_CLR, VPU_REG_RISC_MBOX1_CLR);
2171             break;
2172         default:
2173             break;
2174     }
2175 }
2176 
2177 ///-----------------------------------------------------------------------------
2178 /// Send message to AVCH264
2179 /// @return TRUE or FALSE
2180 ///     - TRUE, Success
2181 ///     - FALSE, Failed
2182 /// @param u8MBox \b IN: MailBox
2183 ///     - AVCH264_HI_MBOX0,
2184 ///     - AVCH264_HI_MBOX1,
2185 ///-----------------------------------------------------------------------------
HAL_VPU_EX_MBoxSend(MS_U32 u32type,MS_U32 u32Msg)2186 MS_BOOL HAL_VPU_EX_MBoxSend(MS_U32 u32type, MS_U32 u32Msg)
2187 {
2188     MS_BOOL bResult = TRUE;
2189 
2190     VPU_MSG_DBG("type=%lu, msg=0x%lx\n", u32type, u32Msg);
2191 
2192     switch (u32type)
2193     {
2194         case VPU_HI_MBOX0:
2195         {
2196             _VPU_Write4Byte(VPU_REG_HI_MBOX0_L, u32Msg);
2197             _VPU_WriteWordMask(VPU_REG_HI_MBOX_SET, VPU_REG_HI_MBOX0_SET, VPU_REG_HI_MBOX0_SET);
2198             break;
2199         }
2200         case VPU_HI_MBOX1:
2201         {
2202             _VPU_Write4Byte(VPU_REG_HI_MBOX1_L, u32Msg);
2203             _VPU_WriteWordMask(VPU_REG_HI_MBOX_SET, VPU_REG_HI_MBOX1_SET, VPU_REG_HI_MBOX1_SET);
2204             break;
2205         }
2206         default:
2207         {
2208             bResult = FALSE;
2209             break;
2210         }
2211     }
2212 
2213     return bResult;
2214 }
2215 
HAL_VPU_EX_GetProgCnt(void)2216 MS_U32 HAL_VPU_EX_GetProgCnt(void)
2217 {
2218 
2219     MS_U16 expc_l=0;
2220     MS_U16 expc_h=0;
2221     expc_l = _VPU_Read2Byte(VPU_REG_EXPC_L) & 0xFFFF;
2222     expc_h = _VPU_Read2Byte(VPU_REG_EXPC_H) & 0xFFFF;
2223     return (((MS_U32)expc_h) << 16) | (MS_U32)expc_l;
2224 }
2225 
HAL_VPU_EX_GetTaskId(MS_U32 u32Id)2226 MS_U8 HAL_VPU_EX_GetTaskId(MS_U32 u32Id)
2227 {
2228     return _VPU_EX_GetOffsetIdx(u32Id);
2229 }
2230 
HAL_VPU_EX_SetShareInfoAddr(MS_U32 u32Id,MS_U32 u32ShmAddr)2231 void HAL_VPU_EX_SetShareInfoAddr(MS_U32 u32Id, MS_U32 u32ShmAddr)
2232 {
2233     MS_U8 u8Offset = _VPU_EX_GetOffsetIdx(u32Id);
2234 
2235     if (u32ShmAddr == 0)
2236     {
2237         pVPUHalContext->u32FWShareInfoAddr[u8Offset] = 0xFFFFFFFF;
2238     }
2239     else
2240     {
2241         if (u8Offset == 0)
2242         {
2243             pVPUHalContext->u32FWShareInfoAddr[u8Offset] = u32ShmAddr;
2244         }
2245         else if (u8Offset == 1)
2246         {
2247             pVPUHalContext->u32FWShareInfoAddr[u8Offset] = u32ShmAddr + TEE_ONE_TASK_SHM_SIZE;
2248         }
2249     }
2250 
2251     VPU_MSG_DBG("set PA ShareInfoAddr[%d] = 0x%lx \n", u8Offset, pVPUHalContext->u32FWShareInfoAddr[u8Offset]);
2252     return;
2253 }
2254 
HAL_VPU_EX_GetShareInfoAddr(MS_U32 u32Id)2255 MS_U32 HAL_VPU_EX_GetShareInfoAddr(MS_U32 u32Id)
2256 {
2257     MS_U8 u8Offset = _VPU_EX_GetOffsetIdx(u32Id);
2258 
2259     return pVPUHalContext->u32FWShareInfoAddr[u8Offset];
2260 }
2261 
2262 
HAL_VPU_EX_IsPowered(void)2263 MS_BOOL HAL_VPU_EX_IsPowered(void)
2264 {
2265     return pVPUHalContext->_bVPUPowered;
2266 }
2267 
HAL_VPU_EX_IsRsted(void)2268 MS_BOOL HAL_VPU_EX_IsRsted(void)
2269 {
2270     return pVPUHalContext->_bVPURsted;
2271 }
2272 
HAL_VPU_EX_MVDInUsed(void)2273 MS_BOOL HAL_VPU_EX_MVDInUsed(void)
2274 {
2275     //MVD is in used for MVD or HVD_TSP mode.
2276     MS_U8 i;
2277     MS_U8 u8UseCnt = 0;
2278 
2279     for (i = 0; i < sizeof(pVPUHalContext->_stVPUStream) / sizeof(pVPUHalContext->_stVPUStream[0]); i++)
2280     {
2281         if ((pVPUHalContext->_stVPUStream[i].eDecodertype == E_VPU_EX_DECODER_MVD) ||
2282             (pVPUHalContext->_stVPUStream[i].eDecodertype == E_VPU_EX_DECODER_HVD) )
2283         {
2284             u8UseCnt++;
2285         }
2286     }
2287 
2288     VPU_MSG_DBG("MVD u8UseCnt=%d\n", u8UseCnt);
2289 
2290     if (u8UseCnt != 0)
2291     {
2292         return TRUE;
2293     }
2294     else
2295     {
2296         return FALSE;
2297     }
2298 }
2299 
HAL_VPU_EX_HVDInUsed(void)2300 MS_BOOL HAL_VPU_EX_HVDInUsed(void)
2301 {
2302     //HVD is in used for HVD or MVD in sub stream.
2303     MS_U8 i;
2304     MS_U8 u8UseCnt = 0;
2305 
2306     for (i = 0; i < sizeof(pVPUHalContext->_stVPUStream) / sizeof(pVPUHalContext->_stVPUStream[0]); i++)
2307     {
2308         if ((E_VPU_EX_DECODER_HVD == pVPUHalContext->_stVPUStream[i].eDecodertype) ||
2309             ((E_VPU_EX_DECODER_MVD == pVPUHalContext->_stVPUStream[i].eDecodertype) && (E_HAL_VPU_SUB_STREAM0 == pVPUHalContext->_stVPUStream[i].eStreamId)))
2310         {
2311             u8UseCnt++;
2312         }
2313     }
2314 
2315     VPU_MSG_DBG("HVD u8UseCnt=%d\n", u8UseCnt);
2316 
2317     if (u8UseCnt != 0)
2318     {
2319         return TRUE;
2320     }
2321     else
2322     {
2323         return FALSE;
2324     }
2325 }
2326 
2327 //-----------------------------------------------------------------------------
2328 /// @brief \b Function \b Name: MDrv_HVD_EX_SetDbgLevel()
2329 /// @brief \b Function \b Description:  Set debug level
2330 /// @param -elevel \b IN : debug level
2331 //-----------------------------------------------------------------------------
HAL_VPU_EX_SetDbgLevel(VPU_EX_UartLevel eLevel)2332 void HAL_VPU_EX_SetDbgLevel(VPU_EX_UartLevel eLevel)
2333 {
2334     VPU_PRINT("%s eLevel=0x%x\n", __FUNCTION__, eLevel);
2335 
2336     switch (eLevel)
2337     {
2338         case E_VPU_EX_UART_LEVEL_ERR:
2339         {
2340             u32VpuUartCtrl = E_VPU_UART_CTRL_ERR;
2341             break;
2342         }
2343         case E_VPU_EX_UART_LEVEL_INFO:
2344         {
2345             u32VpuUartCtrl = E_VPU_UART_CTRL_INFO | E_VPU_UART_CTRL_ERR;
2346             break;
2347         }
2348         case E_VPU_EX_UART_LEVEL_DBG:
2349         {
2350             u32VpuUartCtrl = E_VPU_UART_CTRL_DBG | E_VPU_UART_CTRL_ERR | E_VPU_UART_CTRL_INFO;
2351             break;
2352         }
2353         case E_VPU_EX_UART_LEVEL_TRACE:
2354         {
2355             u32VpuUartCtrl = E_VPU_UART_CTRL_TRACE | E_VPU_UART_CTRL_ERR | E_VPU_UART_CTRL_INFO | E_VPU_UART_CTRL_DBG;
2356             break;
2357         }
2358         case E_VPU_EX_UART_LEVEL_FW:
2359         {
2360             u32VpuUartCtrl = E_VPU_UART_CTRL_DISABLE;
2361             break;
2362         }
2363         default:
2364         {
2365             u32VpuUartCtrl = E_VPU_UART_CTRL_DISABLE;
2366             break;
2367         }
2368     }
2369 }
2370 
HAL_VPU_EX_GetFWVer(MS_U32 u32Id,VPU_EX_FWVerType eVerType)2371 MS_U32 HAL_VPU_EX_GetFWVer(MS_U32 u32Id, VPU_EX_FWVerType eVerType)
2372 {
2373     HVD_Return eCtrlRet = E_HVD_RETURN_FAIL;
2374     MS_U32 u32CmdArg = (MS_U32)eVerType;
2375     MS_U32 u32Version = 0xFFFFFFFF;
2376     eCtrlRet = HAL_HVD_EX_SetCmd(u32Id, E_DUAL_VERSION, u32CmdArg);
2377     if (E_HVD_RETURN_SUCCESS != eCtrlRet)
2378     {
2379         VPU_MSG_ERR("E_DUAL_VERSION NG eCtrlRet=%x\n", eCtrlRet);
2380         return u32Version;
2381     }
2382 
2383     MS_BOOL bRet = false;
2384     MS_U32 u32TimeOut = 0xFFFFFFFF;
2385 
2386     while(--u32TimeOut)
2387     {
2388         if(HAL_VPU_EX_MBoxRdy(VPU_RISC_MBOX0))
2389         {
2390             bRet = HAL_VPU_EX_MBoxRead(VPU_RISC_MBOX0, &u32Version);
2391             if (false == bRet)
2392             {
2393                 VPU_MSG_ERR("E_DUAL_VERSION NG bRet=%x\n", bRet);
2394                 return u32Version;
2395             }
2396 
2397             _VPU_WriteWordMask(  VPU_REG_RISC_MBOX_CLR , VPU_REG_RISC_MBOX0_CLR  , VPU_REG_RISC_MBOX0_CLR);
2398             VPU_MSG_DBG("E_DUAL_VERSION arg=%lx u32Version = 0x%lx\n", u32CmdArg, u32Version);
2399             return u32Version;
2400         }
2401     }
2402 
2403     VPU_MSG_ERR("get E_DUAL_VERSION=%x timeout", eVerType);
2404 
2405     return u32Version;
2406 }
2407 
HAL_VPU_EX_NotSupportDS(void)2408 MS_BOOL HAL_VPU_EX_NotSupportDS(void)
2409 {
2410     return FALSE;
2411 }
2412 
2413 //-----------------------------------------------------------------------------
2414 /// @brief \b Function \b Name: HAL_VPU_EX_MIU1BASE()
2415 /// @brief \b Function \b Description:  Get VPU MIU base address
2416 /// @return - vpu MIU1 base
2417 //-----------------------------------------------------------------------------
HAL_VPU_EX_MIU1BASE(void)2418 MS_U32 HAL_VPU_EX_MIU1BASE(void)
2419 {
2420     return VPU_MIU1BASE_ADDR;
2421 }
2422 
HAL_VPU_EX_GetSHMAddr(void)2423 MS_U32 HAL_VPU_EX_GetSHMAddr(void)
2424 {
2425     if(pVPUHalContext->bEnableVPUSecureMode == FALSE)
2426     {
2427         return 0;
2428     }
2429     return pVPUHalContext->u32VPUSHMAddr;
2430 }
HAL_VPU_EX_EnableSecurityMode(MS_BOOL enable)2431 MS_BOOL HAL_VPU_EX_EnableSecurityMode(MS_BOOL enable)
2432 {
2433     pVPUHalContext->bEnableVPUSecureMode = enable;
2434     return TRUE;
2435 }
2436 
HAL_VPU_EX_CHIP_Capability(void * pHWCap)2437 MS_BOOL HAL_VPU_EX_CHIP_Capability(void* pHWCap)
2438 {
2439     ((VDEC_HwCap*)pHWCap)->u8Cap_Support_Decoder_Num = 2;
2440 
2441     ((VDEC_HwCap*)pHWCap)->bCap_Support_MPEG2 = TRUE;
2442     ((VDEC_HwCap*)pHWCap)->bCap_Support_H263 = TRUE;
2443     ((VDEC_HwCap*)pHWCap)->bCap_Support_MPEG4 = TRUE;
2444     ((VDEC_HwCap*)pHWCap)->bCap_Support_DIVX311 = TRUE;
2445     ((VDEC_HwCap*)pHWCap)->bCap_Support_DIVX412 = TRUE;
2446     ((VDEC_HwCap*)pHWCap)->bCap_Support_FLV = TRUE;
2447     ((VDEC_HwCap*)pHWCap)->bCap_Support_VC1ADV = TRUE;
2448     ((VDEC_HwCap*)pHWCap)->bCap_Support_VC1MAIN = TRUE;
2449 
2450     ((VDEC_HwCap*)pHWCap)->bCap_Support_RV8 = TRUE;
2451     ((VDEC_HwCap*)pHWCap)->bCap_Support_RV9 = TRUE;
2452     ((VDEC_HwCap*)pHWCap)->bCap_Support_H264 = TRUE;
2453     ((VDEC_HwCap*)pHWCap)->bCap_Support_AVS = TRUE;
2454     ((VDEC_HwCap*)pHWCap)->bCap_Support_MJPEG = TRUE;
2455     ((VDEC_HwCap*)pHWCap)->bCap_Support_MVC = TRUE;
2456     ((VDEC_HwCap*)pHWCap)->bCap_Support_VP8 = TRUE;
2457     ((VDEC_HwCap*)pHWCap)->bCap_Support_HEVC = TRUE;
2458     ((VDEC_HwCap*)pHWCap)->bCap_Support_VP9 = TRUE;
2459     ((VDEC_HwCap*)pHWCap)->bCap_Support_AVS_PLUS = TRUE;
2460 
2461     return TRUE;
2462 }
2463 
2464 #else
2465 #include "halVPU_EX.h"
2466 #include "drvMMIO.h"
2467 #include "../hvd_ex/regHVD_EX.h"
2468 
2469 extern int lib_lowprintf(const char *fmt, ...);
2470 #define PRINTF lib_lowprintf
2471 #define HVD_LWORD(x)    (MS_U16)((x)&0xffff)
2472 #define HVD_HWORD(x)    (MS_U16)(((x)>>16)&0xffff)
2473 
2474 MS_U8 u8FW_Binary[] = {
2475     #include "fwVPU.dat"
2476 };
2477 
2478 MS_U32 u32HVDRegOSBase;
2479 
HAL_VPU_EX_LoadCodeInSecure(MS_U32 addr)2480 MS_BOOL HAL_VPU_EX_LoadCodeInSecure(MS_U32 addr)
2481 {
2482     //PRINTF("do load code,u32DestAddr %x\n",addr);
2483     memcpy((void*)addr, (void*)u8FW_Binary, sizeof(u8FW_Binary));
2484     MAsm_CPU_Sync();
2485     MsOS_FlushMemory();
2486 
2487     if (FALSE == (*((MS_U8*)(addr+6))=='R' && *((MS_U8*)(addr+7))=='2'))
2488     {
2489         PRINTF("FW is not R2 version! _%x_ _%x_\n", *(MS_U8*)(addr+6), *(MS_U8*)(addr+7));
2490         return FALSE;
2491     }
2492     return TRUE;
2493 }
2494 
HAL_VPU_EX_SetLockDownRegister(void * param)2495 MS_BOOL HAL_VPU_EX_SetLockDownRegister(void* param)
2496 {
2497 #if 1
2498     MS_U32 u32StAddr_main;
2499     MS_U32 u32StAddr_sub;
2500     MS_U32 u32NonPMBankSize = 0;
2501     VPU_EX_LOCK_DOWN_REGISTER* register_lockdown;
2502 
2503     if(param == NULL)
2504     {
2505         return FALSE;
2506     }
2507 
2508     register_lockdown = (VPU_EX_LOCK_DOWN_REGISTER*)param;
2509 
2510     MDrv_MMIO_GetBASE(&u32HVDRegOSBase, &u32NonPMBankSize, MS_MODULE_HW);
2511 
2512     // ES buffer
2513     u32StAddr_main = register_lockdown->Bitstream_Addr_Main;
2514     u32StAddr_sub = register_lockdown->Bitstream_Addr_Sub;
2515 
2516     if (u32StAddr_main >= register_lockdown->MIU1_BaseAddr)
2517     {
2518         u32StAddr_main -= register_lockdown->MIU1_BaseAddr;
2519     }
2520 
2521     if (u32StAddr_sub >= register_lockdown->MIU1_BaseAddr)
2522     {
2523         u32StAddr_sub -= register_lockdown->MIU1_BaseAddr;
2524     }
2525 
2526     //Lock down register
2527     _HVD_Write2Byte(HVD_REG_ESB_ST_ADDR_L(REG_HVD_BASE), HVD_LWORD(u32StAddr_main >> 3));
2528     _HVD_Write2Byte(HVD_REG_ESB_ST_ADDR_H(REG_HVD_BASE), HVD_HWORD(u32StAddr_main >> 3));
2529 
2530     _HVD_Write2Byte(HVD_REG_ESB_ST_ADDR_L_BS2, HVD_LWORD(u32StAddr_sub >> 3));
2531     _HVD_Write2Byte(HVD_REG_ESB_ST_ADDR_H_BS2, HVD_HWORD(u32StAddr_sub >> 3));
2532     //~
2533 
2534     // Lock Down
2535     //_HVD_Write2Byte(HVD_REG_HI_DUMMY_0, (_HVD_Read2Byte(HVD_REG_HI_DUMMY_0) | (HVD_REG_LOCK_REG_ESB_ST_ADR_L_H|HVD_REG_LOCK_REG_ESB_ST_ADR_L_H_BS2)));
2536     //~
2537 #endif
2538     return TRUE;
2539 }
2540 
2541 #endif
2542 
2543 
2544 
2545