]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/apic_common.c
Merge remote-tracking branch 'remotes/stsquad/tags/pull-mttcg-fixups-for-rc2-280317...
[mirror_qemu.git] / hw / intc / apic_common.c
CommitLineData
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 */
b6a0aa05 20#include "qemu/osdep.h"
2f114315 21#include "qemu/error-report.h"
da34e65c 22#include "qapi/error.h"
33c11879
PB
23#include "qemu-common.h"
24#include "cpu.h"
33d7a288 25#include "qapi/visitor.h"
0d09e41a
PB
26#include "hw/i386/apic.h"
27#include "hw/i386/apic_internal.h"
dae01685 28#include "trace.h"
b0cb0a66 29#include "sysemu/hax.h"
9c17d615 30#include "sysemu/kvm.h"
53a89e26
IM
31#include "hw/qdev.h"
32#include "hw/sysbus.h"
dae01685
JK
33
34static int apic_irq_delivered;
e5ad936b 35bool apic_report_tpr_access;
dae01685 36
d3b0c9e9 37void cpu_set_apic_base(DeviceState *dev, uint64_t val)
dae01685 38{
dae01685
JK
39 trace_cpu_set_apic_base(val);
40
d3b0c9e9
XZ
41 if (dev) {
42 APICCommonState *s = APIC_COMMON(dev);
999e12bb 43 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
facb07cd
IM
44 /* switching to x2APIC, reset possibly modified xAPIC ID */
45 if (!(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
46 (val & MSR_IA32_APICBASE_EXTD)) {
47 s->id = s->initial_apic_id;
48 }
dae01685
JK
49 info->set_base(s, val);
50 }
51}
52
d3b0c9e9 53uint64_t cpu_get_apic_base(DeviceState *dev)
dae01685 54{
d3b0c9e9
XZ
55 if (dev) {
56 APICCommonState *s = APIC_COMMON(dev);
999e12bb
AL
57 trace_cpu_get_apic_base((uint64_t)s->apicbase);
58 return s->apicbase;
59 } else {
dd673288
IM
60 trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
61 return MSR_IA32_APICBASE_BSP;
999e12bb 62 }
dae01685
JK
63}
64
d3b0c9e9 65void cpu_set_apic_tpr(DeviceState *dev, uint8_t val)
dae01685 66{
999e12bb
AL
67 APICCommonState *s;
68 APICCommonClass *info;
dae01685 69
d3b0c9e9 70 if (!dev) {
999e12bb 71 return;
dae01685 72 }
999e12bb 73
d3b0c9e9 74 s = APIC_COMMON(dev);
999e12bb
AL
75 info = APIC_COMMON_GET_CLASS(s);
76
77 info->set_tpr(s, val);
dae01685
JK
78}
79
d3b0c9e9 80uint8_t cpu_get_apic_tpr(DeviceState *dev)
e5ad936b
JK
81{
82 APICCommonState *s;
83 APICCommonClass *info;
84
d3b0c9e9 85 if (!dev) {
e5ad936b
JK
86 return 0;
87 }
88
d3b0c9e9 89 s = APIC_COMMON(dev);
e5ad936b
JK
90 info = APIC_COMMON_GET_CLASS(s);
91
92 return info->get_tpr(s);
93}
94
d3b0c9e9 95void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable)
e5ad936b 96{
d3b0c9e9 97 APICCommonState *s = APIC_COMMON(dev);
e5ad936b
JK
98 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
99
100 apic_report_tpr_access = enable;
101 if (info->enable_tpr_reporting) {
102 info->enable_tpr_reporting(s, enable);
103 }
104}
105
d3b0c9e9 106void apic_enable_vapic(DeviceState *dev, hwaddr paddr)
dae01685 107{
d3b0c9e9 108 APICCommonState *s = APIC_COMMON(dev);
e5ad936b 109 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
dae01685 110
e5ad936b
JK
111 s->vapic_paddr = paddr;
112 info->vapic_base_update(s);
dae01685
JK
113}
114
d3b0c9e9 115void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip,
d362e757
JK
116 TPRAccess access)
117{
d3b0c9e9 118 APICCommonState *s = APIC_COMMON(dev);
e5ad936b 119
d77953b9 120 vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
d362e757
JK
121}
122
dae01685
JK
123void apic_report_irq_delivered(int delivered)
124{
125 apic_irq_delivered += delivered;
126
127 trace_apic_report_irq_delivered(apic_irq_delivered);
128}
129
130void apic_reset_irq_delivered(void)
131{
9bcec938
FCE
132 /* Copy this into a local variable to encourage gcc to emit a plain
133 * register for a sys/sdt.h marker. For details on this workaround, see:
134 * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
135 */
136 volatile int a_i_d = apic_irq_delivered;
137 trace_apic_reset_irq_delivered(a_i_d);
dae01685
JK
138
139 apic_irq_delivered = 0;
140}
141
142int apic_get_irq_delivered(void)
143{
144 trace_apic_get_irq_delivered(apic_irq_delivered);
145
146 return apic_irq_delivered;
147}
148
d3b0c9e9 149void apic_deliver_nmi(DeviceState *dev)
dae01685 150{
d3b0c9e9 151 APICCommonState *s = APIC_COMMON(dev);
999e12bb 152 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
dae01685 153
dae01685
JK
154 info->external_nmi(s);
155}
156
7a380ca3
JK
157bool apic_next_timer(APICCommonState *s, int64_t current_time)
158{
159 int64_t d;
160
161 /* We need to store the timer state separately to support APIC
162 * implementations that maintain a non-QEMU timer, e.g. inside the
163 * host kernel. This open-coded state allows us to migrate between
164 * both models. */
165 s->timer_expiry = -1;
166
167 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
168 return false;
169 }
170
171 d = (current_time - s->initial_count_load_time) >> s->count_shift;
172
173 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
174 if (!s->initial_count) {
175 return false;
176 }
177 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
178 ((uint64_t)s->initial_count + 1);
179 } else {
180 if (d >= s->initial_count) {
181 return false;
182 }
183 d = (uint64_t)s->initial_count + 1;
184 }
185 s->next_time = s->initial_count_load_time + (d << s->count_shift);
186 s->timer_expiry = s->next_time;
187 return true;
188}
189
d3b0c9e9 190void apic_init_reset(DeviceState *dev)
dae01685 191{
927411fa
PB
192 APICCommonState *s;
193 APICCommonClass *info;
dae01685
JK
194 int i;
195
927411fa 196 if (!dev) {
dae01685
JK
197 return;
198 }
927411fa 199 s = APIC_COMMON(dev);
dae01685
JK
200 s->tpr = 0;
201 s->spurious_vec = 0xff;
202 s->log_dest = 0;
203 s->dest_mode = 0xf;
204 memset(s->isr, 0, sizeof(s->isr));
205 memset(s->tmr, 0, sizeof(s->tmr));
206 memset(s->irr, 0, sizeof(s->irr));
207 for (i = 0; i < APIC_LVT_NB; i++) {
208 s->lvt[i] = APIC_LVT_MASKED;
209 }
210 s->esr = 0;
211 memset(s->icr, 0, sizeof(s->icr));
212 s->divide_conf = 0;
213 s->count_shift = 0;
214 s->initial_count = 0;
215 s->initial_count_load_time = 0;
216 s->next_time = 0;
7b4d915e 217 s->wait_for_sipi = !cpu_is_bsp(s->cpu);
dae01685 218
7a380ca3 219 if (s->timer) {
bc72ad67 220 timer_del(s->timer);
7a380ca3
JK
221 }
222 s->timer_expiry = -1;
575a6f40 223
927411fa 224 info = APIC_COMMON_GET_CLASS(s);
575a6f40
PB
225 if (info->reset) {
226 info->reset(s);
227 }
dae01685
JK
228}
229
9cb11fd7 230void apic_designate_bsp(DeviceState *dev, bool bsp)
dd673288 231{
d3b0c9e9 232 if (dev == NULL) {
dd673288
IM
233 return;
234 }
235
d3b0c9e9 236 APICCommonState *s = APIC_COMMON(dev);
9cb11fd7
NA
237 if (bsp) {
238 s->apicbase |= MSR_IA32_APICBASE_BSP;
239 } else {
240 s->apicbase &= ~MSR_IA32_APICBASE_BSP;
241 }
dd673288
IM
242}
243
d3b0c9e9 244static void apic_reset_common(DeviceState *dev)
dae01685 245{
d3b0c9e9 246 APICCommonState *s = APIC_COMMON(dev);
e5ad936b 247 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
81329754 248 uint32_t bsp;
dae01685 249
81329754
DL
250 bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
251 s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
4c34897a 252 s->id = s->initial_apic_id;
dae01685 253
f65e8212
PD
254 apic_reset_irq_delivered();
255
e5ad936b
JK
256 s->vapic_paddr = 0;
257 info->vapic_base_update(s);
258
d3b0c9e9 259 apic_init_reset(dev);
dae01685
JK
260}
261
262/* This function is only used for old state version 1 and 2 */
263static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
264{
265 APICCommonState *s = opaque;
a4aecd28 266 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
dae01685
JK
267 int i;
268
269 if (version_id > 2) {
270 return -EINVAL;
271 }
272
273 /* XXX: what if the base changes? (registered memory regions) */
274 qemu_get_be32s(f, &s->apicbase);
275 qemu_get_8s(f, &s->id);
276 qemu_get_8s(f, &s->arb_id);
277 qemu_get_8s(f, &s->tpr);
278 qemu_get_be32s(f, &s->spurious_vec);
279 qemu_get_8s(f, &s->log_dest);
280 qemu_get_8s(f, &s->dest_mode);
281 for (i = 0; i < 8; i++) {
282 qemu_get_be32s(f, &s->isr[i]);
283 qemu_get_be32s(f, &s->tmr[i]);
284 qemu_get_be32s(f, &s->irr[i]);
285 }
286 for (i = 0; i < APIC_LVT_NB; i++) {
287 qemu_get_be32s(f, &s->lvt[i]);
288 }
289 qemu_get_be32s(f, &s->esr);
290 qemu_get_be32s(f, &s->icr[0]);
291 qemu_get_be32s(f, &s->icr[1]);
292 qemu_get_be32s(f, &s->divide_conf);
293 s->count_shift = qemu_get_be32(f);
294 qemu_get_be32s(f, &s->initial_count);
295 s->initial_count_load_time = qemu_get_be64(f);
296 s->next_time = qemu_get_be64(f);
297
298 if (version_id >= 2) {
a4aecd28
JK
299 s->timer_expiry = qemu_get_be64(f);
300 }
301
302 if (info->post_load) {
303 info->post_load(s);
dae01685
JK
304 }
305 return 0;
306}
307
f6e98444
IM
308static const VMStateDescription vmstate_apic_common;
309
494c2717 310static void apic_common_realize(DeviceState *dev, Error **errp)
dae01685 311{
999e12bb
AL
312 APICCommonState *s = APIC_COMMON(dev);
313 APICCommonClass *info;
e5ad936b 314 static DeviceState *vapic;
f6e98444 315 int instance_id = s->id;
dae01685 316
999e12bb 317 info = APIC_COMMON_GET_CLASS(s);
494c2717 318 info->realize(dev, errp);
e5ad936b 319
a9605e03
JK
320 /* Note: We need at least 1M to map the VAPIC option ROM */
321 if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
b0cb0a66 322 !hax_enabled() && ram_size >= 1024 * 1024) {
e5ad936b
JK
323 vapic = sysbus_create_simple("kvmvapic", -1, NULL);
324 }
325 s->vapic = vapic;
326 if (apic_report_tpr_access && info->enable_tpr_reporting) {
327 info->enable_tpr_reporting(s, true);
328 }
329
f6e98444
IM
330 if (s->legacy_instance_id) {
331 instance_id = -1;
332 }
333 vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
bc5c4f21 334 s, -1, 0, NULL);
dae01685
JK
335}
336
9c156f9d
IM
337static void apic_common_unrealize(DeviceState *dev, Error **errp)
338{
339 APICCommonState *s = APIC_COMMON(dev);
340 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
341
f6e98444 342 vmstate_unregister(NULL, &vmstate_apic_common, s);
9c156f9d
IM
343 info->unrealize(dev, errp);
344
345 if (apic_report_tpr_access && info->enable_tpr_reporting) {
346 info->enable_tpr_reporting(s, false);
347 }
348}
349
c2c00148
PD
350static int apic_pre_load(void *opaque)
351{
352 APICCommonState *s = APIC_COMMON(opaque);
353
354 /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
355 * so that's what apic_common_sipi_needed checks for. Reset to
356 * the value that is assumed when the apic_sipi subsection is
357 * absent.
358 */
359 s->wait_for_sipi = 0;
360 return 0;
361}
362
e5ad936b
JK
363static void apic_dispatch_pre_save(void *opaque)
364{
365 APICCommonState *s = APIC_COMMON(opaque);
366 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
367
368 if (info->pre_save) {
369 info->pre_save(s);
370 }
371}
372
7a380ca3
JK
373static int apic_dispatch_post_load(void *opaque, int version_id)
374{
999e12bb
AL
375 APICCommonState *s = APIC_COMMON(opaque);
376 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
7a380ca3
JK
377
378 if (info->post_load) {
379 info->post_load(s);
380 }
381 return 0;
382}
383
c2c00148
PD
384static bool apic_common_sipi_needed(void *opaque)
385{
386 APICCommonState *s = APIC_COMMON(opaque);
387 return s->wait_for_sipi != 0;
388}
389
390static const VMStateDescription vmstate_apic_common_sipi = {
391 .name = "apic_sipi",
392 .version_id = 1,
393 .minimum_version_id = 1,
5cd8cada 394 .needed = apic_common_sipi_needed,
c2c00148
PD
395 .fields = (VMStateField[]) {
396 VMSTATE_INT32(sipi_vector, APICCommonState),
397 VMSTATE_INT32(wait_for_sipi, APICCommonState),
398 VMSTATE_END_OF_LIST()
399 }
400};
401
dae01685
JK
402static const VMStateDescription vmstate_apic_common = {
403 .name = "apic",
404 .version_id = 3,
405 .minimum_version_id = 3,
406 .minimum_version_id_old = 1,
407 .load_state_old = apic_load_old,
c2c00148 408 .pre_load = apic_pre_load,
e5ad936b 409 .pre_save = apic_dispatch_pre_save,
7a380ca3 410 .post_load = apic_dispatch_post_load,
dae01685
JK
411 .fields = (VMStateField[]) {
412 VMSTATE_UINT32(apicbase, APICCommonState),
413 VMSTATE_UINT8(id, APICCommonState),
414 VMSTATE_UINT8(arb_id, APICCommonState),
415 VMSTATE_UINT8(tpr, APICCommonState),
416 VMSTATE_UINT32(spurious_vec, APICCommonState),
417 VMSTATE_UINT8(log_dest, APICCommonState),
418 VMSTATE_UINT8(dest_mode, APICCommonState),
419 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
420 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
421 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
422 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
423 VMSTATE_UINT32(esr, APICCommonState),
424 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
425 VMSTATE_UINT32(divide_conf, APICCommonState),
426 VMSTATE_INT32(count_shift, APICCommonState),
427 VMSTATE_UINT32(initial_count, APICCommonState),
428 VMSTATE_INT64(initial_count_load_time, APICCommonState),
429 VMSTATE_INT64(next_time, APICCommonState),
7a380ca3
JK
430 VMSTATE_INT64(timer_expiry,
431 APICCommonState), /* open-coded timer state */
dae01685 432 VMSTATE_END_OF_LIST()
c2c00148 433 },
5cd8cada
JQ
434 .subsections = (const VMStateDescription*[]) {
435 &vmstate_apic_common_sipi,
436 NULL
dae01685
JK
437 }
438};
439
440static Property apic_properties_common[] = {
aa93200b 441 DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
e5ad936b
JK
442 DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
443 true),
f6e98444
IM
444 DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
445 false),
dae01685
JK
446 DEFINE_PROP_END_OF_LIST(),
447};
448
33d7a288
IM
449static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
450 void *opaque, Error **errp)
451{
452 APICCommonState *s = APIC_COMMON(obj);
453 int64_t value;
454
455 value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
456 visit_type_int(v, name, &value, errp);
457}
458
459static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
460 void *opaque, Error **errp)
461{
462 APICCommonState *s = APIC_COMMON(obj);
463 DeviceState *dev = DEVICE(obj);
464 Error *local_err = NULL;
465 int64_t value;
466
467 if (dev->realized) {
468 qdev_prop_set_after_realize(dev, name, errp);
469 return;
470 }
471
472 visit_type_int(v, name, &value, &local_err);
473 if (local_err) {
474 error_propagate(errp, local_err);
475 return;
476 }
477
478 s->initial_apic_id = value;
479 s->id = (uint8_t)value;
480}
481
482static void apic_common_initfn(Object *obj)
483{
484 APICCommonState *s = APIC_COMMON(obj);
485
486 s->id = s->initial_apic_id = -1;
487 object_property_add(obj, "id", "int",
488 apic_common_get_id,
489 apic_common_set_id, NULL, NULL, NULL);
490}
491
999e12bb
AL
492static void apic_common_class_init(ObjectClass *klass, void *data)
493{
39bffca2 494 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 495
39bffca2 496 dc->reset = apic_reset_common;
39bffca2 497 dc->props = apic_properties_common;
46232aaa 498 dc->realize = apic_common_realize;
9c156f9d 499 dc->unrealize = apic_common_unrealize;
f37a4374
MA
500 /*
501 * Reason: APIC and CPU need to be wired up by
502 * x86_cpu_apic_create()
503 */
504 dc->cannot_instantiate_with_device_add_yet = true;
999e12bb 505}
dae01685 506
8c43a6f0 507static const TypeInfo apic_common_type = {
999e12bb 508 .name = TYPE_APIC_COMMON,
46232aaa 509 .parent = TYPE_DEVICE,
999e12bb 510 .instance_size = sizeof(APICCommonState),
33d7a288 511 .instance_init = apic_common_initfn,
999e12bb
AL
512 .class_size = sizeof(APICCommonClass),
513 .class_init = apic_common_class_init,
514 .abstract = true,
515};
516
d3b0c9e9 517static void apic_common_register_types(void)
999e12bb
AL
518{
519 type_register_static(&apic_common_type);
520}
521
d3b0c9e9 522type_init(apic_common_register_types)