]>
git.proxmox.com Git - qemu.git/blob - hw/apic.c
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 */
108 static int fls_bit(uint32_t value
)
110 #if defined(__GNUC__)
111 return 31 - __builtin_clz(value
);
113 unsigned int ret
= 0;
116 value
>>= 16, ret
= 16;
118 value
>>= 8, ret
+= 8;
120 value
>>= 4, ret
+= 4;
122 value
>>= 2, ret
+= 2;
123 return ret
+ (value
>> 1);
127 /* Find first bit starting from lsb */
128 static int ffs_bit(uint32_t value
)
130 #if defined(__GNUC__)
131 return __builtin_ffs(value
) - 1;
133 unsigned int ret
= 0;
137 if (!(value
& 0xffff))
138 value
>>= 16, ret
= 16;
140 value
>>= 8, ret
+= 8;
142 value
>>= 4, ret
+= 4;
144 value
>>= 2, ret
+= 2;
151 static inline void set_bit(uint32_t *tab
, int index
)
155 mask
= 1 << (index
& 0x1f);
159 static inline void reset_bit(uint32_t *tab
, int index
)
163 mask
= 1 << (index
& 0x1f);
167 static void apic_local_deliver(CPUState
*env
, int vector
)
169 APICState
*s
= env
->apic_state
;
170 uint32_t lvt
= s
->lvt
[vector
];
173 if (lvt
& APIC_LVT_MASKED
)
176 switch ((lvt
>> 8) & 7) {
178 cpu_interrupt(env
, CPU_INTERRUPT_SMI
);
182 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
186 cpu_interrupt(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(CPUState
*env
, int level
)
201 apic_local_deliver(env
, APIC_LVT_LINT0
);
203 APICState
*s
= env
->apic_state
;
204 uint32_t lvt
= s
->lvt
[APIC_LVT_LINT0
];
206 switch ((lvt
>> 8) & 7) {
208 if (!(lvt
& APIC_LVT_LEVEL_TRIGGER
))
210 reset_bit(s
->irr
, lvt
& 0xff);
213 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
219 #define foreach_apic(apic, deliver_bitmask, code) \
221 int __i, __j, __mask;\
222 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
223 __mask = deliver_bitmask[__i];\
225 for(__j = 0; __j < 32; __j++) {\
226 if (__mask & (1 << __j)) {\
227 apic = local_apics[__i * 32 + __j];\
237 static void apic_bus_deliver(const uint32_t *deliver_bitmask
,
238 uint8_t delivery_mode
,
239 uint8_t vector_num
, uint8_t polarity
,
240 uint8_t trigger_mode
)
242 APICState
*apic_iter
;
244 switch (delivery_mode
) {
246 /* XXX: search for focus processor, arbitration */
250 for(i
= 0; i
< MAX_APIC_WORDS
; i
++) {
251 if (deliver_bitmask
[i
]) {
252 d
= i
* 32 + ffs_bit(deliver_bitmask
[i
]);
257 apic_iter
= local_apics
[d
];
259 apic_set_irq(apic_iter
, vector_num
, trigger_mode
);
269 foreach_apic(apic_iter
, deliver_bitmask
,
270 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_SMI
) );
274 foreach_apic(apic_iter
, deliver_bitmask
,
275 cpu_interrupt(apic_iter
->cpu_env
, CPU_INTERRUPT_NMI
) );
279 /* normal INIT IPI sent to processors */
280 foreach_apic(apic_iter
, deliver_bitmask
,
281 apic_init_ipi(apic_iter
) );
285 /* handled in I/O APIC code */
292 foreach_apic(apic_iter
, deliver_bitmask
,
293 apic_set_irq(apic_iter
, vector_num
, trigger_mode
) );
296 void cpu_set_apic_base(CPUState
*env
, uint64_t val
)
298 APICState
*s
= env
->apic_state
;
300 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", (uint64_t)s
->apicbase
);
321 void cpu_set_apic_tpr(CPUX86State
*env
, uint8_t val
)
323 APICState
*s
= env
->apic_state
;
324 s
->tpr
= (val
& 0x0f) << 4;
328 uint8_t cpu_get_apic_tpr(CPUX86State
*env
)
330 APICState
*s
= env
->apic_state
;
334 /* return -1 if no bit is set */
335 static int get_highest_priority_int(uint32_t *tab
)
338 for(i
= 7; i
>= 0; i
--) {
340 return i
* 32 + fls_bit(tab
[i
]);
346 static int apic_get_ppr(APICState
*s
)
351 isrv
= get_highest_priority_int(s
->isr
);
362 static int apic_get_arb_pri(APICState
*s
)
364 /* XXX: arbitration */
368 /* signal the CPU if an irq is pending */
369 static void apic_update_irq(APICState
*s
)
372 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
374 irrv
= get_highest_priority_int(s
->irr
);
377 ppr
= apic_get_ppr(s
);
378 if (ppr
&& (irrv
& 0xf0) <= (ppr
& 0xf0))
380 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_HARD
);
383 static void apic_set_irq(APICState
*s
, int vector_num
, int trigger_mode
)
385 set_bit(s
->irr
, vector_num
);
387 set_bit(s
->tmr
, vector_num
);
389 reset_bit(s
->tmr
, vector_num
);
393 static void apic_eoi(APICState
*s
)
396 isrv
= get_highest_priority_int(s
->isr
);
399 reset_bit(s
->isr
, isrv
);
400 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
401 set the remote IRR bit for level triggered interrupts. */
405 static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask
,
406 uint8_t dest
, uint8_t dest_mode
)
408 APICState
*apic_iter
;
411 if (dest_mode
== 0) {
413 memset(deliver_bitmask
, 0xff, MAX_APIC_WORDS
* sizeof(uint32_t));
415 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
416 set_bit(deliver_bitmask
, dest
);
419 /* XXX: cluster mode */
420 memset(deliver_bitmask
, 0x00, MAX_APIC_WORDS
* sizeof(uint32_t));
421 for(i
= 0; i
< MAX_APICS
; i
++) {
422 apic_iter
= local_apics
[i
];
424 if (apic_iter
->dest_mode
== 0xf) {
425 if (dest
& apic_iter
->log_dest
)
426 set_bit(deliver_bitmask
, i
);
427 } else if (apic_iter
->dest_mode
== 0x0) {
428 if ((dest
& 0xf0) == (apic_iter
->log_dest
& 0xf0) &&
429 (dest
& apic_iter
->log_dest
& 0x0f)) {
430 set_bit(deliver_bitmask
, i
);
439 static void apic_init_ipi(APICState
*s
)
444 s
->spurious_vec
= 0xff;
447 memset(s
->isr
, 0, sizeof(s
->isr
));
448 memset(s
->tmr
, 0, sizeof(s
->tmr
));
449 memset(s
->irr
, 0, sizeof(s
->irr
));
450 for(i
= 0; i
< APIC_LVT_NB
; i
++)
451 s
->lvt
[i
] = 1 << 16; /* mask LVT */
453 memset(s
->icr
, 0, sizeof(s
->icr
));
456 s
->initial_count
= 0;
457 s
->initial_count_load_time
= 0;
460 cpu_reset(s
->cpu_env
);
462 if (!(s
->apicbase
& MSR_IA32_APICBASE_BSP
))
463 s
->cpu_env
->halted
= 1;
466 /* send a SIPI message to the CPU to start it */
467 static void apic_startup(APICState
*s
, int vector_num
)
469 CPUState
*env
= s
->cpu_env
;
473 cpu_x86_load_seg_cache(env
, R_CS
, vector_num
<< 8, vector_num
<< 12,
478 static void apic_deliver(APICState
*s
, uint8_t dest
, uint8_t dest_mode
,
479 uint8_t delivery_mode
, uint8_t vector_num
,
480 uint8_t polarity
, uint8_t trigger_mode
)
482 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
483 int dest_shorthand
= (s
->icr
[0] >> 18) & 3;
484 APICState
*apic_iter
;
486 switch (dest_shorthand
) {
488 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
491 memset(deliver_bitmask
, 0x00, sizeof(deliver_bitmask
));
492 set_bit(deliver_bitmask
, s
->id
);
495 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
498 memset(deliver_bitmask
, 0xff, sizeof(deliver_bitmask
));
499 reset_bit(deliver_bitmask
, s
->id
);
503 switch (delivery_mode
) {
506 int trig_mode
= (s
->icr
[0] >> 15) & 1;
507 int level
= (s
->icr
[0] >> 14) & 1;
508 if (level
== 0 && trig_mode
== 1) {
509 foreach_apic(apic_iter
, deliver_bitmask
,
510 apic_iter
->arb_id
= apic_iter
->id
);
517 foreach_apic(apic_iter
, deliver_bitmask
,
518 apic_startup(apic_iter
, vector_num
) );
522 apic_bus_deliver(deliver_bitmask
, delivery_mode
, vector_num
, polarity
,
526 int apic_get_interrupt(CPUState
*env
)
528 APICState
*s
= env
->apic_state
;
531 /* if the APIC is installed or enabled, we let the 8259 handle the
535 if (!(s
->spurious_vec
& APIC_SV_ENABLE
))
538 /* XXX: spurious IRQ handling */
539 intno
= get_highest_priority_int(s
->irr
);
542 if (s
->tpr
&& intno
<= s
->tpr
)
543 return s
->spurious_vec
& 0xff;
544 reset_bit(s
->irr
, intno
);
545 set_bit(s
->isr
, intno
);
550 int apic_accept_pic_intr(CPUState
*env
)
552 APICState
*s
= env
->apic_state
;
558 lvt0
= s
->lvt
[APIC_LVT_LINT0
];
560 if ((s
->apicbase
& MSR_IA32_APICBASE_ENABLE
) == 0 ||
561 (lvt0
& APIC_LVT_MASKED
) == 0)
567 static uint32_t apic_get_current_count(APICState
*s
)
571 d
= (qemu_get_clock(vm_clock
) - s
->initial_count_load_time
) >>
573 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
575 val
= s
->initial_count
- (d
% ((uint64_t)s
->initial_count
+ 1));
577 if (d
>= s
->initial_count
)
580 val
= s
->initial_count
- d
;
585 static void apic_timer_update(APICState
*s
, int64_t current_time
)
587 int64_t next_time
, d
;
589 if (!(s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
)) {
590 d
= (current_time
- s
->initial_count_load_time
) >>
592 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
593 if (!s
->initial_count
)
595 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) * ((uint64_t)s
->initial_count
+ 1);
597 if (d
>= s
->initial_count
)
599 d
= (uint64_t)s
->initial_count
+ 1;
601 next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
602 qemu_mod_timer(s
->timer
, next_time
);
603 s
->next_time
= next_time
;
606 qemu_del_timer(s
->timer
);
610 static void apic_timer(void *opaque
)
612 APICState
*s
= opaque
;
614 apic_local_deliver(s
->cpu_env
, APIC_LVT_TIMER
);
615 apic_timer_update(s
, s
->next_time
);
618 static uint32_t apic_mem_readb(void *opaque
, target_phys_addr_t addr
)
623 static uint32_t apic_mem_readw(void *opaque
, target_phys_addr_t addr
)
628 static void apic_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
632 static void apic_mem_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
636 static uint32_t apic_mem_readl(void *opaque
, target_phys_addr_t addr
)
643 env
= cpu_single_env
;
648 index
= (addr
>> 4) & 0xff;
653 case 0x03: /* version */
654 val
= 0x11 | ((APIC_LVT_NB
- 1) << 16); /* version 0x11 */
660 val
= apic_get_arb_pri(s
);
664 val
= apic_get_ppr(s
);
670 val
= s
->log_dest
<< 24;
673 val
= s
->dest_mode
<< 28;
676 val
= s
->spurious_vec
;
679 val
= s
->isr
[index
& 7];
682 val
= s
->tmr
[index
& 7];
685 val
= s
->irr
[index
& 7];
692 val
= s
->icr
[index
& 1];
695 val
= s
->lvt
[index
- 0x32];
698 val
= s
->initial_count
;
701 val
= apic_get_current_count(s
);
704 val
= s
->divide_conf
;
707 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
712 printf("APIC read: %08x = %08x\n", (uint32_t)addr
, val
);
717 static void apic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
723 env
= cpu_single_env
;
729 printf("APIC write: %08x = %08x\n", (uint32_t)addr
, val
);
732 index
= (addr
>> 4) & 0xff;
750 s
->log_dest
= val
>> 24;
753 s
->dest_mode
= val
>> 28;
756 s
->spurious_vec
= val
& 0x1ff;
766 apic_deliver(s
, (s
->icr
[1] >> 24) & 0xff, (s
->icr
[0] >> 11) & 1,
767 (s
->icr
[0] >> 8) & 7, (s
->icr
[0] & 0xff),
768 (s
->icr
[0] >> 14) & 1, (s
->icr
[0] >> 15) & 1);
775 int n
= index
- 0x32;
777 if (n
== APIC_LVT_TIMER
)
778 apic_timer_update(s
, qemu_get_clock(vm_clock
));
782 s
->initial_count
= val
;
783 s
->initial_count_load_time
= qemu_get_clock(vm_clock
);
784 apic_timer_update(s
, s
->initial_count_load_time
);
791 s
->divide_conf
= val
& 0xb;
792 v
= (s
->divide_conf
& 3) | ((s
->divide_conf
>> 1) & 4);
793 s
->count_shift
= (v
+ 1) & 7;
797 s
->esr
|= ESR_ILLEGAL_ADDRESS
;
802 static void apic_save(QEMUFile
*f
, void *opaque
)
804 APICState
*s
= opaque
;
807 qemu_put_be32s(f
, &s
->apicbase
);
808 qemu_put_8s(f
, &s
->id
);
809 qemu_put_8s(f
, &s
->arb_id
);
810 qemu_put_8s(f
, &s
->tpr
);
811 qemu_put_be32s(f
, &s
->spurious_vec
);
812 qemu_put_8s(f
, &s
->log_dest
);
813 qemu_put_8s(f
, &s
->dest_mode
);
814 for (i
= 0; i
< 8; i
++) {
815 qemu_put_be32s(f
, &s
->isr
[i
]);
816 qemu_put_be32s(f
, &s
->tmr
[i
]);
817 qemu_put_be32s(f
, &s
->irr
[i
]);
819 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
820 qemu_put_be32s(f
, &s
->lvt
[i
]);
822 qemu_put_be32s(f
, &s
->esr
);
823 qemu_put_be32s(f
, &s
->icr
[0]);
824 qemu_put_be32s(f
, &s
->icr
[1]);
825 qemu_put_be32s(f
, &s
->divide_conf
);
826 qemu_put_be32(f
, s
->count_shift
);
827 qemu_put_be32s(f
, &s
->initial_count
);
828 qemu_put_be64(f
, s
->initial_count_load_time
);
829 qemu_put_be64(f
, s
->next_time
);
831 qemu_put_timer(f
, s
->timer
);
834 static int apic_load(QEMUFile
*f
, void *opaque
, int version_id
)
836 APICState
*s
= opaque
;
842 /* XXX: what if the base changes? (registered memory regions) */
843 qemu_get_be32s(f
, &s
->apicbase
);
844 qemu_get_8s(f
, &s
->id
);
845 qemu_get_8s(f
, &s
->arb_id
);
846 qemu_get_8s(f
, &s
->tpr
);
847 qemu_get_be32s(f
, &s
->spurious_vec
);
848 qemu_get_8s(f
, &s
->log_dest
);
849 qemu_get_8s(f
, &s
->dest_mode
);
850 for (i
= 0; i
< 8; i
++) {
851 qemu_get_be32s(f
, &s
->isr
[i
]);
852 qemu_get_be32s(f
, &s
->tmr
[i
]);
853 qemu_get_be32s(f
, &s
->irr
[i
]);
855 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
856 qemu_get_be32s(f
, &s
->lvt
[i
]);
858 qemu_get_be32s(f
, &s
->esr
);
859 qemu_get_be32s(f
, &s
->icr
[0]);
860 qemu_get_be32s(f
, &s
->icr
[1]);
861 qemu_get_be32s(f
, &s
->divide_conf
);
862 s
->count_shift
=qemu_get_be32(f
);
863 qemu_get_be32s(f
, &s
->initial_count
);
864 s
->initial_count_load_time
=qemu_get_be64(f
);
865 s
->next_time
=qemu_get_be64(f
);
868 qemu_get_timer(f
, s
->timer
);
872 static void apic_reset(void *opaque
)
874 APICState
*s
= opaque
;
876 s
->apicbase
= 0xfee00000 |
877 (s
->id
? 0 : MSR_IA32_APICBASE_BSP
) | MSR_IA32_APICBASE_ENABLE
;
883 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
884 * time typically by BIOS, so PIC interrupt can be delivered to the
885 * processor when local APIC is enabled.
887 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
891 static CPUReadMemoryFunc
*apic_mem_read
[3] = {
897 static CPUWriteMemoryFunc
*apic_mem_write
[3] = {
903 int apic_init(CPUState
*env
)
907 if (last_apic_id
>= MAX_APICS
)
909 s
= qemu_mallocz(sizeof(APICState
));
913 s
->id
= last_apic_id
++;
914 env
->cpuid_apic_id
= s
->id
;
919 /* XXX: mapping more APICs at the same memory location */
920 if (apic_io_memory
== 0) {
921 /* NOTE: the APIC is directly connected to the CPU - it is not
922 on the global memory bus. */
923 apic_io_memory
= cpu_register_io_memory(0, apic_mem_read
,
924 apic_mem_write
, NULL
);
925 cpu_register_physical_memory(s
->apicbase
& ~0xfff, 0x1000,
928 s
->timer
= qemu_new_timer(vm_clock
, apic_timer
, s
);
930 register_savevm("apic", s
->id
, 2, apic_save
, apic_load
, s
);
931 qemu_register_reset(apic_reset
, s
);
933 local_apics
[s
->id
] = s
;
937 static void ioapic_service(IOAPICState
*s
)
942 uint8_t delivery_mode
;
948 uint32_t deliver_bitmask
[MAX_APIC_WORDS
];
950 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
953 entry
= s
->ioredtbl
[i
];
954 if (!(entry
& APIC_LVT_MASKED
)) {
955 trig_mode
= ((entry
>> 15) & 1);
957 dest_mode
= (entry
>> 11) & 1;
958 delivery_mode
= (entry
>> 8) & 7;
959 polarity
= (entry
>> 13) & 1;
960 if (trig_mode
== APIC_TRIGGER_EDGE
)
962 if (delivery_mode
== APIC_DM_EXTINT
)
963 vector
= pic_read_irq(isa_pic
);
965 vector
= entry
& 0xff;
967 apic_get_delivery_bitmask(deliver_bitmask
, dest
, dest_mode
);
968 apic_bus_deliver(deliver_bitmask
, delivery_mode
,
969 vector
, polarity
, trig_mode
);
975 void ioapic_set_irq(void *opaque
, int vector
, int level
)
977 IOAPICState
*s
= opaque
;
979 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
980 uint32_t mask
= 1 << vector
;
981 uint64_t entry
= s
->ioredtbl
[vector
];
983 if ((entry
>> 15) & 1) {
984 /* level triggered */
1001 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
1003 IOAPICState
*s
= opaque
;
1010 } else if (addr
== 0x10) {
1011 switch (s
->ioregsel
) {
1016 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
1022 index
= (s
->ioregsel
- 0x10) >> 1;
1023 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1024 if (s
->ioregsel
& 1)
1025 val
= s
->ioredtbl
[index
] >> 32;
1027 val
= s
->ioredtbl
[index
] & 0xffffffff;
1031 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
1037 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1039 IOAPICState
*s
= opaque
;
1046 } else if (addr
== 0x10) {
1048 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
1050 switch (s
->ioregsel
) {
1052 s
->id
= (val
>> 24) & 0xff;
1058 index
= (s
->ioregsel
- 0x10) >> 1;
1059 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
1060 if (s
->ioregsel
& 1) {
1061 s
->ioredtbl
[index
] &= 0xffffffff;
1062 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
1064 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
1065 s
->ioredtbl
[index
] |= val
;
1073 static void ioapic_save(QEMUFile
*f
, void *opaque
)
1075 IOAPICState
*s
= opaque
;
1078 qemu_put_8s(f
, &s
->id
);
1079 qemu_put_8s(f
, &s
->ioregsel
);
1080 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1081 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
1085 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
1087 IOAPICState
*s
= opaque
;
1090 if (version_id
!= 1)
1093 qemu_get_8s(f
, &s
->id
);
1094 qemu_get_8s(f
, &s
->ioregsel
);
1095 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
1096 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
1101 static void ioapic_reset(void *opaque
)
1103 IOAPICState
*s
= opaque
;
1106 memset(s
, 0, sizeof(*s
));
1107 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
1108 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
1111 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
1117 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
1123 IOAPICState
*ioapic_init(void)
1128 s
= qemu_mallocz(sizeof(IOAPICState
));
1132 s
->id
= last_apic_id
++;
1134 io_memory
= cpu_register_io_memory(0, ioapic_mem_read
,
1135 ioapic_mem_write
, s
);
1136 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
1138 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
1139 qemu_register_reset(ioapic_reset
, s
);