root / hw / apic.c @ a2d4e44b
History | View | Annotate | Download (26.6 kB)
1 | 574bbf7b | bellard | /*
|
---|---|---|---|
2 | 574bbf7b | bellard | * APIC support
|
3 | 574bbf7b | bellard | *
|
4 | 574bbf7b | bellard | * Copyright (c) 2004-2005 Fabrice Bellard
|
5 | 574bbf7b | bellard | *
|
6 | 574bbf7b | bellard | * This library is free software; you can redistribute it and/or
|
7 | 574bbf7b | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 574bbf7b | bellard | * License as published by the Free Software Foundation; either
|
9 | 574bbf7b | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 574bbf7b | bellard | *
|
11 | 574bbf7b | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 574bbf7b | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 574bbf7b | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 574bbf7b | bellard | * Lesser General Public License for more details.
|
15 | 574bbf7b | bellard | *
|
16 | 574bbf7b | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 574bbf7b | bellard | * License along with this library; if not, write to the Free Software
|
18 | 574bbf7b | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 574bbf7b | bellard | */
|
20 | 574bbf7b | bellard | #include "vl.h" |
21 | 574bbf7b | bellard | |
22 | 574bbf7b | bellard | //#define DEBUG_APIC
|
23 | d592d303 | bellard | //#define DEBUG_IOAPIC
|
24 | 574bbf7b | bellard | |
25 | 574bbf7b | bellard | /* APIC Local Vector Table */
|
26 | 574bbf7b | bellard | #define APIC_LVT_TIMER 0 |
27 | 574bbf7b | bellard | #define APIC_LVT_THERMAL 1 |
28 | 574bbf7b | bellard | #define APIC_LVT_PERFORM 2 |
29 | 574bbf7b | bellard | #define APIC_LVT_LINT0 3 |
30 | 574bbf7b | bellard | #define APIC_LVT_LINT1 4 |
31 | 574bbf7b | bellard | #define APIC_LVT_ERROR 5 |
32 | 574bbf7b | bellard | #define APIC_LVT_NB 6 |
33 | 574bbf7b | bellard | |
34 | 574bbf7b | bellard | /* APIC delivery modes */
|
35 | 574bbf7b | bellard | #define APIC_DM_FIXED 0 |
36 | 574bbf7b | bellard | #define APIC_DM_LOWPRI 1 |
37 | 574bbf7b | bellard | #define APIC_DM_SMI 2 |
38 | 574bbf7b | bellard | #define APIC_DM_NMI 4 |
39 | 574bbf7b | bellard | #define APIC_DM_INIT 5 |
40 | 574bbf7b | bellard | #define APIC_DM_SIPI 6 |
41 | 574bbf7b | bellard | #define APIC_DM_EXTINT 7 |
42 | 574bbf7b | bellard | |
43 | d592d303 | bellard | /* APIC destination mode */
|
44 | d592d303 | bellard | #define APIC_DESTMODE_FLAT 0xf |
45 | d592d303 | bellard | #define APIC_DESTMODE_CLUSTER 1 |
46 | d592d303 | bellard | |
47 | 574bbf7b | bellard | #define APIC_TRIGGER_EDGE 0 |
48 | 574bbf7b | bellard | #define APIC_TRIGGER_LEVEL 1 |
49 | 574bbf7b | bellard | |
50 | 574bbf7b | bellard | #define APIC_LVT_TIMER_PERIODIC (1<<17) |
51 | 574bbf7b | bellard | #define APIC_LVT_MASKED (1<<16) |
52 | 574bbf7b | bellard | #define APIC_LVT_LEVEL_TRIGGER (1<<15) |
53 | 574bbf7b | bellard | #define APIC_LVT_REMOTE_IRR (1<<14) |
54 | 574bbf7b | bellard | #define APIC_INPUT_POLARITY (1<<13) |
55 | 574bbf7b | bellard | #define APIC_SEND_PENDING (1<<12) |
56 | 574bbf7b | bellard | |
57 | d592d303 | bellard | #define IOAPIC_NUM_PINS 0x18 |
58 | d592d303 | bellard | |
59 | 574bbf7b | bellard | #define ESR_ILLEGAL_ADDRESS (1 << 7) |
60 | 574bbf7b | bellard | |
61 | 574bbf7b | bellard | #define APIC_SV_ENABLE (1 << 8) |
62 | 574bbf7b | bellard | |
63 | d3e9db93 | bellard | #define MAX_APICS 255 |
64 | d3e9db93 | bellard | #define MAX_APIC_WORDS 8 |
65 | d3e9db93 | bellard | |
66 | 574bbf7b | bellard | typedef struct APICState { |
67 | 574bbf7b | bellard | CPUState *cpu_env; |
68 | 574bbf7b | bellard | uint32_t apicbase; |
69 | 574bbf7b | bellard | uint8_t id; |
70 | d592d303 | bellard | uint8_t arb_id; |
71 | 574bbf7b | bellard | uint8_t tpr; |
72 | 574bbf7b | bellard | uint32_t spurious_vec; |
73 | d592d303 | bellard | uint8_t log_dest; |
74 | d592d303 | bellard | uint8_t dest_mode; |
75 | 574bbf7b | bellard | uint32_t isr[8]; /* in service register */ |
76 | 574bbf7b | bellard | uint32_t tmr[8]; /* trigger mode register */ |
77 | 574bbf7b | bellard | uint32_t irr[8]; /* interrupt request register */ |
78 | 574bbf7b | bellard | uint32_t lvt[APIC_LVT_NB]; |
79 | 574bbf7b | bellard | uint32_t esr; /* error register */
|
80 | 574bbf7b | bellard | uint32_t icr[2];
|
81 | 574bbf7b | bellard | |
82 | 574bbf7b | bellard | uint32_t divide_conf; |
83 | 574bbf7b | bellard | int count_shift;
|
84 | 574bbf7b | bellard | uint32_t initial_count; |
85 | 574bbf7b | bellard | int64_t initial_count_load_time, next_time; |
86 | 574bbf7b | bellard | QEMUTimer *timer; |
87 | 574bbf7b | bellard | } APICState; |
88 | 574bbf7b | bellard | |
89 | d592d303 | bellard | struct IOAPICState {
|
90 | d592d303 | bellard | uint8_t id; |
91 | d592d303 | bellard | uint8_t ioregsel; |
92 | d592d303 | bellard | |
93 | d592d303 | bellard | uint32_t irr; |
94 | d592d303 | bellard | uint64_t ioredtbl[IOAPIC_NUM_PINS]; |
95 | d592d303 | bellard | }; |
96 | d592d303 | bellard | |
97 | 574bbf7b | bellard | static int apic_io_memory; |
98 | d3e9db93 | bellard | static APICState *local_apics[MAX_APICS + 1]; |
99 | d592d303 | bellard | static int last_apic_id = 0; |
100 | d592d303 | bellard | |
101 | d592d303 | bellard | static void apic_init_ipi(APICState *s); |
102 | d592d303 | bellard | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode); |
103 | d592d303 | bellard | static void apic_update_irq(APICState *s); |
104 | d592d303 | bellard | |
105 | d3e9db93 | bellard | /* Find first bit starting from msb. Return 0 if value = 0 */
|
106 | d3e9db93 | bellard | static int fls_bit(uint32_t value) |
107 | d3e9db93 | bellard | { |
108 | d3e9db93 | bellard | unsigned int ret = 0; |
109 | d3e9db93 | bellard | |
110 | d3e9db93 | bellard | #if defined(HOST_I386)
|
111 | d3e9db93 | bellard | __asm__ __volatile__ ("bsr %1, %0\n" : "+r" (ret) : "rm" (value)); |
112 | d3e9db93 | bellard | return ret;
|
113 | d3e9db93 | bellard | #else
|
114 | d3e9db93 | bellard | if (value > 0xffff) |
115 | d3e9db93 | bellard | value >>= 16, ret = 16; |
116 | d3e9db93 | bellard | if (value > 0xff) |
117 | d3e9db93 | bellard | value >>= 8, ret += 8; |
118 | d3e9db93 | bellard | if (value > 0xf) |
119 | d3e9db93 | bellard | value >>= 4, ret += 4; |
120 | d3e9db93 | bellard | if (value > 0x3) |
121 | d3e9db93 | bellard | value >>= 2, ret += 2; |
122 | d3e9db93 | bellard | return ret + (value >> 1); |
123 | d3e9db93 | bellard | #endif
|
124 | d3e9db93 | bellard | } |
125 | d3e9db93 | bellard | |
126 | d3e9db93 | bellard | /* Find first bit starting from lsb. Return 0 if value = 0 */
|
127 | d3e9db93 | bellard | static int ffs_bit(uint32_t value) |
128 | d3e9db93 | bellard | { |
129 | d3e9db93 | bellard | unsigned int ret = 0; |
130 | d3e9db93 | bellard | |
131 | d3e9db93 | bellard | #if defined(HOST_I386)
|
132 | d3e9db93 | bellard | __asm__ __volatile__ ("bsf %1, %0\n" : "+r" (ret) : "rm" (value)); |
133 | d3e9db93 | bellard | return ret;
|
134 | d3e9db93 | bellard | #else
|
135 | d3e9db93 | bellard | if (!value)
|
136 | d3e9db93 | bellard | return 0; |
137 | d3e9db93 | bellard | if (!(value & 0xffff)) |
138 | d3e9db93 | bellard | value >>= 16, ret = 16; |
139 | d3e9db93 | bellard | if (!(value & 0xff)) |
140 | d3e9db93 | bellard | value >>= 8, ret += 8; |
141 | d3e9db93 | bellard | if (!(value & 0xf)) |
142 | d3e9db93 | bellard | value >>= 4, ret += 4; |
143 | d3e9db93 | bellard | if (!(value & 0x3)) |
144 | d3e9db93 | bellard | value >>= 2, ret += 2; |
145 | d3e9db93 | bellard | if (!(value & 0x1)) |
146 | d3e9db93 | bellard | ret++; |
147 | d3e9db93 | bellard | return ret;
|
148 | d3e9db93 | bellard | #endif
|
149 | d3e9db93 | bellard | } |
150 | d3e9db93 | bellard | |
151 | d3e9db93 | bellard | static inline void set_bit(uint32_t *tab, int index) |
152 | d3e9db93 | bellard | { |
153 | d3e9db93 | bellard | int i, mask;
|
154 | d3e9db93 | bellard | i = index >> 5;
|
155 | d3e9db93 | bellard | mask = 1 << (index & 0x1f); |
156 | d3e9db93 | bellard | tab[i] |= mask; |
157 | d3e9db93 | bellard | } |
158 | d3e9db93 | bellard | |
159 | d3e9db93 | bellard | static inline void reset_bit(uint32_t *tab, int index) |
160 | d3e9db93 | bellard | { |
161 | d3e9db93 | bellard | int i, mask;
|
162 | d3e9db93 | bellard | i = index >> 5;
|
163 | d3e9db93 | bellard | mask = 1 << (index & 0x1f); |
164 | d3e9db93 | bellard | tab[i] &= ~mask; |
165 | d3e9db93 | bellard | } |
166 | d3e9db93 | bellard | |
167 | d3e9db93 | bellard | #define foreach_apic(apic, deliver_bitmask, code) \
|
168 | d3e9db93 | bellard | {\ |
169 | d3e9db93 | bellard | int __i, __j, __mask;\
|
170 | d3e9db93 | bellard | for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\ |
171 | d3e9db93 | bellard | __mask = deliver_bitmask[__i];\ |
172 | d3e9db93 | bellard | if (__mask) {\
|
173 | d3e9db93 | bellard | for(__j = 0; __j < 32; __j++) {\ |
174 | d3e9db93 | bellard | if (__mask & (1 << __j)) {\ |
175 | d3e9db93 | bellard | apic = local_apics[__i * 32 + __j];\
|
176 | d3e9db93 | bellard | if (apic) {\
|
177 | d3e9db93 | bellard | code;\ |
178 | d3e9db93 | bellard | }\ |
179 | d3e9db93 | bellard | }\ |
180 | d3e9db93 | bellard | }\ |
181 | d3e9db93 | bellard | }\ |
182 | d3e9db93 | bellard | }\ |
183 | d3e9db93 | bellard | } |
184 | d3e9db93 | bellard | |
185 | d3e9db93 | bellard | static void apic_bus_deliver(const uint32_t *deliver_bitmask, |
186 | d3e9db93 | bellard | uint8_t delivery_mode, |
187 | d592d303 | bellard | uint8_t vector_num, uint8_t polarity, |
188 | d592d303 | bellard | uint8_t trigger_mode) |
189 | d592d303 | bellard | { |
190 | d592d303 | bellard | APICState *apic_iter; |
191 | d592d303 | bellard | |
192 | d592d303 | bellard | switch (delivery_mode) {
|
193 | d592d303 | bellard | case APIC_DM_LOWPRI:
|
194 | 8dd69b8f | bellard | /* XXX: search for focus processor, arbitration */
|
195 | d3e9db93 | bellard | { |
196 | d3e9db93 | bellard | int i, d;
|
197 | d3e9db93 | bellard | d = -1;
|
198 | d3e9db93 | bellard | for(i = 0; i < MAX_APIC_WORDS; i++) { |
199 | d3e9db93 | bellard | if (deliver_bitmask[i]) {
|
200 | d3e9db93 | bellard | d = i * 32 + ffs_bit(deliver_bitmask[i]);
|
201 | d3e9db93 | bellard | break;
|
202 | d3e9db93 | bellard | } |
203 | d3e9db93 | bellard | } |
204 | d3e9db93 | bellard | if (d >= 0) { |
205 | d3e9db93 | bellard | apic_iter = local_apics[d]; |
206 | d3e9db93 | bellard | if (apic_iter) {
|
207 | d3e9db93 | bellard | apic_set_irq(apic_iter, vector_num, trigger_mode); |
208 | d3e9db93 | bellard | } |
209 | d3e9db93 | bellard | } |
210 | 8dd69b8f | bellard | } |
211 | d3e9db93 | bellard | return;
|
212 | 8dd69b8f | bellard | |
213 | d592d303 | bellard | case APIC_DM_FIXED:
|
214 | d592d303 | bellard | break;
|
215 | d592d303 | bellard | |
216 | d592d303 | bellard | case APIC_DM_SMI:
|
217 | d592d303 | bellard | case APIC_DM_NMI:
|
218 | d592d303 | bellard | break;
|
219 | d592d303 | bellard | |
220 | d592d303 | bellard | case APIC_DM_INIT:
|
221 | d592d303 | bellard | /* normal INIT IPI sent to processors */
|
222 | d3e9db93 | bellard | foreach_apic(apic_iter, deliver_bitmask, |
223 | d3e9db93 | bellard | apic_init_ipi(apic_iter) ); |
224 | d592d303 | bellard | return;
|
225 | d592d303 | bellard | |
226 | d592d303 | bellard | case APIC_DM_EXTINT:
|
227 | b1fc0348 | bellard | /* handled in I/O APIC code */
|
228 | d592d303 | bellard | break;
|
229 | d592d303 | bellard | |
230 | d592d303 | bellard | default:
|
231 | d592d303 | bellard | return;
|
232 | d592d303 | bellard | } |
233 | d592d303 | bellard | |
234 | d3e9db93 | bellard | foreach_apic(apic_iter, deliver_bitmask, |
235 | d3e9db93 | bellard | apic_set_irq(apic_iter, vector_num, trigger_mode) ); |
236 | d592d303 | bellard | } |
237 | 574bbf7b | bellard | |
238 | 574bbf7b | bellard | void cpu_set_apic_base(CPUState *env, uint64_t val)
|
239 | 574bbf7b | bellard | { |
240 | 574bbf7b | bellard | APICState *s = env->apic_state; |
241 | 574bbf7b | bellard | #ifdef DEBUG_APIC
|
242 | 26a76461 | bellard | printf("cpu_set_apic_base: %016" PRIx64 "\n", val); |
243 | 574bbf7b | bellard | #endif
|
244 | 574bbf7b | bellard | s->apicbase = (val & 0xfffff000) |
|
245 | 574bbf7b | bellard | (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE)); |
246 | 574bbf7b | bellard | /* if disabled, cannot be enabled again */
|
247 | 574bbf7b | bellard | if (!(val & MSR_IA32_APICBASE_ENABLE)) {
|
248 | 574bbf7b | bellard | s->apicbase &= ~MSR_IA32_APICBASE_ENABLE; |
249 | 574bbf7b | bellard | env->cpuid_features &= ~CPUID_APIC; |
250 | 574bbf7b | bellard | s->spurious_vec &= ~APIC_SV_ENABLE; |
251 | 574bbf7b | bellard | } |
252 | 574bbf7b | bellard | } |
253 | 574bbf7b | bellard | |
254 | 574bbf7b | bellard | uint64_t cpu_get_apic_base(CPUState *env) |
255 | 574bbf7b | bellard | { |
256 | 574bbf7b | bellard | APICState *s = env->apic_state; |
257 | 574bbf7b | bellard | #ifdef DEBUG_APIC
|
258 | 26a76461 | bellard | printf("cpu_get_apic_base: %016" PRIx64 "\n", (uint64_t)s->apicbase); |
259 | 574bbf7b | bellard | #endif
|
260 | 574bbf7b | bellard | return s->apicbase;
|
261 | 574bbf7b | bellard | } |
262 | 574bbf7b | bellard | |
263 | 9230e66e | bellard | void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
|
264 | 9230e66e | bellard | { |
265 | 9230e66e | bellard | APICState *s = env->apic_state; |
266 | 9230e66e | bellard | s->tpr = (val & 0x0f) << 4; |
267 | d592d303 | bellard | apic_update_irq(s); |
268 | 9230e66e | bellard | } |
269 | 9230e66e | bellard | |
270 | 9230e66e | bellard | uint8_t cpu_get_apic_tpr(CPUX86State *env) |
271 | 9230e66e | bellard | { |
272 | 9230e66e | bellard | APICState *s = env->apic_state; |
273 | 9230e66e | bellard | return s->tpr >> 4; |
274 | 9230e66e | bellard | } |
275 | 9230e66e | bellard | |
276 | d592d303 | bellard | /* return -1 if no bit is set */
|
277 | d592d303 | bellard | static int get_highest_priority_int(uint32_t *tab) |
278 | d592d303 | bellard | { |
279 | d592d303 | bellard | int i;
|
280 | d592d303 | bellard | for(i = 7; i >= 0; i--) { |
281 | d592d303 | bellard | if (tab[i] != 0) { |
282 | d592d303 | bellard | return i * 32 + fls_bit(tab[i]); |
283 | d592d303 | bellard | } |
284 | d592d303 | bellard | } |
285 | d592d303 | bellard | return -1; |
286 | d592d303 | bellard | } |
287 | d592d303 | bellard | |
288 | 574bbf7b | bellard | static int apic_get_ppr(APICState *s) |
289 | 574bbf7b | bellard | { |
290 | 574bbf7b | bellard | int tpr, isrv, ppr;
|
291 | 574bbf7b | bellard | |
292 | 574bbf7b | bellard | tpr = (s->tpr >> 4);
|
293 | 574bbf7b | bellard | isrv = get_highest_priority_int(s->isr); |
294 | 574bbf7b | bellard | if (isrv < 0) |
295 | 574bbf7b | bellard | isrv = 0;
|
296 | 574bbf7b | bellard | isrv >>= 4;
|
297 | 574bbf7b | bellard | if (tpr >= isrv)
|
298 | 574bbf7b | bellard | ppr = s->tpr; |
299 | 574bbf7b | bellard | else
|
300 | 574bbf7b | bellard | ppr = isrv << 4;
|
301 | 574bbf7b | bellard | return ppr;
|
302 | 574bbf7b | bellard | } |
303 | 574bbf7b | bellard | |
304 | d592d303 | bellard | static int apic_get_arb_pri(APICState *s) |
305 | d592d303 | bellard | { |
306 | d592d303 | bellard | /* XXX: arbitration */
|
307 | d592d303 | bellard | return 0; |
308 | d592d303 | bellard | } |
309 | d592d303 | bellard | |
310 | 574bbf7b | bellard | /* signal the CPU if an irq is pending */
|
311 | 574bbf7b | bellard | static void apic_update_irq(APICState *s) |
312 | 574bbf7b | bellard | { |
313 | d592d303 | bellard | int irrv, ppr;
|
314 | d592d303 | bellard | if (!(s->spurious_vec & APIC_SV_ENABLE))
|
315 | d592d303 | bellard | return;
|
316 | 574bbf7b | bellard | irrv = get_highest_priority_int(s->irr); |
317 | 574bbf7b | bellard | if (irrv < 0) |
318 | 574bbf7b | bellard | return;
|
319 | d592d303 | bellard | ppr = apic_get_ppr(s); |
320 | d592d303 | bellard | if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) |
321 | 574bbf7b | bellard | return;
|
322 | 574bbf7b | bellard | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
323 | 574bbf7b | bellard | } |
324 | 574bbf7b | bellard | |
325 | 574bbf7b | bellard | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode) |
326 | 574bbf7b | bellard | { |
327 | 574bbf7b | bellard | set_bit(s->irr, vector_num); |
328 | 574bbf7b | bellard | if (trigger_mode)
|
329 | 574bbf7b | bellard | set_bit(s->tmr, vector_num); |
330 | 574bbf7b | bellard | else
|
331 | 574bbf7b | bellard | reset_bit(s->tmr, vector_num); |
332 | 574bbf7b | bellard | apic_update_irq(s); |
333 | 574bbf7b | bellard | } |
334 | 574bbf7b | bellard | |
335 | 574bbf7b | bellard | static void apic_eoi(APICState *s) |
336 | 574bbf7b | bellard | { |
337 | 574bbf7b | bellard | int isrv;
|
338 | 574bbf7b | bellard | isrv = get_highest_priority_int(s->isr); |
339 | 574bbf7b | bellard | if (isrv < 0) |
340 | 574bbf7b | bellard | return;
|
341 | 574bbf7b | bellard | reset_bit(s->isr, isrv); |
342 | d592d303 | bellard | /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
|
343 | d592d303 | bellard | set the remote IRR bit for level triggered interrupts. */
|
344 | 574bbf7b | bellard | apic_update_irq(s); |
345 | 574bbf7b | bellard | } |
346 | 574bbf7b | bellard | |
347 | d3e9db93 | bellard | static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask, |
348 | d3e9db93 | bellard | uint8_t dest, uint8_t dest_mode) |
349 | d592d303 | bellard | { |
350 | d592d303 | bellard | APICState *apic_iter; |
351 | d3e9db93 | bellard | int i;
|
352 | d592d303 | bellard | |
353 | d592d303 | bellard | if (dest_mode == 0) { |
354 | d3e9db93 | bellard | if (dest == 0xff) { |
355 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t)); |
356 | d3e9db93 | bellard | } else {
|
357 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
358 | d3e9db93 | bellard | set_bit(deliver_bitmask, dest); |
359 | d3e9db93 | bellard | } |
360 | d592d303 | bellard | } else {
|
361 | d592d303 | bellard | /* XXX: cluster mode */
|
362 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
363 | d3e9db93 | bellard | for(i = 0; i < MAX_APICS; i++) { |
364 | d3e9db93 | bellard | apic_iter = local_apics[i]; |
365 | d3e9db93 | bellard | if (apic_iter) {
|
366 | d3e9db93 | bellard | if (apic_iter->dest_mode == 0xf) { |
367 | d3e9db93 | bellard | if (dest & apic_iter->log_dest)
|
368 | d3e9db93 | bellard | set_bit(deliver_bitmask, i); |
369 | d3e9db93 | bellard | } else if (apic_iter->dest_mode == 0x0) { |
370 | d3e9db93 | bellard | if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) && |
371 | d3e9db93 | bellard | (dest & apic_iter->log_dest & 0x0f)) {
|
372 | d3e9db93 | bellard | set_bit(deliver_bitmask, i); |
373 | d3e9db93 | bellard | } |
374 | d3e9db93 | bellard | } |
375 | d3e9db93 | bellard | } |
376 | d592d303 | bellard | } |
377 | d592d303 | bellard | } |
378 | d592d303 | bellard | } |
379 | d592d303 | bellard | |
380 | d592d303 | bellard | |
381 | d592d303 | bellard | static void apic_init_ipi(APICState *s) |
382 | d592d303 | bellard | { |
383 | d592d303 | bellard | int i;
|
384 | d592d303 | bellard | |
385 | d592d303 | bellard | s->tpr = 0;
|
386 | d592d303 | bellard | s->spurious_vec = 0xff;
|
387 | d592d303 | bellard | s->log_dest = 0;
|
388 | e0fd8781 | bellard | s->dest_mode = 0xf;
|
389 | d592d303 | bellard | memset(s->isr, 0, sizeof(s->isr)); |
390 | d592d303 | bellard | memset(s->tmr, 0, sizeof(s->tmr)); |
391 | d592d303 | bellard | memset(s->irr, 0, sizeof(s->irr)); |
392 | b4511723 | bellard | for(i = 0; i < APIC_LVT_NB; i++) |
393 | b4511723 | bellard | s->lvt[i] = 1 << 16; /* mask LVT */ |
394 | d592d303 | bellard | s->esr = 0;
|
395 | d592d303 | bellard | memset(s->icr, 0, sizeof(s->icr)); |
396 | d592d303 | bellard | s->divide_conf = 0;
|
397 | d592d303 | bellard | s->count_shift = 0;
|
398 | d592d303 | bellard | s->initial_count = 0;
|
399 | d592d303 | bellard | s->initial_count_load_time = 0;
|
400 | d592d303 | bellard | s->next_time = 0;
|
401 | d592d303 | bellard | } |
402 | d592d303 | bellard | |
403 | e0fd8781 | bellard | /* send a SIPI message to the CPU to start it */
|
404 | e0fd8781 | bellard | static void apic_startup(APICState *s, int vector_num) |
405 | e0fd8781 | bellard | { |
406 | e0fd8781 | bellard | CPUState *env = s->cpu_env; |
407 | 8dd69b8f | bellard | if (!(env->hflags & HF_HALTED_MASK))
|
408 | e0fd8781 | bellard | return;
|
409 | e0fd8781 | bellard | env->eip = 0;
|
410 | e0fd8781 | bellard | cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12, |
411 | e0fd8781 | bellard | 0xffff, 0); |
412 | 8dd69b8f | bellard | env->hflags &= ~HF_HALTED_MASK; |
413 | e0fd8781 | bellard | } |
414 | e0fd8781 | bellard | |
415 | d592d303 | bellard | static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode, |
416 | d592d303 | bellard | uint8_t delivery_mode, uint8_t vector_num, |
417 | d592d303 | bellard | uint8_t polarity, uint8_t trigger_mode) |
418 | d592d303 | bellard | { |
419 | d3e9db93 | bellard | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
420 | d592d303 | bellard | int dest_shorthand = (s->icr[0] >> 18) & 3; |
421 | d592d303 | bellard | APICState *apic_iter; |
422 | d592d303 | bellard | |
423 | e0fd8781 | bellard | switch (dest_shorthand) {
|
424 | d3e9db93 | bellard | case 0: |
425 | d3e9db93 | bellard | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
426 | d3e9db93 | bellard | break;
|
427 | d3e9db93 | bellard | case 1: |
428 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask)); |
429 | d3e9db93 | bellard | set_bit(deliver_bitmask, s->id); |
430 | d3e9db93 | bellard | break;
|
431 | d3e9db93 | bellard | case 2: |
432 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
433 | d3e9db93 | bellard | break;
|
434 | d3e9db93 | bellard | case 3: |
435 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
436 | d3e9db93 | bellard | reset_bit(deliver_bitmask, s->id); |
437 | d3e9db93 | bellard | break;
|
438 | e0fd8781 | bellard | } |
439 | e0fd8781 | bellard | |
440 | d592d303 | bellard | switch (delivery_mode) {
|
441 | d592d303 | bellard | case APIC_DM_INIT:
|
442 | d592d303 | bellard | { |
443 | d592d303 | bellard | int trig_mode = (s->icr[0] >> 15) & 1; |
444 | d592d303 | bellard | int level = (s->icr[0] >> 14) & 1; |
445 | d592d303 | bellard | if (level == 0 && trig_mode == 1) { |
446 | d3e9db93 | bellard | foreach_apic(apic_iter, deliver_bitmask, |
447 | d3e9db93 | bellard | apic_iter->arb_id = apic_iter->id ); |
448 | d592d303 | bellard | return;
|
449 | d592d303 | bellard | } |
450 | d592d303 | bellard | } |
451 | d592d303 | bellard | break;
|
452 | d592d303 | bellard | |
453 | d592d303 | bellard | case APIC_DM_SIPI:
|
454 | d3e9db93 | bellard | foreach_apic(apic_iter, deliver_bitmask, |
455 | d3e9db93 | bellard | apic_startup(apic_iter, vector_num) ); |
456 | d592d303 | bellard | return;
|
457 | d592d303 | bellard | } |
458 | d592d303 | bellard | |
459 | d592d303 | bellard | apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity, |
460 | d592d303 | bellard | trigger_mode); |
461 | d592d303 | bellard | } |
462 | d592d303 | bellard | |
463 | 574bbf7b | bellard | int apic_get_interrupt(CPUState *env)
|
464 | 574bbf7b | bellard | { |
465 | 574bbf7b | bellard | APICState *s = env->apic_state; |
466 | 574bbf7b | bellard | int intno;
|
467 | 574bbf7b | bellard | |
468 | 574bbf7b | bellard | /* if the APIC is installed or enabled, we let the 8259 handle the
|
469 | 574bbf7b | bellard | IRQs */
|
470 | 574bbf7b | bellard | if (!s)
|
471 | 574bbf7b | bellard | return -1; |
472 | 574bbf7b | bellard | if (!(s->spurious_vec & APIC_SV_ENABLE))
|
473 | 574bbf7b | bellard | return -1; |
474 | 574bbf7b | bellard | |
475 | 574bbf7b | bellard | /* XXX: spurious IRQ handling */
|
476 | 574bbf7b | bellard | intno = get_highest_priority_int(s->irr); |
477 | 574bbf7b | bellard | if (intno < 0) |
478 | 574bbf7b | bellard | return -1; |
479 | d592d303 | bellard | if (s->tpr && intno <= s->tpr)
|
480 | d592d303 | bellard | return s->spurious_vec & 0xff; |
481 | b4511723 | bellard | reset_bit(s->irr, intno); |
482 | 574bbf7b | bellard | set_bit(s->isr, intno); |
483 | 574bbf7b | bellard | apic_update_irq(s); |
484 | 574bbf7b | bellard | return intno;
|
485 | 574bbf7b | bellard | } |
486 | 574bbf7b | bellard | |
487 | 574bbf7b | bellard | static uint32_t apic_get_current_count(APICState *s)
|
488 | 574bbf7b | bellard | { |
489 | 574bbf7b | bellard | int64_t d; |
490 | 574bbf7b | bellard | uint32_t val; |
491 | 574bbf7b | bellard | d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >> |
492 | 574bbf7b | bellard | s->count_shift; |
493 | 574bbf7b | bellard | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
|
494 | 574bbf7b | bellard | /* periodic */
|
495 | d592d303 | bellard | val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
|
496 | 574bbf7b | bellard | } else {
|
497 | 574bbf7b | bellard | if (d >= s->initial_count)
|
498 | 574bbf7b | bellard | val = 0;
|
499 | 574bbf7b | bellard | else
|
500 | 574bbf7b | bellard | val = s->initial_count - d; |
501 | 574bbf7b | bellard | } |
502 | 574bbf7b | bellard | return val;
|
503 | 574bbf7b | bellard | } |
504 | 574bbf7b | bellard | |
505 | 574bbf7b | bellard | static void apic_timer_update(APICState *s, int64_t current_time) |
506 | 574bbf7b | bellard | { |
507 | 574bbf7b | bellard | int64_t next_time, d; |
508 | 574bbf7b | bellard | |
509 | 574bbf7b | bellard | if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
|
510 | 574bbf7b | bellard | d = (current_time - s->initial_count_load_time) >> |
511 | 574bbf7b | bellard | s->count_shift; |
512 | 574bbf7b | bellard | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
|
513 | d592d303 | bellard | d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1); |
514 | 574bbf7b | bellard | } else {
|
515 | 574bbf7b | bellard | if (d >= s->initial_count)
|
516 | 574bbf7b | bellard | goto no_timer;
|
517 | d592d303 | bellard | d = (uint64_t)s->initial_count + 1;
|
518 | 574bbf7b | bellard | } |
519 | 574bbf7b | bellard | next_time = s->initial_count_load_time + (d << s->count_shift); |
520 | 574bbf7b | bellard | qemu_mod_timer(s->timer, next_time); |
521 | 574bbf7b | bellard | s->next_time = next_time; |
522 | 574bbf7b | bellard | } else {
|
523 | 574bbf7b | bellard | no_timer:
|
524 | 574bbf7b | bellard | qemu_del_timer(s->timer); |
525 | 574bbf7b | bellard | } |
526 | 574bbf7b | bellard | } |
527 | 574bbf7b | bellard | |
528 | 574bbf7b | bellard | static void apic_timer(void *opaque) |
529 | 574bbf7b | bellard | { |
530 | 574bbf7b | bellard | APICState *s = opaque; |
531 | 574bbf7b | bellard | |
532 | 574bbf7b | bellard | if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
|
533 | 574bbf7b | bellard | apic_set_irq(s, s->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE);
|
534 | 574bbf7b | bellard | } |
535 | 574bbf7b | bellard | apic_timer_update(s, s->next_time); |
536 | 574bbf7b | bellard | } |
537 | 574bbf7b | bellard | |
538 | 574bbf7b | bellard | static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr) |
539 | 574bbf7b | bellard | { |
540 | 574bbf7b | bellard | return 0; |
541 | 574bbf7b | bellard | } |
542 | 574bbf7b | bellard | |
543 | 574bbf7b | bellard | static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr) |
544 | 574bbf7b | bellard | { |
545 | 574bbf7b | bellard | return 0; |
546 | 574bbf7b | bellard | } |
547 | 574bbf7b | bellard | |
548 | 574bbf7b | bellard | static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
549 | 574bbf7b | bellard | { |
550 | 574bbf7b | bellard | } |
551 | 574bbf7b | bellard | |
552 | 574bbf7b | bellard | static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
553 | 574bbf7b | bellard | { |
554 | 574bbf7b | bellard | } |
555 | 574bbf7b | bellard | |
556 | 574bbf7b | bellard | static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr) |
557 | 574bbf7b | bellard | { |
558 | 574bbf7b | bellard | CPUState *env; |
559 | 574bbf7b | bellard | APICState *s; |
560 | 574bbf7b | bellard | uint32_t val; |
561 | 574bbf7b | bellard | int index;
|
562 | 574bbf7b | bellard | |
563 | 574bbf7b | bellard | env = cpu_single_env; |
564 | 574bbf7b | bellard | if (!env)
|
565 | 574bbf7b | bellard | return 0; |
566 | 574bbf7b | bellard | s = env->apic_state; |
567 | 574bbf7b | bellard | |
568 | 574bbf7b | bellard | index = (addr >> 4) & 0xff; |
569 | 574bbf7b | bellard | switch(index) {
|
570 | 574bbf7b | bellard | case 0x02: /* id */ |
571 | 574bbf7b | bellard | val = s->id << 24;
|
572 | 574bbf7b | bellard | break;
|
573 | 574bbf7b | bellard | case 0x03: /* version */ |
574 | 574bbf7b | bellard | val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */ |
575 | 574bbf7b | bellard | break;
|
576 | 574bbf7b | bellard | case 0x08: |
577 | 574bbf7b | bellard | val = s->tpr; |
578 | 574bbf7b | bellard | break;
|
579 | d592d303 | bellard | case 0x09: |
580 | d592d303 | bellard | val = apic_get_arb_pri(s); |
581 | d592d303 | bellard | break;
|
582 | 574bbf7b | bellard | case 0x0a: |
583 | 574bbf7b | bellard | /* ppr */
|
584 | 574bbf7b | bellard | val = apic_get_ppr(s); |
585 | 574bbf7b | bellard | break;
|
586 | d592d303 | bellard | case 0x0d: |
587 | d592d303 | bellard | val = s->log_dest << 24;
|
588 | d592d303 | bellard | break;
|
589 | d592d303 | bellard | case 0x0e: |
590 | d592d303 | bellard | val = s->dest_mode << 28;
|
591 | d592d303 | bellard | break;
|
592 | 574bbf7b | bellard | case 0x0f: |
593 | 574bbf7b | bellard | val = s->spurious_vec; |
594 | 574bbf7b | bellard | break;
|
595 | 574bbf7b | bellard | case 0x10 ... 0x17: |
596 | 574bbf7b | bellard | val = s->isr[index & 7];
|
597 | 574bbf7b | bellard | break;
|
598 | 574bbf7b | bellard | case 0x18 ... 0x1f: |
599 | 574bbf7b | bellard | val = s->tmr[index & 7];
|
600 | 574bbf7b | bellard | break;
|
601 | 574bbf7b | bellard | case 0x20 ... 0x27: |
602 | 574bbf7b | bellard | val = s->irr[index & 7];
|
603 | 574bbf7b | bellard | break;
|
604 | 574bbf7b | bellard | case 0x28: |
605 | 574bbf7b | bellard | val = s->esr; |
606 | 574bbf7b | bellard | break;
|
607 | 574bbf7b | bellard | case 0x30: |
608 | 574bbf7b | bellard | case 0x31: |
609 | 574bbf7b | bellard | val = s->icr[index & 1];
|
610 | 574bbf7b | bellard | break;
|
611 | e0fd8781 | bellard | case 0x32 ... 0x37: |
612 | e0fd8781 | bellard | val = s->lvt[index - 0x32];
|
613 | e0fd8781 | bellard | break;
|
614 | 574bbf7b | bellard | case 0x38: |
615 | 574bbf7b | bellard | val = s->initial_count; |
616 | 574bbf7b | bellard | break;
|
617 | 574bbf7b | bellard | case 0x39: |
618 | 574bbf7b | bellard | val = apic_get_current_count(s); |
619 | 574bbf7b | bellard | break;
|
620 | 574bbf7b | bellard | case 0x3e: |
621 | 574bbf7b | bellard | val = s->divide_conf; |
622 | 574bbf7b | bellard | break;
|
623 | 574bbf7b | bellard | default:
|
624 | 574bbf7b | bellard | s->esr |= ESR_ILLEGAL_ADDRESS; |
625 | 574bbf7b | bellard | val = 0;
|
626 | 574bbf7b | bellard | break;
|
627 | 574bbf7b | bellard | } |
628 | 574bbf7b | bellard | #ifdef DEBUG_APIC
|
629 | 574bbf7b | bellard | printf("APIC read: %08x = %08x\n", (uint32_t)addr, val);
|
630 | 574bbf7b | bellard | #endif
|
631 | 574bbf7b | bellard | return val;
|
632 | 574bbf7b | bellard | } |
633 | 574bbf7b | bellard | |
634 | 574bbf7b | bellard | static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
635 | 574bbf7b | bellard | { |
636 | 574bbf7b | bellard | CPUState *env; |
637 | 574bbf7b | bellard | APICState *s; |
638 | 574bbf7b | bellard | int index;
|
639 | 574bbf7b | bellard | |
640 | 574bbf7b | bellard | env = cpu_single_env; |
641 | 574bbf7b | bellard | if (!env)
|
642 | 574bbf7b | bellard | return;
|
643 | 574bbf7b | bellard | s = env->apic_state; |
644 | 574bbf7b | bellard | |
645 | 574bbf7b | bellard | #ifdef DEBUG_APIC
|
646 | 574bbf7b | bellard | printf("APIC write: %08x = %08x\n", (uint32_t)addr, val);
|
647 | 574bbf7b | bellard | #endif
|
648 | 574bbf7b | bellard | |
649 | 574bbf7b | bellard | index = (addr >> 4) & 0xff; |
650 | 574bbf7b | bellard | switch(index) {
|
651 | 574bbf7b | bellard | case 0x02: |
652 | 574bbf7b | bellard | s->id = (val >> 24);
|
653 | 574bbf7b | bellard | break;
|
654 | e0fd8781 | bellard | case 0x03: |
655 | e0fd8781 | bellard | break;
|
656 | 574bbf7b | bellard | case 0x08: |
657 | 574bbf7b | bellard | s->tpr = val; |
658 | d592d303 | bellard | apic_update_irq(s); |
659 | 574bbf7b | bellard | break;
|
660 | e0fd8781 | bellard | case 0x09: |
661 | e0fd8781 | bellard | case 0x0a: |
662 | e0fd8781 | bellard | break;
|
663 | 574bbf7b | bellard | case 0x0b: /* EOI */ |
664 | 574bbf7b | bellard | apic_eoi(s); |
665 | 574bbf7b | bellard | break;
|
666 | d592d303 | bellard | case 0x0d: |
667 | d592d303 | bellard | s->log_dest = val >> 24;
|
668 | d592d303 | bellard | break;
|
669 | d592d303 | bellard | case 0x0e: |
670 | d592d303 | bellard | s->dest_mode = val >> 28;
|
671 | d592d303 | bellard | break;
|
672 | 574bbf7b | bellard | case 0x0f: |
673 | 574bbf7b | bellard | s->spurious_vec = val & 0x1ff;
|
674 | d592d303 | bellard | apic_update_irq(s); |
675 | 574bbf7b | bellard | break;
|
676 | e0fd8781 | bellard | case 0x10 ... 0x17: |
677 | e0fd8781 | bellard | case 0x18 ... 0x1f: |
678 | e0fd8781 | bellard | case 0x20 ... 0x27: |
679 | e0fd8781 | bellard | case 0x28: |
680 | e0fd8781 | bellard | break;
|
681 | 574bbf7b | bellard | case 0x30: |
682 | d592d303 | bellard | s->icr[0] = val;
|
683 | d592d303 | bellard | apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1, |
684 | d592d303 | bellard | (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff), |
685 | d592d303 | bellard | (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1); |
686 | d592d303 | bellard | break;
|
687 | 574bbf7b | bellard | case 0x31: |
688 | d592d303 | bellard | s->icr[1] = val;
|
689 | 574bbf7b | bellard | break;
|
690 | 574bbf7b | bellard | case 0x32 ... 0x37: |
691 | 574bbf7b | bellard | { |
692 | 574bbf7b | bellard | int n = index - 0x32; |
693 | 574bbf7b | bellard | s->lvt[n] = val; |
694 | 574bbf7b | bellard | if (n == APIC_LVT_TIMER)
|
695 | 574bbf7b | bellard | apic_timer_update(s, qemu_get_clock(vm_clock)); |
696 | 574bbf7b | bellard | } |
697 | 574bbf7b | bellard | break;
|
698 | 574bbf7b | bellard | case 0x38: |
699 | 574bbf7b | bellard | s->initial_count = val; |
700 | 574bbf7b | bellard | s->initial_count_load_time = qemu_get_clock(vm_clock); |
701 | 574bbf7b | bellard | apic_timer_update(s, s->initial_count_load_time); |
702 | 574bbf7b | bellard | break;
|
703 | e0fd8781 | bellard | case 0x39: |
704 | e0fd8781 | bellard | break;
|
705 | 574bbf7b | bellard | case 0x3e: |
706 | 574bbf7b | bellard | { |
707 | 574bbf7b | bellard | int v;
|
708 | 574bbf7b | bellard | s->divide_conf = val & 0xb;
|
709 | 574bbf7b | bellard | v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4); |
710 | 574bbf7b | bellard | s->count_shift = (v + 1) & 7; |
711 | 574bbf7b | bellard | } |
712 | 574bbf7b | bellard | break;
|
713 | 574bbf7b | bellard | default:
|
714 | 574bbf7b | bellard | s->esr |= ESR_ILLEGAL_ADDRESS; |
715 | 574bbf7b | bellard | break;
|
716 | 574bbf7b | bellard | } |
717 | 574bbf7b | bellard | } |
718 | 574bbf7b | bellard | |
719 | d592d303 | bellard | static void apic_save(QEMUFile *f, void *opaque) |
720 | d592d303 | bellard | { |
721 | d592d303 | bellard | APICState *s = opaque; |
722 | d592d303 | bellard | int i;
|
723 | d592d303 | bellard | |
724 | d592d303 | bellard | qemu_put_be32s(f, &s->apicbase); |
725 | d592d303 | bellard | qemu_put_8s(f, &s->id); |
726 | d592d303 | bellard | qemu_put_8s(f, &s->arb_id); |
727 | d592d303 | bellard | qemu_put_8s(f, &s->tpr); |
728 | d592d303 | bellard | qemu_put_be32s(f, &s->spurious_vec); |
729 | d592d303 | bellard | qemu_put_8s(f, &s->log_dest); |
730 | d592d303 | bellard | qemu_put_8s(f, &s->dest_mode); |
731 | d592d303 | bellard | for (i = 0; i < 8; i++) { |
732 | d592d303 | bellard | qemu_put_be32s(f, &s->isr[i]); |
733 | d592d303 | bellard | qemu_put_be32s(f, &s->tmr[i]); |
734 | d592d303 | bellard | qemu_put_be32s(f, &s->irr[i]); |
735 | d592d303 | bellard | } |
736 | d592d303 | bellard | for (i = 0; i < APIC_LVT_NB; i++) { |
737 | d592d303 | bellard | qemu_put_be32s(f, &s->lvt[i]); |
738 | d592d303 | bellard | } |
739 | d592d303 | bellard | qemu_put_be32s(f, &s->esr); |
740 | d592d303 | bellard | qemu_put_be32s(f, &s->icr[0]);
|
741 | d592d303 | bellard | qemu_put_be32s(f, &s->icr[1]);
|
742 | d592d303 | bellard | qemu_put_be32s(f, &s->divide_conf); |
743 | d592d303 | bellard | qemu_put_be32s(f, &s->count_shift); |
744 | d592d303 | bellard | qemu_put_be32s(f, &s->initial_count); |
745 | d592d303 | bellard | qemu_put_be64s(f, &s->initial_count_load_time); |
746 | d592d303 | bellard | qemu_put_be64s(f, &s->next_time); |
747 | e6cf6a8c | bellard | |
748 | e6cf6a8c | bellard | qemu_put_timer(f, s->timer); |
749 | d592d303 | bellard | } |
750 | d592d303 | bellard | |
751 | d592d303 | bellard | static int apic_load(QEMUFile *f, void *opaque, int version_id) |
752 | d592d303 | bellard | { |
753 | d592d303 | bellard | APICState *s = opaque; |
754 | d592d303 | bellard | int i;
|
755 | d592d303 | bellard | |
756 | e6cf6a8c | bellard | if (version_id > 2) |
757 | d592d303 | bellard | return -EINVAL;
|
758 | d592d303 | bellard | |
759 | d592d303 | bellard | /* XXX: what if the base changes? (registered memory regions) */
|
760 | d592d303 | bellard | qemu_get_be32s(f, &s->apicbase); |
761 | d592d303 | bellard | qemu_get_8s(f, &s->id); |
762 | d592d303 | bellard | qemu_get_8s(f, &s->arb_id); |
763 | d592d303 | bellard | qemu_get_8s(f, &s->tpr); |
764 | d592d303 | bellard | qemu_get_be32s(f, &s->spurious_vec); |
765 | d592d303 | bellard | qemu_get_8s(f, &s->log_dest); |
766 | d592d303 | bellard | qemu_get_8s(f, &s->dest_mode); |
767 | d592d303 | bellard | for (i = 0; i < 8; i++) { |
768 | d592d303 | bellard | qemu_get_be32s(f, &s->isr[i]); |
769 | d592d303 | bellard | qemu_get_be32s(f, &s->tmr[i]); |
770 | d592d303 | bellard | qemu_get_be32s(f, &s->irr[i]); |
771 | d592d303 | bellard | } |
772 | d592d303 | bellard | for (i = 0; i < APIC_LVT_NB; i++) { |
773 | d592d303 | bellard | qemu_get_be32s(f, &s->lvt[i]); |
774 | d592d303 | bellard | } |
775 | d592d303 | bellard | qemu_get_be32s(f, &s->esr); |
776 | d592d303 | bellard | qemu_get_be32s(f, &s->icr[0]);
|
777 | d592d303 | bellard | qemu_get_be32s(f, &s->icr[1]);
|
778 | d592d303 | bellard | qemu_get_be32s(f, &s->divide_conf); |
779 | d592d303 | bellard | qemu_get_be32s(f, &s->count_shift); |
780 | d592d303 | bellard | qemu_get_be32s(f, &s->initial_count); |
781 | d592d303 | bellard | qemu_get_be64s(f, &s->initial_count_load_time); |
782 | d592d303 | bellard | qemu_get_be64s(f, &s->next_time); |
783 | e6cf6a8c | bellard | |
784 | e6cf6a8c | bellard | if (version_id >= 2) |
785 | e6cf6a8c | bellard | qemu_get_timer(f, s->timer); |
786 | d592d303 | bellard | return 0; |
787 | d592d303 | bellard | } |
788 | 574bbf7b | bellard | |
789 | d592d303 | bellard | static void apic_reset(void *opaque) |
790 | d592d303 | bellard | { |
791 | d592d303 | bellard | APICState *s = opaque; |
792 | d592d303 | bellard | apic_init_ipi(s); |
793 | d592d303 | bellard | } |
794 | 574bbf7b | bellard | |
795 | 574bbf7b | bellard | static CPUReadMemoryFunc *apic_mem_read[3] = { |
796 | 574bbf7b | bellard | apic_mem_readb, |
797 | 574bbf7b | bellard | apic_mem_readw, |
798 | 574bbf7b | bellard | apic_mem_readl, |
799 | 574bbf7b | bellard | }; |
800 | 574bbf7b | bellard | |
801 | 574bbf7b | bellard | static CPUWriteMemoryFunc *apic_mem_write[3] = { |
802 | 574bbf7b | bellard | apic_mem_writeb, |
803 | 574bbf7b | bellard | apic_mem_writew, |
804 | 574bbf7b | bellard | apic_mem_writel, |
805 | 574bbf7b | bellard | }; |
806 | 574bbf7b | bellard | |
807 | 574bbf7b | bellard | int apic_init(CPUState *env)
|
808 | 574bbf7b | bellard | { |
809 | 574bbf7b | bellard | APICState *s; |
810 | 574bbf7b | bellard | |
811 | d3e9db93 | bellard | if (last_apic_id >= MAX_APICS)
|
812 | d3e9db93 | bellard | return -1; |
813 | d592d303 | bellard | s = qemu_mallocz(sizeof(APICState));
|
814 | 574bbf7b | bellard | if (!s)
|
815 | 574bbf7b | bellard | return -1; |
816 | 574bbf7b | bellard | env->apic_state = s; |
817 | d592d303 | bellard | apic_init_ipi(s); |
818 | d592d303 | bellard | s->id = last_apic_id++; |
819 | 574bbf7b | bellard | s->cpu_env = env; |
820 | 574bbf7b | bellard | s->apicbase = 0xfee00000 |
|
821 | d592d303 | bellard | (s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
|
822 | 574bbf7b | bellard | |
823 | d592d303 | bellard | /* XXX: mapping more APICs at the same memory location */
|
824 | 574bbf7b | bellard | if (apic_io_memory == 0) { |
825 | 574bbf7b | bellard | /* NOTE: the APIC is directly connected to the CPU - it is not
|
826 | 574bbf7b | bellard | on the global memory bus. */
|
827 | 574bbf7b | bellard | apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
|
828 | 574bbf7b | bellard | apic_mem_write, NULL);
|
829 | d592d303 | bellard | cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000, |
830 | d592d303 | bellard | apic_io_memory); |
831 | 574bbf7b | bellard | } |
832 | 574bbf7b | bellard | s->timer = qemu_new_timer(vm_clock, apic_timer, s); |
833 | d592d303 | bellard | |
834 | d592d303 | bellard | register_savevm("apic", 0, 1, apic_save, apic_load, s); |
835 | d592d303 | bellard | qemu_register_reset(apic_reset, s); |
836 | d592d303 | bellard | |
837 | d3e9db93 | bellard | local_apics[s->id] = s; |
838 | d592d303 | bellard | return 0; |
839 | d592d303 | bellard | } |
840 | d592d303 | bellard | |
841 | d592d303 | bellard | static void ioapic_service(IOAPICState *s) |
842 | d592d303 | bellard | { |
843 | b1fc0348 | bellard | uint8_t i; |
844 | b1fc0348 | bellard | uint8_t trig_mode; |
845 | d592d303 | bellard | uint8_t vector; |
846 | b1fc0348 | bellard | uint8_t delivery_mode; |
847 | d592d303 | bellard | uint32_t mask; |
848 | d592d303 | bellard | uint64_t entry; |
849 | d592d303 | bellard | uint8_t dest; |
850 | d592d303 | bellard | uint8_t dest_mode; |
851 | b1fc0348 | bellard | uint8_t polarity; |
852 | d3e9db93 | bellard | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
853 | d592d303 | bellard | |
854 | b1fc0348 | bellard | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
855 | b1fc0348 | bellard | mask = 1 << i;
|
856 | d592d303 | bellard | if (s->irr & mask) {
|
857 | b1fc0348 | bellard | entry = s->ioredtbl[i]; |
858 | d592d303 | bellard | if (!(entry & APIC_LVT_MASKED)) {
|
859 | b1fc0348 | bellard | trig_mode = ((entry >> 15) & 1); |
860 | d592d303 | bellard | dest = entry >> 56;
|
861 | d592d303 | bellard | dest_mode = (entry >> 11) & 1; |
862 | b1fc0348 | bellard | delivery_mode = (entry >> 8) & 7; |
863 | b1fc0348 | bellard | polarity = (entry >> 13) & 1; |
864 | b1fc0348 | bellard | if (trig_mode == APIC_TRIGGER_EDGE)
|
865 | b1fc0348 | bellard | s->irr &= ~mask; |
866 | b1fc0348 | bellard | if (delivery_mode == APIC_DM_EXTINT)
|
867 | b1fc0348 | bellard | vector = pic_read_irq(isa_pic); |
868 | b1fc0348 | bellard | else
|
869 | b1fc0348 | bellard | vector = entry & 0xff;
|
870 | d3e9db93 | bellard | |
871 | d3e9db93 | bellard | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
872 | d3e9db93 | bellard | apic_bus_deliver(deliver_bitmask, delivery_mode, |
873 | d3e9db93 | bellard | vector, polarity, trig_mode); |
874 | d592d303 | bellard | } |
875 | d592d303 | bellard | } |
876 | d592d303 | bellard | } |
877 | d592d303 | bellard | } |
878 | d592d303 | bellard | |
879 | d592d303 | bellard | void ioapic_set_irq(void *opaque, int vector, int level) |
880 | d592d303 | bellard | { |
881 | d592d303 | bellard | IOAPICState *s = opaque; |
882 | d592d303 | bellard | |
883 | d592d303 | bellard | if (vector >= 0 && vector < IOAPIC_NUM_PINS) { |
884 | d592d303 | bellard | uint32_t mask = 1 << vector;
|
885 | d592d303 | bellard | uint64_t entry = s->ioredtbl[vector]; |
886 | d592d303 | bellard | |
887 | d592d303 | bellard | if ((entry >> 15) & 1) { |
888 | d592d303 | bellard | /* level triggered */
|
889 | d592d303 | bellard | if (level) {
|
890 | d592d303 | bellard | s->irr |= mask; |
891 | d592d303 | bellard | ioapic_service(s); |
892 | d592d303 | bellard | } else {
|
893 | d592d303 | bellard | s->irr &= ~mask; |
894 | d592d303 | bellard | } |
895 | d592d303 | bellard | } else {
|
896 | d592d303 | bellard | /* edge triggered */
|
897 | d592d303 | bellard | if (level) {
|
898 | d592d303 | bellard | s->irr |= mask; |
899 | d592d303 | bellard | ioapic_service(s); |
900 | d592d303 | bellard | } |
901 | d592d303 | bellard | } |
902 | d592d303 | bellard | } |
903 | d592d303 | bellard | } |
904 | d592d303 | bellard | |
905 | d592d303 | bellard | static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr) |
906 | d592d303 | bellard | { |
907 | d592d303 | bellard | IOAPICState *s = opaque; |
908 | d592d303 | bellard | int index;
|
909 | d592d303 | bellard | uint32_t val = 0;
|
910 | d592d303 | bellard | |
911 | d592d303 | bellard | addr &= 0xff;
|
912 | d592d303 | bellard | if (addr == 0x00) { |
913 | d592d303 | bellard | val = s->ioregsel; |
914 | d592d303 | bellard | } else if (addr == 0x10) { |
915 | d592d303 | bellard | switch (s->ioregsel) {
|
916 | d592d303 | bellard | case 0x00: |
917 | d592d303 | bellard | val = s->id << 24;
|
918 | d592d303 | bellard | break;
|
919 | d592d303 | bellard | case 0x01: |
920 | d592d303 | bellard | val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */ |
921 | d592d303 | bellard | break;
|
922 | d592d303 | bellard | case 0x02: |
923 | d592d303 | bellard | val = 0;
|
924 | d592d303 | bellard | break;
|
925 | d592d303 | bellard | default:
|
926 | d592d303 | bellard | index = (s->ioregsel - 0x10) >> 1; |
927 | d592d303 | bellard | if (index >= 0 && index < IOAPIC_NUM_PINS) { |
928 | d592d303 | bellard | if (s->ioregsel & 1) |
929 | d592d303 | bellard | val = s->ioredtbl[index] >> 32;
|
930 | d592d303 | bellard | else
|
931 | d592d303 | bellard | val = s->ioredtbl[index] & 0xffffffff;
|
932 | d592d303 | bellard | } |
933 | d592d303 | bellard | } |
934 | d592d303 | bellard | #ifdef DEBUG_IOAPIC
|
935 | d592d303 | bellard | printf("I/O APIC read: %08x = %08x\n", s->ioregsel, val);
|
936 | d592d303 | bellard | #endif
|
937 | d592d303 | bellard | } |
938 | d592d303 | bellard | return val;
|
939 | d592d303 | bellard | } |
940 | d592d303 | bellard | |
941 | d592d303 | bellard | static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
942 | d592d303 | bellard | { |
943 | d592d303 | bellard | IOAPICState *s = opaque; |
944 | d592d303 | bellard | int index;
|
945 | d592d303 | bellard | |
946 | d592d303 | bellard | addr &= 0xff;
|
947 | d592d303 | bellard | if (addr == 0x00) { |
948 | d592d303 | bellard | s->ioregsel = val; |
949 | d592d303 | bellard | return;
|
950 | d592d303 | bellard | } else if (addr == 0x10) { |
951 | d592d303 | bellard | #ifdef DEBUG_IOAPIC
|
952 | d592d303 | bellard | printf("I/O APIC write: %08x = %08x\n", s->ioregsel, val);
|
953 | d592d303 | bellard | #endif
|
954 | d592d303 | bellard | switch (s->ioregsel) {
|
955 | d592d303 | bellard | case 0x00: |
956 | d592d303 | bellard | s->id = (val >> 24) & 0xff; |
957 | d592d303 | bellard | return;
|
958 | d592d303 | bellard | case 0x01: |
959 | d592d303 | bellard | case 0x02: |
960 | d592d303 | bellard | return;
|
961 | d592d303 | bellard | default:
|
962 | d592d303 | bellard | index = (s->ioregsel - 0x10) >> 1; |
963 | d592d303 | bellard | if (index >= 0 && index < IOAPIC_NUM_PINS) { |
964 | d592d303 | bellard | if (s->ioregsel & 1) { |
965 | d592d303 | bellard | s->ioredtbl[index] &= 0xffffffff;
|
966 | d592d303 | bellard | s->ioredtbl[index] |= (uint64_t)val << 32;
|
967 | d592d303 | bellard | } else {
|
968 | d592d303 | bellard | s->ioredtbl[index] &= ~0xffffffffULL;
|
969 | d592d303 | bellard | s->ioredtbl[index] |= val; |
970 | d592d303 | bellard | } |
971 | d592d303 | bellard | ioapic_service(s); |
972 | d592d303 | bellard | } |
973 | d592d303 | bellard | } |
974 | d592d303 | bellard | } |
975 | d592d303 | bellard | } |
976 | d592d303 | bellard | |
977 | d592d303 | bellard | static void ioapic_save(QEMUFile *f, void *opaque) |
978 | d592d303 | bellard | { |
979 | d592d303 | bellard | IOAPICState *s = opaque; |
980 | d592d303 | bellard | int i;
|
981 | d592d303 | bellard | |
982 | d592d303 | bellard | qemu_put_8s(f, &s->id); |
983 | d592d303 | bellard | qemu_put_8s(f, &s->ioregsel); |
984 | d592d303 | bellard | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
985 | d592d303 | bellard | qemu_put_be64s(f, &s->ioredtbl[i]); |
986 | d592d303 | bellard | } |
987 | d592d303 | bellard | } |
988 | d592d303 | bellard | |
989 | d592d303 | bellard | static int ioapic_load(QEMUFile *f, void *opaque, int version_id) |
990 | d592d303 | bellard | { |
991 | d592d303 | bellard | IOAPICState *s = opaque; |
992 | d592d303 | bellard | int i;
|
993 | d592d303 | bellard | |
994 | d592d303 | bellard | if (version_id != 1) |
995 | d592d303 | bellard | return -EINVAL;
|
996 | d592d303 | bellard | |
997 | d592d303 | bellard | qemu_get_8s(f, &s->id); |
998 | d592d303 | bellard | qemu_get_8s(f, &s->ioregsel); |
999 | d592d303 | bellard | for (i = 0; i < IOAPIC_NUM_PINS; i++) { |
1000 | d592d303 | bellard | qemu_get_be64s(f, &s->ioredtbl[i]); |
1001 | d592d303 | bellard | } |
1002 | 574bbf7b | bellard | return 0; |
1003 | 574bbf7b | bellard | } |
1004 | d592d303 | bellard | |
1005 | d592d303 | bellard | static void ioapic_reset(void *opaque) |
1006 | d592d303 | bellard | { |
1007 | d592d303 | bellard | IOAPICState *s = opaque; |
1008 | d592d303 | bellard | int i;
|
1009 | d592d303 | bellard | |
1010 | d592d303 | bellard | memset(s, 0, sizeof(*s)); |
1011 | d592d303 | bellard | for(i = 0; i < IOAPIC_NUM_PINS; i++) |
1012 | d592d303 | bellard | s->ioredtbl[i] = 1 << 16; /* mask LVT */ |
1013 | d592d303 | bellard | } |
1014 | d592d303 | bellard | |
1015 | d592d303 | bellard | static CPUReadMemoryFunc *ioapic_mem_read[3] = { |
1016 | d592d303 | bellard | ioapic_mem_readl, |
1017 | d592d303 | bellard | ioapic_mem_readl, |
1018 | d592d303 | bellard | ioapic_mem_readl, |
1019 | d592d303 | bellard | }; |
1020 | d592d303 | bellard | |
1021 | d592d303 | bellard | static CPUWriteMemoryFunc *ioapic_mem_write[3] = { |
1022 | d592d303 | bellard | ioapic_mem_writel, |
1023 | d592d303 | bellard | ioapic_mem_writel, |
1024 | d592d303 | bellard | ioapic_mem_writel, |
1025 | d592d303 | bellard | }; |
1026 | d592d303 | bellard | |
1027 | d592d303 | bellard | IOAPICState *ioapic_init(void)
|
1028 | d592d303 | bellard | { |
1029 | d592d303 | bellard | IOAPICState *s; |
1030 | d592d303 | bellard | int io_memory;
|
1031 | d592d303 | bellard | |
1032 | b1fc0348 | bellard | s = qemu_mallocz(sizeof(IOAPICState));
|
1033 | d592d303 | bellard | if (!s)
|
1034 | d592d303 | bellard | return NULL; |
1035 | d592d303 | bellard | ioapic_reset(s); |
1036 | d592d303 | bellard | s->id = last_apic_id++; |
1037 | d592d303 | bellard | |
1038 | d592d303 | bellard | io_memory = cpu_register_io_memory(0, ioapic_mem_read,
|
1039 | d592d303 | bellard | ioapic_mem_write, s); |
1040 | d592d303 | bellard | cpu_register_physical_memory(0xfec00000, 0x1000, io_memory); |
1041 | d592d303 | bellard | |
1042 | d592d303 | bellard | register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s); |
1043 | d592d303 | bellard | qemu_register_reset(ioapic_reset, s); |
1044 | d592d303 | bellard | |
1045 | d592d303 | bellard | return s;
|
1046 | d592d303 | bellard | } |