Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, 2016 ARM Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/cpu.h> |
| 20 | #include <linux/kvm_host.h> |
| 21 | #include <kvm/arm_vgic.h> |
| 22 | #include <asm/kvm_mmu.h> |
| 23 | #include "vgic.h" |
| 24 | |
Eric Auger | 5e6431d | 2015-12-21 14:50:50 +0100 | [diff] [blame^] | 25 | /* CREATION */ |
| 26 | |
| 27 | /** |
| 28 | * kvm_vgic_create: triggered by the instantiation of the VGIC device by |
| 29 | * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only) |
| 30 | * or through the generic KVM_CREATE_DEVICE API ioctl. |
| 31 | * irqchip_in_kernel() tells you if this function succeeded or not. |
| 32 | */ |
| 33 | int kvm_vgic_create(struct kvm *kvm, u32 type) |
| 34 | { |
| 35 | int i, vcpu_lock_idx = -1, ret; |
| 36 | struct kvm_vcpu *vcpu; |
| 37 | |
| 38 | mutex_lock(&kvm->lock); |
| 39 | |
| 40 | if (irqchip_in_kernel(kvm)) { |
| 41 | ret = -EEXIST; |
| 42 | goto out; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * This function is also called by the KVM_CREATE_IRQCHIP handler, |
| 47 | * which had no chance yet to check the availability of the GICv2 |
| 48 | * emulation. So check this here again. KVM_CREATE_DEVICE does |
| 49 | * the proper checks already. |
| 50 | */ |
| 51 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && |
| 52 | !kvm_vgic_global_state.can_emulate_gicv2) { |
| 53 | ret = -ENODEV; |
| 54 | goto out; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Any time a vcpu is run, vcpu_load is called which tries to grab the |
| 59 | * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure |
| 60 | * that no other VCPUs are run while we create the vgic. |
| 61 | */ |
| 62 | ret = -EBUSY; |
| 63 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 64 | if (!mutex_trylock(&vcpu->mutex)) |
| 65 | goto out_unlock; |
| 66 | vcpu_lock_idx = i; |
| 67 | } |
| 68 | |
| 69 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 70 | if (vcpu->arch.has_run_once) |
| 71 | goto out_unlock; |
| 72 | } |
| 73 | ret = 0; |
| 74 | |
| 75 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2) |
| 76 | kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS; |
| 77 | else |
| 78 | kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS; |
| 79 | |
| 80 | if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) { |
| 81 | ret = -E2BIG; |
| 82 | goto out_unlock; |
| 83 | } |
| 84 | |
| 85 | kvm->arch.vgic.in_kernel = true; |
| 86 | kvm->arch.vgic.vgic_model = type; |
| 87 | |
| 88 | /* |
| 89 | * kvm_vgic_global_state.vctrl_base is set on vgic probe (kvm_arch_init) |
| 90 | * it is stored in distributor struct for asm save/restore purpose |
| 91 | */ |
| 92 | kvm->arch.vgic.vctrl_base = kvm_vgic_global_state.vctrl_base; |
| 93 | |
| 94 | kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; |
| 95 | kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; |
| 96 | kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF; |
| 97 | |
| 98 | out_unlock: |
| 99 | for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { |
| 100 | vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); |
| 101 | mutex_unlock(&vcpu->mutex); |
| 102 | } |
| 103 | |
| 104 | out: |
| 105 | mutex_unlock(&kvm->lock); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Eric Auger | 9097773 | 2015-12-01 15:02:35 +0100 | [diff] [blame] | 109 | /* GENERIC PROBE */ |
| 110 | |
| 111 | static void vgic_init_maintenance_interrupt(void *info) |
| 112 | { |
| 113 | enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0); |
| 114 | } |
| 115 | |
| 116 | static int vgic_cpu_notify(struct notifier_block *self, |
| 117 | unsigned long action, void *cpu) |
| 118 | { |
| 119 | switch (action) { |
| 120 | case CPU_STARTING: |
| 121 | case CPU_STARTING_FROZEN: |
| 122 | vgic_init_maintenance_interrupt(NULL); |
| 123 | break; |
| 124 | case CPU_DYING: |
| 125 | case CPU_DYING_FROZEN: |
| 126 | disable_percpu_irq(kvm_vgic_global_state.maint_irq); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return NOTIFY_OK; |
| 131 | } |
| 132 | |
| 133 | static struct notifier_block vgic_cpu_nb = { |
| 134 | .notifier_call = vgic_cpu_notify, |
| 135 | }; |
| 136 | |
| 137 | static irqreturn_t vgic_maintenance_handler(int irq, void *data) |
| 138 | { |
| 139 | /* |
| 140 | * We cannot rely on the vgic maintenance interrupt to be |
| 141 | * delivered synchronously. This means we can only use it to |
| 142 | * exit the VM, and we perform the handling of EOIed |
| 143 | * interrupts on the exit path (see vgic_process_maintenance). |
| 144 | */ |
| 145 | return IRQ_HANDLED; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable |
| 150 | * according to the host GIC model. Accordingly calls either |
| 151 | * vgic_v2/v3_probe which registers the KVM_DEVICE that can be |
| 152 | * instantiated by a guest later on . |
| 153 | */ |
| 154 | int kvm_vgic_hyp_init(void) |
| 155 | { |
| 156 | const struct gic_kvm_info *gic_kvm_info; |
| 157 | int ret; |
| 158 | |
| 159 | gic_kvm_info = gic_get_kvm_info(); |
| 160 | if (!gic_kvm_info) |
| 161 | return -ENODEV; |
| 162 | |
| 163 | if (!gic_kvm_info->maint_irq) { |
| 164 | kvm_err("No vgic maintenance irq\n"); |
| 165 | return -ENXIO; |
| 166 | } |
| 167 | |
| 168 | switch (gic_kvm_info->type) { |
| 169 | case GIC_V2: |
| 170 | ret = vgic_v2_probe(gic_kvm_info); |
| 171 | break; |
| 172 | case GIC_V3: |
| 173 | ret = vgic_v3_probe(gic_kvm_info); |
| 174 | break; |
| 175 | default: |
| 176 | ret = -ENODEV; |
| 177 | }; |
| 178 | |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq; |
| 183 | ret = request_percpu_irq(kvm_vgic_global_state.maint_irq, |
| 184 | vgic_maintenance_handler, |
| 185 | "vgic", kvm_get_running_vcpus()); |
| 186 | if (ret) { |
| 187 | kvm_err("Cannot register interrupt %d\n", |
| 188 | kvm_vgic_global_state.maint_irq); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | ret = __register_cpu_notifier(&vgic_cpu_nb); |
| 193 | if (ret) { |
| 194 | kvm_err("Cannot register vgic CPU notifier\n"); |
| 195 | goto out_free_irq; |
| 196 | } |
| 197 | |
| 198 | on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1); |
| 199 | |
| 200 | kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq); |
| 201 | return 0; |
| 202 | |
| 203 | out_free_irq: |
| 204 | free_percpu_irq(kvm_vgic_global_state.maint_irq, |
| 205 | kvm_get_running_vcpus()); |
| 206 | return ret; |
| 207 | } |