change(esp_hw_support): optimize retention driver RAM cost

(cherry picked from commit 11c590c053)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzhenghui
2026-08-06 15:58:07 +08:00
parent b034439eb3
commit 53e8b5440b
21 changed files with 33 additions and 68 deletions

View File

@@ -17,7 +17,7 @@ extern "C" {
#include "esp_regdma.h"
#include "soc/retention_periph_defs.h"
#define SLEEP_RETENTION_MODULE_BITMAP_SZ ((SLEEP_RETENTION_MODULE_MAX >> 5) + 1)
#define SLEEP_RETENTION_MODULE_BITMAP_SZ (((SLEEP_RETENTION_MODULE_MAX - 1) >> 5) + 1)
/**
* @file sleep_retention.h

View File

@@ -184,7 +184,7 @@ typedef struct {
#define SLEEP_RETENTION_REGDMA_LINK_NR_PRIORITIES (8u)
#define SLEEP_RETENTION_REGDMA_LINK_HIGHEST_PRIORITY (0)
#define SLEEP_RETENTION_REGDMA_LINK_LOWEST_PRIORITY (SLEEP_RETENTION_REGDMA_LINK_NR_PRIORITIES - 1)
#define SLEEP_RETENTION_MODULE_INVALID ((sleep_retention_module_t)(-1)) /* the final node does not belong to any module */
#define SLEEP_RETENTION_MODULE_INVALID ((sleep_retention_module_t)(SLEEP_RETENTION_MODULE_MAX)) /* the final node does not belong to any module */
struct {
sleep_retention_entries_t entries;
uint32_t entries_bitmap: REGDMA_LINK_ENTRY_NUM;
@@ -199,7 +199,7 @@ typedef struct {
sleep_retention_module_bitmap_t inited_modules;
sleep_retention_module_bitmap_t created_modules;
struct sleep_retention_module_object instance[SLEEP_RETENTION_MODULE_MAX + 1];
struct sleep_retention_module_object instance[SLEEP_RETENTION_MODULE_MAX];
#define EXTRA_LINK_NUM (REGDMA_LINK_ENTRY_NUM - 1)
} sleep_retention_t;
@@ -325,7 +325,7 @@ static void sleep_retention_entries_stats(void)
void sleep_retention_dump_modules(FILE *out)
{
for (int i = SLEEP_RETENTION_MODULE_MIN; i <= SLEEP_RETENTION_MODULE_MAX; i++) {
for (int i = SLEEP_RETENTION_MODULE_MIN; i < SLEEP_RETENTION_MODULE_MAX; i++) {
bool inited = sleep_retention_is_module_inited(i);
bool created = sleep_retention_is_module_created(i);
bool is_top = is_top_domain_module(i);
@@ -489,13 +489,17 @@ static void sleep_retention_entries_all_destroy_wrapper(sleep_retention_module_t
priority++;
}
} while (priority < SLEEP_RETENTION_REGDMA_LINK_NR_PRIORITIES);
s_retention.created_modules.bitmap[module >> 5] &= ~BIT(module % 32);
/* INVALID (== MAX) is a non-module sentinel; skip bitmap updates. */
if (module < SLEEP_RETENTION_MODULE_MAX) {
s_retention.created_modules.bitmap[module >> 5] &= ~BIT(module % 32);
}
_lock_release_recursive(&s_retention.lock);
}
static void sleep_retention_entries_do_destroy(sleep_retention_module_t module)
{
assert(SLEEP_RETENTION_MODULE_MIN <= module && module <= SLEEP_RETENTION_MODULE_MAX);
/* Allow SLEEP_RETENTION_MODULE_INVALID for final-default rollback. */
assert(SLEEP_RETENTION_MODULE_MIN <= module && module <= SLEEP_RETENTION_MODULE_INVALID);
_lock_acquire_recursive(&s_retention.lock);
sleep_retention_entries_join();
sleep_retention_entries_stats();
@@ -505,7 +509,7 @@ static void sleep_retention_entries_do_destroy(sleep_retention_module_t module)
static void sleep_retention_entries_destroy(sleep_retention_module_t module)
{
assert(SLEEP_RETENTION_MODULE_MIN <= module && module <= SLEEP_RETENTION_MODULE_MAX);
assert(SLEEP_RETENTION_MODULE_MIN <= module && module < SLEEP_RETENTION_MODULE_MAX);
_lock_acquire_recursive(&s_retention.lock);
sleep_retention_entries_do_destroy(module);
uint32_t created_modules = 0;
@@ -636,7 +640,7 @@ esp_err_t sleep_retention_entries_create(const sleep_retention_entries_config_t
if (priority >= SLEEP_RETENTION_REGDMA_LINK_NR_PRIORITIES) {
return ESP_ERR_INVALID_ARG;
}
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = sleep_retention_entries_check_and_create_final_default();
@@ -677,7 +681,7 @@ sleep_retention_module_bitmap_t IRAM_ATTR sleep_retention_get_created_modules(vo
bool sleep_retention_is_module_inited(sleep_retention_module_t module)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return false;
}
_lock_acquire_recursive(&s_retention.lock);
@@ -688,7 +692,7 @@ bool sleep_retention_is_module_inited(sleep_retention_module_t module)
bool sleep_retention_is_module_created(sleep_retention_module_t module)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return false;
}
_lock_acquire_recursive(&s_retention.lock);
@@ -733,7 +737,7 @@ bool IRAM_ATTR sleep_retention_module_bitmap_eq(sleep_retention_module_bitmap_t
esp_err_t sleep_retention_module_init(sleep_retention_module_t module, sleep_retention_module_init_param_t *param)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return ESP_ERR_INVALID_ARG;
}
if (param == NULL || param->cbs.create.handle == NULL) {
@@ -766,7 +770,7 @@ esp_err_t sleep_retention_module_init(sleep_retention_module_t module, sleep_ret
esp_err_t sleep_retention_module_deinit(sleep_retention_module_t module)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return ESP_ERR_INVALID_ARG;
}
@@ -797,7 +801,7 @@ esp_err_t sleep_retention_module_deinit(sleep_retention_module_t module)
static esp_err_t sleep_retention_passive_module_allocate(sleep_retention_module_t module)
{
assert(module >= SLEEP_RETENTION_MODULE_MIN && module <= SLEEP_RETENTION_MODULE_MAX);
assert(module >= SLEEP_RETENTION_MODULE_MIN && module < SLEEP_RETENTION_MODULE_MAX);
esp_err_t err = ESP_OK;
_lock_acquire_recursive(&s_retention.lock);
@@ -827,7 +831,7 @@ static esp_err_t sleep_retention_passive_module_allocate(sleep_retention_module_
esp_err_t sleep_retention_module_allocate(sleep_retention_module_t module)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return ESP_ERR_INVALID_ARG;
}
@@ -865,7 +869,7 @@ esp_err_t sleep_retention_module_allocate(sleep_retention_module_t module)
static esp_err_t sleep_retention_passive_module_free(sleep_retention_module_t module)
{
assert(module >= SLEEP_RETENTION_MODULE_MIN && module <= SLEEP_RETENTION_MODULE_MAX);
assert(module >= SLEEP_RETENTION_MODULE_MIN && module < SLEEP_RETENTION_MODULE_MAX);
esp_err_t err = ESP_OK;
_lock_acquire_recursive(&s_retention.lock);
@@ -893,7 +897,7 @@ static esp_err_t sleep_retention_passive_module_free(sleep_retention_module_t mo
esp_err_t sleep_retention_module_free(sleep_retention_module_t module)
{
if (module < SLEEP_RETENTION_MODULE_MIN || module > SLEEP_RETENTION_MODULE_MAX) {
if (module < SLEEP_RETENTION_MODULE_MIN || module >= SLEEP_RETENTION_MODULE_MAX) {
return ESP_ERR_INVALID_ARG;
}

View File

@@ -1759,10 +1759,6 @@ config SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED
bool
default y
config SOC_PM_RETENTION_MODULE_NUM
int
default 40
config SOC_CLK_RC_FAST_SUPPORT_CALIBRATION
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -57,7 +56,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_MODEM_PHY = 31,
SLEEP_RETENTION_MODULE_PHY_FE = 32,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -676,8 +676,6 @@
#define SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED (1)
#define SOC_PM_RETENTION_MODULE_NUM (40)
/*-------------------------- CLOCK SUBSYSTEM CAPS ----------------------------------------*/
#define SOC_CLK_RC_FAST_SUPPORT_CALIBRATION (1)
#define SOC_MODEM_CLOCK_IS_INDEPENDENT (1)

View File

@@ -1519,10 +1519,6 @@ config SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED
bool
default y
config SOC_PM_RETENTION_MODULE_NUM
int
default 32
config SOC_CLK_RC_FAST_SUPPORT_CALIBRATION
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -56,7 +55,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_802154_MAC = 30,
SLEEP_RETENTION_MODULE_PHY_FE = 31,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -591,8 +591,6 @@
#define SOC_PM_PAU_REGDMA_UPDATE_CACHE_BEFORE_WAIT_COMPARE (1)
#define SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED (1)
#define SOC_PM_RETENTION_MODULE_NUM (32)
/*-------------------------- CLOCK SUBSYSTEM CAPS ----------------------------------------*/
#define SOC_CLK_RC_FAST_SUPPORT_CALIBRATION (1)
#define SOC_MODEM_CLOCK_IS_INDEPENDENT (1)

View File

@@ -1239,10 +1239,6 @@ config SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED
bool
default y
config SOC_PM_RETENTION_MODULE_NUM
int
default 32
config SOC_CLK_RC_FAST_SUPPORT_CALIBRATION
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -49,7 +48,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_802154_MAC = 30,
SLEEP_RETENTION_MODULE_PHY_FE = 31,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -495,8 +495,6 @@
#define SOC_PM_PAU_REGDMA_UPDATE_CACHE_BEFORE_WAIT_COMPARE (1)
#define SOC_PM_PMU_MIN_SLP_SLOW_CLK_CYCLE_FIXED (1)
#define SOC_PM_RETENTION_MODULE_NUM (32)
/*-------------------------- CLOCK SUBSYSTEM CAPS ----------------------------------------*/
#define SOC_CLK_RC_FAST_SUPPORT_CALIBRATION (1)
#define SOC_MODEM_CLOCK_IS_INDEPENDENT (1)

View File

@@ -1467,10 +1467,6 @@ config SOC_PM_PAU_REGDMA_UPDATE_CACHE_BEFORE_WAIT_COMPARE
bool
default y
config SOC_PM_RETENTION_MODULE_NUM
int
default 32
config SOC_EXT_MEM_CACHE_TAG_IN_CPU_DOMAIN
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -53,7 +52,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_BT_BB = 29,
SLEEP_RETENTION_MODULE_802154_MAC = 30,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -587,8 +587,6 @@
#define SOC_PM_PAU_REGDMA_UPDATE_CACHE_BEFORE_WAIT_COMPARE (1)
#define SOC_PM_RETENTION_MODULE_NUM (32)
#define SOC_EXT_MEM_CACHE_TAG_IN_CPU_DOMAIN (1)
#define SOC_PM_CPU_RETENTION_BY_SW (1)
#define SOC_PM_MODEM_RETENTION_BY_REGDMA (1)

View File

@@ -855,10 +855,6 @@ config SOC_PM_PAU_LINK_NUM
int
default 5
config SOC_PM_RETENTION_MODULE_NUM
int
default 32
config SOC_PM_PAU_REGDMA_LINK_CONFIGURABLE
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -53,7 +52,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_BT_BB = 29,
SLEEP_RETENTION_MODULE_802154_MAC = 30,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -536,7 +536,6 @@
#define SOC_PM_SUPPORT_VDDSDIO_PD (1)
#define SOC_PM_SUPPORT_TOP_PD (1)
#define SOC_PM_PAU_LINK_NUM (5)
#define SOC_PM_RETENTION_MODULE_NUM (32)
#define SOC_PM_PAU_REGDMA_LINK_CONFIGURABLE (1)
#define SOC_PM_CPU_RETENTION_BY_SW (1)
#define SOC_PM_MODEM_RETENTION_BY_REGDMA (1)

View File

@@ -2183,10 +2183,6 @@ config SOC_SLEEP_TGWDT_STOP_WORKAROUND
bool
default y
config SOC_PM_RETENTION_MODULE_NUM
int
default 64
config SOC_PSRAM_VDD_POWER_MPLL
bool
default y

View File

@@ -7,7 +7,6 @@
#pragma once
#include <stdint.h>
#include "soc_caps.h"
#ifdef __cplusplus
extern "C" {
@@ -65,7 +64,7 @@ typedef enum periph_retention_module {
SLEEP_RETENTION_MODULE_DMA2D = 41,
SLEEP_RETENTION_MODULE_PPA = 42,
SLEEP_RETENTION_MODULE_MAX = SOC_PM_RETENTION_MODULE_NUM - 1
SLEEP_RETENTION_MODULE_MAX,
} periph_retention_module_t;
#define is_top_domain_module(m) \

View File

@@ -801,8 +801,6 @@
#define SOC_SLEEP_SYSTIMER_STALL_WORKAROUND 1 //TODO IDF-11381: replace with all xtal field clk gate control
#define SOC_SLEEP_TGWDT_STOP_WORKAROUND 1 //TODO IDF-11381: replace with all xtal field clk gate control
#define SOC_PM_RETENTION_MODULE_NUM (64)
/*-------------------------- PSRAM CAPS ----------------------------*/
#define SOC_PSRAM_VDD_POWER_MPLL (1)

View File

@@ -18,6 +18,8 @@ extern "C" {
#if SOC_PAU_SUPPORTED
#include "soc/retention_periph_defs.h"
#define REGDMA_LINK_ENTRY_NUM (SOC_PM_PAU_LINK_NUM) /* Maximum number of REG DMA linked list entries */
#ifndef ARRAY_SIZE
@@ -181,15 +183,15 @@ typedef struct regdma_link_branch_write_wait_body {
volatile uint32_t mask;
} regdma_link_branch_write_wait_body_t;
ESP_STATIC_ASSERT(REGDMA_LINK_ENTRY_NUM <= 16, "regdma link entry number should equal to and less than 16");
ESP_STATIC_ASSERT(REGDMA_LINK_ENTRY_NUM < 16, "regdma link entry number must be less than 16 to pack module into stats");
typedef struct regdma_link_stats {
volatile uint32_t ref: REGDMA_LINK_ENTRY_NUM, /* a bitmap, identifies which entry has referenced the current link */
#if REGDMA_LINK_ENTRY_NUM < 16
reserve: 16 - REGDMA_LINK_ENTRY_NUM,
#endif
module: 16 - REGDMA_LINK_ENTRY_NUM, /* module id; width leaves room beside ref within the low 16 bits */
id: 16; /* REGDMA linked list node unique identifier */
volatile int module; /* a number used to identify the module to which the current node belongs */
} regdma_link_stats_t;
ESP_STATIC_ASSERT(sizeof(regdma_link_stats_t) == 4, "regdma_link_stats_t must be 4 bytes");
ESP_STATIC_ASSERT(SLEEP_RETENTION_MODULE_MAX < (1u << (16 - REGDMA_LINK_ENTRY_NUM)),
"module id exceeds bitfield width");
typedef struct regdma_link_continuous {
regdma_link_stats_t stat;