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