From 7948c53dde35902389d9e6ca27986e985276c1e0 Mon Sep 17 00:00:00 2001 From: armando Date: Wed, 2 Apr 2025 18:07:40 +0800 Subject: [PATCH 1/3] Revert "fix(cache): disable branch predictor when cache freeze" This reverts commit 6bb78e4573667f59d246e25e433c27bc3430757c. --- components/esp_mm/esp_cache.c | 15 --------- .../include/esp_private/esp_cache_private.h | 33 +------------------ components/spi_flash/cache_utils.c | 13 ++++++-- 3 files changed, 11 insertions(+), 50 deletions(-) diff --git a/components/esp_mm/esp_cache.c b/components/esp_mm/esp_cache.c index 26cacdf134..161df6d28a 100644 --- a/components/esp_mm/esp_cache.c +++ b/components/esp_mm/esp_cache.c @@ -16,7 +16,6 @@ #include "hal/mmu_hal.h" #include "hal/cache_hal.h" #include "hal/cache_ll.h" -#include "esp_cpu.h" #include "esp_cache.h" #include "esp_private/esp_cache_private.h" #include "esp_private/critical_section.h" @@ -96,10 +95,6 @@ esp_err_t esp_cache_msync(void *addr, size_t size, int flags) void esp_cache_suspend_ext_mem_cache(void) { -#if SOC_BRANCH_PREDICTOR_SUPPORTED - //branch predictor will start cache request as well - esp_cpu_branch_prediction_disable(); -#endif #if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) /** * before suspending the external mem cache, writeback internal mem cache content back to external mem cache @@ -113,18 +108,11 @@ void esp_cache_suspend_ext_mem_cache(void) void esp_cache_resume_ext_mem_cache(void) { cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -#if SOC_BRANCH_PREDICTOR_SUPPORTED - esp_cpu_branch_prediction_enable(); -#endif } #if SOC_CACHE_FREEZE_SUPPORTED void esp_cache_freeze_ext_mem_cache(void) { -#if SOC_BRANCH_PREDICTOR_SUPPORTED - //branch predictor will start cache request as well - esp_cpu_branch_prediction_disable(); -#endif #if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) /** * before freezing the external mem cache, writeback internal mem cache content back to external mem cache @@ -138,9 +126,6 @@ void esp_cache_freeze_ext_mem_cache(void) void esp_cache_unfreeze_ext_mem_cache(void) { cache_hal_unfreeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -#if SOC_BRANCH_PREDICTOR_SUPPORTED - esp_cpu_branch_prediction_enable(); -#endif } #endif //#if SOC_CACHE_FREEZE_SUPPORTED diff --git a/components/esp_mm/include/esp_private/esp_cache_private.h b/components/esp_mm/include/esp_private/esp_cache_private.h index 39f1cf9e7e..3f28554220 100644 --- a/components/esp_mm/include/esp_private/esp_cache_private.h +++ b/components/esp_mm/include/esp_private/esp_cache_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,7 +7,6 @@ #include #include -#include "soc/soc_caps.h" #include "esp_err.h" #include "esp_bit_defs.h" #include "esp_heap_caps.h" @@ -16,36 +15,6 @@ extern "C" { #endif -/** - * @brief Suspend external memory cache - * - * @note This API is only for internal usage, no thread safety guaranteed - */ -void esp_cache_suspend_ext_mem_cache(void); - -/** - * @brief Resume external memory cache - * - * @note This API is only for internal usage, no thread safety guaranteed - */ -void esp_cache_resume_ext_mem_cache(void); - -#if SOC_CACHE_FREEZE_SUPPORTED -/** - * @brief Freeze external memory cache - * - * @note This API is only for internal usage, no thread safety guaranteed - */ -void esp_cache_freeze_ext_mem_cache(void); - -/** - * @brief Unfreeze external memory cache - * - * @note This API is only for internal usage, no thread safety guaranteed - */ -void esp_cache_unfreeze_ext_mem_cache(void); -#endif - /** * @brief Helper function for malloc a cache aligned data memory buffer * diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index f2d57d998a..ce4c57c409 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -58,11 +58,11 @@ #include "esp_memory_utils.h" #include "esp_intr_alloc.h" #include "spi_flash_override.h" -#include "esp_private/esp_cache_private.h" #include "esp_private/cache_utils.h" #include "esp_private/spi_flash_os.h" #include "esp_private/freertos_idf_additions_priv.h" #include "esp_log.h" +#include "esp_cpu.h" static __attribute__((unused)) const char *TAG = "cache"; @@ -364,12 +364,19 @@ void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid) void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state) { - esp_cache_suspend_ext_mem_cache(); +#if SOC_BRANCH_PREDICTOR_SUPPORTED + //branch predictor will start cache request as well + esp_cpu_branch_prediction_disable(); +#endif + cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); } void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state) { - esp_cache_resume_ext_mem_cache(); + cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); +#if SOC_BRANCH_PREDICTOR_SUPPORTED + esp_cpu_branch_prediction_enable(); +#endif } bool IRAM_ATTR spi_flash_cache_enabled(void) From 950e54fc96538657f83407b1314822f613cc9174 Mon Sep 17 00:00:00 2001 From: armando Date: Wed, 2 Apr 2025 18:07:49 +0800 Subject: [PATCH 2/3] Revert "fix(cache): fixed l2 cache suspended/frozen but l1cache triggers l2 writeback issue" This reverts commit df1f0f51f44c7e1fdb9fd9c7df2e9efde409d190. --- .../esp_hw_support/mspi_timing_tuning.c | 9 +++-- components/esp_mm/esp_cache.c | 38 +------------------ 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/components/esp_hw_support/mspi_timing_tuning.c b/components/esp_hw_support/mspi_timing_tuning.c index 7b935dcdc9..bfab81120a 100644 --- a/components/esp_hw_support/mspi_timing_tuning.c +++ b/components/esp_hw_support/mspi_timing_tuning.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -14,7 +14,8 @@ #include "soc/io_mux_reg.h" #include "soc/soc.h" #include "hal/spi_flash_hal.h" -#include "esp_private/esp_cache_private.h" +#include "hal/cache_hal.h" +#include "hal/cache_ll.h" #include "esp_private/mspi_timing_tuning.h" #include "esp_private/mspi_timing_config.h" #include "mspi_timing_by_mspi_delay.h" @@ -535,7 +536,7 @@ void mspi_timing_change_speed_mode_cache_safe(bool switch_down) * for preventing concurrent from MSPI to external memory */ #if SOC_CACHE_FREEZE_SUPPORTED - esp_cache_freeze_ext_mem_cache(); + cache_hal_freeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); #endif //#if SOC_CACHE_FREEZE_SUPPORTED if (switch_down) { @@ -547,7 +548,7 @@ void mspi_timing_change_speed_mode_cache_safe(bool switch_down) } #if SOC_CACHE_FREEZE_SUPPORTED - esp_cache_unfreeze_ext_mem_cache(); + cache_hal_unfreeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); #endif //#if SOC_CACHE_FREEZE_SUPPORTED #if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE && !CONFIG_FREERTOS_UNICORE diff --git a/components/esp_mm/esp_cache.c b/components/esp_mm/esp_cache.c index 161df6d28a..84a265a219 100644 --- a/components/esp_mm/esp_cache.c +++ b/components/esp_mm/esp_cache.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -93,42 +93,6 @@ esp_err_t esp_cache_msync(void *addr, size_t size, int flags) return ESP_OK; } -void esp_cache_suspend_ext_mem_cache(void) -{ -#if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) - /** - * before suspending the external mem cache, writeback internal mem cache content back to external mem cache - * to avoid stuck issue caused by internal mem cache auto-writeback - */ - cache_ll_writeback_all(CACHE_LL_LEVEL_INT_MEM, CACHE_TYPE_DATA, CACHE_LL_ID_ALL); -#endif - cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -} - -void esp_cache_resume_ext_mem_cache(void) -{ - cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -} - -#if SOC_CACHE_FREEZE_SUPPORTED -void esp_cache_freeze_ext_mem_cache(void) -{ -#if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) - /** - * before freezing the external mem cache, writeback internal mem cache content back to external mem cache - * to avoid stuck issue caused by internal mem cache auto-writeback - */ - cache_ll_writeback_all(CACHE_LL_LEVEL_INT_MEM, CACHE_TYPE_DATA, CACHE_LL_ID_ALL); -#endif - cache_hal_freeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -} - -void esp_cache_unfreeze_ext_mem_cache(void) -{ - cache_hal_unfreeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); -} -#endif //#if SOC_CACHE_FREEZE_SUPPORTED - //The esp_cache_aligned_malloc function is marked deprecated but also called by other //(also deprecated) functions in this file. In order to work around that generating warnings, it's //split into a non-deprecated internal function and the stubbed external deprecated function. From 9dfbc9a59ccbdd6109dce2011a610bec0b2549a0 Mon Sep 17 00:00:00 2001 From: armando Date: Wed, 26 Mar 2025 16:06:50 +0800 Subject: [PATCH 3/3] fix(cache): fixed l2 cache suspended/frozen but l1cache triggers l2 writeback issue --- .../esp_hw_support/mspi_timing_tuning.c | 9 ++-- components/esp_mm/esp_cache.c | 38 ++++++++++++++++- .../include/esp_private/esp_cache_private.h | 42 ++++++++++++++++++- components/spi_flash/cache_utils.c | 6 +-- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/components/esp_hw_support/mspi_timing_tuning.c b/components/esp_hw_support/mspi_timing_tuning.c index bfab81120a..7b935dcdc9 100644 --- a/components/esp_hw_support/mspi_timing_tuning.c +++ b/components/esp_hw_support/mspi_timing_tuning.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -14,8 +14,7 @@ #include "soc/io_mux_reg.h" #include "soc/soc.h" #include "hal/spi_flash_hal.h" -#include "hal/cache_hal.h" -#include "hal/cache_ll.h" +#include "esp_private/esp_cache_private.h" #include "esp_private/mspi_timing_tuning.h" #include "esp_private/mspi_timing_config.h" #include "mspi_timing_by_mspi_delay.h" @@ -536,7 +535,7 @@ void mspi_timing_change_speed_mode_cache_safe(bool switch_down) * for preventing concurrent from MSPI to external memory */ #if SOC_CACHE_FREEZE_SUPPORTED - cache_hal_freeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + esp_cache_freeze_ext_mem_cache(); #endif //#if SOC_CACHE_FREEZE_SUPPORTED if (switch_down) { @@ -548,7 +547,7 @@ void mspi_timing_change_speed_mode_cache_safe(bool switch_down) } #if SOC_CACHE_FREEZE_SUPPORTED - cache_hal_unfreeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + esp_cache_unfreeze_ext_mem_cache(); #endif //#if SOC_CACHE_FREEZE_SUPPORTED #if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE && !CONFIG_FREERTOS_UNICORE diff --git a/components/esp_mm/esp_cache.c b/components/esp_mm/esp_cache.c index 84a265a219..161df6d28a 100644 --- a/components/esp_mm/esp_cache.c +++ b/components/esp_mm/esp_cache.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -93,6 +93,42 @@ esp_err_t esp_cache_msync(void *addr, size_t size, int flags) return ESP_OK; } +void esp_cache_suspend_ext_mem_cache(void) +{ +#if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) + /** + * before suspending the external mem cache, writeback internal mem cache content back to external mem cache + * to avoid stuck issue caused by internal mem cache auto-writeback + */ + cache_ll_writeback_all(CACHE_LL_LEVEL_INT_MEM, CACHE_TYPE_DATA, CACHE_LL_ID_ALL); +#endif + cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); +} + +void esp_cache_resume_ext_mem_cache(void) +{ + cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); +} + +#if SOC_CACHE_FREEZE_SUPPORTED +void esp_cache_freeze_ext_mem_cache(void) +{ +#if (CONFIG_SPIRAM && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) + /** + * before freezing the external mem cache, writeback internal mem cache content back to external mem cache + * to avoid stuck issue caused by internal mem cache auto-writeback + */ + cache_ll_writeback_all(CACHE_LL_LEVEL_INT_MEM, CACHE_TYPE_DATA, CACHE_LL_ID_ALL); +#endif + cache_hal_freeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); +} + +void esp_cache_unfreeze_ext_mem_cache(void) +{ + cache_hal_unfreeze(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); +} +#endif //#if SOC_CACHE_FREEZE_SUPPORTED + //The esp_cache_aligned_malloc function is marked deprecated but also called by other //(also deprecated) functions in this file. In order to work around that generating warnings, it's //split into a non-deprecated internal function and the stubbed external deprecated function. diff --git a/components/esp_mm/include/esp_private/esp_cache_private.h b/components/esp_mm/include/esp_private/esp_cache_private.h index 3f28554220..7380fddfa4 100644 --- a/components/esp_mm/include/esp_private/esp_cache_private.h +++ b/components/esp_mm/include/esp_private/esp_cache_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,6 +7,7 @@ #include #include +#include "soc/soc_caps.h" #include "esp_err.h" #include "esp_bit_defs.h" #include "esp_heap_caps.h" @@ -15,6 +16,45 @@ extern "C" { #endif +/** + * @brief Suspend external memory cache + * + * @note CPU branch predictor should be disabled before calling this API + * @note This API is only for internal usage, no thread safety guaranteed + * @note This API is Non-Nestable and Non-Reentrant + * @note Call to this API must be followed by a `esp_cache_resume_ext_mem_cache` + */ +void esp_cache_suspend_ext_mem_cache(void); + +/** + * @brief Resume external memory cache + * + * @note This API is only for internal usage, no thread safety guaranteed + * @note This API is Non-Nestable and Non-Reentrant + * @note This API must be called after a `esp_cache_suspend_ext_mem_cache` + */ +void esp_cache_resume_ext_mem_cache(void); + +#if SOC_CACHE_FREEZE_SUPPORTED +/** + * @brief Freeze external memory cache + * + * @note This API is only for internal usage, no thread safety guaranteed + * @note This API is Non-Nestable and Non-Reentrant + * @note Call to this API must be followed by a `esp_cache_unfreeze_ext_mem_cache` + */ +void esp_cache_freeze_ext_mem_cache(void); + +/** + * @brief Unfreeze external memory cache + * + * @note This API is only for internal usage, no thread safety guaranteed + * @note This API is Non-Nestable and Non-Reentrant + * @note This API must be called after a `esp_cache_freeze_ext_mem_cache` + */ +void esp_cache_unfreeze_ext_mem_cache(void); +#endif + /** * @brief Helper function for malloc a cache aligned data memory buffer * diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index ce4c57c409..f4f1c9c9f3 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -58,11 +58,11 @@ #include "esp_memory_utils.h" #include "esp_intr_alloc.h" #include "spi_flash_override.h" +#include "esp_private/esp_cache_private.h" #include "esp_private/cache_utils.h" #include "esp_private/spi_flash_os.h" #include "esp_private/freertos_idf_additions_priv.h" #include "esp_log.h" -#include "esp_cpu.h" static __attribute__((unused)) const char *TAG = "cache"; @@ -368,12 +368,12 @@ void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state) //branch predictor will start cache request as well esp_cpu_branch_prediction_disable(); #endif - cache_hal_suspend(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + esp_cache_suspend_ext_mem_cache(); } void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state) { - cache_hal_resume(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + esp_cache_resume_ext_mem_cache(); #if SOC_BRANCH_PREDICTOR_SUPPORTED esp_cpu_branch_prediction_enable(); #endif