diff options
Diffstat (limited to 'virt/kvm/ioapic.c')
-rw-r--r-- | virt/kvm/ioapic.c | 163 |
1 files changed, 132 insertions, 31 deletions
diff --git a/virt/kvm/ioapic.c b/virt/kvm/ioapic.c index 5ba005c00e2f..2d682977ce82 100644 --- a/virt/kvm/ioapic.c +++ b/virt/kvm/ioapic.c | |||
@@ -50,7 +50,8 @@ | |||
50 | #else | 50 | #else |
51 | #define ioapic_debug(fmt, arg...) | 51 | #define ioapic_debug(fmt, arg...) |
52 | #endif | 52 | #endif |
53 | static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq); | 53 | static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq, |
54 | bool line_status); | ||
54 | 55 | ||
55 | static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, | 56 | static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, |
56 | unsigned long addr, | 57 | unsigned long addr, |
@@ -90,7 +91,80 @@ static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, | |||
90 | return result; | 91 | return result; |
91 | } | 92 | } |
92 | 93 | ||
93 | static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx) | 94 | static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic *ioapic) |
95 | { | ||
96 | ioapic->rtc_status.pending_eoi = 0; | ||
97 | bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS); | ||
98 | } | ||
99 | |||
100 | static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu *vcpu) | ||
101 | { | ||
102 | bool new_val, old_val; | ||
103 | struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; | ||
104 | union kvm_ioapic_redirect_entry *e; | ||
105 | |||
106 | e = &ioapic->redirtbl[RTC_GSI]; | ||
107 | if (!kvm_apic_match_dest(vcpu, NULL, 0, e->fields.dest_id, | ||
108 | e->fields.dest_mode)) | ||
109 | return; | ||
110 | |||
111 | new_val = kvm_apic_pending_eoi(vcpu, e->fields.vector); | ||
112 | old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map); | ||
113 | |||
114 | if (new_val == old_val) | ||
115 | return; | ||
116 | |||
117 | if (new_val) { | ||
118 | __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map); | ||
119 | ioapic->rtc_status.pending_eoi++; | ||
120 | } else { | ||
121 | __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map); | ||
122 | ioapic->rtc_status.pending_eoi--; | ||
123 | } | ||
124 | |||
125 | WARN_ON(ioapic->rtc_status.pending_eoi < 0); | ||
126 | } | ||
127 | |||
128 | void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu) | ||
129 | { | ||
130 | struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; | ||
131 | |||
132 | spin_lock(&ioapic->lock); | ||
133 | __rtc_irq_eoi_tracking_restore_one(vcpu); | ||
134 | spin_unlock(&ioapic->lock); | ||
135 | } | ||
136 | |||
137 | static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic *ioapic) | ||
138 | { | ||
139 | struct kvm_vcpu *vcpu; | ||
140 | int i; | ||
141 | |||
142 | if (RTC_GSI >= IOAPIC_NUM_PINS) | ||
143 | return; | ||
144 | |||
145 | rtc_irq_eoi_tracking_reset(ioapic); | ||
146 | kvm_for_each_vcpu(i, vcpu, ioapic->kvm) | ||
147 | __rtc_irq_eoi_tracking_restore_one(vcpu); | ||
148 | } | ||
149 | |||
150 | static void rtc_irq_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu) | ||
151 | { | ||
152 | if (test_and_clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map)) | ||
153 | --ioapic->rtc_status.pending_eoi; | ||
154 | |||
155 | WARN_ON(ioapic->rtc_status.pending_eoi < 0); | ||
156 | } | ||
157 | |||
158 | static bool rtc_irq_check_coalesced(struct kvm_ioapic *ioapic) | ||
159 | { | ||
160 | if (ioapic->rtc_status.pending_eoi > 0) | ||
161 | return true; /* coalesced */ | ||
162 | |||
163 | return false; | ||
164 | } | ||
165 | |||
166 | static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx, | ||
167 | bool line_status) | ||
94 | { | 168 | { |
95 | union kvm_ioapic_redirect_entry *pent; | 169 | union kvm_ioapic_redirect_entry *pent; |
96 | int injected = -1; | 170 | int injected = -1; |
@@ -98,7 +172,7 @@ static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx) | |||
98 | pent = &ioapic->redirtbl[idx]; | 172 | pent = &ioapic->redirtbl[idx]; |
99 | 173 | ||
100 | if (!pent->fields.mask) { | 174 | if (!pent->fields.mask) { |
101 | injected = ioapic_deliver(ioapic, idx); | 175 | injected = ioapic_deliver(ioapic, idx, line_status); |
102 | if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG) | 176 | if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG) |
103 | pent->fields.remote_irr = 1; | 177 | pent->fields.remote_irr = 1; |
104 | } | 178 | } |
@@ -119,41 +193,48 @@ static void update_handled_vectors(struct kvm_ioapic *ioapic) | |||
119 | smp_wmb(); | 193 | smp_wmb(); |
120 | } | 194 | } |
121 | 195 | ||
122 | void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu, | 196 | void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap, |
123 | u64 *eoi_exit_bitmap) | 197 | u32 *tmr) |
124 | { | 198 | { |
125 | struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; | 199 | struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; |
126 | union kvm_ioapic_redirect_entry *e; | 200 | union kvm_ioapic_redirect_entry *e; |
127 | struct kvm_lapic_irq irqe; | ||
128 | int index; | 201 | int index; |
129 | 202 | ||
130 | spin_lock(&ioapic->lock); | 203 | spin_lock(&ioapic->lock); |
131 | /* traverse ioapic entry to set eoi exit bitmap*/ | ||
132 | for (index = 0; index < IOAPIC_NUM_PINS; index++) { | 204 | for (index = 0; index < IOAPIC_NUM_PINS; index++) { |
133 | e = &ioapic->redirtbl[index]; | 205 | e = &ioapic->redirtbl[index]; |
134 | if (!e->fields.mask && | 206 | if (!e->fields.mask && |
135 | (e->fields.trig_mode == IOAPIC_LEVEL_TRIG || | 207 | (e->fields.trig_mode == IOAPIC_LEVEL_TRIG || |
136 | kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC, | 208 | kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC, |
137 | index))) { | 209 | index) || index == RTC_GSI)) { |
138 | irqe.dest_id = e->fields.dest_id; | 210 | if (kvm_apic_match_dest(vcpu, NULL, 0, |
139 | irqe.vector = e->fields.vector; | 211 | e->fields.dest_id, e->fields.dest_mode)) { |
140 | irqe.dest_mode = e->fields.dest_mode; | 212 | __set_bit(e->fields.vector, |
141 | irqe.delivery_mode = e->fields.delivery_mode << 8; | 213 | (unsigned long *)eoi_exit_bitmap); |
142 | kvm_calculate_eoi_exitmap(vcpu, &irqe, eoi_exit_bitmap); | 214 | if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG) |
215 | __set_bit(e->fields.vector, | ||
216 | (unsigned long *)tmr); | ||
217 | } | ||
143 | } | 218 | } |
144 | } | 219 | } |
145 | spin_unlock(&ioapic->lock); | 220 | spin_unlock(&ioapic->lock); |
146 | } | 221 | } |
147 | EXPORT_SYMBOL_GPL(kvm_ioapic_calculate_eoi_exitmap); | ||
148 | 222 | ||
149 | void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm) | 223 | #ifdef CONFIG_X86 |
224 | void kvm_vcpu_request_scan_ioapic(struct kvm *kvm) | ||
150 | { | 225 | { |
151 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; | 226 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
152 | 227 | ||
153 | if (!kvm_apic_vid_enabled(kvm) || !ioapic) | 228 | if (!ioapic) |
154 | return; | 229 | return; |
155 | kvm_make_update_eoibitmap_request(kvm); | 230 | kvm_make_scan_ioapic_request(kvm); |
156 | } | 231 | } |
232 | #else | ||
233 | void kvm_vcpu_request_scan_ioapic(struct kvm *kvm) | ||
234 | { | ||
235 | return; | ||
236 | } | ||
237 | #endif | ||
157 | 238 | ||
158 | static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) | 239 | static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) |
159 | { | 240 | { |
@@ -195,16 +276,17 @@ static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) | |||
195 | kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after); | 276 | kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after); |
196 | if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG | 277 | if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG |
197 | && ioapic->irr & (1 << index)) | 278 | && ioapic->irr & (1 << index)) |
198 | ioapic_service(ioapic, index); | 279 | ioapic_service(ioapic, index, false); |
199 | kvm_ioapic_make_eoibitmap_request(ioapic->kvm); | 280 | kvm_vcpu_request_scan_ioapic(ioapic->kvm); |
200 | break; | 281 | break; |
201 | } | 282 | } |
202 | } | 283 | } |
203 | 284 | ||
204 | static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq) | 285 | static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq, bool line_status) |
205 | { | 286 | { |
206 | union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq]; | 287 | union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq]; |
207 | struct kvm_lapic_irq irqe; | 288 | struct kvm_lapic_irq irqe; |
289 | int ret; | ||
208 | 290 | ||
209 | ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x " | 291 | ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x " |
210 | "vector=%x trig_mode=%x\n", | 292 | "vector=%x trig_mode=%x\n", |
@@ -220,11 +302,19 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq) | |||
220 | irqe.level = 1; | 302 | irqe.level = 1; |
221 | irqe.shorthand = 0; | 303 | irqe.shorthand = 0; |
222 | 304 | ||
223 | return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe); | 305 | if (irq == RTC_GSI && line_status) { |
306 | BUG_ON(ioapic->rtc_status.pending_eoi != 0); | ||
307 | ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, | ||
308 | ioapic->rtc_status.dest_map); | ||
309 | ioapic->rtc_status.pending_eoi = ret; | ||
310 | } else | ||
311 | ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL); | ||
312 | |||
313 | return ret; | ||
224 | } | 314 | } |
225 | 315 | ||
226 | int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id, | 316 | int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id, |
227 | int level) | 317 | int level, bool line_status) |
228 | { | 318 | { |
229 | u32 old_irr; | 319 | u32 old_irr; |
230 | u32 mask = 1 << irq; | 320 | u32 mask = 1 << irq; |
@@ -244,13 +334,20 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id, | |||
244 | ret = 1; | 334 | ret = 1; |
245 | } else { | 335 | } else { |
246 | int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG); | 336 | int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG); |
337 | |||
338 | if (irq == RTC_GSI && line_status && | ||
339 | rtc_irq_check_coalesced(ioapic)) { | ||
340 | ret = 0; /* coalesced */ | ||
341 | goto out; | ||
342 | } | ||
247 | ioapic->irr |= mask; | 343 | ioapic->irr |= mask; |
248 | if ((edge && old_irr != ioapic->irr) || | 344 | if ((edge && old_irr != ioapic->irr) || |
249 | (!edge && !entry.fields.remote_irr)) | 345 | (!edge && !entry.fields.remote_irr)) |
250 | ret = ioapic_service(ioapic, irq); | 346 | ret = ioapic_service(ioapic, irq, line_status); |
251 | else | 347 | else |
252 | ret = 0; /* report coalesced interrupt */ | 348 | ret = 0; /* report coalesced interrupt */ |
253 | } | 349 | } |
350 | out: | ||
254 | trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); | 351 | trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0); |
255 | spin_unlock(&ioapic->lock); | 352 | spin_unlock(&ioapic->lock); |
256 | 353 | ||
@@ -267,8 +364,8 @@ void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id) | |||
267 | spin_unlock(&ioapic->lock); | 364 | spin_unlock(&ioapic->lock); |
268 | } | 365 | } |
269 | 366 | ||
270 | static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector, | 367 | static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, |
271 | int trigger_mode) | 368 | struct kvm_ioapic *ioapic, int vector, int trigger_mode) |
272 | { | 369 | { |
273 | int i; | 370 | int i; |
274 | 371 | ||
@@ -278,6 +375,8 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector, | |||
278 | if (ent->fields.vector != vector) | 375 | if (ent->fields.vector != vector) |
279 | continue; | 376 | continue; |
280 | 377 | ||
378 | if (i == RTC_GSI) | ||
379 | rtc_irq_eoi(ioapic, vcpu); | ||
281 | /* | 380 | /* |
282 | * We are dropping lock while calling ack notifiers because ack | 381 | * We are dropping lock while calling ack notifiers because ack |
283 | * notifier callbacks for assigned devices call into IOAPIC | 382 | * notifier callbacks for assigned devices call into IOAPIC |
@@ -296,7 +395,7 @@ static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector, | |||
296 | ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG); | 395 | ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG); |
297 | ent->fields.remote_irr = 0; | 396 | ent->fields.remote_irr = 0; |
298 | if (!ent->fields.mask && (ioapic->irr & (1 << i))) | 397 | if (!ent->fields.mask && (ioapic->irr & (1 << i))) |
299 | ioapic_service(ioapic, i); | 398 | ioapic_service(ioapic, i, false); |
300 | } | 399 | } |
301 | } | 400 | } |
302 | 401 | ||
@@ -307,12 +406,12 @@ bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector) | |||
307 | return test_bit(vector, ioapic->handled_vectors); | 406 | return test_bit(vector, ioapic->handled_vectors); |
308 | } | 407 | } |
309 | 408 | ||
310 | void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode) | 409 | void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode) |
311 | { | 410 | { |
312 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; | 411 | struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic; |
313 | 412 | ||
314 | spin_lock(&ioapic->lock); | 413 | spin_lock(&ioapic->lock); |
315 | __kvm_ioapic_update_eoi(ioapic, vector, trigger_mode); | 414 | __kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode); |
316 | spin_unlock(&ioapic->lock); | 415 | spin_unlock(&ioapic->lock); |
317 | } | 416 | } |
318 | 417 | ||
@@ -410,7 +509,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len, | |||
410 | break; | 509 | break; |
411 | #ifdef CONFIG_IA64 | 510 | #ifdef CONFIG_IA64 |
412 | case IOAPIC_REG_EOI: | 511 | case IOAPIC_REG_EOI: |
413 | __kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG); | 512 | __kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG); |
414 | break; | 513 | break; |
415 | #endif | 514 | #endif |
416 | 515 | ||
@@ -431,6 +530,7 @@ void kvm_ioapic_reset(struct kvm_ioapic *ioapic) | |||
431 | ioapic->ioregsel = 0; | 530 | ioapic->ioregsel = 0; |
432 | ioapic->irr = 0; | 531 | ioapic->irr = 0; |
433 | ioapic->id = 0; | 532 | ioapic->id = 0; |
533 | rtc_irq_eoi_tracking_reset(ioapic); | ||
434 | update_handled_vectors(ioapic); | 534 | update_handled_vectors(ioapic); |
435 | } | 535 | } |
436 | 536 | ||
@@ -496,7 +596,8 @@ int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state) | |||
496 | spin_lock(&ioapic->lock); | 596 | spin_lock(&ioapic->lock); |
497 | memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); | 597 | memcpy(ioapic, state, sizeof(struct kvm_ioapic_state)); |
498 | update_handled_vectors(ioapic); | 598 | update_handled_vectors(ioapic); |
499 | kvm_ioapic_make_eoibitmap_request(kvm); | 599 | kvm_vcpu_request_scan_ioapic(kvm); |
600 | kvm_rtc_eoi_tracking_restore_all(ioapic); | ||
500 | spin_unlock(&ioapic->lock); | 601 | spin_unlock(&ioapic->lock); |
501 | return 0; | 602 | return 0; |
502 | } | 603 | } |