xref: /qemu/hw/intc/arm_gicv3_common.c (revision 3faf2b0cd5451c452fdaab32f9d2fb870b084f80)
1 /*
2  * ARM GICv3 support - common bits of emulated and KVM kernel model
3  *
4  * Copyright (c) 2012 Linaro Limited
5  * Copyright (c) 2015 Huawei.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  * Written by Peter Maydell
8  * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qom/cpu.h"
27 #include "hw/intc/arm_gicv3_common.h"
28 #include "gicv3_internal.h"
29 #include "hw/arm/linux-boot-if.h"
30 
31 static void gicv3_pre_save(void *opaque)
32 {
33     GICv3State *s = (GICv3State *)opaque;
34     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
35 
36     if (c->pre_save) {
37         c->pre_save(s);
38     }
39 }
40 
41 static int gicv3_post_load(void *opaque, int version_id)
42 {
43     GICv3State *s = (GICv3State *)opaque;
44     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
45 
46     if (c->post_load) {
47         c->post_load(s);
48     }
49     return 0;
50 }
51 
52 static const VMStateDescription vmstate_gicv3 = {
53     .name = "arm_gicv3",
54     .unmigratable = 1,
55     .pre_save = gicv3_pre_save,
56     .post_load = gicv3_post_load,
57 };
58 
59 void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
60                               const MemoryRegionOps *ops)
61 {
62     SysBusDevice *sbd = SYS_BUS_DEVICE(s);
63     int i;
64 
65     /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
66      * GPIO array layout is thus:
67      *  [0..N-1] spi
68      *  [N..N+31] PPIs for CPU 0
69      *  [N+32..N+63] PPIs for CPU 1
70      *   ...
71      */
72     i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu;
73     qdev_init_gpio_in(DEVICE(s), handler, i);
74 
75     for (i = 0; i < s->num_cpu; i++) {
76         sysbus_init_irq(sbd, &s->cpu[i].parent_irq);
77     }
78     for (i = 0; i < s->num_cpu; i++) {
79         sysbus_init_irq(sbd, &s->cpu[i].parent_fiq);
80     }
81 
82     memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s,
83                           "gicv3_dist", 0x10000);
84     memory_region_init_io(&s->iomem_redist, OBJECT(s), ops ? &ops[1] : NULL, s,
85                           "gicv3_redist", 0x20000 * s->num_cpu);
86 
87     sysbus_init_mmio(sbd, &s->iomem_dist);
88     sysbus_init_mmio(sbd, &s->iomem_redist);
89 }
90 
91 static void arm_gicv3_common_realize(DeviceState *dev, Error **errp)
92 {
93     GICv3State *s = ARM_GICV3_COMMON(dev);
94     int i;
95 
96     /* revision property is actually reserved and currently used only in order
97      * to keep the interface compatible with GICv2 code, avoiding extra
98      * conditions. However, in future it could be used, for example, if we
99      * implement GICv4.
100      */
101     if (s->revision != 3) {
102         error_setg(errp, "unsupported GIC revision %d", s->revision);
103         return;
104     }
105 
106     if (s->num_irq > GICV3_MAXIRQ) {
107         error_setg(errp,
108                    "requested %u interrupt lines exceeds GIC maximum %d",
109                    s->num_irq, GICV3_MAXIRQ);
110         return;
111     }
112     if (s->num_irq < GIC_INTERNAL) {
113         error_setg(errp,
114                    "requested %u interrupt lines is below GIC minimum %d",
115                    s->num_irq, GIC_INTERNAL);
116         return;
117     }
118 
119     /* ITLinesNumber is represented as (N / 32) - 1, so this is an
120      * implementation imposed restriction, not an architectural one,
121      * so we don't have to deal with bitfields where only some of the
122      * bits in a 32-bit word should be valid.
123      */
124     if (s->num_irq % 32) {
125         error_setg(errp,
126                    "%d interrupt lines unsupported: not divisible by 32",
127                    s->num_irq);
128         return;
129     }
130 
131     s->cpu = g_new0(GICv3CPUState, s->num_cpu);
132 
133     for (i = 0; i < s->num_cpu; i++) {
134         CPUState *cpu = qemu_get_cpu(i);
135         uint64_t cpu_affid;
136         int last;
137 
138         s->cpu[i].cpu = cpu;
139         s->cpu[i].gic = s;
140 
141         /* Pre-construct the GICR_TYPER:
142          * For our implementation:
143          *  Top 32 bits are the affinity value of the associated CPU
144          *  CommonLPIAff == 01 (redistributors with same Aff3 share LPI table)
145          *  Processor_Number == CPU index starting from 0
146          *  DPGS == 0 (GICR_CTLR.DPG* not supported)
147          *  Last == 1 if this is the last redistributor in a series of
148          *            contiguous redistributor pages
149          *  DirectLPI == 0 (direct injection of LPIs not supported)
150          *  VLPIS == 0 (virtual LPIs not supported)
151          *  PLPIS == 0 (physical LPIs not supported)
152          */
153         cpu_affid = object_property_get_int(OBJECT(cpu), "mp-affinity", NULL);
154         last = (i == s->num_cpu - 1);
155 
156         /* The CPU mp-affinity property is in MPIDR register format; squash
157          * the affinity bytes into 32 bits as the GICR_TYPER has them.
158          */
159         cpu_affid = (cpu_affid & 0xFF00000000ULL >> 8) | (cpu_affid & 0xFFFFFF);
160         s->cpu[i].gicr_typer = (cpu_affid << 32) |
161             (1 << 24) |
162             (i << 8) |
163             (last << 4);
164     }
165 }
166 
167 static void arm_gicv3_common_reset(DeviceState *dev)
168 {
169     GICv3State *s = ARM_GICV3_COMMON(dev);
170     int i;
171 
172     for (i = 0; i < s->num_cpu; i++) {
173         GICv3CPUState *cs = &s->cpu[i];
174 
175         cs->level = 0;
176         cs->gicr_ctlr = 0;
177         cs->gicr_statusr[GICV3_S] = 0;
178         cs->gicr_statusr[GICV3_NS] = 0;
179         cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep;
180         cs->gicr_propbaser = 0;
181         cs->gicr_pendbaser = 0;
182         /* If we're resetting a TZ-aware GIC as if secure firmware
183          * had set it up ready to start a kernel in non-secure, we
184          * need to set interrupts to group 1 so the kernel can use them.
185          * Otherwise they reset to group 0 like the hardware.
186          */
187         if (s->irq_reset_nonsecure) {
188             cs->gicr_igroupr0 = 0xffffffff;
189         } else {
190             cs->gicr_igroupr0 = 0;
191         }
192 
193         cs->gicr_ienabler0 = 0;
194         cs->gicr_ipendr0 = 0;
195         cs->gicr_iactiver0 = 0;
196         cs->edge_trigger = 0xffff;
197         cs->gicr_igrpmodr0 = 0;
198         cs->gicr_nsacr = 0;
199         memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr));
200 
201         /* State in the CPU interface must *not* be reset here, because it
202          * is part of the CPU's reset domain, not the GIC device's.
203          */
204     }
205 
206     /* For our implementation affinity routing is always enabled */
207     if (s->security_extn) {
208         s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS;
209     } else {
210         s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE;
211     }
212 
213     s->gicd_statusr[GICV3_S] = 0;
214     s->gicd_statusr[GICV3_NS] = 0;
215 
216     memset(s->group, 0, sizeof(s->group));
217     memset(s->grpmod, 0, sizeof(s->grpmod));
218     memset(s->enabled, 0, sizeof(s->enabled));
219     memset(s->pending, 0, sizeof(s->pending));
220     memset(s->active, 0, sizeof(s->active));
221     memset(s->level, 0, sizeof(s->level));
222     memset(s->edge_trigger, 0, sizeof(s->edge_trigger));
223     memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority));
224     memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter));
225     memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr));
226 
227     if (s->irq_reset_nonsecure) {
228         /* If we're resetting a TZ-aware GIC as if secure firmware
229          * had set it up ready to start a kernel in non-secure, we
230          * need to set interrupts to group 1 so the kernel can use them.
231          * Otherwise they reset to group 0 like the hardware.
232          */
233         for (i = GIC_INTERNAL; i < s->num_irq; i++) {
234             gicv3_gicd_group_set(s, i);
235         }
236     }
237 }
238 
239 static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
240                                       bool secure_boot)
241 {
242     GICv3State *s = ARM_GICV3_COMMON(obj);
243 
244     if (s->security_extn && !secure_boot) {
245         /* We're directly booting a kernel into NonSecure. If this GIC
246          * implements the security extensions then we must configure it
247          * to have all the interrupts be NonSecure (this is a job that
248          * is done by the Secure boot firmware in real hardware, and in
249          * this mode QEMU is acting as a minimalist firmware-and-bootloader
250          * equivalent).
251          */
252         s->irq_reset_nonsecure = true;
253     }
254 }
255 
256 static Property arm_gicv3_common_properties[] = {
257     DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1),
258     DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32),
259     DEFINE_PROP_UINT32("revision", GICv3State, revision, 3),
260     DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0),
261     DEFINE_PROP_END_OF_LIST(),
262 };
263 
264 static void arm_gicv3_common_class_init(ObjectClass *klass, void *data)
265 {
266     DeviceClass *dc = DEVICE_CLASS(klass);
267     ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
268 
269     dc->reset = arm_gicv3_common_reset;
270     dc->realize = arm_gicv3_common_realize;
271     dc->props = arm_gicv3_common_properties;
272     dc->vmsd = &vmstate_gicv3;
273     albifc->arm_linux_init = arm_gic_common_linux_init;
274 }
275 
276 static const TypeInfo arm_gicv3_common_type = {
277     .name = TYPE_ARM_GICV3_COMMON,
278     .parent = TYPE_SYS_BUS_DEVICE,
279     .instance_size = sizeof(GICv3State),
280     .class_size = sizeof(ARMGICv3CommonClass),
281     .class_init = arm_gicv3_common_class_init,
282     .abstract = true,
283     .interfaces = (InterfaceInfo []) {
284         { TYPE_ARM_LINUX_BOOT_IF },
285         { },
286     },
287 };
288 
289 static void register_types(void)
290 {
291     type_register_static(&arm_gicv3_common_type);
292 }
293 
294 type_init(register_types)
295