xref: /rk3399_ARM-atf/include/lib/xlat_tables/xlat_tables_v2_helpers.h (revision 72e8f2456af54b75a0a1d92aadfce0b4bcde6ba1)
155c84964SSandrine Bailleux /*
2*4c700c15SGovindraj Raja  * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
355c84964SSandrine Bailleux  *
455c84964SSandrine Bailleux  * SPDX-License-Identifier: BSD-3-Clause
555c84964SSandrine Bailleux  */
655c84964SSandrine Bailleux 
755c84964SSandrine Bailleux /*
855c84964SSandrine Bailleux  * This header file contains internal definitions that are not supposed to be
955c84964SSandrine Bailleux  * used outside of this library code.
1055c84964SSandrine Bailleux  */
1155c84964SSandrine Bailleux 
12e7b9886cSAntonio Nino Diaz #ifndef XLAT_TABLES_V2_HELPERS_H
13e7b9886cSAntonio Nino Diaz #define XLAT_TABLES_V2_HELPERS_H
1455c84964SSandrine Bailleux 
15e7b9886cSAntonio Nino Diaz #ifndef XLAT_TABLES_V2_H
1655c84964SSandrine Bailleux #error "Do not include this header file directly. Include xlat_tables_v2.h instead."
1755c84964SSandrine Bailleux #endif
1855c84964SSandrine Bailleux 
19d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
2055c84964SSandrine Bailleux 
215b395e37SAntonio Nino Diaz #include <stdbool.h>
2255c84964SSandrine Bailleux #include <stddef.h>
2309d40e0eSAntonio Nino Diaz 
2409d40e0eSAntonio Nino Diaz #include <platform_def.h>
2509d40e0eSAntonio Nino Diaz 
2609d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
27e2822458SMasahiro Yamada #include <lib/utils_def.h>
2809d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_arch.h>
2909d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_defs.h>
3055c84964SSandrine Bailleux 
3155c84964SSandrine Bailleux /* Forward declaration */
3255c84964SSandrine Bailleux struct mmap_region;
3355c84964SSandrine Bailleux 
34fdb1964cSSandrine Bailleux /*
35fdb1964cSSandrine Bailleux  * Helper macro to define an mmap_region_t.  This macro allows to specify all
36fdb1964cSSandrine Bailleux  * the fields of the structure but its parameter list is not guaranteed to
37fdb1964cSSandrine Bailleux  * remain stable as we add members to mmap_region_t.
38fdb1964cSSandrine Bailleux  */
39e7b9886cSAntonio Nino Diaz #define MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr)		\
40fdb1964cSSandrine Bailleux 	{							\
41fdb1964cSSandrine Bailleux 		.base_pa = (_pa),				\
42fdb1964cSSandrine Bailleux 		.base_va = (_va),				\
43fdb1964cSSandrine Bailleux 		.size = (_sz),					\
44fdb1964cSSandrine Bailleux 		.attr = (_attr),				\
45fdb1964cSSandrine Bailleux 		.granularity = (_gr),				\
46fdb1964cSSandrine Bailleux 	}
47fdb1964cSSandrine Bailleux 
4855c84964SSandrine Bailleux /* Struct that holds all information about the translation tables. */
4955c84964SSandrine Bailleux struct xlat_ctx {
5055c84964SSandrine Bailleux 	/*
5155c84964SSandrine Bailleux 	 * Max allowed Virtual and Physical Addresses.
5255c84964SSandrine Bailleux 	 */
5355c84964SSandrine Bailleux 	unsigned long long pa_max_address;
5455c84964SSandrine Bailleux 	uintptr_t va_max_address;
5555c84964SSandrine Bailleux 
5655c84964SSandrine Bailleux 	/*
5755c84964SSandrine Bailleux 	 * Array of all memory regions stored in order of ascending end address
5855c84964SSandrine Bailleux 	 * and ascending size to simplify the code that allows overlapping
5955c84964SSandrine Bailleux 	 * regions. The list is terminated by the first entry with size == 0.
6055c84964SSandrine Bailleux 	 * The max size of the list is stored in `mmap_num`. `mmap` points to an
6155c84964SSandrine Bailleux 	 * array of mmap_num + 1 elements, so that there is space for the final
6255c84964SSandrine Bailleux 	 * null entry.
6355c84964SSandrine Bailleux 	 */
6455c84964SSandrine Bailleux 	struct mmap_region *mmap;
65e7b9886cSAntonio Nino Diaz 	int mmap_num;
6655c84964SSandrine Bailleux 
6755c84964SSandrine Bailleux 	/*
6855c84964SSandrine Bailleux 	 * Array of finer-grain translation tables.
6955c84964SSandrine Bailleux 	 * For example, if the initial lookup level is 1 then this array would
7055c84964SSandrine Bailleux 	 * contain both level-2 and level-3 entries.
7155c84964SSandrine Bailleux 	 */
7255c84964SSandrine Bailleux 	uint64_t (*tables)[XLAT_TABLE_ENTRIES];
73e7b9886cSAntonio Nino Diaz 	int tables_num;
7460e8f3cfSPetre-Ionut Tudor #if PLAT_RO_XLAT_TABLES
7560e8f3cfSPetre-Ionut Tudor 	bool readonly_tables;
7660e8f3cfSPetre-Ionut Tudor #endif
7755c84964SSandrine Bailleux 	/*
7855c84964SSandrine Bailleux 	 * Keep track of how many regions are mapped in each table. The base
7955c84964SSandrine Bailleux 	 * table can't be unmapped so it isn't needed to keep track of it.
8055c84964SSandrine Bailleux 	 */
8155c84964SSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC
8255c84964SSandrine Bailleux 	int *tables_mapped_regions;
8355c84964SSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */
8455c84964SSandrine Bailleux 
85e7b9886cSAntonio Nino Diaz 	int next_table;
8655c84964SSandrine Bailleux 
8755c84964SSandrine Bailleux 	/*
8855c84964SSandrine Bailleux 	 * Base translation table. It doesn't need to have the same amount of
8955c84964SSandrine Bailleux 	 * entries as the ones used for other levels.
9055c84964SSandrine Bailleux 	 */
9155c84964SSandrine Bailleux 	uint64_t *base_table;
9255c84964SSandrine Bailleux 	unsigned int base_table_entries;
9355c84964SSandrine Bailleux 
9455c84964SSandrine Bailleux 	/*
9555c84964SSandrine Bailleux 	* Max Physical and Virtual addresses currently in use by the
9655c84964SSandrine Bailleux 	* translation tables. These might get updated as we map/unmap memory
9755c84964SSandrine Bailleux 	* regions but they will never go beyond pa/va_max_address.
9855c84964SSandrine Bailleux 	*/
9955c84964SSandrine Bailleux 	unsigned long long max_pa;
10055c84964SSandrine Bailleux 	uintptr_t max_va;
10155c84964SSandrine Bailleux 
10255c84964SSandrine Bailleux 	/* Level of the base translation table. */
10355c84964SSandrine Bailleux 	unsigned int base_level;
10455c84964SSandrine Bailleux 
1055b395e37SAntonio Nino Diaz 	/* Set to true when the translation tables are initialized. */
1065b395e37SAntonio Nino Diaz 	bool initialized;
10755c84964SSandrine Bailleux 
10855c84964SSandrine Bailleux 	/*
109fd2299e6SAntonio Nino Diaz 	 * Translation regime managed by this xlat_ctx_t. It should be one of
110fd2299e6SAntonio Nino Diaz 	 * the EL*_REGIME defines.
11155c84964SSandrine Bailleux 	 */
112609c9191SAntonio Nino Diaz 	int xlat_regime;
11355c84964SSandrine Bailleux };
11455c84964SSandrine Bailleux 
11555c84964SSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC
116e7b9886cSAntonio Nino Diaz #define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
11755c84964SSandrine Bailleux 	static int _ctx_name##_mapped_regions[_xlat_tables_count];
11855c84964SSandrine Bailleux 
119e7b9886cSAntonio Nino Diaz #define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)				\
12055c84964SSandrine Bailleux 	.tables_mapped_regions = _ctx_name##_mapped_regions,
12155c84964SSandrine Bailleux #else
122e7b9886cSAntonio Nino Diaz #define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
12355c84964SSandrine Bailleux 	/* do nothing */
12455c84964SSandrine Bailleux 
125e7b9886cSAntonio Nino Diaz #define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)				\
12655c84964SSandrine Bailleux 	/* do nothing */
12755c84964SSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */
12855c84964SSandrine Bailleux 
12960e8f3cfSPetre-Ionut Tudor #if PLAT_RO_XLAT_TABLES
13060e8f3cfSPetre-Ionut Tudor #define XLAT_CTX_INIT_TABLE_ATTR()					\
13160e8f3cfSPetre-Ionut Tudor 	.readonly_tables = false,
13260e8f3cfSPetre-Ionut Tudor #else
13360e8f3cfSPetre-Ionut Tudor #define XLAT_CTX_INIT_TABLE_ATTR()
13460e8f3cfSPetre-Ionut Tudor 	/* do nothing */
13560e8f3cfSPetre-Ionut Tudor #endif
13660e8f3cfSPetre-Ionut Tudor 
137e7b9886cSAntonio Nino Diaz #define REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count,		\
138e7b9886cSAntonio Nino Diaz 			_xlat_tables_count, _virt_addr_space_size,	\
139363830dfSMasahiro Yamada 			_phy_addr_space_size, _xlat_regime,		\
140363830dfSMasahiro Yamada 			_table_section, _base_table_section)		\
14155c84964SSandrine Bailleux 	CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size),	\
14255c84964SSandrine Bailleux 		assert_invalid_physical_addr_space_sizefor_##_ctx_name);\
14355c84964SSandrine Bailleux 									\
14455c84964SSandrine Bailleux 	static mmap_region_t _ctx_name##_mmap[_mmap_count + 1];		\
14555c84964SSandrine Bailleux 									\
14655c84964SSandrine Bailleux 	static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count]	\
14755c84964SSandrine Bailleux 		[XLAT_TABLE_ENTRIES]					\
148363830dfSMasahiro Yamada 		__aligned(XLAT_TABLE_SIZE) __section(_table_section);	\
14960e8f3cfSPetre-Ionut Tudor 									\
15060e8f3cfSPetre-Ionut Tudor 	static uint64_t _ctx_name##_base_xlat_table			\
15160e8f3cfSPetre-Ionut Tudor 		[GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)]	\
15260e8f3cfSPetre-Ionut Tudor 		__aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)\
15360e8f3cfSPetre-Ionut Tudor 			* sizeof(uint64_t))				\
154363830dfSMasahiro Yamada 		__section(_base_table_section);				\
15560e8f3cfSPetre-Ionut Tudor 									\
15660e8f3cfSPetre-Ionut Tudor 	XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count)		\
15760e8f3cfSPetre-Ionut Tudor 									\
15860e8f3cfSPetre-Ionut Tudor 	static xlat_ctx_t _ctx_name##_xlat_ctx = {			\
15960e8f3cfSPetre-Ionut Tudor 		.pa_max_address = (_phy_addr_space_size) - 1ULL,	\
16060e8f3cfSPetre-Ionut Tudor 		.va_max_address = (_virt_addr_space_size) - 1UL,	\
16160e8f3cfSPetre-Ionut Tudor 		.mmap = _ctx_name##_mmap,				\
16260e8f3cfSPetre-Ionut Tudor 		.mmap_num = (_mmap_count),				\
16360e8f3cfSPetre-Ionut Tudor 		.tables = _ctx_name##_xlat_tables,			\
164e2822458SMasahiro Yamada 		.tables_num = ARRAY_SIZE(_ctx_name##_xlat_tables),	\
16560e8f3cfSPetre-Ionut Tudor 		 XLAT_CTX_INIT_TABLE_ATTR()				\
16660e8f3cfSPetre-Ionut Tudor 		 XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name)			\
16760e8f3cfSPetre-Ionut Tudor 		.next_table = 0,					\
16860e8f3cfSPetre-Ionut Tudor 		.base_table = _ctx_name##_base_xlat_table,		\
16960e8f3cfSPetre-Ionut Tudor 		.base_table_entries =					\
170e2822458SMasahiro Yamada 			ARRAY_SIZE(_ctx_name##_base_xlat_table),	\
17160e8f3cfSPetre-Ionut Tudor 		.max_pa = 0U,						\
17260e8f3cfSPetre-Ionut Tudor 		.max_va = 0U,						\
17360e8f3cfSPetre-Ionut Tudor 		.base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),\
17460e8f3cfSPetre-Ionut Tudor 		.initialized = false,					\
17560e8f3cfSPetre-Ionut Tudor 		.xlat_regime = (_xlat_regime)				\
17655c84964SSandrine Bailleux 	}
17755c84964SSandrine Bailleux 
178d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/
1790cc7aa89SJeenu Viswambharan 
180e7b9886cSAntonio Nino Diaz #endif /* XLAT_TABLES_V2_HELPERS_H */
181