crosscore_int.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2015-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 "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "esp_intr_alloc.h"
  18. #include "esp_debug_helpers.h"
  19. #include "soc/periph_defs.h"
  20. #include "soc/system_reg.h"
  21. #include "hal/cpu_hal.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/portmacro.h"
  24. #define REASON_YIELD BIT(0)
  25. #define REASON_FREQ_SWITCH BIT(1)
  26. #define REASON_PRINT_BACKTRACE BIT(2)
  27. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  28. static volatile uint32_t reason[portNUM_PROCESSORS];
  29. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
  30. {
  31. portYIELD_FROM_ISR();
  32. }
  33. static void IRAM_ATTR esp_crosscore_isr(void *arg)
  34. {
  35. uint32_t my_reason_val;
  36. //A pointer to the correct reason array item is passed to this ISR.
  37. volatile uint32_t *my_reason = arg;
  38. //Clear the interrupt first.
  39. if (cpu_hal_get_core_id() == 0) {
  40. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  41. } else {
  42. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, 0);
  43. }
  44. //Grab the reason and clear it.
  45. portENTER_CRITICAL_ISR(&reason_spinlock);
  46. my_reason_val = *my_reason;
  47. *my_reason = 0;
  48. portEXIT_CRITICAL_ISR(&reason_spinlock);
  49. //Check what we need to do.
  50. if (my_reason_val & REASON_YIELD) {
  51. esp_crosscore_isr_handle_yield();
  52. }
  53. if (my_reason_val & REASON_FREQ_SWITCH) {
  54. /* Nothing to do here; the frequency switch event was already
  55. * handled by a hook in xtensa_vectors.S. Could be used in the future
  56. * to allow DFS features without the extra latency of the ISR hook.
  57. */
  58. }
  59. if (my_reason_val & REASON_PRINT_BACKTRACE) {
  60. esp_backtrace_print(100);
  61. }
  62. }
  63. // Initialize the crosscore interrupt on this core.
  64. void esp_crosscore_int_init(void)
  65. {
  66. portENTER_CRITICAL(&reason_spinlock);
  67. reason[cpu_hal_get_core_id()] = 0;
  68. portEXIT_CRITICAL(&reason_spinlock);
  69. esp_err_t err;
  70. if (cpu_hal_get_core_id() == 0) {
  71. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void *)&reason[0], NULL);
  72. } else {
  73. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void *)&reason[1], NULL);
  74. }
  75. assert(err == ESP_OK);
  76. }
  77. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask)
  78. {
  79. assert(core_id < portNUM_PROCESSORS);
  80. //Mark the reason we interrupt the other CPU
  81. portENTER_CRITICAL(&reason_spinlock);
  82. reason[core_id] |= reason_mask;
  83. portEXIT_CRITICAL(&reason_spinlock);
  84. //Poke the other CPU.
  85. if (core_id == 0) {
  86. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  87. } else {
  88. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1);
  89. }
  90. }
  91. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  92. {
  93. esp_crosscore_int_send(core_id, REASON_YIELD);
  94. }
  95. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  96. {
  97. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  98. }
  99. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  100. {
  101. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  102. }