blob: 60552df61955dc2232b207bb89bbc7ccdf69350b [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 */
Jan Kiszkadae01682011-10-16 11:16:36 +020019#include "apic_internal.h"
Blue Swirlaa28b9b2010-03-21 19:46:26 +000020#include "apic.h"
Jan Kiszka0280b572011-02-03 22:54:11 +010021#include "ioapic.h"
Jan Kiszka08a82ac2012-05-16 15:41:11 -030022#include "msi.h"
aurel32bb7e7292008-10-12 20:16:03 +000023#include "host-utils.h"
Blue Swirld8023f32010-10-20 16:41:28 +000024#include "trace.h"
Jan Kiszkad96e1732011-10-07 09:19:37 +020025#include "pc.h"
Anthony PERARD9886c232012-06-21 15:41:28 +000026#include "apic-msidef.h"
bellard574bbf72005-01-03 23:27:31 +000027
bellardd3e9db92005-12-17 01:27:28 +000028#define MAX_APIC_WORDS 8
29
Jan Kiszkae5ad9362012-02-17 18:31:19 +010030#define SYNC_FROM_VAPIC 0x1
31#define SYNC_TO_VAPIC 0x2
32#define SYNC_ISR_IRR_TO_VAPIC 0x4
33
Jan Kiszkadae01682011-10-16 11:16:36 +020034static APICCommonState *local_apics[MAX_APICS + 1];
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030035
Jan Kiszkadae01682011-10-16 11:16:36 +020036static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
37static void apic_update_irq(APICCommonState *s);
aliguori610626a2009-03-12 20:25:12 +000038static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
39 uint8_t dest, uint8_t dest_mode);
bellardd592d302005-07-23 19:05:37 +000040
aurel323b63c042008-12-06 10:46:35 +000041/* Find first bit starting from msb */
42static int fls_bit(uint32_t value)
43{
44 return 31 - clz32(value);
45}
46
aurel32e95f5492008-10-12 00:53:17 +000047/* Find first bit starting from lsb */
bellardd3e9db92005-12-17 01:27:28 +000048static int ffs_bit(uint32_t value)
49{
aurel32bb7e7292008-10-12 20:16:03 +000050 return ctz32(value);
bellardd3e9db92005-12-17 01:27:28 +000051}
52
53static inline void set_bit(uint32_t *tab, int index)
54{
55 int i, mask;
56 i = index >> 5;
57 mask = 1 << (index & 0x1f);
58 tab[i] |= mask;
59}
60
61static inline void reset_bit(uint32_t *tab, int index)
62{
63 int i, mask;
64 i = index >> 5;
65 mask = 1 << (index & 0x1f);
66 tab[i] &= ~mask;
67}
68
aliguori73822ec2009-01-15 20:11:34 +000069static inline int get_bit(uint32_t *tab, int index)
70{
71 int i, mask;
72 i = index >> 5;
73 mask = 1 << (index & 0x1f);
74 return !!(tab[i] & mask);
75}
76
Jan Kiszkae5ad9362012-02-17 18:31:19 +010077/* return -1 if no bit is set */
78static int get_highest_priority_int(uint32_t *tab)
79{
80 int i;
81 for (i = 7; i >= 0; i--) {
82 if (tab[i] != 0) {
83 return i * 32 + fls_bit(tab[i]);
84 }
85 }
86 return -1;
87}
88
89static void apic_sync_vapic(APICCommonState *s, int sync_type)
90{
91 VAPICState vapic_state;
92 size_t length;
93 off_t start;
94 int vector;
95
96 if (!s->vapic_paddr) {
97 return;
98 }
99 if (sync_type & SYNC_FROM_VAPIC) {
100 cpu_physical_memory_rw(s->vapic_paddr, (void *)&vapic_state,
101 sizeof(vapic_state), 0);
102 s->tpr = vapic_state.tpr;
103 }
104 if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
105 start = offsetof(VAPICState, isr);
106 length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
107
108 if (sync_type & SYNC_TO_VAPIC) {
109 assert(qemu_cpu_is_self(s->cpu_env));
110
111 vapic_state.tpr = s->tpr;
112 vapic_state.enabled = 1;
113 start = 0;
114 length = sizeof(VAPICState);
115 }
116
117 vector = get_highest_priority_int(s->isr);
118 if (vector < 0) {
119 vector = 0;
120 }
121 vapic_state.isr = vector & 0xf0;
122
123 vapic_state.zero = 0;
124
125 vector = get_highest_priority_int(s->irr);
126 if (vector < 0) {
127 vector = 0;
128 }
129 vapic_state.irr = vector & 0xff;
130
131 cpu_physical_memory_write_rom(s->vapic_paddr + start,
132 ((void *)&vapic_state) + start, length);
133 }
134}
135
136static void apic_vapic_base_update(APICCommonState *s)
137{
138 apic_sync_vapic(s, SYNC_TO_VAPIC);
139}
140
Jan Kiszkadae01682011-10-16 11:16:36 +0200141static void apic_local_deliver(APICCommonState *s, int vector)
aurel32a5b38b52008-04-13 16:08:30 +0000142{
aurel32a5b38b52008-04-13 16:08:30 +0000143 uint32_t lvt = s->lvt[vector];
144 int trigger_mode;
145
Blue Swirld8023f32010-10-20 16:41:28 +0000146 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
147
aurel32a5b38b52008-04-13 16:08:30 +0000148 if (lvt & APIC_LVT_MASKED)
149 return;
150
151 switch ((lvt >> 8) & 7) {
152 case APIC_DM_SMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300153 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
aurel32a5b38b52008-04-13 16:08:30 +0000154 break;
155
156 case APIC_DM_NMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300157 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
aurel32a5b38b52008-04-13 16:08:30 +0000158 break;
159
160 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300161 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel32a5b38b52008-04-13 16:08:30 +0000162 break;
163
164 case APIC_DM_FIXED:
165 trigger_mode = APIC_TRIGGER_EDGE;
166 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
167 (lvt & APIC_LVT_LEVEL_TRIGGER))
168 trigger_mode = APIC_TRIGGER_LEVEL;
169 apic_set_irq(s, lvt & 0xff, trigger_mode);
170 }
171}
172
Blue Swirl92a16d72010-06-19 07:47:42 +0000173void apic_deliver_pic_intr(DeviceState *d, int level)
aurel321a7de942008-08-21 03:14:52 +0000174{
Jan Kiszkadae01682011-10-16 11:16:36 +0200175 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
Blue Swirl92a16d72010-06-19 07:47:42 +0000176
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300177 if (level) {
178 apic_local_deliver(s, APIC_LVT_LINT0);
179 } else {
aurel321a7de942008-08-21 03:14:52 +0000180 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
181
182 switch ((lvt >> 8) & 7) {
183 case APIC_DM_FIXED:
184 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
185 break;
186 reset_bit(s->irr, lvt & 0xff);
187 /* fall through */
188 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300189 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000190 break;
191 }
192 }
193}
194
Jan Kiszkadae01682011-10-16 11:16:36 +0200195static void apic_external_nmi(APICCommonState *s)
Jan Kiszka02c09192011-10-18 00:00:06 +0800196{
Jan Kiszka02c09192011-10-18 00:00:06 +0800197 apic_local_deliver(s, APIC_LVT_LINT1);
198}
199
bellardd3e9db92005-12-17 01:27:28 +0000200#define foreach_apic(apic, deliver_bitmask, code) \
201{\
202 int __i, __j, __mask;\
203 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
204 __mask = deliver_bitmask[__i];\
205 if (__mask) {\
206 for(__j = 0; __j < 32; __j++) {\
207 if (__mask & (1 << __j)) {\
208 apic = local_apics[__i * 32 + __j];\
209 if (apic) {\
210 code;\
211 }\
212 }\
213 }\
214 }\
215 }\
216}
217
ths5fafdf22007-09-16 21:08:06 +0000218static void apic_bus_deliver(const uint32_t *deliver_bitmask,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200219 uint8_t delivery_mode, uint8_t vector_num,
bellardd592d302005-07-23 19:05:37 +0000220 uint8_t trigger_mode)
221{
Jan Kiszkadae01682011-10-16 11:16:36 +0200222 APICCommonState *apic_iter;
bellardd592d302005-07-23 19:05:37 +0000223
224 switch (delivery_mode) {
225 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000226 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000227 {
228 int i, d;
229 d = -1;
230 for(i = 0; i < MAX_APIC_WORDS; i++) {
231 if (deliver_bitmask[i]) {
232 d = i * 32 + ffs_bit(deliver_bitmask[i]);
233 break;
234 }
235 }
236 if (d >= 0) {
237 apic_iter = local_apics[d];
238 if (apic_iter) {
239 apic_set_irq(apic_iter, vector_num, trigger_mode);
240 }
241 }
bellard8dd69b82005-11-23 20:59:44 +0000242 }
bellardd3e9db92005-12-17 01:27:28 +0000243 return;
bellard8dd69b82005-11-23 20:59:44 +0000244
bellardd592d302005-07-23 19:05:37 +0000245 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000246 break;
247
248 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000249 foreach_apic(apic_iter, deliver_bitmask,
250 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
251 return;
252
bellardd592d302005-07-23 19:05:37 +0000253 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000254 foreach_apic(apic_iter, deliver_bitmask,
255 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
256 return;
bellardd592d302005-07-23 19:05:37 +0000257
258 case APIC_DM_INIT:
259 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000260 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300261 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000262 return;
ths3b46e622007-09-17 08:09:54 +0000263
bellardd592d302005-07-23 19:05:37 +0000264 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000265 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000266 break;
267
268 default:
269 return;
270 }
271
ths5fafdf22007-09-16 21:08:06 +0000272 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000273 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000274}
bellard574bbf72005-01-03 23:27:31 +0000275
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200276void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
277 uint8_t vector_num, uint8_t trigger_mode)
aliguori610626a2009-03-12 20:25:12 +0000278{
279 uint32_t deliver_bitmask[MAX_APIC_WORDS];
280
Blue Swirld8023f32010-10-20 16:41:28 +0000281 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200282 trigger_mode);
Blue Swirld8023f32010-10-20 16:41:28 +0000283
aliguori610626a2009-03-12 20:25:12 +0000284 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200285 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000286}
287
Jan Kiszkadae01682011-10-16 11:16:36 +0200288static void apic_set_base(APICCommonState *s, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000289{
ths5fafdf22007-09-16 21:08:06 +0000290 s->apicbase = (val & 0xfffff000) |
bellard574bbf72005-01-03 23:27:31 +0000291 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
292 /* if disabled, cannot be enabled again */
293 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
294 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300295 cpu_clear_apic_feature(s->cpu_env);
bellard574bbf72005-01-03 23:27:31 +0000296 s->spurious_vec &= ~APIC_SV_ENABLE;
297 }
298}
299
Jan Kiszkadae01682011-10-16 11:16:36 +0200300static void apic_set_tpr(APICCommonState *s, uint8_t val)
bellard574bbf72005-01-03 23:27:31 +0000301{
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100302 /* Updates from cr8 are ignored while the VAPIC is active */
303 if (!s->vapic_paddr) {
304 s->tpr = val << 4;
305 apic_update_irq(s);
306 }
bellard9230e662005-01-23 20:46:56 +0000307}
308
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100309static uint8_t apic_get_tpr(APICCommonState *s)
bellardd592d302005-07-23 19:05:37 +0000310{
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100311 apic_sync_vapic(s, SYNC_FROM_VAPIC);
312 return s->tpr >> 4;
bellardd592d302005-07-23 19:05:37 +0000313}
314
Jan Kiszkadae01682011-10-16 11:16:36 +0200315static int apic_get_ppr(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000316{
317 int tpr, isrv, ppr;
318
319 tpr = (s->tpr >> 4);
320 isrv = get_highest_priority_int(s->isr);
321 if (isrv < 0)
322 isrv = 0;
323 isrv >>= 4;
324 if (tpr >= isrv)
325 ppr = s->tpr;
326 else
327 ppr = isrv << 4;
328 return ppr;
329}
330
Jan Kiszkadae01682011-10-16 11:16:36 +0200331static int apic_get_arb_pri(APICCommonState *s)
bellardd592d302005-07-23 19:05:37 +0000332{
333 /* XXX: arbitration */
334 return 0;
335}
336
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200337
338/*
339 * <0 - low prio interrupt,
340 * 0 - no interrupt,
341 * >0 - interrupt number
342 */
Jan Kiszkadae01682011-10-16 11:16:36 +0200343static int apic_irq_pending(APICCommonState *s)
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200344{
345 int irrv, ppr;
346 irrv = get_highest_priority_int(s->irr);
347 if (irrv < 0) {
348 return 0;
349 }
350 ppr = apic_get_ppr(s);
351 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
352 return -1;
353 }
354
355 return irrv;
356}
357
bellard574bbf72005-01-03 23:27:31 +0000358/* signal the CPU if an irq is pending */
Jan Kiszkadae01682011-10-16 11:16:36 +0200359static void apic_update_irq(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000360{
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200361 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
bellardd592d302005-07-23 19:05:37 +0000362 return;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200363 }
364 if (apic_irq_pending(s) > 0) {
365 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
Jan Kiszkad96e1732011-10-07 09:19:37 +0200366 } else if (apic_accept_pic_intr(&s->busdev.qdev) &&
367 pic_get_output(isa_pic)) {
368 apic_deliver_pic_intr(&s->busdev.qdev, 1);
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200369 }
bellard574bbf72005-01-03 23:27:31 +0000370}
371
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100372void apic_poll_irq(DeviceState *d)
373{
374 APICCommonState *s = APIC_COMMON(d);
375
376 apic_sync_vapic(s, SYNC_FROM_VAPIC);
377 apic_update_irq(s);
378}
379
Jan Kiszkadae01682011-10-16 11:16:36 +0200380static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
bellard574bbf72005-01-03 23:27:31 +0000381{
Jan Kiszka343270e2011-12-13 15:39:04 +0100382 apic_report_irq_delivered(!get_bit(s->irr, vector_num));
aliguori73822ec2009-01-15 20:11:34 +0000383
bellard574bbf72005-01-03 23:27:31 +0000384 set_bit(s->irr, vector_num);
385 if (trigger_mode)
386 set_bit(s->tmr, vector_num);
387 else
388 reset_bit(s->tmr, vector_num);
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100389 if (s->vapic_paddr) {
390 apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
391 /*
392 * The vcpu thread needs to see the new IRR before we pull its current
393 * TPR value. That way, if we miss a lowering of the TRP, the guest
394 * has the chance to notice the new IRR and poll for IRQs on its own.
395 */
396 smp_wmb();
397 apic_sync_vapic(s, SYNC_FROM_VAPIC);
398 }
bellard574bbf72005-01-03 23:27:31 +0000399 apic_update_irq(s);
400}
401
Jan Kiszkadae01682011-10-16 11:16:36 +0200402static void apic_eoi(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000403{
404 int isrv;
405 isrv = get_highest_priority_int(s->isr);
406 if (isrv < 0)
407 return;
408 reset_bit(s->isr, isrv);
Jan Kiszka0280b572011-02-03 22:54:11 +0100409 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && get_bit(s->tmr, isrv)) {
410 ioapic_eoi_broadcast(isrv);
411 }
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100412 apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
bellard574bbf72005-01-03 23:27:31 +0000413 apic_update_irq(s);
414}
415
Gleb Natapov678e12c2009-06-10 15:40:48 +0300416static int apic_find_dest(uint8_t dest)
417{
Jan Kiszkadae01682011-10-16 11:16:36 +0200418 APICCommonState *apic = local_apics[dest];
Gleb Natapov678e12c2009-06-10 15:40:48 +0300419 int i;
420
421 if (apic && apic->id == dest)
422 return dest; /* shortcut in case apic->id == apic->idx */
423
424 for (i = 0; i < MAX_APICS; i++) {
425 apic = local_apics[i];
426 if (apic && apic->id == dest)
427 return i;
Alex Williamsonb538e532010-11-05 16:01:29 -0600428 if (!apic)
429 break;
Gleb Natapov678e12c2009-06-10 15:40:48 +0300430 }
431
432 return -1;
433}
434
bellardd3e9db92005-12-17 01:27:28 +0000435static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
436 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000437{
Jan Kiszkadae01682011-10-16 11:16:36 +0200438 APICCommonState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000439 int i;
bellardd592d302005-07-23 19:05:37 +0000440
441 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000442 if (dest == 0xff) {
443 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
444 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300445 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000446 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300447 if (idx >= 0)
448 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000449 }
bellardd592d302005-07-23 19:05:37 +0000450 } else {
451 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000452 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
453 for(i = 0; i < MAX_APICS; i++) {
454 apic_iter = local_apics[i];
455 if (apic_iter) {
456 if (apic_iter->dest_mode == 0xf) {
457 if (dest & apic_iter->log_dest)
458 set_bit(deliver_bitmask, i);
459 } else if (apic_iter->dest_mode == 0x0) {
460 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
461 (dest & apic_iter->log_dest & 0x0f)) {
462 set_bit(deliver_bitmask, i);
463 }
464 }
Alex Williamsonb538e532010-11-05 16:01:29 -0600465 } else {
466 break;
bellardd3e9db92005-12-17 01:27:28 +0000467 }
bellardd592d302005-07-23 19:05:37 +0000468 }
469 }
bellardd592d302005-07-23 19:05:37 +0000470}
471
Jan Kiszkadae01682011-10-16 11:16:36 +0200472static void apic_startup(APICCommonState *s, int vector_num)
bellarde0fd8782005-11-21 23:26:26 +0000473{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300474 s->sipi_vector = vector_num;
475 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
476}
477
Blue Swirl92a16d72010-06-19 07:47:42 +0000478void apic_sipi(DeviceState *d)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300479{
Jan Kiszkadae01682011-10-16 11:16:36 +0200480 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
Blue Swirl92a16d72010-06-19 07:47:42 +0000481
Blue Swirl4a942ce2010-06-19 10:42:31 +0300482 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300483
484 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000485 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300486 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300487 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000488}
489
Blue Swirl92a16d72010-06-19 07:47:42 +0000490static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
bellardd592d302005-07-23 19:05:37 +0000491 uint8_t delivery_mode, uint8_t vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200492 uint8_t trigger_mode)
bellardd592d302005-07-23 19:05:37 +0000493{
Jan Kiszkadae01682011-10-16 11:16:36 +0200494 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellardd3e9db92005-12-17 01:27:28 +0000495 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000496 int dest_shorthand = (s->icr[0] >> 18) & 3;
Jan Kiszkadae01682011-10-16 11:16:36 +0200497 APICCommonState *apic_iter;
bellardd592d302005-07-23 19:05:37 +0000498
bellarde0fd8782005-11-21 23:26:26 +0000499 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000500 case 0:
501 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
502 break;
503 case 1:
504 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300505 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000506 break;
507 case 2:
508 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
509 break;
510 case 3:
511 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300512 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000513 break;
bellarde0fd8782005-11-21 23:26:26 +0000514 }
515
bellardd592d302005-07-23 19:05:37 +0000516 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000517 case APIC_DM_INIT:
518 {
519 int trig_mode = (s->icr[0] >> 15) & 1;
520 int level = (s->icr[0] >> 14) & 1;
521 if (level == 0 && trig_mode == 1) {
ths5fafdf22007-09-16 21:08:06 +0000522 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000523 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000524 return;
525 }
526 }
527 break;
528
529 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000530 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000531 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000532 return;
533 }
534
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200535 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
bellardd592d302005-07-23 19:05:37 +0000536}
537
Blue Swirl92a16d72010-06-19 07:47:42 +0000538int apic_get_interrupt(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000539{
Jan Kiszkadae01682011-10-16 11:16:36 +0200540 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000541 int intno;
542
543 /* if the APIC is installed or enabled, we let the 8259 handle the
544 IRQs */
545 if (!s)
546 return -1;
547 if (!(s->spurious_vec & APIC_SV_ENABLE))
548 return -1;
ths3b46e622007-09-17 08:09:54 +0000549
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100550 apic_sync_vapic(s, SYNC_FROM_VAPIC);
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200551 intno = apic_irq_pending(s);
552
553 if (intno == 0) {
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100554 apic_sync_vapic(s, SYNC_TO_VAPIC);
bellard574bbf72005-01-03 23:27:31 +0000555 return -1;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200556 } else if (intno < 0) {
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100557 apic_sync_vapic(s, SYNC_TO_VAPIC);
bellardd592d302005-07-23 19:05:37 +0000558 return s->spurious_vec & 0xff;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200559 }
bellardb4511722006-10-08 18:20:51 +0000560 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000561 set_bit(s->isr, intno);
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100562 apic_sync_vapic(s, SYNC_TO_VAPIC);
bellard574bbf72005-01-03 23:27:31 +0000563 apic_update_irq(s);
564 return intno;
565}
566
Blue Swirl92a16d72010-06-19 07:47:42 +0000567int apic_accept_pic_intr(DeviceState *d)
ths0e21e122007-10-09 03:08:56 +0000568{
Jan Kiszkadae01682011-10-16 11:16:36 +0200569 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
ths0e21e122007-10-09 03:08:56 +0000570 uint32_t lvt0;
571
572 if (!s)
573 return -1;
574
575 lvt0 = s->lvt[APIC_LVT_LINT0];
576
aurel32a5b38b52008-04-13 16:08:30 +0000577 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
578 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000579 return 1;
580
581 return 0;
582}
583
Jan Kiszkadae01682011-10-16 11:16:36 +0200584static uint32_t apic_get_current_count(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000585{
586 int64_t d;
587 uint32_t val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100588 d = (qemu_get_clock_ns(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000589 s->count_shift;
590 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
591 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000592 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000593 } else {
594 if (d >= s->initial_count)
595 val = 0;
596 else
597 val = s->initial_count - d;
598 }
599 return val;
600}
601
Jan Kiszkadae01682011-10-16 11:16:36 +0200602static void apic_timer_update(APICCommonState *s, int64_t current_time)
bellard574bbf72005-01-03 23:27:31 +0000603{
Jan Kiszka7a380ca2011-10-16 12:19:12 +0200604 if (apic_next_timer(s, current_time)) {
605 qemu_mod_timer(s->timer, s->next_time);
bellard574bbf72005-01-03 23:27:31 +0000606 } else {
bellard574bbf72005-01-03 23:27:31 +0000607 qemu_del_timer(s->timer);
608 }
609}
610
611static void apic_timer(void *opaque)
612{
Jan Kiszkadae01682011-10-16 11:16:36 +0200613 APICCommonState *s = opaque;
bellard574bbf72005-01-03 23:27:31 +0000614
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300615 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000616 apic_timer_update(s, s->next_time);
617}
618
Anthony Liguoric227f092009-10-01 16:12:16 -0500619static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000620{
621 return 0;
622}
623
Anthony Liguoric227f092009-10-01 16:12:16 -0500624static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000625{
626 return 0;
627}
628
Anthony Liguoric227f092009-10-01 16:12:16 -0500629static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000630{
631}
632
Anthony Liguoric227f092009-10-01 16:12:16 -0500633static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000634{
635}
636
Anthony Liguoric227f092009-10-01 16:12:16 -0500637static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000638{
Blue Swirl92a16d72010-06-19 07:47:42 +0000639 DeviceState *d;
Jan Kiszkadae01682011-10-16 11:16:36 +0200640 APICCommonState *s;
bellard574bbf72005-01-03 23:27:31 +0000641 uint32_t val;
642 int index;
643
Blue Swirl92a16d72010-06-19 07:47:42 +0000644 d = cpu_get_current_apic();
645 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000646 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300647 }
Jan Kiszkadae01682011-10-16 11:16:36 +0200648 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000649
650 index = (addr >> 4) & 0xff;
651 switch(index) {
652 case 0x02: /* id */
653 val = s->id << 24;
654 break;
655 case 0x03: /* version */
656 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
657 break;
658 case 0x08:
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100659 apic_sync_vapic(s, SYNC_FROM_VAPIC);
660 if (apic_report_tpr_access) {
661 cpu_report_tpr_access(s->cpu_env, TPR_ACCESS_READ);
662 }
bellard574bbf72005-01-03 23:27:31 +0000663 val = s->tpr;
664 break;
bellardd592d302005-07-23 19:05:37 +0000665 case 0x09:
666 val = apic_get_arb_pri(s);
667 break;
bellard574bbf72005-01-03 23:27:31 +0000668 case 0x0a:
669 /* ppr */
670 val = apic_get_ppr(s);
671 break;
aurel32b237db32008-03-28 22:31:36 +0000672 case 0x0b:
673 val = 0;
674 break;
bellardd592d302005-07-23 19:05:37 +0000675 case 0x0d:
676 val = s->log_dest << 24;
677 break;
678 case 0x0e:
679 val = s->dest_mode << 28;
680 break;
bellard574bbf72005-01-03 23:27:31 +0000681 case 0x0f:
682 val = s->spurious_vec;
683 break;
684 case 0x10 ... 0x17:
685 val = s->isr[index & 7];
686 break;
687 case 0x18 ... 0x1f:
688 val = s->tmr[index & 7];
689 break;
690 case 0x20 ... 0x27:
691 val = s->irr[index & 7];
692 break;
693 case 0x28:
694 val = s->esr;
695 break;
bellard574bbf72005-01-03 23:27:31 +0000696 case 0x30:
697 case 0x31:
698 val = s->icr[index & 1];
699 break;
bellarde0fd8782005-11-21 23:26:26 +0000700 case 0x32 ... 0x37:
701 val = s->lvt[index - 0x32];
702 break;
bellard574bbf72005-01-03 23:27:31 +0000703 case 0x38:
704 val = s->initial_count;
705 break;
706 case 0x39:
707 val = apic_get_current_count(s);
708 break;
709 case 0x3e:
710 val = s->divide_conf;
711 break;
712 default:
713 s->esr |= ESR_ILLEGAL_ADDRESS;
714 val = 0;
715 break;
716 }
Blue Swirld8023f32010-10-20 16:41:28 +0000717 trace_apic_mem_readl(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000718 return val;
719}
720
Andreas Färberf5095c62010-12-19 17:22:39 +0100721static void apic_send_msi(target_phys_addr_t addr, uint32_t data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300722{
723 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
724 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
725 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
726 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
727 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
728 /* XXX: Ignore redirection hint. */
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200729 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300730}
731
Anthony Liguoric227f092009-10-01 16:12:16 -0500732static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000733{
Blue Swirl92a16d72010-06-19 07:47:42 +0000734 DeviceState *d;
Jan Kiszkadae01682011-10-16 11:16:36 +0200735 APICCommonState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300736 int index = (addr >> 4) & 0xff;
737 if (addr > 0xfff || !index) {
738 /* MSI and MMIO APIC are at the same memory location,
739 * but actually not on the global bus: MSI is on PCI bus
740 * APIC is connected directly to the CPU.
741 * Mapping them on the global bus happens to work because
742 * MSI registers are reserved in APIC MMIO and vice versa. */
743 apic_send_msi(addr, val);
744 return;
745 }
bellard574bbf72005-01-03 23:27:31 +0000746
Blue Swirl92a16d72010-06-19 07:47:42 +0000747 d = cpu_get_current_apic();
748 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000749 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300750 }
Jan Kiszkadae01682011-10-16 11:16:36 +0200751 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000752
Blue Swirld8023f32010-10-20 16:41:28 +0000753 trace_apic_mem_writel(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000754
bellard574bbf72005-01-03 23:27:31 +0000755 switch(index) {
756 case 0x02:
757 s->id = (val >> 24);
758 break;
bellarde0fd8782005-11-21 23:26:26 +0000759 case 0x03:
760 break;
bellard574bbf72005-01-03 23:27:31 +0000761 case 0x08:
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100762 if (apic_report_tpr_access) {
763 cpu_report_tpr_access(s->cpu_env, TPR_ACCESS_WRITE);
764 }
bellard574bbf72005-01-03 23:27:31 +0000765 s->tpr = val;
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100766 apic_sync_vapic(s, SYNC_TO_VAPIC);
bellardd592d302005-07-23 19:05:37 +0000767 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000768 break;
bellarde0fd8782005-11-21 23:26:26 +0000769 case 0x09:
770 case 0x0a:
771 break;
bellard574bbf72005-01-03 23:27:31 +0000772 case 0x0b: /* EOI */
773 apic_eoi(s);
774 break;
bellardd592d302005-07-23 19:05:37 +0000775 case 0x0d:
776 s->log_dest = val >> 24;
777 break;
778 case 0x0e:
779 s->dest_mode = val >> 28;
780 break;
bellard574bbf72005-01-03 23:27:31 +0000781 case 0x0f:
782 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000783 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000784 break;
bellarde0fd8782005-11-21 23:26:26 +0000785 case 0x10 ... 0x17:
786 case 0x18 ... 0x1f:
787 case 0x20 ... 0x27:
788 case 0x28:
789 break;
bellard574bbf72005-01-03 23:27:31 +0000790 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000791 s->icr[0] = val;
Blue Swirl92a16d72010-06-19 07:47:42 +0000792 apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
bellardd592d302005-07-23 19:05:37 +0000793 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200794 (s->icr[0] >> 15) & 1);
bellardd592d302005-07-23 19:05:37 +0000795 break;
bellard574bbf72005-01-03 23:27:31 +0000796 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000797 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000798 break;
799 case 0x32 ... 0x37:
800 {
801 int n = index - 0x32;
802 s->lvt[n] = val;
803 if (n == APIC_LVT_TIMER)
Paolo Bonzini74475452011-03-11 16:47:48 +0100804 apic_timer_update(s, qemu_get_clock_ns(vm_clock));
bellard574bbf72005-01-03 23:27:31 +0000805 }
806 break;
807 case 0x38:
808 s->initial_count = val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100809 s->initial_count_load_time = qemu_get_clock_ns(vm_clock);
bellard574bbf72005-01-03 23:27:31 +0000810 apic_timer_update(s, s->initial_count_load_time);
811 break;
bellarde0fd8782005-11-21 23:26:26 +0000812 case 0x39:
813 break;
bellard574bbf72005-01-03 23:27:31 +0000814 case 0x3e:
815 {
816 int v;
817 s->divide_conf = val & 0xb;
818 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
819 s->count_shift = (v + 1) & 7;
820 }
821 break;
822 default:
823 s->esr |= ESR_ILLEGAL_ADDRESS;
824 break;
825 }
826}
827
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100828static void apic_pre_save(APICCommonState *s)
829{
830 apic_sync_vapic(s, SYNC_FROM_VAPIC);
831}
832
Jan Kiszka7a380ca2011-10-16 12:19:12 +0200833static void apic_post_load(APICCommonState *s)
834{
835 if (s->timer_expiry != -1) {
836 qemu_mod_timer(s->timer, s->timer_expiry);
837 } else {
838 qemu_del_timer(s->timer);
839 }
840}
841
Avi Kivity312b4232011-08-15 17:17:16 +0300842static const MemoryRegionOps apic_io_ops = {
843 .old_mmio = {
844 .read = { apic_mem_readb, apic_mem_readw, apic_mem_readl, },
845 .write = { apic_mem_writeb, apic_mem_writew, apic_mem_writel, },
846 },
847 .endianness = DEVICE_NATIVE_ENDIAN,
bellard574bbf72005-01-03 23:27:31 +0000848};
849
Jan Kiszkadae01682011-10-16 11:16:36 +0200850static void apic_init(APICCommonState *s)
Blue Swirl8546b092010-06-19 07:44:07 +0000851{
Jan Kiszkadae01682011-10-16 11:16:36 +0200852 memory_region_init_io(&s->io_memory, &apic_io_ops, s, "apic-msi",
853 MSI_SPACE_SIZE);
Blue Swirl8546b092010-06-19 07:44:07 +0000854
Paolo Bonzini74475452011-03-11 16:47:48 +0100855 s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s);
Blue Swirl8546b092010-06-19 07:44:07 +0000856 local_apics[s->idx] = s;
Jan Kiszka08a82ac2012-05-16 15:41:11 -0300857
858 msi_supported = true;
Blue Swirl8546b092010-06-19 07:44:07 +0000859}
860
Anthony Liguori999e12b2012-01-24 13:12:29 -0600861static void apic_class_init(ObjectClass *klass, void *data)
862{
863 APICCommonClass *k = APIC_COMMON_CLASS(klass);
864
865 k->init = apic_init;
866 k->set_base = apic_set_base;
867 k->set_tpr = apic_set_tpr;
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100868 k->get_tpr = apic_get_tpr;
869 k->vapic_base_update = apic_vapic_base_update;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600870 k->external_nmi = apic_external_nmi;
Jan Kiszkae5ad9362012-02-17 18:31:19 +0100871 k->pre_save = apic_pre_save;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600872 k->post_load = apic_post_load;
873}
874
Anthony Liguori39bffca2011-12-07 21:34:16 -0600875static TypeInfo apic_info = {
876 .name = "apic",
877 .instance_size = sizeof(APICCommonState),
878 .parent = TYPE_APIC_COMMON,
879 .class_init = apic_class_init,
Blue Swirl8546b092010-06-19 07:44:07 +0000880};
881
Andreas Färber83f7d432012-02-09 15:20:55 +0100882static void apic_register_types(void)
Blue Swirl8546b092010-06-19 07:44:07 +0000883{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600884 type_register_static(&apic_info);
Blue Swirl8546b092010-06-19 07:44:07 +0000885}
886
Andreas Färber83f7d432012-02-09 15:20:55 +0100887type_init(apic_register_types)