cache_utils.c 35 KB

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