Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/pci.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / pci.c` 693 lines ยท 26684 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 /* pci.c -- PCIe ECAM discovery from ACPI MCFG (see pci.h). Freestanding. */ 18 #include "pci.h" 19 #include "ck.h" 20 21 #define CFG_VENDOR 0x00u 22 #define CFG_COMMAND 0x04u 23 #define CFG_CLASSREV 0x08u 24 #define CFG_HEADER 0x0eu 25 #define CFG_BAR0 0x10u 26 #define CFG_SUBSYS 0x2cu 27 #define CFG_SECONDARY_BUS 0x19u 28 #define CMD_IO 0x1u 29 #define CMD_MEM 0x2u 30 #define CMD_BM 0x4u 31 32 static uint32_t le32(const uint8_t *p) 33 { 34 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; 35 } 36 static uint64_t le64(const uint8_t *p) { return (uint64_t)le32(p) | (uint64_t)le32(p + 4) << 32; } 37 38 int pci_mcfg_parse(const uint8_t *t, size_t len, pci_ecam *out) 39 { 40 if (!t || !out) return PCI_E_ARG; 41 if (len < 44u + 16u) return PCI_E_MCFG; 42 if (t[0] != 'M' || t[1] != 'C' || t[2] != 'F' || t[3] != 'G') return PCI_E_MCFG; 43 uint32_t tl = le32(t + 4); 44 if (tl != len) return PCI_E_MCFG; 45 if ((len - 44u) % 16u != 0) return PCI_E_MCFG; 46 for (size_t off = 44; off + 16 <= len; off += 16) { 47 const uint8_t *e = t + off; 48 uint16_t seg = (uint16_t)(e[8] | e[9] << 8); 49 if (seg != 0) continue; 50 pci_ecam c; 51 c.base = le64(e); 52 c.segment = seg; 53 c.start_bus = e[10]; 54 c.end_bus = e[11]; 55 if (c.base == 0 || (c.base & 0xfffffu) != 0 || c.start_bus > c.end_bus) return PCI_E_MCFG; 56 *out = c; 57 return PCI_OK; 58 } 59 return PCI_E_MCFG; 60 } 61 62 volatile uint8_t *pci_cfg(const pci_bus_access *a, uint8_t bus, uint8_t dev, uint8_t fn) 63 { 64 if (bus < a->start_bus || bus > a->end_bus || dev > 31 || fn > 7) return 0; 65 uint64_t off = ((uint64_t)(bus - a->start_bus) << 20) | ((uint64_t)dev << 15) | ((uint64_t)fn << 12); 66 return a->ecam + off; 67 } 68 uint32_t pci_r32(volatile uint8_t *c, uint32_t off) { return *(volatile uint32_t *)(c + (off & ~3u)); } 69 void pci_w32(volatile uint8_t *c, uint32_t off, uint32_t v) { *(volatile uint32_t *)(c + (off & ~3u)) = v; } 70 uint16_t pci_r16(volatile uint8_t *c, uint32_t off) 71 { 72 return (uint16_t)(pci_r32(c, off) >> ((off & 2u) * 8u)); 73 } 74 void pci_w16(volatile uint8_t *c, uint32_t off, uint16_t v) 75 { 76 /* 32-bit read-modify-write keeps every access dword sized; only used on 77 * COMMAND (whose STATUS half is write-1-to-clear: write zeros there). */ 78 uint32_t sh = (off & 2u) * 8u; 79 uint32_t keep = (off & 2u) ? 0x0000ffffu : 0u; 80 uint32_t old = pci_r32(c, off) & keep; 81 pci_w32(c, off, old | ((uint32_t)v << sh)); 82 } 83 84 uint64_t pci_bar_size(uint32_t lo, uint32_t hi, int is64) 85 { 86 if (is64) { 87 uint64_t mask = ((uint64_t)hi << 32) | (uint64_t)(lo & ~0xfu); 88 return mask ? ~mask + 1u : 0; 89 } 90 uint32_t mask = lo & ~0xfu; 91 return mask ? (uint64_t)(uint32_t)(~mask + 1u) : 0; 92 } 93 uint64_t pci_window_alloc(pci_window *w, uint64_t size) 94 { 95 if (size == 0 || (size & (size - 1u)) != 0) return 0; 96 uint64_t a = (w->next + size - 1u) & ~(size - 1u); 97 if (a < w->next || a + size < a || a + size > w->limit) return 0; 98 w->next = a + size; 99 return a; 100 } 101 102 #ifdef CK_HOST_TEST 103 /* Host tests only (never in the kernel image): a fake ECAM is plain memory and cannot answer the all-ones 104 * BAR sizing write with a size mask. When set, a non-zero return is the size to pretend BAR `i` has. */ 105 uint64_t (*pci_host_bar_size)(volatile uint8_t *cfg, uint32_t i); 106 #endif 107 108 static void size_bars(pci_func *f) 109 { 110 volatile uint8_t *c = f->cfg; 111 uint32_t nbars = (f->header_type & 0x7fu) == 0 ? 6u : ((f->header_type & 0x7fu) == 1 ? 2u : 0u); 112 uint16_t cmd = pci_r16(c, CFG_COMMAND); 113 pci_w16(c, CFG_COMMAND, (uint16_t)(cmd & ~(CMD_IO | CMD_MEM | CMD_BM))); 114 for (uint32_t i = 0; i < nbars; i++) { 115 pci_bar *b = &f->bar[i]; 116 uint32_t off = CFG_BAR0 + 4u * i; 117 uint32_t orig = pci_r32(c, off); 118 if (orig & 1u) { /* I/O BAR: report, never assign (no I/O space on AArch64 here) */ 119 pci_w32(c, off, 0xffffffffu); 120 uint32_t rb = pci_r32(c, off); 121 pci_w32(c, off, orig); 122 b->io = 1; 123 b->addr = orig & ~3u; 124 b->size = (rb & ~3u) ? (uint64_t)((~(rb & ~3u) + 1u) & 0xffffu) : 0; 125 continue; 126 } 127 int is64 = ((orig >> 1) & 3u) == 2u && i + 1 < nbars; 128 uint32_t orig_hi = is64 ? pci_r32(c, off + 4) : 0; 129 pci_w32(c, off, 0xffffffffu); 130 if (is64) pci_w32(c, off + 4, 0xffffffffu); 131 uint32_t lo = pci_r32(c, off); 132 uint32_t hi = is64 ? pci_r32(c, off + 4) : 0; 133 #ifdef CK_HOST_TEST 134 if (pci_host_bar_size) { 135 uint64_t emu = pci_host_bar_size(c, i); 136 if (emu) { 137 uint64_t mask = ~(emu - 1u); 138 lo = (uint32_t)mask | (orig & 0xfu); 139 hi = is64 ? (uint32_t)(mask >> 32) : 0; 140 } 141 } 142 #endif 143 pci_w32(c, off, orig); 144 if (is64) pci_w32(c, off + 4, orig_hi); 145 b->is64 = (uint8_t)is64; 146 b->prefetch = (uint8_t)((orig >> 3) & 1u); 147 b->size = pci_bar_size(lo, hi, is64); 148 b->addr = ((uint64_t)orig_hi << 32) | (orig & ~0xfu); 149 if (is64) { 150 i++; /* upper half is part of this BAR */ 151 } 152 } 153 pci_w16(c, CFG_COMMAND, cmd); 154 } 155 156 static void fill_func(pci_func *f, uint16_t segment, uint8_t bus, uint8_t dev, uint8_t fn, volatile uint8_t *c) 157 { 158 uint32_t id = pci_r32(c, CFG_VENDOR); 159 f->segment = segment; 160 f->bus = bus; 161 f->dev = dev; 162 f->fn = fn; 163 f->vendor = (uint16_t)id; 164 f->device = (uint16_t)(id >> 16); 165 f->class_code = pci_r32(c, CFG_CLASSREV) >> 8; 166 f->header_type = (uint8_t)(pci_r32(c, 0x0c) >> 16); 167 uint32_t ss = (f->header_type & 0x7fu) == 0 ? pci_r32(c, CFG_SUBSYS) : 0; 168 f->subsys_vendor = (uint16_t)ss; 169 f->subsys_id = (uint16_t)(ss >> 16); 170 f->cfg = c; 171 for (uint32_t i = 0; i < PCI_MAX_BARS; i++) { 172 f->bar[i].addr = f->bar[i].size = 0; 173 f->bar[i].is64 = f->bar[i].prefetch = f->bar[i].io = f->bar[i].assigned_here = 0; 174 } 175 size_bars(f); 176 } 177 178 static int add_func(pci_system *s, uint8_t bus, uint8_t dev, uint8_t fn, volatile uint8_t *c) 179 { 180 if (s->n >= PCI_MAX_FUNCS) return PCI_E_FULL; 181 fill_func(&s->f[s->n++], s->ecam.segment, bus, dev, fn, c); 182 return PCI_OK; 183 } 184 185 static int scan_bus(pci_system *s, uint8_t bus, uint32_t depth) 186 { 187 for (uint8_t dev = 0; dev < 32; dev++) { 188 for (uint8_t fn = 0; fn < 8; fn++) { 189 volatile uint8_t *c = pci_cfg(&s->acc, bus, dev, fn); 190 if (!c) return PCI_OK; 191 uint16_t vendor = (uint16_t)pci_r32(c, CFG_VENDOR); 192 if (vendor == 0xffffu || vendor == 0) { 193 if (fn == 0) break; 194 continue; 195 } 196 int rc = add_func(s, bus, dev, fn, c); 197 if (rc) return rc; 198 const pci_func *f = &s->f[s->n - 1]; 199 if ((f->header_type & 0x7fu) == 1 && depth < 8) { 200 uint8_t sec = (uint8_t)(pci_r32(c, 0x18) >> 8); 201 if (sec > bus && sec <= s->acc.end_bus) { 202 s->bridges_followed++; 203 rc = scan_bus(s, sec, depth + 1); 204 if (rc) return rc; 205 } 206 } 207 if (fn == 0 && !(f->header_type & 0x80u)) break; 208 } 209 } 210 return PCI_OK; 211 } 212 213 static void assign_bars(pci_system *s, pci_window *win) 214 { 215 if (win) { /* never hand out space below a firmware assignment in the window */ 216 for (uint32_t k = 0; k < s->n; k++) 217 for (uint32_t i = 0; i < PCI_MAX_BARS; i++) { 218 const pci_bar *b = &s->f[k].bar[i]; 219 if (b->io || b->size == 0 || b->addr == 0) continue; 220 if (b->addr >= win->base && b->addr < win->limit && b->addr + b->size > win->next) 221 win->next = b->addr + b->size; 222 } 223 } 224 for (uint32_t k = 0; k < s->n; k++) { 225 pci_func *f = &s->f[k]; 226 f->bars_ok = 1; 227 for (uint32_t i = 0; i < PCI_MAX_BARS; i++) { 228 pci_bar *b = &f->bar[i]; 229 if (b->io || b->size == 0 || b->addr != 0) continue; 230 uint64_t a = win ? pci_window_alloc(win, b->size) : 0; 231 if (a == 0 || (!b->is64 && (a >> 32) != 0)) { 232 f->bars_ok = 0; 233 continue; 234 } 235 uint32_t off = CFG_BAR0 + 4u * i; 236 uint32_t low_flags = pci_r32(f->cfg, off) & 0xfu; 237 pci_w32(f->cfg, off, (uint32_t)a | low_flags); 238 if (b->is64) pci_w32(f->cfg, off + 4, (uint32_t)(a >> 32)); 239 b->addr = a; 240 b->assigned_here = 1; 241 } 242 } 243 } 244 245 int pci_enumerate(pci_system *s, pci_window *win) 246 { 247 if (!s || !s->acc.ecam) return PCI_E_ARG; 248 s->n = 0; 249 s->bridges_followed = 0; 250 int rc = scan_bus(s, s->acc.start_bus, 0); 251 if (rc) return rc; 252 assign_bars(s, win); 253 return PCI_OK; 254 } 255 256 int pci_func_from_found(pci_func *out, const pci_found *f, const pci_bus_access *a) 257 { 258 if (!out || !f || !a || !a->ecam) return PCI_E_ARG; 259 if ((f->header_type & 0x7fu) != 0) return PCI_E_ARG; /* endpoints only */ 260 volatile uint8_t *c = pci_cfg(a, f->bus, f->dev, f->fn); 261 if (!c) return PCI_E_ARG; 262 uint32_t id = pci_r32(c, CFG_VENDOR); 263 /* The ECAM must still answer with the identity discovery recorded. */ 264 if ((uint16_t)id != f->vendor || (uint16_t)(id >> 16) != f->device) return PCI_E_ARG; 265 fill_func(out, f->segment, f->bus, f->dev, f->fn, c); 266 out->bars_ok = 1; 267 for (uint32_t i = 0; i < PCI_MAX_BARS; i++) 268 if (!out->bar[i].io && out->bar[i].size && !out->bar[i].addr) out->bars_ok = 0; 269 return PCI_OK; 270 } 271 272 void pci_enable(const pci_func *f, int bm) 273 { 274 uint16_t cmd = pci_r16(f->cfg, CFG_COMMAND); 275 cmd |= CMD_MEM; 276 if (bm) cmd |= CMD_BM; 277 pci_w16(f->cfg, CFG_COMMAND, cmd); 278 } 279 280 #ifdef CK_HOST_TEST 281 int pci_host_bme_stuck; /* host tests only: model a command register whose bus master bit will not clear */ 282 #endif 283 int pci_bus_master_off(const pci_func *f) 284 { 285 #ifdef CK_HOST_TEST 286 if (pci_host_bme_stuck) return -1; 287 #endif 288 uint16_t cmd = pci_r16(f->cfg, CFG_COMMAND); 289 pci_w16(f->cfg, CFG_COMMAND, (uint16_t)(cmd & ~CMD_BM)); 290 return (pci_r16(f->cfg, CFG_COMMAND) & CMD_BM) ? -1 : 0; 291 } 292 293 294 void pci_sweep_bus_master(const pci_bus_access *a, pci_sweep *out) 295 { 296 pci_sweep s; 297 uint8_t *z = (uint8_t *)&s; 298 for (size_t i = 0; i < sizeof s; i++) z[i] = 0; 299 for (uint32_t bus = a->start_bus; bus <= a->end_bus; bus++) { 300 for (uint8_t dev = 0; dev < 32; dev++) { 301 for (uint8_t fn = 0; fn < 8; fn++) { 302 volatile uint8_t *c = pci_cfg(a, (uint8_t)bus, dev, fn); 303 if (!c) break; 304 if ((pci_r32(c, CFG_VENDOR) & 0xffffu) == 0xffffu) { 305 if (fn == 0) break; 306 continue; 307 } 308 s.functions++; 309 uint8_t header = (uint8_t)(pci_r32(c, 0x0c) >> 16); 310 uint16_t cmd = pci_r16(c, CFG_COMMAND); 311 if ((header & 0x7fu) == 1u) { 312 s.bridges++; 313 if (cmd & CMD_BM) s.bridges_bme++; 314 } else if (cmd & CMD_BM) { 315 pci_w16(c, CFG_COMMAND, (uint16_t)(cmd & ~CMD_BM)); 316 uint16_t after = pci_r16(c, CFG_COMMAND); 317 if (s.endpoints_bme < PCI_SWEEP_FINDINGS) { 318 pci_bme_finding *f = &s.f[s.endpoints_bme]; 319 f->bus = (uint8_t)bus; 320 f->dev = dev; 321 f->fn = fn; 322 f->command_before = cmd; 323 f->command_after = after; 324 } 325 s.endpoints_bme++; 326 if (after & CMD_BM) s.still_enabled++; 327 } 328 if (fn == 0 && !(header & 0x80u)) break; 329 } 330 } 331 } 332 *out = s; 333 } 334 335 void pci_sweep_report(const pci_ecam *e, const pci_sweep *s) 336 { 337 ck_printf("dma_sweep: seg %04x bus %02x-%02x functions=%u bridges=%u bridges_bme=%u endpoints_bme_found=%u " 338 "still_enabled=%u\n", 339 e->segment, e->start_bus, e->end_bus, s->functions, s->bridges, s->bridges_bme, s->endpoints_bme, 340 s->still_enabled); 341 uint32_t n = s->endpoints_bme < PCI_SWEEP_FINDINGS ? s->endpoints_bme : PCI_SWEEP_FINDINGS; 342 for (uint32_t i = 0; i < n; i++) { 343 const pci_bme_finding *f = &s->f[i]; 344 ck_printf("dma_sweep: bme %s %04x:%02x:%02x.%u command 0x%04x -> 0x%04x\n", 345 (f->command_after & CMD_BM) ? "STUCK" : "cleared", e->segment, f->bus, f->dev, f->fn, 346 f->command_before, f->command_after); 347 } 348 } 349 350 const pci_func *pci_find_class(const pci_system *s, uint32_t cc, uint32_t mask) 351 { 352 for (uint32_t i = 0; i < s->n; i++) 353 if ((s->f[i].class_code & mask) == (cc & mask)) return &s->f[i]; 354 return 0; 355 } 356 357 const pci_func *pci_find_id(const pci_system *s, uint16_t vendor, uint16_t device) 358 { 359 for (uint32_t i = 0; i < s->n; i++) 360 if (s->f[i].vendor == vendor && s->f[i].device == device) return &s->f[i]; 361 return 0; 362 } 363 364 const char *pci_class_name(uint32_t cc) 365 { 366 switch (cc >> 8) { 367 case 0x0108: return "nvme"; 368 case 0x0200: return "ethernet"; 369 case 0x0600: return "host-bridge"; 370 case 0x0604: return "pci-bridge"; 371 case 0x0c03: return "usb"; 372 case 0x0300: return "display"; 373 case 0x0100: return "scsi"; 374 case 0x0106: return "sata"; 375 case 0x0180: return "storage"; 376 case 0x0700: return "serial"; 377 default: break; 378 } 379 if ((cc >> 16) == 0x01) return "storage"; 380 if ((cc >> 16) == 0x02) return "network"; 381 return "other"; 382 } 383 384 /* ---- stage glue --------------------------------------------------------- */ 385 386 #define QEMU_VIRT_ECAM_HIGH 0x4010000000ull 387 #define QEMU_VIRT_ECAM_LOW 0x3f000000ull 388 #define QEMU_VIRT_MMIO32_BASE 0x10000000ull 389 #define QEMU_VIRT_MMIO32_LIMIT 0x3eff0000ull 390 391 int pci_stage_probe(pci_system *s) 392 { 393 const uint8_t *mcfg = (const uint8_t *)ck_acpi_find("MCFG"); 394 if (!mcfg) { 395 ck_printf("pci: no ACPI MCFG table\n"); 396 return PCI_E_NO_MCFG; 397 } 398 uint32_t len = le32(mcfg + 4); 399 int rc = pci_mcfg_parse(mcfg, len, &s->ecam); 400 if (rc) { 401 ck_printf("pci: MCFG malformed (len=%u)\n", len); 402 return rc; 403 } 404 uint64_t first = s->ecam.base + ((uint64_t)s->ecam.start_bus << 20); 405 uint64_t bytes = (uint64_t)(s->ecam.end_bus - s->ecam.start_bus + 1u) << 20; 406 s->acc.ecam = (volatile uint8_t *)ck_mmio_map(first, (size_t)bytes); 407 s->acc.start_bus = s->ecam.start_bus; 408 s->acc.end_bus = s->ecam.end_bus; 409 ck_printf("pci: ecam base=0x%llx seg=%u bus=%u-%u\n", (unsigned long long)s->ecam.base, 410 s->ecam.segment, s->ecam.start_bus, s->ecam.end_bus); 411 pci_window win, *wp = 0; 412 if (s->ecam.base == QEMU_VIRT_ECAM_HIGH || s->ecam.base == QEMU_VIRT_ECAM_LOW) { 413 win.base = win.next = QEMU_VIRT_MMIO32_BASE; 414 win.limit = QEMU_VIRT_MMIO32_LIMIT; 415 wp = &win; 416 } 417 rc = pci_enumerate(s, wp); 418 if (rc) { 419 ck_printf("pci: enumerate failed rc=%d\n", rc); 420 return rc; 421 } 422 uint32_t assigned = 0, unassigned = 0; 423 for (uint32_t i = 0; i < s->n; i++) { 424 const pci_func *f = &s->f[i]; 425 ck_printf("pci: %02x:%02x.%u %04x:%04x class=%06x %s\n", f->bus, f->dev, f->fn, f->vendor, 426 f->device, f->class_code, pci_class_name(f->class_code)); 427 for (uint32_t b = 0; b < PCI_MAX_BARS; b++) { 428 if (f->bar[b].assigned_here) assigned++; 429 if (!f->bar[b].io && f->bar[b].size && !f->bar[b].addr) unassigned++; 430 } 431 } 432 ck_printf("pci: %u functions, %u bridges followed, bars assigned_here=%u unassigned=%u%s\n", s->n, 433 s->bridges_followed, assigned, unassigned, 434 wp ? " (window: QEMU virt mmio32, QEMU-only assumption)" : " (no window: firmware assignment only)"); 435 return PCI_OK; 436 } 437 438 /* ---- Read-only discovery (see pci.h). ---- */ 439 440 int pci_mcfg_parse_all(const uint8_t *t, size_t len, pci_ecam *out, uint32_t cap, uint32_t *n_out) 441 { 442 if (!t || !out || !n_out) return PCI_E_ARG; 443 if (len < 44u + 16u) return PCI_E_MCFG; 444 if (t[0] != 'M' || t[1] != 'C' || t[2] != 'F' || t[3] != 'G') return PCI_E_MCFG; 445 if (le32(t + 4) != len) return PCI_E_MCFG; 446 if ((len - 44u) % 16u != 0) return PCI_E_MCFG; 447 uint32_t n = 0; 448 for (size_t off = 44; off + 16 <= len; off += 16) { 449 const uint8_t *e = t + off; 450 pci_ecam c; 451 c.base = le64(e); 452 c.segment = (uint16_t)(e[8] | e[9] << 8); 453 c.start_bus = e[10]; 454 c.end_bus = e[11]; 455 if (c.base == 0 || (c.base & 0xfffffu) != 0 || c.start_bus > c.end_bus) return PCI_E_MCFG; 456 uint64_t span = (uint64_t)((uint32_t)c.end_bus + 1u) << 20; 457 if (c.base + span < c.base) return PCI_E_MCFG; 458 for (uint32_t i = 0; i < n; i++) { 459 if (out[i].segment == c.segment && out[i].start_bus <= c.end_bus && c.start_bus <= out[i].end_bus) 460 return PCI_E_MCFG; 461 } 462 if (n >= cap) return PCI_E_FULL; 463 out[n++] = c; 464 } 465 *n_out = n; 466 return PCI_OK; 467 } 468 469 /* The only config access discovery uses: const volatile, so a store through 470 * it does not compile. */ 471 static uint32_t rd32(const volatile uint8_t *c, uint32_t off) { return *(const volatile uint32_t *)(c + (off & ~3u)); } 472 473 #define DISC_MAX_DEPTH 8u 474 475 typedef struct { 476 const pci_bus_access *a; 477 uint16_t segment; 478 pci_found *out; 479 uint32_t cap, n, bridges; 480 uint32_t seen[8]; /* 256-bit visited set of scanned buses */ 481 } disc_ctx; 482 483 static int disc_bus(disc_ctx *d, uint8_t bus, uint32_t depth) 484 { 485 d->seen[bus >> 5] |= 1u << (bus & 31u); 486 for (uint32_t dev = 0; dev < 32; dev++) { 487 for (uint32_t fn = 0; fn < 8; fn++) { 488 const volatile uint8_t *c = pci_cfg(d->a, bus, (uint8_t)dev, (uint8_t)fn); 489 if (!c) return PCI_OK; 490 uint32_t id = rd32(c, CFG_VENDOR); 491 uint16_t vendor = (uint16_t)id; 492 if (vendor == 0xffffu || vendor == 0) { 493 if (fn == 0) break; /* no function 0: no device */ 494 continue; 495 } 496 uint8_t ht = (uint8_t)(rd32(c, 0x0c) >> 16); 497 if (d->n >= d->cap) return PCI_E_FULL; 498 pci_found *f = &d->out[d->n++]; 499 *f = (pci_found){0}; 500 f->segment = d->segment; 501 f->bus = bus; 502 f->dev = (uint8_t)dev; 503 f->fn = (uint8_t)fn; 504 f->vendor = vendor; 505 f->device = (uint16_t)(id >> 16); 506 uint32_t cr = rd32(c, CFG_CLASSREV); 507 f->class_code = cr >> 8; 508 f->revision = (uint8_t)cr; 509 f->command = (uint16_t)rd32(c, CFG_COMMAND); /* read only: decode state as firmware left it */ 510 f->header_type = ht; 511 uint32_t kind = ht & 0x7fu; 512 uint32_t nbars = kind == 0 ? 6u : (kind == 1 ? 2u : 0u); 513 for (uint32_t i = 0; i < nbars; i++) f->bar_raw[i] = rd32(c, CFG_BAR0 + 4u * i); 514 if (kind == 0) { 515 uint32_t ss = rd32(c, CFG_SUBSYS); 516 f->subsys_vendor = (uint16_t)ss; 517 f->subsys_id = (uint16_t)(ss >> 16); 518 } else if (kind == 1) { 519 uint32_t bn = rd32(c, 0x18); 520 f->secondary = (uint8_t)(bn >> 8); 521 f->subordinate = (uint8_t)(bn >> 16); 522 uint8_t sec = f->secondary; 523 if (sec > bus && sec <= d->a->end_bus && f->subordinate >= sec && depth < DISC_MAX_DEPTH && 524 !(d->seen[sec >> 5] & (1u << (sec & 31u)))) { 525 d->bridges++; 526 int rc = disc_bus(d, sec, depth + 1u); 527 if (rc != PCI_OK) return rc; 528 } 529 } 530 if (fn == 0 && !(ht & 0x80u)) break; /* single-function device */ 531 } 532 } 533 return PCI_OK; 534 } 535 536 int pci_discover(const pci_bus_access *a, uint16_t segment, pci_found *out, uint32_t cap, uint32_t *n_out, 537 uint32_t *bridges_followed) 538 { 539 if (!a || !out || !n_out || !a->ecam || a->start_bus > a->end_bus) return PCI_E_ARG; 540 disc_ctx d; 541 d.a = a; 542 d.segment = segment; 543 d.out = out; 544 d.cap = cap; 545 d.n = 0; 546 d.bridges = 0; 547 for (int i = 0; i < 8; i++) d.seen[i] = 0; 548 int rc = disc_bus(&d, a->start_bus, 0); 549 *n_out = d.n; 550 if (bridges_followed) *bridges_followed = d.bridges; 551 return rc; 552 } 553 554 /* ---- Boot discovery report: every MCFG segment, read only. ---- */ 555 556 #define PCI_MAX_SEGMENTS 32u 557 558 static pci_ecam g_disc_ecam[PCI_MAX_SEGMENTS]; 559 static pci_found g_disc_found[PCI_MAX_FUNCS]; 560 /* Every function the last report found, across segments (capped), for pci_stage_disc_found. */ 561 static pci_found g_disc_all[PCI_MAX_FUNCS]; 562 static uint32_t g_disc_all_n; 563 564 /* ECAM accessors the last report mapped, one per MCFG allocation. */ 565 static pci_bus_access g_disc_acc[PCI_MAX_SEGMENTS]; 566 static uint16_t g_disc_acc_seg[PCI_MAX_SEGMENTS]; 567 static uint32_t g_disc_acc_n; 568 569 int pci_stage_disc_access(uint16_t segment, uint8_t bus, pci_bus_access *out) 570 { 571 if (!out) return PCI_E_ARG; 572 for (uint32_t i = 0; i < g_disc_acc_n; i++) 573 if (g_disc_acc_seg[i] == segment && bus >= g_disc_acc[i].start_bus && bus <= g_disc_acc[i].end_bus) { 574 *out = g_disc_acc[i]; 575 return PCI_OK; 576 } 577 return PCI_E_ARG; 578 } 579 580 /* Report-only: prints what firmware declared and what answers, changes no 581 * state. Each segment maps only its declared bus range through the 582 * fail-closed ck_mmio_try_map (a refused map is reported, never retried 583 * another way). pci_discover reads config space and nothing else: no BAR 584 * sizing, no config write. Segment 0 was already mapped by pci_stage_probe; 585 * mapping the same pages again is a no-op. The caller ignores the return 586 * value (it is for the log and tests); binding, sweep and assignment do not 587 * depend on it. */ 588 int pci_stage_discover_report(void) 589 { 590 const uint8_t *mcfg = (const uint8_t *)ck_acpi_find("MCFG"); 591 if (!mcfg) { 592 ck_printf("pci_disc: no ACPI MCFG table\n"); 593 return PCI_E_NO_MCFG; 594 } 595 uint32_t len = le32(mcfg + 4); 596 uint32_t nseg = 0; 597 int rc = pci_mcfg_parse_all(mcfg, len, g_disc_ecam, PCI_MAX_SEGMENTS, &nseg); 598 if (rc) { 599 ck_printf("pci_disc: MCFG malformed or too large (len=%u rc=%d)\n", len, rc); 600 return rc; 601 } 602 uint32_t scanned = 0, total = 0; 603 g_disc_all_n = 0; 604 g_disc_acc_n = 0; 605 for (uint32_t i = 0; i < nseg; i++) { 606 const pci_ecam *e = &g_disc_ecam[i]; 607 uint64_t first = e->base + ((uint64_t)e->start_bus << 20); 608 uint64_t bytes = (uint64_t)((uint32_t)e->end_bus - e->start_bus + 1u) << 20; 609 volatile uint8_t *m = (volatile uint8_t *)ck_mmio_try_map(first, (size_t)bytes); 610 if (!m) { 611 ck_printf("pci_disc: seg %04x bus %02x-%02x REFUSED (ecam map refused, base=0x%llx)\n", e->segment, 612 e->start_bus, e->end_bus, (unsigned long long)e->base); 613 continue; 614 } 615 pci_bus_access a; 616 a.ecam = m; 617 a.start_bus = e->start_bus; 618 a.end_bus = e->end_bus; 619 g_disc_acc[g_disc_acc_n] = a; 620 g_disc_acc_seg[g_disc_acc_n++] = e->segment; 621 uint32_t n = 0, br = 0; 622 int drc = pci_discover(&a, e->segment, g_disc_found, PCI_MAX_FUNCS, &n, &br); 623 ck_printf("pci_disc: seg %04x bus %02x-%02x functions=%u bridges=%u rc=%d\n", e->segment, e->start_bus, 624 e->end_bus, n, br, drc); 625 for (uint32_t j = 0; j < n; j++) { 626 const pci_found *f = &g_disc_found[j]; 627 if (g_disc_all_n < PCI_MAX_FUNCS) 628 g_disc_all[g_disc_all_n++] = *f; 629 ck_printf("pci_disc: %04x:%02x:%02x.%u %04x:%04x class=%06x rev=%02x\n", f->segment, f->bus, f->dev, 630 f->fn, f->vendor, f->device, f->class_code, f->revision); 631 } 632 scanned++; 633 total += n; 634 } 635 ck_printf("pci_disc: segments=%u functions=%u (read-only, no config writes)\n", scanned, total); 636 return PCI_OK; 637 } 638 639 const pci_found *pci_stage_disc_found(uint32_t *n) 640 { 641 *n = g_disc_all_n; 642 return g_disc_all; 643 } 644 645 static int found_before(const pci_found *a, const pci_found *b) 646 { 647 if (a->segment != b->segment) return a->segment < b->segment; 648 if (a->bus != b->bus) return a->bus < b->bus; 649 if (a->dev != b->dev) return a->dev < b->dev; 650 return a->fn < b->fn; 651 } 652 653 const pci_found *pci_disc_find_class(const pci_found *f, uint32_t n, uint32_t class_code, uint32_t mask, 654 uint32_t *count) 655 { 656 const pci_found *best = 0; 657 uint32_t c = 0; 658 for (uint32_t i = 0; f && i < n; i++) { 659 if ((f[i].class_code & mask) != (class_code & mask)) continue; 660 c++; 661 if (!best || found_before(&f[i], best)) best = &f[i]; 662 } 663 if (count) *count = c; 664 return best; 665 } 666 667 /* BAR0 as firmware left it (type bits stripped), 64-bit when the type says so. */ 668 static uint64_t found_bar0(const pci_found *f) 669 { 670 uint32_t raw = f->bar_raw[0]; 671 if (raw & 1u) return 0; /* I/O BAR: not a memory address */ 672 uint64_t a = raw & ~0xfu; 673 if (((raw >> 1) & 3u) == 2u) a |= (uint64_t)f->bar_raw[1] << 32; 674 return a; 675 } 676 677 int pci_nvme_disc_report(const pci_found *f, uint32_t n, const pci_func *bound) 678 { 679 uint32_t cnt = 0; 680 const pci_found *c = pci_disc_find_class(f, n, 0x010802u, 0xffffffu, &cnt); 681 if (!c) { 682 ck_printf("nvme_disc: no class 0x010802 function found by discovery (bound: %s)\n", 683 bound ? "different" : "none"); 684 return 0; 685 } 686 const char *rel = !bound ? "none" 687 : (bound->segment == c->segment && bound->bus == c->bus && bound->dev == c->dev && bound->fn == c->fn) 688 ? "same" : "different"; 689 ck_printf("nvme_disc: candidate seg %04x %02x:%02x.%u bar0=0x%llx decode=%u (bound: %s)\n", 690 c->segment, c->bus, c->dev, c->fn, (unsigned long long)found_bar0(c), (c->command >> 1) & 1u, rel); 691 ck_printf("nvme_disc: candidates=%u\n", cnt); 692 return (int)cnt; 693 }