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/>.
28 #include "qemu-timer.h"
29 #include "host-utils.h"
33 //#define DEBUG_IOAPIC
35 #define IOAPIC_NUM_PINS 0x18
36 #define IOAPIC_DEFAULT_BASE_ADDRESS 0xfec00000
37 #define IOAPIC_LVT_MASKED (1<<16)
39 #define IOAPIC_TRIGGER_EDGE 0
40 #define IOAPIC_TRIGGER_LEVEL 1
42 /*io{apic,sapic} delivery mode*/
43 #define IOAPIC_DM_FIXED 0x0
44 #define IOAPIC_DM_LOWEST_PRIORITY 0x1
45 #define IOAPIC_DM_PMI 0x2
46 #define IOAPIC_DM_NMI 0x4
47 #define IOAPIC_DM_INIT 0x5
48 #define IOAPIC_DM_SIPI 0x5
49 #define IOAPIC_DM_EXTINT 0x7
54 uint64_t base_address
;
57 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
60 static void ioapic_service(IOAPICState
*s
)
65 uint8_t delivery_mode
;
72 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
75 entry
= s
->ioredtbl
[i
];
76 if (!(entry
& IOAPIC_LVT_MASKED
)) {
77 trig_mode
= ((entry
>> 15) & 1);
79 dest_mode
= (entry
>> 11) & 1;
80 delivery_mode
= (entry
>> 8) & 7;
81 polarity
= (entry
>> 13) & 1;
82 if (trig_mode
== IOAPIC_TRIGGER_EDGE
)
84 if (delivery_mode
== IOAPIC_DM_EXTINT
)
85 vector
= pic_read_irq(isa_pic
);
87 vector
= entry
& 0xff;
89 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
90 vector
, polarity
, trig_mode
);
96 void ioapic_set_irq(void *opaque
, int vector
, int level
)
98 IOAPICState
*s
= opaque
;
100 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
101 * to GSI 2. GSI maps to ioapic 1-1. This is not
102 * the cleanest way of doing it but it should work. */
104 if (vector
== 0 && irq0override
) {
108 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
109 uint32_t mask
= 1 << vector
;
110 uint64_t entry
= s
->ioredtbl
[vector
];
112 if ((entry
>> 15) & 1) {
113 /* level triggered */
130 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
132 IOAPICState
*s
= opaque
;
139 } else if (addr
== 0x10) {
140 switch (s
->ioregsel
) {
145 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
151 index
= (s
->ioregsel
- 0x10) >> 1;
152 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
154 val
= s
->ioredtbl
[index
] >> 32;
156 val
= s
->ioredtbl
[index
] & 0xffffffff;
160 printf("I/O APIC read: %08x = %08x\n", s
->ioregsel
, val
);
166 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
168 IOAPICState
*s
= opaque
;
175 } else if (addr
== 0x10) {
177 printf("I/O APIC write: %08x = %08x\n", s
->ioregsel
, val
);
179 switch (s
->ioregsel
) {
181 s
->id
= (val
>> 24) & 0xff;
187 index
= (s
->ioregsel
- 0x10) >> 1;
188 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
189 if (s
->ioregsel
& 1) {
190 s
->ioredtbl
[index
] &= 0xffffffff;
191 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
193 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
194 s
->ioredtbl
[index
] |= val
;
202 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
204 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
205 struct kvm_irqchip chip
;
206 struct kvm_ioapic_state
*kioapic
;
209 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
210 kvm_get_irqchip(kvm_context
, &chip
);
211 kioapic
= &chip
.chip
.ioapic
;
214 s
->ioregsel
= kioapic
->ioregsel
;
215 s
->base_address
= kioapic
->base_address
;
216 s
->irr
= kioapic
->irr
;
217 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
218 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
223 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
225 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
226 struct kvm_irqchip chip
;
227 struct kvm_ioapic_state
*kioapic
;
230 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
231 kioapic
= &chip
.chip
.ioapic
;
234 kioapic
->ioregsel
= s
->ioregsel
;
235 kioapic
->base_address
= s
->base_address
;
236 kioapic
->irr
= s
->irr
;
237 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
238 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
241 kvm_set_irqchip(kvm_context
, &chip
);
245 static void ioapic_pre_save(void *opaque
)
247 IOAPICState
*s
= (void *)opaque
;
249 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
250 kvm_kernel_ioapic_save_to_user(s
);
254 static int ioapic_pre_load(void *opaque
)
256 IOAPICState
*s
= opaque
;
258 /* in case we are doing version 1, we just set these to sane values */
259 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
264 static int ioapic_post_load(void *opaque
, int version_id
)
266 IOAPICState
*s
= opaque
;
268 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
269 kvm_kernel_ioapic_load_from_user(s
);
274 static const VMStateDescription vmstate_ioapic
= {
277 .minimum_version_id
= 1,
278 .minimum_version_id_old
= 1,
279 .pre_load
= ioapic_pre_load
,
280 .post_load
= ioapic_post_load
,
281 .pre_save
= ioapic_pre_save
,
282 .fields
= (VMStateField
[]) {
283 VMSTATE_UINT8(id
, IOAPICState
),
284 VMSTATE_UINT8(ioregsel
, IOAPICState
),
285 VMSTATE_UINT64_V(base_address
, IOAPICState
, 2),
286 VMSTATE_UINT32_V(irr
, IOAPICState
, 2),
287 VMSTATE_UINT64_ARRAY(ioredtbl
, IOAPICState
, IOAPIC_NUM_PINS
),
288 VMSTATE_END_OF_LIST()
292 static void ioapic_reset(void *opaque
)
294 IOAPICState
*s
= opaque
;
297 memset(s
, 0, sizeof(*s
));
298 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
299 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
300 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
301 #ifdef KVM_CAP_IRQCHIP
302 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
303 kvm_kernel_ioapic_load_from_user(s
);
308 static CPUReadMemoryFunc
* const ioapic_mem_read
[3] = {
314 static CPUWriteMemoryFunc
* const ioapic_mem_write
[3] = {
320 qemu_irq
*ioapic_init(void)
326 s
= qemu_mallocz(sizeof(IOAPICState
));
329 io_memory
= cpu_register_io_memory(ioapic_mem_read
,
330 ioapic_mem_write
, s
);
331 cpu_register_physical_memory(0xfec00000, 0x1000, io_memory
);
333 vmstate_register(0, &vmstate_ioapic
, s
);
334 qemu_register_reset(ioapic_reset
, s
);
335 irq
= qemu_allocate_irqs(ioapic_set_irq
, s
, IOAPIC_NUM_PINS
);