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