bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * APIC support |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004-2005 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 18 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 19 | #include "hw.h" |
Blue Swirl | aa28b9b | 2010-03-21 19:46:26 +0000 | [diff] [blame] | 20 | #include "apic.h" |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 21 | #include "ioapic.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 22 | #include "qemu-timer.h" |
aurel32 | bb7e729 | 2008-10-12 20:16:03 +0000 | [diff] [blame] | 23 | #include "host-utils.h" |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 24 | #include "sysbus.h" |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 25 | #include "trace.h" |
Jan Kiszka | d96e173 | 2011-10-07 09:19:37 +0200 | [diff] [blame] | 26 | #include "pc.h" |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 27 | |
| 28 | /* APIC Local Vector Table */ |
| 29 | #define APIC_LVT_TIMER 0 |
| 30 | #define APIC_LVT_THERMAL 1 |
| 31 | #define APIC_LVT_PERFORM 2 |
| 32 | #define APIC_LVT_LINT0 3 |
| 33 | #define APIC_LVT_LINT1 4 |
| 34 | #define APIC_LVT_ERROR 5 |
| 35 | #define APIC_LVT_NB 6 |
| 36 | |
| 37 | /* APIC delivery modes */ |
| 38 | #define APIC_DM_FIXED 0 |
| 39 | #define APIC_DM_LOWPRI 1 |
| 40 | #define APIC_DM_SMI 2 |
| 41 | #define APIC_DM_NMI 4 |
| 42 | #define APIC_DM_INIT 5 |
| 43 | #define APIC_DM_SIPI 6 |
| 44 | #define APIC_DM_EXTINT 7 |
| 45 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 46 | /* APIC destination mode */ |
| 47 | #define APIC_DESTMODE_FLAT 0xf |
| 48 | #define APIC_DESTMODE_CLUSTER 1 |
| 49 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 50 | #define APIC_TRIGGER_EDGE 0 |
| 51 | #define APIC_TRIGGER_LEVEL 1 |
| 52 | |
| 53 | #define APIC_LVT_TIMER_PERIODIC (1<<17) |
| 54 | #define APIC_LVT_MASKED (1<<16) |
| 55 | #define APIC_LVT_LEVEL_TRIGGER (1<<15) |
| 56 | #define APIC_LVT_REMOTE_IRR (1<<14) |
| 57 | #define APIC_INPUT_POLARITY (1<<13) |
| 58 | #define APIC_SEND_PENDING (1<<12) |
| 59 | |
| 60 | #define ESR_ILLEGAL_ADDRESS (1 << 7) |
| 61 | |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 62 | #define APIC_SV_DIRECTED_IO (1<<12) |
| 63 | #define APIC_SV_ENABLE (1<<8) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 64 | |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 65 | #define MAX_APICS 255 |
| 66 | #define MAX_APIC_WORDS 8 |
| 67 | |
Michael S. Tsirkin | 54c96da | 2009-06-21 19:50:03 +0300 | [diff] [blame] | 68 | /* Intel APIC constants: from include/asm/msidef.h */ |
| 69 | #define MSI_DATA_VECTOR_SHIFT 0 |
| 70 | #define MSI_DATA_VECTOR_MASK 0x000000ff |
| 71 | #define MSI_DATA_DELIVERY_MODE_SHIFT 8 |
| 72 | #define MSI_DATA_TRIGGER_SHIFT 15 |
| 73 | #define MSI_DATA_LEVEL_SHIFT 14 |
| 74 | #define MSI_ADDR_DEST_MODE_SHIFT 2 |
| 75 | #define MSI_ADDR_DEST_ID_SHIFT 12 |
| 76 | #define MSI_ADDR_DEST_ID_MASK 0x00ffff0 |
| 77 | |
Michael S. Tsirkin | 54c96da | 2009-06-21 19:50:03 +0300 | [diff] [blame] | 78 | #define MSI_ADDR_SIZE 0x100000 |
| 79 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 80 | typedef struct APICState APICState; |
| 81 | |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 82 | struct APICState { |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 83 | SysBusDevice busdev; |
Avi Kivity | 312b423 | 2011-08-15 17:17:16 +0300 | [diff] [blame] | 84 | MemoryRegion io_memory; |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 85 | void *cpu_env; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 86 | uint32_t apicbase; |
| 87 | uint8_t id; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 88 | uint8_t arb_id; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 89 | uint8_t tpr; |
| 90 | uint32_t spurious_vec; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 91 | uint8_t log_dest; |
| 92 | uint8_t dest_mode; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 93 | uint32_t isr[8]; /* in service register */ |
| 94 | uint32_t tmr[8]; /* trigger mode register */ |
| 95 | uint32_t irr[8]; /* interrupt request register */ |
| 96 | uint32_t lvt[APIC_LVT_NB]; |
| 97 | uint32_t esr; /* error register */ |
| 98 | uint32_t icr[2]; |
| 99 | |
| 100 | uint32_t divide_conf; |
| 101 | int count_shift; |
| 102 | uint32_t initial_count; |
| 103 | int64_t initial_count_load_time, next_time; |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 104 | uint32_t idx; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 105 | QEMUTimer *timer; |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 106 | int sipi_vector; |
| 107 | int wait_for_sipi; |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 108 | }; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 109 | |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 110 | static APICState *local_apics[MAX_APICS + 1]; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 111 | static int apic_irq_delivered; |
| 112 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 113 | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode); |
| 114 | static void apic_update_irq(APICState *s); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 115 | static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask, |
| 116 | uint8_t dest, uint8_t dest_mode); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 117 | |
aurel32 | 3b63c04 | 2008-12-06 10:46:35 +0000 | [diff] [blame] | 118 | /* Find first bit starting from msb */ |
| 119 | static int fls_bit(uint32_t value) |
| 120 | { |
| 121 | return 31 - clz32(value); |
| 122 | } |
| 123 | |
aurel32 | e95f549 | 2008-10-12 00:53:17 +0000 | [diff] [blame] | 124 | /* Find first bit starting from lsb */ |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 125 | static int ffs_bit(uint32_t value) |
| 126 | { |
aurel32 | bb7e729 | 2008-10-12 20:16:03 +0000 | [diff] [blame] | 127 | return ctz32(value); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static inline void set_bit(uint32_t *tab, int index) |
| 131 | { |
| 132 | int i, mask; |
| 133 | i = index >> 5; |
| 134 | mask = 1 << (index & 0x1f); |
| 135 | tab[i] |= mask; |
| 136 | } |
| 137 | |
| 138 | static inline void reset_bit(uint32_t *tab, int index) |
| 139 | { |
| 140 | int i, mask; |
| 141 | i = index >> 5; |
| 142 | mask = 1 << (index & 0x1f); |
| 143 | tab[i] &= ~mask; |
| 144 | } |
| 145 | |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 146 | static inline int get_bit(uint32_t *tab, int index) |
| 147 | { |
| 148 | int i, mask; |
| 149 | i = index >> 5; |
| 150 | mask = 1 << (index & 0x1f); |
| 151 | return !!(tab[i] & mask); |
| 152 | } |
| 153 | |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 154 | static void apic_local_deliver(APICState *s, int vector) |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 155 | { |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 156 | uint32_t lvt = s->lvt[vector]; |
| 157 | int trigger_mode; |
| 158 | |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 159 | trace_apic_local_deliver(vector, (lvt >> 8) & 7); |
| 160 | |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 161 | if (lvt & APIC_LVT_MASKED) |
| 162 | return; |
| 163 | |
| 164 | switch ((lvt >> 8) & 7) { |
| 165 | case APIC_DM_SMI: |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 166 | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI); |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
| 169 | case APIC_DM_NMI: |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 170 | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI); |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | case APIC_DM_EXTINT: |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 174 | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | case APIC_DM_FIXED: |
| 178 | trigger_mode = APIC_TRIGGER_EDGE; |
| 179 | if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) && |
| 180 | (lvt & APIC_LVT_LEVEL_TRIGGER)) |
| 181 | trigger_mode = APIC_TRIGGER_LEVEL; |
| 182 | apic_set_irq(s, lvt & 0xff, trigger_mode); |
| 183 | } |
| 184 | } |
| 185 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 186 | void apic_deliver_pic_intr(DeviceState *d, int level) |
aurel32 | 1a7de94 | 2008-08-21 03:14:52 +0000 | [diff] [blame] | 187 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 188 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 189 | |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 190 | if (level) { |
| 191 | apic_local_deliver(s, APIC_LVT_LINT0); |
| 192 | } else { |
aurel32 | 1a7de94 | 2008-08-21 03:14:52 +0000 | [diff] [blame] | 193 | uint32_t lvt = s->lvt[APIC_LVT_LINT0]; |
| 194 | |
| 195 | switch ((lvt >> 8) & 7) { |
| 196 | case APIC_DM_FIXED: |
| 197 | if (!(lvt & APIC_LVT_LEVEL_TRIGGER)) |
| 198 | break; |
| 199 | reset_bit(s->irr, lvt & 0xff); |
| 200 | /* fall through */ |
| 201 | case APIC_DM_EXTINT: |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 202 | cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
aurel32 | 1a7de94 | 2008-08-21 03:14:52 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Jan Kiszka | 02c0919 | 2011-10-18 00:00:06 +0800 | [diff] [blame] | 208 | void apic_deliver_nmi(DeviceState *d) |
| 209 | { |
| 210 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 211 | |
| 212 | apic_local_deliver(s, APIC_LVT_LINT1); |
| 213 | } |
| 214 | |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 215 | #define foreach_apic(apic, deliver_bitmask, code) \ |
| 216 | {\ |
| 217 | int __i, __j, __mask;\ |
| 218 | for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\ |
| 219 | __mask = deliver_bitmask[__i];\ |
| 220 | if (__mask) {\ |
| 221 | for(__j = 0; __j < 32; __j++) {\ |
| 222 | if (__mask & (1 << __j)) {\ |
| 223 | apic = local_apics[__i * 32 + __j];\ |
| 224 | if (apic) {\ |
| 225 | code;\ |
| 226 | }\ |
| 227 | }\ |
| 228 | }\ |
| 229 | }\ |
| 230 | }\ |
| 231 | } |
| 232 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 233 | static void apic_bus_deliver(const uint32_t *deliver_bitmask, |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 234 | uint8_t delivery_mode, uint8_t vector_num, |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 235 | uint8_t trigger_mode) |
| 236 | { |
| 237 | APICState *apic_iter; |
| 238 | |
| 239 | switch (delivery_mode) { |
| 240 | case APIC_DM_LOWPRI: |
bellard | 8dd69b8 | 2005-11-23 20:59:44 +0000 | [diff] [blame] | 241 | /* XXX: search for focus processor, arbitration */ |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 242 | { |
| 243 | int i, d; |
| 244 | d = -1; |
| 245 | for(i = 0; i < MAX_APIC_WORDS; i++) { |
| 246 | if (deliver_bitmask[i]) { |
| 247 | d = i * 32 + ffs_bit(deliver_bitmask[i]); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | if (d >= 0) { |
| 252 | apic_iter = local_apics[d]; |
| 253 | if (apic_iter) { |
| 254 | apic_set_irq(apic_iter, vector_num, trigger_mode); |
| 255 | } |
| 256 | } |
bellard | 8dd69b8 | 2005-11-23 20:59:44 +0000 | [diff] [blame] | 257 | } |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 258 | return; |
bellard | 8dd69b8 | 2005-11-23 20:59:44 +0000 | [diff] [blame] | 259 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 260 | case APIC_DM_FIXED: |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | case APIC_DM_SMI: |
aurel32 | e2eb9d3 | 2008-04-13 16:08:23 +0000 | [diff] [blame] | 264 | foreach_apic(apic_iter, deliver_bitmask, |
| 265 | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) ); |
| 266 | return; |
| 267 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 268 | case APIC_DM_NMI: |
aurel32 | e2eb9d3 | 2008-04-13 16:08:23 +0000 | [diff] [blame] | 269 | foreach_apic(apic_iter, deliver_bitmask, |
| 270 | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) ); |
| 271 | return; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 272 | |
| 273 | case APIC_DM_INIT: |
| 274 | /* normal INIT IPI sent to processors */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 275 | foreach_apic(apic_iter, deliver_bitmask, |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 276 | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) ); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 277 | return; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 278 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 279 | case APIC_DM_EXTINT: |
bellard | b1fc034 | 2005-07-23 21:43:15 +0000 | [diff] [blame] | 280 | /* handled in I/O APIC code */ |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | default: |
| 284 | return; |
| 285 | } |
| 286 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 287 | foreach_apic(apic_iter, deliver_bitmask, |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 288 | apic_set_irq(apic_iter, vector_num, trigger_mode) ); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 289 | } |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 290 | |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 291 | void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode, |
| 292 | uint8_t vector_num, uint8_t trigger_mode) |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 293 | { |
| 294 | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
| 295 | |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 296 | trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num, |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 297 | trigger_mode); |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 298 | |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 299 | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 300 | apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode); |
aliguori | 610626a | 2009-03-12 20:25:12 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 303 | void cpu_set_apic_base(DeviceState *d, uint64_t val) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 304 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 305 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 306 | |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 307 | trace_cpu_set_apic_base(val); |
| 308 | |
aurel32 | 2c7c13d | 2009-04-08 22:56:26 +0000 | [diff] [blame] | 309 | if (!s) |
| 310 | return; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 311 | s->apicbase = (val & 0xfffff000) | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 312 | (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE)); |
| 313 | /* if disabled, cannot be enabled again */ |
| 314 | if (!(val & MSR_IA32_APICBASE_ENABLE)) { |
| 315 | s->apicbase &= ~MSR_IA32_APICBASE_ENABLE; |
Blue Swirl | 0e26b7b | 2010-06-19 10:42:34 +0300 | [diff] [blame] | 316 | cpu_clear_apic_feature(s->cpu_env); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 317 | s->spurious_vec &= ~APIC_SV_ENABLE; |
| 318 | } |
| 319 | } |
| 320 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 321 | uint64_t cpu_get_apic_base(DeviceState *d) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 322 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 323 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 324 | |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 325 | trace_cpu_get_apic_base(s ? (uint64_t)s->apicbase: 0); |
| 326 | |
aurel32 | 2c7c13d | 2009-04-08 22:56:26 +0000 | [diff] [blame] | 327 | return s ? s->apicbase : 0; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 330 | void cpu_set_apic_tpr(DeviceState *d, uint8_t val) |
bellard | 9230e66 | 2005-01-23 20:46:56 +0000 | [diff] [blame] | 331 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 332 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 333 | |
aurel32 | 2c7c13d | 2009-04-08 22:56:26 +0000 | [diff] [blame] | 334 | if (!s) |
| 335 | return; |
bellard | 9230e66 | 2005-01-23 20:46:56 +0000 | [diff] [blame] | 336 | s->tpr = (val & 0x0f) << 4; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 337 | apic_update_irq(s); |
bellard | 9230e66 | 2005-01-23 20:46:56 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 340 | uint8_t cpu_get_apic_tpr(DeviceState *d) |
bellard | 9230e66 | 2005-01-23 20:46:56 +0000 | [diff] [blame] | 341 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 342 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 343 | |
aurel32 | 2c7c13d | 2009-04-08 22:56:26 +0000 | [diff] [blame] | 344 | return s ? s->tpr >> 4 : 0; |
bellard | 9230e66 | 2005-01-23 20:46:56 +0000 | [diff] [blame] | 345 | } |
| 346 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 347 | /* return -1 if no bit is set */ |
| 348 | static int get_highest_priority_int(uint32_t *tab) |
| 349 | { |
| 350 | int i; |
| 351 | for(i = 7; i >= 0; i--) { |
| 352 | if (tab[i] != 0) { |
aurel32 | 3b63c04 | 2008-12-06 10:46:35 +0000 | [diff] [blame] | 353 | return i * 32 + fls_bit(tab[i]); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | return -1; |
| 357 | } |
| 358 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 359 | static int apic_get_ppr(APICState *s) |
| 360 | { |
| 361 | int tpr, isrv, ppr; |
| 362 | |
| 363 | tpr = (s->tpr >> 4); |
| 364 | isrv = get_highest_priority_int(s->isr); |
| 365 | if (isrv < 0) |
| 366 | isrv = 0; |
| 367 | isrv >>= 4; |
| 368 | if (tpr >= isrv) |
| 369 | ppr = s->tpr; |
| 370 | else |
| 371 | ppr = isrv << 4; |
| 372 | return ppr; |
| 373 | } |
| 374 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 375 | static int apic_get_arb_pri(APICState *s) |
| 376 | { |
| 377 | /* XXX: arbitration */ |
| 378 | return 0; |
| 379 | } |
| 380 | |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * <0 - low prio interrupt, |
| 384 | * 0 - no interrupt, |
| 385 | * >0 - interrupt number |
| 386 | */ |
| 387 | static int apic_irq_pending(APICState *s) |
| 388 | { |
| 389 | int irrv, ppr; |
| 390 | irrv = get_highest_priority_int(s->irr); |
| 391 | if (irrv < 0) { |
| 392 | return 0; |
| 393 | } |
| 394 | ppr = apic_get_ppr(s); |
| 395 | if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) { |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | return irrv; |
| 400 | } |
| 401 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 402 | /* signal the CPU if an irq is pending */ |
| 403 | static void apic_update_irq(APICState *s) |
| 404 | { |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 405 | if (!(s->spurious_vec & APIC_SV_ENABLE)) { |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 406 | return; |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 407 | } |
| 408 | if (apic_irq_pending(s) > 0) { |
| 409 | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
Jan Kiszka | d96e173 | 2011-10-07 09:19:37 +0200 | [diff] [blame] | 410 | } else if (apic_accept_pic_intr(&s->busdev.qdev) && |
| 411 | pic_get_output(isa_pic)) { |
| 412 | apic_deliver_pic_intr(&s->busdev.qdev, 1); |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 413 | } |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Jan Kiszka | 343270e | 2011-12-13 15:39:04 +0100 | [diff] [blame^] | 416 | void apic_report_irq_delivered(int delivered) |
| 417 | { |
| 418 | apic_irq_delivered += delivered; |
| 419 | |
| 420 | trace_apic_report_irq_delivered(apic_irq_delivered); |
| 421 | } |
| 422 | |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 423 | void apic_reset_irq_delivered(void) |
| 424 | { |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 425 | trace_apic_reset_irq_delivered(apic_irq_delivered); |
| 426 | |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 427 | apic_irq_delivered = 0; |
| 428 | } |
| 429 | |
| 430 | int apic_get_irq_delivered(void) |
| 431 | { |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 432 | trace_apic_get_irq_delivered(apic_irq_delivered); |
| 433 | |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 434 | return apic_irq_delivered; |
| 435 | } |
| 436 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 437 | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode) |
| 438 | { |
Jan Kiszka | 343270e | 2011-12-13 15:39:04 +0100 | [diff] [blame^] | 439 | apic_report_irq_delivered(!get_bit(s->irr, vector_num)); |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 440 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 441 | set_bit(s->irr, vector_num); |
| 442 | if (trigger_mode) |
| 443 | set_bit(s->tmr, vector_num); |
| 444 | else |
| 445 | reset_bit(s->tmr, vector_num); |
| 446 | apic_update_irq(s); |
| 447 | } |
| 448 | |
| 449 | static void apic_eoi(APICState *s) |
| 450 | { |
| 451 | int isrv; |
| 452 | isrv = get_highest_priority_int(s->isr); |
| 453 | if (isrv < 0) |
| 454 | return; |
| 455 | reset_bit(s->isr, isrv); |
Jan Kiszka | 0280b57 | 2011-02-03 22:54:11 +0100 | [diff] [blame] | 456 | if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && get_bit(s->tmr, isrv)) { |
| 457 | ioapic_eoi_broadcast(isrv); |
| 458 | } |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 459 | apic_update_irq(s); |
| 460 | } |
| 461 | |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 462 | static int apic_find_dest(uint8_t dest) |
| 463 | { |
| 464 | APICState *apic = local_apics[dest]; |
| 465 | int i; |
| 466 | |
| 467 | if (apic && apic->id == dest) |
| 468 | return dest; /* shortcut in case apic->id == apic->idx */ |
| 469 | |
| 470 | for (i = 0; i < MAX_APICS; i++) { |
| 471 | apic = local_apics[i]; |
| 472 | if (apic && apic->id == dest) |
| 473 | return i; |
Alex Williamson | b538e53 | 2010-11-05 16:01:29 -0600 | [diff] [blame] | 474 | if (!apic) |
| 475 | break; |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | return -1; |
| 479 | } |
| 480 | |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 481 | static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask, |
| 482 | uint8_t dest, uint8_t dest_mode) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 483 | { |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 484 | APICState *apic_iter; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 485 | int i; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 486 | |
| 487 | if (dest_mode == 0) { |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 488 | if (dest == 0xff) { |
| 489 | memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t)); |
| 490 | } else { |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 491 | int idx = apic_find_dest(dest); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 492 | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 493 | if (idx >= 0) |
| 494 | set_bit(deliver_bitmask, idx); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 495 | } |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 496 | } else { |
| 497 | /* XXX: cluster mode */ |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 498 | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
| 499 | for(i = 0; i < MAX_APICS; i++) { |
| 500 | apic_iter = local_apics[i]; |
| 501 | if (apic_iter) { |
| 502 | if (apic_iter->dest_mode == 0xf) { |
| 503 | if (dest & apic_iter->log_dest) |
| 504 | set_bit(deliver_bitmask, i); |
| 505 | } else if (apic_iter->dest_mode == 0x0) { |
| 506 | if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) && |
| 507 | (dest & apic_iter->log_dest & 0x0f)) { |
| 508 | set_bit(deliver_bitmask, i); |
| 509 | } |
| 510 | } |
Alex Williamson | b538e53 | 2010-11-05 16:01:29 -0600 | [diff] [blame] | 511 | } else { |
| 512 | break; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 513 | } |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 518 | void apic_init_reset(DeviceState *d) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 519 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 520 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 521 | int i; |
| 522 | |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 523 | if (!s) |
| 524 | return; |
| 525 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 526 | s->tpr = 0; |
| 527 | s->spurious_vec = 0xff; |
| 528 | s->log_dest = 0; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 529 | s->dest_mode = 0xf; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 530 | memset(s->isr, 0, sizeof(s->isr)); |
| 531 | memset(s->tmr, 0, sizeof(s->tmr)); |
| 532 | memset(s->irr, 0, sizeof(s->irr)); |
bellard | b451172 | 2006-10-08 18:20:51 +0000 | [diff] [blame] | 533 | for(i = 0; i < APIC_LVT_NB; i++) |
| 534 | s->lvt[i] = 1 << 16; /* mask LVT */ |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 535 | s->esr = 0; |
| 536 | memset(s->icr, 0, sizeof(s->icr)); |
| 537 | s->divide_conf = 0; |
| 538 | s->count_shift = 0; |
| 539 | s->initial_count = 0; |
| 540 | s->initial_count_load_time = 0; |
| 541 | s->next_time = 0; |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 542 | s->wait_for_sipi = 1; |
Jan Kiszka | ab388a9 | 2011-10-16 11:59:30 +0200 | [diff] [blame] | 543 | |
| 544 | qemu_del_timer(s->timer); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 545 | } |
| 546 | |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 547 | static void apic_startup(APICState *s, int vector_num) |
| 548 | { |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 549 | s->sipi_vector = vector_num; |
| 550 | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI); |
| 551 | } |
| 552 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 553 | void apic_sipi(DeviceState *d) |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 554 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 555 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
| 556 | |
Blue Swirl | 4a942ce | 2010-06-19 10:42:31 +0300 | [diff] [blame] | 557 | cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI); |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 558 | |
| 559 | if (!s->wait_for_sipi) |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 560 | return; |
Blue Swirl | 0e26b7b | 2010-06-19 10:42:34 +0300 | [diff] [blame] | 561 | cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector); |
Gleb Natapov | b09ea7d | 2009-06-17 23:26:59 +0300 | [diff] [blame] | 562 | s->wait_for_sipi = 0; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 565 | static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode, |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 566 | uint8_t delivery_mode, uint8_t vector_num, |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 567 | uint8_t trigger_mode) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 568 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 569 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 570 | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 571 | int dest_shorthand = (s->icr[0] >> 18) & 3; |
| 572 | APICState *apic_iter; |
| 573 | |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 574 | switch (dest_shorthand) { |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 575 | case 0: |
| 576 | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
| 577 | break; |
| 578 | case 1: |
| 579 | memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask)); |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 580 | set_bit(deliver_bitmask, s->idx); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 581 | break; |
| 582 | case 2: |
| 583 | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
| 584 | break; |
| 585 | case 3: |
| 586 | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 587 | reset_bit(deliver_bitmask, s->idx); |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 588 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 589 | } |
| 590 | |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 591 | switch (delivery_mode) { |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 592 | case APIC_DM_INIT: |
| 593 | { |
| 594 | int trig_mode = (s->icr[0] >> 15) & 1; |
| 595 | int level = (s->icr[0] >> 14) & 1; |
| 596 | if (level == 0 && trig_mode == 1) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 597 | foreach_apic(apic_iter, deliver_bitmask, |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 598 | apic_iter->arb_id = apic_iter->id ); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | } |
| 602 | break; |
| 603 | |
| 604 | case APIC_DM_SIPI: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 605 | foreach_apic(apic_iter, deliver_bitmask, |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 606 | apic_startup(apic_iter, vector_num) ); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 610 | apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 613 | int apic_get_interrupt(DeviceState *d) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 614 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 615 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 616 | int intno; |
| 617 | |
| 618 | /* if the APIC is installed or enabled, we let the 8259 handle the |
| 619 | IRQs */ |
| 620 | if (!s) |
| 621 | return -1; |
| 622 | if (!(s->spurious_vec & APIC_SV_ENABLE)) |
| 623 | return -1; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 624 | |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 625 | intno = apic_irq_pending(s); |
| 626 | |
| 627 | if (intno == 0) { |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 628 | return -1; |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 629 | } else if (intno < 0) { |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 630 | return s->spurious_vec & 0xff; |
Gleb Natapov | 0fbfbb5 | 2011-02-07 16:14:44 +0200 | [diff] [blame] | 631 | } |
bellard | b451172 | 2006-10-08 18:20:51 +0000 | [diff] [blame] | 632 | reset_bit(s->irr, intno); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 633 | set_bit(s->isr, intno); |
| 634 | apic_update_irq(s); |
| 635 | return intno; |
| 636 | } |
| 637 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 638 | int apic_accept_pic_intr(DeviceState *d) |
ths | 0e21e12 | 2007-10-09 03:08:56 +0000 | [diff] [blame] | 639 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 640 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
ths | 0e21e12 | 2007-10-09 03:08:56 +0000 | [diff] [blame] | 641 | uint32_t lvt0; |
| 642 | |
| 643 | if (!s) |
| 644 | return -1; |
| 645 | |
| 646 | lvt0 = s->lvt[APIC_LVT_LINT0]; |
| 647 | |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 648 | if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 || |
| 649 | (lvt0 & APIC_LVT_MASKED) == 0) |
ths | 0e21e12 | 2007-10-09 03:08:56 +0000 | [diff] [blame] | 650 | return 1; |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 655 | static uint32_t apic_get_current_count(APICState *s) |
| 656 | { |
| 657 | int64_t d; |
| 658 | uint32_t val; |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 659 | d = (qemu_get_clock_ns(vm_clock) - s->initial_count_load_time) >> |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 660 | s->count_shift; |
| 661 | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) { |
| 662 | /* periodic */ |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 663 | val = s->initial_count - (d % ((uint64_t)s->initial_count + 1)); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 664 | } else { |
| 665 | if (d >= s->initial_count) |
| 666 | val = 0; |
| 667 | else |
| 668 | val = s->initial_count - d; |
| 669 | } |
| 670 | return val; |
| 671 | } |
| 672 | |
| 673 | static void apic_timer_update(APICState *s, int64_t current_time) |
| 674 | { |
| 675 | int64_t next_time, d; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 676 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 677 | if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 678 | d = (current_time - s->initial_count_load_time) >> |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 679 | s->count_shift; |
| 680 | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) { |
aliguori | 681f8c2 | 2008-08-18 14:19:42 +0000 | [diff] [blame] | 681 | if (!s->initial_count) |
| 682 | goto no_timer; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 683 | d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 684 | } else { |
| 685 | if (d >= s->initial_count) |
| 686 | goto no_timer; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 687 | d = (uint64_t)s->initial_count + 1; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 688 | } |
| 689 | next_time = s->initial_count_load_time + (d << s->count_shift); |
| 690 | qemu_mod_timer(s->timer, next_time); |
| 691 | s->next_time = next_time; |
| 692 | } else { |
| 693 | no_timer: |
| 694 | qemu_del_timer(s->timer); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | static void apic_timer(void *opaque) |
| 699 | { |
| 700 | APICState *s = opaque; |
| 701 | |
Blue Swirl | cf6d64b | 2010-06-19 10:42:08 +0300 | [diff] [blame] | 702 | apic_local_deliver(s, APIC_LVT_TIMER); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 703 | apic_timer_update(s, s->next_time); |
| 704 | } |
| 705 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 706 | static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 707 | { |
| 708 | return 0; |
| 709 | } |
| 710 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 711 | static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 712 | { |
| 713 | return 0; |
| 714 | } |
| 715 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 716 | static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 717 | { |
| 718 | } |
| 719 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 720 | static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 721 | { |
| 722 | } |
| 723 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 724 | static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 725 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 726 | DeviceState *d; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 727 | APICState *s; |
| 728 | uint32_t val; |
| 729 | int index; |
| 730 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 731 | d = cpu_get_current_apic(); |
| 732 | if (!d) { |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 733 | return 0; |
Blue Swirl | 0e26b7b | 2010-06-19 10:42:34 +0300 | [diff] [blame] | 734 | } |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 735 | s = DO_UPCAST(APICState, busdev.qdev, d); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 736 | |
| 737 | index = (addr >> 4) & 0xff; |
| 738 | switch(index) { |
| 739 | case 0x02: /* id */ |
| 740 | val = s->id << 24; |
| 741 | break; |
| 742 | case 0x03: /* version */ |
| 743 | val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */ |
| 744 | break; |
| 745 | case 0x08: |
| 746 | val = s->tpr; |
| 747 | break; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 748 | case 0x09: |
| 749 | val = apic_get_arb_pri(s); |
| 750 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 751 | case 0x0a: |
| 752 | /* ppr */ |
| 753 | val = apic_get_ppr(s); |
| 754 | break; |
aurel32 | b237db3 | 2008-03-28 22:31:36 +0000 | [diff] [blame] | 755 | case 0x0b: |
| 756 | val = 0; |
| 757 | break; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 758 | case 0x0d: |
| 759 | val = s->log_dest << 24; |
| 760 | break; |
| 761 | case 0x0e: |
| 762 | val = s->dest_mode << 28; |
| 763 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 764 | case 0x0f: |
| 765 | val = s->spurious_vec; |
| 766 | break; |
| 767 | case 0x10 ... 0x17: |
| 768 | val = s->isr[index & 7]; |
| 769 | break; |
| 770 | case 0x18 ... 0x1f: |
| 771 | val = s->tmr[index & 7]; |
| 772 | break; |
| 773 | case 0x20 ... 0x27: |
| 774 | val = s->irr[index & 7]; |
| 775 | break; |
| 776 | case 0x28: |
| 777 | val = s->esr; |
| 778 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 779 | case 0x30: |
| 780 | case 0x31: |
| 781 | val = s->icr[index & 1]; |
| 782 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 783 | case 0x32 ... 0x37: |
| 784 | val = s->lvt[index - 0x32]; |
| 785 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 786 | case 0x38: |
| 787 | val = s->initial_count; |
| 788 | break; |
| 789 | case 0x39: |
| 790 | val = apic_get_current_count(s); |
| 791 | break; |
| 792 | case 0x3e: |
| 793 | val = s->divide_conf; |
| 794 | break; |
| 795 | default: |
| 796 | s->esr |= ESR_ILLEGAL_ADDRESS; |
| 797 | val = 0; |
| 798 | break; |
| 799 | } |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 800 | trace_apic_mem_readl(addr, val); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 801 | return val; |
| 802 | } |
| 803 | |
Andreas Färber | f5095c6 | 2010-12-19 17:22:39 +0100 | [diff] [blame] | 804 | static void apic_send_msi(target_phys_addr_t addr, uint32_t data) |
Michael S. Tsirkin | 54c96da | 2009-06-21 19:50:03 +0300 | [diff] [blame] | 805 | { |
| 806 | uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT; |
| 807 | uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT; |
| 808 | uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1; |
| 809 | uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1; |
| 810 | uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7; |
| 811 | /* XXX: Ignore redirection hint. */ |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 812 | apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode); |
Michael S. Tsirkin | 54c96da | 2009-06-21 19:50:03 +0300 | [diff] [blame] | 813 | } |
| 814 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 815 | static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 816 | { |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 817 | DeviceState *d; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 818 | APICState *s; |
Michael S. Tsirkin | 54c96da | 2009-06-21 19:50:03 +0300 | [diff] [blame] | 819 | int index = (addr >> 4) & 0xff; |
| 820 | if (addr > 0xfff || !index) { |
| 821 | /* MSI and MMIO APIC are at the same memory location, |
| 822 | * but actually not on the global bus: MSI is on PCI bus |
| 823 | * APIC is connected directly to the CPU. |
| 824 | * Mapping them on the global bus happens to work because |
| 825 | * MSI registers are reserved in APIC MMIO and vice versa. */ |
| 826 | apic_send_msi(addr, val); |
| 827 | return; |
| 828 | } |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 829 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 830 | d = cpu_get_current_apic(); |
| 831 | if (!d) { |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 832 | return; |
Blue Swirl | 0e26b7b | 2010-06-19 10:42:34 +0300 | [diff] [blame] | 833 | } |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 834 | s = DO_UPCAST(APICState, busdev.qdev, d); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 835 | |
Blue Swirl | d8023f3 | 2010-10-20 16:41:28 +0000 | [diff] [blame] | 836 | trace_apic_mem_writel(addr, val); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 837 | |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 838 | switch(index) { |
| 839 | case 0x02: |
| 840 | s->id = (val >> 24); |
| 841 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 842 | case 0x03: |
| 843 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 844 | case 0x08: |
| 845 | s->tpr = val; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 846 | apic_update_irq(s); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 847 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 848 | case 0x09: |
| 849 | case 0x0a: |
| 850 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 851 | case 0x0b: /* EOI */ |
| 852 | apic_eoi(s); |
| 853 | break; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 854 | case 0x0d: |
| 855 | s->log_dest = val >> 24; |
| 856 | break; |
| 857 | case 0x0e: |
| 858 | s->dest_mode = val >> 28; |
| 859 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 860 | case 0x0f: |
| 861 | s->spurious_vec = val & 0x1ff; |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 862 | apic_update_irq(s); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 863 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 864 | case 0x10 ... 0x17: |
| 865 | case 0x18 ... 0x1f: |
| 866 | case 0x20 ... 0x27: |
| 867 | case 0x28: |
| 868 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 869 | case 0x30: |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 870 | s->icr[0] = val; |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 871 | apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1, |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 872 | (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff), |
Jan Kiszka | 1f6f408 | 2011-08-22 17:46:31 +0200 | [diff] [blame] | 873 | (s->icr[0] >> 15) & 1); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 874 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 875 | case 0x31: |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 876 | s->icr[1] = val; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 877 | break; |
| 878 | case 0x32 ... 0x37: |
| 879 | { |
| 880 | int n = index - 0x32; |
| 881 | s->lvt[n] = val; |
| 882 | if (n == APIC_LVT_TIMER) |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 883 | apic_timer_update(s, qemu_get_clock_ns(vm_clock)); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 884 | } |
| 885 | break; |
| 886 | case 0x38: |
| 887 | s->initial_count = val; |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 888 | s->initial_count_load_time = qemu_get_clock_ns(vm_clock); |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 889 | apic_timer_update(s, s->initial_count_load_time); |
| 890 | break; |
bellard | e0fd878 | 2005-11-21 23:26:26 +0000 | [diff] [blame] | 891 | case 0x39: |
| 892 | break; |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 893 | case 0x3e: |
| 894 | { |
| 895 | int v; |
| 896 | s->divide_conf = val & 0xb; |
| 897 | v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4); |
| 898 | s->count_shift = (v + 1) & 7; |
| 899 | } |
| 900 | break; |
| 901 | default: |
| 902 | s->esr |= ESR_ILLEGAL_ADDRESS; |
| 903 | break; |
| 904 | } |
| 905 | } |
| 906 | |
Juan Quintela | 695dcf7 | 2009-08-20 19:42:28 +0200 | [diff] [blame] | 907 | /* This function is only used for old state version 1 and 2 */ |
| 908 | static int apic_load_old(QEMUFile *f, void *opaque, int version_id) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 909 | { |
| 910 | APICState *s = opaque; |
| 911 | int i; |
| 912 | |
bellard | e6cf6a8 | 2006-08-17 10:48:06 +0000 | [diff] [blame] | 913 | if (version_id > 2) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 914 | return -EINVAL; |
| 915 | |
| 916 | /* XXX: what if the base changes? (registered memory regions) */ |
| 917 | qemu_get_be32s(f, &s->apicbase); |
| 918 | qemu_get_8s(f, &s->id); |
| 919 | qemu_get_8s(f, &s->arb_id); |
| 920 | qemu_get_8s(f, &s->tpr); |
| 921 | qemu_get_be32s(f, &s->spurious_vec); |
| 922 | qemu_get_8s(f, &s->log_dest); |
| 923 | qemu_get_8s(f, &s->dest_mode); |
| 924 | for (i = 0; i < 8; i++) { |
| 925 | qemu_get_be32s(f, &s->isr[i]); |
| 926 | qemu_get_be32s(f, &s->tmr[i]); |
| 927 | qemu_get_be32s(f, &s->irr[i]); |
| 928 | } |
| 929 | for (i = 0; i < APIC_LVT_NB; i++) { |
| 930 | qemu_get_be32s(f, &s->lvt[i]); |
| 931 | } |
| 932 | qemu_get_be32s(f, &s->esr); |
| 933 | qemu_get_be32s(f, &s->icr[0]); |
| 934 | qemu_get_be32s(f, &s->icr[1]); |
| 935 | qemu_get_be32s(f, &s->divide_conf); |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 936 | s->count_shift=qemu_get_be32(f); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 937 | qemu_get_be32s(f, &s->initial_count); |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 938 | s->initial_count_load_time=qemu_get_be64(f); |
| 939 | s->next_time=qemu_get_be64(f); |
bellard | e6cf6a8 | 2006-08-17 10:48:06 +0000 | [diff] [blame] | 940 | |
| 941 | if (version_id >= 2) |
| 942 | qemu_get_timer(f, s->timer); |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | |
Juan Quintela | 695dcf7 | 2009-08-20 19:42:28 +0200 | [diff] [blame] | 946 | static const VMStateDescription vmstate_apic = { |
| 947 | .name = "apic", |
| 948 | .version_id = 3, |
| 949 | .minimum_version_id = 3, |
| 950 | .minimum_version_id_old = 1, |
| 951 | .load_state_old = apic_load_old, |
| 952 | .fields = (VMStateField []) { |
| 953 | VMSTATE_UINT32(apicbase, APICState), |
| 954 | VMSTATE_UINT8(id, APICState), |
| 955 | VMSTATE_UINT8(arb_id, APICState), |
| 956 | VMSTATE_UINT8(tpr, APICState), |
| 957 | VMSTATE_UINT32(spurious_vec, APICState), |
| 958 | VMSTATE_UINT8(log_dest, APICState), |
| 959 | VMSTATE_UINT8(dest_mode, APICState), |
| 960 | VMSTATE_UINT32_ARRAY(isr, APICState, 8), |
| 961 | VMSTATE_UINT32_ARRAY(tmr, APICState, 8), |
| 962 | VMSTATE_UINT32_ARRAY(irr, APICState, 8), |
| 963 | VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB), |
| 964 | VMSTATE_UINT32(esr, APICState), |
| 965 | VMSTATE_UINT32_ARRAY(icr, APICState, 2), |
| 966 | VMSTATE_UINT32(divide_conf, APICState), |
| 967 | VMSTATE_INT32(count_shift, APICState), |
| 968 | VMSTATE_UINT32(initial_count, APICState), |
| 969 | VMSTATE_INT64(initial_count_load_time, APICState), |
| 970 | VMSTATE_INT64(next_time, APICState), |
| 971 | VMSTATE_TIMER(timer, APICState), |
| 972 | VMSTATE_END_OF_LIST() |
| 973 | } |
| 974 | }; |
| 975 | |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 976 | static void apic_reset(DeviceState *d) |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 977 | { |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 978 | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
Avi Kivity | 4c0960c | 2009-08-17 23:19:53 +0300 | [diff] [blame] | 979 | int bsp; |
aurel32 | fec5fa0 | 2008-09-02 00:09:08 +0000 | [diff] [blame] | 980 | |
Avi Kivity | 4c0960c | 2009-08-17 23:19:53 +0300 | [diff] [blame] | 981 | bsp = cpu_is_bsp(s->cpu_env); |
aurel32 | fec5fa0 | 2008-09-02 00:09:08 +0000 | [diff] [blame] | 982 | s->apicbase = 0xfee00000 | |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 983 | (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE; |
aurel32 | fec5fa0 | 2008-09-02 00:09:08 +0000 | [diff] [blame] | 984 | |
Blue Swirl | 92a16d7 | 2010-06-19 07:47:42 +0000 | [diff] [blame] | 985 | apic_init_reset(d); |
ths | 0e21e12 | 2007-10-09 03:08:56 +0000 | [diff] [blame] | 986 | |
Gleb Natapov | 678e12c | 2009-06-10 15:40:48 +0300 | [diff] [blame] | 987 | if (bsp) { |
aurel32 | a5b38b5 | 2008-04-13 16:08:30 +0000 | [diff] [blame] | 988 | /* |
| 989 | * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization |
| 990 | * time typically by BIOS, so PIC interrupt can be delivered to the |
| 991 | * processor when local APIC is enabled. |
| 992 | */ |
| 993 | s->lvt[APIC_LVT_LINT0] = 0x700; |
| 994 | } |
bellard | d592d30 | 2005-07-23 19:05:37 +0000 | [diff] [blame] | 995 | } |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 996 | |
Avi Kivity | 312b423 | 2011-08-15 17:17:16 +0300 | [diff] [blame] | 997 | static const MemoryRegionOps apic_io_ops = { |
| 998 | .old_mmio = { |
| 999 | .read = { apic_mem_readb, apic_mem_readw, apic_mem_readl, }, |
| 1000 | .write = { apic_mem_writeb, apic_mem_writew, apic_mem_writel, }, |
| 1001 | }, |
| 1002 | .endianness = DEVICE_NATIVE_ENDIAN, |
bellard | 574bbf7 | 2005-01-03 23:27:31 +0000 | [diff] [blame] | 1003 | }; |
| 1004 | |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 1005 | static int apic_init1(SysBusDevice *dev) |
| 1006 | { |
| 1007 | APICState *s = FROM_SYSBUS(APICState, dev); |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 1008 | static int last_apic_idx; |
| 1009 | |
| 1010 | if (last_apic_idx >= MAX_APICS) { |
| 1011 | return -1; |
| 1012 | } |
Avi Kivity | 312b423 | 2011-08-15 17:17:16 +0300 | [diff] [blame] | 1013 | memory_region_init_io(&s->io_memory, &apic_io_ops, s, "apic", |
| 1014 | MSI_ADDR_SIZE); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 1015 | sysbus_init_mmio(dev, &s->io_memory); |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 1016 | |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 1017 | s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s); |
Blue Swirl | 8546b09 | 2010-06-19 07:44:07 +0000 | [diff] [blame] | 1018 | s->idx = last_apic_idx++; |
| 1019 | local_apics[s->idx] = s; |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static SysBusDeviceInfo apic_info = { |
| 1024 | .init = apic_init1, |
| 1025 | .qdev.name = "apic", |
| 1026 | .qdev.size = sizeof(APICState), |
| 1027 | .qdev.vmsd = &vmstate_apic, |
| 1028 | .qdev.reset = apic_reset, |
| 1029 | .qdev.no_user = 1, |
| 1030 | .qdev.props = (Property[]) { |
| 1031 | DEFINE_PROP_UINT8("id", APICState, id, -1), |
| 1032 | DEFINE_PROP_PTR("cpu_env", APICState, cpu_env), |
| 1033 | DEFINE_PROP_END_OF_LIST(), |
| 1034 | } |
| 1035 | }; |
| 1036 | |
| 1037 | static void apic_register_devices(void) |
| 1038 | { |
| 1039 | sysbus_register_withprop(&apic_info); |
| 1040 | } |
| 1041 | |
| 1042 | device_init(apic_register_devices) |