blob: 7e14ef9e22171036cc3b0247be0f2fb4eccdeb92 [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>
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00009#include <buffer.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#include <granule.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <ripas.h>
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010012#include <s2tt.h>
13#include <s2tt_pvt_defs.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000014#include <smc.h>
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +000015#include <stdbool.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000016#include <stddef.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000017
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +000018/*
19 * Return a mask for the IPA field on a S2TTE
20 */
21static unsigned long s2tte_lvl_mask(long level, bool lpa2)
22{
23 assert(level <= S2TT_PAGE_LEVEL);
24 assert(level >= S2TT_MIN_STARTING_LEVEL_LPA2);
25
26 unsigned long mask;
27 unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
28 unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
29
30 mask = BIT_MASK_ULL((S2TTE_OA_BITS - 1U), lsb);
31
32 if (lpa2 == true) {
33 mask |= (MASK(LPA2_S2TTE_51_50) | MASK(LPA2_OA_49_48));
34 }
35
36 return mask;
37}
38
39/*
40 * Extracts the PA mapped by an S2TTE, aligned to a given level.
41 */
42static unsigned long s2tte_to_pa(unsigned long s2tte, long level, bool lpa2)
43{
44 unsigned long pa = s2tte & s2tte_lvl_mask(level, lpa2);
45
46 if (lpa2 == true) {
47 pa &= ~MASK(LPA2_S2TTE_51_50);
48 pa |= INPLACE(LPA2_OA_51_50, EXTRACT(LPA2_S2TTE_51_50, s2tte));
49 }
50
51 return pa;
52}
Soby Mathewb4c6df42022-11-09 11:13:29 +000053
54/*
55 * Invalidates S2 TLB entries from [ipa, ipa + size] region tagged with `vmid`.
56 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +000057static void stage2_tlbi_ipa(const struct s2tt_context *s2_ctx,
Soby Mathewb4c6df42022-11-09 11:13:29 +000058 unsigned long ipa,
59 unsigned long size)
60{
61 /*
62 * Notes:
63 *
64 * - This follows the description provided in the Arm ARM on
65 * "Invalidation of TLB entries from stage 2 translations".
66 *
67 * - @TODO: Provide additional information to this primitive so that
68 * we can utilize:
69 * - The TTL level hint, see FEAT_TTL,
70 * - Final level lookup only invalidation,
71 * - Address range invalidation.
72 */
73
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +010074 assert(s2_ctx != NULL);
75
Soby Mathewb4c6df42022-11-09 11:13:29 +000076 /*
77 * Save the current content of vttb_el2.
78 */
79 unsigned long old_vttbr_el2 = read_vttbr_el2();
80
81 /*
82 * Make 'vmid' the `current vmid`. Note that the tlbi instructions
83 * bellow target the TLB entries that match the `current vmid`.
84 */
85 write_vttbr_el2(INPLACE(VTTBR_EL2_VMID, s2_ctx->vmid));
86 isb();
87
88 /*
89 * Invalidate entries in S2 TLB caches that
90 * match both `ipa` & the `current vmid`.
91 */
92 while (size != 0UL) {
93 tlbiipas2e1is(ipa >> 12);
94 size -= GRANULE_SIZE;
95 ipa += GRANULE_SIZE;
96 }
97 dsb(ish);
98
99 /*
100 * The architecture does not require TLB invalidation by IPA to affect
101 * combined Stage-1 + Stage-2 TLBs. Therefore we must invalidate all of
102 * Stage-1 (tagged with the `current vmid`) after invalidating Stage-2.
103 */
104 tlbivmalle1is();
105 dsb(ish);
106 isb();
107
108 /*
109 * Restore the old content of vttb_el2.
110 */
111 write_vttbr_el2(old_vttbr_el2);
112 isb();
113}
114
115/*
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000116 * Returns true if s2tte has 'output address' field, namely, if it is one of:
117 * - assigned_empty
118 * - assigned_ram
119 * - assigned_ns
120 * - assigned_destroyed
121 * - table
122 */
123static bool s2tte_has_pa(const struct s2tt_context *s2_ctx,
124 unsigned long s2tte, long level)
125{
126 unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK;
127
128 return ((desc_type != S2TTE_INVALID) || /* block, page or table */
129 s2tte_is_assigned_empty(s2_ctx, s2tte, level) ||
130 s2tte_is_assigned_destroyed(s2_ctx, s2tte, level));
131}
132
133/*
134 * Creates a TTE containing only the PA.
135 * This function expects 'pa' to be aligned and bounded.
136 */
137static unsigned long pa_to_s2tte(unsigned long pa, bool lpa2)
138{
139 unsigned long tte = pa;
140
141 if (lpa2 == true) {
142 tte &= ~MASK(LPA2_OA_51_50);
143 tte |= INPLACE(LPA2_S2TTE_51_50, EXTRACT(LPA2_OA_51_50, pa));
144 }
145
146 return tte;
147}
148
149/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000150 * Invalidate S2 TLB entries with "addr" IPA.
151 * Call this function after:
152 * 1. A L3 page desc has been removed.
153 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000154void s2tt_invalidate_page(const struct s2tt_context *s2_ctx, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000155{
156 stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
157}
158
159/*
160 * Invalidate S2 TLB entries with "addr" IPA.
161 * Call this function after:
162 * 1. A L2 block desc has been removed, or
163 * 2a. A L2 table desc has been removed, where
164 * 2b. All S2TTEs in L3 table that the L2 table desc was pointed to were invalid.
165 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000166void s2tt_invalidate_block(const struct s2tt_context *s2_ctx, unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000167{
168 stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
169}
170
171/*
172 * Invalidate S2 TLB entries with "addr" IPA.
173 * Call this function after:
174 * 1a. A L2 table desc has been removed, where
175 * 1b. Some S2TTEs in the table that the L2 table desc was pointed to were valid.
176 */
Javier Almansa Sobrino2eb98b02023-12-18 18:10:55 +0000177void s2tt_invalidate_pages_in_block(const struct s2tt_context *s2_ctx,
178 unsigned long addr)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000179{
180 stage2_tlbi_ipa(s2_ctx, addr, BLOCK_L2_SIZE);
181}
182
183/*
184 * Return the index of the entry describing @addr in the translation table at
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000185 * level @level. This only works for non-concatenated page tables, so should
Soby Mathewb4c6df42022-11-09 11:13:29 +0000186 * not be called to get the index for the starting level.
187 *
188 * See the library pseudocode
189 * aarch64/translation/vmsa_addrcalc/AArch64.TTEntryAddress on which this is
190 * modeled.
191 */
192static unsigned long s2_addr_to_idx(unsigned long addr, long level)
193{
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100194 unsigned int levels, lsb;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000195 unsigned int s2tte_stride = (level < S2TT_MIN_STARTING_LEVEL) ?
196 S2TTE_STRIDE_LM1 : S2TTE_STRIDE;
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100197
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100198 levels = (unsigned int)(S2TT_PAGE_LEVEL - level);
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100199 lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000200
201 addr >>= lsb;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000202 addr &= (1UL << s2tte_stride) - 1UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000203 return addr;
204}
205
206/*
207 * Return the index of the entry describing @addr in the translation table
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000208 * starting level. This may return an index >= S2TTES_PER_S2TT when the
Soby Mathewb4c6df42022-11-09 11:13:29 +0000209 * combination of @start_level and @ipa_bits implies concatenated
210 * stage 2 tables.
211 *
212 * See the library pseudocode
213 * aarch64/translation/vmsa_addrcalc/AArch64.S2SLTTEntryAddress on which
214 * this is modeled.
215 */
216static unsigned long s2_sl_addr_to_idx(unsigned long addr, int start_level,
217 unsigned long ipa_bits)
218{
AlexeiFedorovbb1f1c82023-08-25 10:44:49 +0100219 unsigned int levels, lsb;
220
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100221 levels = (unsigned int)(S2TT_PAGE_LEVEL - start_level);
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +0100222 lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000223
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000224 addr &= ((1UL << ipa_bits) - 1UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000225 addr >>= lsb;
226 return addr;
227}
228
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000229static bool entry_is_table(unsigned long entry)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000230{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100231 return ((entry & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000232}
233
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000234static unsigned long table_get_entry(const struct s2tt_context *s2_ctx,
235 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100236 unsigned long idx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000237{
238 unsigned long *table, entry;
239
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000240 (void)s2_ctx;
241
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000242 table = buffer_granule_map(g_tbl, SLOT_RTT);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100243 assert(table != NULL);
244
Soby Mathewb4c6df42022-11-09 11:13:29 +0000245 entry = s2tte_read(&table[idx]);
246 buffer_unmap(table);
247
248 return entry;
249}
250
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000251#define table_entry_to_phys(tte, lpa2) \
252 s2tte_to_pa(tte, S2TT_PAGE_LEVEL, lpa2)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100253
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000254static struct granule *find_next_level_idx(const struct s2tt_context *s2_ctx,
255 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100256 unsigned long idx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000257{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000258 assert(s2_ctx != NULL);
259
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000260 const unsigned long entry = table_get_entry(s2_ctx, g_tbl, idx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000261
262 if (!entry_is_table(entry)) {
263 return NULL;
264 }
265
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000266 return addr_to_granule(table_entry_to_phys(entry, s2_ctx->enable_lpa2));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000267}
268
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000269static struct granule *find_lock_next_level(const struct s2tt_context *s2_ctx,
270 struct granule *g_tbl,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100271 unsigned long map_addr,
272 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000273{
274 const unsigned long idx = s2_addr_to_idx(map_addr, level);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000275 struct granule *g = find_next_level_idx(s2_ctx, g_tbl, idx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000276
277 if (g != NULL) {
278 granule_lock(g, GRANULE_STATE_RTT);
279 }
280
281 return g;
282}
283
284/*
285 * Walk an RTT until level @level using @map_addr.
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000286 * @g_root is the root (level 0/-1) table and must be locked before the call.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000287 * @start_level is the initial lookup level used for the stage 2 translation
288 * tables which may depend on the configuration of the realm, factoring in the
289 * IPA size of the realm and the desired starting level (within the limits
290 * defined by the Armv8 VMSA including options for stage 2 table concatenation).
291 * The function uses hand-over-hand locking to avoid race conditions and allow
292 * concurrent access to RTT tree which is not part of the current walk; when a
293 * next level table is reached it is locked before releasing previously locked
294 * table.
295 * The walk stops when either:
296 * - The entry found is a leaf entry (not an RTT Table entry), or
297 * - Level @level is reached.
298 *
299 * On return:
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100300 * - s2tt_walk::last_level is the last level that has been reached by the walk.
301 * - s2tt_walk.g_llt points to the TABLE granule at level @s2tt_walk::level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000302 * The granule is locked.
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000303 * - s2tt_walk::index is the entry index at s2tt_walk.g_llt for @map_addr.i
304 *
305 * NOTE: This function expects that the root table on the s2 context is
306 * already locked.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000307 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000308void s2tt_walk_lock_unlock(const struct s2tt_context *s2_ctx,
309 unsigned long map_addr,
310 long level,
311 struct s2tt_walk *wi)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000312{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000313 struct granule *g_tbls[NR_RTT_LEVELS_LPA2] = { (struct granule *)NULL };
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000314 struct granule *g_root;
315 unsigned long sl_idx, ipa_bits;
316 int i, start_level, last_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000317
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000318 assert(s2_ctx != NULL);
319
320 start_level = s2_ctx->s2_starting_level;
321 ipa_bits = s2_ctx->ipa_bits;
322
Soby Mathewb4c6df42022-11-09 11:13:29 +0000323 assert(level >= start_level);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100324 assert(level <= S2TT_PAGE_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000325 assert(map_addr < (1UL << ipa_bits));
326 assert(wi != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000327
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100328 if (s2_ctx->enable_lpa2 == true) {
329 assert(ipa_bits <= S2TTE_OA_BITS_LPA2);
330 } else {
331 assert(ipa_bits <= S2TTE_OA_BITS);
332 }
333
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000334 g_root = s2_ctx->g_rtt;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000335
336 /* Handle concatenated starting level (SL) tables */
337 sl_idx = s2_sl_addr_to_idx(map_addr, start_level, ipa_bits);
338 if (sl_idx >= S2TTES_PER_S2TT) {
AlexeiFedorov4faab852023-08-30 15:06:49 +0100339 unsigned int tt_num = (unsigned int)(sl_idx >> S2TTE_STRIDE);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100340 struct granule *g_concat_root;
341
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100342 assert(tt_num < s2_ctx->num_root_rtts);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100343
344 g_concat_root = (struct granule *)((uintptr_t)g_root +
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000345 (tt_num * sizeof(struct granule)));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000346
347 granule_lock(g_concat_root, GRANULE_STATE_RTT);
348 granule_unlock(g_root);
349 g_root = g_concat_root;
350 }
351
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000352 /* 'start_level' can be '-1', so add 1 when used as an index */
353 g_tbls[start_level + 1] = g_root;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000354 for (i = start_level; i < level; i++) {
355 /*
356 * Lock next RTT level. Correct locking order is guaranteed
357 * because reference is obtained from a locked granule
358 * (previous level). Also, hand-over-hand locking/unlocking is
359 * used to avoid race conditions.
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000360 *
361 * Note that as 'start_level' can be -1, we add '1' to the
362 * index 'i' to compensate for the negative value when we
363 * use it to index then 'g_tbls' list.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000364 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000365 g_tbls[i + 1 + 1] = find_lock_next_level(s2_ctx, g_tbls[i + 1],
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000366 map_addr, i);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000367 if (g_tbls[i + 1 + 1] == NULL) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000368 last_level = i;
369 goto out;
370 }
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000371 granule_unlock(g_tbls[i + 1]);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000372 }
373
AlexeiFedorov4faab852023-08-30 15:06:49 +0100374 last_level = (int)level;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000375out:
376 wi->last_level = last_level;
AlexeiFedorove956db32024-04-04 14:36:21 +0100377 /* coverity[deref_overflow:SUPPRESS] */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000378 wi->g_llt = g_tbls[last_level + 1];
Soby Mathewb4c6df42022-11-09 11:13:29 +0000379 wi->index = s2_addr_to_idx(map_addr, last_level);
380}
381
382/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100383 * Creates an unassigned_empty s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000384 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000385unsigned long s2tte_create_unassigned_empty(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000386{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000387 (void)s2_ctx;
388
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100389 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_EMPTY);
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100390}
391
392/*
393 * Creates an unassigned_ram s2tte.
394 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000395unsigned long s2tte_create_unassigned_ram(const struct s2tt_context *s2_ctx)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100396{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000397 (void)s2_ctx;
398
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100399 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000400}
401
402/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100403 * Creates an unassigned_destroyed s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000404 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000405unsigned long s2tte_create_unassigned_destroyed(const struct s2tt_context *s2_ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000406{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000407 (void)s2_ctx;
408
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100409 return (S2TTE_INVALID_HIPAS_UNASSIGNED | S2TTE_INVALID_RIPAS_DESTROYED);
410}
411
412/*
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100413 * Creates an unassigned_ns s2tte.
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100414 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000415unsigned long s2tte_create_unassigned_ns(const struct s2tt_context *s2_ctx)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100416{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000417 (void)s2_ctx;
418
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100419 return (S2TTE_NS | S2TTE_INVALID_HIPAS_UNASSIGNED);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000420}
421
422/*
423 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100424 * RIPAS=@s2tte_ripas, at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000425 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000426static unsigned long s2tte_create_assigned(const struct s2tt_context *s2_ctx,
427 unsigned long pa, long level,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100428 unsigned long s2tte_ripas)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000429{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100430 assert(level >= S2TT_MIN_BLOCK_LEVEL);
431 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100432 assert(EXTRACT(S2TTE_INVALID_RIPAS, s2tte_ripas)
433 <= EXTRACT(S2TTE_INVALID_RIPAS, S2TTE_INVALID_RIPAS_DESTROYED));
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000434 assert(s2_ctx != NULL);
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100435 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000436
437 unsigned long tte = pa_to_s2tte(pa, s2_ctx->enable_lpa2);
438 unsigned long s2tte_page, s2tte_block;
439
440 if (s2_ctx->enable_lpa2 == true) {
441 s2tte_page = S2TTE_PAGE_LPA2;
442 s2tte_block = S2TTE_BLOCK_LPA2;
443 } else {
444 s2tte_page = S2TTE_PAGE;
445 s2tte_block = S2TTE_BLOCK;
446 }
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100447
448 if (s2tte_ripas == S2TTE_INVALID_RIPAS_RAM) {
449 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000450 return (tte | s2tte_page);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100451 }
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000452 return (tte | s2tte_block);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100453 }
454
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000455 return (tte | S2TTE_INVALID_HIPAS_ASSIGNED | s2tte_ripas);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100456}
457
458/*
459 * Creates and invalid s2tte with output address @pa, HIPAS=ASSIGNED and
460 * RIPAS=DESTROYED at level @level.
461 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000462unsigned long s2tte_create_assigned_destroyed(const struct s2tt_context *s2_ctx,
463 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100464{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000465 return s2tte_create_assigned(s2_ctx, pa, level,
466 S2TTE_INVALID_RIPAS_DESTROYED);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100467}
468
469/*
470 * Creates an invalid s2tte with output address @pa, HIPAS=ASSIGNED and
471 * RIPAS=EMPTY at level @level.
472 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000473unsigned long s2tte_create_assigned_empty(const struct s2tt_context *s2_ctx,
474 unsigned long pa, long level)
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100475{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000476 return s2tte_create_assigned(s2_ctx, pa, level,
477 S2TTE_INVALID_RIPAS_EMPTY);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000478}
479
480/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100481 * Creates an assigned_ram s2tte with output address @pa.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000482 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000483unsigned long s2tte_create_assigned_ram(const struct s2tt_context *s2_ctx,
484 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000485{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000486 return s2tte_create_assigned(s2_ctx, pa, level,
487 S2TTE_INVALID_RIPAS_RAM);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000488}
489
490/*
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100491 * Creates an assigned s2tte with output address @pa and the same
492 * RIPAS as passed on @s2tte.
493 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000494unsigned long s2tte_create_assigned_unchanged(const struct s2tt_context *s2_ctx,
495 unsigned long s2tte,
Javier Almansa Sobrino84a1b162023-09-26 17:32:44 +0100496 unsigned long pa,
497 long level)
498{
499 unsigned long current_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
500
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000501 return s2tte_create_assigned(s2_ctx, pa, level, current_ripas);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000502}
503
504/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100505 * Creates an assigned_ns s2tte at level @level.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000506 *
507 * The following S2 TTE fields are provided through @s2tte argument:
508 * - The physical address
509 * - MemAttr
510 * - S2AP
Soby Mathewb4c6df42022-11-09 11:13:29 +0000511 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000512unsigned long s2tte_create_assigned_ns(const struct s2tt_context *s2_ctx,
513 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000514{
Soby Mathew217748d2024-10-02 11:02:19 +0100515 assert(s2_ctx != NULL);
516
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000517 /*
Soby Mathew217748d2024-10-02 11:02:19 +0100518 * We just mask out the DESC_TYPE below. We assume rest of the
519 * bits have been setup properly by the caller.
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000520 */
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100521 unsigned long new_s2tte = s2tte & ~S2TT_DESC_TYPE_MASK;
Soby Mathew217748d2024-10-02 11:02:19 +0100522 bool lpa2 = s2_ctx->enable_lpa2;
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000523
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100524 assert(level >= S2TT_MIN_BLOCK_LEVEL);
525 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100526
Soby Mathew217748d2024-10-02 11:02:19 +0100527 /* The Shareability bits need to be added if FEAT_LPA2 is not enabled */
528 if (!lpa2) {
529 new_s2tte |= S2TTE_SH_IS;
530 }
531
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100532 if (level == S2TT_PAGE_LEVEL) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100533 return (new_s2tte | S2TTE_PAGE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000534 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100535 return (new_s2tte | S2TTE_BLOCK_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000536}
537
538/*
539 * Validate the portion of NS S2TTE that is provided by the host.
540 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000541bool host_ns_s2tte_is_valid(const struct s2tt_context *s2_ctx,
542 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000543{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000544 bool lpa2;
545 unsigned long mask;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100546
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000547 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100548 assert(level >= S2TT_MIN_BLOCK_LEVEL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000549
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000550 lpa2 = s2_ctx->enable_lpa2;
551
Soby Mathew217748d2024-10-02 11:02:19 +0100552 mask = s2tte_lvl_mask(level, lpa2) | S2TTE_NS_ATTR_MASK;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000553
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.
Shruti Gupta3530a712024-09-12 10:50:21 +0100558 * Note that this also checks for the case when FEAT_LPA2 is disabled
559 * for the Realm, then the PA in `s2tte` must be <= 48 bits wide.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000560 */
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
Soby Mathew217748d2024-10-02 11:02:19 +0100586 unsigned long mask = s2tte_lvl_mask(level, s2_ctx->enable_lpa2)
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{
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100600 __unused long min_starting_level;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000601
Chuyue Luo5ef71d92023-10-24 14:29:20 +0100602 (void)level;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000603
604 assert(s2_ctx != NULL);
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +0100605
606 /* cppcheck-suppress misra-c2012-10.6 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000607 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
Shruti Gupta3105c3f2024-02-26 14:06:11 +0000757 /* Only pages at L3 and valid blocks at L2 and L1 allowed */
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +0100758 if (level == S2TT_PAGE_LEVEL) {
759 return (desc_type == S2TTE_L3_PAGE);
760 }
761
762 if ((level >= S2TT_MIN_BLOCK_LEVEL) && (desc_type == S2TTE_L012_BLOCK)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000763 return true;
764 }
765
766 return false;
767}
768
769/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100770 * Returns true if @s2tte is an assigned_ram.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000771 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000772bool s2tte_is_assigned_ram(const struct s2tt_context *s2_ctx,
773 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000774{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000775 return s2tte_check(s2_ctx, s2tte, level, 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000776}
777
778/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100779 * Returns true if @s2tte is an assigned_ns s2tte.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000780 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000781bool s2tte_is_assigned_ns(const struct s2tt_context *s2_ctx,
782 unsigned long s2tte, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000783{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000784 return s2tte_check(s2_ctx, s2tte, level, S2TTE_NS);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000785}
786
787/*
788 * Returns true if @s2tte is a table at level @level.
789 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000790bool s2tte_is_table(const struct s2tt_context *s2_ctx, unsigned long s2tte,
791 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000792{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000793 (void)s2_ctx;
794
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100795 return ((level < S2TT_PAGE_LEVEL) &&
796 ((s2tte & S2TT_DESC_TYPE_MASK) == S2TTE_L012_TABLE));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000797}
798
799/*
800 * Returns RIPAS of @s2tte.
801 *
802 * Caller should ensure that HIPAS=UNASSIGNED or HIPAS=ASSIGNED.
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100803 * The s2tte, if valid, should correspond to RIPAS_RAM.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000804 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000805enum ripas s2tte_get_ripas(const struct s2tt_context *s2_ctx, unsigned long s2tte)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000806{
807 unsigned long desc_ripas = s2tte & S2TTE_INVALID_RIPAS_MASK;
Javier Almansa Sobrinob802a0e2024-07-05 12:52:23 +0100808 unsigned long desc_type = s2tte & S2TT_DESC_TYPE_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000809
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000810 (void)s2_ctx;
811
Soby Mathewb4c6df42022-11-09 11:13:29 +0000812 /*
Javier Almansa Sobrinob802a0e2024-07-05 12:52:23 +0100813 * If a valid S2TTE descriptor is passed, the RIPAS corresponds to
814 * RIPAS_RAM.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000815 */
Javier Almansa Sobrinob802a0e2024-07-05 12:52:23 +0100816 if (desc_type != S2TTE_INVALID) {
817 assert((desc_type == S2TTE_L012_BLOCK) ||
818 (desc_type == S2TTE_L3_PAGE));
819 return RIPAS_RAM;
820 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000821
Javier Almansa Sobrinob802a0e2024-07-05 12:52:23 +0100822 assert(EXTRACT(S2TTE_INVALID_HIPAS, s2tte) <= RMI_ASSIGNED);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100823
824 desc_ripas = desc_ripas >> S2TTE_INVALID_RIPAS_SHIFT;
825
826 assert(desc_ripas <= (unsigned int)RIPAS_DESTROYED);
827
828 return (enum ripas)desc_ripas;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000829}
830
831/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100832 * Populates @s2tt with unassigned_empty s2ttes.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000833 *
834 * The granule is populated before it is made a table,
835 * hence, don't use s2tte_write for access.
836 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000837void s2tt_init_unassigned_empty(const struct s2tt_context *s2_ctx,
838 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000839{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100840 assert(s2tt != NULL);
841
842 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000843 s2tte_create_unassigned_empty(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100844
Soby Mathewb4c6df42022-11-09 11:13:29 +0000845 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100846 s2tt[i] = s2tte;
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100847 }
848
849 dsb(ish);
850}
851
852/*
853 * Populates @s2tt with unassigned_ram s2ttes.
854 *
855 * The granule is populated before it is made a table,
856 * hence, don't use s2tte_write for access.
857 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000858void s2tt_init_unassigned_ram(const struct s2tt_context *s2_ctx,
859 unsigned long *s2tt)
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100860{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100861 assert(s2tt != NULL);
862
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000863 unsigned long s2tte = s2tte_create_unassigned_ram(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100864
AlexeiFedorovc07a6382023-04-14 11:59:18 +0100865 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100866 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000867 }
868
869 dsb(ish);
870}
871
872/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100873 * Populates @s2tt with unassigned_ns s2ttes.
874 *
875 * The granule is populated before it is made a table,
876 * hence, don't use s2tte_write for access.
877 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000878void s2tt_init_unassigned_ns(const struct s2tt_context *s2_ctx,
879 unsigned long *s2tt)
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100880{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100881 assert(s2tt != NULL);
882
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000883 unsigned long s2tte = s2tte_create_unassigned_ns(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100884
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100885 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100886 s2tt[i] = s2tte;
AlexeiFedorov5ceff352023-04-12 16:17:00 +0100887 }
888
889 dsb(ish);
890}
891
892/*
Soby Mathewb4c6df42022-11-09 11:13:29 +0000893 * Populates @s2tt with s2ttes which have HIPAS=DESTROYED.
894 *
895 * The granule is populated before it is made a table,
896 * hence, don't use s2tte_write for access.
897 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000898void s2tt_init_unassigned_destroyed(const struct s2tt_context *s2_ctx,
899 unsigned long *s2tt)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000900{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100901 assert(s2tt != NULL);
902
903 unsigned long s2tte =
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000904 s2tte_create_unassigned_destroyed(s2_ctx);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100905
Soby Mathewb4c6df42022-11-09 11:13:29 +0000906 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100907 s2tt[i] = s2tte;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000908 }
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100909 dsb(ish);
910}
Soby Mathewb4c6df42022-11-09 11:13:29 +0000911
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100912/*
913 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=DESTROYED s2ttes that refer to a
914 * contiguous memory block starting at @pa, and mapped at level @level.
915 *
916 * The granule is populated before it is made a table,
917 * hence, don't use s2tte_write for access.
918 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000919void s2tt_init_assigned_destroyed(const struct s2tt_context *s2_ctx,
920 unsigned long *s2tt, unsigned long pa,
921 long level)
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100922{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +0000923 assert(s2tt != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100924 assert(level >= S2TT_MIN_BLOCK_LEVEL);
925 assert(level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000926 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100927
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100928 const unsigned long map_size = s2tte_map_size(level);
929
930 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000931 s2tt[i] = s2tte_create_assigned_destroyed(s2_ctx, pa, level);
AlexeiFedorovc53b1f72023-07-04 15:37:03 +0100932 pa += map_size;
933 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000934 dsb(ish);
935}
936
Soby Mathewb4c6df42022-11-09 11:13:29 +0000937/*
938 * Populates @s2tt with HIPAS=ASSIGNED, RIPAS=EMPTY s2ttes that refer to a
939 * contiguous memory block starting at @pa, and mapped at level @level.
940 *
941 * The granule is populated before it is made a table,
942 * hence, don't use s2tte_write for access.
943 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000944void s2tt_init_assigned_empty(const struct s2tt_context *s2_ctx,
945 unsigned long *s2tt, unsigned long pa,
946 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000947{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100948 assert(level >= S2TT_MIN_BLOCK_LEVEL);
949 assert(level <= S2TT_PAGE_LEVEL);
950 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000951 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100952
Soby Mathewb4c6df42022-11-09 11:13:29 +0000953 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000954
AlexeiFedorov3a739332023-04-13 13:54:04 +0100955 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000956 s2tt[i] = s2tte_create_assigned_empty(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000957 pa += map_size;
958 }
959 dsb(ish);
960}
961
962/*
AlexeiFedorov3a739332023-04-13 13:54:04 +0100963 * Populates @s2tt with assigned_ram s2ttes that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000964 * contiguous memory block starting at @pa, and mapped at level @level.
965 *
966 * The granule is populated before it is made a table,
967 * hence, don't use s2tte_write for access.
968 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000969void s2tt_init_assigned_ram(const struct s2tt_context *s2_ctx,
970 unsigned long *s2tt, unsigned long pa,
971 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000972{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100973 assert(level >= S2TT_MIN_BLOCK_LEVEL);
974 assert(level <= S2TT_PAGE_LEVEL);
975 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000976 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100977
Soby Mathewb4c6df42022-11-09 11:13:29 +0000978 const unsigned long map_size = s2tte_map_size(level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000979
AlexeiFedorov3a739332023-04-13 13:54:04 +0100980 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000981 s2tt[i] = s2tte_create_assigned_ram(s2_ctx, pa, level);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000982 pa += map_size;
983 }
984 dsb(ish);
985}
986
987/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100988 * Populates @s2tt with NS attributes @attrs that refer to a
Soby Mathewb4c6df42022-11-09 11:13:29 +0000989 * contiguous memory block starting at @pa, and mapped at level @level.
990 *
991 * The granule is populated before it is made a table,
992 * hence, don't use s2tte_write for access.
993 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +0000994void s2tt_init_assigned_ns(const struct s2tt_context *s2_ctx,
995 unsigned long *s2tt, unsigned long attrs,
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +0100996 unsigned long pa, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000997{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +0100998 assert(level >= S2TT_MIN_BLOCK_LEVEL);
999 assert(level <= S2TT_PAGE_LEVEL);
1000 assert(s2tt != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001001 assert(s2tte_is_addr_lvl_aligned(s2_ctx, pa, level));
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001002
Soby Mathewb4c6df42022-11-09 11:13:29 +00001003 const unsigned long map_size = s2tte_map_size(level);
Soby Mathew217748d2024-10-02 11:02:19 +01001004
1005 attrs &= S2TTE_NS_ATTR_MASK;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001006
AlexeiFedorov3a739332023-04-13 13:54:04 +01001007 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001008 s2tt[i] = s2tte_create_assigned_ns(s2_ctx,
Soby Mathew217748d2024-10-02 11:02:19 +01001009 attrs | pa_to_s2tte(pa, s2_ctx->enable_lpa2),
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001010 level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001011 pa += map_size;
1012 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001013
Soby Mathewb4c6df42022-11-09 11:13:29 +00001014 dsb(ish);
1015}
1016
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001017/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001018 * Returns true if s2tte is a live RTTE entry. i.e.,
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001019 * HIPAS is ASSIGNED.
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001020 *
1021 * NOTE: For now, only the RTTE with PA are live.
1022 * This could change with EXPORT/IMPORT support.
1023 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001024static bool s2tte_is_live(const struct s2tt_context *s2_ctx,
1025 unsigned long s2tte, long level)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001026{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001027 return s2tte_has_pa(s2_ctx, s2tte, level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001028}
1029
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001030/* Returns physical address of a S2TTE */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001031unsigned long s2tte_pa(const struct s2tt_context *s2_ctx, unsigned long s2tte,
1032 long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001033{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001034 bool lpa2;
1035 unsigned long pa;
1036 __unused long min_starting_level;
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001037
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001038 assert(s2_ctx != NULL);
1039
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +01001040 /* cppcheck-suppress misra-c2012-10.6 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001041 min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1042 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1043 assert(level >= min_starting_level);
1044 assert(level <= S2TT_PAGE_LEVEL);
1045 assert(s2tte_has_pa(s2_ctx, s2tte, level));
1046
1047 lpa2 = s2_ctx->enable_lpa2;
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001048
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001049 if (s2tte_is_table(s2_ctx, s2tte, level)) {
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001050 pa = table_entry_to_phys(s2tte, lpa2);
1051 } else {
1052 pa = s2tte_to_pa(s2tte, level, lpa2);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001053 }
1054
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001055 return pa;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001056}
1057
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001058bool s2tte_is_addr_lvl_aligned(const struct s2tt_context *s2_ctx,
1059 unsigned long addr, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001060{
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001061 assert(s2_ctx != NULL);
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001062
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +01001063 /* cppcheck-suppress misra-c2012-10.6 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001064 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1065 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1066 unsigned long levels = (unsigned long)(S2TT_PAGE_LEVEL - level);
1067 unsigned long lsb = (levels * S2TTE_STRIDE) + GRANULE_SHIFT;
1068 unsigned long s2tte_oa_bits = (s2_ctx->enable_lpa2 == true) ?
1069 S2TTE_OA_BITS_LPA2 : S2TTE_OA_BITS;
1070
1071 assert(level <= S2TT_PAGE_LEVEL);
1072 assert(level >= min_starting_level);
1073
1074 return (addr == (addr & BIT_MASK_ULL((s2tte_oa_bits - 1U), lsb)));
Soby Mathewb4c6df42022-11-09 11:13:29 +00001075}
1076
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001077typedef bool (*s2tte_type_checker)(const struct s2tt_context *s2_ctx,
1078 unsigned long s2tte);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001079
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001080static bool table_is_uniform_block(const struct s2tt_context *s2_ctx,
1081 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001082 s2tte_type_checker s2tte_is_x)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001083{
AlexeiFedorov377d81f2023-04-14 16:29:12 +01001084 for (unsigned int i = 0U; i < S2TTES_PER_S2TT; i++) {
1085 unsigned long s2tte = s2tte_read(&table[i]);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001086
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001087 if (!s2tte_is_x(s2_ctx, s2tte)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001088 return false;
1089 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001090 }
1091
1092 return true;
1093}
1094
1095/*
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001096 * Returns true if all s2ttes in @table are unassigned_empty.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001097 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001098bool s2tt_is_unassigned_empty_block(const struct s2tt_context *s2_ctx,
1099 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001100{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001101 assert(table != NULL);
1102
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001103 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_empty);
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001104}
1105
1106/*
1107 * Returns true if all s2ttes in @table are unassigned_ram.
1108 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001109bool s2tt_is_unassigned_ram_block(const struct s2tt_context *s2_ctx,
1110 unsigned long *table)
AlexeiFedorovc07a6382023-04-14 11:59:18 +01001111{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001112 assert(table != NULL);
1113
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001114 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ram);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001115}
1116
1117/*
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001118 * Returns true if all s2ttes in @table are unassigned_ns
1119 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001120bool s2tt_is_unassigned_ns_block(const struct s2tt_context *s2_ctx,
1121 unsigned long *table)
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001122{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001123 assert(table != NULL);
1124
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001125 return table_is_uniform_block(s2_ctx, table, s2tte_is_unassigned_ns);
AlexeiFedorov5ceff352023-04-12 16:17:00 +01001126}
1127
1128/*
AlexeiFedorovc53b1f72023-07-04 15:37:03 +01001129 * Returns true if all s2ttes in @table are unassigned_destroyed
Soby Mathewb4c6df42022-11-09 11:13:29 +00001130 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001131bool s2tt_is_unassigned_destroyed_block(const struct s2tt_context *s2_ctx,
1132 unsigned long *table)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001133{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001134 assert(table != NULL);
1135
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001136 return table_is_uniform_block(s2_ctx, table,
1137 s2tte_is_unassigned_destroyed);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001138}
1139
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001140typedef bool (*s2tte_type_level_checker)(const struct s2tt_context *s2_ctx,
1141 unsigned long s2tte, long level);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001142
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001143static bool table_maps_block(const struct s2tt_context *s2_ctx,
1144 unsigned long *table,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001145 long level,
1146 s2tte_type_level_checker s2tte_is_x,
1147 bool check_ns_attrs)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001148{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001149 assert(table != NULL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001150 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001151
Soby Mathew217748d2024-10-02 11:02:19 +01001152 unsigned long base_pa;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001153 unsigned long map_size = s2tte_map_size(level);
1154 unsigned long s2tte = s2tte_read(&table[0]);
Javier Almansa Sobrino8865ec32024-04-18 10:50:03 +01001155 unsigned long s2tt_ns_attrs;
Soby Mathewb4c6df42022-11-09 11:13:29 +00001156 unsigned int i;
1157
Javier Almansa Sobrino6e3b5522024-04-10 12:12:13 +01001158 if (s2_ctx->enable_lpa2 == true) {
1159 assert(level >= S2TT_MIN_STARTING_LEVEL_LPA2);
1160 } else {
1161 assert(level >= S2TT_MIN_STARTING_LEVEL);
1162 }
1163
1164 assert(level <= S2TT_PAGE_LEVEL);
1165
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001166 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001167 return false;
1168 }
1169
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001170 base_pa = s2tte_pa(s2_ctx, s2tte, level);
1171 if (!s2tte_is_addr_lvl_aligned(s2_ctx, base_pa, level - 1L)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001172 return false;
1173 }
1174
Soby Mathew217748d2024-10-02 11:02:19 +01001175 s2tt_ns_attrs = s2tte & S2TTE_NS_ATTR_MASK;
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001176
Soby Mathewb4c6df42022-11-09 11:13:29 +00001177 for (i = 1U; i < S2TTES_PER_S2TT; i++) {
1178 unsigned long expected_pa = base_pa + (i * map_size);
1179
1180 s2tte = s2tte_read(&table[i]);
1181
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001182 if (!s2tte_is_x(s2_ctx, s2tte, level)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001183 return false;
1184 }
1185
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001186 if (s2tte_pa(s2_ctx, s2tte, level) != expected_pa) {
Soby Mathewb4c6df42022-11-09 11:13:29 +00001187 return false;
1188 }
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001189
1190 if (check_ns_attrs) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001191 unsigned long ns_attrs =
Soby Mathew217748d2024-10-02 11:02:19 +01001192 s2tte & S2TTE_NS_ATTR_MASK;
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001193
1194 /*
1195 * We match all the attributes in the S2TTE
1196 * except for the AF bit.
1197 */
Javier Almansa Sobrino8865ec32024-04-18 10:50:03 +01001198 if (s2tt_ns_attrs != ns_attrs) {
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001199 return false;
1200 }
1201 }
Soby Mathewb4c6df42022-11-09 11:13:29 +00001202 }
1203
1204 return true;
1205}
1206
1207/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001208 * Returns true if all s2ttes are assigned_empty
Soby Mathewb4c6df42022-11-09 11:13:29 +00001209 * and refer to a contiguous block of granules aligned to @level - 1.
1210 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001211bool s2tt_maps_assigned_empty_block(const struct s2tt_context *s2_ctx,
1212 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001213{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001214 return table_maps_block(s2_ctx, table, level,
1215 s2tte_is_assigned_empty, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001216}
1217
1218/*
AlexeiFedorov3a739332023-04-13 13:54:04 +01001219 * Returns true if all s2ttes are assigned_ram and
Soby Mathewb4c6df42022-11-09 11:13:29 +00001220 * refer to a contiguous block of granules aligned to @level - 1.
1221 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001222bool s2tt_maps_assigned_ram_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_ram, false);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001227}
1228
1229/*
Javier Almansa Sobrino15fc44e2023-09-29 13:52:04 +01001230 * Returns true if
1231 * - all s2ttes in @table are assigned_ns s2ttes and
1232 * - they refer to a contiguous block of granules aligned to @level - 1 and
1233 * - all the s2tte attributes in @table controlled by the host are identical
AlexeiFedorov3a739332023-04-13 13:54:04 +01001234 *
1235 * @pre: @table maps IPA outside PAR.
Soby Mathewb4c6df42022-11-09 11:13:29 +00001236 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001237bool s2tt_maps_assigned_ns_block(const struct s2tt_context *s2_ctx,
1238 unsigned long *table, long level)
Soby Mathewb4c6df42022-11-09 11:13:29 +00001239{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001240 return table_maps_block(s2_ctx, table, level,
1241 s2tte_is_assigned_ns, true);
Soby Mathewb4c6df42022-11-09 11:13:29 +00001242}
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001243
1244/*
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001245 * Returns true if all s2ttes are assigned_destroyed and
1246 * refer to a contiguous block of granules aligned to @level - 1.
1247 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001248bool s2tt_maps_assigned_destroyed_block(const struct s2tt_context *s2_ctx,
1249 unsigned long *table, long level)
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001250{
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001251 return table_maps_block(s2_ctx, table, level,
1252 s2tte_is_assigned_destroyed, false);
AlexeiFedorov3f840a02023-07-19 10:55:05 +01001253}
1254
1255/*
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001256 * Scan the RTT @s2tt (which is @wi.level), from the entry (@wi.index) and
AlexeiFedorov3ebd4622023-07-18 16:27:39 +01001257 * skip the non-live entries (i.e., HIPAS=UNASSIGNED).
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001258 * In other words, the scanning stops when a live RTTE is encountered or we
1259 * reach the end of this RTT.
1260 *
1261 * For now an RTTE can be considered non-live if it doesn't have a PA.
1262 * NOTE: This would change with EXPORT/IMPORT where we may have metadata stored
1263 * in the RTTE.
1264 *
1265 * @addr is not necessarily aligned to the wi.last_level (e.g., if we were called
1266 * with RMI_ERROR_RTT).
1267 *
1268 * Returns:
1269 * - If the entry @wi.index is live, returns @addr.
1270 * - If none of the entries in the @s2tt are "live", returns the address of the
1271 * first entry in the next table.
1272 * - Otherwise, the address of the first live entry in @s2tt
1273 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001274unsigned long s2tt_skip_non_live_entries(const struct s2tt_context *s2_ctx,
1275 unsigned long addr,
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001276 unsigned long *table,
1277 const struct s2tt_walk *wi)
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001278{
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001279 assert(table != NULL);
1280 assert(wi != NULL);
1281 assert(wi->index <= S2TTES_PER_S2TT);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001282 assert(wi->last_level <= S2TT_PAGE_LEVEL);
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001283 assert(s2_ctx != NULL);
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001284
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001285 unsigned long i, index = wi->index;
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001286 long level = wi->last_level;
1287 unsigned long map_size;
Shruti Gupta0a0d4ee2024-04-22 21:37:25 +01001288
1289 /* cppcheck-suppress misra-c2012-10.6 */
Javier Almansa Sobrinof6fff692024-02-02 17:13:57 +00001290 __unused long min_starting_level = (s2_ctx->enable_lpa2 == true) ?
1291 S2TT_MIN_STARTING_LEVEL_LPA2 : S2TT_MIN_STARTING_LEVEL;
1292
1293 assert(wi->last_level >= min_starting_level);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001294
1295 /*
1296 * If the entry for the map_addr is live,
1297 * return @addr.
1298 */
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001299 if (s2tte_is_live(s2_ctx, s2tte_read(&table[index]), level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001300 return addr;
1301 }
1302
1303 /*
1304 * Align the address DOWN to the map_size, as expected for the @level,
1305 * so that we can compute the correct address by using the index.
1306 */
1307 map_size = s2tte_map_size(level);
1308 addr &= ~(map_size - 1UL);
1309
1310 /* Skip the "index" */
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001311 for (i = index + 1UL; i < S2TTES_PER_S2TT; i++) {
Javier Almansa Sobrino1e1781e2023-10-18 18:25:56 +01001312 unsigned long s2tte = s2tte_read(&table[i]);
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001313
Javier Almansa Sobrino2595cd82024-01-25 18:25:12 +00001314 if (s2tte_is_live(s2_ctx, s2tte, level)) {
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001315 break;
1316 }
1317 }
1318
AlexeiFedorov56e1a8e2023-09-01 17:06:13 +01001319 return (addr + ((i - index) * map_size));
AlexeiFedorov917eabf2023-04-24 12:20:41 +01001320}