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 #ifndef DISABLE_OSAL
44 /* CSL RL includes */
45 #include <ti/csl/cslr_device.h>
46 #include <ti/csl/csl_pscAux.h>
47 #include <ti/csl/csl_semAux.h>
48 #include <ti/csl/csl_cacheAux.h>
49 #include <ti/csl/csl_xmcAux.h>
51 #include <stdlib.h>
52 #include <stdio.h>
54 #include "hplib.h"
55 #include <unistd.h>
56 #include <sys/mman.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/ioctl.h>
60 #include <fcntl.h>
61 #include <errno.h>
62 #include <string.h>
63 //#include "netapi_util.h"
64 #include "hplibmod.h"
65 #include <ti/drv/sa/sa_osal.h>
66 #include <ti/runtime/pktlib/pktlib.h>
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 static __thread int our_core = 0;
75 void* Osal_saGetSCPhyAddr(void* vaddr);
77 static unsigned int cache_op_cycles=0;
78 static unsigned int n_cache_op_cycles=0;
79 void Osal_cache_op_measure_reset(void) { cache_op_cycles=0; n_cache_op_cycles=0;}
80 unsigned int Osal_cache_op_measure(unsigned long long * p_n) { *p_n = n_cache_op_cycles; return cache_op_cycles;}
82 static hplib_spinLock_T qmss_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
83 static hplib_spinLock_T nwal_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
84 static hplib_spinLock_T hplib_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
85 static hplib_spinLock_T cppi_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
86 static hplib_spinLock_T sa_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
87 static hplib_spinLock_T pktlib_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
89 void * Osal_qmssMtCsEnter()
90 {
91 hplib_mSpinLockLock(&qmss_lock);
92 return NULL;
93 }
96 void Osal_qmssMtCsExit(void *key)
97 {
98 hplib_mSpinLockUnlock(&qmss_lock);
99 return;
100 }
102 void Osal_nwalCsEnter(uint32_t *key)
103 {
104 hplib_mSpinLockLock(&nwal_lock);
105 }
107 void Osal_nwalCsExit(uint32_t key)
108 {
109 hplib_mSpinLockUnlock(&nwal_lock);
110 }
112 void Osal_qmssLog ( String fmt, ... )
113 {
114 return;
115 }
118 void Osal_cppiCsEnter (uint32_t *key)
119 {
120 hplib_mSpinLockLock(&cppi_lock);
121 }
123 void Osal_cppiCsExit (uint32_t key)
124 {
125 hplib_mSpinLockUnlock(&cppi_lock);
126 }
128 void Osal_paBeginMemAccess (Ptr addr, uint32_t size)
129 {
130 /*No implementation required for ARM*/
131 }
133 void Osal_paEndMemAccess (Ptr addr, uint32_t size)
134 {
135 /*No implementation required for ARM*/
136 }
137 void Osal_paMtCsEnter (uint32_t *key)
138 {
139 /*No implementation required for ARM*/
140 }
141 void Osal_paMtCsExit (uint32_t key)
142 {
143 /*No implementation required for ARM*/
144 }
146 void* Osal_qmssCsEnter ()
147 {
148 hplib_mSpinLockLock(&qmss_lock);
149 return NULL;
150 }
152 void Osal_qmssCsExit (void * key)
153 {
154 hplib_mSpinLockUnlock(&qmss_lock);
155 }
157 Ptr Osal_qmssMalloc (uint32_t num_bytes)
158 {
159 Ptr ret;
161 Osal_qmss_MallocCounter++;
162 ret = malloc (num_bytes);
163 return ret;
164 }
166 void Osal_qmssFree (Ptr ptr, uint32_t size)
167 {
168 /* Increment the free counter. */
169 Osal_qmss_FreeCounter++;
170 free(ptr);
171 }
173 Ptr Osal_cppiMalloc (uint32_t num_bytes)
174 {
175 Ptr ret;
177 Osal_cppi_MallocCounter++;
178 //num_bytes += (CACHE_L2_LINESIZE-1);
179 num_bytes += (127);
180 ret = malloc (num_bytes);
181 return ret;
182 }
184 void Osal_cppiFree (Ptr ptr, uint32_t size)
185 {
186 /* Increment the free counter. */
187 Osal_cppi_FreeCounter++;
188 free(ptr);
189 }
191 void Osal_qmssBeginMemAccess (void *blockPtr, uint32_t size)
192 {
193 return;
194 }
196 void Osal_qmssEndMemAccess (void *blockPtr, uint32_t size)
197 {
198 return;
199 }
201 void Osal_cppiBeginMemAccess (void *blockPtr, uint32_t size)
202 {
203 return;
204 }
206 void Osal_cppiEndMemAccess (void *blockPtr, uint32_t size)
207 {
208 return;
209 }
211 void Osal_nwalInvalidateCache (void *blockPtr, uint32_t size)
212 {
213 return;
214 }
216 void Osal_nwalWriteBackCache (void *blockPtr, uint32_t size)
217 {
218 return;
219 }
221 uint32_t Osal_nwalGetCacheLineSize (void )
222 {
223 /* By default assumes L2 cache line is enabled. If not return CACHE_L1D_LINESIZE */
224 return (CACHE_L2_LINESIZE);
225 }
227 /********************************************************************
228 * FUNCTION PURPOSE: Convert local address to global
229 ********************************************************************
230 * DESCRIPTION: Returns global address
231 ********************************************************************/
233 unsigned int Osal_nwalLocToGlobAddr(unsigned int x)
234 {
235 return x;
236 }
239 void Osal_nwalSetProcId (uint16_t core_id )
240 {
241 our_core = core_id;
242 }
245 uint16_t Osal_nwalGetProcId (void )
246 {
247 return our_core;
248 }
250 uint64_t Osal_nwalGetTimeStamp(void)
251 {
252 return hplib_mUtilGetTimestamp();
253 }
255 uint16_t Osal_saGetProcId (void )
256 {
257 return 0;
258 }
260 void* Osal_saGetSCPhyAddr(void* vaddr)
261 {
262 if(vaddr == NULL)
263 {
264 return NULL;
265 }
266 return (void *)(memPoolAddr[0].memStartPhy + ((uint8_t*) vaddr - memPoolAddr[0].memStart));
267 }
269 void Osal_saBeginScAccess (void* addr, uint32_t size)
270 {
271 hplib_cacheInv(addr,size);
272 }
274 void Osal_saEndScAccess (void* addr, uint32_t size)
275 {
276 hplib_cacheWb(addr,size);
277 }
280 void Osal_saCsEnter (uint32_t *key)
281 {
282 hplib_mSpinLockLock(&sa_lock);
283 }
285 void Osal_saCsExit (uint32_t key)
286 {
287 hplib_mSpinLockUnlock(&sa_lock);
288 }
291 void Osal_saMtCsEnter (uint32_t *key)
292 {
293 hplib_mSpinLockLock(&sa_lock);
294 }
296 void Osal_saMtCsExit (uint32_t key)
297 {
298 hplib_mSpinLockUnlock(&sa_lock);
299 }
301 void Osal_saBeginMemAccess (void *blockPtr, uint32_t size)
302 {
303 /* not required on ARM */
304 /*Osal_invalidateCache(blockPtr,size); */
305 }
307 void Osal_saEndMemAccess (void *blockPtr, uint32_t size)
308 {
309 /* not required on ARM */
310 /* Osal_writeBackCache(blockPtr,size); */
311 }
313 int Osal_saGetSysEndianMode(void)
314 {
315 #if defined( _BIG_ENDIAN )
316 return((int)sa_SYS_ENDIAN_MODE_BIG);
317 #else
318 return((int)sa_SYS_ENDIAN_MODE_LITTLE);
319 #endif
320 }
322 void Osal_pktLibBeginMemAccess(void* ptr, uint32_t size)
323 {
324 //Osal_invalidateCache(ptr,size);
325 }
328 void Osal_pktLibEndMemAccess(void* ptr, uint32_t size)
329 {
330 //Osal_writeBackCache(ptr,size);
331 }
334 void Osal_pktLibBeginPktAccess(Pktlib_HeapHandle heapHandle, Ti_Pkt* ptrPkt, uint32_t size)
335 {
336 /* not required on ARM */
337 /* Osal_invalidateCache(ptrPkt,size); */
338 }
341 void Osal_pktLibEndPktAccess(Pktlib_HeapHandle heapHandle, Ti_Pkt* ptrPkt, uint32_t size)
342 {
344 /* Cache Write back for the packet. Currently being disabled as it will be done during
345 * QMSS Push operation
347 Osal_writeBackCache((void *)ptrPkt,size);
348 */
349 }
352 void* Osal_pktLibEnterCriticalSection(Pktlib_HeapHandle heapHandle)
353 {
354 /* TODO: We should use the 'heapHandle' and compare it with what we got from the
355 * 'create/find' HEAP API & depending upon the comparison take appropriate action.
356 * Implementations here could range from a MULTI-THREAD protection if the packets in
357 * the heap are being accessed across multiple threads or MULTI-CORE if the packets
358 * are being accessed across multiple cores and features: split and clone are used.
359 * For NWAL layer no protection required.
360 *
361 * For testing we are not doing any of this so we are simply setting it to NOOP */
362 hplib_mSpinLockLock(&pktlib_lock);
363 return NULL;
364 }
367 void Osal_pktLibExitCriticalSection(Pktlib_HeapHandle heapHandle, void* csHandle)
368 {
369 /* TODO: We should use the 'heapHandle' and compare it with what we got from the
370 * 'create/find' HEAP API & depending upon the comparison take appropriate action.
371 * Implementations here could range from a MULTI-THREAD protection if the packets in
372 * the heap are being accessed across multiple threads or MULTI-CORE if the packets
373 * are being accessed across multiple cores and features: split and clone are used.
374 * For NWAL layer no protection required..
375 *
376 * For testing we are not doing any of this so we are simply setting it to NOOP */
377 hplib_mSpinLockUnlock(&pktlib_lock);
378 return;
379 }
381 void* Osal_pktLibPhyToVirt(void *ptr)
382 {
383 return(hplib_mVMPhyToVirt(ptr));
384 }
386 void* Osal_qmssVirtToPhy (void *ptr)
387 {
388 return hplib_mVMVirtToPhy(ptr);
389 }
391 void * Osal_qmssPhyToVirt (void *ptr)
392 {
393 return hplib_mVMPhyToVirt(ptr);
394 }
396 /******************************************************************************
397 * Function to traverse a CPPI descriptor and convert all address references
398 * from virtual to physical.
399 ******************************************************************************/
400 //#define ASSUME_ALL_DESCRIPTOR //define this if mono and host descriptors are present, else don't
401 //define and just host will be assumed (more efficient)
402 void* Osal_qmssConvertDescVirtToPhy(void *descAddr)
403 {
404 return hplib_mVMConvertDescVirtToPhy(descAddr);
405 }
406 /******************************************************************************
407 * Function to traverse a CPPI descriptor and convert all address references
408 * from physical to virtual.
409 ******************************************************************************/
410 void* Osal_qmssConvertDescPhyToVirt(void *descAddr)
411 {
412 return hplib_mVMConvertDescPhyToVirt(descAddr);
413 }
414 void* Osal_stubCsEnter (void)
415 {
416 return NULL;
417 }
418 void Osal_stubCsExit (void *CsHandle)
419 {
420 return;
421 }
422 void* Osal_hplibCsEnter (void)
423 {
424 hplib_mSpinLockLock(&hplib_lock);
425 return NULL;
427 }
428 void Osal_hplibCsExit (void *CsHandle)
429 {
430 hplib_mSpinLockUnlock(&hplib_lock);
431 return;
432 }
433 #endif