cache_utils.c 34 KB

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