xref: /qemu/hw/intc/arm_gicv3_common.c (revision 641be69745c49d3c35efb62ee41d21d701b210ba)
1ff8f06eeSShlomo Pongratz /*
2ff8f06eeSShlomo Pongratz  * ARM GICv3 support - common bits of emulated and KVM kernel model
3ff8f06eeSShlomo Pongratz  *
4ff8f06eeSShlomo Pongratz  * Copyright (c) 2012 Linaro Limited
5ff8f06eeSShlomo Pongratz  * Copyright (c) 2015 Huawei.
607e2034dSPavel Fedin  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7ff8f06eeSShlomo Pongratz  * Written by Peter Maydell
807e2034dSPavel Fedin  * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin
9ff8f06eeSShlomo Pongratz  *
10ff8f06eeSShlomo Pongratz  * This program is free software; you can redistribute it and/or modify
11ff8f06eeSShlomo Pongratz  * it under the terms of the GNU General Public License as published by
12ff8f06eeSShlomo Pongratz  * the Free Software Foundation, either version 2 of the License, or
13ff8f06eeSShlomo Pongratz  * (at your option) any later version.
14ff8f06eeSShlomo Pongratz  *
15ff8f06eeSShlomo Pongratz  * This program is distributed in the hope that it will be useful,
16ff8f06eeSShlomo Pongratz  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ff8f06eeSShlomo Pongratz  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ff8f06eeSShlomo Pongratz  * GNU General Public License for more details.
19ff8f06eeSShlomo Pongratz  *
20ff8f06eeSShlomo Pongratz  * You should have received a copy of the GNU General Public License along
21ff8f06eeSShlomo Pongratz  * with this program; if not, see <http://www.gnu.org/licenses/>.
22ff8f06eeSShlomo Pongratz  */
23ff8f06eeSShlomo Pongratz 
248ef94f0bSPeter Maydell #include "qemu/osdep.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
260b8fa32fSMarkus Armbruster #include "qemu/module.h"
272e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
28ff8f06eeSShlomo Pongratz #include "hw/intc/arm_gicv3_common.h"
29a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
30d6454270SMarkus Armbruster #include "migration/vmstate.h"
3107e2034dSPavel Fedin #include "gicv3_internal.h"
3207e2034dSPavel Fedin #include "hw/arm/linux-boot-if.h"
33910e2048SShannon Zhao #include "sysemu/kvm.h"
34ff8f06eeSShlomo Pongratz 
35341823c1SPeter Maydell 
36341823c1SPeter Maydell static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
37341823c1SPeter Maydell {
38341823c1SPeter Maydell     if (cs->gicd_no_migration_shift_bug) {
39341823c1SPeter Maydell         return;
40341823c1SPeter Maydell     }
41341823c1SPeter Maydell 
42341823c1SPeter Maydell     /* Older versions of QEMU had a bug in the handling of state save/restore
43341823c1SPeter Maydell      * to the KVM GICv3: they got the offset in the bitmap arrays wrong,
44341823c1SPeter Maydell      * so that instead of the data for external interrupts 32 and up
45341823c1SPeter Maydell      * starting at bit position 32 in the bitmap, it started at bit
46341823c1SPeter Maydell      * position 64. If we're receiving data from a QEMU with that bug,
47341823c1SPeter Maydell      * we must move the data down into the right place.
48341823c1SPeter Maydell      */
49341823c1SPeter Maydell     memmove(cs->group, (uint8_t *)cs->group + GIC_INTERNAL / 8,
50341823c1SPeter Maydell             sizeof(cs->group) - GIC_INTERNAL / 8);
51341823c1SPeter Maydell     memmove(cs->grpmod, (uint8_t *)cs->grpmod + GIC_INTERNAL / 8,
52341823c1SPeter Maydell             sizeof(cs->grpmod) - GIC_INTERNAL / 8);
53341823c1SPeter Maydell     memmove(cs->enabled, (uint8_t *)cs->enabled + GIC_INTERNAL / 8,
54341823c1SPeter Maydell             sizeof(cs->enabled) - GIC_INTERNAL / 8);
55341823c1SPeter Maydell     memmove(cs->pending, (uint8_t *)cs->pending + GIC_INTERNAL / 8,
56341823c1SPeter Maydell             sizeof(cs->pending) - GIC_INTERNAL / 8);
57341823c1SPeter Maydell     memmove(cs->active, (uint8_t *)cs->active + GIC_INTERNAL / 8,
58341823c1SPeter Maydell             sizeof(cs->active) - GIC_INTERNAL / 8);
59341823c1SPeter Maydell     memmove(cs->edge_trigger, (uint8_t *)cs->edge_trigger + GIC_INTERNAL / 8,
60341823c1SPeter Maydell             sizeof(cs->edge_trigger) - GIC_INTERNAL / 8);
61341823c1SPeter Maydell 
62341823c1SPeter Maydell     /*
63341823c1SPeter Maydell      * While this new version QEMU doesn't have this kind of bug as we fix it,
64341823c1SPeter Maydell      * so it needs to set the flag to true to indicate that and it's necessary
65341823c1SPeter Maydell      * for next migration to work from this new version QEMU.
66341823c1SPeter Maydell      */
67341823c1SPeter Maydell     cs->gicd_no_migration_shift_bug = true;
68341823c1SPeter Maydell }
69341823c1SPeter Maydell 
7044b1ff31SDr. David Alan Gilbert static int gicv3_pre_save(void *opaque)
71ff8f06eeSShlomo Pongratz {
72ff8f06eeSShlomo Pongratz     GICv3State *s = (GICv3State *)opaque;
73ff8f06eeSShlomo Pongratz     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
74ff8f06eeSShlomo Pongratz 
75ff8f06eeSShlomo Pongratz     if (c->pre_save) {
76ff8f06eeSShlomo Pongratz         c->pre_save(s);
77ff8f06eeSShlomo Pongratz     }
7844b1ff31SDr. David Alan Gilbert 
7944b1ff31SDr. David Alan Gilbert     return 0;
80ff8f06eeSShlomo Pongratz }
81ff8f06eeSShlomo Pongratz 
82ff8f06eeSShlomo Pongratz static int gicv3_post_load(void *opaque, int version_id)
83ff8f06eeSShlomo Pongratz {
84ff8f06eeSShlomo Pongratz     GICv3State *s = (GICv3State *)opaque;
85ff8f06eeSShlomo Pongratz     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
86ff8f06eeSShlomo Pongratz 
87341823c1SPeter Maydell     gicv3_gicd_no_migration_shift_bug_post_load(s);
88341823c1SPeter Maydell 
89ff8f06eeSShlomo Pongratz     if (c->post_load) {
90ff8f06eeSShlomo Pongratz         c->post_load(s);
91ff8f06eeSShlomo Pongratz     }
92ff8f06eeSShlomo Pongratz     return 0;
93ff8f06eeSShlomo Pongratz }
94ff8f06eeSShlomo Pongratz 
954eb833b5SPeter Maydell static bool virt_state_needed(void *opaque)
964eb833b5SPeter Maydell {
974eb833b5SPeter Maydell     GICv3CPUState *cs = opaque;
984eb833b5SPeter Maydell 
994eb833b5SPeter Maydell     return cs->num_list_regs != 0;
1004eb833b5SPeter Maydell }
1014eb833b5SPeter Maydell 
1024eb833b5SPeter Maydell static const VMStateDescription vmstate_gicv3_cpu_virt = {
1034eb833b5SPeter Maydell     .name = "arm_gicv3_cpu/virt",
1044eb833b5SPeter Maydell     .version_id = 1,
1054eb833b5SPeter Maydell     .minimum_version_id = 1,
1064eb833b5SPeter Maydell     .needed = virt_state_needed,
1074eb833b5SPeter Maydell     .fields = (VMStateField[]) {
1084eb833b5SPeter Maydell         VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4),
1094eb833b5SPeter Maydell         VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState),
1104eb833b5SPeter Maydell         VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX),
1114eb833b5SPeter Maydell         VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState),
1124eb833b5SPeter Maydell         VMSTATE_END_OF_LIST()
1134eb833b5SPeter Maydell     }
1144eb833b5SPeter Maydell };
1154eb833b5SPeter Maydell 
116326049ccSPeter Maydell static int vmstate_gicv3_cpu_pre_load(void *opaque)
1176692aac4SVijaya Kumar K {
1186692aac4SVijaya Kumar K     GICv3CPUState *cs = opaque;
1196692aac4SVijaya Kumar K 
1206692aac4SVijaya Kumar K    /*
1216692aac4SVijaya Kumar K     * If the sre_el1 subsection is not transferred this
1226692aac4SVijaya Kumar K     * means SRE_EL1 is 0x7 (which might not be the same as
1236692aac4SVijaya Kumar K     * our reset value).
1246692aac4SVijaya Kumar K     */
1256692aac4SVijaya Kumar K     cs->icc_sre_el1 = 0x7;
1266692aac4SVijaya Kumar K     return 0;
1276692aac4SVijaya Kumar K }
1286692aac4SVijaya Kumar K 
1296692aac4SVijaya Kumar K static bool icc_sre_el1_reg_needed(void *opaque)
1306692aac4SVijaya Kumar K {
1316692aac4SVijaya Kumar K     GICv3CPUState *cs = opaque;
1326692aac4SVijaya Kumar K 
1336692aac4SVijaya Kumar K     return cs->icc_sre_el1 != 7;
1346692aac4SVijaya Kumar K }
1356692aac4SVijaya Kumar K 
1366692aac4SVijaya Kumar K const VMStateDescription vmstate_gicv3_cpu_sre_el1 = {
1376692aac4SVijaya Kumar K     .name = "arm_gicv3_cpu/sre_el1",
1386692aac4SVijaya Kumar K     .version_id = 1,
1396692aac4SVijaya Kumar K     .minimum_version_id = 1,
1406692aac4SVijaya Kumar K     .needed = icc_sre_el1_reg_needed,
1416692aac4SVijaya Kumar K     .fields = (VMStateField[]) {
1426692aac4SVijaya Kumar K         VMSTATE_UINT64(icc_sre_el1, GICv3CPUState),
1436692aac4SVijaya Kumar K         VMSTATE_END_OF_LIST()
1446692aac4SVijaya Kumar K     }
1456692aac4SVijaya Kumar K };
1466692aac4SVijaya Kumar K 
147*641be697SPeter Maydell static bool gicv4_needed(void *opaque)
148*641be697SPeter Maydell {
149*641be697SPeter Maydell     GICv3CPUState *cs = opaque;
150*641be697SPeter Maydell 
151*641be697SPeter Maydell     return cs->gic->revision > 3;
152*641be697SPeter Maydell }
153*641be697SPeter Maydell 
154*641be697SPeter Maydell const VMStateDescription vmstate_gicv3_gicv4 = {
155*641be697SPeter Maydell     .name = "arm_gicv3_cpu/gicv4",
156*641be697SPeter Maydell     .version_id = 1,
157*641be697SPeter Maydell     .minimum_version_id = 1,
158*641be697SPeter Maydell     .needed = gicv4_needed,
159*641be697SPeter Maydell     .fields = (VMStateField[]) {
160*641be697SPeter Maydell         VMSTATE_UINT64(gicr_vpropbaser, GICv3CPUState),
161*641be697SPeter Maydell         VMSTATE_UINT64(gicr_vpendbaser, GICv3CPUState),
162*641be697SPeter Maydell         VMSTATE_END_OF_LIST()
163*641be697SPeter Maydell     }
164*641be697SPeter Maydell };
165*641be697SPeter Maydell 
166757caeedSPavel Fedin static const VMStateDescription vmstate_gicv3_cpu = {
167757caeedSPavel Fedin     .name = "arm_gicv3_cpu",
168757caeedSPavel Fedin     .version_id = 1,
169757caeedSPavel Fedin     .minimum_version_id = 1,
170326049ccSPeter Maydell     .pre_load = vmstate_gicv3_cpu_pre_load,
171757caeedSPavel Fedin     .fields = (VMStateField[]) {
172757caeedSPavel Fedin         VMSTATE_UINT32(level, GICv3CPUState),
173757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ctlr, GICv3CPUState),
174757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2),
175757caeedSPavel Fedin         VMSTATE_UINT32(gicr_waker, GICv3CPUState),
176757caeedSPavel Fedin         VMSTATE_UINT64(gicr_propbaser, GICv3CPUState),
177757caeedSPavel Fedin         VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState),
178757caeedSPavel Fedin         VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState),
179757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState),
180757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState),
181757caeedSPavel Fedin         VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState),
182757caeedSPavel Fedin         VMSTATE_UINT32(edge_trigger, GICv3CPUState),
183757caeedSPavel Fedin         VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState),
184757caeedSPavel Fedin         VMSTATE_UINT32(gicr_nsacr, GICv3CPUState),
185757caeedSPavel Fedin         VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL),
186757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2),
187757caeedSPavel Fedin         VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState),
188757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3),
189757caeedSPavel Fedin         VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4),
190757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3),
191757caeedSPavel Fedin         VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState),
192757caeedSPavel Fedin         VMSTATE_END_OF_LIST()
1934eb833b5SPeter Maydell     },
1944eb833b5SPeter Maydell     .subsections = (const VMStateDescription * []) {
1954eb833b5SPeter Maydell         &vmstate_gicv3_cpu_virt,
1966692aac4SVijaya Kumar K         &vmstate_gicv3_cpu_sre_el1,
197*641be697SPeter Maydell         &vmstate_gicv3_gicv4,
1986692aac4SVijaya Kumar K         NULL
199757caeedSPavel Fedin     }
200757caeedSPavel Fedin };
201757caeedSPavel Fedin 
202326049ccSPeter Maydell static int gicv3_pre_load(void *opaque)
203910e2048SShannon Zhao {
204910e2048SShannon Zhao     GICv3State *cs = opaque;
205910e2048SShannon Zhao 
206910e2048SShannon Zhao    /*
207910e2048SShannon Zhao     * The gicd_no_migration_shift_bug flag is used for migration compatibility
208910e2048SShannon Zhao     * for old version QEMU which may have the GICD bmp shift bug under KVM mode.
209910e2048SShannon Zhao     * Strictly, what we want to know is whether the migration source is using
210910e2048SShannon Zhao     * KVM. Since we don't have any way to determine that, we look at whether the
211910e2048SShannon Zhao     * destination is using KVM; this is close enough because for the older QEMU
212910e2048SShannon Zhao     * versions with this bug KVM -> TCG migration didn't work anyway. If the
213910e2048SShannon Zhao     * source is a newer QEMU without this bug it will transmit the migration
214910e2048SShannon Zhao     * subsection which sets the flag to true; otherwise it will remain set to
215910e2048SShannon Zhao     * the value we select here.
216910e2048SShannon Zhao     */
217910e2048SShannon Zhao     if (kvm_enabled()) {
218910e2048SShannon Zhao         cs->gicd_no_migration_shift_bug = false;
219910e2048SShannon Zhao     }
220910e2048SShannon Zhao 
221910e2048SShannon Zhao     return 0;
222910e2048SShannon Zhao }
223910e2048SShannon Zhao 
22478e9ddd7SPeter Maydell static bool needed_always(void *opaque)
22578e9ddd7SPeter Maydell {
22678e9ddd7SPeter Maydell     return true;
22778e9ddd7SPeter Maydell }
22878e9ddd7SPeter Maydell 
229910e2048SShannon Zhao const VMStateDescription vmstate_gicv3_gicd_no_migration_shift_bug = {
230910e2048SShannon Zhao     .name = "arm_gicv3/gicd_no_migration_shift_bug",
231910e2048SShannon Zhao     .version_id = 1,
232910e2048SShannon Zhao     .minimum_version_id = 1,
23378e9ddd7SPeter Maydell     .needed = needed_always,
234910e2048SShannon Zhao     .fields = (VMStateField[]) {
235910e2048SShannon Zhao         VMSTATE_BOOL(gicd_no_migration_shift_bug, GICv3State),
236910e2048SShannon Zhao         VMSTATE_END_OF_LIST()
237910e2048SShannon Zhao     }
238910e2048SShannon Zhao };
239910e2048SShannon Zhao 
240ff8f06eeSShlomo Pongratz static const VMStateDescription vmstate_gicv3 = {
241ff8f06eeSShlomo Pongratz     .name = "arm_gicv3",
242757caeedSPavel Fedin     .version_id = 1,
243757caeedSPavel Fedin     .minimum_version_id = 1,
244326049ccSPeter Maydell     .pre_load = gicv3_pre_load,
245ff8f06eeSShlomo Pongratz     .pre_save = gicv3_pre_save,
246ff8f06eeSShlomo Pongratz     .post_load = gicv3_post_load,
247252a7a6aSEric Auger     .priority = MIG_PRI_GICV3,
248757caeedSPavel Fedin     .fields = (VMStateField[]) {
249757caeedSPavel Fedin         VMSTATE_UINT32(gicd_ctlr, GICv3State),
250757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2),
251757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE),
252757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE),
253757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE),
254757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE),
255757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE),
256757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE),
257757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE),
258757caeedSPavel Fedin         VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ),
259757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ),
260757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State,
261757caeedSPavel Fedin                              DIV_ROUND_UP(GICV3_MAXIRQ, 16)),
262757caeedSPavel Fedin         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu,
263757caeedSPavel Fedin                                              vmstate_gicv3_cpu, GICv3CPUState),
264757caeedSPavel Fedin         VMSTATE_END_OF_LIST()
265910e2048SShannon Zhao     },
266910e2048SShannon Zhao     .subsections = (const VMStateDescription * []) {
267910e2048SShannon Zhao         &vmstate_gicv3_gicd_no_migration_shift_bug,
268910e2048SShannon Zhao         NULL
269757caeedSPavel Fedin     }
270ff8f06eeSShlomo Pongratz };
271ff8f06eeSShlomo Pongratz 
272ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
27301b5ab8cSPeter Maydell                               const MemoryRegionOps *ops)
274ff8f06eeSShlomo Pongratz {
275ff8f06eeSShlomo Pongratz     SysBusDevice *sbd = SYS_BUS_DEVICE(s);
276ff8f06eeSShlomo Pongratz     int i;
277e5cba10eSPeter Maydell     int cpuidx;
278ff8f06eeSShlomo Pongratz 
279ff8f06eeSShlomo Pongratz     /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
280ff8f06eeSShlomo Pongratz      * GPIO array layout is thus:
281ff8f06eeSShlomo Pongratz      *  [0..N-1] spi
282ff8f06eeSShlomo Pongratz      *  [N..N+31] PPIs for CPU 0
283ff8f06eeSShlomo Pongratz      *  [N+32..N+63] PPIs for CPU 1
284ff8f06eeSShlomo Pongratz      *   ...
285ff8f06eeSShlomo Pongratz      */
286ff8f06eeSShlomo Pongratz     i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu;
287ff8f06eeSShlomo Pongratz     qdev_init_gpio_in(DEVICE(s), handler, i);
288ff8f06eeSShlomo Pongratz 
289ff8f06eeSShlomo Pongratz     for (i = 0; i < s->num_cpu; i++) {
2903faf2b0cSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_irq);
291ff8f06eeSShlomo Pongratz     }
292ff8f06eeSShlomo Pongratz     for (i = 0; i < s->num_cpu; i++) {
2933faf2b0cSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_fiq);
294ff8f06eeSShlomo Pongratz     }
295b53db42bSPeter Maydell     for (i = 0; i < s->num_cpu; i++) {
296b53db42bSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_virq);
297b53db42bSPeter Maydell     }
298b53db42bSPeter Maydell     for (i = 0; i < s->num_cpu; i++) {
299b53db42bSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq);
300b53db42bSPeter Maydell     }
301ff8f06eeSShlomo Pongratz 
302ff8f06eeSShlomo Pongratz     memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s,
303ff8f06eeSShlomo Pongratz                           "gicv3_dist", 0x10000);
304ff8f06eeSShlomo Pongratz     sysbus_init_mmio(sbd, &s->iomem_dist);
3051e575b66SEric Auger 
306e5cba10eSPeter Maydell     s->redist_regions = g_new0(GICv3RedistRegion, s->nb_redist_regions);
307e5cba10eSPeter Maydell     cpuidx = 0;
3081e575b66SEric Auger     for (i = 0; i < s->nb_redist_regions; i++) {
3091e575b66SEric Auger         char *name = g_strdup_printf("gicv3_redist_region[%d]", i);
310e5cba10eSPeter Maydell         GICv3RedistRegion *region = &s->redist_regions[i];
3111e575b66SEric Auger 
312e5cba10eSPeter Maydell         region->gic = s;
313e5cba10eSPeter Maydell         region->cpuidx = cpuidx;
314e5cba10eSPeter Maydell         cpuidx += s->redist_region_count[i];
315e5cba10eSPeter Maydell 
316e5cba10eSPeter Maydell         memory_region_init_io(&region->iomem, OBJECT(s),
317e5cba10eSPeter Maydell                               ops ? &ops[1] : NULL, region, name,
318ae3b3ba1SPeter Maydell                               s->redist_region_count[i] * gicv3_redist_size(s));
319e5cba10eSPeter Maydell         sysbus_init_mmio(sbd, &region->iomem);
3201e575b66SEric Auger         g_free(name);
3211e575b66SEric Auger     }
322ff8f06eeSShlomo Pongratz }
323ff8f06eeSShlomo Pongratz 
324ff8f06eeSShlomo Pongratz static void arm_gicv3_common_realize(DeviceState *dev, Error **errp)
325ff8f06eeSShlomo Pongratz {
326ff8f06eeSShlomo Pongratz     GICv3State *s = ARM_GICV3_COMMON(dev);
32704616415SPeter Maydell     int i, rdist_capacity, cpuidx;
328ff8f06eeSShlomo Pongratz 
329ff8f06eeSShlomo Pongratz     /* revision property is actually reserved and currently used only in order
330ff8f06eeSShlomo Pongratz      * to keep the interface compatible with GICv2 code, avoiding extra
331ff8f06eeSShlomo Pongratz      * conditions. However, in future it could be used, for example, if we
332ff8f06eeSShlomo Pongratz      * implement GICv4.
333ff8f06eeSShlomo Pongratz      */
334ff8f06eeSShlomo Pongratz     if (s->revision != 3) {
335ff8f06eeSShlomo Pongratz         error_setg(errp, "unsupported GIC revision %d", s->revision);
336ff8f06eeSShlomo Pongratz         return;
337ff8f06eeSShlomo Pongratz     }
33807e2034dSPavel Fedin 
33907e2034dSPavel Fedin     if (s->num_irq > GICV3_MAXIRQ) {
34007e2034dSPavel Fedin         error_setg(errp,
34107e2034dSPavel Fedin                    "requested %u interrupt lines exceeds GIC maximum %d",
34207e2034dSPavel Fedin                    s->num_irq, GICV3_MAXIRQ);
34307e2034dSPavel Fedin         return;
34407e2034dSPavel Fedin     }
34507e2034dSPavel Fedin     if (s->num_irq < GIC_INTERNAL) {
34607e2034dSPavel Fedin         error_setg(errp,
34707e2034dSPavel Fedin                    "requested %u interrupt lines is below GIC minimum %d",
34807e2034dSPavel Fedin                    s->num_irq, GIC_INTERNAL);
34907e2034dSPavel Fedin         return;
35007e2034dSPavel Fedin     }
35189ac9d0cSPeter Maydell     if (s->num_cpu == 0) {
35289ac9d0cSPeter Maydell         error_setg(errp, "num-cpu must be at least 1");
35389ac9d0cSPeter Maydell         return;
35489ac9d0cSPeter Maydell     }
35507e2034dSPavel Fedin 
35607e2034dSPavel Fedin     /* ITLinesNumber is represented as (N / 32) - 1, so this is an
35707e2034dSPavel Fedin      * implementation imposed restriction, not an architectural one,
35807e2034dSPavel Fedin      * so we don't have to deal with bitfields where only some of the
35907e2034dSPavel Fedin      * bits in a 32-bit word should be valid.
36007e2034dSPavel Fedin      */
36107e2034dSPavel Fedin     if (s->num_irq % 32) {
36207e2034dSPavel Fedin         error_setg(errp,
36307e2034dSPavel Fedin                    "%d interrupt lines unsupported: not divisible by 32",
36407e2034dSPavel Fedin                    s->num_irq);
36507e2034dSPavel Fedin         return;
36607e2034dSPavel Fedin     }
36707e2034dSPavel Fedin 
368ac30dec3SShashi Mallela     if (s->lpi_enable && !s->dma) {
369ac30dec3SShashi Mallela         error_setg(errp, "Redist-ITS: Guest 'sysmem' reference link not set");
370ac30dec3SShashi Mallela         return;
371ac30dec3SShashi Mallela     }
372ac30dec3SShashi Mallela 
37301b5ab8cSPeter Maydell     rdist_capacity = 0;
37401b5ab8cSPeter Maydell     for (i = 0; i < s->nb_redist_regions; i++) {
37501b5ab8cSPeter Maydell         rdist_capacity += s->redist_region_count[i];
37601b5ab8cSPeter Maydell     }
377671927a1SPeter Maydell     if (rdist_capacity != s->num_cpu) {
37801b5ab8cSPeter Maydell         error_setg(errp, "Capacity of the redist regions(%d) "
379671927a1SPeter Maydell                    "does not match the number of vcpus(%d)",
38001b5ab8cSPeter Maydell                    rdist_capacity, s->num_cpu);
38101b5ab8cSPeter Maydell         return;
38201b5ab8cSPeter Maydell     }
38301b5ab8cSPeter Maydell 
384e5ff041fSPeter Maydell     if (s->lpi_enable) {
385e5ff041fSPeter Maydell         address_space_init(&s->dma_as, s->dma,
386e5ff041fSPeter Maydell                            "gicv3-its-sysmem");
387e5ff041fSPeter Maydell     }
388e5ff041fSPeter Maydell 
38907e2034dSPavel Fedin     s->cpu = g_new0(GICv3CPUState, s->num_cpu);
39007e2034dSPavel Fedin 
39107e2034dSPavel Fedin     for (i = 0; i < s->num_cpu; i++) {
39207e2034dSPavel Fedin         CPUState *cpu = qemu_get_cpu(i);
39307e2034dSPavel Fedin         uint64_t cpu_affid;
39407e2034dSPavel Fedin 
39507e2034dSPavel Fedin         s->cpu[i].cpu = cpu;
39607e2034dSPavel Fedin         s->cpu[i].gic = s;
397d3a3e529SVijaya Kumar K         /* Store GICv3CPUState in CPUARMState gicv3state pointer */
398d3a3e529SVijaya Kumar K         gicv3_set_gicv3state(cpu, &s->cpu[i]);
39907e2034dSPavel Fedin 
40007e2034dSPavel Fedin         /* Pre-construct the GICR_TYPER:
40107e2034dSPavel Fedin          * For our implementation:
40207e2034dSPavel Fedin          *  Top 32 bits are the affinity value of the associated CPU
40307e2034dSPavel Fedin          *  CommonLPIAff == 01 (redistributors with same Aff3 share LPI table)
40407e2034dSPavel Fedin          *  Processor_Number == CPU index starting from 0
40507e2034dSPavel Fedin          *  DPGS == 0 (GICR_CTLR.DPG* not supported)
40607e2034dSPavel Fedin          *  Last == 1 if this is the last redistributor in a series of
40707e2034dSPavel Fedin          *            contiguous redistributor pages
40807e2034dSPavel Fedin          *  DirectLPI == 0 (direct injection of LPIs not supported)
40907e2034dSPavel Fedin          *  VLPIS == 0 (virtual LPIs not supported)
41007e2034dSPavel Fedin          *  PLPIS == 0 (physical LPIs not supported)
41107e2034dSPavel Fedin          */
41277a7a367SMarc-André Lureau         cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL);
41307e2034dSPavel Fedin 
41407e2034dSPavel Fedin         /* The CPU mp-affinity property is in MPIDR register format; squash
41507e2034dSPavel Fedin          * the affinity bytes into 32 bits as the GICR_TYPER has them.
41607e2034dSPavel Fedin          */
41792204403SAndrew Jones         cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) |
41892204403SAndrew Jones                      (cpu_affid & 0xFFFFFF);
41907e2034dSPavel Fedin         s->cpu[i].gicr_typer = (cpu_affid << 32) |
42007e2034dSPavel Fedin             (1 << 24) |
42104616415SPeter Maydell             (i << 8);
422ac30dec3SShashi Mallela 
423ac30dec3SShashi Mallela         if (s->lpi_enable) {
424ac30dec3SShashi Mallela             s->cpu[i].gicr_typer |= GICR_TYPER_PLPIS;
425ac30dec3SShashi Mallela         }
42607e2034dSPavel Fedin     }
42704616415SPeter Maydell 
42804616415SPeter Maydell     /*
42904616415SPeter Maydell      * Now go through and set GICR_TYPER.Last for the final
43004616415SPeter Maydell      * redistributor in each region.
43104616415SPeter Maydell      */
43204616415SPeter Maydell     cpuidx = 0;
43304616415SPeter Maydell     for (i = 0; i < s->nb_redist_regions; i++) {
43404616415SPeter Maydell         cpuidx += s->redist_region_count[i];
43504616415SPeter Maydell         s->cpu[cpuidx - 1].gicr_typer |= GICR_TYPER_LAST;
43604616415SPeter Maydell     }
4377c087bd3SPeter Maydell 
4387c087bd3SPeter Maydell     s->itslist = g_ptr_array_new();
439ff8f06eeSShlomo Pongratz }
440ff8f06eeSShlomo Pongratz 
4411e575b66SEric Auger static void arm_gicv3_finalize(Object *obj)
4421e575b66SEric Auger {
4431e575b66SEric Auger     GICv3State *s = ARM_GICV3_COMMON(obj);
4441e575b66SEric Auger 
4451e575b66SEric Auger     g_free(s->redist_region_count);
4461e575b66SEric Auger }
4471e575b66SEric Auger 
448ff8f06eeSShlomo Pongratz static void arm_gicv3_common_reset(DeviceState *dev)
449ff8f06eeSShlomo Pongratz {
45007e2034dSPavel Fedin     GICv3State *s = ARM_GICV3_COMMON(dev);
45107e2034dSPavel Fedin     int i;
45207e2034dSPavel Fedin 
45307e2034dSPavel Fedin     for (i = 0; i < s->num_cpu; i++) {
45407e2034dSPavel Fedin         GICv3CPUState *cs = &s->cpu[i];
45507e2034dSPavel Fedin 
45607e2034dSPavel Fedin         cs->level = 0;
45707e2034dSPavel Fedin         cs->gicr_ctlr = 0;
4581611956bSPeter Maydell         if (s->lpi_enable) {
4591611956bSPeter Maydell             /* Our implementation supports clearing GICR_CTLR.EnableLPIs */
4601611956bSPeter Maydell             cs->gicr_ctlr |= GICR_CTLR_CES;
4611611956bSPeter Maydell         }
46207e2034dSPavel Fedin         cs->gicr_statusr[GICV3_S] = 0;
46307e2034dSPavel Fedin         cs->gicr_statusr[GICV3_NS] = 0;
46407e2034dSPavel Fedin         cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep;
46507e2034dSPavel Fedin         cs->gicr_propbaser = 0;
46607e2034dSPavel Fedin         cs->gicr_pendbaser = 0;
467*641be697SPeter Maydell         cs->gicr_vpropbaser = 0;
468*641be697SPeter Maydell         cs->gicr_vpendbaser = 0;
46907e2034dSPavel Fedin         /* If we're resetting a TZ-aware GIC as if secure firmware
47007e2034dSPavel Fedin          * had set it up ready to start a kernel in non-secure, we
47107e2034dSPavel Fedin          * need to set interrupts to group 1 so the kernel can use them.
47207e2034dSPavel Fedin          * Otherwise they reset to group 0 like the hardware.
47307e2034dSPavel Fedin          */
47407e2034dSPavel Fedin         if (s->irq_reset_nonsecure) {
47507e2034dSPavel Fedin             cs->gicr_igroupr0 = 0xffffffff;
47607e2034dSPavel Fedin         } else {
47707e2034dSPavel Fedin             cs->gicr_igroupr0 = 0;
47807e2034dSPavel Fedin         }
47907e2034dSPavel Fedin 
48007e2034dSPavel Fedin         cs->gicr_ienabler0 = 0;
48107e2034dSPavel Fedin         cs->gicr_ipendr0 = 0;
48207e2034dSPavel Fedin         cs->gicr_iactiver0 = 0;
48307e2034dSPavel Fedin         cs->edge_trigger = 0xffff;
48407e2034dSPavel Fedin         cs->gicr_igrpmodr0 = 0;
48507e2034dSPavel Fedin         cs->gicr_nsacr = 0;
48607e2034dSPavel Fedin         memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr));
48707e2034dSPavel Fedin 
488ce187c3cSPeter Maydell         cs->hppi.prio = 0xff;
48917fb5e36SShashi Mallela         cs->hpplpi.prio = 0xff;
490ce187c3cSPeter Maydell 
49107e2034dSPavel Fedin         /* State in the CPU interface must *not* be reset here, because it
49207e2034dSPavel Fedin          * is part of the CPU's reset domain, not the GIC device's.
49307e2034dSPavel Fedin          */
49407e2034dSPavel Fedin     }
49507e2034dSPavel Fedin 
49607e2034dSPavel Fedin     /* For our implementation affinity routing is always enabled */
49707e2034dSPavel Fedin     if (s->security_extn) {
49807e2034dSPavel Fedin         s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS;
49907e2034dSPavel Fedin     } else {
50007e2034dSPavel Fedin         s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE;
50107e2034dSPavel Fedin     }
50207e2034dSPavel Fedin 
50307e2034dSPavel Fedin     s->gicd_statusr[GICV3_S] = 0;
50407e2034dSPavel Fedin     s->gicd_statusr[GICV3_NS] = 0;
50507e2034dSPavel Fedin 
50607e2034dSPavel Fedin     memset(s->group, 0, sizeof(s->group));
50707e2034dSPavel Fedin     memset(s->grpmod, 0, sizeof(s->grpmod));
50807e2034dSPavel Fedin     memset(s->enabled, 0, sizeof(s->enabled));
50907e2034dSPavel Fedin     memset(s->pending, 0, sizeof(s->pending));
51007e2034dSPavel Fedin     memset(s->active, 0, sizeof(s->active));
51107e2034dSPavel Fedin     memset(s->level, 0, sizeof(s->level));
51207e2034dSPavel Fedin     memset(s->edge_trigger, 0, sizeof(s->edge_trigger));
51307e2034dSPavel Fedin     memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority));
51407e2034dSPavel Fedin     memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter));
51507e2034dSPavel Fedin     memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr));
516ce187c3cSPeter Maydell     /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must
517ce187c3cSPeter Maydell      * write these to get sane behaviour and we need not populate the
518ce187c3cSPeter Maydell      * pointer cache here; however having the cache be different for
519ce187c3cSPeter Maydell      * "happened to be 0 from reset" and "guest wrote 0" would be
520ce187c3cSPeter Maydell      * too confusing.
521ce187c3cSPeter Maydell      */
522ce187c3cSPeter Maydell     gicv3_cache_all_target_cpustates(s);
52307e2034dSPavel Fedin 
52407e2034dSPavel Fedin     if (s->irq_reset_nonsecure) {
52507e2034dSPavel Fedin         /* If we're resetting a TZ-aware GIC as if secure firmware
52607e2034dSPavel Fedin          * had set it up ready to start a kernel in non-secure, we
52707e2034dSPavel Fedin          * need to set interrupts to group 1 so the kernel can use them.
52807e2034dSPavel Fedin          * Otherwise they reset to group 0 like the hardware.
52907e2034dSPavel Fedin          */
53007e2034dSPavel Fedin         for (i = GIC_INTERNAL; i < s->num_irq; i++) {
53107e2034dSPavel Fedin             gicv3_gicd_group_set(s, i);
53207e2034dSPavel Fedin         }
53307e2034dSPavel Fedin     }
534910e2048SShannon Zhao     s->gicd_no_migration_shift_bug = true;
53507e2034dSPavel Fedin }
53607e2034dSPavel Fedin 
53707e2034dSPavel Fedin static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
53807e2034dSPavel Fedin                                       bool secure_boot)
53907e2034dSPavel Fedin {
54007e2034dSPavel Fedin     GICv3State *s = ARM_GICV3_COMMON(obj);
54107e2034dSPavel Fedin 
54207e2034dSPavel Fedin     if (s->security_extn && !secure_boot) {
54307e2034dSPavel Fedin         /* We're directly booting a kernel into NonSecure. If this GIC
54407e2034dSPavel Fedin          * implements the security extensions then we must configure it
54507e2034dSPavel Fedin          * to have all the interrupts be NonSecure (this is a job that
54607e2034dSPavel Fedin          * is done by the Secure boot firmware in real hardware, and in
54707e2034dSPavel Fedin          * this mode QEMU is acting as a minimalist firmware-and-bootloader
54807e2034dSPavel Fedin          * equivalent).
54907e2034dSPavel Fedin          */
55007e2034dSPavel Fedin         s->irq_reset_nonsecure = true;
55107e2034dSPavel Fedin     }
552ff8f06eeSShlomo Pongratz }
553ff8f06eeSShlomo Pongratz 
554ff8f06eeSShlomo Pongratz static Property arm_gicv3_common_properties[] = {
555ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1),
556ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32),
557ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("revision", GICv3State, revision, 3),
558ac30dec3SShashi Mallela     DEFINE_PROP_BOOL("has-lpi", GICv3State, lpi_enable, 0),
559ff8f06eeSShlomo Pongratz     DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0),
5601e575b66SEric Auger     DEFINE_PROP_ARRAY("redist-region-count", GICv3State, nb_redist_regions,
5611e575b66SEric Auger                       redist_region_count, qdev_prop_uint32, uint32_t),
562ac30dec3SShashi Mallela     DEFINE_PROP_LINK("sysmem", GICv3State, dma, TYPE_MEMORY_REGION,
563ac30dec3SShashi Mallela                      MemoryRegion *),
564ff8f06eeSShlomo Pongratz     DEFINE_PROP_END_OF_LIST(),
565ff8f06eeSShlomo Pongratz };
566ff8f06eeSShlomo Pongratz 
567ff8f06eeSShlomo Pongratz static void arm_gicv3_common_class_init(ObjectClass *klass, void *data)
568ff8f06eeSShlomo Pongratz {
569ff8f06eeSShlomo Pongratz     DeviceClass *dc = DEVICE_CLASS(klass);
57007e2034dSPavel Fedin     ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
571ff8f06eeSShlomo Pongratz 
572ff8f06eeSShlomo Pongratz     dc->reset = arm_gicv3_common_reset;
573ff8f06eeSShlomo Pongratz     dc->realize = arm_gicv3_common_realize;
5744f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_gicv3_common_properties);
575ff8f06eeSShlomo Pongratz     dc->vmsd = &vmstate_gicv3;
57607e2034dSPavel Fedin     albifc->arm_linux_init = arm_gic_common_linux_init;
577ff8f06eeSShlomo Pongratz }
578ff8f06eeSShlomo Pongratz 
579ff8f06eeSShlomo Pongratz static const TypeInfo arm_gicv3_common_type = {
580ff8f06eeSShlomo Pongratz     .name = TYPE_ARM_GICV3_COMMON,
581ff8f06eeSShlomo Pongratz     .parent = TYPE_SYS_BUS_DEVICE,
582ff8f06eeSShlomo Pongratz     .instance_size = sizeof(GICv3State),
583ff8f06eeSShlomo Pongratz     .class_size = sizeof(ARMGICv3CommonClass),
584ff8f06eeSShlomo Pongratz     .class_init = arm_gicv3_common_class_init,
5851e575b66SEric Auger     .instance_finalize = arm_gicv3_finalize,
586ff8f06eeSShlomo Pongratz     .abstract = true,
58707e2034dSPavel Fedin     .interfaces = (InterfaceInfo []) {
58807e2034dSPavel Fedin         { TYPE_ARM_LINUX_BOOT_IF },
58907e2034dSPavel Fedin         { },
59007e2034dSPavel Fedin     },
591ff8f06eeSShlomo Pongratz };
592ff8f06eeSShlomo Pongratz 
593ff8f06eeSShlomo Pongratz static void register_types(void)
594ff8f06eeSShlomo Pongratz {
595ff8f06eeSShlomo Pongratz     type_register_static(&arm_gicv3_common_type);
596ff8f06eeSShlomo Pongratz }
597ff8f06eeSShlomo Pongratz 
598ff8f06eeSShlomo Pongratz type_init(register_types)
599