Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * VGIC 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/bitops.h> |
| 15 | #include <linux/bsearch.h> |
| 16 | #include <linux/kvm.h> |
| 17 | #include <linux/kvm_host.h> |
| 18 | #include <kvm/iodev.h> |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 19 | #include <kvm/arm_arch_timer.h> |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 20 | #include <kvm/arm_vgic.h> |
| 21 | |
| 22 | #include "vgic.h" |
| 23 | #include "vgic-mmio.h" |
| 24 | |
| 25 | unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu, |
| 26 | gpa_t addr, unsigned int len) |
| 27 | { |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu, |
| 32 | gpa_t addr, unsigned int len) |
| 33 | { |
| 34 | return -1UL; |
| 35 | } |
| 36 | |
| 37 | void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr, |
| 38 | unsigned int len, unsigned long val) |
| 39 | { |
| 40 | /* Ignore */ |
| 41 | } |
| 42 | |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value |
| 45 | * of the enabled bit, so there is only one function for both here. |
| 46 | */ |
| 47 | unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu, |
| 48 | gpa_t addr, unsigned int len) |
| 49 | { |
| 50 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 51 | u32 value = 0; |
| 52 | int i; |
| 53 | |
| 54 | /* Loop over all IRQs affected by this read */ |
| 55 | for (i = 0; i < len * 8; i++) { |
| 56 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 57 | |
| 58 | if (irq->enabled) |
| 59 | value |= (1U << i); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 60 | |
| 61 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return value; |
| 65 | } |
| 66 | |
| 67 | void vgic_mmio_write_senable(struct kvm_vcpu *vcpu, |
| 68 | gpa_t addr, unsigned int len, |
| 69 | unsigned long val) |
| 70 | { |
| 71 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 72 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 73 | unsigned long flags; |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 74 | |
| 75 | for_each_set_bit(i, &val, len * 8) { |
| 76 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 77 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 78 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 79 | irq->enabled = true; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 80 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 81 | |
| 82 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, |
| 87 | gpa_t addr, unsigned int len, |
| 88 | unsigned long val) |
| 89 | { |
| 90 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 91 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 92 | unsigned long flags; |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 93 | |
| 94 | for_each_set_bit(i, &val, len * 8) { |
| 95 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 96 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 97 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 98 | |
| 99 | irq->enabled = false; |
| 100 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 101 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 102 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 106 | unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu, |
| 107 | gpa_t addr, unsigned int len) |
| 108 | { |
| 109 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 110 | u32 value = 0; |
| 111 | int i; |
| 112 | |
| 113 | /* Loop over all IRQs affected by this read */ |
| 114 | for (i = 0; i < len * 8; i++) { |
| 115 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Andre Przywara | 62b06f8 | 2018-03-06 09:21:06 +0000 | [diff] [blame] | 116 | unsigned long flags; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 117 | |
Andre Przywara | 62b06f8 | 2018-03-06 09:21:06 +0000 | [diff] [blame] | 118 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 119 | if (irq_is_pending(irq)) |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 120 | value |= (1U << i); |
Andre Przywara | 62b06f8 | 2018-03-06 09:21:06 +0000 | [diff] [blame] | 121 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 122 | |
| 123 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return value; |
| 127 | } |
| 128 | |
Christoffer Dall | 6c1b7521 | 2017-09-14 11:08:45 -0700 | [diff] [blame] | 129 | /* |
| 130 | * This function will return the VCPU that performed the MMIO access and |
| 131 | * trapped from within the VM, and will return NULL if this is a userspace |
| 132 | * access. |
| 133 | * |
| 134 | * We can disable preemption locally around accessing the per-CPU variable, |
| 135 | * and use the resolved vcpu pointer after enabling preemption again, because |
| 136 | * even if the current thread is migrated to another CPU, reading the per-CPU |
| 137 | * value later will give us the same value as we update the per-CPU variable |
| 138 | * in the preempt notifier handlers. |
| 139 | */ |
| 140 | static struct kvm_vcpu *vgic_get_mmio_requester_vcpu(void) |
| 141 | { |
| 142 | struct kvm_vcpu *vcpu; |
| 143 | |
| 144 | preempt_disable(); |
| 145 | vcpu = kvm_arm_get_running_vcpu(); |
| 146 | preempt_enable(); |
| 147 | return vcpu; |
| 148 | } |
| 149 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 150 | /* Must be called with irq->irq_lock held */ |
| 151 | static void vgic_hw_irq_spending(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
| 152 | bool is_uaccess) |
| 153 | { |
| 154 | if (is_uaccess) |
| 155 | return; |
| 156 | |
| 157 | irq->pending_latch = true; |
| 158 | vgic_irq_set_phys_active(irq, true); |
| 159 | } |
| 160 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 161 | void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, |
| 162 | gpa_t addr, unsigned int len, |
| 163 | unsigned long val) |
| 164 | { |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 165 | bool is_uaccess = !vgic_get_mmio_requester_vcpu(); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 166 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 167 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 168 | unsigned long flags; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 169 | |
| 170 | for_each_set_bit(i, &val, len * 8) { |
| 171 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 172 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 173 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 174 | if (irq->hw) |
| 175 | vgic_hw_irq_spending(vcpu, irq, is_uaccess); |
| 176 | else |
| 177 | irq->pending_latch = true; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 178 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 179 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 183 | /* Must be called with irq->irq_lock held */ |
| 184 | static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
| 185 | bool is_uaccess) |
| 186 | { |
| 187 | if (is_uaccess) |
| 188 | return; |
| 189 | |
| 190 | irq->pending_latch = false; |
| 191 | |
| 192 | /* |
| 193 | * We don't want the guest to effectively mask the physical |
| 194 | * interrupt by doing a write to SPENDR followed by a write to |
| 195 | * CPENDR for HW interrupts, so we clear the active state on |
| 196 | * the physical side if the virtual interrupt is not active. |
| 197 | * This may lead to taking an additional interrupt on the |
| 198 | * host, but that should not be a problem as the worst that |
| 199 | * can happen is an additional vgic injection. We also clear |
| 200 | * the pending state to maintain proper semantics for edge HW |
| 201 | * interrupts. |
| 202 | */ |
| 203 | vgic_irq_set_phys_pending(irq, false); |
| 204 | if (!irq->active) |
| 205 | vgic_irq_set_phys_active(irq, false); |
| 206 | } |
| 207 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 208 | void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, |
| 209 | gpa_t addr, unsigned int len, |
| 210 | unsigned long val) |
| 211 | { |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 212 | bool is_uaccess = !vgic_get_mmio_requester_vcpu(); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 213 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 214 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 215 | unsigned long flags; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 216 | |
| 217 | for_each_set_bit(i, &val, len * 8) { |
| 218 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 219 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 220 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 221 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 222 | if (irq->hw) |
| 223 | vgic_hw_irq_cpending(vcpu, irq, is_uaccess); |
| 224 | else |
| 225 | irq->pending_latch = false; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 226 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 227 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 228 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 232 | unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu, |
| 233 | gpa_t addr, unsigned int len) |
| 234 | { |
| 235 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 236 | u32 value = 0; |
| 237 | int i; |
| 238 | |
| 239 | /* Loop over all IRQs affected by this read */ |
| 240 | for (i = 0; i < len * 8; i++) { |
| 241 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 242 | |
| 243 | if (irq->active) |
| 244 | value |= (1U << i); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 245 | |
| 246 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return value; |
| 250 | } |
| 251 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 252 | /* Must be called with irq->irq_lock held */ |
| 253 | static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
| 254 | bool active, bool is_uaccess) |
| 255 | { |
| 256 | if (is_uaccess) |
| 257 | return; |
| 258 | |
| 259 | irq->active = active; |
| 260 | vgic_irq_set_phys_active(irq, active); |
| 261 | } |
| 262 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 263 | static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 264 | bool active) |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 265 | { |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 266 | unsigned long flags; |
Christoffer Dall | 6c1b7521 | 2017-09-14 11:08:45 -0700 | [diff] [blame] | 267 | struct kvm_vcpu *requester_vcpu = vgic_get_mmio_requester_vcpu(); |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 268 | |
Christoffer Dall | 6c1b7521 | 2017-09-14 11:08:45 -0700 | [diff] [blame] | 269 | spin_lock_irqsave(&irq->irq_lock, flags); |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 270 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 271 | /* |
| 272 | * If this virtual IRQ was written into a list register, we |
| 273 | * have to make sure the CPU that runs the VCPU thread has |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 274 | * synced back the LR state to the struct vgic_irq. |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 275 | * |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 276 | * As long as the conditions below are true, we know the VCPU thread |
| 277 | * may be on its way back from the guest (we kicked the VCPU thread in |
| 278 | * vgic_change_active_prepare) and still has to sync back this IRQ, |
| 279 | * so we release and re-acquire the spin_lock to let the other thread |
| 280 | * sync back the IRQ. |
Christoffer Dall | 6c1b7521 | 2017-09-14 11:08:45 -0700 | [diff] [blame] | 281 | * |
| 282 | * When accessing VGIC state from user space, requester_vcpu is |
| 283 | * NULL, which is fine, because we guarantee that no VCPUs are running |
| 284 | * when accessing VGIC state from user space so irq->vcpu->cpu is |
| 285 | * always -1. |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 286 | */ |
| 287 | while (irq->vcpu && /* IRQ may have state in an LR somewhere */ |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 288 | irq->vcpu != requester_vcpu && /* Current thread is not the VCPU thread */ |
Marc Zyngier | 05fb05a | 2016-06-02 09:24:06 +0100 | [diff] [blame] | 289 | irq->vcpu->cpu != -1) /* VCPU thread is running */ |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 290 | cond_resched_lock(&irq->irq_lock); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 291 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame^] | 292 | if (irq->hw) { |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 293 | vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu); |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame^] | 294 | } else { |
| 295 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
| 296 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 297 | irq->active = active; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame^] | 298 | if (model == KVM_DEV_TYPE_ARM_VGIC_V2 && |
| 299 | active && vgic_irq_is_sgi(irq->intid)) |
| 300 | irq->active_source = requester_vcpu->vcpu_id; |
| 301 | } |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 302 | |
| 303 | if (irq->active) |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 304 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 305 | else |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 306 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * If we are fiddling with an IRQ's active state, we have to make sure the IRQ |
| 311 | * is not queued on some running VCPU's LRs, because then the change to the |
| 312 | * active state can be overwritten when the VCPU's state is synced coming back |
| 313 | * from the guest. |
| 314 | * |
| 315 | * For shared interrupts, we have to stop all the VCPUs because interrupts can |
| 316 | * be migrated while we don't hold the IRQ locks and we don't want to be |
| 317 | * chasing moving targets. |
| 318 | * |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 319 | * For private interrupts we don't have to do anything because userspace |
| 320 | * accesses to the VGIC state already require all VCPUs to be stopped, and |
| 321 | * only the VCPU itself can modify its private interrupts active state, which |
| 322 | * guarantees that the VCPU is not running. |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 323 | */ |
| 324 | static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid) |
| 325 | { |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 326 | if (intid > VGIC_NR_PRIVATE_IRQS) |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 327 | kvm_arm_halt_guest(vcpu->kvm); |
| 328 | } |
| 329 | |
| 330 | /* See vgic_change_active_prepare */ |
| 331 | static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid) |
| 332 | { |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 333 | if (intid > VGIC_NR_PRIVATE_IRQS) |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 334 | kvm_arm_resume_guest(vcpu->kvm); |
| 335 | } |
| 336 | |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 337 | static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 338 | gpa_t addr, unsigned int len, |
| 339 | unsigned long val) |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 340 | { |
| 341 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 342 | int i; |
| 343 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 344 | for_each_set_bit(i, &val, len * 8) { |
| 345 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 346 | vgic_mmio_change_active(vcpu, irq, false); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 347 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 348 | } |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 352 | gpa_t addr, unsigned int len, |
| 353 | unsigned long val) |
| 354 | { |
| 355 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 356 | |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 357 | mutex_lock(&vcpu->kvm->lock); |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 358 | vgic_change_active_prepare(vcpu, intid); |
| 359 | |
| 360 | __vgic_mmio_write_cactive(vcpu, addr, len, val); |
| 361 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 362 | vgic_change_active_finish(vcpu, intid); |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 363 | mutex_unlock(&vcpu->kvm->lock); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 366 | void vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu, |
| 367 | gpa_t addr, unsigned int len, |
| 368 | unsigned long val) |
| 369 | { |
| 370 | __vgic_mmio_write_cactive(vcpu, addr, len, val); |
| 371 | } |
| 372 | |
| 373 | static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 374 | gpa_t addr, unsigned int len, |
| 375 | unsigned long val) |
| 376 | { |
| 377 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 378 | int i; |
| 379 | |
| 380 | for_each_set_bit(i, &val, len * 8) { |
| 381 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 382 | vgic_mmio_change_active(vcpu, irq, true); |
| 383 | vgic_put_irq(vcpu->kvm, irq); |
| 384 | } |
| 385 | } |
| 386 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 387 | void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 388 | gpa_t addr, unsigned int len, |
| 389 | unsigned long val) |
| 390 | { |
| 391 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 392 | |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 393 | mutex_lock(&vcpu->kvm->lock); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 394 | vgic_change_active_prepare(vcpu, intid); |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 395 | |
| 396 | __vgic_mmio_write_sactive(vcpu, addr, len, val); |
| 397 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 398 | vgic_change_active_finish(vcpu, intid); |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 399 | mutex_unlock(&vcpu->kvm->lock); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 402 | void vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu, |
| 403 | gpa_t addr, unsigned int len, |
| 404 | unsigned long val) |
| 405 | { |
| 406 | __vgic_mmio_write_sactive(vcpu, addr, len, val); |
| 407 | } |
| 408 | |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 409 | unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu, |
| 410 | gpa_t addr, unsigned int len) |
| 411 | { |
| 412 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 413 | int i; |
| 414 | u64 val = 0; |
| 415 | |
| 416 | for (i = 0; i < len; i++) { |
| 417 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 418 | |
| 419 | val |= (u64)irq->priority << (i * 8); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 420 | |
| 421 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return val; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * We currently don't handle changing the priority of an interrupt that |
| 429 | * is already pending on a VCPU. If there is a need for this, we would |
| 430 | * need to make this VCPU exit and re-evaluate the priorities, potentially |
| 431 | * leading to this interrupt getting presented now to the guest (if it has |
| 432 | * been masked by the priority mask before). |
| 433 | */ |
| 434 | void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, |
| 435 | gpa_t addr, unsigned int len, |
| 436 | unsigned long val) |
| 437 | { |
| 438 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 439 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 440 | unsigned long flags; |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 441 | |
| 442 | for (i = 0; i < len; i++) { |
| 443 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 444 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 445 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 446 | /* Narrow the priority range to what we actually support */ |
| 447 | irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 448 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 449 | |
| 450 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 454 | unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu, |
| 455 | gpa_t addr, unsigned int len) |
| 456 | { |
| 457 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 458 | u32 value = 0; |
| 459 | int i; |
| 460 | |
| 461 | for (i = 0; i < len * 4; i++) { |
| 462 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 463 | |
| 464 | if (irq->config == VGIC_CONFIG_EDGE) |
| 465 | value |= (2U << (i * 2)); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 466 | |
| 467 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return value; |
| 471 | } |
| 472 | |
| 473 | void vgic_mmio_write_config(struct kvm_vcpu *vcpu, |
| 474 | gpa_t addr, unsigned int len, |
| 475 | unsigned long val) |
| 476 | { |
| 477 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 478 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 479 | unsigned long flags; |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 480 | |
| 481 | for (i = 0; i < len * 4; i++) { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 482 | struct vgic_irq *irq; |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * The configuration cannot be changed for SGIs in general, |
| 486 | * for PPIs this is IMPLEMENTATION DEFINED. The arch timer |
| 487 | * code relies on PPIs being level triggered, so we also |
| 488 | * make them read-only here. |
| 489 | */ |
| 490 | if (intid + i < VGIC_NR_PRIVATE_IRQS) |
| 491 | continue; |
| 492 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 493 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 494 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 495 | |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 496 | if (test_bit(i * 2 + 1, &val)) |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 497 | irq->config = VGIC_CONFIG_EDGE; |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 498 | else |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 499 | irq->config = VGIC_CONFIG_LEVEL; |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 500 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 501 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 502 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 506 | u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid) |
| 507 | { |
| 508 | int i; |
| 509 | u64 val = 0; |
| 510 | int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 511 | |
| 512 | for (i = 0; i < 32; i++) { |
| 513 | struct vgic_irq *irq; |
| 514 | |
| 515 | if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs) |
| 516 | continue; |
| 517 | |
| 518 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 519 | if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level) |
| 520 | val |= (1U << i); |
| 521 | |
| 522 | vgic_put_irq(vcpu->kvm, irq); |
| 523 | } |
| 524 | |
| 525 | return val; |
| 526 | } |
| 527 | |
| 528 | void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid, |
| 529 | const u64 val) |
| 530 | { |
| 531 | int i; |
| 532 | int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 533 | unsigned long flags; |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 534 | |
| 535 | for (i = 0; i < 32; i++) { |
| 536 | struct vgic_irq *irq; |
| 537 | bool new_level; |
| 538 | |
| 539 | if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs) |
| 540 | continue; |
| 541 | |
| 542 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 543 | |
| 544 | /* |
| 545 | * Line level is set irrespective of irq type |
| 546 | * (level or edge) to avoid dependency that VM should |
| 547 | * restore irq config before line level. |
| 548 | */ |
| 549 | new_level = !!(val & (1U << i)); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 550 | spin_lock_irqsave(&irq->irq_lock, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 551 | irq->line_level = new_level; |
| 552 | if (new_level) |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 553 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 554 | else |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 555 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 556 | |
| 557 | vgic_put_irq(vcpu->kvm, irq); |
| 558 | } |
| 559 | } |
| 560 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 561 | static int match_region(const void *key, const void *elt) |
| 562 | { |
| 563 | const unsigned int offset = (unsigned long)key; |
| 564 | const struct vgic_register_region *region = elt; |
| 565 | |
| 566 | if (offset < region->reg_offset) |
| 567 | return -1; |
| 568 | |
| 569 | if (offset >= region->reg_offset + region->len) |
| 570 | return 1; |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
Eric Auger | 4b7171a | 2016-12-20 09:20:00 +0100 | [diff] [blame] | 575 | const struct vgic_register_region * |
| 576 | vgic_find_mmio_region(const struct vgic_register_region *regions, |
| 577 | int nr_regions, unsigned int offset) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 578 | { |
Eric Auger | 4b7171a | 2016-12-20 09:20:00 +0100 | [diff] [blame] | 579 | return bsearch((void *)(uintptr_t)offset, regions, nr_regions, |
| 580 | sizeof(regions[0]), match_region); |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Vijaya Kumar K | 5fb247d | 2017-01-26 19:50:50 +0530 | [diff] [blame] | 583 | void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
| 584 | { |
| 585 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 586 | vgic_v2_set_vmcr(vcpu, vmcr); |
| 587 | else |
| 588 | vgic_v3_set_vmcr(vcpu, vmcr); |
| 589 | } |
| 590 | |
| 591 | void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
| 592 | { |
| 593 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 594 | vgic_v2_get_vmcr(vcpu, vmcr); |
| 595 | else |
| 596 | vgic_v3_get_vmcr(vcpu, vmcr); |
| 597 | } |
| 598 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 599 | /* |
| 600 | * kvm_mmio_read_buf() returns a value in a format where it can be converted |
| 601 | * to a byte array and be directly observed as the guest wanted it to appear |
| 602 | * in memory if it had done the store itself, which is LE for the GIC, as the |
| 603 | * guest knows the GIC is always LE. |
| 604 | * |
| 605 | * We convert this value to the CPUs native format to deal with it as a data |
| 606 | * value. |
| 607 | */ |
| 608 | unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len) |
| 609 | { |
| 610 | unsigned long data = kvm_mmio_read_buf(val, len); |
| 611 | |
| 612 | switch (len) { |
| 613 | case 1: |
| 614 | return data; |
| 615 | case 2: |
| 616 | return le16_to_cpu(data); |
| 617 | case 4: |
| 618 | return le32_to_cpu(data); |
| 619 | default: |
| 620 | return le64_to_cpu(data); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * kvm_mmio_write_buf() expects a value in a format such that if converted to |
| 626 | * a byte array it is observed as the guest would see it if it could perform |
| 627 | * the load directly. Since the GIC is LE, and the guest knows this, the |
| 628 | * guest expects a value in little endian format. |
| 629 | * |
| 630 | * We convert the data value from the CPUs native format to LE so that the |
| 631 | * value is returned in the proper format. |
| 632 | */ |
| 633 | void vgic_data_host_to_mmio_bus(void *buf, unsigned int len, |
| 634 | unsigned long data) |
| 635 | { |
| 636 | switch (len) { |
| 637 | case 1: |
| 638 | break; |
| 639 | case 2: |
| 640 | data = cpu_to_le16(data); |
| 641 | break; |
| 642 | case 4: |
| 643 | data = cpu_to_le32(data); |
| 644 | break; |
| 645 | default: |
| 646 | data = cpu_to_le64(data); |
| 647 | } |
| 648 | |
| 649 | kvm_mmio_write_buf(buf, len, data); |
| 650 | } |
| 651 | |
| 652 | static |
| 653 | struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev) |
| 654 | { |
| 655 | return container_of(dev, struct vgic_io_device, dev); |
| 656 | } |
| 657 | |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 658 | static bool check_region(const struct kvm *kvm, |
| 659 | const struct vgic_register_region *region, |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 660 | gpa_t addr, int len) |
| 661 | { |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 662 | int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 663 | |
| 664 | switch (len) { |
| 665 | case sizeof(u8): |
| 666 | flags = VGIC_ACCESS_8bit; |
| 667 | break; |
| 668 | case sizeof(u32): |
| 669 | flags = VGIC_ACCESS_32bit; |
| 670 | break; |
| 671 | case sizeof(u64): |
| 672 | flags = VGIC_ACCESS_64bit; |
| 673 | break; |
| 674 | default: |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) { |
| 679 | if (!region->bits_per_irq) |
| 680 | return true; |
| 681 | |
| 682 | /* Do we access a non-allocated IRQ? */ |
| 683 | return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs; |
| 684 | } |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 685 | |
| 686 | return false; |
| 687 | } |
| 688 | |
Vijaya Kumar K | 94574c9 | 2017-01-26 19:50:47 +0530 | [diff] [blame] | 689 | const struct vgic_register_region * |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 690 | vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, |
| 691 | gpa_t addr, int len) |
| 692 | { |
| 693 | const struct vgic_register_region *region; |
| 694 | |
| 695 | region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions, |
| 696 | addr - iodev->base_addr); |
| 697 | if (!region || !check_region(vcpu->kvm, region, addr, len)) |
| 698 | return NULL; |
| 699 | |
| 700 | return region; |
| 701 | } |
| 702 | |
| 703 | static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 704 | gpa_t addr, u32 *val) |
| 705 | { |
| 706 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 707 | const struct vgic_register_region *region; |
| 708 | struct kvm_vcpu *r_vcpu; |
| 709 | |
| 710 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 711 | if (!region) { |
| 712 | *val = 0; |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 717 | if (region->uaccess_read) |
| 718 | *val = region->uaccess_read(r_vcpu, addr, sizeof(u32)); |
| 719 | else |
| 720 | *val = region->read(r_vcpu, addr, sizeof(u32)); |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 726 | gpa_t addr, const u32 *val) |
| 727 | { |
| 728 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 729 | const struct vgic_register_region *region; |
| 730 | struct kvm_vcpu *r_vcpu; |
| 731 | |
| 732 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 733 | if (!region) |
| 734 | return 0; |
| 735 | |
| 736 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 737 | if (region->uaccess_write) |
| 738 | region->uaccess_write(r_vcpu, addr, sizeof(u32), *val); |
| 739 | else |
| 740 | region->write(r_vcpu, addr, sizeof(u32), *val); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Userland access to VGIC registers. |
| 747 | */ |
| 748 | int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev, |
| 749 | bool is_write, int offset, u32 *val) |
| 750 | { |
| 751 | if (is_write) |
| 752 | return vgic_uaccess_write(vcpu, &dev->dev, offset, val); |
| 753 | else |
| 754 | return vgic_uaccess_read(vcpu, &dev->dev, offset, val); |
| 755 | } |
| 756 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 757 | static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 758 | gpa_t addr, int len, void *val) |
| 759 | { |
| 760 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 761 | const struct vgic_register_region *region; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 762 | unsigned long data = 0; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 763 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 764 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 765 | if (!region) { |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 766 | memset(val, 0, len); |
| 767 | return 0; |
| 768 | } |
| 769 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 770 | switch (iodev->iodev_type) { |
| 771 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 772 | data = region->read(vcpu, addr, len); |
| 773 | break; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 774 | case IODEV_DIST: |
| 775 | data = region->read(vcpu, addr, len); |
| 776 | break; |
| 777 | case IODEV_REDIST: |
| 778 | data = region->read(iodev->redist_vcpu, addr, len); |
| 779 | break; |
| 780 | case IODEV_ITS: |
| 781 | data = region->its_read(vcpu->kvm, iodev->its, addr, len); |
| 782 | break; |
| 783 | } |
| 784 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 785 | vgic_data_host_to_mmio_bus(val, len, data); |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 790 | gpa_t addr, int len, const void *val) |
| 791 | { |
| 792 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 793 | const struct vgic_register_region *region; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 794 | unsigned long data = vgic_data_mmio_bus_to_host(val, len); |
| 795 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 796 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 797 | if (!region) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 798 | return 0; |
| 799 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 800 | switch (iodev->iodev_type) { |
| 801 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 802 | region->write(vcpu, addr, len, data); |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 803 | break; |
| 804 | case IODEV_DIST: |
| 805 | region->write(vcpu, addr, len, data); |
| 806 | break; |
| 807 | case IODEV_REDIST: |
| 808 | region->write(iodev->redist_vcpu, addr, len, data); |
| 809 | break; |
| 810 | case IODEV_ITS: |
| 811 | region->its_write(vcpu->kvm, iodev->its, addr, len, data); |
| 812 | break; |
| 813 | } |
| 814 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | struct kvm_io_device_ops kvm_io_gic_ops = { |
| 819 | .read = dispatch_mmio_read, |
| 820 | .write = dispatch_mmio_write, |
| 821 | }; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 822 | |
| 823 | int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address, |
| 824 | enum vgic_type type) |
| 825 | { |
| 826 | struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev; |
| 827 | int ret = 0; |
| 828 | unsigned int len; |
| 829 | |
| 830 | switch (type) { |
| 831 | case VGIC_V2: |
| 832 | len = vgic_v2_init_dist_iodev(io_device); |
| 833 | break; |
Andre Przywara | ed9b8ce | 2015-12-01 14:34:34 +0000 | [diff] [blame] | 834 | case VGIC_V3: |
| 835 | len = vgic_v3_init_dist_iodev(io_device); |
| 836 | break; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 837 | default: |
| 838 | BUG_ON(1); |
| 839 | } |
| 840 | |
| 841 | io_device->base_addr = dist_base_address; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 842 | io_device->iodev_type = IODEV_DIST; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 843 | io_device->redist_vcpu = NULL; |
| 844 | |
| 845 | mutex_lock(&kvm->slots_lock); |
| 846 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address, |
| 847 | len, &io_device->dev); |
| 848 | mutex_unlock(&kvm->slots_lock); |
| 849 | |
| 850 | return ret; |
| 851 | } |