Zephyr Project API  3.3.0
A Scalable Open Source RTOS
arm_mpu_v8.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Linaro Limited.
3 * Copyright (c) 2018 Nordic Semiconductor ASA.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef _ASMLANGUAGE
9
10/* Convenience macros to represent the ARMv8-M-specific
11 * configuration for memory access permission and
12 * cache-ability attribution.
13 */
14#if defined(CONFIG_AARCH32_ARMV8_R)
15#define MPU_IR_REGION_Msk (0xFFU)
16#define MPU_IR_REGION_Pos 8U
17/* MPU RBAR Register attribute msk Definitions */
18#define MPU_RBAR_BASE_Pos 6U
19#define MPU_RBAR_BASE_Msk (0x3FFFFFFFFFFFFFFUL << MPU_RBAR_BASE_Pos)
20#define MPU_RBAR_SH_Pos 3U
21#define MPU_RBAR_SH_Msk (0x3UL << MPU_RBAR_SH_Pos)
22#define MPU_RBAR_AP_Pos 1U
23#define MPU_RBAR_AP_Msk (0x3UL << MPU_RBAR_AP_Pos)
24/* RBAR XN */
25#define MPU_RBAR_XN_Pos 0U
26#define MPU_RBAR_XN_Msk (0x1UL << MPU_RBAR_XN_Pos)
27
28/* MPU PLBAR Register Definitions */
29#define MPU_RLAR_LIMIT_Pos 6U
30#define MPU_RLAR_LIMIT_Msk (0x3FFFFFFFFFFFFFFUL << MPU_RLAR_LIMIT_Pos)
31#define MPU_RLAR_AttrIndx_Pos 1U
32#define MPU_RLAR_AttrIndx_Msk (0x7UL << MPU_RLAR_AttrIndx_Pos)
33#define MPU_RLAR_EN_Msk (0x1UL)
34#else
36#endif
37
38/* Privileged No Access, Unprivileged No Access */
39/*#define NO_ACCESS 0x0 */
40/*#define NO_ACCESS_Msk ((NO_ACCESS << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) */
41/* Privileged No Access, Unprivileged No Access */
42/*#define P_NA_U_NA 0x0 */
43/*#define P_NA_U_NA_Msk ((P_NA_U_NA << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) */
44/* Privileged Read Write, Unprivileged No Access */
45#define P_RW_U_NA 0x0
46#define P_RW_U_NA_Msk ((P_RW_U_NA << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
47/* Privileged Read Write, Unprivileged Read Only */
48/*#define P_RW_U_RO 0x2 */
49/*#define P_RW_U_RO_Msk ((P_RW_U_RO << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk)*/
50/* Privileged Read Write, Unprivileged Read Write */
51#define P_RW_U_RW 0x1
52#define P_RW_U_RW_Msk ((P_RW_U_RW << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
53/* Privileged Read Write, Unprivileged Read Write */
54#define FULL_ACCESS 0x1
55#define FULL_ACCESS_Msk ((FULL_ACCESS << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
56/* Privileged Read Only, Unprivileged No Access */
57#define P_RO_U_NA 0x2
58#define P_RO_U_NA_Msk ((P_RO_U_NA << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
59/* Privileged Read Only, Unprivileged Read Only */
60#define P_RO_U_RO 0x3
61#define P_RO_U_RO_Msk ((P_RO_U_RO << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
62/* Privileged Read Only, Unprivileged Read Only */
63#define RO 0x3
64#define RO_Msk ((RO << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk)
65
66/* Attribute flag for not-allowing execution (eXecute Never) */
67#define NOT_EXEC MPU_RBAR_XN_Msk
68
69/* Attribute flags for share-ability */
70#define NON_SHAREABLE 0x0
71#define NON_SHAREABLE_Msk \
72 ((NON_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
73#define OUTER_SHAREABLE 0x2
74#define OUTER_SHAREABLE_Msk \
75 ((OUTER_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
76#define INNER_SHAREABLE 0x3
77#define INNER_SHAREABLE_Msk \
78 ((INNER_SHAREABLE << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk)
79
80/* Helper define to calculate the region limit address. */
81#define REGION_LIMIT_ADDR(base, size) \
82 (((base & MPU_RBAR_BASE_Msk) + size - 1) & MPU_RLAR_LIMIT_Msk)
83
84/* Attribute flags for cache-ability */
85#if defined(CONFIG_AARCH32_ARMV8_R)
86/* Memory Attributes for Device Memory
87 * 1.Gathering (G/nG)
88 * Determines whether multiple accesses can be merged into a single
89 * bus transaction.
90 * nG: Number/size of accesses on the bus = number/size of accesses
91 * in code.
92 *
93 * 2.Reordering (R/nR)
94 * Determines whether accesses to the same device can be reordered.
95 * nR: Accesses to the same IMPLEMENTATION DEFINED block size will
96 * appear on the bus in program order.
97 *
98 * 3 Early Write Acknowledgment (E/nE)
99 * Indicates to the memory system whether a buffer can send
100 * acknowledgements.
101 * nE: The response should come from the end slave, not buffering in
102 * the interconnect.
103 */
104#define DEVICE_nGnRnE 0x0U
105#define DEVICE_nGnRE 0x4U
106#define DEVICE_nGRE 0x8U
107#define DEVICE_GRE 0xCU
108#endif
109
110/* Read/Write Allocation Configurations for Cacheable Memory */
111#define R_NON_W_NON 0x0 /* Do not allocate Read/Write */
112#define R_NON_W_ALLOC 0x1 /* Do not allocate Read, Allocate Write */
113#define R_ALLOC_W_NON 0x2 /* Allocate Read, Do not allocate Write */
114#define R_ALLOC_W_ALLOC 0x3 /* Allocate Read/Write */
115
116/* Memory Attributes for Normal Memory */
117#define NORMAL_O_WT_NT 0x80 /* Normal, Outer Write-through non-transient */
118#define NORMAL_O_WB_NT 0xC0 /* Normal, Outer Write-back non-transient */
119#define NORMAL_O_NON_C 0x40 /* Normal, Outer Non-Cacheable */
120
121#define NORMAL_I_WT_NT 0x08 /* Normal, Inner Write-through non-transient */
122#define NORMAL_I_WB_NT 0x0C /* Normal, Inner Write-back non-transient */
123#define NORMAL_I_NON_C 0x04 /* Normal, Inner Non-Cacheable */
124
125#define NORMAL_OUTER_INNER_WRITE_THROUGH_READ_ALLOCATE_NON_TRANS \
126 ((NORMAL_O_WT_NT | (R_ALLOC_W_NON << 4)) \
127 | \
128 (NORMAL_I_WT_NT | R_ALLOC_W_NON)) \
129
130#define NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_TRANS \
131 ((NORMAL_O_WB_NT | (R_ALLOC_W_ALLOC << 4)) \
132 | \
133 (NORMAL_I_WB_NT | R_ALLOC_W_ALLOC))
134
135#define NORMAL_OUTER_INNER_NON_CACHEABLE \
136 ((NORMAL_O_NON_C | (R_NON_W_NON << 4)) \
137 | \
138 (NORMAL_I_NON_C | R_NON_W_NON))
139
140/* Common cache-ability configuration for Flash, SRAM regions */
141#define MPU_CACHE_ATTRIBUTES_FLASH \
142 NORMAL_OUTER_INNER_WRITE_THROUGH_READ_ALLOCATE_NON_TRANS
143#define MPU_CACHE_ATTRIBUTES_SRAM \
144 NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_TRANS
145#define MPU_CACHE_ATTRIBUTES_SRAM_NOCACHE \
146 NORMAL_OUTER_INNER_NON_CACHEABLE
147
148/* Global MAIR configurations */
149#define MPU_MAIR_ATTR_FLASH MPU_CACHE_ATTRIBUTES_FLASH
150#define MPU_MAIR_INDEX_FLASH 0
151#define MPU_MAIR_ATTR_SRAM MPU_CACHE_ATTRIBUTES_SRAM
152#define MPU_MAIR_INDEX_SRAM 1
153#define MPU_MAIR_ATTR_SRAM_NOCACHE MPU_CACHE_ATTRIBUTES_SRAM_NOCACHE
154#define MPU_MAIR_INDEX_SRAM_NOCACHE 2
155
156#if defined(CONFIG_AARCH32_ARMV8_R)
157#define MPU_MAIR_ATTR_DEVICE DEVICE_nGnRnE
158#define MPU_MAIR_INDEX_DEVICE 3
159/* Flash region(s): Attribute-0
160 * SRAM region(s): Attribute-1
161 * SRAM no cache-able regions(s): Attribute-2
162 * DEVICE no cache-able regions(s): Attribute-3
163 */
164#define MPU_MAIR_ATTRS \
165 ((MPU_MAIR_ATTR_FLASH << (MPU_MAIR_INDEX_FLASH * 8)) | \
166 (MPU_MAIR_ATTR_SRAM << (MPU_MAIR_INDEX_SRAM * 8)) | \
167 (MPU_MAIR_ATTR_SRAM_NOCACHE << (MPU_MAIR_INDEX_SRAM_NOCACHE * 8)) | \
168 (MPU_MAIR_ATTR_DEVICE << (MPU_MAIR_INDEX_DEVICE * 8)))
169#else
170/* Flash region(s): Attribute-0
171 * SRAM region(s): Attribute-1
172 * SRAM no cache-able regions(s): Attribute-2
173 */
174#define MPU_MAIR_ATTRS \
175 (((MPU_MAIR_ATTR_FLASH << MPU_MAIR0_Attr0_Pos) & MPU_MAIR0_Attr0_Msk) | \
176 ((MPU_MAIR_ATTR_SRAM << MPU_MAIR0_Attr1_Pos) & MPU_MAIR0_Attr1_Msk) | \
177 ((MPU_MAIR_ATTR_SRAM_NOCACHE << MPU_MAIR0_Attr2_Pos) & \
178 MPU_MAIR0_Attr2_Msk))
179#endif
180
181/* Some helper defines for common regions.
182 *
183 * Note that the ARMv8-M/R MPU architecture requires that the
184 * enabled MPU regions are non-overlapping. Therefore, it is
185 * recommended to use these helper defines only for configuring
186 * fixed MPU regions at build-time (i.e. regions that are not
187 * expected to be re-programmed or re-adjusted at run-time so
188 * that they do not overlap with other MPU regions).
189 */
190#if defined(CONFIG_AARCH32_ARMV8_R)
191#define REGION_RAM_ATTR(limit) \
192 { \
193 .rbar = NOT_EXEC | \
194 P_RW_U_NA_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
195 /* Cache-ability */ \
196 .mair_idx = MPU_MAIR_INDEX_SRAM, \
197 .r_limit = limit - 1, /* Region Limit */ \
198 }
199
200#define REGION_RAM_TEXT_ATTR(limit) \
201 { \
202 .rbar = P_RO_U_RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
203 /* Cache-ability */ \
204 .mair_idx = MPU_MAIR_INDEX_SRAM, \
205 .r_limit = limit - 1, /* Region Limit */ \
206 }
207
208#define REGION_RAM_RO_ATTR(limit) \
209 { \
210 .rbar = NOT_EXEC | \
211 P_RO_U_RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
212 /* Cache-ability */ \
213 .mair_idx = MPU_MAIR_INDEX_SRAM, \
214 .r_limit = limit - 1, /* Region Limit */ \
215 }
216
217#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
218/* Note that the access permissions allow for un-privileged writes, contrary
219 * to ARMv7-M where un-privileged code has Read-Only permissions.
220 */
221#define REGION_FLASH_ATTR(limit) \
222 { \
223 .rbar = P_RW_U_RW_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
224 /* Cache-ability */ \
225 .mair_idx = MPU_MAIR_INDEX_FLASH, \
226 .r_limit = limit - 1, /* Region Limit */ \
227 }
228#else /* CONFIG_MPU_ALLOW_FLASH_WRITE */
229#define REGION_FLASH_ATTR(limit) \
230 { \
231 .rbar = RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
232 /* Cache-ability */ \
233 .mair_idx = MPU_MAIR_INDEX_FLASH, \
234 .r_limit = limit - 1, /* Region Limit */ \
235 }
236#endif /* CONFIG_MPU_ALLOW_FLASH_WRITE */
237
238#define REGION_DEVICE_ATTR(limit) \
239 { \
240 /* AP, XN, SH */ \
241 .rbar = NOT_EXEC | P_RW_U_NA_Msk | NON_SHAREABLE_Msk, \
242 /* Cache-ability */ \
243 .mair_idx = MPU_MAIR_INDEX_DEVICE, \
244 /* Region Limit */ \
245 .r_limit = limit - 1, \
246 }
247#else
248#define REGION_RAM_ATTR(base, size) \
249 {\
250 .rbar = NOT_EXEC | \
251 P_RW_U_NA_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
252 /* Cache-ability */ \
253 .mair_idx = MPU_MAIR_INDEX_SRAM, \
254 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
255 }
256
257#define REGION_RAM_NOCACHE_ATTR(base, size) \
258 {\
259 .rbar = NOT_EXEC | \
260 P_RW_U_NA_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
261 /* Cache-ability */ \
262 .mair_idx = MPU_MAIR_INDEX_SRAM_NOCACHE, \
263 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
264 }
265
266#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
267/* Note that the access permissions allow for un-privileged writes, contrary
268 * to ARMv7-M where un-privileged code has Read-Only permissions.
269 */
270#define REGION_FLASH_ATTR(base, size) \
271 {\
272 .rbar = P_RW_U_RW_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
273 /* Cache-ability */ \
274 .mair_idx = MPU_MAIR_INDEX_FLASH, \
275 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
276 }
277#else /* CONFIG_MPU_ALLOW_FLASH_WRITE */
278#define REGION_FLASH_ATTR(base, size) \
279 {\
280 .rbar = RO_Msk | NON_SHAREABLE_Msk, /* AP, XN, SH */ \
281 /* Cache-ability */ \
282 .mair_idx = MPU_MAIR_INDEX_FLASH, \
283 .r_limit = REGION_LIMIT_ADDR(base, size), /* Region Limit */ \
284 }
285#endif /* CONFIG_MPU_ALLOW_FLASH_WRITE */
286
287#endif
288
289struct arm_mpu_region_attr {
290 /* Attributes belonging to RBAR */
292 /* MAIR index for attribute indirection */
294 /* Region Limit Address value to be written to the RLAR register. */
296};
297
299
300/* Typedef for the k_mem_partition attribute */
301typedef struct {
305
306/* Kernel macros for memory attribution
307 * (access permissions and cache-ability).
308 *
309 * The macros are to be stored in k_mem_partition_attr_t
310 * objects. The format of a k_mem_partition_attr_t object
311 * is as follows: field <rbar> contains a direct mapping
312 * of the <XN> and <AP> bit-fields of the RBAR register;
313 * field <mair_idx> contains a direct mapping of AttrIdx
314 * bit-field, stored in RLAR register.
315 */
316
317/* Read-Write access permission attributes */
318#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
319 {(P_RW_U_RW_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
320#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
321 {(P_RW_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
322#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
323 {(P_RO_U_RO_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
324#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
325 {(P_RO_U_NA_Msk | NOT_EXEC), MPU_MAIR_INDEX_SRAM})
326
327/* Execution-allowed attributes */
328#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
329 {(P_RW_U_RW_Msk), MPU_MAIR_INDEX_SRAM})
330#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
331 {(P_RO_U_RO_Msk), MPU_MAIR_INDEX_SRAM})
332
333/*
334 * @brief Evaluate Write-ability
335 *
336 * Evaluate whether the access permissions include write-ability.
337 *
338 * @param attr The k_mem_partition_attr_t object holding the
339 * MPU attributes to be checked against write-ability.
340 */
341#define K_MEM_PARTITION_IS_WRITABLE(attr) \
342 ({ \
343 int __is_writable__; \
344 switch (attr.rbar & MPU_RBAR_AP_Msk) { \
345 case P_RW_U_RW_Msk: \
346 case P_RW_U_NA_Msk: \
347 __is_writable__ = 1; \
348 break; \
349 default: \
350 __is_writable__ = 0; \
351 } \
352 __is_writable__; \
353 })
354
355/*
356 * @brief Evaluate Execution allowance
357 *
358 * Evaluate whether the access permissions include execution.
359 *
360 * @param attr The k_mem_partition_attr_t object holding the
361 * MPU attributes to be checked against execution
362 * allowance.
363 */
364#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
365 (!((attr.rbar) & (NOT_EXEC)))
366
367/* Attributes for no-cache enabling (share-ability is selected by default) */
368
369/* Read-Write access permission attributes */
370#define K_MEM_PARTITION_P_RW_U_RW_NOCACHE ((k_mem_partition_attr_t) \
371 {(P_RW_U_RW_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
372 MPU_MAIR_INDEX_SRAM_NOCACHE})
373#define K_MEM_PARTITION_P_RW_U_NA_NOCACHE ((k_mem_partition_attr_t) \
374 {(P_RW_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
375 MPU_MAIR_INDEX_SRAM_NOCACHE})
376#define K_MEM_PARTITION_P_RO_U_RO_NOCACHE ((k_mem_partition_attr_t) \
377 {(P_RO_U_RO_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
378 MPU_MAIR_INDEX_SRAM_NOCACHE})
379#define K_MEM_PARTITION_P_RO_U_NA_NOCACHE ((k_mem_partition_attr_t) \
380 {(P_RO_U_NA_Msk | NOT_EXEC | OUTER_SHAREABLE_Msk), \
381 MPU_MAIR_INDEX_SRAM_NOCACHE})
382
383/* Execution-allowed attributes */
384#define K_MEM_PARTITION_P_RWX_U_RWX_NOCACHE ((k_mem_partition_attr_t) \
385 {(P_RW_U_RW_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
386#define K_MEM_PARTITION_P_RX_U_RX_NOCACHE ((k_mem_partition_attr_t) \
387 {(P_RO_U_RO_Msk | OUTER_SHAREABLE_Msk), MPU_MAIR_INDEX_SRAM_NOCACHE})
388
389#endif /* _ASMLANGUAGE */
390
391#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
392 BUILD_ASSERT((size > 0) && ((uint32_t)start % \
393 CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0U) && \
394 ((size) % CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0), \
395 " the start and size of the partition must align " \
396 "with the minimum MPU region size.")
uint32_t k_mem_partition_attr_t
Definition: arch.h:225
CMSIS interface file.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Definition: arm_mpu_v7m.h:141
uint8_t rbar
Definition: arm_mpu_v8.h:291
uint32_t r_limit
Definition: arm_mpu_v8.h:295
uint8_t mair_idx
Definition: arm_mpu_v8.h:293
uint16_t rbar
Definition: arm_mpu_v8.h:302
uint16_t mair_idx
Definition: arm_mpu_v8.h:303