xref: /qemu/hw/intc/arm_gicv3_kvm.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1a7bf3034SPavel Fedin /*
2a7bf3034SPavel Fedin  * ARM Generic Interrupt Controller using KVM in-kernel support
3a7bf3034SPavel Fedin  *
4a7bf3034SPavel Fedin  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5a7bf3034SPavel Fedin  * Written by Pavel Fedin
6a7bf3034SPavel Fedin  * Based on vGICv2 code by Peter Maydell
7a7bf3034SPavel Fedin  *
8a7bf3034SPavel Fedin  * This program is free software; you can redistribute it and/or modify
9a7bf3034SPavel Fedin  * it under the terms of the GNU General Public License as published by
10a7bf3034SPavel Fedin  * the Free Software Foundation, either version 2 of the License, or
11a7bf3034SPavel Fedin  * (at your option) any later version.
12a7bf3034SPavel Fedin  *
13a7bf3034SPavel Fedin  * This program is distributed in the hope that it will be useful,
14a7bf3034SPavel Fedin  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15a7bf3034SPavel Fedin  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16a7bf3034SPavel Fedin  * GNU General Public License for more details.
17a7bf3034SPavel Fedin  *
18a7bf3034SPavel Fedin  * You should have received a copy of the GNU General Public License along
19a7bf3034SPavel Fedin  * with this program; if not, see <http://www.gnu.org/licenses/>.
20a7bf3034SPavel Fedin  */
21a7bf3034SPavel Fedin 
228ef94f0bSPeter Maydell #include "qemu/osdep.h"
23da34e65cSMarkus Armbruster #include "qapi/error.h"
24a7bf3034SPavel Fedin #include "hw/intc/arm_gicv3_common.h"
25a7bf3034SPavel Fedin #include "hw/sysbus.h"
26367b9f52SVijaya Kumar K #include "qemu/error-report.h"
270b8fa32fSMarkus Armbruster #include "qemu/module.h"
28a7bf3034SPavel Fedin #include "sysemu/kvm.h"
2954d31236SMarkus Armbruster #include "sysemu/runstate.h"
30a7bf3034SPavel Fedin #include "kvm_arm.h"
31367b9f52SVijaya Kumar K #include "gicv3_internal.h"
32a7bf3034SPavel Fedin #include "vgic_common.h"
33795c40b8SJuan Quintela #include "migration/blocker.h"
34*db1015e9SEduardo Habkost #include "qom/object.h"
35a7bf3034SPavel Fedin 
36a7bf3034SPavel Fedin #ifdef DEBUG_GICV3_KVM
37a7bf3034SPavel Fedin #define DPRINTF(fmt, ...) \
38a7bf3034SPavel Fedin     do { fprintf(stderr, "kvm_gicv3: " fmt, ## __VA_ARGS__); } while (0)
39a7bf3034SPavel Fedin #else
40a7bf3034SPavel Fedin #define DPRINTF(fmt, ...) \
41a7bf3034SPavel Fedin     do { } while (0)
42a7bf3034SPavel Fedin #endif
43a7bf3034SPavel Fedin 
44a7bf3034SPavel Fedin #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
45*db1015e9SEduardo Habkost typedef struct KVMARMGICv3Class KVMARMGICv3Class;
46a7bf3034SPavel Fedin #define KVM_ARM_GICV3(obj) \
47a7bf3034SPavel Fedin      OBJECT_CHECK(GICv3State, (obj), TYPE_KVM_ARM_GICV3)
48a7bf3034SPavel Fedin #define KVM_ARM_GICV3_CLASS(klass) \
49a7bf3034SPavel Fedin      OBJECT_CLASS_CHECK(KVMARMGICv3Class, (klass), TYPE_KVM_ARM_GICV3)
50a7bf3034SPavel Fedin #define KVM_ARM_GICV3_GET_CLASS(obj) \
51a7bf3034SPavel Fedin      OBJECT_GET_CLASS(KVMARMGICv3Class, (obj), TYPE_KVM_ARM_GICV3)
52a7bf3034SPavel Fedin 
53367b9f52SVijaya Kumar K #define   KVM_DEV_ARM_VGIC_SYSREG(op0, op1, crn, crm, op2)         \
54367b9f52SVijaya Kumar K                              (ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
55367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
56367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
57367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
58367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
59367b9f52SVijaya Kumar K 
60367b9f52SVijaya Kumar K #define ICC_PMR_EL1     \
61367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 4, 6, 0)
62367b9f52SVijaya Kumar K #define ICC_BPR0_EL1    \
63367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 3)
64367b9f52SVijaya Kumar K #define ICC_AP0R_EL1(n) \
65367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 4 | n)
66367b9f52SVijaya Kumar K #define ICC_AP1R_EL1(n) \
67367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 9, n)
68367b9f52SVijaya Kumar K #define ICC_BPR1_EL1    \
69367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 3)
70367b9f52SVijaya Kumar K #define ICC_CTLR_EL1    \
71367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 4)
72367b9f52SVijaya Kumar K #define ICC_SRE_EL1 \
73367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 5)
74367b9f52SVijaya Kumar K #define ICC_IGRPEN0_EL1 \
75367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 6)
76367b9f52SVijaya Kumar K #define ICC_IGRPEN1_EL1 \
77367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 7)
78367b9f52SVijaya Kumar K 
79*db1015e9SEduardo Habkost struct KVMARMGICv3Class {
80a7bf3034SPavel Fedin     ARMGICv3CommonClass parent_class;
81a7bf3034SPavel Fedin     DeviceRealize parent_realize;
82a7bf3034SPavel Fedin     void (*parent_reset)(DeviceState *dev);
83*db1015e9SEduardo Habkost };
84a7bf3034SPavel Fedin 
85a7bf3034SPavel Fedin static void kvm_arm_gicv3_set_irq(void *opaque, int irq, int level)
86a7bf3034SPavel Fedin {
87a7bf3034SPavel Fedin     GICv3State *s = (GICv3State *)opaque;
88a7bf3034SPavel Fedin 
89a7bf3034SPavel Fedin     kvm_arm_gic_set_irq(s->num_irq, irq, level);
90a7bf3034SPavel Fedin }
91a7bf3034SPavel Fedin 
92367b9f52SVijaya Kumar K #define KVM_VGIC_ATTR(reg, typer) \
93367b9f52SVijaya Kumar K     ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (reg))
94367b9f52SVijaya Kumar K 
95367b9f52SVijaya Kumar K static inline void kvm_gicd_access(GICv3State *s, int offset,
96367b9f52SVijaya Kumar K                                    uint32_t *val, bool write)
97367b9f52SVijaya Kumar K {
98367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
99367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(offset, 0),
100556969e9SEric Auger                       val, write, &error_abort);
101367b9f52SVijaya Kumar K }
102367b9f52SVijaya Kumar K 
103367b9f52SVijaya Kumar K static inline void kvm_gicr_access(GICv3State *s, int offset, int cpu,
104367b9f52SVijaya Kumar K                                    uint32_t *val, bool write)
105367b9f52SVijaya Kumar K {
106367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
107367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(offset, s->cpu[cpu].gicr_typer),
108556969e9SEric Auger                       val, write, &error_abort);
109367b9f52SVijaya Kumar K }
110367b9f52SVijaya Kumar K 
111367b9f52SVijaya Kumar K static inline void kvm_gicc_access(GICv3State *s, uint64_t reg, int cpu,
112367b9f52SVijaya Kumar K                                    uint64_t *val, bool write)
113367b9f52SVijaya Kumar K {
114367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
115367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(reg, s->cpu[cpu].gicr_typer),
116556969e9SEric Auger                       val, write, &error_abort);
117367b9f52SVijaya Kumar K }
118367b9f52SVijaya Kumar K 
119367b9f52SVijaya Kumar K static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu,
120367b9f52SVijaya Kumar K                                              uint32_t *val, bool write)
121367b9f52SVijaya Kumar K {
122367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
123367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(irq, s->cpu[cpu].gicr_typer) |
124367b9f52SVijaya Kumar K                       (VGIC_LEVEL_INFO_LINE_LEVEL <<
125367b9f52SVijaya Kumar K                        KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT),
126556969e9SEric Auger                       val, write, &error_abort);
127367b9f52SVijaya Kumar K }
128367b9f52SVijaya Kumar K 
129367b9f52SVijaya Kumar K /* Loop through each distributor IRQ related register; since bits
130367b9f52SVijaya Kumar K  * corresponding to SPIs and PPIs are RAZ/WI when affinity routing
131367b9f52SVijaya Kumar K  * is enabled, we skip those.
132367b9f52SVijaya Kumar K  */
133367b9f52SVijaya Kumar K #define for_each_dist_irq_reg(_irq, _max, _field_width) \
134367b9f52SVijaya Kumar K     for (_irq = GIC_INTERNAL; _irq < _max; _irq += (32 / _field_width))
135367b9f52SVijaya Kumar K 
136367b9f52SVijaya Kumar K static void kvm_dist_get_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
137367b9f52SVijaya Kumar K {
138367b9f52SVijaya Kumar K     uint32_t reg, *field;
139367b9f52SVijaya Kumar K     int irq;
140367b9f52SVijaya Kumar K 
1411dcf3675SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the first 8
1421dcf3675SShannon Zhao      * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
1431dcf3675SShannon Zhao      * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
1441dcf3675SShannon Zhao      * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
1451dcf3675SShannon Zhao      * offset.
1461dcf3675SShannon Zhao      */
1471dcf3675SShannon Zhao     field = (uint32_t *)(bmp + GIC_INTERNAL);
1481dcf3675SShannon Zhao     offset += (GIC_INTERNAL * 8) / 8;
149367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 8) {
150367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
151367b9f52SVijaya Kumar K         *field = reg;
152367b9f52SVijaya Kumar K         offset += 4;
153367b9f52SVijaya Kumar K         field++;
154367b9f52SVijaya Kumar K     }
155367b9f52SVijaya Kumar K }
156367b9f52SVijaya Kumar K 
157367b9f52SVijaya Kumar K static void kvm_dist_put_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
158367b9f52SVijaya Kumar K {
159367b9f52SVijaya Kumar K     uint32_t reg, *field;
160367b9f52SVijaya Kumar K     int irq;
161367b9f52SVijaya Kumar K 
1621dcf3675SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the first 8
1631dcf3675SShannon Zhao      * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
1641dcf3675SShannon Zhao      * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
1651dcf3675SShannon Zhao      * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
1661dcf3675SShannon Zhao      * offset.
1671dcf3675SShannon Zhao      */
1681dcf3675SShannon Zhao     field = (uint32_t *)(bmp + GIC_INTERNAL);
1691dcf3675SShannon Zhao     offset += (GIC_INTERNAL * 8) / 8;
170367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 8) {
171367b9f52SVijaya Kumar K         reg = *field;
172367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
173367b9f52SVijaya Kumar K         offset += 4;
174367b9f52SVijaya Kumar K         field++;
175367b9f52SVijaya Kumar K     }
176367b9f52SVijaya Kumar K }
177367b9f52SVijaya Kumar K 
178367b9f52SVijaya Kumar K static void kvm_dist_get_edge_trigger(GICv3State *s, uint32_t offset,
179367b9f52SVijaya Kumar K                                       uint32_t *bmp)
180367b9f52SVijaya Kumar K {
181367b9f52SVijaya Kumar K     uint32_t reg;
182367b9f52SVijaya Kumar K     int irq;
183367b9f52SVijaya Kumar K 
184910e2048SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the first 2
185910e2048SShannon Zhao      * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
186910e2048SShannon Zhao      * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
187910e2048SShannon Zhao      * them. So it should increase the offset to skip GIC_INTERNAL irqs.
188910e2048SShannon Zhao      * This matches the for_each_dist_irq_reg() macro which also skips the
189910e2048SShannon Zhao      * first GIC_INTERNAL irqs.
190910e2048SShannon Zhao      */
191910e2048SShannon Zhao     offset += (GIC_INTERNAL * 2) / 8;
192367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 2) {
193367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
194367b9f52SVijaya Kumar K         reg = half_unshuffle32(reg >> 1);
195367b9f52SVijaya Kumar K         if (irq % 32 != 0) {
196367b9f52SVijaya Kumar K             reg = (reg << 16);
197367b9f52SVijaya Kumar K         }
198367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) |=  reg;
199367b9f52SVijaya Kumar K         offset += 4;
200367b9f52SVijaya Kumar K     }
201367b9f52SVijaya Kumar K }
202367b9f52SVijaya Kumar K 
203367b9f52SVijaya Kumar K static void kvm_dist_put_edge_trigger(GICv3State *s, uint32_t offset,
204367b9f52SVijaya Kumar K                                       uint32_t *bmp)
205367b9f52SVijaya Kumar K {
206367b9f52SVijaya Kumar K     uint32_t reg;
207367b9f52SVijaya Kumar K     int irq;
208367b9f52SVijaya Kumar K 
209910e2048SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the first 2
210910e2048SShannon Zhao      * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
211910e2048SShannon Zhao      * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
212910e2048SShannon Zhao      * them. So it should increase the offset to skip GIC_INTERNAL irqs.
213910e2048SShannon Zhao      * This matches the for_each_dist_irq_reg() macro which also skips the
214910e2048SShannon Zhao      * first GIC_INTERNAL irqs.
215910e2048SShannon Zhao      */
216910e2048SShannon Zhao     offset += (GIC_INTERNAL * 2) / 8;
217367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 2) {
218367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
219367b9f52SVijaya Kumar K         if (irq % 32 != 0) {
220367b9f52SVijaya Kumar K             reg = (reg & 0xffff0000) >> 16;
221367b9f52SVijaya Kumar K         } else {
222367b9f52SVijaya Kumar K             reg = reg & 0xffff;
223367b9f52SVijaya Kumar K         }
224367b9f52SVijaya Kumar K         reg = half_shuffle32(reg) << 1;
225367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
226367b9f52SVijaya Kumar K         offset += 4;
227367b9f52SVijaya Kumar K     }
228367b9f52SVijaya Kumar K }
229367b9f52SVijaya Kumar K 
230367b9f52SVijaya Kumar K static void kvm_gic_get_line_level_bmp(GICv3State *s, uint32_t *bmp)
231367b9f52SVijaya Kumar K {
232367b9f52SVijaya Kumar K     uint32_t reg;
233367b9f52SVijaya Kumar K     int irq;
234367b9f52SVijaya Kumar K 
235367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
236367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, irq, 0, &reg, false);
237367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) = reg;
238367b9f52SVijaya Kumar K     }
239367b9f52SVijaya Kumar K }
240367b9f52SVijaya Kumar K 
241367b9f52SVijaya Kumar K static void kvm_gic_put_line_level_bmp(GICv3State *s, uint32_t *bmp)
242367b9f52SVijaya Kumar K {
243367b9f52SVijaya Kumar K     uint32_t reg;
244367b9f52SVijaya Kumar K     int irq;
245367b9f52SVijaya Kumar K 
246367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
247367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
248367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, irq, 0, &reg, true);
249367b9f52SVijaya Kumar K     }
250367b9f52SVijaya Kumar K }
251367b9f52SVijaya Kumar K 
252367b9f52SVijaya Kumar K /* Read a bitmap register group from the kernel VGIC. */
253367b9f52SVijaya Kumar K static void kvm_dist_getbmp(GICv3State *s, uint32_t offset, uint32_t *bmp)
254367b9f52SVijaya Kumar K {
255367b9f52SVijaya Kumar K     uint32_t reg;
256367b9f52SVijaya Kumar K     int irq;
257367b9f52SVijaya Kumar K 
258910e2048SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the
259910e2048SShannon Zhao      * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
260910e2048SShannon Zhao      * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
261910e2048SShannon Zhao      * functionality is replaced by the GICR registers. It doesn't need to sync
262910e2048SShannon Zhao      * them. So it should increase the offset to skip GIC_INTERNAL irqs.
263910e2048SShannon Zhao      * This matches the for_each_dist_irq_reg() macro which also skips the
264910e2048SShannon Zhao      * first GIC_INTERNAL irqs.
265910e2048SShannon Zhao      */
266910e2048SShannon Zhao     offset += (GIC_INTERNAL * 1) / 8;
267367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
268367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
269367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) = reg;
270367b9f52SVijaya Kumar K         offset += 4;
271367b9f52SVijaya Kumar K     }
272367b9f52SVijaya Kumar K }
273367b9f52SVijaya Kumar K 
274367b9f52SVijaya Kumar K static void kvm_dist_putbmp(GICv3State *s, uint32_t offset,
275367b9f52SVijaya Kumar K                             uint32_t clroffset, uint32_t *bmp)
276367b9f52SVijaya Kumar K {
277367b9f52SVijaya Kumar K     uint32_t reg;
278367b9f52SVijaya Kumar K     int irq;
279367b9f52SVijaya Kumar K 
280910e2048SShannon Zhao     /* For the KVM GICv3, affinity routing is always enabled, and the
281910e2048SShannon Zhao      * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
282910e2048SShannon Zhao      * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
283910e2048SShannon Zhao      * functionality is replaced by the GICR registers. It doesn't need to sync
284910e2048SShannon Zhao      * them. So it should increase the offset and clroffset to skip GIC_INTERNAL
285910e2048SShannon Zhao      * irqs. This matches the for_each_dist_irq_reg() macro which also skips the
286910e2048SShannon Zhao      * first GIC_INTERNAL irqs.
287910e2048SShannon Zhao      */
288910e2048SShannon Zhao     offset += (GIC_INTERNAL * 1) / 8;
289910e2048SShannon Zhao     if (clroffset != 0) {
290910e2048SShannon Zhao         clroffset += (GIC_INTERNAL * 1) / 8;
291910e2048SShannon Zhao     }
292910e2048SShannon Zhao 
293367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
294367b9f52SVijaya Kumar K         /* If this bitmap is a set/clear register pair, first write to the
295367b9f52SVijaya Kumar K          * clear-reg to clear all bits before using the set-reg to write
296367b9f52SVijaya Kumar K          * the 1 bits.
297367b9f52SVijaya Kumar K          */
298367b9f52SVijaya Kumar K         if (clroffset != 0) {
299367b9f52SVijaya Kumar K             reg = 0;
300367b9f52SVijaya Kumar K             kvm_gicd_access(s, clroffset, &reg, true);
30134ffacaeSShannon Zhao             clroffset += 4;
302367b9f52SVijaya Kumar K         }
303367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
304367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
305367b9f52SVijaya Kumar K         offset += 4;
306367b9f52SVijaya Kumar K     }
307367b9f52SVijaya Kumar K }
308367b9f52SVijaya Kumar K 
309367b9f52SVijaya Kumar K static void kvm_arm_gicv3_check(GICv3State *s)
310367b9f52SVijaya Kumar K {
311367b9f52SVijaya Kumar K     uint32_t reg;
312367b9f52SVijaya Kumar K     uint32_t num_irq;
313367b9f52SVijaya Kumar K 
314367b9f52SVijaya Kumar K     /* Sanity checking s->num_irq */
315367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_TYPER, &reg, false);
316367b9f52SVijaya Kumar K     num_irq = ((reg & 0x1f) + 1) * 32;
317367b9f52SVijaya Kumar K 
318367b9f52SVijaya Kumar K     if (num_irq < s->num_irq) {
319367b9f52SVijaya Kumar K         error_report("Model requests %u IRQs, but kernel supports max %u",
320367b9f52SVijaya Kumar K                      s->num_irq, num_irq);
321367b9f52SVijaya Kumar K         abort();
322367b9f52SVijaya Kumar K     }
323367b9f52SVijaya Kumar K }
324367b9f52SVijaya Kumar K 
325a7bf3034SPavel Fedin static void kvm_arm_gicv3_put(GICv3State *s)
326a7bf3034SPavel Fedin {
327367b9f52SVijaya Kumar K     uint32_t regl, regh, reg;
328367b9f52SVijaya Kumar K     uint64_t reg64, redist_typer;
329367b9f52SVijaya Kumar K     int ncpu, i;
330367b9f52SVijaya Kumar K 
331367b9f52SVijaya Kumar K     kvm_arm_gicv3_check(s);
332367b9f52SVijaya Kumar K 
333367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
334367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
335367b9f52SVijaya Kumar K     redist_typer = ((uint64_t)regh << 32) | regl;
336367b9f52SVijaya Kumar K 
337367b9f52SVijaya Kumar K     reg = s->gicd_ctlr;
338367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_CTLR, &reg, true);
339367b9f52SVijaya Kumar K 
340367b9f52SVijaya Kumar K     if (redist_typer & GICR_TYPER_PLPIS) {
341618bacabSZenghui Yu         /*
342618bacabSZenghui Yu          * Restore base addresses before LPIs are potentially enabled by
343618bacabSZenghui Yu          * GICR_CTLR write
344618bacabSZenghui Yu          */
345367b9f52SVijaya Kumar K         for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
346367b9f52SVijaya Kumar K             GICv3CPUState *c = &s->cpu[ncpu];
347367b9f52SVijaya Kumar K 
348367b9f52SVijaya Kumar K             reg64 = c->gicr_propbaser;
349367b9f52SVijaya Kumar K             regl = (uint32_t)reg64;
350367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, true);
351367b9f52SVijaya Kumar K             regh = (uint32_t)(reg64 >> 32);
352367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, true);
353367b9f52SVijaya Kumar K 
354367b9f52SVijaya Kumar K             reg64 = c->gicr_pendbaser;
355367b9f52SVijaya Kumar K             regl = (uint32_t)reg64;
356367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, true);
357367b9f52SVijaya Kumar K             regh = (uint32_t)(reg64 >> 32);
358367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, true);
359367b9f52SVijaya Kumar K         }
360367b9f52SVijaya Kumar K     }
361367b9f52SVijaya Kumar K 
362367b9f52SVijaya Kumar K     /* Redistributor state (one per CPU) */
363367b9f52SVijaya Kumar K 
364367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
365367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
366367b9f52SVijaya Kumar K 
367367b9f52SVijaya Kumar K         reg = c->gicr_ctlr;
368367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, true);
369367b9f52SVijaya Kumar K 
370367b9f52SVijaya Kumar K         reg = c->gicr_statusr[GICV3_NS];
371367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, true);
372367b9f52SVijaya Kumar K 
373367b9f52SVijaya Kumar K         reg = c->gicr_waker;
374367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, true);
375367b9f52SVijaya Kumar K 
376367b9f52SVijaya Kumar K         reg = c->gicr_igroupr0;
377367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, true);
378367b9f52SVijaya Kumar K 
379367b9f52SVijaya Kumar K         reg = ~0;
380367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICENABLER0, ncpu, &reg, true);
381367b9f52SVijaya Kumar K         reg = c->gicr_ienabler0;
382367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, true);
383367b9f52SVijaya Kumar K 
384367b9f52SVijaya Kumar K         /* Restore config before pending so we treat level/edge correctly */
385367b9f52SVijaya Kumar K         reg = half_shuffle32(c->edge_trigger >> 16) << 1;
386367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, true);
387367b9f52SVijaya Kumar K 
388367b9f52SVijaya Kumar K         reg = c->level;
389367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, 0, ncpu, &reg, true);
390367b9f52SVijaya Kumar K 
391367b9f52SVijaya Kumar K         reg = ~0;
392367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICPENDR0, ncpu, &reg, true);
393367b9f52SVijaya Kumar K         reg = c->gicr_ipendr0;
394367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, true);
395367b9f52SVijaya Kumar K 
396367b9f52SVijaya Kumar K         reg = ~0;
397367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICACTIVER0, ncpu, &reg, true);
398367b9f52SVijaya Kumar K         reg = c->gicr_iactiver0;
399367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, true);
400367b9f52SVijaya Kumar K 
401367b9f52SVijaya Kumar K         for (i = 0; i < GIC_INTERNAL; i += 4) {
402367b9f52SVijaya Kumar K             reg = c->gicr_ipriorityr[i] |
403367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 1] << 8) |
404367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 2] << 16) |
405367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 3] << 24);
406367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, true);
407367b9f52SVijaya Kumar K         }
408367b9f52SVijaya Kumar K     }
409367b9f52SVijaya Kumar K 
410367b9f52SVijaya Kumar K     /* Distributor state (shared between all CPUs */
411367b9f52SVijaya Kumar K     reg = s->gicd_statusr[GICV3_NS];
412367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_STATUSR, &reg, true);
413367b9f52SVijaya Kumar K 
414367b9f52SVijaya Kumar K     /* s->enable bitmap -> GICD_ISENABLERn */
415367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISENABLER, GICD_ICENABLER, s->enabled);
416367b9f52SVijaya Kumar K 
417367b9f52SVijaya Kumar K     /* s->group bitmap -> GICD_IGROUPRn */
418367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_IGROUPR, 0, s->group);
419367b9f52SVijaya Kumar K 
420367b9f52SVijaya Kumar K     /* Restore targets before pending to ensure the pending state is set on
421367b9f52SVijaya Kumar K      * the appropriate CPU interfaces in the kernel
422367b9f52SVijaya Kumar K      */
423367b9f52SVijaya Kumar K 
424367b9f52SVijaya Kumar K     /* s->gicd_irouter[irq] -> GICD_IROUTERn
425367b9f52SVijaya Kumar K      * We can't use kvm_dist_put() here because the registers are 64-bit
426367b9f52SVijaya Kumar K      */
427367b9f52SVijaya Kumar K     for (i = GIC_INTERNAL; i < s->num_irq; i++) {
428367b9f52SVijaya Kumar K         uint32_t offset;
429367b9f52SVijaya Kumar K 
430367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i);
431367b9f52SVijaya Kumar K         reg = (uint32_t)s->gicd_irouter[i];
432367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
433367b9f52SVijaya Kumar K 
434367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
435367b9f52SVijaya Kumar K         reg = (uint32_t)(s->gicd_irouter[i] >> 32);
436367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
437367b9f52SVijaya Kumar K     }
438367b9f52SVijaya Kumar K 
439367b9f52SVijaya Kumar K     /* s->trigger bitmap -> GICD_ICFGRn
440367b9f52SVijaya Kumar K      * (restore configuration registers before pending IRQs so we treat
441367b9f52SVijaya Kumar K      * level/edge correctly)
442367b9f52SVijaya Kumar K      */
443367b9f52SVijaya Kumar K     kvm_dist_put_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
444367b9f52SVijaya Kumar K 
445367b9f52SVijaya Kumar K     /* s->level bitmap ->  line_level */
446367b9f52SVijaya Kumar K     kvm_gic_put_line_level_bmp(s, s->level);
447367b9f52SVijaya Kumar K 
448367b9f52SVijaya Kumar K     /* s->pending bitmap -> GICD_ISPENDRn */
449367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISPENDR, GICD_ICPENDR, s->pending);
450367b9f52SVijaya Kumar K 
451367b9f52SVijaya Kumar K     /* s->active bitmap -> GICD_ISACTIVERn */
452367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISACTIVER, GICD_ICACTIVER, s->active);
453367b9f52SVijaya Kumar K 
454367b9f52SVijaya Kumar K     /* s->gicd_ipriority[] -> GICD_IPRIORITYRn */
455367b9f52SVijaya Kumar K     kvm_dist_put_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
456367b9f52SVijaya Kumar K 
457367b9f52SVijaya Kumar K     /* CPU Interface state (one per CPU) */
458367b9f52SVijaya Kumar K 
459367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
460367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
461367b9f52SVijaya Kumar K         int num_pri_bits;
462367b9f52SVijaya Kumar K 
463367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, true);
464367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
465367b9f52SVijaya Kumar K                         &c->icc_ctlr_el1[GICV3_NS], true);
466367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
467367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G0], true);
468367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
469367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G1NS], true);
470367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, true);
471367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], true);
472367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], true);
473367b9f52SVijaya Kumar K 
474367b9f52SVijaya Kumar K         num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
475367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_MASK) >>
476367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
477367b9f52SVijaya Kumar K 
478367b9f52SVijaya Kumar K         switch (num_pri_bits) {
479367b9f52SVijaya Kumar K         case 7:
480367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][3];
481367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, true);
482367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][2];
483367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, true);
484367b9f52SVijaya Kumar K         case 6:
485367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][1];
486367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
487367b9f52SVijaya Kumar K         default:
488367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][0];
489367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
490367b9f52SVijaya Kumar K         }
491367b9f52SVijaya Kumar K 
492367b9f52SVijaya Kumar K         switch (num_pri_bits) {
493367b9f52SVijaya Kumar K         case 7:
494367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][3];
495367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
496367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][2];
497367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
498367b9f52SVijaya Kumar K         case 6:
499367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][1];
500367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
501367b9f52SVijaya Kumar K         default:
502367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][0];
503367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, true);
504367b9f52SVijaya Kumar K         }
505367b9f52SVijaya Kumar K     }
506a7bf3034SPavel Fedin }
507a7bf3034SPavel Fedin 
508a7bf3034SPavel Fedin static void kvm_arm_gicv3_get(GICv3State *s)
509a7bf3034SPavel Fedin {
510367b9f52SVijaya Kumar K     uint32_t regl, regh, reg;
511367b9f52SVijaya Kumar K     uint64_t reg64, redist_typer;
512367b9f52SVijaya Kumar K     int ncpu, i;
513367b9f52SVijaya Kumar K 
514367b9f52SVijaya Kumar K     kvm_arm_gicv3_check(s);
515367b9f52SVijaya Kumar K 
516367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
517367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
518367b9f52SVijaya Kumar K     redist_typer = ((uint64_t)regh << 32) | regl;
519367b9f52SVijaya Kumar K 
520367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_CTLR, &reg, false);
521367b9f52SVijaya Kumar K     s->gicd_ctlr = reg;
522367b9f52SVijaya Kumar K 
523367b9f52SVijaya Kumar K     /* Redistributor state (one per CPU) */
524367b9f52SVijaya Kumar K 
525367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
526367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
527367b9f52SVijaya Kumar K 
528367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, false);
529367b9f52SVijaya Kumar K         c->gicr_ctlr = reg;
530367b9f52SVijaya Kumar K 
531367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, false);
532367b9f52SVijaya Kumar K         c->gicr_statusr[GICV3_NS] = reg;
533367b9f52SVijaya Kumar K 
534367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, false);
535367b9f52SVijaya Kumar K         c->gicr_waker = reg;
536367b9f52SVijaya Kumar K 
537367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, false);
538367b9f52SVijaya Kumar K         c->gicr_igroupr0 = reg;
539367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, false);
540367b9f52SVijaya Kumar K         c->gicr_ienabler0 = reg;
541367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, false);
542367b9f52SVijaya Kumar K         c->edge_trigger = half_unshuffle32(reg >> 1) << 16;
543367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, 0, ncpu, &reg, false);
544367b9f52SVijaya Kumar K         c->level = reg;
545367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, false);
546367b9f52SVijaya Kumar K         c->gicr_ipendr0 = reg;
547367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, false);
548367b9f52SVijaya Kumar K         c->gicr_iactiver0 = reg;
549367b9f52SVijaya Kumar K 
550367b9f52SVijaya Kumar K         for (i = 0; i < GIC_INTERNAL; i += 4) {
551367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, false);
552367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i] = extract32(reg, 0, 8);
553367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 1] = extract32(reg, 8, 8);
554367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 2] = extract32(reg, 16, 8);
555367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 3] = extract32(reg, 24, 8);
556367b9f52SVijaya Kumar K         }
557367b9f52SVijaya Kumar K     }
558367b9f52SVijaya Kumar K 
559367b9f52SVijaya Kumar K     if (redist_typer & GICR_TYPER_PLPIS) {
560367b9f52SVijaya Kumar K         for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
561367b9f52SVijaya Kumar K             GICv3CPUState *c = &s->cpu[ncpu];
562367b9f52SVijaya Kumar K 
563367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, false);
564367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, false);
565367b9f52SVijaya Kumar K             c->gicr_propbaser = ((uint64_t)regh << 32) | regl;
566367b9f52SVijaya Kumar K 
567367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, false);
568367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, false);
569367b9f52SVijaya Kumar K             c->gicr_pendbaser = ((uint64_t)regh << 32) | regl;
570367b9f52SVijaya Kumar K         }
571367b9f52SVijaya Kumar K     }
572367b9f52SVijaya Kumar K 
573367b9f52SVijaya Kumar K     /* Distributor state (shared between all CPUs */
574367b9f52SVijaya Kumar K 
575367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_STATUSR, &reg, false);
576367b9f52SVijaya Kumar K     s->gicd_statusr[GICV3_NS] = reg;
577367b9f52SVijaya Kumar K 
578367b9f52SVijaya Kumar K     /* GICD_IGROUPRn -> s->group bitmap */
579367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_IGROUPR, s->group);
580367b9f52SVijaya Kumar K 
581367b9f52SVijaya Kumar K     /* GICD_ISENABLERn -> s->enabled bitmap */
582367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISENABLER, s->enabled);
583367b9f52SVijaya Kumar K 
584367b9f52SVijaya Kumar K     /* Line level of irq */
585367b9f52SVijaya Kumar K     kvm_gic_get_line_level_bmp(s, s->level);
586367b9f52SVijaya Kumar K     /* GICD_ISPENDRn -> s->pending bitmap */
587367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISPENDR, s->pending);
588367b9f52SVijaya Kumar K 
589367b9f52SVijaya Kumar K     /* GICD_ISACTIVERn -> s->active bitmap */
590367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISACTIVER, s->active);
591367b9f52SVijaya Kumar K 
592367b9f52SVijaya Kumar K     /* GICD_ICFGRn -> s->trigger bitmap */
593367b9f52SVijaya Kumar K     kvm_dist_get_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
594367b9f52SVijaya Kumar K 
595367b9f52SVijaya Kumar K     /* GICD_IPRIORITYRn -> s->gicd_ipriority[] */
596367b9f52SVijaya Kumar K     kvm_dist_get_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
597367b9f52SVijaya Kumar K 
598367b9f52SVijaya Kumar K     /* GICD_IROUTERn -> s->gicd_irouter[irq] */
599367b9f52SVijaya Kumar K     for (i = GIC_INTERNAL; i < s->num_irq; i++) {
600367b9f52SVijaya Kumar K         uint32_t offset;
601367b9f52SVijaya Kumar K 
602367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i);
603367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &regl, false);
604367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
605367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &regh, false);
606367b9f52SVijaya Kumar K         s->gicd_irouter[i] = ((uint64_t)regh << 32) | regl;
607367b9f52SVijaya Kumar K     }
608367b9f52SVijaya Kumar K 
609367b9f52SVijaya Kumar K     /*****************************************************************
610367b9f52SVijaya Kumar K      * CPU Interface(s) State
611367b9f52SVijaya Kumar K      */
612367b9f52SVijaya Kumar K 
613367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
614367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
615367b9f52SVijaya Kumar K         int num_pri_bits;
616367b9f52SVijaya Kumar K 
617367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, false);
618367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
619367b9f52SVijaya Kumar K                         &c->icc_ctlr_el1[GICV3_NS], false);
620367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
621367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G0], false);
622367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
623367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G1NS], false);
624367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, false);
625367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], false);
626367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], false);
627367b9f52SVijaya Kumar K         num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
628367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_MASK) >>
629367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
630367b9f52SVijaya Kumar K 
631367b9f52SVijaya Kumar K         switch (num_pri_bits) {
632367b9f52SVijaya Kumar K         case 7:
633367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, false);
634367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][3] = reg64;
635367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, false);
636367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][2] = reg64;
637367b9f52SVijaya Kumar K         case 6:
638367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
639367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][1] = reg64;
640367b9f52SVijaya Kumar K         default:
641367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
642367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][0] = reg64;
643367b9f52SVijaya Kumar K         }
644367b9f52SVijaya Kumar K 
645367b9f52SVijaya Kumar K         switch (num_pri_bits) {
646367b9f52SVijaya Kumar K         case 7:
647367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
648367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][3] = reg64;
649367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
650367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][2] = reg64;
651367b9f52SVijaya Kumar K         case 6:
652367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
653367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][1] = reg64;
654367b9f52SVijaya Kumar K         default:
655367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
656367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][0] = reg64;
657367b9f52SVijaya Kumar K         }
658367b9f52SVijaya Kumar K     }
659a7bf3034SPavel Fedin }
660a7bf3034SPavel Fedin 
66107a5628cSVijaya Kumar K static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
66207a5628cSVijaya Kumar K {
66307a5628cSVijaya Kumar K     GICv3State *s;
66407a5628cSVijaya Kumar K     GICv3CPUState *c;
66507a5628cSVijaya Kumar K 
66607a5628cSVijaya Kumar K     c = (GICv3CPUState *)env->gicv3state;
66707a5628cSVijaya Kumar K     s = c->gic;
66807a5628cSVijaya Kumar K 
66907a5628cSVijaya Kumar K     c->icc_pmr_el1 = 0;
67007a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
67107a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
67207a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
67307a5628cSVijaya Kumar K 
67407a5628cSVijaya Kumar K     c->icc_sre_el1 = 0x7;
67507a5628cSVijaya Kumar K     memset(c->icc_apr, 0, sizeof(c->icc_apr));
67607a5628cSVijaya Kumar K     memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
677e7d54416SEric Auger 
678e7d54416SEric Auger     if (s->migration_blocker) {
679e7d54416SEric Auger         return;
680e7d54416SEric Auger     }
681e7d54416SEric Auger 
682e7d54416SEric Auger     /* Initialize to actual HW supported configuration */
683e7d54416SEric Auger     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
6841e11a139SKeqian Zhu                       KVM_VGIC_ATTR(ICC_CTLR_EL1, c->gicr_typer),
685556969e9SEric Auger                       &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
686e7d54416SEric Auger 
687e7d54416SEric Auger     c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
68807a5628cSVijaya Kumar K }
68907a5628cSVijaya Kumar K 
690a7bf3034SPavel Fedin static void kvm_arm_gicv3_reset(DeviceState *dev)
691a7bf3034SPavel Fedin {
692a7bf3034SPavel Fedin     GICv3State *s = ARM_GICV3_COMMON(dev);
693a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
694a7bf3034SPavel Fedin 
695a7bf3034SPavel Fedin     DPRINTF("Reset\n");
696a7bf3034SPavel Fedin 
697a7bf3034SPavel Fedin     kgc->parent_reset(dev);
698367b9f52SVijaya Kumar K 
699367b9f52SVijaya Kumar K     if (s->migration_blocker) {
700367b9f52SVijaya Kumar K         DPRINTF("Cannot put kernel gic state, no kernel interface\n");
701367b9f52SVijaya Kumar K         return;
702367b9f52SVijaya Kumar K     }
703367b9f52SVijaya Kumar K 
704a7bf3034SPavel Fedin     kvm_arm_gicv3_put(s);
705a7bf3034SPavel Fedin }
706a7bf3034SPavel Fedin 
70707a5628cSVijaya Kumar K /*
70807a5628cSVijaya Kumar K  * CPU interface registers of GIC needs to be reset on CPU reset.
70907a5628cSVijaya Kumar K  * For the calling arm_gicv3_icc_reset() on CPU reset, we register
71007a5628cSVijaya Kumar K  * below ARMCPRegInfo. As we reset the whole cpu interface under single
71107a5628cSVijaya Kumar K  * register reset, we define only one register of CPU interface instead
71207a5628cSVijaya Kumar K  * of defining all the registers.
71307a5628cSVijaya Kumar K  */
71407a5628cSVijaya Kumar K static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
71507a5628cSVijaya Kumar K     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
71607a5628cSVijaya Kumar K       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
71707a5628cSVijaya Kumar K       /*
71807a5628cSVijaya Kumar K        * If ARM_CP_NOP is used, resetfn is not called,
71907a5628cSVijaya Kumar K        * So ARM_CP_NO_RAW is appropriate type.
72007a5628cSVijaya Kumar K        */
72107a5628cSVijaya Kumar K       .type = ARM_CP_NO_RAW,
72207a5628cSVijaya Kumar K       .access = PL1_RW,
72307a5628cSVijaya Kumar K       .readfn = arm_cp_read_zero,
72407a5628cSVijaya Kumar K       .writefn = arm_cp_write_ignore,
72507a5628cSVijaya Kumar K       /*
72607a5628cSVijaya Kumar K        * We hang the whole cpu interface reset routine off here
72707a5628cSVijaya Kumar K        * rather than parcelling it out into one little function
72807a5628cSVijaya Kumar K        * per register
72907a5628cSVijaya Kumar K        */
73007a5628cSVijaya Kumar K       .resetfn = arm_gicv3_icc_reset,
73107a5628cSVijaya Kumar K     },
73207a5628cSVijaya Kumar K     REGINFO_SENTINEL
73307a5628cSVijaya Kumar K };
73407a5628cSVijaya Kumar K 
735d5aa0c22SEric Auger /**
736d5aa0c22SEric Auger  * vm_change_state_handler - VM change state callback aiming at flushing
737d5aa0c22SEric Auger  * RDIST pending tables into guest RAM
738d5aa0c22SEric Auger  *
739d5aa0c22SEric Auger  * The tables get flushed to guest RAM whenever the VM gets stopped.
740d5aa0c22SEric Auger  */
741d5aa0c22SEric Auger static void vm_change_state_handler(void *opaque, int running,
742d5aa0c22SEric Auger                                     RunState state)
743d5aa0c22SEric Auger {
744d5aa0c22SEric Auger     GICv3State *s = (GICv3State *)opaque;
745d5aa0c22SEric Auger     Error *err = NULL;
746d5aa0c22SEric Auger     int ret;
747d5aa0c22SEric Auger 
748d5aa0c22SEric Auger     if (running) {
749d5aa0c22SEric Auger         return;
750d5aa0c22SEric Auger     }
751d5aa0c22SEric Auger 
752d5aa0c22SEric Auger     ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
753d5aa0c22SEric Auger                            KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
754d5aa0c22SEric Auger                            NULL, true, &err);
755d5aa0c22SEric Auger     if (err) {
756d5aa0c22SEric Auger         error_report_err(err);
757d5aa0c22SEric Auger     }
758d5aa0c22SEric Auger     if (ret < 0 && ret != -EFAULT) {
759d5aa0c22SEric Auger         abort();
760d5aa0c22SEric Auger     }
761d5aa0c22SEric Auger }
762d5aa0c22SEric Auger 
763d5aa0c22SEric Auger 
764a7bf3034SPavel Fedin static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
765a7bf3034SPavel Fedin {
766a7bf3034SPavel Fedin     GICv3State *s = KVM_ARM_GICV3(dev);
767a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
76880d67333SEric Auger     bool multiple_redist_region_allowed;
769a7bf3034SPavel Fedin     Error *local_err = NULL;
770d19a4d4eSEric Auger     int i;
771a7bf3034SPavel Fedin 
772a7bf3034SPavel Fedin     DPRINTF("kvm_arm_gicv3_realize\n");
773a7bf3034SPavel Fedin 
774a7bf3034SPavel Fedin     kgc->parent_realize(dev, &local_err);
775a7bf3034SPavel Fedin     if (local_err) {
776a7bf3034SPavel Fedin         error_propagate(errp, local_err);
777a7bf3034SPavel Fedin         return;
778a7bf3034SPavel Fedin     }
779a7bf3034SPavel Fedin 
780a7bf3034SPavel Fedin     if (s->security_extn) {
781a7bf3034SPavel Fedin         error_setg(errp, "the in-kernel VGICv3 does not implement the "
782a7bf3034SPavel Fedin                    "security extensions");
783a7bf3034SPavel Fedin         return;
784a7bf3034SPavel Fedin     }
785a7bf3034SPavel Fedin 
7861e575b66SEric Auger     gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL, &local_err);
7871e575b66SEric Auger     if (local_err) {
7881e575b66SEric Auger         error_propagate(errp, local_err);
7891e575b66SEric Auger         return;
7901e575b66SEric Auger     }
791a7bf3034SPavel Fedin 
79207a5628cSVijaya Kumar K     for (i = 0; i < s->num_cpu; i++) {
79307a5628cSVijaya Kumar K         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
79407a5628cSVijaya Kumar K 
79507a5628cSVijaya Kumar K         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
79607a5628cSVijaya Kumar K     }
79707a5628cSVijaya Kumar K 
798a7bf3034SPavel Fedin     /* Try to create the device via the device control API */
799a7bf3034SPavel Fedin     s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
800a7bf3034SPavel Fedin     if (s->dev_fd < 0) {
801a7bf3034SPavel Fedin         error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
802a7bf3034SPavel Fedin         return;
803a7bf3034SPavel Fedin     }
804a7bf3034SPavel Fedin 
80580d67333SEric Auger     multiple_redist_region_allowed =
80680d67333SEric Auger         kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
80780d67333SEric Auger                               KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION);
80880d67333SEric Auger 
80980d67333SEric Auger     if (!multiple_redist_region_allowed && s->nb_redist_regions > 1) {
81080d67333SEric Auger         error_setg(errp, "Multiple VGICv3 redistributor regions are not "
81180d67333SEric Auger                    "supported by this host kernel");
81280d67333SEric Auger         error_append_hint(errp, "A maximum of %d VCPUs can be used",
81380d67333SEric Auger                           s->redist_region_count[0]);
81480d67333SEric Auger         return;
81580d67333SEric Auger     }
81680d67333SEric Auger 
817a7bf3034SPavel Fedin     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
818556969e9SEric Auger                       0, &s->num_irq, true, &error_abort);
819a7bf3034SPavel Fedin 
820a7bf3034SPavel Fedin     /* Tell the kernel to complete VGIC initialization now */
821a7bf3034SPavel Fedin     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
822556969e9SEric Auger                       KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
823a7bf3034SPavel Fedin 
824a7bf3034SPavel Fedin     kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
82519d1bd0bSEric Auger                             KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd, 0);
82680d67333SEric Auger 
82780d67333SEric Auger     if (!multiple_redist_region_allowed) {
8281e575b66SEric Auger         kvm_arm_register_device(&s->iomem_redist[0], -1,
8291e575b66SEric Auger                                 KVM_DEV_ARM_VGIC_GRP_ADDR,
83019d1bd0bSEric Auger                                 KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd, 0);
83180d67333SEric Auger     } else {
83280d67333SEric Auger         /* we register regions in reverse order as "devices" are inserted at
83380d67333SEric Auger          * the head of a QSLIST and the list is then popped from the head
83480d67333SEric Auger          * onwards by kvm_arm_machine_init_done()
83580d67333SEric Auger          */
83680d67333SEric Auger         for (i = s->nb_redist_regions - 1; i >= 0; i--) {
83780d67333SEric Auger             /* Address mask made of the rdist region index and count */
83880d67333SEric Auger             uint64_t addr_ormask =
83980d67333SEric Auger                         i | ((uint64_t)s->redist_region_count[i] << 52);
84080d67333SEric Auger 
84180d67333SEric Auger             kvm_arm_register_device(&s->iomem_redist[i], -1,
84280d67333SEric Auger                                     KVM_DEV_ARM_VGIC_GRP_ADDR,
84380d67333SEric Auger                                     KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION,
84480d67333SEric Auger                                     s->dev_fd, addr_ormask);
84580d67333SEric Auger         }
84680d67333SEric Auger     }
847757caeedSPavel Fedin 
848d19a4d4eSEric Auger     if (kvm_has_gsi_routing()) {
849d19a4d4eSEric Auger         /* set up irq routing */
850d19a4d4eSEric Auger         for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
851d19a4d4eSEric Auger             kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
852d19a4d4eSEric Auger         }
853d19a4d4eSEric Auger 
854d19a4d4eSEric Auger         kvm_gsi_routing_allowed = true;
855d19a4d4eSEric Auger 
856d19a4d4eSEric Auger         kvm_irqchip_commit_routes(kvm_state);
857d19a4d4eSEric Auger     }
858367b9f52SVijaya Kumar K 
859367b9f52SVijaya Kumar K     if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
860367b9f52SVijaya Kumar K                                GICD_CTLR)) {
861367b9f52SVijaya Kumar K         error_setg(&s->migration_blocker, "This operating system kernel does "
862367b9f52SVijaya Kumar K                                           "not support vGICv3 migration");
863386f6c07SMarkus Armbruster         if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
864367b9f52SVijaya Kumar K             error_free(s->migration_blocker);
865367b9f52SVijaya Kumar K             return;
866367b9f52SVijaya Kumar K         }
867367b9f52SVijaya Kumar K     }
868d5aa0c22SEric Auger     if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
869d5aa0c22SEric Auger                               KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
870d5aa0c22SEric Auger         qemu_add_vm_change_state_handler(vm_change_state_handler, s);
871d5aa0c22SEric Auger     }
872a7bf3034SPavel Fedin }
873a7bf3034SPavel Fedin 
874a7bf3034SPavel Fedin static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
875a7bf3034SPavel Fedin {
876a7bf3034SPavel Fedin     DeviceClass *dc = DEVICE_CLASS(klass);
877a7bf3034SPavel Fedin     ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
878a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
879a7bf3034SPavel Fedin 
880a7bf3034SPavel Fedin     agcc->pre_save = kvm_arm_gicv3_get;
881a7bf3034SPavel Fedin     agcc->post_load = kvm_arm_gicv3_put;
882bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
883bf853881SPhilippe Mathieu-Daudé                                     &kgc->parent_realize);
884bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_reset(dc, kvm_arm_gicv3_reset, &kgc->parent_reset);
885a7bf3034SPavel Fedin }
886a7bf3034SPavel Fedin 
887a7bf3034SPavel Fedin static const TypeInfo kvm_arm_gicv3_info = {
888a7bf3034SPavel Fedin     .name = TYPE_KVM_ARM_GICV3,
889a7bf3034SPavel Fedin     .parent = TYPE_ARM_GICV3_COMMON,
890a7bf3034SPavel Fedin     .instance_size = sizeof(GICv3State),
891a7bf3034SPavel Fedin     .class_init = kvm_arm_gicv3_class_init,
892a7bf3034SPavel Fedin     .class_size = sizeof(KVMARMGICv3Class),
893a7bf3034SPavel Fedin };
894a7bf3034SPavel Fedin 
895a7bf3034SPavel Fedin static void kvm_arm_gicv3_register_types(void)
896a7bf3034SPavel Fedin {
897a7bf3034SPavel Fedin     type_register_static(&kvm_arm_gicv3_info);
898a7bf3034SPavel Fedin }
899a7bf3034SPavel Fedin 
900a7bf3034SPavel Fedin type_init(kvm_arm_gicv3_register_types)
901