blob: 716ffc8bbbda1db4cfba8ecbdbce04ffc8455564 [file] [log] [blame]
aliguori610626a2009-03-12 20:25:12 +00001/*
2 * ioapic.c IOAPIC emulation logic
3 *
4 * Copyright (c) 2004-2005 Fabrice Bellard
5 *
6 * Split the ioapic logic from apic.c
7 * Xiantao Zhang <xiantao.zhang@intel.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
Chetan Pant61f3c912020-10-23 12:44:24 +000012 * version 2.1 of the License, or (at your option) any later version.
aliguori610626a2009-03-12 20:25:12 +000013 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000020 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
aliguori610626a2009-03-12 20:25:12 +000021 */
22
Peter Maydellb6a0aa02016-01-26 18:17:03 +000023#include "qemu/osdep.h"
Markus Armbruster11ab69d2018-10-17 10:26:34 +020024#include "qapi/error.h"
Pavel Butsykin6bde8fd2015-09-22 16:18:21 +030025#include "monitor/monitor.h"
Paolo Bonzinid613f8c2015-12-04 11:04:13 +010026#include "hw/i386/apic.h"
Paolo Bonzini852c27e2019-12-12 17:15:43 +010027#include "hw/i386/x86.h"
28#include "hw/intc/i8259.h"
Bernhard Beschow7f546402023-02-13 18:30:31 +010029#include "hw/intc/ioapic.h"
30#include "hw/intc/ioapic_internal.h"
Michael S. Tsirkin455e17a2018-05-03 22:50:32 +030031#include "hw/pci/msi.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020032#include "hw/qdev-properties.h"
Paolo Bonzini15eafc22015-12-17 17:16:08 +010033#include "sysemu/kvm.h"
Markus Armbruster46517dd2019-08-12 07:23:57 +020034#include "sysemu/sysemu.h"
Peter Xucb135f52016-07-14 13:56:23 +080035#include "hw/i386/apic-msidef.h"
Peter Xue3d9c922016-07-14 13:56:27 +080036#include "hw/i386/x86-iommu.h"
Peter Xue5074b32017-01-09 16:55:51 +080037#include "trace.h"
aliguori610626a2009-03-12 20:25:12 +000038
Paolo Bonzini15eafc22015-12-17 17:16:08 +010039#define APIC_DELIVERY_MODE_SHIFT 8
40#define APIC_POLARITY_SHIFT 14
41#define APIC_TRIG_MODE_SHIFT 15
42
Jan Kiszka244ac3a2011-10-16 19:38:22 +020043static IOAPICCommonState *ioapics[MAX_IOAPICS];
Jan Kiszka0280b572011-02-03 22:54:11 +010044
xiaoqiang zhaodb0f8882013-11-05 18:16:05 +080045/* global variable from ioapic_common.c */
46extern int ioapic_no;
47
Peter Xuc15fa0b2016-07-14 13:56:24 +080048struct ioapic_entry_info {
49 /* fields parsed from IOAPIC entries */
50 uint8_t masked;
51 uint8_t trig_mode;
52 uint16_t dest_idx;
53 uint8_t dest_mode;
54 uint8_t delivery_mode;
55 uint8_t vector;
56
57 /* MSI message generated from above parsed fields */
58 uint32_t addr;
59 uint32_t data;
60};
61
62static void ioapic_entry_parse(uint64_t entry, struct ioapic_entry_info *info)
63{
64 memset(info, 0, sizeof(*info));
65 info->masked = (entry >> IOAPIC_LVT_MASKED_SHIFT) & 1;
66 info->trig_mode = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
67 /*
68 * By default, this would be dest_id[8] + reserved[8]. When IR
69 * is enabled, this would be interrupt_index[15] +
70 * interrupt_format[1]. This field never means anything, but
71 * only used to generate corresponding MSI.
72 */
73 info->dest_idx = (entry >> IOAPIC_LVT_DEST_IDX_SHIFT) & 0xffff;
74 info->dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
75 info->delivery_mode = (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) \
76 & IOAPIC_DM_MASK;
77 if (info->delivery_mode == IOAPIC_DM_EXTINT) {
78 info->vector = pic_read_irq(isa_pic);
79 } else {
80 info->vector = entry & IOAPIC_VECTOR_MASK;
81 }
82
83 info->addr = APIC_DEFAULT_ADDRESS | \
84 (info->dest_idx << MSI_ADDR_DEST_IDX_SHIFT) | \
85 (info->dest_mode << MSI_ADDR_DEST_MODE_SHIFT);
86 info->data = (info->vector << MSI_DATA_VECTOR_SHIFT) | \
87 (info->trig_mode << MSI_DATA_TRIGGER_SHIFT) | \
88 (info->delivery_mode << MSI_DATA_DELIVERY_MODE_SHIFT);
89}
90
Jan Kiszka244ac3a2011-10-16 19:38:22 +020091static void ioapic_service(IOAPICCommonState *s)
aliguori610626a2009-03-12 20:25:12 +000092{
Paolo Bonzinif0bb2762019-10-22 09:39:50 +020093 AddressSpace *ioapic_as = X86_MACHINE(qdev_get_machine())->ioapic_as;
Peter Xuc15fa0b2016-07-14 13:56:24 +080094 struct ioapic_entry_info info;
aliguori610626a2009-03-12 20:25:12 +000095 uint8_t i;
aliguori610626a2009-03-12 20:25:12 +000096 uint32_t mask;
97 uint64_t entry;
aliguori610626a2009-03-12 20:25:12 +000098
99 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
100 mask = 1 << i;
101 if (s->irr & mask) {
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100102 int coalesce = 0;
103
aliguori610626a2009-03-12 20:25:12 +0000104 entry = s->ioredtbl[i];
Peter Xuc15fa0b2016-07-14 13:56:24 +0800105 ioapic_entry_parse(entry, &info);
106 if (!info.masked) {
107 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
aliguori610626a2009-03-12 20:25:12 +0000108 s->irr &= ~mask;
Jan Kiszka0280b572011-02-03 22:54:11 +0100109 } else {
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100110 coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
Peter Xue5074b32017-01-09 16:55:51 +0800111 trace_ioapic_set_remote_irr(i);
Jan Kiszka0280b572011-02-03 22:54:11 +0100112 s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
113 }
Peter Xuc15fa0b2016-07-14 13:56:24 +0800114
Peter Xuf99b86b2016-07-31 22:18:05 +0800115 if (coalesce) {
116 /* We are level triggered interrupts, and the
117 * guest should be still working on previous one,
118 * so skip it. */
119 continue;
120 }
121
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100122#ifdef CONFIG_KVM
123 if (kvm_irqchip_is_split()) {
Peter Xuc15fa0b2016-07-14 13:56:24 +0800124 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100125 kvm_set_irq(kvm_state, i, 1);
126 kvm_set_irq(kvm_state, i, 0);
127 } else {
Peter Xuf99b86b2016-07-31 22:18:05 +0800128 kvm_set_irq(kvm_state, i, 1);
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100129 }
130 continue;
131 }
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100132#endif
Peter Xuf99b86b2016-07-31 22:18:05 +0800133
Peter Xucb135f52016-07-14 13:56:23 +0800134 /* No matter whether IR is enabled, we translate
135 * the IOAPIC message into a MSI one, and its
136 * address space will decide whether we need a
137 * translation. */
Peter Xuc15fa0b2016-07-14 13:56:24 +0800138 stl_le_phys(ioapic_as, info.addr, info.data);
aliguori610626a2009-03-12 20:25:12 +0000139 }
140 }
141 }
142}
143
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200144#define SUCCESSIVE_IRQ_MAX_COUNT 10000
145
146static void delayed_ioapic_service_cb(void *opaque)
147{
148 IOAPICCommonState *s = opaque;
149
150 ioapic_service(s);
151}
152
Blue Swirl7d0500c2010-06-17 16:32:47 +0000153static void ioapic_set_irq(void *opaque, int vector, int level)
aliguori610626a2009-03-12 20:25:12 +0000154{
Jan Kiszka244ac3a2011-10-16 19:38:22 +0200155 IOAPICCommonState *s = opaque;
aliguori610626a2009-03-12 20:25:12 +0000156
157 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
158 * to GSI 2. GSI maps to ioapic 1-1. This is not
159 * the cleanest way of doing it but it should work. */
160
Dr. David Alan Gilberta2e6ffa2017-11-02 18:03:10 +0000161 trace_ioapic_set_irq(vector, level);
Peter Xucce54052017-12-29 15:31:03 +0800162 ioapic_stat_update_irq(s, vector, level);
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100163 if (vector == 0) {
aliguori610626a2009-03-12 20:25:12 +0000164 vector = 2;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100165 }
Paolo Bonzini960a4792018-07-04 14:03:10 +0200166 if (vector < IOAPIC_NUM_PINS) {
aliguori610626a2009-03-12 20:25:12 +0000167 uint32_t mask = 1 << vector;
168 uint64_t entry = s->ioredtbl[vector];
169
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100170 if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
171 IOAPIC_TRIGGER_LEVEL) {
aliguori610626a2009-03-12 20:25:12 +0000172 /* level triggered */
173 if (level) {
174 s->irr |= mask;
Paolo Bonzinic5955a52015-07-30 10:19:24 +0200175 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
176 ioapic_service(s);
177 }
aliguori610626a2009-03-12 20:25:12 +0000178 } else {
179 s->irr &= ~mask;
180 }
181 } else {
Jan Kiszka47f7be32011-04-09 13:18:59 +0200182 /* According to the 82093AA manual, we must ignore edge requests
183 * if the input pin is masked. */
184 if (level && !(entry & IOAPIC_LVT_MASKED)) {
aliguori610626a2009-03-12 20:25:12 +0000185 s->irr |= mask;
186 ioapic_service(s);
187 }
188 }
189 }
190}
191
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100192static void ioapic_update_kvm_routes(IOAPICCommonState *s)
193{
194#ifdef CONFIG_KVM
195 int i;
196
197 if (kvm_irqchip_is_split()) {
198 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100199 MSIMessage msg;
Peter Xuc15fa0b2016-07-14 13:56:24 +0800200 struct ioapic_entry_info info;
201 ioapic_entry_parse(s->ioredtbl[i], &info);
Jan Kiszkabe1927c2019-06-02 13:42:13 +0200202 if (!info.masked) {
203 msg.address = info.addr;
204 msg.data = info.data;
205 kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
206 }
Paolo Bonzini15eafc22015-12-17 17:16:08 +0100207 }
208 kvm_irqchip_commit_routes(kvm_state);
209 }
210#endif
211}
212
Peter Xue3d9c922016-07-14 13:56:27 +0800213#ifdef CONFIG_KVM
214static void ioapic_iec_notifier(void *private, bool global,
215 uint32_t index, uint32_t mask)
216{
217 IOAPICCommonState *s = (IOAPICCommonState *)private;
218 /* For simplicity, we just update all the routes */
219 ioapic_update_kvm_routes(s);
220}
221#endif
222
Jan Kiszka0280b572011-02-03 22:54:11 +0100223void ioapic_eoi_broadcast(int vector)
224{
Jan Kiszka244ac3a2011-10-16 19:38:22 +0200225 IOAPICCommonState *s;
Jan Kiszka0280b572011-02-03 22:54:11 +0100226 uint64_t entry;
227 int i, n;
228
Peter Xue5074b32017-01-09 16:55:51 +0800229 trace_ioapic_eoi_broadcast(vector);
230
Jan Kiszka0280b572011-02-03 22:54:11 +0100231 for (i = 0; i < MAX_IOAPICS; i++) {
232 s = ioapics[i];
233 if (!s) {
234 continue;
235 }
236 for (n = 0; n < IOAPIC_NUM_PINS; n++) {
237 entry = s->ioredtbl[n];
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200238
239 if ((entry & IOAPIC_VECTOR_MASK) != vector ||
240 ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) != IOAPIC_TRIGGER_LEVEL) {
241 continue;
242 }
243
Peter Xuc82d9d42020-03-18 10:52:03 -0400244#ifdef CONFIG_KVM
245 /*
246 * When IOAPIC is in the userspace while APIC is still in
247 * the kernel (i.e., split irqchip), we have a trick to
248 * kick the resamplefd logic for registered irqfds from
249 * userspace to deactivate the IRQ. When that happens, it
250 * means the irq bypassed userspace IOAPIC (so the irr and
251 * remote-irr of the table entry should be bypassed too
252 * even if interrupt come). Still kick the resamplefds if
253 * they're bound to the IRQ, to make sure to EOI the
254 * interrupt for the hardware correctly.
255 *
256 * Note: We still need to go through the irr & remote-irr
257 * operations below because we don't know whether there're
258 * emulated devices that are using/sharing the same IRQ.
259 */
260 kvm_resample_fd_notify(n);
261#endif
262
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200263 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
264 continue;
265 }
266
267 trace_ioapic_clear_remote_irr(n, vector);
268 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
269
270 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
Li Qiang03f990a2019-06-21 17:21:19 -0700271 ++s->irq_eoi[n];
272 if (s->irq_eoi[n] >= SUCCESSIVE_IRQ_MAX_COUNT) {
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200273 /*
274 * Real hardware does not deliver the interrupt immediately
275 * during eoi broadcast, and this lets a buggy guest make
276 * slow progress even if it does not correctly handle a
277 * level-triggered interrupt. Emulate this behavior if we
278 * detect an interrupt storm.
279 */
Li Qiang03f990a2019-06-21 17:21:19 -0700280 s->irq_eoi[n] = 0;
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200281 timer_mod_anticipate(s->delayed_ioapic_service_timer,
282 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
283 NANOSECONDS_PER_SECOND / 100);
Li Qiang03f990a2019-06-21 17:21:19 -0700284 trace_ioapic_eoi_delayed_reassert(n);
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200285 } else {
Jan Kiszka0280b572011-02-03 22:54:11 +0100286 ioapic_service(s);
287 }
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200288 } else {
Li Qiang03f990a2019-06-21 17:21:19 -0700289 s->irq_eoi[n] = 0;
Jan Kiszka0280b572011-02-03 22:54:11 +0100290 }
291 }
292 }
293}
294
Jan Kiszka4d5bf5f2011-10-17 13:11:27 +0200295static uint64_t
Avi Kivitya8170e52012-10-23 12:30:10 +0200296ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
aliguori610626a2009-03-12 20:25:12 +0000297{
Jan Kiszka244ac3a2011-10-16 19:38:22 +0200298 IOAPICCommonState *s = opaque;
aliguori610626a2009-03-12 20:25:12 +0000299 int index;
300 uint32_t val = 0;
301
Peter Xue5074b32017-01-09 16:55:51 +0800302 addr &= 0xff;
303
304 switch (addr) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100305 case IOAPIC_IOREGSEL:
aliguori610626a2009-03-12 20:25:12 +0000306 val = s->ioregsel;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100307 break;
308 case IOAPIC_IOWIN:
Jan Kiszka1a440962011-10-17 13:11:29 +0200309 if (size != 4) {
310 break;
311 }
aliguori610626a2009-03-12 20:25:12 +0000312 switch (s->ioregsel) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100313 case IOAPIC_REG_ID:
Paolo Bonzini2f5a3b12015-07-30 10:21:00 +0200314 case IOAPIC_REG_ARB:
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100315 val = s->id << IOAPIC_ID_SHIFT;
316 break;
317 case IOAPIC_REG_VER:
Peter Xu20fd4b72016-08-01 21:59:19 +0800318 val = s->version |
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100319 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
320 break;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100321 default:
322 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
323 if (index >= 0 && index < IOAPIC_NUM_PINS) {
324 if (s->ioregsel & 1) {
325 val = s->ioredtbl[index] >> 32;
326 } else {
327 val = s->ioredtbl[index] & 0xffffffff;
aliguori610626a2009-03-12 20:25:12 +0000328 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100329 }
aliguori610626a2009-03-12 20:25:12 +0000330 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100331 break;
aliguori610626a2009-03-12 20:25:12 +0000332 }
Peter Xue5074b32017-01-09 16:55:51 +0800333
Dr. David Alan Gilberta2e6ffa2017-11-02 18:03:10 +0000334 trace_ioapic_mem_read(addr, s->ioregsel, size, val);
Peter Xue5074b32017-01-09 16:55:51 +0800335
aliguori610626a2009-03-12 20:25:12 +0000336 return val;
337}
338
Peter Xued1263c2016-05-10 18:21:22 +0800339/*
340 * This is to satisfy the hack in Linux kernel. One hack of it is to
341 * simulate clearing the Remote IRR bit of IOAPIC entry using the
342 * following:
343 *
344 * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
345 * Otherwise, we simulate the EOI message manually by changing the trigger
346 * mode to edge and then back to level, with RTE being masked during
347 * this."
348 *
349 * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
350 *
351 * This is based on the assumption that, Remote IRR bit will be
352 * cleared by IOAPIC hardware when configured as edge-triggered
353 * interrupts.
354 *
355 * Without this, level-triggered interrupts in IR mode might fail to
356 * work correctly.
357 */
358static inline void
359ioapic_fix_edge_remote_irr(uint64_t *entry)
360{
361 if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
362 /* Edge-triggered interrupts, make sure remote IRR is zero */
363 *entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
364 }
365}
366
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100367static void
Avi Kivitya8170e52012-10-23 12:30:10 +0200368ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
Jan Kiszka4d5bf5f2011-10-17 13:11:27 +0200369 unsigned int size)
aliguori610626a2009-03-12 20:25:12 +0000370{
Jan Kiszka244ac3a2011-10-16 19:38:22 +0200371 IOAPICCommonState *s = opaque;
aliguori610626a2009-03-12 20:25:12 +0000372 int index;
373
Peter Xue5074b32017-01-09 16:55:51 +0800374 addr &= 0xff;
Dr. David Alan Gilberta2e6ffa2017-11-02 18:03:10 +0000375 trace_ioapic_mem_write(addr, s->ioregsel, size, val);
Peter Xue5074b32017-01-09 16:55:51 +0800376
377 switch (addr) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100378 case IOAPIC_IOREGSEL:
aliguori610626a2009-03-12 20:25:12 +0000379 s->ioregsel = val;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100380 break;
381 case IOAPIC_IOWIN:
Jan Kiszka1a440962011-10-17 13:11:29 +0200382 if (size != 4) {
383 break;
384 }
aliguori610626a2009-03-12 20:25:12 +0000385 switch (s->ioregsel) {
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100386 case IOAPIC_REG_ID:
387 s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
388 break;
389 case IOAPIC_REG_VER:
390 case IOAPIC_REG_ARB:
391 break;
392 default:
393 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
394 if (index >= 0 && index < IOAPIC_NUM_PINS) {
Peter Xu479c2a12016-05-10 18:21:21 +0800395 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100396 if (s->ioregsel & 1) {
397 s->ioredtbl[index] &= 0xffffffff;
398 s->ioredtbl[index] |= (uint64_t)val << 32;
399 } else {
400 s->ioredtbl[index] &= ~0xffffffffULL;
401 s->ioredtbl[index] |= val;
aliguori610626a2009-03-12 20:25:12 +0000402 }
Peter Xu479c2a12016-05-10 18:21:21 +0800403 /* restore RO bits */
404 s->ioredtbl[index] &= IOAPIC_RW_BITS;
405 s->ioredtbl[index] |= ro_bits;
Li Qiangd15d3d52019-06-24 08:16:35 -0700406 s->irq_eoi[index] = 0;
Peter Xued1263c2016-05-10 18:21:22 +0800407 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
David Woodhouse54ad31f2023-03-08 11:19:50 +0000408 ioapic_update_kvm_routes(s);
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100409 ioapic_service(s);
410 }
aliguori610626a2009-03-12 20:25:12 +0000411 }
Jan Kiszka1f5e71a2011-02-03 22:54:14 +0100412 break;
Peter Xu20fd4b72016-08-01 21:59:19 +0800413 case IOAPIC_EOI:
414 /* Explicit EOI is only supported for IOAPIC version 0x20 */
415 if (size != 4 || s->version != 0x20) {
416 break;
417 }
418 ioapic_eoi_broadcast(val);
419 break;
aliguori610626a2009-03-12 20:25:12 +0000420 }
421}
422
Jan Kiszka4d5bf5f2011-10-17 13:11:27 +0200423static const MemoryRegionOps ioapic_io_ops = {
424 .read = ioapic_mem_read,
425 .write = ioapic_mem_write,
426 .endianness = DEVICE_NATIVE_ENDIAN,
aliguori610626a2009-03-12 20:25:12 +0000427};
428
Peter Xue3d9c922016-07-14 13:56:27 +0800429static void ioapic_machine_done_notify(Notifier *notifier, void *data)
430{
431#ifdef CONFIG_KVM
432 IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
433 machine_done);
434
435 if (kvm_irqchip_is_split()) {
436 X86IOMMUState *iommu = x86_iommu_get_default();
437 if (iommu) {
438 /* Register this IOAPIC with IOMMU IEC notifier, so that
439 * when there are IR invalidates, we can be notified to
440 * update kernel IR cache. */
441 x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
442 }
443 }
444#endif
445}
446
Peter Xu8d5516b2017-02-03 15:18:17 +0800447#define IOAPIC_VER_DEF 0x20
448
xiaoqiang zhaodb0f8882013-11-05 18:16:05 +0800449static void ioapic_realize(DeviceState *dev, Error **errp)
aliguori610626a2009-03-12 20:25:12 +0000450{
xiaoqiang zhaodb0f8882013-11-05 18:16:05 +0800451 IOAPICCommonState *s = IOAPIC_COMMON(dev);
xiaoqiang zhaof9771852013-11-05 18:16:04 +0800452
Peter Xu20fd4b72016-08-01 21:59:19 +0800453 if (s->version != 0x11 && s->version != 0x20) {
Markus Armbruster11ab69d2018-10-17 10:26:34 +0200454 error_setg(errp, "IOAPIC only supports version 0x11 or 0x20 "
455 "(default: 0x%x).", IOAPIC_VER_DEF);
456 return;
Peter Xu20fd4b72016-08-01 21:59:19 +0800457 }
458
Paolo Bonzini1437c942013-06-06 21:25:08 -0400459 memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
460 "ioapic", 0x1000);
aliguori610626a2009-03-12 20:25:12 +0000461
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200462 s->delayed_ioapic_service_timer =
463 timer_new_ns(QEMU_CLOCK_VIRTUAL, delayed_ioapic_service_cb, s);
464
xiaoqiang zhaof9771852013-11-05 18:16:04 +0800465 qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
aliguori610626a2009-03-12 20:25:12 +0000466
xiaoqiang zhaodb0f8882013-11-05 18:16:05 +0800467 ioapics[ioapic_no] = s;
Peter Xue3d9c922016-07-14 13:56:27 +0800468 s->machine_done.notify = ioapic_machine_done_notify;
469 qemu_add_machine_init_done_notifier(&s->machine_done);
aliguori610626a2009-03-12 20:25:12 +0000470}
Blue Swirl96051112010-06-19 07:41:43 +0000471
Markus Armbrusterb69c3c22020-05-05 17:29:24 +0200472static void ioapic_unrealize(DeviceState *dev)
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200473{
474 IOAPICCommonState *s = IOAPIC_COMMON(dev);
475
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200476 timer_free(s->delayed_ioapic_service_timer);
477}
478
Peter Xu20fd4b72016-08-01 21:59:19 +0800479static Property ioapic_properties[] = {
Peter Xu8d5516b2017-02-03 15:18:17 +0800480 DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF),
Peter Xu20fd4b72016-08-01 21:59:19 +0800481 DEFINE_PROP_END_OF_LIST(),
482};
483
Anthony Liguori999e12b2012-01-24 13:12:29 -0600484static void ioapic_class_init(ObjectClass *klass, void *data)
485{
486 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600487 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600488
xiaoqiang zhaodb0f8882013-11-05 18:16:05 +0800489 k->realize = ioapic_realize;
Vitaly Kuznetsov958a01d2019-04-02 10:02:15 +0200490 k->unrealize = ioapic_unrealize;
Peter Xu0f254b12017-01-09 16:55:53 +0800491 /*
492 * If APIC is in kernel, we need to update the kernel cache after
493 * migration, otherwise first 24 gsi routes will be invalid.
494 */
495 k->post_load = ioapic_update_kvm_routes;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600496 dc->reset = ioapic_reset_common;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400497 device_class_set_props(dc, ioapic_properties);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600498}
499
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100500static const TypeInfo ioapic_info = {
Li Qiang34bec7a2019-01-04 18:38:31 -0800501 .name = TYPE_IOAPIC,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600502 .parent = TYPE_IOAPIC_COMMON,
503 .instance_size = sizeof(IOAPICCommonState),
504 .class_init = ioapic_class_init,
Blue Swirl96051112010-06-19 07:41:43 +0000505};
506
Andreas Färber83f7d432012-02-09 15:20:55 +0100507static void ioapic_register_types(void)
Blue Swirl96051112010-06-19 07:41:43 +0000508{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600509 type_register_static(&ioapic_info);
Blue Swirl96051112010-06-19 07:41:43 +0000510}
511
Andreas Färber83f7d432012-02-09 15:20:55 +0100512type_init(ioapic_register_types)