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/>
21 #include "apic_internal.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
);
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
)
40 APICCommonState
*s
= APIC_COMMON(d
);
41 trace_cpu_get_apic_base((uint64_t)s
->apicbase
);
44 trace_cpu_get_apic_base(0);
49 void cpu_set_apic_tpr(DeviceState
*d
, uint8_t val
)
52 APICCommonClass
*info
;
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_handle_tpr_access_report(DeviceState
*d
, target_ulong ip
,
76 void apic_report_irq_delivered(int delivered
)
78 apic_irq_delivered
+= delivered
;
80 trace_apic_report_irq_delivered(apic_irq_delivered
);
83 void apic_reset_irq_delivered(void)
85 trace_apic_reset_irq_delivered(apic_irq_delivered
);
87 apic_irq_delivered
= 0;
90 int apic_get_irq_delivered(void)
92 trace_apic_get_irq_delivered(apic_irq_delivered
);
94 return apic_irq_delivered
;
97 void apic_deliver_nmi(DeviceState
*d
)
99 APICCommonState
*s
= APIC_COMMON(d
);
100 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
102 info
->external_nmi(s
);
105 bool apic_next_timer(APICCommonState
*s
, int64_t current_time
)
109 /* We need to store the timer state separately to support APIC
110 * implementations that maintain a non-QEMU timer, e.g. inside the
111 * host kernel. This open-coded state allows us to migrate between
113 s
->timer_expiry
= -1;
115 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_MASKED
) {
119 d
= (current_time
- s
->initial_count_load_time
) >> s
->count_shift
;
121 if (s
->lvt
[APIC_LVT_TIMER
] & APIC_LVT_TIMER_PERIODIC
) {
122 if (!s
->initial_count
) {
125 d
= ((d
/ ((uint64_t)s
->initial_count
+ 1)) + 1) *
126 ((uint64_t)s
->initial_count
+ 1);
128 if (d
>= s
->initial_count
) {
131 d
= (uint64_t)s
->initial_count
+ 1;
133 s
->next_time
= s
->initial_count_load_time
+ (d
<< s
->count_shift
);
134 s
->timer_expiry
= s
->next_time
;
138 void apic_init_reset(DeviceState
*d
)
140 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
147 s
->spurious_vec
= 0xff;
150 memset(s
->isr
, 0, sizeof(s
->isr
));
151 memset(s
->tmr
, 0, sizeof(s
->tmr
));
152 memset(s
->irr
, 0, sizeof(s
->irr
));
153 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
154 s
->lvt
[i
] = APIC_LVT_MASKED
;
157 memset(s
->icr
, 0, sizeof(s
->icr
));
160 s
->initial_count
= 0;
161 s
->initial_count_load_time
= 0;
163 s
->wait_for_sipi
= 1;
166 qemu_del_timer(s
->timer
);
168 s
->timer_expiry
= -1;
171 static void apic_reset_common(DeviceState
*d
)
173 APICCommonState
*s
= DO_UPCAST(APICCommonState
, busdev
.qdev
, d
);
176 bsp
= cpu_is_bsp(s
->cpu_env
);
177 s
->apicbase
= 0xfee00000 |
178 (bsp
? MSR_IA32_APICBASE_BSP
: 0) | MSR_IA32_APICBASE_ENABLE
;
184 * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
185 * time typically by BIOS, so PIC interrupt can be delivered to the
186 * processor when local APIC is enabled.
188 s
->lvt
[APIC_LVT_LINT0
] = 0x700;
192 /* This function is only used for old state version 1 and 2 */
193 static int apic_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
195 APICCommonState
*s
= opaque
;
196 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
199 if (version_id
> 2) {
203 /* XXX: what if the base changes? (registered memory regions) */
204 qemu_get_be32s(f
, &s
->apicbase
);
205 qemu_get_8s(f
, &s
->id
);
206 qemu_get_8s(f
, &s
->arb_id
);
207 qemu_get_8s(f
, &s
->tpr
);
208 qemu_get_be32s(f
, &s
->spurious_vec
);
209 qemu_get_8s(f
, &s
->log_dest
);
210 qemu_get_8s(f
, &s
->dest_mode
);
211 for (i
= 0; i
< 8; i
++) {
212 qemu_get_be32s(f
, &s
->isr
[i
]);
213 qemu_get_be32s(f
, &s
->tmr
[i
]);
214 qemu_get_be32s(f
, &s
->irr
[i
]);
216 for (i
= 0; i
< APIC_LVT_NB
; i
++) {
217 qemu_get_be32s(f
, &s
->lvt
[i
]);
219 qemu_get_be32s(f
, &s
->esr
);
220 qemu_get_be32s(f
, &s
->icr
[0]);
221 qemu_get_be32s(f
, &s
->icr
[1]);
222 qemu_get_be32s(f
, &s
->divide_conf
);
223 s
->count_shift
= qemu_get_be32(f
);
224 qemu_get_be32s(f
, &s
->initial_count
);
225 s
->initial_count_load_time
= qemu_get_be64(f
);
226 s
->next_time
= qemu_get_be64(f
);
228 if (version_id
>= 2) {
229 s
->timer_expiry
= qemu_get_be64(f
);
232 if (info
->post_load
) {
238 static int apic_init_common(SysBusDevice
*dev
)
240 APICCommonState
*s
= APIC_COMMON(dev
);
241 APICCommonClass
*info
;
244 if (apic_no
>= MAX_APICS
) {
249 info
= APIC_COMMON_GET_CLASS(s
);
252 sysbus_init_mmio(&s
->busdev
, &s
->io_memory
);
256 static int apic_dispatch_post_load(void *opaque
, int version_id
)
258 APICCommonState
*s
= APIC_COMMON(opaque
);
259 APICCommonClass
*info
= APIC_COMMON_GET_CLASS(s
);
261 if (info
->post_load
) {
267 static const VMStateDescription vmstate_apic_common
= {
270 .minimum_version_id
= 3,
271 .minimum_version_id_old
= 1,
272 .load_state_old
= apic_load_old
,
273 .post_load
= apic_dispatch_post_load
,
274 .fields
= (VMStateField
[]) {
275 VMSTATE_UINT32(apicbase
, APICCommonState
),
276 VMSTATE_UINT8(id
, APICCommonState
),
277 VMSTATE_UINT8(arb_id
, APICCommonState
),
278 VMSTATE_UINT8(tpr
, APICCommonState
),
279 VMSTATE_UINT32(spurious_vec
, APICCommonState
),
280 VMSTATE_UINT8(log_dest
, APICCommonState
),
281 VMSTATE_UINT8(dest_mode
, APICCommonState
),
282 VMSTATE_UINT32_ARRAY(isr
, APICCommonState
, 8),
283 VMSTATE_UINT32_ARRAY(tmr
, APICCommonState
, 8),
284 VMSTATE_UINT32_ARRAY(irr
, APICCommonState
, 8),
285 VMSTATE_UINT32_ARRAY(lvt
, APICCommonState
, APIC_LVT_NB
),
286 VMSTATE_UINT32(esr
, APICCommonState
),
287 VMSTATE_UINT32_ARRAY(icr
, APICCommonState
, 2),
288 VMSTATE_UINT32(divide_conf
, APICCommonState
),
289 VMSTATE_INT32(count_shift
, APICCommonState
),
290 VMSTATE_UINT32(initial_count
, APICCommonState
),
291 VMSTATE_INT64(initial_count_load_time
, APICCommonState
),
292 VMSTATE_INT64(next_time
, APICCommonState
),
293 VMSTATE_INT64(timer_expiry
,
294 APICCommonState
), /* open-coded timer state */
295 VMSTATE_END_OF_LIST()
299 static Property apic_properties_common
[] = {
300 DEFINE_PROP_UINT8("id", APICCommonState
, id
, -1),
301 DEFINE_PROP_PTR("cpu_env", APICCommonState
, cpu_env
),
302 DEFINE_PROP_END_OF_LIST(),
305 static void apic_common_class_init(ObjectClass
*klass
, void *data
)
307 SysBusDeviceClass
*sc
= SYS_BUS_DEVICE_CLASS(klass
);
308 DeviceClass
*dc
= DEVICE_CLASS(klass
);
310 dc
->vmsd
= &vmstate_apic_common
;
311 dc
->reset
= apic_reset_common
;
313 dc
->props
= apic_properties_common
;
314 sc
->init
= apic_init_common
;
317 static TypeInfo apic_common_type
= {
318 .name
= TYPE_APIC_COMMON
,
319 .parent
= TYPE_SYS_BUS_DEVICE
,
320 .instance_size
= sizeof(APICCommonState
),
321 .class_size
= sizeof(APICCommonClass
),
322 .class_init
= apic_common_class_init
,
326 static void register_types(void)
328 type_register_static(&apic_common_type
);
331 type_init(register_types
)