cpuport.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2006-2021, 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 <sys/types.h>
  14. #include <unistd.h>
  15. #include "cpuport.h"
  16. #define SYSTICK_TICK_CONST (SOC_TIMER_FREQ / RT_TICK_PER_SECOND)
  17. /* Interrupt level for kernel systimer interrupt and software timer interrupt */
  18. #define RT_KERNEL_INTERRUPT_LEVEL 0
  19. /* Initial CSR MSTATUS value when thread created */
  20. #define RT_INITIAL_MSTATUS (MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL)
  21. /**
  22. * @brief from thread used interrupt context switch
  23. *
  24. */
  25. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  26. /**
  27. * @brief to thread used interrupt context switch
  28. *
  29. */
  30. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  31. /**
  32. * @brief flag to indicate context switch in interrupt or not
  33. *
  34. */
  35. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  36. /**
  37. * @brief thread stack frame of saved context
  38. *
  39. */
  40. struct rt_hw_stack_frame
  41. {
  42. rt_ubase_t epc; /*!< epc - epc - program counter */
  43. rt_ubase_t ra; /*!< x1 - ra - return address for jumps */
  44. rt_ubase_t t0; /*!< x5 - t0 - temporary register 0 */
  45. rt_ubase_t t1; /*!< x6 - t1 - temporary register 1 */
  46. rt_ubase_t t2; /*!< x7 - t2 - temporary register 2 */
  47. rt_ubase_t s0_fp; /*!< x8 - s0/fp - saved register 0 or frame pointer */
  48. rt_ubase_t s1; /*!< x9 - s1 - saved register 1 */
  49. rt_ubase_t a0; /*!< x10 - a0 - return value or function argument 0 */
  50. rt_ubase_t a1; /*!< x11 - a1 - return value or function argument 1 */
  51. rt_ubase_t a2; /*!< x12 - a2 - function argument 2 */
  52. rt_ubase_t a3; /*!< x13 - a3 - function argument 3 */
  53. rt_ubase_t a4; /*!< x14 - a4 - function argument 4 */
  54. rt_ubase_t a5; /*!< x15 - a5 - function argument 5 */
  55. #ifndef __riscv_32e
  56. rt_ubase_t a6; /*!< x16 - a6 - function argument 6 */
  57. rt_ubase_t a7; /*!< x17 - s7 - function argument 7 */
  58. rt_ubase_t s2; /*!< x18 - s2 - saved register 2 */
  59. rt_ubase_t s3; /*!< x19 - s3 - saved register 3 */
  60. rt_ubase_t s4; /*!< x20 - s4 - saved register 4 */
  61. rt_ubase_t s5; /*!< x21 - s5 - saved register 5 */
  62. rt_ubase_t s6; /*!< x22 - s6 - saved register 6 */
  63. rt_ubase_t s7; /*!< x23 - s7 - saved register 7 */
  64. rt_ubase_t s8; /*!< x24 - s8 - saved register 8 */
  65. rt_ubase_t s9; /*!< x25 - s9 - saved register 9 */
  66. rt_ubase_t s10; /*!< x26 - s10 - saved register 10 */
  67. rt_ubase_t s11; /*!< x27 - s11 - saved register 11 */
  68. rt_ubase_t t3; /*!< x28 - t3 - temporary register 3 */
  69. rt_ubase_t t4; /*!< x29 - t4 - temporary register 4 */
  70. rt_ubase_t t5; /*!< x30 - t5 - temporary register 5 */
  71. rt_ubase_t t6; /*!< x31 - t6 - temporary register 6 */
  72. #endif
  73. rt_ubase_t mstatus; /*!< - machine status register */
  74. };
  75. /**
  76. * This function will initialize thread stack
  77. *
  78. * @param tentry the entry of thread
  79. * @param parameter the parameter of entry
  80. * @param stack_addr the beginning stack address
  81. * @param texit the function will be called when thread exit
  82. *
  83. * @return stack address
  84. */
  85. rt_uint8_t *rt_hw_stack_init(void *tentry,
  86. void *parameter,
  87. rt_uint8_t *stack_addr,
  88. void *texit)
  89. {
  90. struct rt_hw_stack_frame *frame;
  91. rt_uint8_t *stk;
  92. int i;
  93. stk = stack_addr + sizeof(rt_ubase_t);
  94. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  95. stk -= sizeof(struct rt_hw_stack_frame);
  96. frame = (struct rt_hw_stack_frame *)stk;
  97. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
  98. {
  99. ((rt_ubase_t *)frame)[i] = 0xdeadbeef;
  100. }
  101. frame->ra = (rt_ubase_t)texit;
  102. frame->a0 = (rt_ubase_t)parameter;
  103. frame->epc = (rt_ubase_t)tentry;
  104. frame->mstatus = RT_INITIAL_MSTATUS;
  105. return stk;
  106. }
  107. /**
  108. * @brief Do rt-thread context switch in interrupt context
  109. *
  110. * @param from thread sp of from thread
  111. * @param to thread sp of to thread
  112. */
  113. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
  114. {
  115. if (rt_thread_switch_interrupt_flag == 0)
  116. rt_interrupt_from_thread = from;
  117. rt_interrupt_to_thread = to;
  118. rt_thread_switch_interrupt_flag = 1;
  119. RT_YIELD();
  120. }
  121. /**
  122. * @brief Do rt-thread context switch in task context
  123. *
  124. * @param from thread sp of from thread
  125. * @param to thread sp of to thread
  126. */
  127. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
  128. {
  129. rt_hw_context_switch_interrupt(from, to);
  130. }
  131. /**
  132. * @brief shutdown CPU
  133. *
  134. */
  135. RT_WEAK void rt_hw_cpu_shutdown()
  136. {
  137. rt_uint32_t level;
  138. rt_kprintf("shutdown...\n");
  139. level = rt_hw_interrupt_disable();
  140. while (level)
  141. {
  142. RT_ASSERT(0);
  143. }
  144. }
  145. /**
  146. * @brief Do extra task switch code
  147. *
  148. * @details
  149. *
  150. * - Clear software timer interrupt request flag
  151. * - clear rt_thread_switch_interrupt_flag to 0
  152. */
  153. void rt_hw_taskswitch(void)
  154. {
  155. /* Clear Software IRQ, A MUST */
  156. SysTimer_ClearSWIRQ();
  157. rt_thread_switch_interrupt_flag = 0;
  158. }
  159. /**
  160. * @brief Setup systimer and software timer interrupt
  161. *
  162. * @details
  163. *
  164. * - Set Systimer interrupt as NON-VECTOR interrupt with lowest interrupt level
  165. * - Set software timer interrupt as VECTOR interrupt with lowest interrupt level
  166. * - Enable these two interrupts
  167. */
  168. void rt_hw_ticksetup(void)
  169. {
  170. uint64_t ticks = SYSTICK_TICK_CONST;
  171. /* Make SWI and SysTick the lowest priority interrupts. */
  172. /* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
  173. SysTick_Config(ticks);
  174. ECLIC_DisableIRQ(SysTimer_IRQn);
  175. ECLIC_SetLevelIRQ(SysTimer_IRQn, RT_KERNEL_INTERRUPT_LEVEL);
  176. ECLIC_SetShvIRQ(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
  177. ECLIC_EnableIRQ(SysTimer_IRQn);
  178. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  179. ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
  180. ECLIC_SetLevelIRQ(SysTimerSW_IRQn, RT_KERNEL_INTERRUPT_LEVEL);
  181. ECLIC_EnableIRQ(SysTimerSW_IRQn);
  182. }
  183. /**
  184. * systimer interrupt handler eclic_mtip_handler
  185. * is hard coded in startup_<Device>.S
  186. * We define SysTick_Handler as eclic_mtip_handler
  187. * for easy understanding
  188. */
  189. #define SysTick_Handler eclic_mtip_handler
  190. /**
  191. * @brief This is the timer interrupt service routine.
  192. *
  193. */
  194. void SysTick_Handler(void)
  195. {
  196. /* Reload systimer */
  197. SysTick_Reload(SYSTICK_TICK_CONST);
  198. /* enter interrupt */
  199. rt_interrupt_enter();
  200. /* tick increase */
  201. rt_tick_increase();
  202. /* leave interrupt */
  203. rt_interrupt_leave();
  204. }
  205. /**
  206. * @brief Disable cpu interrupt
  207. *
  208. * @details
  209. *
  210. * - Disable cpu interrupt by clear MIE bit in MSTATUS
  211. * - Return the previous value in MSTATUS before clear MIE bit
  212. *
  213. * @return the previous value in MSTATUS before clear MIE bit
  214. */
  215. rt_base_t rt_hw_interrupt_disable(void)
  216. {
  217. return __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
  218. }
  219. /**
  220. * @brief Restore previous saved interrupt status
  221. *
  222. * @param level previous saved MSTATUS value
  223. */
  224. void rt_hw_interrupt_enable(rt_base_t level)
  225. {
  226. __RV_CSR_WRITE(CSR_MSTATUS, level);
  227. }