Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +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/kvm.h> |
| 18 | #include <linux/kvm_host.h> |
Christoffer Dall | 8e44474 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 19 | #include <linux/list_sort.h> |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 20 | |
| 21 | #include "vgic.h" |
| 22 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 23 | #define CREATE_TRACE_POINTS |
| 24 | #include "../trace.h" |
| 25 | |
| 26 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 27 | #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p) |
| 28 | #else |
| 29 | #define DEBUG_SPINLOCK_BUG_ON(p) |
| 30 | #endif |
| 31 | |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 32 | struct vgic_global __section(.hyp.text) kvm_vgic_global_state; |
| 33 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 34 | /* |
| 35 | * Locking order is always: |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame^] | 36 | * its->cmd_lock (mutex) |
| 37 | * its->its_lock (mutex) |
| 38 | * vgic_cpu->ap_list_lock |
| 39 | * vgic_irq->irq_lock |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 40 | * |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame^] | 41 | * If you need to take multiple locks, always take the upper lock first, |
| 42 | * then the lower ones, e.g. first take the its_lock, then the irq_lock. |
| 43 | * If you are already holding a lock and need to take a higher one, you |
| 44 | * have to drop the lower ranking lock first and re-aquire it after having |
| 45 | * taken the upper one. |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 46 | * |
| 47 | * When taking more than one ap_list_lock at the same time, always take the |
| 48 | * lowest numbered VCPU's ap_list_lock first, so: |
| 49 | * vcpuX->vcpu_id < vcpuY->vcpu_id: |
| 50 | * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock); |
| 51 | * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock); |
| 52 | */ |
| 53 | |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 54 | struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, |
| 55 | u32 intid) |
| 56 | { |
| 57 | /* SGIs and PPIs */ |
| 58 | if (intid <= VGIC_MAX_PRIVATE) |
| 59 | return &vcpu->arch.vgic_cpu.private_irqs[intid]; |
| 60 | |
| 61 | /* SPIs */ |
| 62 | if (intid <= VGIC_MAX_SPI) |
| 63 | return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS]; |
| 64 | |
| 65 | /* LPIs are not yet covered */ |
| 66 | if (intid >= VGIC_MIN_LPI) |
| 67 | return NULL; |
| 68 | |
| 69 | WARN(1, "Looking up struct vgic_irq for reserved INTID"); |
| 70 | return NULL; |
| 71 | } |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 72 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 73 | static void vgic_get_irq_kref(struct vgic_irq *irq) |
| 74 | { |
| 75 | if (irq->intid < VGIC_MIN_LPI) |
| 76 | return; |
| 77 | |
| 78 | kref_get(&irq->refcount); |
| 79 | } |
| 80 | |
| 81 | /* The refcount should never drop to 0 at the moment. */ |
| 82 | static void vgic_irq_release(struct kref *ref) |
| 83 | { |
| 84 | WARN_ON(1); |
| 85 | } |
| 86 | |
| 87 | void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) |
| 88 | { |
| 89 | if (irq->intid < VGIC_MIN_LPI) |
| 90 | return; |
| 91 | |
| 92 | kref_put(&irq->refcount, vgic_irq_release); |
| 93 | } |
| 94 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 95 | /** |
| 96 | * kvm_vgic_target_oracle - compute the target vcpu for an irq |
| 97 | * |
| 98 | * @irq: The irq to route. Must be already locked. |
| 99 | * |
| 100 | * Based on the current state of the interrupt (enabled, pending, |
| 101 | * active, vcpu and target_vcpu), compute the next vcpu this should be |
| 102 | * given to. Return NULL if this shouldn't be injected at all. |
| 103 | * |
| 104 | * Requires the IRQ lock to be held. |
| 105 | */ |
| 106 | static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq) |
| 107 | { |
| 108 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
| 109 | |
| 110 | /* If the interrupt is active, it must stay on the current vcpu */ |
| 111 | if (irq->active) |
| 112 | return irq->vcpu ? : irq->target_vcpu; |
| 113 | |
| 114 | /* |
| 115 | * If the IRQ is not active but enabled and pending, we should direct |
| 116 | * it to its configured target VCPU. |
| 117 | * If the distributor is disabled, pending interrupts shouldn't be |
| 118 | * forwarded. |
| 119 | */ |
| 120 | if (irq->enabled && irq->pending) { |
| 121 | if (unlikely(irq->target_vcpu && |
| 122 | !irq->target_vcpu->kvm->arch.vgic.enabled)) |
| 123 | return NULL; |
| 124 | |
| 125 | return irq->target_vcpu; |
| 126 | } |
| 127 | |
| 128 | /* If neither active nor pending and enabled, then this IRQ should not |
| 129 | * be queued to any VCPU. |
| 130 | */ |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* |
Christoffer Dall | 8e44474 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 135 | * The order of items in the ap_lists defines how we'll pack things in LRs as |
| 136 | * well, the first items in the list being the first things populated in the |
| 137 | * LRs. |
| 138 | * |
| 139 | * A hard rule is that active interrupts can never be pushed out of the LRs |
| 140 | * (and therefore take priority) since we cannot reliably trap on deactivation |
| 141 | * of IRQs and therefore they have to be present in the LRs. |
| 142 | * |
| 143 | * Otherwise things should be sorted by the priority field and the GIC |
| 144 | * hardware support will take care of preemption of priority groups etc. |
| 145 | * |
| 146 | * Return negative if "a" sorts before "b", 0 to preserve order, and positive |
| 147 | * to sort "b" before "a". |
| 148 | */ |
| 149 | static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 150 | { |
| 151 | struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list); |
| 152 | struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list); |
| 153 | bool penda, pendb; |
| 154 | int ret; |
| 155 | |
| 156 | spin_lock(&irqa->irq_lock); |
| 157 | spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING); |
| 158 | |
| 159 | if (irqa->active || irqb->active) { |
| 160 | ret = (int)irqb->active - (int)irqa->active; |
| 161 | goto out; |
| 162 | } |
| 163 | |
| 164 | penda = irqa->enabled && irqa->pending; |
| 165 | pendb = irqb->enabled && irqb->pending; |
| 166 | |
| 167 | if (!penda || !pendb) { |
| 168 | ret = (int)pendb - (int)penda; |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | /* Both pending and enabled, sort by priority */ |
| 173 | ret = irqa->priority - irqb->priority; |
| 174 | out: |
| 175 | spin_unlock(&irqb->irq_lock); |
| 176 | spin_unlock(&irqa->irq_lock); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | /* Must be called with the ap_list_lock held */ |
| 181 | static void vgic_sort_ap_list(struct kvm_vcpu *vcpu) |
| 182 | { |
| 183 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 184 | |
| 185 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 186 | |
| 187 | list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp); |
| 188 | } |
| 189 | |
| 190 | /* |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 191 | * Only valid injection if changing level for level-triggered IRQs or for a |
| 192 | * rising edge. |
| 193 | */ |
| 194 | static bool vgic_validate_injection(struct vgic_irq *irq, bool level) |
| 195 | { |
| 196 | switch (irq->config) { |
| 197 | case VGIC_CONFIG_LEVEL: |
| 198 | return irq->line_level != level; |
| 199 | case VGIC_CONFIG_EDGE: |
| 200 | return level; |
| 201 | } |
| 202 | |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list. |
| 208 | * Do the queuing if necessary, taking the right locks in the right order. |
| 209 | * Returns true when the IRQ was queued, false otherwise. |
| 210 | * |
| 211 | * Needs to be entered with the IRQ lock already held, but will return |
| 212 | * with all locks dropped. |
| 213 | */ |
| 214 | bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq) |
| 215 | { |
| 216 | struct kvm_vcpu *vcpu; |
| 217 | |
| 218 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
| 219 | |
| 220 | retry: |
| 221 | vcpu = vgic_target_oracle(irq); |
| 222 | if (irq->vcpu || !vcpu) { |
| 223 | /* |
| 224 | * If this IRQ is already on a VCPU's ap_list, then it |
| 225 | * cannot be moved or modified and there is no more work for |
| 226 | * us to do. |
| 227 | * |
| 228 | * Otherwise, if the irq is not pending and enabled, it does |
| 229 | * not need to be inserted into an ap_list and there is also |
| 230 | * no more work for us to do. |
| 231 | */ |
| 232 | spin_unlock(&irq->irq_lock); |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * We must unlock the irq lock to take the ap_list_lock where |
| 238 | * we are going to insert this new pending interrupt. |
| 239 | */ |
| 240 | spin_unlock(&irq->irq_lock); |
| 241 | |
| 242 | /* someone can do stuff here, which we re-check below */ |
| 243 | |
| 244 | spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 245 | spin_lock(&irq->irq_lock); |
| 246 | |
| 247 | /* |
| 248 | * Did something change behind our backs? |
| 249 | * |
| 250 | * There are two cases: |
| 251 | * 1) The irq lost its pending state or was disabled behind our |
| 252 | * backs and/or it was queued to another VCPU's ap_list. |
| 253 | * 2) Someone changed the affinity on this irq behind our |
| 254 | * backs and we are now holding the wrong ap_list_lock. |
| 255 | * |
| 256 | * In both cases, drop the locks and retry. |
| 257 | */ |
| 258 | |
| 259 | if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) { |
| 260 | spin_unlock(&irq->irq_lock); |
| 261 | spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 262 | |
| 263 | spin_lock(&irq->irq_lock); |
| 264 | goto retry; |
| 265 | } |
| 266 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 267 | /* |
| 268 | * Grab a reference to the irq to reflect the fact that it is |
| 269 | * now in the ap_list. |
| 270 | */ |
| 271 | vgic_get_irq_kref(irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 272 | list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head); |
| 273 | irq->vcpu = vcpu; |
| 274 | |
| 275 | spin_unlock(&irq->irq_lock); |
| 276 | spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 277 | |
| 278 | kvm_vcpu_kick(vcpu); |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | static int vgic_update_irq_pending(struct kvm *kvm, int cpuid, |
| 284 | unsigned int intid, bool level, |
| 285 | bool mapped_irq) |
| 286 | { |
| 287 | struct kvm_vcpu *vcpu; |
| 288 | struct vgic_irq *irq; |
| 289 | int ret; |
| 290 | |
| 291 | trace_vgic_update_irq_pending(cpuid, intid, level); |
| 292 | |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 293 | ret = vgic_lazy_init(kvm); |
| 294 | if (ret) |
| 295 | return ret; |
| 296 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 297 | vcpu = kvm_get_vcpu(kvm, cpuid); |
| 298 | if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS) |
| 299 | return -EINVAL; |
| 300 | |
| 301 | irq = vgic_get_irq(kvm, vcpu, intid); |
| 302 | if (!irq) |
| 303 | return -EINVAL; |
| 304 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 305 | if (irq->hw != mapped_irq) { |
| 306 | vgic_put_irq(kvm, irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 307 | return -EINVAL; |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 308 | } |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 309 | |
| 310 | spin_lock(&irq->irq_lock); |
| 311 | |
| 312 | if (!vgic_validate_injection(irq, level)) { |
| 313 | /* Nothing to see here, move along... */ |
| 314 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 315 | vgic_put_irq(kvm, irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if (irq->config == VGIC_CONFIG_LEVEL) { |
| 320 | irq->line_level = level; |
| 321 | irq->pending = level || irq->soft_pending; |
| 322 | } else { |
| 323 | irq->pending = true; |
| 324 | } |
| 325 | |
| 326 | vgic_queue_irq_unlock(kvm, irq); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 327 | vgic_put_irq(kvm, irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic |
| 334 | * @kvm: The VM structure pointer |
| 335 | * @cpuid: The CPU for PPIs |
| 336 | * @intid: The INTID to inject a new state to. |
| 337 | * @level: Edge-triggered: true: to trigger the interrupt |
| 338 | * false: to ignore the call |
| 339 | * Level-sensitive true: raise the input signal |
| 340 | * false: lower the input signal |
| 341 | * |
| 342 | * The VGIC is not concerned with devices being active-LOW or active-HIGH for |
| 343 | * level-sensitive interrupts. You can think of the level parameter as 1 |
| 344 | * being HIGH and 0 being LOW and all devices being active-HIGH. |
| 345 | */ |
| 346 | int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, |
| 347 | bool level) |
| 348 | { |
| 349 | return vgic_update_irq_pending(kvm, cpuid, intid, level, false); |
| 350 | } |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 351 | |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 352 | int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid, |
| 353 | bool level) |
| 354 | { |
| 355 | return vgic_update_irq_pending(kvm, cpuid, intid, level, true); |
| 356 | } |
| 357 | |
| 358 | int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq) |
| 359 | { |
| 360 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq); |
| 361 | |
| 362 | BUG_ON(!irq); |
| 363 | |
| 364 | spin_lock(&irq->irq_lock); |
| 365 | |
| 366 | irq->hw = true; |
| 367 | irq->hwintid = phys_irq; |
| 368 | |
| 369 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 370 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq) |
| 376 | { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 377 | struct vgic_irq *irq; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 378 | |
| 379 | if (!vgic_initialized(vcpu->kvm)) |
| 380 | return -EAGAIN; |
| 381 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 382 | irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq); |
| 383 | BUG_ON(!irq); |
| 384 | |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 385 | spin_lock(&irq->irq_lock); |
| 386 | |
| 387 | irq->hw = false; |
| 388 | irq->hwintid = 0; |
| 389 | |
| 390 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 391 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 396 | /** |
| 397 | * vgic_prune_ap_list - Remove non-relevant interrupts from the list |
| 398 | * |
| 399 | * @vcpu: The VCPU pointer |
| 400 | * |
| 401 | * Go over the list of "interesting" interrupts, and prune those that we |
| 402 | * won't have to consider in the near future. |
| 403 | */ |
| 404 | static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) |
| 405 | { |
| 406 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 407 | struct vgic_irq *irq, *tmp; |
| 408 | |
| 409 | retry: |
| 410 | spin_lock(&vgic_cpu->ap_list_lock); |
| 411 | |
| 412 | list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) { |
| 413 | struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB; |
| 414 | |
| 415 | spin_lock(&irq->irq_lock); |
| 416 | |
| 417 | BUG_ON(vcpu != irq->vcpu); |
| 418 | |
| 419 | target_vcpu = vgic_target_oracle(irq); |
| 420 | |
| 421 | if (!target_vcpu) { |
| 422 | /* |
| 423 | * We don't need to process this interrupt any |
| 424 | * further, move it off the list. |
| 425 | */ |
| 426 | list_del(&irq->ap_list); |
| 427 | irq->vcpu = NULL; |
| 428 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * This vgic_put_irq call matches the |
| 432 | * vgic_get_irq_kref in vgic_queue_irq_unlock, |
| 433 | * where we added the LPI to the ap_list. As |
| 434 | * we remove the irq from the list, we drop |
| 435 | * also drop the refcount. |
| 436 | */ |
| 437 | vgic_put_irq(vcpu->kvm, irq); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
| 441 | if (target_vcpu == vcpu) { |
| 442 | /* We're on the right CPU */ |
| 443 | spin_unlock(&irq->irq_lock); |
| 444 | continue; |
| 445 | } |
| 446 | |
| 447 | /* This interrupt looks like it has to be migrated. */ |
| 448 | |
| 449 | spin_unlock(&irq->irq_lock); |
| 450 | spin_unlock(&vgic_cpu->ap_list_lock); |
| 451 | |
| 452 | /* |
| 453 | * Ensure locking order by always locking the smallest |
| 454 | * ID first. |
| 455 | */ |
| 456 | if (vcpu->vcpu_id < target_vcpu->vcpu_id) { |
| 457 | vcpuA = vcpu; |
| 458 | vcpuB = target_vcpu; |
| 459 | } else { |
| 460 | vcpuA = target_vcpu; |
| 461 | vcpuB = vcpu; |
| 462 | } |
| 463 | |
| 464 | spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock); |
| 465 | spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock, |
| 466 | SINGLE_DEPTH_NESTING); |
| 467 | spin_lock(&irq->irq_lock); |
| 468 | |
| 469 | /* |
| 470 | * If the affinity has been preserved, move the |
| 471 | * interrupt around. Otherwise, it means things have |
| 472 | * changed while the interrupt was unlocked, and we |
| 473 | * need to replay this. |
| 474 | * |
| 475 | * In all cases, we cannot trust the list not to have |
| 476 | * changed, so we restart from the beginning. |
| 477 | */ |
| 478 | if (target_vcpu == vgic_target_oracle(irq)) { |
| 479 | struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu; |
| 480 | |
| 481 | list_del(&irq->ap_list); |
| 482 | irq->vcpu = target_vcpu; |
| 483 | list_add_tail(&irq->ap_list, &new_cpu->ap_list_head); |
| 484 | } |
| 485 | |
| 486 | spin_unlock(&irq->irq_lock); |
| 487 | spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock); |
| 488 | spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock); |
| 489 | goto retry; |
| 490 | } |
| 491 | |
| 492 | spin_unlock(&vgic_cpu->ap_list_lock); |
| 493 | } |
| 494 | |
| 495 | static inline void vgic_process_maintenance_interrupt(struct kvm_vcpu *vcpu) |
| 496 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 497 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 498 | vgic_v2_process_maintenance(vcpu); |
| 499 | else |
| 500 | vgic_v3_process_maintenance(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu) |
| 504 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 505 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 506 | vgic_v2_fold_lr_state(vcpu); |
| 507 | else |
| 508 | vgic_v3_fold_lr_state(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* Requires the irq_lock to be held. */ |
| 512 | static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, |
| 513 | struct vgic_irq *irq, int lr) |
| 514 | { |
| 515 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 516 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 517 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 518 | vgic_v2_populate_lr(vcpu, irq, lr); |
| 519 | else |
| 520 | vgic_v3_populate_lr(vcpu, irq, lr); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr) |
| 524 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 525 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 526 | vgic_v2_clear_lr(vcpu, lr); |
| 527 | else |
| 528 | vgic_v3_clear_lr(vcpu, lr); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static inline void vgic_set_underflow(struct kvm_vcpu *vcpu) |
| 532 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 533 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 534 | vgic_v2_set_underflow(vcpu); |
| 535 | else |
| 536 | vgic_v3_set_underflow(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* Requires the ap_list_lock to be held. */ |
| 540 | static int compute_ap_list_depth(struct kvm_vcpu *vcpu) |
| 541 | { |
| 542 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 543 | struct vgic_irq *irq; |
| 544 | int count = 0; |
| 545 | |
| 546 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 547 | |
| 548 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 549 | spin_lock(&irq->irq_lock); |
| 550 | /* GICv2 SGIs can count for more than one... */ |
| 551 | if (vgic_irq_is_sgi(irq->intid) && irq->source) |
| 552 | count += hweight8(irq->source); |
| 553 | else |
| 554 | count++; |
| 555 | spin_unlock(&irq->irq_lock); |
| 556 | } |
| 557 | return count; |
| 558 | } |
| 559 | |
| 560 | /* Requires the VCPU's ap_list_lock to be held. */ |
| 561 | static void vgic_flush_lr_state(struct kvm_vcpu *vcpu) |
| 562 | { |
| 563 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 564 | struct vgic_irq *irq; |
| 565 | int count = 0; |
| 566 | |
| 567 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 568 | |
| 569 | if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) { |
| 570 | vgic_set_underflow(vcpu); |
| 571 | vgic_sort_ap_list(vcpu); |
| 572 | } |
| 573 | |
| 574 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 575 | spin_lock(&irq->irq_lock); |
| 576 | |
| 577 | if (unlikely(vgic_target_oracle(irq) != vcpu)) |
| 578 | goto next; |
| 579 | |
| 580 | /* |
| 581 | * If we get an SGI with multiple sources, try to get |
| 582 | * them in all at once. |
| 583 | */ |
| 584 | do { |
| 585 | vgic_populate_lr(vcpu, irq, count++); |
| 586 | } while (irq->source && count < kvm_vgic_global_state.nr_lr); |
| 587 | |
| 588 | next: |
| 589 | spin_unlock(&irq->irq_lock); |
| 590 | |
| 591 | if (count == kvm_vgic_global_state.nr_lr) |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | vcpu->arch.vgic_cpu.used_lrs = count; |
| 596 | |
| 597 | /* Nuke remaining LRs */ |
| 598 | for ( ; count < kvm_vgic_global_state.nr_lr; count++) |
| 599 | vgic_clear_lr(vcpu, count); |
| 600 | } |
| 601 | |
| 602 | /* Sync back the hardware VGIC state into our emulation after a guest's run. */ |
| 603 | void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) |
| 604 | { |
| 605 | vgic_process_maintenance_interrupt(vcpu); |
| 606 | vgic_fold_lr_state(vcpu); |
| 607 | vgic_prune_ap_list(vcpu); |
| 608 | } |
| 609 | |
| 610 | /* Flush our emulation state into the GIC hardware before entering the guest. */ |
| 611 | void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) |
| 612 | { |
| 613 | spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 614 | vgic_flush_lr_state(vcpu); |
| 615 | spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 616 | } |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 617 | |
| 618 | int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) |
| 619 | { |
| 620 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 621 | struct vgic_irq *irq; |
| 622 | bool pending = false; |
| 623 | |
| 624 | if (!vcpu->kvm->arch.vgic.enabled) |
| 625 | return false; |
| 626 | |
| 627 | spin_lock(&vgic_cpu->ap_list_lock); |
| 628 | |
| 629 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 630 | spin_lock(&irq->irq_lock); |
| 631 | pending = irq->pending && irq->enabled; |
| 632 | spin_unlock(&irq->irq_lock); |
| 633 | |
| 634 | if (pending) |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | spin_unlock(&vgic_cpu->ap_list_lock); |
| 639 | |
| 640 | return pending; |
| 641 | } |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 642 | |
| 643 | void vgic_kick_vcpus(struct kvm *kvm) |
| 644 | { |
| 645 | struct kvm_vcpu *vcpu; |
| 646 | int c; |
| 647 | |
| 648 | /* |
| 649 | * We've injected an interrupt, time to find out who deserves |
| 650 | * a good kick... |
| 651 | */ |
| 652 | kvm_for_each_vcpu(c, vcpu, kvm) { |
| 653 | if (kvm_vgic_vcpu_pending_irq(vcpu)) |
| 654 | kvm_vcpu_kick(vcpu); |
| 655 | } |
| 656 | } |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 657 | |
| 658 | bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq) |
| 659 | { |
| 660 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq); |
| 661 | bool map_is_active; |
| 662 | |
| 663 | spin_lock(&irq->irq_lock); |
| 664 | map_is_active = irq->hw && irq->active; |
| 665 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 666 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 667 | |
| 668 | return map_is_active; |
| 669 | } |