xref: /qemu/hw/intc/arm_gicv3_kvm.c (revision bf853881690db8bbd1de39e4be580310a9cb0ebc)
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"
27a7bf3034SPavel Fedin #include "sysemu/kvm.h"
28d5aa0c22SEric Auger #include "sysemu/sysemu.h"
29a7bf3034SPavel Fedin #include "kvm_arm.h"
30367b9f52SVijaya Kumar K #include "gicv3_internal.h"
31a7bf3034SPavel Fedin #include "vgic_common.h"
32795c40b8SJuan Quintela #include "migration/blocker.h"
33a7bf3034SPavel Fedin 
34a7bf3034SPavel Fedin #ifdef DEBUG_GICV3_KVM
35a7bf3034SPavel Fedin #define DPRINTF(fmt, ...) \
36a7bf3034SPavel Fedin     do { fprintf(stderr, "kvm_gicv3: " fmt, ## __VA_ARGS__); } while (0)
37a7bf3034SPavel Fedin #else
38a7bf3034SPavel Fedin #define DPRINTF(fmt, ...) \
39a7bf3034SPavel Fedin     do { } while (0)
40a7bf3034SPavel Fedin #endif
41a7bf3034SPavel Fedin 
42a7bf3034SPavel Fedin #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
43a7bf3034SPavel Fedin #define KVM_ARM_GICV3(obj) \
44a7bf3034SPavel Fedin      OBJECT_CHECK(GICv3State, (obj), TYPE_KVM_ARM_GICV3)
45a7bf3034SPavel Fedin #define KVM_ARM_GICV3_CLASS(klass) \
46a7bf3034SPavel Fedin      OBJECT_CLASS_CHECK(KVMARMGICv3Class, (klass), TYPE_KVM_ARM_GICV3)
47a7bf3034SPavel Fedin #define KVM_ARM_GICV3_GET_CLASS(obj) \
48a7bf3034SPavel Fedin      OBJECT_GET_CLASS(KVMARMGICv3Class, (obj), TYPE_KVM_ARM_GICV3)
49a7bf3034SPavel Fedin 
50367b9f52SVijaya Kumar K #define   KVM_DEV_ARM_VGIC_SYSREG(op0, op1, crn, crm, op2)         \
51367b9f52SVijaya Kumar K                              (ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
52367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
53367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
54367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
55367b9f52SVijaya Kumar K                               ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
56367b9f52SVijaya Kumar K 
57367b9f52SVijaya Kumar K #define ICC_PMR_EL1     \
58367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 4, 6, 0)
59367b9f52SVijaya Kumar K #define ICC_BPR0_EL1    \
60367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 3)
61367b9f52SVijaya Kumar K #define ICC_AP0R_EL1(n) \
62367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 4 | n)
63367b9f52SVijaya Kumar K #define ICC_AP1R_EL1(n) \
64367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 9, n)
65367b9f52SVijaya Kumar K #define ICC_BPR1_EL1    \
66367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 3)
67367b9f52SVijaya Kumar K #define ICC_CTLR_EL1    \
68367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 4)
69367b9f52SVijaya Kumar K #define ICC_SRE_EL1 \
70367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 5)
71367b9f52SVijaya Kumar K #define ICC_IGRPEN0_EL1 \
72367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 6)
73367b9f52SVijaya Kumar K #define ICC_IGRPEN1_EL1 \
74367b9f52SVijaya Kumar K     KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 7)
75367b9f52SVijaya Kumar K 
76a7bf3034SPavel Fedin typedef struct KVMARMGICv3Class {
77a7bf3034SPavel Fedin     ARMGICv3CommonClass parent_class;
78a7bf3034SPavel Fedin     DeviceRealize parent_realize;
79a7bf3034SPavel Fedin     void (*parent_reset)(DeviceState *dev);
80a7bf3034SPavel Fedin } KVMARMGICv3Class;
81a7bf3034SPavel Fedin 
82a7bf3034SPavel Fedin static void kvm_arm_gicv3_set_irq(void *opaque, int irq, int level)
83a7bf3034SPavel Fedin {
84a7bf3034SPavel Fedin     GICv3State *s = (GICv3State *)opaque;
85a7bf3034SPavel Fedin 
86a7bf3034SPavel Fedin     kvm_arm_gic_set_irq(s->num_irq, irq, level);
87a7bf3034SPavel Fedin }
88a7bf3034SPavel Fedin 
89367b9f52SVijaya Kumar K #define KVM_VGIC_ATTR(reg, typer) \
90367b9f52SVijaya Kumar K     ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (reg))
91367b9f52SVijaya Kumar K 
92367b9f52SVijaya Kumar K static inline void kvm_gicd_access(GICv3State *s, int offset,
93367b9f52SVijaya Kumar K                                    uint32_t *val, bool write)
94367b9f52SVijaya Kumar K {
95367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
96367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(offset, 0),
97556969e9SEric Auger                       val, write, &error_abort);
98367b9f52SVijaya Kumar K }
99367b9f52SVijaya Kumar K 
100367b9f52SVijaya Kumar K static inline void kvm_gicr_access(GICv3State *s, int offset, int cpu,
101367b9f52SVijaya Kumar K                                    uint32_t *val, bool write)
102367b9f52SVijaya Kumar K {
103367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
104367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(offset, s->cpu[cpu].gicr_typer),
105556969e9SEric Auger                       val, write, &error_abort);
106367b9f52SVijaya Kumar K }
107367b9f52SVijaya Kumar K 
108367b9f52SVijaya Kumar K static inline void kvm_gicc_access(GICv3State *s, uint64_t reg, int cpu,
109367b9f52SVijaya Kumar K                                    uint64_t *val, bool write)
110367b9f52SVijaya Kumar K {
111367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
112367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(reg, s->cpu[cpu].gicr_typer),
113556969e9SEric Auger                       val, write, &error_abort);
114367b9f52SVijaya Kumar K }
115367b9f52SVijaya Kumar K 
116367b9f52SVijaya Kumar K static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu,
117367b9f52SVijaya Kumar K                                              uint32_t *val, bool write)
118367b9f52SVijaya Kumar K {
119367b9f52SVijaya Kumar K     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
120367b9f52SVijaya Kumar K                       KVM_VGIC_ATTR(irq, s->cpu[cpu].gicr_typer) |
121367b9f52SVijaya Kumar K                       (VGIC_LEVEL_INFO_LINE_LEVEL <<
122367b9f52SVijaya Kumar K                        KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT),
123556969e9SEric Auger                       val, write, &error_abort);
124367b9f52SVijaya Kumar K }
125367b9f52SVijaya Kumar K 
126367b9f52SVijaya Kumar K /* Loop through each distributor IRQ related register; since bits
127367b9f52SVijaya Kumar K  * corresponding to SPIs and PPIs are RAZ/WI when affinity routing
128367b9f52SVijaya Kumar K  * is enabled, we skip those.
129367b9f52SVijaya Kumar K  */
130367b9f52SVijaya Kumar K #define for_each_dist_irq_reg(_irq, _max, _field_width) \
131367b9f52SVijaya Kumar K     for (_irq = GIC_INTERNAL; _irq < _max; _irq += (32 / _field_width))
132367b9f52SVijaya Kumar K 
133367b9f52SVijaya Kumar K static void kvm_dist_get_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
134367b9f52SVijaya Kumar K {
135367b9f52SVijaya Kumar K     uint32_t reg, *field;
136367b9f52SVijaya Kumar K     int irq;
137367b9f52SVijaya Kumar K 
138367b9f52SVijaya Kumar K     field = (uint32_t *)bmp;
139367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 8) {
140367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
141367b9f52SVijaya Kumar K         *field = reg;
142367b9f52SVijaya Kumar K         offset += 4;
143367b9f52SVijaya Kumar K         field++;
144367b9f52SVijaya Kumar K     }
145367b9f52SVijaya Kumar K }
146367b9f52SVijaya Kumar K 
147367b9f52SVijaya Kumar K static void kvm_dist_put_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
148367b9f52SVijaya Kumar K {
149367b9f52SVijaya Kumar K     uint32_t reg, *field;
150367b9f52SVijaya Kumar K     int irq;
151367b9f52SVijaya Kumar K 
152367b9f52SVijaya Kumar K     field = (uint32_t *)bmp;
153367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 8) {
154367b9f52SVijaya Kumar K         reg = *field;
155367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
156367b9f52SVijaya Kumar K         offset += 4;
157367b9f52SVijaya Kumar K         field++;
158367b9f52SVijaya Kumar K     }
159367b9f52SVijaya Kumar K }
160367b9f52SVijaya Kumar K 
161367b9f52SVijaya Kumar K static void kvm_dist_get_edge_trigger(GICv3State *s, uint32_t offset,
162367b9f52SVijaya Kumar K                                       uint32_t *bmp)
163367b9f52SVijaya Kumar K {
164367b9f52SVijaya Kumar K     uint32_t reg;
165367b9f52SVijaya Kumar K     int irq;
166367b9f52SVijaya Kumar K 
167367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 2) {
168367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
169367b9f52SVijaya Kumar K         reg = half_unshuffle32(reg >> 1);
170367b9f52SVijaya Kumar K         if (irq % 32 != 0) {
171367b9f52SVijaya Kumar K             reg = (reg << 16);
172367b9f52SVijaya Kumar K         }
173367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) |=  reg;
174367b9f52SVijaya Kumar K         offset += 4;
175367b9f52SVijaya Kumar K     }
176367b9f52SVijaya Kumar K }
177367b9f52SVijaya Kumar K 
178367b9f52SVijaya Kumar K static void kvm_dist_put_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 
184367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 2) {
185367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
186367b9f52SVijaya Kumar K         if (irq % 32 != 0) {
187367b9f52SVijaya Kumar K             reg = (reg & 0xffff0000) >> 16;
188367b9f52SVijaya Kumar K         } else {
189367b9f52SVijaya Kumar K             reg = reg & 0xffff;
190367b9f52SVijaya Kumar K         }
191367b9f52SVijaya Kumar K         reg = half_shuffle32(reg) << 1;
192367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
193367b9f52SVijaya Kumar K         offset += 4;
194367b9f52SVijaya Kumar K     }
195367b9f52SVijaya Kumar K }
196367b9f52SVijaya Kumar K 
197367b9f52SVijaya Kumar K static void kvm_gic_get_line_level_bmp(GICv3State *s, uint32_t *bmp)
198367b9f52SVijaya Kumar K {
199367b9f52SVijaya Kumar K     uint32_t reg;
200367b9f52SVijaya Kumar K     int irq;
201367b9f52SVijaya Kumar K 
202367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
203367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, irq, 0, &reg, false);
204367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) = reg;
205367b9f52SVijaya Kumar K     }
206367b9f52SVijaya Kumar K }
207367b9f52SVijaya Kumar K 
208367b9f52SVijaya Kumar K static void kvm_gic_put_line_level_bmp(GICv3State *s, uint32_t *bmp)
209367b9f52SVijaya Kumar K {
210367b9f52SVijaya Kumar K     uint32_t reg;
211367b9f52SVijaya Kumar K     int irq;
212367b9f52SVijaya Kumar K 
213367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
214367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
215367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, irq, 0, &reg, true);
216367b9f52SVijaya Kumar K     }
217367b9f52SVijaya Kumar K }
218367b9f52SVijaya Kumar K 
219367b9f52SVijaya Kumar K /* Read a bitmap register group from the kernel VGIC. */
220367b9f52SVijaya Kumar K static void kvm_dist_getbmp(GICv3State *s, uint32_t offset, uint32_t *bmp)
221367b9f52SVijaya Kumar K {
222367b9f52SVijaya Kumar K     uint32_t reg;
223367b9f52SVijaya Kumar K     int irq;
224367b9f52SVijaya Kumar K 
225367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
226367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, false);
227367b9f52SVijaya Kumar K         *gic_bmp_ptr32(bmp, irq) = reg;
228367b9f52SVijaya Kumar K         offset += 4;
229367b9f52SVijaya Kumar K     }
230367b9f52SVijaya Kumar K }
231367b9f52SVijaya Kumar K 
232367b9f52SVijaya Kumar K static void kvm_dist_putbmp(GICv3State *s, uint32_t offset,
233367b9f52SVijaya Kumar K                             uint32_t clroffset, uint32_t *bmp)
234367b9f52SVijaya Kumar K {
235367b9f52SVijaya Kumar K     uint32_t reg;
236367b9f52SVijaya Kumar K     int irq;
237367b9f52SVijaya Kumar K 
238367b9f52SVijaya Kumar K     for_each_dist_irq_reg(irq, s->num_irq, 1) {
239367b9f52SVijaya Kumar K         /* If this bitmap is a set/clear register pair, first write to the
240367b9f52SVijaya Kumar K          * clear-reg to clear all bits before using the set-reg to write
241367b9f52SVijaya Kumar K          * the 1 bits.
242367b9f52SVijaya Kumar K          */
243367b9f52SVijaya Kumar K         if (clroffset != 0) {
244367b9f52SVijaya Kumar K             reg = 0;
245367b9f52SVijaya Kumar K             kvm_gicd_access(s, clroffset, &reg, true);
246367b9f52SVijaya Kumar K         }
247367b9f52SVijaya Kumar K         reg = *gic_bmp_ptr32(bmp, irq);
248367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
249367b9f52SVijaya Kumar K         offset += 4;
250367b9f52SVijaya Kumar K     }
251367b9f52SVijaya Kumar K }
252367b9f52SVijaya Kumar K 
253367b9f52SVijaya Kumar K static void kvm_arm_gicv3_check(GICv3State *s)
254367b9f52SVijaya Kumar K {
255367b9f52SVijaya Kumar K     uint32_t reg;
256367b9f52SVijaya Kumar K     uint32_t num_irq;
257367b9f52SVijaya Kumar K 
258367b9f52SVijaya Kumar K     /* Sanity checking s->num_irq */
259367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_TYPER, &reg, false);
260367b9f52SVijaya Kumar K     num_irq = ((reg & 0x1f) + 1) * 32;
261367b9f52SVijaya Kumar K 
262367b9f52SVijaya Kumar K     if (num_irq < s->num_irq) {
263367b9f52SVijaya Kumar K         error_report("Model requests %u IRQs, but kernel supports max %u",
264367b9f52SVijaya Kumar K                      s->num_irq, num_irq);
265367b9f52SVijaya Kumar K         abort();
266367b9f52SVijaya Kumar K     }
267367b9f52SVijaya Kumar K }
268367b9f52SVijaya Kumar K 
269a7bf3034SPavel Fedin static void kvm_arm_gicv3_put(GICv3State *s)
270a7bf3034SPavel Fedin {
271367b9f52SVijaya Kumar K     uint32_t regl, regh, reg;
272367b9f52SVijaya Kumar K     uint64_t reg64, redist_typer;
273367b9f52SVijaya Kumar K     int ncpu, i;
274367b9f52SVijaya Kumar K 
275367b9f52SVijaya Kumar K     kvm_arm_gicv3_check(s);
276367b9f52SVijaya Kumar K 
277367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
278367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
279367b9f52SVijaya Kumar K     redist_typer = ((uint64_t)regh << 32) | regl;
280367b9f52SVijaya Kumar K 
281367b9f52SVijaya Kumar K     reg = s->gicd_ctlr;
282367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_CTLR, &reg, true);
283367b9f52SVijaya Kumar K 
284367b9f52SVijaya Kumar K     if (redist_typer & GICR_TYPER_PLPIS) {
285367b9f52SVijaya Kumar K         /* Set base addresses before LPIs are enabled by GICR_CTLR write */
286367b9f52SVijaya Kumar K         for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
287367b9f52SVijaya Kumar K             GICv3CPUState *c = &s->cpu[ncpu];
288367b9f52SVijaya Kumar K 
289367b9f52SVijaya Kumar K             reg64 = c->gicr_propbaser;
290367b9f52SVijaya Kumar K             regl = (uint32_t)reg64;
291367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, true);
292367b9f52SVijaya Kumar K             regh = (uint32_t)(reg64 >> 32);
293367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, true);
294367b9f52SVijaya Kumar K 
295367b9f52SVijaya Kumar K             reg64 = c->gicr_pendbaser;
2967229ec58SPranith Kumar             if (!(c->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
297367b9f52SVijaya Kumar K                 /* Setting PTZ is advised if LPIs are disabled, to reduce
298367b9f52SVijaya Kumar K                  * GIC initialization time.
299367b9f52SVijaya Kumar K                  */
300367b9f52SVijaya Kumar K                 reg64 |= GICR_PENDBASER_PTZ;
301367b9f52SVijaya Kumar K             }
302367b9f52SVijaya Kumar K             regl = (uint32_t)reg64;
303367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, true);
304367b9f52SVijaya Kumar K             regh = (uint32_t)(reg64 >> 32);
305367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, true);
306367b9f52SVijaya Kumar K         }
307367b9f52SVijaya Kumar K     }
308367b9f52SVijaya Kumar K 
309367b9f52SVijaya Kumar K     /* Redistributor state (one per CPU) */
310367b9f52SVijaya Kumar K 
311367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
312367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
313367b9f52SVijaya Kumar K 
314367b9f52SVijaya Kumar K         reg = c->gicr_ctlr;
315367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, true);
316367b9f52SVijaya Kumar K 
317367b9f52SVijaya Kumar K         reg = c->gicr_statusr[GICV3_NS];
318367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, true);
319367b9f52SVijaya Kumar K 
320367b9f52SVijaya Kumar K         reg = c->gicr_waker;
321367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, true);
322367b9f52SVijaya Kumar K 
323367b9f52SVijaya Kumar K         reg = c->gicr_igroupr0;
324367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, true);
325367b9f52SVijaya Kumar K 
326367b9f52SVijaya Kumar K         reg = ~0;
327367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICENABLER0, ncpu, &reg, true);
328367b9f52SVijaya Kumar K         reg = c->gicr_ienabler0;
329367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, true);
330367b9f52SVijaya Kumar K 
331367b9f52SVijaya Kumar K         /* Restore config before pending so we treat level/edge correctly */
332367b9f52SVijaya Kumar K         reg = half_shuffle32(c->edge_trigger >> 16) << 1;
333367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, true);
334367b9f52SVijaya Kumar K 
335367b9f52SVijaya Kumar K         reg = c->level;
336367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, 0, ncpu, &reg, true);
337367b9f52SVijaya Kumar K 
338367b9f52SVijaya Kumar K         reg = ~0;
339367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICPENDR0, ncpu, &reg, true);
340367b9f52SVijaya Kumar K         reg = c->gicr_ipendr0;
341367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, true);
342367b9f52SVijaya Kumar K 
343367b9f52SVijaya Kumar K         reg = ~0;
344367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICACTIVER0, ncpu, &reg, true);
345367b9f52SVijaya Kumar K         reg = c->gicr_iactiver0;
346367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, true);
347367b9f52SVijaya Kumar K 
348367b9f52SVijaya Kumar K         for (i = 0; i < GIC_INTERNAL; i += 4) {
349367b9f52SVijaya Kumar K             reg = c->gicr_ipriorityr[i] |
350367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 1] << 8) |
351367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 2] << 16) |
352367b9f52SVijaya Kumar K                 (c->gicr_ipriorityr[i + 3] << 24);
353367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, true);
354367b9f52SVijaya Kumar K         }
355367b9f52SVijaya Kumar K     }
356367b9f52SVijaya Kumar K 
357367b9f52SVijaya Kumar K     /* Distributor state (shared between all CPUs */
358367b9f52SVijaya Kumar K     reg = s->gicd_statusr[GICV3_NS];
359367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_STATUSR, &reg, true);
360367b9f52SVijaya Kumar K 
361367b9f52SVijaya Kumar K     /* s->enable bitmap -> GICD_ISENABLERn */
362367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISENABLER, GICD_ICENABLER, s->enabled);
363367b9f52SVijaya Kumar K 
364367b9f52SVijaya Kumar K     /* s->group bitmap -> GICD_IGROUPRn */
365367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_IGROUPR, 0, s->group);
366367b9f52SVijaya Kumar K 
367367b9f52SVijaya Kumar K     /* Restore targets before pending to ensure the pending state is set on
368367b9f52SVijaya Kumar K      * the appropriate CPU interfaces in the kernel
369367b9f52SVijaya Kumar K      */
370367b9f52SVijaya Kumar K 
371367b9f52SVijaya Kumar K     /* s->gicd_irouter[irq] -> GICD_IROUTERn
372367b9f52SVijaya Kumar K      * We can't use kvm_dist_put() here because the registers are 64-bit
373367b9f52SVijaya Kumar K      */
374367b9f52SVijaya Kumar K     for (i = GIC_INTERNAL; i < s->num_irq; i++) {
375367b9f52SVijaya Kumar K         uint32_t offset;
376367b9f52SVijaya Kumar K 
377367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i);
378367b9f52SVijaya Kumar K         reg = (uint32_t)s->gicd_irouter[i];
379367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
380367b9f52SVijaya Kumar K 
381367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
382367b9f52SVijaya Kumar K         reg = (uint32_t)(s->gicd_irouter[i] >> 32);
383367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &reg, true);
384367b9f52SVijaya Kumar K     }
385367b9f52SVijaya Kumar K 
386367b9f52SVijaya Kumar K     /* s->trigger bitmap -> GICD_ICFGRn
387367b9f52SVijaya Kumar K      * (restore configuration registers before pending IRQs so we treat
388367b9f52SVijaya Kumar K      * level/edge correctly)
389367b9f52SVijaya Kumar K      */
390367b9f52SVijaya Kumar K     kvm_dist_put_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
391367b9f52SVijaya Kumar K 
392367b9f52SVijaya Kumar K     /* s->level bitmap ->  line_level */
393367b9f52SVijaya Kumar K     kvm_gic_put_line_level_bmp(s, s->level);
394367b9f52SVijaya Kumar K 
395367b9f52SVijaya Kumar K     /* s->pending bitmap -> GICD_ISPENDRn */
396367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISPENDR, GICD_ICPENDR, s->pending);
397367b9f52SVijaya Kumar K 
398367b9f52SVijaya Kumar K     /* s->active bitmap -> GICD_ISACTIVERn */
399367b9f52SVijaya Kumar K     kvm_dist_putbmp(s, GICD_ISACTIVER, GICD_ICACTIVER, s->active);
400367b9f52SVijaya Kumar K 
401367b9f52SVijaya Kumar K     /* s->gicd_ipriority[] -> GICD_IPRIORITYRn */
402367b9f52SVijaya Kumar K     kvm_dist_put_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
403367b9f52SVijaya Kumar K 
404367b9f52SVijaya Kumar K     /* CPU Interface state (one per CPU) */
405367b9f52SVijaya Kumar K 
406367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
407367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
408367b9f52SVijaya Kumar K         int num_pri_bits;
409367b9f52SVijaya Kumar K 
410367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, true);
411367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
412367b9f52SVijaya Kumar K                         &c->icc_ctlr_el1[GICV3_NS], true);
413367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
414367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G0], true);
415367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
416367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G1NS], true);
417367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, true);
418367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], true);
419367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], true);
420367b9f52SVijaya Kumar K 
421367b9f52SVijaya Kumar K         num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
422367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_MASK) >>
423367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
424367b9f52SVijaya Kumar K 
425367b9f52SVijaya Kumar K         switch (num_pri_bits) {
426367b9f52SVijaya Kumar K         case 7:
427367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][3];
428367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, true);
429367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][2];
430367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, true);
431367b9f52SVijaya Kumar K         case 6:
432367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][1];
433367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
434367b9f52SVijaya Kumar K         default:
435367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G0][0];
436367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
437367b9f52SVijaya Kumar K         }
438367b9f52SVijaya Kumar K 
439367b9f52SVijaya Kumar K         switch (num_pri_bits) {
440367b9f52SVijaya Kumar K         case 7:
441367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][3];
442367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
443367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][2];
444367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
445367b9f52SVijaya Kumar K         case 6:
446367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][1];
447367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
448367b9f52SVijaya Kumar K         default:
449367b9f52SVijaya Kumar K             reg64 = c->icc_apr[GICV3_G1NS][0];
450367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, true);
451367b9f52SVijaya Kumar K         }
452367b9f52SVijaya Kumar K     }
453a7bf3034SPavel Fedin }
454a7bf3034SPavel Fedin 
455a7bf3034SPavel Fedin static void kvm_arm_gicv3_get(GICv3State *s)
456a7bf3034SPavel Fedin {
457367b9f52SVijaya Kumar K     uint32_t regl, regh, reg;
458367b9f52SVijaya Kumar K     uint64_t reg64, redist_typer;
459367b9f52SVijaya Kumar K     int ncpu, i;
460367b9f52SVijaya Kumar K 
461367b9f52SVijaya Kumar K     kvm_arm_gicv3_check(s);
462367b9f52SVijaya Kumar K 
463367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
464367b9f52SVijaya Kumar K     kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
465367b9f52SVijaya Kumar K     redist_typer = ((uint64_t)regh << 32) | regl;
466367b9f52SVijaya Kumar K 
467367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_CTLR, &reg, false);
468367b9f52SVijaya Kumar K     s->gicd_ctlr = reg;
469367b9f52SVijaya Kumar K 
470367b9f52SVijaya Kumar K     /* Redistributor state (one per CPU) */
471367b9f52SVijaya Kumar K 
472367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
473367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
474367b9f52SVijaya Kumar K 
475367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, false);
476367b9f52SVijaya Kumar K         c->gicr_ctlr = reg;
477367b9f52SVijaya Kumar K 
478367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, false);
479367b9f52SVijaya Kumar K         c->gicr_statusr[GICV3_NS] = reg;
480367b9f52SVijaya Kumar K 
481367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, false);
482367b9f52SVijaya Kumar K         c->gicr_waker = reg;
483367b9f52SVijaya Kumar K 
484367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, false);
485367b9f52SVijaya Kumar K         c->gicr_igroupr0 = reg;
486367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, false);
487367b9f52SVijaya Kumar K         c->gicr_ienabler0 = reg;
488367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, false);
489367b9f52SVijaya Kumar K         c->edge_trigger = half_unshuffle32(reg >> 1) << 16;
490367b9f52SVijaya Kumar K         kvm_gic_line_level_access(s, 0, ncpu, &reg, false);
491367b9f52SVijaya Kumar K         c->level = reg;
492367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, false);
493367b9f52SVijaya Kumar K         c->gicr_ipendr0 = reg;
494367b9f52SVijaya Kumar K         kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, false);
495367b9f52SVijaya Kumar K         c->gicr_iactiver0 = reg;
496367b9f52SVijaya Kumar K 
497367b9f52SVijaya Kumar K         for (i = 0; i < GIC_INTERNAL; i += 4) {
498367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, false);
499367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i] = extract32(reg, 0, 8);
500367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 1] = extract32(reg, 8, 8);
501367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 2] = extract32(reg, 16, 8);
502367b9f52SVijaya Kumar K             c->gicr_ipriorityr[i + 3] = extract32(reg, 24, 8);
503367b9f52SVijaya Kumar K         }
504367b9f52SVijaya Kumar K     }
505367b9f52SVijaya Kumar K 
506367b9f52SVijaya Kumar K     if (redist_typer & GICR_TYPER_PLPIS) {
507367b9f52SVijaya Kumar K         for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
508367b9f52SVijaya Kumar K             GICv3CPUState *c = &s->cpu[ncpu];
509367b9f52SVijaya Kumar K 
510367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, false);
511367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, false);
512367b9f52SVijaya Kumar K             c->gicr_propbaser = ((uint64_t)regh << 32) | regl;
513367b9f52SVijaya Kumar K 
514367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, false);
515367b9f52SVijaya Kumar K             kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, false);
516367b9f52SVijaya Kumar K             c->gicr_pendbaser = ((uint64_t)regh << 32) | regl;
517367b9f52SVijaya Kumar K         }
518367b9f52SVijaya Kumar K     }
519367b9f52SVijaya Kumar K 
520367b9f52SVijaya Kumar K     /* Distributor state (shared between all CPUs */
521367b9f52SVijaya Kumar K 
522367b9f52SVijaya Kumar K     kvm_gicd_access(s, GICD_STATUSR, &reg, false);
523367b9f52SVijaya Kumar K     s->gicd_statusr[GICV3_NS] = reg;
524367b9f52SVijaya Kumar K 
525367b9f52SVijaya Kumar K     /* GICD_IGROUPRn -> s->group bitmap */
526367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_IGROUPR, s->group);
527367b9f52SVijaya Kumar K 
528367b9f52SVijaya Kumar K     /* GICD_ISENABLERn -> s->enabled bitmap */
529367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISENABLER, s->enabled);
530367b9f52SVijaya Kumar K 
531367b9f52SVijaya Kumar K     /* Line level of irq */
532367b9f52SVijaya Kumar K     kvm_gic_get_line_level_bmp(s, s->level);
533367b9f52SVijaya Kumar K     /* GICD_ISPENDRn -> s->pending bitmap */
534367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISPENDR, s->pending);
535367b9f52SVijaya Kumar K 
536367b9f52SVijaya Kumar K     /* GICD_ISACTIVERn -> s->active bitmap */
537367b9f52SVijaya Kumar K     kvm_dist_getbmp(s, GICD_ISACTIVER, s->active);
538367b9f52SVijaya Kumar K 
539367b9f52SVijaya Kumar K     /* GICD_ICFGRn -> s->trigger bitmap */
540367b9f52SVijaya Kumar K     kvm_dist_get_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
541367b9f52SVijaya Kumar K 
542367b9f52SVijaya Kumar K     /* GICD_IPRIORITYRn -> s->gicd_ipriority[] */
543367b9f52SVijaya Kumar K     kvm_dist_get_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
544367b9f52SVijaya Kumar K 
545367b9f52SVijaya Kumar K     /* GICD_IROUTERn -> s->gicd_irouter[irq] */
546367b9f52SVijaya Kumar K     for (i = GIC_INTERNAL; i < s->num_irq; i++) {
547367b9f52SVijaya Kumar K         uint32_t offset;
548367b9f52SVijaya Kumar K 
549367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i);
550367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &regl, false);
551367b9f52SVijaya Kumar K         offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
552367b9f52SVijaya Kumar K         kvm_gicd_access(s, offset, &regh, false);
553367b9f52SVijaya Kumar K         s->gicd_irouter[i] = ((uint64_t)regh << 32) | regl;
554367b9f52SVijaya Kumar K     }
555367b9f52SVijaya Kumar K 
556367b9f52SVijaya Kumar K     /*****************************************************************
557367b9f52SVijaya Kumar K      * CPU Interface(s) State
558367b9f52SVijaya Kumar K      */
559367b9f52SVijaya Kumar K 
560367b9f52SVijaya Kumar K     for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
561367b9f52SVijaya Kumar K         GICv3CPUState *c = &s->cpu[ncpu];
562367b9f52SVijaya Kumar K         int num_pri_bits;
563367b9f52SVijaya Kumar K 
564367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, false);
565367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
566367b9f52SVijaya Kumar K                         &c->icc_ctlr_el1[GICV3_NS], false);
567367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
568367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G0], false);
569367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
570367b9f52SVijaya Kumar K                         &c->icc_igrpen[GICV3_G1NS], false);
571367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, false);
572367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], false);
573367b9f52SVijaya Kumar K         kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], false);
574367b9f52SVijaya Kumar K         num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
575367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_MASK) >>
576367b9f52SVijaya Kumar K                         ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
577367b9f52SVijaya Kumar K 
578367b9f52SVijaya Kumar K         switch (num_pri_bits) {
579367b9f52SVijaya Kumar K         case 7:
580367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, false);
581367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][3] = reg64;
582367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, false);
583367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][2] = reg64;
584367b9f52SVijaya Kumar K         case 6:
585367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
586367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][1] = reg64;
587367b9f52SVijaya Kumar K         default:
588367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
589367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G0][0] = reg64;
590367b9f52SVijaya Kumar K         }
591367b9f52SVijaya Kumar K 
592367b9f52SVijaya Kumar K         switch (num_pri_bits) {
593367b9f52SVijaya Kumar K         case 7:
594367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
595367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][3] = reg64;
596367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
597367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][2] = reg64;
598367b9f52SVijaya Kumar K         case 6:
599367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
600367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][1] = reg64;
601367b9f52SVijaya Kumar K         default:
602367b9f52SVijaya Kumar K             kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
603367b9f52SVijaya Kumar K             c->icc_apr[GICV3_G1NS][0] = reg64;
604367b9f52SVijaya Kumar K         }
605367b9f52SVijaya Kumar K     }
606a7bf3034SPavel Fedin }
607a7bf3034SPavel Fedin 
60807a5628cSVijaya Kumar K static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
60907a5628cSVijaya Kumar K {
61007a5628cSVijaya Kumar K     ARMCPU *cpu;
61107a5628cSVijaya Kumar K     GICv3State *s;
61207a5628cSVijaya Kumar K     GICv3CPUState *c;
61307a5628cSVijaya Kumar K 
61407a5628cSVijaya Kumar K     c = (GICv3CPUState *)env->gicv3state;
61507a5628cSVijaya Kumar K     s = c->gic;
61607a5628cSVijaya Kumar K     cpu = ARM_CPU(c->cpu);
61707a5628cSVijaya Kumar K 
61807a5628cSVijaya Kumar K     c->icc_pmr_el1 = 0;
61907a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
62007a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
62107a5628cSVijaya Kumar K     c->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
62207a5628cSVijaya Kumar K 
62307a5628cSVijaya Kumar K     c->icc_sre_el1 = 0x7;
62407a5628cSVijaya Kumar K     memset(c->icc_apr, 0, sizeof(c->icc_apr));
62507a5628cSVijaya Kumar K     memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
626e7d54416SEric Auger 
627e7d54416SEric Auger     if (s->migration_blocker) {
628e7d54416SEric Auger         return;
629e7d54416SEric Auger     }
630e7d54416SEric Auger 
631e7d54416SEric Auger     /* Initialize to actual HW supported configuration */
632e7d54416SEric Auger     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
633e7d54416SEric Auger                       KVM_VGIC_ATTR(ICC_CTLR_EL1, cpu->mp_affinity),
634556969e9SEric Auger                       &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
635e7d54416SEric Auger 
636e7d54416SEric Auger     c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
63707a5628cSVijaya Kumar K }
63807a5628cSVijaya Kumar K 
639a7bf3034SPavel Fedin static void kvm_arm_gicv3_reset(DeviceState *dev)
640a7bf3034SPavel Fedin {
641a7bf3034SPavel Fedin     GICv3State *s = ARM_GICV3_COMMON(dev);
642a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
643a7bf3034SPavel Fedin 
644a7bf3034SPavel Fedin     DPRINTF("Reset\n");
645a7bf3034SPavel Fedin 
646a7bf3034SPavel Fedin     kgc->parent_reset(dev);
647367b9f52SVijaya Kumar K 
648367b9f52SVijaya Kumar K     if (s->migration_blocker) {
649367b9f52SVijaya Kumar K         DPRINTF("Cannot put kernel gic state, no kernel interface\n");
650367b9f52SVijaya Kumar K         return;
651367b9f52SVijaya Kumar K     }
652367b9f52SVijaya Kumar K 
653a7bf3034SPavel Fedin     kvm_arm_gicv3_put(s);
654a7bf3034SPavel Fedin }
655a7bf3034SPavel Fedin 
65607a5628cSVijaya Kumar K /*
65707a5628cSVijaya Kumar K  * CPU interface registers of GIC needs to be reset on CPU reset.
65807a5628cSVijaya Kumar K  * For the calling arm_gicv3_icc_reset() on CPU reset, we register
65907a5628cSVijaya Kumar K  * below ARMCPRegInfo. As we reset the whole cpu interface under single
66007a5628cSVijaya Kumar K  * register reset, we define only one register of CPU interface instead
66107a5628cSVijaya Kumar K  * of defining all the registers.
66207a5628cSVijaya Kumar K  */
66307a5628cSVijaya Kumar K static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
66407a5628cSVijaya Kumar K     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
66507a5628cSVijaya Kumar K       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
66607a5628cSVijaya Kumar K       /*
66707a5628cSVijaya Kumar K        * If ARM_CP_NOP is used, resetfn is not called,
66807a5628cSVijaya Kumar K        * So ARM_CP_NO_RAW is appropriate type.
66907a5628cSVijaya Kumar K        */
67007a5628cSVijaya Kumar K       .type = ARM_CP_NO_RAW,
67107a5628cSVijaya Kumar K       .access = PL1_RW,
67207a5628cSVijaya Kumar K       .readfn = arm_cp_read_zero,
67307a5628cSVijaya Kumar K       .writefn = arm_cp_write_ignore,
67407a5628cSVijaya Kumar K       /*
67507a5628cSVijaya Kumar K        * We hang the whole cpu interface reset routine off here
67607a5628cSVijaya Kumar K        * rather than parcelling it out into one little function
67707a5628cSVijaya Kumar K        * per register
67807a5628cSVijaya Kumar K        */
67907a5628cSVijaya Kumar K       .resetfn = arm_gicv3_icc_reset,
68007a5628cSVijaya Kumar K     },
68107a5628cSVijaya Kumar K     REGINFO_SENTINEL
68207a5628cSVijaya Kumar K };
68307a5628cSVijaya Kumar K 
684d5aa0c22SEric Auger /**
685d5aa0c22SEric Auger  * vm_change_state_handler - VM change state callback aiming at flushing
686d5aa0c22SEric Auger  * RDIST pending tables into guest RAM
687d5aa0c22SEric Auger  *
688d5aa0c22SEric Auger  * The tables get flushed to guest RAM whenever the VM gets stopped.
689d5aa0c22SEric Auger  */
690d5aa0c22SEric Auger static void vm_change_state_handler(void *opaque, int running,
691d5aa0c22SEric Auger                                     RunState state)
692d5aa0c22SEric Auger {
693d5aa0c22SEric Auger     GICv3State *s = (GICv3State *)opaque;
694d5aa0c22SEric Auger     Error *err = NULL;
695d5aa0c22SEric Auger     int ret;
696d5aa0c22SEric Auger 
697d5aa0c22SEric Auger     if (running) {
698d5aa0c22SEric Auger         return;
699d5aa0c22SEric Auger     }
700d5aa0c22SEric Auger 
701d5aa0c22SEric Auger     ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
702d5aa0c22SEric Auger                            KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
703d5aa0c22SEric Auger                            NULL, true, &err);
704d5aa0c22SEric Auger     if (err) {
705d5aa0c22SEric Auger         error_report_err(err);
706d5aa0c22SEric Auger     }
707d5aa0c22SEric Auger     if (ret < 0 && ret != -EFAULT) {
708d5aa0c22SEric Auger         abort();
709d5aa0c22SEric Auger     }
710d5aa0c22SEric Auger }
711d5aa0c22SEric Auger 
712d5aa0c22SEric Auger 
713a7bf3034SPavel Fedin static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
714a7bf3034SPavel Fedin {
715a7bf3034SPavel Fedin     GICv3State *s = KVM_ARM_GICV3(dev);
716a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
717a7bf3034SPavel Fedin     Error *local_err = NULL;
718d19a4d4eSEric Auger     int i;
719a7bf3034SPavel Fedin 
720a7bf3034SPavel Fedin     DPRINTF("kvm_arm_gicv3_realize\n");
721a7bf3034SPavel Fedin 
722a7bf3034SPavel Fedin     kgc->parent_realize(dev, &local_err);
723a7bf3034SPavel Fedin     if (local_err) {
724a7bf3034SPavel Fedin         error_propagate(errp, local_err);
725a7bf3034SPavel Fedin         return;
726a7bf3034SPavel Fedin     }
727a7bf3034SPavel Fedin 
728a7bf3034SPavel Fedin     if (s->security_extn) {
729a7bf3034SPavel Fedin         error_setg(errp, "the in-kernel VGICv3 does not implement the "
730a7bf3034SPavel Fedin                    "security extensions");
731a7bf3034SPavel Fedin         return;
732a7bf3034SPavel Fedin     }
733a7bf3034SPavel Fedin 
734a7bf3034SPavel Fedin     gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL);
735a7bf3034SPavel Fedin 
73607a5628cSVijaya Kumar K     for (i = 0; i < s->num_cpu; i++) {
73707a5628cSVijaya Kumar K         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
73807a5628cSVijaya Kumar K 
73907a5628cSVijaya Kumar K         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
74007a5628cSVijaya Kumar K     }
74107a5628cSVijaya Kumar K 
742a7bf3034SPavel Fedin     /* Try to create the device via the device control API */
743a7bf3034SPavel Fedin     s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
744a7bf3034SPavel Fedin     if (s->dev_fd < 0) {
745a7bf3034SPavel Fedin         error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
746a7bf3034SPavel Fedin         return;
747a7bf3034SPavel Fedin     }
748a7bf3034SPavel Fedin 
749a7bf3034SPavel Fedin     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
750556969e9SEric Auger                       0, &s->num_irq, true, &error_abort);
751a7bf3034SPavel Fedin 
752a7bf3034SPavel Fedin     /* Tell the kernel to complete VGIC initialization now */
753a7bf3034SPavel Fedin     kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
754556969e9SEric Auger                       KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
755a7bf3034SPavel Fedin 
756a7bf3034SPavel Fedin     kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
757a7bf3034SPavel Fedin                             KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd);
758a7bf3034SPavel Fedin     kvm_arm_register_device(&s->iomem_redist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
759a7bf3034SPavel Fedin                             KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd);
760757caeedSPavel Fedin 
761d19a4d4eSEric Auger     if (kvm_has_gsi_routing()) {
762d19a4d4eSEric Auger         /* set up irq routing */
763d19a4d4eSEric Auger         kvm_init_irq_routing(kvm_state);
764d19a4d4eSEric Auger         for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
765d19a4d4eSEric Auger             kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
766d19a4d4eSEric Auger         }
767d19a4d4eSEric Auger 
768d19a4d4eSEric Auger         kvm_gsi_routing_allowed = true;
769d19a4d4eSEric Auger 
770d19a4d4eSEric Auger         kvm_irqchip_commit_routes(kvm_state);
771d19a4d4eSEric Auger     }
772367b9f52SVijaya Kumar K 
773367b9f52SVijaya Kumar K     if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
774367b9f52SVijaya Kumar K                                GICD_CTLR)) {
775367b9f52SVijaya Kumar K         error_setg(&s->migration_blocker, "This operating system kernel does "
776367b9f52SVijaya Kumar K                                           "not support vGICv3 migration");
777367b9f52SVijaya Kumar K         migrate_add_blocker(s->migration_blocker, &local_err);
778367b9f52SVijaya Kumar K         if (local_err) {
779367b9f52SVijaya Kumar K             error_propagate(errp, local_err);
780367b9f52SVijaya Kumar K             error_free(s->migration_blocker);
781367b9f52SVijaya Kumar K             return;
782367b9f52SVijaya Kumar K         }
783367b9f52SVijaya Kumar K     }
784d5aa0c22SEric Auger     if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
785d5aa0c22SEric Auger                               KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
786d5aa0c22SEric Auger         qemu_add_vm_change_state_handler(vm_change_state_handler, s);
787d5aa0c22SEric Auger     }
788a7bf3034SPavel Fedin }
789a7bf3034SPavel Fedin 
790a7bf3034SPavel Fedin static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
791a7bf3034SPavel Fedin {
792a7bf3034SPavel Fedin     DeviceClass *dc = DEVICE_CLASS(klass);
793a7bf3034SPavel Fedin     ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
794a7bf3034SPavel Fedin     KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
795a7bf3034SPavel Fedin 
796a7bf3034SPavel Fedin     agcc->pre_save = kvm_arm_gicv3_get;
797a7bf3034SPavel Fedin     agcc->post_load = kvm_arm_gicv3_put;
798*bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
799*bf853881SPhilippe Mathieu-Daudé                                     &kgc->parent_realize);
800*bf853881SPhilippe Mathieu-Daudé     device_class_set_parent_reset(dc, kvm_arm_gicv3_reset, &kgc->parent_reset);
801a7bf3034SPavel Fedin }
802a7bf3034SPavel Fedin 
803a7bf3034SPavel Fedin static const TypeInfo kvm_arm_gicv3_info = {
804a7bf3034SPavel Fedin     .name = TYPE_KVM_ARM_GICV3,
805a7bf3034SPavel Fedin     .parent = TYPE_ARM_GICV3_COMMON,
806a7bf3034SPavel Fedin     .instance_size = sizeof(GICv3State),
807a7bf3034SPavel Fedin     .class_init = kvm_arm_gicv3_class_init,
808a7bf3034SPavel Fedin     .class_size = sizeof(KVMARMGICv3Class),
809a7bf3034SPavel Fedin };
810a7bf3034SPavel Fedin 
811a7bf3034SPavel Fedin static void kvm_arm_gicv3_register_types(void)
812a7bf3034SPavel Fedin {
813a7bf3034SPavel Fedin     type_register_static(&kvm_arm_gicv3_info);
814a7bf3034SPavel Fedin }
815a7bf3034SPavel Fedin 
816a7bf3034SPavel Fedin type_init(kvm_arm_gicv3_register_types)
817