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