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> |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/irq.h> |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 22 | |
| 23 | #include "vgic.h" |
| 24 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 25 | #define CREATE_TRACE_POINTS |
Christoffer Dall | 35d2d5d | 2017-05-04 13:54:17 +0200 | [diff] [blame] | 26 | #include "trace.h" |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 27 | |
| 28 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 29 | #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p) |
| 30 | #else |
| 31 | #define DEBUG_SPINLOCK_BUG_ON(p) |
| 32 | #endif |
| 33 | |
Ard Biesheuvel | 63d7c6a | 2017-03-09 21:51:59 +0100 | [diff] [blame] | 34 | struct vgic_global kvm_vgic_global_state __ro_after_init = { |
| 35 | .gicv3_cpuif = STATIC_KEY_FALSE_INIT, |
| 36 | }; |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 37 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Locking order is always: |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 40 | * kvm->lock (mutex) |
| 41 | * its->cmd_lock (mutex) |
| 42 | * its->its_lock (mutex) |
| 43 | * vgic_cpu->ap_list_lock |
| 44 | * kvm->lpi_list_lock |
| 45 | * vgic_irq->irq_lock |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 46 | * |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 47 | * If you need to take multiple locks, always take the upper lock first, |
| 48 | * then the lower ones, e.g. first take the its_lock, then the irq_lock. |
| 49 | * If you are already holding a lock and need to take a higher one, you |
| 50 | * have to drop the lower ranking lock first and re-aquire it after having |
| 51 | * taken the upper one. |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 52 | * |
| 53 | * When taking more than one ap_list_lock at the same time, always take the |
| 54 | * lowest numbered VCPU's ap_list_lock first, so: |
| 55 | * vcpuX->vcpu_id < vcpuY->vcpu_id: |
| 56 | * spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock); |
| 57 | * spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 58 | * |
| 59 | * Since the VGIC must support injecting virtual interrupts from ISRs, we have |
| 60 | * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer |
| 61 | * spinlocks for any lock that may be taken while injecting an interrupt. |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 62 | */ |
| 63 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Iterate over the VM's list of mapped LPIs to find the one with a |
| 66 | * matching interrupt ID and return a reference to the IRQ structure. |
| 67 | */ |
| 68 | static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid) |
| 69 | { |
| 70 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 71 | struct vgic_irq *irq = NULL; |
| 72 | |
| 73 | spin_lock(&dist->lpi_list_lock); |
| 74 | |
| 75 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 76 | if (irq->intid != intid) |
| 77 | continue; |
| 78 | |
| 79 | /* |
| 80 | * This increases the refcount, the caller is expected to |
| 81 | * call vgic_put_irq() later once it's finished with the IRQ. |
| 82 | */ |
Marc Zyngier | d97594e | 2016-07-17 11:27:23 +0100 | [diff] [blame] | 83 | vgic_get_irq_kref(irq); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 84 | goto out_unlock; |
| 85 | } |
| 86 | irq = NULL; |
| 87 | |
| 88 | out_unlock: |
| 89 | spin_unlock(&dist->lpi_list_lock); |
| 90 | |
| 91 | return irq; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * This looks up the virtual interrupt ID to get the corresponding |
| 96 | * struct vgic_irq. It also increases the refcount, so any caller is expected |
| 97 | * to call vgic_put_irq() once it's finished with this IRQ. |
| 98 | */ |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 99 | struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu, |
| 100 | u32 intid) |
| 101 | { |
| 102 | /* SGIs and PPIs */ |
| 103 | if (intid <= VGIC_MAX_PRIVATE) |
| 104 | return &vcpu->arch.vgic_cpu.private_irqs[intid]; |
| 105 | |
| 106 | /* SPIs */ |
| 107 | if (intid <= VGIC_MAX_SPI) |
| 108 | return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS]; |
| 109 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 110 | /* LPIs */ |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 111 | if (intid >= VGIC_MIN_LPI) |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 112 | return vgic_get_lpi(kvm, intid); |
Christoffer Dall | 64a959d | 2015-11-24 16:51:12 +0100 | [diff] [blame] | 113 | |
| 114 | WARN(1, "Looking up struct vgic_irq for reserved INTID"); |
| 115 | return NULL; |
| 116 | } |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 117 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 118 | /* |
| 119 | * We can't do anything in here, because we lack the kvm pointer to |
| 120 | * lock and remove the item from the lpi_list. So we keep this function |
| 121 | * empty and use the return value of kref_put() to trigger the freeing. |
| 122 | */ |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 123 | static void vgic_irq_release(struct kref *ref) |
| 124 | { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq) |
| 128 | { |
Christoffer Dall | 2cccbb3 | 2016-08-02 22:05:42 +0200 | [diff] [blame] | 129 | struct vgic_dist *dist = &kvm->arch.vgic; |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 130 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 131 | if (irq->intid < VGIC_MIN_LPI) |
| 132 | return; |
| 133 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 134 | spin_lock(&dist->lpi_list_lock); |
Christoffer Dall | 2cccbb3 | 2016-08-02 22:05:42 +0200 | [diff] [blame] | 135 | if (!kref_put(&irq->refcount, vgic_irq_release)) { |
| 136 | spin_unlock(&dist->lpi_list_lock); |
| 137 | return; |
| 138 | }; |
| 139 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 140 | list_del(&irq->lpi_list); |
| 141 | dist->lpi_list_count--; |
| 142 | spin_unlock(&dist->lpi_list_lock); |
| 143 | |
| 144 | kfree(irq); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 147 | /** |
| 148 | * kvm_vgic_target_oracle - compute the target vcpu for an irq |
| 149 | * |
| 150 | * @irq: The irq to route. Must be already locked. |
| 151 | * |
| 152 | * Based on the current state of the interrupt (enabled, pending, |
| 153 | * active, vcpu and target_vcpu), compute the next vcpu this should be |
| 154 | * given to. Return NULL if this shouldn't be injected at all. |
| 155 | * |
| 156 | * Requires the IRQ lock to be held. |
| 157 | */ |
| 158 | static struct kvm_vcpu *vgic_target_oracle(struct vgic_irq *irq) |
| 159 | { |
| 160 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
| 161 | |
| 162 | /* If the interrupt is active, it must stay on the current vcpu */ |
| 163 | if (irq->active) |
| 164 | return irq->vcpu ? : irq->target_vcpu; |
| 165 | |
| 166 | /* |
| 167 | * If the IRQ is not active but enabled and pending, we should direct |
| 168 | * it to its configured target VCPU. |
| 169 | * If the distributor is disabled, pending interrupts shouldn't be |
| 170 | * forwarded. |
| 171 | */ |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 172 | if (irq->enabled && irq_is_pending(irq)) { |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 173 | if (unlikely(irq->target_vcpu && |
| 174 | !irq->target_vcpu->kvm->arch.vgic.enabled)) |
| 175 | return NULL; |
| 176 | |
| 177 | return irq->target_vcpu; |
| 178 | } |
| 179 | |
| 180 | /* If neither active nor pending and enabled, then this IRQ should not |
| 181 | * be queued to any VCPU. |
| 182 | */ |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | /* |
Christoffer Dall | 8e44474 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 187 | * The order of items in the ap_lists defines how we'll pack things in LRs as |
| 188 | * well, the first items in the list being the first things populated in the |
| 189 | * LRs. |
| 190 | * |
| 191 | * A hard rule is that active interrupts can never be pushed out of the LRs |
| 192 | * (and therefore take priority) since we cannot reliably trap on deactivation |
| 193 | * of IRQs and therefore they have to be present in the LRs. |
| 194 | * |
| 195 | * Otherwise things should be sorted by the priority field and the GIC |
| 196 | * hardware support will take care of preemption of priority groups etc. |
| 197 | * |
| 198 | * Return negative if "a" sorts before "b", 0 to preserve order, and positive |
| 199 | * to sort "b" before "a". |
| 200 | */ |
| 201 | static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 202 | { |
| 203 | struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list); |
| 204 | struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list); |
| 205 | bool penda, pendb; |
| 206 | int ret; |
| 207 | |
| 208 | spin_lock(&irqa->irq_lock); |
| 209 | spin_lock_nested(&irqb->irq_lock, SINGLE_DEPTH_NESTING); |
| 210 | |
| 211 | if (irqa->active || irqb->active) { |
| 212 | ret = (int)irqb->active - (int)irqa->active; |
| 213 | goto out; |
| 214 | } |
| 215 | |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 216 | penda = irqa->enabled && irq_is_pending(irqa); |
| 217 | pendb = irqb->enabled && irq_is_pending(irqb); |
Christoffer Dall | 8e44474 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 218 | |
| 219 | if (!penda || !pendb) { |
| 220 | ret = (int)pendb - (int)penda; |
| 221 | goto out; |
| 222 | } |
| 223 | |
| 224 | /* Both pending and enabled, sort by priority */ |
| 225 | ret = irqa->priority - irqb->priority; |
| 226 | out: |
| 227 | spin_unlock(&irqb->irq_lock); |
| 228 | spin_unlock(&irqa->irq_lock); |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /* Must be called with the ap_list_lock held */ |
| 233 | static void vgic_sort_ap_list(struct kvm_vcpu *vcpu) |
| 234 | { |
| 235 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 236 | |
| 237 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 238 | |
| 239 | list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp); |
| 240 | } |
| 241 | |
| 242 | /* |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 243 | * Only valid injection if changing level for level-triggered IRQs or for a |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 244 | * rising edge, and in-kernel connected IRQ lines can only be controlled by |
| 245 | * their owner. |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 246 | */ |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 247 | static bool vgic_validate_injection(struct vgic_irq *irq, bool level, void *owner) |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 248 | { |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 249 | if (irq->owner != owner) |
| 250 | return false; |
| 251 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 252 | switch (irq->config) { |
| 253 | case VGIC_CONFIG_LEVEL: |
| 254 | return irq->line_level != level; |
| 255 | case VGIC_CONFIG_EDGE: |
| 256 | return level; |
| 257 | } |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list. |
| 264 | * Do the queuing if necessary, taking the right locks in the right order. |
| 265 | * Returns true when the IRQ was queued, false otherwise. |
| 266 | * |
| 267 | * Needs to be entered with the IRQ lock already held, but will return |
| 268 | * with all locks dropped. |
| 269 | */ |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 270 | bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq, |
| 271 | unsigned long flags) |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 272 | { |
| 273 | struct kvm_vcpu *vcpu; |
| 274 | |
| 275 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
| 276 | |
| 277 | retry: |
| 278 | vcpu = vgic_target_oracle(irq); |
| 279 | if (irq->vcpu || !vcpu) { |
| 280 | /* |
| 281 | * If this IRQ is already on a VCPU's ap_list, then it |
| 282 | * cannot be moved or modified and there is no more work for |
| 283 | * us to do. |
| 284 | * |
| 285 | * Otherwise, if the irq is not pending and enabled, it does |
| 286 | * not need to be inserted into an ap_list and there is also |
| 287 | * no more work for us to do. |
| 288 | */ |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 289 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Shih-Wei Li | d42c797 | 2016-10-27 15:08:13 +0000 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * We have to kick the VCPU here, because we could be |
| 293 | * queueing an edge-triggered interrupt for which we |
| 294 | * get no EOI maintenance interrupt. In that case, |
| 295 | * while the IRQ is already on the VCPU's AP list, the |
| 296 | * VCPU could have EOI'ed the original interrupt and |
| 297 | * won't see this one until it exits for some other |
| 298 | * reason. |
| 299 | */ |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 300 | if (vcpu) { |
| 301 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
Shih-Wei Li | d42c797 | 2016-10-27 15:08:13 +0000 | [diff] [blame] | 302 | kvm_vcpu_kick(vcpu); |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 303 | } |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * We must unlock the irq lock to take the ap_list_lock where |
| 309 | * we are going to insert this new pending interrupt. |
| 310 | */ |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 311 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 312 | |
| 313 | /* someone can do stuff here, which we re-check below */ |
| 314 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 315 | spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 316 | spin_lock(&irq->irq_lock); |
| 317 | |
| 318 | /* |
| 319 | * Did something change behind our backs? |
| 320 | * |
| 321 | * There are two cases: |
| 322 | * 1) The irq lost its pending state or was disabled behind our |
| 323 | * backs and/or it was queued to another VCPU's ap_list. |
| 324 | * 2) Someone changed the affinity on this irq behind our |
| 325 | * backs and we are now holding the wrong ap_list_lock. |
| 326 | * |
| 327 | * In both cases, drop the locks and retry. |
| 328 | */ |
| 329 | |
| 330 | if (unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq))) { |
| 331 | spin_unlock(&irq->irq_lock); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 332 | spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 333 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 334 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 335 | goto retry; |
| 336 | } |
| 337 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 338 | /* |
| 339 | * Grab a reference to the irq to reflect the fact that it is |
| 340 | * now in the ap_list. |
| 341 | */ |
| 342 | vgic_get_irq_kref(irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 343 | list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head); |
| 344 | irq->vcpu = vcpu; |
| 345 | |
| 346 | spin_unlock(&irq->irq_lock); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 347 | spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 348 | |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 349 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 350 | kvm_vcpu_kick(vcpu); |
| 351 | |
| 352 | return true; |
| 353 | } |
| 354 | |
Christoffer Dall | 11710de | 2017-02-01 11:03:45 +0100 | [diff] [blame] | 355 | /** |
| 356 | * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic |
| 357 | * @kvm: The VM structure pointer |
| 358 | * @cpuid: The CPU for PPIs |
| 359 | * @intid: The INTID to inject a new state to. |
| 360 | * @level: Edge-triggered: true: to trigger the interrupt |
| 361 | * false: to ignore the call |
| 362 | * Level-sensitive true: raise the input signal |
| 363 | * false: lower the input signal |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 364 | * @owner: The opaque pointer to the owner of the IRQ being raised to verify |
| 365 | * that the caller is allowed to inject this IRQ. Userspace |
| 366 | * injections will have owner == NULL. |
Christoffer Dall | 11710de | 2017-02-01 11:03:45 +0100 | [diff] [blame] | 367 | * |
| 368 | * The VGIC is not concerned with devices being active-LOW or active-HIGH for |
| 369 | * level-sensitive interrupts. You can think of the level parameter as 1 |
| 370 | * being HIGH and 0 being LOW and all devices being active-HIGH. |
| 371 | */ |
| 372 | int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid, |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 373 | bool level, void *owner) |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 374 | { |
| 375 | struct kvm_vcpu *vcpu; |
| 376 | struct vgic_irq *irq; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 377 | unsigned long flags; |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 378 | int ret; |
| 379 | |
| 380 | trace_vgic_update_irq_pending(cpuid, intid, level); |
| 381 | |
Eric Auger | ad275b8b | 2015-12-21 18:09:38 +0100 | [diff] [blame] | 382 | ret = vgic_lazy_init(kvm); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 386 | vcpu = kvm_get_vcpu(kvm, cpuid); |
| 387 | if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | irq = vgic_get_irq(kvm, vcpu, intid); |
| 391 | if (!irq) |
| 392 | return -EINVAL; |
| 393 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 394 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 395 | |
Christoffer Dall | cb3f0ad | 2017-05-16 12:41:18 +0200 | [diff] [blame] | 396 | if (!vgic_validate_injection(irq, level, owner)) { |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 397 | /* Nothing to see here, move along... */ |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 398 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 399 | vgic_put_irq(kvm, irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 403 | if (irq->config == VGIC_CONFIG_LEVEL) |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 404 | irq->line_level = level; |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 405 | else |
| 406 | irq->pending_latch = true; |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 407 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 408 | vgic_queue_irq_unlock(kvm, irq, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 409 | vgic_put_irq(kvm, irq); |
Christoffer Dall | 81eeb95 | 2015-11-25 10:02:16 -0800 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 414 | /* @irq->irq_lock must be held */ |
| 415 | static int kvm_vgic_map_irq(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
| 416 | unsigned int host_irq) |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 417 | { |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 418 | struct irq_desc *desc; |
| 419 | struct irq_data *data; |
| 420 | |
| 421 | /* |
| 422 | * Find the physical IRQ number corresponding to @host_irq |
| 423 | */ |
| 424 | desc = irq_to_desc(host_irq); |
| 425 | if (!desc) { |
| 426 | kvm_err("%s: no interrupt descriptor\n", __func__); |
| 427 | return -EINVAL; |
| 428 | } |
| 429 | data = irq_desc_get_irq_data(desc); |
| 430 | while (data->parent_data) |
| 431 | data = data->parent_data; |
| 432 | |
| 433 | irq->hw = true; |
| 434 | irq->host_irq = host_irq; |
| 435 | irq->hwintid = data->hwirq; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* @irq->irq_lock must be held */ |
| 440 | static inline void kvm_vgic_unmap_irq(struct vgic_irq *irq) |
| 441 | { |
| 442 | irq->hw = false; |
| 443 | irq->hwintid = 0; |
| 444 | } |
| 445 | |
| 446 | int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq, |
| 447 | u32 vintid) |
| 448 | { |
| 449 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, vintid); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 450 | unsigned long flags; |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 451 | int ret; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 452 | |
| 453 | BUG_ON(!irq); |
| 454 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 455 | spin_lock_irqsave(&irq->irq_lock, flags); |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 456 | ret = kvm_vgic_map_irq(vcpu, irq, host_irq); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 457 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 458 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 459 | |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 460 | return ret; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 463 | int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid) |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 464 | { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 465 | struct vgic_irq *irq; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 466 | unsigned long flags; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 467 | |
| 468 | if (!vgic_initialized(vcpu->kvm)) |
| 469 | return -EAGAIN; |
| 470 | |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 471 | irq = vgic_get_irq(vcpu->kvm, vcpu, vintid); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 472 | BUG_ON(!irq); |
| 473 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 474 | spin_lock_irqsave(&irq->irq_lock, flags); |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 475 | kvm_vgic_unmap_irq(irq); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 476 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 477 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 482 | /** |
Christoffer Dall | c6ccd30 | 2017-05-04 13:24:20 +0200 | [diff] [blame] | 483 | * kvm_vgic_set_owner - Set the owner of an interrupt for a VM |
| 484 | * |
| 485 | * @vcpu: Pointer to the VCPU (used for PPIs) |
| 486 | * @intid: The virtual INTID identifying the interrupt (PPI or SPI) |
| 487 | * @owner: Opaque pointer to the owner |
| 488 | * |
| 489 | * Returns 0 if intid is not already used by another in-kernel device and the |
| 490 | * owner is set, otherwise returns an error code. |
| 491 | */ |
| 492 | int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) |
| 493 | { |
| 494 | struct vgic_irq *irq; |
Marc Zyngier | 7465894 | 2017-11-30 17:00:30 +0000 | [diff] [blame] | 495 | unsigned long flags; |
Christoffer Dall | c6ccd30 | 2017-05-04 13:24:20 +0200 | [diff] [blame] | 496 | int ret = 0; |
| 497 | |
| 498 | if (!vgic_initialized(vcpu->kvm)) |
| 499 | return -EAGAIN; |
| 500 | |
| 501 | /* SGIs and LPIs cannot be wired up to any device */ |
| 502 | if (!irq_is_ppi(intid) && !vgic_valid_spi(vcpu->kvm, intid)) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid); |
Marc Zyngier | 7465894 | 2017-11-30 17:00:30 +0000 | [diff] [blame] | 506 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | c6ccd30 | 2017-05-04 13:24:20 +0200 | [diff] [blame] | 507 | if (irq->owner && irq->owner != owner) |
| 508 | ret = -EEXIST; |
| 509 | else |
| 510 | irq->owner = owner; |
Marc Zyngier | 7465894 | 2017-11-30 17:00:30 +0000 | [diff] [blame] | 511 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Christoffer Dall | c6ccd30 | 2017-05-04 13:24:20 +0200 | [diff] [blame] | 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | /** |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 517 | * vgic_prune_ap_list - Remove non-relevant interrupts from the list |
| 518 | * |
| 519 | * @vcpu: The VCPU pointer |
| 520 | * |
| 521 | * Go over the list of "interesting" interrupts, and prune those that we |
| 522 | * won't have to consider in the near future. |
| 523 | */ |
| 524 | static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) |
| 525 | { |
| 526 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 527 | struct vgic_irq *irq, *tmp; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 528 | unsigned long flags; |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 529 | |
| 530 | retry: |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 531 | spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 532 | |
| 533 | list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) { |
| 534 | struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB; |
| 535 | |
| 536 | spin_lock(&irq->irq_lock); |
| 537 | |
| 538 | BUG_ON(vcpu != irq->vcpu); |
| 539 | |
| 540 | target_vcpu = vgic_target_oracle(irq); |
| 541 | |
| 542 | if (!target_vcpu) { |
| 543 | /* |
| 544 | * We don't need to process this interrupt any |
| 545 | * further, move it off the list. |
| 546 | */ |
| 547 | list_del(&irq->ap_list); |
| 548 | irq->vcpu = NULL; |
| 549 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * This vgic_put_irq call matches the |
| 553 | * vgic_get_irq_kref in vgic_queue_irq_unlock, |
| 554 | * where we added the LPI to the ap_list. As |
| 555 | * we remove the irq from the list, we drop |
| 556 | * also drop the refcount. |
| 557 | */ |
| 558 | vgic_put_irq(vcpu->kvm, irq); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 559 | continue; |
| 560 | } |
| 561 | |
| 562 | if (target_vcpu == vcpu) { |
| 563 | /* We're on the right CPU */ |
| 564 | spin_unlock(&irq->irq_lock); |
| 565 | continue; |
| 566 | } |
| 567 | |
| 568 | /* This interrupt looks like it has to be migrated. */ |
| 569 | |
| 570 | spin_unlock(&irq->irq_lock); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 571 | spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | * Ensure locking order by always locking the smallest |
| 575 | * ID first. |
| 576 | */ |
| 577 | if (vcpu->vcpu_id < target_vcpu->vcpu_id) { |
| 578 | vcpuA = vcpu; |
| 579 | vcpuB = target_vcpu; |
| 580 | } else { |
| 581 | vcpuA = target_vcpu; |
| 582 | vcpuB = vcpu; |
| 583 | } |
| 584 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 585 | spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 586 | spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock, |
| 587 | SINGLE_DEPTH_NESTING); |
| 588 | spin_lock(&irq->irq_lock); |
| 589 | |
| 590 | /* |
| 591 | * If the affinity has been preserved, move the |
| 592 | * interrupt around. Otherwise, it means things have |
| 593 | * changed while the interrupt was unlocked, and we |
| 594 | * need to replay this. |
| 595 | * |
| 596 | * In all cases, we cannot trust the list not to have |
| 597 | * changed, so we restart from the beginning. |
| 598 | */ |
| 599 | if (target_vcpu == vgic_target_oracle(irq)) { |
| 600 | struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu; |
| 601 | |
| 602 | list_del(&irq->ap_list); |
| 603 | irq->vcpu = target_vcpu; |
| 604 | list_add_tail(&irq->ap_list, &new_cpu->ap_list_head); |
| 605 | } |
| 606 | |
| 607 | spin_unlock(&irq->irq_lock); |
| 608 | spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 609 | spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 610 | goto retry; |
| 611 | } |
| 612 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 613 | spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 616 | static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu) |
| 617 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 618 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 619 | vgic_v2_fold_lr_state(vcpu); |
| 620 | else |
| 621 | vgic_v3_fold_lr_state(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* Requires the irq_lock to be held. */ |
| 625 | static inline void vgic_populate_lr(struct kvm_vcpu *vcpu, |
| 626 | struct vgic_irq *irq, int lr) |
| 627 | { |
| 628 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock)); |
Marc Zyngier | 140b086 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 629 | |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 630 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 631 | vgic_v2_populate_lr(vcpu, irq, lr); |
| 632 | else |
| 633 | vgic_v3_populate_lr(vcpu, irq, lr); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr) |
| 637 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 638 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 639 | vgic_v2_clear_lr(vcpu, lr); |
| 640 | else |
| 641 | vgic_v3_clear_lr(vcpu, lr); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static inline void vgic_set_underflow(struct kvm_vcpu *vcpu) |
| 645 | { |
Marc Zyngier | 59529f6 | 2015-11-30 13:09:53 +0000 | [diff] [blame] | 646 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 647 | vgic_v2_set_underflow(vcpu); |
| 648 | else |
| 649 | vgic_v3_set_underflow(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Requires the ap_list_lock to be held. */ |
| 653 | static int compute_ap_list_depth(struct kvm_vcpu *vcpu) |
| 654 | { |
| 655 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 656 | struct vgic_irq *irq; |
| 657 | int count = 0; |
| 658 | |
| 659 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 660 | |
| 661 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 662 | spin_lock(&irq->irq_lock); |
| 663 | /* GICv2 SGIs can count for more than one... */ |
| 664 | if (vgic_irq_is_sgi(irq->intid) && irq->source) |
| 665 | count += hweight8(irq->source); |
| 666 | else |
| 667 | count++; |
| 668 | spin_unlock(&irq->irq_lock); |
| 669 | } |
| 670 | return count; |
| 671 | } |
| 672 | |
| 673 | /* Requires the VCPU's ap_list_lock to be held. */ |
| 674 | static void vgic_flush_lr_state(struct kvm_vcpu *vcpu) |
| 675 | { |
| 676 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 677 | struct vgic_irq *irq; |
| 678 | int count = 0; |
| 679 | |
| 680 | DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock)); |
| 681 | |
Christoffer Dall | 90cac1f | 2017-03-21 21:16:12 +0100 | [diff] [blame] | 682 | if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 683 | vgic_sort_ap_list(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 684 | |
| 685 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 686 | spin_lock(&irq->irq_lock); |
| 687 | |
| 688 | if (unlikely(vgic_target_oracle(irq) != vcpu)) |
| 689 | goto next; |
| 690 | |
| 691 | /* |
| 692 | * If we get an SGI with multiple sources, try to get |
| 693 | * them in all at once. |
| 694 | */ |
| 695 | do { |
| 696 | vgic_populate_lr(vcpu, irq, count++); |
| 697 | } while (irq->source && count < kvm_vgic_global_state.nr_lr); |
| 698 | |
| 699 | next: |
| 700 | spin_unlock(&irq->irq_lock); |
| 701 | |
Christoffer Dall | 90cac1f | 2017-03-21 21:16:12 +0100 | [diff] [blame] | 702 | if (count == kvm_vgic_global_state.nr_lr) { |
| 703 | if (!list_is_last(&irq->ap_list, |
| 704 | &vgic_cpu->ap_list_head)) |
| 705 | vgic_set_underflow(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 706 | break; |
Christoffer Dall | 90cac1f | 2017-03-21 21:16:12 +0100 | [diff] [blame] | 707 | } |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | vcpu->arch.vgic_cpu.used_lrs = count; |
| 711 | |
| 712 | /* Nuke remaining LRs */ |
| 713 | for ( ; count < kvm_vgic_global_state.nr_lr; count++) |
| 714 | vgic_clear_lr(vcpu, count); |
| 715 | } |
| 716 | |
| 717 | /* Sync back the hardware VGIC state into our emulation after a guest's run. */ |
| 718 | void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) |
| 719 | { |
Shih-Wei Li | f676958 | 2016-10-19 18:12:34 +0000 | [diff] [blame] | 720 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 721 | |
Marc Zyngier | 6277579 | 2017-10-27 15:28:50 +0100 | [diff] [blame] | 722 | WARN_ON(vgic_v4_sync_hwstate(vcpu)); |
| 723 | |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 724 | /* An empty ap_list_head implies used_lrs == 0 */ |
| 725 | if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head)) |
Christoffer Dall | 0099b77 | 2016-09-27 18:53:35 +0200 | [diff] [blame] | 726 | return; |
| 727 | |
Christoffer Dall | 8ac76ef | 2017-03-18 13:48:42 +0100 | [diff] [blame] | 728 | if (vgic_cpu->used_lrs) |
| 729 | vgic_fold_lr_state(vcpu); |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 730 | vgic_prune_ap_list(vcpu); |
| 731 | } |
| 732 | |
| 733 | /* Flush our emulation state into the GIC hardware before entering the guest. */ |
| 734 | void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) |
| 735 | { |
Marc Zyngier | 6277579 | 2017-10-27 15:28:50 +0100 | [diff] [blame] | 736 | WARN_ON(vgic_v4_flush_hwstate(vcpu)); |
| 737 | |
Shih-Wei Li | f676958 | 2016-10-19 18:12:34 +0000 | [diff] [blame] | 738 | /* |
| 739 | * If there are no virtual interrupts active or pending for this |
| 740 | * VCPU, then there is no work to do and we can bail out without |
| 741 | * taking any lock. There is a potential race with someone injecting |
| 742 | * interrupts to the VCPU, but it is a benign race as the VCPU will |
| 743 | * either observe the new interrupt before or after doing this check, |
| 744 | * and introducing additional synchronization mechanism doesn't change |
| 745 | * this. |
| 746 | */ |
| 747 | if (list_empty(&vcpu->arch.vgic_cpu.ap_list_head)) |
Christoffer Dall | 0099b77 | 2016-09-27 18:53:35 +0200 | [diff] [blame] | 748 | return; |
| 749 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 750 | DEBUG_SPINLOCK_BUG_ON(!irqs_disabled()); |
| 751 | |
Marc Zyngier | 0919e84 | 2015-11-26 17:19:25 +0000 | [diff] [blame] | 752 | spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 753 | vgic_flush_lr_state(vcpu); |
| 754 | spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock); |
| 755 | } |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 756 | |
Christoffer Dall | 328e566 | 2016-03-24 11:21:04 +0100 | [diff] [blame] | 757 | void kvm_vgic_load(struct kvm_vcpu *vcpu) |
| 758 | { |
| 759 | if (unlikely(!vgic_initialized(vcpu->kvm))) |
| 760 | return; |
| 761 | |
| 762 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 763 | vgic_v2_load(vcpu); |
| 764 | else |
| 765 | vgic_v3_load(vcpu); |
| 766 | } |
| 767 | |
| 768 | void kvm_vgic_put(struct kvm_vcpu *vcpu) |
| 769 | { |
| 770 | if (unlikely(!vgic_initialized(vcpu->kvm))) |
| 771 | return; |
| 772 | |
| 773 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 774 | vgic_v2_put(vcpu); |
| 775 | else |
| 776 | vgic_v3_put(vcpu); |
| 777 | } |
| 778 | |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 779 | int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) |
| 780 | { |
| 781 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 782 | struct vgic_irq *irq; |
| 783 | bool pending = false; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 784 | unsigned long flags; |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 785 | |
| 786 | if (!vcpu->kvm->arch.vgic.enabled) |
| 787 | return false; |
| 788 | |
Marc Zyngier | c971968 | 2017-10-27 15:28:47 +0100 | [diff] [blame] | 789 | if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last) |
| 790 | return true; |
| 791 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 792 | spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags); |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 793 | |
| 794 | list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) { |
| 795 | spin_lock(&irq->irq_lock); |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 796 | pending = irq_is_pending(irq) && irq->enabled; |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 797 | spin_unlock(&irq->irq_lock); |
| 798 | |
| 799 | if (pending) |
| 800 | break; |
| 801 | } |
| 802 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 803 | spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags); |
Eric Auger | 90eee56 | 2015-12-07 15:30:38 +0000 | [diff] [blame] | 804 | |
| 805 | return pending; |
| 806 | } |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 807 | |
| 808 | void vgic_kick_vcpus(struct kvm *kvm) |
| 809 | { |
| 810 | struct kvm_vcpu *vcpu; |
| 811 | int c; |
| 812 | |
| 813 | /* |
| 814 | * We've injected an interrupt, time to find out who deserves |
| 815 | * a good kick... |
| 816 | */ |
| 817 | kvm_for_each_vcpu(c, vcpu, kvm) { |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 818 | if (kvm_vgic_vcpu_pending_irq(vcpu)) { |
| 819 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 820 | kvm_vcpu_kick(vcpu); |
Andrew Jones | 325f9c6 | 2017-06-04 14:43:59 +0200 | [diff] [blame] | 821 | } |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 822 | } |
| 823 | } |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 824 | |
Eric Auger | 47bbd31 | 2017-10-27 15:28:32 +0100 | [diff] [blame] | 825 | bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid) |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 826 | { |
Andre Przywara | 285a90e | 2017-11-17 17:58:21 +0000 | [diff] [blame] | 827 | struct vgic_irq *irq; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 828 | bool map_is_active; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 829 | unsigned long flags; |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 830 | |
Christoffer Dall | f39d16c | 2016-10-19 12:40:17 +0200 | [diff] [blame] | 831 | if (!vgic_initialized(vcpu->kvm)) |
| 832 | return false; |
| 833 | |
Andre Przywara | 285a90e | 2017-11-17 17:58:21 +0000 | [diff] [blame] | 834 | irq = vgic_get_irq(vcpu->kvm, vcpu, vintid); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 835 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 836 | map_is_active = irq->hw && irq->active; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 837 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 838 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 568e8c9 | 2015-12-22 00:52:33 +0000 | [diff] [blame] | 839 | |
| 840 | return map_is_active; |
| 841 | } |
Andre Przywara | 0e4e82f | 2016-07-15 12:43:38 +0100 | [diff] [blame] | 842 | |