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 void apic_set_irq_delivered(void)
382 apic_irq_delivered
= 1;
385 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
387 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
389 set_bit(s
->irr
, vector_num
);
391 set_bit(s
->tmr
, vector_num
);
393 reset_bit(s
->tmr
, vector_num
);
397 static void apic_eoi(APICState
*s
)
400 isrv
= get_highest_priority_int(s
->isr
);
403 reset_bit(s
->isr
, isrv
);
404 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
405 set the remote IRR bit for level triggered interrupts. */
409 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
410 uint8_t dest
, uint8_t dest_mode
)
412 APICState
*apic_iter
;
415 if (dest_mode
== 0) {
417 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
419 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
420 set_bit(deliver_bitmask
, dest
);
423 /* XXX: cluster mode */
424 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
425 for(i
= 0; i
< MAX_APICS
; i
++) {
426 apic_iter
= local_apics
[i
];
428 if (apic_iter
->dest_mode
== 0xf) {
429 if (dest
& apic_iter
->log_dest
)
430 set_bit(deliver_bitmask
, i
);
431 } else if (apic_iter
->dest_mode
== 0x0) {
432 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
433 (dest
& apic_iter
->log_dest
& 0x0f)) {
434 set_bit(deliver_bitmask
, i
);
443 static void apic_init_ipi(APICState
*s
)
448 s
->spurious_vec
= 0xff;
451 memset(s
->isr
, 0, sizeof(s
->isr
));
452 memset(s
->tmr
, 0, sizeof(s
->tmr
));
453 memset(s
->irr
, 0, sizeof(s
->irr
));
454 for(i
= 0; i
< APIC_LVT_NB
; i
++)
455 s
->lvt
[i
] = 1 << 16; /* mask LVT */
457 memset(s
->icr
, 0, sizeof(s
->icr
));
460 s
->initial_count
= 0;
461 s
->initial_count_load_time
= 0;
464 cpu_reset(s
->cpu_env
);
466 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
) &&
467 (!kvm_enabled() || !qemu_kvm_irqchip_in_kernel()))
468 s
->cpu_env
->halted
= 1;
470 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
472 kvm_apic_init(s
->cpu_env
);
475 /* send a SIPI message to the CPU to start it */
476 static void apic_startup(APICState
*s
, int vector_num
)
478 CPUState
*env
= s
->cpu_env
;
482 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
485 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
486 kvm_update_after_sipi(env
);
489 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
490 uint8_t delivery_mode
, uint8_t vector_num
,
491 uint8_t polarity
, uint8_t trigger_mode
)
493 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
494 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
495 APICState
*apic_iter
;
497 switch (dest_shorthand
) {
499 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
502 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
503 set_bit(deliver_bitmask
, s
->id
);
506 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
509 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
510 reset_bit(deliver_bitmask
, s
->id
);
514 switch (delivery_mode
) {
517 int trig_mode
= (s
->icr
[0] >> 15) & 1;
518 int level
= (s
->icr
[0] >> 14) & 1;
519 if (level
== 0 && trig_mode
== 1) {
520 foreach_apic(apic_iter
, deliver_bitmask
,
521 apic_iter
->arb_id
= apic_iter
->id
);
528 foreach_apic(apic_iter
, deliver_bitmask
,
529 apic_startup(apic_iter
, vector_num
) );
533 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
537 int apic_get_interrupt(CPUState
*env
)
539 APICState
*s
= env
->apic_state
;
542 /* if the APIC is installed or enabled, we let the 8259 handle the
546 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
549 /* XXX: spurious IRQ handling */
550 intno
= get_highest_priority_int(s
->irr
);
553 if (s
->tpr
&& intno
<= s
->tpr
)
554 return s
->spurious_vec
& 0xff;
555 reset_bit(s
->irr
, intno
);
556 set_bit(s
->isr
, intno
);
561 int apic_accept_pic_intr(CPUState
*env
)
563 APICState
*s
= env
->apic_state
;
569 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
571 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
572 (lvt0
& APIC_LVT_MASKED
) == 0)
578 static uint32_t apic_get_current_count(APICState
*s
)
582 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
584 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
586 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
588 if (d
>= s
->initial_count
)
591 val
= s
->initial_count
- d
;
596 static void apic_timer_update(APICState
*s
, int64_t current_time
)
598 int64_t next_time
, d
;
600 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
601 d
= (current_time
- s
->initial_count_load_time
) >>
603 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
604 if (!s
->initial_count
)
606 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
608 if (d
>= s
->initial_count
)
610 d
= (uint64_t)s
->initial_count
+ 1;
612 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
613 qemu_mod_timer(s
->timer
, next_time
);
614 s
->next_time
= next_time
;
617 qemu_del_timer(s
->timer
);
621 static void apic_timer(void *opaque
)
623 APICState
*s
= opaque
;
625 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
626 apic_timer_update(s
, s
->next_time
);
629 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
634 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
639 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
643 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
647 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
654 env
= cpu_single_env
;
659 index
= (addr
>> 4) & 0xff;
664 case 0x03: /* version */
665 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
671 val
= apic_get_arb_pri(s
);
675 val
= apic_get_ppr(s
);
681 val
= s
->log_dest
<< 24;
684 val
= s
->dest_mode
<< 28;
687 val
= s
->spurious_vec
;
690 val
= s
->isr
[index
& 7];
693 val
= s
->tmr
[index
& 7];
696 val
= s
->irr
[index
& 7];
703 val
= s
->icr
[index
& 1];
706 val
= s
->lvt
[index
- 0x32];
709 val
= s
->initial_count
;
712 val
= apic_get_current_count(s
);
715 val
= s
->divide_conf
;
718 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
723 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
728 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
734 env
= cpu_single_env
;
740 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
743 index
= (addr
>> 4) & 0xff;
761 s
->log_dest
= val
>> 24;
764 s
->dest_mode
= val
>> 28;
767 s
->spurious_vec
= val
& 0x1ff;
777 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
778 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
779 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
786 int n
= index
- 0x32;
788 if (n
== APIC_LVT_TIMER
)
789 apic_timer_update(s
, qemu_get_clock(vm_clock
));
793 s
->initial_count
= val
;
794 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
795 apic_timer_update(s
, s
->initial_count_load_time
);
802 s
->divide_conf
= val
& 0xb;
803 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
804 s
->count_shift
= (v
+ 1) & 7;
808 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
813 #ifdef KVM_CAP_IRQCHIP
815 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
817 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
820 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
821 int reg_id
, uint32_t val
)
823 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
826 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
828 struct kvm_lapic_state apic
;
829 struct kvm_lapic_state
*kapic
= &apic
;
832 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
834 s
->id
= kapic_reg(kapic
, 0x2) >> 24;
835 s
->tpr
= kapic_reg(kapic
, 0x8);
836 s
->arb_id
= kapic_reg(kapic
, 0x9);
837 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
838 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
839 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
840 for (i
= 0; i
< 8; i
++) {
841 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
842 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
843 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
845 s
->esr
= kapic_reg(kapic
, 0x28);
846 s
->icr
[0] = kapic_reg(kapic
, 0x30);
847 s
->icr
[1] = kapic_reg(kapic
, 0x31);
848 for (i
= 0; i
< APIC_LVT_NB
; i
++)
849 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
850 s
->initial_count
= kapic_reg(kapic
, 0x38);
851 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
853 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
854 s
->count_shift
= (v
+ 1) & 7;
856 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
857 apic_timer_update(s
, s
->initial_count_load_time
);
860 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
862 struct kvm_lapic_state apic
;
863 struct kvm_lapic_state
*klapic
= &apic
;
866 memset(klapic
, 0, sizeof apic
);
867 kapic_set_reg(klapic
, 0x2, s
->id
<< 24);
868 kapic_set_reg(klapic
, 0x8, s
->tpr
);
869 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
870 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
871 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
872 for (i
= 0; i
< 8; i
++) {
873 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
874 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
875 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
877 kapic_set_reg(klapic
, 0x28, s
->esr
);
878 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
879 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
880 for (i
= 0; i
< APIC_LVT_NB
; i
++)
881 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
882 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
883 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
885 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
890 static void apic_save(QEMUFile
*f
, void *opaque
)
892 APICState
*s
= opaque
;
895 #ifdef KVM_CAP_IRQCHIP
896 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
897 kvm_kernel_lapic_save_to_user(s
);
901 qemu_put_be32s(f
, &s
->apicbase
);
902 qemu_put_8s(f
, &s
->id
);
903 qemu_put_8s(f
, &s
->arb_id
);
904 qemu_put_8s(f
, &s
->tpr
);
905 qemu_put_be32s(f
, &s
->spurious_vec
);
906 qemu_put_8s(f
, &s
->log_dest
);
907 qemu_put_8s(f
, &s
->dest_mode
);
908 for (i
= 0; i
< 8; i
++) {
909 qemu_put_be32s(f
, &s
->isr
[i
]);
910 qemu_put_be32s(f
, &s
->tmr
[i
]);
911 qemu_put_be32s(f
, &s
->irr
[i
]);
913 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
914 qemu_put_be32s(f
, &s
->lvt
[i
]);
916 qemu_put_be32s(f
, &s
->esr
);
917 qemu_put_be32s(f
, &s
->icr
[0]);
918 qemu_put_be32s(f
, &s
->icr
[1]);
919 qemu_put_be32s(f
, &s
->divide_conf
);
920 qemu_put_be32(f
, s
->count_shift
);
921 qemu_put_be32s(f
, &s
->initial_count
);
922 qemu_put_be64(f
, s
->initial_count_load_time
);
923 qemu_put_be64(f
, s
->next_time
);
925 qemu_put_timer(f
, s
->timer
);
928 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
930 APICState
*s
= opaque
;
936 /* XXX: what if the base changes? (registered memory regions) */
937 qemu_get_be32s(f
, &s
->apicbase
);
938 qemu_get_8s(f
, &s
->id
);
939 qemu_get_8s(f
, &s
->arb_id
);
940 qemu_get_8s(f
, &s
->tpr
);
941 qemu_get_be32s(f
, &s
->spurious_vec
);
942 qemu_get_8s(f
, &s
->log_dest
);
943 qemu_get_8s(f
, &s
->dest_mode
);
944 for (i
= 0; i
< 8; i
++) {
945 qemu_get_be32s(f
, &s
->isr
[i
]);
946 qemu_get_be32s(f
, &s
->tmr
[i
]);
947 qemu_get_be32s(f
, &s
->irr
[i
]);
949 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
950 qemu_get_be32s(f
, &s
->lvt
[i
]);
952 qemu_get_be32s(f
, &s
->esr
);
953 qemu_get_be32s(f
, &s
->icr
[0]);
954 qemu_get_be32s(f
, &s
->icr
[1]);
955 qemu_get_be32s(f
, &s
->divide_conf
);
956 s
->count_shift
=qemu_get_be32(f
);
957 qemu_get_be32s(f
, &s
->initial_count
);
958 s
->initial_count_load_time
=qemu_get_be64(f
);
959 s
->next_time
=qemu_get_be64(f
);
962 qemu_get_timer(f
, s
->timer
);
964 #ifdef KVM_CAP_IRQCHIP
965 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
966 kvm_kernel_lapic_load_from_user(s
);
973 static void apic_reset(void *opaque
)
975 APICState
*s
= opaque
;
977 s
->apicbase
= 0xfee00000 |
978 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
984 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
985 * time typically by BIOS, so PIC interrupt can be delivered to the
986 * processor when local APIC is enabled.
988 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
990 #ifdef KVM_CAP_IRQCHIP
991 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
992 kvm_kernel_lapic_load_from_user(s
);
997 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
1003 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
1009 int apic_init(CPUState
*env
)
1013 if (last_apic_id
>= MAX_APICS
)
1015 s
= qemu_mallocz(sizeof(APICState
));
1016 env
->apic_state
= s
;
1017 s
->id
= last_apic_id
++;
1018 env
->cpuid_apic_id
= s
->id
;
1023 /* XXX: mapping more APICs at the same memory location */
1024 if (apic_io_memory
== 0) {
1025 /* NOTE: the APIC is directly connected to the CPU - it is not
1026 on the global memory bus. */
1027 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
1028 apic_mem_write
, NULL
);
1029 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1032 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1034 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
1035 qemu_register_reset(apic_reset
, s
);
1037 local_apics
[s
->id
] = s
;
1041 static void ioapic_service(IOAPICState
*s
)
1046 uint8_t delivery_mode
;
1052 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
1054 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1056 if (s
->irr
& mask
) {
1057 entry
= s
->ioredtbl
[i
];
1058 if (!(entry
& APIC_LVT_MASKED
)) {
1059 trig_mode
= ((entry
>> 15) & 1);
1061 dest_mode
= (entry
>> 11) & 1;
1062 delivery_mode
= (entry
>> 8) & 7;
1063 polarity
= (entry
>> 13) & 1;
1064 if (trig_mode
== APIC_TRIGGER_EDGE
)
1066 if (delivery_mode
== APIC_DM_EXTINT
)
1067 vector
= pic_read_irq(isa_pic
);
1069 vector
= entry
& 0xff;
1071 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
1072 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
1073 vector
, polarity
, trig_mode
);
1079 void ioapic_set_irq(void *opaque
, int vector
, int level
)
1081 IOAPICState
*s
= opaque
;
1084 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
1085 * to GSI 2. GSI maps to ioapic 1-1. This is not
1086 * the cleanest way of doing it but it should work. */
1092 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
1093 uint32_t mask
= 1 << vector
;
1094 uint64_t entry
= s
->ioredtbl
[vector
];
1096 if ((entry
>> 15) & 1) {
1097 /* level triggered */
1105 /* edge triggered */
1114 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1116 IOAPICState
*s
= opaque
;
1123 } else if (addr
== 0x10) {
1124 switch (s
->ioregsel
) {
1129 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1135 index
= (s
->ioregsel
- 0x10) >> 1;
1136 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1137 if (s
->ioregsel
& 1)
1138 val
= s
->ioredtbl
[index
] >> 32;
1140 val
= s
->ioredtbl
[index
] & 0xffffffff;
1144 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1150 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1152 IOAPICState
*s
= opaque
;
1159 } else if (addr
== 0x10) {
1161 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1163 switch (s
->ioregsel
) {
1165 s
->id
= (val
>> 24) & 0xff;
1171 index
= (s
->ioregsel
- 0x10) >> 1;
1172 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1173 if (s
->ioregsel
& 1) {
1174 s
->ioredtbl
[index
] &= 0xffffffff;
1175 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1177 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1178 s
->ioredtbl
[index
] |= val
;
1186 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
1188 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1189 struct kvm_irqchip chip
;
1190 struct kvm_ioapic_state
*kioapic
;
1193 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1194 kvm_get_irqchip(kvm_context
, &chip
);
1195 kioapic
= &chip
.chip
.ioapic
;
1197 s
->id
= kioapic
->id
;
1198 s
->ioregsel
= kioapic
->ioregsel
;
1199 s
->base_address
= kioapic
->base_address
;
1200 s
->irr
= kioapic
->irr
;
1201 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1202 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
1207 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
1209 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1210 struct kvm_irqchip chip
;
1211 struct kvm_ioapic_state
*kioapic
;
1214 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1215 kioapic
= &chip
.chip
.ioapic
;
1217 kioapic
->id
= s
->id
;
1218 kioapic
->ioregsel
= s
->ioregsel
;
1219 kioapic
->base_address
= s
->base_address
;
1220 kioapic
->irr
= s
->irr
;
1221 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1222 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
1225 kvm_set_irqchip(kvm_context
, &chip
);
1229 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1231 IOAPICState
*s
= opaque
;
1234 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1235 kvm_kernel_ioapic_save_to_user(s
);
1238 qemu_put_8s(f
, &s
->id
);
1239 qemu_put_8s(f
, &s
->ioregsel
);
1240 qemu_put_be64s(f
, &s
->base_address
);
1241 qemu_put_be32s(f
, &s
->irr
);
1242 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1243 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1247 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1249 IOAPICState
*s
= opaque
;
1252 if (version_id
< 1 || version_id
> 2)
1255 qemu_get_8s(f
, &s
->id
);
1256 qemu_get_8s(f
, &s
->ioregsel
);
1257 if (version_id
== 2) {
1258 /* for version 2, we get this data off of the wire */
1259 qemu_get_be64s(f
, &s
->base_address
);
1260 qemu_get_be32s(f
, &s
->irr
);
1263 /* in case we are doing version 1, we just set these to sane values */
1264 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1267 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1268 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1271 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1272 kvm_kernel_ioapic_load_from_user(s
);
1278 static void ioapic_reset(void *opaque
)
1280 IOAPICState
*s
= opaque
;
1283 memset(s
, 0, sizeof(*s
));
1284 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1285 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1286 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1287 #ifdef KVM_CAP_IRQCHIP
1288 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1289 kvm_kernel_ioapic_load_from_user(s
);
1294 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1300 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1306 IOAPICState
*ioapic_init(void)
1311 s
= qemu_mallocz(sizeof(IOAPICState
));
1313 s
->id
= last_apic_id
++;
1315 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1316 ioapic_mem_write
, s
);
1317 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1319 register_savevm("ioapic", 0, 2, ioapic_save
, ioapic_load
, s
);
1320 qemu_register_reset(ioapic_reset
, s
);