cache_utils.c 35 KB

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