Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 1 | /* |
Govindraj Raja | eee28e7 | 2023-08-01 15:52:40 -0500 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved. |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 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 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 12 | #ifndef XLAT_TABLES_V2_HELPERS_H |
| 13 | #define XLAT_TABLES_V2_HELPERS_H |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 14 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 15 | #ifndef XLAT_TABLES_V2_H |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 16 | #error "Do not include this header file directly. Include xlat_tables_v2.h instead." |
| 17 | #endif |
| 18 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 19 | #ifndef __ASSEMBLER__ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 20 | |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 21 | #include <stdbool.h> |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 22 | #include <stddef.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 23 | |
| 24 | #include <platform_def.h> |
| 25 | |
| 26 | #include <lib/cassert.h> |
Masahiro Yamada | b042060 | 2020-03-09 17:39:27 +0900 | [diff] [blame] | 27 | #include <lib/utils_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 28 | #include <lib/xlat_tables/xlat_tables_arch.h> |
| 29 | #include <lib/xlat_tables/xlat_tables_defs.h> |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 30 | |
| 31 | /* Forward declaration */ |
| 32 | struct mmap_region; |
| 33 | |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 34 | /* |
| 35 | * Helper macro to define an mmap_region_t. This macro allows to specify all |
| 36 | * the fields of the structure but its parameter list is not guaranteed to |
| 37 | * remain stable as we add members to mmap_region_t. |
| 38 | */ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 39 | #define MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr) \ |
Sandrine Bailleux | 8f23fa8 | 2017-09-28 21:58:12 +0100 | [diff] [blame] | 40 | { \ |
| 41 | .base_pa = (_pa), \ |
| 42 | .base_va = (_va), \ |
| 43 | .size = (_sz), \ |
| 44 | .attr = (_attr), \ |
| 45 | .granularity = (_gr), \ |
| 46 | } |
| 47 | |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 48 | /* Struct that holds all information about the translation tables. */ |
| 49 | struct xlat_ctx { |
| 50 | /* |
| 51 | * Max allowed Virtual and Physical Addresses. |
| 52 | */ |
| 53 | unsigned long long pa_max_address; |
| 54 | uintptr_t va_max_address; |
| 55 | |
| 56 | /* |
| 57 | * Array of all memory regions stored in order of ascending end address |
| 58 | * and ascending size to simplify the code that allows overlapping |
| 59 | * regions. The list is terminated by the first entry with size == 0. |
| 60 | * The max size of the list is stored in `mmap_num`. `mmap` points to an |
| 61 | * array of mmap_num + 1 elements, so that there is space for the final |
| 62 | * null entry. |
| 63 | */ |
| 64 | struct mmap_region *mmap; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 65 | int mmap_num; |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Array of finer-grain translation tables. |
| 69 | * For example, if the initial lookup level is 1 then this array would |
| 70 | * contain both level-2 and level-3 entries. |
| 71 | */ |
| 72 | uint64_t (*tables)[XLAT_TABLE_ENTRIES]; |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 73 | int tables_num; |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 74 | #if PLAT_RO_XLAT_TABLES |
| 75 | bool readonly_tables; |
| 76 | #endif |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 77 | /* |
| 78 | * Keep track of how many regions are mapped in each table. The base |
| 79 | * table can't be unmapped so it isn't needed to keep track of it. |
| 80 | */ |
| 81 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 82 | int *tables_mapped_regions; |
| 83 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 84 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 85 | int next_table; |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Base translation table. It doesn't need to have the same amount of |
| 89 | * entries as the ones used for other levels. |
| 90 | */ |
| 91 | uint64_t *base_table; |
| 92 | unsigned int base_table_entries; |
| 93 | |
| 94 | /* |
| 95 | * Max Physical and Virtual addresses currently in use by the |
| 96 | * translation tables. These might get updated as we map/unmap memory |
| 97 | * regions but they will never go beyond pa/va_max_address. |
| 98 | */ |
| 99 | unsigned long long max_pa; |
| 100 | uintptr_t max_va; |
| 101 | |
| 102 | /* Level of the base translation table. */ |
| 103 | unsigned int base_level; |
| 104 | |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 105 | /* Set to true when the translation tables are initialized. */ |
| 106 | bool initialized; |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 107 | |
| 108 | /* |
Antonio Nino Diaz | f1b84f6 | 2018-07-03 11:58:49 +0100 | [diff] [blame] | 109 | * Translation regime managed by this xlat_ctx_t. It should be one of |
| 110 | * the EL*_REGIME defines. |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 111 | */ |
Antonio Nino Diaz | dcf9d92 | 2017-10-04 16:52:15 +0100 | [diff] [blame] | 112 | int xlat_regime; |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | #if PLAT_XLAT_TABLES_DYNAMIC |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 116 | #define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 117 | static int _ctx_name##_mapped_regions[_xlat_tables_count]; |
| 118 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 119 | #define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 120 | .tables_mapped_regions = _ctx_name##_mapped_regions, |
| 121 | #else |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 122 | #define XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 123 | /* do nothing */ |
| 124 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 125 | #define XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 126 | /* do nothing */ |
| 127 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 128 | |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 129 | #if PLAT_RO_XLAT_TABLES |
| 130 | #define XLAT_CTX_INIT_TABLE_ATTR() \ |
| 131 | .readonly_tables = false, |
| 132 | #else |
| 133 | #define XLAT_CTX_INIT_TABLE_ATTR() |
| 134 | /* do nothing */ |
| 135 | #endif |
| 136 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 137 | #define REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count, \ |
| 138 | _xlat_tables_count, _virt_addr_space_size, \ |
Masahiro Yamada | 0db2375 | 2020-03-06 19:21:26 +0900 | [diff] [blame] | 139 | _phy_addr_space_size, _xlat_regime, \ |
| 140 | _table_section, _base_table_section) \ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 141 | CASSERT(CHECK_PHY_ADDR_SPACE_SIZE(_phy_addr_space_size), \ |
| 142 | assert_invalid_physical_addr_space_sizefor_##_ctx_name);\ |
| 143 | \ |
| 144 | static mmap_region_t _ctx_name##_mmap[_mmap_count + 1]; \ |
| 145 | \ |
| 146 | static uint64_t _ctx_name##_xlat_tables[_xlat_tables_count] \ |
| 147 | [XLAT_TABLE_ENTRIES] \ |
Masahiro Yamada | 0db2375 | 2020-03-06 19:21:26 +0900 | [diff] [blame] | 148 | __aligned(XLAT_TABLE_SIZE) __section(_table_section); \ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 149 | \ |
| 150 | static uint64_t _ctx_name##_base_xlat_table \ |
| 151 | [GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)] \ |
| 152 | __aligned(GET_NUM_BASE_LEVEL_ENTRIES(_virt_addr_space_size)\ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 153 | * sizeof(uint64_t)) \ |
Masahiro Yamada | 0db2375 | 2020-03-06 19:21:26 +0900 | [diff] [blame] | 154 | __section(_base_table_section); \ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 155 | \ |
| 156 | XLAT_ALLOC_DYNMAP_STRUCT(_ctx_name, _xlat_tables_count) \ |
| 157 | \ |
| 158 | static xlat_ctx_t _ctx_name##_xlat_ctx = { \ |
| 159 | .pa_max_address = (_phy_addr_space_size) - 1ULL, \ |
| 160 | .va_max_address = (_virt_addr_space_size) - 1UL, \ |
| 161 | .mmap = _ctx_name##_mmap, \ |
| 162 | .mmap_num = (_mmap_count), \ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 163 | .tables = _ctx_name##_xlat_tables, \ |
Masahiro Yamada | b042060 | 2020-03-09 17:39:27 +0900 | [diff] [blame] | 164 | .tables_num = ARRAY_SIZE(_ctx_name##_xlat_tables), \ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 165 | XLAT_CTX_INIT_TABLE_ATTR() \ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 166 | XLAT_REGISTER_DYNMAP_STRUCT(_ctx_name) \ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 167 | .next_table = 0, \ |
| 168 | .base_table = _ctx_name##_base_xlat_table, \ |
| 169 | .base_table_entries = \ |
Masahiro Yamada | b042060 | 2020-03-09 17:39:27 +0900 | [diff] [blame] | 170 | ARRAY_SIZE(_ctx_name##_base_xlat_table), \ |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 171 | .max_pa = 0U, \ |
| 172 | .max_va = 0U, \ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 173 | .base_level = GET_XLAT_TABLE_LEVEL_BASE(_virt_addr_space_size),\ |
Antonio Nino Diaz | 5c97bd1 | 2018-08-02 09:57:29 +0100 | [diff] [blame] | 174 | .initialized = false, \ |
Petre-Ionut Tudor | e5a6fef | 2019-11-07 15:18:03 +0000 | [diff] [blame] | 175 | .xlat_regime = (_xlat_regime) \ |
Sandrine Bailleux | 3120ea2 | 2017-07-10 13:37:48 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 178 | #endif /*__ASSEMBLER__*/ |
Jeenu Viswambharan | 58e8148 | 2018-04-27 15:06:57 +0100 | [diff] [blame] | 179 | |
Antonio Nino Diaz | 50eb374 | 2018-07-24 10:20:53 +0100 | [diff] [blame] | 180 | #endif /* XLAT_TABLES_V2_HELPERS_H */ |