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 MsOS_nuttx.c
98 /// @brief MStar OS Wrapper
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
103 //-------------------------------------------------------------------------------------------------
104 // Include Files
105 //-------------------------------------------------------------------------------------------------
106 #include <stdio.h>
107 #include <stdlib.h>
108 #include <unistd.h>
109 #include "MsCommon.h"
110 #include "MsOS.h"
111 #include "debug.h"
112 #include "asmCPU.h"
113 #include "drvIRQ.h"
114 #include "string.h"
115 #include "halCHIP.h"
116 #include "halMMIO.h"
117 #include "MsVersion.h"
118 #ifdef CONFIG_UTOPIA_FRAMEWORK
119 #include "utopia.h"
120 #endif
121 //#include "hal_dcache.h"
122 #include "halMPool.h"
123 #include "ULog.h"
124 //-------------------------------------------------------------------------------------------------
125 // Local Defines
126 //------------------------------------------------------------------------------------------------
127 typedef signed char int8_t;
128 typedef unsigned char uint8_t;
129
130 typedef signed short int16_t;
131 typedef unsigned short uint16_t;
132
133 typedef signed int int32_t;
134 typedef unsigned int uint32_t;
135
136 typedef signed long long int64_t;
137 typedef unsigned long long uint64_t;
138
139 struct shm_entry {
140 char name[50]; // FIXME: potential bug
141 void* addr;
142 unsigned int size;
143 };
144
145 #define SHM_ARRAY_LENGTH 200
146 struct shm_array {
147 unsigned int count;
148 struct shm_entry shm_entry[SHM_ARRAY_LENGTH];
149 };
150
151 struct shm_array shm_array;
152
153 /* Values for the process shared (pshared) attribute */
154
155 #define PTHREAD_PROCESS_PRIVATE 0
156 #define PTHREAD_PROCESS_SHARED 1
157
158 // Combine 3-B prefix with s32ID = MSOS_ID_PREFIX | s32Id
159 // to avoid the kernel object being used before initialzed.
160 #define MSOS_ID_PREFIX 0x76540000
161 #define MSOS_ID_PREFIX_MASK 0xFFFF0000
162 #define MSOS_ID_MASK 0x0000FFFF //~MSOS_ID_PREFIX_MASK
163
164 #define TIMER_ABSTIME 1
165
166 #define CLOCK_REALTIME 0
167
168 #ifdef CONFIG_RTC
169 # define CLOCK_ACTIVETIME 1
170 #else
171 # define CLOCK_ACTIVETIME CLOCK_REALTIME
172 #endif
173
174
175
176
177 //-------------------------------------------------------------------------------------------------
178 // Global Variables
179 //-------------------------------------------------------------------------------------------------
180
181 typedef enum {
182 DCACHE_CLEAN = 0x1,
183 DCACHE_AREA_CLEAN = 0x2,
184 DCACHE_INVALIDATE = 0x3,
185 DCACHE_AREA_INVALIDATE = 0x4,
186 ICACHE_INVALIDATE = 0x5,
187 ICACHE_AREA_INVALIDATE = 0x6,
188 WRITE_BUFFER_DRAIN = 0x7,
189 DCACHE_CLEAN_INV = 0x8,
190 DCACHE_AREA_CLEAN_INV = 0x9,
191 L2CACHE_INVALIDATE = 0xA,
192 L2CACHE_AREA_INVALIDATE = 0xB,
193 L2CACHE_CLEAN = 0xC,
194 L2CACHE_AREA_CLEAN = 0xD,
195 L2CACHE_CLEAN_INV = 0xE,
196 L2CACHE_AREA_CLEAN_INV = 0xF
197 } t_cache_operation_id;
198
199 #define paddr_t MS_PHY
200 extern unsigned int cache_maintenance_l1(int op, void *va, size_t len);
201 extern unsigned int cache_maintenance_l2(int op, paddr_t pa, size_t len);
202 //-------------------------------------------------------------------------------------------------
203 // Local Variables
204 //-------------------------------------------------------------------------------------------------
205
206 //
207 // Variable-size Memory pool
208 //
209 //
210
211 //
212 // Task Management
213 //
214
215 //
216 // Mutex
217 //
218 enum mutex_value {
219 MUTEX_VALUE_UNLOCKED,
220 MUTEX_VALUE_LOCKED,
221 };
222
223 struct mutex {
224 enum mutex_value value;
225 size_t num_waiters; /* > 0 means that some thread(s) are waiting */
226 unsigned spin_lock; /* used when operating on this struct */
227 int handle; /* identifier of mutex passed to linux driver */
228 uint32_t tick; /* how recent the operation passed to */
229 /* linux driver is */
230 };
231 typedef struct mutex Optee_mutex;
232
233
234 typedef struct
235 {
236 MS_BOOL bUsed;
237 MS_BOOL bShared;
238 Optee_mutex stMutex;
239 MS_U8 u8Name[MAX_MUTEX_NAME_LENGTH];
240 } MsOS_Mutex_Info;
241
242 static MsOS_Mutex_Info _MsOS_Mutex_Info[MSOS_MUTEX_MAX];
243 static Optee_mutex _MsOS_Mutex_Mutex;
244 #define OPTEE_MUTEX_LOCK() mutex_lock(&_MsOS_Mutex_Mutex)
245 #define OPTEE_MUTEX_UNLOCK() mutex_unlock(&_MsOS_Mutex_Mutex)
246
247
248 //
249 // Semaphore
250 //
251 //typedef int pthread_condattr_t;
252 /**
253 struct Semaphore
254 {
255 uint32_t total_semcount; // >0 -> Num counts available
256 // <0 -> Num tasks waiting for semaphore
257 Optee_mutex Sem_mutex;
258
259 uint32_t current_count;
260
261 };
262 typedef struct Semaphore Optee_semaphore;
263
264 Optee_semaphore Semaphoreformutex[MSOS_SEMAPHORE_MAX];
265
266
267 typedef struct
268 {
269 MS_BOOL bUsed;
270 Optee_semaphore stSemaphore;
271 } MsOS_Semaphore_Info;
272
273 static MsOS_Semaphore_Info _MsOS_Semaphore_Info[MSOS_SEMAPHORE_MAX];
274 static Optee_mutex _MsOS_Semaphore_Mutex;
275
276 //#define OPTEE_Semaphore_LOCK() Semaphore_LOCK(&_MsOS_Semaphore_Mutex)
277 //#define OPTEE_Semaphore_UNLOCK() Semaphore_UNLOCK(&_MsOS_Semaphore_Mutex)
278 */
279 //
280 // Queue
281 //
282
283 //
284 // Timer
285 //
286 typedef struct {
287 uint32_t seconds;
288 uint32_t millis;
289 } TEE_Time;
290
291 #define TEE_Result MS_U32
292 extern TEE_Result tee_time_get_sys_time(TEE_Time *time);
293 extern void tee_time_wait(uint32_t milliseconds_delay);
294 extern void tee_time_wait_mstar(uint32_t usseconds_delay) __attribute__((weak));
295
296
297 //
298 // Interrupt
299 //
300 extern MS_BOOL up_interrupt_context(void);
301
302 //-------------------------------------------------------------------------------------------------
303 // Externel Function Prototypes
304 //-------------------------------------------------------------------------------------------------
305 typedef int (*xcpt_t)(int irq, void *context);
306 extern int hal_irq_attach(int irq, xcpt_t isr);
307 extern int hal_irq_detach(int irq);
308 extern void chip_irq_mask(unsigned irq);
309 extern void chip_irq_unmask(unsigned irq);
310
311
312 /* The following routines create, delete, lock and unlock mutexes. */
313 extern void mutex_init(Optee_mutex *mutex);
314 extern void mutex_lock(Optee_mutex *mutex);
315 extern void mutex_unlock(Optee_mutex *mutex);
316 extern MS_BOOL mutex_trylock(Optee_mutex *mutex);
317 extern void mutex_destroy(Optee_mutex *mutex);
318
319
320 /* Operations on condition variables */
321
322
323 static MSIF_Version _drv_msos_version = {
324 .DDI = { MSOS_DRV_VERSION },
325 };
326
327 //-------------------------------------------------------------------------------------------------
328 // Macros
329 //-------------------------------------------------------------------------------------------------
330 //#define MSOS_PRINT(fmt, args...) PRINTF("[MSOS_PRINT][%06d] " fmt, __LINE__, ## args)
331 #define MSOS_PRINT(fmt, args...)
332
333 #define HAS_FLAG(flag, bit) ((flag) & (bit))
334 #define SET_FLAG(flag, bit) ((flag)|= (bit))
335 #define RESET_FLAG(flag, bit) ((flag)&= (~(bit)))
336
337 #define _TimeAbs(stTime, u32IntMs) \
338 { \
339 clock_gettime(CLOCK_REALTIME, &(stTime)); \
340 if(u32IntMs>0) \
341 { \
342 MS_U32 u32Sec=(u32IntMs)/1000; \
343 (stTime).tv_sec+= u32Sec; \
344 (stTime).tv_nsec+= ((u32IntMs)- (1000* u32Sec))* 1000000; \
345 if ((stTime).tv_nsec>= 1000000000) \
346 { \
347 (stTime).tv_sec++; \
348 (stTime).tv_nsec-= 1000000000; \
349 } \
350 } \
351 }
352
353 // Macros optee
354 #define TEE_SUCCESS 0x00000000
355 extern int core_va2pa_helper(void *va, paddr_t *pa);
356 /* Special macro to avoid breaking strict aliasing rules */
357 #ifdef __GNUC__
358 #define core_va2pa(va, pa) (__extension__ ({ \
359 paddr_t _p; \
360 int _res = core_va2pa_helper((va), &_p); \
361 if (!_res) \
362 *(pa) = _p; \
363 _res; \
364 }))
365 #else
366 #define core_va2pa(pa, va) \
367 core_va2pa_helper((pa), (va))
368 #endif
369
370 ////////////////////////////////////////////////////////////////////////////////
371 /// @brief \b Function \b Name: MDrv_MSOS_GetLibVer
372 /// @brief \b Function \b Description: Show the MSOS driver version
373 /// @param ppVersion \b Out: Library version string
374 /// @return \b Result
375 ////////////////////////////////////////////////////////////////////////////////
MDrv_MSOS_GetLibVer(const MSIF_Version ** ppVersion)376 MS_BOOL MDrv_MSOS_GetLibVer(const MSIF_Version **ppVersion)
377 {
378 if (!ppVersion)
379 return FALSE;
380
381 *ppVersion = &_drv_msos_version;
382 return TRUE;
383 }
384
msos_strcpy(char * strDest,const char * strSrc)385 void msos_strcpy(char* strDest,const char* strSrc)
386 {
387 if((strDest == NULL) || (strSrc == NULL))
388 {
389 printf("\033[35mFunction = %s, Line = %d, msos_strcpy ERROR, strDest is NULL or strSrc is NULL\033[m\n", __PRETTY_FUNCTION__, __LINE__);
390 return;
391 }
392 while((*strDest++ = *strSrc++) != '\0');
393 }
msos_strncpy(char * dest,const char * source,int size_d)394 void msos_strncpy (char* dest, const char* source, int size_d)
395 {
396 if((dest == NULL) || (source == NULL))
397 return ;
398
399 int len = 0 ;
400 while (*source != '\0')
401 {
402 dest[len] = *source ;
403 len ++ ;
404 if (len >= size_d-1)
405 break ;
406 source ++ ;
407 }
408 dest[len] = 0 ;
409 }
410
411 //-------------------------------------------------------------------------------------------------
412 /// Initialize MsOS
413 /// @return TRUE : succeed
414 /// @return FALSE : fail
415 //-------------------------------------------------------------------------------------------------
MsOS_Init(void)416 MS_BOOL MsOS_Init (void)
417 {
418 MS_U32 u32I;
419 for( u32I=0; u32I<MSOS_MUTEX_MAX; u32I++)
420 {
421 _MsOS_Mutex_Info[u32I].bUsed = FALSE;
422 }
423 mutex_init(&_MsOS_Mutex_Mutex);
424
425 #ifdef CONFIG_UTOPIA_FRAMEWORK
426 UtopiaInit();
427 #endif
428
429 return true;
430 }
431
432 //
433 // Memory management
434 //
435 //-------------------------------------------------------------------------------------------------
436 /// Create a variable-size memory pool dynamically
437 /// @param u32PoolSize \b IN: pool size in bytes
438 /// @param u32MinAllocation \b IN: not used
439 /// @param pPoolAddr \b IN: starting address for the memory pool
440 /// @param eAttribute \b IN: only E_MSOS_FIFO - suspended in FIFO order
441 /// @param pPoolName \b IN: not used
442 /// @return >=0 : assigned memory pool ID
443 /// @return < 0 : fail
444 //-------------------------------------------------------------------------------------------------
MsOS_CreateMemoryPool(MS_U32 u32PoolSize,MS_U32 u32MinAllocation,void * pPoolAddr,MsOSAttribute eAttribute,char * pPoolName)445 MS_S32 MsOS_CreateMemoryPool (MS_U32 u32PoolSize,
446 MS_U32 u32MinAllocation,
447 void *pPoolAddr,
448 MsOSAttribute eAttribute,
449 char *pPoolName)
450 {
451 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
452 return -1;
453 }
454
455 //-------------------------------------------------------------------------------------------------
456 /// Delete a variable-size memory pool
457 /// @param s32PoolId \b IN: pool ID
458 /// @return TRUE : succeed
459 /// @return FALSE : fail
460 //-------------------------------------------------------------------------------------------------
MsOS_DeleteMemoryPool(MS_S32 s32PoolId)461 MS_BOOL MsOS_DeleteMemoryPool (MS_S32 s32PoolId)
462 {
463 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
464 return FALSE;
465 }
466
467 //-------------------------------------------------------------------------------------------------
468 /// Get the information of a variable-size memory pool
469 /// @param s32PoolId \b IN: memory pool ID
470 /// @param pPoolAddr \b OUT: holding the starting address for the memory pool
471 /// @param pu32PoolSize \b OUT: holding the total size of the memory pool
472 /// @param pu32FreeSize \b OUT: holding the available free size of the memory pool
473 /// @param pu32LargestFreeBlockSize \b OUT: holding the size of the largest free block
474 /// @return TRUE : succeed
475 /// @return FALSE : the pool has not been created
476 //-------------------------------------------------------------------------------------------------
MsOS_InfoMemoryPool(MS_S32 s32PoolId,void ** pPoolAddr,MS_U32 * pu32PoolSize,MS_U32 * pu32FreeSize,MS_U32 * pu32LargestFreeBlockSize)477 MS_BOOL MsOS_InfoMemoryPool (MS_S32 s32PoolId,
478 void **pPoolAddr,
479 MS_U32 *pu32PoolSize,
480 MS_U32 *pu32FreeSize,
481 MS_U32 *pu32LargestFreeBlockSize)
482 {
483 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
484 return FALSE;
485 }
486
487
488 //-------------------------------------------------------------------------------------------------
489 /// Allocate a memory block with 16-Byte aligned starting address from the variable-size memory pool
490 /// @param u32Size \b IN: request size
491 /// @param s32PoolId \b IN: memory pool ID
492 /// @return NULL : not enough available memory
493 /// @return Otherwise : pointer to the allocated memory block
494 //-------------------------------------------------------------------------------------------------
MsOS_AllocateMemory(MS_U32 u32Size,MS_S32 s32PoolId)495 void * MsOS_AllocateMemory (MS_U32 u32Size, MS_S32 s32PoolId)
496 {
497 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
498 return NULL;
499 }
500
501 //-------------------------------------------------------------------------------------------------
502 /// Free a memory block from the variable-size memory pool
503 /// @param pAddress \b IN: pointer to previously allocated memory block
504 /// @param s32PoolId \b IN: memory pool ID
505 /// @return TRUE : succeed
506 /// @return FALSE : fail
507 //-------------------------------------------------------------------------------------------------
MsOS_FreeMemory(void * pAddress,MS_S32 s32PoolId)508 MS_BOOL MsOS_FreeMemory (void *pAddress, MS_S32 s32PoolId)
509 {
510 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
511 return TRUE;
512 }
513
514
515 //-------------------------------------------------------------------------------------------------
516 /// Reallocate a block of memory with 4-byte aligned start address from the variable-size memory pool
517 /// @param pOrgAddress \b IN: points to the beginning of the original memory block
518 /// @param u32NewSize \b IN: size of new memory block
519 /// @param s32PoolId \b IN: memory pool ID
520 /// @return NULL : not enough available memory to expand the block or u32NewSize == 0 && pOrgAddress != NULL
521 /// @return Otherwise : pointer to the reallocated (and possibly moved) memory block
522 // @note reference realloc in malloc.cxx
523 //-------------------------------------------------------------------------------------------------
MsOS_ReallocateMemory(void * pOrgAddress,MS_U32 u32NewSize,MS_S32 s32PoolId)524 void * MsOS_ReallocateMemory (void *pOrgAddress, MS_U32 u32NewSize, MS_S32 s32PoolId)
525 {
526 // PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
527 return NULL;
528 }
529
530 //-------------------------------------------------------------------------------------------------
531 /// Create a fixed-size memory pool dynamically
532 /// @param u32PoolSize \b IN: pool size in bytes
533 /// @param u32BlockSize \b IN: fixed block size for each allocated block in the pool
534 /// @param pPoolAddr \b IN: starting address for the memory pool
535 /// @param eAttribute \b IN: only E_MSOS_FIFO - suspended in FIFO order
536 /// @param pPoolName \b IN: not used
537 /// @return >=0 : assigned memory pool ID
538 /// @return < 0 : fail
539 //-------------------------------------------------------------------------------------------------
MsOS_CreateFixSizeMemoryPool(MS_U32 u32PoolSize,MS_U32 u32BlockSize,void * pPoolAddr,MsOSAttribute eAttribute,char * pPoolName)540 MS_S32 MsOS_CreateFixSizeMemoryPool (MS_U32 u32PoolSize,
541 MS_U32 u32BlockSize,
542 void *pPoolAddr,
543 MsOSAttribute eAttribute,
544 char *pPoolName)
545 {
546 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
547 return -1;
548 }
549
550 //-------------------------------------------------------------------------------------------------
551 /// Delete a fixed-size memory pool
552 /// @param s32PoolId \b IN: pool ID
553 /// @return TRUE : succeed
554 /// @return FALSE : fail
555 //-------------------------------------------------------------------------------------------------
MsOS_DeleteFixSizeMemoryPool(MS_S32 s32PoolId)556 MS_BOOL MsOS_DeleteFixSizeMemoryPool (MS_S32 s32PoolId)
557 {
558 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
559 return FALSE;
560 }
561
562 //-------------------------------------------------------------------------------------------------
563 /// Get the information of a fixed-size memory pool
564 /// @param s32PoolId \b IN: memory pool ID
565 /// @param pPoolAddr \b OUT: holding the starting address for the memory pool
566 /// @param pu32PoolSize \b OUT: holding the total size of the memory pool
567 /// @param pu32FreeSize \b OUT: holding the available free size of the memory pool
568 /// @param pu32LargestFreeBlockSize \b OUT: holding the size of the largest free block
569 /// @return TRUE : succeed
570 /// @return FALSE : the pool has not been created
571 //-------------------------------------------------------------------------------------------------
MsOS_InfoFixSizeMemoryPool(MS_S32 s32PoolId,void ** pPoolAddr,MS_U32 * pu32PoolSize,MS_U32 * pu32FreeSize,MS_U32 * pu32LargestFreeBlockSize)572 MS_BOOL MsOS_InfoFixSizeMemoryPool (MS_S32 s32PoolId,
573 void **pPoolAddr,
574 MS_U32 *pu32PoolSize,
575 MS_U32 *pu32FreeSize,
576 MS_U32 *pu32LargestFreeBlockSize)
577 {
578 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
579 return FALSE;
580 }
581
582 //-------------------------------------------------------------------------------------------------
583 /// Allocate a memory block from the fixed-size memory pool
584 /// @param s32PoolId \b IN: memory pool ID
585 /// @return NULL : not enough available memory
586 /// @return Otherwise : pointer to the allocated memory block
587 /// @note watch out for alignment if u32BlockSize is not a multiple of 4
588 //-------------------------------------------------------------------------------------------------
MsOS_AllocateFixSizeMemory(MS_S32 s32PoolId)589 void * MsOS_AllocateFixSizeMemory (MS_S32 s32PoolId)
590 {
591 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
592 return NULL;
593 }
594
595 //-------------------------------------------------------------------------------------------------
596 /// Free a memory block from the fixed-size memory pool
597 /// @param pAddress \b IN: pointer to previously allocated memory block
598 /// @param s32PoolId \b IN: memory pool ID
599 /// @return TRUE : succeed
600 /// @return FALSE : fail
601 //-------------------------------------------------------------------------------------------------
MsOS_FreeFixSizeMemory(void * pAddress,MS_S32 s32PoolId)602 MS_BOOL MsOS_FreeFixSizeMemory (void *pAddress, MS_S32 s32PoolId)
603 {
604 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
605 return FALSE;
606 }
607
608
609 //
610 // Task
611 //
612 //-------------------------------------------------------------------------------------------------
613 /// Create a task
614 /// @param pTaskEntry \b IN: task entry point
615 /// @param u32TaskEntryData \b IN: task entry data: a pointer to some static data, or a
616 /// small integer, or NULL if the task does not require any additional data.
617 /// @param eTaskPriority \b IN: task priority
618 /// @param bAutoStart \b IN: start immediately or later
619 /// @param pStackBase \b IN: task stack
620 /// @param u32StackSize \b IN: stack size
621 /// @param pTaskName \b IN: task name
622 /// @return >=0 : assigned Task ID
623 /// @return < 0 : fail
624 //-------------------------------------------------------------------------------------------------
625 #define CONFIG_USERMAIN_STACKSIZE (0x40000)
626 typedef int (*main_t)(int argc, char *argv[]);
627 extern int task_create(const char *name, int priority,
628 int stack_size, main_t entry, const char *argv);
629
MsOS_CreateTask(TaskEntry pTaskEntry,MS_VIRT u32TaskEntryData,TaskPriority eTaskPriority,MS_BOOL bAutoStart,void * pStackBase,MS_U32 u32StackSize,char * pTaskName)630 MS_S32 MsOS_CreateTask (TaskEntry pTaskEntry,
631 MS_VIRT u32TaskEntryData,
632 TaskPriority eTaskPriority,
633 MS_BOOL bAutoStart,
634 void *pStackBase,
635 MS_U32 u32StackSize,
636 char *pTaskName)
637 {
638 #if 0
639 /* optee does not support this */
640 task_create(pTaskName, eTaskPriority, CONFIG_USERMAIN_STACKSIZE
641 , (main_t)pTaskEntry, (const char*)u32TaskEntryData);
642 #endif
643
644 return TRUE;
645 }
646
647 //-------------------------------------------------------------------------------------------------
648 /// Delete a previously created task
649 /// @param s32TaskId \b IN: task ID
650 /// @return TRUE : succeed
651 /// @return FALSE : fail
652 //-------------------------------------------------------------------------------------------------
MsOS_DeleteTask(MS_S32 s32TaskId)653 MS_BOOL MsOS_DeleteTask (MS_S32 s32TaskId)
654 {
655 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
656 return FALSE;
657 }
658
659 //-------------------------------------------------------------------------------------------------
660 /// Yield the execution right to ready tasks with "the same" priority
661 /// @return None
662 //-------------------------------------------------------------------------------------------------
MsOS_YieldTask(void)663 void MsOS_YieldTask (void)
664 {
665 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
666 }
667
668 //-------------------------------------------------------------------------------------------------
669 /// Suspend the calling task for u32Ms milliseconds
670 /// @param u32Ms \b IN: delay 1 ~ MSOS_WAIT_FOREVER ms
671 /// @return None
672 /// @note Not allowed in interrupt context; otherwise, exception will occur.
673 //-------------------------------------------------------------------------------------------------
MsOS_DelayTask(MS_U32 u32Ms)674 void MsOS_DelayTask (MS_U32 u32Ms)
675 {
676 if(tee_time_wait_mstar) tee_time_wait_mstar(u32Ms * 1000);
677 else tee_time_wait(u32Ms);
678 }
679
680 //-------------------------------------------------------------------------------------------------
681 /// Delay for u32Us microseconds
682 /// @param u32Us \b IN: delay 0 ~ 999 us
683 /// @return None
684 /// @note implemented by "busy waiting". Plz call MsOS_DelayTask directly for ms-order delay
685 //-------------------------------------------------------------------------------------------------
MsOS_DelayTaskUs(MS_U32 u32Us)686 void MsOS_DelayTaskUs (MS_U32 u32Us)
687 {
688 if(tee_time_wait_mstar) tee_time_wait_mstar(u32Us);
689 else tee_time_wait(u32Us/1000);
690 }
691
692 //-------------------------------------------------------------------------------------------------
693 /// Delay Poll for u32Us microseconds
694 /// @param u32Us \b IN: delay 0 ~ 999 us
695 /// @return None
696 /// @note implemented by "busy waiting". Plz call MsOS_DelayTask directly for ms-order delay
697 //-------------------------------------------------------------------------------------------------
MsOS_DelayTaskUs_Poll(MS_U32 u32Us)698 void MsOS_DelayTaskUs_Poll(MS_U32 u32Us)
699 {
700 MsOS_DelayTaskUs(u32Us);
701 }
702
703 //-------------------------------------------------------------------------------------------------
704 /// Resume the specified suspended task
705 /// @param s32TaskId \b IN: Task ID
706 /// @return TRUE : succeed
707 /// @return FALSE : fail
708 /// @note This API is not supported in Linux
709 //-------------------------------------------------------------------------------------------------
MsOS_ResumeTask(MS_S32 s32TaskId)710 MS_BOOL MsOS_ResumeTask (MS_S32 s32TaskId)
711 {
712 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
713 return FALSE;
714 }
715
716 //-------------------------------------------------------------------------------------------------
717 /// Suspend the specified task
718 /// @param s32TaskId \b IN: Task ID
719 /// @return TRUE : succeed
720 /// @return FALSE : fail
721 /// @note This API is not supported in Linux
722 //-------------------------------------------------------------------------------------------------
MsOS_SuspendTask(MS_S32 s32TaskId)723 MS_BOOL MsOS_SuspendTask (MS_S32 s32TaskId)
724 {
725 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
726 return FALSE;
727 }
728
729 //-------------------------------------------------------------------------------------------------
730 /// Get a task information
731 /// @param s32TaskId \b IN: task ID
732 /// @param peTaskPriority \b OUT: ptr to task priority
733 /// @param pStackBase \b OUT: ptr to stack base
734 /// @param pu32StackSize \b OUT: ptr to stack size
735 /// @param pTaskName \b OUT: ptr to task name
736 /// @return TRUE : succeed
737 /// @return FALSE : the task has not been created
738 //-------------------------------------------------------------------------------------------------
MsOS_InfoTask(MS_S32 s32TaskId,TaskPriority * peTaskPriority,void * pStackBase,MS_U32 * pu32StackSize,MS_U32 * pu32StackUsed,char * pTaskName)739 MS_BOOL MsOS_InfoTask (MS_S32 s32TaskId,
740 TaskPriority *peTaskPriority,
741 void* pStackBase,
742 MS_U32* pu32StackSize,
743 MS_U32* pu32StackUsed,
744 char *pTaskName)
745 {
746 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
747 return FALSE;
748 }
749
750 //-------------------------------------------------------------------------------------------------
751 /// Get all task ID
752 /// @param ps32TaskIdList \b IN/OUT: the memory for the all task ID
753 /// @return number of task created
754 //-------------------------------------------------------------------------------------------------
MsOS_InfoTaskList(MS_S32 * ps32TaskIdList)755 MS_U32 MsOS_InfoTaskList (MS_S32* ps32TaskIdList)
756 {
757 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
758 return 0;
759 }
760
761
762 //-------------------------------------------------------------------------------------------------
763 /// Get current task ID
764 /// @return >=0 : current task ID
765 /// @return <0 : fail
766 //-------------------------------------------------------------------------------------------------
MsOS_InfoTaskID(void)767 MS_S32 MsOS_InfoTaskID (void)
768 {
769 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
770 return -1;
771 }
772 //-------------------------------------------------------------------------------------------------
773 /// Get thread ID of current thread/process in Linux(Kernel)
774 /// @return : current thread ID
775 //-------------------------------------------------------------------------------------------------
MsOS_GetOSThreadID(void)776 MS_S32 MsOS_GetOSThreadID (void)
777 {
778 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
779 return MsOS_InfoTaskID();
780 }
781
782
783 //
784 // Mutex
785 //
786 //-------------------------------------------------------------------------------------------------
787 /// Create a mutex in the unlocked state
788 /// @param eAttribute \b IN: E_MSOS_FIFO: suspended in FIFO order
789 /// @param pMutexName \b IN: mutex name
790 /// @return >=0 : assigned mutex Id
791 /// @return <0 : fail
792 /// @note A mutex has the concept of an owner, whereas a semaphore does not.
793 /// A mutex provides priority inheritance protocol against proiorty inversion, whereas a binary semaphore does not.
794 //-------------------------------------------------------------------------------------------------
MsOS_CreateMutex(MsOSAttribute eAttribute,char * pMutexName1,MS_U32 u32Flag)795 MS_S32 MsOS_CreateMutex ( MsOSAttribute eAttribute, char *pMutexName1, MS_U32 u32Flag)
796 {
797 MS_S32 s32Id, s32LstUnused = MSOS_MUTEX_MAX;
798 MS_S32 s32Prop = (MSOS_PROCESS_PRIVATE == u32Flag)? PTHREAD_PROCESS_PRIVATE: PTHREAD_PROCESS_SHARED;
799 MS_U8 pMutexName[MAX_MUTEX_NAME_LENGTH];
800 MS_U32 u32MaxLen;
801
802 if (NULL == pMutexName1)
803 {
804 return -1L;
805 }
806 if (strlen(pMutexName1) >= (MAX_MUTEX_NAME_LENGTH-1))
807 {
808 printf("%s: Warning strlen(%s) is longer than MAX_MUTEX_NAME_LENGTH(%d). Oversize char will be discard.\n",
809 __FUNCTION__,pMutexName1,MAX_MUTEX_NAME_LENGTH);
810 }
811 if (0 == (u32MaxLen = MIN(strlen(pMutexName1), (MAX_MUTEX_NAME_LENGTH-1))))
812 {
813 return -1L;
814 }
815 msos_strncpy((char*)pMutexName, (const char*)pMutexName1, u32MaxLen);
816 pMutexName[u32MaxLen] = '\0';
817
818 OPTEE_MUTEX_LOCK();
819 for (s32Id=0;s32Id<MSOS_MUTEX_MAX;s32Id++)
820 {
821
822 if(TRUE == _MsOS_Mutex_Info[s32Id].bUsed)
823 {
824 if (0== strcmp((const char*)_MsOS_Mutex_Info[s32Id].u8Name, (const char*)pMutexName))
825 {
826 break;
827 }
828 }
829
830 if (FALSE==_MsOS_Mutex_Info[s32Id].bUsed && MSOS_MUTEX_MAX==s32LstUnused)
831 {
832 s32LstUnused = s32Id;
833 }
834 }
835
836 if ((MSOS_MUTEX_MAX==s32Id) && (MSOS_MUTEX_MAX>s32LstUnused))
837 {
838 _MsOS_Mutex_Info[s32LstUnused].bUsed = TRUE;
839 msos_strncpy((char*)_MsOS_Mutex_Info[s32LstUnused].u8Name, (const char*)pMutexName,MIN(strlen((const char*)pMutexName),(MAX_MUTEX_NAME_LENGTH-1)));
840 mutex_init(&_MsOS_Mutex_Info[s32LstUnused].stMutex);
841 s32Id = s32LstUnused;
842 }
843 OPTEE_MUTEX_UNLOCK();
844
845 if(MSOS_MUTEX_MAX <= s32Id)
846 {
847 return -1L;
848 }
849
850 s32Id |= MSOS_ID_PREFIX;
851 return s32Id;
852 }
853
854 //-------------------------------------------------------------------------------------------------
855 /// Delete the specified mutex
856 /// @param s32MutexId \b IN: mutex ID
857 /// @return TRUE : succeed
858 /// @return FALSE : fail
859 /// @note It is important that the mutex be in the unlocked state when it is
860 /// destroyed, or else the behavior is undefined.
861 //-------------------------------------------------------------------------------------------------
MsOS_DeleteMutex(MS_S32 s32MutexId)862 MS_BOOL MsOS_DeleteMutex (MS_S32 s32MutexId)
863 {
864 if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
865 {
866 return FALSE;
867 }
868 else
869 {
870 s32MutexId &= MSOS_ID_MASK;
871 }
872
873 if (s32MutexId >= MSOS_MUTEX_MAX)
874 return FALSE;
875
876 OPTEE_MUTEX_LOCK();
877 MS_ASSERT(_MsOS_Mutex_Info[s32MutexId].bUsed);
878 mutex_destroy(&_MsOS_Mutex_Info[s32MutexId].stMutex);
879 _MsOS_Mutex_Info[s32MutexId].bUsed = FALSE;
880 memset(_MsOS_Mutex_Info[s32MutexId].u8Name,0x00,sizeof(_MsOS_Mutex_Info[s32MutexId].u8Name));
881 OPTEE_MUTEX_UNLOCK();
882
883 return TRUE;
884 }
885
886 //-------------------------------------------------------------------------------------------------
887 /// Attempt to lock a mutex
888 /// @param s32MutexId \b IN: mutex ID
889 /// @param u32WaitMs \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the mutex is locked
890 /// @return TRUE : succeed
891 /// @return FALSE : fail
892 //-------------------------------------------------------------------------------------------------
MsOS_ObtainMutex(MS_S32 s32MutexId,MS_U32 u32WaitMs)893 MS_BOOL MsOS_ObtainMutex (MS_S32 s32MutexId, MS_U32 u32WaitMs)
894 {
895
896 MS_BOOL bRet = FALSE;
897
898 if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
899 {
900 return FALSE;
901 }
902 else
903 {
904 s32MutexId &= MSOS_ID_MASK;
905 }
906
907 if (s32MutexId >= MSOS_MUTEX_MAX)
908 return FALSE;
909
910 MS_ASSERT(_MsOS_Mutex_Info[s32MutexId].bUsed);
911
912 if (u32WaitMs==MSOS_WAIT_FOREVER) //blocking wait
913 {
914 mutex_lock(&(_MsOS_Mutex_Info[s32MutexId].stMutex));
915 bRet = TRUE;
916
917 }
918 else if (u32WaitMs==0) //non-blocking
919 {
920
921 mutex_trylock(&_MsOS_Mutex_Info[s32MutexId].stMutex);
922 bRet = TRUE;
923 }
924 else //blocking wait with timeout
925 {
926 printf( "not support\n");
927 bRet = FALSE;
928 }
929
930 return bRet;
931 }
932
933 //-------------------------------------------------------------------------------------------------
934 /// Attempt to unlock a mutex
935 /// @param s32MutexId \b IN: mutex ID
936 /// @return TRUE : succeed
937 /// @return FALSE : fail
938 /// @note Only the owner thread of the mutex can unlock it.
939 //-------------------------------------------------------------------------------------------------
MsOS_ReleaseMutex(MS_S32 s32MutexId)940 MS_BOOL MsOS_ReleaseMutex (MS_S32 s32MutexId)
941 {
942 MS_BOOL bRet = TRUE;
943 if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
944 {
945 return FALSE;
946 }
947 else
948 {
949 s32MutexId &= MSOS_ID_MASK;
950 }
951
952 if (s32MutexId >= MSOS_MUTEX_MAX)
953 return FALSE;
954
955 MS_ASSERT(_MsOS_Mutex_Info[s32MutexId].bUsed);
956 mutex_unlock(&_MsOS_Mutex_Info[s32MutexId].stMutex);
957
958 return bRet;
959 }
960
961 //-------------------------------------------------------------------------------------------------
962 // Get a mutex informaton
963 // @param s32MutexId \b IN: mutex ID
964 // @param peAttribute \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
965 // @param pMutexName \b OUT: ptr to mutex name
966 // @return TRUE : succeed
967 // @return FALSE : the mutex has not been created.
968 //-------------------------------------------------------------------------------------------------
MsOS_InfoMutex(MS_S32 s32MutexId,MsOSAttribute * peAttribute,char * pMutexName)969 MS_BOOL MsOS_InfoMutex (MS_S32 s32MutexId, MsOSAttribute *peAttribute, char *pMutexName)
970 {
971 if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
972 {
973 return FALSE;
974 }
975 else
976 {
977 s32MutexId &= MSOS_ID_MASK;
978 }
979
980 if(_MsOS_Mutex_Info[s32MutexId].bUsed == TRUE)
981 {
982 //ToDo: extend _MsOS_Mutex_Info structure ?
983 *peAttribute = E_MSOS_FIFO; //only FIFO for eCos
984 // @FIXME: linux porting
985 // UTL_strcpy(pMutexName, "ABC");
986 msos_strncpy(pMutexName, (const char*)_MsOS_Mutex_Info[s32MutexId].u8Name,MIN(strlen((const char*)_MsOS_Mutex_Info[s32MutexId].u8Name),(MAX_MUTEX_NAME_LENGTH-1)));
987 return TRUE;
988 }
989 else
990 {
991 return FALSE;
992 }
993 }
994
995 //
996 // Semaphore
997 //
998 //-------------------------------------------------------------------------------------------------
999 /// Create a semaphore
1000 /// @param u32InitCnt \b IN: initial semaphore value
1001 /// @param eAttribute \b IN: E_MSOS_FIFO suspended in FIFO order
1002 /// @param pSemaphoreName \b IN: semaphore name
1003 /// @return >=0 : assigned Semaphore Id
1004 /// @return <0 : fail
1005 /// @note A semaphore does not have the concept of an owner; it is possible for one thread to lock a
1006 /// binary semaphore and another thread to unlock it.
1007 //-------------------------------------------------------------------------------------------------
1008
MsOS_CreateSemaphore(MS_U32 u32InitCnt,MsOSAttribute eAttribute,char * pSemaphoreName)1009 MS_S32 MsOS_CreateSemaphore (MS_U32 u32InitCnt,
1010 MsOSAttribute eAttribute,
1011 char *pSemaphoreName)
1012 {
1013
1014 return MsOS_CreateMutex(E_MSOS_FIFO, pSemaphoreName, MSOS_PROCESS_SHARED);
1015 /*
1016 MS_S32 s32Id;
1017 MS_BOOL result=0;
1018
1019 mutex_lock(&_MsOS_Semaphore_Mutex);
1020 for(s32Id=0;s32Id<MSOS_SEMAPHORE_MAX;s32Id++)
1021 {
1022 if(_MsOS_Semaphore_Info[s32Id].bUsed == FALSE)
1023 {
1024 _MsOS_Semaphore_Info[s32Id].bUsed = TRUE;
1025 break;
1026 }
1027 }
1028
1029 if(s32Id >= MSOS_SEMAPHORE_MAX)
1030 {
1031 mutex_unlock(&_MsOS_Semaphore_Mutex);
1032 return -1;
1033 }
1034 mutex_init(&Semaphoreformutex[s32Id].Sem_mutex);
1035 Semaphoreformutex[s32Id].current_count=u32InitCnt;
1036 Semaphoreformutex[s32Id].total_semcount=u32InitCnt;
1037 s32Id |= MSOS_ID_PREFIX;
1038 mutex_unlock(&_MsOS_Semaphore_Mutex);
1039 return s32Id;
1040 */
1041 }
1042
1043 //-------------------------------------------------------------------------------------------------
1044 /// Delete the specified semaphore
1045 /// @param s32SemaphoreId \b IN: semaphore ID
1046 /// @return TRUE : succeed
1047 /// @return FALSE : fail
1048 /// @note It is important that there are not any threads waiting on the semaphore
1049 /// when this function is called or the behavior is undefined.
1050 //-------------------------------------------------------------------------------------------------
MsOS_DeleteSemaphore(MS_S32 s32SemaphoreId)1051 MS_BOOL MsOS_DeleteSemaphore (MS_S32 s32SemaphoreId)
1052 {
1053 return MsOS_DeleteSemaphore(s32SemaphoreId);
1054 /*
1055 if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1056 {
1057 return FALSE;
1058 }
1059 else
1060 {
1061 s32SemaphoreId &= MSOS_ID_MASK;
1062 }
1063 while (Semaphoreformutex[s32SemaphoreId].current_count != Semaphoreformutex[s32SemaphoreId].total_semcount)
1064 {
1065 if (Semaphoreformutex[s32SemaphoreId].current_count < Semaphoreformutex[s32SemaphoreId].total_semcount)
1066 {
1067 MsOS_ReleaseSemaphore( (s32SemaphoreId|s32SemaphoreId));
1068 }
1069 }
1070 mutex_lock(&_MsOS_Semaphore_Mutex);
1071 _MsOS_Semaphore_Info[s32SemaphoreId].bUsed = FALSE;
1072 mutex_unlock(&_MsOS_Semaphore_Mutex);
1073 return TRUE;
1074 */
1075 }
1076
1077 //-------------------------------------------------------------------------------------------------
1078 /// Attempt to decrement a semaphore count
1079 /// @param s32SemaphoreId \b IN: semaphore ID
1080 /// @param u32WaitMs \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the semaphore count = 0
1081 /// @return TRUE : succeed
1082 /// @return FALSE : fail
1083 //-------------------------------------------------------------------------------------------------
MsOS_ObtainSemaphore(MS_S32 s32SemaphoreId,MS_U32 u32WaitMs)1084 MS_BOOL MsOS_ObtainSemaphore (MS_S32 s32SemaphoreId, MS_U32 u32WaitMs)
1085 {
1086 return MsOS_ObtainMutex(s32SemaphoreId,u32WaitMs);
1087 /*
1088 u32WaitMs=MSOS_WAIT_FOREVER;
1089
1090 if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1091 {
1092 return FALSE;
1093 }
1094 else
1095 {
1096 s32SemaphoreId &= MSOS_ID_MASK;
1097 }
1098
1099 mutex_lock(&_MsOS_Semaphore_Mutex);
1100
1101 if (_MsOS_Semaphore_Info[s32SemaphoreId].bUsed == FALSE)
1102 {
1103 mutex_unlock(&_MsOS_Semaphore_Mutex);
1104 return FALSE;
1105 }
1106
1107 if (u32WaitMs==MSOS_WAIT_FOREVER) //blocking wait
1108 {
1109 Semaphoreformutex[s32SemaphoreId].current_count--;
1110 if(Semaphoreformutex[s32SemaphoreId].current_count<=0)
1111 {
1112 mutex_lock(&Semaphoreformutex[s32SemaphoreId].Sem_mutex);
1113 }
1114 }
1115 mutex_unlock(&_MsOS_Semaphore_Mutex);
1116 return TRUE;
1117 */
1118 }
1119
1120 //-------------------------------------------------------------------------------------------------
1121 /// Increase a semaphore count
1122 /// @param s32SemaphoreId \b IN: semaphore ID
1123 /// @return TRUE : succeed
1124 /// @return FALSE : fail
1125 /// @note It's possible for any thread to increase the semaphore count
1126 //-------------------------------------------------------------------------------------------------
MsOS_ReleaseSemaphore(MS_S32 s32SemaphoreId)1127 MS_BOOL MsOS_ReleaseSemaphore (MS_S32 s32SemaphoreId)
1128 {
1129 return MsOS_ReleaseMutex(s32SemaphoreId);
1130 /*
1131 if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1132 {
1133 return FALSE;
1134 }
1135 else
1136 {
1137 s32SemaphoreId &= MSOS_ID_MASK;
1138 }
1139 mutex_lock(&_MsOS_Semaphore_Mutex);
1140
1141 if (_MsOS_Semaphore_Info[s32SemaphoreId].bUsed == FALSE)
1142 {
1143 mutex_unlock(&_MsOS_Semaphore_Mutex);
1144 return FALSE;
1145 }
1146
1147 if((Semaphoreformutex[s32SemaphoreId].current_count)<=0)
1148 {
1149 mutex_unlock(&Semaphoreformutex[s32SemaphoreId].Sem_mutex);
1150 }
1151 Semaphoreformutex[s32SemaphoreId].current_count++;
1152 mutex_unlock(&_MsOS_Semaphore_Mutex);
1153
1154 return TRUE;
1155 */
1156 }
1157
1158 //-------------------------------------------------------------------------------------------------
1159 // Get a semaphore informaton
1160 // @param s32SemaphoreId \b IN: semaphore ID
1161 // @param pu32InitCnt \b OUT: ptr to initial semaphore value
1162 // @param peAttribute \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
1163 // @param pSemaphoreName \b OUT: ptr to semaphore name
1164 // @return TRUE : succeed
1165 // @return FALSE : the semaphore has not been created.
1166 //-------------------------------------------------------------------------------------------------
MsOS_InfoSemaphore(MS_S32 s32SemaphoreId,MS_U32 * pu32InitCnt,MsOSAttribute * peAttribute,char * pSemaphoreName)1167 MS_BOOL MsOS_InfoSemaphore (MS_S32 s32SemaphoreId, MS_U32 *pu32InitCnt, MsOSAttribute *peAttribute, char *pSemaphoreName)
1168 {
1169 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1170 return TRUE;
1171 }
1172
1173
1174 //
1175 // Event management
1176 //
1177 //-------------------------------------------------------------------------------------------------
1178 /// Create an event group
1179 /// @param pEventName \b IN: event group name
1180 /// @return >=0 : assigned Event Id
1181 /// @return <0 : fail
1182 //-------------------------------------------------------------------------------------------------
MsOS_CreateEventGroup(char * pEventName)1183 MS_S32 MsOS_CreateEventGroup (char *pEventName)
1184 {
1185 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1186 return TRUE;
1187 }
1188
1189 //-------------------------------------------------------------------------------------------------
1190 /// Delete the event group
1191 /// @param s32EventGroupId \b IN: event group ID
1192 /// @return TRUE : succeed
1193 /// @return FALSE : fail, sb is waiting for the event flag
1194 /// @note event group that are being waited on must not be deleted
1195 //-------------------------------------------------------------------------------------------------
MsOS_DeleteEventGroup(MS_S32 s32EventGroupId)1196 MS_BOOL MsOS_DeleteEventGroup (MS_S32 s32EventGroupId)
1197 {
1198 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1199 return TRUE;
1200 }
1201
1202 //-------------------------------------------------------------------------------------------------
1203 /// Set the event flag (bitwise OR w/ current value) in the specified event group
1204 /// @param s32EventGroupId \b IN: event group ID
1205 /// @param u32EventFlag \b IN: event flag value
1206 /// @return TRUE : succeed
1207 /// @return FALSE : fail
1208 //-------------------------------------------------------------------------------------------------
MsOS_SetEvent(MS_S32 s32EventGroupId,MS_U32 u32EventFlag)1209 MS_BOOL MsOS_SetEvent (MS_S32 s32EventGroupId, MS_U32 u32EventFlag)
1210 {
1211 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1212 return TRUE;
1213
1214 }
1215
1216 //-------------------------------------------------------------------------------------------------
1217 /// Clear the specified event flag (bitwise XOR operation) in the specified event group
1218 /// @param s32EventGroupId \b IN: event group ID
1219 /// @param u32EventFlag \b IN: event flag value
1220 /// @returnTRUE : succeed
1221 /// @return FALSE : fail
1222 //-------------------------------------------------------------------------------------------------
MsOS_ClearEvent(MS_S32 s32EventGroupId,MS_U32 u32EventFlag)1223 MS_BOOL MsOS_ClearEvent (MS_S32 s32EventGroupId, MS_U32 u32EventFlag)
1224 {
1225 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1226 return TRUE;
1227 }
1228
1229 //-------------------------------------------------------------------------------------------------
1230 /// Wait for the specified event flag combination from the event group
1231 /// @param s32EventGroupId \b IN: event group ID
1232 /// @param u32WaitEventFlag \b IN: wait event flag value
1233 /// @param pu32RetrievedEventFlag \b OUT: retrieved event flag value
1234 /// @param eWaitMode \b IN: E_AND/E_OR/E_AND_CLEAR/E_OR_CLEAR
1235 /// @param u32WaitMs \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the event is not ready
1236 /// @return TRUE : succeed
1237 /// @return FALSE : fail
1238 //-------------------------------------------------------------------------------------------------
MsOS_WaitEvent(MS_S32 s32EventGroupId,MS_U32 u32WaitEventFlag,MS_U32 * pu32RetrievedEventFlag,EventWaitMode eWaitMode,MS_U32 u32WaitMs)1239 MS_BOOL MsOS_WaitEvent (MS_S32 s32EventGroupId,
1240 MS_U32 u32WaitEventFlag,
1241 MS_U32 *pu32RetrievedEventFlag,
1242 EventWaitMode eWaitMode,
1243 MS_U32 u32WaitMs)
1244 { //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1245 return TRUE;
1246 }
1247
1248 //
1249 // Timer management
1250 //
1251 //-------------------------------------------------------------------------------------------------
1252 /// Create a Timer
1253 /// @param pTimerCb \b IN: timer callback function
1254 /// @param u32FirstTimeMs \b IN: first ms for timer expiration
1255 /// @param u32PeriodTimeMs \b IN: periodic ms for timer expiration after first expiration
1256 /// 0: one shot timer
1257 /// @param bStartTimer \b IN: TRUE: activates the timer after it is created
1258 /// FALSE: leaves the timer disabled after it is created
1259 /// @param pTimerName \b IN: Timer name (not used by eCos)
1260 /// @return >=0 : assigned Timer ID
1261 /// <0 : fail
1262 //-------------------------------------------------------------------------------------------------
MsOS_CreateTimer(TimerCb pTimerCb,MS_U32 u32FirstTimeMs,MS_U32 u32PeriodTimeMs,MS_BOOL bStartTimer,char * pTimerName)1263 MS_S32 MsOS_CreateTimer (TimerCb pTimerCb,
1264 MS_U32 u32FirstTimeMs,
1265 MS_U32 u32PeriodTimeMs,
1266 MS_BOOL bStartTimer,
1267 char *pTimerName)
1268 {
1269 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1270 return TRUE;
1271 }
1272
1273 //-------------------------------------------------------------------------------------------------
1274 /// Delete the Timer
1275 /// @param s32TimerId \b IN: Timer ID
1276 /// @return TRUE : succeed
1277 /// @return FALSE : fail
1278 //-------------------------------------------------------------------------------------------------
MsOS_DeleteTimer(MS_S32 s32TimerId)1279 MS_BOOL MsOS_DeleteTimer (MS_S32 s32TimerId)
1280 {
1281 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1282 return TRUE;
1283 }
1284
1285 //-------------------------------------------------------------------------------------------------
1286 /// Start the Timer
1287 /// @param s32TimerId \b IN: Timer ID
1288 /// @return TRUE : succeed
1289 /// @return FALSE : fail
1290 //-------------------------------------------------------------------------------------------------
MsOS_StartTimer(MS_S32 s32TimerId)1291 MS_BOOL MsOS_StartTimer (MS_S32 s32TimerId)
1292 {
1293 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1294 return TRUE;
1295 }
1296
1297 //-------------------------------------------------------------------------------------------------
1298 /// Stop the Timer
1299 /// @param s32TimerId \b IN: Timer ID
1300 /// @return TRUE : succeed
1301 /// @return FALSE : fail
1302 /// @note MsOS_StopTimer then MsOS_StartTimer => The timer will trigger at the same relative
1303 /// intervals that it would have if it had not been disabled.
1304 //-------------------------------------------------------------------------------------------------
MsOS_StopTimer(MS_S32 s32TimerId)1305 MS_BOOL MsOS_StopTimer (MS_S32 s32TimerId)
1306 {
1307 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1308 return TRUE;
1309 }
1310
1311 //-------------------------------------------------------------------------------------------------
1312 /// Reset a Timer & reset the expiration periods
1313 /// @param s32TimerId \b IN: Timer ID
1314 /// @param u32FirstTimeMs \b IN: first ms for timer expiration
1315 /// @param u32PeriodTimeMs \b IN: periodic ms for timer expiration after first expiration
1316 /// 0: one shot timer
1317 /// @param bStartTimer \b IN: TRUE: activates the timer after it is created
1318 /// FALSE: leaves the timer disabled after it is created
1319 /// @return TRUE : succeed
1320 /// @return FALSE : fail
1321 //-------------------------------------------------------------------------------------------------
MsOS_ResetTimer(MS_S32 s32TimerId,MS_U32 u32FirstTimeMs,MS_U32 u32PeriodTimeMs,MS_BOOL bStartTimer)1322 MS_BOOL MsOS_ResetTimer (MS_S32 s32TimerId,
1323 MS_U32 u32FirstTimeMs,
1324 MS_U32 u32PeriodTimeMs,
1325 MS_BOOL bStartTimer)
1326 {
1327 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1328 return TRUE;
1329 }
1330
1331
1332 //
1333 // System time
1334 //
1335 //-------------------------------------------------------------------------------------------------
1336 /// Get current system time in ms
1337 /// @return system time in ms
1338 //-------------------------------------------------------------------------------------------------
MsOS_GetSystemTime(void)1339 MS_U32 MsOS_GetSystemTime (void) // 1ms/systime
1340 {
1341 TEE_Time current;
1342 MS_U32 system_timer=0;
1343 system_timer=tee_time_get_sys_time(¤t);
1344 return system_timer;
1345 }
1346 //-------------------------------------------------------------------------------------------------
1347 ///[OBSOLETE]
1348 /// Time difference between current time and task time
1349 /// @return system time diff in ms
1350 //-------------------------------------------------------------------------------------------------
MsOS_Timer_DiffTimeFromNow(MS_U32 u32TaskTimer)1351 MS_U32 MsOS_Timer_DiffTimeFromNow(MS_U32 u32TaskTimer) //unit = ms
1352 {
1353 //return (MsOS_GetSystemTime() - u32TaskTimer);
1354 }
1355 //-------------------------------------------------------------------------------------------------
1356 ///[OBSOLETE]
1357 /// Time difference between setting time and task time
1358 /// @return system time diff in ms
1359 //-------------------------------------------------------------------------------------------------
MsOS_Timer_DiffTime(MS_U32 u32Timer,MS_U32 u32TaskTimer)1360 MS_U32 MsOS_Timer_DiffTime(MS_U32 u32Timer, MS_U32 u32TaskTimer) //unit = ms
1361 {
1362 // simple minus is enough, because max(timer_counter) == max(u32)
1363 return (u32Timer - u32TaskTimer);
1364 }
1365 //
1366 // Queue
1367 //
1368 //-------------------------------------------------------------------------------------------------
1369 /// Create a Queue
1370 /// @param pStartAddr \b IN: It is useless now, can pass NULL.
1371 /// @param u32QueueSize \b IN: queue size (byte unit) : now fixed as
1372 /// CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE * u32MessageSize
1373 /// @param eMessageType \b IN: E_MSG_FIXED_SIZE / E_MSG_VAR_SIZE
1374 /// @param u32MessageSize \b IN: message size (byte unit) for E_MSG_FIXED_SIZE
1375 /// max message size (byte unit) for E_MSG_VAR_SIZE
1376 /// @param eAttribute \b IN: E_MSOS_FIFO suspended in FIFO order
1377 /// @param pQueueName \b IN: queue name
1378 /// @return assigned message queue ID
1379 /// @return < 0 - fail
1380 //-------------------------------------------------------------------------------------------------
MsOS_CreateQueue(void * pStartAddr,MS_U32 u32QueueSize,MessageType eMessageType,MS_U32 u32MessageSize,MsOSAttribute eAttribute,char * pQueueName)1381 MS_S32 MsOS_CreateQueue (void *pStartAddr,
1382 MS_U32 u32QueueSize,
1383 MessageType eMessageType,
1384 MS_U32 u32MessageSize,
1385 MsOSAttribute eAttribute,
1386 char *pQueueName)
1387 {
1388 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1389 return -1;
1390 }
1391
1392 //-------------------------------------------------------------------------------------------------
1393 /// Delete the Queue
1394 /// @param s32QueueId \b IN: Queue ID
1395 /// @return TRUE : succeed
1396 /// @return FALSE : fail
1397 /// @note It is important that there are not any threads blocked on the queue
1398 /// when this function is called or the behavior is undefined.
1399 //-------------------------------------------------------------------------------------------------
MsOS_DeleteQueue(MS_S32 s32QueueId)1400 MS_BOOL MsOS_DeleteQueue (MS_S32 s32QueueId)
1401 {
1402 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1403 return FALSE;
1404 }
1405
1406 //-------------------------------------------------------------------------------------------------
1407 /// Send a message to the end of the specified queue
1408 /// @param s32QueueId \b IN: Queue ID
1409 /// @param pu8Message \b IN: ptr to msg to send. NULL ptr is not allowed
1410 /// @param u32Size \b IN: msg size (byte)
1411 /// @param u32WaitMs \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the queue is full
1412 /// @return TRUE : succeed
1413 /// @return FALSE : fail
1414 //-------------------------------------------------------------------------------------------------
MsOS_SendToQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32Size,MS_U32 u32WaitMs)1415 MS_BOOL MsOS_SendToQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32Size, MS_U32 u32WaitMs)
1416 {
1417 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1418 return FALSE;
1419 }
1420
1421 //-------------------------------------------------------------------------------------------------
1422 /// Receive a message from the specified queue
1423 /// @param s32QueueId \b IN: Queue ID
1424 /// @param pu8Message \b OUT: msg destination
1425 /// @param u32IntendedSize \b IN: intended msg size (byte unit) to receive:
1426 /// @param pu32ActualSize \b OUT: actual msg size (byte unit) received
1427 /// @param u32WaitMs \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the queue is empty
1428 /// @return TRUE : succeed
1429 /// @return FALSE : fail
1430 //-------------------------------------------------------------------------------------------------
MsOS_RecvFromQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32IntendedSize,MS_U32 * pu32ActualSize,MS_U32 u32WaitMs)1431 MS_BOOL MsOS_RecvFromQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32IntendedSize, MS_U32 *pu32ActualSize, MS_U32 u32WaitMs)
1432 {
1433 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1434 return FALSE;
1435 }
1436
1437 //-------------------------------------------------------------------------------------------------
1438 /// Receive a message from the specified queue
1439 /// @param s32QueueId \b IN: Queue ID
1440 /// @param pu8Message \b OUT: msg destination
1441 /// @param u32IntendedSize \b IN: intended msg size (byte unit) to receive:
1442 /// @param pu32ActualSize \b OUT: actual msg size (byte unit) received
1443 /// @return TRUE : succeed
1444 /// @return FALSE : fail
1445 //-------------------------------------------------------------------------------------------------
MsOS_PeekFromQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32IntendedSize,MS_U32 * pu32ActualSize)1446 MS_BOOL MsOS_PeekFromQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32IntendedSize, MS_U32 *pu32ActualSize)
1447 {
1448 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1449 return FALSE;
1450 }
1451
1452 //-------------------------------------------------------------------------------------------------
1453 // Get a Queue information
1454 // @param s32QueueId \b IN: Queue ID
1455 // @param pu32QueueSize \b OUT: ptr to queue size (DW)
1456 // @param pu32MessageSize \b OUT: ptr to message size (DW)
1457 // @param peAttribute \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
1458 // @param pQueueName \b OUT: ptr to Queue name
1459 // @return TRUE : succeed
1460 // @return FALSE : the Queue has not been created
1461 //-------------------------------------------------------------------------------------------------
MsOS_InfoQueue(MS_S32 s32QueueId,MS_U32 * pu32QueueSize,MS_U32 * pu32MessageSize,MsOSAttribute * peAttribute,char * pQueueName)1462 MS_BOOL MsOS_InfoQueue ( MS_S32 s32QueueId, MS_U32 *pu32QueueSize, MS_U32 *pu32MessageSize,
1463 MsOSAttribute *peAttribute, char *pQueueName)
1464 {
1465 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1466 return FALSE;
1467 }
1468
1469 //
1470 // Interrupt management
1471 //
1472 //-------------------------------------------------------------------------------------------------
1473 /// Attach the interrupt callback function to interrupt #
1474 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1475 /// @param pIntCb \b IN: Interrupt callback function
1476 /// @return TRUE : succeed
1477 /// @return FALSE : fail
1478 //-------------------------------------------------------------------------------------------------
MsOS_AttachInterrupt(InterruptNum eIntNum,InterruptCb pIntCb)1479 MS_BOOL MsOS_AttachInterrupt (InterruptNum eIntNum, InterruptCb pIntCb)
1480 {
1481 MDrv_IRQ_Attach(eIntNum, pIntCb);
1482
1483 return TRUE;
1484 }
1485
1486 //-------------------------------------------------------------------------------------------------
1487 /// Detach the interrupt callback function from interrupt #
1488 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1489 /// @return TRUE : succeed
1490 /// @return FALSE : fail
1491 //-------------------------------------------------------------------------------------------------
MsOS_DetachInterrupt(InterruptNum eIntNum)1492 MS_BOOL MsOS_DetachInterrupt (InterruptNum eIntNum)
1493 {
1494 MDrv_IRQ_Detech(eIntNum);
1495
1496 return TRUE;
1497 }
1498
1499 //-------------------------------------------------------------------------------------------------
1500 /// Enable (unmask) the interrupt #
1501 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1502 /// @return TRUE : succeed
1503 /// @return FALSE : fail
1504 //-------------------------------------------------------------------------------------------------
MsOS_EnableInterrupt(InterruptNum eIntNum)1505 MS_BOOL MsOS_EnableInterrupt (InterruptNum eIntNum)
1506 {
1507 MDrv_IRQ_UnMask(eIntNum);
1508
1509 return TRUE;
1510 }
1511
1512 //-------------------------------------------------------------------------------------------------
1513 /// Disable (mask) the interrupt #
1514 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1515 /// @return TRUE : succeed
1516 //-------------------------------------------------------------------------------------------------
MsOS_DisableInterrupt(InterruptNum eIntNum)1517 MS_BOOL MsOS_DisableInterrupt (InterruptNum eIntNum)
1518 {
1519 MDrv_IRQ_Mask(eIntNum);
1520
1521 return TRUE;
1522 }
1523
1524 //-------------------------------------------------------------------------------------------------
1525 /// Disable all interrupts (including timer interrupt), the scheduler is disabled.
1526 /// @return Interrupt register value before all interrupts disable
1527 //-------------------------------------------------------------------------------------------------
MsOS_DisableAllInterrupts(void)1528 MS_U32 MsOS_DisableAllInterrupts(void)
1529 {
1530 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1531 return 0x12345678;
1532 }
1533
1534 //-------------------------------------------------------------------------------------------------
1535 /// Restore the interrupts from last MsOS_DisableAllInterrupts.
1536 /// @param u32OldInterrupts \b IN: Interrupt register value from @ref MsOS_DisableAllInterrupts
1537 /// @return TRUE : succeed
1538 //-------------------------------------------------------------------------------------------------
MsOS_RestoreAllInterrupts(MS_U32 u32OldInterrupts)1539 MS_BOOL MsOS_RestoreAllInterrupts(MS_U32 u32OldInterrupts)
1540 {
1541 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1542 return TRUE;
1543 }
1544
1545 //-------------------------------------------------------------------------------------------------
1546 /// Enable all CPU interrupts.
1547 /// @return TRUE : succeed
1548 //-------------------------------------------------------------------------------------------------
MsOS_EnableAllInterrupts(void)1549 MS_BOOL MsOS_EnableAllInterrupts(void)
1550 {
1551 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1552 return TRUE;
1553 }
1554
1555 //-------------------------------------------------------------------------------------------------
1556 /// In Interuupt Context or not
1557 /// @return TRUE : Yes
1558 /// @return FALSE : No
1559 //-------------------------------------------------------------------------------------------------
MsOS_In_Interrupt(void)1560 MS_BOOL MsOS_In_Interrupt (void)
1561 {
1562 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1563 return TRUE;
1564 }
1565
1566 //-------------------------------------------------------------------------------------------------
1567 /// Write back if dirty & Invalidate the cache lines in the given range
1568 /// @param u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1569 /// @param u32Size \b IN: size (must be 16-B aligned)
1570 /// @return TRUE : succeed
1571 /// @return FALSE : fail due to invalide parameter
1572 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Flush(MS_VIRT u32Start,MS_SIZE u32Size)1573 MS_BOOL MsOS_Dcache_Flush( MS_VIRT u32Start, MS_SIZE u32Size )
1574 {
1575 MS_U32 ret=0;
1576 ret = cache_maintenance_l1(DCACHE_AREA_CLEAN, u32Start, u32Size);
1577 if (ret != TEE_SUCCESS)
1578 {
1579 printf( "Optee Clean L1 error, OPTEE Result nummber=%d\n",ret);
1580 }
1581 #ifdef CFG_PL310
1582 ret = cache_maintenance_l2(L2CACHE_AREA_CLEAN_INV, core_va2pa(u32Start), u32Size);
1583 if (ret != TEE_SUCCESS)
1584 {
1585 printf( "Optee Flush L2 error, OPTEE Result nummber=%d\n",ret);
1586 }
1587 #endif /*CFG_PL310*/
1588 ret = cache_maintenance_l1(DCACHE_AREA_CLEAN_INV,u32Start, u32Size);
1589 if (ret != TEE_SUCCESS)
1590 {
1591 printf( "Optee Flush L1 error, OPTEE Result nummber=%d\n",ret);
1592 }
1593 return TRUE;
1594 }
1595
1596 //-------------------------------------------------------------------------------------------------
1597 /// Invalidate the cache lines in the given range
1598 /// @param u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1599 /// @param u32Size \b IN: size (must be 16-B aligned)
1600 /// @return TRUE : succeed
1601 /// @return FALSE : fail due to invalide parameter
1602 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Invalidate(MS_VIRT u32Start,MS_SIZE u32Size)1603 MS_BOOL MsOS_Dcache_Invalidate( MS_VIRT u32Start, MS_SIZE u32Size )
1604 {
1605 #if 0
1606 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1607 return FALSE;
1608 #else
1609 MS_U32 ret=0;
1610 #ifdef CFG_PL310
1611 ret = cache_maintenance_l2(L2CACHE_AREA_INVALIDATE, core_va2pa(u32Start), u32Size);
1612 if (ret != TEE_SUCCESS)
1613 {
1614 printf( "Optee Invalidate L2 error, OPTEE Result nummber=%d\n",ret);
1615 }
1616 #endif /*CFG_PL310*/
1617 ret = cache_maintenance_l1(DCACHE_AREA_INVALIDATE,u32Start, u32Size);
1618 if (ret != TEE_SUCCESS)
1619 {
1620 printf( "Optee Invalidate L1 error, OPTEE Result nummber=%d\n",ret);
1621 }
1622 return TRUE;
1623 #endif
1624 }
1625
1626 //-------------------------------------------------------------------------------------------------
1627 /// Write back if dirty the cache lines in the given range
1628 /// @param u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1629 /// @param u32Size \b IN: size (must be 16-B aligned)
1630 /// @return TRUE : succeed
1631 /// @return FALSE : fail due to invalide parameter
1632 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Writeback(MS_VIRT u32Start,MS_SIZE u32Size)1633 MS_BOOL MsOS_Dcache_Writeback( MS_VIRT u32Start, MS_SIZE u32Size )
1634 {
1635 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1636 return FALSE;
1637 }
1638
MsOS_VA2PA(MS_VIRT addr)1639 MS_PHY MsOS_VA2PA(MS_VIRT addr)
1640 {
1641 return HAL_MsOS_MPool_VA2PA(addr);
1642 }
1643
MsOS_PA2KSEG0(MS_PHY addr)1644 MS_VIRT MsOS_PA2KSEG0(MS_PHY addr)
1645 {
1646 return HAL_MsOS_MPool_PA2KSEG0(addr);
1647 }
1648
MsOS_PA2KSEG1(MS_PHY addr)1649 MS_VIRT MsOS_PA2KSEG1(MS_PHY addr)
1650 {
1651 return HAL_MsOS_MPool_PA2KSEG1(addr);
1652 }
1653
1654
1655 // Share memory operation
MsOS_SHM_Init(void)1656 MS_BOOL MsOS_SHM_Init(void)
1657 {
1658 memset(&shm_array, 0, sizeof(shm_array));
1659 return TRUE;
1660 }
1661
1662 // Share memory operation
1663 // linux behavior simulation for nuttx
1664
MsOS_SHM_GetId(MS_U8 * pu8ClientName,MS_U32 u32BufSize,MS_U32 * pu32ShmId,MS_VIRT * pu32Addr,MS_U32 * pu32BufSize,MS_U32 u32Flag)1665 MS_BOOL MsOS_SHM_GetId(MS_U8* pu8ClientName, MS_U32 u32BufSize, MS_U32* pu32ShmId, MS_VIRT* pu32Addr, MS_U32* pu32BufSize, MS_U32 u32Flag)
1666 {
1667 int i, match = FALSE, result = FALSE;
1668
1669 // search matching entry
1670 for (i = 0; i < shm_array.count; i++)
1671 {
1672 if (0 == strcmp(shm_array.shm_entry[i].name, (char*)pu8ClientName))
1673 {
1674 match = TRUE;
1675 break;
1676 }
1677 }
1678
1679 if (match) {
1680 // if match, fill info
1681 *pu32ShmId = i;
1682 *pu32Addr = (MS_U32)(shm_array.shm_entry[i].addr);
1683 *pu32BufSize = shm_array.shm_entry[i].size;
1684 result = TRUE;
1685 } else if (u32Flag == MSOS_SHM_CREATE) {
1686 // if not match & specify to create one, register to shm_array
1687 *pu32ShmId = shm_array.count;
1688 *pu32Addr = (MS_U32)malloc(u32BufSize);
1689 memset((void*)*pu32Addr, 0, u32BufSize);
1690 *pu32BufSize = u32BufSize;
1691 // this would cause bug, because string source may be "stack variable"
1692 //shm_array.shm_entry[shm_array.count].name = (char*)pu8ClientName;
1693 msos_strcpy(shm_array.shm_entry[shm_array.count].name, (char*)pu8ClientName);
1694 shm_array.shm_entry[shm_array.count].addr = (void*)*pu32Addr;
1695 shm_array.shm_entry[shm_array.count].size = *pu32BufSize;
1696 shm_array.count++;
1697 result = TRUE;
1698 }
1699
1700 return result;
1701 }
1702
MsOS_SHM_FreeId(MS_U8 * pu8ClientName,MS_U32 u32ShmId)1703 MS_BOOL MsOS_SHM_FreeId(MS_U8* pu8ClientName, MS_U32 u32ShmId)
1704 {
1705 return TRUE;
1706 }
1707
1708 //-------------------------------------------------------------------------------------------------
1709 /// Disable the CPU interrupt
1710 /// @return Interrupt register value before all interrupts disable
1711 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DisableInterrupt(void)1712 MS_U32 MsOS_CPU_DisableInterrupt (void)
1713 {
1714 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1715 return TRUE;
1716 }
1717
1718 //-------------------------------------------------------------------------------------------------
1719 /// Enable the CPU interrupt
1720 /// @return TRUE : succeed
1721 //-------------------------------------------------------------------------------------------------
MsOS_CPU_EnableInterrupt(void)1722 MS_BOOL MsOS_CPU_EnableInterrupt (void)
1723 {
1724 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1725 return TRUE;
1726 }
1727
1728 //-------------------------------------------------------------------------------------------------
1729 /// Restore the CPU interrupt from last MsOS_CPU_DisableInterrupts.
1730 /// @param u32OldInterrupts \b IN: Interrupt register value from @ref MsOS_CPU_DisableInterrupts
1731 /// @return TRUE : succeed
1732 //-------------------------------------------------------------------------------------------------
MsOS_CPU_RestoreInterrupt(MS_U32 u32OldInterrupts)1733 MS_BOOL MsOS_CPU_RestoreInterrupt (MS_U32 u32OldInterrupts)
1734 {
1735 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1736 return TRUE;
1737 }
1738
1739 //-------------------------------------------------------------------------------------------------
1740 /// Mask all the CPU interrupt
1741 /// @return TRUE : succeed
1742 //-------------------------------------------------------------------------------------------------
MsOS_CPU_MaskAllInterrupt(void)1743 MS_BOOL MsOS_CPU_MaskAllInterrupt (void)
1744 {
1745 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1746 return TRUE;
1747 }
1748
1749 //-------------------------------------------------------------------------------------------------
1750 /// Mask the CPU interrupt
1751 /// @param intr_num \b IN: Interrupt number in enumerator MHAL_INTERRUPT_TYPE
1752 /// @return TRUE : succeed
1753 //-------------------------------------------------------------------------------------------------
MsOS_CPU_MaskInterrupt(MHAL_INTERRUPT_TYPE intr_num)1754 MS_BOOL MsOS_CPU_MaskInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1755 {
1756 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1757 return TRUE;
1758 }
1759
1760 //-------------------------------------------------------------------------------------------------
1761 /// UnMask the CPU interrupt
1762 /// @param intr_num \b IN: Interrupt number in enumerator MHAL_INTERRUPT_TYPE
1763 /// @return TRUE : succeed
1764 //-------------------------------------------------------------------------------------------------
1765 extern void up_enable_irq(int irq);
1766
MsOS_CPU_UnMaskInterrupt(MHAL_INTERRUPT_TYPE intr_num)1767 MS_BOOL MsOS_CPU_UnMaskInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1768 {
1769 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1770 //up_enable_irq((int) intr_num);
1771 return TRUE;
1772 }
1773
1774 //-------------------------------------------------------------------------------------------------
1775 /// Lock the CPU interrupt
1776 /// @return TRUE : succeed
1777 //-------------------------------------------------------------------------------------------------
MsOS_CPU_LockInterrupt(void)1778 MS_BOOL MsOS_CPU_LockInterrupt (void)
1779 {
1780 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1781 return TRUE;
1782 }
1783
1784 //-------------------------------------------------------------------------------------------------
1785 /// UnLock the CPU interrupt
1786 /// @return TRUE : succeed
1787 //-------------------------------------------------------------------------------------------------
MsOS_CPU_UnLockInterrupt(void)1788 MS_BOOL MsOS_CPU_UnLockInterrupt (void)
1789 {
1790 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1791 return TRUE;
1792 }
1793
1794 //-------------------------------------------------------------------------------------------------
1795 /// Attach the CPU interrupt callback function to interrupt #
1796 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1797 /// @param pIntCb \b IN: Interrupt callback function
1798 /// @param dat \b IN: Data
1799 /// @return TRUE : succeed
1800 //-------------------------------------------------------------------------------------------------
1801 extern int irq_attach(int irq, xcpt_t isr);
1802
MsOS_CPU_AttachInterrupt(MHAL_INTERRUPT_TYPE intr_num,mhal_isr_t isr,MS_U32 dat)1803 MS_BOOL MsOS_CPU_AttachInterrupt (MHAL_INTERRUPT_TYPE intr_num, mhal_isr_t isr, MS_U32 dat)
1804 {
1805 // irq_attach(intr_num, (xcpt_t) isr);
1806 return TRUE;
1807 }
1808
1809 //-------------------------------------------------------------------------------------------------
1810 /// Detach the CPU interrupt callback function to interrupt #
1811 /// @param eIntNum \b IN: Interrupt number in enumerator InterruptNum
1812 /// @param pIntCb \b IN: Interrupt callback function
1813 /// @param dat \b IN: Data
1814 /// @return TRUE : succeed
1815 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DetachInterrupt(MHAL_INTERRUPT_TYPE intr_num)1816 MS_BOOL MsOS_CPU_DetachInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1817 {
1818 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1819 return TRUE;
1820 }
1821
1822 //-------------------------------------------------------------------------------------------------
1823 /// Attach the CPU exception callback function to interrupt #
1824 /// @param eIntNum \b IN: Exception number in enumerator InterruptNum
1825 /// @param pIntCb \b IN: Exception callback function
1826 /// @param dat \b IN: Data
1827 /// @return TRUE : succeed
1828 //-------------------------------------------------------------------------------------------------
MsOS_CPU_AttachException(MHAL_EXCEPTION_TYPE expt_num,mhal_isr_t isr,MS_U32 dat)1829 MS_BOOL MsOS_CPU_AttachException (MHAL_EXCEPTION_TYPE expt_num, mhal_isr_t isr, MS_U32 dat)
1830 {
1831 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1832 return TRUE;
1833 }
1834
1835 //-------------------------------------------------------------------------------------------------
1836 /// Detach the CPU exception callback function to interrupt #
1837 /// @param eIntNum \b IN: Exception number in enumerator InterruptNum
1838 /// @param pIntCb \b IN: Exception callback function
1839 /// @param dat \b IN: Data
1840 /// @return TRUE : succeed
1841 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DetachExceptiont(MHAL_EXCEPTION_TYPE expt_num)1842 MS_BOOL MsOS_CPU_DetachExceptiont (MHAL_EXCEPTION_TYPE expt_num)
1843 {
1844 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1845 return TRUE;
1846 }
1847 //-------------------------------------------------------------------------------------------------
1848 /// Set EBASE
1849 /// @param u32Addr \b IN: MIPS Code Start Address
1850 /// @return TRUE : succeed
1851 //-------------------------------------------------------------------------------------------------
MsOS_CPU_SetEBASE(MS_U32 u32Addr)1852 MS_BOOL MsOS_CPU_SetEBASE (MS_U32 u32Addr)
1853 {
1854 //PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1855 return TRUE;
1856 }
1857 //-------------------------------------------------------------------------------------------------
1858 /// Sync data in EC-Brigde
1859 /// @return TRUE : succeed
1860 /// @return FALSE : fail
1861 //-------------------------------------------------------------------------------------------------
MsOS_Sync(void)1862 void MsOS_Sync(void)
1863 {
1864 MAsm_CPU_Sync();
1865 }
1866
1867 //-------------------------------------------------------------------------------------------------
1868 /// Flush non-cached data into dram
1869 //-------------------------------------------------------------------------------------------------
MsOS_FlushMemory(void)1870 void MsOS_FlushMemory(void)
1871 {
1872 HAL_MMIO_FlushMemory();
1873 }
1874
1875 //-------------------------------------------------------------------------------------------------
1876 /// Flush non-cached data into dram
1877 //-------------------------------------------------------------------------------------------------
MsOS_ReadMemory(void)1878 void MsOS_ReadMemory(void)
1879 {
1880 HAL_MMIO_ReadMemory();
1881 }
1882
MsOS_GetKattribute(char * pAttr)1883 MS_U32 MsOS_GetKattribute(char *pAttr)
1884 {
1885 // This is for linux only, do nothing here
1886 return 0;
1887 }
1888