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 / acpi.h
| 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.h -- ACPI table lookup and the MADT/SPCR/FADT fields the core uses. |
| 18 | * Pure functions over memory (identity mapped in the kernel, host buffers in |
| 19 | * the tests). Every table is length- and checksum-checked before use. */ |
| 20 | #ifndef AIENOS_CK_ACPI_H |
| 21 | #define AIENOS_CK_ACPI_H |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #define CK_ACPI_SDT_HEADER 36u |
| 27 | |
| 28 | /* Sum of bytes is zero. */ |
| 29 | int ck_acpi_sum_ok(const void *p, size_t len); |
| 30 | |
| 31 | /* Validates an ACPI 2.0+ RSDP (signature, 20-byte and extended checksum) |
| 32 | * and returns the XSDT address it names, or 0. Revision 0 RSDPs return the |
| 33 | * RSDT address with *is_xsdt = 0. */ |
| 34 | uint64_t ck_acpi_root(uint64_t rsdp, int *is_xsdt); |
| 35 | |
| 36 | /* First table with this signature listed in the XSDT/RSDT that passes the |
| 37 | * length and checksum checks; NULL if none. */ |
| 38 | const void *ck_acpi_lookup(uint64_t rsdp, const char sig[4]); |
| 39 | |
| 40 | /* Calls fn(table, len, ctx) for every table the root lists (no checksum |
| 41 | * filter; used to map the tables). Returns the number listed, -1 if the |
| 42 | * RSDP/root is invalid. */ |
| 43 | int ck_acpi_each(uint64_t rsdp, void (*fn)(uint64_t table, uint32_t len, void *ctx), void *ctx); |
| 44 | |
| 45 | /* Calls fn(lo, hi, ctx) with the byte span [lo, hi) of the RSDP (its checked |
| 46 | * length), the XSDT/RSDT and every table the root lists (at least the 36-byte |
| 47 | * header each, whatever their checksums). Used to keep all of them out of the |
| 48 | * frame allocator; the caller rounds to pages. Returns the number of spans, |
| 49 | * -1 if the RSDP is invalid (nothing reported). The root is reported even |
| 50 | * when its own checksum fails; its entries are then not trusted. */ |
| 51 | int ck_acpi_spans(uint64_t rsdp, void (*fn)(uint64_t lo, uint64_t hi, void *ctx), void *ctx); |
| 52 | |
| 53 | struct ck_madt_gic { |
| 54 | uint64_t gicd; /* GICD base (type 0x0C) */ |
| 55 | uint32_t gic_version; /* GICD entry field, 0 = "discover from hardware" */ |
| 56 | uint64_t gicr; /* first GICR discovery range (type 0x0E) */ |
| 57 | uint32_t gicr_len; |
| 58 | uint32_t gicc_count; /* enabled or online-capable GICC entries */ |
| 59 | uint64_t gicc_gicr; /* GICR base from the first GICC, if no 0x0E range */ |
| 60 | uint64_t gicc_base; /* GICv2 CPU interface (Physical Base Address) from the first GICC */ |
| 61 | }; |
| 62 | /* 0 on success, -1 on a malformed table (bad entry length, truncation). */ |
| 63 | int ck_madt_parse(const void *madt, struct ck_madt_gic *out); |
| 64 | /* Which GIC the parsed MADT describes: 2 (GICv2, MMIO CPU interface at |
| 65 | * gicc_base), 3 (GICv3/v4, redistributors), 0 (unusable: GICv1, or v2 with no |
| 66 | * CPU interface). GICD version 0 ("discover") is GICv3 when a redistributor is |
| 67 | * named anywhere, else GICv2 when a GICC base is given. */ |
| 68 | int ck_madt_gic_kind(const struct ck_madt_gic *m); |
| 69 | |
| 70 | /* CPU topology from the MADT GICC entries (acpi.rs madt_cpu_topology): enabled |
| 71 | * or online-capable cores, (efficiency class, count) pairs sorted by class, |
| 72 | * cores whose entry is too short (< 77 bytes) to carry a class. Pure parse; |
| 73 | * nothing here starts a core. 0 ok, -1 malformed. */ |
| 74 | #define CK_MAX_CLASSES 8 |
| 75 | struct ck_cpu_topology { |
| 76 | uint32_t cores; |
| 77 | uint8_t class_id[CK_MAX_CLASSES]; |
| 78 | uint32_t class_count[CK_MAX_CLASSES]; |
| 79 | unsigned distinct_classes; |
| 80 | uint32_t unknown_class; |
| 81 | int has_first_mpidr; |
| 82 | uint64_t first_mpidr; |
| 83 | }; |
| 84 | int ck_madt_cpu_topology(const void *madt, struct ck_cpu_topology *out); |
| 85 | /* MPIDR affinity fields of every enabled or online-capable GICC entry (offset |
| 86 | * 68, masked to Aff3 [39:32] and Aff2..Aff0 [23:0]), in table order, for the |
| 87 | * SMP bring-up (core/smp.c). *n gets the number of such entries even when it |
| 88 | * exceeds max (only the first max are stored). 0 ok, -1 malformed (bad entry |
| 89 | * length, truncation, a GICC entry shorter than 76 bytes), -2 more than max. */ |
| 90 | #define CK_MPIDR_AFF_MASK 0xff00ffffffull |
| 91 | int ck_madt_mpidrs(const void *madt, uint64_t *out, unsigned max, unsigned *n); |
| 92 | /* thread.rs place_task: task_index modulo the classified cores, walked class |
| 93 | * by class. 0 and (class, index within class), or -1 if no core is classified. */ |
| 94 | int ck_place_task(const struct ck_cpu_topology *t, uint32_t task_index, uint8_t *class_id, |
| 95 | uint32_t *core); |
| 96 | |
| 97 | struct ck_spcr { |
| 98 | uint8_t interface_type; /* 0x03 PL011, 0x0d/0x0e SBSA, 0x00/0x01/0x12 16550 (core/uart_rx.h) */ |
| 99 | uint8_t space; /* GAS address space: 0 = system memory */ |
| 100 | uint8_t bit_width; |
| 101 | uint8_t access_size; /* GAS access size: 1 byte .. 4 qword */ |
| 102 | uint64_t base; |
| 103 | }; |
| 104 | int ck_spcr_parse(const void *spcr, struct ck_spcr *out); |
| 105 | |
| 106 | /* FADT ARM_BOOT_ARCH flags: bit 0 PSCI compliant, bit 1 PSCI uses HVC. |
| 107 | * -1 if the FADT is too short to carry the field. */ |
| 108 | int ck_fadt_arm_boot_arch(const void *fadt, uint16_t *flags); |
| 109 | |
| 110 | /* IORT (Arm IO Remapping Table): the first SMMUv3 node and the PCI root |
| 111 | * complex ID mappings that point at it (ported from the Rust |
| 112 | * aienos-kernel acpi::iort_smmuv3). Each mapping keeps the PCI segment of |
| 113 | * its root complex: a requester id (bus<<8 | dev<<3 | fn) only means |
| 114 | * something inside one segment, so a lookup is (segment, rid). |
| 115 | * The mapping array is sized by the table: a parse counts the mappings |
| 116 | * that point at the SMMU and refuses the whole table (-1) when there are |
| 117 | * more than CK_IORT_MAX_MAPS. 32 covers the DGX Spark (MEASURED: 15 root |
| 118 | * complexes, one mapping each, to the first SMMUv3) with headroom. */ |
| 119 | #define CK_IORT_MAX_MAPS 32 |
| 120 | struct ck_iort_map { |
| 121 | uint32_t segment; /* PCI segment number of the root complex */ |
| 122 | uint32_t input_base, id_count, output_base; /* id_count = number of IDs - 1 */ |
| 123 | }; |
| 124 | /* Root complex mappings that target an SMMUv3 node other than the first |
| 125 | * (aienos#286 cut B5/B6: on the DGX Spark, MEASURED 2026-10-10, segment 15, |
| 126 | * the GB10's, maps to the second SMMUv3 at 0x13000000). The kernel does not |
| 127 | * drive those SMMUs; it records the mapping so a requester behind one is |
| 128 | * reported as CK_SMMU_OTHER (base and stream named) instead of "no stream". |
| 129 | * CK_IORT_MAX_SMMUS bounds the SMMUv3 nodes remembered in a parse (Spark: |
| 130 | * 3) and CK_IORT_MAX_OTHER the mappings to them (Spark: 1). */ |
| 131 | #define CK_IORT_MAX_SMMUS 8 |
| 132 | #define CK_IORT_MAX_OTHER 8 |
| 133 | struct ck_iort_other { |
| 134 | struct ck_iort_map map; |
| 135 | uint64_t smmu_base; /* register base of the SMMUv3 node the mapping targets */ |
| 136 | uint32_t smmu_off; /* offset of that node in the table */ |
| 137 | }; |
| 138 | /* Every SMMUv3 node of the table in table order (aienos#286 cut B7a): index |
| 139 | * 0 is the first node (`base`, `node_off` above repeat it); the kernel may |
| 140 | * bring up any of them (core/smmu_svc.c keeps one instance per entry). */ |
| 141 | struct ck_iort_node { |
| 142 | uint64_t base; |
| 143 | uint32_t off; |
| 144 | }; |
| 145 | /* IORT RMR node (type 6, reserved memory ranges; ACPICA actbl2.h |
| 146 | * acpi_iort_rmr: flags u32 @16, rmr_count u32 @20, rmr_offset u32 @24; |
| 147 | * acpi_iort_rmr_desc: base_address u64, length u64, reserved u32, 20 bytes |
| 148 | * each). One entry per ID mapping of the node: the mapping names the SMMUv3 |
| 149 | * and the inclusive StreamID range (output_base .. output_base + id_count, |
| 150 | * as Linux iort.c iort_rmr_alloc_sids reads it) whose transactions the |
| 151 | * firmware expects to reach these ranges untranslated. flags: bit 0 remap |
| 152 | * permitted, bit 1 privileged, bits [9:2] access attributes (ACPICA |
| 153 | * ACPI_IORT_RMR_ATTR_*: 4 = Normal Non-cacheable, 5 = Normal IWB-OWB, 0..3 |
| 154 | * Device). On the DGX Spark (MEASURED 2026-10-10, docs/GB10_IORT_DECODE.md) |
| 155 | * one RMR node, flags 0x10 (Normal NC, remap not permitted), streams |
| 156 | * 0x0..=0x100 of the second SMMUv3 (0x13000000), three ranges: |
| 157 | * 0x280000000+2 GiB, 0x300000000+48 MiB, 0xa1600000+386 MiB. */ |
| 158 | #define CK_IORT_MAX_RMR 4 /* RMR (node, mapping) pairs remembered (Spark: 1) */ |
| 159 | #define CK_IORT_MAX_RMR_RANGES 4 /* ranges per RMR node (Spark: 3) */ |
| 160 | #define CK_IORT_RMR_REMAP_PERMITTED 1u |
| 161 | #define CK_IORT_RMR_PRIVILEGED 2u |
| 162 | #define CK_IORT_RMR_ATTR(flags) (((flags) >> 2) & 0xffu) |
| 163 | #define CK_IORT_RMR_ATTR_NORMAL_NC 4u |
| 164 | #define CK_IORT_RMR_ATTR_NORMAL_IWB_OWB 5u |
| 165 | struct ck_iort_rmr_range { |
| 166 | uint64_t base, len; |
| 167 | }; |
| 168 | struct ck_iort_rmr { |
| 169 | uint32_t node_off; /* the RMR node */ |
| 170 | uint32_t smmu_index; /* index into ck_iort_smmu.smmus */ |
| 171 | uint32_t sid_lo, sid_hi; /* inclusive StreamID range on that SMMU */ |
| 172 | uint32_t flags; |
| 173 | uint32_t nranges; |
| 174 | struct ck_iort_rmr_range range[CK_IORT_MAX_RMR_RANGES]; |
| 175 | }; |
| 176 | struct ck_iort_smmu { |
| 177 | uint64_t base; /* SMMUv3 register base (= smmus[0].base) */ |
| 178 | uint32_t node_off; /* offset of the SMMUv3 node in the table (= smmus[0].off) */ |
| 179 | uint32_t nmaps; |
| 180 | struct ck_iort_map map[CK_IORT_MAX_MAPS]; |
| 181 | uint32_t nother; |
| 182 | struct ck_iort_other other[CK_IORT_MAX_OTHER]; |
| 183 | uint32_t nsmmus; |
| 184 | struct ck_iort_node smmus[CK_IORT_MAX_SMMUS]; |
| 185 | uint32_t nrmr; |
| 186 | struct ck_iort_rmr rmr[CK_IORT_MAX_RMR]; |
| 187 | }; |
| 188 | /* 1 found, 0 no SMMUv3 node, -1 malformed (bad signature, truncation, a |
| 189 | * node or mapping array outside the table, base 0, a root complex node |
| 190 | * too short to carry its segment number, more than CK_IORT_MAX_MAPS |
| 191 | * mappings to the SMMU, more than CK_IORT_MAX_SMMUS SMMUv3 nodes or |
| 192 | * CK_IORT_MAX_OTHER mappings to the other SMMUs, or two mappings to the |
| 193 | * SMMU whose input ranges overlap in the same segment: an ambiguous |
| 194 | * requester id is refused, not resolved by table order). A mapping to a |
| 195 | * node that is not an SMMUv3 (an ITS group) is neither a stream nor an |
| 196 | * "other" entry. */ |
| 197 | int ck_iort_parse(const void *iort, struct ck_iort_smmu *out); |
| 198 | /* Stream ID for PCI requester id `rid` (bus<<8 | dev<<3 | fn) in PCI |
| 199 | * segment `segment`; 0 ok, -1 if no mapping of that segment covers it. A |
| 200 | * mapping of another segment never matches. */ |
| 201 | int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, uint32_t *sid); |
| 202 | /* Same lookup over the mappings to SMMUs this kernel does not drive: 0 and |
| 203 | * the stream id plus that SMMU's register base when (segment, rid) is |
| 204 | * covered by one of them, -1 otherwise. Never consulted for granting a |
| 205 | * window; it only names what refuses the requester. */ |
| 206 | int ck_iort_other_stream(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, uint32_t *sid, |
| 207 | uint64_t *smmu_base); |
| 208 | /* Route for PCI requester (segment, rid) (aienos#286 cut B7a): which SMMUv3 |
| 209 | * node (index into s->smmus, base, offset) and StreamID it reaches, through |
| 210 | * the first-node mappings or the other-SMMU mappings, plus every RMR entry |
| 211 | * that covers that stream on that SMMU (pointers into s). 0 found, -1 no |
| 212 | * mapping covers the requester. Pure: reads the parse only. */ |
| 213 | struct ck_iort_route { |
| 214 | uint32_t smmu_index; |
| 215 | uint64_t smmu_base; |
| 216 | uint32_t smmu_off; |
| 217 | uint32_t sid; |
| 218 | uint32_t nrmr; |
| 219 | const struct ck_iort_rmr *rmr[CK_IORT_MAX_RMR]; |
| 220 | }; |
| 221 | int ck_iort_route(const struct ck_iort_smmu *s, uint32_t segment, uint32_t rid, struct ck_iort_route *out); |
| 222 | /* RMR entries covering stream `sid` of the SMMUv3 node at table offset |
| 223 | * `smmu_off` (used for named components, whose target the IORT names by |
| 224 | * node). Fills out->rmr/nrmr only (and smmu_index when the node is known); |
| 225 | * returns that count. */ |
| 226 | uint32_t ck_iort_rmr_for(const struct ck_iort_smmu *s, uint32_t smmu_off, uint32_t sid, struct ck_iort_route *out); |
| 227 | |
| 228 | /* IORT named component (node type 1) whose device object name has the same |
| 229 | * final segment as `name` ("USB0" matches "\\_SB_.USB0"). out gets the node |
| 230 | * name, the stream id of its single (or one-id) mapping to an SMMUv3 node, |
| 231 | * and that SMMUv3 node (offset, register base). target_off == 0: no such |
| 232 | * mapping (no stream for this device). Returns 1 found, 0 no such node, -1 |
| 233 | * malformed (signature, truncation, node, name or mapping outside the |
| 234 | * table), -2 ambiguous (two named components with that final segment). */ |
| 235 | struct ck_iort_named { |
| 236 | char name[48]; |
| 237 | uint32_t node_off, nmaps; |
| 238 | uint32_t stream_id; |
| 239 | uint32_t target_off; /* SMMUv3 node the stream belongs to, 0 if none */ |
| 240 | uint64_t target_base; |
| 241 | }; |
| 242 | int ck_iort_named(const void *iort, const char *name, struct ck_iort_named *out); |
| 243 | |
| 244 | #endif |