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/>
22 #include "qemu-timer.h"
23 #include "host-utils.h"
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 ESR_ILLEGAL_ADDRESS (1 << 7)
61 #define APIC_SV_DIRECTED_IO (1<<12)
62 #define APIC_SV_ENABLE (1<<8)
65 #define MAX_APIC_WORDS 8
67 /* Intel APIC constants: from include/asm/msidef.h */
68 #define MSI_DATA_VECTOR_SHIFT 0
69 #define MSI_DATA_VECTOR_MASK 0x000000ff
70 #define MSI_DATA_DELIVERY_MODE_SHIFT 8
71 #define MSI_DATA_TRIGGER_SHIFT 15
72 #define MSI_DATA_LEVEL_SHIFT 14
73 #define MSI_ADDR_DEST_MODE_SHIFT 2
74 #define MSI_ADDR_DEST_ID_SHIFT 12
75 #define MSI_ADDR_DEST_ID_MASK 0x00ffff0
77 #define MSI_ADDR_SIZE 0x100000
79 typedef struct APICState APICState
;
88 uint32_t spurious_vec
;
91 uint32_t isr
[8]; /* in service register */
92 uint32_t tmr
[8]; /* trigger mode register */
93 uint32_t irr
[8]; /* interrupt request register */
94 uint32_t lvt
[APIC_LVT_NB
];
95 uint32_t esr
; /* error register */
100 uint32_t initial_count
;
101 int64_t initial_count_load_time
, next_time
;
108 static APICState
*local_apics
[MAX_APICS
+ 1];
109 static int apic_irq_delivered
;
111 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
);
112 static void apic_update_irq(APICState
*s
);
113 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
114 uint8_t dest
, uint8_t dest_mode
);
116 /* Find first bit starting from msb */
117 static int fls_bit(uint32_t value
)
119 return 31 - clz32(value
);
122 /* Find first bit starting from lsb */
123 static int ffs_bit(uint32_t value
)
128 static inline void set_bit(uint32_t *tab
, int index
)
132 mask
= 1 << (index
& 0x1f);
136 static inline void reset_bit(uint32_t *tab
, int index
)
140 mask
= 1 << (index
& 0x1f);
144 static inline int get_bit(uint32_t *tab
, int index
)
148 mask
= 1 << (index
& 0x1f);
149 return !!(tab
[i
] & mask
);
152 static void apic_local_deliver(APICState
*s
, int vector
)
154 uint32_t lvt
= s
->lvt
[vector
];
157 trace_apic_local_deliver(vector
, (lvt
>> 8) & 7);
159 if (lvt
& APIC_LVT_MASKED
)
162 switch ((lvt
>> 8) & 7) {
164 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SMI
);
168 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_NMI
);
172 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
176 trigger_mode
= APIC_TRIGGER_EDGE
;
177 if ((vector
== APIC_LVT_LINT0
|| vector
== APIC_LVT_LINT1
) &&
178 (lvt
& APIC_LVT_LEVEL_TRIGGER
))
179 trigger_mode
= APIC_TRIGGER_LEVEL
;
180 apic_set_irq(s
, lvt
& 0xff, trigger_mode
);
184 void apic_deliver_pic_intr(DeviceState
*d
, int level
)
186 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
189 apic_local_deliver(s
, APIC_LVT_LINT0
);
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(s
->cpu_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 trace_apic_deliver_irq(dest
, dest_mode
, delivery_mode
, vector_num
,
290 polarity
, trigger_mode
);
292 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
293 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
297 void cpu_set_apic_base(DeviceState
*d
, uint64_t val
)
299 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
301 trace_cpu_set_apic_base(val
);
305 s
->apicbase
= (val
& 0xfffff000) |
306 (s
->apicbase
& (MSR_IA32_APICBASE_BSP
| MSR_IA32_APICBASE_ENABLE
));
307 /* if disabled, cannot be enabled again */
308 if (!(val
& MSR_IA32_APICBASE_ENABLE
)) {
309 s
->apicbase
&= ~MSR_IA32_APICBASE_ENABLE
;
310 cpu_clear_apic_feature(s
->cpu_env
);
311 s
->spurious_vec
&= ~APIC_SV_ENABLE
;
315 uint64_t cpu_get_apic_base(DeviceState
*d
)
317 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
319 trace_cpu_get_apic_base(s
? (uint64_t)s
->apicbase
: 0);
321 return s
? s
->apicbase
: 0;
324 void cpu_set_apic_tpr(DeviceState
*d
, uint8_t val
)
326 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
330 s
->tpr
= (val
& 0x0f) << 4;
334 uint8_t cpu_get_apic_tpr(DeviceState
*d
)
336 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
338 return s
? s
->tpr
>> 4 : 0;
341 /* return -1 if no bit is set */
342 static int get_highest_priority_int(uint32_t *tab
)
345 for(i
= 7; i
>= 0; i
--) {
347 return i
* 32 + fls_bit(tab
[i
]);
353 static int apic_get_ppr(APICState
*s
)
358 isrv
= get_highest_priority_int(s
->isr
);
369 static int apic_get_arb_pri(APICState
*s
)
371 /* XXX: arbitration */
377 * <0 - low prio interrupt,
379 * >0 - interrupt number
381 static int apic_irq_pending(APICState
*s
)
384 irrv
= get_highest_priority_int(s
->irr
);
388 ppr
= apic_get_ppr(s
);
389 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0)) {
396 /* signal the CPU if an irq is pending */
397 static void apic_update_irq(APICState
*s
)
399 if (!(s
->spurious_vec
& APIC_SV_ENABLE
)) {
402 if (apic_irq_pending(s
) > 0) {
403 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
407 void apic_reset_irq_delivered(void)
409 trace_apic_reset_irq_delivered(apic_irq_delivered
);
411 apic_irq_delivered
= 0;
414 int apic_get_irq_delivered(void)
416 trace_apic_get_irq_delivered(apic_irq_delivered
);
418 return apic_irq_delivered
;
421 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
423 apic_irq_delivered
+= !get_bit(s
->irr
, vector_num
);
425 trace_apic_set_irq(apic_irq_delivered
);
427 set_bit(s
->irr
, vector_num
);
429 set_bit(s
->tmr
, vector_num
);
431 reset_bit(s
->tmr
, vector_num
);
435 static void apic_eoi(APICState
*s
)
438 isrv
= get_highest_priority_int(s
->isr
);
441 reset_bit(s
->isr
, isrv
);
442 if (!(s
->spurious_vec
& APIC_SV_DIRECTED_IO
) && get_bit(s
->tmr
, isrv
)) {
443 ioapic_eoi_broadcast(isrv
);
448 static int apic_find_dest(uint8_t dest
)
450 APICState
*apic
= local_apics
[dest
];
453 if (apic
&& apic
->id
== dest
)
454 return dest
; /* shortcut in case apic->id == apic->idx */
456 for (i
= 0; i
< MAX_APICS
; i
++) {
457 apic
= local_apics
[i
];
458 if (apic
&& apic
->id
== dest
)
467 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
468 uint8_t dest
, uint8_t dest_mode
)
470 APICState
*apic_iter
;
473 if (dest_mode
== 0) {
475 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
477 int idx
= apic_find_dest(dest
);
478 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
480 set_bit(deliver_bitmask
, idx
);
483 /* XXX: cluster mode */
484 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
485 for(i
= 0; i
< MAX_APICS
; i
++) {
486 apic_iter
= local_apics
[i
];
488 if (apic_iter
->dest_mode
== 0xf) {
489 if (dest
& apic_iter
->log_dest
)
490 set_bit(deliver_bitmask
, i
);
491 } else if (apic_iter
->dest_mode
== 0x0) {
492 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
493 (dest
& apic_iter
->log_dest
& 0x0f)) {
494 set_bit(deliver_bitmask
, i
);
504 void apic_init_reset(DeviceState
*d
)
506 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
513 s
->spurious_vec
= 0xff;
516 memset(s
->isr
, 0, sizeof(s
->isr
));
517 memset(s
->tmr
, 0, sizeof(s
->tmr
));
518 memset(s
->irr
, 0, sizeof(s
->irr
));
519 for(i
= 0; i
< APIC_LVT_NB
; i
++)
520 s
->lvt
[i
] = 1 << 16; /* mask LVT */
522 memset(s
->icr
, 0, sizeof(s
->icr
));
525 s
->initial_count
= 0;
526 s
->initial_count_load_time
= 0;
528 s
->wait_for_sipi
= 1;
531 static void apic_startup(APICState
*s
, int vector_num
)
533 s
->sipi_vector
= vector_num
;
534 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
537 void apic_sipi(DeviceState
*d
)
539 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
541 cpu_reset_interrupt(s
->cpu_env
, CPU_INTERRUPT_SIPI
);
543 if (!s
->wait_for_sipi
)
545 cpu_x86_load_seg_cache_sipi(s
->cpu_env
, s
->sipi_vector
);
546 s
->wait_for_sipi
= 0;
549 static void apic_deliver(DeviceState
*d
, uint8_t dest
, uint8_t dest_mode
,
550 uint8_t delivery_mode
, uint8_t vector_num
,
551 uint8_t polarity
, uint8_t trigger_mode
)
553 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
554 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
555 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
556 APICState
*apic_iter
;
558 switch (dest_shorthand
) {
560 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
563 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
564 set_bit(deliver_bitmask
, s
->idx
);
567 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
570 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
571 reset_bit(deliver_bitmask
, s
->idx
);
575 switch (delivery_mode
) {
578 int trig_mode
= (s
->icr
[0] >> 15) & 1;
579 int level
= (s
->icr
[0] >> 14) & 1;
580 if (level
== 0 && trig_mode
== 1) {
581 foreach_apic(apic_iter
, deliver_bitmask
,
582 apic_iter
->arb_id
= apic_iter
->id
);
589 foreach_apic(apic_iter
, deliver_bitmask
,
590 apic_startup(apic_iter
, vector_num
) );
594 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
598 int apic_get_interrupt(DeviceState
*d
)
600 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
603 /* if the APIC is installed or enabled, we let the 8259 handle the
607 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
610 intno
= apic_irq_pending(s
);
614 } else if (intno
< 0) {
615 return s
->spurious_vec
& 0xff;
617 reset_bit(s
->irr
, intno
);
618 set_bit(s
->isr
, intno
);
623 int apic_accept_pic_intr(DeviceState
*d
)
625 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
631 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
633 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
634 (lvt0
& APIC_LVT_MASKED
) == 0)
640 static uint32_t apic_get_current_count(APICState
*s
)
644 d
= (qemu_get_clock_ns(vm_clock
) - s
->initial_count_load_time
) >>
646 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
648 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
650 if (d
>= s
->initial_count
)
653 val
= s
->initial_count
- d
;
658 static void apic_timer_update(APICState
*s
, int64_t current_time
)
660 int64_t next_time
, d
;
662 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
663 d
= (current_time
- s
->initial_count_load_time
) >>
665 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
666 if (!s
->initial_count
)
668 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
670 if (d
>= s
->initial_count
)
672 d
= (uint64_t)s
->initial_count
+ 1;
674 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
675 qemu_mod_timer(s
->timer
, next_time
);
676 s
->next_time
= next_time
;
679 qemu_del_timer(s
->timer
);
683 static void apic_timer(void *opaque
)
685 APICState
*s
= opaque
;
687 apic_local_deliver(s
, APIC_LVT_TIMER
);
688 apic_timer_update(s
, s
->next_time
);
691 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
696 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
701 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
705 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
709 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
716 d
= cpu_get_current_apic();
720 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
722 index
= (addr
>> 4) & 0xff;
727 case 0x03: /* version */
728 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
734 val
= apic_get_arb_pri(s
);
738 val
= apic_get_ppr(s
);
744 val
= s
->log_dest
<< 24;
747 val
= s
->dest_mode
<< 28;
750 val
= s
->spurious_vec
;
753 val
= s
->isr
[index
& 7];
756 val
= s
->tmr
[index
& 7];
759 val
= s
->irr
[index
& 7];
766 val
= s
->icr
[index
& 1];
769 val
= s
->lvt
[index
- 0x32];
772 val
= s
->initial_count
;
775 val
= apic_get_current_count(s
);
778 val
= s
->divide_conf
;
781 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
785 trace_apic_mem_readl(addr
, val
);
789 static void apic_send_msi(target_phys_addr_t addr
, uint32_t data
)
791 uint8_t dest
= (addr
& MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
792 uint8_t vector
= (data
& MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
793 uint8_t dest_mode
= (addr
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
794 uint8_t trigger_mode
= (data
>> MSI_DATA_TRIGGER_SHIFT
) & 0x1;
795 uint8_t delivery
= (data
>> MSI_DATA_DELIVERY_MODE_SHIFT
) & 0x7;
796 /* XXX: Ignore redirection hint. */
797 apic_deliver_irq(dest
, dest_mode
, delivery
, vector
, 0, trigger_mode
);
800 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
804 int index
= (addr
>> 4) & 0xff;
805 if (addr
> 0xfff || !index
) {
806 /* MSI and MMIO APIC are at the same memory location,
807 * but actually not on the global bus: MSI is on PCI bus
808 * APIC is connected directly to the CPU.
809 * Mapping them on the global bus happens to work because
810 * MSI registers are reserved in APIC MMIO and vice versa. */
811 apic_send_msi(addr
, val
);
815 d
= cpu_get_current_apic();
819 s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
821 trace_apic_mem_writel(addr
, val
);
840 s
->log_dest
= val
>> 24;
843 s
->dest_mode
= val
>> 28;
846 s
->spurious_vec
= val
& 0x1ff;
856 apic_deliver(d
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
857 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
858 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
865 int n
= index
- 0x32;
867 if (n
== APIC_LVT_TIMER
)
868 apic_timer_update(s
, qemu_get_clock_ns(vm_clock
));
872 s
->initial_count
= val
;
873 s
->initial_count_load_time
= qemu_get_clock_ns(vm_clock
);
874 apic_timer_update(s
, s
->initial_count_load_time
);
881 s
->divide_conf
= val
& 0xb;
882 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
883 s
->count_shift
= (v
+ 1) & 7;
887 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
892 /* This function is only used for old state version 1 and 2 */
893 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
895 APICState
*s
= opaque
;
901 /* XXX: what if the base changes? (registered memory regions) */
902 qemu_get_be32s(f
, &s
->apicbase
);
903 qemu_get_8s(f
, &s
->id
);
904 qemu_get_8s(f
, &s
->arb_id
);
905 qemu_get_8s(f
, &s
->tpr
);
906 qemu_get_be32s(f
, &s
->spurious_vec
);
907 qemu_get_8s(f
, &s
->log_dest
);
908 qemu_get_8s(f
, &s
->dest_mode
);
909 for (i
= 0; i
< 8; i
++) {
910 qemu_get_be32s(f
, &s
->isr
[i
]);
911 qemu_get_be32s(f
, &s
->tmr
[i
]);
912 qemu_get_be32s(f
, &s
->irr
[i
]);
914 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
915 qemu_get_be32s(f
, &s
->lvt
[i
]);
917 qemu_get_be32s(f
, &s
->esr
);
918 qemu_get_be32s(f
, &s
->icr
[0]);
919 qemu_get_be32s(f
, &s
->icr
[1]);
920 qemu_get_be32s(f
, &s
->divide_conf
);
921 s
->count_shift
=qemu_get_be32(f
);
922 qemu_get_be32s(f
, &s
->initial_count
);
923 s
->initial_count_load_time
=qemu_get_be64(f
);
924 s
->next_time
=qemu_get_be64(f
);
927 qemu_get_timer(f
, s
->timer
);
931 static const VMStateDescription vmstate_apic
= {
934 .minimum_version_id
= 3,
935 .minimum_version_id_old
= 1,
936 .load_state_old
= apic_load_old
,
937 .fields
= (VMStateField
[]) {
938 VMSTATE_UINT32(apicbase
, APICState
),
939 VMSTATE_UINT8(id
, APICState
),
940 VMSTATE_UINT8(arb_id
, APICState
),
941 VMSTATE_UINT8(tpr
, APICState
),
942 VMSTATE_UINT32(spurious_vec
, APICState
),
943 VMSTATE_UINT8(log_dest
, APICState
),
944 VMSTATE_UINT8(dest_mode
, APICState
),
945 VMSTATE_UINT32_ARRAY(isr
, APICState
, 8),
946 VMSTATE_UINT32_ARRAY(tmr
, APICState
, 8),
947 VMSTATE_UINT32_ARRAY(irr
, APICState
, 8),
948 VMSTATE_UINT32_ARRAY(lvt
, APICState
, APIC_LVT_NB
),
949 VMSTATE_UINT32(esr
, APICState
),
950 VMSTATE_UINT32_ARRAY(icr
, APICState
, 2),
951 VMSTATE_UINT32(divide_conf
, APICState
),
952 VMSTATE_INT32(count_shift
, APICState
),
953 VMSTATE_UINT32(initial_count
, APICState
),
954 VMSTATE_INT64(initial_count_load_time
, APICState
),
955 VMSTATE_INT64(next_time
, APICState
),
956 VMSTATE_TIMER(timer
, APICState
),
957 VMSTATE_END_OF_LIST()
961 static void apic_reset(DeviceState
*d
)
963 APICState
*s
= DO_UPCAST(APICState
, busdev
.qdev
, d
);
966 bsp
= cpu_is_bsp(s
->cpu_env
);
967 s
->apicbase
= 0xfee00000 |
968 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
974 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
975 * time typically by BIOS, so PIC interrupt can be delivered to the
976 * processor when local APIC is enabled.
978 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
982 static CPUReadMemoryFunc
* const apic_mem_read
[3] = {
988 static CPUWriteMemoryFunc
* const apic_mem_write
[3] = {
994 static int apic_init1(SysBusDevice
*dev
)
996 APICState
*s
= FROM_SYSBUS(APICState
, dev
);
998 static int last_apic_idx
;
1000 if (last_apic_idx
>= MAX_APICS
) {
1003 apic_io_memory
= cpu_register_io_memory(apic_mem_read
,
1004 apic_mem_write
, NULL
,
1005 DEVICE_NATIVE_ENDIAN
);
1006 sysbus_init_mmio(dev
, MSI_ADDR_SIZE
, apic_io_memory
);
1008 s
->timer
= qemu_new_timer_ns(vm_clock
, apic_timer
, s
);
1009 s
->idx
= last_apic_idx
++;
1010 local_apics
[s
->idx
] = s
;
1014 static SysBusDeviceInfo apic_info
= {
1016 .qdev
.name
= "apic",
1017 .qdev
.size
= sizeof(APICState
),
1018 .qdev
.vmsd
= &vmstate_apic
,
1019 .qdev
.reset
= apic_reset
,
1021 .qdev
.props
= (Property
[]) {
1022 DEFINE_PROP_UINT8("id", APICState
, id
, -1),
1023 DEFINE_PROP_PTR("cpu_env", APICState
, cpu_env
),
1024 DEFINE_PROP_END_OF_LIST(),
1028 static void apic_register_devices(void)
1030 sysbus_register_withprop(&apic_info
);
1033 device_init(apic_register_devices
)