xref: /utopia/UTPA2-700.0.x/modules/sem/drv/sem/drvSEM.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 #if !defined(MSOS_TYPE_LINUX_KERNEL)
79 #include <stdio.h>
80 #include <string.h>
81 #else
82 #include <linux/string.h>
83 #include <linux/slab.h>
84 #endif
85 #include "MsCommon.h"
86 #include "MsVersion.h"
87 #include "drvSEM.h"
88 #include "drvMMIO.h"
89 #include "halSEM.h"
90 
91 #include "utopia_dapi.h"
92 #include "utopia.h"
93 #ifdef MSOS_TYPE_LINUX_KERNEL
94 #include <linux/string.h>
95 #else
96 #include <string.h>
97 #endif
98 #include "ULog.h"
99 
100 //-------------------------------------------------------------------------------------------------
101 //  Local Defines
102 //-------------------------------------------------------------------------------------------------
103 static SEM_DbgLvl _gSEMDbgLevel = E_SEM_DBGLVL_ERROR;
104 static MS_BOOL _gbSEMInitialized = FALSE;
105 #define SEM_MAX_NUM                       (16)
106 #define TAG_SEM "SEM"
107 
108 #ifdef CONFIG_MSTAR_UTPA2K_SEM_ENABLE
109 #define SEM_UTOPIA20 (1)
110 #else
111 #define SEM_UTOPIA20 (0)
112 #endif
113 //#define SEM_UTOPIA20 1
114 //-------------------------------------------------------------------------------------------------
115 //  Debug Functions
116 //-------------------------------------------------------------------------------------------------
117 #define SEM_PRINT()               if (_gSEMDbgLevel >= E_SEM_DBGLVL_ALL) \
118                                         {MS_DEBUG_MSG(ULOGD(TAG_SEM, "\t====   %s   ====\n", __FUNCTION__);)}
119 #define SEM_DEBUG(x, args...)     if (_gSEMDbgLevel >= E_SEM_DBGLVL_INFO ) \
120                                         {MS_DEBUG_MSG(ULOGD(TAG_SEM, "[SEM USER DEBUG][%s]: ", __FUNCTION__); ULOGD(TAG_SEM, x, ##args);)}
121 #define SEM_ERROR(x, args...)      if (_gSEMDbgLevel >= E_SEM_DBGLVL_ERROR) \
122                                         {MS_DEBUG_MSG(ULOGE(TAG_SEM, "[SEM USER ERR][%s]: ", __FUNCTION__); ULOGE(TAG_SEM, x, ##args);)}
123 #define SEM_WARN(x, args...)     if (_gSEMDbgLevel >= E_SEM_DBGLVL_WARNING) \
124                                         {MS_DEBUG_MSG(ULOGW(TAG_SEM, "[SEM USER WARN][%s]: ", __FUNCTION__); ULOGW(TAG_SEM, x, ##args);)}
125 
126 #define SEM_PM51_ID                       0x01
127 #define SEM_AEON_ID                       0x02
128 #define SEM_ARM_MIPS_ID                   0x03
129 
130 #if (defined(MCU_AEON))
131     #define    SEM_RESOURCE_ID            SEM_AEON_ID
132 #else
133     #define    SEM_RESOURCE_ID            SEM_ARM_MIPS_ID
134 #endif
135 
136 //define mutex
137 #define SEM_MUTEX_CREATE(_index)          _s32SEMMutex[_index] = MsOS_CreateMutex(E_MSOS_FIFO, _SEMMutexName[_index], MSOS_PROCESS_SHARED)
138 #define SEM_MUTEX_LOCK(_index, _waitms)   OS_OBTAIN_MUTEX(_s32SEMMutex[_index], _waitms)
139 #define SEM_MUTEX_UNLOCK(_index)          OS_RELEASE_MUTEX(_s32SEMMutex[_index])
140 #define SEM_MUTEX_DELETE(_index)          OS_DELETE_MUTEX(_s32SEMMutex[_index])
141 
142 static MS_S32 _s32SEMMutex[SEM_MAX_NUM + 1] = {-1};
143 static char _SEMMutexName[SEM_MAX_NUM][13] = {"SEMMUTEX0","SEMMUTEX1","SEMMUTEX2","SEMMUTEX3","SEMMUTEX4","SEMMUTEX5","SEMMUTEX6","SEMMUTEX7",\
144                                               "SEMMUTEX8","SEMMUTEX9","SEMMUTEX10","SEMMUTEX11","SEMMUTEX12","SEMMUTEX13","SEMMUTEX14","SEMMUTEX15"};
145 
146 static MSIF_Version _drv_sem_version = {
147     .DDI = { SEM_DRV_VERSION },
148 };
149 
150 #if (SEM_UTOPIA20)
151 #include "drvSEM_priv.h"
152 MS_BOOL bU20SemOpened = FALSE;
_MDrv_SEM_CheckUtopia20Open(void ** pInstance,MS_U32 u32ModuleVersion,void * pAttribute)153 MS_BOOL _MDrv_SEM_CheckUtopia20Open(void** pInstance, MS_U32 u32ModuleVersion, void* pAttribute)
154 {
155 	if (FALSE == bU20SemOpened)
156 	{
157 		if(UtopiaOpen(MODULE_SEM|KERNEL_MODE, pInstance, u32ModuleVersion, pAttribute) != UTOPIA_STATUS_SUCCESS)
158 		{
159 			//WDT_DBG_ERR("Open WDT fail\n");
160 			return FALSE;
161 		}
162 		bU20SemOpened= TRUE;
163 	}
164 	return TRUE;
165 }
166 #endif
167 
168 //-------------------------------------------------------------------------------------------------
169 // Gloabal Functions
170 //-------------------------------------------------------------------------------------------------
171 
172 //-------------------------------------------------------------------------------------------------
173 /// Attempt to get mmio base and create mutex
174 /// @return TRUE : succeed
175 /// @return FALSE : fail
176 //-------------------------------------------------------------------------------------------------
177 
178 void* pInstantSem ;
179 void* pAttributeSEM ;
180 
181 
MDrv_SEM_Init(void)182 MS_BOOL MDrv_SEM_Init(void)
183 {
184     MS_VIRT u32BaseAddr;
185 	MS_PHY u32BaseSize;
186     MS_S32 s32Index;
187 
188     if (_gbSEMInitialized)
189         return TRUE;
190 
191     if (!MDrv_MMIO_GetBASE(&u32BaseAddr, &u32BaseSize, MS_MODULE_SEM))
192     {
193         MS_CRITICAL_MSG(ULOGE(TAG_SEM, "[%s][%d] Get register base fail\n", __FUNCTION__, __LINE__););
194         return FALSE;
195     }
196 
197     // make sure MMIO base is correct
198     // a more appropriate solution might be let MDrv_MMIO_GetBASE return FALSE before getting correct base
199     if (u32BaseSize==0)
200         return FALSE;
201 
202     for(s32Index = 0; s32Index < SEM_MAX_NUM; s32Index++)
203     {
204         SEM_MUTEX_CREATE(s32Index);
205         SEM_DEBUG("_s32SEMMutex[%ld] = %ld\n", s32Index, _s32SEMMutex[s32Index]);
206     }
207 
208     _gbSEMInitialized = TRUE;
209 
210     return HAL_SEM_SetBank(u32BaseAddr);
211 }
212 
213 
214 //-------------------------------------------------------------------------------------------------
215 ///
216 ///
217 ///
218 //-------------------------------------------------------------------------------------------------
219 
MDrv_SEM_Get_Resource_U2K(MS_U8 u8SemID,MS_U16 u16ResId)220 MS_BOOL MDrv_SEM_Get_Resource_U2K(MS_U8 u8SemID, MS_U16 u16ResId)
221 {
222     if (!_gbSEMInitialized)
223     {
224         SEM_WARN("%s is called before init\n", __FUNCTION__);
225         return FALSE;
226     }
227 
228     return HAL_SEM_Get_Resource(u8SemID, u16ResId);
229 }
230 
MDrv_SEM_Get_Resource(MS_U8 u8SemID,MS_U16 u16ResId)231 MS_BOOL MDrv_SEM_Get_Resource(MS_U8 u8SemID, MS_U16 u16ResId)
232 {
233 #if (SEM_UTOPIA20)
234 	MS_U32 u32Ret;
235 	PSEM_GETRESOURCE_PARAM pSemGetResourceParam = NULL;
236 
237 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
238 		return 0;
239 
240 	pSemGetResourceParam = (PSEM_GETRESOURCE_PARAM)malloc(sizeof(SEM_GETRESOURCE_PARAM));
241 
242 	pSemGetResourceParam->u8SemID = u8SemID;
243 	pSemGetResourceParam->u16ResId = u16ResId;
244 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Get_Resource,(MS_U32*)pSemGetResourceParam);
245 	free(pSemGetResourceParam);
246 	return (MS_BOOL)u32Ret;
247 #else
248 	return MDrv_SEM_Get_Resource_U2K(u8SemID,u16ResId);
249 #endif
250 }
251 
252 //-------------------------------------------------------------------------------------------------
253 ///
254 ///
255 ///
256 //-------------------------------------------------------------------------------------------------
257 
MDrv_SEM_Free_Resource_U2K(MS_U8 u8SemID,MS_U16 u16ResId)258 MS_BOOL MDrv_SEM_Free_Resource_U2K(MS_U8 u8SemID, MS_U16 u16ResId)
259 {
260     if (!_gbSEMInitialized)
261     {
262         SEM_WARN("%s is called before init\n", __FUNCTION__);
263         return FALSE;
264     }
265 
266     return HAL_SEM_Free_Resource(u8SemID, u16ResId);
267 }
268 
MDrv_SEM_Free_Resource(MS_U8 u8SemID,MS_U16 u16ResId)269 MS_BOOL MDrv_SEM_Free_Resource(MS_U8 u8SemID, MS_U16 u16ResId)
270 {
271 #if (SEM_UTOPIA20)
272 	MS_U32 u32Ret;
273 	PSEM_FREERESOURCE_PARAM pSemFreeResourceParam = NULL;
274 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
275 		return 0;
276 	pSemFreeResourceParam = (PSEM_FREERESOURCE_PARAM)malloc(sizeof(SEM_FREERESOURCE_PARAM));
277 	pSemFreeResourceParam->u8SemID = u8SemID;
278 	pSemFreeResourceParam->u16ResId = u16ResId;
279 
280 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Free_Resource,(MS_U32*)pSemFreeResourceParam);
281 	free(pSemFreeResourceParam);
282 	return (MS_BOOL)u32Ret;
283 #else
284 	return MDrv_SEM_Free_Resource_U2K(u8SemID,u16ResId);
285 #endif
286 }
287 
288 
289 //-------------------------------------------------------------------------------------------------
290 ///
291 ///
292 ///
293 //-------------------------------------------------------------------------------------------------
MDrv_SEM_Reset_Resource_U2K(MS_U8 u8SemID)294 MS_BOOL MDrv_SEM_Reset_Resource_U2K(MS_U8 u8SemID)
295 {
296     if (!_gbSEMInitialized)
297     {
298         SEM_WARN("%s is called before init\n", __FUNCTION__);
299         return FALSE;
300     }
301 
302     return HAL_SEM_Reset_Resource(u8SemID);
303 }
304 
305 
MDrv_SEM_Reset_Resource(MS_U8 u8SemID)306 MS_BOOL MDrv_SEM_Reset_Resource(MS_U8 u8SemID)
307 {
308 #if (SEM_UTOPIA20)
309 	MS_U32 u32Ret;
310 	PSEM_RESETRESOURCE_PARAM pSemResetResourceParam = NULL;
311 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
312 		return 0;
313 	pSemResetResourceParam = (PSEM_RESETRESOURCE_PARAM)malloc(sizeof(SEM_RESETRESOURCE_PARAM));
314 	pSemResetResourceParam->u8SemID = u8SemID;
315 
316 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Reset_Resource,(MS_U32*)pSemResetResourceParam);
317 	free(pSemResetResourceParam);
318 	return (MS_BOOL)u32Ret;
319 #else
320 	return MDrv_SEM_Reset_Resource_U2K(u8SemID);
321 #endif
322 }
323 
324 //-------------------------------------------------------------------------------------------------
325 ///
326 ///
327 ///
328 //-------------------------------------------------------------------------------------------------
MDrv_SEM_Get_ResourceID_U2K(MS_U8 u8SemID,MS_U16 * pu16ResId)329 MS_BOOL MDrv_SEM_Get_ResourceID_U2K(MS_U8 u8SemID, MS_U16* pu16ResId)
330 {
331     if (!_gbSEMInitialized)
332     {
333         SEM_WARN("%s is called before init\n", __FUNCTION__);
334         return FALSE;
335     }
336 
337     return HAL_SEM_Get_ResourceID(u8SemID, pu16ResId);
338 }
339 
MDrv_SEM_Get_ResourceID(MS_U8 u8SemID,MS_U16 * pu16ResId)340 MS_BOOL MDrv_SEM_Get_ResourceID(MS_U8 u8SemID, MS_U16* pu16ResId)
341 {
342 #if (SEM_UTOPIA20)
343 	MS_U32 u32Ret;
344 	PSEM_GETRESOURCEID_PARAM pSemGetResourceIDParam = NULL;
345 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
346 		return 0;
347 	pSemGetResourceIDParam = (PSEM_GETRESOURCEID_PARAM)malloc(sizeof(SEM_GETRESOURCEID_PARAM));
348 	pSemGetResourceIDParam->u8SemID = u8SemID;
349 	pSemGetResourceIDParam->pu16ResId = pu16ResId;
350 
351 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Get_ResourceID,(MS_U32*)pSemGetResourceIDParam);
352 	free(pSemGetResourceIDParam);
353 	return (MS_BOOL)u32Ret;
354 #else
355 	return MDrv_SEM_Get_ResourceID_U2K(u8SemID, pu16ResId);
356 #endif
357 }
358 
359 //-------------------------------------------------------------------------------------------------
360 ///
361 ///
362 ///
363 //-------------------------------------------------------------------------------------------------
364 
MDrv_SEM_Get_Num(void)365 MS_U32 MDrv_SEM_Get_Num(void)
366 {
367     return HAL_SEM_Get_Num();
368 }
369 
370 //-------------------------------------------------------------------------------------------------
371 ///
372 ///
373 ///
374 //-------------------------------------------------------------------------------------------------
375 
MDrv_SEM_GetLibVer(const MSIF_Version ** ppVersion)376 MS_BOOL MDrv_SEM_GetLibVer(const MSIF_Version **ppVersion)
377 {
378     if (!ppVersion)
379         return FALSE;
380 
381     *ppVersion = &_drv_sem_version;
382     return TRUE;
383 }
384 
385 //-------------------------------------------------------------------------------------------------
386 /// Attempt to lock a hardware semaphore
387 /// @param  SemId       \b IN: hardware semaphore ID
388 /// @param  u32WaitMs   \b IN: 0 ~ SEM_WAIT_FOREVER: suspend time (ms) if the mutex is locked
389 /// @return TRUE : succeed
390 /// @return FALSE : fail
391 //-------------------------------------------------------------------------------------------------
MDrv_SEM_Lock_U2K(eSemId SemId,MS_U32 u32WaitMs)392 MS_BOOL MDrv_SEM_Lock_U2K(eSemId SemId, MS_U32 u32WaitMs)
393 {
394     if (!_gbSEMInitialized)
395     {
396         SEM_WARN("%s is called before init\n", __FUNCTION__);
397         return FALSE;
398     }
399 
400     MS_S16 s16SemId;
401     MS_BOOL bRet;
402     MS_U32  u32SysOldTime, u32Interval;
403 
404     SEM_DEBUG("Lock SemId = %d\n", SemId);
405     SEM_DEBUG("Lock u32WaitMs = %ld\n", u32WaitMs);
406 
407     bRet = FALSE;
408     s16SemId = HAL_SEM_GetSemId(SemId);
409     u32SysOldTime = MsOS_GetSystemTime();
410     u32Interval = 0;
411 
412     if(s16SemId < 0)
413     {
414         SEM_ERROR("Lock SemId%d invalid\n", SemId);
415         return FALSE;
416     }
417 
418     SEM_DEBUG("Lock s16SemId = %d\n", s16SemId);
419 
420     /*blocking*/
421     if(u32WaitMs == SEM_WAIT_FOREVER)
422     {
423         bRet = SEM_MUTEX_LOCK(s16SemId, MSOS_WAIT_FOREVER);
424 
425         if(bRet == FALSE)
426         {
427            SEM_ERROR("Obtain mutex %s failed\n", _SEMMutexName[s16SemId]);
428         }
429         else
430         {
431             do
432             {
433                 bRet = HAL_SEM_Get_Resource((MS_U8)s16SemId, SEM_RESOURCE_ID);
434             }while(bRet!= TRUE);
435 
436             if(bRet == FALSE)
437             {
438                 SEM_MUTEX_UNLOCK(s16SemId);
439                 SEM_ERROR("Obtain hardware semaphore %d failed\n", s16SemId);
440             }
441         }
442     }
443     /*blocking with timeout*/
444     else
445     {
446 
447         bRet = SEM_MUTEX_LOCK(s16SemId, (u32WaitMs - u32Interval));
448 
449         if(bRet == FALSE)
450         {
451            SEM_ERROR("Obtain mutex %s failed\n", _SEMMutexName[s16SemId]);
452         }
453         else
454         {
455             do
456             {
457                 bRet = HAL_SEM_Get_Resource((MS_U8)s16SemId, SEM_RESOURCE_ID);
458                 u32Interval = MsOS_GetSystemTime() - u32SysOldTime ;
459             }while((bRet!= TRUE) && (u32Interval < u32WaitMs));
460 
461             if(bRet == FALSE)
462             {
463                 SEM_MUTEX_UNLOCK(s16SemId);
464                 SEM_ERROR("Obtain hardware semaphore %d failed, timeout=%ld\n", s16SemId, u32WaitMs);
465             }
466         }
467     }
468 
469     return bRet;
470 }
471 
472 
MDrv_SEM_Lock(eSemId SemId,MS_U32 u32WaitMs)473 MS_BOOL MDrv_SEM_Lock(eSemId SemId, MS_U32 u32WaitMs)
474 {
475 #if (SEM_UTOPIA20)
476 	MS_U32 u32Ret;
477 	PSEM_LOCK_PARAM pSemLockParam = NULL;
478 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
479 		return 0;
480 	pSemLockParam = (PSEM_LOCK_PARAM)malloc(sizeof(SEM_LOCK_PARAM));
481 	pSemLockParam->u32SemID = SemId;
482 	pSemLockParam->u32WaitMs = u32WaitMs;
483 
484 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Lock,(MS_U32*)pSemLockParam);
485 	free(pSemLockParam);
486 	return (MS_BOOL)u32Ret;
487 #else
488 	return MDrv_SEM_Lock_U2K(SemId, u32WaitMs);
489 #endif
490 }
491 
492 
493 //-------------------------------------------------------------------------------------------------
494 /// Attempt to unlock a hardware semaphore
495 /// @param  SemId       \b IN: hardware semaphore ID
496 /// @return TRUE : succeed
497 /// @return FALSE : fail
498 //-------------------------------------------------------------------------------------------------
499 
MDrv_SEM_Unlock_U2K(eSemId SemId)500 MS_BOOL MDrv_SEM_Unlock_U2K(eSemId SemId)
501 {
502     if (!_gbSEMInitialized)
503     {
504         SEM_WARN("%s is called before init\n", __FUNCTION__);
505         return FALSE;
506     }
507 
508     MS_S16 s16SemId;
509     MS_BOOL bRet;
510 
511     bRet = FALSE;
512     SEM_DEBUG("Unlock SemId = %d\n", SemId);
513     s16SemId = HAL_SEM_GetSemId(SemId);
514 
515     if(s16SemId < 0)
516     {
517         SEM_ERROR("Unlock SemId%d invalid\n", SemId);
518         return FALSE;
519     }
520 
521     SEM_DEBUG("Unlock s16SemId = %d\n", s16SemId);
522 
523     bRet = HAL_SEM_Free_Resource((MS_U8)s16SemId, SEM_RESOURCE_ID);
524 
525     if(bRet == FALSE)
526     {
527         SEM_ERROR("Release hardware semaphore %d failed\n", s16SemId);
528     }
529 
530     SEM_MUTEX_UNLOCK(s16SemId);
531 
532     return bRet;
533 }
534 
MDrv_SEM_Unlock(eSemId SemId)535 MS_BOOL MDrv_SEM_Unlock(eSemId SemId)
536 {
537 #if (SEM_UTOPIA20)
538 	MS_U32 u32Ret;
539 	PSEM_UNLOCK_PARAM pSemUnlockParam = NULL;
540 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
541 		return 0;
542 	pSemUnlockParam = (PSEM_UNLOCK_PARAM)malloc(sizeof(SEM_UNLOCK_PARAM));
543 	pSemUnlockParam->u32SemID = SemId;
544 
545 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Unlock,(MS_U32*)pSemUnlockParam);
546 	free(pSemUnlockParam);
547 	return (MS_BOOL)u32Ret;
548 #else
549 	return MDrv_SEM_Unlock_U2K(SemId);
550 #endif
551 }
552 
553 //-------------------------------------------------------------------------------------------------
554 /// Attempt to delete a hardware semaphore
555 /// @param  SemId       \b IN: hardware semaphore ID
556 /// @return TRUE : succeed
557 /// @return FALSE : fail
558 //-------------------------------------------------------------------------------------------------
559 
MDrv_SEM_Delete_U2K(eSemId SemId)560 MS_BOOL MDrv_SEM_Delete_U2K(eSemId SemId)
561 {
562     if (!_gbSEMInitialized)
563     {
564         SEM_WARN("%s is called before init\n", __FUNCTION__);
565         return FALSE;
566     }
567 
568     MS_S16 s16SemId;
569     MS_BOOL bRet;
570 
571     bRet = FALSE;
572     SEM_DEBUG("Delete SemId = %d\n", SemId);
573     s16SemId = HAL_SEM_GetSemId(SemId);
574 
575     if(s16SemId < 0)
576     {
577         return FALSE;
578     }
579 
580      bRet = SEM_MUTEX_UNLOCK(s16SemId);
581 
582     if(bRet == FALSE)
583     {
584         SEM_ERROR("Release mutex %s failed\n", _SEMMutexName[s16SemId]);
585     }
586     else
587     {
588         bRet = HAL_SEM_Reset_Resource((MS_U8)s16SemId);
589 
590         if(bRet == FALSE)
591         {
592             SEM_ERROR("Reset hardware semaphore %d failed\n", s16SemId);
593         }
594     }
595 
596     return bRet;
597 }
598 
MDrv_SEM_Delete(eSemId SemId)599 MS_BOOL MDrv_SEM_Delete(eSemId SemId)
600 {
601 #if (SEM_UTOPIA20)
602 	MS_U32 u32Ret;
603 	PSEM_DELETE_PARAM pSemDeleteParam = NULL;
604 	if (FALSE == _MDrv_SEM_CheckUtopia20Open(&pInstantSem, 0, pAttributeSEM))
605 		return 0;
606 	pSemDeleteParam = (PSEM_DELETE_PARAM)malloc(sizeof(SEM_DELETE_PARAM));
607 	pSemDeleteParam->u32SemID = SemId;
608 
609 	u32Ret = UtopiaIoctl(pInstantSem,MDrv_CMD_SEM_Delete,(MS_U32*)pSemDeleteParam);
610 	free(pSemDeleteParam);
611 	return (MS_BOOL)u32Ret;
612 #else
613 	return MDrv_SEM_Delete_U2K(SemId);
614 #endif
615 }
616 
MDrv_SEM_SetPowerState(EN_POWER_MODE u16PowerState)617 MS_U32 MDrv_SEM_SetPowerState(EN_POWER_MODE u16PowerState)
618 {
619     static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
620     MS_U32 u32Return = UTOPIA_STATUS_FAIL;
621 
622     if (u16PowerState == E_POWER_SUSPEND)
623     {
624         _prev_u16PowerState = u16PowerState;
625         u32Return = UTOPIA_STATUS_SUCCESS; //SUSPEND_OK
626     }
627     else if (u16PowerState == E_POWER_RESUME)
628     {
629         if (_prev_u16PowerState == E_POWER_SUSPEND)
630         {
631 
632            //MDrv_SEM_Get_Resource -> Need to store the Regiser value?
633             _prev_u16PowerState = u16PowerState;
634             u32Return = UTOPIA_STATUS_SUCCESS; //RESUME_OK
635         }
636         else
637         {
638             ULOGE(TAG_SEM, "[%s,%5d]It is not suspended yet. We should not resume\n",__FUNCTION__,__LINE__);
639             u32Return = UTOPIA_STATUS_FAIL; //SUSPEND_FAILED
640         }
641     }
642     else
643     {
644         ULOGE(TAG_SEM, "[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
645         u32Return = UTOPIA_STATUS_FAIL;
646     }
647 
648     return u32Return;
649 }
650 
MDrv_SEM_SetDbgLevel(SEM_DbgLvl eLevel)651 MS_BOOL MDrv_SEM_SetDbgLevel(SEM_DbgLvl eLevel)
652 {
653     SEM_DEBUG("Debug level: %u\n", eLevel);
654 
655     _gSEMDbgLevel = eLevel;
656     return TRUE;
657 }
658