summaryrefslogtreecommitdiffstats
path: root/virt
diff options
context:
space:
mode:
authorRadim Krčmář <rkrcmar@redhat.com>2018-01-31 07:34:41 -0500
committerRadim Krčmář <rkrcmar@redhat.com>2018-01-31 07:34:41 -0500
commite53175395d7e12d8474707271bc02a2814279843 (patch)
treeca6a0fc846cffb1b6db999a4595998c160333cf0 /virt
parent810f4600ec5ee79c68dcbb136ed26a652df46348 (diff)
parentcd15d2050c044ca9525ba165e9073ac8e036b8d0 (diff)
Merge tag 'kvm-arm-for-v4.16' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm
KVM/ARM Changes for v4.16 The changes for this version include icache invalidation optimizations (improving VM startup time), support for forwarded level-triggered interrupts (improved performance for timers and passthrough platform devices), a small fix for power-management notifiers, and some cosmetic changes.
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/arm/arch_timer.c137
-rw-r--r--virt/kvm/arm/arm.c63
-rw-r--r--virt/kvm/arm/hyp/vgic-v2-sr.c1
-rw-r--r--virt/kvm/arm/mmu.c64
-rw-r--r--virt/kvm/arm/vgic/vgic-its.c4
-rw-r--r--virt/kvm/arm/vgic/vgic-mmio.c115
-rw-r--r--virt/kvm/arm/vgic/vgic-v2.c29
-rw-r--r--virt/kvm/arm/vgic/vgic-v3.c29
-rw-r--r--virt/kvm/arm/vgic/vgic.c41
-rw-r--r--virt/kvm/arm/vgic/vgic.h8
10 files changed, 368 insertions, 123 deletions
diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index f9555b1e7f15..fb6bd9b9845e 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -97,15 +97,13 @@ static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
97 pr_warn_once("Spurious arch timer IRQ on non-VCPU thread\n"); 97 pr_warn_once("Spurious arch timer IRQ on non-VCPU thread\n");
98 return IRQ_NONE; 98 return IRQ_NONE;
99 } 99 }
100 vtimer = vcpu_vtimer(vcpu);
101 100
102 if (!vtimer->irq.level) { 101 vtimer = vcpu_vtimer(vcpu);
103 vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl); 102 if (kvm_timer_should_fire(vtimer))
104 if (kvm_timer_irq_can_fire(vtimer)) 103 kvm_timer_update_irq(vcpu, true, vtimer);
105 kvm_timer_update_irq(vcpu, true, vtimer);
106 }
107 104
108 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 105 if (static_branch_unlikely(&userspace_irqchip_in_use) &&
106 unlikely(!irqchip_in_kernel(vcpu->kvm)))
109 kvm_vtimer_update_mask_user(vcpu); 107 kvm_vtimer_update_mask_user(vcpu);
110 108
111 return IRQ_HANDLED; 109 return IRQ_HANDLED;
@@ -231,6 +229,16 @@ static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
231{ 229{
232 u64 cval, now; 230 u64 cval, now;
233 231
232 if (timer_ctx->loaded) {
233 u32 cnt_ctl;
234
235 /* Only the virtual timer can be loaded so far */
236 cnt_ctl = read_sysreg_el0(cntv_ctl);
237 return (cnt_ctl & ARCH_TIMER_CTRL_ENABLE) &&
238 (cnt_ctl & ARCH_TIMER_CTRL_IT_STAT) &&
239 !(cnt_ctl & ARCH_TIMER_CTRL_IT_MASK);
240 }
241
234 if (!kvm_timer_irq_can_fire(timer_ctx)) 242 if (!kvm_timer_irq_can_fire(timer_ctx))
235 return false; 243 return false;
236 244
@@ -245,15 +253,7 @@ bool kvm_timer_is_pending(struct kvm_vcpu *vcpu)
245 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 253 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
246 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 254 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
247 255
248 if (vtimer->irq.level || ptimer->irq.level) 256 if (kvm_timer_should_fire(vtimer))
249 return true;
250
251 /*
252 * When this is called from withing the wait loop of kvm_vcpu_block(),
253 * the software view of the timer state is up to date (timer->loaded
254 * is false), and so we can simply check if the timer should fire now.
255 */
256 if (!vtimer->loaded && kvm_timer_should_fire(vtimer))
257 return true; 257 return true;
258 258
259 return kvm_timer_should_fire(ptimer); 259 return kvm_timer_should_fire(ptimer);
@@ -271,9 +271,9 @@ void kvm_timer_update_run(struct kvm_vcpu *vcpu)
271 /* Populate the device bitmap with the timer states */ 271 /* Populate the device bitmap with the timer states */
272 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER | 272 regs->device_irq_level &= ~(KVM_ARM_DEV_EL1_VTIMER |
273 KVM_ARM_DEV_EL1_PTIMER); 273 KVM_ARM_DEV_EL1_PTIMER);
274 if (vtimer->irq.level) 274 if (kvm_timer_should_fire(vtimer))
275 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER; 275 regs->device_irq_level |= KVM_ARM_DEV_EL1_VTIMER;
276 if (ptimer->irq.level) 276 if (kvm_timer_should_fire(ptimer))
277 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER; 277 regs->device_irq_level |= KVM_ARM_DEV_EL1_PTIMER;
278} 278}
279 279
@@ -286,7 +286,8 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
286 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq, 286 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer_ctx->irq.irq,
287 timer_ctx->irq.level); 287 timer_ctx->irq.level);
288 288
289 if (likely(irqchip_in_kernel(vcpu->kvm))) { 289 if (!static_branch_unlikely(&userspace_irqchip_in_use) ||
290 likely(irqchip_in_kernel(vcpu->kvm))) {
290 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, 291 ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
291 timer_ctx->irq.irq, 292 timer_ctx->irq.irq,
292 timer_ctx->irq.level, 293 timer_ctx->irq.level,
@@ -324,12 +325,20 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
324 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 325 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
325 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 326 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
326 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu); 327 struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
328 bool level;
327 329
328 if (unlikely(!timer->enabled)) 330 if (unlikely(!timer->enabled))
329 return; 331 return;
330 332
331 if (kvm_timer_should_fire(vtimer) != vtimer->irq.level) 333 /*
332 kvm_timer_update_irq(vcpu, !vtimer->irq.level, vtimer); 334 * The vtimer virtual interrupt is a 'mapped' interrupt, meaning part
335 * of its lifecycle is offloaded to the hardware, and we therefore may
336 * not have lowered the irq.level value before having to signal a new
337 * interrupt, but have to signal an interrupt every time the level is
338 * asserted.
339 */
340 level = kvm_timer_should_fire(vtimer);
341 kvm_timer_update_irq(vcpu, level, vtimer);
333 342
334 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level) 343 if (kvm_timer_should_fire(ptimer) != ptimer->irq.level)
335 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer); 344 kvm_timer_update_irq(vcpu, !ptimer->irq.level, ptimer);
@@ -337,6 +346,12 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
337 phys_timer_emulate(vcpu); 346 phys_timer_emulate(vcpu);
338} 347}
339 348
349static void __timer_snapshot_state(struct arch_timer_context *timer)
350{
351 timer->cnt_ctl = read_sysreg_el0(cntv_ctl);
352 timer->cnt_cval = read_sysreg_el0(cntv_cval);
353}
354
340static void vtimer_save_state(struct kvm_vcpu *vcpu) 355static void vtimer_save_state(struct kvm_vcpu *vcpu)
341{ 356{
342 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 357 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -348,10 +363,8 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu)
348 if (!vtimer->loaded) 363 if (!vtimer->loaded)
349 goto out; 364 goto out;
350 365
351 if (timer->enabled) { 366 if (timer->enabled)
352 vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl); 367 __timer_snapshot_state(vtimer);
353 vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
354 }
355 368
356 /* Disable the virtual timer */ 369 /* Disable the virtual timer */
357 write_sysreg_el0(0, cntv_ctl); 370 write_sysreg_el0(0, cntv_ctl);
@@ -448,8 +461,7 @@ static void kvm_timer_vcpu_load_vgic(struct kvm_vcpu *vcpu)
448 bool phys_active; 461 bool phys_active;
449 int ret; 462 int ret;
450 463
451 phys_active = vtimer->irq.level || 464 phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
452 kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
453 465
454 ret = irq_set_irqchip_state(host_vtimer_irq, 466 ret = irq_set_irqchip_state(host_vtimer_irq,
455 IRQCHIP_STATE_ACTIVE, 467 IRQCHIP_STATE_ACTIVE,
@@ -496,8 +508,8 @@ bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
496 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER; 508 vlevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_VTIMER;
497 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER; 509 plevel = sregs->device_irq_level & KVM_ARM_DEV_EL1_PTIMER;
498 510
499 return vtimer->irq.level != vlevel || 511 return kvm_timer_should_fire(vtimer) != vlevel ||
500 ptimer->irq.level != plevel; 512 kvm_timer_should_fire(ptimer) != plevel;
501} 513}
502 514
503void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) 515void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
@@ -529,54 +541,27 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
529 set_cntvoff(0); 541 set_cntvoff(0);
530} 542}
531 543
532static void unmask_vtimer_irq(struct kvm_vcpu *vcpu) 544/*
545 * With a userspace irqchip we have to check if the guest de-asserted the
546 * timer and if so, unmask the timer irq signal on the host interrupt
547 * controller to ensure that we see future timer signals.
548 */
549static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
533{ 550{
534 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 551 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
535 552
536 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 553 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
537 kvm_vtimer_update_mask_user(vcpu); 554 __timer_snapshot_state(vtimer);
538 return; 555 if (!kvm_timer_should_fire(vtimer)) {
539 } 556 kvm_timer_update_irq(vcpu, false, vtimer);
540 557 kvm_vtimer_update_mask_user(vcpu);
541 /* 558 }
542 * If the guest disabled the timer without acking the interrupt, then
543 * we must make sure the physical and virtual active states are in
544 * sync by deactivating the physical interrupt, because otherwise we
545 * wouldn't see the next timer interrupt in the host.
546 */
547 if (!kvm_vgic_map_is_active(vcpu, vtimer->irq.irq)) {
548 int ret;
549 ret = irq_set_irqchip_state(host_vtimer_irq,
550 IRQCHIP_STATE_ACTIVE,
551 false);
552 WARN_ON(ret);
553 } 559 }
554} 560}
555 561
556/**
557 * kvm_timer_sync_hwstate - sync timer state from cpu
558 * @vcpu: The vcpu pointer
559 *
560 * Check if any of the timers have expired while we were running in the guest,
561 * and inject an interrupt if that was the case.
562 */
563void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) 562void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
564{ 563{
565 struct arch_timer_context *vtimer = vcpu_vtimer(vcpu); 564 unmask_vtimer_irq_user(vcpu);
566
567 /*
568 * If we entered the guest with the vtimer output asserted we have to
569 * check if the guest has modified the timer so that we should lower
570 * the line at this point.
571 */
572 if (vtimer->irq.level) {
573 vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
574 vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
575 if (!kvm_timer_should_fire(vtimer)) {
576 kvm_timer_update_irq(vcpu, false, vtimer);
577 unmask_vtimer_irq(vcpu);
578 }
579 }
580} 565}
581 566
582int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu) 567int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
@@ -807,6 +792,19 @@ static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
807 return true; 792 return true;
808} 793}
809 794
795bool kvm_arch_timer_get_input_level(int vintid)
796{
797 struct kvm_vcpu *vcpu = kvm_arm_get_running_vcpu();
798 struct arch_timer_context *timer;
799
800 if (vintid == vcpu_vtimer(vcpu)->irq.irq)
801 timer = vcpu_vtimer(vcpu);
802 else
803 BUG(); /* We only map the vtimer so far */
804
805 return kvm_timer_should_fire(timer);
806}
807
810int kvm_timer_enable(struct kvm_vcpu *vcpu) 808int kvm_timer_enable(struct kvm_vcpu *vcpu)
811{ 809{
812 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 810 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -828,7 +826,8 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)
828 return -EINVAL; 826 return -EINVAL;
829 } 827 }
830 828
831 ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq); 829 ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq,
830 kvm_arch_timer_get_input_level);
832 if (ret) 831 if (ret)
833 return ret; 832 return ret;
834 833
diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c
index cd7d90c9f644..92b95ae9a2ca 100644
--- a/virt/kvm/arm/arm.c
+++ b/virt/kvm/arm/arm.c
@@ -71,17 +71,17 @@ static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
71 71
72static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu) 72static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
73{ 73{
74 BUG_ON(preemptible());
75 __this_cpu_write(kvm_arm_running_vcpu, vcpu); 74 __this_cpu_write(kvm_arm_running_vcpu, vcpu);
76} 75}
77 76
77DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
78
78/** 79/**
79 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU. 80 * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU.
80 * Must be called from non-preemptible context 81 * Must be called from non-preemptible context
81 */ 82 */
82struct kvm_vcpu *kvm_arm_get_running_vcpu(void) 83struct kvm_vcpu *kvm_arm_get_running_vcpu(void)
83{ 84{
84 BUG_ON(preemptible());
85 return __this_cpu_read(kvm_arm_running_vcpu); 85 return __this_cpu_read(kvm_arm_running_vcpu);
86} 86}
87 87
@@ -295,6 +295,9 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
295 295
296void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) 296void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
297{ 297{
298 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
299 static_branch_dec(&userspace_irqchip_in_use);
300
298 kvm_mmu_free_memory_caches(vcpu); 301 kvm_mmu_free_memory_caches(vcpu);
299 kvm_timer_vcpu_terminate(vcpu); 302 kvm_timer_vcpu_terminate(vcpu);
300 kvm_pmu_vcpu_destroy(vcpu); 303 kvm_pmu_vcpu_destroy(vcpu);
@@ -532,14 +535,22 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
532 535
533 vcpu->arch.has_run_once = true; 536 vcpu->arch.has_run_once = true;
534 537
535 /* 538 if (likely(irqchip_in_kernel(kvm))) {
536 * Map the VGIC hardware resources before running a vcpu the first 539 /*
537 * time on this VM. 540 * Map the VGIC hardware resources before running a vcpu the
538 */ 541 * first time on this VM.
539 if (unlikely(irqchip_in_kernel(kvm) && !vgic_ready(kvm))) { 542 */
540 ret = kvm_vgic_map_resources(kvm); 543 if (unlikely(!vgic_ready(kvm))) {
541 if (ret) 544 ret = kvm_vgic_map_resources(kvm);
542 return ret; 545 if (ret)
546 return ret;
547 }
548 } else {
549 /*
550 * Tell the rest of the code that there are userspace irqchip
551 * VMs in the wild.
552 */
553 static_branch_inc(&userspace_irqchip_in_use);
543 } 554 }
544 555
545 ret = kvm_timer_enable(vcpu); 556 ret = kvm_timer_enable(vcpu);
@@ -680,19 +691,30 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
680 kvm_vgic_flush_hwstate(vcpu); 691 kvm_vgic_flush_hwstate(vcpu);
681 692
682 /* 693 /*
683 * If we have a singal pending, or need to notify a userspace 694 * Exit if we have a signal pending so that we can deliver the
684 * irqchip about timer or PMU level changes, then we exit (and 695 * signal to user space.
685 * update the timer level state in kvm_timer_update_run
686 * below).
687 */ 696 */
688 if (signal_pending(current) || 697 if (signal_pending(current)) {
689 kvm_timer_should_notify_user(vcpu) ||
690 kvm_pmu_should_notify_user(vcpu)) {
691 ret = -EINTR; 698 ret = -EINTR;
692 run->exit_reason = KVM_EXIT_INTR; 699 run->exit_reason = KVM_EXIT_INTR;
693 } 700 }
694 701
695 /* 702 /*
703 * If we're using a userspace irqchip, then check if we need
704 * to tell a userspace irqchip about timer or PMU level
705 * changes and if so, exit to userspace (the actual level
706 * state gets updated in kvm_timer_update_run and
707 * kvm_pmu_update_run below).
708 */
709 if (static_branch_unlikely(&userspace_irqchip_in_use)) {
710 if (kvm_timer_should_notify_user(vcpu) ||
711 kvm_pmu_should_notify_user(vcpu)) {
712 ret = -EINTR;
713 run->exit_reason = KVM_EXIT_INTR;
714 }
715 }
716
717 /*
696 * Ensure we set mode to IN_GUEST_MODE after we disable 718 * Ensure we set mode to IN_GUEST_MODE after we disable
697 * interrupts and before the final VCPU requests check. 719 * interrupts and before the final VCPU requests check.
698 * See the comment in kvm_vcpu_exiting_guest_mode() and 720 * See the comment in kvm_vcpu_exiting_guest_mode() and
@@ -704,7 +726,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
704 kvm_request_pending(vcpu)) { 726 kvm_request_pending(vcpu)) {
705 vcpu->mode = OUTSIDE_GUEST_MODE; 727 vcpu->mode = OUTSIDE_GUEST_MODE;
706 kvm_pmu_sync_hwstate(vcpu); 728 kvm_pmu_sync_hwstate(vcpu);
707 kvm_timer_sync_hwstate(vcpu); 729 if (static_branch_unlikely(&userspace_irqchip_in_use))
730 kvm_timer_sync_hwstate(vcpu);
708 kvm_vgic_sync_hwstate(vcpu); 731 kvm_vgic_sync_hwstate(vcpu);
709 local_irq_enable(); 732 local_irq_enable();
710 preempt_enable(); 733 preempt_enable();
@@ -748,7 +771,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
748 * we don't want vtimer interrupts to race with syncing the 771 * we don't want vtimer interrupts to race with syncing the
749 * timer virtual interrupt state. 772 * timer virtual interrupt state.
750 */ 773 */
751 kvm_timer_sync_hwstate(vcpu); 774 if (static_branch_unlikely(&userspace_irqchip_in_use))
775 kvm_timer_sync_hwstate(vcpu);
752 776
753 /* 777 /*
754 * We may have taken a host interrupt in HYP mode (ie 778 * We may have taken a host interrupt in HYP mode (ie
@@ -1277,6 +1301,7 @@ static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
1277 cpu_hyp_reset(); 1301 cpu_hyp_reset();
1278 1302
1279 return NOTIFY_OK; 1303 return NOTIFY_OK;
1304 case CPU_PM_ENTER_FAILED:
1280 case CPU_PM_EXIT: 1305 case CPU_PM_EXIT:
1281 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1306 if (__this_cpu_read(kvm_arm_hardware_enabled))
1282 /* The hardware was enabled before suspend. */ 1307 /* The hardware was enabled before suspend. */
diff --git a/virt/kvm/arm/hyp/vgic-v2-sr.c b/virt/kvm/arm/hyp/vgic-v2-sr.c
index d7fd46fe9efb..4fe6e797e8b3 100644
--- a/virt/kvm/arm/hyp/vgic-v2-sr.c
+++ b/virt/kvm/arm/hyp/vgic-v2-sr.c
@@ -21,6 +21,7 @@
21 21
22#include <asm/kvm_emulate.h> 22#include <asm/kvm_emulate.h>
23#include <asm/kvm_hyp.h> 23#include <asm/kvm_hyp.h>
24#include <asm/kvm_mmu.h>
24 25
25static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base) 26static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base)
26{ 27{
diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c
index b36945d49986..a1ea43fa75cf 100644
--- a/virt/kvm/arm/mmu.c
+++ b/virt/kvm/arm/mmu.c
@@ -926,6 +926,25 @@ static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
926 return 0; 926 return 0;
927} 927}
928 928
929static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)
930{
931 pmd_t *pmdp;
932 pte_t *ptep;
933
934 pmdp = stage2_get_pmd(kvm, NULL, addr);
935 if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
936 return false;
937
938 if (pmd_thp_or_huge(*pmdp))
939 return kvm_s2pmd_exec(pmdp);
940
941 ptep = pte_offset_kernel(pmdp, addr);
942 if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
943 return false;
944
945 return kvm_s2pte_exec(ptep);
946}
947
929static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, 948static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
930 phys_addr_t addr, const pte_t *new_pte, 949 phys_addr_t addr, const pte_t *new_pte,
931 unsigned long flags) 950 unsigned long flags)
@@ -1257,10 +1276,14 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
1257 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask); 1276 kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
1258} 1277}
1259 1278
1260static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, kvm_pfn_t pfn, 1279static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
1261 unsigned long size)
1262{ 1280{
1263 __coherent_cache_guest_page(vcpu, pfn, size); 1281 __clean_dcache_guest_page(pfn, size);
1282}
1283
1284static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
1285{
1286 __invalidate_icache_guest_page(pfn, size);
1264} 1287}
1265 1288
1266static void kvm_send_hwpoison_signal(unsigned long address, 1289static void kvm_send_hwpoison_signal(unsigned long address,
@@ -1286,7 +1309,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1286 unsigned long fault_status) 1309 unsigned long fault_status)
1287{ 1310{
1288 int ret; 1311 int ret;
1289 bool write_fault, writable, hugetlb = false, force_pte = false; 1312 bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false;
1290 unsigned long mmu_seq; 1313 unsigned long mmu_seq;
1291 gfn_t gfn = fault_ipa >> PAGE_SHIFT; 1314 gfn_t gfn = fault_ipa >> PAGE_SHIFT;
1292 struct kvm *kvm = vcpu->kvm; 1315 struct kvm *kvm = vcpu->kvm;
@@ -1298,7 +1321,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1298 unsigned long flags = 0; 1321 unsigned long flags = 0;
1299 1322
1300 write_fault = kvm_is_write_fault(vcpu); 1323 write_fault = kvm_is_write_fault(vcpu);
1301 if (fault_status == FSC_PERM && !write_fault) { 1324 exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
1325 VM_BUG_ON(write_fault && exec_fault);
1326
1327 if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
1302 kvm_err("Unexpected L2 read permission error\n"); 1328 kvm_err("Unexpected L2 read permission error\n");
1303 return -EFAULT; 1329 return -EFAULT;
1304 } 1330 }
@@ -1391,7 +1417,19 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1391 new_pmd = kvm_s2pmd_mkwrite(new_pmd); 1417 new_pmd = kvm_s2pmd_mkwrite(new_pmd);
1392 kvm_set_pfn_dirty(pfn); 1418 kvm_set_pfn_dirty(pfn);
1393 } 1419 }
1394 coherent_cache_guest_page(vcpu, pfn, PMD_SIZE); 1420
1421 if (fault_status != FSC_PERM)
1422 clean_dcache_guest_page(pfn, PMD_SIZE);
1423
1424 if (exec_fault) {
1425 new_pmd = kvm_s2pmd_mkexec(new_pmd);
1426 invalidate_icache_guest_page(pfn, PMD_SIZE);
1427 } else if (fault_status == FSC_PERM) {
1428 /* Preserve execute if XN was already cleared */
1429 if (stage2_is_exec(kvm, fault_ipa))
1430 new_pmd = kvm_s2pmd_mkexec(new_pmd);
1431 }
1432
1395 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd); 1433 ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
1396 } else { 1434 } else {
1397 pte_t new_pte = pfn_pte(pfn, mem_type); 1435 pte_t new_pte = pfn_pte(pfn, mem_type);
@@ -1401,7 +1439,19 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
1401 kvm_set_pfn_dirty(pfn); 1439 kvm_set_pfn_dirty(pfn);
1402 mark_page_dirty(kvm, gfn); 1440 mark_page_dirty(kvm, gfn);
1403 } 1441 }
1404 coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE); 1442
1443 if (fault_status != FSC_PERM)
1444 clean_dcache_guest_page(pfn, PAGE_SIZE);
1445
1446 if (exec_fault) {
1447 new_pte = kvm_s2pte_mkexec(new_pte);
1448 invalidate_icache_guest_page(pfn, PAGE_SIZE);
1449 } else if (fault_status == FSC_PERM) {
1450 /* Preserve execute if XN was already cleared */
1451 if (stage2_is_exec(kvm, fault_ipa))
1452 new_pte = kvm_s2pte_mkexec(new_pte);
1453 }
1454
1405 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags); 1455 ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
1406 } 1456 }
1407 1457
diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c
index 8e633bd9cc1e..465095355666 100644
--- a/virt/kvm/arm/vgic/vgic-its.c
+++ b/virt/kvm/arm/vgic/vgic-its.c
@@ -1034,10 +1034,8 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
1034 1034
1035 device = vgic_its_alloc_device(its, device_id, itt_addr, 1035 device = vgic_its_alloc_device(its, device_id, itt_addr,
1036 num_eventid_bits); 1036 num_eventid_bits);
1037 if (IS_ERR(device))
1038 return PTR_ERR(device);
1039 1037
1040 return 0; 1038 return PTR_ERR_OR_ZERO(device);
1041} 1039}
1042 1040
1043/* 1041/*
diff --git a/virt/kvm/arm/vgic/vgic-mmio.c b/virt/kvm/arm/vgic/vgic-mmio.c
index deb51ee16a3d..83d82bd7dc4e 100644
--- a/virt/kvm/arm/vgic/vgic-mmio.c
+++ b/virt/kvm/arm/vgic/vgic-mmio.c
@@ -16,6 +16,7 @@
16#include <linux/kvm.h> 16#include <linux/kvm.h>
17#include <linux/kvm_host.h> 17#include <linux/kvm_host.h>
18#include <kvm/iodev.h> 18#include <kvm/iodev.h>
19#include <kvm/arm_arch_timer.h>
19#include <kvm/arm_vgic.h> 20#include <kvm/arm_vgic.h>
20 21
21#include "vgic.h" 22#include "vgic.h"
@@ -122,10 +123,43 @@ unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
122 return value; 123 return value;
123} 124}
124 125
126/*
127 * This function will return the VCPU that performed the MMIO access and
128 * trapped from within the VM, and will return NULL if this is a userspace
129 * access.
130 *
131 * We can disable preemption locally around accessing the per-CPU variable,
132 * and use the resolved vcpu pointer after enabling preemption again, because
133 * even if the current thread is migrated to another CPU, reading the per-CPU
134 * value later will give us the same value as we update the per-CPU variable
135 * in the preempt notifier handlers.
136 */
137static struct kvm_vcpu *vgic_get_mmio_requester_vcpu(void)
138{
139 struct kvm_vcpu *vcpu;
140
141 preempt_disable();
142 vcpu = kvm_arm_get_running_vcpu();
143 preempt_enable();
144 return vcpu;
145}
146
147/* Must be called with irq->irq_lock held */
148static void vgic_hw_irq_spending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
149 bool is_uaccess)
150{
151 if (is_uaccess)
152 return;
153
154 irq->pending_latch = true;
155 vgic_irq_set_phys_active(irq, true);
156}
157
125void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, 158void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
126 gpa_t addr, unsigned int len, 159 gpa_t addr, unsigned int len,
127 unsigned long val) 160 unsigned long val)
128{ 161{
162 bool is_uaccess = !vgic_get_mmio_requester_vcpu();
129 u32 intid = VGIC_ADDR_TO_INTID(addr, 1); 163 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
130 int i; 164 int i;
131 unsigned long flags; 165 unsigned long flags;
@@ -134,17 +168,45 @@ void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
134 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); 168 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
135 169
136 spin_lock_irqsave(&irq->irq_lock, flags); 170 spin_lock_irqsave(&irq->irq_lock, flags);
137 irq->pending_latch = true; 171 if (irq->hw)
138 172 vgic_hw_irq_spending(vcpu, irq, is_uaccess);
173 else
174 irq->pending_latch = true;
139 vgic_queue_irq_unlock(vcpu->kvm, irq, flags); 175 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
140 vgic_put_irq(vcpu->kvm, irq); 176 vgic_put_irq(vcpu->kvm, irq);
141 } 177 }
142} 178}
143 179
180/* Must be called with irq->irq_lock held */
181static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
182 bool is_uaccess)
183{
184 if (is_uaccess)
185 return;
186
187 irq->pending_latch = false;
188
189 /*
190 * We don't want the guest to effectively mask the physical
191 * interrupt by doing a write to SPENDR followed by a write to
192 * CPENDR for HW interrupts, so we clear the active state on
193 * the physical side if the virtual interrupt is not active.
194 * This may lead to taking an additional interrupt on the
195 * host, but that should not be a problem as the worst that
196 * can happen is an additional vgic injection. We also clear
197 * the pending state to maintain proper semantics for edge HW
198 * interrupts.
199 */
200 vgic_irq_set_phys_pending(irq, false);
201 if (!irq->active)
202 vgic_irq_set_phys_active(irq, false);
203}
204
144void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, 205void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
145 gpa_t addr, unsigned int len, 206 gpa_t addr, unsigned int len,
146 unsigned long val) 207 unsigned long val)
147{ 208{
209 bool is_uaccess = !vgic_get_mmio_requester_vcpu();
148 u32 intid = VGIC_ADDR_TO_INTID(addr, 1); 210 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
149 int i; 211 int i;
150 unsigned long flags; 212 unsigned long flags;
@@ -154,7 +216,10 @@ void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
154 216
155 spin_lock_irqsave(&irq->irq_lock, flags); 217 spin_lock_irqsave(&irq->irq_lock, flags);
156 218
157 irq->pending_latch = false; 219 if (irq->hw)
220 vgic_hw_irq_cpending(vcpu, irq, is_uaccess);
221 else
222 irq->pending_latch = false;
158 223
159 spin_unlock_irqrestore(&irq->irq_lock, flags); 224 spin_unlock_irqrestore(&irq->irq_lock, flags);
160 vgic_put_irq(vcpu->kvm, irq); 225 vgic_put_irq(vcpu->kvm, irq);
@@ -181,27 +246,24 @@ unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
181 return value; 246 return value;
182} 247}
183 248
249/* Must be called with irq->irq_lock held */
250static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
251 bool active, bool is_uaccess)
252{
253 if (is_uaccess)
254 return;
255
256 irq->active = active;
257 vgic_irq_set_phys_active(irq, active);
258}
259
184static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, 260static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
185 bool new_active_state) 261 bool active)
186{ 262{
187 struct kvm_vcpu *requester_vcpu;
188 unsigned long flags; 263 unsigned long flags;
189 spin_lock_irqsave(&irq->irq_lock, flags); 264 struct kvm_vcpu *requester_vcpu = vgic_get_mmio_requester_vcpu();
190 265
191 /* 266 spin_lock_irqsave(&irq->irq_lock, flags);
192 * The vcpu parameter here can mean multiple things depending on how
193 * this function is called; when handling a trap from the kernel it
194 * depends on the GIC version, and these functions are also called as
195 * part of save/restore from userspace.
196 *
197 * Therefore, we have to figure out the requester in a reliable way.
198 *
199 * When accessing VGIC state from user space, the requester_vcpu is
200 * NULL, which is fine, because we guarantee that no VCPUs are running
201 * when accessing VGIC state from user space so irq->vcpu->cpu is
202 * always -1.
203 */
204 requester_vcpu = kvm_arm_get_running_vcpu();
205 267
206 /* 268 /*
207 * If this virtual IRQ was written into a list register, we 269 * If this virtual IRQ was written into a list register, we
@@ -213,14 +275,23 @@ static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
213 * vgic_change_active_prepare) and still has to sync back this IRQ, 275 * vgic_change_active_prepare) and still has to sync back this IRQ,
214 * so we release and re-acquire the spin_lock to let the other thread 276 * so we release and re-acquire the spin_lock to let the other thread
215 * sync back the IRQ. 277 * sync back the IRQ.
278 *
279 * When accessing VGIC state from user space, requester_vcpu is
280 * NULL, which is fine, because we guarantee that no VCPUs are running
281 * when accessing VGIC state from user space so irq->vcpu->cpu is
282 * always -1.
216 */ 283 */
217 while (irq->vcpu && /* IRQ may have state in an LR somewhere */ 284 while (irq->vcpu && /* IRQ may have state in an LR somewhere */
218 irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */ 285 irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */
219 irq->vcpu->cpu != -1) /* VCPU thread is running */ 286 irq->vcpu->cpu != -1) /* VCPU thread is running */
220 cond_resched_lock(&irq->irq_lock); 287 cond_resched_lock(&irq->irq_lock);
221 288
222 irq->active = new_active_state; 289 if (irq->hw)
223 if (new_active_state) 290 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu);
291 else
292 irq->active = active;
293
294 if (irq->active)
224 vgic_queue_irq_unlock(vcpu->kvm, irq, flags); 295 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
225 else 296 else
226 spin_unlock_irqrestore(&irq->irq_lock, flags); 297 spin_unlock_irqrestore(&irq->irq_lock, flags);
diff --git a/virt/kvm/arm/vgic/vgic-v2.c b/virt/kvm/arm/vgic/vgic-v2.c
index 80897102da26..c32d7b93ffd1 100644
--- a/virt/kvm/arm/vgic/vgic-v2.c
+++ b/virt/kvm/arm/vgic/vgic-v2.c
@@ -105,6 +105,26 @@ void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu)
105 irq->pending_latch = false; 105 irq->pending_latch = false;
106 } 106 }
107 107
108 /*
109 * Level-triggered mapped IRQs are special because we only
110 * observe rising edges as input to the VGIC.
111 *
112 * If the guest never acked the interrupt we have to sample
113 * the physical line and set the line level, because the
114 * device state could have changed or we simply need to
115 * process the still pending interrupt later.
116 *
117 * If this causes us to lower the level, we have to also clear
118 * the physical active state, since we will otherwise never be
119 * told when the interrupt becomes asserted again.
120 */
121 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT)) {
122 irq->line_level = vgic_get_phys_line_level(irq);
123
124 if (!irq->line_level)
125 vgic_irq_set_phys_active(irq, false);
126 }
127
108 spin_unlock_irqrestore(&irq->irq_lock, flags); 128 spin_unlock_irqrestore(&irq->irq_lock, flags);
109 vgic_put_irq(vcpu->kvm, irq); 129 vgic_put_irq(vcpu->kvm, irq);
110 } 130 }
@@ -162,6 +182,15 @@ void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
162 val |= GICH_LR_EOI; 182 val |= GICH_LR_EOI;
163 } 183 }
164 184
185 /*
186 * Level-triggered mapped IRQs are special because we only observe
187 * rising edges as input to the VGIC. We therefore lower the line
188 * level here, so that we can take new virtual IRQs. See
189 * vgic_v2_fold_lr_state for more info.
190 */
191 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT))
192 irq->line_level = false;
193
165 /* The GICv2 LR only holds five bits of priority. */ 194 /* The GICv2 LR only holds five bits of priority. */
166 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT; 195 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
167 196
diff --git a/virt/kvm/arm/vgic/vgic-v3.c b/virt/kvm/arm/vgic/vgic-v3.c
index f47e8481fa45..6b329414e57a 100644
--- a/virt/kvm/arm/vgic/vgic-v3.c
+++ b/virt/kvm/arm/vgic/vgic-v3.c
@@ -96,6 +96,26 @@ void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu)
96 irq->pending_latch = false; 96 irq->pending_latch = false;
97 } 97 }
98 98
99 /*
100 * Level-triggered mapped IRQs are special because we only
101 * observe rising edges as input to the VGIC.
102 *
103 * If the guest never acked the interrupt we have to sample
104 * the physical line and set the line level, because the
105 * device state could have changed or we simply need to
106 * process the still pending interrupt later.
107 *
108 * If this causes us to lower the level, we have to also clear
109 * the physical active state, since we will otherwise never be
110 * told when the interrupt becomes asserted again.
111 */
112 if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT)) {
113 irq->line_level = vgic_get_phys_line_level(irq);
114
115 if (!irq->line_level)
116 vgic_irq_set_phys_active(irq, false);
117 }
118
99 spin_unlock_irqrestore(&irq->irq_lock, flags); 119 spin_unlock_irqrestore(&irq->irq_lock, flags);
100 vgic_put_irq(vcpu->kvm, irq); 120 vgic_put_irq(vcpu->kvm, irq);
101 } 121 }
@@ -146,6 +166,15 @@ void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
146 } 166 }
147 167
148 /* 168 /*
169 * Level-triggered mapped IRQs are special because we only observe
170 * rising edges as input to the VGIC. We therefore lower the line
171 * level here, so that we can take new virtual IRQs. See
172 * vgic_v3_fold_lr_state for more info.
173 */
174 if (vgic_irq_is_mapped_level(irq) && (val & ICH_LR_PENDING_BIT))
175 irq->line_level = false;
176
177 /*
149 * We currently only support Group1 interrupts, which is a 178 * We currently only support Group1 interrupts, which is a
150 * known defect. This needs to be addressed at some point. 179 * known defect. This needs to be addressed at some point.
151 */ 180 */
diff --git a/virt/kvm/arm/vgic/vgic.c b/virt/kvm/arm/vgic/vgic.c
index ecb8e25f5fe5..c7c5ef190afa 100644
--- a/virt/kvm/arm/vgic/vgic.c
+++ b/virt/kvm/arm/vgic/vgic.c
@@ -144,6 +144,38 @@ void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq)
144 kfree(irq); 144 kfree(irq);
145} 145}
146 146
147void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending)
148{
149 WARN_ON(irq_set_irqchip_state(irq->host_irq,
150 IRQCHIP_STATE_PENDING,
151 pending));
152}
153
154bool vgic_get_phys_line_level(struct vgic_irq *irq)
155{
156 bool line_level;
157
158 BUG_ON(!irq->hw);
159
160 if (irq->get_input_level)
161 return irq->get_input_level(irq->intid);
162
163 WARN_ON(irq_get_irqchip_state(irq->host_irq,
164 IRQCHIP_STATE_PENDING,
165 &line_level));
166 return line_level;
167}
168
169/* Set/Clear the physical active state */
170void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active)
171{
172
173 BUG_ON(!irq->hw);
174 WARN_ON(irq_set_irqchip_state(irq->host_irq,
175 IRQCHIP_STATE_ACTIVE,
176 active));
177}
178
147/** 179/**
148 * kvm_vgic_target_oracle - compute the target vcpu for an irq 180 * kvm_vgic_target_oracle - compute the target vcpu for an irq
149 * 181 *
@@ -413,7 +445,8 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
413 445
414/* @irq->irq_lock must be held */ 446/* @irq->irq_lock must be held */
415static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq, 447static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
416 unsigned int host_irq) 448 unsigned int host_irq,
449 bool (*get_input_level)(int vindid))
417{ 450{
418 struct irq_desc *desc; 451 struct irq_desc *desc;
419 struct irq_data *data; 452 struct irq_data *data;
@@ -433,6 +466,7 @@ static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
433 irq->hw = true; 466 irq->hw = true;
434 irq->host_irq = host_irq; 467 irq->host_irq = host_irq;
435 irq->hwintid = data->hwirq; 468 irq->hwintid = data->hwirq;
469 irq->get_input_level = get_input_level;
436 return 0; 470 return 0;
437} 471}
438 472
@@ -441,10 +475,11 @@ static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq)
441{ 475{
442 irq->hw = false; 476 irq->hw = false;
443 irq->hwintid = 0; 477 irq->hwintid = 0;
478 irq->get_input_level = NULL;
444} 479}
445 480
446int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq, 481int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
447 u32 vintid) 482 u32 vintid, bool (*get_input_level)(int vindid))
448{ 483{
449 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid); 484 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid);
450 unsigned long flags; 485 unsigned long flags;
@@ -453,7 +488,7 @@ int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
453 BUG_ON(!irq); 488 BUG_ON(!irq);
454 489
455 spin_lock_irqsave(&irq->irq_lock, flags); 490 spin_lock_irqsave(&irq->irq_lock, flags);
456 ret = kvm_vgic_map_irq(vcpu, irq, host_irq); 491 ret = kvm_vgic_map_irq(vcpu, irq, host_irq, get_input_level);
457 spin_unlock_irqrestore(&irq->irq_lock, flags); 492 spin_unlock_irqrestore(&irq->irq_lock, flags);
458 vgic_put_irq(vcpu->kvm, irq); 493 vgic_put_irq(vcpu->kvm, irq);
459 494
diff --git a/virt/kvm/arm/vgic/vgic.h b/virt/kvm/arm/vgic/vgic.h
index efbcf8f96f9c..12c37b89f7a3 100644
--- a/virt/kvm/arm/vgic/vgic.h
+++ b/virt/kvm/arm/vgic/vgic.h
@@ -104,6 +104,11 @@ static inline bool irq_is_pending(struct vgic_irq *irq)
104 return irq->pending_latch || irq->line_level; 104 return irq->pending_latch || irq->line_level;
105} 105}
106 106
107static inline bool vgic_irq_is_mapped_level(struct vgic_irq *irq)
108{
109 return irq->config == VGIC_CONFIG_LEVEL && irq->hw;
110}
111
107/* 112/*
108 * This struct provides an intermediate representation of the fields contained 113 * This struct provides an intermediate representation of the fields contained
109 * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC 114 * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
@@ -140,6 +145,9 @@ vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
140struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, 145struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
141 u32 intid); 146 u32 intid);
142void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq); 147void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
148bool vgic_get_phys_line_level(struct vgic_irq *irq);
149void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending);
150void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active);
143bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq, 151bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
144 unsigned long flags); 152 unsigned long flags);
145void vgic_kick_vcpus(struct kvm *kvm); 153void vgic_kick_vcpus(struct kvm *kvm);