portmacro.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * FreeRTOS Kernel Port For Nuclei RISC-V Processor
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. #ifndef PORTMACRO_H
  28. #define PORTMACRO_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include "nuclei_sdk_soc.h"
  33. /*-----------------------------------------------------------
  34. * Port specific definitions.
  35. *
  36. * The settings in this file configure FreeRTOS correctly for the
  37. * given hardware and compiler.
  38. *
  39. * These settings should not be altered.
  40. *-----------------------------------------------------------
  41. */
  42. /* Type definitions. */
  43. #define portCHAR char
  44. #define portFLOAT float
  45. #define portDOUBLE double
  46. #define portLONG long
  47. #define portSHORT short
  48. #define portSTACK_TYPE unsigned long
  49. #define portBASE_TYPE long
  50. #define portPOINTER_SIZE_TYPE unsigned long
  51. typedef portSTACK_TYPE StackType_t;
  52. typedef long BaseType_t;
  53. typedef unsigned long UBaseType_t;
  54. /* TODO If you want to use openocd rtos-aware debug, you need to define OPENOCD_RTOS_AWARE macro */
  55. //#define OPENOCD_RTOS_AWARE
  56. #ifdef OPENOCD_RTOS_AWARE
  57. #undef configTICK_TYPE_WIDTH_IN_BITS
  58. #define configTICK_TYPE_WIDTH_IN_BITS TICK_TYPE_WIDTH_32_BITS
  59. #endif
  60. /* TODO If you dont want features provided by configMAX_SYSCALL_INTERRUPT_PRIORITY */
  61. /* If configMAX_SYSCALL_INTERRUPT_PRIORITY >= 255, interrupt disable/enable will use MSTATUS.MIE,
  62. * otherwise interrupt disable/enable will use ECLIC.MTH to control interrupt threshold,
  63. * this will make Interrupts that do not call API functions can execute at priorities above configMAX_SYSCALL_INTERRUPT_PRIORITY
  64. * and therefore never be delayed by the RTOS kernel execution.
  65. * For configMAX_SYSCALL_INTERRUPT_PRIORITY set value, please see port.c for details
  66. * About FreeRTOS configuration, see see https://www.freertos.org/zh-cn-cmn-s/Documentation/02-Kernel/03-Supported-devices/02-Customization
  67. */
  68. #ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
  69. // See function prvCheckMaxSysCallPrio and prvCalcMaxSysCallMTH
  70. #define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
  71. #endif
  72. // configTICK_TYPE_WIDTH_IN_BITS
  73. // see https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/03db672b8f45db24aa99f12051f7cf86746b9ed9/examples/template_configuration/FreeRTOSConfig.h#L124-L139
  74. #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
  75. typedef uint16_t TickType_t;
  76. #define portMAX_DELAY ( TickType_t )0xffff
  77. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
  78. typedef uint32_t TickType_t;
  79. #define portMAX_DELAY ( TickType_t )0xFFFFFFFFUL
  80. #else
  81. typedef uint64_t TickType_t;
  82. #define portMAX_DELAY ( TickType_t )0xFFFFFFFFFFFFFFFFULL
  83. #endif
  84. /*-----------------------------------------------------------*/
  85. /* Architecture specifics. */
  86. #define portSTACK_GROWTH ( -1 )
  87. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  88. #ifndef __riscv_32e
  89. /* The stack grows downwards (towards lower addresses) and the stack pointer shall be aligned to a 128-bit boundary upon procedure entry. */
  90. #define portBYTE_ALIGNMENT 16
  91. #else
  92. /* ILP32E calling convention The stack pointer need only be aligned to a 32-bit boundary */
  93. #define portBYTE_ALIGNMENT 4
  94. #endif
  95. /*-----------------------------------------------------------*/
  96. /* Scheduler utilities. */
  97. #define portYIELD() \
  98. { \
  99. /* Set a software interrupt(SWI) request to request a context switch. */ \
  100. SysTimer_SetSWIRQ(); \
  101. /* Barriers are normally not required but do ensure the code is completely \
  102. within the specified behaviour for the architecture. */ \
  103. __RWMB(); \
  104. }
  105. #define portEND_SWITCHING_ISR( xSwitchRequired ) if ( xSwitchRequired != pdFALSE ) portYIELD()
  106. #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
  107. /*-----------------------------------------------------------*/
  108. #if configMAX_SYSCALL_INTERRUPT_PRIORITY >= 255
  109. #define portSET_INTERRUPT_MASK_FROM_ISR() __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE)
  110. #define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) __RV_CSR_WRITE(CSR_MSTATUS, (x))
  111. #define portDISABLE_INTERRUPTS() __RV_CSR_CLEAR(CSR_MSTATUS, MSTATUS_MIE)
  112. #define portENABLE_INTERRUPTS() __RV_CSR_SET(CSR_MSTATUS, MSTATUS_MIE)
  113. #else
  114. #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
  115. #define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) vPortSetBASEPRI(x)
  116. #define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI()
  117. #define portENABLE_INTERRUPTS() vPortSetBASEPRI(0)
  118. #endif
  119. /*-----------------------------------------------------------*/
  120. /* Task function macros as described on the FreeRTOS.org WEB site. These are
  121. not necessary for to use this port. They are defined so the common demo files
  122. (which build with all the ports) will build. */
  123. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
  124. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
  125. /*-----------------------------------------------------------*/
  126. /* Tickless idle/low power functionality. */
  127. #ifndef portSUPPRESS_TICKS_AND_SLEEP
  128. extern void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime);
  129. #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime ) vPortSuppressTicksAndSleep( xExpectedIdleTime )
  130. #endif
  131. /*-----------------------------------------------------------*/
  132. /*-----------------------------------------------------------*/
  133. #ifdef configASSERT
  134. extern void vPortValidateInterruptPriority(void);
  135. #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() vPortValidateInterruptPriority()
  136. #endif
  137. /* portNOP() is not required by this port. */
  138. #define portNOP() __NOP()
  139. #define portINLINE __inline
  140. #ifndef portFORCE_INLINE
  141. #define portFORCE_INLINE inline __attribute__(( always_inline))
  142. #endif
  143. #if configMAX_SYSCALL_INTERRUPT_PRIORITY < 255
  144. /* This variable should not be set in any of the FreeRTOS application
  145. only used internal of FreeRTOS Port code */
  146. extern uint8_t uxMaxSysCallMTH;
  147. /*-----------------------------------------------------------*/
  148. portFORCE_INLINE static void vPortRaiseBASEPRI(void)
  149. {
  150. unsigned long saved_status = __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
  151. ECLIC_SetMth(uxMaxSysCallMTH);
  152. __RWMB();
  153. __RV_CSR_WRITE(CSR_MSTATUS, saved_status);
  154. }
  155. /*-----------------------------------------------------------*/
  156. portFORCE_INLINE static uint8_t ulPortRaiseBASEPRI(void)
  157. {
  158. uint8_t ulOriginalBASEPRI;
  159. unsigned long saved_status = __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
  160. ulOriginalBASEPRI = ECLIC_GetMth();
  161. ECLIC_SetMth(uxMaxSysCallMTH);
  162. __RWMB();
  163. __RV_CSR_WRITE(CSR_MSTATUS, saved_status);
  164. /* This return might not be reached but is necessary to prevent compiler
  165. warnings. */
  166. return ulOriginalBASEPRI;
  167. }
  168. /*-----------------------------------------------------------*/
  169. portFORCE_INLINE static void vPortSetBASEPRI(uint8_t ulNewMaskValue)
  170. {
  171. unsigned long saved_status = __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
  172. ECLIC_SetMth(ulNewMaskValue);
  173. __RWMB();
  174. __RV_CSR_WRITE(CSR_MSTATUS, saved_status);
  175. }
  176. #endif
  177. /*-----------------------------------------------------------*/
  178. #define portMEMORY_BARRIER() __asm volatile( "" ::: "memory" )
  179. #if ( configNUMBER_OF_CORES == 1 )
  180. extern UBaseType_t uxCriticalNesting;
  181. #define portGET_CRITICAL_NESTING_COUNT() ( uxCriticalNesting )
  182. #define portSET_CRITICAL_NESTING_COUNT( x ) ( uxCriticalNesting = ( x ) )
  183. #define portINCREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNesting++ )
  184. #define portDECREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNesting-- )
  185. /* Critical section management. */
  186. extern void vPortEnterCritical(void);
  187. extern void vPortExitCritical(void);
  188. #define portENTER_CRITICAL() vPortEnterCritical()
  189. #define portEXIT_CRITICAL() vPortExitCritical()
  190. #else
  191. /* Return the core ID on which the code is running. */
  192. #define portGET_CORE_ID() __get_hart_index()
  193. /* Set the interrupt mask. */
  194. #define portSET_INTERRUPT_MASK() portSET_INTERRUPT_MASK_FROM_ISR()
  195. /* Clear the interrupt mask. */
  196. #define portCLEAR_INTERRUPT_MASK( x ) portCLEAR_INTERRUPT_MASK_FROM_ISR(x)
  197. /* Request the core ID x to yield. */
  198. #define portYIELD_CORE( x ) \
  199. { \
  200. /* Set a software interrupt(SWI) to core x to request a context switch. */ \
  201. SysTimer_SetHartSWIRQ(x); \
  202. /* Barriers are normally not required but do ensure the code is completely \
  203. within the specified behaviour for the architecture. */ \
  204. __RWMB(); \
  205. }
  206. #define portRTOS_SPINLOCK_COUNT 2
  207. /* Multi-core */
  208. #define portMAX_CORE_COUNT 16
  209. typedef volatile uint32_t spin_lock_t;
  210. extern spin_lock_t hw_sync_locks[portRTOS_SPINLOCK_COUNT];
  211. extern void vPortRecursiveLock(unsigned long ulLockNum, spin_lock_t *pxSpinLock, BaseType_t uxAcquire);
  212. /* Acquire the TASK lock. TASK lock is a recursive lock.
  213. * It should be able to be locked by the same core multiple times. */
  214. #define portGET_TASK_LOCK() vPortRecursiveLock( 1, &hw_sync_locks[1], pdTRUE )
  215. /* Release the TASK lock. If a TASK lock is locked by the same core multiple times,
  216. * it should be released as many times as it is locked. */
  217. #define portRELEASE_TASK_LOCK() vPortRecursiveLock( 1, &hw_sync_locks[1], pdFALSE )
  218. /* Acquire the ISR lock. ISR lock is a recursive lock.
  219. * It should be able to be locked by the same core multiple times. */
  220. #define portGET_ISR_LOCK() vPortRecursiveLock( 0, &hw_sync_locks[0], pdTRUE )
  221. /* Release the ISR lock. If a ISR lock is locked by the same core multiple times, \
  222. * it should be released as many times as it is locked. */
  223. #define portRELEASE_ISR_LOCK() vPortRecursiveLock( 0, &hw_sync_locks[0], pdFALSE )
  224. extern void vTaskEnterCritical( void );
  225. extern void vTaskExitCritical( void );
  226. extern UBaseType_t vTaskEnterCriticalFromISR( void );
  227. extern void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus );
  228. #define portENTER_CRITICAL() vTaskEnterCritical()
  229. #define portEXIT_CRITICAL() vTaskExitCritical()
  230. #define portENTER_CRITICAL_FROM_ISR() vTaskEnterCriticalFromISR()
  231. #define portEXIT_CRITICAL_FROM_ISR( x ) vTaskExitCriticalFromISR( x )
  232. /* Critical nesting count management. */
  233. extern UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ];
  234. #define portGET_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ] )
  235. #define portSET_CRITICAL_NESTING_COUNT( x ) ( uxCriticalNestings[ portGET_CORE_ID() ] = ( x ) )
  236. #define portINCREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]++ )
  237. #define portDECREMENT_CRITICAL_NESTING_COUNT() ( uxCriticalNestings[ portGET_CORE_ID() ]-- )
  238. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242. #endif /* PORTMACRO_H */