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