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