xref: /qemu/hw/intc/arm_gicv3_common.c (revision 45b1f81d9088d102c4b6739281d93bbed88666d4)
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"
270c40daf0SPhilippe Mathieu-Daudé #include "qemu/error-report.h"
282e5b09fdSMarkus Armbruster #include "hw/core/cpu.h"
29ff8f06eeSShlomo Pongratz #include "hw/intc/arm_gicv3_common.h"
30a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
31d6454270SMarkus Armbruster #include "migration/vmstate.h"
3207e2034dSPavel Fedin #include "gicv3_internal.h"
3307e2034dSPavel Fedin #include "hw/arm/linux-boot-if.h"
34910e2048SShannon Zhao #include "sysemu/kvm.h"
35ff8f06eeSShlomo Pongratz 
36341823c1SPeter Maydell 
37341823c1SPeter Maydell static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
38341823c1SPeter Maydell {
39341823c1SPeter Maydell     if (cs->gicd_no_migration_shift_bug) {
40341823c1SPeter Maydell         return;
41341823c1SPeter Maydell     }
42341823c1SPeter Maydell 
43341823c1SPeter Maydell     /* Older versions of QEMU had a bug in the handling of state save/restore
44341823c1SPeter Maydell      * to the KVM GICv3: they got the offset in the bitmap arrays wrong,
45341823c1SPeter Maydell      * so that instead of the data for external interrupts 32 and up
46341823c1SPeter Maydell      * starting at bit position 32 in the bitmap, it started at bit
47341823c1SPeter Maydell      * position 64. If we're receiving data from a QEMU with that bug,
48341823c1SPeter Maydell      * we must move the data down into the right place.
49341823c1SPeter Maydell      */
50341823c1SPeter Maydell     memmove(cs->group, (uint8_t *)cs->group + GIC_INTERNAL / 8,
51341823c1SPeter Maydell             sizeof(cs->group) - GIC_INTERNAL / 8);
52341823c1SPeter Maydell     memmove(cs->grpmod, (uint8_t *)cs->grpmod + GIC_INTERNAL / 8,
53341823c1SPeter Maydell             sizeof(cs->grpmod) - GIC_INTERNAL / 8);
54341823c1SPeter Maydell     memmove(cs->enabled, (uint8_t *)cs->enabled + GIC_INTERNAL / 8,
55341823c1SPeter Maydell             sizeof(cs->enabled) - GIC_INTERNAL / 8);
56341823c1SPeter Maydell     memmove(cs->pending, (uint8_t *)cs->pending + GIC_INTERNAL / 8,
57341823c1SPeter Maydell             sizeof(cs->pending) - GIC_INTERNAL / 8);
58341823c1SPeter Maydell     memmove(cs->active, (uint8_t *)cs->active + GIC_INTERNAL / 8,
59341823c1SPeter Maydell             sizeof(cs->active) - GIC_INTERNAL / 8);
60341823c1SPeter Maydell     memmove(cs->edge_trigger, (uint8_t *)cs->edge_trigger + GIC_INTERNAL / 8,
61341823c1SPeter Maydell             sizeof(cs->edge_trigger) - GIC_INTERNAL / 8);
62341823c1SPeter Maydell 
63341823c1SPeter Maydell     /*
64341823c1SPeter Maydell      * While this new version QEMU doesn't have this kind of bug as we fix it,
65341823c1SPeter Maydell      * so it needs to set the flag to true to indicate that and it's necessary
66341823c1SPeter Maydell      * for next migration to work from this new version QEMU.
67341823c1SPeter Maydell      */
68341823c1SPeter Maydell     cs->gicd_no_migration_shift_bug = true;
69341823c1SPeter Maydell }
70341823c1SPeter Maydell 
7144b1ff31SDr. David Alan Gilbert static int gicv3_pre_save(void *opaque)
72ff8f06eeSShlomo Pongratz {
73ff8f06eeSShlomo Pongratz     GICv3State *s = (GICv3State *)opaque;
74ff8f06eeSShlomo Pongratz     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
75ff8f06eeSShlomo Pongratz 
76ff8f06eeSShlomo Pongratz     if (c->pre_save) {
77ff8f06eeSShlomo Pongratz         c->pre_save(s);
78ff8f06eeSShlomo Pongratz     }
7944b1ff31SDr. David Alan Gilbert 
8044b1ff31SDr. David Alan Gilbert     return 0;
81ff8f06eeSShlomo Pongratz }
82ff8f06eeSShlomo Pongratz 
83ff8f06eeSShlomo Pongratz static int gicv3_post_load(void *opaque, int version_id)
84ff8f06eeSShlomo Pongratz {
85ff8f06eeSShlomo Pongratz     GICv3State *s = (GICv3State *)opaque;
86ff8f06eeSShlomo Pongratz     ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
87ff8f06eeSShlomo Pongratz 
88341823c1SPeter Maydell     gicv3_gicd_no_migration_shift_bug_post_load(s);
89341823c1SPeter Maydell 
90ff8f06eeSShlomo Pongratz     if (c->post_load) {
91ff8f06eeSShlomo Pongratz         c->post_load(s);
92ff8f06eeSShlomo Pongratz     }
93ff8f06eeSShlomo Pongratz     return 0;
94ff8f06eeSShlomo Pongratz }
95ff8f06eeSShlomo Pongratz 
964eb833b5SPeter Maydell static bool virt_state_needed(void *opaque)
974eb833b5SPeter Maydell {
984eb833b5SPeter Maydell     GICv3CPUState *cs = opaque;
994eb833b5SPeter Maydell 
1004eb833b5SPeter Maydell     return cs->num_list_regs != 0;
1014eb833b5SPeter Maydell }
1024eb833b5SPeter Maydell 
1034eb833b5SPeter Maydell static const VMStateDescription vmstate_gicv3_cpu_virt = {
1044eb833b5SPeter Maydell     .name = "arm_gicv3_cpu/virt",
1054eb833b5SPeter Maydell     .version_id = 1,
1064eb833b5SPeter Maydell     .minimum_version_id = 1,
1074eb833b5SPeter Maydell     .needed = virt_state_needed,
108*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
1094eb833b5SPeter Maydell         VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4),
1104eb833b5SPeter Maydell         VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState),
1114eb833b5SPeter Maydell         VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX),
1124eb833b5SPeter Maydell         VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState),
1134eb833b5SPeter Maydell         VMSTATE_END_OF_LIST()
1144eb833b5SPeter Maydell     }
1154eb833b5SPeter Maydell };
1164eb833b5SPeter Maydell 
117326049ccSPeter Maydell static int vmstate_gicv3_cpu_pre_load(void *opaque)
1186692aac4SVijaya Kumar K {
1196692aac4SVijaya Kumar K     GICv3CPUState *cs = opaque;
1206692aac4SVijaya Kumar K 
1216692aac4SVijaya Kumar K    /*
1226692aac4SVijaya Kumar K     * If the sre_el1 subsection is not transferred this
1236692aac4SVijaya Kumar K     * means SRE_EL1 is 0x7 (which might not be the same as
1246692aac4SVijaya Kumar K     * our reset value).
1256692aac4SVijaya Kumar K     */
1266692aac4SVijaya Kumar K     cs->icc_sre_el1 = 0x7;
1276692aac4SVijaya Kumar K     return 0;
1286692aac4SVijaya Kumar K }
1296692aac4SVijaya Kumar K 
1306692aac4SVijaya Kumar K static bool icc_sre_el1_reg_needed(void *opaque)
1316692aac4SVijaya Kumar K {
1326692aac4SVijaya Kumar K     GICv3CPUState *cs = opaque;
1336692aac4SVijaya Kumar K 
1346692aac4SVijaya Kumar K     return cs->icc_sre_el1 != 7;
1356692aac4SVijaya Kumar K }
1366692aac4SVijaya Kumar K 
1376692aac4SVijaya Kumar K const VMStateDescription vmstate_gicv3_cpu_sre_el1 = {
1386692aac4SVijaya Kumar K     .name = "arm_gicv3_cpu/sre_el1",
1396692aac4SVijaya Kumar K     .version_id = 1,
1406692aac4SVijaya Kumar K     .minimum_version_id = 1,
1416692aac4SVijaya Kumar K     .needed = icc_sre_el1_reg_needed,
142*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
1436692aac4SVijaya Kumar K         VMSTATE_UINT64(icc_sre_el1, GICv3CPUState),
1446692aac4SVijaya Kumar K         VMSTATE_END_OF_LIST()
1456692aac4SVijaya Kumar K     }
1466692aac4SVijaya Kumar K };
1476692aac4SVijaya Kumar K 
148641be697SPeter Maydell static bool gicv4_needed(void *opaque)
149641be697SPeter Maydell {
150641be697SPeter Maydell     GICv3CPUState *cs = opaque;
151641be697SPeter Maydell 
152641be697SPeter Maydell     return cs->gic->revision > 3;
153641be697SPeter Maydell }
154641be697SPeter Maydell 
155641be697SPeter Maydell const VMStateDescription vmstate_gicv3_gicv4 = {
156641be697SPeter Maydell     .name = "arm_gicv3_cpu/gicv4",
157641be697SPeter Maydell     .version_id = 1,
158641be697SPeter Maydell     .minimum_version_id = 1,
159641be697SPeter Maydell     .needed = gicv4_needed,
160*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
161641be697SPeter Maydell         VMSTATE_UINT64(gicr_vpropbaser, GICv3CPUState),
162641be697SPeter Maydell         VMSTATE_UINT64(gicr_vpendbaser, GICv3CPUState),
163641be697SPeter Maydell         VMSTATE_END_OF_LIST()
164641be697SPeter Maydell     }
165641be697SPeter Maydell };
166641be697SPeter Maydell 
167757caeedSPavel Fedin static const VMStateDescription vmstate_gicv3_cpu = {
168757caeedSPavel Fedin     .name = "arm_gicv3_cpu",
169757caeedSPavel Fedin     .version_id = 1,
170757caeedSPavel Fedin     .minimum_version_id = 1,
171326049ccSPeter Maydell     .pre_load = vmstate_gicv3_cpu_pre_load,
172*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
173757caeedSPavel Fedin         VMSTATE_UINT32(level, GICv3CPUState),
174757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ctlr, GICv3CPUState),
175757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2),
176757caeedSPavel Fedin         VMSTATE_UINT32(gicr_waker, GICv3CPUState),
177757caeedSPavel Fedin         VMSTATE_UINT64(gicr_propbaser, GICv3CPUState),
178757caeedSPavel Fedin         VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState),
179757caeedSPavel Fedin         VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState),
180757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState),
181757caeedSPavel Fedin         VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState),
182757caeedSPavel Fedin         VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState),
183757caeedSPavel Fedin         VMSTATE_UINT32(edge_trigger, GICv3CPUState),
184757caeedSPavel Fedin         VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState),
185757caeedSPavel Fedin         VMSTATE_UINT32(gicr_nsacr, GICv3CPUState),
186757caeedSPavel Fedin         VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL),
187757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2),
188757caeedSPavel Fedin         VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState),
189757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3),
190757caeedSPavel Fedin         VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4),
191757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3),
192757caeedSPavel Fedin         VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState),
193757caeedSPavel Fedin         VMSTATE_END_OF_LIST()
1944eb833b5SPeter Maydell     },
195*45b1f81dSRichard Henderson     .subsections = (const VMStateDescription * const []) {
1964eb833b5SPeter Maydell         &vmstate_gicv3_cpu_virt,
1976692aac4SVijaya Kumar K         &vmstate_gicv3_cpu_sre_el1,
198641be697SPeter Maydell         &vmstate_gicv3_gicv4,
1996692aac4SVijaya Kumar K         NULL
200757caeedSPavel Fedin     }
201757caeedSPavel Fedin };
202757caeedSPavel Fedin 
203326049ccSPeter Maydell static int gicv3_pre_load(void *opaque)
204910e2048SShannon Zhao {
205910e2048SShannon Zhao     GICv3State *cs = opaque;
206910e2048SShannon Zhao 
207910e2048SShannon Zhao    /*
208910e2048SShannon Zhao     * The gicd_no_migration_shift_bug flag is used for migration compatibility
209910e2048SShannon Zhao     * for old version QEMU which may have the GICD bmp shift bug under KVM mode.
210910e2048SShannon Zhao     * Strictly, what we want to know is whether the migration source is using
211910e2048SShannon Zhao     * KVM. Since we don't have any way to determine that, we look at whether the
212910e2048SShannon Zhao     * destination is using KVM; this is close enough because for the older QEMU
213910e2048SShannon Zhao     * versions with this bug KVM -> TCG migration didn't work anyway. If the
214910e2048SShannon Zhao     * source is a newer QEMU without this bug it will transmit the migration
215910e2048SShannon Zhao     * subsection which sets the flag to true; otherwise it will remain set to
216910e2048SShannon Zhao     * the value we select here.
217910e2048SShannon Zhao     */
218910e2048SShannon Zhao     if (kvm_enabled()) {
219910e2048SShannon Zhao         cs->gicd_no_migration_shift_bug = false;
220910e2048SShannon Zhao     }
221910e2048SShannon Zhao 
222910e2048SShannon Zhao     return 0;
223910e2048SShannon Zhao }
224910e2048SShannon Zhao 
22578e9ddd7SPeter Maydell static bool needed_always(void *opaque)
22678e9ddd7SPeter Maydell {
22778e9ddd7SPeter Maydell     return true;
22878e9ddd7SPeter Maydell }
22978e9ddd7SPeter Maydell 
230910e2048SShannon Zhao const VMStateDescription vmstate_gicv3_gicd_no_migration_shift_bug = {
231910e2048SShannon Zhao     .name = "arm_gicv3/gicd_no_migration_shift_bug",
232910e2048SShannon Zhao     .version_id = 1,
233910e2048SShannon Zhao     .minimum_version_id = 1,
23478e9ddd7SPeter Maydell     .needed = needed_always,
235*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
236910e2048SShannon Zhao         VMSTATE_BOOL(gicd_no_migration_shift_bug, GICv3State),
237910e2048SShannon Zhao         VMSTATE_END_OF_LIST()
238910e2048SShannon Zhao     }
239910e2048SShannon Zhao };
240910e2048SShannon Zhao 
241ff8f06eeSShlomo Pongratz static const VMStateDescription vmstate_gicv3 = {
242ff8f06eeSShlomo Pongratz     .name = "arm_gicv3",
243757caeedSPavel Fedin     .version_id = 1,
244757caeedSPavel Fedin     .minimum_version_id = 1,
245326049ccSPeter Maydell     .pre_load = gicv3_pre_load,
246ff8f06eeSShlomo Pongratz     .pre_save = gicv3_pre_save,
247ff8f06eeSShlomo Pongratz     .post_load = gicv3_post_load,
248252a7a6aSEric Auger     .priority = MIG_PRI_GICV3,
249*45b1f81dSRichard Henderson     .fields = (const VMStateField[]) {
250757caeedSPavel Fedin         VMSTATE_UINT32(gicd_ctlr, GICv3State),
251757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2),
252757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE),
253757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE),
254757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE),
255757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE),
256757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE),
257757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE),
258757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE),
259757caeedSPavel Fedin         VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ),
260757caeedSPavel Fedin         VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ),
261757caeedSPavel Fedin         VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State,
262757caeedSPavel Fedin                              DIV_ROUND_UP(GICV3_MAXIRQ, 16)),
263757caeedSPavel Fedin         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu,
264757caeedSPavel Fedin                                              vmstate_gicv3_cpu, GICv3CPUState),
265757caeedSPavel Fedin         VMSTATE_END_OF_LIST()
266910e2048SShannon Zhao     },
267*45b1f81dSRichard Henderson     .subsections = (const VMStateDescription * const []) {
268910e2048SShannon Zhao         &vmstate_gicv3_gicd_no_migration_shift_bug,
269910e2048SShannon Zhao         NULL
270757caeedSPavel Fedin     }
271ff8f06eeSShlomo Pongratz };
272ff8f06eeSShlomo Pongratz 
273ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
27401b5ab8cSPeter Maydell                               const MemoryRegionOps *ops)
275ff8f06eeSShlomo Pongratz {
276ff8f06eeSShlomo Pongratz     SysBusDevice *sbd = SYS_BUS_DEVICE(s);
277ff8f06eeSShlomo Pongratz     int i;
278e5cba10eSPeter Maydell     int cpuidx;
279ff8f06eeSShlomo Pongratz 
280ff8f06eeSShlomo Pongratz     /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
281ff8f06eeSShlomo Pongratz      * GPIO array layout is thus:
282ff8f06eeSShlomo Pongratz      *  [0..N-1] spi
283ff8f06eeSShlomo Pongratz      *  [N..N+31] PPIs for CPU 0
284ff8f06eeSShlomo Pongratz      *  [N+32..N+63] PPIs for CPU 1
285ff8f06eeSShlomo Pongratz      *   ...
286ff8f06eeSShlomo Pongratz      */
287ff8f06eeSShlomo Pongratz     i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu;
288ff8f06eeSShlomo Pongratz     qdev_init_gpio_in(DEVICE(s), handler, i);
289ff8f06eeSShlomo Pongratz 
290ff8f06eeSShlomo Pongratz     for (i = 0; i < s->num_cpu; i++) {
2913faf2b0cSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_irq);
292ff8f06eeSShlomo Pongratz     }
293ff8f06eeSShlomo Pongratz     for (i = 0; i < s->num_cpu; i++) {
2943faf2b0cSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_fiq);
295ff8f06eeSShlomo Pongratz     }
296b53db42bSPeter Maydell     for (i = 0; i < s->num_cpu; i++) {
297b53db42bSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_virq);
298b53db42bSPeter Maydell     }
299b53db42bSPeter Maydell     for (i = 0; i < s->num_cpu; i++) {
300b53db42bSPeter Maydell         sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq);
301b53db42bSPeter Maydell     }
302ff8f06eeSShlomo Pongratz 
303ff8f06eeSShlomo Pongratz     memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s,
304ff8f06eeSShlomo Pongratz                           "gicv3_dist", 0x10000);
305ff8f06eeSShlomo Pongratz     sysbus_init_mmio(sbd, &s->iomem_dist);
3061e575b66SEric Auger 
307e5cba10eSPeter Maydell     s->redist_regions = g_new0(GICv3RedistRegion, s->nb_redist_regions);
308e5cba10eSPeter Maydell     cpuidx = 0;
3091e575b66SEric Auger     for (i = 0; i < s->nb_redist_regions; i++) {
3101e575b66SEric Auger         char *name = g_strdup_printf("gicv3_redist_region[%d]", i);
311e5cba10eSPeter Maydell         GICv3RedistRegion *region = &s->redist_regions[i];
3121e575b66SEric Auger 
313e5cba10eSPeter Maydell         region->gic = s;
314e5cba10eSPeter Maydell         region->cpuidx = cpuidx;
315e5cba10eSPeter Maydell         cpuidx += s->redist_region_count[i];
316e5cba10eSPeter Maydell 
317e5cba10eSPeter Maydell         memory_region_init_io(&region->iomem, OBJECT(s),
318e5cba10eSPeter Maydell                               ops ? &ops[1] : NULL, region, name,
319ae3b3ba1SPeter Maydell                               s->redist_region_count[i] * gicv3_redist_size(s));
320e5cba10eSPeter Maydell         sysbus_init_mmio(sbd, &region->iomem);
3211e575b66SEric Auger         g_free(name);
3221e575b66SEric Auger     }
323ff8f06eeSShlomo Pongratz }
324ff8f06eeSShlomo Pongratz 
325ff8f06eeSShlomo Pongratz static void arm_gicv3_common_realize(DeviceState *dev, Error **errp)
326ff8f06eeSShlomo Pongratz {
327ff8f06eeSShlomo Pongratz     GICv3State *s = ARM_GICV3_COMMON(dev);
32804616415SPeter Maydell     int i, rdist_capacity, cpuidx;
329ff8f06eeSShlomo Pongratz 
330445d5825SPeter Maydell     /*
331445d5825SPeter Maydell      * This GIC device supports only revisions 3 and 4. The GICv1/v2
332445d5825SPeter Maydell      * is a separate device.
333445d5825SPeter Maydell      * Note that subclasses of this device may impose further restrictions
334445d5825SPeter Maydell      * on the GIC revision: notably, the in-kernel KVM GIC doesn't
335445d5825SPeter Maydell      * support GICv4.
336ff8f06eeSShlomo Pongratz      */
337445d5825SPeter Maydell     if (s->revision != 3 && s->revision != 4) {
338ff8f06eeSShlomo Pongratz         error_setg(errp, "unsupported GIC revision %d", s->revision);
339ff8f06eeSShlomo Pongratz         return;
340ff8f06eeSShlomo Pongratz     }
34107e2034dSPavel Fedin 
34207e2034dSPavel Fedin     if (s->num_irq > GICV3_MAXIRQ) {
34307e2034dSPavel Fedin         error_setg(errp,
34407e2034dSPavel Fedin                    "requested %u interrupt lines exceeds GIC maximum %d",
34507e2034dSPavel Fedin                    s->num_irq, GICV3_MAXIRQ);
34607e2034dSPavel Fedin         return;
34707e2034dSPavel Fedin     }
34807e2034dSPavel Fedin     if (s->num_irq < GIC_INTERNAL) {
34907e2034dSPavel Fedin         error_setg(errp,
35007e2034dSPavel Fedin                    "requested %u interrupt lines is below GIC minimum %d",
35107e2034dSPavel Fedin                    s->num_irq, GIC_INTERNAL);
35207e2034dSPavel Fedin         return;
35307e2034dSPavel Fedin     }
35489ac9d0cSPeter Maydell     if (s->num_cpu == 0) {
35589ac9d0cSPeter Maydell         error_setg(errp, "num-cpu must be at least 1");
35689ac9d0cSPeter Maydell         return;
35789ac9d0cSPeter Maydell     }
35807e2034dSPavel Fedin 
35907e2034dSPavel Fedin     /* ITLinesNumber is represented as (N / 32) - 1, so this is an
36007e2034dSPavel Fedin      * implementation imposed restriction, not an architectural one,
36107e2034dSPavel Fedin      * so we don't have to deal with bitfields where only some of the
36207e2034dSPavel Fedin      * bits in a 32-bit word should be valid.
36307e2034dSPavel Fedin      */
36407e2034dSPavel Fedin     if (s->num_irq % 32) {
36507e2034dSPavel Fedin         error_setg(errp,
36607e2034dSPavel Fedin                    "%d interrupt lines unsupported: not divisible by 32",
36707e2034dSPavel Fedin                    s->num_irq);
36807e2034dSPavel Fedin         return;
36907e2034dSPavel Fedin     }
37007e2034dSPavel Fedin 
371ac30dec3SShashi Mallela     if (s->lpi_enable && !s->dma) {
372ac30dec3SShashi Mallela         error_setg(errp, "Redist-ITS: Guest 'sysmem' reference link not set");
373ac30dec3SShashi Mallela         return;
374ac30dec3SShashi Mallela     }
375ac30dec3SShashi Mallela 
37601b5ab8cSPeter Maydell     rdist_capacity = 0;
37701b5ab8cSPeter Maydell     for (i = 0; i < s->nb_redist_regions; i++) {
37801b5ab8cSPeter Maydell         rdist_capacity += s->redist_region_count[i];
37901b5ab8cSPeter Maydell     }
380671927a1SPeter Maydell     if (rdist_capacity != s->num_cpu) {
38101b5ab8cSPeter Maydell         error_setg(errp, "Capacity of the redist regions(%d) "
382671927a1SPeter Maydell                    "does not match the number of vcpus(%d)",
38301b5ab8cSPeter Maydell                    rdist_capacity, s->num_cpu);
38401b5ab8cSPeter Maydell         return;
38501b5ab8cSPeter Maydell     }
38601b5ab8cSPeter Maydell 
387e5ff041fSPeter Maydell     if (s->lpi_enable) {
388e5ff041fSPeter Maydell         address_space_init(&s->dma_as, s->dma,
389e5ff041fSPeter Maydell                            "gicv3-its-sysmem");
390e5ff041fSPeter Maydell     }
391e5ff041fSPeter Maydell 
39207e2034dSPavel Fedin     s->cpu = g_new0(GICv3CPUState, s->num_cpu);
39307e2034dSPavel Fedin 
39407e2034dSPavel Fedin     for (i = 0; i < s->num_cpu; i++) {
39507e2034dSPavel Fedin         CPUState *cpu = qemu_get_cpu(i);
39607e2034dSPavel Fedin         uint64_t cpu_affid;
39707e2034dSPavel Fedin 
39807e2034dSPavel Fedin         s->cpu[i].cpu = cpu;
39907e2034dSPavel Fedin         s->cpu[i].gic = s;
400d3a3e529SVijaya Kumar K         /* Store GICv3CPUState in CPUARMState gicv3state pointer */
401d3a3e529SVijaya Kumar K         gicv3_set_gicv3state(cpu, &s->cpu[i]);
40207e2034dSPavel Fedin 
40307e2034dSPavel Fedin         /* Pre-construct the GICR_TYPER:
40407e2034dSPavel Fedin          * For our implementation:
40507e2034dSPavel Fedin          *  Top 32 bits are the affinity value of the associated CPU
40607e2034dSPavel Fedin          *  CommonLPIAff == 01 (redistributors with same Aff3 share LPI table)
40707e2034dSPavel Fedin          *  Processor_Number == CPU index starting from 0
40807e2034dSPavel Fedin          *  DPGS == 0 (GICR_CTLR.DPG* not supported)
40907e2034dSPavel Fedin          *  Last == 1 if this is the last redistributor in a series of
41007e2034dSPavel Fedin          *            contiguous redistributor pages
41107e2034dSPavel Fedin          *  DirectLPI == 0 (direct injection of LPIs not supported)
412e2d5e189SPeter Maydell          *  VLPIS == 1 if vLPIs supported (GICv4 and up)
413e2d5e189SPeter Maydell          *  PLPIS == 1 if LPIs supported
41407e2034dSPavel Fedin          */
41577a7a367SMarc-André Lureau         cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL);
41607e2034dSPavel Fedin 
41707e2034dSPavel Fedin         /* The CPU mp-affinity property is in MPIDR register format; squash
41807e2034dSPavel Fedin          * the affinity bytes into 32 bits as the GICR_TYPER has them.
41907e2034dSPavel Fedin          */
42092204403SAndrew Jones         cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) |
42192204403SAndrew Jones                      (cpu_affid & 0xFFFFFF);
42207e2034dSPavel Fedin         s->cpu[i].gicr_typer = (cpu_affid << 32) |
42307e2034dSPavel Fedin             (1 << 24) |
42404616415SPeter Maydell             (i << 8);
425ac30dec3SShashi Mallela 
426ac30dec3SShashi Mallela         if (s->lpi_enable) {
427ac30dec3SShashi Mallela             s->cpu[i].gicr_typer |= GICR_TYPER_PLPIS;
428e2d5e189SPeter Maydell             if (s->revision > 3) {
429e2d5e189SPeter Maydell                 s->cpu[i].gicr_typer |= GICR_TYPER_VLPIS;
430e2d5e189SPeter Maydell             }
431ac30dec3SShashi Mallela         }
43207e2034dSPavel Fedin     }
43304616415SPeter Maydell 
43404616415SPeter Maydell     /*
43504616415SPeter Maydell      * Now go through and set GICR_TYPER.Last for the final
43604616415SPeter Maydell      * redistributor in each region.
43704616415SPeter Maydell      */
43804616415SPeter Maydell     cpuidx = 0;
43904616415SPeter Maydell     for (i = 0; i < s->nb_redist_regions; i++) {
44004616415SPeter Maydell         cpuidx += s->redist_region_count[i];
44104616415SPeter Maydell         s->cpu[cpuidx - 1].gicr_typer |= GICR_TYPER_LAST;
44204616415SPeter Maydell     }
4437c087bd3SPeter Maydell 
4447c087bd3SPeter Maydell     s->itslist = g_ptr_array_new();
445ff8f06eeSShlomo Pongratz }
446ff8f06eeSShlomo Pongratz 
4471e575b66SEric Auger static void arm_gicv3_finalize(Object *obj)
4481e575b66SEric Auger {
4491e575b66SEric Auger     GICv3State *s = ARM_GICV3_COMMON(obj);
4501e575b66SEric Auger 
4511e575b66SEric Auger     g_free(s->redist_region_count);
4521e575b66SEric Auger }
4531e575b66SEric Auger 
454183cac31SPeter Maydell static void arm_gicv3_common_reset_hold(Object *obj)
455ff8f06eeSShlomo Pongratz {
456183cac31SPeter Maydell     GICv3State *s = ARM_GICV3_COMMON(obj);
45707e2034dSPavel Fedin     int i;
45807e2034dSPavel Fedin 
45907e2034dSPavel Fedin     for (i = 0; i < s->num_cpu; i++) {
46007e2034dSPavel Fedin         GICv3CPUState *cs = &s->cpu[i];
46107e2034dSPavel Fedin 
46207e2034dSPavel Fedin         cs->level = 0;
46307e2034dSPavel Fedin         cs->gicr_ctlr = 0;
4641611956bSPeter Maydell         if (s->lpi_enable) {
4651611956bSPeter Maydell             /* Our implementation supports clearing GICR_CTLR.EnableLPIs */
4661611956bSPeter Maydell             cs->gicr_ctlr |= GICR_CTLR_CES;
4671611956bSPeter Maydell         }
46807e2034dSPavel Fedin         cs->gicr_statusr[GICV3_S] = 0;
46907e2034dSPavel Fedin         cs->gicr_statusr[GICV3_NS] = 0;
47007e2034dSPavel Fedin         cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep;
47107e2034dSPavel Fedin         cs->gicr_propbaser = 0;
47207e2034dSPavel Fedin         cs->gicr_pendbaser = 0;
473641be697SPeter Maydell         cs->gicr_vpropbaser = 0;
474641be697SPeter Maydell         cs->gicr_vpendbaser = 0;
47507e2034dSPavel Fedin         /* If we're resetting a TZ-aware GIC as if secure firmware
47607e2034dSPavel Fedin          * had set it up ready to start a kernel in non-secure, we
47707e2034dSPavel Fedin          * need to set interrupts to group 1 so the kernel can use them.
47807e2034dSPavel Fedin          * Otherwise they reset to group 0 like the hardware.
47907e2034dSPavel Fedin          */
48007e2034dSPavel Fedin         if (s->irq_reset_nonsecure) {
48107e2034dSPavel Fedin             cs->gicr_igroupr0 = 0xffffffff;
48207e2034dSPavel Fedin         } else {
48307e2034dSPavel Fedin             cs->gicr_igroupr0 = 0;
48407e2034dSPavel Fedin         }
48507e2034dSPavel Fedin 
48607e2034dSPavel Fedin         cs->gicr_ienabler0 = 0;
48707e2034dSPavel Fedin         cs->gicr_ipendr0 = 0;
48807e2034dSPavel Fedin         cs->gicr_iactiver0 = 0;
48907e2034dSPavel Fedin         cs->edge_trigger = 0xffff;
49007e2034dSPavel Fedin         cs->gicr_igrpmodr0 = 0;
49107e2034dSPavel Fedin         cs->gicr_nsacr = 0;
49207e2034dSPavel Fedin         memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr));
49307e2034dSPavel Fedin 
494ce187c3cSPeter Maydell         cs->hppi.prio = 0xff;
49517fb5e36SShashi Mallela         cs->hpplpi.prio = 0xff;
496c3f21b06SPeter Maydell         cs->hppvlpi.prio = 0xff;
497ce187c3cSPeter Maydell 
49807e2034dSPavel Fedin         /* State in the CPU interface must *not* be reset here, because it
49907e2034dSPavel Fedin          * is part of the CPU's reset domain, not the GIC device's.
50007e2034dSPavel Fedin          */
50107e2034dSPavel Fedin     }
50207e2034dSPavel Fedin 
50307e2034dSPavel Fedin     /* For our implementation affinity routing is always enabled */
50407e2034dSPavel Fedin     if (s->security_extn) {
50507e2034dSPavel Fedin         s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS;
50607e2034dSPavel Fedin     } else {
50707e2034dSPavel Fedin         s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE;
50807e2034dSPavel Fedin     }
50907e2034dSPavel Fedin 
51007e2034dSPavel Fedin     s->gicd_statusr[GICV3_S] = 0;
51107e2034dSPavel Fedin     s->gicd_statusr[GICV3_NS] = 0;
51207e2034dSPavel Fedin 
51307e2034dSPavel Fedin     memset(s->group, 0, sizeof(s->group));
51407e2034dSPavel Fedin     memset(s->grpmod, 0, sizeof(s->grpmod));
51507e2034dSPavel Fedin     memset(s->enabled, 0, sizeof(s->enabled));
51607e2034dSPavel Fedin     memset(s->pending, 0, sizeof(s->pending));
51707e2034dSPavel Fedin     memset(s->active, 0, sizeof(s->active));
51807e2034dSPavel Fedin     memset(s->level, 0, sizeof(s->level));
51907e2034dSPavel Fedin     memset(s->edge_trigger, 0, sizeof(s->edge_trigger));
52007e2034dSPavel Fedin     memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority));
52107e2034dSPavel Fedin     memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter));
52207e2034dSPavel Fedin     memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr));
523ce187c3cSPeter Maydell     /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must
524ce187c3cSPeter Maydell      * write these to get sane behaviour and we need not populate the
525ce187c3cSPeter Maydell      * pointer cache here; however having the cache be different for
526ce187c3cSPeter Maydell      * "happened to be 0 from reset" and "guest wrote 0" would be
527ce187c3cSPeter Maydell      * too confusing.
528ce187c3cSPeter Maydell      */
529ce187c3cSPeter Maydell     gicv3_cache_all_target_cpustates(s);
53007e2034dSPavel Fedin 
53107e2034dSPavel Fedin     if (s->irq_reset_nonsecure) {
53207e2034dSPavel Fedin         /* If we're resetting a TZ-aware GIC as if secure firmware
53307e2034dSPavel Fedin          * had set it up ready to start a kernel in non-secure, we
53407e2034dSPavel Fedin          * need to set interrupts to group 1 so the kernel can use them.
53507e2034dSPavel Fedin          * Otherwise they reset to group 0 like the hardware.
53607e2034dSPavel Fedin          */
53707e2034dSPavel Fedin         for (i = GIC_INTERNAL; i < s->num_irq; i++) {
53807e2034dSPavel Fedin             gicv3_gicd_group_set(s, i);
53907e2034dSPavel Fedin         }
54007e2034dSPavel Fedin     }
541910e2048SShannon Zhao     s->gicd_no_migration_shift_bug = true;
54207e2034dSPavel Fedin }
54307e2034dSPavel Fedin 
54407e2034dSPavel Fedin static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
54507e2034dSPavel Fedin                                       bool secure_boot)
54607e2034dSPavel Fedin {
54707e2034dSPavel Fedin     GICv3State *s = ARM_GICV3_COMMON(obj);
54807e2034dSPavel Fedin 
54907e2034dSPavel Fedin     if (s->security_extn && !secure_boot) {
55007e2034dSPavel Fedin         /* We're directly booting a kernel into NonSecure. If this GIC
55107e2034dSPavel Fedin          * implements the security extensions then we must configure it
55207e2034dSPavel Fedin          * to have all the interrupts be NonSecure (this is a job that
55307e2034dSPavel Fedin          * is done by the Secure boot firmware in real hardware, and in
55407e2034dSPavel Fedin          * this mode QEMU is acting as a minimalist firmware-and-bootloader
55507e2034dSPavel Fedin          * equivalent).
55607e2034dSPavel Fedin          */
55707e2034dSPavel Fedin         s->irq_reset_nonsecure = true;
55807e2034dSPavel Fedin     }
559ff8f06eeSShlomo Pongratz }
560ff8f06eeSShlomo Pongratz 
561ff8f06eeSShlomo Pongratz static Property arm_gicv3_common_properties[] = {
562ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1),
563ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32),
564ff8f06eeSShlomo Pongratz     DEFINE_PROP_UINT32("revision", GICv3State, revision, 3),
565ac30dec3SShashi Mallela     DEFINE_PROP_BOOL("has-lpi", GICv3State, lpi_enable, 0),
566ff8f06eeSShlomo Pongratz     DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0),
56739f29e59SPeter Maydell     /*
56839f29e59SPeter Maydell      * Compatibility property: force 8 bits of physical priority, even
56939f29e59SPeter Maydell      * if the CPU being emulated should have fewer.
57039f29e59SPeter Maydell      */
57139f29e59SPeter Maydell     DEFINE_PROP_BOOL("force-8-bit-prio", GICv3State, force_8bit_prio, 0),
5721e575b66SEric Auger     DEFINE_PROP_ARRAY("redist-region-count", GICv3State, nb_redist_regions,
5731e575b66SEric Auger                       redist_region_count, qdev_prop_uint32, uint32_t),
574ac30dec3SShashi Mallela     DEFINE_PROP_LINK("sysmem", GICv3State, dma, TYPE_MEMORY_REGION,
575ac30dec3SShashi Mallela                      MemoryRegion *),
576ff8f06eeSShlomo Pongratz     DEFINE_PROP_END_OF_LIST(),
577ff8f06eeSShlomo Pongratz };
578ff8f06eeSShlomo Pongratz 
579ff8f06eeSShlomo Pongratz static void arm_gicv3_common_class_init(ObjectClass *klass, void *data)
580ff8f06eeSShlomo Pongratz {
581ff8f06eeSShlomo Pongratz     DeviceClass *dc = DEVICE_CLASS(klass);
582183cac31SPeter Maydell     ResettableClass *rc = RESETTABLE_CLASS(klass);
58307e2034dSPavel Fedin     ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
584ff8f06eeSShlomo Pongratz 
585183cac31SPeter Maydell     rc->phases.hold = arm_gicv3_common_reset_hold;
586ff8f06eeSShlomo Pongratz     dc->realize = arm_gicv3_common_realize;
5874f67d30bSMarc-André Lureau     device_class_set_props(dc, arm_gicv3_common_properties);
588ff8f06eeSShlomo Pongratz     dc->vmsd = &vmstate_gicv3;
58907e2034dSPavel Fedin     albifc->arm_linux_init = arm_gic_common_linux_init;
590ff8f06eeSShlomo Pongratz }
591ff8f06eeSShlomo Pongratz 
592ff8f06eeSShlomo Pongratz static const TypeInfo arm_gicv3_common_type = {
593ff8f06eeSShlomo Pongratz     .name = TYPE_ARM_GICV3_COMMON,
594ff8f06eeSShlomo Pongratz     .parent = TYPE_SYS_BUS_DEVICE,
595ff8f06eeSShlomo Pongratz     .instance_size = sizeof(GICv3State),
596ff8f06eeSShlomo Pongratz     .class_size = sizeof(ARMGICv3CommonClass),
597ff8f06eeSShlomo Pongratz     .class_init = arm_gicv3_common_class_init,
5981e575b66SEric Auger     .instance_finalize = arm_gicv3_finalize,
599ff8f06eeSShlomo Pongratz     .abstract = true,
60007e2034dSPavel Fedin     .interfaces = (InterfaceInfo []) {
60107e2034dSPavel Fedin         { TYPE_ARM_LINUX_BOOT_IF },
60207e2034dSPavel Fedin         { },
60307e2034dSPavel Fedin     },
604ff8f06eeSShlomo Pongratz };
605ff8f06eeSShlomo Pongratz 
606ff8f06eeSShlomo Pongratz static void register_types(void)
607ff8f06eeSShlomo Pongratz {
608ff8f06eeSShlomo Pongratz     type_register_static(&arm_gicv3_common_type);
609ff8f06eeSShlomo Pongratz }
610ff8f06eeSShlomo Pongratz 
611ff8f06eeSShlomo Pongratz type_init(register_types)
6120c40daf0SPhilippe Mathieu-Daudé 
6130c40daf0SPhilippe Mathieu-Daudé const char *gicv3_class_name(void)
6140c40daf0SPhilippe Mathieu-Daudé {
6150c40daf0SPhilippe Mathieu-Daudé     if (kvm_irqchip_in_kernel()) {
6160c40daf0SPhilippe Mathieu-Daudé         return "kvm-arm-gicv3";
6170c40daf0SPhilippe Mathieu-Daudé     } else {
6180c40daf0SPhilippe Mathieu-Daudé         if (kvm_enabled()) {
6190c40daf0SPhilippe Mathieu-Daudé             error_report("Userspace GICv3 is not supported with KVM");
6200c40daf0SPhilippe Mathieu-Daudé             exit(1);
6210c40daf0SPhilippe Mathieu-Daudé         }
6220c40daf0SPhilippe Mathieu-Daudé         return "arm-gicv3";
6230c40daf0SPhilippe Mathieu-Daudé     }
6240c40daf0SPhilippe Mathieu-Daudé }
625