1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015, 2016 ARM Ltd. 4 */ 5 6 #include <linux/uaccess.h> 7 #include <linux/interrupt.h> 8 #include <linux/cpu.h> 9 #include <linux/kvm_host.h> 10 #include <kvm/arm_vgic.h> 11 #include <asm/kvm_emulate.h> 12 #include <asm/kvm_mmu.h> 13 #include "vgic.h" 14 15 /* 16 * Initialization rules: there are multiple stages to the vgic 17 * initialization, both for the distributor and the CPU interfaces. The basic 18 * idea is that even though the VGIC is not functional or not requested from 19 * user space, the critical path of the run loop can still call VGIC functions 20 * that just won't do anything, without them having to check additional 21 * initialization flags to ensure they don't look at uninitialized data 22 * structures. 23 * 24 * Distributor: 25 * 26 * - kvm_vgic_early_init(): initialization of static data that doesn't 27 * depend on any sizing information or emulation type. No allocation 28 * is allowed there. 29 * 30 * - vgic_init(): allocation and initialization of the generic data 31 * structures that depend on sizing information (number of CPUs, 32 * number of interrupts). Also initializes the vcpu specific data 33 * structures. Can be executed lazily for GICv2. 34 * 35 * CPU Interface: 36 * 37 * - kvm_vgic_vcpu_init(): initialization of static data that doesn't depend 38 * on any sizing information. Private interrupts are allocated if not 39 * already allocated at vgic-creation time. 40 */ 41 42 /* EARLY INIT */ 43 44 /** 45 * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures 46 * @kvm: The VM whose VGIC districutor should be initialized 47 * 48 * Only do initialization of static structures that don't require any 49 * allocation or sizing information from userspace. vgic_init() called 50 * kvm_vgic_dist_init() which takes care of the rest. 51 */ 52 void kvm_vgic_early_init(struct kvm *kvm) 53 { 54 struct vgic_dist *dist = &kvm->arch.vgic; 55 56 xa_init_flags(&dist->lpi_xa, XA_FLAGS_LOCK_IRQ); 57 } 58 59 /* CREATION */ 60 61 static int vgic_allocate_private_irqs_locked(struct kvm_vcpu *vcpu, u32 type); 62 63 /** 64 * kvm_vgic_create: triggered by the instantiation of the VGIC device by 65 * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only) 66 * or through the generic KVM_CREATE_DEVICE API ioctl. 67 * irqchip_in_kernel() tells you if this function succeeded or not. 68 * @kvm: kvm struct pointer 69 * @type: KVM_DEV_TYPE_ARM_VGIC_V[235] 70 */ 71 int kvm_vgic_create(struct kvm *kvm, u32 type) 72 { 73 struct kvm_vcpu *vcpu; 74 unsigned long i; 75 int ret; 76 77 /* 78 * This function is also called by the KVM_CREATE_IRQCHIP handler, 79 * which had no chance yet to check the availability of the GICv2 80 * emulation. So check this here again. KVM_CREATE_DEVICE does 81 * the proper checks already. 82 */ 83 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && 84 !kvm_vgic_global_state.can_emulate_gicv2) 85 return -ENODEV; 86 87 /* 88 * Ensure mutual exclusion with vCPU creation and any vCPU ioctls by: 89 * 90 * - Holding kvm->lock to prevent KVM_CREATE_VCPU from reaching 91 * kvm_arch_vcpu_precreate() and ensuring created_vcpus is stable. 92 * This alone is insufficient, as kvm_vm_ioctl_create_vcpu() drops 93 * the kvm->lock before completing the vCPU creation. 94 */ 95 lockdep_assert_held(&kvm->lock); 96 97 /* 98 * - Acquiring the vCPU mutex for every *online* vCPU to prevent 99 * concurrent vCPU ioctls for vCPUs already visible to userspace. 100 */ 101 ret = -EBUSY; 102 if (kvm_trylock_all_vcpus(kvm)) 103 return ret; 104 105 /* 106 * - Taking the config_lock which protects VGIC data structures such 107 * as the per-vCPU arrays of private IRQs (SGIs, PPIs). 108 */ 109 mutex_lock(&kvm->arch.config_lock); 110 111 /* 112 * - Bailing on the entire thing if a vCPU is in the middle of creation, 113 * dropped the kvm->lock, but hasn't reached kvm_arch_vcpu_create(). 114 * 115 * The whole combination of this guarantees that no vCPU can get into 116 * KVM with a VGIC configuration inconsistent with the VM's VGIC. 117 */ 118 if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus)) 119 goto out_unlock; 120 121 if (irqchip_in_kernel(kvm)) { 122 ret = -EEXIST; 123 goto out_unlock; 124 } 125 126 kvm_for_each_vcpu(i, vcpu, kvm) { 127 if (vcpu_has_run_once(vcpu)) 128 goto out_unlock; 129 } 130 ret = 0; 131 132 if (type == KVM_DEV_TYPE_ARM_VGIC_V2) 133 kvm->max_vcpus = VGIC_V2_MAX_CPUS; 134 else if (type == KVM_DEV_TYPE_ARM_VGIC_V3) 135 kvm->max_vcpus = VGIC_V3_MAX_CPUS; 136 else if (type == KVM_DEV_TYPE_ARM_VGIC_V5) 137 kvm->max_vcpus = min(VGIC_V5_MAX_CPUS, 138 kvm_vgic_global_state.max_gic_vcpus); 139 140 if (atomic_read(&kvm->online_vcpus) > kvm->max_vcpus) { 141 ret = -E2BIG; 142 goto out_unlock; 143 } 144 145 kvm->arch.vgic.in_kernel = true; 146 kvm->arch.vgic.vgic_model = type; 147 kvm->arch.vgic.implementation_rev = KVM_VGIC_IMP_REV_LATEST; 148 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; 149 150 switch (type) { 151 case KVM_DEV_TYPE_ARM_VGIC_V2: 152 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; 153 break; 154 case KVM_DEV_TYPE_ARM_VGIC_V3: 155 INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions); 156 break; 157 } 158 159 /* 160 * We've now created the GIC. Update the system register state 161 * to accurately reflect what we've created. 162 */ 163 kvm_vgic_finalize_idregs(kvm); 164 165 kvm_for_each_vcpu(i, vcpu, kvm) { 166 ret = vgic_allocate_private_irqs_locked(vcpu, type); 167 if (ret) 168 break; 169 } 170 171 if (ret) { 172 kvm_for_each_vcpu(i, vcpu, kvm) { 173 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 174 kfree(vgic_cpu->private_irqs); 175 vgic_cpu->private_irqs = NULL; 176 } 177 178 kvm->arch.vgic.vgic_model = 0; 179 kvm->arch.vgic.in_kernel = false; 180 goto out_unlock; 181 } 182 183 if (type == KVM_DEV_TYPE_ARM_VGIC_V3) 184 kvm->arch.vgic.nassgicap = system_supports_direct_sgis(); 185 186 /* 187 * We now know that we have a GICv5. The Arch Timer PPI interrupts may 188 * have been initialised at this stage, but will have done so assuming 189 * that we have an older GIC, meaning that the IntIDs won't be 190 * correct. We init them again, and this time they will be correct. 191 */ 192 if (type == KVM_DEV_TYPE_ARM_VGIC_V5) 193 kvm_timer_init_vm(kvm); 194 195 out_unlock: 196 mutex_unlock(&kvm->arch.config_lock); 197 kvm_unlock_all_vcpus(kvm); 198 return ret; 199 } 200 201 /* INIT/DESTROY */ 202 203 /** 204 * kvm_vgic_dist_init: initialize the dist data structures 205 * @kvm: kvm struct pointer 206 * @nr_spis: number of spis, frozen by caller 207 */ 208 static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis) 209 { 210 struct vgic_dist *dist = &kvm->arch.vgic; 211 struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0); 212 int i; 213 214 if (dist->spis) 215 return 0; 216 217 dist->active_spis = (atomic_t)ATOMIC_INIT(0); 218 dist->spis = kzalloc_objs(struct vgic_irq, nr_spis, GFP_KERNEL_ACCOUNT); 219 if (!dist->spis) 220 return -ENOMEM; 221 222 /* 223 * In the following code we do not take the irq struct lock since 224 * no other action on irq structs can happen while the VGIC is 225 * not initialized yet: 226 * If someone wants to inject an interrupt or does a MMIO access, we 227 * require prior initialization in case of a virtual GICv3 or trigger 228 * initialization when using a virtual GICv2. 229 */ 230 for (i = 0; i < nr_spis; i++) { 231 struct vgic_irq *irq = &dist->spis[i]; 232 233 irq->intid = i + VGIC_NR_PRIVATE_IRQS; 234 INIT_LIST_HEAD(&irq->ap_list); 235 raw_spin_lock_init(&irq->irq_lock); 236 irq->vcpu = NULL; 237 irq->target_vcpu = vcpu0; 238 refcount_set(&irq->refcount, 0); 239 switch (dist->vgic_model) { 240 case KVM_DEV_TYPE_ARM_VGIC_V2: 241 irq->targets = 0; 242 irq->group = 0; 243 break; 244 case KVM_DEV_TYPE_ARM_VGIC_V3: 245 irq->mpidr = 0; 246 irq->group = 1; 247 break; 248 default: 249 kfree(dist->spis); 250 dist->spis = NULL; 251 return -EINVAL; 252 } 253 } 254 return 0; 255 } 256 257 /* Default GICv3 Maintenance Interrupt INTID, as per SBSA */ 258 #define DEFAULT_MI_INTID 25 259 260 int kvm_vgic_vcpu_nv_init(struct kvm_vcpu *vcpu) 261 { 262 int ret; 263 264 guard(mutex)(&vcpu->kvm->arch.config_lock); 265 266 /* 267 * Matching the tradition established with the timers, provide 268 * a default PPI for the maintenance interrupt. It makes 269 * things easier to reason about. 270 */ 271 if (vcpu->kvm->arch.vgic.mi_intid == 0) 272 vcpu->kvm->arch.vgic.mi_intid = DEFAULT_MI_INTID; 273 ret = kvm_vgic_set_owner(vcpu, vcpu->kvm->arch.vgic.mi_intid, vcpu); 274 275 return ret; 276 } 277 278 static void vgic_setup_private_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq, 279 u32 type) 280 { 281 irq->intid = irq - &vcpu->arch.vgic_cpu.private_irqs[0]; 282 283 if (vgic_irq_is_sgi(irq->intid)) { 284 /* SGIs */ 285 irq->enabled = 1; 286 irq->config = VGIC_CONFIG_EDGE; 287 } else { 288 /* PPIs */ 289 irq->config = VGIC_CONFIG_LEVEL; 290 } 291 292 switch (type) { 293 case KVM_DEV_TYPE_ARM_VGIC_V3: 294 irq->group = 1; 295 irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu); 296 break; 297 case KVM_DEV_TYPE_ARM_VGIC_V2: 298 irq->group = 0; 299 irq->targets = BIT(vcpu->vcpu_id); 300 break; 301 } 302 } 303 304 static void vgic_v5_setup_private_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq) 305 { 306 int i = irq - &vcpu->arch.vgic_cpu.private_irqs[0]; 307 308 irq->intid = vgic_v5_make_ppi(i); 309 310 /* The only Edge architected PPI is the SW_PPI */ 311 if (i == GICV5_ARCH_PPI_SW_PPI) 312 irq->config = VGIC_CONFIG_EDGE; 313 else 314 irq->config = VGIC_CONFIG_LEVEL; 315 316 /* Register the GICv5-specific PPI ops */ 317 vgic_v5_set_ppi_ops(vcpu, irq->intid); 318 } 319 320 static int vgic_allocate_private_irqs_locked(struct kvm_vcpu *vcpu, u32 type) 321 { 322 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 323 u32 num_private_irqs; 324 int i; 325 326 lockdep_assert_held(&vcpu->kvm->arch.config_lock); 327 328 if (vgic_cpu->private_irqs) 329 return 0; 330 331 if (vgic_is_v5(vcpu->kvm)) 332 num_private_irqs = VGIC_V5_NR_PRIVATE_IRQS; 333 else 334 num_private_irqs = VGIC_NR_PRIVATE_IRQS; 335 336 vgic_cpu->private_irqs = kzalloc_objs(struct vgic_irq, 337 num_private_irqs, 338 GFP_KERNEL_ACCOUNT); 339 340 if (!vgic_cpu->private_irqs) 341 return -ENOMEM; 342 343 for (i = 0; i < num_private_irqs; i++) { 344 struct vgic_irq *irq = &vcpu->arch.vgic_cpu.private_irqs[i]; 345 346 INIT_LIST_HEAD(&irq->ap_list); 347 raw_spin_lock_init(&irq->irq_lock); 348 irq->vcpu = NULL; 349 irq->target_vcpu = vcpu; 350 refcount_set(&irq->refcount, 0); 351 352 if (vgic_is_v5(vcpu->kvm)) 353 vgic_v5_setup_private_irq(vcpu, irq); 354 else 355 vgic_setup_private_irq(vcpu, irq, type); 356 } 357 358 return 0; 359 } 360 361 static int vgic_allocate_private_irqs(struct kvm_vcpu *vcpu, u32 type) 362 { 363 int ret; 364 365 mutex_lock(&vcpu->kvm->arch.config_lock); 366 ret = vgic_allocate_private_irqs_locked(vcpu, type); 367 mutex_unlock(&vcpu->kvm->arch.config_lock); 368 369 return ret; 370 } 371 372 /** 373 * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data 374 * structures and register VCPU-specific KVM iodevs 375 * 376 * @vcpu: pointer to the VCPU being created and initialized 377 * 378 * Only do initialization, but do not actually enable the 379 * VGIC CPU interface 380 */ 381 int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) 382 { 383 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 384 struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 385 int ret = 0; 386 387 vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF; 388 389 INIT_LIST_HEAD(&vgic_cpu->ap_list_head); 390 raw_spin_lock_init(&vgic_cpu->ap_list_lock); 391 atomic_set(&vgic_cpu->vgic_v3.its_vpe.vlpi_count, 0); 392 393 if (!irqchip_in_kernel(vcpu->kvm)) 394 return 0; 395 396 ret = vgic_allocate_private_irqs(vcpu, dist->vgic_model); 397 if (ret) 398 return ret; 399 400 /* 401 * If we are creating a VCPU with a GICv3 we must also register the 402 * KVM io device for the redistributor that belongs to this VCPU. 403 */ 404 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { 405 mutex_lock(&vcpu->kvm->slots_lock); 406 ret = vgic_register_redist_iodev(vcpu); 407 mutex_unlock(&vcpu->kvm->slots_lock); 408 } 409 return ret; 410 } 411 412 static void kvm_vgic_vcpu_reset(struct kvm_vcpu *vcpu) 413 { 414 const struct vgic_dist *dist = &vcpu->kvm->arch.vgic; 415 416 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V5) 417 vgic_v5_reset(vcpu); 418 else if (kvm_vgic_global_state.type == VGIC_V2) 419 vgic_v2_reset(vcpu); 420 else 421 vgic_v3_reset(vcpu); 422 } 423 424 /* 425 * vgic_init: allocates and initializes dist and vcpu data structures 426 * depending on two dimensioning parameters: 427 * - the number of spis 428 * - the number of vcpus 429 * The function is generally called when nr_spis has been explicitly set 430 * by the guest through the KVM DEVICE API. If not nr_spis is set to 256. 431 * vgic_initialized() returns true when this function has succeeded. 432 */ 433 int vgic_init(struct kvm *kvm) 434 { 435 struct vgic_dist *dist = &kvm->arch.vgic; 436 struct kvm_vcpu *vcpu; 437 int ret = 0; 438 unsigned long idx; 439 440 lockdep_assert_held(&kvm->arch.config_lock); 441 442 if (vgic_initialized(kvm)) 443 return 0; 444 445 /* Are we also in the middle of creating a VCPU? */ 446 if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus)) 447 return -EBUSY; 448 449 if (!vgic_is_v5(kvm)) { 450 /* freeze the number of spis */ 451 if (!dist->nr_spis) 452 dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS; 453 454 ret = kvm_vgic_dist_init(kvm, dist->nr_spis); 455 if (ret) 456 return ret; 457 458 /* 459 * Ensure vPEs are allocated if direct IRQ injection (e.g. vSGIs, 460 * vLPIs) is supported. 461 */ 462 if (vgic_supports_direct_irqs(kvm)) { 463 ret = vgic_v4_init(kvm); 464 if (ret) 465 return ret; 466 } 467 } else { 468 ret = vgic_v5_init(kvm); 469 if (ret) 470 return ret; 471 } 472 473 kvm_for_each_vcpu(idx, vcpu, kvm) 474 kvm_vgic_vcpu_reset(vcpu); 475 476 ret = kvm_vgic_setup_default_irq_routing(kvm); 477 if (ret) 478 return ret; 479 480 vgic_debug_init(kvm); 481 dist->initialized = true; 482 483 return 0; 484 } 485 486 static void kvm_vgic_dist_destroy(struct kvm *kvm) 487 { 488 struct vgic_dist *dist = &kvm->arch.vgic; 489 struct vgic_redist_region *rdreg, *next; 490 491 dist->ready = false; 492 dist->initialized = false; 493 494 kfree(dist->spis); 495 dist->spis = NULL; 496 dist->nr_spis = 0; 497 dist->vgic_dist_base = VGIC_ADDR_UNDEF; 498 499 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { 500 list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) 501 vgic_v3_free_redist_region(kvm, rdreg); 502 INIT_LIST_HEAD(&dist->rd_regions); 503 } else { 504 dist->vgic_cpu_base = VGIC_ADDR_UNDEF; 505 } 506 507 if (vgic_supports_direct_irqs(kvm)) 508 vgic_v4_teardown(kvm); 509 510 xa_destroy(&dist->lpi_xa); 511 } 512 513 static void __kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) 514 { 515 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; 516 517 /* 518 * Retire all pending LPIs on this vcpu anyway as we're 519 * going to destroy it. 520 */ 521 vgic_flush_pending_lpis(vcpu); 522 523 INIT_LIST_HEAD(&vgic_cpu->ap_list_head); 524 kfree(vgic_cpu->private_irqs); 525 vgic_cpu->private_irqs = NULL; 526 527 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { 528 /* 529 * If this vCPU is being destroyed because of a failed creation 530 * then unregister the redistributor to avoid leaving behind a 531 * dangling pointer to the vCPU struct. 532 * 533 * vCPUs that have been successfully created (i.e. added to 534 * kvm->vcpu_array) get unregistered in kvm_vgic_destroy(), as 535 * this function gets called while holding kvm->arch.config_lock 536 * in the VM teardown path and would otherwise introduce a lock 537 * inversion w.r.t. kvm->srcu. 538 * 539 * vCPUs that failed creation are torn down outside of the 540 * kvm->arch.config_lock and do not get unregistered in 541 * kvm_vgic_destroy(), meaning it is both safe and necessary to 542 * do so here. 543 */ 544 if (kvm_get_vcpu_by_id(vcpu->kvm, vcpu->vcpu_id) != vcpu) 545 vgic_unregister_redist_iodev(vcpu); 546 547 vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF; 548 } 549 } 550 551 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) 552 { 553 struct kvm *kvm = vcpu->kvm; 554 555 mutex_lock(&kvm->slots_lock); 556 __kvm_vgic_vcpu_destroy(vcpu); 557 mutex_unlock(&kvm->slots_lock); 558 } 559 560 void kvm_vgic_destroy(struct kvm *kvm) 561 { 562 struct kvm_vcpu *vcpu; 563 unsigned long i; 564 565 mutex_lock(&kvm->slots_lock); 566 mutex_lock(&kvm->arch.config_lock); 567 568 vgic_debug_destroy(kvm); 569 570 kvm_for_each_vcpu(i, vcpu, kvm) 571 __kvm_vgic_vcpu_destroy(vcpu); 572 573 kvm_vgic_dist_destroy(kvm); 574 575 mutex_unlock(&kvm->arch.config_lock); 576 577 if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) 578 kvm_for_each_vcpu(i, vcpu, kvm) 579 vgic_unregister_redist_iodev(vcpu); 580 581 mutex_unlock(&kvm->slots_lock); 582 } 583 584 /** 585 * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest 586 * is a GICv2. A GICv3 must be explicitly initialized by userspace using the 587 * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group. 588 * @kvm: kvm struct pointer 589 */ 590 int vgic_lazy_init(struct kvm *kvm) 591 { 592 int ret = 0; 593 594 if (unlikely(!vgic_initialized(kvm))) { 595 /* 596 * We only provide the automatic initialization of the VGIC 597 * for the legacy case of a GICv2. Any other type must 598 * be explicitly initialized once setup with the respective 599 * KVM device call. 600 */ 601 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) 602 return -EBUSY; 603 604 mutex_lock(&kvm->arch.config_lock); 605 ret = vgic_init(kvm); 606 mutex_unlock(&kvm->arch.config_lock); 607 } 608 609 return ret; 610 } 611 612 /* RESOURCE MAPPING */ 613 614 /** 615 * kvm_vgic_map_resources - map the MMIO regions 616 * @kvm: kvm struct pointer 617 * 618 * Map the MMIO regions depending on the VGIC model exposed to the guest 619 * called on the first VCPU run. 620 * Also map the virtual CPU interface into the VM. 621 * v2 calls vgic_init() if not already done. 622 * v3 and derivatives return an error if the VGIC is not initialized. 623 */ 624 int kvm_vgic_map_resources(struct kvm *kvm) 625 { 626 struct vgic_dist *dist = &kvm->arch.vgic; 627 bool needs_dist = true; 628 enum vgic_type type; 629 gpa_t dist_base; 630 int ret = 0; 631 632 if (likely(smp_load_acquire(&dist->ready))) 633 return 0; 634 635 mutex_lock(&kvm->slots_lock); 636 mutex_lock(&kvm->arch.config_lock); 637 if (dist->ready) 638 goto out; 639 640 if (!irqchip_in_kernel(kvm)) 641 goto out; 642 643 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2) { 644 ret = vgic_v2_map_resources(kvm); 645 type = VGIC_V2; 646 } else if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) { 647 ret = vgic_v3_map_resources(kvm); 648 type = VGIC_V3; 649 } else { 650 ret = vgic_v5_map_resources(kvm); 651 type = VGIC_V5; 652 needs_dist = false; 653 } 654 655 if (ret) 656 goto out; 657 658 if (needs_dist) { 659 dist_base = dist->vgic_dist_base; 660 mutex_unlock(&kvm->arch.config_lock); 661 662 ret = vgic_register_dist_iodev(kvm, dist_base, type); 663 if (ret) { 664 kvm_err("Unable to register VGIC dist MMIO regions\n"); 665 goto out_slots; 666 } 667 } else { 668 mutex_unlock(&kvm->arch.config_lock); 669 } 670 671 smp_store_release(&dist->ready, true); 672 goto out_slots; 673 out: 674 mutex_unlock(&kvm->arch.config_lock); 675 out_slots: 676 if (ret) 677 kvm_vm_dead(kvm); 678 679 mutex_unlock(&kvm->slots_lock); 680 681 return ret; 682 } 683 684 void kvm_vgic_finalize_idregs(struct kvm *kvm) 685 { 686 u32 type = kvm->arch.vgic.vgic_model; 687 u64 aa64pfr0, aa64pfr2, pfr1; 688 689 aa64pfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1) & ~ID_AA64PFR0_EL1_GIC; 690 aa64pfr2 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1) & ~ID_AA64PFR2_EL1_GCIE; 691 pfr1 = kvm_read_vm_id_reg(kvm, SYS_ID_PFR1_EL1) & ~ID_PFR1_EL1_GIC; 692 693 switch (type) { 694 case KVM_DEV_TYPE_ARM_VGIC_V2: 695 break; 696 case KVM_DEV_TYPE_ARM_VGIC_V3: 697 aa64pfr0 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, GIC, IMP); 698 if (kvm_supports_32bit_el0()) 699 pfr1 |= SYS_FIELD_PREP_ENUM(ID_PFR1_EL1, GIC, GICv3); 700 break; 701 case KVM_DEV_TYPE_ARM_VGIC_V5: 702 aa64pfr2 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR2_EL1, GCIE, IMP); 703 break; 704 default: 705 WARN_ONCE(1, "Unknown VGIC type!!!\n"); 706 } 707 708 kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1, aa64pfr0); 709 kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1, aa64pfr2); 710 kvm_set_vm_id_reg(kvm, SYS_ID_PFR1_EL1, pfr1); 711 } 712 713 /* GENERIC PROBE */ 714 715 void kvm_vgic_cpu_up(void) 716 { 717 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0); 718 } 719 720 721 void kvm_vgic_cpu_down(void) 722 { 723 disable_percpu_irq(kvm_vgic_global_state.maint_irq); 724 } 725 726 static irqreturn_t vgic_maintenance_handler(int irq, void *data) 727 { 728 struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)data; 729 730 /* 731 * We cannot rely on the vgic maintenance interrupt to be 732 * delivered synchronously. This means we can only use it to 733 * exit the VM, and we perform the handling of EOIed 734 * interrupts on the exit path (see vgic_fold_lr_state). 735 * 736 * Of course, NV throws a wrench in this plan, and needs 737 * something special. 738 */ 739 if (vcpu && vgic_state_is_nested(vcpu)) 740 vgic_v3_handle_nested_maint_irq(vcpu); 741 742 return IRQ_HANDLED; 743 } 744 745 static struct gic_kvm_info *gic_kvm_info; 746 747 void __init vgic_set_kvm_info(const struct gic_kvm_info *info) 748 { 749 BUG_ON(gic_kvm_info != NULL); 750 gic_kvm_info = kmalloc_obj(*gic_kvm_info); 751 if (gic_kvm_info) 752 *gic_kvm_info = *info; 753 } 754 755 /** 756 * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware 757 * 758 * For a specific CPU, initialize the GIC VE hardware. 759 */ 760 void kvm_vgic_init_cpu_hardware(void) 761 { 762 BUG_ON(preemptible()); 763 764 /* 765 * We want to make sure the list registers start out clear so that we 766 * only have the program the used registers. 767 */ 768 if (kvm_vgic_global_state.type == VGIC_V2) { 769 vgic_v2_init_lrs(); 770 } else if (kvm_vgic_global_state.type == VGIC_V3 || 771 kvm_vgic_global_state.has_gcie_v3_compat) { 772 kvm_call_hyp(__vgic_v3_init_lrs); 773 } 774 } 775 776 /** 777 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable 778 * according to the host GIC model. Accordingly calls either 779 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be 780 * instantiated by a guest later on . 781 */ 782 int kvm_vgic_hyp_init(void) 783 { 784 bool has_mask; 785 int ret; 786 787 if (!gic_kvm_info) 788 return -ENODEV; 789 790 has_mask = !gic_kvm_info->no_maint_irq_mask; 791 792 if (has_mask && !gic_kvm_info->maint_irq) { 793 kvm_err("No vgic maintenance irq\n"); 794 ret = -ENXIO; 795 goto out_free; 796 } 797 798 /* 799 * If we get one of these oddball non-GICs, taint the kernel, 800 * as we have no idea of how they *really* behave. 801 */ 802 if (gic_kvm_info->no_hw_deactivation) { 803 kvm_info("Non-architectural vgic, tainting kernel\n"); 804 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 805 kvm_vgic_global_state.no_hw_deactivation = true; 806 } 807 808 switch (gic_kvm_info->type) { 809 case GIC_V2: 810 ret = vgic_v2_probe(gic_kvm_info); 811 break; 812 case GIC_V3: 813 ret = vgic_v3_probe(gic_kvm_info); 814 if (!ret) { 815 static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif); 816 kvm_info("GIC system register CPU interface enabled\n"); 817 } 818 break; 819 case GIC_V5: 820 ret = vgic_v5_probe(gic_kvm_info); 821 break; 822 default: 823 ret = -ENODEV; 824 } 825 826 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq; 827 828 out_free: 829 kfree(gic_kvm_info); 830 gic_kvm_info = NULL; 831 832 if (ret) 833 return ret; 834 835 if (!has_mask && !kvm_vgic_global_state.maint_irq) 836 return 0; 837 838 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq, 839 vgic_maintenance_handler, 840 "vgic", kvm_get_running_vcpus()); 841 if (ret) { 842 kvm_err("Cannot register interrupt %d\n", 843 kvm_vgic_global_state.maint_irq); 844 return ret; 845 } 846 847 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq); 848 return 0; 849 } 850