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