cache_utils.c 35 KB

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