]> begriffs open source - cmsis/blob - CMSIS/Core/Include/mpu_armv8.h
Core(M): Added __DSP_USED define to ARMv8-MML/Cortex-M33 to catch DSP extension misco...
[cmsis] / CMSIS / Core / Include / mpu_armv8.h
1 /******************************************************************************
2  * @file     mpu_armv8.h
3  * @brief    CMSIS MPU API for ARMv8 MPU
4  * @version  V5.0.3
5  * @date     09. August 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
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
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
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.
23  */
24  
25 #ifndef ARM_MPU_ARMV8_H
26 #define ARM_MPU_ARMV8_H
27
28 /** \brief Attribute for device memory (outer only) */
29 #define ARM_MPU_ATTR_DEVICE                           ( 0U )
30
31 /** \brief Attribute for non-cacheable, normal memory */
32 #define ARM_MPU_ATTR_NON_CACHEABLE                    ( 4U )
33
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.
39 */
40 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
41   (((NT & 1U) << 3U) | ((WB & 1U) << 2U) | ((RA & 1U) << 1U) | (WA & 1U))
42
43 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
44 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
45
46 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
47 #define ARM_MPU_ATTR_DEVICE_nGnRE  (1U)
48
49 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
50 #define ARM_MPU_ATTR_DEVICE_nGRE   (2U)
51
52 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
53 #define ARM_MPU_ATTR_DEVICE_GRE    (3U)
54
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
58 */
59 #define ARM_MPU_ATTR(O, I) (((O & 0xFU) << 4U) | (((O & 0xFU) != 0U) ? (I & 0xFU) : ((I & 0x3U) << 2U)))
60
61 /** \brief Normal memory non-shareable  */
62 #define ARM_MPU_SH_NON   (0U)
63
64 /** \brief Normal memory outer shareable  */
65 #define ARM_MPU_SH_OUTER (2U)
66
67 /** \brief Normal memory inner shareable  */
68 #define ARM_MPU_SH_INNER (3U)
69
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.
73 */
74 #define ARM_MPU_AP_(RO, NP) (((RO & 1U) << 1U) | (NP & 1U))
75
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.
82 */
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))
88
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.
92 */
93 #define ARM_MPU_RLAR(LIMIT, IDX) \
94   ((LIMIT & MPU_RLAR_LIMIT_Msk) | \
95   ((IDX << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
96   (MPU_RLAR_EN_Msk))
97
98 /**
99 * Struct for a single MPU Region
100 */
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 */
104 } ARM_MPU_Region_t;
105     
106 /** Enable the MPU.
107 * \param MPU_Control Default access permissions for unconfigured regions.
108 */
109 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
110 {
111   __DSB();
112   __ISB();
113   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
114 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
115   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
116 #endif
117 }
118
119 /** Disable the MPU.
120 */
121 __STATIC_INLINE void ARM_MPU_Disable(void)
122 {
123   __DSB();
124   __ISB();
125 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
126   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
127 #endif
128   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
129 }
130
131 #ifdef MPU_NS
132 /** Enable the Non-secure MPU.
133 * \param MPU_Control Default access permissions for unconfigured regions.
134 */
135 __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
136 {
137   __DSB();
138   __ISB();
139   MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
140 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
141   SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
142 #endif
143 }
144
145 /** Disable the Non-secure MPU.
146 */
147 __STATIC_INLINE void ARM_MPU_Disable_NS(void)
148 {
149   __DSB();
150   __ISB();
151 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
152   SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
153 #endif
154   MPU_NS->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
155 }
156 #endif
157
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.
162 */
163 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
164 {
165   const uint8_t reg = idx / 4U;
166   const uint32_t pos = ((idx % 4U) * 8U);
167   const uint32_t mask = 0xFFU << pos;
168   
169   if (reg >= (sizeof(MPU->MAIR) / sizeof(MPU->MAIR[0]))) {
170     return; // invalid index
171   }
172   
173   MPU->MAIR[reg] = ((MPU->MAIR[reg] & ~mask) | ((attr << pos) & mask));
174 }
175
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.
179 */
180 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
181 {
182   ARM_MPU_SetMemAttrEx(MPU, idx, attr);
183 }
184
185 #ifdef MPU_NS
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.
189 */
190 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
191 {
192   ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
193 }
194 #endif
195
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.
199 */
200 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
201 {
202   MPU->RNR = rnr;
203   MPU->RLAR = 0U;
204 }
205
206 /** Clear and disable the given MPU region.
207 * \param rnr Region number to be cleared.
208 */
209 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
210 {
211   ARM_MPU_ClrRegionEx(MPU, rnr);
212 }
213
214 #ifdef MPU_NS
215 /** Clear and disable the given Non-secure MPU region.
216 * \param rnr Region number to be cleared.
217 */
218 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
219 {  
220   ARM_MPU_ClrRegionEx(MPU_NS, rnr);
221 }
222 #endif
223
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.
229 */   
230 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
231 {
232   MPU->RNR = rnr;
233   MPU->RBAR = rbar;
234   MPU->RLAR = rlar;
235 }
236
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.
241 */   
242 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
243 {
244   ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
245 }
246
247 #ifdef MPU_NS
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.
252 */   
253 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
254 {
255   ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);  
256 }
257 #endif
258
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.
263 */
264 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
265 {
266   uint32_t i;
267   for (i = 0U; i < len; ++i) 
268   {
269     dst[i] = src[i];
270   }
271 }
272
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.
278 */
279 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) 
280 {
281   static const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
282   if (cnt == 1U) {
283     mpu->RNR = rnr;
284     orderedCpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
285   } else {
286     uint32_t rnrBase   = rnr & ~(MPU_TYPE_RALIASES-1U);
287     uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
288     
289     mpu->RNR = rnrBase;
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);
294     } else {
295       orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
296     }
297   }
298 }
299
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.
304 */
305 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) 
306 {
307   ARM_MPU_LoadEx(MPU, rnr, table, cnt);
308 }
309
310 #ifdef MPU_NS
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.
315 */
316 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) 
317 {
318   ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
319 }
320 #endif
321
322 #endif
323