Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 ARM Ltd. |
| 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 20 | #include <linux/kvm.h> |
| 21 | #include <linux/kvm_host.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/io.h> |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_irq.h> |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 27 | #include <linux/rculist.h> |
Christoffer Dall | 2a2f3e26 | 2014-02-02 13:41:02 -0800 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 29 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 30 | #include <asm/kvm_emulate.h> |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 31 | #include <asm/kvm_arm.h> |
| 32 | #include <asm/kvm_mmu.h> |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 33 | #include <trace/events/kvm.h> |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 34 | #include <asm/kvm.h> |
| 35 | #include <kvm/iodev.h> |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 36 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 37 | /* |
| 38 | * How the whole thing works (courtesy of Christoffer Dall): |
| 39 | * |
| 40 | * - At any time, the dist->irq_pending_on_cpu is the oracle that knows if |
Christoffer Dall | 7e36291 | 2014-06-14 22:34:04 +0200 | [diff] [blame] | 41 | * something is pending on the CPU interface. |
| 42 | * - Interrupts that are pending on the distributor are stored on the |
| 43 | * vgic.irq_pending vgic bitmap (this bitmap is updated by both user land |
| 44 | * ioctls and guest mmio ops, and other in-kernel peripherals such as the |
| 45 | * arch. timers). |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 46 | * - Every time the bitmap changes, the irq_pending_on_cpu oracle is |
| 47 | * recalculated |
| 48 | * - To calculate the oracle, we need info for each cpu from |
| 49 | * compute_pending_for_cpu, which considers: |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 50 | * - PPI: dist->irq_pending & dist->irq_enable |
| 51 | * - SPI: dist->irq_pending & dist->irq_enable & dist->irq_spi_target |
Christoffer Dall | 7e36291 | 2014-06-14 22:34:04 +0200 | [diff] [blame] | 52 | * - irq_spi_target is a 'formatted' version of the GICD_ITARGETSRn |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 53 | * registers, stored on each vcpu. We only keep one bit of |
| 54 | * information per interrupt, making sure that only one vcpu can |
| 55 | * accept the interrupt. |
Christoffer Dall | 7e36291 | 2014-06-14 22:34:04 +0200 | [diff] [blame] | 56 | * - If any of the above state changes, we must recalculate the oracle. |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 57 | * - The same is true when injecting an interrupt, except that we only |
| 58 | * consider a single interrupt at a time. The irq_spi_cpu array |
| 59 | * contains the target CPU for each SPI. |
| 60 | * |
| 61 | * The handling of level interrupts adds some extra complexity. We |
| 62 | * need to track when the interrupt has been EOIed, so we can sample |
| 63 | * the 'line' again. This is achieved as such: |
| 64 | * |
| 65 | * - When a level interrupt is moved onto a vcpu, the corresponding |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 66 | * bit in irq_queued is set. As long as this bit is set, the line |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 67 | * will be ignored for further interrupts. The interrupt is injected |
| 68 | * into the vcpu with the GICH_LR_EOI bit set (generate a |
| 69 | * maintenance interrupt on EOI). |
| 70 | * - When the interrupt is EOIed, the maintenance interrupt fires, |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 71 | * and clears the corresponding bit in irq_queued. This allows the |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 72 | * interrupt line to be sampled again. |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 73 | * - Note that level-triggered interrupts can also be set to pending from |
| 74 | * writes to GICD_ISPENDRn and lowering the external input line does not |
| 75 | * cause the interrupt to become inactive in such a situation. |
| 76 | * Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become |
| 77 | * inactive as long as the external input line is held high. |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 78 | * |
| 79 | * |
| 80 | * Initialization rules: there are multiple stages to the vgic |
| 81 | * initialization, both for the distributor and the CPU interfaces. |
| 82 | * |
| 83 | * Distributor: |
| 84 | * |
| 85 | * - kvm_vgic_early_init(): initialization of static data that doesn't |
| 86 | * depend on any sizing information or emulation type. No allocation |
| 87 | * is allowed there. |
| 88 | * |
| 89 | * - vgic_init(): allocation and initialization of the generic data |
| 90 | * structures that depend on sizing information (number of CPUs, |
| 91 | * number of interrupts). Also initializes the vcpu specific data |
| 92 | * structures. Can be executed lazily for GICv2. |
| 93 | * [to be renamed to kvm_vgic_init??] |
| 94 | * |
| 95 | * CPU Interface: |
| 96 | * |
| 97 | * - kvm_vgic_cpu_early_init(): initialization of static data that |
| 98 | * doesn't depend on any sizing information or emulation type. No |
| 99 | * allocation is allowed there. |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 100 | */ |
| 101 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 102 | #include "vgic.h" |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 103 | |
Marc Zyngier | a1fcb44 | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 104 | static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 105 | static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 106 | static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr); |
| 107 | static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, struct vgic_lr lr_desc); |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 108 | static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu, |
| 109 | int virt_irq); |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 110 | static int compute_pending_for_cpu(struct kvm_vcpu *vcpu); |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 111 | |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 112 | static const struct vgic_ops *vgic_ops; |
| 113 | static const struct vgic_params *vgic; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 114 | |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 115 | static void add_sgi_source(struct kvm_vcpu *vcpu, int irq, int source) |
| 116 | { |
| 117 | vcpu->kvm->arch.vgic.vm_ops.add_sgi_source(vcpu, irq, source); |
| 118 | } |
| 119 | |
| 120 | static bool queue_sgi(struct kvm_vcpu *vcpu, int irq) |
| 121 | { |
| 122 | return vcpu->kvm->arch.vgic.vm_ops.queue_sgi(vcpu, irq); |
| 123 | } |
| 124 | |
| 125 | int kvm_vgic_map_resources(struct kvm *kvm) |
| 126 | { |
| 127 | return kvm->arch.vgic.vm_ops.map_resources(kvm, vgic); |
| 128 | } |
| 129 | |
Victor Kamensky | 9662fb4 | 2014-06-12 09:30:10 -0700 | [diff] [blame] | 130 | /* |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 131 | * struct vgic_bitmap contains a bitmap made of unsigned longs, but |
| 132 | * extracts u32s out of them. |
Victor Kamensky | 9662fb4 | 2014-06-12 09:30:10 -0700 | [diff] [blame] | 133 | * |
| 134 | * This does not work on 64-bit BE systems, because the bitmap access |
| 135 | * will store two consecutive 32-bit words with the higher-addressed |
| 136 | * register's bits at the lower index and the lower-addressed register's |
| 137 | * bits at the higher index. |
| 138 | * |
| 139 | * Therefore, swizzle the register index when accessing the 32-bit word |
| 140 | * registers to access the right register's value. |
| 141 | */ |
| 142 | #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 64 |
| 143 | #define REG_OFFSET_SWIZZLE 1 |
| 144 | #else |
| 145 | #define REG_OFFSET_SWIZZLE 0 |
| 146 | #endif |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 147 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 148 | static int vgic_init_bitmap(struct vgic_bitmap *b, int nr_cpus, int nr_irqs) |
| 149 | { |
| 150 | int nr_longs; |
| 151 | |
| 152 | nr_longs = nr_cpus + BITS_TO_LONGS(nr_irqs - VGIC_NR_PRIVATE_IRQS); |
| 153 | |
| 154 | b->private = kzalloc(sizeof(unsigned long) * nr_longs, GFP_KERNEL); |
| 155 | if (!b->private) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | b->shared = b->private + nr_cpus; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static void vgic_free_bitmap(struct vgic_bitmap *b) |
| 164 | { |
| 165 | kfree(b->private); |
| 166 | b->private = NULL; |
| 167 | b->shared = NULL; |
| 168 | } |
| 169 | |
Christoffer Dall | 2df36a5 | 2014-09-28 16:04:26 +0200 | [diff] [blame] | 170 | /* |
| 171 | * Call this function to convert a u64 value to an unsigned long * bitmask |
| 172 | * in a way that works on both 32-bit and 64-bit LE and BE platforms. |
| 173 | * |
| 174 | * Warning: Calling this function may modify *val. |
| 175 | */ |
| 176 | static unsigned long *u64_to_bitmask(u64 *val) |
| 177 | { |
| 178 | #if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32 |
| 179 | *val = (*val >> 32) | (*val << 32); |
| 180 | #endif |
| 181 | return (unsigned long *)val; |
| 182 | } |
| 183 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 184 | u32 *vgic_bitmap_get_reg(struct vgic_bitmap *x, int cpuid, u32 offset) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 185 | { |
| 186 | offset >>= 2; |
| 187 | if (!offset) |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 188 | return (u32 *)(x->private + cpuid) + REG_OFFSET_SWIZZLE; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 189 | else |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 190 | return (u32 *)(x->shared) + ((offset - 1) ^ REG_OFFSET_SWIZZLE); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static int vgic_bitmap_get_irq_val(struct vgic_bitmap *x, |
| 194 | int cpuid, int irq) |
| 195 | { |
| 196 | if (irq < VGIC_NR_PRIVATE_IRQS) |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 197 | return test_bit(irq, x->private + cpuid); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 198 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 199 | return test_bit(irq - VGIC_NR_PRIVATE_IRQS, x->shared); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 202 | void vgic_bitmap_set_irq_val(struct vgic_bitmap *x, int cpuid, |
| 203 | int irq, int val) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 204 | { |
| 205 | unsigned long *reg; |
| 206 | |
| 207 | if (irq < VGIC_NR_PRIVATE_IRQS) { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 208 | reg = x->private + cpuid; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 209 | } else { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 210 | reg = x->shared; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 211 | irq -= VGIC_NR_PRIVATE_IRQS; |
| 212 | } |
| 213 | |
| 214 | if (val) |
| 215 | set_bit(irq, reg); |
| 216 | else |
| 217 | clear_bit(irq, reg); |
| 218 | } |
| 219 | |
| 220 | static unsigned long *vgic_bitmap_get_cpu_map(struct vgic_bitmap *x, int cpuid) |
| 221 | { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 222 | return x->private + cpuid; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 225 | unsigned long *vgic_bitmap_get_shared_map(struct vgic_bitmap *x) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 226 | { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 227 | return x->shared; |
| 228 | } |
| 229 | |
| 230 | static int vgic_init_bytemap(struct vgic_bytemap *x, int nr_cpus, int nr_irqs) |
| 231 | { |
| 232 | int size; |
| 233 | |
| 234 | size = nr_cpus * VGIC_NR_PRIVATE_IRQS; |
| 235 | size += nr_irqs - VGIC_NR_PRIVATE_IRQS; |
| 236 | |
| 237 | x->private = kzalloc(size, GFP_KERNEL); |
| 238 | if (!x->private) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | x->shared = x->private + nr_cpus * VGIC_NR_PRIVATE_IRQS / sizeof(u32); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static void vgic_free_bytemap(struct vgic_bytemap *b) |
| 246 | { |
| 247 | kfree(b->private); |
| 248 | b->private = NULL; |
| 249 | b->shared = NULL; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 252 | u32 *vgic_bytemap_get_reg(struct vgic_bytemap *x, int cpuid, u32 offset) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 253 | { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 254 | u32 *reg; |
| 255 | |
| 256 | if (offset < VGIC_NR_PRIVATE_IRQS) { |
| 257 | reg = x->private; |
| 258 | offset += cpuid * VGIC_NR_PRIVATE_IRQS; |
| 259 | } else { |
| 260 | reg = x->shared; |
| 261 | offset -= VGIC_NR_PRIVATE_IRQS; |
| 262 | } |
| 263 | |
| 264 | return reg + (offset / sizeof(u32)); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | #define VGIC_CFG_LEVEL 0 |
| 268 | #define VGIC_CFG_EDGE 1 |
| 269 | |
| 270 | static bool vgic_irq_is_edge(struct kvm_vcpu *vcpu, int irq) |
| 271 | { |
| 272 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 273 | int irq_val; |
| 274 | |
| 275 | irq_val = vgic_bitmap_get_irq_val(&dist->irq_cfg, vcpu->vcpu_id, irq); |
| 276 | return irq_val == VGIC_CFG_EDGE; |
| 277 | } |
| 278 | |
| 279 | static int vgic_irq_is_enabled(struct kvm_vcpu *vcpu, int irq) |
| 280 | { |
| 281 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 282 | |
| 283 | return vgic_bitmap_get_irq_val(&dist->irq_enabled, vcpu->vcpu_id, irq); |
| 284 | } |
| 285 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 286 | static int vgic_irq_is_queued(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 287 | { |
| 288 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 289 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 290 | return vgic_bitmap_get_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 293 | static int vgic_irq_is_active(struct kvm_vcpu *vcpu, int irq) |
| 294 | { |
| 295 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 296 | |
| 297 | return vgic_bitmap_get_irq_val(&dist->irq_active, vcpu->vcpu_id, irq); |
| 298 | } |
| 299 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 300 | static void vgic_irq_set_queued(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 301 | { |
| 302 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 303 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 304 | vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 1); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 307 | static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 308 | { |
| 309 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 310 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 311 | vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 312 | } |
| 313 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 314 | static void vgic_irq_set_active(struct kvm_vcpu *vcpu, int irq) |
| 315 | { |
| 316 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 317 | |
| 318 | vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 1); |
| 319 | } |
| 320 | |
| 321 | static void vgic_irq_clear_active(struct kvm_vcpu *vcpu, int irq) |
| 322 | { |
| 323 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 324 | |
| 325 | vgic_bitmap_set_irq_val(&dist->irq_active, vcpu->vcpu_id, irq, 0); |
| 326 | } |
| 327 | |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 328 | static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq) |
| 329 | { |
| 330 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 331 | |
| 332 | return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq); |
| 333 | } |
| 334 | |
| 335 | static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq) |
| 336 | { |
| 337 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 338 | |
| 339 | vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1); |
| 340 | } |
| 341 | |
| 342 | static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq) |
| 343 | { |
| 344 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 345 | |
| 346 | vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0); |
| 347 | } |
| 348 | |
| 349 | static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq) |
| 350 | { |
| 351 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 352 | |
| 353 | return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq); |
| 354 | } |
| 355 | |
| 356 | static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq) |
| 357 | { |
| 358 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 359 | |
| 360 | vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0); |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 361 | if (!vgic_dist_irq_get_level(vcpu, irq)) { |
| 362 | vgic_dist_irq_clear_pending(vcpu, irq); |
| 363 | if (!compute_pending_for_cpu(vcpu)) |
| 364 | clear_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); |
| 365 | } |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 368 | static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq) |
| 369 | { |
| 370 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 371 | |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 372 | return vgic_bitmap_get_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 375 | void vgic_dist_irq_set_pending(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 376 | { |
| 377 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 378 | |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 379 | vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 1); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 382 | void vgic_dist_irq_clear_pending(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 383 | { |
| 384 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 385 | |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 386 | vgic_bitmap_set_irq_val(&dist->irq_pending, vcpu->vcpu_id, irq, 0); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void vgic_cpu_irq_set(struct kvm_vcpu *vcpu, int irq) |
| 390 | { |
| 391 | if (irq < VGIC_NR_PRIVATE_IRQS) |
| 392 | set_bit(irq, vcpu->arch.vgic_cpu.pending_percpu); |
| 393 | else |
| 394 | set_bit(irq - VGIC_NR_PRIVATE_IRQS, |
| 395 | vcpu->arch.vgic_cpu.pending_shared); |
| 396 | } |
| 397 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 398 | void vgic_cpu_irq_clear(struct kvm_vcpu *vcpu, int irq) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 399 | { |
| 400 | if (irq < VGIC_NR_PRIVATE_IRQS) |
| 401 | clear_bit(irq, vcpu->arch.vgic_cpu.pending_percpu); |
| 402 | else |
| 403 | clear_bit(irq - VGIC_NR_PRIVATE_IRQS, |
| 404 | vcpu->arch.vgic_cpu.pending_shared); |
| 405 | } |
| 406 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 407 | static bool vgic_can_sample_irq(struct kvm_vcpu *vcpu, int irq) |
| 408 | { |
Marc Zyngier | 7a67b4b | 2015-06-05 16:45:29 +0100 | [diff] [blame] | 409 | return !vgic_irq_is_queued(vcpu, irq); |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 412 | /** |
| 413 | * vgic_reg_access - access vgic register |
| 414 | * @mmio: pointer to the data describing the mmio access |
| 415 | * @reg: pointer to the virtual backing of vgic distributor data |
| 416 | * @offset: least significant 2 bits used for word offset |
| 417 | * @mode: ACCESS_ mode (see defines above) |
| 418 | * |
| 419 | * Helper to make vgic register access easier using one of the access |
| 420 | * modes defined for vgic register access |
| 421 | * (read,raz,write-ignored,setbit,clearbit,write) |
| 422 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 423 | void vgic_reg_access(struct kvm_exit_mmio *mmio, u32 *reg, |
| 424 | phys_addr_t offset, int mode) |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 425 | { |
| 426 | int word_offset = (offset & 3) * 8; |
| 427 | u32 mask = (1UL << (mmio->len * 8)) - 1; |
| 428 | u32 regval; |
| 429 | |
| 430 | /* |
| 431 | * Any alignment fault should have been delivered to the guest |
| 432 | * directly (ARM ARM B3.12.7 "Prioritization of aborts"). |
| 433 | */ |
| 434 | |
| 435 | if (reg) { |
| 436 | regval = *reg; |
| 437 | } else { |
| 438 | BUG_ON(mode != (ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED)); |
| 439 | regval = 0; |
| 440 | } |
| 441 | |
| 442 | if (mmio->is_write) { |
| 443 | u32 data = mmio_data_read(mmio, mask) << word_offset; |
| 444 | switch (ACCESS_WRITE_MASK(mode)) { |
| 445 | case ACCESS_WRITE_IGNORED: |
| 446 | return; |
| 447 | |
| 448 | case ACCESS_WRITE_SETBIT: |
| 449 | regval |= data; |
| 450 | break; |
| 451 | |
| 452 | case ACCESS_WRITE_CLEARBIT: |
| 453 | regval &= ~data; |
| 454 | break; |
| 455 | |
| 456 | case ACCESS_WRITE_VALUE: |
| 457 | regval = (regval & ~(mask << word_offset)) | data; |
| 458 | break; |
| 459 | } |
| 460 | *reg = regval; |
| 461 | } else { |
| 462 | switch (ACCESS_READ_MASK(mode)) { |
| 463 | case ACCESS_READ_RAZ: |
| 464 | regval = 0; |
| 465 | /* fall through */ |
| 466 | |
| 467 | case ACCESS_READ_VALUE: |
| 468 | mmio_data_write(mmio, mask, regval >> word_offset); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 473 | bool handle_mmio_raz_wi(struct kvm_vcpu *vcpu, struct kvm_exit_mmio *mmio, |
| 474 | phys_addr_t offset) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 475 | { |
| 476 | vgic_reg_access(mmio, NULL, offset, |
| 477 | ACCESS_READ_RAZ | ACCESS_WRITE_IGNORED); |
| 478 | return false; |
| 479 | } |
| 480 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 481 | bool vgic_handle_enable_reg(struct kvm *kvm, struct kvm_exit_mmio *mmio, |
| 482 | phys_addr_t offset, int vcpu_id, int access) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 483 | { |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 484 | u32 *reg; |
| 485 | int mode = ACCESS_READ_VALUE | access; |
| 486 | struct kvm_vcpu *target_vcpu = kvm_get_vcpu(kvm, vcpu_id); |
| 487 | |
| 488 | reg = vgic_bitmap_get_reg(&kvm->arch.vgic.irq_enabled, vcpu_id, offset); |
| 489 | vgic_reg_access(mmio, reg, offset, mode); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 490 | if (mmio->is_write) { |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 491 | if (access & ACCESS_WRITE_CLEARBIT) { |
| 492 | if (offset < 4) /* Force SGI enabled */ |
| 493 | *reg |= 0xffff; |
| 494 | vgic_retire_disabled_irqs(target_vcpu); |
| 495 | } |
| 496 | vgic_update_state(kvm); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 497 | return true; |
| 498 | } |
| 499 | |
| 500 | return false; |
| 501 | } |
| 502 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 503 | bool vgic_handle_set_pending_reg(struct kvm *kvm, |
| 504 | struct kvm_exit_mmio *mmio, |
| 505 | phys_addr_t offset, int vcpu_id) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 506 | { |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 507 | u32 *reg, orig; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 508 | u32 level_mask; |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 509 | int mode = ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT; |
| 510 | struct vgic_dist *dist = &kvm->arch.vgic; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 511 | |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 512 | reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu_id, offset); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 513 | level_mask = (~(*reg)); |
| 514 | |
| 515 | /* Mark both level and edge triggered irqs as pending */ |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 516 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 517 | orig = *reg; |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 518 | vgic_reg_access(mmio, reg, offset, mode); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 519 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 520 | if (mmio->is_write) { |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 521 | /* Set the soft-pending flag only for level-triggered irqs */ |
| 522 | reg = vgic_bitmap_get_reg(&dist->irq_soft_pend, |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 523 | vcpu_id, offset); |
| 524 | vgic_reg_access(mmio, reg, offset, mode); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 525 | *reg &= level_mask; |
| 526 | |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 527 | /* Ignore writes to SGIs */ |
| 528 | if (offset < 2) { |
| 529 | *reg &= ~0xffff; |
| 530 | *reg |= orig & 0xffff; |
| 531 | } |
| 532 | |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 533 | vgic_update_state(kvm); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 534 | return true; |
| 535 | } |
| 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
Christoffer Dall | 544c572 | 2015-10-17 17:55:12 +0200 | [diff] [blame] | 540 | /* |
| 541 | * If a mapped interrupt's state has been modified by the guest such that it |
| 542 | * is no longer active or pending, without it have gone through the sync path, |
| 543 | * then the map->active field must be cleared so the interrupt can be taken |
| 544 | * again. |
| 545 | */ |
| 546 | static void vgic_handle_clear_mapped_irq(struct kvm_vcpu *vcpu) |
| 547 | { |
| 548 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 549 | struct list_head *root; |
| 550 | struct irq_phys_map_entry *entry; |
| 551 | struct irq_phys_map *map; |
| 552 | |
| 553 | rcu_read_lock(); |
| 554 | |
| 555 | /* Check for PPIs */ |
| 556 | root = &vgic_cpu->irq_phys_map_list; |
| 557 | list_for_each_entry_rcu(entry, root, entry) { |
| 558 | map = &entry->map; |
| 559 | |
| 560 | if (!vgic_dist_irq_is_pending(vcpu, map->virt_irq) && |
| 561 | !vgic_irq_is_active(vcpu, map->virt_irq)) |
| 562 | map->active = false; |
| 563 | } |
| 564 | |
| 565 | rcu_read_unlock(); |
| 566 | } |
| 567 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 568 | bool vgic_handle_clear_pending_reg(struct kvm *kvm, |
| 569 | struct kvm_exit_mmio *mmio, |
| 570 | phys_addr_t offset, int vcpu_id) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 571 | { |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 572 | u32 *level_active; |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 573 | u32 *reg, orig; |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 574 | int mode = ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT; |
| 575 | struct vgic_dist *dist = &kvm->arch.vgic; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 576 | |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 577 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 578 | orig = *reg; |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 579 | vgic_reg_access(mmio, reg, offset, mode); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 580 | if (mmio->is_write) { |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 581 | /* Re-set level triggered level-active interrupts */ |
| 582 | level_active = vgic_bitmap_get_reg(&dist->irq_level, |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 583 | vcpu_id, offset); |
| 584 | reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu_id, offset); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 585 | *reg |= *level_active; |
| 586 | |
Christoffer Dall | 9da48b5 | 2014-06-14 22:30:45 +0200 | [diff] [blame] | 587 | /* Ignore writes to SGIs */ |
| 588 | if (offset < 2) { |
| 589 | *reg &= ~0xffff; |
| 590 | *reg |= orig & 0xffff; |
| 591 | } |
| 592 | |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 593 | /* Clear soft-pending flags */ |
| 594 | reg = vgic_bitmap_get_reg(&dist->irq_soft_pend, |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 595 | vcpu_id, offset); |
| 596 | vgic_reg_access(mmio, reg, offset, mode); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 597 | |
Christoffer Dall | 544c572 | 2015-10-17 17:55:12 +0200 | [diff] [blame] | 598 | vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id)); |
Andre Przywara | d97f683 | 2014-06-11 14:11:49 +0200 | [diff] [blame] | 599 | vgic_update_state(kvm); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 600 | return true; |
| 601 | } |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 602 | return false; |
| 603 | } |
| 604 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 605 | bool vgic_handle_set_active_reg(struct kvm *kvm, |
| 606 | struct kvm_exit_mmio *mmio, |
| 607 | phys_addr_t offset, int vcpu_id) |
| 608 | { |
| 609 | u32 *reg; |
| 610 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 611 | |
| 612 | reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset); |
| 613 | vgic_reg_access(mmio, reg, offset, |
| 614 | ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT); |
| 615 | |
| 616 | if (mmio->is_write) { |
| 617 | vgic_update_state(kvm); |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | bool vgic_handle_clear_active_reg(struct kvm *kvm, |
| 625 | struct kvm_exit_mmio *mmio, |
| 626 | phys_addr_t offset, int vcpu_id) |
| 627 | { |
| 628 | u32 *reg; |
| 629 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 630 | |
| 631 | reg = vgic_bitmap_get_reg(&dist->irq_active, vcpu_id, offset); |
| 632 | vgic_reg_access(mmio, reg, offset, |
| 633 | ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT); |
| 634 | |
| 635 | if (mmio->is_write) { |
Christoffer Dall | 544c572 | 2015-10-17 17:55:12 +0200 | [diff] [blame] | 636 | vgic_handle_clear_mapped_irq(kvm_get_vcpu(kvm, vcpu_id)); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 637 | vgic_update_state(kvm); |
| 638 | return true; |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 644 | static u32 vgic_cfg_expand(u16 val) |
| 645 | { |
| 646 | u32 res = 0; |
| 647 | int i; |
| 648 | |
| 649 | /* |
| 650 | * Turn a 16bit value like abcd...mnop into a 32bit word |
| 651 | * a0b0c0d0...m0n0o0p0, which is what the HW cfg register is. |
| 652 | */ |
| 653 | for (i = 0; i < 16; i++) |
| 654 | res |= ((val >> i) & VGIC_CFG_EDGE) << (2 * i + 1); |
| 655 | |
| 656 | return res; |
| 657 | } |
| 658 | |
| 659 | static u16 vgic_cfg_compress(u32 val) |
| 660 | { |
| 661 | u16 res = 0; |
| 662 | int i; |
| 663 | |
| 664 | /* |
| 665 | * Turn a 32bit word a0b0c0d0...m0n0o0p0 into 16bit value like |
| 666 | * abcd...mnop which is what we really care about. |
| 667 | */ |
| 668 | for (i = 0; i < 16; i++) |
| 669 | res |= ((val >> (i * 2 + 1)) & VGIC_CFG_EDGE) << i; |
| 670 | |
| 671 | return res; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * The distributor uses 2 bits per IRQ for the CFG register, but the |
| 676 | * LSB is always 0. As such, we only keep the upper bit, and use the |
| 677 | * two above functions to compress/expand the bits |
| 678 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 679 | bool vgic_handle_cfg_reg(u32 *reg, struct kvm_exit_mmio *mmio, |
| 680 | phys_addr_t offset) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 681 | { |
| 682 | u32 val; |
Marc Zyngier | 6545eae | 2013-08-29 11:08:23 +0100 | [diff] [blame] | 683 | |
Andre Przywara | f2ae85b | 2014-04-11 00:07:18 +0200 | [diff] [blame] | 684 | if (offset & 4) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 685 | val = *reg >> 16; |
| 686 | else |
| 687 | val = *reg & 0xffff; |
| 688 | |
| 689 | val = vgic_cfg_expand(val); |
| 690 | vgic_reg_access(mmio, &val, offset, |
| 691 | ACCESS_READ_VALUE | ACCESS_WRITE_VALUE); |
| 692 | if (mmio->is_write) { |
Christoffer Dall | 8bf9a70 | 2015-08-30 14:42:16 +0200 | [diff] [blame] | 693 | /* Ignore writes to read-only SGI and PPI bits */ |
| 694 | if (offset < 8) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 695 | return false; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 696 | |
| 697 | val = vgic_cfg_compress(val); |
Andre Przywara | f2ae85b | 2014-04-11 00:07:18 +0200 | [diff] [blame] | 698 | if (offset & 4) { |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 699 | *reg &= 0xffff; |
| 700 | *reg |= val << 16; |
| 701 | } else { |
| 702 | *reg &= 0xffff << 16; |
| 703 | *reg |= val; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return false; |
| 708 | } |
| 709 | |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 710 | /** |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 711 | * vgic_unqueue_irqs - move pending/active IRQs from LRs to the distributor |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 712 | * @vgic_cpu: Pointer to the vgic_cpu struct holding the LRs |
| 713 | * |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 714 | * Move any IRQs that have already been assigned to LRs back to the |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 715 | * emulated distributor state so that the complete emulated state can be read |
| 716 | * from the main emulation structures without investigating the LRs. |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 717 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 718 | void vgic_unqueue_irqs(struct kvm_vcpu *vcpu) |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 719 | { |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 720 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 721 | int i; |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 722 | |
| 723 | for_each_set_bit(i, vgic_cpu->lr_used, vgic_cpu->nr_lr) { |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 724 | struct vgic_lr lr = vgic_get_lr(vcpu, i); |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 725 | |
| 726 | /* |
| 727 | * There are three options for the state bits: |
| 728 | * |
| 729 | * 01: pending |
| 730 | * 10: active |
| 731 | * 11: pending and active |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 732 | */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 733 | BUG_ON(!(lr.state & LR_STATE_MASK)); |
| 734 | |
| 735 | /* Reestablish SGI source for pending and active IRQs */ |
| 736 | if (lr.irq < VGIC_NR_SGIS) |
| 737 | add_sgi_source(vcpu, lr.irq, lr.source); |
| 738 | |
| 739 | /* |
| 740 | * If the LR holds an active (10) or a pending and active (11) |
| 741 | * interrupt then move the active state to the |
| 742 | * distributor tracking bit. |
| 743 | */ |
| 744 | if (lr.state & LR_STATE_ACTIVE) { |
| 745 | vgic_irq_set_active(vcpu, lr.irq); |
| 746 | lr.state &= ~LR_STATE_ACTIVE; |
| 747 | } |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * Reestablish the pending state on the distributor and the |
| 751 | * CPU interface. It may have already been pending, but that |
| 752 | * is fine, then we are only setting a few bits that were |
| 753 | * already set. |
| 754 | */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 755 | if (lr.state & LR_STATE_PENDING) { |
| 756 | vgic_dist_irq_set_pending(vcpu, lr.irq); |
| 757 | lr.state &= ~LR_STATE_PENDING; |
| 758 | } |
| 759 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 760 | vgic_set_lr(vcpu, i, lr); |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 761 | |
| 762 | /* |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 763 | * Mark the LR as free for other use. |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 764 | */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 765 | BUG_ON(lr.state & LR_STATE_MASK); |
| 766 | vgic_retire_lr(i, lr.irq, vcpu); |
| 767 | vgic_irq_clear_queued(vcpu, lr.irq); |
Christoffer Dall | cbd333a | 2013-11-15 20:51:31 -0800 | [diff] [blame] | 768 | |
| 769 | /* Finally update the VGIC state. */ |
| 770 | vgic_update_state(vcpu->kvm); |
| 771 | } |
| 772 | } |
| 773 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 774 | const |
Andre Przywara | cf50a1e | 2015-03-26 14:39:32 +0000 | [diff] [blame] | 775 | struct vgic_io_range *vgic_find_range(const struct vgic_io_range *ranges, |
Andre Przywara | 9f199d0 | 2015-03-26 14:39:33 +0000 | [diff] [blame] | 776 | int len, gpa_t offset) |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 777 | { |
Andre Przywara | 9f199d0 | 2015-03-26 14:39:33 +0000 | [diff] [blame] | 778 | while (ranges->len) { |
| 779 | if (offset >= ranges->base && |
| 780 | (offset + len) <= (ranges->base + ranges->len)) |
| 781 | return ranges; |
| 782 | ranges++; |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | return NULL; |
| 786 | } |
| 787 | |
Marc Zyngier | c3c9183 | 2014-07-08 12:09:04 +0100 | [diff] [blame] | 788 | static bool vgic_validate_access(const struct vgic_dist *dist, |
Andre Przywara | cf50a1e | 2015-03-26 14:39:32 +0000 | [diff] [blame] | 789 | const struct vgic_io_range *range, |
Marc Zyngier | c3c9183 | 2014-07-08 12:09:04 +0100 | [diff] [blame] | 790 | unsigned long offset) |
| 791 | { |
| 792 | int irq; |
| 793 | |
| 794 | if (!range->bits_per_irq) |
| 795 | return true; /* Not an irq-based access */ |
| 796 | |
| 797 | irq = offset * 8 / range->bits_per_irq; |
| 798 | if (irq >= dist->nr_irqs) |
| 799 | return false; |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 804 | /* |
| 805 | * Call the respective handler function for the given range. |
| 806 | * We split up any 64 bit accesses into two consecutive 32 bit |
| 807 | * handler calls and merge the result afterwards. |
| 808 | * We do this in a little endian fashion regardless of the host's |
| 809 | * or guest's endianness, because the GIC is always LE and the rest of |
| 810 | * the code (vgic_reg_access) also puts it in a LE fashion already. |
| 811 | * At this point we have already identified the handle function, so |
| 812 | * range points to that one entry and offset is relative to this. |
| 813 | */ |
| 814 | static bool call_range_handler(struct kvm_vcpu *vcpu, |
| 815 | struct kvm_exit_mmio *mmio, |
| 816 | unsigned long offset, |
Andre Przywara | cf50a1e | 2015-03-26 14:39:32 +0000 | [diff] [blame] | 817 | const struct vgic_io_range *range) |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 818 | { |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 819 | struct kvm_exit_mmio mmio32; |
| 820 | bool ret; |
| 821 | |
| 822 | if (likely(mmio->len <= 4)) |
| 823 | return range->handle_mmio(vcpu, mmio, offset); |
| 824 | |
| 825 | /* |
| 826 | * Any access bigger than 4 bytes (that we currently handle in KVM) |
| 827 | * is actually 8 bytes long, caused by a 64-bit access |
| 828 | */ |
| 829 | |
| 830 | mmio32.len = 4; |
| 831 | mmio32.is_write = mmio->is_write; |
Andre Przywara | 9fedf14 | 2014-11-13 16:21:35 +0000 | [diff] [blame] | 832 | mmio32.private = mmio->private; |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 833 | |
| 834 | mmio32.phys_addr = mmio->phys_addr + 4; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 835 | mmio32.data = &((u32 *)mmio->data)[1]; |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 836 | ret = range->handle_mmio(vcpu, &mmio32, offset + 4); |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 837 | |
| 838 | mmio32.phys_addr = mmio->phys_addr; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 839 | mmio32.data = &((u32 *)mmio->data)[0]; |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 840 | ret |= range->handle_mmio(vcpu, &mmio32, offset); |
Andre Przywara | 05bc8aa | 2014-06-05 16:07:50 +0200 | [diff] [blame] | 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 845 | /** |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 846 | * vgic_handle_mmio_access - handle an in-kernel MMIO access |
| 847 | * This is called by the read/write KVM IO device wrappers below. |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 848 | * @vcpu: pointer to the vcpu performing the access |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 849 | * @this: pointer to the KVM IO device in charge |
| 850 | * @addr: guest physical address of the access |
| 851 | * @len: size of the access |
| 852 | * @val: pointer to the data region |
| 853 | * @is_write: read or write access |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 854 | * |
Andre Przywara | 9641525 | 2014-06-02 22:44:37 +0200 | [diff] [blame] | 855 | * returns true if the MMIO access could be performed |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 856 | */ |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 857 | static int vgic_handle_mmio_access(struct kvm_vcpu *vcpu, |
| 858 | struct kvm_io_device *this, gpa_t addr, |
| 859 | int len, void *val, bool is_write) |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 860 | { |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 861 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 862 | struct vgic_io_device *iodev = container_of(this, |
| 863 | struct vgic_io_device, dev); |
| 864 | struct kvm_run *run = vcpu->run; |
| 865 | const struct vgic_io_range *range; |
| 866 | struct kvm_exit_mmio mmio; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 867 | bool updated_state; |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 868 | gpa_t offset; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 869 | |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 870 | offset = addr - iodev->addr; |
| 871 | range = vgic_find_range(iodev->reg_ranges, len, offset); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 872 | if (unlikely(!range || !range->handle_mmio)) { |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 873 | pr_warn("Unhandled access %d %08llx %d\n", is_write, addr, len); |
| 874 | return -ENXIO; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 875 | } |
| 876 | |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 877 | mmio.phys_addr = addr; |
| 878 | mmio.len = len; |
| 879 | mmio.is_write = is_write; |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 880 | mmio.data = val; |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 881 | mmio.private = iodev->redist_vcpu; |
| 882 | |
| 883 | spin_lock(&dist->lock); |
Andre Przywara | 9641525 | 2014-06-02 22:44:37 +0200 | [diff] [blame] | 884 | offset -= range->base; |
Marc Zyngier | c3c9183 | 2014-07-08 12:09:04 +0100 | [diff] [blame] | 885 | if (vgic_validate_access(dist, range, offset)) { |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 886 | updated_state = call_range_handler(vcpu, &mmio, offset, range); |
Marc Zyngier | c3c9183 | 2014-07-08 12:09:04 +0100 | [diff] [blame] | 887 | } else { |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 888 | if (!is_write) |
| 889 | memset(val, 0, len); |
Marc Zyngier | c3c9183 | 2014-07-08 12:09:04 +0100 | [diff] [blame] | 890 | updated_state = false; |
| 891 | } |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 892 | spin_unlock(&dist->lock); |
Andre Przywara | 950324a | 2015-03-28 01:13:13 +0000 | [diff] [blame] | 893 | run->mmio.is_write = is_write; |
| 894 | run->mmio.len = len; |
| 895 | run->mmio.phys_addr = addr; |
| 896 | memcpy(run->mmio.data, val, len); |
| 897 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 898 | kvm_handle_mmio_return(vcpu, run); |
| 899 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 900 | if (updated_state) |
| 901 | vgic_kick_vcpus(vcpu->kvm); |
| 902 | |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 903 | return 0; |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 904 | } |
| 905 | |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 906 | static int vgic_handle_mmio_read(struct kvm_vcpu *vcpu, |
| 907 | struct kvm_io_device *this, |
| 908 | gpa_t addr, int len, void *val) |
Andre Przywara | 9641525 | 2014-06-02 22:44:37 +0200 | [diff] [blame] | 909 | { |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 910 | return vgic_handle_mmio_access(vcpu, this, addr, len, val, false); |
| 911 | } |
Andre Przywara | 9641525 | 2014-06-02 22:44:37 +0200 | [diff] [blame] | 912 | |
Andre Przywara | 6777f77 | 2015-03-26 14:39:34 +0000 | [diff] [blame] | 913 | static int vgic_handle_mmio_write(struct kvm_vcpu *vcpu, |
| 914 | struct kvm_io_device *this, |
| 915 | gpa_t addr, int len, const void *val) |
| 916 | { |
| 917 | return vgic_handle_mmio_access(vcpu, this, addr, len, (void *)val, |
| 918 | true); |
| 919 | } |
| 920 | |
| 921 | struct kvm_io_device_ops vgic_io_ops = { |
| 922 | .read = vgic_handle_mmio_read, |
| 923 | .write = vgic_handle_mmio_write, |
| 924 | }; |
| 925 | |
| 926 | /** |
| 927 | * vgic_register_kvm_io_dev - register VGIC register frame on the KVM I/O bus |
| 928 | * @kvm: The VM structure pointer |
| 929 | * @base: The (guest) base address for the register frame |
| 930 | * @len: Length of the register frame window |
| 931 | * @ranges: Describing the handler functions for each register |
| 932 | * @redist_vcpu_id: The VCPU ID to pass on to the handlers on call |
| 933 | * @iodev: Points to memory to be passed on to the handler |
| 934 | * |
| 935 | * @iodev stores the parameters of this function to be usable by the handler |
| 936 | * respectively the dispatcher function (since the KVM I/O bus framework lacks |
| 937 | * an opaque parameter). Initialization is done in this function, but the |
| 938 | * reference should be valid and unique for the whole VGIC lifetime. |
| 939 | * If the register frame is not mapped for a specific VCPU, pass -1 to |
| 940 | * @redist_vcpu_id. |
| 941 | */ |
| 942 | int vgic_register_kvm_io_dev(struct kvm *kvm, gpa_t base, int len, |
| 943 | const struct vgic_io_range *ranges, |
| 944 | int redist_vcpu_id, |
| 945 | struct vgic_io_device *iodev) |
| 946 | { |
| 947 | struct kvm_vcpu *vcpu = NULL; |
| 948 | int ret; |
| 949 | |
| 950 | if (redist_vcpu_id >= 0) |
| 951 | vcpu = kvm_get_vcpu(kvm, redist_vcpu_id); |
| 952 | |
| 953 | iodev->addr = base; |
| 954 | iodev->len = len; |
| 955 | iodev->reg_ranges = ranges; |
| 956 | iodev->redist_vcpu = vcpu; |
| 957 | |
| 958 | kvm_iodevice_init(&iodev->dev, &vgic_io_ops); |
| 959 | |
| 960 | mutex_lock(&kvm->slots_lock); |
| 961 | |
| 962 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, base, len, |
| 963 | &iodev->dev); |
| 964 | mutex_unlock(&kvm->slots_lock); |
| 965 | |
| 966 | /* Mark the iodev as invalid if registration fails. */ |
| 967 | if (ret) |
| 968 | iodev->dev.ops = NULL; |
| 969 | |
| 970 | return ret; |
Andre Przywara | 9641525 | 2014-06-02 22:44:37 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 973 | static int vgic_nr_shared_irqs(struct vgic_dist *dist) |
| 974 | { |
| 975 | return dist->nr_irqs - VGIC_NR_PRIVATE_IRQS; |
| 976 | } |
| 977 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 978 | static int compute_active_for_cpu(struct kvm_vcpu *vcpu) |
| 979 | { |
| 980 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 981 | unsigned long *active, *enabled, *act_percpu, *act_shared; |
| 982 | unsigned long active_private, active_shared; |
| 983 | int nr_shared = vgic_nr_shared_irqs(dist); |
| 984 | int vcpu_id; |
| 985 | |
| 986 | vcpu_id = vcpu->vcpu_id; |
| 987 | act_percpu = vcpu->arch.vgic_cpu.active_percpu; |
| 988 | act_shared = vcpu->arch.vgic_cpu.active_shared; |
| 989 | |
| 990 | active = vgic_bitmap_get_cpu_map(&dist->irq_active, vcpu_id); |
| 991 | enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id); |
| 992 | bitmap_and(act_percpu, active, enabled, VGIC_NR_PRIVATE_IRQS); |
| 993 | |
| 994 | active = vgic_bitmap_get_shared_map(&dist->irq_active); |
| 995 | enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled); |
| 996 | bitmap_and(act_shared, active, enabled, nr_shared); |
| 997 | bitmap_and(act_shared, act_shared, |
| 998 | vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]), |
| 999 | nr_shared); |
| 1000 | |
| 1001 | active_private = find_first_bit(act_percpu, VGIC_NR_PRIVATE_IRQS); |
| 1002 | active_shared = find_first_bit(act_shared, nr_shared); |
| 1003 | |
| 1004 | return (active_private < VGIC_NR_PRIVATE_IRQS || |
| 1005 | active_shared < nr_shared); |
| 1006 | } |
| 1007 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1008 | static int compute_pending_for_cpu(struct kvm_vcpu *vcpu) |
| 1009 | { |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1010 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1011 | unsigned long *pending, *enabled, *pend_percpu, *pend_shared; |
| 1012 | unsigned long pending_private, pending_shared; |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 1013 | int nr_shared = vgic_nr_shared_irqs(dist); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1014 | int vcpu_id; |
| 1015 | |
| 1016 | vcpu_id = vcpu->vcpu_id; |
| 1017 | pend_percpu = vcpu->arch.vgic_cpu.pending_percpu; |
| 1018 | pend_shared = vcpu->arch.vgic_cpu.pending_shared; |
| 1019 | |
Christoffer Dall | 0d99749 | 2015-10-17 19:05:27 +0200 | [diff] [blame] | 1020 | if (!dist->enabled) { |
| 1021 | bitmap_zero(pend_percpu, VGIC_NR_PRIVATE_IRQS); |
| 1022 | bitmap_zero(pend_shared, nr_shared); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1026 | pending = vgic_bitmap_get_cpu_map(&dist->irq_pending, vcpu_id); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1027 | enabled = vgic_bitmap_get_cpu_map(&dist->irq_enabled, vcpu_id); |
| 1028 | bitmap_and(pend_percpu, pending, enabled, VGIC_NR_PRIVATE_IRQS); |
| 1029 | |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1030 | pending = vgic_bitmap_get_shared_map(&dist->irq_pending); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1031 | enabled = vgic_bitmap_get_shared_map(&dist->irq_enabled); |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 1032 | bitmap_and(pend_shared, pending, enabled, nr_shared); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1033 | bitmap_and(pend_shared, pend_shared, |
| 1034 | vgic_bitmap_get_shared_map(&dist->irq_spi_target[vcpu_id]), |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 1035 | nr_shared); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1036 | |
| 1037 | pending_private = find_first_bit(pend_percpu, VGIC_NR_PRIVATE_IRQS); |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 1038 | pending_shared = find_first_bit(pend_shared, nr_shared); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1039 | return (pending_private < VGIC_NR_PRIVATE_IRQS || |
Marc Zyngier | fb65ab6 | 2014-07-08 12:09:02 +0100 | [diff] [blame] | 1040 | pending_shared < vgic_nr_shared_irqs(dist)); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Update the interrupt state and determine which CPUs have pending |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1045 | * or active interrupts. Must be called with distributor lock held. |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1046 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1047 | void vgic_update_state(struct kvm *kvm) |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1048 | { |
| 1049 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 1050 | struct kvm_vcpu *vcpu; |
| 1051 | int c; |
| 1052 | |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1053 | kvm_for_each_vcpu(c, vcpu, kvm) { |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1054 | if (compute_pending_for_cpu(vcpu)) |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1055 | set_bit(c, dist->irq_pending_on_cpu); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1056 | |
| 1057 | if (compute_active_for_cpu(vcpu)) |
| 1058 | set_bit(c, dist->irq_active_on_cpu); |
| 1059 | else |
| 1060 | clear_bit(c, dist->irq_active_on_cpu); |
Marc Zyngier | b47ef92 | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1061 | } |
Marc Zyngier | 1a89dd9 | 2013-01-21 19:36:12 -0500 | [diff] [blame] | 1062 | } |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 1063 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1064 | static struct vgic_lr vgic_get_lr(const struct kvm_vcpu *vcpu, int lr) |
| 1065 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1066 | return vgic_ops->get_lr(vcpu, lr); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static void vgic_set_lr(struct kvm_vcpu *vcpu, int lr, |
| 1070 | struct vgic_lr vlr) |
| 1071 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1072 | vgic_ops->set_lr(vcpu, lr, vlr); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
Marc Zyngier | 69bb2c9 | 2013-06-04 10:29:39 +0100 | [diff] [blame] | 1075 | static void vgic_sync_lr_elrsr(struct kvm_vcpu *vcpu, int lr, |
| 1076 | struct vgic_lr vlr) |
| 1077 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1078 | vgic_ops->sync_lr_elrsr(vcpu, lr, vlr); |
Marc Zyngier | 69bb2c9 | 2013-06-04 10:29:39 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static inline u64 vgic_get_elrsr(struct kvm_vcpu *vcpu) |
| 1082 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1083 | return vgic_ops->get_elrsr(vcpu); |
Marc Zyngier | 69bb2c9 | 2013-06-04 10:29:39 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
Marc Zyngier | 8d6a031 | 2013-06-04 10:33:43 +0100 | [diff] [blame] | 1086 | static inline u64 vgic_get_eisr(struct kvm_vcpu *vcpu) |
| 1087 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1088 | return vgic_ops->get_eisr(vcpu); |
Marc Zyngier | 8d6a031 | 2013-06-04 10:33:43 +0100 | [diff] [blame] | 1089 | } |
| 1090 | |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 1091 | static inline void vgic_clear_eisr(struct kvm_vcpu *vcpu) |
| 1092 | { |
| 1093 | vgic_ops->clear_eisr(vcpu); |
| 1094 | } |
| 1095 | |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1096 | static inline u32 vgic_get_interrupt_status(struct kvm_vcpu *vcpu) |
| 1097 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1098 | return vgic_ops->get_interrupt_status(vcpu); |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1101 | static inline void vgic_enable_underflow(struct kvm_vcpu *vcpu) |
| 1102 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1103 | vgic_ops->enable_underflow(vcpu); |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static inline void vgic_disable_underflow(struct kvm_vcpu *vcpu) |
| 1107 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1108 | vgic_ops->disable_underflow(vcpu); |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1111 | void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
Marc Zyngier | beee38b | 2014-02-04 17:48:10 +0000 | [diff] [blame] | 1112 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1113 | vgic_ops->get_vmcr(vcpu, vmcr); |
Marc Zyngier | beee38b | 2014-02-04 17:48:10 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1116 | void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
Marc Zyngier | beee38b | 2014-02-04 17:48:10 +0000 | [diff] [blame] | 1117 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1118 | vgic_ops->set_vmcr(vcpu, vmcr); |
Marc Zyngier | beee38b | 2014-02-04 17:48:10 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Marc Zyngier | da8dafd1 | 2013-06-04 11:36:38 +0100 | [diff] [blame] | 1121 | static inline void vgic_enable(struct kvm_vcpu *vcpu) |
| 1122 | { |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1123 | vgic_ops->enable(vcpu); |
Marc Zyngier | da8dafd1 | 2013-06-04 11:36:38 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1126 | static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu) |
| 1127 | { |
| 1128 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1129 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr_nr); |
| 1130 | |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 1131 | /* |
| 1132 | * We must transfer the pending state back to the distributor before |
| 1133 | * retiring the LR, otherwise we may loose edge-triggered interrupts. |
| 1134 | */ |
| 1135 | if (vlr.state & LR_STATE_PENDING) { |
| 1136 | vgic_dist_irq_set_pending(vcpu, irq); |
| 1137 | vlr.hwirq = 0; |
| 1138 | } |
| 1139 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1140 | vlr.state = 0; |
| 1141 | vgic_set_lr(vcpu, lr_nr, vlr); |
| 1142 | clear_bit(lr_nr, vgic_cpu->lr_used); |
| 1143 | vgic_cpu->vgic_irq_lr_map[irq] = LR_EMPTY; |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 1144 | vgic_sync_lr_elrsr(vcpu, lr_nr, vlr); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1145 | } |
Marc Zyngier | a1fcb44 | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1146 | |
| 1147 | /* |
| 1148 | * An interrupt may have been disabled after being made pending on the |
| 1149 | * CPU interface (the classic case is a timer running while we're |
| 1150 | * rebooting the guest - the interrupt would kick as soon as the CPU |
| 1151 | * interface gets enabled, with deadly consequences). |
| 1152 | * |
| 1153 | * The solution is to examine already active LRs, and check the |
| 1154 | * interrupt is still enabled. If not, just retire it. |
| 1155 | */ |
| 1156 | static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu) |
| 1157 | { |
| 1158 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1159 | int lr; |
| 1160 | |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1161 | for_each_set_bit(lr, vgic_cpu->lr_used, vgic->nr_lr) { |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1162 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr); |
Marc Zyngier | a1fcb44 | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1163 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1164 | if (!vgic_irq_is_enabled(vcpu, vlr.irq)) { |
| 1165 | vgic_retire_lr(lr, vlr.irq, vcpu); |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 1166 | if (vgic_irq_is_queued(vcpu, vlr.irq)) |
| 1167 | vgic_irq_clear_queued(vcpu, vlr.irq); |
Marc Zyngier | a1fcb44 | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1172 | static void vgic_queue_irq_to_lr(struct kvm_vcpu *vcpu, int irq, |
| 1173 | int lr_nr, struct vgic_lr vlr) |
| 1174 | { |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1175 | if (vgic_irq_is_active(vcpu, irq)) { |
| 1176 | vlr.state |= LR_STATE_ACTIVE; |
| 1177 | kvm_debug("Set active, clear distributor: 0x%x\n", vlr.state); |
| 1178 | vgic_irq_clear_active(vcpu, irq); |
| 1179 | vgic_update_state(vcpu->kvm); |
Pavel Fedin | 437f996 | 2015-09-25 17:00:29 +0300 | [diff] [blame] | 1180 | } else { |
| 1181 | WARN_ON(!vgic_dist_irq_is_pending(vcpu, irq)); |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1182 | vlr.state |= LR_STATE_PENDING; |
| 1183 | kvm_debug("Set pending: 0x%x\n", vlr.state); |
| 1184 | } |
| 1185 | |
| 1186 | if (!vgic_irq_is_edge(vcpu, irq)) |
| 1187 | vlr.state |= LR_EOI_INT; |
| 1188 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1189 | if (vlr.irq >= VGIC_NR_SGIS) { |
| 1190 | struct irq_phys_map *map; |
| 1191 | map = vgic_irq_map_search(vcpu, irq); |
| 1192 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1193 | if (map) { |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1194 | vlr.hwirq = map->phys_irq; |
| 1195 | vlr.state |= LR_HW; |
| 1196 | vlr.state &= ~LR_EOI_INT; |
| 1197 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1198 | /* |
| 1199 | * Make sure we're not going to sample this |
| 1200 | * again, as a HW-backed interrupt cannot be |
| 1201 | * in the PENDING_ACTIVE stage. |
| 1202 | */ |
| 1203 | vgic_irq_set_queued(vcpu, irq); |
| 1204 | } |
| 1205 | } |
| 1206 | |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1207 | vgic_set_lr(vcpu, lr_nr, vlr); |
Paolo Bonzini | bf0fb67 | 2015-04-07 18:09:20 +0200 | [diff] [blame] | 1208 | vgic_sync_lr_elrsr(vcpu, lr_nr, vlr); |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1211 | /* |
| 1212 | * Queue an interrupt to a CPU virtual interface. Return true on success, |
| 1213 | * or false if it wasn't possible to queue it. |
Andre Przywara | 1d91622 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1214 | * sgi_source must be zero for any non-SGI interrupts. |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1215 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1216 | bool vgic_queue_irq(struct kvm_vcpu *vcpu, u8 sgi_source_id, int irq) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1217 | { |
| 1218 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
Marc Zyngier | 5fb66da | 2014-07-08 12:09:05 +0100 | [diff] [blame] | 1219 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1220 | struct vgic_lr vlr; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1221 | int lr; |
| 1222 | |
| 1223 | /* Sanitize the input... */ |
| 1224 | BUG_ON(sgi_source_id & ~7); |
| 1225 | BUG_ON(sgi_source_id && irq >= VGIC_NR_SGIS); |
Marc Zyngier | 5fb66da | 2014-07-08 12:09:05 +0100 | [diff] [blame] | 1226 | BUG_ON(irq >= dist->nr_irqs); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1227 | |
| 1228 | kvm_debug("Queue IRQ%d\n", irq); |
| 1229 | |
| 1230 | lr = vgic_cpu->vgic_irq_lr_map[irq]; |
| 1231 | |
| 1232 | /* Do we have an active interrupt for the same CPUID? */ |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1233 | if (lr != LR_EMPTY) { |
| 1234 | vlr = vgic_get_lr(vcpu, lr); |
| 1235 | if (vlr.source == sgi_source_id) { |
| 1236 | kvm_debug("LR%d piggyback for IRQ%d\n", lr, vlr.irq); |
| 1237 | BUG_ON(!test_bit(lr, vgic_cpu->lr_used)); |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1238 | vgic_queue_irq_to_lr(vcpu, irq, lr, vlr); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1239 | return true; |
| 1240 | } |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* Try to use another LR for this interrupt */ |
| 1244 | lr = find_first_zero_bit((unsigned long *)vgic_cpu->lr_used, |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1245 | vgic->nr_lr); |
| 1246 | if (lr >= vgic->nr_lr) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1247 | return false; |
| 1248 | |
| 1249 | kvm_debug("LR%d allocated for IRQ%d %x\n", lr, irq, sgi_source_id); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1250 | vgic_cpu->vgic_irq_lr_map[irq] = lr; |
| 1251 | set_bit(lr, vgic_cpu->lr_used); |
| 1252 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1253 | vlr.irq = irq; |
| 1254 | vlr.source = sgi_source_id; |
Alex Bennée | 7176095 | 2015-03-13 17:02:53 +0000 | [diff] [blame] | 1255 | vlr.state = 0; |
| 1256 | vgic_queue_irq_to_lr(vcpu, irq, lr, vlr); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1257 | |
| 1258 | return true; |
| 1259 | } |
| 1260 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1261 | static bool vgic_queue_hwirq(struct kvm_vcpu *vcpu, int irq) |
| 1262 | { |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 1263 | if (!vgic_can_sample_irq(vcpu, irq)) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1264 | return true; /* level interrupt, already queued */ |
| 1265 | |
| 1266 | if (vgic_queue_irq(vcpu, 0, irq)) { |
| 1267 | if (vgic_irq_is_edge(vcpu, irq)) { |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1268 | vgic_dist_irq_clear_pending(vcpu, irq); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1269 | vgic_cpu_irq_clear(vcpu, irq); |
| 1270 | } else { |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 1271 | vgic_irq_set_queued(vcpu, irq); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | return true; |
| 1275 | } |
| 1276 | |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Fill the list registers with pending interrupts before running the |
| 1282 | * guest. |
| 1283 | */ |
| 1284 | static void __kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) |
| 1285 | { |
| 1286 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1287 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1288 | unsigned long *pa_percpu, *pa_shared; |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 1289 | int i, vcpu_id; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1290 | int overflow = 0; |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1291 | int nr_shared = vgic_nr_shared_irqs(dist); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1292 | |
| 1293 | vcpu_id = vcpu->vcpu_id; |
| 1294 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1295 | pa_percpu = vcpu->arch.vgic_cpu.pend_act_percpu; |
| 1296 | pa_shared = vcpu->arch.vgic_cpu.pend_act_shared; |
| 1297 | |
| 1298 | bitmap_or(pa_percpu, vgic_cpu->pending_percpu, vgic_cpu->active_percpu, |
| 1299 | VGIC_NR_PRIVATE_IRQS); |
| 1300 | bitmap_or(pa_shared, vgic_cpu->pending_shared, vgic_cpu->active_shared, |
| 1301 | nr_shared); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1302 | /* |
| 1303 | * We may not have any pending interrupt, or the interrupts |
| 1304 | * may have been serviced from another vcpu. In all cases, |
| 1305 | * move along. |
| 1306 | */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1307 | if (!kvm_vgic_vcpu_pending_irq(vcpu) && !kvm_vgic_vcpu_active_irq(vcpu)) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1308 | goto epilog; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1309 | |
| 1310 | /* SGIs */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1311 | for_each_set_bit(i, pa_percpu, VGIC_NR_SGIS) { |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 1312 | if (!queue_sgi(vcpu, i)) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1313 | overflow = 1; |
| 1314 | } |
| 1315 | |
| 1316 | /* PPIs */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1317 | for_each_set_bit_from(i, pa_percpu, VGIC_NR_PRIVATE_IRQS) { |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1318 | if (!vgic_queue_hwirq(vcpu, i)) |
| 1319 | overflow = 1; |
| 1320 | } |
| 1321 | |
| 1322 | /* SPIs */ |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1323 | for_each_set_bit(i, pa_shared, nr_shared) { |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1324 | if (!vgic_queue_hwirq(vcpu, i + VGIC_NR_PRIVATE_IRQS)) |
| 1325 | overflow = 1; |
| 1326 | } |
| 1327 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1328 | |
| 1329 | |
| 1330 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1331 | epilog: |
| 1332 | if (overflow) { |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1333 | vgic_enable_underflow(vcpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1334 | } else { |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1335 | vgic_disable_underflow(vcpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1336 | /* |
| 1337 | * We're about to run this VCPU, and we've consumed |
| 1338 | * everything the distributor had in store for |
| 1339 | * us. Claim we don't have anything pending. We'll |
| 1340 | * adjust that if needed while exiting. |
| 1341 | */ |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1342 | clear_bit(vcpu_id, dist->irq_pending_on_cpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1346 | static int process_level_irq(struct kvm_vcpu *vcpu, int lr, struct vgic_lr vlr) |
| 1347 | { |
| 1348 | int level_pending = 0; |
| 1349 | |
| 1350 | vlr.state = 0; |
| 1351 | vlr.hwirq = 0; |
| 1352 | vgic_set_lr(vcpu, lr, vlr); |
| 1353 | |
| 1354 | /* |
| 1355 | * If the IRQ was EOIed (called from vgic_process_maintenance) or it |
| 1356 | * went from active to non-active (called from vgic_sync_hwirq) it was |
| 1357 | * also ACKed and we we therefore assume we can clear the soft pending |
| 1358 | * state (should it had been set) for this interrupt. |
| 1359 | * |
| 1360 | * Note: if the IRQ soft pending state was set after the IRQ was |
| 1361 | * acked, it actually shouldn't be cleared, but we have no way of |
| 1362 | * knowing that unless we start trapping ACKs when the soft-pending |
| 1363 | * state is set. |
| 1364 | */ |
| 1365 | vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq); |
| 1366 | |
| 1367 | /* |
| 1368 | * Tell the gic to start sampling the line of this interrupt again. |
| 1369 | */ |
| 1370 | vgic_irq_clear_queued(vcpu, vlr.irq); |
| 1371 | |
| 1372 | /* Any additional pending interrupt? */ |
| 1373 | if (vgic_dist_irq_get_level(vcpu, vlr.irq)) { |
| 1374 | vgic_cpu_irq_set(vcpu, vlr.irq); |
| 1375 | level_pending = 1; |
| 1376 | } else { |
| 1377 | vgic_dist_irq_clear_pending(vcpu, vlr.irq); |
| 1378 | vgic_cpu_irq_clear(vcpu, vlr.irq); |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * Despite being EOIed, the LR may not have |
| 1383 | * been marked as empty. |
| 1384 | */ |
| 1385 | vgic_sync_lr_elrsr(vcpu, lr, vlr); |
| 1386 | |
| 1387 | return level_pending; |
| 1388 | } |
| 1389 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1390 | static bool vgic_process_maintenance(struct kvm_vcpu *vcpu) |
| 1391 | { |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1392 | u32 status = vgic_get_interrupt_status(vcpu); |
Eric Auger | 649cf73 | 2015-03-04 11:14:35 +0100 | [diff] [blame] | 1393 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 1394 | struct kvm *kvm = vcpu->kvm; |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1395 | int level_pending = 0; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1396 | |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1397 | kvm_debug("STATUS = %08x\n", status); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1398 | |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1399 | if (status & INT_STATUS_EOI) { |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1400 | /* |
| 1401 | * Some level interrupts have been EOIed. Clear their |
| 1402 | * active bit. |
| 1403 | */ |
Marc Zyngier | 8d6a031 | 2013-06-04 10:33:43 +0100 | [diff] [blame] | 1404 | u64 eisr = vgic_get_eisr(vcpu); |
Christoffer Dall | 2df36a5 | 2014-09-28 16:04:26 +0200 | [diff] [blame] | 1405 | unsigned long *eisr_ptr = u64_to_bitmask(&eisr); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1406 | int lr; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1407 | |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1408 | for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) { |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1409 | struct vgic_lr vlr = vgic_get_lr(vcpu, lr); |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1410 | |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1411 | WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq)); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1412 | WARN_ON(vlr.state & LR_STATE_MASK); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1413 | |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1414 | |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 1415 | /* |
| 1416 | * kvm_notify_acked_irq calls kvm_set_irq() |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1417 | * to reset the IRQ level, which grabs the dist->lock |
| 1418 | * so we call this before taking the dist->lock. |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 1419 | */ |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 1420 | kvm_notify_acked_irq(kvm, 0, |
| 1421 | vlr.irq - VGIC_NR_PRIVATE_IRQS); |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1422 | |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 1423 | spin_lock(&dist->lock); |
Christoffer Dall | 9103617 | 2015-08-25 22:50:57 +0200 | [diff] [blame] | 1424 | level_pending |= process_level_irq(vcpu, lr, vlr); |
Eric Auger | 649cf73 | 2015-03-04 11:14:35 +0100 | [diff] [blame] | 1425 | spin_unlock(&dist->lock); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
Marc Zyngier | 495dd85 | 2013-06-04 11:02:10 +0100 | [diff] [blame] | 1429 | if (status & INT_STATUS_UNDERFLOW) |
Marc Zyngier | 909d9b5 | 2013-06-04 11:24:17 +0100 | [diff] [blame] | 1430 | vgic_disable_underflow(vcpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1431 | |
Christoffer Dall | ae70593 | 2015-03-13 17:02:56 +0000 | [diff] [blame] | 1432 | /* |
| 1433 | * In the next iterations of the vcpu loop, if we sync the vgic state |
| 1434 | * after flushing it, but before entering the guest (this happens for |
| 1435 | * pending signals and vmid rollovers), then make sure we don't pick |
| 1436 | * up any old maintenance interrupts here. |
| 1437 | */ |
| 1438 | vgic_clear_eisr(vcpu); |
| 1439 | |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1440 | return level_pending; |
| 1441 | } |
| 1442 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1443 | /* |
| 1444 | * Save the physical active state, and reset it to inactive. |
| 1445 | * |
| 1446 | * Return 1 if HW interrupt went from active to inactive, and 0 otherwise. |
| 1447 | */ |
| 1448 | static int vgic_sync_hwirq(struct kvm_vcpu *vcpu, struct vgic_lr vlr) |
| 1449 | { |
| 1450 | struct irq_phys_map *map; |
| 1451 | int ret; |
| 1452 | |
| 1453 | if (!(vlr.state & LR_HW)) |
| 1454 | return 0; |
| 1455 | |
| 1456 | map = vgic_irq_map_search(vcpu, vlr.irq); |
Christoffer Dall | 544c572 | 2015-10-17 17:55:12 +0200 | [diff] [blame] | 1457 | BUG_ON(!map); |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1458 | |
| 1459 | ret = irq_get_irqchip_state(map->irq, |
| 1460 | IRQCHIP_STATE_ACTIVE, |
| 1461 | &map->active); |
| 1462 | |
| 1463 | WARN_ON(ret); |
| 1464 | |
Christoffer Dall | cff9211 | 2015-10-16 12:41:21 +0200 | [diff] [blame] | 1465 | if (map->active) |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1466 | return 0; |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1467 | |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
Eric Auger | 649cf73 | 2015-03-04 11:14:35 +0100 | [diff] [blame] | 1471 | /* Sync back the VGIC state after a guest run */ |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1472 | static void __kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) |
| 1473 | { |
| 1474 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1475 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Marc Zyngier | 69bb2c9 | 2013-06-04 10:29:39 +0100 | [diff] [blame] | 1476 | u64 elrsr; |
| 1477 | unsigned long *elrsr_ptr; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1478 | int lr, pending; |
| 1479 | bool level_pending; |
| 1480 | |
| 1481 | level_pending = vgic_process_maintenance(vcpu); |
Marc Zyngier | 69bb2c9 | 2013-06-04 10:29:39 +0100 | [diff] [blame] | 1482 | elrsr = vgic_get_elrsr(vcpu); |
Christoffer Dall | 2df36a5 | 2014-09-28 16:04:26 +0200 | [diff] [blame] | 1483 | elrsr_ptr = u64_to_bitmask(&elrsr); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1484 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1485 | /* Deal with HW interrupts, and clear mappings for empty LRs */ |
| 1486 | for (lr = 0; lr < vgic->nr_lr; lr++) { |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1487 | struct vgic_lr vlr; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1488 | |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1489 | if (!test_bit(lr, vgic_cpu->lr_used)) |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1490 | continue; |
| 1491 | |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1492 | vlr = vgic_get_lr(vcpu, lr); |
Marc Zyngier | 08fd646 | 2015-06-08 16:06:13 +0100 | [diff] [blame] | 1493 | if (vgic_sync_hwirq(vcpu, vlr)) { |
| 1494 | /* |
| 1495 | * So this is a HW interrupt that the guest |
| 1496 | * EOI-ed. Clean the LR state and allow the |
| 1497 | * interrupt to be sampled again. |
| 1498 | */ |
| 1499 | vlr.state = 0; |
| 1500 | vlr.hwirq = 0; |
| 1501 | vgic_set_lr(vcpu, lr, vlr); |
| 1502 | vgic_irq_clear_queued(vcpu, vlr.irq); |
| 1503 | set_bit(lr, elrsr_ptr); |
| 1504 | } |
| 1505 | |
| 1506 | if (!test_bit(lr, elrsr_ptr)) |
| 1507 | continue; |
| 1508 | |
| 1509 | clear_bit(lr, vgic_cpu->lr_used); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1510 | |
Marc Zyngier | 5fb66da | 2014-07-08 12:09:05 +0100 | [diff] [blame] | 1511 | BUG_ON(vlr.irq >= dist->nr_irqs); |
Marc Zyngier | 8d5c6b0 | 2013-06-03 15:55:02 +0100 | [diff] [blame] | 1512 | vgic_cpu->vgic_irq_lr_map[vlr.irq] = LR_EMPTY; |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | /* Check if we still have something up our sleeve... */ |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1516 | pending = find_first_zero_bit(elrsr_ptr, vgic->nr_lr); |
| 1517 | if (level_pending || pending < vgic->nr_lr) |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1518 | set_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) |
| 1522 | { |
| 1523 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1524 | |
| 1525 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1526 | return; |
| 1527 | |
| 1528 | spin_lock(&dist->lock); |
| 1529 | __kvm_vgic_flush_hwstate(vcpu); |
| 1530 | spin_unlock(&dist->lock); |
| 1531 | } |
| 1532 | |
| 1533 | void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) |
| 1534 | { |
| 1535 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1536 | return; |
| 1537 | |
| 1538 | __kvm_vgic_sync_hwstate(vcpu); |
| 1539 | } |
| 1540 | |
| 1541 | int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) |
| 1542 | { |
| 1543 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1544 | |
| 1545 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1546 | return 0; |
| 1547 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1548 | return test_bit(vcpu->vcpu_id, dist->irq_pending_on_cpu); |
Marc Zyngier | 9d949dc | 2013-01-21 19:36:14 -0500 | [diff] [blame] | 1549 | } |
| 1550 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1551 | int kvm_vgic_vcpu_active_irq(struct kvm_vcpu *vcpu) |
| 1552 | { |
| 1553 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1554 | |
| 1555 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 1556 | return 0; |
| 1557 | |
| 1558 | return test_bit(vcpu->vcpu_id, dist->irq_active_on_cpu); |
| 1559 | } |
| 1560 | |
| 1561 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 1562 | void vgic_kick_vcpus(struct kvm *kvm) |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1563 | { |
| 1564 | struct kvm_vcpu *vcpu; |
| 1565 | int c; |
| 1566 | |
| 1567 | /* |
| 1568 | * We've injected an interrupt, time to find out who deserves |
| 1569 | * a good kick... |
| 1570 | */ |
| 1571 | kvm_for_each_vcpu(c, vcpu, kvm) { |
| 1572 | if (kvm_vgic_vcpu_pending_irq(vcpu)) |
| 1573 | kvm_vcpu_kick(vcpu); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level) |
| 1578 | { |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1579 | int edge_triggered = vgic_irq_is_edge(vcpu, irq); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1580 | |
| 1581 | /* |
| 1582 | * Only inject an interrupt if: |
| 1583 | * - edge triggered and we have a rising edge |
| 1584 | * - level triggered and we change level |
| 1585 | */ |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1586 | if (edge_triggered) { |
| 1587 | int state = vgic_dist_irq_is_pending(vcpu, irq); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1588 | return level > state; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1589 | } else { |
| 1590 | int state = vgic_dist_irq_get_level(vcpu, irq); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1591 | return level != state; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1592 | } |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
Shannon Zhao | 016ed39 | 2014-11-19 10:11:25 +0000 | [diff] [blame] | 1595 | static int vgic_update_irq_pending(struct kvm *kvm, int cpuid, |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1596 | struct irq_phys_map *map, |
| 1597 | unsigned int irq_num, bool level) |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1598 | { |
| 1599 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 1600 | struct kvm_vcpu *vcpu; |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1601 | int edge_triggered, level_triggered; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1602 | int enabled; |
Andre Przywara | a0675c2 | 2014-06-07 00:54:51 +0200 | [diff] [blame] | 1603 | bool ret = true, can_inject = true; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1604 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1605 | if (irq_num >= min(kvm->arch.vgic.nr_irqs, 1020)) |
| 1606 | return -EINVAL; |
| 1607 | |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1608 | spin_lock(&dist->lock); |
| 1609 | |
| 1610 | vcpu = kvm_get_vcpu(kvm, cpuid); |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1611 | edge_triggered = vgic_irq_is_edge(vcpu, irq_num); |
| 1612 | level_triggered = !edge_triggered; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1613 | |
| 1614 | if (!vgic_validate_injection(vcpu, irq_num, level)) { |
| 1615 | ret = false; |
| 1616 | goto out; |
| 1617 | } |
| 1618 | |
| 1619 | if (irq_num >= VGIC_NR_PRIVATE_IRQS) { |
| 1620 | cpuid = dist->irq_spi_cpu[irq_num - VGIC_NR_PRIVATE_IRQS]; |
Andre Przywara | a0675c2 | 2014-06-07 00:54:51 +0200 | [diff] [blame] | 1621 | if (cpuid == VCPU_NOT_ALLOCATED) { |
| 1622 | /* Pretend we use CPU0, and prevent injection */ |
| 1623 | cpuid = 0; |
| 1624 | can_inject = false; |
| 1625 | } |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1626 | vcpu = kvm_get_vcpu(kvm, cpuid); |
| 1627 | } |
| 1628 | |
| 1629 | kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid); |
| 1630 | |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1631 | if (level) { |
| 1632 | if (level_triggered) |
| 1633 | vgic_dist_irq_set_level(vcpu, irq_num); |
Christoffer Dall | 227844f | 2014-06-09 12:27:18 +0200 | [diff] [blame] | 1634 | vgic_dist_irq_set_pending(vcpu, irq_num); |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1635 | } else { |
| 1636 | if (level_triggered) { |
| 1637 | vgic_dist_irq_clear_level(vcpu, irq_num); |
Pavel Fedin | 437f996 | 2015-09-25 17:00:29 +0300 | [diff] [blame] | 1638 | if (!vgic_dist_irq_soft_pend(vcpu, irq_num)) { |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1639 | vgic_dist_irq_clear_pending(vcpu, irq_num); |
Pavel Fedin | 437f996 | 2015-09-25 17:00:29 +0300 | [diff] [blame] | 1640 | vgic_cpu_irq_clear(vcpu, irq_num); |
| 1641 | if (!compute_pending_for_cpu(vcpu)) |
| 1642 | clear_bit(cpuid, dist->irq_pending_on_cpu); |
| 1643 | } |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1644 | } |
wanghaibin | 7d39f9e3 | 2014-11-17 09:27:37 +0000 | [diff] [blame] | 1645 | |
| 1646 | ret = false; |
| 1647 | goto out; |
Christoffer Dall | faa1b46 | 2014-06-14 21:54:51 +0200 | [diff] [blame] | 1648 | } |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1649 | |
| 1650 | enabled = vgic_irq_is_enabled(vcpu, irq_num); |
| 1651 | |
Andre Przywara | a0675c2 | 2014-06-07 00:54:51 +0200 | [diff] [blame] | 1652 | if (!enabled || !can_inject) { |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1653 | ret = false; |
| 1654 | goto out; |
| 1655 | } |
| 1656 | |
Christoffer Dall | dbf20f9 | 2014-06-09 12:55:13 +0200 | [diff] [blame] | 1657 | if (!vgic_can_sample_irq(vcpu, irq_num)) { |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1658 | /* |
| 1659 | * Level interrupt in progress, will be picked up |
| 1660 | * when EOId. |
| 1661 | */ |
| 1662 | ret = false; |
| 1663 | goto out; |
| 1664 | } |
| 1665 | |
| 1666 | if (level) { |
| 1667 | vgic_cpu_irq_set(vcpu, irq_num); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1668 | set_bit(cpuid, dist->irq_pending_on_cpu); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | out: |
| 1672 | spin_unlock(&dist->lock); |
| 1673 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1674 | if (ret) { |
| 1675 | /* kick the specified vcpu */ |
| 1676 | kvm_vcpu_kick(kvm_get_vcpu(kvm, cpuid)); |
| 1677 | } |
| 1678 | |
| 1679 | return 0; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1680 | } |
| 1681 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1682 | static int vgic_lazy_init(struct kvm *kvm) |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1683 | { |
Christoffer Dall | ca7d9c8 | 2014-12-09 14:35:33 +0100 | [diff] [blame] | 1684 | int ret = 0; |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1685 | |
Christoffer Dall | ca7d9c8 | 2014-12-09 14:35:33 +0100 | [diff] [blame] | 1686 | if (unlikely(!vgic_initialized(kvm))) { |
Andre Przywara | 59892136 | 2014-06-03 09:33:10 +0200 | [diff] [blame] | 1687 | /* |
| 1688 | * We only provide the automatic initialization of the VGIC |
| 1689 | * for the legacy case of a GICv2. Any other type must |
| 1690 | * be explicitly initialized once setup with the respective |
| 1691 | * KVM device call. |
| 1692 | */ |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1693 | if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) |
| 1694 | return -EBUSY; |
| 1695 | |
Christoffer Dall | ca7d9c8 | 2014-12-09 14:35:33 +0100 | [diff] [blame] | 1696 | mutex_lock(&kvm->lock); |
| 1697 | ret = vgic_init(kvm); |
| 1698 | mutex_unlock(&kvm->lock); |
Shannon Zhao | 016ed39 | 2014-11-19 10:11:25 +0000 | [diff] [blame] | 1699 | } |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1700 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1701 | return ret; |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * kvm_vgic_inject_irq - Inject an IRQ from a device to the vgic |
| 1706 | * @kvm: The VM structure pointer |
| 1707 | * @cpuid: The CPU for PPIs |
| 1708 | * @irq_num: The IRQ number that is assigned to the device. This IRQ |
| 1709 | * must not be mapped to a HW interrupt. |
| 1710 | * @level: Edge-triggered: true: to trigger the interrupt |
| 1711 | * false: to ignore the call |
| 1712 | * Level-sensitive true: raise the input signal |
| 1713 | * false: lower the input signal |
| 1714 | * |
| 1715 | * The GIC is not concerned with devices being active-LOW or active-HIGH for |
| 1716 | * level-sensitive interrupts. You can think of the level parameter as 1 |
| 1717 | * being HIGH and 0 being LOW and all devices being active-HIGH. |
| 1718 | */ |
| 1719 | int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num, |
| 1720 | bool level) |
| 1721 | { |
| 1722 | struct irq_phys_map *map; |
| 1723 | int ret; |
| 1724 | |
| 1725 | ret = vgic_lazy_init(kvm); |
| 1726 | if (ret) |
| 1727 | return ret; |
| 1728 | |
| 1729 | map = vgic_irq_map_search(kvm_get_vcpu(kvm, cpuid), irq_num); |
| 1730 | if (map) |
Andre Przywara | fd1d0dd | 2015-04-10 16:17:59 +0100 | [diff] [blame] | 1731 | return -EINVAL; |
| 1732 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1733 | return vgic_update_irq_pending(kvm, cpuid, NULL, irq_num, level); |
| 1734 | } |
Christoffer Dall | ca7d9c8 | 2014-12-09 14:35:33 +0100 | [diff] [blame] | 1735 | |
Marc Zyngier | 773299a | 2015-07-24 11:30:43 +0100 | [diff] [blame] | 1736 | /** |
| 1737 | * kvm_vgic_inject_mapped_irq - Inject a physically mapped IRQ to the vgic |
| 1738 | * @kvm: The VM structure pointer |
| 1739 | * @cpuid: The CPU for PPIs |
| 1740 | * @map: Pointer to a irq_phys_map structure describing the mapping |
| 1741 | * @level: Edge-triggered: true: to trigger the interrupt |
| 1742 | * false: to ignore the call |
| 1743 | * Level-sensitive true: raise the input signal |
| 1744 | * false: lower the input signal |
| 1745 | * |
| 1746 | * The GIC is not concerned with devices being active-LOW or active-HIGH for |
| 1747 | * level-sensitive interrupts. You can think of the level parameter as 1 |
| 1748 | * being HIGH and 0 being LOW and all devices being active-HIGH. |
| 1749 | */ |
| 1750 | int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, |
| 1751 | struct irq_phys_map *map, bool level) |
| 1752 | { |
| 1753 | int ret; |
| 1754 | |
| 1755 | ret = vgic_lazy_init(kvm); |
| 1756 | if (ret) |
| 1757 | return ret; |
| 1758 | |
| 1759 | return vgic_update_irq_pending(kvm, cpuid, map, map->virt_irq, level); |
Marc Zyngier | 5863c2c | 2013-01-21 19:36:15 -0500 | [diff] [blame] | 1760 | } |
| 1761 | |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 1762 | static irqreturn_t vgic_maintenance_handler(int irq, void *data) |
| 1763 | { |
| 1764 | /* |
| 1765 | * We cannot rely on the vgic maintenance interrupt to be |
| 1766 | * delivered synchronously. This means we can only use it to |
| 1767 | * exit the VM, and we perform the handling of EOIed |
| 1768 | * interrupts on the exit path (see vgic_process_maintenance). |
| 1769 | */ |
| 1770 | return IRQ_HANDLED; |
| 1771 | } |
| 1772 | |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 1773 | static struct list_head *vgic_get_irq_phys_map_list(struct kvm_vcpu *vcpu, |
| 1774 | int virt_irq) |
| 1775 | { |
| 1776 | if (virt_irq < VGIC_NR_PRIVATE_IRQS) |
| 1777 | return &vcpu->arch.vgic_cpu.irq_phys_map_list; |
| 1778 | else |
| 1779 | return &vcpu->kvm->arch.vgic.irq_phys_map_list; |
| 1780 | } |
| 1781 | |
| 1782 | /** |
| 1783 | * kvm_vgic_map_phys_irq - map a virtual IRQ to a physical IRQ |
| 1784 | * @vcpu: The VCPU pointer |
| 1785 | * @virt_irq: The virtual irq number |
| 1786 | * @irq: The Linux IRQ number |
| 1787 | * |
| 1788 | * Establish a mapping between a guest visible irq (@virt_irq) and a |
| 1789 | * Linux irq (@irq). On injection, @virt_irq will be associated with |
| 1790 | * the physical interrupt represented by @irq. This mapping can be |
| 1791 | * established multiple times as long as the parameters are the same. |
| 1792 | * |
| 1793 | * Returns a valid pointer on success, and an error pointer otherwise |
| 1794 | */ |
| 1795 | struct irq_phys_map *kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, |
| 1796 | int virt_irq, int irq) |
| 1797 | { |
| 1798 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1799 | struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq); |
| 1800 | struct irq_phys_map *map; |
| 1801 | struct irq_phys_map_entry *entry; |
| 1802 | struct irq_desc *desc; |
| 1803 | struct irq_data *data; |
| 1804 | int phys_irq; |
| 1805 | |
| 1806 | desc = irq_to_desc(irq); |
| 1807 | if (!desc) { |
| 1808 | kvm_err("%s: no interrupt descriptor\n", __func__); |
| 1809 | return ERR_PTR(-EINVAL); |
| 1810 | } |
| 1811 | |
| 1812 | data = irq_desc_get_irq_data(desc); |
| 1813 | while (data->parent_data) |
| 1814 | data = data->parent_data; |
| 1815 | |
| 1816 | phys_irq = data->hwirq; |
| 1817 | |
| 1818 | /* Create a new mapping */ |
| 1819 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1820 | if (!entry) |
| 1821 | return ERR_PTR(-ENOMEM); |
| 1822 | |
| 1823 | spin_lock(&dist->irq_phys_map_lock); |
| 1824 | |
| 1825 | /* Try to match an existing mapping */ |
| 1826 | map = vgic_irq_map_search(vcpu, virt_irq); |
| 1827 | if (map) { |
| 1828 | /* Make sure this mapping matches */ |
| 1829 | if (map->phys_irq != phys_irq || |
| 1830 | map->irq != irq) |
| 1831 | map = ERR_PTR(-EINVAL); |
| 1832 | |
| 1833 | /* Found an existing, valid mapping */ |
| 1834 | goto out; |
| 1835 | } |
| 1836 | |
| 1837 | map = &entry->map; |
| 1838 | map->virt_irq = virt_irq; |
| 1839 | map->phys_irq = phys_irq; |
| 1840 | map->irq = irq; |
| 1841 | |
| 1842 | list_add_tail_rcu(&entry->entry, root); |
| 1843 | |
| 1844 | out: |
| 1845 | spin_unlock(&dist->irq_phys_map_lock); |
| 1846 | /* If we've found a hit in the existing list, free the useless |
| 1847 | * entry */ |
| 1848 | if (IS_ERR(map) || map != &entry->map) |
| 1849 | kfree(entry); |
| 1850 | return map; |
| 1851 | } |
| 1852 | |
| 1853 | static struct irq_phys_map *vgic_irq_map_search(struct kvm_vcpu *vcpu, |
| 1854 | int virt_irq) |
| 1855 | { |
| 1856 | struct list_head *root = vgic_get_irq_phys_map_list(vcpu, virt_irq); |
| 1857 | struct irq_phys_map_entry *entry; |
| 1858 | struct irq_phys_map *map; |
| 1859 | |
| 1860 | rcu_read_lock(); |
| 1861 | |
| 1862 | list_for_each_entry_rcu(entry, root, entry) { |
| 1863 | map = &entry->map; |
| 1864 | if (map->virt_irq == virt_irq) { |
| 1865 | rcu_read_unlock(); |
| 1866 | return map; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | rcu_read_unlock(); |
| 1871 | |
| 1872 | return NULL; |
| 1873 | } |
| 1874 | |
| 1875 | static void vgic_free_phys_irq_map_rcu(struct rcu_head *rcu) |
| 1876 | { |
| 1877 | struct irq_phys_map_entry *entry; |
| 1878 | |
| 1879 | entry = container_of(rcu, struct irq_phys_map_entry, rcu); |
| 1880 | kfree(entry); |
| 1881 | } |
| 1882 | |
| 1883 | /** |
Marc Zyngier | 6e84e0e | 2015-06-08 16:13:30 +0100 | [diff] [blame] | 1884 | * kvm_vgic_get_phys_irq_active - Return the active state of a mapped IRQ |
| 1885 | * |
| 1886 | * Return the logical active state of a mapped interrupt. This doesn't |
| 1887 | * necessarily reflects the current HW state. |
| 1888 | */ |
| 1889 | bool kvm_vgic_get_phys_irq_active(struct irq_phys_map *map) |
| 1890 | { |
| 1891 | BUG_ON(!map); |
| 1892 | return map->active; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * kvm_vgic_set_phys_irq_active - Set the active state of a mapped IRQ |
| 1897 | * |
| 1898 | * Set the logical active state of a mapped interrupt. This doesn't |
| 1899 | * immediately affects the HW state. |
| 1900 | */ |
| 1901 | void kvm_vgic_set_phys_irq_active(struct irq_phys_map *map, bool active) |
| 1902 | { |
| 1903 | BUG_ON(!map); |
| 1904 | map->active = active; |
| 1905 | } |
| 1906 | |
| 1907 | /** |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 1908 | * kvm_vgic_unmap_phys_irq - Remove a virtual to physical IRQ mapping |
| 1909 | * @vcpu: The VCPU pointer |
| 1910 | * @map: The pointer to a mapping obtained through kvm_vgic_map_phys_irq |
| 1911 | * |
| 1912 | * Remove an existing mapping between virtual and physical interrupts. |
| 1913 | */ |
| 1914 | int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map) |
| 1915 | { |
| 1916 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 1917 | struct irq_phys_map_entry *entry; |
| 1918 | struct list_head *root; |
| 1919 | |
| 1920 | if (!map) |
| 1921 | return -EINVAL; |
| 1922 | |
| 1923 | root = vgic_get_irq_phys_map_list(vcpu, map->virt_irq); |
| 1924 | |
| 1925 | spin_lock(&dist->irq_phys_map_lock); |
| 1926 | |
| 1927 | list_for_each_entry(entry, root, entry) { |
| 1928 | if (&entry->map == map) { |
| 1929 | list_del_rcu(&entry->entry); |
| 1930 | call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu); |
| 1931 | break; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | spin_unlock(&dist->irq_phys_map_lock); |
| 1936 | |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | static void vgic_destroy_irq_phys_map(struct kvm *kvm, struct list_head *root) |
| 1941 | { |
| 1942 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 1943 | struct irq_phys_map_entry *entry; |
| 1944 | |
| 1945 | spin_lock(&dist->irq_phys_map_lock); |
| 1946 | |
| 1947 | list_for_each_entry(entry, root, entry) { |
| 1948 | list_del_rcu(&entry->entry); |
| 1949 | call_rcu(&entry->rcu, vgic_free_phys_irq_map_rcu); |
| 1950 | } |
| 1951 | |
| 1952 | spin_unlock(&dist->irq_phys_map_lock); |
| 1953 | } |
| 1954 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1955 | void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 1956 | { |
| 1957 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1958 | |
| 1959 | kfree(vgic_cpu->pending_shared); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1960 | kfree(vgic_cpu->active_shared); |
| 1961 | kfree(vgic_cpu->pend_act_shared); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1962 | kfree(vgic_cpu->vgic_irq_lr_map); |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 1963 | vgic_destroy_irq_phys_map(vcpu->kvm, &vgic_cpu->irq_phys_map_list); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1964 | vgic_cpu->pending_shared = NULL; |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1965 | vgic_cpu->active_shared = NULL; |
| 1966 | vgic_cpu->pend_act_shared = NULL; |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1967 | vgic_cpu->vgic_irq_lr_map = NULL; |
| 1968 | } |
| 1969 | |
| 1970 | static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs) |
| 1971 | { |
| 1972 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 1973 | |
| 1974 | int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8; |
| 1975 | vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1976 | vgic_cpu->active_shared = kzalloc(sz, GFP_KERNEL); |
| 1977 | vgic_cpu->pend_act_shared = kzalloc(sz, GFP_KERNEL); |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 1978 | vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1979 | |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 1980 | if (!vgic_cpu->pending_shared |
| 1981 | || !vgic_cpu->active_shared |
| 1982 | || !vgic_cpu->pend_act_shared |
| 1983 | || !vgic_cpu->vgic_irq_lr_map) { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 1984 | kvm_vgic_vcpu_destroy(vcpu); |
| 1985 | return -ENOMEM; |
| 1986 | } |
| 1987 | |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 1988 | memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs); |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 1989 | |
| 1990 | /* |
Marc Zyngier | ca85f62 | 2013-06-18 19:17:28 +0100 | [diff] [blame] | 1991 | * Store the number of LRs per vcpu, so we don't have to go |
| 1992 | * all the way to the distributor structure to find out. Only |
| 1993 | * assembly code should use this one. |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 1994 | */ |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 1995 | vgic_cpu->nr_lr = vgic->nr_lr; |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 1996 | |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 1997 | return 0; |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 1998 | } |
| 1999 | |
Andre Przywara | 3caa2d8 | 2014-06-02 16:26:01 +0200 | [diff] [blame] | 2000 | /** |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 2001 | * kvm_vgic_vcpu_early_init - Earliest possible per-vcpu vgic init stage |
| 2002 | * |
| 2003 | * No memory allocation should be performed here, only static init. |
| 2004 | */ |
| 2005 | void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu) |
| 2006 | { |
| 2007 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; |
| 2008 | INIT_LIST_HEAD(&vgic_cpu->irq_phys_map_list); |
| 2009 | } |
| 2010 | |
| 2011 | /** |
Andre Przywara | 3caa2d8 | 2014-06-02 16:26:01 +0200 | [diff] [blame] | 2012 | * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW |
| 2013 | * |
| 2014 | * The host's GIC naturally limits the maximum amount of VCPUs a guest |
| 2015 | * can use. |
| 2016 | */ |
| 2017 | int kvm_vgic_get_max_vcpus(void) |
| 2018 | { |
| 2019 | return vgic->max_gic_vcpus; |
| 2020 | } |
| 2021 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2022 | void kvm_vgic_destroy(struct kvm *kvm) |
| 2023 | { |
| 2024 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 2025 | struct kvm_vcpu *vcpu; |
| 2026 | int i; |
| 2027 | |
| 2028 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 2029 | kvm_vgic_vcpu_destroy(vcpu); |
| 2030 | |
| 2031 | vgic_free_bitmap(&dist->irq_enabled); |
| 2032 | vgic_free_bitmap(&dist->irq_level); |
| 2033 | vgic_free_bitmap(&dist->irq_pending); |
| 2034 | vgic_free_bitmap(&dist->irq_soft_pend); |
| 2035 | vgic_free_bitmap(&dist->irq_queued); |
| 2036 | vgic_free_bitmap(&dist->irq_cfg); |
| 2037 | vgic_free_bytemap(&dist->irq_priority); |
| 2038 | if (dist->irq_spi_target) { |
| 2039 | for (i = 0; i < dist->nr_cpus; i++) |
| 2040 | vgic_free_bitmap(&dist->irq_spi_target[i]); |
| 2041 | } |
| 2042 | kfree(dist->irq_sgi_sources); |
| 2043 | kfree(dist->irq_spi_cpu); |
Andre Przywara | a0675c2 | 2014-06-07 00:54:51 +0200 | [diff] [blame] | 2044 | kfree(dist->irq_spi_mpidr); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2045 | kfree(dist->irq_spi_target); |
| 2046 | kfree(dist->irq_pending_on_cpu); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 2047 | kfree(dist->irq_active_on_cpu); |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 2048 | vgic_destroy_irq_phys_map(kvm, &dist->irq_phys_map_list); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2049 | dist->irq_sgi_sources = NULL; |
| 2050 | dist->irq_spi_cpu = NULL; |
| 2051 | dist->irq_spi_target = NULL; |
| 2052 | dist->irq_pending_on_cpu = NULL; |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 2053 | dist->irq_active_on_cpu = NULL; |
Christoffer Dall | 1f57be2 | 2014-12-09 14:30:36 +0100 | [diff] [blame] | 2054 | dist->nr_cpus = 0; |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * Allocate and initialize the various data structures. Must be called |
| 2059 | * with kvm->lock held! |
| 2060 | */ |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 2061 | int vgic_init(struct kvm *kvm) |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2062 | { |
| 2063 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 2064 | struct kvm_vcpu *vcpu; |
| 2065 | int nr_cpus, nr_irqs; |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 2066 | int ret, i, vcpu_id; |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2067 | |
Christoffer Dall | 1f57be2 | 2014-12-09 14:30:36 +0100 | [diff] [blame] | 2068 | if (vgic_initialized(kvm)) |
Marc Zyngier | 4956f2b | 2014-07-08 12:09:06 +0100 | [diff] [blame] | 2069 | return 0; |
Marc Zyngier | 5fb66da | 2014-07-08 12:09:05 +0100 | [diff] [blame] | 2070 | |
Marc Zyngier | 4956f2b | 2014-07-08 12:09:06 +0100 | [diff] [blame] | 2071 | nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus); |
| 2072 | if (!nr_cpus) /* No vcpus? Can't be good... */ |
Eric Auger | 66b030e | 2014-12-15 18:43:32 +0100 | [diff] [blame] | 2073 | return -ENODEV; |
Marc Zyngier | 4956f2b | 2014-07-08 12:09:06 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* |
| 2076 | * If nobody configured the number of interrupts, use the |
| 2077 | * legacy one. |
| 2078 | */ |
Marc Zyngier | 5fb66da | 2014-07-08 12:09:05 +0100 | [diff] [blame] | 2079 | if (!dist->nr_irqs) |
| 2080 | dist->nr_irqs = VGIC_NR_IRQS_LEGACY; |
| 2081 | |
| 2082 | nr_irqs = dist->nr_irqs; |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2083 | |
| 2084 | ret = vgic_init_bitmap(&dist->irq_enabled, nr_cpus, nr_irqs); |
| 2085 | ret |= vgic_init_bitmap(&dist->irq_level, nr_cpus, nr_irqs); |
| 2086 | ret |= vgic_init_bitmap(&dist->irq_pending, nr_cpus, nr_irqs); |
| 2087 | ret |= vgic_init_bitmap(&dist->irq_soft_pend, nr_cpus, nr_irqs); |
| 2088 | ret |= vgic_init_bitmap(&dist->irq_queued, nr_cpus, nr_irqs); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 2089 | ret |= vgic_init_bitmap(&dist->irq_active, nr_cpus, nr_irqs); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2090 | ret |= vgic_init_bitmap(&dist->irq_cfg, nr_cpus, nr_irqs); |
| 2091 | ret |= vgic_init_bytemap(&dist->irq_priority, nr_cpus, nr_irqs); |
| 2092 | |
| 2093 | if (ret) |
| 2094 | goto out; |
| 2095 | |
| 2096 | dist->irq_sgi_sources = kzalloc(nr_cpus * VGIC_NR_SGIS, GFP_KERNEL); |
| 2097 | dist->irq_spi_cpu = kzalloc(nr_irqs - VGIC_NR_PRIVATE_IRQS, GFP_KERNEL); |
| 2098 | dist->irq_spi_target = kzalloc(sizeof(*dist->irq_spi_target) * nr_cpus, |
| 2099 | GFP_KERNEL); |
| 2100 | dist->irq_pending_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long), |
| 2101 | GFP_KERNEL); |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 2102 | dist->irq_active_on_cpu = kzalloc(BITS_TO_LONGS(nr_cpus) * sizeof(long), |
| 2103 | GFP_KERNEL); |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2104 | if (!dist->irq_sgi_sources || |
| 2105 | !dist->irq_spi_cpu || |
| 2106 | !dist->irq_spi_target || |
Christoffer Dall | 47a98b1 | 2015-03-13 17:02:54 +0000 | [diff] [blame] | 2107 | !dist->irq_pending_on_cpu || |
| 2108 | !dist->irq_active_on_cpu) { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2109 | ret = -ENOMEM; |
| 2110 | goto out; |
| 2111 | } |
| 2112 | |
| 2113 | for (i = 0; i < nr_cpus; i++) |
| 2114 | ret |= vgic_init_bitmap(&dist->irq_spi_target[i], |
| 2115 | nr_cpus, nr_irqs); |
| 2116 | |
| 2117 | if (ret) |
| 2118 | goto out; |
| 2119 | |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 2120 | ret = kvm->arch.vgic.vm_ops.init_model(kvm); |
| 2121 | if (ret) |
| 2122 | goto out; |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 2123 | |
| 2124 | kvm_for_each_vcpu(vcpu_id, vcpu, kvm) { |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2125 | ret = vgic_vcpu_init_maps(vcpu, nr_irqs); |
| 2126 | if (ret) { |
| 2127 | kvm_err("VGIC: Failed to allocate vcpu memory\n"); |
| 2128 | break; |
| 2129 | } |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2130 | |
Christoffer Dall | 54723bb | 2015-08-30 14:45:20 +0200 | [diff] [blame] | 2131 | /* |
| 2132 | * Enable all SGIs and configure all private IRQs as |
| 2133 | * edge-triggered. |
| 2134 | */ |
| 2135 | for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) { |
| 2136 | if (i < VGIC_NR_SGIS) |
Peter Maydell | 6d3cfbe | 2014-12-04 15:02:24 +0000 | [diff] [blame] | 2137 | vgic_bitmap_set_irq_val(&dist->irq_enabled, |
| 2138 | vcpu->vcpu_id, i, 1); |
| 2139 | if (i < VGIC_NR_PRIVATE_IRQS) |
| 2140 | vgic_bitmap_set_irq_val(&dist->irq_cfg, |
| 2141 | vcpu->vcpu_id, i, |
| 2142 | VGIC_CFG_EDGE); |
| 2143 | } |
| 2144 | |
| 2145 | vgic_enable(vcpu); |
| 2146 | } |
Marc Zyngier | 4956f2b | 2014-07-08 12:09:06 +0100 | [diff] [blame] | 2147 | |
Marc Zyngier | c1bfb57 | 2014-07-08 12:09:01 +0100 | [diff] [blame] | 2148 | out: |
| 2149 | if (ret) |
| 2150 | kvm_vgic_destroy(kvm); |
| 2151 | |
| 2152 | return ret; |
| 2153 | } |
| 2154 | |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 2155 | static int init_vgic_model(struct kvm *kvm, int type) |
| 2156 | { |
| 2157 | switch (type) { |
| 2158 | case KVM_DEV_TYPE_ARM_VGIC_V2: |
| 2159 | vgic_v2_init_emulation(kvm); |
| 2160 | break; |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 2161 | #ifdef CONFIG_ARM_GIC_V3 |
| 2162 | case KVM_DEV_TYPE_ARM_VGIC_V3: |
| 2163 | vgic_v3_init_emulation(kvm); |
| 2164 | break; |
| 2165 | #endif |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 2166 | default: |
| 2167 | return -ENODEV; |
| 2168 | } |
| 2169 | |
Andre Przywara | 3caa2d8 | 2014-06-02 16:26:01 +0200 | [diff] [blame] | 2170 | if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) |
| 2171 | return -E2BIG; |
| 2172 | |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 2173 | return 0; |
| 2174 | } |
| 2175 | |
Marc Zyngier | 6c3d63c | 2014-06-23 17:37:18 +0100 | [diff] [blame] | 2176 | /** |
| 2177 | * kvm_vgic_early_init - Earliest possible vgic initialization stage |
| 2178 | * |
| 2179 | * No memory allocation should be performed here, only static init. |
| 2180 | */ |
| 2181 | void kvm_vgic_early_init(struct kvm *kvm) |
| 2182 | { |
| 2183 | spin_lock_init(&kvm->arch.vgic.lock); |
| 2184 | spin_lock_init(&kvm->arch.vgic.irq_phys_map_lock); |
| 2185 | INIT_LIST_HEAD(&kvm->arch.vgic.irq_phys_map_list); |
| 2186 | } |
| 2187 | |
Andre Przywara | 59892136 | 2014-06-03 09:33:10 +0200 | [diff] [blame] | 2188 | int kvm_vgic_create(struct kvm *kvm, u32 type) |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2189 | { |
Christoffer Dall | 6b50f54 | 2014-11-06 11:47:39 +0000 | [diff] [blame] | 2190 | int i, vcpu_lock_idx = -1, ret; |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2191 | struct kvm_vcpu *vcpu; |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2192 | |
| 2193 | mutex_lock(&kvm->lock); |
| 2194 | |
Andre Przywara | 4ce7ebd | 2014-10-26 23:18:14 +0000 | [diff] [blame] | 2195 | if (irqchip_in_kernel(kvm)) { |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2196 | ret = -EEXIST; |
| 2197 | goto out; |
| 2198 | } |
| 2199 | |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2200 | /* |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 2201 | * This function is also called by the KVM_CREATE_IRQCHIP handler, |
| 2202 | * which had no chance yet to check the availability of the GICv2 |
| 2203 | * emulation. So check this here again. KVM_CREATE_DEVICE does |
| 2204 | * the proper checks already. |
| 2205 | */ |
Wei Yongjun | b52104e | 2015-02-27 19:41:45 +0800 | [diff] [blame] | 2206 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && !vgic->can_emulate_gicv2) { |
| 2207 | ret = -ENODEV; |
| 2208 | goto out; |
| 2209 | } |
Andre Przywara | b5d84ff | 2014-06-03 10:26:03 +0200 | [diff] [blame] | 2210 | |
| 2211 | /* |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2212 | * Any time a vcpu is run, vcpu_load is called which tries to grab the |
| 2213 | * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure |
| 2214 | * that no other VCPUs are run while we create the vgic. |
| 2215 | */ |
Christoffer Dall | 6b50f54 | 2014-11-06 11:47:39 +0000 | [diff] [blame] | 2216 | ret = -EBUSY; |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2217 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2218 | if (!mutex_trylock(&vcpu->mutex)) |
| 2219 | goto out_unlock; |
| 2220 | vcpu_lock_idx = i; |
| 2221 | } |
| 2222 | |
| 2223 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Christoffer Dall | 6b50f54 | 2014-11-06 11:47:39 +0000 | [diff] [blame] | 2224 | if (vcpu->arch.has_run_once) |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2225 | goto out_unlock; |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2226 | } |
Christoffer Dall | 6b50f54 | 2014-11-06 11:47:39 +0000 | [diff] [blame] | 2227 | ret = 0; |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2228 | |
Andre Przywara | b26e5fd | 2014-06-02 16:19:12 +0200 | [diff] [blame] | 2229 | ret = init_vgic_model(kvm, type); |
| 2230 | if (ret) |
| 2231 | goto out_unlock; |
| 2232 | |
Marc Zyngier | f982cf4 | 2014-05-15 10:03:25 +0100 | [diff] [blame] | 2233 | kvm->arch.vgic.in_kernel = true; |
Andre Przywara | 59892136 | 2014-06-03 09:33:10 +0200 | [diff] [blame] | 2234 | kvm->arch.vgic.vgic_model = type; |
Marc Zyngier | 8f186d5 | 2014-02-04 18:13:03 +0000 | [diff] [blame] | 2235 | kvm->arch.vgic.vctrl_base = vgic->vctrl_base; |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2236 | kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; |
| 2237 | kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; |
Andre Przywara | a0675c2 | 2014-06-07 00:54:51 +0200 | [diff] [blame] | 2238 | kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF; |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2239 | |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2240 | out_unlock: |
| 2241 | for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { |
| 2242 | vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); |
| 2243 | mutex_unlock(&vcpu->mutex); |
| 2244 | } |
| 2245 | |
Marc Zyngier | 01ac5e3 | 2013-01-21 19:36:16 -0500 | [diff] [blame] | 2246 | out: |
| 2247 | mutex_unlock(&kvm->lock); |
| 2248 | return ret; |
| 2249 | } |
| 2250 | |
Will Deacon | 1fa451b | 2014-08-26 15:13:24 +0100 | [diff] [blame] | 2251 | static int vgic_ioaddr_overlap(struct kvm *kvm) |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2252 | { |
| 2253 | phys_addr_t dist = kvm->arch.vgic.vgic_dist_base; |
| 2254 | phys_addr_t cpu = kvm->arch.vgic.vgic_cpu_base; |
| 2255 | |
| 2256 | if (IS_VGIC_ADDR_UNDEF(dist) || IS_VGIC_ADDR_UNDEF(cpu)) |
| 2257 | return 0; |
| 2258 | if ((dist <= cpu && dist + KVM_VGIC_V2_DIST_SIZE > cpu) || |
| 2259 | (cpu <= dist && cpu + KVM_VGIC_V2_CPU_SIZE > dist)) |
| 2260 | return -EBUSY; |
| 2261 | return 0; |
| 2262 | } |
| 2263 | |
| 2264 | static int vgic_ioaddr_assign(struct kvm *kvm, phys_addr_t *ioaddr, |
| 2265 | phys_addr_t addr, phys_addr_t size) |
| 2266 | { |
| 2267 | int ret; |
| 2268 | |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2269 | if (addr & ~KVM_PHYS_MASK) |
| 2270 | return -E2BIG; |
| 2271 | |
| 2272 | if (addr & (SZ_4K - 1)) |
| 2273 | return -EINVAL; |
| 2274 | |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2275 | if (!IS_VGIC_ADDR_UNDEF(*ioaddr)) |
| 2276 | return -EEXIST; |
| 2277 | if (addr + size < addr) |
| 2278 | return -EINVAL; |
| 2279 | |
Haibin Wang | 30c2117 | 2014-04-29 14:49:17 +0800 | [diff] [blame] | 2280 | *ioaddr = addr; |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2281 | ret = vgic_ioaddr_overlap(kvm); |
| 2282 | if (ret) |
Haibin Wang | 30c2117 | 2014-04-29 14:49:17 +0800 | [diff] [blame] | 2283 | *ioaddr = VGIC_ADDR_UNDEF; |
| 2284 | |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2285 | return ret; |
| 2286 | } |
| 2287 | |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2288 | /** |
| 2289 | * kvm_vgic_addr - set or get vgic VM base addresses |
| 2290 | * @kvm: pointer to the vm struct |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2291 | * @type: the VGIC addr type, one of KVM_VGIC_V[23]_ADDR_TYPE_XXX |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2292 | * @addr: pointer to address value |
| 2293 | * @write: if true set the address in the VM address space, if false read the |
| 2294 | * address |
| 2295 | * |
| 2296 | * Set or get the vgic base addresses for the distributor and the virtual CPU |
| 2297 | * interface in the VM physical address space. These addresses are properties |
| 2298 | * of the emulated core/SoC and therefore user space initially knows this |
| 2299 | * information. |
| 2300 | */ |
| 2301 | int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write) |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2302 | { |
| 2303 | int r = 0; |
| 2304 | struct vgic_dist *vgic = &kvm->arch.vgic; |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2305 | int type_needed; |
| 2306 | phys_addr_t *addr_ptr, block_size; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2307 | phys_addr_t alignment; |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2308 | |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2309 | mutex_lock(&kvm->lock); |
| 2310 | switch (type) { |
| 2311 | case KVM_VGIC_V2_ADDR_TYPE_DIST: |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2312 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; |
| 2313 | addr_ptr = &vgic->vgic_dist_base; |
| 2314 | block_size = KVM_VGIC_V2_DIST_SIZE; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2315 | alignment = SZ_4K; |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2316 | break; |
| 2317 | case KVM_VGIC_V2_ADDR_TYPE_CPU: |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2318 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V2; |
| 2319 | addr_ptr = &vgic->vgic_cpu_base; |
| 2320 | block_size = KVM_VGIC_V2_CPU_SIZE; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2321 | alignment = SZ_4K; |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2322 | break; |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2323 | #ifdef CONFIG_ARM_GIC_V3 |
| 2324 | case KVM_VGIC_V3_ADDR_TYPE_DIST: |
| 2325 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; |
| 2326 | addr_ptr = &vgic->vgic_dist_base; |
| 2327 | block_size = KVM_VGIC_V3_DIST_SIZE; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2328 | alignment = SZ_64K; |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2329 | break; |
| 2330 | case KVM_VGIC_V3_ADDR_TYPE_REDIST: |
| 2331 | type_needed = KVM_DEV_TYPE_ARM_VGIC_V3; |
| 2332 | addr_ptr = &vgic->vgic_redist_base; |
| 2333 | block_size = KVM_VGIC_V3_REDIST_SIZE; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2334 | alignment = SZ_64K; |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2335 | break; |
| 2336 | #endif |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2337 | default: |
| 2338 | r = -ENODEV; |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2339 | goto out; |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2340 | } |
| 2341 | |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2342 | if (vgic->vgic_model != type_needed) { |
| 2343 | r = -ENODEV; |
| 2344 | goto out; |
| 2345 | } |
| 2346 | |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2347 | if (write) { |
| 2348 | if (!IS_ALIGNED(*addr, alignment)) |
| 2349 | r = -EINVAL; |
| 2350 | else |
| 2351 | r = vgic_ioaddr_assign(kvm, addr_ptr, *addr, |
| 2352 | block_size); |
| 2353 | } else { |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2354 | *addr = *addr_ptr; |
Andre Przywara | 4fa96afd | 2015-01-13 12:02:13 +0000 | [diff] [blame] | 2355 | } |
Andre Przywara | ac3d373 | 2014-06-03 10:26:30 +0200 | [diff] [blame] | 2356 | |
| 2357 | out: |
Christoffer Dall | 330690c | 2013-01-21 19:36:13 -0500 | [diff] [blame] | 2358 | mutex_unlock(&kvm->lock); |
| 2359 | return r; |
| 2360 | } |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2361 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 2362 | int vgic_set_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2363 | { |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2364 | int r; |
| 2365 | |
| 2366 | switch (attr->group) { |
| 2367 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 2368 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2369 | u64 addr; |
| 2370 | unsigned long type = (unsigned long)attr->attr; |
| 2371 | |
| 2372 | if (copy_from_user(&addr, uaddr, sizeof(addr))) |
| 2373 | return -EFAULT; |
| 2374 | |
| 2375 | r = kvm_vgic_addr(dev->kvm, type, &addr, true); |
| 2376 | return (r == -ENODEV) ? -ENXIO : r; |
| 2377 | } |
Marc Zyngier | a98f26f | 2014-07-08 12:09:07 +0100 | [diff] [blame] | 2378 | case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { |
| 2379 | u32 __user *uaddr = (u32 __user *)(long)attr->addr; |
| 2380 | u32 val; |
| 2381 | int ret = 0; |
| 2382 | |
| 2383 | if (get_user(val, uaddr)) |
| 2384 | return -EFAULT; |
| 2385 | |
| 2386 | /* |
| 2387 | * We require: |
| 2388 | * - at least 32 SPIs on top of the 16 SGIs and 16 PPIs |
| 2389 | * - at most 1024 interrupts |
| 2390 | * - a multiple of 32 interrupts |
| 2391 | */ |
| 2392 | if (val < (VGIC_NR_PRIVATE_IRQS + 32) || |
| 2393 | val > VGIC_MAX_IRQS || |
| 2394 | (val & 31)) |
| 2395 | return -EINVAL; |
| 2396 | |
| 2397 | mutex_lock(&dev->kvm->lock); |
| 2398 | |
Christoffer Dall | c52edf5 | 2014-12-09 14:28:09 +0100 | [diff] [blame] | 2399 | if (vgic_ready(dev->kvm) || dev->kvm->arch.vgic.nr_irqs) |
Marc Zyngier | a98f26f | 2014-07-08 12:09:07 +0100 | [diff] [blame] | 2400 | ret = -EBUSY; |
| 2401 | else |
| 2402 | dev->kvm->arch.vgic.nr_irqs = val; |
| 2403 | |
| 2404 | mutex_unlock(&dev->kvm->lock); |
| 2405 | |
| 2406 | return ret; |
| 2407 | } |
Eric Auger | 065c003 | 2014-12-15 18:43:33 +0100 | [diff] [blame] | 2408 | case KVM_DEV_ARM_VGIC_GRP_CTRL: { |
| 2409 | switch (attr->attr) { |
| 2410 | case KVM_DEV_ARM_VGIC_CTRL_INIT: |
| 2411 | r = vgic_init(dev->kvm); |
| 2412 | return r; |
| 2413 | } |
| 2414 | break; |
| 2415 | } |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2418 | return -ENXIO; |
| 2419 | } |
| 2420 | |
Andre Przywara | 8321581 | 2014-06-07 00:53:08 +0200 | [diff] [blame] | 2421 | int vgic_get_common_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2422 | { |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2423 | int r = -ENXIO; |
| 2424 | |
| 2425 | switch (attr->group) { |
| 2426 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 2427 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2428 | u64 addr; |
| 2429 | unsigned long type = (unsigned long)attr->attr; |
| 2430 | |
| 2431 | r = kvm_vgic_addr(dev->kvm, type, &addr, false); |
| 2432 | if (r) |
| 2433 | return (r == -ENODEV) ? -ENXIO : r; |
| 2434 | |
| 2435 | if (copy_to_user(uaddr, &addr, sizeof(addr))) |
| 2436 | return -EFAULT; |
Christoffer Dall | c07a019 | 2013-10-25 21:17:31 +0100 | [diff] [blame] | 2437 | break; |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2438 | } |
Marc Zyngier | a98f26f | 2014-07-08 12:09:07 +0100 | [diff] [blame] | 2439 | case KVM_DEV_ARM_VGIC_GRP_NR_IRQS: { |
| 2440 | u32 __user *uaddr = (u32 __user *)(long)attr->addr; |
Andre Przywara | b60da14 | 2014-08-21 11:08:27 +0100 | [diff] [blame] | 2441 | |
Marc Zyngier | a98f26f | 2014-07-08 12:09:07 +0100 | [diff] [blame] | 2442 | r = put_user(dev->kvm->arch.vgic.nr_irqs, uaddr); |
| 2443 | break; |
| 2444 | } |
Christoffer Dall | c07a019 | 2013-10-25 21:17:31 +0100 | [diff] [blame] | 2445 | |
Christoffer Dall | ce01e4e | 2013-09-23 14:55:56 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | return r; |
Christoffer Dall | 7330672 | 2013-10-25 17:29:18 +0100 | [diff] [blame] | 2449 | } |
| 2450 | |
Andre Przywara | cf50a1e | 2015-03-26 14:39:32 +0000 | [diff] [blame] | 2451 | int vgic_has_attr_regs(const struct vgic_io_range *ranges, phys_addr_t offset) |
Christoffer Dall | c07a019 | 2013-10-25 21:17:31 +0100 | [diff] [blame] | 2452 | { |
Andre Przywara | 9f199d0 | 2015-03-26 14:39:33 +0000 | [diff] [blame] | 2453 | if (vgic_find_range(ranges, 4, offset)) |
Christoffer Dall | c07a019 | 2013-10-25 21:17:31 +0100 | [diff] [blame] | 2454 | return 0; |
| 2455 | else |
| 2456 | return -ENXIO; |
| 2457 | } |
| 2458 | |
Will Deacon | c06a841 | 2014-09-02 10:27:34 +0100 | [diff] [blame] | 2459 | static void vgic_init_maintenance_interrupt(void *info) |
| 2460 | { |
| 2461 | enable_percpu_irq(vgic->maint_irq, 0); |
| 2462 | } |
| 2463 | |
| 2464 | static int vgic_cpu_notify(struct notifier_block *self, |
| 2465 | unsigned long action, void *cpu) |
| 2466 | { |
| 2467 | switch (action) { |
| 2468 | case CPU_STARTING: |
| 2469 | case CPU_STARTING_FROZEN: |
| 2470 | vgic_init_maintenance_interrupt(NULL); |
| 2471 | break; |
| 2472 | case CPU_DYING: |
| 2473 | case CPU_DYING_FROZEN: |
| 2474 | disable_percpu_irq(vgic->maint_irq); |
| 2475 | break; |
| 2476 | } |
| 2477 | |
| 2478 | return NOTIFY_OK; |
| 2479 | } |
| 2480 | |
| 2481 | static struct notifier_block vgic_cpu_nb = { |
| 2482 | .notifier_call = vgic_cpu_notify, |
| 2483 | }; |
| 2484 | |
| 2485 | static const struct of_device_id vgic_ids[] = { |
Mark Rutland | 0f372475 | 2015-03-05 14:47:44 +0000 | [diff] [blame] | 2486 | { .compatible = "arm,cortex-a15-gic", .data = vgic_v2_probe, }, |
| 2487 | { .compatible = "arm,cortex-a7-gic", .data = vgic_v2_probe, }, |
| 2488 | { .compatible = "arm,gic-400", .data = vgic_v2_probe, }, |
| 2489 | { .compatible = "arm,gic-v3", .data = vgic_v3_probe, }, |
Will Deacon | c06a841 | 2014-09-02 10:27:34 +0100 | [diff] [blame] | 2490 | {}, |
| 2491 | }; |
| 2492 | |
| 2493 | int kvm_vgic_hyp_init(void) |
| 2494 | { |
| 2495 | const struct of_device_id *matched_id; |
Christoffer Dall | a875daf | 2014-09-18 18:15:32 -0700 | [diff] [blame] | 2496 | const int (*vgic_probe)(struct device_node *,const struct vgic_ops **, |
| 2497 | const struct vgic_params **); |
Will Deacon | c06a841 | 2014-09-02 10:27:34 +0100 | [diff] [blame] | 2498 | struct device_node *vgic_node; |
| 2499 | int ret; |
| 2500 | |
| 2501 | vgic_node = of_find_matching_node_and_match(NULL, |
| 2502 | vgic_ids, &matched_id); |
| 2503 | if (!vgic_node) { |
| 2504 | kvm_err("error: no compatible GIC node found\n"); |
| 2505 | return -ENODEV; |
| 2506 | } |
| 2507 | |
| 2508 | vgic_probe = matched_id->data; |
| 2509 | ret = vgic_probe(vgic_node, &vgic_ops, &vgic); |
| 2510 | if (ret) |
| 2511 | return ret; |
| 2512 | |
| 2513 | ret = request_percpu_irq(vgic->maint_irq, vgic_maintenance_handler, |
| 2514 | "vgic", kvm_get_running_vcpus()); |
| 2515 | if (ret) { |
| 2516 | kvm_err("Cannot register interrupt %d\n", vgic->maint_irq); |
| 2517 | return ret; |
| 2518 | } |
| 2519 | |
| 2520 | ret = __register_cpu_notifier(&vgic_cpu_nb); |
| 2521 | if (ret) { |
| 2522 | kvm_err("Cannot register vgic CPU notifier\n"); |
| 2523 | goto out_free_irq; |
| 2524 | } |
| 2525 | |
Will Deacon | c06a841 | 2014-09-02 10:27:34 +0100 | [diff] [blame] | 2526 | on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1); |
| 2527 | |
Andre Przywara | ea2f83a | 2014-10-26 23:17:00 +0000 | [diff] [blame] | 2528 | return 0; |
Will Deacon | c06a841 | 2014-09-02 10:27:34 +0100 | [diff] [blame] | 2529 | |
| 2530 | out_free_irq: |
| 2531 | free_percpu_irq(vgic->maint_irq, kvm_get_running_vcpus()); |
| 2532 | return ret; |
| 2533 | } |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 2534 | |
| 2535 | int kvm_irq_map_gsi(struct kvm *kvm, |
| 2536 | struct kvm_kernel_irq_routing_entry *entries, |
| 2537 | int gsi) |
| 2538 | { |
Eric Auger | 0b3289e | 2015-04-13 15:01:59 +0200 | [diff] [blame] | 2539 | return 0; |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin) |
| 2543 | { |
| 2544 | return pin; |
| 2545 | } |
| 2546 | |
| 2547 | int kvm_set_irq(struct kvm *kvm, int irq_source_id, |
| 2548 | u32 irq, int level, bool line_status) |
| 2549 | { |
| 2550 | unsigned int spi = irq + VGIC_NR_PRIVATE_IRQS; |
| 2551 | |
| 2552 | trace_kvm_set_irq(irq, level, irq_source_id); |
| 2553 | |
| 2554 | BUG_ON(!vgic_initialized(kvm)); |
| 2555 | |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 2556 | return kvm_vgic_inject_irq(kvm, 0, spi, level); |
Eric Auger | 174178f | 2015-03-04 11:14:36 +0100 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | /* MSI not implemented yet */ |
| 2560 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
| 2561 | struct kvm *kvm, int irq_source_id, |
| 2562 | int level, bool line_status) |
| 2563 | { |
| 2564 | return 0; |
| 2565 | } |