xref: /qemu/hw/i386/kvm/ioapic.c (revision e3d08143)
154976b75SPaolo Bonzini /*
254976b75SPaolo Bonzini  * KVM in-kernel IOPIC support
354976b75SPaolo Bonzini  *
454976b75SPaolo Bonzini  * Copyright (c) 2011 Siemens AG
554976b75SPaolo Bonzini  *
654976b75SPaolo Bonzini  * Authors:
754976b75SPaolo Bonzini  *  Jan Kiszka          <jan.kiszka@siemens.com>
854976b75SPaolo Bonzini  *
954976b75SPaolo Bonzini  * This work is licensed under the terms of the GNU GPL version 2.
1054976b75SPaolo Bonzini  * See the COPYING file in the top-level directory.
1154976b75SPaolo Bonzini  */
1254976b75SPaolo Bonzini 
13b6a0aa05SPeter Maydell #include "qemu/osdep.h"
14d665d696SPavel Butsykin #include "monitor/monitor.h"
15a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
167f54640bSBernhard Beschow #include "hw/intc/ioapic_internal.h"
172b85e0cdSThomas Huth #include "hw/intc/kvm_irqcount.h"
1854976b75SPaolo Bonzini #include "sysemu/kvm.h"
19bb781b94SPhilippe Mathieu-Daudé #include "kvm/kvm_i386.h"
2054976b75SPaolo Bonzini 
2154976b75SPaolo Bonzini /* PC Utility function */
kvm_pc_setup_irq_routing(bool pci_enabled)2254976b75SPaolo Bonzini void kvm_pc_setup_irq_routing(bool pci_enabled)
2354976b75SPaolo Bonzini {
2454976b75SPaolo Bonzini     KVMState *s = kvm_state;
2554976b75SPaolo Bonzini     int i;
2654976b75SPaolo Bonzini 
278981bae2SEduardo Habkost     assert(kvm_has_gsi_routing());
2854976b75SPaolo Bonzini     for (i = 0; i < 8; ++i) {
2954976b75SPaolo Bonzini         if (i == 2) {
3054976b75SPaolo Bonzini             continue;
3154976b75SPaolo Bonzini         }
3254976b75SPaolo Bonzini         kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i);
3354976b75SPaolo Bonzini     }
3454976b75SPaolo Bonzini     for (i = 8; i < 16; ++i) {
3554976b75SPaolo Bonzini         kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8);
3654976b75SPaolo Bonzini     }
3754976b75SPaolo Bonzini     if (pci_enabled) {
386b29e372SPhilippe Mathieu-Daudé         for (i = 0; i < KVM_IOAPIC_NUM_PINS; ++i) {
3954976b75SPaolo Bonzini             if (i == 0) {
4054976b75SPaolo Bonzini                 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2);
4154976b75SPaolo Bonzini             } else if (i != 2) {
4254976b75SPaolo Bonzini                 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i);
4354976b75SPaolo Bonzini             }
4454976b75SPaolo Bonzini         }
4554976b75SPaolo Bonzini     }
46cb925cf9SAlexander Graf     kvm_irqchip_commit_routes(s);
4754976b75SPaolo Bonzini }
4854976b75SPaolo Bonzini 
4954976b75SPaolo Bonzini typedef struct KVMIOAPICState KVMIOAPICState;
5054976b75SPaolo Bonzini 
5154976b75SPaolo Bonzini struct KVMIOAPICState {
5254976b75SPaolo Bonzini     IOAPICCommonState ioapic;
5354976b75SPaolo Bonzini     uint32_t kvm_gsi_base;
5454976b75SPaolo Bonzini };
5554976b75SPaolo Bonzini 
kvm_ioapic_get(IOAPICCommonState * s)5654976b75SPaolo Bonzini static void kvm_ioapic_get(IOAPICCommonState *s)
5754976b75SPaolo Bonzini {
5854976b75SPaolo Bonzini     struct kvm_irqchip chip;
5954976b75SPaolo Bonzini     struct kvm_ioapic_state *kioapic;
6054976b75SPaolo Bonzini     int ret, i;
6154976b75SPaolo Bonzini 
6254976b75SPaolo Bonzini     chip.chip_id = KVM_IRQCHIP_IOAPIC;
6354976b75SPaolo Bonzini     ret = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, &chip);
6454976b75SPaolo Bonzini     if (ret < 0) {
65d84451d3SDmitry Voronetskiy         fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(-ret));
6654976b75SPaolo Bonzini         abort();
6754976b75SPaolo Bonzini     }
6854976b75SPaolo Bonzini 
6954976b75SPaolo Bonzini     kioapic = &chip.chip.ioapic;
7054976b75SPaolo Bonzini 
7154976b75SPaolo Bonzini     s->id = kioapic->id;
7254976b75SPaolo Bonzini     s->ioregsel = kioapic->ioregsel;
7354976b75SPaolo Bonzini     s->irr = kioapic->irr;
7454976b75SPaolo Bonzini     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
7554976b75SPaolo Bonzini         s->ioredtbl[i] = kioapic->redirtbl[i].bits;
7654976b75SPaolo Bonzini     }
7754976b75SPaolo Bonzini }
7854976b75SPaolo Bonzini 
kvm_ioapic_put(IOAPICCommonState * s)7954976b75SPaolo Bonzini static void kvm_ioapic_put(IOAPICCommonState *s)
8054976b75SPaolo Bonzini {
8154976b75SPaolo Bonzini     struct kvm_irqchip chip;
8254976b75SPaolo Bonzini     struct kvm_ioapic_state *kioapic;
8354976b75SPaolo Bonzini     int ret, i;
8454976b75SPaolo Bonzini 
8554976b75SPaolo Bonzini     chip.chip_id = KVM_IRQCHIP_IOAPIC;
8654976b75SPaolo Bonzini     kioapic = &chip.chip.ioapic;
8754976b75SPaolo Bonzini 
8854976b75SPaolo Bonzini     kioapic->id = s->id;
8954976b75SPaolo Bonzini     kioapic->ioregsel = s->ioregsel;
9054976b75SPaolo Bonzini     kioapic->base_address = s->busdev.mmio[0].addr;
9154976b75SPaolo Bonzini     kioapic->irr = s->irr;
9254976b75SPaolo Bonzini     for (i = 0; i < IOAPIC_NUM_PINS; i++) {
9354976b75SPaolo Bonzini         kioapic->redirtbl[i].bits = s->ioredtbl[i];
9454976b75SPaolo Bonzini     }
9554976b75SPaolo Bonzini 
9654976b75SPaolo Bonzini     ret = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, &chip);
9754976b75SPaolo Bonzini     if (ret < 0) {
98d84451d3SDmitry Voronetskiy         fprintf(stderr, "KVM_SET_IRQCHIP failed: %s\n", strerror(-ret));
9954976b75SPaolo Bonzini         abort();
10054976b75SPaolo Bonzini     }
10154976b75SPaolo Bonzini }
10254976b75SPaolo Bonzini 
kvm_ioapic_reset(DeviceState * dev)10354976b75SPaolo Bonzini static void kvm_ioapic_reset(DeviceState *dev)
10454976b75SPaolo Bonzini {
105b3119631SAndreas Färber     IOAPICCommonState *s = IOAPIC_COMMON(dev);
10654976b75SPaolo Bonzini 
10754976b75SPaolo Bonzini     ioapic_reset_common(dev);
10854976b75SPaolo Bonzini     kvm_ioapic_put(s);
10954976b75SPaolo Bonzini }
11054976b75SPaolo Bonzini 
kvm_ioapic_set_irq(void * opaque,int irq,int level)11154976b75SPaolo Bonzini static void kvm_ioapic_set_irq(void *opaque, int irq, int level)
11254976b75SPaolo Bonzini {
11354976b75SPaolo Bonzini     KVMIOAPICState *s = opaque;
114cce5405eSPeter Xu     IOAPICCommonState *common = IOAPIC_COMMON(s);
11554976b75SPaolo Bonzini     int delivered;
11654976b75SPaolo Bonzini 
117cce5405eSPeter Xu     ioapic_stat_update_irq(common, irq, level);
11854976b75SPaolo Bonzini     delivered = kvm_set_irq(kvm_state, s->kvm_gsi_base + irq, level);
1192b85e0cdSThomas Huth     kvm_report_irq_delivered(delivered);
12054976b75SPaolo Bonzini }
12154976b75SPaolo Bonzini 
kvm_ioapic_realize(DeviceState * dev,Error ** errp)122db0f8888Sxiaoqiang zhao static void kvm_ioapic_realize(DeviceState *dev, Error **errp)
12354976b75SPaolo Bonzini {
124db0f8888Sxiaoqiang zhao     IOAPICCommonState *s = IOAPIC_COMMON(dev);
125f9771858Sxiaoqiang zhao 
126257a7430SPaolo Bonzini     memory_region_init_io(&s->io_memory, OBJECT(dev), NULL, NULL, "kvm-ioapic", 0x1000);
127b7a4104bSPeter Xu     /*
128b7a4104bSPeter Xu      * KVM ioapic only supports 0x11 now. This will only be used when
129b7a4104bSPeter Xu      * we want to dump ioapic version.
130b7a4104bSPeter Xu      */
131b7a4104bSPeter Xu     s->version = 0x11;
13254976b75SPaolo Bonzini 
133f9771858Sxiaoqiang zhao     qdev_init_gpio_in(dev, kvm_ioapic_set_irq, IOAPIC_NUM_PINS);
13454976b75SPaolo Bonzini }
13554976b75SPaolo Bonzini 
13654976b75SPaolo Bonzini static Property kvm_ioapic_properties[] = {
13754976b75SPaolo Bonzini     DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState, kvm_gsi_base, 0),
13854976b75SPaolo Bonzini     DEFINE_PROP_END_OF_LIST()
13954976b75SPaolo Bonzini };
14054976b75SPaolo Bonzini 
kvm_ioapic_class_init(ObjectClass * klass,void * data)14154976b75SPaolo Bonzini static void kvm_ioapic_class_init(ObjectClass *klass, void *data)
14254976b75SPaolo Bonzini {
14354976b75SPaolo Bonzini     IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
14454976b75SPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
14554976b75SPaolo Bonzini 
146db0f8888Sxiaoqiang zhao     k->realize   = kvm_ioapic_realize;
14754976b75SPaolo Bonzini     k->pre_save  = kvm_ioapic_get;
14854976b75SPaolo Bonzini     k->post_load = kvm_ioapic_put;
149*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, kvm_ioapic_reset);
1504f67d30bSMarc-André Lureau     device_class_set_props(dc, kvm_ioapic_properties);
15154976b75SPaolo Bonzini }
15254976b75SPaolo Bonzini 
15354976b75SPaolo Bonzini static const TypeInfo kvm_ioapic_info = {
15434bec7a8SLi Qiang     .name  = TYPE_KVM_IOAPIC,
15554976b75SPaolo Bonzini     .parent = TYPE_IOAPIC_COMMON,
15654976b75SPaolo Bonzini     .instance_size = sizeof(KVMIOAPICState),
15754976b75SPaolo Bonzini     .class_init = kvm_ioapic_class_init,
15854976b75SPaolo Bonzini };
15954976b75SPaolo Bonzini 
kvm_ioapic_register_types(void)16054976b75SPaolo Bonzini static void kvm_ioapic_register_types(void)
16154976b75SPaolo Bonzini {
16254976b75SPaolo Bonzini     type_register_static(&kvm_ioapic_info);
16354976b75SPaolo Bonzini }
16454976b75SPaolo Bonzini 
16554976b75SPaolo Bonzini type_init(kvm_ioapic_register_types)
166