cache_hal.c 3.2 KB

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