Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 2 | /* |
| 3 | * VGIC MMIO handling functions |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/bitops.h> |
| 7 | #include <linux/bsearch.h> |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/irq.h> |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 10 | #include <linux/kvm.h> |
| 11 | #include <linux/kvm_host.h> |
| 12 | #include <kvm/iodev.h> |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 13 | #include <kvm/arm_arch_timer.h> |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 14 | #include <kvm/arm_vgic.h> |
| 15 | |
| 16 | #include "vgic.h" |
| 17 | #include "vgic-mmio.h" |
| 18 | |
| 19 | unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu, |
| 20 | gpa_t addr, unsigned int len) |
| 21 | { |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu, |
| 26 | gpa_t addr, unsigned int len) |
| 27 | { |
| 28 | return -1UL; |
| 29 | } |
| 30 | |
| 31 | void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr, |
| 32 | unsigned int len, unsigned long val) |
| 33 | { |
| 34 | /* Ignore */ |
| 35 | } |
| 36 | |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 37 | int vgic_mmio_uaccess_write_wi(struct kvm_vcpu *vcpu, gpa_t addr, |
| 38 | unsigned int len, unsigned long val) |
| 39 | { |
| 40 | /* Ignore */ |
| 41 | return 0; |
| 42 | } |
| 43 | |
Christoffer Dall | d53c2c29 | 2018-07-16 15:06:25 +0200 | [diff] [blame] | 44 | unsigned long vgic_mmio_read_group(struct kvm_vcpu *vcpu, |
| 45 | gpa_t addr, unsigned int len) |
| 46 | { |
| 47 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 48 | u32 value = 0; |
| 49 | int i; |
| 50 | |
| 51 | /* Loop over all IRQs affected by this read */ |
| 52 | for (i = 0; i < len * 8; i++) { |
| 53 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 54 | |
| 55 | if (irq->group) |
| 56 | value |= BIT(i); |
| 57 | |
| 58 | vgic_put_irq(vcpu->kvm, irq); |
| 59 | } |
| 60 | |
| 61 | return value; |
| 62 | } |
| 63 | |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 64 | static void vgic_update_vsgi(struct vgic_irq *irq) |
| 65 | { |
| 66 | WARN_ON(its_prop_update_vsgi(irq->host_irq, irq->priority, irq->group)); |
| 67 | } |
| 68 | |
Christoffer Dall | d53c2c29 | 2018-07-16 15:06:25 +0200 | [diff] [blame] | 69 | void vgic_mmio_write_group(struct kvm_vcpu *vcpu, gpa_t addr, |
| 70 | unsigned int len, unsigned long val) |
| 71 | { |
| 72 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 73 | int i; |
| 74 | unsigned long flags; |
| 75 | |
| 76 | for (i = 0; i < len * 8; i++) { |
| 77 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 78 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 79 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | d53c2c29 | 2018-07-16 15:06:25 +0200 | [diff] [blame] | 80 | irq->group = !!(val & BIT(i)); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 81 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 82 | vgic_update_vsgi(irq); |
| 83 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 84 | } else { |
| 85 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
| 86 | } |
Christoffer Dall | d53c2c29 | 2018-07-16 15:06:25 +0200 | [diff] [blame] | 87 | |
| 88 | vgic_put_irq(vcpu->kvm, irq); |
| 89 | } |
| 90 | } |
| 91 | |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 92 | /* |
| 93 | * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value |
| 94 | * of the enabled bit, so there is only one function for both here. |
| 95 | */ |
| 96 | unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu, |
| 97 | gpa_t addr, unsigned int len) |
| 98 | { |
| 99 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 100 | u32 value = 0; |
| 101 | int i; |
| 102 | |
| 103 | /* Loop over all IRQs affected by this read */ |
| 104 | for (i = 0; i < len * 8; i++) { |
| 105 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 106 | |
| 107 | if (irq->enabled) |
| 108 | value |= (1U << i); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 109 | |
| 110 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return value; |
| 114 | } |
| 115 | |
| 116 | void vgic_mmio_write_senable(struct kvm_vcpu *vcpu, |
| 117 | gpa_t addr, unsigned int len, |
| 118 | unsigned long val) |
| 119 | { |
| 120 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 121 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 122 | unsigned long flags; |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 123 | |
| 124 | for_each_set_bit(i, &val, len * 8) { |
| 125 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 126 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 127 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 128 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 129 | if (!irq->enabled) { |
| 130 | struct irq_data *data; |
| 131 | |
| 132 | irq->enabled = true; |
| 133 | data = &irq_to_desc(irq->host_irq)->irq_data; |
| 134 | while (irqd_irq_disabled(data)) |
| 135 | enable_irq(irq->host_irq); |
| 136 | } |
| 137 | |
| 138 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 139 | vgic_put_irq(vcpu->kvm, irq); |
| 140 | |
| 141 | continue; |
| 142 | } else if (vgic_irq_is_mapped_level(irq)) { |
Alexandru Elisei | 16e604a | 2019-08-07 10:53:20 +0100 | [diff] [blame] | 143 | bool was_high = irq->line_level; |
| 144 | |
| 145 | /* |
| 146 | * We need to update the state of the interrupt because |
| 147 | * the guest might have changed the state of the device |
| 148 | * while the interrupt was disabled at the VGIC level. |
| 149 | */ |
| 150 | irq->line_level = vgic_get_phys_line_level(irq); |
| 151 | /* |
| 152 | * Deactivate the physical interrupt so the GIC will let |
| 153 | * us know when it is asserted again. |
| 154 | */ |
| 155 | if (!irq->active && was_high && !irq->line_level) |
| 156 | vgic_irq_set_phys_active(irq, false); |
| 157 | } |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 158 | irq->enabled = true; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 159 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 160 | |
| 161 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, |
| 166 | gpa_t addr, unsigned int len, |
| 167 | unsigned long val) |
| 168 | { |
| 169 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 170 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 171 | unsigned long flags; |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 172 | |
| 173 | for_each_set_bit(i, &val, len * 8) { |
| 174 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 175 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 176 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 177 | if (irq->hw && vgic_irq_is_sgi(irq->intid) && irq->enabled) |
| 178 | disable_irq_nosync(irq->host_irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 179 | |
| 180 | irq->enabled = false; |
| 181 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 182 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 183 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Marc Zyngier | 41ee52e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 187 | int vgic_uaccess_write_senable(struct kvm_vcpu *vcpu, |
| 188 | gpa_t addr, unsigned int len, |
| 189 | unsigned long val) |
| 190 | { |
| 191 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 192 | int i; |
| 193 | unsigned long flags; |
| 194 | |
| 195 | for_each_set_bit(i, &val, len * 8) { |
| 196 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 197 | |
| 198 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
| 199 | irq->enabled = true; |
| 200 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
| 201 | |
| 202 | vgic_put_irq(vcpu->kvm, irq); |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int vgic_uaccess_write_cenable(struct kvm_vcpu *vcpu, |
| 209 | gpa_t addr, unsigned int len, |
| 210 | unsigned long val) |
| 211 | { |
| 212 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 213 | int i; |
| 214 | unsigned long flags; |
| 215 | |
| 216 | for_each_set_bit(i, &val, len * 8) { |
| 217 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 218 | |
| 219 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
| 220 | irq->enabled = false; |
| 221 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 222 | |
| 223 | vgic_put_irq(vcpu->kvm, irq); |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 229 | unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu, |
| 230 | gpa_t addr, unsigned int len) |
| 231 | { |
| 232 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 233 | u32 value = 0; |
| 234 | int i; |
| 235 | |
| 236 | /* Loop over all IRQs affected by this read */ |
| 237 | for (i = 0; i < len * 8; i++) { |
| 238 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Andre Przywara | 62b06f8 | 2018-03-06 09:21:06 +0000 | [diff] [blame] | 239 | unsigned long flags; |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 240 | bool val; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 241 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 242 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 243 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 244 | int err; |
| 245 | |
| 246 | val = false; |
| 247 | err = irq_get_irqchip_state(irq->host_irq, |
| 248 | IRQCHIP_STATE_PENDING, |
| 249 | &val); |
| 250 | WARN_RATELIMIT(err, "IRQ %d", irq->host_irq); |
| 251 | } else { |
| 252 | val = irq_is_pending(irq); |
| 253 | } |
| 254 | |
| 255 | value |= ((u32)val << i); |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 256 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 257 | |
| 258 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return value; |
| 262 | } |
| 263 | |
Marc Zyngier | 82e40f5 | 2019-08-28 11:10:16 +0100 | [diff] [blame] | 264 | static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq) |
| 265 | { |
| 266 | return (vgic_irq_is_sgi(irq->intid) && |
| 267 | vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2); |
| 268 | } |
| 269 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 270 | void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, |
| 271 | gpa_t addr, unsigned int len, |
| 272 | unsigned long val) |
| 273 | { |
| 274 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 275 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 276 | unsigned long flags; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 277 | |
| 278 | for_each_set_bit(i, &val, len * 8) { |
| 279 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 280 | |
Marc Zyngier | 82e40f5 | 2019-08-28 11:10:16 +0100 | [diff] [blame] | 281 | /* GICD_ISPENDR0 SGI bits are WI */ |
| 282 | if (is_vgic_v2_sgi(vcpu, irq)) { |
| 283 | vgic_put_irq(vcpu->kvm, irq); |
| 284 | continue; |
| 285 | } |
| 286 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 287 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 288 | |
| 289 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 290 | /* HW SGI? Ask the GIC to inject it */ |
| 291 | int err; |
| 292 | err = irq_set_irqchip_state(irq->host_irq, |
| 293 | IRQCHIP_STATE_PENDING, |
| 294 | true); |
| 295 | WARN_RATELIMIT(err, "IRQ %d", irq->host_irq); |
| 296 | |
| 297 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 298 | vgic_put_irq(vcpu->kvm, irq); |
| 299 | |
| 300 | continue; |
| 301 | } |
| 302 | |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 303 | irq->pending_latch = true; |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 304 | if (irq->hw) |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 305 | vgic_irq_set_phys_active(irq, true); |
| 306 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 307 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 308 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 312 | int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu, |
| 313 | gpa_t addr, unsigned int len, |
| 314 | unsigned long val) |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 315 | { |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 316 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 317 | int i; |
| 318 | unsigned long flags; |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 319 | |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 320 | for_each_set_bit(i, &val, len * 8) { |
| 321 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 322 | |
| 323 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
| 324 | irq->pending_latch = true; |
| 325 | |
| 326 | /* |
| 327 | * GICv2 SGIs are terribly broken. We can't restore |
| 328 | * the source of the interrupt, so just pick the vcpu |
| 329 | * itself as the source... |
| 330 | */ |
| 331 | if (is_vgic_v2_sgi(vcpu, irq)) |
| 332 | irq->source |= BIT(vcpu->vcpu_id); |
| 333 | |
| 334 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
| 335 | |
| 336 | vgic_put_irq(vcpu->kvm, irq); |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* Must be called with irq->irq_lock held */ |
| 343 | static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq) |
| 344 | { |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 345 | irq->pending_latch = false; |
| 346 | |
| 347 | /* |
| 348 | * We don't want the guest to effectively mask the physical |
| 349 | * interrupt by doing a write to SPENDR followed by a write to |
| 350 | * CPENDR for HW interrupts, so we clear the active state on |
| 351 | * the physical side if the virtual interrupt is not active. |
| 352 | * This may lead to taking an additional interrupt on the |
| 353 | * host, but that should not be a problem as the worst that |
| 354 | * can happen is an additional vgic injection. We also clear |
| 355 | * the pending state to maintain proper semantics for edge HW |
| 356 | * interrupts. |
| 357 | */ |
| 358 | vgic_irq_set_phys_pending(irq, false); |
| 359 | if (!irq->active) |
| 360 | vgic_irq_set_phys_active(irq, false); |
| 361 | } |
| 362 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 363 | void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, |
| 364 | gpa_t addr, unsigned int len, |
| 365 | unsigned long val) |
| 366 | { |
| 367 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 368 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 369 | unsigned long flags; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 370 | |
| 371 | for_each_set_bit(i, &val, len * 8) { |
| 372 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 373 | |
Marc Zyngier | 82e40f5 | 2019-08-28 11:10:16 +0100 | [diff] [blame] | 374 | /* GICD_ICPENDR0 SGI bits are WI */ |
| 375 | if (is_vgic_v2_sgi(vcpu, irq)) { |
| 376 | vgic_put_irq(vcpu->kvm, irq); |
| 377 | continue; |
| 378 | } |
| 379 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 380 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 381 | |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 382 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 383 | /* HW SGI? Ask the GIC to clear its pending bit */ |
| 384 | int err; |
| 385 | err = irq_set_irqchip_state(irq->host_irq, |
| 386 | IRQCHIP_STATE_PENDING, |
| 387 | false); |
| 388 | WARN_RATELIMIT(err, "IRQ %d", irq->host_irq); |
| 389 | |
| 390 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 391 | vgic_put_irq(vcpu->kvm, irq); |
| 392 | |
| 393 | continue; |
| 394 | } |
| 395 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 396 | if (irq->hw) |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 397 | vgic_hw_irq_cpending(vcpu, irq); |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 398 | else |
| 399 | irq->pending_latch = false; |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 400 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 401 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 402 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Marc Zyngier | ba1ed9e | 2020-04-09 13:05:26 +0100 | [diff] [blame] | 406 | int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu, |
| 407 | gpa_t addr, unsigned int len, |
| 408 | unsigned long val) |
| 409 | { |
| 410 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 411 | int i; |
| 412 | unsigned long flags; |
| 413 | |
| 414 | for_each_set_bit(i, &val, len * 8) { |
| 415 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 416 | |
| 417 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
| 418 | /* |
| 419 | * More fun with GICv2 SGIs! If we're clearing one of them |
| 420 | * from userspace, which source vcpu to clear? Let's not |
| 421 | * even think of it, and blow the whole set. |
| 422 | */ |
| 423 | if (is_vgic_v2_sgi(vcpu, irq)) |
| 424 | irq->source = 0; |
| 425 | |
| 426 | irq->pending_latch = false; |
| 427 | |
| 428 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
| 429 | |
| 430 | vgic_put_irq(vcpu->kvm, irq); |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * If we are fiddling with an IRQ's active state, we have to make sure the IRQ |
| 438 | * is not queued on some running VCPU's LRs, because then the change to the |
| 439 | * active state can be overwritten when the VCPU's state is synced coming back |
| 440 | * from the guest. |
| 441 | * |
| 442 | * For shared interrupts as well as GICv3 private interrupts, we have to |
| 443 | * stop all the VCPUs because interrupts can be migrated while we don't hold |
| 444 | * the IRQ locks and we don't want to be chasing moving targets. |
| 445 | * |
| 446 | * For GICv2 private interrupts we don't have to do anything because |
| 447 | * userspace accesses to the VGIC state already require all VCPUs to be |
| 448 | * stopped, and only the VCPU itself can modify its private interrupts |
| 449 | * active state, which guarantees that the VCPU is not running. |
| 450 | */ |
| 451 | static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid) |
| 452 | { |
| 453 | if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 || |
| 454 | intid >= VGIC_NR_PRIVATE_IRQS) |
| 455 | kvm_arm_halt_guest(vcpu->kvm); |
| 456 | } |
| 457 | |
| 458 | /* See vgic_access_active_prepare */ |
| 459 | static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid) |
| 460 | { |
| 461 | if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 || |
| 462 | intid >= VGIC_NR_PRIVATE_IRQS) |
| 463 | kvm_arm_resume_guest(vcpu->kvm); |
| 464 | } |
| 465 | |
| 466 | static unsigned long __vgic_mmio_read_active(struct kvm_vcpu *vcpu, |
| 467 | gpa_t addr, unsigned int len) |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 468 | { |
| 469 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 470 | u32 value = 0; |
| 471 | int i; |
| 472 | |
| 473 | /* Loop over all IRQs affected by this read */ |
| 474 | for (i = 0; i < len * 8; i++) { |
| 475 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 476 | |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 477 | /* |
| 478 | * Even for HW interrupts, don't evaluate the HW state as |
| 479 | * all the guest is interested in is the virtual state. |
| 480 | */ |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 481 | if (irq->active) |
| 482 | value |= (1U << i); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 483 | |
| 484 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return value; |
| 488 | } |
| 489 | |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 490 | unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu, |
| 491 | gpa_t addr, unsigned int len) |
| 492 | { |
| 493 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 494 | u32 val; |
| 495 | |
| 496 | mutex_lock(&vcpu->kvm->lock); |
| 497 | vgic_access_active_prepare(vcpu, intid); |
| 498 | |
| 499 | val = __vgic_mmio_read_active(vcpu, addr, len); |
| 500 | |
| 501 | vgic_access_active_finish(vcpu, intid); |
| 502 | mutex_unlock(&vcpu->kvm->lock); |
| 503 | |
| 504 | return val; |
| 505 | } |
| 506 | |
| 507 | unsigned long vgic_uaccess_read_active(struct kvm_vcpu *vcpu, |
| 508 | gpa_t addr, unsigned int len) |
| 509 | { |
| 510 | return __vgic_mmio_read_active(vcpu, addr, len); |
| 511 | } |
| 512 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 513 | /* Must be called with irq->irq_lock held */ |
| 514 | static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq, |
| 515 | bool active, bool is_uaccess) |
| 516 | { |
| 517 | if (is_uaccess) |
| 518 | return; |
| 519 | |
| 520 | irq->active = active; |
| 521 | vgic_irq_set_phys_active(irq, active); |
| 522 | } |
| 523 | |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 524 | 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] | 525 | bool active) |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 526 | { |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 527 | unsigned long flags; |
Paolo Bonzini | 7495e22 | 2020-01-09 09:57:19 -0500 | [diff] [blame] | 528 | struct kvm_vcpu *requester_vcpu = kvm_get_running_vcpu(); |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 529 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 530 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Jintack Lim | 370a0ec | 2017-03-06 05:42:37 -0800 | [diff] [blame] | 531 | |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 532 | if (irq->hw && !vgic_irq_is_sgi(irq->intid)) { |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 533 | vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 534 | } else if (irq->hw && vgic_irq_is_sgi(irq->intid)) { |
| 535 | /* |
| 536 | * GICv4.1 VSGI feature doesn't track an active state, |
| 537 | * so let's not kid ourselves, there is nothing we can |
| 538 | * do here. |
| 539 | */ |
| 540 | irq->active = false; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 541 | } else { |
| 542 | u32 model = vcpu->kvm->arch.vgic.vgic_model; |
Christoffer Dall | 60c3ab3 | 2018-12-11 12:51:03 +0100 | [diff] [blame] | 543 | u8 active_source; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 544 | |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 545 | irq->active = active; |
Christoffer Dall | 60c3ab3 | 2018-12-11 12:51:03 +0100 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * The GICv2 architecture indicates that the source CPUID for |
| 549 | * an SGI should be provided during an EOI which implies that |
| 550 | * the active state is stored somewhere, but at the same time |
| 551 | * this state is not architecturally exposed anywhere and we |
| 552 | * have no way of knowing the right source. |
| 553 | * |
| 554 | * This may lead to a VCPU not being able to receive |
| 555 | * additional instances of a particular SGI after migration |
| 556 | * for a GICv2 VM on some GIC implementations. Oh well. |
| 557 | */ |
| 558 | active_source = (requester_vcpu) ? requester_vcpu->vcpu_id : 0; |
| 559 | |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 560 | if (model == KVM_DEV_TYPE_ARM_VGIC_V2 && |
| 561 | active && vgic_irq_is_sgi(irq->intid)) |
Christoffer Dall | 60c3ab3 | 2018-12-11 12:51:03 +0100 | [diff] [blame] | 562 | irq->active_source = active_source; |
Marc Zyngier | 5369290 | 2018-04-18 10:39:04 +0100 | [diff] [blame] | 563 | } |
Christoffer Dall | df635c5 | 2017-09-01 16:25:12 +0200 | [diff] [blame] | 564 | |
| 565 | if (irq->active) |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 566 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 567 | else |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 568 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 571 | static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 572 | gpa_t addr, unsigned int len, |
| 573 | unsigned long val) |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 574 | { |
| 575 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 576 | int i; |
| 577 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 578 | for_each_set_bit(i, &val, len * 8) { |
| 579 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Christoffer Dall | 35a2d58 | 2016-05-20 15:25:28 +0200 | [diff] [blame] | 580 | vgic_mmio_change_active(vcpu, irq, false); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 581 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 582 | } |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 586 | gpa_t addr, unsigned int len, |
| 587 | unsigned long val) |
| 588 | { |
| 589 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 590 | |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 591 | mutex_lock(&vcpu->kvm->lock); |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 592 | vgic_access_active_prepare(vcpu, intid); |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 593 | |
| 594 | __vgic_mmio_write_cactive(vcpu, addr, len, val); |
| 595 | |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 596 | vgic_access_active_finish(vcpu, intid); |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 597 | mutex_unlock(&vcpu->kvm->lock); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 600 | int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu, |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 601 | gpa_t addr, unsigned int len, |
| 602 | unsigned long val) |
| 603 | { |
| 604 | __vgic_mmio_write_cactive(vcpu, addr, len, val); |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 605 | return 0; |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 609 | gpa_t addr, unsigned int len, |
| 610 | unsigned long val) |
| 611 | { |
| 612 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
| 613 | int i; |
| 614 | |
| 615 | for_each_set_bit(i, &val, len * 8) { |
| 616 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 617 | vgic_mmio_change_active(vcpu, irq, true); |
| 618 | vgic_put_irq(vcpu->kvm, irq); |
| 619 | } |
| 620 | } |
| 621 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 622 | void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 623 | gpa_t addr, unsigned int len, |
| 624 | unsigned long val) |
| 625 | { |
| 626 | u32 intid = VGIC_ADDR_TO_INTID(addr, 1); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 627 | |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 628 | mutex_lock(&vcpu->kvm->lock); |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 629 | vgic_access_active_prepare(vcpu, intid); |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 630 | |
| 631 | __vgic_mmio_write_sactive(vcpu, addr, len, val); |
| 632 | |
Marc Zyngier | 9a50ebb | 2020-04-06 16:21:20 +0100 | [diff] [blame] | 633 | vgic_access_active_finish(vcpu, intid); |
Christoffer Dall | abd7229 | 2017-05-06 20:01:24 +0200 | [diff] [blame] | 634 | mutex_unlock(&vcpu->kvm->lock); |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 637 | int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu, |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 638 | gpa_t addr, unsigned int len, |
| 639 | unsigned long val) |
| 640 | { |
| 641 | __vgic_mmio_write_sactive(vcpu, addr, len, val); |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 642 | return 0; |
Christoffer Dall | 3197191 | 2017-05-16 09:44:39 +0200 | [diff] [blame] | 643 | } |
| 644 | |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 645 | unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu, |
| 646 | gpa_t addr, unsigned int len) |
| 647 | { |
| 648 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 649 | int i; |
| 650 | u64 val = 0; |
| 651 | |
| 652 | for (i = 0; i < len; i++) { |
| 653 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 654 | |
| 655 | val |= (u64)irq->priority << (i * 8); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 656 | |
| 657 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return val; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * We currently don't handle changing the priority of an interrupt that |
| 665 | * is already pending on a VCPU. If there is a need for this, we would |
| 666 | * need to make this VCPU exit and re-evaluate the priorities, potentially |
| 667 | * leading to this interrupt getting presented now to the guest (if it has |
| 668 | * been masked by the priority mask before). |
| 669 | */ |
| 670 | void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, |
| 671 | gpa_t addr, unsigned int len, |
| 672 | unsigned long val) |
| 673 | { |
| 674 | u32 intid = VGIC_ADDR_TO_INTID(addr, 8); |
| 675 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 676 | unsigned long flags; |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 677 | |
| 678 | for (i = 0; i < len; i++) { |
| 679 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 680 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 681 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 682 | /* Narrow the priority range to what we actually support */ |
| 683 | irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS); |
Marc Zyngier | ef1820b | 2020-03-04 20:33:25 +0000 | [diff] [blame] | 684 | if (irq->hw && vgic_irq_is_sgi(irq->intid)) |
| 685 | vgic_update_vsgi(irq); |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 686 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 687 | |
| 688 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 692 | unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu, |
| 693 | gpa_t addr, unsigned int len) |
| 694 | { |
| 695 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 696 | u32 value = 0; |
| 697 | int i; |
| 698 | |
| 699 | for (i = 0; i < len * 4; i++) { |
| 700 | struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 701 | |
| 702 | if (irq->config == VGIC_CONFIG_EDGE) |
| 703 | value |= (2U << (i * 2)); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 704 | |
| 705 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | return value; |
| 709 | } |
| 710 | |
| 711 | void vgic_mmio_write_config(struct kvm_vcpu *vcpu, |
| 712 | gpa_t addr, unsigned int len, |
| 713 | unsigned long val) |
| 714 | { |
| 715 | u32 intid = VGIC_ADDR_TO_INTID(addr, 2); |
| 716 | int i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 717 | unsigned long flags; |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 718 | |
| 719 | for (i = 0; i < len * 4; i++) { |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 720 | struct vgic_irq *irq; |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 721 | |
| 722 | /* |
| 723 | * The configuration cannot be changed for SGIs in general, |
| 724 | * for PPIs this is IMPLEMENTATION DEFINED. The arch timer |
| 725 | * code relies on PPIs being level triggered, so we also |
| 726 | * make them read-only here. |
| 727 | */ |
| 728 | if (intid + i < VGIC_NR_PRIVATE_IRQS) |
| 729 | continue; |
| 730 | |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 731 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 732 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 733 | |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 734 | if (test_bit(i * 2 + 1, &val)) |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 735 | irq->config = VGIC_CONFIG_EDGE; |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 736 | else |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 737 | irq->config = VGIC_CONFIG_LEVEL; |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 738 | |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 739 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | 5dd4b92 | 2016-07-15 12:43:27 +0100 | [diff] [blame] | 740 | vgic_put_irq(vcpu->kvm, irq); |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 744 | u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid) |
| 745 | { |
| 746 | int i; |
| 747 | u64 val = 0; |
| 748 | int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 749 | |
| 750 | for (i = 0; i < 32; i++) { |
| 751 | struct vgic_irq *irq; |
| 752 | |
| 753 | if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs) |
| 754 | continue; |
| 755 | |
| 756 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 757 | if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level) |
| 758 | val |= (1U << i); |
| 759 | |
| 760 | vgic_put_irq(vcpu->kvm, irq); |
| 761 | } |
| 762 | |
| 763 | return val; |
| 764 | } |
| 765 | |
| 766 | void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid, |
| 767 | const u64 val) |
| 768 | { |
| 769 | int i; |
| 770 | 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] | 771 | unsigned long flags; |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 772 | |
| 773 | for (i = 0; i < 32; i++) { |
| 774 | struct vgic_irq *irq; |
| 775 | bool new_level; |
| 776 | |
| 777 | if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs) |
| 778 | continue; |
| 779 | |
| 780 | irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i); |
| 781 | |
| 782 | /* |
| 783 | * Line level is set irrespective of irq type |
| 784 | * (level or edge) to avoid dependency that VM should |
| 785 | * restore irq config before line level. |
| 786 | */ |
| 787 | new_level = !!(val & (1U << i)); |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 788 | raw_spin_lock_irqsave(&irq->irq_lock, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 789 | irq->line_level = new_level; |
| 790 | if (new_level) |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 791 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 792 | else |
Julien Thierry | 8fa3adb | 2019-01-07 15:06:15 +0000 | [diff] [blame] | 793 | raw_spin_unlock_irqrestore(&irq->irq_lock, flags); |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 794 | |
| 795 | vgic_put_irq(vcpu->kvm, irq); |
| 796 | } |
| 797 | } |
| 798 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 799 | static int match_region(const void *key, const void *elt) |
| 800 | { |
| 801 | const unsigned int offset = (unsigned long)key; |
| 802 | const struct vgic_register_region *region = elt; |
| 803 | |
| 804 | if (offset < region->reg_offset) |
| 805 | return -1; |
| 806 | |
| 807 | if (offset >= region->reg_offset + region->len) |
| 808 | return 1; |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
Eric Auger | 4b7171a | 2016-12-20 09:20:00 +0100 | [diff] [blame] | 813 | const struct vgic_register_region * |
| 814 | vgic_find_mmio_region(const struct vgic_register_region *regions, |
| 815 | int nr_regions, unsigned int offset) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 816 | { |
Eric Auger | 4b7171a | 2016-12-20 09:20:00 +0100 | [diff] [blame] | 817 | return bsearch((void *)(uintptr_t)offset, regions, nr_regions, |
| 818 | sizeof(regions[0]), match_region); |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Vijaya Kumar K | 5fb247d | 2017-01-26 19:50:50 +0530 | [diff] [blame] | 821 | void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
| 822 | { |
| 823 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 824 | vgic_v2_set_vmcr(vcpu, vmcr); |
| 825 | else |
| 826 | vgic_v3_set_vmcr(vcpu, vmcr); |
| 827 | } |
| 828 | |
| 829 | void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr) |
| 830 | { |
| 831 | if (kvm_vgic_global_state.type == VGIC_V2) |
| 832 | vgic_v2_get_vmcr(vcpu, vmcr); |
| 833 | else |
| 834 | vgic_v3_get_vmcr(vcpu, vmcr); |
| 835 | } |
| 836 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 837 | /* |
| 838 | * kvm_mmio_read_buf() returns a value in a format where it can be converted |
| 839 | * to a byte array and be directly observed as the guest wanted it to appear |
| 840 | * in memory if it had done the store itself, which is LE for the GIC, as the |
| 841 | * guest knows the GIC is always LE. |
| 842 | * |
| 843 | * We convert this value to the CPUs native format to deal with it as a data |
| 844 | * value. |
| 845 | */ |
| 846 | unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len) |
| 847 | { |
| 848 | unsigned long data = kvm_mmio_read_buf(val, len); |
| 849 | |
| 850 | switch (len) { |
| 851 | case 1: |
| 852 | return data; |
| 853 | case 2: |
| 854 | return le16_to_cpu(data); |
| 855 | case 4: |
| 856 | return le32_to_cpu(data); |
| 857 | default: |
| 858 | return le64_to_cpu(data); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * kvm_mmio_write_buf() expects a value in a format such that if converted to |
| 864 | * a byte array it is observed as the guest would see it if it could perform |
| 865 | * the load directly. Since the GIC is LE, and the guest knows this, the |
| 866 | * guest expects a value in little endian format. |
| 867 | * |
| 868 | * We convert the data value from the CPUs native format to LE so that the |
| 869 | * value is returned in the proper format. |
| 870 | */ |
| 871 | void vgic_data_host_to_mmio_bus(void *buf, unsigned int len, |
| 872 | unsigned long data) |
| 873 | { |
| 874 | switch (len) { |
| 875 | case 1: |
| 876 | break; |
| 877 | case 2: |
| 878 | data = cpu_to_le16(data); |
| 879 | break; |
| 880 | case 4: |
| 881 | data = cpu_to_le32(data); |
| 882 | break; |
| 883 | default: |
| 884 | data = cpu_to_le64(data); |
| 885 | } |
| 886 | |
| 887 | kvm_mmio_write_buf(buf, len, data); |
| 888 | } |
| 889 | |
| 890 | static |
| 891 | struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev) |
| 892 | { |
| 893 | return container_of(dev, struct vgic_io_device, dev); |
| 894 | } |
| 895 | |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 896 | static bool check_region(const struct kvm *kvm, |
| 897 | const struct vgic_register_region *region, |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 898 | gpa_t addr, int len) |
| 899 | { |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 900 | int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS; |
| 901 | |
| 902 | switch (len) { |
| 903 | case sizeof(u8): |
| 904 | flags = VGIC_ACCESS_8bit; |
| 905 | break; |
| 906 | case sizeof(u32): |
| 907 | flags = VGIC_ACCESS_32bit; |
| 908 | break; |
| 909 | case sizeof(u64): |
| 910 | flags = VGIC_ACCESS_64bit; |
| 911 | break; |
| 912 | default: |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) { |
| 917 | if (!region->bits_per_irq) |
| 918 | return true; |
| 919 | |
| 920 | /* Do we access a non-allocated IRQ? */ |
| 921 | return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs; |
| 922 | } |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
Vijaya Kumar K | 94574c9 | 2017-01-26 19:50:47 +0530 | [diff] [blame] | 927 | const struct vgic_register_region * |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 928 | vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, |
| 929 | gpa_t addr, int len) |
| 930 | { |
| 931 | const struct vgic_register_region *region; |
| 932 | |
| 933 | region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions, |
| 934 | addr - iodev->base_addr); |
| 935 | if (!region || !check_region(vcpu->kvm, region, addr, len)) |
| 936 | return NULL; |
| 937 | |
| 938 | return region; |
| 939 | } |
| 940 | |
Eric Auger | da38530 | 2021-04-05 18:39:38 +0200 | [diff] [blame] | 941 | static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 942 | gpa_t addr, u32 *val) |
| 943 | { |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 944 | const struct vgic_register_region *region; |
| 945 | struct kvm_vcpu *r_vcpu; |
| 946 | |
| 947 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 948 | if (!region) { |
| 949 | *val = 0; |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 954 | if (region->uaccess_read) |
| 955 | *val = region->uaccess_read(r_vcpu, addr, sizeof(u32)); |
| 956 | else |
| 957 | *val = region->read(r_vcpu, addr, sizeof(u32)); |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Eric Auger | da38530 | 2021-04-05 18:39:38 +0200 | [diff] [blame] | 962 | static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev, |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 963 | gpa_t addr, const u32 *val) |
| 964 | { |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 965 | const struct vgic_register_region *region; |
| 966 | struct kvm_vcpu *r_vcpu; |
| 967 | |
| 968 | region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32)); |
| 969 | if (!region) |
| 970 | return 0; |
| 971 | |
| 972 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 973 | if (region->uaccess_write) |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 974 | return region->uaccess_write(r_vcpu, addr, sizeof(u32), *val); |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 975 | |
Christoffer Dall | c6e0917 | 2018-07-16 15:06:23 +0200 | [diff] [blame] | 976 | region->write(r_vcpu, addr, sizeof(u32), *val); |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Userland access to VGIC registers. |
| 982 | */ |
| 983 | int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev, |
| 984 | bool is_write, int offset, u32 *val) |
| 985 | { |
| 986 | if (is_write) |
Eric Auger | da38530 | 2021-04-05 18:39:38 +0200 | [diff] [blame] | 987 | return vgic_uaccess_write(vcpu, dev, offset, val); |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 988 | else |
Eric Auger | da38530 | 2021-04-05 18:39:38 +0200 | [diff] [blame] | 989 | return vgic_uaccess_read(vcpu, dev, offset, val); |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 990 | } |
| 991 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 992 | static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 993 | gpa_t addr, int len, void *val) |
| 994 | { |
| 995 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 996 | const struct vgic_register_region *region; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 997 | unsigned long data = 0; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 998 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 999 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 1000 | if (!region) { |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1001 | memset(val, 0, len); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1005 | switch (iodev->iodev_type) { |
| 1006 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 1007 | data = region->read(vcpu, addr, len); |
| 1008 | break; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1009 | case IODEV_DIST: |
| 1010 | data = region->read(vcpu, addr, len); |
| 1011 | break; |
| 1012 | case IODEV_REDIST: |
| 1013 | data = region->read(iodev->redist_vcpu, addr, len); |
| 1014 | break; |
| 1015 | case IODEV_ITS: |
| 1016 | data = region->its_read(vcpu->kvm, iodev->its, addr, len); |
| 1017 | break; |
| 1018 | } |
| 1019 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1020 | vgic_data_host_to_mmio_bus(val, len, data); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 1025 | gpa_t addr, int len, const void *val) |
| 1026 | { |
| 1027 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 1028 | const struct vgic_register_region *region; |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1029 | unsigned long data = vgic_data_mmio_bus_to_host(val, len); |
| 1030 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 1031 | region = vgic_get_mmio_region(vcpu, iodev, addr, len); |
| 1032 | if (!region) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1033 | return 0; |
| 1034 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1035 | switch (iodev->iodev_type) { |
| 1036 | case IODEV_CPUIF: |
Eric Auger | 9d5fcb9 | 2016-07-18 10:57:36 +0000 | [diff] [blame] | 1037 | region->write(vcpu, addr, len, data); |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1038 | break; |
| 1039 | case IODEV_DIST: |
| 1040 | region->write(vcpu, addr, len, data); |
| 1041 | break; |
| 1042 | case IODEV_REDIST: |
| 1043 | region->write(iodev->redist_vcpu, addr, len, data); |
| 1044 | break; |
| 1045 | case IODEV_ITS: |
| 1046 | region->its_write(vcpu->kvm, iodev->its, addr, len, data); |
| 1047 | break; |
| 1048 | } |
| 1049 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
Rikard Falkeborn | 636dcd0 | 2021-12-04 22:35:18 +0100 | [diff] [blame] | 1053 | const struct kvm_io_device_ops kvm_io_gic_ops = { |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1054 | .read = dispatch_mmio_read, |
| 1055 | .write = dispatch_mmio_write, |
| 1056 | }; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 1057 | |
| 1058 | int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address, |
| 1059 | enum vgic_type type) |
| 1060 | { |
| 1061 | struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev; |
| 1062 | int ret = 0; |
| 1063 | unsigned int len; |
| 1064 | |
| 1065 | switch (type) { |
| 1066 | case VGIC_V2: |
| 1067 | len = vgic_v2_init_dist_iodev(io_device); |
| 1068 | break; |
Andre Przywara | ed9b8ce | 2015-12-01 14:34:34 +0000 | [diff] [blame] | 1069 | case VGIC_V3: |
| 1070 | len = vgic_v3_init_dist_iodev(io_device); |
| 1071 | break; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 1072 | default: |
| 1073 | BUG_ON(1); |
| 1074 | } |
| 1075 | |
| 1076 | io_device->base_addr = dist_base_address; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1077 | io_device->iodev_type = IODEV_DIST; |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 1078 | io_device->redist_vcpu = NULL; |
| 1079 | |
| 1080 | mutex_lock(&kvm->slots_lock); |
| 1081 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address, |
| 1082 | len, &io_device->dev); |
| 1083 | mutex_unlock(&kvm->slots_lock); |
| 1084 | |
| 1085 | return ret; |
| 1086 | } |