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