3 * Local APIC virtualization
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
10 * Dor Laor <dor.laor@qumranet.com>
11 * Gregory Haskins <ghaskins@novell.com>
12 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
14 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
20 #include <linux/kvm_host.h>
21 #include <linux/kvm.h>
23 #include <linux/highmem.h>
24 #include <linux/smp.h>
25 #include <linux/hrtimer.h>
27 #include <linux/module.h>
28 #include <asm/processor.h>
31 #include <asm/current.h>
32 #include <asm/apicdef.h>
33 #include <asm/atomic.h>
34 #include <asm/div64.h>
42 #define APIC_BUS_CYCLE_NS 1
44 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
45 #define apic_debug(fmt, arg...)
47 #define APIC_LVT_NUM 6
48 /* 14 is the version for Xeon and Pentium 8.4.8*/
49 #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
50 #define LAPIC_MMIO_LENGTH (1 << 12)
51 /* followed define is not in apicdef.h */
52 #define APIC_SHORT_MASK 0xc0000
53 #define APIC_DEST_NOSHORT 0x0
54 #define APIC_DEST_MASK 0x800
55 #define MAX_APIC_VECTOR 256
57 #define VEC_POS(v) ((v) & (32 - 1))
58 #define REG_POS(v) (((v) >> 5) << 4)
60 static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
62 return *((u32 *) (apic->regs + reg_off));
65 static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
67 *((u32 *) (apic->regs + reg_off)) = val;
70 static inline int apic_test_and_set_vector(int vec, void *bitmap)
72 return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
75 static inline int apic_test_and_clear_vector(int vec, void *bitmap)
77 return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
80 static inline void apic_set_vector(int vec, void *bitmap)
82 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
85 static inline void apic_clear_vector(int vec, void *bitmap)
87 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
90 static inline int apic_hw_enabled(struct kvm_lapic *apic)
92 return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
95 static inline int apic_sw_enabled(struct kvm_lapic *apic)
97 return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
100 static inline int apic_enabled(struct kvm_lapic *apic)
102 return apic_sw_enabled(apic) && apic_hw_enabled(apic);
106 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
109 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
110 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
112 static inline int kvm_apic_id(struct kvm_lapic *apic)
114 return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
117 static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
119 return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
122 static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
124 return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
127 static inline int apic_lvtt_period(struct kvm_lapic *apic)
129 return apic_get_reg(apic, APIC_LVTT) & APIC_LVT_TIMER_PERIODIC;
132 static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
133 LVT_MASK | APIC_LVT_TIMER_PERIODIC, /* LVTT */
134 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
135 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
136 LINT_MASK, LINT_MASK, /* LVT0-1 */
137 LVT_MASK /* LVTERR */
140 static int find_highest_vector(void *bitmap)
143 int word_offset = MAX_APIC_VECTOR >> 5;
145 while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
148 if (likely(!word_offset && !word[0]))
151 return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
154 static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
156 return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
159 static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
161 apic_clear_vector(vec, apic->regs + APIC_IRR);
164 static inline int apic_find_highest_irr(struct kvm_lapic *apic)
168 result = find_highest_vector(apic->regs + APIC_IRR);
169 ASSERT(result == -1 || result >= 16);
174 int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
176 struct kvm_lapic *apic = vcpu->arch.apic;
181 highest_irr = apic_find_highest_irr(apic);
185 EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
187 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
189 struct kvm_lapic *apic = vcpu->arch.apic;
191 if (!apic_test_and_set_irr(vec, apic)) {
192 /* a new pending irq is set in IRR */
194 apic_set_vector(vec, apic->regs + APIC_TMR);
196 apic_clear_vector(vec, apic->regs + APIC_TMR);
197 kvm_vcpu_kick(apic->vcpu);
203 static inline int apic_find_highest_isr(struct kvm_lapic *apic)
207 result = find_highest_vector(apic->regs + APIC_ISR);
208 ASSERT(result == -1 || result >= 16);
213 static void apic_update_ppr(struct kvm_lapic *apic)
218 tpr = apic_get_reg(apic, APIC_TASKPRI);
219 isr = apic_find_highest_isr(apic);
220 isrv = (isr != -1) ? isr : 0;
222 if ((tpr & 0xf0) >= (isrv & 0xf0))
227 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
228 apic, ppr, isr, isrv);
230 apic_set_reg(apic, APIC_PROCPRI, ppr);
233 static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
235 apic_set_reg(apic, APIC_TASKPRI, tpr);
236 apic_update_ppr(apic);
239 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
241 return kvm_apic_id(apic) == dest;
244 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
249 logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
251 switch (apic_get_reg(apic, APIC_DFR)) {
253 if (logical_id & mda)
256 case APIC_DFR_CLUSTER:
257 if (((logical_id >> 4) == (mda >> 0x4))
258 && (logical_id & mda & 0xf))
262 printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
263 apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
270 static int apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
271 int short_hand, int dest, int dest_mode)
274 struct kvm_lapic *target = vcpu->arch.apic;
276 apic_debug("target %p, source %p, dest 0x%x, "
277 "dest_mode 0x%x, short_hand 0x%x",
278 target, source, dest, dest_mode, short_hand);
281 switch (short_hand) {
282 case APIC_DEST_NOSHORT:
283 if (dest_mode == 0) {
285 if ((dest == 0xFF) || (dest == kvm_apic_id(target)))
289 result = kvm_apic_match_logical_addr(target, dest);
292 if (target == source)
295 case APIC_DEST_ALLINC:
298 case APIC_DEST_ALLBUT:
299 if (target != source)
303 printk(KERN_WARNING "Bad dest shorthand value %x\n",
312 * Add a pending IRQ into lapic.
313 * Return 1 if successfully added and 0 if discarded.
315 static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
316 int vector, int level, int trig_mode)
318 int orig_irr, result = 0;
319 struct kvm_vcpu *vcpu = apic->vcpu;
321 switch (delivery_mode) {
324 /* FIXME add logic for vcpu on reset */
325 if (unlikely(!apic_enabled(apic)))
328 orig_irr = apic_test_and_set_irr(vector, apic);
329 if (orig_irr && trig_mode) {
330 apic_debug("level trig mode repeatedly for vector %d",
336 apic_debug("level trig mode for vector %d", vector);
337 apic_set_vector(vector, apic->regs + APIC_TMR);
339 apic_clear_vector(vector, apic->regs + APIC_TMR);
341 if (vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE)
343 else if (vcpu->arch.mp_state == VCPU_MP_STATE_HALTED) {
344 vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
345 if (waitqueue_active(&vcpu->wq))
346 wake_up_interruptible(&vcpu->wq);
349 result = (orig_irr == 0);
353 printk(KERN_DEBUG "Ignoring delivery mode 3\n");
357 printk(KERN_DEBUG "Ignoring guest SMI\n");
360 printk(KERN_DEBUG "Ignoring guest NMI\n");
365 if (vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE)
367 "INIT on a runnable vcpu %d\n",
369 vcpu->arch.mp_state = VCPU_MP_STATE_INIT_RECEIVED;
373 "Ignoring de-assert INIT to vcpu %d\n",
379 case APIC_DM_STARTUP:
380 printk(KERN_DEBUG "SIPI to vcpu %d vector 0x%02x\n",
381 vcpu->vcpu_id, vector);
382 if (vcpu->arch.mp_state == VCPU_MP_STATE_INIT_RECEIVED) {
383 vcpu->arch.sipi_vector = vector;
384 vcpu->arch.mp_state = VCPU_MP_STATE_SIPI_RECEIVED;
385 if (waitqueue_active(&vcpu->wq))
386 wake_up_interruptible(&vcpu->wq);
391 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
398 static struct kvm_lapic *kvm_apic_round_robin(struct kvm *kvm, u8 vector,
399 unsigned long bitmap)
403 struct kvm_lapic *apic = NULL;
405 last = kvm->arch.round_robin_prev_vcpu;
409 if (++next == KVM_MAX_VCPUS)
411 if (kvm->vcpus[next] == NULL || !test_bit(next, &bitmap))
413 apic = kvm->vcpus[next]->arch.apic;
414 if (apic && apic_enabled(apic))
417 } while (next != last);
418 kvm->arch.round_robin_prev_vcpu = next;
421 printk(KERN_DEBUG "vcpu not ready for apic_round_robin\n");
426 struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
427 unsigned long bitmap)
429 struct kvm_lapic *apic;
431 apic = kvm_apic_round_robin(kvm, vector, bitmap);
437 static void apic_set_eoi(struct kvm_lapic *apic)
439 int vector = apic_find_highest_isr(apic);
442 * Not every write EOI will has corresponding ISR,
443 * one example is when Kernel check timer on setup_IO_APIC
448 apic_clear_vector(vector, apic->regs + APIC_ISR);
449 apic_update_ppr(apic);
451 if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
452 kvm_ioapic_update_eoi(apic->vcpu->kvm, vector);
455 static void apic_send_ipi(struct kvm_lapic *apic)
457 u32 icr_low = apic_get_reg(apic, APIC_ICR);
458 u32 icr_high = apic_get_reg(apic, APIC_ICR2);
460 unsigned int dest = GET_APIC_DEST_FIELD(icr_high);
461 unsigned int short_hand = icr_low & APIC_SHORT_MASK;
462 unsigned int trig_mode = icr_low & APIC_INT_LEVELTRIG;
463 unsigned int level = icr_low & APIC_INT_ASSERT;
464 unsigned int dest_mode = icr_low & APIC_DEST_MASK;
465 unsigned int delivery_mode = icr_low & APIC_MODE_MASK;
466 unsigned int vector = icr_low & APIC_VECTOR_MASK;
468 struct kvm_vcpu *target;
469 struct kvm_vcpu *vcpu;
470 unsigned long lpr_map = 0;
473 apic_debug("icr_high 0x%x, icr_low 0x%x, "
474 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
475 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
476 icr_high, icr_low, short_hand, dest,
477 trig_mode, level, dest_mode, delivery_mode, vector);
479 for (i = 0; i < KVM_MAX_VCPUS; i++) {
480 vcpu = apic->vcpu->kvm->vcpus[i];
484 if (vcpu->arch.apic &&
485 apic_match_dest(vcpu, apic, short_hand, dest, dest_mode)) {
486 if (delivery_mode == APIC_DM_LOWEST)
487 set_bit(vcpu->vcpu_id, &lpr_map);
489 __apic_accept_irq(vcpu->arch.apic, delivery_mode,
490 vector, level, trig_mode);
494 if (delivery_mode == APIC_DM_LOWEST) {
495 target = kvm_get_lowest_prio_vcpu(vcpu->kvm, vector, lpr_map);
497 __apic_accept_irq(target->arch.apic, delivery_mode,
498 vector, level, trig_mode);
502 static u32 apic_get_tmcct(struct kvm_lapic *apic)
508 ASSERT(apic != NULL);
510 now = apic->timer.dev.base->get_time();
511 tmcct = apic_get_reg(apic, APIC_TMICT);
513 /* if initial count is 0, current count should also be 0 */
517 if (unlikely(ktime_to_ns(now) <=
518 ktime_to_ns(apic->timer.last_update))) {
520 passed = ktime_add(( {
523 (apic->timer.last_update).tv64}; }
525 apic_debug("time elapsed\n");
527 passed = ktime_sub(now, apic->timer.last_update);
529 counter_passed = div64_64(ktime_to_ns(passed),
530 (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
532 if (counter_passed > tmcct) {
533 if (unlikely(!apic_lvtt_period(apic))) {
534 /* one-shot timers stick at 0 until reset */
538 * periodic timers reset to APIC_TMICT when they
539 * hit 0. The while loop simulates this happening N
540 * times. (counter_passed %= tmcct) would also work,
541 * but might be slower or not work on 32-bit??
543 while (counter_passed > tmcct)
544 counter_passed -= tmcct;
545 tmcct -= counter_passed;
548 tmcct -= counter_passed;
554 static void __report_tpr_access(struct kvm_lapic *apic, bool write)
556 struct kvm_vcpu *vcpu = apic->vcpu;
557 struct kvm_run *run = vcpu->run;
559 set_bit(KVM_REQ_REPORT_TPR_ACCESS, &vcpu->requests);
560 kvm_x86_ops->cache_regs(vcpu);
561 run->tpr_access.rip = vcpu->arch.rip;
562 run->tpr_access.is_write = write;
565 static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
567 if (apic->vcpu->arch.tpr_access_reporting)
568 __report_tpr_access(apic, write);
571 static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
575 if (offset >= LAPIC_MMIO_LENGTH)
580 printk(KERN_WARNING "Access APIC ARBPRI register "
581 "which is for P6\n");
584 case APIC_TMCCT: /* Timer CCR */
585 val = apic_get_tmcct(apic);
589 report_tpr_access(apic, false);
592 apic_update_ppr(apic);
593 val = apic_get_reg(apic, offset);
600 static void apic_mmio_read(struct kvm_io_device *this,
601 gpa_t address, int len, void *data)
603 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
604 unsigned int offset = address - apic->base_address;
605 unsigned char alignment = offset & 0xf;
608 if ((alignment + len) > 4) {
609 printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
610 (unsigned long)address, len);
613 result = __apic_read(apic, offset & ~0xf);
619 memcpy(data, (char *)&result + alignment, len);
622 printk(KERN_ERR "Local APIC read with len = %x, "
623 "should be 1,2, or 4 instead\n", len);
628 static void update_divide_count(struct kvm_lapic *apic)
630 u32 tmp1, tmp2, tdcr;
632 tdcr = apic_get_reg(apic, APIC_TDCR);
634 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
635 apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
637 apic_debug("timer divide count is 0x%x\n",
638 apic->timer.divide_count);
641 static void start_apic_timer(struct kvm_lapic *apic)
643 ktime_t now = apic->timer.dev.base->get_time();
645 apic->timer.last_update = now;
647 apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
648 APIC_BUS_CYCLE_NS * apic->timer.divide_count;
649 atomic_set(&apic->timer.pending, 0);
650 hrtimer_start(&apic->timer.dev,
651 ktime_add_ns(now, apic->timer.period),
654 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
656 "timer initial count 0x%x, period %lldns, "
657 "expire @ 0x%016" PRIx64 ".\n", __FUNCTION__,
658 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
659 apic_get_reg(apic, APIC_TMICT),
661 ktime_to_ns(ktime_add_ns(now,
662 apic->timer.period)));
665 static void apic_mmio_write(struct kvm_io_device *this,
666 gpa_t address, int len, const void *data)
668 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
669 unsigned int offset = address - apic->base_address;
670 unsigned char alignment = offset & 0xf;
674 * APIC register must be aligned on 128-bits boundary.
675 * 32/64/128 bits registers must be accessed thru 32 bits.
678 if (len != 4 || alignment) {
679 if (printk_ratelimit())
680 printk(KERN_ERR "apic write: bad size=%d %lx\n",
687 /* too common printing */
688 if (offset != APIC_EOI)
689 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
690 "0x%x\n", __FUNCTION__, offset, len, val);
695 case APIC_ID: /* Local APIC ID */
696 apic_set_reg(apic, APIC_ID, val);
700 report_tpr_access(apic, true);
701 apic_set_tpr(apic, val & 0xff);
709 apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
713 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
717 apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
718 if (!(val & APIC_SPIV_APIC_ENABLED)) {
722 for (i = 0; i < APIC_LVT_NUM; i++) {
723 lvt_val = apic_get_reg(apic,
724 APIC_LVTT + 0x10 * i);
725 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
726 lvt_val | APIC_LVT_MASKED);
728 atomic_set(&apic->timer.pending, 0);
734 /* No delay here, so we always clear the pending bit */
735 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
740 apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
749 /* TODO: Check vector */
750 if (!apic_sw_enabled(apic))
751 val |= APIC_LVT_MASKED;
753 val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
754 apic_set_reg(apic, offset, val);
759 hrtimer_cancel(&apic->timer.dev);
760 apic_set_reg(apic, APIC_TMICT, val);
761 start_apic_timer(apic);
766 printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
767 apic_set_reg(apic, APIC_TDCR, val);
768 update_divide_count(apic);
772 apic_debug("Local APIC Write to read-only register %x\n",
779 static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
781 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
785 if (apic_hw_enabled(apic) &&
786 (addr >= apic->base_address) &&
787 (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
793 void kvm_free_lapic(struct kvm_vcpu *vcpu)
795 if (!vcpu->arch.apic)
798 hrtimer_cancel(&vcpu->arch.apic->timer.dev);
800 if (vcpu->arch.apic->regs_page)
801 __free_page(vcpu->arch.apic->regs_page);
803 kfree(vcpu->arch.apic);
807 *----------------------------------------------------------------------
809 *----------------------------------------------------------------------
812 void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
814 struct kvm_lapic *apic = vcpu->arch.apic;
818 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
819 | (apic_get_reg(apic, APIC_TASKPRI) & 4));
822 u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
824 struct kvm_lapic *apic = vcpu->arch.apic;
829 tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
831 return (tpr & 0xf0) >> 4;
833 EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8);
835 void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
837 struct kvm_lapic *apic = vcpu->arch.apic;
840 value |= MSR_IA32_APICBASE_BSP;
841 vcpu->arch.apic_base = value;
844 if (apic->vcpu->vcpu_id)
845 value &= ~MSR_IA32_APICBASE_BSP;
847 vcpu->arch.apic_base = value;
848 apic->base_address = apic->vcpu->arch.apic_base &
849 MSR_IA32_APICBASE_BASE;
851 /* with FSB delivery interrupt, we can restart APIC functionality */
852 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
853 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
857 u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
859 return vcpu->arch.apic_base;
861 EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
863 void kvm_lapic_reset(struct kvm_vcpu *vcpu)
865 struct kvm_lapic *apic;
868 apic_debug("%s\n", __FUNCTION__);
871 apic = vcpu->arch.apic;
872 ASSERT(apic != NULL);
874 /* Stop the timer in case it's a reset to an active apic */
875 hrtimer_cancel(&apic->timer.dev);
877 apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
878 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
880 for (i = 0; i < APIC_LVT_NUM; i++)
881 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
882 apic_set_reg(apic, APIC_LVT0,
883 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
885 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
886 apic_set_reg(apic, APIC_SPIV, 0xff);
887 apic_set_reg(apic, APIC_TASKPRI, 0);
888 apic_set_reg(apic, APIC_LDR, 0);
889 apic_set_reg(apic, APIC_ESR, 0);
890 apic_set_reg(apic, APIC_ICR, 0);
891 apic_set_reg(apic, APIC_ICR2, 0);
892 apic_set_reg(apic, APIC_TDCR, 0);
893 apic_set_reg(apic, APIC_TMICT, 0);
894 for (i = 0; i < 8; i++) {
895 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
896 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
897 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
899 update_divide_count(apic);
900 atomic_set(&apic->timer.pending, 0);
901 if (vcpu->vcpu_id == 0)
902 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
903 apic_update_ppr(apic);
905 apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
906 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __FUNCTION__,
907 vcpu, kvm_apic_id(apic),
908 vcpu->arch.apic_base, apic->base_address);
910 EXPORT_SYMBOL_GPL(kvm_lapic_reset);
912 int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
914 struct kvm_lapic *apic = vcpu->arch.apic;
919 ret = apic_enabled(apic);
923 EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
926 *----------------------------------------------------------------------
928 *----------------------------------------------------------------------
931 /* TODO: make sure __apic_timer_fn runs in current pCPU */
932 static int __apic_timer_fn(struct kvm_lapic *apic)
935 wait_queue_head_t *q = &apic->vcpu->wq;
937 atomic_inc(&apic->timer.pending);
938 if (waitqueue_active(q)) {
939 apic->vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
940 wake_up_interruptible(q);
942 if (apic_lvtt_period(apic)) {
944 apic->timer.dev.expires = ktime_add_ns(
945 apic->timer.dev.expires,
951 static int __inject_apic_timer_irq(struct kvm_lapic *apic)
955 vector = apic_lvt_vector(apic, APIC_LVTT);
956 return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
959 static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
961 struct kvm_lapic *apic;
962 int restart_timer = 0;
964 apic = container_of(data, struct kvm_lapic, timer.dev);
966 restart_timer = __apic_timer_fn(apic);
969 return HRTIMER_RESTART;
971 return HRTIMER_NORESTART;
974 int kvm_create_lapic(struct kvm_vcpu *vcpu)
976 struct kvm_lapic *apic;
978 ASSERT(vcpu != NULL);
979 apic_debug("apic_init %d\n", vcpu->vcpu_id);
981 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
985 vcpu->arch.apic = apic;
987 apic->regs_page = alloc_page(GFP_KERNEL);
988 if (apic->regs_page == NULL) {
989 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
991 goto nomem_free_apic;
993 apic->regs = page_address(apic->regs_page);
994 memset(apic->regs, 0, PAGE_SIZE);
997 hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
998 apic->timer.dev.function = apic_timer_fn;
999 apic->base_address = APIC_DEFAULT_PHYS_BASE;
1000 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
1002 kvm_lapic_reset(vcpu);
1003 apic->dev.read = apic_mmio_read;
1004 apic->dev.write = apic_mmio_write;
1005 apic->dev.in_range = apic_mmio_range;
1006 apic->dev.private = apic;
1014 EXPORT_SYMBOL_GPL(kvm_create_lapic);
1016 int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1018 struct kvm_lapic *apic = vcpu->arch.apic;
1021 if (!apic || !apic_enabled(apic))
1024 apic_update_ppr(apic);
1025 highest_irr = apic_find_highest_irr(apic);
1026 if ((highest_irr == -1) ||
1027 ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
1032 int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1034 u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
1037 if (vcpu->vcpu_id == 0) {
1038 if (!apic_hw_enabled(vcpu->arch.apic))
1040 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1041 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1047 void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1049 struct kvm_lapic *apic = vcpu->arch.apic;
1051 if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
1052 atomic_read(&apic->timer.pending) > 0) {
1053 if (__inject_apic_timer_irq(apic))
1054 atomic_dec(&apic->timer.pending);
1058 void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
1060 struct kvm_lapic *apic = vcpu->arch.apic;
1062 if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
1063 apic->timer.last_update = ktime_add_ns(
1064 apic->timer.last_update,
1065 apic->timer.period);
1068 int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1070 int vector = kvm_apic_has_interrupt(vcpu);
1071 struct kvm_lapic *apic = vcpu->arch.apic;
1076 apic_set_vector(vector, apic->regs + APIC_ISR);
1077 apic_update_ppr(apic);
1078 apic_clear_irr(vector, apic);
1082 void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
1084 struct kvm_lapic *apic = vcpu->arch.apic;
1086 apic->base_address = vcpu->arch.apic_base &
1087 MSR_IA32_APICBASE_BASE;
1088 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
1089 apic_update_ppr(apic);
1090 hrtimer_cancel(&apic->timer.dev);
1091 update_divide_count(apic);
1092 start_apic_timer(apic);
1095 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
1097 struct kvm_lapic *apic = vcpu->arch.apic;
1098 struct hrtimer *timer;
1103 timer = &apic->timer.dev;
1104 if (hrtimer_cancel(timer))
1105 hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
1108 void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
1113 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1116 vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
1117 data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
1118 kunmap_atomic(vapic, KM_USER0);
1120 apic_set_tpr(vcpu->arch.apic, data & 0xff);
1123 void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
1126 int max_irr, max_isr;
1127 struct kvm_lapic *apic;
1130 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1133 apic = vcpu->arch.apic;
1134 tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
1135 max_irr = apic_find_highest_irr(apic);
1138 max_isr = apic_find_highest_isr(apic);
1141 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1143 vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
1144 *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
1145 kunmap_atomic(vapic, KM_USER0);
1148 void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
1150 if (!irqchip_in_kernel(vcpu->kvm))
1153 vcpu->arch.apic->vapic_addr = vapic_addr;