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 /// file drvHVD_EX.c
98 /// @brief HVD Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
103 //-------------------------------------------------------------------------------------------------
104 // Include Files
105 //-------------------------------------------------------------------------------------------------
106 #if (!defined(MSOS_TYPE_NUTTX) && !defined(MSOS_TYPE_OPTEE)) || defined(SUPPORT_X_MODEL_FEATURE)
107
108 // Common Definition
109 #ifdef MSOS_TYPE_LINUX_KERNEL
110 #include <linux/string.h>
111 #else
112 #include <string.h>
113 #endif
114
115 #include "drvHVD_Common.h"
116 #include "drvHVD_EX.h"
117 #include "drvHVD_def.h"
118 #include "HVD_EX_Common.h"
119 #include "controller.h"
120 #include "fwHVD_if.h"
121 #include "halVPU_EX.h"
122 #include "halHVD_EX.h"
123 #include "drvSYS.h"
124
125 #if HVD_ENABLE_AUTO_SET_REG_BASE
126 #include "drvMMIO.h"
127 #endif
128
129 //-------------------------------------------------------------------------------------------------
130 // Driver Compiler Options
131 //-------------------------------------------------------------------------------------------------
132
133
134 //-------------------------------------------------------------------------------------------------
135 // Local Defines
136 //-------------------------------------------------------------------------------------------------
137 #ifndef UNUSED
138 #define UNUSED(x) (void)(x)
139 #endif
140
141 //-------------------------------------------------------------------------------------------------
142 // Local Structures
143 //-------------------------------------------------------------------------------------------------
144
145 //-------------------------------------------------------------------------------------------------
146 // Local Functions Prototype
147 //-------------------------------------------------------------------------------------------------
148
149 #define IPAUTH_VIDEO_H264 (27)
150 #define IPAUTH_VIDEO_RM (28)
151 #define IPAUTH_VIDEO_AVS (33)
152 #define IPAUTH_VIDEO_MVC (40)
153 #define IPAUTH_VIDEO_VP8 (61)
154 #define IPAUTH_VIDEO_HEVC (75)
155 #define IPAUTH_VIDEO_VP9 (91)
156 #define IPAUTH_VIDEO_DOLBY_HDR (118)
157 MS_BOOL bModeSelectHD = TRUE;
158 static MS_BOOL bAutoIpcheck = TRUE;
159 #if !defined(SUPPORT_X_MODEL_FEATURE)
160 extern MS_U8 MDrv_AUTH_IPCheck(MS_U8 u8Bit); //FROM #include "drvIPAUTH.h"
161 #endif
162
163 #define HVD_CODEC_INFO_MASK 0x0000000f
164
_MDrv_HVD_AUTH_IPCheck(MS_U32 u32CodecType)165 MS_BOOL _MDrv_HVD_AUTH_IPCheck(MS_U32 u32CodecType)
166 {
167 MS_BOOL bIPAUTHReady = FALSE;
168 VDEC_EX_CODEC_CAP_INFO pCodecCapInfo;
169 #if !defined(SUPPORT_X_MODEL_FEATURE)
170 u32CodecType = (u32CodecType & HVD_CODEC_INFO_MASK);
171 bAutoIpcheck = FALSE;
172 switch (u32CodecType)
173 {
174 case HVD_INIT_HW_AVC:
175 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_H264) == TRUE)
176 {
177 bModeSelectHD =TRUE;
178 bIPAUTHReady = TRUE;
179 bAutoIpcheck =TRUE;
180 }
181 break;
182 case HVD_INIT_HW_AVS:
183 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_AVS) == TRUE)
184 {
185 bModeSelectHD =TRUE;
186 bIPAUTHReady = TRUE;
187 bAutoIpcheck =TRUE;
188 }
189 break;
190 case HVD_INIT_HW_RM:
191 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_RM) == TRUE)
192 {
193 bModeSelectHD =TRUE;
194 bIPAUTHReady =TRUE;
195 bAutoIpcheck =TRUE;
196 }
197 break;
198 case HVD_INIT_HW_MVC: /// SUPPORT_MVC
199 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_MVC) == TRUE)
200 {
201 bModeSelectHD =TRUE;
202 bIPAUTHReady =TRUE;
203 bAutoIpcheck =TRUE;
204 }
205 break;
206 case HVD_INIT_HW_VP8:
207 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_VP8) == TRUE)
208 {
209 bModeSelectHD =TRUE;
210 bIPAUTHReady =TRUE;
211 bAutoIpcheck =TRUE;
212 }
213 break;
214 case HVD_INIT_HW_HEVC:
215 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_HEVC) == TRUE)
216 {
217 bModeSelectHD =TRUE;
218 bIPAUTHReady =TRUE;
219 bAutoIpcheck =TRUE;
220 }
221 break;
222 case HVD_INIT_HW_VP9:
223 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_VP9) == TRUE && HAL_VPU_EX_GetCodecCapInfo( E_HVD_EX_CODEC_TYPE_VP9, &pCodecCapInfo) == TRUE)
224 {
225 bModeSelectHD =TRUE;
226 bIPAUTHReady =TRUE;
227 bAutoIpcheck =TRUE;
228 }
229 break;
230 case HVD_INIT_HW_HEVC_DV:
231 if (MDrv_AUTH_IPCheck(IPAUTH_VIDEO_DOLBY_HDR) == TRUE)
232 {
233 bModeSelectHD =TRUE;
234 bIPAUTHReady =TRUE;
235 bAutoIpcheck =TRUE;
236 }
237 break;
238 default:
239 bIPAUTHReady = FALSE;
240 break;
241 }
242
243 if(bIPAUTHReady == FALSE)
244 {
245 HVD_EX_MSG_ERR("[%s]User using invaild key !!!\n",__FUNCTION__);
246 }
247 #endif
248 return bIPAUTHReady;
249 }
250
251 #ifdef VDEC3
252 #define HVD_MAX_STREAMS 16
253 #else
254 #define HVD_MAX_STREAMS 2
255 #endif
256
257 #define VPU_MAIL_BOX_SEND_FAIL 1
258 #define CODEC_KEY_COMPARE_TIMEOUT 2
259 extern MS_U8 u8CodecCompare[HVD_MAX_STREAMS];
260
261 extern HVD_EX_Result _HVD_EX_Rst(MS_U32 u32Id, MS_BOOL bErrHandle);
262
263 #define _VDEC_GET_IDX(x) (MS_U8)((x)>>24)
264
265 #if defined(CHIP_K6LITE) || defined(CHIP_K6)
_MDrv_HVDSendVPUCMD(MS_U32 u32Id,MS_U32 u32CMD,MS_U32 u32Arg)266 MS_BOOL _MDrv_HVDSendVPUCMD(MS_U32 u32Id,MS_U32 u32CMD,MS_U32 u32Arg)
267 {
268 MS_BOOL bRet = TRUE;
269 MS_U8 u8Idx = _VDEC_GET_IDX(u32Id);
270
271 HAL_VPU_EX_Mutex_Lock();
272 if(HAL_HVD_EX_SetCmd(u32Id, u32CMD, u32Arg) != E_HVD_RETURN_SUCCESS)
273 {
274 bRet = FALSE;
275 }
276 HAL_VPU_EX_Mutex_UnLock();
277
278 if(bRet == FALSE)
279 {
280 HVD_PRINT("[Info]Case1\n");
281 u8CodecCompare[u8Idx] = VPU_MAIL_BOX_SEND_FAIL;
282 }
283
284 return bRet;
285 }
286
_MDrv_HVDSendCodecKey(MS_U32 u32Id,MS_U8 u8Decoder,MS_U16 u16CodecKey)287 MS_BOOL _MDrv_HVDSendCodecKey(MS_U32 u32Id,MS_U8 u8Decoder,MS_U16 u16CodecKey)
288 {
289 MS_U8 u8Idx = HAL_VPU_EX_GetOffsetIdx(u32Id);
290 MS_U32 u32Timeout = 0;
291 struct _ctl_info *ctl_ptr = (struct _ctl_info *) MsOS_PA2KSEG1(HAL_VPU_EX_GetFWCodeAddr(u32Id) + CTL_INFO_ADDR);
292
293 if(_MDrv_HVDSendVPUCMD(u32Id,E_NST_CMD_COMMON_CMD1,(((VDEC_TAG<<24)&0xff000000))|(((u8Decoder<<16)&0xff0000))|(u16CodecKey&0xffff)) == FALSE)
294 {
295 HAL_HVD_EX_DeInit(u32Id);
296 return FALSE;
297 }
298
299 if(_MDrv_HVDSendVPUCMD(u32Id,E_NST_CMD_COMMON_MASK,0) == FALSE)
300 {
301 HAL_HVD_EX_DeInit(u32Id);
302 return FALSE;
303 }
304
305 MsOS_ReadMemory();
306 u32Timeout = HVD_GetSysTime_ms() + 500; //timeout 500 ms
307 while (CTL_TASK_CMD != ctl_ptr->task_statue[u8Idx])
308 {
309 if (HVD_GetSysTime_ms() > u32Timeout)
310 {
311 HVD_PRINT("[Info]Case2\n");
312 u8CodecCompare[u8Idx] = CODEC_KEY_COMPARE_TIMEOUT;
313 HAL_HVD_EX_DeInit(u32Id);
314 return FALSE;
315 }
316 MsOS_ReadMemory();
317 }
318
319 return TRUE;
320 }
321 #endif
322
323
_HVD_EX_Init_(MS_U32 u32Id,MS_U32 u32ModeFlag)324 HVD_EX_Result _HVD_EX_Init_(MS_U32 u32Id,MS_U32 u32ModeFlag)
325 {
326 HVD_EX_Result eRst = E_HVD_EX_FAIL;
327
328 UNUSED(u32ModeFlag);
329 if(bAutoIpcheck)
330 {
331 eRst = _HVD_EX_Rst(u32Id ,FALSE);
332 #ifndef CONFIG_MBOOT
333 #if defined(CHIP_K6LITE) || defined(CHIP_K6)
334 MS_U32 u32CodecType = u32ModeFlag & HVD_CODEC_INFO_MASK;
335 if(eRst == E_HVD_EX_OK)
336 {
337 if(u32CodecType != HVD_INIT_HW_MJPEG)
338 {
339 if(_MDrv_HVDSendCodecKey(u32Id,HVD_DECODER,u32CodecType) == FALSE)
340 {
341 eRst = E_HVD_EX_FAIL;
342 }
343 }
344 }
345 #endif
346 #endif
347 }
348 else
349 {
350 HVD_PRINT("[%s] .. STOP and Contact VDEC team!!!\n",__FUNCTION__);
351 }
352
353 return eRst;
354 }
355
356 #else
357
358 #include "drvHVD_def.h"
359 #include "HVD_EX_Common.h"
360 #include "controller.h"
361 #include "halHVD_EX.h"
362
_MDrv_HVD_EX_TEE_AllocateEsBufHandle_(MS_U8 u8Idx,MS_PHY pVPUCodecAddr,MS_U32 u32ReqSize,MS_U32 u32BuffEnd,MS_U32 * pu32EsHandle)363 MS_BOOL _MDrv_HVD_EX_TEE_AllocateEsBufHandle_(MS_U8 u8Idx, MS_PHY pVPUCodecAddr, MS_U32 u32ReqSize, MS_U32 u32BuffEnd, MS_U32 *pu32EsHandle)
364 {
365 VDEC_ESMap_Table *pEsHandleQueue = (VDEC_ESMap_Table *) HAL_HVD_EX_GetVpuShmAddr(u8Idx, pVPUCodecAddr);
366 const MS_U32 u32QueueSize = sizeof(pEsHandleQueue->stEntry) / sizeof(VDEC_ESMap_Entry);
367
368 //HVD_ERR("allocate with read %d write %d\n", pEsHandleQueue->u32RdPtr, pEsHandleQueue->u32WrPtr);
369 // queue is empty
370 if (pEsHandleQueue->u32WrPtr == pEsHandleQueue->u32RdPtr)
371 {
372 pEsHandleQueue->stEntry[pEsHandleQueue->u32WrPtr].u32Offset = 0;
373 pEsHandleQueue->stEntry[pEsHandleQueue->u32WrPtr].u32Length = u32ReqSize;
374 }
375 else
376 {
377 MS_U32 u32PrevWptr, u32Vacancy;
378 MS_U32 u32WriteOffset, u32ReadOffset, u32WriteEndOffset;
379
380 if (pEsHandleQueue->u32WrPtr == 0)
381 u32PrevWptr = u32QueueSize - 1;
382 else
383 u32PrevWptr = pEsHandleQueue->u32WrPtr - 1;
384 // check if queue is full
385 if (pEsHandleQueue->u32WrPtr == pEsHandleQueue->u32RdPtr)
386 {
387 u32Vacancy = u32QueueSize;
388 }
389 else if (pEsHandleQueue->u32WrPtr > pEsHandleQueue->u32RdPtr)
390 {
391 u32Vacancy = u32QueueSize - (pEsHandleQueue->u32WrPtr - pEsHandleQueue->u32RdPtr);
392 }
393 else
394 {
395 // pEsHandleQueue->u32RdPtr > pEsHandleQueue->u32WrPtr
396 u32Vacancy = pEsHandleQueue->u32RdPtr - pEsHandleQueue->u32WrPtr;
397 }
398 u32Vacancy--; // read write pointer should not be the same after inserting one entry
399 if (u32Vacancy == 0)
400 return FALSE;
401
402 // check free size and if we need to wrap around in ES buffer
403 u32WriteOffset = pEsHandleQueue->stEntry[u32PrevWptr].u32Offset + pEsHandleQueue->stEntry[u32PrevWptr].u32Length;
404 u32ReadOffset = pEsHandleQueue->stEntry[pEsHandleQueue->u32RdPtr].u32Offset;
405
406 u32WriteEndOffset = u32WriteOffset + u32ReqSize;
407
408 if (u32ReadOffset <= u32WriteOffset)
409 {
410 if (u32WriteEndOffset >= u32BuffEnd)
411 {
412 u32WriteOffset = 0;
413 u32WriteEndOffset = u32ReqSize; // start from 0
414
415 if (u32WriteEndOffset > u32ReadOffset)
416 {
417 return FALSE;
418 }
419 }
420 }
421 else
422 {
423 if (u32WriteEndOffset > u32ReadOffset)
424 {
425 return FALSE;
426 }
427 }
428
429 pEsHandleQueue->stEntry[pEsHandleQueue->u32WrPtr].u32Offset = u32WriteOffset;
430 pEsHandleQueue->stEntry[pEsHandleQueue->u32WrPtr].u32Length = u32ReqSize;
431 }
432
433 *pu32EsHandle = pEsHandleQueue->u32WrPtr;
434
435 pEsHandleQueue->u32WrPtr++;
436 if (pEsHandleQueue->u32WrPtr >= u32QueueSize)
437 pEsHandleQueue->u32WrPtr = 0;
438
439 MsOS_FlushMemory();
440 return TRUE;
441 }
442
_MDrv_HVD_EX_TEE_GetESBufByHandle_(MS_U8 u8Idx,MS_PHY pVPUCodecAddr,MS_U32 u32EsHandle,MS_PHY * pPhyAddr)443 MS_BOOL _MDrv_HVD_EX_TEE_GetESBufByHandle_(MS_U8 u8Idx, MS_PHY pVPUCodecAddr, MS_U32 u32EsHandle, MS_PHY *pPhyAddr)
444 {
445 VDEC_ESMap_Table *pEsHandleQueue = (VDEC_ESMap_Table *) HAL_HVD_EX_GetVpuShmAddr(u8Idx, pVPUCodecAddr);
446
447 if (u32EsHandle >= sizeof(pEsHandleQueue->stEntry) / sizeof(VDEC_ESMap_Entry))
448 return FALSE;
449
450 *pPhyAddr = pEsHandleQueue->stEntry[u32EsHandle].u32Offset;
451 return TRUE;
452 }
453
454 #endif
455