crosscore_int.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include "esp_attr.h"
  8. #include "esp_err.h"
  9. #include "esp_cpu.h"
  10. #include "esp_intr_alloc.h"
  11. #include "esp_debug_helpers.h"
  12. #include "soc/periph_defs.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/portmacro.h"
  15. #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  16. #include "esp_gdbstub.h"
  17. #endif
  18. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  19. #include "soc/dport_reg.h"
  20. #else
  21. #include "soc/system_reg.h"
  22. #endif
  23. #if CONFIG_IDF_TARGET_ESP32P4
  24. #include "soc/hp_system_reg.h"
  25. #endif
  26. #define REASON_YIELD BIT(0)
  27. #define REASON_FREQ_SWITCH BIT(1)
  28. #define REASON_PRINT_BACKTRACE BIT(2)
  29. #define REASON_GDB_CALL BIT(3)
  30. #define REASON_TWDT_ABORT BIT(4)
  31. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  32. static volatile uint32_t reason[portNUM_PROCESSORS];
  33. /*
  34. ToDo: There is a small chance the CPU already has yielded when this ISR is serviced. In that case, it's running the intended task but
  35. the ISR will cause it to switch _away_ from it. portYIELD_FROM_ISR will probably just schedule the task again, but have to check that.
  36. */
  37. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
  38. {
  39. portYIELD_FROM_ISR();
  40. }
  41. static void IRAM_ATTR esp_crosscore_isr(void *arg) {
  42. uint32_t my_reason_val;
  43. //A pointer to the correct reason array item is passed to this ISR.
  44. volatile uint32_t *my_reason=arg;
  45. //Clear the interrupt first.
  46. #if CONFIG_IDF_TARGET_ESP32
  47. if (esp_cpu_get_core_id()==0) {
  48. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  49. } else {
  50. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
  51. }
  52. #elif CONFIG_IDF_TARGET_ESP32S2
  53. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  54. #elif CONFIG_IDF_TARGET_ESP32S3
  55. if (esp_cpu_get_core_id()==0) {
  56. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  57. } else {
  58. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, 0);
  59. }
  60. #elif CONFIG_IDF_TARGET_ESP32P4
  61. if (esp_cpu_get_core_id() == 0) {
  62. WRITE_PERI_REG(HP_SYSTEM_CPU_INT_FROM_CPU_0_REG, 0);
  63. } else {
  64. WRITE_PERI_REG(HP_SYSTEM_CPU_INT_FROM_CPU_1_REG, 0);
  65. }
  66. #elif CONFIG_IDF_TARGET_ARCH_RISCV
  67. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  68. #endif
  69. //Grab the reason and clear it.
  70. portENTER_CRITICAL_ISR(&reason_spinlock);
  71. my_reason_val=*my_reason;
  72. *my_reason=0;
  73. portEXIT_CRITICAL_ISR(&reason_spinlock);
  74. //Check what we need to do.
  75. if (my_reason_val & REASON_YIELD) {
  76. esp_crosscore_isr_handle_yield();
  77. }
  78. if (my_reason_val & REASON_FREQ_SWITCH) {
  79. /* Nothing to do here; the frequency switch event was already
  80. * handled by a hook in xtensa_vectors.S. Could be used in the future
  81. * to allow DFS features without the extra latency of the ISR hook.
  82. */
  83. }
  84. #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  85. if (my_reason_val & REASON_GDB_CALL) {
  86. update_breakpoints();
  87. }
  88. #endif // !CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  89. if (my_reason_val & REASON_PRINT_BACKTRACE) {
  90. esp_backtrace_print(100);
  91. }
  92. #if CONFIG_ESP_TASK_WDT_EN
  93. if (my_reason_val & REASON_TWDT_ABORT) {
  94. extern void task_wdt_timeout_abort(bool);
  95. /* Called from a crosscore interrupt, thus, we are not the core that received
  96. * the TWDT interrupt, call the function with `false` as a parameter. */
  97. task_wdt_timeout_abort(false);
  98. }
  99. #endif // CONFIG_ESP_TASK_WDT_EN
  100. }
  101. //Initialize the crosscore interrupt on this core. Call this once
  102. //on each active core.
  103. void esp_crosscore_int_init(void) {
  104. portENTER_CRITICAL(&reason_spinlock);
  105. reason[esp_cpu_get_core_id()]=0;
  106. portEXIT_CRITICAL(&reason_spinlock);
  107. esp_err_t err __attribute__((unused)) = ESP_OK;
  108. #if portNUM_PROCESSORS > 1
  109. if (esp_cpu_get_core_id()==0) {
  110. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  111. } else {
  112. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
  113. }
  114. #else
  115. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  116. #endif
  117. ESP_ERROR_CHECK(err);
  118. }
  119. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
  120. assert(core_id<portNUM_PROCESSORS);
  121. //Mark the reason we interrupt the other CPU
  122. portENTER_CRITICAL_ISR(&reason_spinlock);
  123. reason[core_id] |= reason_mask;
  124. portEXIT_CRITICAL_ISR(&reason_spinlock);
  125. //Poke the other CPU.
  126. #if CONFIG_IDF_TARGET_ESP32
  127. if (core_id==0) {
  128. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  129. } else {
  130. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  131. }
  132. #elif CONFIG_IDF_TARGET_ESP32S2
  133. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  134. #elif CONFIG_IDF_TARGET_ESP32S3
  135. if (core_id==0) {
  136. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  137. } else {
  138. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1);
  139. }
  140. #elif CONFIG_IDF_TARGET_ESP32P4
  141. if (core_id==0) {
  142. WRITE_PERI_REG(HP_SYSTEM_CPU_INT_FROM_CPU_0_REG, HP_SYSTEM_CPU_INT_FROM_CPU_0);
  143. } else {
  144. WRITE_PERI_REG(HP_SYSTEM_CPU_INT_FROM_CPU_1_REG, HP_SYSTEM_CPU_INT_FROM_CPU_1);
  145. }
  146. #elif CONFIG_IDF_TARGET_ARCH_RISCV
  147. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  148. #endif
  149. }
  150. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  151. {
  152. esp_crosscore_int_send(core_id, REASON_YIELD);
  153. }
  154. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  155. {
  156. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  157. }
  158. void IRAM_ATTR esp_crosscore_int_send_gdb_call(int core_id)
  159. {
  160. esp_crosscore_int_send(core_id, REASON_GDB_CALL);
  161. }
  162. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  163. {
  164. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  165. }
  166. #if CONFIG_ESP_TASK_WDT_EN
  167. void IRAM_ATTR esp_crosscore_int_send_twdt_abort(int core_id) {
  168. esp_crosscore_int_send(core_id, REASON_TWDT_ABORT);
  169. }
  170. #endif // CONFIG_ESP_TASK_WDT_EN