crosscore_int.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include "esp_attr.h"
  15. #include "esp_err.h"
  16. #include "esp_intr_alloc.h"
  17. #include "esp_debug_helpers.h"
  18. #include "soc/periph_defs.h"
  19. #include "hal/cpu_hal.h"
  20. #include "freertos/FreeRTOS.h"
  21. #include "freertos/portmacro.h"
  22. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  23. #include "soc/dport_reg.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2
  25. #include "soc/system_reg.h"
  26. #endif
  27. #define REASON_YIELD BIT(0)
  28. #define REASON_FREQ_SWITCH BIT(1)
  29. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  30. #define REASON_PRINT_BACKTRACE BIT(2)
  31. #endif
  32. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  33. static volatile uint32_t reason[portNUM_PROCESSORS];
  34. /*
  35. 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
  36. 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.
  37. */
  38. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
  39. {
  40. portYIELD_FROM_ISR();
  41. }
  42. static void IRAM_ATTR esp_crosscore_isr(void *arg) {
  43. uint32_t my_reason_val;
  44. //A pointer to the correct reason array item is passed to this ISR.
  45. volatile uint32_t *my_reason=arg;
  46. //Clear the interrupt first.
  47. #if CONFIG_IDF_TARGET_ESP32
  48. if (cpu_hal_get_core_id()==0) {
  49. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  50. } else {
  51. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
  52. }
  53. #elif CONFIG_IDF_TARGET_ESP32S2
  54. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  55. #elif CONFIG_IDF_TARGET_ESP32S3
  56. if (cpu_hal_get_core_id()==0) {
  57. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  58. } else {
  59. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, 0);
  60. }
  61. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  62. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  63. #endif
  64. //Grab the reason and clear it.
  65. portENTER_CRITICAL_ISR(&reason_spinlock);
  66. my_reason_val=*my_reason;
  67. *my_reason=0;
  68. portEXIT_CRITICAL_ISR(&reason_spinlock);
  69. //Check what we need to do.
  70. if (my_reason_val & REASON_YIELD) {
  71. esp_crosscore_isr_handle_yield();
  72. }
  73. if (my_reason_val & REASON_FREQ_SWITCH) {
  74. /* Nothing to do here; the frequency switch event was already
  75. * handled by a hook in xtensa_vectors.S. Could be used in the future
  76. * to allow DFS features without the extra latency of the ISR hook.
  77. */
  78. }
  79. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2 // IDF-2986
  80. if (my_reason_val & REASON_PRINT_BACKTRACE) {
  81. esp_backtrace_print(100);
  82. }
  83. #endif
  84. }
  85. //Initialize the crosscore interrupt on this core. Call this once
  86. //on each active core.
  87. void esp_crosscore_int_init(void) {
  88. portENTER_CRITICAL(&reason_spinlock);
  89. reason[cpu_hal_get_core_id()]=0;
  90. portEXIT_CRITICAL(&reason_spinlock);
  91. esp_err_t err __attribute__((unused)) = ESP_OK;
  92. #if portNUM_PROCESSORS > 1
  93. if (cpu_hal_get_core_id()==0) {
  94. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  95. } else {
  96. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
  97. }
  98. #else
  99. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  100. #endif
  101. ESP_ERROR_CHECK(err);
  102. }
  103. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
  104. assert(core_id<portNUM_PROCESSORS);
  105. //Mark the reason we interrupt the other CPU
  106. portENTER_CRITICAL_ISR(&reason_spinlock);
  107. reason[core_id] |= reason_mask;
  108. portEXIT_CRITICAL_ISR(&reason_spinlock);
  109. //Poke the other CPU.
  110. #if CONFIG_IDF_TARGET_ESP32
  111. if (core_id==0) {
  112. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  113. } else {
  114. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  115. }
  116. #elif CONFIG_IDF_TARGET_ESP32S2
  117. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  118. #elif CONFIG_IDF_TARGET_ESP32S3
  119. if (core_id==0) {
  120. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  121. } else {
  122. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1);
  123. }
  124. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  125. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  126. #endif
  127. }
  128. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  129. {
  130. esp_crosscore_int_send(core_id, REASON_YIELD);
  131. }
  132. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  133. {
  134. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  135. }
  136. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  137. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  138. {
  139. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  140. }
  141. #endif