crosscore_int.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifdef CONFIG_ESP_TASK_WDT
  87. if (my_reason_val & REASON_TWDT_ABORT) {
  88. extern void task_wdt_timeout_abort_xtensa(bool);
  89. /* Called from a crosscore interrupt, thus, we are not the core that received
  90. * the TWDT interrupt, call the function with `false` as a parameter. */
  91. task_wdt_timeout_abort_xtensa(false);
  92. }
  93. #endif
  94. #endif // CONFIG_IDF_TARGET_ARCH_XTENSA
  95. }
  96. //Initialize the crosscore interrupt on this core. Call this once
  97. //on each active core.
  98. void esp_crosscore_int_init(void) {
  99. portENTER_CRITICAL(&reason_spinlock);
  100. reason[esp_cpu_get_core_id()]=0;
  101. portEXIT_CRITICAL(&reason_spinlock);
  102. esp_err_t err __attribute__((unused)) = ESP_OK;
  103. #if portNUM_PROCESSORS > 1
  104. if (esp_cpu_get_core_id()==0) {
  105. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  106. } else {
  107. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
  108. }
  109. #else
  110. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  111. #endif
  112. ESP_ERROR_CHECK(err);
  113. }
  114. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
  115. assert(core_id<portNUM_PROCESSORS);
  116. //Mark the reason we interrupt the other CPU
  117. portENTER_CRITICAL_ISR(&reason_spinlock);
  118. reason[core_id] |= reason_mask;
  119. portEXIT_CRITICAL_ISR(&reason_spinlock);
  120. //Poke the other CPU.
  121. #if CONFIG_IDF_TARGET_ESP32
  122. if (core_id==0) {
  123. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  124. } else {
  125. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  126. }
  127. #elif CONFIG_IDF_TARGET_ESP32S2
  128. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  129. #elif CONFIG_IDF_TARGET_ESP32S3
  130. if (core_id==0) {
  131. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  132. } else {
  133. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1);
  134. }
  135. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  136. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  137. #endif
  138. }
  139. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  140. {
  141. esp_crosscore_int_send(core_id, REASON_YIELD);
  142. }
  143. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  144. {
  145. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  146. }
  147. void IRAM_ATTR esp_crosscore_int_send_gdb_call(int core_id)
  148. {
  149. esp_crosscore_int_send(core_id, REASON_GDB_CALL);
  150. }
  151. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 && !IDF_TARGET_ESP32C2
  152. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  153. {
  154. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  155. }
  156. #ifdef CONFIG_ESP_TASK_WDT
  157. void IRAM_ATTR esp_crosscore_int_send_twdt_abort(int core_id) {
  158. esp_crosscore_int_send(core_id, REASON_TWDT_ABORT);
  159. }
  160. #endif
  161. #endif