2020#include "freertos/task.h"
2121#include "esp_heap_caps.h"
2222#include "riscv/csr.h"
23- #include "soc/cache_reg.h"
2423#include "soc/clic_reg.h"
2524#include "soc/rtc_periph.h"
2625#include "soc/soc_caps.h"
3231
3332#include "esp32p4/rom/ets_sys.h"
3433#include "esp32p4/rom/rtc.h"
35- #include "esp32p4/rom/cache.h"
3634#include "rvsleep-frames.h"
3735
3836#if CONFIG_PM_CHECK_SLEEP_RETENTION_FRAME
@@ -76,7 +74,6 @@ typedef struct {
7674 struct {
7775 RvCoreCriticalSleepFrame * critical_frame [portNUM_PROCESSORS ];
7876 RvCoreNonCriticalSleepFrame * non_critical_frame [portNUM_PROCESSORS ];
79- cpu_domain_dev_sleep_frame_t * cache_config_frame ;
8077 cpu_domain_dev_sleep_frame_t * clic_frame [portNUM_PROCESSORS ];
8178 } retent ;
8279} sleep_cpu_retention_t ;
@@ -107,15 +104,6 @@ static void * cpu_domain_dev_sleep_frame_alloc_and_init(const cpu_domain_dev_reg
107104 return frame ;
108105}
109106
110- static inline void * cpu_domain_cache_config_sleep_frame_alloc_and_init (void )
111- {
112- const static cpu_domain_dev_regs_region_t regions [] = {
113- { .start = CACHE_L1_ICACHE_CTRL_REG , .end = CACHE_L1_BYPASS_CACHE_CONF_REG + 4 },
114- { .start = CACHE_L2_CACHE_CTRL_REG , .end = CACHE_L2_CACHE_BLOCKSIZE_CONF_REG + 4 }
115- };
116- return cpu_domain_dev_sleep_frame_alloc_and_init (regions , sizeof (regions ) / sizeof (regions [0 ]));
117- }
118-
119107static inline void * cpu_domain_clic_sleep_frame_alloc_and_init (uint8_t core_id )
120108{
121109 const static cpu_domain_dev_regs_region_t regions [portNUM_PROCESSORS ][2 ] = {
@@ -146,13 +134,6 @@ static esp_err_t esp_sleep_cpu_retention_init_impl(void)
146134 s_cpu_retention .retent .non_critical_frame [core_id ] = (RvCoreNonCriticalSleepFrame * )frame ;
147135 }
148136 }
149- if (s_cpu_retention .retent .cache_config_frame == NULL ) {
150- void * frame = cpu_domain_cache_config_sleep_frame_alloc_and_init ();
151- if (frame == NULL ) {
152- goto err ;
153- }
154- s_cpu_retention .retent .cache_config_frame = (cpu_domain_dev_sleep_frame_t * )frame ;
155- }
156137 for (uint8_t core_id = 0 ; core_id < portNUM_PROCESSORS ; ++ core_id ) {
157138 if (s_cpu_retention .retent .clic_frame [core_id ] == NULL ) {
158139 void * frame = cpu_domain_clic_sleep_frame_alloc_and_init (core_id );
@@ -186,10 +167,6 @@ static esp_err_t esp_sleep_cpu_retention_deinit_impl(void)
186167 s_cpu_retention .retent .non_critical_frame [core_id ] = NULL ;
187168 }
188169 }
189- if (s_cpu_retention .retent .cache_config_frame ) {
190- heap_caps_free ((void * )s_cpu_retention .retent .cache_config_frame );
191- s_cpu_retention .retent .cache_config_frame = NULL ;
192- }
193170 for (uint8_t core_id = 0 ; core_id < portNUM_PROCESSORS ; ++ core_id ) {
194171 if (s_cpu_retention .retent .clic_frame [core_id ]) {
195172 heap_caps_free ((void * )s_cpu_retention .retent .clic_frame [core_id ]);
@@ -215,6 +192,7 @@ static TCM_IRAM_ATTR RvCoreNonCriticalSleepFrame * rv_core_noncritical_regs_save
215192
216193 frame -> mscratch = RV_READ_CSR (mscratch );
217194 frame -> misa = RV_READ_CSR (misa );
195+ frame -> mhcr = RV_READ_CSR (MHCR );
218196 frame -> tselect = RV_READ_CSR (tselect );
219197 frame -> tdata1 = RV_READ_CSR (tdata1 );
220198 frame -> tdata2 = RV_READ_CSR (tdata2 );
@@ -283,6 +261,7 @@ static TCM_IRAM_ATTR void rv_core_noncritical_regs_restore(void)
283261
284262 RV_WRITE_CSR (mscratch , frame -> mscratch );
285263 RV_WRITE_CSR (misa , frame -> misa );
264+ RV_WRITE_CSR (MHCR , frame -> mhcr );
286265 RV_WRITE_CSR (tselect , frame -> tselect );
287266 RV_WRITE_CSR (tdata1 , frame -> tdata1 );
288267 RV_WRITE_CSR (tdata2 , frame -> tdata2 );
@@ -403,6 +382,8 @@ static TCM_IRAM_ATTR esp_err_t do_cpu_retention(sleep_cpu_entry_cb_t goto_sleep,
403382 uint32_t wakeup_opt , uint32_t reject_opt , uint32_t lslp_mem_inf_fpu , bool dslp )
404383{
405384 uint8_t core_id = esp_cpu_get_core_id ();
385+ /* mstatus is core privated CSR, do it near the core critical regs restore */
386+ uint32_t mstatus = save_mstatus_and_disable_global_int ();
406387 rv_core_critical_regs_save ();
407388
408389 RvCoreCriticalSleepFrame * frame = s_cpu_retention .retent .critical_frame [core_id ];
@@ -428,21 +409,19 @@ static TCM_IRAM_ATTR esp_err_t do_cpu_retention(sleep_cpu_entry_cb_t goto_sleep,
428409 validate_retention_frame_crc ((uint32_t * )frame , RV_SLEEP_CTX_SZ1 - 2 * sizeof (long ), (uint32_t * )(& frame -> frame_crc ));
429410 }
430411#endif
431-
412+ restore_mstatus ( mstatus );
432413 return pmu_sleep_finish (dslp );
433414}
434415
435416esp_err_t TCM_IRAM_ATTR esp_sleep_cpu_retention (uint32_t (* goto_sleep )(uint32_t , uint32_t , uint32_t , bool ),
436417 uint32_t wakeup_opt , uint32_t reject_opt , uint32_t lslp_mem_inf_fpu , bool dslp )
437418{
438419 esp_sleep_execute_event_callbacks (SLEEP_EVENT_SW_CPU_TO_MEM_START , (void * )0 );
439- uint32_t mstatus = save_mstatus_and_disable_global_int ();
440420 uint8_t core_id = esp_cpu_get_core_id ();
441421#if ESP_SLEEP_POWER_DOWN_CPU && !CONFIG_FREERTOS_UNICORE
442422 atomic_store (& s_smp_retention_state [core_id ], SMP_BACKUP_START );
443423#endif
444424 cpu_domain_dev_regs_save (s_cpu_retention .retent .clic_frame [core_id ]);
445- cpu_domain_dev_regs_save (s_cpu_retention .retent .cache_config_frame );
446425 rv_core_noncritical_regs_save ();
447426
448427#if CONFIG_PM_CHECK_SLEEP_RETENTION_FRAME
@@ -467,10 +446,8 @@ esp_err_t TCM_IRAM_ATTR esp_sleep_cpu_retention(uint32_t (*goto_sleep)(uint32_t,
467446 atomic_store (& s_smp_retention_state [core_id ], SMP_RESTORE_START );
468447#endif
469448
470- rv_core_noncritical_regs_restore ();
471- cpu_domain_dev_regs_restore (s_cpu_retention .retent .cache_config_frame );
472449 cpu_domain_dev_regs_restore (s_cpu_retention .retent .clic_frame [core_id ]);
473- restore_mstatus ( mstatus );
450+ rv_core_noncritical_regs_restore ( );
474451
475452#if ESP_SLEEP_POWER_DOWN_CPU && !CONFIG_FREERTOS_UNICORE
476453 atomic_store (& s_smp_retention_state [core_id ], SMP_RESTORE_DONE );
@@ -495,7 +472,6 @@ bool cpu_domain_pd_allowed(void)
495472 allowed &= (s_cpu_retention .retent .critical_frame [core_id ] != NULL );
496473 allowed &= (s_cpu_retention .retent .non_critical_frame [core_id ] != NULL );
497474 }
498- allowed &= (s_cpu_retention .retent .cache_config_frame != NULL );
499475 for (uint8_t core_id = 0 ; core_id < portNUM_PROCESSORS ; ++ core_id ) {
500476 allowed &= (s_cpu_retention .retent .clic_frame [core_id ] != NULL );
501477 }
@@ -544,6 +520,7 @@ static TCM_IRAM_ATTR void smp_core_do_retention(void)
544520 atomic_store (& s_smp_retention_state [core_id ], SMP_BACKUP_START );
545521 rv_core_noncritical_regs_save ();
546522 cpu_domain_dev_regs_save (s_cpu_retention .retent .clic_frame [core_id ]);
523+ uint32_t mstatus = save_mstatus_and_disable_global_int ();
547524 rv_core_critical_regs_save ();
548525 RvCoreCriticalSleepFrame * frame_critical = s_cpu_retention .retent .critical_frame [core_id ];
549526 if ((frame_critical -> pmufunc & 0x3 ) == 0x1 ) {
@@ -563,6 +540,7 @@ static TCM_IRAM_ATTR void smp_core_do_retention(void)
563540 REG_CLR_BIT (HP_SYS_CLKRST_HP_RST_EN0_REG , HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL );
564541 }
565542 atomic_store (& s_smp_retention_state [core_id ], SMP_RESTORE_START );
543+ restore_mstatus (mstatus );
566544 cpu_domain_dev_regs_restore (s_cpu_retention .retent .clic_frame [core_id ]);
567545 rv_core_noncritical_regs_restore ();
568546 atomic_store (& s_smp_retention_state [core_id ], SMP_RESTORE_DONE );
0 commit comments