From 5c7a7a1d53a6f309688e6203d0d61a2a2c7d3f39 Mon Sep 17 00:00:00 2001 From: Sylvio Alves Date: Mon, 7 Oct 2024 17:25:21 -0300 Subject: [PATCH] soc: esp32: unify runtime heap usage This commit applies several changes in the way "heap_runtime" feature is used. It can't be split due to bisectabily issues. Whenever the feature is enabled, a new heap is created and custom malloc/calloc/free functions are added into the build system. Those functions are currently used for internal Wi-Fi and BLE drivers only. Such changes are described below: 1) Rename heap.c to esp_heap_runtime.c for better readability. 2) Rename RUNTIME_HEAP to HEAP_RUNTIME to make it similar to what is available in Zephyr. 3) Add runtime heap to BT as such as Wi-Fi. Fixes #79490 Fixes #79470 Signed-off-by: Sylvio Alves --- drivers/bluetooth/hci/Kconfig | 1 + drivers/bluetooth/hci/Kconfig.esp32 | 19 +++++ drivers/wifi/esp32/Kconfig.esp32 | 6 ++ soc/espressif/common/CMakeLists.txt | 6 +- soc/espressif/common/Kconfig | 2 +- soc/espressif/common/Kconfig.wifi | 2 +- soc/espressif/common/esp_heap_runtime.c | 77 +++++++++++++++++++ soc/espressif/common/heap.c | 48 ------------ .../common/include/esp_heap_runtime.h | 43 +++++++++++ west.yml | 2 +- 10 files changed, 151 insertions(+), 55 deletions(-) create mode 100644 drivers/bluetooth/hci/Kconfig.esp32 create mode 100644 soc/espressif/common/esp_heap_runtime.c delete mode 100644 soc/espressif/common/heap.c create mode 100644 soc/espressif/common/include/esp_heap_runtime.h diff --git a/drivers/bluetooth/hci/Kconfig b/drivers/bluetooth/hci/Kconfig index aacef695ef12af4..23a287176e51704 100644 --- a/drivers/bluetooth/hci/Kconfig +++ b/drivers/bluetooth/hci/Kconfig @@ -202,6 +202,7 @@ menuconfig BT_AIROC Infineon's AIROC™ Wi-Fi & combos portfolio integrates IEEE 802.11a/b/g/n/ac/ax Wi-Fi and Bluetooth® 5.2 in a single-chip solution to enable small-form-factor IoT designs. +source "drivers/bluetooth/hci/Kconfig.esp32" source "drivers/bluetooth/hci/Kconfig.infineon" source "drivers/bluetooth/hci/Kconfig.nxp" diff --git a/drivers/bluetooth/hci/Kconfig.esp32 b/drivers/bluetooth/hci/Kconfig.esp32 new file mode 100644 index 000000000000000..cea379b046bbb58 --- /dev/null +++ b/drivers/bluetooth/hci/Kconfig.esp32 @@ -0,0 +1,19 @@ +# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. +# SPDX-License-Identifier: Apache-2.0 + +if BT_ESP32 + +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 + + config ESP_BT_HEAP_SYSTEM + bool "Bluetooth adapter use system heap" + +endchoice # ESP_BT_HEAP + +endif diff --git a/drivers/wifi/esp32/Kconfig.esp32 b/drivers/wifi/esp32/Kconfig.esp32 index 14bb744c3cc4350..f6b37899a8ff2ce 100644 --- a/drivers/wifi/esp32/Kconfig.esp32 +++ b/drivers/wifi/esp32/Kconfig.esp32 @@ -20,6 +20,12 @@ menuconfig WIFI_ESP32 if WIFI_ESP32 +config HEAP_MEM_POOL_ADD_SIZE_WIFI + int + default 4096 + help + Make sure there is a minimal heap available for Wi-Fi driver. + config NET_TCP_WORKQ_STACK_SIZE default 2048 diff --git a/soc/espressif/common/CMakeLists.txt b/soc/espressif/common/CMakeLists.txt index f51b5f8683d2b86..d5e0a563412bc47 100644 --- a/soc/espressif/common/CMakeLists.txt +++ b/soc/espressif/common/CMakeLists.txt @@ -1,11 +1,9 @@ # Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. # SPDX-License-Identifier: Apache-2.0 -if(CONFIG_SOC_SERIES_ESP32 OR CONFIG_SOC_SERIES_ESP32S2 OR CONFIG_SOC_SERIES_ESP32S3) - zephyr_include_directories(include) -endif() +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_SPIRAM psram.c) - zephyr_sources_ifdef(CONFIG_ESP_RUNTIME_HEAP heap.c) + zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c) endif() diff --git a/soc/espressif/common/Kconfig b/soc/espressif/common/Kconfig index 23f82735f29f7b7..05ba9901f6ed459 100644 --- a/soc/espressif/common/Kconfig +++ b/soc/espressif/common/Kconfig @@ -24,7 +24,7 @@ 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_RUNTIME_HEAP +config ESP_HEAP_RUNTIME bool default y help diff --git a/soc/espressif/common/Kconfig.wifi b/soc/espressif/common/Kconfig.wifi index 0f329a0b2401e82..a52cff4858388a2 100644 --- a/soc/espressif/common/Kconfig.wifi +++ b/soc/espressif/common/Kconfig.wifi @@ -35,7 +35,7 @@ choice ESP_WIFI_HEAP config ESP_WIFI_HEAP_RUNTIME bool "Wifi adapter use ESP runtime heap" - depends on ESP_RUNTIME_HEAP + depends on ESP_HEAP_RUNTIME config ESP_WIFI_HEAP_SPIRAM bool "Wifi adapter use SPIRAM heap" diff --git a/soc/espressif/common/esp_heap_runtime.c b/soc/espressif/common/esp_heap_runtime.c new file mode 100644 index 000000000000000..210e714c7923daa --- /dev/null +++ b/soc/espressif/common/esp_heap_runtime.c @@ -0,0 +1,77 @@ +/* + * 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/heap.c b/soc/espressif/common/heap.c deleted file mode 100644 index 6e2d2efc9869a7a..000000000000000 --- a/soc/espressif/common/heap.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include -#include "esp_log.h" - -#define TAG "heap" - -/* ESP dynamic pool heap */ -extern unsigned int z_mapped_end; -extern unsigned int _heap_sentry; -static void *esp_runtime_heap_init_mem = &z_mapped_end; - -#define ESP_RUNTIME_HEAP_MAX_SIZE ((uintptr_t)&_heap_sentry - (uintptr_t)&z_mapped_end) - -struct k_heap esp_runtime_heap; - -static int esp_runtime_heap_init(void) -{ - ESP_EARLY_LOGI(TAG, "ESP runtime heap init at 0x%x size %d kB.\n", - esp_runtime_heap_init_mem, ESP_RUNTIME_HEAP_MAX_SIZE/1024); - - k_heap_init(&esp_runtime_heap, esp_runtime_heap_init_mem, ESP_RUNTIME_HEAP_MAX_SIZE); - -#ifdef CONFIG_ESP_WIFI_HEAP_RUNTIME - -#if defined(CONFIG_WIFI) && defined(CONFIG_BT) - assert(ESP_RUNTIME_HEAP_MAX_SIZE > 65535); -#elif defined(CONFIG_WIFI) - assert(ESP_RUNTIME_HEAP_MAX_SIZE > 51200); -#elif defined(CONFIG_BT) - assert(ESP_RUNTIME_HEAP_MAX_SIZE > 40960); -#endif - -#endif /* CONFIG_ESP_WIFI_HEAP_RUNTIME */ - - return 0; -} - -SYS_INIT(esp_runtime_heap_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 new file mode 100644 index 000000000000000..11f7139b71544e8 --- /dev/null +++ b/soc/espressif/common/include/esp_heap_runtime.h @@ -0,0 +1,43 @@ +/* + * 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/west.yml b/west.yml index e6b06fc71fab1b9..967e38e1150b85f 100644 --- a/west.yml +++ b/west.yml @@ -157,7 +157,7 @@ manifest: groups: - hal - name: hal_espressif - revision: ef9d884533208005468a118c7ada56e92ae9ba8b + revision: pull/348/head path: modules/hal/espressif west-commands: west/west-commands.yml groups: