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.1 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 "qapi/error.h"
25 #include "monitor/monitor.h"
26 #include "hw/i386/apic.h"
27 #include "hw/i386/x86.h"
28 #include "hw/intc/i8259.h"
29 #include "hw/intc/ioapic.h"
30 #include "hw/intc/ioapic_internal.h"
31 #include "hw/pci/msi.h"
32 #include "hw/qdev-properties.h"
33 #include "sysemu/kvm.h"
34 #include "sysemu/sysemu.h"
35 #include "hw/i386/apic-msidef.h"
36 #include "hw/i386/x86-iommu.h"
39 #define APIC_DELIVERY_MODE_SHIFT 8
40 #define APIC_POLARITY_SHIFT 14
41 #define APIC_TRIG_MODE_SHIFT 15
43 static IOAPICCommonState
*ioapics
[MAX_IOAPICS
];
45 /* global variable from ioapic_common.c */
48 struct ioapic_entry_info
{
49 /* fields parsed from IOAPIC entries */
54 uint8_t delivery_mode
;
57 /* MSI message generated from above parsed fields */
62 static void ioapic_entry_parse(uint64_t entry
, struct ioapic_entry_info
*info
)
64 memset(info
, 0, sizeof(*info
));
65 info
->masked
= (entry
>> IOAPIC_LVT_MASKED_SHIFT
) & 1;
66 info
->trig_mode
= (entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1;
68 * By default, this would be dest_id[8] + reserved[8]. When IR
69 * is enabled, this would be interrupt_index[15] +
70 * interrupt_format[1]. This field never means anything, but
71 * only used to generate corresponding MSI.
73 info
->dest_idx
= (entry
>> IOAPIC_LVT_DEST_IDX_SHIFT
) & 0xffff;
74 info
->dest_mode
= (entry
>> IOAPIC_LVT_DEST_MODE_SHIFT
) & 1;
75 info
->delivery_mode
= (entry
>> IOAPIC_LVT_DELIV_MODE_SHIFT
) \
77 if (info
->delivery_mode
== IOAPIC_DM_EXTINT
) {
78 info
->vector
= pic_read_irq(isa_pic
);
80 info
->vector
= entry
& IOAPIC_VECTOR_MASK
;
83 info
->addr
= APIC_DEFAULT_ADDRESS
| \
84 (info
->dest_idx
<< MSI_ADDR_DEST_IDX_SHIFT
) | \
85 (info
->dest_mode
<< MSI_ADDR_DEST_MODE_SHIFT
);
86 info
->data
= (info
->vector
<< MSI_DATA_VECTOR_SHIFT
) | \
87 (info
->trig_mode
<< MSI_DATA_TRIGGER_SHIFT
) | \
88 (info
->delivery_mode
<< MSI_DATA_DELIVERY_MODE_SHIFT
);
91 static void ioapic_service(IOAPICCommonState
*s
)
93 AddressSpace
*ioapic_as
= X86_MACHINE(qdev_get_machine())->ioapic_as
;
94 struct ioapic_entry_info info
;
99 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
104 entry
= s
->ioredtbl
[i
];
105 ioapic_entry_parse(entry
, &info
);
107 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
110 coalesce
= s
->ioredtbl
[i
] & IOAPIC_LVT_REMOTE_IRR
;
111 trace_ioapic_set_remote_irr(i
);
112 s
->ioredtbl
[i
] |= IOAPIC_LVT_REMOTE_IRR
;
116 /* We are level triggered interrupts, and the
117 * guest should be still working on previous one,
123 if (kvm_irqchip_is_split()) {
124 if (info
.trig_mode
== IOAPIC_TRIGGER_EDGE
) {
125 kvm_set_irq(kvm_state
, i
, 1);
126 kvm_set_irq(kvm_state
, i
, 0);
128 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 #define SUCCESSIVE_IRQ_MAX_COUNT 10000
146 static void delayed_ioapic_service_cb(void *opaque
)
148 IOAPICCommonState
*s
= opaque
;
153 static void ioapic_set_irq(void *opaque
, int vector
, int level
)
155 IOAPICCommonState
*s
= opaque
;
157 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
158 * to GSI 2. GSI maps to ioapic 1-1. This is not
159 * the cleanest way of doing it but it should work. */
161 trace_ioapic_set_irq(vector
, level
);
162 ioapic_stat_update_irq(s
, vector
, level
);
166 if (vector
< IOAPIC_NUM_PINS
) {
167 uint32_t mask
= 1 << vector
;
168 uint64_t entry
= s
->ioredtbl
[vector
];
170 if (((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) ==
171 IOAPIC_TRIGGER_LEVEL
) {
172 /* level triggered */
175 if (!(entry
& IOAPIC_LVT_REMOTE_IRR
)) {
182 /* According to the 82093AA manual, we must ignore edge requests
183 * if the input pin is masked. */
184 if (level
&& !(entry
& IOAPIC_LVT_MASKED
)) {
192 static void ioapic_update_kvm_routes(IOAPICCommonState
*s
)
197 if (kvm_irqchip_is_split()) {
198 for (i
= 0; i
< IOAPIC_NUM_PINS
; i
++) {
200 struct ioapic_entry_info info
;
201 ioapic_entry_parse(s
->ioredtbl
[i
], &info
);
203 msg
.address
= info
.addr
;
204 msg
.data
= info
.data
;
205 kvm_irqchip_update_msi_route(kvm_state
, i
, msg
, NULL
);
208 kvm_irqchip_commit_routes(kvm_state
);
214 static void ioapic_iec_notifier(void *private, bool global
,
215 uint32_t index
, uint32_t mask
)
217 IOAPICCommonState
*s
= (IOAPICCommonState
*)private;
218 /* For simplicity, we just update all the routes */
219 ioapic_update_kvm_routes(s
);
223 void ioapic_eoi_broadcast(int vector
)
225 IOAPICCommonState
*s
;
229 trace_ioapic_eoi_broadcast(vector
);
231 for (i
= 0; i
< MAX_IOAPICS
; i
++) {
236 for (n
= 0; n
< IOAPIC_NUM_PINS
; n
++) {
237 entry
= s
->ioredtbl
[n
];
239 if ((entry
& IOAPIC_VECTOR_MASK
) != vector
||
240 ((entry
>> IOAPIC_LVT_TRIGGER_MODE_SHIFT
) & 1) != IOAPIC_TRIGGER_LEVEL
) {
246 * When IOAPIC is in the userspace while APIC is still in
247 * the kernel (i.e., split irqchip), we have a trick to
248 * kick the resamplefd logic for registered irqfds from
249 * userspace to deactivate the IRQ. When that happens, it
250 * means the irq bypassed userspace IOAPIC (so the irr and
251 * remote-irr of the table entry should be bypassed too
252 * even if interrupt come). Still kick the resamplefds if
253 * they're bound to the IRQ, to make sure to EOI the
254 * interrupt for the hardware correctly.
256 * Note: We still need to go through the irr & remote-irr
257 * operations below because we don't know whether there're
258 * emulated devices that are using/sharing the same IRQ.
260 kvm_resample_fd_notify(n
);
263 if (!(entry
& IOAPIC_LVT_REMOTE_IRR
)) {
267 trace_ioapic_clear_remote_irr(n
, vector
);
268 s
->ioredtbl
[n
] = entry
& ~IOAPIC_LVT_REMOTE_IRR
;
270 if (!(entry
& IOAPIC_LVT_MASKED
) && (s
->irr
& (1 << n
))) {
272 if (s
->irq_eoi
[n
] >= SUCCESSIVE_IRQ_MAX_COUNT
) {
274 * Real hardware does not deliver the interrupt immediately
275 * during eoi broadcast, and this lets a buggy guest make
276 * slow progress even if it does not correctly handle a
277 * level-triggered interrupt. Emulate this behavior if we
278 * detect an interrupt storm.
281 timer_mod_anticipate(s
->delayed_ioapic_service_timer
,
282 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
283 NANOSECONDS_PER_SECOND
/ 100);
284 trace_ioapic_eoi_delayed_reassert(n
);
296 ioapic_mem_read(void *opaque
, hwaddr addr
, unsigned int size
)
298 IOAPICCommonState
*s
= opaque
;
305 case IOAPIC_IOREGSEL
:
312 switch (s
->ioregsel
) {
315 val
= s
->id
<< IOAPIC_ID_SHIFT
;
319 ((IOAPIC_NUM_PINS
- 1) << IOAPIC_VER_ENTRIES_SHIFT
);
322 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
323 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
324 if (s
->ioregsel
& 1) {
325 val
= s
->ioredtbl
[index
] >> 32;
327 val
= s
->ioredtbl
[index
] & 0xffffffff;
334 trace_ioapic_mem_read(addr
, s
->ioregsel
, size
, val
);
340 * This is to satisfy the hack in Linux kernel. One hack of it is to
341 * simulate clearing the Remote IRR bit of IOAPIC entry using the
344 * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
345 * Otherwise, we simulate the EOI message manually by changing the trigger
346 * mode to edge and then back to level, with RTE being masked during
349 * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
351 * This is based on the assumption that, Remote IRR bit will be
352 * cleared by IOAPIC hardware when configured as edge-triggered
355 * Without this, level-triggered interrupts in IR mode might fail to
359 ioapic_fix_edge_remote_irr(uint64_t *entry
)
361 if (!(*entry
& IOAPIC_LVT_TRIGGER_MODE
)) {
362 /* Edge-triggered interrupts, make sure remote IRR is zero */
363 *entry
&= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR
);
368 ioapic_mem_write(void *opaque
, hwaddr addr
, uint64_t val
,
371 IOAPICCommonState
*s
= opaque
;
375 trace_ioapic_mem_write(addr
, s
->ioregsel
, size
, val
);
378 case IOAPIC_IOREGSEL
:
385 switch (s
->ioregsel
) {
387 s
->id
= (val
>> IOAPIC_ID_SHIFT
) & IOAPIC_ID_MASK
;
393 index
= (s
->ioregsel
- IOAPIC_REG_REDTBL_BASE
) >> 1;
394 if (index
>= 0 && index
< IOAPIC_NUM_PINS
) {
395 uint64_t ro_bits
= s
->ioredtbl
[index
] & IOAPIC_RO_BITS
;
396 if (s
->ioregsel
& 1) {
397 s
->ioredtbl
[index
] &= 0xffffffff;
398 s
->ioredtbl
[index
] |= (uint64_t)val
<< 32;
400 s
->ioredtbl
[index
] &= ~0xffffffffULL
;
401 s
->ioredtbl
[index
] |= val
;
403 /* restore RO bits */
404 s
->ioredtbl
[index
] &= IOAPIC_RW_BITS
;
405 s
->ioredtbl
[index
] |= ro_bits
;
406 s
->irq_eoi
[index
] = 0;
407 ioapic_fix_edge_remote_irr(&s
->ioredtbl
[index
]);
408 ioapic_update_kvm_routes(s
);
414 /* Explicit EOI is only supported for IOAPIC version 0x20 */
415 if (size
!= 4 || s
->version
!= 0x20) {
418 ioapic_eoi_broadcast(val
);
423 static const MemoryRegionOps ioapic_io_ops
= {
424 .read
= ioapic_mem_read
,
425 .write
= ioapic_mem_write
,
426 .endianness
= DEVICE_NATIVE_ENDIAN
,
429 static void ioapic_machine_done_notify(Notifier
*notifier
, void *data
)
432 IOAPICCommonState
*s
= container_of(notifier
, IOAPICCommonState
,
435 if (kvm_irqchip_is_split()) {
436 X86IOMMUState
*iommu
= x86_iommu_get_default();
438 /* Register this IOAPIC with IOMMU IEC notifier, so that
439 * when there are IR invalidates, we can be notified to
440 * update kernel IR cache. */
441 x86_iommu_iec_register_notifier(iommu
, ioapic_iec_notifier
, s
);
447 #define IOAPIC_VER_DEF 0x20
449 static void ioapic_realize(DeviceState
*dev
, Error
**errp
)
451 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
453 if (s
->version
!= 0x11 && s
->version
!= 0x20) {
454 error_setg(errp
, "IOAPIC only supports version 0x11 or 0x20 "
455 "(default: 0x%x).", IOAPIC_VER_DEF
);
459 memory_region_init_io(&s
->io_memory
, OBJECT(s
), &ioapic_io_ops
, s
,
462 s
->delayed_ioapic_service_timer
=
463 timer_new_ns(QEMU_CLOCK_VIRTUAL
, delayed_ioapic_service_cb
, s
);
465 qdev_init_gpio_in(dev
, ioapic_set_irq
, IOAPIC_NUM_PINS
);
467 ioapics
[ioapic_no
] = s
;
468 s
->machine_done
.notify
= ioapic_machine_done_notify
;
469 qemu_add_machine_init_done_notifier(&s
->machine_done
);
472 static void ioapic_unrealize(DeviceState
*dev
)
474 IOAPICCommonState
*s
= IOAPIC_COMMON(dev
);
476 timer_free(s
->delayed_ioapic_service_timer
);
479 static Property ioapic_properties
[] = {
480 DEFINE_PROP_UINT8("version", IOAPICCommonState
, version
, IOAPIC_VER_DEF
),
481 DEFINE_PROP_END_OF_LIST(),
484 static void ioapic_class_init(ObjectClass
*klass
, void *data
)
486 IOAPICCommonClass
*k
= IOAPIC_COMMON_CLASS(klass
);
487 DeviceClass
*dc
= DEVICE_CLASS(klass
);
489 k
->realize
= ioapic_realize
;
490 k
->unrealize
= ioapic_unrealize
;
492 * If APIC is in kernel, we need to update the kernel cache after
493 * migration, otherwise first 24 gsi routes will be invalid.
495 k
->post_load
= ioapic_update_kvm_routes
;
496 dc
->reset
= ioapic_reset_common
;
497 device_class_set_props(dc
, ioapic_properties
);
500 static const TypeInfo ioapic_info
= {
502 .parent
= TYPE_IOAPIC_COMMON
,
503 .instance_size
= sizeof(IOAPICCommonState
),
504 .class_init
= ioapic_class_init
,
507 static void ioapic_register_types(void)
509 type_register_static(&ioapic_info
);
512 type_init(ioapic_register_types
)