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"
34 //#define DEBUG_IOAPIC
37 #define DPRINTF(fmt, ...) \
38 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...)
43 #define IOAPIC_DEFAULT_BASE_ADDRESS 0xfec00000
44 #define IOAPIC_LVT_MASKED (1<<16)
46 #define IOAPIC_TRIGGER_EDGE 0
47 #define IOAPIC_TRIGGER_LEVEL 1
49 /*io{apic,sapic} delivery mode*/
50 #define IOAPIC_DM_FIXED 0x0
51 #define IOAPIC_DM_LOWEST_PRIORITY 0x1
52 #define IOAPIC_DM_PMI 0x2
53 #define IOAPIC_DM_NMI 0x4
54 #define IOAPIC_DM_INIT 0x5
55 #define IOAPIC_DM_SIPI 0x5
56 #define IOAPIC_DM_EXTINT 0x7
58 typedef struct IOAPICState IOAPICState
;
64 uint64_t base_address
;
67 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
70 static void ioapic_service(IOAPICState
*s
)
75 uint8_t delivery_mode
;
82 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
85 entry
= s
->ioredtbl
[i
];
86 if (!(entry
& IOAPIC_LVT_MASKED
)) {
87 trig_mode
= ((entry
>> 15) & 1);
89 dest_mode
= (entry
>> 11) & 1;
90 delivery_mode
= (entry
>> 8) & 7;
91 polarity
= (entry
>> 13) & 1;
92 if (trig_mode
== IOAPIC_TRIGGER_EDGE
)
94 if (delivery_mode
== IOAPIC_DM_EXTINT
)
95 vector
= pic_read_irq(isa_pic
);
97 vector
= entry
& 0xff;
99 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
100 vector
, polarity
, trig_mode
);
106 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
108 IOAPICState
*s
= opaque
;
110 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
111 * to GSI 2. GSI maps to ioapic 1-1. This is not
112 * the cleanest way of doing it but it should work. */
114 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
115 if (vector
== 0 && irq0override
) {
119 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
120 uint32_t mask
= 1 << vector
;
121 uint64_t entry
= s
->ioredtbl
[vector
];
123 if ((entry
>> 15) & 1) {
124 /* level triggered */
141 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
143 IOAPICState
*s
= opaque
;
150 } else if (addr
== 0x10) {
151 switch (s
->ioregsel
) {
156 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
162 index
= (s
->ioregsel
- 0x10) >> 1;
163 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
165 val
= s
->ioredtbl
[index
] >> 32;
167 val
= s
->ioredtbl
[index
] & 0xffffffff;
170 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
175 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
177 IOAPICState
*s
= opaque
;
184 } else if (addr
== 0x10) {
185 DPRINTF("write: %08x = %08x\n", s
->ioregsel
, val
);
186 switch (s
->ioregsel
) {
188 s
->id
= (val
>> 24) & 0xff;
194 index
= (s
->ioregsel
- 0x10) >> 1;
195 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
196 if (s
->ioregsel
& 1) {
197 s
->ioredtbl
[index
] &= 0xffffffff;
198 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
200 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
201 s
->ioredtbl
[index
] |= val
;
209 static void kvm_kernel_ioapic_save_to_user(IOAPICState
*s
)
211 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
212 struct kvm_irqchip chip
;
213 struct kvm_ioapic_state
*kioapic
;
216 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
217 kvm_get_irqchip(kvm_context
, &chip
);
218 kioapic
= &chip
.chip
.ioapic
;
221 s
->ioregsel
= kioapic
->ioregsel
;
222 s
->base_address
= kioapic
->base_address
;
223 s
->irr
= kioapic
->irr
;
224 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
225 s
->ioredtbl
[i
] = kioapic
->redirtbl
[i
].bits
;
230 static void kvm_kernel_ioapic_load_from_user(IOAPICState
*s
)
232 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
233 struct kvm_irqchip chip
;
234 struct kvm_ioapic_state
*kioapic
;
237 chip
.chip_id
= KVM_IRQCHIP_IOAPIC
;
238 kioapic
= &chip
.chip
.ioapic
;
241 kioapic
->ioregsel
= s
->ioregsel
;
242 kioapic
->base_address
= s
->base_address
;
243 kioapic
->irr
= s
->irr
;
244 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
245 kioapic
->redirtbl
[i
].bits
= s
->ioredtbl
[i
];
248 kvm_set_irqchip(kvm_context
, &chip
);
252 static void ioapic_pre_save(void *opaque
)
254 IOAPICState
*s
= (void *)opaque
;
256 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
257 kvm_kernel_ioapic_save_to_user(s
);
261 static int ioapic_pre_load(void *opaque
)
263 IOAPICState
*s
= opaque
;
265 /* in case we are doing version 1, we just set these to sane values */
266 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
271 static int ioapic_post_load(void *opaque
, int version_id
)
273 IOAPICState
*s
= opaque
;
275 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
276 kvm_kernel_ioapic_load_from_user(s
);
281 static const VMStateDescription vmstate_ioapic
= {
284 .minimum_version_id
= 1,
285 .minimum_version_id_old
= 1,
286 .pre_load
= ioapic_pre_load
,
287 .post_load
= ioapic_post_load
,
288 .pre_save
= ioapic_pre_save
,
289 .fields
= (VMStateField
[]) {
290 VMSTATE_UINT8(id
, IOAPICState
),
291 VMSTATE_UINT8(ioregsel
, IOAPICState
),
292 VMSTATE_UINT64_V(base_address
, IOAPICState
, 2),
293 VMSTATE_UINT32_V(irr
, IOAPICState
, 2),
294 VMSTATE_UINT64_ARRAY(ioredtbl
, IOAPICState
, IOAPIC_NUM_PINS
),
295 VMSTATE_END_OF_LIST()
299 static void ioapic_reset(DeviceState
*d
)
301 IOAPICState
*s
= DO_UPCAST(IOAPICState
, busdev
.qdev
, d
);
304 s
->base_address
= IOAPIC_DEFAULT_BASE_ADDRESS
;
308 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
309 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
310 #ifdef KVM_CAP_IRQCHIP
311 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
312 kvm_kernel_ioapic_load_from_user(s
);
317 static CPUReadMemoryFunc
* const ioapic_mem_read
[3] = {
323 static CPUWriteMemoryFunc
* const ioapic_mem_write
[3] = {
329 static int ioapic_init1(SysBusDevice
*dev
)
331 IOAPICState
*s
= FROM_SYSBUS(IOAPICState
, dev
);
334 io_memory
= cpu_register_io_memory(ioapic_mem_read
,
335 ioapic_mem_write
, s
);
336 sysbus_init_mmio(dev
, 0x1000, io_memory
);
338 qdev_init_gpio_in(&dev
->qdev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
343 static SysBusDeviceInfo ioapic_info
= {
344 .init
= ioapic_init1
,
345 .qdev
.name
= "ioapic",
346 .qdev
.size
= sizeof(IOAPICState
),
347 .qdev
.vmsd
= &vmstate_ioapic
,
348 .qdev
.reset
= ioapic_reset
,
352 static void ioapic_register_devices(void)
354 sysbus_register_withprop(&ioapic_info
);
357 device_init(ioapic_register_devices
)