Skip to content

Commit

Permalink
[nrf fromlist] arch: arm: cortex_m: pm_s2ram: Rework S2RAM mark funct…
Browse files Browse the repository at this point in the history
…ions

The S2RAM procedure requires marker checking after reset.
Such checking is performed on very early stage of the system initialization
and must ensure that the stack is not used due to the TLS pointer which is
not initialized yet.

Upstream PR: zephyrproject-rtos/zephyr#80039

Signed-off-by: Adam Kondraciuk <[email protected]>
  • Loading branch information
adamkondraciuk committed Oct 18, 2024
1 parent 3bedb2d commit d90712a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
86 changes: 74 additions & 12 deletions arch/arm/core/cortex_m/pm_s2ram.S
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@ GTEXT(pm_s2ram_mark_set)
GTEXT(pm_s2ram_mark_check_and_clear)
GDATA(_cpu_context)

#if !(CONFIG_PM_S2RAM_CUSTOM_MARKING)
#define MAGIC (0xDABBAD00)
GDATA(marker)

SECTION_FUNC(TEXT, pm_s2ram_mark_set)
/*
* Set the marker to MAGIC value
*/
ldr r0, =marker
ldr r2, =MAGIC
str r2, [r0]

/*
* Restore the PC and continue
*/
mov r15, r1

SECTION_FUNC(TEXT, pm_s2ram_mark_check_and_clear)
/* Set return value to 0 */
mov r0, #0

/*
* Check the marker
*/
ldr r2, =marker
ldr r5, [r2]
ldr r3, =MAGIC
cmp r5, r3
bne exit

/*
* Reset the marker
*/
str r0, [r2]

/*
* Set return value to 1
*/
mov r0, #1

exit:
/*
* Restore the PC and continue
*/
mov r15, r1
#endif

SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
/*
* Save the CPU context
Expand Down Expand Up @@ -67,10 +114,16 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
mrs r2, control
str r2, [r1, #___cpu_context_t_control_OFFSET]

/*
* Save the return address from `pm_s2ram_mark_set`
*/
mov r1, r15
add r1, #6

/*
* Mark entering suspend to RAM.
*/
bl pm_s2ram_mark_set
b pm_s2ram_mark_set

/*
* Call the system_off function passed as parameter. This should never
Expand All @@ -83,10 +136,16 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
* not successful (in r0 the return value).
*/

/*
* Save the return address from `pm_s2ram_mark_check_and_clear`
*/
mov r1, r15
add r1, #6

/*
* Reset the marking of suspend to RAM, return is ignored.
*/
bl pm_s2ram_mark_check_and_clear
b pm_s2ram_mark_check_and_clear

/* Move system_off back to r0 as return value */
mov r0, r4
Expand All @@ -97,18 +156,21 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)

GTEXT(arch_pm_s2ram_resume)
SECTION_FUNC(TEXT, arch_pm_s2ram_resume)


/*
* Save the return address from `pm_s2ram_mark_check_and_clear`
*/
mov r1, r15
add r1, #6

/*
* Check if reset occurred after suspending to RAM.
* Store LR to ensure we can continue boot when we are not suspended
* to RAM. In addition to LR, R0 is pushed too, to ensure "SP mod 8 = 0",
* as stated by ARM rule 6.2.1.2 for AAPCS32.
*/
push {r0, lr}
bl pm_s2ram_mark_check_and_clear
cmp r0, #0x1
pop {r0, lr}
beq resume
bx lr
*/
b pm_s2ram_mark_check_and_clear
cmp r0, #0x1
beq resume
bx lr

resume:
/*
Expand Down
20 changes: 1 addition & 19 deletions arch/arm/core/cortex_m/pm_s2ram.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <zephyr/arch/common/pm_s2ram.h>

#define MAGIC (0xDABBAD00)

/**
* CPU context for S2RAM
*/
Expand All @@ -20,22 +18,6 @@ __noinit _cpu_context_t _cpu_context;
/**
* S2RAM Marker
*/
static __noinit uint32_t marker;

void pm_s2ram_mark_set(void)
{
marker = MAGIC;
}

bool pm_s2ram_mark_check_and_clear(void)
{
if (marker == MAGIC) {
marker = 0;

return true;
}

return false;
}
__noinit uint32_t marker;

#endif /* CONFIG_PM_S2RAM_CUSTOM_MARKING */
10 changes: 10 additions & 0 deletions include/zephyr/arch/common/pm_s2ram.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ int arch_pm_s2ram_suspend(pm_s2ram_system_off_fn_t system_off);
*
* Default implementation is setting a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
* allows custom implementation.
* The following requirements must be fulfilled:
* - the function cannot use stack (most likely asm function)
* - the content of the R1, R4 and LR registers must remain unchanged
* - the last instruction restores the PC stored in R1 register
*
*/
void pm_s2ram_mark_set(void);

Expand All @@ -76,6 +81,11 @@ void pm_s2ram_mark_set(void);
*
* Default implementation is checking a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
* allows custom implementation.
* The following requirements must be fulfilled:
* - the function cannot use stack (most likely asm function)
* - the content of the R1, R4 and LR registers must remain unchanged
* - the function's return value is passed by R0
* - the last instruction restores the PC stored in R1 register
*
* @retval true if marking is found which indicates resuming after suspend-to-RAM.
* @retval false if marking is not found which indicates standard boot.
Expand Down

0 comments on commit d90712a

Please sign in to comment.