cache_utils.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 "soc/dport_reg.h"
  23. #include <esp32/rom/spi_flash.h>
  24. #include <esp32/rom/cache.h>
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #include "esp32s2/rom/spi_flash.h"
  27. #include "esp32s2/rom/cache.h"
  28. #include "soc/extmem_reg.h"
  29. #include "soc/cache_memory.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S3
  31. #include "esp32s3/rom/spi_flash.h"
  32. #include "esp32s3/rom/cache.h"
  33. #include "soc/extmem_reg.h"
  34. #include "soc/cache_memory.h"
  35. #elif CONFIG_IDF_TARGET_ESP32C3
  36. #include "esp32c3/rom/spi_flash.h"
  37. #include "esp32c3/rom/cache.h"
  38. #include "soc/extmem_reg.h"
  39. #include "soc/cache_memory.h"
  40. #endif
  41. #include <soc/soc.h>
  42. #include "sdkconfig.h"
  43. #ifndef CONFIG_FREERTOS_UNICORE
  44. #include "esp_ipc.h"
  45. #endif
  46. #include "esp_attr.h"
  47. #include "esp_intr_alloc.h"
  48. #include "esp_spi_flash.h"
  49. #include "esp_log.h"
  50. static __attribute__((unused)) const char *TAG = "cache";
  51. #define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid
  52. #define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  53. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \
  54. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) )
  55. #define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  56. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \
  57. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0)))
  58. #define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP)
  59. #define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP)
  60. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
  61. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  62. static uint32_t s_flash_op_cache_state[2];
  63. #ifndef CONFIG_FREERTOS_UNICORE
  64. static SemaphoreHandle_t s_flash_op_mutex;
  65. static volatile bool s_flash_op_can_start = false;
  66. static volatile bool s_flash_op_complete = false;
  67. #ifndef NDEBUG
  68. static volatile int s_flash_op_cpu = -1;
  69. #endif
  70. void spi_flash_init_lock(void)
  71. {
  72. s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
  73. assert(s_flash_op_mutex != NULL);
  74. }
  75. void spi_flash_op_lock(void)
  76. {
  77. xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
  78. }
  79. void spi_flash_op_unlock(void)
  80. {
  81. xSemaphoreGiveRecursive(s_flash_op_mutex);
  82. }
  83. /*
  84. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  85. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  86. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  87. when accessing psram from the former CPU.
  88. */
  89. void IRAM_ATTR spi_flash_op_block_func(void *arg)
  90. {
  91. // Disable scheduler on this CPU
  92. vTaskSuspendAll();
  93. // Restore interrupts that aren't located in IRAM
  94. esp_intr_noniram_disable();
  95. uint32_t cpuid = (uint32_t) arg;
  96. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  97. // CPU may reset the flag back to false before IPC task has a chance to check it
  98. // (if it is preempted by an ISR taking non-trivial amount of time)
  99. s_flash_op_complete = false;
  100. s_flash_op_can_start = true;
  101. while (!s_flash_op_complete) {
  102. // busy loop here and wait for the other CPU to finish flash operation
  103. }
  104. // Flash operation is complete, re-enable cache
  105. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  106. // Restore interrupts that aren't located in IRAM
  107. esp_intr_noniram_enable();
  108. // Re-enable scheduler
  109. xTaskResumeAll();
  110. }
  111. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  112. {
  113. assert(esp_ptr_in_dram((const void *)get_sp()));
  114. spi_flash_op_lock();
  115. const uint32_t cpuid = xPortGetCoreID();
  116. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  117. #ifndef NDEBUG
  118. // For sanity check later: record the CPU which has started doing flash operation
  119. assert(s_flash_op_cpu == -1);
  120. s_flash_op_cpu = cpuid;
  121. #endif
  122. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  123. // Scheduler hasn't been started yet, it means that spi_flash API is being
  124. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  125. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  126. // which is in IRAM. So it is safe to disable cache for the other_cpuid here.
  127. assert(other_cpuid == 1);
  128. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  129. } else {
  130. // Temporarily raise current task priority to prevent a deadlock while
  131. // waiting for IPC task to start on the other CPU
  132. int old_prio = uxTaskPriorityGet(NULL);
  133. vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1);
  134. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  135. // disable cache there and block other tasks from executing.
  136. s_flash_op_can_start = false;
  137. esp_err_t ret = esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void *) other_cpuid);
  138. assert(ret == ESP_OK);
  139. while (!s_flash_op_can_start) {
  140. // Busy loop and wait for spi_flash_op_block_func to disable cache
  141. // on the other CPU
  142. }
  143. // Disable scheduler on the current CPU
  144. vTaskSuspendAll();
  145. // Can now set the priority back to the normal one
  146. vTaskPrioritySet(NULL, old_prio);
  147. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  148. // occupied by highest priority task
  149. assert(xPortGetCoreID() == cpuid);
  150. }
  151. // Kill interrupts that aren't located in IRAM
  152. esp_intr_noniram_disable();
  153. // This CPU executes this routine, with non-IRAM interrupts and the scheduler
  154. // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
  155. // with non-iram interrupts and the scheduler disabled. None of these CPUs will
  156. // touch external RAM or flash this way, so we can safely disable caches.
  157. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  158. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  159. }
  160. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  161. {
  162. const uint32_t cpuid = xPortGetCoreID();
  163. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  164. #ifndef NDEBUG
  165. // Sanity check: flash operation ends on the same CPU as it has started
  166. assert(cpuid == s_flash_op_cpu);
  167. // More sanity check: if scheduler isn't started, only CPU0 can call this.
  168. assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
  169. s_flash_op_cpu = -1;
  170. #endif
  171. // Re-enable cache on both CPUs. After this, cache (flash and external RAM) should work again.
  172. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  173. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  174. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  175. // Signal to spi_flash_op_block_task that flash operation is complete
  176. s_flash_op_complete = true;
  177. }
  178. // Re-enable non-iram interrupts
  179. esp_intr_noniram_enable();
  180. // Resume tasks on the current CPU, if the scheduler has started.
  181. // NOTE: enabling non-IRAM interrupts has to happen before this,
  182. // because once the scheduler has started, due to preemption the
  183. // current task can end up being moved to the other CPU.
  184. // But esp_intr_noniram_enable has to be called on the same CPU which
  185. // called esp_intr_noniram_disable
  186. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  187. xTaskResumeAll();
  188. }
  189. // Release API lock
  190. spi_flash_op_unlock();
  191. }
  192. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  193. {
  194. const uint32_t cpuid = xPortGetCoreID();
  195. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  196. // do not care about other CPU, it was halted upon entering panic handler
  197. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  198. // Kill interrupts that aren't located in IRAM
  199. esp_intr_noniram_disable();
  200. // Disable cache on this CPU as well
  201. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  202. }
  203. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  204. {
  205. const uint32_t cpuid = xPortGetCoreID();
  206. // Re-enable cache on this CPU
  207. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  208. // Re-enable non-iram interrupts
  209. esp_intr_noniram_enable();
  210. }
  211. #else // CONFIG_FREERTOS_UNICORE
  212. void spi_flash_init_lock(void)
  213. {
  214. }
  215. void spi_flash_op_lock(void)
  216. {
  217. vTaskSuspendAll();
  218. }
  219. void spi_flash_op_unlock(void)
  220. {
  221. xTaskResumeAll();
  222. }
  223. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  224. {
  225. spi_flash_op_lock();
  226. esp_intr_noniram_disable();
  227. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  228. }
  229. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  230. {
  231. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  232. esp_intr_noniram_enable();
  233. spi_flash_op_unlock();
  234. }
  235. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  236. {
  237. // Kill interrupts that aren't located in IRAM
  238. esp_intr_noniram_disable();
  239. // Disable cache on this CPU as well
  240. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  241. }
  242. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  243. {
  244. // Re-enable cache on this CPU
  245. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  246. // Re-enable non-iram interrupts
  247. esp_intr_noniram_enable();
  248. }
  249. #endif // CONFIG_FREERTOS_UNICORE
  250. /**
  251. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  252. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  253. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  254. */
  255. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
  256. {
  257. #if CONFIG_IDF_TARGET_ESP32
  258. uint32_t ret = 0;
  259. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  260. if (cpuid == 0) {
  261. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  262. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  263. ;
  264. }
  265. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  266. }
  267. #if !CONFIG_FREERTOS_UNICORE
  268. else {
  269. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  270. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  271. ;
  272. }
  273. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  274. }
  275. #endif
  276. *saved_state = ret;
  277. #elif CONFIG_IDF_TARGET_ESP32S2
  278. *saved_state = Cache_Suspend_ICache();
  279. #elif CONFIG_IDF_TARGET_ESP32S3
  280. uint32_t icache_state, dcache_state;
  281. icache_state = Cache_Suspend_ICache() << 16;
  282. dcache_state = Cache_Suspend_DCache();
  283. *saved_state = icache_state | dcache_state;
  284. #elif CONFIG_IDF_TARGET_ESP32C3
  285. uint32_t icache_state;
  286. icache_state = Cache_Suspend_ICache() << 16;
  287. *saved_state = icache_state;
  288. #endif
  289. }
  290. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  291. {
  292. #if CONFIG_IDF_TARGET_ESP32
  293. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  294. if (cpuid == 0) {
  295. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  296. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  297. }
  298. #if !CONFIG_FREERTOS_UNICORE
  299. else {
  300. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  301. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  302. }
  303. #endif
  304. #elif CONFIG_IDF_TARGET_ESP32S2
  305. Cache_Resume_ICache(saved_state);
  306. #elif CONFIG_IDF_TARGET_ESP32S3
  307. Cache_Resume_DCache(saved_state & 0xffff);
  308. Cache_Resume_ICache(saved_state >> 16);
  309. #elif CONFIG_IDF_TARGET_ESP32C3
  310. Cache_Resume_ICache(saved_state >> 16);
  311. #endif
  312. }
  313. IRAM_ATTR bool spi_flash_cache_enabled(void)
  314. {
  315. #if CONFIG_IDF_TARGET_ESP32
  316. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  317. #if portNUM_PROCESSORS == 2
  318. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  319. #endif
  320. #elif CONFIG_IDF_TARGET_ESP32S2
  321. bool result = (REG_GET_BIT(EXTMEM_PRO_ICACHE_CTRL_REG, EXTMEM_PRO_ICACHE_ENABLE) != 0);
  322. #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  323. bool result = (REG_GET_BIT(EXTMEM_ICACHE_CTRL_REG, EXTMEM_ICACHE_ENABLE) != 0);
  324. #endif
  325. return result;
  326. }
  327. #if CONFIG_IDF_TARGET_ESP32S2
  328. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  329. {
  330. cache_size_t cache_size;
  331. cache_ways_t cache_ways;
  332. cache_line_size_t cache_line_size;
  333. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  334. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  335. cache_size = CACHE_SIZE_8KB;
  336. #else
  337. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  338. cache_size = CACHE_SIZE_16KB;
  339. #endif
  340. cache_ways = CACHE_4WAYS_ASSOC;
  341. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  342. cache_line_size = CACHE_LINE_SIZE_16B;
  343. #else
  344. cache_line_size = CACHE_LINE_SIZE_32B;
  345. #endif
  346. 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);
  347. Cache_Suspend_ICache();
  348. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  349. Cache_Invalidate_ICache_All();
  350. Cache_Resume_ICache(0);
  351. }
  352. IRAM_ATTR void esp_config_data_cache_mode(void)
  353. {
  354. cache_size_t cache_size;
  355. cache_ways_t cache_ways;
  356. cache_line_size_t cache_line_size;
  357. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  358. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  359. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  360. cache_size = CACHE_SIZE_8KB;
  361. #else
  362. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  363. cache_size = CACHE_SIZE_16KB;
  364. #endif
  365. #else
  366. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  367. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  368. cache_size = CACHE_SIZE_8KB;
  369. #else
  370. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  371. cache_size = CACHE_SIZE_16KB;
  372. #endif
  373. #endif
  374. cache_ways = CACHE_4WAYS_ASSOC;
  375. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  376. cache_line_size = CACHE_LINE_SIZE_16B;
  377. #else
  378. cache_line_size = CACHE_LINE_SIZE_32B;
  379. #endif
  380. 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);
  381. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  382. Cache_Invalidate_DCache_All();
  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(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_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(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_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. uint32_t drom0_in_icache = 1;//always 1 in esp32s2
  429. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  430. drom0_in_icache = 0;
  431. #endif
  432. if (icache_wrap_enable) {
  433. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B
  434. icache_wrap_size = 16;
  435. #else
  436. icache_wrap_size = 32;
  437. #endif
  438. }
  439. if (dcache_wrap_enable) {
  440. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B || CONFIG_ESP32S3_DATA_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B
  441. dcache_wrap_size = 16;
  442. #else
  443. dcache_wrap_size = 32;
  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(void);
  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(void);
  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 (drom0_in_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 (drom0_in_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. #ifdef CONFIG_FLASHMODE_QIO
  527. flash_support_wrap = true;
  528. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  529. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  530. flash_support_wrap = false;
  531. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  532. }
  533. #else
  534. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  535. #endif
  536. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  537. extern bool psram_support_wrap_size(uint32_t wrap_size);
  538. if (!psram_support_wrap_size(spiram_wrap_size)) {
  539. spiram_support_wrap = false;
  540. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  541. }
  542. #endif
  543. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  544. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  545. return ESP_FAIL;
  546. }
  547. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  548. if (flash_support_wrap && flash_wrap_size > 0) {
  549. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  550. spi_flash_enable_wrap(flash_wrap_size);
  551. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  552. }
  553. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  554. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  555. if (spiram_support_wrap && spiram_wrap_size > 0) {
  556. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  557. psram_enable_wrap(spiram_wrap_size);
  558. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  559. }
  560. #endif
  561. return ESP_OK;
  562. }
  563. #endif
  564. #if CONFIG_IDF_TARGET_ESP32S3
  565. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  566. {
  567. cache_size_t cache_size;
  568. cache_ways_t cache_ways;
  569. cache_line_size_t cache_line_size;
  570. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_16KB
  571. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_INVALID);
  572. cache_size = CACHE_SIZE_HALF;
  573. #else
  574. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1);
  575. cache_size = CACHE_SIZE_FULL;
  576. #endif
  577. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_4WAYS
  578. cache_ways = CACHE_4WAYS_ASSOC;
  579. #else
  580. cache_ways = CACHE_8WAYS_ASSOC;
  581. #endif
  582. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  583. cache_line_size = CACHE_LINE_SIZE_16B;
  584. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  585. cache_line_size = CACHE_LINE_SIZE_32B;
  586. #else
  587. cache_line_size = CACHE_LINE_SIZE_64B;
  588. #endif
  589. ESP_EARLY_LOGI(TAG, "Instruction cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 16 : 32, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  590. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  591. Cache_Invalidate_ICache_All();
  592. extern void Cache_Enable_ICache(uint32_t autoload);
  593. Cache_Enable_ICache(0);
  594. }
  595. IRAM_ATTR void esp_config_data_cache_mode(void)
  596. {
  597. cache_size_t cache_size;
  598. cache_ways_t cache_ways;
  599. cache_line_size_t cache_line_size;
  600. #if CONFIG_ESP32S3_DATA_CACHE_32KB
  601. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK1, CACHE_MEMORY_INVALID);
  602. cache_size = CACHE_SIZE_HALF;
  603. #else
  604. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1);
  605. cache_size = CACHE_SIZE_FULL;
  606. #endif
  607. #if CONFIG_ESP32S3_DATA_CACHE_4WAYS
  608. cache_ways = CACHE_4WAYS_ASSOC;
  609. #else
  610. cache_ways = CACHE_8WAYS_ASSOC;
  611. #endif
  612. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  613. cache_line_size = CACHE_LINE_SIZE_16B;
  614. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  615. cache_line_size = CACHE_LINE_SIZE_32B;
  616. #else
  617. cache_line_size = CACHE_LINE_SIZE_64B;
  618. #endif
  619. // ESP_EARLY_LOGI(TAG, "Data cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 32 : 64, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  620. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  621. Cache_Invalidate_DCache_All();
  622. }
  623. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  624. {
  625. uint32_t i_autoload, d_autoload;
  626. if (icache) {
  627. i_autoload = Cache_Suspend_ICache();
  628. }
  629. if (dcache) {
  630. d_autoload = Cache_Suspend_DCache();
  631. }
  632. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  633. if (icache) {
  634. Cache_Resume_ICache(i_autoload);
  635. }
  636. if (dcache) {
  637. Cache_Resume_DCache(d_autoload);
  638. }
  639. }
  640. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  641. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  642. {
  643. uint32_t i_autoload, d_autoload;
  644. if (icache) {
  645. i_autoload = Cache_Suspend_ICache();
  646. }
  647. if (dcache) {
  648. d_autoload = Cache_Suspend_DCache();
  649. }
  650. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_SRAM_RD_WRAP_AROUND);
  651. if (icache) {
  652. Cache_Resume_ICache(i_autoload);
  653. }
  654. if (dcache) {
  655. Cache_Resume_DCache(d_autoload);
  656. }
  657. }
  658. #endif
  659. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  660. {
  661. int icache_wrap_size = 0, dcache_wrap_size = 0;
  662. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  663. int flash_wrap_size = 0, spiram_wrap_size = 0;
  664. int flash_count = 0, spiram_count = 0;
  665. int i;
  666. bool flash_spiram_wrap_together, flash_support_wrap = false, spiram_support_wrap = true;
  667. uint32_t drom0_in_icache = 0;//always 0 in chip7.2.4
  668. if (icache_wrap_enable) {
  669. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  670. icache_wrap_size = 16;
  671. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  672. icache_wrap_size = 32;
  673. #else
  674. icache_wrap_size = 64;
  675. #endif
  676. }
  677. if (dcache_wrap_enable) {
  678. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  679. dcache_wrap_size = 16;
  680. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  681. dcache_wrap_size = 32;
  682. #else
  683. dcache_wrap_size = 64;
  684. #endif
  685. }
  686. uint32_t instruction_use_spiram = 0;
  687. uint32_t rodata_use_spiram = 0;
  688. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  689. extern uint32_t esp_spiram_instruction_access_enabled();
  690. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  691. #endif
  692. #if CONFIG_SPIRAM_RODATA
  693. extern uint32_t esp_spiram_rodata_access_enabled();
  694. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  695. #endif
  696. if (instruction_use_spiram) {
  697. spiram_wrap_sizes[0] = icache_wrap_size;
  698. } else {
  699. flash_wrap_sizes[0] = icache_wrap_size;
  700. }
  701. if (rodata_use_spiram) {
  702. if (drom0_in_icache) {
  703. spiram_wrap_sizes[0] = icache_wrap_size;
  704. } else {
  705. spiram_wrap_sizes[1] = dcache_wrap_size;
  706. }
  707. #ifdef CONFIG_EXT_RODATA_SUPPORT
  708. spiram_wrap_sizes[1] = dcache_wrap_size;
  709. #endif
  710. } else {
  711. if (drom0_in_icache) {
  712. flash_wrap_sizes[0] = icache_wrap_size;
  713. } else {
  714. flash_wrap_sizes[1] = dcache_wrap_size;
  715. }
  716. #ifdef CONFIG_EXT_RODATA_SUPPORT
  717. flash_wrap_sizes[1] = dcache_wrap_size;
  718. #endif
  719. }
  720. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  721. spiram_wrap_sizes[1] = dcache_wrap_size;
  722. #endif
  723. for (i = 0; i < 2; i++) {
  724. if (flash_wrap_sizes[i] != -1) {
  725. flash_count++;
  726. flash_wrap_size = flash_wrap_sizes[i];
  727. }
  728. }
  729. for (i = 0; i < 2; i++) {
  730. if (spiram_wrap_sizes[i] != -1) {
  731. spiram_count++;
  732. spiram_wrap_size = spiram_wrap_sizes[i];
  733. }
  734. }
  735. if (flash_count + spiram_count <= 2) {
  736. flash_spiram_wrap_together = false;
  737. } else {
  738. flash_spiram_wrap_together = true;
  739. }
  740. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  741. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  742. if (spiram_wrap_size == 0) {
  743. return ESP_FAIL;
  744. }
  745. if (flash_spiram_wrap_together) {
  746. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  747. return ESP_FAIL;
  748. }
  749. }
  750. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  751. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  752. if (flash_wrap_size == 0) {
  753. return ESP_FAIL;
  754. }
  755. if (flash_spiram_wrap_together) {
  756. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  757. return ESP_FAIL;
  758. }
  759. }
  760. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  761. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  762. return ESP_FAIL;
  763. }
  764. #ifdef CONFIG_FLASHMODE_QIO
  765. flash_support_wrap = true;
  766. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  767. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  768. flash_support_wrap = false;
  769. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  770. }
  771. #else
  772. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  773. #endif
  774. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  775. extern bool psram_support_wrap_size(uint32_t wrap_size);
  776. if (!psram_support_wrap_size(spiram_wrap_size)) {
  777. spiram_support_wrap = false;
  778. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  779. }
  780. #endif
  781. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  782. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  783. return ESP_FAIL;
  784. }
  785. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  786. if (flash_support_wrap && flash_wrap_size > 0) {
  787. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  788. spi_flash_enable_wrap(flash_wrap_size);
  789. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  790. }
  791. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  792. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  793. if (spiram_support_wrap && spiram_wrap_size > 0) {
  794. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  795. psram_enable_wrap(spiram_wrap_size);
  796. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  797. }
  798. #endif
  799. return ESP_OK;
  800. }
  801. #endif
  802. void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
  803. {
  804. #if CONFIG_IDF_TARGET_ESP32
  805. uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid);
  806. cache_value &= DPORT_CACHE_GET_MASK(cpuid);
  807. // Re-enable cache on this CPU
  808. spi_flash_restore_cache(cpuid, cache_value);
  809. #else
  810. spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
  811. #endif
  812. }