Skip to contentSkip to conversation

ARM: KVM: Enable in-kernel PMU with user space gic

PATCH Superseded Andrew Jones 2 participants 9 messages Jul 1, 2017 – Jul 18, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ARM: KVM: Enable in-kernel PMU with user space gic

Andrew Jones (3):
hw/arm/virt: add pmu interrupt state target/arm/kvm: split pmu init from creation hw/arm/virt: allow pmu instantiation with userspace irqchip

 hw/arm/virt.c        | 13 +++++++++++--
 target/arm/cpu.c     |  2 ++
 target/arm/cpu.h     |  2 ++
 target/arm/kvm.c     |  6 +++++-
 target/arm/kvm32.c   |  6 ++++++
 target/arm/kvm64.c   | 55 +++++++++++++++++++++++++---------------------------
 target/arm/kvm_arm.h |  6 ++++++
 7 files changed, 58 insertions(+), 32 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 hw/arm/virt: add pmu interrupt state

Mimicking gicv3-maintenance-interrupt, add the PMU's interrupt to CPU state.

---
 hw/arm/virt.c    | 3 +++
 target/arm/cpu.c | 2 ++
 target/arm/cpu.h | 2 ++
 3 files changed, 7 insertions(+)
hw/arm/virt.c +3 -0
index 010f7244bf7c..9781e1cc5ed7 100644
@@ -610,6 +610,9 @@ static void create_gic(VirtMachineState *vms, qemu_irq *pic)
         qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
                                     qdev_get_gpio_in(gicdev, ppibase
                                                      + ARCH_GICV3_MAINT_IRQ));
+        qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
+                                    qdev_get_gpio_in(gicdev, ppibase
+                                                     + VIRTUAL_PMU_IRQ));
 
         sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
         sysbus_connect_irq(gicbusdev, i + smp_cpus,
target/arm/cpu.c +2 -0
index 28a914129857..70cc8f4474af 100644
@@ -499,6 +499,8 @@ static void arm_cpu_initfn(Object *obj)
 
     qdev_init_gpio_out_named(DEVICE(cpu), &cpu->gicv3_maintenance_interrupt,
                              "gicv3-maintenance-interrupt", 1);
+    qdev_init_gpio_out_named(DEVICE(cpu), &cpu->pmu_interrupt,
+                             "pmu-interrupt", 1);
 #endif
 
     /* DTB consumers generally don't in fact care what the 'compatible'
target/arm/cpu.h +2 -0
index 102c58afac52..8d91166eb97b 100644
@@ -584,6 +584,8 @@ struct ARMCPU {
     qemu_irq gt_timer_outputs[NUM_GTIMERS];
     /* GPIO output for GICv3 maintenance interrupt signal */
     qemu_irq gicv3_maintenance_interrupt;
+    /* GPIO output for the PMU interrupt */
+    qemu_irq pmu_interrupt;
 
     /* MemoryRegion to use for secure physical accesses */
     MemoryRegion *secure_memory;
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 2/3 target/arm/kvm: split pmu init from creation

In reply to Andrew Jones

When adding a PMU with a userspace irqchip we only do the INIT stage of the device creation.

---
 hw/arm/virt.c        | 10 ++++++++--
 target/arm/kvm32.c   |  6 ++++++
 target/arm/kvm64.c   | 52 +++++++++++++++++++++++++---------------------------
 target/arm/kvm_arm.h |  6 ++++++
 4 files changed, 45 insertions(+), 29 deletions(-)
hw/arm/virt.c +8 -2
index 9781e1cc5ed7..0cb8b479232d 100644
@@ -492,8 +492,14 @@ static void fdt_add_pmu_nodes(const VirtMachineState *vms)
 
     CPU_FOREACH(cpu) {
         armcpu = ARM_CPU(cpu);
-        if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU) ||
-            (kvm_enabled() && !kvm_arm_pmu_create(cpu, PPI(VIRTUAL_PMU_IRQ)))) {
+        if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
+            return;
+        }
+        if (kvm_enabled() &&
+            !kvm_arm_pmu_create(cpu, PPI(VIRTUAL_PMU_IRQ))) {
+            return;
+        }
+        if (kvm_enabled() && !kvm_arm_pmu_init(cpu)) {
             return;
         }
     }
target/arm/kvm32.c +6 -0
index 069da0c5fd10..a51695f25911 100644
@@ -527,3 +527,9 @@ int kvm_arm_pmu_create(CPUState *cs, int irq)
     qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
     return 0;
 }
+
+int kvm_arm_pmu_init(CPUState *cs)
+{
+    qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
+    return 0;
+}
target/arm/kvm64.c +25 -27

71 lines not on this page · raw message

target/arm/kvm_arm.h +6 -0
index 633d08828a5d..3382762aa023 100644
@@ -196,6 +196,7 @@ int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu);
 int kvm_arm_vgic_probe(void);
 
 int kvm_arm_pmu_create(CPUState *cs, int irq);
+int kvm_arm_pmu_init(CPUState *cs);
 
 #else
 
@@ -209,6 +210,11 @@ static inline int kvm_arm_pmu_create(CPUState *cs, int irq)
     return 0;
 }
 
+static inline int kvm_arm_pmu_init(CPUState *cs)
+{
+    return 0;
+}
+
 #endif
 
 static inline const char *gic_class_name(void)
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [Qemu-arm] [PATCH 2/3] target/arm/kvm: split pmu init from creation

Quoted 2 lines · Andrew Jones

When adding a PMU with a userspace irqchip we only do the INIT stage of the device creation.

Can you explain why? My assumption would be that either
(a) we don't need the kernel's PMU, in which case don't create it at all, or
(b) we do need the kernel's PMU, so we should both create and INIT it.

If we do one but not the other then we've left a half created and unusable PMU device in the kernel, haven't we?

thanks
-- PMM

#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] target/arm/kvm: split pmu init from creation

Quoted 4 lines · Peter Maydell
Quoted 2 lines · Andrew Jones

When adding a PMU with a userspace irqchip we only do the INIT stage of the device creation.

Quoted 9 lines

Can you explain why? My assumption would be that either
(a) we don't need the kernel's PMU, in which case don't create it at all, or
(b) we do need the kernel's PMU, so we should both create and INIT it.

If we do one but not the other then we've left a half created and unusable PMU device in the kernel, haven't we?

I should have renamed the 'create' function to 'set_irq', then it would make sense, because after the split done by this patch 'create' only sets the irq, which can only be done with an in-kernel irqchip. Both irqchip types still require INIT though, see kernel doc Documentation/virtual/kvm/devices/vcpu.txt.

I'll send a v2 that renames the create function. But, before I do, assuming the rename, do you have another comments on this or the next patch? We might as well batch all the changes :-)

Thanks,
drew

#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] target/arm/kvm: split pmu init from creation

Quoted 15 lines · Andrew Jones
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Andrew Jones

When adding a PMU with a userspace irqchip we only do the INIT stage of the device creation.

Quoted 9 lines

Can you explain why? My assumption would be that either
(a) we don't need the kernel's PMU, in which case don't create it at all, or
(b) we do need the kernel's PMU, so we should both create and INIT it.

If we do one but not the other then we've left a half created and unusable PMU device in the kernel, haven't we?

Quoted 5 lines

I should have renamed the 'create' function to 'set_irq', then it would make sense, because after the split done by this patch 'create' only sets the irq, which can only be done with an in-kernel irqchip. Both irqchip types still require INIT though, see kernel doc Documentation/virtual/kvm/devices/vcpu.txt.

Oh, I see, I read the commit message as "we only do the INIT stage when adding a PMU with a userspace irqchip", which isn't the same meaning as what you actually wrote. (perhaps "When adding a PMU with a userspace irqchip we skip the set-irq stage of device creation" would be clearer?)

Quoted 3 lines

I'll send a v2 that renames the create function. But, before I do, assuming the rename, do you have another comments on this or the next patch? We might as well batch all the changes :-)

I think they looked mostly OK. A minor nit:

+ if (kvm_enabled() &&
+ !kvm_arm_pmu_create(cpu, PPI(VIRTUAL_PMU_IRQ))) {
+ return;
+ }

+        if (kvm_enabled() && !kvm_arm_pmu_init(cpu)) {
             return;
         }

might be better as

       if (kvm_enabled()) {
           if (!kvm_arm_pmu_create(...)) {
               /* error handling */
           }
           if (!kvm_arm_pmu_init(...)) {
               /* error handling */
           }
       }

(and I'm not sure "silently return" is really the right error handling, but that is what we do currently, so whatever.)

thanks
-- PMM

#7 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/3] target/arm/kvm: split pmu init from creation

Quoted 43 lines · Peter Maydell
Quoted 15 lines · Andrew Jones
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Andrew Jones

When adding a PMU with a userspace irqchip we only do the INIT stage of the device creation.

Quoted 9 lines

Can you explain why? My assumption would be that either
(a) we don't need the kernel's PMU, in which case don't create it at all, or
(b) we do need the kernel's PMU, so we should both create and INIT it.

If we do one but not the other then we've left a half created and unusable PMU device in the kernel, haven't we?

Quoted 5 lines

I should have renamed the 'create' function to 'set_irq', then it would make sense, because after the split done by this patch 'create' only sets the irq, which can only be done with an in-kernel irqchip. Both irqchip types still require INIT though, see kernel doc Documentation/virtual/kvm/devices/vcpu.txt.

Oh, I see, I read the commit message as "we only do the INIT stage when adding a PMU with a userspace irqchip", which isn't the same meaning as what you actually wrote. (perhaps "When adding a PMU with a userspace irqchip we skip the set-irq stage of device creation" would be clearer?)

Quoted 3 lines

I'll send a v2 that renames the create function. But, before I do, assuming the rename, do you have another comments on this or the next patch? We might as well batch all the changes :-)

I think they looked mostly OK. A minor nit:

hw/arm/virt.c +5 -0
+        if (kvm_enabled() &&
+            !kvm_arm_pmu_create(cpu, PPI(VIRTUAL_PMU_IRQ))) {
+            return;
+        }
+        if (kvm_enabled() && !kvm_arm_pmu_init(cpu)) {
             return;
         }

might be better as

Quoted 8 lines
       if (kvm_enabled()) {
           if (!kvm_arm_pmu_create(...)) {
               /* error handling */
           }
           if (!kvm_arm_pmu_init(...)) {
               /* error handling */
           }
       }

With the top version I was setting things up for a one-liner change in the next patch, as only the first condition needs to have s/kvm_enabled/kvm_irqchip_in_kernel/ done to it.

Quoted 3 lines

(and I'm not sure "silently return" is really the right error handling, but that is what we do currently, so whatever.)

I'll give the error handling a bit of thought before respinning.

Thanks,
drew

#8 lore raw

PATCH 3/3 hw/arm/virt: allow pmu instantiation with userspace irqchip

In reply to Andrew Jones

Move the in-kernel-irqchip test to only guard the creation, not the init'ing of the PMU. Also add the PMU to the KVM device irq line synchronization to enable its use.

---
 hw/arm/virt.c      | 2 +-
 target/arm/kvm.c   | 6 +++++-
 target/arm/kvm64.c | 3 +--
 3 files changed, 7 insertions(+), 4 deletions(-)
hw/arm/virt.c +1 -1
index 0cb8b479232d..53592fd0f30c 100644
@@ -495,7 +495,7 @@ static void fdt_add_pmu_nodes(const VirtMachineState *vms)
         if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
             return;
         }
-        if (kvm_enabled() &&
+        if (kvm_irqchip_in_kernel() &&
             !kvm_arm_pmu_create(cpu, PPI(VIRTUAL_PMU_IRQ))) {
             return;
         }
target/arm/kvm.c +5 -1
index 7c17f0d629d7..211a7bf7befd 100644
@@ -567,7 +567,11 @@ MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
             switched_level &= ~KVM_ARM_DEV_EL1_PTIMER;
         }
 
-        /* XXX PMU IRQ is missing */
+        if (switched_level & KVM_ARM_DEV_PMU) {
+            qemu_set_irq(cpu->pmu_interrupt,
+                         !!(run->s.regs.device_irq_level & KVM_ARM_DEV_PMU));
+            switched_level &= ~KVM_ARM_DEV_PMU;
+        }
 
         if (switched_level) {
             qemu_log_mask(LOG_UNIMP, "%s: unhandled in-kernel device IRQ %x\n",
target/arm/kvm64.c +1 -2
index d94e0a04f015..54e58e407812 100644
@@ -506,8 +506,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
     }
-    if (!kvm_irqchip_in_kernel() ||
-        !kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
+    if (!kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PMU_V3)) {
             cpu->has_pmu = false;
     }
     if (cpu->has_pmu) {
Signature · 1 line
1.8.3.1