Skip to content

Commit 608a5ec

Browse files
msmttchrmathieuchopstm
authored andcommitted
arch: arm: cortex_m: Add API for SCB save and restore
Add new API to save and restore SCB context. This is typically useful when entering and exiting suspend-to-RAM low-power modes. The scb_context_t and the backup/restore functions are designed to only handle SCB registers that are: - Mutable: Their values can be changed by software. - Configurable: They control system behavior or features. - Stateful: Their values represent a specific configuration that an application might want to preserve and restore. Registers excluded from backup/restore are: 1. CPU/feature identification registers Motivation: These registers are fixed in hardware and read-only. 2. ICSR (Interrupt Control and State Register) Motivation: Most bits of ICSR bits are read-only or write-only and represent volatile system state. STTNS is the only read-write field and could be considered part of the system state, but it is only present on certain ARMv8-M CPUs, and Zephyr does not use it. 3. CFSR (Configurable Fault Status Register) HFSR (HardFault Status Register) DFSR (Debug Fault Status Register) AFSR (Auxiliary Fault Status Register) MMFAR (MemManage Fault Address Register) BFAR (BusFault Address Register) Motivation: These registers are read/write-one-to-clear and contain only fault-related information (which is volatile). Signed-off-by: Mathieu Choplain <[email protected]> Co-authored-by: Michele Sardo <[email protected]>
1 parent 20efb9e commit 608a5ec

File tree

2 files changed

+186
-0
lines changed
  • arch/arm/core/cortex_m
  • include/zephyr/arch/arm/cortex_m

2 files changed

+186
-0
lines changed

arch/arm/core/cortex_m/scb.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2013-2014 Wind River Systems, Inc.
3+
* Copyright (c) 2025 STMicroelectronics
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -22,6 +23,7 @@
2223
#include <zephyr/linker/linker-defs.h>
2324
#include <zephyr/cache.h>
2425
#include <zephyr/arch/cache.h>
26+
#include <zephyr/arch/arm/cortex_m/scb.h>
2527

2628
#if defined(CONFIG_CPU_HAS_NXP_SYSMPU)
2729
#include <fsl_sysmpu.h>
@@ -151,3 +153,99 @@ void z_arm_init_arch_hw_at_boot(void)
151153
barrier_isync_fence_full();
152154
}
153155
#endif /* CONFIG_INIT_ARCH_HW_AT_BOOT */
156+
157+
/**
158+
* @brief Save essential SCB registers into a provided context structure.
159+
*
160+
* This function reads the current values of critical System Control Block (SCB)
161+
* registers that are safe to backup, and stores them into the `context` structure.
162+
* Access to SCB registers requires atomicity and consistency, so calling code
163+
* should guarantee that interrupts are disabled.
164+
*
165+
* @param context Pointer to an `scb_context` structure where the register
166+
* values will be stored. Must not be NULL.
167+
*/
168+
void z_arm_save_scb_context(struct scb_context *context)
169+
{
170+
__ASSERT_NO_MSG(context != NULL);
171+
172+
#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
173+
context->vtor = SCB->VTOR;
174+
#endif
175+
context->aircr = SCB->AIRCR;
176+
context->scr = SCB->SCR;
177+
context->ccr = SCB->CCR;
178+
179+
/*
180+
* Backup the System Handler Priority Registers.
181+
* SCB->SHPR is defined as u8[] or u32[] depending
182+
* on the target Cortex-M core, but it can always
183+
* be accessed using word-sized reads and writes.
184+
* Make u32 pointer using explicit cast to allow
185+
* access on all cores without compiler warnings.
186+
*/
187+
volatile uint32_t *shpr = (volatile uint32_t *)SCB->SHPR;
188+
189+
for (int i = 0; i < SHPR_SIZE_W; i++) {
190+
context->shpr[i] = shpr[i];
191+
}
192+
193+
context->shcsr = SCB->SHCSR;
194+
#if defined(CPACR_PRESENT)
195+
context->cpacr = SCB->CPACR;
196+
#endif /* CPACR_PRESENT */
197+
}
198+
199+
/**
200+
* @brief Restores essential SCB registers from a provided context structure.
201+
*
202+
* This function writes the values from the `context` structure back to the
203+
* respective System Control Block (SCB) registers. Access to SCB registers
204+
* requires atomicity and consistency, so calling code should guarantee that
205+
* interrupts are disabled.
206+
*
207+
* @param context Pointer to a `scb_context` structure containing the
208+
* register values to be restored. Must not be NULL.
209+
*/
210+
void z_arm_restore_scb_context(const struct scb_context *context)
211+
{
212+
__ASSERT_NO_MSG(context != NULL);
213+
214+
#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
215+
/* Restore VTOR if present on this CPU */
216+
SCB->VTOR = context->vtor;
217+
#endif
218+
/* Restoring AIRCR requires writing VECTKEY along with desired bits.
219+
* Mask backed up data to ensure only modifiable bits are restored.
220+
*/
221+
SCB->AIRCR = (context->aircr & ~SCB_AIRCR_VECTKEY_Msk) |
222+
(AIRCR_VECT_KEY_PERMIT_WRITE << SCB_AIRCR_VECTKEY_Pos);
223+
224+
SCB->SCR = context->scr;
225+
SCB->CCR = context->ccr;
226+
227+
/* Restore System Handler Priority Registers */
228+
volatile uint32_t *shpr = (volatile uint32_t *)SCB->SHPR;
229+
230+
for (int i = 0; i < SHPR_SIZE_W; i++) {
231+
shpr[i] = context->shpr[i];
232+
}
233+
234+
/* Restore SHCSR */
235+
SCB->SHCSR = context->shcsr;
236+
237+
#if defined(CPACR_PRESENT)
238+
/* Restore CPACR */
239+
SCB->CPACR = context->cpacr;
240+
#endif /* CPACR_PRESENT */
241+
242+
/**
243+
* Ensure that updates to the SCB are visible by executing a DSB followed by ISB.
244+
* This sequence is recommended in the M-profile Architecture Reference Manuals:
245+
* - ARMv6: DDI0419 Issue E - §B2.5 "Barrier support for system correctness"
246+
* - ARMv7: DDI0403 Issue E.e - §A3.7.3 "Memory barriers" (at end of section)
247+
* - ARMv8: DDI0553 Version B.Y - §B7.2.16 "Synchronization requirements [...]"
248+
*/
249+
__DSB();
250+
__ISB();
251+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @file
9+
* @brief System control block context helpers for Cortex-M CPUs
10+
*
11+
* System control block context helpers for backup and restore
12+
*/
13+
14+
#ifndef ARM_CORTEX_M_SCB_H_
15+
#define ARM_CORTEX_M_SCB_H_
16+
17+
#include <stdint.h>
18+
#include <cmsis_core.h>
19+
20+
/* Define macros for CPU-conditional features */
21+
#if defined(CONFIG_CPU_CORTEX_M0) || \
22+
defined(CONFIG_CPU_CORTEX_M0PLUS) || \
23+
defined(CONFIG_CPU_CORTEX_M1) || \
24+
defined(CONFIG_CPU_CORTEX_M23)
25+
#define SHPR_SIZE_W 2
26+
#else
27+
#define SHPR_SIZE_W 3
28+
#define CPACR_PRESENT 1
29+
#endif
30+
31+
/**
32+
* @brief Structure to store essential, mutable SCB register values for backup/restore.
33+
*
34+
* This structure only contains SCB registers that are safe and meaningful to backup
35+
* and restore. In particular, registers that are read-only (such as CPUID) or contain
36+
* volatile information (ICSR / CFSR) are ignored, since their value is tied to the
37+
* system state or fixed in hardware, rather than related to a configuration option.
38+
*/
39+
struct scb_context {
40+
#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
41+
uint32_t vtor; /*!< Vector Table Offset Register */
42+
#endif
43+
uint32_t aircr; /*!< Application Interrupt and Reset Control Register */
44+
uint32_t scr; /*!< System Control Register */
45+
uint32_t ccr; /*!< Configuration Control Register */
46+
uint32_t shpr[SHPR_SIZE_W]; /*!< System Handler Priority Registers */
47+
uint32_t shcsr; /*!< System Handler Control and State Register */
48+
#if defined(CPACR_PRESENT)
49+
uint32_t cpacr; /*!< Coprocessor Access Control Register */
50+
#endif /* CPACR_PRESENT */
51+
};
52+
53+
/**
54+
* @name SCB Register Backup/Restore Functions
55+
* @brief Functions for saving and restoring mutable SCB register state.
56+
* @{
57+
*/
58+
59+
/**
60+
* @brief Save essential SCB registers into a provided context structure.
61+
*
62+
* This function reads the current values of critical System Control Block (SCB)
63+
* registers that are safe to backup and stores them into the `context` structure.
64+
*
65+
* @param context Pointer to an `scb_context` structure where the register
66+
* values will be stored. Must not be NULL.
67+
*/
68+
void z_arm_save_scb_context(struct scb_context *context);
69+
70+
/**
71+
* @brief Restores essential SCB registers from a provided context structure.
72+
*
73+
* This function writes the values from the `context` structure back to the
74+
* respective System Control Block (SCB) registers.
75+
*
76+
* @warning Extreme caution is advised when restoring SCB registers. Only
77+
* mutable registers are restored. Specifically, the ICSR register
78+
* is NOT restored directly due to its volatile nature and read-only/
79+
* write-only bits.
80+
*
81+
* @param context Pointer to a `scb_context` structure containing the
82+
* register values to be restored. Must not be NULL.
83+
*/
84+
void z_arm_restore_scb_context(const struct scb_context *context);
85+
86+
/** @} */
87+
88+
#endif /* ARM_CORTEX_M_SCB_H_ */

0 commit comments

Comments
 (0)