blob: 5d54f3e5b0a2f36ed5ec163c6e443c42d9138b31 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <arch_helpers.h>
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00007#include <assert.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <bitmap.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00009#include <granule.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#include <ripas.h>
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010011#include <s2tt.h>
12#include <s2tt_pvt_defs.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000013#include <smc.h>
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +000014#include <stdbool.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <stddef.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000016
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +000017/*
18 * Return a mask for the IPA field on a S2TTE
19 */
20static unsigned long s2tte_lvl_mask(long level, bool lpa2)
21{
22 assert(level <= S2TT_PAGE_LEVEL);
23 assert(level >= S2TT_MIN_STARTING_LEVEL_LPA2);
24
25 unsigned long mask;
26 unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
27 unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
28
29 mask = BIT_MASK_ULL((S2TTE_OA_BITS - 1U), lsb);
30
31 if (lpa2 == true) {
32 mask |= (MASK(LPA2_S2TTE_51_50) | MASK(LPA2_OA_49_48));
33 }
34
35 return mask;
36}
37
38/*
39 * Extracts the PA mapped by an S2TTE, aligned to a given level.
40 */
41static unsigned long s2tte_to_pa(unsigned long s2tte, long level, bool lpa2)
42{
43 unsigned long pa = s2tte & s2tte_lvl_mask(level, lpa2);
44
45 if (lpa2 == true) {
46 pa &= ~MASK(LPA2_S2TTE_51_50);
47 pa |= INPLACE(LPA2_OA_51_50, EXTRACT(LPA2_S2TTE_51_50, s2tte));
48 }
49
50 return pa;
51}
Soby Mathewb4c6df42022-11-09 11:13:29 +000052
53/*
54 * Invalidates S2 TLB entries from [ipa, ipa + size] region tagged with `vmid`.
55 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +000056static void stage2_tlbi_ipa(const struct s2tt_context *s2_ctx,
Soby Mathewb4c6df42022-11-09 11:13:29 +000057 unsigned long ipa,
58 unsigned long size)
59{
60 /*
61 * Notes:
62 *
63 * - This follows the description provided in the Arm ARM on
64 * "Invalidation of TLB entries from stage 2 translations".
65 *
66 * - @TODO: Provide additional information to this primitive so that
67 * we can utilize:
68 * - The TTL level hint, see FEAT_TTL,
69 * - Final level lookup only invalidation,
70 * - Address range invalidation.
71 */
72
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010073 assert(s2_ctx != NULL);
74
Soby Mathewb4c6df42022-11-09 11:13:29 +000075 /*
76 * Save the current content of vttb_el2.
77 */
78 unsigned long old_vttbr_el2 = read_vttbr_el2();
79
80 /*
81 * Make 'vmid' the `current vmid`. Note that the tlbi instructions
82 * bellow target the TLB entries that match the `current vmid`.
83 */
84 write_vttbr_el2(INPLACE(VTTBR_EL2_VMID, s2_ctx->vmid));
85 isb();
86
87 /*
88 * Invalidate entries in S2 TLB caches that
89 * match both `ipa` & the `current vmid`.
90 */
91 while (size != 0UL) {
92 tlbiipas2e1is(ipa >> 12);
93 size -= GRANULE_SIZE;
94 ipa += GRANULE_SIZE;
95 }
96 dsb(ish);
97
98 /*
99 * The architecture does not require TLB invalidation by IPA to affect
100 * combined Stage-1 + Stage-2 TLBs. Therefore we must invalidate all of
101 * Stage-1 (tagged with the `current vmid`) after invalidating Stage-2.
102 */
103 tlbivmalle1is();
104 dsb(ish);
105 isb();
106
107 /*
108 * Restore the old content of vttb_el2.
109 */
110 write_vttbr_el2(old_vttbr_el2);
111 isb();
112}
113
114/*
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000115 * Returns true if s2tte has 'output address' field, namely, if it is one of:
116 * - assigned_empty
117 * - assigned_ram
118 * - assigned_ns
119 * - assigned_destroyed
120 * - table
121 */
122static bool s2tte_has_pa(const struct s2tt_context *s2_ctx,
123 unsigned long s2tte, long level)
124{
125 unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK;
126
127 return ((desc_type != S2TTE_INVALID) || /* block, page or table */
128 s2tte_is_assigned_empty(s2_ctx, s2tte, level) ||
129 s2tte_is_assigned_destroyed(s2_ctx, s2tte, level));
130}
131
132/*
133 * Creates a TTE containing only the PA.
134 * This function expects 'pa' to be aligned and bounded.
135 */
136static unsigned long pa_to_s2tte(unsigned long pa, bool lpa2)
137{
138 unsigned long tte = pa;
139
140 if (lpa2 == true) {
141 tte &= ~MASK(LPA2_OA_51_50);
142 tte |= INPLACE(LPA2_S2TTE_51_50, EXTRACT(LPA2_OA_51_50, pa));
143 }
144
145 return tte;
146}
147
148/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000149 * Invalidate S2 TLB entries with "addr" IPA.
150 * Call this function after:
151 * 1. A L3 page desc has been removed.
152 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000153void s2tt_invalidate_page(const struct s2tt_context *s2_ctx, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000154{
155 stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
156}
157
158/*
159 * Invalidate S2 TLB entries with "addr" IPA.
160 * Call this function after:
161 * 1. A L2 block desc has been removed, or
162 * 2a. A L2 table desc has been removed, where
163 * 2b. All S2TTEs in L3 table that the L2 table desc was pointed to were invalid.
164 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000165void s2tt_invalidate_block(const struct s2tt_context *s2_ctx, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000166{
167 stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
168}
169
170/*
171 * Invalidate S2 TLB entries with "addr" IPA.
172 * Call this function after:
173 * 1a. A L2 table desc has been removed, where
174 * 1b. Some S2TTEs in the table that the L2 table desc was pointed to were valid.
175 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000176void s2tt_invalidate_pages_in_block(const struct s2tt_context *s2_ctx,
177 unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000178{
179 stage2_tlbi_ipa(s2_ctx, addr, BLOCK_L2_SIZE);
180}
181
182/*
183 * Return the index of the entry describing @addr in the translation table at
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000184 * level @level. This only works for non-concatenated page tables, so should
Soby Mathewb4c6df42022-11-09 11:13:29 +0000185 * not be called to get the index for the starting level.
186 *
187 * See the library pseudocode
188 * aarch64/translation/vmsa_addrcalc/AArch64.TTEntryAddress on which this is
189 * modeled.
190 */
191static unsigned long s2_addr_to_idx(unsigned long addr, long level)
192{
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100193 unsigned int levels, lsb;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000194 unsigned int s2tte_stride = (level < S2TT_MIN_STARTING_LEVEL) ?
195 S2TTE_STRIDE_LM1 : S2TTE_STRIDE;
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100196
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100197 levels = (unsigned int)(S2TT_PAGE_LEVEL - level);
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100198 lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000199
200 addr >>= lsb;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000201 addr &= (1UL << s2tte_stride) - 1UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000202 return addr;
203}
204
205/*
206 * Return the index of the entry describing @addr in the translation table
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000207 * starting level. This may return an index >= S2TTES_PER_S2TT when the
Soby Mathewb4c6df42022-11-09 11:13:29 +0000208 * combination of @start_level and @ipa_bits implies concatenated
209 * stage 2 tables.
210 *
211 * See the library pseudocode
212 * aarch64/translation/vmsa_addrcalc/AArch64.S2SLTTEntryAddress on which
213 * this is modeled.
214 */
215static unsigned long s2_sl_addr_to_idx(unsigned long addr, int start_level,
216 unsigned long ipa_bits)
217{
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100218 unsigned int levels, lsb;
219
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100220 levels = (unsigned int)(S2TT_PAGE_LEVEL - start_level);
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100221 lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000222
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000223 addr &= ((1UL << ipa_bits) - 1UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000224 addr >>= lsb;
225 return addr;
226}
227
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000228static bool entry_is_table(unsigned long entry)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000229{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100230 return ((entry & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000231}
232
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000233static unsigned long table_get_entry(const struct s2tt_context *s2_ctx,
234 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100235 unsigned long idx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000236{
237 unsigned long *table, entry;
238
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000239 (void)s2_ctx;
240
Soby Mathewb4c6df42022-11-09 11:13:29 +0000241 table = granule_map(g_tbl, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100242 assert(table != NULL);
243
Soby Mathewb4c6df42022-11-09 11:13:29 +0000244 entry = s2tte_read(&table[idx]);
245 buffer_unmap(table);
246
247 return entry;
248}
249
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000250#define table_entry_to_phys(tte, lpa2) \
251 s2tte_to_pa(tte, S2TT_PAGE_LEVEL, lpa2)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100252
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000253static struct granule *find_next_level_idx(const struct s2tt_context *s2_ctx,
254 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100255 unsigned long idx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000256{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000257 assert(s2_ctx != NULL);
258
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000259 const unsigned long entry = table_get_entry(s2_ctx, g_tbl, idx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000260
261 if (!entry_is_table(entry)) {
262 return NULL;
263 }
264
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000265 return addr_to_granule(table_entry_to_phys(entry, s2_ctx->enable_lpa2));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000266}
267
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000268static struct granule *find_lock_next_level(const struct s2tt_context *s2_ctx,
269 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100270 unsigned long map_addr,
271 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000272{
273 const unsigned long idx = s2_addr_to_idx(map_addr, level);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000274 struct granule *g = find_next_level_idx(s2_ctx, g_tbl, idx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000275
276 if (g != NULL) {
277 granule_lock(g, GRANULE_STATE_RTT);
278 }
279
280 return g;
281}
282
283/*
284 * Walk an RTT until level @level using @map_addr.
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000285 * @g_root is the root (level 0/-1) table and must be locked before the call.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000286 * @start_level is the initial lookup level used for the stage 2 translation
287 * tables which may depend on the configuration of the realm, factoring in the
288 * IPA size of the realm and the desired starting level (within the limits
289 * defined by the Armv8 VMSA including options for stage 2 table concatenation).
290 * The function uses hand-over-hand locking to avoid race conditions and allow
291 * concurrent access to RTT tree which is not part of the current walk; when a
292 * next level table is reached it is locked before releasing previously locked
293 * table.
294 * The walk stops when either:
295 * - The entry found is a leaf entry (not an RTT Table entry), or
296 * - Level @level is reached.
297 *
298 * On return:
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100299 * - s2tt_walk::last_level is the last level that has been reached by the walk.
300 * - s2tt_walk.g_llt points to the TABLE granule at level @s2tt_walk::level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000301 * The granule is locked.
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000302 * - s2tt_walk::index is the entry index at s2tt_walk.g_llt for @map_addr.i
303 *
304 * NOTE: This function expects that the root table on the s2 context is
305 * already locked.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000306 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000307void s2tt_walk_lock_unlock(const struct s2tt_context *s2_ctx,
308 unsigned long map_addr,
309 long level,
310 struct s2tt_walk *wi)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000311{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000312 struct granule *g_tbls[NR_RTT_LEVELS_LPA2] = { (struct granule *)NULL };
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000313 struct granule *g_root;
314 unsigned long sl_idx, ipa_bits;
315 int i, start_level, last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000316
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000317 assert(s2_ctx != NULL);
318
319 start_level = s2_ctx->s2_starting_level;
320 ipa_bits = s2_ctx->ipa_bits;
321
Soby Mathewb4c6df42022-11-09 11:13:29 +0000322 assert(level >= start_level);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100323 assert(level <= S2TT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000324 assert(map_addr < (1UL << ipa_bits));
325 assert(wi != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000326
327 g_root = s2_ctx->g_rtt;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000328
329 /* Handle concatenated starting level (SL) tables */
330 sl_idx = s2_sl_addr_to_idx(map_addr, start_level, ipa_bits);
331 if (sl_idx >= S2TTES_PER_S2TT) {
AlexeiFedorov4faab852023-08-30 15:06:49 +0100332 unsigned int tt_num = (unsigned int)(sl_idx >> S2TTE_STRIDE);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100333 struct granule *g_concat_root;
334
335 assert(tt_num < S2TTE_MAX_CONCAT_TABLES);
336
337 g_concat_root = (struct granule *)((uintptr_t)g_root +
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000338 (tt_num * sizeof(struct granule)));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000339
340 granule_lock(g_concat_root, GRANULE_STATE_RTT);
341 granule_unlock(g_root);
342 g_root = g_concat_root;
343 }
344
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000345 /* 'start_level' can be '-1', so add 1 when used as an index */
346 g_tbls[start_level + 1] = g_root;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000347 for (i = start_level; i < level; i++) {
348 /*
349 * Lock next RTT level. Correct locking order is guaranteed
350 * because reference is obtained from a locked granule
351 * (previous level). Also, hand-over-hand locking/unlocking is
352 * used to avoid race conditions.
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000353 *
354 * Note that as 'start_level' can be -1, we add '1' to the
355 * index 'i' to compensate for the negative value when we
356 * use it to index then 'g_tbls' list.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000357 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000358 g_tbls[i + 1 + 1] = find_lock_next_level(s2_ctx, g_tbls[i + 1],
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000359 map_addr, i);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000360 if (g_tbls[i + 1 + 1] == NULL) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000361 last_level = i;
362 goto out;
363 }
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000364 granule_unlock(g_tbls[i + 1]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000365 }
366
AlexeiFedorov4faab852023-08-30 15:06:49 +0100367 last_level = (int)level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000368out:
369 wi->last_level = last_level;
AlexeiFedorove956db32024-04-04 14:36:21 +0100370 /* coverity[deref_overflow:SUPPRESS] */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000371 wi->g_llt = g_tbls[last_level + 1];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000372 wi->index = s2_addr_to_idx(map_addr, last_level);
373}
374
375/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100376 * Creates an unassigned_empty s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000377 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000378unsigned long s2tte_create_unassigned_empty(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000379{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000380 (void)s2_ctx;
381
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100382 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_EMPTY);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100383}
384
385/*
386 * Creates an unassigned_ram s2tte.
387 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000388unsigned long s2tte_create_unassigned_ram(const struct s2tt_context *s2_ctx)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100389{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000390 (void)s2_ctx;
391
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100392 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000393}
394
395/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100396 * Creates an unassigned_destroyed s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000397 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000398unsigned long s2tte_create_unassigned_destroyed(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000399{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000400 (void)s2_ctx;
401
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100402 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_DESTROYED);
403}
404
405/*
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100406 * Creates an unassigned_ns s2tte.
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100407 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000408unsigned long s2tte_create_unassigned_ns(const struct s2tt_context *s2_ctx)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100409{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000410 (void)s2_ctx;
411
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100412 return (S2TTE_NS | S2TTE_INVALID_HIPAS_UNASSIGNED |
413 S2TTE_INVALID_UNPROTECTED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000414}
415
416/*
417 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100418 * RIPAS=@s2tte_ripas, at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000419 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000420static unsigned long s2tte_create_assigned(const struct s2tt_context *s2_ctx,
421 unsigned long pa, long level,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100422 unsigned long s2tte_ripas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000423{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100424 assert(level >= S2TT_MIN_BLOCK_LEVEL);
425 assert(level <= S2TT_PAGE_LEVEL);
426 assert(s2tte_ripas <= S2TTE_INVALID_RIPAS_DESTROYED);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000427 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000428 assert(s2_ctx != NULL);
429
430 unsigned long tte = pa_to_s2tte(pa, s2_ctx->enable_lpa2);
431 unsigned long s2tte_page, s2tte_block;
432
433 if (s2_ctx->enable_lpa2 == true) {
434 s2tte_page = S2TTE_PAGE_LPA2;
435 s2tte_block = S2TTE_BLOCK_LPA2;
436 } else {
437 s2tte_page = S2TTE_PAGE;
438 s2tte_block = S2TTE_BLOCK;
439 }
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100440
441 if (s2tte_ripas == S2TTE_INVALID_RIPAS_RAM) {
442 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000443 return (tte | s2tte_page);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100444 }
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000445 return (tte | s2tte_block);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100446 }
447
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000448 return (tte | S2TTE_INVALID_HIPAS_ASSIGNED | s2tte_ripas);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100449}
450
451/*
452 * Creates and invalid s2tte with output address @pa, HIPAS=ASSIGNED and
453 * RIPAS=DESTROYED at level @level.
454 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000455unsigned long s2tte_create_assigned_destroyed(const struct s2tt_context *s2_ctx,
456 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100457{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000458 return s2tte_create_assigned(s2_ctx, pa, level,
459 S2TTE_INVALID_RIPAS_DESTROYED);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100460}
461
462/*
463 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
464 * RIPAS=EMPTY at level @level.
465 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000466unsigned long s2tte_create_assigned_empty(const struct s2tt_context *s2_ctx,
467 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100468{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000469 return s2tte_create_assigned(s2_ctx, pa, level,
470 S2TTE_INVALID_RIPAS_EMPTY);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000471}
472
473/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100474 * Creates an assigned_ram s2tte with output address @pa.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000475 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000476unsigned long s2tte_create_assigned_ram(const struct s2tt_context *s2_ctx,
477 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000478{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000479 return s2tte_create_assigned(s2_ctx, pa, level,
480 S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000481}
482
483/*
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100484 * Creates an assigned s2tte with output address @pa and the same
485 * RIPAS as passed on @s2tte.
486 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000487unsigned long s2tte_create_assigned_unchanged(const struct s2tt_context *s2_ctx,
488 unsigned long s2tte,
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100489 unsigned long pa,
490 long level)
491{
492 unsigned long current_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
493
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000494 return s2tte_create_assigned(s2_ctx, pa, level, current_ripas);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000495}
496
497/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100498 * Creates an assigned_ns s2tte at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000499 *
500 * The following S2 TTE fields are provided through @s2tte argument:
501 * - The physical address
502 * - MemAttr
503 * - S2AP
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000504 * - Shareability (when FEAT_LPA2 is disabled)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000505 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000506unsigned long s2tte_create_assigned_ns(const struct s2tt_context *s2_ctx,
507 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000508{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000509 /*
510 * We just mask out the DESC_TYPE below. The Shareability bits
511 * without FEAT_LPA2 are at the same position as OA bits [51:50]
512 * with FEAT_LPA2 enabled, so we don't need to cater for that
513 * separately.
514 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100515 unsigned long new_s2tte = s2tte & ~S2TT_DESC_TYPE_MASK;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100516
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000517 (void)s2_ctx;
518
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100519 assert(level >= S2TT_MIN_BLOCK_LEVEL);
520 assert(level <= S2TT_PAGE_LEVEL);
521 assert((s2tte & S2TTE_NS_ATTR_RMM) == 0UL);
522
523 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100524 return (new_s2tte | S2TTE_PAGE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000525 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100526 return (new_s2tte | S2TTE_BLOCK_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000527}
528
529/*
530 * Validate the portion of NS S2TTE that is provided by the host.
531 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000532bool host_ns_s2tte_is_valid(const struct s2tt_context *s2_ctx,
533 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000534{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100535
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000536 bool lpa2;
537 unsigned long mask;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100538
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000539 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100540 assert(level >= S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000541
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000542 lpa2 = s2_ctx->enable_lpa2;
543
544 mask = s2tte_lvl_mask(level, lpa2);
545 if (lpa2 == true) {
546 mask |= S2TTE_NS_ATTR_LPA2_MASK;
547 } else {
548 mask |= S2TTE_NS_ATTR_MASK;
549
550 /* Only SH_IS is allowed */
551 if ((s2tte & S2TTE_SH_MASK) != S2TTE_SH_IS) {
552 return false;
553 }
554 }
555
Soby Mathewb4c6df42022-11-09 11:13:29 +0000556 /*
557 * Test that all fields that are not controlled by the host are zero
558 * and that the output address is correctly aligned. Note that
559 * the host is permitted to map any physical address outside PAR.
560 */
561 if ((s2tte & ~mask) != 0UL) {
562 return false;
563 }
564
565 /*
566 * Only one value masked by S2TTE_MEMATTR_MASK is invalid/reserved.
567 */
568 if ((s2tte & S2TTE_MEMATTR_MASK) == S2TTE_MEMATTR_FWB_RESERVED) {
569 return false;
570 }
571
572 /*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000573 * Note that all the values that are masked by S2TTE_AP_MASK are valid.
574 */
575 return true;
576}
577
578/*
579 * Returns the portion of NS S2TTE that is set by the host.
580 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000581unsigned long host_ns_s2tte(const struct s2tt_context *s2_ctx,
582 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000583{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000584 assert(s2_ctx != NULL);
585
586 unsigned long mask = s2tte_lvl_mask(level, s2_ctx->enable_lpa2);
587
588 mask |= (s2_ctx->enable_lpa2 == true) ? S2TTE_NS_ATTR_LPA2_MASK :
589 S2TTE_NS_ATTR_MASK;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000590
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100591 assert(level >= S2TT_MIN_BLOCK_LEVEL);
592
Soby Mathewb4c6df42022-11-09 11:13:29 +0000593 return (s2tte & mask);
594}
595
596/*
597 * Creates a table s2tte at level @level with output address @pa.
598 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000599unsigned long s2tte_create_table(const struct s2tt_context *s2_ctx,
600 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000601{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000602 __unused int min_starting_level;
603
Chuyue Luo5ef71d92023-10-24 14:29:20 +0100604 (void)level;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000605
606 assert(s2_ctx != NULL);
607 min_starting_level = (s2_ctx->enable_lpa2 == true) ?
608 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
Chuyue Luo5ef71d92023-10-24 14:29:20 +0100609
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100610 assert(level < S2TT_PAGE_LEVEL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000611 assert(level >= min_starting_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000612 assert(GRANULE_ALIGNED(pa));
613
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000614 return (pa_to_s2tte(pa, s2_ctx->enable_lpa2) | S2TTE_TABLE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000615}
616
617/*
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100618 * Returns true if s2tte has defined ripas value, namely if it is one of:
619 * - unassigned_empty
620 * - unassigned_ram
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100621 * - unassigned_destroyed
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100622 * - assigned_empty
623 * - assigned_ram
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100624 * - assigned_destroyed
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100625 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000626bool s2tte_has_ripas(const struct s2tt_context *s2_ctx,
627 unsigned long s2tte, long level)
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100628{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000629 return (((s2tte & S2TTE_NS) == 0UL) && !s2tte_is_table(s2_ctx,
630 s2tte, level));
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100631}
632
633/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000634 * Returns true if @s2tte has HIPAS=@hipas.
635 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100636static inline bool s2tte_has_hipas(unsigned long s2tte, unsigned long hipas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000637{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100638 unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000639 unsigned long invalid_desc_hipas = s2tte & S2TTE_INVALID_HIPAS_MASK;
640
AlexeiFedorov63614ea2023-07-14 17:07:20 +0100641 return ((desc_type == S2TTE_INVALID) && (invalid_desc_hipas == hipas));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000642}
643
644/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100645 * Returns true if @s2tte has HIPAS=UNASSIGNED and RIPAS=@ripas.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000646 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100647static inline bool s2tte_has_unassigned_ripas(unsigned long s2tte,
648 unsigned long ripas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000649{
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100650 return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) &&
651 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED));
652}
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100653
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100654/*
655 * Returns true if @s2tte has HIPAS=ASSIGNED and RIPAS=@ripas.
656 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100657static inline bool s2tte_has_assigned_ripas(unsigned long s2tte,
658 unsigned long ripas)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100659{
660 return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) &&
661 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_ASSIGNED));
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100662}
663
664/*
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100665 * Returns true if @s2tte has HIPAS=UNASSIGNED.
666 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000667bool s2tte_is_unassigned(const struct s2tt_context *s2_ctx, unsigned long s2tte)
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100668{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000669 (void)s2_ctx;
670
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100671 return s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED);
672}
673
674/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100675 * Returns true if @s2tte is an unassigned_empty.
676 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000677bool s2tte_is_unassigned_empty(const struct s2tt_context *s2_ctx,
678 unsigned long s2tte)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100679{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000680 (void)s2_ctx;
681
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100682 return (((s2tte & S2TTE_NS) == 0UL) &&
683 s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY));
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100684}
685
686/*
687 * Returns true if @s2tte is an unassigned_ram.
688 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000689bool s2tte_is_unassigned_ram(const struct s2tt_context *s2_ctx,
690 unsigned long s2tte)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100691{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000692 (void)s2_ctx;
693
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100694 return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_RAM);
695}
696
697/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100698 * Returns true if @s2tte is unassigned_ns.
699 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000700bool s2tte_is_unassigned_ns(const struct s2tt_context *s2_ctx,
701 unsigned long s2tte)
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100702{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000703 (void)s2_ctx;
704
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100705 return (((s2tte & S2TTE_NS) != 0UL) &&
706 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000707}
708
709/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100710 * Returns true if @s2tte has RIPAS=DESTROYED.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000711 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000712bool s2tte_is_unassigned_destroyed(const struct s2tt_context *s2_ctx,
713 unsigned long s2tte)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000714{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000715 (void)s2_ctx;
716
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100717 return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED);
718}
719
720/*
721 * Returns true if @s2tte is an assigned_destroyed.
722 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000723bool s2tte_is_assigned_destroyed(const struct s2tt_context *s2_ctx,
724 unsigned long s2tte, long level)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100725{
726 (void)level;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000727 (void)s2_ctx;
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100728
729 return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000730}
731
732/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100733 * Returns true if @s2tte is an assigned_empty.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000734 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000735bool s2tte_is_assigned_empty(const struct s2tt_context *s2_ctx,
736 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000737{
738 (void)level;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000739 (void)s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000740
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100741 return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000742}
743
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000744static bool s2tte_check(const struct s2tt_context *s2_ctx, unsigned long s2tte,
745 long level, unsigned long ns)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000746{
747 unsigned long desc_type;
748
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000749 (void)s2_ctx;
750
Soby Mathewb4c6df42022-11-09 11:13:29 +0000751 if ((s2tte & S2TTE_NS) != ns) {
752 return false;
753 }
754
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100755 desc_type = s2tte & S2TT_DESC_TYPE_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000756
757 /* Only pages at L3 and valid blocks at L2 allowed */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100758 if (((level == S2TT_PAGE_LEVEL) && (desc_type == S2TTE_L3_PAGE)) ||
759 ((level == S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK))) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000760 return true;
761 }
762
763 return false;
764}
765
766/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100767 * Returns true if @s2tte is an assigned_ram.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000768 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000769bool s2tte_is_assigned_ram(const struct s2tt_context *s2_ctx,
770 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000771{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000772 return s2tte_check(s2_ctx, s2tte, level, 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000773}
774
775/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100776 * Returns true if @s2tte is an assigned_ns s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000777 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000778bool s2tte_is_assigned_ns(const struct s2tt_context *s2_ctx,
779 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000780{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000781 return s2tte_check(s2_ctx, s2tte, level, S2TTE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000782}
783
784/*
785 * Returns true if @s2tte is a table at level @level.
786 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000787bool s2tte_is_table(const struct s2tt_context *s2_ctx, unsigned long s2tte,
788 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000789{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000790 (void)s2_ctx;
791
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100792 return ((level < S2TT_PAGE_LEVEL) &&
793 ((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000794}
795
796/*
797 * Returns RIPAS of @s2tte.
798 *
799 * Caller should ensure that HIPAS=UNASSIGNED or HIPAS=ASSIGNED.
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100800 * The s2tte, if valid, should correspond to RIPAS_RAM.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000801 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000802enum ripas s2tte_get_ripas(const struct s2tt_context *s2_ctx, unsigned long s2tte)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000803{
804 unsigned long desc_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
805
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000806 (void)s2_ctx;
807
Soby Mathewb4c6df42022-11-09 11:13:29 +0000808 /*
809 * If valid s2tte descriptor is passed, then ensure S2AP[0]
810 * bit is 1 (S2AP is set to RW for lower EL), which corresponds
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100811 * to RIPAS_RAM (bits[6:5] = b01) on a valid descriptor.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000812 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100813 assert(((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_INVALID) ||
814 (desc_ripas == S2TTE_INVALID_RIPAS_RAM));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000815
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100816 assert(EXTRACT(S2TTE_INVALID_HIPAS, s2tte) <=
817 EXTRACT(S2TTE_INVALID_HIPAS, S2TTE_INVALID_HIPAS_ASSIGNED));
818
819 desc_ripas = desc_ripas >> S2TTE_INVALID_RIPAS_SHIFT;
820
821 assert(desc_ripas <= (unsigned int)RIPAS_DESTROYED);
822
823 return (enum ripas)desc_ripas;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000824}
825
826/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100827 * Populates @s2tt with unassigned_empty s2ttes.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000828 *
829 * The granule is populated before it is made a table,
830 * hence, don't use s2tte_write for access.
831 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000832void s2tt_init_unassigned_empty(const struct s2tt_context *s2_ctx,
833 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000834{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100835 assert(s2tt != NULL);
836
837 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000838 s2tte_create_unassigned_empty(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100839
Soby Mathewb4c6df42022-11-09 11:13:29 +0000840 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100841 s2tt[i] = s2tte;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100842 }
843
844 dsb(ish);
845}
846
847/*
848 * Populates @s2tt with unassigned_ram s2ttes.
849 *
850 * The granule is populated before it is made a table,
851 * hence, don't use s2tte_write for access.
852 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000853void s2tt_init_unassigned_ram(const struct s2tt_context *s2_ctx,
854 unsigned long *s2tt)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100855{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100856 assert(s2tt != NULL);
857
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000858 unsigned long s2tte = s2tte_create_unassigned_ram(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100859
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100860 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100861 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000862 }
863
864 dsb(ish);
865}
866
867/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100868 * Populates @s2tt with unassigned_ns s2ttes.
869 *
870 * The granule is populated before it is made a table,
871 * hence, don't use s2tte_write for access.
872 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000873void s2tt_init_unassigned_ns(const struct s2tt_context *s2_ctx,
874 unsigned long *s2tt)
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100875{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100876 assert(s2tt != NULL);
877
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000878 unsigned long s2tte = s2tte_create_unassigned_ns(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100879
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100880 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100881 s2tt[i] = s2tte;
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100882 }
883
884 dsb(ish);
885}
886
887/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000888 * Populates @s2tt with s2ttes which have HIPAS=DESTROYED.
889 *
890 * The granule is populated before it is made a table,
891 * hence, don't use s2tte_write for access.
892 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000893void s2tt_init_unassigned_destroyed(const struct s2tt_context *s2_ctx,
894 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000895{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100896 assert(s2tt != NULL);
897
898 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000899 s2tte_create_unassigned_destroyed(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100900
Soby Mathewb4c6df42022-11-09 11:13:29 +0000901 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100902 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000903 }
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100904 dsb(ish);
905}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000906
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100907/*
908 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=DESTROYED s2ttes that refer to a
909 * contiguous memory block starting at @pa, and mapped at level @level.
910 *
911 * The granule is populated before it is made a table,
912 * hence, don't use s2tte_write for access.
913 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000914void s2tt_init_assigned_destroyed(const struct s2tt_context *s2_ctx,
915 unsigned long *s2tt, unsigned long pa,
916 long level)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100917{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000918 assert(s2tt != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100919 assert(level >= S2TT_MIN_BLOCK_LEVEL);
920 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000921 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100922
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100923 const unsigned long map_size = s2tte_map_size(level);
924
925 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000926 s2tt[i] = s2tte_create_assigned_destroyed(s2_ctx, pa, level);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100927 pa += map_size;
928 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000929 dsb(ish);
930}
931
Soby Mathewb4c6df42022-11-09 11:13:29 +0000932/*
933 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=EMPTY s2ttes that refer to a
934 * contiguous memory block starting at @pa, and mapped at level @level.
935 *
936 * The granule is populated before it is made a table,
937 * hence, don't use s2tte_write for access.
938 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000939void s2tt_init_assigned_empty(const struct s2tt_context *s2_ctx,
940 unsigned long *s2tt, unsigned long pa,
941 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000942{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100943 assert(level >= S2TT_MIN_BLOCK_LEVEL);
944 assert(level <= S2TT_PAGE_LEVEL);
945 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000946 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100947
Soby Mathewb4c6df42022-11-09 11:13:29 +0000948 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000949
AlexeiFedorov3a739332023-04-13 13:54:04 +0100950 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000951 s2tt[i] = s2tte_create_assigned_empty(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000952 pa += map_size;
953 }
954 dsb(ish);
955}
956
957/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100958 * Populates @s2tt with assigned_ram s2ttes that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000959 * contiguous memory block starting at @pa, and mapped at level @level.
960 *
961 * The granule is populated before it is made a table,
962 * hence, don't use s2tte_write for access.
963 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000964void s2tt_init_assigned_ram(const struct s2tt_context *s2_ctx,
965 unsigned long *s2tt, unsigned long pa,
966 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000967{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100968 assert(level >= S2TT_MIN_BLOCK_LEVEL);
969 assert(level <= S2TT_PAGE_LEVEL);
970 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000971 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100972
Soby Mathewb4c6df42022-11-09 11:13:29 +0000973 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000974
AlexeiFedorov3a739332023-04-13 13:54:04 +0100975 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000976 s2tt[i] = s2tte_create_assigned_ram(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000977 pa += map_size;
978 }
979 dsb(ish);
980}
981
982/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100983 * Populates @s2tt with NS attributes @attrs that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000984 * contiguous memory block starting at @pa, and mapped at level @level.
985 *
986 * The granule is populated before it is made a table,
987 * hence, don't use s2tte_write for access.
988 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000989void s2tt_init_assigned_ns(const struct s2tt_context *s2_ctx,
990 unsigned long *s2tt, unsigned long attrs,
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100991 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000992{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100993 assert(level >= S2TT_MIN_BLOCK_LEVEL);
994 assert(level <= S2TT_PAGE_LEVEL);
995 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000996 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100997
Soby Mathewb4c6df42022-11-09 11:13:29 +0000998 const unsigned long map_size = s2tte_map_size(level);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000999 unsigned long ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ?
1000 S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001001
AlexeiFedorov3a739332023-04-13 13:54:04 +01001002 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001003 unsigned long s2tte = attrs & ns_attr_host_mask;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001004
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001005 s2tt[i] = s2tte_create_assigned_ns(s2_ctx,
1006 s2tte | pa_to_s2tte(pa, s2_ctx->enable_lpa2),
1007 level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001008 pa += map_size;
1009 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001010
Soby Mathewb4c6df42022-11-09 11:13:29 +00001011 dsb(ish);
1012}
1013
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001014/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001015 * Returns true if s2tte is a live RTTE entry. i.e.,
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001016 * HIPAS is ASSIGNED.
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001017 *
1018 * NOTE: For now, only the RTTE with PA are live.
1019 * This could change with EXPORT/IMPORT support.
1020 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001021static bool s2tte_is_live(const struct s2tt_context *s2_ctx,
1022 unsigned long s2tte, long level)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001023{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001024 return s2tte_has_pa(s2_ctx, s2tte, level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001025}
1026
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001027/* Returns physical address of a S2TTE */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001028unsigned long s2tte_pa(const struct s2tt_context *s2_ctx, unsigned long s2tte,
1029 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001030{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001031 bool lpa2;
1032 unsigned long pa;
1033 __unused long min_starting_level;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001034
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001035 assert(s2_ctx != NULL);
1036
1037 min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1038 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1039 assert(level >= min_starting_level);
1040 assert(level <= S2TT_PAGE_LEVEL);
1041 assert(s2tte_has_pa(s2_ctx, s2tte, level));
1042
1043 lpa2 = s2_ctx->enable_lpa2;
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001044
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001045 if (s2tte_is_table(s2_ctx, s2tte, level)) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001046 pa = table_entry_to_phys(s2tte, lpa2);
1047 } else {
1048 pa = s2tte_to_pa(s2tte, level, lpa2);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001049 }
1050
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001051 return pa;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001052}
1053
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001054bool s2tte_is_addr_lvl_aligned(const struct s2tt_context *s2_ctx,
1055 unsigned long addr, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001056{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001057 assert(s2_ctx != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001058
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001059 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1060 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1061 unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
1062 unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
1063 unsigned long s2tte_oa_bits = (s2_ctx->enable_lpa2 == true) ?
1064 S2TTE_OA_BITS_LPA2 : S2TTE_OA_BITS;
1065
1066 assert(level <= S2TT_PAGE_LEVEL);
1067 assert(level >= min_starting_level);
1068
1069 return (addr == (addr & BIT_MASK_ULL((s2tte_oa_bits - 1U), lsb)));
Soby Mathewb4c6df42022-11-09 11:13:29 +00001070}
1071
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001072typedef bool (*s2tte_type_checker)(const struct s2tt_context *s2_ctx,
1073 unsigned long s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001074
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001075static bool table_is_uniform_block(const struct s2tt_context *s2_ctx,
1076 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001077 s2tte_type_checker s2tte_is_x)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001078{
AlexeiFedorov377d81f2023-04-14 16:29:12 +01001079 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
1080 unsigned long s2tte = s2tte_read(&table[i]);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001081
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001082 if (!s2tte_is_x(s2_ctx, s2tte)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001083 return false;
1084 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001085 }
1086
1087 return true;
1088}
1089
1090/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001091 * Returns true if all s2ttes in @table are unassigned_empty.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001092 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001093bool s2tt_is_unassigned_empty_block(const struct s2tt_context *s2_ctx,
1094 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001095{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001096 assert(table != NULL);
1097
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001098 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_empty);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001099}
1100
1101/*
1102 * Returns true if all s2ttes in @table are unassigned_ram.
1103 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001104bool s2tt_is_unassigned_ram_block(const struct s2tt_context *s2_ctx,
1105 unsigned long *table)
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001106{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001107 assert(table != NULL);
1108
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001109 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ram);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001110}
1111
1112/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001113 * Returns true if all s2ttes in @table are unassigned_ns
1114 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001115bool s2tt_is_unassigned_ns_block(const struct s2tt_context *s2_ctx,
1116 unsigned long *table)
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001117{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001118 assert(table != NULL);
1119
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001120 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ns);
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001121}
1122
1123/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001124 * Returns true if all s2ttes in @table are unassigned_destroyed
Soby Mathewb4c6df42022-11-09 11:13:29 +00001125 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001126bool s2tt_is_unassigned_destroyed_block(const struct s2tt_context *s2_ctx,
1127 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001128{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001129 assert(table != NULL);
1130
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001131 return table_is_uniform_block(s2_ctx, table,
1132 s2tte_is_unassigned_destroyed);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001133}
1134
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001135typedef bool (*s2tte_type_level_checker)(const struct s2tt_context *s2_ctx,
1136 unsigned long s2tte, long level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001137
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001138static bool table_maps_block(const struct s2tt_context *s2_ctx,
1139 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001140 long level,
1141 s2tte_type_level_checker s2tte_is_x,
1142 bool check_ns_attrs)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001143{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001144 assert(table != NULL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001145 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001146
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001147 unsigned long base_pa, ns_attr_host_mask;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001148 unsigned long map_size = s2tte_map_size(level);
1149 unsigned long s2tte = s2tte_read(&table[0]);
1150 unsigned int i;
1151
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001152 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001153 return false;
1154 }
1155
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001156 base_pa = s2tte_pa(s2_ctx, s2tte, level);
1157 if (!s2tte_is_addr_lvl_aligned(s2_ctx, base_pa, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001158 return false;
1159 }
1160
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001161 ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ?
1162 S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK;
1163
Soby Mathewb4c6df42022-11-09 11:13:29 +00001164 for (i = 1U; i < S2TTES_PER_S2TT; i++) {
1165 unsigned long expected_pa = base_pa + (i * map_size);
1166
1167 s2tte = s2tte_read(&table[i]);
1168
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001169 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001170 return false;
1171 }
1172
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001173 if (s2tte_pa(s2_ctx, s2tte, level) != expected_pa) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001174 return false;
1175 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001176
1177 if (check_ns_attrs) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001178 unsigned long ns_attrs =
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001179 s2tte & ns_attr_host_mask;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001180
1181 /*
1182 * We match all the attributes in the S2TTE
1183 * except for the AF bit.
1184 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001185 if ((s2tte & ns_attr_host_mask) != ns_attrs) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001186 return false;
1187 }
1188 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001189 }
1190
1191 return true;
1192}
1193
1194/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001195 * Returns true if all s2ttes are assigned_empty
Soby Mathewb4c6df42022-11-09 11:13:29 +00001196 * and refer to a contiguous block of granules aligned to @level - 1.
1197 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001198bool s2tt_maps_assigned_empty_block(const struct s2tt_context *s2_ctx,
1199 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001200{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001201 return table_maps_block(s2_ctx, table, level,
1202 s2tte_is_assigned_empty, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001203}
1204
1205/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001206 * Returns true if all s2ttes are assigned_ram and
Soby Mathewb4c6df42022-11-09 11:13:29 +00001207 * refer to a contiguous block of granules aligned to @level - 1.
1208 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001209bool s2tt_maps_assigned_ram_block(const struct s2tt_context *s2_ctx,
1210 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001211{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001212 return table_maps_block(s2_ctx, table, level,
1213 s2tte_is_assigned_ram, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001214}
1215
1216/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001217 * Returns true if
1218 * - all s2ttes in @table are assigned_ns s2ttes and
1219 * - they refer to a contiguous block of granules aligned to @level - 1 and
1220 * - all the s2tte attributes in @table controlled by the host are identical
AlexeiFedorov3a739332023-04-13 13:54:04 +01001221 *
1222 * @pre: @table maps IPA outside PAR.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001223 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001224bool s2tt_maps_assigned_ns_block(const struct s2tt_context *s2_ctx,
1225 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001226{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001227 return table_maps_block(s2_ctx, table, level,
1228 s2tte_is_assigned_ns, true);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001229}
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001230
1231/*
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001232 * Returns true if all s2ttes are assigned_destroyed and
1233 * refer to a contiguous block of granules aligned to @level - 1.
1234 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001235bool s2tt_maps_assigned_destroyed_block(const struct s2tt_context *s2_ctx,
1236 unsigned long *table, long level)
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001237{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001238 return table_maps_block(s2_ctx, table, level,
1239 s2tte_is_assigned_destroyed, false);
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001240}
1241
1242/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001243 * Scan the RTT @s2tt (which is @wi.level), from the entry (@wi.index) and
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001244 * skip the non-live entries (i.e., HIPAS=UNASSIGNED).
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001245 * In other words, the scanning stops when a live RTTE is encountered or we
1246 * reach the end of this RTT.
1247 *
1248 * For now an RTTE can be considered non-live if it doesn't have a PA.
1249 * NOTE: This would change with EXPORT/IMPORT where we may have metadata stored
1250 * in the RTTE.
1251 *
1252 * @addr is not necessarily aligned to the wi.last_level (e.g., if we were called
1253 * with RMI_ERROR_RTT).
1254 *
1255 * Returns:
1256 * - If the entry @wi.index is live, returns @addr.
1257 * - If none of the entries in the @s2tt are "live", returns the address of the
1258 * first entry in the next table.
1259 * - Otherwise, the address of the first live entry in @s2tt
1260 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001261unsigned long s2tt_skip_non_live_entries(const struct s2tt_context *s2_ctx,
1262 unsigned long addr,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001263 unsigned long *table,
1264 const struct s2tt_walk *wi)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001265{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001266 assert(table != NULL);
1267 assert(wi != NULL);
1268 assert(wi->index <= S2TTES_PER_S2TT);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001269 assert(wi->last_level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001270 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001271
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001272 unsigned long i, index = wi->index;
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001273 long level = wi->last_level;
1274 unsigned long map_size;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001275 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1276 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1277
1278 assert(wi->last_level >= min_starting_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001279
1280 /*
1281 * If the entry for the map_addr is live,
1282 * return @addr.
1283 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001284 if (s2tte_is_live(s2_ctx, s2tte_read(&table[index]), level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001285 return addr;
1286 }
1287
1288 /*
1289 * Align the address DOWN to the map_size, as expected for the @level,
1290 * so that we can compute the correct address by using the index.
1291 */
1292 map_size = s2tte_map_size(level);
1293 addr &= ~(map_size - 1UL);
1294
1295 /* Skip the "index" */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001296 for (i = index + 1UL; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001297 unsigned long s2tte = s2tte_read(&table[i]);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001298
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001299 if (s2tte_is_live(s2_ctx, s2tte, level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001300 break;
1301 }
1302 }
1303
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001304 return (addr + ((i - index) * map_size));
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001305}