HPM SDK
HPMicro Software Development Kit
hpm_interrupt.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_INTERRUPT_H
9 #define HPM_INTERRUPT_H
10 #include "hpm_common.h"
11 #include "hpm_csr_drv.h"
12 #include "hpm_plic_drv.h"
13 
20 #define M_MODE 0
21 #define S_MODE 1
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Machine mode API: these APIs are supposed to be called at machine mode */
28 
34 ATTR_ALWAYS_INLINE static inline void enable_global_irq(uint32_t mask)
35 {
36  set_csr(CSR_MSTATUS, mask);
37 }
38 
45 ATTR_ALWAYS_INLINE static inline uint32_t disable_global_irq(uint32_t mask)
46 {
47  return read_clear_csr(CSR_MSTATUS, mask);
48 }
49 
55 ATTR_ALWAYS_INLINE static inline void restore_global_irq(uint32_t mask)
56 {
57  set_csr(CSR_MSTATUS, mask);
58 }
59 
64 ATTR_ALWAYS_INLINE static inline void enable_irq_from_intc(void)
65 {
67 }
68 
73 ATTR_ALWAYS_INLINE static inline void disable_irq_from_intc(void)
74 {
76 }
77 
81 ATTR_ALWAYS_INLINE static inline void enable_mchtmr_irq(void)
82 {
84 }
85 
90 ATTR_ALWAYS_INLINE static inline void disable_mchtmr_irq(void)
91 {
93 }
94 
100 ATTR_ALWAYS_INLINE static inline void delegate_irq(uint32_t mask)
101 {
102  set_csr(CSR_MIDELEG, mask);
103 }
104 
110 ATTR_ALWAYS_INLINE static inline void undelegate_irq(uint32_t mask)
111 {
112  clear_csr(CSR_MIDELEG, mask);
113 }
114 
115 
116 /* Supervisor mode API: these APIs are supposed to be called at supervisor mode */
117 
123 ATTR_ALWAYS_INLINE static inline void enable_s_global_irq(uint32_t mask)
124 {
125  set_csr(CSR_SSTATUS, mask);
126 }
127 
134 ATTR_ALWAYS_INLINE static inline uint32_t disable_s_global_irq(uint32_t mask)
135 {
136  return read_clear_csr(CSR_SSTATUS, mask);
137 }
138 
144 ATTR_ALWAYS_INLINE static inline void restore_s_global_irq(uint32_t mask)
145 {
146  set_csr(CSR_SSTATUS, mask);
147 }
148 
153 ATTR_ALWAYS_INLINE static inline void disable_s_irq_from_intc(void)
154 {
156 }
157 
162 ATTR_ALWAYS_INLINE static inline void enable_s_irq_from_intc(void)
163 {
165 }
166 
170 ATTR_ALWAYS_INLINE static inline void enable_s_mchtmr_irq(void)
171 {
173 }
174 
179 ATTR_ALWAYS_INLINE static inline void disable_s_mchtmr_irq(void)
180 {
182 }
183 
184 
185 /*
186  * CPU Machine SWI control
187  *
188  * Machine SWI (MSIP) is connected to PLICSW irq 1.
189  */
190 #define PLICSWI 1
191 
196 ATTR_ALWAYS_INLINE static inline void intc_m_init_swi(void)
197 {
198  __plic_enable_irq(HPM_PLICSW_BASE, HPM_PLIC_TARGET_M_MODE, PLICSWI);
199 }
200 
201 
206 ATTR_ALWAYS_INLINE static inline void intc_m_enable_swi(void)
207 {
209 }
210 
211 
216 ATTR_ALWAYS_INLINE static inline void intc_m_disable_swi(void)
217 {
219 }
220 
221 
226 ATTR_ALWAYS_INLINE static inline void intc_m_trigger_swi(void)
227 {
228  __plic_set_irq_pending(HPM_PLICSW_BASE, PLICSWI);
229 }
230 
235 ATTR_ALWAYS_INLINE static inline void intc_m_claim_swi(void)
236 {
237  __plic_claim_irq(HPM_PLICSW_BASE, 0);
238 }
239 
244 ATTR_ALWAYS_INLINE static inline void intc_m_complete_swi(void)
245 {
246  __plic_complete_irq(HPM_PLICSW_BASE, HPM_PLIC_TARGET_M_MODE, PLICSWI);
247 }
248 
249 /*
250  * @brief Enable IRQ for machine mode
251  *
252  * @param[in] irq Interrupt number
253  */
254 #define intc_m_enable_irq(irq) \
255  intc_enable_irq(HPM_PLIC_TARGET_M_MODE, irq)
256 
257 /*
258  * @brief Disable IRQ for machine mode
259  *
260  * @param[in] irq Interrupt number
261  */
262 #define intc_m_disable_irq(irq) \
263  intc_disable_irq(HPM_PLIC_TARGET_M_MODE, irq)
264 
265 #define intc_m_set_threshold(threshold) \
266  intc_set_threshold(HPM_PLIC_TARGET_M_MODE, threshold)
267 
268 #define intc_m_get_threshold() \
269  intc_get_threshold(HPM_PLIC_TARGET_M_MODE)
270 
271 /*
272  * @brief Complete IRQ for machine mode
273  *
274  * @param[in] irq Interrupt number
275  */
276 #define intc_m_complete_irq(irq) \
277  intc_complete_irq(HPM_PLIC_TARGET_M_MODE, irq)
278 
279 /*
280  * @brief Claim IRQ for machine mode
281  *
282  */
283 #define intc_m_claim_irq() intc_claim_irq(HPM_PLIC_TARGET_M_MODE)
284 
285 /*
286  * @brief Enable IRQ for machine mode with priority
287  *
288  * @param[in] irq Interrupt number
289  * @param[in] priority Priority of interrupt
290  */
291 #define intc_m_enable_irq_with_priority(irq, priority) \
292  do { \
293  intc_set_irq_priority(irq, priority); \
294  intc_m_enable_irq(irq); \
295  } while (0)
296 
297 
298 
299 /* Supervisor mode */
300 
305 ATTR_ALWAYS_INLINE static inline void intc_s_enable_swi(void)
306 {
308 }
309 
310 
315 ATTR_ALWAYS_INLINE static inline void intc_s_disable_swi(void)
316 {
318 }
319 
320 
325 ATTR_ALWAYS_INLINE static inline void intc_s_trigger_swi(void)
326 {
328 }
329 
330 
335 ATTR_ALWAYS_INLINE static inline void intc_s_complete_swi(void)
336 {
338 }
339 
340 /*
341  * @brief Enable IRQ for supervisor mode
342  *
343  * @param[in] irq Interrupt number
344  */
345 #define intc_s_enable_irq(irq) \
346  intc_enable_irq(HPM_PLIC_TARGET_S_MODE, irq)
347 
348 /*
349  * @brief Disable IRQ for supervisor mode
350  *
351  * @param[in] irq Interrupt number
352  */
353 #define intc_s_disable_irq(irq) \
354  intc_disable_irq(HPM_PLIC_TARGET_S_MODE, irq)
355 
356 #define intc_set_s_threshold(threshold) \
357  intc_set_threshold(HPM_PLIC_TARGET_S_MODE, threshold)
358 
359 /*
360  * @brief Complete IRQ for supervisor mode
361  *
362  * @param[in] irq Interrupt number
363  */
364 #define intc_s_complete_irq(irq) \
365  intc_complete_irq(HPM_PLIC_TARGET_S_MODE, irq)
366 
367 /*
368  * @brief Claim IRQ for supervisor mode
369  *
370  */
371 #define intc_s_claim_irq() intc_claim_irq(HPM_PLIC_TARGET_S_MODE)
372 
373 /*
374  * @brief Enable IRQ for supervisor mode with priority
375  *
376  * @param[in] irq Interrupt number
377  * @param[in] priority Priority of interrupt
378  */
379 #define intc_s_enable_irq_with_priority(irq, priority) \
380  do { \
381  intc_set_irq_priority(irq, priority); \
382  intc_s_enable_irq(irq); \
383  } while (0)
384 
385 
386 /*
387  * @brief Enable specific interrupt
388  *
389  * @param[in] target Target to handle specific interrupt
390  * @param[in] irq Interrupt number
391  */
392 ATTR_ALWAYS_INLINE static inline void intc_enable_irq(uint32_t target, uint32_t irq)
393 {
394  __plic_enable_irq(HPM_PLIC_BASE, target, irq);
395 }
396 
403 ATTR_ALWAYS_INLINE static inline void intc_set_irq_priority(uint32_t irq, uint32_t priority)
404 {
405  __plic_set_irq_priority(HPM_PLIC_BASE, irq, priority);
406 }
407 
414 ATTR_ALWAYS_INLINE static inline void intc_disable_irq(uint32_t target, uint32_t irq)
415 {
416  __plic_disable_irq(HPM_PLIC_BASE, target, irq);
417 }
418 
425 ATTR_ALWAYS_INLINE static inline void intc_set_threshold(uint32_t target, uint32_t threshold)
426 {
427  __plic_set_threshold(HPM_PLIC_BASE, target, threshold);
428 }
429 
435 ATTR_ALWAYS_INLINE static inline uint32_t intc_get_threshold(uint32_t target)
436 {
437  return __plic_get_threshold(HPM_PLIC_BASE, target);
438 }
439 
446 ATTR_ALWAYS_INLINE static inline uint32_t intc_claim_irq(uint32_t target)
447 {
448  return __plic_claim_irq(HPM_PLIC_BASE, target);
449 }
450 
458 ATTR_ALWAYS_INLINE static inline void intc_complete_irq(uint32_t target, uint32_t irq)
459 {
460  __plic_complete_irq(HPM_PLIC_BASE, target, irq);
461 }
462 
463 /*
464  * Vectored based irq install and uninstall
465  */
466 /* Machine mode */
467 extern int __vector_table[];
468 extern void default_irq_entry(void);
469 
477 ATTR_ALWAYS_INLINE static inline void install_isr(uint32_t irq, uint32_t isr)
478 {
479  __vector_table[irq] = isr;
480 }
481 
488 ATTR_ALWAYS_INLINE static inline void uninstall_isr(uint32_t irq)
489 {
490  __vector_table[irq] = (int) default_irq_entry;
491 }
492 
493 /* Supervisor mode */
494 extern int __vector_s_table[];
495 extern void default_s_irq_entry(void);
503 ATTR_ALWAYS_INLINE static inline void install_s_isr(uint32_t irq, uint32_t isr)
504 {
505  __vector_s_table[irq] = isr;
506 }
507 
514 ATTR_ALWAYS_INLINE static inline void uninstall_s_isr(uint32_t irq)
515 {
516  __vector_s_table[irq] = (int) default_s_irq_entry;
517 }
518 
519 
520 /*
521  * Inline nested irq entry/exit macros
522  */
523 /*
524  * @brief Save CSR
525  * @param[in] r Target CSR to be saved
526  */
527 #define SAVE_CSR(r) register long __##r = read_csr(r);
528 
529 /*
530  * @brief Restore macro
531  *
532  * @param[in] r Target CSR to be restored
533  */
534 #define RESTORE_CSR(r) write_csr(r, __##r);
535 
536 #if defined(SUPPORT_PFT_ARCH) && SUPPORT_PFT_ARCH
537 #define SAVE_MXSTATUS() SAVE_CSR(CSR_MXSTATUS)
538 #define RESTORE_MXSTATUS() RESTORE_CSR(CSR_MXSTATUS)
539 #else
540 #define SAVE_MXSTATUS()
541 #define RESTORE_MXSTATUS()
542 #endif
543 
544 #ifdef __riscv_flen
545 #define SAVE_FCSR() register int __fcsr = read_fcsr();
546 #define RESTORE_FCSR() write_fcsr(__fcsr);
547 #else
548 #define SAVE_FCSR()
549 #define RESTORE_FCSR()
550 #endif
551 
552 #ifdef __riscv_dsp
553 #define SAVE_UCODE() SAVE_CSR(CSR_UCODE)
554 #define RESTORE_UCODE() RESTORE_CSR(CSR_UCODE)
555 #else
556 #define SAVE_UCODE()
557 #define RESTORE_UCODE()
558 #endif
559 
560 #ifdef __riscv_flen
561 #if __riscv_flen == 32
562 /* RV32I caller registers + MCAUSE + MEPC + MSTATUS +MXSTATUS + 20 FPU caller registers +FCSR + UCODE (DSP) */
563 #define CONTEXT_REG_NUM (4 * (16 + 4 + 20))
564 #else /* __riscv_flen = 64 */
565 /* RV32I caller registers + MCAUSE + MEPC + MSTATUS +MXSTATUS + 20 DFPU caller + FCSR registers + UCODE (DSP) */
566 #define CONTEXT_REG_NUM (4 * (16 + 4 + 20 * 2))
567 #endif
568 
569 #else
570 /* RV32I caller registers + MCAUSE + MEPC + MSTATUS +MXSTATUS + UCODE (DSP)*/
571 #define CONTEXT_REG_NUM (4 * (16 + 4))
572 #endif
573 
574 #ifdef __riscv_flen
575 /*
576  * Save FPU caller registers:
577  * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
578  */
579 #if __riscv_flen == 32
580 #ifdef __ICCRISCV__
581 #define SAVE_FPU_CONTEXT() { \
582  __asm volatile("\n\
583  c.fswsp ft0, 20*4\n\
584  c.fswsp ft1, 21*4 \n\
585  c.fswsp ft2, 22*4 \n\
586  c.fswsp ft3, 23*4 \n\
587  c.fswsp ft4, 24*4 \n\
588  c.fswsp ft5, 25*4 \n\
589  c.fswsp ft6, 26*4 \n\
590  c.fswsp ft7, 27*4 \n\
591  c.fswsp fa0, 28*4 \n\
592  c.fswsp fa1, 29*4 \n\
593  c.fswsp fa2, 30*4 \n\
594  c.fswsp fa3, 31*4 \n\
595  c.fswsp fa4, 32*4 \n\
596  c.fswsp fa5, 33*4 \n\
597  c.fswsp fa6, 34*4 \n\
598  c.fswsp fa7, 35*4 \n\
599  c.fswsp ft8, 36*4 \n\
600  c.fswsp ft9, 37*4 \n\
601  c.fswsp ft10, 38*4 \n\
602  c.fswsp ft11, 39*4 \n");\
603 }
604 
605 /*
606  * Restore FPU caller registers:
607  * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
608  */
609 #define RESTORE_FPU_CONTEXT() { \
610  __asm volatile("\n\
611  c.flwsp ft0, 20*4\n\
612  c.flwsp ft1, 21*4 \n\
613  c.flwsp ft2, 22*4 \n\
614  c.flwsp ft3, 23*4 \n\
615  c.flwsp ft4, 24*4 \n\
616  c.flwsp ft5, 25*4 \n\
617  c.flwsp ft6, 26*4 \n\
618  c.flwsp ft7, 27*4 \n\
619  c.flwsp fa0, 28*4 \n\
620  c.flwsp fa1, 29*4 \n\
621  c.flwsp fa2, 30*4 \n\
622  c.flwsp fa3, 31*4 \n\
623  c.flwsp fa4, 32*4 \n\
624  c.flwsp fa5, 33*4 \n\
625  c.flwsp fa6, 34*4 \n\
626  c.flwsp fa7, 35*4 \n\
627  c.flwsp ft8, 36*4 \n\
628  c.flwsp ft9, 37*4 \n\
629  c.flwsp ft10, 38*4 \n\
630  c.flwsp ft11, 39*4 \n");\
631 }
632 #else /* __ICCRISCV__ not defined */
633 #define SAVE_FPU_CONTEXT() { \
634  __asm volatile("\n\
635  c.fswsp ft0, 20*4(sp)\n\
636  c.fswsp ft1, 21*4(sp) \n\
637  c.fswsp ft2, 22*4(sp) \n\
638  c.fswsp ft3, 23*4(sp) \n\
639  c.fswsp ft4, 24*4(sp) \n\
640  c.fswsp ft5, 25*4(sp) \n\
641  c.fswsp ft6, 26*4(sp) \n\
642  c.fswsp ft7, 27*4(sp) \n\
643  c.fswsp fa0, 28*4(sp) \n\
644  c.fswsp fa1, 29*4(sp) \n\
645  c.fswsp fa2, 30*4(sp) \n\
646  c.fswsp fa3, 31*4(sp) \n\
647  c.fswsp fa4, 32*4(sp) \n\
648  c.fswsp fa5, 33*4(sp) \n\
649  c.fswsp fa6, 34*4(sp) \n\
650  c.fswsp fa7, 35*4(sp) \n\
651  c.fswsp ft8, 36*4(sp) \n\
652  c.fswsp ft9, 37*4(sp) \n\
653  c.fswsp ft10, 38*4(sp) \n\
654  c.fswsp ft11, 39*4(sp) \n");\
655 }
656 
657 /*
658  * Restore FPU caller registers:
659  * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
660  */
661 #define RESTORE_FPU_CONTEXT() { \
662  __asm volatile("\n\
663  c.flwsp ft0, 20*4(sp)\n\
664  c.flwsp ft1, 21*4(sp) \n\
665  c.flwsp ft2, 22*4(sp) \n\
666  c.flwsp ft3, 23*4(sp) \n\
667  c.flwsp ft4, 24*4(sp) \n\
668  c.flwsp ft5, 25*4(sp) \n\
669  c.flwsp ft6, 26*4(sp) \n\
670  c.flwsp ft7, 27*4(sp) \n\
671  c.flwsp fa0, 28*4(sp) \n\
672  c.flwsp fa1, 29*4(sp) \n\
673  c.flwsp fa2, 30*4(sp) \n\
674  c.flwsp fa3, 31*4(sp) \n\
675  c.flwsp fa4, 32*4(sp) \n\
676  c.flwsp fa5, 33*4(sp) \n\
677  c.flwsp fa6, 34*4(sp) \n\
678  c.flwsp fa7, 35*4(sp) \n\
679  c.flwsp ft8, 36*4(sp) \n\
680  c.flwsp ft9, 37*4(sp) \n\
681  c.flwsp ft10, 38*4(sp) \n\
682  c.flwsp ft11, 39*4(sp) \n");\
683 }
684 #endif
685 #else /*__riscv_flen == 64*/
686 #ifdef __ICCRISCV__
687 #define SAVE_FPU_CONTEXT() { \
688  __asm volatile("\n\
689  c.fsdsp ft0, 20*4\n\
690  c.fsdsp ft1, 22*4 \n\
691  c.fsdsp ft2, 24*4 \n\
692  c.fsdsp ft3, 26*4 \n\
693  c.fsdsp ft4, 28*4 \n\
694  c.fsdsp ft5, 30*4 \n\
695  c.fsdsp ft6, 32*4 \n\
696  c.fsdsp ft7, 34*4 \n\
697  c.fsdsp fa0, 36*4 \n\
698  c.fsdsp fa1, 38*4 \n\
699  c.fsdsp fa2, 40*4 \n\
700  c.fsdsp fa3, 42*4 \n\
701  c.fsdsp fa4, 44*4 \n\
702  c.fsdsp fa5, 46*4 \n\
703  c.fsdsp fa6, 48*4 \n\
704  c.fsdsp fa7, 50*4 \n\
705  c.fsdsp ft8, 52*4 \n\
706  c.fsdsp ft9, 54*4 \n\
707  c.fsdsp ft10, 56*4 \n\
708  c.fsdsp ft11, 58*4 \n");\
709 }
710 
711 /*
712  * Restore FPU caller registers:
713  * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
714  */
715 #define RESTORE_FPU_CONTEXT() { \
716  __asm volatile("\n\
717  c.fldsp ft0, 20*4\n\
718  c.fldsp ft1, 22*4 \n\
719  c.fldsp ft2, 24*4 \n\
720  c.fldsp ft3, 26*4 \n\
721  c.fldsp ft4, 28*4 \n\
722  c.fldsp ft5, 30*4 \n\
723  c.fldsp ft6, 32*4 \n\
724  c.fldsp ft7, 34*4 \n\
725  c.fldsp fa0, 36*4 \n\
726  c.fldsp fa1, 38*4 \n\
727  c.fldsp fa2, 40*4 \n\
728  c.fldsp fa3, 42*4 \n\
729  c.fldsp fa4, 44*4 \n\
730  c.fldsp fa5, 46*4 \n\
731  c.fldsp fa6, 48*4 \n\
732  c.fldsp fa7, 50*4 \n\
733  c.fldsp ft8, 52*4 \n\
734  c.fldsp ft9, 54*4 \n\
735  c.fldsp ft10, 56*4 \n\
736  c.fldsp ft11, 58*4 \n");\
737 }
738 #else /*__riscv_flen == 64*/
739 #define SAVE_FPU_CONTEXT() { \
740  __asm volatile("\n\
741  c.fsdsp ft0, 20*4(sp)\n\
742  c.fsdsp ft1, 22*4(sp) \n\
743  c.fsdsp ft2, 24*4(sp) \n\
744  c.fsdsp ft3, 26*4(sp) \n\
745  c.fsdsp ft4, 28*4(sp) \n\
746  c.fsdsp ft5, 30*4(sp) \n\
747  c.fsdsp ft6, 32*4(sp) \n\
748  c.fsdsp ft7, 34*4(sp) \n\
749  c.fsdsp fa0, 36*4(sp) \n\
750  c.fsdsp fa1, 38*4(sp) \n\
751  c.fsdsp fa2, 40*4(sp) \n\
752  c.fsdsp fa3, 42*4(sp) \n\
753  c.fsdsp fa4, 44*4(sp) \n\
754  c.fsdsp fa5, 46*4(sp) \n\
755  c.fsdsp fa6, 48*4(sp) \n\
756  c.fsdsp fa7, 50*4(sp) \n\
757  c.fsdsp ft8, 52*4(sp) \n\
758  c.fsdsp ft9, 54*4(sp) \n\
759  c.fsdsp ft10, 56*4(sp) \n\
760  c.fsdsp ft11, 58*4(sp) \n");\
761 }
762 
763 /*
764  * Restore FPU caller registers:
765  * NOTE: To simplify the logic, the FPU caller registers are always stored at word offset 20 in the stack
766  */
767 #define RESTORE_FPU_CONTEXT() { \
768  __asm volatile("\n\
769  c.fldsp ft0, 20*4(sp)\n\
770  c.fldsp ft1, 22*4(sp) \n\
771  c.fldsp ft2, 24*4(sp) \n\
772  c.fldsp ft3, 26*4(sp) \n\
773  c.fldsp ft4, 28*4(sp) \n\
774  c.fldsp ft5, 30*4(sp) \n\
775  c.fldsp ft6, 32*4(sp) \n\
776  c.fldsp ft7, 34*4(sp) \n\
777  c.fldsp fa0, 36*4(sp) \n\
778  c.fldsp fa1, 38*4(sp) \n\
779  c.fldsp fa2, 40*4(sp) \n\
780  c.fldsp fa3, 42*4(sp) \n\
781  c.fldsp fa4, 44*4(sp) \n\
782  c.fldsp fa5, 46*4(sp) \n\
783  c.fldsp fa6, 48*4(sp) \n\
784  c.fldsp fa7, 50*4(sp) \n\
785  c.fldsp ft8, 52*4(sp) \n\
786  c.fldsp ft9, 54*4(sp) \n\
787  c.fldsp ft10, 56*4(sp) \n\
788  c.fldsp ft11, 58*4(sp) \n");\
789 }
790 #endif
791 #endif
792 #else
793 #define SAVE_FPU_CONTEXT()
794 #define RESTORE_FPU_CONTEXT()
795 #endif
796 
797 #ifdef __ICCRISCV__
801 #define SAVE_CALLER_CONTEXT() { \
802  __asm volatile("addi sp, sp, %0" : : "i"(-CONTEXT_REG_NUM) :);\
803  __asm volatile("\n\
804  c.swsp ra, 0*4 \n\
805  c.swsp t0, 1*4 \n\
806  c.swsp t1, 2*4 \n\
807  c.swsp t2, 3*4 \n\
808  c.swsp s1, 4*4 \n\
809  c.swsp a0, 5*4 \n\
810  c.swsp a1, 6*4 \n\
811  c.swsp a2, 7*4 \n\
812  c.swsp a3, 8*4 \n\
813  c.swsp a4, 9*4 \n\
814  c.swsp a5, 10*4 \n\
815  c.swsp a6, 11*4 \n\
816  c.swsp a7, 12*4 \n\
817  c.swsp s2, 13*4 \n\
818  c.swsp s3, 14*4 \n\
819  c.swsp s4, 15*4 \n\
820  c.swsp t3, 16*4 \n\
821  c.swsp t4, 17*4 \n\
822  c.swsp t5, 18*4 \n\
823  c.swsp t6, 19*4"); \
824  SAVE_FPU_CONTEXT(); \
825 }
826 
830 #define RESTORE_CALLER_CONTEXT() { \
831  __asm volatile("\n\
832  c.lwsp ra, 0*4 \n\
833  c.lwsp t0, 1*4 \n\
834  c.lwsp t1, 2*4 \n\
835  c.lwsp t2, 3*4 \n\
836  c.lwsp s1, 4*4 \n\
837  c.lwsp a0, 5*4 \n\
838  c.lwsp a1, 6*4 \n\
839  c.lwsp a2, 7*4 \n\
840  c.lwsp a3, 8*4 \n\
841  c.lwsp a4, 9*4 \n\
842  c.lwsp a5, 10*4 \n\
843  c.lwsp a6, 11*4 \n\
844  c.lwsp a7, 12*4 \n\
845  c.lwsp s2, 13*4 \n\
846  c.lwsp s3, 14*4 \n\
847  c.lwsp s4, 15*4 \n\
848  c.lwsp t3, 16*4 \n\
849  c.lwsp t4, 17*4 \n\
850  c.lwsp t5, 18*4 \n\
851  c.lwsp t6, 19*4 \n");\
852  RESTORE_FPU_CONTEXT(); \
853  __asm volatile("addi sp, sp, %0" : : "i"(CONTEXT_REG_NUM) :);\
854 }
855 #else
859 #define SAVE_CALLER_CONTEXT() { \
860  __asm volatile("addi sp, sp, %0" : : "i"(-CONTEXT_REG_NUM) :);\
861  __asm volatile("\n\
862  c.swsp ra, 0*4(sp) \n\
863  c.swsp t0, 1*4(sp) \n\
864  c.swsp t1, 2*4(sp) \n\
865  c.swsp t2, 3*4(sp) \n\
866  c.swsp s1, 4*4(sp) \n\
867  c.swsp a0, 5*4(sp) \n\
868  c.swsp a1, 6*4(sp) \n\
869  c.swsp a2, 7*4(sp) \n\
870  c.swsp a3, 8*4(sp) \n\
871  c.swsp a4, 9*4(sp) \n\
872  c.swsp a5, 10*4(sp) \n\
873  c.swsp a6, 11*4(sp) \n\
874  c.swsp a7, 12*4(sp) \n\
875  c.swsp s2, 13*4(sp) \n\
876  c.swsp s3, 14*4(sp) \n\
877  c.swsp s4, 15*4(sp) \n\
878  c.swsp t3, 16*4(sp) \n\
879  c.swsp t4, 17*4(sp) \n\
880  c.swsp t5, 18*4(sp) \n\
881  c.swsp t6, 19*4(sp)"); \
882  SAVE_FPU_CONTEXT(); \
883 }
884 
888 #define RESTORE_CALLER_CONTEXT() { \
889  __asm volatile("\n\
890  c.lwsp ra, 0*4(sp) \n\
891  c.lwsp t0, 1*4(sp) \n\
892  c.lwsp t1, 2*4(sp) \n\
893  c.lwsp t2, 3*4(sp) \n\
894  c.lwsp s1, 4*4(sp) \n\
895  c.lwsp a0, 5*4(sp) \n\
896  c.lwsp a1, 6*4(sp) \n\
897  c.lwsp a2, 7*4(sp) \n\
898  c.lwsp a3, 8*4(sp) \n\
899  c.lwsp a4, 9*4(sp) \n\
900  c.lwsp a5, 10*4(sp) \n\
901  c.lwsp a6, 11*4(sp) \n\
902  c.lwsp a7, 12*4(sp) \n\
903  c.lwsp s2, 13*4(sp) \n\
904  c.lwsp s3, 14*4(sp) \n\
905  c.lwsp s4, 15*4(sp) \n\
906  c.lwsp t3, 16*4(sp) \n\
907  c.lwsp t4, 17*4(sp) \n\
908  c.lwsp t5, 18*4(sp) \n\
909  c.lwsp t6, 19*4(sp) \n");\
910  RESTORE_FPU_CONTEXT(); \
911  __asm volatile("addi sp, sp, %0" : : "i"(CONTEXT_REG_NUM) :);\
912 }
913 #endif
914 
915 #ifdef __riscv_flen
916 #define SAVE_FPU_STATE() { \
917  __asm volatile("frcsr s1\n"); \
918 }
919 
920 #define RESTORE_FPU_STATE() { \
921  __asm volatile("fscsr s1\n"); \
922 }
923 #else
924 #define SAVE_FPU_STATE()
925 #define RESTORE_FPU_STATE()
926 #endif
927 
928 #ifdef __riscv_dsp
929 /*
930  * Save DSP context
931  * NOTE: DSP context registers are stored at word offset 41 in the stack
932  */
933 #define SAVE_DSP_CONTEXT() { \
934  __asm volatile("csrrs s4, %0, x0\n" ::"i"(CSR_UCODE):); \
935 }
936 /*
937  * @brief Restore DSP context
938  * @note DSP context registers are stored at word offset 41 in the stack
939  */
940 #define RESTORE_DSP_CONTEXT() {\
941  __asm volatile("csrw %0, s4\n" ::"i"(CSR_UCODE):); \
942 }
943 
944 #else
945 #define SAVE_DSP_CONTEXT()
946 #define RESTORE_DSP_CONTEXT()
947 #endif
948 
949 /*
950  * @brief Enter Nested IRQ Handling
951  * @note To simplify the logic, Nested IRQ related registers are stored in the stack as below:
952  * MCAUSE - word offset 16 (not used in the vectored mode)
953  * EPC - word offset 17
954  * MSTATUS = word offset 18
955  * MXSTATUS = word offset 19
956  */
957 #define ENTER_NESTED_IRQ_HANDLING_M() { \
958  __asm volatile("\n\
959  csrr s2, mepc \n\
960  csrr s3, mstatus \n");\
961  SAVE_FPU_STATE(); \
962  SAVE_DSP_CONTEXT(); \
963  __asm volatile("csrsi mstatus, 8"); \
964 }
965 
966 /*
967  * @brief Complete IRQ Handling
968  */
969 #define COMPLETE_IRQ_HANDLING_M(irq_num) { \
970  __asm volatile("csrci mstatus, 8"); \
971  __asm volatile("lui a4, 0xe4200"); \
972  __asm volatile("li a3, %0" : : "i" (irq_num) :); \
973  __asm volatile("sw a3, 4(a4)"); \
974 }
975 
976 /*
977  * @brief Exit Nested IRQ Handling
978  * @note To simplify the logic, Nested IRQ related registers are stored in the stack as below:
979  * MCAUSE - word offset 16 (not used in the vectored mode)
980  * EPC - word offset 17
981  * MSTATUS = word offset 18
982  * MXSTATUS = word offset 19
983  */
984 #define EXIT_NESTED_IRQ_HANDLING_M() { \
985  __asm volatile("\n\
986  csrw mstatus, s3 \n\
987  csrw mepc, s2 \n");\
988  RESTORE_FPU_STATE(); \
989  RESTORE_DSP_CONTEXT(); \
990 }
991 
992 
993 #define ENTER_NESTED_IRQ_HANDLING_S() {\
994  __asm volatile("\n\
995  csrr s2, sepc \n\
996  csrr s3, sstatus \n");\
997  SAVE_FPU_STATE(); \
998  SAVE_DSP_CONTEXT(); \
999  __asm volatile("csrsi sstatus, 2"); \
1000 }
1001 #define COMPLETE_IRQ_HANDLING_S(irq_num) {\
1002  __asm volatile("csrci sstatus, 2"); \
1003  __asm volatile("lui a4, 0xe4201"); \
1004  __asm volatile("li a3, %0" : : "i" (irq_num) :); \
1005  __asm volatile("sw a3, 4(a4)"); \
1006 }
1007 
1008 /*
1009  * @brief Exit Nested IRQ Handling at supervisor mode
1010  * @note To simplify the logic, Nested IRQ related registers are stored in the stack as below:
1011  * SCAUSE - word offset 16 (not used in the vectored mode)
1012  * EPC - word offset 17
1013  * SSTATUS = word offset 18
1014  */
1015 #define EXIT_NESTED_IRQ_HANDLING_S() { \
1016  __asm volatile("\n\
1017  csrw sstatus, s3 \n\
1018  csrw sepc, s2 \n");\
1019  RESTORE_FPU_STATE(); \
1020  RESTORE_DSP_CONTEXT(); \
1021 }
1022 
1023 /* @brief Nested IRQ entry macro : Save CSRs and enable global irq. */
1024 #define NESTED_IRQ_ENTER() \
1025  SAVE_CSR(CSR_MEPC) \
1026  SAVE_CSR(CSR_MSTATUS) \
1027  SAVE_MXSTATUS() \
1028  SAVE_FCSR() \
1029  SAVE_UCODE() \
1030  set_csr(CSR_MSTATUS, CSR_MSTATUS_MIE_MASK);
1031 
1032 /* @brief Nested IRQ exit macro : Restore CSRs */
1033 #define NESTED_IRQ_EXIT() \
1034  RESTORE_CSR(CSR_MSTATUS) \
1035  RESTORE_CSR(CSR_MEPC) \
1036  RESTORE_MXSTATUS() \
1037  RESTORE_FCSR() \
1038  RESTORE_UCODE()
1039 
1040 #ifdef __cplusplus
1041 #define HPM_EXTERN_C extern "C"
1042 #else
1043 #define HPM_EXTERN_C
1044 #endif
1045 
1046 #define ISR_NAME_M(irq_num) default_isr_##irq_num
1047 #define ISR_NAME_S(irq_num) default_isr_s_##irq_num
1054 #if !defined(USE_NONVECTOR_MODE) || (USE_NONVECTOR_MODE == 0)
1055 #if defined(CONFIG_FREERTOS) && CONFIG_FREERTOS
1056 #define FREERTOS_VECTOR_ISR_WRAPPER_NAME(irq_num) irq_handler_wrapper_##irq_num
1057 #define SDK_DECLARE_EXT_ISR_M(irq_num, isr) \
1058 void isr(void) __attribute__((section(".isr_vector"))); \
1059 HPM_EXTERN_C void FREERTOS_VECTOR_ISR_WRAPPER_NAME(irq_num)(void) __attribute__((section(".isr_vector"))); \
1060 void FREERTOS_VECTOR_ISR_WRAPPER_NAME(irq_num)(void) \
1061 { \
1062  isr();\
1063 }
1064 
1071 #define SDK_DECLARE_EXT_ISR_S(irq_num, isr) \
1072 void isr(void) __attribute__((section(".isr_s_vector")));\
1073 HPM_EXTERN_C void FREERTOS_VECTOR_ISR_WRAPPER_NAME(irq_num)(void) __attribute__((section(".isr_s_vector")));\
1074 void FREERTOS_VECTOR_ISR_WRAPPER_NAME(irq_num)(void) \
1075 { \
1076  isr();\
1077 }
1078 #else
1079 
1080 #define SDK_DECLARE_EXT_ISR_M(irq_num, isr) \
1081 void isr(void) __attribute__((section(".isr_vector")));\
1082 HPM_EXTERN_C HPM_ATTR_MACHINE_INTERRUPT void ISR_NAME_M(irq_num)(void);\
1083 HPM_ATTR_MACHINE_INTERRUPT void ISR_NAME_M(irq_num)(void) \
1084 { \
1085  SAVE_CALLER_CONTEXT(); \
1086  ENTER_NESTED_IRQ_HANDLING_M();\
1087  __asm volatile("la t1, %0\n\t" : : "i" (isr) : );\
1088  __asm volatile("jalr t1\n");\
1089  COMPLETE_IRQ_HANDLING_M(irq_num);\
1090  EXIT_NESTED_IRQ_HANDLING_M();\
1091  RESTORE_CALLER_CONTEXT();\
1092  __asm volatile("fence io, io");\
1093 }
1094 
1101 #define SDK_DECLARE_EXT_ISR_S(irq_num, isr) \
1102 void isr(void) __attribute__((section(".isr_s_vector")));\
1103 HPM_EXTERN_C HPM_ATTR_SUPERVISOR_INTERRUPT void ISR_NAME_S(irq_num)(void);\
1104 HPM_ATTR_SUPERVISOR_INTERRUPT void ISR_NAME_S(irq_num)(void) {\
1105  SAVE_CALLER_CONTEXT(); \
1106  ENTER_NESTED_IRQ_HANDLING_S();\
1107  __asm volatile("la t1, %0\n\t" : : "i" (isr) : );\
1108  __asm volatile("jalr t1\n");\
1109  COMPLETE_IRQ_HANDLING_S(irq_num);\
1110  EXIT_NESTED_IRQ_HANDLING_S();\
1111  RESTORE_CALLER_CONTEXT();\
1112  __asm volatile("fence io, io");\
1113 }
1114 #endif
1115 #else
1116 
1117 #define SDK_DECLARE_EXT_ISR_M(irq_num, isr) \
1118 void isr(void) __attribute__((section(".isr_vector")));\
1119 HPM_EXTERN_C void ISR_NAME_M(irq_num)(void) __attribute__((section(".isr_vector")));\
1120 void ISR_NAME_M(irq_num)(void) { \
1121  isr(); \
1122 }
1123 
1124 #define SDK_DECLARE_EXT_ISR_S(irq_num, isr) \
1125 void isr(void) __attribute__((section(".isr_vector")));\
1126 HPM_EXTERN_C void ISR_NAME_S(irq_num)(void) __attribute__((section(".isr_vector")));\
1127 void ISR_NAME_S(irq_num)(void) { \
1128  isr(); \
1129 }
1130 
1131 #endif
1132 
1133 
1139 #define SDK_DECLARE_MCHTMR_ISR(isr) \
1140 void isr(void) __attribute__((section(".isr_vector")));\
1141 HPM_EXTERN_C void mchtmr_isr(void) __attribute__((section(".isr_vector"))); \
1142 void mchtmr_isr(void) {\
1143  isr();\
1144 }
1145 
1151 #define SDK_DECLARE_SWI_ISR(isr)\
1152 void isr(void) __attribute__((section(".isr_vector")));\
1153 HPM_EXTERN_C void swi_isr(void) __attribute__((section(".isr_vector"))); \
1154 void swi_isr(void) {\
1155  isr();\
1156 }
1157 
1158 /* Supervisor mode */
1159 
1165 #define SDK_DECLARE_MCHTMR_ISR_S(isr) \
1166 void isr(void) __attribute__((section(".isr_vector")));\
1167 HPM_EXTERN_C void mchtmr_s_isr(void) __attribute__((section(".isr_vector"))); \
1168 void mchtmr_s_isr(void) {\
1169  isr();\
1170 }
1171 
1177 #define SDK_DECLARE_SWI_ISR_S(isr)\
1178 void isr(void) __attribute__((section(".isr_vector")));\
1179 HPM_EXTERN_C void swi_s_isr(void) __attribute__((section(".isr_vector"))); \
1180 void swi_s_isr(void) {\
1181  isr();\
1182 }
1183 
1184 #define CSR_MSTATUS_MPP_S_MODE (0x1)
1185 #define MODE_SWITCH_FROM_M(mstatus, mepc, label, mode) \
1186 do { \
1187  if (label) { \
1188  write_csr(mepc, label); \
1189  } \
1190  clear_csr(mstatus, CSR_MSTATUS_MPP_MASK); \
1191  set_csr(mstatus, CSR_MSTATUS_MPP_SET(mode)); \
1192 } while(0)
1193 
1194 typedef void (*s_mode_entry)(void);
1195 
1201 static inline void switch_to_s_mode(s_mode_entry entry)
1202 {
1203  write_csr(CSR_SEPC, entry);
1205  if (entry) {
1206  __asm("mret");
1207  }
1208 }
1209 #ifdef __cplusplus
1210 }
1211 #endif
1212 
1216 #endif /* HPM_INTERRUPT_H */
#define CSR_MIE_MEIE_MASK
Definition: hpm_csr_regs.h:734
#define CSR_MEPC
Definition: hpm_csr_regs.h:31
#define CSR_MSTATUS
Definition: hpm_csr_regs.h:21
#define CSR_MIE_MTIE_MASK
Definition: hpm_csr_regs.h:758
#define CSR_MIE
Definition: hpm_csr_regs.h:23
#define CSR_MIE_MSIE_MASK
Definition: hpm_csr_regs.h:782
#define HPM_PLICSW_BASE
Definition: hpm_soc.h:52
#define HPM_PLIC_BASE
Definition: hpm_soc.h:38
#define CSR_SEPC
Definition: hpm_csr_regs.h:27
#define CSR_SSTATUS
Definition: hpm_csr_regs.h:21
#define CSR_SIE_SEIE_MASK
Definition: hpm_csr_regs.h:677
#define CSR_SIE_SSIE_MASK
Definition: hpm_csr_regs.h:725
#define CSR_SIP
Definition: hpm_csr_regs.h:30
#define CSR_SIE_STIE_MASK
Definition: hpm_csr_regs.h:701
#define CSR_MIDELEG
Definition: hpm_csr_regs.h:35
#define CSR_SIP_SSIP_MASK
Definition: hpm_csr_regs.h:889
#define CSR_SIE
Definition: hpm_csr_regs.h:24
static ATTR_ALWAYS_INLINE void intc_complete_irq(uint32_t target, uint32_t irq)
Complete IRQ.
Definition: hpm_interrupt.h:458
static ATTR_ALWAYS_INLINE void enable_global_irq(uint32_t mask)
Enable global IRQ with mask.
Definition: hpm_interrupt.h:34
static ATTR_ALWAYS_INLINE void intc_m_disable_swi(void)
Disable software interrupt.
Definition: hpm_interrupt.h:216
static ATTR_ALWAYS_INLINE void intc_s_complete_swi(void)
Complete software interrupt for supervisor mode.
Definition: hpm_interrupt.h:335
static ATTR_ALWAYS_INLINE void intc_s_trigger_swi(void)
Trigger software interrupt for supervisor mode.
Definition: hpm_interrupt.h:325
static ATTR_ALWAYS_INLINE void disable_irq_from_intc(void)
Disable IRQ from interrupt controller.
Definition: hpm_interrupt.h:73
static ATTR_ALWAYS_INLINE void intc_set_irq_priority(uint32_t irq, uint32_t priority)
Set interrupt priority.
Definition: hpm_interrupt.h:403
static ATTR_ALWAYS_INLINE void intc_disable_irq(uint32_t target, uint32_t irq)
Disable specific interrupt.
Definition: hpm_interrupt.h:414
static ATTR_ALWAYS_INLINE void intc_m_trigger_swi(void)
Trigger software interrupt.
Definition: hpm_interrupt.h:226
static ATTR_ALWAYS_INLINE void install_isr(uint32_t irq, uint32_t isr)
Install ISR for certain IRQ for ram based vector table.
Definition: hpm_interrupt.h:477
static ATTR_ALWAYS_INLINE void restore_s_global_irq(uint32_t mask)
Restore global IRQ with mask for supervisor mode.
Definition: hpm_interrupt.h:144
static ATTR_ALWAYS_INLINE void enable_irq_from_intc(void)
Enable IRQ from interrupt controller.
Definition: hpm_interrupt.h:64
static ATTR_ALWAYS_INLINE void restore_global_irq(uint32_t mask)
Restore global IRQ with mask.
Definition: hpm_interrupt.h:55
static ATTR_ALWAYS_INLINE void uninstall_s_isr(uint32_t irq)
Uninstall ISR for certain IRQ for ram based vector table for supervisor mode.
Definition: hpm_interrupt.h:514
static ATTR_ALWAYS_INLINE void enable_s_global_irq(uint32_t mask)
Enable global IRQ with mask for supervisor mode.
Definition: hpm_interrupt.h:123
static ATTR_ALWAYS_INLINE void intc_enable_irq(uint32_t target, uint32_t irq)
Definition: hpm_interrupt.h:392
static ATTR_ALWAYS_INLINE void install_s_isr(uint32_t irq, uint32_t isr)
Install ISR for certain IRQ for ram based vector table for supervisor mode.
Definition: hpm_interrupt.h:503
static ATTR_ALWAYS_INLINE void enable_s_irq_from_intc(void)
Enable IRQ from interrupt controller for supervisor mode.
Definition: hpm_interrupt.h:162
static ATTR_ALWAYS_INLINE void delegate_irq(uint32_t mask)
Delegate IRQ handling.
Definition: hpm_interrupt.h:100
static ATTR_ALWAYS_INLINE void undelegate_irq(uint32_t mask)
Undelegate IRQ handling.
Definition: hpm_interrupt.h:110
static ATTR_ALWAYS_INLINE void disable_mchtmr_irq(void)
Disable machine timer IRQ.
Definition: hpm_interrupt.h:90
static ATTR_ALWAYS_INLINE void intc_m_init_swi(void)
Initialize software interrupt.
Definition: hpm_interrupt.h:196
static ATTR_ALWAYS_INLINE void intc_m_enable_swi(void)
Enable software interrupt.
Definition: hpm_interrupt.h:206
static ATTR_ALWAYS_INLINE void intc_m_complete_swi(void)
Complete software interrupt.
Definition: hpm_interrupt.h:244
static ATTR_ALWAYS_INLINE void intc_s_disable_swi(void)
Disable software interrupt for supervisor mode.
Definition: hpm_interrupt.h:315
static void switch_to_s_mode(s_mode_entry entry)
Switch mode to supervisor from machine.
Definition: hpm_interrupt.h:1201
static ATTR_ALWAYS_INLINE uint32_t intc_get_threshold(uint32_t target)
Get interrupt threshold.
Definition: hpm_interrupt.h:435
static ATTR_ALWAYS_INLINE void enable_s_mchtmr_irq(void)
Enable machine timer IRQ for supervisor mode.
Definition: hpm_interrupt.h:170
static ATTR_ALWAYS_INLINE void intc_set_threshold(uint32_t target, uint32_t threshold)
Set interrupt threshold.
Definition: hpm_interrupt.h:425
static ATTR_ALWAYS_INLINE void uninstall_isr(uint32_t irq)
Uninstall ISR for certain IRQ for ram based vector table.
Definition: hpm_interrupt.h:488
static ATTR_ALWAYS_INLINE uint32_t disable_s_global_irq(uint32_t mask)
Disable global IRQ with mask and return sstatus for supervisor mode.
Definition: hpm_interrupt.h:134
static ATTR_ALWAYS_INLINE uint32_t intc_claim_irq(uint32_t target)
Claim IRQ.
Definition: hpm_interrupt.h:446
static ATTR_ALWAYS_INLINE void intc_m_claim_swi(void)
Claim software interrupt.
Definition: hpm_interrupt.h:235
static ATTR_ALWAYS_INLINE uint32_t disable_global_irq(uint32_t mask)
Disable global IRQ with mask and return mstatus.
Definition: hpm_interrupt.h:45
static ATTR_ALWAYS_INLINE void disable_s_mchtmr_irq(void)
Disable machine timer IRQ.
Definition: hpm_interrupt.h:179
static ATTR_ALWAYS_INLINE void enable_mchtmr_irq(void)
Enable machine timer IRQ.
Definition: hpm_interrupt.h:81
static ATTR_ALWAYS_INLINE void disable_s_irq_from_intc(void)
Disable IRQ from interrupt controller for supervisor mode.
Definition: hpm_interrupt.h:153
static ATTR_ALWAYS_INLINE void intc_s_enable_swi(void)
Enable software interrupt for supervisor mode.
Definition: hpm_interrupt.h:305
#define CSR_MSTATUS_MPP_S_MODE
Definition: hpm_interrupt.h:1184
void default_s_irq_entry(void)
#define MODE_SWITCH_FROM_M(mstatus, mepc, label, mode)
Definition: hpm_interrupt.h:1185
void default_irq_entry(void)
#define PLICSWI
Definition: hpm_interrupt.h:190
void(* s_mode_entry)(void)
Definition: hpm_interrupt.h:1195
write_csr((0x7CC), cmd)
#define HPM_PLIC_TARGET_M_MODE
Definition: hpm_plic_drv.h:17
#define set_csr(csr_num, bit)
set bits in csr
Definition: riscv_core.h:58
#define clear_csr(csr_num, bit)
clear bits in csr
Definition: riscv_core.h:30
#define read_clear_csr(csr_num, bit)
read and clear bits in csr
Definition: riscv_core.h:40