target-i386: Return runnability information on query-cpu-definitions
[qemu.git] / hw / intc / arm_gic_kvm.c
blobae7ac58ffd4bfd4a4aba13e3abeb00e513c39abb
1 /*
2 * ARM Generic Interrupt Controller using KVM in-kernel support
4 * Copyright (c) 2012 Linaro Limited
5 * Written by Peter Maydell
6 * Save/Restore logic added by Christoffer Dall.
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 "qemu-common.h"
25 #include "cpu.h"
26 #include "hw/sysbus.h"
27 #include "migration/migration.h"
28 #include "sysemu/kvm.h"
29 #include "kvm_arm.h"
30 #include "gic_internal.h"
31 #include "vgic_common.h"
33 //#define DEBUG_GIC_KVM
35 #ifdef DEBUG_GIC_KVM
36 static const int debug_gic_kvm = 1;
37 #else
38 static const int debug_gic_kvm = 0;
39 #endif
41 #define DPRINTF(fmt, ...) do { \
42 if (debug_gic_kvm) { \
43 printf("arm_gic: " fmt , ## __VA_ARGS__); \
44 } \
45 } while (0)
47 #define TYPE_KVM_ARM_GIC "kvm-arm-gic"
48 #define KVM_ARM_GIC(obj) \
49 OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC)
50 #define KVM_ARM_GIC_CLASS(klass) \
51 OBJECT_CLASS_CHECK(KVMARMGICClass, (klass), TYPE_KVM_ARM_GIC)
52 #define KVM_ARM_GIC_GET_CLASS(obj) \
53 OBJECT_GET_CLASS(KVMARMGICClass, (obj), TYPE_KVM_ARM_GIC)
55 typedef struct KVMARMGICClass {
56 ARMGICCommonClass parent_class;
57 DeviceRealize parent_realize;
58 void (*parent_reset)(DeviceState *dev);
59 } KVMARMGICClass;
61 void kvm_arm_gic_set_irq(uint32_t num_irq, int irq, int level)
63 /* Meaning of the 'irq' parameter:
64 * [0..N-1] : external interrupts
65 * [N..N+31] : PPI (internal) interrupts for CPU 0
66 * [N+32..N+63] : PPI (internal interrupts for CPU 1
67 * ...
68 * Convert this to the kernel's desired encoding, which
69 * has separate fields in the irq number for type,
70 * CPU number and interrupt number.
72 int kvm_irq, irqtype, cpu;
74 if (irq < (num_irq - GIC_INTERNAL)) {
75 /* External interrupt. The kernel numbers these like the GIC
76 * hardware, with external interrupt IDs starting after the
77 * internal ones.
79 irqtype = KVM_ARM_IRQ_TYPE_SPI;
80 cpu = 0;
81 irq += GIC_INTERNAL;
82 } else {
83 /* Internal interrupt: decode into (cpu, interrupt id) */
84 irqtype = KVM_ARM_IRQ_TYPE_PPI;
85 irq -= (num_irq - GIC_INTERNAL);
86 cpu = irq / GIC_INTERNAL;
87 irq %= GIC_INTERNAL;
89 kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT)
90 | (cpu << KVM_ARM_IRQ_VCPU_SHIFT) | irq;
92 kvm_set_irq(kvm_state, kvm_irq, !!level);
95 static void kvm_arm_gicv2_set_irq(void *opaque, int irq, int level)
97 GICState *s = (GICState *)opaque;
99 kvm_arm_gic_set_irq(s->num_irq, irq, level);
102 static bool kvm_arm_gic_can_save_restore(GICState *s)
104 return s->dev_fd >= 0;
107 #define KVM_VGIC_ATTR(offset, cpu) \
108 ((((uint64_t)(cpu) << KVM_DEV_ARM_VGIC_CPUID_SHIFT) & \
109 KVM_DEV_ARM_VGIC_CPUID_MASK) | \
110 (((uint64_t)(offset) << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) & \
111 KVM_DEV_ARM_VGIC_OFFSET_MASK))
113 static void kvm_gicd_access(GICState *s, int offset, int cpu,
114 uint32_t *val, bool write)
116 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
117 KVM_VGIC_ATTR(offset, cpu), val, write);
120 static void kvm_gicc_access(GICState *s, int offset, int cpu,
121 uint32_t *val, bool write)
123 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
124 KVM_VGIC_ATTR(offset, cpu), val, write);
127 #define for_each_irq_reg(_ctr, _max_irq, _field_width) \
128 for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++)
131 * Translate from the in-kernel field for an IRQ value to/from the qemu
132 * representation.
134 typedef void (*vgic_translate_fn)(GICState *s, int irq, int cpu,
135 uint32_t *field, bool to_kernel);
137 /* synthetic translate function used for clear/set registers to completely
138 * clear a setting using a clear-register before setting the remaining bits
139 * using a set-register */
140 static void translate_clear(GICState *s, int irq, int cpu,
141 uint32_t *field, bool to_kernel)
143 if (to_kernel) {
144 *field = ~0;
145 } else {
146 /* does not make sense: qemu model doesn't use set/clear regs */
147 abort();
151 static void translate_group(GICState *s, int irq, int cpu,
152 uint32_t *field, bool to_kernel)
154 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
156 if (to_kernel) {
157 *field = GIC_TEST_GROUP(irq, cm);
158 } else {
159 if (*field & 1) {
160 GIC_SET_GROUP(irq, cm);
165 static void translate_enabled(GICState *s, int irq, int cpu,
166 uint32_t *field, bool to_kernel)
168 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
170 if (to_kernel) {
171 *field = GIC_TEST_ENABLED(irq, cm);
172 } else {
173 if (*field & 1) {
174 GIC_SET_ENABLED(irq, cm);
179 static void translate_pending(GICState *s, int irq, int cpu,
180 uint32_t *field, bool to_kernel)
182 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
184 if (to_kernel) {
185 *field = gic_test_pending(s, irq, cm);
186 } else {
187 if (*field & 1) {
188 GIC_SET_PENDING(irq, cm);
189 /* TODO: Capture is level-line is held high in the kernel */
194 static void translate_active(GICState *s, int irq, int cpu,
195 uint32_t *field, bool to_kernel)
197 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
199 if (to_kernel) {
200 *field = GIC_TEST_ACTIVE(irq, cm);
201 } else {
202 if (*field & 1) {
203 GIC_SET_ACTIVE(irq, cm);
208 static void translate_trigger(GICState *s, int irq, int cpu,
209 uint32_t *field, bool to_kernel)
211 if (to_kernel) {
212 *field = (GIC_TEST_EDGE_TRIGGER(irq)) ? 0x2 : 0x0;
213 } else {
214 if (*field & 0x2) {
215 GIC_SET_EDGE_TRIGGER(irq);
220 static void translate_priority(GICState *s, int irq, int cpu,
221 uint32_t *field, bool to_kernel)
223 if (to_kernel) {
224 *field = GIC_GET_PRIORITY(irq, cpu) & 0xff;
225 } else {
226 gic_set_priority(s, cpu, irq, *field & 0xff, MEMTXATTRS_UNSPECIFIED);
230 static void translate_targets(GICState *s, int irq, int cpu,
231 uint32_t *field, bool to_kernel)
233 if (to_kernel) {
234 *field = s->irq_target[irq] & 0xff;
235 } else {
236 s->irq_target[irq] = *field & 0xff;
240 static void translate_sgisource(GICState *s, int irq, int cpu,
241 uint32_t *field, bool to_kernel)
243 if (to_kernel) {
244 *field = s->sgi_pending[irq][cpu] & 0xff;
245 } else {
246 s->sgi_pending[irq][cpu] = *field & 0xff;
250 /* Read a register group from the kernel VGIC */
251 static void kvm_dist_get(GICState *s, uint32_t offset, int width,
252 int maxirq, vgic_translate_fn translate_fn)
254 uint32_t reg;
255 int i;
256 int j;
257 int irq;
258 int cpu;
259 int regsz = 32 / width; /* irqs per kernel register */
260 uint32_t field;
262 for_each_irq_reg(i, maxirq, width) {
263 irq = i * regsz;
264 cpu = 0;
265 while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
266 kvm_gicd_access(s, offset, cpu, &reg, false);
267 for (j = 0; j < regsz; j++) {
268 field = extract32(reg, j * width, width);
269 translate_fn(s, irq + j, cpu, &field, false);
272 cpu++;
274 offset += 4;
278 /* Write a register group to the kernel VGIC */
279 static void kvm_dist_put(GICState *s, uint32_t offset, int width,
280 int maxirq, vgic_translate_fn translate_fn)
282 uint32_t reg;
283 int i;
284 int j;
285 int irq;
286 int cpu;
287 int regsz = 32 / width; /* irqs per kernel register */
288 uint32_t field;
290 for_each_irq_reg(i, maxirq, width) {
291 irq = i * regsz;
292 cpu = 0;
293 while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
294 reg = 0;
295 for (j = 0; j < regsz; j++) {
296 translate_fn(s, irq + j, cpu, &field, true);
297 reg = deposit32(reg, j * width, width, field);
299 kvm_gicd_access(s, offset, cpu, &reg, true);
301 cpu++;
303 offset += 4;
307 static void kvm_arm_gic_put(GICState *s)
309 uint32_t reg;
310 int i;
311 int cpu;
312 int num_cpu;
313 int num_irq;
315 /* Note: We do the restore in a slightly different order than the save
316 * (where the order doesn't matter and is simply ordered according to the
317 * register offset values */
319 /*****************************************************************
320 * Distributor State
323 /* s->ctlr -> GICD_CTLR */
324 reg = s->ctlr;
325 kvm_gicd_access(s, 0x0, 0, &reg, true);
327 /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
328 kvm_gicd_access(s, 0x4, 0, &reg, false);
329 num_irq = ((reg & 0x1f) + 1) * 32;
330 num_cpu = ((reg & 0xe0) >> 5) + 1;
332 if (num_irq < s->num_irq) {
333 fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
334 s->num_irq, num_irq);
335 abort();
336 } else if (num_cpu != s->num_cpu) {
337 fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
338 s->num_cpu, num_cpu);
339 /* Did we not create the VCPUs in the kernel yet? */
340 abort();
343 /* TODO: Consider checking compatibility with the IIDR ? */
345 /* irq_state[n].enabled -> GICD_ISENABLERn */
346 kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
347 kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
349 /* irq_state[n].group -> GICD_IGROUPRn */
350 kvm_dist_put(s, 0x80, 1, s->num_irq, translate_group);
352 /* s->irq_target[irq] -> GICD_ITARGETSRn
353 * (restore targets before pending to ensure the pending state is set on
354 * the appropriate CPU interfaces in the kernel) */
355 kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
357 /* irq_state[n].trigger -> GICD_ICFGRn
358 * (restore configuration registers before pending IRQs so we treat
359 * level/edge correctly) */
360 kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
362 /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
363 kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
364 kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
366 /* irq_state[n].active -> GICD_ISACTIVERn */
367 kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
368 kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
371 /* s->priorityX[irq] -> ICD_IPRIORITYRn */
372 kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
374 /* s->sgi_pending -> ICD_CPENDSGIRn */
375 kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
376 kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);
379 /*****************************************************************
380 * CPU Interface(s) State
383 for (cpu = 0; cpu < s->num_cpu; cpu++) {
384 /* s->cpu_ctlr[cpu] -> GICC_CTLR */
385 reg = s->cpu_ctlr[cpu];
386 kvm_gicc_access(s, 0x00, cpu, &reg, true);
388 /* s->priority_mask[cpu] -> GICC_PMR */
389 reg = (s->priority_mask[cpu] & 0xff);
390 kvm_gicc_access(s, 0x04, cpu, &reg, true);
392 /* s->bpr[cpu] -> GICC_BPR */
393 reg = (s->bpr[cpu] & 0x7);
394 kvm_gicc_access(s, 0x08, cpu, &reg, true);
396 /* s->abpr[cpu] -> GICC_ABPR */
397 reg = (s->abpr[cpu] & 0x7);
398 kvm_gicc_access(s, 0x1c, cpu, &reg, true);
400 /* s->apr[n][cpu] -> GICC_APRn */
401 for (i = 0; i < 4; i++) {
402 reg = s->apr[i][cpu];
403 kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, true);
408 static void kvm_arm_gic_get(GICState *s)
410 uint32_t reg;
411 int i;
412 int cpu;
414 /*****************************************************************
415 * Distributor State
418 /* GICD_CTLR -> s->ctlr */
419 kvm_gicd_access(s, 0x0, 0, &reg, false);
420 s->ctlr = reg;
422 /* Sanity checking on GICD_TYPER -> s->num_irq, s->num_cpu */
423 kvm_gicd_access(s, 0x4, 0, &reg, false);
424 s->num_irq = ((reg & 0x1f) + 1) * 32;
425 s->num_cpu = ((reg & 0xe0) >> 5) + 1;
427 if (s->num_irq > GIC_MAXIRQ) {
428 fprintf(stderr, "Too many IRQs reported from the kernel: %d\n",
429 s->num_irq);
430 abort();
433 /* GICD_IIDR -> ? */
434 kvm_gicd_access(s, 0x8, 0, &reg, false);
436 /* Clear all the IRQ settings */
437 for (i = 0; i < s->num_irq; i++) {
438 memset(&s->irq_state[i], 0, sizeof(s->irq_state[0]));
441 /* GICD_IGROUPRn -> irq_state[n].group */
442 kvm_dist_get(s, 0x80, 1, s->num_irq, translate_group);
444 /* GICD_ISENABLERn -> irq_state[n].enabled */
445 kvm_dist_get(s, 0x100, 1, s->num_irq, translate_enabled);
447 /* GICD_ISPENDRn -> irq_state[n].pending + irq_state[n].level */
448 kvm_dist_get(s, 0x200, 1, s->num_irq, translate_pending);
450 /* GICD_ISACTIVERn -> irq_state[n].active */
451 kvm_dist_get(s, 0x300, 1, s->num_irq, translate_active);
453 /* GICD_ICFRn -> irq_state[n].trigger */
454 kvm_dist_get(s, 0xc00, 2, s->num_irq, translate_trigger);
456 /* GICD_IPRIORITYRn -> s->priorityX[irq] */
457 kvm_dist_get(s, 0x400, 8, s->num_irq, translate_priority);
459 /* GICD_ITARGETSRn -> s->irq_target[irq] */
460 kvm_dist_get(s, 0x800, 8, s->num_irq, translate_targets);
462 /* GICD_CPENDSGIRn -> s->sgi_pending */
463 kvm_dist_get(s, 0xf10, 8, GIC_NR_SGIS, translate_sgisource);
466 /*****************************************************************
467 * CPU Interface(s) State
470 for (cpu = 0; cpu < s->num_cpu; cpu++) {
471 /* GICC_CTLR -> s->cpu_ctlr[cpu] */
472 kvm_gicc_access(s, 0x00, cpu, &reg, false);
473 s->cpu_ctlr[cpu] = reg;
475 /* GICC_PMR -> s->priority_mask[cpu] */
476 kvm_gicc_access(s, 0x04, cpu, &reg, false);
477 s->priority_mask[cpu] = (reg & 0xff);
479 /* GICC_BPR -> s->bpr[cpu] */
480 kvm_gicc_access(s, 0x08, cpu, &reg, false);
481 s->bpr[cpu] = (reg & 0x7);
483 /* GICC_ABPR -> s->abpr[cpu] */
484 kvm_gicc_access(s, 0x1c, cpu, &reg, false);
485 s->abpr[cpu] = (reg & 0x7);
487 /* GICC_APRn -> s->apr[n][cpu] */
488 for (i = 0; i < 4; i++) {
489 kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, false);
490 s->apr[i][cpu] = reg;
495 static void kvm_arm_gic_reset(DeviceState *dev)
497 GICState *s = ARM_GIC_COMMON(dev);
498 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
500 kgc->parent_reset(dev);
502 if (kvm_arm_gic_can_save_restore(s)) {
503 kvm_arm_gic_put(s);
507 static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
509 int i;
510 GICState *s = KVM_ARM_GIC(dev);
511 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
512 Error *local_err = NULL;
513 int ret;
515 kgc->parent_realize(dev, &local_err);
516 if (local_err) {
517 error_propagate(errp, local_err);
518 return;
521 if (s->security_extn) {
522 error_setg(errp, "the in-kernel VGIC does not implement the "
523 "security extensions");
524 return;
527 gic_init_irqs_and_mmio(s, kvm_arm_gicv2_set_irq, NULL);
529 for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
530 qemu_irq irq = qdev_get_gpio_in(dev, i);
531 kvm_irqchip_set_qemuirq_gsi(kvm_state, irq, i);
534 /* Try to create the device via the device control API */
535 s->dev_fd = -1;
536 ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false);
537 if (ret >= 0) {
538 s->dev_fd = ret;
540 /* Newstyle API is used, we may have attributes */
541 if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0)) {
542 uint32_t numirqs = s->num_irq;
543 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0,
544 &numirqs, true);
546 /* Tell the kernel to complete VGIC initialization now */
547 if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
548 KVM_DEV_ARM_VGIC_CTRL_INIT)) {
549 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
550 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true);
552 } else if (ret != -ENODEV && ret != -ENOTSUP) {
553 error_setg_errno(errp, -ret, "error creating in-kernel VGIC");
554 return;
557 /* Distributor */
558 kvm_arm_register_device(&s->iomem,
559 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
560 | KVM_VGIC_V2_ADDR_TYPE_DIST,
561 KVM_DEV_ARM_VGIC_GRP_ADDR,
562 KVM_VGIC_V2_ADDR_TYPE_DIST,
563 s->dev_fd);
564 /* CPU interface for current core. Unlike arm_gic, we don't
565 * provide the "interface for core #N" memory regions, because
566 * cores with a VGIC don't have those.
568 kvm_arm_register_device(&s->cpuiomem[0],
569 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
570 | KVM_VGIC_V2_ADDR_TYPE_CPU,
571 KVM_DEV_ARM_VGIC_GRP_ADDR,
572 KVM_VGIC_V2_ADDR_TYPE_CPU,
573 s->dev_fd);
575 if (!kvm_arm_gic_can_save_restore(s)) {
576 error_setg(&s->migration_blocker, "This operating system kernel does "
577 "not support vGICv2 migration");
578 migrate_add_blocker(s->migration_blocker);
581 if (kvm_has_gsi_routing()) {
582 /* set up irq routing */
583 kvm_init_irq_routing(kvm_state);
584 for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
585 kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
588 kvm_gsi_routing_allowed = true;
590 kvm_irqchip_commit_routes(kvm_state);
594 static void kvm_arm_gic_class_init(ObjectClass *klass, void *data)
596 DeviceClass *dc = DEVICE_CLASS(klass);
597 ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass);
598 KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass);
600 agcc->pre_save = kvm_arm_gic_get;
601 agcc->post_load = kvm_arm_gic_put;
602 kgc->parent_realize = dc->realize;
603 kgc->parent_reset = dc->reset;
604 dc->realize = kvm_arm_gic_realize;
605 dc->reset = kvm_arm_gic_reset;
608 static const TypeInfo kvm_arm_gic_info = {
609 .name = TYPE_KVM_ARM_GIC,
610 .parent = TYPE_ARM_GIC_COMMON,
611 .instance_size = sizeof(GICState),
612 .class_init = kvm_arm_gic_class_init,
613 .class_size = sizeof(KVMARMGICClass),
616 static void kvm_arm_gic_register_types(void)
618 type_register_static(&kvm_arm_gic_info);
621 type_init(kvm_arm_gic_register_types)