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/>.
23 #include "qemu/osdep.h"
24 #include "monitor/monitor.h"
26 #include "hw/i386/pc.h"
27 #include "hw/i386/apic.h"
28 #include "hw/i386/ioapic.h"
29 #include "hw/i386/ioapic_internal.h"
30 #include "include/hw/pci/msi.h"
31 #include "sysemu/kvm.h"
32 #include "target-i386/cpu.h"
33 #include "hw/i386/apic-msidef.h"
35 //#define DEBUG_IOAPIC
38 #define DPRINTF(fmt, ...) \
39 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
41 #define DPRINTF(fmt, ...)
44 #define APIC_DELIVERY_MODE_SHIFT 8
45 #define APIC_POLARITY_SHIFT 14
46 #define APIC_TRIG_MODE_SHIFT 15
48 static IOAPICCommonState
*ioapics
[MAX_IOAPICS
];
50 /* global variable from ioapic_common.c */
53 struct ioapic_entry_info
{
54 /* fields parsed from IOAPIC entries */
59 uint8_t delivery_mode
;
62 /* MSI message generated from above parsed fields */
67 static void ioapic_entry_parse(uint64_t entry
, struct ioapic_entry_info
*info
)
69 memset(info
, 0, sizeof(*info
));
70 info
->masked
= (entry
>> IOAPIC_LVT_MASKED_SHIFT
) & 1;
71 info
->trig_mode
= (entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1;
73 * By default, this would be dest_id[8] + reserved[8]. When IR
74 * is enabled, this would be interrupt_index[15] +
75 * interrupt_format[1]. This field never means anything, but
76 * only used to generate corresponding MSI.
78 info
->dest_idx
= (entry
>> IOAPIC_LVT_DEST_IDX_SHIFT
) & 0xffff;
79 info
->dest_mode
= (entry
>> IOAPIC_LVT_DEST_MODE_SHIFT
) & 1;
80 info
->delivery_mode
= (entry
>> IOAPIC_LVT_DELIV_MODE_SHIFT
) \
82 if (info
->delivery_mode
== IOAPIC_DM_EXTINT
) {
83 info
->vector
= pic_read_irq(isa_pic
);
85 info
->vector
= entry
& IOAPIC_VECTOR_MASK
;
88 info
->addr
= APIC_DEFAULT_ADDRESS
| \
89 (info
->dest_idx
<< MSI_ADDR_DEST_IDX_SHIFT
) | \
90 (info
->dest_mode
<< MSI_ADDR_DEST_MODE_SHIFT
);
91 info
->data
= (info
->vector
<< MSI_DATA_VECTOR_SHIFT
) | \
92 (info
->trig_mode
<< MSI_DATA_TRIGGER_SHIFT
) | \
93 (info
->delivery_mode
<< MSI_DATA_DELIVERY_MODE_SHIFT
);
96 static void ioapic_service(IOAPICCommonState
*s
)
98 AddressSpace
*ioapic_as
= PC_MACHINE(qdev_get_machine())->ioapic_as
;
99 struct ioapic_entry_info info
;
104 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
109 entry
= s
->ioredtbl
[i
];
110 ioapic_entry_parse(entry
, &info
);
112 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
115 coalesce
= s
->ioredtbl
[i
] & IOAPIC_LVT_REMOTE_IRR
;
116 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
120 if (kvm_irqchip_is_split()) {
121 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
122 kvm_set_irq(kvm_state
, i
, 1);
123 kvm_set_irq(kvm_state
, i
, 0);
126 kvm_set_irq(kvm_state
, i
, 1);
134 /* No matter whether IR is enabled, we translate
135 * the IOAPIC message into a MSI one, and its
136 * address space will decide whether we need a
138 stl_le_phys(ioapic_as
, info
.addr
, info
.data
);
144 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
146 IOAPICCommonState
*s
= opaque
;
148 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
149 * to GSI 2. GSI maps to ioapic 1-1. This is not
150 * the cleanest way of doing it but it should work. */
152 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
156 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
157 uint32_t mask
= 1 << vector
;
158 uint64_t entry
= s
->ioredtbl
[vector
];
160 if (((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) ==
161 IOAPIC_TRIGGER_LEVEL
) {
162 /* level triggered */
165 if (!(entry
& IOAPIC_LVT_REMOTE_IRR
)) {
172 /* According to the 82093AA manual, we must ignore edge requests
173 * if the input pin is masked. */
174 if (level
&& !(entry
& IOAPIC_LVT_MASKED
)) {
182 static void ioapic_update_kvm_routes(IOAPICCommonState
*s
)
187 if (kvm_irqchip_is_split()) {
188 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
190 struct ioapic_entry_info info
;
191 ioapic_entry_parse(s
->ioredtbl
[i
], &info
);
192 msg
.address
= info
.addr
;
193 msg
.data
= info
.data
;
194 kvm_irqchip_update_msi_route(kvm_state
, i
, msg
, NULL
);
196 kvm_irqchip_commit_routes(kvm_state
);
201 void ioapic_eoi_broadcast(int vector
)
203 IOAPICCommonState
*s
;
207 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
212 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
213 entry
= s
->ioredtbl
[n
];
214 if ((entry
& IOAPIC_LVT_REMOTE_IRR
)
215 && (entry
& IOAPIC_VECTOR_MASK
) == vector
) {
216 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
217 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
225 void ioapic_dump_state(Monitor
*mon
, const QDict
*qdict
)
229 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
230 if (ioapics
[i
] != 0) {
231 ioapic_print_redtbl(mon
, ioapics
[i
]);
237 ioapic_mem_read(void *opaque
, hwaddr addr
, unsigned int size
)
239 IOAPICCommonState
*s
= opaque
;
243 switch (addr
& 0xff) {
244 case IOAPIC_IOREGSEL
:
251 switch (s
->ioregsel
) {
254 val
= s
->id
<< IOAPIC_ID_SHIFT
;
257 val
= IOAPIC_VERSION
|
258 ((IOAPIC_NUM_PINS
- 1) << IOAPIC_VER_ENTRIES_SHIFT
);
261 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
262 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
263 if (s
->ioregsel
& 1) {
264 val
= s
->ioredtbl
[index
] >> 32;
266 val
= s
->ioredtbl
[index
] & 0xffffffff;
270 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
277 * This is to satisfy the hack in Linux kernel. One hack of it is to
278 * simulate clearing the Remote IRR bit of IOAPIC entry using the
281 * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
282 * Otherwise, we simulate the EOI message manually by changing the trigger
283 * mode to edge and then back to level, with RTE being masked during
286 * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
288 * This is based on the assumption that, Remote IRR bit will be
289 * cleared by IOAPIC hardware when configured as edge-triggered
292 * Without this, level-triggered interrupts in IR mode might fail to
296 ioapic_fix_edge_remote_irr(uint64_t *entry
)
298 if (!(*entry
& IOAPIC_LVT_TRIGGER_MODE
)) {
299 /* Edge-triggered interrupts, make sure remote IRR is zero */
300 *entry
&= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR
);
305 ioapic_mem_write(void *opaque
, hwaddr addr
, uint64_t val
,
308 IOAPICCommonState
*s
= opaque
;
311 switch (addr
& 0xff) {
312 case IOAPIC_IOREGSEL
:
319 DPRINTF("write: %08x = %08" PRIx64
"\n", s
->ioregsel
, val
);
320 switch (s
->ioregsel
) {
322 s
->id
= (val
>> IOAPIC_ID_SHIFT
) & IOAPIC_ID_MASK
;
328 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
329 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
330 uint64_t ro_bits
= s
->ioredtbl
[index
] & IOAPIC_RO_BITS
;
331 if (s
->ioregsel
& 1) {
332 s
->ioredtbl
[index
] &= 0xffffffff;
333 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
335 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
336 s
->ioredtbl
[index
] |= val
;
338 /* restore RO bits */
339 s
->ioredtbl
[index
] &= IOAPIC_RW_BITS
;
340 s
->ioredtbl
[index
] |= ro_bits
;
341 ioapic_fix_edge_remote_irr(&s
->ioredtbl
[index
]);
348 ioapic_update_kvm_routes(s
);
351 static const MemoryRegionOps ioapic_io_ops
= {
352 .read
= ioapic_mem_read
,
353 .write
= ioapic_mem_write
,
354 .endianness
= DEVICE_NATIVE_ENDIAN
,
357 static void ioapic_realize(DeviceState
*dev
, Error
**errp
)
359 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
361 memory_region_init_io(&s
->io_memory
, OBJECT(s
), &ioapic_io_ops
, s
,
364 qdev_init_gpio_in(dev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
366 ioapics
[ioapic_no
] = s
;
369 static void ioapic_class_init(ObjectClass
*klass
, void *data
)
371 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
372 DeviceClass
*dc
= DEVICE_CLASS(klass
);
374 k
->realize
= ioapic_realize
;
375 dc
->reset
= ioapic_reset_common
;
378 static const TypeInfo ioapic_info
= {
380 .parent
= TYPE_IOAPIC_COMMON
,
381 .instance_size
= sizeof(IOAPICCommonState
),
382 .class_init
= ioapic_class_init
,
385 static void ioapic_register_types(void)
387 type_register_static(&ioapic_info
);
390 type_init(ioapic_register_types
)