1 /**
2 * @file rm_osal.c
3 *
4 * @brief
5 * This is the OS abstraction layer and is used by the the RM LLD.
6 *
7 * \par
8 * ============================================================================
9 * @n (C) Copyright 2012, 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 */
41 #include <xdc/std.h>
42 #include <xdc/runtime/Memory.h>
43 #include <xdc/runtime/Error.h>
45 #include <ti/sysbios/family/c64p/Hwi.h>
47 /* CSL modules */
48 #include <ti/csl/csl_semAux.h>
49 /* CSL Cache module includes */
50 #include <ti/csl/csl_cacheAux.h>
51 /* CSL XMC module includes */
52 #include <ti/csl/csl_xmcAux.h>
54 /**********************************************************************
55 ****************************** Defines *******************************
56 **********************************************************************/
58 #define RM_HW_SEM 2
60 /**********************************************************************
61 ************************** Global Variables **************************
62 **********************************************************************/
63 uint32_t rmMallocCounter = 0;
64 uint32_t rmFreeCounter = 0;
66 /**********************************************************************
67 *************************** OSAL Functions **************************
68 **********************************************************************/
70 /**
71 * @b Description
72 * @n
73 * The function is used to allocate a memory block of the specified size.
74 *
75 * @param[in] num_bytes
76 * Number of bytes to be allocated.
77 *
78 * @retval
79 * Allocated block address
80 */
81 void* Osal_rmMalloc (uint32_t num_bytes)
82 {
83 Error_Block errorBlock;
85 /* Increment the allocation counter. */
86 rmMallocCounter++;
88 /* Allocate memory. */
89 return Memory_alloc(NULL, num_bytes, 0, &errorBlock);
90 }
92 /**
93 * @b Description
94 * @n
95 * The function is used to free a memory block of the specified size.
96 *
97 * @param[in] ptr
98 * Pointer to the memory block to be cleaned up.
99 *
100 * @param[in] size
101 * Size of the memory block to be cleaned up.
102 *
103 * @retval
104 * Not Applicable
105 */
106 void Osal_rmFree (void *ptr, uint32_t size)
107 {
108 /* Increment the free counter. */
109 rmFreeCounter++;
110 Memory_free(NULL, ptr, size);
111 }
113 /**
114 * @b Description
115 * @n
116 * The function is used to enter a critical section.
117 * Function protects against
118 *
119 * access from multiple cores
120 * and
121 * access from multiple threads on single core
122 *
123 * @retval
124 * Handle used to lock critical section
125 */
126 void* Osal_rmCsEnter (void)
127 {
128 /* Get the hardware semaphore */
129 while ((CSL_semAcquireDirect (RM_HW_SEM)) == 0);
131 /* Create Semaphore for protection against access from multiple threads
132 * Not created here becasue application is not multithreaded
133 * */
134 return NULL;
135 }
137 /**
138 * @b Description
139 * @n
140 * The function is used to exit a critical section
141 * protected using Osal_rmCsEnter() API.
142 *
143 * @param[in] CsHandle
144 * Handle for unlocking critical section.
145 *
146 * @retval
147 * Not Applicable
148 */
149 void Osal_rmCsExit (void *CsHandle)
150 {
151 /* Release Semaphore using handle */
153 /* Release the hardware semaphore */
154 CSL_semReleaseSemaphore (RM_HW_SEM);
156 return;
157 }
158 /**
159 * @b Description
160 * @n
161 * The function is used to enter a critical section.
162 * Function protects against
163 * access from multiple threads on single core
164 *
165 * @retval
166 * Handle used to lock critical section
167 */
168 void* Osal_rmMtCsEnter (void)
169 {
170 /* Create Semaphore for protection against access from multiple threads
171 * Not created here becasue application is not multithreaded
172 * */
173 return NULL;
174 }
176 /**
177 * @b Description
178 * @n
179 * The function is used to exit a critical section
180 * protected using Osal_rmCsEnter() API.
181 *
182 * @param[in] CsHandle
183 * Handle for unlocking critical section.
184 *
185 * @retval
186 * Not Applicable
187 */
188 void Osal_rmMtCsExit (void *CsHandle)
189 {
190 /* Release Semaphore using handle */
192 return;
193 }
195 /**
196 * @b Description
197 * @n
198 * The function is used to indicate that a block of memory is
199 * about to be accessed. If the memory block is cached then this
200 * indicates that the application would need to ensure that the
201 * cache is updated with the data from the actual memory.
202 *
203 * @param[in] ptr
204 * Address of memory block
205 *
206 * @param[in] size
207 * Size of memory block
208 *
209 * @retval
210 * Not Applicable
211 */
212 void Osal_rmBeginMemAccess (void *ptr, uint32_t size)
213 {
214 uint32_t key;
216 /* Disable Interrupts */
217 key = Hwi_disable();
219 /* Cleanup the prefetch buffer also. */
220 CSL_XMC_invalidatePrefetchBuffer();
222 #ifdef L2_CACHE
223 /* Invalidate L2 cache. This should invalidate L1D as well.
224 * Wait until operation is complete. */
225 CACHE_invL2 (ptr, size, CACHE_FENCE_WAIT);
226 #else
227 /* Invalidate L1D cache and wait until operation is complete.
228 * Use this approach if L2 cache is not enabled */
229 CACHE_invL1d (ptr, size, CACHE_FENCE_WAIT);
230 #endif
232 asm (" nop 4");
233 asm (" nop 4");
234 asm (" nop 4");
235 asm (" nop 4");
237 /* Reenable Interrupts. */
238 Hwi_restore(key);
240 return;
241 }
243 /**
244 * @b Description
245 * @n
246 * The function is used to indicate that the block of memory has
247 * finished being accessed. If the memory block is cached then the
248 * application would need to ensure that the contents of the cache
249 * are updated immediately to the actual memory.
250 *
251 * @param[in] ptr
252 * Address of memory block
253 *
254 * @param[in] size
255 * Size of memory block
256 *
257 * @retval
258 * Not Applicable
259 */
260 void Osal_rmEndMemAccess (void *ptr, uint32_t size)
261 {
262 uint32_t key;
264 /* Disable Interrupts */
265 key = Hwi_disable();
267 #ifdef L2_CACHE
268 /* Writeback L2 cache. This should Writeback L1D as well.
269 * Wait until operation is complete. */
270 CACHE_wbL2 (ptr, size, CACHE_FENCE_WAIT);
272 #else
273 /* Writeback L1D cache and wait until operation is complete.
274 * Use this approach if L2 cache is not enabled */
275 CACHE_wbL1d (ptr, size, CACHE_FENCE_WAIT);
276 #endif
278 asm (" nop 4");
279 asm (" nop 4");
280 asm (" nop 4");
281 asm (" nop 4");
283 /* Reenable Interrupts. */
284 Hwi_restore(key);
285 return;
286 }