Skip to content

Commit 0f0068f

Browse files
author
Jiang Jiang Jian
committed
Merge branch 'fix/fix_branch_predictor_access_flash_after_cache_diabled' into 'master'
fix(esp_hw_support): fix branch predictor access flash after cache disabled Closes PM-329 See merge request espressif/esp-idf!36281
2 parents 5238519 + 7a4cc8e commit 0f0068f

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

components/esp_hw_support/sleep_modes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#include "soc/rtc.h"
5858
#include "regi2c_ctrl.h" //For `REGI2C_ANA_CALI_PD_WORKAROUND`, temp
5959

60-
#include "hal/cache_hal.h"
6160
#include "hal/cache_ll.h"
6261
#include "hal/clk_tree_ll.h"
6362
#include "hal/wdt_hal.h"
@@ -70,6 +69,7 @@
7069
#include "sdkconfig.h"
7170
#include "esp_rom_uart.h"
7271
#include "esp_rom_sys.h"
72+
#include "esp_private/cache_utils.h"
7373
#include "esp_private/brownout.h"
7474
#include "esp_private/sleep_console.h"
7575
#include "esp_private/sleep_cpu.h"
@@ -494,7 +494,7 @@ static int s_cache_suspend_cnt = 0;
494494
static void IRAM_ATTR suspend_cache(void) {
495495
s_cache_suspend_cnt++;
496496
if (s_cache_suspend_cnt == 1) {
497-
cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
497+
spi_flash_disable_cache(esp_cpu_get_core_id(), NULL);
498498
}
499499
}
500500

@@ -503,7 +503,7 @@ static void IRAM_ATTR resume_cache(void) {
503503
s_cache_suspend_cnt--;
504504
assert(s_cache_suspend_cnt >= 0 && DRAM_STR("cache resume doesn't match suspend ops"));
505505
if (s_cache_suspend_cnt == 0) {
506-
cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
506+
spi_flash_restore_cache(esp_cpu_get_core_id(), 0);
507507
}
508508
}
509509

components/esp_system/port/cpu_start.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "esp_memprot.h"
6464
#endif
6565

66+
#include "esp_private/cache_utils.h"
6667
#include "esp_private/rtc_clk.h"
6768
#include "esp_rtc_time.h"
6869
#include "rom/rtc.h"
@@ -694,7 +695,6 @@ void IRAM_ATTR call_start_cpu0(void)
694695
#if CONFIG_ESP32S2_DATA_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
695696
dcache_wrap_enable = 1;
696697
#endif
697-
extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
698698
esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
699699
#endif
700700

@@ -706,7 +706,6 @@ void IRAM_ATTR call_start_cpu0(void)
706706
#if CONFIG_IDF_TARGET_ESP32C2
707707
// TODO : IDF-5020
708708
#if CONFIG_ESP32C2_INSTRUCTION_CACHE_WRAP
709-
extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable);
710709
esp_enable_cache_wrap(1);
711710
#endif
712711
#endif

components/spi_flash/cache_utils.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -33,21 +33,14 @@
3333
#include "esp_memory_utils.h"
3434
#include "esp_intr_alloc.h"
3535
#include "spi_flash_override.h"
36+
#include "esp_private/cache_utils.h"
3637
#include "esp_private/spi_flash_os.h"
3738
#include "esp_private/freertos_idf_additions_priv.h"
3839
#include "esp_log.h"
3940
#include "esp_cpu.h"
4041

4142
static __attribute__((unused)) const char *TAG = "cache";
4243

43-
44-
/**
45-
* These two shouldn't be declared as static otherwise if `CONFIG_SPI_FLASH_ROM_IMPL` is enabled,
46-
* they won't get replaced by the rom version
47-
*/
48-
void spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
49-
void spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
50-
5144
// Used only on ROM impl. in idf, this param unused, cache status hold by hal
5245
static uint32_t s_flash_op_cache_state[2];
5346

components/spi_flash/include/esp_private/cache_utils.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -64,7 +64,22 @@ bool spi_flash_check_and_flush_cache(size_t start_addr, size_t length);
6464
void esp_config_instruction_cache_mode(void);
6565
//config data cache size and cache block size by menuconfig
6666
void esp_config_data_cache_mode(void);
67-
//enable cache wrap mode for instruction cache and data cache
67+
#endif
68+
69+
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2
70+
/**
71+
* @brief enable cache wrap mode for i/d shared cache
72+
* @param icache_wrap_enable enable cache wrap mode for i/d shared cache
73+
* @return ESP_OK on success, ESP_FAIL otherwise
74+
*/
75+
esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable);
76+
#elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2
77+
/**
78+
* @brief enable cache wrap mode for instruction cache and data cache
79+
* @param icache_wrap_enable enable cache wrap mode for i cache
80+
* @param dcache_wrap_enable enable cache wrap mode for d cache
81+
* @return ESP_OK on success, ESP_FAIL otherwise
82+
*/
6883
esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable);
6984
#endif
7085

@@ -81,6 +96,22 @@ bool spi_flash_cache_enabled(void);
8196
*/
8297
void spi_flash_enable_cache(uint32_t cpuid);
8398

99+
/**
100+
* @brief Suspend the Cache access to external memory, will disable branch predictor if supported.
101+
*
102+
* @param cpuid the core number to enable the cache for, meaning less on shared cache.
103+
* @param saved_state Cache status hold by hal (Used only on ROM impl. in idf, this param unused)
104+
*/
105+
void spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
106+
107+
/**
108+
* @brief Resume the Cache access to external memory, will enable branch predictor if supported.
109+
*
110+
* @param cpuid the core number to enable the cache for, meaning less on shared cache.
111+
* @param saved_state Cache status hold by hal (Used only on ROM impl. in idf, this param unused)
112+
*/
113+
void spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
114+
84115
#ifdef __cplusplus
85116
}
86117
#endif

0 commit comments

Comments
 (0)