linux-headers: Update to v3.10-rc5
[qemu/ar7.git] / hw / intc / apic_common.c
blobb03e904a7aac97096907a81cb7a97ba4b41985f6
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 "hw/i386/apic.h"
21 #include "hw/i386/apic_internal.h"
22 #include "trace.h"
23 #include "sysemu/kvm.h"
24 #include "hw/qdev.h"
25 #include "hw/sysbus.h"
27 static int apic_irq_delivered;
28 bool apic_report_tpr_access;
30 void cpu_set_apic_base(DeviceState *d, uint64_t val)
32 trace_cpu_set_apic_base(val);
34 if (d) {
35 APICCommonState *s = APIC_COMMON(d);
36 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
37 info->set_base(s, val);
41 uint64_t cpu_get_apic_base(DeviceState *d)
43 if (d) {
44 APICCommonState *s = APIC_COMMON(d);
45 trace_cpu_get_apic_base((uint64_t)s->apicbase);
46 return s->apicbase;
47 } else {
48 trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
49 return MSR_IA32_APICBASE_BSP;
53 void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
55 APICCommonState *s;
56 APICCommonClass *info;
58 if (!d) {
59 return;
62 s = APIC_COMMON(d);
63 info = APIC_COMMON_GET_CLASS(s);
65 info->set_tpr(s, val);
68 uint8_t cpu_get_apic_tpr(DeviceState *d)
70 APICCommonState *s;
71 APICCommonClass *info;
73 if (!d) {
74 return 0;
77 s = APIC_COMMON(d);
78 info = APIC_COMMON_GET_CLASS(s);
80 return info->get_tpr(s);
83 void apic_enable_tpr_access_reporting(DeviceState *d, bool enable)
85 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
86 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
88 apic_report_tpr_access = enable;
89 if (info->enable_tpr_reporting) {
90 info->enable_tpr_reporting(s, enable);
94 void apic_enable_vapic(DeviceState *d, hwaddr paddr)
96 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
97 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
99 s->vapic_paddr = paddr;
100 info->vapic_base_update(s);
103 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
104 TPRAccess access)
106 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
108 vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
111 void apic_report_irq_delivered(int delivered)
113 apic_irq_delivered += delivered;
115 trace_apic_report_irq_delivered(apic_irq_delivered);
118 void apic_reset_irq_delivered(void)
120 trace_apic_reset_irq_delivered(apic_irq_delivered);
122 apic_irq_delivered = 0;
125 int apic_get_irq_delivered(void)
127 trace_apic_get_irq_delivered(apic_irq_delivered);
129 return apic_irq_delivered;
132 void apic_deliver_nmi(DeviceState *d)
134 APICCommonState *s = APIC_COMMON(d);
135 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
137 info->external_nmi(s);
140 bool apic_next_timer(APICCommonState *s, int64_t current_time)
142 int64_t d;
144 /* We need to store the timer state separately to support APIC
145 * implementations that maintain a non-QEMU timer, e.g. inside the
146 * host kernel. This open-coded state allows us to migrate between
147 * both models. */
148 s->timer_expiry = -1;
150 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
151 return false;
154 d = (current_time - s->initial_count_load_time) >> s->count_shift;
156 if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
157 if (!s->initial_count) {
158 return false;
160 d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
161 ((uint64_t)s->initial_count + 1);
162 } else {
163 if (d >= s->initial_count) {
164 return false;
166 d = (uint64_t)s->initial_count + 1;
168 s->next_time = s->initial_count_load_time + (d << s->count_shift);
169 s->timer_expiry = s->next_time;
170 return true;
173 void apic_init_reset(DeviceState *d)
175 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
176 int i;
178 if (!s) {
179 return;
181 s->tpr = 0;
182 s->spurious_vec = 0xff;
183 s->log_dest = 0;
184 s->dest_mode = 0xf;
185 memset(s->isr, 0, sizeof(s->isr));
186 memset(s->tmr, 0, sizeof(s->tmr));
187 memset(s->irr, 0, sizeof(s->irr));
188 for (i = 0; i < APIC_LVT_NB; i++) {
189 s->lvt[i] = APIC_LVT_MASKED;
191 s->esr = 0;
192 memset(s->icr, 0, sizeof(s->icr));
193 s->divide_conf = 0;
194 s->count_shift = 0;
195 s->initial_count = 0;
196 s->initial_count_load_time = 0;
197 s->next_time = 0;
198 s->wait_for_sipi = 1;
200 if (s->timer) {
201 qemu_del_timer(s->timer);
203 s->timer_expiry = -1;
206 void apic_designate_bsp(DeviceState *d)
208 if (d == NULL) {
209 return;
212 APICCommonState *s = APIC_COMMON(d);
213 s->apicbase |= MSR_IA32_APICBASE_BSP;
216 static void apic_reset_common(DeviceState *d)
218 APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
219 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
220 bool bsp;
222 bsp = cpu_is_bsp(s->cpu);
223 s->apicbase = APIC_DEFAULT_ADDRESS |
224 (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
226 s->vapic_paddr = 0;
227 info->vapic_base_update(s);
229 apic_init_reset(d);
231 if (bsp) {
233 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
234 * time typically by BIOS, so PIC interrupt can be delivered to the
235 * processor when local APIC is enabled.
237 s->lvt[APIC_LVT_LINT0] = 0x700;
241 /* This function is only used for old state version 1 and 2 */
242 static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
244 APICCommonState *s = opaque;
245 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
246 int i;
248 if (version_id > 2) {
249 return -EINVAL;
252 /* XXX: what if the base changes? (registered memory regions) */
253 qemu_get_be32s(f, &s->apicbase);
254 qemu_get_8s(f, &s->id);
255 qemu_get_8s(f, &s->arb_id);
256 qemu_get_8s(f, &s->tpr);
257 qemu_get_be32s(f, &s->spurious_vec);
258 qemu_get_8s(f, &s->log_dest);
259 qemu_get_8s(f, &s->dest_mode);
260 for (i = 0; i < 8; i++) {
261 qemu_get_be32s(f, &s->isr[i]);
262 qemu_get_be32s(f, &s->tmr[i]);
263 qemu_get_be32s(f, &s->irr[i]);
265 for (i = 0; i < APIC_LVT_NB; i++) {
266 qemu_get_be32s(f, &s->lvt[i]);
268 qemu_get_be32s(f, &s->esr);
269 qemu_get_be32s(f, &s->icr[0]);
270 qemu_get_be32s(f, &s->icr[1]);
271 qemu_get_be32s(f, &s->divide_conf);
272 s->count_shift = qemu_get_be32(f);
273 qemu_get_be32s(f, &s->initial_count);
274 s->initial_count_load_time = qemu_get_be64(f);
275 s->next_time = qemu_get_be64(f);
277 if (version_id >= 2) {
278 s->timer_expiry = qemu_get_be64(f);
281 if (info->post_load) {
282 info->post_load(s);
284 return 0;
287 static int apic_init_common(ICCDevice *dev)
289 APICCommonState *s = APIC_COMMON(dev);
290 APICCommonClass *info;
291 static DeviceState *vapic;
292 static int apic_no;
293 static bool mmio_registered;
295 if (apic_no >= MAX_APICS) {
296 return -1;
298 s->idx = apic_no++;
300 info = APIC_COMMON_GET_CLASS(s);
301 info->init(s);
302 if (!mmio_registered) {
303 ICCBus *b = ICC_BUS(qdev_get_parent_bus(DEVICE(dev)));
304 memory_region_add_subregion(b->apic_address_space, 0, &s->io_memory);
305 mmio_registered = true;
308 /* Note: We need at least 1M to map the VAPIC option ROM */
309 if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
310 ram_size >= 1024 * 1024) {
311 vapic = sysbus_create_simple("kvmvapic", -1, NULL);
313 s->vapic = vapic;
314 if (apic_report_tpr_access && info->enable_tpr_reporting) {
315 info->enable_tpr_reporting(s, true);
318 return 0;
321 static void apic_dispatch_pre_save(void *opaque)
323 APICCommonState *s = APIC_COMMON(opaque);
324 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
326 if (info->pre_save) {
327 info->pre_save(s);
331 static int apic_dispatch_post_load(void *opaque, int version_id)
333 APICCommonState *s = APIC_COMMON(opaque);
334 APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
336 if (info->post_load) {
337 info->post_load(s);
339 return 0;
342 static const VMStateDescription vmstate_apic_common = {
343 .name = "apic",
344 .version_id = 3,
345 .minimum_version_id = 3,
346 .minimum_version_id_old = 1,
347 .load_state_old = apic_load_old,
348 .pre_save = apic_dispatch_pre_save,
349 .post_load = apic_dispatch_post_load,
350 .fields = (VMStateField[]) {
351 VMSTATE_UINT32(apicbase, APICCommonState),
352 VMSTATE_UINT8(id, APICCommonState),
353 VMSTATE_UINT8(arb_id, APICCommonState),
354 VMSTATE_UINT8(tpr, APICCommonState),
355 VMSTATE_UINT32(spurious_vec, APICCommonState),
356 VMSTATE_UINT8(log_dest, APICCommonState),
357 VMSTATE_UINT8(dest_mode, APICCommonState),
358 VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
359 VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
360 VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
361 VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
362 VMSTATE_UINT32(esr, APICCommonState),
363 VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
364 VMSTATE_UINT32(divide_conf, APICCommonState),
365 VMSTATE_INT32(count_shift, APICCommonState),
366 VMSTATE_UINT32(initial_count, APICCommonState),
367 VMSTATE_INT64(initial_count_load_time, APICCommonState),
368 VMSTATE_INT64(next_time, APICCommonState),
369 VMSTATE_INT64(timer_expiry,
370 APICCommonState), /* open-coded timer state */
371 VMSTATE_END_OF_LIST()
375 static Property apic_properties_common[] = {
376 DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
377 DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
378 true),
379 DEFINE_PROP_END_OF_LIST(),
382 static void apic_common_class_init(ObjectClass *klass, void *data)
384 ICCDeviceClass *idc = ICC_DEVICE_CLASS(klass);
385 DeviceClass *dc = DEVICE_CLASS(klass);
387 dc->vmsd = &vmstate_apic_common;
388 dc->reset = apic_reset_common;
389 dc->no_user = 1;
390 dc->props = apic_properties_common;
391 idc->init = apic_init_common;
394 static const TypeInfo apic_common_type = {
395 .name = TYPE_APIC_COMMON,
396 .parent = TYPE_ICC_DEVICE,
397 .instance_size = sizeof(APICCommonState),
398 .class_size = sizeof(APICCommonClass),
399 .class_init = apic_common_class_init,
400 .abstract = true,
403 static void register_types(void)
405 type_register_static(&apic_common_type);
408 type_init(register_types)