cache_hal.c 3.1 KB

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