interrupt_iar.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2019-Present Nuclei Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023/09/25 Huaqi First Nuclei RISC-V porting implementation For IAR CC
  9. */
  10. #include "riscv_encoding.h"
  11. DISABLE_MIE MACRO
  12. csrci CSR_MSTATUS, MSTATUS_MIE
  13. ENDM
  14. SAVE_CONTEXT MACRO
  15. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  16. #else
  17. csrrw sp, CSR_MSCRATCHCSWL, sp
  18. /* Allocate stack space for context saving */
  19. #ifndef __riscv_32e
  20. addi sp, sp, -20*REGBYTES
  21. #else
  22. addi sp, sp, -14*REGBYTES
  23. #endif /* __riscv_32e */
  24. STORE x1, 0*REGBYTES(sp)
  25. STORE x4, 1*REGBYTES(sp)
  26. STORE x5, 2*REGBYTES(sp)
  27. STORE x6, 3*REGBYTES(sp)
  28. STORE x7, 4*REGBYTES(sp)
  29. STORE x10, 5*REGBYTES(sp)
  30. STORE x11, 6*REGBYTES(sp)
  31. STORE x12, 7*REGBYTES(sp)
  32. STORE x13, 8*REGBYTES(sp)
  33. STORE x14, 9*REGBYTES(sp)
  34. STORE x15, 10*REGBYTES(sp)
  35. #ifndef __riscv_32e
  36. STORE x16, 14*REGBYTES(sp)
  37. STORE x17, 15*REGBYTES(sp)
  38. STORE x28, 16*REGBYTES(sp)
  39. STORE x29, 17*REGBYTES(sp)
  40. STORE x30, 18*REGBYTES(sp)
  41. STORE x31, 19*REGBYTES(sp)
  42. #endif /* __riscv_32e */
  43. #endif
  44. ENDM
  45. RESTORE_CONTEXT MACRO
  46. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  47. #else
  48. LOAD x1, 0*REGBYTES(sp)
  49. LOAD x4, 1*REGBYTES(sp)
  50. LOAD x5, 2*REGBYTES(sp)
  51. LOAD x6, 3*REGBYTES(sp)
  52. LOAD x7, 4*REGBYTES(sp)
  53. LOAD x10, 5*REGBYTES(sp)
  54. LOAD x11, 6*REGBYTES(sp)
  55. LOAD x12, 7*REGBYTES(sp)
  56. LOAD x13, 8*REGBYTES(sp)
  57. LOAD x14, 9*REGBYTES(sp)
  58. LOAD x15, 10*REGBYTES(sp)
  59. #ifndef __riscv_32e
  60. LOAD x16, 14*REGBYTES(sp)
  61. LOAD x17, 15*REGBYTES(sp)
  62. LOAD x28, 16*REGBYTES(sp)
  63. LOAD x29, 17*REGBYTES(sp)
  64. LOAD x30, 18*REGBYTES(sp)
  65. LOAD x31, 19*REGBYTES(sp)
  66. /* De-allocate the stack space */
  67. addi sp, sp, 20*REGBYTES
  68. #else
  69. /* De-allocate the stack space */
  70. addi sp, sp, 14*REGBYTES
  71. #endif /* __riscv_32e */
  72. csrrw sp, CSR_MSCRATCHCSWL, sp
  73. #endif
  74. ENDM
  75. SAVE_CSR_CONTEXT MACRO
  76. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  77. #else
  78. /* Store CSR mcause to stack using pushmcause */
  79. csrrwi x0, CSR_PUSHMCAUSE, 11
  80. /* Store CSR mepc to stack using pushmepc */
  81. csrrwi x0, CSR_PUSHMEPC, 12
  82. /* Store CSR msub to stack using pushmsub */
  83. csrrwi x0, CSR_PUSHMSUBM, 13
  84. #endif
  85. ENDM
  86. RESTORE_CSR_CONTEXT MACRO
  87. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  88. #else
  89. LOAD x5, 13*REGBYTES(sp)
  90. csrw CSR_MSUBM, x5
  91. LOAD x5, 12*REGBYTES(sp)
  92. csrw CSR_MEPC, x5
  93. LOAD x5, 11*REGBYTES(sp)
  94. csrw CSR_MCAUSE, x5
  95. #endif
  96. ENDM
  97. PUBLIC exc_entry, irq_entry, default_intexc_handler
  98. PUBLIC Undef_Handler
  99. EXTERN core_exception_handler
  100. EXTERN CSTACK$$Limit
  101. SECTION `.text`:CODE:NOROOT(2)
  102. CODE
  103. ALIGN 6
  104. exc_entry:
  105. /* Save the caller saving registers (context) */
  106. SAVE_CONTEXT
  107. /* Save the necessary CSR registers */
  108. SAVE_CSR_CONTEXT
  109. /*
  110. * Set the exception handler function arguments
  111. * argument 1: mcause value
  112. * argument 2: current stack point(SP) value
  113. */
  114. csrr a0, mcause
  115. mv a1, sp
  116. /*
  117. * TODO: Call the exception handler function
  118. * By default, the function template is provided in
  119. * system_Device.c, you can adjust it as you want
  120. */
  121. call core_exception_handler
  122. /* Restore the necessary CSR registers */
  123. RESTORE_CSR_CONTEXT
  124. /* Restore the caller saving registers (context) */
  125. RESTORE_CONTEXT
  126. /* Return to regular code */
  127. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  128. csrrwi x0, CSR_POPXRET, 0
  129. #else
  130. mret
  131. #endif
  132. ALIGN 2
  133. irq_entry:
  134. /* Save the caller saving registers (context) */
  135. SAVE_CONTEXT
  136. /* Save the necessary CSR registers */
  137. SAVE_CSR_CONTEXT
  138. /* This special CSR read/write operation, which is actually
  139. * claim the CLIC to find its pending highest ID, if the ID
  140. * is not 0, then automatically enable the mstatus.MIE, and
  141. * jump to its vector-entry-label, and update the link register
  142. */
  143. csrrw ra, CSR_JALMNXTI, ra
  144. /* Critical section with interrupts disabled */
  145. DISABLE_MIE
  146. /* Restore the necessary CSR registers */
  147. RESTORE_CSR_CONTEXT
  148. /* Restore the caller saving registers (context) */
  149. RESTORE_CONTEXT
  150. /* Return to regular code */
  151. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  152. csrrwi x0, CSR_POPXRET, 0
  153. #else
  154. mret
  155. #endif
  156. default_intexc_handler:
  157. Undef_Handler:
  158. j Undef_Handler
  159. END