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 | 55cc01f | 2015-12-01 12:42:05 +0000 | [diff] [blame^] | 67 | static void vgic_mmio_write_sgir(struct kvm_vcpu *source_vcpu, |
| 68 | gpa_t addr, unsigned int len, |
| 69 | unsigned long val) |
| 70 | { |
| 71 | int nr_vcpus = atomic_read(&source_vcpu->kvm->online_vcpus); |
| 72 | int intid = val & 0xf; |
| 73 | int targets = (val >> 16) & 0xff; |
| 74 | int mode = (val >> 24) & 0x03; |
| 75 | int c; |
| 76 | struct kvm_vcpu *vcpu; |
| 77 | |
| 78 | switch (mode) { |
| 79 | case 0x0: /* as specified by targets */ |
| 80 | break; |
| 81 | case 0x1: |
| 82 | targets = (1U << nr_vcpus) - 1; /* all, ... */ |
| 83 | targets &= ~(1U << source_vcpu->vcpu_id); /* but self */ |
| 84 | break; |
| 85 | case 0x2: /* this very vCPU only */ |
| 86 | targets = (1U << source_vcpu->vcpu_id); |
| 87 | break; |
| 88 | case 0x3: /* reserved */ |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | kvm_for_each_vcpu(c, vcpu, source_vcpu->kvm) { |
| 93 | struct vgic_irq *irq; |
| 94 | |
| 95 | if (!(targets & (1U << c))) |
| 96 | continue; |
| 97 | |
| 98 | irq = vgic_get_irq(source_vcpu->kvm, vcpu, intid); |
| 99 | |
| 100 | spin_lock(&irq->irq_lock); |
| 101 | irq->pending = true; |
| 102 | irq->source |= 1U << source_vcpu->vcpu_id; |
| 103 | |
| 104 | vgic_queue_irq_unlock(source_vcpu->kvm, irq); |
| 105 | } |
| 106 | } |
| 107 | |
Andre Przywara | 2c234d6 | 2015-12-01 12:41:55 +0000 | [diff] [blame] | 108 | static unsigned long vgic_mmio_read_target(struct kvm_vcpu *vcpu, |
| 109 | gpa_t addr, unsigned int len) |
| 110 | { |
| 111 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 112 | int i; |
| 113 | u64 val = 0; |
| 114 | |
| 115 | for (i = 0; i < len; i++) { |
| 116 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 117 | |
| 118 | val |= (u64)irq->targets << (i * 8); |
| 119 | } |
| 120 | |
| 121 | return val; |
| 122 | } |
| 123 | |
| 124 | static void vgic_mmio_write_target(struct kvm_vcpu *vcpu, |
| 125 | gpa_t addr, unsigned int len, |
| 126 | unsigned long val) |
| 127 | { |
| 128 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 129 | int i; |
| 130 | |
| 131 | /* GICD_ITARGETSR[0-7] are read-only */ |
| 132 | if (intid < VGIC_NR_PRIVATE_IRQS) |
| 133 | return; |
| 134 | |
| 135 | for (i = 0; i < len; i++) { |
| 136 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, NULL, intid + i); |
| 137 | int target; |
| 138 | |
| 139 | spin_lock(&irq->irq_lock); |
| 140 | |
| 141 | irq->targets = (val >> (i * 8)) & 0xff; |
| 142 | target = irq->targets ? __ffs(irq->targets) : 0; |
| 143 | irq->target_vcpu = kvm_get_vcpu(vcpu->kvm, target); |
| 144 | |
| 145 | spin_unlock(&irq->irq_lock); |
| 146 | } |
| 147 | } |
| 148 | |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 149 | static const struct vgic_register_region vgic_v2_dist_registers[] = { |
| 150 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_CTRL, |
Marc Zyngier | 2b0cda8 | 2016-04-26 11:06:47 +0100 | [diff] [blame] | 151 | vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 152 | VGIC_ACCESS_32bit), |
| 153 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_IGROUP, |
| 154 | vgic_mmio_read_rao, vgic_mmio_write_wi, 1, |
| 155 | VGIC_ACCESS_32bit), |
| 156 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_SET, |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 157 | vgic_mmio_read_enable, vgic_mmio_write_senable, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 158 | VGIC_ACCESS_32bit), |
| 159 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ENABLE_CLEAR, |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 160 | vgic_mmio_read_enable, vgic_mmio_write_cenable, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 161 | VGIC_ACCESS_32bit), |
| 162 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_SET, |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 163 | vgic_mmio_read_pending, vgic_mmio_write_spending, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 164 | VGIC_ACCESS_32bit), |
| 165 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PENDING_CLEAR, |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 166 | vgic_mmio_read_pending, vgic_mmio_write_cpending, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 167 | VGIC_ACCESS_32bit), |
| 168 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_SET, |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 169 | vgic_mmio_read_active, vgic_mmio_write_sactive, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 170 | VGIC_ACCESS_32bit), |
| 171 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_ACTIVE_CLEAR, |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 172 | vgic_mmio_read_active, vgic_mmio_write_cactive, 1, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 173 | VGIC_ACCESS_32bit), |
| 174 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_PRI, |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 175 | vgic_mmio_read_priority, vgic_mmio_write_priority, 8, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 176 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 177 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_TARGET, |
Andre Przywara | 2c234d6 | 2015-12-01 12:41:55 +0000 | [diff] [blame] | 178 | vgic_mmio_read_target, vgic_mmio_write_target, 8, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 179 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 180 | REGISTER_DESC_WITH_BITS_PER_IRQ(GIC_DIST_CONFIG, |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 181 | vgic_mmio_read_config, vgic_mmio_write_config, 2, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 182 | VGIC_ACCESS_32bit), |
| 183 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SOFTINT, |
Andre Przywara | 55cc01f | 2015-12-01 12:42:05 +0000 | [diff] [blame^] | 184 | vgic_mmio_read_raz, vgic_mmio_write_sgir, 4, |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 185 | VGIC_ACCESS_32bit), |
| 186 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_CLEAR, |
| 187 | vgic_mmio_read_raz, vgic_mmio_write_wi, 16, |
| 188 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 189 | REGISTER_DESC_WITH_LENGTH(GIC_DIST_SGI_PENDING_SET, |
| 190 | vgic_mmio_read_raz, vgic_mmio_write_wi, 16, |
| 191 | VGIC_ACCESS_32bit | VGIC_ACCESS_8bit), |
| 192 | }; |
| 193 | |
| 194 | unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev) |
| 195 | { |
| 196 | dev->regions = vgic_v2_dist_registers; |
| 197 | dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers); |
| 198 | |
| 199 | kvm_iodevice_init(&dev->dev, &kvm_io_gic_ops); |
| 200 | |
| 201 | return SZ_4K; |
| 202 | } |