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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;
109 static void apic_init_ipi(APICState
*s
);
110 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
111 static void apic_update_irq(APICState
*s
);
113 /* Find first bit starting from msb */
114 static int fls_bit(uint32_t value
)
116 return 31 - clz32(value
);
119 /* Find first bit starting from lsb */
120 static int ffs_bit(uint32_t value
)
125 static inline void set_bit(uint32_t *tab
, int index
)
129 mask
= 1 << (index
& 0x1f);
133 static inline void reset_bit(uint32_t *tab
, int index
)
137 mask
= 1 << (index
& 0x1f);
141 static void apic_local_deliver(CPUState
*env
, int vector
)
143 APICState
*s
= env
->apic_state
;
144 uint32_t lvt
= s
->lvt
[vector
];
147 if (lvt
& APIC_LVT_MASKED
)
150 switch ((lvt
>> 8) & 7) {
152 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
156 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
160 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
164 trigger_mode
= APIC_TRIGGER_EDGE
;
165 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
166 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
167 trigger_mode
= APIC_TRIGGER_LEVEL
;
168 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
172 void apic_deliver_pic_intr(CPUState
*env
, int level
)
175 apic_local_deliver(env
, APIC_LVT_LINT0
);
177 APICState
*s
= env
->apic_state
;
178 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
180 switch ((lvt
>> 8) & 7) {
182 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
184 reset_bit(s
->irr
, lvt
& 0xff);
187 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
193 #define foreach_apic(apic, deliver_bitmask, code) \
195 int __i, __j, __mask;\
196 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
197 __mask = deliver_bitmask[__i];\
199 for(__j = 0; __j < 32; __j++) {\
200 if (__mask & (1 << __j)) {\
201 apic = local_apics[__i * 32 + __j];\
211 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
212 uint8_t delivery_mode
,
213 uint8_t vector_num
, uint8_t polarity
,
214 uint8_t trigger_mode
)
216 APICState
*apic_iter
;
218 switch (delivery_mode
) {
220 /* XXX: search for focus processor, arbitration */
224 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
225 if (deliver_bitmask
[i
]) {
226 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
231 apic_iter
= local_apics
[d
];
233 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
243 foreach_apic(apic_iter
, deliver_bitmask
,
244 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
248 foreach_apic(apic_iter
, deliver_bitmask
,
249 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
253 /* normal INIT IPI sent to processors */
254 foreach_apic(apic_iter
, deliver_bitmask
,
255 apic_init_ipi(apic_iter
) );
259 /* handled in I/O APIC code */
266 foreach_apic(apic_iter
, deliver_bitmask
,
267 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
270 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
272 APICState
*s
= env
->apic_state
;
274 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
276 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel())
279 s
->apicbase
= (val
& 0xfffff000) |
280 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
281 /* if disabled, cannot be enabled again */
282 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
283 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
284 env
->cpuid_features
&= ~CPUID_APIC
;
285 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
289 uint64_t cpu_get_apic_base(CPUState
*env
)
291 APICState
*s
= env
->apic_state
;
293 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
298 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
300 APICState
*s
= env
->apic_state
;
301 s
->tpr
= (val
& 0x0f) << 4;
305 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
307 APICState
*s
= env
->apic_state
;
311 /* return -1 if no bit is set */
312 static int get_highest_priority_int(uint32_t *tab
)
315 for(i
= 7; i
>= 0; i
--) {
317 return i
* 32 + fls_bit(tab
[i
]);
323 static int apic_get_ppr(APICState
*s
)
328 isrv
= get_highest_priority_int(s
->isr
);
339 static int apic_get_arb_pri(APICState
*s
)
341 /* XXX: arbitration */
345 /* signal the CPU if an irq is pending */
346 static void apic_update_irq(APICState
*s
)
349 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
351 irrv
= get_highest_priority_int(s
->irr
);
354 ppr
= apic_get_ppr(s
);
355 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
357 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
360 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
362 set_bit(s
->irr
, vector_num
);
364 set_bit(s
->tmr
, vector_num
);
366 reset_bit(s
->tmr
, vector_num
);
370 static void apic_eoi(APICState
*s
)
373 isrv
= get_highest_priority_int(s
->isr
);
376 reset_bit(s
->isr
, isrv
);
377 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
378 set the remote IRR bit for level triggered interrupts. */
382 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
383 uint8_t dest
, uint8_t dest_mode
)
385 APICState
*apic_iter
;
388 if (dest_mode
== 0) {
390 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
392 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
393 set_bit(deliver_bitmask
, dest
);
396 /* XXX: cluster mode */
397 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
398 for(i
= 0; i
< MAX_APICS
; i
++) {
399 apic_iter
= local_apics
[i
];
401 if (apic_iter
->dest_mode
== 0xf) {
402 if (dest
& apic_iter
->log_dest
)
403 set_bit(deliver_bitmask
, i
);
404 } else if (apic_iter
->dest_mode
== 0x0) {
405 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
406 (dest
& apic_iter
->log_dest
& 0x0f)) {
407 set_bit(deliver_bitmask
, i
);
416 static void apic_init_ipi(APICState
*s
)
421 s
->spurious_vec
= 0xff;
424 memset(s
->isr
, 0, sizeof(s
->isr
));
425 memset(s
->tmr
, 0, sizeof(s
->tmr
));
426 memset(s
->irr
, 0, sizeof(s
->irr
));
427 for(i
= 0; i
< APIC_LVT_NB
; i
++)
428 s
->lvt
[i
] = 1 << 16; /* mask LVT */
430 memset(s
->icr
, 0, sizeof(s
->icr
));
433 s
->initial_count
= 0;
434 s
->initial_count_load_time
= 0;
437 cpu_reset(s
->cpu_env
);
439 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
) && !qemu_kvm_irqchip_in_kernel())
440 s
->cpu_env
->halted
= 1;
442 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
444 kvm_apic_init(s
->cpu_env
);
447 /* send a SIPI message to the CPU to start it */
448 static void apic_startup(APICState
*s
, int vector_num
)
450 CPUState
*env
= s
->cpu_env
;
454 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
457 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
458 kvm_update_after_sipi(env
);
461 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
462 uint8_t delivery_mode
, uint8_t vector_num
,
463 uint8_t polarity
, uint8_t trigger_mode
)
465 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
466 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
467 APICState
*apic_iter
;
469 switch (dest_shorthand
) {
471 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
474 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
475 set_bit(deliver_bitmask
, s
->id
);
478 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
481 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
482 reset_bit(deliver_bitmask
, s
->id
);
486 switch (delivery_mode
) {
489 int trig_mode
= (s
->icr
[0] >> 15) & 1;
490 int level
= (s
->icr
[0] >> 14) & 1;
491 if (level
== 0 && trig_mode
== 1) {
492 foreach_apic(apic_iter
, deliver_bitmask
,
493 apic_iter
->arb_id
= apic_iter
->id
);
500 foreach_apic(apic_iter
, deliver_bitmask
,
501 apic_startup(apic_iter
, vector_num
) );
505 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
509 int apic_get_interrupt(CPUState
*env
)
511 APICState
*s
= env
->apic_state
;
514 /* if the APIC is installed or enabled, we let the 8259 handle the
518 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
521 /* XXX: spurious IRQ handling */
522 intno
= get_highest_priority_int(s
->irr
);
525 if (s
->tpr
&& intno
<= s
->tpr
)
526 return s
->spurious_vec
& 0xff;
527 reset_bit(s
->irr
, intno
);
528 set_bit(s
->isr
, intno
);
533 int apic_accept_pic_intr(CPUState
*env
)
535 APICState
*s
= env
->apic_state
;
541 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
543 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
544 (lvt0
& APIC_LVT_MASKED
) == 0)
550 static uint32_t apic_get_current_count(APICState
*s
)
554 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
556 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
558 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
560 if (d
>= s
->initial_count
)
563 val
= s
->initial_count
- d
;
568 static void apic_timer_update(APICState
*s
, int64_t current_time
)
570 int64_t next_time
, d
;
572 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
573 d
= (current_time
- s
->initial_count_load_time
) >>
575 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
576 if (!s
->initial_count
)
578 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
580 if (d
>= s
->initial_count
)
582 d
= (uint64_t)s
->initial_count
+ 1;
584 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
585 qemu_mod_timer(s
->timer
, next_time
);
586 s
->next_time
= next_time
;
589 qemu_del_timer(s
->timer
);
593 static void apic_timer(void *opaque
)
595 APICState
*s
= opaque
;
597 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
598 apic_timer_update(s
, s
->next_time
);
601 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
606 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
611 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
615 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
619 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
626 env
= cpu_single_env
;
631 index
= (addr
>> 4) & 0xff;
636 case 0x03: /* version */
637 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
643 val
= apic_get_arb_pri(s
);
647 val
= apic_get_ppr(s
);
653 val
= s
->log_dest
<< 24;
656 val
= s
->dest_mode
<< 28;
659 val
= s
->spurious_vec
;
662 val
= s
->isr
[index
& 7];
665 val
= s
->tmr
[index
& 7];
668 val
= s
->irr
[index
& 7];
675 val
= s
->icr
[index
& 1];
678 val
= s
->lvt
[index
- 0x32];
681 val
= s
->initial_count
;
684 val
= apic_get_current_count(s
);
687 val
= s
->divide_conf
;
690 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
695 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
700 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
706 env
= cpu_single_env
;
712 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
715 index
= (addr
>> 4) & 0xff;
733 s
->log_dest
= val
>> 24;
736 s
->dest_mode
= val
>> 28;
739 s
->spurious_vec
= val
& 0x1ff;
749 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
750 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
751 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
758 int n
= index
- 0x32;
760 if (n
== APIC_LVT_TIMER
)
761 apic_timer_update(s
, qemu_get_clock(vm_clock
));
765 s
->initial_count
= val
;
766 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
767 apic_timer_update(s
, s
->initial_count_load_time
);
774 s
->divide_conf
= val
& 0xb;
775 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
776 s
->count_shift
= (v
+ 1) & 7;
780 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
785 #ifdef KVM_CAP_IRQCHIP
787 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
789 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
792 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
793 int reg_id
, uint32_t val
)
795 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
798 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
800 struct kvm_lapic_state apic
;
801 struct kvm_lapic_state
*kapic
= &apic
;
804 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
806 s
->id
= kapic_reg(kapic
, 0x2);
807 s
->tpr
= kapic_reg(kapic
, 0x8);
808 s
->arb_id
= kapic_reg(kapic
, 0x9);
809 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
810 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
811 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
812 for (i
= 0; i
< 8; i
++) {
813 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
814 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
815 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
817 s
->esr
= kapic_reg(kapic
, 0x28);
818 s
->icr
[0] = kapic_reg(kapic
, 0x30);
819 s
->icr
[1] = kapic_reg(kapic
, 0x31);
820 for (i
= 0; i
< APIC_LVT_NB
; i
++)
821 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
822 s
->initial_count
= kapic_reg(kapic
, 0x38);
823 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
825 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
826 s
->count_shift
= (v
+ 1) & 7;
828 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
829 apic_timer_update(s
, s
->initial_count_load_time
);
832 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
834 struct kvm_lapic_state apic
;
835 struct kvm_lapic_state
*klapic
= &apic
;
838 memset(klapic
, 0, sizeof apic
);
839 kapic_set_reg(klapic
, 0x2, s
->id
);
840 kapic_set_reg(klapic
, 0x8, s
->tpr
);
841 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
842 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
843 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
844 for (i
= 0; i
< 8; i
++) {
845 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
846 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
847 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
849 kapic_set_reg(klapic
, 0x28, s
->esr
);
850 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
851 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
852 for (i
= 0; i
< APIC_LVT_NB
; i
++)
853 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
854 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
855 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
857 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
862 static void apic_save(QEMUFile
*f
, void *opaque
)
864 APICState
*s
= opaque
;
867 #ifdef KVM_CAP_IRQCHIP
868 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
869 kvm_kernel_lapic_save_to_user(s
);
873 qemu_put_be32s(f
, &s
->apicbase
);
874 qemu_put_8s(f
, &s
->id
);
875 qemu_put_8s(f
, &s
->arb_id
);
876 qemu_put_8s(f
, &s
->tpr
);
877 qemu_put_be32s(f
, &s
->spurious_vec
);
878 qemu_put_8s(f
, &s
->log_dest
);
879 qemu_put_8s(f
, &s
->dest_mode
);
880 for (i
= 0; i
< 8; i
++) {
881 qemu_put_be32s(f
, &s
->isr
[i
]);
882 qemu_put_be32s(f
, &s
->tmr
[i
]);
883 qemu_put_be32s(f
, &s
->irr
[i
]);
885 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
886 qemu_put_be32s(f
, &s
->lvt
[i
]);
888 qemu_put_be32s(f
, &s
->esr
);
889 qemu_put_be32s(f
, &s
->icr
[0]);
890 qemu_put_be32s(f
, &s
->icr
[1]);
891 qemu_put_be32s(f
, &s
->divide_conf
);
892 qemu_put_be32(f
, s
->count_shift
);
893 qemu_put_be32s(f
, &s
->initial_count
);
894 qemu_put_be64(f
, s
->initial_count_load_time
);
895 qemu_put_be64(f
, s
->next_time
);
897 qemu_put_timer(f
, s
->timer
);
900 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
902 APICState
*s
= opaque
;
908 /* XXX: what if the base changes? (registered memory regions) */
909 qemu_get_be32s(f
, &s
->apicbase
);
910 qemu_get_8s(f
, &s
->id
);
911 qemu_get_8s(f
, &s
->arb_id
);
912 qemu_get_8s(f
, &s
->tpr
);
913 qemu_get_be32s(f
, &s
->spurious_vec
);
914 qemu_get_8s(f
, &s
->log_dest
);
915 qemu_get_8s(f
, &s
->dest_mode
);
916 for (i
= 0; i
< 8; i
++) {
917 qemu_get_be32s(f
, &s
->isr
[i
]);
918 qemu_get_be32s(f
, &s
->tmr
[i
]);
919 qemu_get_be32s(f
, &s
->irr
[i
]);
921 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
922 qemu_get_be32s(f
, &s
->lvt
[i
]);
924 qemu_get_be32s(f
, &s
->esr
);
925 qemu_get_be32s(f
, &s
->icr
[0]);
926 qemu_get_be32s(f
, &s
->icr
[1]);
927 qemu_get_be32s(f
, &s
->divide_conf
);
928 s
->count_shift
=qemu_get_be32(f
);
929 qemu_get_be32s(f
, &s
->initial_count
);
930 s
->initial_count_load_time
=qemu_get_be64(f
);
931 s
->next_time
=qemu_get_be64(f
);
934 qemu_get_timer(f
, s
->timer
);
936 #ifdef KVM_CAP_IRQCHIP
937 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
938 kvm_kernel_lapic_load_from_user(s
);
945 static void apic_reset(void *opaque
)
947 APICState
*s
= opaque
;
949 s
->apicbase
= 0xfee00000 |
950 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
956 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
957 * time typically by BIOS, so PIC interrupt can be delivered to the
958 * processor when local APIC is enabled.
960 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
962 #ifdef KVM_CAP_IRQCHIP
963 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
964 kvm_kernel_lapic_load_from_user(s
);
969 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
975 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
981 int apic_init(CPUState
*env
)
985 if (last_apic_id
>= MAX_APICS
)
987 s
= qemu_mallocz(sizeof(APICState
));
991 s
->id
= last_apic_id
++;
992 env
->cpuid_apic_id
= s
->id
;
997 /* XXX: mapping more APICs at the same memory location */
998 if (apic_io_memory
== 0) {
999 /* NOTE: the APIC is directly connected to the CPU - it is not
1000 on the global memory bus. */
1001 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
1002 apic_mem_write
, NULL
);
1003 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
1006 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1008 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
1009 qemu_register_reset(apic_reset
, s
);
1011 local_apics
[s
->id
] = s
;
1015 static void ioapic_service(IOAPICState
*s
)
1020 uint8_t delivery_mode
;
1026 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
1028 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1030 if (s
->irr
& mask
) {
1031 entry
= s
->ioredtbl
[i
];
1032 if (!(entry
& APIC_LVT_MASKED
)) {
1033 trig_mode
= ((entry
>> 15) & 1);
1035 dest_mode
= (entry
>> 11) & 1;
1036 delivery_mode
= (entry
>> 8) & 7;
1037 polarity
= (entry
>> 13) & 1;
1038 if (trig_mode
== APIC_TRIGGER_EDGE
)
1040 if (delivery_mode
== APIC_DM_EXTINT
)
1041 vector
= pic_read_irq(isa_pic
);
1043 vector
= entry
& 0xff;
1045 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
1046 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
1047 vector
, polarity
, trig_mode
);
1053 void ioapic_set_irq(void *opaque
, int vector
, int level
)
1055 IOAPICState
*s
= opaque
;
1057 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
1058 uint32_t mask
= 1 << vector
;
1059 uint64_t entry
= s
->ioredtbl
[vector
];
1061 if ((entry
>> 15) & 1) {
1062 /* level triggered */
1070 /* edge triggered */
1079 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1081 IOAPICState
*s
= opaque
;
1088 } else if (addr
== 0x10) {
1089 switch (s
->ioregsel
) {
1094 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1100 index
= (s
->ioregsel
- 0x10) >> 1;
1101 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1102 if (s
->ioregsel
& 1)
1103 val
= s
->ioredtbl
[index
] >> 32;
1105 val
= s
->ioredtbl
[index
] & 0xffffffff;
1109 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1115 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1117 IOAPICState
*s
= opaque
;
1124 } else if (addr
== 0x10) {
1126 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1128 switch (s
->ioregsel
) {
1130 s
->id
= (val
>> 24) & 0xff;
1136 index
= (s
->ioregsel
- 0x10) >> 1;
1137 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1138 if (s
->ioregsel
& 1) {
1139 s
->ioredtbl
[index
] &= 0xffffffff;
1140 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1142 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1143 s
->ioredtbl
[index
] |= val
;
1151 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
1153 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1154 struct kvm_irqchip chip
;
1155 struct kvm_ioapic_state
*kioapic
;
1158 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1159 kvm_get_irqchip(kvm_context
, &chip
);
1160 kioapic
= &chip
.chip
.ioapic
;
1162 s
->id
= kioapic
->id
;
1163 s
->ioregsel
= kioapic
->ioregsel
;
1164 s
->base_address
= kioapic
->base_address
;
1165 s
->irr
= kioapic
->irr
;
1166 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1167 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
1172 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
1174 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1175 struct kvm_irqchip chip
;
1176 struct kvm_ioapic_state
*kioapic
;
1179 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1180 kioapic
= &chip
.chip
.ioapic
;
1182 kioapic
->id
= s
->id
;
1183 kioapic
->ioregsel
= s
->ioregsel
;
1184 kioapic
->base_address
= s
->base_address
;
1185 kioapic
->irr
= s
->irr
;
1186 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1187 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
1190 kvm_set_irqchip(kvm_context
, &chip
);
1194 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1196 IOAPICState
*s
= opaque
;
1199 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1200 kvm_kernel_ioapic_save_to_user(s
);
1203 qemu_put_8s(f
, &s
->id
);
1204 qemu_put_8s(f
, &s
->ioregsel
);
1205 qemu_put_be64s(f
, &s
->base_address
);
1206 qemu_put_be32s(f
, &s
->irr
);
1207 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1208 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1212 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1214 IOAPICState
*s
= opaque
;
1217 if (version_id
< 1 || version_id
> 2)
1220 qemu_get_8s(f
, &s
->id
);
1221 qemu_get_8s(f
, &s
->ioregsel
);
1222 if (version_id
== 2) {
1223 /* for version 2, we get this data off of the wire */
1224 qemu_get_be64s(f
, &s
->base_address
);
1225 qemu_get_be32s(f
, &s
->irr
);
1228 /* in case we are doing version 1, we just set these to sane values */
1229 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1232 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1233 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1236 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1237 kvm_kernel_ioapic_load_from_user(s
);
1243 static void ioapic_reset(void *opaque
)
1245 IOAPICState
*s
= opaque
;
1248 memset(s
, 0, sizeof(*s
));
1249 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1250 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1251 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1252 #ifdef KVM_CAP_IRQCHIP
1253 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1254 kvm_kernel_ioapic_load_from_user(s
);
1259 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1265 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1271 IOAPICState
*ioapic_init(void)
1276 s
= qemu_mallocz(sizeof(IOAPICState
));
1280 s
->id
= last_apic_id
++;
1282 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1283 ioapic_mem_write
, s
);
1284 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1286 register_savevm("ioapic", 0, 2, ioapic_save
, ioapic_load
, s
);
1287 qemu_register_reset(ioapic_reset
, s
);