cache_utils.c 31 KB

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