From ccd4ff2945e4a2cbc2c73410006a24d0f42fe6b3 Mon Sep 17 00:00:00 2001 From: Li Shuai Date: Tue, 28 Mar 2023 16:47:48 +0800 Subject: [PATCH] sleep retention: implement the extra linked list retention for modules with retention clock bugs --- .../include/esp_private/sleep_retention.h | 10 +++ components/esp_hw_support/sleep_retention.c | 82 +++++++++++++++++-- .../esp32c6/include/soc/Kconfig.soc_caps.in | 4 + components/soc/esp32c6/include/soc/soc_caps.h | 1 + 4 files changed, 89 insertions(+), 8 deletions(-) diff --git a/components/esp_hw_support/include/esp_private/sleep_retention.h b/components/esp_hw_support/include/esp_private/sleep_retention.h index 10a0d1b80b..98ed4b01c9 100644 --- a/components/esp_hw_support/include/esp_private/sleep_retention.h +++ b/components/esp_hw_support/include/esp_private/sleep_retention.h @@ -101,6 +101,16 @@ void * sleep_retention_find_link_by_id(int id); */ void sleep_retention_entries_get(sleep_retention_entries_t *entries); +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +/** + * @brief Software trigger REGDMA to do extra linked list retention + * + * @param backup_or_restore true for backup register context to memory + * or false for restore to register from memory + */ +void sleep_retention_do_extra_retention(bool backup_or_restore); +#endif + /** * @brief Get all registered modules that require sleep retention * diff --git a/components/esp_hw_support/sleep_retention.c b/components/esp_hw_support/sleep_retention.c index de6110dae7..76d4327b0f 100644 --- a/components/esp_hw_support/sleep_retention.c +++ b/components/esp_hw_support/sleep_retention.c @@ -41,15 +41,21 @@ typedef struct { * The PMU module triggers REGDMA to use the corresponding linked list when * swtiching between different power states. For example: * - * Current power state Next power state This entry will be used by REGDMA - * PMU_HP_ACTIVE PMU_HP_SLEEP entry0 - * PMU_HP_SLEEP PMU_HP_ACTIVE entry0 - * PMU_HP_MODEM PMU_HP_SLEEP entry1 - * PMU_HP_SLEEP PMU_HP_MODEM entry1 - * PMU_HP_MODEM PMU_HP_ACTIVE entry2 + * +---------------+---------------+-------------------+-----------+ + * | Current | The next | The entry will be | Retention | + * | PMU state | PMU state | used by REGDMA | clock | + * +---------------+---------------+-------------------+-----------+ + * | PMU_HP_ACTIVE | PMU_HP_SLEEP | entry0 | XTAL | + * | PMU_HP_SLEEP | PMU_HP_ACTIVE | entry0 | XTAL | + * | PMU_HP_MODEM | PMU_HP_SLEEP | ------ | XTAL | + * | PMU_HP_SLEEP | PMU_HP_MODEM | entry1 | XTAL | + * | PMU_HP_MODEM | PMU_HP_ACTIVE | entry2 | PLL | + * |---------------------------------------------------------------| + * | PMU_HP_ACTIVE | PMU_HP_ACTIVE | entry3 | PLL | (Clock BUG) + * +---------------+---------------+-------------------+-----------+ * * +--------+ +-------------------------+ +-------------+ +-----------+ +--------+ +-----+ - * entry2 -> | | -> | WiFi MAC Minimum System | -> | | -------------------------> | | -> | | -> | End | + * entry2 -> | | -> | WiFi MAC Minimum System | -> | | -------------------------> | ######### | -> | ###### | -> | End | * | SOC | +-------------------------+ | Digital | | Bluetooth | | Zigbee | +-----+ * | System | +--------+ | Peripherals | +------+ +------+ | / BLE | | | +-----+ * entry0 -> | | ----------> | | ---------> | | -> | | -> | | -> | | -> | | -> | End | @@ -57,6 +63,22 @@ typedef struct { * | System | | MAC | | BB | +-----+ * entry1 ------------------------> | |-----------------------------> | | -> | | -> | End | * +--------+ +------+ +------+ +-----+ + * + * The entry3 (alias: extra linked list) is used for backup and restore of + * modules (such as BLE or 15.4 modules) with retention clock bugs. + * + * +---------+ +----------+ +-------------+ +-----+ + * entry3 -> | BLE MAC | -> | 15.4 MAC | -> | BLE/15.4 BB | -> | End | + * +---------+ +----------+ +-------------+ +-----+ + * + * Using it (extra linked list) for retention has the following constraints: + * 1. The PLL clock must be enabled (can be done with esp_pm_lock_acquire() + * interface to acquire a pm lock of type ESP_PM_APB_FREQ_MAX. + * 2. When using the sleep_retention_entries_create() interface to create an + * extra linked list, the node owner must be equal to BIT(3). + * 3. Use the sleep_retention_do_extra_retention() interface to backup or + * restore the register context, which ensures only one backup or restore + * when multiple modules (BLE and 15.4) exists. */ #define SLEEP_RETENTION_REGDMA_LINK_NR_PRIORITIES (8u) #define SLEEP_RETENTION_REGDMA_LINK_HIGHEST_PRIORITY (0) @@ -71,9 +93,18 @@ typedef struct { _lock_t lock; regdma_link_priority_t highpri; uint32_t modules; +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +#define EXTRA_LINK_NUM (REGDMA_LINK_ENTRY_NUM - 1) + int extra_refs; +#endif } sleep_retention_t; -static DRAM_ATTR __attribute__((unused)) sleep_retention_t s_retention = { .highpri = (uint8_t)-1, .modules = 0 }; +static DRAM_ATTR __attribute__((unused)) sleep_retention_t s_retention = { + .highpri = (uint8_t)-1, .modules = 0 +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + , .extra_refs = 0 +#endif +}; #define SLEEP_RETENTION_ENTRY_BITMAP_MASK (BIT(REGDMA_LINK_ENTRY_NUM) - 1) #define SLEEP_RETENTION_ENTRY_BITMAP(bitmap) ((bitmap) & SLEEP_RETENTION_ENTRY_BITMAP_MASK) @@ -358,6 +389,13 @@ static esp_err_t sleep_retention_entries_create_impl(const sleep_retention_entri { _lock_acquire_recursive(&s_retention.lock); for (int i = num - 1; i >= 0; i--) { +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + if ((retent[i].owner > BIT(EXTRA_LINK_NUM)) && (retent[i].config.id != 0xffff)) { + _lock_release_recursive(&s_retention.lock); + sleep_retention_entries_do_destroy(module); + return ESP_ERR_NOT_SUPPORTED; + } +#endif void *link = sleep_retention_entries_try_create(&retent[i].config, retent[i].owner, priority, module); if (link == NULL) { _lock_release_recursive(&s_retention.lock); @@ -464,3 +502,31 @@ uint32_t IRAM_ATTR sleep_retention_get_modules(void) { return s_retention.modules; } + +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +void sleep_retention_do_extra_retention(bool backup_or_restore) +{ + _lock_acquire_recursive(&s_retention.lock); + if (s_retention.highpri < SLEEP_RETENTION_REGDMA_LINK_HIGHEST_PRIORITY || + s_retention.highpri > SLEEP_RETENTION_REGDMA_LINK_LOWEST_PRIORITY) { + _lock_release_recursive(&s_retention.lock); + return; + } + const uint32_t clk_bug_modules = SLEEP_RETENTION_MODULE_BLE_MAC | SLEEP_RETENTION_MODULE_802154_MAC; + const int cnt_modules = __builtin_popcount(clk_bug_modules & s_retention.modules); + // Set extra linked list head pointer to hardware + pau_regdma_set_extra_link_addr(s_retention.lists[s_retention.highpri].entries[EXTRA_LINK_NUM]); + if (backup_or_restore) { + if (s_retention.extra_refs++ == (cnt_modules - 1)) { + pau_regdma_trigger_extra_link_backup(); + } + } else { + if (--s_retention.extra_refs == (cnt_modules - 1)) { + pau_regdma_trigger_extra_link_restore(); + } + } + int refs = s_retention.extra_refs; + _lock_release_recursive(&s_retention.lock); + assert(refs >= 0 && refs <= cnt_modules); +} +#endif diff --git a/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in b/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in index 4e6fe9f26c..dc4ba796d0 100644 --- a/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in @@ -1147,6 +1147,10 @@ config SOC_PM_MODEM_RETENTION_BY_REGDMA bool default y +config SOC_PM_RETENTION_HAS_CLOCK_BUG + bool + default y + config SOC_PM_PAU_LINK_NUM int default 4 diff --git a/components/soc/esp32c6/include/soc/soc_caps.h b/components/soc/esp32c6/include/soc/soc_caps.h index 27506385fa..d7a162689c 100644 --- a/components/soc/esp32c6/include/soc/soc_caps.h +++ b/components/soc/esp32c6/include/soc/soc_caps.h @@ -475,6 +475,7 @@ #define SOC_PM_CPU_RETENTION_BY_SW (1) #define SOC_PM_MODEM_RETENTION_BY_REGDMA (1) +#define SOC_PM_RETENTION_HAS_CLOCK_BUG (1) #define SOC_PM_PAU_LINK_NUM (4)