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 bisectability 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 <sylvio.alves@espressif.com>
This commit is contained in:
Sylvio Alves 2024-10-07 17:25:21 -03:00 committed by Carles Cufí
commit 59f0418d2e
10 changed files with 151 additions and 55 deletions

View file

@ -202,6 +202,7 @@ config BT_AIROC
Infineon's AIROC™ Wi-Fi & combos portfolio integrates 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 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. 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.infineon"
source "drivers/bluetooth/hci/Kconfig.nxp" source "drivers/bluetooth/hci/Kconfig.nxp"

View file

@ -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

View file

@ -20,6 +20,12 @@ menuconfig WIFI_ESP32
if 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 config NET_TCP_WORKQ_STACK_SIZE
default 2048 default 2048

View file

@ -1,11 +1,9 @@
# Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd. # Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0 # 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)
zephyr_include_directories(include)
endif()
if(NOT CONFIG_MCUBOOT AND NOT CONFIG_SOC_ESP32_APPCPU AND NOT CONFIG_SOC_ESP32S3_APPCPU) 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_SPIRAM psram.c)
zephyr_sources_ifdef(CONFIG_ESP_RUNTIME_HEAP heap.c) zephyr_sources_ifdef(CONFIG_ESP_HEAP_RUNTIME esp_heap_runtime.c)
endif() endif()

View file

@ -24,7 +24,7 @@ config ESP_SIMPLE_BOOT
Please note that this method brings the system up with all memories set-up, but 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. all other features, such as secure boot OTA or slots management are not available.
config ESP_RUNTIME_HEAP config ESP_HEAP_RUNTIME
bool bool
default y default y
help help

View file

@ -35,7 +35,7 @@ choice ESP_WIFI_HEAP
config ESP_WIFI_HEAP_RUNTIME config ESP_WIFI_HEAP_RUNTIME
bool "Wifi adapter use ESP runtime heap" bool "Wifi adapter use ESP runtime heap"
depends on ESP_RUNTIME_HEAP depends on ESP_HEAP_RUNTIME
config ESP_WIFI_HEAP_SPIRAM config ESP_WIFI_HEAP_SPIRAM
bool "Wifi adapter use SPIRAM heap" bool "Wifi adapter use SPIRAM heap"

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <zephyr/types.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <esp_err.h>
#include <esp_heap_runtime.h>
#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);

View file

@ -1,48 +0,0 @@
/*
* Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stddef.h>
#include <stdint.h>
#include <zephyr/types.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <esp_err.h>
#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);

View file

@ -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);

View file

@ -157,7 +157,7 @@ manifest:
groups: groups:
- hal - hal
- name: hal_espressif - name: hal_espressif
revision: 61a002ad757f567cdef92014b483e6f325c41afc revision: 5e7220b429caa5d374286b3a3122c5d303c5f78a
path: modules/hal/espressif path: modules/hal/espressif
west-commands: west/west-commands.yml west-commands: west/west-commands.yml
groups: groups: