cache_utils.c 34 KB

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