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"
27 //#define DEBUG_IOAPIC
29 /* APIC Local Vector Table */
30 #define APIC_LVT_TIMER 0
31 #define APIC_LVT_THERMAL 1
32 #define APIC_LVT_PERFORM 2
33 #define APIC_LVT_LINT0 3
34 #define APIC_LVT_LINT1 4
35 #define APIC_LVT_ERROR 5
38 /* APIC delivery modes */
39 #define APIC_DM_FIXED 0
40 #define APIC_DM_LOWPRI 1
43 #define APIC_DM_INIT 5
44 #define APIC_DM_SIPI 6
45 #define APIC_DM_EXTINT 7
47 /* APIC destination mode */
48 #define APIC_DESTMODE_FLAT 0xf
49 #define APIC_DESTMODE_CLUSTER 1
51 #define APIC_TRIGGER_EDGE 0
52 #define APIC_TRIGGER_LEVEL 1
54 #define APIC_LVT_TIMER_PERIODIC (1<<17)
55 #define APIC_LVT_MASKED (1<<16)
56 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
57 #define APIC_LVT_REMOTE_IRR (1<<14)
58 #define APIC_INPUT_POLARITY (1<<13)
59 #define APIC_SEND_PENDING (1<<12)
61 /* FIXME: it's now hard coded to be equal with KVM_IOAPIC_NUM_PINS */
62 #define IOAPIC_NUM_PINS 0x18
63 #define IOAPIC_DEFAULT_BASE_ADDRESS 0xfec00000
65 #define ESR_ILLEGAL_ADDRESS (1 << 7)
67 #define APIC_SV_ENABLE (1 << 8)
70 #define MAX_APIC_WORDS 8
72 typedef struct APICState
{
78 uint32_t spurious_vec
;
81 uint32_t isr
[8]; /* in service register */
82 uint32_t tmr
[8]; /* trigger mode register */
83 uint32_t irr
[8]; /* interrupt request register */
84 uint32_t lvt
[APIC_LVT_NB
];
85 uint32_t esr
; /* error register */
90 uint32_t initial_count
;
91 int64_t initial_count_load_time
, next_time
;
98 uint64_t base_address
;
101 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
104 static int apic_io_memory
;
105 static APICState
*local_apics
[MAX_APICS
+ 1];
106 static int last_apic_id
= 0;
108 static void apic_init_ipi(APICState
*s
);
109 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
110 static void apic_update_irq(APICState
*s
);
112 /* Find first bit starting from msb. Return 0 if value = 0 */
113 static int fls_bit(uint32_t value
)
115 unsigned int ret
= 0;
117 #if defined(HOST_I386)
118 __asm__
__volatile__ ("bsr %1, %0\n" : "+r" (ret
) : "rm" (value
));
122 value
>>= 16, ret
= 16;
124 value
>>= 8, ret
+= 8;
126 value
>>= 4, ret
+= 4;
128 value
>>= 2, ret
+= 2;
129 return ret
+ (value
>> 1);
133 /* Find first bit starting from lsb. Return 0 if value = 0 */
134 static int ffs_bit(uint32_t value
)
136 unsigned int ret
= 0;
138 #if defined(HOST_I386)
139 __asm__
__volatile__ ("bsf %1, %0\n" : "+r" (ret
) : "rm" (value
));
144 if (!(value
& 0xffff))
145 value
>>= 16, ret
= 16;
147 value
>>= 8, ret
+= 8;
149 value
>>= 4, ret
+= 4;
151 value
>>= 2, ret
+= 2;
158 static inline void set_bit(uint32_t *tab
, int index
)
162 mask
= 1 << (index
& 0x1f);
166 static inline void reset_bit(uint32_t *tab
, int index
)
170 mask
= 1 << (index
& 0x1f);
174 #define foreach_apic(apic, deliver_bitmask, code) \
176 int __i, __j, __mask;\
177 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
178 __mask = deliver_bitmask[__i];\
180 for(__j = 0; __j < 32; __j++) {\
181 if (__mask & (1 << __j)) {\
182 apic = local_apics[__i * 32 + __j];\
192 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
193 uint8_t delivery_mode
,
194 uint8_t vector_num
, uint8_t polarity
,
195 uint8_t trigger_mode
)
197 APICState
*apic_iter
;
199 switch (delivery_mode
) {
201 /* XXX: search for focus processor, arbitration */
205 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
206 if (deliver_bitmask
[i
]) {
207 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
212 apic_iter
= local_apics
[d
];
214 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
228 /* normal INIT IPI sent to processors */
229 foreach_apic(apic_iter
, deliver_bitmask
,
230 apic_init_ipi(apic_iter
) );
234 /* handled in I/O APIC code */
241 foreach_apic(apic_iter
, deliver_bitmask
,
242 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
245 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
247 APICState
*s
= env
->apic_state
;
249 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
251 s
->apicbase
= (val
& 0xfffff000) |
252 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
253 /* if disabled, cannot be enabled again */
254 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
255 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
256 env
->cpuid_features
&= ~CPUID_APIC
;
257 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
261 uint64_t cpu_get_apic_base(CPUState
*env
)
263 APICState
*s
= env
->apic_state
;
265 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
270 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
272 APICState
*s
= env
->apic_state
;
273 s
->tpr
= (val
& 0x0f) << 4;
277 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
279 APICState
*s
= env
->apic_state
;
283 /* return -1 if no bit is set */
284 static int get_highest_priority_int(uint32_t *tab
)
287 for(i
= 7; i
>= 0; i
--) {
289 return i
* 32 + fls_bit(tab
[i
]);
295 static int apic_get_ppr(APICState
*s
)
300 isrv
= get_highest_priority_int(s
->isr
);
311 static int apic_get_arb_pri(APICState
*s
)
313 /* XXX: arbitration */
317 /* signal the CPU if an irq is pending */
318 static void apic_update_irq(APICState
*s
)
321 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
323 irrv
= get_highest_priority_int(s
->irr
);
326 ppr
= apic_get_ppr(s
);
327 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
329 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
332 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
334 set_bit(s
->irr
, vector_num
);
336 set_bit(s
->tmr
, vector_num
);
338 reset_bit(s
->tmr
, vector_num
);
342 static void apic_eoi(APICState
*s
)
345 isrv
= get_highest_priority_int(s
->isr
);
348 reset_bit(s
->isr
, isrv
);
349 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
350 set the remote IRR bit for level triggered interrupts. */
354 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
355 uint8_t dest
, uint8_t dest_mode
)
357 APICState
*apic_iter
;
360 if (dest_mode
== 0) {
362 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
364 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
365 set_bit(deliver_bitmask
, dest
);
368 /* XXX: cluster mode */
369 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
370 for(i
= 0; i
< MAX_APICS
; i
++) {
371 apic_iter
= local_apics
[i
];
373 if (apic_iter
->dest_mode
== 0xf) {
374 if (dest
& apic_iter
->log_dest
)
375 set_bit(deliver_bitmask
, i
);
376 } else if (apic_iter
->dest_mode
== 0x0) {
377 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
378 (dest
& apic_iter
->log_dest
& 0x0f)) {
379 set_bit(deliver_bitmask
, i
);
388 static void apic_init_ipi(APICState
*s
)
393 s
->spurious_vec
= 0xff;
396 memset(s
->isr
, 0, sizeof(s
->isr
));
397 memset(s
->tmr
, 0, sizeof(s
->tmr
));
398 memset(s
->irr
, 0, sizeof(s
->irr
));
399 for(i
= 0; i
< APIC_LVT_NB
; i
++)
400 s
->lvt
[i
] = 1 << 16; /* mask LVT */
402 memset(s
->icr
, 0, sizeof(s
->icr
));
405 s
->initial_count
= 0;
406 s
->initial_count_load_time
= 0;
409 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
411 kvm_apic_init(s
->cpu_env
);
414 /* send a SIPI message to the CPU to start it */
415 static void apic_startup(APICState
*s
, int vector_num
)
417 CPUState
*env
= s
->cpu_env
;
418 if (!(env
->hflags
& HF_HALTED_MASK
))
421 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
423 env
->hflags
&= ~HF_HALTED_MASK
;
424 if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
425 kvm_update_after_sipi(env
);
428 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
429 uint8_t delivery_mode
, uint8_t vector_num
,
430 uint8_t polarity
, uint8_t trigger_mode
)
432 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
433 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
434 APICState
*apic_iter
;
436 switch (dest_shorthand
) {
438 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
441 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
442 set_bit(deliver_bitmask
, s
->id
);
445 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
448 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
449 reset_bit(deliver_bitmask
, s
->id
);
453 switch (delivery_mode
) {
456 int trig_mode
= (s
->icr
[0] >> 15) & 1;
457 int level
= (s
->icr
[0] >> 14) & 1;
458 if (level
== 0 && trig_mode
== 1) {
459 foreach_apic(apic_iter
, deliver_bitmask
,
460 apic_iter
->arb_id
= apic_iter
->id
);
467 foreach_apic(apic_iter
, deliver_bitmask
,
468 apic_startup(apic_iter
, vector_num
) );
472 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
476 int apic_get_interrupt(CPUState
*env
)
478 APICState
*s
= env
->apic_state
;
481 /* if the APIC is installed or enabled, we let the 8259 handle the
485 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
488 /* XXX: spurious IRQ handling */
489 intno
= get_highest_priority_int(s
->irr
);
492 if (s
->tpr
&& intno
<= s
->tpr
)
493 return s
->spurious_vec
& 0xff;
494 reset_bit(s
->irr
, intno
);
495 set_bit(s
->isr
, intno
);
500 int apic_accept_pic_intr(CPUState
*env
)
502 APICState
*s
= env
->apic_state
;
508 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
511 ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
512 ((lvt0
& APIC_LVT_MASKED
) == 0 &&
513 ((lvt0
>> 8) & 0x7) == APIC_DM_EXTINT
)))
519 static uint32_t apic_get_current_count(APICState
*s
)
523 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
525 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
527 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
529 if (d
>= s
->initial_count
)
532 val
= s
->initial_count
- d
;
537 static void apic_timer_update(APICState
*s
, int64_t current_time
)
539 int64_t next_time
, d
;
541 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
542 d
= (current_time
- s
->initial_count_load_time
) >>
544 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
545 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
547 if (d
>= s
->initial_count
)
549 d
= (uint64_t)s
->initial_count
+ 1;
551 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
552 qemu_mod_timer(s
->timer
, next_time
);
553 s
->next_time
= next_time
;
556 qemu_del_timer(s
->timer
);
560 static void apic_timer(void *opaque
)
562 APICState
*s
= opaque
;
564 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
565 apic_set_irq(s
, s
->lvt
[APIC_LVT_TIMER
] & 0xff, APIC_TRIGGER_EDGE
);
567 apic_timer_update(s
, s
->next_time
);
570 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
575 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
580 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
584 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
588 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
595 env
= cpu_single_env
;
600 index
= (addr
>> 4) & 0xff;
605 case 0x03: /* version */
606 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
612 val
= apic_get_arb_pri(s
);
616 val
= apic_get_ppr(s
);
621 val
= s
->log_dest
<< 24;
624 val
= s
->dest_mode
<< 28;
627 val
= s
->spurious_vec
;
630 val
= s
->isr
[index
& 7];
633 val
= s
->tmr
[index
& 7];
636 val
= s
->irr
[index
& 7];
643 val
= s
->icr
[index
& 1];
646 val
= s
->lvt
[index
- 0x32];
649 val
= s
->initial_count
;
652 val
= apic_get_current_count(s
);
655 val
= s
->divide_conf
;
658 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
663 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
668 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
674 env
= cpu_single_env
;
680 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
683 index
= (addr
>> 4) & 0xff;
701 s
->log_dest
= val
>> 24;
704 s
->dest_mode
= val
>> 28;
707 s
->spurious_vec
= val
& 0x1ff;
717 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
718 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
719 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
726 int n
= index
- 0x32;
728 if (n
== APIC_LVT_TIMER
)
729 apic_timer_update(s
, qemu_get_clock(vm_clock
));
733 s
->initial_count
= val
;
734 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
735 apic_timer_update(s
, s
->initial_count_load_time
);
742 s
->divide_conf
= val
& 0xb;
743 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
744 s
->count_shift
= (v
+ 1) & 7;
748 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
753 #ifdef KVM_CAP_IRQCHIP
755 static inline uint32_t kapic_reg(struct kvm_lapic_state
*kapic
, int reg_id
)
757 return *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4)));
760 static inline void kapic_set_reg(struct kvm_lapic_state
*kapic
,
761 int reg_id
, uint32_t val
)
763 *((uint32_t *) (kapic
->regs
+ (reg_id
<< 4))) = val
;
766 static void kvm_kernel_lapic_save_to_user(APICState
*s
)
768 struct kvm_lapic_state apic
;
769 struct kvm_lapic_state
*kapic
= &apic
;
772 kvm_get_lapic(kvm_context
, s
->cpu_env
->cpu_index
, kapic
);
774 s
->id
= kapic_reg(kapic
, 0x2);
775 s
->tpr
= kapic_reg(kapic
, 0x8);
776 s
->arb_id
= kapic_reg(kapic
, 0x9);
777 s
->log_dest
= kapic_reg(kapic
, 0xd) >> 24;
778 s
->dest_mode
= kapic_reg(kapic
, 0xe) >> 28;
779 s
->spurious_vec
= kapic_reg(kapic
, 0xf);
780 for (i
= 0; i
< 8; i
++) {
781 s
->isr
[i
] = kapic_reg(kapic
, 0x10 + i
);
782 s
->tmr
[i
] = kapic_reg(kapic
, 0x18 + i
);
783 s
->irr
[i
] = kapic_reg(kapic
, 0x20 + i
);
785 s
->esr
= kapic_reg(kapic
, 0x28);
786 s
->icr
[0] = kapic_reg(kapic
, 0x30);
787 s
->icr
[1] = kapic_reg(kapic
, 0x31);
788 for (i
= 0; i
< APIC_LVT_NB
; i
++)
789 s
->lvt
[i
] = kapic_reg(kapic
, 0x32 + i
);
790 s
->initial_count
= kapic_reg(kapic
, 0x38);
791 s
->divide_conf
= kapic_reg(kapic
, 0x3e);
793 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
794 s
->count_shift
= (v
+ 1) & 7;
796 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
797 apic_timer_update(s
, s
->initial_count_load_time
);
800 static void kvm_kernel_lapic_load_from_user(APICState
*s
)
802 struct kvm_lapic_state apic
;
803 struct kvm_lapic_state
*klapic
= &apic
;
806 memset(klapic
, 0, sizeof apic
);
807 kapic_set_reg(klapic
, 0x2, s
->id
);
808 kapic_set_reg(klapic
, 0x8, s
->tpr
);
809 kapic_set_reg(klapic
, 0xd, s
->log_dest
<< 24);
810 kapic_set_reg(klapic
, 0xe, s
->dest_mode
<< 28 | 0x0fffffff);
811 kapic_set_reg(klapic
, 0xf, s
->spurious_vec
);
812 for (i
= 0; i
< 8; i
++) {
813 kapic_set_reg(klapic
, 0x10 + i
, s
->isr
[i
]);
814 kapic_set_reg(klapic
, 0x18 + i
, s
->tmr
[i
]);
815 kapic_set_reg(klapic
, 0x20 + i
, s
->irr
[i
]);
817 kapic_set_reg(klapic
, 0x28, s
->esr
);
818 kapic_set_reg(klapic
, 0x30, s
->icr
[0]);
819 kapic_set_reg(klapic
, 0x31, s
->icr
[1]);
820 for (i
= 0; i
< APIC_LVT_NB
; i
++)
821 kapic_set_reg(klapic
, 0x32 + i
, s
->lvt
[i
]);
822 kapic_set_reg(klapic
, 0x38, s
->initial_count
);
823 kapic_set_reg(klapic
, 0x3e, s
->divide_conf
);
825 kvm_set_lapic(kvm_context
, s
->cpu_env
->cpu_index
, klapic
);
830 static void apic_save(QEMUFile
*f
, void *opaque
)
832 APICState
*s
= opaque
;
835 #ifdef KVM_CAP_IRQCHIP
836 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
837 kvm_kernel_lapic_save_to_user(s
);
841 qemu_put_be32s(f
, &s
->apicbase
);
842 qemu_put_8s(f
, &s
->id
);
843 qemu_put_8s(f
, &s
->arb_id
);
844 qemu_put_8s(f
, &s
->tpr
);
845 qemu_put_be32s(f
, &s
->spurious_vec
);
846 qemu_put_8s(f
, &s
->log_dest
);
847 qemu_put_8s(f
, &s
->dest_mode
);
848 for (i
= 0; i
< 8; i
++) {
849 qemu_put_be32s(f
, &s
->isr
[i
]);
850 qemu_put_be32s(f
, &s
->tmr
[i
]);
851 qemu_put_be32s(f
, &s
->irr
[i
]);
853 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
854 qemu_put_be32s(f
, &s
->lvt
[i
]);
856 qemu_put_be32s(f
, &s
->esr
);
857 qemu_put_be32s(f
, &s
->icr
[0]);
858 qemu_put_be32s(f
, &s
->icr
[1]);
859 qemu_put_be32s(f
, &s
->divide_conf
);
860 qemu_put_be32(f
, s
->count_shift
);
861 qemu_put_be32s(f
, &s
->initial_count
);
862 qemu_put_be64(f
, s
->initial_count_load_time
);
863 qemu_put_be64(f
, s
->next_time
);
865 qemu_put_timer(f
, s
->timer
);
868 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
870 APICState
*s
= opaque
;
876 /* XXX: what if the base changes? (registered memory regions) */
877 qemu_get_be32s(f
, &s
->apicbase
);
878 qemu_get_8s(f
, &s
->id
);
879 qemu_get_8s(f
, &s
->arb_id
);
880 qemu_get_8s(f
, &s
->tpr
);
881 qemu_get_be32s(f
, &s
->spurious_vec
);
882 qemu_get_8s(f
, &s
->log_dest
);
883 qemu_get_8s(f
, &s
->dest_mode
);
884 for (i
= 0; i
< 8; i
++) {
885 qemu_get_be32s(f
, &s
->isr
[i
]);
886 qemu_get_be32s(f
, &s
->tmr
[i
]);
887 qemu_get_be32s(f
, &s
->irr
[i
]);
889 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
890 qemu_get_be32s(f
, &s
->lvt
[i
]);
892 qemu_get_be32s(f
, &s
->esr
);
893 qemu_get_be32s(f
, &s
->icr
[0]);
894 qemu_get_be32s(f
, &s
->icr
[1]);
895 qemu_get_be32s(f
, &s
->divide_conf
);
896 s
->count_shift
=qemu_get_be32(f
);
897 qemu_get_be32s(f
, &s
->initial_count
);
898 s
->initial_count_load_time
=qemu_get_be64(f
);
899 s
->next_time
=qemu_get_be64(f
);
902 qemu_get_timer(f
, s
->timer
);
904 #ifdef KVM_CAP_IRQCHIP
905 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
906 kvm_kernel_lapic_load_from_user(s
);
913 static void apic_reset(void *opaque
)
915 APICState
*s
= opaque
;
919 * LINT0 delivery mode is set to ExtInt at initialization time
920 * typically by BIOS, so PIC interrupt can be delivered to the
921 * processor when local APIC is enabled.
923 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
924 #ifdef KVM_CAP_IRQCHIP
925 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
926 kvm_kernel_lapic_load_from_user(s
);
931 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
937 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
943 int apic_init(CPUState
*env
)
947 if (last_apic_id
>= MAX_APICS
)
949 s
= qemu_mallocz(sizeof(APICState
));
954 s
->id
= last_apic_id
++;
955 env
->cpuid_apic_id
= s
->id
;
957 s
->apicbase
= 0xfee00000 |
958 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
961 * LINT0 delivery mode is set to ExtInt at initialization time
962 * typically by BIOS, so PIC interrupt can be delivered to the
963 * processor when local APIC is enabled.
965 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
967 /* XXX: mapping more APICs at the same memory location */
968 if (apic_io_memory
== 0) {
969 /* NOTE: the APIC is directly connected to the CPU - it is not
970 on the global memory bus. */
971 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
972 apic_mem_write
, NULL
);
973 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
976 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
978 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
979 qemu_register_reset(apic_reset
, s
);
981 local_apics
[s
->id
] = s
;
985 static void ioapic_service(IOAPICState
*s
)
990 uint8_t delivery_mode
;
996 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
998 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1000 if (s
->irr
& mask
) {
1001 entry
= s
->ioredtbl
[i
];
1002 if (!(entry
& APIC_LVT_MASKED
)) {
1003 trig_mode
= ((entry
>> 15) & 1);
1005 dest_mode
= (entry
>> 11) & 1;
1006 delivery_mode
= (entry
>> 8) & 7;
1007 polarity
= (entry
>> 13) & 1;
1008 if (trig_mode
== APIC_TRIGGER_EDGE
)
1010 if (delivery_mode
== APIC_DM_EXTINT
)
1011 vector
= pic_read_irq(isa_pic
);
1013 vector
= entry
& 0xff;
1015 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
1016 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
1017 vector
, polarity
, trig_mode
);
1023 void ioapic_set_irq(void *opaque
, int vector
, int level
)
1025 IOAPICState
*s
= opaque
;
1027 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
1028 uint32_t mask
= 1 << vector
;
1029 uint64_t entry
= s
->ioredtbl
[vector
];
1031 if ((entry
>> 15) & 1) {
1032 /* level triggered */
1040 /* edge triggered */
1049 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1051 IOAPICState
*s
= opaque
;
1058 } else if (addr
== 0x10) {
1059 switch (s
->ioregsel
) {
1064 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1070 index
= (s
->ioregsel
- 0x10) >> 1;
1071 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1072 if (s
->ioregsel
& 1)
1073 val
= s
->ioredtbl
[index
] >> 32;
1075 val
= s
->ioredtbl
[index
] & 0xffffffff;
1079 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1085 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1087 IOAPICState
*s
= opaque
;
1094 } else if (addr
== 0x10) {
1096 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1098 switch (s
->ioregsel
) {
1100 s
->id
= (val
>> 24) & 0xff;
1106 index
= (s
->ioregsel
- 0x10) >> 1;
1107 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1108 if (s
->ioregsel
& 1) {
1109 s
->ioredtbl
[index
] &= 0xffffffff;
1110 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1112 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1113 s
->ioredtbl
[index
] |= val
;
1121 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
1123 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1124 struct kvm_irqchip chip
;
1125 struct kvm_ioapic_state
*kioapic
;
1128 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1129 kvm_get_irqchip(kvm_context
, &chip
);
1130 kioapic
= &chip
.chip
.ioapic
;
1132 s
->id
= kioapic
->id
;
1133 s
->ioregsel
= kioapic
->ioregsel
;
1134 s
->base_address
= kioapic
->base_address
;
1135 s
->irr
= kioapic
->irr
;
1136 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1137 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
1142 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
1144 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
1145 struct kvm_irqchip chip
;
1146 struct kvm_ioapic_state
*kioapic
;
1149 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
1150 kioapic
= &chip
.chip
.ioapic
;
1152 kioapic
->id
= s
->id
;
1153 kioapic
->ioregsel
= s
->ioregsel
;
1154 kioapic
->base_address
= s
->base_address
;
1155 kioapic
->irr
= s
->irr
;
1156 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1157 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
1160 kvm_set_irqchip(kvm_context
, &chip
);
1164 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1166 IOAPICState
*s
= opaque
;
1169 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1170 kvm_kernel_ioapic_save_to_user(s
);
1173 qemu_put_8s(f
, &s
->id
);
1174 qemu_put_8s(f
, &s
->ioregsel
);
1175 qemu_put_be64s(f
, &s
->base_address
);
1176 qemu_put_be32s(f
, &s
->irr
);
1177 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1178 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1182 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1184 IOAPICState
*s
= opaque
;
1187 if (version_id
< 1 || version_id
> 2)
1190 qemu_get_8s(f
, &s
->id
);
1191 qemu_get_8s(f
, &s
->ioregsel
);
1192 if (version_id
== 2) {
1193 /* for version 2, we get this data off of the wire */
1194 qemu_get_be64s(f
, &s
->base_address
);
1195 qemu_get_be32s(f
, &s
->irr
);
1198 /* in case we are doing version 1, we just set these to sane values */
1199 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
1202 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1203 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1206 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1207 kvm_kernel_ioapic_load_from_user(s
);
1213 static void ioapic_reset(void *opaque
)
1215 IOAPICState
*s
= opaque
;
1218 memset(s
, 0, sizeof(*s
));
1219 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1220 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1223 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1229 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1235 IOAPICState
*ioapic_init(void)
1240 s
= qemu_mallocz(sizeof(IOAPICState
));
1244 s
->id
= last_apic_id
++;
1246 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1247 ioapic_mem_write
, s
);
1248 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1250 register_savevm("ioapic", 0, 2, ioapic_save
, ioapic_load
, s
);
1251 qemu_register_reset(ioapic_reset
, s
);