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 "apic_internal.h"
23 #include "host-utils.h"
26 #include "apic-msidef.h"
28 #define MAX_APIC_WORDS 8
30 #define SYNC_FROM_VAPIC 0x1
31 #define SYNC_TO_VAPIC 0x2
32 #define SYNC_ISR_IRR_TO_VAPIC 0x4
34 static APICCommonState *local_apics[MAX_APICS + 1];
36 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
37 static void apic_update_irq(APICCommonState *s);
38 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
39 uint8_t dest, uint8_t dest_mode);
41 /* Find first bit starting from msb */
42 static int fls_bit(uint32_t value)
44 return 31 - clz32(value);
47 /* Find first bit starting from lsb */
48 static int ffs_bit(uint32_t value)
53 static inline void set_bit(uint32_t *tab, int index)
57 mask = 1 << (index & 0x1f);
61 static inline void reset_bit(uint32_t *tab, int index)
65 mask = 1 << (index & 0x1f);
69 static inline int get_bit(uint32_t *tab, int index)
73 mask = 1 << (index & 0x1f);
74 return !!(tab[i] & mask);
77 /* return -1 if no bit is set */
78 static int get_highest_priority_int(uint32_t *tab)
81 for (i = 7; i >= 0; i--) {
83 return i * 32 + fls_bit(tab[i]);
89 static void apic_sync_vapic(APICCommonState *s, int sync_type)
91 VAPICState vapic_state;
96 if (!s->vapic_paddr) {
99 if (sync_type & SYNC_FROM_VAPIC) {
100 cpu_physical_memory_rw(s->vapic_paddr, (void *)&vapic_state,
101 sizeof(vapic_state), 0);
102 s->tpr = vapic_state.tpr;
104 if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
105 start = offsetof(VAPICState, isr);
106 length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
108 if (sync_type & SYNC_TO_VAPIC) {
109 assert(qemu_cpu_is_self(s->cpu_env));
111 vapic_state.tpr = s->tpr;
112 vapic_state.enabled = 1;
114 length = sizeof(VAPICState);
117 vector = get_highest_priority_int(s->isr);
121 vapic_state.isr = vector & 0xf0;
123 vapic_state.zero = 0;
125 vector = get_highest_priority_int(s->irr);
129 vapic_state.irr = vector & 0xff;
131 cpu_physical_memory_write_rom(s->vapic_paddr + start,
132 ((void *)&vapic_state) + start, length);
136 static void apic_vapic_base_update(APICCommonState *s)
138 apic_sync_vapic(s, SYNC_TO_VAPIC);
141 static void apic_local_deliver(APICCommonState *s, int vector)
143 uint32_t lvt = s->lvt[vector];
146 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
148 if (lvt & APIC_LVT_MASKED)
151 switch ((lvt >> 8) & 7) {
153 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
157 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
161 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
165 trigger_mode = APIC_TRIGGER_EDGE;
166 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
167 (lvt & APIC_LVT_LEVEL_TRIGGER))
168 trigger_mode = APIC_TRIGGER_LEVEL;
169 apic_set_irq(s, lvt & 0xff, trigger_mode);
173 void apic_deliver_pic_intr(DeviceState *d, int level)
175 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
178 apic_local_deliver(s, APIC_LVT_LINT0);
180 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
182 switch ((lvt >> 8) & 7) {
184 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
186 reset_bit(s->irr, lvt & 0xff);
189 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
195 static void apic_external_nmi(APICCommonState *s)
197 apic_local_deliver(s, APIC_LVT_LINT1);
200 #define foreach_apic(apic, deliver_bitmask, code) \
202 int __i, __j, __mask;\
203 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
204 __mask = deliver_bitmask[__i];\
206 for(__j = 0; __j < 32; __j++) {\
207 if (__mask & (1 << __j)) {\
208 apic = local_apics[__i * 32 + __j];\
218 static void apic_bus_deliver(const uint32_t *deliver_bitmask,
219 uint8_t delivery_mode, uint8_t vector_num,
220 uint8_t trigger_mode)
222 APICCommonState *apic_iter;
224 switch (delivery_mode) {
226 /* XXX: search for focus processor, arbitration */
230 for(i = 0; i < MAX_APIC_WORDS; i++) {
231 if (deliver_bitmask[i]) {
232 d = i * 32 + ffs_bit(deliver_bitmask[i]);
237 apic_iter = local_apics[d];
239 apic_set_irq(apic_iter, vector_num, trigger_mode);
249 foreach_apic(apic_iter, deliver_bitmask,
250 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
254 foreach_apic(apic_iter, deliver_bitmask,
255 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
259 /* normal INIT IPI sent to processors */
260 foreach_apic(apic_iter, deliver_bitmask,
261 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
265 /* handled in I/O APIC code */
272 foreach_apic(apic_iter, deliver_bitmask,
273 apic_set_irq(apic_iter, vector_num, trigger_mode) );
276 void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
277 uint8_t vector_num, uint8_t trigger_mode)
279 uint32_t deliver_bitmask[MAX_APIC_WORDS];
281 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
284 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
285 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
288 static void apic_set_base(APICCommonState *s, uint64_t val)
290 s->apicbase = (val & 0xfffff000) |
291 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
292 /* if disabled, cannot be enabled again */
293 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
294 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
295 cpu_clear_apic_feature(s->cpu_env);
296 s->spurious_vec &= ~APIC_SV_ENABLE;
300 static void apic_set_tpr(APICCommonState *s, uint8_t val)
302 /* Updates from cr8 are ignored while the VAPIC is active */
303 if (!s->vapic_paddr) {
309 static uint8_t apic_get_tpr(APICCommonState *s)
311 apic_sync_vapic(s, SYNC_FROM_VAPIC);
315 static int apic_get_ppr(APICCommonState *s)
320 isrv = get_highest_priority_int(s->isr);
331 static int apic_get_arb_pri(APICCommonState *s)
333 /* XXX: arbitration */
339 * <0 - low prio interrupt,
341 * >0 - interrupt number
343 static int apic_irq_pending(APICCommonState *s)
346 irrv = get_highest_priority_int(s->irr);
350 ppr = apic_get_ppr(s);
351 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
358 /* signal the CPU if an irq is pending */
359 static void apic_update_irq(APICCommonState *s)
361 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
364 if (apic_irq_pending(s) > 0) {
365 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
369 void apic_poll_irq(DeviceState *d)
371 APICCommonState *s = APIC_COMMON(d);
373 apic_sync_vapic(s, SYNC_FROM_VAPIC);
377 static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
379 apic_report_irq_delivered(!get_bit(s->irr, vector_num));
381 set_bit(s->irr, vector_num);
383 set_bit(s->tmr, vector_num);
385 reset_bit(s->tmr, vector_num);
386 if (s->vapic_paddr) {
387 apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
389 * The vcpu thread needs to see the new IRR before we pull its current
390 * TPR value. That way, if we miss a lowering of the TRP, the guest
391 * has the chance to notice the new IRR and poll for IRQs on its own.
394 apic_sync_vapic(s, SYNC_FROM_VAPIC);
399 static void apic_eoi(APICCommonState *s)
402 isrv = get_highest_priority_int(s->isr);
405 reset_bit(s->isr, isrv);
406 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && get_bit(s->tmr, isrv)) {
407 ioapic_eoi_broadcast(isrv);
409 apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
413 static int apic_find_dest(uint8_t dest)
415 APICCommonState *apic = local_apics[dest];
418 if (apic && apic->id == dest)
419 return dest; /* shortcut in case apic->id == apic->idx */
421 for (i = 0; i < MAX_APICS; i++) {
422 apic = local_apics[i];
423 if (apic && apic->id == dest)
432 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
433 uint8_t dest, uint8_t dest_mode)
435 APICCommonState *apic_iter;
438 if (dest_mode == 0) {
440 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
442 int idx = apic_find_dest(dest);
443 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
445 set_bit(deliver_bitmask, idx);
448 /* XXX: cluster mode */
449 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
450 for(i = 0; i < MAX_APICS; i++) {
451 apic_iter = local_apics[i];
453 if (apic_iter->dest_mode == 0xf) {
454 if (dest & apic_iter->log_dest)
455 set_bit(deliver_bitmask, i);
456 } else if (apic_iter->dest_mode == 0x0) {
457 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
458 (dest & apic_iter->log_dest & 0x0f)) {
459 set_bit(deliver_bitmask, i);
469 static void apic_startup(APICCommonState *s, int vector_num)
471 s->sipi_vector = vector_num;
472 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
475 void apic_sipi(DeviceState *d)
477 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
479 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
481 if (!s->wait_for_sipi)
483 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
484 s->wait_for_sipi = 0;
487 static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
488 uint8_t delivery_mode, uint8_t vector_num,
489 uint8_t trigger_mode)
491 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
492 uint32_t deliver_bitmask[MAX_APIC_WORDS];
493 int dest_shorthand = (s->icr[0] >> 18) & 3;
494 APICCommonState *apic_iter;
496 switch (dest_shorthand) {
498 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
501 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
502 set_bit(deliver_bitmask, s->idx);
505 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
508 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
509 reset_bit(deliver_bitmask, s->idx);
513 switch (delivery_mode) {
516 int trig_mode = (s->icr[0] >> 15) & 1;
517 int level = (s->icr[0] >> 14) & 1;
518 if (level == 0 && trig_mode == 1) {
519 foreach_apic(apic_iter, deliver_bitmask,
520 apic_iter->arb_id = apic_iter->id );
527 foreach_apic(apic_iter, deliver_bitmask,
528 apic_startup(apic_iter, vector_num) );
532 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
535 static bool apic_check_pic(APICCommonState *s)
537 if (!apic_accept_pic_intr(&s->busdev.qdev) || !pic_get_output(isa_pic)) {
540 apic_deliver_pic_intr(&s->busdev.qdev, 1);
544 int apic_get_interrupt(DeviceState *d)
546 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
549 /* if the APIC is installed or enabled, we let the 8259 handle the
553 if (!(s->spurious_vec & APIC_SV_ENABLE))
556 apic_sync_vapic(s, SYNC_FROM_VAPIC);
557 intno = apic_irq_pending(s);
560 apic_sync_vapic(s, SYNC_TO_VAPIC);
562 } else if (intno < 0) {
563 apic_sync_vapic(s, SYNC_TO_VAPIC);
564 return s->spurious_vec & 0xff;
566 reset_bit(s->irr, intno);
567 set_bit(s->isr, intno);
568 apic_sync_vapic(s, SYNC_TO_VAPIC);
570 /* re-inject if there is still a pending PIC interrupt */
578 int apic_accept_pic_intr(DeviceState *d)
580 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
586 lvt0 = s->lvt[APIC_LVT_LINT0];
588 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
589 (lvt0 & APIC_LVT_MASKED) == 0)
595 static uint32_t apic_get_current_count(APICCommonState *s)
599 d = (qemu_get_clock_ns(vm_clock) - s->initial_count_load_time) >>
601 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
603 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
605 if (d >= s->initial_count)
608 val = s->initial_count - d;
613 static void apic_timer_update(APICCommonState *s, int64_t current_time)
615 if (apic_next_timer(s, current_time)) {
616 qemu_mod_timer(s->timer, s->next_time);
618 qemu_del_timer(s->timer);
622 static void apic_timer(void *opaque)
624 APICCommonState *s = opaque;
626 apic_local_deliver(s, APIC_LVT_TIMER);
627 apic_timer_update(s, s->next_time);
630 static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
635 static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
640 static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
644 static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
648 static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
655 d = cpu_get_current_apic();
659 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
661 index = (addr >> 4) & 0xff;
666 case 0x03: /* version */
667 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
670 apic_sync_vapic(s, SYNC_FROM_VAPIC);
671 if (apic_report_tpr_access) {
672 cpu_report_tpr_access(s->cpu_env, TPR_ACCESS_READ);
677 val = apic_get_arb_pri(s);
681 val = apic_get_ppr(s);
687 val = s->log_dest << 24;
690 val = s->dest_mode << 28;
693 val = s->spurious_vec;
696 val = s->isr[index & 7];
699 val = s->tmr[index & 7];
702 val = s->irr[index & 7];
709 val = s->icr[index & 1];
712 val = s->lvt[index - 0x32];
715 val = s->initial_count;
718 val = apic_get_current_count(s);
721 val = s->divide_conf;
724 s->esr |= ESR_ILLEGAL_ADDRESS;
728 trace_apic_mem_readl(addr, val);
732 static void apic_send_msi(target_phys_addr_t addr, uint32_t data)
734 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
735 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
736 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
737 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
738 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
739 /* XXX: Ignore redirection hint. */
740 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
743 static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
747 int index = (addr >> 4) & 0xff;
748 if (addr > 0xfff || !index) {
749 /* MSI and MMIO APIC are at the same memory location,
750 * but actually not on the global bus: MSI is on PCI bus
751 * APIC is connected directly to the CPU.
752 * Mapping them on the global bus happens to work because
753 * MSI registers are reserved in APIC MMIO and vice versa. */
754 apic_send_msi(addr, val);
758 d = cpu_get_current_apic();
762 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
764 trace_apic_mem_writel(addr, val);
773 if (apic_report_tpr_access) {
774 cpu_report_tpr_access(s->cpu_env, TPR_ACCESS_WRITE);
777 apic_sync_vapic(s, SYNC_TO_VAPIC);
787 s->log_dest = val >> 24;
790 s->dest_mode = val >> 28;
793 s->spurious_vec = val & 0x1ff;
803 apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
804 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
805 (s->icr[0] >> 15) & 1);
812 int n = index - 0x32;
814 if (n == APIC_LVT_TIMER) {
815 apic_timer_update(s, qemu_get_clock_ns(vm_clock));
816 } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
822 s->initial_count = val;
823 s->initial_count_load_time = qemu_get_clock_ns(vm_clock);
824 apic_timer_update(s, s->initial_count_load_time);
831 s->divide_conf = val & 0xb;
832 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
833 s->count_shift = (v + 1) & 7;
837 s->esr |= ESR_ILLEGAL_ADDRESS;
842 static void apic_pre_save(APICCommonState *s)
844 apic_sync_vapic(s, SYNC_FROM_VAPIC);
847 static void apic_post_load(APICCommonState *s)
849 if (s->timer_expiry != -1) {
850 qemu_mod_timer(s->timer, s->timer_expiry);
852 qemu_del_timer(s->timer);
856 static const MemoryRegionOps apic_io_ops = {
858 .read = { apic_mem_readb, apic_mem_readw, apic_mem_readl, },
859 .write = { apic_mem_writeb, apic_mem_writew, apic_mem_writel, },
861 .endianness = DEVICE_NATIVE_ENDIAN,
864 static void apic_init(APICCommonState *s)
866 memory_region_init_io(&s->io_memory, &apic_io_ops, s, "apic-msi",
869 s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s);
870 local_apics[s->idx] = s;
872 msi_supported = true;
875 static void apic_class_init(ObjectClass *klass, void *data)
877 APICCommonClass *k = APIC_COMMON_CLASS(klass);
880 k->set_base = apic_set_base;
881 k->set_tpr = apic_set_tpr;
882 k->get_tpr = apic_get_tpr;
883 k->vapic_base_update = apic_vapic_base_update;
884 k->external_nmi = apic_external_nmi;
885 k->pre_save = apic_pre_save;
886 k->post_load = apic_post_load;
889 static TypeInfo apic_info = {
891 .instance_size = sizeof(APICCommonState),
892 .parent = TYPE_APIC_COMMON,
893 .class_init = apic_class_init,
896 static void apic_register_types(void)
898 type_register_static(&apic_info);
901 type_init(apic_register_types)