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/>
23 #include "qemu-timer.h"
24 #include "host-utils.h"
29 /* APIC Local Vector Table */
30 #define APIC_LVT_TIMER 0
31 #define APIC_LVT_THERMAL 1
32 #define APIC_LVT_PERFORM 2
33 #define APIC_LVT_LINT0 3
34 #define APIC_LVT_LINT1 4
35 #define APIC_LVT_ERROR 5
38 /* APIC delivery modes */
39 #define APIC_DM_FIXED 0
40 #define APIC_DM_LOWPRI 1
43 #define APIC_DM_INIT 5
44 #define APIC_DM_SIPI 6
45 #define APIC_DM_EXTINT 7
47 /* APIC destination mode */
48 #define APIC_DESTMODE_FLAT 0xf
49 #define APIC_DESTMODE_CLUSTER 1
51 #define APIC_TRIGGER_EDGE 0
52 #define APIC_TRIGGER_LEVEL 1
54 #define APIC_LVT_TIMER_PERIODIC (1<<17)
55 #define APIC_LVT_MASKED (1<<16)
56 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
57 #define APIC_LVT_REMOTE_IRR (1<<14)
58 #define APIC_INPUT_POLARITY (1<<13)
59 #define APIC_SEND_PENDING (1<<12)
61 #define ESR_ILLEGAL_ADDRESS (1 << 7)
63 #define APIC_SV_ENABLE (1 << 8)
66 #define MAX_APIC_WORDS 8
68 /* Intel APIC constants: from include/asm/msidef.h */
69 #define MSI_DATA_VECTOR_SHIFT 0
70 #define MSI_DATA_VECTOR_MASK 0x000000ff
71 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
72 #define MSI_DATA_TRIGGER_SHIFT 15
73 #define MSI_DATA_LEVEL_SHIFT 14
74 #define MSI_ADDR_DEST_MODE_SHIFT 2
75 #define MSI_ADDR_DEST_ID_SHIFT 12
76 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
78 #define MSI_ADDR_BASE 0xfee00000
79 #define MSI_ADDR_SIZE 0x100000
81 typedef struct APICState
{
87 uint32_t spurious_vec
;
90 uint32_t isr
[8]; /* in service register */
91 uint32_t tmr
[8]; /* trigger mode register */
92 uint32_t irr
[8]; /* interrupt request register */
93 uint32_t lvt
[APIC_LVT_NB
];
94 uint32_t esr
; /* error register */
99 uint32_t initial_count
;
100 int64_t initial_count_load_time
, next_time
;
107 static int apic_io_memory
;
108 static APICState
*local_apics
[MAX_APICS
+ 1];
109 static int last_apic_idx
= 0;
110 static int apic_irq_delivered
;
113 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
114 static void apic_update_irq(APICState
*s
);
115 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
116 uint8_t dest
, uint8_t dest_mode
);
118 /* Find first bit starting from msb */
119 static int fls_bit(uint32_t value
)
121 return 31 - clz32(value
);
124 /* Find first bit starting from lsb */
125 static int ffs_bit(uint32_t value
)
130 static inline void set_bit(uint32_t *tab
, int index
)
134 mask
= 1 << (index
& 0x1f);
138 static inline void reset_bit(uint32_t *tab
, int index
)
142 mask
= 1 << (index
& 0x1f);
146 static inline int get_bit(uint32_t *tab
, int index
)
150 mask
= 1 << (index
& 0x1f);
151 return !!(tab
[i
] & mask
);
154 static void apic_local_deliver(CPUState
*env
, int vector
)
156 APICState
*s
= env
->apic_state
;
157 uint32_t lvt
= s
->lvt
[vector
];
160 if (lvt
& APIC_LVT_MASKED
)
163 switch ((lvt
>> 8) & 7) {
165 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
169 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
173 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
177 trigger_mode
= APIC_TRIGGER_EDGE
;
178 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
179 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
180 trigger_mode
= APIC_TRIGGER_LEVEL
;
181 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
185 void apic_deliver_pic_intr(CPUState
*env
, int level
)
188 apic_local_deliver(env
, APIC_LVT_LINT0
);
190 APICState
*s
= env
->apic_state
;
191 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
193 switch ((lvt
>> 8) & 7) {
195 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
197 reset_bit(s
->irr
, lvt
& 0xff);
200 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
206 #define foreach_apic(apic, deliver_bitmask, code) \
208 int __i, __j, __mask;\
209 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
210 __mask = deliver_bitmask[__i];\
212 for(__j = 0; __j < 32; __j++) {\
213 if (__mask & (1 << __j)) {\
214 apic = local_apics[__i * 32 + __j];\
224 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
225 uint8_t delivery_mode
,
226 uint8_t vector_num
, uint8_t polarity
,
227 uint8_t trigger_mode
)
229 APICState
*apic_iter
;
231 switch (delivery_mode
) {
233 /* XXX: search for focus processor, arbitration */
237 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
238 if (deliver_bitmask
[i
]) {
239 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
244 apic_iter
= local_apics
[d
];
246 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
256 foreach_apic(apic_iter
, deliver_bitmask
,
257 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
261 foreach_apic(apic_iter
, deliver_bitmask
,
262 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
266 /* normal INIT IPI sent to processors */
267 foreach_apic(apic_iter
, deliver_bitmask
,
268 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_INIT
) );
272 /* handled in I/O APIC code */
279 foreach_apic(apic_iter
, deliver_bitmask
,
280 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
283 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
284 uint8_t delivery_mode
, uint8_t vector_num
,
285 uint8_t polarity
, uint8_t trigger_mode
)
287 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
289 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
290 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
294 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
296 APICState
*s
= env
->apic_state
;
298 printf("cpu_set_apic_base: %016" PRIx64
"\n", val
);
302 s
->apicbase
= (val
& 0xfffff000) |
303 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
304 /* if disabled, cannot be enabled again */
305 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
306 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
307 env
->cpuid_features
&= ~CPUID_APIC
;
308 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
312 uint64_t cpu_get_apic_base(CPUState
*env
)
314 APICState
*s
= env
->apic_state
;
316 printf("cpu_get_apic_base: %016" PRIx64
"\n",
317 s
? (uint64_t)s
->apicbase
: 0);
319 return s
? s
->apicbase
: 0;
322 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
324 APICState
*s
= env
->apic_state
;
327 s
->tpr
= (val
& 0x0f) << 4;
331 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
333 APICState
*s
= env
->apic_state
;
334 return s
? s
->tpr
>> 4 : 0;
337 /* return -1 if no bit is set */
338 static int get_highest_priority_int(uint32_t *tab
)
341 for(i
= 7; i
>= 0; i
--) {
343 return i
* 32 + fls_bit(tab
[i
]);
349 static int apic_get_ppr(APICState
*s
)
354 isrv
= get_highest_priority_int(s
->isr
);
365 static int apic_get_arb_pri(APICState
*s
)
367 /* XXX: arbitration */
371 /* signal the CPU if an irq is pending */
372 static void apic_update_irq(APICState
*s
)
375 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
377 irrv
= get_highest_priority_int(s
->irr
);
380 ppr
= apic_get_ppr(s
);
381 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
383 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
386 void apic_reset_irq_delivered(void)
388 apic_irq_delivered
= 0;
391 int apic_get_irq_delivered(void)
393 return apic_irq_delivered
;
396 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
398 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
400 set_bit(s
->irr
, vector_num
);
402 set_bit(s
->tmr
, vector_num
);
404 reset_bit(s
->tmr
, vector_num
);
408 static void apic_eoi(APICState
*s
)
411 isrv
= get_highest_priority_int(s
->isr
);
414 reset_bit(s
->isr
, isrv
);
415 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
416 set the remote IRR bit for level triggered interrupts. */
420 static int apic_find_dest(uint8_t dest
)
422 APICState
*apic
= local_apics
[dest
];
425 if (apic
&& apic
->id
== dest
)
426 return dest
; /* shortcut in case apic->id == apic->idx */
428 for (i
= 0; i
< MAX_APICS
; i
++) {
429 apic
= local_apics
[i
];
430 if (apic
&& apic
->id
== dest
)
437 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
438 uint8_t dest
, uint8_t dest_mode
)
440 APICState
*apic_iter
;
443 if (dest_mode
== 0) {
445 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
447 int idx
= apic_find_dest(dest
);
448 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
450 set_bit(deliver_bitmask
, idx
);
453 /* XXX: cluster mode */
454 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
455 for(i
= 0; i
< MAX_APICS
; i
++) {
456 apic_iter
= local_apics
[i
];
458 if (apic_iter
->dest_mode
== 0xf) {
459 if (dest
& apic_iter
->log_dest
)
460 set_bit(deliver_bitmask
, i
);
461 } else if (apic_iter
->dest_mode
== 0x0) {
462 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
463 (dest
& apic_iter
->log_dest
& 0x0f)) {
464 set_bit(deliver_bitmask
, i
);
473 void apic_init_reset(CPUState
*env
)
475 APICState
*s
= env
->apic_state
;
482 s
->spurious_vec
= 0xff;
485 memset(s
->isr
, 0, sizeof(s
->isr
));
486 memset(s
->tmr
, 0, sizeof(s
->tmr
));
487 memset(s
->irr
, 0, sizeof(s
->irr
));
488 for(i
= 0; i
< APIC_LVT_NB
; i
++)
489 s
->lvt
[i
] = 1 << 16; /* mask LVT */
491 memset(s
->icr
, 0, sizeof(s
->icr
));
494 s
->initial_count
= 0;
495 s
->initial_count_load_time
= 0;
497 s
->wait_for_sipi
= 1;
499 env
->halted
= !(s
->apicbase
& MSR_IA32_APICBASE_BSP
);
502 static void apic_startup(APICState
*s
, int vector_num
)
504 s
->sipi_vector
= vector_num
;
505 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
508 void apic_sipi(CPUState
*env
)
510 APICState
*s
= env
->apic_state
;
512 cpu_reset_interrupt(env
, CPU_INTERRUPT_SIPI
);
514 if (!s
->wait_for_sipi
)
518 cpu_x86_load_seg_cache(env
, R_CS
, s
->sipi_vector
<< 8, s
->sipi_vector
<< 12,
519 env
->segs
[R_CS
].limit
, env
->segs
[R_CS
].flags
);
521 s
->wait_for_sipi
= 0;
524 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
525 uint8_t delivery_mode
, uint8_t vector_num
,
526 uint8_t polarity
, uint8_t trigger_mode
)
528 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
529 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
530 APICState
*apic_iter
;
532 switch (dest_shorthand
) {
534 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
537 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
538 set_bit(deliver_bitmask
, s
->idx
);
541 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
544 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
545 reset_bit(deliver_bitmask
, s
->idx
);
549 switch (delivery_mode
) {
552 int trig_mode
= (s
->icr
[0] >> 15) & 1;
553 int level
= (s
->icr
[0] >> 14) & 1;
554 if (level
== 0 && trig_mode
== 1) {
555 foreach_apic(apic_iter
, deliver_bitmask
,
556 apic_iter
->arb_id
= apic_iter
->id
);
563 foreach_apic(apic_iter
, deliver_bitmask
,
564 apic_startup(apic_iter
, vector_num
) );
568 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
572 int apic_get_interrupt(CPUState
*env
)
574 APICState
*s
= env
->apic_state
;
577 /* if the APIC is installed or enabled, we let the 8259 handle the
581 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
584 /* XXX: spurious IRQ handling */
585 intno
= get_highest_priority_int(s
->irr
);
588 if (s
->tpr
&& intno
<= s
->tpr
)
589 return s
->spurious_vec
& 0xff;
590 reset_bit(s
->irr
, intno
);
591 set_bit(s
->isr
, intno
);
596 int apic_accept_pic_intr(CPUState
*env
)
598 APICState
*s
= env
->apic_state
;
604 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
606 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
607 (lvt0
& APIC_LVT_MASKED
) == 0)
613 static uint32_t apic_get_current_count(APICState
*s
)
617 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
619 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
621 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
623 if (d
>= s
->initial_count
)
626 val
= s
->initial_count
- d
;
631 static void apic_timer_update(APICState
*s
, int64_t current_time
)
633 int64_t next_time
, d
;
635 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
636 d
= (current_time
- s
->initial_count_load_time
) >>
638 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
639 if (!s
->initial_count
)
641 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
643 if (d
>= s
->initial_count
)
645 d
= (uint64_t)s
->initial_count
+ 1;
647 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
648 qemu_mod_timer(s
->timer
, next_time
);
649 s
->next_time
= next_time
;
652 qemu_del_timer(s
->timer
);
656 static void apic_timer(void *opaque
)
658 APICState
*s
= opaque
;
660 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
661 apic_timer_update(s
, s
->next_time
);
664 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
669 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
674 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
678 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
682 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
689 env
= cpu_single_env
;
694 index
= (addr
>> 4) & 0xff;
699 case 0x03: /* version */
700 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
706 val
= apic_get_arb_pri(s
);
710 val
= apic_get_ppr(s
);
716 val
= s
->log_dest
<< 24;
719 val
= s
->dest_mode
<< 28;
722 val
= s
->spurious_vec
;
725 val
= s
->isr
[index
& 7];
728 val
= s
->tmr
[index
& 7];
731 val
= s
->irr
[index
& 7];
738 val
= s
->icr
[index
& 1];
741 val
= s
->lvt
[index
- 0x32];
744 val
= s
->initial_count
;
747 val
= apic_get_current_count(s
);
750 val
= s
->divide_conf
;
753 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
758 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
763 static void apic_send_msi(target_phys_addr_t addr
, uint32 data
)
765 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
766 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
767 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
768 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
769 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
770 /* XXX: Ignore redirection hint. */
771 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, 0, trigger_mode
);
774 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
778 int index
= (addr
>> 4) & 0xff;
779 if (addr
> 0xfff || !index
) {
780 /* MSI and MMIO APIC are at the same memory location,
781 * but actually not on the global bus: MSI is on PCI bus
782 * APIC is connected directly to the CPU.
783 * Mapping them on the global bus happens to work because
784 * MSI registers are reserved in APIC MMIO and vice versa. */
785 apic_send_msi(addr
, val
);
789 env
= cpu_single_env
;
795 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
815 s
->log_dest
= val
>> 24;
818 s
->dest_mode
= val
>> 28;
821 s
->spurious_vec
= val
& 0x1ff;
831 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
832 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
833 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
840 int n
= index
- 0x32;
842 if (n
== APIC_LVT_TIMER
)
843 apic_timer_update(s
, qemu_get_clock(vm_clock
));
847 s
->initial_count
= val
;
848 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
849 apic_timer_update(s
, s
->initial_count_load_time
);
856 s
->divide_conf
= val
& 0xb;
857 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
858 s
->count_shift
= (v
+ 1) & 7;
862 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
867 /* This function is only used for old state version 1 and 2 */
868 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
870 APICState
*s
= opaque
;
876 /* XXX: what if the base changes? (registered memory regions) */
877 qemu_get_be32s(f
, &s
->apicbase
);
878 qemu_get_8s(f
, &s
->id
);
879 qemu_get_8s(f
, &s
->arb_id
);
880 qemu_get_8s(f
, &s
->tpr
);
881 qemu_get_be32s(f
, &s
->spurious_vec
);
882 qemu_get_8s(f
, &s
->log_dest
);
883 qemu_get_8s(f
, &s
->dest_mode
);
884 for (i
= 0; i
< 8; i
++) {
885 qemu_get_be32s(f
, &s
->isr
[i
]);
886 qemu_get_be32s(f
, &s
->tmr
[i
]);
887 qemu_get_be32s(f
, &s
->irr
[i
]);
889 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
890 qemu_get_be32s(f
, &s
->lvt
[i
]);
892 qemu_get_be32s(f
, &s
->esr
);
893 qemu_get_be32s(f
, &s
->icr
[0]);
894 qemu_get_be32s(f
, &s
->icr
[1]);
895 qemu_get_be32s(f
, &s
->divide_conf
);
896 s
->count_shift
=qemu_get_be32(f
);
897 qemu_get_be32s(f
, &s
->initial_count
);
898 s
->initial_count_load_time
=qemu_get_be64(f
);
899 s
->next_time
=qemu_get_be64(f
);
902 qemu_get_timer(f
, s
->timer
);
906 static const VMStateDescription vmstate_apic
= {
909 .minimum_version_id
= 3,
910 .minimum_version_id_old
= 1,
911 .load_state_old
= apic_load_old
,
912 .fields
= (VMStateField
[]) {
913 VMSTATE_UINT32(apicbase
, APICState
),
914 VMSTATE_UINT8(id
, APICState
),
915 VMSTATE_UINT8(arb_id
, APICState
),
916 VMSTATE_UINT8(tpr
, APICState
),
917 VMSTATE_UINT32(spurious_vec
, APICState
),
918 VMSTATE_UINT8(log_dest
, APICState
),
919 VMSTATE_UINT8(dest_mode
, APICState
),
920 VMSTATE_UINT32_ARRAY(isr
, APICState
, 8),
921 VMSTATE_UINT32_ARRAY(tmr
, APICState
, 8),
922 VMSTATE_UINT32_ARRAY(irr
, APICState
, 8),
923 VMSTATE_UINT32_ARRAY(lvt
, APICState
, APIC_LVT_NB
),
924 VMSTATE_UINT32(esr
, APICState
),
925 VMSTATE_UINT32_ARRAY(icr
, APICState
, 2),
926 VMSTATE_UINT32(divide_conf
, APICState
),
927 VMSTATE_INT32(count_shift
, APICState
),
928 VMSTATE_UINT32(initial_count
, APICState
),
929 VMSTATE_INT64(initial_count_load_time
, APICState
),
930 VMSTATE_INT64(next_time
, APICState
),
931 VMSTATE_TIMER(timer
, APICState
),
932 VMSTATE_END_OF_LIST()
936 static void apic_reset(void *opaque
)
938 APICState
*s
= opaque
;
941 bsp
= cpu_is_bsp(s
->cpu_env
);
942 s
->apicbase
= 0xfee00000 |
943 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
945 cpu_reset(s
->cpu_env
);
946 apic_init_reset(s
->cpu_env
);
950 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
951 * time typically by BIOS, so PIC interrupt can be delivered to the
952 * processor when local APIC is enabled.
954 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
958 static CPUReadMemoryFunc
* const apic_mem_read
[3] = {
964 static CPUWriteMemoryFunc
* const apic_mem_write
[3] = {
970 int apic_init(CPUState
*env
)
974 if (last_apic_idx
>= MAX_APICS
)
976 s
= qemu_mallocz(sizeof(APICState
));
978 s
->idx
= last_apic_idx
++;
979 s
->id
= env
->cpuid_apic_id
;
984 /* XXX: mapping more APICs at the same memory location */
985 if (apic_io_memory
== 0) {
986 /* NOTE: the APIC is directly connected to the CPU - it is not
987 on the global memory bus. */
988 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
989 apic_mem_write
, NULL
);
990 /* XXX: what if the base changes? */
991 cpu_register_physical_memory(MSI_ADDR_BASE
, MSI_ADDR_SIZE
,
994 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
996 vmstate_register(s
->idx
, &vmstate_apic
, s
);
997 qemu_register_reset(apic_reset
, s
);
999 local_apics
[s
->idx
] = s
;