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