aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 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 <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 Pant | 61f3c91 | 2020-10-23 12:44:24 +0000 | [diff] [blame] | 12 | * version 2.1 of the License, or (at your option) any later version. |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 20 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Peter Maydell | b6a0aa0 | 2016-01-26 18:17:03 +0000 | [diff] [blame] | 23 | #include "qemu/osdep.h" |
Markus Armbruster | 11ab69d | 2018-10-17 10:26:34 +0200 | [diff] [blame] | 24 | #include "qapi/error.h" |
Pavel Butsykin | 6bde8fd | 2015-09-22 16:18:21 +0300 | [diff] [blame] | 25 | #include "monitor/monitor.h" |
Paolo Bonzini | d613f8c | 2015-12-04 11:04:13 +0100 | [diff] [blame] | 26 | #include "hw/i386/apic.h" |
Paolo Bonzini | 852c27e | 2019-12-12 17:15:43 +0100 | [diff] [blame] | 27 | #include "hw/i386/x86.h" |
| 28 | #include "hw/intc/i8259.h" |
Bernhard Beschow | 7f54640 | 2023-02-13 18:30:31 +0100 | [diff] [blame] | 29 | #include "hw/intc/ioapic.h" |
| 30 | #include "hw/intc/ioapic_internal.h" |
Michael S. Tsirkin | 455e17a | 2018-05-03 22:50:32 +0300 | [diff] [blame] | 31 | #include "hw/pci/msi.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 32 | #include "hw/qdev-properties.h" |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 33 | #include "sysemu/kvm.h" |
Markus Armbruster | 46517dd | 2019-08-12 07:23:57 +0200 | [diff] [blame] | 34 | #include "sysemu/sysemu.h" |
Peter Xu | cb135f5 | 2016-07-14 13:56:23 +0800 | [diff] [blame] | 35 | #include "hw/i386/apic-msidef.h" |
Peter Xu | e3d9c92 | 2016-07-14 13:56:27 +0800 | [diff] [blame] | 36 | #include "hw/i386/x86-iommu.h" |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 37 | #include "trace.h" |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 38 | |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 39 | #define APIC_DELIVERY_MODE_SHIFT 8 |
| 40 | #define APIC_POLARITY_SHIFT 14 |
| 41 | #define APIC_TRIG_MODE_SHIFT 15 |
| 42 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 43 | static IOAPICCommonState *ioapics[MAX_IOAPICS]; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 44 | |
xiaoqiang zhao | db0f888 | 2013-11-05 18:16:05 +0800 | [diff] [blame] | 45 | /* global variable from ioapic_common.c */ |
| 46 | extern int ioapic_no; |
| 47 | |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 48 | struct 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 | |
| 62 | static 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 Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 91 | static void ioapic_service(IOAPICCommonState *s) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 92 | { |
Paolo Bonzini | f0bb276 | 2019-10-22 09:39:50 +0200 | [diff] [blame] | 93 | AddressSpace *ioapic_as = X86_MACHINE(qdev_get_machine())->ioapic_as; |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 94 | struct ioapic_entry_info info; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 95 | uint8_t i; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 96 | uint32_t mask; |
| 97 | uint64_t entry; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
| 100 | mask = 1 << i; |
| 101 | if (s->irr & mask) { |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 102 | int coalesce = 0; |
| 103 | |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 104 | entry = s->ioredtbl[i]; |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 105 | ioapic_entry_parse(entry, &info); |
| 106 | if (!info.masked) { |
| 107 | if (info.trig_mode == IOAPIC_TRIGGER_EDGE) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 108 | s->irr &= ~mask; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 109 | } else { |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 110 | coalesce = s->ioredtbl[i] & IOAPIC_LVT_REMOTE_IRR; |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 111 | trace_ioapic_set_remote_irr(i); |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 112 | s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR; |
| 113 | } |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 114 | |
Peter Xu | f99b86b | 2016-07-31 22:18:05 +0800 | [diff] [blame] | 115 | 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 Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 122 | #ifdef CONFIG_KVM |
| 123 | if (kvm_irqchip_is_split()) { |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 124 | if (info.trig_mode == IOAPIC_TRIGGER_EDGE) { |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 125 | kvm_set_irq(kvm_state, i, 1); |
| 126 | kvm_set_irq(kvm_state, i, 0); |
| 127 | } else { |
Peter Xu | f99b86b | 2016-07-31 22:18:05 +0800 | [diff] [blame] | 128 | kvm_set_irq(kvm_state, i, 1); |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 129 | } |
| 130 | continue; |
| 131 | } |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 132 | #endif |
Peter Xu | f99b86b | 2016-07-31 22:18:05 +0800 | [diff] [blame] | 133 | |
Peter Xu | cb135f5 | 2016-07-14 13:56:23 +0800 | [diff] [blame] | 134 | /* 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 Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 138 | stl_le_phys(ioapic_as, info.addr, info.data); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 144 | #define SUCCESSIVE_IRQ_MAX_COUNT 10000 |
| 145 | |
| 146 | static void delayed_ioapic_service_cb(void *opaque) |
| 147 | { |
| 148 | IOAPICCommonState *s = opaque; |
| 149 | |
| 150 | ioapic_service(s); |
| 151 | } |
| 152 | |
Blue Swirl | 7d0500c | 2010-06-17 16:32:47 +0000 | [diff] [blame] | 153 | static void ioapic_set_irq(void *opaque, int vector, int level) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 154 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 155 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 156 | |
| 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 Gilbert | a2e6ffa | 2017-11-02 18:03:10 +0000 | [diff] [blame] | 161 | trace_ioapic_set_irq(vector, level); |
Peter Xu | cce5405 | 2017-12-29 15:31:03 +0800 | [diff] [blame] | 162 | ioapic_stat_update_irq(s, vector, level); |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 163 | if (vector == 0) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 164 | vector = 2; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 165 | } |
Paolo Bonzini | 960a479 | 2018-07-04 14:03:10 +0200 | [diff] [blame] | 166 | if (vector < IOAPIC_NUM_PINS) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 167 | uint32_t mask = 1 << vector; |
| 168 | uint64_t entry = s->ioredtbl[vector]; |
| 169 | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 170 | if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) == |
| 171 | IOAPIC_TRIGGER_LEVEL) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 172 | /* level triggered */ |
| 173 | if (level) { |
| 174 | s->irr |= mask; |
Paolo Bonzini | c5955a5 | 2015-07-30 10:19:24 +0200 | [diff] [blame] | 175 | if (!(entry & IOAPIC_LVT_REMOTE_IRR)) { |
| 176 | ioapic_service(s); |
| 177 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 178 | } else { |
| 179 | s->irr &= ~mask; |
| 180 | } |
| 181 | } else { |
Jan Kiszka | 47f7be3 | 2011-04-09 13:18:59 +0200 | [diff] [blame] | 182 | /* According to the 82093AA manual, we must ignore edge requests |
| 183 | * if the input pin is masked. */ |
| 184 | if (level && !(entry & IOAPIC_LVT_MASKED)) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 185 | s->irr |= mask; |
| 186 | ioapic_service(s); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Paolo Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 192 | static 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 Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 199 | MSIMessage msg; |
Peter Xu | c15fa0b | 2016-07-14 13:56:24 +0800 | [diff] [blame] | 200 | struct ioapic_entry_info info; |
| 201 | ioapic_entry_parse(s->ioredtbl[i], &info); |
Jan Kiszka | be1927c | 2019-06-02 13:42:13 +0200 | [diff] [blame] | 202 | 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 Bonzini | 15eafc2 | 2015-12-17 17:16:08 +0100 | [diff] [blame] | 207 | } |
| 208 | kvm_irqchip_commit_routes(kvm_state); |
| 209 | } |
| 210 | #endif |
| 211 | } |
| 212 | |
Peter Xu | e3d9c92 | 2016-07-14 13:56:27 +0800 | [diff] [blame] | 213 | #ifdef CONFIG_KVM |
| 214 | static 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 Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 223 | void ioapic_eoi_broadcast(int vector) |
| 224 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 225 | IOAPICCommonState *s; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 226 | uint64_t entry; |
| 227 | int i, n; |
| 228 | |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 229 | trace_ioapic_eoi_broadcast(vector); |
| 230 | |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 231 | 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 Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 238 | |
| 239 | if ((entry & IOAPIC_VECTOR_MASK) != vector || |
| 240 | ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) != IOAPIC_TRIGGER_LEVEL) { |
| 241 | continue; |
| 242 | } |
| 243 | |
Peter Xu | c82d9d4 | 2020-03-18 10:52:03 -0400 | [diff] [blame] | 244 | #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 Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 263 | 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 Qiang | 03f990a | 2019-06-21 17:21:19 -0700 | [diff] [blame] | 271 | ++s->irq_eoi[n]; |
| 272 | if (s->irq_eoi[n] >= SUCCESSIVE_IRQ_MAX_COUNT) { |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 273 | /* |
| 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 Qiang | 03f990a | 2019-06-21 17:21:19 -0700 | [diff] [blame] | 280 | s->irq_eoi[n] = 0; |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 281 | timer_mod_anticipate(s->delayed_ioapic_service_timer, |
| 282 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
| 283 | NANOSECONDS_PER_SECOND / 100); |
Li Qiang | 03f990a | 2019-06-21 17:21:19 -0700 | [diff] [blame] | 284 | trace_ioapic_eoi_delayed_reassert(n); |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 285 | } else { |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 286 | ioapic_service(s); |
| 287 | } |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 288 | } else { |
Li Qiang | 03f990a | 2019-06-21 17:21:19 -0700 | [diff] [blame] | 289 | s->irq_eoi[n] = 0; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 295 | static uint64_t |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 296 | ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 297 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 298 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 299 | int index; |
| 300 | uint32_t val = 0; |
| 301 | |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 302 | addr &= 0xff; |
| 303 | |
| 304 | switch (addr) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 305 | case IOAPIC_IOREGSEL: |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 306 | val = s->ioregsel; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 307 | break; |
| 308 | case IOAPIC_IOWIN: |
Jan Kiszka | 1a44096 | 2011-10-17 13:11:29 +0200 | [diff] [blame] | 309 | if (size != 4) { |
| 310 | break; |
| 311 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 312 | switch (s->ioregsel) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 313 | case IOAPIC_REG_ID: |
Paolo Bonzini | 2f5a3b1 | 2015-07-30 10:21:00 +0200 | [diff] [blame] | 314 | case IOAPIC_REG_ARB: |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 315 | val = s->id << IOAPIC_ID_SHIFT; |
| 316 | break; |
| 317 | case IOAPIC_REG_VER: |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 318 | val = s->version | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 319 | ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT); |
| 320 | break; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 321 | 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; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 328 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 329 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 330 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 331 | break; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 332 | } |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 333 | |
Dr. David Alan Gilbert | a2e6ffa | 2017-11-02 18:03:10 +0000 | [diff] [blame] | 334 | trace_ioapic_mem_read(addr, s->ioregsel, size, val); |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 335 | |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 336 | return val; |
| 337 | } |
| 338 | |
Peter Xu | ed1263c | 2016-05-10 18:21:22 +0800 | [diff] [blame] | 339 | /* |
| 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 | */ |
| 358 | static inline void |
| 359 | ioapic_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 Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 367 | static void |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 368 | ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val, |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 369 | unsigned int size) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 370 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 371 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 372 | int index; |
| 373 | |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 374 | addr &= 0xff; |
Dr. David Alan Gilbert | a2e6ffa | 2017-11-02 18:03:10 +0000 | [diff] [blame] | 375 | trace_ioapic_mem_write(addr, s->ioregsel, size, val); |
Peter Xu | e5074b3 | 2017-01-09 16:55:51 +0800 | [diff] [blame] | 376 | |
| 377 | switch (addr) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 378 | case IOAPIC_IOREGSEL: |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 379 | s->ioregsel = val; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 380 | break; |
| 381 | case IOAPIC_IOWIN: |
Jan Kiszka | 1a44096 | 2011-10-17 13:11:29 +0200 | [diff] [blame] | 382 | if (size != 4) { |
| 383 | break; |
| 384 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 385 | switch (s->ioregsel) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 386 | 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 Xu | 479c2a1 | 2016-05-10 18:21:21 +0800 | [diff] [blame] | 395 | uint64_t ro_bits = s->ioredtbl[index] & IOAPIC_RO_BITS; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 396 | 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; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 402 | } |
Peter Xu | 479c2a1 | 2016-05-10 18:21:21 +0800 | [diff] [blame] | 403 | /* restore RO bits */ |
| 404 | s->ioredtbl[index] &= IOAPIC_RW_BITS; |
| 405 | s->ioredtbl[index] |= ro_bits; |
Li Qiang | d15d3d5 | 2019-06-24 08:16:35 -0700 | [diff] [blame] | 406 | s->irq_eoi[index] = 0; |
Peter Xu | ed1263c | 2016-05-10 18:21:22 +0800 | [diff] [blame] | 407 | ioapic_fix_edge_remote_irr(&s->ioredtbl[index]); |
David Woodhouse | 54ad31f | 2023-03-08 11:19:50 +0000 | [diff] [blame] | 408 | ioapic_update_kvm_routes(s); |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 409 | ioapic_service(s); |
| 410 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 411 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 412 | break; |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 413 | 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; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 423 | static const MemoryRegionOps ioapic_io_ops = { |
| 424 | .read = ioapic_mem_read, |
| 425 | .write = ioapic_mem_write, |
| 426 | .endianness = DEVICE_NATIVE_ENDIAN, |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
Peter Xu | e3d9c92 | 2016-07-14 13:56:27 +0800 | [diff] [blame] | 429 | static 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 Xu | 8d5516b | 2017-02-03 15:18:17 +0800 | [diff] [blame] | 447 | #define IOAPIC_VER_DEF 0x20 |
| 448 | |
xiaoqiang zhao | db0f888 | 2013-11-05 18:16:05 +0800 | [diff] [blame] | 449 | static void ioapic_realize(DeviceState *dev, Error **errp) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 450 | { |
xiaoqiang zhao | db0f888 | 2013-11-05 18:16:05 +0800 | [diff] [blame] | 451 | IOAPICCommonState *s = IOAPIC_COMMON(dev); |
xiaoqiang zhao | f977185 | 2013-11-05 18:16:04 +0800 | [diff] [blame] | 452 | |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 453 | if (s->version != 0x11 && s->version != 0x20) { |
Markus Armbruster | 11ab69d | 2018-10-17 10:26:34 +0200 | [diff] [blame] | 454 | error_setg(errp, "IOAPIC only supports version 0x11 or 0x20 " |
| 455 | "(default: 0x%x).", IOAPIC_VER_DEF); |
| 456 | return; |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 457 | } |
| 458 | |
Paolo Bonzini | 1437c94 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 459 | memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s, |
| 460 | "ioapic", 0x1000); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 461 | |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 462 | s->delayed_ioapic_service_timer = |
| 463 | timer_new_ns(QEMU_CLOCK_VIRTUAL, delayed_ioapic_service_cb, s); |
| 464 | |
xiaoqiang zhao | f977185 | 2013-11-05 18:16:04 +0800 | [diff] [blame] | 465 | qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 466 | |
xiaoqiang zhao | db0f888 | 2013-11-05 18:16:05 +0800 | [diff] [blame] | 467 | ioapics[ioapic_no] = s; |
Peter Xu | e3d9c92 | 2016-07-14 13:56:27 +0800 | [diff] [blame] | 468 | s->machine_done.notify = ioapic_machine_done_notify; |
| 469 | qemu_add_machine_init_done_notifier(&s->machine_done); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 470 | } |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 471 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 472 | static void ioapic_unrealize(DeviceState *dev) |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 473 | { |
| 474 | IOAPICCommonState *s = IOAPIC_COMMON(dev); |
| 475 | |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 476 | timer_free(s->delayed_ioapic_service_timer); |
| 477 | } |
| 478 | |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 479 | static Property ioapic_properties[] = { |
Peter Xu | 8d5516b | 2017-02-03 15:18:17 +0800 | [diff] [blame] | 480 | DEFINE_PROP_UINT8("version", IOAPICCommonState, version, IOAPIC_VER_DEF), |
Peter Xu | 20fd4b7 | 2016-08-01 21:59:19 +0800 | [diff] [blame] | 481 | DEFINE_PROP_END_OF_LIST(), |
| 482 | }; |
| 483 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 484 | static void ioapic_class_init(ObjectClass *klass, void *data) |
| 485 | { |
| 486 | IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 487 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 488 | |
xiaoqiang zhao | db0f888 | 2013-11-05 18:16:05 +0800 | [diff] [blame] | 489 | k->realize = ioapic_realize; |
Vitaly Kuznetsov | 958a01d | 2019-04-02 10:02:15 +0200 | [diff] [blame] | 490 | k->unrealize = ioapic_unrealize; |
Peter Xu | 0f254b1 | 2017-01-09 16:55:53 +0800 | [diff] [blame] | 491 | /* |
| 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 Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 496 | dc->reset = ioapic_reset_common; |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 497 | device_class_set_props(dc, ioapic_properties); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 498 | } |
| 499 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 500 | static const TypeInfo ioapic_info = { |
Li Qiang | 34bec7a | 2019-01-04 18:38:31 -0800 | [diff] [blame] | 501 | .name = TYPE_IOAPIC, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 502 | .parent = TYPE_IOAPIC_COMMON, |
| 503 | .instance_size = sizeof(IOAPICCommonState), |
| 504 | .class_init = ioapic_class_init, |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 507 | static void ioapic_register_types(void) |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 508 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 509 | type_register_static(&ioapic_info); |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 512 | type_init(ioapic_register_types) |