RTEMS 7.0-rc1
Loading...
Searching...
No Matches
mpu_armv8.h
1/*
2 * The file was modified by RTEMS contributors.
3 */
4/******************************************************************************
5 * @file mpu_armv8.h
6 * @brief CMSIS MPU API for Armv8-M and Armv8.1-M MPU
7 * @version V5.9.0
8 * @date 11. April 2023
9 ******************************************************************************/
10/*
11 * Copyright (c) 2017-2022 Arm Limited. All rights reserved.
12 *
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the License); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
27
28#if defined ( __ICCARM__ )
29 #pragma system_include /* treat file as system include file for MISRA check */
30#elif defined (__clang__)
31 #pragma clang system_header /* treat file as system include file */
32#endif
33
34#ifndef ARM_MPU_ARMV8_H
35#define ARM_MPU_ARMV8_H
36
38#define ARM_MPU_ATTR_DEVICE ( 0U )
39
41#define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
42
49#define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
50 ((((NT) & 1U) << 3U) | (((WB) & 1U) << 2U) | (((RA) & 1U) << 1U) | ((WA) & 1U))
51
53#define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
54
56#define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
57
59#define ARM_MPU_ATTR_DEVICE_nGRE (2U)
60
62#define ARM_MPU_ATTR_DEVICE_GRE (3U)
63
67#define MPU_ATTR_NORMAL_OUTER_NON_CACHEABLE (0b0100)
68#define MPU_ATTR_NORMAL_OUTER_WT_TR_RA (0b0010)
69#define MPU_ATTR_NORMAL_OUTER_WT_TR_WA (0b0001)
70#define MPU_ATTR_NORMAL_OUTER_WT_TR_RA_WA (0b0011)
71#define MPU_ATTR_NORMAL_OUTER_WT_RA (0b1010)
72#define MPU_ATTR_NORMAL_OUTER_WT_WA (0b1001)
73#define MPU_ATTR_NORMAL_OUTER_WT_RA_WA (0b1011)
74#define MPU_ATTR_NORMAL_OUTER_WB_TR_RA (0b0101)
75#define MPU_ATTR_NORMAL_OUTER_WB_TR_WA (0b0110)
76#define MPU_ATTR_NORMAL_OUTER_WB_TR_RA_WA (0b0111)
77#define MPU_ATTR_NORMAL_OUTER_WB_RA (0b1101)
78#define MPU_ATTR_NORMAL_OUTER_WB_WA (0b1110)
79#define MPU_ATTR_NORMAL_OUTER_WB_RA_WA (0b1111)
80#define MPU_ATTR_NORMAL_INNER_NON_CACHEABLE (0b0100)
81#define MPU_ATTR_NORMAL_INNER_WT_TR_RA (0b0010)
82#define MPU_ATTR_NORMAL_INNER_WT_TR_WA (0b0001)
83#define MPU_ATTR_NORMAL_INNER_WT_TR_RA_WA (0b0011)
84#define MPU_ATTR_NORMAL_INNER_WT_RA (0b1010)
85#define MPU_ATTR_NORMAL_INNER_WT_WA (0b1001)
86#define MPU_ATTR_NORMAL_INNER_WT_RA_WA (0b1011)
87#define MPU_ATTR_NORMAL_INNER_WB_TR_RA (0b0101)
88#define MPU_ATTR_NORMAL_INNER_WB_TR_WA (0b0110)
89#define MPU_ATTR_NORMAL_INNER_WB_TR_RA_WA (0b0111)
90#define MPU_ATTR_NORMAL_INNER_WB_RA (0b1101)
91#define MPU_ATTR_NORMAL_INNER_WB_WA (0b1110)
92#define MPU_ATTR_NORMAL_INNER_WB_RA_WA (0b1111)
93
98#define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
99
100/* \brief Specifies MAIR_ATTR number */
101#define MAIR_ATTR(x) ((x > 7 || x < 0) ? 0 : x)
102
107#define ARM_MPU_SH_NON (0U)
108
110#define ARM_MPU_SH_OUTER (2U)
111
113#define ARM_MPU_SH_INNER (3U)
114
120#define ARM_MPU_AP_RW (0U)
121
123#define ARM_MPU_AP_RO (1U)
124
126#define ARM_MPU_AP_NP (1U)
127
129#define ARM_MPU_AP_PO (0U)
130
131/*
132 * Execute-never
133 * XN = Execute-never, EX = Executable
134 */
136#define ARM_MPU_XN (1U)
137
139#define ARM_MPU_EX (0U)
140
145#define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
146
154#define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
155 (((BASE) & MPU_RBAR_BASE_Msk) | \
156 (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
157 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
158 (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
159
164#define ARM_MPU_RLAR(LIMIT, IDX) \
165 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
166 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
167 (MPU_RLAR_EN_Msk))
168
169#if defined(MPU_RLAR_PXN_Pos)
170
176#define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
177 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
178 (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
179 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
180 (MPU_RLAR_EN_Msk))
181
182#endif
183
187typedef struct {
188 uint32_t RBAR;
189 uint32_t RLAR;
191
196#ifndef __rtems__
197__STATIC_INLINE uint32_t ARM_MPU_TYPE()
198#else /* __rtems__ */
199__STATIC_INLINE uint32_t ARM_MPU_TYPE(void)
200#endif /* __rtems__ */
201{
202 return ((MPU->TYPE) >> 8);
203}
204
208__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
209{
210 __DMB();
211 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
212#ifdef SCB_SHCSR_MEMFAULTENA_Msk
214#endif
215 __DSB();
216 __ISB();
217}
218
221__STATIC_INLINE void ARM_MPU_Disable(void)
222{
223 __DMB();
224#ifdef SCB_SHCSR_MEMFAULTENA_Msk
225 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
226#endif
227 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
228 __DSB();
229 __ISB();
230}
231
232#ifdef MPU_NS
236__STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
237{
238 __DMB();
239 MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
240#ifdef SCB_SHCSR_MEMFAULTENA_Msk
241 SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
242#endif
243 __DSB();
244 __ISB();
245}
246
249__STATIC_INLINE void ARM_MPU_Disable_NS(void)
250{
251 __DMB();
252#ifdef SCB_SHCSR_MEMFAULTENA_Msk
253 SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
254#endif
255 MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
256 __DSB();
257 __ISB();
258}
259#endif
260
266__STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
267{
268 const uint8_t reg = idx / 4U;
269 const uint32_t pos = ((idx % 4U) * 8U);
270 const uint32_t mask = 0xFFU << pos;
271 const uint32_t val = (uint32_t)attr << pos;
272
273 if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
274 return; // invalid index
275 }
276
277 mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | (val & mask));
278}
279
284__STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
285{
286 ARM_MPU_SetMemAttrEx(MPU, idx, attr);
287}
288
289#ifdef MPU_NS
294__STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
295{
296 ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
297}
298#endif
299
304__STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
305{
306 mpu->RNR = rnr;
307 mpu->RLAR = 0U;
308}
309
313__STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
314{
315 ARM_MPU_ClrRegionEx(MPU, rnr);
316}
317
318#ifdef MPU_NS
322__STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
323{
324 ARM_MPU_ClrRegionEx(MPU_NS, rnr);
325}
326#endif
327
334__STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
335{
336 mpu->RNR = rnr;
337 mpu->RBAR = rbar;
338 mpu->RLAR = rlar;
339}
340
346__STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
347{
348 ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
349}
350
351#ifdef MPU_NS
357__STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
358{
359 ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
360}
361#endif
362
368__STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
369{
370 uint32_t i;
371 for (i = 0U; i < len; ++i)
372 {
373 dst[i] = src[i];
374 }
375}
376
383__STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
384{
385 const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
386 if (cnt == 1U) {
387 mpu->RNR = rnr;
388 ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
389 } else {
390 uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
391 uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
392
393 mpu->RNR = rnrBase;
394 while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
395 uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
396 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
397 table += c;
398 cnt -= c;
399 rnrOffset = 0U;
400 rnrBase += MPU_TYPE_RALIASES;
401 mpu->RNR = rnrBase;
402 }
403
404 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
405 }
406}
407
413__STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
414{
415 ARM_MPU_LoadEx(MPU, rnr, table, cnt);
416}
417
418#ifdef MPU_NS
424__STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
425{
426 ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
427}
428#endif
429
430#endif
431
__STATIC_FORCEINLINE void __DSB(void)
Data Synchronization Barrier.
Definition: cmsis_gcc.h:286
__STATIC_FORCEINLINE void __DMB(void)
Data Memory Barrier.
Definition: cmsis_gcc.h:297
__STATIC_FORCEINLINE void __ISB(void)
Instruction Synchronization Barrier.
Definition: cmsis_gcc.h:275
#define SCB_SHCSR_MEMFAULTENA_Msk
Definition: core_cm33.h:723
#define SCB
Definition: core_cm33.h:2237
__STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)
Definition: mpu_armv7.h:239
__STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)
Definition: mpu_armv7.h:228
__STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const *table, uint32_t cnt)
Definition: mpu_armv7.h:264
__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
Definition: mpu_armv7.h:191
__STATIC_INLINE void ARM_MPU_Disable(void)
Definition: mpu_armv7.h:204
__STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
Definition: mpu_armv7.h:218
__STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t *dst, const uint32_t *__RESTRICT src, uint32_t len)
Definition: mpu_armv7.h:251
Definition: mpu_armv7.h:183
uint32_t RLAR
Definition: mpu_armv8.h:189
uint32_t RBAR
The region base address register value (RBAR)
Definition: mpu_armv7.h:184