Zephyr Project API  3.2.0
A Scalable Open Source RTOS
arm_mmu.h
Go to the documentation of this file.
1/*
2 * Copyright 2019 Broadcom
3 * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef ZEPHYR_INCLUDE_ARCH_ARM64_ARM_MMU_H_
8#define ZEPHYR_INCLUDE_ARCH_ARM64_ARM_MMU_H_
9
10#ifndef _ASMLANGUAGE
11#include <stdint.h>
12#include <stdlib.h>
13#endif
14
15/* Following Memory types supported through MAIR encodings can be passed
16 * by user through "attrs"(attributes) field of specified memory region.
17 * As MAIR supports such 8 encodings, we will reserve attrs[2:0];
18 * so that we can provide encodings upto 7 if needed in future.
19 */
20#define MT_TYPE_MASK 0x7U
21#define MT_TYPE(attr) (attr & MT_TYPE_MASK)
22#define MT_DEVICE_nGnRnE 0U
23#define MT_DEVICE_nGnRE 1U
24#define MT_DEVICE_GRE 2U
25#define MT_NORMAL_NC 3U
26#define MT_NORMAL 4U
27#define MT_NORMAL_WT 5U
28
29#define MEMORY_ATTRIBUTES ((0x00 << (MT_DEVICE_nGnRnE * 8)) | \
30 (0x04 << (MT_DEVICE_nGnRE * 8)) | \
31 (0x0c << (MT_DEVICE_GRE * 8)) | \
32 (0x44 << (MT_NORMAL_NC * 8)) | \
33 (0xffUL << (MT_NORMAL * 8)) | \
34 (0xbbUL << (MT_NORMAL_WT * 8)))
35
36/* More flags from user's perspective are supported using remaining bits
37 * of "attrs" field, i.e. attrs[31:3], underlying code will take care
38 * of setting PTE fields correctly.
39 *
40 * current usage of attrs[31:3] is:
41 * attrs[3] : Access Permissions
42 * attrs[4] : Memory access from secure/ns state
43 * attrs[5] : Execute Permissions privileged mode (PXN)
44 * attrs[6] : Execute Permissions unprivileged mode (UXN)
45 * attrs[7] : Mirror RO/RW permissions to EL0
46 * attrs[8] : Overwrite existing mapping if any
47 *
48 */
49#define MT_PERM_SHIFT 3U
50#define MT_SEC_SHIFT 4U
51#define MT_P_EXECUTE_SHIFT 5U
52#define MT_U_EXECUTE_SHIFT 6U
53#define MT_RW_AP_SHIFT 7U
54#define MT_NO_OVERWRITE_SHIFT 8U
55
56#define MT_RO (0U << MT_PERM_SHIFT)
57#define MT_RW (1U << MT_PERM_SHIFT)
58
59#define MT_RW_AP_ELx (1U << MT_RW_AP_SHIFT)
60#define MT_RW_AP_EL_HIGHER (0U << MT_RW_AP_SHIFT)
61
62#define MT_SECURE (0U << MT_SEC_SHIFT)
63#define MT_NS (1U << MT_SEC_SHIFT)
64
65#define MT_P_EXECUTE (0U << MT_P_EXECUTE_SHIFT)
66#define MT_P_EXECUTE_NEVER (1U << MT_P_EXECUTE_SHIFT)
67
68#define MT_U_EXECUTE (0U << MT_U_EXECUTE_SHIFT)
69#define MT_U_EXECUTE_NEVER (1U << MT_U_EXECUTE_SHIFT)
70
71#define MT_NO_OVERWRITE (1U << MT_NO_OVERWRITE_SHIFT)
72
73#define MT_P_RW_U_RW (MT_RW | MT_RW_AP_ELx | MT_P_EXECUTE_NEVER | MT_U_EXECUTE_NEVER)
74#define MT_P_RW_U_NA (MT_RW | MT_RW_AP_EL_HIGHER | MT_P_EXECUTE_NEVER | MT_U_EXECUTE_NEVER)
75#define MT_P_RO_U_RO (MT_RO | MT_RW_AP_ELx | MT_P_EXECUTE_NEVER | MT_U_EXECUTE_NEVER)
76#define MT_P_RO_U_NA (MT_RO | MT_RW_AP_EL_HIGHER | MT_P_EXECUTE_NEVER | MT_U_EXECUTE_NEVER)
77#define MT_P_RO_U_RX (MT_RO | MT_RW_AP_ELx | MT_P_EXECUTE_NEVER | MT_U_EXECUTE)
78#define MT_P_RX_U_RX (MT_RO | MT_RW_AP_ELx | MT_P_EXECUTE | MT_U_EXECUTE)
79#define MT_P_RX_U_NA (MT_RO | MT_RW_AP_EL_HIGHER | MT_P_EXECUTE | MT_U_EXECUTE_NEVER)
80
81#ifdef CONFIG_ARMV8_A_NS
82#define MT_DEFAULT_SECURE_STATE MT_NS
83#else
84#define MT_DEFAULT_SECURE_STATE MT_SECURE
85#endif
86
87/*
88 * PTE descriptor can be Block descriptor or Table descriptor
89 * or Page descriptor.
90 */
91#define PTE_DESC_TYPE_MASK 3U
92#define PTE_BLOCK_DESC 1U
93#define PTE_TABLE_DESC 3U
94#define PTE_PAGE_DESC 3U
95#define PTE_INVALID_DESC 0U
96
97/*
98 * Block and Page descriptor attributes fields
99 */
100#define PTE_BLOCK_DESC_MEMTYPE(x) (x << 2)
101#define PTE_BLOCK_DESC_NS (1ULL << 5)
102#define PTE_BLOCK_DESC_AP_ELx (1ULL << 6)
103#define PTE_BLOCK_DESC_AP_EL_HIGHER (0ULL << 6)
104#define PTE_BLOCK_DESC_AP_RO (1ULL << 7)
105#define PTE_BLOCK_DESC_AP_RW (0ULL << 7)
106#define PTE_BLOCK_DESC_NON_SHARE (0ULL << 8)
107#define PTE_BLOCK_DESC_OUTER_SHARE (2ULL << 8)
108#define PTE_BLOCK_DESC_INNER_SHARE (3ULL << 8)
109#define PTE_BLOCK_DESC_AF (1ULL << 10)
110#define PTE_BLOCK_DESC_NG (1ULL << 11)
111#define PTE_BLOCK_DESC_PXN (1ULL << 53)
112#define PTE_BLOCK_DESC_UXN (1ULL << 54)
113
114/*
115 * TCR definitions.
116 */
117#define TCR_EL1_IPS_SHIFT 32U
118#define TCR_EL2_PS_SHIFT 16U
119#define TCR_EL3_PS_SHIFT 16U
120
121#define TCR_T0SZ_SHIFT 0U
122#define TCR_T0SZ(x) ((64 - (x)) << TCR_T0SZ_SHIFT)
123
124#define TCR_IRGN_NC (0ULL << 8)
125#define TCR_IRGN_WBWA (1ULL << 8)
126#define TCR_IRGN_WT (2ULL << 8)
127#define TCR_IRGN_WBNWA (3ULL << 8)
128#define TCR_IRGN_MASK (3ULL << 8)
129#define TCR_ORGN_NC (0ULL << 10)
130#define TCR_ORGN_WBWA (1ULL << 10)
131#define TCR_ORGN_WT (2ULL << 10)
132#define TCR_ORGN_WBNWA (3ULL << 10)
133#define TCR_ORGN_MASK (3ULL << 10)
134#define TCR_SHARED_NON (0ULL << 12)
135#define TCR_SHARED_OUTER (2ULL << 12)
136#define TCR_SHARED_INNER (3ULL << 12)
137#define TCR_TG0_4K (0ULL << 14)
138#define TCR_TG0_64K (1ULL << 14)
139#define TCR_TG0_16K (2ULL << 14)
140#define TCR_EPD1_DISABLE (1ULL << 23)
141#define TCR_TG1_16K (1ULL << 30)
142#define TCR_TG1_4K (2ULL << 30)
143#define TCR_TG1_64K (3ULL << 30)
144
145#define TCR_PS_BITS_4GB 0x0ULL
146#define TCR_PS_BITS_64GB 0x1ULL
147#define TCR_PS_BITS_1TB 0x2ULL
148#define TCR_PS_BITS_4TB 0x3ULL
149#define TCR_PS_BITS_16TB 0x4ULL
150#define TCR_PS_BITS_256TB 0x5ULL
151
152#ifndef _ASMLANGUAGE
153
154/* Region definition data structure */
155struct arm_mmu_region {
156 /* Region Base Physical Address */
158 /* Region Base Virtual Address */
160 /* Region size */
161 size_t size;
162 /* Region Name */
163 const char *name;
164 /* Region Attributes */
166};
167
168/* MMU configuration data structure */
169struct arm_mmu_config {
170 /* Number of regions */
171 unsigned int num_regions;
172 /* Regions */
173 const struct arm_mmu_region *mmu_regions;
174};
175
178};
179
180/* Convenience macros to represent the ARMv8-A-specific
181 * configuration for memory access permission and
182 * cache-ability attribution.
183 */
184
185#define MMU_REGION_ENTRY(_name, _base_pa, _base_va, _size, _attrs) \
186 {\
187 .name = _name, \
188 .base_pa = _base_pa, \
189 .base_va = _base_va, \
190 .size = _size, \
191 .attrs = _attrs, \
192 }
193
194#define MMU_REGION_FLAT_ENTRY(name, adr, sz, attrs) \
195 MMU_REGION_ENTRY(name, adr, adr, sz, attrs)
196
197/* Kernel macros for memory attribution
198 * (access permissions and cache-ability).
199 *
200 * The macros are to be stored in k_mem_partition_attr_t
201 * objects. The format of a k_mem_partition_attr_t object
202 * is an uint32_t composed by permission and attribute flags
203 * located in include/arch/arm64/arm_mmu.h
204 */
205
206/* Read-Write access permission attributes */
207#define K_MEM_PARTITION_P_RW_U_RW ((k_mem_partition_attr_t) \
208 {MT_P_RW_U_RW})
209#define K_MEM_PARTITION_P_RW_U_NA ((k_mem_partition_attr_t) \
210 {MT_P_RW_U_NA})
211#define K_MEM_PARTITION_P_RO_U_RO ((k_mem_partition_attr_t) \
212 {MT_P_RO_U_RO})
213#define K_MEM_PARTITION_P_RO_U_NA ((k_mem_partition_attr_t) \
214 {MT_P_RO_U_NA})
215/* Execution-allowed attributes */
216#define K_MEM_PARTITION_P_RX_U_RX ((k_mem_partition_attr_t) \
217 {MT_P_RX_U_RX})
218/* Typedef for the k_mem_partition attribute */
220
221/* Reference to the MMU configuration.
222 *
223 * This struct is defined and populated for each SoC (in the SoC definition),
224 * and holds the build-time configuration information for the fixed MMU
225 * regions enabled during kernel initialization.
226 */
227extern const struct arm_mmu_config mmu_config;
228
229#endif /* _ASMLANGUAGE */
230
231#endif /* ZEPHYR_INCLUDE_ARCH_ARM64_ARM_MMU_H_ */
const struct arm_mmu_config mmu_config
uint32_t k_mem_partition_attr_t
Definition: arch.h:220
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:105
Definition: arm_mmu.h:85
const struct arm_mmu_region * mmu_regions
Definition: arm_mmu.h:89
unsigned int num_regions
Definition: arm_mmu.h:171
Definition: arm_mmu.h:176
uint64_t * base_xlat_table
Definition: arm_mmu.h:177
Definition: arm_mmu.h:71
uintptr_t base_va
Definition: arm_mmu.h:75
size_t size
Definition: arm_mmu.h:77
uintptr_t base_pa
Definition: arm_mmu.h:73
const char * name
Definition: arm_mmu.h:79
uint32_t attrs
Definition: arm_mmu.h:81
uint32_t attrs
Definition: arm_mmu.h:219