4 * Copyright (c) 2004-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>
19 #include "qemu/osdep.h"
20 #include "qemu-common.h"
22 #include "qemu/thread.h"
23 #include "hw/i386/apic_internal.h"
24 #include "hw/i386/apic.h"
25 #include "hw/i386/ioapic.h"
26 #include "hw/pci/msi.h"
27 #include "qemu/host-utils.h"
29 #include "hw/i386/pc.h"
30 #include "hw/i386/apic-msidef.h"
32 #define MAX_APIC_WORDS 8
34 #define SYNC_FROM_VAPIC 0x1
35 #define SYNC_TO_VAPIC 0x2
36 #define SYNC_ISR_IRR_TO_VAPIC 0x4
38 static APICCommonState
*local_apics
[MAX_APICS
+ 1];
40 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
);
41 static void apic_update_irq(APICCommonState
*s
);
42 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
43 uint8_t dest
, uint8_t dest_mode
);
45 /* Find first bit starting from msb */
46 static int apic_fls_bit(uint32_t value
)
48 return 31 - clz32(value
);
51 /* Find first bit starting from lsb */
52 static int apic_ffs_bit(uint32_t value
)
57 static inline void apic_reset_bit(uint32_t *tab
, int index
)
61 mask
= 1 << (index
& 0x1f);
65 /* return -1 if no bit is set */
66 static int get_highest_priority_int(uint32_t *tab
)
69 for (i
= 7; i
>= 0; i
--) {
71 return i
* 32 + apic_fls_bit(tab
[i
]);
77 static void apic_sync_vapic(APICCommonState
*s
, int sync_type
)
79 VAPICState vapic_state
;
84 if (!s
->vapic_paddr
) {
87 if (sync_type
& SYNC_FROM_VAPIC
) {
88 cpu_physical_memory_read(s
->vapic_paddr
, &vapic_state
,
90 s
->tpr
= vapic_state
.tpr
;
92 if (sync_type
& (SYNC_TO_VAPIC
| SYNC_ISR_IRR_TO_VAPIC
)) {
93 start
= offsetof(VAPICState
, isr
);
94 length
= offsetof(VAPICState
, enabled
) - offsetof(VAPICState
, isr
);
96 if (sync_type
& SYNC_TO_VAPIC
) {
97 assert(qemu_cpu_is_self(CPU(s
->cpu
)));
99 vapic_state
.tpr
= s
->tpr
;
100 vapic_state
.enabled
= 1;
102 length
= sizeof(VAPICState
);
105 vector
= get_highest_priority_int(s
->isr
);
109 vapic_state
.isr
= vector
& 0xf0;
111 vapic_state
.zero
= 0;
113 vector
= get_highest_priority_int(s
->irr
);
117 vapic_state
.irr
= vector
& 0xff;
119 cpu_physical_memory_write_rom(&address_space_memory
,
120 s
->vapic_paddr
+ start
,
121 ((void *)&vapic_state
) + start
, length
);
125 static void apic_vapic_base_update(APICCommonState
*s
)
127 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
130 static void apic_local_deliver(APICCommonState
*s
, int vector
)
132 uint32_t lvt
= s
->lvt
[vector
];
135 trace_apic_local_deliver(vector
, (lvt
>> 8) & 7);
137 if (lvt
& APIC_LVT_MASKED
)
140 switch ((lvt
>> 8) & 7) {
142 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SMI
);
146 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_NMI
);
150 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_HARD
);
154 trigger_mode
= APIC_TRIGGER_EDGE
;
155 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
156 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
157 trigger_mode
= APIC_TRIGGER_LEVEL
;
158 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
162 void apic_deliver_pic_intr(DeviceState
*dev
, int level
)
164 APICCommonState
*s
= APIC_COMMON(dev
);
167 apic_local_deliver(s
, APIC_LVT_LINT0
);
169 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
171 switch ((lvt
>> 8) & 7) {
173 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
175 apic_reset_bit(s
->irr
, lvt
& 0xff);
184 static void apic_external_nmi(APICCommonState
*s
)
186 apic_local_deliver(s
, APIC_LVT_LINT1
);
189 #define foreach_apic(apic, deliver_bitmask, code) \
192 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
193 uint32_t __mask = deliver_bitmask[__i];\
195 for(__j = 0; __j < 32; __j++) {\
196 if (__mask & (1U << __j)) {\
197 apic = local_apics[__i * 32 + __j];\
207 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
208 uint8_t delivery_mode
, uint8_t vector_num
,
209 uint8_t trigger_mode
)
211 APICCommonState
*apic_iter
;
213 switch (delivery_mode
) {
215 /* XXX: search for focus processor, arbitration */
219 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
220 if (deliver_bitmask
[i
]) {
221 d
= i
* 32 + apic_ffs_bit(deliver_bitmask
[i
]);
226 apic_iter
= local_apics
[d
];
228 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
238 foreach_apic(apic_iter
, deliver_bitmask
,
239 cpu_interrupt(CPU(apic_iter
->cpu
), CPU_INTERRUPT_SMI
)
244 foreach_apic(apic_iter
, deliver_bitmask
,
245 cpu_interrupt(CPU(apic_iter
->cpu
), CPU_INTERRUPT_NMI
)
250 /* normal INIT IPI sent to processors */
251 foreach_apic(apic_iter
, deliver_bitmask
,
252 cpu_interrupt(CPU(apic_iter
->cpu
),
258 /* handled in I/O APIC code */
265 foreach_apic(apic_iter
, deliver_bitmask
,
266 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
269 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
, uint8_t delivery_mode
,
270 uint8_t vector_num
, uint8_t trigger_mode
)
272 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
274 trace_apic_deliver_irq(dest
, dest_mode
, delivery_mode
, vector_num
,
277 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
278 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, trigger_mode
);
281 static void apic_set_base(APICCommonState
*s
, uint64_t val
)
283 s
->apicbase
= (val
& 0xfffff000) |
284 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
285 /* if disabled, cannot be enabled again */
286 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
287 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
288 cpu_clear_apic_feature(&s
->cpu
->env
);
289 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
293 static void apic_set_tpr(APICCommonState
*s
, uint8_t val
)
295 /* Updates from cr8 are ignored while the VAPIC is active */
296 if (!s
->vapic_paddr
) {
302 static uint8_t apic_get_tpr(APICCommonState
*s
)
304 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
308 int apic_get_ppr(APICCommonState
*s
)
313 isrv
= get_highest_priority_int(s
->isr
);
324 static int apic_get_arb_pri(APICCommonState
*s
)
326 /* XXX: arbitration */
332 * <0 - low prio interrupt,
334 * >0 - interrupt number
336 static int apic_irq_pending(APICCommonState
*s
)
340 if (!(s
->spurious_vec
& APIC_SV_ENABLE
)) {
344 irrv
= get_highest_priority_int(s
->irr
);
348 ppr
= apic_get_ppr(s
);
349 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0)) {
356 /* signal the CPU if an irq is pending */
357 static void apic_update_irq(APICCommonState
*s
)
360 DeviceState
*dev
= (DeviceState
*)s
;
363 if (!qemu_cpu_is_self(cpu
)) {
364 cpu_interrupt(cpu
, CPU_INTERRUPT_POLL
);
365 } else if (apic_irq_pending(s
) > 0) {
366 cpu_interrupt(cpu
, CPU_INTERRUPT_HARD
);
367 } else if (!apic_accept_pic_intr(dev
) || !pic_get_output(isa_pic
)) {
368 cpu_reset_interrupt(cpu
, CPU_INTERRUPT_HARD
);
372 void apic_poll_irq(DeviceState
*dev
)
374 APICCommonState
*s
= APIC_COMMON(dev
);
376 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
380 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
)
382 apic_report_irq_delivered(!apic_get_bit(s
->irr
, vector_num
));
384 apic_set_bit(s
->irr
, vector_num
);
386 apic_set_bit(s
->tmr
, vector_num
);
388 apic_reset_bit(s
->tmr
, vector_num
);
389 if (s
->vapic_paddr
) {
390 apic_sync_vapic(s
, SYNC_ISR_IRR_TO_VAPIC
);
392 * The vcpu thread needs to see the new IRR before we pull its current
393 * TPR value. That way, if we miss a lowering of the TRP, the guest
394 * has the chance to notice the new IRR and poll for IRQs on its own.
397 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
402 static void apic_eoi(APICCommonState
*s
)
405 isrv
= get_highest_priority_int(s
->isr
);
408 apic_reset_bit(s
->isr
, isrv
);
409 if (!(s
->spurious_vec
& APIC_SV_DIRECTED_IO
) && apic_get_bit(s
->tmr
, isrv
)) {
410 ioapic_eoi_broadcast(isrv
);
412 apic_sync_vapic(s
, SYNC_FROM_VAPIC
| SYNC_TO_VAPIC
);
416 static int apic_find_dest(uint8_t dest
)
418 APICCommonState
*apic
= local_apics
[dest
];
421 if (apic
&& apic
->id
== dest
)
422 return dest
; /* shortcut in case apic->id == apic->idx */
424 for (i
= 0; i
< MAX_APICS
; i
++) {
425 apic
= local_apics
[i
];
426 if (apic
&& apic
->id
== dest
)
435 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
436 uint8_t dest
, uint8_t dest_mode
)
438 APICCommonState
*apic_iter
;
441 if (dest_mode
== 0) {
443 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
445 int idx
= apic_find_dest(dest
);
446 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
448 apic_set_bit(deliver_bitmask
, idx
);
451 /* XXX: cluster mode */
452 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
453 for(i
= 0; i
< MAX_APICS
; i
++) {
454 apic_iter
= local_apics
[i
];
456 if (apic_iter
->dest_mode
== 0xf) {
457 if (dest
& apic_iter
->log_dest
)
458 apic_set_bit(deliver_bitmask
, i
);
459 } else if (apic_iter
->dest_mode
== 0x0) {
460 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
461 (dest
& apic_iter
->log_dest
& 0x0f)) {
462 apic_set_bit(deliver_bitmask
, i
);
472 static void apic_startup(APICCommonState
*s
, int vector_num
)
474 s
->sipi_vector
= vector_num
;
475 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SIPI
);
478 void apic_sipi(DeviceState
*dev
)
480 APICCommonState
*s
= APIC_COMMON(dev
);
482 cpu_reset_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SIPI
);
484 if (!s
->wait_for_sipi
)
486 cpu_x86_load_seg_cache_sipi(s
->cpu
, s
->sipi_vector
);
487 s
->wait_for_sipi
= 0;
490 static void apic_deliver(DeviceState
*dev
, uint8_t dest
, uint8_t dest_mode
,
491 uint8_t delivery_mode
, uint8_t vector_num
,
492 uint8_t trigger_mode
)
494 APICCommonState
*s
= APIC_COMMON(dev
);
495 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
496 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
497 APICCommonState
*apic_iter
;
499 switch (dest_shorthand
) {
501 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
504 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
505 apic_set_bit(deliver_bitmask
, s
->idx
);
508 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
511 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
512 apic_reset_bit(deliver_bitmask
, s
->idx
);
516 switch (delivery_mode
) {
519 int trig_mode
= (s
->icr
[0] >> 15) & 1;
520 int level
= (s
->icr
[0] >> 14) & 1;
521 if (level
== 0 && trig_mode
== 1) {
522 foreach_apic(apic_iter
, deliver_bitmask
,
523 apic_iter
->arb_id
= apic_iter
->id
);
530 foreach_apic(apic_iter
, deliver_bitmask
,
531 apic_startup(apic_iter
, vector_num
) );
535 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, trigger_mode
);
538 static bool apic_check_pic(APICCommonState
*s
)
540 DeviceState
*dev
= (DeviceState
*)s
;
542 if (!apic_accept_pic_intr(dev
) || !pic_get_output(isa_pic
)) {
545 apic_deliver_pic_intr(dev
, 1);
549 int apic_get_interrupt(DeviceState
*dev
)
551 APICCommonState
*s
= APIC_COMMON(dev
);
554 /* if the APIC is installed or enabled, we let the 8259 handle the
558 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
561 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
562 intno
= apic_irq_pending(s
);
564 /* if there is an interrupt from the 8259, let the caller handle
565 * that first since ExtINT interrupts ignore the priority.
567 if (intno
== 0 || apic_check_pic(s
)) {
568 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
570 } else if (intno
< 0) {
571 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
572 return s
->spurious_vec
& 0xff;
574 apic_reset_bit(s
->irr
, intno
);
575 apic_set_bit(s
->isr
, intno
);
576 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
583 int apic_accept_pic_intr(DeviceState
*dev
)
585 APICCommonState
*s
= APIC_COMMON(dev
);
591 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
593 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
594 (lvt0
& APIC_LVT_MASKED
) == 0)
600 static uint32_t apic_get_current_count(APICCommonState
*s
)
604 d
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - s
->initial_count_load_time
) >>
606 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
608 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
610 if (d
>= s
->initial_count
)
613 val
= s
->initial_count
- d
;
618 static void apic_timer_update(APICCommonState
*s
, int64_t current_time
)
620 if (apic_next_timer(s
, current_time
)) {
621 timer_mod(s
->timer
, s
->next_time
);
627 static void apic_timer(void *opaque
)
629 APICCommonState
*s
= opaque
;
631 apic_local_deliver(s
, APIC_LVT_TIMER
);
632 apic_timer_update(s
, s
->next_time
);
635 static uint32_t apic_mem_readb(void *opaque
, hwaddr addr
)
640 static uint32_t apic_mem_readw(void *opaque
, hwaddr addr
)
645 static void apic_mem_writeb(void *opaque
, hwaddr addr
, uint32_t val
)
649 static void apic_mem_writew(void *opaque
, hwaddr addr
, uint32_t val
)
653 static uint32_t apic_mem_readl(void *opaque
, hwaddr addr
)
660 dev
= cpu_get_current_apic();
664 s
= APIC_COMMON(dev
);
666 index
= (addr
>> 4) & 0xff;
671 case 0x03: /* version */
672 val
= s
->version
| ((APIC_LVT_NB
- 1) << 16);
675 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
676 if (apic_report_tpr_access
) {
677 cpu_report_tpr_access(&s
->cpu
->env
, TPR_ACCESS_READ
);
682 val
= apic_get_arb_pri(s
);
686 val
= apic_get_ppr(s
);
692 val
= s
->log_dest
<< 24;
695 val
= (s
->dest_mode
<< 28) | 0xfffffff;
698 val
= s
->spurious_vec
;
701 val
= s
->isr
[index
& 7];
704 val
= s
->tmr
[index
& 7];
707 val
= s
->irr
[index
& 7];
714 val
= s
->icr
[index
& 1];
717 val
= s
->lvt
[index
- 0x32];
720 val
= s
->initial_count
;
723 val
= apic_get_current_count(s
);
726 val
= s
->divide_conf
;
729 s
->esr
|= APIC_ESR_ILLEGAL_ADDRESS
;
733 trace_apic_mem_readl(addr
, val
);
737 static void apic_send_msi(hwaddr addr
, uint32_t data
)
739 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
740 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
741 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
742 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
743 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
744 /* XXX: Ignore redirection hint. */
745 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, trigger_mode
);
748 static void apic_mem_writel(void *opaque
, hwaddr addr
, uint32_t val
)
752 int index
= (addr
>> 4) & 0xff;
753 if (addr
> 0xfff || !index
) {
754 /* MSI and MMIO APIC are at the same memory location,
755 * but actually not on the global bus: MSI is on PCI bus
756 * APIC is connected directly to the CPU.
757 * Mapping them on the global bus happens to work because
758 * MSI registers are reserved in APIC MMIO and vice versa. */
759 apic_send_msi(addr
, val
);
763 dev
= cpu_get_current_apic();
767 s
= APIC_COMMON(dev
);
769 trace_apic_mem_writel(addr
, val
);
778 if (apic_report_tpr_access
) {
779 cpu_report_tpr_access(&s
->cpu
->env
, TPR_ACCESS_WRITE
);
782 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
792 s
->log_dest
= val
>> 24;
795 s
->dest_mode
= val
>> 28;
798 s
->spurious_vec
= val
& 0x1ff;
808 apic_deliver(dev
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
809 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
810 (s
->icr
[0] >> 15) & 1);
817 int n
= index
- 0x32;
819 if (n
== APIC_LVT_TIMER
) {
820 apic_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
821 } else if (n
== APIC_LVT_LINT0
&& apic_check_pic(s
)) {
827 s
->initial_count
= val
;
828 s
->initial_count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
829 apic_timer_update(s
, s
->initial_count_load_time
);
836 s
->divide_conf
= val
& 0xb;
837 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
838 s
->count_shift
= (v
+ 1) & 7;
842 s
->esr
|= APIC_ESR_ILLEGAL_ADDRESS
;
847 static void apic_pre_save(APICCommonState
*s
)
849 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
852 static void apic_post_load(APICCommonState
*s
)
854 if (s
->timer_expiry
!= -1) {
855 timer_mod(s
->timer
, s
->timer_expiry
);
861 static const MemoryRegionOps apic_io_ops
= {
863 .read
= { apic_mem_readb
, apic_mem_readw
, apic_mem_readl
, },
864 .write
= { apic_mem_writeb
, apic_mem_writew
, apic_mem_writel
, },
866 .endianness
= DEVICE_NATIVE_ENDIAN
,
869 static void apic_realize(DeviceState
*dev
, Error
**errp
)
871 APICCommonState
*s
= APIC_COMMON(dev
);
873 memory_region_init_io(&s
->io_memory
, OBJECT(s
), &apic_io_ops
, s
, "apic-msi",
876 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, apic_timer
, s
);
877 local_apics
[s
->idx
] = s
;
879 msi_nonbroken
= true;
882 static void apic_class_init(ObjectClass
*klass
, void *data
)
884 APICCommonClass
*k
= APIC_COMMON_CLASS(klass
);
886 k
->realize
= apic_realize
;
887 k
->set_base
= apic_set_base
;
888 k
->set_tpr
= apic_set_tpr
;
889 k
->get_tpr
= apic_get_tpr
;
890 k
->vapic_base_update
= apic_vapic_base_update
;
891 k
->external_nmi
= apic_external_nmi
;
892 k
->pre_save
= apic_pre_save
;
893 k
->post_load
= apic_post_load
;
896 static const TypeInfo apic_info
= {
898 .instance_size
= sizeof(APICCommonState
),
899 .parent
= TYPE_APIC_COMMON
,
900 .class_init
= apic_class_init
,
903 static void apic_register_types(void)
905 type_register_static(&apic_info
);
908 type_init(apic_register_types
)