blob: d686b510b05bc55daa780ac4ac642ba253ee123c [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"
pbrook87ecb682007-11-17 17:14:51 +000021#include "qemu-timer.h"
aurel32bb7e7292008-10-12 20:16:03 +000022#include "host-utils.h"
Blue Swirl8546b092010-06-19 07:44:07 +000023#include "sysbus.h"
bellard574bbf72005-01-03 23:27:31 +000024
25//#define DEBUG_APIC
Blue Swirl0a3c5922010-05-29 20:23:48 +000026//#define DEBUG_COALESCING
27
28#ifdef DEBUG_APIC
29#define DPRINTF(fmt, ...) \
30 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
31#else
32#define DPRINTF(fmt, ...)
33#endif
34
35#ifdef DEBUG_COALESCING
36#define DPRINTF_C(fmt, ...) \
37 do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
38#else
39#define DPRINTF_C(fmt, ...)
40#endif
bellard574bbf72005-01-03 23:27:31 +000041
42/* APIC Local Vector Table */
43#define APIC_LVT_TIMER 0
44#define APIC_LVT_THERMAL 1
45#define APIC_LVT_PERFORM 2
46#define APIC_LVT_LINT0 3
47#define APIC_LVT_LINT1 4
48#define APIC_LVT_ERROR 5
49#define APIC_LVT_NB 6
50
51/* APIC delivery modes */
52#define APIC_DM_FIXED 0
53#define APIC_DM_LOWPRI 1
54#define APIC_DM_SMI 2
55#define APIC_DM_NMI 4
56#define APIC_DM_INIT 5
57#define APIC_DM_SIPI 6
58#define APIC_DM_EXTINT 7
59
bellardd592d302005-07-23 19:05:37 +000060/* APIC destination mode */
61#define APIC_DESTMODE_FLAT 0xf
62#define APIC_DESTMODE_CLUSTER 1
63
bellard574bbf72005-01-03 23:27:31 +000064#define APIC_TRIGGER_EDGE 0
65#define APIC_TRIGGER_LEVEL 1
66
67#define APIC_LVT_TIMER_PERIODIC (1<<17)
68#define APIC_LVT_MASKED (1<<16)
69#define APIC_LVT_LEVEL_TRIGGER (1<<15)
70#define APIC_LVT_REMOTE_IRR (1<<14)
71#define APIC_INPUT_POLARITY (1<<13)
72#define APIC_SEND_PENDING (1<<12)
73
74#define ESR_ILLEGAL_ADDRESS (1 << 7)
75
76#define APIC_SV_ENABLE (1 << 8)
77
bellardd3e9db92005-12-17 01:27:28 +000078#define MAX_APICS 255
79#define MAX_APIC_WORDS 8
80
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030081/* Intel APIC constants: from include/asm/msidef.h */
82#define MSI_DATA_VECTOR_SHIFT 0
83#define MSI_DATA_VECTOR_MASK 0x000000ff
84#define MSI_DATA_DELIVERY_MODE_SHIFT 8
85#define MSI_DATA_TRIGGER_SHIFT 15
86#define MSI_DATA_LEVEL_SHIFT 14
87#define MSI_ADDR_DEST_MODE_SHIFT 2
88#define MSI_ADDR_DEST_ID_SHIFT 12
89#define MSI_ADDR_DEST_ID_MASK 0x00ffff0
90
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030091#define MSI_ADDR_SIZE 0x100000
92
Blue Swirl92a16d72010-06-19 07:47:42 +000093typedef struct APICState APICState;
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 Swirl92a16d72010-06-19 07:47:42 +0000198void apic_deliver_pic_intr(DeviceState *d, int level)
aurel321a7de942008-08-21 03:14:52 +0000199{
Blue Swirl92a16d72010-06-19 07:47:42 +0000200 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
201
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300202 if (level) {
203 apic_local_deliver(s, APIC_LVT_LINT0);
204 } else {
aurel321a7de942008-08-21 03:14:52 +0000205 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
206
207 switch ((lvt >> 8) & 7) {
208 case APIC_DM_FIXED:
209 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
210 break;
211 reset_bit(s->irr, lvt & 0xff);
212 /* fall through */
213 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300214 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000215 break;
216 }
217 }
218}
219
bellardd3e9db92005-12-17 01:27:28 +0000220#define foreach_apic(apic, deliver_bitmask, code) \
221{\
222 int __i, __j, __mask;\
223 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
224 __mask = deliver_bitmask[__i];\
225 if (__mask) {\
226 for(__j = 0; __j < 32; __j++) {\
227 if (__mask & (1 << __j)) {\
228 apic = local_apics[__i * 32 + __j];\
229 if (apic) {\
230 code;\
231 }\
232 }\
233 }\
234 }\
235 }\
236}
237
ths5fafdf22007-09-16 21:08:06 +0000238static void apic_bus_deliver(const uint32_t *deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000239 uint8_t delivery_mode,
bellardd592d302005-07-23 19:05:37 +0000240 uint8_t vector_num, uint8_t polarity,
241 uint8_t trigger_mode)
242{
243 APICState *apic_iter;
244
245 switch (delivery_mode) {
246 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000247 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000248 {
249 int i, d;
250 d = -1;
251 for(i = 0; i < MAX_APIC_WORDS; i++) {
252 if (deliver_bitmask[i]) {
253 d = i * 32 + ffs_bit(deliver_bitmask[i]);
254 break;
255 }
256 }
257 if (d >= 0) {
258 apic_iter = local_apics[d];
259 if (apic_iter) {
260 apic_set_irq(apic_iter, vector_num, trigger_mode);
261 }
262 }
bellard8dd69b82005-11-23 20:59:44 +0000263 }
bellardd3e9db92005-12-17 01:27:28 +0000264 return;
bellard8dd69b82005-11-23 20:59:44 +0000265
bellardd592d302005-07-23 19:05:37 +0000266 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000267 break;
268
269 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000270 foreach_apic(apic_iter, deliver_bitmask,
271 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
272 return;
273
bellardd592d302005-07-23 19:05:37 +0000274 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000275 foreach_apic(apic_iter, deliver_bitmask,
276 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
277 return;
bellardd592d302005-07-23 19:05:37 +0000278
279 case APIC_DM_INIT:
280 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000281 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300282 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000283 return;
ths3b46e622007-09-17 08:09:54 +0000284
bellardd592d302005-07-23 19:05:37 +0000285 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000286 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000287 break;
288
289 default:
290 return;
291 }
292
ths5fafdf22007-09-16 21:08:06 +0000293 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000294 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000295}
bellard574bbf72005-01-03 23:27:31 +0000296
aliguori610626a2009-03-12 20:25:12 +0000297void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
298 uint8_t delivery_mode, uint8_t vector_num,
299 uint8_t polarity, uint8_t trigger_mode)
300{
301 uint32_t deliver_bitmask[MAX_APIC_WORDS];
302
Blue Swirl0a3c5922010-05-29 20:23:48 +0000303 DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
304 " polarity %d trigger_mode %d\n", __func__, dest, dest_mode,
305 delivery_mode, vector_num, polarity, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000306 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
307 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
308 trigger_mode);
309}
310
Blue Swirl92a16d72010-06-19 07:47:42 +0000311void cpu_set_apic_base(DeviceState *d, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000312{
Blue Swirl92a16d72010-06-19 07:47:42 +0000313 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
314
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 Swirl92a16d72010-06-19 07:47:42 +0000328uint64_t cpu_get_apic_base(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000329{
Blue Swirl92a16d72010-06-19 07:47:42 +0000330 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
331
Blue Swirl0a3c5922010-05-29 20:23:48 +0000332 DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
333 s ? (uint64_t)s->apicbase: 0);
aurel322c7c13d2009-04-08 22:56:26 +0000334 return s ? s->apicbase : 0;
bellard574bbf72005-01-03 23:27:31 +0000335}
336
Blue Swirl92a16d72010-06-19 07:47:42 +0000337void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
bellard9230e662005-01-23 20:46:56 +0000338{
Blue Swirl92a16d72010-06-19 07:47:42 +0000339 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
340
aurel322c7c13d2009-04-08 22:56:26 +0000341 if (!s)
342 return;
bellard9230e662005-01-23 20:46:56 +0000343 s->tpr = (val & 0x0f) << 4;
bellardd592d302005-07-23 19:05:37 +0000344 apic_update_irq(s);
bellard9230e662005-01-23 20:46:56 +0000345}
346
Blue Swirl92a16d72010-06-19 07:47:42 +0000347uint8_t cpu_get_apic_tpr(DeviceState *d)
bellard9230e662005-01-23 20:46:56 +0000348{
Blue Swirl92a16d72010-06-19 07:47:42 +0000349 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
350
aurel322c7c13d2009-04-08 22:56:26 +0000351 return s ? s->tpr >> 4 : 0;
bellard9230e662005-01-23 20:46:56 +0000352}
353
bellardd592d302005-07-23 19:05:37 +0000354/* return -1 if no bit is set */
355static int get_highest_priority_int(uint32_t *tab)
356{
357 int i;
358 for(i = 7; i >= 0; i--) {
359 if (tab[i] != 0) {
aurel323b63c042008-12-06 10:46:35 +0000360 return i * 32 + fls_bit(tab[i]);
bellardd592d302005-07-23 19:05:37 +0000361 }
362 }
363 return -1;
364}
365
bellard574bbf72005-01-03 23:27:31 +0000366static int apic_get_ppr(APICState *s)
367{
368 int tpr, isrv, ppr;
369
370 tpr = (s->tpr >> 4);
371 isrv = get_highest_priority_int(s->isr);
372 if (isrv < 0)
373 isrv = 0;
374 isrv >>= 4;
375 if (tpr >= isrv)
376 ppr = s->tpr;
377 else
378 ppr = isrv << 4;
379 return ppr;
380}
381
bellardd592d302005-07-23 19:05:37 +0000382static int apic_get_arb_pri(APICState *s)
383{
384 /* XXX: arbitration */
385 return 0;
386}
387
bellard574bbf72005-01-03 23:27:31 +0000388/* signal the CPU if an irq is pending */
389static void apic_update_irq(APICState *s)
390{
bellardd592d302005-07-23 19:05:37 +0000391 int irrv, ppr;
392 if (!(s->spurious_vec & APIC_SV_ENABLE))
393 return;
bellard574bbf72005-01-03 23:27:31 +0000394 irrv = get_highest_priority_int(s->irr);
395 if (irrv < 0)
396 return;
bellardd592d302005-07-23 19:05:37 +0000397 ppr = apic_get_ppr(s);
398 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
bellard574bbf72005-01-03 23:27:31 +0000399 return;
400 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
401}
402
aliguori73822ec2009-01-15 20:11:34 +0000403void apic_reset_irq_delivered(void)
404{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000405 DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000406 apic_irq_delivered = 0;
407}
408
409int apic_get_irq_delivered(void)
410{
Blue Swirl0a3c5922010-05-29 20:23:48 +0000411 DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000412 return apic_irq_delivered;
413}
414
bellard574bbf72005-01-03 23:27:31 +0000415static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
416{
aliguori73822ec2009-01-15 20:11:34 +0000417 apic_irq_delivered += !get_bit(s->irr, vector_num);
Blue Swirl0a3c5922010-05-29 20:23:48 +0000418 DPRINTF_C("%s: coalescing %d\n", __func__, apic_irq_delivered);
aliguori73822ec2009-01-15 20:11:34 +0000419
bellard574bbf72005-01-03 23:27:31 +0000420 set_bit(s->irr, vector_num);
421 if (trigger_mode)
422 set_bit(s->tmr, vector_num);
423 else
424 reset_bit(s->tmr, vector_num);
425 apic_update_irq(s);
426}
427
428static void apic_eoi(APICState *s)
429{
430 int isrv;
431 isrv = get_highest_priority_int(s->isr);
432 if (isrv < 0)
433 return;
434 reset_bit(s->isr, isrv);
bellardd592d302005-07-23 19:05:37 +0000435 /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
436 set the remote IRR bit for level triggered interrupts. */
bellard574bbf72005-01-03 23:27:31 +0000437 apic_update_irq(s);
438}
439
Gleb Natapov678e12c2009-06-10 15:40:48 +0300440static int apic_find_dest(uint8_t dest)
441{
442 APICState *apic = local_apics[dest];
443 int i;
444
445 if (apic && apic->id == dest)
446 return dest; /* shortcut in case apic->id == apic->idx */
447
448 for (i = 0; i < MAX_APICS; i++) {
449 apic = local_apics[i];
450 if (apic && apic->id == dest)
451 return i;
452 }
453
454 return -1;
455}
456
bellardd3e9db92005-12-17 01:27:28 +0000457static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
458 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000459{
bellardd592d302005-07-23 19:05:37 +0000460 APICState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000461 int i;
bellardd592d302005-07-23 19:05:37 +0000462
463 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000464 if (dest == 0xff) {
465 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
466 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300467 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000468 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300469 if (idx >= 0)
470 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000471 }
bellardd592d302005-07-23 19:05:37 +0000472 } else {
473 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000474 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
475 for(i = 0; i < MAX_APICS; i++) {
476 apic_iter = local_apics[i];
477 if (apic_iter) {
478 if (apic_iter->dest_mode == 0xf) {
479 if (dest & apic_iter->log_dest)
480 set_bit(deliver_bitmask, i);
481 } else if (apic_iter->dest_mode == 0x0) {
482 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
483 (dest & apic_iter->log_dest & 0x0f)) {
484 set_bit(deliver_bitmask, i);
485 }
486 }
487 }
bellardd592d302005-07-23 19:05:37 +0000488 }
489 }
bellardd592d302005-07-23 19:05:37 +0000490}
491
Blue Swirl92a16d72010-06-19 07:47:42 +0000492void apic_init_reset(DeviceState *d)
bellardd592d302005-07-23 19:05:37 +0000493{
Blue Swirl92a16d72010-06-19 07:47:42 +0000494 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellardd592d302005-07-23 19:05:37 +0000495 int i;
496
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300497 if (!s)
498 return;
499
bellardd592d302005-07-23 19:05:37 +0000500 s->tpr = 0;
501 s->spurious_vec = 0xff;
502 s->log_dest = 0;
bellarde0fd8782005-11-21 23:26:26 +0000503 s->dest_mode = 0xf;
bellardd592d302005-07-23 19:05:37 +0000504 memset(s->isr, 0, sizeof(s->isr));
505 memset(s->tmr, 0, sizeof(s->tmr));
506 memset(s->irr, 0, sizeof(s->irr));
bellardb4511722006-10-08 18:20:51 +0000507 for(i = 0; i < APIC_LVT_NB; i++)
508 s->lvt[i] = 1 << 16; /* mask LVT */
bellardd592d302005-07-23 19:05:37 +0000509 s->esr = 0;
510 memset(s->icr, 0, sizeof(s->icr));
511 s->divide_conf = 0;
512 s->count_shift = 0;
513 s->initial_count = 0;
514 s->initial_count_load_time = 0;
515 s->next_time = 0;
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300516 s->wait_for_sipi = 1;
bellardd592d302005-07-23 19:05:37 +0000517}
518
bellarde0fd8782005-11-21 23:26:26 +0000519static void apic_startup(APICState *s, int vector_num)
520{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300521 s->sipi_vector = vector_num;
522 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
523}
524
Blue Swirl92a16d72010-06-19 07:47:42 +0000525void apic_sipi(DeviceState *d)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300526{
Blue Swirl92a16d72010-06-19 07:47:42 +0000527 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
528
Blue Swirl4a942ce2010-06-19 10:42:31 +0300529 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300530
531 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000532 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300533 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300534 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000535}
536
Blue Swirl92a16d72010-06-19 07:47:42 +0000537static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
bellardd592d302005-07-23 19:05:37 +0000538 uint8_t delivery_mode, uint8_t vector_num,
539 uint8_t polarity, uint8_t trigger_mode)
540{
Blue Swirl92a16d72010-06-19 07:47:42 +0000541 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellardd3e9db92005-12-17 01:27:28 +0000542 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000543 int dest_shorthand = (s->icr[0] >> 18) & 3;
544 APICState *apic_iter;
545
bellarde0fd8782005-11-21 23:26:26 +0000546 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000547 case 0:
548 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
549 break;
550 case 1:
551 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300552 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000553 break;
554 case 2:
555 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
556 break;
557 case 3:
558 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300559 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000560 break;
bellarde0fd8782005-11-21 23:26:26 +0000561 }
562
bellardd592d302005-07-23 19:05:37 +0000563 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000564 case APIC_DM_INIT:
565 {
566 int trig_mode = (s->icr[0] >> 15) & 1;
567 int level = (s->icr[0] >> 14) & 1;
568 if (level == 0 && trig_mode == 1) {
ths5fafdf22007-09-16 21:08:06 +0000569 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000570 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000571 return;
572 }
573 }
574 break;
575
576 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000577 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000578 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000579 return;
580 }
581
bellardd592d302005-07-23 19:05:37 +0000582 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
583 trigger_mode);
584}
585
Blue Swirl92a16d72010-06-19 07:47:42 +0000586int apic_get_interrupt(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000587{
Blue Swirl92a16d72010-06-19 07:47:42 +0000588 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000589 int intno;
590
591 /* if the APIC is installed or enabled, we let the 8259 handle the
592 IRQs */
593 if (!s)
594 return -1;
595 if (!(s->spurious_vec & APIC_SV_ENABLE))
596 return -1;
ths3b46e622007-09-17 08:09:54 +0000597
bellard574bbf72005-01-03 23:27:31 +0000598 /* XXX: spurious IRQ handling */
599 intno = get_highest_priority_int(s->irr);
600 if (intno < 0)
601 return -1;
bellardd592d302005-07-23 19:05:37 +0000602 if (s->tpr && intno <= s->tpr)
603 return s->spurious_vec & 0xff;
bellardb4511722006-10-08 18:20:51 +0000604 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000605 set_bit(s->isr, intno);
606 apic_update_irq(s);
607 return intno;
608}
609
Blue Swirl92a16d72010-06-19 07:47:42 +0000610int apic_accept_pic_intr(DeviceState *d)
ths0e21e122007-10-09 03:08:56 +0000611{
Blue Swirl92a16d72010-06-19 07:47:42 +0000612 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
ths0e21e122007-10-09 03:08:56 +0000613 uint32_t lvt0;
614
615 if (!s)
616 return -1;
617
618 lvt0 = s->lvt[APIC_LVT_LINT0];
619
aurel32a5b38b52008-04-13 16:08:30 +0000620 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
621 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000622 return 1;
623
624 return 0;
625}
626
bellard574bbf72005-01-03 23:27:31 +0000627static uint32_t apic_get_current_count(APICState *s)
628{
629 int64_t d;
630 uint32_t val;
ths5fafdf22007-09-16 21:08:06 +0000631 d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000632 s->count_shift;
633 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
634 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000635 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000636 } else {
637 if (d >= s->initial_count)
638 val = 0;
639 else
640 val = s->initial_count - d;
641 }
642 return val;
643}
644
645static void apic_timer_update(APICState *s, int64_t current_time)
646{
647 int64_t next_time, d;
ths3b46e622007-09-17 08:09:54 +0000648
bellard574bbf72005-01-03 23:27:31 +0000649 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
ths5fafdf22007-09-16 21:08:06 +0000650 d = (current_time - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000651 s->count_shift;
652 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
aliguori681f8c22008-08-18 14:19:42 +0000653 if (!s->initial_count)
654 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000655 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
bellard574bbf72005-01-03 23:27:31 +0000656 } else {
657 if (d >= s->initial_count)
658 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000659 d = (uint64_t)s->initial_count + 1;
bellard574bbf72005-01-03 23:27:31 +0000660 }
661 next_time = s->initial_count_load_time + (d << s->count_shift);
662 qemu_mod_timer(s->timer, next_time);
663 s->next_time = next_time;
664 } else {
665 no_timer:
666 qemu_del_timer(s->timer);
667 }
668}
669
670static void apic_timer(void *opaque)
671{
672 APICState *s = opaque;
673
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300674 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000675 apic_timer_update(s, s->next_time);
676}
677
Anthony Liguoric227f092009-10-01 16:12:16 -0500678static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000679{
680 return 0;
681}
682
Anthony Liguoric227f092009-10-01 16:12:16 -0500683static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000684{
685 return 0;
686}
687
Anthony Liguoric227f092009-10-01 16:12:16 -0500688static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000689{
690}
691
Anthony Liguoric227f092009-10-01 16:12:16 -0500692static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000693{
694}
695
Anthony Liguoric227f092009-10-01 16:12:16 -0500696static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000697{
Blue Swirl92a16d72010-06-19 07:47:42 +0000698 DeviceState *d;
bellard574bbf72005-01-03 23:27:31 +0000699 APICState *s;
700 uint32_t val;
701 int index;
702
Blue Swirl92a16d72010-06-19 07:47:42 +0000703 d = cpu_get_current_apic();
704 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000705 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300706 }
Blue Swirl92a16d72010-06-19 07:47:42 +0000707 s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000708
709 index = (addr >> 4) & 0xff;
710 switch(index) {
711 case 0x02: /* id */
712 val = s->id << 24;
713 break;
714 case 0x03: /* version */
715 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
716 break;
717 case 0x08:
718 val = s->tpr;
719 break;
bellardd592d302005-07-23 19:05:37 +0000720 case 0x09:
721 val = apic_get_arb_pri(s);
722 break;
bellard574bbf72005-01-03 23:27:31 +0000723 case 0x0a:
724 /* ppr */
725 val = apic_get_ppr(s);
726 break;
aurel32b237db32008-03-28 22:31:36 +0000727 case 0x0b:
728 val = 0;
729 break;
bellardd592d302005-07-23 19:05:37 +0000730 case 0x0d:
731 val = s->log_dest << 24;
732 break;
733 case 0x0e:
734 val = s->dest_mode << 28;
735 break;
bellard574bbf72005-01-03 23:27:31 +0000736 case 0x0f:
737 val = s->spurious_vec;
738 break;
739 case 0x10 ... 0x17:
740 val = s->isr[index & 7];
741 break;
742 case 0x18 ... 0x1f:
743 val = s->tmr[index & 7];
744 break;
745 case 0x20 ... 0x27:
746 val = s->irr[index & 7];
747 break;
748 case 0x28:
749 val = s->esr;
750 break;
bellard574bbf72005-01-03 23:27:31 +0000751 case 0x30:
752 case 0x31:
753 val = s->icr[index & 1];
754 break;
bellarde0fd8782005-11-21 23:26:26 +0000755 case 0x32 ... 0x37:
756 val = s->lvt[index - 0x32];
757 break;
bellard574bbf72005-01-03 23:27:31 +0000758 case 0x38:
759 val = s->initial_count;
760 break;
761 case 0x39:
762 val = apic_get_current_count(s);
763 break;
764 case 0x3e:
765 val = s->divide_conf;
766 break;
767 default:
768 s->esr |= ESR_ILLEGAL_ADDRESS;
769 val = 0;
770 break;
771 }
Blue Swirl0a3c5922010-05-29 20:23:48 +0000772 DPRINTF("read: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000773 return val;
774}
775
Anthony Liguoric227f092009-10-01 16:12:16 -0500776static void apic_send_msi(target_phys_addr_t addr, uint32 data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300777{
778 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
779 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
780 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
781 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
782 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
783 /* XXX: Ignore redirection hint. */
784 apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
785}
786
Anthony Liguoric227f092009-10-01 16:12:16 -0500787static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000788{
Blue Swirl92a16d72010-06-19 07:47:42 +0000789 DeviceState *d;
bellard574bbf72005-01-03 23:27:31 +0000790 APICState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300791 int index = (addr >> 4) & 0xff;
792 if (addr > 0xfff || !index) {
793 /* MSI and MMIO APIC are at the same memory location,
794 * but actually not on the global bus: MSI is on PCI bus
795 * APIC is connected directly to the CPU.
796 * Mapping them on the global bus happens to work because
797 * MSI registers are reserved in APIC MMIO and vice versa. */
798 apic_send_msi(addr, val);
799 return;
800 }
bellard574bbf72005-01-03 23:27:31 +0000801
Blue Swirl92a16d72010-06-19 07:47:42 +0000802 d = cpu_get_current_apic();
803 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000804 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300805 }
Blue Swirl92a16d72010-06-19 07:47:42 +0000806 s = DO_UPCAST(APICState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000807
Blue Swirl0a3c5922010-05-29 20:23:48 +0000808 DPRINTF("write: " TARGET_FMT_plx " = %08x\n", addr, val);
bellard574bbf72005-01-03 23:27:31 +0000809
bellard574bbf72005-01-03 23:27:31 +0000810 switch(index) {
811 case 0x02:
812 s->id = (val >> 24);
813 break;
bellarde0fd8782005-11-21 23:26:26 +0000814 case 0x03:
815 break;
bellard574bbf72005-01-03 23:27:31 +0000816 case 0x08:
817 s->tpr = val;
bellardd592d302005-07-23 19:05:37 +0000818 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000819 break;
bellarde0fd8782005-11-21 23:26:26 +0000820 case 0x09:
821 case 0x0a:
822 break;
bellard574bbf72005-01-03 23:27:31 +0000823 case 0x0b: /* EOI */
824 apic_eoi(s);
825 break;
bellardd592d302005-07-23 19:05:37 +0000826 case 0x0d:
827 s->log_dest = val >> 24;
828 break;
829 case 0x0e:
830 s->dest_mode = val >> 28;
831 break;
bellard574bbf72005-01-03 23:27:31 +0000832 case 0x0f:
833 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000834 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000835 break;
bellarde0fd8782005-11-21 23:26:26 +0000836 case 0x10 ... 0x17:
837 case 0x18 ... 0x1f:
838 case 0x20 ... 0x27:
839 case 0x28:
840 break;
bellard574bbf72005-01-03 23:27:31 +0000841 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000842 s->icr[0] = val;
Blue Swirl92a16d72010-06-19 07:47:42 +0000843 apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
bellardd592d302005-07-23 19:05:37 +0000844 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
845 (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
846 break;
bellard574bbf72005-01-03 23:27:31 +0000847 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000848 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000849 break;
850 case 0x32 ... 0x37:
851 {
852 int n = index - 0x32;
853 s->lvt[n] = val;
854 if (n == APIC_LVT_TIMER)
855 apic_timer_update(s, qemu_get_clock(vm_clock));
856 }
857 break;
858 case 0x38:
859 s->initial_count = val;
860 s->initial_count_load_time = qemu_get_clock(vm_clock);
861 apic_timer_update(s, s->initial_count_load_time);
862 break;
bellarde0fd8782005-11-21 23:26:26 +0000863 case 0x39:
864 break;
bellard574bbf72005-01-03 23:27:31 +0000865 case 0x3e:
866 {
867 int v;
868 s->divide_conf = val & 0xb;
869 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
870 s->count_shift = (v + 1) & 7;
871 }
872 break;
873 default:
874 s->esr |= ESR_ILLEGAL_ADDRESS;
875 break;
876 }
877}
878
Juan Quintela695dcf72009-08-20 19:42:28 +0200879/* This function is only used for old state version 1 and 2 */
880static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
bellardd592d302005-07-23 19:05:37 +0000881{
882 APICState *s = opaque;
883 int i;
884
bellarde6cf6a82006-08-17 10:48:06 +0000885 if (version_id > 2)
bellardd592d302005-07-23 19:05:37 +0000886 return -EINVAL;
887
888 /* XXX: what if the base changes? (registered memory regions) */
889 qemu_get_be32s(f, &s->apicbase);
890 qemu_get_8s(f, &s->id);
891 qemu_get_8s(f, &s->arb_id);
892 qemu_get_8s(f, &s->tpr);
893 qemu_get_be32s(f, &s->spurious_vec);
894 qemu_get_8s(f, &s->log_dest);
895 qemu_get_8s(f, &s->dest_mode);
896 for (i = 0; i < 8; i++) {
897 qemu_get_be32s(f, &s->isr[i]);
898 qemu_get_be32s(f, &s->tmr[i]);
899 qemu_get_be32s(f, &s->irr[i]);
900 }
901 for (i = 0; i < APIC_LVT_NB; i++) {
902 qemu_get_be32s(f, &s->lvt[i]);
903 }
904 qemu_get_be32s(f, &s->esr);
905 qemu_get_be32s(f, &s->icr[0]);
906 qemu_get_be32s(f, &s->icr[1]);
907 qemu_get_be32s(f, &s->divide_conf);
thsbee8d682007-12-16 23:41:11 +0000908 s->count_shift=qemu_get_be32(f);
bellardd592d302005-07-23 19:05:37 +0000909 qemu_get_be32s(f, &s->initial_count);
thsbee8d682007-12-16 23:41:11 +0000910 s->initial_count_load_time=qemu_get_be64(f);
911 s->next_time=qemu_get_be64(f);
bellarde6cf6a82006-08-17 10:48:06 +0000912
913 if (version_id >= 2)
914 qemu_get_timer(f, s->timer);
bellardd592d302005-07-23 19:05:37 +0000915 return 0;
916}
917
Juan Quintela695dcf72009-08-20 19:42:28 +0200918static const VMStateDescription vmstate_apic = {
919 .name = "apic",
920 .version_id = 3,
921 .minimum_version_id = 3,
922 .minimum_version_id_old = 1,
923 .load_state_old = apic_load_old,
924 .fields = (VMStateField []) {
925 VMSTATE_UINT32(apicbase, APICState),
926 VMSTATE_UINT8(id, APICState),
927 VMSTATE_UINT8(arb_id, APICState),
928 VMSTATE_UINT8(tpr, APICState),
929 VMSTATE_UINT32(spurious_vec, APICState),
930 VMSTATE_UINT8(log_dest, APICState),
931 VMSTATE_UINT8(dest_mode, APICState),
932 VMSTATE_UINT32_ARRAY(isr, APICState, 8),
933 VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
934 VMSTATE_UINT32_ARRAY(irr, APICState, 8),
935 VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB),
936 VMSTATE_UINT32(esr, APICState),
937 VMSTATE_UINT32_ARRAY(icr, APICState, 2),
938 VMSTATE_UINT32(divide_conf, APICState),
939 VMSTATE_INT32(count_shift, APICState),
940 VMSTATE_UINT32(initial_count, APICState),
941 VMSTATE_INT64(initial_count_load_time, APICState),
942 VMSTATE_INT64(next_time, APICState),
943 VMSTATE_TIMER(timer, APICState),
944 VMSTATE_END_OF_LIST()
945 }
946};
947
Blue Swirl8546b092010-06-19 07:44:07 +0000948static void apic_reset(DeviceState *d)
bellardd592d302005-07-23 19:05:37 +0000949{
Blue Swirl8546b092010-06-19 07:44:07 +0000950 APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
Avi Kivity4c0960c2009-08-17 23:19:53 +0300951 int bsp;
aurel32fec5fa02008-09-02 00:09:08 +0000952
Avi Kivity4c0960c2009-08-17 23:19:53 +0300953 bsp = cpu_is_bsp(s->cpu_env);
aurel32fec5fa02008-09-02 00:09:08 +0000954 s->apicbase = 0xfee00000 |
Gleb Natapov678e12c2009-06-10 15:40:48 +0300955 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
aurel32fec5fa02008-09-02 00:09:08 +0000956
Blue Swirl92a16d72010-06-19 07:47:42 +0000957 apic_init_reset(d);
ths0e21e122007-10-09 03:08:56 +0000958
Gleb Natapov678e12c2009-06-10 15:40:48 +0300959 if (bsp) {
aurel32a5b38b52008-04-13 16:08:30 +0000960 /*
961 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
962 * time typically by BIOS, so PIC interrupt can be delivered to the
963 * processor when local APIC is enabled.
964 */
965 s->lvt[APIC_LVT_LINT0] = 0x700;
966 }
bellardd592d302005-07-23 19:05:37 +0000967}
bellard574bbf72005-01-03 23:27:31 +0000968
Blue Swirld60efc62009-08-25 18:29:31 +0000969static CPUReadMemoryFunc * const apic_mem_read[3] = {
bellard574bbf72005-01-03 23:27:31 +0000970 apic_mem_readb,
971 apic_mem_readw,
972 apic_mem_readl,
973};
974
Blue Swirld60efc62009-08-25 18:29:31 +0000975static CPUWriteMemoryFunc * const apic_mem_write[3] = {
bellard574bbf72005-01-03 23:27:31 +0000976 apic_mem_writeb,
977 apic_mem_writew,
978 apic_mem_writel,
979};
980
Blue Swirl8546b092010-06-19 07:44:07 +0000981static int apic_init1(SysBusDevice *dev)
982{
983 APICState *s = FROM_SYSBUS(APICState, dev);
984 int apic_io_memory;
985 static int last_apic_idx;
986
987 if (last_apic_idx >= MAX_APICS) {
988 return -1;
989 }
990 apic_io_memory = cpu_register_io_memory(apic_mem_read,
991 apic_mem_write, NULL);
992 sysbus_init_mmio(dev, MSI_ADDR_SIZE, apic_io_memory);
993
994 s->timer = qemu_new_timer(vm_clock, apic_timer, s);
995 s->idx = last_apic_idx++;
996 local_apics[s->idx] = s;
997 return 0;
998}
999
1000static SysBusDeviceInfo apic_info = {
1001 .init = apic_init1,
1002 .qdev.name = "apic",
1003 .qdev.size = sizeof(APICState),
1004 .qdev.vmsd = &vmstate_apic,
1005 .qdev.reset = apic_reset,
1006 .qdev.no_user = 1,
1007 .qdev.props = (Property[]) {
1008 DEFINE_PROP_UINT8("id", APICState, id, -1),
1009 DEFINE_PROP_PTR("cpu_env", APICState, cpu_env),
1010 DEFINE_PROP_END_OF_LIST(),
1011 }
1012};
1013
1014static void apic_register_devices(void)
1015{
1016 sysbus_register_withprop(&apic_info);
1017}
1018
1019device_init(apic_register_devices)