blob: f18fdc63a236196e91272d2b7fa4939fd8600741 [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"
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030021#include "msix.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"
bellard574bbf72005-01-03 23:27:31 +000025
26//#define DEBUG_APIC
Blue Swirl0a3c5922010-05-29 20:23:48 +000027//#define DEBUG_COALESCING
28
29#ifdef DEBUG_APIC
30#define DPRINTF(fmt, ...) \
31 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
32#else
33#define DPRINTF(fmt, ...)
34#endif
35
36#ifdef DEBUG_COALESCING
37#define DPRINTF_C(fmt, ...) \
38 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
39#else
40#define DPRINTF_C(fmt, ...)
41#endif
bellard574bbf72005-01-03 23:27:31 +000042
43/* APIC Local Vector Table */
44#define APIC_LVT_TIMER 0
45#define APIC_LVT_THERMAL 1
46#define APIC_LVT_PERFORM 2
47#define APIC_LVT_LINT0 3
48#define APIC_LVT_LINT1 4
49#define APIC_LVT_ERROR 5
50#define APIC_LVT_NB 6
51
52/* APIC delivery modes */
53#define APIC_DM_FIXED 0
54#define APIC_DM_LOWPRI 1
55#define APIC_DM_SMI 2
56#define APIC_DM_NMI 4
57#define APIC_DM_INIT 5
58#define APIC_DM_SIPI 6
59#define APIC_DM_EXTINT 7
60
bellardd592d302005-07-23 19:05:37 +000061/* APIC destination mode */
62#define APIC_DESTMODE_FLAT 0xf
63#define APIC_DESTMODE_CLUSTER 1
64
bellard574bbf72005-01-03 23:27:31 +000065#define APIC_TRIGGER_EDGE 0
66#define APIC_TRIGGER_LEVEL 1
67
68#define APIC_LVT_TIMER_PERIODIC (1<<17)
69#define APIC_LVT_MASKED (1<<16)
70#define APIC_LVT_LEVEL_TRIGGER (1<<15)
71#define APIC_LVT_REMOTE_IRR (1<<14)
72#define APIC_INPUT_POLARITY (1<<13)
73#define APIC_SEND_PENDING (1<<12)
74
75#define ESR_ILLEGAL_ADDRESS (1 << 7)
76
77#define APIC_SV_ENABLE (1 << 8)
78
bellardd3e9db92005-12-17 01:27:28 +000079#define MAX_APICS 255
80#define MAX_APIC_WORDS 8
81
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030082/* Intel APIC constants: from include/asm/msidef.h */
83#define MSI_DATA_VECTOR_SHIFT 0
84#define MSI_DATA_VECTOR_MASK 0x000000ff
85#define MSI_DATA_DELIVERY_MODE_SHIFT 8
86#define MSI_DATA_TRIGGER_SHIFT 15
87#define MSI_DATA_LEVEL_SHIFT 14
88#define MSI_ADDR_DEST_MODE_SHIFT 2
89#define MSI_ADDR_DEST_ID_SHIFT 12
90#define MSI_ADDR_DEST_ID_MASK 0x00ffff0
91
92#define MSI_ADDR_BASE 0xfee00000
93#define MSI_ADDR_SIZE 0x100000
94
Blue Swirlcf6d64b2010-06-19 10:42:08 +030095struct APICState {
Blue Swirl8546b092010-06-19 07:44:07 +000096 SysBusDevice busdev;
97 void *cpu_env;
bellard574bbf72005-01-03 23:27:31 +000098 uint32_t apicbase;
99 uint8_t id;
bellardd592d302005-07-23 19:05:37 +0000100 uint8_t arb_id;
bellard574bbf72005-01-03 23:27:31 +0000101 uint8_t tpr;
102 uint32_t spurious_vec;
bellardd592d302005-07-23 19:05:37 +0000103 uint8_t log_dest;
104 uint8_t dest_mode;
bellard574bbf72005-01-03 23:27:31 +0000105 uint32_t isr[8]; /* in service register */
106 uint32_t tmr[8]; /* trigger mode register */
107 uint32_t irr[8]; /* interrupt request register */
108 uint32_t lvt[APIC_LVT_NB];
109 uint32_t esr; /* error register */
110 uint32_t icr[2];
111
112 uint32_t divide_conf;
113 int count_shift;
114 uint32_t initial_count;
115 int64_t initial_count_load_time, next_time;
Gleb Natapov678e12c2009-06-10 15:40:48 +0300116 uint32_t idx;
bellard574bbf72005-01-03 23:27:31 +0000117 QEMUTimer *timer;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300118 int sipi_vector;
119 int wait_for_sipi;
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300120};
bellard574bbf72005-01-03 23:27:31 +0000121
bellardd3e9db92005-12-17 01:27:28 +0000122static APICState *local_apics[MAX_APICS + 1];
aliguori73822ec2009-01-15 20:11:34 +0000123static int apic_irq_delivered;
124
bellardd592d302005-07-23 19:05:37 +0000125static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
126static void apic_update_irq(APICState *s);
aliguori610626a2009-03-12 20:25:12 +0000127static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
128 uint8_t dest, uint8_t dest_mode);
bellardd592d302005-07-23 19:05:37 +0000129
aurel323b63c042008-12-06 10:46:35 +0000130/* Find first bit starting from msb */
131static int fls_bit(uint32_t value)
132{
133 return 31 - clz32(value);
134}
135
aurel32e95f5492008-10-12 00:53:17 +0000136/* Find first bit starting from lsb */
bellardd3e9db92005-12-17 01:27:28 +0000137static int ffs_bit(uint32_t value)
138{
aurel32bb7e7292008-10-12 20:16:03 +0000139 return ctz32(value);
bellardd3e9db92005-12-17 01:27:28 +0000140}
141
142static inline void set_bit(uint32_t *tab, int index)
143{
144 int i, mask;
145 i = index >> 5;
146 mask = 1 << (index & 0x1f);
147 tab[i] |= mask;
148}
149
150static inline void reset_bit(uint32_t *tab, int index)
151{
152 int i, mask;
153 i = index >> 5;
154 mask = 1 << (index & 0x1f);
155 tab[i] &= ~mask;
156}
157
aliguori73822ec2009-01-15 20:11:34 +0000158static inline int get_bit(uint32_t *tab, int index)
159{
160 int i, mask;
161 i = index >> 5;
162 mask = 1 << (index & 0x1f);
163 return !!(tab[i] & mask);
164}
165
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300166static void apic_local_deliver(APICState *s, int vector)
aurel32a5b38b52008-04-13 16:08:30 +0000167{
aurel32a5b38b52008-04-13 16:08:30 +0000168 uint32_t lvt = s->lvt[vector];
169 int trigger_mode;
170
Blue Swirl0a3c5922010-05-29 20:23:48 +0000171 DPRINTF("%s: vector %d delivery mode %d\n", __func__, vector,
172 (lvt >> 8) & 7);
aurel32a5b38b52008-04-13 16:08:30 +0000173 if (lvt & APIC_LVT_MASKED)
174 return;
175
176 switch ((lvt >> 8) & 7) {
177 case APIC_DM_SMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300178 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
aurel32a5b38b52008-04-13 16:08:30 +0000179 break;
180
181 case APIC_DM_NMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300182 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
aurel32a5b38b52008-04-13 16:08:30 +0000183 break;
184
185 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300186 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel32a5b38b52008-04-13 16:08:30 +0000187 break;
188
189 case APIC_DM_FIXED:
190 trigger_mode = APIC_TRIGGER_EDGE;
191 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
192 (lvt & APIC_LVT_LEVEL_TRIGGER))
193 trigger_mode = APIC_TRIGGER_LEVEL;
194 apic_set_irq(s, lvt & 0xff, trigger_mode);
195 }
196}
197
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300198void apic_deliver_pic_intr(APICState *s, int level)
aurel321a7de942008-08-21 03:14:52 +0000199{
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300200 if (level) {
201 apic_local_deliver(s, APIC_LVT_LINT0);
202 } else {
aurel321a7de942008-08-21 03:14:52 +0000203 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
204
205 switch ((lvt >> 8) & 7) {
206 case APIC_DM_FIXED:
207 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
208 break;
209 reset_bit(s->irr, lvt & 0xff);
210 /* fall through */
211 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300212 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000213 break;
214 }
215 }
216}
217
bellardd3e9db92005-12-17 01:27:28 +0000218#define foreach_apic(apic, deliver_bitmask, code) \
219{\
220 int __i, __j, __mask;\
221 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
222 __mask = deliver_bitmask[__i];\
223 if (__mask) {\
224 for(__j = 0; __j < 32; __j++) {\
225 if (__mask & (1 << __j)) {\
226 apic = local_apics[__i * 32 + __j];\
227 if (apic) {\
228 code;\
229 }\
230 }\
231 }\
232 }\
233 }\
234}
235
ths5fafdf22007-09-16 21:08:06 +0000236static void apic_bus_deliver(const uint32_t *deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000237 uint8_t delivery_mode,
bellardd592d302005-07-23 19:05:37 +0000238 uint8_t vector_num, uint8_t polarity,
239 uint8_t trigger_mode)
240{
241 APICState *apic_iter;
242
243 switch (delivery_mode) {
244 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000245 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000246 {
247 int i, d;
248 d = -1;
249 for(i = 0; i < MAX_APIC_WORDS; i++) {
250 if (deliver_bitmask[i]) {
251 d = i * 32 + ffs_bit(deliver_bitmask[i]);
252 break;
253 }
254 }
255 if (d >= 0) {
256 apic_iter = local_apics[d];
257 if (apic_iter) {
258 apic_set_irq(apic_iter, vector_num, trigger_mode);
259 }
260 }
bellard8dd69b82005-11-23 20:59:44 +0000261 }
bellardd3e9db92005-12-17 01:27:28 +0000262 return;
bellard8dd69b82005-11-23 20:59:44 +0000263
bellardd592d302005-07-23 19:05:37 +0000264 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000265 break;
266
267 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000268 foreach_apic(apic_iter, deliver_bitmask,
269 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
270 return;
271
bellardd592d302005-07-23 19:05:37 +0000272 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000273 foreach_apic(apic_iter, deliver_bitmask,
274 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
275 return;
bellardd592d302005-07-23 19:05:37 +0000276
277 case APIC_DM_INIT:
278 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000279 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300280 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000281 return;
ths3b46e622007-09-17 08:09:54 +0000282
bellardd592d302005-07-23 19:05:37 +0000283 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000284 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000285 break;
286
287 default:
288 return;
289 }
290
ths5fafdf22007-09-16 21:08:06 +0000291 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000292 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000293}
bellard574bbf72005-01-03 23:27:31 +0000294
aliguori610626a2009-03-12 20:25:12 +0000295void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
296 uint8_t delivery_mode, uint8_t vector_num,
297 uint8_t polarity, uint8_t trigger_mode)
298{
299 uint32_t deliver_bitmask[MAX_APIC_WORDS];
300
Blue Swirl0a3c5922010-05-29 20:23:48 +0000301 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
302 " polarity %d trigger_mode %d\n", __func__, dest, dest_mode,
303 delivery_mode, vector_num, polarity, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000304 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
305 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
306 trigger_mode);
307}
308
Blue Swirl4a942ce2010-06-19 10:42:31 +0300309void cpu_set_apic_base(APICState *s, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000310{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000311 DPRINTF("cpu_set_apic_base: %016" PRIx64 "\n", val);
aurel322c7c13d2009-04-08 22:56:26 +0000312 if (!s)
313 return;
ths5fafdf22007-09-16 21:08:06 +0000314 s->apicbase = (val & 0xfffff000) |
bellard574bbf72005-01-03 23:27:31 +0000315 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
316 /* if disabled, cannot be enabled again */
317 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
318 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300319 cpu_clear_apic_feature(s->cpu_env);
bellard574bbf72005-01-03 23:27:31 +0000320 s->spurious_vec &= ~APIC_SV_ENABLE;
321 }
322}
323
Blue Swirl4a942ce2010-06-19 10:42:31 +0300324uint64_t cpu_get_apic_base(APICState *s)
bellard574bbf72005-01-03 23:27:31 +0000325{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000326 DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
327 s ? (uint64_t)s->apicbase: 0);
aurel322c7c13d2009-04-08 22:56:26 +0000328 return s ? s->apicbase : 0;
bellard574bbf72005-01-03 23:27:31 +0000329}
330
Blue Swirl4a942ce2010-06-19 10:42:31 +0300331void cpu_set_apic_tpr(APICState *s, uint8_t val)
bellard9230e662005-01-23 20:46:56 +0000332{
aurel322c7c13d2009-04-08 22:56:26 +0000333 if (!s)
334 return;
bellard9230e662005-01-23 20:46:56 +0000335 s->tpr = (val & 0x0f) << 4;
bellardd592d302005-07-23 19:05:37 +0000336 apic_update_irq(s);
bellard9230e662005-01-23 20:46:56 +0000337}
338
Blue Swirl4a942ce2010-06-19 10:42:31 +0300339uint8_t cpu_get_apic_tpr(APICState *s)
bellard9230e662005-01-23 20:46:56 +0000340{
aurel322c7c13d2009-04-08 22:56:26 +0000341 return s ? s->tpr >> 4 : 0;
bellard9230e662005-01-23 20:46:56 +0000342}
343
bellardd592d302005-07-23 19:05:37 +0000344/* return -1 if no bit is set */
345static int get_highest_priority_int(uint32_t *tab)
346{
347 int i;
348 for(i = 7; i >= 0; i--) {
349 if (tab[i] != 0) {
aurel323b63c042008-12-06 10:46:35 +0000350 return i * 32 + fls_bit(tab[i]);
bellardd592d302005-07-23 19:05:37 +0000351 }
352 }
353 return -1;
354}
355
bellard574bbf72005-01-03 23:27:31 +0000356static int apic_get_ppr(APICState *s)
357{
358 int tpr, isrv, ppr;
359
360 tpr = (s->tpr >> 4);
361 isrv = get_highest_priority_int(s->isr);
362 if (isrv < 0)
363 isrv = 0;
364 isrv >>= 4;
365 if (tpr >= isrv)
366 ppr = s->tpr;
367 else
368 ppr = isrv << 4;
369 return ppr;
370}
371
bellardd592d302005-07-23 19:05:37 +0000372static int apic_get_arb_pri(APICState *s)
373{
374 /* XXX: arbitration */
375 return 0;
376}
377
bellard574bbf72005-01-03 23:27:31 +0000378/* signal the CPU if an irq is pending */
379static void apic_update_irq(APICState *s)
380{
bellardd592d302005-07-23 19:05:37 +0000381 int irrv, ppr;
382 if (!(s->spurious_vec & APIC_SV_ENABLE))
383 return;
bellard574bbf72005-01-03 23:27:31 +0000384 irrv = get_highest_priority_int(s->irr);
385 if (irrv < 0)
386 return;
bellardd592d302005-07-23 19:05:37 +0000387 ppr = apic_get_ppr(s);
388 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
bellard574bbf72005-01-03 23:27:31 +0000389 return;
390 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
391}
392
aliguori73822ec2009-01-15 20:11:34 +0000393void apic_reset_irq_delivered(void)
394{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000395 DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000396 apic_irq_delivered = 0;
397}
398
399int apic_get_irq_delivered(void)
400{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000401 DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000402 return apic_irq_delivered;
403}
404
bellard574bbf72005-01-03 23:27:31 +0000405static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
406{
aliguori73822ec2009-01-15 20:11:34 +0000407 apic_irq_delivered += !get_bit(s->irr, vector_num);
Blue Swirl0a3c5922010-05-29 20:23:48 +0000408 DPRINTF_C("%s: coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000409
bellard574bbf72005-01-03 23:27:31 +0000410 set_bit(s->irr, vector_num);
411 if (trigger_mode)
412 set_bit(s->tmr, vector_num);
413 else
414 reset_bit(s->tmr, vector_num);
415 apic_update_irq(s);
416}
417
418static void apic_eoi(APICState *s)
419{
420 int isrv;
421 isrv = get_highest_priority_int(s->isr);
422 if (isrv < 0)
423 return;
424 reset_bit(s->isr, isrv);
bellardd592d302005-07-23 19:05:37 +0000425 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
426 set the remote IRR bit for level triggered interrupts. */
bellard574bbf72005-01-03 23:27:31 +0000427 apic_update_irq(s);
428}
429
Gleb Natapov678e12c2009-06-10 15:40:48 +0300430static int apic_find_dest(uint8_t dest)
431{
432 APICState *apic = local_apics[dest];
433 int i;
434
435 if (apic && apic->id == dest)
436 return dest; /* shortcut in case apic->id == apic->idx */
437
438 for (i = 0; i < MAX_APICS; i++) {
439 apic = local_apics[i];
440 if (apic && apic->id == dest)
441 return i;
442 }
443
444 return -1;
445}
446
bellardd3e9db92005-12-17 01:27:28 +0000447static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
448 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000449{
bellardd592d302005-07-23 19:05:37 +0000450 APICState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000451 int i;
bellardd592d302005-07-23 19:05:37 +0000452
453 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000454 if (dest == 0xff) {
455 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
456 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300457 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000458 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300459 if (idx >= 0)
460 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000461 }
bellardd592d302005-07-23 19:05:37 +0000462 } else {
463 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000464 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
465 for(i = 0; i < MAX_APICS; i++) {
466 apic_iter = local_apics[i];
467 if (apic_iter) {
468 if (apic_iter->dest_mode == 0xf) {
469 if (dest & apic_iter->log_dest)
470 set_bit(deliver_bitmask, i);
471 } else if (apic_iter->dest_mode == 0x0) {
472 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
473 (dest & apic_iter->log_dest & 0x0f)) {
474 set_bit(deliver_bitmask, i);
475 }
476 }
477 }
bellardd592d302005-07-23 19:05:37 +0000478 }
479 }
bellardd592d302005-07-23 19:05:37 +0000480}
481
482
Blue Swirl4a942ce2010-06-19 10:42:31 +0300483void apic_init_reset(APICState *s)
bellardd592d302005-07-23 19:05:37 +0000484{
485 int i;
486
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300487 if (!s)
488 return;
489
bellardd592d302005-07-23 19:05:37 +0000490 s->tpr = 0;
491 s->spurious_vec = 0xff;
492 s->log_dest = 0;
bellarde0fd8782005-11-21 23:26:26 +0000493 s->dest_mode = 0xf;
bellardd592d302005-07-23 19:05:37 +0000494 memset(s->isr, 0, sizeof(s->isr));
495 memset(s->tmr, 0, sizeof(s->tmr));
496 memset(s->irr, 0, sizeof(s->irr));
bellardb4511722006-10-08 18:20:51 +0000497 for(i = 0; i < APIC_LVT_NB; i++)
498 s->lvt[i] = 1 << 16; /* mask LVT */
bellardd592d302005-07-23 19:05:37 +0000499 s->esr = 0;
500 memset(s->icr, 0, sizeof(s->icr));
501 s->divide_conf = 0;
502 s->count_shift = 0;
503 s->initial_count = 0;
504 s->initial_count_load_time = 0;
505 s->next_time = 0;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300506 s->wait_for_sipi = 1;
bellardd592d302005-07-23 19:05:37 +0000507}
508
bellarde0fd8782005-11-21 23:26:26 +0000509static void apic_startup(APICState *s, int vector_num)
510{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300511 s->sipi_vector = vector_num;
512 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
513}
514
Blue Swirl4a942ce2010-06-19 10:42:31 +0300515void apic_sipi(APICState *s)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300516{
Blue Swirl4a942ce2010-06-19 10:42:31 +0300517 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300518
519 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000520 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300521 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300522 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000523}
524
bellardd592d302005-07-23 19:05:37 +0000525static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
526 uint8_t delivery_mode, uint8_t vector_num,
527 uint8_t polarity, uint8_t trigger_mode)
528{
bellardd3e9db92005-12-17 01:27:28 +0000529 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000530 int dest_shorthand = (s->icr[0] >> 18) & 3;
531 APICState *apic_iter;
532
bellarde0fd8782005-11-21 23:26:26 +0000533 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000534 case 0:
535 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
536 break;
537 case 1:
538 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300539 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000540 break;
541 case 2:
542 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
543 break;
544 case 3:
545 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300546 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000547 break;
bellarde0fd8782005-11-21 23:26:26 +0000548 }
549
bellardd592d302005-07-23 19:05:37 +0000550 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000551 case APIC_DM_INIT:
552 {
553 int trig_mode = (s->icr[0] >> 15) & 1;
554 int level = (s->icr[0] >> 14) & 1;
555 if (level == 0 && trig_mode == 1) {
ths5fafdf22007-09-16 21:08:06 +0000556 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000557 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000558 return;
559 }
560 }
561 break;
562
563 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000564 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000565 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000566 return;
567 }
568
bellardd592d302005-07-23 19:05:37 +0000569 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
570 trigger_mode);
571}
572
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300573int apic_get_interrupt(APICState *s)
bellard574bbf72005-01-03 23:27:31 +0000574{
bellard574bbf72005-01-03 23:27:31 +0000575 int intno;
576
577 /* if the APIC is installed or enabled, we let the 8259 handle the
578 IRQs */
579 if (!s)
580 return -1;
581 if (!(s->spurious_vec & APIC_SV_ENABLE))
582 return -1;
ths3b46e622007-09-17 08:09:54 +0000583
bellard574bbf72005-01-03 23:27:31 +0000584 /* XXX: spurious IRQ handling */
585 intno = get_highest_priority_int(s->irr);
586 if (intno < 0)
587 return -1;
bellardd592d302005-07-23 19:05:37 +0000588 if (s->tpr && intno <= s->tpr)
589 return s->spurious_vec & 0xff;
bellardb4511722006-10-08 18:20:51 +0000590 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000591 set_bit(s->isr, intno);
592 apic_update_irq(s);
593 return intno;
594}
595
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300596int apic_accept_pic_intr(APICState *s)
ths0e21e122007-10-09 03:08:56 +0000597{
ths0e21e122007-10-09 03:08:56 +0000598 uint32_t lvt0;
599
600 if (!s)
601 return -1;
602
603 lvt0 = s->lvt[APIC_LVT_LINT0];
604
aurel32a5b38b52008-04-13 16:08:30 +0000605 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
606 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000607 return 1;
608
609 return 0;
610}
611
bellard574bbf72005-01-03 23:27:31 +0000612static uint32_t apic_get_current_count(APICState *s)
613{
614 int64_t d;
615 uint32_t val;
ths5fafdf22007-09-16 21:08:06 +0000616 d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000617 s->count_shift;
618 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
619 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000620 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000621 } else {
622 if (d >= s->initial_count)
623 val = 0;
624 else
625 val = s->initial_count - d;
626 }
627 return val;
628}
629
630static void apic_timer_update(APICState *s, int64_t current_time)
631{
632 int64_t next_time, d;
ths3b46e622007-09-17 08:09:54 +0000633
bellard574bbf72005-01-03 23:27:31 +0000634 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
ths5fafdf22007-09-16 21:08:06 +0000635 d = (current_time - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000636 s->count_shift;
637 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
aliguori681f8c22008-08-18 14:19:42 +0000638 if (!s->initial_count)
639 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000640 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
bellard574bbf72005-01-03 23:27:31 +0000641 } else {
642 if (d >= s->initial_count)
643 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000644 d = (uint64_t)s->initial_count + 1;
bellard574bbf72005-01-03 23:27:31 +0000645 }
646 next_time = s->initial_count_load_time + (d << s->count_shift);
647 qemu_mod_timer(s->timer, next_time);
648 s->next_time = next_time;
649 } else {
650 no_timer:
651 qemu_del_timer(s->timer);
652 }
653}
654
655static void apic_timer(void *opaque)
656{
657 APICState *s = opaque;
658
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300659 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000660 apic_timer_update(s, s->next_time);
661}
662
Anthony Liguoric227f092009-10-01 16:12:16 -0500663static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000664{
665 return 0;
666}
667
Anthony Liguoric227f092009-10-01 16:12:16 -0500668static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000669{
670 return 0;
671}
672
Anthony Liguoric227f092009-10-01 16:12:16 -0500673static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000674{
675}
676
Anthony Liguoric227f092009-10-01 16:12:16 -0500677static void apic_mem_writew(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 uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000682{
bellard574bbf72005-01-03 23:27:31 +0000683 APICState *s;
684 uint32_t val;
685 int index;
686
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300687 s = cpu_get_current_apic();
688 if (!s) {
bellard574bbf72005-01-03 23:27:31 +0000689 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300690 }
bellard574bbf72005-01-03 23:27:31 +0000691
692 index = (addr >> 4) & 0xff;
693 switch(index) {
694 case 0x02: /* id */
695 val = s->id << 24;
696 break;
697 case 0x03: /* version */
698 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
699 break;
700 case 0x08:
701 val = s->tpr;
702 break;
bellardd592d302005-07-23 19:05:37 +0000703 case 0x09:
704 val = apic_get_arb_pri(s);
705 break;
bellard574bbf72005-01-03 23:27:31 +0000706 case 0x0a:
707 /* ppr */
708 val = apic_get_ppr(s);
709 break;
aurel32b237db32008-03-28 22:31:36 +0000710 case 0x0b:
711 val = 0;
712 break;
bellardd592d302005-07-23 19:05:37 +0000713 case 0x0d:
714 val = s->log_dest << 24;
715 break;
716 case 0x0e:
717 val = s->dest_mode << 28;
718 break;
bellard574bbf72005-01-03 23:27:31 +0000719 case 0x0f:
720 val = s->spurious_vec;
721 break;
722 case 0x10 ... 0x17:
723 val = s->isr[index & 7];
724 break;
725 case 0x18 ... 0x1f:
726 val = s->tmr[index & 7];
727 break;
728 case 0x20 ... 0x27:
729 val = s->irr[index & 7];
730 break;
731 case 0x28:
732 val = s->esr;
733 break;
bellard574bbf72005-01-03 23:27:31 +0000734 case 0x30:
735 case 0x31:
736 val = s->icr[index & 1];
737 break;
bellarde0fd8782005-11-21 23:26:26 +0000738 case 0x32 ... 0x37:
739 val = s->lvt[index - 0x32];
740 break;
bellard574bbf72005-01-03 23:27:31 +0000741 case 0x38:
742 val = s->initial_count;
743 break;
744 case 0x39:
745 val = apic_get_current_count(s);
746 break;
747 case 0x3e:
748 val = s->divide_conf;
749 break;
750 default:
751 s->esr |= ESR_ILLEGAL_ADDRESS;
752 val = 0;
753 break;
754 }
Blue Swirl0a3c5922010-05-29 20:23:48 +0000755 DPRINTF("read: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000756 return val;
757}
758
Anthony Liguoric227f092009-10-01 16:12:16 -0500759static void apic_send_msi(target_phys_addr_t addr, uint32 data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300760{
761 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
762 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
763 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
764 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
765 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
766 /* XXX: Ignore redirection hint. */
767 apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
768}
769
Anthony Liguoric227f092009-10-01 16:12:16 -0500770static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000771{
bellard574bbf72005-01-03 23:27:31 +0000772 APICState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300773 int index = (addr >> 4) & 0xff;
774 if (addr > 0xfff || !index) {
775 /* MSI and MMIO APIC are at the same memory location,
776 * but actually not on the global bus: MSI is on PCI bus
777 * APIC is connected directly to the CPU.
778 * Mapping them on the global bus happens to work because
779 * MSI registers are reserved in APIC MMIO and vice versa. */
780 apic_send_msi(addr, val);
781 return;
782 }
bellard574bbf72005-01-03 23:27:31 +0000783
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300784 s = cpu_get_current_apic();
785 if (!s) {
bellard574bbf72005-01-03 23:27:31 +0000786 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300787 }
bellard574bbf72005-01-03 23:27:31 +0000788
Blue Swirl0a3c5922010-05-29 20:23:48 +0000789 DPRINTF("write: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000790
bellard574bbf72005-01-03 23:27:31 +0000791 switch(index) {
792 case 0x02:
793 s->id = (val >> 24);
794 break;
bellarde0fd8782005-11-21 23:26:26 +0000795 case 0x03:
796 break;
bellard574bbf72005-01-03 23:27:31 +0000797 case 0x08:
798 s->tpr = val;
bellardd592d302005-07-23 19:05:37 +0000799 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000800 break;
bellarde0fd8782005-11-21 23:26:26 +0000801 case 0x09:
802 case 0x0a:
803 break;
bellard574bbf72005-01-03 23:27:31 +0000804 case 0x0b: /* EOI */
805 apic_eoi(s);
806 break;
bellardd592d302005-07-23 19:05:37 +0000807 case 0x0d:
808 s->log_dest = val >> 24;
809 break;
810 case 0x0e:
811 s->dest_mode = val >> 28;
812 break;
bellard574bbf72005-01-03 23:27:31 +0000813 case 0x0f:
814 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000815 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000816 break;
bellarde0fd8782005-11-21 23:26:26 +0000817 case 0x10 ... 0x17:
818 case 0x18 ... 0x1f:
819 case 0x20 ... 0x27:
820 case 0x28:
821 break;
bellard574bbf72005-01-03 23:27:31 +0000822 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000823 s->icr[0] = val;
824 apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
825 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
826 (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
827 break;
bellard574bbf72005-01-03 23:27:31 +0000828 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000829 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000830 break;
831 case 0x32 ... 0x37:
832 {
833 int n = index - 0x32;
834 s->lvt[n] = val;
835 if (n == APIC_LVT_TIMER)
836 apic_timer_update(s, qemu_get_clock(vm_clock));
837 }
838 break;
839 case 0x38:
840 s->initial_count = val;
841 s->initial_count_load_time = qemu_get_clock(vm_clock);
842 apic_timer_update(s, s->initial_count_load_time);
843 break;
bellarde0fd8782005-11-21 23:26:26 +0000844 case 0x39:
845 break;
bellard574bbf72005-01-03 23:27:31 +0000846 case 0x3e:
847 {
848 int v;
849 s->divide_conf = val & 0xb;
850 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
851 s->count_shift = (v + 1) & 7;
852 }
853 break;
854 default:
855 s->esr |= ESR_ILLEGAL_ADDRESS;
856 break;
857 }
858}
859
Juan Quintela695dcf72009-08-20 19:42:28 +0200860/* This function is only used for old state version 1 and 2 */
861static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
bellardd592d302005-07-23 19:05:37 +0000862{
863 APICState *s = opaque;
864 int i;
865
bellarde6cf6a82006-08-17 10:48:06 +0000866 if (version_id > 2)
bellardd592d302005-07-23 19:05:37 +0000867 return -EINVAL;
868
869 /* XXX: what if the base changes? (registered memory regions) */
870 qemu_get_be32s(f, &s->apicbase);
871 qemu_get_8s(f, &s->id);
872 qemu_get_8s(f, &s->arb_id);
873 qemu_get_8s(f, &s->tpr);
874 qemu_get_be32s(f, &s->spurious_vec);
875 qemu_get_8s(f, &s->log_dest);
876 qemu_get_8s(f, &s->dest_mode);
877 for (i = 0; i < 8; i++) {
878 qemu_get_be32s(f, &s->isr[i]);
879 qemu_get_be32s(f, &s->tmr[i]);
880 qemu_get_be32s(f, &s->irr[i]);
881 }
882 for (i = 0; i < APIC_LVT_NB; i++) {
883 qemu_get_be32s(f, &s->lvt[i]);
884 }
885 qemu_get_be32s(f, &s->esr);
886 qemu_get_be32s(f, &s->icr[0]);
887 qemu_get_be32s(f, &s->icr[1]);
888 qemu_get_be32s(f, &s->divide_conf);
thsbee8d682007-12-16 23:41:11 +0000889 s->count_shift=qemu_get_be32(f);
bellardd592d302005-07-23 19:05:37 +0000890 qemu_get_be32s(f, &s->initial_count);
thsbee8d682007-12-16 23:41:11 +0000891 s->initial_count_load_time=qemu_get_be64(f);
892 s->next_time=qemu_get_be64(f);
bellarde6cf6a82006-08-17 10:48:06 +0000893
894 if (version_id >= 2)
895 qemu_get_timer(f, s->timer);
bellardd592d302005-07-23 19:05:37 +0000896 return 0;
897}
898
Juan Quintela695dcf72009-08-20 19:42:28 +0200899static const VMStateDescription vmstate_apic = {
900 .name = "apic",
901 .version_id = 3,
902 .minimum_version_id = 3,
903 .minimum_version_id_old = 1,
904 .load_state_old = apic_load_old,
905 .fields = (VMStateField []) {
906 VMSTATE_UINT32(apicbase, APICState),
907 VMSTATE_UINT8(id, APICState),
908 VMSTATE_UINT8(arb_id, APICState),
909 VMSTATE_UINT8(tpr, APICState),
910 VMSTATE_UINT32(spurious_vec, APICState),
911 VMSTATE_UINT8(log_dest, APICState),
912 VMSTATE_UINT8(dest_mode, APICState),
913 VMSTATE_UINT32_ARRAY(isr, APICState, 8),
914 VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
915 VMSTATE_UINT32_ARRAY(irr, APICState, 8),
916 VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB),
917 VMSTATE_UINT32(esr, APICState),
918 VMSTATE_UINT32_ARRAY(icr, APICState, 2),
919 VMSTATE_UINT32(divide_conf, APICState),
920 VMSTATE_INT32(count_shift, APICState),
921 VMSTATE_UINT32(initial_count, APICState),
922 VMSTATE_INT64(initial_count_load_time, APICState),
923 VMSTATE_INT64(next_time, APICState),
924 VMSTATE_TIMER(timer, APICState),
925 VMSTATE_END_OF_LIST()
926 }
927};
928
Blue Swirl8546b092010-06-19 07:44:07 +0000929static void apic_reset(DeviceState *d)
bellardd592d302005-07-23 19:05:37 +0000930{
Blue Swirl8546b092010-06-19 07:44:07 +0000931 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
Avi Kivity4c0960c2009-08-17 23:19:53 +0300932 int bsp;
aurel32fec5fa02008-09-02 00:09:08 +0000933
Avi Kivity4c0960c2009-08-17 23:19:53 +0300934 bsp = cpu_is_bsp(s->cpu_env);
aurel32fec5fa02008-09-02 00:09:08 +0000935 s->apicbase = 0xfee00000 |
Gleb Natapov678e12c2009-06-10 15:40:48 +0300936 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
aurel32fec5fa02008-09-02 00:09:08 +0000937
Blue Swirl4a942ce2010-06-19 10:42:31 +0300938 apic_init_reset(s);
ths0e21e122007-10-09 03:08:56 +0000939
Gleb Natapov678e12c2009-06-10 15:40:48 +0300940 if (bsp) {
aurel32a5b38b52008-04-13 16:08:30 +0000941 /*
942 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
943 * time typically by BIOS, so PIC interrupt can be delivered to the
944 * processor when local APIC is enabled.
945 */
946 s->lvt[APIC_LVT_LINT0] = 0x700;
947 }
bellardd592d302005-07-23 19:05:37 +0000948}
bellard574bbf72005-01-03 23:27:31 +0000949
Blue Swirld60efc62009-08-25 18:29:31 +0000950static CPUReadMemoryFunc * const apic_mem_read[3] = {
bellard574bbf72005-01-03 23:27:31 +0000951 apic_mem_readb,
952 apic_mem_readw,
953 apic_mem_readl,
954};
955
Blue Swirld60efc62009-08-25 18:29:31 +0000956static CPUWriteMemoryFunc * const apic_mem_write[3] = {
bellard574bbf72005-01-03 23:27:31 +0000957 apic_mem_writeb,
958 apic_mem_writew,
959 apic_mem_writel,
960};
961
Blue Swirl8546b092010-06-19 07:44:07 +0000962APICState *apic_init(void *env, uint8_t apic_id)
bellard574bbf72005-01-03 23:27:31 +0000963{
Blue Swirl8546b092010-06-19 07:44:07 +0000964 DeviceState *dev;
965 SysBusDevice *d;
bellard574bbf72005-01-03 23:27:31 +0000966 APICState *s;
Blue Swirl8546b092010-06-19 07:44:07 +0000967 static int apic_mapped;
bellard574bbf72005-01-03 23:27:31 +0000968
Blue Swirl8546b092010-06-19 07:44:07 +0000969 dev = qdev_create(NULL, "apic");
970 qdev_prop_set_uint8(dev, "id", apic_id);
971 qdev_prop_set_ptr(dev, "cpu_env", env);
972 qdev_init_nofail(dev);
973 d = sysbus_from_qdev(dev);
974
975 /* XXX: mapping more APICs at the same memory location */
976 if (apic_mapped == 0) {
977 /* NOTE: the APIC is directly connected to the CPU - it is not
978 on the global memory bus. */
979 /* XXX: what if the base changes? */
980 sysbus_mmio_map(d, 0, MSI_ADDR_BASE);
981 apic_mapped = 1;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300982 }
bellard574bbf72005-01-03 23:27:31 +0000983
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300984 msix_supported = 1;
ths0e21e122007-10-09 03:08:56 +0000985
Blue Swirl8546b092010-06-19 07:44:07 +0000986 s = DO_UPCAST(APICState, busdev.qdev, dev);
bellardd592d302005-07-23 19:05:37 +0000987
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300988 return s;
bellard574bbf72005-01-03 23:27:31 +0000989}
Blue Swirl8546b092010-06-19 07:44:07 +0000990
991static int apic_init1(SysBusDevice *dev)
992{
993 APICState *s = FROM_SYSBUS(APICState, dev);
994 int apic_io_memory;
995 static int last_apic_idx;
996
997 if (last_apic_idx >= MAX_APICS) {
998 return -1;
999 }
1000 apic_io_memory = cpu_register_io_memory(apic_mem_read,
1001 apic_mem_write, NULL);
1002 sysbus_init_mmio(dev, MSI_ADDR_SIZE, apic_io_memory);
1003
1004 s->timer = qemu_new_timer(vm_clock, apic_timer, s);
1005 s->idx = last_apic_idx++;
1006 local_apics[s->idx] = s;
1007 return 0;
1008}
1009
1010static SysBusDeviceInfo apic_info = {
1011 .init = apic_init1,
1012 .qdev.name = "apic",
1013 .qdev.size = sizeof(APICState),
1014 .qdev.vmsd = &vmstate_apic,
1015 .qdev.reset = apic_reset,
1016 .qdev.no_user = 1,
1017 .qdev.props = (Property[]) {
1018 DEFINE_PROP_UINT8("id", APICState, id, -1),
1019 DEFINE_PROP_PTR("cpu_env", APICState, cpu_env),
1020 DEFINE_PROP_END_OF_LIST(),
1021 }
1022};
1023
1024static void apic_register_devices(void)
1025{
1026 sysbus_register_withprop(&apic_info);
1027}
1028
1029device_init(apic_register_devices)