xref: /qemu/hw/intc/ioapic.c (revision 20fd4b7b6d9282fe0cb83601f1821f31bd257458)
1610626afSaliguori /*
2610626afSaliguori  *  ioapic.c IOAPIC emulation logic
3610626afSaliguori  *
4610626afSaliguori  *  Copyright (c) 2004-2005 Fabrice Bellard
5610626afSaliguori  *
6610626afSaliguori  *  Split the ioapic logic from apic.c
7610626afSaliguori  *  Xiantao Zhang <xiantao.zhang@intel.com>
8610626afSaliguori  *
9610626afSaliguori  * This library is free software; you can redistribute it and/or
10610626afSaliguori  * modify it under the terms of the GNU Lesser General Public
11610626afSaliguori  * License as published by the Free Software Foundation; either
12610626afSaliguori  * version 2 of the License, or (at your option) any later version.
13610626afSaliguori  *
14610626afSaliguori  * This library is distributed in the hope that it will be useful,
15610626afSaliguori  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16610626afSaliguori  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17610626afSaliguori  * Lesser General Public License for more details.
18610626afSaliguori  *
19610626afSaliguori  * You should have received a copy of the GNU Lesser General Public
208167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21610626afSaliguori  */
22610626afSaliguori 
23b6a0aa05SPeter Maydell #include "qemu/osdep.h"
24*20fd4b7bSPeter Xu #include "qemu/error-report.h"
256bde8fd6SPavel Butsykin #include "monitor/monitor.h"
2683c9f4caSPaolo Bonzini #include "hw/hw.h"
270d09e41aSPaolo Bonzini #include "hw/i386/pc.h"
28d613f8ccSPaolo Bonzini #include "hw/i386/apic.h"
290d09e41aSPaolo Bonzini #include "hw/i386/ioapic.h"
300d09e41aSPaolo Bonzini #include "hw/i386/ioapic_internal.h"
3115eafc2eSPaolo Bonzini #include "include/hw/pci/msi.h"
3215eafc2eSPaolo Bonzini #include "sysemu/kvm.h"
33cb135f59SPeter Xu #include "target-i386/cpu.h"
34cb135f59SPeter Xu #include "hw/i386/apic-msidef.h"
35e3d9c925SPeter Xu #include "hw/i386/x86-iommu.h"
36610626afSaliguori 
37610626afSaliguori //#define DEBUG_IOAPIC
38610626afSaliguori 
399af9b330SBlue Swirl #ifdef DEBUG_IOAPIC
409af9b330SBlue Swirl #define DPRINTF(fmt, ...)                                       \
419af9b330SBlue Swirl     do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
429af9b330SBlue Swirl #else
439af9b330SBlue Swirl #define DPRINTF(fmt, ...)
449af9b330SBlue Swirl #endif
459af9b330SBlue Swirl 
4615eafc2eSPaolo Bonzini #define APIC_DELIVERY_MODE_SHIFT 8
4715eafc2eSPaolo Bonzini #define APIC_POLARITY_SHIFT 14
4815eafc2eSPaolo Bonzini #define APIC_TRIG_MODE_SHIFT 15
4915eafc2eSPaolo Bonzini 
50244ac3afSJan Kiszka static IOAPICCommonState *ioapics[MAX_IOAPICS];
510280b571SJan Kiszka 
52db0f8888Sxiaoqiang zhao /* global variable from ioapic_common.c */
53db0f8888Sxiaoqiang zhao extern int ioapic_no;
54db0f8888Sxiaoqiang zhao 
55c15fa0beSPeter Xu struct ioapic_entry_info {
56c15fa0beSPeter Xu     /* fields parsed from IOAPIC entries */
57c15fa0beSPeter Xu     uint8_t masked;
58c15fa0beSPeter Xu     uint8_t trig_mode;
59c15fa0beSPeter Xu     uint16_t dest_idx;
60c15fa0beSPeter Xu     uint8_t dest_mode;
61c15fa0beSPeter Xu     uint8_t delivery_mode;
62c15fa0beSPeter Xu     uint8_t vector;
63c15fa0beSPeter Xu 
64c15fa0beSPeter Xu     /* MSI message generated from above parsed fields */
65c15fa0beSPeter Xu     uint32_t addr;
66c15fa0beSPeter Xu     uint32_t data;
67c15fa0beSPeter Xu };
68c15fa0beSPeter Xu 
69c15fa0beSPeter Xu static void ioapic_entry_parse(uint64_t entry, struct ioapic_entry_info *info)
70c15fa0beSPeter Xu {
71c15fa0beSPeter Xu     memset(info, 0, sizeof(*info));
72c15fa0beSPeter Xu     info->masked = (entry >> IOAPIC_LVT_MASKED_SHIFT) & 1;
73c15fa0beSPeter Xu     info->trig_mode = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
74c15fa0beSPeter Xu     /*
75c15fa0beSPeter Xu      * By default, this would be dest_id[8] + reserved[8]. When IR
76c15fa0beSPeter Xu      * is enabled, this would be interrupt_index[15] +
77c15fa0beSPeter Xu      * interrupt_format[1]. This field never means anything, but
78c15fa0beSPeter Xu      * only used to generate corresponding MSI.
79c15fa0beSPeter Xu      */
80c15fa0beSPeter Xu     info->dest_idx = (entry >> IOAPIC_LVT_DEST_IDX_SHIFT) & 0xffff;
81c15fa0beSPeter Xu     info->dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
82c15fa0beSPeter Xu     info->delivery_mode = (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) \
83c15fa0beSPeter Xu         & IOAPIC_DM_MASK;
84c15fa0beSPeter Xu     if (info->delivery_mode == IOAPIC_DM_EXTINT) {
85c15fa0beSPeter Xu         info->vector = pic_read_irq(isa_pic);
86c15fa0beSPeter Xu     } else {
87c15fa0beSPeter Xu         info->vector = entry & IOAPIC_VECTOR_MASK;
88c15fa0beSPeter Xu     }
89c15fa0beSPeter Xu 
90c15fa0beSPeter Xu     info->addr = APIC_DEFAULT_ADDRESS | \
91c15fa0beSPeter Xu         (info->dest_idx << MSI_ADDR_DEST_IDX_SHIFT) | \
92c15fa0beSPeter Xu         (info->dest_mode << MSI_ADDR_DEST_MODE_SHIFT);
93c15fa0beSPeter Xu     info->data = (info->vector << MSI_DATA_VECTOR_SHIFT) | \
94c15fa0beSPeter Xu         (info->trig_mode << MSI_DATA_TRIGGER_SHIFT) | \
95c15fa0beSPeter Xu         (info->delivery_mode << MSI_DATA_DELIVERY_MODE_SHIFT);
96c15fa0beSPeter Xu }
97c15fa0beSPeter Xu 
98244ac3afSJan Kiszka static void ioapic_service(IOAPICCommonState *s)
99610626afSaliguori {
100cb135f59SPeter Xu     AddressSpace *ioapic_as = PC_MACHINE(qdev_get_machine())->ioapic_as;
101c15fa0beSPeter Xu     struct ioapic_entry_info info;
102610626afSaliguori     uint8_t i;
103610626afSaliguori     uint32_t mask;
104610626afSaliguori     uint64_t entry;
105610626afSaliguori 
106610626afSaliguori     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
107610626afSaliguori         mask = 1 << i;
108610626afSaliguori         if (s->irr & mask) {
10915eafc2eSPaolo Bonzini             int coalesce = 0;
11015eafc2eSPaolo Bonzini 
111610626afSaliguori             entry = s->ioredtbl[i];
112c15fa0beSPeter Xu             ioapic_entry_parse(entry, &info);
113c15fa0beSPeter Xu             if (!info.masked) {
114c15fa0beSPeter Xu                 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
115610626afSaliguori                     s->irr &= ~mask;
1160280b571SJan Kiszka                 } else {
11715eafc2eSPaolo Bonzini                     coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
1180280b571SJan Kiszka                     s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
1190280b571SJan Kiszka                 }
120c15fa0beSPeter Xu 
121f99b86b9SPeter Xu                 if (coalesce) {
122f99b86b9SPeter Xu                     /* We are level triggered interrupts, and the
123f99b86b9SPeter Xu                      * guest should be still working on previous one,
124f99b86b9SPeter Xu                      * so skip it. */
125f99b86b9SPeter Xu                     continue;
126f99b86b9SPeter Xu                 }
127f99b86b9SPeter Xu 
12815eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
12915eafc2eSPaolo Bonzini                 if (kvm_irqchip_is_split()) {
130c15fa0beSPeter Xu                     if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
13115eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
13215eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 0);
13315eafc2eSPaolo Bonzini                     } else {
13415eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
13515eafc2eSPaolo Bonzini                     }
13615eafc2eSPaolo Bonzini                     continue;
13715eafc2eSPaolo Bonzini                 }
13815eafc2eSPaolo Bonzini #endif
139f99b86b9SPeter Xu 
140cb135f59SPeter Xu                 /* No matter whether IR is enabled, we translate
141cb135f59SPeter Xu                  * the IOAPIC message into a MSI one, and its
142cb135f59SPeter Xu                  * address space will decide whether we need a
143cb135f59SPeter Xu                  * translation. */
144c15fa0beSPeter Xu                 stl_le_phys(ioapic_as, info.addr, info.data);
145610626afSaliguori             }
146610626afSaliguori         }
147610626afSaliguori     }
148610626afSaliguori }
149610626afSaliguori 
1507d0500c4SBlue Swirl static void ioapic_set_irq(void *opaque, int vector, int level)
151610626afSaliguori {
152244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
153610626afSaliguori 
154610626afSaliguori     /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
155610626afSaliguori      * to GSI 2.  GSI maps to ioapic 1-1.  This is not
156610626afSaliguori      * the cleanest way of doing it but it should work. */
157610626afSaliguori 
1589af9b330SBlue Swirl     DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
1591f5e71a8SJan Kiszka     if (vector == 0) {
160610626afSaliguori         vector = 2;
1611f5e71a8SJan Kiszka     }
162610626afSaliguori     if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
163610626afSaliguori         uint32_t mask = 1 << vector;
164610626afSaliguori         uint64_t entry = s->ioredtbl[vector];
165610626afSaliguori 
1661f5e71a8SJan Kiszka         if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
1671f5e71a8SJan Kiszka             IOAPIC_TRIGGER_LEVEL) {
168610626afSaliguori             /* level triggered */
169610626afSaliguori             if (level) {
170610626afSaliguori                 s->irr |= mask;
171c5955a56SPaolo Bonzini                 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
172610626afSaliguori                     ioapic_service(s);
173c5955a56SPaolo Bonzini                 }
174610626afSaliguori             } else {
175610626afSaliguori                 s->irr &= ~mask;
176610626afSaliguori             }
177610626afSaliguori         } else {
17847f7be39SJan Kiszka             /* According to the 82093AA manual, we must ignore edge requests
17947f7be39SJan Kiszka              * if the input pin is masked. */
18047f7be39SJan Kiszka             if (level && !(entry & IOAPIC_LVT_MASKED)) {
181610626afSaliguori                 s->irr |= mask;
182610626afSaliguori                 ioapic_service(s);
183610626afSaliguori             }
184610626afSaliguori         }
185610626afSaliguori     }
186610626afSaliguori }
187610626afSaliguori 
18815eafc2eSPaolo Bonzini static void ioapic_update_kvm_routes(IOAPICCommonState *s)
18915eafc2eSPaolo Bonzini {
19015eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
19115eafc2eSPaolo Bonzini     int i;
19215eafc2eSPaolo Bonzini 
19315eafc2eSPaolo Bonzini     if (kvm_irqchip_is_split()) {
19415eafc2eSPaolo Bonzini         for (i = 0; i < IOAPIC_NUM_PINS; i++) {
19515eafc2eSPaolo Bonzini             MSIMessage msg;
196c15fa0beSPeter Xu             struct ioapic_entry_info info;
197c15fa0beSPeter Xu             ioapic_entry_parse(s->ioredtbl[i], &info);
198c15fa0beSPeter Xu             msg.address = info.addr;
199c15fa0beSPeter Xu             msg.data = info.data;
20015eafc2eSPaolo Bonzini             kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
20115eafc2eSPaolo Bonzini         }
20215eafc2eSPaolo Bonzini         kvm_irqchip_commit_routes(kvm_state);
20315eafc2eSPaolo Bonzini     }
20415eafc2eSPaolo Bonzini #endif
20515eafc2eSPaolo Bonzini }
20615eafc2eSPaolo Bonzini 
207e3d9c925SPeter Xu #ifdef CONFIG_KVM
208e3d9c925SPeter Xu static void ioapic_iec_notifier(void *private, bool global,
209e3d9c925SPeter Xu                                 uint32_t index, uint32_t mask)
210e3d9c925SPeter Xu {
211e3d9c925SPeter Xu     IOAPICCommonState *s = (IOAPICCommonState *)private;
212e3d9c925SPeter Xu     /* For simplicity, we just update all the routes */
213e3d9c925SPeter Xu     ioapic_update_kvm_routes(s);
214e3d9c925SPeter Xu }
215e3d9c925SPeter Xu #endif
216e3d9c925SPeter Xu 
2170280b571SJan Kiszka void ioapic_eoi_broadcast(int vector)
2180280b571SJan Kiszka {
219244ac3afSJan Kiszka     IOAPICCommonState *s;
2200280b571SJan Kiszka     uint64_t entry;
2210280b571SJan Kiszka     int i, n;
2220280b571SJan Kiszka 
2230280b571SJan Kiszka     for (i = 0; i < MAX_IOAPICS; i++) {
2240280b571SJan Kiszka         s = ioapics[i];
2250280b571SJan Kiszka         if (!s) {
2260280b571SJan Kiszka             continue;
2270280b571SJan Kiszka         }
2280280b571SJan Kiszka         for (n = 0; n < IOAPIC_NUM_PINS; n++) {
2290280b571SJan Kiszka             entry = s->ioredtbl[n];
2301f5e71a8SJan Kiszka             if ((entry & IOAPIC_LVT_REMOTE_IRR)
2311f5e71a8SJan Kiszka                 && (entry & IOAPIC_VECTOR_MASK) == vector) {
2320280b571SJan Kiszka                 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
2330280b571SJan Kiszka                 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
2340280b571SJan Kiszka                     ioapic_service(s);
2350280b571SJan Kiszka                 }
2360280b571SJan Kiszka             }
2370280b571SJan Kiszka         }
2380280b571SJan Kiszka     }
2390280b571SJan Kiszka }
2400280b571SJan Kiszka 
2416bde8fd6SPavel Butsykin void ioapic_dump_state(Monitor *mon, const QDict *qdict)
2426bde8fd6SPavel Butsykin {
2436bde8fd6SPavel Butsykin     int i;
2446bde8fd6SPavel Butsykin 
2456bde8fd6SPavel Butsykin     for (i = 0; i < MAX_IOAPICS; i++) {
2466bde8fd6SPavel Butsykin         if (ioapics[i] != 0) {
2476bde8fd6SPavel Butsykin             ioapic_print_redtbl(mon, ioapics[i]);
2486bde8fd6SPavel Butsykin         }
2496bde8fd6SPavel Butsykin     }
2506bde8fd6SPavel Butsykin }
2516bde8fd6SPavel Butsykin 
2524d5bf5f6SJan Kiszka static uint64_t
253a8170e5eSAvi Kivity ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
254610626afSaliguori {
255244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
256610626afSaliguori     int index;
257610626afSaliguori     uint32_t val = 0;
258610626afSaliguori 
2591f5e71a8SJan Kiszka     switch (addr & 0xff) {
2601f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
261610626afSaliguori         val = s->ioregsel;
2621f5e71a8SJan Kiszka         break;
2631f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
2641a440963SJan Kiszka         if (size != 4) {
2651a440963SJan Kiszka             break;
2661a440963SJan Kiszka         }
267610626afSaliguori         switch (s->ioregsel) {
2681f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
2692f5a3b12SPaolo Bonzini         case IOAPIC_REG_ARB:
2701f5e71a8SJan Kiszka             val = s->id << IOAPIC_ID_SHIFT;
271610626afSaliguori             break;
2721f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
273*20fd4b7bSPeter Xu             val = s->version |
2741f5e71a8SJan Kiszka                 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
275610626afSaliguori             break;
276610626afSaliguori         default:
2771f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
278610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
2791f5e71a8SJan Kiszka                 if (s->ioregsel & 1) {
280610626afSaliguori                     val = s->ioredtbl[index] >> 32;
2811f5e71a8SJan Kiszka                 } else {
282610626afSaliguori                     val = s->ioredtbl[index] & 0xffffffff;
283610626afSaliguori                 }
284610626afSaliguori             }
2851f5e71a8SJan Kiszka         }
2869af9b330SBlue Swirl         DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
2871f5e71a8SJan Kiszka         break;
288610626afSaliguori     }
289610626afSaliguori     return val;
290610626afSaliguori }
291610626afSaliguori 
292ed1263c3SPeter Xu /*
293ed1263c3SPeter Xu  * This is to satisfy the hack in Linux kernel. One hack of it is to
294ed1263c3SPeter Xu  * simulate clearing the Remote IRR bit of IOAPIC entry using the
295ed1263c3SPeter Xu  * following:
296ed1263c3SPeter Xu  *
297ed1263c3SPeter Xu  * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
298ed1263c3SPeter Xu  * Otherwise, we simulate the EOI message manually by changing the trigger
299ed1263c3SPeter Xu  * mode to edge and then back to level, with RTE being masked during
300ed1263c3SPeter Xu  * this."
301ed1263c3SPeter Xu  *
302ed1263c3SPeter Xu  * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
303ed1263c3SPeter Xu  *
304ed1263c3SPeter Xu  * This is based on the assumption that, Remote IRR bit will be
305ed1263c3SPeter Xu  * cleared by IOAPIC hardware when configured as edge-triggered
306ed1263c3SPeter Xu  * interrupts.
307ed1263c3SPeter Xu  *
308ed1263c3SPeter Xu  * Without this, level-triggered interrupts in IR mode might fail to
309ed1263c3SPeter Xu  * work correctly.
310ed1263c3SPeter Xu  */
311ed1263c3SPeter Xu static inline void
312ed1263c3SPeter Xu ioapic_fix_edge_remote_irr(uint64_t *entry)
313ed1263c3SPeter Xu {
314ed1263c3SPeter Xu     if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
315ed1263c3SPeter Xu         /* Edge-triggered interrupts, make sure remote IRR is zero */
316ed1263c3SPeter Xu         *entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
317ed1263c3SPeter Xu     }
318ed1263c3SPeter Xu }
319ed1263c3SPeter Xu 
3201f5e71a8SJan Kiszka static void
321a8170e5eSAvi Kivity ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
3224d5bf5f6SJan Kiszka                  unsigned int size)
323610626afSaliguori {
324244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
325610626afSaliguori     int index;
326610626afSaliguori 
3271f5e71a8SJan Kiszka     switch (addr & 0xff) {
3281f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
329610626afSaliguori         s->ioregsel = val;
3301f5e71a8SJan Kiszka         break;
3311f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
3321a440963SJan Kiszka         if (size != 4) {
3331a440963SJan Kiszka             break;
3341a440963SJan Kiszka         }
3350c1f781bSJason Wang         DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
336610626afSaliguori         switch (s->ioregsel) {
3371f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
3381f5e71a8SJan Kiszka             s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
3391f5e71a8SJan Kiszka             break;
3401f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
3411f5e71a8SJan Kiszka         case IOAPIC_REG_ARB:
3421f5e71a8SJan Kiszka             break;
343610626afSaliguori         default:
3441f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
345610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
346479c2a1cSPeter Xu                 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
347610626afSaliguori                 if (s->ioregsel & 1) {
348610626afSaliguori                     s->ioredtbl[index] &= 0xffffffff;
349610626afSaliguori                     s->ioredtbl[index] |= (uint64_t)val << 32;
350610626afSaliguori                 } else {
351610626afSaliguori                     s->ioredtbl[index] &= ~0xffffffffULL;
352610626afSaliguori                     s->ioredtbl[index] |= val;
353610626afSaliguori                 }
354479c2a1cSPeter Xu                 /* restore RO bits */
355479c2a1cSPeter Xu                 s->ioredtbl[index] &= IOAPIC_RW_BITS;
356479c2a1cSPeter Xu                 s->ioredtbl[index] |= ro_bits;
357ed1263c3SPeter Xu                 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
358610626afSaliguori                 ioapic_service(s);
359610626afSaliguori             }
360610626afSaliguori         }
3611f5e71a8SJan Kiszka         break;
362*20fd4b7bSPeter Xu     case IOAPIC_EOI:
363*20fd4b7bSPeter Xu         /* Explicit EOI is only supported for IOAPIC version 0x20 */
364*20fd4b7bSPeter Xu         if (size != 4 || s->version != 0x20) {
365*20fd4b7bSPeter Xu             break;
366*20fd4b7bSPeter Xu         }
367*20fd4b7bSPeter Xu         ioapic_eoi_broadcast(val);
368*20fd4b7bSPeter Xu         break;
369610626afSaliguori     }
37015eafc2eSPaolo Bonzini 
37115eafc2eSPaolo Bonzini     ioapic_update_kvm_routes(s);
372610626afSaliguori }
373610626afSaliguori 
3744d5bf5f6SJan Kiszka static const MemoryRegionOps ioapic_io_ops = {
3754d5bf5f6SJan Kiszka     .read = ioapic_mem_read,
3764d5bf5f6SJan Kiszka     .write = ioapic_mem_write,
3774d5bf5f6SJan Kiszka     .endianness = DEVICE_NATIVE_ENDIAN,
378610626afSaliguori };
379610626afSaliguori 
380e3d9c925SPeter Xu static void ioapic_machine_done_notify(Notifier *notifier, void *data)
381e3d9c925SPeter Xu {
382e3d9c925SPeter Xu #ifdef CONFIG_KVM
383e3d9c925SPeter Xu     IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
384e3d9c925SPeter Xu                                         machine_done);
385e3d9c925SPeter Xu 
386e3d9c925SPeter Xu     if (kvm_irqchip_is_split()) {
387e3d9c925SPeter Xu         X86IOMMUState *iommu = x86_iommu_get_default();
388e3d9c925SPeter Xu         if (iommu) {
389e3d9c925SPeter Xu             /* Register this IOAPIC with IOMMU IEC notifier, so that
390e3d9c925SPeter Xu              * when there are IR invalidates, we can be notified to
391e3d9c925SPeter Xu              * update kernel IR cache. */
392e3d9c925SPeter Xu             x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
393e3d9c925SPeter Xu         }
394e3d9c925SPeter Xu     }
395e3d9c925SPeter Xu #endif
396e3d9c925SPeter Xu }
397e3d9c925SPeter Xu 
398db0f8888Sxiaoqiang zhao static void ioapic_realize(DeviceState *dev, Error **errp)
399610626afSaliguori {
400db0f8888Sxiaoqiang zhao     IOAPICCommonState *s = IOAPIC_COMMON(dev);
401f9771858Sxiaoqiang zhao 
402*20fd4b7bSPeter Xu     if (s->version != 0x11 && s->version != 0x20) {
403*20fd4b7bSPeter Xu         error_report("IOAPIC only supports version 0x11 or 0x20 "
404*20fd4b7bSPeter Xu                      "(default: 0x11).");
405*20fd4b7bSPeter Xu         exit(1);
406*20fd4b7bSPeter Xu     }
407*20fd4b7bSPeter Xu 
4081437c94bSPaolo Bonzini     memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
4091437c94bSPaolo Bonzini                           "ioapic", 0x1000);
410610626afSaliguori 
411f9771858Sxiaoqiang zhao     qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
412610626afSaliguori 
413db0f8888Sxiaoqiang zhao     ioapics[ioapic_no] = s;
414e3d9c925SPeter Xu     s->machine_done.notify = ioapic_machine_done_notify;
415e3d9c925SPeter Xu     qemu_add_machine_init_done_notifier(&s->machine_done);
416610626afSaliguori }
41796051119SBlue Swirl 
418*20fd4b7bSPeter Xu static Property ioapic_properties[] = {
419*20fd4b7bSPeter Xu     DEFINE_PROP_UINT8("version", IOAPICCommonState, version, 0x11),
420*20fd4b7bSPeter Xu     DEFINE_PROP_END_OF_LIST(),
421*20fd4b7bSPeter Xu };
422*20fd4b7bSPeter Xu 
423999e12bbSAnthony Liguori static void ioapic_class_init(ObjectClass *klass, void *data)
424999e12bbSAnthony Liguori {
425999e12bbSAnthony Liguori     IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
42639bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
427999e12bbSAnthony Liguori 
428db0f8888Sxiaoqiang zhao     k->realize = ioapic_realize;
42939bffca2SAnthony Liguori     dc->reset = ioapic_reset_common;
430*20fd4b7bSPeter Xu     dc->props = ioapic_properties;
431999e12bbSAnthony Liguori }
432999e12bbSAnthony Liguori 
4338c43a6f0SAndreas Färber static const TypeInfo ioapic_info = {
434999e12bbSAnthony Liguori     .name          = "ioapic",
43539bffca2SAnthony Liguori     .parent        = TYPE_IOAPIC_COMMON,
43639bffca2SAnthony Liguori     .instance_size = sizeof(IOAPICCommonState),
437999e12bbSAnthony Liguori     .class_init    = ioapic_class_init,
43896051119SBlue Swirl };
43996051119SBlue Swirl 
44083f7d43aSAndreas Färber static void ioapic_register_types(void)
44196051119SBlue Swirl {
44239bffca2SAnthony Liguori     type_register_static(&ioapic_info);
44396051119SBlue Swirl }
44496051119SBlue Swirl 
44583f7d43aSAndreas Färber type_init(ioapic_register_types)
446