From 76486bbfcbf217db1ea5049237c7b34c34d3740d Mon Sep 17 00:00:00 2001 From: Wang Mengyang Date: Wed, 20 Mar 2024 19:27:43 +0800 Subject: [PATCH] change(bt): Rename linker script file names and symbol names 1. rename linker files 2. support memory release in case that ESP_ALLOW_BSS_SEG_EXTERNAL_MEMORY is enabled 3. improve the implementation of memory release --- components/bt/CMakeLists.txt | 3 +- components/bt/controller/esp32/bt.c | 115 ++++++++++++------ components/bt/controller/esp32c2/bt.c | 13 +- components/bt/controller/esp32c3/bt.c | 100 ++++++++++----- components/bt/controller/esp32h2/bt.c | 50 ++++---- components/bt/linker_common.lf | 16 ++- ...er_common_esp32c2.lf => linker_esp32c2.lf} | 16 ++- components/bt/linker_esp_ble_controller.lf | 2 +- components/bt/linker_rw_bt_controller.lf | 2 +- 9 files changed, 213 insertions(+), 104 deletions(-) rename components/bt/{linker_common_esp32c2.lf => linker_esp32c2.lf} (62%) diff --git a/components/bt/CMakeLists.txt b/components/bt/CMakeLists.txt index 99309cf455..0ea2e1ac17 100644 --- a/components/bt/CMakeLists.txt +++ b/components/bt/CMakeLists.txt @@ -29,8 +29,7 @@ if(CONFIG_BT_ENABLED) elseif(CONFIG_IDF_TARGET_ESP32C2) list(APPEND srcs "controller/esp32c2/bt.c") list(APPEND include_dirs include/esp32c2/include) - set(ldscripts "linker_common_esp32c2.lf") - list(APPEND ldscripts "linker_esp_ble_controller.lf") + set(ldscripts "linker_esp32c2.lf") endif() diff --git a/components/bt/controller/esp32/bt.c b/components/bt/controller/esp32/bt.c index 07f9cd45b1..4567226ddb 100644 --- a/components/bt/controller/esp32/bt.c +++ b/components/bt/controller/esp32/bt.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -801,7 +801,7 @@ static int32_t queue_send_hlevel_wrapper(void *queue, void *item, uint32_t block * @param item The message which will be send * @param hptw need do task yield or not * @return send success or not - * There is an issue here: When the queue is full, it may reture true but it send fail to the queue, sometimes. + * There is an issue here: When the queue is full, it may return true but it send fail to the queue, sometimes. * But in Bluetooth controller's isr, We don't care about the return value. * It only required tp send success when the queue is empty all the time. * So, this function meets the requirement. @@ -1352,7 +1352,26 @@ static esp_err_t esp_bt_mem_release_area(const bt_area_t *area) return ret; } -esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) +static esp_err_t esp_bt_mem_release_areas(const bt_area_t *area1, const bt_area_t *area2) +{ + esp_err_t ret = ESP_OK; + + if (area1->end == area2->start) { + bt_area_t merged_area = { + .start = area1->start, + .end = area2->end, + .name = area1->name + }; + ret = esp_bt_mem_release_area(&merged_area); + } else { + esp_bt_mem_release_area(area1); + ret = esp_bt_mem_release_area(area2); + } + + return ret; +} + +static esp_err_t esp_bt_controller_rom_mem_release(esp_bt_mode_t mode) { bool update = true; intptr_t mem_start=(intptr_t) NULL, mem_end=(intptr_t) NULL; @@ -1403,6 +1422,17 @@ esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) } } + return ESP_OK; +} + +esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) +{ + esp_err_t ret = ESP_OK; + + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + if (mode == ESP_BT_MODE_BTDM) { bt_area_t cont_bss = { .start = (intptr_t)&_bt_controller_bss_start, @@ -1414,51 +1444,64 @@ esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) .end = (intptr_t)&_bt_controller_data_end, .name = "BT Controller Data" }; - esp_bt_mem_release_area(&cont_bss); - esp_bt_mem_release_area(&cont_data); + + ret = esp_bt_mem_release_areas(&cont_data, &cont_bss); } - return ESP_OK; + if (ret == ESP_OK) { + ret = esp_bt_controller_rom_mem_release(mode); + } + + return ret; } esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) { - int ret; - intptr_t mem_start, mem_end; + esp_err_t ret = ESP_OK; - ret = esp_bt_controller_mem_release(mode); - if (ret != ESP_OK) { - return ret; + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; } + bt_area_t bss = { + .start = (intptr_t)&_bt_bss_start, + .end = (intptr_t)&_bt_bss_end, + .name = "BT BSS", + }; + bt_area_t cont_bss = { + .start = (intptr_t)&_bt_controller_bss_start, + .end = (intptr_t)&_bt_controller_bss_end, + .name = "BT Controller BSS", + }; + bt_area_t data = { + .start = (intptr_t)&_bt_data_start, + .end = (intptr_t)&_bt_data_end, + .name = "BT Data", + }; + bt_area_t cont_data = { + .start = (intptr_t)&_bt_controller_data_start, + .end = (intptr_t)&_bt_controller_data_end, + .name = "BT Controller Data" + }; + if (mode == ESP_BT_MODE_BTDM) { - mem_start = (intptr_t)&_bt_bss_start; - mem_end = (intptr_t)&_bt_bss_end; - if (mem_start != mem_end) { - ESP_LOGD(BTDM_LOG_TAG, "Release BT BSS [0x%08x] - [0x%08x]", mem_start, mem_end); - ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); - } - mem_start = (intptr_t)&_bt_data_start; - mem_end = (intptr_t)&_bt_data_end; - if (mem_start != mem_end) { - ESP_LOGD(BTDM_LOG_TAG, "Release BT Data [0x%08x] - [0x%08x]", mem_start, mem_end); - ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + /* Start by freeing Bluetooth BSS section */ + if (ret == ESP_OK) { + ret = esp_bt_mem_release_areas(&bss, &cont_bss); } - mem_start = (intptr_t)&_bt_controller_bss_start; - mem_end = (intptr_t)&_bt_controller_bss_end; - if (mem_start != mem_end) { - ESP_LOGD(BTDM_LOG_TAG, "Release Controller BSS [0x%08x] - [0x%08x]", mem_start, mem_end); - ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); - } - mem_start = (intptr_t)&_bt_controller_data_start; - mem_end = (intptr_t)&_bt_controller_data_end; - if (mem_start != mem_end) { - ESP_LOGD(BTDM_LOG_TAG, "Release Controller Data [0x%08x] - [0x%08x]", mem_start, mem_end); - ESP_ERROR_CHECK(try_heap_caps_add_region(mem_start, mem_end)); + /* Do the same thing with the Bluetooth data section */ + if (ret == ESP_OK) { + ret = esp_bt_mem_release_areas(&data, &cont_data); } } - return ESP_OK; + + /* free data and BSS section for Bluetooth controller ROM code */ + if (ret == ESP_OK) { + ret = esp_bt_controller_rom_mem_release(mode); + } + + return ret; } #if CONFIG_BTDM_CTRL_HLI @@ -1767,7 +1810,7 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) sdk_config_set_bt_pll_track_enable(true); - // inititalize bluetooth baseband + // initialize bluetooth baseband btdm_check_and_init_bb(); ret = btdm_controller_enable(mode); @@ -1930,7 +1973,7 @@ esp_err_t esp_ble_scan_dupilcate_list_flush(void) /** * This function re-write controller's function, - * As coredump can not show paramerters in function which is in a .a file. + * As coredump can not show parameters in function which is in a .a file. * * After coredump fixing this issue, just delete this function. */ diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index f475114128..f9f8d72bca 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -926,6 +926,7 @@ static esp_err_t esp_bt_mem_release_area(const bt_area_t *area) return ret; } +#ifndef CONFIG_BT_RELEASE_IRAM static esp_err_t esp_bt_mem_release_areas(const bt_area_t *area1, const bt_area_t *area2) { esp_err_t ret = ESP_OK; @@ -944,6 +945,7 @@ static esp_err_t esp_bt_mem_release_areas(const bt_area_t *area1, const bt_area_ return ret; } +#endif esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) { @@ -956,18 +958,21 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) * memory into 3 different regions (IRAM, BLE-IRAM, DRAM). So `ESP_SYSTEM_PMP_IDRAM_SPLIT` needs * to be disabled. */ - ESP_LOGE(NIMBLE_PORT_LOG_TAG, "`ESP_SYSTEM_PMP_IDRAM_SPLIT` should be disabled!"); - assert(0); + #error "ESP_SYSTEM_PMP_IDRAM_SPLIT should be disabled to allow BT to be released" #endif // CONFIG_BT_RELEASE_IRAM && CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT + if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + if ((mode & ESP_BT_MODE_BLE) == 0) { return ret; } #if CONFIG_BT_RELEASE_IRAM bt_area_t merged_region = { - .start = (intptr_t)MAP_IRAM_TO_DRAM((intptr_t)&_iram_bt_text_start); - .end = (intptr_t)&_bss_bt_end; + .start = (intptr_t)MAP_IRAM_TO_DRAM((intptr_t)&_iram_bt_text_start), + .end = (intptr_t)&_bss_bt_end, .name = "BT Text, BSS and Data" }; ret = esp_bt_mem_release_area(&merged_region); diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 98ac3bb7f0..34aa5c2733 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -1020,42 +1020,45 @@ typedef struct { const char* name; } bt_area_t; -static esp_err_t esp_bt_mem_release_areas(const bt_area_t* area1, const bt_area_t* area2) +static esp_err_t esp_bt_mem_release_area(const bt_area_t *area) { esp_err_t ret = ESP_OK; - intptr_t mem_start = 0; - intptr_t mem_end = 0; + intptr_t mem_start = area->start; + intptr_t mem_end = area->end; + if (mem_start != mem_end) { + ESP_LOGD(BT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area->name, mem_start, mem_end, mem_end - mem_start); + ret = try_heap_caps_add_region(mem_start, mem_end); + } + return ret; +} - if(area1->end == area2->start) { - mem_start = area1->start; - mem_end = area2->end; - if (mem_start != mem_end) { - ESP_LOGD(BT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area1->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } +static esp_err_t esp_bt_mem_release_areas(const bt_area_t *area1, const bt_area_t *area2) +{ + esp_err_t ret = ESP_OK; + + if (area1->end == area2->start) { + bt_area_t merged_area = { + .start = area1->start, + .end = area2->end, + .name = area1->name + }; + ret = esp_bt_mem_release_area(&merged_area); } else { - mem_start = area1->start; - mem_end = area1->end; - if (mem_start != mem_end) { - ESP_LOGD(BT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area1->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } - - mem_start = area2->start; - mem_end = area2->end; - if (ret == ESP_OK && mem_start != mem_end) { - ESP_LOGD(BT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area2->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } + esp_bt_mem_release_area(area1); + ret = esp_bt_mem_release_area(area2); } return ret; } - -esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) +esp_err_t esp_bt_controller_rom_mem_release(esp_bt_mode_t mode) { esp_err_t ret = ESP_OK; + + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + bt_area_t rom_btdm_data = { .start = (intptr_t) ets_rom_layout_p->data_start_btdm, .end = (intptr_t) ets_rom_layout_p->data_end_btdm, @@ -1077,7 +1080,6 @@ esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) .name = "ROM interface btdm BSS", }; - if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { ret = ESP_ERR_INVALID_STATE; } @@ -1096,9 +1098,48 @@ esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) return ret; } +esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode) +{ + esp_err_t ret = ESP_OK; + + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + + bt_area_t cont_bss = { + .start = (intptr_t)&_bt_controller_bss_start, + .end = (intptr_t)&_bt_controller_bss_end, + .name = "BT Controller BSS", + }; + + bt_area_t cont_data = { + .start = (intptr_t)&_bt_controller_data_start, + .end = (intptr_t)&_bt_controller_data_end, + .name = "BT Controller Data" + }; + + if (mode & ESP_BT_MODE_BLE) { + /* free data and BSS section for libbtdm_app.a */ + if (ret == ESP_OK) { + ret = esp_bt_mem_release_areas(&cont_data, &cont_bss); + } + /* free data and BSS section for Bluetooth controller ROM code */ + if (ret == ESP_OK) { + ret = esp_bt_controller_rom_mem_release(mode); + } + } + + return ret; +} + esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) { esp_err_t ret = ESP_OK; + + if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + bt_area_t bss = { .start = (intptr_t)&_bt_bss_start, .end = (intptr_t)&_bt_bss_end, @@ -1120,8 +1161,6 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) .name = "BT Controller Data" }; - ret = esp_bt_controller_mem_release(mode); - if (mode & ESP_BT_MODE_BLE) { /* Start by freeing Bluetooth BSS section */ if (ret == ESP_OK) { @@ -1132,6 +1171,11 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) if (ret == ESP_OK) { ret = esp_bt_mem_release_areas(&data, &cont_data); } + + /* free data and BSS section for Bluetooth controller ROM code */ + if (ret == ESP_OK) { + ret = esp_bt_controller_rom_mem_release(mode); + } } return ret; diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index df164f15af..1fc30c6880 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -813,33 +813,32 @@ typedef struct { const char* name; } bt_area_t; -static esp_err_t esp_bt_mem_release_areas(const bt_area_t* area1, const bt_area_t* area2) +static esp_err_t esp_bt_mem_release_area(const bt_area_t *area) { esp_err_t ret = ESP_OK; - intptr_t mem_start = 0; - intptr_t mem_end = 0; + intptr_t mem_start = area->start; + intptr_t mem_end = area->end; + if (mem_start != mem_end) { + ESP_LOGD(NIMBLE_PORT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area->name, mem_start, mem_end, mem_end - mem_start); + ret = try_heap_caps_add_region(mem_start, mem_end); + } + return ret; +} - if(area1->end == area2->start) { - mem_start = area1->start; - mem_end = area2->end; - if (mem_start != mem_end) { - ESP_LOGD(NIMBLE_PORT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area1->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } +static esp_err_t esp_bt_mem_release_areas(const bt_area_t *area1, const bt_area_t *area2) +{ + esp_err_t ret = ESP_OK; + + if (area1->end == area2->start) { + bt_area_t merged_area = { + .start = area1->start, + .end = area2->end, + .name = area1->name + }; + ret = esp_bt_mem_release_area(&merged_area); } else { - mem_start = area1->start; - mem_end = area1->end; - if (mem_start != mem_end) { - ESP_LOGD(NIMBLE_PORT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area1->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } - - mem_start = area2->start; - mem_end = area2->end; - if (ret == ESP_OK && mem_start != mem_end) { - ESP_LOGD(NIMBLE_PORT_LOG_TAG, "Release %s [0x%08x] - [0x%08x], len %d", area2->name, mem_start, mem_end, mem_end - mem_start); - ret = try_heap_caps_add_region(mem_start, mem_end); - } + esp_bt_mem_release_area(area1); + ret = esp_bt_mem_release_area(area2); } return ret; @@ -848,6 +847,11 @@ static esp_err_t esp_bt_mem_release_areas(const bt_area_t* area1, const bt_area_ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode) { esp_err_t ret = ESP_OK; + + if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_IDLE) { + return ESP_ERR_INVALID_STATE; + } + bt_area_t bss = { .start = (intptr_t)&_bt_bss_start, .end = (intptr_t)&_bt_bss_end, diff --git a/components/bt/linker_common.lf b/components/bt/linker_common.lf index af38b5af11..501acd9505 100644 --- a/components/bt/linker_common.lf +++ b/components/bt/linker_common.lf @@ -6,7 +6,7 @@ entries: entries: COMMON -[scheme:bt_start_end] +[scheme:bt_default] entries: bt_bss -> dram0_bss bt_common -> dram0_bss @@ -30,7 +30,13 @@ entries: [mapping:bt] archive: libbt.a entries: - * (bt_start_end); - bt_bss -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_bss), - bt_common -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_common), - data -> dram0_data ALIGN(4) ALIGN(4, post) SURROUND(bt_data) + if ESP_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y: + * (bt_extram_bss); + bt_bss -> extern_ram ALIGN(4) ALIGN(4, post) SURROUND(bt_bss), + bt_common -> extern_ram ALIGN(4) ALIGN(4, post) SURROUND(bt_common), + data -> dram0_data ALIGN(4) ALIGN(4, post) SURROUND(bt_data) + else: + * (bt_default); + bt_bss -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_bss), + bt_common -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_common), + data -> dram0_data ALIGN(4) ALIGN(4, post) SURROUND(bt_data) diff --git a/components/bt/linker_common_esp32c2.lf b/components/bt/linker_esp32c2.lf similarity index 62% rename from components/bt/linker_common_esp32c2.lf rename to components/bt/linker_esp32c2.lf index 2fb68d6850..7178420a00 100644 --- a/components/bt/linker_common_esp32c2.lf +++ b/components/bt/linker_esp32c2.lf @@ -1,4 +1,4 @@ -[sections:bt_text] +[sections:bt_iram_text] entries: .iram1+ @@ -17,9 +17,9 @@ entries: entries: COMMON -[scheme:bt_start_end] +[scheme:bt_default] entries: - bt_text -> iram0_bt_text + bt_iram_text -> iram0_bt_text bt_bss -> dram0_bt_bss bt_common -> dram0_bt_bss bt_data -> dram0_bt_data @@ -36,7 +36,15 @@ entries: [mapping:bt] archive: libbt.a entries: - * (bt_start_end); + * (bt_default); bt_bss -> dram0_bt_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_bss), bt_common -> dram0_bt_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_common), bt_data -> dram0_bt_data ALIGN(4) ALIGN(4, post) SURROUND(bt_data) + +[mapping:ble_app] +archive: libble_app.a +entries: + * (bt_default); + bt_bss -> dram0_bt_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_bss), + bt_common -> dram0_bt_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_common), + bt_data -> dram0_bt_data ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_data) diff --git a/components/bt/linker_esp_ble_controller.lf b/components/bt/linker_esp_ble_controller.lf index e5b8590988..fe8598112b 100644 --- a/components/bt/linker_esp_ble_controller.lf +++ b/components/bt/linker_esp_ble_controller.lf @@ -1,7 +1,7 @@ [mapping:ble_app] archive: libble_app.a entries: - * (bt_start_end); + * (bt_default); bt_bss -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_bss), bt_common -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_common), data -> dram0_data ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_data) diff --git a/components/bt/linker_rw_bt_controller.lf b/components/bt/linker_rw_bt_controller.lf index 3b19bdbe24..083d6e90b7 100644 --- a/components/bt/linker_rw_bt_controller.lf +++ b/components/bt/linker_rw_bt_controller.lf @@ -1,7 +1,7 @@ [mapping:btdm] archive: libbtdm_app.a entries: - * (bt_start_end); + * (bt_default); bt_bss -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_bss), bt_common -> dram0_bss ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_common), data -> dram0_data ALIGN(4) ALIGN(4, post) SURROUND(bt_controller_data)