acpi: uACPI, for interrupt routing, the power button and power-off

The firmware's AML is now interpreted. uACPI 6.1.1 (MIT) is fetched by
ports/uacpi, checksum-verified, and compiled into the kernel unmodified,
the same way lwIP is. kernel/acpi/host.c is its host layer: memory,
mapping, port and PCI config I/O, time, mutexes and events that spin
before the scheduler exists, the SCI, and an "acpi" thread for the
work the SCI defers.

PCI interrupts go where the board says. pci_init() now routes every
device's INTx through the root bridge's _PRT before drivers bind. It
follows link devices' _CRS, uses a bridge's own _PRT where the namespace
has one, and swizzles through bridges that don't. Before this, INTx
waited on the 8259's line number, which is the wrong pin in APIC mode.
irq_register() takes a trigger mode rather than a level/low flag,
because q35's links and most SCIs are level, active-high. The MADT's
interrupt source overrides are now parsed, and are what places the SCI.

The power button syncs the filesystems and powers off. shutdown() and
reboot() go through the firmware's S5 and reset register first, and
only then fall back to the emulators' ports, which a real board does
not answer.

Tested under QEMU: q35 with MSI off routes xHCI to GSI 22,
level/active-high, and it takes real interrupts with nothing polled.
system_powerdown powers off q35 and i440fx (QEMU exits 0), and q35
with 4 CPUs boots from NVMe with USB hot-plug working alongside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S1giNvrHTrPMPw7V6x1kQ4
This commit is contained in:
Nexoniarz 2026-10-22 16:12:28 +02:00
commit 93f981c7ea
16 changed files with 1237 additions and 14 deletions

View file

@ -300,7 +300,7 @@ irq_info(char *buf, int cap)
if(line[irq].polled)
off = emit(buf, cap, off, " [polled]");
off = emit(buf, cap, off, line[irq].msi ? " msi\n" :
line[irq].level_low ? " level\n" : " edge\n");
IRQ_IS_LEVEL(line[irq].level_low) ? " level\n" : " edge\n");
}
// How many timer interrupts were lost.
@ -424,7 +424,7 @@ irq_poll_thread(void)
if(line[irq].n == 0 || line[irq].msi || reserved(irq))
continue;
int poll = line[irq].polled ||
(line[irq].level_low && line[irq].delivered == 0 &&
(IRQ_IS_LEVEL(line[irq].level_low) && line[irq].delivered == 0 &&
now - line[irq].since > POLL_AFTER);
if(!poll)
continue;

View file

@ -26,6 +26,7 @@
#include "device.h"
#include "traps.h"
#include "irq.h"
#include "acpi.h"
#include "memlayout.h"
#include "proc.h"
@ -275,7 +276,7 @@ pci_irq_attach(struct pci_dev *d, irq_handler_t fn, void *arg,
if(d->irq == 0xFF || d->irq >= NIRQ_IOAPIC)
return -1;
if(irq_register(d->irq, fn, arg, name, 1) < 0)
if(irq_register(d->irq, fn, arg, name, d->irq_mode) < 0)
return -1;
d->irq_kind = PCI_IRQ_INTX;
d->irq_line = d->irq;
@ -394,6 +395,7 @@ add_func(uint8 bus, uint8 slot, uint8 func)
d->class = pci_read8(bus, slot, func, PCI_CLASS);
d->header_type = pci_read8(bus, slot, func, PCI_HEADER_TYPE);
d->irq = pci_read8(bus, slot, func, PCI_INTERRUPT_LINE);
d->irq_mode = IRQ_LEVEL_LOW;
// Only header type 0 has six BARs. A bridge (type 1) has two and a very
// different layout, and reading it as though it had six would hand out
@ -692,5 +694,10 @@ pci_init(void)
scan_bus(0);
kinfo("pci: %d device%s\n", ndevs, ndevs == 1 ? "" : "s");
// Before any driver binds: the line each device's config space names is
// the 8259's number, which is not the IOAPIC pin it is wired to. ACPI's
// _PRT says which pin it really is; see kernel/acpi/aml.c.
acpi_pci_route();
pci_bind_all();
}