2 * APIC support - common bits of emulated and KVM kernel model
4 * Copyright (c) 2004-2005 Fabrice Bellard
5 * Copyright (c) 2011 Jan Kiszka, Siemens AG
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.
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.
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/>
20 #include "qemu/osdep.h"
21 #include "qemu/error-report.h"
22 #include "qapi/error.h"
23 #include "qemu-common.h"
25 #include "qapi/visitor.h"
26 #include "hw/i386/apic.h"
27 #include "hw/i386/apic_internal.h"
29 #include "sysemu/kvm.h"
31 #include "hw/sysbus.h"
33 static int apic_irq_delivered
;
34 bool apic_report_tpr_access
;
36 void cpu_set_apic_base(DeviceState
*dev
, uint64_t val
)
38 trace_cpu_set_apic_base(val
);
41 APICCommonState
*s
= APIC_COMMON(dev
);
42 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
43 info
->set_base(s
, val
);
47 uint64_t cpu_get_apic_base(DeviceState
*dev
)
50 APICCommonState
*s
= APIC_COMMON(dev
);
51 trace_cpu_get_apic_base((uint64_t)s
->apicbase
);
54 trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP
);
55 return MSR_IA32_APICBASE_BSP
;
59 void cpu_set_apic_tpr(DeviceState
*dev
, uint8_t val
)
62 APICCommonClass
*info
;
69 info
= APIC_COMMON_GET_CLASS(s
);
71 info
->set_tpr(s
, val
);
74 uint8_t cpu_get_apic_tpr(DeviceState
*dev
)
77 APICCommonClass
*info
;
84 info
= APIC_COMMON_GET_CLASS(s
);
86 return info
->get_tpr(s
);
89 void apic_enable_tpr_access_reporting(DeviceState
*dev
, bool enable
)
91 APICCommonState
*s
= APIC_COMMON(dev
);
92 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
94 apic_report_tpr_access
= enable
;
95 if (info
->enable_tpr_reporting
) {
96 info
->enable_tpr_reporting(s
, enable
);
100 void apic_enable_vapic(DeviceState
*dev
, hwaddr paddr
)
102 APICCommonState
*s
= APIC_COMMON(dev
);
103 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
105 s
->vapic_paddr
= paddr
;
106 info
->vapic_base_update(s
);
109 void apic_handle_tpr_access_report(DeviceState
*dev
, target_ulong ip
,
112 APICCommonState
*s
= APIC_COMMON(dev
);
114 vapic_report_tpr_access(s
->vapic
, CPU(s
->cpu
), ip
, access
);
117 void apic_report_irq_delivered(int delivered
)
119 apic_irq_delivered
+= delivered
;
121 trace_apic_report_irq_delivered(apic_irq_delivered
);
124 void apic_reset_irq_delivered(void)
126 /* Copy this into a local variable to encourage gcc to emit a plain
127 * register for a sys/sdt.h marker. For details on this workaround, see:
128 * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
130 volatile int a_i_d
= apic_irq_delivered
;
131 trace_apic_reset_irq_delivered(a_i_d
);
133 apic_irq_delivered
= 0;
136 int apic_get_irq_delivered(void)
138 trace_apic_get_irq_delivered(apic_irq_delivered
);
140 return apic_irq_delivered
;
143 void apic_deliver_nmi(DeviceState
*dev
)
145 APICCommonState
*s
= APIC_COMMON(dev
);
146 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
148 info
->external_nmi(s
);
151 bool apic_next_timer(APICCommonState
*s
, int64_t current_time
)
155 /* We need to store the timer state separately to support APIC
156 * implementations that maintain a non-QEMU timer, e.g. inside the
157 * host kernel. This open-coded state allows us to migrate between
159 s
->timer_expiry
= -1;
161 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
) {
165 d
= (current_time
- s
->initial_count_load_time
) >> s
->count_shift
;
167 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
168 if (!s
->initial_count
) {
171 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) *
172 ((uint64_t)s
->initial_count
+ 1);
174 if (d
>= s
->initial_count
) {
177 d
= (uint64_t)s
->initial_count
+ 1;
179 s
->next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
180 s
->timer_expiry
= s
->next_time
;
184 void apic_init_reset(DeviceState
*dev
)
187 APICCommonClass
*info
;
193 s
= APIC_COMMON(dev
);
195 s
->spurious_vec
= 0xff;
198 memset(s
->isr
, 0, sizeof(s
->isr
));
199 memset(s
->tmr
, 0, sizeof(s
->tmr
));
200 memset(s
->irr
, 0, sizeof(s
->irr
));
201 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
202 s
->lvt
[i
] = APIC_LVT_MASKED
;
205 memset(s
->icr
, 0, sizeof(s
->icr
));
208 s
->initial_count
= 0;
209 s
->initial_count_load_time
= 0;
211 s
->wait_for_sipi
= !cpu_is_bsp(s
->cpu
);
216 s
->timer_expiry
= -1;
218 info
= APIC_COMMON_GET_CLASS(s
);
224 void apic_designate_bsp(DeviceState
*dev
, bool bsp
)
230 APICCommonState
*s
= APIC_COMMON(dev
);
232 s
->apicbase
|= MSR_IA32_APICBASE_BSP
;
234 s
->apicbase
&= ~MSR_IA32_APICBASE_BSP
;
238 static void apic_reset_common(DeviceState
*dev
)
240 APICCommonState
*s
= APIC_COMMON(dev
);
241 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
244 bsp
= s
->apicbase
& MSR_IA32_APICBASE_BSP
;
245 s
->apicbase
= APIC_DEFAULT_ADDRESS
| bsp
| MSR_IA32_APICBASE_ENABLE
;
246 s
->id
= s
->initial_apic_id
;
249 info
->vapic_base_update(s
);
251 apic_init_reset(dev
);
254 /* This function is only used for old state version 1 and 2 */
255 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
257 APICCommonState
*s
= opaque
;
258 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
261 if (version_id
> 2) {
265 /* XXX: what if the base changes? (registered memory regions) */
266 qemu_get_be32s(f
, &s
->apicbase
);
267 qemu_get_8s(f
, &s
->id
);
268 qemu_get_8s(f
, &s
->arb_id
);
269 qemu_get_8s(f
, &s
->tpr
);
270 qemu_get_be32s(f
, &s
->spurious_vec
);
271 qemu_get_8s(f
, &s
->log_dest
);
272 qemu_get_8s(f
, &s
->dest_mode
);
273 for (i
= 0; i
< 8; i
++) {
274 qemu_get_be32s(f
, &s
->isr
[i
]);
275 qemu_get_be32s(f
, &s
->tmr
[i
]);
276 qemu_get_be32s(f
, &s
->irr
[i
]);
278 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
279 qemu_get_be32s(f
, &s
->lvt
[i
]);
281 qemu_get_be32s(f
, &s
->esr
);
282 qemu_get_be32s(f
, &s
->icr
[0]);
283 qemu_get_be32s(f
, &s
->icr
[1]);
284 qemu_get_be32s(f
, &s
->divide_conf
);
285 s
->count_shift
= qemu_get_be32(f
);
286 qemu_get_be32s(f
, &s
->initial_count
);
287 s
->initial_count_load_time
= qemu_get_be64(f
);
288 s
->next_time
= qemu_get_be64(f
);
290 if (version_id
>= 2) {
291 s
->timer_expiry
= qemu_get_be64(f
);
294 if (info
->post_load
) {
300 static const VMStateDescription vmstate_apic_common
;
302 static void apic_common_realize(DeviceState
*dev
, Error
**errp
)
304 APICCommonState
*s
= APIC_COMMON(dev
);
305 APICCommonClass
*info
;
306 static DeviceState
*vapic
;
307 int instance_id
= s
->id
;
309 info
= APIC_COMMON_GET_CLASS(s
);
310 info
->realize(dev
, errp
);
312 /* Note: We need at least 1M to map the VAPIC option ROM */
313 if (!vapic
&& s
->vapic_control
& VAPIC_ENABLE_MASK
&&
314 ram_size
>= 1024 * 1024) {
315 vapic
= sysbus_create_simple("kvmvapic", -1, NULL
);
318 if (apic_report_tpr_access
&& info
->enable_tpr_reporting
) {
319 info
->enable_tpr_reporting(s
, true);
322 if (s
->legacy_instance_id
) {
325 vmstate_register_with_alias_id(NULL
, instance_id
, &vmstate_apic_common
,
329 static void apic_common_unrealize(DeviceState
*dev
, Error
**errp
)
331 APICCommonState
*s
= APIC_COMMON(dev
);
332 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
334 vmstate_unregister(NULL
, &vmstate_apic_common
, s
);
335 info
->unrealize(dev
, errp
);
337 if (apic_report_tpr_access
&& info
->enable_tpr_reporting
) {
338 info
->enable_tpr_reporting(s
, false);
342 static int apic_pre_load(void *opaque
)
344 APICCommonState
*s
= APIC_COMMON(opaque
);
346 /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
347 * so that's what apic_common_sipi_needed checks for. Reset to
348 * the value that is assumed when the apic_sipi subsection is
351 s
->wait_for_sipi
= 0;
355 static void apic_dispatch_pre_save(void *opaque
)
357 APICCommonState
*s
= APIC_COMMON(opaque
);
358 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
360 if (info
->pre_save
) {
365 static int apic_dispatch_post_load(void *opaque
, int version_id
)
367 APICCommonState
*s
= APIC_COMMON(opaque
);
368 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
370 if (info
->post_load
) {
376 static bool apic_common_sipi_needed(void *opaque
)
378 APICCommonState
*s
= APIC_COMMON(opaque
);
379 return s
->wait_for_sipi
!= 0;
382 static const VMStateDescription vmstate_apic_common_sipi
= {
385 .minimum_version_id
= 1,
386 .needed
= apic_common_sipi_needed
,
387 .fields
= (VMStateField
[]) {
388 VMSTATE_INT32(sipi_vector
, APICCommonState
),
389 VMSTATE_INT32(wait_for_sipi
, APICCommonState
),
390 VMSTATE_END_OF_LIST()
394 static const VMStateDescription vmstate_apic_common
= {
397 .minimum_version_id
= 3,
398 .minimum_version_id_old
= 1,
399 .load_state_old
= apic_load_old
,
400 .pre_load
= apic_pre_load
,
401 .pre_save
= apic_dispatch_pre_save
,
402 .post_load
= apic_dispatch_post_load
,
403 .fields
= (VMStateField
[]) {
404 VMSTATE_UINT32(apicbase
, APICCommonState
),
405 VMSTATE_UINT8(id
, APICCommonState
),
406 VMSTATE_UINT8(arb_id
, APICCommonState
),
407 VMSTATE_UINT8(tpr
, APICCommonState
),
408 VMSTATE_UINT32(spurious_vec
, APICCommonState
),
409 VMSTATE_UINT8(log_dest
, APICCommonState
),
410 VMSTATE_UINT8(dest_mode
, APICCommonState
),
411 VMSTATE_UINT32_ARRAY(isr
, APICCommonState
, 8),
412 VMSTATE_UINT32_ARRAY(tmr
, APICCommonState
, 8),
413 VMSTATE_UINT32_ARRAY(irr
, APICCommonState
, 8),
414 VMSTATE_UINT32_ARRAY(lvt
, APICCommonState
, APIC_LVT_NB
),
415 VMSTATE_UINT32(esr
, APICCommonState
),
416 VMSTATE_UINT32_ARRAY(icr
, APICCommonState
, 2),
417 VMSTATE_UINT32(divide_conf
, APICCommonState
),
418 VMSTATE_INT32(count_shift
, APICCommonState
),
419 VMSTATE_UINT32(initial_count
, APICCommonState
),
420 VMSTATE_INT64(initial_count_load_time
, APICCommonState
),
421 VMSTATE_INT64(next_time
, APICCommonState
),
422 VMSTATE_INT64(timer_expiry
,
423 APICCommonState
), /* open-coded timer state */
424 VMSTATE_END_OF_LIST()
426 .subsections
= (const VMStateDescription
*[]) {
427 &vmstate_apic_common_sipi
,
432 static Property apic_properties_common
[] = {
433 DEFINE_PROP_UINT8("version", APICCommonState
, version
, 0x14),
434 DEFINE_PROP_BIT("vapic", APICCommonState
, vapic_control
, VAPIC_ENABLE_BIT
,
436 DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState
, legacy_instance_id
,
438 DEFINE_PROP_END_OF_LIST(),
441 static void apic_common_get_id(Object
*obj
, Visitor
*v
, const char *name
,
442 void *opaque
, Error
**errp
)
444 APICCommonState
*s
= APIC_COMMON(obj
);
447 value
= s
->apicbase
& MSR_IA32_APICBASE_EXTD
? s
->initial_apic_id
: s
->id
;
448 visit_type_int(v
, name
, &value
, errp
);
451 static void apic_common_set_id(Object
*obj
, Visitor
*v
, const char *name
,
452 void *opaque
, Error
**errp
)
454 APICCommonState
*s
= APIC_COMMON(obj
);
455 DeviceState
*dev
= DEVICE(obj
);
456 Error
*local_err
= NULL
;
460 qdev_prop_set_after_realize(dev
, name
, errp
);
464 visit_type_int(v
, name
, &value
, &local_err
);
466 error_propagate(errp
, local_err
);
470 s
->initial_apic_id
= value
;
471 s
->id
= (uint8_t)value
;
474 static void apic_common_initfn(Object
*obj
)
476 APICCommonState
*s
= APIC_COMMON(obj
);
478 s
->id
= s
->initial_apic_id
= -1;
479 object_property_add(obj
, "id", "int",
481 apic_common_set_id
, NULL
, NULL
, NULL
);
484 static void apic_common_class_init(ObjectClass
*klass
, void *data
)
486 DeviceClass
*dc
= DEVICE_CLASS(klass
);
488 dc
->reset
= apic_reset_common
;
489 dc
->props
= apic_properties_common
;
490 dc
->realize
= apic_common_realize
;
491 dc
->unrealize
= apic_common_unrealize
;
493 * Reason: APIC and CPU need to be wired up by
494 * x86_cpu_apic_create()
496 dc
->cannot_instantiate_with_device_add_yet
= true;
499 static const TypeInfo apic_common_type
= {
500 .name
= TYPE_APIC_COMMON
,
501 .parent
= TYPE_DEVICE
,
502 .instance_size
= sizeof(APICCommonState
),
503 .instance_init
= apic_common_initfn
,
504 .class_size
= sizeof(APICCommonClass
),
505 .class_init
= apic_common_class_init
,
509 static void apic_common_register_types(void)
511 type_register_static(&apic_common_type
);
514 type_init(apic_common_register_types
)