From d23549db4ca0c34b366beeb27839cfb9e4fb2de7 Mon Sep 17 00:00:00 2001 From: Erki Aring Date: Tue, 4 Mar 2025 14:47:10 +0200 Subject: [PATCH 1/2] fix(psram): correct heap vaddr calculation - fixes https://github.com/espressif/esp-idf/issues/15496 Signed-off-by: armando --- components/esp_psram/esp_psram.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/components/esp_psram/esp_psram.c b/components/esp_psram/esp_psram.c index 0ca1f8f792..4cdd53ba54 100644 --- a/components/esp_psram/esp_psram.c +++ b/components/esp_psram/esp_psram.c @@ -307,21 +307,31 @@ static void s_psram_mapping(uint32_t psram_available_size, uint32_t start_page) * After mapping, we DON'T care about the PSRAM PHYSICAL ADDRESS ANYMORE! *----------------------------------------------------------------------------*/ - //------------------------------------Configure .bss in PSRAM-------------------------------------// + //------------------------------------Configure heap in PSRAM-------------------------------------// + uintptr_t ext_segment_start = UINTPTR_MAX; + uintptr_t ext_segment_end = 0; + #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY - //should never be negative number - uint32_t ext_bss_size = ((intptr_t)&_ext_ram_bss_end - (intptr_t)&_ext_ram_bss_start); - ESP_EARLY_LOGV(TAG, "ext_bss_size is %" PRIu32, ext_bss_size); - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_bss_size; - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_bss_size; + ext_segment_start = (uintptr_t)&_ext_ram_bss_start; + ext_segment_end = (uintptr_t)&_ext_ram_bss_end; #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY - uint32_t ext_noinit_size = ((intptr_t)&_ext_ram_noinit_end - (intptr_t)&_ext_ram_noinit_start); - ESP_EARLY_LOGV(TAG, "ext_noinit_size is %" PRIu32, ext_noinit_size); - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_noinit_size; - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_noinit_size; -#endif + if ((uintptr_t)&_ext_ram_noinit_start < ext_segment_start) { + ext_segment_start = (uintptr_t)&_ext_ram_noinit_start; + } + if ((uintptr_t)&_ext_ram_noinit_end > ext_segment_end) { + ext_segment_end = (uintptr_t)&_ext_ram_noinit_end; + } +#endif //#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY + + if ((ext_segment_start != UINTPTR_MAX) || (ext_segment_end != 0)) { + assert(ext_segment_end >= ext_segment_start); + uint32_t ext_segment_size = ext_segment_end - ext_segment_start; + ESP_EARLY_LOGV(TAG, "ext_segment_size is %" PRIu32, ext_segment_size); + s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_segment_size; + s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_segment_size; + } #if CONFIG_IDF_TARGET_ESP32 s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= esp_himem_reserved_area_size() - 1; From 53d019563b7cf68056fe3be558b582dd0511580e Mon Sep 17 00:00:00 2001 From: armando Date: Mon, 10 Mar 2025 14:42:50 +0800 Subject: [PATCH 2/2] refactor(psram): rename .bss .noinit segments to sections Closes https://github.com/espressif/esp-idf/pull/15513 --- components/esp_psram/esp_psram.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/components/esp_psram/esp_psram.c b/components/esp_psram/esp_psram.c index 4cdd53ba54..beebd0dbbc 100644 --- a/components/esp_psram/esp_psram.c +++ b/components/esp_psram/esp_psram.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -307,30 +307,30 @@ static void s_psram_mapping(uint32_t psram_available_size, uint32_t start_page) * After mapping, we DON'T care about the PSRAM PHYSICAL ADDRESS ANYMORE! *----------------------------------------------------------------------------*/ - //------------------------------------Configure heap in PSRAM-------------------------------------// - uintptr_t ext_segment_start = UINTPTR_MAX; - uintptr_t ext_segment_end = 0; + //------------------------------------Configure other sections in PSRAM-------------------------------------// + uintptr_t ext_section_start = UINTPTR_MAX; + uintptr_t ext_section_end = 0; #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY - ext_segment_start = (uintptr_t)&_ext_ram_bss_start; - ext_segment_end = (uintptr_t)&_ext_ram_bss_end; + ext_section_start = (uintptr_t)&_ext_ram_bss_start; + ext_section_end = (uintptr_t)&_ext_ram_bss_end; #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY - if ((uintptr_t)&_ext_ram_noinit_start < ext_segment_start) { - ext_segment_start = (uintptr_t)&_ext_ram_noinit_start; + if ((uintptr_t)&_ext_ram_noinit_start < ext_section_start) { + ext_section_start = (uintptr_t)&_ext_ram_noinit_start; } - if ((uintptr_t)&_ext_ram_noinit_end > ext_segment_end) { - ext_segment_end = (uintptr_t)&_ext_ram_noinit_end; + if ((uintptr_t)&_ext_ram_noinit_end > ext_section_end) { + ext_section_end = (uintptr_t)&_ext_ram_noinit_end; } #endif //#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY - if ((ext_segment_start != UINTPTR_MAX) || (ext_segment_end != 0)) { - assert(ext_segment_end >= ext_segment_start); - uint32_t ext_segment_size = ext_segment_end - ext_segment_start; - ESP_EARLY_LOGV(TAG, "ext_segment_size is %" PRIu32, ext_segment_size); - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_segment_size; - s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_segment_size; + if ((ext_section_start != UINTPTR_MAX) || (ext_section_end != 0)) { + assert(ext_section_end >= ext_section_start); + uint32_t ext_section_size = ext_section_end - ext_section_start; + ESP_EARLY_LOGV(TAG, "ext_section_size is %" PRIu32, ext_section_size); + s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_section_size; + s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_section_size; } #if CONFIG_IDF_TARGET_ESP32