2 * ioapic.c IOAPIC emulation logic
4 * Copyright (c) 2004-2005 Fabrice Bellard
6 * Split the ioapic logic from apic.c
7 * Xiantao Zhang <xiantao.zhang@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "qemu-timer.h"
26 #include "host-utils.h"
28 //#define DEBUG_IOAPIC
30 #define IOAPIC_NUM_PINS 0x18
31 #define IOAPIC_LVT_MASKED (1<<16)
33 #define IOAPIC_TRIGGER_EDGE 0
34 #define IOAPIC_TRIGGER_LEVEL 1
36 /*io{apic,sapic} delivery mode*/
37 #define IOAPIC_DM_FIXED 0x0
38 #define IOAPIC_DM_LOWEST_PRIORITY 0x1
39 #define IOAPIC_DM_PMI 0x2
40 #define IOAPIC_DM_NMI 0x4
41 #define IOAPIC_DM_INIT 0x5
42 #define IOAPIC_DM_SIPI 0x5
43 #define IOAPIC_DM_EXTINT 0x7
50 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
53 static void ioapic_service(IOAPICState
*s
)
58 uint8_t delivery_mode
;
65 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
68 entry
= s
->ioredtbl
[i
];
69 if (!(entry
& IOAPIC_LVT_MASKED
)) {
70 trig_mode
= ((entry
>> 15) & 1);
72 dest_mode
= (entry
>> 11) & 1;
73 delivery_mode
= (entry
>> 8) & 7;
74 polarity
= (entry
>> 13) & 1;
75 if (trig_mode
== IOAPIC_TRIGGER_EDGE
)
77 if (delivery_mode
== IOAPIC_DM_EXTINT
)
78 vector
= pic_read_irq(isa_pic
);
80 vector
= entry
& 0xff;
82 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
83 vector
, polarity
, trig_mode
);
89 void ioapic_set_irq(void *opaque
, int vector
, int level
)
91 IOAPICState
*s
= opaque
;
93 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
94 * to GSI 2. GSI maps to ioapic 1-1. This is not
95 * the cleanest way of doing it but it should work. */
100 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
101 uint32_t mask
= 1 << vector
;
102 uint64_t entry
= s
->ioredtbl
[vector
];
104 if ((entry
>> 15) & 1) {
105 /* level triggered */
122 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
124 IOAPICState
*s
= opaque
;
131 } else if (addr
== 0x10) {
132 switch (s
->ioregsel
) {
137 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
143 index
= (s
->ioregsel
- 0x10) >> 1;
144 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
146 val
= s
->ioredtbl
[index
] >> 32;
148 val
= s
->ioredtbl
[index
] & 0xffffffff;
152 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
158 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
160 IOAPICState
*s
= opaque
;
167 } else if (addr
== 0x10) {
169 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
171 switch (s
->ioregsel
) {
173 s
->id
= (val
>> 24) & 0xff;
179 index
= (s
->ioregsel
- 0x10) >> 1;
180 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
181 if (s
->ioregsel
& 1) {
182 s
->ioredtbl
[index
] &= 0xffffffff;
183 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
185 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
186 s
->ioredtbl
[index
] |= val
;
194 static void ioapic_save(QEMUFile
*f
, void *opaque
)
196 IOAPICState
*s
= opaque
;
199 qemu_put_8s(f
, &s
->id
);
200 qemu_put_8s(f
, &s
->ioregsel
);
201 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
202 qemu_put_be64s(f
, &s
->ioredtbl
[i
]);
206 static int ioapic_load(QEMUFile
*f
, void *opaque
, int version_id
)
208 IOAPICState
*s
= opaque
;
214 qemu_get_8s(f
, &s
->id
);
215 qemu_get_8s(f
, &s
->ioregsel
);
216 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
217 qemu_get_be64s(f
, &s
->ioredtbl
[i
]);
222 static void ioapic_reset(void *opaque
)
224 IOAPICState
*s
= opaque
;
227 memset(s
, 0, sizeof(*s
));
228 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
229 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
232 static CPUReadMemoryFunc
*ioapic_mem_read
[3] = {
238 static CPUWriteMemoryFunc
*ioapic_mem_write
[3] = {
244 IOAPICState
*ioapic_init(void)
249 s
= qemu_mallocz(sizeof(IOAPICState
));
252 io_memory
= cpu_register_io_memory(ioapic_mem_read
,
253 ioapic_mem_write
, s
);
254 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
256 register_savevm("ioapic", 0, 1, ioapic_save
, ioapic_load
, s
);
257 qemu_register_reset(ioapic_reset
, s
);