blob: 754d91201d44ea0d046bca36d763182df7797cea [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;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000370 wi->g_llt = g_tbls[last_level + 1];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000371 wi->index = s2_addr_to_idx(map_addr, last_level);
372}
373
374/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100375 * Creates an unassigned_empty s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000376 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000377unsigned long s2tte_create_unassigned_empty(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000378{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000379 (void)s2_ctx;
380
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100381 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_EMPTY);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100382}
383
384/*
385 * Creates an unassigned_ram s2tte.
386 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000387unsigned long s2tte_create_unassigned_ram(const struct s2tt_context *s2_ctx)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100388{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000389 (void)s2_ctx;
390
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100391 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000392}
393
394/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100395 * Creates an unassigned_destroyed s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000396 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000397unsigned long s2tte_create_unassigned_destroyed(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000398{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000399 (void)s2_ctx;
400
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100401 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_DESTROYED);
402}
403
404/*
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100405 * Creates an unassigned_ns s2tte.
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100406 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000407unsigned long s2tte_create_unassigned_ns(const struct s2tt_context *s2_ctx)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100408{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000409 (void)s2_ctx;
410
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100411 return (S2TTE_NS | S2TTE_INVALID_HIPAS_UNASSIGNED |
412 S2TTE_INVALID_UNPROTECTED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000413}
414
415/*
416 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100417 * RIPAS=@s2tte_ripas, at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000418 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000419static unsigned long s2tte_create_assigned(const struct s2tt_context *s2_ctx,
420 unsigned long pa, long level,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100421 unsigned long s2tte_ripas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000422{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100423 assert(level >= S2TT_MIN_BLOCK_LEVEL);
424 assert(level <= S2TT_PAGE_LEVEL);
425 assert(s2tte_ripas <= S2TTE_INVALID_RIPAS_DESTROYED);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000426 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000427 assert(s2_ctx != NULL);
428
429 unsigned long tte = pa_to_s2tte(pa, s2_ctx->enable_lpa2);
430 unsigned long s2tte_page, s2tte_block;
431
432 if (s2_ctx->enable_lpa2 == true) {
433 s2tte_page = S2TTE_PAGE_LPA2;
434 s2tte_block = S2TTE_BLOCK_LPA2;
435 } else {
436 s2tte_page = S2TTE_PAGE;
437 s2tte_block = S2TTE_BLOCK;
438 }
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100439
440 if (s2tte_ripas == S2TTE_INVALID_RIPAS_RAM) {
441 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000442 return (tte | s2tte_page);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100443 }
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000444 return (tte | s2tte_block);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100445 }
446
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000447 return (tte | S2TTE_INVALID_HIPAS_ASSIGNED | s2tte_ripas);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100448}
449
450/*
451 * Creates and invalid s2tte with output address @pa, HIPAS=ASSIGNED and
452 * RIPAS=DESTROYED at level @level.
453 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000454unsigned long s2tte_create_assigned_destroyed(const struct s2tt_context *s2_ctx,
455 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100456{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000457 return s2tte_create_assigned(s2_ctx, pa, level,
458 S2TTE_INVALID_RIPAS_DESTROYED);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100459}
460
461/*
462 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
463 * RIPAS=EMPTY at level @level.
464 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000465unsigned long s2tte_create_assigned_empty(const struct s2tt_context *s2_ctx,
466 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100467{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000468 return s2tte_create_assigned(s2_ctx, pa, level,
469 S2TTE_INVALID_RIPAS_EMPTY);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000470}
471
472/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100473 * Creates an assigned_ram s2tte with output address @pa.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000474 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000475unsigned long s2tte_create_assigned_ram(const struct s2tt_context *s2_ctx,
476 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000477{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000478 return s2tte_create_assigned(s2_ctx, pa, level,
479 S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000480}
481
482/*
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100483 * Creates an assigned s2tte with output address @pa and the same
484 * RIPAS as passed on @s2tte.
485 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000486unsigned long s2tte_create_assigned_unchanged(const struct s2tt_context *s2_ctx,
487 unsigned long s2tte,
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100488 unsigned long pa,
489 long level)
490{
491 unsigned long current_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
492
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000493 return s2tte_create_assigned(s2_ctx, pa, level, current_ripas);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000494}
495
496/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100497 * Creates an assigned_ns s2tte at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000498 *
499 * The following S2 TTE fields are provided through @s2tte argument:
500 * - The physical address
501 * - MemAttr
502 * - S2AP
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000503 * - Shareability (when FEAT_LPA2 is disabled)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000504 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000505unsigned long s2tte_create_assigned_ns(const struct s2tt_context *s2_ctx,
506 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000507{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000508 /*
509 * We just mask out the DESC_TYPE below. The Shareability bits
510 * without FEAT_LPA2 are at the same position as OA bits [51:50]
511 * with FEAT_LPA2 enabled, so we don't need to cater for that
512 * separately.
513 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100514 unsigned long new_s2tte = s2tte & ~S2TT_DESC_TYPE_MASK;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100515
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000516 (void)s2_ctx;
517
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100518 assert(level >= S2TT_MIN_BLOCK_LEVEL);
519 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100520
521 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100522 return (new_s2tte | S2TTE_PAGE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000523 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100524 return (new_s2tte | S2TTE_BLOCK_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000525}
526
527/*
528 * Validate the portion of NS S2TTE that is provided by the host.
529 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000530bool host_ns_s2tte_is_valid(const struct s2tt_context *s2_ctx,
531 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000532{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100533
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000534 bool lpa2;
535 unsigned long mask;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100536
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000537 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100538 assert(level >= S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000539
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000540 lpa2 = s2_ctx->enable_lpa2;
541
542 mask = s2tte_lvl_mask(level, lpa2);
543 if (lpa2 == true) {
544 mask |= S2TTE_NS_ATTR_LPA2_MASK;
545 } else {
546 mask |= S2TTE_NS_ATTR_MASK;
547
548 /* Only SH_IS is allowed */
549 if ((s2tte & S2TTE_SH_MASK) != S2TTE_SH_IS) {
550 return false;
551 }
552 }
553
Soby Mathewb4c6df42022-11-09 11:13:29 +0000554 /*
555 * Test that all fields that are not controlled by the host are zero
556 * and that the output address is correctly aligned. Note that
557 * the host is permitted to map any physical address outside PAR.
558 */
559 if ((s2tte & ~mask) != 0UL) {
560 return false;
561 }
562
563 /*
564 * Only one value masked by S2TTE_MEMATTR_MASK is invalid/reserved.
565 */
566 if ((s2tte & S2TTE_MEMATTR_MASK) == S2TTE_MEMATTR_FWB_RESERVED) {
567 return false;
568 }
569
570 /*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000571 * Note that all the values that are masked by S2TTE_AP_MASK are valid.
572 */
573 return true;
574}
575
576/*
577 * Returns the portion of NS S2TTE that is set by the host.
578 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000579unsigned long host_ns_s2tte(const struct s2tt_context *s2_ctx,
580 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000581{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000582 assert(s2_ctx != NULL);
583
584 unsigned long mask = s2tte_lvl_mask(level, s2_ctx->enable_lpa2);
585
586 mask |= (s2_ctx->enable_lpa2 == true) ? S2TTE_NS_ATTR_LPA2_MASK :
587 S2TTE_NS_ATTR_MASK;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000588
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100589 assert(level >= S2TT_MIN_BLOCK_LEVEL);
590
Soby Mathewb4c6df42022-11-09 11:13:29 +0000591 return (s2tte & mask);
592}
593
594/*
595 * Creates a table s2tte at level @level with output address @pa.
596 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000597unsigned long s2tte_create_table(const struct s2tt_context *s2_ctx,
598 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000599{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000600 __unused int min_starting_level;
601
Chuyue Luo5ef71d92023-10-24 14:29:20 +0100602 (void)level;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000603
604 assert(s2_ctx != NULL);
605 min_starting_level = (s2_ctx->enable_lpa2 == true) ?
606 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
Chuyue Luo5ef71d92023-10-24 14:29:20 +0100607
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100608 assert(level < S2TT_PAGE_LEVEL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000609 assert(level >= min_starting_level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000610 assert(GRANULE_ALIGNED(pa));
611
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000612 return (pa_to_s2tte(pa, s2_ctx->enable_lpa2) | S2TTE_TABLE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000613}
614
615/*
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100616 * Returns true if s2tte has defined ripas value, namely if it is one of:
617 * - unassigned_empty
618 * - unassigned_ram
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100619 * - unassigned_destroyed
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100620 * - assigned_empty
621 * - assigned_ram
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100622 * - assigned_destroyed
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100623 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000624bool s2tte_has_ripas(const struct s2tt_context *s2_ctx,
625 unsigned long s2tte, long level)
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100626{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000627 return (((s2tte & S2TTE_NS) == 0UL) && !s2tte_is_table(s2_ctx,
628 s2tte, level));
AlexeiFedorov0fb44552023-04-14 15:37:58 +0100629}
630
631/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000632 * Returns true if @s2tte has HIPAS=@hipas.
633 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100634static inline bool s2tte_has_hipas(unsigned long s2tte, unsigned long hipas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000635{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100636 unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000637 unsigned long invalid_desc_hipas = s2tte & S2TTE_INVALID_HIPAS_MASK;
638
AlexeiFedorov63614ea2023-07-14 17:07:20 +0100639 return ((desc_type == S2TTE_INVALID) && (invalid_desc_hipas == hipas));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000640}
641
642/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100643 * Returns true if @s2tte has HIPAS=UNASSIGNED and RIPAS=@ripas.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000644 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100645static inline bool s2tte_has_unassigned_ripas(unsigned long s2tte,
646 unsigned long ripas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000647{
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100648 return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) &&
649 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED));
650}
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100651
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100652/*
653 * Returns true if @s2tte has HIPAS=ASSIGNED and RIPAS=@ripas.
654 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100655static inline bool s2tte_has_assigned_ripas(unsigned long s2tte,
656 unsigned long ripas)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100657{
658 return (((s2tte & S2TTE_INVALID_RIPAS_MASK) == ripas) &&
659 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_ASSIGNED));
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100660}
661
662/*
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100663 * Returns true if @s2tte has HIPAS=UNASSIGNED.
664 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000665bool s2tte_is_unassigned(const struct s2tt_context *s2_ctx, unsigned long s2tte)
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100666{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000667 (void)s2_ctx;
668
AlexeiFedorov0f9cd1f2023-07-10 17:04:58 +0100669 return s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED);
670}
671
672/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100673 * Returns true if @s2tte is an unassigned_empty.
674 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000675bool s2tte_is_unassigned_empty(const struct s2tt_context *s2_ctx,
676 unsigned long s2tte)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100677{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000678 (void)s2_ctx;
679
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100680 return (((s2tte & S2TTE_NS) == 0UL) &&
681 s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY));
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100682}
683
684/*
685 * Returns true if @s2tte is an unassigned_ram.
686 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000687bool s2tte_is_unassigned_ram(const struct s2tt_context *s2_ctx,
688 unsigned long s2tte)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100689{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000690 (void)s2_ctx;
691
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100692 return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_RAM);
693}
694
695/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100696 * Returns true if @s2tte is unassigned_ns.
697 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000698bool s2tte_is_unassigned_ns(const struct s2tt_context *s2_ctx,
699 unsigned long s2tte)
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100700{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000701 (void)s2_ctx;
702
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100703 return (((s2tte & S2TTE_NS) != 0UL) &&
704 s2tte_has_hipas(s2tte, S2TTE_INVALID_HIPAS_UNASSIGNED));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000705}
706
707/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100708 * Returns true if @s2tte has RIPAS=DESTROYED.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000709 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000710bool s2tte_is_unassigned_destroyed(const struct s2tt_context *s2_ctx,
711 unsigned long s2tte)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000712{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000713 (void)s2_ctx;
714
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100715 return s2tte_has_unassigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED);
716}
717
718/*
719 * Returns true if @s2tte is an assigned_destroyed.
720 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000721bool s2tte_is_assigned_destroyed(const struct s2tt_context *s2_ctx,
722 unsigned long s2tte, long level)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100723{
724 (void)level;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000725 (void)s2_ctx;
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100726
727 return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_DESTROYED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000728}
729
730/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100731 * Returns true if @s2tte is an assigned_empty.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000732 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000733bool s2tte_is_assigned_empty(const struct s2tt_context *s2_ctx,
734 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000735{
736 (void)level;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000737 (void)s2_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000738
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100739 return s2tte_has_assigned_ripas(s2tte, S2TTE_INVALID_RIPAS_EMPTY);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000740}
741
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000742static bool s2tte_check(const struct s2tt_context *s2_ctx, unsigned long s2tte,
743 long level, unsigned long ns)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000744{
745 unsigned long desc_type;
746
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000747 (void)s2_ctx;
748
Soby Mathewb4c6df42022-11-09 11:13:29 +0000749 if ((s2tte & S2TTE_NS) != ns) {
750 return false;
751 }
752
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100753 desc_type = s2tte & S2TT_DESC_TYPE_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000754
Shruti Gupta3105c3f2024-02-26 14:06:11 +0000755 /* Only pages at L3 and valid blocks at L2 and L1 allowed */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100756 if (((level == S2TT_PAGE_LEVEL) && (desc_type == S2TTE_L3_PAGE)) ||
Shruti Gupta3105c3f2024-02-26 14:06:11 +0000757 ((level >= S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK))) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000758 return true;
759 }
760
761 return false;
762}
763
764/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100765 * Returns true if @s2tte is an assigned_ram.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000766 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000767bool s2tte_is_assigned_ram(const struct s2tt_context *s2_ctx,
768 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000769{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000770 return s2tte_check(s2_ctx, s2tte, level, 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000771}
772
773/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100774 * Returns true if @s2tte is an assigned_ns s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000775 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000776bool s2tte_is_assigned_ns(const struct s2tt_context *s2_ctx,
777 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000778{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000779 return s2tte_check(s2_ctx, s2tte, level, S2TTE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000780}
781
782/*
783 * Returns true if @s2tte is a table at level @level.
784 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000785bool s2tte_is_table(const struct s2tt_context *s2_ctx, unsigned long s2tte,
786 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000787{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000788 (void)s2_ctx;
789
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100790 return ((level < S2TT_PAGE_LEVEL) &&
791 ((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000792}
793
794/*
795 * Returns RIPAS of @s2tte.
796 *
797 * Caller should ensure that HIPAS=UNASSIGNED or HIPAS=ASSIGNED.
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100798 * The s2tte, if valid, should correspond to RIPAS_RAM.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000799 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000800enum ripas s2tte_get_ripas(const struct s2tt_context *s2_ctx, unsigned long s2tte)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000801{
802 unsigned long desc_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
803
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000804 (void)s2_ctx;
805
Soby Mathewb4c6df42022-11-09 11:13:29 +0000806 /*
807 * If valid s2tte descriptor is passed, then ensure S2AP[0]
808 * bit is 1 (S2AP is set to RW for lower EL), which corresponds
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100809 * to RIPAS_RAM (bits[6:5] = b01) on a valid descriptor.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000810 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100811 assert(((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_INVALID) ||
812 (desc_ripas == S2TTE_INVALID_RIPAS_RAM));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000813
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100814 assert(EXTRACT(S2TTE_INVALID_HIPAS, s2tte) <=
815 EXTRACT(S2TTE_INVALID_HIPAS, S2TTE_INVALID_HIPAS_ASSIGNED));
816
817 desc_ripas = desc_ripas >> S2TTE_INVALID_RIPAS_SHIFT;
818
819 assert(desc_ripas <= (unsigned int)RIPAS_DESTROYED);
820
821 return (enum ripas)desc_ripas;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000822}
823
824/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100825 * Populates @s2tt with unassigned_empty s2ttes.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000826 *
827 * The granule is populated before it is made a table,
828 * hence, don't use s2tte_write for access.
829 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000830void s2tt_init_unassigned_empty(const struct s2tt_context *s2_ctx,
831 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000832{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100833 assert(s2tt != NULL);
834
835 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000836 s2tte_create_unassigned_empty(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100837
Soby Mathewb4c6df42022-11-09 11:13:29 +0000838 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100839 s2tt[i] = s2tte;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100840 }
841
842 dsb(ish);
843}
844
845/*
846 * Populates @s2tt with unassigned_ram s2ttes.
847 *
848 * The granule is populated before it is made a table,
849 * hence, don't use s2tte_write for access.
850 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000851void s2tt_init_unassigned_ram(const struct s2tt_context *s2_ctx,
852 unsigned long *s2tt)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100853{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100854 assert(s2tt != NULL);
855
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000856 unsigned long s2tte = s2tte_create_unassigned_ram(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100857
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100858 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100859 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000860 }
861
862 dsb(ish);
863}
864
865/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100866 * Populates @s2tt with unassigned_ns s2ttes.
867 *
868 * The granule is populated before it is made a table,
869 * hence, don't use s2tte_write for access.
870 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000871void s2tt_init_unassigned_ns(const struct s2tt_context *s2_ctx,
872 unsigned long *s2tt)
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100873{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100874 assert(s2tt != NULL);
875
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000876 unsigned long s2tte = s2tte_create_unassigned_ns(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100877
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100878 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100879 s2tt[i] = s2tte;
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100880 }
881
882 dsb(ish);
883}
884
885/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000886 * Populates @s2tt with s2ttes which have HIPAS=DESTROYED.
887 *
888 * The granule is populated before it is made a table,
889 * hence, don't use s2tte_write for access.
890 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000891void s2tt_init_unassigned_destroyed(const struct s2tt_context *s2_ctx,
892 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000893{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100894 assert(s2tt != NULL);
895
896 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000897 s2tte_create_unassigned_destroyed(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100898
Soby Mathewb4c6df42022-11-09 11:13:29 +0000899 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100900 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000901 }
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100902 dsb(ish);
903}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000904
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100905/*
906 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=DESTROYED s2ttes that refer to a
907 * contiguous memory block starting at @pa, and mapped at level @level.
908 *
909 * The granule is populated before it is made a table,
910 * hence, don't use s2tte_write for access.
911 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000912void s2tt_init_assigned_destroyed(const struct s2tt_context *s2_ctx,
913 unsigned long *s2tt, unsigned long pa,
914 long level)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100915{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000916 assert(s2tt != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100917 assert(level >= S2TT_MIN_BLOCK_LEVEL);
918 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000919 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100920
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100921 const unsigned long map_size = s2tte_map_size(level);
922
923 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000924 s2tt[i] = s2tte_create_assigned_destroyed(s2_ctx, pa, level);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100925 pa += map_size;
926 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000927 dsb(ish);
928}
929
Soby Mathewb4c6df42022-11-09 11:13:29 +0000930/*
931 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=EMPTY s2ttes that refer to a
932 * contiguous memory block starting at @pa, and mapped at level @level.
933 *
934 * The granule is populated before it is made a table,
935 * hence, don't use s2tte_write for access.
936 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000937void s2tt_init_assigned_empty(const struct s2tt_context *s2_ctx,
938 unsigned long *s2tt, unsigned long pa,
939 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000940{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100941 assert(level >= S2TT_MIN_BLOCK_LEVEL);
942 assert(level <= S2TT_PAGE_LEVEL);
943 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000944 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100945
Soby Mathewb4c6df42022-11-09 11:13:29 +0000946 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000947
AlexeiFedorov3a739332023-04-13 13:54:04 +0100948 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000949 s2tt[i] = s2tte_create_assigned_empty(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000950 pa += map_size;
951 }
952 dsb(ish);
953}
954
955/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100956 * Populates @s2tt with assigned_ram s2ttes that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000957 * contiguous memory block starting at @pa, and mapped at level @level.
958 *
959 * The granule is populated before it is made a table,
960 * hence, don't use s2tte_write for access.
961 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000962void s2tt_init_assigned_ram(const struct s2tt_context *s2_ctx,
963 unsigned long *s2tt, unsigned long pa,
964 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000965{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100966 assert(level >= S2TT_MIN_BLOCK_LEVEL);
967 assert(level <= S2TT_PAGE_LEVEL);
968 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000969 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100970
Soby Mathewb4c6df42022-11-09 11:13:29 +0000971 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000972
AlexeiFedorov3a739332023-04-13 13:54:04 +0100973 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000974 s2tt[i] = s2tte_create_assigned_ram(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000975 pa += map_size;
976 }
977 dsb(ish);
978}
979
980/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100981 * Populates @s2tt with NS attributes @attrs that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000982 * contiguous memory block starting at @pa, and mapped at level @level.
983 *
984 * The granule is populated before it is made a table,
985 * hence, don't use s2tte_write for access.
986 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000987void s2tt_init_assigned_ns(const struct s2tt_context *s2_ctx,
988 unsigned long *s2tt, unsigned long attrs,
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100989 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000990{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100991 assert(level >= S2TT_MIN_BLOCK_LEVEL);
992 assert(level <= S2TT_PAGE_LEVEL);
993 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000994 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100995
Soby Mathewb4c6df42022-11-09 11:13:29 +0000996 const unsigned long map_size = s2tte_map_size(level);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000997 unsigned long ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ?
998 S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000999
AlexeiFedorov3a739332023-04-13 13:54:04 +01001000 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001001 unsigned long s2tte = attrs & ns_attr_host_mask;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001002
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001003 s2tt[i] = s2tte_create_assigned_ns(s2_ctx,
1004 s2tte | pa_to_s2tte(pa, s2_ctx->enable_lpa2),
1005 level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001006 pa += map_size;
1007 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001008
Soby Mathewb4c6df42022-11-09 11:13:29 +00001009 dsb(ish);
1010}
1011
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001012/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001013 * Returns true if s2tte is a live RTTE entry. i.e.,
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001014 * HIPAS is ASSIGNED.
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001015 *
1016 * NOTE: For now, only the RTTE with PA are live.
1017 * This could change with EXPORT/IMPORT support.
1018 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001019static bool s2tte_is_live(const struct s2tt_context *s2_ctx,
1020 unsigned long s2tte, long level)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001021{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001022 return s2tte_has_pa(s2_ctx, s2tte, level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001023}
1024
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001025/* Returns physical address of a S2TTE */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001026unsigned long s2tte_pa(const struct s2tt_context *s2_ctx, unsigned long s2tte,
1027 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001028{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001029 bool lpa2;
1030 unsigned long pa;
1031 __unused long min_starting_level;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001032
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001033 assert(s2_ctx != NULL);
1034
1035 min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1036 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1037 assert(level >= min_starting_level);
1038 assert(level <= S2TT_PAGE_LEVEL);
1039 assert(s2tte_has_pa(s2_ctx, s2tte, level));
1040
1041 lpa2 = s2_ctx->enable_lpa2;
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001042
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001043 if (s2tte_is_table(s2_ctx, s2tte, level)) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001044 pa = table_entry_to_phys(s2tte, lpa2);
1045 } else {
1046 pa = s2tte_to_pa(s2tte, level, lpa2);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001047 }
1048
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001049 return pa;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001050}
1051
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001052bool s2tte_is_addr_lvl_aligned(const struct s2tt_context *s2_ctx,
1053 unsigned long addr, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001054{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001055 assert(s2_ctx != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001056
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001057 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1058 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1059 unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
1060 unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
1061 unsigned long s2tte_oa_bits = (s2_ctx->enable_lpa2 == true) ?
1062 S2TTE_OA_BITS_LPA2 : S2TTE_OA_BITS;
1063
1064 assert(level <= S2TT_PAGE_LEVEL);
1065 assert(level >= min_starting_level);
1066
1067 return (addr == (addr & BIT_MASK_ULL((s2tte_oa_bits - 1U), lsb)));
Soby Mathewb4c6df42022-11-09 11:13:29 +00001068}
1069
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001070typedef bool (*s2tte_type_checker)(const struct s2tt_context *s2_ctx,
1071 unsigned long s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001072
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001073static bool table_is_uniform_block(const struct s2tt_context *s2_ctx,
1074 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001075 s2tte_type_checker s2tte_is_x)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001076{
AlexeiFedorov377d81f2023-04-14 16:29:12 +01001077 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
1078 unsigned long s2tte = s2tte_read(&table[i]);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001079
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001080 if (!s2tte_is_x(s2_ctx, s2tte)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001081 return false;
1082 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001083 }
1084
1085 return true;
1086}
1087
1088/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001089 * Returns true if all s2ttes in @table are unassigned_empty.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001090 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001091bool s2tt_is_unassigned_empty_block(const struct s2tt_context *s2_ctx,
1092 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001093{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001094 assert(table != NULL);
1095
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001096 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_empty);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001097}
1098
1099/*
1100 * Returns true if all s2ttes in @table are unassigned_ram.
1101 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001102bool s2tt_is_unassigned_ram_block(const struct s2tt_context *s2_ctx,
1103 unsigned long *table)
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001104{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001105 assert(table != NULL);
1106
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001107 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ram);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001108}
1109
1110/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001111 * Returns true if all s2ttes in @table are unassigned_ns
1112 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001113bool s2tt_is_unassigned_ns_block(const struct s2tt_context *s2_ctx,
1114 unsigned long *table)
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001115{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001116 assert(table != NULL);
1117
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001118 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ns);
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001119}
1120
1121/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001122 * Returns true if all s2ttes in @table are unassigned_destroyed
Soby Mathewb4c6df42022-11-09 11:13:29 +00001123 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001124bool s2tt_is_unassigned_destroyed_block(const struct s2tt_context *s2_ctx,
1125 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001126{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001127 assert(table != NULL);
1128
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001129 return table_is_uniform_block(s2_ctx, table,
1130 s2tte_is_unassigned_destroyed);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001131}
1132
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001133typedef bool (*s2tte_type_level_checker)(const struct s2tt_context *s2_ctx,
1134 unsigned long s2tte, long level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001135
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001136static bool table_maps_block(const struct s2tt_context *s2_ctx,
1137 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001138 long level,
1139 s2tte_type_level_checker s2tte_is_x,
1140 bool check_ns_attrs)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001141{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001142 assert(table != NULL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001143 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001144
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001145 unsigned long base_pa, ns_attr_host_mask;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001146 unsigned long map_size = s2tte_map_size(level);
1147 unsigned long s2tte = s2tte_read(&table[0]);
1148 unsigned int i;
1149
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001150 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001151 return false;
1152 }
1153
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001154 base_pa = s2tte_pa(s2_ctx, s2tte, level);
1155 if (!s2tte_is_addr_lvl_aligned(s2_ctx, base_pa, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001156 return false;
1157 }
1158
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001159 ns_attr_host_mask = (s2_ctx->enable_lpa2 == true) ?
1160 S2TTE_NS_ATTR_LPA2_MASK : S2TTE_NS_ATTR_MASK;
1161
Soby Mathewb4c6df42022-11-09 11:13:29 +00001162 for (i = 1U; i < S2TTES_PER_S2TT; i++) {
1163 unsigned long expected_pa = base_pa + (i * map_size);
1164
1165 s2tte = s2tte_read(&table[i]);
1166
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001167 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001168 return false;
1169 }
1170
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001171 if (s2tte_pa(s2_ctx, s2tte, level) != expected_pa) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001172 return false;
1173 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001174
1175 if (check_ns_attrs) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001176 unsigned long ns_attrs =
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001177 s2tte & ns_attr_host_mask;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001178
1179 /*
1180 * We match all the attributes in the S2TTE
1181 * except for the AF bit.
1182 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001183 if ((s2tte & ns_attr_host_mask) != ns_attrs) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001184 return false;
1185 }
1186 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001187 }
1188
1189 return true;
1190}
1191
1192/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001193 * Returns true if all s2ttes are assigned_empty
Soby Mathewb4c6df42022-11-09 11:13:29 +00001194 * and refer to a contiguous block of granules aligned to @level - 1.
1195 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001196bool s2tt_maps_assigned_empty_block(const struct s2tt_context *s2_ctx,
1197 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001198{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001199 return table_maps_block(s2_ctx, table, level,
1200 s2tte_is_assigned_empty, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001201}
1202
1203/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001204 * Returns true if all s2ttes are assigned_ram and
Soby Mathewb4c6df42022-11-09 11:13:29 +00001205 * refer to a contiguous block of granules aligned to @level - 1.
1206 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001207bool s2tt_maps_assigned_ram_block(const struct s2tt_context *s2_ctx,
1208 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001209{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001210 return table_maps_block(s2_ctx, table, level,
1211 s2tte_is_assigned_ram, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001212}
1213
1214/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001215 * Returns true if
1216 * - all s2ttes in @table are assigned_ns s2ttes and
1217 * - they refer to a contiguous block of granules aligned to @level - 1 and
1218 * - all the s2tte attributes in @table controlled by the host are identical
AlexeiFedorov3a739332023-04-13 13:54:04 +01001219 *
1220 * @pre: @table maps IPA outside PAR.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001221 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001222bool s2tt_maps_assigned_ns_block(const struct s2tt_context *s2_ctx,
1223 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001224{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001225 return table_maps_block(s2_ctx, table, level,
1226 s2tte_is_assigned_ns, true);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001227}
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001228
1229/*
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001230 * Returns true if all s2ttes are assigned_destroyed and
1231 * refer to a contiguous block of granules aligned to @level - 1.
1232 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001233bool s2tt_maps_assigned_destroyed_block(const struct s2tt_context *s2_ctx,
1234 unsigned long *table, long level)
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001235{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001236 return table_maps_block(s2_ctx, table, level,
1237 s2tte_is_assigned_destroyed, false);
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001238}
1239
1240/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001241 * Scan the RTT @s2tt (which is @wi.level), from the entry (@wi.index) and
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001242 * skip the non-live entries (i.e., HIPAS=UNASSIGNED).
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001243 * In other words, the scanning stops when a live RTTE is encountered or we
1244 * reach the end of this RTT.
1245 *
1246 * For now an RTTE can be considered non-live if it doesn't have a PA.
1247 * NOTE: This would change with EXPORT/IMPORT where we may have metadata stored
1248 * in the RTTE.
1249 *
1250 * @addr is not necessarily aligned to the wi.last_level (e.g., if we were called
1251 * with RMI_ERROR_RTT).
1252 *
1253 * Returns:
1254 * - If the entry @wi.index is live, returns @addr.
1255 * - If none of the entries in the @s2tt are "live", returns the address of the
1256 * first entry in the next table.
1257 * - Otherwise, the address of the first live entry in @s2tt
1258 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001259unsigned long s2tt_skip_non_live_entries(const struct s2tt_context *s2_ctx,
1260 unsigned long addr,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001261 unsigned long *table,
1262 const struct s2tt_walk *wi)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001263{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001264 assert(table != NULL);
1265 assert(wi != NULL);
1266 assert(wi->index <= S2TTES_PER_S2TT);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001267 assert(wi->last_level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001268 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001269
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001270 unsigned long i, index = wi->index;
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001271 long level = wi->last_level;
1272 unsigned long map_size;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001273 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1274 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1275
1276 assert(wi->last_level >= min_starting_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001277
1278 /*
1279 * If the entry for the map_addr is live,
1280 * return @addr.
1281 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001282 if (s2tte_is_live(s2_ctx, s2tte_read(&table[index]), level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001283 return addr;
1284 }
1285
1286 /*
1287 * Align the address DOWN to the map_size, as expected for the @level,
1288 * so that we can compute the correct address by using the index.
1289 */
1290 map_size = s2tte_map_size(level);
1291 addr &= ~(map_size - 1UL);
1292
1293 /* Skip the "index" */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001294 for (i = index + 1UL; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001295 unsigned long s2tte = s2tte_read(&table[i]);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001296
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001297 if (s2tte_is_live(s2_ctx, s2tte, level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001298 break;
1299 }
1300 }
1301
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001302 return (addr + ((i - index) * map_size));
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001303}