Skip to contentSkip to conversation

Enable QEMU to control MPIDR values

PATCH In review Andrew Jones 2 participants 5 messages Feb 27, 2017 – Mar 2, 2017

lore
Share & tools
lore
#0 lore raw

cover Enable QEMU to control MPIDR values

Igor needs to know/select vcpu MPIDR values early in order to map vcpus to vnodes. Waiting until after vcpu init, when KVM sets them, is too late. With this patch, and its KVM counterpart, QEMU can control the values, allowing it to know/select them whenever it likes.

Andrew Jones (2):
HACK: update linux/kvm header
arm: kvm: set MPIDR when we can

 linux-headers/linux/kvm.h |  1 +
 target/arm/cpu.c          |  3 +--
 target/arm/kvm32.c        | 22 ++++++++++++++--------
 target/arm/kvm64.c        | 22 ++++++++++++++--------
 4 files changed, 30 insertions(+), 18 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/2 HACK: update linux/kvm header

---
 linux-headers/linux/kvm.h | 1 +
 1 file changed, 1 insertion(+)
linux-headers/linux/kvm.h +1 -0
index bb0ed712232d..38e636c85886 100644
@@ -870,6 +870,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_S390_USER_INSTR0 130
 #define KVM_CAP_MSI_DEVID 131
 #define KVM_CAP_PPC_HTM 132
+#define KVM_CAP_ARM_USER_MPIDR 137
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/2 arm: kvm: set MPIDR when we can

In reply to Andrew Jones

If KVM supports user provided MPIDRs for the vcpus, then provide them. This simplifies management of vcpus as we can be sure of the identity of a vcpu even before it's been instantiated.

---
 target/arm/cpu.c   |  3 +--
 target/arm/kvm32.c | 22 ++++++++++++++--------
 target/arm/kvm64.c | 22 ++++++++++++++--------
 3 files changed, 29 insertions(+), 18 deletions(-)
target/arm/cpu.c +1 -2
index f7157dc0e59d..83cd5eefbef8 100644
@@ -741,8 +741,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
-     * We don't support setting cluster ID ([16..23]) (known as Aff2
+    /* We don't currently 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.
      */
target/arm/kvm32.c +14 -8
index 069da0c5fd10..5ad732498e5c 100644
@@ -186,7 +186,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
     uint64_t v;
-    uint32_t mpidr;
     struct kvm_one_reg r;
     ARMCPU *cpu = ARM_CPU(cs);
 
@@ -223,16 +222,23 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return -EINVAL;
     }
 
-    /*
-     * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
-     * Currently KVM has its own idea about MPIDR assignment, so we
-     * override our defaults with what we get from KVM.
-     */
-    ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
+    if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_USER_MPIDR)) {
+        ret = kvm_set_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR),
+                              &cpu->mp_affinity);
+    } else {
+        /* If we can't set the MPIDR, then KVM will generate one. We must
+         * update our copy to that one in order to stay synchronized.
+         */
+        uint32_t mpidr;
+
+        ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
+        if (!ret) {
+            cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
+        }
+    }
     if (ret) {
         return ret;
     }
-    cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
 
     return kvm_arm_init_cpreg_list(cpu);
 }
target/arm/kvm64.c +14 -8
index 61111091ad3b..a7d3f876c43b 100644
@@ -486,7 +486,6 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
-    uint64_t mpidr;
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
 
@@ -524,16 +523,23 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
-    /*
-     * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
-     * Currently KVM has its own idea about MPIDR assignment, so we
-     * override our defaults with what we get from KVM.
-     */
-    ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
+    if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_USER_MPIDR)) {
+        ret = kvm_set_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR),
+                              &cpu->mp_affinity);
+    } else {
+        /* If we can't set the MPIDR, then KVM will generate one. We must
+         * update our copy to that one in order to stay synchronized.
+         */
+        uint64_t mpidr;
+
+        ret = kvm_get_one_reg(cs, ARM64_SYS_REG(ARM_CPU_ID_MPIDR), &mpidr);
+        if (!ret) {
+            cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
+        }
+    }
     if (ret) {
         return ret;
     }
-    cpu->mp_affinity = mpidr & ARM64_AFFINITY_MASK;
 
     kvm_arm_init_debug(cs);
 
Signature · 1 line
2.9.3
#3 lore raw

Re: [Qemu-arm] [PATCH 2/2] arm: kvm: set MPIDR when we can

Quoted 41 lines · Andrew Jones

If KVM supports user provided MPIDRs for the vcpus, then provide them. This simplifies management of vcpus as we can be sure of the identity of a vcpu even before it's been instantiated.

---
 target/arm/cpu.c   |  3 +--
 target/arm/kvm32.c | 22 ++++++++++++++--------
 target/arm/kvm64.c | 22 ++++++++++++++--------
 3 files changed, 29 insertions(+), 18 deletions(-)
target/arm/cpu.c +1 -2
index f7157dc0e59d..83cd5eefbef8 100644
@@ -741,8 +741,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
-     * We don't support setting cluster ID ([16..23]) (known as Aff2
+    /* We don't currently 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.
      */
target/arm/kvm32.c +0 -1
index 069da0c5fd10..5ad732498e5c 100644
@@ -186,7 +186,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
     uint64_t v;
-    uint32_t mpidr;
     struct kvm_one_reg r;
     ARMCPU *cpu = ARM_CPU(cs);
 
@@ -223,16 +222,23 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return -EINVAL;
     }

target/arm/kvm32.c int kvm_arch_init_vcpu(CPUState *cs) · line 227

Quoted 10 lines
target/arm/kvm32.c +3 -6
 
-    /*
-     * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
-     * Currently KVM has its own idea about MPIDR assignment, so we
-     * override our defaults with what we get from KVM.
-     */
-    ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
+    if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_USER_MPIDR)) {
+        ret = kvm_set_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR),
+                              &cpu->mp_affinity);

mp_affinity is only the affinity bits of the MPIDR. I would expect the kernel set_one_reg interface for the MPIDR to want a complete MPIDR value with M, U and MT bits.

(compare the way that when we set mp_affinity from the kernel reg. value we mask out the bits we don't care about).

Similarly with the 64-bit version of this code.

Quoted 7 lines
target/arm/kvm32.c +7 -0
+    } else {
+        /* If we can't set the MPIDR, then KVM will generate one. We must
+         * update our copy to that one in order to stay synchronized.
+         */
+        uint32_t mpidr;
+
+        ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);

target/arm/kvm32.c int kvm_arch_init_vcpu(CPUState *cs) · line 243

Quoted 10 lines
target/arm/kvm32.c +4 -1
+        if (!ret) {
+            cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
+        }
+    }
     if (ret) {
         return ret;
     }
-    cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
 
     return kvm_arm_init_cpreg_list(cpu);

Otherwise this looks fine to me.

PS: I assume we are targeting 2.10 (or whatever we call it) with this.

thanks
-- PMM

#4 lore raw

Re: [Qemu-arm] [PATCH 2/2] arm: kvm: set MPIDR when we can

Quoted 43 lines · Peter Maydell
Quoted 42 lines · Andrew Jones

If KVM supports user provided MPIDRs for the vcpus, then provide them. This simplifies management of vcpus as we can be sure of the identity of a vcpu even before it's been instantiated.

---
 target/arm/cpu.c   |  3 +--
 target/arm/kvm32.c | 22 ++++++++++++++--------
 target/arm/kvm64.c | 22 ++++++++++++++--------
 3 files changed, 29 insertions(+), 18 deletions(-)
target/arm/cpu.c +1 -2
index f7157dc0e59d..83cd5eefbef8 100644
@@ -741,8 +741,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    /* This cpu-id-to-MPIDR affinity is used only for TCG; KVM will override it.
-     * We don't support setting cluster ID ([16..23]) (known as Aff2
+    /* We don't currently 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.
      */
target/arm/kvm32.c +0 -1
index 069da0c5fd10..5ad732498e5c 100644
@@ -186,7 +186,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
 {
     int ret;
     uint64_t v;
-    uint32_t mpidr;
     struct kvm_one_reg r;
     ARMCPU *cpu = ARM_CPU(cs);
 
@@ -223,16 +222,23 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return -EINVAL;
     }
 
Quoted 13 lines

target/arm/kvm32.c int kvm_arch_init_vcpu(CPUState *cs) · line 227

Quoted 9 lines
target/arm/kvm32.c +3 -6
-    /*
-     * When KVM is in use, PSCI is emulated in-kernel and not by qemu.
-     * Currently KVM has its own idea about MPIDR assignment, so we
-     * override our defaults with what we get from KVM.
-     */
-    ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
+    if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_USER_MPIDR)) {
+        ret = kvm_set_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR),
+                              &cpu->mp_affinity);

mp_affinity is only the affinity bits of the MPIDR. I would expect the kernel set_one_reg interface for the MPIDR to want a complete MPIDR value with M, U and MT bits.

Thanks, I'll fix this.

Quoted 22 lines

(compare the way that when we set mp_affinity from the kernel reg. value we mask out the bits we don't care about).

Similarly with the 64-bit version of this code.

Quoted 16 lines
target/arm/kvm32.c +11 -1
+    } else {
+        /* If we can't set the MPIDR, then KVM will generate one. We must
+         * update our copy to that one in order to stay synchronized.
+         */
+        uint32_t mpidr;
+
+        ret = kvm_get_one_reg(cs, ARM_CP15_REG32(ARM_CPU_ID_MPIDR), &mpidr);
+        if (!ret) {
+            cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
+        }
+    }
     if (ret) {
         return ret;
     }
-    cpu->mp_affinity = mpidr & ARM32_AFFINITY_MASK;
 
Quoted 5 lines

target/arm/kvm32.c int kvm_arch_init_vcpu(CPUState *cs) · line 243

Quoted 1 line
target/arm/kvm32.c +0 -0
     return kvm_arm_init_cpreg_list(cpu);

Otherwise this looks fine to me.

PS: I assume we are targeting 2.10 (or whatever we call it) with this.

Yeah, this is 2.10 material.

Thanks,
drew