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 |
| 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 |
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 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 23 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 24 | #include "hw/i386/pc.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 25 | #include "hw/i386/ioapic.h" |
| 26 | #include "hw/i386/ioapic_internal.h" |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 27 | |
| 28 | //#define DEBUG_IOAPIC |
| 29 | |
Blue Swirl | 9af9b33 | 2010-05-31 18:59:45 +0000 | [diff] [blame] | 30 | #ifdef DEBUG_IOAPIC |
| 31 | #define DPRINTF(fmt, ...) \ |
| 32 | do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0) |
| 33 | #else |
| 34 | #define DPRINTF(fmt, ...) |
| 35 | #endif |
| 36 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 37 | static IOAPICCommonState *ioapics[MAX_IOAPICS]; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 38 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 39 | static void ioapic_service(IOAPICCommonState *s) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 40 | { |
| 41 | uint8_t i; |
| 42 | uint8_t trig_mode; |
| 43 | uint8_t vector; |
| 44 | uint8_t delivery_mode; |
| 45 | uint32_t mask; |
| 46 | uint64_t entry; |
| 47 | uint8_t dest; |
| 48 | uint8_t dest_mode; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 49 | |
| 50 | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
| 51 | mask = 1 << i; |
| 52 | if (s->irr & mask) { |
| 53 | entry = s->ioredtbl[i]; |
| 54 | if (!(entry & IOAPIC_LVT_MASKED)) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 55 | trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1); |
| 56 | dest = entry >> IOAPIC_LVT_DEST_SHIFT; |
| 57 | dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1; |
| 58 | delivery_mode = |
| 59 | (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 60 | if (trig_mode == IOAPIC_TRIGGER_EDGE) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 61 | s->irr &= ~mask; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 62 | } else { |
| 63 | s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR; |
| 64 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 65 | if (delivery_mode == IOAPIC_DM_EXTINT) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 66 | vector = pic_read_irq(isa_pic); |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 67 | } else { |
| 68 | vector = entry & IOAPIC_VECTOR_MASK; |
| 69 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 70 | apic_deliver_irq(dest, dest_mode, delivery_mode, |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 71 | vector, trig_mode); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Blue Swirl | 7d0500c | 2010-06-17 16:32:47 +0000 | [diff] [blame] | 77 | static void ioapic_set_irq(void *opaque, int vector, int level) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 78 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 79 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 80 | |
| 81 | /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps |
| 82 | * to GSI 2. GSI maps to ioapic 1-1. This is not |
| 83 | * the cleanest way of doing it but it should work. */ |
| 84 | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 85 | DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector); |
| 86 | if (vector == 0) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 87 | vector = 2; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 88 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 89 | if (vector >= 0 && vector < IOAPIC_NUM_PINS) { |
| 90 | uint32_t mask = 1 << vector; |
| 91 | uint64_t entry = s->ioredtbl[vector]; |
| 92 | |
Jan Kiszka | 0035e50 | 2011-08-22 17:46:42 +0200 | [diff] [blame] | 93 | if (entry & (1 << IOAPIC_LVT_POLARITY_SHIFT)) { |
| 94 | level = !level; |
| 95 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 96 | if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) == |
| 97 | IOAPIC_TRIGGER_LEVEL) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 98 | /* level triggered */ |
| 99 | if (level) { |
| 100 | s->irr |= mask; |
| 101 | ioapic_service(s); |
| 102 | } else { |
| 103 | s->irr &= ~mask; |
| 104 | } |
| 105 | } else { |
Jan Kiszka | 47f7be3 | 2011-04-09 13:18:59 +0200 | [diff] [blame] | 106 | /* According to the 82093AA manual, we must ignore edge requests |
| 107 | * if the input pin is masked. */ |
| 108 | if (level && !(entry & IOAPIC_LVT_MASKED)) { |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 109 | s->irr |= mask; |
| 110 | ioapic_service(s); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 116 | void ioapic_eoi_broadcast(int vector) |
| 117 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 118 | IOAPICCommonState *s; |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 119 | uint64_t entry; |
| 120 | int i, n; |
| 121 | |
| 122 | for (i = 0; i < MAX_IOAPICS; i++) { |
| 123 | s = ioapics[i]; |
| 124 | if (!s) { |
| 125 | continue; |
| 126 | } |
| 127 | for (n = 0; n < IOAPIC_NUM_PINS; n++) { |
| 128 | entry = s->ioredtbl[n]; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 129 | if ((entry & IOAPIC_LVT_REMOTE_IRR) |
| 130 | && (entry & IOAPIC_VECTOR_MASK) == vector) { |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 131 | s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR; |
| 132 | if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) { |
| 133 | ioapic_service(s); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 140 | static uint64_t |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 141 | ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 142 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 143 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 144 | int index; |
| 145 | uint32_t val = 0; |
| 146 | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 147 | switch (addr & 0xff) { |
| 148 | case IOAPIC_IOREGSEL: |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 149 | val = s->ioregsel; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 150 | break; |
| 151 | case IOAPIC_IOWIN: |
Jan Kiszka | 1a44096 | 2011-10-17 13:11:29 +0200 | [diff] [blame] | 152 | if (size != 4) { |
| 153 | break; |
| 154 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 155 | switch (s->ioregsel) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 156 | case IOAPIC_REG_ID: |
| 157 | val = s->id << IOAPIC_ID_SHIFT; |
| 158 | break; |
| 159 | case IOAPIC_REG_VER: |
| 160 | val = IOAPIC_VERSION | |
| 161 | ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT); |
| 162 | break; |
| 163 | case IOAPIC_REG_ARB: |
| 164 | val = 0; |
| 165 | break; |
| 166 | default: |
| 167 | index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1; |
| 168 | if (index >= 0 && index < IOAPIC_NUM_PINS) { |
| 169 | if (s->ioregsel & 1) { |
| 170 | val = s->ioredtbl[index] >> 32; |
| 171 | } else { |
| 172 | val = s->ioredtbl[index] & 0xffffffff; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 173 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 174 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 175 | } |
Blue Swirl | 9af9b33 | 2010-05-31 18:59:45 +0000 | [diff] [blame] | 176 | DPRINTF("read: %08x = %08x\n", s->ioregsel, val); |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 177 | break; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 178 | } |
| 179 | return val; |
| 180 | } |
| 181 | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 182 | static void |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 183 | ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val, |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 184 | unsigned int size) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 185 | { |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 186 | IOAPICCommonState *s = opaque; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 187 | int index; |
| 188 | |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 189 | switch (addr & 0xff) { |
| 190 | case IOAPIC_IOREGSEL: |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 191 | s->ioregsel = val; |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 192 | break; |
| 193 | case IOAPIC_IOWIN: |
Jan Kiszka | 1a44096 | 2011-10-17 13:11:29 +0200 | [diff] [blame] | 194 | if (size != 4) { |
| 195 | break; |
| 196 | } |
Jason Wang | 0c1f781 | 2012-03-19 11:19:57 +0800 | [diff] [blame] | 197 | DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 198 | switch (s->ioregsel) { |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 199 | case IOAPIC_REG_ID: |
| 200 | s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK; |
| 201 | break; |
| 202 | case IOAPIC_REG_VER: |
| 203 | case IOAPIC_REG_ARB: |
| 204 | break; |
| 205 | default: |
| 206 | index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1; |
| 207 | if (index >= 0 && index < IOAPIC_NUM_PINS) { |
| 208 | if (s->ioregsel & 1) { |
| 209 | s->ioredtbl[index] &= 0xffffffff; |
| 210 | s->ioredtbl[index] |= (uint64_t)val << 32; |
| 211 | } else { |
| 212 | s->ioredtbl[index] &= ~0xffffffffULL; |
| 213 | s->ioredtbl[index] |= val; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 214 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 215 | ioapic_service(s); |
| 216 | } |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 217 | } |
Jan Kiszka | 1f5e71a | 2011-02-03 22:54:14 +0100 | [diff] [blame] | 218 | break; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Jan Kiszka | 4d5bf5f | 2011-10-17 13:11:27 +0200 | [diff] [blame] | 222 | static const MemoryRegionOps ioapic_io_ops = { |
| 223 | .read = ioapic_mem_read, |
| 224 | .write = ioapic_mem_write, |
| 225 | .endianness = DEVICE_NATIVE_ENDIAN, |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 228 | static void ioapic_init(IOAPICCommonState *s, int instance_no) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 229 | { |
Paolo Bonzini | 1437c94 | 2013-06-06 21:25:08 -0400 | [diff] [blame^] | 230 | memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s, |
| 231 | "ioapic", 0x1000); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 232 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 233 | qdev_init_gpio_in(&s->busdev.qdev, ioapic_set_irq, IOAPIC_NUM_PINS); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 234 | |
Jan Kiszka | 244ac3a | 2011-10-16 19:38:22 +0200 | [diff] [blame] | 235 | ioapics[instance_no] = s; |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 236 | } |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 237 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 238 | static void ioapic_class_init(ObjectClass *klass, void *data) |
| 239 | { |
| 240 | IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 241 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 242 | |
| 243 | k->init = ioapic_init; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 244 | dc->reset = ioapic_reset_common; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 245 | } |
| 246 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 247 | static const TypeInfo ioapic_info = { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 248 | .name = "ioapic", |
| 249 | .parent = TYPE_IOAPIC_COMMON, |
| 250 | .instance_size = sizeof(IOAPICCommonState), |
| 251 | .class_init = ioapic_class_init, |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 254 | static void ioapic_register_types(void) |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 255 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 256 | type_register_static(&ioapic_info); |
Blue Swirl | 9605111 | 2010-06-19 07:41:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 259 | type_init(ioapic_register_types) |