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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22 #include "qemu-timer.h"
23 #include "host-utils.h"
26 //#define DEBUG_IOAPIC
28 /* APIC Local Vector Table */
29 #define APIC_LVT_TIMER 0
30 #define APIC_LVT_THERMAL 1
31 #define APIC_LVT_PERFORM 2
32 #define APIC_LVT_LINT0 3
33 #define APIC_LVT_LINT1 4
34 #define APIC_LVT_ERROR 5
37 /* APIC delivery modes */
38 #define APIC_DM_FIXED 0
39 #define APIC_DM_LOWPRI 1
42 #define APIC_DM_INIT 5
43 #define APIC_DM_SIPI 6
44 #define APIC_DM_EXTINT 7
46 /* APIC destination mode */
47 #define APIC_DESTMODE_FLAT 0xf
48 #define APIC_DESTMODE_CLUSTER 1
50 #define APIC_TRIGGER_EDGE 0
51 #define APIC_TRIGGER_LEVEL 1
53 #define APIC_LVT_TIMER_PERIODIC (1<<17)
54 #define APIC_LVT_MASKED (1<<16)
55 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
56 #define APIC_LVT_REMOTE_IRR (1<<14)
57 #define APIC_INPUT_POLARITY (1<<13)
58 #define APIC_SEND_PENDING (1<<12)
60 #define IOAPIC_NUM_PINS 0x18
62 #define ESR_ILLEGAL_ADDRESS (1 << 7)
64 #define APIC_SV_ENABLE (1 << 8)
67 #define MAX_APIC_WORDS 8
69 typedef struct APICState
{
75 uint32_t spurious_vec
;
78 uint32_t isr
[8]; /* in service register */
79 uint32_t tmr
[8]; /* trigger mode register */
80 uint32_t irr
[8]; /* interrupt request register */
81 uint32_t lvt
[APIC_LVT_NB
];
82 uint32_t esr
; /* error register */
87 uint32_t initial_count
;
88 int64_t initial_count_load_time
, next_time
;
97 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
100 static int apic_io_memory
;
101 static APICState
*local_apics
[MAX_APICS
+ 1];
102 static int last_apic_id
= 0;
103 static int apic_irq_delivered
;
106 static void apic_init_ipi(APICState
*s
);
107 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
108 static void apic_update_irq(APICState
*s
);
110 /* Find first bit starting from msb */
111 static int fls_bit(uint32_t value
)
113 return 31 - clz32(value
);
116 /* Find first bit starting from lsb */
117 static int ffs_bit(uint32_t value
)
122 static inline void set_bit(uint32_t *tab
, int index
)
126 mask
= 1 << (index
& 0x1f);
130 static inline void reset_bit(uint32_t *tab
, int index
)
134 mask
= 1 << (index
& 0x1f);
138 static inline int get_bit(uint32_t *tab
, int index
)
142 mask
= 1 << (index
& 0x1f);
143 return !!(tab
[i
] & mask
);
146 static void apic_local_deliver(CPUState
*env
, int vector
)
148 APICState
*s
= env
->apic_state
;
149 uint32_t lvt
= s
->lvt
[vector
];
152 if (lvt
& APIC_LVT_MASKED
)
155 switch ((lvt
>> 8) & 7) {
157 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
161 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
165 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
169 trigger_mode
= APIC_TRIGGER_EDGE
;
170 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
171 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
172 trigger_mode
= APIC_TRIGGER_LEVEL
;
173 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
177 void apic_deliver_pic_intr(CPUState
*env
, int level
)
180 apic_local_deliver(env
, APIC_LVT_LINT0
);
182 APICState
*s
= env
->apic_state
;
183 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
185 switch ((lvt
>> 8) & 7) {
187 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
189 reset_bit(s
->irr
, lvt
& 0xff);
192 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
198 #define foreach_apic(apic, deliver_bitmask, code) \
200 int __i, __j, __mask;\
201 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
202 __mask = deliver_bitmask[__i];\
204 for(__j = 0; __j < 32; __j++) {\
205 if (__mask & (1 << __j)) {\
206 apic = local_apics[__i * 32 + __j];\
216 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
217 uint8_t delivery_mode
,
218 uint8_t vector_num
, uint8_t polarity
,
219 uint8_t trigger_mode
)
221 APICState
*apic_iter
;
223 switch (delivery_mode
) {
225 /* XXX: search for focus processor, arbitration */
229 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
230 if (deliver_bitmask
[i
]) {
231 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
236 apic_iter
= local_apics
[d
];
238 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
248 foreach_apic(apic_iter
, deliver_bitmask
,
249 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
253 foreach_apic(apic_iter
, deliver_bitmask
,
254 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
258 /* normal INIT IPI sent to processors */
259 foreach_apic(apic_iter
, deliver_bitmask
,
260 apic_init_ipi(apic_iter
) );
264 /* handled in I/O APIC code */
271 foreach_apic(apic_iter
, deliver_bitmask
,
272 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
275 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
277 APICState
*s
= env
->apic_state
;
279 printf("cpu_set_apic_base: %016" PRIx64
"\n", 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 env
->cpuid_features
&= ~CPUID_APIC
;
287 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
291 uint64_t cpu_get_apic_base(CPUState
*env
)
293 APICState
*s
= env
->apic_state
;
295 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
300 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
302 APICState
*s
= env
->apic_state
;
303 s
->tpr
= (val
& 0x0f) << 4;
307 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
309 APICState
*s
= env
->apic_state
;
313 /* return -1 if no bit is set */
314 static int get_highest_priority_int(uint32_t *tab
)
317 for(i
= 7; i
>= 0; i
--) {
319 return i
* 32 + fls_bit(tab
[i
]);
325 static int apic_get_ppr(APICState
*s
)
330 isrv
= get_highest_priority_int(s
->isr
);
341 static int apic_get_arb_pri(APICState
*s
)
343 /* XXX: arbitration */
347 /* signal the CPU if an irq is pending */
348 static void apic_update_irq(APICState
*s
)
351 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
353 irrv
= get_highest_priority_int(s
->irr
);
356 ppr
= apic_get_ppr(s
);
357 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
359 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
362 void apic_reset_irq_delivered(void)
364 apic_irq_delivered
= 0;
367 int apic_get_irq_delivered(void)
369 return apic_irq_delivered
;
372 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
374 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
376 set_bit(s
->irr
, vector_num
);
378 set_bit(s
->tmr
, vector_num
);
380 reset_bit(s
->tmr
, vector_num
);
384 static void apic_eoi(APICState
*s
)
387 isrv
= get_highest_priority_int(s
->isr
);
390 reset_bit(s
->isr
, isrv
);
391 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
392 set the remote IRR bit for level triggered interrupts. */
396 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
397 uint8_t dest
, uint8_t dest_mode
)
399 APICState
*apic_iter
;
402 if (dest_mode
== 0) {
404 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
406 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
407 set_bit(deliver_bitmask
, dest
);
410 /* XXX: cluster mode */
411 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
412 for(i
= 0; i
< MAX_APICS
; i
++) {
413 apic_iter
= local_apics
[i
];
415 if (apic_iter
->dest_mode
== 0xf) {
416 if (dest
& apic_iter
->log_dest
)
417 set_bit(deliver_bitmask
, i
);
418 } else if (apic_iter
->dest_mode
== 0x0) {
419 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
420 (dest
& apic_iter
->log_dest
& 0x0f)) {
421 set_bit(deliver_bitmask
, i
);
430 static void apic_init_ipi(APICState
*s
)
435 s
->spurious_vec
= 0xff;
438 memset(s
->isr
, 0, sizeof(s
->isr
));
439 memset(s
->tmr
, 0, sizeof(s
->tmr
));
440 memset(s
->irr
, 0, sizeof(s
->irr
));
441 for(i
= 0; i
< APIC_LVT_NB
; i
++)
442 s
->lvt
[i
] = 1 << 16; /* mask LVT */
444 memset(s
->icr
, 0, sizeof(s
->icr
));
447 s
->initial_count
= 0;
448 s
->initial_count_load_time
= 0;
451 cpu_reset(s
->cpu_env
);
453 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
))
454 s
->cpu_env
->halted
= 1;
457 /* send a SIPI message to the CPU to start it */
458 static void apic_startup(APICState
*s
, int vector_num
)
460 CPUState
*env
= s
->cpu_env
;
464 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
469 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
470 uint8_t delivery_mode
, uint8_t vector_num
,
471 uint8_t polarity
, uint8_t trigger_mode
)
473 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
474 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
475 APICState
*apic_iter
;
477 switch (dest_shorthand
) {
479 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
482 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
483 set_bit(deliver_bitmask
, s
->id
);
486 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
489 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
490 reset_bit(deliver_bitmask
, s
->id
);
494 switch (delivery_mode
) {
497 int trig_mode
= (s
->icr
[0] >> 15) & 1;
498 int level
= (s
->icr
[0] >> 14) & 1;
499 if (level
== 0 && trig_mode
== 1) {
500 foreach_apic(apic_iter
, deliver_bitmask
,
501 apic_iter
->arb_id
= apic_iter
->id
);
508 foreach_apic(apic_iter
, deliver_bitmask
,
509 apic_startup(apic_iter
, vector_num
) );
513 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
517 int apic_get_interrupt(CPUState
*env
)
519 APICState
*s
= env
->apic_state
;
522 /* if the APIC is installed or enabled, we let the 8259 handle the
526 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
529 /* XXX: spurious IRQ handling */
530 intno
= get_highest_priority_int(s
->irr
);
533 if (s
->tpr
&& intno
<= s
->tpr
)
534 return s
->spurious_vec
& 0xff;
535 reset_bit(s
->irr
, intno
);
536 set_bit(s
->isr
, intno
);
541 int apic_accept_pic_intr(CPUState
*env
)
543 APICState
*s
= env
->apic_state
;
549 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
551 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
552 (lvt0
& APIC_LVT_MASKED
) == 0)
558 static uint32_t apic_get_current_count(APICState
*s
)
562 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
564 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
566 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
568 if (d
>= s
->initial_count
)
571 val
= s
->initial_count
- d
;
576 static void apic_timer_update(APICState
*s
, int64_t current_time
)
578 int64_t next_time
, d
;
580 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
581 d
= (current_time
- s
->initial_count_load_time
) >>
583 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
584 if (!s
->initial_count
)
586 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
588 if (d
>= s
->initial_count
)
590 d
= (uint64_t)s
->initial_count
+ 1;
592 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
593 qemu_mod_timer(s
->timer
, next_time
);
594 s
->next_time
= next_time
;
597 qemu_del_timer(s
->timer
);
601 static void apic_timer(void *opaque
)
603 APICState
*s
= opaque
;
605 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
606 apic_timer_update(s
, s
->next_time
);
609 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
614 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
619 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
623 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
627 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
634 env
= cpu_single_env
;
639 index
= (addr
>> 4) & 0xff;
644 case 0x03: /* version */
645 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
651 val
= apic_get_arb_pri(s
);
655 val
= apic_get_ppr(s
);
661 val
= s
->log_dest
<< 24;
664 val
= s
->dest_mode
<< 28;
667 val
= s
->spurious_vec
;
670 val
= s
->isr
[index
& 7];
673 val
= s
->tmr
[index
& 7];
676 val
= s
->irr
[index
& 7];
683 val
= s
->icr
[index
& 1];
686 val
= s
->lvt
[index
- 0x32];
689 val
= s
->initial_count
;
692 val
= apic_get_current_count(s
);
695 val
= s
->divide_conf
;
698 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
703 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
708 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
714 env
= cpu_single_env
;
720 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
723 index
= (addr
>> 4) & 0xff;
741 s
->log_dest
= val
>> 24;
744 s
->dest_mode
= val
>> 28;
747 s
->spurious_vec
= val
& 0x1ff;
757 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
758 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
759 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
766 int n
= index
- 0x32;
768 if (n
== APIC_LVT_TIMER
)
769 apic_timer_update(s
, qemu_get_clock(vm_clock
));
773 s
->initial_count
= val
;
774 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
775 apic_timer_update(s
, s
->initial_count_load_time
);
782 s
->divide_conf
= val
& 0xb;
783 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
784 s
->count_shift
= (v
+ 1) & 7;
788 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
793 static void apic_save(QEMUFile
*f
, void *opaque
)
795 APICState
*s
= opaque
;
798 qemu_put_be32s(f
, &s
->apicbase
);
799 qemu_put_8s(f
, &s
->id
);
800 qemu_put_8s(f
, &s
->arb_id
);
801 qemu_put_8s(f
, &s
->tpr
);
802 qemu_put_be32s(f
, &s
->spurious_vec
);
803 qemu_put_8s(f
, &s
->log_dest
);
804 qemu_put_8s(f
, &s
->dest_mode
);
805 for (i
= 0; i
< 8; i
++) {
806 qemu_put_be32s(f
, &s
->isr
[i
]);
807 qemu_put_be32s(f
, &s
->tmr
[i
]);
808 qemu_put_be32s(f
, &s
->irr
[i
]);
810 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
811 qemu_put_be32s(f
, &s
->lvt
[i
]);
813 qemu_put_be32s(f
, &s
->esr
);
814 qemu_put_be32s(f
, &s
->icr
[0]);
815 qemu_put_be32s(f
, &s
->icr
[1]);
816 qemu_put_be32s(f
, &s
->divide_conf
);
817 qemu_put_be32(f
, s
->count_shift
);
818 qemu_put_be32s(f
, &s
->initial_count
);
819 qemu_put_be64(f
, s
->initial_count_load_time
);
820 qemu_put_be64(f
, s
->next_time
);
822 qemu_put_timer(f
, s
->timer
);
825 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
827 APICState
*s
= opaque
;
833 /* XXX: what if the base changes? (registered memory regions) */
834 qemu_get_be32s(f
, &s
->apicbase
);
835 qemu_get_8s(f
, &s
->id
);
836 qemu_get_8s(f
, &s
->arb_id
);
837 qemu_get_8s(f
, &s
->tpr
);
838 qemu_get_be32s(f
, &s
->spurious_vec
);
839 qemu_get_8s(f
, &s
->log_dest
);
840 qemu_get_8s(f
, &s
->dest_mode
);
841 for (i
= 0; i
< 8; i
++) {
842 qemu_get_be32s(f
, &s
->isr
[i
]);
843 qemu_get_be32s(f
, &s
->tmr
[i
]);
844 qemu_get_be32s(f
, &s
->irr
[i
]);
846 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
847 qemu_get_be32s(f
, &s
->lvt
[i
]);
849 qemu_get_be32s(f
, &s
->esr
);
850 qemu_get_be32s(f
, &s
->icr
[0]);
851 qemu_get_be32s(f
, &s
->icr
[1]);
852 qemu_get_be32s(f
, &s
->divide_conf
);
853 s
->count_shift
=qemu_get_be32(f
);
854 qemu_get_be32s(f
, &s
->initial_count
);
855 s
->initial_count_load_time
=qemu_get_be64(f
);
856 s
->next_time
=qemu_get_be64(f
);
859 qemu_get_timer(f
, s
->timer
);
863 static void apic_reset(void *opaque
)
865 APICState
*s
= opaque
;
867 s
->apicbase
= 0xfee00000 |
868 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
874 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
875 * time typically by BIOS, so PIC interrupt can be delivered to the
876 * processor when local APIC is enabled.
878 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
882 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
888 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
894 int apic_init(CPUState
*env
)
898 if (last_apic_id
>= MAX_APICS
)
900 s
= qemu_mallocz(sizeof(APICState
));
902 s
->id
= last_apic_id
++;
903 env
->cpuid_apic_id
= s
->id
;
908 /* XXX: mapping more APICs at the same memory location */
909 if (apic_io_memory
== 0) {
910 /* NOTE: the APIC is directly connected to the CPU - it is not
911 on the global memory bus. */
912 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
913 apic_mem_write
, NULL
);
914 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
917 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
919 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
920 qemu_register_reset(apic_reset
, s
);
922 local_apics
[s
->id
] = s
;
926 static void ioapic_service(IOAPICState
*s
)
931 uint8_t delivery_mode
;
937 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
939 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
942 entry
= s
->ioredtbl
[i
];
943 if (!(entry
& APIC_LVT_MASKED
)) {
944 trig_mode
= ((entry
>> 15) & 1);
946 dest_mode
= (entry
>> 11) & 1;
947 delivery_mode
= (entry
>> 8) & 7;
948 polarity
= (entry
>> 13) & 1;
949 if (trig_mode
== APIC_TRIGGER_EDGE
)
951 if (delivery_mode
== APIC_DM_EXTINT
)
952 vector
= pic_read_irq(isa_pic
);
954 vector
= entry
& 0xff;
956 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
957 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
958 vector
, polarity
, trig_mode
);
964 void ioapic_set_irq(void *opaque
, int vector
, int level
)
966 IOAPICState
*s
= opaque
;
968 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
969 * to GSI 2. GSI maps to ioapic 1-1. This is not
970 * the cleanest way of doing it but it should work. */
975 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
976 uint32_t mask
= 1 << vector
;
977 uint64_t entry
= s
->ioredtbl
[vector
];
979 if ((entry
>> 15) & 1) {
980 /* level triggered */
997 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
999 IOAPICState
*s
= opaque
;
1006 } else if (addr
== 0x10) {
1007 switch (s
->ioregsel
) {
1012 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1018 index
= (s
->ioregsel
- 0x10) >> 1;
1019 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1020 if (s
->ioregsel
& 1)
1021 val
= s
->ioredtbl
[index
] >> 32;
1023 val
= s
->ioredtbl
[index
] & 0xffffffff;
1027 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1033 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1035 IOAPICState
*s
= opaque
;
1042 } else if (addr
== 0x10) {
1044 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1046 switch (s
->ioregsel
) {
1048 s
->id
= (val
>> 24) & 0xff;
1054 index
= (s
->ioregsel
- 0x10) >> 1;
1055 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1056 if (s
->ioregsel
& 1) {
1057 s
->ioredtbl
[index
] &= 0xffffffff;
1058 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1060 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1061 s
->ioredtbl
[index
] |= val
;
1069 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1071 IOAPICState
*s
= opaque
;
1074 qemu_put_8s(f
, &s
->id
);
1075 qemu_put_8s(f
, &s
->ioregsel
);
1076 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1077 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1081 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1083 IOAPICState
*s
= opaque
;
1086 if (version_id
!= 1)
1089 qemu_get_8s(f
, &s
->id
);
1090 qemu_get_8s(f
, &s
->ioregsel
);
1091 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1092 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1097 static void ioapic_reset(void *opaque
)
1099 IOAPICState
*s
= opaque
;
1102 memset(s
, 0, sizeof(*s
));
1103 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1104 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1107 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1113 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1119 IOAPICState
*ioapic_init(void)
1124 s
= qemu_mallocz(sizeof(IOAPICState
));
1126 s
->id
= last_apic_id
++;
1128 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1129 ioapic_mem_write
, s
);
1130 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1132 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
1133 qemu_register_reset(ioapic_reset
, s
);