cache_hal.c 3.4 KB

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