1 /* 2 * KVM in-kernel IOPIC support 3 * 4 * Copyright (c) 2011 Siemens AG 5 * 6 * Authors: 7 * Jan Kiszka <jan.kiszka@siemens.com> 8 * 9 * This work is licensed under the terms of the GNU GPL version 2. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "monitor/monitor.h" 15 #include "hw/i386/x86.h" 16 #include "hw/irq.h" 17 #include "hw/qdev-properties.h" 18 #include "hw/i386/ioapic_internal.h" 19 #include "hw/i386/apic_internal.h" 20 #include "sysemu/kvm.h" 21 22 /* PC Utility function */ 23 void kvm_pc_setup_irq_routing(bool pci_enabled) 24 { 25 KVMState *s = kvm_state; 26 int i; 27 28 assert(kvm_has_gsi_routing()); 29 for (i = 0; i < 8; ++i) { 30 if (i == 2) { 31 continue; 32 } 33 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i); 34 } 35 for (i = 8; i < 16; ++i) { 36 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8); 37 } 38 if (pci_enabled) { 39 for (i = 0; i < 24; ++i) { 40 if (i == 0) { 41 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2); 42 } else if (i != 2) { 43 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i); 44 } 45 } 46 } 47 kvm_irqchip_commit_routes(s); 48 } 49 50 typedef struct KVMIOAPICState KVMIOAPICState; 51 52 struct KVMIOAPICState { 53 IOAPICCommonState ioapic; 54 uint32_t kvm_gsi_base; 55 }; 56 57 static void kvm_ioapic_get(IOAPICCommonState *s) 58 { 59 struct kvm_irqchip chip; 60 struct kvm_ioapic_state *kioapic; 61 int ret, i; 62 63 chip.chip_id = KVM_IRQCHIP_IOAPIC; 64 ret = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, &chip); 65 if (ret < 0) { 66 fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret)); 67 abort(); 68 } 69 70 kioapic = &chip.chip.ioapic; 71 72 s->id = kioapic->id; 73 s->ioregsel = kioapic->ioregsel; 74 s->irr = kioapic->irr; 75 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 76 s->ioredtbl[i] = kioapic->redirtbl[i].bits; 77 } 78 } 79 80 static void kvm_ioapic_put(IOAPICCommonState *s) 81 { 82 struct kvm_irqchip chip; 83 struct kvm_ioapic_state *kioapic; 84 int ret, i; 85 86 chip.chip_id = KVM_IRQCHIP_IOAPIC; 87 kioapic = &chip.chip.ioapic; 88 89 kioapic->id = s->id; 90 kioapic->ioregsel = s->ioregsel; 91 kioapic->base_address = s->busdev.mmio[0].addr; 92 kioapic->irr = s->irr; 93 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 94 kioapic->redirtbl[i].bits = s->ioredtbl[i]; 95 } 96 97 ret = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, &chip); 98 if (ret < 0) { 99 fprintf(stderr, "KVM_SET_IRQCHIP failed: %s\n", strerror(ret)); 100 abort(); 101 } 102 } 103 104 static void kvm_ioapic_reset(DeviceState *dev) 105 { 106 IOAPICCommonState *s = IOAPIC_COMMON(dev); 107 108 ioapic_reset_common(dev); 109 kvm_ioapic_put(s); 110 } 111 112 static void kvm_ioapic_set_irq(void *opaque, int irq, int level) 113 { 114 KVMIOAPICState *s = opaque; 115 IOAPICCommonState *common = IOAPIC_COMMON(s); 116 int delivered; 117 118 ioapic_stat_update_irq(common, irq, level); 119 delivered = kvm_set_irq(kvm_state, s->kvm_gsi_base + irq, level); 120 apic_report_irq_delivered(delivered); 121 } 122 123 static void kvm_ioapic_realize(DeviceState *dev, Error **errp) 124 { 125 IOAPICCommonState *s = IOAPIC_COMMON(dev); 126 127 memory_region_init_io(&s->io_memory, OBJECT(dev), NULL, NULL, "kvm-ioapic", 0x1000); 128 /* 129 * KVM ioapic only supports 0x11 now. This will only be used when 130 * we want to dump ioapic version. 131 */ 132 s->version = 0x11; 133 134 qdev_init_gpio_in(dev, kvm_ioapic_set_irq, IOAPIC_NUM_PINS); 135 } 136 137 static Property kvm_ioapic_properties[] = { 138 DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState, kvm_gsi_base, 0), 139 DEFINE_PROP_END_OF_LIST() 140 }; 141 142 static void kvm_ioapic_class_init(ObjectClass *klass, void *data) 143 { 144 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass); 145 DeviceClass *dc = DEVICE_CLASS(klass); 146 147 k->realize = kvm_ioapic_realize; 148 k->pre_save = kvm_ioapic_get; 149 k->post_load = kvm_ioapic_put; 150 dc->reset = kvm_ioapic_reset; 151 device_class_set_props(dc, kvm_ioapic_properties); 152 } 153 154 static const TypeInfo kvm_ioapic_info = { 155 .name = TYPE_KVM_IOAPIC, 156 .parent = TYPE_IOAPIC_COMMON, 157 .instance_size = sizeof(KVMIOAPICState), 158 .class_init = kvm_ioapic_class_init, 159 }; 160 161 static void kvm_ioapic_register_types(void) 162 { 163 type_register_static(&kvm_ioapic_info); 164 } 165 166 type_init(kvm_ioapic_register_types) 167