memory: drop AddressSpaceOps
[qemu-kvm.git] / hw / apic_common.c
blob26991b451603d8d11e2bda5d015fcbc74466a73f
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"
24 static int apic_irq_delivered;
26 void cpu_set_apic_base(DeviceState *d, uint64_t val)
28 trace_cpu_set_apic_base(val);
30 if (d) {
31 APICCommonState *s = APIC_COMMON(d);
32 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
33 info->set_base(s, val);
37 uint64_t cpu_get_apic_base(DeviceState *d)
39 if (d) {
40 APICCommonState *s = APIC_COMMON(d);
41 trace_cpu_get_apic_base((uint64_t)s->apicbase);
42 return s->apicbase;
43 } else {
44 trace_cpu_get_apic_base(0);
45 return 0;
49 void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
51 APICCommonState *s;
52 APICCommonClass *info;
54 if (!d) {
55 return;
58 s = APIC_COMMON(d);
59 info = APIC_COMMON_GET_CLASS(s);
61 info->set_tpr(s, val);
64 uint8_t cpu_get_apic_tpr(DeviceState *d)
66 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
68 return s ? s->tpr >> 4 : 0;
71 void apic_report_irq_delivered(int delivered)
73 apic_irq_delivered += delivered;
75 trace_apic_report_irq_delivered(apic_irq_delivered);
78 void apic_reset_irq_delivered(void)
80 trace_apic_reset_irq_delivered(apic_irq_delivered);
82 apic_irq_delivered = 0;
85 int apic_get_irq_delivered(void)
87 trace_apic_get_irq_delivered(apic_irq_delivered);
89 return apic_irq_delivered;
92 void apic_deliver_nmi(DeviceState *d)
94 APICCommonState *s = APIC_COMMON(d);
95 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
97 info->external_nmi(s);
100 bool apic_next_timer(APICCommonState *s, int64_t current_time)
102 int64_t d;
104 /* We need to store the timer state separately to support APIC
105 * implementations that maintain a non-QEMU timer, e.g. inside the
106 * host kernel. This open-coded state allows us to migrate between
107 * both models. */
108 s->timer_expiry = -1;
110 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
111 return false;
114 d = (current_time - s->initial_count_load_time) >> s->count_shift;
116 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
117 if (!s->initial_count) {
118 return false;
120 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
121 ((uint64_t)s->initial_count + 1);
122 } else {
123 if (d >= s->initial_count) {
124 return false;
126 d = (uint64_t)s->initial_count + 1;
128 s->next_time = s->initial_count_load_time + (d << s->count_shift);
129 s->timer_expiry = s->next_time;
130 return true;
133 void apic_init_reset(DeviceState *d)
135 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
136 int i;
138 if (!s) {
139 return;
141 s->tpr = 0;
142 s->spurious_vec = 0xff;
143 s->log_dest = 0;
144 s->dest_mode = 0xf;
145 memset(s->isr, 0, sizeof(s->isr));
146 memset(s->tmr, 0, sizeof(s->tmr));
147 memset(s->irr, 0, sizeof(s->irr));
148 for (i = 0; i < APIC_LVT_NB; i++) {
149 s->lvt[i] = APIC_LVT_MASKED;
151 s->esr = 0;
152 memset(s->icr, 0, sizeof(s->icr));
153 s->divide_conf = 0;
154 s->count_shift = 0;
155 s->initial_count = 0;
156 s->initial_count_load_time = 0;
157 s->next_time = 0;
158 s->wait_for_sipi = 1;
160 if (s->timer) {
161 qemu_del_timer(s->timer);
163 s->timer_expiry = -1;
166 static void apic_reset_common(DeviceState *d)
168 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
169 bool bsp;
171 bsp = cpu_is_bsp(s->cpu_env);
172 s->apicbase = 0xfee00000 |
173 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
175 apic_init_reset(d);
177 if (bsp) {
179 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
180 * time typically by BIOS, so PIC interrupt can be delivered to the
181 * processor when local APIC is enabled.
183 s->lvt[APIC_LVT_LINT0] = 0x700;
187 /* This function is only used for old state version 1 and 2 */
188 static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
190 APICCommonState *s = opaque;
191 int i;
193 if (version_id > 2) {
194 return -EINVAL;
197 /* XXX: what if the base changes? (registered memory regions) */
198 qemu_get_be32s(f, &s->apicbase);
199 qemu_get_8s(f, &s->id);
200 qemu_get_8s(f, &s->arb_id);
201 qemu_get_8s(f, &s->tpr);
202 qemu_get_be32s(f, &s->spurious_vec);
203 qemu_get_8s(f, &s->log_dest);
204 qemu_get_8s(f, &s->dest_mode);
205 for (i = 0; i < 8; i++) {
206 qemu_get_be32s(f, &s->isr[i]);
207 qemu_get_be32s(f, &s->tmr[i]);
208 qemu_get_be32s(f, &s->irr[i]);
210 for (i = 0; i < APIC_LVT_NB; i++) {
211 qemu_get_be32s(f, &s->lvt[i]);
213 qemu_get_be32s(f, &s->esr);
214 qemu_get_be32s(f, &s->icr[0]);
215 qemu_get_be32s(f, &s->icr[1]);
216 qemu_get_be32s(f, &s->divide_conf);
217 s->count_shift = qemu_get_be32(f);
218 qemu_get_be32s(f, &s->initial_count);
219 s->initial_count_load_time = qemu_get_be64(f);
220 s->next_time = qemu_get_be64(f);
222 if (version_id >= 2) {
223 qemu_get_timer(f, s->timer);
225 return 0;
228 static int apic_init_common(SysBusDevice *dev)
230 APICCommonState *s = APIC_COMMON(dev);
231 APICCommonClass *info;
232 static int apic_no;
234 if (apic_no >= MAX_APICS) {
235 return -1;
237 s->idx = apic_no++;
239 info = APIC_COMMON_GET_CLASS(s);
240 info->init(s);
242 sysbus_init_mmio(&s->busdev, &s->io_memory);
243 return 0;
246 static int apic_dispatch_post_load(void *opaque, int version_id)
248 APICCommonState *s = APIC_COMMON(opaque);
249 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
251 if (info->post_load) {
252 info->post_load(s);
254 return 0;
257 static const VMStateDescription vmstate_apic_common = {
258 .name = "apic",
259 .version_id = 3,
260 .minimum_version_id = 3,
261 .minimum_version_id_old = 1,
262 .load_state_old = apic_load_old,
263 .post_load = apic_dispatch_post_load,
264 .fields = (VMStateField[]) {
265 VMSTATE_UINT32(apicbase, APICCommonState),
266 VMSTATE_UINT8(id, APICCommonState),
267 VMSTATE_UINT8(arb_id, APICCommonState),
268 VMSTATE_UINT8(tpr, APICCommonState),
269 VMSTATE_UINT32(spurious_vec, APICCommonState),
270 VMSTATE_UINT8(log_dest, APICCommonState),
271 VMSTATE_UINT8(dest_mode, APICCommonState),
272 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
273 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
274 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
275 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
276 VMSTATE_UINT32(esr, APICCommonState),
277 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
278 VMSTATE_UINT32(divide_conf, APICCommonState),
279 VMSTATE_INT32(count_shift, APICCommonState),
280 VMSTATE_UINT32(initial_count, APICCommonState),
281 VMSTATE_INT64(initial_count_load_time, APICCommonState),
282 VMSTATE_INT64(next_time, APICCommonState),
283 VMSTATE_INT64(timer_expiry,
284 APICCommonState), /* open-coded timer state */
285 VMSTATE_END_OF_LIST()
289 static Property apic_properties_common[] = {
290 DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
291 DEFINE_PROP_PTR("cpu_env", APICCommonState, cpu_env),
292 DEFINE_PROP_END_OF_LIST(),
295 static void apic_common_class_init(ObjectClass *klass, void *data)
297 SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
298 DeviceClass *dc = DEVICE_CLASS(klass);
300 dc->vmsd = &vmstate_apic_common;
301 dc->reset = apic_reset_common;
302 dc->no_user = 1;
303 dc->props = apic_properties_common;
304 sc->init = apic_init_common;
307 static TypeInfo apic_common_type = {
308 .name = TYPE_APIC_COMMON,
309 .parent = TYPE_SYS_BUS_DEVICE,
310 .instance_size = sizeof(APICCommonState),
311 .class_size = sizeof(APICCommonClass),
312 .class_init = apic_common_class_init,
313 .abstract = true,
316 static void register_devices(void)
318 type_register_static(&apic_common_type);
321 device_init(register_devices);