aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / xhci_fence.c
| 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 | /* xhci_fence.c -- xHCI DMA fence (see xhci_fence.h). Freestanding. */ |
| 18 | #include "xhci_fence.h" |
| 19 | #include "ck.h" |
| 20 | #include "usb_kbd.h" |
| 21 | #include "xhci_acpi_fmt.h" |
| 22 | |
| 23 | #if defined(CK_TEST_XHCI_MUTATION) && defined(CK_HARDWARE_STAGING) |
| 24 | #error "CK_TEST_XHCI_MUTATION (TEST-ONLY xHCI DMA fence mutation) cannot be combined with CK_HARDWARE_STAGING" |
| 25 | #endif |
| 26 | #if defined(CK_TEST_XHCI_MUTATION) && defined(CK_QEMU_UNSAFE_DMA) |
| 27 | #error "CK_TEST_XHCI_MUTATION cannot be combined with CK_QEMU_UNSAFE_DMA" |
| 28 | #endif |
| 29 | #if defined(CK_TEST_XHCI_MUTATION) |
| 30 | #if CK_TEST_XHCI_MUTATION < 1 || CK_TEST_XHCI_MUTATION > 4 |
| 31 | #error "CK_TEST_XHCI_MUTATION must be 1..4 (bm-left-on, no-revoke, grant-no-smmu, no-sweep)" |
| 32 | #endif |
| 33 | #define CK_XHCI_MUT CK_TEST_XHCI_MUTATION |
| 34 | #else |
| 35 | #define CK_XHCI_MUT 0 |
| 36 | #endif |
| 37 | #if defined(CK_CONSOLE_SESSION) && CK_CONSOLE_SESSION |
| 38 | #if defined(CK_HARDWARE_STAGING) |
| 39 | #error "CK_CONSOLE_SESSION (QEMU test console session) cannot be combined with CK_HARDWARE_STAGING" |
| 40 | #endif |
| 41 | #if defined(CK_QEMU_UNSAFE_DMA) |
| 42 | #error "CK_CONSOLE_SESSION cannot be combined with CK_QEMU_UNSAFE_DMA" |
| 43 | #endif |
| 44 | static int g_session; /* 1 while ck_xhci_session drives the fence */ |
| 45 | #define KBD_RUN(b, m, p, n) (g_session ? ck_kbd_session_phase(b, m, p, n) : ck_kbd_phase(b, m, p, n)) |
| 46 | #else |
| 47 | #if defined(CK_TEST_CONSOLE_MUTATION) |
| 48 | #error "CK_TEST_CONSOLE_MUTATION needs CK_CONSOLE_SESSION=1" |
| 49 | #endif |
| 50 | #define KBD_RUN(b, m, p, n) ck_kbd_phase(b, m, p, n) |
| 51 | #endif |
| 52 | |
| 53 | #define CMD_MEM 0x2u |
| 54 | #define CMD_BM 0x4u |
| 55 | /* xHCI capability / operational registers (xHCI 1.2 sections 5.3, 5.4). */ |
| 56 | #define XHCI_CAPLENGTH 0x00u /* bits 7:0 CAPLENGTH, 31:16 HCIVERSION */ |
| 57 | #define XHCI_USBCMD 0x00u /* operational */ |
| 58 | #define XHCI_USBSTS 0x04u |
| 59 | #define USBCMD_RS 0x1u |
| 60 | #define USBCMD_HCRST 0x2u |
| 61 | #define USBSTS_HCH 0x1u |
| 62 | #define USBSTS_CNR 0x800u |
| 63 | |
| 64 | typedef struct { |
| 65 | const pci_func *pf; |
| 66 | int plat; /* ACPI platform controller (no PCI function, no bus-master bit) */ |
| 67 | char name[48]; /* its ACPI name (platform only) */ |
| 68 | volatile uint8_t *bar0; |
| 69 | uint32_t op; /* operational register offset (CAPLENGTH) */ |
| 70 | int bm_on, confined, mapped; |
| 71 | uint32_t stream_id; |
| 72 | const char *state; |
| 73 | } ck_xhci; |
| 74 | |
| 75 | static ck_xhci g_x; |
| 76 | static uint8_t *g_dma_mem; |
| 77 | static uint64_t g_dma_phys; |
| 78 | |
| 79 | static uint32_t x_r32(uint32_t off) { return *(volatile uint32_t *)(g_x.bar0 + off); } |
| 80 | static void x_w32(uint32_t off, uint32_t v) { *(volatile uint32_t *)(g_x.bar0 + off) = v; } |
| 81 | |
| 82 | int ck_xhci_live(void) { return (g_x.pf || g_x.plat) && (g_x.bm_on || g_x.confined); } |
| 83 | const char *ck_xhci_state(void) { return g_x.state ? g_x.state : "absent"; } |
| 84 | |
| 85 | #if CK_XHCI_MUT |
| 86 | static const char *mut_name(void) |
| 87 | { |
| 88 | switch (CK_XHCI_MUT) { |
| 89 | case CK_XHCI_MUT_BM_LEFT_ON: return "bm-left-on"; |
| 90 | case CK_XHCI_MUT_NO_REVOKE: return "no-revoke"; |
| 91 | case CK_XHCI_MUT_GRANT_NO_SMMU: return "grant-no-smmu"; |
| 92 | default: return "no-sweep"; |
| 93 | } |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | int ck_xhci_sweep_enabled(void) |
| 98 | { |
| 99 | #if CK_XHCI_MUT |
| 100 | ck_printf("WARNING: TEST-ONLY xHCI MUTATION BUILD (CK_TEST_XHCI_MUTATION=%s, QEMU gate self-test only, " |
| 101 | "never a PASS)\n", mut_name()); |
| 102 | #endif |
| 103 | return CK_XHCI_MUT != CK_XHCI_MUT_NO_SWEEP; |
| 104 | } |
| 105 | |
| 106 | void ck_xhci_after_sweep(const pci_system *pci) |
| 107 | { |
| 108 | #if CK_XHCI_MUT == CK_XHCI_MUT_BM_LEFT_ON |
| 109 | const pci_func *f = pci_find_class(pci, CK_XHCI_CLASS, 0xffffffu); |
| 110 | if (f) pci_w16(f->cfg, 0x04, (uint16_t)(pci_r16(f->cfg, 0x04) | CMD_BM)); |
| 111 | #else |
| 112 | (void)pci; |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | /* Make sure the controller is halted (R/S = 0, wait for HCH) while it may |
| 117 | * still DMA. Returns 0 when USBSTS.HCH reads 1. */ |
| 118 | static int halt(const char *when) |
| 119 | { |
| 120 | if (!g_x.mapped) return 0; |
| 121 | uint32_t cmd = x_r32(g_x.op + XHCI_USBCMD); |
| 122 | if (cmd & USBCMD_RS) x_w32(g_x.op + XHCI_USBCMD, cmd & ~USBCMD_RS); |
| 123 | uint64_t t0 = ck_time_us(); |
| 124 | uint32_t sts; |
| 125 | while (!((sts = x_r32(g_x.op + XHCI_USBSTS)) & USBSTS_HCH) && ck_time_us() - t0 < CK_XHCI_HALT_WAIT_US) |
| 126 | ck_udelay(10); |
| 127 | int ok = (sts & USBSTS_HCH) != 0; |
| 128 | ck_printf("xhci: %s usbcmd=0x%08x usbsts=0x%08x halted=%s\n", when, cmd, sts, ok ? "yes" : "NO (TIMEOUT)"); |
| 129 | return ok ? 0 : -1; |
| 130 | } |
| 131 | |
| 132 | void ck_xhci_release(void) |
| 133 | { |
| 134 | ck_xhci *x = &g_x; |
| 135 | if (!x->pf && !x->plat) return; |
| 136 | if (x->plat && x->bm_on) { |
| 137 | /* No bus-master bit on a platform controller: halt it, reset it (drops |
| 138 | * every ring pointer it holds), then the stream goes back to abort |
| 139 | * below, which is the gate that actually stops its DMA. */ |
| 140 | int h = halt("halt before revoke"); |
| 141 | int reset_ok = 0; |
| 142 | if (h == 0) { |
| 143 | x_w32(x->op + XHCI_USBCMD, USBCMD_HCRST); |
| 144 | uint64_t t0 = ck_time_us(); |
| 145 | while (((x_r32(x->op + XHCI_USBCMD) & USBCMD_HCRST) || (x_r32(x->op + XHCI_USBSTS) & USBSTS_CNR)) && |
| 146 | ck_time_us() - t0 < 100000u) |
| 147 | ck_udelay(20); |
| 148 | reset_ok = !(x_r32(x->op + XHCI_USBCMD) & USBCMD_HCRST); |
| 149 | } |
| 150 | x->bm_on = 0; |
| 151 | ck_printf("dma_gate: xhci %s quiesced (halted=%s reset=%s)\n", x->name, h == 0 ? "yes" : "NO", |
| 152 | reset_ok ? "yes" : "NO"); |
| 153 | } |
| 154 | if (x->pf && x->bm_on) { |
| 155 | (void)halt("halt before revoke"); |
| 156 | #if CK_XHCI_MUT == CK_XHCI_MUT_NO_REVOKE |
| 157 | /* TEST-ONLY mutation: bus mastering is left on. */ |
| 158 | int off = (pci_r16(x->pf->cfg, 0x04) & CMD_BM) ? -1 : 0; |
| 159 | #elif defined(CK_TEST_CONSOLE_MUTATION) |
| 160 | /* TEST-ONLY console-session mutation: after the session's exit the bus |
| 161 | * master is left on (the devices-stage fence still revokes normally). */ |
| 162 | int off = g_session ? ((pci_r16(x->pf->cfg, 0x04) & CMD_BM) ? -1 : 0) : pci_bus_master_off(x->pf); |
| 163 | #else |
| 164 | int off = pci_bus_master_off(x->pf); |
| 165 | #endif |
| 166 | if (off == 0) { |
| 167 | x->bm_on = 0; |
| 168 | ck_printf("dma_gate: xhci bus master revoked\n"); |
| 169 | } else /* bm_on stays set: the reset quiesce hook retries the revoke */ |
| 170 | ck_printf("dma_gate: xhci bus master revoke FAILED (command register still has BME)\n"); |
| 171 | uint16_t c = pci_r16(x->pf->cfg, 0x04); |
| 172 | ck_printf("xhci_pci: after phase command=0x%04x bus_master=%s\n", c, (c & CMD_BM) ? "on" : "off"); |
| 173 | } |
| 174 | if (x->confined) { |
| 175 | /* A failed abort leaves the stream live in the SMMU service: it stays |
| 176 | * confined (ck_xhci_live stays 1) and a later release retries. */ |
| 177 | int urc = ck_dma_unconfine(x->stream_id); |
| 178 | if (urc == 0) x->confined = 0; |
| 179 | ck_printf("smmu: xhci stream 0x%x %s (rc=%d%s%s)\n", x->stream_id, |
| 180 | urc == 0 ? "returned to abort" : "abort FAILED, stays confined", urc, urc ? ": " : "", |
| 181 | urc ? ck_dma_unconfine_why(urc) : ""); |
| 182 | if (urc != 0) ck_dma_fault_report("xhci", x->stream_id); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /* ---- ACPI platform controllers (NEXT-PHASE-3 cut 2) ---- |
| 188 | * Discovery runs once per boot, read-only, before any controller is |
| 189 | * touched; the lines it prints are the ones tools/ck_acpi_scan.c predicts |
| 190 | * from firmware tables. */ |
| 191 | static struct ck_platform_dev g_plat[CK_XHCI_PLAT_MAX]; |
| 192 | static int g_nplat = -2; /* -2 not scanned yet, -1 no DSDT */ |
| 193 | #ifdef CK_HOST_TEST |
| 194 | int ck_xhci_host_forget(void) |
| 195 | { |
| 196 | if (ck_xhci_live()) return -1; |
| 197 | g_nplat = -2; |
| 198 | g_x.pf = 0; |
| 199 | g_x.plat = 0; |
| 200 | g_x.name[0] = 0; |
| 201 | g_x.bar0 = 0; |
| 202 | g_x.op = 0; |
| 203 | g_x.bm_on = g_x.confined = g_x.mapped = 0; |
| 204 | g_x.stream_id = 0; |
| 205 | g_x.state = "absent"; |
| 206 | return 0; |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | static void acpi_discover(void) |
| 211 | { |
| 212 | if (g_nplat != -2) return; |
| 213 | static const char *const ids[] = CK_XHCI_ACPI_IDS; |
| 214 | static const char *const ctl[] = { CK_ACPI_CONTROL_ID }; |
| 215 | struct ck_acpi_scan_info info; |
| 216 | struct ck_platform_dev c; |
| 217 | int nc = ck_acpi_platform_devices(ctl, 1, &c, 1, &info); |
| 218 | if (nc < 0) { |
| 219 | ck_printf(CK_ACPI_SCAN_NONE); |
| 220 | g_nplat = -1; |
| 221 | return; |
| 222 | } |
| 223 | ck_printf(CK_ACPI_SCAN_FMT, info.tables, info.refused, info.first_refusal, info.devices); |
| 224 | if (nc > 0) |
| 225 | ck_printf(CK_ACPI_CONTROL_FMT, c.hid, c.name, (unsigned long long)c.mmio_base, (unsigned long long)c.mmio_len); |
| 226 | else |
| 227 | ck_printf(CK_ACPI_CONTROL_NONE); |
| 228 | int n = ck_acpi_platform_devices(ids, CK_XHCI_ACPI_NIDS, g_plat, CK_XHCI_PLAT_MAX, &info); |
| 229 | if (n > (int)CK_XHCI_PLAT_MAX) n = (int)CK_XHCI_PLAT_MAX; |
| 230 | g_nplat = n < 0 ? -1 : n; |
| 231 | for (int i = 0; i < g_nplat; i++) |
| 232 | ck_printf(CK_XHCI_ACPI_FMT, g_plat[i].name, g_plat[i].hid[0] ? g_plat[i].hid : "-", |
| 233 | g_plat[i].cid[0] ? g_plat[i].cid : "-", g_plat[i].table, (unsigned long long)g_plat[i].mmio_base, |
| 234 | (unsigned long long)g_plat[i].mmio_len); |
| 235 | ck_printf(CK_XHCI_ACPI_COUNT_FMT, g_nplat < 0 ? 0 : g_nplat); |
| 236 | } |
| 237 | |
| 238 | static int same_last_seg(const char *a, const char *b) |
| 239 | { |
| 240 | const char *la = a, *lb = b; |
| 241 | for (const char *p = a; *p; p++) |
| 242 | if (*p == '.' || *p == '\\' || *p == '^') la = p + 1; |
| 243 | for (const char *p = b; *p; p++) |
| 244 | if (*p == '.' || *p == '\\' || *p == '^') lb = p + 1; |
| 245 | while (*la && *la == *lb) la++, lb++; |
| 246 | return *la == *lb; |
| 247 | } |
| 248 | |
| 249 | /* One platform controller: map, halt whatever the firmware left running, |
| 250 | * SMMU-confined grant by its ACPI name or fail-closed deny, keyboard phase, |
| 251 | * then release (halt, reset, stream back to abort). *kbd_rc gets the phase |
| 252 | * result, or 1 when the phase did not run. */ |
| 253 | static int plat_fence_one(int idx, int *kbd_rc) |
| 254 | { |
| 255 | const struct ck_platform_dev *p = &g_plat[idx]; |
| 256 | ck_xhci *x = &g_x; |
| 257 | *kbd_rc = 1; |
| 258 | if (ck_xhci_live()) { /* never forget a stream an earlier release could not abort */ |
| 259 | ck_printf("keyboard: unavailable (previous xHCI fence still live; release it first)\n"); |
| 260 | return CK_XHCI_E_RELEASE; |
| 261 | } |
| 262 | x->pf = 0; |
| 263 | x->plat = 0; |
| 264 | x->bar0 = 0; |
| 265 | x->op = 0; |
| 266 | x->bm_on = x->confined = x->mapped = 0; |
| 267 | x->stream_id = 0; |
| 268 | unsigned k = 0; |
| 269 | for (; k + 1 < sizeof x->name && p->name[k]; k++) x->name[k] = p->name[k]; |
| 270 | x->name[k] = 0; |
| 271 | ck_printf(CK_XHCI_PLAT_TRY_FMT, x->name, (unsigned long long)p->mmio_base, (unsigned long long)p->mmio_len); |
| 272 | for (int j = 0; j < g_nplat; j++) |
| 273 | if (j != idx && same_last_seg(g_plat[j].name, p->name)) { |
| 274 | ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "name not unique in DSDT/SSDT"); |
| 275 | x->state = "ambiguous"; |
| 276 | return CK_XHCI_E_ARG; |
| 277 | } |
| 278 | if (!p->mmio_base || p->mmio_len < 0x1000u || (p->mmio_base & 0xfffu)) { |
| 279 | ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "no usable _CRS memory range"); |
| 280 | x->state = "bar-unusable"; |
| 281 | return CK_XHCI_E_ARG; |
| 282 | } |
| 283 | volatile uint8_t *bar = (volatile uint8_t *)ck_mmio_try_map(p->mmio_base, (size_t)p->mmio_len); |
| 284 | if (!bar) { |
| 285 | ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "memory range not mappable as MMIO"); |
| 286 | x->state = "bar-unusable"; |
| 287 | return CK_XHCI_E_ARG; |
| 288 | } |
| 289 | x->bar0 = bar; |
| 290 | x->mapped = 1; |
| 291 | uint32_t cap = x_r32(XHCI_CAPLENGTH); |
| 292 | x->op = cap & 0xffu; |
| 293 | ck_printf("xhci: %s caplength=0x%02x hciversion=0x%04x (platform)\n", x->name, cap & 0xffu, cap >> 16); |
| 294 | /* Stop any DMA the firmware left running before the SMMU decision; a |
| 295 | * denied controller stays halted. */ |
| 296 | if (halt("halted before gate")) { |
| 297 | ck_printf("keyboard: unavailable (platform xHCI %s would not halt)\n", x->name); |
| 298 | x->state = "halt-failed"; |
| 299 | return CK_XHCI_E_HALT; |
| 300 | } |
| 301 | if (!g_dma_mem) g_dma_mem = ck_dma_alloc(CK_XHCI_DMA_BYTES, 4096, &g_dma_phys); |
| 302 | if (!g_dma_mem) { |
| 303 | ck_printf("keyboard: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)CK_XHCI_DMA_BYTES); |
| 304 | x->state = "dma-alloc-failed"; |
| 305 | return CK_XHCI_E_DMA; |
| 306 | } |
| 307 | struct ck_dma_confinement cf = { 0, 0, 0, 0 }; |
| 308 | int src = ck_dma_confine_named(x->name, g_dma_phys, CK_XHCI_DMA_BYTES, &cf); |
| 309 | if (src) { |
| 310 | if (src == CK_SMMU_OTHER) |
| 311 | ck_printf(CK_XHCI_PLAT_OTHER_FMT, x->name, (unsigned long long)cf.smmu_base, cf.stream_id); |
| 312 | else |
| 313 | ck_printf(CK_XHCI_PLAT_DENY_FMT, x->name, |
| 314 | src == CK_SMMU_ABSENT ? "NoSmmu" |
| 315 | : src == CK_SMMU_NOSTREAM ? "NoStream" |
| 316 | : src == CK_SMMU_EARG ? "Ambiguous" |
| 317 | : "SmmuNotReady"); |
| 318 | x->state = "denied"; |
| 319 | return CK_XHCI_E_DENIED; |
| 320 | } |
| 321 | x->plat = 1; |
| 322 | x->confined = 1; |
| 323 | x->bm_on = 1; /* may DMA from here until the release */ |
| 324 | x->stream_id = cf.stream_id; |
| 325 | ck_printf(CK_XHCI_PLAT_GRANT_FMT, x->name, (unsigned long long)cf.smmu_base, cf.stream_id); |
| 326 | ck_printf("smmu_dma_window: xhci %s only, translation active iova=0x%llx len=0x%llx\n", x->name, |
| 327 | (unsigned long long)cf.iova, (unsigned long long)cf.len); |
| 328 | *kbd_rc = KBD_RUN(x->bar0, g_dma_mem, g_dma_phys, CK_XHCI_DMA_BYTES); |
| 329 | x->state = "fenced"; |
| 330 | ck_xhci_release(); |
| 331 | return CK_XHCI_OK; |
| 332 | } |
| 333 | |
| 334 | /* Every platform controller in table order until one carries a keyboard. */ |
| 335 | static int plat_fence_all(void) |
| 336 | { |
| 337 | if (g_nplat <= 0) { |
| 338 | ck_printf("keyboard: unavailable (no xHCI controller)\n"); |
| 339 | g_x.state = "absent"; |
| 340 | return CK_XHCI_ABSENT; |
| 341 | } |
| 342 | int rc = CK_XHCI_ABSENT, any_fenced = 0; |
| 343 | for (int i = 0; i < g_nplat; i++) { |
| 344 | int krc; |
| 345 | rc = plat_fence_one(i, &krc); |
| 346 | if (rc == CK_XHCI_E_RELEASE || ck_xhci_live()) { |
| 347 | g_x.state = "release-failed"; |
| 348 | return CK_XHCI_E_RELEASE; |
| 349 | } |
| 350 | if (rc == CK_XHCI_OK) any_fenced = 1; |
| 351 | if (rc == CK_XHCI_OK && krc == 0) return CK_XHCI_OK; /* keyboard found and its phase ran */ |
| 352 | if (ck_kbd_recovery_requested()) return rc; |
| 353 | } |
| 354 | if (any_fenced) g_x.state = "fenced"; |
| 355 | ck_printf("keyboard: unavailable (no keyboard on any platform xHCI)\n"); |
| 356 | return any_fenced ? CK_XHCI_OK : rc; |
| 357 | } |
| 358 | |
| 359 | int ck_xhci_fence(const pci_system *pci) |
| 360 | { |
| 361 | ck_xhci *x = &g_x; |
| 362 | if (ck_xhci_live()) { |
| 363 | ck_printf("keyboard: unavailable (previous xHCI fence still live; release it first)\n"); |
| 364 | return CK_XHCI_E_ARG; |
| 365 | } |
| 366 | acpi_discover(); |
| 367 | x->pf = 0; |
| 368 | x->plat = 0; |
| 369 | x->name[0] = 0; |
| 370 | x->bar0 = 0; |
| 371 | x->op = 0; |
| 372 | x->bm_on = x->confined = x->mapped = 0; |
| 373 | x->stream_id = 0; |
| 374 | x->state = "absent"; |
| 375 | const pci_func *f = pci ? pci_find_class(pci, CK_XHCI_CLASS, 0xffffffu) : 0; |
| 376 | if (!f) { |
| 377 | /* No PCI xHCI (the DGX Spark has none): the ACPI platform |
| 378 | * controllers found by acpi_discover, each behind its own fence. */ |
| 379 | ck_printf("keyboard: no PCI xHCI controller; trying %d ACPI platform controller(s)\n", g_nplat < 0 ? 0 : g_nplat); |
| 380 | return plat_fence_all(); |
| 381 | } |
| 382 | x->pf = f; |
| 383 | const pci_bar *b = &f->bar[0]; |
| 384 | /* Rust: "keyboard: xhci {seg:04x}:{bus:02x}:{dev:02x}.{fn} mmio {:#x}". */ |
| 385 | ck_printf("keyboard: xhci %04x:%02x:%02x.%u mmio 0x%llx (found post-exit on the ECAM walk)\n", |
| 386 | pci->ecam.segment, f->bus, f->dev, f->fn, (unsigned long long)b->addr); |
| 387 | uint16_t command = pci_r16(f->cfg, 0x04); |
| 388 | ck_printf("xhci_pci: command=0x%04x bus_master=%s\n", command, (command & CMD_BM) ? "on" : "off"); |
| 389 | if (command & CMD_BM) { |
| 390 | /* The sweep could not clear it (or it came back): no gate, no DMA. The |
| 391 | * bus-master bit is left as found; the reset quiesce hook does not |
| 392 | * touch an unbound controller. */ |
| 393 | x->state = "bme-stuck"; |
| 394 | ck_printf("keyboard: unavailable (xHCI bus master would not clear)\n"); |
| 395 | return CK_XHCI_E_BME; |
| 396 | } |
| 397 | if (b->io || b->addr == 0 || b->size < 0x1000u) { |
| 398 | x->state = "bar-unusable"; |
| 399 | ck_printf("keyboard: unavailable (xHCI BAR0 unusable)\n"); |
| 400 | return CK_XHCI_E_ARG; |
| 401 | } |
| 402 | /* DMA gate: the window is exactly the region a driver would use. */ |
| 403 | if (!g_dma_mem) g_dma_mem = ck_dma_alloc(CK_XHCI_DMA_BYTES, 4096, &g_dma_phys); |
| 404 | if (!g_dma_mem) { |
| 405 | x->state = "dma-alloc-failed"; |
| 406 | ck_printf("keyboard: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)CK_XHCI_DMA_BYTES); |
| 407 | return CK_XHCI_E_DMA; |
| 408 | } |
| 409 | uint32_t rid = ((uint32_t)f->bus << 8) | ((uint32_t)f->dev << 3) | (uint32_t)f->fn; |
| 410 | struct ck_dma_confinement cf; |
| 411 | int src = ck_dma_confine(f->segment, rid, g_dma_phys, CK_XHCI_DMA_BYTES, &cf); |
| 412 | if (src == 0) { |
| 413 | x->confined = 1; |
| 414 | x->stream_id = cf.stream_id; |
| 415 | ck_printf("smmu: enabled base=0x%llx stream_id=0x%x\n", (unsigned long long)cf.smmu_base, cf.stream_id); |
| 416 | ck_printf("smmu_dma_window: xhci only, translation active iova=0x%llx len=0x%llx rid=0x%x\n", |
| 417 | (unsigned long long)cf.iova, (unsigned long long)cf.len, rid); |
| 418 | ck_printf("dma_gate: xhci granted (Confined), bus master on\n"); |
| 419 | } else { |
| 420 | #if CK_XHCI_MUT == CK_XHCI_MUT_GRANT_NO_SMMU |
| 421 | /* TEST-ONLY mutation: DMA granted without confinement. */ |
| 422 | ck_printf("dma_gate: xhci granted (TestMutationNoSmmu rc=%d), bus master on\n", src); |
| 423 | #else |
| 424 | if (src == CK_SMMU_ABSENT) |
| 425 | ck_printf("dma_gate: xhci denied (NoSmmu), bus master stays off\n"); |
| 426 | else |
| 427 | ck_printf("dma_gate: xhci denied (SmmuNotReady rc=%d), bus master stays off\n", src); |
| 428 | ck_printf("keyboard: unavailable (SMMU DMA isolation not active)\n"); |
| 429 | uint16_t c = pci_r16(f->cfg, 0x04); |
| 430 | ck_printf("xhci_pci: after deny command=0x%04x bus_master=%s\n", c, (c & CMD_BM) ? "on" : "off"); |
| 431 | x->state = "denied"; |
| 432 | return CK_XHCI_E_DENIED; |
| 433 | #endif |
| 434 | } |
| 435 | x->bar0 = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); |
| 436 | pci_enable(f, 1); |
| 437 | x->bm_on = 1; |
| 438 | x->mapped = 1; |
| 439 | uint32_t cap = x_r32(XHCI_CAPLENGTH); |
| 440 | x->op = cap & 0xffu; |
| 441 | ck_printf("xhci: caplength=0x%02x hciversion=0x%04x dma_window=0x%llx+0x%x\n", cap & 0xffu, cap >> 16, |
| 442 | (unsigned long long)g_dma_phys, (unsigned)CK_XHCI_DMA_BYTES); |
| 443 | int hrc = halt("halted after grant"); |
| 444 | /* Rows 29-30 and the recovery-access hook (usb_kbd.c): the polled HID |
| 445 | * boot-keyboard driver runs only here, inside the confined grant, on the |
| 446 | * fence's DMA region; the release below halts and revokes whatever it |
| 447 | * returns. A controller that would not halt is never driven. */ |
| 448 | int krc = hrc == 0 ? KBD_RUN(x->bar0, g_dma_mem, g_dma_phys, CK_XHCI_DMA_BYTES) : 1; |
| 449 | x->state = hrc ? "halt-failed" : "fenced"; |
| 450 | ck_xhci_release(); |
| 451 | if (ck_xhci_live()) { /* the stream is still confined: no platform attempt, no success */ |
| 452 | x->state = "release-failed"; |
| 453 | return CK_XHCI_E_RELEASE; |
| 454 | } |
| 455 | /* A PCI controller without a keyboard: try the platform ones next. */ |
| 456 | if (krc == CK_KBD_E_NOKBD && g_nplat > 0 && !ck_kbd_recovery_requested()) { |
| 457 | ck_printf("keyboard: none on the PCI xHCI; trying %d ACPI platform controller(s)\n", g_nplat); |
| 458 | x->pf = 0; |
| 459 | int prc = plat_fence_all(); |
| 460 | /* A refused abort leaves that platform stream confined. Other |
| 461 | * platform errors (deny, halt, bad BAR, no DMA page) happen before |
| 462 | * a grant, and the PCI controller is already released, so those |
| 463 | * stay the PCI fence's result. */ |
| 464 | if (prc == CK_XHCI_E_RELEASE) return prc; |
| 465 | if (prc == CK_XHCI_OK) return CK_XHCI_OK; |
| 466 | x->state = "fenced"; |
| 467 | } |
| 468 | return hrc ? CK_XHCI_E_HALT : CK_XHCI_OK; |
| 469 | } |
| 470 | |
| 471 | #if defined(CK_CONSOLE_SESSION) && CK_CONSOLE_SESSION |
| 472 | int ck_xhci_session(const pci_system *pci) |
| 473 | { |
| 474 | g_session = 1; |
| 475 | int rc = ck_xhci_fence(pci); |
| 476 | g_session = 0; |
| 477 | return rc; |
| 478 | } |
| 479 | #endif |