context_gcc.S 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/08/20 zx.chen The T-HEAD RISC-V CPU E906 porting implementation
  9. */
  10. #include "cpuport.h"
  11. #ifdef RT_USING_SMP
  12. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  13. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  14. #endif
  15. /*
  16. * Functions: vPortYield
  17. */
  18. .global vPortYield
  19. .type vPortYield, %function
  20. vPortYield:
  21. li t0, 0xE080100C
  22. lb t1, (t0)
  23. li t2, 0x01
  24. or t1, t1, t2
  25. sb t1, (t0)
  26. ret
  27. /*
  28. * #ifdef RT_USING_SMP
  29. * void rt_hw_context_switch_to(rt_ubase_t to, stuct rt_thread *to_thread);
  30. * #else
  31. * void rt_hw_context_switch_to(rt_ubase_t to);
  32. * #endif
  33. * a0 --> to
  34. * a1 --> to_thread
  35. */
  36. .globl rt_hw_context_switch_to
  37. rt_hw_context_switch_to:
  38. /* save a0 to to_thread */
  39. la t0, rt_interrupt_to_thread
  40. STORE a0, (t0)
  41. /* save 0 to from_thread */
  42. la t0, rt_interrupt_from_thread
  43. li t1, 0
  44. STORE t1, (t0)
  45. /* set rt_thread_switch_interrupt_flag=1 */
  46. la t0, rt_thread_switch_interrupt_flag
  47. li t1, 1
  48. STORE t1, (t0)
  49. /* enable mexstatus SPUSHEN and SPSWAPEN */
  50. #if ((CONFIG_CPU_E906==1) || (CONFIG_CPU_E906F==1) || (CONFIG_CPU_E906FD==1))
  51. uint32_t mexstatus;
  52. mexstatus = __get_MEXSTATUS();
  53. mexstatus |= (0x2 << 16);
  54. __set_MEXSTATUS(mexstatus);
  55. #endif
  56. csrw mscratch, sp
  57. /* set software interrupt */
  58. li t0, 0xE080100C
  59. lb t1, (t0)
  60. li t2, 0x01
  61. or t1, t1, t2
  62. sb t1, (t0)
  63. /* enable global interrup */
  64. csrsi mstatus, 8
  65. ret
  66. /*
  67. * #ifdef RT_USING_SMP
  68. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  69. * #else
  70. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  71. * #endif
  72. *
  73. * a0 --> from
  74. * a1 --> to
  75. * a2 --> to_thread
  76. */
  77. .globl rt_hw_context_switch
  78. rt_hw_context_switch:
  79. .globl rt_hw_context_switch_interrupt
  80. rt_hw_context_switch_interrupt:
  81. /* check rt_thread_switch_interrupt_flag */
  82. la t0, rt_thread_switch_interrupt_flag
  83. lw t1, (t0)
  84. li t2, 1
  85. beq t1, t2, .reswitch
  86. /* set rt_thread_switch_interrupt_flag=1 */
  87. STORE t2, (t0)
  88. /* update from_thread */
  89. la t0, rt_interrupt_from_thread
  90. STORE a0, (t0)
  91. .reswitch:
  92. /* update to_thread */
  93. la t0, rt_interrupt_to_thread
  94. STORE a1, (t0)
  95. /* set software interrupt */
  96. li t0, 0xE080100C
  97. lb t1, (t0)
  98. li t2, 0x01
  99. or t1, t1, t2
  100. sb t1, (t0)
  101. ret
  102. /*
  103. * PendSV_Handler
  104. */
  105. .global PendSV_Handler
  106. .type PendSV_Handler, %function
  107. PendSV_Handler:
  108. /* check rt_thread_switch_interrupt_flag */
  109. sw t0, (-4)(sp)
  110. sw t1, (-8)(sp)
  111. la t0, rt_thread_switch_interrupt_flag
  112. lw t1, (t0)
  113. beqz t1, .pendsv_exit
  114. /* clear rt_thread_switch_interrupt_flag */
  115. li t1, 0x0
  116. sw t1, (t0)
  117. /* check rt_interrupt_from_thread */
  118. la t0, rt_interrupt_from_thread
  119. lw t1, (t0)
  120. beqz t1, .switch_to_thead
  121. /* save from thread context */
  122. lw t0, (-4)(sp)
  123. lw t1, (-8)(sp)
  124. #ifdef ARCH_RISCV_FPU
  125. addi sp, sp, -32 * FREGBYTES
  126. FSTORE f0, 0 * FREGBYTES(sp)
  127. FSTORE f1, 1 * FREGBYTES(sp)
  128. FSTORE f2, 2 * FREGBYTES(sp)
  129. FSTORE f3, 3 * FREGBYTES(sp)
  130. FSTORE f4, 4 * FREGBYTES(sp)
  131. FSTORE f5, 5 * FREGBYTES(sp)
  132. FSTORE f6, 6 * FREGBYTES(sp)
  133. FSTORE f7, 7 * FREGBYTES(sp)
  134. FSTORE f8, 8 * FREGBYTES(sp)
  135. FSTORE f9, 9 * FREGBYTES(sp)
  136. FSTORE f10, 10 * FREGBYTES(sp)
  137. FSTORE f11, 11 * FREGBYTES(sp)
  138. FSTORE f12, 12 * FREGBYTES(sp)
  139. FSTORE f13, 13 * FREGBYTES(sp)
  140. FSTORE f14, 14 * FREGBYTES(sp)
  141. FSTORE f15, 15 * FREGBYTES(sp)
  142. FSTORE f16, 16 * FREGBYTES(sp)
  143. FSTORE f17, 17 * FREGBYTES(sp)
  144. FSTORE f18, 18 * FREGBYTES(sp)
  145. FSTORE f19, 19 * FREGBYTES(sp)
  146. FSTORE f20, 20 * FREGBYTES(sp)
  147. FSTORE f21, 21 * FREGBYTES(sp)
  148. FSTORE f22, 22 * FREGBYTES(sp)
  149. FSTORE f23, 23 * FREGBYTES(sp)
  150. FSTORE f24, 24 * FREGBYTES(sp)
  151. FSTORE f25, 25 * FREGBYTES(sp)
  152. FSTORE f26, 26 * FREGBYTES(sp)
  153. FSTORE f27, 27 * FREGBYTES(sp)
  154. FSTORE f28, 28 * FREGBYTES(sp)
  155. FSTORE f29, 29 * FREGBYTES(sp)
  156. FSTORE f30, 30 * FREGBYTES(sp)
  157. FSTORE f31, 31 * FREGBYTES(sp)
  158. #endif
  159. addi sp, sp, -32 * REGBYTES
  160. STORE x1, 1 * REGBYTES(sp)
  161. csrr x1, mepc
  162. STORE x1, 0 * REGBYTES(sp)
  163. csrr x1, mstatus
  164. andi x1, x1, 8
  165. beqz x1, .save_mpie
  166. li x1, 0x80
  167. .save_mpie:
  168. STORE x1, 2 * REGBYTES(sp)
  169. /* x3 don't need save */
  170. STORE x4, 4 * REGBYTES(sp)
  171. STORE x5, 5 * REGBYTES(sp)
  172. STORE x6, 6 * REGBYTES(sp)
  173. STORE x7, 7 * REGBYTES(sp)
  174. STORE x8, 8 * REGBYTES(sp)
  175. STORE x9, 9 * REGBYTES(sp)
  176. STORE x10, 10 * REGBYTES(sp)
  177. STORE x11, 11 * REGBYTES(sp)
  178. STORE x12, 12 * REGBYTES(sp)
  179. STORE x13, 13 * REGBYTES(sp)
  180. STORE x14, 14 * REGBYTES(sp)
  181. STORE x15, 15 * REGBYTES(sp)
  182. STORE x16, 16 * REGBYTES(sp)
  183. STORE x17, 17 * REGBYTES(sp)
  184. STORE x18, 18 * REGBYTES(sp)
  185. STORE x19, 19 * REGBYTES(sp)
  186. STORE x20, 20 * REGBYTES(sp)
  187. STORE x21, 21 * REGBYTES(sp)
  188. STORE x22, 22 * REGBYTES(sp)
  189. STORE x23, 23 * REGBYTES(sp)
  190. STORE x24, 24 * REGBYTES(sp)
  191. STORE x25, 25 * REGBYTES(sp)
  192. STORE x26, 26 * REGBYTES(sp)
  193. STORE x27, 27 * REGBYTES(sp)
  194. STORE x28, 28 * REGBYTES(sp)
  195. STORE x29, 29 * REGBYTES(sp)
  196. STORE x30, 30 * REGBYTES(sp)
  197. STORE x31, 31 * REGBYTES(sp)
  198. /* store from_thread sp */
  199. la t0, rt_interrupt_from_thread
  200. lw t0, (t0)
  201. sw sp, (t0)
  202. .switch_to_thead:
  203. /* restore to thread context
  204. * sp(0) -> epc;
  205. * sp(1) -> ra;
  206. * sp(i) -> x(i+2)
  207. */
  208. la t0, rt_interrupt_to_thread
  209. lw t0, (t0)
  210. LOAD sp, (t0)
  211. /* restore ra to mepc */
  212. LOAD a1, 0 * REGBYTES(sp)
  213. csrw mepc, a1
  214. LOAD x1, 1 * REGBYTES(sp)
  215. /* force to machin mode(MPP=11) */
  216. li a1, 0x1880
  217. csrs mstatus, a1
  218. LOAD a1, 2 * REGBYTES(sp)
  219. csrs mstatus, a1
  220. /* x3 don't need restore */
  221. LOAD x4, 4 * REGBYTES(sp)
  222. LOAD x5, 5 * REGBYTES(sp)
  223. LOAD x6, 6 * REGBYTES(sp)
  224. LOAD x7, 7 * REGBYTES(sp)
  225. LOAD x8, 8 * REGBYTES(sp)
  226. LOAD x9, 9 * REGBYTES(sp)
  227. LOAD x10, 10 * REGBYTES(sp)
  228. LOAD x11, 11 * REGBYTES(sp)
  229. LOAD x12, 12 * REGBYTES(sp)
  230. LOAD x13, 13 * REGBYTES(sp)
  231. LOAD x14, 14 * REGBYTES(sp)
  232. LOAD x15, 15 * REGBYTES(sp)
  233. LOAD x16, 16 * REGBYTES(sp)
  234. LOAD x17, 17 * REGBYTES(sp)
  235. LOAD x18, 18 * REGBYTES(sp)
  236. LOAD x19, 19 * REGBYTES(sp)
  237. LOAD x20, 20 * REGBYTES(sp)
  238. LOAD x21, 21 * REGBYTES(sp)
  239. LOAD x22, 22 * REGBYTES(sp)
  240. LOAD x23, 23 * REGBYTES(sp)
  241. LOAD x24, 24 * REGBYTES(sp)
  242. LOAD x25, 25 * REGBYTES(sp)
  243. LOAD x26, 26 * REGBYTES(sp)
  244. LOAD x27, 27 * REGBYTES(sp)
  245. LOAD x28, 28 * REGBYTES(sp)
  246. LOAD x29, 29 * REGBYTES(sp)
  247. LOAD x30, 30 * REGBYTES(sp)
  248. LOAD x31, 31 * REGBYTES(sp)
  249. addi sp, sp, 32 * REGBYTES
  250. #ifdef ARCH_RISCV_FPU
  251. FLOAD f0, 0 * FREGBYTES(sp)
  252. FLOAD f1, 1 * FREGBYTES(sp)
  253. FLOAD f2, 2 * FREGBYTES(sp)
  254. FLOAD f3, 3 * FREGBYTES(sp)
  255. FLOAD f4, 4 * FREGBYTES(sp)
  256. FLOAD f5, 5 * FREGBYTES(sp)
  257. FLOAD f6, 6 * FREGBYTES(sp)
  258. FLOAD f7, 7 * FREGBYTES(sp)
  259. FLOAD f8, 8 * FREGBYTES(sp)
  260. FLOAD f9, 9 * FREGBYTES(sp)
  261. FLOAD f10, 10 * FREGBYTES(sp)
  262. FLOAD f11, 11 * FREGBYTES(sp)
  263. FLOAD f12, 12 * FREGBYTES(sp)
  264. FLOAD f13, 13 * FREGBYTES(sp)
  265. FLOAD f14, 14 * FREGBYTES(sp)
  266. FLOAD f15, 15 * FREGBYTES(sp)
  267. FLOAD f16, 16 * FREGBYTES(sp)
  268. FLOAD f17, 17 * FREGBYTES(sp)
  269. FLOAD f18, 18 * FREGBYTES(sp)
  270. FLOAD f19, 19 * FREGBYTES(sp)
  271. FLOAD f20, 20 * FREGBYTES(sp)
  272. FLOAD f21, 21 * FREGBYTES(sp)
  273. FLOAD f22, 22 * FREGBYTES(sp)
  274. FLOAD f23, 23 * FREGBYTES(sp)
  275. FLOAD f24, 24 * FREGBYTES(sp)
  276. FLOAD f25, 25 * FREGBYTES(sp)
  277. FLOAD f26, 26 * FREGBYTES(sp)
  278. FLOAD f27, 27 * FREGBYTES(sp)
  279. FLOAD f28, 28 * FREGBYTES(sp)
  280. FLOAD f29, 29 * FREGBYTES(sp)
  281. FLOAD f30, 30 * FREGBYTES(sp)
  282. FLOAD f31, 31 * FREGBYTES(sp)
  283. addi sp, sp, 32 * FREGBYTES
  284. #endif
  285. .pendsv_exit:
  286. mret