libparanut
A Hardware Abstraction Layer for ParaNut Architectures.
paranut.h
Go to the documentation of this file.
1 
245 /*Includes*********************************************************************/
246 
247 #include <stdint.h>
248 #include <stdlib.h>
249 #include "pn_config.h"
250 
251 /*Typedefs*********************************************************************/
252 
340 #if PN_RWIDTH == 8
341 
342  typedef int8_t PN_CID;
343  typedef int8_t PN_NUMC;
344  typedef uint8_t PN_CMSK;
345  typedef int8_t PN_NUMG;
346 
347 #elif PN_RWIDTH == 16
348 
349  typedef int16_t PN_CID;
350  typedef int16_t PN_NUMC;
351  typedef uint16_t PN_CMSK;
352  typedef int16_t PN_NUMG;
353 
354 #elif PN_RWIDTH == 32
355 
356  typedef int32_t PN_CID;
357  typedef int32_t PN_NUMC;
358  typedef uint32_t PN_CMSK;
359  typedef int32_t PN_NUMG;
360 
361 #elif PN_RWIDTH == 64
362 
363  typedef int64_t PN_CID;
364  typedef int64_t PN_NUMC;
365  typedef uint64_t PN_CMSK;
366  typedef int64_t PN_NUMG;
367 
368 #elif PN_RWIDTH == 128
369 
370  typedef int128_t PN_CID;
371  typedef int128_t PN_NUMC;
372  typedef uint128_t PN_CMSK;
373  typedef int128_t PN_NUMG;
374 
375 #else
376 
377  #error Your ParaNut has a very exotic register width that was not considered\
378  when libparanut was written. Check paranut.h, section Typedefs, and add \
379  your funky register width there.
380 
381 #endif
382 
391 /*Error Codes******************************************************************/
392 
413 #define PN_SUCCESS 0
414 
421 #define PN_ERR_PARAM (-1)
422 
429 #define PN_ERR_NOIMP (-2)
430 
437 #define PN_ERR_COPU (-3)
438 
446 #define PN_ERR_MATCH (-4)
447 
455 #define PN_ERR_LOCKOCC (-5)
456 
495 #define PN_ERR_CACHE_LINESIZE (-6)
496 
504 #define PN_ERR_EXC (-8)
505 
514 /*Modes************************************************************************/
515 
561 #define PN_M0 0x0U
562 
567 #define PN_M1 0x1U
568 
573 #define PN_M2 0x2U
574 
579 #define PN_M3 0x3U
580 
593 /*Base Module******************************************************************/
594 
661 PN_NUMC pn_numcores(void);
662 
675 PN_CMSK pn_m2cap(void);
676 
693 PN_CMSK pn_m2cap_g(PN_NUMG groupnum);
694 
711 PN_CMSK pn_m3cap(void);
712 
729 PN_CMSK pn_m3cap_g(PN_NUMG groupnum);
730 
737 PN_CID pn_coreid(void);
738 
751 PN_CID pn_coreid_g(PN_NUMG *groupnum);
752 
767 void pn_halt(void);
768 
791 int pn_halt_CoPU(PN_CID coreid);
792 
813 int pn_halt_CoPU_m(PN_CMSK coremask);
814 
841 int pn_halt_CoPU_gm(PN_CMSK *coremask_array, PN_NUMG array_size);
842 
853 unsigned int pn_clock_freq(void);
854 
864 unsigned int pn_timebase_us(void);
865 
881 long long int pn_time_ns(void);
882 
892 int pn_simulation(void);
893 
902 /*Linked Module****************************************************************/
903 
981 PN_CID pn_begin_linked(PN_NUMC numcores, void *frame_adr, char flags);
982 
990 #define PN_BEGIN_LINKED(NUMCORES) ({ \
991  char flags = 0; \
992  void *frame_addr = __builtin_frame_address(0); \
993  void *fp = NULL; \
994  asm volatile("mv %0, s0" : "=r" (fp)); \
995  if(fp == frame_addr){ \
996  flags = 1; \
997  } \
998  pn_begin_linked(NUMCORES, __builtin_frame_address(0), flags);})
999 
1052 PN_CID pn_begin_linked_m(PN_CMSK coremask, void *frame_adr, char flags);
1053 
1061 #define PN_BEGIN_LINKED_M(COREMASK) \
1062  char flags = 0; \
1063  void *frame_addr = __builtin_frame_address(0); \
1064  void *fp = NULL; \
1065  asm volatile("mv %0, s0" : "=r" (fp)); \
1066  if(fp == frame_addr){ \
1067  flags = 1; \
1068  } \
1069  pn_begin_linked_m(COREMASK, __builtin_frame_address(0), flags)
1070 
1111 PN_CID pn_begin_linked_gm(PN_CMSK *coremask_array, PN_NUMG array_size, char flags);
1112 
1126 int pn_end_linked(void);
1127 
1135 #define PN_END_LINKED() pn_end_linked()
1136 
1137 
1179 PN_CID pn_run_linked(PN_NUMC numcores, void (*func)(void *args, PN_CID cid), void *args);
1180 
1225 PN_CID pn_run_linked_m(PN_CMSK coremask, void (*func)(), void *args);
1226 
1235 PN_CID pn_run_linked_gm(PN_CMSK *coremask_array, PN_NUMG array_size, void *function, void *args);
1236 
1237 
1238 
1247 /*Threaded Module**************************************************************/
1248 
1286 void pn_thread_entry(void);
1287 
1323 PN_CID pn_begin_threaded(PN_NUMC numcores);
1324 
1360 
1401 PN_CID pn_begin_threaded_gm(PN_CMSK *coremask_array, PN_NUMG array_size);
1402 
1414 int pn_end_threaded(void);
1415 
1456 PN_CMSK pn_run_threaded(PN_NUMC numcores, void (*function)(void *args, PN_CID cid), void *args);
1457 
1501 PN_CMSK pn_run_threaded_m(PN_CMSK coremask, void (*function)(void *args, PN_CID cid), void *args);
1502 
1503 
1534 PN_CMSK pn_join_thread_m(PN_CMSK coremask, uint32_t timeout);
1535 
1544 /*Cache Module*****************************************************************/
1545 
1584 int pn_cache_init(void);
1585 
1598 int pn_cache_enable(void);
1599 
1614 int pn_cache_disable(void);
1615 
1622 unsigned long pn_cache_linesize(void);
1623 
1630 unsigned long pn_cache_size(void);
1631 
1643 int pn_cache_invalidate(void *addr, unsigned long size);
1644 
1653 int pn_cache_invalidate_all(void);
1654 
1666 int pn_cache_writeback(void *addr, unsigned long size);
1667 
1675 int pn_cache_writeback_all(void);
1676 
1689 int pn_cache_flush(void *addr, unsigned long size);
1690 
1698 int pn_cache_flush_all(void);
1699 
1708 /*Exception Module*************************************************************/
1709 
1748 void pn_exception_init(void);
1749 
1783  void (*handler)(
1784  unsigned int cause,
1785  unsigned int program_counter,
1786  unsigned int mtval),
1787  unsigned int exception_code
1788  );
1789 
1796 void pn_ecall(void);
1797 
1804 void pn_interrupt_enable(void);
1805 
1812 void pn_interrupt_disable(void);
1813 
1821 void pn_progress_mepc(void);
1822 
1831 /*Spinlock Module**************************************************************/
1832 
1896 typedef struct __pn_spinlock
1897 {
1898  PN_CID owner_ID;
1902 } _pn_spinlock;
1903 
1934 int pn_spinlock_init(_pn_spinlock *spinlock);
1935 
1952 int pn_spinlock_lock(_pn_spinlock *spinlock);
1953 
1970 int pn_spinlock_trylock(_pn_spinlock *spinlock);
1971 
1987 int pn_spinlock_unlock(_pn_spinlock *spinlock);
1988 
2007 int pn_spinlock_destroy(_pn_spinlock *spinlock);
2008 
2021 /*EOF**************************************************************************/
int pn_halt_CoPU_gm(PN_CMSK *coremask_array, PN_NUMG array_size)
Halts the CoPUs specified in the coremask_array.
Definition: pn_base.c:221
void pn_halt(void)
Halt whatever core the function is executed on.
Definition: pn_base.c:134
unsigned int pn_clock_freq(void)
Returns system clock frequency in Hz.
Definition: pn_base.c:235
PN_CMSK pn_m3cap_g(PN_NUMG groupnum)
Check which cores are capable of Mode 3 operation.
Definition: pn_base.c:107
int pn_halt_CoPU_m(PN_CMSK coremask)
Halts one or more CoPUs.
Definition: pn_base.c:187
int pn_halt_CoPU(PN_CID coreid)
Halts a CoPU.
Definition: pn_base.c:140
PN_CID pn_coreid(void)
Get the ID of the core that this function is executed on.
Definition: pn_base.c:120
unsigned int pn_timebase_us(void)
Returns machine timer timebase in us.
Definition: pn_base.c:240
PN_NUMC pn_numcores(void)
Get the number of cores in your system.
Definition: pn_base.c:62
PN_CMSK pn_m2cap(void)
Check which cores are capable of Mode 2 operation.
Definition: pn_base.c:71
PN_CMSK pn_m3cap(void)
Check which cores are capable of Mode 3 operation.
Definition: pn_base.c:93
long long int pn_time_ns(void)
Returns system time in ns. Does not care for overflow.
Definition: pn_base.c:245
PN_CID pn_coreid_g(PN_NUMG *groupnum)
Get the ID and group number of the core that this code is running on.
Definition: pn_base.c:125
PN_CMSK pn_m2cap_g(PN_NUMG groupnum)
Check which cores are capable of Mode 2 operation.
Definition: pn_base.c:80
int pn_simulation(void)
Checks if we run in simulation instead of real hardware.
Definition: pn_base.c:291
int pn_cache_disable(void)
Disables instruction and data cache.
Definition: pn_cache.c:351
unsigned long pn_cache_linesize(void)
Returns the cache line size in bit.
Definition: pn_cache.c:368
unsigned long pn_cache_size(void)
Returns the cache size in Byte.
Definition: pn_cache.c:373
int pn_cache_flush(void *addr, unsigned long size)
Combination of pn_cache_invalidate() and pn_cache_writeback().
Definition: pn_cache.c:403
int pn_cache_invalidate(void *addr, unsigned long size)
Invalidates the cache entries containing the given address range.
Definition: pn_cache.c:378
int pn_cache_flush_all(void)
Flushes the whole cache.
Definition: pn_cache.c:408
int pn_cache_init(void)
Function that has to be called in the main function before any of the functions in the Cache Module c...
Definition: pn_cache.c:211
int pn_cache_enable(void)
Enables instruction and data cache. When changing this, make sure that the pn_cache_init() function i...
Definition: pn_cache.c:333
int pn_cache_invalidate_all(void)
Invalidates the whole cache. When changing this, make sure that the pn_cache_init() function is still...
Definition: pn_cache.c:383
int pn_cache_writeback(void *addr, unsigned long size)
Writes back the cache lines that cached the given address range.
Definition: pn_cache.c:393
int pn_cache_writeback_all(void)
Writes whole cache back.
Definition: pn_cache.c:398
int pn_exception_set_handler(void(*handler)(unsigned int cause, unsigned int program_counter, unsigned int mtval), unsigned int exception_code)
Set your own exception handler.
Definition: pn_exception.c:370
void pn_interrupt_disable(void)
Disables interrupts only.
Definition: pn_exception.c:546
void pn_interrupt_enable(void)
Enables interrupts only.
Definition: pn_exception.c:537
void pn_progress_mepc(void)
Sets program counter of the register which keeps the exception return adress to next instruction.
Definition: pn_exception.c:555
void pn_exception_init(void)
Initializes libparanut internal exception handling. Interrupts (not exceptions in general!...
Definition: pn_exception.c:362
void pn_ecall(void)
Raises an environment call exception.
Definition: pn_exception.c:529
PN_CID pn_run_linked_gm(PN_CMSK *coremask_array, PN_NUMG array_size, void *function, void *args)
NOT IMPLEMENTED YET, run a function in linked mode using group mask.
Definition: pn_link.c:186
PN_CID pn_begin_linked_gm(PN_CMSK *coremask_array, PN_NUMG array_size, char flags)
Definition: pn_link.c:102
PN_CID pn_run_linked(PN_NUMC numcores, void(*func)(), void *args)
Starts a given function in linked mode.
Definition: pn_link.c:139
int pn_end_linked(void)
Ends linked execution.
Definition: pn_link.c:112
int pn_spinlock_lock(_pn_spinlock *spinlock)
Waits for a lock. Forever, if it must. Use with caution.
Definition: pn_spinlock.c:79
int pn_spinlock_destroy(_pn_spinlock *spinlock)
Destroys a lock.
Definition: pn_spinlock.c:139
int pn_spinlock_init(_pn_spinlock *spinlock)
Creates a lock.
Definition: pn_spinlock.c:64
int pn_spinlock_trylock(_pn_spinlock *spinlock)
Tries to acquire a lock. Nonblocking.
Definition: pn_spinlock.c:98
int pn_spinlock_unlock(_pn_spinlock *spinlock)
Unlocks a lock.
Definition: pn_spinlock.c:114
PN_CID pn_begin_threaded_gm(PN_CMSK *coremask_array, PN_NUMG array_size)
Puts the CPUs specified in the coremask_array in threaded mode.
Definition: pn_thread.c:166
PN_CMSK pn_join_thread_m(PN_CMSK coremask, uint32_t timeout)
Waits until a specified set of CePUs is stopped.
Definition: pn_thread.c:259
void pn_thread_entry(void)
Function that has to be called for CoPUs at the end of the startup code.
Definition: pn_thread.c:79
PN_CID pn_begin_threaded(PN_NUMC numcores)
Puts numcores CPUs in threaded mode.
Definition: pn_thread.c:116
PN_CMSK pn_run_threaded(PN_NUMC numcores, void(*function)(void *args, PN_CID cid), void *args)
Excutes a given function on the CoPUs specified in numcores.
Definition: pn_thread.c:209
int pn_end_threaded(void)
Ends threaded execution.
Definition: pn_thread.c:176
PN_CID pn_begin_threaded_m(PN_CMSK coremask)
Puts the CPUs specified in the coremask in threaded mode.
Definition: pn_thread.c:145
int32_t PN_CID
Signed type that can be used to address any core in this architecture.
Definition: paranut.h:356
int32_t PN_NUMG
Signed type that can be used to address any group in this architecture.
Definition: paranut.h:359
struct __pn_spinlock _pn_spinlock
Renaming of struct __pn_spinlock for your convenience.
int32_t PN_NUMC
Signed type that can hold the maximum number of cores in this architecture.
Definition: paranut.h:357
uint32_t PN_CMSK
Unsigned type that can act as a core mask.
Definition: paranut.h:358
A synchronization primitive. Use _pn_spinlock instead of this.
Definition: paranut.h:1897