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-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 */
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 #if defined(OSAL_MALLOC_STATIC)
98 uint32_t index = size_malloced;
99 #endif
100 size_malloced += size;
102 if (size_malloced >= OSAL_MALLOC_BLK_SIZE) {
103 Sa_osalLog ("WARNING: malloc overrun heap!!!\n");
104 result = (void *) NULL;
105 }
106 else
107 {
108 #if defined(OSAL_MALLOC_STATIC)
109 result = &osal_malloc_block[index];
110 #else
111 result = malloc(size);
112 #endif
113 }
114 //Osal_Log ("Osal_malloc(%d) @ 0x%08x (sum=%x)\n", size, result, size_malloced);
115 return result;
116 }
118 void *Osal_Malloc
119 (
120 uint32_t region,
121 uint32_t size,
122 uint32_t align,
123 uint32_t fWordAccess
124 )
125 {
126 size += align;
127 void *result = Osal_malloc(size);
128 if (result)
129 {
130 uintptr_t *aligned = (uintptr_t *)((uintptr_t)result & ~(align - 1));
131 if ((uintptr_t)aligned != (uintptr_t)result)
132 {
133 aligned = (uintptr_t *)((uintptr_t)aligned + align);
134 size_malloced += ((uintptr_t) aligned - (uintptr_t) result);
135 result = (void *)aligned;
136 }
137 }
138 return result;
139 }
142 /**
143 * @b Description
144 * @n
145 * The function is used to allocate a memory block of the specified size.
146 *
147 * Note: If the LLD is used by applications on multiple core, the "salldHeap"
148 * should be in shared memory
149 *
150 * @param[in] num_bytes
151 * Number of bytes to be allocated.
152 *
153 * @retval
154 * Allocated block address
155 */
156 /* Macro to align x to y */
157 #define align(x,y) ((x + y) & (~y))
159 uintptr_t fw_mem_alloc_ptr = (uintptr_t) 0x80030000UL;
160 uintptr_t fw_mem_end = (uintptr_t) 0x80040000UL;
161 uintptr_t salld_sim_malloc (uint32_t size, int alignment)
162 {
163 #ifdef USE_BIOS
164 uintptr_t ret_addr;
165 Error_Block errorBlock;
167 /* Increment the allocation counter. */
168 salldMallocCounter++;
170 /* Allocate memory. */
171 ret_addr = (uintptr_t)Memory_alloc (NULL, size, alignment, &errorBlock);
172 if (ret_addr == (uintptr_t) NULL)
173 {
174 salld_sim_halt();
175 }
176 return (ret_addr);
178 #else
179 uintptr_t ptr;
180 ptr = (uintptr_t)Osal_Malloc((uint32_t)0U, size, (uint32_t)alignment, (uint32_t) 0U);
181 if (ptr == (uintptr_t) NULL)
182 {
183 SALog ("\n\n ------- Osal Malloc error ---------\n");
184 System_flush();
185 salld_sim_halt();
186 }
187 return(ptr);
188 #endif
189 }
191 /**
192 * @b Description
193 * @n
194 * The function is used to free a memory block of the specified size allocated
195 * using Osal_saMalloc() API.
196 *
197 * @param[in] ptr
198 * Pointer to the memory block to be cleaned up.
199 *
200 * @param[in] size
201 * Size of the memory block to be cleaned up.
202 *
203 * @retval
204 * Not Applicable
205 */
206 void salld_sim_free (Ptr ptr, uint32_t size)
207 {
208 /* Increment the free counter. */
209 salldFreeCounter++;
210 #ifdef USE_BIOS
211 Memory_free (NULL, ptr, size);
212 #else
213 #if defined(OSAL_MALLOC_STATIC)
214 #else
215 free(ptr);
216 #endif
217 #endif
218 }
220 /**
221 * @brief The macro is used by the SA LLD to indicate that a block
222 * of memory is about to be accessed. If the memory block is cached then
223 * this indicates that the application would need to ensure that the cache
224 * is updated with the data from the actual memory.
225 *
226 * <b> Prototype: </b>
227 * The following is the C prototype for the expected OSAL API.
228 *
229 * @verbatim
230 void Osal_saBeginMemAccess (void* addr, uint32_t sizeWords)
231 @endverbatim
232 *
233 * <b> Parameters </b>
234 * @n Address of memory block.
235 * @n Size of memory block.
236 */
237 void Osal_saBeginMemAccess (void* addr, uint32_t size)
238 {
239 uint32_t key;
240 /* Disable Interrupts */
241 key = HwiP_disable();
242 #if defined (TEST_CORE_CACHE_COHERENT)
243 CacheP_fenceDma2Cpu((uintptr_t) addr, size, OSAL_CACHEP_COHERENT);
244 #else
245 CacheP_fenceDma2Cpu((uintptr_t) addr, size, OSAL_CACHEP_NOT_COHERENT);
246 #endif
247 /* Reenable Interrupts. */
248 HwiP_restore(key);
249 }
250 /**
251 * @brief The macro is used by the SA LLD to indicate that the block of
252 * memory has finished being updated. If the memory block is cached then the
253 * application would need to ensure that the contents of the cache are updated
254 * immediately to the actual memory.
255 *
256 * <b> Prototype: </b>
257 * The following is the C prototype for the expected OSAL API.
258 *
259 * @verbatim
260 void Osal_saEndMemAccess (void* addr, uint32_t sizeWords)
261 @endverbatim
262 *
263 * <b> Parameters </b>
264 * @n Address of memory block.
265 * @n Size of memory block.
266 */
269 void Osal_saEndMemAccess (void* addr, uint32_t size)
270 {
271 uint32_t key;
272 /* Disable Interrupts */
273 key = HwiP_disable();
274 #if defined (TEST_CORE_CACHE_COHERENT)
275 CacheP_fenceCpu2Dma((uintptr_t) addr, size, OSAL_CACHEP_COHERENT);
276 #else
277 CacheP_fenceCpu2Dma((uintptr_t) addr, size, OSAL_CACHEP_NOT_COHERENT);
278 #endif
279 /* Reenable Interrupts. */
280 HwiP_restore(key);
281 }
283 /**
284 * @brief The macro is used by the SA LLD to indicate that the security
285 * context byuffer is about to be accessed. If the security context buffer
286 * is cacheable then this indicates that the application would need to ensure
287 * that the cache is updated with the data from the actual memory since the
288 * security context will be updated by SASS Cache engine.
289 * If the security context buffers are non-cacheable then these macros can
290 * be defined to be NOP.
291 *
292 * <b> Prototype: </b>
293 * The following is the C prototype for the expected OSAL API.
294 *
295 * @verbatim
296 void Osal_saBeginScAccess (void* addr, uint32_t sizeWords)
297 @endverbatim
298 *
299 * <b> Parameters </b>
300 * @n Address of memory block.
301 * @n Size of memory block.
302 */
304 void Osal_saBeginScAccess (void* addr, uint32_t size)
305 {
306 #if defined(TEST_CORE_CACHE_COHERENT)
307 /* No cache operations are needed */
308 #else
309 uint32_t key;
310 /* Disable Interrupts */
311 key = HwiP_disable();
312 CacheP_Inv(addr, size);
313 /* Reenable Interrupts. */
314 HwiP_restore(key);
315 #endif
316 }
320 /**
321 * @brief The macro is used by the SA LLD to indicate that the security
322 * context buffer has finished being updated. If the memory block is cacheable
323 * then the application would need to ensure that the contents of the cache are
324 * updated immediately to the actual memory.
325 * If the security context buffers are non-cacheable then these macros can
326 * be defined to be NOP.
327 *
328 * <b> Prototype: </b>
329 * The following is the C prototype for the expected OSAL API.
330 *
331 * @verbatim
332 void Osal_saEndScAccess (void* addr, uint32_t sizeWords)
333 @endverbatim
334 *
335 * <b> Parameters </b>
336 * @n Address of memory block.
337 * @n Size of memory block.
338 */
340 void Osal_saEndScAccess (void* addr, uint32_t size)
341 {
342 #if defined (TEST_CORE_CACHE_COHERENT)
343 /* No cache operations are needed */
344 #else
345 uint32_t key;
346 /* Disable Interrupts */
347 key = HwiP_disable();
348 CacheP_wbInv(addr, size);
349 /* Reenable Interrupts. */
350 HwiP_restore(key);
351 #endif
352 }
354 /**
355 * @b Description
356 * @n
357 * The function is used to enter a critical section.
358 * Function protects against
359 *
360 * access from multiple threads on single core
361 * and
362 * access from multiple cores
363 *
364 * @param[in] key
365 * Key used to lock the critical section.
366 *
367 * @retval
368 * Not Applicable
369 */
370 void Osal_saMtCsEnter (uint32_t *key)
371 {
372 SemaphoreP_pend (tFramework.tfSaSem, 10000);
373 }
375 /**
376 * @b Description
377 * @n
378 * The function is used to exit a critical section
379 * protected using Osal_salldCsEnter() API.
380 *
381 * @param[in] key
382 * Key used to unlock the critical section.
383 *
384 * @retval
385 * Not Applicable
386 */
387 void Osal_saMtCsExit (uint32_t key)
388 {
389 SemaphoreP_post (tFramework.tfSaSem);
390 }
392 /**
393 * @b Description
394 * @n
395 * The function is the SA LLD OSAL Logging API which logs
396 * the messages on the console.
397 *
398 * @param[in] fmt
399 * Formatted String.
400 *
401 * @retval
402 * Not Applicable
403 */
404 void Osal_saLog ( String fmt, ... )
405 {
407 }
409 void* Osal_saGetSCPhyAddr(void* vaddr)
410 {
411 return vaddr;
412 }
414 uint16_t Osal_saGetProcId (void )
415 {
416 #ifdef _TMS320C6X
417 uint32_t coreNum = CSL_chipReadReg(CSL_CHIP_DNUM);
418 #else
419 uint32_t coreNum = 0;
420 #endif
421 return (uint16_t)coreNum;
422 }
425 /**
426 * @brief The macro is used by the SA LLD to the Endian mode of the system (SoC).
427 *
428 * <b> Prototype: </b>
429 * The following is the C prototype for the expected OSAL API.
430 *
431 * @verbatim
432 * int Osal_saGetSysEndianMode(void)
433 * @endverbatim
434 *
435 * <b> Return Value Endian mode of the system (SoC) </b>
436 * <b> Parameters </b>
437 * @n Endian mode of the system (SoC).
438 */
439 int Osal_saGetSysEndianMode(void)
440 {
441 uint32_t val[1];
442 uint8_t *pVal = (uint8_t *) val;
443 int endian;
444 val[0] = (uint32_t) 0x12345678;
445 if (*pVal == 0x12)
446 {
447 endian = (int)sa_SYS_ENDIAN_MODE_BIG;
448 }
449 else
450 {
451 endian = (int)sa_SYS_ENDIAN_MODE_LITTLE;
452 }
453 return (endian);
454 }