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