Merge remote-tracking branch 'mdroth/qga-pull-5-15-12' into staging
[qemu/opensuse.git] / hw / apic_common.c
blob60b82596e7b6f3e8701c2b3246f853cf89804c90
1 /*
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 "apic.h"
21 #include "apic_internal.h"
22 #include "trace.h"
23 #include "kvm.h"
25 static int apic_irq_delivered;
26 bool apic_report_tpr_access;
28 void cpu_set_apic_base(DeviceState *d, uint64_t val)
30 trace_cpu_set_apic_base(val);
32 if (d) {
33 APICCommonState *s = APIC_COMMON(d);
34 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
35 info->set_base(s, val);
39 uint64_t cpu_get_apic_base(DeviceState *d)
41 if (d) {
42 APICCommonState *s = APIC_COMMON(d);
43 trace_cpu_get_apic_base((uint64_t)s->apicbase);
44 return s->apicbase;
45 } else {
46 trace_cpu_get_apic_base(0);
47 return 0;
51 void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
53 APICCommonState *s;
54 APICCommonClass *info;
56 if (!d) {
57 return;
60 s = APIC_COMMON(d);
61 info = APIC_COMMON_GET_CLASS(s);
63 info->set_tpr(s, val);
66 uint8_t cpu_get_apic_tpr(DeviceState *d)
68 APICCommonState *s;
69 APICCommonClass *info;
71 if (!d) {
72 return 0;
75 s = APIC_COMMON(d);
76 info = APIC_COMMON_GET_CLASS(s);
78 return info->get_tpr(s);
81 void apic_enable_tpr_access_reporting(DeviceState *d, bool enable)
83 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
84 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
86 apic_report_tpr_access = enable;
87 if (info->enable_tpr_reporting) {
88 info->enable_tpr_reporting(s, enable);
92 void apic_enable_vapic(DeviceState *d, target_phys_addr_t paddr)
94 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
95 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
97 s->vapic_paddr = paddr;
98 info->vapic_base_update(s);
101 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
102 TPRAccess access)
104 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
106 vapic_report_tpr_access(s->vapic, s->cpu_env, ip, access);
109 void apic_report_irq_delivered(int delivered)
111 apic_irq_delivered += delivered;
113 trace_apic_report_irq_delivered(apic_irq_delivered);
116 void apic_reset_irq_delivered(void)
118 trace_apic_reset_irq_delivered(apic_irq_delivered);
120 apic_irq_delivered = 0;
123 int apic_get_irq_delivered(void)
125 trace_apic_get_irq_delivered(apic_irq_delivered);
127 return apic_irq_delivered;
130 void apic_deliver_nmi(DeviceState *d)
132 APICCommonState *s = APIC_COMMON(d);
133 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
135 info->external_nmi(s);
138 bool apic_next_timer(APICCommonState *s, int64_t current_time)
140 int64_t d;
142 /* We need to store the timer state separately to support APIC
143 * implementations that maintain a non-QEMU timer, e.g. inside the
144 * host kernel. This open-coded state allows us to migrate between
145 * both models. */
146 s->timer_expiry = -1;
148 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
149 return false;
152 d = (current_time - s->initial_count_load_time) >> s->count_shift;
154 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
155 if (!s->initial_count) {
156 return false;
158 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
159 ((uint64_t)s->initial_count + 1);
160 } else {
161 if (d >= s->initial_count) {
162 return false;
164 d = (uint64_t)s->initial_count + 1;
166 s->next_time = s->initial_count_load_time + (d << s->count_shift);
167 s->timer_expiry = s->next_time;
168 return true;
171 void apic_init_reset(DeviceState *d)
173 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
174 int i;
176 if (!s) {
177 return;
179 s->tpr = 0;
180 s->spurious_vec = 0xff;
181 s->log_dest = 0;
182 s->dest_mode = 0xf;
183 memset(s->isr, 0, sizeof(s->isr));
184 memset(s->tmr, 0, sizeof(s->tmr));
185 memset(s->irr, 0, sizeof(s->irr));
186 for (i = 0; i < APIC_LVT_NB; i++) {
187 s->lvt[i] = APIC_LVT_MASKED;
189 s->esr = 0;
190 memset(s->icr, 0, sizeof(s->icr));
191 s->divide_conf = 0;
192 s->count_shift = 0;
193 s->initial_count = 0;
194 s->initial_count_load_time = 0;
195 s->next_time = 0;
196 s->wait_for_sipi = 1;
198 if (s->timer) {
199 qemu_del_timer(s->timer);
201 s->timer_expiry = -1;
204 static void apic_reset_common(DeviceState *d)
206 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
207 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
208 bool bsp;
210 bsp = cpu_is_bsp(s->cpu_env);
211 s->apicbase = 0xfee00000 |
212 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
214 s->vapic_paddr = 0;
215 info->vapic_base_update(s);
217 apic_init_reset(d);
219 if (bsp) {
221 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
222 * time typically by BIOS, so PIC interrupt can be delivered to the
223 * processor when local APIC is enabled.
225 s->lvt[APIC_LVT_LINT0] = 0x700;
229 /* This function is only used for old state version 1 and 2 */
230 static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
232 APICCommonState *s = opaque;
233 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
234 int i;
236 if (version_id > 2) {
237 return -EINVAL;
240 /* XXX: what if the base changes? (registered memory regions) */
241 qemu_get_be32s(f, &s->apicbase);
242 qemu_get_8s(f, &s->id);
243 qemu_get_8s(f, &s->arb_id);
244 qemu_get_8s(f, &s->tpr);
245 qemu_get_be32s(f, &s->spurious_vec);
246 qemu_get_8s(f, &s->log_dest);
247 qemu_get_8s(f, &s->dest_mode);
248 for (i = 0; i < 8; i++) {
249 qemu_get_be32s(f, &s->isr[i]);
250 qemu_get_be32s(f, &s->tmr[i]);
251 qemu_get_be32s(f, &s->irr[i]);
253 for (i = 0; i < APIC_LVT_NB; i++) {
254 qemu_get_be32s(f, &s->lvt[i]);
256 qemu_get_be32s(f, &s->esr);
257 qemu_get_be32s(f, &s->icr[0]);
258 qemu_get_be32s(f, &s->icr[1]);
259 qemu_get_be32s(f, &s->divide_conf);
260 s->count_shift = qemu_get_be32(f);
261 qemu_get_be32s(f, &s->initial_count);
262 s->initial_count_load_time = qemu_get_be64(f);
263 s->next_time = qemu_get_be64(f);
265 if (version_id >= 2) {
266 s->timer_expiry = qemu_get_be64(f);
269 if (info->post_load) {
270 info->post_load(s);
272 return 0;
275 static int apic_init_common(SysBusDevice *dev)
277 APICCommonState *s = APIC_COMMON(dev);
278 APICCommonClass *info;
279 static DeviceState *vapic;
280 static int apic_no;
282 if (apic_no >= MAX_APICS) {
283 return -1;
285 s->idx = apic_no++;
287 info = APIC_COMMON_GET_CLASS(s);
288 info->init(s);
290 sysbus_init_mmio(dev, &s->io_memory);
292 if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK) {
293 vapic = sysbus_create_simple("kvmvapic", -1, NULL);
295 s->vapic = vapic;
296 if (apic_report_tpr_access && info->enable_tpr_reporting) {
297 info->enable_tpr_reporting(s, true);
300 return 0;
303 static void apic_dispatch_pre_save(void *opaque)
305 APICCommonState *s = APIC_COMMON(opaque);
306 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
308 if (info->pre_save) {
309 info->pre_save(s);
313 static int apic_dispatch_post_load(void *opaque, int version_id)
315 APICCommonState *s = APIC_COMMON(opaque);
316 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
318 if (info->post_load) {
319 info->post_load(s);
321 return 0;
324 static const VMStateDescription vmstate_apic_common = {
325 .name = "apic",
326 .version_id = 3,
327 .minimum_version_id = 3,
328 .minimum_version_id_old = 1,
329 .load_state_old = apic_load_old,
330 .pre_save = apic_dispatch_pre_save,
331 .post_load = apic_dispatch_post_load,
332 .fields = (VMStateField[]) {
333 VMSTATE_UINT32(apicbase, APICCommonState),
334 VMSTATE_UINT8(id, APICCommonState),
335 VMSTATE_UINT8(arb_id, APICCommonState),
336 VMSTATE_UINT8(tpr, APICCommonState),
337 VMSTATE_UINT32(spurious_vec, APICCommonState),
338 VMSTATE_UINT8(log_dest, APICCommonState),
339 VMSTATE_UINT8(dest_mode, APICCommonState),
340 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
341 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
342 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
343 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
344 VMSTATE_UINT32(esr, APICCommonState),
345 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
346 VMSTATE_UINT32(divide_conf, APICCommonState),
347 VMSTATE_INT32(count_shift, APICCommonState),
348 VMSTATE_UINT32(initial_count, APICCommonState),
349 VMSTATE_INT64(initial_count_load_time, APICCommonState),
350 VMSTATE_INT64(next_time, APICCommonState),
351 VMSTATE_INT64(timer_expiry,
352 APICCommonState), /* open-coded timer state */
353 VMSTATE_END_OF_LIST()
357 static Property apic_properties_common[] = {
358 DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
359 DEFINE_PROP_PTR("cpu_env", APICCommonState, cpu_env),
360 DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
361 true),
362 DEFINE_PROP_END_OF_LIST(),
365 static void apic_common_class_init(ObjectClass *klass, void *data)
367 SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
368 DeviceClass *dc = DEVICE_CLASS(klass);
370 dc->vmsd = &vmstate_apic_common;
371 dc->reset = apic_reset_common;
372 dc->no_user = 1;
373 dc->props = apic_properties_common;
374 sc->init = apic_init_common;
377 static TypeInfo apic_common_type = {
378 .name = TYPE_APIC_COMMON,
379 .parent = TYPE_SYS_BUS_DEVICE,
380 .instance_size = sizeof(APICCommonState),
381 .class_size = sizeof(APICCommonClass),
382 .class_init = apic_common_class_init,
383 .abstract = true,
386 static void register_types(void)
388 type_register_static(&apic_common_type);
391 type_init(register_types)