xref: /openbmc/qemu/hw/intc/arm_gic_kvm.c (revision 1da41cc1c6c3efbe2ed47228068bd80dbdc49d0e)
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     int ret;
101 
102     kgc->parent_realize(dev, errp);
103     if (error_is_set(errp)) {
104         return;
105     }
106 
107     i = s->num_irq - GIC_INTERNAL;
108     /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
109      * GPIO array layout is thus:
110      *  [0..N-1] SPIs
111      *  [N..N+31] PPIs for CPU 0
112      *  [N+32..N+63] PPIs for CPU 1
113      *   ...
114      */
115     i += (GIC_INTERNAL * s->num_cpu);
116     qdev_init_gpio_in(dev, kvm_arm_gic_set_irq, i);
117     /* We never use our outbound IRQ lines but provide them so that
118      * we maintain the same interface as the non-KVM GIC.
119      */
120     for (i = 0; i < s->num_cpu; i++) {
121         sysbus_init_irq(sbd, &s->parent_irq[i]);
122     }
123 
124     /* Try to create the device via the device control API */
125     s->dev_fd = -1;
126     ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false);
127     if (ret >= 0) {
128         s->dev_fd = ret;
129     } else if (ret != -ENODEV && ret != -ENOTSUP) {
130         error_setg_errno(errp, -ret, "error creating in-kernel VGIC");
131         return;
132     }
133 
134     /* Distributor */
135     memory_region_init_reservation(&s->iomem, OBJECT(s),
136                                    "kvm-gic_dist", 0x1000);
137     sysbus_init_mmio(sbd, &s->iomem);
138     kvm_arm_register_device(&s->iomem,
139                             (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
140                             | KVM_VGIC_V2_ADDR_TYPE_DIST,
141                             KVM_DEV_ARM_VGIC_GRP_ADDR,
142                             KVM_VGIC_V2_ADDR_TYPE_DIST,
143                             s->dev_fd);
144     /* CPU interface for current core. Unlike arm_gic, we don't
145      * provide the "interface for core #N" memory regions, because
146      * cores with a VGIC don't have those.
147      */
148     memory_region_init_reservation(&s->cpuiomem[0], OBJECT(s),
149                                    "kvm-gic_cpu", 0x1000);
150     sysbus_init_mmio(sbd, &s->cpuiomem[0]);
151     kvm_arm_register_device(&s->cpuiomem[0],
152                             (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
153                             | KVM_VGIC_V2_ADDR_TYPE_CPU,
154                             KVM_DEV_ARM_VGIC_GRP_ADDR,
155                             KVM_VGIC_V2_ADDR_TYPE_CPU,
156                             s->dev_fd);
157 }
158 
159 static void kvm_arm_gic_class_init(ObjectClass *klass, void *data)
160 {
161     DeviceClass *dc = DEVICE_CLASS(klass);
162     ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass);
163     KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass);
164 
165     agcc->pre_save = kvm_arm_gic_get;
166     agcc->post_load = kvm_arm_gic_put;
167     kgc->parent_realize = dc->realize;
168     kgc->parent_reset = dc->reset;
169     dc->realize = kvm_arm_gic_realize;
170     dc->reset = kvm_arm_gic_reset;
171 }
172 
173 static const TypeInfo kvm_arm_gic_info = {
174     .name = TYPE_KVM_ARM_GIC,
175     .parent = TYPE_ARM_GIC_COMMON,
176     .instance_size = sizeof(GICState),
177     .class_init = kvm_arm_gic_class_init,
178     .class_size = sizeof(KVMARMGICClass),
179 };
180 
181 static void kvm_arm_gic_register_types(void)
182 {
183     type_register_static(&kvm_arm_gic_info);
184 }
185 
186 type_init(kvm_arm_gic_register_types)
187