xref: /openbmc/qemu/hw/intc/arm_gicv3_common.c (revision d0e31a10)
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 bool virt_state_needed(void *opaque)
53 {
54     GICv3CPUState *cs = opaque;
55 
56     return cs->num_list_regs != 0;
57 }
58 
59 static const VMStateDescription vmstate_gicv3_cpu_virt = {
60     .name = "arm_gicv3_cpu/virt",
61     .version_id = 1,
62     .minimum_version_id = 1,
63     .needed = virt_state_needed,
64     .fields = (VMStateField[]) {
65         VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4),
66         VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState),
67         VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX),
68         VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState),
69         VMSTATE_END_OF_LIST()
70     }
71 };
72 
73 static int icc_sre_el1_reg_pre_load(void *opaque)
74 {
75     GICv3CPUState *cs = opaque;
76 
77    /*
78     * If the sre_el1 subsection is not transferred this
79     * means SRE_EL1 is 0x7 (which might not be the same as
80     * our reset value).
81     */
82     cs->icc_sre_el1 = 0x7;
83     return 0;
84 }
85 
86 static bool icc_sre_el1_reg_needed(void *opaque)
87 {
88     GICv3CPUState *cs = opaque;
89 
90     return cs->icc_sre_el1 != 7;
91 }
92 
93 const VMStateDescription vmstate_gicv3_cpu_sre_el1 = {
94     .name = "arm_gicv3_cpu/sre_el1",
95     .version_id = 1,
96     .minimum_version_id = 1,
97     .pre_load = icc_sre_el1_reg_pre_load,
98     .needed = icc_sre_el1_reg_needed,
99     .fields = (VMStateField[]) {
100         VMSTATE_UINT64(icc_sre_el1, GICv3CPUState),
101         VMSTATE_END_OF_LIST()
102     }
103 };
104 
105 static const VMStateDescription vmstate_gicv3_cpu = {
106     .name = "arm_gicv3_cpu",
107     .version_id = 1,
108     .minimum_version_id = 1,
109     .fields = (VMStateField[]) {
110         VMSTATE_UINT32(level, GICv3CPUState),
111         VMSTATE_UINT32(gicr_ctlr, GICv3CPUState),
112         VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2),
113         VMSTATE_UINT32(gicr_waker, GICv3CPUState),
114         VMSTATE_UINT64(gicr_propbaser, GICv3CPUState),
115         VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState),
116         VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState),
117         VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState),
118         VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState),
119         VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState),
120         VMSTATE_UINT32(edge_trigger, GICv3CPUState),
121         VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState),
122         VMSTATE_UINT32(gicr_nsacr, GICv3CPUState),
123         VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL),
124         VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2),
125         VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState),
126         VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3),
127         VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4),
128         VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3),
129         VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState),
130         VMSTATE_END_OF_LIST()
131     },
132     .subsections = (const VMStateDescription * []) {
133         &vmstate_gicv3_cpu_virt,
134         NULL
135     },
136     .subsections = (const VMStateDescription * []) {
137         &vmstate_gicv3_cpu_sre_el1,
138         NULL
139     }
140 };
141 
142 static const VMStateDescription vmstate_gicv3 = {
143     .name = "arm_gicv3",
144     .version_id = 1,
145     .minimum_version_id = 1,
146     .pre_save = gicv3_pre_save,
147     .post_load = gicv3_post_load,
148     .fields = (VMStateField[]) {
149         VMSTATE_UINT32(gicd_ctlr, GICv3State),
150         VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2),
151         VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE),
152         VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE),
153         VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE),
154         VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE),
155         VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE),
156         VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE),
157         VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE),
158         VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ),
159         VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ),
160         VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State,
161                              DIV_ROUND_UP(GICV3_MAXIRQ, 16)),
162         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu,
163                                              vmstate_gicv3_cpu, GICv3CPUState),
164         VMSTATE_END_OF_LIST()
165     }
166 };
167 
168 void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
169                               const MemoryRegionOps *ops)
170 {
171     SysBusDevice *sbd = SYS_BUS_DEVICE(s);
172     int i;
173 
174     /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
175      * GPIO array layout is thus:
176      *  [0..N-1] spi
177      *  [N..N+31] PPIs for CPU 0
178      *  [N+32..N+63] PPIs for CPU 1
179      *   ...
180      */
181     i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu;
182     qdev_init_gpio_in(DEVICE(s), handler, i);
183 
184     for (i = 0; i < s->num_cpu; i++) {
185         sysbus_init_irq(sbd, &s->cpu[i].parent_irq);
186     }
187     for (i = 0; i < s->num_cpu; i++) {
188         sysbus_init_irq(sbd, &s->cpu[i].parent_fiq);
189     }
190     for (i = 0; i < s->num_cpu; i++) {
191         sysbus_init_irq(sbd, &s->cpu[i].parent_virq);
192     }
193     for (i = 0; i < s->num_cpu; i++) {
194         sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq);
195     }
196 
197     memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s,
198                           "gicv3_dist", 0x10000);
199     memory_region_init_io(&s->iomem_redist, OBJECT(s), ops ? &ops[1] : NULL, s,
200                           "gicv3_redist", 0x20000 * s->num_cpu);
201 
202     sysbus_init_mmio(sbd, &s->iomem_dist);
203     sysbus_init_mmio(sbd, &s->iomem_redist);
204 }
205 
206 static void arm_gicv3_common_realize(DeviceState *dev, Error **errp)
207 {
208     GICv3State *s = ARM_GICV3_COMMON(dev);
209     int i;
210 
211     /* revision property is actually reserved and currently used only in order
212      * to keep the interface compatible with GICv2 code, avoiding extra
213      * conditions. However, in future it could be used, for example, if we
214      * implement GICv4.
215      */
216     if (s->revision != 3) {
217         error_setg(errp, "unsupported GIC revision %d", s->revision);
218         return;
219     }
220 
221     if (s->num_irq > GICV3_MAXIRQ) {
222         error_setg(errp,
223                    "requested %u interrupt lines exceeds GIC maximum %d",
224                    s->num_irq, GICV3_MAXIRQ);
225         return;
226     }
227     if (s->num_irq < GIC_INTERNAL) {
228         error_setg(errp,
229                    "requested %u interrupt lines is below GIC minimum %d",
230                    s->num_irq, GIC_INTERNAL);
231         return;
232     }
233 
234     /* ITLinesNumber is represented as (N / 32) - 1, so this is an
235      * implementation imposed restriction, not an architectural one,
236      * so we don't have to deal with bitfields where only some of the
237      * bits in a 32-bit word should be valid.
238      */
239     if (s->num_irq % 32) {
240         error_setg(errp,
241                    "%d interrupt lines unsupported: not divisible by 32",
242                    s->num_irq);
243         return;
244     }
245 
246     s->cpu = g_new0(GICv3CPUState, s->num_cpu);
247 
248     for (i = 0; i < s->num_cpu; i++) {
249         CPUState *cpu = qemu_get_cpu(i);
250         uint64_t cpu_affid;
251         int last;
252 
253         s->cpu[i].cpu = cpu;
254         s->cpu[i].gic = s;
255         /* Store GICv3CPUState in CPUARMState gicv3state pointer */
256         gicv3_set_gicv3state(cpu, &s->cpu[i]);
257 
258         /* Pre-construct the GICR_TYPER:
259          * For our implementation:
260          *  Top 32 bits are the affinity value of the associated CPU
261          *  CommonLPIAff == 01 (redistributors with same Aff3 share LPI table)
262          *  Processor_Number == CPU index starting from 0
263          *  DPGS == 0 (GICR_CTLR.DPG* not supported)
264          *  Last == 1 if this is the last redistributor in a series of
265          *            contiguous redistributor pages
266          *  DirectLPI == 0 (direct injection of LPIs not supported)
267          *  VLPIS == 0 (virtual LPIs not supported)
268          *  PLPIS == 0 (physical LPIs not supported)
269          */
270         cpu_affid = object_property_get_int(OBJECT(cpu), "mp-affinity", NULL);
271         last = (i == s->num_cpu - 1);
272 
273         /* The CPU mp-affinity property is in MPIDR register format; squash
274          * the affinity bytes into 32 bits as the GICR_TYPER has them.
275          */
276         cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) |
277                      (cpu_affid & 0xFFFFFF);
278         s->cpu[i].gicr_typer = (cpu_affid << 32) |
279             (1 << 24) |
280             (i << 8) |
281             (last << 4);
282     }
283 }
284 
285 static void arm_gicv3_common_reset(DeviceState *dev)
286 {
287     GICv3State *s = ARM_GICV3_COMMON(dev);
288     int i;
289 
290     for (i = 0; i < s->num_cpu; i++) {
291         GICv3CPUState *cs = &s->cpu[i];
292 
293         cs->level = 0;
294         cs->gicr_ctlr = 0;
295         cs->gicr_statusr[GICV3_S] = 0;
296         cs->gicr_statusr[GICV3_NS] = 0;
297         cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep;
298         cs->gicr_propbaser = 0;
299         cs->gicr_pendbaser = 0;
300         /* If we're resetting a TZ-aware GIC as if secure firmware
301          * had set it up ready to start a kernel in non-secure, we
302          * need to set interrupts to group 1 so the kernel can use them.
303          * Otherwise they reset to group 0 like the hardware.
304          */
305         if (s->irq_reset_nonsecure) {
306             cs->gicr_igroupr0 = 0xffffffff;
307         } else {
308             cs->gicr_igroupr0 = 0;
309         }
310 
311         cs->gicr_ienabler0 = 0;
312         cs->gicr_ipendr0 = 0;
313         cs->gicr_iactiver0 = 0;
314         cs->edge_trigger = 0xffff;
315         cs->gicr_igrpmodr0 = 0;
316         cs->gicr_nsacr = 0;
317         memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr));
318 
319         cs->hppi.prio = 0xff;
320 
321         /* State in the CPU interface must *not* be reset here, because it
322          * is part of the CPU's reset domain, not the GIC device's.
323          */
324     }
325 
326     /* For our implementation affinity routing is always enabled */
327     if (s->security_extn) {
328         s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS;
329     } else {
330         s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE;
331     }
332 
333     s->gicd_statusr[GICV3_S] = 0;
334     s->gicd_statusr[GICV3_NS] = 0;
335 
336     memset(s->group, 0, sizeof(s->group));
337     memset(s->grpmod, 0, sizeof(s->grpmod));
338     memset(s->enabled, 0, sizeof(s->enabled));
339     memset(s->pending, 0, sizeof(s->pending));
340     memset(s->active, 0, sizeof(s->active));
341     memset(s->level, 0, sizeof(s->level));
342     memset(s->edge_trigger, 0, sizeof(s->edge_trigger));
343     memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority));
344     memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter));
345     memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr));
346     /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must
347      * write these to get sane behaviour and we need not populate the
348      * pointer cache here; however having the cache be different for
349      * "happened to be 0 from reset" and "guest wrote 0" would be
350      * too confusing.
351      */
352     gicv3_cache_all_target_cpustates(s);
353 
354     if (s->irq_reset_nonsecure) {
355         /* If we're resetting a TZ-aware GIC as if secure firmware
356          * had set it up ready to start a kernel in non-secure, we
357          * need to set interrupts to group 1 so the kernel can use them.
358          * Otherwise they reset to group 0 like the hardware.
359          */
360         for (i = GIC_INTERNAL; i < s->num_irq; i++) {
361             gicv3_gicd_group_set(s, i);
362         }
363     }
364 }
365 
366 static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
367                                       bool secure_boot)
368 {
369     GICv3State *s = ARM_GICV3_COMMON(obj);
370 
371     if (s->security_extn && !secure_boot) {
372         /* We're directly booting a kernel into NonSecure. If this GIC
373          * implements the security extensions then we must configure it
374          * to have all the interrupts be NonSecure (this is a job that
375          * is done by the Secure boot firmware in real hardware, and in
376          * this mode QEMU is acting as a minimalist firmware-and-bootloader
377          * equivalent).
378          */
379         s->irq_reset_nonsecure = true;
380     }
381 }
382 
383 static Property arm_gicv3_common_properties[] = {
384     DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1),
385     DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32),
386     DEFINE_PROP_UINT32("revision", GICv3State, revision, 3),
387     DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0),
388     DEFINE_PROP_END_OF_LIST(),
389 };
390 
391 static void arm_gicv3_common_class_init(ObjectClass *klass, void *data)
392 {
393     DeviceClass *dc = DEVICE_CLASS(klass);
394     ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
395 
396     dc->reset = arm_gicv3_common_reset;
397     dc->realize = arm_gicv3_common_realize;
398     dc->props = arm_gicv3_common_properties;
399     dc->vmsd = &vmstate_gicv3;
400     albifc->arm_linux_init = arm_gic_common_linux_init;
401 }
402 
403 static const TypeInfo arm_gicv3_common_type = {
404     .name = TYPE_ARM_GICV3_COMMON,
405     .parent = TYPE_SYS_BUS_DEVICE,
406     .instance_size = sizeof(GICv3State),
407     .class_size = sizeof(ARMGICv3CommonClass),
408     .class_init = arm_gicv3_common_class_init,
409     .abstract = true,
410     .interfaces = (InterfaceInfo []) {
411         { TYPE_ARM_LINUX_BOOT_IF },
412         { },
413     },
414 };
415 
416 static void register_types(void)
417 {
418     type_register_static(&arm_gicv3_common_type);
419 }
420 
421 type_init(register_types)
422