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/tools/ck_acpi_scan.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 / tools / ck_acpi_scan.c` 345 lines ยท 16048 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 /* ck_acpi_scan.c -- host tool (never part of an image): runs the kernel's 18 * platform xHCI discovery over firmware tables dumped under Linux and 19 * prints the console lines the kernel will print on that machine. 20 * 21 * ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...] 22 * 23 * Tables are given in the order the kernel visits them: the DSDT named by 24 * the FADT first, then every SSDT in XSDT order. The scan is the kernel's 25 * own code (core/acpi_dev.c, core/acpi.c) and the lines come from the same 26 * format strings (dev/xhci_acpi_fmt.h). 27 * 28 * Output, one line each: 29 * plain lines the kernel prints them byte for byte (they depend only on 30 * the tables); 31 * "? " lines the kernel prints a line here whose values come from the 32 * hardware (controller registers, the SMMU, DMA addresses); 33 * shown with the part that is not known from tables; 34 * "# " lines tool notes: IORT nodes and stream mappings, assumptions. 35 * The per-controller lines assume no PCI xHCI (measured on the DGX Spark: 36 * no PCI class 0x0c03 device) and that every controller is tried, i.e. no 37 * keyboard is found on an earlier one (the kernel stops at the first 38 * controller whose keyboard phase succeeds). Exit 0, or 2 on a usage or 39 * read error. */ 40 #include 41 #include 42 #include 43 #include 44 45 #include "acpi.h" 46 #include "acpi_dev.h" 47 #include "xhci_acpi_fmt.h" 48 49 #define FILE_MAX (4u << 20) 50 #define TABLES_MAX 32 51 52 struct table { 53 const char *path; 54 uint8_t *data; 55 size_t len; 56 }; 57 58 /* Same fields as struct ck_acpi_scan_info in include/ck.h (kernel only). */ 59 struct ck_acpi_scan_info { 60 unsigned tables, refused, devices; 61 int first_refusal; 62 }; 63 64 struct plat { 65 struct ck_acpi_device d; 66 char table[5]; 67 }; 68 69 static uint8_t *read_file(const char *path, size_t *len) 70 { 71 FILE *f = fopen(path, "rb"); 72 if (!f) { 73 fprintf(stderr, "ck_acpi_scan: cannot open %s\n", path); 74 return 0; 75 } 76 uint8_t *b = malloc(FILE_MAX); 77 size_t n = b ? fread(b, 1, FILE_MAX, f) : 0; 78 int more = b && fgetc(f) != EOF; 79 fclose(f); 80 if (!b || more) { 81 fprintf(stderr, "ck_acpi_scan: %s missing or larger than %u bytes\n", path, FILE_MAX); 82 free(b); 83 return 0; 84 } 85 *len = n; 86 return b; 87 } 88 89 static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } 90 static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; } 91 92 /* Mirrors core/acpi_platform.c scan_block over every table. */ 93 static int scan_all(struct table *t, int nt, const char *const *ids, unsigned nids, struct plat *out, unsigned max, 94 struct ck_acpi_scan_info *info) 95 { 96 int n = 0; 97 memset(info, 0, sizeof *info); 98 for (int i = 0; i < nt; i++) { 99 if (t[i].len < 36) { 100 info->refused++; 101 if (!info->first_refusal) info->first_refusal = CK_AML_E_LEN; 102 continue; 103 } 104 info->tables++; 105 uint32_t len = rd32(t[i].data + 4); 106 size_t avail = len >= 36 && len <= t[i].len ? len : 36; 107 struct ck_acpi_device d[CK_XHCI_PLAT_MAX]; 108 unsigned seen = 0; 109 int rc = ck_aml_scan(t[i].data, avail, ids, nids, d, CK_XHCI_PLAT_MAX, &seen); 110 if (rc < 0) { 111 info->refused++; 112 if (!info->first_refusal) info->first_refusal = rc; 113 continue; 114 } 115 info->devices += seen; 116 for (int k = 0; k < rc; k++) { 117 if (k < (int)CK_XHCI_PLAT_MAX && n < (int)max && out) { 118 out[n].d = d[k]; 119 memcpy(out[n].table, t[i].data, 4); 120 out[n].table[4] = 0; 121 } 122 n++; 123 } 124 } 125 return n; 126 } 127 128 static void iort_notes(const uint8_t *t) 129 { 130 uint32_t len = rd32(t + 4), count = rd32(t + 36), off = rd32(t + 40); 131 printf("# iort: length=%u nodes=%u\n", len, count); 132 for (uint32_t i = 0; i < count && off >= 48 && off < len && len - off >= 16; i++) { 133 const uint8_t *n = t + off; 134 uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; 135 if (nlen < 16 || nlen > len - off) break; 136 uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); 137 if (n[0] == 4 && nlen >= 24) 138 printf("# iort: node@0x%x smmuv3 base=0x%llx\n", off, (unsigned long long)rd64(n + 16)); 139 else if (n[0] == 6 && nlen >= 28) { 140 uint32_t flags = rd32(n + 16), rcount = rd32(n + 20), roff = rd32(n + 24); 141 printf("# iort: node@0x%x rmr flags=0x%x (attr=%u remap=%u priv=%u) ranges=%u maps=%u\n", off, flags, 142 (flags >> 2) & 0xff, flags & 1, (flags >> 1) & 1, rcount, mcount); 143 for (uint32_t r = 0; r < rcount && roff + (r + 1) * 20 <= nlen; r++) 144 printf("# reserved base=0x%llx len=0x%llx\n", (unsigned long long)rd64(n + roff + r * 20), 145 (unsigned long long)rd64(n + roff + r * 20 + 8)); 146 } 147 else if (n[0] == 1 && nlen > 29) { 148 char name[64]; 149 uint32_t end = mcount ? moff : nlen, k = 0; 150 for (; k + 1 < sizeof name && 29 + k < end && n[29 + k]; k++) name[k] = (char)n[29 + k]; 151 name[k] = 0; 152 printf("# iort: node@0x%x named %s maps=%u\n", off, name, mcount); 153 } else 154 printf("# iort: node@0x%x type=%u maps=%u\n", off, n[0], mcount); 155 for (uint32_t m = 0; m < mcount && moff + (m + 1) * 20 <= nlen; m++) { 156 const uint8_t *e = n + moff + m * 20; 157 uint32_t ref = rd32(e + 12); 158 unsigned long long tb = ref + 24 <= len && t[ref] == 4 ? (unsigned long long)rd64(t + ref + 16) : 0; 159 printf("# map in=0x%x count=%u out=0x%x ref=0x%x flags=0x%x smmu=0x%llx\n", rd32(e), rd32(e + 4), 160 rd32(e + 8), ref, rd32(e + 16), tb); 161 } 162 off += nlen; 163 } 164 } 165 166 /* Static PCI route inventory only. Stream-table geometry is selected from 167 * each hardware instance's ID registers; it is not a parser limit. */ 168 static void parse_notes(const uint8_t *t) 169 { 170 uint32_t len = rd32(t + 4), count = rd32(t + 36), off = rd32(t + 40), first = 0, rc_maps = 0, max_out = 0; 171 for (int pass = 0; pass < 2; pass++) { 172 uint32_t o = off; 173 for (uint32_t i = 0; i < count && o >= 48 && o < len && len - o >= 16; i++) { 174 const uint8_t *n = t + o; 175 uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; 176 if (nlen < 16 || nlen > len - o) break; 177 if (pass == 0 && n[0] == 4 && !first) first = o; 178 uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); 179 if (pass == 1 && n[0] == 2) 180 for (uint32_t m = 0; m < mcount && moff + (m + 1) * 20 <= nlen; m++) { 181 const uint8_t *e = n + moff + m * 20; 182 if (rd32(e + 12) != first) continue; 183 rc_maps++; 184 uint32_t top = rd32(e + 8) + rd32(e + 4); 185 if (top > max_out) max_out = top; 186 } 187 o += nlen; 188 } 189 } 190 printf("# smmu: %u PCI root-complex mapping(s) to the first SMMUv3 (parser limit CK_IORT_MAX_MAPS=%u), highest stream id 0x%x; hardware stream-table geometry NOT_RUN\n", 191 rc_maps, (unsigned)CK_IORT_MAX_MAPS, max_out); 192 } 193 194 static int last_seg_same(const char *a, const char *b) { return ck_aml_last_seg_eq(a, b); } 195 196 int main(int argc, char **argv) 197 { 198 const char *iort_path = 0; 199 struct table t[TABLES_MAX]; 200 int nt = 0; 201 for (int i = 1; i < argc; i++) { 202 if (!strcmp(argv[i], "--iort") && i + 1 < argc) { 203 iort_path = argv[++i]; 204 continue; 205 } 206 if (argv[i][0] == '-' || nt == TABLES_MAX) { 207 fprintf(stderr, "usage: ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...]\n"); 208 return 2; 209 } 210 t[nt].path = argv[i]; 211 if (!(t[nt].data = read_file(argv[i], &t[nt].len))) return 2; 212 nt++; 213 } 214 if (!nt) { 215 fprintf(stderr, "usage: ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...]\n"); 216 return 2; 217 } 218 uint8_t *iort = 0; 219 size_t iort_len = 0; 220 if (iort_path && !(iort = read_file(iort_path, &iort_len))) return 2; 221 if (iort && (iort_len < 48 || rd32(iort + 4) > iort_len)) { 222 fprintf(stderr, "ck_acpi_scan: %s is shorter than its header length\n", iort_path); 223 return 2; 224 } 225 226 /* ---- discovery (dev/xhci_fence.c acpi_discover) ---- */ 227 static const char *const ids[] = CK_XHCI_ACPI_IDS; 228 static const char *const ctl[] = { CK_ACPI_CONTROL_ID }; 229 struct ck_acpi_scan_info info; 230 struct plat c, p[CK_XHCI_PLAT_MAX]; 231 int nc = scan_all(t, nt, ctl, 1, &c, 1, &info); 232 printf(CK_ACPI_SCAN_FMT, info.tables, info.refused, info.first_refusal, info.devices); 233 if (nc > 0) 234 printf(CK_ACPI_CONTROL_FMT, c.d.hid, c.d.path, (unsigned long long)c.d.mmio_base, (unsigned long long)c.d.mmio_len); 235 else 236 printf(CK_ACPI_CONTROL_NONE); 237 int n = scan_all(t, nt, ids, CK_XHCI_ACPI_NIDS, p, CK_XHCI_PLAT_MAX, &info); 238 if (n > (int)CK_XHCI_PLAT_MAX) n = (int)CK_XHCI_PLAT_MAX; 239 for (int i = 0; i < n; i++) 240 printf(CK_XHCI_ACPI_FMT, p[i].d.path, p[i].d.hid[0] ? p[i].d.hid : "-", p[i].d.cid[0] ? p[i].d.cid : "-", 241 p[i].table, (unsigned long long)p[i].d.mmio_base, (unsigned long long)p[i].d.mmio_len); 242 printf(CK_XHCI_ACPI_COUNT_FMT, n); 243 244 /* ---- IORT (core/acpi.c, core/smmu_svc.c) ---- */ 245 struct ck_iort_smmu first; 246 int prc = 0; 247 if (iort) { 248 iort_notes(iort); 249 prc = ck_iort_parse(iort, &first); 250 if (prc == 1) { 251 printf("# smmu: %u SMMUv3 node(s), each brought up by its first confined stream (cut B7a); first: node@0x%x base=0x%llx (pci maps=%u, maps to other SMMUs=%u, rmr nodes=%u)\n", 252 first.nsmmus, first.node_off, (unsigned long long)first.base, first.nmaps, first.nother, first.nrmr); 253 for (uint32_t i = 1; i < first.nsmmus; i++) 254 printf("# smmu: instance %u: node@0x%x base=0x%llx\n", i, first.smmus[i].off, 255 (unsigned long long)first.smmus[i].base); 256 for (uint32_t i = 0; i < first.nother; i++) { 257 struct ck_iort_route rt; 258 int rrc = ck_iort_route(&first, first.other[i].map.segment, first.other[i].map.input_base, &rt); 259 printf("# smmu: segment %u rid 0x%x..0x%x -> stream 0x%x.. on SMMUv3 node@0x%x base=0x%llx (route: instance %d, reserved ranges for the first rid: %u)\n", 260 first.other[i].map.segment, first.other[i].map.input_base, 261 first.other[i].map.input_base + first.other[i].map.id_count, first.other[i].map.output_base, 262 first.other[i].smmu_off, (unsigned long long)first.other[i].smmu_base, 263 rrc == 0 ? (int)rt.smmu_index : -1, rrc == 0 ? rt.nrmr : 0u); 264 } 265 for (uint32_t i = 0; i < first.nrmr; i++) { 266 uint32_t attr = CK_IORT_RMR_ATTR(first.rmr[i].flags); 267 printf("# smmu: rmr node@0x%x: streams 0x%x..0x%x of instance %u, %u range(s), attr %u (%s)\n", 268 first.rmr[i].node_off, first.rmr[i].sid_lo, first.rmr[i].sid_hi, first.rmr[i].smmu_index, 269 first.rmr[i].nranges, attr, 270 attr == CK_IORT_RMR_ATTR_NORMAL_NC || attr == CK_IORT_RMR_ATTR_NORMAL_IWB_OWB 271 ? "identity-mapped beside a confined window" 272 : "unsupported: confinement refused"); 273 } 274 } else 275 printf("# smmu: ck_iort_parse=%d (%s)\n", prc, prc == 0 ? "no SMMUv3 node" : "malformed"); 276 parse_notes(iort); 277 } else 278 printf("# smmu: no IORT given; dma_gate lines below assume none (NoSmmu)\n"); 279 280 /* ---- fence (dev/xhci_fence.c ck_xhci_fence, plat_fence_one) ---- */ 281 printf("keyboard: no PCI xHCI controller; trying %d ACPI platform controller(s)\n", n); 282 if (n == 0) printf("keyboard: unavailable (no xHCI controller)\n"); 283 int fenced = 0, smmu_said = 0; 284 for (int i = 0; i < n; i++) { 285 const char *nm = p[i].d.path; 286 printf(CK_XHCI_PLAT_TRY_FMT, nm, (unsigned long long)p[i].d.mmio_base, (unsigned long long)p[i].d.mmio_len); 287 int amb = 0; 288 for (int j = 0; j < n; j++) 289 if (j != i && last_seg_same(p[j].d.path, nm)) amb = 1; 290 if (amb) { 291 printf(CK_XHCI_PLAT_SKIP_FMT, nm, "name not unique in DSDT/SSDT"); 292 continue; 293 } 294 if (!p[i].d.mmio_base || p[i].d.mmio_len < 0x1000u || (p[i].d.mmio_base & 0xfffu)) { 295 printf(CK_XHCI_PLAT_SKIP_FMT, nm, "no usable _CRS memory range"); 296 continue; 297 } 298 printf("? xhci: %s caplength= hciversion= (platform)\n", nm); 299 printf("? xhci: halted before gate usbcmd= usbsts= halted=\n"); 300 const char *deny = 0; 301 if (!iort || prc == 0) 302 deny = "NoSmmu"; 303 else if (prc < 0) { 304 if (!smmu_said++) /* bring_up caches its state: once per boot, by the first DMA grant request (may be an earlier device) */ 305 printf("? smmu: IORT malformed, SMMU unusable (fail closed) [once per boot, at the first DMA grant request]\n"); 306 deny = "SmmuNotReady"; 307 } 308 if (deny) { 309 printf(CK_XHCI_PLAT_DENY_FMT, nm, deny); 310 if (iort) { 311 struct ck_iort_named w; 312 int wrc = ck_iort_named(iort, nm, &w); 313 if (wrc == 1 && w.target_off) 314 printf("# %s: IORT named component %s stream 0x%x on the SMMUv3 at 0x%llx%s\n", nm, w.name, 315 w.stream_id, (unsigned long long)w.target_base, 316 prc != 1 ? " (the first SMMUv3 node; granted only once the kernel can bring it up)" : ""); 317 else 318 printf("# %s: no IORT stream (ck_iort_named=%d)\n", nm, wrc); 319 } 320 continue; 321 } 322 struct ck_iort_named nc2; 323 int nrc = ck_iort_named(iort, nm, &nc2); 324 if (nrc != 1 || !nc2.target_off) { 325 printf(CK_XHCI_PLAT_DENY_FMT, nm, nrc == -2 ? "Ambiguous" : "NoStream"); 326 continue; 327 } 328 uint32_t si; 329 for (si = 0; si < first.nsmmus; si++) 330 if (first.smmus[si].off == nc2.target_off) break; 331 if (si == first.nsmmus) { 332 printf(CK_XHCI_PLAT_DENY_FMT, nm, "NoStream"); 333 continue; 334 } 335 printf("# route: instance %u at 0x%llx; grant below CONDITIONAL on hardware bring-up, supported stream width and confinement (NOT_RUN)\n", 336 si, (unsigned long long)nc2.target_base); 337 printf("? " CK_XHCI_PLAT_GRANT_FMT, nm, (unsigned long long)nc2.target_base, nc2.stream_id); 338 printf("? smmu_dma_window: xhci %s only, translation active iova= len=\n", nm); 339 printf("? keyboard phase on %s, then release: halt, reset, stream 0x%x back to abort\n", nm, nc2.stream_id); 340 fenced++; 341 } 342 if (n > 0) printf("keyboard: unavailable (no keyboard on any platform xHCI)\n"); 343 printf("# %d controller(s) would reach the keyboard phase\n", fenced); 344 return 0; 345 }