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