1 /* 2 * ARM Generic Interrupt Controller using KVM in-kernel support 3 * 4 * Copyright (c) 2012 Linaro Limited 5 * Written by Peter Maydell 6 * Save/Restore logic added by Christoffer Dall. 7 * 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. 12 * 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. 17 * 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/>. 20 */ 21 22 #include "hw/sysbus.h" 23 #include "migration/migration.h" 24 #include "sysemu/kvm.h" 25 #include "kvm_arm.h" 26 #include "gic_internal.h" 27 #include "vgic_common.h" 28 29 //#define DEBUG_GIC_KVM 30 31 #ifdef DEBUG_GIC_KVM 32 static const int debug_gic_kvm = 1; 33 #else 34 static const int debug_gic_kvm = 0; 35 #endif 36 37 #define DPRINTF(fmt, ...) do { \ 38 if (debug_gic_kvm) { \ 39 printf("arm_gic: " fmt , ## __VA_ARGS__); \ 40 } \ 41 } while (0) 42 43 #define TYPE_KVM_ARM_GIC "kvm-arm-gic" 44 #define KVM_ARM_GIC(obj) \ 45 OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC) 46 #define KVM_ARM_GIC_CLASS(klass) \ 47 OBJECT_CLASS_CHECK(KVMARMGICClass, (klass), TYPE_KVM_ARM_GIC) 48 #define KVM_ARM_GIC_GET_CLASS(obj) \ 49 OBJECT_GET_CLASS(KVMARMGICClass, (obj), TYPE_KVM_ARM_GIC) 50 51 typedef struct KVMARMGICClass { 52 ARMGICCommonClass parent_class; 53 DeviceRealize parent_realize; 54 void (*parent_reset)(DeviceState *dev); 55 } KVMARMGICClass; 56 57 void kvm_arm_gic_set_irq(uint32_t num_irq, int irq, int level) 58 { 59 /* Meaning of the 'irq' parameter: 60 * [0..N-1] : external interrupts 61 * [N..N+31] : PPI (internal) interrupts for CPU 0 62 * [N+32..N+63] : PPI (internal interrupts for CPU 1 63 * ... 64 * Convert this to the kernel's desired encoding, which 65 * has separate fields in the irq number for type, 66 * CPU number and interrupt number. 67 */ 68 int kvm_irq, irqtype, cpu; 69 70 if (irq < (num_irq - GIC_INTERNAL)) { 71 /* External interrupt. The kernel numbers these like the GIC 72 * hardware, with external interrupt IDs starting after the 73 * internal ones. 74 */ 75 irqtype = KVM_ARM_IRQ_TYPE_SPI; 76 cpu = 0; 77 irq += GIC_INTERNAL; 78 } else { 79 /* Internal interrupt: decode into (cpu, interrupt id) */ 80 irqtype = KVM_ARM_IRQ_TYPE_PPI; 81 irq -= (num_irq - GIC_INTERNAL); 82 cpu = irq / GIC_INTERNAL; 83 irq %= GIC_INTERNAL; 84 } 85 kvm_irq = (irqtype << KVM_ARM_IRQ_TYPE_SHIFT) 86 | (cpu << KVM_ARM_IRQ_VCPU_SHIFT) | irq; 87 88 kvm_set_irq(kvm_state, kvm_irq, !!level); 89 } 90 91 static void kvm_arm_gicv2_set_irq(void *opaque, int irq, int level) 92 { 93 GICState *s = (GICState *)opaque; 94 95 kvm_arm_gic_set_irq(s->num_irq, irq, level); 96 } 97 98 static bool kvm_arm_gic_can_save_restore(GICState *s) 99 { 100 return s->dev_fd >= 0; 101 } 102 103 #define KVM_VGIC_ATTR(offset, cpu) \ 104 ((((uint64_t)(cpu) << KVM_DEV_ARM_VGIC_CPUID_SHIFT) & \ 105 KVM_DEV_ARM_VGIC_CPUID_MASK) | \ 106 (((uint64_t)(offset) << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) & \ 107 KVM_DEV_ARM_VGIC_OFFSET_MASK)) 108 109 static void kvm_gicd_access(GICState *s, int offset, int cpu, 110 uint32_t *val, bool write) 111 { 112 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, 113 KVM_VGIC_ATTR(offset, cpu), val, write); 114 } 115 116 static void kvm_gicc_access(GICState *s, int offset, int cpu, 117 uint32_t *val, bool write) 118 { 119 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS, 120 KVM_VGIC_ATTR(offset, cpu), val, write); 121 } 122 123 #define for_each_irq_reg(_ctr, _max_irq, _field_width) \ 124 for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++) 125 126 /* 127 * Translate from the in-kernel field for an IRQ value to/from the qemu 128 * representation. 129 */ 130 typedef void (*vgic_translate_fn)(GICState *s, int irq, int cpu, 131 uint32_t *field, bool to_kernel); 132 133 /* synthetic translate function used for clear/set registers to completely 134 * clear a setting using a clear-register before setting the remaining bits 135 * using a set-register */ 136 static void translate_clear(GICState *s, int irq, int cpu, 137 uint32_t *field, bool to_kernel) 138 { 139 if (to_kernel) { 140 *field = ~0; 141 } else { 142 /* does not make sense: qemu model doesn't use set/clear regs */ 143 abort(); 144 } 145 } 146 147 static void translate_group(GICState *s, int irq, int cpu, 148 uint32_t *field, bool to_kernel) 149 { 150 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 151 152 if (to_kernel) { 153 *field = GIC_TEST_GROUP(irq, cm); 154 } else { 155 if (*field & 1) { 156 GIC_SET_GROUP(irq, cm); 157 } 158 } 159 } 160 161 static void translate_enabled(GICState *s, int irq, int cpu, 162 uint32_t *field, bool to_kernel) 163 { 164 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 165 166 if (to_kernel) { 167 *field = GIC_TEST_ENABLED(irq, cm); 168 } else { 169 if (*field & 1) { 170 GIC_SET_ENABLED(irq, cm); 171 } 172 } 173 } 174 175 static void translate_pending(GICState *s, int irq, int cpu, 176 uint32_t *field, bool to_kernel) 177 { 178 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 179 180 if (to_kernel) { 181 *field = gic_test_pending(s, irq, cm); 182 } else { 183 if (*field & 1) { 184 GIC_SET_PENDING(irq, cm); 185 /* TODO: Capture is level-line is held high in the kernel */ 186 } 187 } 188 } 189 190 static void translate_active(GICState *s, int irq, int cpu, 191 uint32_t *field, bool to_kernel) 192 { 193 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK; 194 195 if (to_kernel) { 196 *field = GIC_TEST_ACTIVE(irq, cm); 197 } else { 198 if (*field & 1) { 199 GIC_SET_ACTIVE(irq, cm); 200 } 201 } 202 } 203 204 static void translate_trigger(GICState *s, int irq, int cpu, 205 uint32_t *field, bool to_kernel) 206 { 207 if (to_kernel) { 208 *field = (GIC_TEST_EDGE_TRIGGER(irq)) ? 0x2 : 0x0; 209 } else { 210 if (*field & 0x2) { 211 GIC_SET_EDGE_TRIGGER(irq); 212 } 213 } 214 } 215 216 static void translate_priority(GICState *s, int irq, int cpu, 217 uint32_t *field, bool to_kernel) 218 { 219 if (to_kernel) { 220 *field = GIC_GET_PRIORITY(irq, cpu) & 0xff; 221 } else { 222 gic_set_priority(s, cpu, irq, *field & 0xff, MEMTXATTRS_UNSPECIFIED); 223 } 224 } 225 226 static void translate_targets(GICState *s, int irq, int cpu, 227 uint32_t *field, bool to_kernel) 228 { 229 if (to_kernel) { 230 *field = s->irq_target[irq] & 0xff; 231 } else { 232 s->irq_target[irq] = *field & 0xff; 233 } 234 } 235 236 static void translate_sgisource(GICState *s, int irq, int cpu, 237 uint32_t *field, bool to_kernel) 238 { 239 if (to_kernel) { 240 *field = s->sgi_pending[irq][cpu] & 0xff; 241 } else { 242 s->sgi_pending[irq][cpu] = *field & 0xff; 243 } 244 } 245 246 /* Read a register group from the kernel VGIC */ 247 static void kvm_dist_get(GICState *s, uint32_t offset, int width, 248 int maxirq, vgic_translate_fn translate_fn) 249 { 250 uint32_t reg; 251 int i; 252 int j; 253 int irq; 254 int cpu; 255 int regsz = 32 / width; /* irqs per kernel register */ 256 uint32_t field; 257 258 for_each_irq_reg(i, maxirq, width) { 259 irq = i * regsz; 260 cpu = 0; 261 while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) { 262 kvm_gicd_access(s, offset, cpu, ®, false); 263 for (j = 0; j < regsz; j++) { 264 field = extract32(reg, j * width, width); 265 translate_fn(s, irq + j, cpu, &field, false); 266 } 267 268 cpu++; 269 } 270 offset += 4; 271 } 272 } 273 274 /* Write a register group to the kernel VGIC */ 275 static void kvm_dist_put(GICState *s, uint32_t offset, int width, 276 int maxirq, vgic_translate_fn translate_fn) 277 { 278 uint32_t reg; 279 int i; 280 int j; 281 int irq; 282 int cpu; 283 int regsz = 32 / width; /* irqs per kernel register */ 284 uint32_t field; 285 286 for_each_irq_reg(i, maxirq, width) { 287 irq = i * regsz; 288 cpu = 0; 289 while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) { 290 reg = 0; 291 for (j = 0; j < regsz; j++) { 292 translate_fn(s, irq + j, cpu, &field, true); 293 reg = deposit32(reg, j * width, width, field); 294 } 295 kvm_gicd_access(s, offset, cpu, ®, true); 296 297 cpu++; 298 } 299 offset += 4; 300 } 301 } 302 303 static void kvm_arm_gic_put(GICState *s) 304 { 305 uint32_t reg; 306 int i; 307 int cpu; 308 int num_cpu; 309 int num_irq; 310 311 /* Note: We do the restore in a slightly different order than the save 312 * (where the order doesn't matter and is simply ordered according to the 313 * register offset values */ 314 315 /***************************************************************** 316 * Distributor State 317 */ 318 319 /* s->ctlr -> GICD_CTLR */ 320 reg = s->ctlr; 321 kvm_gicd_access(s, 0x0, 0, ®, true); 322 323 /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */ 324 kvm_gicd_access(s, 0x4, 0, ®, false); 325 num_irq = ((reg & 0x1f) + 1) * 32; 326 num_cpu = ((reg & 0xe0) >> 5) + 1; 327 328 if (num_irq < s->num_irq) { 329 fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n", 330 s->num_irq, num_irq); 331 abort(); 332 } else if (num_cpu != s->num_cpu) { 333 fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n", 334 s->num_cpu, num_cpu); 335 /* Did we not create the VCPUs in the kernel yet? */ 336 abort(); 337 } 338 339 /* TODO: Consider checking compatibility with the IIDR ? */ 340 341 /* irq_state[n].enabled -> GICD_ISENABLERn */ 342 kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear); 343 kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled); 344 345 /* irq_state[n].group -> GICD_IGROUPRn */ 346 kvm_dist_put(s, 0x80, 1, s->num_irq, translate_group); 347 348 /* s->irq_target[irq] -> GICD_ITARGETSRn 349 * (restore targets before pending to ensure the pending state is set on 350 * the appropriate CPU interfaces in the kernel) */ 351 kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets); 352 353 /* irq_state[n].trigger -> GICD_ICFGRn 354 * (restore configuration registers before pending IRQs so we treat 355 * level/edge correctly) */ 356 kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger); 357 358 /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */ 359 kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear); 360 kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending); 361 362 /* irq_state[n].active -> GICD_ISACTIVERn */ 363 kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear); 364 kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active); 365 366 367 /* s->priorityX[irq] -> ICD_IPRIORITYRn */ 368 kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority); 369 370 /* s->sgi_pending -> ICD_CPENDSGIRn */ 371 kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear); 372 kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource); 373 374 375 /***************************************************************** 376 * CPU Interface(s) State 377 */ 378 379 for (cpu = 0; cpu < s->num_cpu; cpu++) { 380 /* s->cpu_ctlr[cpu] -> GICC_CTLR */ 381 reg = s->cpu_ctlr[cpu]; 382 kvm_gicc_access(s, 0x00, cpu, ®, true); 383 384 /* s->priority_mask[cpu] -> GICC_PMR */ 385 reg = (s->priority_mask[cpu] & 0xff); 386 kvm_gicc_access(s, 0x04, cpu, ®, true); 387 388 /* s->bpr[cpu] -> GICC_BPR */ 389 reg = (s->bpr[cpu] & 0x7); 390 kvm_gicc_access(s, 0x08, cpu, ®, true); 391 392 /* s->abpr[cpu] -> GICC_ABPR */ 393 reg = (s->abpr[cpu] & 0x7); 394 kvm_gicc_access(s, 0x1c, cpu, ®, true); 395 396 /* s->apr[n][cpu] -> GICC_APRn */ 397 for (i = 0; i < 4; i++) { 398 reg = s->apr[i][cpu]; 399 kvm_gicc_access(s, 0xd0 + i * 4, cpu, ®, true); 400 } 401 } 402 } 403 404 static void kvm_arm_gic_get(GICState *s) 405 { 406 uint32_t reg; 407 int i; 408 int cpu; 409 410 /***************************************************************** 411 * Distributor State 412 */ 413 414 /* GICD_CTLR -> s->ctlr */ 415 kvm_gicd_access(s, 0x0, 0, ®, false); 416 s->ctlr = reg; 417 418 /* Sanity checking on GICD_TYPER -> s->num_irq, s->num_cpu */ 419 kvm_gicd_access(s, 0x4, 0, ®, false); 420 s->num_irq = ((reg & 0x1f) + 1) * 32; 421 s->num_cpu = ((reg & 0xe0) >> 5) + 1; 422 423 if (s->num_irq > GIC_MAXIRQ) { 424 fprintf(stderr, "Too many IRQs reported from the kernel: %d\n", 425 s->num_irq); 426 abort(); 427 } 428 429 /* GICD_IIDR -> ? */ 430 kvm_gicd_access(s, 0x8, 0, ®, false); 431 432 /* Clear all the IRQ settings */ 433 for (i = 0; i < s->num_irq; i++) { 434 memset(&s->irq_state[i], 0, sizeof(s->irq_state[0])); 435 } 436 437 /* GICD_IGROUPRn -> irq_state[n].group */ 438 kvm_dist_get(s, 0x80, 1, s->num_irq, translate_group); 439 440 /* GICD_ISENABLERn -> irq_state[n].enabled */ 441 kvm_dist_get(s, 0x100, 1, s->num_irq, translate_enabled); 442 443 /* GICD_ISPENDRn -> irq_state[n].pending + irq_state[n].level */ 444 kvm_dist_get(s, 0x200, 1, s->num_irq, translate_pending); 445 446 /* GICD_ISACTIVERn -> irq_state[n].active */ 447 kvm_dist_get(s, 0x300, 1, s->num_irq, translate_active); 448 449 /* GICD_ICFRn -> irq_state[n].trigger */ 450 kvm_dist_get(s, 0xc00, 2, s->num_irq, translate_trigger); 451 452 /* GICD_IPRIORITYRn -> s->priorityX[irq] */ 453 kvm_dist_get(s, 0x400, 8, s->num_irq, translate_priority); 454 455 /* GICD_ITARGETSRn -> s->irq_target[irq] */ 456 kvm_dist_get(s, 0x800, 8, s->num_irq, translate_targets); 457 458 /* GICD_CPENDSGIRn -> s->sgi_pending */ 459 kvm_dist_get(s, 0xf10, 8, GIC_NR_SGIS, translate_sgisource); 460 461 462 /***************************************************************** 463 * CPU Interface(s) State 464 */ 465 466 for (cpu = 0; cpu < s->num_cpu; cpu++) { 467 /* GICC_CTLR -> s->cpu_ctlr[cpu] */ 468 kvm_gicc_access(s, 0x00, cpu, ®, false); 469 s->cpu_ctlr[cpu] = reg; 470 471 /* GICC_PMR -> s->priority_mask[cpu] */ 472 kvm_gicc_access(s, 0x04, cpu, ®, false); 473 s->priority_mask[cpu] = (reg & 0xff); 474 475 /* GICC_BPR -> s->bpr[cpu] */ 476 kvm_gicc_access(s, 0x08, cpu, ®, false); 477 s->bpr[cpu] = (reg & 0x7); 478 479 /* GICC_ABPR -> s->abpr[cpu] */ 480 kvm_gicc_access(s, 0x1c, cpu, ®, false); 481 s->abpr[cpu] = (reg & 0x7); 482 483 /* GICC_APRn -> s->apr[n][cpu] */ 484 for (i = 0; i < 4; i++) { 485 kvm_gicc_access(s, 0xd0 + i * 4, cpu, ®, false); 486 s->apr[i][cpu] = reg; 487 } 488 } 489 } 490 491 static void kvm_arm_gic_reset(DeviceState *dev) 492 { 493 GICState *s = ARM_GIC_COMMON(dev); 494 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s); 495 496 kgc->parent_reset(dev); 497 498 if (kvm_arm_gic_can_save_restore(s)) { 499 kvm_arm_gic_put(s); 500 } 501 } 502 503 static void kvm_arm_gic_realize(DeviceState *dev, Error **errp) 504 { 505 int i; 506 GICState *s = KVM_ARM_GIC(dev); 507 KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s); 508 Error *local_err = NULL; 509 int ret; 510 511 kgc->parent_realize(dev, &local_err); 512 if (local_err) { 513 error_propagate(errp, local_err); 514 return; 515 } 516 517 if (s->security_extn) { 518 error_setg(errp, "the in-kernel VGIC does not implement the " 519 "security extensions"); 520 return; 521 } 522 523 gic_init_irqs_and_mmio(s, kvm_arm_gicv2_set_irq, NULL); 524 525 for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) { 526 qemu_irq irq = qdev_get_gpio_in(dev, i); 527 kvm_irqchip_set_qemuirq_gsi(kvm_state, irq, i); 528 } 529 530 /* Try to create the device via the device control API */ 531 s->dev_fd = -1; 532 ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false); 533 if (ret >= 0) { 534 s->dev_fd = ret; 535 536 /* Newstyle API is used, we may have attributes */ 537 if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0)) { 538 uint32_t numirqs = s->num_irq; 539 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0, 540 &numirqs, true); 541 } 542 /* Tell the kernel to complete VGIC initialization now */ 543 if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 544 KVM_DEV_ARM_VGIC_CTRL_INIT)) { 545 kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 546 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true); 547 } 548 } else if (ret != -ENODEV && ret != -ENOTSUP) { 549 error_setg_errno(errp, -ret, "error creating in-kernel VGIC"); 550 return; 551 } 552 553 /* Distributor */ 554 kvm_arm_register_device(&s->iomem, 555 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT) 556 | KVM_VGIC_V2_ADDR_TYPE_DIST, 557 KVM_DEV_ARM_VGIC_GRP_ADDR, 558 KVM_VGIC_V2_ADDR_TYPE_DIST, 559 s->dev_fd); 560 /* CPU interface for current core. Unlike arm_gic, we don't 561 * provide the "interface for core #N" memory regions, because 562 * cores with a VGIC don't have those. 563 */ 564 kvm_arm_register_device(&s->cpuiomem[0], 565 (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT) 566 | KVM_VGIC_V2_ADDR_TYPE_CPU, 567 KVM_DEV_ARM_VGIC_GRP_ADDR, 568 KVM_VGIC_V2_ADDR_TYPE_CPU, 569 s->dev_fd); 570 571 if (!kvm_arm_gic_can_save_restore(s)) { 572 error_setg(&s->migration_blocker, "This operating system kernel does " 573 "not support vGICv2 migration"); 574 migrate_add_blocker(s->migration_blocker); 575 } 576 } 577 578 static void kvm_arm_gic_class_init(ObjectClass *klass, void *data) 579 { 580 DeviceClass *dc = DEVICE_CLASS(klass); 581 ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass); 582 KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass); 583 584 agcc->pre_save = kvm_arm_gic_get; 585 agcc->post_load = kvm_arm_gic_put; 586 kgc->parent_realize = dc->realize; 587 kgc->parent_reset = dc->reset; 588 dc->realize = kvm_arm_gic_realize; 589 dc->reset = kvm_arm_gic_reset; 590 } 591 592 static const TypeInfo kvm_arm_gic_info = { 593 .name = TYPE_KVM_ARM_GIC, 594 .parent = TYPE_ARM_GIC_COMMON, 595 .instance_size = sizeof(GICState), 596 .class_init = kvm_arm_gic_class_init, 597 .class_size = sizeof(KVMARMGICClass), 598 }; 599 600 static void kvm_arm_gic_register_types(void) 601 { 602 type_register_static(&kvm_arm_gic_info); 603 } 604 605 type_init(kvm_arm_gic_register_types) 606