From fa8bf187448d93e5c4c0ddeaf6c7fbb384d1b946 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 15 Sep 2025 12:04:08 -0400 Subject: kernel/amd64: Move ioapic_read_madt() to acpi_subr Signed-off-by: Ian Moffett --- src/sys/acpi/acpi_subr.c | 40 +++++++++++++++++++++++++++++++ src/sys/arch/amd64/mainbus/ioapic.c | 48 ++----------------------------------- src/sys/include/acpi/acpi.h | 16 +++++++++++++ 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/sys/acpi/acpi_subr.c b/src/sys/acpi/acpi_subr.c index eab1f4f..d953fd5 100644 --- a/src/sys/acpi/acpi_subr.c +++ b/src/sys/acpi/acpi_subr.c @@ -27,6 +27,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -64,3 +65,42 @@ acpi_query(const char *query) return NULL; } + +/* + * Read a MADT entry + */ +int +acpi_read_madt(uint32_t type, int(*cb)(struct apic_header *, size_t arg), + size_t arg) +{ + static struct acpi_madt *madt = NULL; + uint8_t *cur, *end; + int retval = -1; + struct apic_header *apichdr; + + /* Try to read the MADT table */ + if (madt == NULL) { + madt = acpi_query("APIC"); + } + if (madt == NULL) { + panic("ioapic_read_madt: failed to get MADT\n"); + } + + cur = (uint8_t *)(madt + 1); + end = (uint8_t *)madt + madt->hdr.length; + while (cur < end) { + apichdr = (void *)cur; + if (apichdr->type == type) { + retval = cb(apichdr, arg); + } + + /* If the entry was found, stop */ + if (retval >= 0) { + return retval; + } + + cur += apichdr->length; + } + + return -1; +} diff --git a/src/sys/arch/amd64/mainbus/ioapic.c b/src/sys/arch/amd64/mainbus/ioapic.c index 95ef55a..acb7a0c 100644 --- a/src/sys/arch/amd64/mainbus/ioapic.c +++ b/src/sys/arch/amd64/mainbus/ioapic.c @@ -37,7 +37,6 @@ #include static struct ioapic *ioapic = NULL; -static struct acpi_madt *madt = NULL; static struct apic_header *override = NULL; /* @@ -125,49 +124,6 @@ ioapic_write_redentry(const union ioapic_redentry *entry, uint8_t index) ioapic_writel(IOREDTBL + index * 2 + 1, (uint32_t)(entry->value >> 32)); } -/* - * Read a MADT entry - * - * @type: Type that we should scan for - * @cb: Callback (returns 0 if entry is found) - * @arg: Optional argument - * - * Returns the callback return value, on success, - * otherwise a less than zero value on failure. - */ -static int -ioapic_read_madt(uint32_t type, int(*cb)(struct apic_header *, size_t arg), - size_t arg) -{ - uint8_t *cur, *end; - int retval = -1; - struct apic_header *apichdr; - - /* Try to read the MADT table */ - madt = acpi_query("APIC"); - if (madt == NULL) { - panic("ioapic_read_madt: failed to get MADT\n"); - } - - cur = (uint8_t *)(madt + 1); - end = (uint8_t *)madt + madt->hdr.length; - while (cur < end) { - apichdr = (void *)cur; - if (apichdr->type == type) { - retval = cb(apichdr, arg); - } - - /* If the entry was found, stop */ - if (retval >= 0) { - return retval; - } - - cur += apichdr->length; - } - - return -1; -} - /* * Set the I/O APIC MMIO base address */ @@ -220,7 +176,7 @@ ioapic_route_vec(uint8_t irq, uint8_t vector) union ioapic_redentry redent; int gsi; - gsi = ioapic_read_madt( + gsi = acpi_read_madt( APIC_TYPE_INTERRUPT_OVERRIDE, __irq_to_gsi, irq @@ -242,7 +198,7 @@ ioapic_init(void) uint8_t ver, nredir; if (ioapic == NULL) { - ioapic_read_madt( + acpi_read_madt( APIC_TYPE_IO_APIC, __ioapic_callback, 0 diff --git a/src/sys/include/acpi/acpi.h b/src/sys/include/acpi/acpi.h index ebc2129..16c1653 100644 --- a/src/sys/include/acpi/acpi.h +++ b/src/sys/include/acpi/acpi.h @@ -74,4 +74,20 @@ void *acpi_query(const char *query); */ int acpi_early_init(void); + +/* + * Read a MADT entry + * + * @type: Type that we should scan for + * @cb: Callback (returns 0 if entry is found) + * @arg: Optional argument + * + * Returns the callback return value, on success, + * otherwise a less than zero value on failure. + */ +int acpi_read_madt( + uint32_t type, int(*cb)(struct apic_header *, size_t arg), + size_t arg +); + #endif /* !_MACHINE_ACPI_H_ */ -- cgit v1.2.3