@@ -958,11 +958,115 @@ static void make_acpi_dsdt(libxl__gc *gc, struct xc_dom_image *dom)
dom->acpitable_size += ROUNDUP(dom->acpitable_blob->dsdt.size, 3);
}
+static void make_acpi_madt_gicc(void *table, int nr_cpus, uint64_t gicc_base)
+{
+ uint32_t i;
+ uint64_t mpidr_aff;
+ struct acpi_madt_generic_interrupt *gicc = table;
+
+ for (i = 0; i < nr_cpus; i++) {
+ gicc->type = ACPI_APIC_GENERIC_INTERRUPT;
+ gicc->length = sizeof(*gicc);
+ gicc->base_address = gicc_base;
+ gicc->cpu_interface_number = i;
+
+ /*
+ * We will use AFF0 and AFF1 when constructing the MPIDR value of the
+ * guest at the moment, for it is enough for the current max vcpu
+ * number.
+ */
+ mpidr_aff = (i & 0x0f) | (((i >> 4) & 0xff) << 8);
+ gicc->arm_mpidr = mpidr_aff;
+ gicc->uid = i;
+ gicc->flags = 1;
+
+ gicc += 1;
+ }
+}
+
+static void make_acpi_madt_gicd(void *table, uint64_t gicd_base,
+ uint8_t gic_version)
+{
+ struct acpi_madt_generic_distributor *gicd = table;
+
+ gicd->type = ACPI_APIC_GENERIC_DISTRIBUTOR;
+ gicd->length = sizeof(*gicd);
+ gicd->base_address = gicd_base;
+ 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->type = ACPI_APIC_GENERIC_REDISTRIBUTOR;
+ gicr->length = sizeof(*gicr);
+ gicr->base_address = gicr_base;
+ gicr->range_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)
+{
+ uint32_t size;
+ void *table;
+ struct acpi_madt_descriptor *madt;
+
+ switch (xc_config->gic_version) {
+ case XEN_DOMCTL_CONFIG_GIC_V2:
+ size = sizeof(struct acpi_madt_descriptor) +
+ sizeof(struct acpi_madt_generic_interrupt) * nr_cpus +
+ sizeof(struct acpi_madt_generic_distributor);
+ table = libxl__zalloc(gc, size);
+ madt = (struct acpi_madt_descriptor *)table;
+
+ table += sizeof(struct acpi_madt_descriptor);
+ 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, 2);
+ break;
+ case XEN_DOMCTL_CONFIG_GIC_V3:
+ size = sizeof(struct acpi_madt_descriptor) +
+ 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_madt_descriptor *)table;
+
+ table += sizeof(struct acpi_madt_descriptor);
+ 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, 3);
+
+ 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 %s",
+ gicv_to_string(xc_config->gic_version));
+ return ERROR_FAIL;
+ }
+
+ make_acpi_header(&madt->header, "APIC", size, 3);
+
+ dom->acpitable_blob->madt.table = (void *)madt;
+ /* Align to 64bit. */
+ dom->acpitable_blob->madt.size = size;
+ dom->acpitable_size += dom->acpitable_blob->madt.size;
+
+ return 0;
+}
+
static int prepare_acpi(libxl__gc *gc, libxl_domain_build_info *info,
libxl__domain_build_state *state,
struct xc_dom_image *dom)
{
const libxl_version_info *vers;
+ int rc;
/* convenience aliases */
xc_domain_configuration_t *xc_config = &state->config;
@@ -981,6 +1085,9 @@ static int prepare_acpi(libxl__gc *gc, libxl_domain_build_info *info,
make_acpi_gtdt(gc, dom);
make_acpi_fadt(gc, dom);
make_acpi_dsdt(gc, dom);
+ rc = make_acpi_madt(gc, dom, info->max_vcpus, xc_config);
+ if (rc)
+ return rc;
return 0;
}