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 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 #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
,
220 uint8_t vector_num
, uint8_t polarity
,
221 uint8_t trigger_mode
)
223 APICState
*apic_iter
;
225 switch (delivery_mode
) {
227 /* XXX: search for focus processor, arbitration */
231 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
232 if (deliver_bitmask
[i
]) {
233 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
238 apic_iter
= local_apics
[d
];
240 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
250 foreach_apic(apic_iter
, deliver_bitmask
,
251 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
255 foreach_apic(apic_iter
, deliver_bitmask
,
256 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
260 /* normal INIT IPI sent to processors */
261 foreach_apic(apic_iter
, deliver_bitmask
,
262 apic_init_ipi(apic_iter
) );
266 /* handled in I/O APIC code */
273 foreach_apic(apic_iter
, deliver_bitmask
,
274 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
277 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
279 APICState
*s
= env
->apic_state
;
281 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
283 s
->apicbase
= (val
& 0xfffff000) |
284 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
285 /* if disabled, cannot be enabled again */
286 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
287 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
288 env
->cpuid_features
&= ~CPUID_APIC
;
289 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
293 uint64_t cpu_get_apic_base(CPUState
*env
)
295 APICState
*s
= env
->apic_state
;
297 printf("cpu_get_apic_base: %016" PRIx64
"\n", (uint64_t)s
->apicbase
);
302 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
304 APICState
*s
= env
->apic_state
;
305 s
->tpr
= (val
& 0x0f) << 4;
309 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
311 APICState
*s
= env
->apic_state
;
315 /* return -1 if no bit is set */
316 static int get_highest_priority_int(uint32_t *tab
)
319 for(i
= 7; i
>= 0; i
--) {
321 return i
* 32 + fls_bit(tab
[i
]);
327 static int apic_get_ppr(APICState
*s
)
332 isrv
= get_highest_priority_int(s
->isr
);
343 static int apic_get_arb_pri(APICState
*s
)
345 /* XXX: arbitration */
349 /* signal the CPU if an irq is pending */
350 static void apic_update_irq(APICState
*s
)
353 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
355 irrv
= get_highest_priority_int(s
->irr
);
358 ppr
= apic_get_ppr(s
);
359 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
361 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
364 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
366 set_bit(s
->irr
, vector_num
);
368 set_bit(s
->tmr
, vector_num
);
370 reset_bit(s
->tmr
, vector_num
);
374 static void apic_eoi(APICState
*s
)
377 isrv
= get_highest_priority_int(s
->isr
);
380 reset_bit(s
->isr
, isrv
);
381 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
382 set the remote IRR bit for level triggered interrupts. */
386 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
387 uint8_t dest
, uint8_t dest_mode
)
389 APICState
*apic_iter
;
392 if (dest_mode
== 0) {
394 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
396 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
397 set_bit(deliver_bitmask
, dest
);
400 /* XXX: cluster mode */
401 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
402 for(i
= 0; i
< MAX_APICS
; i
++) {
403 apic_iter
= local_apics
[i
];
405 if (apic_iter
->dest_mode
== 0xf) {
406 if (dest
& apic_iter
->log_dest
)
407 set_bit(deliver_bitmask
, i
);
408 } else if (apic_iter
->dest_mode
== 0x0) {
409 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
410 (dest
& apic_iter
->log_dest
& 0x0f)) {
411 set_bit(deliver_bitmask
, i
);
420 static void apic_init_ipi(APICState
*s
)
425 s
->spurious_vec
= 0xff;
428 memset(s
->isr
, 0, sizeof(s
->isr
));
429 memset(s
->tmr
, 0, sizeof(s
->tmr
));
430 memset(s
->irr
, 0, sizeof(s
->irr
));
431 for(i
= 0; i
< APIC_LVT_NB
; i
++)
432 s
->lvt
[i
] = 1 << 16; /* mask LVT */
434 memset(s
->icr
, 0, sizeof(s
->icr
));
437 s
->initial_count
= 0;
438 s
->initial_count_load_time
= 0;
442 /* send a SIPI message to the CPU to start it */
443 static void apic_startup(APICState
*s
, int vector_num
)
445 CPUState
*env
= s
->cpu_env
;
449 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
454 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
455 uint8_t delivery_mode
, uint8_t vector_num
,
456 uint8_t polarity
, uint8_t trigger_mode
)
458 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
459 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
460 APICState
*apic_iter
;
462 switch (dest_shorthand
) {
464 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
467 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
468 set_bit(deliver_bitmask
, s
->id
);
471 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
474 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
475 reset_bit(deliver_bitmask
, s
->id
);
479 switch (delivery_mode
) {
482 int trig_mode
= (s
->icr
[0] >> 15) & 1;
483 int level
= (s
->icr
[0] >> 14) & 1;
484 if (level
== 0 && trig_mode
== 1) {
485 foreach_apic(apic_iter
, deliver_bitmask
,
486 apic_iter
->arb_id
= apic_iter
->id
);
493 foreach_apic(apic_iter
, deliver_bitmask
,
494 apic_startup(apic_iter
, vector_num
) );
498 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
502 int apic_get_interrupt(CPUState
*env
)
504 APICState
*s
= env
->apic_state
;
507 /* if the APIC is installed or enabled, we let the 8259 handle the
511 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
514 /* XXX: spurious IRQ handling */
515 intno
= get_highest_priority_int(s
->irr
);
518 if (s
->tpr
&& intno
<= s
->tpr
)
519 return s
->spurious_vec
& 0xff;
520 reset_bit(s
->irr
, intno
);
521 set_bit(s
->isr
, intno
);
526 int apic_accept_pic_intr(CPUState
*env
)
528 APICState
*s
= env
->apic_state
;
534 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
536 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
537 (lvt0
& APIC_LVT_MASKED
) == 0)
543 static uint32_t apic_get_current_count(APICState
*s
)
547 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
549 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
551 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
553 if (d
>= s
->initial_count
)
556 val
= s
->initial_count
- d
;
561 static void apic_timer_update(APICState
*s
, int64_t current_time
)
563 int64_t next_time
, d
;
565 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
566 d
= (current_time
- s
->initial_count_load_time
) >>
568 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
569 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
571 if (d
>= s
->initial_count
)
573 d
= (uint64_t)s
->initial_count
+ 1;
575 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
576 qemu_mod_timer(s
->timer
, next_time
);
577 s
->next_time
= next_time
;
580 qemu_del_timer(s
->timer
);
584 static void apic_timer(void *opaque
)
586 APICState
*s
= opaque
;
588 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
589 apic_timer_update(s
, s
->next_time
);
592 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
597 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
602 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
606 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
610 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
617 env
= cpu_single_env
;
622 index
= (addr
>> 4) & 0xff;
627 case 0x03: /* version */
628 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
634 val
= apic_get_arb_pri(s
);
638 val
= apic_get_ppr(s
);
644 val
= s
->log_dest
<< 24;
647 val
= s
->dest_mode
<< 28;
650 val
= s
->spurious_vec
;
653 val
= s
->isr
[index
& 7];
656 val
= s
->tmr
[index
& 7];
659 val
= s
->irr
[index
& 7];
666 val
= s
->icr
[index
& 1];
669 val
= s
->lvt
[index
- 0x32];
672 val
= s
->initial_count
;
675 val
= apic_get_current_count(s
);
678 val
= s
->divide_conf
;
681 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
686 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
691 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
697 env
= cpu_single_env
;
703 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
706 index
= (addr
>> 4) & 0xff;
724 s
->log_dest
= val
>> 24;
727 s
->dest_mode
= val
>> 28;
730 s
->spurious_vec
= val
& 0x1ff;
740 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
741 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
742 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
749 int n
= index
- 0x32;
751 if (n
== APIC_LVT_TIMER
)
752 apic_timer_update(s
, qemu_get_clock(vm_clock
));
756 s
->initial_count
= val
;
757 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
758 apic_timer_update(s
, s
->initial_count_load_time
);
765 s
->divide_conf
= val
& 0xb;
766 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
767 s
->count_shift
= (v
+ 1) & 7;
771 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
776 static void apic_save(QEMUFile
*f
, void *opaque
)
778 APICState
*s
= opaque
;
781 qemu_put_be32s(f
, &s
->apicbase
);
782 qemu_put_8s(f
, &s
->id
);
783 qemu_put_8s(f
, &s
->arb_id
);
784 qemu_put_8s(f
, &s
->tpr
);
785 qemu_put_be32s(f
, &s
->spurious_vec
);
786 qemu_put_8s(f
, &s
->log_dest
);
787 qemu_put_8s(f
, &s
->dest_mode
);
788 for (i
= 0; i
< 8; i
++) {
789 qemu_put_be32s(f
, &s
->isr
[i
]);
790 qemu_put_be32s(f
, &s
->tmr
[i
]);
791 qemu_put_be32s(f
, &s
->irr
[i
]);
793 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
794 qemu_put_be32s(f
, &s
->lvt
[i
]);
796 qemu_put_be32s(f
, &s
->esr
);
797 qemu_put_be32s(f
, &s
->icr
[0]);
798 qemu_put_be32s(f
, &s
->icr
[1]);
799 qemu_put_be32s(f
, &s
->divide_conf
);
800 qemu_put_be32(f
, s
->count_shift
);
801 qemu_put_be32s(f
, &s
->initial_count
);
802 qemu_put_be64(f
, s
->initial_count_load_time
);
803 qemu_put_be64(f
, s
->next_time
);
805 qemu_put_timer(f
, s
->timer
);
808 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
810 APICState
*s
= opaque
;
816 /* XXX: what if the base changes? (registered memory regions) */
817 qemu_get_be32s(f
, &s
->apicbase
);
818 qemu_get_8s(f
, &s
->id
);
819 qemu_get_8s(f
, &s
->arb_id
);
820 qemu_get_8s(f
, &s
->tpr
);
821 qemu_get_be32s(f
, &s
->spurious_vec
);
822 qemu_get_8s(f
, &s
->log_dest
);
823 qemu_get_8s(f
, &s
->dest_mode
);
824 for (i
= 0; i
< 8; i
++) {
825 qemu_get_be32s(f
, &s
->isr
[i
]);
826 qemu_get_be32s(f
, &s
->tmr
[i
]);
827 qemu_get_be32s(f
, &s
->irr
[i
]);
829 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
830 qemu_get_be32s(f
, &s
->lvt
[i
]);
832 qemu_get_be32s(f
, &s
->esr
);
833 qemu_get_be32s(f
, &s
->icr
[0]);
834 qemu_get_be32s(f
, &s
->icr
[1]);
835 qemu_get_be32s(f
, &s
->divide_conf
);
836 s
->count_shift
=qemu_get_be32(f
);
837 qemu_get_be32s(f
, &s
->initial_count
);
838 s
->initial_count_load_time
=qemu_get_be64(f
);
839 s
->next_time
=qemu_get_be64(f
);
842 qemu_get_timer(f
, s
->timer
);
846 static void apic_reset(void *opaque
)
848 APICState
*s
= opaque
;
853 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
854 * time typically by BIOS, so PIC interrupt can be delivered to the
855 * processor when local APIC is enabled.
857 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
861 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
867 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
873 int apic_init(CPUState
*env
)
877 if (last_apic_id
>= MAX_APICS
)
879 s
= qemu_mallocz(sizeof(APICState
));
883 s
->id
= last_apic_id
++;
884 env
->cpuid_apic_id
= s
->id
;
886 s
->apicbase
= 0xfee00000 |
887 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
891 /* XXX: mapping more APICs at the same memory location */
892 if (apic_io_memory
== 0) {
893 /* NOTE: the APIC is directly connected to the CPU - it is not
894 on the global memory bus. */
895 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
896 apic_mem_write
, NULL
);
897 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
900 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
902 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
903 qemu_register_reset(apic_reset
, s
);
905 local_apics
[s
->id
] = s
;
909 static void ioapic_service(IOAPICState
*s
)
914 uint8_t delivery_mode
;
920 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
922 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
925 entry
= s
->ioredtbl
[i
];
926 if (!(entry
& APIC_LVT_MASKED
)) {
927 trig_mode
= ((entry
>> 15) & 1);
929 dest_mode
= (entry
>> 11) & 1;
930 delivery_mode
= (entry
>> 8) & 7;
931 polarity
= (entry
>> 13) & 1;
932 if (trig_mode
== APIC_TRIGGER_EDGE
)
934 if (delivery_mode
== APIC_DM_EXTINT
)
935 vector
= pic_read_irq(isa_pic
);
937 vector
= entry
& 0xff;
939 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
940 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
941 vector
, polarity
, trig_mode
);
947 void ioapic_set_irq(void *opaque
, int vector
, int level
)
949 IOAPICState
*s
= opaque
;
951 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
952 uint32_t mask
= 1 << vector
;
953 uint64_t entry
= s
->ioredtbl
[vector
];
955 if ((entry
>> 15) & 1) {
956 /* level triggered */
973 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
975 IOAPICState
*s
= opaque
;
982 } else if (addr
== 0x10) {
983 switch (s
->ioregsel
) {
988 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
994 index
= (s
->ioregsel
- 0x10) >> 1;
995 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
997 val
= s
->ioredtbl
[index
] >> 32;
999 val
= s
->ioredtbl
[index
] & 0xffffffff;
1003 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1009 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1011 IOAPICState
*s
= opaque
;
1018 } else if (addr
== 0x10) {
1020 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1022 switch (s
->ioregsel
) {
1024 s
->id
= (val
>> 24) & 0xff;
1030 index
= (s
->ioregsel
- 0x10) >> 1;
1031 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1032 if (s
->ioregsel
& 1) {
1033 s
->ioredtbl
[index
] &= 0xffffffff;
1034 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1036 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1037 s
->ioredtbl
[index
] |= val
;
1045 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1047 IOAPICState
*s
= opaque
;
1050 qemu_put_8s(f
, &s
->id
);
1051 qemu_put_8s(f
, &s
->ioregsel
);
1052 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1053 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1057 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1059 IOAPICState
*s
= opaque
;
1062 if (version_id
!= 1)
1065 qemu_get_8s(f
, &s
->id
);
1066 qemu_get_8s(f
, &s
->ioregsel
);
1067 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1068 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1073 static void ioapic_reset(void *opaque
)
1075 IOAPICState
*s
= opaque
;
1078 memset(s
, 0, sizeof(*s
));
1079 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1080 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1083 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1089 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1095 IOAPICState
*ioapic_init(void)
1100 s
= qemu_mallocz(sizeof(IOAPICState
));
1104 s
->id
= last_apic_id
++;
1106 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1107 ioapic_mem_write
, s
);
1108 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1110 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
1111 qemu_register_reset(ioapic_reset
, s
);