From 0d9587952d6aabd29dc67f4f5c16f6dd3fcf12b4 Mon Sep 17 00:00:00 2001 From: Li Shuai Date: Thu, 16 Apr 2026 20:26:15 +0800 Subject: [PATCH] change(esp_wifi): support wifi module retention runtime attach and detach --- .../lowpower/port/esp32c5/sleep_clock.c | 4 ++-- components/esp_hw_support/sleep_modem.c | 6 +++++- components/esp_phy/esp32c5/phy_init_data.c | 13 ++++++++++++- components/esp_wifi/src/wifi_init.c | 11 ++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/components/esp_hw_support/lowpower/port/esp32c5/sleep_clock.c b/components/esp_hw_support/lowpower/port/esp32c5/sleep_clock.c index f8c71f4e6e1..2ca8d5defc5 100644 --- a/components/esp_hw_support/lowpower/port/esp32c5/sleep_clock.c +++ b/components/esp_hw_support/lowpower/port/esp32c5/sleep_clock.c @@ -142,7 +142,7 @@ ESP_SYSTEM_INIT_FN(sleep_clock_startup_init, SECONDARY, BIT(0), 106) { sleep_retention_module_init_param_t init_param = { .cbs = { .create = { .handle = sleep_clock_system_retention_init, .arg = NULL } }, - .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE + .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE | SLEEP_RETENTION_MODULE_ATTR_ATTACH }; sleep_retention_module_init(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM, &init_param); @@ -150,7 +150,7 @@ ESP_SYSTEM_INIT_FN(sleep_clock_startup_init, SECONDARY, BIT(0), 106) init_param = (sleep_retention_module_init_param_t) { .cbs = { .create = { .handle = sleep_clock_modem_retention_init, .arg = NULL } }, .depends.bitmap[SLEEP_RETENTION_MODULE_CLOCK_SYSTEM >> 5] = BIT(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM % 32), - .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE + .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE | SLEEP_RETENTION_MODULE_ATTR_ATTACH }; sleep_retention_module_init(SLEEP_RETENTION_MODULE_CLOCK_MODEM, &init_param); #endif diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index fb318afd660..72d9cc20ef0 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -198,6 +198,7 @@ bool modem_domain_pd_allowed(void) #if SOC_PM_MODEM_RETENTION_BY_REGDMA && SOC_PAU_SUPPORTED const sleep_retention_module_bitmap_t inited_modules = sleep_retention_get_inited_modules(); const sleep_retention_module_bitmap_t created_modules = sleep_retention_get_created_modules(); + const sleep_retention_module_bitmap_t retained_modules = sleep_retention_get_retained_modules(); sleep_retention_module_bitmap_t mask = (sleep_retention_module_bitmap_t){ .bitmap = { 0 } }; #if SOC_WIFI_SUPPORTED @@ -215,7 +216,10 @@ bool modem_domain_pd_allowed(void) const sleep_retention_module_bitmap_t modem_domain_inited_modules = sleep_retention_module_bitmap_and(inited_modules, mask); const sleep_retention_module_bitmap_t modem_domain_created_modules = sleep_retention_module_bitmap_and(created_modules, mask); - return sleep_retention_module_bitmap_eq(modem_domain_inited_modules, modem_domain_created_modules); + const sleep_retention_module_bitmap_t modem_domain_retained_modules = sleep_retention_module_bitmap_and(retained_modules, mask); + bool ic = sleep_retention_module_bitmap_eq(modem_domain_inited_modules, modem_domain_created_modules); + bool cr = sleep_retention_module_bitmap_eq(modem_domain_created_modules, modem_domain_retained_modules); + return ic && cr; #else return false; /* MODEM power domain is controlled by each module (WiFi, Bluetooth or 15.4) of modem */ #endif diff --git a/components/esp_phy/esp32c5/phy_init_data.c b/components/esp_phy/esp32c5/phy_init_data.c index fcf1e743e2b..a216a678c0b 100644 --- a/components/esp_phy/esp32c5/phy_init_data.c +++ b/components/esp_phy/esp32c5/phy_init_data.c @@ -316,6 +316,7 @@ void esp_phy_sleep_data_init(void) { sleep_retention_module_init_param_t init_param = { .cbs = { .create = { .handle = sleep_retention_wifi_bb_init, .arg = NULL } }, + .attribute = SLEEP_RETENTION_MODULE_ATTR_ATTACH, .depends = RETENTION_MODULE_BITMAP_INIT(CLOCK_MODEM) }; esp_err_t err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_WIFI_BB, &init_param); @@ -326,12 +327,22 @@ void esp_phy_sleep_data_init(void) err = sleep_retention_module_allocate(SLEEP_RETENTION_MODULE_WIFI_BB); if (err != ESP_OK) { ESP_LOGW(TAG, "failed to allocate sleep retention linked list for wifi bb retention"); + return; + } + err = sleep_retention_module_attach(SLEEP_RETENTION_MODULE_WIFI_BB); + if (err != ESP_OK) { + ESP_LOGW(TAG, "failed to attach sleep retention linked list for wifi bb retention"); } } void esp_phy_sleep_data_deinit(void) { - esp_err_t err = sleep_retention_module_free(SLEEP_RETENTION_MODULE_WIFI_BB); + esp_err_t err = sleep_retention_module_detach(SLEEP_RETENTION_MODULE_WIFI_BB); + if (err != ESP_OK) { + ESP_LOGW(TAG, "failed to detach sleep retention linked list for wifi bb retention"); + return; + } + err = sleep_retention_module_free(SLEEP_RETENTION_MODULE_WIFI_BB); if (err != ESP_OK) { ESP_LOGW(TAG, "failed to free sleep retention linked list for wifi bb retention"); return; diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 37e2552bfad..81394283272 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -162,6 +162,10 @@ static void esp_wifi_mac_pd_mem_init(void) if (err != ESP_OK) { ESP_LOGW(TAG, "failed to allocate sleep retention linked list for wifi mac retention"); } + err = sleep_retention_module_attach(SLEEP_RETENTION_MODULE_WIFI_MAC); + if (err != ESP_OK) { + ESP_LOGW(TAG, "failed to attach sleep retention linked list for wifi mac retention"); + } #endif esp_wifi_internal_set_mac_sleep(true); } @@ -169,7 +173,11 @@ static void esp_wifi_mac_pd_mem_deinit(void) { esp_wifi_internal_set_mac_sleep(false); #if SOC_PM_MODEM_RETENTION_BY_REGDMA - esp_err_t err = sleep_retention_module_free(SLEEP_RETENTION_MODULE_WIFI_MAC); + esp_err_t err = sleep_retention_module_detach(SLEEP_RETENTION_MODULE_WIFI_MAC); + if (err != ESP_OK) { + ESP_LOGW(TAG, "failed to detach sleep retention linked list for wifi mac retention"); + } + err = sleep_retention_module_free(SLEEP_RETENTION_MODULE_WIFI_MAC); if (err != ESP_OK) { ESP_LOGW(TAG, "failed to free sleep retention linked list for wifi mac retention"); } @@ -385,6 +393,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) #if SOC_PM_MODEM_RETENTION_BY_REGDMA sleep_retention_module_init_param_t init_param = { .cbs = { .create = { .handle = init_wifi_mac_sleep_retention, .arg = NULL } }, + .attribute = SLEEP_RETENTION_MODULE_ATTR_ATTACH }; init_param.depends.bitmap[SLEEP_RETENTION_MODULE_WIFI_BB >> 5] |= BIT(SLEEP_RETENTION_MODULE_WIFI_BB % 32); init_param.depends.bitmap[SLEEP_RETENTION_MODULE_CLOCK_MODEM >> 5] |= BIT(SLEEP_RETENTION_MODULE_CLOCK_MODEM % 32);