blob: d3d2fc4ed515500d1965fc2f130383667def0811 [file] [log] [blame]
Sandrine Bailleux3120ea22017-07-10 13:37:48 +01001/*
Jeenu Viswambharan58e81482018-04-27 15:06:57 +01002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Sandrine Bailleux3120ea22017-07-10 13:37:48 +01003 *
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
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010012#ifndef XLAT_TABLES_V2_HELPERS_H
13#define XLAT_TABLES_V2_HELPERS_H
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010014
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010015#ifndef XLAT_TABLES_V2_H
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010016#error "Do not include this header file directly. Include xlat_tables_v2.h instead."
17#endif
18
19#ifndef __ASSEMBLY__
20
21#include <cassert.h>
22#include <platform_def.h>
23#include <stddef.h>
24#include <xlat_tables_arch.h>
25#include <xlat_tables_defs.h>
26
27/* Forward declaration */
28struct mmap_region;
29
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +010030/*
31 * Helper macro to define an mmap_region_t. This macro allows to specify all
32 * the fields of the structure but its parameter list is not guaranteed to
33 * remain stable as we add members to mmap_region_t.
34 */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010035#define MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr) \
Sandrine Bailleux8f23fa82017-09-28 21:58:12 +010036 { \
37 .base_pa = (_pa), \
38 .base_va = (_va), \
39 .size = (_sz), \
40 .attr = (_attr), \
41 .granularity = (_gr), \
42 }
43
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010044/* Struct that holds all information about the translation tables. */
45struct xlat_ctx {
46 /*
47 * Max allowed Virtual and Physical Addresses.
48 */
49 unsigned long long pa_max_address;
50 uintptr_t va_max_address;
51
52 /*
53 * Array of all memory regions stored in order of ascending end address
54 * and ascending size to simplify the code that allows overlapping
55 * regions. The list is terminated by the first entry with size == 0.
56 * The max size of the list is stored in `mmap_num`. `mmap` points to an
57 * array of mmap_num + 1 elements, so that there is space for the final
58 * null entry.
59 */
60 struct mmap_region *mmap;
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010061 int mmap_num;
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010062
63 /*
64 * Array of finer-grain translation tables.
65 * For example, if the initial lookup level is 1 then this array would
66 * contain both level-2 and level-3 entries.
67 */
68 uint64_t (*tables)[XLAT_TABLE_ENTRIES];
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010069 int tables_num;
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010070 /*
71 * Keep track of how many regions are mapped in each table. The base
72 * table can't be unmapped so it isn't needed to keep track of it.
73 */
74#if PLAT_XLAT_TABLES_DYNAMIC
75 int *tables_mapped_regions;
76#endif /* PLAT_XLAT_TABLES_DYNAMIC */
77
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010078 int next_table;
Sandrine Bailleux3120ea22017-07-10 13:37:48 +010079
80 /*
81 * Base translation table. It doesn't need to have the same amount of
82 * entries as the ones used for other levels.
83 */
84 uint64_t *base_table;
85 unsigned int base_table_entries;
86
87 /*
88 * Max Physical and Virtual addresses currently in use by the
89 * translation tables. These might get updated as we map/unmap memory
90 * regions but they will never go beyond pa/va_max_address.
91 */
92 unsigned long long max_pa;
93 uintptr_t max_va;
94
95 /* Level of the base translation table. */
96 unsigned int base_level;
97
98 /* Set to 1 when the translation tables are initialized. */
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +010099 int initialized;
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100100
101 /*
Antonio Nino Diazf1b84f62018-07-03 11:58:49 +0100102 * Translation regime managed by this xlat_ctx_t. It should be one of
103 * the EL*_REGIME defines.
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100104 */
Antonio Nino Diazdcf9d922017-10-04 16:52:15 +0100105 int xlat_regime;
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100106};
107
108#if PLAT_XLAT_TABLES_DYNAMIC
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100109#define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100110 static int _ctx_name##_mapped_regions[_xlat_tables_count];
111
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100112#define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100113 .tables_mapped_regions = _ctx_name##_mapped_regions,
114#else
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100115#define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100116 /* do nothing */
117
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100118#define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100119 /* do nothing */
120#endif /* PLAT_XLAT_TABLES_DYNAMIC */
121
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100122#define REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count, \
123 _xlat_tables_count, _virt_addr_space_size, \
124 _phy_addr_space_size, _xlat_regime, _section_name)\
125 CASSERT(CHECK_VIRT_ADDR_SPACE_SIZE(_virt_addr_space_size), \
126 assert_invalid_virtual_addr_space_size_for_##_ctx_name);\
127 \
128 CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size), \
129 assert_invalid_physical_addr_space_sizefor_##_ctx_name);\
130 \
131 static mmap_region_t _ctx_name##_mmap[_mmap_count + 1]; \
132 \
133 static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count] \
134 [XLAT_TABLE_ENTRIES] \
135 __aligned(XLAT_TABLE_SIZE) __section(_section_name); \
136 \
137 static uint64_t _ctx_name##_base_xlat_table \
138 [GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)] \
139 __aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)\
140 * sizeof(uint64_t)); \
141 \
142 XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \
143 \
144 static xlat_ctx_t _ctx_name##_xlat_ctx = { \
145 .va_max_address = (_virt_addr_space_size) - 1UL, \
146 .pa_max_address = (_phy_addr_space_size) - 1ULL, \
147 .mmap = _ctx_name##_mmap, \
148 .mmap_num = (_mmap_count), \
149 .base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),\
150 .base_table = _ctx_name##_base_xlat_table, \
151 .base_table_entries = \
152 GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size),\
153 .tables = _ctx_name##_xlat_tables, \
154 .tables_num = _xlat_tables_count, \
155 XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \
156 .xlat_regime = (_xlat_regime), \
157 .max_pa = 0U, \
158 .max_va = 0U, \
159 .next_table = 0, \
160 .initialized = 0, \
Sandrine Bailleux3120ea22017-07-10 13:37:48 +0100161 }
162
Jeenu Viswambharan58e81482018-04-27 15:06:57 +0100163#endif /*__ASSEMBLY__*/
164
Antonio Nino Diaz50eb3742018-07-24 10:20:53 +0100165#endif /* XLAT_TABLES_V2_HELPERS_H */