SmartSnippets DA1459x SDK
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 03. February 2021
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2017-2021 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  /* Copyright (c) 2019-2023 Modified by Renesas Electronics Corporation and/or its affiliates. */
25 
26 #if defined ( __ICCARM__ )
27  #pragma system_include /* treat file as system include file for MISRA check */
28 #elif defined (__clang__)
29  #pragma clang system_header /* treat file as system include file */
30 #endif
31 
32 #ifndef ARM_MPU_ARMV8_H
33 #define ARM_MPU_ARMV8_H
34 
36 #define ARM_MPU_ATTR_DEVICE ( 0U )
37 
39 #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
40 
47 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
48  ((((NT) & 1U) << 3U) | (((WB) & 1U) << 2U) | (((RA) & 1U) << 1U) | ((WA) & 1U))
49 
51 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
52 
54 #define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
55 
57 #define ARM_MPU_ATTR_DEVICE_nGRE (2U)
58 
60 #define ARM_MPU_ATTR_DEVICE_GRE (3U)
61 
66 #define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
67 
69 #define ARM_MPU_SH_NON (0U)
70 
72 #define ARM_MPU_SH_OUTER (2U)
73 
75 #define ARM_MPU_SH_INNER (3U)
76 
81 #define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
82 
90 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
91  (((BASE) & MPU_RBAR_BASE_Msk) | \
92  (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
93  ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
94  (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
95 
100 #define ARM_MPU_RLAR(LIMIT, IDX) \
101  (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
102  (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
103  (MPU_RLAR_EN_Msk))
104 
105 #if defined(MPU_RLAR_PXN_Pos)
106 
112 #define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
113  (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
114  (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
115  (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
116  (MPU_RLAR_EN_Msk))
117 
118 #endif
119 
123 typedef struct {
124  uint32_t RBAR;
125  uint32_t RLAR;
127 
131 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
132 {
133  __DMB();
134  MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
135 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
136  SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
137 #endif
138  __DSB();
139  __ISB();
140 }
141 
144 __STATIC_INLINE void ARM_MPU_Disable(void)
145 {
146  __DMB();
147 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
148  SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
149 #endif
150  MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
151  __DSB();
152  __ISB();
153 }
154 
155 #ifdef MPU_NS
156 
159 __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
160 {
161  __DMB();
162  MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
163 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
164  SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
165 #endif
166  __DSB();
167  __ISB();
168 }
169 
172 __STATIC_INLINE void ARM_MPU_Disable_NS(void)
173 {
174  __DMB();
175 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
176  SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
177 #endif
178  MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
179  __DSB();
180  __ISB();
181 }
182 #endif
183 
189 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
190 {
191  const uint8_t reg = idx / 4U;
192  const uint32_t pos = ((idx % 4U) * 8U);
193  const uint32_t mask = 0xFFU << pos;
194 
195  if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
196  return; // invalid index
197  }
198 
199  mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));
200 }
201 
206 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
207 {
208  ARM_MPU_SetMemAttrEx(MPU, idx, attr);
209 }
210 
211 #ifdef MPU_NS
212 
216 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
217 {
218  ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
219 }
220 #endif
221 
226 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
227 {
228  mpu->RNR = rnr;
229  mpu->RLAR = 0U;
230 }
231 
235 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
236 {
237  ARM_MPU_ClrRegionEx(MPU, rnr);
238 }
239 
240 #ifdef MPU_NS
241 
244 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
245 {
246  ARM_MPU_ClrRegionEx(MPU_NS, rnr);
247 }
248 #endif
249 
256 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
257 {
258  mpu->RNR = rnr;
259  mpu->RBAR = rbar;
260  mpu->RLAR = rlar;
261 }
262 
268 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
269 {
270  ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
271 }
272 
273 #ifdef MPU_NS
274 
279 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
280 {
281  ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
282 }
283 #endif
284 
290 __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
291 {
292  uint32_t i;
293  for (i = 0U; i < len; ++i)
294  {
295  dst[i] = src[i];
296  }
297 }
298 
305 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
306 {
307  const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
308  if (cnt == 1U) {
309  mpu->RNR = rnr;
310  ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
311  } else {
312  uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
313  uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
314 
315  mpu->RNR = rnrBase;
316  while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
317  uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
318  ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
319  table += c;
320  cnt -= c;
321  rnrOffset = 0U;
322  rnrBase += MPU_TYPE_RALIASES;
323  mpu->RNR = rnrBase;
324  }
325 
326  ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
327  }
328 }
329 
335 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
336 {
337  ARM_MPU_LoadEx(MPU, rnr, table, cnt);
338 }
339 
340 #ifdef MPU_NS
341 
346 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
347 {
348  ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
349 }
350 #endif
351 
352 #endif
353 
__ISB
__STATIC_FORCEINLINE void __ISB(void)
Instruction Synchronization Barrier.
Definition: cmsis_gcc.h:874
ARM_MPU_Region_t
Definition: mpu_armv8.h:122
SCB_SHCSR_MEMFAULTENA_Msk
#define SCB_SHCSR_MEMFAULTENA_Msk
Definition: core_cm33.h:699
__DSB
__STATIC_FORCEINLINE void __DSB(void)
Data Synchronization Barrier.
Definition: cmsis_gcc.h:885
__DMB
__STATIC_FORCEINLINE void __DMB(void)
Data Memory Barrier.
Definition: cmsis_gcc.h:896
SCB
#define SCB
Definition: core_cm0.h:542