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/>.
27 #include "qemu-timer.h"
28 #include "host-utils.h"
31 //#define DEBUG_IOAPIC
34 #define DPRINTF(fmt, ...) \
35 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...)
42 #define IOAPIC_LVT_MASKED (1 << 16)
43 #define IOAPIC_LVT_REMOTE_IRR (1 << 14)
45 #define IOAPIC_TRIGGER_EDGE 0
46 #define IOAPIC_TRIGGER_LEVEL 1
48 /*io{apic,sapic} delivery mode*/
49 #define IOAPIC_DM_FIXED 0x0
50 #define IOAPIC_DM_LOWEST_PRIORITY 0x1
51 #define IOAPIC_DM_PMI 0x2
52 #define IOAPIC_DM_NMI 0x4
53 #define IOAPIC_DM_INIT 0x5
54 #define IOAPIC_DM_SIPI 0x5
55 #define IOAPIC_DM_EXTINT 0x7
57 typedef struct IOAPICState IOAPICState
;
65 uint64_t ioredtbl
[IOAPIC_NUM_PINS
];
68 static IOAPICState
*ioapics
[MAX_IOAPICS
];
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
) {
95 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
97 if (delivery_mode
== IOAPIC_DM_EXTINT
)
98 vector
= pic_read_irq(isa_pic
);
100 vector
= entry
& 0xff;
102 apic_deliver_irq(dest
, dest_mode
, delivery_mode
,
103 vector
, polarity
, trig_mode
);
109 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
111 IOAPICState
*s
= opaque
;
113 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
114 * to GSI 2. GSI maps to ioapic 1-1. This is not
115 * the cleanest way of doing it but it should work. */
117 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
121 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
122 uint32_t mask
= 1 << vector
;
123 uint64_t entry
= s
->ioredtbl
[vector
];
125 if ((entry
>> 15) & 1) {
126 /* level triggered */
143 void ioapic_eoi_broadcast(int vector
)
149 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
154 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
155 entry
= s
->ioredtbl
[n
];
156 if ((entry
& IOAPIC_LVT_REMOTE_IRR
) && (entry
& 0xff) == vector
) {
157 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
158 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
166 static uint32_t ioapic_mem_readl(void *opaque
, target_phys_addr_t addr
)
168 IOAPICState
*s
= opaque
;
175 } else if (addr
== 0x10) {
176 switch (s
->ioregsel
) {
181 val
= 0x11 | ((IOAPIC_NUM_PINS
- 1) << 16); /* version 0x11 */
187 index
= (s
->ioregsel
- 0x10) >> 1;
188 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
190 val
= s
->ioredtbl
[index
] >> 32;
192 val
= s
->ioredtbl
[index
] & 0xffffffff;
195 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
200 static void ioapic_mem_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
202 IOAPICState
*s
= opaque
;
209 } else if (addr
== 0x10) {
210 DPRINTF("write: %08x = %08x\n", s
->ioregsel
, val
);
211 switch (s
->ioregsel
) {
213 s
->id
= (val
>> 24) & 0xff;
219 index
= (s
->ioregsel
- 0x10) >> 1;
220 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
221 if (s
->ioregsel
& 1) {
222 s
->ioredtbl
[index
] &= 0xffffffff;
223 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
225 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
226 s
->ioredtbl
[index
] |= val
;
234 static int ioapic_post_load(void *opaque
, int version_id
)
236 IOAPICState
*s
= opaque
;
238 if (version_id
== 1) {
245 static const VMStateDescription vmstate_ioapic
= {
248 .post_load
= ioapic_post_load
,
249 .minimum_version_id
= 1,
250 .minimum_version_id_old
= 1,
251 .fields
= (VMStateField
[]) {
252 VMSTATE_UINT8(id
, IOAPICState
),
253 VMSTATE_UINT8(ioregsel
, IOAPICState
),
254 VMSTATE_UINT32_V(irr
, IOAPICState
, 2),
255 VMSTATE_UINT64_ARRAY(ioredtbl
, IOAPICState
, IOAPIC_NUM_PINS
),
256 VMSTATE_END_OF_LIST()
260 static void ioapic_reset(DeviceState
*d
)
262 IOAPICState
*s
= DO_UPCAST(IOAPICState
, busdev
.qdev
, d
);
268 for(i
= 0; i
< IOAPIC_NUM_PINS
; i
++)
269 s
->ioredtbl
[i
] = 1 << 16; /* mask LVT */
272 static CPUReadMemoryFunc
* const ioapic_mem_read
[3] = {
278 static CPUWriteMemoryFunc
* const ioapic_mem_write
[3] = {
284 static int ioapic_init1(SysBusDevice
*dev
)
286 IOAPICState
*s
= FROM_SYSBUS(IOAPICState
, dev
);
288 static int ioapic_no
;
290 if (ioapic_no
>= MAX_IOAPICS
) {
294 io_memory
= cpu_register_io_memory(ioapic_mem_read
,
296 DEVICE_NATIVE_ENDIAN
);
297 sysbus_init_mmio(dev
, 0x1000, io_memory
);
299 qdev_init_gpio_in(&dev
->qdev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
301 ioapics
[ioapic_no
++] = s
;
306 static SysBusDeviceInfo ioapic_info
= {
307 .init
= ioapic_init1
,
308 .qdev
.name
= "ioapic",
309 .qdev
.size
= sizeof(IOAPICState
),
310 .qdev
.vmsd
= &vmstate_ioapic
,
311 .qdev
.reset
= ioapic_reset
,
315 static void ioapic_register_devices(void)
317 sysbus_register_withprop(&ioapic_info
);
320 device_init(ioapic_register_devices
)