xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2_helpers.h (revision 6563c0beb8b64168debba5e6ea40719fc810796c)
1 /*
2  * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * This header file contains internal definitions that are not supposed to be
9  * used outside of this library code.
10  */
11 
12 #ifndef __XLAT_TABLES_V2_HELPERS_H__
13 #define __XLAT_TABLES_V2_HELPERS_H__
14 
15 #ifndef __XLAT_TABLES_V2_H__
16 #error "Do not include this header file directly. Include xlat_tables_v2.h instead."
17 #endif
18 
19 /* Offsets into mmu_cfg_params array. All parameters are 64 bits wide. */
20 #define MMU_CFG_MAIR		0
21 #define MMU_CFG_TCR		1
22 #define MMU_CFG_TTBR0		2
23 #define MMU_CFG_PARAM_MAX	3
24 
25 #ifndef __ASSEMBLY__
26 
27 #include <cassert.h>
28 #include <platform_def.h>
29 #include <stddef.h>
30 #include <xlat_tables_arch.h>
31 #include <xlat_tables_defs.h>
32 
33 /* Parameters of register values required when enabling MMU */
34 extern uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
35 
36 /* Forward declaration */
37 struct mmap_region;
38 
39 /*
40  * Helper macro to define an mmap_region_t.  This macro allows to specify all
41  * the fields of the structure but its parameter list is not guaranteed to
42  * remain stable as we add members to mmap_region_t.
43  */
44 #define _MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr)	\
45 	{							\
46 		.base_pa = (_pa),				\
47 		.base_va = (_va),				\
48 		.size = (_sz),					\
49 		.attr = (_attr),				\
50 		.granularity = (_gr),				\
51 	}
52 
53 /* Struct that holds all information about the translation tables. */
54 struct xlat_ctx {
55 	/*
56 	 * Max allowed Virtual and Physical Addresses.
57 	 */
58 	unsigned long long pa_max_address;
59 	uintptr_t va_max_address;
60 
61 	/*
62 	 * Array of all memory regions stored in order of ascending end address
63 	 * and ascending size to simplify the code that allows overlapping
64 	 * regions. The list is terminated by the first entry with size == 0.
65 	 * The max size of the list is stored in `mmap_num`. `mmap` points to an
66 	 * array of mmap_num + 1 elements, so that there is space for the final
67 	 * null entry.
68 	 */
69 	struct mmap_region *mmap;
70 	unsigned int mmap_num;
71 
72 	/*
73 	 * Array of finer-grain translation tables.
74 	 * For example, if the initial lookup level is 1 then this array would
75 	 * contain both level-2 and level-3 entries.
76 	 */
77 	uint64_t (*tables)[XLAT_TABLE_ENTRIES];
78 	unsigned int tables_num;
79 	/*
80 	 * Keep track of how many regions are mapped in each table. The base
81 	 * table can't be unmapped so it isn't needed to keep track of it.
82 	 */
83 #if PLAT_XLAT_TABLES_DYNAMIC
84 	int *tables_mapped_regions;
85 #endif /* PLAT_XLAT_TABLES_DYNAMIC */
86 
87 	unsigned int next_table;
88 
89 	/*
90 	 * Base translation table. It doesn't need to have the same amount of
91 	 * entries as the ones used for other levels.
92 	 */
93 	uint64_t *base_table;
94 	unsigned int base_table_entries;
95 
96 	/*
97 	* Max Physical and Virtual addresses currently in use by the
98 	* translation tables. These might get updated as we map/unmap memory
99 	* regions but they will never go beyond pa/va_max_address.
100 	*/
101 	unsigned long long max_pa;
102 	uintptr_t max_va;
103 
104 	/* Level of the base translation table. */
105 	unsigned int base_level;
106 
107 	/* Set to 1 when the translation tables are initialized. */
108 	unsigned int initialized;
109 
110 	/*
111 	 * Translation regime managed by this xlat_ctx_t. It should be one of
112 	 * the EL*_REGIME defines.
113 	 */
114 	int xlat_regime;
115 };
116 
117 #if PLAT_XLAT_TABLES_DYNAMIC
118 #define _ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
119 	static int _ctx_name##_mapped_regions[_xlat_tables_count];
120 
121 #define _REGISTER_DYNMAP_STRUCT(_ctx_name)				\
122 	.tables_mapped_regions = _ctx_name##_mapped_regions,
123 #else
124 #define _ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
125 	/* do nothing */
126 
127 #define _REGISTER_DYNMAP_STRUCT(_ctx_name)				\
128 	/* do nothing */
129 #endif /* PLAT_XLAT_TABLES_DYNAMIC */
130 
131 #define _REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count, _xlat_tables_count,	\
132 			_virt_addr_space_size, _phy_addr_space_size,		\
133 			_xlat_regime, _section_name)				\
134 	CASSERT(CHECK_VIRT_ADDR_SPACE_SIZE(_virt_addr_space_size),		\
135 		assert_invalid_virtual_addr_space_size_for_##_ctx_name);	\
136 										\
137 	CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size),		\
138 		assert_invalid_physical_addr_space_sizefor_##_ctx_name);	\
139 										\
140 	static mmap_region_t _ctx_name##_mmap[_mmap_count + 1];			\
141 										\
142 	static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count]		\
143 		[XLAT_TABLE_ENTRIES]						\
144 		__aligned(XLAT_TABLE_SIZE) __section(_section_name);		\
145 										\
146 	static uint64_t _ctx_name##_base_xlat_table				\
147 		[GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)]		\
148 		__aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)	\
149 			* sizeof(uint64_t));					\
150 										\
151 	_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)			\
152 										\
153 	static xlat_ctx_t _ctx_name##_xlat_ctx = {				\
154 		.va_max_address = (_virt_addr_space_size) - 1,			\
155 		.pa_max_address = (_phy_addr_space_size) - 1,			\
156 		.mmap = _ctx_name##_mmap,					\
157 		.mmap_num = (_mmap_count),					\
158 		.base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),	\
159 		.base_table = _ctx_name##_base_xlat_table,			\
160 		.base_table_entries =						\
161 			GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size),	\
162 		.tables = _ctx_name##_xlat_tables,				\
163 		.tables_num = _xlat_tables_count,				\
164 		 _REGISTER_DYNMAP_STRUCT(_ctx_name)				\
165 		.xlat_regime = (_xlat_regime),					\
166 		.max_pa = 0,							\
167 		.max_va = 0,							\
168 		.next_table = 0,						\
169 		.initialized = 0,						\
170 	}
171 
172 #endif /*__ASSEMBLY__*/
173 
174 #endif /* __XLAT_TABLES_V2_HELPERS_H__ */
175