hpm_interrupt.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_INTERRUPT_H
  8. #define HPM_INTERRUPT_H
  9. #include "riscv/riscv_core.h"
  10. #include "hpm_common.h"
  11. #include "hpm_plic_drv.h"
  12. /**
  13. * @brief INTERRUPT driver APIs
  14. * @defgroup irq_interface INTERRUPT driver APIs
  15. * @{
  16. */
  17. #define M_MODE 0 /*!< Machine mode */
  18. #define S_MODE 1 /*!< Supervisor mode */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* Machine mode API: these APIs are supposed to be called at machine mode */
  23. /**
  24. * @brief Enable global IRQ with mask
  25. *
  26. * @param[in] mask interrupt mask to be enabaled
  27. */
  28. ATTR_ALWAYS_INLINE static inline void enable_global_irq(uint32_t mask)
  29. {
  30. set_csr(CSR_MSTATUS, mask);
  31. }
  32. /**
  33. * @brief Disable global IRQ with mask and return mstatus
  34. *
  35. * @param[in] mask interrupt mask to be disabled
  36. * @retval current mstatus value before irq mask is disabled
  37. */
  38. ATTR_ALWAYS_INLINE static inline uint32_t disable_global_irq(uint32_t mask)
  39. {
  40. return read_clear_csr(CSR_MSTATUS, mask);
  41. }
  42. /**
  43. * @brief Restore global IRQ with mask
  44. *
  45. * @param[in] mask interrupt mask to be restored
  46. */
  47. ATTR_ALWAYS_INLINE static inline void restore_global_irq(uint32_t mask)
  48. {
  49. set_csr(CSR_MSTATUS, mask);
  50. }
  51. /**
  52. * @brief Enable IRQ from interrupt controller
  53. *
  54. */
  55. ATTR_ALWAYS_INLINE static inline void enable_irq_from_intc(void)
  56. {
  57. set_csr(CSR_MIE, CSR_MIE_MEIE_MASK);
  58. }
  59. /**
  60. * @brief Disable IRQ from interrupt controller
  61. *
  62. */
  63. ATTR_ALWAYS_INLINE static inline void disable_irq_from_intc(void)
  64. {
  65. clear_csr(CSR_MIE, CSR_MIE_MEIE_MASK);
  66. }
  67. /**
  68. * @brief Enable machine timer IRQ
  69. */
  70. ATTR_ALWAYS_INLINE static inline void enable_mchtmr_irq(void)
  71. {
  72. set_csr(CSR_MIE, CSR_MIE_MTIE_MASK);
  73. }
  74. /**
  75. * @brief Disable machine timer IRQ
  76. *
  77. */
  78. ATTR_ALWAYS_INLINE static inline void disable_mchtmr_irq(void)
  79. {
  80. clear_csr(CSR_MIE, CSR_MIE_MTIE_MASK);
  81. }
  82. /*
  83. * CPU Machine SWI control
  84. *
  85. * Machine SWI (MSIP) is connected to PLICSW irq 1.
  86. */
  87. #define PLICSWI 1
  88. /**
  89. * @brief Initialize software interrupt
  90. *
  91. */
  92. ATTR_ALWAYS_INLINE static inline void intc_m_init_swi(void)
  93. {
  94. __plic_enable_irq(HPM_PLICSW_BASE, HPM_PLIC_TARGET_M_MODE, PLICSWI);
  95. }
  96. /**
  97. * @brief Enable software interrupt
  98. *
  99. */
  100. ATTR_ALWAYS_INLINE static inline void intc_m_enable_swi(void)
  101. {
  102. set_csr(CSR_MIE, CSR_MIE_MSIE_MASK);
  103. }
  104. /**
  105. * @brief Disable software interrupt
  106. *
  107. */
  108. ATTR_ALWAYS_INLINE static inline void intc_m_disable_swi(void)
  109. {
  110. clear_csr(CSR_MIE, CSR_MIE_MSIE_MASK);
  111. }
  112. /**
  113. * @brief Trigger software interrupt
  114. *
  115. */
  116. ATTR_ALWAYS_INLINE static inline void intc_m_trigger_swi(void)
  117. {
  118. __plic_set_irq_pending(HPM_PLICSW_BASE, PLICSWI);
  119. }
  120. /**
  121. * @brief Claim software interrupt
  122. *
  123. */
  124. ATTR_ALWAYS_INLINE static inline void intc_m_claim_swi(void)
  125. {
  126. __plic_claim_irq(HPM_PLICSW_BASE, 0);
  127. }
  128. /**
  129. * @brief Complete software interrupt
  130. *
  131. */
  132. ATTR_ALWAYS_INLINE static inline void intc_m_complete_swi(void)
  133. {
  134. __plic_complete_irq(HPM_PLICSW_BASE, HPM_PLIC_TARGET_M_MODE, PLICSWI);
  135. }
  136. /*
  137. * @brief Enable IRQ for machine mode
  138. *
  139. * @param[in] irq Interrupt number
  140. */
  141. #define intc_m_enable_irq(irq) \
  142. intc_enable_irq(HPM_PLIC_TARGET_M_MODE, irq)
  143. /*
  144. * @brief Disable IRQ for machine mode
  145. *
  146. * @param[in] irq Interrupt number
  147. */
  148. #define intc_m_disable_irq(irq) \
  149. intc_disable_irq(HPM_PLIC_TARGET_M_MODE, irq)
  150. #define intc_m_set_threshold(threshold) \
  151. intc_set_threshold(HPM_PLIC_TARGET_M_MODE, threshold)
  152. /*
  153. * @brief Complete IRQ for machine mode
  154. *
  155. * @param[in] irq Interrupt number
  156. */
  157. #define intc_m_complete_irq(irq) \
  158. intc_complete_irq(HPM_PLIC_TARGET_M_MODE, irq)
  159. /*
  160. * @brief Claim IRQ for machine mode
  161. *
  162. */
  163. #define intc_m_claim_irq() intc_claim_irq(HPM_PLIC_TARGET_M_MODE)
  164. /*
  165. * @brief Enable IRQ for machine mode with priority
  166. *
  167. * @param[in] irq Interrupt number
  168. * @param[in] priority Priority of interrupt
  169. */
  170. #define intc_m_enable_irq_with_priority(irq, priority) \
  171. do { \
  172. intc_set_irq_priority(irq, priority); \
  173. intc_m_enable_irq(irq); \
  174. } while (0)
  175. /*
  176. * @brief Enable specific interrupt
  177. *
  178. * @param[in] target Target to handle specific interrupt
  179. * @param[in] irq Interrupt number
  180. */
  181. ATTR_ALWAYS_INLINE static inline void intc_enable_irq(uint32_t target, uint32_t irq)
  182. {
  183. __plic_enable_irq(HPM_PLIC_BASE, target, irq);
  184. }
  185. /**
  186. * @brief Set interrupt priority
  187. *
  188. * @param[in] irq Interrupt number
  189. * @param[in] priority Priority of interrupt
  190. */
  191. ATTR_ALWAYS_INLINE static inline void intc_set_irq_priority(uint32_t irq, uint32_t priority)
  192. {
  193. __plic_set_irq_priority(HPM_PLIC_BASE, irq, priority);
  194. }
  195. /**
  196. * @brief Disable specific interrupt
  197. *
  198. * @param[in] target Target to handle specific interrupt
  199. * @param[in] irq Interrupt number
  200. */
  201. ATTR_ALWAYS_INLINE static inline void intc_disable_irq(uint32_t target, uint32_t irq)
  202. {
  203. __plic_disable_irq(HPM_PLIC_BASE, target, irq);
  204. }
  205. /**
  206. * @brief Set interrupt threshold
  207. *
  208. * @param[in] target Target to handle specific interrupt
  209. * @param[in] threshold Threshold of IRQ can be serviced
  210. */
  211. ATTR_ALWAYS_INLINE static inline void intc_set_threshold(uint32_t target, uint32_t threshold)
  212. {
  213. __plic_set_threshold(HPM_PLIC_BASE, target, threshold);
  214. }
  215. /**
  216. * @brief Claim IRQ
  217. *
  218. * @param[in] target Target to handle specific interrupt
  219. *
  220. */
  221. ATTR_ALWAYS_INLINE static inline uint32_t intc_claim_irq(uint32_t target)
  222. {
  223. return __plic_claim_irq(HPM_PLIC_BASE, target);
  224. }
  225. /**
  226. * @brief Complete IRQ
  227. *
  228. * @param[in] target Target to handle specific interrupt
  229. * @param[in] irq Specific IRQ to be completed
  230. *
  231. */
  232. ATTR_ALWAYS_INLINE static inline void intc_complete_irq(uint32_t target, uint32_t irq)
  233. {
  234. __plic_complete_irq(HPM_PLIC_BASE, target, irq);
  235. }
  236. /*
  237. * Vectored based irq install and uninstall
  238. */
  239. /* Machine mode */
  240. extern int __vector_table[];
  241. extern void default_irq_entry(void);
  242. /**
  243. * @brief Install ISR for certain IRQ for ram based vector table
  244. *
  245. * @param[in] irq Target interrupt number
  246. * @param[in] isr Interrupt service routine
  247. *
  248. */
  249. ATTR_ALWAYS_INLINE static inline void install_isr(uint32_t irq, uint32_t isr)
  250. {
  251. __vector_table[irq] = isr;
  252. }
  253. /**
  254. * @brief Uninstall ISR for certain IRQ for ram based vector table
  255. *
  256. * @param[in] irq Target interrupt number
  257. *
  258. */
  259. ATTR_ALWAYS_INLINE static inline void uninstall_isr(uint32_t irq)
  260. {
  261. __vector_table[irq] = (int) default_irq_entry;
  262. }
  263. /*
  264. * Inline nested irq entry/exit macros
  265. */
  266. /*
  267. * @brief Save CSR
  268. * @param[in] r Target CSR to be saved
  269. */
  270. #define SAVE_CSR(r) register long __##r = read_csr(r);
  271. /*
  272. * @brief Restore macro
  273. *
  274. * @param[in] r Target CSR to be restored
  275. */
  276. #define RESTORE_CSR(r) write_csr(r, __##r);
  277. #if defined(SUPPORT_PFT_ARCH) && SUPPORT_PFT_ARCH
  278. #define SAVE_MXSTATUS() SAVE_CSR(CSR_MXSTATUS)
  279. #define RESTORE_MXSTATUS() RESTORE_CSR(CSR_MXSTATUS)
  280. #else
  281. #define SAVE_MXSTATUS()
  282. #define RESTORE_MXSTATUS()
  283. #endif
  284. #ifdef __riscv_flen
  285. #define SAVE_FCSR() register int __fcsr = read_fcsr();
  286. #define RESTORE_FCSR() write_fcsr(__fcsr);
  287. #else
  288. #define SAVE_FCSR()
  289. #define RESTORE_FCSR()
  290. #endif
  291. #ifdef __riscv_dsp
  292. #define SAVE_UCODE() SAVE_CSR(CSR_UCODE)
  293. #define RESTORE_UCODE() RESTORE_CSR(CSR_UCODE)
  294. #else
  295. #define SAVE_UCODE()
  296. #define RESTORE_UCODE()
  297. #endif
  298. #ifdef __riscv_flen
  299. #if __riscv_flen == 32
  300. /* RV32I caller registers + MCAUSE + MEPC + MSTATUS + 20 FPU caller registers */
  301. #define CONTEXT_REG_NUM (4 * (16 + 4 + 20))
  302. #else /* __riscv_flen = 64 */
  303. /* RV32I caller registers + MCAUSE + MEPC + MSTATUS + 20 DFPU caller */
  304. #define CONTEXT_REG_NUM (4*(16 + 4 + 20*2))
  305. #endif
  306. #else
  307. /* RV32I caller registers + MCAUSE + MEPC + MSTATUS */
  308. #define CONTEXT_REG_NUM (4 * (16 + 4))
  309. #endif
  310. #ifdef __riscv_flen
  311. /*
  312. * Save FPU caller registers:
  313. * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
  314. */
  315. #if __riscv_flen == 32
  316. #define SAVE_FPU_CONTEXT() { \
  317. __asm volatile("\n\
  318. c.fswsp ft0, 20*4(sp)\n\
  319. c.fswsp ft1, 21*4(sp) \n\
  320. c.fswsp ft2, 22*4(sp) \n\
  321. c.fswsp ft3, 23*4(sp) \n\
  322. c.fswsp ft4, 24*4(sp) \n\
  323. c.fswsp ft5, 25*4(sp) \n\
  324. c.fswsp ft6, 26*4(sp) \n\
  325. c.fswsp ft7, 27*4(sp) \n\
  326. c.fswsp fa0, 28*4(sp) \n\
  327. c.fswsp fa1, 29*4(sp) \n\
  328. c.fswsp fa2, 30*4(sp) \n\
  329. c.fswsp fa3, 31*4(sp) \n\
  330. c.fswsp fa4, 32*4(sp) \n\
  331. c.fswsp fa5, 33*4(sp) \n\
  332. c.fswsp fa6, 34*4(sp) \n\
  333. c.fswsp fa7, 35*4(sp) \n\
  334. c.fswsp ft8, 36*4(sp) \n\
  335. c.fswsp ft9, 37*4(sp) \n\
  336. c.fswsp ft10, 38*4(sp) \n\
  337. c.fswsp ft11, 39*4(sp) \n");\
  338. }
  339. /*
  340. * Restore FPU caller registers:
  341. * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
  342. */
  343. #define RESTORE_FPU_CONTEXT() { \
  344. __asm volatile("\n\
  345. c.flwsp ft0, 20*4(sp)\n\
  346. c.flwsp ft1, 21*4(sp) \n\
  347. c.flwsp ft2, 22*4(sp) \n\
  348. c.flwsp ft3, 23*4(sp) \n\
  349. c.flwsp ft4, 24*4(sp) \n\
  350. c.flwsp ft5, 25*4(sp) \n\
  351. c.flwsp ft6, 26*4(sp) \n\
  352. c.flwsp ft7, 27*4(sp) \n\
  353. c.flwsp fa0, 28*4(sp) \n\
  354. c.flwsp fa1, 29*4(sp) \n\
  355. c.flwsp fa2, 30*4(sp) \n\
  356. c.flwsp fa3, 31*4(sp) \n\
  357. c.flwsp fa4, 32*4(sp) \n\
  358. c.flwsp fa5, 33*4(sp) \n\
  359. c.flwsp fa6, 34*4(sp) \n\
  360. c.flwsp fa7, 35*4(sp) \n\
  361. c.flwsp ft8, 36*4(sp) \n\
  362. c.flwsp ft9, 37*4(sp) \n\
  363. c.flwsp ft10, 38*4(sp) \n\
  364. c.flwsp ft11, 39*4(sp) \n");\
  365. }
  366. #else /*__riscv_flen == 64*/
  367. #define SAVE_FPU_CONTEXT() { \
  368. __asm volatile("\n\
  369. c.fsdsp ft0, 20*4(sp)\n\
  370. c.fsdsp ft1, 22*4(sp) \n\
  371. c.fsdsp ft2, 24*4(sp) \n\
  372. c.fsdsp ft3, 26*4(sp) \n\
  373. c.fsdsp ft4, 28*4(sp) \n\
  374. c.fsdsp ft5, 30*4(sp) \n\
  375. c.fsdsp ft6, 32*4(sp) \n\
  376. c.fsdsp ft7, 34*4(sp) \n\
  377. c.fsdsp fa0, 36*4(sp) \n\
  378. c.fsdsp fa1, 38*4(sp) \n\
  379. c.fsdsp fa2, 40*4(sp) \n\
  380. c.fsdsp fa3, 42*4(sp) \n\
  381. c.fsdsp fa4, 44*4(sp) \n\
  382. c.fsdsp fa5, 46*4(sp) \n\
  383. c.fsdsp fa6, 48*4(sp) \n\
  384. c.fsdsp fa7, 50*4(sp) \n\
  385. c.fsdsp ft8, 52*4(sp) \n\
  386. c.fsdsp ft9, 54*4(sp) \n\
  387. c.fsdsp ft10, 56*4(sp) \n\
  388. c.fsdsp ft11, 58*4(sp) \n");\
  389. }
  390. /*
  391. * Restore FPU caller registers:
  392. * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
  393. */
  394. #define RESTORE_FPU_CONTEXT() { \
  395. __asm volatile("\n\
  396. c.fldsp ft0, 20*4(sp)\n\
  397. c.fldsp ft1, 22*4(sp) \n\
  398. c.fldsp ft2, 24*4(sp) \n\
  399. c.fldsp ft3, 26*4(sp) \n\
  400. c.fldsp ft4, 28*4(sp) \n\
  401. c.fldsp ft5, 30*4(sp) \n\
  402. c.fldsp ft6, 32*4(sp) \n\
  403. c.fldsp ft7, 34*4(sp) \n\
  404. c.fldsp fa0, 36*4(sp) \n\
  405. c.fldsp fa1, 38*4(sp) \n\
  406. c.fldsp fa2, 40*4(sp) \n\
  407. c.fldsp fa3, 42*4(sp) \n\
  408. c.fldsp fa4, 44*4(sp) \n\
  409. c.fldsp fa5, 46*4(sp) \n\
  410. c.fldsp fa6, 48*4(sp) \n\
  411. c.fldsp fa7, 50*4(sp) \n\
  412. c.fldsp ft8, 52*4(sp) \n\
  413. c.fldsp ft9, 54*4(sp) \n\
  414. c.fldsp ft10, 56*4(sp) \n\
  415. c.fldsp ft11, 58*4(sp) \n");\
  416. }
  417. #endif
  418. #else
  419. #define SAVE_FPU_CONTEXT()
  420. #define RESTORE_FPU_CONTEXT()
  421. #endif
  422. /**
  423. * @brief Save the caller registers based on the RISC-V ABI specification
  424. */
  425. #define SAVE_CALLER_CONTEXT() { \
  426. __asm volatile("addi sp, sp, %0" : : "i"(-CONTEXT_REG_NUM) :);\
  427. __asm volatile("\n\
  428. c.swsp ra, 0*4(sp) \n\
  429. c.swsp t0, 1*4(sp) \n\
  430. c.swsp t1, 2*4(sp) \n\
  431. c.swsp t2, 3*4(sp) \n\
  432. c.swsp s0, 4*4(sp) \n\
  433. c.swsp s1, 5*4(sp) \n\
  434. c.swsp a0, 6*4(sp) \n\
  435. c.swsp a1, 7*4(sp) \n\
  436. c.swsp a2, 8*4(sp) \n\
  437. c.swsp a3, 9*4(sp) \n\
  438. c.swsp a4, 10*4(sp) \n\
  439. c.swsp a5, 11*4(sp) \n\
  440. c.swsp a6, 12*4(sp) \n\
  441. c.swsp a7, 13*4(sp) \n\
  442. c.swsp s2, 14*4(sp) \n\
  443. c.swsp s3, 15*4(sp) \n\
  444. c.swsp t3, 16*4(sp) \n\
  445. c.swsp t4, 17*4(sp) \n\
  446. c.swsp t5, 18*4(sp) \n\
  447. c.swsp t6, 19*4(sp)"); \
  448. SAVE_FPU_CONTEXT(); \
  449. }
  450. /**
  451. * @brief Restore the caller registers based on the RISC-V ABI specification
  452. */
  453. #define RESTORE_CALLER_CONTEXT() { \
  454. __asm volatile("\n\
  455. c.lwsp ra, 0*4(sp) \n\
  456. c.lwsp t0, 1*4(sp) \n\
  457. c.lwsp t1, 2*4(sp) \n\
  458. c.lwsp t2, 3*4(sp) \n\
  459. c.lwsp s0, 4*4(sp) \n\
  460. c.lwsp s1, 5*4(sp) \n\
  461. c.lwsp a0, 6*4(sp) \n\
  462. c.lwsp a1, 7*4(sp) \n\
  463. c.lwsp a2, 8*4(sp) \n\
  464. c.lwsp a3, 9*4(sp) \n\
  465. c.lwsp a4, 10*4(sp) \n\
  466. c.lwsp a5, 11*4(sp) \n\
  467. c.lwsp a6, 12*4(sp) \n\
  468. c.lwsp a7, 13*4(sp) \n\
  469. c.lwsp s2, 14*4(sp) \n\
  470. c.lwsp s3, 15*4(sp) \n\
  471. c.lwsp t3, 16*4(sp) \n\
  472. c.lwsp t4, 17*4(sp) \n\
  473. c.lwsp t5, 18*4(sp) \n\
  474. c.lwsp t6, 19*4(sp) \n");\
  475. RESTORE_FPU_CONTEXT(); \
  476. __asm volatile("addi sp, sp, %0" : : "i"(CONTEXT_REG_NUM) :);\
  477. }
  478. #ifdef __riscv_flen
  479. #define SAVE_FPU_STATE() { \
  480. __asm volatile("frsr s1\n"); \
  481. }
  482. #define RESTORE_FPU_STATE() { \
  483. __asm volatile("fssr s1\n"); \
  484. }
  485. #else
  486. #define SAVE_FPU_STATE()
  487. #define RESTORE_FPU_STATE()
  488. #endif
  489. #ifdef __riscv_dsp
  490. /*
  491. * Save DSP context
  492. * NOTE: DSP context registers are stored at word offset 41 in the stack
  493. */
  494. #define SAVE_DSP_CONTEXT() { \
  495. __asm volatile("rdov s0\n"); \
  496. }
  497. /*
  498. * @brief Restore DSP context
  499. * @note DSP context registers are stored at word offset 41 in the stack
  500. */
  501. #define RESTORE_DSP_CONTEXT() {\
  502. __asm volatile("csrw ucode, s0\n"); \
  503. }
  504. #else
  505. #define SAVE_DSP_CONTEXT()
  506. #define RESTORE_DSP_CONTEXT()
  507. #endif
  508. /*
  509. * @brief Enter Nested IRQ Handling
  510. * @note To simplify the logic, Nested IRQ related registers are stored in the stack as below:
  511. * MCAUSE - word offset 16 (not used in the vectored mode)
  512. * EPC - word offset 17
  513. * MSTATUS = word offset 18
  514. * MXSTATUS = word offset 19
  515. */
  516. #define ENTER_NESTED_IRQ_HANDLING_M() { \
  517. __asm volatile("\n\
  518. csrr s2, mepc \n\
  519. csrr s3, mstatus \n");\
  520. SAVE_FPU_STATE(); \
  521. SAVE_DSP_CONTEXT(); \
  522. __asm volatile ("\n\
  523. c.li a5, 8\n\
  524. csrs mstatus, a5\n"); \
  525. }
  526. /*
  527. * @brief Complete IRQ Handling
  528. */
  529. #define COMPLETE_IRQ_HANDLING_M(irq_num) { \
  530. __asm volatile("\n\
  531. lui a5, 0x1\n\
  532. addi a5, a5, -2048\n\
  533. csrc mie, a5\n"); \
  534. __asm volatile("\n\
  535. lui a4, 0xe4200\n");\
  536. __asm volatile("li a3, %0" : : "i" (irq_num) :); \
  537. __asm volatile("sw a3, 4(a4)\n\
  538. fence io, io\n"); \
  539. __asm volatile("csrs mie, a5"); \
  540. }
  541. /*
  542. * @brief Exit Nested IRQ Handling
  543. * @note To simplify the logic, Nested IRQ related registers are stored in the stack as below:
  544. * MCAUSE - word offset 16 (not used in the vectored mode)
  545. * EPC - word offset 17
  546. * MSTATUS = word offset 18
  547. * MXSTATUS = word offset 19
  548. */
  549. #define EXIT_NESTED_IRQ_HANDLING_M() { \
  550. __asm volatile("\n\
  551. csrw mstatus, s3 \n\
  552. csrw mepc, s2 \n");\
  553. RESTORE_FPU_STATE(); \
  554. RESTORE_DSP_CONTEXT(); \
  555. }
  556. /* @brief Nested IRQ entry macro : Save CSRs and enable global irq. */
  557. #define NESTED_IRQ_ENTER() \
  558. SAVE_CSR(CSR_MEPC) \
  559. SAVE_CSR(CSR_MSTATUS) \
  560. SAVE_MXSTATUS() \
  561. SAVE_FCSR() \
  562. SAVE_UCODE() \
  563. set_csr(CSR_MSTATUS, CSR_MSTATUS_MIE_MASK);
  564. /* @brief Nested IRQ exit macro : Restore CSRs */
  565. #define NESTED_IRQ_EXIT() \
  566. RESTORE_CSR(CSR_MSTATUS) \
  567. RESTORE_CSR(CSR_MEPC) \
  568. RESTORE_MXSTATUS() \
  569. RESTORE_FCSR() \
  570. RESTORE_UCODE()
  571. /*
  572. * @brief Nested IRQ exit macro : Restore CSRs
  573. * @param[in] irq Target interrupt number
  574. */
  575. #define NESTED_VPLIC_COMPLETE_INTERRUPT(irq) \
  576. do { \
  577. clear_csr(CSR_MIE, CSR_MIP_MEIP_MASK); \
  578. __plic_complete_irq(HPM_PLIC_BASE, HPM_PLIC_TARGET_M_MODE, irq); \
  579. __asm volatile("fence io, io"); \
  580. set_csr(CSR_MIE, CSR_MIP_MEIP_MASK); \
  581. } while (0)
  582. #ifdef __cplusplus
  583. #define HPM_EXTERN_C extern "C"
  584. #else
  585. #define HPM_EXTERN_C
  586. #endif
  587. #define ISR_NAME_M(irq_num) default_isr_##irq_num
  588. /**
  589. * @brief Declare an external interrupt handler for machine mode
  590. *
  591. * @param[in] irq_num - IRQ number index
  592. * @param[in] isr - Application IRQ handler function pointer
  593. */
  594. #ifndef USE_NONVECTOR_MODE
  595. #define SDK_DECLARE_EXT_ISR_M(irq_num, isr) \
  596. void isr(void) __attribute__((section(".isr_vector")));\
  597. HPM_EXTERN_C void ISR_NAME_M(irq_num)(void) __attribute__((section(".isr_vector")));\
  598. void ISR_NAME_M(irq_num)(void) \
  599. { \
  600. SAVE_CALLER_CONTEXT(); \
  601. ENTER_NESTED_IRQ_HANDLING_M();\
  602. __asm volatile("la t1, %0\n\t" : : "i" (isr) : );\
  603. __asm volatile("jalr t1\n");\
  604. COMPLETE_IRQ_HANDLING_M(irq_num);\
  605. EXIT_NESTED_IRQ_HANDLING_M();\
  606. RESTORE_CALLER_CONTEXT();\
  607. __asm volatile("mret\n");\
  608. }
  609. #else
  610. #define SDK_DECLARE_EXT_ISR_M(irq_num, isr) \
  611. void isr(void) __attribute__((section(".isr_vector")));\
  612. HPM_EXTERN_C void ISR_NAME_M(irq_num)(void) __attribute__((section(".isr_vector")));\
  613. void ISR_NAME_M(irq_num)(void) \
  614. { \
  615. isr(); \
  616. }
  617. #endif
  618. /**
  619. * @brief Declare machine timer interrupt handler
  620. *
  621. * @param[in] isr - MCHTMR IRQ handler function pointer
  622. */
  623. #define SDK_DECLARE_MCHTMR_ISR(isr) \
  624. void isr(void) __attribute__((section(".isr_vector")));\
  625. HPM_EXTERN_C void mchtmr_isr(void) __attribute__((section(".isr_vector"))); \
  626. void mchtmr_isr(void) \
  627. { \
  628. isr();\
  629. }
  630. /**
  631. * @brief Declare machine software interrupt handler
  632. *
  633. * @param[in] isr - SWI IRQ handler function pointer
  634. */
  635. #define SDK_DECLARE_SWI_ISR(isr)\
  636. void isr(void) __attribute__((section(".isr_vector")));\
  637. HPM_EXTERN_C void swi_isr(void) __attribute__((section(".isr_vector"))); \
  638. void swi_isr(void) \
  639. { \
  640. isr();\
  641. }
  642. #ifdef __cplusplus
  643. }
  644. #endif
  645. /**
  646. * @}
  647. */
  648. #endif /* HPM_INTERRUPT_H */