1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
11 
12 #include <freertos/FreeRTOS.h>
13 #include <freertos/task.h>
14 #include <freertos/semphr.h>
15 #if CONFIG_IDF_TARGET_ESP32
16 #include "soc/dport_reg.h"
17 #include <esp32/rom/cache.h>
18 #elif CONFIG_IDF_TARGET_ESP32S2
19 #include "esp32s2/rom/cache.h"
20 #include "soc/extmem_reg.h"
21 #include "soc/ext_mem_defs.h"
22 #elif CONFIG_IDF_TARGET_ESP32S3
23 #include "esp32s3/rom/cache.h"
24 #include "soc/extmem_reg.h"
25 #include "soc/ext_mem_defs.h"
26 #elif CONFIG_IDF_TARGET_ESP32C3
27 #include "esp32c3/rom/cache.h"
28 #include "soc/extmem_reg.h"
29 #include "soc/ext_mem_defs.h"
30 #elif CONFIG_IDF_TARGET_ESP32C2
31 #include "esp32c2/rom/cache.h"
32 #include "soc/extmem_reg.h"
33 #include "soc/ext_mem_defs.h"
34 #elif CONFIG_IDF_TARGET_ESP32C6
35 #include "esp32c6/rom/cache.h"
36 #include "soc/extmem_reg.h"
37 #include "soc/ext_mem_defs.h"
38 #elif CONFIG_IDF_TARGET_ESP32H2
39 #include "esp32h2/rom/cache.h"
40 #include "soc/extmem_reg.h"
41 #include "soc/ext_mem_defs.h"
42 #endif
43 #include "esp_rom_spiflash.h"
44 #include "hal/cache_hal.h"
45 #include "hal/cache_ll.h"
46 #include <soc/soc.h>
47 #include "sdkconfig.h"
48 #ifndef CONFIG_FREERTOS_UNICORE
49 #include "esp_private/esp_ipc.h"
50 #endif
51 #include "esp_attr.h"
52 #include "esp_memory_utils.h"
53 #include "esp_intr_alloc.h"
54 #include "spi_flash_mmap.h"
55 #include "spi_flash_override.h"
56 #include "esp_private/spi_flash_os.h"
57 #include "esp_private/freertos_idf_additions_priv.h"
58 #include "esp_log.h"
59 #include "esp_cpu.h"
60 
61 static __attribute__((unused)) const char *TAG = "cache";
62 
63 
64 /**
65  * These two shouldn't be declared as static otherwise if `CONFIG_SPI_FLASH_ROM_IMPL` is enabled,
66  * they won't get replaced by the rom version
67  */
68 void spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
69 void spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
70 
71 // Used only on ROM impl. in idf, this param unused, cache status hold by hal
72 static uint32_t s_flash_op_cache_state[2];
73 
74 
75 #ifndef CONFIG_FREERTOS_UNICORE
76 static SemaphoreHandle_t s_flash_op_mutex;
77 static volatile bool s_flash_op_can_start = false;
78 static volatile bool s_flash_op_complete = false;
79 #ifndef NDEBUG
80 static volatile int s_flash_op_cpu = -1;
81 #endif
82 
esp_task_stack_is_sane_cache_disabled(void)83 static inline bool esp_task_stack_is_sane_cache_disabled(void)
84 {
85     const void *sp = (const void *)esp_cpu_get_sp();
86 
87     return esp_ptr_in_dram(sp)
88 #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
89         || esp_ptr_in_rtc_dram_fast(sp)
90 #endif
91     ;
92 }
93 
spi_flash_init_lock(void)94 void spi_flash_init_lock(void)
95 {
96     s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
97     assert(s_flash_op_mutex != NULL);
98 }
99 
spi_flash_op_lock(void)100 void spi_flash_op_lock(void)
101 {
102     xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
103 }
104 
spi_flash_op_unlock(void)105 void spi_flash_op_unlock(void)
106 {
107     xSemaphoreGiveRecursive(s_flash_op_mutex);
108 }
109 /*
110  If you're going to modify this, keep in mind that while the flash caches of the pro and app
111  cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
112  with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
113  when accessing psram from the former CPU.
114 */
115 
spi_flash_op_block_func(void * arg)116 void IRAM_ATTR spi_flash_op_block_func(void *arg)
117 {
118     // Disable scheduler on this CPU
119 #ifdef CONFIG_FREERTOS_SMP
120     /*
121     Note: FreeRTOS SMP has changed the behavior of scheduler suspension. But the vTaskPreemptionDisable() function should
122     achieve the same affect as before (i.e., prevent the current task from being preempted).
123     */
124     vTaskPreemptionDisable(NULL);
125 #else
126     vTaskSuspendAll();
127 #endif // CONFIG_FREERTOS_SMP
128     // Restore interrupts that aren't located in IRAM
129     esp_intr_noniram_disable();
130     uint32_t cpuid = (uint32_t) arg;
131     // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
132     // CPU may reset the flag back to false before IPC task has a chance to check it
133     // (if it is preempted by an ISR taking non-trivial amount of time)
134     s_flash_op_complete = false;
135     s_flash_op_can_start = true;
136     while (!s_flash_op_complete) {
137         // busy loop here and wait for the other CPU to finish flash operation
138     }
139     // Flash operation is complete, re-enable cache
140     spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
141     // Restore interrupts that aren't located in IRAM
142     esp_intr_noniram_enable();
143 #ifdef CONFIG_FREERTOS_SMP
144     //Note: Scheduler suspension behavior changed in FreeRTOS SMP
145     vTaskPreemptionEnable(NULL);
146 #else
147     // Re-enable scheduler
148     xTaskResumeAll();
149 #endif // CONFIG_FREERTOS_SMP
150 }
151 
spi_flash_disable_interrupts_caches_and_other_cpu(void)152 void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
153 {
154     assert(esp_task_stack_is_sane_cache_disabled());
155 
156     spi_flash_op_lock();
157 
158     int cpuid = xPortGetCoreID();
159     uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
160 #ifndef NDEBUG
161     // For sanity check later: record the CPU which has started doing flash operation
162     assert(s_flash_op_cpu == -1);
163     s_flash_op_cpu = cpuid;
164 #endif
165 
166     if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
167         // Scheduler hasn't been started yet, it means that spi_flash API is being
168         // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
169         // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
170         // which is in IRAM. So it is safe to disable cache for the other_cpuid after
171         // esp_intr_noniram_disable.
172         assert(other_cpuid == 1);
173     } else {
174         bool ipc_call_was_send_to_other_cpu;
175         do {
176             #ifdef CONFIG_FREERTOS_SMP
177                 //Note: Scheduler suspension behavior changed in FreeRTOS SMP
178                 vTaskPreemptionDisable(NULL);
179             #else
180                 // Disable scheduler on the current CPU
181                 vTaskSuspendAll();
182             #endif // CONFIG_FREERTOS_SMP
183 
184             cpuid = xPortGetCoreID();
185             other_cpuid = (cpuid == 0) ? 1 : 0;
186             #ifndef NDEBUG
187                 s_flash_op_cpu = cpuid;
188             #endif
189 
190             s_flash_op_can_start = false;
191 
192             ipc_call_was_send_to_other_cpu = esp_ipc_call_nonblocking(other_cpuid, &spi_flash_op_block_func, (void *) other_cpuid) == ESP_OK;
193 
194             if (!ipc_call_was_send_to_other_cpu) {
195                 // IPC call was not send to other cpu because another nonblocking API is running now.
196                 // Enable the Scheduler again will not help the IPC to speed it up
197                 // but there is a benefit to schedule to a higher priority task before the nonblocking running IPC call is done.
198                 #ifdef CONFIG_FREERTOS_SMP
199                     //Note: Scheduler suspension behavior changed in FreeRTOS SMP
200                     vTaskPreemptionEnable(NULL);
201                 #else
202                     xTaskResumeAll();
203                 #endif // CONFIG_FREERTOS_SMP
204             }
205         } while (!ipc_call_was_send_to_other_cpu);
206 
207         while (!s_flash_op_can_start) {
208             // Busy loop and wait for spi_flash_op_block_func to disable cache
209             // on the other CPU
210         }
211     }
212 
213     // Kill interrupts that aren't located in IRAM
214     esp_intr_noniram_disable();
215     // This CPU executes this routine, with non-IRAM interrupts and the scheduler
216     // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
217     // with non-iram interrupts and the scheduler disabled. None of these CPUs will
218     // touch external RAM or flash this way, so we can safely disable caches.
219     spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
220 #if SOC_IDCACHE_PER_CORE
221     //only needed if cache(s) is per core
222     spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
223 #endif
224 }
225 
spi_flash_enable_interrupts_caches_and_other_cpu(void)226 void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
227 {
228     const int cpuid = xPortGetCoreID();
229 
230 #ifndef NDEBUG
231     // Sanity check: flash operation ends on the same CPU as it has started
232     assert(cpuid == s_flash_op_cpu);
233     // More sanity check: if scheduler isn't started, only CPU0 can call this.
234     assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
235     s_flash_op_cpu = -1;
236 #endif
237 
238     // Re-enable cache. After this, cache (flash and external RAM) should work again.
239     spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
240 #if SOC_IDCACHE_PER_CORE
241     //only needed if cache(s) is per core
242     const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
243     spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
244 #endif
245 
246     if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
247         // Signal to spi_flash_op_block_task that flash operation is complete
248         s_flash_op_complete = true;
249     }
250 
251     // Re-enable non-iram interrupts
252     esp_intr_noniram_enable();
253 
254     // Resume tasks on the current CPU, if the scheduler has started.
255     // NOTE: enabling non-IRAM interrupts has to happen before this,
256     // because once the scheduler has started, due to preemption the
257     // current task can end up being moved to the other CPU.
258     // But esp_intr_noniram_enable has to be called on the same CPU which
259     // called esp_intr_noniram_disable
260     if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
261 #ifdef CONFIG_FREERTOS_SMP
262         //Note: Scheduler suspension behavior changed in FreeRTOS SMP
263         vTaskPreemptionEnable(NULL);
264 #else
265         xTaskResumeAll();
266 #endif // CONFIG_FREERTOS_SMP
267     }
268     // Release API lock
269     spi_flash_op_unlock();
270 }
271 
spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)272 void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
273 {
274     const uint32_t cpuid = xPortGetCoreID();
275     const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
276 
277     // do not care about other CPU, it was halted upon entering panic handler
278     spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
279     // Kill interrupts that aren't located in IRAM
280     esp_intr_noniram_disable();
281     // Disable cache on this CPU as well
282     spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
283 }
284 
spi_flash_enable_interrupts_caches_no_os(void)285 void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
286 {
287     const uint32_t cpuid = xPortGetCoreID();
288 
289     // Re-enable cache on this CPU
290     spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
291     // Re-enable non-iram interrupts
292     esp_intr_noniram_enable();
293 }
294 
295 #else // CONFIG_FREERTOS_UNICORE
296 
spi_flash_init_lock(void)297 void spi_flash_init_lock(void)
298 {
299 }
300 
spi_flash_op_lock(void)301 void spi_flash_op_lock(void)
302 {
303 #ifdef CONFIG_FREERTOS_SMP
304     if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
305         //Note: Scheduler suspension behavior changed in FreeRTOS SMP
306         vTaskPreemptionDisable(NULL);
307     }
308 #else
309     vTaskSuspendAll();
310 #endif // CONFIG_FREERTOS_SMP
311 }
312 
spi_flash_op_unlock(void)313 void spi_flash_op_unlock(void)
314 {
315 #ifdef CONFIG_FREERTOS_SMP
316     if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
317         //Note: Scheduler suspension behavior changed in FreeRTOS SMP
318         vTaskPreemptionEnable(NULL);
319     }
320 #else
321     xTaskResumeAll();
322 #endif // CONFIG_FREERTOS_SMP
323 }
324 
325 
spi_flash_disable_interrupts_caches_and_other_cpu(void)326 void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
327 {
328     spi_flash_op_lock();
329     esp_intr_noniram_disable();
330     spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
331 }
332 
spi_flash_enable_interrupts_caches_and_other_cpu(void)333 void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
334 {
335     spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
336     esp_intr_noniram_enable();
337     spi_flash_op_unlock();
338 }
339 
spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)340 void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
341 {
342     // Kill interrupts that aren't located in IRAM
343     esp_intr_noniram_disable();
344     // Disable cache on this CPU as well
345     spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
346 }
347 
spi_flash_enable_interrupts_caches_no_os(void)348 void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
349 {
350     // Re-enable cache on this CPU
351     spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
352     // Re-enable non-iram interrupts
353     esp_intr_noniram_enable();
354 }
355 
356 #endif // CONFIG_FREERTOS_UNICORE
357 
358 
spi_flash_enable_cache(uint32_t cpuid)359 void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
360 {
361 #if CONFIG_IDF_TARGET_ESP32
362     uint32_t cache_value = cache_ll_l1_get_enabled_bus(cpuid);
363 
364     // Re-enable cache on this CPU
365     spi_flash_restore_cache(cpuid, cache_value);
366 #else
367     spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
368 #endif
369 }
370 
371 /**
372  * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
373  * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
374  * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
375  */
spi_flash_disable_cache(uint32_t cpuid,uint32_t * saved_state)376 void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
377 {
378     cache_hal_suspend(CACHE_TYPE_ALL);
379 }
380 
spi_flash_restore_cache(uint32_t cpuid,uint32_t saved_state)381 void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
382 {
383     cache_hal_resume(CACHE_TYPE_ALL);
384 }
385 
spi_flash_cache_enabled(void)386 bool IRAM_ATTR spi_flash_cache_enabled(void)
387 {
388     return cache_hal_is_cache_enabled(CACHE_TYPE_ALL);
389 }
390 
391 #if CONFIG_IDF_TARGET_ESP32S2
esp_config_instruction_cache_mode(void)392 IRAM_ATTR void esp_config_instruction_cache_mode(void)
393 {
394     cache_size_t cache_size;
395     cache_ways_t cache_ways;
396     cache_line_size_t cache_line_size;
397 
398 #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
399     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
400     cache_size = CACHE_SIZE_8KB;
401 #else
402     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
403     cache_size = CACHE_SIZE_16KB;
404 #endif
405     cache_ways = CACHE_4WAYS_ASSOC;
406 #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
407     cache_line_size = CACHE_LINE_SIZE_16B;
408 #else
409     cache_line_size = CACHE_LINE_SIZE_32B;
410 #endif
411     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);
412     Cache_Suspend_ICache();
413     Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
414     Cache_Invalidate_ICache_All();
415     Cache_Resume_ICache(0);
416 }
417 
esp_config_data_cache_mode(void)418 IRAM_ATTR void esp_config_data_cache_mode(void)
419 {
420 #define CACHE_SIZE_0KB  99  //If Cache set to 0 KB, cache is bypassed, the cache size doesn't take into effect. Set this macro to a unique value for log
421 
422     cache_size_t cache_size;
423     cache_ways_t cache_ways;
424     cache_line_size_t cache_line_size;
425 
426 #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
427 #if CONFIG_ESP32S2_DATA_CACHE_0KB
428     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
429     cache_size = CACHE_SIZE_0KB;
430 #elif CONFIG_ESP32S2_DATA_CACHE_8KB
431     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
432     cache_size = CACHE_SIZE_8KB;
433 #else
434     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
435     cache_size = CACHE_SIZE_16KB;
436 #endif
437 #else
438 #if CONFIG_ESP32S2_DATA_CACHE_0KB
439     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
440     cache_size = CACHE_SIZE_0KB;
441 #elif CONFIG_ESP32S2_DATA_CACHE_8KB
442     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
443     cache_size = CACHE_SIZE_8KB;
444 #else
445     Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
446     cache_size = CACHE_SIZE_16KB;
447 #endif
448 #endif
449 
450     cache_ways = CACHE_4WAYS_ASSOC;
451 #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
452     cache_line_size = CACHE_LINE_SIZE_16B;
453 #else
454     cache_line_size = CACHE_LINE_SIZE_32B;
455 #endif
456     ESP_EARLY_LOGI(TAG, "Data cache \t\t: size %dKB, %dWays, cache line size %dByte", (cache_size == CACHE_SIZE_0KB) ? 0 : ((cache_size == CACHE_SIZE_8KB) ? 8 : 16), 4, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : 32);
457     Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
458     Cache_Invalidate_DCache_All();
459 }
460 
esp_enable_cache_flash_wrap(bool icache,bool dcache)461 static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
462 {
463     uint32_t i_autoload, d_autoload;
464     if (icache) {
465         i_autoload = Cache_Suspend_ICache();
466     }
467     if (dcache) {
468         d_autoload = Cache_Suspend_DCache();
469     }
470     REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_FLASH_WRAP_AROUND);
471     if (icache) {
472         Cache_Resume_ICache(i_autoload);
473     }
474     if (dcache) {
475         Cache_Resume_DCache(d_autoload);
476     }
477 }
478 
479 #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
esp_enable_cache_spiram_wrap(bool icache,bool dcache)480 static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
481 {
482     uint32_t i_autoload, d_autoload;
483     if (icache) {
484         i_autoload = Cache_Suspend_ICache();
485     }
486     if (dcache) {
487         d_autoload = Cache_Suspend_DCache();
488     }
489     REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_SRAM_RD_WRAP_AROUND);
490     if (icache) {
491         Cache_Resume_ICache(i_autoload);
492     }
493     if (dcache) {
494         Cache_Resume_DCache(d_autoload);
495     }
496 }
497 #endif
498 
esp_enable_cache_wrap(bool icache_wrap_enable,bool dcache_wrap_enable)499 esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
500 {
501     int icache_wrap_size = 0, dcache_wrap_size = 0;
502     int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
503     int flash_wrap_size = 0, spiram_wrap_size = 0;
504     int flash_count = 0, spiram_count = 0;
505     int i;
506     bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
507     uint32_t drom0_in_icache = 1;//always 1 in esp32s2
508 #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6
509     drom0_in_icache = 0;
510 #endif
511 
512     if (icache_wrap_enable) {
513 #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
514         icache_wrap_size = FLASH_WRAP_SIZE_16B;
515 #else
516         icache_wrap_size = FLASH_WRAP_SIZE_32B;
517 #endif
518     }
519     if (dcache_wrap_enable) {
520 #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B || CONFIG_ESP32S3_DATA_CACHE_LINE_16B
521         dcache_wrap_size = FLASH_WRAP_SIZE_16B;
522 #else
523         dcache_wrap_size = FLASH_WRAP_SIZE_32B;
524 #endif
525     }
526 
527     uint32_t instruction_use_spiram = 0;
528     uint32_t rodata_use_spiram = 0;
529 #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
530     extern uint32_t esp_spiram_instruction_access_enabled(void);
531     instruction_use_spiram = esp_spiram_instruction_access_enabled();
532 #endif
533 #if CONFIG_SPIRAM_RODATA
534     extern uint32_t esp_spiram_rodata_access_enabled(void);
535     rodata_use_spiram = esp_spiram_rodata_access_enabled();
536 #endif
537 
538     if (instruction_use_spiram) {
539         spiram_wrap_sizes[0] = icache_wrap_size;
540     } else {
541         flash_wrap_sizes[0] = icache_wrap_size;
542     }
543     if (rodata_use_spiram) {
544         if (drom0_in_icache) {
545             spiram_wrap_sizes[0] = icache_wrap_size;
546         } else {
547             spiram_wrap_sizes[1] = dcache_wrap_size;
548             flash_wrap_sizes[1] = dcache_wrap_size;
549         }
550     } else {
551         if (drom0_in_icache) {
552             flash_wrap_sizes[0] = icache_wrap_size;
553         } else {
554             flash_wrap_sizes[1] = dcache_wrap_size;
555         }
556     }
557 #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
558     spiram_wrap_sizes[1] = dcache_wrap_size;
559 #endif
560     for (i = 0; i < 2; i++) {
561         if (flash_wrap_sizes[i] != -1) {
562             flash_count++;
563             flash_wrap_size = flash_wrap_sizes[i];
564         }
565     }
566     for (i = 0; i < 2; i++) {
567         if (spiram_wrap_sizes[i] != -1) {
568             spiram_count++;
569             spiram_wrap_size = spiram_wrap_sizes[i];
570         }
571     }
572     if (flash_count + spiram_count <= 2) {
573         flash_spiram_wrap_together = false;
574     } else {
575         flash_spiram_wrap_together = true;
576     }
577     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);
578     if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
579         ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
580         if (spiram_wrap_size == 0) {
581             return ESP_FAIL;
582         }
583         if (flash_spiram_wrap_together) {
584             ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
585             return ESP_FAIL;
586         }
587     }
588     if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
589         ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
590         if (flash_wrap_size == 0) {
591             return ESP_FAIL;
592         }
593         if (flash_spiram_wrap_together) {
594             ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
595             return ESP_FAIL;
596         }
597     }
598 
599     if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
600         ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
601         return ESP_FAIL;
602     }
603 
604 #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
605     flash_support_wrap = true;
606     spi_flash_wrap_probe();
607     if (!spi_flash_support_wrap_size(flash_wrap_size)) {
608         flash_support_wrap = false;
609         ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
610     }
611 #else
612     ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
613 #endif
614 
615 #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
616     extern bool psram_support_wrap_size(uint32_t wrap_size);
617     if (!psram_support_wrap_size(spiram_wrap_size)) {
618         spiram_support_wrap = false;
619         ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
620     }
621 #endif
622 
623     if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
624         ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
625         return ESP_FAIL;
626     }
627 
628     if (flash_support_wrap && flash_wrap_size > 0) {
629         ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
630         spi_flash_wrap_enable(flash_wrap_size);
631         esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
632     }
633 #if (CONFIG_IDF_TARGET_ESP32S2 && CONFIG_SPIRAM)
634     extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
635     if (spiram_support_wrap && spiram_wrap_size > 0) {
636         ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
637         psram_enable_wrap(spiram_wrap_size);
638         esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
639     }
640 #endif
641 
642     return ESP_OK;
643 
644 }
645 #endif
646 #if CONFIG_IDF_TARGET_ESP32S3
esp_config_instruction_cache_mode(void)647 IRAM_ATTR void esp_config_instruction_cache_mode(void)
648 {
649     cache_size_t cache_size;
650     cache_ways_t cache_ways;
651     cache_line_size_t cache_line_size;
652 
653 #if CONFIG_ESP32S3_INSTRUCTION_CACHE_16KB
654     Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_INVALID);
655     cache_size = CACHE_SIZE_HALF;
656 #else
657     Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1);
658     cache_size = CACHE_SIZE_FULL;
659 #endif
660 #if CONFIG_ESP32S3_INSTRUCTION_CACHE_4WAYS
661     cache_ways = CACHE_4WAYS_ASSOC;
662 #else
663     cache_ways = CACHE_8WAYS_ASSOC;
664 #endif
665 #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
666     cache_line_size = CACHE_LINE_SIZE_16B;
667 #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
668     cache_line_size = CACHE_LINE_SIZE_32B;
669 #else
670     cache_line_size = CACHE_LINE_SIZE_64B;
671 #endif
672     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));
673     Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
674     Cache_Invalidate_ICache_All();
675     extern void Cache_Enable_ICache(uint32_t autoload);
676     Cache_Enable_ICache(0);
677 }
678 
esp_config_data_cache_mode(void)679 IRAM_ATTR void esp_config_data_cache_mode(void)
680 {
681     cache_size_t cache_size;
682     cache_ways_t cache_ways;
683     cache_line_size_t cache_line_size;
684 
685 #if CONFIG_ESP32S3_DATA_CACHE_32KB
686     Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK1, CACHE_MEMORY_INVALID);
687     cache_size = CACHE_SIZE_HALF;
688 #else
689     Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1);
690     cache_size = CACHE_SIZE_FULL;
691 #endif
692 #if CONFIG_ESP32S3_DATA_CACHE_4WAYS
693     cache_ways = CACHE_4WAYS_ASSOC;
694 #else
695     cache_ways = CACHE_8WAYS_ASSOC;
696 #endif
697 #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
698     cache_line_size = CACHE_LINE_SIZE_16B;
699 #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
700     cache_line_size = CACHE_LINE_SIZE_32B;
701 #else
702     cache_line_size = CACHE_LINE_SIZE_64B;
703 #endif
704     // 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));
705     Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
706     Cache_Invalidate_DCache_All();
707 }
708 
esp_enable_cache_flash_wrap(bool icache,bool dcache)709 static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
710 {
711     uint32_t i_autoload, d_autoload;
712     if (icache) {
713         i_autoload = Cache_Suspend_ICache();
714     }
715     if (dcache) {
716         d_autoload = Cache_Suspend_DCache();
717     }
718     REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
719     if (icache) {
720         Cache_Resume_ICache(i_autoload);
721     }
722     if (dcache) {
723         Cache_Resume_DCache(d_autoload);
724     }
725 }
726 
727 #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
esp_enable_cache_spiram_wrap(bool icache,bool dcache)728 static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
729 {
730     uint32_t i_autoload, d_autoload;
731     if (icache) {
732         i_autoload = Cache_Suspend_ICache();
733     }
734     if (dcache) {
735         d_autoload = Cache_Suspend_DCache();
736     }
737     REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_SRAM_RD_WRAP_AROUND);
738     if (icache) {
739         Cache_Resume_ICache(i_autoload);
740     }
741     if (dcache) {
742         Cache_Resume_DCache(d_autoload);
743     }
744 }
745 #endif
746 
esp_enable_cache_wrap(bool icache_wrap_enable,bool dcache_wrap_enable)747 esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
748 {
749     int icache_wrap_size = 0, dcache_wrap_size = 0;
750     int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
751     int flash_wrap_size = 0, spiram_wrap_size = 0;
752     int flash_count = 0, spiram_count = 0;
753     int i;
754     bool flash_spiram_wrap_together, flash_support_wrap = false, spiram_support_wrap = true;
755     uint32_t drom0_in_icache = 0;//always 0 in chip7.2.4
756 
757     if (icache_wrap_enable) {
758 #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
759         icache_wrap_size = FLASH_WRAP_SIZE_16B;
760 #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
761         icache_wrap_size = FLASH_WRAP_SIZE_32B;
762 #else
763         icache_wrap_size = FLASH_WRAP_SIZE_64B;
764 #endif
765     }
766     if (dcache_wrap_enable) {
767 #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
768         dcache_wrap_size = FLASH_WRAP_SIZE_16B;
769 #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
770         dcache_wrap_size = FLASH_WRAP_SIZE_32B;
771 #else
772         dcache_wrap_size = FLASH_WRAP_SIZE_64B;
773 #endif
774     }
775 
776     uint32_t instruction_use_spiram = 0;
777     uint32_t rodata_use_spiram = 0;
778 #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
779     extern uint32_t esp_spiram_instruction_access_enabled(void);
780     instruction_use_spiram = esp_spiram_instruction_access_enabled();
781 #endif
782 #if CONFIG_SPIRAM_RODATA
783     extern uint32_t esp_spiram_rodata_access_enabled(void);
784     rodata_use_spiram = esp_spiram_rodata_access_enabled();
785 #endif
786 
787     if (instruction_use_spiram) {
788         spiram_wrap_sizes[0] = icache_wrap_size;
789     } else {
790         flash_wrap_sizes[0] = icache_wrap_size;
791     }
792     if (rodata_use_spiram) {
793         if (drom0_in_icache) {
794             spiram_wrap_sizes[0] = icache_wrap_size;
795         } else {
796             spiram_wrap_sizes[1] = dcache_wrap_size;
797         }
798     } else {
799         if (drom0_in_icache) {
800             flash_wrap_sizes[0] = icache_wrap_size;
801         } else {
802             flash_wrap_sizes[1] = dcache_wrap_size;
803         }
804     }
805 #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
806     spiram_wrap_sizes[1] = dcache_wrap_size;
807 #endif
808     for (i = 0; i < 2; i++) {
809         if (flash_wrap_sizes[i] != -1) {
810             flash_count++;
811             flash_wrap_size = flash_wrap_sizes[i];
812         }
813     }
814     for (i = 0; i < 2; i++) {
815         if (spiram_wrap_sizes[i] != -1) {
816             spiram_count++;
817             spiram_wrap_size = spiram_wrap_sizes[i];
818         }
819     }
820     if (flash_count + spiram_count <= 2) {
821         flash_spiram_wrap_together = false;
822     } else {
823         flash_spiram_wrap_together = true;
824     }
825     if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
826         ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
827         if (spiram_wrap_size == 0) {
828             return ESP_FAIL;
829         }
830         if (flash_spiram_wrap_together) {
831             ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
832             return ESP_FAIL;
833         }
834     }
835     if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
836         ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
837         if (flash_wrap_size == 0) {
838             return ESP_FAIL;
839         }
840         if (flash_spiram_wrap_together) {
841             ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
842             return ESP_FAIL;
843         }
844     }
845 
846     if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
847         ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
848         return ESP_FAIL;
849     }
850 
851 #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
852     flash_support_wrap = true;
853     spi_flash_wrap_probe();
854     if (!spi_flash_support_wrap_size(flash_wrap_size)) {
855         flash_support_wrap = false;
856         ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
857     }
858 #else
859     ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
860 #endif
861 
862 
863 #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
864     extern bool psram_support_wrap_size(uint32_t wrap_size);
865     if (!psram_support_wrap_size(spiram_wrap_size)) {
866         spiram_support_wrap = false;
867         ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
868     }
869 #endif
870 
871     if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
872         ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
873         return ESP_FAIL;
874     }
875 
876     if (flash_support_wrap && flash_wrap_size > 0) {
877         ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
878         spi_flash_wrap_enable(flash_wrap_size);
879         esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
880     }
881 #if (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_SPIRAM)
882     extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
883     if (spiram_support_wrap && spiram_wrap_size > 0) {
884         ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
885         psram_enable_wrap(spiram_wrap_size);
886         esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
887     }
888 #endif
889 
890     return ESP_OK;
891 
892 }
893 #endif
894 
895 #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
896 
esp_enable_cache_flash_wrap(bool icache)897 static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache)
898 {
899     uint32_t i_autoload;
900     if (icache) {
901         i_autoload = Cache_Suspend_ICache();
902     }
903     REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
904     if (icache) {
905         Cache_Resume_ICache(i_autoload);
906     }
907 }
908 
esp_enable_cache_wrap(bool icache_wrap_enable)909 esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable)
910 {
911     int flash_wrap_size = 0;
912     bool flash_support_wrap = false;
913 
914     if (icache_wrap_enable) {
915         flash_wrap_size = 32;
916     }
917 
918 #ifdef CONFIG_ESPTOOLPY_FLASHMODE_QIO
919     flash_support_wrap = true;
920     spi_flash_wrap_probe();
921     if (!spi_flash_support_wrap_size(flash_wrap_size)) {
922         flash_support_wrap = false;
923         ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
924     }
925 #else
926     ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
927 #endif // CONFIG_ESPTOOLPY_FLASHMODE_QIO
928 
929     if (flash_support_wrap && flash_wrap_size > 0) {
930         ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
931         spi_flash_wrap_enable(flash_wrap_size);
932         esp_enable_cache_flash_wrap((flash_wrap_size > 0));
933     }
934     return ESP_OK;
935 }
936 #endif // CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
937