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 "qemu/error-report.h"
25 #include "monitor/monitor.h"
27 #include "hw/i386/pc.h"
28 #include "hw/i386/apic.h"
29 #include "hw/i386/ioapic.h"
30 #include "hw/i386/ioapic_internal.h"
31 #include "include/hw/pci/msi.h"
32 #include "sysemu/kvm.h"
33 #include "target/i386/cpu.h"
34 #include "hw/i386/apic-msidef.h"
35 #include "hw/i386/x86-iommu.h"
38 //#define DEBUG_IOAPIC
41 #define DPRINTF(fmt, ...) \
42 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
44 #define DPRINTF(fmt, ...)
47 #define APIC_DELIVERY_MODE_SHIFT 8
48 #define APIC_POLARITY_SHIFT 14
49 #define APIC_TRIG_MODE_SHIFT 15
51 static IOAPICCommonState
*ioapics
[MAX_IOAPICS
];
53 /* global variable from ioapic_common.c */
56 struct ioapic_entry_info
{
57 /* fields parsed from IOAPIC entries */
62 uint8_t delivery_mode
;
65 /* MSI message generated from above parsed fields */
70 static void ioapic_entry_parse(uint64_t entry
, struct ioapic_entry_info
*info
)
72 memset(info
, 0, sizeof(*info
));
73 info
->masked
= (entry
>> IOAPIC_LVT_MASKED_SHIFT
) & 1;
74 info
->trig_mode
= (entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1;
76 * By default, this would be dest_id[8] + reserved[8]. When IR
77 * is enabled, this would be interrupt_index[15] +
78 * interrupt_format[1]. This field never means anything, but
79 * only used to generate corresponding MSI.
81 info
->dest_idx
= (entry
>> IOAPIC_LVT_DEST_IDX_SHIFT
) & 0xffff;
82 info
->dest_mode
= (entry
>> IOAPIC_LVT_DEST_MODE_SHIFT
) & 1;
83 info
->delivery_mode
= (entry
>> IOAPIC_LVT_DELIV_MODE_SHIFT
) \
85 if (info
->delivery_mode
== IOAPIC_DM_EXTINT
) {
86 info
->vector
= pic_read_irq(isa_pic
);
88 info
->vector
= entry
& IOAPIC_VECTOR_MASK
;
91 info
->addr
= APIC_DEFAULT_ADDRESS
| \
92 (info
->dest_idx
<< MSI_ADDR_DEST_IDX_SHIFT
) | \
93 (info
->dest_mode
<< MSI_ADDR_DEST_MODE_SHIFT
);
94 info
->data
= (info
->vector
<< MSI_DATA_VECTOR_SHIFT
) | \
95 (info
->trig_mode
<< MSI_DATA_TRIGGER_SHIFT
) | \
96 (info
->delivery_mode
<< MSI_DATA_DELIVERY_MODE_SHIFT
);
99 static void ioapic_service(IOAPICCommonState
*s
)
101 AddressSpace
*ioapic_as
= PC_MACHINE(qdev_get_machine())->ioapic_as
;
102 struct ioapic_entry_info info
;
107 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
112 entry
= s
->ioredtbl
[i
];
113 ioapic_entry_parse(entry
, &info
);
115 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
118 coalesce
= s
->ioredtbl
[i
] & IOAPIC_LVT_REMOTE_IRR
;
119 trace_ioapic_set_remote_irr(i
);
120 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
124 /* We are level triggered interrupts, and the
125 * guest should be still working on previous one,
131 if (kvm_irqchip_is_split()) {
132 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
133 kvm_set_irq(kvm_state
, i
, 1);
134 kvm_set_irq(kvm_state
, i
, 0);
136 kvm_set_irq(kvm_state
, i
, 1);
142 /* No matter whether IR is enabled, we translate
143 * the IOAPIC message into a MSI one, and its
144 * address space will decide whether we need a
146 stl_le_phys(ioapic_as
, info
.addr
, info
.data
);
152 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
154 IOAPICCommonState
*s
= opaque
;
156 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
157 * to GSI 2. GSI maps to ioapic 1-1. This is not
158 * the cleanest way of doing it but it should work. */
160 DPRINTF("%s: %s vec %x\n", __func__
, level
? "raise" : "lower", vector
);
164 if (vector
>= 0 && vector
< IOAPIC_NUM_PINS
) {
165 uint32_t mask
= 1 << vector
;
166 uint64_t entry
= s
->ioredtbl
[vector
];
168 if (((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) ==
169 IOAPIC_TRIGGER_LEVEL
) {
170 /* level triggered */
173 if (!(entry
& IOAPIC_LVT_REMOTE_IRR
)) {
180 /* According to the 82093AA manual, we must ignore edge requests
181 * if the input pin is masked. */
182 if (level
&& !(entry
& IOAPIC_LVT_MASKED
)) {
190 static void ioapic_update_kvm_routes(IOAPICCommonState
*s
)
195 if (kvm_irqchip_is_split()) {
196 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
198 struct ioapic_entry_info info
;
199 ioapic_entry_parse(s
->ioredtbl
[i
], &info
);
200 msg
.address
= info
.addr
;
201 msg
.data
= info
.data
;
202 kvm_irqchip_update_msi_route(kvm_state
, i
, msg
, NULL
);
204 kvm_irqchip_commit_routes(kvm_state
);
210 static void ioapic_iec_notifier(void *private, bool global
,
211 uint32_t index
, uint32_t mask
)
213 IOAPICCommonState
*s
= (IOAPICCommonState
*)private;
214 /* For simplicity, we just update all the routes */
215 ioapic_update_kvm_routes(s
);
219 void ioapic_eoi_broadcast(int vector
)
221 IOAPICCommonState
*s
;
225 trace_ioapic_eoi_broadcast(vector
);
227 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
232 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
233 entry
= s
->ioredtbl
[n
];
234 if ((entry
& IOAPIC_LVT_REMOTE_IRR
)
235 && (entry
& IOAPIC_VECTOR_MASK
) == vector
) {
236 trace_ioapic_clear_remote_irr(n
, vector
);
237 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
238 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
246 void ioapic_dump_state(Monitor
*mon
, const QDict
*qdict
)
250 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
251 if (ioapics
[i
] != 0) {
252 ioapic_print_redtbl(mon
, ioapics
[i
]);
258 ioapic_mem_read(void *opaque
, hwaddr addr
, unsigned int size
)
260 IOAPICCommonState
*s
= opaque
;
267 case IOAPIC_IOREGSEL
:
274 switch (s
->ioregsel
) {
277 val
= s
->id
<< IOAPIC_ID_SHIFT
;
281 ((IOAPIC_NUM_PINS
- 1) << IOAPIC_VER_ENTRIES_SHIFT
);
284 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
285 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
286 if (s
->ioregsel
& 1) {
287 val
= s
->ioredtbl
[index
] >> 32;
289 val
= s
->ioredtbl
[index
] & 0xffffffff;
293 DPRINTF("read: %08x = %08x\n", s
->ioregsel
, val
);
297 trace_ioapic_mem_read(addr
, size
, val
);
303 * This is to satisfy the hack in Linux kernel. One hack of it is to
304 * simulate clearing the Remote IRR bit of IOAPIC entry using the
307 * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
308 * Otherwise, we simulate the EOI message manually by changing the trigger
309 * mode to edge and then back to level, with RTE being masked during
312 * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
314 * This is based on the assumption that, Remote IRR bit will be
315 * cleared by IOAPIC hardware when configured as edge-triggered
318 * Without this, level-triggered interrupts in IR mode might fail to
322 ioapic_fix_edge_remote_irr(uint64_t *entry
)
324 if (!(*entry
& IOAPIC_LVT_TRIGGER_MODE
)) {
325 /* Edge-triggered interrupts, make sure remote IRR is zero */
326 *entry
&= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR
);
331 ioapic_mem_write(void *opaque
, hwaddr addr
, uint64_t val
,
334 IOAPICCommonState
*s
= opaque
;
338 trace_ioapic_mem_write(addr
, size
, val
);
341 case IOAPIC_IOREGSEL
:
348 DPRINTF("write: %08x = %08" PRIx64
"\n", s
->ioregsel
, val
);
349 switch (s
->ioregsel
) {
351 s
->id
= (val
>> IOAPIC_ID_SHIFT
) & IOAPIC_ID_MASK
;
357 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
358 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
359 uint64_t ro_bits
= s
->ioredtbl
[index
] & IOAPIC_RO_BITS
;
360 if (s
->ioregsel
& 1) {
361 s
->ioredtbl
[index
] &= 0xffffffff;
362 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
364 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
365 s
->ioredtbl
[index
] |= val
;
367 /* restore RO bits */
368 s
->ioredtbl
[index
] &= IOAPIC_RW_BITS
;
369 s
->ioredtbl
[index
] |= ro_bits
;
370 ioapic_fix_edge_remote_irr(&s
->ioredtbl
[index
]);
376 /* Explicit EOI is only supported for IOAPIC version 0x20 */
377 if (size
!= 4 || s
->version
!= 0x20) {
380 ioapic_eoi_broadcast(val
);
384 ioapic_update_kvm_routes(s
);
387 static const MemoryRegionOps ioapic_io_ops
= {
388 .read
= ioapic_mem_read
,
389 .write
= ioapic_mem_write
,
390 .endianness
= DEVICE_NATIVE_ENDIAN
,
393 static void ioapic_machine_done_notify(Notifier
*notifier
, void *data
)
396 IOAPICCommonState
*s
= container_of(notifier
, IOAPICCommonState
,
399 if (kvm_irqchip_is_split()) {
400 X86IOMMUState
*iommu
= x86_iommu_get_default();
402 /* Register this IOAPIC with IOMMU IEC notifier, so that
403 * when there are IR invalidates, we can be notified to
404 * update kernel IR cache. */
405 x86_iommu_iec_register_notifier(iommu
, ioapic_iec_notifier
, s
);
411 #define IOAPIC_VER_DEF 0x20
413 static void ioapic_realize(DeviceState
*dev
, Error
**errp
)
415 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
417 if (s
->version
!= 0x11 && s
->version
!= 0x20) {
418 error_report("IOAPIC only supports version 0x11 or 0x20 "
419 "(default: 0x%x).", IOAPIC_VER_DEF
);
423 memory_region_init_io(&s
->io_memory
, OBJECT(s
), &ioapic_io_ops
, s
,
426 qdev_init_gpio_in(dev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
428 ioapics
[ioapic_no
] = s
;
429 s
->machine_done
.notify
= ioapic_machine_done_notify
;
430 qemu_add_machine_init_done_notifier(&s
->machine_done
);
433 static Property ioapic_properties
[] = {
434 DEFINE_PROP_UINT8("version", IOAPICCommonState
, version
, IOAPIC_VER_DEF
),
435 DEFINE_PROP_END_OF_LIST(),
438 static void ioapic_class_init(ObjectClass
*klass
, void *data
)
440 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
441 DeviceClass
*dc
= DEVICE_CLASS(klass
);
443 k
->realize
= ioapic_realize
;
445 * If APIC is in kernel, we need to update the kernel cache after
446 * migration, otherwise first 24 gsi routes will be invalid.
448 k
->post_load
= ioapic_update_kvm_routes
;
449 dc
->reset
= ioapic_reset_common
;
450 dc
->props
= ioapic_properties
;
452 * FIXME: Set only because we are not sure yet if this device
453 * will be outside the q35 sysbus whitelist.
455 dc
->user_creatable
= true;
458 static const TypeInfo ioapic_info
= {
460 .parent
= TYPE_IOAPIC_COMMON
,
461 .instance_size
= sizeof(IOAPICCommonState
),
462 .class_init
= ioapic_class_init
,
465 static void ioapic_register_types(void)
467 type_register_static(&ioapic_info
);
470 type_init(ioapic_register_types
)