interrupt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <stddef.h>
  16. #include <assert.h>
  17. #include "riscv/interrupt.h"
  18. #include "soc/interrupt_reg.h"
  19. #include "riscv/csr.h"
  20. #include "esp_attr.h"
  21. #define RV_INT_COUNT 32
  22. static inline void assert_valid_rv_int_num(int rv_int_num)
  23. {
  24. assert(rv_int_num != 0 && rv_int_num < RV_INT_COUNT && "Invalid CPU interrupt number");
  25. }
  26. /*************************** Software interrupt dispatcher ***************************/
  27. typedef struct {
  28. intr_handler_t handler;
  29. void *arg;
  30. } intr_handler_item_t;
  31. static intr_handler_item_t s_intr_handlers[32];
  32. void intr_handler_set(int int_no, intr_handler_t fn, void *arg)
  33. {
  34. assert_valid_rv_int_num(int_no);
  35. s_intr_handlers[int_no] = (intr_handler_item_t) {
  36. .handler = fn,
  37. .arg = arg
  38. };
  39. }
  40. intr_handler_t intr_handler_get(int rv_int_num)
  41. {
  42. return s_intr_handlers[rv_int_num].handler;
  43. }
  44. void *intr_handler_get_arg(int rv_int_num)
  45. {
  46. return s_intr_handlers[rv_int_num].arg;
  47. }
  48. /* called from vectors.S */
  49. void _global_interrupt_handler(intptr_t sp, int mcause)
  50. {
  51. intr_handler_item_t it = s_intr_handlers[mcause];
  52. if (it.handler) {
  53. (*it.handler)(it.arg);
  54. }
  55. }
  56. /*************************** RISC-V interrupt enable/disable ***************************/
  57. void intr_matrix_route(int intr_src, int intr_num)
  58. {
  59. assert(intr_num != 0);
  60. REG_WRITE(DR_REG_INTERRUPT_BASE + 4 * intr_src, intr_num);
  61. }
  62. void riscv_global_interrupts_enable(void)
  63. {
  64. RV_SET_CSR(mstatus, MSTATUS_MIE);
  65. }
  66. void riscv_global_interrupts_disable(void)
  67. {
  68. RV_CLEAR_CSR(mstatus, MSTATUS_MIE);
  69. }
  70. uint32_t esprv_intc_get_interrupt_unmask(void)
  71. {
  72. return REG_READ(INTERRUPT_CORE0_CPU_INT_ENABLE_REG);
  73. }
  74. /*************************** Exception names. Used in .gdbinit file. ***************************/
  75. const char *riscv_excp_names[16] __attribute__((used)) = {
  76. "misaligned_fetch",
  77. "fault_fetch",
  78. "illegal_instruction",
  79. "breakpoint",
  80. "misaligned_load",
  81. "fault_load",
  82. "misaligned_store",
  83. "fault_store",
  84. "user_ecall",
  85. "supervisor_ecall",
  86. "hypervisor_ecall",
  87. "machine_ecall",
  88. "exec_page_fault",
  89. "load_page_fault",
  90. "reserved",
  91. "store_page_fault"
  92. };