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