blob: ca1db3852ace0a4759de0a57a44a3badda4f2570 [file] [log] [blame]
Eddie Dong97222cc2007-09-12 10:58:04 +03001
2/*
3 * Local APIC virtualization
4 *
5 * Copyright (C) 2006 Qumranet, Inc.
6 * Copyright (C) 2007 Novell
7 * Copyright (C) 2007 Intel
8 *
9 * Authors:
10 * Dor Laor <dor.laor@qumranet.com>
11 * Gregory Haskins <ghaskins@novell.com>
12 * Yaozu (Eddie) Dong <eddie.dong@intel.com>
13 *
14 * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
15 *
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
18 */
19
20#include "kvm.h"
21#include <linux/kvm.h>
22#include <linux/mm.h>
23#include <linux/highmem.h>
24#include <linux/smp.h>
25#include <linux/hrtimer.h>
26#include <linux/io.h>
27#include <linux/module.h>
28#include <asm/processor.h>
29#include <asm/msr.h>
30#include <asm/page.h>
31#include <asm/current.h>
32#include <asm/apicdef.h>
33#include <asm/atomic.h>
34#include <asm/div64.h>
35#include "irq.h"
36
37#define PRId64 "d"
38#define PRIx64 "llx"
39#define PRIu64 "u"
40#define PRIo64 "o"
41
42#define APIC_BUS_CYCLE_NS 1
43
44/* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
45#define apic_debug(fmt, arg...)
46
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
56
57#define VEC_POS(v) ((v) & (32 - 1))
58#define REG_POS(v) (((v) >> 5) << 4)
59static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
60{
61 return *((u32 *) (apic->regs + reg_off));
62}
63
64static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
65{
66 *((u32 *) (apic->regs + reg_off)) = val;
67}
68
69static inline int apic_test_and_set_vector(int vec, void *bitmap)
70{
71 return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
72}
73
74static inline int apic_test_and_clear_vector(int vec, void *bitmap)
75{
76 return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
77}
78
79static inline void apic_set_vector(int vec, void *bitmap)
80{
81 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
82}
83
84static inline void apic_clear_vector(int vec, void *bitmap)
85{
86 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
87}
88
89static inline int apic_hw_enabled(struct kvm_lapic *apic)
90{
91 return (apic)->vcpu->apic_base & MSR_IA32_APICBASE_ENABLE;
92}
93
94static inline int apic_sw_enabled(struct kvm_lapic *apic)
95{
96 return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
97}
98
99static inline int apic_enabled(struct kvm_lapic *apic)
100{
101 return apic_sw_enabled(apic) && apic_hw_enabled(apic);
102}
103
104#define LVT_MASK \
105 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
106
107#define LINT_MASK \
108 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
109 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
110
111static inline int kvm_apic_id(struct kvm_lapic *apic)
112{
113 return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
114}
115
116static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
117{
118 return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
119}
120
121static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
122{
123 return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
124}
125
126static inline int apic_lvtt_period(struct kvm_lapic *apic)
127{
128 return apic_get_reg(apic, APIC_LVTT) & APIC_LVT_TIMER_PERIODIC;
129}
130
131static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
132 LVT_MASK | APIC_LVT_TIMER_PERIODIC, /* LVTT */
133 LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
134 LVT_MASK | APIC_MODE_MASK, /* LVTPC */
135 LINT_MASK, LINT_MASK, /* LVT0-1 */
136 LVT_MASK /* LVTERR */
137};
138
139static int find_highest_vector(void *bitmap)
140{
141 u32 *word = bitmap;
142 int word_offset = MAX_APIC_VECTOR >> 5;
143
144 while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
145 continue;
146
147 if (likely(!word_offset && !word[0]))
148 return -1;
149 else
150 return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
151}
152
153static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
154{
155 return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
156}
157
158static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
159{
160 apic_clear_vector(vec, apic->regs + APIC_IRR);
161}
162
163static inline int apic_find_highest_irr(struct kvm_lapic *apic)
164{
165 int result;
166
167 result = find_highest_vector(apic->regs + APIC_IRR);
168 ASSERT(result == -1 || result >= 16);
169
170 return result;
171}
172
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800173int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
174{
175 struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
176 int highest_irr;
177
178 if (!apic)
179 return 0;
180 highest_irr = apic_find_highest_irr(apic);
181
182 return highest_irr;
183}
184EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
185
Eddie Dong97222cc2007-09-12 10:58:04 +0300186int kvm_apic_set_irq(struct kvm_lapic *apic, u8 vec, u8 trig)
187{
188 if (!apic_test_and_set_irr(vec, apic)) {
189 /* a new pending irq is set in IRR */
190 if (trig)
191 apic_set_vector(vec, apic->regs + APIC_TMR);
192 else
193 apic_clear_vector(vec, apic->regs + APIC_TMR);
194 kvm_vcpu_kick(apic->vcpu);
195 return 1;
196 }
197 return 0;
198}
199
200static inline int apic_find_highest_isr(struct kvm_lapic *apic)
201{
202 int result;
203
204 result = find_highest_vector(apic->regs + APIC_ISR);
205 ASSERT(result == -1 || result >= 16);
206
207 return result;
208}
209
210static void apic_update_ppr(struct kvm_lapic *apic)
211{
212 u32 tpr, isrv, ppr;
213 int isr;
214
215 tpr = apic_get_reg(apic, APIC_TASKPRI);
216 isr = apic_find_highest_isr(apic);
217 isrv = (isr != -1) ? isr : 0;
218
219 if ((tpr & 0xf0) >= (isrv & 0xf0))
220 ppr = tpr & 0xff;
221 else
222 ppr = isrv & 0xf0;
223
224 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
225 apic, ppr, isr, isrv);
226
227 apic_set_reg(apic, APIC_PROCPRI, ppr);
228}
229
230static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
231{
232 apic_set_reg(apic, APIC_TASKPRI, tpr);
233 apic_update_ppr(apic);
234}
235
236int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
237{
238 return kvm_apic_id(apic) == dest;
239}
240
241int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
242{
243 int result = 0;
244 u8 logical_id;
245
246 logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
247
248 switch (apic_get_reg(apic, APIC_DFR)) {
249 case APIC_DFR_FLAT:
250 if (logical_id & mda)
251 result = 1;
252 break;
253 case APIC_DFR_CLUSTER:
254 if (((logical_id >> 4) == (mda >> 0x4))
255 && (logical_id & mda & 0xf))
256 result = 1;
257 break;
258 default:
259 printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
260 apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
261 break;
262 }
263
264 return result;
265}
266
267static int apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
268 int short_hand, int dest, int dest_mode)
269{
270 int result = 0;
271 struct kvm_lapic *target = vcpu->apic;
272
273 apic_debug("target %p, source %p, dest 0x%x, "
274 "dest_mode 0x%x, short_hand 0x%x",
275 target, source, dest, dest_mode, short_hand);
276
277 ASSERT(!target);
278 switch (short_hand) {
279 case APIC_DEST_NOSHORT:
280 if (dest_mode == 0) {
281 /* Physical mode. */
282 if ((dest == 0xFF) || (dest == kvm_apic_id(target)))
283 result = 1;
284 } else
285 /* Logical mode. */
286 result = kvm_apic_match_logical_addr(target, dest);
287 break;
288 case APIC_DEST_SELF:
289 if (target == source)
290 result = 1;
291 break;
292 case APIC_DEST_ALLINC:
293 result = 1;
294 break;
295 case APIC_DEST_ALLBUT:
296 if (target != source)
297 result = 1;
298 break;
299 default:
300 printk(KERN_WARNING "Bad dest shorthand value %x\n",
301 short_hand);
302 break;
303 }
304
305 return result;
306}
307
308/*
309 * Add a pending IRQ into lapic.
310 * Return 1 if successfully added and 0 if discarded.
311 */
312static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
313 int vector, int level, int trig_mode)
314{
315 int result = 0;
Eddie Dong1b9778d2007-09-03 16:56:58 +0300316 int orig_irr;
Eddie Dong97222cc2007-09-12 10:58:04 +0300317
318 switch (delivery_mode) {
319 case APIC_DM_FIXED:
320 case APIC_DM_LOWEST:
321 /* FIXME add logic for vcpu on reset */
322 if (unlikely(!apic_enabled(apic)))
323 break;
324
Eddie Dong1b9778d2007-09-03 16:56:58 +0300325 orig_irr = apic_test_and_set_irr(vector, apic);
326 if (orig_irr && trig_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300327 apic_debug("level trig mode repeatedly for vector %d",
328 vector);
329 break;
330 }
331
332 if (trig_mode) {
333 apic_debug("level trig mode for vector %d", vector);
334 apic_set_vector(vector, apic->regs + APIC_TMR);
335 } else
336 apic_clear_vector(vector, apic->regs + APIC_TMR);
337
338 kvm_vcpu_kick(apic->vcpu);
339
Eddie Dong1b9778d2007-09-03 16:56:58 +0300340 result = (orig_irr == 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300341 break;
342
343 case APIC_DM_REMRD:
344 printk(KERN_DEBUG "Ignoring delivery mode 3\n");
345 break;
346
347 case APIC_DM_SMI:
348 printk(KERN_DEBUG "Ignoring guest SMI\n");
349 break;
350 case APIC_DM_NMI:
351 printk(KERN_DEBUG "Ignoring guest NMI\n");
352 break;
353
354 case APIC_DM_INIT:
355 printk(KERN_DEBUG "Ignoring guest INIT\n");
356 break;
357
358 case APIC_DM_STARTUP:
359 printk(KERN_DEBUG "Ignoring guest STARTUP\n");
360 break;
361
362 default:
363 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
364 delivery_mode);
365 break;
366 }
367 return result;
368}
369
370struct kvm_lapic *kvm_apic_round_robin(struct kvm *kvm, u8 vector,
371 unsigned long bitmap)
372{
373 int vcpu_id;
He, Qing932f72a2007-09-03 17:01:36 +0300374 int last;
375 int next;
376 struct kvm_lapic *apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300377
He, Qing932f72a2007-09-03 17:01:36 +0300378 last = kvm->round_robin_prev_vcpu;
379 next = last;
380
381 do {
382 if (++next == KVM_MAX_VCPUS)
383 next = 0;
384 if (kvm->vcpus[next] == NULL || !test_bit(next, &bitmap))
385 continue;
386 apic = kvm->vcpus[next]->apic;
387 if (apic && apic_enabled(apic))
388 break;
389 apic = NULL;
390 } while (next != last);
391 kvm->round_robin_prev_vcpu = next;
392
393 if (!apic) {
394 vcpu_id = ffs(bitmap) - 1;
395 if (vcpu_id < 0) {
396 vcpu_id = 0;
397 printk(KERN_DEBUG "vcpu not ready for apic_round_robin\n");
398 }
399 apic = kvm->vcpus[vcpu_id]->apic;
400 }
401
402 return apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300403}
404
405static void apic_set_eoi(struct kvm_lapic *apic)
406{
407 int vector = apic_find_highest_isr(apic);
408
409 /*
410 * Not every write EOI will has corresponding ISR,
411 * one example is when Kernel check timer on setup_IO_APIC
412 */
413 if (vector == -1)
414 return;
415
416 apic_clear_vector(vector, apic->regs + APIC_ISR);
417 apic_update_ppr(apic);
418
419 if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
420 kvm_ioapic_update_eoi(apic->vcpu->kvm, vector);
421}
422
423static void apic_send_ipi(struct kvm_lapic *apic)
424{
425 u32 icr_low = apic_get_reg(apic, APIC_ICR);
426 u32 icr_high = apic_get_reg(apic, APIC_ICR2);
427
428 unsigned int dest = GET_APIC_DEST_FIELD(icr_high);
429 unsigned int short_hand = icr_low & APIC_SHORT_MASK;
430 unsigned int trig_mode = icr_low & APIC_INT_LEVELTRIG;
431 unsigned int level = icr_low & APIC_INT_ASSERT;
432 unsigned int dest_mode = icr_low & APIC_DEST_MASK;
433 unsigned int delivery_mode = icr_low & APIC_MODE_MASK;
434 unsigned int vector = icr_low & APIC_VECTOR_MASK;
435
436 struct kvm_lapic *target;
437 struct kvm_vcpu *vcpu;
438 unsigned long lpr_map = 0;
439 int i;
440
441 apic_debug("icr_high 0x%x, icr_low 0x%x, "
442 "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
443 "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
444 icr_high, icr_low, short_hand, dest,
445 trig_mode, level, dest_mode, delivery_mode, vector);
446
447 for (i = 0; i < KVM_MAX_VCPUS; i++) {
448 vcpu = apic->vcpu->kvm->vcpus[i];
449 if (!vcpu)
450 continue;
451
452 if (vcpu->apic &&
453 apic_match_dest(vcpu, apic, short_hand, dest, dest_mode)) {
454 if (delivery_mode == APIC_DM_LOWEST)
455 set_bit(vcpu->vcpu_id, &lpr_map);
456 else
457 __apic_accept_irq(vcpu->apic, delivery_mode,
458 vector, level, trig_mode);
459 }
460 }
461
462 if (delivery_mode == APIC_DM_LOWEST) {
463 target = kvm_apic_round_robin(vcpu->kvm, vector, lpr_map);
464 if (target != NULL)
465 __apic_accept_irq(target, delivery_mode,
466 vector, level, trig_mode);
467 }
468}
469
470static u32 apic_get_tmcct(struct kvm_lapic *apic)
471{
472 u32 counter_passed;
473 ktime_t passed, now = apic->timer.dev.base->get_time();
474 u32 tmcct = apic_get_reg(apic, APIC_TMICT);
475
476 ASSERT(apic != NULL);
477
478 if (unlikely(ktime_to_ns(now) <=
479 ktime_to_ns(apic->timer.last_update))) {
480 /* Wrap around */
481 passed = ktime_add(( {
482 (ktime_t) {
483 .tv64 = KTIME_MAX -
484 (apic->timer.last_update).tv64}; }
485 ), now);
486 apic_debug("time elapsed\n");
487 } else
488 passed = ktime_sub(now, apic->timer.last_update);
489
490 counter_passed = div64_64(ktime_to_ns(passed),
491 (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
492 tmcct -= counter_passed;
493
494 if (tmcct <= 0) {
495 if (unlikely(!apic_lvtt_period(apic)))
496 tmcct = 0;
497 else
498 do {
499 tmcct += apic_get_reg(apic, APIC_TMICT);
500 } while (tmcct <= 0);
501 }
502
503 return tmcct;
504}
505
506static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
507{
508 u32 val = 0;
509
510 if (offset >= LAPIC_MMIO_LENGTH)
511 return 0;
512
513 switch (offset) {
514 case APIC_ARBPRI:
515 printk(KERN_WARNING "Access APIC ARBPRI register "
516 "which is for P6\n");
517 break;
518
519 case APIC_TMCCT: /* Timer CCR */
520 val = apic_get_tmcct(apic);
521 break;
522
523 default:
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800524 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300525 val = apic_get_reg(apic, offset);
526 break;
527 }
528
529 return val;
530}
531
532static void apic_mmio_read(struct kvm_io_device *this,
533 gpa_t address, int len, void *data)
534{
535 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
536 unsigned int offset = address - apic->base_address;
537 unsigned char alignment = offset & 0xf;
538 u32 result;
539
540 if ((alignment + len) > 4) {
541 printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
542 (unsigned long)address, len);
543 return;
544 }
545 result = __apic_read(apic, offset & ~0xf);
546
547 switch (len) {
548 case 1:
549 case 2:
550 case 4:
551 memcpy(data, (char *)&result + alignment, len);
552 break;
553 default:
554 printk(KERN_ERR "Local APIC read with len = %x, "
555 "should be 1,2, or 4 instead\n", len);
556 break;
557 }
558}
559
560static void update_divide_count(struct kvm_lapic *apic)
561{
562 u32 tmp1, tmp2, tdcr;
563
564 tdcr = apic_get_reg(apic, APIC_TDCR);
565 tmp1 = tdcr & 0xf;
566 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
567 apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
568
569 apic_debug("timer divide count is 0x%x\n",
570 apic->timer.divide_count);
571}
572
573static void start_apic_timer(struct kvm_lapic *apic)
574{
575 ktime_t now = apic->timer.dev.base->get_time();
576
577 apic->timer.last_update = now;
578
579 apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
580 APIC_BUS_CYCLE_NS * apic->timer.divide_count;
581 atomic_set(&apic->timer.pending, 0);
582 hrtimer_start(&apic->timer.dev,
583 ktime_add_ns(now, apic->timer.period),
584 HRTIMER_MODE_ABS);
585
586 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
587 PRIx64 ", "
588 "timer initial count 0x%x, period %lldns, "
589 "expire @ 0x%016" PRIx64 ".\n", __FUNCTION__,
590 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
591 apic_get_reg(apic, APIC_TMICT),
592 apic->timer.period,
593 ktime_to_ns(ktime_add_ns(now,
594 apic->timer.period)));
595}
596
597static void apic_mmio_write(struct kvm_io_device *this,
598 gpa_t address, int len, const void *data)
599{
600 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
601 unsigned int offset = address - apic->base_address;
602 unsigned char alignment = offset & 0xf;
603 u32 val;
604
605 /*
606 * APIC register must be aligned on 128-bits boundary.
607 * 32/64/128 bits registers must be accessed thru 32 bits.
608 * Refer SDM 8.4.1
609 */
610 if (len != 4 || alignment) {
611 if (printk_ratelimit())
612 printk(KERN_ERR "apic write: bad size=%d %lx\n",
613 len, (long)address);
614 return;
615 }
616
617 val = *(u32 *) data;
618
619 /* too common printing */
620 if (offset != APIC_EOI)
621 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
622 "0x%x\n", __FUNCTION__, offset, len, val);
623
624 offset &= 0xff0;
625
626 switch (offset) {
627 case APIC_ID: /* Local APIC ID */
628 apic_set_reg(apic, APIC_ID, val);
629 break;
630
631 case APIC_TASKPRI:
632 apic_set_tpr(apic, val & 0xff);
633 break;
634
635 case APIC_EOI:
636 apic_set_eoi(apic);
637 break;
638
639 case APIC_LDR:
640 apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
641 break;
642
643 case APIC_DFR:
644 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
645 break;
646
647 case APIC_SPIV:
648 apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
649 if (!(val & APIC_SPIV_APIC_ENABLED)) {
650 int i;
651 u32 lvt_val;
652
653 for (i = 0; i < APIC_LVT_NUM; i++) {
654 lvt_val = apic_get_reg(apic,
655 APIC_LVTT + 0x10 * i);
656 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
657 lvt_val | APIC_LVT_MASKED);
658 }
659 atomic_set(&apic->timer.pending, 0);
660
661 }
662 break;
663
664 case APIC_ICR:
665 /* No delay here, so we always clear the pending bit */
666 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
667 apic_send_ipi(apic);
668 break;
669
670 case APIC_ICR2:
671 apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
672 break;
673
674 case APIC_LVTT:
675 case APIC_LVTTHMR:
676 case APIC_LVTPC:
677 case APIC_LVT0:
678 case APIC_LVT1:
679 case APIC_LVTERR:
680 /* TODO: Check vector */
681 if (!apic_sw_enabled(apic))
682 val |= APIC_LVT_MASKED;
683
684 val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
685 apic_set_reg(apic, offset, val);
686
687 break;
688
689 case APIC_TMICT:
690 hrtimer_cancel(&apic->timer.dev);
691 apic_set_reg(apic, APIC_TMICT, val);
692 start_apic_timer(apic);
693 return;
694
695 case APIC_TDCR:
696 if (val & 4)
697 printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
698 apic_set_reg(apic, APIC_TDCR, val);
699 update_divide_count(apic);
700 break;
701
702 default:
703 apic_debug("Local APIC Write to read-only register %x\n",
704 offset);
705 break;
706 }
707
708}
709
710static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
711{
712 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
713 int ret = 0;
714
715
716 if (apic_hw_enabled(apic) &&
717 (addr >= apic->base_address) &&
718 (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
719 ret = 1;
720
721 return ret;
722}
723
724void kvm_free_apic(struct kvm_lapic *apic)
725{
726 if (!apic)
727 return;
728
729 hrtimer_cancel(&apic->timer.dev);
730
731 if (apic->regs_page) {
732 __free_page(apic->regs_page);
733 apic->regs_page = 0;
734 }
735
736 kfree(apic);
737}
738
739/*
740 *----------------------------------------------------------------------
741 * LAPIC interface
742 *----------------------------------------------------------------------
743 */
744
745void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
746{
747 struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
748
749 if (!apic)
750 return;
751 apic_set_tpr(apic, ((cr8 & 0x0f) << 4));
752}
753
754u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
755{
756 struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
757 u64 tpr;
758
759 if (!apic)
760 return 0;
761 tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
762
763 return (tpr & 0xf0) >> 4;
764}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800765EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8);
Eddie Dong97222cc2007-09-12 10:58:04 +0300766
767void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
768{
769 struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
770
771 if (!apic) {
772 value |= MSR_IA32_APICBASE_BSP;
773 vcpu->apic_base = value;
774 return;
775 }
776 if (apic->vcpu->vcpu_id)
777 value &= ~MSR_IA32_APICBASE_BSP;
778
779 vcpu->apic_base = value;
780 apic->base_address = apic->vcpu->apic_base &
781 MSR_IA32_APICBASE_BASE;
782
783 /* with FSB delivery interrupt, we can restart APIC functionality */
784 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
785 "0x%lx.\n", apic->apic_base, apic->base_address);
786
787}
788
789u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
790{
791 return vcpu->apic_base;
792}
793EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
794
795static void lapic_reset(struct kvm_vcpu *vcpu)
796{
797 struct kvm_lapic *apic;
798 int i;
799
800 apic_debug("%s\n", __FUNCTION__);
801
802 ASSERT(vcpu);
803 apic = vcpu->apic;
804 ASSERT(apic != NULL);
805
806 /* Stop the timer in case it's a reset to an active apic */
807 hrtimer_cancel(&apic->timer.dev);
808
809 apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
810 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
811
812 for (i = 0; i < APIC_LVT_NUM; i++)
813 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Qing He40487c62007-09-17 14:47:13 +0800814 apic_set_reg(apic, APIC_LVT0,
815 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Eddie Dong97222cc2007-09-12 10:58:04 +0300816
817 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
818 apic_set_reg(apic, APIC_SPIV, 0xff);
819 apic_set_reg(apic, APIC_TASKPRI, 0);
820 apic_set_reg(apic, APIC_LDR, 0);
821 apic_set_reg(apic, APIC_ESR, 0);
822 apic_set_reg(apic, APIC_ICR, 0);
823 apic_set_reg(apic, APIC_ICR2, 0);
824 apic_set_reg(apic, APIC_TDCR, 0);
825 apic_set_reg(apic, APIC_TMICT, 0);
826 for (i = 0; i < 8; i++) {
827 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
828 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
829 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
830 }
831 apic->timer.divide_count = 0;
832 atomic_set(&apic->timer.pending, 0);
833 if (vcpu->vcpu_id == 0)
834 vcpu->apic_base |= MSR_IA32_APICBASE_BSP;
835 apic_update_ppr(apic);
836
837 apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
838 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __FUNCTION__,
839 vcpu, kvm_apic_id(apic),
840 vcpu->apic_base, apic->base_address);
841}
842
843int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
844{
845 struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
846 int ret = 0;
847
848 if (!apic)
849 return 0;
850 ret = apic_enabled(apic);
851
852 return ret;
853}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800854EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
Eddie Dong97222cc2007-09-12 10:58:04 +0300855
856/*
857 *----------------------------------------------------------------------
858 * timer interface
859 *----------------------------------------------------------------------
860 */
Eddie Dong1b9778d2007-09-03 16:56:58 +0300861
862/* TODO: make sure __apic_timer_fn runs in current pCPU */
Eddie Dong97222cc2007-09-12 10:58:04 +0300863static int __apic_timer_fn(struct kvm_lapic *apic)
864{
Eddie Dong97222cc2007-09-12 10:58:04 +0300865 int result = 0;
Eddie Dong1b9778d2007-09-03 16:56:58 +0300866 wait_queue_head_t *q = &apic->vcpu->wq;
Eddie Dong97222cc2007-09-12 10:58:04 +0300867
Eddie Dong97222cc2007-09-12 10:58:04 +0300868 atomic_inc(&apic->timer.pending);
Eddie Dong1b9778d2007-09-03 16:56:58 +0300869 if (waitqueue_active(q))
870 wake_up_interruptible(q);
Eddie Dong97222cc2007-09-12 10:58:04 +0300871 if (apic_lvtt_period(apic)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300872 result = 1;
873 apic->timer.dev.expires = ktime_add_ns(
874 apic->timer.dev.expires,
875 apic->timer.period);
876 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300877 return result;
878}
879
Eddie Dong1b9778d2007-09-03 16:56:58 +0300880static int __inject_apic_timer_irq(struct kvm_lapic *apic)
881{
882 int vector;
883
884 vector = apic_lvt_vector(apic, APIC_LVTT);
885 return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
886}
887
Eddie Dong97222cc2007-09-12 10:58:04 +0300888static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
889{
890 struct kvm_lapic *apic;
891 int restart_timer = 0;
892
893 apic = container_of(data, struct kvm_lapic, timer.dev);
894
895 restart_timer = __apic_timer_fn(apic);
896
897 if (restart_timer)
898 return HRTIMER_RESTART;
899 else
900 return HRTIMER_NORESTART;
901}
902
903int kvm_create_lapic(struct kvm_vcpu *vcpu)
904{
905 struct kvm_lapic *apic;
906
907 ASSERT(vcpu != NULL);
908 apic_debug("apic_init %d\n", vcpu->vcpu_id);
909
910 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
911 if (!apic)
912 goto nomem;
913
914 vcpu->apic = apic;
915
916 apic->regs_page = alloc_page(GFP_KERNEL);
917 if (apic->regs_page == NULL) {
918 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
919 vcpu->vcpu_id);
920 goto nomem;
921 }
922 apic->regs = page_address(apic->regs_page);
923 memset(apic->regs, 0, PAGE_SIZE);
924 apic->vcpu = vcpu;
925
926 hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
927 apic->timer.dev.function = apic_timer_fn;
928 apic->base_address = APIC_DEFAULT_PHYS_BASE;
929 vcpu->apic_base = APIC_DEFAULT_PHYS_BASE;
930
931 lapic_reset(vcpu);
932 apic->dev.read = apic_mmio_read;
933 apic->dev.write = apic_mmio_write;
934 apic->dev.in_range = apic_mmio_range;
935 apic->dev.private = apic;
936
937 return 0;
938nomem:
939 kvm_free_apic(apic);
940 return -ENOMEM;
941}
942EXPORT_SYMBOL_GPL(kvm_create_lapic);
943
944int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
945{
946 struct kvm_lapic *apic = vcpu->apic;
947 int highest_irr;
948
949 if (!apic || !apic_enabled(apic))
950 return -1;
951
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800952 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300953 highest_irr = apic_find_highest_irr(apic);
954 if ((highest_irr == -1) ||
955 ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
956 return -1;
957 return highest_irr;
958}
959
Qing He40487c62007-09-17 14:47:13 +0800960int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
961{
962 u32 lvt0 = apic_get_reg(vcpu->apic, APIC_LVT0);
963 int r = 0;
964
965 if (vcpu->vcpu_id == 0) {
966 if (!apic_hw_enabled(vcpu->apic))
967 r = 1;
968 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
969 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
970 r = 1;
971 }
972 return r;
973}
974
Eddie Dong1b9778d2007-09-03 16:56:58 +0300975void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
976{
977 struct kvm_lapic *apic = vcpu->apic;
978
979 if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
980 atomic_read(&apic->timer.pending) > 0) {
981 if (__inject_apic_timer_irq(apic))
982 atomic_dec(&apic->timer.pending);
983 }
984}
985
986void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
987{
988 struct kvm_lapic *apic = vcpu->apic;
989
990 if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
991 apic->timer.last_update = ktime_add_ns(
992 apic->timer.last_update,
993 apic->timer.period);
994}
995
Eddie Dong97222cc2007-09-12 10:58:04 +0300996int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
997{
998 int vector = kvm_apic_has_interrupt(vcpu);
999 struct kvm_lapic *apic = vcpu->apic;
1000
1001 if (vector == -1)
1002 return -1;
1003
1004 apic_set_vector(vector, apic->regs + APIC_ISR);
1005 apic_update_ppr(apic);
1006 apic_clear_irr(vector, apic);
1007 return vector;
1008}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001009
1010void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
1011{
1012 struct kvm_lapic *apic = vcpu->apic;
1013
1014 apic->base_address = vcpu->apic_base &
1015 MSR_IA32_APICBASE_BASE;
1016 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
1017 apic_update_ppr(apic);
1018 hrtimer_cancel(&apic->timer.dev);
1019 update_divide_count(apic);
1020 start_apic_timer(apic);
1021}
Eddie Donga3d7f852007-09-03 16:15:12 +03001022
1023void kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
1024{
1025 struct kvm_lapic *apic = vcpu->apic;
1026 struct hrtimer *timer;
1027
1028 if (!apic)
1029 return;
1030
1031 timer = &apic->timer.dev;
1032 if (hrtimer_cancel(timer))
1033 hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
1034}
1035EXPORT_SYMBOL_GPL(kvm_migrate_apic_timer);