Skip to content

Commit 3dbbdf7

Browse files
chazyChristoffer Dall
authored andcommitted
KVM: arm/arm64: Report PMU overflow interrupts to userspace irqchip
When not using an in-kernel VGIC, but instead emulating an interrupt controller in userspace, we should report the PMU overflow status to that userspace interrupt controller using the KVM_CAP_ARM_USER_IRQ feature. Reviewed-by: Alexander Graf <agraf@suse.de> Reviewed-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
1 parent d9e1397 commit 3dbbdf7

4 files changed

Lines changed: 51 additions & 11 deletions

File tree

‎arch/arm/kvm/arm.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
635635

636636
/*
637637
* If we have a singal pending, or need to notify a userspace
638-
* irqchip about timer level changes, then we exit (and update
639-
* the timer level state in kvm_timer_update_run below).
638+
* irqchip about timer or PMU level changes, then we exit (and
639+
* update the timer level state in kvm_timer_update_run
640+
* below).
640641
*/
641642
if (signal_pending(current) ||
642-
kvm_timer_should_notify_user(vcpu)) {
643+
kvm_timer_should_notify_user(vcpu) ||
644+
kvm_pmu_should_notify_user(vcpu)) {
643645
ret = -EINTR;
644646
run->exit_reason = KVM_EXIT_INTR;
645647
}
@@ -712,7 +714,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
712714
}
713715

714716
/* Tell userspace about in-kernel device output levels */
715-
kvm_timer_update_run(vcpu);
717+
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
718+
kvm_timer_update_run(vcpu);
719+
kvm_pmu_update_run(vcpu);
720+
}
716721

717722
if (vcpu->sigset_active)
718723
sigprocmask(SIG_SETMASK, &sigsaved, NULL);

‎include/kvm/arm_pmu.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val);
5050
void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val);
5151
void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu);
5252
void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu);
53+
bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu);
54+
void kvm_pmu_update_run(struct kvm_vcpu *vcpu);
5355
void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val);
5456
void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val);
5557
void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
@@ -85,6 +87,11 @@ static inline void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val) {}
8587
static inline void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val) {}
8688
static inline void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) {}
8789
static inline void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) {}
90+
static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
91+
{
92+
return false;
93+
}
94+
static inline void kvm_pmu_update_run(struct kvm_vcpu *vcpu) {}
8895
static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {}
8996
static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {}
9097
static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu,

‎virt/kvm/arm/arch_timer.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ void kvm_timer_update_run(struct kvm_vcpu *vcpu)
193193
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
194194
struct kvm_sync_regs *regs = &vcpu->run->s.regs;
195195

196-
if (likely(irqchip_in_kernel(vcpu->kvm)))
197-
return;
198-
199196
/* Populate the device bitmap with the timer states */
200197
regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
201198
KVM_ARM_DEV_EL1_PTIMER);

‎virt/kvm/arm/pmu.c‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,44 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
230230
return;
231231

232232
overflow = !!kvm_pmu_overflow_status(vcpu);
233-
if (pmu->irq_level != overflow) {
234-
pmu->irq_level = overflow;
235-
kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
236-
pmu->irq_num, overflow);
233+
if (pmu->irq_level == overflow)
234+
return;
235+
236+
pmu->irq_level = overflow;
237+
238+
if (likely(irqchip_in_kernel(vcpu->kvm))) {
239+
int ret;
240+
ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
241+
pmu->irq_num, overflow);
242+
WARN_ON(ret);
237243
}
238244
}
239245

246+
bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
247+
{
248+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
249+
struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
250+
bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
251+
252+
if (likely(irqchip_in_kernel(vcpu->kvm)))
253+
return false;
254+
255+
return pmu->irq_level != run_level;
256+
}
257+
258+
/*
259+
* Reflect the PMU overflow interrupt output level into the kvm_run structure
260+
*/
261+
void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
262+
{
263+
struct kvm_sync_regs *regs = &vcpu->run->s.regs;
264+
265+
/* Populate the timer bitmap for user space */
266+
regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
267+
if (vcpu->arch.pmu.irq_level)
268+
regs->device_irq_level |= KVM_ARM_DEV_PMU;
269+
}
270+
240271
/**
241272
* kvm_pmu_flush_hwstate - flush pmu state to cpu
242273
* @vcpu: The vcpu pointer

0 commit comments

Comments
 (0)