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 | { |
| 183 | spin_lock(&irq->irq_lock); |
| 184 | /* |
| 185 | * If this virtual IRQ was written into a list register, we |
| 186 | * have to make sure the CPU that runs the VCPU thread has |
| 187 | * synced back LR state to the struct vgic_irq. We can only |
| 188 | * know this for sure, when either this irq is not assigned to |
| 189 | * anyone's AP list anymore, or the VCPU thread is not |
| 190 | * running on any CPUs. |
| 191 | * |
| 192 | * In the opposite case, we know the VCPU thread may be on its |
| 193 | * way back from the guest and still has to sync back this |
| 194 | * IRQ, so we release and re-acquire the spin_lock to let the |
| 195 | * other thread sync back the IRQ. |
| 196 | */ |
| 197 | while (irq->vcpu && /* IRQ may have state in an LR somewhere */ |
Marc Zyngier | 05fb05a | 2016-06-02 09:24:06 +0100 | [diff] [blame] | 198 | irq->vcpu->cpu != -1) /* VCPU thread is running */ |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 199 | cond_resched_lock(&irq->irq_lock); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 200 | |
| 201 | irq->active = new_active_state; |
| 202 | if (new_active_state) |
| 203 | vgic_queue_irq_unlock(vcpu->kvm, irq); |
| 204 | else |
| 205 | spin_unlock(&irq->irq_lock); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * If we are fiddling with an IRQ's active state, we have to make sure the IRQ |
| 210 | * is not queued on some running VCPU's LRs, because then the change to the |
| 211 | * active state can be overwritten when the VCPU's state is synced coming back |
| 212 | * from the guest. |
| 213 | * |
| 214 | * For shared interrupts, we have to stop all the VCPUs because interrupts can |
| 215 | * be migrated while we don't hold the IRQ locks and we don't want to be |
| 216 | * chasing moving targets. |
| 217 | * |
| 218 | * For private interrupts, we only have to make sure the single and only VCPU |
| 219 | * that can potentially queue the IRQ is stopped. |
| 220 | */ |
| 221 | static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid) |
| 222 | { |
| 223 | if (intid < VGIC_NR_PRIVATE_IRQS) |
| 224 | kvm_arm_halt_vcpu(vcpu); |
| 225 | else |
| 226 | kvm_arm_halt_guest(vcpu->kvm); |
| 227 | } |
| 228 | |
| 229 | /* See vgic_change_active_prepare */ |
| 230 | static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid) |
| 231 | { |
| 232 | if (intid < VGIC_NR_PRIVATE_IRQS) |
| 233 | kvm_arm_resume_vcpu(vcpu); |
| 234 | else |
| 235 | kvm_arm_resume_guest(vcpu->kvm); |
| 236 | } |
| 237 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 238 | void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 239 | gpa_t addr, unsigned int len, |
| 240 | unsigned long val) |
| 241 | { |
| 242 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 243 | int i; |
| 244 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 245 | vgic_change_active_prepare(vcpu, intid); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 246 | for_each_set_bit(i, &val, len * 8) { |
| 247 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 248 | vgic_mmio_change_active(vcpu, irq, false); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 249 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 250 | } |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 251 | vgic_change_active_finish(vcpu, intid); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 255 | gpa_t addr, unsigned int len, |
| 256 | unsigned long val) |
| 257 | { |
| 258 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 259 | int i; |
| 260 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 261 | vgic_change_active_prepare(vcpu, intid); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 262 | for_each_set_bit(i, &val, len * 8) { |
| 263 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 264 | vgic_mmio_change_active(vcpu, irq, true); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 265 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 266 | } |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 267 | vgic_change_active_finish(vcpu, intid); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 270 | unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu, |
| 271 | gpa_t addr, unsigned int len) |
| 272 | { |
| 273 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 274 | int i; |
| 275 | u64 val = 0; |
| 276 | |
| 277 | for (i = 0; i < len; i++) { |
| 278 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 279 | |
| 280 | val |= (u64)irq->priority << (i * 8); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 281 | |
| 282 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return val; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * We currently don't handle changing the priority of an interrupt that |
| 290 | * is already pending on a VCPU. If there is a need for this, we would |
| 291 | * need to make this VCPU exit and re-evaluate the priorities, potentially |
| 292 | * leading to this interrupt getting presented now to the guest (if it has |
| 293 | * been masked by the priority mask before). |
| 294 | */ |
| 295 | void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, |
| 296 | gpa_t addr, unsigned int len, |
| 297 | unsigned long val) |
| 298 | { |
| 299 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < len; i++) { |
| 303 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 304 | |
| 305 | spin_lock(&irq->irq_lock); |
| 306 | /* Narrow the priority range to what we actually support */ |
| 307 | irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS); |
| 308 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 309 | |
| 310 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 314 | unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu, |
| 315 | gpa_t addr, unsigned int len) |
| 316 | { |
| 317 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 318 | u32 value = 0; |
| 319 | int i; |
| 320 | |
| 321 | for (i = 0; i < len * 4; i++) { |
| 322 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 323 | |
| 324 | if (irq->config == VGIC_CONFIG_EDGE) |
| 325 | value |= (2U << (i * 2)); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 326 | |
| 327 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return value; |
| 331 | } |
| 332 | |
| 333 | void vgic_mmio_write_config(struct kvm_vcpu *vcpu, |
| 334 | gpa_t addr, unsigned int len, |
| 335 | unsigned long val) |
| 336 | { |
| 337 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 338 | int i; |
| 339 | |
| 340 | for (i = 0; i < len * 4; i++) { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 341 | struct vgic_irq *irq; |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * The configuration cannot be changed for SGIs in general, |
| 345 | * for PPIs this is IMPLEMENTATION DEFINED. The arch timer |
| 346 | * code relies on PPIs being level triggered, so we also |
| 347 | * make them read-only here. |
| 348 | */ |
| 349 | if (intid + i < VGIC_NR_PRIVATE_IRQS) |
| 350 | continue; |
| 351 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 352 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 353 | spin_lock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 354 | |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 355 | if (test_bit(i * 2 + 1, &val)) |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 356 | irq->config = VGIC_CONFIG_EDGE; |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 357 | else |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 358 | irq->config = VGIC_CONFIG_LEVEL; |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 359 | |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 360 | spin_unlock(&irq->irq_lock); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 361 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 365 | static int match_region(const void *key, const void *elt) |
| 366 | { |
| 367 | const unsigned int offset = (unsigned long)key; |
| 368 | const struct vgic_register_region *region = elt; |
| 369 | |
| 370 | if (offset < region->reg_offset) |
| 371 | return -1; |
| 372 | |
| 373 | if (offset >= region->reg_offset + region->len) |
| 374 | return 1; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* Find the proper register handler entry given a certain address offset. */ |
| 380 | static const struct vgic_register_region * |
| 381 | vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions, |
| 382 | unsigned int offset) |
| 383 | { |
| 384 | return bsearch((void *)(uintptr_t)offset, region, nr_regions, |
| 385 | sizeof(region[0]), match_region); |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * kvm_mmio_read_buf() returns a value in a format where it can be converted |
| 390 | * to a byte array and be directly observed as the guest wanted it to appear |
| 391 | * in memory if it had done the store itself, which is LE for the GIC, as the |
| 392 | * guest knows the GIC is always LE. |
| 393 | * |
| 394 | * We convert this value to the CPUs native format to deal with it as a data |
| 395 | * value. |
| 396 | */ |
| 397 | unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len) |
| 398 | { |
| 399 | unsigned long data = kvm_mmio_read_buf(val, len); |
| 400 | |
| 401 | switch (len) { |
| 402 | case 1: |
| 403 | return data; |
| 404 | case 2: |
| 405 | return le16_to_cpu(data); |
| 406 | case 4: |
| 407 | return le32_to_cpu(data); |
| 408 | default: |
| 409 | return le64_to_cpu(data); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * kvm_mmio_write_buf() expects a value in a format such that if converted to |
| 415 | * a byte array it is observed as the guest would see it if it could perform |
| 416 | * the load directly. Since the GIC is LE, and the guest knows this, the |
| 417 | * guest expects a value in little endian format. |
| 418 | * |
| 419 | * We convert the data value from the CPUs native format to LE so that the |
| 420 | * value is returned in the proper format. |
| 421 | */ |
| 422 | void vgic_data_host_to_mmio_bus(void *buf, unsigned int len, |
| 423 | unsigned long data) |
| 424 | { |
| 425 | switch (len) { |
| 426 | case 1: |
| 427 | break; |
| 428 | case 2: |
| 429 | data = cpu_to_le16(data); |
| 430 | break; |
| 431 | case 4: |
| 432 | data = cpu_to_le32(data); |
| 433 | break; |
| 434 | default: |
| 435 | data = cpu_to_le64(data); |
| 436 | } |
| 437 | |
| 438 | kvm_mmio_write_buf(buf, len, data); |
| 439 | } |
| 440 | |
| 441 | static |
| 442 | struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev) |
| 443 | { |
| 444 | return container_of(dev, struct vgic_io_device, dev); |
| 445 | } |
| 446 | |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 447 | static bool check_region(const struct kvm *kvm, |
| 448 | const struct vgic_register_region *region, |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 449 | gpa_t addr, int len) |
| 450 | { |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 451 | int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 452 | |
| 453 | switch (len) { |
| 454 | case sizeof(u8): |
| 455 | flags = VGIC_ACCESS_8bit; |
| 456 | break; |
| 457 | case sizeof(u32): |
| 458 | flags = VGIC_ACCESS_32bit; |
| 459 | break; |
| 460 | case sizeof(u64): |
| 461 | flags = VGIC_ACCESS_64bit; |
| 462 | break; |
| 463 | default: |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) { |
| 468 | if (!region->bits_per_irq) |
| 469 | return true; |
| 470 | |
| 471 | /* Do we access a non-allocated IRQ? */ |
| 472 | return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs; |
| 473 | } |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 474 | |
| 475 | return false; |
| 476 | } |
| 477 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame^] | 478 | static const struct vgic_register_region * |
| 479 | vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, |
| 480 | gpa_t addr, int len) |
| 481 | { |
| 482 | const struct vgic_register_region *region; |
| 483 | |
| 484 | region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions, |
| 485 | addr - iodev->base_addr); |
| 486 | if (!region || !check_region(vcpu->kvm, region, addr, len)) |
| 487 | return NULL; |
| 488 | |
| 489 | return region; |
| 490 | } |
| 491 | |
| 492 | static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 493 | gpa_t addr, u32 *val) |
| 494 | { |
| 495 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 496 | const struct vgic_register_region *region; |
| 497 | struct kvm_vcpu *r_vcpu; |
| 498 | |
| 499 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 500 | if (!region) { |
| 501 | *val = 0; |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 506 | if (region->uaccess_read) |
| 507 | *val = region->uaccess_read(r_vcpu, addr, sizeof(u32)); |
| 508 | else |
| 509 | *val = region->read(r_vcpu, addr, sizeof(u32)); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 515 | gpa_t addr, const u32 *val) |
| 516 | { |
| 517 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 518 | const struct vgic_register_region *region; |
| 519 | struct kvm_vcpu *r_vcpu; |
| 520 | |
| 521 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 522 | if (!region) |
| 523 | return 0; |
| 524 | |
| 525 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 526 | if (region->uaccess_write) |
| 527 | region->uaccess_write(r_vcpu, addr, sizeof(u32), *val); |
| 528 | else |
| 529 | region->write(r_vcpu, addr, sizeof(u32), *val); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Userland access to VGIC registers. |
| 536 | */ |
| 537 | int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev, |
| 538 | bool is_write, int offset, u32 *val) |
| 539 | { |
| 540 | if (is_write) |
| 541 | return vgic_uaccess_write(vcpu, &dev->dev, offset, val); |
| 542 | else |
| 543 | return vgic_uaccess_read(vcpu, &dev->dev, offset, val); |
| 544 | } |
| 545 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 546 | static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 547 | gpa_t addr, int len, void *val) |
| 548 | { |
| 549 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 550 | const struct vgic_register_region *region; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 551 | unsigned long data = 0; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 552 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame^] | 553 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 554 | if (!region) { |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 555 | memset(val, 0, len); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 559 | switch (iodev->iodev_type) { |
| 560 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 561 | data = region->read(vcpu, addr, len); |
| 562 | break; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 563 | case IODEV_DIST: |
| 564 | data = region->read(vcpu, addr, len); |
| 565 | break; |
| 566 | case IODEV_REDIST: |
| 567 | data = region->read(iodev->redist_vcpu, addr, len); |
| 568 | break; |
| 569 | case IODEV_ITS: |
| 570 | data = region->its_read(vcpu->kvm, iodev->its, addr, len); |
| 571 | break; |
| 572 | } |
| 573 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 574 | vgic_data_host_to_mmio_bus(val, len, data); |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 579 | gpa_t addr, int len, const void *val) |
| 580 | { |
| 581 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 582 | const struct vgic_register_region *region; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 583 | unsigned long data = vgic_data_mmio_bus_to_host(val, len); |
| 584 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame^] | 585 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 586 | if (!region) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 587 | return 0; |
| 588 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 589 | switch (iodev->iodev_type) { |
| 590 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 591 | region->write(vcpu, addr, len, data); |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 592 | break; |
| 593 | case IODEV_DIST: |
| 594 | region->write(vcpu, addr, len, data); |
| 595 | break; |
| 596 | case IODEV_REDIST: |
| 597 | region->write(iodev->redist_vcpu, addr, len, data); |
| 598 | break; |
| 599 | case IODEV_ITS: |
| 600 | region->its_write(vcpu->kvm, iodev->its, addr, len, data); |
| 601 | break; |
| 602 | } |
| 603 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | struct kvm_io_device_ops kvm_io_gic_ops = { |
| 608 | .read = dispatch_mmio_read, |
| 609 | .write = dispatch_mmio_write, |
| 610 | }; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 611 | |
| 612 | int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address, |
| 613 | enum vgic_type type) |
| 614 | { |
| 615 | struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev; |
| 616 | int ret = 0; |
| 617 | unsigned int len; |
| 618 | |
| 619 | switch (type) { |
| 620 | case VGIC_V2: |
| 621 | len = vgic_v2_init_dist_iodev(io_device); |
| 622 | break; |
Andre Przywara | ed9b8ce | 2015-12-01 14:34:34 +0000 | [diff] [blame] | 623 | case VGIC_V3: |
| 624 | len = vgic_v3_init_dist_iodev(io_device); |
| 625 | break; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 626 | default: |
| 627 | BUG_ON(1); |
| 628 | } |
| 629 | |
| 630 | io_device->base_addr = dist_base_address; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 631 | io_device->iodev_type = IODEV_DIST; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 632 | io_device->redist_vcpu = NULL; |
| 633 | |
| 634 | mutex_lock(&kvm->slots_lock); |
| 635 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address, |
| 636 | len, &io_device->dev); |
| 637 | mutex_unlock(&kvm->slots_lock); |
| 638 | |
| 639 | return ret; |
| 640 | } |