2 * Copyright (C) 2001 MandrakeSoft S.A.
3 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
8 * http://www.linux-mandrake.com/
9 * http://www.mandrakesoft.com/
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Yunhong Jiang <yunhong.jiang@intel.com>
26 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
27 * Based on Xen 3.1 code.
30 #include <linux/kvm_host.h>
31 #include <linux/kvm.h>
33 #include <linux/highmem.h>
34 #include <linux/smp.h>
35 #include <linux/hrtimer.h>
37 #include <linux/slab.h>
38 #include <linux/export.h>
39 #include <asm/processor.h>
41 #include <asm/current.h>
42 #include <trace/events/kvm.h>
49 #define ioapic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg)
51 #define ioapic_debug(fmt, arg...)
53 static int ioapic_service(struct kvm_ioapic
*vioapic
, int irq
,
56 static unsigned long ioapic_read_indirect(struct kvm_ioapic
*ioapic
,
60 unsigned long result
= 0;
62 switch (ioapic
->ioregsel
) {
63 case IOAPIC_REG_VERSION
:
64 result
= ((((IOAPIC_NUM_PINS
- 1) & 0xff) << 16)
65 | (IOAPIC_VERSION_ID
& 0xff));
68 case IOAPIC_REG_APIC_ID
:
69 case IOAPIC_REG_ARB_ID
:
70 result
= ((ioapic
->id
& 0xf) << 24);
75 u32 redir_index
= (ioapic
->ioregsel
- 0x10) >> 1;
78 if (redir_index
< IOAPIC_NUM_PINS
)
80 ioapic
->redirtbl
[redir_index
].bits
;
82 redir_content
= ~0ULL;
84 result
= (ioapic
->ioregsel
& 0x1) ?
85 (redir_content
>> 32) & 0xffffffff :
86 redir_content
& 0xffffffff;
94 static void rtc_irq_eoi_tracking_reset(struct kvm_ioapic
*ioapic
)
96 ioapic
->rtc_status
.pending_eoi
= 0;
97 bitmap_zero(ioapic
->rtc_status
.dest_map
, KVM_MAX_VCPUS
);
100 static void __rtc_irq_eoi_tracking_restore_one(struct kvm_vcpu
*vcpu
)
102 bool new_val
, old_val
;
103 struct kvm_ioapic
*ioapic
= vcpu
->kvm
->arch
.vioapic
;
104 union kvm_ioapic_redirect_entry
*e
;
106 e
= &ioapic
->redirtbl
[RTC_GSI
];
107 if (!kvm_apic_match_dest(vcpu
, NULL
, 0, e
->fields
.dest_id
,
108 e
->fields
.dest_mode
))
111 new_val
= kvm_apic_pending_eoi(vcpu
, e
->fields
.vector
);
112 old_val
= test_bit(vcpu
->vcpu_id
, ioapic
->rtc_status
.dest_map
);
114 if (new_val
== old_val
)
118 __set_bit(vcpu
->vcpu_id
, ioapic
->rtc_status
.dest_map
);
119 ioapic
->rtc_status
.pending_eoi
++;
121 __clear_bit(vcpu
->vcpu_id
, ioapic
->rtc_status
.dest_map
);
122 ioapic
->rtc_status
.pending_eoi
--;
125 WARN_ON(ioapic
->rtc_status
.pending_eoi
< 0);
128 void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu
*vcpu
)
130 struct kvm_ioapic
*ioapic
= vcpu
->kvm
->arch
.vioapic
;
132 spin_lock(&ioapic
->lock
);
133 __rtc_irq_eoi_tracking_restore_one(vcpu
);
134 spin_unlock(&ioapic
->lock
);
137 static void kvm_rtc_eoi_tracking_restore_all(struct kvm_ioapic
*ioapic
)
139 struct kvm_vcpu
*vcpu
;
142 if (RTC_GSI
>= IOAPIC_NUM_PINS
)
145 rtc_irq_eoi_tracking_reset(ioapic
);
146 kvm_for_each_vcpu(i
, vcpu
, ioapic
->kvm
)
147 __rtc_irq_eoi_tracking_restore_one(vcpu
);
150 static void rtc_irq_eoi(struct kvm_ioapic
*ioapic
, struct kvm_vcpu
*vcpu
)
152 if (test_and_clear_bit(vcpu
->vcpu_id
, ioapic
->rtc_status
.dest_map
))
153 --ioapic
->rtc_status
.pending_eoi
;
155 WARN_ON(ioapic
->rtc_status
.pending_eoi
< 0);
158 static bool rtc_irq_check_coalesced(struct kvm_ioapic
*ioapic
)
160 if (ioapic
->rtc_status
.pending_eoi
> 0)
161 return true; /* coalesced */
166 static int ioapic_set_irq(struct kvm_ioapic
*ioapic
, unsigned int irq
,
167 int irq_level
, bool line_status
)
169 union kvm_ioapic_redirect_entry entry
;
174 entry
= ioapic
->redirtbl
[irq
];
175 edge
= (entry
.fields
.trig_mode
== IOAPIC_EDGE_TRIG
);
178 ioapic
->irr
&= ~mask
;
184 * Return 0 for coalesced interrupts; for edge-triggered interrupts,
185 * this only happens if a previous edge has not been delivered due
186 * do masking. For level interrupts, the remote_irr field tells
187 * us if the interrupt is waiting for an EOI.
189 * RTC is special: it is edge-triggered, but userspace likes to know
190 * if it has been already ack-ed via EOI because coalesced RTC
191 * interrupts lead to time drift in Windows guests. So we track
192 * EOI manually for the RTC interrupt.
194 if (irq
== RTC_GSI
&& line_status
&&
195 rtc_irq_check_coalesced(ioapic
)) {
200 old_irr
= ioapic
->irr
;
202 if ((edge
&& old_irr
== ioapic
->irr
) ||
203 (!edge
&& entry
.fields
.remote_irr
)) {
208 ret
= ioapic_service(ioapic
, irq
, line_status
);
211 trace_kvm_ioapic_set_irq(entry
.bits
, irq
, ret
== 0);
215 static void kvm_ioapic_inject_all(struct kvm_ioapic
*ioapic
, unsigned long irr
)
219 rtc_irq_eoi_tracking_reset(ioapic
);
220 for_each_set_bit(idx
, &irr
, IOAPIC_NUM_PINS
)
221 ioapic_set_irq(ioapic
, idx
, 1, true);
223 kvm_rtc_eoi_tracking_restore_all(ioapic
);
227 static void update_handled_vectors(struct kvm_ioapic
*ioapic
)
229 DECLARE_BITMAP(handled_vectors
, 256);
232 memset(handled_vectors
, 0, sizeof(handled_vectors
));
233 for (i
= 0; i
< IOAPIC_NUM_PINS
; ++i
)
234 __set_bit(ioapic
->redirtbl
[i
].fields
.vector
, handled_vectors
);
235 memcpy(ioapic
->handled_vectors
, handled_vectors
,
236 sizeof(handled_vectors
));
240 void kvm_ioapic_scan_entry(struct kvm_vcpu
*vcpu
, u64
*eoi_exit_bitmap
,
243 struct kvm_ioapic
*ioapic
= vcpu
->kvm
->arch
.vioapic
;
244 union kvm_ioapic_redirect_entry
*e
;
247 spin_lock(&ioapic
->lock
);
248 for (index
= 0; index
< IOAPIC_NUM_PINS
; index
++) {
249 e
= &ioapic
->redirtbl
[index
];
250 if (!e
->fields
.mask
&&
251 (e
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
||
252 kvm_irq_has_notifier(ioapic
->kvm
, KVM_IRQCHIP_IOAPIC
,
253 index
) || index
== RTC_GSI
)) {
254 if (kvm_apic_match_dest(vcpu
, NULL
, 0,
255 e
->fields
.dest_id
, e
->fields
.dest_mode
)) {
256 __set_bit(e
->fields
.vector
,
257 (unsigned long *)eoi_exit_bitmap
);
258 if (e
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
)
259 __set_bit(e
->fields
.vector
,
260 (unsigned long *)tmr
);
264 spin_unlock(&ioapic
->lock
);
268 void kvm_vcpu_request_scan_ioapic(struct kvm
*kvm
)
270 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
274 kvm_make_scan_ioapic_request(kvm
);
277 void kvm_vcpu_request_scan_ioapic(struct kvm
*kvm
)
283 static void ioapic_write_indirect(struct kvm_ioapic
*ioapic
, u32 val
)
286 bool mask_before
, mask_after
;
287 union kvm_ioapic_redirect_entry
*e
;
289 switch (ioapic
->ioregsel
) {
290 case IOAPIC_REG_VERSION
:
291 /* Writes are ignored. */
294 case IOAPIC_REG_APIC_ID
:
295 ioapic
->id
= (val
>> 24) & 0xf;
298 case IOAPIC_REG_ARB_ID
:
302 index
= (ioapic
->ioregsel
- 0x10) >> 1;
304 ioapic_debug("change redir index %x val %x\n", index
, val
);
305 if (index
>= IOAPIC_NUM_PINS
)
307 e
= &ioapic
->redirtbl
[index
];
308 mask_before
= e
->fields
.mask
;
309 if (ioapic
->ioregsel
& 1) {
310 e
->bits
&= 0xffffffff;
311 e
->bits
|= (u64
) val
<< 32;
313 e
->bits
&= ~0xffffffffULL
;
314 e
->bits
|= (u32
) val
;
315 e
->fields
.remote_irr
= 0;
317 update_handled_vectors(ioapic
);
318 mask_after
= e
->fields
.mask
;
319 if (mask_before
!= mask_after
)
320 kvm_fire_mask_notifiers(ioapic
->kvm
, KVM_IRQCHIP_IOAPIC
, index
, mask_after
);
321 if (e
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
322 && ioapic
->irr
& (1 << index
))
323 ioapic_service(ioapic
, index
, false);
324 kvm_vcpu_request_scan_ioapic(ioapic
->kvm
);
329 static int ioapic_service(struct kvm_ioapic
*ioapic
, int irq
, bool line_status
)
331 union kvm_ioapic_redirect_entry
*entry
= &ioapic
->redirtbl
[irq
];
332 struct kvm_lapic_irq irqe
;
335 if (entry
->fields
.mask
)
338 ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
339 "vector=%x trig_mode=%x\n",
340 entry
->fields
.dest_id
, entry
->fields
.dest_mode
,
341 entry
->fields
.delivery_mode
, entry
->fields
.vector
,
342 entry
->fields
.trig_mode
);
344 irqe
.dest_id
= entry
->fields
.dest_id
;
345 irqe
.vector
= entry
->fields
.vector
;
346 irqe
.dest_mode
= entry
->fields
.dest_mode
;
347 irqe
.trig_mode
= entry
->fields
.trig_mode
;
348 irqe
.delivery_mode
= entry
->fields
.delivery_mode
<< 8;
352 if (irqe
.trig_mode
== IOAPIC_EDGE_TRIG
)
353 ioapic
->irr
&= ~(1 << irq
);
355 if (irq
== RTC_GSI
&& line_status
) {
356 BUG_ON(ioapic
->rtc_status
.pending_eoi
!= 0);
357 ret
= kvm_irq_delivery_to_apic(ioapic
->kvm
, NULL
, &irqe
,
358 ioapic
->rtc_status
.dest_map
);
359 ioapic
->rtc_status
.pending_eoi
= ret
;
361 ret
= kvm_irq_delivery_to_apic(ioapic
->kvm
, NULL
, &irqe
, NULL
);
363 if (ret
&& irqe
.trig_mode
== IOAPIC_LEVEL_TRIG
)
364 entry
->fields
.remote_irr
= 1;
369 int kvm_ioapic_set_irq(struct kvm_ioapic
*ioapic
, int irq
, int irq_source_id
,
370 int level
, bool line_status
)
374 BUG_ON(irq
< 0 || irq
>= IOAPIC_NUM_PINS
);
376 spin_lock(&ioapic
->lock
);
377 irq_level
= __kvm_irq_line_state(&ioapic
->irq_states
[irq
],
378 irq_source_id
, level
);
379 ret
= ioapic_set_irq(ioapic
, irq
, irq_level
, line_status
);
381 spin_unlock(&ioapic
->lock
);
386 void kvm_ioapic_clear_all(struct kvm_ioapic
*ioapic
, int irq_source_id
)
390 spin_lock(&ioapic
->lock
);
391 for (i
= 0; i
< KVM_IOAPIC_NUM_PINS
; i
++)
392 __clear_bit(irq_source_id
, &ioapic
->irq_states
[i
]);
393 spin_unlock(&ioapic
->lock
);
396 static void __kvm_ioapic_update_eoi(struct kvm_vcpu
*vcpu
,
397 struct kvm_ioapic
*ioapic
, int vector
, int trigger_mode
)
401 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
402 union kvm_ioapic_redirect_entry
*ent
= &ioapic
->redirtbl
[i
];
404 if (ent
->fields
.vector
!= vector
)
408 rtc_irq_eoi(ioapic
, vcpu
);
410 * We are dropping lock while calling ack notifiers because ack
411 * notifier callbacks for assigned devices call into IOAPIC
412 * recursively. Since remote_irr is cleared only after call
413 * to notifiers if the same vector will be delivered while lock
414 * is dropped it will be put into irr and will be delivered
415 * after ack notifier returns.
417 spin_unlock(&ioapic
->lock
);
418 kvm_notify_acked_irq(ioapic
->kvm
, KVM_IRQCHIP_IOAPIC
, i
);
419 spin_lock(&ioapic
->lock
);
421 if (trigger_mode
!= IOAPIC_LEVEL_TRIG
)
424 ASSERT(ent
->fields
.trig_mode
== IOAPIC_LEVEL_TRIG
);
425 ent
->fields
.remote_irr
= 0;
426 if (ioapic
->irr
& (1 << i
))
427 ioapic_service(ioapic
, i
, false);
431 bool kvm_ioapic_handles_vector(struct kvm
*kvm
, int vector
)
433 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
435 return test_bit(vector
, ioapic
->handled_vectors
);
438 void kvm_ioapic_update_eoi(struct kvm_vcpu
*vcpu
, int vector
, int trigger_mode
)
440 struct kvm_ioapic
*ioapic
= vcpu
->kvm
->arch
.vioapic
;
442 spin_lock(&ioapic
->lock
);
443 __kvm_ioapic_update_eoi(vcpu
, ioapic
, vector
, trigger_mode
);
444 spin_unlock(&ioapic
->lock
);
447 static inline struct kvm_ioapic
*to_ioapic(struct kvm_io_device
*dev
)
449 return container_of(dev
, struct kvm_ioapic
, dev
);
452 static inline int ioapic_in_range(struct kvm_ioapic
*ioapic
, gpa_t addr
)
454 return ((addr
>= ioapic
->base_address
&&
455 (addr
< ioapic
->base_address
+ IOAPIC_MEM_LENGTH
)));
458 static int ioapic_mmio_read(struct kvm_io_device
*this, gpa_t addr
, int len
,
461 struct kvm_ioapic
*ioapic
= to_ioapic(this);
463 if (!ioapic_in_range(ioapic
, addr
))
466 ioapic_debug("addr %lx\n", (unsigned long)addr
);
467 ASSERT(!(addr
& 0xf)); /* check alignment */
470 spin_lock(&ioapic
->lock
);
472 case IOAPIC_REG_SELECT
:
473 result
= ioapic
->ioregsel
;
476 case IOAPIC_REG_WINDOW
:
477 result
= ioapic_read_indirect(ioapic
, addr
, len
);
484 spin_unlock(&ioapic
->lock
);
488 *(u64
*) val
= result
;
493 memcpy(val
, (char *)&result
, len
);
496 printk(KERN_WARNING
"ioapic: wrong length %d\n", len
);
501 static int ioapic_mmio_write(struct kvm_io_device
*this, gpa_t addr
, int len
,
504 struct kvm_ioapic
*ioapic
= to_ioapic(this);
506 if (!ioapic_in_range(ioapic
, addr
))
509 ioapic_debug("ioapic_mmio_write addr=%p len=%d val=%p\n",
510 (void*)addr
, len
, val
);
511 ASSERT(!(addr
& 0xf)); /* check alignment */
525 printk(KERN_WARNING
"ioapic: Unsupported size %d\n", len
);
530 spin_lock(&ioapic
->lock
);
532 case IOAPIC_REG_SELECT
:
533 ioapic
->ioregsel
= data
& 0xFF; /* 8-bit register */
536 case IOAPIC_REG_WINDOW
:
537 ioapic_write_indirect(ioapic
, data
);
541 __kvm_ioapic_update_eoi(NULL
, ioapic
, data
, IOAPIC_LEVEL_TRIG
);
548 spin_unlock(&ioapic
->lock
);
552 static void kvm_ioapic_reset(struct kvm_ioapic
*ioapic
)
556 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
557 ioapic
->redirtbl
[i
].fields
.mask
= 1;
558 ioapic
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
559 ioapic
->ioregsel
= 0;
562 rtc_irq_eoi_tracking_reset(ioapic
);
563 update_handled_vectors(ioapic
);
566 static const struct kvm_io_device_ops ioapic_mmio_ops
= {
567 .read
= ioapic_mmio_read
,
568 .write
= ioapic_mmio_write
,
571 int kvm_ioapic_init(struct kvm
*kvm
)
573 struct kvm_ioapic
*ioapic
;
576 ioapic
= kzalloc(sizeof(struct kvm_ioapic
), GFP_KERNEL
);
579 spin_lock_init(&ioapic
->lock
);
580 kvm
->arch
.vioapic
= ioapic
;
581 kvm_ioapic_reset(ioapic
);
582 kvm_iodevice_init(&ioapic
->dev
, &ioapic_mmio_ops
);
584 mutex_lock(&kvm
->slots_lock
);
585 ret
= kvm_io_bus_register_dev(kvm
, KVM_MMIO_BUS
, ioapic
->base_address
,
586 IOAPIC_MEM_LENGTH
, &ioapic
->dev
);
587 mutex_unlock(&kvm
->slots_lock
);
589 kvm
->arch
.vioapic
= NULL
;
596 void kvm_ioapic_destroy(struct kvm
*kvm
)
598 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
601 kvm_io_bus_unregister_dev(kvm
, KVM_MMIO_BUS
, &ioapic
->dev
);
602 kvm
->arch
.vioapic
= NULL
;
607 int kvm_get_ioapic(struct kvm
*kvm
, struct kvm_ioapic_state
*state
)
609 struct kvm_ioapic
*ioapic
= ioapic_irqchip(kvm
);
613 spin_lock(&ioapic
->lock
);
614 memcpy(state
, ioapic
, sizeof(struct kvm_ioapic_state
));
615 spin_unlock(&ioapic
->lock
);
619 int kvm_set_ioapic(struct kvm
*kvm
, struct kvm_ioapic_state
*state
)
621 struct kvm_ioapic
*ioapic
= ioapic_irqchip(kvm
);
625 spin_lock(&ioapic
->lock
);
626 memcpy(ioapic
, state
, sizeof(struct kvm_ioapic_state
));
628 update_handled_vectors(ioapic
);
629 kvm_vcpu_request_scan_ioapic(kvm
);
630 kvm_ioapic_inject_all(ioapic
, state
->irr
);
631 spin_unlock(&ioapic
->lock
);