cpuport.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. * Copyright (c) 2019-Present Nuclei Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020/03/26 Huaqi Nuclei RISC-V Core porting code.
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <stdio.h>
  14. #include "cpuport.h"
  15. #ifndef BOOT_HARTID
  16. #define EXECUTE_HARTID 0
  17. #else
  18. #define EXECUTE_HARTID BOOT_HARTID
  19. #endif
  20. #define SYSTICK_TICK_CONST (SOC_TIMER_FREQ / RT_TICK_PER_SECOND)
  21. #ifndef configKERNEL_INTERRUPT_PRIORITY
  22. #define configKERNEL_INTERRUPT_PRIORITY 0
  23. #endif
  24. #ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
  25. // See function prvCheckMaxSysCallPrio and prvCalcMaxSysCallMTH
  26. #define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
  27. #endif
  28. #ifdef SMODE_RTOS
  29. #define SysTick_Handler eclic_stip_handler
  30. extern void eclic_ssip_handler(void);
  31. #define portINITIAL_XSTATUS ( SSTATUS_SPP | SSTATUS_SPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  32. #if defined(__SSTC_PRESENT) && __SSTC_PRESENT == 1
  33. #define SMODE_TIMER_IRQ SysTimer_S_IRQn
  34. #define SMODE_SWI_IRQ SysTimerSW_S_IRQn
  35. #define SMODE_SET_SWI() SysTimer_SetSWIRQ_S()
  36. #define SMODE_CLR_SWI() SysTimer_ClearSWIRQ_S()
  37. #define SMODE_TICK_CONFIG() SysTick_Config_S(SYSTICK_TICK_CONST)
  38. #define SMODE_TICK_RELOAD() SysTick_Reload_S(SYSTICK_TICK_CONST)
  39. #else
  40. #define SMODE_TIMER_IRQ SysTimer_IRQn
  41. #define SMODE_SWI_IRQ SysTimerSW_IRQn
  42. #define SMODE_SET_SWI() SysTimer_SetHartSWIRQ(EXECUTE_HARTID)
  43. #define SMODE_CLR_SWI() SysTimer_ClearHartSWIRQ(EXECUTE_HARTID)
  44. #define SMODE_TICK_CONFIG() SysTick_HartConfig(SYSTICK_TICK_CONST, EXECUTE_HARTID)
  45. #define SMODE_TICK_RELOAD() SysTick_HartReload(SYSTICK_TICK_CONST, EXECUTE_HARTID)
  46. #endif
  47. #else
  48. #define SysTick_Handler eclic_mtip_handler
  49. #define portINITIAL_XSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  50. #endif
  51. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  52. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  53. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  54. void SysTick_Handler(void);
  55. /* Stack frame size 32 REGBYTES(4/8) for most cases, but for ilp32e mode, it's 14 REGBYTES(4) */
  56. struct rt_hw_stack_frame {
  57. rt_ubase_t epc; /* epc - epc - program counter */
  58. rt_ubase_t ra; /* x1 - ra - return address for jumps */
  59. rt_ubase_t t0; /* x5 - t0 - temporary register 0 */
  60. rt_ubase_t t1; /* x6 - t1 - temporary register 1 */
  61. rt_ubase_t t2; /* x7 - t2 - temporary register 2 */
  62. rt_ubase_t s0_fp; /* x8 - s0/fp - saved register 0 or frame pointer */
  63. rt_ubase_t s1; /* x9 - s1 - saved register 1 */
  64. rt_ubase_t a0; /* x10 - a0 - return value or function argument 0 */
  65. rt_ubase_t a1; /* x11 - a1 - return value or function argument 1 */
  66. rt_ubase_t a2; /* x12 - a2 - function argument 2 */
  67. rt_ubase_t a3; /* x13 - a3 - function argument 3 */
  68. rt_ubase_t a4; /* x14 - a4 - function argument 4 */
  69. rt_ubase_t a5; /* x15 - a5 - function argument 5 */
  70. #ifndef __riscv_32e
  71. rt_ubase_t a6; /* x16 - a6 - function argument 6 */
  72. rt_ubase_t a7; /* x17 - s7 - function argument 7 */
  73. rt_ubase_t s2; /* x18 - s2 - saved register 2 */
  74. rt_ubase_t s3; /* x19 - s3 - saved register 3 */
  75. rt_ubase_t s4; /* x20 - s4 - saved register 4 */
  76. rt_ubase_t s5; /* x21 - s5 - saved register 5 */
  77. rt_ubase_t s6; /* x22 - s6 - saved register 6 */
  78. rt_ubase_t s7; /* x23 - s7 - saved register 7 */
  79. rt_ubase_t s8; /* x24 - s8 - saved register 8 */
  80. rt_ubase_t s9; /* x25 - s9 - saved register 9 */
  81. rt_ubase_t s10; /* x26 - s10 - saved register 10 */
  82. rt_ubase_t s11; /* x27 - s11 - saved register 11 */
  83. rt_ubase_t t3; /* x28 - t3 - temporary register 3 */
  84. rt_ubase_t t4; /* x29 - t4 - temporary register 4 */
  85. rt_ubase_t t5; /* x30 - t5 - temporary register 5 */
  86. rt_ubase_t t6; /* x31 - t6 - temporary register 6 */
  87. rt_ubase_t rsv0; /* reserved 0 - reserved to make space */
  88. rt_ubase_t rsv1; /* reserved 1 - reserved to make space */
  89. #endif
  90. rt_ubase_t xstatus; /* - m/s status register */
  91. };
  92. /**
  93. * This function will initialize thread stack
  94. *
  95. * @param tentry the entry of thread
  96. * @param parameter the parameter of entry
  97. * @param stack_addr the beginning stack address
  98. * @param texit the function will be called when thread exit
  99. *
  100. * @return stack address
  101. */
  102. rt_uint8_t* rt_hw_stack_init(void* tentry,
  103. void* parameter,
  104. rt_uint8_t* stack_addr,
  105. void* texit)
  106. {
  107. struct rt_hw_stack_frame* frame;
  108. rt_uint8_t* stk;
  109. int i;
  110. stk = stack_addr + sizeof(rt_ubase_t);
  111. /* https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc */
  112. /* 32-bit boundary for ilp32e, and 128-bit boundary for others */
  113. #ifndef __riscv_32e
  114. stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, 16);
  115. #else
  116. stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, 4);
  117. #endif
  118. stk -= sizeof(struct rt_hw_stack_frame);
  119. frame = (struct rt_hw_stack_frame*)stk;
  120. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++) {
  121. ((rt_ubase_t*)frame)[i] = 0xdeadbeef;
  122. }
  123. frame->ra = (rt_ubase_t)texit;
  124. frame->a0 = (rt_ubase_t)parameter;
  125. frame->epc = (rt_ubase_t)tentry;
  126. frame->xstatus = portINITIAL_XSTATUS;
  127. return stk;
  128. }
  129. /*
  130. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  131. */
  132. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
  133. {
  134. if (rt_thread_switch_interrupt_flag == 0) {
  135. rt_interrupt_from_thread = from;
  136. }
  137. rt_interrupt_to_thread = to;
  138. rt_thread_switch_interrupt_flag = 1;
  139. /* Set a software interrupt(SWI) request to request a context switch. */
  140. #ifdef SMODE_RTOS
  141. SMODE_SET_SWI();
  142. #else
  143. SysTimer_SetSWIRQ();
  144. #endif
  145. /* Barriers are normally not required but do ensure the code is completely
  146. within the specified behaviour for the architecture. */
  147. __RWMB();
  148. }
  149. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
  150. {
  151. rt_hw_context_switch_interrupt(from, to);
  152. }
  153. /** shutdown CPU */
  154. void rt_hw_cpu_shutdown()
  155. {
  156. rt_uint32_t level;
  157. rt_kprintf("shutdown...\n");
  158. level = rt_hw_interrupt_disable();
  159. while (level) {
  160. RT_ASSERT(0);
  161. }
  162. }
  163. void xPortTaskSwitch(void)
  164. {
  165. /* Clear Software IRQ, A MUST */
  166. #ifdef SMODE_RTOS
  167. SMODE_CLR_SWI();
  168. #else
  169. SysTimer_ClearSWIRQ();
  170. #endif
  171. rt_thread_switch_interrupt_flag = 0;
  172. // make from thread to be to thread
  173. // If there is another swi interrupt triggered by other harts
  174. // not through rt_hw_context_switch or rt_hw_context_switch_interrupt
  175. // the task switch should just do a same task save and restore
  176. rt_interrupt_from_thread = rt_interrupt_to_thread;
  177. }
  178. void vPortSetupTimerInterrupt(void)
  179. {
  180. #ifdef SMODE_RTOS
  181. #if defined(__TEE_PRESENT) && __TEE_PRESENT == 1
  182. SMODE_TICK_CONFIG();
  183. ECLIC_DisableIRQ_S(SMODE_TIMER_IRQ);
  184. ECLIC_SetLevelIRQ_S(SMODE_TIMER_IRQ, configKERNEL_INTERRUPT_PRIORITY);
  185. ECLIC_SetShvIRQ_S(SMODE_TIMER_IRQ, ECLIC_NON_VECTOR_INTERRUPT);
  186. ECLIC_SetVector_S(SMODE_TIMER_IRQ, (rv_csr_t)SysTick_Handler);
  187. ECLIC_EnableIRQ_S(SMODE_TIMER_IRQ);
  188. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  189. ECLIC_SetShvIRQ_S(SMODE_SWI_IRQ, ECLIC_VECTOR_INTERRUPT);
  190. ECLIC_SetLevelIRQ_S(SMODE_SWI_IRQ, configKERNEL_INTERRUPT_PRIORITY);
  191. ECLIC_SetVector_S(SMODE_SWI_IRQ, (rv_csr_t)eclic_ssip_handler);
  192. ECLIC_EnableIRQ_S(SMODE_SWI_IRQ);
  193. #else
  194. #error "TEE feature is required for RT-Thread S-Mode support"
  195. #endif
  196. #else
  197. /* Make SWI and SysTick the lowest priority interrupts. */
  198. /* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
  199. SysTick_Config(SYSTICK_TICK_CONST);
  200. ECLIC_DisableIRQ(SysTimer_IRQn);
  201. ECLIC_SetLevelIRQ(SysTimer_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  202. ECLIC_SetShvIRQ(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
  203. ECLIC_EnableIRQ(SysTimer_IRQn);
  204. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  205. ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
  206. ECLIC_SetLevelIRQ(SysTimerSW_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  207. ECLIC_EnableIRQ(SysTimerSW_IRQn);
  208. #endif
  209. }
  210. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  211. #ifndef RT_HEAP_SIZE
  212. #warning RT_HEAP_SIZE is not defined in rtconfig.h, using default 2048
  213. #define RT_HEAP_SIZE 2048
  214. #endif
  215. static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 4K(1024 * 4)
  216. RT_WEAK void* rt_heap_begin_get(void)
  217. {
  218. return rt_heap;
  219. }
  220. RT_WEAK void* rt_heap_end_get(void)
  221. {
  222. return rt_heap + RT_HEAP_SIZE;
  223. }
  224. #endif
  225. // NOTE: define top of stack, it will be used as non-vector interrupt/exception stack when OS started
  226. #ifndef __ICCRISCV__
  227. // _sp is defined in linker script such as gcc_evalsoc_ilm.ld
  228. extern char _sp[];
  229. #define __RTT_INT_STACK (_sp)
  230. #else
  231. // CSTACK$$Limit is defined in iar linker script such iar_evalsoc_ilm.icf
  232. extern char CSTACK$$Limit[];
  233. #define __RTT_INT_STACK (CSTACK$$Limit)
  234. #endif
  235. /**
  236. * This function will initial your board.
  237. */
  238. void rt_hw_board_init()
  239. {
  240. /* OS Tick Configuration */
  241. vPortSetupTimerInterrupt();
  242. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  243. #ifdef RT_USING_COMPONENTS_INIT
  244. rt_components_board_init();
  245. #endif
  246. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  247. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  248. #endif
  249. rt_hw_interrupt_disable();
  250. // Enable interrupt and task sp swap
  251. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  252. // NOTE: setup interrupt stack pointer for CSR_MTSP or CSR_STSP depends on which mode RTT run on
  253. __RV_CSR_WRITE(CSR_XTSP, (unsigned long)__RTT_INT_STACK);
  254. // NOTE: enable trap sp auto swap
  255. __RV_CSR_SET(CSR_XECLIC_CTL, XECLIC_CTL_TSP_EN);
  256. #endif
  257. }
  258. /* This is the timer interrupt service routine. */
  259. void SysTick_Handler(void)
  260. {
  261. // Reload timer
  262. #ifdef SMODE_RTOS
  263. SMODE_TICK_RELOAD();
  264. #else
  265. SysTick_Reload(SYSTICK_TICK_CONST);
  266. #endif
  267. /* enter interrupt */
  268. rt_interrupt_enter();
  269. /* tick increase */
  270. rt_tick_increase();
  271. /* leave interrupt */
  272. rt_interrupt_leave();
  273. }
  274. void rt_hw_console_output(const char* str)
  275. {
  276. rt_size_t size = 0;
  277. size = rt_strlen(str);
  278. for (int i = 0; i < size; i ++) {
  279. putchar(str[i]);
  280. }
  281. }
  282. char rt_hw_console_getchar(void)
  283. {
  284. char ch = -1;
  285. ch = (char)getchar();
  286. return ch;
  287. }
  288. rt_base_t rt_hw_interrupt_disable(void)
  289. {
  290. return __RV_CSR_READ_CLEAR(CSR_XSTATUS, XSTATUS_XIE);
  291. }
  292. void rt_hw_interrupt_enable(rt_base_t level)
  293. {
  294. __RV_CSR_WRITE(CSR_XSTATUS, level);
  295. }