crosscore_int.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  21. #include "soc/system_reg.h"
  22. #endif
  23. #define REASON_YIELD BIT(0)
  24. #define REASON_FREQ_SWITCH BIT(1)
  25. #define REASON_GDB_CALL BIT(3)
  26. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 && !IDF_TARGET_ESP32C2
  27. #define REASON_PRINT_BACKTRACE BIT(2)
  28. #define REASON_TWDT_ABORT BIT(4)
  29. #endif
  30. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  31. static volatile uint32_t reason[portNUM_PROCESSORS];
  32. /*
  33. 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
  34. 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.
  35. */
  36. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
  37. {
  38. portYIELD_FROM_ISR();
  39. }
  40. static void IRAM_ATTR esp_crosscore_isr(void *arg) {
  41. uint32_t my_reason_val;
  42. //A pointer to the correct reason array item is passed to this ISR.
  43. volatile uint32_t *my_reason=arg;
  44. //Clear the interrupt first.
  45. #if CONFIG_IDF_TARGET_ESP32
  46. if (esp_cpu_get_core_id()==0) {
  47. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  48. } else {
  49. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
  50. }
  51. #elif CONFIG_IDF_TARGET_ESP32S2
  52. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  53. #elif CONFIG_IDF_TARGET_ESP32S3
  54. if (esp_cpu_get_core_id()==0) {
  55. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  56. } else {
  57. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, 0);
  58. }
  59. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  60. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  61. #endif
  62. //Grab the reason and clear it.
  63. portENTER_CRITICAL_ISR(&reason_spinlock);
  64. my_reason_val=*my_reason;
  65. *my_reason=0;
  66. portEXIT_CRITICAL_ISR(&reason_spinlock);
  67. //Check what we need to do.
  68. if (my_reason_val & REASON_YIELD) {
  69. esp_crosscore_isr_handle_yield();
  70. }
  71. if (my_reason_val & REASON_FREQ_SWITCH) {
  72. /* Nothing to do here; the frequency switch event was already
  73. * handled by a hook in xtensa_vectors.S. Could be used in the future
  74. * to allow DFS features without the extra latency of the ISR hook.
  75. */
  76. }
  77. #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  78. if (my_reason_val & REASON_GDB_CALL) {
  79. update_breakpoints();
  80. }
  81. #endif // !CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  82. #if CONFIG_IDF_TARGET_ARCH_XTENSA // IDF-2986
  83. if (my_reason_val & REASON_PRINT_BACKTRACE) {
  84. esp_backtrace_print(100);
  85. }
  86. if (my_reason_val & REASON_TWDT_ABORT) {
  87. extern void task_wdt_timeout_abort_xtensa(bool);
  88. /* Called from a crosscore interrupt, thus, we are not the core that received
  89. * the TWDT interrupt, call the function with `false` as a parameter. */
  90. task_wdt_timeout_abort_xtensa(false);
  91. }
  92. #endif // CONFIG_IDF_TARGET_ARCH_XTENSA
  93. }
  94. //Initialize the crosscore interrupt on this core. Call this once
  95. //on each active core.
  96. void esp_crosscore_int_init(void) {
  97. portENTER_CRITICAL(&reason_spinlock);
  98. reason[esp_cpu_get_core_id()]=0;
  99. portEXIT_CRITICAL(&reason_spinlock);
  100. esp_err_t err __attribute__((unused)) = ESP_OK;
  101. #if portNUM_PROCESSORS > 1
  102. if (esp_cpu_get_core_id()==0) {
  103. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  104. } else {
  105. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
  106. }
  107. #else
  108. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  109. #endif
  110. ESP_ERROR_CHECK(err);
  111. }
  112. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
  113. assert(core_id<portNUM_PROCESSORS);
  114. //Mark the reason we interrupt the other CPU
  115. portENTER_CRITICAL_ISR(&reason_spinlock);
  116. reason[core_id] |= reason_mask;
  117. portEXIT_CRITICAL_ISR(&reason_spinlock);
  118. //Poke the other CPU.
  119. #if CONFIG_IDF_TARGET_ESP32
  120. if (core_id==0) {
  121. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  122. } else {
  123. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  124. }
  125. #elif CONFIG_IDF_TARGET_ESP32S2
  126. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  127. #elif CONFIG_IDF_TARGET_ESP32S3
  128. if (core_id==0) {
  129. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  130. } else {
  131. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1);
  132. }
  133. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  134. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  135. #endif
  136. }
  137. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  138. {
  139. esp_crosscore_int_send(core_id, REASON_YIELD);
  140. }
  141. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  142. {
  143. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  144. }
  145. void IRAM_ATTR esp_crosscore_int_send_gdb_call(int core_id)
  146. {
  147. esp_crosscore_int_send(core_id, REASON_GDB_CALL);
  148. }
  149. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 && !IDF_TARGET_ESP32C2
  150. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  151. {
  152. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  153. }
  154. void IRAM_ATTR esp_crosscore_int_send_twdt_abort(int core_id) {
  155. esp_crosscore_int_send(core_id, REASON_TWDT_ABORT);
  156. }
  157. #endif