blob: f9201fbc61d1fcbedfedd33608e9da5aed861141 [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
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030021#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>
Roman Zippel6f6d6a12008-05-01 04:34:28 -070028#include <linux/math64.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030029#include <asm/processor.h>
30#include <asm/msr.h>
31#include <asm/page.h>
32#include <asm/current.h>
33#include <asm/apicdef.h>
34#include <asm/atomic.h>
Eddie Dong97222cc2007-09-12 10:58:04 +030035#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)
Zhang Xiantaoad312c72007-12-13 23:50:52 +080059
Eddie Dong97222cc2007-09-12 10:58:04 +030060static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
61{
62 return *((u32 *) (apic->regs + reg_off));
63}
64
65static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
66{
67 *((u32 *) (apic->regs + reg_off)) = val;
68}
69
70static inline int apic_test_and_set_vector(int vec, void *bitmap)
71{
72 return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
73}
74
75static inline int apic_test_and_clear_vector(int vec, void *bitmap)
76{
77 return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
78}
79
80static inline void apic_set_vector(int vec, void *bitmap)
81{
82 set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
83}
84
85static inline void apic_clear_vector(int vec, void *bitmap)
86{
87 clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
88}
89
90static inline int apic_hw_enabled(struct kvm_lapic *apic)
91{
Zhang Xiantaoad312c72007-12-13 23:50:52 +080092 return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
Eddie Dong97222cc2007-09-12 10:58:04 +030093}
94
95static inline int apic_sw_enabled(struct kvm_lapic *apic)
96{
97 return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
98}
99
100static inline int apic_enabled(struct kvm_lapic *apic)
101{
102 return apic_sw_enabled(apic) && apic_hw_enabled(apic);
103}
104
105#define LVT_MASK \
106 (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
107
108#define LINT_MASK \
109 (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
110 APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
111
112static inline int kvm_apic_id(struct kvm_lapic *apic)
113{
114 return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
115}
116
117static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
118{
119 return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
120}
121
122static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
123{
124 return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
125}
126
127static inline int apic_lvtt_period(struct kvm_lapic *apic)
128{
129 return apic_get_reg(apic, APIC_LVTT) & APIC_LVT_TIMER_PERIODIC;
130}
131
132static 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 */
138};
139
140static int find_highest_vector(void *bitmap)
141{
142 u32 *word = bitmap;
143 int word_offset = MAX_APIC_VECTOR >> 5;
144
145 while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
146 continue;
147
148 if (likely(!word_offset && !word[0]))
149 return -1;
150 else
151 return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
152}
153
154static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
155{
156 return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
157}
158
159static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
160{
161 apic_clear_vector(vec, apic->regs + APIC_IRR);
162}
163
164static inline int apic_find_highest_irr(struct kvm_lapic *apic)
165{
166 int result;
167
168 result = find_highest_vector(apic->regs + APIC_IRR);
169 ASSERT(result == -1 || result >= 16);
170
171 return result;
172}
173
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800174int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
175{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800176 struct kvm_lapic *apic = vcpu->arch.apic;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800177 int highest_irr;
178
179 if (!apic)
180 return 0;
181 highest_irr = apic_find_highest_irr(apic);
182
183 return highest_irr;
184}
185EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
186
Zhang Xiantao8be54532007-12-02 22:35:57 +0800187int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
Eddie Dong97222cc2007-09-12 10:58:04 +0300188{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800189 struct kvm_lapic *apic = vcpu->arch.apic;
Zhang Xiantao8be54532007-12-02 22:35:57 +0800190
Eddie Dong97222cc2007-09-12 10:58:04 +0300191 if (!apic_test_and_set_irr(vec, apic)) {
192 /* a new pending irq is set in IRR */
193 if (trig)
194 apic_set_vector(vec, apic->regs + APIC_TMR);
195 else
196 apic_clear_vector(vec, apic->regs + APIC_TMR);
197 kvm_vcpu_kick(apic->vcpu);
198 return 1;
199 }
200 return 0;
201}
202
203static inline int apic_find_highest_isr(struct kvm_lapic *apic)
204{
205 int result;
206
207 result = find_highest_vector(apic->regs + APIC_ISR);
208 ASSERT(result == -1 || result >= 16);
209
210 return result;
211}
212
213static void apic_update_ppr(struct kvm_lapic *apic)
214{
215 u32 tpr, isrv, ppr;
216 int isr;
217
218 tpr = apic_get_reg(apic, APIC_TASKPRI);
219 isr = apic_find_highest_isr(apic);
220 isrv = (isr != -1) ? isr : 0;
221
222 if ((tpr & 0xf0) >= (isrv & 0xf0))
223 ppr = tpr & 0xff;
224 else
225 ppr = isrv & 0xf0;
226
227 apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
228 apic, ppr, isr, isrv);
229
230 apic_set_reg(apic, APIC_PROCPRI, ppr);
231}
232
233static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
234{
235 apic_set_reg(apic, APIC_TASKPRI, tpr);
236 apic_update_ppr(apic);
237}
238
239int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
240{
241 return kvm_apic_id(apic) == dest;
242}
243
244int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
245{
246 int result = 0;
247 u8 logical_id;
248
249 logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
250
251 switch (apic_get_reg(apic, APIC_DFR)) {
252 case APIC_DFR_FLAT:
253 if (logical_id & mda)
254 result = 1;
255 break;
256 case APIC_DFR_CLUSTER:
257 if (((logical_id >> 4) == (mda >> 0x4))
258 && (logical_id & mda & 0xf))
259 result = 1;
260 break;
261 default:
262 printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
263 apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
264 break;
265 }
266
267 return result;
268}
269
270static int apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
271 int short_hand, int dest, int dest_mode)
272{
273 int result = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800274 struct kvm_lapic *target = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300275
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);
279
280 ASSERT(!target);
281 switch (short_hand) {
282 case APIC_DEST_NOSHORT:
283 if (dest_mode == 0) {
284 /* Physical mode. */
285 if ((dest == 0xFF) || (dest == kvm_apic_id(target)))
286 result = 1;
287 } else
288 /* Logical mode. */
289 result = kvm_apic_match_logical_addr(target, dest);
290 break;
291 case APIC_DEST_SELF:
292 if (target == source)
293 result = 1;
294 break;
295 case APIC_DEST_ALLINC:
296 result = 1;
297 break;
298 case APIC_DEST_ALLBUT:
299 if (target != source)
300 result = 1;
301 break;
302 default:
303 printk(KERN_WARNING "Bad dest shorthand value %x\n",
304 short_hand);
305 break;
306 }
307
308 return result;
309}
310
311/*
312 * Add a pending IRQ into lapic.
313 * Return 1 if successfully added and 0 if discarded.
314 */
315static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
316 int vector, int level, int trig_mode)
317{
He, Qingc5ec1532007-09-03 17:07:41 +0300318 int orig_irr, result = 0;
319 struct kvm_vcpu *vcpu = apic->vcpu;
Eddie Dong97222cc2007-09-12 10:58:04 +0300320
321 switch (delivery_mode) {
322 case APIC_DM_FIXED:
323 case APIC_DM_LOWEST:
324 /* FIXME add logic for vcpu on reset */
325 if (unlikely(!apic_enabled(apic)))
326 break;
327
Eddie Dong1b9778d2007-09-03 16:56:58 +0300328 orig_irr = apic_test_and_set_irr(vector, apic);
329 if (orig_irr && trig_mode) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300330 apic_debug("level trig mode repeatedly for vector %d",
331 vector);
332 break;
333 }
334
335 if (trig_mode) {
336 apic_debug("level trig mode for vector %d", vector);
337 apic_set_vector(vector, apic->regs + APIC_TMR);
338 } else
339 apic_clear_vector(vector, apic->regs + APIC_TMR);
340
Avi Kivitya4535292008-04-13 17:54:35 +0300341 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
He, Qingc5ec1532007-09-03 17:07:41 +0300342 kvm_vcpu_kick(vcpu);
Avi Kivitya4535292008-04-13 17:54:35 +0300343 else if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) {
344 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
He, Qingc5ec1532007-09-03 17:07:41 +0300345 if (waitqueue_active(&vcpu->wq))
346 wake_up_interruptible(&vcpu->wq);
347 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300348
Eddie Dong1b9778d2007-09-03 16:56:58 +0300349 result = (orig_irr == 0);
Eddie Dong97222cc2007-09-12 10:58:04 +0300350 break;
351
352 case APIC_DM_REMRD:
353 printk(KERN_DEBUG "Ignoring delivery mode 3\n");
354 break;
355
356 case APIC_DM_SMI:
357 printk(KERN_DEBUG "Ignoring guest SMI\n");
358 break;
359 case APIC_DM_NMI:
360 printk(KERN_DEBUG "Ignoring guest NMI\n");
361 break;
362
363 case APIC_DM_INIT:
He, Qingc5ec1532007-09-03 17:07:41 +0300364 if (level) {
Avi Kivitya4535292008-04-13 17:54:35 +0300365 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
He, Qingc5ec1532007-09-03 17:07:41 +0300366 printk(KERN_DEBUG
367 "INIT on a runnable vcpu %d\n",
368 vcpu->vcpu_id);
Avi Kivitya4535292008-04-13 17:54:35 +0300369 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
He, Qingc5ec1532007-09-03 17:07:41 +0300370 kvm_vcpu_kick(vcpu);
371 } else {
372 printk(KERN_DEBUG
373 "Ignoring de-assert INIT to vcpu %d\n",
374 vcpu->vcpu_id);
375 }
376
Eddie Dong97222cc2007-09-12 10:58:04 +0300377 break;
378
379 case APIC_DM_STARTUP:
He, Qingc5ec1532007-09-03 17:07:41 +0300380 printk(KERN_DEBUG "SIPI to vcpu %d vector 0x%02x\n",
381 vcpu->vcpu_id, vector);
Avi Kivitya4535292008-04-13 17:54:35 +0300382 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800383 vcpu->arch.sipi_vector = vector;
Avi Kivitya4535292008-04-13 17:54:35 +0300384 vcpu->arch.mp_state = KVM_MP_STATE_SIPI_RECEIVED;
He, Qingc5ec1532007-09-03 17:07:41 +0300385 if (waitqueue_active(&vcpu->wq))
386 wake_up_interruptible(&vcpu->wq);
387 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300388 break;
389
390 default:
391 printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
392 delivery_mode);
393 break;
394 }
395 return result;
396}
397
Zhang Xiantao8be54532007-12-02 22:35:57 +0800398static struct kvm_lapic *kvm_apic_round_robin(struct kvm *kvm, u8 vector,
Eddie Dong97222cc2007-09-12 10:58:04 +0300399 unsigned long bitmap)
400{
He, Qing932f72a2007-09-03 17:01:36 +0300401 int last;
402 int next;
Qing Hee4d47f42007-09-24 17:39:41 +0800403 struct kvm_lapic *apic = NULL;
Eddie Dong97222cc2007-09-12 10:58:04 +0300404
Zhang Xiantaobfc6d222007-12-14 10:20:16 +0800405 last = kvm->arch.round_robin_prev_vcpu;
He, Qing932f72a2007-09-03 17:01:36 +0300406 next = last;
407
408 do {
409 if (++next == KVM_MAX_VCPUS)
410 next = 0;
411 if (kvm->vcpus[next] == NULL || !test_bit(next, &bitmap))
412 continue;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800413 apic = kvm->vcpus[next]->arch.apic;
He, Qing932f72a2007-09-03 17:01:36 +0300414 if (apic && apic_enabled(apic))
415 break;
416 apic = NULL;
417 } while (next != last);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +0800418 kvm->arch.round_robin_prev_vcpu = next;
He, Qing932f72a2007-09-03 17:01:36 +0300419
Qing Hee4d47f42007-09-24 17:39:41 +0800420 if (!apic)
421 printk(KERN_DEBUG "vcpu not ready for apic_round_robin\n");
He, Qing932f72a2007-09-03 17:01:36 +0300422
423 return apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300424}
425
Zhang Xiantao8be54532007-12-02 22:35:57 +0800426struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
427 unsigned long bitmap)
428{
429 struct kvm_lapic *apic;
430
431 apic = kvm_apic_round_robin(kvm, vector, bitmap);
432 if (apic)
433 return apic->vcpu;
434 return NULL;
435}
436
Eddie Dong97222cc2007-09-12 10:58:04 +0300437static void apic_set_eoi(struct kvm_lapic *apic)
438{
439 int vector = apic_find_highest_isr(apic);
440
441 /*
442 * Not every write EOI will has corresponding ISR,
443 * one example is when Kernel check timer on setup_IO_APIC
444 */
445 if (vector == -1)
446 return;
447
448 apic_clear_vector(vector, apic->regs + APIC_ISR);
449 apic_update_ppr(apic);
450
451 if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
452 kvm_ioapic_update_eoi(apic->vcpu->kvm, vector);
453}
454
455static void apic_send_ipi(struct kvm_lapic *apic)
456{
457 u32 icr_low = apic_get_reg(apic, APIC_ICR);
458 u32 icr_high = apic_get_reg(apic, APIC_ICR2);
459
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;
467
Zhang Xiantao8be54532007-12-02 22:35:57 +0800468 struct kvm_vcpu *target;
Eddie Dong97222cc2007-09-12 10:58:04 +0300469 struct kvm_vcpu *vcpu;
470 unsigned long lpr_map = 0;
471 int i;
472
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);
478
479 for (i = 0; i < KVM_MAX_VCPUS; i++) {
480 vcpu = apic->vcpu->kvm->vcpus[i];
481 if (!vcpu)
482 continue;
483
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800484 if (vcpu->arch.apic &&
Eddie Dong97222cc2007-09-12 10:58:04 +0300485 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);
488 else
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800489 __apic_accept_irq(vcpu->arch.apic, delivery_mode,
Eddie Dong97222cc2007-09-12 10:58:04 +0300490 vector, level, trig_mode);
491 }
492 }
493
494 if (delivery_mode == APIC_DM_LOWEST) {
Zhang Xiantao8be54532007-12-02 22:35:57 +0800495 target = kvm_get_lowest_prio_vcpu(vcpu->kvm, vector, lpr_map);
Eddie Dong97222cc2007-09-12 10:58:04 +0300496 if (target != NULL)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800497 __apic_accept_irq(target->arch.apic, delivery_mode,
Eddie Dong97222cc2007-09-12 10:58:04 +0300498 vector, level, trig_mode);
499 }
500}
501
502static u32 apic_get_tmcct(struct kvm_lapic *apic)
503{
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200504 u64 counter_passed;
505 ktime_t passed, now;
506 u32 tmcct;
Eddie Dong97222cc2007-09-12 10:58:04 +0300507
508 ASSERT(apic != NULL);
509
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200510 now = apic->timer.dev.base->get_time();
511 tmcct = apic_get_reg(apic, APIC_TMICT);
512
513 /* if initial count is 0, current count should also be 0 */
514 if (tmcct == 0)
515 return 0;
516
Eddie Dong97222cc2007-09-12 10:58:04 +0300517 if (unlikely(ktime_to_ns(now) <=
518 ktime_to_ns(apic->timer.last_update))) {
519 /* Wrap around */
520 passed = ktime_add(( {
521 (ktime_t) {
522 .tv64 = KTIME_MAX -
523 (apic->timer.last_update).tv64}; }
524 ), now);
525 apic_debug("time elapsed\n");
526 } else
527 passed = ktime_sub(now, apic->timer.last_update);
528
Roman Zippel6f6d6a12008-05-01 04:34:28 -0700529 counter_passed = div64_u64(ktime_to_ns(passed),
530 (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
Eddie Dong97222cc2007-09-12 10:58:04 +0300531
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200532 if (counter_passed > tmcct) {
533 if (unlikely(!apic_lvtt_period(apic))) {
534 /* one-shot timers stick at 0 until reset */
Eddie Dong97222cc2007-09-12 10:58:04 +0300535 tmcct = 0;
Kevin Pedretti9da8f4e2007-10-21 08:55:50 +0200536 } else {
537 /*
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??
542 */
543 while (counter_passed > tmcct)
544 counter_passed -= tmcct;
545 tmcct -= counter_passed;
546 }
547 } else {
548 tmcct -= counter_passed;
Eddie Dong97222cc2007-09-12 10:58:04 +0300549 }
550
551 return tmcct;
552}
553
Avi Kivityb209749f2007-10-22 16:50:39 +0200554static void __report_tpr_access(struct kvm_lapic *apic, bool write)
555{
556 struct kvm_vcpu *vcpu = apic->vcpu;
557 struct kvm_run *run = vcpu->run;
558
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;
563}
564
565static inline void report_tpr_access(struct kvm_lapic *apic, bool write)
566{
567 if (apic->vcpu->arch.tpr_access_reporting)
568 __report_tpr_access(apic, write);
569}
570
Eddie Dong97222cc2007-09-12 10:58:04 +0300571static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
572{
573 u32 val = 0;
574
Joerg Roedelc7bf23b2008-04-30 17:55:59 +0200575 KVMTRACE_1D(APIC_ACCESS, apic->vcpu, (u32)offset, handler);
576
Eddie Dong97222cc2007-09-12 10:58:04 +0300577 if (offset >= LAPIC_MMIO_LENGTH)
578 return 0;
579
580 switch (offset) {
581 case APIC_ARBPRI:
582 printk(KERN_WARNING "Access APIC ARBPRI register "
583 "which is for P6\n");
584 break;
585
586 case APIC_TMCCT: /* Timer CCR */
587 val = apic_get_tmcct(apic);
588 break;
589
Avi Kivityb209749f2007-10-22 16:50:39 +0200590 case APIC_TASKPRI:
591 report_tpr_access(apic, false);
592 /* fall thru */
Eddie Dong97222cc2007-09-12 10:58:04 +0300593 default:
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800594 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300595 val = apic_get_reg(apic, offset);
596 break;
597 }
598
599 return val;
600}
601
602static void apic_mmio_read(struct kvm_io_device *this,
603 gpa_t address, int len, void *data)
604{
605 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
606 unsigned int offset = address - apic->base_address;
607 unsigned char alignment = offset & 0xf;
608 u32 result;
609
610 if ((alignment + len) > 4) {
611 printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
612 (unsigned long)address, len);
613 return;
614 }
615 result = __apic_read(apic, offset & ~0xf);
616
617 switch (len) {
618 case 1:
619 case 2:
620 case 4:
621 memcpy(data, (char *)&result + alignment, len);
622 break;
623 default:
624 printk(KERN_ERR "Local APIC read with len = %x, "
625 "should be 1,2, or 4 instead\n", len);
626 break;
627 }
628}
629
630static void update_divide_count(struct kvm_lapic *apic)
631{
632 u32 tmp1, tmp2, tdcr;
633
634 tdcr = apic_get_reg(apic, APIC_TDCR);
635 tmp1 = tdcr & 0xf;
636 tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
637 apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
638
639 apic_debug("timer divide count is 0x%x\n",
640 apic->timer.divide_count);
641}
642
643static void start_apic_timer(struct kvm_lapic *apic)
644{
645 ktime_t now = apic->timer.dev.base->get_time();
646
647 apic->timer.last_update = now;
648
649 apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
650 APIC_BUS_CYCLE_NS * apic->timer.divide_count;
651 atomic_set(&apic->timer.pending, 0);
Avi Kivity0b975a32008-02-24 14:37:50 +0200652
653 if (!apic->timer.period)
654 return;
655
Eddie Dong97222cc2007-09-12 10:58:04 +0300656 hrtimer_start(&apic->timer.dev,
657 ktime_add_ns(now, apic->timer.period),
658 HRTIMER_MODE_ABS);
659
660 apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
661 PRIx64 ", "
662 "timer initial count 0x%x, period %lldns, "
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800663 "expire @ 0x%016" PRIx64 ".\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +0300664 APIC_BUS_CYCLE_NS, ktime_to_ns(now),
665 apic_get_reg(apic, APIC_TMICT),
666 apic->timer.period,
667 ktime_to_ns(ktime_add_ns(now,
668 apic->timer.period)));
669}
670
671static void apic_mmio_write(struct kvm_io_device *this,
672 gpa_t address, int len, const void *data)
673{
674 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
675 unsigned int offset = address - apic->base_address;
676 unsigned char alignment = offset & 0xf;
677 u32 val;
678
679 /*
680 * APIC register must be aligned on 128-bits boundary.
681 * 32/64/128 bits registers must be accessed thru 32 bits.
682 * Refer SDM 8.4.1
683 */
684 if (len != 4 || alignment) {
685 if (printk_ratelimit())
686 printk(KERN_ERR "apic write: bad size=%d %lx\n",
687 len, (long)address);
688 return;
689 }
690
691 val = *(u32 *) data;
692
693 /* too common printing */
694 if (offset != APIC_EOI)
695 apic_debug("%s: offset 0x%x with length 0x%x, and value is "
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800696 "0x%x\n", __func__, offset, len, val);
Eddie Dong97222cc2007-09-12 10:58:04 +0300697
698 offset &= 0xff0;
699
Joerg Roedelc7bf23b2008-04-30 17:55:59 +0200700 KVMTRACE_1D(APIC_ACCESS, apic->vcpu, (u32)offset, handler);
701
Eddie Dong97222cc2007-09-12 10:58:04 +0300702 switch (offset) {
703 case APIC_ID: /* Local APIC ID */
704 apic_set_reg(apic, APIC_ID, val);
705 break;
706
707 case APIC_TASKPRI:
Avi Kivityb209749f2007-10-22 16:50:39 +0200708 report_tpr_access(apic, true);
Eddie Dong97222cc2007-09-12 10:58:04 +0300709 apic_set_tpr(apic, val & 0xff);
710 break;
711
712 case APIC_EOI:
713 apic_set_eoi(apic);
714 break;
715
716 case APIC_LDR:
717 apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
718 break;
719
720 case APIC_DFR:
721 apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
722 break;
723
724 case APIC_SPIV:
725 apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
726 if (!(val & APIC_SPIV_APIC_ENABLED)) {
727 int i;
728 u32 lvt_val;
729
730 for (i = 0; i < APIC_LVT_NUM; i++) {
731 lvt_val = apic_get_reg(apic,
732 APIC_LVTT + 0x10 * i);
733 apic_set_reg(apic, APIC_LVTT + 0x10 * i,
734 lvt_val | APIC_LVT_MASKED);
735 }
736 atomic_set(&apic->timer.pending, 0);
737
738 }
739 break;
740
741 case APIC_ICR:
742 /* No delay here, so we always clear the pending bit */
743 apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
744 apic_send_ipi(apic);
745 break;
746
747 case APIC_ICR2:
748 apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
749 break;
750
751 case APIC_LVTT:
752 case APIC_LVTTHMR:
753 case APIC_LVTPC:
754 case APIC_LVT0:
755 case APIC_LVT1:
756 case APIC_LVTERR:
757 /* TODO: Check vector */
758 if (!apic_sw_enabled(apic))
759 val |= APIC_LVT_MASKED;
760
761 val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
762 apic_set_reg(apic, offset, val);
763
764 break;
765
766 case APIC_TMICT:
767 hrtimer_cancel(&apic->timer.dev);
768 apic_set_reg(apic, APIC_TMICT, val);
769 start_apic_timer(apic);
770 return;
771
772 case APIC_TDCR:
773 if (val & 4)
774 printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
775 apic_set_reg(apic, APIC_TDCR, val);
776 update_divide_count(apic);
777 break;
778
779 default:
780 apic_debug("Local APIC Write to read-only register %x\n",
781 offset);
782 break;
783 }
784
785}
786
787static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
788{
789 struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
790 int ret = 0;
791
792
793 if (apic_hw_enabled(apic) &&
794 (addr >= apic->base_address) &&
795 (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
796 ret = 1;
797
798 return ret;
799}
800
Rusty Russelld5894442007-10-08 10:48:30 +1000801void kvm_free_lapic(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +0300802{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800803 if (!vcpu->arch.apic)
Eddie Dong97222cc2007-09-12 10:58:04 +0300804 return;
805
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800806 hrtimer_cancel(&vcpu->arch.apic->timer.dev);
Eddie Dong97222cc2007-09-12 10:58:04 +0300807
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800808 if (vcpu->arch.apic->regs_page)
809 __free_page(vcpu->arch.apic->regs_page);
Eddie Dong97222cc2007-09-12 10:58:04 +0300810
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800811 kfree(vcpu->arch.apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300812}
813
814/*
815 *----------------------------------------------------------------------
816 * LAPIC interface
817 *----------------------------------------------------------------------
818 */
819
820void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
821{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800822 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300823
824 if (!apic)
825 return;
Avi Kivityb93463a2007-10-25 16:52:32 +0200826 apic_set_tpr(apic, ((cr8 & 0x0f) << 4)
827 | (apic_get_reg(apic, APIC_TASKPRI) & 4));
Eddie Dong97222cc2007-09-12 10:58:04 +0300828}
Joerg Roedelec7cf692008-04-16 16:51:16 +0200829EXPORT_SYMBOL_GPL(kvm_lapic_set_tpr);
Eddie Dong97222cc2007-09-12 10:58:04 +0300830
831u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
832{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800833 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300834 u64 tpr;
835
836 if (!apic)
837 return 0;
838 tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
839
840 return (tpr & 0xf0) >> 4;
841}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800842EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8);
Eddie Dong97222cc2007-09-12 10:58:04 +0300843
844void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
845{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800846 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300847
848 if (!apic) {
849 value |= MSR_IA32_APICBASE_BSP;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800850 vcpu->arch.apic_base = value;
Eddie Dong97222cc2007-09-12 10:58:04 +0300851 return;
852 }
853 if (apic->vcpu->vcpu_id)
854 value &= ~MSR_IA32_APICBASE_BSP;
855
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800856 vcpu->arch.apic_base = value;
857 apic->base_address = apic->vcpu->arch.apic_base &
Eddie Dong97222cc2007-09-12 10:58:04 +0300858 MSR_IA32_APICBASE_BASE;
859
860 /* with FSB delivery interrupt, we can restart APIC functionality */
861 apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800862 "0x%lx.\n", apic->vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +0300863
864}
865
866u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
867{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800868 return vcpu->arch.apic_base;
Eddie Dong97222cc2007-09-12 10:58:04 +0300869}
870EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
871
He, Qingc5ec1532007-09-03 17:07:41 +0300872void kvm_lapic_reset(struct kvm_vcpu *vcpu)
Eddie Dong97222cc2007-09-12 10:58:04 +0300873{
874 struct kvm_lapic *apic;
875 int i;
876
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800877 apic_debug("%s\n", __func__);
Eddie Dong97222cc2007-09-12 10:58:04 +0300878
879 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800880 apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300881 ASSERT(apic != NULL);
882
883 /* Stop the timer in case it's a reset to an active apic */
884 hrtimer_cancel(&apic->timer.dev);
885
886 apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
887 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
888
889 for (i = 0; i < APIC_LVT_NUM; i++)
890 apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
Qing He40487c62007-09-17 14:47:13 +0800891 apic_set_reg(apic, APIC_LVT0,
892 SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
Eddie Dong97222cc2007-09-12 10:58:04 +0300893
894 apic_set_reg(apic, APIC_DFR, 0xffffffffU);
895 apic_set_reg(apic, APIC_SPIV, 0xff);
896 apic_set_reg(apic, APIC_TASKPRI, 0);
897 apic_set_reg(apic, APIC_LDR, 0);
898 apic_set_reg(apic, APIC_ESR, 0);
899 apic_set_reg(apic, APIC_ICR, 0);
900 apic_set_reg(apic, APIC_ICR2, 0);
901 apic_set_reg(apic, APIC_TDCR, 0);
902 apic_set_reg(apic, APIC_TMICT, 0);
903 for (i = 0; i < 8; i++) {
904 apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
905 apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
906 apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
907 }
Kevin Pedrettib33ac882007-10-21 08:54:53 +0200908 update_divide_count(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +0300909 atomic_set(&apic->timer.pending, 0);
910 if (vcpu->vcpu_id == 0)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800911 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
Eddie Dong97222cc2007-09-12 10:58:04 +0300912 apic_update_ppr(apic);
913
914 apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800915 "0x%016" PRIx64 ", base_address=0x%0lx.\n", __func__,
Eddie Dong97222cc2007-09-12 10:58:04 +0300916 vcpu, kvm_apic_id(apic),
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800917 vcpu->arch.apic_base, apic->base_address);
Eddie Dong97222cc2007-09-12 10:58:04 +0300918}
He, Qingc5ec1532007-09-03 17:07:41 +0300919EXPORT_SYMBOL_GPL(kvm_lapic_reset);
Eddie Dong97222cc2007-09-12 10:58:04 +0300920
921int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
922{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800923 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +0300924 int ret = 0;
925
926 if (!apic)
927 return 0;
928 ret = apic_enabled(apic);
929
930 return ret;
931}
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800932EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
Eddie Dong97222cc2007-09-12 10:58:04 +0300933
934/*
935 *----------------------------------------------------------------------
936 * timer interface
937 *----------------------------------------------------------------------
938 */
Eddie Dong1b9778d2007-09-03 16:56:58 +0300939
940/* TODO: make sure __apic_timer_fn runs in current pCPU */
Eddie Dong97222cc2007-09-12 10:58:04 +0300941static int __apic_timer_fn(struct kvm_lapic *apic)
942{
Eddie Dong97222cc2007-09-12 10:58:04 +0300943 int result = 0;
Eddie Dong1b9778d2007-09-03 16:56:58 +0300944 wait_queue_head_t *q = &apic->vcpu->wq;
Eddie Dong97222cc2007-09-12 10:58:04 +0300945
Eddie Dong97222cc2007-09-12 10:58:04 +0300946 atomic_inc(&apic->timer.pending);
Marcelo Tosatti06e05642008-06-06 16:37:36 -0300947 set_bit(KVM_REQ_PENDING_TIMER, &apic->vcpu->requests);
Mike Dayd77c26f2007-10-08 09:02:08 -0400948 if (waitqueue_active(q)) {
Avi Kivitya4535292008-04-13 17:54:35 +0300949 apic->vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Eddie Dong1b9778d2007-09-03 16:56:58 +0300950 wake_up_interruptible(q);
He, Qingc5ec1532007-09-03 17:07:41 +0300951 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300952 if (apic_lvtt_period(apic)) {
Eddie Dong97222cc2007-09-12 10:58:04 +0300953 result = 1;
954 apic->timer.dev.expires = ktime_add_ns(
955 apic->timer.dev.expires,
956 apic->timer.period);
957 }
Eddie Dong97222cc2007-09-12 10:58:04 +0300958 return result;
959}
960
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300961int apic_has_pending_timer(struct kvm_vcpu *vcpu)
962{
963 struct kvm_lapic *lapic = vcpu->arch.apic;
964
Marcelo Tosatti54aaace2008-05-14 02:29:06 -0300965 if (lapic && apic_enabled(lapic) && apic_lvt_enabled(lapic, APIC_LVTT))
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300966 return atomic_read(&lapic->timer.pending);
967
968 return 0;
969}
970
Eddie Dong1b9778d2007-09-03 16:56:58 +0300971static int __inject_apic_timer_irq(struct kvm_lapic *apic)
972{
973 int vector;
974
975 vector = apic_lvt_vector(apic, APIC_LVTT);
976 return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
977}
978
Eddie Dong97222cc2007-09-12 10:58:04 +0300979static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
980{
981 struct kvm_lapic *apic;
982 int restart_timer = 0;
983
984 apic = container_of(data, struct kvm_lapic, timer.dev);
985
986 restart_timer = __apic_timer_fn(apic);
987
988 if (restart_timer)
989 return HRTIMER_RESTART;
990 else
991 return HRTIMER_NORESTART;
992}
993
994int kvm_create_lapic(struct kvm_vcpu *vcpu)
995{
996 struct kvm_lapic *apic;
997
998 ASSERT(vcpu != NULL);
999 apic_debug("apic_init %d\n", vcpu->vcpu_id);
1000
1001 apic = kzalloc(sizeof(*apic), GFP_KERNEL);
1002 if (!apic)
1003 goto nomem;
1004
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001005 vcpu->arch.apic = apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001006
1007 apic->regs_page = alloc_page(GFP_KERNEL);
1008 if (apic->regs_page == NULL) {
1009 printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
1010 vcpu->vcpu_id);
Rusty Russelld5894442007-10-08 10:48:30 +10001011 goto nomem_free_apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001012 }
1013 apic->regs = page_address(apic->regs_page);
1014 memset(apic->regs, 0, PAGE_SIZE);
1015 apic->vcpu = vcpu;
1016
1017 hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1018 apic->timer.dev.function = apic_timer_fn;
1019 apic->base_address = APIC_DEFAULT_PHYS_BASE;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001020 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE;
Eddie Dong97222cc2007-09-12 10:58:04 +03001021
He, Qingc5ec1532007-09-03 17:07:41 +03001022 kvm_lapic_reset(vcpu);
Eddie Dong97222cc2007-09-12 10:58:04 +03001023 apic->dev.read = apic_mmio_read;
1024 apic->dev.write = apic_mmio_write;
1025 apic->dev.in_range = apic_mmio_range;
1026 apic->dev.private = apic;
1027
1028 return 0;
Rusty Russelld5894442007-10-08 10:48:30 +10001029nomem_free_apic:
1030 kfree(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001031nomem:
Eddie Dong97222cc2007-09-12 10:58:04 +03001032 return -ENOMEM;
1033}
1034EXPORT_SYMBOL_GPL(kvm_create_lapic);
1035
1036int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
1037{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001038 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001039 int highest_irr;
1040
1041 if (!apic || !apic_enabled(apic))
1042 return -1;
1043
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001044 apic_update_ppr(apic);
Eddie Dong97222cc2007-09-12 10:58:04 +03001045 highest_irr = apic_find_highest_irr(apic);
1046 if ((highest_irr == -1) ||
1047 ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
1048 return -1;
1049 return highest_irr;
1050}
1051
Qing He40487c62007-09-17 14:47:13 +08001052int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
1053{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001054 u32 lvt0 = apic_get_reg(vcpu->arch.apic, APIC_LVT0);
Qing He40487c62007-09-17 14:47:13 +08001055 int r = 0;
1056
1057 if (vcpu->vcpu_id == 0) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001058 if (!apic_hw_enabled(vcpu->arch.apic))
Qing He40487c62007-09-17 14:47:13 +08001059 r = 1;
1060 if ((lvt0 & APIC_LVT_MASKED) == 0 &&
1061 GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
1062 r = 1;
1063 }
1064 return r;
1065}
1066
Eddie Dong1b9778d2007-09-03 16:56:58 +03001067void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
1068{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001069 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001070
1071 if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
1072 atomic_read(&apic->timer.pending) > 0) {
1073 if (__inject_apic_timer_irq(apic))
1074 atomic_dec(&apic->timer.pending);
1075 }
1076}
1077
1078void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
1079{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001080 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong1b9778d2007-09-03 16:56:58 +03001081
1082 if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
1083 apic->timer.last_update = ktime_add_ns(
1084 apic->timer.last_update,
1085 apic->timer.period);
1086}
1087
Eddie Dong97222cc2007-09-12 10:58:04 +03001088int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
1089{
1090 int vector = kvm_apic_has_interrupt(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001091 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong97222cc2007-09-12 10:58:04 +03001092
1093 if (vector == -1)
1094 return -1;
1095
1096 apic_set_vector(vector, apic->regs + APIC_ISR);
1097 apic_update_ppr(apic);
1098 apic_clear_irr(vector, apic);
1099 return vector;
1100}
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001101
1102void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
1103{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001104 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001105
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001106 apic->base_address = vcpu->arch.apic_base &
Eddie Dong96ad2cc2007-09-06 12:22:56 +03001107 MSR_IA32_APICBASE_BASE;
1108 apic_set_reg(apic, APIC_LVR, APIC_VERSION);
1109 apic_update_ppr(apic);
1110 hrtimer_cancel(&apic->timer.dev);
1111 update_divide_count(apic);
1112 start_apic_timer(apic);
1113}
Eddie Donga3d7f852007-09-03 16:15:12 +03001114
Avi Kivity2f52d582008-01-16 12:49:30 +02001115void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
Eddie Donga3d7f852007-09-03 16:15:12 +03001116{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001117 struct kvm_lapic *apic = vcpu->arch.apic;
Eddie Donga3d7f852007-09-03 16:15:12 +03001118 struct hrtimer *timer;
1119
1120 if (!apic)
1121 return;
1122
1123 timer = &apic->timer.dev;
1124 if (hrtimer_cancel(timer))
1125 hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
1126}
Avi Kivityb93463a2007-10-25 16:52:32 +02001127
1128void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu)
1129{
1130 u32 data;
1131 void *vapic;
1132
1133 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1134 return;
1135
1136 vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
1137 data = *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr));
1138 kunmap_atomic(vapic, KM_USER0);
1139
1140 apic_set_tpr(vcpu->arch.apic, data & 0xff);
1141}
1142
1143void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu)
1144{
1145 u32 data, tpr;
1146 int max_irr, max_isr;
1147 struct kvm_lapic *apic;
1148 void *vapic;
1149
1150 if (!irqchip_in_kernel(vcpu->kvm) || !vcpu->arch.apic->vapic_addr)
1151 return;
1152
1153 apic = vcpu->arch.apic;
1154 tpr = apic_get_reg(apic, APIC_TASKPRI) & 0xff;
1155 max_irr = apic_find_highest_irr(apic);
1156 if (max_irr < 0)
1157 max_irr = 0;
1158 max_isr = apic_find_highest_isr(apic);
1159 if (max_isr < 0)
1160 max_isr = 0;
1161 data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
1162
1163 vapic = kmap_atomic(vcpu->arch.apic->vapic_page, KM_USER0);
1164 *(u32 *)(vapic + offset_in_page(vcpu->arch.apic->vapic_addr)) = data;
1165 kunmap_atomic(vapic, KM_USER0);
1166}
1167
1168void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr)
1169{
1170 if (!irqchip_in_kernel(vcpu->kvm))
1171 return;
1172
1173 vcpu->arch.apic->vapic_addr = vapic_addr;
1174}