From 6e6ab2f8abd5467afb0235ca7c94d5a206bdf55f Mon Sep 17 00:00:00 2001 From: Marek Matej Date: Wed, 29 Jan 2025 14:25:09 +0100 Subject: [PATCH] soc: espressif: Remove ESP heap and use heap adapter Remove ESP heap from the sources. System heap is default heap. Use heap adapter layer to configure used heap. Use MEM_POOL memory request config to Wi-Fi and Bluetooth drivers. Update the Wi-Fi and BLE memory needs. Signed-off-by: Marek Matej --- drivers/bluetooth/hci/Kconfig.esp32 | 15 ++-- drivers/wifi/esp32/Kconfig.esp32 | 31 +++----- soc/espressif/common/CMakeLists.txt | 1 - soc/espressif/common/Kconfig | 8 -- soc/espressif/common/esp_heap_runtime.c | 77 ------------------- .../common/include/esp_heap_runtime.h | 43 ----------- soc/espressif/common/loader.c | 12 ++- soc/espressif/esp32/default.ld | 2 + soc/espressif/esp32/default_appcpu.ld | 2 + soc/espressif/esp32c2/default.ld | 3 +- soc/espressif/esp32c3/default.ld | 3 +- soc/espressif/esp32c6/default.ld | 2 + soc/espressif/esp32s2/default.ld | 1 + soc/espressif/esp32s3/default.ld | 4 +- soc/espressif/esp32s3/default_appcpu.ld | 3 +- 15 files changed, 45 insertions(+), 162 deletions(-) delete mode 100644 soc/espressif/common/esp_heap_runtime.c delete mode 100644 soc/espressif/common/include/esp_heap_runtime.h diff --git a/drivers/bluetooth/hci/Kconfig.esp32 b/drivers/bluetooth/hci/Kconfig.esp32 index ad7a53d5de3..294b6a1cd84 100644 --- a/drivers/bluetooth/hci/Kconfig.esp32 +++ b/drivers/bluetooth/hci/Kconfig.esp32 @@ -2,16 +2,19 @@ if BT_ESP32 +config HEAP_MEM_POOL_ADD_SIZE_ESP_BT + int + default 25600 if ESP_BT_HEAP_SYSTEM + default 0 + help + Make sure there is a minimal heap available for BT driver. + choice ESP_BT_HEAP prompt "Bluetooth adapter heap in use" - default ESP_BT_HEAP_RUNTIME - - config ESP_BT_HEAP_RUNTIME - bool "Bluetooth adapter use ESP runtime heap" - depends on ESP_HEAP_RUNTIME + default ESP_BT_HEAP_SYSTEM config ESP_BT_HEAP_SYSTEM - bool "Bluetooth adapter use system heap" + bool "Bluetooth adapter use the kernel mempool heap (k_malloc)" endchoice # ESP_BT_HEAP diff --git a/drivers/wifi/esp32/Kconfig.esp32 b/drivers/wifi/esp32/Kconfig.esp32 index 3d5bdf90cad..039e6f1f398 100644 --- a/drivers/wifi/esp32/Kconfig.esp32 +++ b/drivers/wifi/esp32/Kconfig.esp32 @@ -20,12 +20,22 @@ menuconfig WIFI_ESP32 if WIFI_ESP32 -config HEAP_MEM_POOL_ADD_SIZE_WIFI +config HEAP_MEM_POOL_ADD_SIZE_ESP_WIFI int - default 4096 + default 40960 if ESP_WIFI_HEAP_SYSTEM + default 0 help Make sure there is a minimal heap available for Wi-Fi driver. +choice ESP_WIFI_HEAP + prompt "Wi-Fi adapter heap memory" + default ESP_WIFI_HEAP_SYSTEM + + config ESP_WIFI_HEAP_SYSTEM + bool "Wi-Fi adapter use kernel mempool heap (k_malloc)" + +endchoice # ESP_WIFI_HEAP + config NET_TCP_WORKQ_STACK_SIZE default 2048 @@ -333,23 +343,6 @@ config ESP32_WIFI_SLP_DEFAULT_WAIT_BROADCAST_DATA_TIME before entering the sleep process. If a broadcast packet is received with more data bits, the time will refreshed. unit: milliseconds. -choice ESP_WIFI_HEAP - prompt "Wifi adapter heap in use" - default ESP_WIFI_HEAP_RUNTIME - - config ESP_WIFI_HEAP_RUNTIME - bool "Wifi adapter use ESP runtime heap" - depends on ESP_HEAP_RUNTIME - - config ESP_WIFI_HEAP_SPIRAM - bool "Wifi adapter use SPIRAM heap" - depends on ESP_SPIRAM - - config ESP_WIFI_HEAP_SYSTEM - bool "Wifi adapter use system heap" - -endchoice # ESP_WIFI_HEAP - config ESP32_WIFI_FTM_ENABLE bool "WiFi FTM" default n diff --git a/soc/espressif/common/CMakeLists.txt b/soc/espressif/common/CMakeLists.txt index 4cd6217126a..3ba79987a3b 100644 --- a/soc/espressif/common/CMakeLists.txt +++ b/soc/espressif/common/CMakeLists.txt @@ -4,6 +4,5 @@ zephyr_include_directories(include) if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU) - zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c) zephyr_sources_ifdef(CONFIG_ESP_SPIRAM esp_psram.c) endif() diff --git a/soc/espressif/common/Kconfig b/soc/espressif/common/Kconfig index e33aa2e0f38..1928b48f1bb 100644 --- a/soc/espressif/common/Kconfig +++ b/soc/espressif/common/Kconfig @@ -24,14 +24,6 @@ config ESP_SIMPLE_BOOT Please note that this method brings the system up with all memories set-up, but all other features, such as secure boot OTA or slots management are not available. -config ESP_HEAP_RUNTIME - bool - default y - help - Enabling this will allocate SRAM area starting from the last linked data at the symbolic `_end`, - ending at the last memory location that can be safely accessed (depending on a boot mode). - This is a memory pool used in runtime to create a new heap memory. - config ESP32_TIMER_TASK_STACK_SIZE int "Stack size of the high resolution ESP Timer" default 4096 diff --git a/soc/espressif/common/esp_heap_runtime.c b/soc/espressif/common/esp_heap_runtime.c deleted file mode 100644 index 210e714c792..00000000000 --- a/soc/espressif/common/esp_heap_runtime.c +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include "esp_log.h" - -#define TAG "heap_runtime" - -/* ESP dynamic pool heap */ -extern unsigned int z_mapped_end; -extern unsigned int _heap_sentry; -static void *esp_heap_runtime_init_mem = &z_mapped_end; - -#define ESP_HEAP_RUNTIME_MAX_SIZE ((uintptr_t)&_heap_sentry - (uintptr_t)&z_mapped_end) - -static struct k_heap esp_heap_runtime; - -static int esp_heap_runtime_init(void) -{ - ESP_EARLY_LOGI(TAG, "ESP heap runtime init at 0x%x size %d kB.\n", - esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE / 1024); - - k_heap_init(&esp_heap_runtime, esp_heap_runtime_init_mem, ESP_HEAP_RUNTIME_MAX_SIZE); - -#if defined(CONFIG_WIFI_ESP32) && defined(CONFIG_BT_ESP32) - assert(ESP_HEAP_RUNTIME_MAX_SIZE > 65535); -#elif defined(CONFIG_WIFI_ESP32) - assert(ESP_HEAP_RUNTIME_MAX_SIZE > 51200); -#elif defined(CONFIG_BT_ESP32) - assert(ESP_HEAP_RUNTIME_MAX_SIZE > 40960); -#endif - - return 0; -} - -void *esp_heap_runtime_malloc(size_t size) -{ - return k_heap_alloc(&esp_heap_runtime, size, K_NO_WAIT); -} - -void *esp_heap_runtime_calloc(size_t n, size_t size) -{ - size_t sz; - - if (__builtin_mul_overflow(n, size, &sz)) { - return NULL; - } - void *ptr = k_heap_alloc(&esp_heap_runtime, sz, K_NO_WAIT); - - if (ptr) { - memset(ptr, 0, sz); - } - - return ptr; -} - -void *esp_heap_runtime_realloc(void *ptr, size_t bytes) -{ - return k_heap_realloc(&esp_heap_runtime, ptr, bytes, K_NO_WAIT); -} - -void esp_heap_runtime_free(void *mem) -{ - k_heap_free(&esp_heap_runtime, mem); -} - -SYS_INIT(esp_heap_runtime_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); diff --git a/soc/espressif/common/include/esp_heap_runtime.h b/soc/espressif/common/include/esp_heap_runtime.h deleted file mode 100644 index 11f7139b715..00000000000 --- a/soc/espressif/common/include/esp_heap_runtime.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/** - * @brief Allocate memory from the esp_heap_runtime. - * - * @param size Amount of memory requested (in bytes). - * - * @return Address of the allocated memory if successful; otherwise NULL. - */ -void *esp_heap_runtime_malloc(size_t size); - -/** - * @brief Allocate memory from esp_heap_runtime, array style - * - * @param n Number of elements in the requested array - * @param size Size of each array element (in bytes). - * - * @return Address of the allocated memory if successful; otherwise NULL. - */ -void *esp_heap_runtime_calloc(size_t n, size_t size); - -/** - * @brief Reallocate memory from a esp_heap_runtime - * - * @param ptr Original pointer returned from a previous allocation - * @param bytes Desired size of block to allocate - * - * @return Pointer to memory the caller can now use, or NULL - */ -void *esp_heap_runtime_realloc(void *ptr, size_t bytes); - -/** - * @brief Free memory allocated from esp_heap_runtime. - * - * If @a ptr is NULL, no operation is performed. - * - * @param ptr Pointer to previously allocated memory. - */ -void esp_heap_runtime_free(void *mem); diff --git a/soc/espressif/common/loader.c b/soc/espressif/common/loader.c index 1cbab4b7829..ea9089da4f9 100644 --- a/soc/espressif/common/loader.c +++ b/soc/espressif/common/loader.c @@ -74,6 +74,7 @@ static HDR_ATTR void (*_entry_point)(void) = &__start; esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr; extern uint32_t _image_irom_start, _image_irom_size, _image_irom_vaddr; extern uint32_t _image_drom_start, _image_drom_size, _image_drom_vaddr; +extern uint32_t _libc_heap_size; #ifndef CONFIG_MCUBOOT static uint32_t _app_irom_start = @@ -84,6 +85,7 @@ static uint32_t _app_drom_start = (FIXED_PARTITION_OFFSET(slot0_partition) + (uint32_t)&_image_drom_start); static uint32_t _app_drom_size = (uint32_t)&_image_drom_size; +static uint32_t libc_heap_size = (uint32_t)&_libc_heap_size; #endif static uint32_t _app_irom_vaddr = ((uint32_t)&_image_irom_vaddr); @@ -260,7 +262,7 @@ void __start(void) ".option norelax\n" "la gp, __global_pointer$\n" ".option pop"); -#endif /* CONFIG_RISCV_GP */ +#endif #ifndef CONFIG_BOOTLOADER_MCUBOOT /* Init fundamental components */ @@ -278,12 +280,14 @@ void __start(void) #ifndef CONFIG_SOC_SERIES_ESP32C2 /* Disable RNG entropy source as it was already used */ soc_random_disable(); -#endif /* CONFIG_SOC_SERIES_ESP32C2 */ +#endif #if defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C3) /* Disable glitch detection as it can be falsely triggered by EMI interference */ ESP_EARLY_LOGI(TAG, "Disabling glitch detection"); ana_clock_glitch_reset_config(false); -#endif /* CONFIG_SOC_SERIES_ESP32S2 */ - ESP_EARLY_LOGI(TAG, "Jumping to the main image..."); +#endif +#if !defined(CONFIG_MCUBOOT) + ESP_EARLY_LOGI(TAG, "libc heap size %d kB.", libc_heap_size / 1024); +#endif __esp_platform_start(); } diff --git a/soc/espressif/esp32/default.ld b/soc/espressif/esp32/default.ld index 10a19e749a5..b8207b9aa71 100644 --- a/soc/espressif/esp32/default.ld +++ b/soc/espressif/esp32/default.ld @@ -100,7 +100,9 @@ _rom_store_table = 0; PROVIDE(_memmap_vecbase_reset = 0x40000450); PROVIDE(_memmap_reset_vector = 0x40000400); +/* Heap size calculations for PROCPU is also valid for AMP scenario */ _heap_sentry = SRAM2_DRAM_END; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32/default_appcpu.ld b/soc/espressif/esp32/default_appcpu.ld index 4761120afa6..71c640df534 100644 --- a/soc/espressif/esp32/default_appcpu.ld +++ b/soc/espressif/esp32/default_appcpu.ld @@ -72,7 +72,9 @@ _rom_store_table = 0; PROVIDE(_memmap_vecbase_reset = 0x40000450); PROVIDE(_memmap_reset_vector = 0x40000400); +/* Heap size calculations for APPCPU */ _heap_sentry = BOOTLOADER_DRAM_SEG_START + APPCPU_DRAM_SIZE; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32c2/default.ld b/soc/espressif/esp32c2/default.ld index e633adece44..07b46786c6f 100644 --- a/soc/espressif/esp32c2/default.ld +++ b/soc/espressif/esp32c2/default.ld @@ -85,8 +85,9 @@ _rom_store_table = 0; _iram_dram_offset = IRAM_DRAM_OFFSET; -/* Used as a pointer to the heap end */ +/* Heap size calculations */ _heap_sentry = DRAM_RESERVED_START; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32c3/default.ld b/soc/espressif/esp32c3/default.ld index b4ff2ea917e..46f179f446c 100644 --- a/soc/espressif/esp32c3/default.ld +++ b/soc/espressif/esp32c3/default.ld @@ -98,8 +98,9 @@ _rom_store_table = 0; _iram_dram_offset = IRAM_DRAM_OFFSET; -/* Stack sentry */ +/* Heap size calculations */ _heap_sentry = DRAM_RESERVED_START; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32c6/default.ld b/soc/espressif/esp32c6/default.ld index 4d497dcdade..74a6e13301e 100644 --- a/soc/espressif/esp32c6/default.ld +++ b/soc/espressif/esp32c6/default.ld @@ -100,7 +100,9 @@ REGION_ALIAS("rtc_data_location", rtc_iram_seg ); /* Default entry point: */ ENTRY(CONFIG_KERNEL_ENTRY) +/* Heap size calculations */ _heap_sentry = DRAM_RESERVED_START; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32s2/default.ld b/soc/espressif/esp32s2/default.ld index 979fb58baeb..f57add55aac 100644 --- a/soc/espressif/esp32s2/default.ld +++ b/soc/espressif/esp32s2/default.ld @@ -105,6 +105,7 @@ _rom_store_table = 0; /* Used as a pointer to the heap end */ _heap_sentry = DRAM_RESERVED_START; +_libc_heap_size = _heap_sentry - _end; SECTIONS { diff --git a/soc/espressif/esp32s3/default.ld b/soc/espressif/esp32s3/default.ld index 81008e09086..09c706f8bc3 100644 --- a/soc/espressif/esp32s3/default.ld +++ b/soc/espressif/esp32s3/default.ld @@ -102,13 +102,15 @@ MEMORY /* Default entry point: */ ENTRY(CONFIG_KERNEL_ENTRY) -/* Used as a pointer to the heap end */ +/* Heap size calculations differs between the APPCPU and PROCPU */ #ifdef CONFIG_SOC_ENABLE_APPCPU _heap_sentry = procpu_dram_end; #else _heap_sentry = DRAM_RESERVED_START; #endif +_libc_heap_size = _heap_sentry - _end; + SECTIONS { _iram_dram_offset = IRAM_DRAM_OFFSET; diff --git a/soc/espressif/esp32s3/default_appcpu.ld b/soc/espressif/esp32s3/default_appcpu.ld index 37b648be039..2b36ef3d19e 100644 --- a/soc/espressif/esp32s3/default_appcpu.ld +++ b/soc/espressif/esp32s3/default_appcpu.ld @@ -66,8 +66,9 @@ MEMORY /* Default entry point: */ ENTRY(__appcpu_start) -/* Used as a pointer to the heap end */ +/* Heap size calculations for APPCPU */ _heap_sentry = DRAM_RESERVED_START; +_libc_heap_size = _heap_sentry - _end; SECTIONS {