]>
Commit | Line | Data |
---|---|---|
dae01685 JK |
1 | /* |
2 | * APIC support - common bits of emulated and KVM kernel model | |
3 | * | |
4 | * Copyright (c) 2004-2005 Fabrice Bellard | |
5 | * Copyright (c) 2011 Jan Kiszka, Siemens AG | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, see <http://www.gnu.org/licenses/> | |
19 | */ | |
0d09e41a PB |
20 | #include "hw/i386/apic.h" |
21 | #include "hw/i386/apic_internal.h" | |
dae01685 | 22 | #include "trace.h" |
9c17d615 | 23 | #include "sysemu/kvm.h" |
53a89e26 IM |
24 | #include "hw/qdev.h" |
25 | #include "hw/sysbus.h" | |
dae01685 JK |
26 | |
27 | static int apic_irq_delivered; | |
e5ad936b | 28 | bool apic_report_tpr_access; |
dae01685 | 29 | |
d3b0c9e9 | 30 | void cpu_set_apic_base(DeviceState *dev, uint64_t val) |
dae01685 | 31 | { |
dae01685 JK |
32 | trace_cpu_set_apic_base(val); |
33 | ||
d3b0c9e9 XZ |
34 | if (dev) { |
35 | APICCommonState *s = APIC_COMMON(dev); | |
999e12bb | 36 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
dae01685 JK |
37 | info->set_base(s, val); |
38 | } | |
39 | } | |
40 | ||
d3b0c9e9 | 41 | uint64_t cpu_get_apic_base(DeviceState *dev) |
dae01685 | 42 | { |
d3b0c9e9 XZ |
43 | if (dev) { |
44 | APICCommonState *s = APIC_COMMON(dev); | |
999e12bb AL |
45 | trace_cpu_get_apic_base((uint64_t)s->apicbase); |
46 | return s->apicbase; | |
47 | } else { | |
dd673288 IM |
48 | trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP); |
49 | return MSR_IA32_APICBASE_BSP; | |
999e12bb | 50 | } |
dae01685 JK |
51 | } |
52 | ||
d3b0c9e9 | 53 | void cpu_set_apic_tpr(DeviceState *dev, uint8_t val) |
dae01685 | 54 | { |
999e12bb AL |
55 | APICCommonState *s; |
56 | APICCommonClass *info; | |
dae01685 | 57 | |
d3b0c9e9 | 58 | if (!dev) { |
999e12bb | 59 | return; |
dae01685 | 60 | } |
999e12bb | 61 | |
d3b0c9e9 | 62 | s = APIC_COMMON(dev); |
999e12bb AL |
63 | info = APIC_COMMON_GET_CLASS(s); |
64 | ||
65 | info->set_tpr(s, val); | |
dae01685 JK |
66 | } |
67 | ||
d3b0c9e9 | 68 | uint8_t cpu_get_apic_tpr(DeviceState *dev) |
e5ad936b JK |
69 | { |
70 | APICCommonState *s; | |
71 | APICCommonClass *info; | |
72 | ||
d3b0c9e9 | 73 | if (!dev) { |
e5ad936b JK |
74 | return 0; |
75 | } | |
76 | ||
d3b0c9e9 | 77 | s = APIC_COMMON(dev); |
e5ad936b JK |
78 | info = APIC_COMMON_GET_CLASS(s); |
79 | ||
80 | return info->get_tpr(s); | |
81 | } | |
82 | ||
d3b0c9e9 | 83 | void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable) |
e5ad936b | 84 | { |
d3b0c9e9 | 85 | APICCommonState *s = APIC_COMMON(dev); |
e5ad936b JK |
86 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
87 | ||
88 | apic_report_tpr_access = enable; | |
89 | if (info->enable_tpr_reporting) { | |
90 | info->enable_tpr_reporting(s, enable); | |
91 | } | |
92 | } | |
93 | ||
d3b0c9e9 | 94 | void apic_enable_vapic(DeviceState *dev, hwaddr paddr) |
dae01685 | 95 | { |
d3b0c9e9 | 96 | APICCommonState *s = APIC_COMMON(dev); |
e5ad936b | 97 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
dae01685 | 98 | |
e5ad936b JK |
99 | s->vapic_paddr = paddr; |
100 | info->vapic_base_update(s); | |
dae01685 JK |
101 | } |
102 | ||
d3b0c9e9 | 103 | void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip, |
d362e757 JK |
104 | TPRAccess access) |
105 | { | |
d3b0c9e9 | 106 | APICCommonState *s = APIC_COMMON(dev); |
e5ad936b | 107 | |
d77953b9 | 108 | vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access); |
d362e757 JK |
109 | } |
110 | ||
dae01685 JK |
111 | void apic_report_irq_delivered(int delivered) |
112 | { | |
113 | apic_irq_delivered += delivered; | |
114 | ||
115 | trace_apic_report_irq_delivered(apic_irq_delivered); | |
116 | } | |
117 | ||
118 | void apic_reset_irq_delivered(void) | |
119 | { | |
9bcec938 FCE |
120 | /* Copy this into a local variable to encourage gcc to emit a plain |
121 | * register for a sys/sdt.h marker. For details on this workaround, see: | |
122 | * https://sourceware.org/bugzilla/show_bug.cgi?id=13296 | |
123 | */ | |
124 | volatile int a_i_d = apic_irq_delivered; | |
125 | trace_apic_reset_irq_delivered(a_i_d); | |
dae01685 JK |
126 | |
127 | apic_irq_delivered = 0; | |
128 | } | |
129 | ||
130 | int apic_get_irq_delivered(void) | |
131 | { | |
132 | trace_apic_get_irq_delivered(apic_irq_delivered); | |
133 | ||
134 | return apic_irq_delivered; | |
135 | } | |
136 | ||
d3b0c9e9 | 137 | void apic_deliver_nmi(DeviceState *dev) |
dae01685 | 138 | { |
d3b0c9e9 | 139 | APICCommonState *s = APIC_COMMON(dev); |
999e12bb | 140 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
dae01685 | 141 | |
dae01685 JK |
142 | info->external_nmi(s); |
143 | } | |
144 | ||
7a380ca3 JK |
145 | bool apic_next_timer(APICCommonState *s, int64_t current_time) |
146 | { | |
147 | int64_t d; | |
148 | ||
149 | /* We need to store the timer state separately to support APIC | |
150 | * implementations that maintain a non-QEMU timer, e.g. inside the | |
151 | * host kernel. This open-coded state allows us to migrate between | |
152 | * both models. */ | |
153 | s->timer_expiry = -1; | |
154 | ||
155 | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) { | |
156 | return false; | |
157 | } | |
158 | ||
159 | d = (current_time - s->initial_count_load_time) >> s->count_shift; | |
160 | ||
161 | if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) { | |
162 | if (!s->initial_count) { | |
163 | return false; | |
164 | } | |
165 | d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * | |
166 | ((uint64_t)s->initial_count + 1); | |
167 | } else { | |
168 | if (d >= s->initial_count) { | |
169 | return false; | |
170 | } | |
171 | d = (uint64_t)s->initial_count + 1; | |
172 | } | |
173 | s->next_time = s->initial_count_load_time + (d << s->count_shift); | |
174 | s->timer_expiry = s->next_time; | |
175 | return true; | |
176 | } | |
177 | ||
d3b0c9e9 | 178 | void apic_init_reset(DeviceState *dev) |
dae01685 | 179 | { |
927411fa PB |
180 | APICCommonState *s; |
181 | APICCommonClass *info; | |
dae01685 JK |
182 | int i; |
183 | ||
927411fa | 184 | if (!dev) { |
dae01685 JK |
185 | return; |
186 | } | |
927411fa | 187 | s = APIC_COMMON(dev); |
dae01685 JK |
188 | s->tpr = 0; |
189 | s->spurious_vec = 0xff; | |
190 | s->log_dest = 0; | |
191 | s->dest_mode = 0xf; | |
192 | memset(s->isr, 0, sizeof(s->isr)); | |
193 | memset(s->tmr, 0, sizeof(s->tmr)); | |
194 | memset(s->irr, 0, sizeof(s->irr)); | |
195 | for (i = 0; i < APIC_LVT_NB; i++) { | |
196 | s->lvt[i] = APIC_LVT_MASKED; | |
197 | } | |
198 | s->esr = 0; | |
199 | memset(s->icr, 0, sizeof(s->icr)); | |
200 | s->divide_conf = 0; | |
201 | s->count_shift = 0; | |
202 | s->initial_count = 0; | |
203 | s->initial_count_load_time = 0; | |
204 | s->next_time = 0; | |
7b4d915e | 205 | s->wait_for_sipi = !cpu_is_bsp(s->cpu); |
dae01685 | 206 | |
7a380ca3 | 207 | if (s->timer) { |
bc72ad67 | 208 | timer_del(s->timer); |
7a380ca3 JK |
209 | } |
210 | s->timer_expiry = -1; | |
575a6f40 | 211 | |
927411fa | 212 | info = APIC_COMMON_GET_CLASS(s); |
575a6f40 PB |
213 | if (info->reset) { |
214 | info->reset(s); | |
215 | } | |
dae01685 JK |
216 | } |
217 | ||
d3b0c9e9 | 218 | void apic_designate_bsp(DeviceState *dev) |
dd673288 | 219 | { |
d3b0c9e9 | 220 | if (dev == NULL) { |
dd673288 IM |
221 | return; |
222 | } | |
223 | ||
d3b0c9e9 | 224 | APICCommonState *s = APIC_COMMON(dev); |
dd673288 IM |
225 | s->apicbase |= MSR_IA32_APICBASE_BSP; |
226 | } | |
227 | ||
d3b0c9e9 | 228 | static void apic_reset_common(DeviceState *dev) |
dae01685 | 229 | { |
d3b0c9e9 | 230 | APICCommonState *s = APIC_COMMON(dev); |
e5ad936b | 231 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
dae01685 JK |
232 | bool bsp; |
233 | ||
60671e58 | 234 | bsp = cpu_is_bsp(s->cpu); |
dab86234 | 235 | s->apicbase = APIC_DEFAULT_ADDRESS | |
dae01685 JK |
236 | (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE; |
237 | ||
e5ad936b JK |
238 | s->vapic_paddr = 0; |
239 | info->vapic_base_update(s); | |
240 | ||
d3b0c9e9 | 241 | apic_init_reset(dev); |
dae01685 JK |
242 | |
243 | if (bsp) { | |
244 | /* | |
245 | * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization | |
246 | * time typically by BIOS, so PIC interrupt can be delivered to the | |
247 | * processor when local APIC is enabled. | |
248 | */ | |
249 | s->lvt[APIC_LVT_LINT0] = 0x700; | |
250 | } | |
251 | } | |
252 | ||
253 | /* This function is only used for old state version 1 and 2 */ | |
254 | static int apic_load_old(QEMUFile *f, void *opaque, int version_id) | |
255 | { | |
256 | APICCommonState *s = opaque; | |
a4aecd28 | 257 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); |
dae01685 JK |
258 | int i; |
259 | ||
260 | if (version_id > 2) { | |
261 | return -EINVAL; | |
262 | } | |
263 | ||
264 | /* XXX: what if the base changes? (registered memory regions) */ | |
265 | qemu_get_be32s(f, &s->apicbase); | |
266 | qemu_get_8s(f, &s->id); | |
267 | qemu_get_8s(f, &s->arb_id); | |
268 | qemu_get_8s(f, &s->tpr); | |
269 | qemu_get_be32s(f, &s->spurious_vec); | |
270 | qemu_get_8s(f, &s->log_dest); | |
271 | qemu_get_8s(f, &s->dest_mode); | |
272 | for (i = 0; i < 8; i++) { | |
273 | qemu_get_be32s(f, &s->isr[i]); | |
274 | qemu_get_be32s(f, &s->tmr[i]); | |
275 | qemu_get_be32s(f, &s->irr[i]); | |
276 | } | |
277 | for (i = 0; i < APIC_LVT_NB; i++) { | |
278 | qemu_get_be32s(f, &s->lvt[i]); | |
279 | } | |
280 | qemu_get_be32s(f, &s->esr); | |
281 | qemu_get_be32s(f, &s->icr[0]); | |
282 | qemu_get_be32s(f, &s->icr[1]); | |
283 | qemu_get_be32s(f, &s->divide_conf); | |
284 | s->count_shift = qemu_get_be32(f); | |
285 | qemu_get_be32s(f, &s->initial_count); | |
286 | s->initial_count_load_time = qemu_get_be64(f); | |
287 | s->next_time = qemu_get_be64(f); | |
288 | ||
289 | if (version_id >= 2) { | |
a4aecd28 JK |
290 | s->timer_expiry = qemu_get_be64(f); |
291 | } | |
292 | ||
293 | if (info->post_load) { | |
294 | info->post_load(s); | |
dae01685 JK |
295 | } |
296 | return 0; | |
297 | } | |
298 | ||
494c2717 | 299 | static void apic_common_realize(DeviceState *dev, Error **errp) |
dae01685 | 300 | { |
999e12bb AL |
301 | APICCommonState *s = APIC_COMMON(dev); |
302 | APICCommonClass *info; | |
e5ad936b | 303 | static DeviceState *vapic; |
dae01685 | 304 | static int apic_no; |
53a89e26 | 305 | static bool mmio_registered; |
dae01685 JK |
306 | |
307 | if (apic_no >= MAX_APICS) { | |
494c2717 XZ |
308 | error_setg(errp, "%s initialization failed.", |
309 | object_get_typename(OBJECT(dev))); | |
310 | return; | |
dae01685 JK |
311 | } |
312 | s->idx = apic_no++; | |
313 | ||
999e12bb | 314 | info = APIC_COMMON_GET_CLASS(s); |
494c2717 | 315 | info->realize(dev, errp); |
53a89e26 | 316 | if (!mmio_registered) { |
494c2717 | 317 | ICCBus *b = ICC_BUS(qdev_get_parent_bus(dev)); |
53a89e26 IM |
318 | memory_region_add_subregion(b->apic_address_space, 0, &s->io_memory); |
319 | mmio_registered = true; | |
320 | } | |
e5ad936b | 321 | |
a9605e03 JK |
322 | /* Note: We need at least 1M to map the VAPIC option ROM */ |
323 | if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK && | |
324 | ram_size >= 1024 * 1024) { | |
e5ad936b JK |
325 | vapic = sysbus_create_simple("kvmvapic", -1, NULL); |
326 | } | |
327 | s->vapic = vapic; | |
328 | if (apic_report_tpr_access && info->enable_tpr_reporting) { | |
329 | info->enable_tpr_reporting(s, true); | |
330 | } | |
331 | ||
dae01685 JK |
332 | } |
333 | ||
c2c00148 PD |
334 | static int apic_pre_load(void *opaque) |
335 | { | |
336 | APICCommonState *s = APIC_COMMON(opaque); | |
337 | ||
338 | /* The default is !cpu_is_bsp(s->cpu), but the common value is 0 | |
339 | * so that's what apic_common_sipi_needed checks for. Reset to | |
340 | * the value that is assumed when the apic_sipi subsection is | |
341 | * absent. | |
342 | */ | |
343 | s->wait_for_sipi = 0; | |
344 | return 0; | |
345 | } | |
346 | ||
e5ad936b JK |
347 | static void apic_dispatch_pre_save(void *opaque) |
348 | { | |
349 | APICCommonState *s = APIC_COMMON(opaque); | |
350 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); | |
351 | ||
352 | if (info->pre_save) { | |
353 | info->pre_save(s); | |
354 | } | |
355 | } | |
356 | ||
7a380ca3 JK |
357 | static int apic_dispatch_post_load(void *opaque, int version_id) |
358 | { | |
999e12bb AL |
359 | APICCommonState *s = APIC_COMMON(opaque); |
360 | APICCommonClass *info = APIC_COMMON_GET_CLASS(s); | |
7a380ca3 JK |
361 | |
362 | if (info->post_load) { | |
363 | info->post_load(s); | |
364 | } | |
365 | return 0; | |
366 | } | |
367 | ||
c2c00148 PD |
368 | static bool apic_common_sipi_needed(void *opaque) |
369 | { | |
370 | APICCommonState *s = APIC_COMMON(opaque); | |
371 | return s->wait_for_sipi != 0; | |
372 | } | |
373 | ||
374 | static const VMStateDescription vmstate_apic_common_sipi = { | |
375 | .name = "apic_sipi", | |
376 | .version_id = 1, | |
377 | .minimum_version_id = 1, | |
378 | .fields = (VMStateField[]) { | |
379 | VMSTATE_INT32(sipi_vector, APICCommonState), | |
380 | VMSTATE_INT32(wait_for_sipi, APICCommonState), | |
381 | VMSTATE_END_OF_LIST() | |
382 | } | |
383 | }; | |
384 | ||
dae01685 JK |
385 | static const VMStateDescription vmstate_apic_common = { |
386 | .name = "apic", | |
387 | .version_id = 3, | |
388 | .minimum_version_id = 3, | |
389 | .minimum_version_id_old = 1, | |
390 | .load_state_old = apic_load_old, | |
c2c00148 | 391 | .pre_load = apic_pre_load, |
e5ad936b | 392 | .pre_save = apic_dispatch_pre_save, |
7a380ca3 | 393 | .post_load = apic_dispatch_post_load, |
dae01685 JK |
394 | .fields = (VMStateField[]) { |
395 | VMSTATE_UINT32(apicbase, APICCommonState), | |
396 | VMSTATE_UINT8(id, APICCommonState), | |
397 | VMSTATE_UINT8(arb_id, APICCommonState), | |
398 | VMSTATE_UINT8(tpr, APICCommonState), | |
399 | VMSTATE_UINT32(spurious_vec, APICCommonState), | |
400 | VMSTATE_UINT8(log_dest, APICCommonState), | |
401 | VMSTATE_UINT8(dest_mode, APICCommonState), | |
402 | VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8), | |
403 | VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8), | |
404 | VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8), | |
405 | VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB), | |
406 | VMSTATE_UINT32(esr, APICCommonState), | |
407 | VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2), | |
408 | VMSTATE_UINT32(divide_conf, APICCommonState), | |
409 | VMSTATE_INT32(count_shift, APICCommonState), | |
410 | VMSTATE_UINT32(initial_count, APICCommonState), | |
411 | VMSTATE_INT64(initial_count_load_time, APICCommonState), | |
412 | VMSTATE_INT64(next_time, APICCommonState), | |
7a380ca3 JK |
413 | VMSTATE_INT64(timer_expiry, |
414 | APICCommonState), /* open-coded timer state */ | |
dae01685 | 415 | VMSTATE_END_OF_LIST() |
c2c00148 PD |
416 | }, |
417 | .subsections = (VMStateSubsection[]) { | |
418 | { | |
419 | .vmsd = &vmstate_apic_common_sipi, | |
420 | .needed = apic_common_sipi_needed, | |
421 | }, | |
422 | VMSTATE_END_OF_LIST() | |
dae01685 JK |
423 | } |
424 | }; | |
425 | ||
426 | static Property apic_properties_common[] = { | |
427 | DEFINE_PROP_UINT8("id", APICCommonState, id, -1), | |
aa93200b | 428 | DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14), |
e5ad936b JK |
429 | DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT, |
430 | true), | |
dae01685 JK |
431 | DEFINE_PROP_END_OF_LIST(), |
432 | }; | |
433 | ||
999e12bb AL |
434 | static void apic_common_class_init(ObjectClass *klass, void *data) |
435 | { | |
53a89e26 | 436 | ICCDeviceClass *idc = ICC_DEVICE_CLASS(klass); |
39bffca2 | 437 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 438 | |
39bffca2 AL |
439 | dc->vmsd = &vmstate_apic_common; |
440 | dc->reset = apic_reset_common; | |
39bffca2 | 441 | dc->props = apic_properties_common; |
494c2717 | 442 | idc->realize = apic_common_realize; |
f37a4374 MA |
443 | /* |
444 | * Reason: APIC and CPU need to be wired up by | |
445 | * x86_cpu_apic_create() | |
446 | */ | |
447 | dc->cannot_instantiate_with_device_add_yet = true; | |
999e12bb | 448 | } |
dae01685 | 449 | |
8c43a6f0 | 450 | static const TypeInfo apic_common_type = { |
999e12bb | 451 | .name = TYPE_APIC_COMMON, |
53a89e26 | 452 | .parent = TYPE_ICC_DEVICE, |
999e12bb AL |
453 | .instance_size = sizeof(APICCommonState), |
454 | .class_size = sizeof(APICCommonClass), | |
455 | .class_init = apic_common_class_init, | |
456 | .abstract = true, | |
457 | }; | |
458 | ||
d3b0c9e9 | 459 | static void apic_common_register_types(void) |
999e12bb AL |
460 | { |
461 | type_register_static(&apic_common_type); | |
462 | } | |
463 | ||
d3b0c9e9 | 464 | type_init(apic_common_register_types) |