Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * VGICv2 MMIO handling functions |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/irqchip/arm-gic.h> |
| 15 | #include <linux/kvm.h> |
| 16 | #include <linux/kvm_host.h> |
| 17 | #include <kvm/iodev.h> |
| 18 | #include <kvm/arm_vgic.h> |
| 19 | |
| 20 | #include "vgic.h" |
| 21 | #include "vgic-mmio.h" |
| 22 | |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 23 | static unsigned long vgic_mmio_read_v2_misc(struct kvm_vcpu *vcpu, |
| 24 | gpa_t addr, unsigned int len) |
| 25 | { |
| 26 | u32 value; |
| 27 | |
| 28 | switch (addr & 0x0c) { |
| 29 | case GIC_DIST_CTRL: |
| 30 | value = vcpu->kvm->arch.vgic.enabled ? GICD_ENABLE : 0; |
| 31 | break; |
| 32 | case GIC_DIST_CTR: |
| 33 | value = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 34 | value = (value >> 5) - 1; |
| 35 | value |= (atomic_read(&vcpu->kvm->online_vcpus) - 1) << 5; |
| 36 | break; |
| 37 | case GIC_DIST_IIDR: |
| 38 | value = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); |
| 39 | break; |
| 40 | default: |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | return value; |
| 45 | } |
| 46 | |
| 47 | static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu, |
| 48 | gpa_t addr, unsigned int len, |
| 49 | unsigned long val) |
| 50 | { |
| 51 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
| 52 | bool was_enabled = dist->enabled; |
| 53 | |
| 54 | switch (addr & 0x0c) { |
| 55 | case GIC_DIST_CTRL: |
| 56 | dist->enabled = val & GICD_ENABLE; |
| 57 | if (!was_enabled && dist->enabled) |
| 58 | vgic_kick_vcpus(vcpu->kvm); |
| 59 | break; |
| 60 | case GIC_DIST_CTR: |
| 61 | case GIC_DIST_IIDR: |
| 62 | /* Nothing to do */ |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 67 | static const struct vgic_register_region vgic_v2_dist_registers[] = { |
| 68 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 69 | vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 70 | VGIC_ACCESS_32bit), |
| 71 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_IGROUP, |
| 72 | vgic_mmio_read_rao, vgic_mmio_write_wi, 1, |
| 73 | VGIC_ACCESS_32bit), |
| 74 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_SET, |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 75 | vgic_mmio_read_enable, vgic_mmio_write_senable, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 76 | VGIC_ACCESS_32bit), |
| 77 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_CLEAR, |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 78 | vgic_mmio_read_enable, vgic_mmio_write_cenable, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 79 | VGIC_ACCESS_32bit), |
| 80 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET, |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame^] | 81 | vgic_mmio_read_pending, vgic_mmio_write_spending, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 82 | VGIC_ACCESS_32bit), |
| 83 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_CLEAR, |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame^] | 84 | vgic_mmio_read_pending, vgic_mmio_write_cpending, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 85 | VGIC_ACCESS_32bit), |
| 86 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET, |
| 87 | vgic_mmio_read_raz, vgic_mmio_write_wi, 1, |
| 88 | VGIC_ACCESS_32bit), |
| 89 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_CLEAR, |
| 90 | vgic_mmio_read_raz, vgic_mmio_write_wi, 1, |
| 91 | VGIC_ACCESS_32bit), |
| 92 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, |
| 93 | vgic_mmio_read_raz, vgic_mmio_write_wi, 8, |
| 94 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 95 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET, |
| 96 | vgic_mmio_read_raz, vgic_mmio_write_wi, 8, |
| 97 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 98 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG, |
| 99 | vgic_mmio_read_raz, vgic_mmio_write_wi, 2, |
| 100 | VGIC_ACCESS_32bit), |
| 101 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT, |
| 102 | vgic_mmio_read_raz, vgic_mmio_write_wi, 4, |
| 103 | VGIC_ACCESS_32bit), |
| 104 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR, |
| 105 | vgic_mmio_read_raz, vgic_mmio_write_wi, 16, |
| 106 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 107 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_SET, |
| 108 | vgic_mmio_read_raz, vgic_mmio_write_wi, 16, |
| 109 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 110 | }; |
| 111 | |
| 112 | unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev) |
| 113 | { |
| 114 | dev->regions = vgic_v2_dist_registers; |
| 115 | dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers); |
| 116 | |
| 117 | kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops); |
| 118 | |
| 119 | return SZ_4K; |
| 120 | } |