cache_utils.c 21 KB

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