6bf0d2dc1b4a89c3912ea5262f5ec95bc5c08564
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 /* CSL includes */
57 #include <ti/csl/csl_cacheAux.h>
58 #include <ti/csl/csl_xmcAux.h>
60 /**********************************************************************
61 ****************************** Defines *******************************
62 **********************************************************************/
64 /**********************************************************************
65 ************************** Global Variables **************************
66 **********************************************************************/
67 uint32_t rmMallocCounter = 0;
68 uint32_t rmFreeCounter = 0;
70 int32_t rmByteAlloc = 0;
71 int32_t rmByteFree = 0;
73 /**********************************************************************
74 *************************** OSAL Functions **************************
75 **********************************************************************/
77 /* FUNCTION PURPOSE: Allocates memory
78 ***********************************************************************
79 * DESCRIPTION: The function is used to allocate a memory block of the
80 * specified size.
81 */
82 void *Osal_rmMalloc (uint32_t num_bytes)
83 {
84 Error_Block errorBlock;
86 /* Increment the allocation counter. */
87 rmMallocCounter++;
88 rmByteAlloc += num_bytes;
90 /* Allocate memory. */
91 return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
92 }
94 /* FUNCTION PURPOSE: Frees memory
95 ***********************************************************************
96 * DESCRIPTION: The function is used to free a memory block of the
97 * specified size.
98 */
99 void Osal_rmFree (void *ptr, uint32_t size)
100 {
101 /* Increment the free counter. */
102 rmFreeCounter++;
103 rmByteFree += size;
104 Memory_free(NULL, ptr, size);
105 }
107 /* FUNCTION PURPOSE: Critical section enter
108 ***********************************************************************
109 * DESCRIPTION: The function is used to enter a critical section.
110 * Function protects against
111 *
112 * access from multiple cores
113 * and
114 * access from multiple threads on single core
115 */
116 void *Osal_rmCsEnter(void)
117 {
118 return NULL;
119 }
121 /* FUNCTION PURPOSE: Critical section exit
122 ***********************************************************************
123 * DESCRIPTION: The function is used to exit a critical section
124 * protected using Osal_rmCsEnter() API.
125 */
126 void Osal_rmCsExit(void *CsHandle)
127 {
129 }
131 /* FUNCTION PURPOSE: Multi-threaded critical section enter
132 ***********************************************************************
133 * DESCRIPTION: The function is used to enter a multi-threaded critical
134 * section. Function protects against
135 *
136 * access from multiple threads on single core
137 */
138 void *Osal_rmMtCsEnter(void *mtSemObj)
139 {
140 Semaphore_pend((Semaphore_Handle)mtSemObj, BIOS_WAIT_FOREVER);
141 return NULL;
142 }
144 /* FUNCTION PURPOSE: Multi-threaded critical section exit
145 ***********************************************************************
146 * DESCRIPTION: The function is used to exit a multi-threaded critical
147 * section protected using Osal_rmMtCsEnter() API.
148 */
149 void Osal_rmMtCsExit(void *mtSemObj, void *CsHandle)
150 {
151 Semaphore_post((Semaphore_Handle)mtSemObj);
152 }
154 /* FUNCTION PURPOSE: Cache invalidate
155 ***********************************************************************
156 * DESCRIPTION: The function is used to indicate that a block of memory is
157 * about to be accessed. If the memory block is cached then this
158 * indicates that the application would need to ensure that the
159 * cache is updated with the data from the actual memory.
160 */
161 void Osal_rmBeginMemAccess(void *ptr, uint32_t size)
162 {
163 uint32_t key;
165 /* Disable Interrupts */
166 key = Hwi_disable();
168 /* Cleanup the prefetch buffer also. */
169 CSL_XMC_invalidatePrefetchBuffer();
171 #ifdef L2_CACHE
172 /* Invalidate L2 cache. This should invalidate L1D as well.
173 * Wait until operation is complete. */
174 CACHE_invL2 (ptr, size, CACHE_FENCE_WAIT);
175 #else
176 /* Invalidate L1D cache and wait until operation is complete.
177 * Use this approach if L2 cache is not enabled */
178 CACHE_invL1d (ptr, size, CACHE_FENCE_WAIT);
179 #endif
181 /* Reenable Interrupts. */
182 Hwi_restore(key);
184 return;
185 }
187 /* FUNCTION PURPOSE: Cache writeback
188 ***********************************************************************
189 * DESCRIPTION: The function is used to indicate that the block of memory has
190 * finished being accessed. If the memory block is cached then the
191 * application would need to ensure that the contents of the cache
192 * are updated immediately to the actual memory.
193 */
194 void Osal_rmEndMemAccess(void *ptr, uint32_t size)
195 {
196 uint32_t key;
198 /* Disable Interrupts */
199 key = Hwi_disable();
201 #ifdef L2_CACHE
202 /* Writeback L2 cache. This should Writeback L1D as well.
203 * Wait until operation is complete. */
204 CACHE_wbL2 (ptr, size, CACHE_FENCE_WAIT);
206 #else
207 /* Writeback L1D cache and wait until operation is complete.
208 * Use this approach if L2 cache is not enabled */
209 CACHE_wbL1d (ptr, size, CACHE_FENCE_WAIT);
210 #endif
212 /* Reenable Interrupts. */
213 Hwi_restore(key);
215 return;
216 }
218 /* FUNCTION PURPOSE: Creates a task blocking object
219 ***********************************************************************
220 * DESCRIPTION: The function is used to create a task blocking object
221 * capable of blocking the task a RM instance is running
222 * within
223 */
224 void *Osal_rmTaskBlockCreate(void)
225 {
226 Semaphore_Params semParams;
228 Semaphore_Params_init(&semParams);
229 return((void *)Semaphore_create(0, &semParams, NULL));
230 }
232 /* FUNCTION PURPOSE: Blocks a RM instance
233 ***********************************************************************
234 * DESCRIPTION: The function is used to block a task whose context a
235 * RM instance is running within.
236 */
237 void Osal_rmTaskBlock(void *handle)
238 {
239 Semaphore_pend((Semaphore_Handle)handle, BIOS_WAIT_FOREVER);
240 }
242 /* FUNCTION PURPOSE: unBlocks a RM instance
243 ***********************************************************************
244 * DESCRIPTION: The function is used to unblock a task whose context a
245 * RM instance is running within.
246 */
247 void Osal_rmTaskUnblock(void *handle)
248 {
249 Semaphore_post((Semaphore_Handle)handle);
250 }
252 /* FUNCTION PURPOSE: Deletes a task blocking object
253 ***********************************************************************
254 * DESCRIPTION: The function is used to delete a task blocking object
255 * provided to a RM instance
256 */
257 void Osal_rmTaskBlockDelete(void *handle)
258 {
259 Semaphore_delete((Semaphore_Handle *)&handle);
260 }
262 /* FUNCTION PURPOSE: Prints a variable list
263 ***********************************************************************
264 * DESCRIPTION: The function is used to print a string to the console
265 */
266 void Osal_rmLog (char *fmt, ... )
267 {
268 VaList ap;
270 va_start(ap, fmt);
271 System_vprintf(fmt, ap);
272 va_end(ap);
273 }