| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |  The cache has an interrupt that can be raised as soon as an access to a cached | 
					
						
							|  |  |  |  region (flash, psram) is done without the cache being enabled. We use that here | 
					
						
							|  |  |  |  to panic the CPU, which from a debugging perspective is better than grabbing bad | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  |  data from the bus. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-16 18:20:58 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | #include "esp_err.h"
 | 
					
						
							|  |  |  | #include "esp_attr.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-16 18:20:58 +05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "esp_intr_alloc.h"
 | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | #include "soc/dport_reg.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-16 18:20:58 +05:00
										 |  |  | #include "hal/cpu_hal.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "esp32/dport_access.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-21 13:07:34 +08:00
										 |  |  | #include "esp32/rom/ets_sys.h" // for intr_matrix_set
 | 
					
						
							| 
									
										
										
										
											2020-02-16 18:20:58 +05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | #include "sdkconfig.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | void esp_cache_err_int_init(void) | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 18:20:58 +05:00
										 |  |  |     uint32_t core_id = cpu_hal_get_core_id(); | 
					
						
							| 
									
										
										
										
											2020-03-10 16:46:10 +01:00
										 |  |  |     ESP_INTR_DISABLE(ETS_MEMACCESS_ERR_INUM); | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // We do not register a handler for the interrupt because it is interrupt
 | 
					
						
							|  |  |  |     // level 4 which is not serviceable from C. Instead, xtensa_vectors.S has
 | 
					
						
							|  |  |  |     // a call to the panic handler for
 | 
					
						
							|  |  |  |     // this interrupt.
 | 
					
						
							| 
									
										
										
										
											2020-03-10 16:46:10 +01:00
										 |  |  |     intr_matrix_set(core_id, ETS_CACHE_IA_INTR_SOURCE, ETS_MEMACCESS_ERR_INUM); | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Enable invalid cache access interrupt when the cache is disabled.
 | 
					
						
							|  |  |  |     // When the interrupt happens, we can not determine the CPU where the
 | 
					
						
							|  |  |  |     // invalid cache access has occurred. We enable the interrupt to catch
 | 
					
						
							|  |  |  |     // invalid access on both CPUs, but the interrupt is connected to the
 | 
					
						
							|  |  |  |     // CPU which happens to call this function.
 | 
					
						
							|  |  |  |     // For this reason, panic handler backtrace will not be correct if the
 | 
					
						
							|  |  |  |     // interrupt is connected to PRO CPU and invalid access happens on the APP
 | 
					
						
							|  |  |  |     // CPU.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (core_id == PRO_CPU_NUM) { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_SET_PERI_REG_MASK(DPORT_CACHE_IA_INT_EN_REG, | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  |             DPORT_CACHE_IA_INT_PRO_OPPOSITE | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_PRO_DRAM1 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_PRO_DROM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_PRO_IROM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_PRO_IRAM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_PRO_IRAM1); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |         DPORT_SET_PERI_REG_MASK(DPORT_CACHE_IA_INT_EN_REG, | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  |             DPORT_CACHE_IA_INT_APP_OPPOSITE | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_APP_DRAM1 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_APP_DROM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_APP_IROM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_APP_IRAM0 | | 
					
						
							|  |  |  |             DPORT_CACHE_IA_INT_APP_IRAM1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-10 16:46:10 +01:00
										 |  |  |     ESP_INTR_ENABLE(ETS_MEMACCESS_ERR_INUM); | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | int IRAM_ATTR esp_cache_err_get_cpuid(void) | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     const uint32_t pro_mask = | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_DRAM1 | | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_DROM0 | | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_IROM0 | | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_IRAM0 | | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_IRAM1 | | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_OPPOSITE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |     if (DPORT_GET_PERI_REG_MASK(DPORT_PRO_DCACHE_DBUG3_REG, pro_mask)) { | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  |         return PRO_CPU_NUM; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uint32_t app_mask = | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_DRAM1 | | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_DROM0 | | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_IROM0 | | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_IRAM0 | | 
					
						
							|  |  |  |             DPORT_APP_CPU_DISABLED_CACHE_IA_IRAM1 | | 
					
						
							|  |  |  |             DPORT_PRO_CPU_DISABLED_CACHE_IA_OPPOSITE; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 20:03:04 +08:00
										 |  |  |     if (DPORT_GET_PERI_REG_MASK(DPORT_APP_DCACHE_DBUG3_REG, app_mask)) { | 
					
						
							| 
									
										
										
										
											2017-03-09 20:50:39 +08:00
										 |  |  |         return APP_CPU_NUM; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } |