cache_utils.c 21 KB

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