2 * Copyright (c) 2017-2022 Arm Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * CMSIS-Core(M) MPU API for Armv8-M and Armv8.1-M MPU
23 #ifndef ARM_MPU_ARMV8_H
24 #define ARM_MPU_ARMV8_H
26 #if defined ( __ICCARM__ )
27 #pragma system_include /* treat file as system include file for MISRA check */
28 #elif defined (__clang__)
29 #pragma clang system_header /* treat file as system include file */
32 /** \brief Attribute for device memory (outer only) */
33 #define ARM_MPU_ATTR_DEVICE ( 0U )
35 /** \brief Attribute for non-cacheable, normal memory */
36 #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
38 /** \brief Attribute for Normal memory, Outer and Inner cacheability.
39 * \param NT Non-Transient: Set to 1 for Non-transient data. Set to 0 for Transient data.
40 * \param WB Write-Back: Set to 1 to use a Write-Back policy. Set to 0 to use a Write-Through policy.
41 * \param RA Read Allocation: Set to 1 to enable cache allocation on read miss. Set to 0 to disable cache allocation on read miss.
42 * \param WA Write Allocation: Set to 1 to enable cache allocation on write miss. Set to 0 to disable cache allocation on write miss.
44 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
45 ((((NT) & 1U) << 3U) | (((WB) & 1U) << 2U) | (((RA) & 1U) << 1U) | ((WA) & 1U))
47 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
48 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
50 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
51 #define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
53 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
54 #define ARM_MPU_ATTR_DEVICE_nGRE (2U)
56 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
57 #define ARM_MPU_ATTR_DEVICE_GRE (3U)
59 /** \brief Normal memory outer-cacheable and inner-cacheable attributes
60 * WT = Write Through, WB = Write Back, TR = Transient, RA = Read-Allocate, WA = Write Allocate
62 #define MPU_ATTR_NORMAL_OUTER_NON_CACHEABLE (0b0100)
63 #define MPU_ATTR_NORMAL_OUTER_WT_TR_RA (0b0010)
64 #define MPU_ATTR_NORMAL_OUTER_WT_TR_WA (0b0001)
65 #define MPU_ATTR_NORMAL_OUTER_WT_TR_RA_WA (0b0011)
66 #define MPU_ATTR_NORMAL_OUTER_WT_RA (0b1010)
67 #define MPU_ATTR_NORMAL_OUTER_WT_WA (0b1001)
68 #define MPU_ATTR_NORMAL_OUTER_WT_RA_WA (0b1011)
69 #define MPU_ATTR_NORMAL_OUTER_WB_TR_RA (0b0101)
70 #define MPU_ATTR_NORMAL_OUTER_WB_TR_WA (0b0110)
71 #define MPU_ATTR_NORMAL_OUTER_WB_TR_RA_WA (0b0111)
72 #define MPU_ATTR_NORMAL_OUTER_WB_RA (0b1101)
73 #define MPU_ATTR_NORMAL_OUTER_WB_WA (0b1110)
74 #define MPU_ATTR_NORMAL_OUTER_WB_RA_WA (0b1111)
75 #define MPU_ATTR_NORMAL_INNER_NON_CACHEABLE (0b0100)
76 #define MPU_ATTR_NORMAL_INNER_WT_TR_RA (0b0010)
77 #define MPU_ATTR_NORMAL_INNER_WT_TR_WA (0b0001)
78 #define MPU_ATTR_NORMAL_INNER_WT_TR_RA_WA (0b0011)
79 #define MPU_ATTR_NORMAL_INNER_WT_RA (0b1010)
80 #define MPU_ATTR_NORMAL_INNER_WT_WA (0b1001)
81 #define MPU_ATTR_NORMAL_INNER_WT_RA_WA (0b1011)
82 #define MPU_ATTR_NORMAL_INNER_WB_TR_RA (0b0101)
83 #define MPU_ATTR_NORMAL_INNER_WB_TR_WA (0b0110)
84 #define MPU_ATTR_NORMAL_INNER_WB_TR_RA_WA (0b0111)
85 #define MPU_ATTR_NORMAL_INNER_WB_RA (0b1101)
86 #define MPU_ATTR_NORMAL_INNER_WB_WA (0b1110)
87 #define MPU_ATTR_NORMAL_INNER_WB_RA_WA (0b1111)
89 /** \brief Memory Attribute
90 * \param O Outer memory attributes
91 * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
93 #define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
95 /* \brief Specifies MAIR_ATTR number */
96 #define MAIR_ATTR(x) ((x > 7 || x < 0) ? 0 : x)
101 /** \brief Normal memory, non-shareable */
102 #define ARM_MPU_SH_NON (0U)
104 /** \brief Normal memory, outer shareable */
105 #define ARM_MPU_SH_OUTER (2U)
107 /** \brief Normal memory, inner shareable */
108 #define ARM_MPU_SH_INNER (3U)
112 * AP = Access permission, RO = Read-only, RW = Read/Write, NP = Any privilege, PO = Privileged code only
114 /** \brief Normal memory, read/write */
115 #define ARM_MPU_AP_RW (0U)
117 /** \brief Normal memory, read-only */
118 #define ARM_MPU_AP_RO (1U)
120 /** \brief Normal memory, any privilege level */
121 #define ARM_MPU_AP_NP (1U)
123 /** \brief Normal memory, privileged access only */
124 #define ARM_MPU_AP_PO (0U)
128 * XN = Execute-never, EX = Executable
130 /** \brief Normal memory, Execution only permitted if read permitted */
131 #define ARM_MPU_XN (1U)
133 /** \brief Normal memory, Execution only permitted if read permitted */
134 #define ARM_MPU_EX (0U)
136 /** \brief Memory access permissions
137 * \param RO Read-Only: Set to 1 for read-only memory. Set to 0 for a read/write memory.
138 * \param NP Non-Privileged: Set to 1 for non-privileged memory. Set to 0 for privileged memory.
140 #define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
142 /** \brief Region Base Address Register value
143 * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
144 * \param SH Defines the Shareability domain for this memory region.
145 * \param RO Read-Only: Set to 1 for a read-only memory region. Set to 0 for a read/write memory region.
146 * \param NP Non-Privileged: Set to 1 for a non-privileged memory region. Set to 0 for privileged memory region.
147 * \param XN eXecute Never: Set to 1 for a non-executable memory region. Set to 0 for an executable memory region.
149 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
150 (((BASE) & MPU_RBAR_BASE_Msk) | \
151 (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
152 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
153 (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
155 /** \brief Region Limit Address Register value
156 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
157 * \param IDX The attribute index to be associated with this memory region.
159 #define ARM_MPU_RLAR(LIMIT, IDX) \
160 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
161 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
164 #if defined(MPU_RLAR_PXN_Pos)
166 /** \brief Region Limit Address Register with PXN value
167 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
168 * \param PXN Privileged execute never. Defines whether code can be executed from this privileged region.
169 * \param IDX The attribute index to be associated with this memory region.
171 #define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
172 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
173 (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
174 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
180 * Struct for a single MPU Region
183 uint32_t RBAR; /*!< Region Base Address Register value */
184 uint32_t RLAR; /*!< Region Limit Address Register value */
188 \brief Read MPU Type Register
189 \return Number of MPU regions
191 __STATIC_INLINE uint32_t ARM_MPU_TYPE()
193 return ((MPU->TYPE) >> 8);
197 * \param MPU_Control Default access permissions for unconfigured regions.
199 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
202 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
203 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
204 SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
212 __STATIC_INLINE void ARM_MPU_Disable(void)
215 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
216 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
218 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
224 /** Enable the Non-secure MPU.
225 * \param MPU_Control Default access permissions for unconfigured regions.
227 __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
230 MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
231 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
232 SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
238 /** Disable the Non-secure MPU.
240 __STATIC_INLINE void ARM_MPU_Disable_NS(void)
243 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
244 SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
246 MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
252 /** Set the memory attribute encoding to the given MPU.
253 * \param mpu Pointer to the MPU to be configured.
254 * \param idx The attribute index to be set [0-7]
255 * \param attr The attribute value to be set.
257 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
259 const uint8_t reg = idx / 4U;
260 const uint32_t pos = ((idx % 4U) * 8U);
261 const uint32_t mask = 0xFFU << pos;
263 if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
264 return; // invalid index
267 mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));
270 /** Set the memory attribute encoding.
271 * \param idx The attribute index to be set [0-7]
272 * \param attr The attribute value to be set.
274 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
276 ARM_MPU_SetMemAttrEx(MPU, idx, attr);
280 /** Set the memory attribute encoding to the Non-secure MPU.
281 * \param idx The attribute index to be set [0-7]
282 * \param attr The attribute value to be set.
284 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
286 ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
290 /** Clear and disable the given MPU region of the given MPU.
291 * \param mpu Pointer to MPU to be used.
292 * \param rnr Region number to be cleared.
294 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
300 /** Clear and disable the given MPU region.
301 * \param rnr Region number to be cleared.
303 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
305 ARM_MPU_ClrRegionEx(MPU, rnr);
309 /** Clear and disable the given Non-secure MPU region.
310 * \param rnr Region number to be cleared.
312 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
314 ARM_MPU_ClrRegionEx(MPU_NS, rnr);
318 /** Configure the given MPU region of the given MPU.
319 * \param mpu Pointer to MPU to be used.
320 * \param rnr Region number to be configured.
321 * \param rbar Value for RBAR register.
322 * \param rlar Value for RLAR register.
324 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
331 /** Configure the given MPU region.
332 * \param rnr Region number to be configured.
333 * \param rbar Value for RBAR register.
334 * \param rlar Value for RLAR register.
336 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
338 ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
342 /** Configure the given Non-secure MPU region.
343 * \param rnr Region number to be configured.
344 * \param rbar Value for RBAR register.
345 * \param rlar Value for RLAR register.
347 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
349 ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
353 /** Memcpy with strictly ordered memory access, e.g. used by code in ARM_MPU_LoadEx()
354 * \param dst Destination data is copied to.
355 * \param src Source data is copied from.
356 * \param len Amount of data words to be copied.
358 __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
361 for (i = 0U; i < len; ++i)
367 /** Load the given number of MPU regions from a table to the given MPU.
368 * \param mpu Pointer to the MPU registers to be used.
369 * \param rnr First region number to be configured.
370 * \param table Pointer to the MPU configuration table.
371 * \param cnt Amount of regions to be configured.
373 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
375 const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
378 ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
380 uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
381 uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
384 while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
385 uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
386 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
390 rnrBase += MPU_TYPE_RALIASES;
394 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
398 /** Load the given number of MPU regions from a table.
399 * \param rnr First region number to be configured.
400 * \param table Pointer to the MPU configuration table.
401 * \param cnt Amount of regions to be configured.
403 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
405 ARM_MPU_LoadEx(MPU, rnr, table, cnt);
409 /** Load the given number of MPU regions from a table to the Non-secure MPU.
410 * \param rnr First region number to be configured.
411 * \param table Pointer to the MPU configuration table.
412 * \param cnt Amount of regions to be configured.
414 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
416 ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);