xref: /qemu/hw/intc/ioapic.c (revision 455e17a1b85fbc3d8c7208b5bc9a742d8508b324)
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"
31*455e17a1SMichael 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);
1511f5e71a8SJan Kiszka     if (vector == 0) {
152610626afSaliguori         vector = 2;
1531f5e71a8SJan Kiszka     }
154610626afSaliguori     if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
155610626afSaliguori         uint32_t mask = 1 << vector;
156610626afSaliguori         uint64_t entry = s->ioredtbl[vector];
157610626afSaliguori 
1581f5e71a8SJan Kiszka         if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
1591f5e71a8SJan Kiszka             IOAPIC_TRIGGER_LEVEL) {
160610626afSaliguori             /* level triggered */
161610626afSaliguori             if (level) {
162610626afSaliguori                 s->irr |= mask;
163c5955a56SPaolo Bonzini                 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
164610626afSaliguori                     ioapic_service(s);
165c5955a56SPaolo Bonzini                 }
166610626afSaliguori             } else {
167610626afSaliguori                 s->irr &= ~mask;
168610626afSaliguori             }
169610626afSaliguori         } else {
17047f7be39SJan Kiszka             /* According to the 82093AA manual, we must ignore edge requests
17147f7be39SJan Kiszka              * if the input pin is masked. */
17247f7be39SJan Kiszka             if (level && !(entry & IOAPIC_LVT_MASKED)) {
173610626afSaliguori                 s->irr |= mask;
174610626afSaliguori                 ioapic_service(s);
175610626afSaliguori             }
176610626afSaliguori         }
177610626afSaliguori     }
178610626afSaliguori }
179610626afSaliguori 
18015eafc2eSPaolo Bonzini static void ioapic_update_kvm_routes(IOAPICCommonState *s)
18115eafc2eSPaolo Bonzini {
18215eafc2eSPaolo Bonzini #ifdef CONFIG_KVM
18315eafc2eSPaolo Bonzini     int i;
18415eafc2eSPaolo Bonzini 
18515eafc2eSPaolo Bonzini     if (kvm_irqchip_is_split()) {
18615eafc2eSPaolo Bonzini         for (i = 0; i < IOAPIC_NUM_PINS; i++) {
18715eafc2eSPaolo Bonzini             MSIMessage msg;
188c15fa0beSPeter Xu             struct ioapic_entry_info info;
189c15fa0beSPeter Xu             ioapic_entry_parse(s->ioredtbl[i], &info);
190c15fa0beSPeter Xu             msg.address = info.addr;
191c15fa0beSPeter Xu             msg.data = info.data;
19215eafc2eSPaolo Bonzini             kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
19315eafc2eSPaolo Bonzini         }
19415eafc2eSPaolo Bonzini         kvm_irqchip_commit_routes(kvm_state);
19515eafc2eSPaolo Bonzini     }
19615eafc2eSPaolo Bonzini #endif
19715eafc2eSPaolo Bonzini }
19815eafc2eSPaolo Bonzini 
199e3d9c925SPeter Xu #ifdef CONFIG_KVM
200e3d9c925SPeter Xu static void ioapic_iec_notifier(void *private, bool global,
201e3d9c925SPeter Xu                                 uint32_t index, uint32_t mask)
202e3d9c925SPeter Xu {
203e3d9c925SPeter Xu     IOAPICCommonState *s = (IOAPICCommonState *)private;
204e3d9c925SPeter Xu     /* For simplicity, we just update all the routes */
205e3d9c925SPeter Xu     ioapic_update_kvm_routes(s);
206e3d9c925SPeter Xu }
207e3d9c925SPeter Xu #endif
208e3d9c925SPeter Xu 
2090280b571SJan Kiszka void ioapic_eoi_broadcast(int vector)
2100280b571SJan Kiszka {
211244ac3afSJan Kiszka     IOAPICCommonState *s;
2120280b571SJan Kiszka     uint64_t entry;
2130280b571SJan Kiszka     int i, n;
2140280b571SJan Kiszka 
215e5074b38SPeter Xu     trace_ioapic_eoi_broadcast(vector);
216e5074b38SPeter Xu 
2170280b571SJan Kiszka     for (i = 0; i < MAX_IOAPICS; i++) {
2180280b571SJan Kiszka         s = ioapics[i];
2190280b571SJan Kiszka         if (!s) {
2200280b571SJan Kiszka             continue;
2210280b571SJan Kiszka         }
2220280b571SJan Kiszka         for (n = 0; n < IOAPIC_NUM_PINS; n++) {
2230280b571SJan Kiszka             entry = s->ioredtbl[n];
2241f5e71a8SJan Kiszka             if ((entry & IOAPIC_LVT_REMOTE_IRR)
2251f5e71a8SJan Kiszka                 && (entry & IOAPIC_VECTOR_MASK) == vector) {
226e5074b38SPeter Xu                 trace_ioapic_clear_remote_irr(n, vector);
2270280b571SJan Kiszka                 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
2280280b571SJan Kiszka                 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
2290280b571SJan Kiszka                     ioapic_service(s);
2300280b571SJan Kiszka                 }
2310280b571SJan Kiszka             }
2320280b571SJan Kiszka         }
2330280b571SJan Kiszka     }
2340280b571SJan Kiszka }
2350280b571SJan Kiszka 
2366bde8fd6SPavel Butsykin void ioapic_dump_state(Monitor *mon, const QDict *qdict)
2376bde8fd6SPavel Butsykin {
2386bde8fd6SPavel Butsykin     int i;
2396bde8fd6SPavel Butsykin 
2406bde8fd6SPavel Butsykin     for (i = 0; i < MAX_IOAPICS; i++) {
2416bde8fd6SPavel Butsykin         if (ioapics[i] != 0) {
2426bde8fd6SPavel Butsykin             ioapic_print_redtbl(mon, ioapics[i]);
2436bde8fd6SPavel Butsykin         }
2446bde8fd6SPavel Butsykin     }
2456bde8fd6SPavel Butsykin }
2466bde8fd6SPavel Butsykin 
2474d5bf5f6SJan Kiszka static uint64_t
248a8170e5eSAvi Kivity ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
249610626afSaliguori {
250244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
251610626afSaliguori     int index;
252610626afSaliguori     uint32_t val = 0;
253610626afSaliguori 
254e5074b38SPeter Xu     addr &= 0xff;
255e5074b38SPeter Xu 
256e5074b38SPeter Xu     switch (addr) {
2571f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
258610626afSaliguori         val = s->ioregsel;
2591f5e71a8SJan Kiszka         break;
2601f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
2611a440963SJan Kiszka         if (size != 4) {
2621a440963SJan Kiszka             break;
2631a440963SJan Kiszka         }
264610626afSaliguori         switch (s->ioregsel) {
2651f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
2662f5a3b12SPaolo Bonzini         case IOAPIC_REG_ARB:
2671f5e71a8SJan Kiszka             val = s->id << IOAPIC_ID_SHIFT;
268610626afSaliguori             break;
2691f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
27020fd4b7bSPeter Xu             val = s->version |
2711f5e71a8SJan Kiszka                 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
272610626afSaliguori             break;
273610626afSaliguori         default:
2741f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
275610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
2761f5e71a8SJan Kiszka                 if (s->ioregsel & 1) {
277610626afSaliguori                     val = s->ioredtbl[index] >> 32;
2781f5e71a8SJan Kiszka                 } else {
279610626afSaliguori                     val = s->ioredtbl[index] & 0xffffffff;
280610626afSaliguori                 }
281610626afSaliguori             }
2821f5e71a8SJan Kiszka         }
2831f5e71a8SJan Kiszka         break;
284610626afSaliguori     }
285e5074b38SPeter Xu 
286a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_read(addr, s->ioregsel, size, val);
287e5074b38SPeter Xu 
288610626afSaliguori     return val;
289610626afSaliguori }
290610626afSaliguori 
291ed1263c3SPeter Xu /*
292ed1263c3SPeter Xu  * This is to satisfy the hack in Linux kernel. One hack of it is to
293ed1263c3SPeter Xu  * simulate clearing the Remote IRR bit of IOAPIC entry using the
294ed1263c3SPeter Xu  * following:
295ed1263c3SPeter Xu  *
296ed1263c3SPeter Xu  * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
297ed1263c3SPeter Xu  * Otherwise, we simulate the EOI message manually by changing the trigger
298ed1263c3SPeter Xu  * mode to edge and then back to level, with RTE being masked during
299ed1263c3SPeter Xu  * this."
300ed1263c3SPeter Xu  *
301ed1263c3SPeter Xu  * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
302ed1263c3SPeter Xu  *
303ed1263c3SPeter Xu  * This is based on the assumption that, Remote IRR bit will be
304ed1263c3SPeter Xu  * cleared by IOAPIC hardware when configured as edge-triggered
305ed1263c3SPeter Xu  * interrupts.
306ed1263c3SPeter Xu  *
307ed1263c3SPeter Xu  * Without this, level-triggered interrupts in IR mode might fail to
308ed1263c3SPeter Xu  * work correctly.
309ed1263c3SPeter Xu  */
310ed1263c3SPeter Xu static inline void
311ed1263c3SPeter Xu ioapic_fix_edge_remote_irr(uint64_t *entry)
312ed1263c3SPeter Xu {
313ed1263c3SPeter Xu     if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
314ed1263c3SPeter Xu         /* Edge-triggered interrupts, make sure remote IRR is zero */
315ed1263c3SPeter Xu         *entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
316ed1263c3SPeter Xu     }
317ed1263c3SPeter Xu }
318ed1263c3SPeter Xu 
3191f5e71a8SJan Kiszka static void
320a8170e5eSAvi Kivity ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
3214d5bf5f6SJan Kiszka                  unsigned int size)
322610626afSaliguori {
323244ac3afSJan Kiszka     IOAPICCommonState *s = opaque;
324610626afSaliguori     int index;
325610626afSaliguori 
326e5074b38SPeter Xu     addr &= 0xff;
327a2e6ffabSDr. David Alan Gilbert     trace_ioapic_mem_write(addr, s->ioregsel, size, val);
328e5074b38SPeter Xu 
329e5074b38SPeter Xu     switch (addr) {
3301f5e71a8SJan Kiszka     case IOAPIC_IOREGSEL:
331610626afSaliguori         s->ioregsel = val;
3321f5e71a8SJan Kiszka         break;
3331f5e71a8SJan Kiszka     case IOAPIC_IOWIN:
3341a440963SJan Kiszka         if (size != 4) {
3351a440963SJan Kiszka             break;
3361a440963SJan Kiszka         }
337610626afSaliguori         switch (s->ioregsel) {
3381f5e71a8SJan Kiszka         case IOAPIC_REG_ID:
3391f5e71a8SJan Kiszka             s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
3401f5e71a8SJan Kiszka             break;
3411f5e71a8SJan Kiszka         case IOAPIC_REG_VER:
3421f5e71a8SJan Kiszka         case IOAPIC_REG_ARB:
3431f5e71a8SJan Kiszka             break;
344610626afSaliguori         default:
3451f5e71a8SJan Kiszka             index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
346610626afSaliguori             if (index >= 0 && index < IOAPIC_NUM_PINS) {
347479c2a1cSPeter Xu                 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
348610626afSaliguori                 if (s->ioregsel & 1) {
349610626afSaliguori                     s->ioredtbl[index] &= 0xffffffff;
350610626afSaliguori                     s->ioredtbl[index] |= (uint64_t)val << 32;
351610626afSaliguori                 } else {
352610626afSaliguori                     s->ioredtbl[index] &= ~0xffffffffULL;
353610626afSaliguori                     s->ioredtbl[index] |= val;
354610626afSaliguori                 }
355479c2a1cSPeter Xu                 /* restore RO bits */
356479c2a1cSPeter Xu                 s->ioredtbl[index] &= IOAPIC_RW_BITS;
357479c2a1cSPeter Xu                 s->ioredtbl[index] |= ro_bits;
358ed1263c3SPeter Xu                 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
359610626afSaliguori                 ioapic_service(s);
360610626afSaliguori             }
361610626afSaliguori         }
3621f5e71a8SJan Kiszka         break;
36320fd4b7bSPeter Xu     case IOAPIC_EOI:
36420fd4b7bSPeter Xu         /* Explicit EOI is only supported for IOAPIC version 0x20 */
36520fd4b7bSPeter Xu         if (size != 4 || s->version != 0x20) {
36620fd4b7bSPeter Xu             break;
36720fd4b7bSPeter Xu         }
36820fd4b7bSPeter Xu         ioapic_eoi_broadcast(val);
36920fd4b7bSPeter Xu         break;
370610626afSaliguori     }
37115eafc2eSPaolo Bonzini 
37215eafc2eSPaolo Bonzini     ioapic_update_kvm_routes(s);
373610626afSaliguori }
374610626afSaliguori 
3754d5bf5f6SJan Kiszka static const MemoryRegionOps ioapic_io_ops = {
3764d5bf5f6SJan Kiszka     .read = ioapic_mem_read,
3774d5bf5f6SJan Kiszka     .write = ioapic_mem_write,
3784d5bf5f6SJan Kiszka     .endianness = DEVICE_NATIVE_ENDIAN,
379610626afSaliguori };
380610626afSaliguori 
381e3d9c925SPeter Xu static void ioapic_machine_done_notify(Notifier *notifier, void *data)
382e3d9c925SPeter Xu {
383e3d9c925SPeter Xu #ifdef CONFIG_KVM
384e3d9c925SPeter Xu     IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
385e3d9c925SPeter Xu                                         machine_done);
386e3d9c925SPeter Xu 
387e3d9c925SPeter Xu     if (kvm_irqchip_is_split()) {
388e3d9c925SPeter Xu         X86IOMMUState *iommu = x86_iommu_get_default();
389e3d9c925SPeter Xu         if (iommu) {
390e3d9c925SPeter Xu             /* Register this IOAPIC with IOMMU IEC notifier, so that
391e3d9c925SPeter Xu              * when there are IR invalidates, we can be notified to
392e3d9c925SPeter Xu              * update kernel IR cache. */
393e3d9c925SPeter Xu             x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
394e3d9c925SPeter Xu         }
395e3d9c925SPeter Xu     }
396e3d9c925SPeter Xu #endif
397e3d9c925SPeter Xu }
398e3d9c925SPeter Xu 
3998d5516beSPeter Xu #define IOAPIC_VER_DEF 0x20
4008d5516beSPeter Xu 
401db0f8888Sxiaoqiang zhao static void ioapic_realize(DeviceState *dev, Error **errp)
402610626afSaliguori {
403db0f8888Sxiaoqiang zhao     IOAPICCommonState *s = IOAPIC_COMMON(dev);
404f9771858Sxiaoqiang zhao 
40520fd4b7bSPeter Xu     if (s->version != 0x11 && s->version != 0x20) {
40620fd4b7bSPeter Xu         error_report("IOAPIC only supports version 0x11 or 0x20 "
4078d5516beSPeter Xu                      "(default: 0x%x).", IOAPIC_VER_DEF);
40820fd4b7bSPeter Xu         exit(1);
40920fd4b7bSPeter Xu     }
41020fd4b7bSPeter Xu 
4111437c94bSPaolo Bonzini     memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
4121437c94bSPaolo Bonzini                           "ioapic", 0x1000);
413610626afSaliguori 
414f9771858Sxiaoqiang zhao     qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
415610626afSaliguori 
416db0f8888Sxiaoqiang zhao     ioapics[ioapic_no] = s;
417e3d9c925SPeter Xu     s->machine_done.notify = ioapic_machine_done_notify;
418e3d9c925SPeter Xu     qemu_add_machine_init_done_notifier(&s->machine_done);
419610626afSaliguori }
42096051119SBlue Swirl 
42120fd4b7bSPeter Xu static Property ioapic_properties[] = {
4228d5516beSPeter Xu     DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF),
42320fd4b7bSPeter Xu     DEFINE_PROP_END_OF_LIST(),
42420fd4b7bSPeter Xu };
42520fd4b7bSPeter Xu 
426999e12bbSAnthony Liguori static void ioapic_class_init(ObjectClass *klass, void *data)
427999e12bbSAnthony Liguori {
428999e12bbSAnthony Liguori     IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
42939bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
430999e12bbSAnthony Liguori 
431db0f8888Sxiaoqiang zhao     k->realize = ioapic_realize;
4320f254b1aSPeter Xu     /*
4330f254b1aSPeter Xu      * If APIC is in kernel, we need to update the kernel cache after
4340f254b1aSPeter Xu      * migration, otherwise first 24 gsi routes will be invalid.
4350f254b1aSPeter Xu      */
4360f254b1aSPeter Xu     k->post_load = ioapic_update_kvm_routes;
43739bffca2SAnthony Liguori     dc->reset = ioapic_reset_common;
43820fd4b7bSPeter Xu     dc->props = ioapic_properties;
439999e12bbSAnthony Liguori }
440999e12bbSAnthony Liguori 
4418c43a6f0SAndreas Färber static const TypeInfo ioapic_info = {
442999e12bbSAnthony Liguori     .name          = "ioapic",
44339bffca2SAnthony Liguori     .parent        = TYPE_IOAPIC_COMMON,
44439bffca2SAnthony Liguori     .instance_size = sizeof(IOAPICCommonState),
445999e12bbSAnthony Liguori     .class_init    = ioapic_class_init,
44696051119SBlue Swirl };
44796051119SBlue Swirl 
44883f7d43aSAndreas Färber static void ioapic_register_types(void)
44996051119SBlue Swirl {
45039bffca2SAnthony Liguori     type_register_static(&ioapic_info);
45196051119SBlue Swirl }
45296051119SBlue Swirl 
45383f7d43aSAndreas Färber type_init(ioapic_register_types)
454