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/>
24 #include "qemu-timer.h"
25 #include "host-utils.h"
29 //#define DEBUG_COALESCING
32 #define DPRINTF(fmt, ...) \
33 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...)
38 #ifdef DEBUG_COALESCING
39 #define DPRINTF_C(fmt, ...) \
40 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
42 #define DPRINTF_C(fmt, ...)
45 /* APIC Local Vector Table */
46 #define APIC_LVT_TIMER 0
47 #define APIC_LVT_THERMAL 1
48 #define APIC_LVT_PERFORM 2
49 #define APIC_LVT_LINT0 3
50 #define APIC_LVT_LINT1 4
51 #define APIC_LVT_ERROR 5
54 /* APIC delivery modes */
55 #define APIC_DM_FIXED 0
56 #define APIC_DM_LOWPRI 1
59 #define APIC_DM_INIT 5
60 #define APIC_DM_SIPI 6
61 #define APIC_DM_EXTINT 7
63 /* APIC destination mode */
64 #define APIC_DESTMODE_FLAT 0xf
65 #define APIC_DESTMODE_CLUSTER 1
67 #define APIC_TRIGGER_EDGE 0
68 #define APIC_TRIGGER_LEVEL 1
70 #define APIC_LVT_TIMER_PERIODIC (1<<17)
71 #define APIC_LVT_MASKED (1<<16)
72 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
73 #define APIC_LVT_REMOTE_IRR (1<<14)
74 #define APIC_INPUT_POLARITY (1<<13)
75 #define APIC_SEND_PENDING (1<<12)
77 #define ESR_ILLEGAL_ADDRESS (1 << 7)
79 #define APIC_SV_ENABLE (1 << 8)
82 #define MAX_APIC_WORDS 8
84 /* Intel APIC constants: from include/asm/msidef.h */
85 #define MSI_DATA_VECTOR_SHIFT 0
86 #define MSI_DATA_VECTOR_MASK 0x000000ff
87 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
88 #define MSI_DATA_TRIGGER_SHIFT 15
89 #define MSI_DATA_LEVEL_SHIFT 14
90 #define MSI_ADDR_DEST_MODE_SHIFT 2
91 #define MSI_ADDR_DEST_ID_SHIFT 12
92 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
94 #define MSI_ADDR_BASE 0xfee00000
95 #define MSI_ADDR_SIZE 0x100000
97 typedef struct APICState
{
103 uint32_t spurious_vec
;
106 uint32_t isr
[8]; /* in service register */
107 uint32_t tmr
[8]; /* trigger mode register */
108 uint32_t irr
[8]; /* interrupt request register */
109 uint32_t lvt
[APIC_LVT_NB
];
110 uint32_t esr
; /* error register */
113 uint32_t divide_conf
;
115 uint32_t initial_count
;
116 int64_t initial_count_load_time
, next_time
;
123 static int apic_io_memory
;
124 static APICState
*local_apics
[MAX_APICS
+ 1];
125 static int last_apic_idx
= 0;
126 static int apic_irq_delivered
;
129 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
130 static void apic_update_irq(APICState
*s
);
131 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
132 uint8_t dest
, uint8_t dest_mode
);
134 /* Find first bit starting from msb */
135 static int fls_bit(uint32_t value
)
137 return 31 - clz32(value
);
140 /* Find first bit starting from lsb */
141 static int ffs_bit(uint32_t value
)
146 static inline void set_bit(uint32_t *tab
, int index
)
150 mask
= 1 << (index
& 0x1f);
154 static inline void reset_bit(uint32_t *tab
, int index
)
158 mask
= 1 << (index
& 0x1f);
162 static inline int get_bit(uint32_t *tab
, int index
)
166 mask
= 1 << (index
& 0x1f);
167 return !!(tab
[i
] & mask
);
170 static void apic_local_deliver(CPUState
*env
, int vector
)
172 APICState
*s
= env
->apic_state
;
173 uint32_t lvt
= s
->lvt
[vector
];
176 DPRINTF("%s: vector %d delivery mode %d\n", __func__
, vector
,
178 if (lvt
& APIC_LVT_MASKED
)
181 switch ((lvt
>> 8) & 7) {
183 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
187 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
191 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
195 trigger_mode
= APIC_TRIGGER_EDGE
;
196 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
197 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
198 trigger_mode
= APIC_TRIGGER_LEVEL
;
199 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
203 void apic_deliver_pic_intr(CPUState
*env
, int level
)
206 apic_local_deliver(env
, APIC_LVT_LINT0
);
208 APICState
*s
= env
->apic_state
;
209 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
211 switch ((lvt
>> 8) & 7) {
213 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
215 reset_bit(s
->irr
, lvt
& 0xff);
218 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
224 #define foreach_apic(apic, deliver_bitmask, code) \
226 int __i, __j, __mask;\
227 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
228 __mask = deliver_bitmask[__i];\
230 for(__j = 0; __j < 32; __j++) {\
231 if (__mask & (1 << __j)) {\
232 apic = local_apics[__i * 32 + __j];\
242 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
243 uint8_t delivery_mode
,
244 uint8_t vector_num
, uint8_t polarity
,
245 uint8_t trigger_mode
)
247 APICState
*apic_iter
;
249 switch (delivery_mode
) {
251 /* XXX: search for focus processor, arbitration */
255 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
256 if (deliver_bitmask
[i
]) {
257 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
262 apic_iter
= local_apics
[d
];
264 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
274 foreach_apic(apic_iter
, deliver_bitmask
,
275 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
279 foreach_apic(apic_iter
, deliver_bitmask
,
280 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
284 /* normal INIT IPI sent to processors */
285 foreach_apic(apic_iter
, deliver_bitmask
,
286 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_INIT
) );
290 /* handled in I/O APIC code */
297 foreach_apic(apic_iter
, deliver_bitmask
,
298 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
301 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
302 uint8_t delivery_mode
, uint8_t vector_num
,
303 uint8_t polarity
, uint8_t trigger_mode
)
305 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
307 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
308 " polarity %d trigger_mode %d\n", __func__
, dest
, dest_mode
,
309 delivery_mode
, vector_num
, polarity
, trigger_mode
);
310 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
311 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
315 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
317 APICState
*s
= env
->apic_state
;
319 DPRINTF("cpu_set_apic_base: %016" PRIx64
"\n", val
);
322 s
->apicbase
= (val
& 0xfffff000) |
323 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
324 /* if disabled, cannot be enabled again */
325 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
326 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
327 env
->cpuid_features
&= ~CPUID_APIC
;
328 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
332 uint64_t cpu_get_apic_base(CPUState
*env
)
334 APICState
*s
= env
->apic_state
;
336 DPRINTF("cpu_get_apic_base: %016" PRIx64
"\n",
337 s
? (uint64_t)s
->apicbase
: 0);
338 return s
? s
->apicbase
: 0;
341 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
343 APICState
*s
= env
->apic_state
;
346 s
->tpr
= (val
& 0x0f) << 4;
350 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
352 APICState
*s
= env
->apic_state
;
353 return s
? s
->tpr
>> 4 : 0;
356 /* return -1 if no bit is set */
357 static int get_highest_priority_int(uint32_t *tab
)
360 for(i
= 7; i
>= 0; i
--) {
362 return i
* 32 + fls_bit(tab
[i
]);
368 static int apic_get_ppr(APICState
*s
)
373 isrv
= get_highest_priority_int(s
->isr
);
384 static int apic_get_arb_pri(APICState
*s
)
386 /* XXX: arbitration */
390 /* signal the CPU if an irq is pending */
391 static void apic_update_irq(APICState
*s
)
394 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
396 irrv
= get_highest_priority_int(s
->irr
);
399 ppr
= apic_get_ppr(s
);
400 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
402 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
405 void apic_reset_irq_delivered(void)
407 DPRINTF_C("%s: old coalescing %d\n", __func__
, apic_irq_delivered
);
408 apic_irq_delivered
= 0;
411 int apic_get_irq_delivered(void)
413 DPRINTF_C("%s: returning coalescing %d\n", __func__
, apic_irq_delivered
);
414 return apic_irq_delivered
;
417 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
419 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
420 DPRINTF_C("%s: coalescing %d\n", __func__
, apic_irq_delivered
);
422 set_bit(s
->irr
, vector_num
);
424 set_bit(s
->tmr
, vector_num
);
426 reset_bit(s
->tmr
, vector_num
);
430 static void apic_eoi(APICState
*s
)
433 isrv
= get_highest_priority_int(s
->isr
);
436 reset_bit(s
->isr
, isrv
);
437 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
438 set the remote IRR bit for level triggered interrupts. */
442 static int apic_find_dest(uint8_t dest
)
444 APICState
*apic
= local_apics
[dest
];
447 if (apic
&& apic
->id
== dest
)
448 return dest
; /* shortcut in case apic->id == apic->idx */
450 for (i
= 0; i
< MAX_APICS
; i
++) {
451 apic
= local_apics
[i
];
452 if (apic
&& apic
->id
== dest
)
459 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
460 uint8_t dest
, uint8_t dest_mode
)
462 APICState
*apic_iter
;
465 if (dest_mode
== 0) {
467 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
469 int idx
= apic_find_dest(dest
);
470 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
472 set_bit(deliver_bitmask
, idx
);
475 /* XXX: cluster mode */
476 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
477 for(i
= 0; i
< MAX_APICS
; i
++) {
478 apic_iter
= local_apics
[i
];
480 if (apic_iter
->dest_mode
== 0xf) {
481 if (dest
& apic_iter
->log_dest
)
482 set_bit(deliver_bitmask
, i
);
483 } else if (apic_iter
->dest_mode
== 0x0) {
484 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
485 (dest
& apic_iter
->log_dest
& 0x0f)) {
486 set_bit(deliver_bitmask
, i
);
495 void apic_init_reset(CPUState
*env
)
497 APICState
*s
= env
->apic_state
;
504 s
->spurious_vec
= 0xff;
507 memset(s
->isr
, 0, sizeof(s
->isr
));
508 memset(s
->tmr
, 0, sizeof(s
->tmr
));
509 memset(s
->irr
, 0, sizeof(s
->irr
));
510 for(i
= 0; i
< APIC_LVT_NB
; i
++)
511 s
->lvt
[i
] = 1 << 16; /* mask LVT */
513 memset(s
->icr
, 0, sizeof(s
->icr
));
516 s
->initial_count
= 0;
517 s
->initial_count_load_time
= 0;
519 s
->wait_for_sipi
= 1;
521 env
->halted
= !(s
->apicbase
& MSR_IA32_APICBASE_BSP
);
524 static void apic_startup(APICState
*s
, int vector_num
)
526 s
->sipi_vector
= vector_num
;
527 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
530 void apic_sipi(CPUState
*env
)
532 APICState
*s
= env
->apic_state
;
534 cpu_reset_interrupt(env
, CPU_INTERRUPT_SIPI
);
536 if (!s
->wait_for_sipi
)
540 cpu_x86_load_seg_cache(env
, R_CS
, s
->sipi_vector
<< 8, s
->sipi_vector
<< 12,
541 env
->segs
[R_CS
].limit
, env
->segs
[R_CS
].flags
);
543 s
->wait_for_sipi
= 0;
546 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
547 uint8_t delivery_mode
, uint8_t vector_num
,
548 uint8_t polarity
, uint8_t trigger_mode
)
550 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
551 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
552 APICState
*apic_iter
;
554 switch (dest_shorthand
) {
556 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
559 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
560 set_bit(deliver_bitmask
, s
->idx
);
563 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
566 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
567 reset_bit(deliver_bitmask
, s
->idx
);
571 switch (delivery_mode
) {
574 int trig_mode
= (s
->icr
[0] >> 15) & 1;
575 int level
= (s
->icr
[0] >> 14) & 1;
576 if (level
== 0 && trig_mode
== 1) {
577 foreach_apic(apic_iter
, deliver_bitmask
,
578 apic_iter
->arb_id
= apic_iter
->id
);
585 foreach_apic(apic_iter
, deliver_bitmask
,
586 apic_startup(apic_iter
, vector_num
) );
590 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
594 int apic_get_interrupt(CPUState
*env
)
596 APICState
*s
= env
->apic_state
;
599 /* if the APIC is installed or enabled, we let the 8259 handle the
603 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
606 /* XXX: spurious IRQ handling */
607 intno
= get_highest_priority_int(s
->irr
);
610 if (s
->tpr
&& intno
<= s
->tpr
)
611 return s
->spurious_vec
& 0xff;
612 reset_bit(s
->irr
, intno
);
613 set_bit(s
->isr
, intno
);
618 int apic_accept_pic_intr(CPUState
*env
)
620 APICState
*s
= env
->apic_state
;
626 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
628 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
629 (lvt0
& APIC_LVT_MASKED
) == 0)
635 static uint32_t apic_get_current_count(APICState
*s
)
639 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
641 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
643 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
645 if (d
>= s
->initial_count
)
648 val
= s
->initial_count
- d
;
653 static void apic_timer_update(APICState
*s
, int64_t current_time
)
655 int64_t next_time
, d
;
657 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
658 d
= (current_time
- s
->initial_count_load_time
) >>
660 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
661 if (!s
->initial_count
)
663 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
665 if (d
>= s
->initial_count
)
667 d
= (uint64_t)s
->initial_count
+ 1;
669 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
670 qemu_mod_timer(s
->timer
, next_time
);
671 s
->next_time
= next_time
;
674 qemu_del_timer(s
->timer
);
678 static void apic_timer(void *opaque
)
680 APICState
*s
= opaque
;
682 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
683 apic_timer_update(s
, s
->next_time
);
686 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
691 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
696 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
700 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
704 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
711 env
= cpu_single_env
;
716 index
= (addr
>> 4) & 0xff;
721 case 0x03: /* version */
722 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
728 val
= apic_get_arb_pri(s
);
732 val
= apic_get_ppr(s
);
738 val
= s
->log_dest
<< 24;
741 val
= s
->dest_mode
<< 28;
744 val
= s
->spurious_vec
;
747 val
= s
->isr
[index
& 7];
750 val
= s
->tmr
[index
& 7];
753 val
= s
->irr
[index
& 7];
760 val
= s
->icr
[index
& 1];
763 val
= s
->lvt
[index
- 0x32];
766 val
= s
->initial_count
;
769 val
= apic_get_current_count(s
);
772 val
= s
->divide_conf
;
775 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
779 DPRINTF("read: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
783 static void apic_send_msi(target_phys_addr_t addr
, uint32 data
)
785 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
786 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
787 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
788 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
789 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
790 /* XXX: Ignore redirection hint. */
791 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, 0, trigger_mode
);
794 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
798 int index
= (addr
>> 4) & 0xff;
799 if (addr
> 0xfff || !index
) {
800 /* MSI and MMIO APIC are at the same memory location,
801 * but actually not on the global bus: MSI is on PCI bus
802 * APIC is connected directly to the CPU.
803 * Mapping them on the global bus happens to work because
804 * MSI registers are reserved in APIC MMIO and vice versa. */
805 apic_send_msi(addr
, val
);
809 env
= cpu_single_env
;
814 DPRINTF("write: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
833 s
->log_dest
= val
>> 24;
836 s
->dest_mode
= val
>> 28;
839 s
->spurious_vec
= val
& 0x1ff;
849 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
850 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
851 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
858 int n
= index
- 0x32;
860 if (n
== APIC_LVT_TIMER
)
861 apic_timer_update(s
, qemu_get_clock(vm_clock
));
865 s
->initial_count
= val
;
866 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
867 apic_timer_update(s
, s
->initial_count_load_time
);
874 s
->divide_conf
= val
& 0xb;
875 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
876 s
->count_shift
= (v
+ 1) & 7;
880 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
885 /* This function is only used for old state version 1 and 2 */
886 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
888 APICState
*s
= opaque
;
894 /* XXX: what if the base changes? (registered memory regions) */
895 qemu_get_be32s(f
, &s
->apicbase
);
896 qemu_get_8s(f
, &s
->id
);
897 qemu_get_8s(f
, &s
->arb_id
);
898 qemu_get_8s(f
, &s
->tpr
);
899 qemu_get_be32s(f
, &s
->spurious_vec
);
900 qemu_get_8s(f
, &s
->log_dest
);
901 qemu_get_8s(f
, &s
->dest_mode
);
902 for (i
= 0; i
< 8; i
++) {
903 qemu_get_be32s(f
, &s
->isr
[i
]);
904 qemu_get_be32s(f
, &s
->tmr
[i
]);
905 qemu_get_be32s(f
, &s
->irr
[i
]);
907 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
908 qemu_get_be32s(f
, &s
->lvt
[i
]);
910 qemu_get_be32s(f
, &s
->esr
);
911 qemu_get_be32s(f
, &s
->icr
[0]);
912 qemu_get_be32s(f
, &s
->icr
[1]);
913 qemu_get_be32s(f
, &s
->divide_conf
);
914 s
->count_shift
=qemu_get_be32(f
);
915 qemu_get_be32s(f
, &s
->initial_count
);
916 s
->initial_count_load_time
=qemu_get_be64(f
);
917 s
->next_time
=qemu_get_be64(f
);
920 qemu_get_timer(f
, s
->timer
);
924 static const VMStateDescription vmstate_apic
= {
927 .minimum_version_id
= 3,
928 .minimum_version_id_old
= 1,
929 .load_state_old
= apic_load_old
,
930 .fields
= (VMStateField
[]) {
931 VMSTATE_UINT32(apicbase
, APICState
),
932 VMSTATE_UINT8(id
, APICState
),
933 VMSTATE_UINT8(arb_id
, APICState
),
934 VMSTATE_UINT8(tpr
, APICState
),
935 VMSTATE_UINT32(spurious_vec
, APICState
),
936 VMSTATE_UINT8(log_dest
, APICState
),
937 VMSTATE_UINT8(dest_mode
, APICState
),
938 VMSTATE_UINT32_ARRAY(isr
, APICState
, 8),
939 VMSTATE_UINT32_ARRAY(tmr
, APICState
, 8),
940 VMSTATE_UINT32_ARRAY(irr
, APICState
, 8),
941 VMSTATE_UINT32_ARRAY(lvt
, APICState
, APIC_LVT_NB
),
942 VMSTATE_UINT32(esr
, APICState
),
943 VMSTATE_UINT32_ARRAY(icr
, APICState
, 2),
944 VMSTATE_UINT32(divide_conf
, APICState
),
945 VMSTATE_INT32(count_shift
, APICState
),
946 VMSTATE_UINT32(initial_count
, APICState
),
947 VMSTATE_INT64(initial_count_load_time
, APICState
),
948 VMSTATE_INT64(next_time
, APICState
),
949 VMSTATE_TIMER(timer
, APICState
),
950 VMSTATE_END_OF_LIST()
954 static void apic_reset(void *opaque
)
956 APICState
*s
= opaque
;
959 bsp
= cpu_is_bsp(s
->cpu_env
);
960 s
->apicbase
= 0xfee00000 |
961 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
963 cpu_reset(s
->cpu_env
);
964 apic_init_reset(s
->cpu_env
);
968 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
969 * time typically by BIOS, so PIC interrupt can be delivered to the
970 * processor when local APIC is enabled.
972 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
976 static CPUReadMemoryFunc
* const apic_mem_read
[3] = {
982 static CPUWriteMemoryFunc
* const apic_mem_write
[3] = {
988 int apic_init(CPUState
*env
)
992 if (last_apic_idx
>= MAX_APICS
)
994 s
= qemu_mallocz(sizeof(APICState
));
996 s
->idx
= last_apic_idx
++;
997 s
->id
= env
->cpuid_apic_id
;
1002 /* XXX: mapping more APICs at the same memory location */
1003 if (apic_io_memory
== 0) {
1004 /* NOTE: the APIC is directly connected to the CPU - it is not
1005 on the global memory bus. */
1006 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
1007 apic_mem_write
, NULL
);
1008 /* XXX: what if the base changes? */
1009 cpu_register_physical_memory(MSI_ADDR_BASE
, MSI_ADDR_SIZE
,
1012 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
1014 vmstate_register(s
->idx
, &vmstate_apic
, s
);
1015 qemu_register_reset(apic_reset
, s
);
1017 local_apics
[s
->idx
] = s
;