1 /******************************************************************************
3 * @brief CMSIS MPU API for ARMv8 MPU
5 * @date 09. August 2017
6 ******************************************************************************/
8 * Copyright (c) 2017 ARM Limited. All rights reserved.
10 * SPDX-License-Identifier: Apache-2.0
12 * Licensed under the Apache License, Version 2.0 (the License); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
25 #ifndef ARM_MPU_ARMV8_H
26 #define ARM_MPU_ARMV8_H
28 /** \brief Attribute for device memory (outer only) */
29 #define ARM_MPU_ATTR_DEVICE ( 0U )
31 /** \brief Attribute for non-cacheable, normal memory */
32 #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
34 /** \brief Attribute for normal memory (outer and inner)
35 * \param NT Non-Transient: Set to 1 for non-transient data.
36 * \param WB Write-Back: Set to 1 to use write-back update policy.
37 * \param RA Read Allocation: Set to 1 to use cache allocation on read miss.
38 * \param WA Write Allocation: Set to 1 to use cache allocation on write miss.
40 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
41 (((NT & 1U) << 3U) | ((WB & 1U) << 2U) | ((RA & 1U) << 1U) | (WA & 1U))
43 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
44 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
46 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
47 #define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
49 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
50 #define ARM_MPU_ATTR_DEVICE_nGRE (2U)
52 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
53 #define ARM_MPU_ATTR_DEVICE_GRE (3U)
55 /** \brief Memory Attribute
56 * \param O Outer memory attributes
57 * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
59 #define ARM_MPU_ATTR(O, I) (((O & 0xFU) << 4U) | (((O & 0xFU) != 0U) ? (I & 0xFU) : ((I & 0x3U) << 2U)))
61 /** \brief Normal memory non-shareable */
62 #define ARM_MPU_SH_NON (0U)
64 /** \brief Normal memory outer shareable */
65 #define ARM_MPU_SH_OUTER (2U)
67 /** \brief Normal memory inner shareable */
68 #define ARM_MPU_SH_INNER (3U)
70 /** \brief Memory access permissions
71 * \param RO Read-Only: Set to 1 for read-only memory.
72 * \param NP Non-Privileged: Set to 1 for non-privileged memory.
74 #define ARM_MPU_AP_(RO, NP) (((RO & 1U) << 1U) | (NP & 1U))
76 /** \brief Region Base Address Register value
77 * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
78 * \param SH Defines the Shareability domain for this memory region.
79 * \param RO Read-Only: Set to 1 for a read-only memory region.
80 * \param NP Non-Privileged: Set to 1 for a non-privileged memory region.
81 * \oaram XN eXecute Never: Set to 1 for a non-executable memory region.
83 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
84 ((BASE & MPU_RBAR_BASE_Pos) | \
85 ((SH << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
86 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
87 ((XN << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
89 /** \brief Region Limit Address Register value
90 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
91 * \param IDX The attribute index to be associated with this memory region.
93 #define ARM_MPU_RLAR(LIMIT, IDX) \
94 ((LIMIT & MPU_RLAR_LIMIT_Msk) | \
95 ((IDX << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
99 * Struct for a single MPU Region
101 typedef struct _ARM_MPU_Region_t {
102 uint32_t RBAR; /*!< Region Base Address Register value */
103 uint32_t RLAR; /*!< Region Limit Address Register value */
107 * \param MPU_Control Default access permissions for unconfigured regions.
109 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
113 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
114 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
115 SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
121 __STATIC_INLINE void ARM_MPU_Disable(void)
125 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
126 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
128 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
132 /** Enable the Non-secure MPU.
133 * \param MPU_Control Default access permissions for unconfigured regions.
135 __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
139 MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
140 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
141 SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
145 /** Disable the Non-secure MPU.
147 __STATIC_INLINE void ARM_MPU_Disable_NS(void)
151 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
152 SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
154 MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
158 /** Set the memory attribute encoding to the given MPU.
159 * \param mpu Pointer to the MPU to be configured.
160 * \param idx The attribute index to be set [0-7]
161 * \param attr The attribute value to be set.
163 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
165 const uint8_t reg = idx / 4U;
166 const uint32_t pos = ((idx % 4U) * 8U);
167 const uint32_t mask = 0xFFU << pos;
169 if (reg >= (sizeof(MPU->MAIR) / sizeof(MPU->MAIR[0]))) {
170 return; // invalid index
173 MPU->MAIR[reg] = ((MPU->MAIR[reg] & ~mask) | ((attr << pos) & mask));
176 /** Set the memory attribute encoding.
177 * \param idx The attribute index to be set [0-7]
178 * \param attr The attribute value to be set.
180 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
182 ARM_MPU_SetMemAttrEx(MPU, idx, attr);
186 /** Set the memory attribute encoding to the Non-secure MPU.
187 * \param idx The attribute index to be set [0-7]
188 * \param attr The attribute value to be set.
190 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
192 ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
196 /** Clear and disable the given MPU region of the given MPU.
197 * \param mpu Pointer to MPU to be used.
198 * \param rnr Region number to be cleared.
200 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
206 /** Clear and disable the given MPU region.
207 * \param rnr Region number to be cleared.
209 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
211 ARM_MPU_ClrRegionEx(MPU, rnr);
215 /** Clear and disable the given Non-secure MPU region.
216 * \param rnr Region number to be cleared.
218 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
220 ARM_MPU_ClrRegionEx(MPU_NS, rnr);
224 /** Configure the given MPU region of the given MPU.
225 * \param mpu Pointer to MPU to be used.
226 * \param rnr Region number to be configured.
227 * \param rbar Value for RBAR register.
228 * \param rlar Value for RLAR register.
230 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
237 /** Configure the given MPU region.
238 * \param rnr Region number to be configured.
239 * \param rbar Value for RBAR register.
240 * \param rlar Value for RLAR register.
242 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
244 ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
248 /** Configure the given Non-secure MPU region.
249 * \param rnr Region number to be configured.
250 * \param rbar Value for RBAR register.
251 * \param rlar Value for RLAR register.
253 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
255 ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
259 /** Memcopy with strictly ordered memory access, e.g. for register targets.
260 * \param dst Destination data is copied to.
261 * \param src Source data is copied from.
262 * \param len Amount of data words to be copied.
264 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
267 for (i = 0U; i < len; ++i)
273 /** Load the given number of MPU regions from a table to the given MPU.
274 * \param mpu Pointer to the MPU registers to be used.
275 * \param rnr First region number to be configured.
276 * \param table Pointer to the MPU configuration table.
277 * \param cnt Amount of regions to be configured.
279 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
281 static const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
284 orderedCpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
286 uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
287 uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
290 if ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
291 uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
292 orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
293 ARM_MPU_LoadEx(mpu, rnr + c, table + c, cnt - c);
295 orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
300 /** Load the given number of MPU regions from a table.
301 * \param rnr First region number to be configured.
302 * \param table Pointer to the MPU configuration table.
303 * \param cnt Amount of regions to be configured.
305 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
307 ARM_MPU_LoadEx(MPU, rnr, table, cnt);
311 /** Load the given number of MPU regions from a table to the Non-secure MPU.
312 * \param rnr First region number to be configured.
313 * \param table Pointer to the MPU configuration table.
314 * \param cnt Amount of regions to be configured.
316 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
318 ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);