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