YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
mpu_armv8.h
1/******************************************************************************
2 * @file mpu_armv8.h
3 * @brief CMSIS MPU API for Armv8-M and Armv8.1-M MPU
4 * @version V5.9.0
5 * @date 11. April 2023
6 ******************************************************************************/
7/*
8 * Copyright (c) 2017-2022 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#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 */
29#endif
30
31#ifndef ARM_MPU_ARMV8_H
32#define ARM_MPU_ARMV8_H
33
35#define ARM_MPU_ATTR_DEVICE ( 0U )
36
38#define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
39
46#define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
47 ((((NT) & 1U) << 3U) | (((WB) & 1U) << 2U) | (((RA) & 1U) << 1U) | ((WA) & 1U))
48
50#define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
51
53#define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
54
56#define ARM_MPU_ATTR_DEVICE_nGRE (2U)
57
59#define ARM_MPU_ATTR_DEVICE_GRE (3U)
60
64#define MPU_ATTR_NORMAL_OUTER_NON_CACHEABLE (0b0100)
65#define MPU_ATTR_NORMAL_OUTER_WT_TR_RA (0b0010)
66#define MPU_ATTR_NORMAL_OUTER_WT_TR_WA (0b0001)
67#define MPU_ATTR_NORMAL_OUTER_WT_TR_RA_WA (0b0011)
68#define MPU_ATTR_NORMAL_OUTER_WT_RA (0b1010)
69#define MPU_ATTR_NORMAL_OUTER_WT_WA (0b1001)
70#define MPU_ATTR_NORMAL_OUTER_WT_RA_WA (0b1011)
71#define MPU_ATTR_NORMAL_OUTER_WB_TR_RA (0b0101)
72#define MPU_ATTR_NORMAL_OUTER_WB_TR_WA (0b0110)
73#define MPU_ATTR_NORMAL_OUTER_WB_TR_RA_WA (0b0111)
74#define MPU_ATTR_NORMAL_OUTER_WB_RA (0b1101)
75#define MPU_ATTR_NORMAL_OUTER_WB_WA (0b1110)
76#define MPU_ATTR_NORMAL_OUTER_WB_RA_WA (0b1111)
77#define MPU_ATTR_NORMAL_INNER_NON_CACHEABLE (0b0100)
78#define MPU_ATTR_NORMAL_INNER_WT_TR_RA (0b0010)
79#define MPU_ATTR_NORMAL_INNER_WT_TR_WA (0b0001)
80#define MPU_ATTR_NORMAL_INNER_WT_TR_RA_WA (0b0011)
81#define MPU_ATTR_NORMAL_INNER_WT_RA (0b1010)
82#define MPU_ATTR_NORMAL_INNER_WT_WA (0b1001)
83#define MPU_ATTR_NORMAL_INNER_WT_RA_WA (0b1011)
84#define MPU_ATTR_NORMAL_INNER_WB_TR_RA (0b0101)
85#define MPU_ATTR_NORMAL_INNER_WB_TR_WA (0b0110)
86#define MPU_ATTR_NORMAL_INNER_WB_TR_RA_WA (0b0111)
87#define MPU_ATTR_NORMAL_INNER_WB_RA (0b1101)
88#define MPU_ATTR_NORMAL_INNER_WB_WA (0b1110)
89#define MPU_ATTR_NORMAL_INNER_WB_RA_WA (0b1111)
90
95#define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
96
97/* \brief Specifies MAIR_ATTR number */
98#define MAIR_ATTR(x) ((x > 7 || x < 0) ? 0 : x)
99
104#define ARM_MPU_SH_NON (0U)
105
107#define ARM_MPU_SH_OUTER (2U)
108
110#define ARM_MPU_SH_INNER (3U)
111
117#define ARM_MPU_AP_RW (0U)
118
120#define ARM_MPU_AP_RO (1U)
121
123#define ARM_MPU_AP_NP (1U)
124
126#define ARM_MPU_AP_PO (0U)
127
128/*
129 * Execute-never
130 * XN = Execute-never, EX = Executable
131 */
133#define ARM_MPU_XN (1U)
134
136#define ARM_MPU_EX (0U)
137
142#define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
143
151#define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
152 (((BASE) & MPU_RBAR_BASE_Msk) | \
153 (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
154 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
155 (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
156
161#define ARM_MPU_RLAR(LIMIT, IDX) \
162 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
163 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
164 (MPU_RLAR_EN_Msk))
165
166#if defined(MPU_RLAR_PXN_Pos)
167
173#define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
174 (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
175 (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
176 (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
177 (MPU_RLAR_EN_Msk))
178
179#endif
180
184typedef struct {
185 uint32_t RBAR;
186 uint32_t RLAR;
188
193__STATIC_INLINE uint32_t ARM_MPU_TYPE()
194{
195 return ((MPU->TYPE) >> 8);
196}
197
201__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
202{
203 __DMB();
204 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
205#ifdef SCB_SHCSR_MEMFAULTENA_Msk
207#endif
208 __DSB();
209 __ISB();
210}
211
214__STATIC_INLINE void ARM_MPU_Disable(void)
215{
216 __DMB();
217#ifdef SCB_SHCSR_MEMFAULTENA_Msk
218 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
219#endif
220 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
221 __DSB();
222 __ISB();
223}
224
225#ifdef MPU_NS
229__STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
230{
231 __DMB();
232 MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
233#ifdef SCB_SHCSR_MEMFAULTENA_Msk
234 SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
235#endif
236 __DSB();
237 __ISB();
238}
239
242__STATIC_INLINE void ARM_MPU_Disable_NS(void)
243{
244 __DMB();
245#ifdef SCB_SHCSR_MEMFAULTENA_Msk
246 SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
247#endif
248 MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
249 __DSB();
250 __ISB();
251}
252#endif
253
259__STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
260{
261 const uint8_t reg = idx / 4U;
262 const uint32_t pos = ((idx % 4U) * 8U);
263 const uint32_t mask = 0xFFU << pos;
264 const uint32_t val = (uint32_t)attr << pos;
265
266 if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
267 return; // invalid index
268 }
269
270 mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | (val & mask));
271}
272
277__STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
278{
279 ARM_MPU_SetMemAttrEx(MPU, idx, attr);
280}
281
282#ifdef MPU_NS
287__STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
288{
289 ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
290}
291#endif
292
297__STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
298{
299 mpu->RNR = rnr;
300 mpu->RLAR = 0U;
301}
302
306__STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
307{
308 ARM_MPU_ClrRegionEx(MPU, rnr);
309}
310
311#ifdef MPU_NS
315__STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
316{
317 ARM_MPU_ClrRegionEx(MPU_NS, rnr);
318}
319#endif
320
327__STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
328{
329 mpu->RNR = rnr;
330 mpu->RBAR = rbar;
331 mpu->RLAR = rlar;
332}
333
339__STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
340{
341 ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
342}
343
344#ifdef MPU_NS
350__STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
351{
352 ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
353}
354#endif
355
361__STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
362{
363 uint32_t i;
364 for (i = 0U; i < len; ++i)
365 {
366 dst[i] = src[i];
367 }
368}
369
376__STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
377{
378 const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
379 if (cnt == 1U) {
380 mpu->RNR = rnr;
381 ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
382 } else {
383 uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
384 uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
385
386 mpu->RNR = rnrBase;
387 while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
388 uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
389 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
390 table += c;
391 cnt -= c;
392 rnrOffset = 0U;
393 rnrBase += MPU_TYPE_RALIASES;
394 mpu->RNR = rnrBase;
395 }
396
397 ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
398 }
399}
400
406__STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
407{
408 ARM_MPU_LoadEx(MPU, rnr, table, cnt);
409}
410
411#ifdef MPU_NS
417__STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
418{
419 ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
420}
421#endif
422
423#endif
424
#define SCB
#define SCB_SHCSR_MEMFAULTENA_Msk
#define __ISB()
Instruction Synchronization Barrier.
#define __DSB()
Data Synchronization Barrier.
#define __DMB()
Data Memory Barrier.
uint32_t RBAR
The region base address register value (RBAR)
Definition mpu_armv7.h:184