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/core/acpi.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 / core / acpi.c` 723 lines ยท 25693 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 /* acpi.c -- see acpi.h. Unaligned little-endian reads only (ACPI tables are 18 * byte packed and the kernel builds with -mstrict-align). */ 19 #include "acpi.h" 20 21 static uint32_t rd32(const uint8_t *p) 22 { 23 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; 24 } 25 26 static uint64_t rd64(const uint8_t *p) 27 { 28 return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; 29 } 30 31 static const uint8_t *ptr(uint64_t a) 32 { 33 return (const uint8_t *)(uintptr_t)a; 34 } 35 36 int ck_acpi_sum_ok(const void *p, size_t len) 37 { 38 const uint8_t *b = p; 39 uint8_t s = 0; 40 for (size_t i = 0; i < len; i++) 41 s = (uint8_t)(s + b[i]); 42 return s == 0; 43 } 44 45 static int sig_eq(const uint8_t *p, const char *sig, size_t n) 46 { 47 for (size_t i = 0; i < n; i++) 48 if (p[i] != (uint8_t)sig[i]) 49 return 0; 50 return 1; 51 } 52 53 uint64_t ck_acpi_root(uint64_t rsdp, int *is_xsdt) 54 { 55 const uint8_t *r = ptr(rsdp); 56 if (!r || !sig_eq(r, "RSD PTR ", 8) || !ck_acpi_sum_ok(r, 20)) 57 return 0; 58 if (r[15] >= 2) { 59 uint32_t len = rd32(r + 20); 60 if (len < 36 || len > 4096 || !ck_acpi_sum_ok(r, len)) 61 return 0; 62 uint64_t x = rd64(r + 24); 63 if (x) { 64 if (is_xsdt) 65 *is_xsdt = 1; 66 return x; 67 } 68 } 69 if (is_xsdt) 70 *is_xsdt = 0; 71 return rd32(r + 16); 72 } 73 74 static int root_entries(uint64_t rsdp, const uint8_t **ents, uint32_t *count, uint32_t *width) 75 { 76 int x = 0; 77 const uint8_t *root = ptr(ck_acpi_root(rsdp, &x)); 78 if (!root || !sig_eq(root, x ? "XSDT" : "RSDT", 4)) 79 return -1; 80 uint32_t len = rd32(root + 4); 81 if (len < CK_ACPI_SDT_HEADER || len > (1u << 20) || !ck_acpi_sum_ok(root, len)) 82 return -1; 83 *width = x ? 8 : 4; 84 *count = (len - CK_ACPI_SDT_HEADER) / *width; 85 *ents = root + CK_ACPI_SDT_HEADER; 86 return 0; 87 } 88 89 const void *ck_acpi_lookup(uint64_t rsdp, const char sig[4]) 90 { 91 const uint8_t *ents; 92 uint32_t count, width; 93 if (root_entries(rsdp, &ents, &count, &width)) 94 return 0; 95 for (uint32_t i = 0; i < count; i++) { 96 const uint8_t *t = ptr(width == 8 ? rd64(ents + i * 8) : rd32(ents + i * 4)); 97 if (!t || !sig_eq(t, sig, 4)) 98 continue; 99 uint32_t len = rd32(t + 4); 100 if (len < CK_ACPI_SDT_HEADER || len > (1u << 24) || !ck_acpi_sum_ok(t, len)) 101 continue; 102 return t; 103 } 104 return 0; 105 } 106 107 int ck_acpi_each(uint64_t rsdp, void (*fn)(uint64_t, uint32_t, void *), void *ctx) 108 { 109 const uint8_t *ents; 110 uint32_t count, width; 111 if (root_entries(rsdp, &ents, &count, &width)) 112 return -1; 113 for (uint32_t i = 0; i < count; i++) { 114 uint64_t a = width == 8 ? rd64(ents + i * 8) : rd32(ents + i * 4); 115 if (a) 116 fn(a, rd32(ptr(a) + 4), ctx); 117 } 118 return (int)count; 119 } 120 121 int ck_madt_parse(const void *madt, struct ck_madt_gic *out) 122 { 123 const uint8_t *m = madt; 124 struct ck_madt_gic g = { 0 }; 125 uint32_t len = rd32(m + 4); 126 if (!sig_eq(m, "APIC", 4) || len < 44) 127 return -1; 128 for (uint32_t at = 44; at < len;) { 129 if (at + 2 > len) 130 return -1; 131 uint8_t type = m[at], elen = m[at + 1]; 132 if (elen < 2 || at + elen > len) 133 return -1; 134 const uint8_t *e = m + at; 135 if (type == 0x0b) { /* GICC */ 136 if (elen < 76) 137 return -1; 138 uint32_t flags = rd32(e + 12); 139 if (flags & (1u | 8u)) { 140 if (!g.gicc_count) { 141 g.gicc_base = rd64(e + 32); 142 g.gicc_gicr = rd64(e + 60); 143 } 144 g.gicc_count++; 145 } 146 } else if (type == 0x0c) { /* GICD */ 147 if (elen < 24) 148 return -1; 149 if (!g.gicd) { 150 g.gicd = rd64(e + 8); 151 g.gic_version = e[20]; 152 } 153 } else if (type == 0x0e) { /* GICR discovery range */ 154 if (elen < 16) 155 return -1; 156 if (!g.gicr) { 157 g.gicr = rd64(e + 4); 158 g.gicr_len = rd32(e + 12); 159 } 160 } 161 at += elen; 162 } 163 *out = g; 164 return 0; 165 } 166 167 168 int ck_madt_gic_kind(const struct ck_madt_gic *m) 169 { 170 if (m->gic_version == 2) 171 return m->gicc_base ? 2 : 0; 172 if (m->gic_version >= 3) 173 return 3; 174 if (m->gic_version == 0) { 175 if (m->gicr || m->gicc_gicr) 176 return 3; 177 if (m->gicc_base) 178 return 2; 179 } 180 return 0; 181 } 182 183 int ck_madt_mpidrs(const void *madt, uint64_t *out, unsigned max, unsigned *n) 184 { 185 const uint8_t *m = madt; 186 uint32_t len = rd32(m + 4); 187 unsigned count = 0; 188 *n = 0; 189 if (!sig_eq(m, "APIC", 4) || len < 44) 190 return -1; 191 for (uint32_t at = 44; at < len;) { 192 if (at + 2 > len) 193 return -1; 194 uint8_t type = m[at], elen = m[at + 1]; 195 if (elen < 2 || at + elen > len) 196 return -1; 197 const uint8_t *e = m + at; 198 if (type == 0x0b) { /* GICC */ 199 if (elen < 76) 200 return -1; 201 if (rd32(e + 12) & (1u | 8u)) { 202 if (count < max) 203 out[count] = rd64(e + 68) & CK_MPIDR_AFF_MASK; 204 count++; 205 } 206 } 207 at += elen; 208 } 209 *n = count; 210 return count > max ? -2 : 0; 211 } 212 213 static void topo_add(struct ck_cpu_topology *t, uint8_t cls) 214 { 215 for (unsigned i = 0; i < t->distinct_classes; i++) 216 if (t->class_id[i] == cls) { 217 t->class_count[i]++; 218 return; 219 } 220 if (t->distinct_classes >= CK_MAX_CLASSES) { 221 t->unknown_class++; 222 return; 223 } 224 /* Insert keeping the classes sorted. */ 225 unsigned at = t->distinct_classes; 226 while (at > 0 && t->class_id[at - 1] > cls) { 227 t->class_id[at] = t->class_id[at - 1]; 228 t->class_count[at] = t->class_count[at - 1]; 229 at--; 230 } 231 t->class_id[at] = cls; 232 t->class_count[at] = 1; 233 t->distinct_classes++; 234 } 235 236 int ck_madt_cpu_topology(const void *madt, struct ck_cpu_topology *out) 237 { 238 const uint8_t *m = madt; 239 struct ck_cpu_topology t = { 0 }; 240 uint32_t len = rd32(m + 4); 241 if (!sig_eq(m, "APIC", 4) || len < 44) 242 return -1; 243 for (uint32_t at = 44; at < len;) { 244 if (at + 2 > len) 245 return -1; 246 uint8_t type = m[at], elen = m[at + 1]; 247 if (elen < 2 || at + elen > len) 248 return -1; 249 const uint8_t *e = m + at; 250 if (type == 0x0b) { /* GICC */ 251 if (elen < 16) 252 return -1; 253 if (rd32(e + 12) & (1u | 8u)) { 254 t.cores++; 255 if (!t.has_first_mpidr && elen >= 76) { 256 t.has_first_mpidr = 1; 257 t.first_mpidr = rd64(e + 68); 258 } 259 if (elen >= 77) 260 topo_add(&t, e[76]); 261 else 262 t.unknown_class++; 263 } 264 } 265 at += elen; 266 } 267 *out = t; 268 return 0; 269 } 270 271 int ck_place_task(const struct ck_cpu_topology *t, uint32_t task_index, uint8_t *class_id, 272 uint32_t *core) 273 { 274 uint32_t capacity = 0; 275 for (unsigned i = 0; i < t->distinct_classes; i++) 276 capacity += t->class_count[i]; 277 if (capacity == 0) 278 return -1; 279 uint32_t idx = task_index % capacity; 280 for (unsigned i = 0; i < t->distinct_classes; i++) { 281 if (idx < t->class_count[i]) { 282 *class_id = t->class_id[i]; 283 *core = idx; 284 return 0; 285 } 286 idx -= t->class_count[i]; 287 } 288 return -1; 289 } 290 int ck_spcr_parse(const void *spcr, struct ck_spcr *out) 291 { 292 const uint8_t *s = spcr; 293 if (!sig_eq(s, "SPCR", 4) || rd32(s + 4) < 52) 294 return -1; 295 out->interface_type = s[36]; 296 out->space = s[40]; 297 out->bit_width = s[41]; 298 out->access_size = s[43]; 299 out->base = rd64(s + 44); 300 return 0; 301 } 302 303 int ck_fadt_arm_boot_arch(const void *fadt, uint16_t *flags) 304 { 305 const uint8_t *f = fadt; 306 if (!sig_eq(f, "FACP", 4) || rd32(f + 4) < 131) 307 return -1; 308 *flags = (uint16_t)(f[129] | f[130] << 8); 309 return 0; 310 } 311 312 static uint64_t span_end(uint64_t a, uint64_t len) 313 { 314 /* Saturate below the last 4 KiB page so page rounding cannot wrap. */ 315 const uint64_t top = ~(uint64_t)0xfff; 316 return (a >= top || len > top - a) ? top : a + len; 317 } 318 319 int ck_acpi_spans(uint64_t rsdp, void (*fn)(uint64_t lo, uint64_t hi, void *ctx), void *ctx) 320 { 321 int x = 0; 322 uint64_t root = ck_acpi_root(rsdp, &x); 323 if (!root) 324 return -1; 325 const uint8_t *r = ptr(rsdp); 326 /* ck_acpi_root has checked the RSDP length (36..4096) for revision 2+. */ 327 uint32_t rlen = r[15] >= 2 ? rd32(r + 20) : 20; 328 fn(rsdp, span_end(rsdp, rlen), ctx); 329 int n = 1; 330 uint32_t len = rd32(ptr(root) + 4); 331 fn(root, span_end(root, len < CK_ACPI_SDT_HEADER ? CK_ACPI_SDT_HEADER : len), ctx); 332 n++; 333 const uint8_t *ents; 334 uint32_t count, width; 335 if (root_entries(rsdp, &ents, &count, &width)) 336 return n; 337 for (uint32_t i = 0; i < count; i++) { 338 uint64_t a = width == 8 ? rd64(ents + i * 8) : rd32(ents + i * 4); 339 if (!a) 340 continue; 341 uint32_t tl = rd32(ptr(a) + 4); 342 fn(a, span_end(a, tl < CK_ACPI_SDT_HEADER ? CK_ACPI_SDT_HEADER : tl), ctx); 343 n++; 344 } 345 return n; 346 } 347 348 /* TEST-only IORT mutants (make iort-mutants): each must make test_smmu 349 * FAIL. Host builds only; never part of an image. */ 350 #if defined(CK_IORT_MUTANT_TRUNCATE) || defined(CK_IORT_MUTANT_SEGMENT_BLIND) || defined(CK_IORT_MUTANT_OVERLAP_OK) || \ 351 defined(CK_IORT_MUTANT_OTHER_BLIND) || defined(CK_IORT_MUTANT_RMR_BLIND) || \ 352 defined(CK_IORT_MUTANT_RMR_EXCLUSIVE) || defined(CK_IORT_MUTANT_ROUTE_FIRST) 353 #if !__STDC_HOSTED__ 354 #error "CK_IORT_MUTANT_* are host test mutants only" 355 #endif 356 #ifdef CK_HARDWARE_STAGING 357 #error "CK_IORT_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" 358 #endif 359 #endif 360 361 /* IORT walk: calls back for every node after checking its header, length 362 * and ID-mapping array bounds. -1 on any malformed node. */ 363 #define IORT_NODE_HDR 16u 364 #define IORT_NODE_ROOT_COMPLEX 2u 365 #define IORT_NODE_SMMUV3 4u 366 #define IORT_NODE_RMR 6u 367 #define IORT_RMR_HDR 28u /* node header + flags, rmr_count, rmr_offset */ 368 #define IORT_RMR_DESC_BYTES 20u /* base u64, length u64, reserved u32 */ 369 #define IORT_MAP_BYTES 20u 370 #define IORT_TABLE_HDR 48u /* SDT header + node count, node array offset, reserved */ 371 #define IORT_RC_SEGMENT 28u /* root complex node: pci_segment_number */ 372 373 static int iort_nodes(const uint8_t *t, uint32_t len, uint32_t *count, uint32_t *first) 374 { 375 if (!sig_eq(t, "IORT", 4) || len < IORT_TABLE_HDR) 376 return -1; 377 *count = rd32(t + 36); 378 *first = rd32(t + 40); 379 return 0; 380 } 381 382 int ck_iort_parse(const void *iort, struct ck_iort_smmu *out) 383 { 384 const uint8_t *t = iort; 385 struct ck_iort_smmu s = { 0 }; 386 uint32_t len = rd32(t + 4), count, at; 387 if (iort_nodes(t, len, &count, &at)) 388 return -1; 389 int found = 0; 390 /* Every SMMUv3 node seen in pass 0 goes to s.smmus (table order); the 391 * first one is `base`/`node_off`, the ones root complexes map to 392 * besides it are named by ck_iort_other_stream, and ck_iort_route 393 * resolves a requester to any of them (cut B7a). */ 394 for (int pass = 0; pass < 2; pass++) { 395 uint32_t off = at; 396 for (uint32_t i = 0; i < count; i++) { 397 if (off < IORT_TABLE_HDR || off > len || len - off < IORT_NODE_HDR) 398 return -1; 399 const uint8_t *n = t + off; 400 uint8_t type = n[0]; 401 uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; 402 if (nlen < IORT_NODE_HDR || nlen > len - off) 403 return -1; 404 uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); 405 if (mcount) { 406 if (mcount > nlen / IORT_MAP_BYTES || moff < IORT_NODE_HDR || moff > nlen || 407 (uint64_t)mcount * IORT_MAP_BYTES > (uint64_t)(nlen - moff)) 408 return -1; 409 } 410 if (pass == 0 && type == IORT_NODE_SMMUV3) { 411 if (nlen < 24) 412 return -1; 413 uint64_t base = rd64(n + 16); 414 if (!base) 415 return -1; 416 if (s.nsmmus >= CK_IORT_MAX_SMMUS) 417 return -1; 418 s.smmus[s.nsmmus].off = off; 419 s.smmus[s.nsmmus].base = base; 420 s.nsmmus++; 421 if (!found) { 422 s.base = base; 423 s.node_off = off; 424 found = 1; 425 } 426 } 427 if (pass == 1 && type == IORT_NODE_RMR) { 428 #ifndef CK_IORT_MUTANT_RMR_BLIND 429 /* RMR node (ACPICA actbl2.h acpi_iort_rmr): flags @16, 430 * rmr_count @20, rmr_offset @24 (from the node start); 431 * acpi_iort_rmr_desc 20 bytes: base_address u64, length 432 * u64, reserved u32. One entry per ID mapping that targets 433 * an SMMUv3 node; its output range is the StreamID range 434 * (Linux iort.c iort_rmr_alloc_sids reads output_base and 435 * id_count the same way). Ranges must be page aligned and 436 * non-empty: the kernel maps them identity. */ 437 if (nlen < IORT_RMR_HDR) 438 return -1; 439 uint32_t flags = rd32(n + 16), rcount = rd32(n + 20), roff = rd32(n + 24); 440 if (!rcount || rcount > CK_IORT_MAX_RMR_RANGES || roff < IORT_RMR_HDR || roff > nlen || 441 (uint64_t)rcount * IORT_RMR_DESC_BYTES > (uint64_t)(nlen - roff)) 442 return -1; 443 struct ck_iort_rmr r = { 0 }; 444 r.node_off = off; 445 r.flags = flags; 446 r.nranges = rcount; 447 for (uint32_t d = 0; d < rcount; d++) { 448 const uint8_t *e = n + roff + d * IORT_RMR_DESC_BYTES; 449 uint64_t b = rd64(e), l = rd64(e + 8); 450 if (!l || (b & 0xfffu) || (l & 0xfffu) || b + l < b) 451 return -1; 452 r.range[d].base = b; 453 r.range[d].len = l; 454 } 455 for (uint32_t m = 0; m < mcount; m++) { 456 const uint8_t *e = n + moff + m * IORT_MAP_BYTES; 457 uint32_t ref = rd32(e + 12), k; 458 for (k = 0; k < s.nsmmus; k++) 459 if (s.smmus[k].off == ref) 460 break; 461 if (k == s.nsmmus) 462 continue; /* a mapping to a non-SMMU node is not a stream range */ 463 if (s.nrmr >= CK_IORT_MAX_RMR) 464 return -1; 465 uint32_t ob = rd32(e + 8), cnt = rd32(e + 4); 466 if (ob + cnt < ob) 467 return -1; 468 r.smmu_index = k; 469 r.sid_lo = ob; 470 #ifndef CK_IORT_MUTANT_RMR_EXCLUSIVE 471 r.sid_hi = ob + cnt; /* id_count = number of IDs - 1: inclusive */ 472 #else 473 r.sid_hi = cnt ? ob + cnt - 1u : ob; /* TEST mutant: id_count read as a count */ 474 #endif 475 s.rmr[s.nrmr++] = r; 476 } 477 #endif 478 } 479 if (pass == 1 && type == IORT_NODE_ROOT_COMPLEX) { 480 /* Root complex node: PCI segment number at +28 (ACPICA 481 * actbl2.h acpi_iort_root_complex: memory_properties u64 482 * @16, ats_attribute u32 @24, pci_segment_number u32 @28). */ 483 if (nlen < IORT_RC_SEGMENT + 4) 484 return -1; 485 uint32_t seg = rd32(n + IORT_RC_SEGMENT); 486 for (uint32_t m = 0; m < mcount; m++) { 487 const uint8_t *e = n + moff + m * IORT_MAP_BYTES; 488 uint32_t ref = rd32(e + 12); 489 if (ref != s.node_off) { 490 #ifndef CK_IORT_MUTANT_OTHER_BLIND 491 /* A mapping to another SMMUv3 node: remembered so 492 * the requester is reported as behind that SMMU. 493 * A mapping to anything else (an ITS group) is not 494 * a stream at all. */ 495 for (uint32_t k = 0; k < s.nsmmus; k++) { 496 if (s.smmus[k].off != ref) 497 continue; 498 if (s.nother >= CK_IORT_MAX_OTHER) 499 return -1; 500 struct ck_iort_other *o = &s.other[s.nother++]; 501 o->map.segment = seg; 502 o->map.input_base = rd32(e); 503 o->map.id_count = rd32(e + 4); 504 o->map.output_base = rd32(e + 8); 505 o->smmu_base = s.smmus[k].base; 506 o->smmu_off = s.smmus[k].off; 507 break; 508 } 509 #else 510 (void)0; /* TEST mutant: other SMMUs forgotten */ 511 #endif 512 continue; 513 } 514 if (s.nmaps >= CK_IORT_MAX_MAPS) { 515 #ifdef CK_IORT_MUTANT_TRUNCATE 516 continue; /* TEST mutant: drop the extra mappings silently */ 517 #else 518 return -1; 519 #endif 520 } 521 struct ck_iort_map nm = { seg, rd32(e), rd32(e + 4), rd32(e + 8) }; 522 #ifndef CK_IORT_MUTANT_OVERLAP_OK 523 for (uint32_t k = 0; k < s.nmaps; k++) { 524 const struct ck_iort_map *o = &s.map[k]; 525 if (o->segment == seg && 526 (uint64_t)nm.input_base <= (uint64_t)o->input_base + o->id_count && 527 (uint64_t)o->input_base <= (uint64_t)nm.input_base + nm.id_count) 528 return -1; 529 } 530 #endif 531 s.map[s.nmaps++] = nm; 532 } 533 } 534 off += nlen; 535 } 536 if (pass == 0 && !found) 537 return 0; 538 } 539 *out = s; 540 return 1; 541 } 542 543 int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, uint32_t *sid) 544 { 545 for (uint32_t i = 0; i < s->nmaps; i++) { 546 const struct ck_iort_map *m = &s->map[i]; 547 #ifndef CK_IORT_MUTANT_SEGMENT_BLIND 548 if (m->segment != segment) 549 continue; 550 #else 551 (void)segment; /* TEST mutant: the pre-fix segment-blind lookup */ 552 #endif 553 if (rid < m->input_base) 554 continue; 555 uint32_t rel = rid - m->input_base; 556 if (rel > m->id_count || m->output_base + rel < m->output_base) 557 continue; 558 *sid = m->output_base + rel; 559 return 0; 560 } 561 return -1; 562 } 563 564 int ck_iort_other_stream(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, uint32_t *sid, 565 uint64_t *smmu_base) 566 { 567 for (uint32_t i = 0; i < s->nother; i++) { 568 const struct ck_iort_map *m = &s->other[i].map; 569 if (m->segment != segment || rid < m->input_base) 570 continue; 571 uint32_t rel = rid - m->input_base; 572 if (rel > m->id_count || m->output_base + rel < m->output_base) 573 continue; 574 *sid = m->output_base + rel; 575 *smmu_base = s->other[i].smmu_base; 576 return 0; 577 } 578 return -1; 579 } 580 581 uint32_t ck_iort_rmr_for(const struct ck_iort_smmu *s, uint32_t smmu_off, uint32_t sid, struct ck_iort_route *out) 582 { 583 out->nrmr = 0; 584 for (uint32_t i = 0; i < s->nrmr; i++) { 585 const struct ck_iort_rmr *r = &s->rmr[i]; 586 if (r->smmu_index >= s->nsmmus || s->smmus[r->smmu_index].off != smmu_off) 587 continue; 588 if (sid < r->sid_lo || sid > r->sid_hi) 589 continue; 590 if (out->nrmr < CK_IORT_MAX_RMR) 591 out->rmr[out->nrmr++] = r; 592 } 593 return out->nrmr; 594 } 595 596 int ck_iort_route(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, struct ck_iort_route *out) 597 { 598 struct ck_iort_route r = { 0 }; 599 if (ck_iort_stream_id(s, segment, rid, &r.sid) == 0) { 600 r.smmu_index = 0; 601 r.smmu_base = s->base; 602 r.smmu_off = s->node_off; 603 } else { 604 const struct ck_iort_other *hit = 0; 605 for (uint32_t i = 0; i < s->nother && !hit; i++) { 606 const struct ck_iort_map *m = &s->other[i].map; 607 if (m->segment != segment || rid < m->input_base) 608 continue; 609 uint32_t rel = rid - m->input_base; 610 if (rel > m->id_count || m->output_base + rel < m->output_base) 611 continue; 612 hit = &s->other[i]; 613 r.sid = m->output_base + rel; 614 } 615 if (!hit) 616 return -1; 617 uint32_t k; 618 for (k = 0; k < s->nsmmus; k++) 619 if (s->smmus[k].off == hit->smmu_off) 620 break; 621 if (k == s->nsmmus) 622 return -1; /* not reachable after a successful parse */ 623 #ifndef CK_IORT_MUTANT_ROUTE_FIRST 624 r.smmu_index = k; 625 #else 626 r.smmu_index = 0; /* TEST mutant: every requester routed to the first SMMU */ 627 #endif 628 r.smmu_base = s->smmus[r.smmu_index].base; 629 r.smmu_off = s->smmus[r.smmu_index].off; 630 } 631 (void)ck_iort_rmr_for(s, r.smmu_off, r.sid, &r); 632 *out = r; 633 return 0; 634 } 635 636 /* ---- IORT named components (platform devices such as the DGX Spark USB 637 * controllers, ACPI ids NVDA8000/NVDA8001) ---- */ 638 #define IORT_NODE_NAMED 1u 639 #define IORT_NAMED_NAME_OFF 29u /* flags u32 @16, memory properties u64 @20, address size u8 @28, name @29 */ 640 #define IORT_MAP_SINGLE 0x1u 641 642 /* Final segment of an ASCII object path, padded to 4 with '_'; -1 if a 643 * segment is longer than 4 or the path is empty. */ 644 static int last_seg4(const char *p, const char *end, char seg[4]) 645 { 646 const char *s = p; 647 for (const char *q = p; q < end && *q; q++) 648 if (*q == '.' || *q == '\\' || *q == '^') s = q + 1; 649 int i = 0; 650 for (; s < end && *s && i < 4; i++, s++) seg[i] = *s; 651 if (i == 0 || (s < end && *s)) return -1; 652 for (; i < 4; i++) seg[i] = '_'; 653 return 0; 654 } 655 656 int ck_iort_named(const void *iort, const char *name, struct ck_iort_named *out) 657 { 658 const uint8_t *t = iort; 659 uint32_t len = rd32(t + 4), count, at; 660 if (!name || iort_nodes(t, len, &count, &at)) 661 return -1; 662 char want[4]; 663 if (last_seg4(name, name + 64, want)) 664 return -1; 665 int found = 0; 666 struct ck_iort_named r = { 0 }; 667 uint32_t off = at; 668 for (uint32_t i = 0; i < count; i++) { 669 if (off < IORT_TABLE_HDR || off > len || len - off < IORT_NODE_HDR) 670 return -1; 671 const uint8_t *n = t + off; 672 uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; 673 if (nlen < IORT_NODE_HDR || nlen > len - off) 674 return -1; 675 uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); 676 if (mcount && (mcount > nlen / IORT_MAP_BYTES || moff < IORT_NODE_HDR || moff > nlen || 677 (uint64_t)mcount * IORT_MAP_BYTES > (uint64_t)(nlen - moff))) 678 return -1; 679 if (n[0] == IORT_NODE_NAMED) { 680 if (nlen <= IORT_NAMED_NAME_OFF) 681 return -1; 682 const char *nm = (const char *)(n + IORT_NAMED_NAME_OFF); 683 const char *nend = (const char *)(n + (mcount ? moff : nlen)); 684 if (nend <= nm) 685 return -1; 686 char seg[4]; 687 if (last_seg4(nm, nend, seg) == 0 && seg[0] == want[0] && seg[1] == want[1] && seg[2] == want[2] && 688 seg[3] == want[3]) { 689 if (found) 690 return -2; /* two named components carry this name: refuse */ 691 found = 1; 692 r.node_off = off; 693 uint32_t k = 0; 694 for (; k < sizeof r.name - 1 && nm + k < nend && nm[k]; k++) 695 r.name[k] = nm[k]; 696 r.name[k] = 0; 697 r.nmaps = mcount; 698 /* The stream: a single mapping, else a mapping of exactly one id. */ 699 r.target_off = 0; 700 for (uint32_t m = 0; m < mcount; m++) { 701 const uint8_t *e = n + moff + m * IORT_MAP_BYTES; 702 uint32_t ref = rd32(e + 12); 703 if (ref < IORT_TABLE_HDR || ref > len || len - ref < IORT_NODE_HDR) 704 return -1; 705 if (t[ref] != IORT_NODE_SMMUV3) 706 continue; 707 if ((rd32(e + 16) & IORT_MAP_SINGLE) || rd32(e + 4) == 0) { 708 r.stream_id = rd32(e + 8); 709 r.target_off = ref; 710 uint32_t tlen = (uint32_t)t[ref + 1] | (uint32_t)t[ref + 2] << 8; 711 r.target_base = tlen >= 24 && tlen <= len - ref ? rd64(t + ref + 16) : 0; 712 break; 713 } 714 } 715 } 716 } 717 off += nlen; 718 } 719 if (!found) 720 return 0; 721 *out = r; 722 return 1; 723 }