root / hw / apic.c @ d8023f31
History | View | Annotate | Download (26.2 kB)
1 | 574bbf7b | bellard | /*
|
---|---|---|---|
2 | 574bbf7b | bellard | * APIC support
|
3 | 5fafdf24 | ths | *
|
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 | 8167ee88 | Blue Swirl | * License along with this library; if not, see <http://www.gnu.org/licenses/>
|
18 | 574bbf7b | bellard | */
|
19 | 87ecb68b | pbrook | #include "hw.h" |
20 | aa28b9bf | Blue Swirl | #include "apic.h" |
21 | 87ecb68b | pbrook | #include "qemu-timer.h" |
22 | bb7e7293 | aurel32 | #include "host-utils.h" |
23 | 8546b099 | Blue Swirl | #include "sysbus.h" |
24 | d8023f31 | Blue Swirl | #include "trace.h" |
25 | 574bbf7b | bellard | |
26 | 574bbf7b | bellard | /* APIC Local Vector Table */
|
27 | 574bbf7b | bellard | #define APIC_LVT_TIMER 0 |
28 | 574bbf7b | bellard | #define APIC_LVT_THERMAL 1 |
29 | 574bbf7b | bellard | #define APIC_LVT_PERFORM 2 |
30 | 574bbf7b | bellard | #define APIC_LVT_LINT0 3 |
31 | 574bbf7b | bellard | #define APIC_LVT_LINT1 4 |
32 | 574bbf7b | bellard | #define APIC_LVT_ERROR 5 |
33 | 574bbf7b | bellard | #define APIC_LVT_NB 6 |
34 | 574bbf7b | bellard | |
35 | 574bbf7b | bellard | /* APIC delivery modes */
|
36 | 574bbf7b | bellard | #define APIC_DM_FIXED 0 |
37 | 574bbf7b | bellard | #define APIC_DM_LOWPRI 1 |
38 | 574bbf7b | bellard | #define APIC_DM_SMI 2 |
39 | 574bbf7b | bellard | #define APIC_DM_NMI 4 |
40 | 574bbf7b | bellard | #define APIC_DM_INIT 5 |
41 | 574bbf7b | bellard | #define APIC_DM_SIPI 6 |
42 | 574bbf7b | bellard | #define APIC_DM_EXTINT 7 |
43 | 574bbf7b | bellard | |
44 | d592d303 | bellard | /* APIC destination mode */
|
45 | d592d303 | bellard | #define APIC_DESTMODE_FLAT 0xf |
46 | d592d303 | bellard | #define APIC_DESTMODE_CLUSTER 1 |
47 | d592d303 | bellard | |
48 | 574bbf7b | bellard | #define APIC_TRIGGER_EDGE 0 |
49 | 574bbf7b | bellard | #define APIC_TRIGGER_LEVEL 1 |
50 | 574bbf7b | bellard | |
51 | 574bbf7b | bellard | #define APIC_LVT_TIMER_PERIODIC (1<<17) |
52 | 574bbf7b | bellard | #define APIC_LVT_MASKED (1<<16) |
53 | 574bbf7b | bellard | #define APIC_LVT_LEVEL_TRIGGER (1<<15) |
54 | 574bbf7b | bellard | #define APIC_LVT_REMOTE_IRR (1<<14) |
55 | 574bbf7b | bellard | #define APIC_INPUT_POLARITY (1<<13) |
56 | 574bbf7b | bellard | #define APIC_SEND_PENDING (1<<12) |
57 | 574bbf7b | bellard | |
58 | 574bbf7b | bellard | #define ESR_ILLEGAL_ADDRESS (1 << 7) |
59 | 574bbf7b | bellard | |
60 | 574bbf7b | bellard | #define APIC_SV_ENABLE (1 << 8) |
61 | 574bbf7b | bellard | |
62 | d3e9db93 | bellard | #define MAX_APICS 255 |
63 | d3e9db93 | bellard | #define MAX_APIC_WORDS 8 |
64 | d3e9db93 | bellard | |
65 | 54c96da7 | Michael S. Tsirkin | /* Intel APIC constants: from include/asm/msidef.h */
|
66 | 54c96da7 | Michael S. Tsirkin | #define MSI_DATA_VECTOR_SHIFT 0 |
67 | 54c96da7 | Michael S. Tsirkin | #define MSI_DATA_VECTOR_MASK 0x000000ff |
68 | 54c96da7 | Michael S. Tsirkin | #define MSI_DATA_DELIVERY_MODE_SHIFT 8 |
69 | 54c96da7 | Michael S. Tsirkin | #define MSI_DATA_TRIGGER_SHIFT 15 |
70 | 54c96da7 | Michael S. Tsirkin | #define MSI_DATA_LEVEL_SHIFT 14 |
71 | 54c96da7 | Michael S. Tsirkin | #define MSI_ADDR_DEST_MODE_SHIFT 2 |
72 | 54c96da7 | Michael S. Tsirkin | #define MSI_ADDR_DEST_ID_SHIFT 12 |
73 | 54c96da7 | Michael S. Tsirkin | #define MSI_ADDR_DEST_ID_MASK 0x00ffff0 |
74 | 54c96da7 | Michael S. Tsirkin | |
75 | 54c96da7 | Michael S. Tsirkin | #define MSI_ADDR_SIZE 0x100000 |
76 | 54c96da7 | Michael S. Tsirkin | |
77 | 92a16d7a | Blue Swirl | typedef struct APICState APICState; |
78 | 92a16d7a | Blue Swirl | |
79 | cf6d64bf | Blue Swirl | struct APICState {
|
80 | 8546b099 | Blue Swirl | SysBusDevice busdev; |
81 | 8546b099 | Blue Swirl | void *cpu_env;
|
82 | 574bbf7b | bellard | uint32_t apicbase; |
83 | 574bbf7b | bellard | uint8_t id; |
84 | d592d303 | bellard | uint8_t arb_id; |
85 | 574bbf7b | bellard | uint8_t tpr; |
86 | 574bbf7b | bellard | uint32_t spurious_vec; |
87 | d592d303 | bellard | uint8_t log_dest; |
88 | d592d303 | bellard | uint8_t dest_mode; |
89 | 574bbf7b | bellard | uint32_t isr[8]; /* in service register */ |
90 | 574bbf7b | bellard | uint32_t tmr[8]; /* trigger mode register */ |
91 | 574bbf7b | bellard | uint32_t irr[8]; /* interrupt request register */ |
92 | 574bbf7b | bellard | uint32_t lvt[APIC_LVT_NB]; |
93 | 574bbf7b | bellard | uint32_t esr; /* error register */
|
94 | 574bbf7b | bellard | uint32_t icr[2];
|
95 | 574bbf7b | bellard | |
96 | 574bbf7b | bellard | uint32_t divide_conf; |
97 | 574bbf7b | bellard | int count_shift;
|
98 | 574bbf7b | bellard | uint32_t initial_count; |
99 | 574bbf7b | bellard | int64_t initial_count_load_time, next_time; |
100 | 678e12cc | Gleb Natapov | uint32_t idx; |
101 | 574bbf7b | bellard | QEMUTimer *timer; |
102 | b09ea7d5 | Gleb Natapov | int sipi_vector;
|
103 | b09ea7d5 | Gleb Natapov | int wait_for_sipi;
|
104 | cf6d64bf | Blue Swirl | }; |
105 | 574bbf7b | bellard | |
106 | d3e9db93 | bellard | static APICState *local_apics[MAX_APICS + 1]; |
107 | 73822ec8 | aliguori | static int apic_irq_delivered; |
108 | 73822ec8 | aliguori | |
109 | d592d303 | bellard | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode); |
110 | d592d303 | bellard | static void apic_update_irq(APICState *s); |
111 | 610626af | aliguori | static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask, |
112 | 610626af | aliguori | uint8_t dest, uint8_t dest_mode); |
113 | d592d303 | bellard | |
114 | 3b63c04e | aurel32 | /* Find first bit starting from msb */
|
115 | 3b63c04e | aurel32 | static int fls_bit(uint32_t value) |
116 | 3b63c04e | aurel32 | { |
117 | 3b63c04e | aurel32 | return 31 - clz32(value); |
118 | 3b63c04e | aurel32 | } |
119 | 3b63c04e | aurel32 | |
120 | e95f5491 | aurel32 | /* Find first bit starting from lsb */
|
121 | d3e9db93 | bellard | static int ffs_bit(uint32_t value) |
122 | d3e9db93 | bellard | { |
123 | bb7e7293 | aurel32 | return ctz32(value);
|
124 | d3e9db93 | bellard | } |
125 | d3e9db93 | bellard | |
126 | d3e9db93 | bellard | static inline void set_bit(uint32_t *tab, int index) |
127 | d3e9db93 | bellard | { |
128 | d3e9db93 | bellard | int i, mask;
|
129 | d3e9db93 | bellard | i = index >> 5;
|
130 | d3e9db93 | bellard | mask = 1 << (index & 0x1f); |
131 | d3e9db93 | bellard | tab[i] |= mask; |
132 | d3e9db93 | bellard | } |
133 | d3e9db93 | bellard | |
134 | d3e9db93 | bellard | static inline void reset_bit(uint32_t *tab, int index) |
135 | d3e9db93 | bellard | { |
136 | d3e9db93 | bellard | int i, mask;
|
137 | d3e9db93 | bellard | i = index >> 5;
|
138 | d3e9db93 | bellard | mask = 1 << (index & 0x1f); |
139 | d3e9db93 | bellard | tab[i] &= ~mask; |
140 | d3e9db93 | bellard | } |
141 | d3e9db93 | bellard | |
142 | 73822ec8 | aliguori | static inline int get_bit(uint32_t *tab, int index) |
143 | 73822ec8 | aliguori | { |
144 | 73822ec8 | aliguori | int i, mask;
|
145 | 73822ec8 | aliguori | i = index >> 5;
|
146 | 73822ec8 | aliguori | mask = 1 << (index & 0x1f); |
147 | 73822ec8 | aliguori | return !!(tab[i] & mask);
|
148 | 73822ec8 | aliguori | } |
149 | 73822ec8 | aliguori | |
150 | cf6d64bf | Blue Swirl | static void apic_local_deliver(APICState *s, int vector) |
151 | a5b38b51 | aurel32 | { |
152 | a5b38b51 | aurel32 | uint32_t lvt = s->lvt[vector]; |
153 | a5b38b51 | aurel32 | int trigger_mode;
|
154 | a5b38b51 | aurel32 | |
155 | d8023f31 | Blue Swirl | trace_apic_local_deliver(vector, (lvt >> 8) & 7); |
156 | d8023f31 | Blue Swirl | |
157 | a5b38b51 | aurel32 | if (lvt & APIC_LVT_MASKED)
|
158 | a5b38b51 | aurel32 | return;
|
159 | a5b38b51 | aurel32 | |
160 | a5b38b51 | aurel32 | switch ((lvt >> 8) & 7) { |
161 | a5b38b51 | aurel32 | case APIC_DM_SMI:
|
162 | cf6d64bf | Blue Swirl | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI); |
163 | a5b38b51 | aurel32 | break;
|
164 | a5b38b51 | aurel32 | |
165 | a5b38b51 | aurel32 | case APIC_DM_NMI:
|
166 | cf6d64bf | Blue Swirl | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI); |
167 | a5b38b51 | aurel32 | break;
|
168 | a5b38b51 | aurel32 | |
169 | a5b38b51 | aurel32 | case APIC_DM_EXTINT:
|
170 | cf6d64bf | Blue Swirl | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
171 | a5b38b51 | aurel32 | break;
|
172 | a5b38b51 | aurel32 | |
173 | a5b38b51 | aurel32 | case APIC_DM_FIXED:
|
174 | a5b38b51 | aurel32 | trigger_mode = APIC_TRIGGER_EDGE; |
175 | a5b38b51 | aurel32 | if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
|
176 | a5b38b51 | aurel32 | (lvt & APIC_LVT_LEVEL_TRIGGER)) |
177 | a5b38b51 | aurel32 | trigger_mode = APIC_TRIGGER_LEVEL; |
178 | a5b38b51 | aurel32 | apic_set_irq(s, lvt & 0xff, trigger_mode);
|
179 | a5b38b51 | aurel32 | } |
180 | a5b38b51 | aurel32 | } |
181 | a5b38b51 | aurel32 | |
182 | 92a16d7a | Blue Swirl | void apic_deliver_pic_intr(DeviceState *d, int level) |
183 | 1a7de94a | aurel32 | { |
184 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
185 | 92a16d7a | Blue Swirl | |
186 | cf6d64bf | Blue Swirl | if (level) {
|
187 | cf6d64bf | Blue Swirl | apic_local_deliver(s, APIC_LVT_LINT0); |
188 | cf6d64bf | Blue Swirl | } else {
|
189 | 1a7de94a | aurel32 | uint32_t lvt = s->lvt[APIC_LVT_LINT0]; |
190 | 1a7de94a | aurel32 | |
191 | 1a7de94a | aurel32 | switch ((lvt >> 8) & 7) { |
192 | 1a7de94a | aurel32 | case APIC_DM_FIXED:
|
193 | 1a7de94a | aurel32 | if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
|
194 | 1a7de94a | aurel32 | break;
|
195 | 1a7de94a | aurel32 | reset_bit(s->irr, lvt & 0xff);
|
196 | 1a7de94a | aurel32 | /* fall through */
|
197 | 1a7de94a | aurel32 | case APIC_DM_EXTINT:
|
198 | cf6d64bf | Blue Swirl | cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
199 | 1a7de94a | aurel32 | break;
|
200 | 1a7de94a | aurel32 | } |
201 | 1a7de94a | aurel32 | } |
202 | 1a7de94a | aurel32 | } |
203 | 1a7de94a | aurel32 | |
204 | d3e9db93 | bellard | #define foreach_apic(apic, deliver_bitmask, code) \
|
205 | d3e9db93 | bellard | {\ |
206 | d3e9db93 | bellard | int __i, __j, __mask;\
|
207 | d3e9db93 | bellard | for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\ |
208 | d3e9db93 | bellard | __mask = deliver_bitmask[__i];\ |
209 | d3e9db93 | bellard | if (__mask) {\
|
210 | d3e9db93 | bellard | for(__j = 0; __j < 32; __j++) {\ |
211 | d3e9db93 | bellard | if (__mask & (1 << __j)) {\ |
212 | d3e9db93 | bellard | apic = local_apics[__i * 32 + __j];\
|
213 | d3e9db93 | bellard | if (apic) {\
|
214 | d3e9db93 | bellard | code;\ |
215 | d3e9db93 | bellard | }\ |
216 | d3e9db93 | bellard | }\ |
217 | d3e9db93 | bellard | }\ |
218 | d3e9db93 | bellard | }\ |
219 | d3e9db93 | bellard | }\ |
220 | d3e9db93 | bellard | } |
221 | d3e9db93 | bellard | |
222 | 5fafdf24 | ths | static void apic_bus_deliver(const uint32_t *deliver_bitmask, |
223 | d3e9db93 | bellard | uint8_t delivery_mode, |
224 | d592d303 | bellard | uint8_t vector_num, uint8_t polarity, |
225 | d592d303 | bellard | uint8_t trigger_mode) |
226 | d592d303 | bellard | { |
227 | d592d303 | bellard | APICState *apic_iter; |
228 | d592d303 | bellard | |
229 | d592d303 | bellard | switch (delivery_mode) {
|
230 | d592d303 | bellard | case APIC_DM_LOWPRI:
|
231 | 8dd69b8f | bellard | /* XXX: search for focus processor, arbitration */
|
232 | d3e9db93 | bellard | { |
233 | d3e9db93 | bellard | int i, d;
|
234 | d3e9db93 | bellard | d = -1;
|
235 | d3e9db93 | bellard | for(i = 0; i < MAX_APIC_WORDS; i++) { |
236 | d3e9db93 | bellard | if (deliver_bitmask[i]) {
|
237 | d3e9db93 | bellard | d = i * 32 + ffs_bit(deliver_bitmask[i]);
|
238 | d3e9db93 | bellard | break;
|
239 | d3e9db93 | bellard | } |
240 | d3e9db93 | bellard | } |
241 | d3e9db93 | bellard | if (d >= 0) { |
242 | d3e9db93 | bellard | apic_iter = local_apics[d]; |
243 | d3e9db93 | bellard | if (apic_iter) {
|
244 | d3e9db93 | bellard | apic_set_irq(apic_iter, vector_num, trigger_mode); |
245 | d3e9db93 | bellard | } |
246 | d3e9db93 | bellard | } |
247 | 8dd69b8f | bellard | } |
248 | d3e9db93 | bellard | return;
|
249 | 8dd69b8f | bellard | |
250 | d592d303 | bellard | case APIC_DM_FIXED:
|
251 | d592d303 | bellard | break;
|
252 | d592d303 | bellard | |
253 | d592d303 | bellard | case APIC_DM_SMI:
|
254 | e2eb9d3e | aurel32 | foreach_apic(apic_iter, deliver_bitmask, |
255 | e2eb9d3e | aurel32 | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) ); |
256 | e2eb9d3e | aurel32 | return;
|
257 | e2eb9d3e | aurel32 | |
258 | d592d303 | bellard | case APIC_DM_NMI:
|
259 | e2eb9d3e | aurel32 | foreach_apic(apic_iter, deliver_bitmask, |
260 | e2eb9d3e | aurel32 | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) ); |
261 | e2eb9d3e | aurel32 | return;
|
262 | d592d303 | bellard | |
263 | d592d303 | bellard | case APIC_DM_INIT:
|
264 | d592d303 | bellard | /* normal INIT IPI sent to processors */
|
265 | 5fafdf24 | ths | foreach_apic(apic_iter, deliver_bitmask, |
266 | b09ea7d5 | Gleb Natapov | cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) ); |
267 | d592d303 | bellard | return;
|
268 | 3b46e624 | ths | |
269 | d592d303 | bellard | case APIC_DM_EXTINT:
|
270 | b1fc0348 | bellard | /* handled in I/O APIC code */
|
271 | d592d303 | bellard | break;
|
272 | d592d303 | bellard | |
273 | d592d303 | bellard | default:
|
274 | d592d303 | bellard | return;
|
275 | d592d303 | bellard | } |
276 | d592d303 | bellard | |
277 | 5fafdf24 | ths | foreach_apic(apic_iter, deliver_bitmask, |
278 | d3e9db93 | bellard | apic_set_irq(apic_iter, vector_num, trigger_mode) ); |
279 | d592d303 | bellard | } |
280 | 574bbf7b | bellard | |
281 | 610626af | aliguori | void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
|
282 | 610626af | aliguori | uint8_t delivery_mode, uint8_t vector_num, |
283 | 610626af | aliguori | uint8_t polarity, uint8_t trigger_mode) |
284 | 610626af | aliguori | { |
285 | 610626af | aliguori | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
286 | 610626af | aliguori | |
287 | d8023f31 | Blue Swirl | trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num, |
288 | d8023f31 | Blue Swirl | polarity, trigger_mode); |
289 | d8023f31 | Blue Swirl | |
290 | 610626af | aliguori | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
291 | 610626af | aliguori | apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity, |
292 | 610626af | aliguori | trigger_mode); |
293 | 610626af | aliguori | } |
294 | 610626af | aliguori | |
295 | 92a16d7a | Blue Swirl | void cpu_set_apic_base(DeviceState *d, uint64_t val)
|
296 | 574bbf7b | bellard | { |
297 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
298 | 92a16d7a | Blue Swirl | |
299 | d8023f31 | Blue Swirl | trace_cpu_set_apic_base(val); |
300 | d8023f31 | Blue Swirl | |
301 | 2c7c13d4 | aurel32 | if (!s)
|
302 | 2c7c13d4 | aurel32 | return;
|
303 | 5fafdf24 | ths | s->apicbase = (val & 0xfffff000) |
|
304 | 574bbf7b | bellard | (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE)); |
305 | 574bbf7b | bellard | /* if disabled, cannot be enabled again */
|
306 | 574bbf7b | bellard | if (!(val & MSR_IA32_APICBASE_ENABLE)) {
|
307 | 574bbf7b | bellard | s->apicbase &= ~MSR_IA32_APICBASE_ENABLE; |
308 | 0e26b7b8 | Blue Swirl | cpu_clear_apic_feature(s->cpu_env); |
309 | 574bbf7b | bellard | s->spurious_vec &= ~APIC_SV_ENABLE; |
310 | 574bbf7b | bellard | } |
311 | 574bbf7b | bellard | } |
312 | 574bbf7b | bellard | |
313 | 92a16d7a | Blue Swirl | uint64_t cpu_get_apic_base(DeviceState *d) |
314 | 574bbf7b | bellard | { |
315 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
316 | 92a16d7a | Blue Swirl | |
317 | d8023f31 | Blue Swirl | trace_cpu_get_apic_base(s ? (uint64_t)s->apicbase: 0);
|
318 | d8023f31 | Blue Swirl | |
319 | 2c7c13d4 | aurel32 | return s ? s->apicbase : 0; |
320 | 574bbf7b | bellard | } |
321 | 574bbf7b | bellard | |
322 | 92a16d7a | Blue Swirl | void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
|
323 | 9230e66e | bellard | { |
324 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
325 | 92a16d7a | Blue Swirl | |
326 | 2c7c13d4 | aurel32 | if (!s)
|
327 | 2c7c13d4 | aurel32 | return;
|
328 | 9230e66e | bellard | s->tpr = (val & 0x0f) << 4; |
329 | d592d303 | bellard | apic_update_irq(s); |
330 | 9230e66e | bellard | } |
331 | 9230e66e | bellard | |
332 | 92a16d7a | Blue Swirl | uint8_t cpu_get_apic_tpr(DeviceState *d) |
333 | 9230e66e | bellard | { |
334 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
335 | 92a16d7a | Blue Swirl | |
336 | 2c7c13d4 | aurel32 | return s ? s->tpr >> 4 : 0; |
337 | 9230e66e | bellard | } |
338 | 9230e66e | bellard | |
339 | d592d303 | bellard | /* return -1 if no bit is set */
|
340 | d592d303 | bellard | static int get_highest_priority_int(uint32_t *tab) |
341 | d592d303 | bellard | { |
342 | d592d303 | bellard | int i;
|
343 | d592d303 | bellard | for(i = 7; i >= 0; i--) { |
344 | d592d303 | bellard | if (tab[i] != 0) { |
345 | 3b63c04e | aurel32 | return i * 32 + fls_bit(tab[i]); |
346 | d592d303 | bellard | } |
347 | d592d303 | bellard | } |
348 | d592d303 | bellard | return -1; |
349 | d592d303 | bellard | } |
350 | d592d303 | bellard | |
351 | 574bbf7b | bellard | static int apic_get_ppr(APICState *s) |
352 | 574bbf7b | bellard | { |
353 | 574bbf7b | bellard | int tpr, isrv, ppr;
|
354 | 574bbf7b | bellard | |
355 | 574bbf7b | bellard | tpr = (s->tpr >> 4);
|
356 | 574bbf7b | bellard | isrv = get_highest_priority_int(s->isr); |
357 | 574bbf7b | bellard | if (isrv < 0) |
358 | 574bbf7b | bellard | isrv = 0;
|
359 | 574bbf7b | bellard | isrv >>= 4;
|
360 | 574bbf7b | bellard | if (tpr >= isrv)
|
361 | 574bbf7b | bellard | ppr = s->tpr; |
362 | 574bbf7b | bellard | else
|
363 | 574bbf7b | bellard | ppr = isrv << 4;
|
364 | 574bbf7b | bellard | return ppr;
|
365 | 574bbf7b | bellard | } |
366 | 574bbf7b | bellard | |
367 | d592d303 | bellard | static int apic_get_arb_pri(APICState *s) |
368 | d592d303 | bellard | { |
369 | d592d303 | bellard | /* XXX: arbitration */
|
370 | d592d303 | bellard | return 0; |
371 | d592d303 | bellard | } |
372 | d592d303 | bellard | |
373 | 574bbf7b | bellard | /* signal the CPU if an irq is pending */
|
374 | 574bbf7b | bellard | static void apic_update_irq(APICState *s) |
375 | 574bbf7b | bellard | { |
376 | d592d303 | bellard | int irrv, ppr;
|
377 | d592d303 | bellard | if (!(s->spurious_vec & APIC_SV_ENABLE))
|
378 | d592d303 | bellard | return;
|
379 | 574bbf7b | bellard | irrv = get_highest_priority_int(s->irr); |
380 | 574bbf7b | bellard | if (irrv < 0) |
381 | 574bbf7b | bellard | return;
|
382 | d592d303 | bellard | ppr = apic_get_ppr(s); |
383 | d592d303 | bellard | if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) |
384 | 574bbf7b | bellard | return;
|
385 | 574bbf7b | bellard | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD); |
386 | 574bbf7b | bellard | } |
387 | 574bbf7b | bellard | |
388 | 73822ec8 | aliguori | void apic_reset_irq_delivered(void) |
389 | 73822ec8 | aliguori | { |
390 | d8023f31 | Blue Swirl | trace_apic_reset_irq_delivered(apic_irq_delivered); |
391 | d8023f31 | Blue Swirl | |
392 | 73822ec8 | aliguori | apic_irq_delivered = 0;
|
393 | 73822ec8 | aliguori | } |
394 | 73822ec8 | aliguori | |
395 | 73822ec8 | aliguori | int apic_get_irq_delivered(void) |
396 | 73822ec8 | aliguori | { |
397 | d8023f31 | Blue Swirl | trace_apic_get_irq_delivered(apic_irq_delivered); |
398 | d8023f31 | Blue Swirl | |
399 | 73822ec8 | aliguori | return apic_irq_delivered;
|
400 | 73822ec8 | aliguori | } |
401 | 73822ec8 | aliguori | |
402 | 574bbf7b | bellard | static void apic_set_irq(APICState *s, int vector_num, int trigger_mode) |
403 | 574bbf7b | bellard | { |
404 | 73822ec8 | aliguori | apic_irq_delivered += !get_bit(s->irr, vector_num); |
405 | d8023f31 | Blue Swirl | |
406 | d8023f31 | Blue Swirl | trace_apic_set_irq(apic_irq_delivered); |
407 | 73822ec8 | aliguori | |
408 | 574bbf7b | bellard | set_bit(s->irr, vector_num); |
409 | 574bbf7b | bellard | if (trigger_mode)
|
410 | 574bbf7b | bellard | set_bit(s->tmr, vector_num); |
411 | 574bbf7b | bellard | else
|
412 | 574bbf7b | bellard | reset_bit(s->tmr, vector_num); |
413 | 574bbf7b | bellard | apic_update_irq(s); |
414 | 574bbf7b | bellard | } |
415 | 574bbf7b | bellard | |
416 | 574bbf7b | bellard | static void apic_eoi(APICState *s) |
417 | 574bbf7b | bellard | { |
418 | 574bbf7b | bellard | int isrv;
|
419 | 574bbf7b | bellard | isrv = get_highest_priority_int(s->isr); |
420 | 574bbf7b | bellard | if (isrv < 0) |
421 | 574bbf7b | bellard | return;
|
422 | 574bbf7b | bellard | reset_bit(s->isr, isrv); |
423 | d592d303 | bellard | /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
|
424 | d592d303 | bellard | set the remote IRR bit for level triggered interrupts. */
|
425 | 574bbf7b | bellard | apic_update_irq(s); |
426 | 574bbf7b | bellard | } |
427 | 574bbf7b | bellard | |
428 | 678e12cc | Gleb Natapov | static int apic_find_dest(uint8_t dest) |
429 | 678e12cc | Gleb Natapov | { |
430 | 678e12cc | Gleb Natapov | APICState *apic = local_apics[dest]; |
431 | 678e12cc | Gleb Natapov | int i;
|
432 | 678e12cc | Gleb Natapov | |
433 | 678e12cc | Gleb Natapov | if (apic && apic->id == dest)
|
434 | 678e12cc | Gleb Natapov | return dest; /* shortcut in case apic->id == apic->idx */ |
435 | 678e12cc | Gleb Natapov | |
436 | 678e12cc | Gleb Natapov | for (i = 0; i < MAX_APICS; i++) { |
437 | 678e12cc | Gleb Natapov | apic = local_apics[i]; |
438 | 678e12cc | Gleb Natapov | if (apic && apic->id == dest)
|
439 | 678e12cc | Gleb Natapov | return i;
|
440 | 678e12cc | Gleb Natapov | } |
441 | 678e12cc | Gleb Natapov | |
442 | 678e12cc | Gleb Natapov | return -1; |
443 | 678e12cc | Gleb Natapov | } |
444 | 678e12cc | Gleb Natapov | |
445 | d3e9db93 | bellard | static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask, |
446 | d3e9db93 | bellard | uint8_t dest, uint8_t dest_mode) |
447 | d592d303 | bellard | { |
448 | d592d303 | bellard | APICState *apic_iter; |
449 | d3e9db93 | bellard | int i;
|
450 | d592d303 | bellard | |
451 | d592d303 | bellard | if (dest_mode == 0) { |
452 | d3e9db93 | bellard | if (dest == 0xff) { |
453 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t)); |
454 | d3e9db93 | bellard | } else {
|
455 | 678e12cc | Gleb Natapov | int idx = apic_find_dest(dest);
|
456 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
457 | 678e12cc | Gleb Natapov | if (idx >= 0) |
458 | 678e12cc | Gleb Natapov | set_bit(deliver_bitmask, idx); |
459 | d3e9db93 | bellard | } |
460 | d592d303 | bellard | } else {
|
461 | d592d303 | bellard | /* XXX: cluster mode */
|
462 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t)); |
463 | d3e9db93 | bellard | for(i = 0; i < MAX_APICS; i++) { |
464 | d3e9db93 | bellard | apic_iter = local_apics[i]; |
465 | d3e9db93 | bellard | if (apic_iter) {
|
466 | d3e9db93 | bellard | if (apic_iter->dest_mode == 0xf) { |
467 | d3e9db93 | bellard | if (dest & apic_iter->log_dest)
|
468 | d3e9db93 | bellard | set_bit(deliver_bitmask, i); |
469 | d3e9db93 | bellard | } else if (apic_iter->dest_mode == 0x0) { |
470 | d3e9db93 | bellard | if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) && |
471 | d3e9db93 | bellard | (dest & apic_iter->log_dest & 0x0f)) {
|
472 | d3e9db93 | bellard | set_bit(deliver_bitmask, i); |
473 | d3e9db93 | bellard | } |
474 | d3e9db93 | bellard | } |
475 | d3e9db93 | bellard | } |
476 | d592d303 | bellard | } |
477 | d592d303 | bellard | } |
478 | d592d303 | bellard | } |
479 | d592d303 | bellard | |
480 | 92a16d7a | Blue Swirl | void apic_init_reset(DeviceState *d)
|
481 | d592d303 | bellard | { |
482 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
483 | d592d303 | bellard | int i;
|
484 | d592d303 | bellard | |
485 | b09ea7d5 | Gleb Natapov | if (!s)
|
486 | b09ea7d5 | Gleb Natapov | return;
|
487 | b09ea7d5 | Gleb Natapov | |
488 | d592d303 | bellard | s->tpr = 0;
|
489 | d592d303 | bellard | s->spurious_vec = 0xff;
|
490 | d592d303 | bellard | s->log_dest = 0;
|
491 | e0fd8781 | bellard | s->dest_mode = 0xf;
|
492 | d592d303 | bellard | memset(s->isr, 0, sizeof(s->isr)); |
493 | d592d303 | bellard | memset(s->tmr, 0, sizeof(s->tmr)); |
494 | d592d303 | bellard | memset(s->irr, 0, sizeof(s->irr)); |
495 | b4511723 | bellard | for(i = 0; i < APIC_LVT_NB; i++) |
496 | b4511723 | bellard | s->lvt[i] = 1 << 16; /* mask LVT */ |
497 | d592d303 | bellard | s->esr = 0;
|
498 | d592d303 | bellard | memset(s->icr, 0, sizeof(s->icr)); |
499 | d592d303 | bellard | s->divide_conf = 0;
|
500 | d592d303 | bellard | s->count_shift = 0;
|
501 | d592d303 | bellard | s->initial_count = 0;
|
502 | d592d303 | bellard | s->initial_count_load_time = 0;
|
503 | d592d303 | bellard | s->next_time = 0;
|
504 | b09ea7d5 | Gleb Natapov | s->wait_for_sipi = 1;
|
505 | d592d303 | bellard | } |
506 | d592d303 | bellard | |
507 | e0fd8781 | bellard | static void apic_startup(APICState *s, int vector_num) |
508 | e0fd8781 | bellard | { |
509 | b09ea7d5 | Gleb Natapov | s->sipi_vector = vector_num; |
510 | b09ea7d5 | Gleb Natapov | cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI); |
511 | b09ea7d5 | Gleb Natapov | } |
512 | b09ea7d5 | Gleb Natapov | |
513 | 92a16d7a | Blue Swirl | void apic_sipi(DeviceState *d)
|
514 | b09ea7d5 | Gleb Natapov | { |
515 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
516 | 92a16d7a | Blue Swirl | |
517 | 4a942cea | Blue Swirl | cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI); |
518 | b09ea7d5 | Gleb Natapov | |
519 | b09ea7d5 | Gleb Natapov | if (!s->wait_for_sipi)
|
520 | e0fd8781 | bellard | return;
|
521 | 0e26b7b8 | Blue Swirl | cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector); |
522 | b09ea7d5 | Gleb Natapov | s->wait_for_sipi = 0;
|
523 | e0fd8781 | bellard | } |
524 | e0fd8781 | bellard | |
525 | 92a16d7a | Blue Swirl | static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode, |
526 | d592d303 | bellard | uint8_t delivery_mode, uint8_t vector_num, |
527 | d592d303 | bellard | uint8_t polarity, uint8_t trigger_mode) |
528 | d592d303 | bellard | { |
529 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
530 | d3e9db93 | bellard | uint32_t deliver_bitmask[MAX_APIC_WORDS]; |
531 | d592d303 | bellard | int dest_shorthand = (s->icr[0] >> 18) & 3; |
532 | d592d303 | bellard | APICState *apic_iter; |
533 | d592d303 | bellard | |
534 | e0fd8781 | bellard | switch (dest_shorthand) {
|
535 | d3e9db93 | bellard | case 0: |
536 | d3e9db93 | bellard | apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode); |
537 | d3e9db93 | bellard | break;
|
538 | d3e9db93 | bellard | case 1: |
539 | d3e9db93 | bellard | memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask)); |
540 | 678e12cc | Gleb Natapov | set_bit(deliver_bitmask, s->idx); |
541 | d3e9db93 | bellard | break;
|
542 | d3e9db93 | bellard | case 2: |
543 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
544 | d3e9db93 | bellard | break;
|
545 | d3e9db93 | bellard | case 3: |
546 | d3e9db93 | bellard | memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask)); |
547 | 678e12cc | Gleb Natapov | reset_bit(deliver_bitmask, s->idx); |
548 | d3e9db93 | bellard | break;
|
549 | e0fd8781 | bellard | } |
550 | e0fd8781 | bellard | |
551 | d592d303 | bellard | switch (delivery_mode) {
|
552 | d592d303 | bellard | case APIC_DM_INIT:
|
553 | d592d303 | bellard | { |
554 | d592d303 | bellard | int trig_mode = (s->icr[0] >> 15) & 1; |
555 | d592d303 | bellard | int level = (s->icr[0] >> 14) & 1; |
556 | d592d303 | bellard | if (level == 0 && trig_mode == 1) { |
557 | 5fafdf24 | ths | foreach_apic(apic_iter, deliver_bitmask, |
558 | d3e9db93 | bellard | apic_iter->arb_id = apic_iter->id ); |
559 | d592d303 | bellard | return;
|
560 | d592d303 | bellard | } |
561 | d592d303 | bellard | } |
562 | d592d303 | bellard | break;
|
563 | d592d303 | bellard | |
564 | d592d303 | bellard | case APIC_DM_SIPI:
|
565 | 5fafdf24 | ths | foreach_apic(apic_iter, deliver_bitmask, |
566 | d3e9db93 | bellard | apic_startup(apic_iter, vector_num) ); |
567 | d592d303 | bellard | return;
|
568 | d592d303 | bellard | } |
569 | d592d303 | bellard | |
570 | d592d303 | bellard | apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity, |
571 | d592d303 | bellard | trigger_mode); |
572 | d592d303 | bellard | } |
573 | d592d303 | bellard | |
574 | 92a16d7a | Blue Swirl | int apic_get_interrupt(DeviceState *d)
|
575 | 574bbf7b | bellard | { |
576 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
577 | 574bbf7b | bellard | int intno;
|
578 | 574bbf7b | bellard | |
579 | 574bbf7b | bellard | /* if the APIC is installed or enabled, we let the 8259 handle the
|
580 | 574bbf7b | bellard | IRQs */
|
581 | 574bbf7b | bellard | if (!s)
|
582 | 574bbf7b | bellard | return -1; |
583 | 574bbf7b | bellard | if (!(s->spurious_vec & APIC_SV_ENABLE))
|
584 | 574bbf7b | bellard | return -1; |
585 | 3b46e624 | ths | |
586 | 574bbf7b | bellard | /* XXX: spurious IRQ handling */
|
587 | 574bbf7b | bellard | intno = get_highest_priority_int(s->irr); |
588 | 574bbf7b | bellard | if (intno < 0) |
589 | 574bbf7b | bellard | return -1; |
590 | d592d303 | bellard | if (s->tpr && intno <= s->tpr)
|
591 | d592d303 | bellard | return s->spurious_vec & 0xff; |
592 | b4511723 | bellard | reset_bit(s->irr, intno); |
593 | 574bbf7b | bellard | set_bit(s->isr, intno); |
594 | 574bbf7b | bellard | apic_update_irq(s); |
595 | 574bbf7b | bellard | return intno;
|
596 | 574bbf7b | bellard | } |
597 | 574bbf7b | bellard | |
598 | 92a16d7a | Blue Swirl | int apic_accept_pic_intr(DeviceState *d)
|
599 | 0e21e12b | ths | { |
600 | 92a16d7a | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
601 | 0e21e12b | ths | uint32_t lvt0; |
602 | 0e21e12b | ths | |
603 | 0e21e12b | ths | if (!s)
|
604 | 0e21e12b | ths | return -1; |
605 | 0e21e12b | ths | |
606 | 0e21e12b | ths | lvt0 = s->lvt[APIC_LVT_LINT0]; |
607 | 0e21e12b | ths | |
608 | a5b38b51 | aurel32 | if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 || |
609 | a5b38b51 | aurel32 | (lvt0 & APIC_LVT_MASKED) == 0)
|
610 | 0e21e12b | ths | return 1; |
611 | 0e21e12b | ths | |
612 | 0e21e12b | ths | return 0; |
613 | 0e21e12b | ths | } |
614 | 0e21e12b | ths | |
615 | 574bbf7b | bellard | static uint32_t apic_get_current_count(APICState *s)
|
616 | 574bbf7b | bellard | { |
617 | 574bbf7b | bellard | int64_t d; |
618 | 574bbf7b | bellard | uint32_t val; |
619 | 5fafdf24 | ths | d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >> |
620 | 574bbf7b | bellard | s->count_shift; |
621 | 574bbf7b | bellard | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
|
622 | 574bbf7b | bellard | /* periodic */
|
623 | d592d303 | bellard | val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
|
624 | 574bbf7b | bellard | } else {
|
625 | 574bbf7b | bellard | if (d >= s->initial_count)
|
626 | 574bbf7b | bellard | val = 0;
|
627 | 574bbf7b | bellard | else
|
628 | 574bbf7b | bellard | val = s->initial_count - d; |
629 | 574bbf7b | bellard | } |
630 | 574bbf7b | bellard | return val;
|
631 | 574bbf7b | bellard | } |
632 | 574bbf7b | bellard | |
633 | 574bbf7b | bellard | static void apic_timer_update(APICState *s, int64_t current_time) |
634 | 574bbf7b | bellard | { |
635 | 574bbf7b | bellard | int64_t next_time, d; |
636 | 3b46e624 | ths | |
637 | 574bbf7b | bellard | if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
|
638 | 5fafdf24 | ths | d = (current_time - s->initial_count_load_time) >> |
639 | 574bbf7b | bellard | s->count_shift; |
640 | 574bbf7b | bellard | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
|
641 | 681f8c29 | aliguori | if (!s->initial_count)
|
642 | 681f8c29 | aliguori | goto no_timer;
|
643 | d592d303 | bellard | d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1); |
644 | 574bbf7b | bellard | } else {
|
645 | 574bbf7b | bellard | if (d >= s->initial_count)
|
646 | 574bbf7b | bellard | goto no_timer;
|
647 | d592d303 | bellard | d = (uint64_t)s->initial_count + 1;
|
648 | 574bbf7b | bellard | } |
649 | 574bbf7b | bellard | next_time = s->initial_count_load_time + (d << s->count_shift); |
650 | 574bbf7b | bellard | qemu_mod_timer(s->timer, next_time); |
651 | 574bbf7b | bellard | s->next_time = next_time; |
652 | 574bbf7b | bellard | } else {
|
653 | 574bbf7b | bellard | no_timer:
|
654 | 574bbf7b | bellard | qemu_del_timer(s->timer); |
655 | 574bbf7b | bellard | } |
656 | 574bbf7b | bellard | } |
657 | 574bbf7b | bellard | |
658 | 574bbf7b | bellard | static void apic_timer(void *opaque) |
659 | 574bbf7b | bellard | { |
660 | 574bbf7b | bellard | APICState *s = opaque; |
661 | 574bbf7b | bellard | |
662 | cf6d64bf | Blue Swirl | apic_local_deliver(s, APIC_LVT_TIMER); |
663 | 574bbf7b | bellard | apic_timer_update(s, s->next_time); |
664 | 574bbf7b | bellard | } |
665 | 574bbf7b | bellard | |
666 | c227f099 | Anthony Liguori | static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr) |
667 | 574bbf7b | bellard | { |
668 | 574bbf7b | bellard | return 0; |
669 | 574bbf7b | bellard | } |
670 | 574bbf7b | bellard | |
671 | c227f099 | Anthony Liguori | static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr) |
672 | 574bbf7b | bellard | { |
673 | 574bbf7b | bellard | return 0; |
674 | 574bbf7b | bellard | } |
675 | 574bbf7b | bellard | |
676 | c227f099 | Anthony Liguori | static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val) |
677 | 574bbf7b | bellard | { |
678 | 574bbf7b | bellard | } |
679 | 574bbf7b | bellard | |
680 | c227f099 | Anthony Liguori | static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val) |
681 | 574bbf7b | bellard | { |
682 | 574bbf7b | bellard | } |
683 | 574bbf7b | bellard | |
684 | c227f099 | Anthony Liguori | static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr) |
685 | 574bbf7b | bellard | { |
686 | 92a16d7a | Blue Swirl | DeviceState *d; |
687 | 574bbf7b | bellard | APICState *s; |
688 | 574bbf7b | bellard | uint32_t val; |
689 | 574bbf7b | bellard | int index;
|
690 | 574bbf7b | bellard | |
691 | 92a16d7a | Blue Swirl | d = cpu_get_current_apic(); |
692 | 92a16d7a | Blue Swirl | if (!d) {
|
693 | 574bbf7b | bellard | return 0; |
694 | 0e26b7b8 | Blue Swirl | } |
695 | 92a16d7a | Blue Swirl | s = DO_UPCAST(APICState, busdev.qdev, d); |
696 | 574bbf7b | bellard | |
697 | 574bbf7b | bellard | index = (addr >> 4) & 0xff; |
698 | 574bbf7b | bellard | switch(index) {
|
699 | 574bbf7b | bellard | case 0x02: /* id */ |
700 | 574bbf7b | bellard | val = s->id << 24;
|
701 | 574bbf7b | bellard | break;
|
702 | 574bbf7b | bellard | case 0x03: /* version */ |
703 | 574bbf7b | bellard | val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */ |
704 | 574bbf7b | bellard | break;
|
705 | 574bbf7b | bellard | case 0x08: |
706 | 574bbf7b | bellard | val = s->tpr; |
707 | 574bbf7b | bellard | break;
|
708 | d592d303 | bellard | case 0x09: |
709 | d592d303 | bellard | val = apic_get_arb_pri(s); |
710 | d592d303 | bellard | break;
|
711 | 574bbf7b | bellard | case 0x0a: |
712 | 574bbf7b | bellard | /* ppr */
|
713 | 574bbf7b | bellard | val = apic_get_ppr(s); |
714 | 574bbf7b | bellard | break;
|
715 | b237db36 | aurel32 | case 0x0b: |
716 | b237db36 | aurel32 | val = 0;
|
717 | b237db36 | aurel32 | break;
|
718 | d592d303 | bellard | case 0x0d: |
719 | d592d303 | bellard | val = s->log_dest << 24;
|
720 | d592d303 | bellard | break;
|
721 | d592d303 | bellard | case 0x0e: |
722 | d592d303 | bellard | val = s->dest_mode << 28;
|
723 | d592d303 | bellard | break;
|
724 | 574bbf7b | bellard | case 0x0f: |
725 | 574bbf7b | bellard | val = s->spurious_vec; |
726 | 574bbf7b | bellard | break;
|
727 | 574bbf7b | bellard | case 0x10 ... 0x17: |
728 | 574bbf7b | bellard | val = s->isr[index & 7];
|
729 | 574bbf7b | bellard | break;
|
730 | 574bbf7b | bellard | case 0x18 ... 0x1f: |
731 | 574bbf7b | bellard | val = s->tmr[index & 7];
|
732 | 574bbf7b | bellard | break;
|
733 | 574bbf7b | bellard | case 0x20 ... 0x27: |
734 | 574bbf7b | bellard | val = s->irr[index & 7];
|
735 | 574bbf7b | bellard | break;
|
736 | 574bbf7b | bellard | case 0x28: |
737 | 574bbf7b | bellard | val = s->esr; |
738 | 574bbf7b | bellard | break;
|
739 | 574bbf7b | bellard | case 0x30: |
740 | 574bbf7b | bellard | case 0x31: |
741 | 574bbf7b | bellard | val = s->icr[index & 1];
|
742 | 574bbf7b | bellard | break;
|
743 | e0fd8781 | bellard | case 0x32 ... 0x37: |
744 | e0fd8781 | bellard | val = s->lvt[index - 0x32];
|
745 | e0fd8781 | bellard | break;
|
746 | 574bbf7b | bellard | case 0x38: |
747 | 574bbf7b | bellard | val = s->initial_count; |
748 | 574bbf7b | bellard | break;
|
749 | 574bbf7b | bellard | case 0x39: |
750 | 574bbf7b | bellard | val = apic_get_current_count(s); |
751 | 574bbf7b | bellard | break;
|
752 | 574bbf7b | bellard | case 0x3e: |
753 | 574bbf7b | bellard | val = s->divide_conf; |
754 | 574bbf7b | bellard | break;
|
755 | 574bbf7b | bellard | default:
|
756 | 574bbf7b | bellard | s->esr |= ESR_ILLEGAL_ADDRESS; |
757 | 574bbf7b | bellard | val = 0;
|
758 | 574bbf7b | bellard | break;
|
759 | 574bbf7b | bellard | } |
760 | d8023f31 | Blue Swirl | trace_apic_mem_readl(addr, val); |
761 | 574bbf7b | bellard | return val;
|
762 | 574bbf7b | bellard | } |
763 | 574bbf7b | bellard | |
764 | c227f099 | Anthony Liguori | static void apic_send_msi(target_phys_addr_t addr, uint32 data) |
765 | 54c96da7 | Michael S. Tsirkin | { |
766 | 54c96da7 | Michael S. Tsirkin | uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT; |
767 | 54c96da7 | Michael S. Tsirkin | uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT; |
768 | 54c96da7 | Michael S. Tsirkin | uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
|
769 | 54c96da7 | Michael S. Tsirkin | uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
|
770 | 54c96da7 | Michael S. Tsirkin | uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
|
771 | 54c96da7 | Michael S. Tsirkin | /* XXX: Ignore redirection hint. */
|
772 | 54c96da7 | Michael S. Tsirkin | apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
|
773 | 54c96da7 | Michael S. Tsirkin | } |
774 | 54c96da7 | Michael S. Tsirkin | |
775 | c227f099 | Anthony Liguori | static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
776 | 574bbf7b | bellard | { |
777 | 92a16d7a | Blue Swirl | DeviceState *d; |
778 | 574bbf7b | bellard | APICState *s; |
779 | 54c96da7 | Michael S. Tsirkin | int index = (addr >> 4) & 0xff; |
780 | 54c96da7 | Michael S. Tsirkin | if (addr > 0xfff || !index) { |
781 | 54c96da7 | Michael S. Tsirkin | /* MSI and MMIO APIC are at the same memory location,
|
782 | 54c96da7 | Michael S. Tsirkin | * but actually not on the global bus: MSI is on PCI bus
|
783 | 54c96da7 | Michael S. Tsirkin | * APIC is connected directly to the CPU.
|
784 | 54c96da7 | Michael S. Tsirkin | * Mapping them on the global bus happens to work because
|
785 | 54c96da7 | Michael S. Tsirkin | * MSI registers are reserved in APIC MMIO and vice versa. */
|
786 | 54c96da7 | Michael S. Tsirkin | apic_send_msi(addr, val); |
787 | 54c96da7 | Michael S. Tsirkin | return;
|
788 | 54c96da7 | Michael S. Tsirkin | } |
789 | 574bbf7b | bellard | |
790 | 92a16d7a | Blue Swirl | d = cpu_get_current_apic(); |
791 | 92a16d7a | Blue Swirl | if (!d) {
|
792 | 574bbf7b | bellard | return;
|
793 | 0e26b7b8 | Blue Swirl | } |
794 | 92a16d7a | Blue Swirl | s = DO_UPCAST(APICState, busdev.qdev, d); |
795 | 574bbf7b | bellard | |
796 | d8023f31 | Blue Swirl | trace_apic_mem_writel(addr, val); |
797 | 574bbf7b | bellard | |
798 | 574bbf7b | bellard | switch(index) {
|
799 | 574bbf7b | bellard | case 0x02: |
800 | 574bbf7b | bellard | s->id = (val >> 24);
|
801 | 574bbf7b | bellard | break;
|
802 | e0fd8781 | bellard | case 0x03: |
803 | e0fd8781 | bellard | break;
|
804 | 574bbf7b | bellard | case 0x08: |
805 | 574bbf7b | bellard | s->tpr = val; |
806 | d592d303 | bellard | apic_update_irq(s); |
807 | 574bbf7b | bellard | break;
|
808 | e0fd8781 | bellard | case 0x09: |
809 | e0fd8781 | bellard | case 0x0a: |
810 | e0fd8781 | bellard | break;
|
811 | 574bbf7b | bellard | case 0x0b: /* EOI */ |
812 | 574bbf7b | bellard | apic_eoi(s); |
813 | 574bbf7b | bellard | break;
|
814 | d592d303 | bellard | case 0x0d: |
815 | d592d303 | bellard | s->log_dest = val >> 24;
|
816 | d592d303 | bellard | break;
|
817 | d592d303 | bellard | case 0x0e: |
818 | d592d303 | bellard | s->dest_mode = val >> 28;
|
819 | d592d303 | bellard | break;
|
820 | 574bbf7b | bellard | case 0x0f: |
821 | 574bbf7b | bellard | s->spurious_vec = val & 0x1ff;
|
822 | d592d303 | bellard | apic_update_irq(s); |
823 | 574bbf7b | bellard | break;
|
824 | e0fd8781 | bellard | case 0x10 ... 0x17: |
825 | e0fd8781 | bellard | case 0x18 ... 0x1f: |
826 | e0fd8781 | bellard | case 0x20 ... 0x27: |
827 | e0fd8781 | bellard | case 0x28: |
828 | e0fd8781 | bellard | break;
|
829 | 574bbf7b | bellard | case 0x30: |
830 | d592d303 | bellard | s->icr[0] = val;
|
831 | 92a16d7a | Blue Swirl | apic_deliver(d, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1, |
832 | d592d303 | bellard | (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff), |
833 | d592d303 | bellard | (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1); |
834 | d592d303 | bellard | break;
|
835 | 574bbf7b | bellard | case 0x31: |
836 | d592d303 | bellard | s->icr[1] = val;
|
837 | 574bbf7b | bellard | break;
|
838 | 574bbf7b | bellard | case 0x32 ... 0x37: |
839 | 574bbf7b | bellard | { |
840 | 574bbf7b | bellard | int n = index - 0x32; |
841 | 574bbf7b | bellard | s->lvt[n] = val; |
842 | 574bbf7b | bellard | if (n == APIC_LVT_TIMER)
|
843 | 574bbf7b | bellard | apic_timer_update(s, qemu_get_clock(vm_clock)); |
844 | 574bbf7b | bellard | } |
845 | 574bbf7b | bellard | break;
|
846 | 574bbf7b | bellard | case 0x38: |
847 | 574bbf7b | bellard | s->initial_count = val; |
848 | 574bbf7b | bellard | s->initial_count_load_time = qemu_get_clock(vm_clock); |
849 | 574bbf7b | bellard | apic_timer_update(s, s->initial_count_load_time); |
850 | 574bbf7b | bellard | break;
|
851 | e0fd8781 | bellard | case 0x39: |
852 | e0fd8781 | bellard | break;
|
853 | 574bbf7b | bellard | case 0x3e: |
854 | 574bbf7b | bellard | { |
855 | 574bbf7b | bellard | int v;
|
856 | 574bbf7b | bellard | s->divide_conf = val & 0xb;
|
857 | 574bbf7b | bellard | v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4); |
858 | 574bbf7b | bellard | s->count_shift = (v + 1) & 7; |
859 | 574bbf7b | bellard | } |
860 | 574bbf7b | bellard | break;
|
861 | 574bbf7b | bellard | default:
|
862 | 574bbf7b | bellard | s->esr |= ESR_ILLEGAL_ADDRESS; |
863 | 574bbf7b | bellard | break;
|
864 | 574bbf7b | bellard | } |
865 | 574bbf7b | bellard | } |
866 | 574bbf7b | bellard | |
867 | 695dcf71 | Juan Quintela | /* This function is only used for old state version 1 and 2 */
|
868 | 695dcf71 | Juan Quintela | static int apic_load_old(QEMUFile *f, void *opaque, int version_id) |
869 | d592d303 | bellard | { |
870 | d592d303 | bellard | APICState *s = opaque; |
871 | d592d303 | bellard | int i;
|
872 | d592d303 | bellard | |
873 | e6cf6a8c | bellard | if (version_id > 2) |
874 | d592d303 | bellard | return -EINVAL;
|
875 | d592d303 | bellard | |
876 | d592d303 | bellard | /* XXX: what if the base changes? (registered memory regions) */
|
877 | d592d303 | bellard | qemu_get_be32s(f, &s->apicbase); |
878 | d592d303 | bellard | qemu_get_8s(f, &s->id); |
879 | d592d303 | bellard | qemu_get_8s(f, &s->arb_id); |
880 | d592d303 | bellard | qemu_get_8s(f, &s->tpr); |
881 | d592d303 | bellard | qemu_get_be32s(f, &s->spurious_vec); |
882 | d592d303 | bellard | qemu_get_8s(f, &s->log_dest); |
883 | d592d303 | bellard | qemu_get_8s(f, &s->dest_mode); |
884 | d592d303 | bellard | for (i = 0; i < 8; i++) { |
885 | d592d303 | bellard | qemu_get_be32s(f, &s->isr[i]); |
886 | d592d303 | bellard | qemu_get_be32s(f, &s->tmr[i]); |
887 | d592d303 | bellard | qemu_get_be32s(f, &s->irr[i]); |
888 | d592d303 | bellard | } |
889 | d592d303 | bellard | for (i = 0; i < APIC_LVT_NB; i++) { |
890 | d592d303 | bellard | qemu_get_be32s(f, &s->lvt[i]); |
891 | d592d303 | bellard | } |
892 | d592d303 | bellard | qemu_get_be32s(f, &s->esr); |
893 | d592d303 | bellard | qemu_get_be32s(f, &s->icr[0]);
|
894 | d592d303 | bellard | qemu_get_be32s(f, &s->icr[1]);
|
895 | d592d303 | bellard | qemu_get_be32s(f, &s->divide_conf); |
896 | bee8d684 | ths | s->count_shift=qemu_get_be32(f); |
897 | d592d303 | bellard | qemu_get_be32s(f, &s->initial_count); |
898 | bee8d684 | ths | s->initial_count_load_time=qemu_get_be64(f); |
899 | bee8d684 | ths | s->next_time=qemu_get_be64(f); |
900 | e6cf6a8c | bellard | |
901 | e6cf6a8c | bellard | if (version_id >= 2) |
902 | e6cf6a8c | bellard | qemu_get_timer(f, s->timer); |
903 | d592d303 | bellard | return 0; |
904 | d592d303 | bellard | } |
905 | 574bbf7b | bellard | |
906 | 695dcf71 | Juan Quintela | static const VMStateDescription vmstate_apic = { |
907 | 695dcf71 | Juan Quintela | .name = "apic",
|
908 | 695dcf71 | Juan Quintela | .version_id = 3,
|
909 | 695dcf71 | Juan Quintela | .minimum_version_id = 3,
|
910 | 695dcf71 | Juan Quintela | .minimum_version_id_old = 1,
|
911 | 695dcf71 | Juan Quintela | .load_state_old = apic_load_old, |
912 | 695dcf71 | Juan Quintela | .fields = (VMStateField []) { |
913 | 695dcf71 | Juan Quintela | VMSTATE_UINT32(apicbase, APICState), |
914 | 695dcf71 | Juan Quintela | VMSTATE_UINT8(id, APICState), |
915 | 695dcf71 | Juan Quintela | VMSTATE_UINT8(arb_id, APICState), |
916 | 695dcf71 | Juan Quintela | VMSTATE_UINT8(tpr, APICState), |
917 | 695dcf71 | Juan Quintela | VMSTATE_UINT32(spurious_vec, APICState), |
918 | 695dcf71 | Juan Quintela | VMSTATE_UINT8(log_dest, APICState), |
919 | 695dcf71 | Juan Quintela | VMSTATE_UINT8(dest_mode, APICState), |
920 | 695dcf71 | Juan Quintela | VMSTATE_UINT32_ARRAY(isr, APICState, 8),
|
921 | 695dcf71 | Juan Quintela | VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
|
922 | 695dcf71 | Juan Quintela | VMSTATE_UINT32_ARRAY(irr, APICState, 8),
|
923 | 695dcf71 | Juan Quintela | VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB), |
924 | 695dcf71 | Juan Quintela | VMSTATE_UINT32(esr, APICState), |
925 | 695dcf71 | Juan Quintela | VMSTATE_UINT32_ARRAY(icr, APICState, 2),
|
926 | 695dcf71 | Juan Quintela | VMSTATE_UINT32(divide_conf, APICState), |
927 | 695dcf71 | Juan Quintela | VMSTATE_INT32(count_shift, APICState), |
928 | 695dcf71 | Juan Quintela | VMSTATE_UINT32(initial_count, APICState), |
929 | 695dcf71 | Juan Quintela | VMSTATE_INT64(initial_count_load_time, APICState), |
930 | 695dcf71 | Juan Quintela | VMSTATE_INT64(next_time, APICState), |
931 | 695dcf71 | Juan Quintela | VMSTATE_TIMER(timer, APICState), |
932 | 695dcf71 | Juan Quintela | VMSTATE_END_OF_LIST() |
933 | 695dcf71 | Juan Quintela | } |
934 | 695dcf71 | Juan Quintela | }; |
935 | 695dcf71 | Juan Quintela | |
936 | 8546b099 | Blue Swirl | static void apic_reset(DeviceState *d) |
937 | d592d303 | bellard | { |
938 | 8546b099 | Blue Swirl | APICState *s = DO_UPCAST(APICState, busdev.qdev, d); |
939 | 4c0960c0 | Avi Kivity | int bsp;
|
940 | fec5fa02 | aurel32 | |
941 | 4c0960c0 | Avi Kivity | bsp = cpu_is_bsp(s->cpu_env); |
942 | fec5fa02 | aurel32 | s->apicbase = 0xfee00000 |
|
943 | 678e12cc | Gleb Natapov | (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
|
944 | fec5fa02 | aurel32 | |
945 | 92a16d7a | Blue Swirl | apic_init_reset(d); |
946 | 0e21e12b | ths | |
947 | 678e12cc | Gleb Natapov | if (bsp) {
|
948 | a5b38b51 | aurel32 | /*
|
949 | a5b38b51 | aurel32 | * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
|
950 | a5b38b51 | aurel32 | * time typically by BIOS, so PIC interrupt can be delivered to the
|
951 | a5b38b51 | aurel32 | * processor when local APIC is enabled.
|
952 | a5b38b51 | aurel32 | */
|
953 | a5b38b51 | aurel32 | s->lvt[APIC_LVT_LINT0] = 0x700;
|
954 | a5b38b51 | aurel32 | } |
955 | d592d303 | bellard | } |
956 | 574bbf7b | bellard | |
957 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const apic_mem_read[3] = { |
958 | 574bbf7b | bellard | apic_mem_readb, |
959 | 574bbf7b | bellard | apic_mem_readw, |
960 | 574bbf7b | bellard | apic_mem_readl, |
961 | 574bbf7b | bellard | }; |
962 | 574bbf7b | bellard | |
963 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const apic_mem_write[3] = { |
964 | 574bbf7b | bellard | apic_mem_writeb, |
965 | 574bbf7b | bellard | apic_mem_writew, |
966 | 574bbf7b | bellard | apic_mem_writel, |
967 | 574bbf7b | bellard | }; |
968 | 574bbf7b | bellard | |
969 | 8546b099 | Blue Swirl | static int apic_init1(SysBusDevice *dev) |
970 | 8546b099 | Blue Swirl | { |
971 | 8546b099 | Blue Swirl | APICState *s = FROM_SYSBUS(APICState, dev); |
972 | 8546b099 | Blue Swirl | int apic_io_memory;
|
973 | 8546b099 | Blue Swirl | static int last_apic_idx; |
974 | 8546b099 | Blue Swirl | |
975 | 8546b099 | Blue Swirl | if (last_apic_idx >= MAX_APICS) {
|
976 | 8546b099 | Blue Swirl | return -1; |
977 | 8546b099 | Blue Swirl | } |
978 | 8546b099 | Blue Swirl | apic_io_memory = cpu_register_io_memory(apic_mem_read, |
979 | 8546b099 | Blue Swirl | apic_mem_write, NULL);
|
980 | 8546b099 | Blue Swirl | sysbus_init_mmio(dev, MSI_ADDR_SIZE, apic_io_memory); |
981 | 8546b099 | Blue Swirl | |
982 | 8546b099 | Blue Swirl | s->timer = qemu_new_timer(vm_clock, apic_timer, s); |
983 | 8546b099 | Blue Swirl | s->idx = last_apic_idx++; |
984 | 8546b099 | Blue Swirl | local_apics[s->idx] = s; |
985 | 8546b099 | Blue Swirl | return 0; |
986 | 8546b099 | Blue Swirl | } |
987 | 8546b099 | Blue Swirl | |
988 | 8546b099 | Blue Swirl | static SysBusDeviceInfo apic_info = {
|
989 | 8546b099 | Blue Swirl | .init = apic_init1, |
990 | 8546b099 | Blue Swirl | .qdev.name = "apic",
|
991 | 8546b099 | Blue Swirl | .qdev.size = sizeof(APICState),
|
992 | 8546b099 | Blue Swirl | .qdev.vmsd = &vmstate_apic, |
993 | 8546b099 | Blue Swirl | .qdev.reset = apic_reset, |
994 | 8546b099 | Blue Swirl | .qdev.no_user = 1,
|
995 | 8546b099 | Blue Swirl | .qdev.props = (Property[]) { |
996 | 8546b099 | Blue Swirl | DEFINE_PROP_UINT8("id", APICState, id, -1), |
997 | 8546b099 | Blue Swirl | DEFINE_PROP_PTR("cpu_env", APICState, cpu_env),
|
998 | 8546b099 | Blue Swirl | DEFINE_PROP_END_OF_LIST(), |
999 | 8546b099 | Blue Swirl | } |
1000 | 8546b099 | Blue Swirl | }; |
1001 | 8546b099 | Blue Swirl | |
1002 | 8546b099 | Blue Swirl | static void apic_register_devices(void) |
1003 | 8546b099 | Blue Swirl | { |
1004 | 8546b099 | Blue Swirl | sysbus_register_withprop(&apic_info); |
1005 | 8546b099 | Blue Swirl | } |
1006 | 8546b099 | Blue Swirl | |
1007 | 8546b099 | Blue Swirl | device_init(apic_register_devices) |