Skip to content

Commit

Permalink
soc: esp32: unify runtime heap usage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sylvioalves committed Oct 7, 2024
1 parent 2784ddc commit 5c7a7a1
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 55 deletions.
1 change: 1 addition & 0 deletions drivers/bluetooth/hci/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
19 changes: 19 additions & 0 deletions drivers/bluetooth/hci/Kconfig.esp32
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions drivers/wifi/esp32/Kconfig.esp32
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 2 additions & 4 deletions soc/espressif/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion soc/espressif/common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/common/Kconfig.wifi
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
77 changes: 77 additions & 0 deletions soc/espressif/common/esp_heap_runtime.c
Original file line number Diff line number Diff line change
@@ -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);
48 changes: 0 additions & 48 deletions soc/espressif/common/heap.c

This file was deleted.

43 changes: 43 additions & 0 deletions soc/espressif/common/include/esp_heap_runtime.h
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5c7a7a1

Please sign in to comment.