xref: /openbmc/qemu/hw/intc/apic_common.c (revision 33d7a288298f02df3eadd509735f0f75e3f80d73)
1 /*
2  *  APIC support - common bits of emulated and KVM kernel model
3  *
4  *  Copyright (c) 2004-2005 Fabrice Bellard
5  *  Copyright (c) 2011      Jan Kiszka, Siemens AG
6  *
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.
11  *
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.
16  *
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/>
19  */
20 #include "qemu/osdep.h"
21 #include "qemu/error-report.h"
22 #include "qapi/error.h"
23 #include "qemu-common.h"
24 #include "cpu.h"
25 #include "qapi/visitor.h"
26 #include "hw/i386/apic.h"
27 #include "hw/i386/apic_internal.h"
28 #include "trace.h"
29 #include "sysemu/kvm.h"
30 #include "hw/qdev.h"
31 #include "hw/sysbus.h"
32 
33 static int apic_irq_delivered;
34 bool apic_report_tpr_access;
35 
36 void cpu_set_apic_base(DeviceState *dev, uint64_t val)
37 {
38     trace_cpu_set_apic_base(val);
39 
40     if (dev) {
41         APICCommonState *s = APIC_COMMON(dev);
42         APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
43         info->set_base(s, val);
44     }
45 }
46 
47 uint64_t cpu_get_apic_base(DeviceState *dev)
48 {
49     if (dev) {
50         APICCommonState *s = APIC_COMMON(dev);
51         trace_cpu_get_apic_base((uint64_t)s->apicbase);
52         return s->apicbase;
53     } else {
54         trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
55         return MSR_IA32_APICBASE_BSP;
56     }
57 }
58 
59 void cpu_set_apic_tpr(DeviceState *dev, uint8_t val)
60 {
61     APICCommonState *s;
62     APICCommonClass *info;
63 
64     if (!dev) {
65         return;
66     }
67 
68     s = APIC_COMMON(dev);
69     info = APIC_COMMON_GET_CLASS(s);
70 
71     info->set_tpr(s, val);
72 }
73 
74 uint8_t cpu_get_apic_tpr(DeviceState *dev)
75 {
76     APICCommonState *s;
77     APICCommonClass *info;
78 
79     if (!dev) {
80         return 0;
81     }
82 
83     s = APIC_COMMON(dev);
84     info = APIC_COMMON_GET_CLASS(s);
85 
86     return info->get_tpr(s);
87 }
88 
89 void apic_enable_tpr_access_reporting(DeviceState *dev, bool enable)
90 {
91     APICCommonState *s = APIC_COMMON(dev);
92     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
93 
94     apic_report_tpr_access = enable;
95     if (info->enable_tpr_reporting) {
96         info->enable_tpr_reporting(s, enable);
97     }
98 }
99 
100 void apic_enable_vapic(DeviceState *dev, hwaddr paddr)
101 {
102     APICCommonState *s = APIC_COMMON(dev);
103     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
104 
105     s->vapic_paddr = paddr;
106     info->vapic_base_update(s);
107 }
108 
109 void apic_handle_tpr_access_report(DeviceState *dev, target_ulong ip,
110                                    TPRAccess access)
111 {
112     APICCommonState *s = APIC_COMMON(dev);
113 
114     vapic_report_tpr_access(s->vapic, CPU(s->cpu), ip, access);
115 }
116 
117 void apic_report_irq_delivered(int delivered)
118 {
119     apic_irq_delivered += delivered;
120 
121     trace_apic_report_irq_delivered(apic_irq_delivered);
122 }
123 
124 void apic_reset_irq_delivered(void)
125 {
126     /* Copy this into a local variable to encourage gcc to emit a plain
127      * register for a sys/sdt.h marker.  For details on this workaround, see:
128      * https://sourceware.org/bugzilla/show_bug.cgi?id=13296
129      */
130     volatile int a_i_d = apic_irq_delivered;
131     trace_apic_reset_irq_delivered(a_i_d);
132 
133     apic_irq_delivered = 0;
134 }
135 
136 int apic_get_irq_delivered(void)
137 {
138     trace_apic_get_irq_delivered(apic_irq_delivered);
139 
140     return apic_irq_delivered;
141 }
142 
143 void apic_deliver_nmi(DeviceState *dev)
144 {
145     APICCommonState *s = APIC_COMMON(dev);
146     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
147 
148     info->external_nmi(s);
149 }
150 
151 bool apic_next_timer(APICCommonState *s, int64_t current_time)
152 {
153     int64_t d;
154 
155     /* We need to store the timer state separately to support APIC
156      * implementations that maintain a non-QEMU timer, e.g. inside the
157      * host kernel. This open-coded state allows us to migrate between
158      * both models. */
159     s->timer_expiry = -1;
160 
161     if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
162         return false;
163     }
164 
165     d = (current_time - s->initial_count_load_time) >> s->count_shift;
166 
167     if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
168         if (!s->initial_count) {
169             return false;
170         }
171         d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
172             ((uint64_t)s->initial_count + 1);
173     } else {
174         if (d >= s->initial_count) {
175             return false;
176         }
177         d = (uint64_t)s->initial_count + 1;
178     }
179     s->next_time = s->initial_count_load_time + (d << s->count_shift);
180     s->timer_expiry = s->next_time;
181     return true;
182 }
183 
184 void apic_init_reset(DeviceState *dev)
185 {
186     APICCommonState *s;
187     APICCommonClass *info;
188     int i;
189 
190     if (!dev) {
191         return;
192     }
193     s = APIC_COMMON(dev);
194     s->tpr = 0;
195     s->spurious_vec = 0xff;
196     s->log_dest = 0;
197     s->dest_mode = 0xf;
198     memset(s->isr, 0, sizeof(s->isr));
199     memset(s->tmr, 0, sizeof(s->tmr));
200     memset(s->irr, 0, sizeof(s->irr));
201     for (i = 0; i < APIC_LVT_NB; i++) {
202         s->lvt[i] = APIC_LVT_MASKED;
203     }
204     s->esr = 0;
205     memset(s->icr, 0, sizeof(s->icr));
206     s->divide_conf = 0;
207     s->count_shift = 0;
208     s->initial_count = 0;
209     s->initial_count_load_time = 0;
210     s->next_time = 0;
211     s->wait_for_sipi = !cpu_is_bsp(s->cpu);
212 
213     if (s->timer) {
214         timer_del(s->timer);
215     }
216     s->timer_expiry = -1;
217 
218     info = APIC_COMMON_GET_CLASS(s);
219     if (info->reset) {
220         info->reset(s);
221     }
222 }
223 
224 void apic_designate_bsp(DeviceState *dev, bool bsp)
225 {
226     if (dev == NULL) {
227         return;
228     }
229 
230     APICCommonState *s = APIC_COMMON(dev);
231     if (bsp) {
232         s->apicbase |= MSR_IA32_APICBASE_BSP;
233     } else {
234         s->apicbase &= ~MSR_IA32_APICBASE_BSP;
235     }
236 }
237 
238 static void apic_reset_common(DeviceState *dev)
239 {
240     APICCommonState *s = APIC_COMMON(dev);
241     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
242     uint32_t bsp;
243 
244     bsp = s->apicbase & MSR_IA32_APICBASE_BSP;
245     s->apicbase = APIC_DEFAULT_ADDRESS | bsp | MSR_IA32_APICBASE_ENABLE;
246 
247     s->vapic_paddr = 0;
248     info->vapic_base_update(s);
249 
250     apic_init_reset(dev);
251 }
252 
253 /* This function is only used for old state version 1 and 2 */
254 static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
255 {
256     APICCommonState *s = opaque;
257     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
258     int i;
259 
260     if (version_id > 2) {
261         return -EINVAL;
262     }
263 
264     /* XXX: what if the base changes? (registered memory regions) */
265     qemu_get_be32s(f, &s->apicbase);
266     qemu_get_8s(f, &s->id);
267     qemu_get_8s(f, &s->arb_id);
268     qemu_get_8s(f, &s->tpr);
269     qemu_get_be32s(f, &s->spurious_vec);
270     qemu_get_8s(f, &s->log_dest);
271     qemu_get_8s(f, &s->dest_mode);
272     for (i = 0; i < 8; i++) {
273         qemu_get_be32s(f, &s->isr[i]);
274         qemu_get_be32s(f, &s->tmr[i]);
275         qemu_get_be32s(f, &s->irr[i]);
276     }
277     for (i = 0; i < APIC_LVT_NB; i++) {
278         qemu_get_be32s(f, &s->lvt[i]);
279     }
280     qemu_get_be32s(f, &s->esr);
281     qemu_get_be32s(f, &s->icr[0]);
282     qemu_get_be32s(f, &s->icr[1]);
283     qemu_get_be32s(f, &s->divide_conf);
284     s->count_shift = qemu_get_be32(f);
285     qemu_get_be32s(f, &s->initial_count);
286     s->initial_count_load_time = qemu_get_be64(f);
287     s->next_time = qemu_get_be64(f);
288 
289     if (version_id >= 2) {
290         s->timer_expiry = qemu_get_be64(f);
291     }
292 
293     if (info->post_load) {
294         info->post_load(s);
295     }
296     return 0;
297 }
298 
299 static const VMStateDescription vmstate_apic_common;
300 
301 static void apic_common_realize(DeviceState *dev, Error **errp)
302 {
303     APICCommonState *s = APIC_COMMON(dev);
304     APICCommonClass *info;
305     static DeviceState *vapic;
306     int instance_id = s->id;
307 
308     info = APIC_COMMON_GET_CLASS(s);
309     info->realize(dev, errp);
310 
311     /* Note: We need at least 1M to map the VAPIC option ROM */
312     if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
313         ram_size >= 1024 * 1024) {
314         vapic = sysbus_create_simple("kvmvapic", -1, NULL);
315     }
316     s->vapic = vapic;
317     if (apic_report_tpr_access && info->enable_tpr_reporting) {
318         info->enable_tpr_reporting(s, true);
319     }
320 
321     if (s->legacy_instance_id) {
322         instance_id = -1;
323     }
324     vmstate_register_with_alias_id(NULL, instance_id, &vmstate_apic_common,
325                                    s, -1, 0);
326 }
327 
328 static void apic_common_unrealize(DeviceState *dev, Error **errp)
329 {
330     APICCommonState *s = APIC_COMMON(dev);
331     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
332 
333     vmstate_unregister(NULL, &vmstate_apic_common, s);
334     info->unrealize(dev, errp);
335 
336     if (apic_report_tpr_access && info->enable_tpr_reporting) {
337         info->enable_tpr_reporting(s, false);
338     }
339 }
340 
341 static int apic_pre_load(void *opaque)
342 {
343     APICCommonState *s = APIC_COMMON(opaque);
344 
345     /* The default is !cpu_is_bsp(s->cpu), but the common value is 0
346      * so that's what apic_common_sipi_needed checks for.  Reset to
347      * the value that is assumed when the apic_sipi subsection is
348      * absent.
349      */
350     s->wait_for_sipi = 0;
351     return 0;
352 }
353 
354 static void apic_dispatch_pre_save(void *opaque)
355 {
356     APICCommonState *s = APIC_COMMON(opaque);
357     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
358 
359     if (info->pre_save) {
360         info->pre_save(s);
361     }
362 }
363 
364 static int apic_dispatch_post_load(void *opaque, int version_id)
365 {
366     APICCommonState *s = APIC_COMMON(opaque);
367     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
368 
369     if (info->post_load) {
370         info->post_load(s);
371     }
372     return 0;
373 }
374 
375 static bool apic_common_sipi_needed(void *opaque)
376 {
377     APICCommonState *s = APIC_COMMON(opaque);
378     return s->wait_for_sipi != 0;
379 }
380 
381 static const VMStateDescription vmstate_apic_common_sipi = {
382     .name = "apic_sipi",
383     .version_id = 1,
384     .minimum_version_id = 1,
385     .needed = apic_common_sipi_needed,
386     .fields = (VMStateField[]) {
387         VMSTATE_INT32(sipi_vector, APICCommonState),
388         VMSTATE_INT32(wait_for_sipi, APICCommonState),
389         VMSTATE_END_OF_LIST()
390     }
391 };
392 
393 static const VMStateDescription vmstate_apic_common = {
394     .name = "apic",
395     .version_id = 3,
396     .minimum_version_id = 3,
397     .minimum_version_id_old = 1,
398     .load_state_old = apic_load_old,
399     .pre_load = apic_pre_load,
400     .pre_save = apic_dispatch_pre_save,
401     .post_load = apic_dispatch_post_load,
402     .fields = (VMStateField[]) {
403         VMSTATE_UINT32(apicbase, APICCommonState),
404         VMSTATE_UINT8(id, APICCommonState),
405         VMSTATE_UINT8(arb_id, APICCommonState),
406         VMSTATE_UINT8(tpr, APICCommonState),
407         VMSTATE_UINT32(spurious_vec, APICCommonState),
408         VMSTATE_UINT8(log_dest, APICCommonState),
409         VMSTATE_UINT8(dest_mode, APICCommonState),
410         VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
411         VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
412         VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
413         VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
414         VMSTATE_UINT32(esr, APICCommonState),
415         VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
416         VMSTATE_UINT32(divide_conf, APICCommonState),
417         VMSTATE_INT32(count_shift, APICCommonState),
418         VMSTATE_UINT32(initial_count, APICCommonState),
419         VMSTATE_INT64(initial_count_load_time, APICCommonState),
420         VMSTATE_INT64(next_time, APICCommonState),
421         VMSTATE_INT64(timer_expiry,
422                       APICCommonState), /* open-coded timer state */
423         VMSTATE_END_OF_LIST()
424     },
425     .subsections = (const VMStateDescription*[]) {
426         &vmstate_apic_common_sipi,
427         NULL
428     }
429 };
430 
431 static Property apic_properties_common[] = {
432     DEFINE_PROP_UINT8("version", APICCommonState, version, 0x14),
433     DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
434                     true),
435     DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
436                      false),
437     DEFINE_PROP_END_OF_LIST(),
438 };
439 
440 static void apic_common_get_id(Object *obj, Visitor *v, const char *name,
441                                void *opaque, Error **errp)
442 {
443     APICCommonState *s = APIC_COMMON(obj);
444     int64_t value;
445 
446     value = s->apicbase & MSR_IA32_APICBASE_EXTD ? s->initial_apic_id : s->id;
447     visit_type_int(v, name, &value, errp);
448 }
449 
450 static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
451                                void *opaque, Error **errp)
452 {
453     APICCommonState *s = APIC_COMMON(obj);
454     DeviceState *dev = DEVICE(obj);
455     Error *local_err = NULL;
456     int64_t value;
457 
458     if (dev->realized) {
459         qdev_prop_set_after_realize(dev, name, errp);
460         return;
461     }
462 
463     visit_type_int(v, name, &value, &local_err);
464     if (local_err) {
465         error_propagate(errp, local_err);
466         return;
467     }
468 
469     s->initial_apic_id = value;
470     s->id = (uint8_t)value;
471 }
472 
473 static void apic_common_initfn(Object *obj)
474 {
475     APICCommonState *s = APIC_COMMON(obj);
476 
477     s->id = s->initial_apic_id = -1;
478     object_property_add(obj, "id", "int",
479                         apic_common_get_id,
480                         apic_common_set_id, NULL, NULL, NULL);
481 }
482 
483 static void apic_common_class_init(ObjectClass *klass, void *data)
484 {
485     DeviceClass *dc = DEVICE_CLASS(klass);
486 
487     dc->reset = apic_reset_common;
488     dc->props = apic_properties_common;
489     dc->realize = apic_common_realize;
490     dc->unrealize = apic_common_unrealize;
491     /*
492      * Reason: APIC and CPU need to be wired up by
493      * x86_cpu_apic_create()
494      */
495     dc->cannot_instantiate_with_device_add_yet = true;
496 }
497 
498 static const TypeInfo apic_common_type = {
499     .name = TYPE_APIC_COMMON,
500     .parent = TYPE_DEVICE,
501     .instance_size = sizeof(APICCommonState),
502     .instance_init = apic_common_initfn,
503     .class_size = sizeof(APICCommonClass),
504     .class_init = apic_common_class_init,
505     .abstract = true,
506 };
507 
508 static void apic_common_register_types(void)
509 {
510     type_register_static(&apic_common_type);
511 }
512 
513 type_init(apic_common_register_types)
514