xref: /qemu/hw/intc/ioapic.c (revision 34bec7a8d04f23d6258dcf338894956b945673a5)
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"
2411ab69d6SMarkus Armbruster #include "qapi/error.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"
31455e17a1SMichael S. Tsirkin #include "hw/pci/msi.h"
3215eafc2eSPaolo Bonzini #include "sysemu/kvm.h"
33cb135f59SPeter Xu #include "hw/i386/apic-msidef.h"
34e3d9c925SPeter Xu #include "hw/i386/x86-iommu.h"
35e5074b38SPeter Xu #include "trace.h"
36610626afSaliguori 
3715eafc2eSPaolo Bonzini #define APIC_DELIVERY_MODE_SHIFT 8
3815eafc2eSPaolo Bonzini #define APIC_POLARITY_SHIFT 14
3915eafc2eSPaolo Bonzini #define APIC_TRIG_MODE_SHIFT 15
4015eafc2eSPaolo Bonzini 
41244ac3afSJan Kiszka static IOAPICCommonState *ioapics[MAX_IOAPICS];
420280b571SJan Kiszka 
43db0f8888Sxiaoqiang zhao /* global variable from ioapic_common.c */
44db0f8888Sxiaoqiang zhao extern int ioapic_no;
45db0f8888Sxiaoqiang zhao 
46c15fa0beSPeter Xu struct ioapic_entry_info {
47c15fa0beSPeter Xu     /* fields parsed from IOAPIC entries */
48c15fa0beSPeter Xu     uint8_t masked;
49c15fa0beSPeter Xu     uint8_t trig_mode;
50c15fa0beSPeter Xu     uint16_t dest_idx;
51c15fa0beSPeter Xu     uint8_t dest_mode;
52c15fa0beSPeter Xu     uint8_t delivery_mode;
53c15fa0beSPeter Xu     uint8_t vector;
54c15fa0beSPeter Xu 
55c15fa0beSPeter Xu     /* MSI message generated from above parsed fields */
56c15fa0beSPeter Xu     uint32_t addr;
57c15fa0beSPeter Xu     uint32_t data;
58c15fa0beSPeter Xu };
59c15fa0beSPeter Xu 
60c15fa0beSPeter Xu static void ioapic_entry_parse(uint64_t entry, struct ioapic_entry_info *info)
61c15fa0beSPeter Xu {
62c15fa0beSPeter Xu     memset(info, 0, sizeof(*info));
63c15fa0beSPeter Xu     info->masked = (entry >> IOAPIC_LVT_MASKED_SHIFT) & 1;
64c15fa0beSPeter Xu     info->trig_mode = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
65c15fa0beSPeter Xu     /*
66c15fa0beSPeter Xu      * By default, this would be dest_id[8] + reserved[8]. When IR
67c15fa0beSPeter Xu      * is enabled, this would be interrupt_index[15] +
68c15fa0beSPeter Xu      * interrupt_format[1]. This field never means anything, but
69c15fa0beSPeter Xu      * only used to generate corresponding MSI.
70c15fa0beSPeter Xu      */
71c15fa0beSPeter Xu     info->dest_idx = (entry >> IOAPIC_LVT_DEST_IDX_SHIFT) & 0xffff;
72c15fa0beSPeter Xu     info->dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
73c15fa0beSPeter Xu     info->delivery_mode = (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) \
74c15fa0beSPeter Xu         & IOAPIC_DM_MASK;
75c15fa0beSPeter Xu     if (info->delivery_mode == IOAPIC_DM_EXTINT) {
76c15fa0beSPeter Xu         info->vector = pic_read_irq(isa_pic);
77c15fa0beSPeter Xu     } else {
78c15fa0beSPeter Xu         info->vector = entry & IOAPIC_VECTOR_MASK;
79c15fa0beSPeter Xu     }
80c15fa0beSPeter Xu 
81c15fa0beSPeter Xu     info->addr = APIC_DEFAULT_ADDRESS | \
82c15fa0beSPeter Xu         (info->dest_idx << MSI_ADDR_DEST_IDX_SHIFT) | \
83c15fa0beSPeter Xu         (info->dest_mode << MSI_ADDR_DEST_MODE_SHIFT);
84c15fa0beSPeter Xu     info->data = (info->vector << MSI_DATA_VECTOR_SHIFT) | \
85c15fa0beSPeter Xu         (info->trig_mode << MSI_DATA_TRIGGER_SHIFT) | \
86c15fa0beSPeter Xu         (info->delivery_mode << MSI_DATA_DELIVERY_MODE_SHIFT);
87c15fa0beSPeter Xu }
88c15fa0beSPeter Xu 
89244ac3afSJan Kiszka static void ioapic_service(IOAPICCommonState *s)
90610626afSaliguori {
91cb135f59SPeter Xu     AddressSpace *ioapic_as = PC_MACHINE(qdev_get_machine())->ioapic_as;
92c15fa0beSPeter Xu     struct ioapic_entry_info info;
93610626afSaliguori     uint8_t i;
94610626afSaliguori     uint32_t mask;
95610626afSaliguori     uint64_t entry;
96610626afSaliguori 
97610626afSaliguori     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
98610626afSaliguori         mask = 1 << i;
99610626afSaliguori         if (s->irr & mask) {
10015eafc2eSPaolo Bonzini             int coalesce = 0;
10115eafc2eSPaolo Bonzini 
102610626afSaliguori             entry = s->ioredtbl[i];
103c15fa0beSPeter Xu             ioapic_entry_parse(entry, &info);
104c15fa0beSPeter Xu             if (!info.masked) {
105c15fa0beSPeter Xu                 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
106610626afSaliguori                     s->irr &= ~mask;
1070280b571SJan Kiszka                 } else {
10815eafc2eSPaolo Bonzini                     coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
109e5074b38SPeter Xu                     trace_ioapic_set_remote_irr(i);
1100280b571SJan Kiszka                     s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
1110280b571SJan Kiszka                 }
112c15fa0beSPeter Xu 
113f99b86b9SPeter Xu                 if (coalesce) {
114f99b86b9SPeter Xu                     /* We are level triggered interrupts, and the
115f99b86b9SPeter Xu                      * guest should be still working on previous one,
116f99b86b9SPeter Xu                      * so skip it. */
117f99b86b9SPeter Xu                     continue;
118f99b86b9SPeter Xu                 }
119f99b86b9SPeter Xu 
12015eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
12115eafc2eSPaolo Bonzini                 if (kvm_irqchip_is_split()) {
122c15fa0beSPeter Xu                     if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
12315eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
12415eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 0);
12515eafc2eSPaolo Bonzini                     } else {
12615eafc2eSPaolo Bonzini                         kvm_set_irq(kvm_state, i, 1);
12715eafc2eSPaolo Bonzini                     }
12815eafc2eSPaolo Bonzini                     continue;
12915eafc2eSPaolo Bonzini                 }
13015eafc2eSPaolo Bonzini #endif
131f99b86b9SPeter Xu 
132cb135f59SPeter Xu                 /* No matter whether IR is enabled, we translate
133cb135f59SPeter Xu                  * the IOAPIC message into a MSI one, and its
134cb135f59SPeter Xu                  * address space will decide whether we need a
135cb135f59SPeter Xu                  * translation. */
136c15fa0beSPeter Xu                 stl_le_phys(ioapic_as, info.addr, info.data);
137610626afSaliguori             }
138610626afSaliguori         }
139610626afSaliguori     }
140610626afSaliguori }
141610626afSaliguori 
1427d0500c4SBlue Swirl static void ioapic_set_irq(void *opaque, int vector, int level)
143610626afSaliguori {
144244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
145610626afSaliguori 
146610626afSaliguori     /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
147610626afSaliguori      * to GSI 2.  GSI maps to ioapic 1-1.  This is not
148610626afSaliguori      * the cleanest way of doing it but it should work. */
149610626afSaliguori 
150a2e6ffabSDr. David Alan Gilbert     trace_ioapic_set_irq(vector, level);
151cce5405eSPeter Xu     ioapic_stat_update_irq(s, vector, level);
1521f5e71a8SJan Kiszka     if (vector == 0) {
153610626afSaliguori         vector = 2;
1541f5e71a8SJan Kiszka     }
155960a479fSPaolo Bonzini     if (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 
2374d5bf5f6SJan Kiszka static uint64_t
238a8170e5eSAvi Kivity ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
239610626afSaliguori {
240244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
241610626afSaliguori     int index;
242610626afSaliguori     uint32_t val = 0;
243610626afSaliguori 
244e5074b38SPeter Xu     addr &= 0xff;
245e5074b38SPeter Xu 
246e5074b38SPeter Xu     switch (addr) {
2471f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
248610626afSaliguori         val = s->ioregsel;
2491f5e71a8SJan Kiszka         break;
2501f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
2511a440963SJan Kiszka         if (size != 4) {
2521a440963SJan Kiszka             break;
2531a440963SJan Kiszka         }
254610626afSaliguori         switch (s->ioregsel) {
2551f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
2562f5a3b12SPaolo Bonzini         case IOAPIC_REG_ARB:
2571f5e71a8SJan Kiszka             val = s->id << IOAPIC_ID_SHIFT;
258610626afSaliguori             break;
2591f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
26020fd4b7bSPeter Xu             val = s->version |
2611f5e71a8SJan Kiszka                 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
262610626afSaliguori             break;
263610626afSaliguori         default:
2641f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
265610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
2661f5e71a8SJan Kiszka                 if (s->ioregsel & 1) {
267610626afSaliguori                     val = s->ioredtbl[index] >> 32;
2681f5e71a8SJan Kiszka                 } else {
269610626afSaliguori                     val = s->ioredtbl[index] & 0xffffffff;
270610626afSaliguori                 }
271610626afSaliguori             }
2721f5e71a8SJan Kiszka         }
2731f5e71a8SJan Kiszka         break;
274610626afSaliguori     }
275e5074b38SPeter Xu 
276a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_read(addr, s->ioregsel, size, val);
277e5074b38SPeter Xu 
278610626afSaliguori     return val;
279610626afSaliguori }
280610626afSaliguori 
281ed1263c3SPeter Xu /*
282ed1263c3SPeter Xu  * This is to satisfy the hack in Linux kernel. One hack of it is to
283ed1263c3SPeter Xu  * simulate clearing the Remote IRR bit of IOAPIC entry using the
284ed1263c3SPeter Xu  * following:
285ed1263c3SPeter Xu  *
286ed1263c3SPeter Xu  * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
287ed1263c3SPeter Xu  * Otherwise, we simulate the EOI message manually by changing the trigger
288ed1263c3SPeter Xu  * mode to edge and then back to level, with RTE being masked during
289ed1263c3SPeter Xu  * this."
290ed1263c3SPeter Xu  *
291ed1263c3SPeter Xu  * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
292ed1263c3SPeter Xu  *
293ed1263c3SPeter Xu  * This is based on the assumption that, Remote IRR bit will be
294ed1263c3SPeter Xu  * cleared by IOAPIC hardware when configured as edge-triggered
295ed1263c3SPeter Xu  * interrupts.
296ed1263c3SPeter Xu  *
297ed1263c3SPeter Xu  * Without this, level-triggered interrupts in IR mode might fail to
298ed1263c3SPeter Xu  * work correctly.
299ed1263c3SPeter Xu  */
300ed1263c3SPeter Xu static inline void
301ed1263c3SPeter Xu ioapic_fix_edge_remote_irr(uint64_t *entry)
302ed1263c3SPeter Xu {
303ed1263c3SPeter Xu     if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
304ed1263c3SPeter Xu         /* Edge-triggered interrupts, make sure remote IRR is zero */
305ed1263c3SPeter Xu         *entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
306ed1263c3SPeter Xu     }
307ed1263c3SPeter Xu }
308ed1263c3SPeter Xu 
3091f5e71a8SJan Kiszka static void
310a8170e5eSAvi Kivity ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
3114d5bf5f6SJan Kiszka                  unsigned int size)
312610626afSaliguori {
313244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
314610626afSaliguori     int index;
315610626afSaliguori 
316e5074b38SPeter Xu     addr &= 0xff;
317a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_write(addr, s->ioregsel, size, val);
318e5074b38SPeter Xu 
319e5074b38SPeter Xu     switch (addr) {
3201f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
321610626afSaliguori         s->ioregsel = val;
3221f5e71a8SJan Kiszka         break;
3231f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
3241a440963SJan Kiszka         if (size != 4) {
3251a440963SJan Kiszka             break;
3261a440963SJan Kiszka         }
327610626afSaliguori         switch (s->ioregsel) {
3281f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
3291f5e71a8SJan Kiszka             s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
3301f5e71a8SJan Kiszka             break;
3311f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
3321f5e71a8SJan Kiszka         case IOAPIC_REG_ARB:
3331f5e71a8SJan Kiszka             break;
334610626afSaliguori         default:
3351f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
336610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
337479c2a1cSPeter Xu                 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
338610626afSaliguori                 if (s->ioregsel & 1) {
339610626afSaliguori                     s->ioredtbl[index] &= 0xffffffff;
340610626afSaliguori                     s->ioredtbl[index] |= (uint64_t)val << 32;
341610626afSaliguori                 } else {
342610626afSaliguori                     s->ioredtbl[index] &= ~0xffffffffULL;
343610626afSaliguori                     s->ioredtbl[index] |= val;
344610626afSaliguori                 }
345479c2a1cSPeter Xu                 /* restore RO bits */
346479c2a1cSPeter Xu                 s->ioredtbl[index] &= IOAPIC_RW_BITS;
347479c2a1cSPeter Xu                 s->ioredtbl[index] |= ro_bits;
348ed1263c3SPeter Xu                 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
349610626afSaliguori                 ioapic_service(s);
350610626afSaliguori             }
351610626afSaliguori         }
3521f5e71a8SJan Kiszka         break;
35320fd4b7bSPeter Xu     case IOAPIC_EOI:
35420fd4b7bSPeter Xu         /* Explicit EOI is only supported for IOAPIC version 0x20 */
35520fd4b7bSPeter Xu         if (size != 4 || s->version != 0x20) {
35620fd4b7bSPeter Xu             break;
35720fd4b7bSPeter Xu         }
35820fd4b7bSPeter Xu         ioapic_eoi_broadcast(val);
35920fd4b7bSPeter Xu         break;
360610626afSaliguori     }
36115eafc2eSPaolo Bonzini 
36215eafc2eSPaolo Bonzini     ioapic_update_kvm_routes(s);
363610626afSaliguori }
364610626afSaliguori 
3654d5bf5f6SJan Kiszka static const MemoryRegionOps ioapic_io_ops = {
3664d5bf5f6SJan Kiszka     .read = ioapic_mem_read,
3674d5bf5f6SJan Kiszka     .write = ioapic_mem_write,
3684d5bf5f6SJan Kiszka     .endianness = DEVICE_NATIVE_ENDIAN,
369610626afSaliguori };
370610626afSaliguori 
371e3d9c925SPeter Xu static void ioapic_machine_done_notify(Notifier *notifier, void *data)
372e3d9c925SPeter Xu {
373e3d9c925SPeter Xu #ifdef CONFIG_KVM
374e3d9c925SPeter Xu     IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
375e3d9c925SPeter Xu                                         machine_done);
376e3d9c925SPeter Xu 
377e3d9c925SPeter Xu     if (kvm_irqchip_is_split()) {
378e3d9c925SPeter Xu         X86IOMMUState *iommu = x86_iommu_get_default();
379e3d9c925SPeter Xu         if (iommu) {
380e3d9c925SPeter Xu             /* Register this IOAPIC with IOMMU IEC notifier, so that
381e3d9c925SPeter Xu              * when there are IR invalidates, we can be notified to
382e3d9c925SPeter Xu              * update kernel IR cache. */
383e3d9c925SPeter Xu             x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
384e3d9c925SPeter Xu         }
385e3d9c925SPeter Xu     }
386e3d9c925SPeter Xu #endif
387e3d9c925SPeter Xu }
388e3d9c925SPeter Xu 
3898d5516beSPeter Xu #define IOAPIC_VER_DEF 0x20
3908d5516beSPeter Xu 
391db0f8888Sxiaoqiang zhao static void ioapic_realize(DeviceState *dev, Error **errp)
392610626afSaliguori {
393db0f8888Sxiaoqiang zhao     IOAPICCommonState *s = IOAPIC_COMMON(dev);
394f9771858Sxiaoqiang zhao 
39520fd4b7bSPeter Xu     if (s->version != 0x11 && s->version != 0x20) {
39611ab69d6SMarkus Armbruster         error_setg(errp, "IOAPIC only supports version 0x11 or 0x20 "
3978d5516beSPeter Xu                    "(default: 0x%x).", IOAPIC_VER_DEF);
39811ab69d6SMarkus Armbruster         return;
39920fd4b7bSPeter Xu     }
40020fd4b7bSPeter Xu 
4011437c94bSPaolo Bonzini     memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
4021437c94bSPaolo Bonzini                           "ioapic", 0x1000);
403610626afSaliguori 
404f9771858Sxiaoqiang zhao     qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
405610626afSaliguori 
406db0f8888Sxiaoqiang zhao     ioapics[ioapic_no] = s;
407e3d9c925SPeter Xu     s->machine_done.notify = ioapic_machine_done_notify;
408e3d9c925SPeter Xu     qemu_add_machine_init_done_notifier(&s->machine_done);
409610626afSaliguori }
41096051119SBlue Swirl 
41120fd4b7bSPeter Xu static Property ioapic_properties[] = {
4128d5516beSPeter Xu     DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF),
41320fd4b7bSPeter Xu     DEFINE_PROP_END_OF_LIST(),
41420fd4b7bSPeter Xu };
41520fd4b7bSPeter Xu 
416999e12bbSAnthony Liguori static void ioapic_class_init(ObjectClass *klass, void *data)
417999e12bbSAnthony Liguori {
418999e12bbSAnthony Liguori     IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
41939bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
420999e12bbSAnthony Liguori 
421db0f8888Sxiaoqiang zhao     k->realize = ioapic_realize;
4220f254b1aSPeter Xu     /*
4230f254b1aSPeter Xu      * If APIC is in kernel, we need to update the kernel cache after
4240f254b1aSPeter Xu      * migration, otherwise first 24 gsi routes will be invalid.
4250f254b1aSPeter Xu      */
4260f254b1aSPeter Xu     k->post_load = ioapic_update_kvm_routes;
42739bffca2SAnthony Liguori     dc->reset = ioapic_reset_common;
42820fd4b7bSPeter Xu     dc->props = ioapic_properties;
429999e12bbSAnthony Liguori }
430999e12bbSAnthony Liguori 
4318c43a6f0SAndreas Färber static const TypeInfo ioapic_info = {
432*34bec7a8SLi Qiang     .name          = TYPE_IOAPIC,
43339bffca2SAnthony Liguori     .parent        = TYPE_IOAPIC_COMMON,
43439bffca2SAnthony Liguori     .instance_size = sizeof(IOAPICCommonState),
435999e12bbSAnthony Liguori     .class_init    = ioapic_class_init,
43696051119SBlue Swirl };
43796051119SBlue Swirl 
43883f7d43aSAndreas Färber static void ioapic_register_types(void)
43996051119SBlue Swirl {
44039bffca2SAnthony Liguori     type_register_static(&ioapic_info);
44196051119SBlue Swirl }
44296051119SBlue Swirl 
44383f7d43aSAndreas Färber type_init(ioapic_register_types)
444