1 /**
2 * @file salldosal.c
3 *
4 * @brief
5 * This is the OS abstraction layer and is used by the the SA LLD.
6 *
7 * \par
8 * ============================================================================
9 * @n (C) Copyright 2009-2020, 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 */
40 #include "../unittest.h"
41 #include <ti/drv/sa/sa_osal.h>
43 /* CSL CHIP, SEM Functional layer includes */
44 #if defined (BUILD_DSP)
45 #include <ti/csl/csl_chip.h>
46 #endif
48 #ifndef NSS_LITE2
49 #include <ti/csl/csl_semAux.h>
50 #endif
52 /**********************************************************************
53 ****************************** Defines *******************************
54 **********************************************************************/
56 #define SALLD_HW_SEM 1
58 /**********************************************************************
59 ************************** Global Variables **************************
60 **********************************************************************/
61 uint32_t salldMallocCounter = 0;
62 uint32_t salldFreeCounter = 0;
64 /**********************************************************************
65 ******************************* Macros *******************************
66 **********************************************************************/
68 /**********************************************************************
69 *************************** OSAL Functions **************************
70 **********************************************************************/
72 uint32_t size_malloced = 0;
74 #if defined(SAU_PRMOTE_DEMOTE_TEST)
75 #define OSAL_MALLOC_BLK_SIZE (0x1000)
76 #else
77 #define OSAL_MALLOC_BLK_SIZE (0x10000)
78 #endif
80 /* R5F tool chain fails to do malloc() and hence
81 * providing a static block for memory requirements
82 */
83 #if defined(__TI_ARM_V7R4__) || defined(SAU_PRMOTE_DEMOTE_TEST)
84 #define OSAL_MALLOC_STATIC
85 #else
86 #undef OSAL_MALLOC_STATIC
87 #endif
89 #if defined(OSAL_MALLOC_STATIC)
90 uint8_t osal_malloc_block[OSAL_MALLOC_BLK_SIZE] __attribute__ ((aligned (128))); /* 200 KB memory reserved */
91 #endif
94 void *Osal_malloc (uint32_t size)
95 {
96 void *result;
97 char msg_malloc[80] = "WARNING: malloc overrun heap!!!\n";
98 #if defined(OSAL_MALLOC_STATIC)
99 uint32_t index = size_malloced;
100 #endif
101 size_malloced += size;
103 if (size_malloced >= OSAL_MALLOC_BLK_SIZE) {
104 Sa_osalLog (msg_malloc);
105 result = (void *) NULL;
106 }
107 else
108 {
109 #if defined(OSAL_MALLOC_STATIC)
110 result = &osal_malloc_block[index];
111 #else
112 result = malloc(size);
113 #endif
114 }
115 //Osal_Log ("Osal_malloc(%d) @ 0x%08x (sum=%x)\n", size, result, size_malloced);
116 return result;
117 }
119 void *Osal_Malloc
120 (
121 uint32_t region,
122 uint32_t size,
123 uint32_t align,
124 uint32_t fWordAccess
125 )
126 {
127 size += align;
128 void *result = Osal_malloc(size);
129 if (result)
130 {
131 uintptr_t *aligned = (uintptr_t *)((uintptr_t)result & ~(align - 1));
132 if ((uintptr_t)aligned != (uintptr_t)result)
133 {
134 aligned = (uintptr_t *)((uintptr_t)aligned + align);
135 size_malloced += ((uintptr_t) aligned - (uintptr_t) result);
136 result = (void *)aligned;
137 }
138 }
139 return result;
140 }
143 /**
144 * @b Description
145 * @n
146 * The function is used to allocate a memory block of the specified size.
147 *
148 * Note: If the LLD is used by applications on multiple core, the "salldHeap"
149 * should be in shared memory
150 *
151 * @param[in] num_bytes
152 * Number of bytes to be allocated.
153 *
154 * @retval
155 * Allocated block address
156 */
157 /* Macro to align x to y */
158 #define align(x,y) ((x + y) & (~y))
160 uintptr_t fw_mem_alloc_ptr = (uintptr_t) 0x80030000UL;
161 uintptr_t fw_mem_end = (uintptr_t) 0x80040000UL;
162 uintptr_t salld_sim_malloc (uint32_t size, int alignment)
163 {
164 #ifdef USE_BIOS
165 uintptr_t ret_addr;
166 Error_Block errorBlock;
168 /* Increment the allocation counter. */
169 salldMallocCounter++;
171 /* Allocate memory. */
172 ret_addr = (uintptr_t)Memory_alloc (NULL, size, alignment, &errorBlock);
173 if (ret_addr == (uintptr_t) NULL)
174 {
175 salld_sim_halt();
176 }
177 return (ret_addr);
179 #else
180 uintptr_t ptr;
181 ptr = (uintptr_t)Osal_Malloc((uint32_t)0U, size, (uint32_t)alignment, (uint32_t) 0U);
182 if (ptr == (uintptr_t) NULL)
183 {
184 SALog ("\n\n ------- Osal Malloc error ---------\n");
185 System_flush();
186 salld_sim_halt();
187 }
188 return(ptr);
189 #endif
190 }
192 /**
193 * @b Description
194 * @n
195 * The function is used to free a memory block of the specified size allocated
196 * using Osal_saMalloc() API.
197 *
198 * @param[in] ptr
199 * Pointer to the memory block to be cleaned up.
200 *
201 * @param[in] size
202 * Size of the memory block to be cleaned up.
203 *
204 * @retval
205 * Not Applicable
206 */
207 void salld_sim_free (Ptr ptr, uint32_t size)
208 {
209 /* Increment the free counter. */
210 salldFreeCounter++;
211 #ifdef USE_BIOS
212 Memory_free (NULL, ptr, size);
213 #else
214 #if defined(OSAL_MALLOC_STATIC)
215 #else
216 free(ptr);
217 #endif
218 #endif
219 }
221 /**
222 * @brief The macro is used by the SA LLD to indicate that a block
223 * of memory is about to be accessed. If the memory block is cached then
224 * this indicates that the application would need to ensure that the cache
225 * is updated with the data from the actual memory.
226 *
227 * <b> Prototype: </b>
228 * The following is the C prototype for the expected OSAL API.
229 *
230 * @verbatim
231 void Osal_saBeginMemAccess (void* addr, uint32_t sizeWords)
232 @endverbatim
233 *
234 * <b> Parameters </b>
235 * @n Address of memory block.
236 * @n Size of memory block.
237 */
238 void Osal_saBeginMemAccess (void* addr, uint32_t size)
239 {
240 uint32_t key;
241 /* Disable Interrupts */
242 key = HwiP_disable();
243 #if defined (TEST_CORE_CACHE_COHERENT)
244 CacheP_fenceDma2Cpu((uintptr_t) addr, size, OSAL_CACHEP_COHERENT);
245 #else
246 CacheP_fenceDma2Cpu((uintptr_t) addr, size, OSAL_CACHEP_NOT_COHERENT);
247 #endif
248 /* Reenable Interrupts. */
249 HwiP_restore(key);
250 }
251 /**
252 * @brief The macro is used by the SA LLD to indicate that the block of
253 * memory has finished being updated. If the memory block is cached then the
254 * application would need to ensure that the contents of the cache are updated
255 * immediately to the actual memory.
256 *
257 * <b> Prototype: </b>
258 * The following is the C prototype for the expected OSAL API.
259 *
260 * @verbatim
261 void Osal_saEndMemAccess (void* addr, uint32_t sizeWords)
262 @endverbatim
263 *
264 * <b> Parameters </b>
265 * @n Address of memory block.
266 * @n Size of memory block.
267 */
270 void Osal_saEndMemAccess (void* addr, uint32_t size)
271 {
272 uint32_t key;
273 /* Disable Interrupts */
274 key = HwiP_disable();
275 #if defined (TEST_CORE_CACHE_COHERENT)
276 CacheP_fenceCpu2Dma((uintptr_t) addr, size, OSAL_CACHEP_COHERENT);
277 #else
278 CacheP_fenceCpu2Dma((uintptr_t) addr, size, OSAL_CACHEP_NOT_COHERENT);
279 #endif
280 /* Reenable Interrupts. */
281 HwiP_restore(key);
282 }
284 /**
285 * @brief The macro is used by the SA LLD to indicate that the security
286 * context byuffer is about to be accessed. If the security context buffer
287 * is cacheable then this indicates that the application would need to ensure
288 * that the cache is updated with the data from the actual memory since the
289 * security context will be updated by SASS Cache engine.
290 * If the security context buffers are non-cacheable then these macros can
291 * be defined to be NOP.
292 *
293 * <b> Prototype: </b>
294 * The following is the C prototype for the expected OSAL API.
295 *
296 * @verbatim
297 void Osal_saBeginScAccess (void* addr, uint32_t sizeWords)
298 @endverbatim
299 *
300 * <b> Parameters </b>
301 * @n Address of memory block.
302 * @n Size of memory block.
303 */
305 void Osal_saBeginScAccess (void* addr, uint32_t size)
306 {
307 #if defined(TEST_CORE_CACHE_COHERENT)
308 /* No cache operations are needed */
309 #else
310 uint32_t key;
311 /* Disable Interrupts */
312 key = HwiP_disable();
313 CacheP_Inv(addr, size);
314 /* Reenable Interrupts. */
315 HwiP_restore(key);
316 #endif
317 }
321 /**
322 * @brief The macro is used by the SA LLD to indicate that the security
323 * context buffer has finished being updated. If the memory block is cacheable
324 * then the application would need to ensure that the contents of the cache are
325 * updated immediately to the actual memory.
326 * If the security context buffers are non-cacheable then these macros can
327 * be defined to be NOP.
328 *
329 * <b> Prototype: </b>
330 * The following is the C prototype for the expected OSAL API.
331 *
332 * @verbatim
333 void Osal_saEndScAccess (void* addr, uint32_t sizeWords)
334 @endverbatim
335 *
336 * <b> Parameters </b>
337 * @n Address of memory block.
338 * @n Size of memory block.
339 */
341 void Osal_saEndScAccess (void* addr, uint32_t size)
342 {
343 #if defined (TEST_CORE_CACHE_COHERENT)
344 /* No cache operations are needed */
345 #else
346 uint32_t key;
347 /* Disable Interrupts */
348 key = HwiP_disable();
349 CacheP_wbInv(addr, size);
350 /* Reenable Interrupts. */
351 HwiP_restore(key);
352 #endif
353 }
355 /**
356 * @b Description
357 * @n
358 * The function is used to enter a critical section.
359 * Function protects against
360 *
361 * access from multiple threads on single core
362 * and
363 * access from multiple cores
364 *
365 * @param[in] key
366 * Key used to lock the critical section.
367 *
368 * @retval
369 * Not Applicable
370 */
371 void Osal_saMtCsEnter (uint32_t *key)
372 {
373 SemaphoreP_pend (tFramework.tfSaSem, 10000);
374 }
376 /**
377 * @b Description
378 * @n
379 * The function is used to exit a critical section
380 * protected using Osal_salldCsEnter() API.
381 *
382 * @param[in] key
383 * Key used to unlock the critical section.
384 *
385 * @retval
386 * Not Applicable
387 */
388 void Osal_saMtCsExit (uint32_t key)
389 {
390 SemaphoreP_post (tFramework.tfSaSem);
391 }
393 /**
394 * @b Description
395 * @n
396 * The function is the SA LLD OSAL Logging API which logs
397 * the messages on the console.
398 *
399 * @param[in] fmt
400 * Formatted String.
401 *
402 * @retval
403 * Not Applicable
404 */
405 void Osal_saLog ( String fmt, ... )
406 {
408 }
410 void* Osal_saGetSCPhyAddr(void* vaddr)
411 {
412 return vaddr;
413 }
415 uint16_t Osal_saGetProcId (void )
416 {
417 #ifdef _TMS320C6X
418 uint32_t coreNum = CSL_chipReadReg(CSL_CHIP_DNUM);
419 #else
420 uint32_t coreNum = 0;
421 #endif
422 return (uint16_t)coreNum;
423 }
426 /**
427 * @brief The macro is used by the SA LLD to the Endian mode of the system (SoC).
428 *
429 * <b> Prototype: </b>
430 * The following is the C prototype for the expected OSAL API.
431 *
432 * @verbatim
433 * int Osal_saGetSysEndianMode(void)
434 * @endverbatim
435 *
436 * <b> Return Value Endian mode of the system (SoC) </b>
437 * <b> Parameters </b>
438 * @n Endian mode of the system (SoC).
439 */
440 int Osal_saGetSysEndianMode(void)
441 {
442 uint32_t val[1];
443 uint8_t *pVal = (uint8_t *) val;
444 int endian;
445 val[0] = (uint32_t) 0x12345678;
446 if (*pVal == 0x12)
447 {
448 endian = (int)sa_SYS_ENDIAN_MODE_BIG;
449 }
450 else
451 {
452 endian = (int)sa_SYS_ENDIAN_MODE_LITTLE;
453 }
454 return (endian);
455 }