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 / dev / pi5_pcie.c

429 lines · 16570 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/* pi5_pcie.c -- see pi5_pcie.h. The decision and parsing logic is hosted-testable
18 * (tests/test_pi5_pcie.c); the MMIO glue at the bottom is the CK_PI5_PCIE image only. */
19#include "pi5_pcie.h"
20
21/* TEST-only mutants (make pi5-pcie-mutants): each removes one decision and
22 * must make test_pi5_pcie FAIL. Host builds only; never part of an image. */
23#if defined(CK_PCIE_MUTANT_NO_OUTSIDE) || defined(CK_PCIE_MUTANT_NO_FULLCOVER) || \
24 defined(CK_PCIE_MUTANT_NO_REMAP) || defined(CK_PCIE_MUTANT_NO_INDEX) || defined(CK_PCIE_MUTANT_NO_ALIGN) || \
25 defined(CK_PCIE_MUTANT_NO_MERGE)
26#if !__STDC_HOSTED__
27#error "CK_PCIE_MUTANT_* are host test mutants only"
28#endif
29#endif
30
31static uint32_t le32(const uint8_t *p)
32{
33 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
34}
35
36int ck_pi5pcie_cfg_read32(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn, uint32_t reg,
37 uint32_t *out)
38{
39 if (!io || !io->rd || !io->wr || !out)
40 return CK_PI5PCIE_E_ARG;
41 if (reg >= 4096u || (reg & 3u) != 0 || fn > 7u)
42 return CK_PI5PCIE_E_ADDR;
43 if (bus == 0) { /* the root port only (PciSegmentLib.c:96-98) */
44 if (dev != 0 || fn != 0)
45 return CK_PI5PCIE_E_ADDR;
46 *out = io->rd(io->ctx, reg);
47 return CK_PI5PCIE_OK;
48 }
49 if (bus != 1 || dev != 0) /* one device on bus 1 (PciSegmentLib.c:101-104) */
50 return CK_PI5PCIE_E_ADDR;
51#ifndef CK_PCIE_MUTANT_NO_INDEX
52 io->wr(io->ctx, CK_PI5PCIE_EXT_CFG_INDEX, ((uint32_t)bus << 20) | ((uint32_t)dev << 15) | ((uint32_t)fn << 12));
53#endif
54 *out = io->rd(io->ctx, CK_PI5PCIE_EXT_CFG_DATA + reg);
55 return CK_PI5PCIE_OK;
56}
57
58int ck_pi5pcie_read_func(const struct ck_pi5pcie_io *io, uint8_t bus, uint8_t dev, uint8_t fn,
59 struct ck_pi5pcie_func *f)
60{
61 uint32_t v, i;
62 int rc;
63 if (!f)
64 return CK_PI5PCIE_E_ARG;
65 *f = (struct ck_pi5pcie_func){0};
66 f->bus = bus;
67 f->dev = dev;
68 f->fn = fn;
69 rc = ck_pi5pcie_cfg_read32(io, bus, dev, fn, 0, &v);
70 if (rc)
71 return rc;
72 f->vendor = (uint16_t)(v & 0xffffu);
73 f->device = (uint16_t)(v >> 16);
74 if (f->vendor == 0xffffu || f->vendor == 0)
75 return 1;
76 if (ck_pi5pcie_cfg_read32(io, bus, dev, fn, 4, &v))
77 return CK_PI5PCIE_E_ADDR;
78 f->command = (uint16_t)(v & 0xffffu);
79 if (ck_pi5pcie_cfg_read32(io, bus, dev, fn, 8, &v))
80 return CK_PI5PCIE_E_ADDR;
81 f->class_code = v >> 8;
82 f->revision = (uint8_t)(v & 0xffu);
83 if (ck_pi5pcie_cfg_read32(io, bus, dev, fn, 0xc, &v))
84 return CK_PI5PCIE_E_ADDR;
85 f->header_type = (uint8_t)((v >> 16) & 0x7fu);
86 f->nbars = f->header_type == 0 ? 6 : f->header_type == 1 ? 2 : 0;
87 for (i = 0; i < f->nbars; i++)
88 if (ck_pi5pcie_cfg_read32(io, bus, dev, fn, 0x10 + 4 * i, &f->bar_raw[i]))
89 return CK_PI5PCIE_E_ADDR;
90 if (f->header_type == 1 && !ck_pi5pcie_cfg_read32(io, bus, dev, fn, 0x18, &v)) {
91 f->primary = (uint8_t)(v & 0xffu);
92 f->secondary = (uint8_t)((v >> 8) & 0xffu);
93 f->subordinate = (uint8_t)((v >> 16) & 0xffu);
94 }
95 return 0;
96}
97
98int ck_pi5pcie_rp1_check(int read_rc, const struct ck_pi5pcie_func *f)
99{
100 if (read_rc != 0 || !f)
101 return CK_PI5PCIE_RP1_ABSENT;
102 if (f->vendor != CK_PI5PCIE_RP1_VENDOR || f->device != CK_PI5PCIE_RP1_DEVICE)
103 return CK_PI5PCIE_RP1_MISMATCH;
104 return CK_PI5PCIE_RP1_OK;
105}
106
107uint32_t ck_pi5pcie_bar_decode(const uint32_t *raw, uint32_t idx, uint32_t nbars, struct ck_pi5pcie_bar *b)
108{
109 uint32_t r;
110 b->kind = CK_PI5PCIE_BAR_BAD;
111 b->prefetch = 0;
112 b->addr = 0;
113 if (!raw || idx >= nbars)
114 return 1;
115 r = raw[idx];
116 if (r == 0) {
117 b->kind = CK_PI5PCIE_BAR_EMPTY;
118 return 1;
119 }
120 if (r & 1u) {
121 b->kind = CK_PI5PCIE_BAR_IO;
122 b->addr = r & ~3u;
123 return 1;
124 }
125 b->prefetch = (uint8_t)((r >> 3) & 1u);
126 switch ((r >> 1) & 3u) {
127 case 0:
128 b->kind = CK_PI5PCIE_BAR_MEM32;
129 b->addr = r & ~0xfu;
130 return 1;
131 case 2:
132 if (idx + 1 >= nbars)
133 return 1; /* a 64-bit BAR needs its upper half: BAD */
134 b->kind = CK_PI5PCIE_BAR_MEM64;
135 b->addr = (uint64_t)(r & ~0xfu) | ((uint64_t)raw[idx + 1] << 32);
136 return 2;
137 default:
138 return 1; /* 1 = below-1MiB legacy, 3 = reserved */
139 }
140}
141
142int ck_pi5pcie_ib_size_decode(uint32_t code, uint64_t *size)
143{
144 if (code == 0)
145 return 0;
146 if (code >= 1 && code <= 21) {
147 *size = 1ull << (code + 15);
148 return 1;
149 }
150 if (code >= 0x1c && code <= 0x1f) {
151 *size = 1ull << (code - 0x1c + 12);
152 return 1;
153 }
154 return -1;
155}
156
157/* Register offsets: Bcm2712Pcie.h:161-185 (PCIE_IB_WIN_LO/HI, PCIE_IB_WIN_REMAP_LO/HI). */
158static uint32_t ib_lo(uint32_t i) { return i == 0 ? 0x4034u : 0x40d4u + (i - 1) * 8u; }
159static uint32_t ib_remap_lo(uint32_t i) { return i == 0 ? 0x40b4u : 0x410cu + (i - 1) * 8u; }
160
161int ck_pi5pcie_ib_read(const struct ck_pi5pcie_io *io, uint32_t idx, struct ck_pi5pcie_ibwin *w)
162{
163 uint32_t lo, hi, rlo, rhi;
164 uint64_t size = 0;
165 int sd;
166 if (!io || !io->rd || !w || idx >= CK_PI5PCIE_IB_WINDOWS)
167 return CK_PI5PCIE_E_ARG;
168 *w = (struct ck_pi5pcie_ibwin){0};
169 w->idx = idx;
170 lo = io->rd(io->ctx, ib_lo(idx));
171 hi = io->rd(io->ctx, ib_lo(idx) + 4);
172 rlo = io->rd(io->ctx, ib_remap_lo(idx));
173 rhi = io->rd(io->ctx, ib_remap_lo(idx) + 4);
174 sd = ck_pi5pcie_ib_size_decode(lo & CK_PI5PCIE_BAR_SIZE_MASK, &size);
175 w->enabled = sd != 0;
176 w->bus_base = ((uint64_t)hi << 32) | (lo & ~CK_PI5PCIE_BAR_SIZE_MASK);
177 w->remap_en = (uint8_t)(rlo & 1u);
178 w->cpu_base = (uint64_t)(rlo & 0xfffff000u) | ((uint64_t)(rhi & 0xffu) << 32);
179 if (sd > 0) {
180 w->size = size;
181 w->size_valid = 1;
182#ifndef CK_PCIE_MUTANT_NO_ALIGN
183 if ((w->bus_base & (size - 1)) != 0)
184 w->size_valid = 0; /* base not aligned to size: not a window the model understands */
185#endif
186 }
187 return 0;
188}
189
190struct iv {
191 uint64_t s, e; /* [s, e) */
192};
193
194int ck_pi5pcie_ib_verdict(const struct ck_pi5pcie_ibwin *w, uint32_t n, uint32_t unmodelled, uint64_t b_cpu,
195 uint64_t b_size, struct ck_pi5pcie_dmav *out)
196{
197 struct iv v[CK_PI5PCIE_IB_WINDOWS];
198 uint32_t cnt = 0, i, j;
199 uint64_t b_end, reach = 0, inside = 0;
200 if (!out)
201 return CK_PI5PCIE_E_ARG;
202 *out = (struct ck_pi5pcie_dmav){0};
203 out->verdict = CK_PI5PCIE_DMA_UNKNOWN;
204 if ((n && !w) || n > CK_PI5PCIE_IB_WINDOWS || b_size == 0 || b_cpu > UINT64_MAX - b_size)
205 return CK_PI5PCIE_E_ARG;
206 b_end = b_cpu + b_size;
207 for (i = 0; i < n; i++) {
208 if (!w[i].enabled)
209 continue;
210 out->windows++;
211 if (!w[i].size_valid)
212 return 0; /* UNKNOWN */
213#ifndef CK_PCIE_MUTANT_NO_REMAP
214 if (!w[i].remap_en)
215 return 0; /* where the window lands in memory is not known: UNKNOWN */
216#endif
217 if (w[i].cpu_base > UINT64_MAX - w[i].size)
218 return 0;
219 v[cnt].s = w[i].cpu_base;
220 v[cnt].e = w[i].cpu_base + w[i].size;
221 cnt++;
222 }
223 if (cnt == 0) {
224 out->verdict = CK_PI5PCIE_DMA_NO_WINDOW;
225 return 0;
226 }
227 for (i = 1; i < cnt; i++) { /* insertion sort by start */
228 struct iv t = v[i];
229 for (j = i; j > 0 && v[j - 1].s > t.s; j--)
230 v[j] = v[j - 1];
231 v[j] = t;
232 }
233#ifndef CK_PCIE_MUTANT_NO_MERGE
234 for (i = 0, j = 0; i < cnt; i++) { /* union: overlapping or touching ranges merge */
235 if (j > 0 && v[i].s <= v[j - 1].e) {
236 if (v[i].e > v[j - 1].e)
237 v[j - 1].e = v[i].e;
238 } else {
239 v[j++] = v[i];
240 }
241 }
242 cnt = j;
243#endif
244 for (i = 0; i < cnt; i++) {
245 uint64_t lo = v[i].s > b_cpu ? v[i].s : b_cpu;
246 uint64_t hi = v[i].e < b_end ? v[i].e : b_end;
247 reach += v[i].e - v[i].s;
248 if (hi > lo)
249 inside += hi - lo;
250 }
251 out->reach_bytes = reach;
252 out->inside_bytes = inside;
253 out->outside_bytes = reach - inside;
254 if (inside == 0) {
255 out->verdict = CK_PI5PCIE_DMA_MISS;
256 return 0;
257 }
258#ifndef CK_PCIE_MUTANT_NO_FULLCOVER
259 if (inside < b_size) {
260 out->verdict = CK_PI5PCIE_DMA_PARTIAL;
261 return 0;
262 }
263#endif
264#ifndef CK_PCIE_MUTANT_NO_OUTSIDE
265 if (out->outside_bytes != 0) {
266 out->verdict = CK_PI5PCIE_DMA_WIDER;
267 return 0;
268 }
269#endif
270 out->verdict = unmodelled ? CK_PI5PCIE_DMA_UNKNOWN : CK_PI5PCIE_DMA_CONFINED;
271 return 0;
272}
273
274const char *ck_pi5pcie_verdict_name(int v)
275{
276 switch (v) {
277 case CK_PI5PCIE_DMA_NO_WINDOW: return "NO_WINDOW";
278 case CK_PI5PCIE_DMA_MISS: return "MISS";
279 case CK_PI5PCIE_DMA_PARTIAL: return "PARTIAL_COVER";
280 case CK_PI5PCIE_DMA_WIDER: return "NOT_CONFINED";
281 case CK_PI5PCIE_DMA_CONFINED: return "CONFINED";
282 default: return "UNKNOWN";
283 }
284}
285
286void ck_pi5pcie_door_decide(const uint8_t *t, size_t len, uint16_t segment, uint64_t rc_phys,
287 struct ck_pi5pcie_door *out)
288{
289 size_t off;
290 out->why = CK_PI5PCIE_WHY_NO_MCFG;
291 out->base = 0;
292 out->bus_min = out->bus_max = 0;
293 if (!t)
294 return;
295 out->why = CK_PI5PCIE_WHY_MALFORMED;
296 if (len < 44u + 16u || t[0] != 'M' || t[1] != 'C' || t[2] != 'F' || t[3] != 'G' || le32(t + 4) != len ||
297 (len - 44u) % 16u != 0)
298 return;
299 out->why = CK_PI5PCIE_WHY_NO_SEGMENT;
300 for (off = 44; off + 16u <= len; off += 16u) {
301 const uint8_t *e = t + off;
302 uint64_t base = (uint64_t)le32(e) | ((uint64_t)le32(e + 4) << 32);
303 if ((uint16_t)(e[8] | (e[9] << 8)) != segment)
304 continue;
305 out->base = base;
306 out->bus_min = e[10];
307 out->bus_max = e[11];
308 if (base == rc_phys + CK_PI5PCIE_EXT_CFG_DATA)
309 out->why = CK_PI5PCIE_WHY_DATA_WINDOW;
310 else if (e[11] < 1 || e[10] > 1)
311 out->why = CK_PI5PCIE_WHY_NARROW;
312 else
313 out->why = CK_PI5PCIE_WHY_ECAM_CANDIDATE;
314 return;
315 }
316}
317
318const char *ck_pi5pcie_why_name(int why)
319{
320 switch (why) {
321 case CK_PI5PCIE_WHY_NO_MCFG: return "no_mcfg";
322 case CK_PI5PCIE_WHY_MALFORMED: return "mcfg_malformed";
323 case CK_PI5PCIE_WHY_NO_SEGMENT: return "no_segment_2_entry";
324 case CK_PI5PCIE_WHY_DATA_WINDOW: return "entry_is_the_4k_data_window";
325 case CK_PI5PCIE_WHY_NARROW: return "entry_bus_range_excludes_bus_1";
326 case CK_PI5PCIE_WHY_ECAM_CANDIDATE: return "ecam_candidate_unverified_not_used";
327 default: return "?";
328 }
329}
330
331#ifdef CK_PI5_PCIE
332#include "ck.h"
333#include "dma_window.h"
334
335static volatile uint8_t *rc;
336
337static uint32_t mm_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(rc + off); }
338static void mm_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(rc + off) = v; }
339
340static void print_bar(const struct ck_pi5pcie_func *f, uint32_t i, struct ck_pi5pcie_bar *b)
341{
342 static const char *const k[] = {"empty", "mem32", "mem64", "io", "bad"};
343 ck_printf("pi5_pcie: rp1 bar%u raw=0x%08x kind=%s%s addr=0x%llx size=unsized (read-only)\n", i, f->bar_raw[i],
344 k[b->kind], b->prefetch ? " prefetch" : "", (unsigned long long)b->addr);
345}
346
347void ck_pi5pcie_report(void)
348{
349 static const struct ck_pi5pcie_io io = {mm_rd, mm_wr, 0};
350 struct ck_pi5pcie_func root, rp1;
351 struct ck_pi5pcie_door door;
352 struct ck_pi5pcie_ibwin win[CK_PI5PCIE_IB_WINDOWS];
353 struct ck_pi5pcie_dmav dv;
354 struct ck_dmaw dw;
355 int rrc, rrp1, ver, n_en = 0;
356 uint32_t i, unmodelled = 0, idx_before;
357 uint64_t b_phys = 0;
358 void *bounce;
359 const uint8_t *mcfg = (const uint8_t *)ck_acpi_find("MCFG");
360
361 ck_printf("pi5_pcie: PCIe2 root complex 0x%llx (RP1 host), software-only DMA confinement (Pi 5, weaker than the Spark)\n",
362 (unsigned long long)CK_PI5PCIE_RC2_PHYS);
363 ck_pi5pcie_door_decide(mcfg, mcfg ? (size_t)le32(mcfg + 4) : 0, CK_PI5PCIE_SEGMENT, CK_PI5PCIE_RC2_PHYS, &door);
364 ck_printf("pi5_pcie: door=rc_window (index 0x%x, data 0x%x); ecam not used: %s mcfg_seg2_base=0x%llx bus=%u-%u\n",
365 CK_PI5PCIE_EXT_CFG_INDEX, CK_PI5PCIE_EXT_CFG_DATA, ck_pi5pcie_why_name(door.why),
366 (unsigned long long)door.base, door.bus_min, door.bus_max);
367 rc = (volatile uint8_t *)ck_mmio_try_map(CK_PI5PCIE_RC2_PHYS, CK_PI5PCIE_RC_BYTES);
368 if (!rc) {
369 ck_printf("pi5_pcie: FAIL map 0x%llx (refused); nothing read\n", (unsigned long long)CK_PI5PCIE_RC2_PHYS);
370 return;
371 }
372 idx_before = mm_rd(0, CK_PI5PCIE_EXT_CFG_INDEX);
373 rrc = ck_pi5pcie_read_func(&io, 0, 0, 0, &root);
374 if (rrc == 0)
375 ck_printf("pi5_pcie: root 00:00.0 %04x:%04x class=%06x rev=%02x type=%u sec=%u sub=%u cmd=0x%04x\n", root.vendor,
376 root.device, root.class_code, root.revision, root.header_type, root.secondary, root.subordinate,
377 root.command);
378 else
379 ck_printf("pi5_pcie: root 00:00.0 read rc=%d\n", rrc);
380 rrp1 = ck_pi5pcie_read_func(&io, 1, 0, 0, &rp1);
381 ver = ck_pi5pcie_rp1_check(rrp1, &rp1);
382 ck_printf("pi5_pcie: index_before=0x%08x index_now=0x%08x (selected 01:00.0)\n", idx_before,
383 mm_rd(0, CK_PI5PCIE_EXT_CFG_INDEX));
384 if (ver == CK_PI5PCIE_RP1_ABSENT)
385 ck_printf("pi5_pcie: rp1 01:00.0 ABSENT (read rc=%d vendor=%04x)\n", rrp1, rp1.vendor);
386 else {
387 ck_printf("pi5_pcie: rp1 01:00.0 %04x:%04x %s (expected %04x:%04x) class=%06x rev=%02x type=%u cmd=0x%04x\n",
388 rp1.vendor, rp1.device, ver == CK_PI5PCIE_RP1_OK ? "MATCH" : "MISMATCH", CK_PI5PCIE_RP1_VENDOR,
389 CK_PI5PCIE_RP1_DEVICE, rp1.class_code, rp1.revision, rp1.header_type, rp1.command);
390 for (i = 0; i < rp1.nbars;) {
391 struct ck_pi5pcie_bar b;
392 uint32_t used = ck_pi5pcie_bar_decode(rp1.bar_raw, i, rp1.nbars, &b);
393 print_bar(&rp1, i, &b);
394 i += used;
395 }
396 }
397 for (i = 0; i < CK_PI5PCIE_IB_WINDOWS; i++) {
398 if (ck_pi5pcie_ib_read(&io, i, &win[i]))
399 continue;
400 if (win[i].enabled) {
401 n_en++;
402 ck_printf("pi5_pcie: inbound win%u bus=0x%llx size=0x%llx cpu=0x%llx remap=%u valid=%u\n", i,
403 (unsigned long long)win[i].bus_base, (unsigned long long)win[i].size,
404 (unsigned long long)win[i].cpu_base, win[i].remap_en, win[i].size_valid);
405 }
406 }
407 if (mm_rd(0, CK_PI5PCIE_RC_BAR1_LO) & CK_PI5PCIE_BAR_SIZE_MASK)
408 unmodelled++;
409 if (mm_rd(0, CK_PI5PCIE_RC_BAR3_LO) & CK_PI5PCIE_BAR_SIZE_MASK)
410 unmodelled++;
411 ck_printf("pi5_pcie: inbound enabled=%d other_bar_windows=%u (gisb/scb; 0 = disabled)\n", n_en, unmodelled);
412 bounce = ck_dma_alloc(CK_DMAW_MAX_BYTES, CK_DMAW_MAX_BYTES, &b_phys);
413 if (!bounce || ck_dmaw_init(&dw, bounce, b_phys, CK_DMAW_MAX_BYTES) != CK_DMAW_OK) {
414 ck_printf("pi5_dma: FAIL no bounce window (alloc/init); verdict not computed\n");
415 return;
416 }
417 ck_pi5pcie_ib_verdict(win, CK_PI5PCIE_IB_WINDOWS, unmodelled, b_phys, CK_DMAW_MAX_BYTES, &dv);
418 ck_printf("pi5_dma: bounce cpu=0x%llx size=0x%llx (ADR 0020 rule 1, zeroed, bus==cpu per ck_dma_alloc)\n",
419 (unsigned long long)b_phys, (unsigned long long)dw.size);
420 ck_printf("pi5_dma: hw_window=%s reach=0x%llx inside=0x%llx outside=0x%llx\n", ck_pi5pcie_verdict_name(dv.verdict),
421 (unsigned long long)dv.reach_bytes, (unsigned long long)dv.inside_bytes,
422 (unsigned long long)dv.outside_bytes);
423 if (dv.verdict == CK_PI5PCIE_DMA_CONFINED)
424 ck_printf("pi5_dma: hardware window confines DMA to the bounce region\n");
425 else
426 ck_printf("pi5_dma: hardware window does NOT confine DMA to the bounce region (%s): software checks only\n",
427 ck_pi5pcie_verdict_name(dv.verdict));
428}
429#endif