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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22 #include "qemu-timer.h"
23 #include "host-utils.h"
28 //#define DEBUG_IOAPIC
30 /* APIC Local Vector Table */
31 #define APIC_LVT_TIMER 0
32 #define APIC_LVT_THERMAL 1
33 #define APIC_LVT_PERFORM 2
34 #define APIC_LVT_LINT0 3
35 #define APIC_LVT_LINT1 4
36 #define APIC_LVT_ERROR 5
39 /* APIC delivery modes */
40 #define APIC_DM_FIXED 0
41 #define APIC_DM_LOWPRI 1
44 #define APIC_DM_INIT 5
45 #define APIC_DM_SIPI 6
46 #define APIC_DM_EXTINT 7
48 /* APIC destination mode */
49 #define APIC_DESTMODE_FLAT 0xf
50 #define APIC_DESTMODE_CLUSTER 1
52 #define APIC_TRIGGER_EDGE 0
53 #define APIC_TRIGGER_LEVEL 1
55 #define APIC_LVT_TIMER_PERIODIC (1<<17)
56 #define APIC_LVT_MASKED (1<<16)
57 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
58 #define APIC_LVT_REMOTE_IRR (1<<14)
59 #define APIC_INPUT_POLARITY (1<<13)
60 #define APIC_SEND_PENDING (1<<12)
62 /* FIXME: it's now hard coded to be equal with KVM_IOAPIC_NUM_PINS */
63 #define IOAPIC_NUM_PINS 0x18
64 #define IOAPIC_DEFAULT_BASE_ADDRESS 0xfec00000
66 #define ESR_ILLEGAL_ADDRESS (1 << 7)
68 #define APIC_SV_ENABLE (1 << 8)
71 #define MAX_APIC_WORDS 8
73 typedef struct APICState
{
79 uint32_t spurious_vec
;
82 uint32_t isr
[8]; /* in service register */
83 uint32_t tmr
[8]; /* trigger mode register */
84 uint32_t irr
[8]; /* interrupt request register */
85 uint32_t lvt
[APIC_LVT_NB
];
86 uint32_t esr
; /* error register */
91 uint32_t initial_count
;
92 int64_t initial_count_load_time
, next_time
;
99 uint64_t base_address
;
102 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
105 static int apic_io_memory
;
106 static APICState
*local_apics
[MAX_APICS
+ 1];
107 static int last_apic_id
= 0;
108 static int apic_irq_delivered
;
111 static void apic_init_ipi(APICState
*s
);
112 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
113 static void apic_update_irq(APICState
*s
);
115 /* Find first bit starting from msb */
116 static int fls_bit(uint32_t value
)
118 return 31 - clz32(value
);
121 /* Find first bit starting from lsb */
122 static int ffs_bit(uint32_t value
)
127 static inline void set_bit(uint32_t *tab
, int index
)
131 mask
= 1 << (index
& 0x1f);
135 static inline void reset_bit(uint32_t *tab
, int index
)
139 mask
= 1 << (index
& 0x1f);
143 static inline int get_bit(uint32_t *tab
, int index
)
147 mask
= 1 << (index
& 0x1f);
148 return !!(tab
[i
] & mask
);
151 static void apic_local_deliver(CPUState
*env
, int vector
)
153 APICState
*s
= env
->apic_state
;
154 uint32_t lvt
= s
->lvt
[vector
];
157 if (lvt
& APIC_LVT_MASKED
)
160 switch ((lvt
>> 8) & 7) {
162 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
166 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
170 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
174 trigger_mode
= APIC_TRIGGER_EDGE
;
175 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
176 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
177 trigger_mode
= APIC_TRIGGER_LEVEL
;
178 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
182 void apic_deliver_pic_intr(CPUState
*env
, int level
)
185 apic_local_deliver(env
, APIC_LVT_LINT0
);
187 APICState
*s
= env
->apic_state
;
188 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
190 switch ((lvt
>> 8) & 7) {
192 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
194 reset_bit(s
->irr
, lvt
& 0xff);
197 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
203 #define foreach_apic(apic, deliver_bitmask, code) \
205 int __i, __j, __mask;\
206 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
207 __mask = deliver_bitmask[__i];\
209 for(__j = 0; __j < 32; __j++) {\
210 if (__mask & (1 << __j)) {\
211 apic = local_apics[__i * 32 + __j];\
221 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
222 uint8_t delivery_mode
,
223 uint8_t vector_num
, uint8_t polarity
,
224 uint8_t trigger_mode
)
226 APICState
*apic_iter
;
228 switch (delivery_mode
) {
230 /* XXX: search for focus processor, arbitration */
234 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
235 if (deliver_bitmask
[i
]) {
236 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
241 apic_iter
= local_apics
[d
];
243 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
253 foreach_apic(apic_iter
, deliver_bitmask
,
254 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
258 foreach_apic(apic_iter
, deliver_bitmask
,
259 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
263 /* normal INIT IPI sent to processors */
264 foreach_apic(apic_iter
, deliver_bitmask
,
265 apic_init_ipi(apic_iter
) );
269 /* handled in I/O APIC code */
276 foreach_apic(apic_iter
, deliver_bitmask
,
277 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
280 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
282 APICState
*s
= env
->apic_state
;
284 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
286 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel())
289 s
->apicbase
= (val
& 0xfffff000) |
290 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
291 /* if disabled, cannot be enabled again */
292 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
293 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
294 env
->cpuid_features
&= ~CPUID_APIC
;
295 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
299 uint64_t cpu_get_apic_base(CPUState
*env
)
301 APICState
*s
= env
->apic_state
;
303 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
308 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
310 APICState
*s
= env
->apic_state
;
311 s
->tpr
= (val
& 0x0f) << 4;
315 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
317 APICState
*s
= env
->apic_state
;
321 /* return -1 if no bit is set */
322 static int get_highest_priority_int(uint32_t *tab
)
325 for(i
= 7; i
>= 0; i
--) {
327 return i
* 32 + fls_bit(tab
[i
]);
333 static int apic_get_ppr(APICState
*s
)
338 isrv
= get_highest_priority_int(s
->isr
);
349 static int apic_get_arb_pri(APICState
*s
)
351 /* XXX: arbitration */
355 /* signal the CPU if an irq is pending */
356 static void apic_update_irq(APICState
*s
)
359 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
361 irrv
= get_highest_priority_int(s
->irr
);
364 ppr
= apic_get_ppr(s
);
365 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
367 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
370 void apic_reset_irq_delivered(void)
372 apic_irq_delivered
= 0;
375 int apic_get_irq_delivered(void)
377 return apic_irq_delivered
;
380 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
382 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
384 set_bit(s
->irr
, vector_num
);
386 set_bit(s
->tmr
, vector_num
);
388 reset_bit(s
->tmr
, vector_num
);
392 static void apic_eoi(APICState
*s
)
395 isrv
= get_highest_priority_int(s
->isr
);
398 reset_bit(s
->isr
, isrv
);
399 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
400 set the remote IRR bit for level triggered interrupts. */
404 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
405 uint8_t dest
, uint8_t dest_mode
)
407 APICState
*apic_iter
;
410 if (dest_mode
== 0) {
412 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
414 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
415 set_bit(deliver_bitmask
, dest
);
418 /* XXX: cluster mode */
419 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
420 for(i
= 0; i
< MAX_APICS
; i
++) {
421 apic_iter
= local_apics
[i
];
423 if (apic_iter
->dest_mode
== 0xf) {
424 if (dest
& apic_iter
->log_dest
)
425 set_bit(deliver_bitmask
, i
);
426 } else if (apic_iter
->dest_mode
== 0x0) {
427 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
428 (dest
& apic_iter
->log_dest
& 0x0f)) {
429 set_bit(deliver_bitmask
, i
);
438 static void apic_init_ipi(APICState
*s
)
443 s
->spurious_vec
= 0xff;
446 memset(s
->isr
, 0, sizeof(s
->isr
));
447 memset(s
->tmr
, 0, sizeof(s
->tmr
));
448 memset(s
->irr
, 0, sizeof(s
->irr
));
449 for(i
= 0; i
< APIC_LVT_NB
; i
++)
450 s
->lvt
[i
] = 1 << 16; /* mask LVT */
452 memset(s
->icr
, 0, sizeof(s
->icr
));
455 s
->initial_count
= 0;
456 s
->initial_count_load_time
= 0;
459 cpu_reset(s
->cpu_env
);
461 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
) &&
462 (!kvm_enabled() || !qemu_kvm_irqchip_in_kernel()))
463 s
->cpu_env
->halted
= 1;
465 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
467 kvm_apic_init(s
->cpu_env
);
470 /* send a SIPI message to the CPU to start it */
471 static void apic_startup(APICState
*s
, int vector_num
)
473 CPUState
*env
= s
->cpu_env
;
477 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
480 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
481 kvm_update_after_sipi(env
);
484 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
485 uint8_t delivery_mode
, uint8_t vector_num
,
486 uint8_t polarity
, uint8_t trigger_mode
)
488 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
489 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
490 APICState
*apic_iter
;
492 switch (dest_shorthand
) {
494 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
497 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
498 set_bit(deliver_bitmask
, s
->id
);
501 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
504 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
505 reset_bit(deliver_bitmask
, s
->id
);
509 switch (delivery_mode
) {
512 int trig_mode
= (s
->icr
[0] >> 15) & 1;
513 int level
= (s
->icr
[0] >> 14) & 1;
514 if (level
== 0 && trig_mode
== 1) {
515 foreach_apic(apic_iter
, deliver_bitmask
,
516 apic_iter
->arb_id
= apic_iter
->id
);
523 foreach_apic(apic_iter
, deliver_bitmask
,
524 apic_startup(apic_iter
, vector_num
) );
528 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
532 int apic_get_interrupt(CPUState
*env
)
534 APICState
*s
= env
->apic_state
;
537 /* if the APIC is installed or enabled, we let the 8259 handle the
541 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
544 /* XXX: spurious IRQ handling */
545 intno
= get_highest_priority_int(s
->irr
);
548 if (s
->tpr
&& intno
<= s
->tpr
)
549 return s
->spurious_vec
& 0xff;
550 reset_bit(s
->irr
, intno
);
551 set_bit(s
->isr
, intno
);
556 int apic_accept_pic_intr(CPUState
*env
)
558 APICState
*s
= env
->apic_state
;
564 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
566 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
567 (lvt0
& APIC_LVT_MASKED
) == 0)
573 static uint32_t apic_get_current_count(APICState
*s
)
577 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
579 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
581 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
583 if (d
>= s
->initial_count
)
586 val
= s
->initial_count
- d
;
591 static void apic_timer_update(APICState
*s
, int64_t current_time
)
593 int64_t next_time
, d
;
595 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
596 d
= (current_time
- s
->initial_count_load_time
) >>
598 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
599 if (!s
->initial_count
)
601 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
603 if (d
>= s
->initial_count
)
605 d
= (uint64_t)s
->initial_count
+ 1;
607 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
608 qemu_mod_timer(s
->timer
, next_time
);
609 s
->next_time
= next_time
;
612 qemu_del_timer(s
->timer
);
616 static void apic_timer(void *opaque
)
618 APICState
*s
= opaque
;
620 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
621 apic_timer_update(s
, s
->next_time
);
624 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
629 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
634 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
638 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
642 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
649 env
= cpu_single_env
;
654 index
= (addr
>> 4) & 0xff;
659 case 0x03: /* version */
660 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
666 val
= apic_get_arb_pri(s
);
670 val
= apic_get_ppr(s
);
676 val
= s
->log_dest
<< 24;
679 val
= s
->dest_mode
<< 28;
682 val
= s
->spurious_vec
;
685 val
= s
->isr
[index
& 7];
688 val
= s
->tmr
[index
& 7];
691 val
= s
->irr
[index
& 7];
698 val
= s
->icr
[index
& 1];
701 val
= s
->lvt
[index
- 0x32];
704 val
= s
->initial_count
;
707 val
= apic_get_current_count(s
);
710 val
= s
->divide_conf
;
713 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
718 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
723 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
729 env
= cpu_single_env
;
735 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
738 index
= (addr
>> 4) & 0xff;
756 s
->log_dest
= val
>> 24;
759 s
->dest_mode
= val
>> 28;
762 s
->spurious_vec
= val
& 0x1ff;
772 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
773 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
774 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
781 int n
= index
- 0x32;
783 if (n
== APIC_LVT_TIMER
)
784 apic_timer_update(s
, qemu_get_clock(vm_clock
));
788 s
->initial_count
= val
;
789 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
790 apic_timer_update(s
, s
->initial_count_load_time
);
797 s
->divide_conf
= val
& 0xb;
798 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
799 s
->count_shift
= (v
+ 1) & 7;
803 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
808 #ifdef KVM_CAP_IRQCHIP
810 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
812 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
815 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
816 int reg_id
, uint32_t val
)
818 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
821 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
823 struct kvm_lapic_state apic
;
824 struct kvm_lapic_state
*kapic
= &apic
;
827 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
829 s
->id
= kapic_reg(kapic
, 0x2);
830 s
->tpr
= kapic_reg(kapic
, 0x8);
831 s
->arb_id
= kapic_reg(kapic
, 0x9);
832 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
833 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
834 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
835 for (i
= 0; i
< 8; i
++) {
836 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
837 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
838 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
840 s
->esr
= kapic_reg(kapic
, 0x28);
841 s
->icr
[0] = kapic_reg(kapic
, 0x30);
842 s
->icr
[1] = kapic_reg(kapic
, 0x31);
843 for (i
= 0; i
< APIC_LVT_NB
; i
++)
844 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
845 s
->initial_count
= kapic_reg(kapic
, 0x38);
846 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
848 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
849 s
->count_shift
= (v
+ 1) & 7;
851 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
852 apic_timer_update(s
, s
->initial_count_load_time
);
855 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
857 struct kvm_lapic_state apic
;
858 struct kvm_lapic_state
*klapic
= &apic
;
861 memset(klapic
, 0, sizeof apic
);
862 kapic_set_reg(klapic
, 0x2, s
->id
);
863 kapic_set_reg(klapic
, 0x8, s
->tpr
);
864 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
865 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
866 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
867 for (i
= 0; i
< 8; i
++) {
868 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
869 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
870 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
872 kapic_set_reg(klapic
, 0x28, s
->esr
);
873 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
874 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
875 for (i
= 0; i
< APIC_LVT_NB
; i
++)
876 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
877 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
878 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
880 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
885 static void apic_save(QEMUFile
*f
, void *opaque
)
887 APICState
*s
= opaque
;
890 #ifdef KVM_CAP_IRQCHIP
891 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
892 kvm_kernel_lapic_save_to_user(s
);
896 qemu_put_be32s(f
, &s
->apicbase
);
897 qemu_put_8s(f
, &s
->id
);
898 qemu_put_8s(f
, &s
->arb_id
);
899 qemu_put_8s(f
, &s
->tpr
);
900 qemu_put_be32s(f
, &s
->spurious_vec
);
901 qemu_put_8s(f
, &s
->log_dest
);
902 qemu_put_8s(f
, &s
->dest_mode
);
903 for (i
= 0; i
< 8; i
++) {
904 qemu_put_be32s(f
, &s
->isr
[i
]);
905 qemu_put_be32s(f
, &s
->tmr
[i
]);
906 qemu_put_be32s(f
, &s
->irr
[i
]);
908 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
909 qemu_put_be32s(f
, &s
->lvt
[i
]);
911 qemu_put_be32s(f
, &s
->esr
);
912 qemu_put_be32s(f
, &s
->icr
[0]);
913 qemu_put_be32s(f
, &s
->icr
[1]);
914 qemu_put_be32s(f
, &s
->divide_conf
);
915 qemu_put_be32(f
, s
->count_shift
);
916 qemu_put_be32s(f
, &s
->initial_count
);
917 qemu_put_be64(f
, s
->initial_count_load_time
);
918 qemu_put_be64(f
, s
->next_time
);
920 qemu_put_timer(f
, s
->timer
);
923 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
925 APICState
*s
= opaque
;
931 /* XXX: what if the base changes? (registered memory regions) */
932 qemu_get_be32s(f
, &s
->apicbase
);
933 qemu_get_8s(f
, &s
->id
);
934 qemu_get_8s(f
, &s
->arb_id
);
935 qemu_get_8s(f
, &s
->tpr
);
936 qemu_get_be32s(f
, &s
->spurious_vec
);
937 qemu_get_8s(f
, &s
->log_dest
);
938 qemu_get_8s(f
, &s
->dest_mode
);
939 for (i
= 0; i
< 8; i
++) {
940 qemu_get_be32s(f
, &s
->isr
[i
]);
941 qemu_get_be32s(f
, &s
->tmr
[i
]);
942 qemu_get_be32s(f
, &s
->irr
[i
]);
944 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
945 qemu_get_be32s(f
, &s
->lvt
[i
]);
947 qemu_get_be32s(f
, &s
->esr
);
948 qemu_get_be32s(f
, &s
->icr
[0]);
949 qemu_get_be32s(f
, &s
->icr
[1]);
950 qemu_get_be32s(f
, &s
->divide_conf
);
951 s
->count_shift
=qemu_get_be32(f
);
952 qemu_get_be32s(f
, &s
->initial_count
);
953 s
->initial_count_load_time
=qemu_get_be64(f
);
954 s
->next_time
=qemu_get_be64(f
);
957 qemu_get_timer(f
, s
->timer
);
959 #ifdef KVM_CAP_IRQCHIP
960 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
961 kvm_kernel_lapic_load_from_user(s
);
968 static void apic_reset(void *opaque
)
970 APICState
*s
= opaque
;
972 s
->apicbase
= 0xfee00000 |
973 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
979 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
980 * time typically by BIOS, so PIC interrupt can be delivered to the
981 * processor when local APIC is enabled.
983 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
985 #ifdef KVM_CAP_IRQCHIP
986 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
987 kvm_kernel_lapic_load_from_user(s
);
992 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
998 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
1004 int apic_init(CPUState
*env
)
1008 if (last_apic_id
>= MAX_APICS
)
1010 s
= qemu_mallocz(sizeof(APICState
));
1013 env
->apic_state
= s
;
1014 s
->id
= last_apic_id
++;
1015 env
->cpuid_apic_id
= s
->id
;
1020 /* XXX: mapping more APICs at the same memory location */
1021 if (apic_io_memory
== 0) {
1022 /* NOTE: the APIC is directly connected to the CPU - it is not
1023 on the global memory bus. */
1024 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
1025 apic_mem_write
, NULL
);
1026 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1029 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1031 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
1032 qemu_register_reset(apic_reset
, s
);
1034 local_apics
[s
->id
] = s
;
1038 static void ioapic_service(IOAPICState
*s
)
1043 uint8_t delivery_mode
;
1049 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
1051 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1053 if (s
->irr
& mask
) {
1054 entry
= s
->ioredtbl
[i
];
1055 if (!(entry
& APIC_LVT_MASKED
)) {
1056 trig_mode
= ((entry
>> 15) & 1);
1058 dest_mode
= (entry
>> 11) & 1;
1059 delivery_mode
= (entry
>> 8) & 7;
1060 polarity
= (entry
>> 13) & 1;
1061 if (trig_mode
== APIC_TRIGGER_EDGE
)
1063 if (delivery_mode
== APIC_DM_EXTINT
)
1064 vector
= pic_read_irq(isa_pic
);
1066 vector
= entry
& 0xff;
1068 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
1069 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
1070 vector
, polarity
, trig_mode
);
1076 void ioapic_set_irq(void *opaque
, int vector
, int level
)
1078 IOAPICState
*s
= opaque
;
1081 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
1082 * to GSI 2. GSI maps to ioapic 1-1. This is not
1083 * the cleanest way of doing it but it should work. */
1089 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
1090 uint32_t mask
= 1 << vector
;
1091 uint64_t entry
= s
->ioredtbl
[vector
];
1093 if ((entry
>> 15) & 1) {
1094 /* level triggered */
1102 /* edge triggered */
1111 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1113 IOAPICState
*s
= opaque
;
1120 } else if (addr
== 0x10) {
1121 switch (s
->ioregsel
) {
1126 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1132 index
= (s
->ioregsel
- 0x10) >> 1;
1133 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1134 if (s
->ioregsel
& 1)
1135 val
= s
->ioredtbl
[index
] >> 32;
1137 val
= s
->ioredtbl
[index
] & 0xffffffff;
1141 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1147 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1149 IOAPICState
*s
= opaque
;
1156 } else if (addr
== 0x10) {
1158 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1160 switch (s
->ioregsel
) {
1162 s
->id
= (val
>> 24) & 0xff;
1168 index
= (s
->ioregsel
- 0x10) >> 1;
1169 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1170 if (s
->ioregsel
& 1) {
1171 s
->ioredtbl
[index
] &= 0xffffffff;
1172 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1174 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1175 s
->ioredtbl
[index
] |= val
;
1183 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
1185 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1186 struct kvm_irqchip chip
;
1187 struct kvm_ioapic_state
*kioapic
;
1190 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1191 kvm_get_irqchip(kvm_context
, &chip
);
1192 kioapic
= &chip
.chip
.ioapic
;
1194 s
->id
= kioapic
->id
;
1195 s
->ioregsel
= kioapic
->ioregsel
;
1196 s
->base_address
= kioapic
->base_address
;
1197 s
->irr
= kioapic
->irr
;
1198 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1199 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
1204 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
1206 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1207 struct kvm_irqchip chip
;
1208 struct kvm_ioapic_state
*kioapic
;
1211 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1212 kioapic
= &chip
.chip
.ioapic
;
1214 kioapic
->id
= s
->id
;
1215 kioapic
->ioregsel
= s
->ioregsel
;
1216 kioapic
->base_address
= s
->base_address
;
1217 kioapic
->irr
= s
->irr
;
1218 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1219 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
1222 kvm_set_irqchip(kvm_context
, &chip
);
1226 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1228 IOAPICState
*s
= opaque
;
1231 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1232 kvm_kernel_ioapic_save_to_user(s
);
1235 qemu_put_8s(f
, &s
->id
);
1236 qemu_put_8s(f
, &s
->ioregsel
);
1237 qemu_put_be64s(f
, &s
->base_address
);
1238 qemu_put_be32s(f
, &s
->irr
);
1239 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1240 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1244 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1246 IOAPICState
*s
= opaque
;
1249 if (version_id
< 1 || version_id
> 2)
1252 qemu_get_8s(f
, &s
->id
);
1253 qemu_get_8s(f
, &s
->ioregsel
);
1254 if (version_id
== 2) {
1255 /* for version 2, we get this data off of the wire */
1256 qemu_get_be64s(f
, &s
->base_address
);
1257 qemu_get_be32s(f
, &s
->irr
);
1260 /* in case we are doing version 1, we just set these to sane values */
1261 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1264 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1265 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1268 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1269 kvm_kernel_ioapic_load_from_user(s
);
1275 static void ioapic_reset(void *opaque
)
1277 IOAPICState
*s
= opaque
;
1280 memset(s
, 0, sizeof(*s
));
1281 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1282 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1283 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1284 #ifdef KVM_CAP_IRQCHIP
1285 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1286 kvm_kernel_ioapic_load_from_user(s
);
1291 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1297 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1303 IOAPICState
*ioapic_init(void)
1308 s
= qemu_mallocz(sizeof(IOAPICState
));
1312 s
->id
= last_apic_id
++;
1314 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1315 ioapic_mem_write
, s
);
1316 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1318 register_savevm("ioapic", 0, 2, ioapic_save
, ioapic_load
, s
);
1319 qemu_register_reset(ioapic_reset
, s
);