cache_utils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <freertos/FreeRTOS.h>
  11. #include <freertos/task.h>
  12. #include <freertos/semphr.h>
  13. #if CONFIG_IDF_TARGET_ESP32
  14. #include <esp32/rom/spi_flash.h>
  15. #include <esp32/rom/cache.h>
  16. #elif CONFIG_IDF_TARGET_ESP32S2
  17. #include "esp32s2/rom/spi_flash.h"
  18. #include "esp32s2/rom/cache.h"
  19. #include "soc/extmem_reg.h"
  20. #include "soc/cache_memory.h"
  21. #endif
  22. #include <soc/soc.h>
  23. #include <soc/dport_reg.h>
  24. #include "sdkconfig.h"
  25. #ifndef CONFIG_FREERTOS_UNICORE
  26. #include "esp_ipc.h"
  27. #endif
  28. #include "esp_attr.h"
  29. #include "esp_intr_alloc.h"
  30. #include "esp_spi_flash.h"
  31. #include "esp_log.h"
  32. static __attribute__((unused)) const char *TAG = "cache";
  33. #define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid
  34. #define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  35. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \
  36. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) )
  37. #define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  38. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \
  39. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0)))
  40. #define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP)
  41. #define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP)
  42. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
  43. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  44. static uint32_t s_flash_op_cache_state[2];
  45. #ifndef CONFIG_FREERTOS_UNICORE
  46. static SemaphoreHandle_t s_flash_op_mutex;
  47. static volatile bool s_flash_op_can_start = false;
  48. static volatile bool s_flash_op_complete = false;
  49. #ifndef NDEBUG
  50. static volatile int s_flash_op_cpu = -1;
  51. #endif
  52. static inline bool esp_task_stack_is_sane_cache_disabled(void)
  53. {
  54. const void *sp = (const void *)get_sp();
  55. return esp_ptr_in_dram(sp)
  56. #if CONFIG_ESP32_ALLOW_RTC_FAST_MEM_AS_HEAP || CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP
  57. || esp_ptr_in_rtc_dram_fast(sp)
  58. #endif
  59. ;
  60. }
  61. void spi_flash_init_lock(void)
  62. {
  63. s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
  64. assert(s_flash_op_mutex != NULL);
  65. }
  66. void spi_flash_op_lock(void)
  67. {
  68. xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
  69. }
  70. void spi_flash_op_unlock(void)
  71. {
  72. xSemaphoreGiveRecursive(s_flash_op_mutex);
  73. }
  74. /*
  75. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  76. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  77. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  78. when accessing psram from the former CPU.
  79. */
  80. void IRAM_ATTR spi_flash_op_block_func(void *arg)
  81. {
  82. // Disable scheduler on this CPU
  83. vTaskSuspendAll();
  84. // Restore interrupts that aren't located in IRAM
  85. esp_intr_noniram_disable();
  86. uint32_t cpuid = (uint32_t) arg;
  87. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  88. // CPU may reset the flag back to false before IPC task has a chance to check it
  89. // (if it is preempted by an ISR taking non-trivial amount of time)
  90. s_flash_op_complete = false;
  91. s_flash_op_can_start = true;
  92. while (!s_flash_op_complete) {
  93. // busy loop here and wait for the other CPU to finish flash operation
  94. }
  95. // Flash operation is complete, re-enable cache
  96. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  97. // Restore interrupts that aren't located in IRAM
  98. esp_intr_noniram_enable();
  99. // Re-enable scheduler
  100. xTaskResumeAll();
  101. }
  102. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  103. {
  104. assert(esp_task_stack_is_sane_cache_disabled());
  105. spi_flash_op_lock();
  106. const uint32_t cpuid = xPortGetCoreID();
  107. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  108. #ifndef NDEBUG
  109. // For sanity check later: record the CPU which has started doing flash operation
  110. assert(s_flash_op_cpu == -1);
  111. s_flash_op_cpu = cpuid;
  112. #endif
  113. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  114. // Scheduler hasn't been started yet, it means that spi_flash API is being
  115. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  116. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  117. // which is in IRAM. So it is safe to disable cache for the other_cpuid after
  118. // esp_intr_noniram_disable.
  119. assert(other_cpuid == 1);
  120. } else {
  121. // Temporarily raise current task priority to prevent a deadlock while
  122. // waiting for IPC task to start on the other CPU
  123. int old_prio = uxTaskPriorityGet(NULL);
  124. vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1);
  125. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  126. // disable cache there and block other tasks from executing.
  127. s_flash_op_can_start = false;
  128. esp_err_t ret = esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void *) other_cpuid);
  129. assert(ret == ESP_OK);
  130. while (!s_flash_op_can_start) {
  131. // Busy loop and wait for spi_flash_op_block_func to disable cache
  132. // on the other CPU
  133. }
  134. // Disable scheduler on the current CPU
  135. vTaskSuspendAll();
  136. // Can now set the priority back to the normal one
  137. vTaskPrioritySet(NULL, old_prio);
  138. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  139. // occupied by highest priority task
  140. assert(xPortGetCoreID() == cpuid);
  141. }
  142. // Kill interrupts that aren't located in IRAM
  143. esp_intr_noniram_disable();
  144. // This CPU executes this routine, with non-IRAM interrupts and the scheduler
  145. // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
  146. // with non-iram interrupts and the scheduler disabled. None of these CPUs will
  147. // touch external RAM or flash this way, so we can safely disable caches.
  148. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  149. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  150. }
  151. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  152. {
  153. const uint32_t cpuid = xPortGetCoreID();
  154. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  155. #ifndef NDEBUG
  156. // Sanity check: flash operation ends on the same CPU as it has started
  157. assert(cpuid == s_flash_op_cpu);
  158. // More sanity check: if scheduler isn't started, only CPU0 can call this.
  159. assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
  160. s_flash_op_cpu = -1;
  161. #endif
  162. // Re-enable cache on both CPUs. After this, cache (flash and external RAM) should work again.
  163. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  164. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  165. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  166. // Signal to spi_flash_op_block_task that flash operation is complete
  167. s_flash_op_complete = true;
  168. }
  169. // Re-enable non-iram interrupts
  170. esp_intr_noniram_enable();
  171. // Resume tasks on the current CPU, if the scheduler has started.
  172. // NOTE: enabling non-IRAM interrupts has to happen before this,
  173. // because once the scheduler has started, due to preemption the
  174. // current task can end up being moved to the other CPU.
  175. // But esp_intr_noniram_enable has to be called on the same CPU which
  176. // called esp_intr_noniram_disable
  177. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  178. xTaskResumeAll();
  179. }
  180. // Release API lock
  181. spi_flash_op_unlock();
  182. }
  183. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  184. {
  185. const uint32_t cpuid = xPortGetCoreID();
  186. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  187. // do not care about other CPU, it was halted upon entering panic handler
  188. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  189. // Kill interrupts that aren't located in IRAM
  190. esp_intr_noniram_disable();
  191. // Disable cache on this CPU as well
  192. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  193. }
  194. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  195. {
  196. const uint32_t cpuid = xPortGetCoreID();
  197. // Re-enable cache on this CPU
  198. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  199. // Re-enable non-iram interrupts
  200. esp_intr_noniram_enable();
  201. }
  202. #else // CONFIG_FREERTOS_UNICORE
  203. void spi_flash_init_lock(void)
  204. {
  205. }
  206. void spi_flash_op_lock(void)
  207. {
  208. vTaskSuspendAll();
  209. }
  210. void spi_flash_op_unlock(void)
  211. {
  212. xTaskResumeAll();
  213. }
  214. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  215. {
  216. spi_flash_op_lock();
  217. esp_intr_noniram_disable();
  218. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  219. }
  220. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  221. {
  222. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  223. esp_intr_noniram_enable();
  224. spi_flash_op_unlock();
  225. }
  226. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  227. {
  228. // Kill interrupts that aren't located in IRAM
  229. esp_intr_noniram_disable();
  230. // Disable cache on this CPU as well
  231. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  232. }
  233. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  234. {
  235. // Re-enable cache on this CPU
  236. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  237. // Re-enable non-iram interrupts
  238. esp_intr_noniram_enable();
  239. }
  240. #endif // CONFIG_FREERTOS_UNICORE
  241. /**
  242. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  243. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  244. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  245. */
  246. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
  247. {
  248. #if CONFIG_IDF_TARGET_ESP32
  249. uint32_t ret = 0;
  250. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  251. if (cpuid == 0) {
  252. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  253. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  254. ;
  255. }
  256. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  257. }
  258. #if !CONFIG_FREERTOS_UNICORE
  259. else {
  260. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  261. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  262. ;
  263. }
  264. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  265. }
  266. #endif
  267. *saved_state = ret;
  268. #elif CONFIG_IDF_TARGET_ESP32S2
  269. *saved_state = Cache_Suspend_ICache();
  270. #endif
  271. }
  272. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  273. {
  274. #if CONFIG_IDF_TARGET_ESP32
  275. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  276. if (cpuid == 0) {
  277. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  278. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  279. }
  280. #if !CONFIG_FREERTOS_UNICORE
  281. else {
  282. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  283. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  284. }
  285. #endif
  286. #elif CONFIG_IDF_TARGET_ESP32S2
  287. Cache_Resume_ICache(saved_state);
  288. #endif
  289. }
  290. IRAM_ATTR bool spi_flash_cache_enabled(void)
  291. {
  292. #if CONFIG_IDF_TARGET_ESP32
  293. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  294. #if portNUM_PROCESSORS == 2
  295. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  296. #endif
  297. #elif CONFIG_IDF_TARGET_ESP32S2
  298. bool result = (REG_GET_BIT(EXTMEM_PRO_ICACHE_CTRL_REG, EXTMEM_PRO_ICACHE_ENABLE) != 0);
  299. #endif
  300. return result;
  301. }
  302. #if CONFIG_IDF_TARGET_ESP32S2
  303. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  304. {
  305. cache_size_t cache_size;
  306. cache_ways_t cache_ways;
  307. cache_line_size_t cache_line_size;
  308. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  309. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  310. cache_size = CACHE_SIZE_8KB;
  311. #else
  312. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  313. cache_size = CACHE_SIZE_16KB;
  314. #endif
  315. cache_ways = CACHE_4WAYS_ASSOC;
  316. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  317. cache_line_size = CACHE_LINE_SIZE_16B;
  318. #else
  319. cache_line_size = CACHE_LINE_SIZE_32B;
  320. #endif
  321. 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);
  322. Cache_Suspend_ICache();
  323. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  324. Cache_Invalidate_ICache_All();
  325. Cache_Resume_ICache(0);
  326. }
  327. IRAM_ATTR void esp_config_data_cache_mode(void)
  328. {
  329. cache_size_t cache_size;
  330. cache_ways_t cache_ways;
  331. cache_line_size_t cache_line_size;
  332. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  333. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  334. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  335. cache_size = CACHE_SIZE_8KB;
  336. #else
  337. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  338. cache_size = CACHE_SIZE_16KB;
  339. #endif
  340. #else
  341. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  342. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  343. cache_size = CACHE_SIZE_8KB;
  344. #else
  345. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  346. cache_size = CACHE_SIZE_16KB;
  347. #endif
  348. #endif
  349. cache_ways = CACHE_4WAYS_ASSOC;
  350. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  351. cache_line_size = CACHE_LINE_SIZE_16B;
  352. #else
  353. cache_line_size = CACHE_LINE_SIZE_32B;
  354. #endif
  355. 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);
  356. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  357. Cache_Invalidate_DCache_All();
  358. }
  359. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  360. {
  361. uint32_t i_autoload, d_autoload;
  362. if (icache) {
  363. i_autoload = Cache_Suspend_ICache();
  364. }
  365. if (dcache) {
  366. d_autoload = Cache_Suspend_DCache();
  367. }
  368. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_FLASH_WRAP_AROUND);
  369. if (icache) {
  370. Cache_Resume_ICache(i_autoload);
  371. }
  372. if (dcache) {
  373. Cache_Resume_DCache(d_autoload);
  374. }
  375. }
  376. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  377. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  378. {
  379. uint32_t i_autoload, d_autoload;
  380. if (icache) {
  381. i_autoload = Cache_Suspend_ICache();
  382. }
  383. if (dcache) {
  384. d_autoload = Cache_Suspend_DCache();
  385. }
  386. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_SRAM_RD_WRAP_AROUND);
  387. if (icache) {
  388. Cache_Resume_ICache(i_autoload);
  389. }
  390. if (dcache) {
  391. Cache_Resume_DCache(d_autoload);
  392. }
  393. }
  394. #endif
  395. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  396. {
  397. int icache_wrap_size = 0, dcache_wrap_size = 0;
  398. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  399. int flash_wrap_size = 0, spiram_wrap_size = 0;
  400. int flash_count = 0, spiram_count = 0;
  401. int i;
  402. bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
  403. uint32_t drom0_in_icache = 1;//always 1 in esp32s2
  404. if (icache_wrap_enable) {
  405. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  406. icache_wrap_size = 16;
  407. #else
  408. icache_wrap_size = 32;
  409. #endif
  410. }
  411. if (dcache_wrap_enable) {
  412. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  413. dcache_wrap_size = 16;
  414. #else
  415. dcache_wrap_size = 32;
  416. #endif
  417. }
  418. uint32_t instruction_use_spiram = 0;
  419. uint32_t rodata_use_spiram = 0;
  420. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  421. extern uint32_t esp_spiram_instruction_access_enabled(void);
  422. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  423. #endif
  424. #if CONFIG_SPIRAM_RODATA
  425. extern uint32_t esp_spiram_rodata_access_enabled(void);
  426. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  427. #endif
  428. if (instruction_use_spiram) {
  429. spiram_wrap_sizes[0] = icache_wrap_size;
  430. } else {
  431. flash_wrap_sizes[0] = icache_wrap_size;
  432. }
  433. if (rodata_use_spiram) {
  434. if (drom0_in_icache) {
  435. spiram_wrap_sizes[0] = icache_wrap_size;
  436. } else {
  437. spiram_wrap_sizes[1] = dcache_wrap_size;
  438. flash_wrap_sizes[1] = dcache_wrap_size;
  439. }
  440. #ifdef CONFIG_EXT_RODATA_SUPPORT
  441. spiram_wrap_sizes[1] = dcache_wrap_size;
  442. #endif
  443. } else {
  444. if (drom0_in_icache) {
  445. flash_wrap_sizes[0] = icache_wrap_size;
  446. } else {
  447. flash_wrap_sizes[1] = dcache_wrap_size;
  448. }
  449. #ifdef CONFIG_EXT_RODATA_SUPPORT
  450. flash_wrap_sizes[1] = dcache_wrap_size;
  451. #endif
  452. }
  453. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  454. spiram_wrap_sizes[1] = dcache_wrap_size;
  455. #endif
  456. for (i = 0; i < 2; i++) {
  457. if (flash_wrap_sizes[i] != -1) {
  458. flash_count++;
  459. flash_wrap_size = flash_wrap_sizes[i];
  460. }
  461. }
  462. for (i = 0; i < 2; i++) {
  463. if (spiram_wrap_sizes[i] != -1) {
  464. spiram_count++;
  465. spiram_wrap_size = spiram_wrap_sizes[i];
  466. }
  467. }
  468. if (flash_count + spiram_count <= 2) {
  469. flash_spiram_wrap_together = false;
  470. } else {
  471. flash_spiram_wrap_together = true;
  472. }
  473. 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);
  474. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  475. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  476. if (spiram_wrap_size == 0) {
  477. return ESP_FAIL;
  478. }
  479. if (flash_spiram_wrap_together) {
  480. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  481. return ESP_FAIL;
  482. }
  483. }
  484. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  485. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  486. if (flash_wrap_size == 0) {
  487. return ESP_FAIL;
  488. }
  489. if (flash_spiram_wrap_together) {
  490. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  491. return ESP_FAIL;
  492. }
  493. }
  494. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  495. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  496. return ESP_FAIL;
  497. }
  498. #ifdef CONFIG_FLASHMODE_QIO
  499. flash_support_wrap = true;
  500. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  501. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  502. flash_support_wrap = false;
  503. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  504. }
  505. #else
  506. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  507. #endif
  508. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  509. extern bool psram_support_wrap_size(uint32_t wrap_size);
  510. if (!psram_support_wrap_size(spiram_wrap_size)) {
  511. spiram_support_wrap = false;
  512. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  513. }
  514. #endif
  515. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  516. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  517. return ESP_FAIL;
  518. }
  519. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  520. if (flash_support_wrap && flash_wrap_size > 0) {
  521. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  522. spi_flash_enable_wrap(flash_wrap_size);
  523. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  524. }
  525. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  526. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  527. if (spiram_support_wrap && spiram_wrap_size > 0) {
  528. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  529. psram_enable_wrap(spiram_wrap_size);
  530. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  531. }
  532. #endif
  533. return ESP_OK;
  534. }
  535. #endif
  536. void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
  537. {
  538. #if CONFIG_IDF_TARGET_ESP32
  539. uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid);
  540. cache_value &= DPORT_CACHE_GET_MASK(cpuid);
  541. // Re-enable cache on this CPU
  542. spi_flash_restore_cache(cpuid, cache_value);
  543. #else
  544. spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
  545. #endif
  546. }