blob: 632d6eba04b44b470227d694dd72f06508a5572a [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"
20#include "pc.h"
Blue Swirlaa28b9b2010-03-21 19:46:26 +000021#include "apic.h"
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030022#include "pci.h"
23#include "msix.h"
pbrook87ecb682007-11-17 17:14:51 +000024#include "qemu-timer.h"
aurel32bb7e7292008-10-12 20:16:03 +000025#include "host-utils.h"
Jan Kiszka8d2ba1f2009-06-27 09:24:58 +020026#include "kvm.h"
bellard574bbf72005-01-03 23:27:31 +000027
28//#define DEBUG_APIC
Blue Swirl0a3c5922010-05-29 20:23:48 +000029//#define DEBUG_COALESCING
30
31#ifdef DEBUG_APIC
32#define DPRINTF(fmt, ...) \
33 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
34#else
35#define DPRINTF(fmt, ...)
36#endif
37
38#ifdef DEBUG_COALESCING
39#define DPRINTF_C(fmt, ...) \
40 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
41#else
42#define DPRINTF_C(fmt, ...)
43#endif
bellard574bbf72005-01-03 23:27:31 +000044
45/* APIC Local Vector Table */
46#define APIC_LVT_TIMER 0
47#define APIC_LVT_THERMAL 1
48#define APIC_LVT_PERFORM 2
49#define APIC_LVT_LINT0 3
50#define APIC_LVT_LINT1 4
51#define APIC_LVT_ERROR 5
52#define APIC_LVT_NB 6
53
54/* APIC delivery modes */
55#define APIC_DM_FIXED 0
56#define APIC_DM_LOWPRI 1
57#define APIC_DM_SMI 2
58#define APIC_DM_NMI 4
59#define APIC_DM_INIT 5
60#define APIC_DM_SIPI 6
61#define APIC_DM_EXTINT 7
62
bellardd592d302005-07-23 19:05:37 +000063/* APIC destination mode */
64#define APIC_DESTMODE_FLAT 0xf
65#define APIC_DESTMODE_CLUSTER 1
66
bellard574bbf72005-01-03 23:27:31 +000067#define APIC_TRIGGER_EDGE 0
68#define APIC_TRIGGER_LEVEL 1
69
70#define APIC_LVT_TIMER_PERIODIC (1<<17)
71#define APIC_LVT_MASKED (1<<16)
72#define APIC_LVT_LEVEL_TRIGGER (1<<15)
73#define APIC_LVT_REMOTE_IRR (1<<14)
74#define APIC_INPUT_POLARITY (1<<13)
75#define APIC_SEND_PENDING (1<<12)
76
77#define ESR_ILLEGAL_ADDRESS (1 << 7)
78
79#define APIC_SV_ENABLE (1 << 8)
80
bellardd3e9db92005-12-17 01:27:28 +000081#define MAX_APICS 255
82#define MAX_APIC_WORDS 8
83
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030084/* Intel APIC constants: from include/asm/msidef.h */
85#define MSI_DATA_VECTOR_SHIFT 0
86#define MSI_DATA_VECTOR_MASK 0x000000ff
87#define MSI_DATA_DELIVERY_MODE_SHIFT 8
88#define MSI_DATA_TRIGGER_SHIFT 15
89#define MSI_DATA_LEVEL_SHIFT 14
90#define MSI_ADDR_DEST_MODE_SHIFT 2
91#define MSI_ADDR_DEST_ID_SHIFT 12
92#define MSI_ADDR_DEST_ID_MASK 0x00ffff0
93
94#define MSI_ADDR_BASE 0xfee00000
95#define MSI_ADDR_SIZE 0x100000
96
Blue Swirlcf6d64b2010-06-19 10:42:08 +030097struct APICState {
bellard574bbf72005-01-03 23:27:31 +000098 CPUState *cpu_env;
99 uint32_t apicbase;
100 uint8_t id;
bellardd592d302005-07-23 19:05:37 +0000101 uint8_t arb_id;
bellard574bbf72005-01-03 23:27:31 +0000102 uint8_t tpr;
103 uint32_t spurious_vec;
bellardd592d302005-07-23 19:05:37 +0000104 uint8_t log_dest;
105 uint8_t dest_mode;
bellard574bbf72005-01-03 23:27:31 +0000106 uint32_t isr[8]; /* in service register */
107 uint32_t tmr[8]; /* trigger mode register */
108 uint32_t irr[8]; /* interrupt request register */
109 uint32_t lvt[APIC_LVT_NB];
110 uint32_t esr; /* error register */
111 uint32_t icr[2];
112
113 uint32_t divide_conf;
114 int count_shift;
115 uint32_t initial_count;
116 int64_t initial_count_load_time, next_time;
Gleb Natapov678e12c2009-06-10 15:40:48 +0300117 uint32_t idx;
bellard574bbf72005-01-03 23:27:31 +0000118 QEMUTimer *timer;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300119 int sipi_vector;
120 int wait_for_sipi;
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300121};
bellard574bbf72005-01-03 23:27:31 +0000122
123static int apic_io_memory;
bellardd3e9db92005-12-17 01:27:28 +0000124static APICState *local_apics[MAX_APICS + 1];
Gleb Natapov678e12c2009-06-10 15:40:48 +0300125static int last_apic_idx = 0;
aliguori73822ec2009-01-15 20:11:34 +0000126static int apic_irq_delivered;
127
bellardd592d302005-07-23 19:05:37 +0000128
bellardd592d302005-07-23 19:05:37 +0000129static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
130static void apic_update_irq(APICState *s);
aliguori610626a2009-03-12 20:25:12 +0000131static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
132 uint8_t dest, uint8_t dest_mode);
bellardd592d302005-07-23 19:05:37 +0000133
aurel323b63c042008-12-06 10:46:35 +0000134/* Find first bit starting from msb */
135static int fls_bit(uint32_t value)
136{
137 return 31 - clz32(value);
138}
139
aurel32e95f5492008-10-12 00:53:17 +0000140/* Find first bit starting from lsb */
bellardd3e9db92005-12-17 01:27:28 +0000141static int ffs_bit(uint32_t value)
142{
aurel32bb7e7292008-10-12 20:16:03 +0000143 return ctz32(value);
bellardd3e9db92005-12-17 01:27:28 +0000144}
145
146static inline void set_bit(uint32_t *tab, int index)
147{
148 int i, mask;
149 i = index >> 5;
150 mask = 1 << (index & 0x1f);
151 tab[i] |= mask;
152}
153
154static inline void reset_bit(uint32_t *tab, int index)
155{
156 int i, mask;
157 i = index >> 5;
158 mask = 1 << (index & 0x1f);
159 tab[i] &= ~mask;
160}
161
aliguori73822ec2009-01-15 20:11:34 +0000162static inline int get_bit(uint32_t *tab, int index)
163{
164 int i, mask;
165 i = index >> 5;
166 mask = 1 << (index & 0x1f);
167 return !!(tab[i] & mask);
168}
169
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300170static void apic_local_deliver(APICState *s, int vector)
aurel32a5b38b52008-04-13 16:08:30 +0000171{
aurel32a5b38b52008-04-13 16:08:30 +0000172 uint32_t lvt = s->lvt[vector];
173 int trigger_mode;
174
Blue Swirl0a3c5922010-05-29 20:23:48 +0000175 DPRINTF("%s: vector %d delivery mode %d\n", __func__, vector,
176 (lvt >> 8) & 7);
aurel32a5b38b52008-04-13 16:08:30 +0000177 if (lvt & APIC_LVT_MASKED)
178 return;
179
180 switch ((lvt >> 8) & 7) {
181 case APIC_DM_SMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300182 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
aurel32a5b38b52008-04-13 16:08:30 +0000183 break;
184
185 case APIC_DM_NMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300186 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
aurel32a5b38b52008-04-13 16:08:30 +0000187 break;
188
189 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300190 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel32a5b38b52008-04-13 16:08:30 +0000191 break;
192
193 case APIC_DM_FIXED:
194 trigger_mode = APIC_TRIGGER_EDGE;
195 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
196 (lvt & APIC_LVT_LEVEL_TRIGGER))
197 trigger_mode = APIC_TRIGGER_LEVEL;
198 apic_set_irq(s, lvt & 0xff, trigger_mode);
199 }
200}
201
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300202void apic_deliver_pic_intr(APICState *s, int level)
aurel321a7de942008-08-21 03:14:52 +0000203{
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300204 if (level) {
205 apic_local_deliver(s, APIC_LVT_LINT0);
206 } else {
aurel321a7de942008-08-21 03:14:52 +0000207 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
208
209 switch ((lvt >> 8) & 7) {
210 case APIC_DM_FIXED:
211 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
212 break;
213 reset_bit(s->irr, lvt & 0xff);
214 /* fall through */
215 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300216 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000217 break;
218 }
219 }
220}
221
bellardd3e9db92005-12-17 01:27:28 +0000222#define foreach_apic(apic, deliver_bitmask, code) \
223{\
224 int __i, __j, __mask;\
225 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
226 __mask = deliver_bitmask[__i];\
227 if (__mask) {\
228 for(__j = 0; __j < 32; __j++) {\
229 if (__mask & (1 << __j)) {\
230 apic = local_apics[__i * 32 + __j];\
231 if (apic) {\
232 code;\
233 }\
234 }\
235 }\
236 }\
237 }\
238}
239
ths5fafdf22007-09-16 21:08:06 +0000240static void apic_bus_deliver(const uint32_t *deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000241 uint8_t delivery_mode,
bellardd592d302005-07-23 19:05:37 +0000242 uint8_t vector_num, uint8_t polarity,
243 uint8_t trigger_mode)
244{
245 APICState *apic_iter;
246
247 switch (delivery_mode) {
248 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000249 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000250 {
251 int i, d;
252 d = -1;
253 for(i = 0; i < MAX_APIC_WORDS; i++) {
254 if (deliver_bitmask[i]) {
255 d = i * 32 + ffs_bit(deliver_bitmask[i]);
256 break;
257 }
258 }
259 if (d >= 0) {
260 apic_iter = local_apics[d];
261 if (apic_iter) {
262 apic_set_irq(apic_iter, vector_num, trigger_mode);
263 }
264 }
bellard8dd69b82005-11-23 20:59:44 +0000265 }
bellardd3e9db92005-12-17 01:27:28 +0000266 return;
bellard8dd69b82005-11-23 20:59:44 +0000267
bellardd592d302005-07-23 19:05:37 +0000268 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000269 break;
270
271 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000272 foreach_apic(apic_iter, deliver_bitmask,
273 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
274 return;
275
bellardd592d302005-07-23 19:05:37 +0000276 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000277 foreach_apic(apic_iter, deliver_bitmask,
278 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
279 return;
bellardd592d302005-07-23 19:05:37 +0000280
281 case APIC_DM_INIT:
282 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000283 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300284 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000285 return;
ths3b46e622007-09-17 08:09:54 +0000286
bellardd592d302005-07-23 19:05:37 +0000287 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000288 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000289 break;
290
291 default:
292 return;
293 }
294
ths5fafdf22007-09-16 21:08:06 +0000295 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000296 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000297}
bellard574bbf72005-01-03 23:27:31 +0000298
aliguori610626a2009-03-12 20:25:12 +0000299void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
300 uint8_t delivery_mode, uint8_t vector_num,
301 uint8_t polarity, uint8_t trigger_mode)
302{
303 uint32_t deliver_bitmask[MAX_APIC_WORDS];
304
Blue Swirl0a3c5922010-05-29 20:23:48 +0000305 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
306 " polarity %d trigger_mode %d\n", __func__, dest, dest_mode,
307 delivery_mode, vector_num, polarity, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000308 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
309 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
310 trigger_mode);
311}
312
Blue Swirl4a942ce2010-06-19 10:42:31 +0300313void cpu_set_apic_base(APICState *s, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000314{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000315 DPRINTF("cpu_set_apic_base: %016" PRIx64 "\n", val);
aurel322c7c13d2009-04-08 22:56:26 +0000316 if (!s)
317 return;
ths5fafdf22007-09-16 21:08:06 +0000318 s->apicbase = (val & 0xfffff000) |
bellard574bbf72005-01-03 23:27:31 +0000319 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
320 /* if disabled, cannot be enabled again */
321 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
322 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300323 cpu_clear_apic_feature(s->cpu_env);
bellard574bbf72005-01-03 23:27:31 +0000324 s->spurious_vec &= ~APIC_SV_ENABLE;
325 }
326}
327
Blue Swirl4a942ce2010-06-19 10:42:31 +0300328uint64_t cpu_get_apic_base(APICState *s)
bellard574bbf72005-01-03 23:27:31 +0000329{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000330 DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
331 s ? (uint64_t)s->apicbase: 0);
aurel322c7c13d2009-04-08 22:56:26 +0000332 return s ? s->apicbase : 0;
bellard574bbf72005-01-03 23:27:31 +0000333}
334
Blue Swirl4a942ce2010-06-19 10:42:31 +0300335void cpu_set_apic_tpr(APICState *s, uint8_t val)
bellard9230e662005-01-23 20:46:56 +0000336{
aurel322c7c13d2009-04-08 22:56:26 +0000337 if (!s)
338 return;
bellard9230e662005-01-23 20:46:56 +0000339 s->tpr = (val & 0x0f) << 4;
bellardd592d302005-07-23 19:05:37 +0000340 apic_update_irq(s);
bellard9230e662005-01-23 20:46:56 +0000341}
342
Blue Swirl4a942ce2010-06-19 10:42:31 +0300343uint8_t cpu_get_apic_tpr(APICState *s)
bellard9230e662005-01-23 20:46:56 +0000344{
aurel322c7c13d2009-04-08 22:56:26 +0000345 return s ? s->tpr >> 4 : 0;
bellard9230e662005-01-23 20:46:56 +0000346}
347
bellardd592d302005-07-23 19:05:37 +0000348/* return -1 if no bit is set */
349static int get_highest_priority_int(uint32_t *tab)
350{
351 int i;
352 for(i = 7; i >= 0; i--) {
353 if (tab[i] != 0) {
aurel323b63c042008-12-06 10:46:35 +0000354 return i * 32 + fls_bit(tab[i]);
bellardd592d302005-07-23 19:05:37 +0000355 }
356 }
357 return -1;
358}
359
bellard574bbf72005-01-03 23:27:31 +0000360static int apic_get_ppr(APICState *s)
361{
362 int tpr, isrv, ppr;
363
364 tpr = (s->tpr >> 4);
365 isrv = get_highest_priority_int(s->isr);
366 if (isrv < 0)
367 isrv = 0;
368 isrv >>= 4;
369 if (tpr >= isrv)
370 ppr = s->tpr;
371 else
372 ppr = isrv << 4;
373 return ppr;
374}
375
bellardd592d302005-07-23 19:05:37 +0000376static int apic_get_arb_pri(APICState *s)
377{
378 /* XXX: arbitration */
379 return 0;
380}
381
bellard574bbf72005-01-03 23:27:31 +0000382/* signal the CPU if an irq is pending */
383static void apic_update_irq(APICState *s)
384{
bellardd592d302005-07-23 19:05:37 +0000385 int irrv, ppr;
386 if (!(s->spurious_vec & APIC_SV_ENABLE))
387 return;
bellard574bbf72005-01-03 23:27:31 +0000388 irrv = get_highest_priority_int(s->irr);
389 if (irrv < 0)
390 return;
bellardd592d302005-07-23 19:05:37 +0000391 ppr = apic_get_ppr(s);
392 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
bellard574bbf72005-01-03 23:27:31 +0000393 return;
394 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
395}
396
aliguori73822ec2009-01-15 20:11:34 +0000397void apic_reset_irq_delivered(void)
398{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000399 DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000400 apic_irq_delivered = 0;
401}
402
403int apic_get_irq_delivered(void)
404{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000405 DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000406 return apic_irq_delivered;
407}
408
bellard574bbf72005-01-03 23:27:31 +0000409static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
410{
aliguori73822ec2009-01-15 20:11:34 +0000411 apic_irq_delivered += !get_bit(s->irr, vector_num);
Blue Swirl0a3c5922010-05-29 20:23:48 +0000412 DPRINTF_C("%s: coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000413
bellard574bbf72005-01-03 23:27:31 +0000414 set_bit(s->irr, vector_num);
415 if (trigger_mode)
416 set_bit(s->tmr, vector_num);
417 else
418 reset_bit(s->tmr, vector_num);
419 apic_update_irq(s);
420}
421
422static void apic_eoi(APICState *s)
423{
424 int isrv;
425 isrv = get_highest_priority_int(s->isr);
426 if (isrv < 0)
427 return;
428 reset_bit(s->isr, isrv);
bellardd592d302005-07-23 19:05:37 +0000429 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
430 set the remote IRR bit for level triggered interrupts. */
bellard574bbf72005-01-03 23:27:31 +0000431 apic_update_irq(s);
432}
433
Gleb Natapov678e12c2009-06-10 15:40:48 +0300434static int apic_find_dest(uint8_t dest)
435{
436 APICState *apic = local_apics[dest];
437 int i;
438
439 if (apic && apic->id == dest)
440 return dest; /* shortcut in case apic->id == apic->idx */
441
442 for (i = 0; i < MAX_APICS; i++) {
443 apic = local_apics[i];
444 if (apic && apic->id == dest)
445 return i;
446 }
447
448 return -1;
449}
450
bellardd3e9db92005-12-17 01:27:28 +0000451static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
452 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000453{
bellardd592d302005-07-23 19:05:37 +0000454 APICState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000455 int i;
bellardd592d302005-07-23 19:05:37 +0000456
457 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000458 if (dest == 0xff) {
459 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
460 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300461 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000462 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300463 if (idx >= 0)
464 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000465 }
bellardd592d302005-07-23 19:05:37 +0000466 } else {
467 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000468 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
469 for(i = 0; i < MAX_APICS; i++) {
470 apic_iter = local_apics[i];
471 if (apic_iter) {
472 if (apic_iter->dest_mode == 0xf) {
473 if (dest & apic_iter->log_dest)
474 set_bit(deliver_bitmask, i);
475 } else if (apic_iter->dest_mode == 0x0) {
476 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
477 (dest & apic_iter->log_dest & 0x0f)) {
478 set_bit(deliver_bitmask, i);
479 }
480 }
481 }
bellardd592d302005-07-23 19:05:37 +0000482 }
483 }
bellardd592d302005-07-23 19:05:37 +0000484}
485
486
Blue Swirl4a942ce2010-06-19 10:42:31 +0300487void apic_init_reset(APICState *s)
bellardd592d302005-07-23 19:05:37 +0000488{
bellardd592d302005-07-23 19:05:37 +0000489 int i;
490
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300491 if (!s)
492 return;
493
bellardd592d302005-07-23 19:05:37 +0000494 s->tpr = 0;
495 s->spurious_vec = 0xff;
496 s->log_dest = 0;
bellarde0fd8782005-11-21 23:26:26 +0000497 s->dest_mode = 0xf;
bellardd592d302005-07-23 19:05:37 +0000498 memset(s->isr, 0, sizeof(s->isr));
499 memset(s->tmr, 0, sizeof(s->tmr));
500 memset(s->irr, 0, sizeof(s->irr));
bellardb4511722006-10-08 18:20:51 +0000501 for(i = 0; i < APIC_LVT_NB; i++)
502 s->lvt[i] = 1 << 16; /* mask LVT */
bellardd592d302005-07-23 19:05:37 +0000503 s->esr = 0;
504 memset(s->icr, 0, sizeof(s->icr));
505 s->divide_conf = 0;
506 s->count_shift = 0;
507 s->initial_count = 0;
508 s->initial_count_load_time = 0;
509 s->next_time = 0;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300510 s->wait_for_sipi = 1;
bellardd592d302005-07-23 19:05:37 +0000511}
512
bellarde0fd8782005-11-21 23:26:26 +0000513static void apic_startup(APICState *s, int vector_num)
514{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300515 s->sipi_vector = vector_num;
516 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
517}
518
Blue Swirl4a942ce2010-06-19 10:42:31 +0300519void apic_sipi(APICState *s)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300520{
Blue Swirl4a942ce2010-06-19 10:42:31 +0300521 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300522
523 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000524 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300525 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300526 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000527}
528
bellardd592d302005-07-23 19:05:37 +0000529static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
530 uint8_t delivery_mode, uint8_t vector_num,
531 uint8_t polarity, uint8_t trigger_mode)
532{
bellardd3e9db92005-12-17 01:27:28 +0000533 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000534 int dest_shorthand = (s->icr[0] >> 18) & 3;
535 APICState *apic_iter;
536
bellarde0fd8782005-11-21 23:26:26 +0000537 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000538 case 0:
539 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
540 break;
541 case 1:
542 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300543 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000544 break;
545 case 2:
546 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
547 break;
548 case 3:
549 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300550 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000551 break;
bellarde0fd8782005-11-21 23:26:26 +0000552 }
553
bellardd592d302005-07-23 19:05:37 +0000554 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000555 case APIC_DM_INIT:
556 {
557 int trig_mode = (s->icr[0] >> 15) & 1;
558 int level = (s->icr[0] >> 14) & 1;
559 if (level == 0 && trig_mode == 1) {
ths5fafdf22007-09-16 21:08:06 +0000560 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000561 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000562 return;
563 }
564 }
565 break;
566
567 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000568 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000569 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000570 return;
571 }
572
bellardd592d302005-07-23 19:05:37 +0000573 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
574 trigger_mode);
575}
576
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300577int apic_get_interrupt(APICState *s)
bellard574bbf72005-01-03 23:27:31 +0000578{
bellard574bbf72005-01-03 23:27:31 +0000579 int intno;
580
581 /* if the APIC is installed or enabled, we let the 8259 handle the
582 IRQs */
583 if (!s)
584 return -1;
585 if (!(s->spurious_vec & APIC_SV_ENABLE))
586 return -1;
ths3b46e622007-09-17 08:09:54 +0000587
bellard574bbf72005-01-03 23:27:31 +0000588 /* XXX: spurious IRQ handling */
589 intno = get_highest_priority_int(s->irr);
590 if (intno < 0)
591 return -1;
bellardd592d302005-07-23 19:05:37 +0000592 if (s->tpr && intno <= s->tpr)
593 return s->spurious_vec & 0xff;
bellardb4511722006-10-08 18:20:51 +0000594 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000595 set_bit(s->isr, intno);
596 apic_update_irq(s);
597 return intno;
598}
599
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300600int apic_accept_pic_intr(APICState *s)
ths0e21e122007-10-09 03:08:56 +0000601{
ths0e21e122007-10-09 03:08:56 +0000602 uint32_t lvt0;
603
604 if (!s)
605 return -1;
606
607 lvt0 = s->lvt[APIC_LVT_LINT0];
608
aurel32a5b38b52008-04-13 16:08:30 +0000609 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
610 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000611 return 1;
612
613 return 0;
614}
615
bellard574bbf72005-01-03 23:27:31 +0000616static uint32_t apic_get_current_count(APICState *s)
617{
618 int64_t d;
619 uint32_t val;
ths5fafdf22007-09-16 21:08:06 +0000620 d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000621 s->count_shift;
622 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
623 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000624 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000625 } else {
626 if (d >= s->initial_count)
627 val = 0;
628 else
629 val = s->initial_count - d;
630 }
631 return val;
632}
633
634static void apic_timer_update(APICState *s, int64_t current_time)
635{
636 int64_t next_time, d;
ths3b46e622007-09-17 08:09:54 +0000637
bellard574bbf72005-01-03 23:27:31 +0000638 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
ths5fafdf22007-09-16 21:08:06 +0000639 d = (current_time - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000640 s->count_shift;
641 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
aliguori681f8c22008-08-18 14:19:42 +0000642 if (!s->initial_count)
643 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000644 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
bellard574bbf72005-01-03 23:27:31 +0000645 } else {
646 if (d >= s->initial_count)
647 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000648 d = (uint64_t)s->initial_count + 1;
bellard574bbf72005-01-03 23:27:31 +0000649 }
650 next_time = s->initial_count_load_time + (d << s->count_shift);
651 qemu_mod_timer(s->timer, next_time);
652 s->next_time = next_time;
653 } else {
654 no_timer:
655 qemu_del_timer(s->timer);
656 }
657}
658
659static void apic_timer(void *opaque)
660{
661 APICState *s = opaque;
662
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300663 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000664 apic_timer_update(s, s->next_time);
665}
666
Anthony Liguoric227f092009-10-01 16:12:16 -0500667static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000668{
669 return 0;
670}
671
Anthony Liguoric227f092009-10-01 16:12:16 -0500672static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000673{
674 return 0;
675}
676
Anthony Liguoric227f092009-10-01 16:12:16 -0500677static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000678{
679}
680
Anthony Liguoric227f092009-10-01 16:12:16 -0500681static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000682{
683}
684
Anthony Liguoric227f092009-10-01 16:12:16 -0500685static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000686{
bellard574bbf72005-01-03 23:27:31 +0000687 APICState *s;
688 uint32_t val;
689 int index;
690
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300691 s = cpu_get_current_apic();
692 if (!s) {
bellard574bbf72005-01-03 23:27:31 +0000693 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300694 }
bellard574bbf72005-01-03 23:27:31 +0000695
696 index = (addr >> 4) & 0xff;
697 switch(index) {
698 case 0x02: /* id */
699 val = s->id << 24;
700 break;
701 case 0x03: /* version */
702 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
703 break;
704 case 0x08:
705 val = s->tpr;
706 break;
bellardd592d302005-07-23 19:05:37 +0000707 case 0x09:
708 val = apic_get_arb_pri(s);
709 break;
bellard574bbf72005-01-03 23:27:31 +0000710 case 0x0a:
711 /* ppr */
712 val = apic_get_ppr(s);
713 break;
aurel32b237db32008-03-28 22:31:36 +0000714 case 0x0b:
715 val = 0;
716 break;
bellardd592d302005-07-23 19:05:37 +0000717 case 0x0d:
718 val = s->log_dest << 24;
719 break;
720 case 0x0e:
721 val = s->dest_mode << 28;
722 break;
bellard574bbf72005-01-03 23:27:31 +0000723 case 0x0f:
724 val = s->spurious_vec;
725 break;
726 case 0x10 ... 0x17:
727 val = s->isr[index & 7];
728 break;
729 case 0x18 ... 0x1f:
730 val = s->tmr[index & 7];
731 break;
732 case 0x20 ... 0x27:
733 val = s->irr[index & 7];
734 break;
735 case 0x28:
736 val = s->esr;
737 break;
bellard574bbf72005-01-03 23:27:31 +0000738 case 0x30:
739 case 0x31:
740 val = s->icr[index & 1];
741 break;
bellarde0fd8782005-11-21 23:26:26 +0000742 case 0x32 ... 0x37:
743 val = s->lvt[index - 0x32];
744 break;
bellard574bbf72005-01-03 23:27:31 +0000745 case 0x38:
746 val = s->initial_count;
747 break;
748 case 0x39:
749 val = apic_get_current_count(s);
750 break;
751 case 0x3e:
752 val = s->divide_conf;
753 break;
754 default:
755 s->esr |= ESR_ILLEGAL_ADDRESS;
756 val = 0;
757 break;
758 }
Blue Swirl0a3c5922010-05-29 20:23:48 +0000759 DPRINTF("read: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000760 return val;
761}
762
Anthony Liguoric227f092009-10-01 16:12:16 -0500763static void apic_send_msi(target_phys_addr_t addr, uint32 data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300764{
765 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
766 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
767 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
768 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
769 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
770 /* XXX: Ignore redirection hint. */
771 apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
772}
773
Anthony Liguoric227f092009-10-01 16:12:16 -0500774static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000775{
bellard574bbf72005-01-03 23:27:31 +0000776 APICState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300777 int index = (addr >> 4) & 0xff;
778 if (addr > 0xfff || !index) {
779 /* MSI and MMIO APIC are at the same memory location,
780 * but actually not on the global bus: MSI is on PCI bus
781 * APIC is connected directly to the CPU.
782 * Mapping them on the global bus happens to work because
783 * MSI registers are reserved in APIC MMIO and vice versa. */
784 apic_send_msi(addr, val);
785 return;
786 }
bellard574bbf72005-01-03 23:27:31 +0000787
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300788 s = cpu_get_current_apic();
789 if (!s) {
bellard574bbf72005-01-03 23:27:31 +0000790 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300791 }
bellard574bbf72005-01-03 23:27:31 +0000792
Blue Swirl0a3c5922010-05-29 20:23:48 +0000793 DPRINTF("write: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000794
bellard574bbf72005-01-03 23:27:31 +0000795 switch(index) {
796 case 0x02:
797 s->id = (val >> 24);
798 break;
bellarde0fd8782005-11-21 23:26:26 +0000799 case 0x03:
800 break;
bellard574bbf72005-01-03 23:27:31 +0000801 case 0x08:
802 s->tpr = val;
bellardd592d302005-07-23 19:05:37 +0000803 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000804 break;
bellarde0fd8782005-11-21 23:26:26 +0000805 case 0x09:
806 case 0x0a:
807 break;
bellard574bbf72005-01-03 23:27:31 +0000808 case 0x0b: /* EOI */
809 apic_eoi(s);
810 break;
bellardd592d302005-07-23 19:05:37 +0000811 case 0x0d:
812 s->log_dest = val >> 24;
813 break;
814 case 0x0e:
815 s->dest_mode = val >> 28;
816 break;
bellard574bbf72005-01-03 23:27:31 +0000817 case 0x0f:
818 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000819 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000820 break;
bellarde0fd8782005-11-21 23:26:26 +0000821 case 0x10 ... 0x17:
822 case 0x18 ... 0x1f:
823 case 0x20 ... 0x27:
824 case 0x28:
825 break;
bellard574bbf72005-01-03 23:27:31 +0000826 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000827 s->icr[0] = val;
828 apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
829 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
830 (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
831 break;
bellard574bbf72005-01-03 23:27:31 +0000832 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000833 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000834 break;
835 case 0x32 ... 0x37:
836 {
837 int n = index - 0x32;
838 s->lvt[n] = val;
839 if (n == APIC_LVT_TIMER)
840 apic_timer_update(s, qemu_get_clock(vm_clock));
841 }
842 break;
843 case 0x38:
844 s->initial_count = val;
845 s->initial_count_load_time = qemu_get_clock(vm_clock);
846 apic_timer_update(s, s->initial_count_load_time);
847 break;
bellarde0fd8782005-11-21 23:26:26 +0000848 case 0x39:
849 break;
bellard574bbf72005-01-03 23:27:31 +0000850 case 0x3e:
851 {
852 int v;
853 s->divide_conf = val & 0xb;
854 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
855 s->count_shift = (v + 1) & 7;
856 }
857 break;
858 default:
859 s->esr |= ESR_ILLEGAL_ADDRESS;
860 break;
861 }
862}
863
Juan Quintela695dcf72009-08-20 19:42:28 +0200864/* This function is only used for old state version 1 and 2 */
865static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
bellardd592d302005-07-23 19:05:37 +0000866{
867 APICState *s = opaque;
868 int i;
869
bellarde6cf6a82006-08-17 10:48:06 +0000870 if (version_id > 2)
bellardd592d302005-07-23 19:05:37 +0000871 return -EINVAL;
872
873 /* XXX: what if the base changes? (registered memory regions) */
874 qemu_get_be32s(f, &s->apicbase);
875 qemu_get_8s(f, &s->id);
876 qemu_get_8s(f, &s->arb_id);
877 qemu_get_8s(f, &s->tpr);
878 qemu_get_be32s(f, &s->spurious_vec);
879 qemu_get_8s(f, &s->log_dest);
880 qemu_get_8s(f, &s->dest_mode);
881 for (i = 0; i < 8; i++) {
882 qemu_get_be32s(f, &s->isr[i]);
883 qemu_get_be32s(f, &s->tmr[i]);
884 qemu_get_be32s(f, &s->irr[i]);
885 }
886 for (i = 0; i < APIC_LVT_NB; i++) {
887 qemu_get_be32s(f, &s->lvt[i]);
888 }
889 qemu_get_be32s(f, &s->esr);
890 qemu_get_be32s(f, &s->icr[0]);
891 qemu_get_be32s(f, &s->icr[1]);
892 qemu_get_be32s(f, &s->divide_conf);
thsbee8d682007-12-16 23:41:11 +0000893 s->count_shift=qemu_get_be32(f);
bellardd592d302005-07-23 19:05:37 +0000894 qemu_get_be32s(f, &s->initial_count);
thsbee8d682007-12-16 23:41:11 +0000895 s->initial_count_load_time=qemu_get_be64(f);
896 s->next_time=qemu_get_be64(f);
bellarde6cf6a82006-08-17 10:48:06 +0000897
898 if (version_id >= 2)
899 qemu_get_timer(f, s->timer);
bellardd592d302005-07-23 19:05:37 +0000900 return 0;
901}
902
Juan Quintela695dcf72009-08-20 19:42:28 +0200903static const VMStateDescription vmstate_apic = {
904 .name = "apic",
905 .version_id = 3,
906 .minimum_version_id = 3,
907 .minimum_version_id_old = 1,
908 .load_state_old = apic_load_old,
909 .fields = (VMStateField []) {
910 VMSTATE_UINT32(apicbase, APICState),
911 VMSTATE_UINT8(id, APICState),
912 VMSTATE_UINT8(arb_id, APICState),
913 VMSTATE_UINT8(tpr, APICState),
914 VMSTATE_UINT32(spurious_vec, APICState),
915 VMSTATE_UINT8(log_dest, APICState),
916 VMSTATE_UINT8(dest_mode, APICState),
917 VMSTATE_UINT32_ARRAY(isr, APICState, 8),
918 VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
919 VMSTATE_UINT32_ARRAY(irr, APICState, 8),
920 VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB),
921 VMSTATE_UINT32(esr, APICState),
922 VMSTATE_UINT32_ARRAY(icr, APICState, 2),
923 VMSTATE_UINT32(divide_conf, APICState),
924 VMSTATE_INT32(count_shift, APICState),
925 VMSTATE_UINT32(initial_count, APICState),
926 VMSTATE_INT64(initial_count_load_time, APICState),
927 VMSTATE_INT64(next_time, APICState),
928 VMSTATE_TIMER(timer, APICState),
929 VMSTATE_END_OF_LIST()
930 }
931};
932
bellardd592d302005-07-23 19:05:37 +0000933static void apic_reset(void *opaque)
934{
935 APICState *s = opaque;
Avi Kivity4c0960c2009-08-17 23:19:53 +0300936 int bsp;
aurel32fec5fa02008-09-02 00:09:08 +0000937
Avi Kivity4c0960c2009-08-17 23:19:53 +0300938 bsp = cpu_is_bsp(s->cpu_env);
aurel32fec5fa02008-09-02 00:09:08 +0000939 s->apicbase = 0xfee00000 |
Gleb Natapov678e12c2009-06-10 15:40:48 +0300940 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
aurel32fec5fa02008-09-02 00:09:08 +0000941
Blue Swirl4a942ce2010-06-19 10:42:31 +0300942 apic_init_reset(s);
ths0e21e122007-10-09 03:08:56 +0000943
Gleb Natapov678e12c2009-06-10 15:40:48 +0300944 if (bsp) {
aurel32a5b38b52008-04-13 16:08:30 +0000945 /*
946 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
947 * time typically by BIOS, so PIC interrupt can be delivered to the
948 * processor when local APIC is enabled.
949 */
950 s->lvt[APIC_LVT_LINT0] = 0x700;
951 }
bellardd592d302005-07-23 19:05:37 +0000952}
bellard574bbf72005-01-03 23:27:31 +0000953
Blue Swirld60efc62009-08-25 18:29:31 +0000954static CPUReadMemoryFunc * const apic_mem_read[3] = {
bellard574bbf72005-01-03 23:27:31 +0000955 apic_mem_readb,
956 apic_mem_readw,
957 apic_mem_readl,
958};
959
Blue Swirld60efc62009-08-25 18:29:31 +0000960static CPUWriteMemoryFunc * const apic_mem_write[3] = {
bellard574bbf72005-01-03 23:27:31 +0000961 apic_mem_writeb,
962 apic_mem_writew,
963 apic_mem_writel,
964};
965
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300966APICState *apic_init(CPUState *env, uint32_t apic_id)
bellard574bbf72005-01-03 23:27:31 +0000967{
968 APICState *s;
bellard574bbf72005-01-03 23:27:31 +0000969
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300970 if (last_apic_idx >= MAX_APICS) {
971 return NULL;
972 }
bellardd592d302005-07-23 19:05:37 +0000973 s = qemu_mallocz(sizeof(APICState));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300974 s->idx = last_apic_idx++;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300975 s->id = apic_id;
bellard574bbf72005-01-03 23:27:31 +0000976 s->cpu_env = env;
bellard574bbf72005-01-03 23:27:31 +0000977
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300978 msix_supported = 1;
ths0e21e122007-10-09 03:08:56 +0000979
bellardd592d302005-07-23 19:05:37 +0000980 /* XXX: mapping more APICs at the same memory location */
bellard574bbf72005-01-03 23:27:31 +0000981 if (apic_io_memory == 0) {
982 /* NOTE: the APIC is directly connected to the CPU - it is not
983 on the global memory bus. */
Avi Kivity1eed09c2009-06-14 11:38:51 +0300984 apic_io_memory = cpu_register_io_memory(apic_mem_read,
bellard574bbf72005-01-03 23:27:31 +0000985 apic_mem_write, NULL);
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300986 /* XXX: what if the base changes? */
987 cpu_register_physical_memory(MSI_ADDR_BASE, MSI_ADDR_SIZE,
bellardd592d302005-07-23 19:05:37 +0000988 apic_io_memory);
bellard574bbf72005-01-03 23:27:31 +0000989 }
990 s->timer = qemu_new_timer(vm_clock, apic_timer, s);
bellardd592d302005-07-23 19:05:37 +0000991
Juan Quintela695dcf72009-08-20 19:42:28 +0200992 vmstate_register(s->idx, &vmstate_apic, s);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200993 qemu_register_reset(apic_reset, s);
ths3b46e622007-09-17 08:09:54 +0000994
Gleb Natapov678e12c2009-06-10 15:40:48 +0300995 local_apics[s->idx] = s;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300996 return s;
bellard574bbf72005-01-03 23:27:31 +0000997}