ppc/pnv: Distribute RAM among the chips
[qemu.git] / hw / intc / arm_gicv3_kvm.c
blob5c09f00dec2bd590aa1f8ccf0b1e67c196e7409e
1 /*
2 * ARM Generic Interrupt Controller using KVM in-kernel support
4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Written by Pavel Fedin
6 * Based on vGICv2 code by Peter Maydell
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "hw/intc/arm_gicv3_common.h"
25 #include "qemu/error-report.h"
26 #include "qemu/module.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/runstate.h"
29 #include "kvm_arm.h"
30 #include "gicv3_internal.h"
31 #include "vgic_common.h"
32 #include "migration/blocker.h"
33 #include "qom/object.h"
35 #ifdef DEBUG_GICV3_KVM
36 #define DPRINTF(fmt, ...) \
37 do { fprintf(stderr, "kvm_gicv3: " fmt, ## __VA_ARGS__); } while (0)
38 #else
39 #define DPRINTF(fmt, ...) \
40 do { } while (0)
41 #endif
43 #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
44 typedef struct KVMARMGICv3Class KVMARMGICv3Class;
45 /* This is reusing the GICv3State typedef from ARM_GICV3_ITS_COMMON */
46 DECLARE_OBJ_CHECKERS(GICv3State, KVMARMGICv3Class,
47 KVM_ARM_GICV3, TYPE_KVM_ARM_GICV3)
49 #define KVM_DEV_ARM_VGIC_SYSREG(op0, op1, crn, crm, op2) \
50 (ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
51 ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
52 ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
53 ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
54 ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
56 #define ICC_PMR_EL1 \
57 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 4, 6, 0)
58 #define ICC_BPR0_EL1 \
59 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 3)
60 #define ICC_AP0R_EL1(n) \
61 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 4 | n)
62 #define ICC_AP1R_EL1(n) \
63 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 9, n)
64 #define ICC_BPR1_EL1 \
65 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 3)
66 #define ICC_CTLR_EL1 \
67 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 4)
68 #define ICC_SRE_EL1 \
69 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 5)
70 #define ICC_IGRPEN0_EL1 \
71 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 6)
72 #define ICC_IGRPEN1_EL1 \
73 KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 7)
75 struct KVMARMGICv3Class {
76 ARMGICv3CommonClass parent_class;
77 DeviceRealize parent_realize;
78 void (*parent_reset)(DeviceState *dev);
81 static void kvm_arm_gicv3_set_irq(void *opaque, int irq, int level)
83 GICv3State *s = (GICv3State *)opaque;
85 kvm_arm_gic_set_irq(s->num_irq, irq, level);
88 #define KVM_VGIC_ATTR(reg, typer) \
89 ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (reg))
91 static inline void kvm_gicd_access(GICv3State *s, int offset,
92 uint32_t *val, bool write)
94 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
95 KVM_VGIC_ATTR(offset, 0),
96 val, write, &error_abort);
99 static inline void kvm_gicr_access(GICv3State *s, int offset, int cpu,
100 uint32_t *val, bool write)
102 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
103 KVM_VGIC_ATTR(offset, s->cpu[cpu].gicr_typer),
104 val, write, &error_abort);
107 static inline void kvm_gicc_access(GICv3State *s, uint64_t reg, int cpu,
108 uint64_t *val, bool write)
110 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
111 KVM_VGIC_ATTR(reg, s->cpu[cpu].gicr_typer),
112 val, write, &error_abort);
115 static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu,
116 uint32_t *val, bool write)
118 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
119 KVM_VGIC_ATTR(irq, s->cpu[cpu].gicr_typer) |
120 (VGIC_LEVEL_INFO_LINE_LEVEL <<
121 KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT),
122 val, write, &error_abort);
125 /* Loop through each distributor IRQ related register; since bits
126 * corresponding to SPIs and PPIs are RAZ/WI when affinity routing
127 * is enabled, we skip those.
129 #define for_each_dist_irq_reg(_irq, _max, _field_width) \
130 for (_irq = GIC_INTERNAL; _irq < _max; _irq += (32 / _field_width))
132 static void kvm_dist_get_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
134 uint32_t reg, *field;
135 int irq;
137 /* For the KVM GICv3, affinity routing is always enabled, and the first 8
138 * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
139 * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
140 * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
141 * offset.
143 field = (uint32_t *)(bmp + GIC_INTERNAL);
144 offset += (GIC_INTERNAL * 8) / 8;
145 for_each_dist_irq_reg(irq, s->num_irq, 8) {
146 kvm_gicd_access(s, offset, &reg, false);
147 *field = reg;
148 offset += 4;
149 field++;
153 static void kvm_dist_put_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
155 uint32_t reg, *field;
156 int irq;
158 /* For the KVM GICv3, affinity routing is always enabled, and the first 8
159 * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
160 * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
161 * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
162 * offset.
164 field = (uint32_t *)(bmp + GIC_INTERNAL);
165 offset += (GIC_INTERNAL * 8) / 8;
166 for_each_dist_irq_reg(irq, s->num_irq, 8) {
167 reg = *field;
168 kvm_gicd_access(s, offset, &reg, true);
169 offset += 4;
170 field++;
174 static void kvm_dist_get_edge_trigger(GICv3State *s, uint32_t offset,
175 uint32_t *bmp)
177 uint32_t reg;
178 int irq;
180 /* For the KVM GICv3, affinity routing is always enabled, and the first 2
181 * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
182 * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
183 * them. So it should increase the offset to skip GIC_INTERNAL irqs.
184 * This matches the for_each_dist_irq_reg() macro which also skips the
185 * first GIC_INTERNAL irqs.
187 offset += (GIC_INTERNAL * 2) / 8;
188 for_each_dist_irq_reg(irq, s->num_irq, 2) {
189 kvm_gicd_access(s, offset, &reg, false);
190 reg = half_unshuffle32(reg >> 1);
191 if (irq % 32 != 0) {
192 reg = (reg << 16);
194 *gic_bmp_ptr32(bmp, irq) |= reg;
195 offset += 4;
199 static void kvm_dist_put_edge_trigger(GICv3State *s, uint32_t offset,
200 uint32_t *bmp)
202 uint32_t reg;
203 int irq;
205 /* For the KVM GICv3, affinity routing is always enabled, and the first 2
206 * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
207 * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
208 * them. So it should increase the offset to skip GIC_INTERNAL irqs.
209 * This matches the for_each_dist_irq_reg() macro which also skips the
210 * first GIC_INTERNAL irqs.
212 offset += (GIC_INTERNAL * 2) / 8;
213 for_each_dist_irq_reg(irq, s->num_irq, 2) {
214 reg = *gic_bmp_ptr32(bmp, irq);
215 if (irq % 32 != 0) {
216 reg = (reg & 0xffff0000) >> 16;
217 } else {
218 reg = reg & 0xffff;
220 reg = half_shuffle32(reg) << 1;
221 kvm_gicd_access(s, offset, &reg, true);
222 offset += 4;
226 static void kvm_gic_get_line_level_bmp(GICv3State *s, uint32_t *bmp)
228 uint32_t reg;
229 int irq;
231 for_each_dist_irq_reg(irq, s->num_irq, 1) {
232 kvm_gic_line_level_access(s, irq, 0, &reg, false);
233 *gic_bmp_ptr32(bmp, irq) = reg;
237 static void kvm_gic_put_line_level_bmp(GICv3State *s, uint32_t *bmp)
239 uint32_t reg;
240 int irq;
242 for_each_dist_irq_reg(irq, s->num_irq, 1) {
243 reg = *gic_bmp_ptr32(bmp, irq);
244 kvm_gic_line_level_access(s, irq, 0, &reg, true);
248 /* Read a bitmap register group from the kernel VGIC. */
249 static void kvm_dist_getbmp(GICv3State *s, uint32_t offset, uint32_t *bmp)
251 uint32_t reg;
252 int irq;
254 /* For the KVM GICv3, affinity routing is always enabled, and the
255 * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
256 * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
257 * functionality is replaced by the GICR registers. It doesn't need to sync
258 * them. So it should increase the offset to skip GIC_INTERNAL irqs.
259 * This matches the for_each_dist_irq_reg() macro which also skips the
260 * first GIC_INTERNAL irqs.
262 offset += (GIC_INTERNAL * 1) / 8;
263 for_each_dist_irq_reg(irq, s->num_irq, 1) {
264 kvm_gicd_access(s, offset, &reg, false);
265 *gic_bmp_ptr32(bmp, irq) = reg;
266 offset += 4;
270 static void kvm_dist_putbmp(GICv3State *s, uint32_t offset,
271 uint32_t clroffset, uint32_t *bmp)
273 uint32_t reg;
274 int irq;
276 /* For the KVM GICv3, affinity routing is always enabled, and the
277 * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
278 * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
279 * functionality is replaced by the GICR registers. It doesn't need to sync
280 * them. So it should increase the offset and clroffset to skip GIC_INTERNAL
281 * irqs. This matches the for_each_dist_irq_reg() macro which also skips the
282 * first GIC_INTERNAL irqs.
284 offset += (GIC_INTERNAL * 1) / 8;
285 if (clroffset != 0) {
286 clroffset += (GIC_INTERNAL * 1) / 8;
289 for_each_dist_irq_reg(irq, s->num_irq, 1) {
290 /* If this bitmap is a set/clear register pair, first write to the
291 * clear-reg to clear all bits before using the set-reg to write
292 * the 1 bits.
294 if (clroffset != 0) {
295 reg = 0;
296 kvm_gicd_access(s, clroffset, &reg, true);
297 clroffset += 4;
299 reg = *gic_bmp_ptr32(bmp, irq);
300 kvm_gicd_access(s, offset, &reg, true);
301 offset += 4;
305 static void kvm_arm_gicv3_check(GICv3State *s)
307 uint32_t reg;
308 uint32_t num_irq;
310 /* Sanity checking s->num_irq */
311 kvm_gicd_access(s, GICD_TYPER, &reg, false);
312 num_irq = ((reg & 0x1f) + 1) * 32;
314 if (num_irq < s->num_irq) {
315 error_report("Model requests %u IRQs, but kernel supports max %u",
316 s->num_irq, num_irq);
317 abort();
321 static void kvm_arm_gicv3_put(GICv3State *s)
323 uint32_t regl, regh, reg;
324 uint64_t reg64, redist_typer;
325 int ncpu, i;
327 kvm_arm_gicv3_check(s);
329 kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
330 kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
331 redist_typer = ((uint64_t)regh << 32) | regl;
333 reg = s->gicd_ctlr;
334 kvm_gicd_access(s, GICD_CTLR, &reg, true);
336 if (redist_typer & GICR_TYPER_PLPIS) {
338 * Restore base addresses before LPIs are potentially enabled by
339 * GICR_CTLR write
341 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
342 GICv3CPUState *c = &s->cpu[ncpu];
344 reg64 = c->gicr_propbaser;
345 regl = (uint32_t)reg64;
346 kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, true);
347 regh = (uint32_t)(reg64 >> 32);
348 kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, true);
350 reg64 = c->gicr_pendbaser;
351 regl = (uint32_t)reg64;
352 kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, true);
353 regh = (uint32_t)(reg64 >> 32);
354 kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, true);
358 /* Redistributor state (one per CPU) */
360 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
361 GICv3CPUState *c = &s->cpu[ncpu];
363 reg = c->gicr_ctlr;
364 kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, true);
366 reg = c->gicr_statusr[GICV3_NS];
367 kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, true);
369 reg = c->gicr_waker;
370 kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, true);
372 reg = c->gicr_igroupr0;
373 kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, true);
375 reg = ~0;
376 kvm_gicr_access(s, GICR_ICENABLER0, ncpu, &reg, true);
377 reg = c->gicr_ienabler0;
378 kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, true);
380 /* Restore config before pending so we treat level/edge correctly */
381 reg = half_shuffle32(c->edge_trigger >> 16) << 1;
382 kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, true);
384 reg = c->level;
385 kvm_gic_line_level_access(s, 0, ncpu, &reg, true);
387 reg = ~0;
388 kvm_gicr_access(s, GICR_ICPENDR0, ncpu, &reg, true);
389 reg = c->gicr_ipendr0;
390 kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, true);
392 reg = ~0;
393 kvm_gicr_access(s, GICR_ICACTIVER0, ncpu, &reg, true);
394 reg = c->gicr_iactiver0;
395 kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, true);
397 for (i = 0; i < GIC_INTERNAL; i += 4) {
398 reg = c->gicr_ipriorityr[i] |
399 (c->gicr_ipriorityr[i + 1] << 8) |
400 (c->gicr_ipriorityr[i + 2] << 16) |
401 (c->gicr_ipriorityr[i + 3] << 24);
402 kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, true);
406 /* Distributor state (shared between all CPUs */
407 reg = s->gicd_statusr[GICV3_NS];
408 kvm_gicd_access(s, GICD_STATUSR, &reg, true);
410 /* s->enable bitmap -> GICD_ISENABLERn */
411 kvm_dist_putbmp(s, GICD_ISENABLER, GICD_ICENABLER, s->enabled);
413 /* s->group bitmap -> GICD_IGROUPRn */
414 kvm_dist_putbmp(s, GICD_IGROUPR, 0, s->group);
416 /* Restore targets before pending to ensure the pending state is set on
417 * the appropriate CPU interfaces in the kernel
420 /* s->gicd_irouter[irq] -> GICD_IROUTERn
421 * We can't use kvm_dist_put() here because the registers are 64-bit
423 for (i = GIC_INTERNAL; i < s->num_irq; i++) {
424 uint32_t offset;
426 offset = GICD_IROUTER + (sizeof(uint32_t) * i);
427 reg = (uint32_t)s->gicd_irouter[i];
428 kvm_gicd_access(s, offset, &reg, true);
430 offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
431 reg = (uint32_t)(s->gicd_irouter[i] >> 32);
432 kvm_gicd_access(s, offset, &reg, true);
435 /* s->trigger bitmap -> GICD_ICFGRn
436 * (restore configuration registers before pending IRQs so we treat
437 * level/edge correctly)
439 kvm_dist_put_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
441 /* s->level bitmap -> line_level */
442 kvm_gic_put_line_level_bmp(s, s->level);
444 /* s->pending bitmap -> GICD_ISPENDRn */
445 kvm_dist_putbmp(s, GICD_ISPENDR, GICD_ICPENDR, s->pending);
447 /* s->active bitmap -> GICD_ISACTIVERn */
448 kvm_dist_putbmp(s, GICD_ISACTIVER, GICD_ICACTIVER, s->active);
450 /* s->gicd_ipriority[] -> GICD_IPRIORITYRn */
451 kvm_dist_put_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
453 /* CPU Interface state (one per CPU) */
455 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
456 GICv3CPUState *c = &s->cpu[ncpu];
457 int num_pri_bits;
459 kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, true);
460 kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
461 &c->icc_ctlr_el1[GICV3_NS], true);
462 kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
463 &c->icc_igrpen[GICV3_G0], true);
464 kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
465 &c->icc_igrpen[GICV3_G1NS], true);
466 kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, true);
467 kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], true);
468 kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], true);
470 num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
471 ICC_CTLR_EL1_PRIBITS_MASK) >>
472 ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
474 switch (num_pri_bits) {
475 case 7:
476 reg64 = c->icc_apr[GICV3_G0][3];
477 kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, true);
478 reg64 = c->icc_apr[GICV3_G0][2];
479 kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, true);
480 /* fall through */
481 case 6:
482 reg64 = c->icc_apr[GICV3_G0][1];
483 kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
484 /* fall through */
485 default:
486 reg64 = c->icc_apr[GICV3_G0][0];
487 kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
490 switch (num_pri_bits) {
491 case 7:
492 reg64 = c->icc_apr[GICV3_G1NS][3];
493 kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
494 reg64 = c->icc_apr[GICV3_G1NS][2];
495 kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
496 /* fall through */
497 case 6:
498 reg64 = c->icc_apr[GICV3_G1NS][1];
499 kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
500 /* fall through */
501 default:
502 reg64 = c->icc_apr[GICV3_G1NS][0];
503 kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, true);
508 static void kvm_arm_gicv3_get(GICv3State *s)
510 uint32_t regl, regh, reg;
511 uint64_t reg64, redist_typer;
512 int ncpu, i;
514 kvm_arm_gicv3_check(s);
516 kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
517 kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
518 redist_typer = ((uint64_t)regh << 32) | regl;
520 kvm_gicd_access(s, GICD_CTLR, &reg, false);
521 s->gicd_ctlr = reg;
523 /* Redistributor state (one per CPU) */
525 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
526 GICv3CPUState *c = &s->cpu[ncpu];
528 kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, false);
529 c->gicr_ctlr = reg;
531 kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, false);
532 c->gicr_statusr[GICV3_NS] = reg;
534 kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, false);
535 c->gicr_waker = reg;
537 kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, false);
538 c->gicr_igroupr0 = reg;
539 kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, false);
540 c->gicr_ienabler0 = reg;
541 kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, false);
542 c->edge_trigger = half_unshuffle32(reg >> 1) << 16;
543 kvm_gic_line_level_access(s, 0, ncpu, &reg, false);
544 c->level = reg;
545 kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, false);
546 c->gicr_ipendr0 = reg;
547 kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, false);
548 c->gicr_iactiver0 = reg;
550 for (i = 0; i < GIC_INTERNAL; i += 4) {
551 kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, false);
552 c->gicr_ipriorityr[i] = extract32(reg, 0, 8);
553 c->gicr_ipriorityr[i + 1] = extract32(reg, 8, 8);
554 c->gicr_ipriorityr[i + 2] = extract32(reg, 16, 8);
555 c->gicr_ipriorityr[i + 3] = extract32(reg, 24, 8);
559 if (redist_typer & GICR_TYPER_PLPIS) {
560 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
561 GICv3CPUState *c = &s->cpu[ncpu];
563 kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, false);
564 kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, false);
565 c->gicr_propbaser = ((uint64_t)regh << 32) | regl;
567 kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, false);
568 kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, false);
569 c->gicr_pendbaser = ((uint64_t)regh << 32) | regl;
573 /* Distributor state (shared between all CPUs */
575 kvm_gicd_access(s, GICD_STATUSR, &reg, false);
576 s->gicd_statusr[GICV3_NS] = reg;
578 /* GICD_IGROUPRn -> s->group bitmap */
579 kvm_dist_getbmp(s, GICD_IGROUPR, s->group);
581 /* GICD_ISENABLERn -> s->enabled bitmap */
582 kvm_dist_getbmp(s, GICD_ISENABLER, s->enabled);
584 /* Line level of irq */
585 kvm_gic_get_line_level_bmp(s, s->level);
586 /* GICD_ISPENDRn -> s->pending bitmap */
587 kvm_dist_getbmp(s, GICD_ISPENDR, s->pending);
589 /* GICD_ISACTIVERn -> s->active bitmap */
590 kvm_dist_getbmp(s, GICD_ISACTIVER, s->active);
592 /* GICD_ICFGRn -> s->trigger bitmap */
593 kvm_dist_get_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
595 /* GICD_IPRIORITYRn -> s->gicd_ipriority[] */
596 kvm_dist_get_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
598 /* GICD_IROUTERn -> s->gicd_irouter[irq] */
599 for (i = GIC_INTERNAL; i < s->num_irq; i++) {
600 uint32_t offset;
602 offset = GICD_IROUTER + (sizeof(uint32_t) * i);
603 kvm_gicd_access(s, offset, &regl, false);
604 offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
605 kvm_gicd_access(s, offset, &regh, false);
606 s->gicd_irouter[i] = ((uint64_t)regh << 32) | regl;
609 /*****************************************************************
610 * CPU Interface(s) State
613 for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
614 GICv3CPUState *c = &s->cpu[ncpu];
615 int num_pri_bits;
617 kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, false);
618 kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
619 &c->icc_ctlr_el1[GICV3_NS], false);
620 kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
621 &c->icc_igrpen[GICV3_G0], false);
622 kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
623 &c->icc_igrpen[GICV3_G1NS], false);
624 kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, false);
625 kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], false);
626 kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], false);
627 num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
628 ICC_CTLR_EL1_PRIBITS_MASK) >>
629 ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
631 switch (num_pri_bits) {
632 case 7:
633 kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, false);
634 c->icc_apr[GICV3_G0][3] = reg64;
635 kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, false);
636 c->icc_apr[GICV3_G0][2] = reg64;
637 /* fall through */
638 case 6:
639 kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
640 c->icc_apr[GICV3_G0][1] = reg64;
641 /* fall through */
642 default:
643 kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
644 c->icc_apr[GICV3_G0][0] = reg64;
647 switch (num_pri_bits) {
648 case 7:
649 kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
650 c->icc_apr[GICV3_G1NS][3] = reg64;
651 kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
652 c->icc_apr[GICV3_G1NS][2] = reg64;
653 /* fall through */
654 case 6:
655 kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
656 c->icc_apr[GICV3_G1NS][1] = reg64;
657 /* fall through */
658 default:
659 kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
660 c->icc_apr[GICV3_G1NS][0] = reg64;
665 static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
667 GICv3State *s;
668 GICv3CPUState *c;
670 c = (GICv3CPUState *)env->gicv3state;
671 s = c->gic;
673 c->icc_pmr_el1 = 0;
674 c->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
675 c->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
676 c->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
678 c->icc_sre_el1 = 0x7;
679 memset(c->icc_apr, 0, sizeof(c->icc_apr));
680 memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
682 if (s->migration_blocker) {
683 return;
686 /* Initialize to actual HW supported configuration */
687 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
688 KVM_VGIC_ATTR(ICC_CTLR_EL1, c->gicr_typer),
689 &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
691 c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
694 static void kvm_arm_gicv3_reset(DeviceState *dev)
696 GICv3State *s = ARM_GICV3_COMMON(dev);
697 KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
699 DPRINTF("Reset\n");
701 kgc->parent_reset(dev);
703 if (s->migration_blocker) {
704 DPRINTF("Cannot put kernel gic state, no kernel interface\n");
705 return;
708 kvm_arm_gicv3_put(s);
712 * CPU interface registers of GIC needs to be reset on CPU reset.
713 * For the calling arm_gicv3_icc_reset() on CPU reset, we register
714 * below ARMCPRegInfo. As we reset the whole cpu interface under single
715 * register reset, we define only one register of CPU interface instead
716 * of defining all the registers.
718 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
719 { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
720 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
722 * If ARM_CP_NOP is used, resetfn is not called,
723 * So ARM_CP_NO_RAW is appropriate type.
725 .type = ARM_CP_NO_RAW,
726 .access = PL1_RW,
727 .readfn = arm_cp_read_zero,
728 .writefn = arm_cp_write_ignore,
730 * We hang the whole cpu interface reset routine off here
731 * rather than parcelling it out into one little function
732 * per register
734 .resetfn = arm_gicv3_icc_reset,
736 REGINFO_SENTINEL
740 * vm_change_state_handler - VM change state callback aiming at flushing
741 * RDIST pending tables into guest RAM
743 * The tables get flushed to guest RAM whenever the VM gets stopped.
745 static void vm_change_state_handler(void *opaque, bool running,
746 RunState state)
748 GICv3State *s = (GICv3State *)opaque;
749 Error *err = NULL;
750 int ret;
752 if (running) {
753 return;
756 ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
757 KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
758 NULL, true, &err);
759 if (err) {
760 error_report_err(err);
762 if (ret < 0 && ret != -EFAULT) {
763 abort();
768 static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
770 GICv3State *s = KVM_ARM_GICV3(dev);
771 KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
772 bool multiple_redist_region_allowed;
773 Error *local_err = NULL;
774 int i;
776 DPRINTF("kvm_arm_gicv3_realize\n");
778 kgc->parent_realize(dev, &local_err);
779 if (local_err) {
780 error_propagate(errp, local_err);
781 return;
784 if (s->security_extn) {
785 error_setg(errp, "the in-kernel VGICv3 does not implement the "
786 "security extensions");
787 return;
790 gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL, &local_err);
791 if (local_err) {
792 error_propagate(errp, local_err);
793 return;
796 for (i = 0; i < s->num_cpu; i++) {
797 ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
799 define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
802 /* Try to create the device via the device control API */
803 s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
804 if (s->dev_fd < 0) {
805 error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
806 return;
809 multiple_redist_region_allowed =
810 kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
811 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION);
813 if (!multiple_redist_region_allowed && s->nb_redist_regions > 1) {
814 error_setg(errp, "Multiple VGICv3 redistributor regions are not "
815 "supported by this host kernel");
816 error_append_hint(errp, "A maximum of %d VCPUs can be used",
817 s->redist_region_count[0]);
818 return;
821 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
822 0, &s->num_irq, true, &error_abort);
824 /* Tell the kernel to complete VGIC initialization now */
825 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
826 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
828 kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
829 KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd, 0);
831 if (!multiple_redist_region_allowed) {
832 kvm_arm_register_device(&s->iomem_redist[0], -1,
833 KVM_DEV_ARM_VGIC_GRP_ADDR,
834 KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd, 0);
835 } else {
836 /* we register regions in reverse order as "devices" are inserted at
837 * the head of a QSLIST and the list is then popped from the head
838 * onwards by kvm_arm_machine_init_done()
840 for (i = s->nb_redist_regions - 1; i >= 0; i--) {
841 /* Address mask made of the rdist region index and count */
842 uint64_t addr_ormask =
843 i | ((uint64_t)s->redist_region_count[i] << 52);
845 kvm_arm_register_device(&s->iomem_redist[i], -1,
846 KVM_DEV_ARM_VGIC_GRP_ADDR,
847 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION,
848 s->dev_fd, addr_ormask);
852 if (kvm_has_gsi_routing()) {
853 /* set up irq routing */
854 for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
855 kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
858 kvm_gsi_routing_allowed = true;
860 kvm_irqchip_commit_routes(kvm_state);
863 if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
864 GICD_CTLR)) {
865 error_setg(&s->migration_blocker, "This operating system kernel does "
866 "not support vGICv3 migration");
867 if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
868 error_free(s->migration_blocker);
869 return;
872 if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
873 KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
874 qemu_add_vm_change_state_handler(vm_change_state_handler, s);
878 static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
880 DeviceClass *dc = DEVICE_CLASS(klass);
881 ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
882 KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
884 agcc->pre_save = kvm_arm_gicv3_get;
885 agcc->post_load = kvm_arm_gicv3_put;
886 device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
887 &kgc->parent_realize);
888 device_class_set_parent_reset(dc, kvm_arm_gicv3_reset, &kgc->parent_reset);
891 static const TypeInfo kvm_arm_gicv3_info = {
892 .name = TYPE_KVM_ARM_GICV3,
893 .parent = TYPE_ARM_GICV3_COMMON,
894 .instance_size = sizeof(GICv3State),
895 .class_init = kvm_arm_gicv3_class_init,
896 .class_size = sizeof(KVMARMGICv3Class),
899 static void kvm_arm_gicv3_register_types(void)
901 type_register_static(&kvm_arm_gicv3_info);
904 type_init(kvm_arm_gicv3_register_types)