cache_hal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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. #if CONFIG_IDF_TARGET_ESP32S2
  19. #include "esp32s2/rom/cache.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S3
  21. #include "esp32s3/rom/cache.h"
  22. #elif CONFIG_IDF_TARGET_ESP32C3
  23. #include "esp32c3/rom/cache.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C2
  25. #include "esp32c2/rom/cache.h"
  26. #elif CONFIG_IDF_TARGET_ESP32H4
  27. #include "esp32h4/rom/cache.h"
  28. #elif CONFIG_IDF_TARGET_ESP32C6
  29. #include "esp32c6/rom/cache.h"
  30. #elif CONFIG_IDF_TARGET_ESP32H2
  31. #include "esp32h2/rom/cache.h"
  32. #endif
  33. /*------------------------------------------------------------------------------
  34. * Unified Cache Control
  35. * See cache_hal.h for more info about these HAL APIs
  36. * This file is in internal RAM.
  37. * Now this file doesn't compile on ESP32
  38. *----------------------------------------------------------------------------*/
  39. /**
  40. * To know if autoload is enabled or not.
  41. *
  42. * We should have a unified flag for this aim, then we don't need to call following 2 functions
  43. * to know the flag.
  44. *
  45. * Suggest ROM keeping this flag value to BIT(2). Then we can replace following lines to:
  46. * #define DATA_AUTOLOAD_FLAG BIT(2)
  47. * #define INST_AUTOLOAD_FLAG BIT(2)
  48. */
  49. #define DATA_AUTOLOAD_FLAG Cache_Disable_DCache()
  50. #define INST_AUTOLOAD_FLAG Cache_Disable_ICache()
  51. /**
  52. * Necessary hal contexts, could be maintained by upper layer in the future
  53. */
  54. typedef struct {
  55. uint32_t data_autoload_flag;
  56. uint32_t inst_autoload_flag;
  57. } cache_hal_context_t;
  58. static cache_hal_context_t ctx;
  59. void cache_hal_init(void)
  60. {
  61. #if SOC_SHARED_IDCACHE_SUPPORTED
  62. ctx.data_autoload_flag = INST_AUTOLOAD_FLAG;
  63. Cache_Enable_ICache(ctx.data_autoload_flag);
  64. #else
  65. ctx.data_autoload_flag = DATA_AUTOLOAD_FLAG;
  66. Cache_Enable_DCache(ctx.data_autoload_flag);
  67. ctx.inst_autoload_flag = INST_AUTOLOAD_FLAG;
  68. Cache_Enable_ICache(ctx.inst_autoload_flag);
  69. #endif
  70. cache_ll_l1_enable_bus(0, CACHE_LL_DEFAULT_DBUS_MASK);
  71. cache_ll_l1_enable_bus(0, CACHE_LL_DEFAULT_IBUS_MASK);
  72. #if !CONFIG_FREERTOS_UNICORE
  73. cache_ll_l1_enable_bus(1, CACHE_LL_DEFAULT_DBUS_MASK);
  74. cache_ll_l1_enable_bus(1, CACHE_LL_DEFAULT_IBUS_MASK);
  75. #endif
  76. }
  77. void cache_hal_disable(cache_type_t type)
  78. {
  79. #if SOC_SHARED_IDCACHE_SUPPORTED
  80. Cache_Disable_ICache();
  81. #else
  82. if (type == CACHE_TYPE_DATA) {
  83. Cache_Disable_DCache();
  84. } else if (type == CACHE_TYPE_INSTRUCTION) {
  85. Cache_Disable_ICache();
  86. } else {
  87. Cache_Disable_ICache();
  88. Cache_Disable_DCache();
  89. }
  90. #endif
  91. }
  92. void cache_hal_enable(cache_type_t type)
  93. {
  94. #if SOC_SHARED_IDCACHE_SUPPORTED
  95. Cache_Enable_ICache(ctx.inst_autoload_flag);
  96. #else
  97. if (type == CACHE_TYPE_DATA) {
  98. Cache_Enable_DCache(ctx.data_autoload_flag);
  99. } else if (type == CACHE_TYPE_INSTRUCTION) {
  100. Cache_Enable_ICache(ctx.inst_autoload_flag);
  101. } else {
  102. Cache_Enable_ICache(ctx.inst_autoload_flag);
  103. Cache_Enable_DCache(ctx.data_autoload_flag);
  104. }
  105. #endif
  106. }
  107. void cache_hal_invalidate_addr(uint32_t vaddr, uint32_t size)
  108. {
  109. //Now only esp32 has 2 MMUs, this file doesn't build on esp32
  110. HAL_ASSERT(mmu_hal_check_valid_ext_vaddr_region(0, vaddr, size, MMU_VADDR_DATA | MMU_VADDR_INSTRUCTION));
  111. Cache_Invalidate_Addr(vaddr, size);
  112. }