Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
nxp_mpu.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Linaro Limited.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_MPU_NXP_MPU_H_
7#define ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_MPU_NXP_MPU_H_
8
9#ifndef _ASMLANGUAGE
10
11#include <fsl_common.h>
12
13#define NXP_MPU_BASE SYSMPU_BASE
14
15#define NXP_MPU_REGION_NUMBER 12
16
17/* Bus Master User Mode Access */
18#define UM_READ 4
19#define UM_WRITE 2
20#define UM_EXEC 1
21
22#define BM0_UM_SHIFT 0
23#define BM1_UM_SHIFT 6
24#define BM2_UM_SHIFT 12
25#define BM3_UM_SHIFT 18
26
27/* Bus Master Supervisor Mode Access */
28#define SM_RWX_ALLOW 0
29#define SM_RX_ALLOW 1
30#define SM_RW_ALLOW 2
31#define SM_SAME_AS_UM 3
32
33#define BM0_SM_SHIFT 3
34#define BM1_SM_SHIFT 9
35#define BM2_SM_SHIFT 15
36#define BM3_SM_SHIFT 21
37
38#define BM4_WE_SHIFT 24
39#define BM4_RE_SHIFT 25
40
41#ifdef CONFIG_USB_KINETIS
42#define BM4_PERMISSIONS ((1 << BM4_RE_SHIFT) | (1 << BM4_WE_SHIFT))
43#else
44#define BM4_PERMISSIONS 0
45#endif
46
47/* Read Attribute */
48#define MPU_REGION_READ ((UM_READ << BM0_UM_SHIFT) | \
49 (UM_READ << BM1_UM_SHIFT) | \
50 (UM_READ << BM2_UM_SHIFT) | \
51 (UM_READ << BM3_UM_SHIFT))
52
53/* Write Attribute */
54#define MPU_REGION_WRITE ((UM_WRITE << BM0_UM_SHIFT) | \
55 (UM_WRITE << BM1_UM_SHIFT) | \
56 (UM_WRITE << BM2_UM_SHIFT) | \
57 (UM_WRITE << BM3_UM_SHIFT))
58
59/* Execute Attribute */
60#define MPU_REGION_EXEC ((UM_EXEC << BM0_UM_SHIFT) | \
61 (UM_EXEC << BM1_UM_SHIFT) | \
62 (UM_EXEC << BM2_UM_SHIFT) | \
63 (UM_EXEC << BM3_UM_SHIFT))
64
65/* Super User Attributes */
66#define MPU_REGION_SU ((SM_SAME_AS_UM << BM0_SM_SHIFT) | \
67 (SM_SAME_AS_UM << BM1_SM_SHIFT) | \
68 (SM_SAME_AS_UM << BM2_SM_SHIFT) | \
69 (SM_SAME_AS_UM << BM3_SM_SHIFT))
70
71#define MPU_REGION_SU_RX ((SM_RX_ALLOW << BM0_SM_SHIFT) | \
72 (SM_RX_ALLOW << BM1_SM_SHIFT) | \
73 (SM_RX_ALLOW << BM2_SM_SHIFT) | \
74 (SM_RX_ALLOW << BM3_SM_SHIFT))
75
76#define MPU_REGION_SU_RW ((SM_RW_ALLOW << BM0_SM_SHIFT) | \
77 (SM_RW_ALLOW << BM1_SM_SHIFT) | \
78 (SM_RW_ALLOW << BM2_SM_SHIFT) | \
79 (SM_RW_ALLOW << BM3_SM_SHIFT))
80
81#define MPU_REGION_SU_RWX ((SM_RWX_ALLOW << BM0_SM_SHIFT) | \
82 (SM_RWX_ALLOW << BM1_SM_SHIFT) | \
83 (SM_RWX_ALLOW << BM2_SM_SHIFT) | \
84 (SM_RWX_ALLOW << BM3_SM_SHIFT))
85
86/* The ENDADDR field has the last 5 bit reserved and set to 1 */
87#define ENDADDR_ROUND(x) (x - 0x1F)
88
89#define REGION_USER_MODE_ATTR {(MPU_REGION_READ | \
90 MPU_REGION_WRITE | \
91 MPU_REGION_SU)}
92
93/* Some helper defines for common regions */
94#if defined(CONFIG_MPU_ALLOW_FLASH_WRITE)
95#define REGION_RAM_ATTR {((MPU_REGION_SU_RWX) | \
96 ((UM_READ | UM_WRITE | UM_EXEC) << BM3_UM_SHIFT) | \
97 (BM4_PERMISSIONS))}
98
99#define REGION_FLASH_ATTR {(MPU_REGION_SU_RWX)}
100
101#else
102#define REGION_RAM_ATTR {((MPU_REGION_SU_RW) | \
103 ((UM_READ | UM_WRITE) << BM3_UM_SHIFT) | \
104 (BM4_PERMISSIONS))}
105
106#define REGION_FLASH_ATTR {(MPU_REGION_READ | \
107 MPU_REGION_EXEC | \
108 MPU_REGION_SU)}
109#endif
110
111#define REGION_IO_ATTR {(MPU_REGION_READ | \
112 MPU_REGION_WRITE | \
113 MPU_REGION_EXEC | \
114 MPU_REGION_SU)}
115
116#define REGION_RO_ATTR {(MPU_REGION_READ | MPU_REGION_SU)}
117
118#define REGION_USER_RO_ATTR {(MPU_REGION_READ | \
119 MPU_REGION_SU)}
120
121/* ENET (Master 3) and USB (Master 4) devices will not be able
122to access RAM when the region is dynamically disabled in NXP MPU.
123DEBUGGER (Master 1) can't be disabled in Region 0. */
124#define REGION_DEBUGGER_AND_DEVICE_ATTR {((MPU_REGION_SU) | \
125 ((UM_READ | UM_WRITE) << BM3_UM_SHIFT) | \
126 (BM4_PERMISSIONS))}
127
128#define REGION_DEBUG_ATTR {MPU_REGION_SU}
129
130#define REGION_BACKGROUND_ATTR {MPU_REGION_SU_RW}
131
133 /* NXP MPU region access permission attributes */
135};
136
138
139/* Typedef for the k_mem_partition attribute*/
140typedef struct {
143
144/* Kernel macros for memory attribution
145 * (access permissions and cache-ability).
146 *
147 * The macros are to be stored in k_mem_partition_attr_t
148 * objects.
149 */
150
151/* Read-Write access permission attributes */
152#define K_MEM_PARTITION_P_NA_U_NA ((k_mem_partition_attr_t) \
153 {(MPU_REGION_SU)})
154#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
155 {(MPU_REGION_READ | MPU_REGION_WRITE | MPU_REGION_SU)})
156#define K_MEM_PARTITION_P_RW_U_RO ((k_mem_partition_attr_t) \
157 {(MPU_REGION_READ | MPU_REGION_SU_RW)})
158#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
159 {(MPU_REGION_SU_RW)})
160#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
161 {(MPU_REGION_READ | MPU_REGION_SU)})
162#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
163 {(MPU_REGION_SU_RX)})
164
165/* Execution-allowed attributes */
166#define K_MEM_PARTITION_P_RWX_U_RWX ((k_mem_partition_attr_t) \
167 {(MPU_REGION_READ | MPU_REGION_WRITE | \
168 MPU_REGION_EXEC | MPU_REGION_SU)})
169#define K_MEM_PARTITION_P_RWX_U_RX ((k_mem_partition_attr_t) \
170 {(MPU_REGION_READ | MPU_REGION_EXEC | MPU_REGION_SU_RWX)})
171#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
172 {(MPU_REGION_READ | MPU_REGION_EXEC | MPU_REGION_SU)})
173
174/*
175 * @brief Evaluate Write-ability
176 *
177 * Evaluate whether the access permissions include write-ability.
178 *
179 * @param attr The k_mem_partition_attr_t object holding the
180 * MPU attributes to be checked against write-ability.
181 */
182#define K_MEM_PARTITION_IS_WRITABLE(attr) \
183 ({ \
184 int __is_writable__; \
185 switch (attr.ap_attr) { \
186 case MPU_REGION_WRITE: \
187 case MPU_REGION_SU_RW: \
188 __is_writable__ = 1; \
189 break; \
190 default: \
191 __is_writable__ = 0; \
192 } \
193 __is_writable__; \
194 })
195
196/*
197 * @brief Evaluate Execution allowance
198 *
199 * Evaluate whether the access permissions include execution.
200 *
201 * @param attr The k_mem_partition_attr_t object holding the
202 * MPU attributes to be checked against execution
203 * allowance.
204 */
205#define K_MEM_PARTITION_IS_EXECUTABLE(attr) \
206 ({ \
207 int __is_executable__; \
208 switch (attr.ap_attr) { \
209 case MPU_REGION_SU_RX: \
210 case MPU_REGION_EXEC: \
211 __is_executable__ = 1; \
212 break; \
213 default: \
214 __is_executable__ = 0; \
215 } \
216 __is_executable__; \
217 })
218
219
220/* Region definition data structure */
222 /* Region Base Address */
224 /* Region End Address */
226 /* Region Name */
227 const char *name;
228 /* Region Attributes */
230};
231
232#define MPU_REGION_ENTRY(_name, _base, _end, _attr) \
233 {\
234 .name = _name, \
235 .base = _base, \
236 .end = _end, \
237 .attr = _attr, \
238 }
239
240/* MPU configuration data structure */
242 /* Number of regions */
244 /* Regions */
246 /* SRAM Region */
248};
249
250/* Reference to the MPU configuration.
251 *
252 * This struct is defined and populated for each SoC (in the SoC definition),
253 * and holds the build-time configuration information for the fixed MPU
254 * regions enabled during kernel initialization. Dynamic MPU regions (e.g.
255 * for Thread Stack, Stack Guards, etc.) are programmed during runtime, thus,
256 * not kept here.
257 */
258extern const struct nxp_mpu_config mpu_config;
259
260#endif /* _ASMLANGUAGE */
261
262#define _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size) \
263 BUILD_ASSERT((size) % \
264 CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0 && \
265 (size) >= CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE && \
266 (uint32_t)(start) % CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE == 0, \
267 "the size of the partition must align with minimum MPU \
268 region size" \
269 " and greater than or equal to minimum MPU region size." \
270 "start address of the partition must align with minimum MPU \
271 region size.")
272
273#endif /* ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_MPU_NXP_MPU_H_ */
uint32_t k_mem_partition_attr_t
Definition: arch.h:267
const struct nxp_mpu_config mpu_config
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
uint32_t ap_attr
Definition: nxp_mpu.h:141
Definition: nxp_mpu.h:241
uint32_t sram_region
Definition: nxp_mpu.h:247
const struct nxp_mpu_region * mpu_regions
Definition: nxp_mpu.h:245
uint32_t num_regions
Definition: nxp_mpu.h:243
Definition: nxp_mpu.h:132
uint32_t attr
Definition: nxp_mpu.h:134
Definition: nxp_mpu.h:221
uint32_t base
Definition: nxp_mpu.h:223
const char * name
Definition: nxp_mpu.h:227
uint32_t end
Definition: nxp_mpu.h:225
nxp_mpu_region_attr_t attr
Definition: nxp_mpu.h:229