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