]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/rm-lld.git/blob - test/rm_shared_osal.c
eb8988aab0ae8932833a0c7e5053754808a1452c
[keystone-rtos/rm-lld.git] / test / rm_shared_osal.c
1 /**
2  *   @file  rm_osal.c
3  *
4  *   @brief
5  *      This is the OS abstraction layer used by the Resource Manager.
6  *
7  *  \par
8  *  ============================================================================
9  *  @n   (C) Copyright 2012-2013, Texas Instruments, Inc.
10  *
11  *  Redistribution and use in source and binary forms, with or without
12  *  modification, are permitted provided that the following conditions
13  *  are met:
14  *
15  *    Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  *    Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the
21  *    distribution.
22  *
23  *    Neither the name of Texas Instruments Incorporated nor the names of
24  *    its contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *
39  *  \par
40 */
42 /* Standard Includes */
43 #include <stdarg.h>
45 /* XDC Includes */
46 #include <xdc/std.h>
47 #include <xdc/runtime/Memory.h>
48 #include <xdc/runtime/Error.h>
49 #include <xdc/runtime/System.h>
51 /* BIOS Includes */
52 #include <ti/sysbios/BIOS.h>
53 #include <ti/sysbios/hal/Hwi.h>
54 #include <ti/sysbios/knl/Semaphore.h>
56 /* IPC includes */ 
57 #include <ti/ipc/SharedRegion.h>
59 /* CSL includes */
60 #include <ti/csl/csl_semAux.h>
61 #include <ti/csl/csl_cacheAux.h>
62 #include <ti/csl/csl_xmcAux.h>
64 /**********************************************************************
65  ****************************** Defines *******************************
66  **********************************************************************/
68 /* Try to avoid conflict with GateMP in rm_shared_test.c */
69 #define RM_HW_SEM     4
71 /**********************************************************************
72  ************************** Global Variables **************************
73  **********************************************************************/
74 uint32_t rmMallocCounter = 0;
75 uint32_t rmFreeCounter   = 0;
77 /**********************************************************************
78  *************************** OSAL Functions **************************
79  **********************************************************************/
81 /* FUNCTION PURPOSE: Allocates memory
82  ***********************************************************************
83  * DESCRIPTION: The function is used to allocate a memory block of the
84  *              specified size.
85  */
86 void *Osal_rmMalloc (uint32_t num_bytes)
87 {
88         Error_Block     errorBlock;
90     /* Increment the allocation counter. */
91     rmMallocCounter++;
93         /* Allocate memory. */
94         return Memory_alloc(SharedRegion_getHeap(0), num_bytes, 0, &errorBlock);
95 }
97 /* FUNCTION PURPOSE: Frees memory
98  ***********************************************************************
99  * DESCRIPTION: The function is used to free a memory block of the
100  *              specified size.
101  */ 
102 void Osal_rmFree (void *ptr, uint32_t size)
104     /* Increment the free counter. */
105     rmFreeCounter++;
106         Memory_free(SharedRegion_getHeap(0), ptr, size);
109 /* FUNCTION PURPOSE: Critical section enter
110  ***********************************************************************
111  * DESCRIPTION: The function is used to enter a critical section.
112  *              Function protects against 
113  *      
114  *              access from multiple cores 
115  *              and 
116  *              access from multiple threads on single core
117  */  
118 void *Osal_rmCsEnter(void)
120     /* Get the hardware semaphore for protection against multiple core access */
121     while ((CSL_semAcquireDirect (RM_HW_SEM)) == 0);
122     return NULL;
125 /* FUNCTION PURPOSE: Critical section exit
126  ***********************************************************************
127  * DESCRIPTION: The function is used to exit a critical section 
128  *              protected using Osal_cppiCsEnter() API.
129  */  
130 void Osal_rmCsExit(void *CsHandle)
132     /* Release the hardware semaphore */ 
133     CSL_semReleaseSemaphore (RM_HW_SEM);
136 /* FUNCTION PURPOSE: Cache invalidate
137  ***********************************************************************
138  * DESCRIPTION: The function is used to indicate that a block of memory is 
139  *              about to be accessed. If the memory block is cached then this 
140  *              indicates that the application would need to ensure that the 
141  *              cache is updated with the data from the actual memory.
142  */  
143 void Osal_rmBeginMemAccess(void *ptr, uint32_t size)
145     uint32_t    key;
147     /* Disable Interrupts */
148     key = Hwi_disable();
150     /* Cleanup the prefetch buffer also. */
151     CSL_XMC_invalidatePrefetchBuffer();
153 #ifdef L2_CACHE
154     /* Invalidate L2 cache. This should invalidate L1D as well. 
155      * Wait until operation is complete. */    
156     CACHE_invL2 (ptr, size, CACHE_FENCE_WAIT);
157 #else       
158     /* Invalidate L1D cache and wait until operation is complete. 
159      * Use this approach if L2 cache is not enabled */    
160     CACHE_invL1d (ptr, size, CACHE_FENCE_WAIT);
161 #endif
163     /* Reenable Interrupts. */
164     Hwi_restore(key);
165     return;
168 /* FUNCTION PURPOSE: Cache writeback
169  ***********************************************************************
170  * DESCRIPTION: The function is used to indicate that the block of memory has 
171  *              finished being accessed. If the memory block is cached then the 
172  *              application would need to ensure that the contents of the cache 
173  *              are updated immediately to the actual memory. 
174  */  
175 void Osal_rmEndMemAccess(void *ptr, uint32_t size)
177     uint32_t    key;
179     /* Disable Interrupts */
180     key = Hwi_disable();
182 #ifdef L2_CACHE
183     /* Writeback L2 cache. This should Writeback L1D as well. 
184      * Wait until operation is complete. */ 
185     CACHE_wbL2 (ptr, size, CACHE_FENCE_WAIT);
187 #else    
188     /* Writeback L1D cache and wait until operation is complete. 
189      * Use this approach if L2 cache is not enabled */    
190     CACHE_wbL1d (ptr, size, CACHE_FENCE_WAIT);
191 #endif
193     /* Reenable Interrupts. */
194     Hwi_restore(key);
195     return;
198 /* FUNCTION PURPOSE: Creates a task blocking object
199  ***********************************************************************
200  * DESCRIPTION: The function is used to create a task blocking object
201  *              capable of blocking the task a RM instance is running
202  *              within
203  */
204 void *Osal_rmTaskBlockCreate(void)
206     Semaphore_Params semParams;    
208     Semaphore_Params_init(&semParams);
209     return((void *)Semaphore_create(0, &semParams, NULL));
212 /* FUNCTION PURPOSE: Blocks a RM instance
213  ***********************************************************************
214  * DESCRIPTION: The function is used to block a task whose context a
215  *              RM instance is running within.
216  */
217 void Osal_rmTaskBlock(void *handle)
219     Semaphore_pend((Semaphore_Handle)handle, BIOS_WAIT_FOREVER);
222 /* FUNCTION PURPOSE: unBlocks a RM instance
223  ***********************************************************************
224  * DESCRIPTION: The function is used to unblock a task whose context a
225  *              RM instance is running within.
226  */
227 void Osal_rmTaskUnblock(void *handle)
229     Semaphore_post((Semaphore_Handle)handle);
232 /* FUNCTION PURPOSE: Deletes a task blocking object
233  ***********************************************************************
234  * DESCRIPTION: The function is used to delete a task blocking object
235  *              provided to a RM instance
236  */
237 void Osal_rmTaskBlockDelete(void *handle)
239     Semaphore_delete((Semaphore_Handle *)&handle);
242 /* FUNCTION PURPOSE: Prints a variable list
243  ***********************************************************************
244  * DESCRIPTION: The function is used to print a string to the console
245  */
246 void Osal_rmLog (char *fmt, ... )
248     VaList ap;
249     
250     va_start(ap, fmt);
251     System_vprintf(fmt, ap);
252     va_end(ap);