cache_hal.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include <stdint.h>
  8. #include "sdkconfig.h"
  9. #include "esp_err.h"
  10. #include "esp_attr.h"
  11. #include "hal/assert.h"
  12. #include "hal/cache_hal.h"
  13. #include "hal/cache_types.h"
  14. #include "hal/cache_ll.h"
  15. #include "hal/mmu_hal.h"
  16. #include "hal/mmu_ll.h"
  17. #include "soc/soc_caps.h"
  18. #include "rom/cache.h"
  19. /*------------------------------------------------------------------------------
  20. * Unified Cache Control
  21. * See cache_hal.h for more info about these HAL APIs
  22. * This file is in internal RAM.
  23. * Now this file doesn't compile on ESP32
  24. *----------------------------------------------------------------------------*/
  25. /**
  26. * To know if autoload is enabled or not.
  27. *
  28. * We should have a unified flag for this aim, then we don't need to call following 2 functions
  29. * to know the flag.
  30. *
  31. * Suggest ROM keeping this flag value to BIT(2). Then we can replace following lines to:
  32. * #define DATA_AUTOLOAD_FLAG BIT(2)
  33. * #define INST_AUTOLOAD_FLAG BIT(2)
  34. */
  35. #if CONFIG_IDF_TARGET_ESP32P4 //TODO: IDF-7516
  36. #define DATA_AUTOLOAD_ENABLE Cache_Disable_L2_Cache()
  37. #define INST_AUTOLOAD_ENABLE Cache_Disable_L2_Cache()
  38. #else
  39. #define DATA_AUTOLOAD_ENABLE cache_ll_is_cache_autoload_enabled(CACHE_TYPE_DATA)
  40. #define INST_AUTOLOAD_ENABLE cache_ll_is_cache_autoload_enabled(CACHE_TYPE_INSTRUCTION)
  41. #endif
  42. /**
  43. * Necessary hal contexts, could be maintained by upper layer in the future
  44. */
  45. typedef struct {
  46. bool data_autoload_en;
  47. bool inst_autoload_en;
  48. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  49. // There's no register indicating if cache is enabled on these chips, use sw flag to save this state.
  50. volatile bool cache_enabled;
  51. #endif
  52. } cache_hal_context_t;
  53. static cache_hal_context_t ctx;
  54. void cache_hal_init(void)
  55. {
  56. ctx.data_autoload_en = DATA_AUTOLOAD_ENABLE;
  57. ctx.inst_autoload_en = INST_AUTOLOAD_ENABLE;
  58. #if SOC_CACHE_L2_SUPPORTED
  59. Cache_Enable_L2_Cache(ctx.inst_autoload_en);
  60. #else
  61. cache_ll_enable_cache(CACHE_TYPE_ALL, ctx.inst_autoload_en, ctx.data_autoload_en);
  62. #endif //SOC_CACHE_L2_SUPPORTED
  63. cache_ll_l1_enable_bus(0, CACHE_LL_DEFAULT_DBUS_MASK);
  64. cache_ll_l1_enable_bus(0, CACHE_LL_DEFAULT_IBUS_MASK);
  65. #if !CONFIG_FREERTOS_UNICORE
  66. cache_ll_l1_enable_bus(1, CACHE_LL_DEFAULT_DBUS_MASK);
  67. cache_ll_l1_enable_bus(1, CACHE_LL_DEFAULT_IBUS_MASK);
  68. #endif
  69. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  70. ctx.cache_enabled = 1;
  71. #endif
  72. }
  73. void cache_hal_disable(cache_type_t type)
  74. {
  75. #if SOC_CACHE_L2_SUPPORTED
  76. Cache_Disable_L2_Cache();
  77. #else
  78. cache_ll_disable_cache(type);
  79. #endif //SOC_CACHE_L2_SUPPORTED
  80. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  81. ctx.cache_enabled = 0;
  82. #endif
  83. }
  84. void cache_hal_enable(cache_type_t type)
  85. {
  86. #if SOC_CACHE_L2_SUPPORTED
  87. Cache_Enable_L2_Cache(ctx.inst_autoload_en);
  88. #else
  89. cache_ll_enable_cache(type, ctx.inst_autoload_en, ctx.data_autoload_en);
  90. #endif //SOC_CACHE_L2_SUPPORTED
  91. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  92. ctx.cache_enabled = 1;
  93. #endif
  94. }
  95. void cache_hal_suspend(cache_type_t type)
  96. {
  97. #if SOC_CACHE_L2_SUPPORTED
  98. Cache_Suspend_L2_Cache();
  99. #else
  100. cache_ll_suspend_cache(type);
  101. #endif //SOC_CACHE_L2_SUPPORTED
  102. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  103. ctx.cache_enabled = 0;
  104. #endif
  105. }
  106. void cache_hal_resume(cache_type_t type)
  107. {
  108. #if SOC_CACHE_L2_SUPPORTED
  109. Cache_Resume_L2_Cache(ctx.inst_autoload_en);
  110. #else
  111. cache_ll_resume_cache(type, ctx.inst_autoload_en, ctx.data_autoload_en);
  112. #endif
  113. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  114. ctx.cache_enabled = 1;
  115. #endif
  116. }
  117. bool cache_hal_is_cache_enabled(cache_type_t type)
  118. {
  119. bool enabled;
  120. #if CACHE_LL_ENABLE_DISABLE_STATE_SW
  121. enabled = ctx.cache_enabled;
  122. #else
  123. enabled = cache_ll_is_cache_enabled(type);
  124. #endif //CACHE_LL_ENABLE_DISABLE_STATE_SW
  125. return enabled;
  126. }
  127. void cache_hal_invalidate_addr(uint32_t vaddr, uint32_t size)
  128. {
  129. //Now only esp32 has 2 MMUs, this file doesn't build on esp32
  130. HAL_ASSERT(mmu_hal_check_valid_ext_vaddr_region(0, vaddr, size, MMU_VADDR_DATA | MMU_VADDR_INSTRUCTION));
  131. #if CONFIG_IDF_TARGET_ESP32P4
  132. Cache_Invalidate_Addr(CACHE_MAP_L1_DCACHE | CACHE_MAP_L2_CACHE, vaddr, size);
  133. #else
  134. cache_ll_invalidate_addr(vaddr, size);
  135. #endif
  136. }
  137. #if SOC_CACHE_WRITEBACK_SUPPORTED
  138. void cache_hal_writeback_addr(uint32_t vaddr, uint32_t size)
  139. {
  140. HAL_ASSERT(mmu_hal_check_valid_ext_vaddr_region(0, vaddr, size, MMU_VADDR_DATA));
  141. #if CONFIG_IDF_TARGET_ESP32P4
  142. Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE, vaddr, size);
  143. Cache_WriteBack_Addr(CACHE_MAP_L2_CACHE, vaddr, size);
  144. #else
  145. cache_ll_writeback_addr(vaddr, size);
  146. #endif
  147. }
  148. #endif //#if SOC_CACHE_WRITEBACK_SUPPORTED
  149. #if SOC_CACHE_FREEZE_SUPPORTED
  150. void cache_hal_freeze(cache_type_t type)
  151. {
  152. #if SOC_CACHE_L2_SUPPORTED
  153. Cache_Freeze_L2_Cache_Enable(CACHE_FREEZE_ACK_BUSY);
  154. #else
  155. cache_ll_freeze_cache(type);
  156. #endif //SOC_CACHE_L2_SUPPORTED
  157. }
  158. void cache_hal_unfreeze(cache_type_t type)
  159. {
  160. #if SOC_CACHE_L2_SUPPORTED
  161. Cache_Freeze_L2_Cache_Disable();
  162. #else
  163. cache_ll_unfreeze_cache(type);
  164. #endif //SOC_CACHE_L2_SUPPORTED
  165. }
  166. #endif //#if SOC_CACHE_FREEZE_SUPPORTED
  167. uint32_t cache_hal_get_cache_line_size(cache_type_t type)
  168. {
  169. uint32_t line_size = 0;
  170. #if SOC_CACHE_L2_SUPPORTED
  171. line_size = Cache_Get_L2_Cache_Line_Size();
  172. #else
  173. line_size = cache_ll_get_line_size(type);
  174. #endif //SOC_CACHE_L2_SUPPORTED
  175. return line_size;
  176. }