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