]>
git.proxmox.com Git - mirror_qemu.git/blob - hw/apic.c
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
23 //#define DEBUG_IOAPIC
25 /* APIC Local Vector Table */
26 #define APIC_LVT_TIMER 0
27 #define APIC_LVT_THERMAL 1
28 #define APIC_LVT_PERFORM 2
29 #define APIC_LVT_LINT0 3
30 #define APIC_LVT_LINT1 4
31 #define APIC_LVT_ERROR 5
34 /* APIC delivery modes */
35 #define APIC_DM_FIXED 0
36 #define APIC_DM_LOWPRI 1
39 #define APIC_DM_INIT 5
40 #define APIC_DM_SIPI 6
41 #define APIC_DM_EXTINT 7
43 /* APIC destination mode */
44 #define APIC_DESTMODE_FLAT 0xf
45 #define APIC_DESTMODE_CLUSTER 1
47 #define APIC_TRIGGER_EDGE 0
48 #define APIC_TRIGGER_LEVEL 1
50 #define APIC_LVT_TIMER_PERIODIC (1<<17)
51 #define APIC_LVT_MASKED (1<<16)
52 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
53 #define APIC_LVT_REMOTE_IRR (1<<14)
54 #define APIC_INPUT_POLARITY (1<<13)
55 #define APIC_SEND_PENDING (1<<12)
57 #define IOAPIC_NUM_PINS 0x18
59 #define ESR_ILLEGAL_ADDRESS (1 << 7)
61 #define APIC_SV_ENABLE (1 << 8)
63 typedef struct APICState
{
69 uint32_t spurious_vec
;
72 uint32_t isr
[8]; /* in service register */
73 uint32_t tmr
[8]; /* trigger mode register */
74 uint32_t irr
[8]; /* interrupt request register */
75 uint32_t lvt
[APIC_LVT_NB
];
76 uint32_t esr
; /* error register */
81 uint32_t initial_count
;
82 int64_t initial_count_load_time
, next_time
;
85 struct APICState
*next_apic
;
93 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
96 static int apic_io_memory
;
97 static APICState
*first_local_apic
= NULL
;
98 static int last_apic_id
= 0;
100 static void apic_init_ipi(APICState
*s
);
101 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
102 static void apic_update_irq(APICState
*s
);
104 static void apic_bus_deliver(uint32_t deliver_bitmask
, uint8_t delivery_mode
,
105 uint8_t vector_num
, uint8_t polarity
,
106 uint8_t trigger_mode
)
108 APICState
*apic_iter
;
110 switch (delivery_mode
) {
113 /* XXX: arbitration */
121 /* normal INIT IPI sent to processors */
122 for (apic_iter
= first_local_apic
; apic_iter
!= NULL
;
123 apic_iter
= apic_iter
->next_apic
) {
124 if (deliver_bitmask
& (1 << apic_iter
->id
))
125 apic_init_ipi(apic_iter
);
130 /* handled in I/O APIC code */
137 for (apic_iter
= first_local_apic
; apic_iter
!= NULL
;
138 apic_iter
= apic_iter
->next_apic
) {
139 if (deliver_bitmask
& (1 << apic_iter
->id
))
140 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
144 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
146 APICState
*s
= env
->apic_state
;
148 printf("cpu_set_apic_base: %016llx\n", val
);
150 s
->apicbase
= (val
& 0xfffff000) |
151 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
152 /* if disabled, cannot be enabled again */
153 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
154 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
155 env
->cpuid_features
&= ~CPUID_APIC
;
156 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
160 uint64_t cpu_get_apic_base(CPUState
*env
)
162 APICState
*s
= env
->apic_state
;
164 printf("cpu_get_apic_base: %016llx\n", (uint64_t)s
->apicbase
);
169 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
171 APICState
*s
= env
->apic_state
;
172 s
->tpr
= (val
& 0x0f) << 4;
176 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
178 APICState
*s
= env
->apic_state
;
182 static int fls_bit(int value
)
184 unsigned int ret
= 0;
187 __asm__
__volatile__ ("bsr %1, %0\n" : "+r" (ret
) : "rm" (value
));
191 value
>>= 16, ret
= 16;
193 value
>>= 8, ret
+= 8;
195 value
>>= 4, ret
+= 4;
197 value
>>= 2, ret
+= 2;
198 return ret
+ (value
>> 1);
202 static inline void set_bit(uint32_t *tab
, int index
)
206 mask
= 1 << (index
& 0x1f);
210 static inline void reset_bit(uint32_t *tab
, int index
)
214 mask
= 1 << (index
& 0x1f);
218 /* return -1 if no bit is set */
219 static int get_highest_priority_int(uint32_t *tab
)
222 for(i
= 7; i
>= 0; i
--) {
224 return i
* 32 + fls_bit(tab
[i
]);
230 static int apic_get_ppr(APICState
*s
)
235 isrv
= get_highest_priority_int(s
->isr
);
246 static int apic_get_arb_pri(APICState
*s
)
248 /* XXX: arbitration */
252 /* signal the CPU if an irq is pending */
253 static void apic_update_irq(APICState
*s
)
256 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
258 irrv
= get_highest_priority_int(s
->irr
);
261 ppr
= apic_get_ppr(s
);
262 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
264 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
267 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
269 set_bit(s
->irr
, vector_num
);
271 set_bit(s
->tmr
, vector_num
);
273 reset_bit(s
->tmr
, vector_num
);
277 static void apic_eoi(APICState
*s
)
280 isrv
= get_highest_priority_int(s
->isr
);
283 reset_bit(s
->isr
, isrv
);
284 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
285 set the remote IRR bit for level triggered interrupts. */
289 static uint32_t apic_get_delivery_bitmask(uint8_t dest
, uint8_t dest_mode
)
292 APICState
*apic_iter
;
294 if (dest_mode
== 0) {
300 /* XXX: cluster mode */
301 for (apic_iter
= first_local_apic
; apic_iter
!= NULL
;
302 apic_iter
= apic_iter
->next_apic
) {
303 if (dest
& apic_iter
->log_dest
)
304 mask
|= (1 << apic_iter
->id
);
312 static void apic_init_ipi(APICState
*s
)
316 for(i
= 0; i
< APIC_LVT_NB
; i
++)
317 s
->lvt
[i
] = 1 << 16; /* mask LVT */
319 s
->spurious_vec
= 0xff;
322 memset(s
->isr
, 0, sizeof(s
->isr
));
323 memset(s
->tmr
, 0, sizeof(s
->tmr
));
324 memset(s
->irr
, 0, sizeof(s
->irr
));
325 memset(s
->lvt
, 0, sizeof(s
->lvt
));
327 memset(s
->icr
, 0, sizeof(s
->icr
));
330 s
->initial_count
= 0;
331 s
->initial_count_load_time
= 0;
335 /* send a SIPI message to the CPU to start it */
336 static void apic_startup(APICState
*s
, int vector_num
)
338 CPUState
*env
= s
->cpu_env
;
339 if (!env
->cpu_halted
)
342 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
347 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
348 uint8_t delivery_mode
, uint8_t vector_num
,
349 uint8_t polarity
, uint8_t trigger_mode
)
351 uint32_t deliver_bitmask
= 0;
352 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
353 APICState
*apic_iter
;
355 switch (dest_shorthand
) {
357 deliver_bitmask
= apic_get_delivery_bitmask(dest
, dest_mode
);
360 deliver_bitmask
= (1 << s
->id
);
363 deliver_bitmask
= 0xffffffff;
366 deliver_bitmask
= 0xffffffff & ~(1 << s
->id
);
370 switch (delivery_mode
) {
372 /* XXX: search for focus processor, arbitration */
378 int trig_mode
= (s
->icr
[0] >> 15) & 1;
379 int level
= (s
->icr
[0] >> 14) & 1;
380 if (level
== 0 && trig_mode
== 1) {
381 for (apic_iter
= first_local_apic
; apic_iter
!= NULL
;
382 apic_iter
= apic_iter
->next_apic
) {
383 if (deliver_bitmask
& (1 << apic_iter
->id
)) {
384 apic_iter
->arb_id
= apic_iter
->id
;
393 for (apic_iter
= first_local_apic
; apic_iter
!= NULL
;
394 apic_iter
= apic_iter
->next_apic
) {
395 if (deliver_bitmask
& (1 << apic_iter
->id
)) {
396 apic_startup(apic_iter
, vector_num
);
402 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
406 int apic_get_interrupt(CPUState
*env
)
408 APICState
*s
= env
->apic_state
;
411 /* if the APIC is installed or enabled, we let the 8259 handle the
415 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
418 /* XXX: spurious IRQ handling */
419 intno
= get_highest_priority_int(s
->irr
);
422 reset_bit(s
->irr
, intno
);
423 if (s
->tpr
&& intno
<= s
->tpr
)
424 return s
->spurious_vec
& 0xff;
425 set_bit(s
->isr
, intno
);
430 static uint32_t apic_get_current_count(APICState
*s
)
434 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
436 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
438 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
440 if (d
>= s
->initial_count
)
443 val
= s
->initial_count
- d
;
448 static void apic_timer_update(APICState
*s
, int64_t current_time
)
450 int64_t next_time
, d
;
452 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
453 d
= (current_time
- s
->initial_count_load_time
) >>
455 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
456 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
458 if (d
>= s
->initial_count
)
460 d
= (uint64_t)s
->initial_count
+ 1;
462 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
463 qemu_mod_timer(s
->timer
, next_time
);
464 s
->next_time
= next_time
;
467 qemu_del_timer(s
->timer
);
471 static void apic_timer(void *opaque
)
473 APICState
*s
= opaque
;
475 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
476 apic_set_irq(s
, s
->lvt
[APIC_LVT_TIMER
] & 0xff, APIC_TRIGGER_EDGE
);
478 apic_timer_update(s
, s
->next_time
);
481 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
486 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
491 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
495 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
499 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
506 env
= cpu_single_env
;
511 index
= (addr
>> 4) & 0xff;
516 case 0x03: /* version */
517 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
523 val
= apic_get_arb_pri(s
);
527 val
= apic_get_ppr(s
);
530 val
= s
->log_dest
<< 24;
533 val
= s
->dest_mode
<< 28;
536 val
= s
->spurious_vec
;
539 val
= s
->isr
[index
& 7];
542 val
= s
->tmr
[index
& 7];
545 val
= s
->irr
[index
& 7];
552 val
= s
->icr
[index
& 1];
555 val
= s
->lvt
[index
- 0x32];
558 val
= s
->initial_count
;
561 val
= apic_get_current_count(s
);
564 val
= s
->divide_conf
;
567 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
572 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
577 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
583 env
= cpu_single_env
;
589 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
592 index
= (addr
>> 4) & 0xff;
610 s
->log_dest
= val
>> 24;
613 s
->dest_mode
= val
>> 28;
616 s
->spurious_vec
= val
& 0x1ff;
626 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
627 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
628 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
635 int n
= index
- 0x32;
637 if (n
== APIC_LVT_TIMER
)
638 apic_timer_update(s
, qemu_get_clock(vm_clock
));
642 s
->initial_count
= val
;
643 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
644 apic_timer_update(s
, s
->initial_count_load_time
);
651 s
->divide_conf
= val
& 0xb;
652 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
653 s
->count_shift
= (v
+ 1) & 7;
657 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
662 static void apic_save(QEMUFile
*f
, void *opaque
)
664 APICState
*s
= opaque
;
667 qemu_put_be32s(f
, &s
->apicbase
);
668 qemu_put_8s(f
, &s
->id
);
669 qemu_put_8s(f
, &s
->arb_id
);
670 qemu_put_8s(f
, &s
->tpr
);
671 qemu_put_be32s(f
, &s
->spurious_vec
);
672 qemu_put_8s(f
, &s
->log_dest
);
673 qemu_put_8s(f
, &s
->dest_mode
);
674 for (i
= 0; i
< 8; i
++) {
675 qemu_put_be32s(f
, &s
->isr
[i
]);
676 qemu_put_be32s(f
, &s
->tmr
[i
]);
677 qemu_put_be32s(f
, &s
->irr
[i
]);
679 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
680 qemu_put_be32s(f
, &s
->lvt
[i
]);
682 qemu_put_be32s(f
, &s
->esr
);
683 qemu_put_be32s(f
, &s
->icr
[0]);
684 qemu_put_be32s(f
, &s
->icr
[1]);
685 qemu_put_be32s(f
, &s
->divide_conf
);
686 qemu_put_be32s(f
, &s
->count_shift
);
687 qemu_put_be32s(f
, &s
->initial_count
);
688 qemu_put_be64s(f
, &s
->initial_count_load_time
);
689 qemu_put_be64s(f
, &s
->next_time
);
692 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
694 APICState
*s
= opaque
;
700 /* XXX: what if the base changes? (registered memory regions) */
701 qemu_get_be32s(f
, &s
->apicbase
);
702 qemu_get_8s(f
, &s
->id
);
703 qemu_get_8s(f
, &s
->arb_id
);
704 qemu_get_8s(f
, &s
->tpr
);
705 qemu_get_be32s(f
, &s
->spurious_vec
);
706 qemu_get_8s(f
, &s
->log_dest
);
707 qemu_get_8s(f
, &s
->dest_mode
);
708 for (i
= 0; i
< 8; i
++) {
709 qemu_get_be32s(f
, &s
->isr
[i
]);
710 qemu_get_be32s(f
, &s
->tmr
[i
]);
711 qemu_get_be32s(f
, &s
->irr
[i
]);
713 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
714 qemu_get_be32s(f
, &s
->lvt
[i
]);
716 qemu_get_be32s(f
, &s
->esr
);
717 qemu_get_be32s(f
, &s
->icr
[0]);
718 qemu_get_be32s(f
, &s
->icr
[1]);
719 qemu_get_be32s(f
, &s
->divide_conf
);
720 qemu_get_be32s(f
, &s
->count_shift
);
721 qemu_get_be32s(f
, &s
->initial_count
);
722 qemu_get_be64s(f
, &s
->initial_count_load_time
);
723 qemu_get_be64s(f
, &s
->next_time
);
727 static void apic_reset(void *opaque
)
729 APICState
*s
= opaque
;
733 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
739 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
745 int apic_init(CPUState
*env
)
749 s
= qemu_mallocz(sizeof(APICState
));
754 s
->id
= last_apic_id
++;
756 s
->apicbase
= 0xfee00000 |
757 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
759 /* XXX: mapping more APICs at the same memory location */
760 if (apic_io_memory
== 0) {
761 /* NOTE: the APIC is directly connected to the CPU - it is not
762 on the global memory bus. */
763 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
764 apic_mem_write
, NULL
);
765 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
768 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
770 register_savevm("apic", 0, 1, apic_save
, apic_load
, s
);
771 qemu_register_reset(apic_reset
, s
);
773 s
->next_apic
= first_local_apic
;
774 first_local_apic
= s
;
779 static void ioapic_service(IOAPICState
*s
)
784 uint8_t delivery_mode
;
791 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
794 entry
= s
->ioredtbl
[i
];
795 if (!(entry
& APIC_LVT_MASKED
)) {
796 trig_mode
= ((entry
>> 15) & 1);
798 dest_mode
= (entry
>> 11) & 1;
799 delivery_mode
= (entry
>> 8) & 7;
800 polarity
= (entry
>> 13) & 1;
801 if (trig_mode
== APIC_TRIGGER_EDGE
)
803 if (delivery_mode
== APIC_DM_EXTINT
)
804 vector
= pic_read_irq(isa_pic
);
806 vector
= entry
& 0xff;
807 apic_bus_deliver(apic_get_delivery_bitmask(dest
, dest_mode
),
808 delivery_mode
, vector
, polarity
, trig_mode
);
814 void ioapic_set_irq(void *opaque
, int vector
, int level
)
816 IOAPICState
*s
= opaque
;
818 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
819 uint32_t mask
= 1 << vector
;
820 uint64_t entry
= s
->ioredtbl
[vector
];
822 if ((entry
>> 15) & 1) {
823 /* level triggered */
840 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
842 IOAPICState
*s
= opaque
;
849 } else if (addr
== 0x10) {
850 switch (s
->ioregsel
) {
855 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
861 index
= (s
->ioregsel
- 0x10) >> 1;
862 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
864 val
= s
->ioredtbl
[index
] >> 32;
866 val
= s
->ioredtbl
[index
] & 0xffffffff;
870 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
876 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
878 IOAPICState
*s
= opaque
;
885 } else if (addr
== 0x10) {
887 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
889 switch (s
->ioregsel
) {
891 s
->id
= (val
>> 24) & 0xff;
897 index
= (s
->ioregsel
- 0x10) >> 1;
898 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
899 if (s
->ioregsel
& 1) {
900 s
->ioredtbl
[index
] &= 0xffffffff;
901 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
903 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
904 s
->ioredtbl
[index
] |= val
;
912 static void ioapic_save(QEMUFile
*f
, void *opaque
)
914 IOAPICState
*s
= opaque
;
917 qemu_put_8s(f
, &s
->id
);
918 qemu_put_8s(f
, &s
->ioregsel
);
919 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
920 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
924 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
926 IOAPICState
*s
= opaque
;
932 qemu_get_8s(f
, &s
->id
);
933 qemu_get_8s(f
, &s
->ioregsel
);
934 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
935 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
940 static void ioapic_reset(void *opaque
)
942 IOAPICState
*s
= opaque
;
945 memset(s
, 0, sizeof(*s
));
946 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
947 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
950 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
956 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
962 IOAPICState
*ioapic_init(void)
967 s
= qemu_mallocz(sizeof(IOAPICState
));
971 s
->id
= last_apic_id
++;
973 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
974 ioapic_mem_write
, s
);
975 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
977 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
978 qemu_register_reset(ioapic_reset
, s
);