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