cache_utils.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
  318. {
  319. cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
  320. }
  321. void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  322. {
  323. cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
  324. }
  325. bool IRAM_ATTR spi_flash_cache_enabled(void)
  326. {
  327. return cache_hal_is_cache_enabled(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
  328. }
  329. #if CONFIG_IDF_TARGET_ESP32S2
  330. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  331. {
  332. cache_size_t cache_size;
  333. cache_ways_t cache_ways;
  334. cache_line_size_t cache_line_size;
  335. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  336. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  337. cache_size = CACHE_SIZE_8KB;
  338. #else
  339. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  340. cache_size = CACHE_SIZE_16KB;
  341. #endif
  342. cache_ways = CACHE_4WAYS_ASSOC;
  343. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  344. cache_line_size = CACHE_LINE_SIZE_16B;
  345. #else
  346. cache_line_size = CACHE_LINE_SIZE_32B;
  347. #endif
  348. 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);
  349. Cache_Suspend_ICache();
  350. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  351. Cache_Invalidate_ICache_All();
  352. Cache_Resume_ICache(0);
  353. }
  354. IRAM_ATTR void esp_config_data_cache_mode(void)
  355. {
  356. cache_size_t cache_size;
  357. cache_ways_t cache_ways;
  358. cache_line_size_t cache_line_size;
  359. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  360. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  361. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  362. cache_size = CACHE_SIZE_8KB;
  363. #else
  364. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  365. cache_size = CACHE_SIZE_16KB;
  366. #endif
  367. #else
  368. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  369. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  370. cache_size = CACHE_SIZE_8KB;
  371. #else
  372. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  373. cache_size = CACHE_SIZE_16KB;
  374. #endif
  375. #endif
  376. cache_ways = CACHE_4WAYS_ASSOC;
  377. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  378. cache_line_size = CACHE_LINE_SIZE_16B;
  379. #else
  380. cache_line_size = CACHE_LINE_SIZE_32B;
  381. #endif
  382. 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);
  383. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  384. Cache_Invalidate_DCache_All();
  385. }
  386. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  387. {
  388. uint32_t i_autoload, d_autoload;
  389. if (icache) {
  390. i_autoload = Cache_Suspend_ICache();
  391. }
  392. if (dcache) {
  393. d_autoload = Cache_Suspend_DCache();
  394. }
  395. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_FLASH_WRAP_AROUND);
  396. if (icache) {
  397. Cache_Resume_ICache(i_autoload);
  398. }
  399. if (dcache) {
  400. Cache_Resume_DCache(d_autoload);
  401. }
  402. }
  403. #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
  404. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  405. {
  406. uint32_t i_autoload, d_autoload;
  407. if (icache) {
  408. i_autoload = Cache_Suspend_ICache();
  409. }
  410. if (dcache) {
  411. d_autoload = Cache_Suspend_DCache();
  412. }
  413. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_SRAM_RD_WRAP_AROUND);
  414. if (icache) {
  415. Cache_Resume_ICache(i_autoload);
  416. }
  417. if (dcache) {
  418. Cache_Resume_DCache(d_autoload);
  419. }
  420. }
  421. #endif
  422. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  423. {
  424. int icache_wrap_size = 0, dcache_wrap_size = 0;
  425. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  426. int flash_wrap_size = 0, spiram_wrap_size = 0;
  427. int flash_count = 0, spiram_count = 0;
  428. int i;
  429. bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
  430. uint32_t drom0_in_icache = 1;//always 1 in esp32s2
  431. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32P4
  432. drom0_in_icache = 0;
  433. #endif
  434. if (icache_wrap_enable) {
  435. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  436. icache_wrap_size = FLASH_WRAP_SIZE_16B;
  437. #else
  438. icache_wrap_size = FLASH_WRAP_SIZE_32B;
  439. #endif
  440. }
  441. if (dcache_wrap_enable) {
  442. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B || CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  443. dcache_wrap_size = FLASH_WRAP_SIZE_16B;
  444. #else
  445. dcache_wrap_size = FLASH_WRAP_SIZE_32B;
  446. #endif
  447. }
  448. uint32_t instruction_use_spiram = 0;
  449. uint32_t rodata_use_spiram = 0;
  450. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  451. extern uint32_t esp_spiram_instruction_access_enabled(void);
  452. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  453. #endif
  454. #if CONFIG_SPIRAM_RODATA
  455. extern uint32_t esp_spiram_rodata_access_enabled(void);
  456. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  457. #endif
  458. if (instruction_use_spiram) {
  459. spiram_wrap_sizes[0] = icache_wrap_size;
  460. } else {
  461. flash_wrap_sizes[0] = icache_wrap_size;
  462. }
  463. if (rodata_use_spiram) {
  464. if (drom0_in_icache) {
  465. spiram_wrap_sizes[0] = icache_wrap_size;
  466. } else {
  467. spiram_wrap_sizes[1] = dcache_wrap_size;
  468. flash_wrap_sizes[1] = dcache_wrap_size;
  469. }
  470. } else {
  471. if (drom0_in_icache) {
  472. flash_wrap_sizes[0] = icache_wrap_size;
  473. } else {
  474. flash_wrap_sizes[1] = dcache_wrap_size;
  475. }
  476. }
  477. #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
  478. spiram_wrap_sizes[1] = dcache_wrap_size;
  479. #endif
  480. for (i = 0; i < 2; i++) {
  481. if (flash_wrap_sizes[i] != -1) {
  482. flash_count++;
  483. flash_wrap_size = flash_wrap_sizes[i];
  484. }
  485. }
  486. for (i = 0; i < 2; i++) {
  487. if (spiram_wrap_sizes[i] != -1) {
  488. spiram_count++;
  489. spiram_wrap_size = spiram_wrap_sizes[i];
  490. }
  491. }
  492. if (flash_count + spiram_count <= 2) {
  493. flash_spiram_wrap_together = false;
  494. } else {
  495. flash_spiram_wrap_together = true;
  496. }
  497. 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);
  498. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  499. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  500. if (spiram_wrap_size == 0) {
  501. return ESP_FAIL;
  502. }
  503. if (flash_spiram_wrap_together) {
  504. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  505. return ESP_FAIL;
  506. }
  507. }
  508. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  509. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  510. if (flash_wrap_size == 0) {
  511. return ESP_FAIL;
  512. }
  513. if (flash_spiram_wrap_together) {
  514. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  515. return ESP_FAIL;
  516. }
  517. }
  518. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  519. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  520. return ESP_FAIL;
  521. }
  522. #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
  523. flash_support_wrap = true;
  524. spi_flash_wrap_probe();
  525. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  526. flash_support_wrap = false;
  527. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  528. }
  529. #else
  530. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  531. #endif
  532. #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
  533. extern bool psram_support_wrap_size(uint32_t wrap_size);
  534. if (!psram_support_wrap_size(spiram_wrap_size)) {
  535. spiram_support_wrap = false;
  536. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  537. }
  538. #endif
  539. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  540. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  541. return ESP_FAIL;
  542. }
  543. if (flash_support_wrap && flash_wrap_size > 0) {
  544. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  545. spi_flash_wrap_enable(flash_wrap_size);
  546. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  547. }
  548. #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
  549. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  550. if (spiram_support_wrap && spiram_wrap_size > 0) {
  551. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  552. psram_enable_wrap(spiram_wrap_size);
  553. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  554. }
  555. #endif
  556. return ESP_OK;
  557. }
  558. #endif
  559. #if CONFIG_IDF_TARGET_ESP32S3
  560. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  561. {
  562. cache_size_t cache_size;
  563. cache_ways_t cache_ways;
  564. cache_line_size_t cache_line_size;
  565. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_16KB
  566. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_INVALID);
  567. cache_size = CACHE_SIZE_HALF;
  568. #else
  569. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1);
  570. cache_size = CACHE_SIZE_FULL;
  571. #endif
  572. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_4WAYS
  573. cache_ways = CACHE_4WAYS_ASSOC;
  574. #else
  575. cache_ways = CACHE_8WAYS_ASSOC;
  576. #endif
  577. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  578. cache_line_size = CACHE_LINE_SIZE_16B;
  579. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  580. cache_line_size = CACHE_LINE_SIZE_32B;
  581. #else
  582. cache_line_size = CACHE_LINE_SIZE_64B;
  583. #endif
  584. 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));
  585. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  586. Cache_Invalidate_ICache_All();
  587. extern void Cache_Enable_ICache(uint32_t autoload);
  588. Cache_Enable_ICache(0);
  589. }
  590. IRAM_ATTR void esp_config_data_cache_mode(void)
  591. {
  592. cache_size_t cache_size;
  593. cache_ways_t cache_ways;
  594. cache_line_size_t cache_line_size;
  595. #if CONFIG_ESP32S3_DATA_CACHE_32KB
  596. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK1, CACHE_MEMORY_INVALID);
  597. cache_size = CACHE_SIZE_HALF;
  598. #else
  599. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1);
  600. cache_size = CACHE_SIZE_FULL;
  601. #endif
  602. #if CONFIG_ESP32S3_DATA_CACHE_4WAYS
  603. cache_ways = CACHE_4WAYS_ASSOC;
  604. #else
  605. cache_ways = CACHE_8WAYS_ASSOC;
  606. #endif
  607. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  608. cache_line_size = CACHE_LINE_SIZE_16B;
  609. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  610. cache_line_size = CACHE_LINE_SIZE_32B;
  611. #else
  612. cache_line_size = CACHE_LINE_SIZE_64B;
  613. #endif
  614. // 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));
  615. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  616. Cache_Invalidate_DCache_All();
  617. }
  618. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  619. {
  620. uint32_t i_autoload, d_autoload;
  621. if (icache) {
  622. i_autoload = Cache_Suspend_ICache();
  623. }
  624. if (dcache) {
  625. d_autoload = Cache_Suspend_DCache();
  626. }
  627. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  628. if (icache) {
  629. Cache_Resume_ICache(i_autoload);
  630. }
  631. if (dcache) {
  632. Cache_Resume_DCache(d_autoload);
  633. }
  634. }
  635. #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
  636. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  637. {
  638. uint32_t i_autoload, d_autoload;
  639. if (icache) {
  640. i_autoload = Cache_Suspend_ICache();
  641. }
  642. if (dcache) {
  643. d_autoload = Cache_Suspend_DCache();
  644. }
  645. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_SRAM_RD_WRAP_AROUND);
  646. if (icache) {
  647. Cache_Resume_ICache(i_autoload);
  648. }
  649. if (dcache) {
  650. Cache_Resume_DCache(d_autoload);
  651. }
  652. }
  653. #endif
  654. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  655. {
  656. int icache_wrap_size = 0, dcache_wrap_size = 0;
  657. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  658. int flash_wrap_size = 0, spiram_wrap_size = 0;
  659. int flash_count = 0, spiram_count = 0;
  660. int i;
  661. bool flash_spiram_wrap_together, flash_support_wrap = false, spiram_support_wrap = true;
  662. uint32_t drom0_in_icache = 0;//always 0 in chip7.2.4
  663. if (icache_wrap_enable) {
  664. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  665. icache_wrap_size = FLASH_WRAP_SIZE_16B;
  666. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  667. icache_wrap_size = FLASH_WRAP_SIZE_32B;
  668. #else
  669. icache_wrap_size = FLASH_WRAP_SIZE_64B;
  670. #endif
  671. }
  672. if (dcache_wrap_enable) {
  673. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  674. dcache_wrap_size = FLASH_WRAP_SIZE_16B;
  675. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  676. dcache_wrap_size = FLASH_WRAP_SIZE_32B;
  677. #else
  678. dcache_wrap_size = FLASH_WRAP_SIZE_64B;
  679. #endif
  680. }
  681. uint32_t instruction_use_spiram = 0;
  682. uint32_t rodata_use_spiram = 0;
  683. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  684. extern uint32_t esp_spiram_instruction_access_enabled(void);
  685. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  686. #endif
  687. #if CONFIG_SPIRAM_RODATA
  688. extern uint32_t esp_spiram_rodata_access_enabled(void);
  689. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  690. #endif
  691. if (instruction_use_spiram) {
  692. spiram_wrap_sizes[0] = icache_wrap_size;
  693. } else {
  694. flash_wrap_sizes[0] = icache_wrap_size;
  695. }
  696. if (rodata_use_spiram) {
  697. if (drom0_in_icache) {
  698. spiram_wrap_sizes[0] = icache_wrap_size;
  699. } else {
  700. spiram_wrap_sizes[1] = dcache_wrap_size;
  701. }
  702. } else {
  703. if (drom0_in_icache) {
  704. flash_wrap_sizes[0] = icache_wrap_size;
  705. } else {
  706. flash_wrap_sizes[1] = dcache_wrap_size;
  707. }
  708. }
  709. #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
  710. spiram_wrap_sizes[1] = dcache_wrap_size;
  711. #endif
  712. for (i = 0; i < 2; i++) {
  713. if (flash_wrap_sizes[i] != -1) {
  714. flash_count++;
  715. flash_wrap_size = flash_wrap_sizes[i];
  716. }
  717. }
  718. for (i = 0; i < 2; i++) {
  719. if (spiram_wrap_sizes[i] != -1) {
  720. spiram_count++;
  721. spiram_wrap_size = spiram_wrap_sizes[i];
  722. }
  723. }
  724. if (flash_count + spiram_count <= 2) {
  725. flash_spiram_wrap_together = false;
  726. } else {
  727. flash_spiram_wrap_together = true;
  728. }
  729. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  730. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  731. if (spiram_wrap_size == 0) {
  732. return ESP_FAIL;
  733. }
  734. if (flash_spiram_wrap_together) {
  735. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  736. return ESP_FAIL;
  737. }
  738. }
  739. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  740. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  741. if (flash_wrap_size == 0) {
  742. return ESP_FAIL;
  743. }
  744. if (flash_spiram_wrap_together) {
  745. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  746. return ESP_FAIL;
  747. }
  748. }
  749. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  750. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  751. return ESP_FAIL;
  752. }
  753. #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
  754. flash_support_wrap = true;
  755. spi_flash_wrap_probe();
  756. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  757. flash_support_wrap = false;
  758. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  759. }
  760. #else
  761. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  762. #endif
  763. #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
  764. extern bool psram_support_wrap_size(uint32_t wrap_size);
  765. if (!psram_support_wrap_size(spiram_wrap_size)) {
  766. spiram_support_wrap = false;
  767. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  768. }
  769. #endif
  770. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  771. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  772. return ESP_FAIL;
  773. }
  774. if (flash_support_wrap && flash_wrap_size > 0) {
  775. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  776. spi_flash_wrap_enable(flash_wrap_size);
  777. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  778. }
  779. #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
  780. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  781. if (spiram_support_wrap && spiram_wrap_size > 0) {
  782. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  783. psram_enable_wrap(spiram_wrap_size);
  784. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  785. }
  786. #endif
  787. return ESP_OK;
  788. }
  789. #endif
  790. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
  791. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache)
  792. {
  793. uint32_t i_autoload;
  794. if (icache) {
  795. i_autoload = Cache_Suspend_ICache();
  796. }
  797. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  798. if (icache) {
  799. Cache_Resume_ICache(i_autoload);
  800. }
  801. }
  802. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable)
  803. {
  804. int flash_wrap_size = 0;
  805. bool flash_support_wrap = false;
  806. if (icache_wrap_enable) {
  807. flash_wrap_size = 32;
  808. }
  809. #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
  810. flash_support_wrap = true;
  811. spi_flash_wrap_probe();
  812. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  813. flash_support_wrap = false;
  814. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  815. }
  816. #else
  817. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  818. #endif // CONFIG_ESPTOOLPY_FLASHMODE_QIO
  819. if (flash_support_wrap && flash_wrap_size > 0) {
  820. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  821. spi_flash_wrap_enable(flash_wrap_size);
  822. esp_enable_cache_flash_wrap((flash_wrap_size > 0));
  823. }
  824. return ESP_OK;
  825. }
  826. #endif // CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
  827. #if CONFIG_IDF_TARGET_ESP32P4
  828. //TODO: IDF-5670
  829. void esp_config_l2_cache_mode(void)
  830. {
  831. cache_size_t cache_size;
  832. cache_line_size_t cache_line_size;
  833. #if CONFIG_CACHE_L2_CACHE_128KB
  834. cache_size = CACHE_SIZE_128K;
  835. #elif CONFIG_CACHE_L2_CACHE_256KB
  836. cache_size = CACHE_SIZE_256K;
  837. #else
  838. cache_size = CACHE_SIZE_512K;
  839. #endif
  840. #if CONFIG_CACHE_L2_CACHE_LINE_64B
  841. cache_line_size = CACHE_LINE_SIZE_64B;
  842. #else
  843. cache_line_size = CACHE_LINE_SIZE_128B;
  844. #endif
  845. Cache_Set_L2_Cache_Mode(cache_size, 8, cache_line_size);
  846. Cache_Invalidate_All(CACHE_MAP_L2_CACHE);
  847. }
  848. #endif