git: 7f6f47151bde - stable/13 - bhyve: build MADT table by basl

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Mon, 05 Dec 2022 07:42:04 UTC
The branch stable/13 has been updated by corvink:

URL: https://cgit.FreeBSD.org/src/commit/?id=7f6f47151bde0a36b4461777bfc27833a209753b

commit 7f6f47151bde0a36b4461777bfc27833a209753b
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2022-04-06 09:10:42 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2022-12-05 07:39:39 +0000

    bhyve: build MADT table by basl
    
    Building the MADT table by basl will allow it to be loaded by qemu's
    ACPI table loader in the future.
    
    Reviewed by:            jhb, markj (older version)
    Approved by:            manu (mentor)
    MFC after:              2 weeks
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D36997
    
    (cherry picked from commit 897fe59adf7b160fc2cf7a3f10334b236853c15a)
---
 usr.sbin/bhyve/acpi.c | 165 +++++++++++++++++++++++---------------------------
 1 file changed, 76 insertions(+), 89 deletions(-)

diff --git a/usr.sbin/bhyve/acpi.c b/usr.sbin/bhyve/acpi.c
index 82a7e0b90748..67b62a07bffa 100644
--- a/usr.sbin/bhyve/acpi.c
+++ b/usr.sbin/bhyve/acpi.c
@@ -109,7 +109,9 @@ __FBSDID("$FreeBSD$");
 #define BHYVE_ASL_SUFFIX	".aml"
 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
 
+#define BHYVE_ADDRESS_IOAPIC 	0xFEC00000
 #define BHYVE_ADDRESS_HPET 	0xFED00000
+#define BHYVE_ADDRESS_LAPIC 	0xFEE00000
 
 static int basl_keep_temps;
 static int basl_verbose_iasl;
@@ -241,94 +243,6 @@ err_exit:
 	return (errno);
 }
 
-static int
-basl_fwrite_madt(FILE *fp)
-{
-	int i;
-
-	EFPRINTF(fp, "/*\n");
-	EFPRINTF(fp, " * bhyve MADT template\n");
-	EFPRINTF(fp, " */\n");
-	EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
-	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
-	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
-	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
-	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
-	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT  \"\n");
-	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
-
-	/* iasl will fill in the compiler ID/revision fields */
-	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
-	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
-	EFPRINTF(fp, "\n");
-
-	EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
-	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
-	EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
-	EFPRINTF(fp, "\n");
-
-	/* Add a Processor Local APIC entry for each CPU */
-	for (i = 0; i < basl_ncpu; i++) {
-		EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
-		EFPRINTF(fp, "[0001]\t\tLength : 08\n");
-		/* iasl expects hex values for the proc and apic id's */
-		EFPRINTF(fp, "[0001]\t\tProcessor ID : %02x\n", i);
-		EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02x\n", i);
-		EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
-		EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
-		EFPRINTF(fp, "\t\t\tRuntime Online Capable : 0\n");
-		EFPRINTF(fp, "\n");
-	}
-
-	/* Always a single IOAPIC entry, with ID 0 */
-	EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
-	EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
-	/* iasl expects a hex value for the i/o apic id */
-	EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02x\n", 0);
-	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
-	EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
-	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
-	EFPRINTF(fp, "\n");
-
-	/* Legacy IRQ0 is connected to pin 2 of the IOAPIC */
-	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
-	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
-	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
-	EFPRINTF(fp, "[0001]\t\tSource : 00\n");
-	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000002\n");
-	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
-	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
-	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
-	EFPRINTF(fp, "\n");
-
-	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
-	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
-	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
-	EFPRINTF(fp, "[0001]\t\tSource : %02X\n", SCI_INT);
-	EFPRINTF(fp, "[0004]\t\tInterrupt : %08X\n", SCI_INT);
-	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
-	EFPRINTF(fp, "\t\t\tPolarity : 3\n");
-	EFPRINTF(fp, "\t\t\tTrigger Mode : 3\n");
-	EFPRINTF(fp, "\n");
-
-	/* Local APIC NMI is connected to LINT 1 on all CPUs */
-	EFPRINTF(fp, "[0001]\t\tSubtable Type : 04\n");
-	EFPRINTF(fp, "[0001]\t\tLength : 06\n");
-	EFPRINTF(fp, "[0001]\t\tProcessor ID : FF\n");
-	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
-	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
-	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
-	EFPRINTF(fp, "[0001]\t\tInterrupt Input LINT : 01\n");
-	EFPRINTF(fp, "\n");
-
-	EFFLUSH(fp);
-
-	return (0);
-
-err_exit:
-	return (errno);
-}
-
 static int
 basl_fwrite_fadt(FILE *fp)
 {
@@ -896,6 +810,79 @@ build_hpet(struct vmctx *const ctx)
 	return (0);
 }
 
+static int
+build_madt(struct vmctx *const ctx)
+{
+	ACPI_TABLE_MADT madt;
+	ACPI_MADT_LOCAL_APIC madt_lapic;
+	ACPI_MADT_IO_APIC madt_ioapic;
+	ACPI_MADT_INTERRUPT_OVERRIDE madt_irq_override;
+	ACPI_MADT_LOCAL_APIC_NMI madt_lapic_nmi;
+	struct basl_table *table;
+
+	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MADT,
+	    BASL_TABLE_ALIGNMENT, MADT_OFFSET));
+
+	memset(&madt, 0, sizeof(madt));
+	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MADT, 1, 1));
+	madt.Address = htole32(BHYVE_ADDRESS_LAPIC);
+	madt.Flags = htole32(ACPI_MADT_PCAT_COMPAT);
+	BASL_EXEC(basl_table_append_content(table, &madt, sizeof(madt)));
+
+	/* Local APIC for each CPU */
+	for (int i = 0; i < basl_ncpu; ++i) {
+		memset(&madt_lapic, 0, sizeof(madt_lapic));
+		madt_lapic.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC;
+		madt_lapic.Header.Length = sizeof(madt_lapic);
+		madt_lapic.ProcessorId = i;
+		madt_lapic.Id = i;
+		madt_lapic.LapicFlags = htole32(ACPI_MADT_ENABLED);
+		BASL_EXEC(basl_table_append_bytes(table, &madt_lapic,
+		    sizeof(madt_lapic)));
+	}
+
+	/* I/O APIC */
+	memset(&madt_ioapic, 0, sizeof(madt_ioapic));
+	madt_ioapic.Header.Type = ACPI_MADT_TYPE_IO_APIC;
+	madt_ioapic.Header.Length = sizeof(madt_ioapic);
+	madt_ioapic.Address = htole32(BHYVE_ADDRESS_IOAPIC);
+	BASL_EXEC(
+	    basl_table_append_bytes(table, &madt_ioapic, sizeof(madt_ioapic)));
+
+	/* Legacy IRQ0 is connected to pin 2 of the I/O APIC */
+	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
+	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
+	madt_irq_override.Header.Length = sizeof(madt_irq_override);
+	madt_irq_override.GlobalIrq = htole32(2);
+	madt_irq_override.IntiFlags = htole16(
+	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
+	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
+	    sizeof(madt_irq_override)));
+
+	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
+	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
+	madt_irq_override.Header.Length = sizeof(madt_irq_override);
+	madt_irq_override.SourceIrq = SCI_INT;
+	madt_irq_override.GlobalIrq = htole32(SCI_INT);
+	madt_irq_override.IntiFlags = htole16(
+	    ACPI_MADT_POLARITY_ACTIVE_LOW | ACPI_MADT_TRIGGER_LEVEL);
+	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
+	    sizeof(madt_irq_override)));
+
+	/* Local APIC NMI is conntected to LINT 1 on all CPUs */
+	memset(&madt_lapic_nmi, 0, sizeof(madt_lapic_nmi));
+	madt_lapic_nmi.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC_NMI;
+	madt_lapic_nmi.Header.Length = sizeof(madt_lapic_nmi);
+	madt_lapic_nmi.ProcessorId = 0xFF;
+	madt_lapic_nmi.IntiFlags = htole16(
+	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
+	madt_lapic_nmi.Lint = 1;
+	BASL_EXEC(basl_table_append_bytes(table, &madt_lapic_nmi,
+	    sizeof(madt_lapic_nmi)));
+
+	return (0);
+}
+
 static int
 build_mcfg(struct vmctx *const ctx)
 {
@@ -955,8 +942,8 @@ acpi_build(struct vmctx *ctx, int ncpu)
 	BASL_EXEC(basl_compile(ctx, basl_fwrite_rsdp, 0));
 	BASL_EXEC(basl_compile(ctx, basl_fwrite_rsdt, RSDT_OFFSET));
 	BASL_EXEC(basl_compile(ctx, basl_fwrite_xsdt, XSDT_OFFSET));
-	BASL_EXEC(basl_compile(ctx, basl_fwrite_madt, MADT_OFFSET));
 	BASL_EXEC(basl_compile(ctx, basl_fwrite_fadt, FADT_OFFSET));
+	BASL_EXEC(build_madt(ctx));
 	BASL_EXEC(build_hpet(ctx));
 	BASL_EXEC(build_mcfg(ctx));
 	BASL_EXEC(build_facs(ctx));