cache_utils.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <freertos/FreeRTOS.h>
  11. #include <freertos/task.h>
  12. #include <freertos/semphr.h>
  13. #if CONFIG_IDF_TARGET_ESP32
  14. #include "soc/dport_reg.h"
  15. #include <esp32/rom/spi_flash.h>
  16. #include <esp32/rom/cache.h>
  17. #elif CONFIG_IDF_TARGET_ESP32S2
  18. #include "esp32s2/rom/spi_flash.h"
  19. #include "esp32s2/rom/cache.h"
  20. #include "soc/extmem_reg.h"
  21. #include "soc/cache_memory.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/rom/spi_flash.h"
  24. #include "esp32s3/rom/cache.h"
  25. #include "soc/extmem_reg.h"
  26. #include "soc/cache_memory.h"
  27. #elif CONFIG_IDF_TARGET_ESP32C3
  28. #include "esp32c3/rom/spi_flash.h"
  29. #include "esp32c3/rom/cache.h"
  30. #include "soc/extmem_reg.h"
  31. #include "soc/cache_memory.h"
  32. #elif CONFIG_IDF_TARGET_ESP32H2
  33. #include "esp32h2/rom/spi_flash.h"
  34. #include "esp32h2/rom/cache.h"
  35. #include "soc/extmem_reg.h"
  36. #include "soc/cache_memory.h"
  37. #endif
  38. #include <soc/soc.h>
  39. #include "sdkconfig.h"
  40. #ifndef CONFIG_FREERTOS_UNICORE
  41. #include "esp_ipc.h"
  42. #endif
  43. #include "esp_attr.h"
  44. #include "esp_intr_alloc.h"
  45. #include "esp_spi_flash.h"
  46. #include "esp_log.h"
  47. static __attribute__((unused)) const char *TAG = "cache";
  48. #define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid
  49. #define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  50. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \
  51. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) )
  52. #define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  53. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \
  54. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0)))
  55. #define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP)
  56. #define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP)
  57. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
  58. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  59. static uint32_t s_flash_op_cache_state[2];
  60. #ifndef CONFIG_FREERTOS_UNICORE
  61. static SemaphoreHandle_t s_flash_op_mutex;
  62. static volatile bool s_flash_op_can_start = false;
  63. static volatile bool s_flash_op_complete = false;
  64. #ifndef NDEBUG
  65. static volatile int s_flash_op_cpu = -1;
  66. #endif
  67. static inline bool esp_task_stack_is_sane_cache_disabled(void)
  68. {
  69. const void *sp = (const void *)esp_cpu_get_sp();
  70. return esp_ptr_in_dram(sp)
  71. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  72. || esp_ptr_in_rtc_dram_fast(sp)
  73. #endif
  74. ;
  75. }
  76. void spi_flash_init_lock(void)
  77. {
  78. s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
  79. assert(s_flash_op_mutex != NULL);
  80. }
  81. void spi_flash_op_lock(void)
  82. {
  83. xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
  84. }
  85. void spi_flash_op_unlock(void)
  86. {
  87. xSemaphoreGiveRecursive(s_flash_op_mutex);
  88. }
  89. /*
  90. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  91. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  92. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  93. when accessing psram from the former CPU.
  94. */
  95. void IRAM_ATTR spi_flash_op_block_func(void *arg)
  96. {
  97. // Disable scheduler on this CPU
  98. vTaskSuspendAll();
  99. // Restore interrupts that aren't located in IRAM
  100. esp_intr_noniram_disable();
  101. uint32_t cpuid = (uint32_t) arg;
  102. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  103. // CPU may reset the flag back to false before IPC task has a chance to check it
  104. // (if it is preempted by an ISR taking non-trivial amount of time)
  105. s_flash_op_complete = false;
  106. s_flash_op_can_start = true;
  107. while (!s_flash_op_complete) {
  108. // busy loop here and wait for the other CPU to finish flash operation
  109. }
  110. // Flash operation is complete, re-enable cache
  111. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  112. // Restore interrupts that aren't located in IRAM
  113. esp_intr_noniram_enable();
  114. // Re-enable scheduler
  115. xTaskResumeAll();
  116. }
  117. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  118. {
  119. assert(esp_task_stack_is_sane_cache_disabled());
  120. spi_flash_op_lock();
  121. const int cpuid = xPortGetCoreID();
  122. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  123. #ifndef NDEBUG
  124. // For sanity check later: record the CPU which has started doing flash operation
  125. assert(s_flash_op_cpu == -1);
  126. s_flash_op_cpu = cpuid;
  127. #endif
  128. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  129. // Scheduler hasn't been started yet, it means that spi_flash API is being
  130. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  131. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  132. // which is in IRAM. So it is safe to disable cache for the other_cpuid after
  133. // esp_intr_noniram_disable.
  134. assert(other_cpuid == 1);
  135. } else {
  136. // Temporarily raise current task priority to prevent a deadlock while
  137. // waiting for IPC task to start on the other CPU
  138. int old_prio = uxTaskPriorityGet(NULL);
  139. vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1);
  140. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  141. // disable cache there and block other tasks from executing.
  142. s_flash_op_can_start = false;
  143. ESP_ERROR_CHECK(esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void *) other_cpuid));
  144. while (!s_flash_op_can_start) {
  145. // Busy loop and wait for spi_flash_op_block_func to disable cache
  146. // on the other CPU
  147. }
  148. // Disable scheduler on the current CPU
  149. vTaskSuspendAll();
  150. // Can now set the priority back to the normal one
  151. vTaskPrioritySet(NULL, old_prio);
  152. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  153. // occupied by highest priority task
  154. assert(xPortGetCoreID() == cpuid);
  155. }
  156. // Kill interrupts that aren't located in IRAM
  157. esp_intr_noniram_disable();
  158. // This CPU executes this routine, with non-IRAM interrupts and the scheduler
  159. // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
  160. // with non-iram interrupts and the scheduler disabled. None of these CPUs will
  161. // touch external RAM or flash this way, so we can safely disable caches.
  162. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  163. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  164. }
  165. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  166. {
  167. const int cpuid = xPortGetCoreID();
  168. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  169. #ifndef NDEBUG
  170. // Sanity check: flash operation ends on the same CPU as it has started
  171. assert(cpuid == s_flash_op_cpu);
  172. // More sanity check: if scheduler isn't started, only CPU0 can call this.
  173. assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
  174. s_flash_op_cpu = -1;
  175. #endif
  176. // Re-enable cache on both CPUs. After this, cache (flash and external RAM) should work again.
  177. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  178. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  179. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  180. // Signal to spi_flash_op_block_task that flash operation is complete
  181. s_flash_op_complete = true;
  182. }
  183. // Re-enable non-iram interrupts
  184. esp_intr_noniram_enable();
  185. // Resume tasks on the current CPU, if the scheduler has started.
  186. // NOTE: enabling non-IRAM interrupts has to happen before this,
  187. // because once the scheduler has started, due to preemption the
  188. // current task can end up being moved to the other CPU.
  189. // But esp_intr_noniram_enable has to be called on the same CPU which
  190. // called esp_intr_noniram_disable
  191. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  192. xTaskResumeAll();
  193. }
  194. // Release API lock
  195. spi_flash_op_unlock();
  196. }
  197. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  198. {
  199. const uint32_t cpuid = xPortGetCoreID();
  200. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  201. // do not care about other CPU, it was halted upon entering panic handler
  202. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  203. // Kill interrupts that aren't located in IRAM
  204. esp_intr_noniram_disable();
  205. // Disable cache on this CPU as well
  206. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  207. }
  208. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  209. {
  210. const uint32_t cpuid = xPortGetCoreID();
  211. // Re-enable cache on this CPU
  212. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  213. // Re-enable non-iram interrupts
  214. esp_intr_noniram_enable();
  215. }
  216. #else // CONFIG_FREERTOS_UNICORE
  217. void spi_flash_init_lock(void)
  218. {
  219. }
  220. void spi_flash_op_lock(void)
  221. {
  222. vTaskSuspendAll();
  223. }
  224. void spi_flash_op_unlock(void)
  225. {
  226. xTaskResumeAll();
  227. }
  228. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  229. {
  230. spi_flash_op_lock();
  231. esp_intr_noniram_disable();
  232. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  233. }
  234. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  235. {
  236. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  237. esp_intr_noniram_enable();
  238. spi_flash_op_unlock();
  239. }
  240. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  241. {
  242. // Kill interrupts that aren't located in IRAM
  243. esp_intr_noniram_disable();
  244. // Disable cache on this CPU as well
  245. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  246. }
  247. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  248. {
  249. // Re-enable cache on this CPU
  250. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  251. // Re-enable non-iram interrupts
  252. esp_intr_noniram_enable();
  253. }
  254. #endif // CONFIG_FREERTOS_UNICORE
  255. /**
  256. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  257. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  258. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  259. */
  260. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
  261. {
  262. #if CONFIG_IDF_TARGET_ESP32
  263. uint32_t ret = 0;
  264. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  265. if (cpuid == 0) {
  266. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  267. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  268. ;
  269. }
  270. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  271. }
  272. #if !CONFIG_FREERTOS_UNICORE
  273. else {
  274. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  275. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  276. ;
  277. }
  278. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  279. }
  280. #endif
  281. *saved_state = ret;
  282. #elif CONFIG_IDF_TARGET_ESP32S2
  283. *saved_state = Cache_Suspend_ICache();
  284. #elif CONFIG_IDF_TARGET_ESP32S3
  285. uint32_t icache_state, dcache_state;
  286. icache_state = Cache_Suspend_ICache() << 16;
  287. dcache_state = Cache_Suspend_DCache();
  288. *saved_state = icache_state | dcache_state;
  289. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  290. uint32_t icache_state;
  291. icache_state = Cache_Suspend_ICache() << 16;
  292. *saved_state = icache_state;
  293. #endif
  294. }
  295. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  296. {
  297. #if CONFIG_IDF_TARGET_ESP32
  298. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  299. if (cpuid == 0) {
  300. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  301. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  302. }
  303. #if !CONFIG_FREERTOS_UNICORE
  304. else {
  305. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  306. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  307. }
  308. #endif
  309. #elif CONFIG_IDF_TARGET_ESP32S2
  310. Cache_Resume_ICache(saved_state);
  311. #elif CONFIG_IDF_TARGET_ESP32S3
  312. Cache_Resume_DCache(saved_state & 0xffff);
  313. Cache_Resume_ICache(saved_state >> 16);
  314. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  315. Cache_Resume_ICache(saved_state >> 16);
  316. #endif
  317. }
  318. IRAM_ATTR bool spi_flash_cache_enabled(void)
  319. {
  320. #if CONFIG_IDF_TARGET_ESP32
  321. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  322. #if portNUM_PROCESSORS == 2
  323. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  324. #endif
  325. #elif CONFIG_IDF_TARGET_ESP32S2
  326. bool result = (REG_GET_BIT(EXTMEM_PRO_ICACHE_CTRL_REG, EXTMEM_PRO_ICACHE_ENABLE) != 0);
  327. #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  328. bool result = (REG_GET_BIT(EXTMEM_ICACHE_CTRL_REG, EXTMEM_ICACHE_ENABLE) != 0);
  329. #endif
  330. return result;
  331. }
  332. #if CONFIG_IDF_TARGET_ESP32S2
  333. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  334. {
  335. cache_size_t cache_size;
  336. cache_ways_t cache_ways;
  337. cache_line_size_t cache_line_size;
  338. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  339. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  340. cache_size = CACHE_SIZE_8KB;
  341. #else
  342. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  343. cache_size = CACHE_SIZE_16KB;
  344. #endif
  345. cache_ways = CACHE_4WAYS_ASSOC;
  346. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  347. cache_line_size = CACHE_LINE_SIZE_16B;
  348. #else
  349. cache_line_size = CACHE_LINE_SIZE_32B;
  350. #endif
  351. ESP_EARLY_LOGI(TAG, "Instruction cache \t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16, 4, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : 32);
  352. Cache_Suspend_ICache();
  353. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  354. Cache_Invalidate_ICache_All();
  355. Cache_Resume_ICache(0);
  356. }
  357. IRAM_ATTR void esp_config_data_cache_mode(void)
  358. {
  359. cache_size_t cache_size;
  360. cache_ways_t cache_ways;
  361. cache_line_size_t cache_line_size;
  362. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  363. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  364. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  365. cache_size = CACHE_SIZE_8KB;
  366. #else
  367. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  368. cache_size = CACHE_SIZE_16KB;
  369. #endif
  370. #else
  371. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  372. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  373. cache_size = CACHE_SIZE_8KB;
  374. #else
  375. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  376. cache_size = CACHE_SIZE_16KB;
  377. #endif
  378. #endif
  379. cache_ways = CACHE_4WAYS_ASSOC;
  380. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  381. cache_line_size = CACHE_LINE_SIZE_16B;
  382. #else
  383. cache_line_size = CACHE_LINE_SIZE_32B;
  384. #endif
  385. ESP_EARLY_LOGI(TAG, "Data cache \t\t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16, 4, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : 32);
  386. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  387. Cache_Invalidate_DCache_All();
  388. }
  389. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  390. {
  391. uint32_t i_autoload, d_autoload;
  392. if (icache) {
  393. i_autoload = Cache_Suspend_ICache();
  394. }
  395. if (dcache) {
  396. d_autoload = Cache_Suspend_DCache();
  397. }
  398. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_FLASH_WRAP_AROUND);
  399. if (icache) {
  400. Cache_Resume_ICache(i_autoload);
  401. }
  402. if (dcache) {
  403. Cache_Resume_DCache(d_autoload);
  404. }
  405. }
  406. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  407. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  408. {
  409. uint32_t i_autoload, d_autoload;
  410. if (icache) {
  411. i_autoload = Cache_Suspend_ICache();
  412. }
  413. if (dcache) {
  414. d_autoload = Cache_Suspend_DCache();
  415. }
  416. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_SRAM_RD_WRAP_AROUND);
  417. if (icache) {
  418. Cache_Resume_ICache(i_autoload);
  419. }
  420. if (dcache) {
  421. Cache_Resume_DCache(d_autoload);
  422. }
  423. }
  424. #endif
  425. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  426. {
  427. int icache_wrap_size = 0, dcache_wrap_size = 0;
  428. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  429. int flash_wrap_size = 0, spiram_wrap_size = 0;
  430. int flash_count = 0, spiram_count = 0;
  431. int i;
  432. bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
  433. uint32_t drom0_in_icache = 1;//always 1 in esp32s2
  434. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  435. drom0_in_icache = 0;
  436. #endif
  437. if (icache_wrap_enable) {
  438. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32H2_INSTRUCTION_CACHE_LINE_16B
  439. icache_wrap_size = 16;
  440. #else
  441. icache_wrap_size = 32;
  442. #endif
  443. }
  444. if (dcache_wrap_enable) {
  445. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B || CONFIG_ESP32S3_DATA_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32H2_INSTRUCTION_CACHE_LINE_16B
  446. dcache_wrap_size = 16;
  447. #else
  448. dcache_wrap_size = 32;
  449. #endif
  450. }
  451. uint32_t instruction_use_spiram = 0;
  452. uint32_t rodata_use_spiram = 0;
  453. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  454. extern uint32_t esp_spiram_instruction_access_enabled(void);
  455. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  456. #endif
  457. #if CONFIG_SPIRAM_RODATA
  458. extern uint32_t esp_spiram_rodata_access_enabled(void);
  459. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  460. #endif
  461. if (instruction_use_spiram) {
  462. spiram_wrap_sizes[0] = icache_wrap_size;
  463. } else {
  464. flash_wrap_sizes[0] = icache_wrap_size;
  465. }
  466. if (rodata_use_spiram) {
  467. if (drom0_in_icache) {
  468. spiram_wrap_sizes[0] = icache_wrap_size;
  469. } else {
  470. spiram_wrap_sizes[1] = dcache_wrap_size;
  471. flash_wrap_sizes[1] = dcache_wrap_size;
  472. }
  473. #ifdef CONFIG_EXT_RODATA_SUPPORT
  474. spiram_wrap_sizes[1] = dcache_wrap_size;
  475. #endif
  476. } else {
  477. if (drom0_in_icache) {
  478. flash_wrap_sizes[0] = icache_wrap_size;
  479. } else {
  480. flash_wrap_sizes[1] = dcache_wrap_size;
  481. }
  482. #ifdef CONFIG_EXT_RODATA_SUPPORT
  483. flash_wrap_sizes[1] = dcache_wrap_size;
  484. #endif
  485. }
  486. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  487. spiram_wrap_sizes[1] = dcache_wrap_size;
  488. #endif
  489. for (i = 0; i < 2; i++) {
  490. if (flash_wrap_sizes[i] != -1) {
  491. flash_count++;
  492. flash_wrap_size = flash_wrap_sizes[i];
  493. }
  494. }
  495. for (i = 0; i < 2; i++) {
  496. if (spiram_wrap_sizes[i] != -1) {
  497. spiram_count++;
  498. spiram_wrap_size = spiram_wrap_sizes[i];
  499. }
  500. }
  501. if (flash_count + spiram_count <= 2) {
  502. flash_spiram_wrap_together = false;
  503. } else {
  504. flash_spiram_wrap_together = true;
  505. }
  506. ESP_EARLY_LOGI(TAG, "flash_count=%d, size=%d, spiram_count=%d, size=%d,together=%d", flash_count, flash_wrap_size, spiram_count, spiram_wrap_size, flash_spiram_wrap_together);
  507. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  508. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  509. if (spiram_wrap_size == 0) {
  510. return ESP_FAIL;
  511. }
  512. if (flash_spiram_wrap_together) {
  513. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  514. return ESP_FAIL;
  515. }
  516. }
  517. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  518. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  519. if (flash_wrap_size == 0) {
  520. return ESP_FAIL;
  521. }
  522. if (flash_spiram_wrap_together) {
  523. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  524. return ESP_FAIL;
  525. }
  526. }
  527. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  528. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  529. return ESP_FAIL;
  530. }
  531. #ifdef CONFIG_FLASHMODE_QIO
  532. flash_support_wrap = true;
  533. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  534. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  535. flash_support_wrap = false;
  536. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  537. }
  538. #else
  539. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  540. #endif
  541. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  542. extern bool psram_support_wrap_size(uint32_t wrap_size);
  543. if (!psram_support_wrap_size(spiram_wrap_size)) {
  544. spiram_support_wrap = false;
  545. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  546. }
  547. #endif
  548. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  549. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  550. return ESP_FAIL;
  551. }
  552. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  553. if (flash_support_wrap && flash_wrap_size > 0) {
  554. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  555. spi_flash_enable_wrap(flash_wrap_size);
  556. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  557. }
  558. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  559. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  560. if (spiram_support_wrap && spiram_wrap_size > 0) {
  561. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  562. psram_enable_wrap(spiram_wrap_size);
  563. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  564. }
  565. #endif
  566. return ESP_OK;
  567. }
  568. #endif
  569. #if CONFIG_IDF_TARGET_ESP32S3
  570. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  571. {
  572. cache_size_t cache_size;
  573. cache_ways_t cache_ways;
  574. cache_line_size_t cache_line_size;
  575. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_16KB
  576. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_INVALID);
  577. cache_size = CACHE_SIZE_HALF;
  578. #else
  579. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1);
  580. cache_size = CACHE_SIZE_FULL;
  581. #endif
  582. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_4WAYS
  583. cache_ways = CACHE_4WAYS_ASSOC;
  584. #else
  585. cache_ways = CACHE_8WAYS_ASSOC;
  586. #endif
  587. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  588. cache_line_size = CACHE_LINE_SIZE_16B;
  589. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  590. cache_line_size = CACHE_LINE_SIZE_32B;
  591. #else
  592. cache_line_size = CACHE_LINE_SIZE_64B;
  593. #endif
  594. ESP_EARLY_LOGI(TAG, "Instruction cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 16 : 32, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  595. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  596. Cache_Invalidate_ICache_All();
  597. extern void Cache_Enable_ICache(uint32_t autoload);
  598. Cache_Enable_ICache(0);
  599. }
  600. IRAM_ATTR void esp_config_data_cache_mode(void)
  601. {
  602. cache_size_t cache_size;
  603. cache_ways_t cache_ways;
  604. cache_line_size_t cache_line_size;
  605. #if CONFIG_ESP32S3_DATA_CACHE_32KB
  606. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK1, CACHE_MEMORY_INVALID);
  607. cache_size = CACHE_SIZE_HALF;
  608. #else
  609. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1);
  610. cache_size = CACHE_SIZE_FULL;
  611. #endif
  612. #if CONFIG_ESP32S3_DATA_CACHE_4WAYS
  613. cache_ways = CACHE_4WAYS_ASSOC;
  614. #else
  615. cache_ways = CACHE_8WAYS_ASSOC;
  616. #endif
  617. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  618. cache_line_size = CACHE_LINE_SIZE_16B;
  619. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  620. cache_line_size = CACHE_LINE_SIZE_32B;
  621. #else
  622. cache_line_size = CACHE_LINE_SIZE_64B;
  623. #endif
  624. // ESP_EARLY_LOGI(TAG, "Data cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 32 : 64, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  625. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  626. Cache_Invalidate_DCache_All();
  627. }
  628. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  629. {
  630. uint32_t i_autoload, d_autoload;
  631. if (icache) {
  632. i_autoload = Cache_Suspend_ICache();
  633. }
  634. if (dcache) {
  635. d_autoload = Cache_Suspend_DCache();
  636. }
  637. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  638. if (icache) {
  639. Cache_Resume_ICache(i_autoload);
  640. }
  641. if (dcache) {
  642. Cache_Resume_DCache(d_autoload);
  643. }
  644. }
  645. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  646. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  647. {
  648. uint32_t i_autoload, d_autoload;
  649. if (icache) {
  650. i_autoload = Cache_Suspend_ICache();
  651. }
  652. if (dcache) {
  653. d_autoload = Cache_Suspend_DCache();
  654. }
  655. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_SRAM_RD_WRAP_AROUND);
  656. if (icache) {
  657. Cache_Resume_ICache(i_autoload);
  658. }
  659. if (dcache) {
  660. Cache_Resume_DCache(d_autoload);
  661. }
  662. }
  663. #endif
  664. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  665. {
  666. int icache_wrap_size = 0, dcache_wrap_size = 0;
  667. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  668. int flash_wrap_size = 0, spiram_wrap_size = 0;
  669. int flash_count = 0, spiram_count = 0;
  670. int i;
  671. bool flash_spiram_wrap_together, flash_support_wrap = false, spiram_support_wrap = true;
  672. uint32_t drom0_in_icache = 0;//always 0 in chip7.2.4
  673. if (icache_wrap_enable) {
  674. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  675. icache_wrap_size = 16;
  676. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  677. icache_wrap_size = 32;
  678. #else
  679. icache_wrap_size = 64;
  680. #endif
  681. }
  682. if (dcache_wrap_enable) {
  683. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  684. dcache_wrap_size = 16;
  685. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  686. dcache_wrap_size = 32;
  687. #else
  688. dcache_wrap_size = 64;
  689. #endif
  690. }
  691. uint32_t instruction_use_spiram = 0;
  692. uint32_t rodata_use_spiram = 0;
  693. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  694. extern uint32_t esp_spiram_instruction_access_enabled();
  695. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  696. #endif
  697. #if CONFIG_SPIRAM_RODATA
  698. extern uint32_t esp_spiram_rodata_access_enabled();
  699. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  700. #endif
  701. if (instruction_use_spiram) {
  702. spiram_wrap_sizes[0] = icache_wrap_size;
  703. } else {
  704. flash_wrap_sizes[0] = icache_wrap_size;
  705. }
  706. if (rodata_use_spiram) {
  707. if (drom0_in_icache) {
  708. spiram_wrap_sizes[0] = icache_wrap_size;
  709. } else {
  710. spiram_wrap_sizes[1] = dcache_wrap_size;
  711. }
  712. #ifdef CONFIG_EXT_RODATA_SUPPORT
  713. spiram_wrap_sizes[1] = dcache_wrap_size;
  714. #endif
  715. } else {
  716. if (drom0_in_icache) {
  717. flash_wrap_sizes[0] = icache_wrap_size;
  718. } else {
  719. flash_wrap_sizes[1] = dcache_wrap_size;
  720. }
  721. #ifdef CONFIG_EXT_RODATA_SUPPORT
  722. flash_wrap_sizes[1] = dcache_wrap_size;
  723. #endif
  724. }
  725. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  726. spiram_wrap_sizes[1] = dcache_wrap_size;
  727. #endif
  728. for (i = 0; i < 2; i++) {
  729. if (flash_wrap_sizes[i] != -1) {
  730. flash_count++;
  731. flash_wrap_size = flash_wrap_sizes[i];
  732. }
  733. }
  734. for (i = 0; i < 2; i++) {
  735. if (spiram_wrap_sizes[i] != -1) {
  736. spiram_count++;
  737. spiram_wrap_size = spiram_wrap_sizes[i];
  738. }
  739. }
  740. if (flash_count + spiram_count <= 2) {
  741. flash_spiram_wrap_together = false;
  742. } else {
  743. flash_spiram_wrap_together = true;
  744. }
  745. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  746. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  747. if (spiram_wrap_size == 0) {
  748. return ESP_FAIL;
  749. }
  750. if (flash_spiram_wrap_together) {
  751. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  752. return ESP_FAIL;
  753. }
  754. }
  755. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  756. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  757. if (flash_wrap_size == 0) {
  758. return ESP_FAIL;
  759. }
  760. if (flash_spiram_wrap_together) {
  761. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  762. return ESP_FAIL;
  763. }
  764. }
  765. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  766. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  767. return ESP_FAIL;
  768. }
  769. #ifdef CONFIG_FLASHMODE_QIO
  770. flash_support_wrap = true;
  771. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  772. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  773. flash_support_wrap = false;
  774. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  775. }
  776. #else
  777. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  778. #endif
  779. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  780. extern bool psram_support_wrap_size(uint32_t wrap_size);
  781. if (!psram_support_wrap_size(spiram_wrap_size)) {
  782. spiram_support_wrap = false;
  783. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  784. }
  785. #endif
  786. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  787. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  788. return ESP_FAIL;
  789. }
  790. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  791. if (flash_support_wrap && flash_wrap_size > 0) {
  792. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  793. spi_flash_enable_wrap(flash_wrap_size);
  794. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  795. }
  796. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  797. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  798. if (spiram_support_wrap && spiram_wrap_size > 0) {
  799. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  800. psram_enable_wrap(spiram_wrap_size);
  801. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  802. }
  803. #endif
  804. return ESP_OK;
  805. }
  806. #endif
  807. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  808. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache)
  809. {
  810. uint32_t i_autoload;
  811. if (icache) {
  812. i_autoload = Cache_Suspend_ICache();
  813. }
  814. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  815. if (icache) {
  816. Cache_Resume_ICache(i_autoload);
  817. }
  818. }
  819. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable)
  820. {
  821. int flash_wrap_size = 0;
  822. bool flash_support_wrap = false;
  823. if (icache_wrap_enable) {
  824. flash_wrap_size = 32;
  825. }
  826. #ifdef CONFIG_FLASHMODE_QIO
  827. flash_support_wrap = true;
  828. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  829. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  830. flash_support_wrap = false;
  831. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  832. }
  833. #else
  834. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  835. #endif // CONFIG_FLASHMODE_QIO
  836. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  837. if (flash_support_wrap && flash_wrap_size > 0) {
  838. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  839. spi_flash_enable_wrap(flash_wrap_size);
  840. esp_enable_cache_flash_wrap((flash_wrap_size > 0));
  841. }
  842. return ESP_OK;
  843. }
  844. #endif // CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  845. void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
  846. {
  847. #if CONFIG_IDF_TARGET_ESP32
  848. uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid);
  849. cache_value &= DPORT_CACHE_GET_MASK(cpuid);
  850. // Re-enable cache on this CPU
  851. spi_flash_restore_cache(cpuid, cache_value);
  852. #else
  853. spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
  854. #endif
  855. }