cache_utils.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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_ESP32S2BETA
  25. #include "esp32s2beta/rom/spi_flash.h"
  26. #include "esp32s2beta/rom/cache.h"
  27. #endif
  28. #include <soc/soc.h>
  29. #include <soc/dport_reg.h>
  30. #include "sdkconfig.h"
  31. #include "esp_ipc.h"
  32. #include "esp_attr.h"
  33. #include "esp_intr_alloc.h"
  34. #include "esp_spi_flash.h"
  35. #include "esp_log.h"
  36. #include "soc/soc_memory_layout.h"
  37. static __attribute__((unused)) const char* TAG = "cache";
  38. #define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid
  39. #define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  40. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \
  41. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) )
  42. #define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  43. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \
  44. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0)))
  45. #define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP)
  46. #define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP)
  47. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t* saved_state);
  48. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  49. static uint32_t s_flash_op_cache_state[2];
  50. #ifndef CONFIG_FREERTOS_UNICORE
  51. static SemaphoreHandle_t s_flash_op_mutex;
  52. static volatile bool s_flash_op_can_start = false;
  53. static volatile bool s_flash_op_complete = false;
  54. #ifndef NDEBUG
  55. static volatile int s_flash_op_cpu = -1;
  56. #endif
  57. void spi_flash_init_lock(void)
  58. {
  59. s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
  60. assert(s_flash_op_mutex != NULL);
  61. }
  62. void spi_flash_op_lock(void)
  63. {
  64. xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
  65. }
  66. void spi_flash_op_unlock(void)
  67. {
  68. xSemaphoreGiveRecursive(s_flash_op_mutex);
  69. }
  70. /*
  71. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  72. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  73. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  74. when accessing psram from the former CPU.
  75. */
  76. void IRAM_ATTR spi_flash_op_block_func(void* arg)
  77. {
  78. // Disable scheduler on this CPU
  79. vTaskSuspendAll();
  80. // Restore interrupts that aren't located in IRAM
  81. esp_intr_noniram_disable();
  82. uint32_t cpuid = (uint32_t) arg;
  83. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  84. // CPU may reset the flag back to false before IPC task has a chance to check it
  85. // (if it is preempted by an ISR taking non-trivial amount of time)
  86. s_flash_op_complete = false;
  87. s_flash_op_can_start = true;
  88. while (!s_flash_op_complete) {
  89. // busy loop here and wait for the other CPU to finish flash operation
  90. }
  91. // Flash operation is complete, re-enable cache
  92. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  93. // Restore interrupts that aren't located in IRAM
  94. esp_intr_noniram_enable();
  95. // Re-enable scheduler
  96. xTaskResumeAll();
  97. }
  98. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  99. {
  100. assert(esp_ptr_in_dram((const void *)get_sp()));
  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_ESP32S2BETA
  265. *saved_state = Cache_Suspend_ICache();
  266. if (!Cache_Drom0_Using_ICache()) {
  267. *(saved_state + 1) = Cache_Suspend_DCache();
  268. }
  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_ESP32S2BETA
  286. Cache_Resume_ICache(saved_state);
  287. if (!Cache_Drom0_Using_ICache()) {
  288. Cache_Resume_DCache(s_flash_op_cache_state[1]);
  289. }
  290. #endif
  291. }
  292. IRAM_ATTR bool spi_flash_cache_enabled(void)
  293. {
  294. #if CONFIG_IDF_TARGET_ESP32
  295. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  296. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  297. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_ICACHE_CTRL_REG, DPORT_PRO_ICACHE_ENABLE) != 0);
  298. if (!Cache_Drom0_Using_ICache()) {
  299. result = result && (DPORT_REG_GET_BIT(DPORT_PRO_DCACHE_CTRL_REG, DPORT_PRO_DCACHE_ENABLE) != 0);
  300. }
  301. #endif
  302. #if portNUM_PROCESSORS == 2
  303. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  304. #endif
  305. return result;
  306. }
  307. #if CONFIG_IDF_TARGET_ESP32S2BETA
  308. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  309. {
  310. cache_size_t cache_size;
  311. cache_ways_t cache_ways;
  312. cache_line_size_t cache_line_size;
  313. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  314. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  315. cache_size = CACHE_SIZE_8KB;
  316. #else
  317. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  318. cache_size = CACHE_SIZE_16KB;
  319. #endif
  320. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_4WAYS
  321. cache_ways = CACHE_4WAYS_ASSOC;
  322. #else
  323. cache_ways = CACHE_8WAYS_ASSOC;
  324. #endif
  325. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  326. cache_line_size = CACHE_LINE_SIZE_16B;
  327. #elif CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_32B
  328. cache_line_size = CACHE_LINE_SIZE_32B;
  329. #else
  330. cache_line_size = CACHE_LINE_SIZE_64B;
  331. #endif
  332. ESP_EARLY_LOGI(TAG, "Instruction cache \t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16,cache_ways == CACHE_4WAYS_ASSOC ? 4: 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  333. Cache_Suspend_ICache();
  334. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  335. Cache_Invalidate_ICache_All();
  336. Cache_Resume_ICache(0);
  337. }
  338. IRAM_ATTR void esp_config_data_cache_mode(void)
  339. {
  340. cache_size_t cache_size;
  341. cache_ways_t cache_ways;
  342. cache_line_size_t cache_line_size;
  343. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  344. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  345. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  346. cache_size = CACHE_SIZE_8KB;
  347. #else
  348. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  349. cache_size = CACHE_SIZE_16KB;
  350. #endif
  351. #else
  352. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  353. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  354. cache_size = CACHE_SIZE_8KB;
  355. #else
  356. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  357. cache_size = CACHE_SIZE_16KB;
  358. #endif
  359. #endif
  360. #if CONFIG_ESP32S2_DATA_CACHE_4WAYS
  361. cache_ways = CACHE_4WAYS_ASSOC;
  362. #else
  363. cache_ways = CACHE_8WAYS_ASSOC;
  364. #endif
  365. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  366. cache_line_size = CACHE_LINE_SIZE_16B;
  367. #elif CONFIG_ESP32S2_DATA_CACHE_LINE_32B
  368. cache_line_size = CACHE_LINE_SIZE_32B;
  369. #else
  370. cache_line_size = CACHE_LINE_SIZE_64B;
  371. #endif
  372. ESP_EARLY_LOGI(TAG, "Data cache \t\t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16, cache_ways == CACHE_4WAYS_ASSOC ? 4: 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  373. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  374. Cache_Invalidate_DCache_All();
  375. }
  376. void esp_switch_rodata_to_dcache(void)
  377. {
  378. REG_CLR_BIT(DPORT_PRO_DCACHE_CTRL1_REG, DPORT_PRO_DCACHE_MASK_DROM0);
  379. Cache_Drom0_Source_DCache();
  380. MMU_Drom_ICache_Unmap();
  381. REG_SET_BIT(DPORT_PRO_ICACHE_CTRL1_REG, DPORT_PRO_ICACHE_MASK_DROM0);
  382. ESP_EARLY_LOGI(TAG, "Switch rodata load path to data cache.");
  383. }
  384. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  385. {
  386. uint32_t i_autoload, d_autoload;
  387. if (icache) {
  388. i_autoload = Cache_Suspend_ICache();
  389. }
  390. if (dcache) {
  391. d_autoload = Cache_Suspend_DCache();
  392. }
  393. REG_SET_BIT(DPORT_PRO_CACHE_WRAP_AROUND_CTRL_REG, DPORT_PRO_CACHE_FLASH_WRAP_AROUND);
  394. if (icache) {
  395. Cache_Resume_ICache(i_autoload);
  396. }
  397. if (dcache) {
  398. Cache_Resume_DCache(d_autoload);
  399. }
  400. }
  401. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  402. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  403. {
  404. uint32_t i_autoload, d_autoload;
  405. if (icache) {
  406. i_autoload = Cache_Suspend_ICache();
  407. }
  408. if (dcache) {
  409. d_autoload = Cache_Suspend_DCache();
  410. }
  411. REG_SET_BIT(DPORT_PRO_CACHE_WRAP_AROUND_CTRL_REG, DPORT_PRO_CACHE_SRAM_RD_WRAP_AROUND);
  412. if (icache) {
  413. Cache_Resume_ICache(i_autoload);
  414. }
  415. if (dcache) {
  416. Cache_Resume_DCache(d_autoload);
  417. }
  418. }
  419. #endif
  420. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  421. {
  422. int icache_wrap_size = 0, dcache_wrap_size = 0;
  423. int flash_wrap_sizes[2]={-1, -1}, spiram_wrap_sizes[2]={-1, -1};
  424. int flash_wrap_size = 0, spiram_wrap_size = 0;
  425. int flash_count = 0, spiram_count = 0;
  426. int i;
  427. bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
  428. if (icache_wrap_enable) {
  429. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  430. icache_wrap_size = 16;
  431. #elif CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_32B
  432. icache_wrap_size = 32;
  433. #else
  434. icache_wrap_size = 64;
  435. #endif
  436. }
  437. if (dcache_wrap_enable) {
  438. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  439. dcache_wrap_size = 16;
  440. #elif CONFIG_ESP32S2_DATA_CACHE_LINE_32B
  441. dcache_wrap_size = 32;
  442. #else
  443. dcache_wrap_size = 64;
  444. #endif
  445. }
  446. uint32_t instruction_use_spiram = 0;
  447. uint32_t rodata_use_spiram = 0;
  448. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  449. extern uint32_t esp_spiram_instruction_access_enabled();
  450. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  451. #endif
  452. #if CONFIG_SPIRAM_RODATA
  453. extern uint32_t esp_spiram_rodata_access_enabled();
  454. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  455. #endif
  456. if (instruction_use_spiram) {
  457. spiram_wrap_sizes[0] = icache_wrap_size;
  458. } else {
  459. flash_wrap_sizes[0] = icache_wrap_size;
  460. }
  461. if (rodata_use_spiram) {
  462. if (Cache_Drom0_Using_ICache()) {
  463. spiram_wrap_sizes[0] = icache_wrap_size;
  464. } else {
  465. spiram_wrap_sizes[1] = dcache_wrap_size;
  466. flash_wrap_sizes[1] = dcache_wrap_size;
  467. }
  468. #ifdef CONFIG_EXT_RODATA_SUPPORT
  469. spiram_wrap_sizes[1] = dcache_wrap_size;
  470. #endif
  471. } else {
  472. if (Cache_Drom0_Using_ICache()) {
  473. flash_wrap_sizes[0] = icache_wrap_size;
  474. } else {
  475. flash_wrap_sizes[1] = dcache_wrap_size;
  476. }
  477. #ifdef CONFIG_EXT_RODATA_SUPPORT
  478. flash_wrap_sizes[1] = dcache_wrap_size;
  479. #endif
  480. }
  481. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  482. spiram_wrap_sizes[1] = dcache_wrap_size;
  483. #endif
  484. for (i = 0; i < 2; i++) {
  485. if (flash_wrap_sizes[i] != -1) {
  486. flash_count++;
  487. flash_wrap_size = flash_wrap_sizes[i];
  488. }
  489. }
  490. for (i = 0; i < 2; i++) {
  491. if (spiram_wrap_sizes[i] != -1) {
  492. spiram_count++;
  493. spiram_wrap_size = spiram_wrap_sizes[i];
  494. }
  495. }
  496. if (flash_count + spiram_count <= 2) {
  497. flash_spiram_wrap_together = false;
  498. } else {
  499. flash_spiram_wrap_together = true;
  500. }
  501. 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);
  502. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  503. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  504. if (spiram_wrap_size == 0) {
  505. return ESP_FAIL;
  506. }
  507. if (flash_spiram_wrap_together) {
  508. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  509. return ESP_FAIL;
  510. }
  511. }
  512. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  513. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  514. if (flash_wrap_size == 0) {
  515. return ESP_FAIL;
  516. }
  517. if (flash_spiram_wrap_together) {
  518. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  519. return ESP_FAIL;
  520. }
  521. }
  522. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  523. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  524. return ESP_FAIL;
  525. }
  526. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  527. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  528. flash_support_wrap = false;
  529. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  530. }
  531. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  532. extern bool psram_support_wrap_size(uint32_t wrap_size);
  533. if (!psram_support_wrap_size(spiram_wrap_size)) {
  534. spiram_support_wrap = false;
  535. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  536. }
  537. #endif
  538. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  539. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  540. return ESP_FAIL;
  541. }
  542. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  543. if (flash_support_wrap && flash_wrap_size > 0) {
  544. ESP_EARLY_LOGI(TAG, "Flash wrap enabled.");
  545. spi_flash_enable_wrap(flash_wrap_size);
  546. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  547. }
  548. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  549. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  550. if (spiram_support_wrap && spiram_wrap_size > 0) {
  551. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled.");
  552. psram_enable_wrap(spiram_wrap_size);
  553. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  554. }
  555. #endif
  556. return ESP_OK;
  557. }
  558. #endif
  559. void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
  560. {
  561. #if CONFIG_IDF_TARGET_ESP32
  562. uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid);
  563. cache_value &= DPORT_CACHE_GET_MASK(cpuid);
  564. // Re-enable cache on this CPU
  565. spi_flash_restore_cache(cpuid, cache_value);
  566. #else
  567. spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
  568. #endif
  569. }