cache_utils.c 33 KB

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