cache_utils.c 22 KB

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