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