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/thread.h"
21 #include "hw/i386/apic_internal.h"
22 #include "hw/i386/apic.h"
23 #include "hw/i386/ioapic.h"
24 #include "hw/pci/msi.h"
25 #include "qemu/host-utils.h"
27 #include "hw/i386/pc.h"
28 #include "hw/i386/apic-msidef.h"
30 #define MAX_APIC_WORDS 8
32 #define SYNC_FROM_VAPIC 0x1
33 #define SYNC_TO_VAPIC 0x2
34 #define SYNC_ISR_IRR_TO_VAPIC 0x4
36 static APICCommonState
*local_apics
[MAX_APICS
+ 1];
38 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
);
39 static void apic_update_irq(APICCommonState
*s
);
40 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
41 uint8_t dest
, uint8_t dest_mode
);
43 /* Find first bit starting from msb */
44 static int apic_fls_bit(uint32_t value
)
46 return 31 - clz32(value
);
49 /* Find first bit starting from lsb */
50 static int apic_ffs_bit(uint32_t value
)
55 static inline void apic_reset_bit(uint32_t *tab
, int index
)
59 mask
= 1 << (index
& 0x1f);
63 /* return -1 if no bit is set */
64 static int get_highest_priority_int(uint32_t *tab
)
67 for (i
= 7; i
>= 0; i
--) {
69 return i
* 32 + apic_fls_bit(tab
[i
]);
75 static void apic_sync_vapic(APICCommonState
*s
, int sync_type
)
77 VAPICState vapic_state
;
82 if (!s
->vapic_paddr
) {
85 if (sync_type
& SYNC_FROM_VAPIC
) {
86 cpu_physical_memory_read(s
->vapic_paddr
, &vapic_state
,
88 s
->tpr
= vapic_state
.tpr
;
90 if (sync_type
& (SYNC_TO_VAPIC
| SYNC_ISR_IRR_TO_VAPIC
)) {
91 start
= offsetof(VAPICState
, isr
);
92 length
= offsetof(VAPICState
, enabled
) - offsetof(VAPICState
, isr
);
94 if (sync_type
& SYNC_TO_VAPIC
) {
95 assert(qemu_cpu_is_self(CPU(s
->cpu
)));
97 vapic_state
.tpr
= s
->tpr
;
98 vapic_state
.enabled
= 1;
100 length
= sizeof(VAPICState
);
103 vector
= get_highest_priority_int(s
->isr
);
107 vapic_state
.isr
= vector
& 0xf0;
109 vapic_state
.zero
= 0;
111 vector
= get_highest_priority_int(s
->irr
);
115 vapic_state
.irr
= vector
& 0xff;
117 cpu_physical_memory_write_rom(&address_space_memory
,
118 s
->vapic_paddr
+ start
,
119 ((void *)&vapic_state
) + start
, length
);
123 static void apic_vapic_base_update(APICCommonState
*s
)
125 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
128 static void apic_local_deliver(APICCommonState
*s
, int vector
)
130 uint32_t lvt
= s
->lvt
[vector
];
133 trace_apic_local_deliver(vector
, (lvt
>> 8) & 7);
135 if (lvt
& APIC_LVT_MASKED
)
138 switch ((lvt
>> 8) & 7) {
140 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SMI
);
144 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_NMI
);
148 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_HARD
);
152 trigger_mode
= APIC_TRIGGER_EDGE
;
153 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
154 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
155 trigger_mode
= APIC_TRIGGER_LEVEL
;
156 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
160 void apic_deliver_pic_intr(DeviceState
*dev
, int level
)
162 APICCommonState
*s
= APIC_COMMON(dev
);
165 apic_local_deliver(s
, APIC_LVT_LINT0
);
167 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
169 switch ((lvt
>> 8) & 7) {
171 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
173 apic_reset_bit(s
->irr
, lvt
& 0xff);
182 static void apic_external_nmi(APICCommonState
*s
)
184 apic_local_deliver(s
, APIC_LVT_LINT1
);
187 #define foreach_apic(apic, deliver_bitmask, code) \
190 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
191 uint32_t __mask = deliver_bitmask[__i];\
193 for(__j = 0; __j < 32; __j++) {\
194 if (__mask & (1U << __j)) {\
195 apic = local_apics[__i * 32 + __j];\
205 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
206 uint8_t delivery_mode
, uint8_t vector_num
,
207 uint8_t trigger_mode
)
209 APICCommonState
*apic_iter
;
211 switch (delivery_mode
) {
213 /* XXX: search for focus processor, arbitration */
217 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
218 if (deliver_bitmask
[i
]) {
219 d
= i
* 32 + apic_ffs_bit(deliver_bitmask
[i
]);
224 apic_iter
= local_apics
[d
];
226 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
236 foreach_apic(apic_iter
, deliver_bitmask
,
237 cpu_interrupt(CPU(apic_iter
->cpu
), CPU_INTERRUPT_SMI
)
242 foreach_apic(apic_iter
, deliver_bitmask
,
243 cpu_interrupt(CPU(apic_iter
->cpu
), CPU_INTERRUPT_NMI
)
248 /* normal INIT IPI sent to processors */
249 foreach_apic(apic_iter
, deliver_bitmask
,
250 cpu_interrupt(CPU(apic_iter
->cpu
),
256 /* handled in I/O APIC code */
263 foreach_apic(apic_iter
, deliver_bitmask
,
264 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
267 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
, uint8_t delivery_mode
,
268 uint8_t vector_num
, uint8_t trigger_mode
)
270 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
272 trace_apic_deliver_irq(dest
, dest_mode
, delivery_mode
, vector_num
,
275 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
276 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, trigger_mode
);
279 static void apic_set_base(APICCommonState
*s
, uint64_t val
)
281 s
->apicbase
= (val
& 0xfffff000) |
282 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
283 /* if disabled, cannot be enabled again */
284 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
285 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
286 cpu_clear_apic_feature(&s
->cpu
->env
);
287 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
291 static void apic_set_tpr(APICCommonState
*s
, uint8_t val
)
293 /* Updates from cr8 are ignored while the VAPIC is active */
294 if (!s
->vapic_paddr
) {
300 static uint8_t apic_get_tpr(APICCommonState
*s
)
302 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
306 int apic_get_ppr(APICCommonState
*s
)
311 isrv
= get_highest_priority_int(s
->isr
);
322 static int apic_get_arb_pri(APICCommonState
*s
)
324 /* XXX: arbitration */
330 * <0 - low prio interrupt,
332 * >0 - interrupt number
334 static int apic_irq_pending(APICCommonState
*s
)
338 if (!(s
->spurious_vec
& APIC_SV_ENABLE
)) {
342 irrv
= get_highest_priority_int(s
->irr
);
346 ppr
= apic_get_ppr(s
);
347 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0)) {
354 /* signal the CPU if an irq is pending */
355 static void apic_update_irq(APICCommonState
*s
)
358 DeviceState
*dev
= (DeviceState
*)s
;
361 if (!qemu_cpu_is_self(cpu
)) {
362 cpu_interrupt(cpu
, CPU_INTERRUPT_POLL
);
363 } else if (apic_irq_pending(s
) > 0) {
364 cpu_interrupt(cpu
, CPU_INTERRUPT_HARD
);
365 } else if (!apic_accept_pic_intr(dev
) || !pic_get_output(isa_pic
)) {
366 cpu_reset_interrupt(cpu
, CPU_INTERRUPT_HARD
);
370 void apic_poll_irq(DeviceState
*dev
)
372 APICCommonState
*s
= APIC_COMMON(dev
);
374 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
378 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
)
380 apic_report_irq_delivered(!apic_get_bit(s
->irr
, vector_num
));
382 apic_set_bit(s
->irr
, vector_num
);
384 apic_set_bit(s
->tmr
, vector_num
);
386 apic_reset_bit(s
->tmr
, vector_num
);
387 if (s
->vapic_paddr
) {
388 apic_sync_vapic(s
, SYNC_ISR_IRR_TO_VAPIC
);
390 * The vcpu thread needs to see the new IRR before we pull its current
391 * TPR value. That way, if we miss a lowering of the TRP, the guest
392 * has the chance to notice the new IRR and poll for IRQs on its own.
395 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
400 static void apic_eoi(APICCommonState
*s
)
403 isrv
= get_highest_priority_int(s
->isr
);
406 apic_reset_bit(s
->isr
, isrv
);
407 if (!(s
->spurious_vec
& APIC_SV_DIRECTED_IO
) && apic_get_bit(s
->tmr
, isrv
)) {
408 ioapic_eoi_broadcast(isrv
);
410 apic_sync_vapic(s
, SYNC_FROM_VAPIC
| SYNC_TO_VAPIC
);
414 static int apic_find_dest(uint8_t dest
)
416 APICCommonState
*apic
= local_apics
[dest
];
419 if (apic
&& apic
->id
== dest
)
420 return dest
; /* shortcut in case apic->id == apic->idx */
422 for (i
= 0; i
< MAX_APICS
; i
++) {
423 apic
= local_apics
[i
];
424 if (apic
&& apic
->id
== dest
)
433 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
434 uint8_t dest
, uint8_t dest_mode
)
436 APICCommonState
*apic_iter
;
439 if (dest_mode
== 0) {
441 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
443 int idx
= apic_find_dest(dest
);
444 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
446 apic_set_bit(deliver_bitmask
, idx
);
449 /* XXX: cluster mode */
450 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
451 for(i
= 0; i
< MAX_APICS
; i
++) {
452 apic_iter
= local_apics
[i
];
454 if (apic_iter
->dest_mode
== 0xf) {
455 if (dest
& apic_iter
->log_dest
)
456 apic_set_bit(deliver_bitmask
, i
);
457 } else if (apic_iter
->dest_mode
== 0x0) {
458 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
459 (dest
& apic_iter
->log_dest
& 0x0f)) {
460 apic_set_bit(deliver_bitmask
, i
);
470 static void apic_startup(APICCommonState
*s
, int vector_num
)
472 s
->sipi_vector
= vector_num
;
473 cpu_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SIPI
);
476 void apic_sipi(DeviceState
*dev
)
478 APICCommonState
*s
= APIC_COMMON(dev
);
480 cpu_reset_interrupt(CPU(s
->cpu
), CPU_INTERRUPT_SIPI
);
482 if (!s
->wait_for_sipi
)
484 cpu_x86_load_seg_cache_sipi(s
->cpu
, s
->sipi_vector
);
485 s
->wait_for_sipi
= 0;
488 static void apic_deliver(DeviceState
*dev
, uint8_t dest
, uint8_t dest_mode
,
489 uint8_t delivery_mode
, uint8_t vector_num
,
490 uint8_t trigger_mode
)
492 APICCommonState
*s
= APIC_COMMON(dev
);
493 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
494 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
495 APICCommonState
*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 apic_set_bit(deliver_bitmask
, s
->idx
);
506 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
509 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
510 apic_reset_bit(deliver_bitmask
, s
->idx
);
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
, trigger_mode
);
536 static bool apic_check_pic(APICCommonState
*s
)
538 DeviceState
*dev
= (DeviceState
*)s
;
540 if (!apic_accept_pic_intr(dev
) || !pic_get_output(isa_pic
)) {
543 apic_deliver_pic_intr(dev
, 1);
547 int apic_get_interrupt(DeviceState
*dev
)
549 APICCommonState
*s
= APIC_COMMON(dev
);
552 /* if the APIC is installed or enabled, we let the 8259 handle the
556 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
559 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
560 intno
= apic_irq_pending(s
);
562 /* if there is an interrupt from the 8259, let the caller handle
563 * that first since ExtINT interrupts ignore the priority.
565 if (intno
== 0 || apic_check_pic(s
)) {
566 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
568 } else if (intno
< 0) {
569 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
570 return s
->spurious_vec
& 0xff;
572 apic_reset_bit(s
->irr
, intno
);
573 apic_set_bit(s
->isr
, intno
);
574 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
581 int apic_accept_pic_intr(DeviceState
*dev
)
583 APICCommonState
*s
= APIC_COMMON(dev
);
589 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
591 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
592 (lvt0
& APIC_LVT_MASKED
) == 0)
598 static uint32_t apic_get_current_count(APICCommonState
*s
)
602 d
= (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - s
->initial_count_load_time
) >>
604 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
606 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
608 if (d
>= s
->initial_count
)
611 val
= s
->initial_count
- d
;
616 static void apic_timer_update(APICCommonState
*s
, int64_t current_time
)
618 if (apic_next_timer(s
, current_time
)) {
619 timer_mod(s
->timer
, s
->next_time
);
625 static void apic_timer(void *opaque
)
627 APICCommonState
*s
= opaque
;
629 apic_local_deliver(s
, APIC_LVT_TIMER
);
630 apic_timer_update(s
, s
->next_time
);
633 static uint32_t apic_mem_readb(void *opaque
, hwaddr addr
)
638 static uint32_t apic_mem_readw(void *opaque
, hwaddr addr
)
643 static void apic_mem_writeb(void *opaque
, hwaddr addr
, uint32_t val
)
647 static void apic_mem_writew(void *opaque
, hwaddr addr
, uint32_t val
)
651 static uint32_t apic_mem_readl(void *opaque
, hwaddr addr
)
658 dev
= cpu_get_current_apic();
662 s
= APIC_COMMON(dev
);
664 index
= (addr
>> 4) & 0xff;
669 case 0x03: /* version */
670 val
= s
->version
| ((APIC_LVT_NB
- 1) << 16);
673 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
674 if (apic_report_tpr_access
) {
675 cpu_report_tpr_access(&s
->cpu
->env
, TPR_ACCESS_READ
);
680 val
= apic_get_arb_pri(s
);
684 val
= apic_get_ppr(s
);
690 val
= s
->log_dest
<< 24;
693 val
= (s
->dest_mode
<< 28) | 0xfffffff;
696 val
= s
->spurious_vec
;
699 val
= s
->isr
[index
& 7];
702 val
= s
->tmr
[index
& 7];
705 val
= s
->irr
[index
& 7];
712 val
= s
->icr
[index
& 1];
715 val
= s
->lvt
[index
- 0x32];
718 val
= s
->initial_count
;
721 val
= apic_get_current_count(s
);
724 val
= s
->divide_conf
;
727 s
->esr
|= APIC_ESR_ILLEGAL_ADDRESS
;
731 trace_apic_mem_readl(addr
, val
);
735 static void apic_send_msi(hwaddr addr
, uint32_t data
)
737 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
738 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
739 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
740 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
741 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
742 /* XXX: Ignore redirection hint. */
743 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, trigger_mode
);
746 static void apic_mem_writel(void *opaque
, hwaddr addr
, uint32_t val
)
750 int index
= (addr
>> 4) & 0xff;
751 if (addr
> 0xfff || !index
) {
752 /* MSI and MMIO APIC are at the same memory location,
753 * but actually not on the global bus: MSI is on PCI bus
754 * APIC is connected directly to the CPU.
755 * Mapping them on the global bus happens to work because
756 * MSI registers are reserved in APIC MMIO and vice versa. */
757 apic_send_msi(addr
, val
);
761 dev
= cpu_get_current_apic();
765 s
= APIC_COMMON(dev
);
767 trace_apic_mem_writel(addr
, val
);
776 if (apic_report_tpr_access
) {
777 cpu_report_tpr_access(&s
->cpu
->env
, TPR_ACCESS_WRITE
);
780 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
790 s
->log_dest
= val
>> 24;
793 s
->dest_mode
= val
>> 28;
796 s
->spurious_vec
= val
& 0x1ff;
806 apic_deliver(dev
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
807 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
808 (s
->icr
[0] >> 15) & 1);
815 int n
= index
- 0x32;
817 if (n
== APIC_LVT_TIMER
) {
818 apic_timer_update(s
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
819 } else if (n
== APIC_LVT_LINT0
&& apic_check_pic(s
)) {
825 s
->initial_count
= val
;
826 s
->initial_count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
827 apic_timer_update(s
, s
->initial_count_load_time
);
834 s
->divide_conf
= val
& 0xb;
835 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
836 s
->count_shift
= (v
+ 1) & 7;
840 s
->esr
|= APIC_ESR_ILLEGAL_ADDRESS
;
845 static void apic_pre_save(APICCommonState
*s
)
847 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
850 static void apic_post_load(APICCommonState
*s
)
852 if (s
->timer_expiry
!= -1) {
853 timer_mod(s
->timer
, s
->timer_expiry
);
859 static const MemoryRegionOps apic_io_ops
= {
861 .read
= { apic_mem_readb
, apic_mem_readw
, apic_mem_readl
, },
862 .write
= { apic_mem_writeb
, apic_mem_writew
, apic_mem_writel
, },
864 .endianness
= DEVICE_NATIVE_ENDIAN
,
867 static void apic_realize(DeviceState
*dev
, Error
**errp
)
869 APICCommonState
*s
= APIC_COMMON(dev
);
871 memory_region_init_io(&s
->io_memory
, OBJECT(s
), &apic_io_ops
, s
, "apic-msi",
874 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, apic_timer
, s
);
875 local_apics
[s
->idx
] = s
;
877 msi_nonbroken
= true;
880 static void apic_class_init(ObjectClass
*klass
, void *data
)
882 APICCommonClass
*k
= APIC_COMMON_CLASS(klass
);
884 k
->realize
= apic_realize
;
885 k
->set_base
= apic_set_base
;
886 k
->set_tpr
= apic_set_tpr
;
887 k
->get_tpr
= apic_get_tpr
;
888 k
->vapic_base_update
= apic_vapic_base_update
;
889 k
->external_nmi
= apic_external_nmi
;
890 k
->pre_save
= apic_pre_save
;
891 k
->post_load
= apic_post_load
;
894 static const TypeInfo apic_info
= {
896 .instance_size
= sizeof(APICCommonState
),
897 .parent
= TYPE_APIC_COMMON
,
898 .class_init
= apic_class_init
,
901 static void apic_register_types(void)
903 type_register_static(&apic_info
);
906 type_init(apic_register_types
)