1 /* 2 * ARM Generic Interrupt Controller using KVM in-kernel support 3 * 4 * Copyright (c) 2012 Linaro Limited 5 * Written by Peter Maydell 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program 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 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "hw/sysbus.h" 22 #include "sysemu/kvm.h" 23 #include "kvm_arm.h" 24 #include "gic_internal.h" 25 26 #define TYPE_KVM_ARM_GIC "kvm-arm-gic" 27 #define KVM_ARM_GIC(obj) \ 28 OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC) 29 #define KVM_ARM_GIC_CLASS(klass) \ 30 OBJECT_CLASS_CHECK(KVMARMGICClass, (klass), TYPE_KVM_ARM_GIC) 31 #define KVM_ARM_GIC_GET_CLASS(obj) \ 32 OBJECT_GET_CLASS(KVMARMGICClass, (obj), TYPE_KVM_ARM_GIC) 33 34 typedef struct KVMARMGICClass { 35 ARMGICCommonClass parent_class; 36 DeviceRealize parent_realize; 37 void (*parent_reset)(DeviceState *dev); 38 } KVMARMGICClass; 39 40 static void kvm_arm_gic_set_irq(void *opaque, int irq, int level) 41 { 42 /* Meaning of the 'irq' parameter: 43 * [0..N-1] : external interrupts 44 * [N..N+31] : PPI (internal) interrupts for CPU 0 45 * [N+32..N+63] : PPI (internal interrupts for CPU 1 46 * ... 47 * Convert this to the kernel's desired encoding, which 48 * has separate fields in the irq number for type, 49 * CPU number and interrupt number. 50 */ 51 GICState *s = (GICState *)opaque; 52 int kvm_irq, irqtype, cpu; 53 54 if (irq < (s->num_irq - GIC_INTERNAL)) { 55 /* External interrupt. The kernel numbers these like the GIC 56 * hardware, with external interrupt IDs starting after the 57 * internal ones. 58 */ 59 irqtype = KVM_ARM_IRQ_TYPE_SPI; 60 cpu = 0; 61 irq += GIC_INTERNAL; 62 } else { 63 /* Internal interrupt: decode into (cpu, interrupt id) */ 64 irqtype = KVM_ARM_IRQ_TYPE_PPI; 65 irq -= (s->num_irq - GIC_INTERNAL); 66 cpu = irq / GIC_INTERNAL; 67 irq %= GIC_INTERNAL; 68 } 69 kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) 70 | (cpu << KVM_ARM_IRQ_VCPU_SHIFT) | irq; 71 72 kvm_set_irq(kvm_state, kvm_irq, !!level); 73 } 74 75 static void kvm_arm_gic_put(GICState *s) 76 { 77 /* TODO: there isn't currently a kernel interface to set the GIC state */ 78 } 79 80 static void kvm_arm_gic_get(GICState *s) 81 { 82 /* TODO: there isn't currently a kernel interface to get the GIC state */ 83 } 84 85 static void kvm_arm_gic_reset(DeviceState *dev) 86 { 87 GICState *s = ARM_GIC_COMMON(dev); 88 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s); 89 90 kgc->parent_reset(dev); 91 kvm_arm_gic_put(s); 92 } 93 94 static void kvm_arm_gic_realize(DeviceState *dev, Error **errp) 95 { 96 int i; 97 GICState *s = KVM_ARM_GIC(dev); 98 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 99 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s); 100 101 kgc->parent_realize(dev, errp); 102 if (error_is_set(errp)) { 103 return; 104 } 105 106 i = s->num_irq - GIC_INTERNAL; 107 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU. 108 * GPIO array layout is thus: 109 * [0..N-1] SPIs 110 * [N..N+31] PPIs for CPU 0 111 * [N+32..N+63] PPIs for CPU 1 112 * ... 113 */ 114 i += (GIC_INTERNAL * s->num_cpu); 115 qdev_init_gpio_in(dev, kvm_arm_gic_set_irq, i); 116 /* We never use our outbound IRQ lines but provide them so that 117 * we maintain the same interface as the non-KVM GIC. 118 */ 119 for (i = 0; i < s->num_cpu; i++) { 120 sysbus_init_irq(sbd, &s->parent_irq[i]); 121 } 122 /* Distributor */ 123 memory_region_init_reservation(&s->iomem, "kvm-gic_dist", 0x1000); 124 sysbus_init_mmio(sbd, &s->iomem); 125 kvm_arm_register_device(&s->iomem, 126 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT) 127 | KVM_VGIC_V2_ADDR_TYPE_DIST); 128 /* CPU interface for current core. Unlike arm_gic, we don't 129 * provide the "interface for core #N" memory regions, because 130 * cores with a VGIC don't have those. 131 */ 132 memory_region_init_reservation(&s->cpuiomem[0], "kvm-gic_cpu", 0x1000); 133 sysbus_init_mmio(sbd, &s->cpuiomem[0]); 134 kvm_arm_register_device(&s->cpuiomem[0], 135 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT) 136 | KVM_VGIC_V2_ADDR_TYPE_CPU); 137 } 138 139 static void kvm_arm_gic_class_init(ObjectClass *klass, void *data) 140 { 141 DeviceClass *dc = DEVICE_CLASS(klass); 142 ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass); 143 KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass); 144 145 agcc->pre_save = kvm_arm_gic_get; 146 agcc->post_load = kvm_arm_gic_put; 147 kgc->parent_realize = dc->realize; 148 kgc->parent_reset = dc->reset; 149 dc->realize = kvm_arm_gic_realize; 150 dc->reset = kvm_arm_gic_reset; 151 dc->no_user = 1; 152 } 153 154 static const TypeInfo kvm_arm_gic_info = { 155 .name = TYPE_KVM_ARM_GIC, 156 .parent = TYPE_ARM_GIC_COMMON, 157 .instance_size = sizeof(GICState), 158 .class_init = kvm_arm_gic_class_init, 159 .class_size = sizeof(KVMARMGICClass), 160 }; 161 162 static void kvm_arm_gic_register_types(void) 163 { 164 type_register_static(&kvm_arm_gic_info); 165 } 166 167 type_init(kvm_arm_gic_register_types) 168