diff mbox

[Xen-devel,RESEND,08/14] libxl/arm: Construct ACPI MADT table

Message ID 1464670986-10256-9-git-send-email-zhaoshenglong@huawei.com
State New
Headers show

Commit Message

Shannon Zhao May 31, 2016, 5:03 a.m. UTC
From: Shannon Zhao <shannon.zhao@linaro.org>

According to the gic version, construct the MADT table.

Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
---
 tools/libxl/libxl_arm.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)

Comments

Julien Grall June 7, 2016, 1:40 p.m. UTC | #1
Hello Shannon,

On 31/05/16 06:03, Shannon Zhao wrote:
> From: Shannon Zhao <shannon.zhao@linaro.org>
>
> According to the gic version, construct the MADT table.

NIT: s/gic/GIC/

>
> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
> ---
>   tools/libxl/libxl_arm.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 107 insertions(+)
>
> diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c
> index 7949635..f72f692 100644
> --- a/tools/libxl/libxl_arm.c
> +++ b/tools/libxl/libxl_arm.c
> @@ -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);

I would prefer if you introduce an helper to compute the MPIDR to avoid 
open coding in 2 places (DT and ACPI).

> +        gicc->arm_mpidr = mpidr_aff;
> +        gicc->uid = i;

I don't see any code to create the associated processor device object in 
the DSDT.

> +        gicc->flags = 1;

Please use a define here.

> +
> +        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;

This field is has been added with ACPI 5.1 errata, which technically is 
not ACPI 5.1.

I don't mind if you use it, however it would need a comment to explain 
that this field had no other meaning before...

> +}
> +
> +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;

Pointless cast.

> +    /* Align to 64bit. */

Pointless comment.

> +    dom->acpitable_blob->madt.size = size;
> +    dom->acpitable_size += dom->acpitable_blob->madt.size;
> +
> +    return 0;
> +}
> +

Regards,
diff mbox

Patch

diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c
index 7949635..f72f692 100644
--- a/tools/libxl/libxl_arm.c
+++ b/tools/libxl/libxl_arm.c
@@ -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;
 }