blob: bec493bf63fd60737b3334148f01abafcb249750 [file] [log] [blame]
bellard574bbf72005-01-03 23:27:31 +00001/*
2 * APIC support
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard574bbf72005-01-03 23:27:31 +00004 * 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 Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>
bellard574bbf72005-01-03 23:27:31 +000018 */
pbrook87ecb682007-11-17 17:14:51 +000019#include "hw.h"
Blue Swirlaa28b9b2010-03-21 19:46:26 +000020#include "apic.h"
Jan Kiszka0280b572011-02-03 22:54:11 +010021#include "ioapic.h"
pbrook87ecb682007-11-17 17:14:51 +000022#include "qemu-timer.h"
aurel32bb7e7292008-10-12 20:16:03 +000023#include "host-utils.h"
Blue Swirl8546b092010-06-19 07:44:07 +000024#include "sysbus.h"
Blue Swirld8023f32010-10-20 16:41:28 +000025#include "trace.h"
Jan Kiszkad96e1732011-10-07 09:19:37 +020026#include "pc.h"
bellard574bbf72005-01-03 23:27:31 +000027
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
bellardd592d302005-07-23 19:05:37 +000046/* APIC destination mode */
47#define APIC_DESTMODE_FLAT 0xf
48#define APIC_DESTMODE_CLUSTER 1
49
bellard574bbf72005-01-03 23:27:31 +000050#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 Kiszka0280b572011-02-03 22:54:11 +010062#define APIC_SV_DIRECTED_IO (1<<12)
63#define APIC_SV_ENABLE (1<<8)
bellard574bbf72005-01-03 23:27:31 +000064
bellardd3e9db92005-12-17 01:27:28 +000065#define MAX_APICS 255
66#define MAX_APIC_WORDS 8
67
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030068/* 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. Tsirkin54c96da2009-06-21 19:50:03 +030078#define MSI_ADDR_SIZE 0x100000
79
Blue Swirl92a16d72010-06-19 07:47:42 +000080typedef struct APICState APICState;
81
Blue Swirlcf6d64b2010-06-19 10:42:08 +030082struct APICState {
Blue Swirl8546b092010-06-19 07:44:07 +000083 SysBusDevice busdev;
Avi Kivity312b4232011-08-15 17:17:16 +030084 MemoryRegion io_memory;
Blue Swirl8546b092010-06-19 07:44:07 +000085 void *cpu_env;
bellard574bbf72005-01-03 23:27:31 +000086 uint32_t apicbase;
87 uint8_t id;
bellardd592d302005-07-23 19:05:37 +000088 uint8_t arb_id;
bellard574bbf72005-01-03 23:27:31 +000089 uint8_t tpr;
90 uint32_t spurious_vec;
bellardd592d302005-07-23 19:05:37 +000091 uint8_t log_dest;
92 uint8_t dest_mode;
bellard574bbf72005-01-03 23:27:31 +000093 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 Natapov678e12c2009-06-10 15:40:48 +0300104 uint32_t idx;
bellard574bbf72005-01-03 23:27:31 +0000105 QEMUTimer *timer;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300106 int sipi_vector;
107 int wait_for_sipi;
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300108};
bellard574bbf72005-01-03 23:27:31 +0000109
bellardd3e9db92005-12-17 01:27:28 +0000110static APICState *local_apics[MAX_APICS + 1];
aliguori73822ec2009-01-15 20:11:34 +0000111static int apic_irq_delivered;
112
bellardd592d302005-07-23 19:05:37 +0000113static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
114static void apic_update_irq(APICState *s);
aliguori610626a2009-03-12 20:25:12 +0000115static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
116 uint8_t dest, uint8_t dest_mode);
bellardd592d302005-07-23 19:05:37 +0000117
aurel323b63c042008-12-06 10:46:35 +0000118/* Find first bit starting from msb */
119static int fls_bit(uint32_t value)
120{
121 return 31 - clz32(value);
122}
123
aurel32e95f5492008-10-12 00:53:17 +0000124/* Find first bit starting from lsb */
bellardd3e9db92005-12-17 01:27:28 +0000125static int ffs_bit(uint32_t value)
126{
aurel32bb7e7292008-10-12 20:16:03 +0000127 return ctz32(value);
bellardd3e9db92005-12-17 01:27:28 +0000128}
129
130static 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
138static 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
aliguori73822ec2009-01-15 20:11:34 +0000146static 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 Swirlcf6d64b2010-06-19 10:42:08 +0300154static void apic_local_deliver(APICState *s, int vector)
aurel32a5b38b52008-04-13 16:08:30 +0000155{
aurel32a5b38b52008-04-13 16:08:30 +0000156 uint32_t lvt = s->lvt[vector];
157 int trigger_mode;
158
Blue Swirld8023f32010-10-20 16:41:28 +0000159 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
160
aurel32a5b38b52008-04-13 16:08:30 +0000161 if (lvt & APIC_LVT_MASKED)
162 return;
163
164 switch ((lvt >> 8) & 7) {
165 case APIC_DM_SMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300166 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
aurel32a5b38b52008-04-13 16:08:30 +0000167 break;
168
169 case APIC_DM_NMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300170 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
aurel32a5b38b52008-04-13 16:08:30 +0000171 break;
172
173 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300174 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel32a5b38b52008-04-13 16:08:30 +0000175 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 Swirl92a16d72010-06-19 07:47:42 +0000186void apic_deliver_pic_intr(DeviceState *d, int level)
aurel321a7de942008-08-21 03:14:52 +0000187{
Blue Swirl92a16d72010-06-19 07:47:42 +0000188 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
189
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300190 if (level) {
191 apic_local_deliver(s, APIC_LVT_LINT0);
192 } else {
aurel321a7de942008-08-21 03:14:52 +0000193 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 Swirlcf6d64b2010-06-19 10:42:08 +0300202 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000203 break;
204 }
205 }
206}
207
Jan Kiszka02c09192011-10-18 00:00:06 +0800208void 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
bellardd3e9db92005-12-17 01:27:28 +0000215#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
ths5fafdf22007-09-16 21:08:06 +0000233static void apic_bus_deliver(const uint32_t *deliver_bitmask,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200234 uint8_t delivery_mode, uint8_t vector_num,
bellardd592d302005-07-23 19:05:37 +0000235 uint8_t trigger_mode)
236{
237 APICState *apic_iter;
238
239 switch (delivery_mode) {
240 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000241 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000242 {
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 }
bellard8dd69b82005-11-23 20:59:44 +0000257 }
bellardd3e9db92005-12-17 01:27:28 +0000258 return;
bellard8dd69b82005-11-23 20:59:44 +0000259
bellardd592d302005-07-23 19:05:37 +0000260 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000261 break;
262
263 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000264 foreach_apic(apic_iter, deliver_bitmask,
265 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
266 return;
267
bellardd592d302005-07-23 19:05:37 +0000268 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000269 foreach_apic(apic_iter, deliver_bitmask,
270 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
271 return;
bellardd592d302005-07-23 19:05:37 +0000272
273 case APIC_DM_INIT:
274 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000275 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300276 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000277 return;
ths3b46e622007-09-17 08:09:54 +0000278
bellardd592d302005-07-23 19:05:37 +0000279 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000280 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000281 break;
282
283 default:
284 return;
285 }
286
ths5fafdf22007-09-16 21:08:06 +0000287 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000288 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000289}
bellard574bbf72005-01-03 23:27:31 +0000290
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200291void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
292 uint8_t vector_num, uint8_t trigger_mode)
aliguori610626a2009-03-12 20:25:12 +0000293{
294 uint32_t deliver_bitmask[MAX_APIC_WORDS];
295
Blue Swirld8023f32010-10-20 16:41:28 +0000296 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200297 trigger_mode);
Blue Swirld8023f32010-10-20 16:41:28 +0000298
aliguori610626a2009-03-12 20:25:12 +0000299 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200300 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000301}
302
Blue Swirl92a16d72010-06-19 07:47:42 +0000303void cpu_set_apic_base(DeviceState *d, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000304{
Blue Swirl92a16d72010-06-19 07:47:42 +0000305 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
306
Blue Swirld8023f32010-10-20 16:41:28 +0000307 trace_cpu_set_apic_base(val);
308
aurel322c7c13d2009-04-08 22:56:26 +0000309 if (!s)
310 return;
ths5fafdf22007-09-16 21:08:06 +0000311 s->apicbase = (val & 0xfffff000) |
bellard574bbf72005-01-03 23:27:31 +0000312 (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 Swirl0e26b7b2010-06-19 10:42:34 +0300316 cpu_clear_apic_feature(s->cpu_env);
bellard574bbf72005-01-03 23:27:31 +0000317 s->spurious_vec &= ~APIC_SV_ENABLE;
318 }
319}
320
Blue Swirl92a16d72010-06-19 07:47:42 +0000321uint64_t cpu_get_apic_base(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000322{
Blue Swirl92a16d72010-06-19 07:47:42 +0000323 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
324
Blue Swirld8023f32010-10-20 16:41:28 +0000325 trace_cpu_get_apic_base(s ? (uint64_t)s->apicbase: 0);
326
aurel322c7c13d2009-04-08 22:56:26 +0000327 return s ? s->apicbase : 0;
bellard574bbf72005-01-03 23:27:31 +0000328}
329
Blue Swirl92a16d72010-06-19 07:47:42 +0000330void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
bellard9230e662005-01-23 20:46:56 +0000331{
Blue Swirl92a16d72010-06-19 07:47:42 +0000332 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
333
aurel322c7c13d2009-04-08 22:56:26 +0000334 if (!s)
335 return;
bellard9230e662005-01-23 20:46:56 +0000336 s->tpr = (val & 0x0f) << 4;
bellardd592d302005-07-23 19:05:37 +0000337 apic_update_irq(s);
bellard9230e662005-01-23 20:46:56 +0000338}
339
Blue Swirl92a16d72010-06-19 07:47:42 +0000340uint8_t cpu_get_apic_tpr(DeviceState *d)
bellard9230e662005-01-23 20:46:56 +0000341{
Blue Swirl92a16d72010-06-19 07:47:42 +0000342 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
343
aurel322c7c13d2009-04-08 22:56:26 +0000344 return s ? s->tpr >> 4 : 0;
bellard9230e662005-01-23 20:46:56 +0000345}
346
bellardd592d302005-07-23 19:05:37 +0000347/* return -1 if no bit is set */
348static int get_highest_priority_int(uint32_t *tab)
349{
350 int i;
351 for(i = 7; i >= 0; i--) {
352 if (tab[i] != 0) {
aurel323b63c042008-12-06 10:46:35 +0000353 return i * 32 + fls_bit(tab[i]);
bellardd592d302005-07-23 19:05:37 +0000354 }
355 }
356 return -1;
357}
358
bellard574bbf72005-01-03 23:27:31 +0000359static 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
bellardd592d302005-07-23 19:05:37 +0000375static int apic_get_arb_pri(APICState *s)
376{
377 /* XXX: arbitration */
378 return 0;
379}
380
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200381
382/*
383 * <0 - low prio interrupt,
384 * 0 - no interrupt,
385 * >0 - interrupt number
386 */
387static 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
bellard574bbf72005-01-03 23:27:31 +0000402/* signal the CPU if an irq is pending */
403static void apic_update_irq(APICState *s)
404{
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200405 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
bellardd592d302005-07-23 19:05:37 +0000406 return;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200407 }
408 if (apic_irq_pending(s) > 0) {
409 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
Jan Kiszkad96e1732011-10-07 09:19:37 +0200410 } 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 Natapov0fbfbb52011-02-07 16:14:44 +0200413 }
bellard574bbf72005-01-03 23:27:31 +0000414}
415
Jan Kiszka343270e2011-12-13 15:39:04 +0100416void apic_report_irq_delivered(int delivered)
417{
418 apic_irq_delivered += delivered;
419
420 trace_apic_report_irq_delivered(apic_irq_delivered);
421}
422
aliguori73822ec2009-01-15 20:11:34 +0000423void apic_reset_irq_delivered(void)
424{
Blue Swirld8023f32010-10-20 16:41:28 +0000425 trace_apic_reset_irq_delivered(apic_irq_delivered);
426
aliguori73822ec2009-01-15 20:11:34 +0000427 apic_irq_delivered = 0;
428}
429
430int apic_get_irq_delivered(void)
431{
Blue Swirld8023f32010-10-20 16:41:28 +0000432 trace_apic_get_irq_delivered(apic_irq_delivered);
433
aliguori73822ec2009-01-15 20:11:34 +0000434 return apic_irq_delivered;
435}
436
bellard574bbf72005-01-03 23:27:31 +0000437static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
438{
Jan Kiszka343270e2011-12-13 15:39:04 +0100439 apic_report_irq_delivered(!get_bit(s->irr, vector_num));
aliguori73822ec2009-01-15 20:11:34 +0000440
bellard574bbf72005-01-03 23:27:31 +0000441 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
449static 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 Kiszka0280b572011-02-03 22:54:11 +0100456 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && get_bit(s->tmr, isrv)) {
457 ioapic_eoi_broadcast(isrv);
458 }
bellard574bbf72005-01-03 23:27:31 +0000459 apic_update_irq(s);
460}
461
Gleb Natapov678e12c2009-06-10 15:40:48 +0300462static 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 Williamsonb538e532010-11-05 16:01:29 -0600474 if (!apic)
475 break;
Gleb Natapov678e12c2009-06-10 15:40:48 +0300476 }
477
478 return -1;
479}
480
bellardd3e9db92005-12-17 01:27:28 +0000481static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
482 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000483{
bellardd592d302005-07-23 19:05:37 +0000484 APICState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000485 int i;
bellardd592d302005-07-23 19:05:37 +0000486
487 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000488 if (dest == 0xff) {
489 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
490 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300491 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000492 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300493 if (idx >= 0)
494 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000495 }
bellardd592d302005-07-23 19:05:37 +0000496 } else {
497 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000498 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 Williamsonb538e532010-11-05 16:01:29 -0600511 } else {
512 break;
bellardd3e9db92005-12-17 01:27:28 +0000513 }
bellardd592d302005-07-23 19:05:37 +0000514 }
515 }
bellardd592d302005-07-23 19:05:37 +0000516}
517
Blue Swirl92a16d72010-06-19 07:47:42 +0000518void apic_init_reset(DeviceState *d)
bellardd592d302005-07-23 19:05:37 +0000519{
Blue Swirl92a16d72010-06-19 07:47:42 +0000520 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellardd592d302005-07-23 19:05:37 +0000521 int i;
522
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300523 if (!s)
524 return;
525
bellardd592d302005-07-23 19:05:37 +0000526 s->tpr = 0;
527 s->spurious_vec = 0xff;
528 s->log_dest = 0;
bellarde0fd8782005-11-21 23:26:26 +0000529 s->dest_mode = 0xf;
bellardd592d302005-07-23 19:05:37 +0000530 memset(s->isr, 0, sizeof(s->isr));
531 memset(s->tmr, 0, sizeof(s->tmr));
532 memset(s->irr, 0, sizeof(s->irr));
bellardb4511722006-10-08 18:20:51 +0000533 for(i = 0; i < APIC_LVT_NB; i++)
534 s->lvt[i] = 1 << 16; /* mask LVT */
bellardd592d302005-07-23 19:05:37 +0000535 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 Natapovb09ea7d2009-06-17 23:26:59 +0300542 s->wait_for_sipi = 1;
Jan Kiszkaab388a92011-10-16 11:59:30 +0200543
544 qemu_del_timer(s->timer);
bellardd592d302005-07-23 19:05:37 +0000545}
546
bellarde0fd8782005-11-21 23:26:26 +0000547static void apic_startup(APICState *s, int vector_num)
548{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300549 s->sipi_vector = vector_num;
550 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
551}
552
Blue Swirl92a16d72010-06-19 07:47:42 +0000553void apic_sipi(DeviceState *d)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300554{
Blue Swirl92a16d72010-06-19 07:47:42 +0000555 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
556
Blue Swirl4a942ce2010-06-19 10:42:31 +0300557 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300558
559 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000560 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300561 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300562 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000563}
564
Blue Swirl92a16d72010-06-19 07:47:42 +0000565static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
bellardd592d302005-07-23 19:05:37 +0000566 uint8_t delivery_mode, uint8_t vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200567 uint8_t trigger_mode)
bellardd592d302005-07-23 19:05:37 +0000568{
Blue Swirl92a16d72010-06-19 07:47:42 +0000569 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellardd3e9db92005-12-17 01:27:28 +0000570 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000571 int dest_shorthand = (s->icr[0] >> 18) & 3;
572 APICState *apic_iter;
573
bellarde0fd8782005-11-21 23:26:26 +0000574 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000575 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 Natapov678e12c2009-06-10 15:40:48 +0300580 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000581 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 Natapov678e12c2009-06-10 15:40:48 +0300587 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000588 break;
bellarde0fd8782005-11-21 23:26:26 +0000589 }
590
bellardd592d302005-07-23 19:05:37 +0000591 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000592 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) {
ths5fafdf22007-09-16 21:08:06 +0000597 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000598 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000599 return;
600 }
601 }
602 break;
603
604 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000605 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000606 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000607 return;
608 }
609
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200610 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
bellardd592d302005-07-23 19:05:37 +0000611}
612
Blue Swirl92a16d72010-06-19 07:47:42 +0000613int apic_get_interrupt(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000614{
Blue Swirl92a16d72010-06-19 07:47:42 +0000615 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000616 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;
ths3b46e622007-09-17 08:09:54 +0000624
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200625 intno = apic_irq_pending(s);
626
627 if (intno == 0) {
bellard574bbf72005-01-03 23:27:31 +0000628 return -1;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200629 } else if (intno < 0) {
bellardd592d302005-07-23 19:05:37 +0000630 return s->spurious_vec & 0xff;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200631 }
bellardb4511722006-10-08 18:20:51 +0000632 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000633 set_bit(s->isr, intno);
634 apic_update_irq(s);
635 return intno;
636}
637
Blue Swirl92a16d72010-06-19 07:47:42 +0000638int apic_accept_pic_intr(DeviceState *d)
ths0e21e122007-10-09 03:08:56 +0000639{
Blue Swirl92a16d72010-06-19 07:47:42 +0000640 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
ths0e21e122007-10-09 03:08:56 +0000641 uint32_t lvt0;
642
643 if (!s)
644 return -1;
645
646 lvt0 = s->lvt[APIC_LVT_LINT0];
647
aurel32a5b38b52008-04-13 16:08:30 +0000648 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
649 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000650 return 1;
651
652 return 0;
653}
654
bellard574bbf72005-01-03 23:27:31 +0000655static uint32_t apic_get_current_count(APICState *s)
656{
657 int64_t d;
658 uint32_t val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100659 d = (qemu_get_clock_ns(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000660 s->count_shift;
661 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
662 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000663 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000664 } else {
665 if (d >= s->initial_count)
666 val = 0;
667 else
668 val = s->initial_count - d;
669 }
670 return val;
671}
672
673static void apic_timer_update(APICState *s, int64_t current_time)
674{
675 int64_t next_time, d;
ths3b46e622007-09-17 08:09:54 +0000676
bellard574bbf72005-01-03 23:27:31 +0000677 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
ths5fafdf22007-09-16 21:08:06 +0000678 d = (current_time - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000679 s->count_shift;
680 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
aliguori681f8c22008-08-18 14:19:42 +0000681 if (!s->initial_count)
682 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000683 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
bellard574bbf72005-01-03 23:27:31 +0000684 } else {
685 if (d >= s->initial_count)
686 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000687 d = (uint64_t)s->initial_count + 1;
bellard574bbf72005-01-03 23:27:31 +0000688 }
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
698static void apic_timer(void *opaque)
699{
700 APICState *s = opaque;
701
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300702 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000703 apic_timer_update(s, s->next_time);
704}
705
Anthony Liguoric227f092009-10-01 16:12:16 -0500706static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000707{
708 return 0;
709}
710
Anthony Liguoric227f092009-10-01 16:12:16 -0500711static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000712{
713 return 0;
714}
715
Anthony Liguoric227f092009-10-01 16:12:16 -0500716static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000717{
718}
719
Anthony Liguoric227f092009-10-01 16:12:16 -0500720static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000721{
722}
723
Anthony Liguoric227f092009-10-01 16:12:16 -0500724static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000725{
Blue Swirl92a16d72010-06-19 07:47:42 +0000726 DeviceState *d;
bellard574bbf72005-01-03 23:27:31 +0000727 APICState *s;
728 uint32_t val;
729 int index;
730
Blue Swirl92a16d72010-06-19 07:47:42 +0000731 d = cpu_get_current_apic();
732 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000733 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300734 }
Blue Swirl92a16d72010-06-19 07:47:42 +0000735 s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000736
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;
bellardd592d302005-07-23 19:05:37 +0000748 case 0x09:
749 val = apic_get_arb_pri(s);
750 break;
bellard574bbf72005-01-03 23:27:31 +0000751 case 0x0a:
752 /* ppr */
753 val = apic_get_ppr(s);
754 break;
aurel32b237db32008-03-28 22:31:36 +0000755 case 0x0b:
756 val = 0;
757 break;
bellardd592d302005-07-23 19:05:37 +0000758 case 0x0d:
759 val = s->log_dest << 24;
760 break;
761 case 0x0e:
762 val = s->dest_mode << 28;
763 break;
bellard574bbf72005-01-03 23:27:31 +0000764 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;
bellard574bbf72005-01-03 23:27:31 +0000779 case 0x30:
780 case 0x31:
781 val = s->icr[index & 1];
782 break;
bellarde0fd8782005-11-21 23:26:26 +0000783 case 0x32 ... 0x37:
784 val = s->lvt[index - 0x32];
785 break;
bellard574bbf72005-01-03 23:27:31 +0000786 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 Swirld8023f32010-10-20 16:41:28 +0000800 trace_apic_mem_readl(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000801 return val;
802}
803
Andreas Färberf5095c62010-12-19 17:22:39 +0100804static void apic_send_msi(target_phys_addr_t addr, uint32_t data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300805{
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 Kiszka1f6f4082011-08-22 17:46:31 +0200812 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300813}
814
Anthony Liguoric227f092009-10-01 16:12:16 -0500815static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000816{
Blue Swirl92a16d72010-06-19 07:47:42 +0000817 DeviceState *d;
bellard574bbf72005-01-03 23:27:31 +0000818 APICState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300819 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 }
bellard574bbf72005-01-03 23:27:31 +0000829
Blue Swirl92a16d72010-06-19 07:47:42 +0000830 d = cpu_get_current_apic();
831 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000832 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300833 }
Blue Swirl92a16d72010-06-19 07:47:42 +0000834 s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000835
Blue Swirld8023f32010-10-20 16:41:28 +0000836 trace_apic_mem_writel(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000837
bellard574bbf72005-01-03 23:27:31 +0000838 switch(index) {
839 case 0x02:
840 s->id = (val >> 24);
841 break;
bellarde0fd8782005-11-21 23:26:26 +0000842 case 0x03:
843 break;
bellard574bbf72005-01-03 23:27:31 +0000844 case 0x08:
845 s->tpr = val;
bellardd592d302005-07-23 19:05:37 +0000846 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000847 break;
bellarde0fd8782005-11-21 23:26:26 +0000848 case 0x09:
849 case 0x0a:
850 break;
bellard574bbf72005-01-03 23:27:31 +0000851 case 0x0b: /* EOI */
852 apic_eoi(s);
853 break;
bellardd592d302005-07-23 19:05:37 +0000854 case 0x0d:
855 s->log_dest = val >> 24;
856 break;
857 case 0x0e:
858 s->dest_mode = val >> 28;
859 break;
bellard574bbf72005-01-03 23:27:31 +0000860 case 0x0f:
861 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000862 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000863 break;
bellarde0fd8782005-11-21 23:26:26 +0000864 case 0x10 ... 0x17:
865 case 0x18 ... 0x1f:
866 case 0x20 ... 0x27:
867 case 0x28:
868 break;
bellard574bbf72005-01-03 23:27:31 +0000869 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000870 s->icr[0] = val;
Blue Swirl92a16d72010-06-19 07:47:42 +0000871 apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
bellardd592d302005-07-23 19:05:37 +0000872 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200873 (s->icr[0] >> 15) & 1);
bellardd592d302005-07-23 19:05:37 +0000874 break;
bellard574bbf72005-01-03 23:27:31 +0000875 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000876 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000877 break;
878 case 0x32 ... 0x37:
879 {
880 int n = index - 0x32;
881 s->lvt[n] = val;
882 if (n == APIC_LVT_TIMER)
Paolo Bonzini74475452011-03-11 16:47:48 +0100883 apic_timer_update(s, qemu_get_clock_ns(vm_clock));
bellard574bbf72005-01-03 23:27:31 +0000884 }
885 break;
886 case 0x38:
887 s->initial_count = val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100888 s->initial_count_load_time = qemu_get_clock_ns(vm_clock);
bellard574bbf72005-01-03 23:27:31 +0000889 apic_timer_update(s, s->initial_count_load_time);
890 break;
bellarde0fd8782005-11-21 23:26:26 +0000891 case 0x39:
892 break;
bellard574bbf72005-01-03 23:27:31 +0000893 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 Quintela695dcf72009-08-20 19:42:28 +0200907/* This function is only used for old state version 1 and 2 */
908static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
bellardd592d302005-07-23 19:05:37 +0000909{
910 APICState *s = opaque;
911 int i;
912
bellarde6cf6a82006-08-17 10:48:06 +0000913 if (version_id > 2)
bellardd592d302005-07-23 19:05:37 +0000914 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);
thsbee8d682007-12-16 23:41:11 +0000936 s->count_shift=qemu_get_be32(f);
bellardd592d302005-07-23 19:05:37 +0000937 qemu_get_be32s(f, &s->initial_count);
thsbee8d682007-12-16 23:41:11 +0000938 s->initial_count_load_time=qemu_get_be64(f);
939 s->next_time=qemu_get_be64(f);
bellarde6cf6a82006-08-17 10:48:06 +0000940
941 if (version_id >= 2)
942 qemu_get_timer(f, s->timer);
bellardd592d302005-07-23 19:05:37 +0000943 return 0;
944}
945
Juan Quintela695dcf72009-08-20 19:42:28 +0200946static 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 Swirl8546b092010-06-19 07:44:07 +0000976static void apic_reset(DeviceState *d)
bellardd592d302005-07-23 19:05:37 +0000977{
Blue Swirl8546b092010-06-19 07:44:07 +0000978 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
Avi Kivity4c0960c2009-08-17 23:19:53 +0300979 int bsp;
aurel32fec5fa02008-09-02 00:09:08 +0000980
Avi Kivity4c0960c2009-08-17 23:19:53 +0300981 bsp = cpu_is_bsp(s->cpu_env);
aurel32fec5fa02008-09-02 00:09:08 +0000982 s->apicbase = 0xfee00000 |
Gleb Natapov678e12c2009-06-10 15:40:48 +0300983 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
aurel32fec5fa02008-09-02 00:09:08 +0000984
Blue Swirl92a16d72010-06-19 07:47:42 +0000985 apic_init_reset(d);
ths0e21e122007-10-09 03:08:56 +0000986
Gleb Natapov678e12c2009-06-10 15:40:48 +0300987 if (bsp) {
aurel32a5b38b52008-04-13 16:08:30 +0000988 /*
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 }
bellardd592d302005-07-23 19:05:37 +0000995}
bellard574bbf72005-01-03 23:27:31 +0000996
Avi Kivity312b4232011-08-15 17:17:16 +0300997static 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,
bellard574bbf72005-01-03 23:27:31 +00001003};
1004
Blue Swirl8546b092010-06-19 07:44:07 +00001005static int apic_init1(SysBusDevice *dev)
1006{
1007 APICState *s = FROM_SYSBUS(APICState, dev);
Blue Swirl8546b092010-06-19 07:44:07 +00001008 static int last_apic_idx;
1009
1010 if (last_apic_idx >= MAX_APICS) {
1011 return -1;
1012 }
Avi Kivity312b4232011-08-15 17:17:16 +03001013 memory_region_init_io(&s->io_memory, &apic_io_ops, s, "apic",
1014 MSI_ADDR_SIZE);
Avi Kivity750ecd42011-11-27 11:38:10 +02001015 sysbus_init_mmio(dev, &s->io_memory);
Blue Swirl8546b092010-06-19 07:44:07 +00001016
Paolo Bonzini74475452011-03-11 16:47:48 +01001017 s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s);
Blue Swirl8546b092010-06-19 07:44:07 +00001018 s->idx = last_apic_idx++;
1019 local_apics[s->idx] = s;
1020 return 0;
1021}
1022
1023static 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
1037static void apic_register_devices(void)
1038{
1039 sysbus_register_withprop(&apic_info);
1040}
1041
1042device_init(apic_register_devices)