summaryrefslogtreecommitdiffstats
path: root/virt
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2015-11-04 10:24:17 -0500
committerPaolo Bonzini <pbonzini@redhat.com>2015-11-04 10:24:17 -0500
commit197a4f4b063e4e7a603ff1de56b3cf0400fabc30 (patch)
tree36a3d057cec3aff49cf2fe9df3e63218595dd68b /virt
parentd6cf98e06ea4c4071596bc28f2a0f21412d5c6dc (diff)
parent26caea7693cb99833fe4ecc544c842289d6b3f69 (diff)
Merge tag 'kvm-arm-for-4.4' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD
KVM/ARM Changes for v4.4-rc1 Includes a number of fixes for the arch-timer, introducing proper level-triggered semantics for the arch-timers, a series of patches to synchronously halt a guest (prerequisite for IRQ forwarding), some tracepoint improvements, a tweak for the EL2 panic handlers, some more VGIC cleanups getting rid of redundant state, and finally a stylistic change that gets rid of some ctags warnings. Conflicts: arch/x86/include/asm/kvm_host.h
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/arm/arch_timer.c174
-rw-r--r--virt/kvm/arm/trace.h63
-rw-r--r--virt/kvm/arm/vgic-v2.c6
-rw-r--r--virt/kvm/arm/vgic-v3.c6
-rw-r--r--virt/kvm/arm/vgic.c327
-rw-r--r--virt/kvm/kvm_main.c3
6 files changed, 331 insertions, 248 deletions
diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 48c6e1ac6827..21a0ab2d8919 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -28,6 +28,8 @@
28#include <kvm/arm_vgic.h> 28#include <kvm/arm_vgic.h>
29#include <kvm/arm_arch_timer.h> 29#include <kvm/arm_arch_timer.h>
30 30
31#include "trace.h"
32
31static struct timecounter *timecounter; 33static struct timecounter *timecounter;
32static struct workqueue_struct *wqueue; 34static struct workqueue_struct *wqueue;
33static unsigned int host_vtimer_irq; 35static unsigned int host_vtimer_irq;
@@ -59,18 +61,6 @@ static void timer_disarm(struct arch_timer_cpu *timer)
59 } 61 }
60} 62}
61 63
62static void kvm_timer_inject_irq(struct kvm_vcpu *vcpu)
63{
64 int ret;
65 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
66
67 kvm_vgic_set_phys_irq_active(timer->map, true);
68 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
69 timer->map,
70 timer->irq->level);
71 WARN_ON(ret);
72}
73
74static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id) 64static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
75{ 65{
76 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id; 66 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
@@ -111,14 +101,20 @@ static enum hrtimer_restart kvm_timer_expire(struct hrtimer *hrt)
111 return HRTIMER_NORESTART; 101 return HRTIMER_NORESTART;
112} 102}
113 103
104static bool kvm_timer_irq_can_fire(struct kvm_vcpu *vcpu)
105{
106 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
107
108 return !(timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) &&
109 (timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE);
110}
111
114bool kvm_timer_should_fire(struct kvm_vcpu *vcpu) 112bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
115{ 113{
116 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 114 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
117 cycle_t cval, now; 115 cycle_t cval, now;
118 116
119 if ((timer->cntv_ctl & ARCH_TIMER_CTRL_IT_MASK) || 117 if (!kvm_timer_irq_can_fire(vcpu))
120 !(timer->cntv_ctl & ARCH_TIMER_CTRL_ENABLE) ||
121 kvm_vgic_get_phys_irq_active(timer->map))
122 return false; 118 return false;
123 119
124 cval = timer->cntv_cval; 120 cval = timer->cntv_cval;
@@ -127,62 +123,143 @@ bool kvm_timer_should_fire(struct kvm_vcpu *vcpu)
127 return cval <= now; 123 return cval <= now;
128} 124}
129 125
126static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level)
127{
128 int ret;
129 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
130
131 BUG_ON(!vgic_initialized(vcpu->kvm));
132
133 timer->irq.level = new_level;
134 trace_kvm_timer_update_irq(vcpu->vcpu_id, timer->map->virt_irq,
135 timer->irq.level);
136 ret = kvm_vgic_inject_mapped_irq(vcpu->kvm, vcpu->vcpu_id,
137 timer->map,
138 timer->irq.level);
139 WARN_ON(ret);
140}
141
142/*
143 * Check if there was a change in the timer state (should we raise or lower
144 * the line level to the GIC).
145 */
146static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
147{
148 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
149
150 /*
151 * If userspace modified the timer registers via SET_ONE_REG before
152 * the vgic was initialized, we mustn't set the timer->irq.level value
153 * because the guest would never see the interrupt. Instead wait
154 * until we call this function from kvm_timer_flush_hwstate.
155 */
156 if (!vgic_initialized(vcpu->kvm))
157 return;
158
159 if (kvm_timer_should_fire(vcpu) != timer->irq.level)
160 kvm_timer_update_irq(vcpu, !timer->irq.level);
161}
162
163/*
164 * Schedule the background timer before calling kvm_vcpu_block, so that this
165 * thread is removed from its waitqueue and made runnable when there's a timer
166 * interrupt to handle.
167 */
168void kvm_timer_schedule(struct kvm_vcpu *vcpu)
169{
170 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
171 u64 ns;
172 cycle_t cval, now;
173
174 BUG_ON(timer_is_armed(timer));
175
176 /*
177 * No need to schedule a background timer if the guest timer has
178 * already expired, because kvm_vcpu_block will return before putting
179 * the thread to sleep.
180 */
181 if (kvm_timer_should_fire(vcpu))
182 return;
183
184 /*
185 * If the timer is not capable of raising interrupts (disabled or
186 * masked), then there's no more work for us to do.
187 */
188 if (!kvm_timer_irq_can_fire(vcpu))
189 return;
190
191 /* The timer has not yet expired, schedule a background timer */
192 cval = timer->cntv_cval;
193 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
194
195 ns = cyclecounter_cyc2ns(timecounter->cc,
196 cval - now,
197 timecounter->mask,
198 &timecounter->frac);
199 timer_arm(timer, ns);
200}
201
202void kvm_timer_unschedule(struct kvm_vcpu *vcpu)
203{
204 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
205 timer_disarm(timer);
206}
207
130/** 208/**
131 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu 209 * kvm_timer_flush_hwstate - prepare to move the virt timer to the cpu
132 * @vcpu: The vcpu pointer 210 * @vcpu: The vcpu pointer
133 * 211 *
134 * Disarm any pending soft timers, since the world-switch code will write the 212 * Check if the virtual timer has expired while we were running in the host,
135 * virtual timer state back to the physical CPU. 213 * and inject an interrupt if that was the case.
136 */ 214 */
137void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu) 215void kvm_timer_flush_hwstate(struct kvm_vcpu *vcpu)
138{ 216{
139 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 217 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
218 bool phys_active;
219 int ret;
140 220
141 /* 221 kvm_timer_update_state(vcpu);
142 * We're about to run this vcpu again, so there is no need to
143 * keep the background timer running, as we're about to
144 * populate the CPU timer again.
145 */
146 timer_disarm(timer);
147 222
148 /* 223 /*
149 * If the timer expired while we were not scheduled, now is the time 224 * If we enter the guest with the virtual input level to the VGIC
150 * to inject it. 225 * asserted, then we have already told the VGIC what we need to, and
226 * we don't need to exit from the guest until the guest deactivates
227 * the already injected interrupt, so therefore we should set the
228 * hardware active state to prevent unnecessary exits from the guest.
229 *
230 * Conversely, if the virtual input level is deasserted, then always
231 * clear the hardware active state to ensure that hardware interrupts
232 * from the timer triggers a guest exit.
151 */ 233 */
152 if (kvm_timer_should_fire(vcpu)) 234 if (timer->irq.level)
153 kvm_timer_inject_irq(vcpu); 235 phys_active = true;
236 else
237 phys_active = false;
238
239 ret = irq_set_irqchip_state(timer->map->irq,
240 IRQCHIP_STATE_ACTIVE,
241 phys_active);
242 WARN_ON(ret);
154} 243}
155 244
156/** 245/**
157 * kvm_timer_sync_hwstate - sync timer state from cpu 246 * kvm_timer_sync_hwstate - sync timer state from cpu
158 * @vcpu: The vcpu pointer 247 * @vcpu: The vcpu pointer
159 * 248 *
160 * Check if the virtual timer was armed and either schedule a corresponding 249 * Check if the virtual timer has expired while we were running in the guest,
161 * soft timer or inject directly if already expired. 250 * and inject an interrupt if that was the case.
162 */ 251 */
163void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu) 252void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
164{ 253{
165 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu; 254 struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
166 cycle_t cval, now;
167 u64 ns;
168 255
169 BUG_ON(timer_is_armed(timer)); 256 BUG_ON(timer_is_armed(timer));
170 257
171 if (kvm_timer_should_fire(vcpu)) { 258 /*
172 /* 259 * The guest could have modified the timer registers or the timer
173 * Timer has already expired while we were not 260 * could have expired, update the timer state.
174 * looking. Inject the interrupt and carry on. 261 */
175 */ 262 kvm_timer_update_state(vcpu);
176 kvm_timer_inject_irq(vcpu);
177 return;
178 }
179
180 cval = timer->cntv_cval;
181 now = kvm_phys_timer_read() - vcpu->kvm->arch.timer.cntvoff;
182
183 ns = cyclecounter_cyc2ns(timecounter->cc, cval - now, timecounter->mask,
184 &timecounter->frac);
185 timer_arm(timer, ns);
186} 263}
187 264
188int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu, 265int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
@@ -197,7 +274,7 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
197 * kvm_vcpu_set_target(). To handle this, we determine 274 * kvm_vcpu_set_target(). To handle this, we determine
198 * vcpu timer irq number when the vcpu is reset. 275 * vcpu timer irq number when the vcpu is reset.
199 */ 276 */
200 timer->irq = irq; 277 timer->irq.irq = irq->irq;
201 278
202 /* 279 /*
203 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8 280 * The bits in CNTV_CTL are architecturally reset to UNKNOWN for ARMv8
@@ -206,6 +283,7 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu,
206 * the ARMv7 architecture. 283 * the ARMv7 architecture.
207 */ 284 */
208 timer->cntv_ctl = 0; 285 timer->cntv_ctl = 0;
286 kvm_timer_update_state(vcpu);
209 287
210 /* 288 /*
211 * Tell the VGIC that the virtual interrupt is tied to a 289 * Tell the VGIC that the virtual interrupt is tied to a
@@ -250,6 +328,8 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value)
250 default: 328 default:
251 return -1; 329 return -1;
252 } 330 }
331
332 kvm_timer_update_state(vcpu);
253 return 0; 333 return 0;
254} 334}
255 335
diff --git a/virt/kvm/arm/trace.h b/virt/kvm/arm/trace.h
new file mode 100644
index 000000000000..37d8b98867d5
--- /dev/null
+++ b/virt/kvm/arm/trace.h
@@ -0,0 +1,63 @@
1#if !defined(_TRACE_KVM_H) || defined(TRACE_HEADER_MULTI_READ)
2#define _TRACE_KVM_H
3
4#include <linux/tracepoint.h>
5
6#undef TRACE_SYSTEM
7#define TRACE_SYSTEM kvm
8
9/*
10 * Tracepoints for vgic
11 */
12TRACE_EVENT(vgic_update_irq_pending,
13 TP_PROTO(unsigned long vcpu_id, __u32 irq, bool level),
14 TP_ARGS(vcpu_id, irq, level),
15
16 TP_STRUCT__entry(
17 __field( unsigned long, vcpu_id )
18 __field( __u32, irq )
19 __field( bool, level )
20 ),
21
22 TP_fast_assign(
23 __entry->vcpu_id = vcpu_id;
24 __entry->irq = irq;
25 __entry->level = level;
26 ),
27
28 TP_printk("VCPU: %ld, IRQ %d, level: %d",
29 __entry->vcpu_id, __entry->irq, __entry->level)
30);
31
32/*
33 * Tracepoints for arch_timer
34 */
35TRACE_EVENT(kvm_timer_update_irq,
36 TP_PROTO(unsigned long vcpu_id, __u32 irq, int level),
37 TP_ARGS(vcpu_id, irq, level),
38
39 TP_STRUCT__entry(
40 __field( unsigned long, vcpu_id )
41 __field( __u32, irq )
42 __field( int, level )
43 ),
44
45 TP_fast_assign(
46 __entry->vcpu_id = vcpu_id;
47 __entry->irq = irq;
48 __entry->level = level;
49 ),
50
51 TP_printk("VCPU: %ld, IRQ %d, level %d",
52 __entry->vcpu_id, __entry->irq, __entry->level)
53);
54
55#endif /* _TRACE_KVM_H */
56
57#undef TRACE_INCLUDE_PATH
58#define TRACE_INCLUDE_PATH ../../../virt/kvm/arm
59#undef TRACE_INCLUDE_FILE
60#define TRACE_INCLUDE_FILE trace
61
62/* This part must be outside protection */
63#include <trace/define_trace.h>
diff --git a/virt/kvm/arm/vgic-v2.c b/virt/kvm/arm/vgic-v2.c
index 8d7b04db8471..ff02f08df74d 100644
--- a/virt/kvm/arm/vgic-v2.c
+++ b/virt/kvm/arm/vgic-v2.c
@@ -79,11 +79,7 @@ static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr,
79 lr_val |= (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT); 79 lr_val |= (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT);
80 80
81 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val; 81 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val;
82}
83 82
84static void vgic_v2_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
85 struct vgic_lr lr_desc)
86{
87 if (!(lr_desc.state & LR_STATE_MASK)) 83 if (!(lr_desc.state & LR_STATE_MASK))
88 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr |= (1ULL << lr); 84 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr |= (1ULL << lr);
89 else 85 else
@@ -158,6 +154,7 @@ static void vgic_v2_enable(struct kvm_vcpu *vcpu)
158 * anyway. 154 * anyway.
159 */ 155 */
160 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0; 156 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
157 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0;
161 158
162 /* Get the show on the road... */ 159 /* Get the show on the road... */
163 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN; 160 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
@@ -166,7 +163,6 @@ static void vgic_v2_enable(struct kvm_vcpu *vcpu)
166static const struct vgic_ops vgic_v2_ops = { 163static const struct vgic_ops vgic_v2_ops = {
167 .get_lr = vgic_v2_get_lr, 164 .get_lr = vgic_v2_get_lr,
168 .set_lr = vgic_v2_set_lr, 165 .set_lr = vgic_v2_set_lr,
169 .sync_lr_elrsr = vgic_v2_sync_lr_elrsr,
170 .get_elrsr = vgic_v2_get_elrsr, 166 .get_elrsr = vgic_v2_get_elrsr,
171 .get_eisr = vgic_v2_get_eisr, 167 .get_eisr = vgic_v2_get_eisr,
172 .clear_eisr = vgic_v2_clear_eisr, 168 .clear_eisr = vgic_v2_clear_eisr,
diff --git a/virt/kvm/arm/vgic-v3.c b/virt/kvm/arm/vgic-v3.c
index 7dd5d62f10a1..487d6357b7e7 100644
--- a/virt/kvm/arm/vgic-v3.c
+++ b/virt/kvm/arm/vgic-v3.c
@@ -112,11 +112,7 @@ static void vgic_v3_set_lr(struct kvm_vcpu *vcpu, int lr,
112 } 112 }
113 113
114 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val; 114 vcpu->arch.vgic_cpu.vgic_v3.vgic_lr[LR_INDEX(lr)] = lr_val;
115}
116 115
117static void vgic_v3_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
118 struct vgic_lr lr_desc)
119{
120 if (!(lr_desc.state & LR_STATE_MASK)) 116 if (!(lr_desc.state & LR_STATE_MASK))
121 vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr |= (1U << lr); 117 vcpu->arch.vgic_cpu.vgic_v3.vgic_elrsr |= (1U << lr);
122 else 118 else
@@ -193,6 +189,7 @@ static void vgic_v3_enable(struct kvm_vcpu *vcpu)
193 * anyway. 189 * anyway.
194 */ 190 */
195 vgic_v3->vgic_vmcr = 0; 191 vgic_v3->vgic_vmcr = 0;
192 vgic_v3->vgic_elrsr = ~0;
196 193
197 /* 194 /*
198 * If we are emulating a GICv3, we do it in an non-GICv2-compatible 195 * If we are emulating a GICv3, we do it in an non-GICv2-compatible
@@ -211,7 +208,6 @@ static void vgic_v3_enable(struct kvm_vcpu *vcpu)
211static const struct vgic_ops vgic_v3_ops = { 208static const struct vgic_ops vgic_v3_ops = {
212 .get_lr = vgic_v3_get_lr, 209 .get_lr = vgic_v3_get_lr,
213 .set_lr = vgic_v3_set_lr, 210 .set_lr = vgic_v3_set_lr,
214 .sync_lr_elrsr = vgic_v3_sync_lr_elrsr,
215 .get_elrsr = vgic_v3_get_elrsr, 211 .get_elrsr = vgic_v3_get_elrsr,
216 .get_eisr = vgic_v3_get_eisr, 212 .get_eisr = vgic_v3_get_eisr,
217 .clear_eisr = vgic_v3_clear_eisr, 213 .clear_eisr = vgic_v3_clear_eisr,
diff --git a/virt/kvm/arm/vgic.c b/virt/kvm/arm/vgic.c
index 6bd1c9bf7ae7..fe451d4885ae 100644
--- a/virt/kvm/arm/vgic.c
+++ b/virt/kvm/arm/vgic.c
@@ -34,6 +34,9 @@
34#include <asm/kvm.h> 34#include <asm/kvm.h>
35#include <kvm/iodev.h> 35#include <kvm/iodev.h>
36 36
37#define CREATE_TRACE_POINTS
38#include "trace.h"
39
37/* 40/*
38 * How the whole thing works (courtesy of Christoffer Dall): 41 * How the whole thing works (courtesy of Christoffer Dall):
39 * 42 *
@@ -102,11 +105,13 @@
102#include "vgic.h" 105#include "vgic.h"
103 106
104static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu); 107static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
105static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu); 108static void vgic_retire_lr(int lr_nr, struct kvm_vcpu *vcpu);
106static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr); 109static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr);
107static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc); 110static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc);
111static u64 vgic_get_elrsr(struct kvm_vcpu *vcpu);
108static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu, 112static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu,
109 int virt_irq); 113 int virt_irq);
114static int compute_pending_for_cpu(struct kvm_vcpu *vcpu);
110 115
111static const struct vgic_ops *vgic_ops; 116static const struct vgic_ops *vgic_ops;
112static const struct vgic_params *vgic; 117static const struct vgic_params *vgic;
@@ -357,6 +362,11 @@ static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
357 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 362 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
358 363
359 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0); 364 vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
365 if (!vgic_dist_irq_get_level(vcpu, irq)) {
366 vgic_dist_irq_clear_pending(vcpu, irq);
367 if (!compute_pending_for_cpu(vcpu))
368 clear_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
369 }
360} 370}
361 371
362static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq) 372static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
@@ -654,10 +664,9 @@ bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
654 vgic_reg_access(mmio, &val, offset, 664 vgic_reg_access(mmio, &val, offset,
655 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE); 665 ACCESS_READ_VALUE | ACCESS_WRITE_VALUE);
656 if (mmio->is_write) { 666 if (mmio->is_write) {
657 if (offset < 8) { 667 /* Ignore writes to read-only SGI and PPI bits */
658 *reg = ~0U; /* Force PPIs/SGIs to 1 */ 668 if (offset < 8)
659 return false; 669 return false;
660 }
661 670
662 val = vgic_cfg_compress(val); 671 val = vgic_cfg_compress(val);
663 if (offset & 4) { 672 if (offset & 4) {
@@ -683,9 +692,11 @@ bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio,
683void vgic_unqueue_irqs(struct kvm_vcpu *vcpu) 692void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
684{ 693{
685 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 694 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
695 u64 elrsr = vgic_get_elrsr(vcpu);
696 unsigned long *elrsr_ptr = u64_to_bitmask(&elrsr);
686 int i; 697 int i;
687 698
688 for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) { 699 for_each_clear_bit(i, elrsr_ptr, vgic_cpu->nr_lr) {
689 struct vgic_lr lr = vgic_get_lr(vcpu, i); 700 struct vgic_lr lr = vgic_get_lr(vcpu, i);
690 701
691 /* 702 /*
@@ -706,30 +717,14 @@ void vgic_unqueue_irqs(struct kvm_vcpu *vcpu)
706 * interrupt then move the active state to the 717 * interrupt then move the active state to the
707 * distributor tracking bit. 718 * distributor tracking bit.
708 */ 719 */
709 if (lr.state & LR_STATE_ACTIVE) { 720 if (lr.state & LR_STATE_ACTIVE)
710 vgic_irq_set_active(vcpu, lr.irq); 721 vgic_irq_set_active(vcpu, lr.irq);
711 lr.state &= ~LR_STATE_ACTIVE;
712 }
713 722
714 /* 723 /*
715 * Reestablish the pending state on the distributor and the 724 * Reestablish the pending state on the distributor and the
716 * CPU interface. It may have already been pending, but that 725 * CPU interface and mark the LR as free for other use.
717 * is fine, then we are only setting a few bits that were
718 * already set.
719 */
720 if (lr.state & LR_STATE_PENDING) {
721 vgic_dist_irq_set_pending(vcpu, lr.irq);
722 lr.state &= ~LR_STATE_PENDING;
723 }
724
725 vgic_set_lr(vcpu, i, lr);
726
727 /*
728 * Mark the LR as free for other use.
729 */ 726 */
730 BUG_ON(lr.state & LR_STATE_MASK); 727 vgic_retire_lr(i, vcpu);
731 vgic_retire_lr(i, lr.irq, vcpu);
732 vgic_irq_clear_queued(vcpu, lr.irq);
733 728
734 /* Finally update the VGIC state. */ 729 /* Finally update the VGIC state. */
735 vgic_update_state(vcpu->kvm); 730 vgic_update_state(vcpu->kvm);
@@ -982,6 +977,12 @@ static int compute_pending_for_cpu(struct kvm_vcpu *vcpu)
982 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu; 977 pend_percpu = vcpu->arch.vgic_cpu.pending_percpu;
983 pend_shared = vcpu->arch.vgic_cpu.pending_shared; 978 pend_shared = vcpu->arch.vgic_cpu.pending_shared;
984 979
980 if (!dist->enabled) {
981 bitmap_zero(pend_percpu, VGIC_NR_PRIVATE_IRQS);
982 bitmap_zero(pend_shared, nr_shared);
983 return 0;
984 }
985
985 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id); 986 pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id);
986 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id); 987 enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id);
987 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS); 988 bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS);
@@ -1009,11 +1010,6 @@ void vgic_update_state(struct kvm *kvm)
1009 struct kvm_vcpu *vcpu; 1010 struct kvm_vcpu *vcpu;
1010 int c; 1011 int c;
1011 1012
1012 if (!dist->enabled) {
1013 set_bit(0, dist->irq_pending_on_cpu);
1014 return;
1015 }
1016
1017 kvm_for_each_vcpu(c, vcpu, kvm) { 1013 kvm_for_each_vcpu(c, vcpu, kvm) {
1018 if (compute_pending_for_cpu(vcpu)) 1014 if (compute_pending_for_cpu(vcpu))
1019 set_bit(c, dist->irq_pending_on_cpu); 1015 set_bit(c, dist->irq_pending_on_cpu);
@@ -1036,12 +1032,6 @@ static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr,
1036 vgic_ops->set_lr(vcpu, lr, vlr); 1032 vgic_ops->set_lr(vcpu, lr, vlr);
1037} 1033}
1038 1034
1039static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr,
1040 struct vgic_lr vlr)
1041{
1042 vgic_ops->sync_lr_elrsr(vcpu, lr, vlr);
1043}
1044
1045static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu) 1035static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu)
1046{ 1036{
1047 return vgic_ops->get_elrsr(vcpu); 1037 return vgic_ops->get_elrsr(vcpu);
@@ -1087,16 +1077,23 @@ static inline void vgic_enable(struct kvm_vcpu *vcpu)
1087 vgic_ops->enable(vcpu); 1077 vgic_ops->enable(vcpu);
1088} 1078}
1089 1079
1090static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu) 1080static void vgic_retire_lr(int lr_nr, struct kvm_vcpu *vcpu)
1091{ 1081{
1092 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1093 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr); 1082 struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr);
1094 1083
1084 vgic_irq_clear_queued(vcpu, vlr.irq);
1085
1086 /*
1087 * We must transfer the pending state back to the distributor before
1088 * retiring the LR, otherwise we may loose edge-triggered interrupts.
1089 */
1090 if (vlr.state & LR_STATE_PENDING) {
1091 vgic_dist_irq_set_pending(vcpu, vlr.irq);
1092 vlr.hwirq = 0;
1093 }
1094
1095 vlr.state = 0; 1095 vlr.state = 0;
1096 vgic_set_lr(vcpu, lr_nr, vlr); 1096 vgic_set_lr(vcpu, lr_nr, vlr);
1097 clear_bit(lr_nr, vgic_cpu->lr_used);
1098 vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY;
1099 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1100} 1097}
1101 1098
1102/* 1099/*
@@ -1110,17 +1107,15 @@ static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu)
1110 */ 1107 */
1111static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu) 1108static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu)
1112{ 1109{
1113 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 1110 u64 elrsr = vgic_get_elrsr(vcpu);
1111 unsigned long *elrsr_ptr = u64_to_bitmask(&elrsr);
1114 int lr; 1112 int lr;
1115 1113
1116 for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) { 1114 for_each_clear_bit(lr, elrsr_ptr, vgic->nr_lr) {
1117 struct vgic_lr vlr = vgic_get_lr(vcpu, lr); 1115 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1118 1116
1119 if (!vgic_irq_is_enabled(vcpu, vlr.irq)) { 1117 if (!vgic_irq_is_enabled(vcpu, vlr.irq))
1120 vgic_retire_lr(lr, vlr.irq, vcpu); 1118 vgic_retire_lr(lr, vcpu);
1121 if (vgic_irq_is_queued(vcpu, vlr.irq))
1122 vgic_irq_clear_queued(vcpu, vlr.irq);
1123 }
1124 } 1119 }
1125} 1120}
1126 1121
@@ -1132,7 +1127,8 @@ static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1132 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state); 1127 kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state);
1133 vgic_irq_clear_active(vcpu, irq); 1128 vgic_irq_clear_active(vcpu, irq);
1134 vgic_update_state(vcpu->kvm); 1129 vgic_update_state(vcpu->kvm);
1135 } else if (vgic_dist_irq_is_pending(vcpu, irq)) { 1130 } else {
1131 WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq));
1136 vlr.state |= LR_STATE_PENDING; 1132 vlr.state |= LR_STATE_PENDING;
1137 kvm_debug("Set pending: 0x%x\n", vlr.state); 1133 kvm_debug("Set pending: 0x%x\n", vlr.state);
1138 } 1134 }
@@ -1159,7 +1155,6 @@ static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1159 } 1155 }
1160 1156
1161 vgic_set_lr(vcpu, lr_nr, vlr); 1157 vgic_set_lr(vcpu, lr_nr, vlr);
1162 vgic_sync_lr_elrsr(vcpu, lr_nr, vlr);
1163} 1158}
1164 1159
1165/* 1160/*
@@ -1169,8 +1164,9 @@ static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq,
1169 */ 1164 */
1170bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq) 1165bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1171{ 1166{
1172 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1173 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1167 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1168 u64 elrsr = vgic_get_elrsr(vcpu);
1169 unsigned long *elrsr_ptr = u64_to_bitmask(&elrsr);
1174 struct vgic_lr vlr; 1170 struct vgic_lr vlr;
1175 int lr; 1171 int lr;
1176 1172
@@ -1181,28 +1177,22 @@ bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq)
1181 1177
1182 kvm_debug("Queue IRQ%d\n", irq); 1178 kvm_debug("Queue IRQ%d\n", irq);
1183 1179
1184 lr = vgic_cpu->vgic_irq_lr_map[irq];
1185
1186 /* Do we have an active interrupt for the same CPUID? */ 1180 /* Do we have an active interrupt for the same CPUID? */
1187 if (lr != LR_EMPTY) { 1181 for_each_clear_bit(lr, elrsr_ptr, vgic->nr_lr) {
1188 vlr = vgic_get_lr(vcpu, lr); 1182 vlr = vgic_get_lr(vcpu, lr);
1189 if (vlr.source == sgi_source_id) { 1183 if (vlr.irq == irq && vlr.source == sgi_source_id) {
1190 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq); 1184 kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq);
1191 BUG_ON(!test_bit(lr, vgic_cpu->lr_used));
1192 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr); 1185 vgic_queue_irq_to_lr(vcpu, irq, lr, vlr);
1193 return true; 1186 return true;
1194 } 1187 }
1195 } 1188 }
1196 1189
1197 /* Try to use another LR for this interrupt */ 1190 /* Try to use another LR for this interrupt */
1198 lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used, 1191 lr = find_first_bit(elrsr_ptr, vgic->nr_lr);
1199 vgic->nr_lr);
1200 if (lr >= vgic->nr_lr) 1192 if (lr >= vgic->nr_lr)
1201 return false; 1193 return false;
1202 1194
1203 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id); 1195 kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id);
1204 vgic_cpu->vgic_irq_lr_map[irq] = lr;
1205 set_bit(lr, vgic_cpu->lr_used);
1206 1196
1207 vlr.irq = irq; 1197 vlr.irq = irq;
1208 vlr.source = sgi_source_id; 1198 vlr.source = sgi_source_id;
@@ -1240,7 +1230,7 @@ static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
1240 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 1230 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1241 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1231 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1242 unsigned long *pa_percpu, *pa_shared; 1232 unsigned long *pa_percpu, *pa_shared;
1243 int i, vcpu_id, lr, ret; 1233 int i, vcpu_id;
1244 int overflow = 0; 1234 int overflow = 0;
1245 int nr_shared = vgic_nr_shared_irqs(dist); 1235 int nr_shared = vgic_nr_shared_irqs(dist);
1246 1236
@@ -1295,39 +1285,62 @@ epilog:
1295 */ 1285 */
1296 clear_bit(vcpu_id, dist->irq_pending_on_cpu); 1286 clear_bit(vcpu_id, dist->irq_pending_on_cpu);
1297 } 1287 }
1288}
1298 1289
1299 for (lr = 0; lr < vgic->nr_lr; lr++) { 1290static int process_queued_irq(struct kvm_vcpu *vcpu,
1300 struct vgic_lr vlr; 1291 int lr, struct vgic_lr vlr)
1292{
1293 int pending = 0;
1301 1294
1302 if (!test_bit(lr, vgic_cpu->lr_used)) 1295 /*
1303 continue; 1296 * If the IRQ was EOIed (called from vgic_process_maintenance) or it
1297 * went from active to non-active (called from vgic_sync_hwirq) it was
1298 * also ACKed and we we therefore assume we can clear the soft pending
1299 * state (should it had been set) for this interrupt.
1300 *
1301 * Note: if the IRQ soft pending state was set after the IRQ was
1302 * acked, it actually shouldn't be cleared, but we have no way of
1303 * knowing that unless we start trapping ACKs when the soft-pending
1304 * state is set.
1305 */
1306 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1304 1307
1305 vlr = vgic_get_lr(vcpu, lr); 1308 /*
1309 * Tell the gic to start sampling this interrupt again.
1310 */
1311 vgic_irq_clear_queued(vcpu, vlr.irq);
1306 1312
1307 /* 1313 /* Any additional pending interrupt? */
1308 * If we have a mapping, and the virtual interrupt is 1314 if (vgic_irq_is_edge(vcpu, vlr.irq)) {
1309 * presented to the guest (as pending or active), then we must 1315 BUG_ON(!(vlr.state & LR_HW));
1310 * set the state to active in the physical world. See 1316 pending = vgic_dist_irq_is_pending(vcpu, vlr.irq);
1311 * Documentation/virtual/kvm/arm/vgic-mapped-irqs.txt. 1317 } else {
1312 */ 1318 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
1313 if (vlr.state & LR_HW) { 1319 vgic_cpu_irq_set(vcpu, vlr.irq);
1314 struct irq_phys_map *map; 1320 pending = 1;
1315 map = vgic_irq_map_search(vcpu, vlr.irq); 1321 } else {
1316 1322 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
1317 ret = irq_set_irqchip_state(map->irq, 1323 vgic_cpu_irq_clear(vcpu, vlr.irq);
1318 IRQCHIP_STATE_ACTIVE,
1319 true);
1320 WARN_ON(ret);
1321 } 1324 }
1322 } 1325 }
1326
1327 /*
1328 * Despite being EOIed, the LR may not have
1329 * been marked as empty.
1330 */
1331 vlr.state = 0;
1332 vlr.hwirq = 0;
1333 vgic_set_lr(vcpu, lr, vlr);
1334
1335 return pending;
1323} 1336}
1324 1337
1325static bool vgic_process_maintenance(struct kvm_vcpu *vcpu) 1338static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1326{ 1339{
1327 u32 status = vgic_get_interrupt_status(vcpu); 1340 u32 status = vgic_get_interrupt_status(vcpu);
1328 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1341 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1329 bool level_pending = false;
1330 struct kvm *kvm = vcpu->kvm; 1342 struct kvm *kvm = vcpu->kvm;
1343 int level_pending = 0;
1331 1344
1332 kvm_debug("STATUS = %08x\n", status); 1345 kvm_debug("STATUS = %08x\n", status);
1333 1346
@@ -1342,54 +1355,22 @@ static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1342 1355
1343 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) { 1356 for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
1344 struct vgic_lr vlr = vgic_get_lr(vcpu, lr); 1357 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1345 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
1346 1358
1347 spin_lock(&dist->lock); 1359 WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
1348 vgic_irq_clear_queued(vcpu, vlr.irq);
1349 WARN_ON(vlr.state & LR_STATE_MASK); 1360 WARN_ON(vlr.state & LR_STATE_MASK);
1350 vlr.state = 0;
1351 vgic_set_lr(vcpu, lr, vlr);
1352 1361
1353 /*
1354 * If the IRQ was EOIed it was also ACKed and we we
1355 * therefore assume we can clear the soft pending
1356 * state (should it had been set) for this interrupt.
1357 *
1358 * Note: if the IRQ soft pending state was set after
1359 * the IRQ was acked, it actually shouldn't be
1360 * cleared, but we have no way of knowing that unless
1361 * we start trapping ACKs when the soft-pending state
1362 * is set.
1363 */
1364 vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1365 1362
1366 /* 1363 /*
1367 * kvm_notify_acked_irq calls kvm_set_irq() 1364 * kvm_notify_acked_irq calls kvm_set_irq()
1368 * to reset the IRQ level. Need to release the 1365 * to reset the IRQ level, which grabs the dist->lock
1369 * lock for kvm_set_irq to grab it. 1366 * so we call this before taking the dist->lock.
1370 */ 1367 */
1371 spin_unlock(&dist->lock);
1372
1373 kvm_notify_acked_irq(kvm, 0, 1368 kvm_notify_acked_irq(kvm, 0,
1374 vlr.irq - VGIC_NR_PRIVATE_IRQS); 1369 vlr.irq - VGIC_NR_PRIVATE_IRQS);
1375 spin_lock(&dist->lock);
1376
1377 /* Any additional pending interrupt? */
1378 if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
1379 vgic_cpu_irq_set(vcpu, vlr.irq);
1380 level_pending = true;
1381 } else {
1382 vgic_dist_irq_clear_pending(vcpu, vlr.irq);
1383 vgic_cpu_irq_clear(vcpu, vlr.irq);
1384 }
1385 1370
1371 spin_lock(&dist->lock);
1372 level_pending |= process_queued_irq(vcpu, lr, vlr);
1386 spin_unlock(&dist->lock); 1373 spin_unlock(&dist->lock);
1387
1388 /*
1389 * Despite being EOIed, the LR may not have
1390 * been marked as empty.
1391 */
1392 vgic_sync_lr_elrsr(vcpu, lr, vlr);
1393 } 1374 }
1394 } 1375 }
1395 1376
@@ -1410,40 +1391,40 @@ static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
1410/* 1391/*
1411 * Save the physical active state, and reset it to inactive. 1392 * Save the physical active state, and reset it to inactive.
1412 * 1393 *
1413 * Return 1 if HW interrupt went from active to inactive, and 0 otherwise. 1394 * Return true if there's a pending forwarded interrupt to queue.
1414 */ 1395 */
1415static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr) 1396static bool vgic_sync_hwirq(struct kvm_vcpu *vcpu, int lr, struct vgic_lr vlr)
1416{ 1397{
1398 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1417 struct irq_phys_map *map; 1399 struct irq_phys_map *map;
1400 bool phys_active;
1401 bool level_pending;
1418 int ret; 1402 int ret;
1419 1403
1420 if (!(vlr.state & LR_HW)) 1404 if (!(vlr.state & LR_HW))
1421 return 0; 1405 return false;
1422 1406
1423 map = vgic_irq_map_search(vcpu, vlr.irq); 1407 map = vgic_irq_map_search(vcpu, vlr.irq);
1424 BUG_ON(!map || !map->active); 1408 BUG_ON(!map);
1425 1409
1426 ret = irq_get_irqchip_state(map->irq, 1410 ret = irq_get_irqchip_state(map->irq,
1427 IRQCHIP_STATE_ACTIVE, 1411 IRQCHIP_STATE_ACTIVE,
1428 &map->active); 1412 &phys_active);
1429 1413
1430 WARN_ON(ret); 1414 WARN_ON(ret);
1431 1415
1432 if (map->active) { 1416 if (phys_active)
1433 ret = irq_set_irqchip_state(map->irq,
1434 IRQCHIP_STATE_ACTIVE,
1435 false);
1436 WARN_ON(ret);
1437 return 0; 1417 return 0;
1438 }
1439 1418
1440 return 1; 1419 spin_lock(&dist->lock);
1420 level_pending = process_queued_irq(vcpu, lr, vlr);
1421 spin_unlock(&dist->lock);
1422 return level_pending;
1441} 1423}
1442 1424
1443/* Sync back the VGIC state after a guest run */ 1425/* Sync back the VGIC state after a guest run */
1444static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) 1426static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1445{ 1427{
1446 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1447 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 1428 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1448 u64 elrsr; 1429 u64 elrsr;
1449 unsigned long *elrsr_ptr; 1430 unsigned long *elrsr_ptr;
@@ -1451,40 +1432,18 @@ static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
1451 bool level_pending; 1432 bool level_pending;
1452 1433
1453 level_pending = vgic_process_maintenance(vcpu); 1434 level_pending = vgic_process_maintenance(vcpu);
1454 elrsr = vgic_get_elrsr(vcpu);
1455 elrsr_ptr = u64_to_bitmask(&elrsr);
1456 1435
1457 /* Deal with HW interrupts, and clear mappings for empty LRs */ 1436 /* Deal with HW interrupts, and clear mappings for empty LRs */
1458 for (lr = 0; lr < vgic->nr_lr; lr++) { 1437 for (lr = 0; lr < vgic->nr_lr; lr++) {
1459 struct vgic_lr vlr; 1438 struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1460
1461 if (!test_bit(lr, vgic_cpu->lr_used))
1462 continue;
1463
1464 vlr = vgic_get_lr(vcpu, lr);
1465 if (vgic_sync_hwirq(vcpu, vlr)) {
1466 /*
1467 * So this is a HW interrupt that the guest
1468 * EOI-ed. Clean the LR state and allow the
1469 * interrupt to be sampled again.
1470 */
1471 vlr.state = 0;
1472 vlr.hwirq = 0;
1473 vgic_set_lr(vcpu, lr, vlr);
1474 vgic_irq_clear_queued(vcpu, vlr.irq);
1475 set_bit(lr, elrsr_ptr);
1476 }
1477
1478 if (!test_bit(lr, elrsr_ptr))
1479 continue;
1480
1481 clear_bit(lr, vgic_cpu->lr_used);
1482 1439
1440 level_pending |= vgic_sync_hwirq(vcpu, lr, vlr);
1483 BUG_ON(vlr.irq >= dist->nr_irqs); 1441 BUG_ON(vlr.irq >= dist->nr_irqs);
1484 vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY;
1485 } 1442 }
1486 1443
1487 /* Check if we still have something up our sleeve... */ 1444 /* Check if we still have something up our sleeve... */
1445 elrsr = vgic_get_elrsr(vcpu);
1446 elrsr_ptr = u64_to_bitmask(&elrsr);
1488 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr); 1447 pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr);
1489 if (level_pending || pending < vgic->nr_lr) 1448 if (level_pending || pending < vgic->nr_lr)
1490 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); 1449 set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu);
@@ -1574,6 +1533,8 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1574 int enabled; 1533 int enabled;
1575 bool ret = true, can_inject = true; 1534 bool ret = true, can_inject = true;
1576 1535
1536 trace_vgic_update_irq_pending(cpuid, irq_num, level);
1537
1577 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020)) 1538 if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020))
1578 return -EINVAL; 1539 return -EINVAL;
1579 1540
@@ -1607,8 +1568,12 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
1607 } else { 1568 } else {
1608 if (level_triggered) { 1569 if (level_triggered) {
1609 vgic_dist_irq_clear_level(vcpu, irq_num); 1570 vgic_dist_irq_clear_level(vcpu, irq_num);
1610 if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) 1571 if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) {
1611 vgic_dist_irq_clear_pending(vcpu, irq_num); 1572 vgic_dist_irq_clear_pending(vcpu, irq_num);
1573 vgic_cpu_irq_clear(vcpu, irq_num);
1574 if (!compute_pending_for_cpu(vcpu))
1575 clear_bit(cpuid, dist->irq_pending_on_cpu);
1576 }
1612 } 1577 }
1613 1578
1614 ret = false; 1579 ret = false;
@@ -1849,30 +1814,6 @@ static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu)
1849} 1814}
1850 1815
1851/** 1816/**
1852 * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ
1853 *
1854 * Return the logical active state of a mapped interrupt. This doesn't
1855 * necessarily reflects the current HW state.
1856 */
1857bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map)
1858{
1859 BUG_ON(!map);
1860 return map->active;
1861}
1862
1863/**
1864 * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ
1865 *
1866 * Set the logical active state of a mapped interrupt. This doesn't
1867 * immediately affects the HW state.
1868 */
1869void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active)
1870{
1871 BUG_ON(!map);
1872 map->active = active;
1873}
1874
1875/**
1876 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping 1817 * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping
1877 * @vcpu: The VCPU pointer 1818 * @vcpu: The VCPU pointer
1878 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq 1819 * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq
@@ -1927,12 +1868,10 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
1927 kfree(vgic_cpu->pending_shared); 1868 kfree(vgic_cpu->pending_shared);
1928 kfree(vgic_cpu->active_shared); 1869 kfree(vgic_cpu->active_shared);
1929 kfree(vgic_cpu->pend_act_shared); 1870 kfree(vgic_cpu->pend_act_shared);
1930 kfree(vgic_cpu->vgic_irq_lr_map);
1931 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list); 1871 vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list);
1932 vgic_cpu->pending_shared = NULL; 1872 vgic_cpu->pending_shared = NULL;
1933 vgic_cpu->active_shared = NULL; 1873 vgic_cpu->active_shared = NULL;
1934 vgic_cpu->pend_act_shared = NULL; 1874 vgic_cpu->pend_act_shared = NULL;
1935 vgic_cpu->vgic_irq_lr_map = NULL;
1936} 1875}
1937 1876
1938static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs) 1877static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
@@ -1943,18 +1882,14 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
1943 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL); 1882 vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
1944 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL); 1883 vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL);
1945 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL); 1884 vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL);
1946 vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
1947 1885
1948 if (!vgic_cpu->pending_shared 1886 if (!vgic_cpu->pending_shared
1949 || !vgic_cpu->active_shared 1887 || !vgic_cpu->active_shared
1950 || !vgic_cpu->pend_act_shared 1888 || !vgic_cpu->pend_act_shared) {
1951 || !vgic_cpu->vgic_irq_lr_map) {
1952 kvm_vgic_vcpu_destroy(vcpu); 1889 kvm_vgic_vcpu_destroy(vcpu);
1953 return -ENOMEM; 1890 return -ENOMEM;
1954 } 1891 }
1955 1892
1956 memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
1957
1958 /* 1893 /*
1959 * Store the number of LRs per vcpu, so we don't have to go 1894 * Store the number of LRs per vcpu, so we don't have to go
1960 * all the way to the distributor structure to find out. Only 1895 * all the way to the distributor structure to find out. Only
@@ -2096,14 +2031,24 @@ int vgic_init(struct kvm *kvm)
2096 break; 2031 break;
2097 } 2032 }
2098 2033
2099 for (i = 0; i < dist->nr_irqs; i++) { 2034 /*
2100 if (i < VGIC_NR_PPIS) 2035 * Enable and configure all SGIs to be edge-triggere and
2036 * configure all PPIs as level-triggered.
2037 */
2038 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
2039 if (i < VGIC_NR_SGIS) {
2040 /* SGIs */
2101 vgic_bitmap_set_irq_val(&dist->irq_enabled, 2041 vgic_bitmap_set_irq_val(&dist->irq_enabled,
2102 vcpu->vcpu_id, i, 1); 2042 vcpu->vcpu_id, i, 1);
2103 if (i < VGIC_NR_PRIVATE_IRQS)
2104 vgic_bitmap_set_irq_val(&dist->irq_cfg, 2043 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2105 vcpu->vcpu_id, i, 2044 vcpu->vcpu_id, i,
2106 VGIC_CFG_EDGE); 2045 VGIC_CFG_EDGE);
2046 } else if (i < VGIC_NR_PRIVATE_IRQS) {
2047 /* PPIs */
2048 vgic_bitmap_set_irq_val(&dist->irq_cfg,
2049 vcpu->vcpu_id, i,
2050 VGIC_CFG_LEVEL);
2051 }
2107 } 2052 }
2108 2053
2109 vgic_enable(vcpu); 2054 vgic_enable(vcpu);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index a75502c93c3e..484079efea5b 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2021,6 +2021,8 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
2021 } while (single_task_running() && ktime_before(cur, stop)); 2021 } while (single_task_running() && ktime_before(cur, stop));
2022 } 2022 }
2023 2023
2024 kvm_arch_vcpu_blocking(vcpu);
2025
2024 for (;;) { 2026 for (;;) {
2025 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE); 2027 prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
2026 2028
@@ -2034,6 +2036,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
2034 finish_wait(&vcpu->wq, &wait); 2036 finish_wait(&vcpu->wq, &wait);
2035 cur = ktime_get(); 2037 cur = ktime_get();
2036 2038
2039 kvm_arch_vcpu_unblocking(vcpu);
2037out: 2040out:
2038 block_ns = ktime_to_ns(cur) - ktime_to_ns(start); 2041 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
2039 2042