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_pi5_pcie.c

368 lines · 16190 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_pi5_pcie.c -- Pi 5 PCIe root complex access, RP1 enumerator and inbound
18 * window verdict (dev/pi5_pcie.c) over a FAKE register file. Hosted: this
19 * proves the decisions, NOT the hardware. Run under ASan/UBSan and with each
20 * CK_PCIE_MUTANT_* (make pi5-pcie-mutants); every mutant must make it FAIL. */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include "ck_test.h"
25#include "pi5_pcie.h"
26
27#define MiB (1ull << 20)
28#define GiB (1ull << 30)
29
30/* The fake root complex: one register page, a root-port config space at the
31 * base, and a 4 KiB data window that shows RP1 only while INDEX selects 01:00.0. */
32struct fake {
33 uint32_t regs[0xa000 / 4];
34 uint32_t rp1[1024];
35 uint32_t reads, writes, last_write_off, last_write_val;
36};
37
38static uint32_t f_rd(void *c, uint32_t off)
39{
40 struct fake *f = c;
41 f->reads++;
42 if (off >= 0x8000 && off < 0x9000) {
43 if (f->regs[0x9000 / 4] == 0x00100000u)
44 return f->rp1[(off - 0x8000) / 4];
45 return 0xffffffffu; /* nothing at the selected function */
46 }
47 return f->regs[off / 4];
48}
49
50static void f_wr(void *c, uint32_t off, uint32_t v)
51{
52 struct fake *f = c;
53 f->writes++;
54 f->last_write_off = off;
55 f->last_write_val = v;
56 f->regs[off / 4] = v;
57}
58
59static struct fake fk;
60static struct ck_pi5pcie_io io = {f_rd, f_wr, &fk};
61
62static void fresh(void)
63{
64 memset(&fk, 0, sizeof fk);
65 fk.regs[0] = 0x271214e4u; /* root port: vendor 14e4 device 2712 */
66 fk.regs[1] = 0x00100006u;
67 fk.regs[2] = 0x06040001u; /* class 060400 rev 01 */
68 fk.regs[3] = 0x00010000u; /* header type 1 */
69 fk.regs[6] = 0x00010100u; /* primary 0, secondary 1, subordinate 1 */
70 fk.rp1[0] = 0x00011de4u; /* vendor 1de4 device 0001 */
71 fk.rp1[1] = 0x00000000u;
72 fk.rp1[2] = 0x02000000u | 0x1u; /* class 020000 rev 01 */
73 fk.rp1[3] = 0x00800000u; /* header type 0, multifunction bit set */
74 fk.rp1[4] = 0; /* BAR0: empty */
75 fk.rp1[5] = 0x1f000000u | 0x4u; /* BAR1: mem64 */
76 fk.rp1[6] = 0x1u; /* BAR1 upper = 1 */
77}
78
79static void t_cfg_access(void)
80{
81 uint32_t v = 0;
82 fresh();
83 CHECK(ck_pi5pcie_cfg_read32(&io, 0, 0, 0, 0, &v) == 0 && v == 0x271214e4u);
84 CHECK(fk.writes == 0); /* bus 0 is the RC page: no INDEX write */
85 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 0, 0, &v) == 0 && v == 0x00011de4u);
86 CHECK(fk.writes == 1 && fk.last_write_off == 0x9000 && fk.last_write_val == 0x00100000u);
87 /* fn 1 selects a different function: INDEX is bus<<20 | fn<<12, shows nothing */
88 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 1, 0, &v) == 0 && v == 0xffffffffu);
89 CHECK(fk.last_write_val == 0x00101000u);
90 /* addresses the root complex cannot reach touch nothing */
91 fresh();
92 CHECK(ck_pi5pcie_cfg_read32(&io, 0, 1, 0, 0, &v) == CK_PI5PCIE_E_ADDR);
93 CHECK(ck_pi5pcie_cfg_read32(&io, 0, 0, 1, 0, &v) == CK_PI5PCIE_E_ADDR);
94 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 1, 0, 0, &v) == CK_PI5PCIE_E_ADDR);
95 CHECK(ck_pi5pcie_cfg_read32(&io, 2, 0, 0, 0, &v) == CK_PI5PCIE_E_ADDR);
96 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 8, 0, &v) == CK_PI5PCIE_E_ADDR);
97 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 0, 4096, &v) == CK_PI5PCIE_E_ADDR);
98 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 0, 2, &v) == CK_PI5PCIE_E_ADDR);
99 CHECK(ck_pi5pcie_cfg_read32(&io, 1, 0, 0, 0, NULL) == CK_PI5PCIE_E_ARG);
100 CHECK(fk.reads == 0 && fk.writes == 0);
101}
102
103static void t_probe_rp1(void)
104{
105 struct ck_pi5pcie_func root, rp1;
106 int rc;
107 fresh();
108 rc = ck_pi5pcie_read_func(&io, 0, 0, 0, &root);
109 CHECK(rc == 0 && root.vendor == 0x14e4 && root.device == 0x2712 && root.class_code == 0x060400 &&
110 root.header_type == 1 && root.nbars == 2 && root.primary == 0 && root.secondary == 1 && root.subordinate == 1);
111 rc = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
112 CHECK(rc == 0 && rp1.vendor == 0x1de4 && rp1.device == 0x0001 && rp1.class_code == 0x020000 && rp1.revision == 1);
113 CHECK(rp1.header_type == 0 && rp1.nbars == 6); /* multifunction bit masked out of the type */
114 CHECK(ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_OK);
115 /* wrong ID */
116 fk.rp1[0] = 0x00011de5u;
117 rc = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
118 CHECK(ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_MISMATCH);
119 fk.rp1[0] = 0x00021de4u;
120 rc = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
121 CHECK(ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_MISMATCH);
122 /* absent: all ones, then vendor 0 */
123 fk.rp1[0] = 0xffffffffu;
124 rc = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
125 CHECK(rc == 1 && ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_ABSENT);
126 fk.rp1[0] = 0;
127 rc = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
128 CHECK(rc == 1 && ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_ABSENT);
129 /* an address error is also "absent", never a match */
130 rc = ck_pi5pcie_read_func(&io, 3, 0, 0, &rp1);
131 CHECK(rc == CK_PI5PCIE_E_ADDR && ck_pi5pcie_rp1_check(rc, &rp1) == CK_PI5PCIE_RP1_ABSENT);
132}
133
134static void t_probe_needs_index(void)
135{
136 /* If INDEX points elsewhere first (firmware left it at another function),
137 * the probe must still land on 01:00.0 by writing INDEX itself. */
138 struct ck_pi5pcie_func rp1;
139 fresh();
140 fk.regs[0x9000 / 4] = 0x00000000u;
141 CHECK(ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1) == 0 && rp1.vendor == 0x1de4);
142}
143
144static void t_bar(void)
145{
146 struct ck_pi5pcie_bar b;
147 uint32_t raw[6];
148 memset(raw, 0, sizeof raw);
149 CHECK(ck_pi5pcie_bar_decode(raw, 0, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_EMPTY);
150 raw[0] = 0xe0000008u; /* mem32 prefetch */
151 CHECK(ck_pi5pcie_bar_decode(raw, 0, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_MEM32 && b.prefetch && b.addr == 0xe0000000u);
152 raw[1] = 0x1f000000u | 0x4u; /* mem64, upper in raw[2] */
153 raw[2] = 0x1u;
154 CHECK(ck_pi5pcie_bar_decode(raw, 1, 6, &b) == 2 && b.kind == CK_PI5PCIE_BAR_MEM64 && !b.prefetch &&
155 b.addr == 0x11f000000ull);
156 raw[3] = 0xc001u; /* io */
157 CHECK(ck_pi5pcie_bar_decode(raw, 3, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_IO && b.addr == 0xc000u);
158 raw[4] = 0x2u; /* legacy below-1MiB type: refused */
159 CHECK(ck_pi5pcie_bar_decode(raw, 4, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_BAD);
160 raw[5] = 0x4u | 0x1000u; /* 64-bit at the last register has no upper half */
161 CHECK(ck_pi5pcie_bar_decode(raw, 5, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_BAD);
162 CHECK(ck_pi5pcie_bar_decode(raw, 6, 6, &b) == 1 && b.kind == CK_PI5PCIE_BAR_BAD);
163 raw[1] = 0x4u | 0x1000u; /* type-1 header has two BARs: a 64-bit BAR at 1 is BAD */
164 CHECK(ck_pi5pcie_bar_decode(raw, 1, 2, &b) == 1 && b.kind == CK_PI5PCIE_BAR_BAD);
165}
166
167static void t_ib_size(void)
168{
169 uint64_t s = 0;
170 CHECK(ck_pi5pcie_ib_size_decode(0, &s) == 0);
171 CHECK(ck_pi5pcie_ib_size_decode(1, &s) == 1 && s == 64 * 1024);
172 CHECK(ck_pi5pcie_ib_size_decode(6, &s) == 1 && s == 2 * MiB);
173 CHECK(ck_pi5pcie_ib_size_decode(21, &s) == 1 && s == 64 * GiB);
174 CHECK(ck_pi5pcie_ib_size_decode(0x1c, &s) == 1 && s == 4096);
175 CHECK(ck_pi5pcie_ib_size_decode(0x1f, &s) == 1 && s == 32768);
176 CHECK(ck_pi5pcie_ib_size_decode(22, &s) == -1);
177 CHECK(ck_pi5pcie_ib_size_decode(0x1b, &s) == -1);
178 CHECK(ck_pi5pcie_ib_size_decode(0x20, &s) == -1);
179}
180
181static void t_ib_read(void)
182{
183 struct ck_pi5pcie_ibwin w;
184 /* what the frozen firmware programs: bus 0 -> CPU 0, 64 GiB, remap on */
185 fresh();
186 fk.regs[0x4034 / 4] = 21;
187 fk.regs[0x4038 / 4] = 0;
188 fk.regs[0x40b4 / 4] = 1;
189 fk.regs[0x40b8 / 4] = 0;
190 CHECK(ck_pi5pcie_ib_read(&io, 0, &w) == 0 && w.enabled && w.size_valid && w.remap_en && w.size == 64 * GiB &&
191 w.bus_base == 0 && w.cpu_base == 0);
192 /* a 2 MiB window at bus 0x40200000 mapped to CPU 0x1_4020_0000 */
193 fk.regs[0x40d4 / 4] = 0x40200000u | 6;
194 fk.regs[0x40d8 / 4] = 0;
195 fk.regs[0x410c / 4] = 0x40200000u | 1;
196 fk.regs[0x4110 / 4] = 1;
197 CHECK(ck_pi5pcie_ib_read(&io, 1, &w) == 0 && w.enabled && w.size_valid && w.size == 2 * MiB &&
198 w.bus_base == 0x40200000u && w.cpu_base == 0x140200000ull);
199 /* window 7 lives at its own offsets and does not alias window 6 */
200 fk.regs[0x4104 / 4] = 0x80000000u | 16; /* code 16 = 2 GiB */
201 fk.regs[0x413c / 4] = 1;
202 CHECK(ck_pi5pcie_ib_read(&io, 7, &w) == 0 && w.enabled && w.size == 2 * GiB && w.bus_base == 0x80000000u && w.remap_en);
203 CHECK(ck_pi5pcie_ib_read(&io, 6, &w) == 0 && !w.enabled);
204 /* disabled, misaligned, bad code, remap off */
205 CHECK(ck_pi5pcie_ib_read(&io, 2, &w) == 0 && !w.enabled && !w.size_valid);
206 fk.regs[0x40dc / 4] = 0x00010000u | 6; /* 2 MiB window at a 64 KiB aligned base */
207 CHECK(ck_pi5pcie_ib_read(&io, 2, &w) == 0 && w.enabled && !w.size_valid);
208 fk.regs[0x40e4 / 4] = 22;
209 CHECK(ck_pi5pcie_ib_read(&io, 3, &w) == 0 && w.enabled && !w.size_valid);
210 CHECK(ck_pi5pcie_ib_read(&io, 8, &w) == CK_PI5PCIE_E_ARG);
211}
212
213static struct ck_pi5pcie_ibwin mk(uint64_t bus, uint64_t size, uint64_t cpu, int remap)
214{
215 struct ck_pi5pcie_ibwin w = {0};
216 w.enabled = 1;
217 w.size_valid = 1;
218 w.remap_en = (uint8_t)remap;
219 w.bus_base = bus;
220 w.size = size;
221 w.cpu_base = cpu;
222 return w;
223}
224
225#define B 0x4600000ull /* bounce region used below: 2 MiB at 0x4600000 */
226#define BS (2 * MiB)
227
228static void t_verdict(void)
229{
230 struct ck_pi5pcie_ibwin w[8];
231 struct ck_pi5pcie_dmav d;
232 memset(w, 0, sizeof w);
233 /* what the firmware programs today: all of RAM */
234 w[0] = mk(0, 64 * GiB, 0, 1);
235 CHECK(ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d) == 0);
236 CHECK(d.verdict == CK_PI5PCIE_DMA_WIDER && d.reach_bytes == 64 * GiB && d.inside_bytes == BS &&
237 d.outside_bytes == 64 * GiB - BS && d.windows == 1);
238 /* exactly the bounce block: confined */
239 w[0] = mk(B, BS, B, 1);
240 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
241 CHECK(d.verdict == CK_PI5PCIE_DMA_CONFINED && d.outside_bytes == 0 && d.inside_bytes == BS);
242 /* confined by remap: bus 0 -> CPU B */
243 w[0] = mk(0, BS, B, 1);
244 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
245 CHECK(d.verdict == CK_PI5PCIE_DMA_CONFINED);
246 /* two windows that together are exactly the block, and an overlapping duplicate: still confined */
247 w[0] = mk(B, MiB, B, 1);
248 w[1] = mk(B + MiB, MiB, B + MiB, 1);
249 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
250 CHECK(d.verdict == CK_PI5PCIE_DMA_CONFINED && d.reach_bytes == BS && d.windows == 2);
251 w[2] = mk(B, MiB, B, 1);
252 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
253 CHECK(d.verdict == CK_PI5PCIE_DMA_CONFINED && d.reach_bytes == BS); /* union, not a sum */
254 memset(w, 0, sizeof w);
255 /* only half the block reachable: partial, the driver must not use the rest */
256 w[0] = mk(B, MiB, B, 1);
257 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
258 CHECK(d.verdict == CK_PI5PCIE_DMA_PARTIAL && d.inside_bytes == MiB && d.outside_bytes == 0);
259 /* partial cover plus reach elsewhere is still partial (cover is the first problem) */
260 w[1] = mk(0, 64 * 1024, 0, 1);
261 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
262 CHECK(d.verdict == CK_PI5PCIE_DMA_PARTIAL && d.outside_bytes == 64 * 1024);
263 memset(w, 0, sizeof w);
264 /* disjoint window: miss */
265 w[0] = mk(0x10000000u, 2 * MiB, 0x10000000u, 1);
266 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
267 CHECK(d.verdict == CK_PI5PCIE_DMA_MISS);
268 /* a window that ends exactly where the block starts does not touch it */
269 w[0] = mk(B - BS, BS, B - BS, 1);
270 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
271 CHECK(d.verdict == CK_PI5PCIE_DMA_MISS);
272 /* no window enabled: DMA cannot reach memory */
273 memset(w, 0, sizeof w);
274 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
275 CHECK(d.verdict == CK_PI5PCIE_DMA_NO_WINDOW && d.windows == 0);
276 /* remap off: where it lands is not known */
277 w[0] = mk(B, BS, B, 0);
278 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
279 CHECK(d.verdict == CK_PI5PCIE_DMA_UNKNOWN);
280 /* invalid window */
281 w[0] = mk(B, BS, B, 1);
282 w[0].size_valid = 0;
283 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
284 CHECK(d.verdict == CK_PI5PCIE_DMA_UNKNOWN);
285 /* an enabled window outside the model (GISB/SCB) cannot be called confined */
286 w[0] = mk(B, BS, B, 1);
287 ck_pi5pcie_ib_verdict(w, 8, 1, B, BS, &d);
288 CHECK(d.verdict == CK_PI5PCIE_DMA_UNKNOWN);
289 w[0] = mk(0, 64 * GiB, 0, 1);
290 ck_pi5pcie_ib_verdict(w, 8, 1, B, BS, &d);
291 CHECK(d.verdict == CK_PI5PCIE_DMA_WIDER); /* already known to be wider */
292 /* wrap: a window whose end passes 2^64 is refused, not folded */
293 w[0] = mk(0, 64 * GiB, 0xfffffffff0000000ull, 1);
294 ck_pi5pcie_ib_verdict(w, 8, 0, B, BS, &d);
295 CHECK(d.verdict == CK_PI5PCIE_DMA_UNKNOWN);
296 /* bad arguments */
297 CHECK(ck_pi5pcie_ib_verdict(w, 8, 0, B, 0, &d) == CK_PI5PCIE_E_ARG);
298 CHECK(ck_pi5pcie_ib_verdict(w, 8, 0, 0xfffffffffff00000ull, 2 * MiB, &d) == CK_PI5PCIE_E_ARG);
299 CHECK(ck_pi5pcie_ib_verdict(w, 9, 0, B, BS, &d) == CK_PI5PCIE_E_ARG);
300 CHECK(!strcmp(ck_pi5pcie_verdict_name(CK_PI5PCIE_DMA_WIDER), "NOT_CONFINED"));
301}
302
303static void put32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); }
304
305static size_t mcfg(uint8_t *t, int n, const uint64_t *base, const uint16_t *seg, const uint8_t *bmax)
306{
307 size_t len = 44 + 16u * (size_t)n;
308 memset(t, 0, len);
309 memcpy(t, "MCFG", 4);
310 put32(t + 4, (uint32_t)len);
311 for (int i = 0; i < n; i++) {
312 uint8_t *e = t + 44 + 16 * i;
313 put32(e, (uint32_t)base[i]);
314 put32(e + 4, (uint32_t)(base[i] >> 32));
315 e[8] = (uint8_t)seg[i];
316 e[9] = (uint8_t)(seg[i] >> 8);
317 e[10] = 0;
318 e[11] = bmax[i];
319 }
320 return len;
321}
322
323static void t_door(void)
324{
325 uint8_t t[44 + 16 * 3];
326 struct ck_pi5pcie_door d;
327 /* exactly what Mcfg.aslc:26-48 declares for the three controllers */
328 uint64_t base[3] = {0x1000100000ull + 0x8000, 0x1000110000ull + 0x8000, 0x1000120000ull + 0x8000};
329 uint16_t seg[3] = {0, 1, 2};
330 uint8_t bmax[3] = {0, 0, 0};
331 size_t len = mcfg(t, 3, base, seg, bmax);
332 ck_pi5pcie_door_decide(NULL, 0, 2, 0x1000120000ull, &d);
333 CHECK(d.why == CK_PI5PCIE_WHY_NO_MCFG);
334 ck_pi5pcie_door_decide(t, len, 2, 0x1000120000ull, &d);
335 CHECK(d.why == CK_PI5PCIE_WHY_DATA_WINDOW && d.base == 0x1000128000ull && d.bus_max == 0);
336 ck_pi5pcie_door_decide(t, len, 7, 0x1000120000ull, &d);
337 CHECK(d.why == CK_PI5PCIE_WHY_NO_SEGMENT);
338 ck_pi5pcie_door_decide(t, len - 1, 2, 0x1000120000ull, &d);
339 CHECK(d.why == CK_PI5PCIE_WHY_MALFORMED);
340 t[0] = 0; /* hidden: signature zeroed, as the default firmware mode does */
341 ck_pi5pcie_door_decide(t, len, 2, 0x1000120000ull, &d);
342 CHECK(d.why == CK_PI5PCIE_WHY_MALFORMED);
343 t[0] = 'M';
344 /* a narrow bus range that is not the data window */
345 base[2] = 0x2000000000ull;
346 len = mcfg(t, 3, base, seg, bmax);
347 ck_pi5pcie_door_decide(t, len, 2, 0x1000120000ull, &d);
348 CHECK(d.why == CK_PI5PCIE_WHY_NARROW);
349 /* a real-looking ECAM: still reported as a candidate, never silently chosen */
350 bmax[2] = 1;
351 len = mcfg(t, 3, base, seg, bmax);
352 ck_pi5pcie_door_decide(t, len, 2, 0x1000120000ull, &d);
353 CHECK(d.why == CK_PI5PCIE_WHY_ECAM_CANDIDATE && d.base == 0x2000000000ull);
354 CHECK(strcmp(ck_pi5pcie_why_name(d.why), "?") != 0);
355}
356
357int main(void)
358{
359 t_cfg_access();
360 t_probe_rp1();
361 t_probe_needs_index();
362 t_bar();
363 t_ib_size();
364 t_ib_read();
365 t_verdict();
366 t_door();
367 return ck_t_verdict("test_pi5_pcie");
368}