1 /******************************************************************************
3 * @brief CMSIS MPU API for Armv8-M MPU
5 * @date 10. January 2018
6 ******************************************************************************/
8 * Copyright (c) 2017-2018 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 #if defined ( __ICCARM__ )
26 #pragma system_include /* treat file as system include file for MISRA check */
27 #elif defined (__clang__)
28 #pragma clang system_header /* treat file as system include file */
31 #ifndef ARM_MPU_ARMV8_H
32 #define ARM_MPU_ARMV8_H
34 /** \brief Attribute for device memory (outer only) */
35 #define ARM_MPU_ATTR_DEVICE ( 0U )
37 /** \brief Attribute for non-cacheable, normal memory */
38 #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
40 /** \brief Attribute for normal memory (outer and inner)
41 * \param NT Non-Transient: Set to 1 for non-transient data.
42 * \param WB Write-Back: Set to 1 to use write-back update policy.
43 * \param RA Read Allocation: Set to 1 to use cache allocation on read miss.
44 * \param WA Write Allocation: Set to 1 to use cache allocation on write miss.
46 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
47 (((NT & 1U) << 3U) | ((WB & 1U) << 2U) | ((RA & 1U) << 1U) | (WA & 1U))
49 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
50 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
52 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
53 #define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
55 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
56 #define ARM_MPU_ATTR_DEVICE_nGRE (2U)
58 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
59 #define ARM_MPU_ATTR_DEVICE_GRE (3U)
61 /** \brief Memory Attribute
62 * \param O Outer memory attributes
63 * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
65 #define ARM_MPU_ATTR(O, I) (((O & 0xFU) << 4U) | (((O & 0xFU) != 0U) ? (I & 0xFU) : ((I & 0x3U) << 2U)))
67 /** \brief Normal memory non-shareable */
68 #define ARM_MPU_SH_NON (0U)
70 /** \brief Normal memory outer shareable */
71 #define ARM_MPU_SH_OUTER (2U)
73 /** \brief Normal memory inner shareable */
74 #define ARM_MPU_SH_INNER (3U)
76 /** \brief Memory access permissions
77 * \param RO Read-Only: Set to 1 for read-only memory.
78 * \param NP Non-Privileged: Set to 1 for non-privileged memory.
80 #define ARM_MPU_AP_(RO, NP) (((RO & 1U) << 1U) | (NP & 1U))
82 /** \brief Region Base Address Register value
83 * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
84 * \param SH Defines the Shareability domain for this memory region.
85 * \param RO Read-Only: Set to 1 for a read-only memory region.
86 * \param NP Non-Privileged: Set to 1 for a non-privileged memory region.
87 * \oaram XN eXecute Never: Set to 1 for a non-executable memory region.
89 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
90 ((BASE & MPU_RBAR_BASE_Pos) | \
91 ((SH << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
92 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
93 ((XN << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
95 /** \brief Region Limit Address Register value
96 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
97 * \param IDX The attribute index to be associated with this memory region.
99 #define ARM_MPU_RLAR(LIMIT, IDX) \
100 ((LIMIT & MPU_RLAR_LIMIT_Msk) | \
101 ((IDX << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
105 * Struct for a single MPU Region
108 uint32_t RBAR
; /*!< Region Base Address Register value */
109 uint32_t RLAR
; /*!< Region Limit Address Register value */
113 * \param MPU_Control Default access permissions for unconfigured regions.
115 __STATIC_INLINE
void ARM_MPU_Enable(uint32_t MPU_Control
)
119 MPU
->CTRL
= MPU_Control
| MPU_CTRL_ENABLE_Msk
;
120 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
121 SCB
->SHCSR
|= SCB_SHCSR_MEMFAULTENA_Msk
;
127 __STATIC_INLINE
void ARM_MPU_Disable(void)
131 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
132 SCB
->SHCSR
&= ~SCB_SHCSR_MEMFAULTENA_Msk
;
134 MPU
->CTRL
&= ~MPU_CTRL_ENABLE_Msk
;
138 /** Enable the Non-secure MPU.
139 * \param MPU_Control Default access permissions for unconfigured regions.
141 __STATIC_INLINE
void ARM_MPU_Enable_NS(uint32_t MPU_Control
)
145 MPU_NS
->CTRL
= MPU_Control
| MPU_CTRL_ENABLE_Msk
;
146 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
147 SCB_NS
->SHCSR
|= SCB_SHCSR_MEMFAULTENA_Msk
;
151 /** Disable the Non-secure MPU.
153 __STATIC_INLINE
void ARM_MPU_Disable_NS(void)
157 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
158 SCB_NS
->SHCSR
&= ~SCB_SHCSR_MEMFAULTENA_Msk
;
160 MPU_NS
->CTRL
&= ~MPU_CTRL_ENABLE_Msk
;
164 /** Set the memory attribute encoding to the given MPU.
165 * \param mpu Pointer to the MPU to be configured.
166 * \param idx The attribute index to be set [0-7]
167 * \param attr The attribute value to be set.
169 __STATIC_INLINE
void ARM_MPU_SetMemAttrEx(MPU_Type
* mpu
, uint8_t idx
, uint8_t attr
)
171 const uint8_t reg
= idx
/ 4U;
172 const uint32_t pos
= ((idx
% 4U) * 8U);
173 const uint32_t mask
= 0xFFU
<< pos
;
175 if (reg
>= (sizeof(mpu
->MAIR
) / sizeof(mpu
->MAIR
[0]))) {
176 return; // invalid index
179 mpu
->MAIR
[reg
] = ((mpu
->MAIR
[reg
] & ~mask
) | ((attr
<< pos
) & mask
));
182 /** Set the memory attribute encoding.
183 * \param idx The attribute index to be set [0-7]
184 * \param attr The attribute value to be set.
186 __STATIC_INLINE
void ARM_MPU_SetMemAttr(uint8_t idx
, uint8_t attr
)
188 ARM_MPU_SetMemAttrEx(MPU
, idx
, attr
);
192 /** Set the memory attribute encoding to the Non-secure MPU.
193 * \param idx The attribute index to be set [0-7]
194 * \param attr The attribute value to be set.
196 __STATIC_INLINE
void ARM_MPU_SetMemAttr_NS(uint8_t idx
, uint8_t attr
)
198 ARM_MPU_SetMemAttrEx(MPU_NS
, idx
, attr
);
202 /** Clear and disable the given MPU region of the given MPU.
203 * \param mpu Pointer to MPU to be used.
204 * \param rnr Region number to be cleared.
206 __STATIC_INLINE
void ARM_MPU_ClrRegionEx(MPU_Type
* mpu
, uint32_t rnr
)
212 /** Clear and disable the given MPU region.
213 * \param rnr Region number to be cleared.
215 __STATIC_INLINE
void ARM_MPU_ClrRegion(uint32_t rnr
)
217 ARM_MPU_ClrRegionEx(MPU
, rnr
);
221 /** Clear and disable the given Non-secure MPU region.
222 * \param rnr Region number to be cleared.
224 __STATIC_INLINE
void ARM_MPU_ClrRegion_NS(uint32_t rnr
)
226 ARM_MPU_ClrRegionEx(MPU_NS
, rnr
);
230 /** Configure the given MPU region of the given MPU.
231 * \param mpu Pointer to MPU to be used.
232 * \param rnr Region number to be configured.
233 * \param rbar Value for RBAR register.
234 * \param rlar Value for RLAR register.
236 __STATIC_INLINE
void ARM_MPU_SetRegionEx(MPU_Type
* mpu
, uint32_t rnr
, uint32_t rbar
, uint32_t rlar
)
243 /** Configure the given MPU region.
244 * \param rnr Region number to be configured.
245 * \param rbar Value for RBAR register.
246 * \param rlar Value for RLAR register.
248 __STATIC_INLINE
void ARM_MPU_SetRegion(uint32_t rnr
, uint32_t rbar
, uint32_t rlar
)
250 ARM_MPU_SetRegionEx(MPU
, rnr
, rbar
, rlar
);
254 /** Configure the given Non-secure MPU region.
255 * \param rnr Region number to be configured.
256 * \param rbar Value for RBAR register.
257 * \param rlar Value for RLAR register.
259 __STATIC_INLINE
void ARM_MPU_SetRegion_NS(uint32_t rnr
, uint32_t rbar
, uint32_t rlar
)
261 ARM_MPU_SetRegionEx(MPU_NS
, rnr
, rbar
, rlar
);
265 /** Memcopy with strictly ordered memory access, e.g. for register targets.
266 * \param dst Destination data is copied to.
267 * \param src Source data is copied from.
268 * \param len Amount of data words to be copied.
270 __STATIC_INLINE
void orderedCpy(volatile uint32_t* dst
, const uint32_t* __RESTRICT src
, uint32_t len
)
273 for (i
= 0U; i
< len
; ++i
)
279 /** Load the given number of MPU regions from a table to the given MPU.
280 * \param mpu Pointer to the MPU registers to be used.
281 * \param rnr First region number to be configured.
282 * \param table Pointer to the MPU configuration table.
283 * \param cnt Amount of regions to be configured.
285 __STATIC_INLINE
void ARM_MPU_LoadEx(MPU_Type
* mpu
, uint32_t rnr
, ARM_MPU_Region_t
const* table
, uint32_t cnt
)
287 const uint32_t rowWordSize
= sizeof(ARM_MPU_Region_t
)/4U;
290 orderedCpy(&(mpu
->RBAR
), &(table
->RBAR
), rowWordSize
);
292 uint32_t rnrBase
= rnr
& ~(MPU_TYPE_RALIASES
-1U);
293 uint32_t rnrOffset
= rnr
% MPU_TYPE_RALIASES
;
296 while ((rnrOffset
+ cnt
) > MPU_TYPE_RALIASES
) {
297 uint32_t c
= MPU_TYPE_RALIASES
- rnrOffset
;
298 orderedCpy(&(mpu
->RBAR
)+(rnrOffset
*2U), &(table
->RBAR
), c
*rowWordSize
);
302 rnrBase
+= MPU_TYPE_RALIASES
;
306 orderedCpy(&(mpu
->RBAR
)+(rnrOffset
*2U), &(table
->RBAR
), cnt
*rowWordSize
);
310 /** Load the given number of MPU regions from a table.
311 * \param rnr First region number to be configured.
312 * \param table Pointer to the MPU configuration table.
313 * \param cnt Amount of regions to be configured.
315 __STATIC_INLINE
void ARM_MPU_Load(uint32_t rnr
, ARM_MPU_Region_t
const* table
, uint32_t cnt
)
317 ARM_MPU_LoadEx(MPU
, rnr
, table
, cnt
);
321 /** Load the given number of MPU regions from a table to the Non-secure MPU.
322 * \param rnr First region number to be configured.
323 * \param table Pointer to the MPU configuration table.
324 * \param cnt Amount of regions to be configured.
326 __STATIC_INLINE
void ARM_MPU_Load_NS(uint32_t rnr
, ARM_MPU_Region_t
const* table
, uint32_t cnt
)
328 ARM_MPU_LoadEx(MPU_NS
, rnr
, table
, cnt
);