cache_err_int.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /*
  15. The cache has an interrupt that can be raised as soon as an access to a cached
  16. region (flash) is done without the cache being enabled. We use that here
  17. to panic the CPU, which from a debugging perspective is better than grabbing bad
  18. data from the bus.
  19. */
  20. #include "esp32c3/rom/ets_sys.h"
  21. #include "esp_attr.h"
  22. #include "esp_intr_alloc.h"
  23. #include "soc/extmem_reg.h"
  24. #include "soc/periph_defs.h"
  25. #include "riscv/interrupt.h"
  26. void esp_cache_err_int_init(void)
  27. {
  28. const uint32_t core_id = 0;
  29. /* Disable cache interrupts if enabled. */
  30. ESP_INTR_DISABLE(ETS_CACHEERR_INUM);
  31. /**
  32. * Bind all cache errors to ETS_CACHEERR_INUM interrupt. we will deal with
  33. * them in handler by different types
  34. * I) Cache access error
  35. * 1. dbus trying to write to icache
  36. * 2. dbus authentication fail
  37. * 3. cpu access icache while dbus is disabled [1]
  38. * 4. ibus authentication fail
  39. * 5. ibus trying to write icache
  40. * 6. cpu access icache while ibus is disabled
  41. * II) Cache illegal error
  42. * 1. dbus counter overflow
  43. * 2. ibus counter overflow
  44. * 3. mmu entry fault
  45. * 4. icache preload configurations fault
  46. * 5. icache sync configuration fault
  47. *
  48. * [1]: On ESP32C3 boards, the caches are shared but buses are still
  49. * distinct. So, we have an ibus and a dbus sharing the same cache.
  50. * This error can occur if the dbus performs a request but the icache
  51. * (or simply cache) is disabled.
  52. */
  53. intr_matrix_set(core_id, ETS_CACHE_IA_INTR_SOURCE, ETS_CACHEERR_INUM);
  54. intr_matrix_set(core_id, ETS_CACHE_CORE0_ACS_INTR_SOURCE, ETS_CACHEERR_INUM);
  55. /* Set the type and priority to cache error interrupts. */
  56. esprv_intc_int_set_type(BIT(ETS_CACHEERR_INUM), INTR_TYPE_LEVEL);
  57. esprv_intc_int_set_priority(ETS_CACHEERR_INUM, SOC_INTERRUPT_LEVEL_MEDIUM);
  58. /* On the hardware side, stat by clearing all the bits reponsible for
  59. * enabling cache access error interrupts. */
  60. SET_PERI_REG_MASK(EXTMEM_CORE0_ACS_CACHE_INT_CLR_REG,
  61. EXTMEM_CORE0_DBUS_WR_IC_INT_CLR |
  62. EXTMEM_CORE0_DBUS_REJECT_INT_CLR |
  63. EXTMEM_CORE0_DBUS_ACS_MSK_IC_INT_CLR |
  64. EXTMEM_CORE0_IBUS_REJECT_INT_CLR |
  65. EXTMEM_CORE0_IBUS_WR_IC_INT_CLR |
  66. EXTMEM_CORE0_IBUS_ACS_MSK_IC_INT_CLR);
  67. /* Enable these interrupts. */
  68. SET_PERI_REG_MASK(EXTMEM_CORE0_ACS_CACHE_INT_ENA_REG,
  69. EXTMEM_CORE0_DBUS_WR_IC_INT_ENA |
  70. EXTMEM_CORE0_DBUS_REJECT_INT_ENA |
  71. EXTMEM_CORE0_DBUS_ACS_MSK_IC_INT_ENA |
  72. EXTMEM_CORE0_IBUS_REJECT_INT_ENA |
  73. EXTMEM_CORE0_IBUS_WR_IC_INT_ENA |
  74. EXTMEM_CORE0_IBUS_ACS_MSK_IC_INT_ENA);
  75. /* Same goes for cache illegal error: start by clearing the bits and then
  76. * set them back. */
  77. SET_PERI_REG_MASK(EXTMEM_CACHE_ILG_INT_CLR_REG,
  78. EXTMEM_MMU_ENTRY_FAULT_INT_CLR |
  79. EXTMEM_ICACHE_PRELOAD_OP_FAULT_INT_CLR |
  80. EXTMEM_ICACHE_SYNC_OP_FAULT_INT_CLR);
  81. SET_PERI_REG_MASK(EXTMEM_CACHE_ILG_INT_ENA_REG,
  82. EXTMEM_MMU_ENTRY_FAULT_INT_ENA |
  83. EXTMEM_ICACHE_PRELOAD_OP_FAULT_INT_ENA |
  84. EXTMEM_ICACHE_SYNC_OP_FAULT_INT_ENA);
  85. /* Enable the interrupts for cache error. */
  86. ESP_INTR_ENABLE(ETS_CACHEERR_INUM);
  87. }
  88. int IRAM_ATTR esp_cache_err_get_cpuid(void)
  89. {
  90. return 0;
  91. }