hw/9pfs: Endian fixes for virtfs
[qemu.git] / hw / apic_common.c
blobc91f7d53918a6def783cd6cd8e5728823762118d
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 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
192 int i;
194 if (version_id > 2) {
195 return -EINVAL;
198 /* XXX: what if the base changes? (registered memory regions) */
199 qemu_get_be32s(f, &s->apicbase);
200 qemu_get_8s(f, &s->id);
201 qemu_get_8s(f, &s->arb_id);
202 qemu_get_8s(f, &s->tpr);
203 qemu_get_be32s(f, &s->spurious_vec);
204 qemu_get_8s(f, &s->log_dest);
205 qemu_get_8s(f, &s->dest_mode);
206 for (i = 0; i < 8; i++) {
207 qemu_get_be32s(f, &s->isr[i]);
208 qemu_get_be32s(f, &s->tmr[i]);
209 qemu_get_be32s(f, &s->irr[i]);
211 for (i = 0; i < APIC_LVT_NB; i++) {
212 qemu_get_be32s(f, &s->lvt[i]);
214 qemu_get_be32s(f, &s->esr);
215 qemu_get_be32s(f, &s->icr[0]);
216 qemu_get_be32s(f, &s->icr[1]);
217 qemu_get_be32s(f, &s->divide_conf);
218 s->count_shift = qemu_get_be32(f);
219 qemu_get_be32s(f, &s->initial_count);
220 s->initial_count_load_time = qemu_get_be64(f);
221 s->next_time = qemu_get_be64(f);
223 if (version_id >= 2) {
224 s->timer_expiry = qemu_get_be64(f);
227 if (info->post_load) {
228 info->post_load(s);
230 return 0;
233 static int apic_init_common(SysBusDevice *dev)
235 APICCommonState *s = APIC_COMMON(dev);
236 APICCommonClass *info;
237 static int apic_no;
239 if (apic_no >= MAX_APICS) {
240 return -1;
242 s->idx = apic_no++;
244 info = APIC_COMMON_GET_CLASS(s);
245 info->init(s);
247 sysbus_init_mmio(&s->busdev, &s->io_memory);
248 return 0;
251 static int apic_dispatch_post_load(void *opaque, int version_id)
253 APICCommonState *s = APIC_COMMON(opaque);
254 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
256 if (info->post_load) {
257 info->post_load(s);
259 return 0;
262 static const VMStateDescription vmstate_apic_common = {
263 .name = "apic",
264 .version_id = 3,
265 .minimum_version_id = 3,
266 .minimum_version_id_old = 1,
267 .load_state_old = apic_load_old,
268 .post_load = apic_dispatch_post_load,
269 .fields = (VMStateField[]) {
270 VMSTATE_UINT32(apicbase, APICCommonState),
271 VMSTATE_UINT8(id, APICCommonState),
272 VMSTATE_UINT8(arb_id, APICCommonState),
273 VMSTATE_UINT8(tpr, APICCommonState),
274 VMSTATE_UINT32(spurious_vec, APICCommonState),
275 VMSTATE_UINT8(log_dest, APICCommonState),
276 VMSTATE_UINT8(dest_mode, APICCommonState),
277 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
278 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
279 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
280 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
281 VMSTATE_UINT32(esr, APICCommonState),
282 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
283 VMSTATE_UINT32(divide_conf, APICCommonState),
284 VMSTATE_INT32(count_shift, APICCommonState),
285 VMSTATE_UINT32(initial_count, APICCommonState),
286 VMSTATE_INT64(initial_count_load_time, APICCommonState),
287 VMSTATE_INT64(next_time, APICCommonState),
288 VMSTATE_INT64(timer_expiry,
289 APICCommonState), /* open-coded timer state */
290 VMSTATE_END_OF_LIST()
294 static Property apic_properties_common[] = {
295 DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
296 DEFINE_PROP_PTR("cpu_env", APICCommonState, cpu_env),
297 DEFINE_PROP_END_OF_LIST(),
300 static void apic_common_class_init(ObjectClass *klass, void *data)
302 SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
303 DeviceClass *dc = DEVICE_CLASS(klass);
305 dc->vmsd = &vmstate_apic_common;
306 dc->reset = apic_reset_common;
307 dc->no_user = 1;
308 dc->props = apic_properties_common;
309 sc->init = apic_init_common;
312 static TypeInfo apic_common_type = {
313 .name = TYPE_APIC_COMMON,
314 .parent = TYPE_SYS_BUS_DEVICE,
315 .instance_size = sizeof(APICCommonState),
316 .class_size = sizeof(APICCommonClass),
317 .class_init = apic_common_class_init,
318 .abstract = true,
321 static void register_types(void)
323 type_register_static(&apic_common_type);
326 type_init(register_types)