xlat_tables_v2_helpers.h 6.64 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
9
10
11
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/*
 * This header file contains internal definitions that are not supposed to be
 * used outside of this library code.
 */

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
12
13
#ifndef XLAT_TABLES_V2_HELPERS_H
#define XLAT_TABLES_V2_HELPERS_H
14

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
15
#ifndef XLAT_TABLES_V2_H
16
17
18
#error "Do not include this header file directly. Include xlat_tables_v2.h instead."
#endif

19
#ifndef __ASSEMBLER__
20

21
#include <stdbool.h>
22
#include <stddef.h>
23
24
25
26
27
28

#include <platform_def.h>

#include <lib/cassert.h>
#include <lib/xlat_tables/xlat_tables_arch.h>
#include <lib/xlat_tables/xlat_tables_defs.h>
29
30
31
32

/* Forward declaration */
struct mmap_region;

33
34
35
36
37
/*
 * Helper macro to define an mmap_region_t.  This macro allows to specify all
 * the fields of the structure but its parameter list is not guaranteed to
 * remain stable as we add members to mmap_region_t.
 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
38
#define MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr)		\
39
40
41
42
43
44
45
46
	{							\
		.base_pa = (_pa),				\
		.base_va = (_va),				\
		.size = (_sz),					\
		.attr = (_attr),				\
		.granularity = (_gr),				\
	}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Struct that holds all information about the translation tables. */
struct xlat_ctx {
	/*
	 * Max allowed Virtual and Physical Addresses.
	 */
	unsigned long long pa_max_address;
	uintptr_t va_max_address;

	/*
	 * Array of all memory regions stored in order of ascending end address
	 * and ascending size to simplify the code that allows overlapping
	 * regions. The list is terminated by the first entry with size == 0.
	 * The max size of the list is stored in `mmap_num`. `mmap` points to an
	 * array of mmap_num + 1 elements, so that there is space for the final
	 * null entry.
	 */
	struct mmap_region *mmap;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
64
	int mmap_num;
65
66
67
68
69
70
71

	/*
	 * Array of finer-grain translation tables.
	 * For example, if the initial lookup level is 1 then this array would
	 * contain both level-2 and level-3 entries.
	 */
	uint64_t (*tables)[XLAT_TABLE_ENTRIES];
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
72
	int tables_num;
73
74
75
#if PLAT_RO_XLAT_TABLES
	bool readonly_tables;
#endif
76
77
78
79
80
81
82
83
	/*
	 * Keep track of how many regions are mapped in each table. The base
	 * table can't be unmapped so it isn't needed to keep track of it.
	 */
#if PLAT_XLAT_TABLES_DYNAMIC
	int *tables_mapped_regions;
#endif /* PLAT_XLAT_TABLES_DYNAMIC */

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
84
	int next_table;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

	/*
	 * Base translation table. It doesn't need to have the same amount of
	 * entries as the ones used for other levels.
	 */
	uint64_t *base_table;
	unsigned int base_table_entries;

	/*
	* Max Physical and Virtual addresses currently in use by the
	* translation tables. These might get updated as we map/unmap memory
	* regions but they will never go beyond pa/va_max_address.
	*/
	unsigned long long max_pa;
	uintptr_t max_va;

	/* Level of the base translation table. */
	unsigned int base_level;

104
105
	/* Set to true when the translation tables are initialized. */
	bool initialized;
106
107

	/*
108
109
	 * Translation regime managed by this xlat_ctx_t. It should be one of
	 * the EL*_REGIME defines.
110
	 */
111
	int xlat_regime;
112
113
114
};

#if PLAT_XLAT_TABLES_DYNAMIC
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
115
#define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
116
117
	static int _ctx_name##_mapped_regions[_xlat_tables_count];

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
118
#define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)				\
119
120
	.tables_mapped_regions = _ctx_name##_mapped_regions,
#else
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
121
#define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
122
123
	/* do nothing */

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
124
#define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)				\
125
126
127
	/* do nothing */
#endif /* PLAT_XLAT_TABLES_DYNAMIC */

128
129
130
131
132
133
134
135
#if PLAT_RO_XLAT_TABLES
#define XLAT_CTX_INIT_TABLE_ATTR()					\
	.readonly_tables = false,
#else
#define XLAT_CTX_INIT_TABLE_ATTR()
	/* do nothing */
#endif

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count,		\
			_xlat_tables_count, _virt_addr_space_size,	\
			_phy_addr_space_size, _xlat_regime, _section_name)\
	CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size),	\
		assert_invalid_physical_addr_space_sizefor_##_ctx_name);\
									\
	static mmap_region_t _ctx_name##_mmap[_mmap_count + 1];		\
									\
	static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count]	\
		[XLAT_TABLE_ENTRIES]					\
		__aligned(XLAT_TABLE_SIZE) __section(_section_name);	\
									\
	static uint64_t _ctx_name##_base_xlat_table			\
		[GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)]	\
		__aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)\
			* sizeof(uint64_t));				\
									\
	XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
									\
	static xlat_ctx_t _ctx_name##_xlat_ctx = {			\
		.pa_max_address = (_phy_addr_space_size) - 1ULL,	\
157
		.va_max_address = (_virt_addr_space_size) - 1UL,	\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
158
159
		.mmap = _ctx_name##_mmap,				\
		.mmap_num = (_mmap_count),				\
160
161
162
163
164
		.tables = _ctx_name##_xlat_tables,			\
		.tables_num = _xlat_tables_count,			\
		 XLAT_CTX_INIT_TABLE_ATTR()				\
		 XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)			\
		.next_table = 0,					\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
165
166
167
		.base_table = _ctx_name##_base_xlat_table,		\
		.base_table_entries =					\
			GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size),\
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
		.max_pa = 0U,						\
		.max_va = 0U,						\
		.base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),\
		.initialized = false,					\
		.xlat_regime = (_xlat_regime)				\
	}

#define REGISTER_XLAT_CONTEXT_RO_BASE_TABLE(_ctx_name, _mmap_count,	\
			_xlat_tables_count, _virt_addr_space_size,	\
			_phy_addr_space_size, _xlat_regime, _section_name)\
	CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size),	\
		assert_invalid_physical_addr_space_sizefor_##_ctx_name);\
									\
	static mmap_region_t _ctx_name##_mmap[_mmap_count + 1];		\
									\
	static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count]	\
		[XLAT_TABLE_ENTRIES]					\
		__aligned(XLAT_TABLE_SIZE) __section(_section_name);	\
									\
	static uint64_t _ctx_name##_base_xlat_table			\
		[GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)]	\
		__aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)\
			* sizeof(uint64_t))				\
		__section(".rodata");					\
									\
	XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
									\
	static xlat_ctx_t _ctx_name##_xlat_ctx = {			\
		.pa_max_address = (_phy_addr_space_size) - 1ULL,	\
		.va_max_address = (_virt_addr_space_size) - 1UL,	\
		.mmap = _ctx_name##_mmap,				\
		.mmap_num = (_mmap_count),				\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
200
201
		.tables = _ctx_name##_xlat_tables,			\
		.tables_num = _xlat_tables_count,			\
202
		 XLAT_CTX_INIT_TABLE_ATTR()				\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
203
		 XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)			\
204
205
206
207
		.next_table = 0,					\
		.base_table = _ctx_name##_base_xlat_table,		\
		.base_table_entries =					\
			GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size),\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
208
209
		.max_pa = 0U,						\
		.max_va = 0U,						\
210
		.base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),\
211
		.initialized = false,					\
212
		.xlat_regime = (_xlat_regime)				\
213
214
	}

215
#endif /*__ASSEMBLER__*/
216

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
217
#endif /* XLAT_TABLES_V2_HELPERS_H */