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 / core / smmu_svc.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 | /* smmu_svc.c -- the ck.h DMA isolation services (ck_dma_confine and |
| 18 | * friends) over core/smmu.c, the IORT parser and the DMA pool. Kernel only |
| 19 | * (the host tests drive core/smmu.c directly against a model SMMU). |
| 20 | * |
| 21 | * One instance block per SMMUv3 node the IORT lists (cut B7a, aienos#286): |
| 22 | * a requester is routed to the node its root complex or named component |
| 23 | * maps it to (ck_iort_route / ck_iort_named), and that node is brought up |
| 24 | * the first time a stream behind it is confined. The DGX Spark lists three |
| 25 | * (docs/GB10_IORT_DECODE.md): A at 0x13800000 (PCI segments 0..14, the |
| 26 | * USB controllers), B at 0x13000000 (segment 15: the GB10, stream 0x100, |
| 27 | * and HDA0), C for the small platform peripherals. QEMU virt lists one. |
| 28 | * |
| 29 | * Firmware-reserved ranges (IORT RMR nodes): a stream the RMR names gets |
| 30 | * its ranges identity-mapped in the same stage-1 table as its DMA window, |
| 31 | * so the device keeps reaching what the firmware set up for it while |
| 32 | * everything else still faults and aborts. Only Normal memory attributes |
| 33 | * (Non-cacheable, or Inner/Outer Write-back, both mapped Normal |
| 34 | * Non-cacheable like the window) are honoured; any other attribute refuses |
| 35 | * the confinement (fail closed). A stream the RMR names but nobody confines |
| 36 | * stays aborted like every other stream. |
| 37 | * |
| 38 | * Memory: per instance a stream table (two-level when the SMMU supports it: |
| 39 | * an L1 table of at most 128 KiB plus one 4 KiB L2 page per granted span; |
| 40 | * else linear, 4096 STEs, 256 KiB), command and event queues, the context |
| 41 | * descriptors and every stage-1 table come from ck_dma_alloc, which is |
| 42 | * Normal Non-cacheable, so a dsb orders them before register writes. */ |
| 43 | #include "ck_internal.h" |
| 44 | #include "pt.h" |
| 45 | #include "smmu.h" |
| 46 | |
| 47 | #define STE_N 4096u /* stream ids 0..4095: PCI buses 0..15 (QEMU virt uses bus 0) */ |
| 48 | #define QUEUE_N 16u |
| 49 | /* Window leaf: Normal Non-cacheable (same as the CPU's DMA pool mapping), |
| 50 | * read/write, AP[1] = 1 so unprivileged device transactions are allowed |
| 51 | * (PCIe requests are usually unprivileged), never executable. */ |
| 52 | #define WINDOW_ATTRS (CK_PT_NORMAL_NC | (1ull << 6)) |
| 53 | |
| 54 | struct inst { |
| 55 | int state; /* 0 not tried, 1 up, CK_SMMU_FAILED */ |
| 56 | uint32_t index; /* position in g.iort.smmus */ |
| 57 | uint64_t base; |
| 58 | uint32_t node_off; |
| 59 | struct ck_smmu_regs regs; /* ctx = this instance */ |
| 60 | struct ck_smmu_tables t; |
| 61 | uint64_t *cds; /* CK_DMA_MAX_STREAMS context descriptors, one page */ |
| 62 | uint32_t sid[CK_DMA_MAX_STREAMS]; |
| 63 | int used[CK_DMA_MAX_STREAMS]; |
| 64 | }; |
| 65 | |
| 66 | static struct { |
| 67 | int state; /* IORT: 0 not read, 1 parsed, CK_SMMU_ABSENT / CK_SMMU_FAILED */ |
| 68 | struct ck_iort_smmu iort; |
| 69 | struct inst inst[CK_IORT_MAX_SMMUS]; |
| 70 | } g; |
| 71 | |
| 72 | static uint32_t r_rd32(void *ctx, uint32_t off) |
| 73 | { |
| 74 | return *(volatile uint32_t *)(uintptr_t)(((struct inst *)ctx)->base + off); |
| 75 | } |
| 76 | static void r_wr32(void *ctx, uint32_t off, uint32_t v) |
| 77 | { |
| 78 | *(volatile uint32_t *)(uintptr_t)(((struct inst *)ctx)->base + off) = v; |
| 79 | } |
| 80 | static void r_wr64(void *ctx, uint32_t off, uint64_t v) |
| 81 | { |
| 82 | *(volatile uint64_t *)(uintptr_t)(((struct inst *)ctx)->base + off) = v; |
| 83 | } |
| 84 | static void r_bar(void *ctx) |
| 85 | { |
| 86 | (void)ctx; |
| 87 | ck_mb(); |
| 88 | } |
| 89 | |
| 90 | static int pt_alloc(void *ctx, uint64_t *phys) |
| 91 | { |
| 92 | (void)ctx; |
| 93 | return ck_dma_alloc(4096, 4096, phys) ? 0 : -1; |
| 94 | } |
| 95 | |
| 96 | static int l2_alloc(void *ctx, uint64_t **page) |
| 97 | { |
| 98 | (void)ctx; |
| 99 | uint64_t p; |
| 100 | *page = ck_dma_alloc(CK_SMMU_L2_BYTES, CK_SMMU_L2_BYTES, &p); |
| 101 | return *page ? 0 : -1; |
| 102 | } |
| 103 | |
| 104 | /* Stream table format: two-level when the SMMU supports it |
| 105 | * (IDR0.ST_LEVEL == 0b01), sized to MIN(IDR1.SIDSIZE, CK_SMMU_L2_MAX_BITS) |
| 106 | * StreamID bits, so the DGX Spark's PCI streams (up to 0xfffff, MEASURED |
| 107 | * from its IORT) fit; L2 spans are allocated per grant. Otherwise the |
| 108 | * linear table of STE_N entries. 0 ok, -1 allocation failed. */ |
| 109 | static int alloc_strtab(struct inst *in) |
| 110 | { |
| 111 | uint64_t p; |
| 112 | uint32_t idr0 = r_rd32(in, CK_SMMU_IDR0), sidsize = r_rd32(in, CK_SMMU_IDR1) & CK_SMMU_IDR1_SIDSIZE_MASK; |
| 113 | if (((idr0 >> CK_SMMU_IDR0_ST_LEVEL_SHIFT) & CK_SMMU_IDR0_ST_LEVEL_MASK) == 1u && sidsize >= CK_SMMU_L2_MIN_BITS) { |
| 114 | uint32_t bits = sidsize < CK_SMMU_L2_MAX_BITS ? sidsize : CK_SMMU_L2_MAX_BITS; |
| 115 | uint32_t l1_bytes = (1u << (bits - CK_SMMU_SPLIT)) * 8u; |
| 116 | if (l1_bytes < 64u) |
| 117 | l1_bytes = 64u; |
| 118 | in->t.sid_bits = bits; |
| 119 | in->t.l2_alloc = l2_alloc; |
| 120 | in->t.l1 = ck_dma_alloc(l1_bytes, l1_bytes, &p); |
| 121 | ck_printf("smmu: stream table 2-level log2size=%u split=%u (spans allocated per grant)\n", bits, |
| 122 | CK_SMMU_SPLIT); |
| 123 | return in->t.l1 ? 0 : -1; |
| 124 | } |
| 125 | in->t.ste_n = STE_N; |
| 126 | in->t.strtab = ck_dma_alloc(STE_N * CK_SMMU_STE_BYTES, STE_N * CK_SMMU_STE_BYTES, &p); |
| 127 | ck_printf("smmu: stream table linear entries=%u\n", STE_N); |
| 128 | return in->t.strtab ? 0 : -1; |
| 129 | } |
| 130 | |
| 131 | /* Read and parse the IORT once per boot. 0 parsed, else CK_SMMU_ABSENT / |
| 132 | * CK_SMMU_FAILED (fail closed). */ |
| 133 | static int parse_iort(void) |
| 134 | { |
| 135 | if (g.state) |
| 136 | return g.state == 1 ? 0 : g.state; |
| 137 | g.state = CK_SMMU_ABSENT; |
| 138 | const void *iort = ck_acpi_find("IORT"); |
| 139 | if (!iort) |
| 140 | return g.state; |
| 141 | int rc = ck_iort_parse(iort, &g.iort); |
| 142 | if (rc == 0) |
| 143 | return g.state; |
| 144 | if (rc < 0) { |
| 145 | g.state = CK_SMMU_FAILED; |
| 146 | ck_printf("smmu: IORT malformed, SMMU unusable (fail closed)\n"); |
| 147 | return g.state; |
| 148 | } |
| 149 | g.state = 1; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* Bring up the SMMUv3 node at g.iort.smmus[index] the first time it is |
| 154 | * needed. 0 up, else CK_SMMU_FAILED (fail closed). */ |
| 155 | static int bring_up(uint32_t index) |
| 156 | { |
| 157 | struct inst *in = &g.inst[index]; |
| 158 | if (in->state) |
| 159 | return in->state == 1 ? 0 : in->state; |
| 160 | in->state = CK_SMMU_FAILED; |
| 161 | in->index = index; |
| 162 | in->base = g.iort.smmus[index].base; |
| 163 | in->node_off = g.iort.smmus[index].off; |
| 164 | in->regs.ctx = in; |
| 165 | in->regs.rd32 = r_rd32; |
| 166 | in->regs.wr32 = r_wr32; |
| 167 | in->regs.wr64 = r_wr64; |
| 168 | in->regs.barrier = r_bar; |
| 169 | ck_mmio_map(in->base, CK_SMMU_MMIO_BYTES); |
| 170 | uint64_t p; |
| 171 | int st = alloc_strtab(in); /* first, as before: the largest aligned block */ |
| 172 | in->t.cmd_n = QUEUE_N; |
| 173 | in->t.evt_n = QUEUE_N; |
| 174 | in->t.cmdq = ck_dma_alloc(4096, 4096, &p); |
| 175 | in->t.evtq = ck_dma_alloc(4096, 4096, &p); |
| 176 | in->cds = ck_dma_alloc(4096, 4096, &p); |
| 177 | if (st || !in->t.cmdq || !in->t.evtq || !in->cds) { |
| 178 | ck_printf("smmu: table allocation failed (fail closed)\n"); |
| 179 | return in->state; |
| 180 | } |
| 181 | int rc = ck_smmu_enable(&in->regs, &in->t, CK_SMMU_DEFAULT_SPINS); |
| 182 | if (rc) { |
| 183 | ck_printf("smmu: bring-up failed rc=%d base=0x%llx (fail closed)\n", rc, (unsigned long long)in->base); |
| 184 | return in->state; |
| 185 | } |
| 186 | if (index) |
| 187 | ck_printf("smmu: instance %u up base=0x%llx\n", index, (unsigned long long)in->base); |
| 188 | in->state = 1; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int confine_sid(uint32_t index, uint32_t sid, uint64_t phys, uint64_t len, const struct ck_iort_route *rt, |
| 193 | struct ck_dma_confinement *out); |
| 194 | |
| 195 | int ck_dma_confine(uint32_t segment, uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) |
| 196 | { |
| 197 | if (!len || (phys & 0xfffu) || (len & 0xfffu) || phys + len < phys) |
| 198 | return CK_SMMU_EARG; |
| 199 | int rc = parse_iort(); |
| 200 | if (rc) |
| 201 | return rc; |
| 202 | struct ck_iort_route rt; |
| 203 | if (ck_iort_route(&g.iort, segment, rid, &rt)) |
| 204 | return CK_SMMU_NOSTREAM; |
| 205 | rc = bring_up(rt.smmu_index); |
| 206 | if (rc) |
| 207 | return rc; |
| 208 | return confine_sid(rt.smmu_index, rt.sid, phys, len, &rt, out); |
| 209 | } |
| 210 | |
| 211 | int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) |
| 212 | { |
| 213 | if (!acpi_name || !len || (phys & 0xfffu) || (len & 0xfffu) || phys + len < phys) |
| 214 | return CK_SMMU_EARG; |
| 215 | int rc = parse_iort(); |
| 216 | if (rc) |
| 217 | return rc; |
| 218 | const void *iort = ck_acpi_find("IORT"); |
| 219 | struct ck_iort_named nc; |
| 220 | int nrc = iort ? ck_iort_named(iort, acpi_name, &nc) : 0; |
| 221 | if (nrc != 1 || !nc.target_off) |
| 222 | return nrc == -2 ? CK_SMMU_EARG : CK_SMMU_NOSTREAM; |
| 223 | /* The SMMUv3 node the component maps to; one the parser did not keep |
| 224 | * (beyond CK_IORT_MAX_SMMUS) is refused, never granted unconfined. */ |
| 225 | uint32_t index; |
| 226 | for (index = 0; index < g.iort.nsmmus; index++) |
| 227 | if (g.iort.smmus[index].off == nc.target_off) |
| 228 | break; |
| 229 | if (index == g.iort.nsmmus) { |
| 230 | out->smmu_base = nc.target_base; /* reported, never used */ |
| 231 | out->stream_id = nc.stream_id; |
| 232 | out->iova = out->len = 0; |
| 233 | return CK_SMMU_OTHER; |
| 234 | } |
| 235 | rc = bring_up(index); |
| 236 | if (rc) |
| 237 | return rc; |
| 238 | struct ck_iort_route rt = { 0 }; |
| 239 | (void)ck_iort_rmr_for(&g.iort, nc.target_off, nc.stream_id, &rt); |
| 240 | return confine_sid(index, nc.stream_id, phys, len, &rt, out); |
| 241 | } |
| 242 | |
| 243 | /* Identity-map every firmware-reserved range the IORT names for this |
| 244 | * stream into pt, Normal Non-cacheable. 0 ok (ranges mapped in *n), -1 an |
| 245 | * attribute this kernel does not honour or a range that collides with the |
| 246 | * window (fail closed). */ |
| 247 | static int map_reserved(struct ck_pt *pt, const struct ck_iort_route *rt, uint32_t sid, uint32_t *n) |
| 248 | { |
| 249 | *n = 0; |
| 250 | for (uint32_t i = 0; i < rt->nrmr; i++) { |
| 251 | const struct ck_iort_rmr *r = rt->rmr[i]; |
| 252 | uint32_t attr = CK_IORT_RMR_ATTR(r->flags); |
| 253 | if (attr != CK_IORT_RMR_ATTR_NORMAL_NC && attr != CK_IORT_RMR_ATTR_NORMAL_IWB_OWB) { |
| 254 | ck_printf("smmu: stream 0x%x reserved range attr %u unsupported (fail closed)\n", sid, attr); |
| 255 | return -1; |
| 256 | } |
| 257 | for (uint32_t k = 0; k < r->nranges; k++) { |
| 258 | if (ck_pt_map(pt, r->range[k].base, r->range[k].base, r->range[k].len, WINDOW_ATTRS)) { |
| 259 | ck_printf("smmu: stream 0x%x reserved range 0x%llx+0x%llx not mappable (fail closed)\n", sid, |
| 260 | (unsigned long long)r->range[k].base, (unsigned long long)r->range[k].len); |
| 261 | return -1; |
| 262 | } |
| 263 | #ifdef CK_B7B_EVENT_READOUT |
| 264 | ck_printf("b7b_rmr: sid=0x%x base=0x%llx len=0x%llx flags=0x%x identity=yes\n", |
| 265 | sid, (unsigned long long)r->range[k].base, |
| 266 | (unsigned long long)r->range[k].len, r->flags); |
| 267 | #endif |
| 268 | (*n)++; |
| 269 | } |
| 270 | } |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* Stage-1 window [phys, phys+len) (plus the stream's reserved ranges) for |
| 275 | * stream sid on instance index, which is up. */ |
| 276 | static int confine_sid(uint32_t index, uint32_t sid, uint64_t phys, uint64_t len, const struct ck_iort_route *rt, |
| 277 | struct ck_dma_confinement *out) |
| 278 | { |
| 279 | struct inst *in = &g.inst[index]; |
| 280 | int rc; |
| 281 | if (!ck_smmu_sid_in_range(&in->t, sid)) |
| 282 | return CK_SMMU_NOSTREAM; /* beyond the stream table this SMMU runs */ |
| 283 | int slot = -1; |
| 284 | for (int i = 0; i < CK_DMA_MAX_STREAMS; i++) { |
| 285 | if (in->used[i] && in->sid[i] == sid) |
| 286 | return CK_SMMU_EARG; |
| 287 | if (!in->used[i] && slot < 0) |
| 288 | slot = i; |
| 289 | } |
| 290 | if (slot < 0) |
| 291 | return CK_SMMU_EARG; |
| 292 | struct ck_pt pt; |
| 293 | uint32_t reserved; |
| 294 | if (ck_pt_init(&pt, pt_alloc, 0) || ck_pt_map(&pt, phys, phys, len, WINDOW_ATTRS) || |
| 295 | map_reserved(&pt, rt, sid, &reserved)) |
| 296 | return CK_SMMU_FAILED; |
| 297 | uint64_t *cd = in->cds + (uint64_t)slot * 8u; |
| 298 | if (ck_smmu_cd_stage1(cd, pt.root, (uint16_t)(slot + 1), CK_MAIR_VALUE, 16)) |
| 299 | return CK_SMMU_FAILED; |
| 300 | uint64_t ste[8]; |
| 301 | ck_smmu_ste_stage1(ste, (uint64_t)(uintptr_t)cd); |
| 302 | ck_mb(); /* tables and CD written before the STE goes live */ |
| 303 | rc = ck_smmu_install_ste(&in->regs, &in->t, sid, ste, CK_SMMU_DEFAULT_SPINS); |
| 304 | if (rc) { |
| 305 | (void)ck_smmu_abort_ste(&in->regs, &in->t, sid, CK_SMMU_DEFAULT_SPINS); |
| 306 | ck_printf("smmu: stream 0x%x install failed rc=%d (stream aborts)\n", sid, rc); |
| 307 | return CK_SMMU_FAILED; |
| 308 | } |
| 309 | if (reserved) |
| 310 | ck_printf("smmu: stream 0x%x on 0x%llx: %u firmware-reserved range(s) identity-mapped beside the window\n", |
| 311 | sid, (unsigned long long)in->base, reserved); |
| 312 | in->used[slot] = 1; |
| 313 | in->sid[slot] = sid; |
| 314 | out->smmu_base = in->base; |
| 315 | out->stream_id = sid; |
| 316 | out->iova = phys; |
| 317 | out->len = len; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* The one live instance holding stream_id confined, or NULL. Two instances |
| 322 | * holding the same stream id (the namespace is per SMMU) is ambiguous: |
| 323 | * *ambiguous is set and NULL returned. */ |
| 324 | static struct inst *holder(uint32_t stream_id, int *slot, int *ambiguous) |
| 325 | { |
| 326 | struct inst *found = 0; |
| 327 | *ambiguous = 0; |
| 328 | for (uint32_t k = 0; k < CK_IORT_MAX_SMMUS; k++) { |
| 329 | struct inst *in = &g.inst[k]; |
| 330 | if (in->state != 1) |
| 331 | continue; |
| 332 | for (int i = 0; i < CK_DMA_MAX_STREAMS; i++) { |
| 333 | if (!in->used[i] || in->sid[i] != stream_id) |
| 334 | continue; |
| 335 | if (found) { |
| 336 | *ambiguous = 1; |
| 337 | return 0; |
| 338 | } |
| 339 | found = in; |
| 340 | *slot = i; |
| 341 | } |
| 342 | } |
| 343 | return found; |
| 344 | } |
| 345 | |
| 346 | int ck_dma_unconfine(uint32_t stream_id) |
| 347 | { |
| 348 | int slot = 0, amb; |
| 349 | struct inst *in = holder(stream_id, &slot, &amb); |
| 350 | if (amb) |
| 351 | return CK_SMMU_AMBIGUOUS; /* fail closed: neither holder is aborted */ |
| 352 | if (!in) |
| 353 | return CK_SMMU_EARG; /* not confined */ |
| 354 | if (ck_smmu_abort_ste(&in->regs, &in->t, stream_id, CK_SMMU_DEFAULT_SPINS)) |
| 355 | return CK_SMMU_FAILED; |
| 356 | in->used[slot] = 0; |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | #ifdef CK_B7B_EVENT_READOUT |
| 361 | static void b7b_event(void *ctx, const struct ck_smmu_event *e) |
| 362 | { |
| 363 | const struct inst *in = ctx; |
| 364 | ck_printf("b7b_event: instance=%u base=0x%llx sid=0x%x type=0x%02x name=%s addr=0x%llx " |
| 365 | "raw=%016llx,%016llx,%016llx,%016llx\n", |
| 366 | in->index, (unsigned long long)in->base, e->sid, e->type, |
| 367 | ck_smmu_event_name(e->type), (unsigned long long)e->addr, |
| 368 | (unsigned long long)e->raw[0], (unsigned long long)e->raw[1], |
| 369 | (unsigned long long)e->raw[2], (unsigned long long)e->raw[3]); |
| 370 | } |
| 371 | |
| 372 | void ck_b7b_event_readout(void) |
| 373 | { |
| 374 | ck_puts("b7b_events: final begin all_sids=yes\n"); |
| 375 | for (uint32_t k = 0; k < CK_IORT_MAX_SMMUS; k++) { |
| 376 | struct inst *in = &g.inst[k]; |
| 377 | if (in->state != 1) { |
| 378 | ck_printf("b7b_events: instance=%u state=%s\n", k, |
| 379 | in->state == CK_SMMU_FAILED ? "failed" : "not-live"); |
| 380 | continue; |
| 381 | } |
| 382 | struct ck_smmu_observation o = ck_smmu_observe_events(&in->regs, &in->t, b7b_event, in); |
| 383 | ck_printf("b7b_events: instance=%u base=0x%llx events=%u overflow=%d pending=%d\n", |
| 384 | k, (unsigned long long)in->base, o.events, o.overflow, o.pending); |
| 385 | } |
| 386 | ck_puts("b7b_events: final end\n"); |
| 387 | } |
| 388 | #endif |
| 389 | |
| 390 | int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) |
| 391 | { |
| 392 | int matched = 0, ovf = 0, any = 0; |
| 393 | for (uint32_t k = 0; k < CK_IORT_MAX_SMMUS; k++) { |
| 394 | struct inst *in = &g.inst[k]; |
| 395 | if (in->state != 1) |
| 396 | continue; |
| 397 | any = 1; |
| 398 | int n; |
| 399 | struct ck_smmu_event ev[4]; |
| 400 | do { |
| 401 | int o = 0; |
| 402 | n = ck_smmu_read_events(&in->regs, &in->t, ev, 4, &o); |
| 403 | ovf |= o; |
| 404 | #ifdef CK_B7B_EVENT_READOUT |
| 405 | if (o) |
| 406 | ck_printf("b7b_events: instance=%u source=dma_faults overflow=1\n", k); |
| 407 | #endif |
| 408 | for (int i = 0; i < n; i++) { |
| 409 | #ifdef CK_B7B_EVENT_READOUT |
| 410 | /* Report before filtering: other SIDs must not disappear when |
| 411 | * a device's negative test consumes the shared event queue. */ |
| 412 | b7b_event(in, &ev[i]); |
| 413 | #endif |
| 414 | if (ev[i].sid != stream_id) |
| 415 | continue; |
| 416 | if (!matched && first) { |
| 417 | first->type = ev[i].type; |
| 418 | first->stream_id = ev[i].sid; |
| 419 | first->addr = ev[i].addr; |
| 420 | } |
| 421 | matched++; |
| 422 | } |
| 423 | } while (n == 4); |
| 424 | } |
| 425 | if (!any) |
| 426 | return -1; |
| 427 | if (first) |
| 428 | first->overflow = ovf; |
| 429 | return matched; |
| 430 | } |
| 431 | |
| 432 | #ifdef CK_B7B_PROBE |
| 433 | /* Cut B7b preparation (aienos#286; Drake's ruling of 2026-10-10 authorizes the |
| 434 | * preparation, not the boot): a probe build (make CK_B7B_PROBE=1, own OUT) |
| 435 | * that, once per boot, confines PCI segment 15 requester 0x100 (the GB10, IORT |
| 436 | * stream 0x100 on SMMUv3 B at 0x13000000 on the DGX Spark, docs/GB10_IORT_DECODE.md) |
| 437 | * on a one-page DMA window, reports the route, the RMR count and SMMU_IDR0 / |
| 438 | * SMMU_IDR1 of the node it came up on (read only: "Access to this field is RO", |
| 439 | * IHI0070H.a 6.3.1 / 6.3.2), unconfines, and prints one summary line. No DMA is |
| 440 | * started; the window page is never handed to a device. After the probe the |
| 441 | * node stays enabled with every stream aborting (as after any unconfine). |
| 442 | * QEMU virt has no segment 15, so there the expected result is a fail-closed |
| 443 | * refusal. CK_B7B_PROBE_TEST_SEGMENT0=1 (TEST-ONLY, QEMU positive path, |
| 444 | * refused with CK_HARDWARE_STAGING) probes segment 0 instead. |
| 445 | * Lines judged by scripts/qemu_ck_b7b_probe_test.sh. */ |
| 446 | #if defined(CK_B7B_PROBE_TEST_SEGMENT0) && defined(CK_HARDWARE_STAGING) |
| 447 | #error "CK_B7B_PROBE_TEST_SEGMENT0 (TEST-only QEMU positive path) cannot be combined with CK_HARDWARE_STAGING" |
| 448 | #endif |
| 449 | #ifdef CK_B7B_PROBE_TEST_SEGMENT0 |
| 450 | #define B7B_SEGMENT 0u |
| 451 | #else |
| 452 | #define B7B_SEGMENT 15u |
| 453 | #endif |
| 454 | #define B7B_RID 0x100u |
| 455 | |
| 456 | static const char *b7b_rc(int rc) |
| 457 | { |
| 458 | switch (rc) { |
| 459 | case 0: return "ok"; |
| 460 | case CK_SMMU_ABSENT: return "absent"; |
| 461 | case CK_SMMU_FAILED: return "failed"; |
| 462 | case CK_SMMU_NOSTREAM: return "nostream"; |
| 463 | case CK_SMMU_EARG: return "earg"; |
| 464 | case CK_SMMU_OTHER: return "other"; |
| 465 | default: return "unknown"; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | void ck_b7b_probe(void) |
| 470 | { |
| 471 | #ifdef CK_B7B_PROBE_TEST_SEGMENT0 |
| 472 | ck_puts("b7b_probe: TEST-ONLY B7b probe on PCI segment 0 (QEMU positive path); never hardware evidence\n"); |
| 473 | #endif |
| 474 | ck_printf("b7b_probe: start segment=%u rid=0x%x window_bytes=4096 no_dma=yes\n", B7B_SEGMENT, B7B_RID); |
| 475 | int rc = parse_iort(); |
| 476 | struct ck_iort_route rt; |
| 477 | if (rc) { |
| 478 | ck_printf("b7b_probe: iort rc=%d (%s)\n", rc, b7b_rc(rc)); |
| 479 | } else if (ck_iort_route(&g.iort, B7B_SEGMENT, B7B_RID, &rt)) { |
| 480 | ck_printf("b7b_probe: route none (smmus=%u)\n", g.iort.nsmmus); |
| 481 | } else { |
| 482 | ck_printf("b7b_probe: route instance=%u base=0x%llx stream=0x%x rmr_nodes=%u (smmus=%u)\n", rt.smmu_index, |
| 483 | (unsigned long long)rt.smmu_base, rt.sid, rt.nrmr, g.iort.nsmmus); |
| 484 | } |
| 485 | uint64_t phys = 0; |
| 486 | if (!ck_dma_alloc(4096, 4096, &phys)) { |
| 487 | ck_puts("b7b_probe: no DMA page\nAIENOS_B7B_PROBE: FAIL\n"); |
| 488 | return; |
| 489 | } |
| 490 | struct ck_dma_confinement cf; |
| 491 | rc = ck_dma_confine(B7B_SEGMENT, B7B_RID, phys, 4096, &cf); |
| 492 | if (rc) { |
| 493 | ck_printf("b7b_probe: confine refused rc=%d (%s); nothing granted\n", rc, b7b_rc(rc)); |
| 494 | ck_puts("AIENOS_B7B_PROBE: REFUSED\n"); |
| 495 | return; |
| 496 | } |
| 497 | ck_printf("b7b_probe: confined stream=0x%x smmu_base=0x%llx iova=0x%llx len=0x%llx\n", cf.stream_id, |
| 498 | (unsigned long long)cf.smmu_base, (unsigned long long)cf.iova, (unsigned long long)cf.len); |
| 499 | int idr_ok = 0; |
| 500 | for (uint32_t k = 0; k < CK_IORT_MAX_SMMUS; k++) { |
| 501 | struct inst *in = &g.inst[k]; |
| 502 | if (in->state != 1 || in->base != cf.smmu_base) |
| 503 | continue; |
| 504 | uint32_t idr0 = r_rd32(in, CK_SMMU_IDR0), idr1 = r_rd32(in, CK_SMMU_IDR1); |
| 505 | ck_printf("b7b_probe: instance=%u idr0=0x%08x idr1=0x%08x st_level=%u sidsize=%u\n", k, idr0, idr1, |
| 506 | (idr0 >> CK_SMMU_IDR0_ST_LEVEL_SHIFT) & CK_SMMU_IDR0_ST_LEVEL_MASK, |
| 507 | idr1 & CK_SMMU_IDR1_SIDSIZE_MASK); |
| 508 | idr_ok = 1; |
| 509 | break; |
| 510 | } |
| 511 | if (!idr_ok) |
| 512 | ck_puts("b7b_probe: no live instance at the confined base\n"); |
| 513 | int urc = ck_dma_unconfine(cf.stream_id); |
| 514 | ck_printf("b7b_probe: unconfine rc=%d (%s); stream 0x%x aborts again\n", urc, b7b_rc(urc), cf.stream_id); |
| 515 | ck_puts(urc || !idr_ok ? "AIENOS_B7B_PROBE: FAIL\n" : "AIENOS_B7B_PROBE: CONFINED_AND_RELEASED\n"); |
| 516 | } |
| 517 | #endif |