Skip to contentSkip to conversation

Implement GIC-500 from GICv3 family for arm64

RFC PATCH Superseded Shlomo Pongratz 4 participants 14 messages Jun 4, 2015 – Jun 8, 2015

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Implement GIC-500 from GICv3 family for arm64

From: Shlomo Pongratz <[email protected]>

This patch is a first step toward 128 cores support for arm64.

At first only 64 cores are supported for two reasons:
First the largest integer type has the size of 64 bits and modifying essential data structures in order to support 128 cores will require the usage of bitops.
Second currently the Linux (kernel) can be configured to support up to 64 cores thus there is no urgency with 128 cores support.

Things left to do:

There is a need to support flexible clusters size. The GIC-500 can support up to 128 cores, up to 32 clusters and up to 8 cores is a cluster. So for example, if one wishes to have 16 cores, the options are:
2 clusters of 8 cores each, 4 clusters with 4 cores each Currently only the first option is supported.
There is an issue of passing clock affinity to via the dtb. In the dtb

interrupt section there are only 24 bit left to affinity since the variable is a 32 bit entity and 8 bits are reserved for flags. See Documentation/devicetree/bindings/arm/arch_timer.txt. Note that this issue is not seems to be critical as when checking /proc/irq/3/smp_affinity with 32 cores all 32 bits are one.

The last issue is to add support for 128 cores. This requires the usage of bitops and currently can be tested up to 64 cores.

Special thanks to Peter Crostwaite whose patch to th Linux (kernel) i.e. Implement cpu_relax as yield solved the problem of the boot process getting stuck for 24 cores and more.

V2:
- Split the original patch to 4 patches
- Add SRE API to the GIC code.
- Add call to gicv3_update to armv8_gicv3_set_priority_mask.
- Cosmetic changes.
- Fix number of irq when reading GICD_TYPER.

V3:
- Replace my original 1 & 4 patches with Pavel's patches.
- Add groups support to complies with new GICv2 addtions
- Cosmetic changes.

Pavel Fedin (2):
Use Aff1 with mpidr This is an improved and KVM-aware alternative to Add virt-v3 machine that uses GIC-500

Shlomo Pongratz (2):
Implment GIC-500
GICv3 support

 hw/arm/virt.c                      |  165 ++-
 hw/intc/Makefile.objs              |    2 +
 hw/intc/arm_gicv3.c                | 2086 ++++++++++++++++++++++++++++++++++++
 hw/intc/arm_gicv3_common.c         |  217 ++++
 hw/intc/gicv3_internal.h           |  161 +++
 include/hw/arm/virt.h              |    4 +
 include/hw/boards.h                |    1 +
 include/hw/intc/arm_gicv3.h        |   44 +
 include/hw/intc/arm_gicv3_common.h |  119 ++
 target-arm/cpu-qom.h               |    3 +
 target-arm/cpu.c                   |   17 +
 target-arm/cpu.h                   |   12 +
 target-arm/cpu64.c                 |  105 ++
 target-arm/helper.c                |    9 +-
 target-arm/kvm64.c                 |   25 +
 target-arm/psci.c                  |   20 +-
 16 files changed, 2952 insertions(+), 38 deletions(-)
 create mode 100644 hw/intc/arm_gicv3.c
 create mode 100644 hw/intc/arm_gicv3_common.c
 create mode 100644 hw/intc/gicv3_internal.h
 create mode 100644 include/hw/intc/arm_gicv3.h
 create mode 100644 include/hw/intc/arm_gicv3_common.h
Signature · 1 line
1.9.1
#1 lore raw

RFC PATCH v3 1/4 Use Aff1 with mpidr This is an improved and KVM-aware alternative to

From: Pavel Fedin <[email protected]>

1. MPIDR value (minus feature bits) is now directly stored in CPU instance.
2. Default assignment is based on original rule (8 CPUs per cluster).
3. If KVM is used, MPIDR values are overridden with those from KVM. This is necessary for
proper KVM PSCI functioning.
4. Some cosmetic changes which would make further expansion of this code easier.
5. Added some debugging macros because CPU ID assignment is tricky part, and if there are
some problems, i think there should be a quick way to make sure they are correct.
This does not have an RFC mark because it is perfectly okay to be committed
alone, it
does not break anything. Commit message follows. Cc'ed to all interested parties because i
really hope to get things going somewhere this time.

In order to support up to 128 cores with GIC-500 (GICv3 implementation) affinity1 must be used. GIC-500 support up to 32 clusters with up to 8 cores in a cluster. So for example, if one wishes to have 16 cores, the options are: 2 clusters of 8 cores each, 4 clusters with 4 cores each Currently only the first option is supported for TCG. However, KVM expects to have the same clusters layout as host machine has. Therefore, if we use 64-bit KVM we override CPU IDs with those provided by the KVM. For 32-bit systems it is OK to leave the default because so far we do not have more than 8 CPUs on any of them.
This implementation has a potential to be extended with some methods which would define cluster layout instead of hardcoded CPUS_PER_CLUSTER definition. This would allow to emulate real machines with different layouts. However, in case of KVM we would still have to inherit IDs from the host.

---
 hw/arm/virt.c        |  6 +++++-
 target-arm/cpu-qom.h |  3 +++
 target-arm/cpu.c     | 17 +++++++++++++++++
 target-arm/helper.c  |  9 +++------
 target-arm/kvm64.c   | 25 +++++++++++++++++++++++++
 target-arm/psci.c    | 20 ++++++++++++++++++--
 6 files changed, 71 insertions(+), 9 deletions(-)
hw/arm/virt.c +5 -1
index 05db8cb..19c8c3a 100644
@@ -294,7 +294,11 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
                                         "enable-method", "psci");
         }
 
-        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
+        /*
+         * If cpus node's #address-cells property is set to 1
+         * The reg cell bits [23:0] must be set to bits [23:0] of MPIDR_EL1.
+         */
+        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", armcpu->mpidr);
         g_free(nodename);
     }
 }
target-arm/cpu-qom.h +3 -0
index ed5a644..a382a09 100644
@@ -159,6 +159,7 @@ typedef struct ARMCPU {
     uint64_t id_aa64mmfr1;
     uint32_t dbgdidr;
     uint32_t clidr;
+    uint64_t mpidr; /* Without feature bits */
     /* The elements of this array are the CCSIDR values for each cache,
      * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
      */
@@ -171,6 +172,8 @@ typedef struct ARMCPU {
     uint64_t rvbar;
 } ARMCPU;
 
+#define CPUS_PER_CLUSTER 8
+
 #define TYPE_AARCH64_CPU "aarch64-cpu"
 #define AARCH64_CPU_CLASS(klass) \
     OBJECT_CLASS_CHECK(AArch64CPUClass, (klass), TYPE_AARCH64_CPU)
target-arm/cpu.c +17 -0
index 4a888ab..7272466 100644
@@ -31,6 +31,12 @@
 #include "sysemu/kvm.h"
 #include "kvm_arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("armcpu: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif
+
 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -388,12 +394,23 @@ static void arm_cpu_initfn(Object *obj)
     CPUState *cs = CPU(obj);
     ARMCPU *cpu = ARM_CPU(obj);
     static bool inited;
+    uint32_t Aff1, Aff0;
 
     cs->env_ptr = &cpu->env;
     cpu_exec_init(&cpu->env);
     cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
                                          g_free, g_free);
 
+    /*
+     * We don't support setting cluster ID ([16..23]) (known as Aff2
+     * in later ARM ARM versions), or any of the higher affinity level fields,
+     * so these bits always RAZ.
+     */
+    Aff1 = cs->cpu_index / CPUS_PER_CLUSTER;
+    Aff0 = cs->cpu_index % CPUS_PER_CLUSTER;
+    cpu->mpidr = (Aff1 << 8) | Aff0;
+    DPRINTF("Init(%p): index %d mpidr 0x%jX\n", cpu, cs->cpu_index, cpu->mpidr);
+
 #ifndef CONFIG_USER_ONLY
     /* Our inbound IRQ and FIQ lines */
     if (kvm_enabled()) {
target-arm/helper.c +3 -6
index 1cc4993..99c7a30 100644
@@ -2063,12 +2063,9 @@ static const ARMCPRegInfo strongarm_cp_reginfo[] = {
 
 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
-    CPUState *cs = CPU(arm_env_get_cpu(env));
-    uint32_t mpidr = cs->cpu_index;
-    /* We don't support setting cluster ID ([8..11]) (known as Aff1
-     * in later ARM ARM versions), or any of the higher affinity level fields,
-     * so these bits always RAZ.
-     */
+    ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
+    uint64_t mpidr = cpu->mpidr;
+
     if (arm_feature(env, ARM_FEATURE_V7MP)) {
         mpidr |= (1U << 31);
         /* Cores which are uniprocessor (non-coherent)
target-arm/kvm64.c +25 -0
index 93c1ca8..99fa34e 100644
@@ -25,6 +25,12 @@
 #include "internals.h"
 #include "hw/arm/arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("arm-kvm64: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif
+
 static inline void set_feature(uint64_t *features, int feature)
 {
     *features |= 1ULL << feature;
@@ -77,6 +83,10 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
     return true;
 }
 
+#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFULL
+#define ARM_CPU_ID             3, 0, 0, 0
+#define ARM_CPU_ID_MPIDR       5
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
@@ -107,6 +117,21 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
+    /*
+     * When KVM is in use, psci is emulated in-kernel and not by qemu.
+     * In order for it to work correctly we should use correct MPIDR values,
+     * which appear to be inherited from the host.
+     * So we override our defaults by asking KVM about these values.
+     */
+    ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR),
+                          &cpu->mpidr);
+    if (ret) {
+        return ret;
+    }
+    cpu->mpidr &= ARM_MPIDR_HWID_BITMASK;
+    DPRINTF("Init(%p): index %d MPIDR 0x%jX\n", cpu,
+            cs->cpu_index, cpu->mpidr);
+
     return kvm_arm_init_cpreg_list(cpu);
 }
 
target-arm/psci.c +18 -2
index d8fafab..d03896f 100644
@@ -72,6 +72,22 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
     }
 }
 
+static uint32_t mpidr_to_idx(uint64_t mpidr)
+{
+    uint32_t Aff1, Aff0;
+
+    /*
+     * MPIDR_EL1 [RES0:affinity-3:RES1:U:RES0:MT:affinity-1:affinity-0]
+     * GIC 500 code currently supports 32 clusters with 8 cores each
+     * but no more than 128 cores. Future version will have flexible
+     * affinity selection
+     * GIC 400 supports 8 cores so 0x7 for Aff0 is O.K. too
+     */
+     Aff1 = (mpidr & 0xff00) >> 8;
+     Aff0 = mpidr & 0xff;
+     return Aff1 * CPUS_PER_CLUSTER + Aff0;
+}
+
 void arm_handle_psci_call(ARMCPU *cpu)
 {
     /*
@@ -121,7 +137,7 @@ void arm_handle_psci_call(ARMCPU *cpu)
 
         switch (param[2]) {
         case 0:
-            target_cpu_state = qemu_get_cpu(mpidr & 0xff);
+            target_cpu_state = qemu_get_cpu(mpidr_to_idx(mpidr));
             if (!target_cpu_state) {
                 ret = QEMU_PSCI_RET_INVALID_PARAMS;
                 break;
@@ -153,7 +169,7 @@ void arm_handle_psci_call(ARMCPU *cpu)
         context_id = param[3];
 
         /* change to the cpu we are powering up */
-        target_cpu_state = qemu_get_cpu(mpidr & 0xff);
+        target_cpu_state = qemu_get_cpu(mpidr_to_idx(mpidr));
         if (!target_cpu_state) {
             ret = QEMU_PSCI_RET_INVALID_PARAMS;
             break;
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Quoted 1 line · Shlomo Pongratz

From: Pavel Fedin <[email protected]>

Hi. I think this patch largely makes sense, but I have some comments below. If you want to fix these and resend as a standalone patch I'm happy to apply that.

Quoted 7 lines

1. MPIDR value (minus feature bits) is now directly stored in CPU instance.
2. Default assignment is based on original rule (8 CPUs per cluster).
3. If KVM is used, MPIDR values are overridden with those from KVM. This is necessary for
proper KVM PSCI functioning.
4. Some cosmetic changes which would make further expansion of this code easier.
5. Added some debugging macros because CPU ID assignment is tricky part, and if

Quoted 8 lines

there are
some problems, i think there should be a quick way to make sure they are correct.
This does not have an RFC mark because it is perfectly okay to be committed
alone, it
does not break anything. Commit message follows. Cc'ed to all interested parties because i
really hope to get things going somewhere this time.

This sort of commentary belongs below the '---' line, so it doesn't end up in the final git commit message.

Quoted 6 lines

In order to support up to 128 cores with GIC-500 (GICv3 implementation) affinity1 must be used. GIC-500 support up to 32 clusters with up to 8 cores in a cluster. So for example, if one wishes to have 16 cores, the options are: 2 clusters of 8 cores each, 4 clusters with 4 cores each Currently only the first option is supported for TCG. However, KVM expects to have the same clusters layout as host machine has. Therefore, if we use

Quoted 8 lines

64-bit KVM we override CPU IDs with those provided by the KVM. For 32-bit systems it is OK to leave the default because so far we do not have more than 8 CPUs on any of them.
This implementation has a potential to be extended with some methods which would define cluster layout instead of hardcoded CPUS_PER_CLUSTER definition. This would allow to emulate real machines with different layouts. However, in case of KVM we would still have to inherit IDs from the host.

I agree that we want to be using the same idea of MPIDR as KVM when KVM is enabled, certainly. But this commit message has
a number of inaccuracies:
* KVM doesn't inherit IDs from the host
* we don't need to have the same cluster layout as the host machine
* this isn't specific to 64-bit VMs so we should fix 32 bits at the same time

The bug we're actually trying to fix here is just that the kernel's mapping from VCPU ID to MPIDR value has changed from the simplistic implementation it originally had. So in userspace we need to read the MPIDR value back from the kernel rather than assuming we know the mapping. (It happens that the reason the kernel changed was to accommodate GICv3 and its restrictions on affinity values, but that's not relevant to why this patch is needed.)

Quoted 16 lines
---
 hw/arm/virt.c        |  6 +++++-
 target-arm/cpu-qom.h |  3 +++
 target-arm/cpu.c     | 17 +++++++++++++++++
 target-arm/helper.c  |  9 +++------
 target-arm/kvm64.c   | 25 +++++++++++++++++++++++++
 target-arm/psci.c    | 20 ++++++++++++++++++--
 6 files changed, 71 insertions(+), 9 deletions(-)
hw/arm/virt.c +0 -0
index 05db8cb..19c8c3a 100644

hw/arm/virt.c static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi) · line 300

Quoted 8 lines
hw/arm/virt.c +4 -1
@@ -294,7 +294,11 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
                                         "enable-method", "psci");
         }
 
-        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
+        /*
+         * If cpus node's #address-cells property is set to 1
+         * The reg cell bits [23:0] must be set to bits [23:0] of MPIDR_EL1.
+         */

This comment is either obvious or misleading depending on your point of view.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 162

Quoted 13 lines
hw/arm/virt.c +1 -0
+        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", armcpu->mpidr);
         g_free(nodename);
     }
 }
target-arm/cpu-qom.h +1 -0
index ed5a644..a382a09 100644
@@ -159,6 +159,7 @@ typedef struct ARMCPU {
     uint64_t id_aa64mmfr1;
     uint32_t dbgdidr;
     uint32_t clidr;
+    uint64_t mpidr; /* Without feature bits */

I think it would be less confusing to store the entire MPIDR here, and then mask it out if there are places that only want the affinity bits.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 175

Quoted 8 lines
target-arm/cpu-qom.h +1 -0
     /* The elements of this array are the CCSIDR values for each cache,
      * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
      */
@@ -171,6 +172,8 @@ typedef struct ARMCPU {
     uint64_t rvbar;
 } ARMCPU;
 
+#define CPUS_PER_CLUSTER 8

Changes I suggest below will mean this #define is only used in one place so it won't need to be in a header.

Quoted 9 lines
target-arm/cpu-qom.h +1 -0
+
 #define TYPE_AARCH64_CPU "aarch64-cpu"
 #define AARCH64_CPU_CLASS(klass) \
     OBJECT_CLASS_CHECK(AArch64CPUClass, (klass), TYPE_AARCH64_CPU)
target-arm/cpu.c +0 -0
index 4a888ab..7272466 100644

target-arm/cpu.c line 38

Quoted 8 lines
target-arm/cpu.c +5 -0
@@ -31,6 +31,12 @@
 #include "sysemu/kvm.h"
 #include "kvm_arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("armcpu: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif

Drop these debug macros, please.

Quoted 66 lines
target-arm/cpu.c +12 -0
+
 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -388,12 +394,23 @@ static void arm_cpu_initfn(Object *obj)
     CPUState *cs = CPU(obj);
     ARMCPU *cpu = ARM_CPU(obj);
     static bool inited;
+    uint32_t Aff1, Aff0;
 
     cs->env_ptr = &cpu->env;
     cpu_exec_init(&cpu->env);
     cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
                                          g_free, g_free);
 
+    /*
+     * We don't support setting cluster ID ([16..23]) (known as Aff2
+     * in later ARM ARM versions), or any of the higher affinity level fields,
+     * so these bits always RAZ.
+     */
+    Aff1 = cs->cpu_index / CPUS_PER_CLUSTER;
+    Aff0 = cs->cpu_index % CPUS_PER_CLUSTER;
+    cpu->mpidr = (Aff1 << 8) | Aff0;
+    DPRINTF("Init(%p): index %d mpidr 0x%jX\n", cpu, cs->cpu_index, cpu->mpidr);
+
 #ifndef CONFIG_USER_ONLY
     /* Our inbound IRQ and FIQ lines */
     if (kvm_enabled()) {
target-arm/helper.c +3 -6
index 1cc4993..99c7a30 100644
@@ -2063,12 +2063,9 @@ static const ARMCPRegInfo strongarm_cp_reginfo[] = {
 
 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
-    CPUState *cs = CPU(arm_env_get_cpu(env));
-    uint32_t mpidr = cs->cpu_index;
-    /* We don't support setting cluster ID ([8..11]) (known as Aff1
-     * in later ARM ARM versions), or any of the higher affinity level fields,
-     * so these bits always RAZ.
-     */
+    ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
+    uint64_t mpidr = cpu->mpidr;
+
     if (arm_feature(env, ARM_FEATURE_V7MP)) {
         mpidr |= (1U << 31);
         /* Cores which are uniprocessor (non-coherent)
target-arm/kvm64.c +6 -0
index 93c1ca8..99fa34e 100644
@@ -25,6 +25,12 @@
 #include "internals.h"
 #include "hw/arm/arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("arm-kvm64: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif
+
 static inline void set_feature(uint64_t *features, int feature)
 {
     *features |= 1ULL << feature;

target-arm/kvm64.c bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc) · line 88

Quoted 6 lines
target-arm/kvm64.c +3 -0
@@ -77,6 +83,10 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
     return true;
 }
 
+#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFULL
+#define ARM_CPU_ID             3, 0, 0, 0
+#define ARM_CPU_ID_MPIDR       5

I don't see the point in separating the op2 value from the rest.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 121

Quoted 10 lines
target-arm/kvm64.c +3 -0
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
@@ -107,6 +117,21 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
+    /*
+     * When KVM is in use, psci is emulated in-kernel and not by qemu.

PSCI and QEMU should both be all-caps.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 123

Quoted 2 lines
target-arm/kvm64.c +2 -0
+     * In order for it to work correctly we should use correct MPIDR values,
+     * which appear to be inherited from the host.

I don't think they're inherited from the host: it's just that different host kernels use different mappings from vcpu ID to MPIDR (see reset_mpidr() in arch/arm64/kvm/sys_regs.c; its implementation has changed over time).
In any case we don't need to speculate; you can just say:

In order for it to work correctly we must use MPIDR values in the device tree which match the MPIDR values the kernel has picked for the vcpus, so ask KVM what those values are.

Quoted 4 lines
target-arm/kvm64.c +4 -0
+     * So we override our defaults by asking KVM about these values.
+     */
+    ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR),
+                          &cpu->mpidr);

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 137

Quoted 10 lines
target-arm/kvm64.c +7 -0
+    if (ret) {
+        return ret;
+    }
+    cpu->mpidr &= ARM_MPIDR_HWID_BITMASK;
+    DPRINTF("Init(%p): index %d MPIDR 0x%jX\n", cpu,
+            cs->cpu_index, cpu->mpidr);
+
     return kvm_arm_init_cpreg_list(cpu);
 }
 

There should also be an equivalent patch to kvm32.c, because PSCI on 32-bit systems has the same issue.

Quoted 29 lines
target-arm/psci.c +16 -0
index d8fafab..d03896f 100644
@@ -72,6 +72,22 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
     }
 }
 
+static uint32_t mpidr_to_idx(uint64_t mpidr)
+{
+    uint32_t Aff1, Aff0;
+
+    /*
+     * MPIDR_EL1 [RES0:affinity-3:RES1:U:RES0:MT:affinity-1:affinity-0]
+     * GIC 500 code currently supports 32 clusters with 8 cores each
+     * but no more than 128 cores. Future version will have flexible
+     * affinity selection
+     * GIC 400 supports 8 cores so 0x7 for Aff0 is O.K. too
+     */
+     Aff1 = (mpidr & 0xff00) >> 8;
+     Aff0 = mpidr & 0xff;
+     return Aff1 * CPUS_PER_CLUSTER + Aff0;
+}
 
+
 void arm_handle_psci_call(ARMCPU *cpu)
 {
     /*

target-arm/psci.c void arm_handle_psci_call(ARMCPU *cpu) · line 140

Quoted 5 lines
target-arm/psci.c +1 -1
@@ -121,7 +137,7 @@ void arm_handle_psci_call(ARMCPU *cpu)
 
         switch (param[2]) {
         case 0:
-            target_cpu_state = qemu_get_cpu(mpidr & 0xff);
+            target_cpu_state = qemu_get_cpu(mpidr_to_idx(mpidr));

Rather than doing this, you can just have an
arm_get_cpu_by_mpidr() which does

    CPU_FOREACH(cs) {
        ARMCPU *cpu = ARM_CPU(cs);

        if (cpu->mpidr == mpidr) {
            return cpu;
        }
    }
    return NULL;

ie same as qemu_get_cpu() but checking on mpidr rather than cpu_index. Then we don't need to make assumptions about the mpidr-to-index mapping here at all (and this code will continue to work if in future we decide to expose the cluster topology via CPU properties for the board to set.)

thanks
-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Hi all.
Patch #1 is actually Pavel Fedin's patch
https://qemu-devel/2015-05/msg04495.html, which I included as a replacement to my original patch #1 "as there could be only one". I think that Pavel's needs to address all the issues in the original thread.
Best regards.
S.P.

Quoted 328 lines · Peter Maydell
Quoted 1 line · Shlomo Pongratz

From: Pavel Fedin <[email protected]>

Hi. I think this patch largely makes sense, but I have some comments below. If you want to fix these and resend as a standalone patch I'm happy to apply that.

Quoted 1 line

1. MPIDR value (minus feature bits) is now directly stored in CPU

instance.

Quoted 2 lines

2. Default assignment is based on original rule (8 CPUs per cluster).
3. If KVM is used, MPIDR values are overridden with those from KVM. This

is

Quoted 3 lines

necessary for
proper KVM PSCI functioning.
4. Some cosmetic changes which would make further expansion of this code

easier.

Quoted 1 line

5. Added some debugging macros because CPU ID assignment is tricky part,

and if

Quoted 4 lines

there are
some problems, i think there should be a quick way to make sure they are correct.
This does not have an RFC mark because it is perfectly okay to be

committed

Quoted 4 lines

alone, it
does not break anything. Commit message follows. Cc'ed to all interested parties because i
really hope to get things going somewhere this time.

This sort of commentary belongs below the '---' line, so it doesn't end up in the final git commit message.

Quoted 5 lines

In order to support up to 128 cores with GIC-500 (GICv3 implementation) affinity1 must be used. GIC-500 support up to 32 clusters with up to 8 cores in a cluster. So for example, if one wishes to have 16 cores, the options are: 2 clusters of 8 cores each, 4 clusters with 4 cores each Currently only the first option is supported for TCG. However, KVM

expects

Quoted 1 line

to have the same clusters layout as host machine has. Therefore, if we

use

Quoted 4 lines

64-bit KVM we override CPU IDs with those provided by the KVM. For 32-bit systems it is OK to leave the default because so far we do not have more than 8 CPUs on any of them.
This implementation has a potential to be extended with some methods

which

Quoted 4 lines

would define cluster layout instead of hardcoded CPUS_PER_CLUSTER definition. This would allow to emulate real machines with different layouts. However, in case of KVM we would still have to inherit IDs from the host.

I agree that we want to be using the same idea of MPIDR as KVM when KVM is enabled, certainly. But this commit message has
a number of inaccuracies:
* KVM doesn't inherit IDs from the host
* we don't need to have the same cluster layout as the host machine
* this isn't specific to 64-bit VMs so we should fix 32 bits at the same
time

The bug we're actually trying to fix here is just that the kernel's mapping from VCPU ID to MPIDR value has changed from the simplistic implementation it originally had. So in userspace we need to read the MPIDR value back from the kernel rather than assuming we know the mapping. (It happens that the reason the kernel changed was to accommodate GICv3 and its restrictions on affinity values, but that's not relevant to why this patch is needed.)

hw/arm/virt.c static void fdt_add_cpu_nodes(const VirtBoardInfo

Quoted 16 lines
---
 hw/arm/virt.c        |  6 +++++-
 target-arm/cpu-qom.h |  3 +++
 target-arm/cpu.c     | 17 +++++++++++++++++
 target-arm/helper.c  |  9 +++------
 target-arm/kvm64.c   | 25 +++++++++++++++++++++++++
 target-arm/psci.c    | 20 ++++++++++++++++++--
 6 files changed, 71 insertions(+), 9 deletions(-)
hw/arm/virt.c +0 -0
index 05db8cb..19c8c3a 100644
@@ -294,7 +294,11 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo

*vbi)

hw/arm/virt.c static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)

Quoted 7 lines
hw/arm/virt.c +3 -1
                                         "enable-method", "psci");
         }
 
-        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
+        /*
+         * If cpus node's #address-cells property is set to 1
+         * The reg cell bits [23:0] must be set to bits [23:0] of

MPIDR_EL1.

hw/arm/virt.c static void fdt_add_cpu_nodes(const VirtBoardInfo · line 300

Quoted 1 line
hw/arm/virt.c +1 -0
+         */

This comment is either obvious or misleading depending on your point of view.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 162

Quoted 13 lines
hw/arm/virt.c +1 -0
+        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", armcpu->mpidr);
         g_free(nodename);
     }
 }
target-arm/cpu-qom.h +1 -0
index ed5a644..a382a09 100644
@@ -159,6 +159,7 @@ typedef struct ARMCPU {
     uint64_t id_aa64mmfr1;
     uint32_t dbgdidr;
     uint32_t clidr;
+    uint64_t mpidr; /* Without feature bits */

I think it would be less confusing to store the entire MPIDR here, and then mask it out if there are places that only want the affinity bits.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 175

Quoted 8 lines
target-arm/cpu-qom.h +1 -0
     /* The elements of this array are the CCSIDR values for each cache,
      * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
      */
@@ -171,6 +172,8 @@ typedef struct ARMCPU {
     uint64_t rvbar;
 } ARMCPU;
 
+#define CPUS_PER_CLUSTER 8

Changes I suggest below will mean this #define is only used in one place so it won't need to be in a header.

Quoted 9 lines
target-arm/cpu-qom.h +1 -0
+
 #define TYPE_AARCH64_CPU "aarch64-cpu"
 #define AARCH64_CPU_CLASS(klass) \
     OBJECT_CLASS_CHECK(AArch64CPUClass, (klass), TYPE_AARCH64_CPU)
target-arm/cpu.c +0 -0
index 4a888ab..7272466 100644

target-arm/cpu.c line 38

Quoted 8 lines
target-arm/cpu.c +5 -0
@@ -31,6 +31,12 @@
 #include "sysemu/kvm.h"
 #include "kvm_arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("armcpu: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif

Drop these debug macros, please.

Quoted 8 lines
target-arm/cpu.c +1 -0
+
 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -388,12 +394,23 @@ static void arm_cpu_initfn(Object *obj)
     CPUState *cs = CPU(obj);
     ARMCPU *cpu = ARM_CPU(obj);
     static bool inited;

target-arm/cpu.c static void arm_cpu_initfn(Object *obj) · line 406

Quoted 10 lines
target-arm/cpu.c +4 -0
+    uint32_t Aff1, Aff0;
 
     cs->env_ptr = &cpu->env;
     cpu_exec_init(&cpu->env);
     cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
                                          g_free, g_free);
 
+    /*
+     * We don't support setting cluster ID ([16..23]) (known as Aff2
+     * in later ARM ARM versions), or any of the higher affinity level

fields,

target-arm/cpu.c static void arm_cpu_initfn(Object *obj)

Quoted 6 lines
target-arm/cpu.c +6 -0
+     * so these bits always RAZ.
+     */
+    Aff1 = cs->cpu_index / CPUS_PER_CLUSTER;
+    Aff0 = cs->cpu_index % CPUS_PER_CLUSTER;
+    cpu->mpidr = (Aff1 << 8) | Aff0;
+    DPRINTF("Init(%p): index %d mpidr 0x%jX\n", cpu, cs->cpu_index,

cpu->mpidr);

target-arm/helper.c static const ARMCPRegInfo strongarm_cp_reginfo[]

Quoted 9 lines
target-arm/cpu.c +1 -0
+
 #ifndef CONFIG_USER_ONLY
     /* Our inbound IRQ and FIQ lines */
     if (kvm_enabled()) {
target-arm/helper.c +0 -0
index 1cc4993..99c7a30 100644
@@ -2063,12 +2063,9 @@ static const ARMCPRegInfo strongarm_cp_reginfo[]

= {

target-arm/helper.c static const ARMCPRegInfo strongarm_cp_reginfo[] = {

Quoted 7 lines
target-arm/helper.c +0 -4
 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
-    CPUState *cs = CPU(arm_env_get_cpu(env));
-    uint32_t mpidr = cs->cpu_index;
-    /* We don't support setting cluster ID ([8..11]) (known as Aff1
-     * in later ARM ARM versions), or any of the higher affinity level

fields,

Quoted 13 lines
target-arm/helper.c +3 -2
-     * so these bits always RAZ.
-     */
+    ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
+    uint64_t mpidr = cpu->mpidr;
+
     if (arm_feature(env, ARM_FEATURE_V7MP)) {
         mpidr |= (1U << 31);
         /* Cores which are uniprocessor (non-coherent)
target-arm/kvm64.c +0 -0
index 93c1ca8..99fa34e 100644

target-arm/kvm64.c line 29

Quoted 5 lines
target-arm/kvm64.c +2 -0
@@ -25,6 +25,12 @@
 #include "internals.h"
 #include "hw/arm/arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("arm-kvm64: " format , ##

__VA_ARGS__)

target-arm/kvm64.c bool kvm_arm_get_host_cpu_features(ARMHostCPUClass

Quoted 8 lines
target-arm/kvm64.c +4 -0
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif
+
 static inline void set_feature(uint64_t *features, int feature)
 {
     *features |= 1ULL << feature;
@@ -77,6 +83,10 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUClass

*ahcc)

target-arm/kvm64.c bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc) · line 88

Quoted 6 lines
target-arm/kvm64.c +3 -0
     return true;
 }
 
+#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFULL
+#define ARM_CPU_ID             3, 0, 0, 0
+#define ARM_CPU_ID_MPIDR       5

I don't see the point in separating the op2 value from the rest.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 121

Quoted 10 lines
target-arm/kvm64.c +3 -0
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
@@ -107,6 +117,21 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
+    /*
+     * When KVM is in use, psci is emulated in-kernel and not by qemu.

PSCI and QEMU should both be all-caps.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 122

Quoted 1 line
target-arm/kvm64.c +1 -0
+     * In order for it to work correctly we should use correct MPIDR

values,

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 123

Quoted 1 line
target-arm/kvm64.c +1 -0
+     * which appear to be inherited from the host.

I don't think they're inherited from the host: it's just that different host kernels use different mappings from vcpu ID to MPIDR (see reset_mpidr() in arch/arm64/kvm/sys_regs.c; its implementation has changed over time).
In any case we don't need to speculate; you can just say:

In order for it to work correctly we must use MPIDR values in the device tree which match the MPIDR values the kernel has picked for the vcpus, so ask KVM what those values are.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs)

Quoted 3 lines
target-arm/kvm64.c +3 -0
+     * So we override our defaults by asking KVM about these values.
+     */
+    ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID,

ARM_CPU_ID_MPIDR),

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 137

Quoted 11 lines
target-arm/kvm64.c +8 -0
+                          &cpu->mpidr);
+    if (ret) {
+        return ret;
+    }
+    cpu->mpidr &= ARM_MPIDR_HWID_BITMASK;
+    DPRINTF("Init(%p): index %d MPIDR 0x%jX\n", cpu,
+            cs->cpu_index, cpu->mpidr);
+
     return kvm_arm_init_cpreg_list(cpu);
 }
 

There should also be an equivalent patch to kvm32.c, because PSCI on 32-bit systems has the same issue.

Quoted 29 lines
target-arm/psci.c +16 -0
index d8fafab..d03896f 100644
@@ -72,6 +72,22 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
     }
 }
 
+static uint32_t mpidr_to_idx(uint64_t mpidr)
+{
+    uint32_t Aff1, Aff0;
+
+    /*
+     * MPIDR_EL1 [RES0:affinity-3:RES1:U:RES0:MT:affinity-1:affinity-0]
+     * GIC 500 code currently supports 32 clusters with 8 cores each
+     * but no more than 128 cores. Future version will have flexible
+     * affinity selection
+     * GIC 400 supports 8 cores so 0x7 for Aff0 is O.K. too
+     */
+     Aff1 = (mpidr & 0xff00) >> 8;
+     Aff0 = mpidr & 0xff;
+     return Aff1 * CPUS_PER_CLUSTER + Aff0;
+}
 
+
 void arm_handle_psci_call(ARMCPU *cpu)
 {
     /*

target-arm/psci.c void arm_handle_psci_call(ARMCPU *cpu) · line 140

Quoted 5 lines
target-arm/psci.c +1 -1
@@ -121,7 +137,7 @@ void arm_handle_psci_call(ARMCPU *cpu)
 
         switch (param[2]) {
         case 0:
-            target_cpu_state = qemu_get_cpu(mpidr & 0xff);
+            target_cpu_state = qemu_get_cpu(mpidr_to_idx(mpidr));

Rather than doing this, you can just have an
arm_get_cpu_by_mpidr() which does

    CPU_FOREACH(cs) {
        ARMCPU *cpu = ARM_CPU(cs);

        if (cpu->mpidr == mpidr) {
            return cpu;
        }
    }
    return NULL;

ie same as qemu_get_cpu() but checking on mpidr rather than cpu_index. Then we don't need to make assumptions about the mpidr-to-index mapping here at all (and this code will continue to work if in future we decide to expose the cluster topology via CPU properties for the board to set.)

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Hi!

Quoted 1 line

I think that Pavel's needs to address all the issues in the original thread.

Ok. I have read the thread and i'll prepare a fixed version of this shortly.

Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia

#5 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

In reply to Peter Maydell
Quoted 3 lines

I think it would be less confusing to store the entire MPIDR here, and then mask it out if there are places that only want the affinity bits.

I thought about it when i developed this, but in my implementation affinity bits are assigned during CPU instantiation, while feature bits can be added later, during realize. I could tweak set_feature() to sync up MPIDR value but perhaps it isn't the best thing to do. Additionally, currently we support only ARM_FEATURE_V7MP, which is 32-bit only. I believe this would be a more complex modification which would touch more of 32-bit code.
So would it be acceptable to leave mpidr handling as it is?

Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia

#6 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Quoted 8 lines · Pavel Fedin
Quoted 3 lines

I think it would be less confusing to store the entire MPIDR here, and then mask it out if there are places that only want the affinity bits.

I thought about it when i developed this, but in my implementation
affinity bits are assigned during CPU instantiation, while feature bits can be added later, during realize. I could tweak set_feature() to sync up MPIDR value but perhaps it isn't the best thing to do.

Can we not just assign the whole thing at realize?

Quoted 3 lines

Additionally, currently we support only ARM_FEATURE_V7MP, which is
32-bit only. I believe this would be a more complex modification which would touch more of 32-bit code.

It's only 32-bit in the sense that all 64-bit systems (indeed, all v8 systems) have the feature and so the bit is effectively RES1.

You need to fix the 32-bit side anyway.

Quoted 1 line

So would it be acceptable to leave mpidr handling as it is?

I still think that having the whole mpidr in the struct will be less confusing.

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Hello!

Quoted 6 lines
Quoted 4 lines

I thought about it when i developed this, but in my implementation
affinity bits are assigned during CPU instantiation, while feature bits can be added later, during realize. I could tweak set_feature() to sync up MPIDR value but perhaps it isn't the best thing to do.

Can we not just assign the whole thing at realize?

kvm_arch_init_vcpu() is called before realize, therefore i would have to track down whether KVM is active or not.

Quoted 1 line

You need to fix the 32-bit side anyway.

Actually already done in my working tree.

Quoted 2 lines

I still think that having the whole mpidr in the struct will be less confusing.

Ok, if you are really insisting on that, i can assign IDs where this is currently done and add feature bits in arm_cpu_realizefn().
Last argument: the rest of qemu code (property assignment, lookup in PSCI, etc) actually needs IDs without feature bits. The only function which needs full version is mpidr_read(). So does it still worth of putting AND with bitmasks everywhere? May be just rename 'mpidr' to something like 'mp_id' ?

Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia

#8 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

Quoted 5 lines · Pavel Fedin

Last argument: the rest of qemu code (property assignment, lookup
in PSCI, etc) actually needs IDs without feature bits. The only function which needs full version is mpidr_read(). So does it still worth of putting AND with bitmasks everywhere? May be just rename 'mpidr' to something like 'mp_id' ?

Mmm, OK, if we don't call it mpidr I think this will be OK. I suggest "mp_affinity".

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

In reply to Pavel Fedin
Quoted 10 lines · On Mon, 08 Jun 2015 12:39:56 +0300 Pavel Fedin <[email protected]> wrote:

Hello!

Quoted 6 lines
Quoted 4 lines

I thought about it when i developed this, but in my implementation
affinity bits are assigned during CPU instantiation, while feature bits can be added later, during realize. I could tweak set_feature() to sync up MPIDR value but perhaps it isn't the best thing to do.

Can we not just assign the whole thing at realize?

kvm_arch_init_vcpu() is called before realize, therefore i would have to track down whether KVM is active or not.

that's because call flow looks wrong.
If KVM would be fixed to use QEMU's mpidr then you can like x86 target

1. create vcpu
2. set mpidr property on it from board code
3. call cpu.realize() -> which will push mpidr to KVM

Quoted 16 lines
Quoted 2 lines

You need to fix the 32-bit side anyway.

Actually already done in my working tree.

Quoted 2 lines

I still think that having the whole mpidr in the struct will be less confusing.

Ok, if you are really insisting on that, i can assign IDs where this is currently done and add feature bits in arm_cpu_realizefn().
Last argument: the rest of qemu code (property assignment, lookup in PSCI, etc) actually needs IDs without feature bits. The only function which needs full version is mpidr_read(). So does it still worth of putting AND with bitmasks everywhere? May be just rename 'mpidr' to something like 'mp_id' ?

Kind regards,
Pavel Fedin
Expert Engineer
Samsung Electronics Research center Russia

#10 lore raw

Re: [Qemu-devel] [PATCH RFC V3 1/4] Use Aff1 with mpidr This is an improved and KVM-aware alternative to

In reply to Peter Maydell
Quoted 127 lines · On Thu, 4 Jun 2015 18:17:39 +0100 Peter Maydell <[email protected]>…
Quoted 1 line · Shlomo Pongratz

From: Pavel Fedin <[email protected]>

Hi. I think this patch largely makes sense, but I have some comments below. If you want to fix these and resend as a standalone patch I'm happy to apply that.

Quoted 7 lines

1. MPIDR value (minus feature bits) is now directly stored in CPU instance.
2. Default assignment is based on original rule (8 CPUs per cluster).
3. If KVM is used, MPIDR values are overridden with those from KVM. This is necessary for
proper KVM PSCI functioning.
4. Some cosmetic changes which would make further expansion of this code easier.
5. Added some debugging macros because CPU ID assignment is tricky part, and if

Quoted 8 lines

there are
some problems, i think there should be a quick way to make sure they are correct.
This does not have an RFC mark because it is perfectly okay to be committed
alone, it
does not break anything. Commit message follows. Cc'ed to all interested parties because i
really hope to get things going somewhere this time.

This sort of commentary belongs below the '---' line, so it doesn't end up in the final git commit message.

Quoted 6 lines

In order to support up to 128 cores with GIC-500 (GICv3 implementation) affinity1 must be used. GIC-500 support up to 32 clusters with up to 8 cores in a cluster. So for example, if one wishes to have 16 cores, the options are: 2 clusters of 8 cores each, 4 clusters with 4 cores each Currently only the first option is supported for TCG. However, KVM expects to have the same clusters layout as host machine has. Therefore, if we use

Quoted 8 lines

64-bit KVM we override CPU IDs with those provided by the KVM. For 32-bit systems it is OK to leave the default because so far we do not have more than 8 CPUs on any of them.
This implementation has a potential to be extended with some methods which would define cluster layout instead of hardcoded CPUS_PER_CLUSTER definition. This would allow to emulate real machines with different layouts. However, in case of KVM we would still have to inherit IDs from the host.

I agree that we want to be using the same idea of MPIDR as KVM when KVM is enabled, certainly. But this commit message has
a number of inaccuracies:
* KVM doesn't inherit IDs from the host
* we don't need to have the same cluster layout as the host machine
* this isn't specific to 64-bit VMs so we should fix 32 bits at the same time

The bug we're actually trying to fix here is just that the kernel's mapping from VCPU ID to MPIDR value has changed from the simplistic implementation it originally had. So in userspace we need to read the MPIDR value back from the kernel rather than assuming we know the mapping. (It happens that the reason the kernel changed was to accommodate GICv3 and its restrictions on affinity values, but that's not relevant to why this patch is needed.)

Quoted 16 lines
---
 hw/arm/virt.c        |  6 +++++-
 target-arm/cpu-qom.h |  3 +++
 target-arm/cpu.c     | 17 +++++++++++++++++
 target-arm/helper.c  |  9 +++------
 target-arm/kvm64.c   | 25 +++++++++++++++++++++++++
 target-arm/psci.c    | 20 ++++++++++++++++++--
 6 files changed, 71 insertions(+), 9 deletions(-)
hw/arm/virt.c +0 -0
index 05db8cb..19c8c3a 100644

hw/arm/virt.c static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi) · line 300

Quoted 8 lines
hw/arm/virt.c +4 -1
@@ -294,7 +294,11 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
                                         "enable-method", "psci");
         }
 
-        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", cpu);
+        /*
+         * If cpus node's #address-cells property is set to 1
+         * The reg cell bits [23:0] must be set to bits [23:0] of MPIDR_EL1.
+         */

This comment is either obvious or misleading depending on your point of view.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 162

Quoted 13 lines
hw/arm/virt.c +1 -0
+        qemu_fdt_setprop_cell(vbi->fdt, nodename, "reg", armcpu->mpidr);
         g_free(nodename);
     }
 }
target-arm/cpu-qom.h +1 -0
index ed5a644..a382a09 100644
@@ -159,6 +159,7 @@ typedef struct ARMCPU {
     uint64_t id_aa64mmfr1;
     uint32_t dbgdidr;
     uint32_t clidr;
+    uint64_t mpidr; /* Without feature bits */

I think it would be less confusing to store the entire MPIDR here, and then mask it out if there are places that only want the affinity bits.

target-arm/cpu-qom.h typedef struct ARMCPU { · line 175

Quoted 8 lines
target-arm/cpu-qom.h +1 -0
     /* The elements of this array are the CCSIDR values for each cache,
      * in the order L1DCache, L1ICache, L2DCache, L2ICache, etc.
      */
@@ -171,6 +172,8 @@ typedef struct ARMCPU {
     uint64_t rvbar;
 } ARMCPU;
 
+#define CPUS_PER_CLUSTER 8

Changes I suggest below will mean this #define is only used in one place so it won't need to be in a header.

Quoted 12 lines
target-arm/cpu-qom.h +1 -0
+
 #define TYPE_AARCH64_CPU "aarch64-cpu"
 #define AARCH64_CPU_CLASS(klass) \
     OBJECT_CLASS_CHECK(AArch64CPUClass, (klass), TYPE_AARCH64_CPU)
target-arm/cpu.c +0 -0
index 4a888ab..7272466 100644
@@ -31,6 +31,12 @@
 #include "sysemu/kvm.h"
 #include "kvm_arm.h"
 
Quoted 7 lines

target-arm/cpu.c line 38

Quoted 5 lines
target-arm/cpu.c +5 -0
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("armcpu: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif

Drop these debug macros, please.

or if it's useful then use trace-points instead, which could be turned on/off without recompiling.

Quoted 98 lines
Quoted 67 lines
target-arm/cpu.c +12 -0
+
 static void arm_cpu_set_pc(CPUState *cs, vaddr value)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -388,12 +394,23 @@ static void arm_cpu_initfn(Object *obj)
     CPUState *cs = CPU(obj);
     ARMCPU *cpu = ARM_CPU(obj);
     static bool inited;
+    uint32_t Aff1, Aff0;
 
     cs->env_ptr = &cpu->env;
     cpu_exec_init(&cpu->env);
     cpu->cp_regs = g_hash_table_new_full(g_int_hash, g_int_equal,
                                          g_free, g_free);
 
+    /*
+     * We don't support setting cluster ID ([16..23]) (known as Aff2
+     * in later ARM ARM versions), or any of the higher affinity level fields,
+     * so these bits always RAZ.
+     */
+    Aff1 = cs->cpu_index / CPUS_PER_CLUSTER;
+    Aff0 = cs->cpu_index % CPUS_PER_CLUSTER;
+    cpu->mpidr = (Aff1 << 8) | Aff0;
+    DPRINTF("Init(%p): index %d mpidr 0x%jX\n", cpu, cs->cpu_index, cpu->mpidr);
+
 #ifndef CONFIG_USER_ONLY
     /* Our inbound IRQ and FIQ lines */
     if (kvm_enabled()) {
target-arm/helper.c +3 -6
index 1cc4993..99c7a30 100644
@@ -2063,12 +2063,9 @@ static const ARMCPRegInfo strongarm_cp_reginfo[] = {
 
 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
-    CPUState *cs = CPU(arm_env_get_cpu(env));
-    uint32_t mpidr = cs->cpu_index;
-    /* We don't support setting cluster ID ([8..11]) (known as Aff1
-     * in later ARM ARM versions), or any of the higher affinity level fields,
-     * so these bits always RAZ.
-     */
+    ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
+    uint64_t mpidr = cpu->mpidr;
+
     if (arm_feature(env, ARM_FEATURE_V7MP)) {
         mpidr |= (1U << 31);
         /* Cores which are uniprocessor (non-coherent)
target-arm/kvm64.c +5 -0
index 93c1ca8..99fa34e 100644
@@ -25,6 +25,12 @@
 #include "internals.h"
 #include "hw/arm/arm.h"
 
+#ifdef DEBUG
+# define DPRINTF(format, ...) printf("arm-kvm64: " format , ## __VA_ARGS__)
+#else
+# define DPRINTF(format, ...) do { } while (0)
+#endif
 
 static inline void set_feature(uint64_t *features, int feature)
 {
     *features |= 1ULL << feature;

target-arm/kvm64.c bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc) · line 88

Quoted 6 lines
target-arm/kvm64.c +3 -0
@@ -77,6 +83,10 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
     return true;
 }
 
+#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFULL
+#define ARM_CPU_ID             3, 0, 0, 0
+#define ARM_CPU_ID_MPIDR       5

I don't see the point in separating the op2 value from the rest.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 121

Quoted 10 lines
target-arm/kvm64.c +3 -0
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
@@ -107,6 +117,21 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
+    /*
+     * When KVM is in use, psci is emulated in-kernel and not by qemu.

PSCI and QEMU should both be all-caps.

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 123

Quoted 2 lines
target-arm/kvm64.c +2 -0
+     * In order for it to work correctly we should use correct MPIDR values,
+     * which appear to be inherited from the host.

I don't think they're inherited from the host: it's just that different host kernels use different mappings from vcpu ID to MPIDR (see reset_mpidr() in arch/arm64/kvm/sys_regs.c; its implementation has changed over time).
In any case we don't need to speculate; you can just say:

Quoted 3 lines

In order for it to work correctly we must use MPIDR values in the device tree which match the MPIDR values the kernel has picked for the vcpus, so ask KVM what those values are.

Could we set QEMU's generated mpidr in kernel instead of pulling it from kernel, like we do with APIC ID in x86 and fix kernel not to reset it its own value (i.e. untie mpidr from vcpuid)?

Then later we could move setting mpidr from kvm_arch_init_vcpu() into board code which should be setting it, since it knows/defines what topology it has.

Quoted 72 lines
Quoted 4 lines
target-arm/kvm64.c +4 -0
+     * So we override our defaults by asking KVM about these values.
+     */
+    ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR),
+                          &cpu->mpidr);

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 137

Quoted 10 lines
target-arm/kvm64.c +7 -0
+    if (ret) {
+        return ret;
+    }
+    cpu->mpidr &= ARM_MPIDR_HWID_BITMASK;
+    DPRINTF("Init(%p): index %d MPIDR 0x%jX\n", cpu,
+            cs->cpu_index, cpu->mpidr);
+
     return kvm_arm_init_cpreg_list(cpu);
 }
 

There should also be an equivalent patch to kvm32.c, because PSCI on 32-bit systems has the same issue.

Quoted 29 lines
target-arm/psci.c +16 -0
index d8fafab..d03896f 100644
@@ -72,6 +72,22 @@ bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
     }
 }
 
+static uint32_t mpidr_to_idx(uint64_t mpidr)
+{
+    uint32_t Aff1, Aff0;
+
+    /*
+     * MPIDR_EL1 [RES0:affinity-3:RES1:U:RES0:MT:affinity-1:affinity-0]
+     * GIC 500 code currently supports 32 clusters with 8 cores each
+     * but no more than 128 cores. Future version will have flexible
+     * affinity selection
+     * GIC 400 supports 8 cores so 0x7 for Aff0 is O.K. too
+     */
+     Aff1 = (mpidr & 0xff00) >> 8;
+     Aff0 = mpidr & 0xff;
+     return Aff1 * CPUS_PER_CLUSTER + Aff0;
+}
 
+
 void arm_handle_psci_call(ARMCPU *cpu)
 {
     /*

target-arm/psci.c void arm_handle_psci_call(ARMCPU *cpu) · line 140

Quoted 5 lines
target-arm/psci.c +1 -1
@@ -121,7 +137,7 @@ void arm_handle_psci_call(ARMCPU *cpu)
 
         switch (param[2]) {
         case 0:
-            target_cpu_state = qemu_get_cpu(mpidr & 0xff);
+            target_cpu_state = qemu_get_cpu(mpidr_to_idx(mpidr));

Rather than doing this, you can just have an
arm_get_cpu_by_mpidr() which does

    CPU_FOREACH(cs) {
        ARMCPU *cpu = ARM_CPU(cs);

        if (cpu->mpidr == mpidr) {
            return cpu;
        }
    }
    return NULL;

ie same as qemu_get_cpu() but checking on mpidr rather than cpu_index. Then we don't need to make assumptions about the mpidr-to-index mapping here at all (and this code will continue to work if in future we decide to expose the cluster topology via CPU properties for the board to set.)

thanks
-- PMM

#11 lore raw

RFC PATCH v3 2/4 Implment GIC-500

In reply to Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

Implement GIC-500 from GICv3 family for arm64

This patch is a first step toward 128 cores support for arm64.

At first only 64 cores are supported for two reasons:
First the largest integer type has the size of 64 bits and modifying essential data structures in order to support 128 cores will require the usage of bitops.
Second currently the Linux (kernel) can be configured to support up to 64 cores thus there is no urgency with 128 cores support.

Things left to do:

Currently the booting Linux may got stuck. The probability of getting stuck increases with the number of cores. I'll appreciate core review.

---
 hw/intc/Makefile.objs              |    2 +
 hw/intc/arm_gicv3.c                | 2086 ++++++++++++++++++++++++++++++++++++
 hw/intc/arm_gicv3_common.c         |  217 ++++
 hw/intc/gicv3_internal.h           |  161 +++
 include/hw/intc/arm_gicv3.h        |   44 +
 include/hw/intc/arm_gicv3_common.h |  119 ++
 6 files changed, 2629 insertions(+)
 create mode 100644 hw/intc/arm_gicv3.c
 create mode 100644 hw/intc/arm_gicv3_common.c
 create mode 100644 hw/intc/gicv3_internal.h
 create mode 100644 include/hw/intc/arm_gicv3.h
 create mode 100644 include/hw/intc/arm_gicv3_common.h
hw/intc/Makefile.objs +2 -0
index 843864a..41fe9ec 100644
@@ -11,6 +11,8 @@ common-obj-$(CONFIG_SLAVIO) += slavio_intctl.o
 common-obj-$(CONFIG_IOAPIC) += ioapic_common.o
 common-obj-$(CONFIG_ARM_GIC) += arm_gic_common.o
 common-obj-$(CONFIG_ARM_GIC) += arm_gic.o
+common-obj-$(CONFIG_ARM_GIC) += arm_gicv3_common.o
+common-obj-$(CONFIG_ARM_GIC) += arm_gicv3.o
 common-obj-$(CONFIG_OPENPIC) += openpic.o
 
 obj-$(CONFIG_APIC) += apic.o apic_common.o
hw/intc/arm_gicv3.c new file +2086 -0

2,086 lines not on this page · raw message

hw/intc/arm_gicv3_common.c new file +217 -0

217 lines not on this page · raw message

hw/intc/gicv3_internal.h new file +161 -0

161 lines not on this page · raw message

include/hw/intc/arm_gicv3.h new file +44 -0

44 lines not on this page · raw message

include/hw/intc/arm_gicv3_common.h new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
1.9.1
#12 lore raw

RFC PATCH v3 3/4 GICv3 support

In reply to Shlomo Pongratz

From: Shlomo Pongratz <[email protected]>

Add system instructions used by the Linux (kernel) GICv3 device driver

---
 target-arm/cpu.h   |  12 ++++++
 target-arm/cpu64.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
target-arm/cpu.h +12 -0
index 21b5b8e..810490d 100644
@@ -1013,6 +1013,18 @@ void armv7m_nvic_set_pending(void *opaque, int irq);
 int armv7m_nvic_acknowledge_irq(void *opaque);
 void armv7m_nvic_complete_irq(void *opaque, int irq);
 
+void armv8_gicv3_set_sgi(void *opaque, int cpuindex, uint64_t value);
+uint64_t armv8_gicv3_acknowledge_irq(void *opaque, int cpuindex,
+                              MemTxAttrs attrs);
+void armv8_gicv3_complete_irq(void *opaque, int cpuindex, int irq,
+                              MemTxAttrs attrs);
+uint64_t armv8_gicv3_get_priority_mask(void *opaque, int cpuindex);
+void armv8_gicv3_set_priority_mask(void *opaque, int cpuindex, uint32_t mask);
+uint64_t armv8_gicv3_get_sre(void *opaque);
+void armv8_gicv3_set_sre(void *opaque, uint64_t sre);
+uint64_t armv8_gicv3_get_igrpen1(void *opaque, int cpuindex);
+void armv8_gicv3_set_igrpen1(void *opaque, int cpuindex, uint64_t igrpen1);
+
 /* Interface for defining coprocessor registers.
  * Registers are defined in tables of arm_cp_reginfo structs
  * which are passed to define_arm_cp_regs().
target-arm/cpu64.c +105 -0

117 lines not on this page · raw message

Signature · 1 line
1.9.1
#13 lore raw

RFC PATCH v3 4/4 Add virt-v3 machine that uses GIC-500

In reply to Shlomo Pongratz

From: Pavel Fedin <[email protected]>

I would like to offer this, slightly improved implementation. The key thing is a new kernel_irqchip_type member in Machine class. Currently it it used only by virt machine for its internal purposes, however in future it is to be passed to KVM in kvm_irqchip_create(). The variable is defined as int in order to be architecture agnostic, for potential future users.

---
 hw/arm/virt.c         | 159 +++++++++++++++++++++++++++++++++++++++++---------
 include/hw/arm/virt.h |   4 ++
 include/hw/boards.h   |   1 +
 3 files changed, 135 insertions(+), 29 deletions(-)
hw/arm/virt.c +130 -29
index 19c8c3a..22a39b4 100644
@@ -45,6 +45,7 @@
 #include "qemu/error-report.h"
 #include "hw/pci-host/gpex.h"
 #include "hw/arm/virt-acpi-build.h"
+#include "linux/kvm.h" /* For KVM_DEV_TYPE_ARM_VGIC_V{2|3} */
 
 /* Number of external interrupt lines to configure the GIC with */
 #define NUM_IRQS 128
@@ -58,7 +59,7 @@
 #define GIC_FDT_IRQ_FLAGS_LEVEL_LO 8
 
 #define GIC_FDT_IRQ_PPI_CPU_START 8
-#define GIC_FDT_IRQ_PPI_CPU_WIDTH 8
+#define GIC_FDT_IRQ_PPI_CPU_WIDTH 24
 
 typedef struct VirtBoardInfo {
     struct arm_boot_info bootinfo;
@@ -69,6 +70,7 @@ typedef struct VirtBoardInfo {
     void *fdt;
     int fdt_size;
     uint32_t clock_phandle;
+    const char *class_name;
 } VirtBoardInfo;
 
 typedef struct {
@@ -82,6 +84,7 @@ typedef struct {
 } VirtMachineState;
 
 #define TYPE_VIRT_MACHINE   "virt"
+#define TYPE_VIRTV3_MACHINE   "virt-v3"
 #define VIRT_MACHINE(obj) \
     OBJECT_CHECK(VirtMachineState, (obj), TYPE_VIRT_MACHINE)
 #define VIRT_MACHINE_GET_CLASS(obj) \
@@ -103,20 +106,24 @@ typedef struct {
  */
 static const MemMapEntry a15memmap[] = {
     /* Space up to 0x8000000 is reserved for a boot ROM */
-    [VIRT_FLASH] =      {          0, 0x08000000 },
-    [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
+    [VIRT_FLASH] =           {          0, 0x08000000 },
+    [VIRT_CPUPERIPHS] =      { 0x08000000, 0x00020000 },
     /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
-    [VIRT_GIC_DIST] =   { 0x08000000, 0x00010000 },
-    [VIRT_GIC_CPU] =    { 0x08010000, 0x00010000 },
-    [VIRT_UART] =       { 0x09000000, 0x00001000 },
-    [VIRT_RTC] =        { 0x09010000, 0x00001000 },
-    [VIRT_FW_CFG] =     { 0x09020000, 0x0000000a },
-    [VIRT_MMIO] =       { 0x0a000000, 0x00000200 },
+    [VIRT_GIC_DIST] =        { 0x08000000, 0x00010000 },
+    [VIRT_GIC_CPU] =         { 0x08010000, 0x00010000 },
+    /* On v3 VIRT_GIC_DIST_SPI takes place of VIRT_GIC_CPU */
+    [VIRT_ITS_CONTROL] =     { 0x08020000, 0x00010000 },
+    [VIRT_ITS_TRANSLATION] = { 0x08030000, 0x00010000 },
+    [VIRT_LPI] =             { 0x08040000, 0x00800000 },
+    [VIRT_UART] =            { 0x09000000, 0x00001000 },
+    [VIRT_RTC] =             { 0x09010000, 0x00001000 },
+    [VIRT_FW_CFG] =          { 0x09020000, 0x0000000a },
+    [VIRT_MMIO] =            { 0x0a000000, 0x00000200 },
     /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
-    [VIRT_PCIE_MMIO] =  { 0x10000000, 0x2eff0000 },
-    [VIRT_PCIE_PIO] =   { 0x3eff0000, 0x00010000 },
-    [VIRT_PCIE_ECAM] =  { 0x3f000000, 0x01000000 },
-    [VIRT_MEM] =        { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
+    [VIRT_PCIE_MMIO] =       { 0x10000000, 0x2eff0000 },
+    [VIRT_PCIE_PIO] =        { 0x3eff0000, 0x00010000 },
+    [VIRT_PCIE_ECAM] =       { 0x3f000000, 0x01000000 },
+    [VIRT_MEM] =             { 0x40000000, 30ULL * 1024 * 1024 * 1024 },
 };
 
 static const int a15irqmap[] = {
@@ -131,16 +138,25 @@ static VirtBoardInfo machines[] = {
         .cpu_model = "cortex-a15",
         .memmap = a15memmap,
         .irqmap = a15irqmap,
+        .class_name = TYPE_ARM_CPU,
+    },
+    {
+        .cpu_model = "cortex-a53",
+        .memmap = a15memmap,
+        .irqmap = a15irqmap,
+        .class_name = TYPE_AARCH64_CPU,
     },
     {
         .cpu_model = "cortex-a57",
         .memmap = a15memmap,
         .irqmap = a15irqmap,
+        .class_name = TYPE_AARCH64_CPU,
     },
     {
         .cpu_model = "host",
         .memmap = a15memmap,
         .irqmap = a15irqmap,
+        .class_name = TYPE_ARM_CPU,
     },
 };
 
@@ -209,7 +225,6 @@ static void fdt_add_psci_node(const VirtBoardInfo *vbi)
     if (armcpu->psci_version == 2) {
         const char comp[] = "arm,psci-0.2\0arm,psci";
         qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
-
         cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
         if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
             cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
@@ -222,7 +237,6 @@ static void fdt_add_psci_node(const VirtBoardInfo *vbi)
         }
     } else {
         qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
-
         cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
         cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
         cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
@@ -249,10 +263,13 @@ static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
      * they are edge-triggered.
      */
     ARMCPU *armcpu;
+    uint32_t max;
     uint32_t irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
 
+    /* Argument is 32 bit but 8 bits are reserved for flags */
+    max = (vbi->smp_cpus >= 24) ? 24 : vbi->smp_cpus;
     irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
-                         GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << vbi->smp_cpus) - 1);
+                         GIC_FDT_IRQ_PPI_CPU_WIDTH, (1 << max) - 1);
 
     qemu_fdt_add_subnode(vbi->fdt, "/timer");
 
@@ -276,6 +293,18 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
 {
     int cpu;
 
+    /*
+     * From Documentation/devicetree/bindings/arm/cpus.txt
+     *  On ARM v8 64-bit systems value should be set to 2,
+     *  that corresponds to the MPIDR_EL1 register size.
+     *  If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
+     *  in the system, #address-cells can be set to 1, since
+     *  MPIDR_EL1[63:32] bits are not used for CPUs
+     *  identification.
+     *
+     *  Now GIC500 doesn't support affinities 2 & 3 so currently
+     *  #address-cells can stay 1 until future GIC
+     */
     qemu_fdt_add_subnode(vbi->fdt, "/cpus");
     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#address-cells", 0x1);
     qemu_fdt_setprop_cell(vbi->fdt, "/cpus", "#size-cells", 0x0);
@@ -303,7 +332,7 @@ static void fdt_add_cpu_nodes(const VirtBoardInfo *vbi)
     }
 }
 
-static uint32_t fdt_add_gic_node(const VirtBoardInfo *vbi)
+static uint32_t fdt_add_gic_node(const VirtBoardInfo *vbi, int type)
 {
     uint32_t gic_phandle;
 
@@ -311,35 +340,65 @@ static uint32_t fdt_add_gic_node(const VirtBoardInfo *vbi)
     qemu_fdt_setprop_cell(vbi->fdt, "/", "interrupt-parent", gic_phandle);
 
     qemu_fdt_add_subnode(vbi->fdt, "/intc");
-    /* 'cortex-a15-gic' means 'GIC v2' */
-    qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
-                            "arm,cortex-a15-gic");
     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "#interrupt-cells", 3);
     qemu_fdt_setprop(vbi->fdt, "/intc", "interrupt-controller", NULL, 0);
-    qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
+    if (type == KVM_DEV_TYPE_ARM_VGIC_V3) {
+        qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
+                                "arm,gic-v3");
+        qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
+                                     2, vbi->memmap[VIRT_GIC_DIST].base,
+                                     2, vbi->memmap[VIRT_GIC_DIST].size,
+#if 0                                /* Currently no need for SPI & ITS */
+                                     2, vbi->memmap[VIRT_GIC_DIST_SPI].base,
+                                     2, vbi->memmap[VIRT_GIC_DIST_SPI].size,
+                                     2, vbi->memmap[VIRT_ITS_CONTROL].base,
+                                     2, vbi->memmap[VIRT_ITS_CONTROL].size,
+                                     2, vbi->memmap[VIRT_ITS_TRANSLATION].base,
+                                     2, vbi->memmap[VIRT_ITS_TRANSLATION].size,
+#endif
+                                     2, vbi->memmap[VIRT_LPI].base,
+                                     2, vbi->memmap[VIRT_LPI].size);
+    } else {
+        /* 'cortex-a15-gic' means 'GIC v2' */
+        qemu_fdt_setprop_string(vbi->fdt, "/intc", "compatible",
+                                "arm,cortex-a15-gic");
+        qemu_fdt_setprop_sized_cells(vbi->fdt, "/intc", "reg",
                                      2, vbi->memmap[VIRT_GIC_DIST].base,
                                      2, vbi->memmap[VIRT_GIC_DIST].size,
                                      2, vbi->memmap[VIRT_GIC_CPU].base,
                                      2, vbi->memmap[VIRT_GIC_CPU].size);
+    }
     qemu_fdt_setprop_cell(vbi->fdt, "/intc", "phandle", gic_phandle);
 
     return gic_phandle;
 }
 
-static uint32_t create_gic(const VirtBoardInfo *vbi, qemu_irq *pic)
+static uint32_t create_gic(const VirtBoardInfo *vbi, qemu_irq *pic, int type)
 {
     /* We create a standalone GIC v2 */
     DeviceState *gicdev;
     SysBusDevice *gicbusdev;
-    const char *gictype = "arm_gic";
+    const char *gictype;
     int i;
 
-    if (kvm_irqchip_in_kernel()) {
+    if (type == KVM_DEV_TYPE_ARM_VGIC_V3) {
+        gictype = "arm_gicv3";
+    } else if (kvm_irqchip_in_kernel()) {
         gictype = "kvm-arm-gic";
+    } else {
+        gictype = "arm_gic";
     }
 
     gicdev = qdev_create(NULL, gictype);
-    qdev_prop_set_uint32(gicdev, "revision", 2);
+
+    for (i = 0; i < vbi->smp_cpus; i++) {
+        CPUState *cpu = qemu_get_cpu(i);
+        CPUARMState *env = cpu->env_ptr;
+        env->nvic = gicdev;
+    }
+
+    qdev_prop_set_uint32(gicdev, "revision",
+                         type == KVM_DEV_TYPE_ARM_VGIC_V3 ? 3 : 2);
     qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
     /* Note that the num-irq property counts both internal and external
      * interrupts; there are always 32 of the former (mandated by GIC spec).
@@ -349,6 +408,11 @@ static uint32_t create_gic(const VirtBoardInfo *vbi, qemu_irq *pic)
     gicbusdev = SYS_BUS_DEVICE(gicdev);
     sysbus_mmio_map(gicbusdev, 0, vbi->memmap[VIRT_GIC_DIST].base);
     sysbus_mmio_map(gicbusdev, 1, vbi->memmap[VIRT_GIC_CPU].base);
+    if (type == KVM_DEV_TYPE_ARM_VGIC_V3) {
+        sysbus_mmio_map(gicbusdev, 2, vbi->memmap[VIRT_ITS_CONTROL].base);
+        sysbus_mmio_map(gicbusdev, 3, vbi->memmap[VIRT_ITS_TRANSLATION].base);
+        sysbus_mmio_map(gicbusdev, 4, vbi->memmap[VIRT_LPI].base);
+    }
 
     /* Wire the outputs from each CPU's generic timer to the
      * appropriate GIC PPI inputs, and the GIC's IRQ output to
@@ -375,7 +439,7 @@ static uint32_t create_gic(const VirtBoardInfo *vbi, qemu_irq *pic)
         pic[i] = qdev_get_gpio_in(gicdev, i);
     }
 
-    return fdt_add_gic_node(vbi);
+    return fdt_add_gic_node(vbi, type);
 }
 
 static void create_uart(const VirtBoardInfo *vbi, qemu_irq *pic)
@@ -748,7 +812,7 @@ static void machvirt_init(MachineState *machine)
     create_fdt(vbi);
 
     for (n = 0; n < smp_cpus; n++) {
-        ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
+        ObjectClass *oc = cpu_class_by_name(vbi->class_name, cpustr[0]);
         CPUClass *cc = CPU_CLASS(oc);
         Object *cpuobj;
         Error *err = NULL;
@@ -798,7 +862,7 @@ static void machvirt_init(MachineState *machine)
 
     create_flash(vbi);
 
-    gic_phandle = create_gic(vbi, pic);
+    gic_phandle = create_gic(vbi, pic, machine->kernel_irqchip_type);
 
     create_uart(vbi, pic);
 
@@ -848,7 +912,7 @@ static void virt_set_secure(Object *obj, bool value, Error **errp)
     vms->secure = value;
 }
 
-static void virt_instance_init(Object *obj)
+static void virt_instance_init_common(Object *obj)
 {
     VirtMachineState *vms = VIRT_MACHINE(obj);
 
@@ -862,6 +926,14 @@ static void virt_instance_init(Object *obj)
                                     NULL);
 }
 
+static void virt_instance_init(Object *obj)
+{
+    MachineState *ms = MACHINE(obj);
+
+    ms->kernel_irqchip_type = KVM_DEV_TYPE_ARM_VGIC_V2;
+    virt_instance_init_common(obj);
+}
+
 static void virt_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -881,9 +953,38 @@ static const TypeInfo machvirt_info = {
     .class_init = virt_class_init,
 };
 
+static void virtv3_instance_init(Object *obj)
+{
+    MachineState *ms = MACHINE(obj);
+
+    ms->kernel_irqchip_type = KVM_DEV_TYPE_ARM_VGIC_V3;
+    virt_instance_init_common(obj);
+}
+
+static void virtv3_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+
+    mc->name = TYPE_VIRTV3_MACHINE;
+    mc->desc = "ARM Virtual Machine with GICv3",
+    mc->init = machvirt_init;
+    /* With gic3 full implementation (with bitops) rase the lmit to 128 */
+    mc->max_cpus = 64;
+}
+
+static const TypeInfo machvirtv3_info = {
+    .name = TYPE_VIRTV3_MACHINE,
+    .parent = TYPE_VIRT_MACHINE,
+    .instance_size = sizeof(VirtMachineState),
+    .instance_init = virtv3_instance_init,
+    .class_size = sizeof(VirtMachineClass),
+    .class_init = virtv3_class_init,
+};
+
 static void machvirt_machine_init(void)
 {
     type_register_static(&machvirt_info);
+    type_register_static(&machvirtv3_info);
 }
 
 machine_init(machvirt_machine_init);
include/hw/arm/virt.h +4 -0
index ceec8b3..6554630 100644
@@ -45,6 +45,10 @@ enum {
     VIRT_CPUPERIPHS,
     VIRT_GIC_DIST,
     VIRT_GIC_CPU,
+    VIRT_GIC_DIST_SPI = VIRT_GIC_CPU,
+    VIRT_ITS_CONTROL,
+    VIRT_ITS_TRANSLATION,
+    VIRT_LPI,
     VIRT_UART,
     VIRT_MMIO,
     VIRT_RTC,
include/hw/boards.h +1 -0
index ff79797..0734317 100644
@@ -125,6 +125,7 @@ struct MachineState {
     char *accel;
     bool kernel_irqchip_allowed;
     bool kernel_irqchip_required;
+    int kernel_irqchip_type;
     int kvm_shadow_mem;
     char *dtb;
     char *dumpdtb;
Signature · 1 line
1.9.1