xref: /qemu/hw/intc/ioapic.c (revision a2e6ffab97e57f823b6bf15371920c6901bb7cbc)
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"
2420fd4b7bSPeter 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"
33fcf5ef2aSThomas Huth #include "target/i386/cpu.h"
34cb135f59SPeter Xu #include "hw/i386/apic-msidef.h"
35e3d9c925SPeter Xu #include "hw/i386/x86-iommu.h"
36e5074b38SPeter Xu #include "trace.h"
37610626afSaliguori 
3815eafc2eSPaolo Bonzini #define APIC_DELIVERY_MODE_SHIFT 8
3915eafc2eSPaolo Bonzini #define APIC_POLARITY_SHIFT 14
4015eafc2eSPaolo Bonzini #define APIC_TRIG_MODE_SHIFT 15
4115eafc2eSPaolo Bonzini 
42244ac3afSJan Kiszka static IOAPICCommonState *ioapics[MAX_IOAPICS];
430280b571SJan Kiszka 
44db0f8888Sxiaoqiang zhao /* global variable from ioapic_common.c */
45db0f8888Sxiaoqiang zhao extern int ioapic_no;
46db0f8888Sxiaoqiang zhao 
47c15fa0beSPeter Xu struct ioapic_entry_info {
48c15fa0beSPeter Xu     /* fields parsed from IOAPIC entries */
49c15fa0beSPeter Xu     uint8_t masked;
50c15fa0beSPeter Xu     uint8_t trig_mode;
51c15fa0beSPeter Xu     uint16_t dest_idx;
52c15fa0beSPeter Xu     uint8_t dest_mode;
53c15fa0beSPeter Xu     uint8_t delivery_mode;
54c15fa0beSPeter Xu     uint8_t vector;
55c15fa0beSPeter Xu 
56c15fa0beSPeter Xu     /* MSI message generated from above parsed fields */
57c15fa0beSPeter Xu     uint32_t addr;
58c15fa0beSPeter Xu     uint32_t data;
59c15fa0beSPeter Xu };
60c15fa0beSPeter Xu 
61c15fa0beSPeter Xu static void ioapic_entry_parse(uint64_t entry, struct ioapic_entry_info *info)
62c15fa0beSPeter Xu {
63c15fa0beSPeter Xu     memset(info, 0, sizeof(*info));
64c15fa0beSPeter Xu     info->masked = (entry >> IOAPIC_LVT_MASKED_SHIFT) & 1;
65c15fa0beSPeter Xu     info->trig_mode = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
66c15fa0beSPeter Xu     /*
67c15fa0beSPeter Xu      * By default, this would be dest_id[8] + reserved[8]. When IR
68c15fa0beSPeter Xu      * is enabled, this would be interrupt_index[15] +
69c15fa0beSPeter Xu      * interrupt_format[1]. This field never means anything, but
70c15fa0beSPeter Xu      * only used to generate corresponding MSI.
71c15fa0beSPeter Xu      */
72c15fa0beSPeter Xu     info->dest_idx = (entry >> IOAPIC_LVT_DEST_IDX_SHIFT) & 0xffff;
73c15fa0beSPeter Xu     info->dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
74c15fa0beSPeter Xu     info->delivery_mode = (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) \
75c15fa0beSPeter Xu         & IOAPIC_DM_MASK;
76c15fa0beSPeter Xu     if (info->delivery_mode == IOAPIC_DM_EXTINT) {
77c15fa0beSPeter Xu         info->vector = pic_read_irq(isa_pic);
78c15fa0beSPeter Xu     } else {
79c15fa0beSPeter Xu         info->vector = entry & IOAPIC_VECTOR_MASK;
80c15fa0beSPeter Xu     }
81c15fa0beSPeter Xu 
82c15fa0beSPeter Xu     info->addr = APIC_DEFAULT_ADDRESS | \
83c15fa0beSPeter Xu         (info->dest_idx << MSI_ADDR_DEST_IDX_SHIFT) | \
84c15fa0beSPeter Xu         (info->dest_mode << MSI_ADDR_DEST_MODE_SHIFT);
85c15fa0beSPeter Xu     info->data = (info->vector << MSI_DATA_VECTOR_SHIFT) | \
86c15fa0beSPeter Xu         (info->trig_mode << MSI_DATA_TRIGGER_SHIFT) | \
87c15fa0beSPeter Xu         (info->delivery_mode << MSI_DATA_DELIVERY_MODE_SHIFT);
88c15fa0beSPeter Xu }
89c15fa0beSPeter Xu 
90244ac3afSJan Kiszka static void ioapic_service(IOAPICCommonState *s)
91610626afSaliguori {
92cb135f59SPeter Xu     AddressSpace *ioapic_as = PC_MACHINE(qdev_get_machine())->ioapic_as;
93c15fa0beSPeter Xu     struct ioapic_entry_info info;
94610626afSaliguori     uint8_t i;
95610626afSaliguori     uint32_t mask;
96610626afSaliguori     uint64_t entry;
97610626afSaliguori 
98610626afSaliguori     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
99610626afSaliguori         mask = 1 << i;
100610626afSaliguori         if (s->irr & mask) {
10115eafc2eSPaolo Bonzini             int coalesce = 0;
10215eafc2eSPaolo Bonzini 
103610626afSaliguori             entry = s->ioredtbl[i];
104c15fa0beSPeter Xu             ioapic_entry_parse(entry, &info);
105c15fa0beSPeter Xu             if (!info.masked) {
106c15fa0beSPeter Xu                 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
107610626afSaliguori                     s->irr &= ~mask;
1080280b571SJan Kiszka                 } else {
10915eafc2eSPaolo Bonzini                     coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
110e5074b38SPeter Xu                     trace_ioapic_set_remote_irr(i);
1110280b571SJan Kiszka                     s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
1120280b571SJan Kiszka                 }
113c15fa0beSPeter Xu 
114f99b86b9SPeter Xu                 if (coalesce) {
115f99b86b9SPeter Xu                     /* We are level triggered interrupts, and the
116f99b86b9SPeter Xu                      * guest should be still working on previous one,
117f99b86b9SPeter Xu                      * so skip it. */
118f99b86b9SPeter Xu                     continue;
119f99b86b9SPeter Xu                 }
120f99b86b9SPeter Xu 
12115eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
12215eafc2eSPaolo Bonzini                 if (kvm_irqchip_is_split()) {
123c15fa0beSPeter Xu                     if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
12415eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
12515eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 0);
12615eafc2eSPaolo Bonzini                     } else {
12715eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
12815eafc2eSPaolo Bonzini                     }
12915eafc2eSPaolo Bonzini                     continue;
13015eafc2eSPaolo Bonzini                 }
13115eafc2eSPaolo Bonzini #endif
132f99b86b9SPeter Xu 
133cb135f59SPeter Xu                 /* No matter whether IR is enabled, we translate
134cb135f59SPeter Xu                  * the IOAPIC message into a MSI one, and its
135cb135f59SPeter Xu                  * address space will decide whether we need a
136cb135f59SPeter Xu                  * translation. */
137c15fa0beSPeter Xu                 stl_le_phys(ioapic_as, info.addr, info.data);
138610626afSaliguori             }
139610626afSaliguori         }
140610626afSaliguori     }
141610626afSaliguori }
142610626afSaliguori 
1437d0500c4SBlue Swirl static void ioapic_set_irq(void *opaque, int vector, int level)
144610626afSaliguori {
145244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
146610626afSaliguori 
147610626afSaliguori     /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
148610626afSaliguori      * to GSI 2.  GSI maps to ioapic 1-1.  This is not
149610626afSaliguori      * the cleanest way of doing it but it should work. */
150610626afSaliguori 
151*a2e6ffabSDr. David Alan Gilbert     trace_ioapic_set_irq(vector, level);
1521f5e71a8SJan Kiszka     if (vector == 0) {
153610626afSaliguori         vector = 2;
1541f5e71a8SJan Kiszka     }
155610626afSaliguori     if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
156610626afSaliguori         uint32_t mask = 1 << vector;
157610626afSaliguori         uint64_t entry = s->ioredtbl[vector];
158610626afSaliguori 
1591f5e71a8SJan Kiszka         if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
1601f5e71a8SJan Kiszka             IOAPIC_TRIGGER_LEVEL) {
161610626afSaliguori             /* level triggered */
162610626afSaliguori             if (level) {
163610626afSaliguori                 s->irr |= mask;
164c5955a56SPaolo Bonzini                 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
165610626afSaliguori                     ioapic_service(s);
166c5955a56SPaolo Bonzini                 }
167610626afSaliguori             } else {
168610626afSaliguori                 s->irr &= ~mask;
169610626afSaliguori             }
170610626afSaliguori         } else {
17147f7be39SJan Kiszka             /* According to the 82093AA manual, we must ignore edge requests
17247f7be39SJan Kiszka              * if the input pin is masked. */
17347f7be39SJan Kiszka             if (level && !(entry & IOAPIC_LVT_MASKED)) {
174610626afSaliguori                 s->irr |= mask;
175610626afSaliguori                 ioapic_service(s);
176610626afSaliguori             }
177610626afSaliguori         }
178610626afSaliguori     }
179610626afSaliguori }
180610626afSaliguori 
18115eafc2eSPaolo Bonzini static void ioapic_update_kvm_routes(IOAPICCommonState *s)
18215eafc2eSPaolo Bonzini {
18315eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
18415eafc2eSPaolo Bonzini     int i;
18515eafc2eSPaolo Bonzini 
18615eafc2eSPaolo Bonzini     if (kvm_irqchip_is_split()) {
18715eafc2eSPaolo Bonzini         for (i = 0; i < IOAPIC_NUM_PINS; i++) {
18815eafc2eSPaolo Bonzini             MSIMessage msg;
189c15fa0beSPeter Xu             struct ioapic_entry_info info;
190c15fa0beSPeter Xu             ioapic_entry_parse(s->ioredtbl[i], &info);
191c15fa0beSPeter Xu             msg.address = info.addr;
192c15fa0beSPeter Xu             msg.data = info.data;
19315eafc2eSPaolo Bonzini             kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
19415eafc2eSPaolo Bonzini         }
19515eafc2eSPaolo Bonzini         kvm_irqchip_commit_routes(kvm_state);
19615eafc2eSPaolo Bonzini     }
19715eafc2eSPaolo Bonzini #endif
19815eafc2eSPaolo Bonzini }
19915eafc2eSPaolo Bonzini 
200e3d9c925SPeter Xu #ifdef CONFIG_KVM
201e3d9c925SPeter Xu static void ioapic_iec_notifier(void *private, bool global,
202e3d9c925SPeter Xu                                 uint32_t index, uint32_t mask)
203e3d9c925SPeter Xu {
204e3d9c925SPeter Xu     IOAPICCommonState *s = (IOAPICCommonState *)private;
205e3d9c925SPeter Xu     /* For simplicity, we just update all the routes */
206e3d9c925SPeter Xu     ioapic_update_kvm_routes(s);
207e3d9c925SPeter Xu }
208e3d9c925SPeter Xu #endif
209e3d9c925SPeter Xu 
2100280b571SJan Kiszka void ioapic_eoi_broadcast(int vector)
2110280b571SJan Kiszka {
212244ac3afSJan Kiszka     IOAPICCommonState *s;
2130280b571SJan Kiszka     uint64_t entry;
2140280b571SJan Kiszka     int i, n;
2150280b571SJan Kiszka 
216e5074b38SPeter Xu     trace_ioapic_eoi_broadcast(vector);
217e5074b38SPeter Xu 
2180280b571SJan Kiszka     for (i = 0; i < MAX_IOAPICS; i++) {
2190280b571SJan Kiszka         s = ioapics[i];
2200280b571SJan Kiszka         if (!s) {
2210280b571SJan Kiszka             continue;
2220280b571SJan Kiszka         }
2230280b571SJan Kiszka         for (n = 0; n < IOAPIC_NUM_PINS; n++) {
2240280b571SJan Kiszka             entry = s->ioredtbl[n];
2251f5e71a8SJan Kiszka             if ((entry & IOAPIC_LVT_REMOTE_IRR)
2261f5e71a8SJan Kiszka                 && (entry & IOAPIC_VECTOR_MASK) == vector) {
227e5074b38SPeter Xu                 trace_ioapic_clear_remote_irr(n, vector);
2280280b571SJan Kiszka                 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
2290280b571SJan Kiszka                 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
2300280b571SJan Kiszka                     ioapic_service(s);
2310280b571SJan Kiszka                 }
2320280b571SJan Kiszka             }
2330280b571SJan Kiszka         }
2340280b571SJan Kiszka     }
2350280b571SJan Kiszka }
2360280b571SJan Kiszka 
2376bde8fd6SPavel Butsykin void ioapic_dump_state(Monitor *mon, const QDict *qdict)
2386bde8fd6SPavel Butsykin {
2396bde8fd6SPavel Butsykin     int i;
2406bde8fd6SPavel Butsykin 
2416bde8fd6SPavel Butsykin     for (i = 0; i < MAX_IOAPICS; i++) {
2426bde8fd6SPavel Butsykin         if (ioapics[i] != 0) {
2436bde8fd6SPavel Butsykin             ioapic_print_redtbl(mon, ioapics[i]);
2446bde8fd6SPavel Butsykin         }
2456bde8fd6SPavel Butsykin     }
2466bde8fd6SPavel Butsykin }
2476bde8fd6SPavel Butsykin 
2484d5bf5f6SJan Kiszka static uint64_t
249a8170e5eSAvi Kivity ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
250610626afSaliguori {
251244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
252610626afSaliguori     int index;
253610626afSaliguori     uint32_t val = 0;
254610626afSaliguori 
255e5074b38SPeter Xu     addr &= 0xff;
256e5074b38SPeter Xu 
257e5074b38SPeter Xu     switch (addr) {
2581f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
259610626afSaliguori         val = s->ioregsel;
2601f5e71a8SJan Kiszka         break;
2611f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
2621a440963SJan Kiszka         if (size != 4) {
2631a440963SJan Kiszka             break;
2641a440963SJan Kiszka         }
265610626afSaliguori         switch (s->ioregsel) {
2661f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
2672f5a3b12SPaolo Bonzini         case IOAPIC_REG_ARB:
2681f5e71a8SJan Kiszka             val = s->id << IOAPIC_ID_SHIFT;
269610626afSaliguori             break;
2701f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
27120fd4b7bSPeter Xu             val = s->version |
2721f5e71a8SJan Kiszka                 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
273610626afSaliguori             break;
274610626afSaliguori         default:
2751f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
276610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
2771f5e71a8SJan Kiszka                 if (s->ioregsel & 1) {
278610626afSaliguori                     val = s->ioredtbl[index] >> 32;
2791f5e71a8SJan Kiszka                 } else {
280610626afSaliguori                     val = s->ioredtbl[index] & 0xffffffff;
281610626afSaliguori                 }
282610626afSaliguori             }
2831f5e71a8SJan Kiszka         }
2841f5e71a8SJan Kiszka         break;
285610626afSaliguori     }
286e5074b38SPeter Xu 
287*a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_read(addr, s->ioregsel, size, val);
288e5074b38SPeter Xu 
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 
327e5074b38SPeter Xu     addr &= 0xff;
328*a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_write(addr, s->ioregsel, size, val);
329e5074b38SPeter Xu 
330e5074b38SPeter Xu     switch (addr) {
3311f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
332610626afSaliguori         s->ioregsel = val;
3331f5e71a8SJan Kiszka         break;
3341f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
3351a440963SJan Kiszka         if (size != 4) {
3361a440963SJan Kiszka             break;
3371a440963SJan Kiszka         }
338610626afSaliguori         switch (s->ioregsel) {
3391f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
3401f5e71a8SJan Kiszka             s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
3411f5e71a8SJan Kiszka             break;
3421f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
3431f5e71a8SJan Kiszka         case IOAPIC_REG_ARB:
3441f5e71a8SJan Kiszka             break;
345610626afSaliguori         default:
3461f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
347610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
348479c2a1cSPeter Xu                 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
349610626afSaliguori                 if (s->ioregsel & 1) {
350610626afSaliguori                     s->ioredtbl[index] &= 0xffffffff;
351610626afSaliguori                     s->ioredtbl[index] |= (uint64_t)val << 32;
352610626afSaliguori                 } else {
353610626afSaliguori                     s->ioredtbl[index] &= ~0xffffffffULL;
354610626afSaliguori                     s->ioredtbl[index] |= val;
355610626afSaliguori                 }
356479c2a1cSPeter Xu                 /* restore RO bits */
357479c2a1cSPeter Xu                 s->ioredtbl[index] &= IOAPIC_RW_BITS;
358479c2a1cSPeter Xu                 s->ioredtbl[index] |= ro_bits;
359ed1263c3SPeter Xu                 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
360610626afSaliguori                 ioapic_service(s);
361610626afSaliguori             }
362610626afSaliguori         }
3631f5e71a8SJan Kiszka         break;
36420fd4b7bSPeter Xu     case IOAPIC_EOI:
36520fd4b7bSPeter Xu         /* Explicit EOI is only supported for IOAPIC version 0x20 */
36620fd4b7bSPeter Xu         if (size != 4 || s->version != 0x20) {
36720fd4b7bSPeter Xu             break;
36820fd4b7bSPeter Xu         }
36920fd4b7bSPeter Xu         ioapic_eoi_broadcast(val);
37020fd4b7bSPeter Xu         break;
371610626afSaliguori     }
37215eafc2eSPaolo Bonzini 
37315eafc2eSPaolo Bonzini     ioapic_update_kvm_routes(s);
374610626afSaliguori }
375610626afSaliguori 
3764d5bf5f6SJan Kiszka static const MemoryRegionOps ioapic_io_ops = {
3774d5bf5f6SJan Kiszka     .read = ioapic_mem_read,
3784d5bf5f6SJan Kiszka     .write = ioapic_mem_write,
3794d5bf5f6SJan Kiszka     .endianness = DEVICE_NATIVE_ENDIAN,
380610626afSaliguori };
381610626afSaliguori 
382e3d9c925SPeter Xu static void ioapic_machine_done_notify(Notifier *notifier, void *data)
383e3d9c925SPeter Xu {
384e3d9c925SPeter Xu #ifdef CONFIG_KVM
385e3d9c925SPeter Xu     IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
386e3d9c925SPeter Xu                                         machine_done);
387e3d9c925SPeter Xu 
388e3d9c925SPeter Xu     if (kvm_irqchip_is_split()) {
389e3d9c925SPeter Xu         X86IOMMUState *iommu = x86_iommu_get_default();
390e3d9c925SPeter Xu         if (iommu) {
391e3d9c925SPeter Xu             /* Register this IOAPIC with IOMMU IEC notifier, so that
392e3d9c925SPeter Xu              * when there are IR invalidates, we can be notified to
393e3d9c925SPeter Xu              * update kernel IR cache. */
394e3d9c925SPeter Xu             x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
395e3d9c925SPeter Xu         }
396e3d9c925SPeter Xu     }
397e3d9c925SPeter Xu #endif
398e3d9c925SPeter Xu }
399e3d9c925SPeter Xu 
4008d5516beSPeter Xu #define IOAPIC_VER_DEF 0x20
4018d5516beSPeter Xu 
402db0f8888Sxiaoqiang zhao static void ioapic_realize(DeviceState *dev, Error **errp)
403610626afSaliguori {
404db0f8888Sxiaoqiang zhao     IOAPICCommonState *s = IOAPIC_COMMON(dev);
405f9771858Sxiaoqiang zhao 
40620fd4b7bSPeter Xu     if (s->version != 0x11 && s->version != 0x20) {
40720fd4b7bSPeter Xu         error_report("IOAPIC only supports version 0x11 or 0x20 "
4088d5516beSPeter Xu                      "(default: 0x%x).", IOAPIC_VER_DEF);
40920fd4b7bSPeter Xu         exit(1);
41020fd4b7bSPeter Xu     }
41120fd4b7bSPeter Xu 
4121437c94bSPaolo Bonzini     memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
4131437c94bSPaolo Bonzini                           "ioapic", 0x1000);
414610626afSaliguori 
415f9771858Sxiaoqiang zhao     qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
416610626afSaliguori 
417db0f8888Sxiaoqiang zhao     ioapics[ioapic_no] = s;
418e3d9c925SPeter Xu     s->machine_done.notify = ioapic_machine_done_notify;
419e3d9c925SPeter Xu     qemu_add_machine_init_done_notifier(&s->machine_done);
420610626afSaliguori }
42196051119SBlue Swirl 
42220fd4b7bSPeter Xu static Property ioapic_properties[] = {
4238d5516beSPeter Xu     DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF),
42420fd4b7bSPeter Xu     DEFINE_PROP_END_OF_LIST(),
42520fd4b7bSPeter Xu };
42620fd4b7bSPeter Xu 
427999e12bbSAnthony Liguori static void ioapic_class_init(ObjectClass *klass, void *data)
428999e12bbSAnthony Liguori {
429999e12bbSAnthony Liguori     IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
43039bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
431999e12bbSAnthony Liguori 
432db0f8888Sxiaoqiang zhao     k->realize = ioapic_realize;
4330f254b1aSPeter Xu     /*
4340f254b1aSPeter Xu      * If APIC is in kernel, we need to update the kernel cache after
4350f254b1aSPeter Xu      * migration, otherwise first 24 gsi routes will be invalid.
4360f254b1aSPeter Xu      */
4370f254b1aSPeter Xu     k->post_load = ioapic_update_kvm_routes;
43839bffca2SAnthony Liguori     dc->reset = ioapic_reset_common;
43920fd4b7bSPeter Xu     dc->props = ioapic_properties;
440999e12bbSAnthony Liguori }
441999e12bbSAnthony Liguori 
4428c43a6f0SAndreas Färber static const TypeInfo ioapic_info = {
443999e12bbSAnthony Liguori     .name          = "ioapic",
44439bffca2SAnthony Liguori     .parent        = TYPE_IOAPIC_COMMON,
44539bffca2SAnthony Liguori     .instance_size = sizeof(IOAPICCommonState),
446999e12bbSAnthony Liguori     .class_init    = ioapic_class_init,
44796051119SBlue Swirl };
44896051119SBlue Swirl 
44983f7d43aSAndreas Färber static void ioapic_register_types(void)
45096051119SBlue Swirl {
45139bffca2SAnthony Liguori     type_register_static(&ioapic_info);
45296051119SBlue Swirl }
45396051119SBlue Swirl 
45483f7d43aSAndreas Färber type_init(ioapic_register_types)
455