interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tests / test_smmu.c

816 lines · 35125 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_smmu.c -- core/smmu.c against a model SMMUv3, and the IORT parser
18 * (core/acpi.c) on a crafted table shaped like QEMU virt's with
19 * iommu=smmuv3. Ported in spirit from the Rust smmu.rs unit tests: bring-up
20 * keeps GBPA.ABORT through SMMUEN, every failure fails closed (CR0 = 0),
21 * STE install/abort invalidate the stream, queues wrap, events decode. */
22#include <stdint.h>
23#include <string.h>
24#include "ck_test.h"
25#include "acpi.h"
26#include "smmu.h"
27
28/* ---- model SMMU ---------------------------------------------------------- */
29#define LOGN 256
30struct model {
31 uint32_t idr0, idr1, cr0, gbpa, cmdq_prod, cmdq_cons, evtq_prod, evtq_cons, strtab_cfg, cr1;
32 uint64_t strtab, cmdq_base, evtq_base;
33 int gbpa_stuck; /* UPDATE never clears */
34 int abort_ignored; /* ABORT never latches */
35 int ack_stuck_en; /* CR0ACK never shows SMMUEN */
36 int cmdq_stuck; /* CONS never advances */
37 int cmdq_err; /* CONS reports an error */
38 uint32_t wlog_off[LOGN], wlog_val[LOGN];
39 int wn;
40 uint64_t cmds[64][2];
41 int ncmd;
42 int barriers;
43};
44static struct model M;
45
46static uint32_t m_rd(void *ctx, uint32_t off)
47{
48 struct model *m = ctx;
49 switch (off) {
50 case CK_SMMU_IDR0: return m->idr0;
51 case CK_SMMU_IDR1: return m->idr1;
52 case CK_SMMU_CR0ACK: return m->ack_stuck_en ? (m->cr0 & ~CK_SMMU_CR0_SMMUEN) : m->cr0;
53 case CK_SMMU_GBPA: return m->gbpa;
54 case CK_SMMU_CMDQ_PROD: return m->cmdq_prod;
55 case CK_SMMU_CMDQ_CONS: return m->cmdq_cons | (m->cmdq_err ? (1u << 24) : 0);
56 case CK_SMMU_EVENTQ_PROD: return m->evtq_prod;
57 case CK_SMMU_EVENTQ_CONS: return m->evtq_cons;
58 }
59 return 0;
60}
61static void m_consume(struct model *m)
62{
63 uint32_t n = 1u << (m->cmdq_base & 0x1f);
64 uint64_t *q = (uint64_t *)(uintptr_t)(m->cmdq_base & ~0x1full);
65 while (m->cmdq_cons != m->cmdq_prod) {
66 uint32_t idx = m->cmdq_cons & (n - 1);
67 if (m->ncmd < 64) {
68 m->cmds[m->ncmd][0] = q[idx * 2];
69 m->cmds[m->ncmd][1] = q[idx * 2 + 1];
70 m->ncmd++;
71 }
72 uint32_t wrap = m->cmdq_cons & n;
73 idx++;
74 if (idx == n) { idx = 0; wrap ^= n; }
75 m->cmdq_cons = idx | wrap;
76 }
77}
78static void m_wr(void *ctx, uint32_t off, uint32_t v)
79{
80 struct model *m = ctx;
81 if (m->wn < LOGN) { m->wlog_off[m->wn] = off; m->wlog_val[m->wn] = v; m->wn++; }
82 switch (off) {
83 case CK_SMMU_CR0: m->cr0 = v; break;
84 case CK_SMMU_CR1: m->cr1 = v; break;
85 case CK_SMMU_GBPA:
86 if (m->gbpa_stuck) { m->gbpa = v; break; }
87 m->gbpa = v & ~CK_SMMU_GBPA_UPDATE;
88 if (m->abort_ignored) m->gbpa &= ~CK_SMMU_GBPA_ABORT;
89 break;
90 case CK_SMMU_CMDQ_PROD:
91 m->cmdq_prod = v;
92 if (!m->cmdq_stuck && !m->cmdq_err) m_consume(m);
93 break;
94 case CK_SMMU_CMDQ_CONS: m->cmdq_cons = v; break;
95 case CK_SMMU_EVENTQ_PROD: m->evtq_prod = v; break;
96 case CK_SMMU_EVENTQ_CONS: m->evtq_cons = v; break;
97 case CK_SMMU_STRTAB_BASE_CFG: m->strtab_cfg = v; break;
98 }
99}
100static void m_wr64(void *ctx, uint32_t off, uint64_t v)
101{
102 struct model *m = ctx;
103 if (m->wn < LOGN) { m->wlog_off[m->wn] = off; m->wlog_val[m->wn] = (uint32_t)v; m->wn++; }
104 if (off == CK_SMMU_CMDQ_BASE) m->cmdq_base = v;
105 if (off == CK_SMMU_EVENTQ_BASE) m->evtq_base = v;
106 if (off == CK_SMMU_STRTAB_BASE) m->strtab = v;
107}
108static void m_bar(void *ctx) { ((struct model *)ctx)->barriers++; }
109static const struct ck_smmu_regs R = { &M, m_rd, m_wr, m_wr64, m_bar };
110
111#define SIDS 16u
112#define QN 16u
113static struct {
114 uint64_t ste[SIDS * 8] __attribute__((aligned(1024)));
115 uint64_t cmd[QN * 2] __attribute__((aligned(256)));
116 uint64_t evt[QN * 4] __attribute__((aligned(512)));
117} mem;
118static struct ck_smmu_tables T;
119
120static void reset(void)
121{
122 memset(&M, 0, sizeof M);
123 memset(&T, 0, sizeof T);
124 memset(&mem, 0xee, sizeof mem);
125 M.idr0 = CK_SMMU_IDR0_S1P;
126 M.idr1 = 16u | (19u << 16) | (19u << 21);
127 T.strtab = mem.ste; T.ste_n = SIDS;
128 T.cmdq = mem.cmd; T.cmd_n = QN;
129 T.evtq = mem.evt; T.evt_n = QN;
130}
131static int first_write(uint32_t off, uint32_t mask, uint32_t val)
132{
133 for (int i = 0; i < M.wn; i++)
134 if (M.wlog_off[i] == off && (M.wlog_val[i] & mask) == val) return i;
135 return -1;
136}
137static uint32_t last_cr0(void)
138{
139 uint32_t v = 0xffffffffu;
140 for (int i = 0; i < M.wn; i++)
141 if (M.wlog_off[i] == CK_SMMU_CR0) v = M.wlog_val[i];
142 return v;
143}
144
145static void test_layouts(void)
146{
147 uint64_t s[8], cd[8], c[2];
148 ck_smmu_ste_abort(s);
149 CHECK(s[0] == 1 && s[1] == 0 && s[7] == 0);
150 ck_smmu_ste_stage1(s, 0x40001040ull);
151 CHECK(s[0] == (1ull | (5ull << 1) | 0x40001040ull));
152 ck_smmu_ste_stage1(s, 0x40001047ull); /* low bits never leak into control bits */
153 CHECK((s[0] & 0x3f) == 0xb);
154 CHECK(ck_smmu_cd_stage1(cd, 0x80000000ull, 3, 0x4404ff, 16) == 0);
155 CHECK((cd[0] & 0x3f) == 16); /* T0SZ */
156 CHECK(((cd[0] >> 6) & 3) == 0); /* TG0 4 KiB */
157 CHECK(((cd[0] >> 8) & 0x3f) == 0); /* IRGN0/ORGN0/SH0: Non-cacheable */
158 CHECK(((cd[0] >> 14) & 1) == 0); /* EPD0 clear: TTBR0 walks */
159 CHECK((cd[0] >> 30) & 1); /* EPD1 */
160 CHECK((cd[0] >> 31) & 1); /* V */
161 CHECK(((cd[0] >> 32) & 7) == 5); /* IPS 48-bit */
162 CHECK((cd[0] >> 41) & 1); /* AA64 */
163 CHECK(((cd[0] >> 45) & 3) == 3); /* R and A */
164 CHECK((cd[0] >> 48) == 3); /* ASID */
165 CHECK(cd[1] == 0x80000000ull && cd[2] == 0 && cd[3] == 0x4404ff);
166 CHECK(ck_smmu_cd_stage1(cd, 0x80000800ull, 1, 0, 16) == CK_SMMU_EINVAL);
167 CHECK(ck_smmu_cd_stage1(cd, 0x80000000ull, 1, 0, 40) == CK_SMMU_EINVAL);
168 ck_smmu_cmd(c, CK_SMMU_CMD_CFGI_ALL, 0, 0);
169 CHECK(c[0] == 0x04 && c[1] == 31);
170 ck_smmu_cmd(c, CK_SMMU_CMD_CFGI_STE, 0x10, 1);
171 CHECK(c[0] == (0x03ull | (0x10ull << 32)) && c[1] == 1);
172 CHECK(ck_smmu_q_encode(3, 0, 16) == 3 && ck_smmu_q_encode(3, 1, 16) == 0x13 && ck_smmu_q_encode(17, 0, 16) == 1);
173}
174
175static void test_tables_check(void)
176{
177 reset();
178 CHECK(ck_smmu_tables_check(&T) == 0);
179 struct ck_smmu_tables b = T;
180 b.ste_n = 12;
181 CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL);
182 b = T;
183 b.cmd_n = 1;
184 CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL);
185 b = T;
186 b.strtab = mem.ste + 8; /* 64-byte aligned, not 1 KiB */
187 CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL);
188 b = T;
189 b.evtq = 0;
190 CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL);
191 reset();
192 T.strtab = mem.ste + 8;
193 CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EINVAL);
194 CHECK(M.wn == 0); /* nothing touched */
195}
196
197static void test_enable_ok(void)
198{
199 reset();
200 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
201 int abort_at = first_write(CK_SMMU_GBPA, CK_SMMU_GBPA_ABORT, CK_SMMU_GBPA_ABORT);
202 int en_at = first_write(CK_SMMU_CR0, CK_SMMU_CR0_SMMUEN, CK_SMMU_CR0_SMMUEN);
203 CHECK(abort_at >= 0 && en_at > abort_at);
204 CHECK(M.wlog_off[0] == CK_SMMU_GBPA); /* abort is the very first write */
205 CHECK(M.gbpa & CK_SMMU_GBPA_ABORT); /* and never cleared */
206 CHECK(M.cr0 == (CK_SMMU_CR0_SMMUEN | CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN));
207 CHECK(M.cr1 == 0);
208 CHECK(M.strtab_cfg == 4 && M.strtab == (uint64_t)(uintptr_t)mem.ste);
209 CHECK(M.cmdq_base == ((uint64_t)(uintptr_t)mem.cmd | 4) && M.evtq_base == ((uint64_t)(uintptr_t)mem.evt | 4));
210 int all_abort = 1;
211 for (unsigned i = 0; i < SIDS; i++)
212 for (unsigned w = 0; w < 8; w++)
213 if (mem.ste[i * 8 + w] != (w == 0 ? 1u : 0u)) all_abort = 0;
214 CHECK(all_abort);
215 CHECK(M.ncmd == 3 && (M.cmds[0][0] & 0xff) == CK_SMMU_CMD_CFGI_ALL &&
216 (M.cmds[1][0] & 0xff) == CK_SMMU_CMD_TLBI_NSNH_ALL && (M.cmds[2][0] & 0xff) == CK_SMMU_CMD_SYNC);
217 /* SMMUEN only after the invalidation commands were consumed. */
218 CHECK(first_write(CK_SMMU_CMDQ_PROD, 0xffffffffu, 3) < en_at);
219 CHECK(M.barriers > 0);
220}
221
222static void test_enable_fail_closed(void)
223{
224 reset();
225 M.gbpa = CK_SMMU_GBPA_UPDATE;
226 M.gbpa_stuck = 1;
227 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT);
228 CHECK(first_write(CK_SMMU_CR0, 0, 0) < 0); /* CR0 never touched */
229
230 reset();
231 M.abort_ignored = 1;
232 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ENOABORT);
233 CHECK(first_write(CK_SMMU_CR0, 0, 0) < 0);
234
235 reset();
236 M.ack_stuck_en = 1;
237 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT);
238 CHECK(last_cr0() == 0 && (M.gbpa & CK_SMMU_GBPA_ABORT));
239
240 reset();
241 M.cmdq_err = 1;
242 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ECMDQ);
243 CHECK(last_cr0() == 0);
244 CHECK(first_write(CK_SMMU_CR0, CK_SMMU_CR0_SMMUEN, CK_SMMU_CR0_SMMUEN) < 0);
245
246 reset();
247 M.cmdq_stuck = 1;
248 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT);
249 CHECK(last_cr0() == 0);
250
251 reset();
252 M.idr0 = 0; /* no stage 1 */
253 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP);
254 CHECK(last_cr0() == 0);
255
256 reset();
257 M.idr1 = 3u | (19u << 16) | (19u << 21); /* 8 stream ids < 16 */
258 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP);
259 reset();
260 M.idr1 = 16u | (19u << 16) | (3u << 21); /* command queue too small */
261 CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP);
262}
263
264static void test_install_abort(void)
265{
266 reset();
267 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
268 M.ncmd = 0;
269 uint64_t ste[8];
270 ck_smmu_ste_stage1(ste, 0x12345640ull);
271 CHECK(ck_smmu_install_ste(&R, &T, 5, ste, 100) == 0);
272 CHECK(mem.ste[5 * 8] == ste[0] && mem.ste[4 * 8] == 1 && mem.ste[6 * 8] == 1);
273 /* abort+invalidate, then install+invalidate: two CFGI_STE(5) batches */
274 CHECK(M.ncmd == 8 && (M.cmds[0][0] & 0xff) == CK_SMMU_CMD_CFGI_STE && (M.cmds[0][0] >> 32) == 5 &&
275 M.cmds[0][1] == 1 && (M.cmds[1][0] & 0xff) == CK_SMMU_CMD_CFGI_CD_ALL && (M.cmds[1][0] >> 32) == 5 &&
276 (M.cmds[4][0] & 0xff) == CK_SMMU_CMD_CFGI_STE && (M.cmds[5][0] & 0xff) == CK_SMMU_CMD_CFGI_CD_ALL &&
277 (M.cmds[6][0] & 0xff) == CK_SMMU_CMD_TLBI_NSNH_ALL && (M.cmds[7][0] & 0xff) == CK_SMMU_CMD_SYNC);
278 CHECK(ck_smmu_install_ste(&R, &T, SIDS, ste, 100) == CK_SMMU_EINVAL);
279 CHECK(ck_smmu_abort_ste(&R, &T, SIDS, 100) == CK_SMMU_EINVAL);
280 M.ncmd = 0;
281 CHECK(ck_smmu_abort_ste(&R, &T, 5, 100) == 0);
282 CHECK(mem.ste[5 * 8] == 1 && mem.ste[5 * 8 + 1] == 0);
283 CHECK(M.ncmd == 4 && (M.cmds[0][0] >> 32) == 5 && (M.cmds[1][0] >> 32) == 5);
284 /* A stuck queue on install leaves the stream in abort. */
285 M.cmdq_stuck = 1;
286 CHECK(ck_smmu_install_ste(&R, &T, 7, ste, 50) == CK_SMMU_ETIMEOUT);
287 CHECK(mem.ste[7 * 8] == 1);
288}
289
290static void test_queue_wrap(void)
291{
292 reset();
293 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
294 M.ncmd = 0;
295 uint64_t c[20][2];
296 for (int i = 0; i < 20; i++)
297 ck_smmu_cmd(c[i], CK_SMMU_CMD_SYNC, (uint32_t)i, 0);
298 CHECK(ck_smmu_submit(&R, &T, (const uint64_t (*)[2])c, 20, 100) == 0);
299 CHECK(M.ncmd == 20 && (M.cmds[19][0] >> 32) == 19);
300 /* 3 (enable) + 20 = 23 = one wrap of 16 + index 7 */
301 CHECK(M.cmdq_prod == (7u | 16u) && M.cmdq_cons == M.cmdq_prod);
302 /* A full queue that never drains times out. */
303 M.cmdq_stuck = 1;
304 CHECK(ck_smmu_submit(&R, &T, (const uint64_t (*)[2])c, 20, 50) == CK_SMMU_ETIMEOUT);
305}
306
307static void test_events(void)
308{
309 reset();
310 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
311 mem.evt[0] = 0x10 | (0x10ull << 32);
312 mem.evt[2] = 0x7000;
313 mem.evt[4] = 0x06 | (0x20ull << 32);
314 mem.evt[6] = 0;
315 M.evtq_prod = 2;
316 struct ck_smmu_event ev[4];
317 int ovf = -1;
318 CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 2);
319 CHECK(ev[0].type == 0x10 && ev[0].sid == 0x10 && ev[0].addr == 0x7000 && ev[1].type == 0x06 && ev[1].sid == 0x20);
320 CHECK(ovf == 0 && M.evtq_cons == 2);
321 CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 0);
322 /* Wrapped producer with overflow flag: acknowledged once. */
323 M.evtq_prod = 1u | 16u | (1u << 31);
324 M.evtq_cons = 15;
325 CHECK(ck_smmu_read_events(&R, &T, ev, 1, &ovf) == 1 && ovf == 1);
326 CHECK(M.evtq_cons == (0u | 16u | (1u << 31)));
327 CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 1 && ovf == 0);
328}
329
330struct observed_events {
331 struct ck_smmu_event events[16];
332 unsigned count;
333 int inject;
334};
335
336static void observe_event(void *ctx, const struct ck_smmu_event *e)
337{
338 struct observed_events *seen = ctx;
339 CHECK(seen->count < 16);
340 if (seen->count < 16)
341 seen->events[seen->count++] = *e;
342 if (seen->inject && seen->count == 16)
343 M.evtq_prod = 17u | (1u << 31);
344}
345
346static void test_event_observer(void)
347{
348 reset();
349 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
350 struct observed_events seen = {0};
351 struct ck_smmu_observation o = ck_smmu_observe_events(&R, &T, observe_event, &seen);
352 CHECK(!o.events && !o.overflow && !o.pending && !seen.count);
353 for (unsigned i = 0; i < 16; i++) {
354 mem.evt[i * 4] = ((uint64_t)(0x100 + i) << 32) | (i ? 0x06 : 0x10);
355 mem.evt[i * 4 + 1] = 0x1234 + i;
356 mem.evt[i * 4 + 2] = 0x8000 + i * 0x1000;
357 mem.evt[i * 4 + 3] = 0xabcd + i;
358 }
359 M.evtq_prod = 16u | (1u << 31); /* full queue, reported loss */
360 seen.inject = 1; /* producer keeps going: observer must remain bounded */
361 o = ck_smmu_observe_events(&R, &T, observe_event, &seen);
362 CHECK(o.events == 16 && o.overflow && o.pending && seen.count == 16);
363 for (unsigned i = 0; i < seen.count; i++) {
364 CHECK(seen.events[i].sid == 0x100 + i);
365 CHECK(seen.events[i].raw[1] == 0x1234 + i && seen.events[i].raw[3] == 0xabcd + i);
366 CHECK(seen.events[i].addr == 0x8000 + i * 0x1000);
367 }
368 seen.count = 0;
369 seen.inject = 0;
370 o = ck_smmu_observe_events(&R, &T, observe_event, &seen);
371 CHECK(o.events == 1 && !o.overflow && !o.pending && seen.count == 1);
372 CHECK(strcmp(ck_smmu_event_name(0x10), "F_TRANSLATION") == 0);
373 CHECK(strcmp(ck_smmu_event_name(0x06), "F_STREAM_DISABLED") == 0);
374 CHECK(strcmp(ck_smmu_event_name(0xff), "UNKNOWN") == 0);
375}
376
377/* ---- IORT -------------------------------------------------------------- */
378static void w16(uint8_t *p, uint16_t v) { memcpy(p, &v, 2); }
379static void w32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); }
380static void w64(uint8_t *p, uint64_t v) { memcpy(p, &v, 8); }
381
382/* header(48) | ITS group (24 bytes, type 0) | SMMUv3 node (68 bytes, one
383 * mapping to the ITS) | root complex (36 + 20*2 bytes, mappings to SMMU
384 * and to the ITS) */
385static uint32_t build_iort(uint8_t *t, int smmu_present)
386{
387 memset(t, 0, 512);
388 memcpy(t, "IORT", 4);
389 uint32_t at = 48, its = at, smmu, rc;
390 t[at] = 0; w16(t + at + 1, 24); w32(t + at + 16, 1); at += 24;
391 smmu = at;
392 t[at] = smmu_present ? 4 : 1; w16(t + at + 1, 88);
393 w32(t + at + 8, 1); w32(t + at + 12, 68);
394 w64(t + at + 16, 0x09050000ull);
395 w32(t + at + 68 + 4, 0xffff); w32(t + at + 68 + 12, its);
396 at += 88;
397 rc = at;
398 t[at] = 2; w16(t + at + 1, 36 + 40);
399 w32(t + at + 8, 2); w32(t + at + 12, 36);
400 w32(t + at + 36 + 0, 0); w32(t + at + 36 + 4, 0xffff); w32(t + at + 36 + 8, 0); w32(t + at + 36 + 12, smmu);
401 w32(t + at + 56 + 0, 0x10000); w32(t + at + 56 + 4, 0xff); w32(t + at + 56 + 8, 0); w32(t + at + 56 + 12, its);
402 at += 76;
403 w32(t + 4, at);
404 w32(t + 36, 3);
405 w32(t + 40, 48);
406 (void)rc;
407 return at;
408}
409
410static void test_iort(void)
411{
412 static uint8_t t[512];
413 struct ck_iort_smmu s;
414 uint32_t sid = 0;
415 build_iort(t, 1);
416 CHECK(ck_iort_parse(t, &s) == 1);
417 CHECK(s.base == 0x09050000ull && s.nmaps == 1 && s.map[0].id_count == 0xffff);
418 CHECK(ck_iort_stream_id(&s, 0, 0x0010, &sid) == 0 && sid == 0x10);
419 CHECK(ck_iort_stream_id(&s, 0, 0xffff, &sid) == 0 && sid == 0xffff);
420 CHECK(ck_iort_stream_id(&s, 0, 0x10000, &sid) == -1); /* the ITS-only mapping is not an SMMU stream */
421 /* ... and not an "other SMMU" entry either: only SMMUv3 nodes count. */
422 uint64_t ob = 0;
423 CHECK(s.nother == 0 && ck_iort_other_stream(&s, 0, 0x10000, &sid, &ob) == -1);
424 build_iort(t, 0);
425 CHECK(ck_iort_parse(t, &s) == 0);
426 /* Malformed: node length past the table, mapping array past the node,
427 * SMMU base 0, bad signature. */
428 build_iort(t, 1);
429 w16(t + 48 + 24 + 1, 400);
430 CHECK(ck_iort_parse(t, &s) == -1);
431 build_iort(t, 1);
432 w32(t + 48 + 24 + 88 + 8, 9);
433 CHECK(ck_iort_parse(t, &s) == -1);
434 build_iort(t, 1);
435 w64(t + 48 + 24 + 16, 0);
436 CHECK(ck_iort_parse(t, &s) == -1);
437 build_iort(t, 1);
438 t[0] = 'X';
439 CHECK(ck_iort_parse(t, &s) == -1);
440 build_iort(t, 1);
441 w32(t + 36, 9); /* more nodes than fit */
442 CHECK(ck_iort_parse(t, &s) == -1);
443 build_iort(t, 1);
444 w32(t + 40, 40); /* node array inside the 48-byte IORT header */
445 CHECK(ck_iort_parse(t, &s) == -1);
446 /* A mapping whose output would wrap is never a stream. */
447 struct ck_iort_smmu w = { .base = 1, .nmaps = 1, .map = { { 0, 0, 0xffff, 0xfffffff0u } } };
448 CHECK(ck_iort_stream_id(&w, 0, 0x20, &sid) == -1);
449}
450
451/* ---- IORT on the DGX Spark's shape (MEASURED from its IORT, 2026-10-06:
452 * 15 root complexes for PCI segments 0..14, each mapping requester ids
453 * 0..0xffff to streams 0x10000*(segment+1) on the first SMMUv3
454 * (0x13800000); segment 15's root complex maps to the second SMMUv3
455 * (0x13000000) instead). ------------------------------------------------ */
456static uint32_t iort_begin(uint8_t *t, size_t cap)
457{
458 memset(t, 0, cap);
459 memcpy(t, "IORT", 4);
460 w32(t + 40, 48);
461 return 48;
462}
463static uint32_t iort_smmu(uint8_t *t, uint32_t at, uint64_t base)
464{
465 t[at] = 4; w16(t + at + 1, 88);
466 w64(t + at + 16, base);
467 return at + 88;
468}
469/* Root complex for `seg` with `n` mappings: mapping i covers
470 * [in + i*step, in + i*step + cnt] -> out + i*step, to node `ref`. */
471static uint32_t iort_rc(uint8_t *t, uint32_t at, uint32_t seg, uint32_t n, uint32_t in, uint32_t cnt, uint32_t step,
472 uint32_t out, uint32_t ref)
473{
474 t[at] = 2; w16(t + at + 1, (uint16_t)(36 + 20 * n));
475 w32(t + at + 8, n); w32(t + at + 12, 36);
476 w32(t + at + 28, seg);
477 for (uint32_t i = 0; i < n; i++) {
478 uint8_t *e = t + at + 36 + 20 * i;
479 w32(e, in + i * step); w32(e + 4, cnt); w32(e + 8, out + i * step); w32(e + 12, ref);
480 }
481 return at + 36 + 20 * n;
482}
483static void iort_end(uint8_t *t, uint32_t at, uint32_t nodes)
484{
485 w32(t + 4, at);
486 w32(t + 36, nodes);
487}
488/* RMR node (type 6) with `nr` ranges and one mapping covering streams
489 * [in, in + cnt] of node `ref`: flags @16, rmr_count @20, rmr_offset @24,
490 * mapping array @28, descriptors (20 bytes each) after it. */
491static uint32_t iort_rmr(uint8_t *t, uint32_t at, uint32_t flags, uint32_t in, uint32_t cnt, uint32_t ref,
492 const uint64_t *ranges, uint32_t nr)
493{
494 uint32_t roff = 28 + 20, nlen = roff + 20 * nr;
495 t[at] = 6; w16(t + at + 1, (uint16_t)nlen);
496 w32(t + at + 8, 1); w32(t + at + 12, 28);
497 w32(t + at + 16, flags); w32(t + at + 20, nr); w32(t + at + 24, roff);
498 uint8_t *e = t + at + 28;
499 w32(e, in); w32(e + 4, cnt); w32(e + 8, in); w32(e + 12, ref);
500 for (uint32_t i = 0; i < nr; i++) {
501 w64(t + at + roff + 20 * i, ranges[2 * i]);
502 w64(t + at + roff + 20 * i + 8, ranges[2 * i + 1]);
503 }
504 return at + nlen;
505}
506
507static void test_iort_spark_shape(void)
508{
509 static uint8_t t[2048];
510 struct ck_iort_smmu s;
511 uint32_t sid = 0;
512 uint32_t at = iort_begin(t, sizeof t), a = at;
513 at = iort_smmu(t, at, 0x13800000ull);
514 uint32_t b = at;
515 at = iort_smmu(t, at, 0x13000000ull);
516 for (uint32_t seg = 0; seg < 15; seg++)
517 at = iort_rc(t, at, seg, 1, 0, 0xffff, 0, 0x10000u * (seg + 1), a);
518 uint32_t rc15 = at;
519 at = iort_rc(t, at, 15, 1, 0, 0xffff, 0, 0, b);
520 iort_end(t, at, 18);
521 CHECK(ck_iort_parse(t, &s) == 1);
522 CHECK(s.base == 0x13800000ull && s.node_off == a && s.nmaps == 15);
523 CHECK(s.map[4].segment == 4 && s.map[4].output_base == 0x50000);
524 /* GB10 000f:01:00.0 (MEASURED 2026-10-10, docs/GB10_IORT_DECODE.md):
525 * segment 15 maps rid 0..0xffff one-to-one onto the second SMMUv3
526 * (0x13000000), so rid 0x100 is stream 0x100 behind an SMMU this
527 * kernel does not drive. The first-SMMU lookup must refuse it and
528 * the other-SMMU lookup must name it. */
529 uint64_t ob = 0;
530 CHECK(s.nother == 1 && s.other[0].map.segment == 15 && s.other[0].smmu_off == b &&
531 s.other[0].smmu_base == 0x13000000ull && s.other[0].map.id_count == 0xffff);
532 CHECK(ck_iort_other_stream(&s, 15, 0x100, &sid, &ob) == 0 && sid == 0x100 && ob == 0x13000000ull);
533 CHECK(ck_iort_other_stream(&s, 15, 0x0, &sid, &ob) == 0 && sid == 0x0);
534 CHECK(ck_iort_other_stream(&s, 15, 0x10000, &sid, &ob) == -1); /* past the range */
535 CHECK(ck_iort_other_stream(&s, 4, 0x100, &sid, &ob) == -1); /* on the first SMMU, not "other" */
536 CHECK(ck_iort_other_stream(&s, 16, 0x100, &sid, &ob) == -1); /* no such segment */
537 /* NVMe 0004:01:00.0 (MEASURED: segment 4, behind smmu 0x13800000):
538 * rid = bus 1 << 8 | dev 0 << 3 | fn 0 = 0x100; segment 4's mapping
539 * starts at stream 0x50000, so 0x50000 + 0x100 = 0x50100. */
540 CHECK(ck_iort_stream_id(&s, 4, 0x100, &sid) == 0 && sid == 0x50100);
541 CHECK(ck_iort_stream_id(&s, 0, 0x100, &sid) == 0 && sid == 0x10100);
542 /* Cut B7a: every SMMUv3 node is kept in table order and a requester is
543 * routed to the node it reaches. */
544 CHECK(s.nsmmus == 2 && s.smmus[0].off == a && s.smmus[0].base == 0x13800000ull && s.smmus[1].off == b &&
545 s.smmus[1].base == 0x13000000ull);
546 struct ck_iort_route rt;
547 CHECK(ck_iort_route(&s, 4, 0x100, &rt) == 0 && rt.smmu_index == 0 && rt.smmu_base == 0x13800000ull &&
548 rt.sid == 0x50100 && rt.nrmr == 0);
549 CHECK(ck_iort_route(&s, 15, 0x100, &rt) == 0 && rt.smmu_index == 1 && rt.smmu_base == 0x13000000ull &&
550 rt.smmu_off == b && rt.sid == 0x100 && rt.nrmr == 0); /* no RMR node in this table */
551 CHECK(ck_iort_route(&s, 16, 0x100, &rt) == -1);
552 CHECK(ck_iort_route(&s, 15, 0x10000, &rt) == -1);
553 /* The same shape with the Spark's RMR node (MEASURED 2026-10-10,
554 * docs/GB10_IORT_DECODE.md): flags 0x10 (Normal NC, remap not
555 * permitted), streams 0x0..=0x100 of the second SMMUv3, three ranges. */
556 static const uint64_t spark_rmr[6] = { 0x300000000ull, 0x3000000ull, 0x280000000ull, 0x80000000ull,
557 0xa1600000ull, 0x18200000ull };
558 at = iort_rmr(t, at, 0x10, 0, 0x100, b, spark_rmr, 3);
559 iort_end(t, at, 19);
560 CHECK(ck_iort_parse(t, &s) == 1 && s.nrmr == 1 && s.rmr[0].smmu_index == 1 && s.rmr[0].sid_lo == 0 &&
561 s.rmr[0].sid_hi == 0x100 && s.rmr[0].flags == 0x10 && s.rmr[0].nranges == 3 &&
562 s.rmr[0].range[1].base == 0x280000000ull && s.rmr[0].range[1].len == 0x80000000ull);
563 CHECK(CK_IORT_RMR_ATTR(s.rmr[0].flags) == CK_IORT_RMR_ATTR_NORMAL_NC && !(s.rmr[0].flags & CK_IORT_RMR_REMAP_PERMITTED));
564 /* The GB10 stream (0x100, the last one, inclusive) is covered; 0x101 is not. */
565 CHECK(ck_iort_route(&s, 15, 0x100, &rt) == 0 && rt.smmu_index == 1 && rt.nrmr == 1 && rt.rmr[0] == &s.rmr[0] &&
566 rt.rmr[0]->range[2].base == 0xa1600000ull);
567 CHECK(ck_iort_route(&s, 15, 0x101, &rt) == 0 && rt.smmu_index == 1 && rt.nrmr == 0);
568 CHECK(ck_iort_route(&s, 15, 0x0, &rt) == 0 && rt.nrmr == 1);
569 /* Stream 0x100 of the FIRST SMMU (segment 0 rid 0x100 is 0x10100; here
570 * use a table where the first node has a stream 0x100): the RMR names
571 * the second node only, so the first node's stream 0x100 gets none. */
572 CHECK(ck_iort_rmr_for(&s, a, 0x100, &rt) == 0);
573 CHECK(ck_iort_rmr_for(&s, b, 0x100, &rt) == 1);
574 /* Malformed RMR nodes refuse the whole table: unaligned range, empty
575 * range, zero descriptors, descriptor array past the node. */
576 uint32_t rmr_at = at - (28 + 20 + 20 * 3);
577 w64(t + rmr_at + 48 + 20 * 0, 0x300000001ull);
578 CHECK(ck_iort_parse(t, &s) == -1);
579 w64(t + rmr_at + 48 + 20 * 0, 0x300000000ull);
580 w64(t + rmr_at + 48 + 20 * 0 + 8, 0);
581 CHECK(ck_iort_parse(t, &s) == -1);
582 w64(t + rmr_at + 48 + 20 * 0 + 8, 0x3000000ull);
583 w32(t + rmr_at + 20, 0);
584 CHECK(ck_iort_parse(t, &s) == -1);
585 w32(t + rmr_at + 20, 4); /* claims 4 descriptors, the node holds 3 */
586 CHECK(ck_iort_parse(t, &s) == -1);
587 w32(t + rmr_at + 20, 3);
588 CHECK(ck_iort_parse(t, &s) == 1 && s.nrmr == 1);
589 /* An RMR mapping to a non-SMMU node (the root complex) is ignored, not a range. */
590 w32(t + rmr_at + 28 + 12, rc15);
591 CHECK(ck_iort_parse(t, &s) == 1 && s.nrmr == 0);
592 w32(t + rmr_at + 28 + 12, b);
593 CHECK(ck_iort_stream_id(&s, 0, 0x0, &sid) == 0 && sid == 0x10000);
594 CHECK(ck_iort_stream_id(&s, 14, 0xffff, &sid) == 0 && sid == 0xfffff);
595 /* Wrong segment: segment 15 belongs to the second SMMU, segment 16
596 * does not exist; a segment-blind lookup would hand out segment 0's
597 * stream 0x10000 / 0x10100 here. */
598 sid = 0xdead;
599 CHECK(ck_iort_stream_id(&s, 15, 0x0, &sid) == -1 && sid == 0xdead);
600 CHECK(ck_iort_stream_id(&s, 16, 0x100, &sid) == -1);
601 CHECK(ck_iort_stream_id(&s, 4, 0x10000, &sid) == -1);
602 /* Segment 15's mappings never land on the first SMMU's list. */
603 for (uint32_t i = 0; i < s.nmaps; i++)
604 CHECK(s.map[i].segment != 15);
605 /* A root complex node too short to carry its segment is refused. */
606 w16(t + rc15 + 1, 24); w32(t + rc15 + 8, 0); w32(t + rc15 + 12, 0);
607 iort_end(t, rc15 + 24, 18);
608 CHECK(ck_iort_parse(t, &s) == -1);
609}
610
611static void test_iort_cap_overlap(void)
612{
613 static uint8_t t[2048];
614 struct ck_iort_smmu s;
615 uint32_t sid = 0;
616 /* Exactly CK_IORT_MAX_MAPS mappings: accepted, all kept. */
617 uint32_t at = iort_begin(t, sizeof t), a = at;
618 at = iort_smmu(t, at, 0x13800000ull);
619 at = iort_rc(t, at, 0, CK_IORT_MAX_MAPS, 0, 0xff, 0x100, 0x1000, a);
620 iort_end(t, at, 2);
621 CHECK(ck_iort_parse(t, &s) == 1 && s.nmaps == CK_IORT_MAX_MAPS);
622 CHECK(ck_iort_stream_id(&s, 0, 0x1f05, &sid) == 0 && sid == 0x2f05);
623 /* One more: the whole table is refused, not truncated. */
624 at = iort_begin(t, sizeof t);
625 at = iort_smmu(t, at, 0x13800000ull);
626 at = iort_rc(t, at, 0, CK_IORT_MAX_MAPS + 1, 0, 0xff, 0x100, 0x1000, a);
627 iort_end(t, at, 2);
628 CHECK(ck_iort_parse(t, &s) == -1);
629 /* Overlapping input ranges in one segment: refused. */
630 at = iort_begin(t, sizeof t);
631 at = iort_smmu(t, at, 0x13800000ull);
632 at = iort_rc(t, at, 0, 2, 0, 0xff, 0x80, 0x1000, a);
633 iort_end(t, at, 2);
634 CHECK(ck_iort_parse(t, &s) == -1);
635 /* Overlap split over two root complexes of the same segment: refused. */
636 at = iort_begin(t, sizeof t);
637 at = iort_smmu(t, at, 0x13800000ull);
638 at = iort_rc(t, at, 3, 1, 0x10, 0xff, 0, 0x1000, a);
639 at = iort_rc(t, at, 3, 1, 0x10f, 0, 0, 0x2000, a);
640 iort_end(t, at, 3);
641 CHECK(ck_iort_parse(t, &s) == -1);
642 /* Adjacent ranges, and the same range in two segments: accepted. */
643 at = iort_begin(t, sizeof t);
644 at = iort_smmu(t, at, 0x13800000ull);
645 at = iort_rc(t, at, 0, 2, 0, 0xff, 0x100, 0x1000, a);
646 at = iort_rc(t, at, 1, 1, 0, 0xff, 0, 0x5000, a);
647 iort_end(t, at, 3);
648 CHECK(ck_iort_parse(t, &s) == 1 && s.nmaps == 3);
649 CHECK(ck_iort_stream_id(&s, 0, 0x100, &sid) == 0 && sid == 0x1100);
650 CHECK(ck_iort_stream_id(&s, 1, 0x10, &sid) == 0 && sid == 0x5010);
651}
652
653/* ---- two-level stream table (Arm IHI 0070 H.a 6.3.25, 5.1) ------------- */
654#define L2_SID_BITS 20u
655#define L1_N (1u << (L2_SID_BITS - CK_SMMU_SPLIT))
656#define POOL_N 4
657static struct {
658 uint64_t l1[L1_N + 16] __attribute__((aligned(L1_N * 8)));
659 uint64_t page[POOL_N][CK_SMMU_L2_STES * 8] __attribute__((aligned(CK_SMMU_L2_BYTES)));
660} l2mem;
661static int pool_next, pool_misalign;
662static int pool_alloc(void *ctx, uint64_t **page)
663{
664 (void)ctx;
665 if (pool_next >= POOL_N) return -1;
666 *page = l2mem.page[pool_next++] + (pool_misalign ? 8 : 0);
667 return 0;
668}
669
670/* Independent model of the SMMU's stream table walk (IHI 0070 3.3.1,
671 * 5.1.1): -1 terminated (out of range, invalid span, outside the span,
672 * STE.V = 0), -2 a valid span whose L2 table is at address 0, else the STE
673 * Config (0 abort, 5 stage-1 translate). */
674static int walk(uint32_t sid)
675{
676 uint32_t cfg = M.strtab_cfg, split = (cfg >> 6) & 0x1f, log2size = cfg & 0x3f;
677 if (((cfg >> 16) & 3) != 1 || (sid >> log2size)) return -1;
678 const uint64_t *l1 = (const uint64_t *)(uintptr_t)(M.strtab & 0x00ffffffffffffc0ull);
679 uint64_t d = l1[sid >> split];
680 uint32_t span = (uint32_t)(d & 0x1f);
681 if (span == 0 || span > 11 || span > split + 1) return -1;
682 uint32_t idx = sid & ((1u << split) - 1u);
683 if (idx >= (1u << (span - 1))) return -1;
684 uint64_t l2 = d & 0x00ffffffffffffc0ull & ~((1ull << (6 + span - 1)) - 1ull);
685 if (l2 == 0) return -2; /* the SMMU would fetch STEs from physical address 0 */
686 const uint64_t *ste = (const uint64_t *)(uintptr_t)l2 + (uint64_t)idx * 8u;
687 if (!(ste[0] & 1)) return -1;
688 return (int)((ste[0] >> 1) & 7);
689}
690static void reset_l2(void)
691{
692 reset();
693 memset(&l2mem, 0xff, sizeof l2mem); /* dirty pages: the attach must fill them */
694 pool_next = 0;
695 pool_misalign = 0;
696 M.idr0 = CK_SMMU_IDR0_S1P | (1u << CK_SMMU_IDR0_ST_LEVEL_SHIFT);
697 M.idr1 = L2_SID_BITS | (19u << 16) | (19u << 21);
698 T.strtab = 0; T.ste_n = 0;
699 T.l1 = l2mem.l1; T.sid_bits = L2_SID_BITS;
700 T.l2_alloc = pool_alloc; T.l2_ctx = 0;
701}
702static int saw_cmd(uint8_t op, uint32_t sid, uint64_t w1)
703{
704 for (int i = 0; i < M.ncmd; i++)
705 if ((M.cmds[i][0] & 0xff) == op && (uint32_t)(M.cmds[i][0] >> 32) == sid && M.cmds[i][1] == w1) return 1;
706 return 0;
707}
708
709static void test_two_level(void)
710{
711 uint64_t ste[8];
712 ck_smmu_ste_stage1(ste, 0x40001000ull);
713 reset_l2();
714 CHECK(ck_smmu_tables_check(&T) == 0);
715 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
716 CHECK(M.strtab_cfg == ((1u << 16) | (6u << 6) | 20u)); /* FMT 2-level, SPLIT 6, LOG2SIZE 20 */
717 CHECK(M.strtab == (uint64_t)(uintptr_t)l2mem.l1);
718 int all_invalid = 1;
719 for (uint32_t i = 0; i < L1_N; i++)
720 if (l2mem.l1[i] != 0) all_invalid = 0;
721 CHECK(all_invalid);
722 CHECK(pool_next == 0); /* lazy: no L2 table before the first grant */
723 CHECK(walk(0x0) == -1 && walk(0x50100) == -1);
724 /* Grant 0x0-0x5 (the USB named components) and 0x50100 (the NVMe). */
725 for (uint32_t sid = 0; sid <= 5; sid++)
726 CHECK(ck_smmu_install_ste(&R, &T, sid, ste, 100) == 0);
727 CHECK(pool_next == 1); /* one span covers 0x0-0x3f */
728 M.ncmd = 0;
729 CHECK(ck_smmu_install_ste(&R, &T, 0x50100, ste, 100) == 0);
730 CHECK(pool_next == 2);
731 CHECK(ck_smmu_l1std(&T, 0x50100) == ((uint64_t)(uintptr_t)l2mem.page[1] | 7u)); /* Span 7 = 64 STEs */
732 CHECK(l2mem.l1[0x50100 >> 6] == ck_smmu_l1std(&T, 0x50100));
733 CHECK(saw_cmd(CK_SMMU_CMD_CFGI_STE, 0x50100, 0)); /* non-leaf: the L1STD */
734 CHECK(saw_cmd(CK_SMMU_CMD_CFGI_STE, 0x50100, 1)); /* leaf: the STE */
735 for (uint32_t sid = 0; sid <= 5; sid++)
736 CHECK(walk(sid) == 5);
737 CHECK(walk(0x50100) == 5);
738 /* Refused: the neighbour in the same span aborts, other spans and
739 * out-of-range ids are terminated. */
740 CHECK(walk(0x50101) == 0);
741 CHECK(walk(0x6) == 0 && walk(0x3f) == 0);
742 CHECK(walk(0x40) == -1 && walk(0x50140) == -1 && walk(0x500ff) == -1);
743 CHECK(walk(0x100000) == -1);
744 CHECK(ck_smmu_sid_in_range(&T, 0xfffff) == 1 && ck_smmu_sid_in_range(&T, 0x100000) == 0);
745 CHECK(ck_smmu_install_ste(&R, &T, 0x100000, ste, 100) == CK_SMMU_EINVAL);
746 CHECK(pool_next == 2);
747 /* Revoke: back to abort; aborting an unattached span allocates nothing. */
748 CHECK(ck_smmu_abort_ste(&R, &T, 0x50100, 100) == 0 && walk(0x50100) == 0);
749 CHECK(ck_smmu_abort_ste(&R, &T, 0x80, 100) == 0 && walk(0x80) == -1 && pool_next == 2);
750 /* A descriptor the kernel did not write is refused, never overwritten. */
751 l2mem.l1[2] = 0x1234;
752 CHECK(ck_smmu_install_ste(&R, &T, 0x80, ste, 100) == CK_SMMU_EINVAL && l2mem.l1[2] == 0x1234);
753 CHECK(ck_smmu_abort_ste(&R, &T, 0x80, 100) == CK_SMMU_EINVAL);
754 CHECK(pool_next == 2);
755}
756
757static void test_two_level_refusals(void)
758{
759 uint64_t ste[8];
760 ck_smmu_ste_stage1(ste, 0x40001000ull);
761 /* Bad L1 pointer: not aligned to the L1 size (128 KiB here). */
762 reset_l2();
763 T.l1 = l2mem.l1 + 1;
764 CHECK(ck_smmu_tables_check(&T) == CK_SMMU_EINVAL);
765 CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EINVAL && M.cr0 == 0 && M.wn == 0);
766 reset_l2();
767 T.l1 = l2mem.l1 + 8; /* 64-byte aligned only */
768 CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EINVAL);
769 /* LOG2SIZE bounds and a missing allocator. */
770 reset_l2();
771 T.sid_bits = CK_SMMU_L2_MAX_BITS + 1;
772 CHECK(ck_smmu_tables_check(&T) == CK_SMMU_EINVAL);
773 T.sid_bits = CK_SMMU_SPLIT;
774 CHECK(ck_smmu_tables_check(&T) == CK_SMMU_EINVAL);
775 reset_l2();
776 T.l2_alloc = 0;
777 CHECK(ck_smmu_tables_check(&T) == CK_SMMU_EINVAL);
778 /* The SMMU must report 2-level support and enough StreamID bits. */
779 reset_l2();
780 M.idr0 = CK_SMMU_IDR0_S1P;
781 CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EUNSUP && last_cr0() == 0);
782 reset_l2();
783 M.idr1 = 16u | (19u << 16) | (19u << 21);
784 CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EUNSUP);
785 /* Bad L2 pointer from the allocator: refused, span stays invalid. */
786 reset_l2();
787 CHECK(ck_smmu_enable(&R, &T, 100) == 0);
788 pool_misalign = 1;
789 CHECK(ck_smmu_install_ste(&R, &T, 0x50100, ste, 100) == CK_SMMU_EINVAL);
790 CHECK(ck_smmu_l1std(&T, 0x50100) == 0 && walk(0x50100) == -1);
791 /* Allocator exhausted: refused, span stays invalid. */
792 pool_misalign = 0;
793 pool_next = POOL_N;
794 CHECK(ck_smmu_install_ste(&R, &T, 0x50100, ste, 100) == CK_SMMU_EINVAL && walk(0x50100) == -1);
795 /* The linear path is unchanged by the two-level fields being zero. */
796 reset();
797 CHECK(ck_smmu_enable(&R, &T, 100) == 0 && M.strtab_cfg == 4u && M.strtab == (uint64_t)(uintptr_t)mem.ste);
798}
799
800int main(void)
801{
802 test_layouts();
803 test_tables_check();
804 test_enable_ok();
805 test_enable_fail_closed();
806 test_install_abort();
807 test_queue_wrap();
808 test_events();
809 test_event_observer();
810 test_iort();
811 test_iort_spark_shape();
812 test_iort_cap_overlap();
813 test_two_level();
814 test_two_level_refusals();
815 return ck_t_verdict("test_smmu");
816}