ppc4xx_pci: QOM'ify ppc4xx PCI host bridge
[qemu/agraf.git] / hw / apic_common.c
blob371f95d909f4ee1edf514e3121a07bb239273cb6
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(MSR_IA32_APICBASE_BSP);
47 return MSR_IA32_APICBASE_BSP;
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 void apic_designate_bsp(DeviceState *d)
206 if (d == NULL) {
207 return;
210 APICCommonState *s = APIC_COMMON(d);
211 s->apicbase |= MSR_IA32_APICBASE_BSP;
214 static void apic_reset_common(DeviceState *d)
216 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
217 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
218 bool bsp;
220 bsp = cpu_is_bsp(x86_env_get_cpu(s->cpu_env));
221 s->apicbase = 0xfee00000 |
222 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
224 s->vapic_paddr = 0;
225 info->vapic_base_update(s);
227 apic_init_reset(d);
229 if (bsp) {
231 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
232 * time typically by BIOS, so PIC interrupt can be delivered to the
233 * processor when local APIC is enabled.
235 s->lvt[APIC_LVT_LINT0] = 0x700;
239 /* This function is only used for old state version 1 and 2 */
240 static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
242 APICCommonState *s = opaque;
243 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
244 int i;
246 if (version_id > 2) {
247 return -EINVAL;
250 /* XXX: what if the base changes? (registered memory regions) */
251 qemu_get_be32s(f, &s->apicbase);
252 qemu_get_8s(f, &s->id);
253 qemu_get_8s(f, &s->arb_id);
254 qemu_get_8s(f, &s->tpr);
255 qemu_get_be32s(f, &s->spurious_vec);
256 qemu_get_8s(f, &s->log_dest);
257 qemu_get_8s(f, &s->dest_mode);
258 for (i = 0; i < 8; i++) {
259 qemu_get_be32s(f, &s->isr[i]);
260 qemu_get_be32s(f, &s->tmr[i]);
261 qemu_get_be32s(f, &s->irr[i]);
263 for (i = 0; i < APIC_LVT_NB; i++) {
264 qemu_get_be32s(f, &s->lvt[i]);
266 qemu_get_be32s(f, &s->esr);
267 qemu_get_be32s(f, &s->icr[0]);
268 qemu_get_be32s(f, &s->icr[1]);
269 qemu_get_be32s(f, &s->divide_conf);
270 s->count_shift = qemu_get_be32(f);
271 qemu_get_be32s(f, &s->initial_count);
272 s->initial_count_load_time = qemu_get_be64(f);
273 s->next_time = qemu_get_be64(f);
275 if (version_id >= 2) {
276 s->timer_expiry = qemu_get_be64(f);
279 if (info->post_load) {
280 info->post_load(s);
282 return 0;
285 static int apic_init_common(SysBusDevice *dev)
287 APICCommonState *s = APIC_COMMON(dev);
288 APICCommonClass *info;
289 static DeviceState *vapic;
290 static int apic_no;
292 if (apic_no >= MAX_APICS) {
293 return -1;
295 s->idx = apic_no++;
297 info = APIC_COMMON_GET_CLASS(s);
298 info->init(s);
300 sysbus_init_mmio(dev, &s->io_memory);
302 /* Note: We need at least 1M to map the VAPIC option ROM */
303 if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
304 ram_size >= 1024 * 1024) {
305 vapic = sysbus_create_simple("kvmvapic", -1, NULL);
307 s->vapic = vapic;
308 if (apic_report_tpr_access && info->enable_tpr_reporting) {
309 info->enable_tpr_reporting(s, true);
312 return 0;
315 static void apic_dispatch_pre_save(void *opaque)
317 APICCommonState *s = APIC_COMMON(opaque);
318 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
320 if (info->pre_save) {
321 info->pre_save(s);
325 static int apic_dispatch_post_load(void *opaque, int version_id)
327 APICCommonState *s = APIC_COMMON(opaque);
328 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
330 if (info->post_load) {
331 info->post_load(s);
333 return 0;
336 static const VMStateDescription vmstate_apic_common = {
337 .name = "apic",
338 .version_id = 3,
339 .minimum_version_id = 3,
340 .minimum_version_id_old = 1,
341 .load_state_old = apic_load_old,
342 .pre_save = apic_dispatch_pre_save,
343 .post_load = apic_dispatch_post_load,
344 .fields = (VMStateField[]) {
345 VMSTATE_UINT32(apicbase, APICCommonState),
346 VMSTATE_UINT8(id, APICCommonState),
347 VMSTATE_UINT8(arb_id, APICCommonState),
348 VMSTATE_UINT8(tpr, APICCommonState),
349 VMSTATE_UINT32(spurious_vec, APICCommonState),
350 VMSTATE_UINT8(log_dest, APICCommonState),
351 VMSTATE_UINT8(dest_mode, APICCommonState),
352 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
353 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
354 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
355 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
356 VMSTATE_UINT32(esr, APICCommonState),
357 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
358 VMSTATE_UINT32(divide_conf, APICCommonState),
359 VMSTATE_INT32(count_shift, APICCommonState),
360 VMSTATE_UINT32(initial_count, APICCommonState),
361 VMSTATE_INT64(initial_count_load_time, APICCommonState),
362 VMSTATE_INT64(next_time, APICCommonState),
363 VMSTATE_INT64(timer_expiry,
364 APICCommonState), /* open-coded timer state */
365 VMSTATE_END_OF_LIST()
369 static Property apic_properties_common[] = {
370 DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
371 DEFINE_PROP_PTR("cpu_env", APICCommonState, cpu_env),
372 DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
373 true),
374 DEFINE_PROP_END_OF_LIST(),
377 static void apic_common_class_init(ObjectClass *klass, void *data)
379 SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
380 DeviceClass *dc = DEVICE_CLASS(klass);
382 dc->vmsd = &vmstate_apic_common;
383 dc->reset = apic_reset_common;
384 dc->no_user = 1;
385 dc->props = apic_properties_common;
386 sc->init = apic_init_common;
389 static TypeInfo apic_common_type = {
390 .name = TYPE_APIC_COMMON,
391 .parent = TYPE_SYS_BUS_DEVICE,
392 .instance_size = sizeof(APICCommonState),
393 .class_size = sizeof(APICCommonClass),
394 .class_init = apic_common_class_init,
395 .abstract = true,
398 static void register_types(void)
400 type_register_static(&apic_common_type);
403 type_init(register_types)