cache_utils.c 34 KB

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