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