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/pc.h" 16 #include "hw/irq.h" 17 #include "hw/i386/ioapic_internal.h" 18 #include "hw/i386/apic_internal.h" 19 #include "sysemu/kvm.h" 20 21 /* PC Utility function */ 22 void kvm_pc_setup_irq_routing(bool pci_enabled) 23 { 24 KVMState *s = kvm_state; 25 int i; 26 27 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) { 28 for (i = 0; i < 8; ++i) { 29 if (i == 2) { 30 continue; 31 } 32 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_MASTER, i); 33 } 34 for (i = 8; i < 16; ++i) { 35 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_PIC_SLAVE, i - 8); 36 } 37 if (pci_enabled) { 38 for (i = 0; i < 24; ++i) { 39 if (i == 0) { 40 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, 2); 41 } else if (i != 2) { 42 kvm_irqchip_add_irq_route(s, i, KVM_IRQCHIP_IOAPIC, i); 43 } 44 } 45 } 46 kvm_irqchip_commit_routes(s); 47 } 48 } 49 50 void kvm_pc_gsi_handler(void *opaque, int n, int level) 51 { 52 GSIState *s = opaque; 53 54 if (n < ISA_NUM_IRQS) { 55 /* Kernel will forward to both PIC and IOAPIC */ 56 qemu_set_irq(s->i8259_irq[n], level); 57 } else { 58 qemu_set_irq(s->ioapic_irq[n], level); 59 } 60 } 61 62 typedef struct KVMIOAPICState KVMIOAPICState; 63 64 struct KVMIOAPICState { 65 IOAPICCommonState ioapic; 66 uint32_t kvm_gsi_base; 67 }; 68 69 static void kvm_ioapic_get(IOAPICCommonState *s) 70 { 71 struct kvm_irqchip chip; 72 struct kvm_ioapic_state *kioapic; 73 int ret, i; 74 75 chip.chip_id = KVM_IRQCHIP_IOAPIC; 76 ret = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, &chip); 77 if (ret < 0) { 78 fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret)); 79 abort(); 80 } 81 82 kioapic = &chip.chip.ioapic; 83 84 s->id = kioapic->id; 85 s->ioregsel = kioapic->ioregsel; 86 s->irr = kioapic->irr; 87 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 88 s->ioredtbl[i] = kioapic->redirtbl[i].bits; 89 } 90 } 91 92 static void kvm_ioapic_put(IOAPICCommonState *s) 93 { 94 struct kvm_irqchip chip; 95 struct kvm_ioapic_state *kioapic; 96 int ret, i; 97 98 chip.chip_id = KVM_IRQCHIP_IOAPIC; 99 kioapic = &chip.chip.ioapic; 100 101 kioapic->id = s->id; 102 kioapic->ioregsel = s->ioregsel; 103 kioapic->base_address = s->busdev.mmio[0].addr; 104 kioapic->irr = s->irr; 105 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 106 kioapic->redirtbl[i].bits = s->ioredtbl[i]; 107 } 108 109 ret = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, &chip); 110 if (ret < 0) { 111 fprintf(stderr, "KVM_GET_IRQCHIP failed: %s\n", strerror(ret)); 112 abort(); 113 } 114 } 115 116 static void kvm_ioapic_reset(DeviceState *dev) 117 { 118 IOAPICCommonState *s = IOAPIC_COMMON(dev); 119 120 ioapic_reset_common(dev); 121 kvm_ioapic_put(s); 122 } 123 124 static void kvm_ioapic_set_irq(void *opaque, int irq, int level) 125 { 126 KVMIOAPICState *s = opaque; 127 IOAPICCommonState *common = IOAPIC_COMMON(s); 128 int delivered; 129 130 ioapic_stat_update_irq(common, irq, level); 131 delivered = kvm_set_irq(kvm_state, s->kvm_gsi_base + irq, level); 132 apic_report_irq_delivered(delivered); 133 } 134 135 static void kvm_ioapic_realize(DeviceState *dev, Error **errp) 136 { 137 IOAPICCommonState *s = IOAPIC_COMMON(dev); 138 139 memory_region_init_io(&s->io_memory, OBJECT(dev), NULL, NULL, "kvm-ioapic", 0x1000); 140 /* 141 * KVM ioapic only supports 0x11 now. This will only be used when 142 * we want to dump ioapic version. 143 */ 144 s->version = 0x11; 145 146 qdev_init_gpio_in(dev, kvm_ioapic_set_irq, IOAPIC_NUM_PINS); 147 } 148 149 static Property kvm_ioapic_properties[] = { 150 DEFINE_PROP_UINT32("gsi_base", KVMIOAPICState, kvm_gsi_base, 0), 151 DEFINE_PROP_END_OF_LIST() 152 }; 153 154 static void kvm_ioapic_class_init(ObjectClass *klass, void *data) 155 { 156 IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass); 157 DeviceClass *dc = DEVICE_CLASS(klass); 158 159 k->realize = kvm_ioapic_realize; 160 k->pre_save = kvm_ioapic_get; 161 k->post_load = kvm_ioapic_put; 162 dc->reset = kvm_ioapic_reset; 163 dc->props = kvm_ioapic_properties; 164 } 165 166 static const TypeInfo kvm_ioapic_info = { 167 .name = TYPE_KVM_IOAPIC, 168 .parent = TYPE_IOAPIC_COMMON, 169 .instance_size = sizeof(KVMIOAPICState), 170 .class_init = kvm_ioapic_class_init, 171 }; 172 173 static void kvm_ioapic_register_types(void) 174 { 175 type_register_static(&kvm_ioapic_info); 176 } 177 178 type_init(kvm_ioapic_register_types) 179