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"
22 #include "host-utils.h"
26 #define MAX_APIC_WORDS 8
28 /* Intel APIC constants: from include/asm/msidef.h */
29 #define MSI_DATA_VECTOR_SHIFT 0
30 #define MSI_DATA_VECTOR_MASK 0x000000ff
31 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
32 #define MSI_DATA_TRIGGER_SHIFT 15
33 #define MSI_DATA_LEVEL_SHIFT 14
34 #define MSI_ADDR_DEST_MODE_SHIFT 2
35 #define MSI_ADDR_DEST_ID_SHIFT 12
36 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
38 #define SYNC_FROM_VAPIC 0x1
39 #define SYNC_TO_VAPIC 0x2
40 #define SYNC_ISR_IRR_TO_VAPIC 0x4
42 static APICCommonState
*local_apics
[MAX_APICS
+ 1];
44 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
);
45 static void apic_update_irq(APICCommonState
*s
);
46 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
47 uint8_t dest
, uint8_t dest_mode
);
49 /* Find first bit starting from msb */
50 static int fls_bit(uint32_t value
)
52 return 31 - clz32(value
);
55 /* Find first bit starting from lsb */
56 static int ffs_bit(uint32_t value
)
61 static inline void set_bit(uint32_t *tab
, int index
)
65 mask
= 1 << (index
& 0x1f);
69 static inline void reset_bit(uint32_t *tab
, int index
)
73 mask
= 1 << (index
& 0x1f);
77 static inline int get_bit(uint32_t *tab
, int index
)
81 mask
= 1 << (index
& 0x1f);
82 return !!(tab
[i
] & mask
);
85 /* return -1 if no bit is set */
86 static int get_highest_priority_int(uint32_t *tab
)
89 for (i
= 7; i
>= 0; i
--) {
91 return i
* 32 + fls_bit(tab
[i
]);
97 static void apic_sync_vapic(APICCommonState
*s
, int sync_type
)
99 VAPICState vapic_state
;
104 if (!s
->vapic_paddr
) {
107 if (sync_type
& SYNC_FROM_VAPIC
) {
108 cpu_physical_memory_rw(s
->vapic_paddr
, (void *)&vapic_state
,
109 sizeof(vapic_state
), 0);
110 s
->tpr
= vapic_state
.tpr
;
112 if (sync_type
& (SYNC_TO_VAPIC
| SYNC_ISR_IRR_TO_VAPIC
)) {
113 start
= offsetof(VAPICState
, isr
);
114 length
= offsetof(VAPICState
, enabled
) - offsetof(VAPICState
, isr
);
116 if (sync_type
& SYNC_TO_VAPIC
) {
117 assert(qemu_cpu_is_self(s
->cpu_env
));
119 vapic_state
.tpr
= s
->tpr
;
120 vapic_state
.enabled
= 1;
122 length
= sizeof(VAPICState
);
125 vector
= get_highest_priority_int(s
->isr
);
129 vapic_state
.isr
= vector
& 0xf0;
131 vapic_state
.zero
= 0;
133 vector
= get_highest_priority_int(s
->irr
);
137 vapic_state
.irr
= vector
& 0xff;
139 cpu_physical_memory_write_rom(s
->vapic_paddr
+ start
,
140 ((void *)&vapic_state
) + start
, length
);
144 static void apic_vapic_base_update(APICCommonState
*s
)
146 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
149 static void apic_local_deliver(APICCommonState
*s
, int vector
)
151 uint32_t lvt
= s
->lvt
[vector
];
154 trace_apic_local_deliver(vector
, (lvt
>> 8) & 7);
156 if (lvt
& APIC_LVT_MASKED
)
159 switch ((lvt
>> 8) & 7) {
161 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SMI
);
165 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_NMI
);
169 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
173 trigger_mode
= APIC_TRIGGER_EDGE
;
174 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
175 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
176 trigger_mode
= APIC_TRIGGER_LEVEL
;
177 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
181 void apic_deliver_pic_intr(DeviceState
*d
, int level
)
183 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
186 apic_local_deliver(s
, APIC_LVT_LINT0
);
188 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
190 switch ((lvt
>> 8) & 7) {
192 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
194 reset_bit(s
->irr
, lvt
& 0xff);
197 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
203 static void apic_external_nmi(APICCommonState
*s
)
205 apic_local_deliver(s
, APIC_LVT_LINT1
);
208 #define foreach_apic(apic, deliver_bitmask, code) \
210 int __i, __j, __mask;\
211 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
212 __mask = deliver_bitmask[__i];\
214 for(__j = 0; __j < 32; __j++) {\
215 if (__mask & (1 << __j)) {\
216 apic = local_apics[__i * 32 + __j];\
226 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
227 uint8_t delivery_mode
, uint8_t vector_num
,
228 uint8_t trigger_mode
)
230 APICCommonState
*apic_iter
;
232 switch (delivery_mode
) {
234 /* XXX: search for focus processor, arbitration */
238 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
239 if (deliver_bitmask
[i
]) {
240 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
245 apic_iter
= local_apics
[d
];
247 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
257 foreach_apic(apic_iter
, deliver_bitmask
,
258 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
262 foreach_apic(apic_iter
, deliver_bitmask
,
263 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
267 /* normal INIT IPI sent to processors */
268 foreach_apic(apic_iter
, deliver_bitmask
,
269 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_INIT
) );
273 /* handled in I/O APIC code */
280 foreach_apic(apic_iter
, deliver_bitmask
,
281 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
284 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
, uint8_t delivery_mode
,
285 uint8_t vector_num
, uint8_t trigger_mode
)
287 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
289 trace_apic_deliver_irq(dest
, dest_mode
, delivery_mode
, vector_num
,
292 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
293 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, trigger_mode
);
296 static void apic_set_base(APICCommonState
*s
, uint64_t val
)
298 s
->apicbase
= (val
& 0xfffff000) |
299 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
300 /* if disabled, cannot be enabled again */
301 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
302 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
303 cpu_clear_apic_feature(s
->cpu_env
);
304 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
308 static void apic_set_tpr(APICCommonState
*s
, uint8_t val
)
310 /* Updates from cr8 are ignored while the VAPIC is active */
311 if (!s
->vapic_paddr
) {
317 static uint8_t apic_get_tpr(APICCommonState
*s
)
319 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
323 static int apic_get_ppr(APICCommonState
*s
)
328 isrv
= get_highest_priority_int(s
->isr
);
339 static int apic_get_arb_pri(APICCommonState
*s
)
341 /* XXX: arbitration */
347 * <0 - low prio interrupt,
349 * >0 - interrupt number
351 static int apic_irq_pending(APICCommonState
*s
)
354 irrv
= get_highest_priority_int(s
->irr
);
358 ppr
= apic_get_ppr(s
);
359 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0)) {
366 /* signal the CPU if an irq is pending */
367 static void apic_update_irq(APICCommonState
*s
)
369 if (!(s
->spurious_vec
& APIC_SV_ENABLE
)) {
372 if (apic_irq_pending(s
) > 0) {
373 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
374 } else if (apic_accept_pic_intr(&s
->busdev
.qdev
) &&
375 pic_get_output(isa_pic
)) {
376 apic_deliver_pic_intr(&s
->busdev
.qdev
, 1);
380 void apic_poll_irq(DeviceState
*d
)
382 APICCommonState
*s
= APIC_COMMON(d
);
384 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
388 static void apic_set_irq(APICCommonState
*s
, int vector_num
, int trigger_mode
)
390 apic_report_irq_delivered(!get_bit(s
->irr
, vector_num
));
392 set_bit(s
->irr
, vector_num
);
394 set_bit(s
->tmr
, vector_num
);
396 reset_bit(s
->tmr
, vector_num
);
397 if (s
->vapic_paddr
) {
398 apic_sync_vapic(s
, SYNC_ISR_IRR_TO_VAPIC
);
400 * The vcpu thread needs to see the new IRR before we pull its current
401 * TPR value. That way, if we miss a lowering of the TRP, the guest
402 * has the chance to notice the new IRR and poll for IRQs on its own.
405 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
410 static void apic_eoi(APICCommonState
*s
)
413 isrv
= get_highest_priority_int(s
->isr
);
416 reset_bit(s
->isr
, isrv
);
417 if (!(s
->spurious_vec
& APIC_SV_DIRECTED_IO
) && get_bit(s
->tmr
, isrv
)) {
418 ioapic_eoi_broadcast(isrv
);
420 apic_sync_vapic(s
, SYNC_FROM_VAPIC
| SYNC_TO_VAPIC
);
424 static int apic_find_dest(uint8_t dest
)
426 APICCommonState
*apic
= local_apics
[dest
];
429 if (apic
&& apic
->id
== dest
)
430 return dest
; /* shortcut in case apic->id == apic->idx */
432 for (i
= 0; i
< MAX_APICS
; i
++) {
433 apic
= local_apics
[i
];
434 if (apic
&& apic
->id
== dest
)
443 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
444 uint8_t dest
, uint8_t dest_mode
)
446 APICCommonState
*apic_iter
;
449 if (dest_mode
== 0) {
451 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
453 int idx
= apic_find_dest(dest
);
454 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
456 set_bit(deliver_bitmask
, idx
);
459 /* XXX: cluster mode */
460 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
461 for(i
= 0; i
< MAX_APICS
; i
++) {
462 apic_iter
= local_apics
[i
];
464 if (apic_iter
->dest_mode
== 0xf) {
465 if (dest
& apic_iter
->log_dest
)
466 set_bit(deliver_bitmask
, i
);
467 } else if (apic_iter
->dest_mode
== 0x0) {
468 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
469 (dest
& apic_iter
->log_dest
& 0x0f)) {
470 set_bit(deliver_bitmask
, i
);
480 static void apic_startup(APICCommonState
*s
, int vector_num
)
482 s
->sipi_vector
= vector_num
;
483 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
486 void apic_sipi(DeviceState
*d
)
488 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
490 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
492 if (!s
->wait_for_sipi
)
494 cpu_x86_load_seg_cache_sipi(s
->cpu_env
, s
->sipi_vector
);
495 s
->wait_for_sipi
= 0;
498 static void apic_deliver(DeviceState
*d
, uint8_t dest
, uint8_t dest_mode
,
499 uint8_t delivery_mode
, uint8_t vector_num
,
500 uint8_t trigger_mode
)
502 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
503 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
504 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
505 APICCommonState
*apic_iter
;
507 switch (dest_shorthand
) {
509 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
512 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
513 set_bit(deliver_bitmask
, s
->idx
);
516 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
519 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
520 reset_bit(deliver_bitmask
, s
->idx
);
524 switch (delivery_mode
) {
527 int trig_mode
= (s
->icr
[0] >> 15) & 1;
528 int level
= (s
->icr
[0] >> 14) & 1;
529 if (level
== 0 && trig_mode
== 1) {
530 foreach_apic(apic_iter
, deliver_bitmask
,
531 apic_iter
->arb_id
= apic_iter
->id
);
538 foreach_apic(apic_iter
, deliver_bitmask
,
539 apic_startup(apic_iter
, vector_num
) );
543 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, trigger_mode
);
546 int apic_get_interrupt(DeviceState
*d
)
548 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
551 /* if the APIC is installed or enabled, we let the 8259 handle the
555 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
558 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
559 intno
= apic_irq_pending(s
);
562 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
564 } else if (intno
< 0) {
565 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
566 return s
->spurious_vec
& 0xff;
568 reset_bit(s
->irr
, intno
);
569 set_bit(s
->isr
, intno
);
570 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
575 int apic_accept_pic_intr(DeviceState
*d
)
577 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
583 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
585 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
586 (lvt0
& APIC_LVT_MASKED
) == 0)
592 static uint32_t apic_get_current_count(APICCommonState
*s
)
596 d
= (qemu_get_clock_ns(vm_clock
) - s
->initial_count_load_time
) >>
598 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
600 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
602 if (d
>= s
->initial_count
)
605 val
= s
->initial_count
- d
;
610 static void apic_timer_update(APICCommonState
*s
, int64_t current_time
)
612 if (apic_next_timer(s
, current_time
)) {
613 qemu_mod_timer(s
->timer
, s
->next_time
);
615 qemu_del_timer(s
->timer
);
619 static void apic_timer(void *opaque
)
621 APICCommonState
*s
= opaque
;
623 apic_local_deliver(s
, APIC_LVT_TIMER
);
624 apic_timer_update(s
, s
->next_time
);
627 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
632 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
637 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
641 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
645 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
652 d
= cpu_get_current_apic();
656 s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
658 index
= (addr
>> 4) & 0xff;
663 case 0x03: /* version */
664 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
667 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
668 if (apic_report_tpr_access
) {
669 cpu_report_tpr_access(s
->cpu_env
, TPR_ACCESS_READ
);
674 val
= apic_get_arb_pri(s
);
678 val
= apic_get_ppr(s
);
684 val
= s
->log_dest
<< 24;
687 val
= s
->dest_mode
<< 28;
690 val
= s
->spurious_vec
;
693 val
= s
->isr
[index
& 7];
696 val
= s
->tmr
[index
& 7];
699 val
= s
->irr
[index
& 7];
706 val
= s
->icr
[index
& 1];
709 val
= s
->lvt
[index
- 0x32];
712 val
= s
->initial_count
;
715 val
= apic_get_current_count(s
);
718 val
= s
->divide_conf
;
721 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
725 trace_apic_mem_readl(addr
, val
);
729 static void apic_send_msi(target_phys_addr_t addr
, uint32_t data
)
731 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
732 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
733 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
734 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
735 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
736 /* XXX: Ignore redirection hint. */
737 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, trigger_mode
);
740 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
744 int index
= (addr
>> 4) & 0xff;
745 if (addr
> 0xfff || !index
) {
746 /* MSI and MMIO APIC are at the same memory location,
747 * but actually not on the global bus: MSI is on PCI bus
748 * APIC is connected directly to the CPU.
749 * Mapping them on the global bus happens to work because
750 * MSI registers are reserved in APIC MMIO and vice versa. */
751 apic_send_msi(addr
, val
);
755 d
= cpu_get_current_apic();
759 s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
761 trace_apic_mem_writel(addr
, val
);
770 if (apic_report_tpr_access
) {
771 cpu_report_tpr_access(s
->cpu_env
, TPR_ACCESS_WRITE
);
774 apic_sync_vapic(s
, SYNC_TO_VAPIC
);
784 s
->log_dest
= val
>> 24;
787 s
->dest_mode
= val
>> 28;
790 s
->spurious_vec
= val
& 0x1ff;
800 apic_deliver(d
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
801 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
802 (s
->icr
[0] >> 15) & 1);
809 int n
= index
- 0x32;
811 if (n
== APIC_LVT_TIMER
)
812 apic_timer_update(s
, qemu_get_clock_ns(vm_clock
));
816 s
->initial_count
= val
;
817 s
->initial_count_load_time
= qemu_get_clock_ns(vm_clock
);
818 apic_timer_update(s
, s
->initial_count_load_time
);
825 s
->divide_conf
= val
& 0xb;
826 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
827 s
->count_shift
= (v
+ 1) & 7;
831 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
836 static void apic_pre_save(APICCommonState
*s
)
838 apic_sync_vapic(s
, SYNC_FROM_VAPIC
);
841 static void apic_post_load(APICCommonState
*s
)
843 if (s
->timer_expiry
!= -1) {
844 qemu_mod_timer(s
->timer
, s
->timer_expiry
);
846 qemu_del_timer(s
->timer
);
850 static const MemoryRegionOps apic_io_ops
= {
852 .read
= { apic_mem_readb
, apic_mem_readw
, apic_mem_readl
, },
853 .write
= { apic_mem_writeb
, apic_mem_writew
, apic_mem_writel
, },
855 .endianness
= DEVICE_NATIVE_ENDIAN
,
858 static void apic_init(APICCommonState
*s
)
860 memory_region_init_io(&s
->io_memory
, &apic_io_ops
, s
, "apic-msi",
863 s
->timer
= qemu_new_timer_ns(vm_clock
, apic_timer
, s
);
864 local_apics
[s
->idx
] = s
;
867 static void apic_class_init(ObjectClass
*klass
, void *data
)
869 APICCommonClass
*k
= APIC_COMMON_CLASS(klass
);
872 k
->set_base
= apic_set_base
;
873 k
->set_tpr
= apic_set_tpr
;
874 k
->get_tpr
= apic_get_tpr
;
875 k
->vapic_base_update
= apic_vapic_base_update
;
876 k
->external_nmi
= apic_external_nmi
;
877 k
->pre_save
= apic_pre_save
;
878 k
->post_load
= apic_post_load
;
881 static TypeInfo apic_info
= {
883 .instance_size
= sizeof(APICCommonState
),
884 .parent
= TYPE_APIC_COMMON
,
885 .class_init
= apic_class_init
,
888 static void apic_register_types(void)
890 type_register_static(&apic_info
);
893 type_init(apic_register_types
)