context_gcc.S 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. * Copyright (c) 2021, Alibaba Group Holding Limited
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020/08/20 zx.chen The T-HEAD RISC-V CPU E906 porting implementation
  10. * 2021/08/13 zx.chen update T-HEAD E9xx-series(E906/7/F/D/P) CPU porting code.
  11. */
  12. #include "cpuport.h"
  13. #ifdef RT_USING_SMP
  14. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  15. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  16. #endif
  17. /*
  18. * Functions: vPortYield
  19. */
  20. .global vPortYield
  21. .type vPortYield, %function
  22. vPortYield:
  23. li t0, 0xE080100C
  24. lb t1, (t0)
  25. li t2, 0x01
  26. or t1, t1, t2
  27. sb t1, (t0)
  28. ret
  29. /*
  30. * #ifdef RT_USING_SMP
  31. * void rt_hw_context_switch_to(rt_ubase_t to, stuct rt_thread *to_thread);
  32. * #else
  33. * void rt_hw_context_switch_to(rt_ubase_t to);
  34. * #endif
  35. * a0 --> to
  36. * a1 --> to_thread
  37. */
  38. .globl rt_hw_context_switch_to
  39. rt_hw_context_switch_to:
  40. /* save a0 to to_thread */
  41. la t0, rt_interrupt_to_thread
  42. STORE a0, (t0)
  43. /* save 0 to from_thread */
  44. la t0, rt_interrupt_from_thread
  45. li t1, 0
  46. STORE t1, (t0)
  47. /* set rt_thread_switch_interrupt_flag=1 */
  48. la t0, rt_thread_switch_interrupt_flag
  49. li t1, 1
  50. STORE t1, (t0)
  51. /* enable mexstatus SPUSHEN */
  52. #ifdef CONFIG_THEAD_EXT_SPUSHEN
  53. li t0, 0x10000
  54. csrs mexstatus, t0
  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. /* restore from thread context t0,t1 */
  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. #ifdef ARCH_RISCV_DSP
  160. addi sp, sp, -33 * REGBYTES
  161. #else
  162. addi sp, sp, -32 * REGBYTES
  163. #endif
  164. STORE x1, 1 * REGBYTES(sp)
  165. csrr x1, mepc
  166. STORE x1, 0 * REGBYTES(sp)
  167. csrr x1, mstatus
  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. #ifdef ARCH_RISCV_DSP
  199. csrr t0, vxsat
  200. STORE t0, 32 * REGBYTES(sp)
  201. #endif
  202. /* store from_thread sp */
  203. la t0, rt_interrupt_from_thread
  204. lw t0, (t0)
  205. sw sp, (t0)
  206. .switch_to_thead:
  207. /* restore to thread context
  208. * sp(0) -> epc;
  209. * sp(1) -> ra;
  210. * sp(i) -> x(i+2)
  211. */
  212. la t0, rt_interrupt_to_thread
  213. lw t0, (t0)
  214. LOAD sp, (t0)
  215. #ifdef ARCH_RISCV_DSP
  216. LOAD a1, 32 * REGBYTES(sp)
  217. csrw vxsat, a1
  218. #endif
  219. /* restore ra to mepc */
  220. LOAD a1, 0 * REGBYTES(sp)
  221. csrw mepc, a1
  222. LOAD x1, 1 * REGBYTES(sp)
  223. /* force to machine mode(MPP=11) */
  224. LOAD a1, 2 * REGBYTES(sp)
  225. csrw mstatus, a1
  226. /* x3 don't need restore */
  227. LOAD x4, 4 * REGBYTES(sp)
  228. LOAD x5, 5 * REGBYTES(sp)
  229. LOAD x6, 6 * REGBYTES(sp)
  230. LOAD x7, 7 * REGBYTES(sp)
  231. LOAD x8, 8 * REGBYTES(sp)
  232. LOAD x9, 9 * REGBYTES(sp)
  233. LOAD x10, 10 * REGBYTES(sp)
  234. LOAD x11, 11 * REGBYTES(sp)
  235. LOAD x12, 12 * REGBYTES(sp)
  236. LOAD x13, 13 * REGBYTES(sp)
  237. LOAD x14, 14 * REGBYTES(sp)
  238. LOAD x15, 15 * REGBYTES(sp)
  239. LOAD x16, 16 * REGBYTES(sp)
  240. LOAD x17, 17 * REGBYTES(sp)
  241. LOAD x18, 18 * REGBYTES(sp)
  242. LOAD x19, 19 * REGBYTES(sp)
  243. LOAD x20, 20 * REGBYTES(sp)
  244. LOAD x21, 21 * REGBYTES(sp)
  245. LOAD x22, 22 * REGBYTES(sp)
  246. LOAD x23, 23 * REGBYTES(sp)
  247. LOAD x24, 24 * REGBYTES(sp)
  248. LOAD x25, 25 * REGBYTES(sp)
  249. LOAD x26, 26 * REGBYTES(sp)
  250. LOAD x27, 27 * REGBYTES(sp)
  251. LOAD x28, 28 * REGBYTES(sp)
  252. LOAD x29, 29 * REGBYTES(sp)
  253. LOAD x30, 30 * REGBYTES(sp)
  254. LOAD x31, 31 * REGBYTES(sp)
  255. #ifdef ARCH_RISCV_DSP
  256. addi sp, sp, 33 * REGBYTES
  257. #else
  258. addi sp, sp, 32 * REGBYTES
  259. #endif
  260. #ifdef ARCH_RISCV_FPU
  261. FLOAD f0, 0 * FREGBYTES(sp)
  262. FLOAD f1, 1 * FREGBYTES(sp)
  263. FLOAD f2, 2 * FREGBYTES(sp)
  264. FLOAD f3, 3 * FREGBYTES(sp)
  265. FLOAD f4, 4 * FREGBYTES(sp)
  266. FLOAD f5, 5 * FREGBYTES(sp)
  267. FLOAD f6, 6 * FREGBYTES(sp)
  268. FLOAD f7, 7 * FREGBYTES(sp)
  269. FLOAD f8, 8 * FREGBYTES(sp)
  270. FLOAD f9, 9 * FREGBYTES(sp)
  271. FLOAD f10, 10 * FREGBYTES(sp)
  272. FLOAD f11, 11 * FREGBYTES(sp)
  273. FLOAD f12, 12 * FREGBYTES(sp)
  274. FLOAD f13, 13 * FREGBYTES(sp)
  275. FLOAD f14, 14 * FREGBYTES(sp)
  276. FLOAD f15, 15 * FREGBYTES(sp)
  277. FLOAD f16, 16 * FREGBYTES(sp)
  278. FLOAD f17, 17 * FREGBYTES(sp)
  279. FLOAD f18, 18 * FREGBYTES(sp)
  280. FLOAD f19, 19 * FREGBYTES(sp)
  281. FLOAD f20, 20 * FREGBYTES(sp)
  282. FLOAD f21, 21 * FREGBYTES(sp)
  283. FLOAD f22, 22 * FREGBYTES(sp)
  284. FLOAD f23, 23 * FREGBYTES(sp)
  285. FLOAD f24, 24 * FREGBYTES(sp)
  286. FLOAD f25, 25 * FREGBYTES(sp)
  287. FLOAD f26, 26 * FREGBYTES(sp)
  288. FLOAD f27, 27 * FREGBYTES(sp)
  289. FLOAD f28, 28 * FREGBYTES(sp)
  290. FLOAD f29, 29 * FREGBYTES(sp)
  291. FLOAD f30, 30 * FREGBYTES(sp)
  292. FLOAD f31, 31 * FREGBYTES(sp)
  293. addi sp, sp, 32 * FREGBYTES
  294. #endif
  295. .pendsv_exit:
  296. mret
  297. /*
  298. * rt_base_t rt_hw_interrupt_disable(void);
  299. */
  300. .globl rt_hw_interrupt_disable
  301. .type rt_hw_interrupt_disable, %function
  302. rt_hw_interrupt_disable:
  303. csrrci a0, mstatus, 8
  304. ret
  305. /*
  306. * void rt_hw_interrupt_enable(rt_base_t level);
  307. */
  308. .globl rt_hw_interrupt_enable
  309. .type rt_hw_interrupt_enable, %function
  310. rt_hw_interrupt_enable:
  311. csrw mstatus, a0
  312. ret