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/>
21 #include "qemu-timer.h"
22 #include "host-utils.h"
26 //#define DEBUG_COALESCING
29 #define DPRINTF(fmt, ...) \
30 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
32 #define DPRINTF(fmt, ...)
35 #ifdef DEBUG_COALESCING
36 #define DPRINTF_C(fmt, ...) \
37 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
39 #define DPRINTF_C(fmt, ...)
42 /* APIC Local Vector Table */
43 #define APIC_LVT_TIMER 0
44 #define APIC_LVT_THERMAL 1
45 #define APIC_LVT_PERFORM 2
46 #define APIC_LVT_LINT0 3
47 #define APIC_LVT_LINT1 4
48 #define APIC_LVT_ERROR 5
51 /* APIC delivery modes */
52 #define APIC_DM_FIXED 0
53 #define APIC_DM_LOWPRI 1
56 #define APIC_DM_INIT 5
57 #define APIC_DM_SIPI 6
58 #define APIC_DM_EXTINT 7
60 /* APIC destination mode */
61 #define APIC_DESTMODE_FLAT 0xf
62 #define APIC_DESTMODE_CLUSTER 1
64 #define APIC_TRIGGER_EDGE 0
65 #define APIC_TRIGGER_LEVEL 1
67 #define APIC_LVT_TIMER_PERIODIC (1<<17)
68 #define APIC_LVT_MASKED (1<<16)
69 #define APIC_LVT_LEVEL_TRIGGER (1<<15)
70 #define APIC_LVT_REMOTE_IRR (1<<14)
71 #define APIC_INPUT_POLARITY (1<<13)
72 #define APIC_SEND_PENDING (1<<12)
74 #define ESR_ILLEGAL_ADDRESS (1 << 7)
76 #define APIC_SV_ENABLE (1 << 8)
79 #define MAX_APIC_WORDS 8
81 /* Intel APIC constants: from include/asm/msidef.h */
82 #define MSI_DATA_VECTOR_SHIFT 0
83 #define MSI_DATA_VECTOR_MASK 0x000000ff
84 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
85 #define MSI_DATA_TRIGGER_SHIFT 15
86 #define MSI_DATA_LEVEL_SHIFT 14
87 #define MSI_ADDR_DEST_MODE_SHIFT 2
88 #define MSI_ADDR_DEST_ID_SHIFT 12
89 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
91 #define MSI_ADDR_SIZE 0x100000
93 typedef struct APICState APICState
;
102 uint32_t spurious_vec
;
105 uint32_t isr
[8]; /* in service register */
106 uint32_t tmr
[8]; /* trigger mode register */
107 uint32_t irr
[8]; /* interrupt request register */
108 uint32_t lvt
[APIC_LVT_NB
];
109 uint32_t esr
; /* error register */
112 uint32_t divide_conf
;
114 uint32_t initial_count
;
115 int64_t initial_count_load_time
, next_time
;
122 static APICState
*local_apics
[MAX_APICS
+ 1];
123 static int apic_irq_delivered
;
125 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
126 static void apic_update_irq(APICState
*s
);
127 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
128 uint8_t dest
, uint8_t dest_mode
);
130 /* Find first bit starting from msb */
131 static int fls_bit(uint32_t value
)
133 return 31 - clz32(value
);
136 /* Find first bit starting from lsb */
137 static int ffs_bit(uint32_t value
)
142 static inline void set_bit(uint32_t *tab
, int index
)
146 mask
= 1 << (index
& 0x1f);
150 static inline void reset_bit(uint32_t *tab
, int index
)
154 mask
= 1 << (index
& 0x1f);
158 static inline int get_bit(uint32_t *tab
, int index
)
162 mask
= 1 << (index
& 0x1f);
163 return !!(tab
[i
] & mask
);
166 static void apic_local_deliver(APICState
*s
, int vector
)
168 uint32_t lvt
= s
->lvt
[vector
];
171 DPRINTF("%s: vector %d delivery mode %d\n", __func__
, vector
,
173 if (lvt
& APIC_LVT_MASKED
)
176 switch ((lvt
>> 8) & 7) {
178 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SMI
);
182 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_NMI
);
186 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
190 trigger_mode
= APIC_TRIGGER_EDGE
;
191 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
192 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
193 trigger_mode
= APIC_TRIGGER_LEVEL
;
194 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
198 void apic_deliver_pic_intr(DeviceState
*d
, int level
)
200 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
203 apic_local_deliver(s
, APIC_LVT_LINT0
);
205 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
207 switch ((lvt
>> 8) & 7) {
209 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
211 reset_bit(s
->irr
, lvt
& 0xff);
214 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
220 #define foreach_apic(apic, deliver_bitmask, code) \
222 int __i, __j, __mask;\
223 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
224 __mask = deliver_bitmask[__i];\
226 for(__j = 0; __j < 32; __j++) {\
227 if (__mask & (1 << __j)) {\
228 apic = local_apics[__i * 32 + __j];\
238 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
239 uint8_t delivery_mode
,
240 uint8_t vector_num
, uint8_t polarity
,
241 uint8_t trigger_mode
)
243 APICState
*apic_iter
;
245 switch (delivery_mode
) {
247 /* XXX: search for focus processor, arbitration */
251 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
252 if (deliver_bitmask
[i
]) {
253 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
258 apic_iter
= local_apics
[d
];
260 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
270 foreach_apic(apic_iter
, deliver_bitmask
,
271 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
275 foreach_apic(apic_iter
, deliver_bitmask
,
276 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
280 /* normal INIT IPI sent to processors */
281 foreach_apic(apic_iter
, deliver_bitmask
,
282 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_INIT
) );
286 /* handled in I/O APIC code */
293 foreach_apic(apic_iter
, deliver_bitmask
,
294 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
297 void apic_deliver_irq(uint8_t dest
, uint8_t dest_mode
,
298 uint8_t delivery_mode
, uint8_t vector_num
,
299 uint8_t polarity
, uint8_t trigger_mode
)
301 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
303 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
304 " polarity %d trigger_mode %d\n", __func__
, dest
, dest_mode
,
305 delivery_mode
, vector_num
, polarity
, trigger_mode
);
306 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
307 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
311 void cpu_set_apic_base(DeviceState
*d
, uint64_t val
)
313 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
315 DPRINTF("cpu_set_apic_base: %016" PRIx64
"\n", val
);
318 s
->apicbase
= (val
& 0xfffff000) |
319 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
320 /* if disabled, cannot be enabled again */
321 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
322 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
323 cpu_clear_apic_feature(s
->cpu_env
);
324 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
328 uint64_t cpu_get_apic_base(DeviceState
*d
)
330 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
332 DPRINTF("cpu_get_apic_base: %016" PRIx64
"\n",
333 s
? (uint64_t)s
->apicbase
: 0);
334 return s
? s
->apicbase
: 0;
337 void cpu_set_apic_tpr(DeviceState
*d
, uint8_t val
)
339 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
343 s
->tpr
= (val
& 0x0f) << 4;
347 uint8_t cpu_get_apic_tpr(DeviceState
*d
)
349 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
351 return s
? s
->tpr
>> 4 : 0;
354 /* return -1 if no bit is set */
355 static int get_highest_priority_int(uint32_t *tab
)
358 for(i
= 7; i
>= 0; i
--) {
360 return i
* 32 + fls_bit(tab
[i
]);
366 static int apic_get_ppr(APICState
*s
)
371 isrv
= get_highest_priority_int(s
->isr
);
382 static int apic_get_arb_pri(APICState
*s
)
384 /* XXX: arbitration */
388 /* signal the CPU if an irq is pending */
389 static void apic_update_irq(APICState
*s
)
392 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
394 irrv
= get_highest_priority_int(s
->irr
);
397 ppr
= apic_get_ppr(s
);
398 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
400 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
403 void apic_reset_irq_delivered(void)
405 DPRINTF_C("%s: old coalescing %d\n", __func__
, apic_irq_delivered
);
406 apic_irq_delivered
= 0;
409 int apic_get_irq_delivered(void)
411 DPRINTF_C("%s: returning coalescing %d\n", __func__
, apic_irq_delivered
);
412 return apic_irq_delivered
;
415 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
417 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
418 DPRINTF_C("%s: coalescing %d\n", __func__
, apic_irq_delivered
);
420 set_bit(s
->irr
, vector_num
);
422 set_bit(s
->tmr
, vector_num
);
424 reset_bit(s
->tmr
, vector_num
);
428 static void apic_eoi(APICState
*s
)
431 isrv
= get_highest_priority_int(s
->isr
);
434 reset_bit(s
->isr
, isrv
);
435 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
436 set the remote IRR bit for level triggered interrupts. */
440 static int apic_find_dest(uint8_t dest
)
442 APICState
*apic
= local_apics
[dest
];
445 if (apic
&& apic
->id
== dest
)
446 return dest
; /* shortcut in case apic->id == apic->idx */
448 for (i
= 0; i
< MAX_APICS
; i
++) {
449 apic
= local_apics
[i
];
450 if (apic
&& apic
->id
== dest
)
457 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
458 uint8_t dest
, uint8_t dest_mode
)
460 APICState
*apic_iter
;
463 if (dest_mode
== 0) {
465 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
467 int idx
= apic_find_dest(dest
);
468 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
470 set_bit(deliver_bitmask
, idx
);
473 /* XXX: cluster mode */
474 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
475 for(i
= 0; i
< MAX_APICS
; i
++) {
476 apic_iter
= local_apics
[i
];
478 if (apic_iter
->dest_mode
== 0xf) {
479 if (dest
& apic_iter
->log_dest
)
480 set_bit(deliver_bitmask
, i
);
481 } else if (apic_iter
->dest_mode
== 0x0) {
482 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
483 (dest
& apic_iter
->log_dest
& 0x0f)) {
484 set_bit(deliver_bitmask
, i
);
492 void apic_init_reset(DeviceState
*d
)
494 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
501 s
->spurious_vec
= 0xff;
504 memset(s
->isr
, 0, sizeof(s
->isr
));
505 memset(s
->tmr
, 0, sizeof(s
->tmr
));
506 memset(s
->irr
, 0, sizeof(s
->irr
));
507 for(i
= 0; i
< APIC_LVT_NB
; i
++)
508 s
->lvt
[i
] = 1 << 16; /* mask LVT */
510 memset(s
->icr
, 0, sizeof(s
->icr
));
513 s
->initial_count
= 0;
514 s
->initial_count_load_time
= 0;
516 s
->wait_for_sipi
= 1;
519 static void apic_startup(APICState
*s
, int vector_num
)
521 s
->sipi_vector
= vector_num
;
522 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
525 void apic_sipi(DeviceState
*d
)
527 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
529 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
531 if (!s
->wait_for_sipi
)
533 cpu_x86_load_seg_cache_sipi(s
->cpu_env
, s
->sipi_vector
);
534 s
->wait_for_sipi
= 0;
537 static void apic_deliver(DeviceState
*d
, uint8_t dest
, uint8_t dest_mode
,
538 uint8_t delivery_mode
, uint8_t vector_num
,
539 uint8_t polarity
, uint8_t trigger_mode
)
541 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
542 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
543 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
544 APICState
*apic_iter
;
546 switch (dest_shorthand
) {
548 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
551 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
552 set_bit(deliver_bitmask
, s
->idx
);
555 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
558 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
559 reset_bit(deliver_bitmask
, s
->idx
);
563 switch (delivery_mode
) {
566 int trig_mode
= (s
->icr
[0] >> 15) & 1;
567 int level
= (s
->icr
[0] >> 14) & 1;
568 if (level
== 0 && trig_mode
== 1) {
569 foreach_apic(apic_iter
, deliver_bitmask
,
570 apic_iter
->arb_id
= apic_iter
->id
);
577 foreach_apic(apic_iter
, deliver_bitmask
,
578 apic_startup(apic_iter
, vector_num
) );
582 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
586 int apic_get_interrupt(DeviceState
*d
)
588 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
591 /* if the APIC is installed or enabled, we let the 8259 handle the
595 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
598 /* XXX: spurious IRQ handling */
599 intno
= get_highest_priority_int(s
->irr
);
602 if (s
->tpr
&& intno
<= s
->tpr
)
603 return s
->spurious_vec
& 0xff;
604 reset_bit(s
->irr
, intno
);
605 set_bit(s
->isr
, intno
);
610 int apic_accept_pic_intr(DeviceState
*d
)
612 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
618 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
620 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
621 (lvt0
& APIC_LVT_MASKED
) == 0)
627 static uint32_t apic_get_current_count(APICState
*s
)
631 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
633 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
635 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
637 if (d
>= s
->initial_count
)
640 val
= s
->initial_count
- d
;
645 static void apic_timer_update(APICState
*s
, int64_t current_time
)
647 int64_t next_time
, d
;
649 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
650 d
= (current_time
- s
->initial_count_load_time
) >>
652 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
653 if (!s
->initial_count
)
655 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
657 if (d
>= s
->initial_count
)
659 d
= (uint64_t)s
->initial_count
+ 1;
661 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
662 qemu_mod_timer(s
->timer
, next_time
);
663 s
->next_time
= next_time
;
666 qemu_del_timer(s
->timer
);
670 static void apic_timer(void *opaque
)
672 APICState
*s
= opaque
;
674 apic_local_deliver(s
, APIC_LVT_TIMER
);
675 apic_timer_update(s
, s
->next_time
);
678 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
683 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
688 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
692 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
696 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
703 d
= cpu_get_current_apic();
707 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
709 index
= (addr
>> 4) & 0xff;
714 case 0x03: /* version */
715 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
721 val
= apic_get_arb_pri(s
);
725 val
= apic_get_ppr(s
);
731 val
= s
->log_dest
<< 24;
734 val
= s
->dest_mode
<< 28;
737 val
= s
->spurious_vec
;
740 val
= s
->isr
[index
& 7];
743 val
= s
->tmr
[index
& 7];
746 val
= s
->irr
[index
& 7];
753 val
= s
->icr
[index
& 1];
756 val
= s
->lvt
[index
- 0x32];
759 val
= s
->initial_count
;
762 val
= apic_get_current_count(s
);
765 val
= s
->divide_conf
;
768 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
772 DPRINTF("read: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
776 static void apic_send_msi(target_phys_addr_t addr
, uint32 data
)
778 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
779 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
780 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
781 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
782 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
783 /* XXX: Ignore redirection hint. */
784 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, 0, trigger_mode
);
787 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
791 int index
= (addr
>> 4) & 0xff;
792 if (addr
> 0xfff || !index
) {
793 /* MSI and MMIO APIC are at the same memory location,
794 * but actually not on the global bus: MSI is on PCI bus
795 * APIC is connected directly to the CPU.
796 * Mapping them on the global bus happens to work because
797 * MSI registers are reserved in APIC MMIO and vice versa. */
798 apic_send_msi(addr
, val
);
802 d
= cpu_get_current_apic();
806 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
808 DPRINTF("write: " TARGET_FMT_plx
" = %08x\n", addr
, val
);
827 s
->log_dest
= val
>> 24;
830 s
->dest_mode
= val
>> 28;
833 s
->spurious_vec
= val
& 0x1ff;
843 apic_deliver(d
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
844 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
845 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
852 int n
= index
- 0x32;
854 if (n
== APIC_LVT_TIMER
)
855 apic_timer_update(s
, qemu_get_clock(vm_clock
));
859 s
->initial_count
= val
;
860 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
861 apic_timer_update(s
, s
->initial_count_load_time
);
868 s
->divide_conf
= val
& 0xb;
869 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
870 s
->count_shift
= (v
+ 1) & 7;
874 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
879 /* This function is only used for old state version 1 and 2 */
880 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
882 APICState
*s
= opaque
;
888 /* XXX: what if the base changes? (registered memory regions) */
889 qemu_get_be32s(f
, &s
->apicbase
);
890 qemu_get_8s(f
, &s
->id
);
891 qemu_get_8s(f
, &s
->arb_id
);
892 qemu_get_8s(f
, &s
->tpr
);
893 qemu_get_be32s(f
, &s
->spurious_vec
);
894 qemu_get_8s(f
, &s
->log_dest
);
895 qemu_get_8s(f
, &s
->dest_mode
);
896 for (i
= 0; i
< 8; i
++) {
897 qemu_get_be32s(f
, &s
->isr
[i
]);
898 qemu_get_be32s(f
, &s
->tmr
[i
]);
899 qemu_get_be32s(f
, &s
->irr
[i
]);
901 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
902 qemu_get_be32s(f
, &s
->lvt
[i
]);
904 qemu_get_be32s(f
, &s
->esr
);
905 qemu_get_be32s(f
, &s
->icr
[0]);
906 qemu_get_be32s(f
, &s
->icr
[1]);
907 qemu_get_be32s(f
, &s
->divide_conf
);
908 s
->count_shift
=qemu_get_be32(f
);
909 qemu_get_be32s(f
, &s
->initial_count
);
910 s
->initial_count_load_time
=qemu_get_be64(f
);
911 s
->next_time
=qemu_get_be64(f
);
914 qemu_get_timer(f
, s
->timer
);
918 static const VMStateDescription vmstate_apic
= {
921 .minimum_version_id
= 3,
922 .minimum_version_id_old
= 1,
923 .load_state_old
= apic_load_old
,
924 .fields
= (VMStateField
[]) {
925 VMSTATE_UINT32(apicbase
, APICState
),
926 VMSTATE_UINT8(id
, APICState
),
927 VMSTATE_UINT8(arb_id
, APICState
),
928 VMSTATE_UINT8(tpr
, APICState
),
929 VMSTATE_UINT32(spurious_vec
, APICState
),
930 VMSTATE_UINT8(log_dest
, APICState
),
931 VMSTATE_UINT8(dest_mode
, APICState
),
932 VMSTATE_UINT32_ARRAY(isr
, APICState
, 8),
933 VMSTATE_UINT32_ARRAY(tmr
, APICState
, 8),
934 VMSTATE_UINT32_ARRAY(irr
, APICState
, 8),
935 VMSTATE_UINT32_ARRAY(lvt
, APICState
, APIC_LVT_NB
),
936 VMSTATE_UINT32(esr
, APICState
),
937 VMSTATE_UINT32_ARRAY(icr
, APICState
, 2),
938 VMSTATE_UINT32(divide_conf
, APICState
),
939 VMSTATE_INT32(count_shift
, APICState
),
940 VMSTATE_UINT32(initial_count
, APICState
),
941 VMSTATE_INT64(initial_count_load_time
, APICState
),
942 VMSTATE_INT64(next_time
, APICState
),
943 VMSTATE_TIMER(timer
, APICState
),
944 VMSTATE_END_OF_LIST()
948 static void apic_reset(DeviceState
*d
)
950 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
953 bsp
= cpu_is_bsp(s
->cpu_env
);
954 s
->apicbase
= 0xfee00000 |
955 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
961 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
962 * time typically by BIOS, so PIC interrupt can be delivered to the
963 * processor when local APIC is enabled.
965 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
969 static CPUReadMemoryFunc
* const apic_mem_read
[3] = {
975 static CPUWriteMemoryFunc
* const apic_mem_write
[3] = {
981 static int apic_init1(SysBusDevice
*dev
)
983 APICState
*s
= FROM_SYSBUS(APICState
, dev
);
985 static int last_apic_idx
;
987 if (last_apic_idx
>= MAX_APICS
) {
990 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
991 apic_mem_write
, NULL
);
992 sysbus_init_mmio(dev
, MSI_ADDR_SIZE
, apic_io_memory
);
994 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
995 s
->idx
= last_apic_idx
++;
996 local_apics
[s
->idx
] = s
;
1000 static SysBusDeviceInfo apic_info
= {
1002 .qdev
.name
= "apic",
1003 .qdev
.size
= sizeof(APICState
),
1004 .qdev
.vmsd
= &vmstate_apic
,
1005 .qdev
.reset
= apic_reset
,
1007 .qdev
.props
= (Property
[]) {
1008 DEFINE_PROP_UINT8("id", APICState
, id
, -1),
1009 DEFINE_PROP_PTR("cpu_env", APICState
, cpu_env
),
1010 DEFINE_PROP_END_OF_LIST(),
1014 static void apic_register_devices(void)
1016 sysbus_register_withprop(&apic_info
);
1019 device_init(apic_register_devices
)