interrupt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stddef.h>
  8. #include <assert.h>
  9. #include "soc/soc.h"
  10. #include "riscv/interrupt.h"
  11. #include "soc/interrupt_reg.h"
  12. #include "riscv/csr.h"
  13. #include "esp_attr.h"
  14. #include "riscv/rv_utils.h"
  15. #if SOC_INT_CLIC_SUPPORTED
  16. /**
  17. * If the target is using the CLIC as the interrupt controller, we have 32 external interrupt lines and 16 internal
  18. * lines. Let's consider the internal ones reserved and not mappable to any handler.
  19. */
  20. #define RV_EXTERNAL_INT_COUNT 32
  21. #define RV_EXTERNAL_INT_OFFSET (CLIC_EXT_INTR_NUM_OFFSET)
  22. #else // !SOC_INT_CLIC_SUPPORTED
  23. /**
  24. * In the case of INTC, all the interrupt lines are dedicated to external peripherals, so the offset is 0.
  25. * In the case of PLIC, the reserved interrupts are not contiguous, moreover, they are already marked as
  26. * unusable by the interrupt allocator, so the offset can also be 0 here.
  27. */
  28. #define RV_EXTERNAL_INT_COUNT 32
  29. #define RV_EXTERNAL_INT_OFFSET 0
  30. /* Since DR_REG_INTERRUPT_CORE0_BASE is not defined on some single-core targets, use the former
  31. * DR_REG_INTERRUPT_BASE macro instead. */
  32. #ifndef DR_REG_INTERRUPT_CORE0_BASE
  33. #define DR_REG_INTERRUPT_CORE0_BASE DR_REG_INTERRUPT_BASE
  34. #endif // DR_REG_INTERRUPT_CORE0_BASE
  35. #endif // SOC_INT_CLIC_SUPPORTED
  36. typedef struct {
  37. intr_handler_t handler;
  38. void *arg;
  39. } intr_handler_item_t;
  40. static intr_handler_item_t s_intr_handlers[SOC_CPU_CORES_NUM][RV_EXTERNAL_INT_COUNT];
  41. static inline void assert_valid_rv_int_num(int rv_int_num)
  42. {
  43. #if !SOC_INT_CLIC_SUPPORTED
  44. assert(rv_int_num != 0 && "Invalid CPU interrupt number");
  45. #endif
  46. assert(rv_int_num < RV_EXTERNAL_INT_COUNT && "Invalid CPU interrupt number");
  47. }
  48. static intr_handler_item_t* intr_get_item(int int_no)
  49. {
  50. assert_valid_rv_int_num(int_no);
  51. const uint32_t id = rv_utils_get_core_id();
  52. return &s_intr_handlers[id][int_no];
  53. }
  54. /*************************** Software interrupt dispatcher ***************************/
  55. void intr_handler_set(int int_no, intr_handler_t fn, void *arg)
  56. {
  57. intr_handler_item_t* item = intr_get_item(int_no);
  58. *item = (intr_handler_item_t) {
  59. .handler = fn,
  60. .arg = arg
  61. };
  62. }
  63. intr_handler_t intr_handler_get(int rv_int_num)
  64. {
  65. const intr_handler_item_t* item = intr_get_item(rv_int_num);
  66. return item->handler;
  67. }
  68. void *intr_handler_get_arg(int rv_int_num)
  69. {
  70. const intr_handler_item_t* item = intr_get_item(rv_int_num);
  71. return item->arg;
  72. }
  73. /* called from vectors.S */
  74. void _global_interrupt_handler(intptr_t sp, int mcause)
  75. {
  76. /* mcause contains the interrupt number that triggered the current interrupt, this number
  77. * also take into account local/internal interrupt, however, this should not happen in practice,
  78. * since we never map any peripheral to those. */
  79. assert(mcause >= RV_EXTERNAL_INT_OFFSET && "Interrupt sources must not be mapped to local interrupts");
  80. const intr_handler_item_t* item = intr_get_item(mcause - RV_EXTERNAL_INT_OFFSET);
  81. if (item->handler) {
  82. (*item->handler)(item->arg);
  83. }
  84. }
  85. /*************************** RISC-V interrupt enable/disable ***************************/
  86. void intr_matrix_route(int intr_src, int intr_num)
  87. {
  88. assert_valid_rv_int_num(intr_num);
  89. if (rv_utils_get_core_id() == 0) {
  90. REG_WRITE(DR_REG_INTERRUPT_CORE0_BASE + 4 * intr_src, intr_num + RV_EXTERNAL_INT_OFFSET);
  91. }
  92. #if SOC_CPU_CORES_NUM > 1
  93. else {
  94. REG_WRITE(DR_REG_INTERRUPT_CORE1_BASE + 4 * intr_src, intr_num + RV_EXTERNAL_INT_OFFSET);
  95. }
  96. #endif // SOC_CPU_CORES_NUM > 1
  97. }
  98. // CLIC for each interrupt line provides a IE register
  99. // this api is not used
  100. #if !SOC_INT_CLIC_SUPPORTED
  101. uint32_t esprv_intc_get_interrupt_unmask(void)
  102. {
  103. return REG_READ(INTERRUPT_CORE0_CPU_INT_ENABLE_REG);
  104. }
  105. #endif
  106. /*************************** ESP-RV Interrupt Controller ***************************/
  107. #if SOC_INT_CLIC_SUPPORTED
  108. enum intr_type esprv_intc_int_get_type(int rv_int_num)
  109. {
  110. uint32_t intr_type_reg = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_TRIG);
  111. return (intr_type_reg & 1) ? INTR_TYPE_EDGE : INTR_TYPE_LEVEL;
  112. }
  113. int esprv_intc_int_get_priority(int rv_int_num)
  114. {
  115. uint32_t intr_priority_reg = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_CTL);
  116. return (intr_priority_reg >> (8 - NLBITS));
  117. }
  118. bool esprv_intc_int_is_vectored(int rv_int_num)
  119. {
  120. const uint32_t shv = REG_GET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_SHV);
  121. return shv != 0;
  122. }
  123. void esprv_intc_int_set_vectored(int rv_int_num, bool vectored)
  124. {
  125. REG_SET_FIELD(CLIC_INT_CTRL_REG(rv_int_num + RV_EXTERNAL_INT_OFFSET), CLIC_INT_ATTR_SHV, vectored ? 1 : 0);
  126. }
  127. #else // !SOC_INT_CLIC_SUPPORTED
  128. enum intr_type esprv_intc_int_get_type(int rv_int_num)
  129. {
  130. uint32_t intr_type_reg = REG_READ(INTERRUPT_CORE0_CPU_INT_TYPE_REG);
  131. return (intr_type_reg & (1 << rv_int_num)) ? INTR_TYPE_EDGE : INTR_TYPE_LEVEL;
  132. }
  133. int esprv_intc_int_get_priority(int rv_int_num)
  134. {
  135. uint32_t intr_priority_reg = REG_READ(INTC_INT_PRIO_REG(rv_int_num));
  136. return intr_priority_reg;
  137. }
  138. #endif // SOC_INT_CLIC_SUPPORTED
  139. /*************************** Exception names. Used in .gdbinit file. ***************************/
  140. const char *riscv_excp_names[16] __attribute__((used)) = {
  141. "misaligned_fetch",
  142. "fault_fetch",
  143. "illegal_instruction",
  144. "breakpoint",
  145. "misaligned_load",
  146. "fault_load",
  147. "misaligned_store",
  148. "fault_store",
  149. "user_ecall",
  150. "supervisor_ecall",
  151. "hypervisor_ecall",
  152. "machine_ecall",
  153. "exec_page_fault",
  154. "load_page_fault",
  155. "reserved",
  156. "store_page_fault"
  157. };