4 * Copyright (c) 2004-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>
21 #include "qemu-timer.h"
22 #include "host-utils.h"
27 //#define DEBUG_COALESCING
30 #define DPRINTF(fmt, ...) \
31 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...)
36 #ifdef DEBUG_COALESCING
37 #define DPRINTF_C(fmt, ...) \
38 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
40 #define DPRINTF_C(fmt, ...)
43 /* APIC Local Vector Table */
44 #define APIC_LVT_TIMER 0
45 #define APIC_LVT_THERMAL 1
46 #define APIC_LVT_PERFORM 2
47 #define APIC_LVT_LINT0 3
48 #define APIC_LVT_LINT1 4
49 #define APIC_LVT_ERROR 5
52 /* APIC delivery modes */
53 #define APIC_DM_FIXED 0
54 #define APIC_DM_LOWPRI 1
57 #define APIC_DM_INIT 5
58 #define APIC_DM_SIPI 6
59 #define APIC_DM_EXTINT 7
61 /* APIC destination mode */
62 #define APIC_DESTMODE_FLAT 0xf
63 #define APIC_DESTMODE_CLUSTER 1
65 #define APIC_TRIGGER_EDGE 0
66 #define APIC_TRIGGER_LEVEL 1
68 #define APIC_LVT_TIMER_PERIODIC (1<<17)
69 #define APIC_LVT_MASKED (1<<16)
70 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
71 #define APIC_LVT_REMOTE_IRR (1<<14)
72 #define APIC_INPUT_POLARITY (1<<13)
73 #define APIC_SEND_PENDING (1<<12)
75 #define ESR_ILLEGAL_ADDRESS (1 << 7)
77 #define APIC_SV_ENABLE (1 << 8)
80 #define MAX_APIC_WORDS 8
82 /* Intel APIC constants: from include/asm/msidef.h */
83 #define MSI_DATA_VECTOR_SHIFT 0
84 #define MSI_DATA_VECTOR_MASK 0x000000ff
85 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
86 #define MSI_DATA_TRIGGER_SHIFT 15
87 #define MSI_DATA_LEVEL_SHIFT 14
88 #define MSI_ADDR_DEST_MODE_SHIFT 2
89 #define MSI_ADDR_DEST_ID_SHIFT 12
90 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
92 #define MSI_ADDR_SIZE 0x100000
94 typedef struct APICState APICState
;
103 uint32_t spurious_vec
;
106 uint32_t isr
[8]; /* in service register */
107 uint32_t tmr
[8]; /* trigger mode register */
108 uint32_t irr
[8]; /* interrupt request register */
109 uint32_t lvt
[APIC_LVT_NB
];
110 uint32_t esr
; /* error register */
113 uint32_t divide_conf
;
115 uint32_t initial_count
;
116 int64_t initial_count_load_time
, next_time
;
123 static APICState
*local_apics
[MAX_APICS
+ 1];
124 static int apic_irq_delivered
;
126 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
127 static void apic_update_irq(APICState
*s
);
128 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
129 uint8_t dest
, uint8_t dest_mode
);
131 /* Find first bit starting from msb */
132 static int fls_bit(uint32_t value
)
134 return 31 - clz32(value
);
137 /* Find first bit starting from lsb */
138 static int ffs_bit(uint32_t value
)
143 static inline void set_bit(uint32_t *tab
, int index
)
147 mask
= 1 << (index
& 0x1f);
151 static inline void reset_bit(uint32_t *tab
, int index
)
155 mask
= 1 << (index
& 0x1f);
159 static inline int get_bit(uint32_t *tab
, int index
)
163 mask
= 1 << (index
& 0x1f);
164 return !!(tab
[i
] & mask
);
167 static void apic_local_deliver(APICState
*s
, int vector
)
169 uint32_t lvt
= s
->lvt
[vector
];
172 DPRINTF("%s: vector %d delivery mode %d\n", __func__
, vector
,
174 if (lvt
& APIC_LVT_MASKED
)
177 switch ((lvt
>> 8) & 7) {
179 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SMI
);
183 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_NMI
);
187 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
191 trigger_mode
= APIC_TRIGGER_EDGE
;
192 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
193 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
194 trigger_mode
= APIC_TRIGGER_LEVEL
;
195 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
199 void apic_deliver_pic_intr(DeviceState
*d
, int level
)
201 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
204 apic_local_deliver(s
, APIC_LVT_LINT0
);
206 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
208 switch ((lvt
>> 8) & 7) {
210 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
212 reset_bit(s
->irr
, lvt
& 0xff);
215 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
221 #define foreach_apic(apic, deliver_bitmask, code) \
223 int __i, __j, __mask;\
224 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
225 __mask = deliver_bitmask[__i];\
227 for(__j = 0; __j < 32; __j++) {\
228 if (__mask & (1 << __j)) {\
229 apic = local_apics[__i * 32 + __j];\
239 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
240 uint8_t delivery_mode
,
241 uint8_t vector_num
, uint8_t polarity
,
242 uint8_t trigger_mode
)
244 APICState
*apic_iter
;
246 switch (delivery_mode
) {
248 /* XXX: search for focus processor, arbitration */
252 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
253 if (deliver_bitmask
[i
]) {
254 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
259 apic_iter
= local_apics
[d
];
261 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
271 foreach_apic(apic_iter
, deliver_bitmask
,
272 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
276 foreach_apic(apic_iter
, deliver_bitmask
,
277 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
281 /* normal INIT IPI sent to processors */
282 foreach_apic(apic_iter
, deliver_bitmask
,
283 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_INIT
) );
287 /* handled in I/O APIC code */
294 foreach_apic(apic_iter
, deliver_bitmask
,
295 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
298 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
299 uint8_t delivery_mode
, uint8_t vector_num
,
300 uint8_t polarity
, uint8_t trigger_mode
)
302 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
304 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
305 " polarity %d trigger_mode %d\n", __func__
, dest
, dest_mode
,
306 delivery_mode
, vector_num
, polarity
, trigger_mode
);
307 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
308 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
312 void cpu_set_apic_base(DeviceState
*d
, uint64_t val
)
314 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
316 DPRINTF("cpu_set_apic_base: %016" PRIx64
"\n", val
);
319 if (kvm_enabled() && kvm_irqchip_in_kernel())
322 s
->apicbase
= (val
& 0xfffff000) |
323 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
324 /* if disabled, cannot be enabled again */
325 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
326 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
327 cpu_clear_apic_feature(s
->cpu_env
);
328 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
332 uint64_t cpu_get_apic_base(DeviceState
*d
)
334 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
336 DPRINTF("cpu_get_apic_base: %016" PRIx64
"\n",
337 s
? (uint64_t)s
->apicbase
: 0);
338 return s
? s
->apicbase
: 0;
341 void cpu_set_apic_tpr(DeviceState
*d
, uint8_t val
)
343 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
347 s
->tpr
= (val
& 0x0f) << 4;
351 uint8_t cpu_get_apic_tpr(DeviceState
*d
)
353 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
355 return s
? s
->tpr
>> 4 : 0;
358 /* return -1 if no bit is set */
359 static int get_highest_priority_int(uint32_t *tab
)
362 for(i
= 7; i
>= 0; i
--) {
364 return i
* 32 + fls_bit(tab
[i
]);
370 static int apic_get_ppr(APICState
*s
)
375 isrv
= get_highest_priority_int(s
->isr
);
386 static int apic_get_arb_pri(APICState
*s
)
388 /* XXX: arbitration */
392 /* signal the CPU if an irq is pending */
393 static void apic_update_irq(APICState
*s
)
396 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
398 irrv
= get_highest_priority_int(s
->irr
);
401 ppr
= apic_get_ppr(s
);
402 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
404 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
407 void apic_reset_irq_delivered(void)
409 DPRINTF_C("%s: old coalescing %d\n", __func__
, apic_irq_delivered
);
410 apic_irq_delivered
= 0;
413 int apic_get_irq_delivered(void)
415 DPRINTF_C("%s: returning coalescing %d\n", __func__
, apic_irq_delivered
);
416 return apic_irq_delivered
;
419 void apic_set_irq_delivered(void)
421 apic_irq_delivered
= 1;
424 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
426 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
427 DPRINTF_C("%s: coalescing %d\n", __func__
, apic_irq_delivered
);
429 set_bit(s
->irr
, vector_num
);
431 set_bit(s
->tmr
, vector_num
);
433 reset_bit(s
->tmr
, vector_num
);
437 static void apic_eoi(APICState
*s
)
440 isrv
= get_highest_priority_int(s
->isr
);
443 reset_bit(s
->isr
, isrv
);
444 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
445 set the remote IRR bit for level triggered interrupts. */
449 static int apic_find_dest(uint8_t dest
)
451 APICState
*apic
= local_apics
[dest
];
454 if (apic
&& apic
->id
== dest
)
455 return dest
; /* shortcut in case apic->id == apic->idx */
457 for (i
= 0; i
< MAX_APICS
; i
++) {
458 apic
= local_apics
[i
];
459 if (apic
&& apic
->id
== dest
)
466 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
467 uint8_t dest
, uint8_t dest_mode
)
469 APICState
*apic_iter
;
472 if (dest_mode
== 0) {
474 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
476 int idx
= apic_find_dest(dest
);
477 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
479 set_bit(deliver_bitmask
, idx
);
482 /* XXX: cluster mode */
483 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
484 for(i
= 0; i
< MAX_APICS
; i
++) {
485 apic_iter
= local_apics
[i
];
487 if (apic_iter
->dest_mode
== 0xf) {
488 if (dest
& apic_iter
->log_dest
)
489 set_bit(deliver_bitmask
, i
);
490 } else if (apic_iter
->dest_mode
== 0x0) {
491 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
492 (dest
& apic_iter
->log_dest
& 0x0f)) {
493 set_bit(deliver_bitmask
, i
);
501 void apic_init_reset(DeviceState
*d
)
503 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
510 s
->spurious_vec
= 0xff;
513 memset(s
->isr
, 0, sizeof(s
->isr
));
514 memset(s
->tmr
, 0, sizeof(s
->tmr
));
515 memset(s
->irr
, 0, sizeof(s
->irr
));
516 for(i
= 0; i
< APIC_LVT_NB
; i
++)
517 s
->lvt
[i
] = 1 << 16; /* mask LVT */
519 memset(s
->icr
, 0, sizeof(s
->icr
));
522 s
->initial_count
= 0;
523 s
->initial_count_load_time
= 0;
525 s
->wait_for_sipi
= 1;
528 static void apic_startup(APICState
*s
, int vector_num
)
530 s
->sipi_vector
= vector_num
;
531 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
534 void apic_sipi(DeviceState
*d
)
536 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
538 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
540 if (!s
->wait_for_sipi
)
542 cpu_x86_load_seg_cache_sipi(s
->cpu_env
, s
->sipi_vector
);
543 s
->wait_for_sipi
= 0;
546 static void apic_deliver(DeviceState
*d
, uint8_t dest
, uint8_t dest_mode
,
547 uint8_t delivery_mode
, uint8_t vector_num
,
548 uint8_t polarity
, uint8_t trigger_mode
)
550 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
551 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
552 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
553 APICState
*apic_iter
;
555 switch (dest_shorthand
) {
557 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
560 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
561 set_bit(deliver_bitmask
, s
->idx
);
564 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
567 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
568 reset_bit(deliver_bitmask
, s
->idx
);
572 switch (delivery_mode
) {
575 int trig_mode
= (s
->icr
[0] >> 15) & 1;
576 int level
= (s
->icr
[0] >> 14) & 1;
577 if (level
== 0 && trig_mode
== 1) {
578 foreach_apic(apic_iter
, deliver_bitmask
,
579 apic_iter
->arb_id
= apic_iter
->id
);
586 foreach_apic(apic_iter
, deliver_bitmask
,
587 apic_startup(apic_iter
, vector_num
) );
591 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
595 int apic_get_interrupt(DeviceState
*d
)
597 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
600 /* if the APIC is installed or enabled, we let the 8259 handle the
604 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
607 /* XXX: spurious IRQ handling */
608 intno
= get_highest_priority_int(s
->irr
);
611 if (s
->tpr
&& intno
<= s
->tpr
)
612 return s
->spurious_vec
& 0xff;
613 reset_bit(s
->irr
, intno
);
614 set_bit(s
->isr
, intno
);
619 int apic_accept_pic_intr(DeviceState
*d
)
621 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
627 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
629 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
630 (lvt0
& APIC_LVT_MASKED
) == 0)
636 static uint32_t apic_get_current_count(APICState
*s
)
640 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
642 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
644 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
646 if (d
>= s
->initial_count
)
649 val
= s
->initial_count
- d
;
654 static void apic_timer_update(APICState
*s
, int64_t current_time
)
656 int64_t next_time
, d
;
658 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
659 d
= (current_time
- s
->initial_count_load_time
) >>
661 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
662 if (!s
->initial_count
)
664 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
666 if (d
>= s
->initial_count
)
668 d
= (uint64_t)s
->initial_count
+ 1;
670 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
671 qemu_mod_timer(s
->timer
, next_time
);
672 s
->next_time
= next_time
;
675 qemu_del_timer(s
->timer
);
679 static void apic_timer(void *opaque
)
681 APICState
*s
= opaque
;
683 apic_local_deliver(s
, APIC_LVT_TIMER
);
684 apic_timer_update(s
, s
->next_time
);
687 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
692 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
697 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
701 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
705 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
712 d
= cpu_get_current_apic();
716 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
718 index
= (addr
>> 4) & 0xff;
723 case 0x03: /* version */
724 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
730 val
= apic_get_arb_pri(s
);
734 val
= apic_get_ppr(s
);
740 val
= s
->log_dest
<< 24;
743 val
= s
->dest_mode
<< 28;
746 val
= s
->spurious_vec
;
749 val
= s
->isr
[index
& 7];
752 val
= s
->tmr
[index
& 7];
755 val
= s
->irr
[index
& 7];
762 val
= s
->icr
[index
& 1];
765 val
= s
->lvt
[index
- 0x32];
768 val
= s
->initial_count
;
771 val
= apic_get_current_count(s
);
774 val
= s
->divide_conf
;
777 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
781 DPRINTF("read: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
785 static void apic_send_msi(target_phys_addr_t addr
, uint32 data
)
787 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
788 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
789 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
790 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
791 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
792 /* XXX: Ignore redirection hint. */
793 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, 0, trigger_mode
);
796 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
800 int index
= (addr
>> 4) & 0xff;
801 if (addr
> 0xfff || !index
) {
802 /* MSI and MMIO APIC are at the same memory location,
803 * but actually not on the global bus: MSI is on PCI bus
804 * APIC is connected directly to the CPU.
805 * Mapping them on the global bus happens to work because
806 * MSI registers are reserved in APIC MMIO and vice versa. */
807 apic_send_msi(addr
, val
);
811 d
= cpu_get_current_apic();
815 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
817 DPRINTF("write: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
836 s
->log_dest
= val
>> 24;
839 s
->dest_mode
= val
>> 28;
842 s
->spurious_vec
= val
& 0x1ff;
852 apic_deliver(d
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
853 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
854 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
861 int n
= index
- 0x32;
863 if (n
== APIC_LVT_TIMER
)
864 apic_timer_update(s
, qemu_get_clock(vm_clock
));
868 s
->initial_count
= val
;
869 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
870 apic_timer_update(s
, s
->initial_count_load_time
);
877 s
->divide_conf
= val
& 0xb;
878 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
879 s
->count_shift
= (v
+ 1) & 7;
883 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
888 #ifdef KVM_CAP_IRQCHIP
890 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
892 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
895 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
896 int reg_id
, uint32_t val
)
898 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
901 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
903 struct kvm_lapic_state apic
;
904 struct kvm_lapic_state
*kapic
= &apic
;
907 kvm_get_lapic(s
->cpu_env
, kapic
);
909 s
->id
= kapic_reg(kapic
, 0x2) >> 24;
910 s
->tpr
= kapic_reg(kapic
, 0x8);
911 s
->arb_id
= kapic_reg(kapic
, 0x9);
912 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
913 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
914 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
915 for (i
= 0; i
< 8; i
++) {
916 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
917 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
918 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
920 s
->esr
= kapic_reg(kapic
, 0x28);
921 s
->icr
[0] = kapic_reg(kapic
, 0x30);
922 s
->icr
[1] = kapic_reg(kapic
, 0x31);
923 for (i
= 0; i
< APIC_LVT_NB
; i
++)
924 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
925 s
->initial_count
= kapic_reg(kapic
, 0x38);
926 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
928 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
929 s
->count_shift
= (v
+ 1) & 7;
931 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
932 apic_timer_update(s
, s
->initial_count_load_time
);
935 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
937 struct kvm_lapic_state apic
;
938 struct kvm_lapic_state
*klapic
= &apic
;
941 memset(klapic
, 0, sizeof apic
);
942 kapic_set_reg(klapic
, 0x2, s
->id
<< 24);
943 kapic_set_reg(klapic
, 0x8, s
->tpr
);
944 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
945 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
946 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
947 for (i
= 0; i
< 8; i
++) {
948 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
949 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
950 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
952 kapic_set_reg(klapic
, 0x28, s
->esr
);
953 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
954 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
955 for (i
= 0; i
< APIC_LVT_NB
; i
++)
956 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
957 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
958 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
960 kvm_set_lapic(s
->cpu_env
, klapic
);
965 void kvm_load_lapic(CPUState
*env
)
967 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, env
->apic_state
);
969 #ifdef KVM_CAP_IRQCHIP
970 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
971 kvm_kernel_lapic_load_from_user(s
);
976 void kvm_save_lapic(CPUState
*env
)
978 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, env
->apic_state
);
980 #ifdef KVM_CAP_IRQCHIP
981 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
982 kvm_kernel_lapic_save_to_user(s
);
987 /* This function is only used for old state version 1 and 2 */
988 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
990 APICState
*s
= opaque
;
996 /* XXX: what if the base changes? (registered memory regions) */
997 qemu_get_be32s(f
, &s
->apicbase
);
998 qemu_get_8s(f
, &s
->id
);
999 qemu_get_8s(f
, &s
->arb_id
);
1000 qemu_get_8s(f
, &s
->tpr
);
1001 qemu_get_be32s(f
, &s
->spurious_vec
);
1002 qemu_get_8s(f
, &s
->log_dest
);
1003 qemu_get_8s(f
, &s
->dest_mode
);
1004 for (i
= 0; i
< 8; i
++) {
1005 qemu_get_be32s(f
, &s
->isr
[i
]);
1006 qemu_get_be32s(f
, &s
->tmr
[i
]);
1007 qemu_get_be32s(f
, &s
->irr
[i
]);
1009 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
1010 qemu_get_be32s(f
, &s
->lvt
[i
]);
1012 qemu_get_be32s(f
, &s
->esr
);
1013 qemu_get_be32s(f
, &s
->icr
[0]);
1014 qemu_get_be32s(f
, &s
->icr
[1]);
1015 qemu_get_be32s(f
, &s
->divide_conf
);
1016 s
->count_shift
=qemu_get_be32(f
);
1017 qemu_get_be32s(f
, &s
->initial_count
);
1018 s
->initial_count_load_time
=qemu_get_be64(f
);
1019 s
->next_time
=qemu_get_be64(f
);
1021 if (version_id
>= 2)
1022 qemu_get_timer(f
, s
->timer
);
1026 static const VMStateDescription vmstate_apic
= {
1029 .minimum_version_id
= 3,
1030 .minimum_version_id_old
= 1,
1031 .load_state_old
= apic_load_old
,
1032 .fields
= (VMStateField
[]) {
1033 VMSTATE_UINT32(apicbase
, APICState
),
1034 VMSTATE_UINT8(id
, APICState
),
1035 VMSTATE_UINT8(arb_id
, APICState
),
1036 VMSTATE_UINT8(tpr
, APICState
),
1037 VMSTATE_UINT32(spurious_vec
, APICState
),
1038 VMSTATE_UINT8(log_dest
, APICState
),
1039 VMSTATE_UINT8(dest_mode
, APICState
),
1040 VMSTATE_UINT32_ARRAY(isr
, APICState
, 8),
1041 VMSTATE_UINT32_ARRAY(tmr
, APICState
, 8),
1042 VMSTATE_UINT32_ARRAY(irr
, APICState
, 8),
1043 VMSTATE_UINT32_ARRAY(lvt
, APICState
, APIC_LVT_NB
),
1044 VMSTATE_UINT32(esr
, APICState
),
1045 VMSTATE_UINT32_ARRAY(icr
, APICState
, 2),
1046 VMSTATE_UINT32(divide_conf
, APICState
),
1047 VMSTATE_INT32(count_shift
, APICState
),
1048 VMSTATE_UINT32(initial_count
, APICState
),
1049 VMSTATE_INT64(initial_count_load_time
, APICState
),
1050 VMSTATE_INT64(next_time
, APICState
),
1051 VMSTATE_TIMER(timer
, APICState
),
1052 VMSTATE_END_OF_LIST()
1056 static void apic_reset(DeviceState
*d
)
1058 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
1061 bsp
= cpu_is_bsp(s
->cpu_env
);
1062 s
->apicbase
= 0xfee00000 |
1063 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
1069 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
1070 * time typically by BIOS, so PIC interrupt can be delivered to the
1071 * processor when local APIC is enabled.
1073 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
1077 static CPUReadMemoryFunc
* const apic_mem_read
[3] = {
1083 static CPUWriteMemoryFunc
* const apic_mem_write
[3] = {
1089 static int apic_init1(SysBusDevice
*dev
)
1091 APICState
*s
= FROM_SYSBUS(APICState
, dev
);
1093 static int last_apic_idx
;
1095 if (last_apic_idx
>= MAX_APICS
) {
1098 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
1099 apic_mem_write
, NULL
);
1100 sysbus_init_mmio(dev
, MSI_ADDR_SIZE
, apic_io_memory
);
1102 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1103 s
->idx
= last_apic_idx
++;
1104 local_apics
[s
->idx
] = s
;
1108 static SysBusDeviceInfo apic_info
= {
1110 .qdev
.name
= "apic",
1111 .qdev
.size
= sizeof(APICState
),
1112 .qdev
.vmsd
= &vmstate_apic
,
1113 .qdev
.reset
= apic_reset
,
1115 .qdev
.props
= (Property
[]) {
1116 DEFINE_PROP_UINT8("id", APICState
, id
, -1),
1117 DEFINE_PROP_PTR("cpu_env", APICState
, cpu_env
),
1118 DEFINE_PROP_END_OF_LIST(),
1122 static void apic_register_devices(void)
1124 sysbus_register_withprop(&apic_info
);
1127 device_init(apic_register_devices
)