]> Git Repo - qemu.git/blame - hw/intc/ioapic.c
xics: setup cpu at realize time
[qemu.git] / hw / intc / ioapic.c
CommitLineData
610626af
AL
1/*
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 <[email protected]>
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
12 * version 2 of the License, or (at your option) any later version.
13 *
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
8167ee88 20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
610626af
AL
21 */
22
b6a0aa05 23#include "qemu/osdep.h"
20fd4b7b 24#include "qemu/error-report.h"
6bde8fd6 25#include "monitor/monitor.h"
83c9f4ca 26#include "hw/hw.h"
0d09e41a 27#include "hw/i386/pc.h"
d613f8cc 28#include "hw/i386/apic.h"
0d09e41a
PB
29#include "hw/i386/ioapic.h"
30#include "hw/i386/ioapic_internal.h"
15eafc2e
PB
31#include "include/hw/pci/msi.h"
32#include "sysemu/kvm.h"
fcf5ef2a 33#include "target/i386/cpu.h"
cb135f59 34#include "hw/i386/apic-msidef.h"
e3d9c925 35#include "hw/i386/x86-iommu.h"
e5074b38 36#include "trace.h"
610626af
AL
37
38//#define DEBUG_IOAPIC
39
9af9b330
BS
40#ifdef DEBUG_IOAPIC
41#define DPRINTF(fmt, ...) \
42 do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
43#else
44#define DPRINTF(fmt, ...)
45#endif
46
15eafc2e
PB
47#define APIC_DELIVERY_MODE_SHIFT 8
48#define APIC_POLARITY_SHIFT 14
49#define APIC_TRIG_MODE_SHIFT 15
50
244ac3af 51static IOAPICCommonState *ioapics[MAX_IOAPICS];
0280b571 52
db0f8888
XZ
53/* global variable from ioapic_common.c */
54extern int ioapic_no;
55
c15fa0be
PX
56struct ioapic_entry_info {
57 /* fields parsed from IOAPIC entries */
58 uint8_t masked;
59 uint8_t trig_mode;
60 uint16_t dest_idx;
61 uint8_t dest_mode;
62 uint8_t delivery_mode;
63 uint8_t vector;
64
65 /* MSI message generated from above parsed fields */
66 uint32_t addr;
67 uint32_t data;
68};
69
70static void ioapic_entry_parse(uint64_t entry, struct ioapic_entry_info *info)
71{
72 memset(info, 0, sizeof(*info));
73 info->masked = (entry >> IOAPIC_LVT_MASKED_SHIFT) & 1;
74 info->trig_mode = (entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1;
75 /*
76 * By default, this would be dest_id[8] + reserved[8]. When IR
77 * is enabled, this would be interrupt_index[15] +
78 * interrupt_format[1]. This field never means anything, but
79 * only used to generate corresponding MSI.
80 */
81 info->dest_idx = (entry >> IOAPIC_LVT_DEST_IDX_SHIFT) & 0xffff;
82 info->dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
83 info->delivery_mode = (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) \
84 & IOAPIC_DM_MASK;
85 if (info->delivery_mode == IOAPIC_DM_EXTINT) {
86 info->vector = pic_read_irq(isa_pic);
87 } else {
88 info->vector = entry & IOAPIC_VECTOR_MASK;
89 }
90
91 info->addr = APIC_DEFAULT_ADDRESS | \
92 (info->dest_idx << MSI_ADDR_DEST_IDX_SHIFT) | \
93 (info->dest_mode << MSI_ADDR_DEST_MODE_SHIFT);
94 info->data = (info->vector << MSI_DATA_VECTOR_SHIFT) | \
95 (info->trig_mode << MSI_DATA_TRIGGER_SHIFT) | \
96 (info->delivery_mode << MSI_DATA_DELIVERY_MODE_SHIFT);
97}
98
244ac3af 99static void ioapic_service(IOAPICCommonState *s)
610626af 100{
cb135f59 101 AddressSpace *ioapic_as = PC_MACHINE(qdev_get_machine())->ioapic_as;
c15fa0be 102 struct ioapic_entry_info info;
610626af 103 uint8_t i;
610626af
AL
104 uint32_t mask;
105 uint64_t entry;
610626af
AL
106
107 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
108 mask = 1 << i;
109 if (s->irr & mask) {
15eafc2e
PB
110 int coalesce = 0;
111
610626af 112 entry = s->ioredtbl[i];
c15fa0be
PX
113 ioapic_entry_parse(entry, &info);
114 if (!info.masked) {
115 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
610626af 116 s->irr &= ~mask;
0280b571 117 } else {
15eafc2e 118 coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR;
e5074b38 119 trace_ioapic_set_remote_irr(i);
0280b571
JK
120 s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
121 }
c15fa0be 122
f99b86b9
PX
123 if (coalesce) {
124 /* We are level triggered interrupts, and the
125 * guest should be still working on previous one,
126 * so skip it. */
127 continue;
128 }
129
15eafc2e
PB
130#ifdef CONFIG_KVM
131 if (kvm_irqchip_is_split()) {
c15fa0be 132 if (info.trig_mode == IOAPIC_TRIGGER_EDGE) {
15eafc2e
PB
133 kvm_set_irq(kvm_state, i, 1);
134 kvm_set_irq(kvm_state, i, 0);
135 } else {
f99b86b9 136 kvm_set_irq(kvm_state, i, 1);
15eafc2e
PB
137 }
138 continue;
139 }
15eafc2e 140#endif
f99b86b9 141
cb135f59
PX
142 /* No matter whether IR is enabled, we translate
143 * the IOAPIC message into a MSI one, and its
144 * address space will decide whether we need a
145 * translation. */
c15fa0be 146 stl_le_phys(ioapic_as, info.addr, info.data);
610626af
AL
147 }
148 }
149 }
150}
151
7d0500c4 152static void ioapic_set_irq(void *opaque, int vector, int level)
610626af 153{
244ac3af 154 IOAPICCommonState *s = opaque;
610626af
AL
155
156 /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
157 * to GSI 2. GSI maps to ioapic 1-1. This is not
158 * the cleanest way of doing it but it should work. */
159
1f5e71a8
JK
160 DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
161 if (vector == 0) {
610626af 162 vector = 2;
1f5e71a8 163 }
610626af
AL
164 if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
165 uint32_t mask = 1 << vector;
166 uint64_t entry = s->ioredtbl[vector];
167
1f5e71a8
JK
168 if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
169 IOAPIC_TRIGGER_LEVEL) {
610626af
AL
170 /* level triggered */
171 if (level) {
172 s->irr |= mask;
c5955a56
PB
173 if (!(entry & IOAPIC_LVT_REMOTE_IRR)) {
174 ioapic_service(s);
175 }
610626af
AL
176 } else {
177 s->irr &= ~mask;
178 }
179 } else {
47f7be39
JK
180 /* According to the 82093AA manual, we must ignore edge requests
181 * if the input pin is masked. */
182 if (level && !(entry & IOAPIC_LVT_MASKED)) {
610626af
AL
183 s->irr |= mask;
184 ioapic_service(s);
185 }
186 }
187 }
188}
189
15eafc2e
PB
190static void ioapic_update_kvm_routes(IOAPICCommonState *s)
191{
192#ifdef CONFIG_KVM
193 int i;
194
195 if (kvm_irqchip_is_split()) {
196 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
15eafc2e 197 MSIMessage msg;
c15fa0be
PX
198 struct ioapic_entry_info info;
199 ioapic_entry_parse(s->ioredtbl[i], &info);
200 msg.address = info.addr;
201 msg.data = info.data;
15eafc2e
PB
202 kvm_irqchip_update_msi_route(kvm_state, i, msg, NULL);
203 }
204 kvm_irqchip_commit_routes(kvm_state);
205 }
206#endif
207}
208
e3d9c925
PX
209#ifdef CONFIG_KVM
210static void ioapic_iec_notifier(void *private, bool global,
211 uint32_t index, uint32_t mask)
212{
213 IOAPICCommonState *s = (IOAPICCommonState *)private;
214 /* For simplicity, we just update all the routes */
215 ioapic_update_kvm_routes(s);
216}
217#endif
218
0280b571
JK
219void ioapic_eoi_broadcast(int vector)
220{
244ac3af 221 IOAPICCommonState *s;
0280b571
JK
222 uint64_t entry;
223 int i, n;
224
e5074b38
PX
225 trace_ioapic_eoi_broadcast(vector);
226
0280b571
JK
227 for (i = 0; i < MAX_IOAPICS; i++) {
228 s = ioapics[i];
229 if (!s) {
230 continue;
231 }
232 for (n = 0; n < IOAPIC_NUM_PINS; n++) {
233 entry = s->ioredtbl[n];
1f5e71a8
JK
234 if ((entry & IOAPIC_LVT_REMOTE_IRR)
235 && (entry & IOAPIC_VECTOR_MASK) == vector) {
e5074b38 236 trace_ioapic_clear_remote_irr(n, vector);
0280b571
JK
237 s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
238 if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
239 ioapic_service(s);
240 }
241 }
242 }
243 }
244}
245
6bde8fd6
PB
246void ioapic_dump_state(Monitor *mon, const QDict *qdict)
247{
248 int i;
249
250 for (i = 0; i < MAX_IOAPICS; i++) {
251 if (ioapics[i] != 0) {
252 ioapic_print_redtbl(mon, ioapics[i]);
253 }
254 }
255}
256
4d5bf5f6 257static uint64_t
a8170e5e 258ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
610626af 259{
244ac3af 260 IOAPICCommonState *s = opaque;
610626af
AL
261 int index;
262 uint32_t val = 0;
263
e5074b38
PX
264 addr &= 0xff;
265
266 switch (addr) {
1f5e71a8 267 case IOAPIC_IOREGSEL:
610626af 268 val = s->ioregsel;
1f5e71a8
JK
269 break;
270 case IOAPIC_IOWIN:
1a440963
JK
271 if (size != 4) {
272 break;
273 }
610626af 274 switch (s->ioregsel) {
1f5e71a8 275 case IOAPIC_REG_ID:
2f5a3b12 276 case IOAPIC_REG_ARB:
1f5e71a8
JK
277 val = s->id << IOAPIC_ID_SHIFT;
278 break;
279 case IOAPIC_REG_VER:
20fd4b7b 280 val = s->version |
1f5e71a8
JK
281 ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
282 break;
1f5e71a8
JK
283 default:
284 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
285 if (index >= 0 && index < IOAPIC_NUM_PINS) {
286 if (s->ioregsel & 1) {
287 val = s->ioredtbl[index] >> 32;
288 } else {
289 val = s->ioredtbl[index] & 0xffffffff;
610626af 290 }
1f5e71a8 291 }
610626af 292 }
9af9b330 293 DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
1f5e71a8 294 break;
610626af 295 }
e5074b38
PX
296
297 trace_ioapic_mem_read(addr, size, val);
298
610626af
AL
299 return val;
300}
301
ed1263c3
PX
302/*
303 * This is to satisfy the hack in Linux kernel. One hack of it is to
304 * simulate clearing the Remote IRR bit of IOAPIC entry using the
305 * following:
306 *
307 * "For IO-APIC's with EOI register, we use that to do an explicit EOI.
308 * Otherwise, we simulate the EOI message manually by changing the trigger
309 * mode to edge and then back to level, with RTE being masked during
310 * this."
311 *
312 * (See linux kernel __eoi_ioapic_pin() comment in commit c0205701)
313 *
314 * This is based on the assumption that, Remote IRR bit will be
315 * cleared by IOAPIC hardware when configured as edge-triggered
316 * interrupts.
317 *
318 * Without this, level-triggered interrupts in IR mode might fail to
319 * work correctly.
320 */
321static inline void
322ioapic_fix_edge_remote_irr(uint64_t *entry)
323{
324 if (!(*entry & IOAPIC_LVT_TRIGGER_MODE)) {
325 /* Edge-triggered interrupts, make sure remote IRR is zero */
326 *entry &= ~((uint64_t)IOAPIC_LVT_REMOTE_IRR);
327 }
328}
329
1f5e71a8 330static void
a8170e5e 331ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
4d5bf5f6 332 unsigned int size)
610626af 333{
244ac3af 334 IOAPICCommonState *s = opaque;
610626af
AL
335 int index;
336
e5074b38
PX
337 addr &= 0xff;
338 trace_ioapic_mem_write(addr, size, val);
339
340 switch (addr) {
1f5e71a8 341 case IOAPIC_IOREGSEL:
610626af 342 s->ioregsel = val;
1f5e71a8
JK
343 break;
344 case IOAPIC_IOWIN:
1a440963
JK
345 if (size != 4) {
346 break;
347 }
0c1f781b 348 DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
610626af 349 switch (s->ioregsel) {
1f5e71a8
JK
350 case IOAPIC_REG_ID:
351 s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
352 break;
353 case IOAPIC_REG_VER:
354 case IOAPIC_REG_ARB:
355 break;
356 default:
357 index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
358 if (index >= 0 && index < IOAPIC_NUM_PINS) {
479c2a1c 359 uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS;
1f5e71a8
JK
360 if (s->ioregsel & 1) {
361 s->ioredtbl[index] &= 0xffffffff;
362 s->ioredtbl[index] |= (uint64_t)val << 32;
363 } else {
364 s->ioredtbl[index] &= ~0xffffffffULL;
365 s->ioredtbl[index] |= val;
610626af 366 }
479c2a1c
PX
367 /* restore RO bits */
368 s->ioredtbl[index] &= IOAPIC_RW_BITS;
369 s->ioredtbl[index] |= ro_bits;
ed1263c3 370 ioapic_fix_edge_remote_irr(&s->ioredtbl[index]);
1f5e71a8
JK
371 ioapic_service(s);
372 }
610626af 373 }
1f5e71a8 374 break;
20fd4b7b
PX
375 case IOAPIC_EOI:
376 /* Explicit EOI is only supported for IOAPIC version 0x20 */
377 if (size != 4 || s->version != 0x20) {
378 break;
379 }
380 ioapic_eoi_broadcast(val);
381 break;
610626af 382 }
15eafc2e
PB
383
384 ioapic_update_kvm_routes(s);
610626af
AL
385}
386
4d5bf5f6
JK
387static const MemoryRegionOps ioapic_io_ops = {
388 .read = ioapic_mem_read,
389 .write = ioapic_mem_write,
390 .endianness = DEVICE_NATIVE_ENDIAN,
610626af
AL
391};
392
e3d9c925
PX
393static void ioapic_machine_done_notify(Notifier *notifier, void *data)
394{
395#ifdef CONFIG_KVM
396 IOAPICCommonState *s = container_of(notifier, IOAPICCommonState,
397 machine_done);
398
399 if (kvm_irqchip_is_split()) {
400 X86IOMMUState *iommu = x86_iommu_get_default();
401 if (iommu) {
402 /* Register this IOAPIC with IOMMU IEC notifier, so that
403 * when there are IR invalidates, we can be notified to
404 * update kernel IR cache. */
405 x86_iommu_iec_register_notifier(iommu, ioapic_iec_notifier, s);
406 }
407 }
408#endif
409}
410
8d5516be
PX
411#define IOAPIC_VER_DEF 0x20
412
db0f8888 413static void ioapic_realize(DeviceState *dev, Error **errp)
610626af 414{
db0f8888 415 IOAPICCommonState *s = IOAPIC_COMMON(dev);
f9771858 416
20fd4b7b
PX
417 if (s->version != 0x11 && s->version != 0x20) {
418 error_report("IOAPIC only supports version 0x11 or 0x20 "
8d5516be 419 "(default: 0x%x).", IOAPIC_VER_DEF);
20fd4b7b
PX
420 exit(1);
421 }
422
1437c94b
PB
423 memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
424 "ioapic", 0x1000);
610626af 425
f9771858 426 qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
0280b571 427
db0f8888 428 ioapics[ioapic_no] = s;
e3d9c925
PX
429 s->machine_done.notify = ioapic_machine_done_notify;
430 qemu_add_machine_init_done_notifier(&s->machine_done);
610626af 431}
96051119 432
20fd4b7b 433static Property ioapic_properties[] = {
8d5516be 434 DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF),
20fd4b7b
PX
435 DEFINE_PROP_END_OF_LIST(),
436};
437
999e12bb
AL
438static void ioapic_class_init(ObjectClass *klass, void *data)
439{
440 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
39bffca2 441 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 442
db0f8888 443 k->realize = ioapic_realize;
0f254b1a
PX
444 /*
445 * If APIC is in kernel, we need to update the kernel cache after
446 * migration, otherwise first 24 gsi routes will be invalid.
447 */
448 k->post_load = ioapic_update_kvm_routes;
39bffca2 449 dc->reset = ioapic_reset_common;
20fd4b7b 450 dc->props = ioapic_properties;
999e12bb
AL
451}
452
8c43a6f0 453static const TypeInfo ioapic_info = {
39bffca2
AL
454 .name = "ioapic",
455 .parent = TYPE_IOAPIC_COMMON,
456 .instance_size = sizeof(IOAPICCommonState),
457 .class_init = ioapic_class_init,
96051119
BS
458};
459
83f7d43a 460static void ioapic_register_types(void)
96051119 461{
39bffca2 462 type_register_static(&ioapic_info);
96051119
BS
463}
464
83f7d43a 465type_init(ioapic_register_types)
This page took 0.747025 seconds and 4 git commands to generate.