Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <arch_helpers.h> |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 8 | #include <bitmap.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 9 | #include <granule.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 10 | #include <ripas.h> |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 11 | #include <s2tt.h> |
| 12 | #include <s2tt_pvt_defs.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 13 | #include <smc.h> |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 14 | #include <stdbool.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 15 | #include <stddef.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 16 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Return a mask for the IPA field on a S2TTE |
| 19 | */ |
| 20 | static unsigned long s2tte_lvl_mask(long level, bool lpa2) |
| 21 | { |
| 22 | assert(level <= S2TT_PAGE_LEVEL); |
| 23 | assert(level >= S2TT_MIN_STARTING_LEVEL_LPA2); |
| 24 | |
| 25 | unsigned long mask; |
| 26 | unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level); |
| 27 | unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT; |
| 28 | |
| 29 | mask = BIT_MASK_ULL((S2TTE_OA_BITS - 1U), lsb); |
| 30 | |
| 31 | if (lpa2 == true) { |
| 32 | mask |= (MASK(LPA2_S2TTE_51_50) | MASK(LPA2_OA_49_48)); |
| 33 | } |
| 34 | |
| 35 | return mask; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Extracts the PA mapped by an S2TTE, aligned to a given level. |
| 40 | */ |
| 41 | static unsigned long s2tte_to_pa(unsigned long s2tte, long level, bool lpa2) |
| 42 | { |
| 43 | unsigned long pa = s2tte & s2tte_lvl_mask(level, lpa2); |
| 44 | |
| 45 | if (lpa2 == true) { |
| 46 | pa &= ~MASK(LPA2_S2TTE_51_50); |
| 47 | pa |= INPLACE(LPA2_OA_51_50, EXTRACT(LPA2_S2TTE_51_50, s2tte)); |
| 48 | } |
| 49 | |
| 50 | return pa; |
| 51 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Invalidates S2 TLB entries from [ipa, ipa + size] region tagged with `vmid`. |
| 55 | */ |
Javier Almansa Sobrino | 2eb98b0 | 2023-12-18 18:10:55 +0000 | [diff] [blame] | 56 | static void stage2_tlbi_ipa(const struct s2tt_context *s2_ctx, |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 57 | unsigned long ipa, |
| 58 | unsigned long size) |
| 59 | { |
| 60 | /* |
| 61 | * Notes: |
| 62 | * |
| 63 | * - This follows the description provided in the Arm ARM on |
| 64 | * "Invalidation of TLB entries from stage 2 translations". |
| 65 | * |
| 66 | * - @TODO: Provide additional information to this primitive so that |
| 67 | * we can utilize: |
| 68 | * - The TTL level hint, see FEAT_TTL, |
| 69 | * - Final level lookup only invalidation, |
| 70 | * - Address range invalidation. |
| 71 | */ |
| 72 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 73 | assert(s2_ctx != NULL); |
| 74 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Save the current content of vttb_el2. |
| 77 | */ |
| 78 | unsigned long old_vttbr_el2 = read_vttbr_el2(); |
| 79 | |
| 80 | /* |
| 81 | * Make 'vmid' the `current vmid`. Note that the tlbi instructions |
| 82 | * bellow target the TLB entries that match the `current vmid`. |
| 83 | */ |
| 84 | write_vttbr_el2(INPLACE(VTTBR_EL2_VMID, s2_ctx->vmid)); |
| 85 | isb(); |
| 86 | |
| 87 | /* |
| 88 | * Invalidate entries in S2 TLB caches that |
| 89 | * match both `ipa` & the `current vmid`. |
| 90 | */ |
| 91 | while (size != 0UL) { |
| 92 | tlbiipas2e1is(ipa >> 12); |
| 93 | size -= GRANULE_SIZE; |
| 94 | ipa += GRANULE_SIZE; |
| 95 | } |
| 96 | dsb(ish); |
| 97 | |
| 98 | /* |
| 99 | * The architecture does not require TLB invalidation by IPA to affect |
| 100 | * combined Stage-1 + Stage-2 TLBs. Therefore we must invalidate all of |
| 101 | * Stage-1 (tagged with the `current vmid`) after invalidating Stage-2. |
| 102 | */ |
| 103 | tlbivmalle1is(); |
| 104 | dsb(ish); |
| 105 | isb(); |
| 106 | |
| 107 | /* |
| 108 | * Restore the old content of vttb_el2. |
| 109 | */ |
| 110 | write_vttbr_el2(old_vttbr_el2); |
| 111 | isb(); |
| 112 | } |
| 113 | |
| 114 | /* |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 115 | * Returns true if s2tte has 'output address' field, namely, if it is one of: |
| 116 | * - assigned_empty |
| 117 | * - assigned_ram |
| 118 | * - assigned_ns |
| 119 | * - assigned_destroyed |
| 120 | * - table |
| 121 | */ |
| 122 | static bool s2tte_has_pa(const struct s2tt_context *s2_ctx, |
| 123 | unsigned long s2tte, long level) |
| 124 | { |
| 125 | unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK; |
| 126 | |
| 127 | return ((desc_type != S2TTE_INVALID) || /* block, page or table */ |
| 128 | s2tte_is_assigned_empty(s2_ctx, s2tte, level) || |
| 129 | s2tte_is_assigned_destroyed(s2_ctx, s2tte, level)); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Creates a TTE containing only the PA. |
| 134 | * This function expects 'pa' to be aligned and bounded. |
| 135 | */ |
| 136 | static unsigned long pa_to_s2tte(unsigned long pa, bool lpa2) |
| 137 | { |
| 138 | unsigned long tte = pa; |
| 139 | |
| 140 | if (lpa2 == true) { |
| 141 | tte &= ~MASK(LPA2_OA_51_50); |
| 142 | tte |= INPLACE(LPA2_S2TTE_51_50, EXTRACT(LPA2_OA_51_50, pa)); |
| 143 | } |
| 144 | |
| 145 | return tte; |
| 146 | } |
| 147 | |
| 148 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 149 | * Invalidate S2 TLB entries with "addr" IPA. |
| 150 | * Call this function after: |
| 151 | * 1. A L3 page desc has been removed. |
| 152 | */ |
Javier Almansa Sobrino | 2eb98b0 | 2023-12-18 18:10:55 +0000 | [diff] [blame] | 153 | void s2tt_invalidate_page(const struct s2tt_context *s2_ctx, unsigned long addr) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 154 | { |
| 155 | stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Invalidate S2 TLB entries with "addr" IPA. |
| 160 | * Call this function after: |
| 161 | * 1. A L2 block desc has been removed, or |
| 162 | * 2a. A L2 table desc has been removed, where |
| 163 | * 2b. All S2TTEs in L3 table that the L2 table desc was pointed to were invalid. |
| 164 | */ |
Javier Almansa Sobrino | 2eb98b0 | 2023-12-18 18:10:55 +0000 | [diff] [blame] | 165 | void s2tt_invalidate_block(const struct s2tt_context *s2_ctx, unsigned long addr) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 166 | { |
| 167 | stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Invalidate S2 TLB entries with "addr" IPA. |
| 172 | * Call this function after: |
| 173 | * 1a. A L2 table desc has been removed, where |
| 174 | * 1b. Some S2TTEs in the table that the L2 table desc was pointed to were valid. |
| 175 | */ |
Javier Almansa Sobrino | 2eb98b0 | 2023-12-18 18:10:55 +0000 | [diff] [blame] | 176 | void s2tt_invalidate_pages_in_block(const struct s2tt_context *s2_ctx, |
| 177 | unsigned long addr) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 178 | { |
| 179 | stage2_tlbi_ipa(s2_ctx, addr, BLOCK_L2_SIZE); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Return the index of the entry describing @addr in the translation table at |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 184 | * level @level. This only works for non-concatenated page tables, so should |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 185 | * not be called to get the index for the starting level. |
| 186 | * |
| 187 | * See the library pseudocode |
| 188 | * aarch64/translation/vmsa_addrcalc/AArch64.TTEntryAddress on which this is |
| 189 | * modeled. |
| 190 | */ |
| 191 | static unsigned long s2_addr_to_idx(unsigned long addr, long level) |
| 192 | { |
AlexeiFedorov | bb1f1c8 | 2023-08-25 10:44:49 +0100 | [diff] [blame] | 193 | unsigned int levels, lsb; |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 194 | unsigned int s2tte_stride = (level < S2TT_MIN_STARTING_LEVEL) ? |
| 195 | S2TTE_STRIDE_LM1 : S2TTE_STRIDE; |
AlexeiFedorov | bb1f1c8 | 2023-08-25 10:44:49 +0100 | [diff] [blame] | 196 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 197 | levels = (unsigned int)(S2TT_PAGE_LEVEL - level); |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 198 | lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 199 | |
| 200 | addr >>= lsb; |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 201 | addr &= (1UL << s2tte_stride) - 1UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 202 | return addr; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Return the index of the entry describing @addr in the translation table |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 207 | * starting level. This may return an index >= S2TTES_PER_S2TT when the |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 208 | * combination of @start_level and @ipa_bits implies concatenated |
| 209 | * stage 2 tables. |
| 210 | * |
| 211 | * See the library pseudocode |
| 212 | * aarch64/translation/vmsa_addrcalc/AArch64.S2SLTTEntryAddress on which |
| 213 | * this is modeled. |
| 214 | */ |
| 215 | static unsigned long s2_sl_addr_to_idx(unsigned long addr, int start_level, |
| 216 | unsigned long ipa_bits) |
| 217 | { |
AlexeiFedorov | bb1f1c8 | 2023-08-25 10:44:49 +0100 | [diff] [blame] | 218 | unsigned int levels, lsb; |
| 219 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 220 | levels = (unsigned int)(S2TT_PAGE_LEVEL - start_level); |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 221 | lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 222 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 223 | addr &= ((1UL << ipa_bits) - 1UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 224 | addr >>= lsb; |
| 225 | return addr; |
| 226 | } |
| 227 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 228 | static bool entry_is_table(unsigned long entry) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 229 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 230 | return ((entry & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 233 | static unsigned long table_get_entry(const struct s2tt_context *s2_ctx, |
| 234 | struct granule *g_tbl, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 235 | unsigned long idx) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 236 | { |
| 237 | unsigned long *table, entry; |
| 238 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 239 | (void)s2_ctx; |
| 240 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 241 | table = granule_map(g_tbl, SLOT_RTT); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 242 | assert(table != NULL); |
| 243 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 244 | entry = s2tte_read(&table[idx]); |
| 245 | buffer_unmap(table); |
| 246 | |
| 247 | return entry; |
| 248 | } |
| 249 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 250 | #define table_entry_to_phys(tte, lpa2) \ |
| 251 | s2tte_to_pa(tte, S2TT_PAGE_LEVEL, lpa2) |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 252 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 253 | static struct granule *find_next_level_idx(const struct s2tt_context *s2_ctx, |
| 254 | struct granule *g_tbl, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 255 | unsigned long idx) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 256 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 257 | assert(s2_ctx != NULL); |
| 258 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 259 | const unsigned long entry = table_get_entry(s2_ctx, g_tbl, idx); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 260 | |
| 261 | if (!entry_is_table(entry)) { |
| 262 | return NULL; |
| 263 | } |
| 264 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 265 | return addr_to_granule(table_entry_to_phys(entry, s2_ctx->enable_lpa2)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 268 | static struct granule *find_lock_next_level(const struct s2tt_context *s2_ctx, |
| 269 | struct granule *g_tbl, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 270 | unsigned long map_addr, |
| 271 | long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 272 | { |
| 273 | const unsigned long idx = s2_addr_to_idx(map_addr, level); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 274 | struct granule *g = find_next_level_idx(s2_ctx, g_tbl, idx); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 275 | |
| 276 | if (g != NULL) { |
| 277 | granule_lock(g, GRANULE_STATE_RTT); |
| 278 | } |
| 279 | |
| 280 | return g; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Walk an RTT until level @level using @map_addr. |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 285 | * @g_root is the root (level 0/-1) table and must be locked before the call. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 286 | * @start_level is the initial lookup level used for the stage 2 translation |
| 287 | * tables which may depend on the configuration of the realm, factoring in the |
| 288 | * IPA size of the realm and the desired starting level (within the limits |
| 289 | * defined by the Armv8 VMSA including options for stage 2 table concatenation). |
| 290 | * The function uses hand-over-hand locking to avoid race conditions and allow |
| 291 | * concurrent access to RTT tree which is not part of the current walk; when a |
| 292 | * next level table is reached it is locked before releasing previously locked |
| 293 | * table. |
| 294 | * The walk stops when either: |
| 295 | * - The entry found is a leaf entry (not an RTT Table entry), or |
| 296 | * - Level @level is reached. |
| 297 | * |
| 298 | * On return: |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 299 | * - s2tt_walk::last_level is the last level that has been reached by the walk. |
| 300 | * - s2tt_walk.g_llt points to the TABLE granule at level @s2tt_walk::level. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 301 | * The granule is locked. |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 302 | * - s2tt_walk::index is the entry index at s2tt_walk.g_llt for @map_addr.i |
| 303 | * |
| 304 | * NOTE: This function expects that the root table on the s2 context is |
| 305 | * already locked. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 306 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 307 | void s2tt_walk_lock_unlock(const struct s2tt_context *s2_ctx, |
| 308 | unsigned long map_addr, |
| 309 | long level, |
| 310 | struct s2tt_walk *wi) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 311 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 312 | struct granule *g_tbls[NR_RTT_LEVELS_LPA2] = { (struct granule *)NULL }; |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 313 | struct granule *g_root; |
| 314 | unsigned long sl_idx, ipa_bits; |
| 315 | int i, start_level, last_level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 316 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 317 | assert(s2_ctx != NULL); |
| 318 | |
| 319 | start_level = s2_ctx->s2_starting_level; |
| 320 | ipa_bits = s2_ctx->ipa_bits; |
| 321 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 322 | assert(level >= start_level); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 323 | assert(level <= S2TT_PAGE_LEVEL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 324 | assert(map_addr < (1UL << ipa_bits)); |
| 325 | assert(wi != NULL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 326 | |
| 327 | g_root = s2_ctx->g_rtt; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 328 | |
| 329 | /* Handle concatenated starting level (SL) tables */ |
| 330 | sl_idx = s2_sl_addr_to_idx(map_addr, start_level, ipa_bits); |
| 331 | if (sl_idx >= S2TTES_PER_S2TT) { |
AlexeiFedorov | 4faab85 | 2023-08-30 15:06:49 +0100 | [diff] [blame] | 332 | unsigned int tt_num = (unsigned int)(sl_idx >> S2TTE_STRIDE); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 333 | struct granule *g_concat_root; |
| 334 | |
| 335 | assert(tt_num < S2TTE_MAX_CONCAT_TABLES); |
| 336 | |
| 337 | g_concat_root = (struct granule *)((uintptr_t)g_root + |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 338 | (tt_num * sizeof(struct granule))); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 339 | |
| 340 | granule_lock(g_concat_root, GRANULE_STATE_RTT); |
| 341 | granule_unlock(g_root); |
| 342 | g_root = g_concat_root; |
| 343 | } |
| 344 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 345 | /* 'start_level' can be '-1', so add 1 when used as an index */ |
| 346 | g_tbls[start_level + 1] = g_root; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 347 | for (i = start_level; i < level; i++) { |
| 348 | /* |
| 349 | * Lock next RTT level. Correct locking order is guaranteed |
| 350 | * because reference is obtained from a locked granule |
| 351 | * (previous level). Also, hand-over-hand locking/unlocking is |
| 352 | * used to avoid race conditions. |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 353 | * |
| 354 | * Note that as 'start_level' can be -1, we add '1' to the |
| 355 | * index 'i' to compensate for the negative value when we |
| 356 | * use it to index then 'g_tbls' list. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 357 | */ |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 358 | g_tbls[i + 1 + 1] = find_lock_next_level(s2_ctx, g_tbls[i + 1], |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 359 | map_addr, i); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 360 | if (g_tbls[i + 1 + 1] == NULL) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 361 | last_level = i; |
| 362 | goto out; |
| 363 | } |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 364 | granule_unlock(g_tbls[i + 1]); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 365 | } |
| 366 | |
AlexeiFedorov | 4faab85 | 2023-08-30 15:06:49 +0100 | [diff] [blame] | 367 | last_level = (int)level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 368 | out: |
| 369 | wi->last_level = last_level; |
AlexeiFedorov | e956db3 | 2024-04-04 14:36:21 +0100 | [diff] [blame^] | 370 | /* coverity[deref_overflow:SUPPRESS] */ |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 371 | wi->g_llt = g_tbls[last_level + 1]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 372 | wi->index = s2_addr_to_idx(map_addr, last_level); |
| 373 | } |
| 374 | |
| 375 | /* |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 376 | * Creates an unassigned_empty s2tte. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 377 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 378 | unsigned long s2tte_create_unassigned_empty(const struct s2tt_context *s2_ctx) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 379 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 380 | (void)s2_ctx; |
| 381 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 382 | return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_EMPTY); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Creates an unassigned_ram s2tte. |
| 387 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 388 | unsigned long s2tte_create_unassigned_ram(const struct s2tt_context *s2_ctx) |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 389 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 390 | (void)s2_ctx; |
| 391 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 392 | return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_RAM); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 396 | * Creates an unassigned_destroyed s2tte. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 397 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 398 | unsigned long s2tte_create_unassigned_destroyed(const struct s2tt_context *s2_ctx) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 399 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 400 | (void)s2_ctx; |
| 401 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 402 | return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_DESTROYED); |
| 403 | } |
| 404 | |
| 405 | /* |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 406 | * Creates an unassigned_ns s2tte. |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 407 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 408 | unsigned long s2tte_create_unassigned_ns(const struct s2tt_context *s2_ctx) |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 409 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 410 | (void)s2_ctx; |
| 411 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 412 | return (S2TTE_NS | S2TTE_INVALID_HIPAS_UNASSIGNED | |
| 413 | S2TTE_INVALID_UNPROTECTED); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 418 | * RIPAS=@s2tte_ripas, at level @level. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 419 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 420 | static unsigned long s2tte_create_assigned(const struct s2tt_context *s2_ctx, |
| 421 | unsigned long pa, long level, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 422 | unsigned long s2tte_ripas) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 423 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 424 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 425 | assert(level <= S2TT_PAGE_LEVEL); |
| 426 | assert(s2tte_ripas <= S2TTE_INVALID_RIPAS_DESTROYED); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 427 | assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level)); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 428 | assert(s2_ctx != NULL); |
| 429 | |
| 430 | unsigned long tte = pa_to_s2tte(pa, s2_ctx->enable_lpa2); |
| 431 | unsigned long s2tte_page, s2tte_block; |
| 432 | |
| 433 | if (s2_ctx->enable_lpa2 == true) { |
| 434 | s2tte_page = S2TTE_PAGE_LPA2; |
| 435 | s2tte_block = S2TTE_BLOCK_LPA2; |
| 436 | } else { |
| 437 | s2tte_page = S2TTE_PAGE; |
| 438 | s2tte_block = S2TTE_BLOCK; |
| 439 | } |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 440 | |
| 441 | if (s2tte_ripas == S2TTE_INVALID_RIPAS_RAM) { |
| 442 | if (level == S2TT_PAGE_LEVEL) { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 443 | return (tte | s2tte_page); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 444 | } |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 445 | return (tte | s2tte_block); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 448 | return (tte | S2TTE_INVALID_HIPAS_ASSIGNED | s2tte_ripas); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Creates and invalid s2tte with output address @pa, HIPAS=ASSIGNED and |
| 453 | * RIPAS=DESTROYED at level @level. |
| 454 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 455 | unsigned long s2tte_create_assigned_destroyed(const struct s2tt_context *s2_ctx, |
| 456 | unsigned long pa, long level) |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 457 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 458 | return s2tte_create_assigned(s2_ctx, pa, level, |
| 459 | S2TTE_INVALID_RIPAS_DESTROYED); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and |
| 464 | * RIPAS=EMPTY at level @level. |
| 465 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 466 | unsigned long s2tte_create_assigned_empty(const struct s2tt_context *s2_ctx, |
| 467 | unsigned long pa, long level) |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 468 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 469 | return s2tte_create_assigned(s2_ctx, pa, level, |
| 470 | S2TTE_INVALID_RIPAS_EMPTY); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 474 | * Creates an assigned_ram s2tte with output address @pa. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 475 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 476 | unsigned long s2tte_create_assigned_ram(const struct s2tt_context *s2_ctx, |
| 477 | unsigned long pa, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 478 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 479 | return s2tte_create_assigned(s2_ctx, pa, level, |
| 480 | S2TTE_INVALID_RIPAS_RAM); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* |
Javier Almansa Sobrino | 84a1b16 | 2023-09-26 17:32:44 +0100 | [diff] [blame] | 484 | * Creates an assigned s2tte with output address @pa and the same |
| 485 | * RIPAS as passed on @s2tte. |
| 486 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 487 | unsigned long s2tte_create_assigned_unchanged(const struct s2tt_context *s2_ctx, |
| 488 | unsigned long s2tte, |
Javier Almansa Sobrino | 84a1b16 | 2023-09-26 17:32:44 +0100 | [diff] [blame] | 489 | unsigned long pa, |
| 490 | long level) |
| 491 | { |
| 492 | unsigned long current_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK; |
| 493 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 494 | return s2tte_create_assigned(s2_ctx, pa, level, current_ripas); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 498 | * Creates an assigned_ns s2tte at level @level. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 499 | * |
| 500 | * The following S2 TTE fields are provided through @s2tte argument: |
| 501 | * - The physical address |
| 502 | * - MemAttr |
| 503 | * - S2AP |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 504 | * - Shareability (when FEAT_LPA2 is disabled) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 505 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 506 | unsigned long s2tte_create_assigned_ns(const struct s2tt_context *s2_ctx, |
| 507 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 508 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 509 | /* |
| 510 | * We just mask out the DESC_TYPE below. The Shareability bits |
| 511 | * without FEAT_LPA2 are at the same position as OA bits [51:50] |
| 512 | * with FEAT_LPA2 enabled, so we don't need to cater for that |
| 513 | * separately. |
| 514 | */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 515 | unsigned long new_s2tte = s2tte & ~S2TT_DESC_TYPE_MASK; |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 516 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 517 | (void)s2_ctx; |
| 518 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 519 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 520 | assert(level <= S2TT_PAGE_LEVEL); |
| 521 | assert((s2tte & S2TTE_NS_ATTR_RMM) == 0UL); |
| 522 | |
| 523 | if (level == S2TT_PAGE_LEVEL) { |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 524 | return (new_s2tte | S2TTE_PAGE_NS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 525 | } |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 526 | return (new_s2tte | S2TTE_BLOCK_NS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Validate the portion of NS S2TTE that is provided by the host. |
| 531 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 532 | bool host_ns_s2tte_is_valid(const struct s2tt_context *s2_ctx, |
| 533 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 534 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 535 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 536 | bool lpa2; |
| 537 | unsigned long mask; |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 538 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 539 | assert(s2_ctx != NULL); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 540 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 541 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 542 | lpa2 = s2_ctx->enable_lpa2; |
| 543 | |
| 544 | mask = s2tte_lvl_mask(level, lpa2); |
| 545 | if (lpa2 == true) { |
| 546 | mask |= S2TTE_NS_ATTR_LPA2_MASK; |
| 547 | } else { |
| 548 | mask |= S2TTE_NS_ATTR_MASK; |
| 549 | |
| 550 | /* Only SH_IS is allowed */ |
| 551 | if ((s2tte & S2TTE_SH_MASK) != S2TTE_SH_IS) { |
| 552 | return false; |
| 553 | } |
| 554 | } |
| 555 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Test that all fields that are not controlled by the host are zero |
| 558 | * and that the output address is correctly aligned. Note that |
| 559 | * the host is permitted to map any physical address outside PAR. |
| 560 | */ |
| 561 | if ((s2tte & ~mask) != 0UL) { |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Only one value masked by S2TTE_MEMATTR_MASK is invalid/reserved. |
| 567 | */ |
| 568 | if ((s2tte & S2TTE_MEMATTR_MASK) == S2TTE_MEMATTR_FWB_RESERVED) { |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 573 | * Note that all the values that are masked by S2TTE_AP_MASK are valid. |
| 574 | */ |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Returns the portion of NS S2TTE that is set by the host. |
| 580 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 581 | unsigned long host_ns_s2tte(const struct s2tt_context *s2_ctx, |
| 582 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 583 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 584 | assert(s2_ctx != NULL); |
| 585 | |
| 586 | unsigned long mask = s2tte_lvl_mask(level, s2_ctx->enable_lpa2); |
| 587 | |
| 588 | mask |= (s2_ctx->enable_lpa2 == true) ? S2TTE_NS_ATTR_LPA2_MASK : |
| 589 | S2TTE_NS_ATTR_MASK; |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 590 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 591 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 592 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 593 | return (s2tte & mask); |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Creates a table s2tte at level @level with output address @pa. |
| 598 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 599 | unsigned long s2tte_create_table(const struct s2tt_context *s2_ctx, |
| 600 | unsigned long pa, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 601 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 602 | __unused int min_starting_level; |
| 603 | |
Chuyue Luo | 5ef71d9 | 2023-10-24 14:29:20 +0100 | [diff] [blame] | 604 | (void)level; |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 605 | |
| 606 | assert(s2_ctx != NULL); |
| 607 | min_starting_level = (s2_ctx->enable_lpa2 == true) ? |
| 608 | S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL; |
Chuyue Luo | 5ef71d9 | 2023-10-24 14:29:20 +0100 | [diff] [blame] | 609 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 610 | assert(level < S2TT_PAGE_LEVEL); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 611 | assert(level >= min_starting_level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 612 | assert(GRANULE_ALIGNED(pa)); |
| 613 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 614 | return (pa_to_s2tte(pa, s2_ctx->enable_lpa2) | S2TTE_TABLE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /* |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 618 | * Returns true if s2tte has defined ripas value, namely if it is one of: |
| 619 | * - unassigned_empty |
| 620 | * - unassigned_ram |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 621 | * - unassigned_destroyed |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 622 | * - assigned_empty |
| 623 | * - assigned_ram |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 624 | * - assigned_destroyed |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 625 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 626 | bool s2tte_has_ripas(const struct s2tt_context *s2_ctx, |
| 627 | unsigned long s2tte, long level) |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 628 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 629 | return (((s2tte & S2TTE_NS) == 0UL) && !s2tte_is_table(s2_ctx, |
| 630 | s2tte, level)); |
AlexeiFedorov | 0fb4455 | 2023-04-14 15:37:58 +0100 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 634 | * Returns true if @s2tte has HIPAS=@hipas. |
| 635 | */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 636 | static inline bool s2tte_has_hipas(unsigned long s2tte, unsigned long hipas) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 637 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 638 | unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 639 | unsigned long invalid_desc_hipas = s2tte & S2TTE_INVALID_HIPAS_MASK; |
| 640 | |
AlexeiFedorov | 63614ea | 2023-07-14 17:07:20 +0100 | [diff] [blame] | 641 | return ((desc_type == S2TTE_INVALID) && (invalid_desc_hipas == hipas)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 645 | * Returns true if @s2tte has HIPAS=UNASSIGNED and RIPAS=@ripas. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 646 | */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 647 | static inline bool s2tte_has_unassigned_ripas(unsigned long s2tte, |
| 648 | unsigned long ripas) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 649 | { |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 650 | return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) && |
| 651 | s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED)); |
| 652 | } |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 653 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 654 | /* |
| 655 | * Returns true if @s2tte has HIPAS=ASSIGNED and RIPAS=@ripas. |
| 656 | */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 657 | static inline bool s2tte_has_assigned_ripas(unsigned long s2tte, |
| 658 | unsigned long ripas) |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 659 | { |
| 660 | return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) && |
| 661 | s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_ASSIGNED)); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
AlexeiFedorov | 0f9cd1f | 2023-07-10 17:04:58 +0100 | [diff] [blame] | 665 | * Returns true if @s2tte has HIPAS=UNASSIGNED. |
| 666 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 667 | bool s2tte_is_unassigned(const struct s2tt_context *s2_ctx, unsigned long s2tte) |
AlexeiFedorov | 0f9cd1f | 2023-07-10 17:04:58 +0100 | [diff] [blame] | 668 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 669 | (void)s2_ctx; |
| 670 | |
AlexeiFedorov | 0f9cd1f | 2023-07-10 17:04:58 +0100 | [diff] [blame] | 671 | return s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED); |
| 672 | } |
| 673 | |
| 674 | /* |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 675 | * Returns true if @s2tte is an unassigned_empty. |
| 676 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 677 | bool s2tte_is_unassigned_empty(const struct s2tt_context *s2_ctx, |
| 678 | unsigned long s2tte) |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 679 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 680 | (void)s2_ctx; |
| 681 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 682 | return (((s2tte & S2TTE_NS) == 0UL) && |
| 683 | s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY)); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Returns true if @s2tte is an unassigned_ram. |
| 688 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 689 | bool s2tte_is_unassigned_ram(const struct s2tt_context *s2_ctx, |
| 690 | unsigned long s2tte) |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 691 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 692 | (void)s2_ctx; |
| 693 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 694 | return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_RAM); |
| 695 | } |
| 696 | |
| 697 | /* |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 698 | * Returns true if @s2tte is unassigned_ns. |
| 699 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 700 | bool s2tte_is_unassigned_ns(const struct s2tt_context *s2_ctx, |
| 701 | unsigned long s2tte) |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 702 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 703 | (void)s2_ctx; |
| 704 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 705 | return (((s2tte & S2TTE_NS) != 0UL) && |
| 706 | s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 710 | * Returns true if @s2tte has RIPAS=DESTROYED. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 711 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 712 | bool s2tte_is_unassigned_destroyed(const struct s2tt_context *s2_ctx, |
| 713 | unsigned long s2tte) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 714 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 715 | (void)s2_ctx; |
| 716 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 717 | return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED); |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Returns true if @s2tte is an assigned_destroyed. |
| 722 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 723 | bool s2tte_is_assigned_destroyed(const struct s2tt_context *s2_ctx, |
| 724 | unsigned long s2tte, long level) |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 725 | { |
| 726 | (void)level; |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 727 | (void)s2_ctx; |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 728 | |
| 729 | return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 733 | * Returns true if @s2tte is an assigned_empty. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 734 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 735 | bool s2tte_is_assigned_empty(const struct s2tt_context *s2_ctx, |
| 736 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 737 | { |
| 738 | (void)level; |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 739 | (void)s2_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 740 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 741 | return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 744 | static bool s2tte_check(const struct s2tt_context *s2_ctx, unsigned long s2tte, |
| 745 | long level, unsigned long ns) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 746 | { |
| 747 | unsigned long desc_type; |
| 748 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 749 | (void)s2_ctx; |
| 750 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 751 | if ((s2tte & S2TTE_NS) != ns) { |
| 752 | return false; |
| 753 | } |
| 754 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 755 | desc_type = s2tte & S2TT_DESC_TYPE_MASK; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 756 | |
| 757 | /* Only pages at L3 and valid blocks at L2 allowed */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 758 | if (((level == S2TT_PAGE_LEVEL) && (desc_type == S2TTE_L3_PAGE)) || |
| 759 | ((level == S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK))) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 767 | * Returns true if @s2tte is an assigned_ram. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 768 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 769 | bool s2tte_is_assigned_ram(const struct s2tt_context *s2_ctx, |
| 770 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 771 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 772 | return s2tte_check(s2_ctx, s2tte, level, 0UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 776 | * Returns true if @s2tte is an assigned_ns s2tte. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 777 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 778 | bool s2tte_is_assigned_ns(const struct s2tt_context *s2_ctx, |
| 779 | unsigned long s2tte, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 780 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 781 | return s2tte_check(s2_ctx, s2tte, level, S2TTE_NS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Returns true if @s2tte is a table at level @level. |
| 786 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 787 | bool s2tte_is_table(const struct s2tt_context *s2_ctx, unsigned long s2tte, |
| 788 | long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 789 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 790 | (void)s2_ctx; |
| 791 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 792 | return ((level < S2TT_PAGE_LEVEL) && |
| 793 | ((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Returns RIPAS of @s2tte. |
| 798 | * |
| 799 | * Caller should ensure that HIPAS=UNASSIGNED or HIPAS=ASSIGNED. |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 800 | * The s2tte, if valid, should correspond to RIPAS_RAM. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 801 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 802 | enum ripas s2tte_get_ripas(const struct s2tt_context *s2_ctx, unsigned long s2tte) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 803 | { |
| 804 | unsigned long desc_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK; |
| 805 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 806 | (void)s2_ctx; |
| 807 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 808 | /* |
| 809 | * If valid s2tte descriptor is passed, then ensure S2AP[0] |
| 810 | * bit is 1 (S2AP is set to RW for lower EL), which corresponds |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 811 | * to RIPAS_RAM (bits[6:5] = b01) on a valid descriptor. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 812 | */ |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 813 | assert(((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_INVALID) || |
| 814 | (desc_ripas == S2TTE_INVALID_RIPAS_RAM)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 815 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 816 | assert(EXTRACT(S2TTE_INVALID_HIPAS, s2tte) <= |
| 817 | EXTRACT(S2TTE_INVALID_HIPAS, S2TTE_INVALID_HIPAS_ASSIGNED)); |
| 818 | |
| 819 | desc_ripas = desc_ripas >> S2TTE_INVALID_RIPAS_SHIFT; |
| 820 | |
| 821 | assert(desc_ripas <= (unsigned int)RIPAS_DESTROYED); |
| 822 | |
| 823 | return (enum ripas)desc_ripas; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 827 | * Populates @s2tt with unassigned_empty s2ttes. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 828 | * |
| 829 | * The granule is populated before it is made a table, |
| 830 | * hence, don't use s2tte_write for access. |
| 831 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 832 | void s2tt_init_unassigned_empty(const struct s2tt_context *s2_ctx, |
| 833 | unsigned long *s2tt) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 834 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 835 | assert(s2tt != NULL); |
| 836 | |
| 837 | unsigned long s2tte = |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 838 | s2tte_create_unassigned_empty(s2_ctx); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 839 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 840 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 841 | s2tt[i] = s2tte; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | dsb(ish); |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Populates @s2tt with unassigned_ram s2ttes. |
| 849 | * |
| 850 | * The granule is populated before it is made a table, |
| 851 | * hence, don't use s2tte_write for access. |
| 852 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 853 | void s2tt_init_unassigned_ram(const struct s2tt_context *s2_ctx, |
| 854 | unsigned long *s2tt) |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 855 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 856 | assert(s2tt != NULL); |
| 857 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 858 | unsigned long s2tte = s2tte_create_unassigned_ram(s2_ctx); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 859 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 860 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 861 | s2tt[i] = s2tte; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | dsb(ish); |
| 865 | } |
| 866 | |
| 867 | /* |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 868 | * Populates @s2tt with unassigned_ns s2ttes. |
| 869 | * |
| 870 | * The granule is populated before it is made a table, |
| 871 | * hence, don't use s2tte_write for access. |
| 872 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 873 | void s2tt_init_unassigned_ns(const struct s2tt_context *s2_ctx, |
| 874 | unsigned long *s2tt) |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 875 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 876 | assert(s2tt != NULL); |
| 877 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 878 | unsigned long s2tte = s2tte_create_unassigned_ns(s2_ctx); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 879 | |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 880 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 881 | s2tt[i] = s2tte; |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | dsb(ish); |
| 885 | } |
| 886 | |
| 887 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 888 | * Populates @s2tt with s2ttes which have HIPAS=DESTROYED. |
| 889 | * |
| 890 | * The granule is populated before it is made a table, |
| 891 | * hence, don't use s2tte_write for access. |
| 892 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 893 | void s2tt_init_unassigned_destroyed(const struct s2tt_context *s2_ctx, |
| 894 | unsigned long *s2tt) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 895 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 896 | assert(s2tt != NULL); |
| 897 | |
| 898 | unsigned long s2tte = |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 899 | s2tte_create_unassigned_destroyed(s2_ctx); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 900 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 901 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 902 | s2tt[i] = s2tte; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 903 | } |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 904 | dsb(ish); |
| 905 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 906 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 907 | /* |
| 908 | * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=DESTROYED s2ttes that refer to a |
| 909 | * contiguous memory block starting at @pa, and mapped at level @level. |
| 910 | * |
| 911 | * The granule is populated before it is made a table, |
| 912 | * hence, don't use s2tte_write for access. |
| 913 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 914 | void s2tt_init_assigned_destroyed(const struct s2tt_context *s2_ctx, |
| 915 | unsigned long *s2tt, unsigned long pa, |
| 916 | long level) |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 917 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 918 | assert(s2tt != NULL); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 919 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 920 | assert(level <= S2TT_PAGE_LEVEL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 921 | assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level)); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 922 | |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 923 | const unsigned long map_size = s2tte_map_size(level); |
| 924 | |
| 925 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 926 | s2tt[i] = s2tte_create_assigned_destroyed(s2_ctx, pa, level); |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 927 | pa += map_size; |
| 928 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 929 | dsb(ish); |
| 930 | } |
| 931 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 932 | /* |
| 933 | * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=EMPTY s2ttes that refer to a |
| 934 | * contiguous memory block starting at @pa, and mapped at level @level. |
| 935 | * |
| 936 | * The granule is populated before it is made a table, |
| 937 | * hence, don't use s2tte_write for access. |
| 938 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 939 | void s2tt_init_assigned_empty(const struct s2tt_context *s2_ctx, |
| 940 | unsigned long *s2tt, unsigned long pa, |
| 941 | long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 942 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 943 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 944 | assert(level <= S2TT_PAGE_LEVEL); |
| 945 | assert(s2tt != NULL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 946 | assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level)); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 947 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 948 | const unsigned long map_size = s2tte_map_size(level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 949 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 950 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 951 | s2tt[i] = s2tte_create_assigned_empty(s2_ctx, pa, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 952 | pa += map_size; |
| 953 | } |
| 954 | dsb(ish); |
| 955 | } |
| 956 | |
| 957 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 958 | * Populates @s2tt with assigned_ram s2ttes that refer to a |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 959 | * contiguous memory block starting at @pa, and mapped at level @level. |
| 960 | * |
| 961 | * The granule is populated before it is made a table, |
| 962 | * hence, don't use s2tte_write for access. |
| 963 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 964 | void s2tt_init_assigned_ram(const struct s2tt_context *s2_ctx, |
| 965 | unsigned long *s2tt, unsigned long pa, |
| 966 | long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 967 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 968 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 969 | assert(level <= S2TT_PAGE_LEVEL); |
| 970 | assert(s2tt != NULL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 971 | assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level)); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 972 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 973 | const unsigned long map_size = s2tte_map_size(level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 974 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 975 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 976 | s2tt[i] = s2tte_create_assigned_ram(s2_ctx, pa, level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 977 | pa += map_size; |
| 978 | } |
| 979 | dsb(ish); |
| 980 | } |
| 981 | |
| 982 | /* |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 983 | * Populates @s2tt with NS attributes @attrs that refer to a |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 984 | * contiguous memory block starting at @pa, and mapped at level @level. |
| 985 | * |
| 986 | * The granule is populated before it is made a table, |
| 987 | * hence, don't use s2tte_write for access. |
| 988 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 989 | void s2tt_init_assigned_ns(const struct s2tt_context *s2_ctx, |
| 990 | unsigned long *s2tt, unsigned long attrs, |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 991 | unsigned long pa, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 992 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 993 | assert(level >= S2TT_MIN_BLOCK_LEVEL); |
| 994 | assert(level <= S2TT_PAGE_LEVEL); |
| 995 | assert(s2tt != NULL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 996 | assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level)); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 997 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 998 | const unsigned long map_size = s2tte_map_size(level); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 999 | unsigned long ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ? |
| 1000 | S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1001 | |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 1002 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1003 | unsigned long s2tte = attrs & ns_attr_host_mask; |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1004 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1005 | s2tt[i] = s2tte_create_assigned_ns(s2_ctx, |
| 1006 | s2tte | pa_to_s2tte(pa, s2_ctx->enable_lpa2), |
| 1007 | level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1008 | pa += map_size; |
| 1009 | } |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1010 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1011 | dsb(ish); |
| 1012 | } |
| 1013 | |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1014 | /* |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1015 | * Returns true if s2tte is a live RTTE entry. i.e., |
AlexeiFedorov | 3ebd462 | 2023-07-18 16:27:39 +0100 | [diff] [blame] | 1016 | * HIPAS is ASSIGNED. |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1017 | * |
| 1018 | * NOTE: For now, only the RTTE with PA are live. |
| 1019 | * This could change with EXPORT/IMPORT support. |
| 1020 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1021 | static bool s2tte_is_live(const struct s2tt_context *s2_ctx, |
| 1022 | unsigned long s2tte, long level) |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1023 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1024 | return s2tte_has_pa(s2_ctx, s2tte, level); |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1027 | /* Returns physical address of a S2TTE */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1028 | unsigned long s2tte_pa(const struct s2tt_context *s2_ctx, unsigned long s2tte, |
| 1029 | long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1030 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1031 | bool lpa2; |
| 1032 | unsigned long pa; |
| 1033 | __unused long min_starting_level; |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1034 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1035 | assert(s2_ctx != NULL); |
| 1036 | |
| 1037 | min_starting_level = (s2_ctx->enable_lpa2 == true) ? |
| 1038 | S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL; |
| 1039 | assert(level >= min_starting_level); |
| 1040 | assert(level <= S2TT_PAGE_LEVEL); |
| 1041 | assert(s2tte_has_pa(s2_ctx, s2tte, level)); |
| 1042 | |
| 1043 | lpa2 = s2_ctx->enable_lpa2; |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1044 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1045 | if (s2tte_is_table(s2_ctx, s2tte, level)) { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1046 | pa = table_entry_to_phys(s2tte, lpa2); |
| 1047 | } else { |
| 1048 | pa = s2tte_to_pa(s2tte, level, lpa2); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1051 | return pa; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1054 | bool s2tte_is_addr_lvl_aligned(const struct s2tt_context *s2_ctx, |
| 1055 | unsigned long addr, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1056 | { |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1057 | assert(s2_ctx != NULL); |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1058 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1059 | __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ? |
| 1060 | S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL; |
| 1061 | unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level); |
| 1062 | unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT; |
| 1063 | unsigned long s2tte_oa_bits = (s2_ctx->enable_lpa2 == true) ? |
| 1064 | S2TTE_OA_BITS_LPA2 : S2TTE_OA_BITS; |
| 1065 | |
| 1066 | assert(level <= S2TT_PAGE_LEVEL); |
| 1067 | assert(level >= min_starting_level); |
| 1068 | |
| 1069 | return (addr == (addr & BIT_MASK_ULL((s2tte_oa_bits - 1U), lsb))); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1072 | typedef bool (*s2tte_type_checker)(const struct s2tt_context *s2_ctx, |
| 1073 | unsigned long s2tte); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1074 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1075 | static bool table_is_uniform_block(const struct s2tt_context *s2_ctx, |
| 1076 | unsigned long *table, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1077 | s2tte_type_checker s2tte_is_x) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1078 | { |
AlexeiFedorov | 377d81f | 2023-04-14 16:29:12 +0100 | [diff] [blame] | 1079 | for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) { |
| 1080 | unsigned long s2tte = s2tte_read(&table[i]); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1081 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1082 | if (!s2tte_is_x(s2_ctx, s2tte)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1083 | return false; |
| 1084 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | return true; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1091 | * Returns true if all s2ttes in @table are unassigned_empty. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1092 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1093 | bool s2tt_is_unassigned_empty_block(const struct s2tt_context *s2_ctx, |
| 1094 | unsigned long *table) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1095 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1096 | assert(table != NULL); |
| 1097 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1098 | return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_empty); |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Returns true if all s2ttes in @table are unassigned_ram. |
| 1103 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1104 | bool s2tt_is_unassigned_ram_block(const struct s2tt_context *s2_ctx, |
| 1105 | unsigned long *table) |
AlexeiFedorov | c07a638 | 2023-04-14 11:59:18 +0100 | [diff] [blame] | 1106 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1107 | assert(table != NULL); |
| 1108 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1109 | return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ram); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 1113 | * Returns true if all s2ttes in @table are unassigned_ns |
| 1114 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1115 | bool s2tt_is_unassigned_ns_block(const struct s2tt_context *s2_ctx, |
| 1116 | unsigned long *table) |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 1117 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1118 | assert(table != NULL); |
| 1119 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1120 | return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ns); |
AlexeiFedorov | 5ceff35 | 2023-04-12 16:17:00 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* |
AlexeiFedorov | c53b1f7 | 2023-07-04 15:37:03 +0100 | [diff] [blame] | 1124 | * Returns true if all s2ttes in @table are unassigned_destroyed |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1125 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1126 | bool s2tt_is_unassigned_destroyed_block(const struct s2tt_context *s2_ctx, |
| 1127 | unsigned long *table) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1128 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1129 | assert(table != NULL); |
| 1130 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1131 | return table_is_uniform_block(s2_ctx, table, |
| 1132 | s2tte_is_unassigned_destroyed); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1135 | typedef bool (*s2tte_type_level_checker)(const struct s2tt_context *s2_ctx, |
| 1136 | unsigned long s2tte, long level); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1137 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1138 | static bool table_maps_block(const struct s2tt_context *s2_ctx, |
| 1139 | unsigned long *table, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1140 | long level, |
| 1141 | s2tte_type_level_checker s2tte_is_x, |
| 1142 | bool check_ns_attrs) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1143 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1144 | assert(table != NULL); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1145 | assert(s2_ctx != NULL); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1146 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1147 | unsigned long base_pa, ns_attr_host_mask; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1148 | unsigned long map_size = s2tte_map_size(level); |
| 1149 | unsigned long s2tte = s2tte_read(&table[0]); |
| 1150 | unsigned int i; |
| 1151 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1152 | if (!s2tte_is_x(s2_ctx, s2tte, level)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
| 1155 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1156 | base_pa = s2tte_pa(s2_ctx, s2tte, level); |
| 1157 | if (!s2tte_is_addr_lvl_aligned(s2_ctx, base_pa, level - 1L)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1158 | return false; |
| 1159 | } |
| 1160 | |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1161 | ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ? |
| 1162 | S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK; |
| 1163 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1164 | for (i = 1U; i < S2TTES_PER_S2TT; i++) { |
| 1165 | unsigned long expected_pa = base_pa + (i * map_size); |
| 1166 | |
| 1167 | s2tte = s2tte_read(&table[i]); |
| 1168 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1169 | if (!s2tte_is_x(s2_ctx, s2tte, level)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1170 | return false; |
| 1171 | } |
| 1172 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1173 | if (s2tte_pa(s2_ctx, s2tte, level) != expected_pa) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1176 | |
| 1177 | if (check_ns_attrs) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1178 | unsigned long ns_attrs = |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1179 | s2tte & ns_attr_host_mask; |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1180 | |
| 1181 | /* |
| 1182 | * We match all the attributes in the S2TTE |
| 1183 | * except for the AF bit. |
| 1184 | */ |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1185 | if ((s2tte & ns_attr_host_mask) != ns_attrs) { |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1186 | return false; |
| 1187 | } |
| 1188 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 1195 | * Returns true if all s2ttes are assigned_empty |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1196 | * and refer to a contiguous block of granules aligned to @level - 1. |
| 1197 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1198 | bool s2tt_maps_assigned_empty_block(const struct s2tt_context *s2_ctx, |
| 1199 | unsigned long *table, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1200 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1201 | return table_maps_block(s2_ctx, table, level, |
| 1202 | s2tte_is_assigned_empty, false); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 1206 | * Returns true if all s2ttes are assigned_ram and |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1207 | * refer to a contiguous block of granules aligned to @level - 1. |
| 1208 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1209 | bool s2tt_maps_assigned_ram_block(const struct s2tt_context *s2_ctx, |
| 1210 | unsigned long *table, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1211 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1212 | return table_maps_block(s2_ctx, table, level, |
| 1213 | s2tte_is_assigned_ram, false); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* |
Javier Almansa Sobrino | 15fc44e | 2023-09-29 13:52:04 +0100 | [diff] [blame] | 1217 | * Returns true if |
| 1218 | * - all s2ttes in @table are assigned_ns s2ttes and |
| 1219 | * - they refer to a contiguous block of granules aligned to @level - 1 and |
| 1220 | * - all the s2tte attributes in @table controlled by the host are identical |
AlexeiFedorov | 3a73933 | 2023-04-13 13:54:04 +0100 | [diff] [blame] | 1221 | * |
| 1222 | * @pre: @table maps IPA outside PAR. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1223 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1224 | bool s2tt_maps_assigned_ns_block(const struct s2tt_context *s2_ctx, |
| 1225 | unsigned long *table, long level) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1226 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1227 | return table_maps_block(s2_ctx, table, level, |
| 1228 | s2tte_is_assigned_ns, true); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1229 | } |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1230 | |
| 1231 | /* |
AlexeiFedorov | 3f840a0 | 2023-07-19 10:55:05 +0100 | [diff] [blame] | 1232 | * Returns true if all s2ttes are assigned_destroyed and |
| 1233 | * refer to a contiguous block of granules aligned to @level - 1. |
| 1234 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1235 | bool s2tt_maps_assigned_destroyed_block(const struct s2tt_context *s2_ctx, |
| 1236 | unsigned long *table, long level) |
AlexeiFedorov | 3f840a0 | 2023-07-19 10:55:05 +0100 | [diff] [blame] | 1237 | { |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1238 | return table_maps_block(s2_ctx, table, level, |
| 1239 | s2tte_is_assigned_destroyed, false); |
AlexeiFedorov | 3f840a0 | 2023-07-19 10:55:05 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /* |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1243 | * Scan the RTT @s2tt (which is @wi.level), from the entry (@wi.index) and |
AlexeiFedorov | 3ebd462 | 2023-07-18 16:27:39 +0100 | [diff] [blame] | 1244 | * skip the non-live entries (i.e., HIPAS=UNASSIGNED). |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1245 | * In other words, the scanning stops when a live RTTE is encountered or we |
| 1246 | * reach the end of this RTT. |
| 1247 | * |
| 1248 | * For now an RTTE can be considered non-live if it doesn't have a PA. |
| 1249 | * NOTE: This would change with EXPORT/IMPORT where we may have metadata stored |
| 1250 | * in the RTTE. |
| 1251 | * |
| 1252 | * @addr is not necessarily aligned to the wi.last_level (e.g., if we were called |
| 1253 | * with RMI_ERROR_RTT). |
| 1254 | * |
| 1255 | * Returns: |
| 1256 | * - If the entry @wi.index is live, returns @addr. |
| 1257 | * - If none of the entries in the @s2tt are "live", returns the address of the |
| 1258 | * first entry in the next table. |
| 1259 | * - Otherwise, the address of the first live entry in @s2tt |
| 1260 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1261 | unsigned long s2tt_skip_non_live_entries(const struct s2tt_context *s2_ctx, |
| 1262 | unsigned long addr, |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1263 | unsigned long *table, |
| 1264 | const struct s2tt_walk *wi) |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1265 | { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1266 | assert(table != NULL); |
| 1267 | assert(wi != NULL); |
| 1268 | assert(wi->index <= S2TTES_PER_S2TT); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1269 | assert(wi->last_level <= S2TT_PAGE_LEVEL); |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1270 | assert(s2_ctx != NULL); |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1271 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 1272 | unsigned long i, index = wi->index; |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1273 | long level = wi->last_level; |
| 1274 | unsigned long map_size; |
Javier Almansa Sobrino | f6fff69 | 2024-02-02 17:13:57 +0000 | [diff] [blame] | 1275 | __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ? |
| 1276 | S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL; |
| 1277 | |
| 1278 | assert(wi->last_level >= min_starting_level); |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1279 | |
| 1280 | /* |
| 1281 | * If the entry for the map_addr is live, |
| 1282 | * return @addr. |
| 1283 | */ |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1284 | if (s2tte_is_live(s2_ctx, s2tte_read(&table[index]), level)) { |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1285 | return addr; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Align the address DOWN to the map_size, as expected for the @level, |
| 1290 | * so that we can compute the correct address by using the index. |
| 1291 | */ |
| 1292 | map_size = s2tte_map_size(level); |
| 1293 | addr &= ~(map_size - 1UL); |
| 1294 | |
| 1295 | /* Skip the "index" */ |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 1296 | for (i = index + 1UL; i < S2TTES_PER_S2TT; i++) { |
Javier Almansa Sobrino | 1e1781e | 2023-10-18 18:25:56 +0100 | [diff] [blame] | 1297 | unsigned long s2tte = s2tte_read(&table[i]); |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1298 | |
Javier Almansa Sobrino | 2595cd8 | 2024-01-25 18:25:12 +0000 | [diff] [blame] | 1299 | if (s2tte_is_live(s2_ctx, s2tte, level)) { |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | |
AlexeiFedorov | 56e1a8e | 2023-09-01 17:06:13 +0100 | [diff] [blame] | 1304 | return (addr + ((i - index) * map_size)); |
AlexeiFedorov | 917eabf | 2023-04-24 12:20:41 +0100 | [diff] [blame] | 1305 | } |