cache_utils.c 21 KB

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