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
22 #include "qemu-timer.h"
25 //#define DEBUG_IOAPIC
27 /* APIC Local Vector Table */
28 #define APIC_LVT_TIMER 0
29 #define APIC_LVT_THERMAL 1
30 #define APIC_LVT_PERFORM 2
31 #define APIC_LVT_LINT0 3
32 #define APIC_LVT_LINT1 4
33 #define APIC_LVT_ERROR 5
36 /* APIC delivery modes */
37 #define APIC_DM_FIXED 0
38 #define APIC_DM_LOWPRI 1
41 #define APIC_DM_INIT 5
42 #define APIC_DM_SIPI 6
43 #define APIC_DM_EXTINT 7
45 /* APIC destination mode */
46 #define APIC_DESTMODE_FLAT 0xf
47 #define APIC_DESTMODE_CLUSTER 1
49 #define APIC_TRIGGER_EDGE 0
50 #define APIC_TRIGGER_LEVEL 1
52 #define APIC_LVT_TIMER_PERIODIC (1<<17)
53 #define APIC_LVT_MASKED (1<<16)
54 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
55 #define APIC_LVT_REMOTE_IRR (1<<14)
56 #define APIC_INPUT_POLARITY (1<<13)
57 #define APIC_SEND_PENDING (1<<12)
59 #define IOAPIC_NUM_PINS 0x18
61 #define ESR_ILLEGAL_ADDRESS (1 << 7)
63 #define APIC_SV_ENABLE (1 << 8)
66 #define MAX_APIC_WORDS 8
68 typedef struct APICState
{
74 uint32_t spurious_vec
;
77 uint32_t isr
[8]; /* in service register */
78 uint32_t tmr
[8]; /* trigger mode register */
79 uint32_t irr
[8]; /* interrupt request register */
80 uint32_t lvt
[APIC_LVT_NB
];
81 uint32_t esr
; /* error register */
86 uint32_t initial_count
;
87 int64_t initial_count_load_time
, next_time
;
96 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
99 static int apic_io_memory
;
100 static APICState
*local_apics
[MAX_APICS
+ 1];
101 static int last_apic_id
= 0;
103 static void apic_init_ipi(APICState
*s
);
104 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
105 static void apic_update_irq(APICState
*s
);
107 /* Find first bit starting from msb. Return 0 if value = 0 */
108 static int fls_bit(uint32_t value
)
110 unsigned int ret
= 0;
112 #if defined(HOST_I386)
113 __asm__
__volatile__ ("bsr %1, %0\n" : "+r" (ret
) : "rm" (value
));
117 value
>>= 16, ret
= 16;
119 value
>>= 8, ret
+= 8;
121 value
>>= 4, ret
+= 4;
123 value
>>= 2, ret
+= 2;
124 return ret
+ (value
>> 1);
128 /* Find first bit starting from lsb. Return 0 if value = 0 */
129 static int ffs_bit(uint32_t value
)
131 unsigned int ret
= 0;
133 #if defined(HOST_I386)
134 __asm__
__volatile__ ("bsf %1, %0\n" : "+r" (ret
) : "rm" (value
));
139 if (!(value
& 0xffff))
140 value
>>= 16, ret
= 16;
142 value
>>= 8, ret
+= 8;
144 value
>>= 4, ret
+= 4;
146 value
>>= 2, ret
+= 2;
153 static inline void set_bit(uint32_t *tab
, int index
)
157 mask
= 1 << (index
& 0x1f);
161 static inline void reset_bit(uint32_t *tab
, int index
)
165 mask
= 1 << (index
& 0x1f);
169 static void apic_local_deliver(CPUState
*env
, int vector
)
171 APICState
*s
= env
->apic_state
;
172 uint32_t lvt
= s
->lvt
[vector
];
175 if (lvt
& APIC_LVT_MASKED
)
178 switch ((lvt
>> 8) & 7) {
180 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
184 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
188 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
192 trigger_mode
= APIC_TRIGGER_EDGE
;
193 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
194 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
195 trigger_mode
= APIC_TRIGGER_LEVEL
;
196 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
200 void apic_deliver_pic_intr(CPUState
*env
, int level
)
203 apic_local_deliver(env
, APIC_LVT_LINT0
);
205 APICState
*s
= env
->apic_state
;
206 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
208 switch ((lvt
>> 8) & 7) {
210 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
212 reset_bit(s
->irr
, lvt
& 0xff);
215 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
221 #define foreach_apic(apic, deliver_bitmask, code) \
223 int __i, __j, __mask;\
224 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
225 __mask = deliver_bitmask[__i];\
227 for(__j = 0; __j < 32; __j++) {\
228 if (__mask & (1 << __j)) {\
229 apic = local_apics[__i * 32 + __j];\
239 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
240 uint8_t delivery_mode
,
241 uint8_t vector_num
, uint8_t polarity
,
242 uint8_t trigger_mode
)
244 APICState
*apic_iter
;
246 switch (delivery_mode
) {
248 /* XXX: search for focus processor, arbitration */
252 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
253 if (deliver_bitmask
[i
]) {
254 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
259 apic_iter
= local_apics
[d
];
261 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
271 foreach_apic(apic_iter
, deliver_bitmask
,
272 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
276 foreach_apic(apic_iter
, deliver_bitmask
,
277 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
281 /* normal INIT IPI sent to processors */
282 foreach_apic(apic_iter
, deliver_bitmask
,
283 apic_init_ipi(apic_iter
) );
287 /* handled in I/O APIC code */
294 foreach_apic(apic_iter
, deliver_bitmask
,
295 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
298 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
300 APICState
*s
= env
->apic_state
;
302 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
304 s
->apicbase
= (val
& 0xfffff000) |
305 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
306 /* if disabled, cannot be enabled again */
307 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
308 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
309 env
->cpuid_features
&= ~CPUID_APIC
;
310 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
314 uint64_t cpu_get_apic_base(CPUState
*env
)
316 APICState
*s
= env
->apic_state
;
318 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
323 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
325 APICState
*s
= env
->apic_state
;
326 s
->tpr
= (val
& 0x0f) << 4;
330 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
332 APICState
*s
= env
->apic_state
;
336 /* return -1 if no bit is set */
337 static int get_highest_priority_int(uint32_t *tab
)
340 for(i
= 7; i
>= 0; i
--) {
342 return i
* 32 + fls_bit(tab
[i
]);
348 static int apic_get_ppr(APICState
*s
)
353 isrv
= get_highest_priority_int(s
->isr
);
364 static int apic_get_arb_pri(APICState
*s
)
366 /* XXX: arbitration */
370 /* signal the CPU if an irq is pending */
371 static void apic_update_irq(APICState
*s
)
374 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
376 irrv
= get_highest_priority_int(s
->irr
);
379 ppr
= apic_get_ppr(s
);
380 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
382 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
385 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
387 set_bit(s
->irr
, vector_num
);
389 set_bit(s
->tmr
, vector_num
);
391 reset_bit(s
->tmr
, vector_num
);
395 static void apic_eoi(APICState
*s
)
398 isrv
= get_highest_priority_int(s
->isr
);
401 reset_bit(s
->isr
, isrv
);
402 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
403 set the remote IRR bit for level triggered interrupts. */
407 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
408 uint8_t dest
, uint8_t dest_mode
)
410 APICState
*apic_iter
;
413 if (dest_mode
== 0) {
415 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
417 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
418 set_bit(deliver_bitmask
, dest
);
421 /* XXX: cluster mode */
422 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
423 for(i
= 0; i
< MAX_APICS
; i
++) {
424 apic_iter
= local_apics
[i
];
426 if (apic_iter
->dest_mode
== 0xf) {
427 if (dest
& apic_iter
->log_dest
)
428 set_bit(deliver_bitmask
, i
);
429 } else if (apic_iter
->dest_mode
== 0x0) {
430 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
431 (dest
& apic_iter
->log_dest
& 0x0f)) {
432 set_bit(deliver_bitmask
, i
);
441 static void apic_init_ipi(APICState
*s
)
446 s
->spurious_vec
= 0xff;
449 memset(s
->isr
, 0, sizeof(s
->isr
));
450 memset(s
->tmr
, 0, sizeof(s
->tmr
));
451 memset(s
->irr
, 0, sizeof(s
->irr
));
452 for(i
= 0; i
< APIC_LVT_NB
; i
++)
453 s
->lvt
[i
] = 1 << 16; /* mask LVT */
455 memset(s
->icr
, 0, sizeof(s
->icr
));
458 s
->initial_count
= 0;
459 s
->initial_count_load_time
= 0;
463 /* send a SIPI message to the CPU to start it */
464 static void apic_startup(APICState
*s
, int vector_num
)
466 CPUState
*env
= s
->cpu_env
;
470 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
475 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
476 uint8_t delivery_mode
, uint8_t vector_num
,
477 uint8_t polarity
, uint8_t trigger_mode
)
479 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
480 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
481 APICState
*apic_iter
;
483 switch (dest_shorthand
) {
485 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
488 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
489 set_bit(deliver_bitmask
, s
->id
);
492 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
495 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
496 reset_bit(deliver_bitmask
, s
->id
);
500 switch (delivery_mode
) {
503 int trig_mode
= (s
->icr
[0] >> 15) & 1;
504 int level
= (s
->icr
[0] >> 14) & 1;
505 if (level
== 0 && trig_mode
== 1) {
506 foreach_apic(apic_iter
, deliver_bitmask
,
507 apic_iter
->arb_id
= apic_iter
->id
);
514 foreach_apic(apic_iter
, deliver_bitmask
,
515 apic_startup(apic_iter
, vector_num
) );
519 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
523 int apic_get_interrupt(CPUState
*env
)
525 APICState
*s
= env
->apic_state
;
528 /* if the APIC is installed or enabled, we let the 8259 handle the
532 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
535 /* XXX: spurious IRQ handling */
536 intno
= get_highest_priority_int(s
->irr
);
539 if (s
->tpr
&& intno
<= s
->tpr
)
540 return s
->spurious_vec
& 0xff;
541 reset_bit(s
->irr
, intno
);
542 set_bit(s
->isr
, intno
);
547 int apic_accept_pic_intr(CPUState
*env
)
549 APICState
*s
= env
->apic_state
;
555 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
557 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
558 (lvt0
& APIC_LVT_MASKED
) == 0)
564 static uint32_t apic_get_current_count(APICState
*s
)
568 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
570 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
572 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
574 if (d
>= s
->initial_count
)
577 val
= s
->initial_count
- d
;
582 static void apic_timer_update(APICState
*s
, int64_t current_time
)
584 int64_t next_time
, d
;
586 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
587 d
= (current_time
- s
->initial_count_load_time
) >>
589 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
590 if (!s
->initial_count
)
592 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
594 if (d
>= s
->initial_count
)
596 d
= (uint64_t)s
->initial_count
+ 1;
598 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
599 qemu_mod_timer(s
->timer
, next_time
);
600 s
->next_time
= next_time
;
603 qemu_del_timer(s
->timer
);
607 static void apic_timer(void *opaque
)
609 APICState
*s
= opaque
;
611 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
612 apic_timer_update(s
, s
->next_time
);
615 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
620 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
625 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
629 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
633 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
640 env
= cpu_single_env
;
645 index
= (addr
>> 4) & 0xff;
650 case 0x03: /* version */
651 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
657 val
= apic_get_arb_pri(s
);
661 val
= apic_get_ppr(s
);
667 val
= s
->log_dest
<< 24;
670 val
= s
->dest_mode
<< 28;
673 val
= s
->spurious_vec
;
676 val
= s
->isr
[index
& 7];
679 val
= s
->tmr
[index
& 7];
682 val
= s
->irr
[index
& 7];
689 val
= s
->icr
[index
& 1];
692 val
= s
->lvt
[index
- 0x32];
695 val
= s
->initial_count
;
698 val
= apic_get_current_count(s
);
701 val
= s
->divide_conf
;
704 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
709 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
714 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
720 env
= cpu_single_env
;
726 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
729 index
= (addr
>> 4) & 0xff;
747 s
->log_dest
= val
>> 24;
750 s
->dest_mode
= val
>> 28;
753 s
->spurious_vec
= val
& 0x1ff;
763 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
764 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
765 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
772 int n
= index
- 0x32;
774 if (n
== APIC_LVT_TIMER
)
775 apic_timer_update(s
, qemu_get_clock(vm_clock
));
779 s
->initial_count
= val
;
780 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
781 apic_timer_update(s
, s
->initial_count_load_time
);
788 s
->divide_conf
= val
& 0xb;
789 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
790 s
->count_shift
= (v
+ 1) & 7;
794 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
799 static void apic_save(QEMUFile
*f
, void *opaque
)
801 APICState
*s
= opaque
;
804 qemu_put_be32s(f
, &s
->apicbase
);
805 qemu_put_8s(f
, &s
->id
);
806 qemu_put_8s(f
, &s
->arb_id
);
807 qemu_put_8s(f
, &s
->tpr
);
808 qemu_put_be32s(f
, &s
->spurious_vec
);
809 qemu_put_8s(f
, &s
->log_dest
);
810 qemu_put_8s(f
, &s
->dest_mode
);
811 for (i
= 0; i
< 8; i
++) {
812 qemu_put_be32s(f
, &s
->isr
[i
]);
813 qemu_put_be32s(f
, &s
->tmr
[i
]);
814 qemu_put_be32s(f
, &s
->irr
[i
]);
816 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
817 qemu_put_be32s(f
, &s
->lvt
[i
]);
819 qemu_put_be32s(f
, &s
->esr
);
820 qemu_put_be32s(f
, &s
->icr
[0]);
821 qemu_put_be32s(f
, &s
->icr
[1]);
822 qemu_put_be32s(f
, &s
->divide_conf
);
823 qemu_put_be32(f
, s
->count_shift
);
824 qemu_put_be32s(f
, &s
->initial_count
);
825 qemu_put_be64(f
, s
->initial_count_load_time
);
826 qemu_put_be64(f
, s
->next_time
);
828 qemu_put_timer(f
, s
->timer
);
831 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
833 APICState
*s
= opaque
;
839 /* XXX: what if the base changes? (registered memory regions) */
840 qemu_get_be32s(f
, &s
->apicbase
);
841 qemu_get_8s(f
, &s
->id
);
842 qemu_get_8s(f
, &s
->arb_id
);
843 qemu_get_8s(f
, &s
->tpr
);
844 qemu_get_be32s(f
, &s
->spurious_vec
);
845 qemu_get_8s(f
, &s
->log_dest
);
846 qemu_get_8s(f
, &s
->dest_mode
);
847 for (i
= 0; i
< 8; i
++) {
848 qemu_get_be32s(f
, &s
->isr
[i
]);
849 qemu_get_be32s(f
, &s
->tmr
[i
]);
850 qemu_get_be32s(f
, &s
->irr
[i
]);
852 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
853 qemu_get_be32s(f
, &s
->lvt
[i
]);
855 qemu_get_be32s(f
, &s
->esr
);
856 qemu_get_be32s(f
, &s
->icr
[0]);
857 qemu_get_be32s(f
, &s
->icr
[1]);
858 qemu_get_be32s(f
, &s
->divide_conf
);
859 s
->count_shift
=qemu_get_be32(f
);
860 qemu_get_be32s(f
, &s
->initial_count
);
861 s
->initial_count_load_time
=qemu_get_be64(f
);
862 s
->next_time
=qemu_get_be64(f
);
865 qemu_get_timer(f
, s
->timer
);
869 static void apic_reset(void *opaque
)
871 APICState
*s
= opaque
;
873 s
->apicbase
= 0xfee00000 |
874 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
880 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
881 * time typically by BIOS, so PIC interrupt can be delivered to the
882 * processor when local APIC is enabled.
884 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
888 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
894 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
900 int apic_init(CPUState
*env
)
904 if (last_apic_id
>= MAX_APICS
)
906 s
= qemu_mallocz(sizeof(APICState
));
910 s
->id
= last_apic_id
++;
911 env
->cpuid_apic_id
= s
->id
;
916 /* XXX: mapping more APICs at the same memory location */
917 if (apic_io_memory
== 0) {
918 /* NOTE: the APIC is directly connected to the CPU - it is not
919 on the global memory bus. */
920 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
921 apic_mem_write
, NULL
);
922 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
925 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
927 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
928 qemu_register_reset(apic_reset
, s
);
930 local_apics
[s
->id
] = s
;
934 static void ioapic_service(IOAPICState
*s
)
939 uint8_t delivery_mode
;
945 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
947 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
950 entry
= s
->ioredtbl
[i
];
951 if (!(entry
& APIC_LVT_MASKED
)) {
952 trig_mode
= ((entry
>> 15) & 1);
954 dest_mode
= (entry
>> 11) & 1;
955 delivery_mode
= (entry
>> 8) & 7;
956 polarity
= (entry
>> 13) & 1;
957 if (trig_mode
== APIC_TRIGGER_EDGE
)
959 if (delivery_mode
== APIC_DM_EXTINT
)
960 vector
= pic_read_irq(isa_pic
);
962 vector
= entry
& 0xff;
964 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
965 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
966 vector
, polarity
, trig_mode
);
972 void ioapic_set_irq(void *opaque
, int vector
, int level
)
974 IOAPICState
*s
= opaque
;
976 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
977 uint32_t mask
= 1 << vector
;
978 uint64_t entry
= s
->ioredtbl
[vector
];
980 if ((entry
>> 15) & 1) {
981 /* level triggered */
998 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1000 IOAPICState
*s
= opaque
;
1007 } else if (addr
== 0x10) {
1008 switch (s
->ioregsel
) {
1013 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1019 index
= (s
->ioregsel
- 0x10) >> 1;
1020 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1021 if (s
->ioregsel
& 1)
1022 val
= s
->ioredtbl
[index
] >> 32;
1024 val
= s
->ioredtbl
[index
] & 0xffffffff;
1028 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1034 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1036 IOAPICState
*s
= opaque
;
1043 } else if (addr
== 0x10) {
1045 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1047 switch (s
->ioregsel
) {
1049 s
->id
= (val
>> 24) & 0xff;
1055 index
= (s
->ioregsel
- 0x10) >> 1;
1056 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1057 if (s
->ioregsel
& 1) {
1058 s
->ioredtbl
[index
] &= 0xffffffff;
1059 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1061 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1062 s
->ioredtbl
[index
] |= val
;
1070 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1072 IOAPICState
*s
= opaque
;
1075 qemu_put_8s(f
, &s
->id
);
1076 qemu_put_8s(f
, &s
->ioregsel
);
1077 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1078 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1082 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1084 IOAPICState
*s
= opaque
;
1087 if (version_id
!= 1)
1090 qemu_get_8s(f
, &s
->id
);
1091 qemu_get_8s(f
, &s
->ioregsel
);
1092 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1093 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1098 static void ioapic_reset(void *opaque
)
1100 IOAPICState
*s
= opaque
;
1103 memset(s
, 0, sizeof(*s
));
1104 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1105 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1108 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1114 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1120 IOAPICState
*ioapic_init(void)
1125 s
= qemu_mallocz(sizeof(IOAPICState
));
1129 s
->id
= last_apic_id
++;
1131 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1132 ioapic_mem_write
, s
);
1133 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1135 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
1136 qemu_register_reset(ioapic_reset
, s
);