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/x86/pci_x86.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 / x86 / pci_x86.c` 503 lines ยท 22932 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_x86.c -- AIENOS-IMAC-KERNEL-1: the PCI walk and the devices it hands 18 * out, for the 2017 iMac and QEMU q35. 19 * 20 * The walk reads the config space of every function in the ECAM window the 21 * ACPI MCFG names, buses start..end, honoring the multi-function bit. It 22 * remembers each function, then drives, in this order: every AHCI controller 23 * (ahci_x86.c), every NVMe controller (native/disk/disk_nvme.c through the 24 * glue below), a look at the Ethernet controller (PCI identity, power state, 25 * PHY link), then the BCM57766 driven for transmit by tg3_x86.c, which waits 26 * up to 8 s for a link. Config writes: memory decode and bus mastering for the 27 * functions the kernel drives, and the BCM57766's own register-access and 28 * PCIe settings (tg3_x86.c). Afterwards, ck_x86_usb offers each xHCI 29 * controller to the keyboard driver (xhci_x86.c), and ck_x86_sound hands 30 * each sound controller to hda_x86.c, which plays the startup chirp. 31 * 32 * Disks are read-only here. The NVMe driver resets and enables the controller 33 * and creates its queues (no media is touched by that); the kernel then reads 34 * through a disk_dev whose write and flush hooks always refuse, and this file 35 * never calls nvme_write or nvme_flush (the build checks). Each disk's GPT is 36 * checked by dev/disk_part.c. Serial numbers and MAC addresses are never 37 * printed. QEMU runs of this path qualify nothing physical. */ 38 #include "../core/ck_internal.h" 39 #include "../mm/frames.h" 40 #include "disk.h" 41 #include "disk_nvme.h" 42 #include "disk_part.h" 43 #include "x86.h" 44 #include "bench_x86.h" 45 46 #define PCI_MAX 64u 47 #define NVME_MAX 2u 48 #define NVME_DMA_PAGES (NVME_FIXED_PAGES + 32u) /* 128 KiB bounce */ 49 50 struct pfn { 51 volatile uint8_t *cfg; 52 uint8_t bus, dev, fn, ht; 53 uint16_t vendor, device; 54 uint32_t cls; 55 }; 56 57 static struct pfn found[PCI_MAX]; 58 static unsigned nfound; 59 static uint64_t g_tsc_hz; 60 static struct ck_x86_storage_result res; 61 62 static uint32_t cr32(volatile uint8_t *cfg, unsigned off) { return *(volatile uint32_t *)(cfg + off); } 63 static uint16_t cr16(volatile uint8_t *cfg, unsigned off) { return *(volatile uint16_t *)(cfg + off); } 64 static void cw16(volatile uint8_t *cfg, unsigned off, uint16_t v) { *(volatile uint16_t *)(cfg + off) = v; } 65 static void cw32(volatile uint8_t *cfg, unsigned off, uint32_t v) { *(volatile uint32_t *)(cfg + off) = v; } 66 67 void ck_x86_disk_report(const char *tag, unsigned ctrl, unsigned index, const disk_dev *dev) 68 { 69 static uint8_t blk[4096]; 70 if (disk_read(dev, 0, 1, blk) != DISK_OK) { 71 ck_printf("%s_read: ctrl=%u disk=%u lba0 read FAILED\n", tag, ctrl, index); 72 return; 73 } 74 ck_printf("%s_read: ctrl=%u disk=%u lba=0 bytes=%u boot_sig=%02x%02x first=%02x%02x%02x%02x\n", tag, ctrl, 75 index, dev->block_size, blk[510], blk[511], blk[0], blk[1], blk[2], blk[3]); 76 ck_part part; 77 ck_gpt_info info; 78 int rc = ck_gpt_find_aienos(dev, &part, &info); 79 if (rc == CK_GPT_OK) { 80 res.gpt_valid++; 81 ck_printf("%s_gpt: ctrl=%u disk=%u gpt=valid used=%u aienos first_lba=%llu last_lba=%llu\n", tag, ctrl, 82 index, info.used, (unsigned long long)part.first_lba, 83 (unsigned long long)(part.first_lba + part.blocks - 1)); 84 } else if (rc == CK_GPT_ENOPART) { 85 res.gpt_valid++; 86 ck_printf("%s_gpt: ctrl=%u disk=%u gpt=valid used=%u last_lba=%llu, no AIENOS partition, nothing " 87 "written\n", tag, ctrl, index, info.used, (unsigned long long)info.last_lba); 88 } else { 89 ck_printf("%s_gpt: ctrl=%u disk=%u refused: %s\n", tag, ctrl, index, ck_gpt_strerror(rc)); 90 } 91 } 92 93 /* ---- NVMe glue ---- */ 94 struct nvme_slot { 95 volatile uint8_t *bar; 96 nvme_ctrl c; 97 disk_dev dev; 98 }; 99 static struct nvme_slot nvmes[NVME_MAX]; 100 101 static uint32_t nv_rd32(void *ctx, uint32_t off) { return *(volatile uint32_t *)((volatile uint8_t *)ctx + off); } 102 static void nv_wr32(void *ctx, uint32_t off, uint32_t v) { *(volatile uint32_t *)((volatile uint8_t *)ctx + off) = v; } 103 static void nv_barrier(void *ctx) 104 { 105 (void)ctx; 106 __asm__ volatile("mfence" ::: "memory"); 107 } 108 static void nv_delay_us(void *ctx, uint32_t us) 109 { 110 (void)ctx; 111 uint64_t t0 = ck_rdtsc(), n = g_tsc_hz / `1000000` * us; 112 while (ck_rdtsc() - t0 < n) 113 ck_pause(); 114 } 115 static int ro_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf) 116 { 117 (void)ctx, (void)lba, (void)count, (void)buf; 118 return DISK_ESTATE; /* read-only */ 119 } 120 static int ro_flush(void *ctx) 121 { 122 (void)ctx; 123 return DISK_ESTATE; 124 } 125 126 static void nvme_start(const struct pfn *f, struct ck_frames *fr) 127 { 128 unsigned idx = res.nvme_ctrls++; 129 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 130 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 131 ck_printf("pci: nvme %02x:%02x.%u id=%04x:%04x bar0=0x%llx\n", f->bus, f->dev, f->fn, f->vendor, f->device, 132 (unsigned long long)bar); 133 if (idx >= NVME_MAX || (lo & 1) || !bar || ck_x86_map_mmio(bar, 0x4000)) { 134 ck_printf("nvme: ctrl=%u skipped (BAR0 not a usable memory BAR, or more than %u controllers)\n", idx, 135 NVME_MAX); 136 return; 137 } 138 uint64_t pa; 139 if (ck_frames_alloc(fr, NVME_DMA_PAGES, CK_PAGE, &pa)) { 140 ck_printf("nvme: ctrl=%u no DMA frames\n", idx); 141 return; 142 } 143 memset((void *)(uintptr_t)pa, 0, NVME_DMA_PAGES * CK_PAGE); 144 cw16(f->cfg, 4, (uint16_t)(cr16(f->cfg, 4) | 0x6)); /* memory decode + bus master */ 145 struct nvme_slot *s = &nvmes[idx]; 146 s->bar = (volatile uint8_t *)(uintptr_t)bar; 147 nvme_ops ops = {(void *)(uintptr_t)bar, nv_rd32, nv_wr32, 0, 0, nv_barrier, nv_delay_us}; 148 nvme_dma dma = {(uint8_t *)(uintptr_t)pa, pa, NVME_DMA_PAGES * CK_PAGE}; 149 nvme_config cfg = {0, 0}; 150 int rc = nvme_init(&s->c, &ops, &dma, &cfg); 151 if (rc != NVME_OK) { 152 ck_printf("nvme: ctrl=%u init refused rc=%d (no data was read or written)\n", idx, rc); 153 return; 154 } 155 if (nvme_disk(&s->c, &s->dev) != DISK_OK) { 156 ck_printf("nvme: ctrl=%u no disk view\n", idx); 157 return; 158 } 159 s->dev.write = ro_write; 160 s->dev.flush = ro_flush; 161 res.nvme_disks++; 162 ck_printf("nvme_disk: ctrl=%u disk=0 version=%u.%u nsid=%u blocks=%llu block=%u mib=%llu access=read-only\n", 163 idx, s->c.vs >> 16, (s->c.vs >> 8) & 0xff, s->c.nsid, (unsigned long long)s->c.block_count, 164 s->c.block_size, (unsigned long long)(s->c.block_count * s->c.block_size >> 20)); 165 ck_x86_disk_report("nvme", idx, 0, &s->dev); 166 } 167 168 /* ---- Broadcom Ethernet: a look, nothing driven ---- 169 * Config reads only, plus, when the chip is awake (D0) with memory decode on, 170 * MII reads of the PHY's status and ID through the MAC's MI_COM register. */ 171 #define TG3_MI_COM 0x44c 172 #define TG3_MI_MODE 0x454 173 #define MI_START_BUSY 0x20000000u 174 #define MI_READ 0x08000000u 175 176 static int mii_read(volatile uint8_t *bar, unsigned reg, uint16_t *out) 177 { 178 volatile uint32_t *com = (volatile uint32_t *)(bar + TG3_MI_COM); 179 *com = MI_READ | MI_START_BUSY | 1u << 21 | (reg & 0x1f) << 16; /* PHY address 1 */ 180 uint64_t t0 = ck_rdtsc(); 181 uint32_t v; 182 while ((v = *com) & MI_START_BUSY) 183 if (ck_rdtsc() - t0 > g_tsc_hz / 100) /* 10 ms */ 184 return -1; 185 *out = (uint16_t)v; 186 return 0; 187 } 188 189 static void net_look(const struct pfn *f) 190 { 191 res.net_found++; 192 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 193 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 194 uint16_t cmd = cr16(f->cfg, 4); 195 unsigned dstate = 9; 196 if (cr16(f->cfg, 6) & 0x10) 197 for (unsigned p = cr32(f->cfg, 0x34) & 0xfc, n = 0; p && n < 48; n++) { 198 uint32_t h = cr32(f->cfg, p); 199 if ((h & 0xff) == 1) { 200 dstate = cr16(f->cfg, p + 4) & 3; 201 break; 202 } 203 p = (h >> 8) & 0xfc; 204 } 205 ck_printf("net: %02x:%02x.%u id=%04x:%04x bar0=0x%llx mem_decode=%u bus_master=%u power=D%u\n", f->bus, f->dev, 206 f->fn, f->vendor, f->device, (unsigned long long)bar, cmd & 2 ? 1u : 0u, cmd & 4 ? 1u : 0u, dstate); 207 if (f->vendor != 0x14e4 || dstate != 0 || !(cmd & 2) || !bar || ck_x86_map_mmio(bar, 0x10000)) { 208 ck_puts("net: PHY not read (not a Broadcom chip, chip asleep, or BAR off)\n"); 209 return; 210 } 211 volatile uint8_t *b = (volatile uint8_t *)(uintptr_t)bar; 212 uint32_t mode = *(volatile uint32_t *)(b + TG3_MI_MODE); 213 if (mode & 0x10) { /* hardware auto-poll owns the MII bus: leave it */ 214 ck_printf("net: PHY not read (MI auto-poll on, mi_mode=0x%x)\n", mode); 215 return; 216 } 217 uint16_t bmsr = 0, id1 = 0, id2 = 0; 218 if (mii_read(b, 1, &bmsr) || mii_read(b, 1, &bmsr) || mii_read(b, 2, &id1) || mii_read(b, 3, &id2)) { 219 ck_puts("net: PHY did not answer on the MII bus\n"); 220 return; 221 } 222 res.net_link = (bmsr & 4) ? 1 : 2; 223 ck_printf("net: phy_id=%04x:%04x bmsr=0x%04x link=%s autoneg_done=%u\n", id1, id2, bmsr, bmsr & 4 ? "up" : "down", 224 bmsr & 0x20 ? 1u : 0u); 225 } 226 227 /* Drives the BCM57766 (tg3_x86.c) and looks at the link once. Nothing at 228 * start-up waits for it (autonegotiation takes seconds); the report, the only 229 * user of the network, polls the link itself during the hold. */ 230 static void net_drive(const struct pfn *f, struct ck_frames *fr) 231 { 232 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 233 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 234 if (f->vendor != 0x14e4 || f->device != 0x1686 || !bar || ck_x86_map_mmio(bar, 0x10000)) 235 return; 236 if (ck_x86_tg3_start((volatile uint8_t *)(uintptr_t)bar, f->cfg, fr, g_tsc_hz)) { 237 res.net_driver = 2; 238 return; 239 } 240 res.net_driver = 1; 241 res.net_link = ck_x86_tg3_link() ? 1 : 3; 242 if (res.net_link != 1) 243 ck_puts("net_tg3: link not up yet (autonegotiation under way, or no cable); the report goes out once it is\n"); 244 } 245 246 /* Brings a function to D0 through its power management capability, if it has 247 * one and is in D3hot. */ 248 static void to_d0(const struct pfn *f, uint64_t tsc_hz) 249 { 250 if (!(cr16(f->cfg, 6) & 0x10)) 251 return; 252 for (unsigned p = cr32(f->cfg, 0x34) & 0xfc, k = 0; p && k < 48; k++) { 253 uint32_t h = cr32(f->cfg, p); 254 if ((h & 0xff) == 1) { 255 uint16_t pmcsr = cr16(f->cfg, p + 4); 256 if (pmcsr & 3) { 257 cw16(f->cfg, p + 4, (uint16_t)(pmcsr & ~3u)); 258 uint64_t t0 = ck_rdtsc(); 259 while (ck_rdtsc() - t0 < tsc_hz / 100) /* 10 ms, PCI PM 1.2 D3hot -> D0 */ 260 ck_pause(); 261 } 262 return; 263 } 264 p = (h >> 8) & 0xfc; 265 } 266 } 267 268 /* ---- USB: the keyboard ---- 269 * Every xHCI function (class 0x0c0330) in walk order, brought to D0 with 270 * memory decode and bus mastering on, is offered to xhci_x86.c until one has a 271 * boot keyboard. A controller that answers all ones (the Thunderbolt one 272 * powered down with nothing plugged in) is skipped there. */ 273 void ck_x86_usb(struct ck_frames *fr, uint64_t tsc_hz) 274 { 275 unsigned n = 0; 276 for (unsigned i = 0; i < nfound && !ck_x86_kbd_ready(); i++) { 277 const struct pfn *f = &found[i]; 278 if (f->cls != 0x0c0330 || (f->ht & 0x7f) != 0) 279 continue; 280 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 281 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 282 ck_printf("pci: xhci %02x:%02x.%u id=%04x:%04x bar0=0x%llx\n", f->bus, f->dev, f->fn, f->vendor, f->device, 283 (unsigned long long)bar); 284 unsigned ctrl = n++; 285 if ((lo & 1) || !bar || ck_x86_map_mmio(bar, 0x10000)) { 286 ck_printf("xhci: ctrl=%u skipped (BAR0 not a usable memory BAR)\n", ctrl); 287 continue; 288 } 289 to_d0(f, tsc_hz); 290 cw16(f->cfg, 4, (uint16_t)(cr16(f->cfg, 4) | 0x6)); /* memory decode + bus master */ 291 (void)ck_x86_xhci_start(ctrl, (volatile uint8_t *)(uintptr_t)bar, fr, tsc_hz); 292 } 293 if (!n) 294 ck_puts("keyboard: none (no USB controller)\n"); 295 else if (!ck_x86_kbd_ready()) 296 ck_puts("keyboard: none (no USB keyboard found)\n"); 297 } 298 299 /* ---- Sound ---- 300 * Every High Definition Audio function (class 0x0403) in walk order, brought 301 * to D0 with memory decode and bus mastering on, is handed to hda_x86.c; the 302 * first with a speaker path plays the chirp. Intel's controllers first get 303 * Linux's PCI settings (sound/hda/controllers/intel.c, docs-read record): 304 * TCSEL (0x44, bits 0-2) cleared, which ends playback static on some codecs; 305 * on the PCH ones from Sunrise Point on (the Skylake family) snooped DMA 306 * (DEVC 0x78, NOSNOOP bit 11 cleared) and the misc backbone clock gate 307 * (CGCTL 0x48, MISCBDCGE bit 6) off while the controller resets, on after. */ 308 static int intel_skl(const struct pfn *f) 309 { 310 static const uint16_t ids[] = {0xa170, 0x9d70, 0xa171, 0x9d71, 0xa2f0}; 311 for (unsigned k = 0; k < sizeof ids / sizeof ids[0]; k++) 312 if (f->vendor == 0x8086 && f->device == ids[k]) 313 return 1; 314 return 0; 315 } 316 317 void ck_x86_sound(struct ck_frames *fr, uint64_t tsc_hz, uint64_t night_t0) 318 { 319 unsigned n = 0; 320 int playing = 0; 321 for (unsigned i = 0; i < nfound; i++) { 322 const struct pfn *f = &found[i]; 323 if ((f->cls >> 8) != 0x0403 || (f->ht & 0x7f) != 0) 324 continue; 325 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 326 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 327 ck_printf("pci: sound %02x:%02x.%u id=%04x:%04x class=%06x bar0=0x%llx\n", f->bus, f->dev, f->fn, f->vendor, 328 f->device, f->cls, (unsigned long long)bar); 329 unsigned ctrl = n++; 330 if ((lo & 1) || !bar || ck_x86_map_mmio(bar, 0x4000)) { 331 ck_printf("sound: ctrl=%u skipped (BAR0 not a usable memory BAR)\n", ctrl); 332 continue; 333 } 334 to_d0(f, tsc_hz); 335 cw16(f->cfg, 4, (uint16_t)(cr16(f->cfg, 4) | 0x6)); /* memory decode, bus master */ 336 int skl = intel_skl(f); 337 if (f->vendor == 0x8086) 338 cw32(f->cfg, 0x44, cr32(f->cfg, 0x44) & ~7u); 339 if (skl) { 340 cw16(f->cfg, 0x78, (uint16_t)(cr16(f->cfg, 0x78) & ~0x800u)); 341 cw32(f->cfg, 0x48, cr32(f->cfg, 0x48) & ~0x40u); 342 } 343 int r = ck_x86_hda_start(ctrl, (volatile uint8_t *)(uintptr_t)bar, fr, tsc_hz, night_t0, !playing); 344 if (skl) 345 cw32(f->cfg, 0x48, cr32(f->cfg, 0x48) | 0x40u); 346 playing |= r > 0; 347 } 348 if (!n) 349 ck_puts("sound: none (no High Definition Audio controller)\n"); 350 } 351 352 /* ---- Display ---- 353 * The first display controller (class 0x03) whose VRAM aperture (BAR0) holds 354 * the framebuffer goes to panel_x86.c, which decides whether it knows the 355 * chip well enough to switch the panel; the others are only listed. */ 356 void ck_x86_display(uint64_t fb_pa, uint64_t fb_bytes, struct ck_frames *fr, uint64_t tsc_hz) 357 { 358 (void)fr; /* used only by the GPU driver (CK_X86_GPU=1) */ 359 const struct pfn *own = 0; 360 for (unsigned i = 0; i < nfound; i++) { 361 const struct pfn *f = &found[i]; 362 if ((f->cls >> 16) != 0x03 || (f->ht & 0x7f) != 0) 363 continue; 364 uint32_t lo = cr32(f->cfg, 0x10), hi = (lo & 6) == 4 ? cr32(f->cfg, 0x14) : 0; 365 uint64_t bar = ((uint64_t)hi << 32 | lo) & ~0xfull; 366 if (!own && !(lo & 1) && bar && fb_pa >= bar && fb_pa + fb_bytes <= bar + (256ull << 20)) 367 own = f; 368 else 369 ck_printf("pci: display %02x:%02x.%u id=%04x:%04x class=%06x bar0=0x%llx (not the framebuffer's chip)\n", 370 f->bus, f->dev, f->fn, f->vendor, f->device, f->cls, (unsigned long long)bar); 371 } 372 if (own) { 373 ck_x86_panel_start(own->cfg, own->bus, own->dev, own->fn, own->vendor, own->device, own->cls, fb_pa, fb_bytes, tsc_hz); 374 /* GPU bench console: find BAR5 (register aperture) and pass BAR0 (VRAM). */ 375 uint32_t b5lo = cr32(own->cfg, 0x24), b5hi = (b5lo & 6) == 4 ? cr32(own->cfg, 0x28) : 0; 376 uint64_t bar5v = (((uint64_t)b5hi << 32 | b5lo) & ~0xfull); 377 uint32_t b0lo = cr32(own->cfg, 0x10), b0hi = (b0lo & 6) == 4 ? cr32(own->cfg, 0x14) : 0; 378 uint64_t bar0v = (((uint64_t)b0hi << 32 | b0lo) & ~0xfull); 379 /* No register aperture (QEMU's display has no BAR5): the bench says it is off, and the 380 * QEMU test build (CK_X86_BENCH_TEST) uses its RAM stand-ins. */ 381 if (bar5v && !(b5lo & 1) && ck_x86_map_mmio(bar5v, 0x40000) == 0 && bar0v && !(b0lo & 1)) 382 ck_x86_bench_init(own->cfg, (volatile uint32_t *)(uintptr_t)bar5v, 383 (volatile uint32_t *)(uintptr_t)bar0v); 384 else 385 ck_x86_bench_init(0, 0, 0); 386 } else { 387 ck_x86_panel_start(0, 0, 0, 0, 0, 0, 0, fb_pa, fb_bytes, tsc_hz); 388 ck_x86_bench_init(0, 0, 0); 389 } 390 #if CK_X86_GPU 391 /* GPU driver entry: called only when a Polaris display chip was found. 392 * ck_x86_panel_start has already mapped BAR5; pass both BARs and the 393 * framebuffer geometry obtained from the GOP. */ 394 if (own) { 395 uint32_t lo0 = cr32(own->cfg, 0x10), hi0 = (lo0 & 6) == 4 ? cr32(own->cfg, 0x14) : 0; 396 uint64_t bar0 = ((uint64_t)hi0 << 32 | lo0) & ~0xfull; 397 uint32_t lo5 = cr32(own->cfg, 0x24); 398 uint64_t bar5 = lo5 & ~0xfu; 399 /* Recover fb_w, fb_h, fb_pitch from the console framebuffer. */ 400 volatile uint32_t *px; uint32_t fw, fh, fp, fmt; 401 if (ck_x86_console_fb_get(&px, &fw, &fh, &fp, &fmt) != 0) { 402 fw = 3840; fh = 2160; fp = 3840; /* defaults for 2017 iMac 5K GOP */ 403 } 404 ck_x86_gpu_start(own->cfg, own->bus, own->dev, own->fn, own->vendor, own->device, 405 bar0, bar5, fb_pa, fw, fh, fp, fr, g_tsc_hz); 406 } 407 #endif /* CK_X86_GPU */ 408 } 409 410 const struct ck_x86_storage_result *ck_x86_storage(uint64_t rsdp, struct ck_frames *fr, uint64_t tsc_hz, 411 uint64_t map_top) 412 { 413 g_tsc_hz = tsc_hz; 414 (void)map_top; /* device registers above it are mapped on demand (ck_x86_map_mmio) */ 415 ck_x86_dmar_inventory(rsdp); 416 const uint8_t *m = rsdp ? ck_acpi_lookup(rsdp, "MCFG") : 0; 417 if (!m) { 418 ck_puts("pci: no ACPI MCFG, no devices\n"); 419 return &res; 420 } 421 uint32_t len; 422 memcpy(&len, m + 4, 4); 423 for (uint32_t off = 44; off + 16 <= len; off += 16) { 424 uint64_t base; 425 uint16_t seg; 426 memcpy(&base, m + off, 8); 427 memcpy(&seg, m + off + 8, 2); 428 uint8_t sb = m[off + 10], eb = m[off + 11]; 429 if (!base || sb > eb || ck_x86_map_mmio(base + ((uint64_t)sb << 20), (uint64_t)(eb - sb + 1) << 20)) { 430 ck_printf("pci: ecam seg=%u at 0x%llx not mappable, skipped\n", seg, 431 (unsigned long long)base); 432 continue; 433 } 434 for (unsigned bus = sb; bus <= eb; bus++) 435 for (unsigned dev = 0; dev < 32; dev++) 436 for (unsigned fn = 0; fn < 8; fn++) { 437 volatile uint8_t *cfg = (volatile uint8_t *)(uintptr_t)(base + ((uint64_t)bus << 20) + 438 ((uint64_t)dev << 15) + 439 ((uint64_t)fn << 12)); 440 uint32_t idr = cr32(cfg, 0); 441 if ((idr & 0xffff) == 0xffff || (idr & 0xffff) == 0) { 442 if (fn == 0) 443 break; 444 continue; 445 } 446 res.pci_functions++; 447 uint8_t ht = (uint8_t)(cr32(cfg, 0x0c) >> 16); 448 ck_printf("pci: fn %02x:%02x.%u id=%04x:%04x class=%06x segment=%u requester=%04x\n", 449 bus, dev, fn, idr & 0xffffu, idr >> 16, cr32(cfg, 8) >> 8, seg, 450 bus << 8 | dev << 3 | fn); /* every function, for the driver inventory */ 451 if (nfound < PCI_MAX) 452 found[nfound++] = (struct pfn){cfg, (uint8_t)bus, (uint8_t)dev, (uint8_t)fn, ht, 453 (uint16_t)idr, (uint16_t)(idr >> 16), cr32(cfg, 8) >> 8}; 454 if (fn == 0 && !(ht & 0x80)) 455 break; 456 } 457 } 458 459 for (unsigned i = 0; i < nfound; i++) { 460 const struct pfn *f = &found[i]; 461 if (f->cls != 0x010601 || (f->ht & 0x7f) != 0) 462 continue; 463 uint32_t bar = cr32(f->cfg, 0x24); 464 uint64_t abar = bar & ~0xfu; 465 ck_printf("pci: ahci %02x:%02x.%u id=%04x:%04x bar5=0x%x\n", f->bus, f->dev, f->fn, f->vendor, f->device, bar); 466 if ((bar & 1) || (bar & 6) || !abar || ck_x86_map_mmio(abar, 0x1100)) { 467 ck_puts("ahci: BAR5 is not a usable 32-bit memory BAR, controller skipped\n"); 468 } else { 469 cw16(f->cfg, 4, (uint16_t)(cr16(f->cfg, 4) | 0x6)); /* memory decode + bus master */ 470 ck_x86_ahci_start(res.ahci_ctrls, abar, fr, tsc_hz); 471 } 472 res.ahci_ctrls++; 473 } 474 res.ahci_disks = ck_x86_ahci_disks(); 475 ck_x86_step("disk"); 476 for (unsigned i = 0; i < nfound; i++) 477 if (found[i].cls == 0x010802 && (found[i].ht & 0x7f) == 0) 478 nvme_start(&found[i], fr); 479 ck_x86_step("nvme"); 480 for (unsigned i = 0; i < nfound; i++) 481 if ((found[i].cls >> 8) == 0x0200 && (found[i].ht & 0x7f) == 0) 482 net_look(&found[i]); 483 for (unsigned i = 0; i < nfound; i++) 484 if ((found[i].cls >> 8) == 0x0200 && (found[i].ht & 0x7f) == 0 && !res.net_driver) 485 net_drive(&found[i], fr); 486 ck_x86_step("network"); 487 if (!res.net_found) 488 ck_puts("net: no Ethernet controller\n"); 489 ck_printf("storage: pci_functions=%u ahci_controllers=%u disks=%u nvme_controllers=%u nvme_disks=%u " 490 "gpt_valid=%u writes=none\n", res.pci_functions, res.ahci_ctrls, res.ahci_disks, res.nvme_ctrls, 491 res.nvme_disks, res.gpt_valid); 492 return &res; 493 } 494 495 int ck_x86_pci_fn(unsigned i, uint16_t *vendor, uint16_t *device, uint32_t *cls) 496 { 497 if (i >= nfound) 498 return -1; 499 *vendor = found[i].vendor; 500 *device = found[i].device; 501 *cls = found[i].cls; 502 return 0; 503 }