blob: 387a46940a071ca61ec02eb97a3bb8b97654d062 [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"
aurel32bb7e7292008-10-12 20:16:03 +000022#include "host-utils.h"
Blue Swirld8023f32010-10-20 16:41:28 +000023#include "trace.h"
Jan Kiszkad96e1732011-10-07 09:19:37 +020024#include "pc.h"
bellard574bbf72005-01-03 23:27:31 +000025
bellardd3e9db92005-12-17 01:27:28 +000026#define MAX_APIC_WORDS 8
27
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030028/* Intel APIC constants: from include/asm/msidef.h */
29#define MSI_DATA_VECTOR_SHIFT 0
30#define MSI_DATA_VECTOR_MASK 0x000000ff
31#define MSI_DATA_DELIVERY_MODE_SHIFT 8
32#define MSI_DATA_TRIGGER_SHIFT 15
33#define MSI_DATA_LEVEL_SHIFT 14
34#define MSI_ADDR_DEST_MODE_SHIFT 2
35#define MSI_ADDR_DEST_ID_SHIFT 12
36#define MSI_ADDR_DEST_ID_MASK 0x00ffff0
37
Jan Kiszkadae01682011-10-16 11:16:36 +020038static APICCommonState *local_apics[MAX_APICS + 1];
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +030039
Jan Kiszkadae01682011-10-16 11:16:36 +020040static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
41static void apic_update_irq(APICCommonState *s);
aliguori610626a2009-03-12 20:25:12 +000042static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
43 uint8_t dest, uint8_t dest_mode);
bellardd592d302005-07-23 19:05:37 +000044
aurel323b63c042008-12-06 10:46:35 +000045/* Find first bit starting from msb */
46static int fls_bit(uint32_t value)
47{
48 return 31 - clz32(value);
49}
50
aurel32e95f5492008-10-12 00:53:17 +000051/* Find first bit starting from lsb */
bellardd3e9db92005-12-17 01:27:28 +000052static int ffs_bit(uint32_t value)
53{
aurel32bb7e7292008-10-12 20:16:03 +000054 return ctz32(value);
bellardd3e9db92005-12-17 01:27:28 +000055}
56
57static inline void set_bit(uint32_t *tab, int index)
58{
59 int i, mask;
60 i = index >> 5;
61 mask = 1 << (index & 0x1f);
62 tab[i] |= mask;
63}
64
65static inline void reset_bit(uint32_t *tab, int index)
66{
67 int i, mask;
68 i = index >> 5;
69 mask = 1 << (index & 0x1f);
70 tab[i] &= ~mask;
71}
72
aliguori73822ec2009-01-15 20:11:34 +000073static inline int get_bit(uint32_t *tab, int index)
74{
75 int i, mask;
76 i = index >> 5;
77 mask = 1 << (index & 0x1f);
78 return !!(tab[i] & mask);
79}
80
Jan Kiszkadae01682011-10-16 11:16:36 +020081static void apic_local_deliver(APICCommonState *s, int vector)
aurel32a5b38b52008-04-13 16:08:30 +000082{
aurel32a5b38b52008-04-13 16:08:30 +000083 uint32_t lvt = s->lvt[vector];
84 int trigger_mode;
85
Blue Swirld8023f32010-10-20 16:41:28 +000086 trace_apic_local_deliver(vector, (lvt >> 8) & 7);
87
aurel32a5b38b52008-04-13 16:08:30 +000088 if (lvt & APIC_LVT_MASKED)
89 return;
90
91 switch ((lvt >> 8) & 7) {
92 case APIC_DM_SMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +030093 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
aurel32a5b38b52008-04-13 16:08:30 +000094 break;
95
96 case APIC_DM_NMI:
Blue Swirlcf6d64b2010-06-19 10:42:08 +030097 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
aurel32a5b38b52008-04-13 16:08:30 +000098 break;
99
100 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300101 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel32a5b38b52008-04-13 16:08:30 +0000102 break;
103
104 case APIC_DM_FIXED:
105 trigger_mode = APIC_TRIGGER_EDGE;
106 if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
107 (lvt & APIC_LVT_LEVEL_TRIGGER))
108 trigger_mode = APIC_TRIGGER_LEVEL;
109 apic_set_irq(s, lvt & 0xff, trigger_mode);
110 }
111}
112
Blue Swirl92a16d72010-06-19 07:47:42 +0000113void apic_deliver_pic_intr(DeviceState *d, int level)
aurel321a7de942008-08-21 03:14:52 +0000114{
Jan Kiszkadae01682011-10-16 11:16:36 +0200115 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
Blue Swirl92a16d72010-06-19 07:47:42 +0000116
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300117 if (level) {
118 apic_local_deliver(s, APIC_LVT_LINT0);
119 } else {
aurel321a7de942008-08-21 03:14:52 +0000120 uint32_t lvt = s->lvt[APIC_LVT_LINT0];
121
122 switch ((lvt >> 8) & 7) {
123 case APIC_DM_FIXED:
124 if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
125 break;
126 reset_bit(s->irr, lvt & 0xff);
127 /* fall through */
128 case APIC_DM_EXTINT:
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300129 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
aurel321a7de942008-08-21 03:14:52 +0000130 break;
131 }
132 }
133}
134
Jan Kiszkadae01682011-10-16 11:16:36 +0200135static void apic_external_nmi(APICCommonState *s)
Jan Kiszka02c09192011-10-18 00:00:06 +0800136{
Jan Kiszka02c09192011-10-18 00:00:06 +0800137 apic_local_deliver(s, APIC_LVT_LINT1);
138}
139
bellardd3e9db92005-12-17 01:27:28 +0000140#define foreach_apic(apic, deliver_bitmask, code) \
141{\
142 int __i, __j, __mask;\
143 for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
144 __mask = deliver_bitmask[__i];\
145 if (__mask) {\
146 for(__j = 0; __j < 32; __j++) {\
147 if (__mask & (1 << __j)) {\
148 apic = local_apics[__i * 32 + __j];\
149 if (apic) {\
150 code;\
151 }\
152 }\
153 }\
154 }\
155 }\
156}
157
ths5fafdf22007-09-16 21:08:06 +0000158static void apic_bus_deliver(const uint32_t *deliver_bitmask,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200159 uint8_t delivery_mode, uint8_t vector_num,
bellardd592d302005-07-23 19:05:37 +0000160 uint8_t trigger_mode)
161{
Jan Kiszkadae01682011-10-16 11:16:36 +0200162 APICCommonState *apic_iter;
bellardd592d302005-07-23 19:05:37 +0000163
164 switch (delivery_mode) {
165 case APIC_DM_LOWPRI:
bellard8dd69b82005-11-23 20:59:44 +0000166 /* XXX: search for focus processor, arbitration */
bellardd3e9db92005-12-17 01:27:28 +0000167 {
168 int i, d;
169 d = -1;
170 for(i = 0; i < MAX_APIC_WORDS; i++) {
171 if (deliver_bitmask[i]) {
172 d = i * 32 + ffs_bit(deliver_bitmask[i]);
173 break;
174 }
175 }
176 if (d >= 0) {
177 apic_iter = local_apics[d];
178 if (apic_iter) {
179 apic_set_irq(apic_iter, vector_num, trigger_mode);
180 }
181 }
bellard8dd69b82005-11-23 20:59:44 +0000182 }
bellardd3e9db92005-12-17 01:27:28 +0000183 return;
bellard8dd69b82005-11-23 20:59:44 +0000184
bellardd592d302005-07-23 19:05:37 +0000185 case APIC_DM_FIXED:
bellardd592d302005-07-23 19:05:37 +0000186 break;
187
188 case APIC_DM_SMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000189 foreach_apic(apic_iter, deliver_bitmask,
190 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
191 return;
192
bellardd592d302005-07-23 19:05:37 +0000193 case APIC_DM_NMI:
aurel32e2eb9d32008-04-13 16:08:23 +0000194 foreach_apic(apic_iter, deliver_bitmask,
195 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
196 return;
bellardd592d302005-07-23 19:05:37 +0000197
198 case APIC_DM_INIT:
199 /* normal INIT IPI sent to processors */
ths5fafdf22007-09-16 21:08:06 +0000200 foreach_apic(apic_iter, deliver_bitmask,
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300201 cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
bellardd592d302005-07-23 19:05:37 +0000202 return;
ths3b46e622007-09-17 08:09:54 +0000203
bellardd592d302005-07-23 19:05:37 +0000204 case APIC_DM_EXTINT:
bellardb1fc0342005-07-23 21:43:15 +0000205 /* handled in I/O APIC code */
bellardd592d302005-07-23 19:05:37 +0000206 break;
207
208 default:
209 return;
210 }
211
ths5fafdf22007-09-16 21:08:06 +0000212 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000213 apic_set_irq(apic_iter, vector_num, trigger_mode) );
bellardd592d302005-07-23 19:05:37 +0000214}
bellard574bbf72005-01-03 23:27:31 +0000215
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200216void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
217 uint8_t vector_num, uint8_t trigger_mode)
aliguori610626a2009-03-12 20:25:12 +0000218{
219 uint32_t deliver_bitmask[MAX_APIC_WORDS];
220
Blue Swirld8023f32010-10-20 16:41:28 +0000221 trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200222 trigger_mode);
Blue Swirld8023f32010-10-20 16:41:28 +0000223
aliguori610626a2009-03-12 20:25:12 +0000224 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200225 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
aliguori610626a2009-03-12 20:25:12 +0000226}
227
Jan Kiszkadae01682011-10-16 11:16:36 +0200228static void apic_set_base(APICCommonState *s, uint64_t val)
bellard574bbf72005-01-03 23:27:31 +0000229{
ths5fafdf22007-09-16 21:08:06 +0000230 s->apicbase = (val & 0xfffff000) |
bellard574bbf72005-01-03 23:27:31 +0000231 (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
232 /* if disabled, cannot be enabled again */
233 if (!(val & MSR_IA32_APICBASE_ENABLE)) {
234 s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300235 cpu_clear_apic_feature(s->cpu_env);
bellard574bbf72005-01-03 23:27:31 +0000236 s->spurious_vec &= ~APIC_SV_ENABLE;
237 }
238}
239
Jan Kiszkadae01682011-10-16 11:16:36 +0200240static void apic_set_tpr(APICCommonState *s, uint8_t val)
bellard574bbf72005-01-03 23:27:31 +0000241{
bellard9230e662005-01-23 20:46:56 +0000242 s->tpr = (val & 0x0f) << 4;
bellardd592d302005-07-23 19:05:37 +0000243 apic_update_irq(s);
bellard9230e662005-01-23 20:46:56 +0000244}
245
bellardd592d302005-07-23 19:05:37 +0000246/* return -1 if no bit is set */
247static int get_highest_priority_int(uint32_t *tab)
248{
249 int i;
250 for(i = 7; i >= 0; i--) {
251 if (tab[i] != 0) {
aurel323b63c042008-12-06 10:46:35 +0000252 return i * 32 + fls_bit(tab[i]);
bellardd592d302005-07-23 19:05:37 +0000253 }
254 }
255 return -1;
256}
257
Jan Kiszkadae01682011-10-16 11:16:36 +0200258static int apic_get_ppr(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000259{
260 int tpr, isrv, ppr;
261
262 tpr = (s->tpr >> 4);
263 isrv = get_highest_priority_int(s->isr);
264 if (isrv < 0)
265 isrv = 0;
266 isrv >>= 4;
267 if (tpr >= isrv)
268 ppr = s->tpr;
269 else
270 ppr = isrv << 4;
271 return ppr;
272}
273
Jan Kiszkadae01682011-10-16 11:16:36 +0200274static int apic_get_arb_pri(APICCommonState *s)
bellardd592d302005-07-23 19:05:37 +0000275{
276 /* XXX: arbitration */
277 return 0;
278}
279
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200280
281/*
282 * <0 - low prio interrupt,
283 * 0 - no interrupt,
284 * >0 - interrupt number
285 */
Jan Kiszkadae01682011-10-16 11:16:36 +0200286static int apic_irq_pending(APICCommonState *s)
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200287{
288 int irrv, ppr;
289 irrv = get_highest_priority_int(s->irr);
290 if (irrv < 0) {
291 return 0;
292 }
293 ppr = apic_get_ppr(s);
294 if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
295 return -1;
296 }
297
298 return irrv;
299}
300
bellard574bbf72005-01-03 23:27:31 +0000301/* signal the CPU if an irq is pending */
Jan Kiszkadae01682011-10-16 11:16:36 +0200302static void apic_update_irq(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000303{
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200304 if (!(s->spurious_vec & APIC_SV_ENABLE)) {
bellardd592d302005-07-23 19:05:37 +0000305 return;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200306 }
307 if (apic_irq_pending(s) > 0) {
308 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
Jan Kiszkad96e1732011-10-07 09:19:37 +0200309 } else if (apic_accept_pic_intr(&s->busdev.qdev) &&
310 pic_get_output(isa_pic)) {
311 apic_deliver_pic_intr(&s->busdev.qdev, 1);
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200312 }
bellard574bbf72005-01-03 23:27:31 +0000313}
314
Jan Kiszkadae01682011-10-16 11:16:36 +0200315static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
bellard574bbf72005-01-03 23:27:31 +0000316{
Jan Kiszka343270e2011-12-13 15:39:04 +0100317 apic_report_irq_delivered(!get_bit(s->irr, vector_num));
aliguori73822ec2009-01-15 20:11:34 +0000318
bellard574bbf72005-01-03 23:27:31 +0000319 set_bit(s->irr, vector_num);
320 if (trigger_mode)
321 set_bit(s->tmr, vector_num);
322 else
323 reset_bit(s->tmr, vector_num);
324 apic_update_irq(s);
325}
326
Jan Kiszkadae01682011-10-16 11:16:36 +0200327static void apic_eoi(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000328{
329 int isrv;
330 isrv = get_highest_priority_int(s->isr);
331 if (isrv < 0)
332 return;
333 reset_bit(s->isr, isrv);
Jan Kiszka0280b572011-02-03 22:54:11 +0100334 if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && get_bit(s->tmr, isrv)) {
335 ioapic_eoi_broadcast(isrv);
336 }
bellard574bbf72005-01-03 23:27:31 +0000337 apic_update_irq(s);
338}
339
Gleb Natapov678e12c2009-06-10 15:40:48 +0300340static int apic_find_dest(uint8_t dest)
341{
Jan Kiszkadae01682011-10-16 11:16:36 +0200342 APICCommonState *apic = local_apics[dest];
Gleb Natapov678e12c2009-06-10 15:40:48 +0300343 int i;
344
345 if (apic && apic->id == dest)
346 return dest; /* shortcut in case apic->id == apic->idx */
347
348 for (i = 0; i < MAX_APICS; i++) {
349 apic = local_apics[i];
350 if (apic && apic->id == dest)
351 return i;
Alex Williamsonb538e532010-11-05 16:01:29 -0600352 if (!apic)
353 break;
Gleb Natapov678e12c2009-06-10 15:40:48 +0300354 }
355
356 return -1;
357}
358
bellardd3e9db92005-12-17 01:27:28 +0000359static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
360 uint8_t dest, uint8_t dest_mode)
bellardd592d302005-07-23 19:05:37 +0000361{
Jan Kiszkadae01682011-10-16 11:16:36 +0200362 APICCommonState *apic_iter;
bellardd3e9db92005-12-17 01:27:28 +0000363 int i;
bellardd592d302005-07-23 19:05:37 +0000364
365 if (dest_mode == 0) {
bellardd3e9db92005-12-17 01:27:28 +0000366 if (dest == 0xff) {
367 memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
368 } else {
Gleb Natapov678e12c2009-06-10 15:40:48 +0300369 int idx = apic_find_dest(dest);
bellardd3e9db92005-12-17 01:27:28 +0000370 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300371 if (idx >= 0)
372 set_bit(deliver_bitmask, idx);
bellardd3e9db92005-12-17 01:27:28 +0000373 }
bellardd592d302005-07-23 19:05:37 +0000374 } else {
375 /* XXX: cluster mode */
bellardd3e9db92005-12-17 01:27:28 +0000376 memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
377 for(i = 0; i < MAX_APICS; i++) {
378 apic_iter = local_apics[i];
379 if (apic_iter) {
380 if (apic_iter->dest_mode == 0xf) {
381 if (dest & apic_iter->log_dest)
382 set_bit(deliver_bitmask, i);
383 } else if (apic_iter->dest_mode == 0x0) {
384 if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
385 (dest & apic_iter->log_dest & 0x0f)) {
386 set_bit(deliver_bitmask, i);
387 }
388 }
Alex Williamsonb538e532010-11-05 16:01:29 -0600389 } else {
390 break;
bellardd3e9db92005-12-17 01:27:28 +0000391 }
bellardd592d302005-07-23 19:05:37 +0000392 }
393 }
bellardd592d302005-07-23 19:05:37 +0000394}
395
Jan Kiszkadae01682011-10-16 11:16:36 +0200396static void apic_startup(APICCommonState *s, int vector_num)
bellarde0fd8782005-11-21 23:26:26 +0000397{
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300398 s->sipi_vector = vector_num;
399 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
400}
401
Blue Swirl92a16d72010-06-19 07:47:42 +0000402void apic_sipi(DeviceState *d)
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300403{
Jan Kiszkadae01682011-10-16 11:16:36 +0200404 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
Blue Swirl92a16d72010-06-19 07:47:42 +0000405
Blue Swirl4a942ce2010-06-19 10:42:31 +0300406 cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300407
408 if (!s->wait_for_sipi)
bellarde0fd8782005-11-21 23:26:26 +0000409 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300410 cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
Gleb Natapovb09ea7d2009-06-17 23:26:59 +0300411 s->wait_for_sipi = 0;
bellarde0fd8782005-11-21 23:26:26 +0000412}
413
Blue Swirl92a16d72010-06-19 07:47:42 +0000414static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
bellardd592d302005-07-23 19:05:37 +0000415 uint8_t delivery_mode, uint8_t vector_num,
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200416 uint8_t trigger_mode)
bellardd592d302005-07-23 19:05:37 +0000417{
Jan Kiszkadae01682011-10-16 11:16:36 +0200418 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellardd3e9db92005-12-17 01:27:28 +0000419 uint32_t deliver_bitmask[MAX_APIC_WORDS];
bellardd592d302005-07-23 19:05:37 +0000420 int dest_shorthand = (s->icr[0] >> 18) & 3;
Jan Kiszkadae01682011-10-16 11:16:36 +0200421 APICCommonState *apic_iter;
bellardd592d302005-07-23 19:05:37 +0000422
bellarde0fd8782005-11-21 23:26:26 +0000423 switch (dest_shorthand) {
bellardd3e9db92005-12-17 01:27:28 +0000424 case 0:
425 apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
426 break;
427 case 1:
428 memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300429 set_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000430 break;
431 case 2:
432 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
433 break;
434 case 3:
435 memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
Gleb Natapov678e12c2009-06-10 15:40:48 +0300436 reset_bit(deliver_bitmask, s->idx);
bellardd3e9db92005-12-17 01:27:28 +0000437 break;
bellarde0fd8782005-11-21 23:26:26 +0000438 }
439
bellardd592d302005-07-23 19:05:37 +0000440 switch (delivery_mode) {
bellardd592d302005-07-23 19:05:37 +0000441 case APIC_DM_INIT:
442 {
443 int trig_mode = (s->icr[0] >> 15) & 1;
444 int level = (s->icr[0] >> 14) & 1;
445 if (level == 0 && trig_mode == 1) {
ths5fafdf22007-09-16 21:08:06 +0000446 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000447 apic_iter->arb_id = apic_iter->id );
bellardd592d302005-07-23 19:05:37 +0000448 return;
449 }
450 }
451 break;
452
453 case APIC_DM_SIPI:
ths5fafdf22007-09-16 21:08:06 +0000454 foreach_apic(apic_iter, deliver_bitmask,
bellardd3e9db92005-12-17 01:27:28 +0000455 apic_startup(apic_iter, vector_num) );
bellardd592d302005-07-23 19:05:37 +0000456 return;
457 }
458
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200459 apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
bellardd592d302005-07-23 19:05:37 +0000460}
461
Blue Swirl92a16d72010-06-19 07:47:42 +0000462int apic_get_interrupt(DeviceState *d)
bellard574bbf72005-01-03 23:27:31 +0000463{
Jan Kiszkadae01682011-10-16 11:16:36 +0200464 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000465 int intno;
466
467 /* if the APIC is installed or enabled, we let the 8259 handle the
468 IRQs */
469 if (!s)
470 return -1;
471 if (!(s->spurious_vec & APIC_SV_ENABLE))
472 return -1;
ths3b46e622007-09-17 08:09:54 +0000473
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200474 intno = apic_irq_pending(s);
475
476 if (intno == 0) {
bellard574bbf72005-01-03 23:27:31 +0000477 return -1;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200478 } else if (intno < 0) {
bellardd592d302005-07-23 19:05:37 +0000479 return s->spurious_vec & 0xff;
Gleb Natapov0fbfbb52011-02-07 16:14:44 +0200480 }
bellardb4511722006-10-08 18:20:51 +0000481 reset_bit(s->irr, intno);
bellard574bbf72005-01-03 23:27:31 +0000482 set_bit(s->isr, intno);
483 apic_update_irq(s);
484 return intno;
485}
486
Blue Swirl92a16d72010-06-19 07:47:42 +0000487int apic_accept_pic_intr(DeviceState *d)
ths0e21e122007-10-09 03:08:56 +0000488{
Jan Kiszkadae01682011-10-16 11:16:36 +0200489 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
ths0e21e122007-10-09 03:08:56 +0000490 uint32_t lvt0;
491
492 if (!s)
493 return -1;
494
495 lvt0 = s->lvt[APIC_LVT_LINT0];
496
aurel32a5b38b52008-04-13 16:08:30 +0000497 if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
498 (lvt0 & APIC_LVT_MASKED) == 0)
ths0e21e122007-10-09 03:08:56 +0000499 return 1;
500
501 return 0;
502}
503
Jan Kiszkadae01682011-10-16 11:16:36 +0200504static uint32_t apic_get_current_count(APICCommonState *s)
bellard574bbf72005-01-03 23:27:31 +0000505{
506 int64_t d;
507 uint32_t val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100508 d = (qemu_get_clock_ns(vm_clock) - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000509 s->count_shift;
510 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
511 /* periodic */
bellardd592d302005-07-23 19:05:37 +0000512 val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
bellard574bbf72005-01-03 23:27:31 +0000513 } else {
514 if (d >= s->initial_count)
515 val = 0;
516 else
517 val = s->initial_count - d;
518 }
519 return val;
520}
521
Jan Kiszkadae01682011-10-16 11:16:36 +0200522static void apic_timer_update(APICCommonState *s, int64_t current_time)
bellard574bbf72005-01-03 23:27:31 +0000523{
524 int64_t next_time, d;
ths3b46e622007-09-17 08:09:54 +0000525
bellard574bbf72005-01-03 23:27:31 +0000526 if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
ths5fafdf22007-09-16 21:08:06 +0000527 d = (current_time - s->initial_count_load_time) >>
bellard574bbf72005-01-03 23:27:31 +0000528 s->count_shift;
529 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
aliguori681f8c22008-08-18 14:19:42 +0000530 if (!s->initial_count)
531 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000532 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
bellard574bbf72005-01-03 23:27:31 +0000533 } else {
534 if (d >= s->initial_count)
535 goto no_timer;
bellardd592d302005-07-23 19:05:37 +0000536 d = (uint64_t)s->initial_count + 1;
bellard574bbf72005-01-03 23:27:31 +0000537 }
538 next_time = s->initial_count_load_time + (d << s->count_shift);
539 qemu_mod_timer(s->timer, next_time);
540 s->next_time = next_time;
541 } else {
542 no_timer:
543 qemu_del_timer(s->timer);
544 }
545}
546
547static void apic_timer(void *opaque)
548{
Jan Kiszkadae01682011-10-16 11:16:36 +0200549 APICCommonState *s = opaque;
bellard574bbf72005-01-03 23:27:31 +0000550
Blue Swirlcf6d64b2010-06-19 10:42:08 +0300551 apic_local_deliver(s, APIC_LVT_TIMER);
bellard574bbf72005-01-03 23:27:31 +0000552 apic_timer_update(s, s->next_time);
553}
554
Anthony Liguoric227f092009-10-01 16:12:16 -0500555static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000556{
557 return 0;
558}
559
Anthony Liguoric227f092009-10-01 16:12:16 -0500560static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000561{
562 return 0;
563}
564
Anthony Liguoric227f092009-10-01 16:12:16 -0500565static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000566{
567}
568
Anthony Liguoric227f092009-10-01 16:12:16 -0500569static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000570{
571}
572
Anthony Liguoric227f092009-10-01 16:12:16 -0500573static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
bellard574bbf72005-01-03 23:27:31 +0000574{
Blue Swirl92a16d72010-06-19 07:47:42 +0000575 DeviceState *d;
Jan Kiszkadae01682011-10-16 11:16:36 +0200576 APICCommonState *s;
bellard574bbf72005-01-03 23:27:31 +0000577 uint32_t val;
578 int index;
579
Blue Swirl92a16d72010-06-19 07:47:42 +0000580 d = cpu_get_current_apic();
581 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000582 return 0;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300583 }
Jan Kiszkadae01682011-10-16 11:16:36 +0200584 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000585
586 index = (addr >> 4) & 0xff;
587 switch(index) {
588 case 0x02: /* id */
589 val = s->id << 24;
590 break;
591 case 0x03: /* version */
592 val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
593 break;
594 case 0x08:
595 val = s->tpr;
596 break;
bellardd592d302005-07-23 19:05:37 +0000597 case 0x09:
598 val = apic_get_arb_pri(s);
599 break;
bellard574bbf72005-01-03 23:27:31 +0000600 case 0x0a:
601 /* ppr */
602 val = apic_get_ppr(s);
603 break;
aurel32b237db32008-03-28 22:31:36 +0000604 case 0x0b:
605 val = 0;
606 break;
bellardd592d302005-07-23 19:05:37 +0000607 case 0x0d:
608 val = s->log_dest << 24;
609 break;
610 case 0x0e:
611 val = s->dest_mode << 28;
612 break;
bellard574bbf72005-01-03 23:27:31 +0000613 case 0x0f:
614 val = s->spurious_vec;
615 break;
616 case 0x10 ... 0x17:
617 val = s->isr[index & 7];
618 break;
619 case 0x18 ... 0x1f:
620 val = s->tmr[index & 7];
621 break;
622 case 0x20 ... 0x27:
623 val = s->irr[index & 7];
624 break;
625 case 0x28:
626 val = s->esr;
627 break;
bellard574bbf72005-01-03 23:27:31 +0000628 case 0x30:
629 case 0x31:
630 val = s->icr[index & 1];
631 break;
bellarde0fd8782005-11-21 23:26:26 +0000632 case 0x32 ... 0x37:
633 val = s->lvt[index - 0x32];
634 break;
bellard574bbf72005-01-03 23:27:31 +0000635 case 0x38:
636 val = s->initial_count;
637 break;
638 case 0x39:
639 val = apic_get_current_count(s);
640 break;
641 case 0x3e:
642 val = s->divide_conf;
643 break;
644 default:
645 s->esr |= ESR_ILLEGAL_ADDRESS;
646 val = 0;
647 break;
648 }
Blue Swirld8023f32010-10-20 16:41:28 +0000649 trace_apic_mem_readl(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000650 return val;
651}
652
Andreas Färberf5095c62010-12-19 17:22:39 +0100653static void apic_send_msi(target_phys_addr_t addr, uint32_t data)
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300654{
655 uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
656 uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
657 uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
658 uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
659 uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
660 /* XXX: Ignore redirection hint. */
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200661 apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300662}
663
Anthony Liguoric227f092009-10-01 16:12:16 -0500664static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard574bbf72005-01-03 23:27:31 +0000665{
Blue Swirl92a16d72010-06-19 07:47:42 +0000666 DeviceState *d;
Jan Kiszkadae01682011-10-16 11:16:36 +0200667 APICCommonState *s;
Michael S. Tsirkin54c96da2009-06-21 19:50:03 +0300668 int index = (addr >> 4) & 0xff;
669 if (addr > 0xfff || !index) {
670 /* MSI and MMIO APIC are at the same memory location,
671 * but actually not on the global bus: MSI is on PCI bus
672 * APIC is connected directly to the CPU.
673 * Mapping them on the global bus happens to work because
674 * MSI registers are reserved in APIC MMIO and vice versa. */
675 apic_send_msi(addr, val);
676 return;
677 }
bellard574bbf72005-01-03 23:27:31 +0000678
Blue Swirl92a16d72010-06-19 07:47:42 +0000679 d = cpu_get_current_apic();
680 if (!d) {
bellard574bbf72005-01-03 23:27:31 +0000681 return;
Blue Swirl0e26b7b2010-06-19 10:42:34 +0300682 }
Jan Kiszkadae01682011-10-16 11:16:36 +0200683 s = DO_UPCAST(APICCommonState, busdev.qdev, d);
bellard574bbf72005-01-03 23:27:31 +0000684
Blue Swirld8023f32010-10-20 16:41:28 +0000685 trace_apic_mem_writel(addr, val);
bellard574bbf72005-01-03 23:27:31 +0000686
bellard574bbf72005-01-03 23:27:31 +0000687 switch(index) {
688 case 0x02:
689 s->id = (val >> 24);
690 break;
bellarde0fd8782005-11-21 23:26:26 +0000691 case 0x03:
692 break;
bellard574bbf72005-01-03 23:27:31 +0000693 case 0x08:
694 s->tpr = val;
bellardd592d302005-07-23 19:05:37 +0000695 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000696 break;
bellarde0fd8782005-11-21 23:26:26 +0000697 case 0x09:
698 case 0x0a:
699 break;
bellard574bbf72005-01-03 23:27:31 +0000700 case 0x0b: /* EOI */
701 apic_eoi(s);
702 break;
bellardd592d302005-07-23 19:05:37 +0000703 case 0x0d:
704 s->log_dest = val >> 24;
705 break;
706 case 0x0e:
707 s->dest_mode = val >> 28;
708 break;
bellard574bbf72005-01-03 23:27:31 +0000709 case 0x0f:
710 s->spurious_vec = val & 0x1ff;
bellardd592d302005-07-23 19:05:37 +0000711 apic_update_irq(s);
bellard574bbf72005-01-03 23:27:31 +0000712 break;
bellarde0fd8782005-11-21 23:26:26 +0000713 case 0x10 ... 0x17:
714 case 0x18 ... 0x1f:
715 case 0x20 ... 0x27:
716 case 0x28:
717 break;
bellard574bbf72005-01-03 23:27:31 +0000718 case 0x30:
bellardd592d302005-07-23 19:05:37 +0000719 s->icr[0] = val;
Blue Swirl92a16d72010-06-19 07:47:42 +0000720 apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
bellardd592d302005-07-23 19:05:37 +0000721 (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
Jan Kiszka1f6f4082011-08-22 17:46:31 +0200722 (s->icr[0] >> 15) & 1);
bellardd592d302005-07-23 19:05:37 +0000723 break;
bellard574bbf72005-01-03 23:27:31 +0000724 case 0x31:
bellardd592d302005-07-23 19:05:37 +0000725 s->icr[1] = val;
bellard574bbf72005-01-03 23:27:31 +0000726 break;
727 case 0x32 ... 0x37:
728 {
729 int n = index - 0x32;
730 s->lvt[n] = val;
731 if (n == APIC_LVT_TIMER)
Paolo Bonzini74475452011-03-11 16:47:48 +0100732 apic_timer_update(s, qemu_get_clock_ns(vm_clock));
bellard574bbf72005-01-03 23:27:31 +0000733 }
734 break;
735 case 0x38:
736 s->initial_count = val;
Paolo Bonzini74475452011-03-11 16:47:48 +0100737 s->initial_count_load_time = qemu_get_clock_ns(vm_clock);
bellard574bbf72005-01-03 23:27:31 +0000738 apic_timer_update(s, s->initial_count_load_time);
739 break;
bellarde0fd8782005-11-21 23:26:26 +0000740 case 0x39:
741 break;
bellard574bbf72005-01-03 23:27:31 +0000742 case 0x3e:
743 {
744 int v;
745 s->divide_conf = val & 0xb;
746 v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
747 s->count_shift = (v + 1) & 7;
748 }
749 break;
750 default:
751 s->esr |= ESR_ILLEGAL_ADDRESS;
752 break;
753 }
754}
755
Avi Kivity312b4232011-08-15 17:17:16 +0300756static const MemoryRegionOps apic_io_ops = {
757 .old_mmio = {
758 .read = { apic_mem_readb, apic_mem_readw, apic_mem_readl, },
759 .write = { apic_mem_writeb, apic_mem_writew, apic_mem_writel, },
760 },
761 .endianness = DEVICE_NATIVE_ENDIAN,
bellard574bbf72005-01-03 23:27:31 +0000762};
763
Jan Kiszkadae01682011-10-16 11:16:36 +0200764static void apic_init(APICCommonState *s)
Blue Swirl8546b092010-06-19 07:44:07 +0000765{
Jan Kiszkadae01682011-10-16 11:16:36 +0200766 memory_region_init_io(&s->io_memory, &apic_io_ops, s, "apic-msi",
767 MSI_SPACE_SIZE);
Blue Swirl8546b092010-06-19 07:44:07 +0000768
Paolo Bonzini74475452011-03-11 16:47:48 +0100769 s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s);
Blue Swirl8546b092010-06-19 07:44:07 +0000770 local_apics[s->idx] = s;
Blue Swirl8546b092010-06-19 07:44:07 +0000771}
772
Jan Kiszkadae01682011-10-16 11:16:36 +0200773static APICCommonInfo apic_info = {
774 .busdev.qdev.name = "apic",
775 .init = apic_init,
776 .set_base = apic_set_base,
777 .set_tpr = apic_set_tpr,
778 .external_nmi = apic_external_nmi,
Blue Swirl8546b092010-06-19 07:44:07 +0000779};
780
781static void apic_register_devices(void)
782{
Jan Kiszkadae01682011-10-16 11:16:36 +0200783 apic_qdev_register(&apic_info);
Blue Swirl8546b092010-06-19 07:44:07 +0000784}
785
786device_init(apic_register_devices)