@@ -42,6 +42,8 @@ typedef uint64_t u64;
#define ACPI_LEVEL_SENSITIVE (u8) 0x00
#define ACPI_ACTIVE_LOW (u8) 0x01
+#define ACPI_GICC_ENABLED 1
+
enum {
RSDP,
XSDT,
@@ -137,6 +139,101 @@ static void make_acpi_gtdt(libxl__gc *gc, struct xc_dom_image *dom)
dom->acpitable_size += ROUNDUP(acpitables[GTDT].size, 3);
}
+static void make_acpi_madt_gicc(void *table, int nr_cpus, uint64_t gicc_base)
+{
+ uint32_t i;
+ struct acpi_madt_generic_interrupt *gicc = table;
+
+ for (i = 0; i < nr_cpus; i++) {
+ gicc->header.type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
+ gicc->header.length = sizeof(*gicc);
+ gicc->base_address = gicc_base;
+ gicc->cpu_interface_number = i;
+ gicc->arm_mpidr = libxl__compute_mpdir(i);
+ gicc->uid = i;
+ gicc->flags = ACPI_GICC_ENABLED;
+ gicc++;
+ }
+}
+
+static void make_acpi_madt_gicd(void *table, uint64_t gicd_base,
+ uint8_t gic_version)
+{
+ struct acpi_madt_generic_distributor *gicd = table;
+
+ gicd->header.type = ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR;
+ gicd->header.length = sizeof(*gicd);
+ gicd->base_address = gicd_base;
+ /* This version filed has no meaning before ACPI 5.1 errata. */
+ gicd->version = gic_version;
+}
+
+static void make_acpi_madt_gicr(void *table, uint64_t gicr_base,
+ uint64_t gicr_size)
+{
+ struct acpi_madt_generic_redistributor *gicr = table;
+
+ gicr->header.type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
+ gicr->header.length = sizeof(*gicr);
+ gicr->base_address = gicr_base;
+ gicr->length = gicr_size;
+}
+
+static int make_acpi_madt(libxl__gc *gc, struct xc_dom_image *dom, int nr_cpus,
+ xc_domain_configuration_t *xc_config)
+{
+ size_t size;
+ void *table;
+ struct acpi_table_madt *madt;
+
+ switch (xc_config->gic_version) {
+ case XEN_DOMCTL_CONFIG_GIC_V2:
+ size = sizeof(struct acpi_table_madt) +
+ sizeof(struct acpi_madt_generic_interrupt) * nr_cpus +
+ sizeof(struct acpi_madt_generic_distributor);
+ table = libxl__zalloc(gc, size);
+ madt = (struct acpi_table_madt *)table;
+
+ table += sizeof(struct acpi_table_madt);
+ make_acpi_madt_gicc(table, nr_cpus, GUEST_GICC_BASE);
+
+ table += sizeof(struct acpi_madt_generic_interrupt) * nr_cpus;
+ make_acpi_madt_gicd(table, GUEST_GICD_BASE, ACPI_MADT_GIC_VERSION_V2);
+ break;
+ case XEN_DOMCTL_CONFIG_GIC_V3:
+ size = sizeof(struct acpi_table_madt) +
+ sizeof(struct acpi_madt_generic_interrupt) * nr_cpus +
+ sizeof(struct acpi_madt_generic_distributor) +
+ sizeof(struct acpi_madt_generic_redistributor);
+ table = libxl__zalloc(gc, size);
+ madt = (struct acpi_table_madt *)table;
+
+ table += sizeof(struct acpi_table_madt);
+ make_acpi_madt_gicc(table, nr_cpus, 0);
+
+ table += sizeof(struct acpi_madt_generic_interrupt) * nr_cpus;
+ make_acpi_madt_gicd(table, GUEST_GICV3_GICD_BASE,
+ ACPI_MADT_GIC_VERSION_V3);
+
+ table += sizeof(struct acpi_madt_generic_distributor);
+ make_acpi_madt_gicr(table, GUEST_GICV3_GICR0_BASE,
+ GUEST_GICV3_GICR0_SIZE);
+ break;
+ default:
+ LOG(ERROR, "Unknown GIC version");
+ return ERROR_FAIL;
+ }
+
+ make_acpi_header(&madt->header, "APIC", size, 3);
+
+ acpitables[MADT].table = madt;
+ acpitables[MADT].size = size;
+ /* Align to 64bit. */
+ dom->acpitable_size += ROUNDUP(acpitables[MADT].size, 3);
+
+ return 0;
+}
+
int libxl__prepare_acpi(libxl__gc *gc, libxl_domain_build_info *info,
libxl__domain_build_state *state,
struct xc_dom_image *dom)
@@ -160,6 +257,9 @@ int libxl__prepare_acpi(libxl__gc *gc, libxl_domain_build_info *info,
make_acpi_rsdp(gc, dom);
make_acpi_xsdt(gc, dom);
make_acpi_gtdt(gc, dom);
+ rc = make_acpi_madt(gc, dom, info->max_vcpus, xc_config);
+ if (rc)
+ return rc;
return 0;
}