cache_utils.c 34 KB

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