^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015, 2016 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <kvm/arm_vgic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/kvm_emulate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/kvm_mmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "vgic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Initialization rules: there are multiple stages to the vgic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * initialization, both for the distributor and the CPU interfaces. The basic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * idea is that even though the VGIC is not functional or not requested from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * user space, the critical path of the run loop can still call VGIC functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * that just won't do anything, without them having to check additional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * initialization flags to ensure they don't look at uninitialized data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Distributor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * - kvm_vgic_early_init(): initialization of static data that doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * depend on any sizing information or emulation type. No allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * is allowed there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * - vgic_init(): allocation and initialization of the generic data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * structures that depend on sizing information (number of CPUs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * number of interrupts). Also initializes the vcpu specific data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * structures. Can be executed lazily for GICv2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * CPU Interface:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * - kvm_vgic_vcpu_init(): initialization of static data that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * doesn't depend on any sizing information or emulation type. No
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * allocation is allowed there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* EARLY INIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @kvm: The VM whose VGIC districutor should be initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Only do initialization of static structures that don't require any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * allocation or sizing information from userspace. vgic_init() called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * kvm_vgic_dist_init() which takes care of the rest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void kvm_vgic_early_init(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct vgic_dist *dist = &kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) INIT_LIST_HEAD(&dist->lpi_list_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) INIT_LIST_HEAD(&dist->lpi_translation_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) raw_spin_lock_init(&dist->lpi_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* CREATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * kvm_vgic_create: triggered by the instantiation of the VGIC device by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * or through the generic KVM_CREATE_DEVICE API ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * irqchip_in_kernel() tells you if this function succeeded or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @kvm: kvm struct pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int kvm_vgic_create(struct kvm *kvm, u32 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (irqchip_in_kernel(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * This function is also called by the KVM_CREATE_IRQCHIP handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * which had no chance yet to check the availability of the GICv2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * emulation. So check this here again. KVM_CREATE_DEVICE does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * the proper checks already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) !kvm_vgic_global_state.can_emulate_gicv2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!lock_all_vcpus(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (vcpu->arch.has_run_once)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kvm->arch.vgic.in_kernel = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kvm->arch.vgic.vgic_model = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unlock_all_vcpus(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* INIT/DESTROY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * kvm_vgic_dist_init: initialize the dist data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @kvm: kvm struct pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @nr_spis: number of spis, frozen by caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct vgic_dist *dist = &kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!dist->spis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * In the following code we do not take the irq struct lock since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * no other action on irq structs can happen while the VGIC is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * not initialized yet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * If someone wants to inject an interrupt or does a MMIO access, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * require prior initialization in case of a virtual GICv3 or trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * initialization when using a virtual GICv2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (i = 0; i < nr_spis; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct vgic_irq *irq = &dist->spis[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) irq->intid = i + VGIC_NR_PRIVATE_IRQS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) INIT_LIST_HEAD(&irq->ap_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) raw_spin_lock_init(&irq->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) irq->vcpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) irq->target_vcpu = vcpu0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kref_init(&irq->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) switch (dist->vgic_model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) case KVM_DEV_TYPE_ARM_VGIC_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) irq->targets = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) irq->group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) case KVM_DEV_TYPE_ARM_VGIC_V3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) irq->mpidr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) irq->group = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) kfree(dist->spis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dist->spis = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * structures and register VCPU-specific KVM iodevs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @vcpu: pointer to the VCPU being created and initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Only do initialization, but do not actually enable the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * VGIC CPU interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) raw_spin_lock_init(&vgic_cpu->ap_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) atomic_set(&vgic_cpu->vgic_v3.its_vpe.vlpi_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Enable and configure all SGIs to be edge-triggered and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * configure all PPIs as level-triggered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) INIT_LIST_HEAD(&irq->ap_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) raw_spin_lock_init(&irq->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) irq->intid = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) irq->vcpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) irq->target_vcpu = vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) kref_init(&irq->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (vgic_irq_is_sgi(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* SGIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) irq->enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) irq->config = VGIC_CONFIG_EDGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* PPIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) irq->config = VGIC_CONFIG_LEVEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (!irqchip_in_kernel(vcpu->kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * If we are creating a VCPU with a GICv3 we must also register the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * KVM io device for the redistributor that belongs to this VCPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mutex_lock(&vcpu->kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ret = vgic_register_redist_iodev(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) mutex_unlock(&vcpu->kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (kvm_vgic_global_state.type == VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) vgic_v2_enable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) vgic_v3_enable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * vgic_init: allocates and initializes dist and vcpu data structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * depending on two dimensioning parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * - the number of spis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * - the number of vcpus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * The function is generally called when nr_spis has been explicitly set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * vgic_initialized() returns true when this function has succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Must be called with kvm->lock held!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) int vgic_init(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct vgic_dist *dist = &kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int ret = 0, i, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (vgic_initialized(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Are we also in the middle of creating a VCPU? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* freeze the number of spis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!dist->nr_spis)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Initialize groups on CPUs created before the VGIC type was known */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) kvm_for_each_vcpu(idx, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) switch (dist->vgic_model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case KVM_DEV_TYPE_ARM_VGIC_V3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) irq->group = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case KVM_DEV_TYPE_ARM_VGIC_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) irq->group = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) irq->targets = 1U << idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (vgic_has_its(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) vgic_lpi_translation_cache_init(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * If we have GICv4.1 enabled, unconditionnaly request enable the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * v4 support so that we get HW-accelerated vSGIs. Otherwise, only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * enable it if we present a virtual ITS to the guest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (vgic_supports_direct_msis(kvm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = vgic_v4_init(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kvm_for_each_vcpu(i, vcpu, kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) kvm_vgic_vcpu_enable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ret = kvm_vgic_setup_default_irq_routing(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) vgic_debug_init(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dist->implementation_rev = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) dist->initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void kvm_vgic_dist_destroy(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct vgic_dist *dist = &kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct vgic_redist_region *rdreg, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dist->ready = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) dist->initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) kfree(dist->spis);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) dist->spis = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) dist->nr_spis = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) list_del(&rdreg->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) kfree(rdreg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) INIT_LIST_HEAD(&dist->rd_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (vgic_has_its(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) vgic_lpi_translation_cache_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (vgic_supports_direct_msis(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) vgic_v4_teardown(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Retire all pending LPIs on this vcpu anyway as we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * going to destroy it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) vgic_flush_pending_lpis(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* To be called with kvm->lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void __kvm_vgic_destroy(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) vgic_debug_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) kvm_for_each_vcpu(i, vcpu, kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) kvm_vgic_vcpu_destroy(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kvm_vgic_dist_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) void kvm_vgic_destroy(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) mutex_lock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) __kvm_vgic_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) mutex_unlock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * @kvm: kvm struct pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int vgic_lazy_init(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (unlikely(!vgic_initialized(kvm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * We only provide the automatic initialization of the VGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * for the legacy case of a GICv2. Any other type must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * be explicitly initialized once setup with the respective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * KVM device call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) mutex_lock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ret = vgic_init(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) mutex_unlock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* RESOURCE MAPPING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * Map the MMIO regions depending on the VGIC model exposed to the guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * called on the first VCPU run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * Also map the virtual CPU interface into the VM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * v2 calls vgic_init() if not already done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * v3 and derivatives return an error if the VGIC is not initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * vgic_ready() returns true if this function has succeeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @kvm: kvm struct pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int kvm_vgic_map_resources(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct vgic_dist *dist = &kvm->arch.vgic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (likely(vgic_ready(kvm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) mutex_lock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (vgic_ready(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!irqchip_in_kernel(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ret = vgic_v2_map_resources(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) ret = vgic_v3_map_resources(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) __kvm_vgic_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) dist->ready = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) mutex_unlock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* GENERIC PROBE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static int vgic_init_cpu_starting(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static int vgic_init_cpu_dying(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) disable_percpu_irq(kvm_vgic_global_state.maint_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static irqreturn_t vgic_maintenance_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * We cannot rely on the vgic maintenance interrupt to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * delivered synchronously. This means we can only use it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * exit the VM, and we perform the handling of EOIed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * interrupts on the exit path (see vgic_fold_lr_state).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * For a specific CPU, initialize the GIC VE hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) void kvm_vgic_init_cpu_hardware(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) BUG_ON(preemptible());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * We want to make sure the list registers start out clear so that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) * only have the program the used registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (kvm_vgic_global_state.type == VGIC_V2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) vgic_v2_init_lrs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) kvm_call_hyp(__vgic_v3_init_lrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * according to the host GIC model. Accordingly calls either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * instantiated by a guest later on .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int kvm_vgic_hyp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) const struct gic_kvm_info *gic_kvm_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) gic_kvm_info = gic_get_kvm_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (!gic_kvm_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (!gic_kvm_info->maint_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) kvm_err("No vgic maintenance irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) switch (gic_kvm_info->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) case GIC_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) ret = vgic_v2_probe(gic_kvm_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) case GIC_V3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) ret = vgic_v3_probe(gic_kvm_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) kvm_info("GIC system register CPU interface enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) vgic_maintenance_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) "vgic", kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) kvm_err("Cannot register interrupt %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) kvm_vgic_global_state.maint_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) "kvm/arm/vgic:starting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) vgic_init_cpu_starting, vgic_init_cpu_dying);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) kvm_err("Cannot register vgic CPU notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) goto out_free_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) out_free_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) free_percpu_irq(kvm_vgic_global_state.maint_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) kvm_get_running_vcpus());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }