| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-18 10:32:56 +08:00
										 |  |  |  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include "esp_attr.h"
 | 
					
						
							|  |  |  | #include "esp_err.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  | #include "esp_cpu.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-26 16:30:43 +08:00
										 |  |  | #include "esp_intr_alloc.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-27 17:58:12 +08:00
										 |  |  | #include "esp_debug_helpers.h"
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #include "soc/periph_defs.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | #include "freertos/FreeRTOS.h"
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #include "freertos/portmacro.h"
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 22:54:09 +03:00
										 |  |  | #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
 | 
					
						
							|  |  |  | #include "esp_gdbstub.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
 | 
					
						
							|  |  |  | #include "soc/dport_reg.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-12 20:42:28 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #include "soc/system_reg.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  | #define REASON_YIELD            BIT(0)
 | 
					
						
							|  |  |  | #define REASON_FREQ_SWITCH      BIT(1)
 | 
					
						
							| 
									
										
										
										
											2021-06-07 22:54:09 +03:00
										 |  |  | #define REASON_GDB_CALL         BIT(3)
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-13 17:06:40 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ARCH_XTENSA
 | 
					
						
							| 
									
										
										
										
											2020-03-27 17:58:12 +08:00
										 |  |  | #define REASON_PRINT_BACKTRACE  BIT(2)
 | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | #define REASON_TWDT_ABORT       BIT(4)
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  | static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED; | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | static volatile uint32_t reason[portNUM_PROCESSORS]; | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | 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. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void) | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     portYIELD_FROM_ISR(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 00:52:45 +08:00
										 |  |  | static void IRAM_ATTR esp_crosscore_isr(void *arg) { | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     uint32_t my_reason_val; | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //A pointer to the correct reason array item is passed to this ISR.
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     volatile uint32_t *my_reason=arg; | 
					
						
							| 
									
										
										
										
											2016-11-10 17:59:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //Clear the interrupt first.
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ESP32
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     if (esp_cpu_get_core_id()==0) { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ESP32S2
 | 
					
						
							|  |  |  |     DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0); | 
					
						
							|  |  |  | #elif CONFIG_IDF_TARGET_ESP32S3
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     if (esp_cpu_get_core_id()==0) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  |         WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, 0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-13 17:06:40 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ARCH_RISCV
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  |     WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //Grab the reason and clear it.
 | 
					
						
							| 
									
										
										
										
											2018-01-29 21:40:38 +08:00
										 |  |  |     portENTER_CRITICAL_ISR(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     my_reason_val=*my_reason; | 
					
						
							|  |  |  |     *my_reason=0; | 
					
						
							| 
									
										
										
										
											2018-01-29 21:40:38 +08:00
										 |  |  |     portEXIT_CRITICAL_ISR(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //Check what we need to do.
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     if (my_reason_val & REASON_YIELD) { | 
					
						
							|  |  |  |         esp_crosscore_isr_handle_yield(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (my_reason_val & REASON_FREQ_SWITCH) { | 
					
						
							|  |  |  |         /* Nothing to do here; the frequency switch event was already
 | 
					
						
							|  |  |  |          * handled by a hook in xtensa_vectors.S. Could be used in the future | 
					
						
							|  |  |  |          * to allow DFS features without the extra latency of the ISR hook. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-07 22:54:09 +03:00
										 |  |  | #if CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
 | 
					
						
							|  |  |  |     if (my_reason_val & REASON_GDB_CALL) { | 
					
						
							|  |  |  |         update_breakpoints(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif // !CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
 | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ARCH_XTENSA // IDF-2986
 | 
					
						
							| 
									
										
										
										
											2020-03-27 17:58:12 +08:00
										 |  |  |     if (my_reason_val & REASON_PRINT_BACKTRACE) { | 
					
						
							|  |  |  |         esp_backtrace_print(100); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 17:58:14 +08:00
										 |  |  | #if CONFIG_ESP_TASK_WDT_EN
 | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  |     if (my_reason_val & REASON_TWDT_ABORT) { | 
					
						
							|  |  |  |         extern void task_wdt_timeout_abort_xtensa(bool); | 
					
						
							|  |  |  |         /* Called from a crosscore interrupt, thus, we are not the core that received
 | 
					
						
							|  |  |  |          * the TWDT interrupt, call the function with `false` as a parameter. */ | 
					
						
							|  |  |  |         task_wdt_timeout_abort_xtensa(false); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-23 17:58:14 +08:00
										 |  |  | #endif // CONFIG_ESP_TASK_WDT_EN
 | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | #endif // CONFIG_IDF_TARGET_ARCH_XTENSA
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //Initialize the crosscore interrupt on this core. Call this once
 | 
					
						
							|  |  |  | //on each active core.
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void esp_crosscore_int_init(void) { | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     portENTER_CRITICAL(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     reason[esp_cpu_get_core_id()]=0; | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     portEXIT_CRITICAL(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  |     esp_err_t err __attribute__((unused)) = ESP_OK; | 
					
						
							|  |  |  | #if portNUM_PROCESSORS > 1
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:24:42 +08:00
										 |  |  |     if (esp_cpu_get_core_id()==0) { | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |         err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |         err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(err); | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  | static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) { | 
					
						
							|  |  |  |     assert(core_id<portNUM_PROCESSORS); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //Mark the reason we interrupt the other CPU
 | 
					
						
							| 
									
										
										
										
											2019-03-25 16:09:21 +05:30
										 |  |  |     portENTER_CRITICAL_ISR(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     reason[core_id] |= reason_mask; | 
					
						
							| 
									
										
										
										
											2019-03-25 16:09:21 +05:30
										 |  |  |     portEXIT_CRITICAL_ISR(&reason_spinlock); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     //Poke the other CPU.
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ESP32
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  |     if (core_id==0) { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1); | 
					
						
							| 
									
										
										
										
											2016-10-27 16:07:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ESP32S2
 | 
					
						
							|  |  |  |     DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0); | 
					
						
							|  |  |  | #elif CONFIG_IDF_TARGET_ESP32S3
 | 
					
						
							|  |  |  |     if (core_id==0) { | 
					
						
							|  |  |  |         WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_1_REG, SYSTEM_CPU_INTR_FROM_CPU_1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-13 17:06:40 +08:00
										 |  |  | #elif CONFIG_IDF_TARGET_ARCH_RISCV
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  |     WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-10-26 21:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 22:29:08 +08:00
										 |  |  | void IRAM_ATTR esp_crosscore_int_send_yield(int core_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_crosscore_int_send(core_id, REASON_YIELD); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 22:54:09 +03:00
										 |  |  | void IRAM_ATTR esp_crosscore_int_send_gdb_call(int core_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_crosscore_int_send(core_id, REASON_GDB_CALL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-13 17:06:40 +08:00
										 |  |  | #if CONFIG_IDF_TARGET_ARCH_XTENSA
 | 
					
						
							| 
									
										
										
										
											2020-03-27 17:58:12 +08:00
										 |  |  | void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 17:58:14 +08:00
										 |  |  | #if CONFIG_ESP_TASK_WDT_EN
 | 
					
						
							| 
									
										
										
										
											2022-07-13 17:27:37 +08:00
										 |  |  | void IRAM_ATTR esp_crosscore_int_send_twdt_abort(int core_id) { | 
					
						
							|  |  |  |     esp_crosscore_int_send(core_id, REASON_TWDT_ABORT); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-23 17:58:14 +08:00
										 |  |  | #endif // CONFIG_ESP_TASK_WDT_EN
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:06:41 +08:00
										 |  |  | #endif
 |