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