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