1 /******************************************************************************
2 * FILE PURPOSE: Functions to OSAL related routines for running NWAL, PA, QMSS,etc
3 ******************************************************************************
4 * FILE NAME: osal.c
5 *
6 * DESCRIPTION: Functions to initialize framework resources for running NWAL
7 *
8 * REVISION HISTORY:
9 *
10 * Copyright (c) Texas Instruments Incorporated 2010-2011
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
42 /* CSL RL includes */
43 #include <ti/csl/cslr_device.h>
44 #include <ti/csl/csl_pscAux.h>
45 #include <ti/csl/csl_semAux.h>
46 #include <ti/csl/csl_cacheAux.h>
47 #include <ti/csl/csl_xmcAux.h>
49 #include <stdlib.h>
50 #include <stdio.h>
52 #include "netapi_tune.h"
53 #include "hplib.h"
54 #include <unistd.h>
55 #include <sys/mman.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/ioctl.h>
59 #include <fcntl.h>
60 #include <errno.h>
61 #include <string.h>
62 #include "netapi_util.h"
63 #include "hplibmod.h"
64 #include <ti/drv/sa/sa_osal.h>
65 #include <ti/runtime/pktlib/pktlib.h>
66 #define System_printf printf
68 uint32_t Osal_qmss_MallocCounter =0;
69 uint32_t Osal_qmss_FreeCounter =0;
70 uint32_t Osal_cppi_MallocCounter =0;
71 uint32_t Osal_cppi_FreeCounter =0;
73 void* Osal_saGetSCPhyAddr(void* vaddr);
77 /* TODO: */
78 #define DNUM 0
80 #if 0
81 uint32_t globalCritkey;
83 /* Lock to be used for critical section */
84 pthread_mutex_t mutex_lock;
86 void nwalTest_osalInit()
87 {
88 pthread_mutex_init(&mutex_lock, NULL);
89 return;
90 }
92 void nwalTest_osalshutdown()
93 {
94 pthread_mutex_destroy(&mutex_lock);
95 return;
96 }
98 static inline void nwalTest_osalEnterCS()
99 {
100 #if 0
101 pthread_mutex_lock(&mutex_lock);
102 #endif
103 return;
104 }
106 static inline void nwalTest_osalLeaveCS()
107 {
109 #if 0
110 pthread_mutex_unlock(&mutex_lock);
111 #endif
112 return;
113 }
115 #endif
116 static unsigned int cache_op_cycles=0;
117 static unsigned int n_cache_op_cycles=0;
118 void Osal_cache_op_measure_reset(void) { cache_op_cycles=0; n_cache_op_cycles=0;}
119 /*****************************************************************************
120 * FUNCTION PURPOSE: Cache Invalidation Routine
121 *****************************************************************************
122 * DESCRIPTION: Cache Invalidation Routine
123 *****************************************************************************/
124 static inline void Osal_invalidateCache (void *blockPtr, uint32_t size)
125 {
126 #ifdef NETAPI_TUNE_USE_CACHE_OPS
127 register unsigned int v1;
128 register unsigned int v2;
130 v1= hplib_mUtil_GetTickCount();
131 if ((blockPtr <hplib_VM_mem_start)||( blockPtr>hplib_VM_mem_end)) return;
132 hplib_CacheInv(blockPtr, size);
133 v2= hplib_mUtil_GetTickCount();
134 cache_op_cycles += (v2-v1);
135 n_cache_op_cycles+=1;
136 #endif
138 return;
139 }
141 /*****************************************************************************
142 * FUNCTION PURPOSE: Cache Writeback Routine
143 *****************************************************************************
144 * DESCRIPTION: Cache Invalidation Routine
145 *****************************************************************************/
146 /* stats */
148 unsigned int Osal_cache_op_measure(int * p_n) { *p_n = n_cache_op_cycles; return cache_op_cycles;}
150 static inline void Osal_writeBackCache (void *blockPtr, uint32_t size)
151 {
152 #ifdef NETAPI_TUNE_USE_CACHE_OPS
153 register unsigned int v1;
154 register unsigned int v2;
156 v1= hplib_mUtil_GetTickCount();
157 if (((uint8_t*)blockPtr <hplib_VM_mem_start)||( (uint8_t*)blockPtr>hplib_VM_mem_end)) return;
158 hplib_CacheWb(blockPtr, size);
159 v2= hplib_mUtil_GetTickCount();
160 cache_op_cycles += (v2-v1);
161 n_cache_op_cycles+=1;
162 #endif
163 return;
164 }
166 static HPLIB_SPINLOCK_T qmss_lock = HPLIB_SPINLOCK_UNLOCKED_INITIALIZER;
167 static HPLIB_SPINLOCK_T nwal_lock = HPLIB_SPINLOCK_UNLOCKED_INITIALIZER;
168 static HPLIB_SPINLOCK_T hplib_lock = HPLIB_SPINLOCK_UNLOCKED_INITIALIZER;
170 void * Osal_qmssMtCsEnter()
171 {
172 /* Stub Function. TBD: Would need to handle when for multi proc access
173 * To be handled once infrastructure is available from Kernel
174 */
175 //hplib_spinlock_lock(&qmss_lock)
176 return NULL;
177 }
180 void Osal_qmssMtCsExit(void *key)
181 {
182 /* Stub Function. TBD: Would need to handle when for multi proc access
183 * To be handled once infrastructure is available from Kernel
184 */
185 //hplib_spinlock_unlock(&qmss_lock)
186 return;
187 }
189 void Osal_nwalCsEnter(uint32_t *key)
190 {
191 /* Stub Function. TBD: Would need to handle when for multi proc access
192 * To be handled once infrastructure is available from Kernel
193 */
194 return;
195 }
197 void Osal_nwalCsExit(uint32_t key)
198 {
199 /* Stub Function. TBD: Would need to handle when for multi proc access
200 * To be handled once infrastructure is available from Kernel
201 */
202 return;
203 }
206 void Osal_qmssLog ( String fmt, ... )
207 {
208 }
211 void Osal_cppiCsEnter (uint32_t *key)
212 {
214 /* Stub Function. TBD: Would need to handle when for multi proc access
215 * To be handled once infrastructure is available from Kernel
216 */
217 return;
218 }
220 void Osal_cppiCsExit (uint32_t key)
221 {
223 /* Stub Function. TBD: Would need to handle when for multi proc access
224 * To be handled once infrastructure is available from Kernel
225 */
226 return;
227 }
229 void Osal_cppiLog ( String fmt, ... )
230 {
231 }
233 void Osal_paBeginMemAccess (Ptr addr, uint32_t size)
234 {
235 /* Stub Function. TBD: Would need to handle when for multi proc access
236 * To be handled once infrastructure is available from Kernel
237 */
239 }
241 void Osal_paEndMemAccess (Ptr addr, uint32_t size)
242 {
243 /* Stub Function. TBD: Would need to handle when for multi proc access
244 * To be handled once infrastructure is available from Kernel
245 */
246 }
247 void Osal_paMtCsEnter (uint32_t *key)
248 {
249 /* Stub Function. TBD: Would need to handle when for multi proc access
250 * To be handled once infrastructure is available from Kernel
251 */
252 }
253 void Osal_paMtCsExit (uint32_t key)
254 {
255 /* Stub Function. TBD: Would need to handle when for multi proc access
256 * To be handled once infrastructure is available from Kernel
257 */
258 }
261 void* Osal_qmssCsEnter ()
262 {
264 /* Stub Function. TBD: Would need to handle when for multi proc access
265 * To be handled once infrastructure is available from Kernel
266 */
267 return(NULL);
268 }
270 void Osal_qmssCsExit (void * key)
271 {
272 /* Stub Function. TBD: Would need to handle when for multi proc access
273 * To be handled once infrastructure is available from Kernel
274 */
275 return;
276 }
278 Ptr Osal_qmssMalloc (uint32_t num_bytes)
279 {
280 Ptr ret;
282 Osal_qmss_MallocCounter++;
283 ret = malloc (num_bytes);
284 if(ret==NULL)
285 {
286 System_printf("\nERROR! QMSS Malloc failed!\n");
287 }
289 return ret;
290 }
292 void Osal_qmssFree (Ptr ptr, uint32_t size)
293 {
294 /* Increment the free counter. */
295 Osal_qmss_FreeCounter++;
296 free(ptr);
297 }
299 Ptr Osal_cppiMalloc (uint32_t num_bytes)
300 {
301 Ptr ret;
303 Osal_cppi_MallocCounter++;
304 num_bytes += (CACHE_L2_LINESIZE-1);
305 ret = malloc (num_bytes);
307 if(ret==NULL)
308 {
309 System_printf("\nERROR! CPPI Malloc failed!\n");
310 }
312 return ret;
313 }
315 void Osal_cppiFree (Ptr ptr, uint32_t size)
316 {
317 /* Increment the free counter. */
318 Osal_cppi_FreeCounter++;
319 free(ptr);
320 }
322 void Osal_qmssBeginMemAccess (void *blockPtr, uint32_t size)
323 {
324 //Osal_invalidateCache(blockPtr,size);
325 return;
326 }
328 void Osal_qmssEndMemAccess (void *blockPtr, uint32_t size)
329 {
330 //Osal_writeBackCache(blockPtr,size);
331 return;
332 }
334 void Osal_cppiBeginMemAccess (void *blockPtr, uint32_t size)
335 {
336 //Osal_invalidateCache(blockPtr,size);
337 return;
338 }
340 void Osal_cppiEndMemAccess (void *blockPtr, uint32_t size)
341 {
342 //Osal_writeBackCache(blockPtr,size);
343 return;
344 }
346 void Osal_nwalInvalidateCache (void *blockPtr, uint32_t size)
347 {
348 //Osal_invalidateCache(blockPtr,size);
349 return;
350 }
352 void Osal_nwalWriteBackCache (void *blockPtr, uint32_t size)
353 {
354 //Osal_writeBackCache(blockPtr,size);
355 return;
356 }
358 uint32_t Osal_nwalGetCacheLineSize (void )
359 {
360 /* By default assumes L2 cache line is enabled. If not return CACHE_L1D_LINESIZE */
361 return (CACHE_L2_LINESIZE);
362 }
364 /********************************************************************
365 * FUNCTION PURPOSE: Convert local address to global
366 ********************************************************************
367 * DESCRIPTION: Returns global address
368 ********************************************************************/
370 unsigned int Osal_nwalLocToGlobAddr(unsigned int x)
371 {
372 return x;
373 }
375 uint16_t Osal_nwalGetProcId (void )
376 {
377 return DNUM;
378 }
379 uint64_t Osal_nwalGetTimeStamp(void)
380 {
381 /* Stub function to return timestamp
382 */
383 return hplib_mUtil_GetTimestamp();
384 }
386 uint16_t Osal_saGetProcId (void )
387 {
388 return 0;
389 }
391 void* Osal_saGetSCPhyAddr(void* vaddr)
392 {
393 if(vaddr == NULL)
394 {
395 return NULL;
396 }
397 return (void *)(hplib_VM_mem_start_phy + ((uint8_t*) vaddr - hplib_VM_mem_start));
399 }
401 void Osal_saBeginScAccess (void* addr, uint32_t size)
402 {
403 Osal_invalidateCache(addr,size);
405 }
407 void Osal_saEndScAccess (void* addr, uint32_t size)
408 {
409 Osal_writeBackCache(addr,size);
411 }
414 void Osal_saCsEnter (uint32_t *key)
415 {
416 /* Stub Function. TBD: Would need to handle when for multi proc access
417 * To be handled once infrastructure is available from Kernel
418 */
419 //((CSL_semAcquireDirect (SA_HW_SEM)) == 0);
420 return;
421 }
423 void Osal_saCsExit (uint32_t key)
424 {
425 /* Stub Function. TBD: Would need to handle when for multi proc access
426 * To be handled once infrastructure is available from Kernel
427 */
428 return;
429 }
432 void Osal_saMtCsEnter (uint32_t *key)
433 {
434 /* Stub Function. TBD: Would need to handle when for multi proc access
435 * To be handled once infrastructure is available from Kernel
436 */
437 return;
438 }
440 void Osal_saMtCsExit (uint32_t key)
441 {
442 /* Stub Function. TBD: Would need to handle when for multi proc access
443 * To be handled once infrastructure is available from Kernel
444 */
445 return;
446 }
448 void Osal_saBeginMemAccess (void *blockPtr, uint32_t size)
449 {
450 Osal_invalidateCache(blockPtr,size);
451 return;
452 }
454 void Osal_saEndMemAccess (void *blockPtr, uint32_t size)
455 {
456 Osal_writeBackCache(blockPtr,size);
457 return;
458 }
460 int Osal_saGetSysEndianMode(void)
461 {
462 #if defined( _BIG_ENDIAN )
463 return((int)sa_SYS_ENDIAN_MODE_BIG);
464 #else
465 return((int)sa_SYS_ENDIAN_MODE_LITTLE);
466 #endif
467 }
469 void Osal_pktLibBeginMemAccess(void* ptr, uint32_t size)
470 {
471 //Osal_invalidateCache(ptr,size);
472 }
475 void Osal_pktLibEndMemAccess(void* ptr, uint32_t size)
476 {
477 //Osal_writeBackCache(ptr,size);
478 }
481 void Osal_pktLibBeginPktAccess(Pktlib_HeapHandle heapHandle, Ti_Pkt* ptrPkt, uint32_t size)
482 {
483 Osal_invalidateCache(ptrPkt,size);
484 }
487 void Osal_pktLibEndPktAccess(Pktlib_HeapHandle heapHandle, Ti_Pkt* ptrPkt, uint32_t size)
488 {
490 /* Cache Write back for the packet. Currently being disabled as it will be done during
491 * QMSS Push operation
493 Osal_writeBackCache((void *)ptrPkt,size);
494 */
495 }
498 void* Osal_pktLibEnterCriticalSection(Pktlib_HeapHandle heapHandle)
499 {
500 /* TODO: We should use the 'heapHandle' and compare it with what we got from the
501 * 'create/find' HEAP API & depending upon the comparison take appropriate action.
502 * Implementations here could range from a MULTI-THREAD protection if the packets in
503 * the heap are being accessed across multiple threads or MULTI-CORE if the packets
504 * are being accessed across multiple cores and features: split and clone are used.
505 * For NWAL layer no protection required.
506 *
507 * For testing we are not doing any of this so we are simply setting it to NOOP */
508 return NULL;
509 }
512 void Osal_pktLibExitCriticalSection(Pktlib_HeapHandle heapHandle, void* csHandle)
513 {
514 /* TODO: We should use the 'heapHandle' and compare it with what we got from the
515 * 'create/find' HEAP API & depending upon the comparison take appropriate action.
516 * Implementations here could range from a MULTI-THREAD protection if the packets in
517 * the heap are being accessed across multiple threads or MULTI-CORE if the packets
518 * are being accessed across multiple cores and features: split and clone are used.
519 * For NWAL layer no protection required..
520 *
521 * For testing we are not doing any of this so we are simply setting it to NOOP */
522 return;
523 }
526 void* Osal_pktLibPhyToVirt(void *ptr)
527 {
528 return(hplib_mVM_PhyToVirt(ptr));
529 }
531 void* Osal_qmssVirtToPhy (void *ptr)
532 {
533 return hplib_mVM_VirtToPhy(ptr);
534 }
536 void * Osal_qmssPhyToVirt (void *ptr)
537 {
538 return hplib_mVM_PhyToVirt(ptr);
539 }
541 /******************************************************************************
542 * Function to traverse a CPPI descriptor and convert all address references
543 * from virtual to physical.
544 ******************************************************************************/
545 //#define ASSUME_ALL_DESCRIPTOR //define this if mono and host descriptors are present, else don't
546 //define and just host will be assumed (more efficient)
547 void* Osal_qmssConvertDescVirtToPhy(void *descAddr)
548 {
549 hplib_Util_ConvertDescVirtToPhy(descAddr);
550 }
551 /******************************************************************************
552 * Function to traverse a CPPI descriptor and convert all address references
553 * from physical to virtual.
554 ******************************************************************************/
555 void* Osal_qmssConvertDescPhyToVirt(void *descAddr)
556 {
557 hplib_Util_ConvertDescPhyToVirt(descAddr);
558 }
559 void* Osal_stubCsEnter (void)
560 {
563 }
564 void Osal_stubCsExit (void *CsHandle)
565 {
566 /* Release Semaphore using handle */
569 return;
570 }
571 void* Osal_hplibCsEnter (void)
572 {
574 hplib_mSpinlockLock(&hplib_lock);
576 }
577 void Osal_hplibCsExit (void *CsHandle)
578 {
579 hplib_mSpinlockUnlock(&hplib_lock);
581 return;
582 }