Skip to content

Commit 12463ab

Browse files
rugeGerritsenadamkondraciuk
authored andcommitted
[nrf fromlist] soc: nordic: nrf54h: s2ram: Use ARM MPU save/restore funcs
This reduced the amount of duplicate code and unifies the code with other platforms. MPU retention was originally added in ee9d239. Upstream PR #: 97073 Signed-off-by: Rubin Gerritsen <[email protected]>
1 parent 34f1aa3 commit 12463ab

File tree

1 file changed

+10
-68
lines changed

1 file changed

+10
-68
lines changed

soc/nordic/nrf54h/pm_s2ram.c

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <zephyr/arch/cpu.h>
7+
#include <zephyr/arch/arm/mpu/arm_mpu.h>
78
#include <zephyr/arch/common/pm_s2ram.h>
89
#include <zephyr/linker/sections.h>
910
#include <zephyr/sys/util.h>
@@ -22,32 +23,13 @@
2223
#define SCnSCB_CPPWR_SU10_Pos 20U /*!< CPPWR: SU10 Position */
2324
#define SCnSCB_CPPWR_SU10_Msk (1UL << SCnSCB_CPPWR_SU10_Pos) /*!< CPPWR: SU10 Mask */
2425

25-
/* Currently dynamic regions are only used in case of userspace or stack guard and
26-
* stack guard is not used by default on Cortex-M33 because there is a dedicated
27-
* mechanism for stack overflow detection. Unless those condition change we don't
28-
* need to store MPU content, it can just be reinitialized on resuming.
29-
*/
30-
#define MPU_USE_DYNAMIC_REGIONS IS_ENABLED(CONFIG_USERSPACE) || IS_ENABLED(CONFIG_MPU_STACK_GUARD)
31-
32-
/* TODO: The num-mpu-regions property should be used. Needs to be added to dts bindings. */
33-
#define MPU_MAX_NUM_REGIONS 16
34-
3526
typedef struct {
3627
/* NVIC components stored into RAM. */
3728
uint32_t ISER[NVIC_MEMBER_SIZE(ISER)];
3829
uint32_t ISPR[NVIC_MEMBER_SIZE(ISPR)];
3930
uint8_t IPR[NVIC_MEMBER_SIZE(IPR)];
4031
} _nvic_context_t;
4132

42-
typedef struct {
43-
uint32_t RNR;
44-
uint32_t RBAR[MPU_MAX_NUM_REGIONS];
45-
uint32_t RLAR[MPU_MAX_NUM_REGIONS];
46-
uint32_t MAIR0;
47-
uint32_t MAIR1;
48-
uint32_t CTRL;
49-
} _mpu_context_t;
50-
5133
typedef struct {
5234
uint32_t ICSR;
5335
uint32_t VTOR;
@@ -76,7 +58,9 @@ typedef struct {
7658

7759
struct backup {
7860
_nvic_context_t nvic_context;
79-
_mpu_context_t mpu_context;
61+
#if defined(CONFIG_ARM_MPU)
62+
struct z_mpu_context_retained mpu_context;
63+
#endif
8064
_scb_context_t scb_context;
8165
#if defined(CONFIG_FPU) && !defined(CONFIG_FPU_SHARING)
8266
_fpu_context_t fpu_context;
@@ -85,52 +69,6 @@ struct backup {
8569

8670
static __noinit struct backup backup_data;
8771

88-
extern void z_arm_configure_static_mpu_regions(void);
89-
extern int z_arm_mpu_init(void);
90-
91-
/* MPU registers cannot be simply copied because content of RBARx RLARx registers
92-
* depends on region which is selected by RNR register.
93-
*/
94-
static void mpu_save(_mpu_context_t *backup)
95-
{
96-
if (!MPU_USE_DYNAMIC_REGIONS) {
97-
return;
98-
}
99-
100-
backup->RNR = MPU->RNR;
101-
102-
for (uint8_t i = 0; i < MPU_MAX_NUM_REGIONS; i++) {
103-
MPU->RNR = i;
104-
backup->RBAR[i] = MPU->RBAR;
105-
backup->RLAR[i] = MPU->RLAR;
106-
}
107-
backup->MAIR0 = MPU->MAIR0;
108-
backup->MAIR1 = MPU->MAIR1;
109-
backup->CTRL = MPU->CTRL;
110-
}
111-
112-
static void mpu_restore(_mpu_context_t *backup)
113-
{
114-
if (!MPU_USE_DYNAMIC_REGIONS) {
115-
z_arm_mpu_init();
116-
z_arm_configure_static_mpu_regions();
117-
return;
118-
}
119-
120-
uint32_t rnr = backup->RNR;
121-
122-
for (uint8_t i = 0; i < MPU_MAX_NUM_REGIONS; i++) {
123-
MPU->RNR = i;
124-
MPU->RBAR = backup->RBAR[i];
125-
MPU->RLAR = backup->RLAR[i];
126-
}
127-
128-
MPU->MAIR0 = backup->MAIR0;
129-
MPU->MAIR1 = backup->MAIR1;
130-
MPU->RNR = rnr;
131-
MPU->CTRL = backup->CTRL;
132-
}
133-
13472
static void nvic_save(_nvic_context_t *backup)
13573
{
13674
memcpy(backup->ISER, (uint32_t *)NVIC->ISER, sizeof(NVIC->ISER));
@@ -245,7 +183,9 @@ int soc_s2ram_suspend(pm_s2ram_system_off_fn_t system_off)
245183
fpu_power_down();
246184
#endif
247185
nvic_save(&backup_data.nvic_context);
248-
mpu_save(&backup_data.mpu_context);
186+
#if defined(CONFIG_ARM_MPU)
187+
z_arm_save_mpu_context(&backup_data.mpu_context);
188+
#endif
249189
ret = arch_pm_s2ram_suspend(system_off);
250190
/* Cache and FPU are powered down so power up is needed even if s2ram failed. */
251191
nrf_power_up_cache();
@@ -260,7 +200,9 @@ int soc_s2ram_suspend(pm_s2ram_system_off_fn_t system_off)
260200
return ret;
261201
}
262202

263-
mpu_restore(&backup_data.mpu_context);
203+
#if defined(CONFIG_ARM_MPU)
204+
z_arm_restore_mpu_context(&backup_data.mpu_context);
205+
#endif
264206
nvic_restore(&backup_data.nvic_context);
265207
scb_restore(&backup_data.scb_context);
266208

0 commit comments

Comments
 (0)