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_platform.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 | /* acpi_platform.c -- ck_acpi_platform_devices (ck.h): the DSDT named by the |
| 18 | * FADT plus every SSDT the XSDT lists, each scanned by core/acpi_dev.c. |
| 19 | * Kernel only; the scan itself is host tested (tests/test_acpi_dev.c) and |
| 20 | * run over real firmware tables by tools/ck_acpi_scan.c. */ |
| 21 | #include "ck_internal.h" |
| 22 | #include "acpi.h" |
| 23 | #include "acpi_dev.h" |
| 24 | |
| 25 | #define SCAN_MAX 8u |
| 26 | |
| 27 | struct scan_ctx { |
| 28 | const char *const *ids; |
| 29 | unsigned nids, max; |
| 30 | struct ck_platform_dev *out; |
| 31 | struct ck_acpi_scan_info info; |
| 32 | int n; |
| 33 | }; |
| 34 | |
| 35 | 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; } |
| 36 | |
| 37 | static void copy(char *dst, size_t cap, const char *src) |
| 38 | { |
| 39 | size_t i = 0; |
| 40 | for (; i + 1 < cap && src[i]; i++) dst[i] = src[i]; |
| 41 | dst[i] = 0; |
| 42 | } |
| 43 | |
| 44 | static void scan_block(struct scan_ctx *c, uint64_t pa) |
| 45 | { |
| 46 | if (!pa || !ck_mm_mapped(pa, 36)) { |
| 47 | c->info.refused++; |
| 48 | if (!c->info.first_refusal) c->info.first_refusal = CK_AML_E_LEN; |
| 49 | return; |
| 50 | } |
| 51 | const uint8_t *t = (const uint8_t *)(uintptr_t)pa; |
| 52 | uint32_t len = rd32(t + 4); |
| 53 | c->info.tables++; |
| 54 | /* avail is what is mapped: a length that runs past the mapping is a |
| 55 | * truncated table and is refused by the length check. */ |
| 56 | size_t avail = len >= 36 && ck_mm_mapped(pa, len) ? len : 36; |
| 57 | struct ck_acpi_device d[SCAN_MAX]; |
| 58 | unsigned seen = 0; |
| 59 | int rc = ck_aml_scan(t, avail, c->ids, c->nids, d, SCAN_MAX, &seen); |
| 60 | if (rc < 0) { |
| 61 | c->info.refused++; |
| 62 | if (!c->info.first_refusal) c->info.first_refusal = rc; |
| 63 | return; |
| 64 | } |
| 65 | c->info.devices += seen; |
| 66 | for (int i = 0; i < rc; i++) { |
| 67 | if (i < (int)SCAN_MAX && c->n < (int)c->max && c->out) { |
| 68 | struct ck_platform_dev *o = &c->out[c->n]; |
| 69 | copy(o->name, sizeof o->name, d[i].path); |
| 70 | copy(o->hid, sizeof o->hid, d[i].hid); |
| 71 | copy(o->cid, sizeof o->cid, d[i].cid); |
| 72 | o->mmio_base = d[i].mmio_base; |
| 73 | o->mmio_len = d[i].mmio_len; |
| 74 | o->table[0] = (char)t[0], o->table[1] = (char)t[1], o->table[2] = (char)t[2], o->table[3] = (char)t[3]; |
| 75 | o->table[4] = 0; |
| 76 | } |
| 77 | c->n++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void each_table(uint64_t table, uint32_t len, void *ctx) |
| 82 | { |
| 83 | (void)len; |
| 84 | if (!table || !ck_mm_mapped(table, 4)) return; |
| 85 | const uint8_t *t = (const uint8_t *)(uintptr_t)table; |
| 86 | if (t[0] == 'S' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T') scan_block(ctx, table); |
| 87 | } |
| 88 | |
| 89 | int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max, |
| 90 | struct ck_acpi_scan_info *info) |
| 91 | { |
| 92 | struct scan_ctx c = { ids, nids, max, out, { 0, 0, 0, 0 }, 0 }; |
| 93 | const struct ck_handoff *h = ck_handoff_get(); |
| 94 | const uint8_t *fadt = ck_acpi_find("FACP"); |
| 95 | uint64_t dsdt = 0; |
| 96 | if (fadt) { |
| 97 | uint32_t flen = rd32(fadt + 4); |
| 98 | if (flen >= 148) dsdt = (uint64_t)rd32(fadt + 140) | (uint64_t)rd32(fadt + 144) << 32; /* X_DSDT */ |
| 99 | if (!dsdt && flen >= 44) dsdt = rd32(fadt + 40); /* DSDT */ |
| 100 | } |
| 101 | if (!dsdt || !h || !h->rsdp) { |
| 102 | if (info) *info = c.info; |
| 103 | return -1; |
| 104 | } |
| 105 | scan_block(&c, dsdt); |
| 106 | (void)ck_acpi_each(h->rsdp, each_table, &c); |
| 107 | if (info) *info = c.info; |
| 108 | return c.n; |
| 109 | } |