1 // SPDX-License-Identifier: GPL-2.0-only
3 * VGIC MMIO handling functions
6 #include <linux/bitops.h>
7 #include <linux/bsearch.h>
8 #include <linux/interrupt.h>
10 #include <linux/kvm.h>
11 #include <linux/kvm_host.h>
12 #include <kvm/iodev.h>
13 #include <kvm/arm_arch_timer.h>
14 #include <kvm/arm_vgic.h>
17 #include "vgic-mmio.h"
19 unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
20 gpa_t addr, unsigned int len)
25 unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
26 gpa_t addr, unsigned int len)
31 void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
32 unsigned int len, unsigned long val)
37 int vgic_mmio_uaccess_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
38 unsigned int len, unsigned long val)
44 unsigned long vgic_mmio_read_group(struct kvm_vcpu *vcpu,
45 gpa_t addr, unsigned int len)
47 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
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);
58 vgic_put_irq(vcpu->kvm, irq);
64 static void vgic_update_vsgi(struct vgic_irq *irq)
66 WARN_ON(its_prop_update_vsgi(irq->host_irq, irq->priority, irq->group));
69 void vgic_mmio_write_group(struct kvm_vcpu *vcpu, gpa_t addr,
70 unsigned int len, unsigned long val)
72 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
76 for (i = 0; i < len * 8; i++) {
77 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
79 raw_spin_lock_irqsave(&irq->irq_lock, flags);
80 irq->group = !!(val & BIT(i));
81 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
82 vgic_update_vsgi(irq);
83 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
85 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
88 vgic_put_irq(vcpu->kvm, irq);
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.
96 unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
97 gpa_t addr, unsigned int len)
99 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
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);
110 vgic_put_irq(vcpu->kvm, irq);
116 void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
117 gpa_t addr, unsigned int len,
120 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
124 for_each_set_bit(i, &val, len * 8) {
125 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
127 raw_spin_lock_irqsave(&irq->irq_lock, flags);
128 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
130 struct irq_data *data;
133 data = &irq_to_desc(irq->host_irq)->irq_data;
134 while (irqd_irq_disabled(data))
135 enable_irq(irq->host_irq);
138 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
139 vgic_put_irq(vcpu->kvm, irq);
142 } else if (vgic_irq_is_mapped_level(irq)) {
143 bool was_high = irq->line_level;
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.
150 irq->line_level = vgic_get_phys_line_level(irq);
152 * Deactivate the physical interrupt so the GIC will let
153 * us know when it is asserted again.
155 if (!irq->active && was_high && !irq->line_level)
156 vgic_irq_set_phys_active(irq, false);
159 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
161 vgic_put_irq(vcpu->kvm, irq);
165 void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
166 gpa_t addr, unsigned int len,
169 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
173 for_each_set_bit(i, &val, len * 8) {
174 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
176 raw_spin_lock_irqsave(&irq->irq_lock, flags);
177 if (irq->hw && vgic_irq_is_sgi(irq->intid) && irq->enabled)
178 disable_irq_nosync(irq->host_irq);
180 irq->enabled = false;
182 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
183 vgic_put_irq(vcpu->kvm, irq);
187 int vgic_uaccess_write_senable(struct kvm_vcpu *vcpu,
188 gpa_t addr, unsigned int len,
191 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
195 for_each_set_bit(i, &val, len * 8) {
196 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
198 raw_spin_lock_irqsave(&irq->irq_lock, flags);
200 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
202 vgic_put_irq(vcpu->kvm, irq);
208 int vgic_uaccess_write_cenable(struct kvm_vcpu *vcpu,
209 gpa_t addr, unsigned int len,
212 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
216 for_each_set_bit(i, &val, len * 8) {
217 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
219 raw_spin_lock_irqsave(&irq->irq_lock, flags);
220 irq->enabled = false;
221 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
223 vgic_put_irq(vcpu->kvm, irq);
229 static unsigned long __read_pending(struct kvm_vcpu *vcpu,
230 gpa_t addr, unsigned int len,
233 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
237 /* Loop over all IRQs affected by this read */
238 for (i = 0; i < len * 8; i++) {
239 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
243 raw_spin_lock_irqsave(&irq->irq_lock, flags);
244 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
248 err = irq_get_irqchip_state(irq->host_irq,
249 IRQCHIP_STATE_PENDING,
251 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
252 } else if (!is_user && vgic_irq_is_mapped_level(irq)) {
253 val = vgic_get_phys_line_level(irq);
255 val = irq_is_pending(irq);
258 value |= ((u32)val << i);
259 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
261 vgic_put_irq(vcpu->kvm, irq);
267 unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
268 gpa_t addr, unsigned int len)
270 return __read_pending(vcpu, addr, len, false);
273 unsigned long vgic_uaccess_read_pending(struct kvm_vcpu *vcpu,
274 gpa_t addr, unsigned int len)
276 return __read_pending(vcpu, addr, len, true);
279 static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
281 return (vgic_irq_is_sgi(irq->intid) &&
282 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2);
285 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
286 gpa_t addr, unsigned int len,
289 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
293 for_each_set_bit(i, &val, len * 8) {
294 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
296 /* GICD_ISPENDR0 SGI bits are WI */
297 if (is_vgic_v2_sgi(vcpu, irq)) {
298 vgic_put_irq(vcpu->kvm, irq);
302 raw_spin_lock_irqsave(&irq->irq_lock, flags);
304 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
305 /* HW SGI? Ask the GIC to inject it */
307 err = irq_set_irqchip_state(irq->host_irq,
308 IRQCHIP_STATE_PENDING,
310 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
312 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
313 vgic_put_irq(vcpu->kvm, irq);
318 irq->pending_latch = true;
320 vgic_irq_set_phys_active(irq, true);
322 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
323 vgic_put_irq(vcpu->kvm, irq);
327 int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,
328 gpa_t addr, unsigned int len,
331 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
335 for_each_set_bit(i, &val, len * 8) {
336 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
338 raw_spin_lock_irqsave(&irq->irq_lock, flags);
339 irq->pending_latch = true;
342 * GICv2 SGIs are terribly broken. We can't restore
343 * the source of the interrupt, so just pick the vcpu
344 * itself as the source...
346 if (is_vgic_v2_sgi(vcpu, irq))
347 irq->source |= BIT(vcpu->vcpu_id);
349 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
351 vgic_put_irq(vcpu->kvm, irq);
357 /* Must be called with irq->irq_lock held */
358 static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
360 irq->pending_latch = false;
363 * We don't want the guest to effectively mask the physical
364 * interrupt by doing a write to SPENDR followed by a write to
365 * CPENDR for HW interrupts, so we clear the active state on
366 * the physical side if the virtual interrupt is not active.
367 * This may lead to taking an additional interrupt on the
368 * host, but that should not be a problem as the worst that
369 * can happen is an additional vgic injection. We also clear
370 * the pending state to maintain proper semantics for edge HW
373 vgic_irq_set_phys_pending(irq, false);
375 vgic_irq_set_phys_active(irq, false);
378 void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
379 gpa_t addr, unsigned int len,
382 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
386 for_each_set_bit(i, &val, len * 8) {
387 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
389 /* GICD_ICPENDR0 SGI bits are WI */
390 if (is_vgic_v2_sgi(vcpu, irq)) {
391 vgic_put_irq(vcpu->kvm, irq);
395 raw_spin_lock_irqsave(&irq->irq_lock, flags);
397 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
398 /* HW SGI? Ask the GIC to clear its pending bit */
400 err = irq_set_irqchip_state(irq->host_irq,
401 IRQCHIP_STATE_PENDING,
403 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
405 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
406 vgic_put_irq(vcpu->kvm, irq);
412 vgic_hw_irq_cpending(vcpu, irq);
414 irq->pending_latch = false;
416 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
417 vgic_put_irq(vcpu->kvm, irq);
421 int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,
422 gpa_t addr, unsigned int len,
425 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
429 for_each_set_bit(i, &val, len * 8) {
430 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
432 raw_spin_lock_irqsave(&irq->irq_lock, flags);
434 * More fun with GICv2 SGIs! If we're clearing one of them
435 * from userspace, which source vcpu to clear? Let's not
436 * even think of it, and blow the whole set.
438 if (is_vgic_v2_sgi(vcpu, irq))
441 irq->pending_latch = false;
443 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
445 vgic_put_irq(vcpu->kvm, irq);
452 * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
453 * is not queued on some running VCPU's LRs, because then the change to the
454 * active state can be overwritten when the VCPU's state is synced coming back
457 * For shared interrupts as well as GICv3 private interrupts, we have to
458 * stop all the VCPUs because interrupts can be migrated while we don't hold
459 * the IRQ locks and we don't want to be chasing moving targets.
461 * For GICv2 private interrupts we don't have to do anything because
462 * userspace accesses to the VGIC state already require all VCPUs to be
463 * stopped, and only the VCPU itself can modify its private interrupts
464 * active state, which guarantees that the VCPU is not running.
466 static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
468 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
469 intid >= VGIC_NR_PRIVATE_IRQS)
470 kvm_arm_halt_guest(vcpu->kvm);
473 /* See vgic_access_active_prepare */
474 static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid)
476 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
477 intid >= VGIC_NR_PRIVATE_IRQS)
478 kvm_arm_resume_guest(vcpu->kvm);
481 static unsigned long __vgic_mmio_read_active(struct kvm_vcpu *vcpu,
482 gpa_t addr, unsigned int len)
484 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
488 /* Loop over all IRQs affected by this read */
489 for (i = 0; i < len * 8; i++) {
490 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
493 * Even for HW interrupts, don't evaluate the HW state as
494 * all the guest is interested in is the virtual state.
499 vgic_put_irq(vcpu->kvm, irq);
505 unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
506 gpa_t addr, unsigned int len)
508 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
511 mutex_lock(&vcpu->kvm->lock);
512 vgic_access_active_prepare(vcpu, intid);
514 val = __vgic_mmio_read_active(vcpu, addr, len);
516 vgic_access_active_finish(vcpu, intid);
517 mutex_unlock(&vcpu->kvm->lock);
522 unsigned long vgic_uaccess_read_active(struct kvm_vcpu *vcpu,
523 gpa_t addr, unsigned int len)
525 return __vgic_mmio_read_active(vcpu, addr, len);
528 /* Must be called with irq->irq_lock held */
529 static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
530 bool active, bool is_uaccess)
535 irq->active = active;
536 vgic_irq_set_phys_active(irq, active);
539 static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
543 struct kvm_vcpu *requester_vcpu = kvm_get_running_vcpu();
545 raw_spin_lock_irqsave(&irq->irq_lock, flags);
547 if (irq->hw && !vgic_irq_is_sgi(irq->intid)) {
548 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu);
549 } else if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
551 * GICv4.1 VSGI feature doesn't track an active state,
552 * so let's not kid ourselves, there is nothing we can
557 u32 model = vcpu->kvm->arch.vgic.vgic_model;
560 irq->active = active;
563 * The GICv2 architecture indicates that the source CPUID for
564 * an SGI should be provided during an EOI which implies that
565 * the active state is stored somewhere, but at the same time
566 * this state is not architecturally exposed anywhere and we
567 * have no way of knowing the right source.
569 * This may lead to a VCPU not being able to receive
570 * additional instances of a particular SGI after migration
571 * for a GICv2 VM on some GIC implementations. Oh well.
573 active_source = (requester_vcpu) ? requester_vcpu->vcpu_id : 0;
575 if (model == KVM_DEV_TYPE_ARM_VGIC_V2 &&
576 active && vgic_irq_is_sgi(irq->intid))
577 irq->active_source = active_source;
581 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
583 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
586 static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
587 gpa_t addr, unsigned int len,
590 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
593 for_each_set_bit(i, &val, len * 8) {
594 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
595 vgic_mmio_change_active(vcpu, irq, false);
596 vgic_put_irq(vcpu->kvm, irq);
600 void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
601 gpa_t addr, unsigned int len,
604 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
606 mutex_lock(&vcpu->kvm->lock);
607 vgic_access_active_prepare(vcpu, intid);
609 __vgic_mmio_write_cactive(vcpu, addr, len, val);
611 vgic_access_active_finish(vcpu, intid);
612 mutex_unlock(&vcpu->kvm->lock);
615 int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
616 gpa_t addr, unsigned int len,
619 __vgic_mmio_write_cactive(vcpu, addr, len, val);
623 static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
624 gpa_t addr, unsigned int len,
627 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
630 for_each_set_bit(i, &val, len * 8) {
631 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
632 vgic_mmio_change_active(vcpu, irq, true);
633 vgic_put_irq(vcpu->kvm, irq);
637 void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
638 gpa_t addr, unsigned int len,
641 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
643 mutex_lock(&vcpu->kvm->lock);
644 vgic_access_active_prepare(vcpu, intid);
646 __vgic_mmio_write_sactive(vcpu, addr, len, val);
648 vgic_access_active_finish(vcpu, intid);
649 mutex_unlock(&vcpu->kvm->lock);
652 int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
653 gpa_t addr, unsigned int len,
656 __vgic_mmio_write_sactive(vcpu, addr, len, val);
660 unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
661 gpa_t addr, unsigned int len)
663 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
667 for (i = 0; i < len; i++) {
668 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
670 val |= (u64)irq->priority << (i * 8);
672 vgic_put_irq(vcpu->kvm, irq);
679 * We currently don't handle changing the priority of an interrupt that
680 * is already pending on a VCPU. If there is a need for this, we would
681 * need to make this VCPU exit and re-evaluate the priorities, potentially
682 * leading to this interrupt getting presented now to the guest (if it has
683 * been masked by the priority mask before).
685 void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
686 gpa_t addr, unsigned int len,
689 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
693 for (i = 0; i < len; i++) {
694 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
696 raw_spin_lock_irqsave(&irq->irq_lock, flags);
697 /* Narrow the priority range to what we actually support */
698 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
699 if (irq->hw && vgic_irq_is_sgi(irq->intid))
700 vgic_update_vsgi(irq);
701 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
703 vgic_put_irq(vcpu->kvm, irq);
707 unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
708 gpa_t addr, unsigned int len)
710 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
714 for (i = 0; i < len * 4; i++) {
715 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
717 if (irq->config == VGIC_CONFIG_EDGE)
718 value |= (2U << (i * 2));
720 vgic_put_irq(vcpu->kvm, irq);
726 void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
727 gpa_t addr, unsigned int len,
730 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
734 for (i = 0; i < len * 4; i++) {
735 struct vgic_irq *irq;
738 * The configuration cannot be changed for SGIs in general,
739 * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
740 * code relies on PPIs being level triggered, so we also
741 * make them read-only here.
743 if (intid + i < VGIC_NR_PRIVATE_IRQS)
746 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
747 raw_spin_lock_irqsave(&irq->irq_lock, flags);
749 if (test_bit(i * 2 + 1, &val))
750 irq->config = VGIC_CONFIG_EDGE;
752 irq->config = VGIC_CONFIG_LEVEL;
754 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
755 vgic_put_irq(vcpu->kvm, irq);
759 u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
763 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
765 for (i = 0; i < 32; i++) {
766 struct vgic_irq *irq;
768 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
771 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
772 if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level)
775 vgic_put_irq(vcpu->kvm, irq);
781 void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
785 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
788 for (i = 0; i < 32; i++) {
789 struct vgic_irq *irq;
792 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
795 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
798 * Line level is set irrespective of irq type
799 * (level or edge) to avoid dependency that VM should
800 * restore irq config before line level.
802 new_level = !!(val & (1U << i));
803 raw_spin_lock_irqsave(&irq->irq_lock, flags);
804 irq->line_level = new_level;
806 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
808 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
810 vgic_put_irq(vcpu->kvm, irq);
814 static int match_region(const void *key, const void *elt)
816 const unsigned int offset = (unsigned long)key;
817 const struct vgic_register_region *region = elt;
819 if (offset < region->reg_offset)
822 if (offset >= region->reg_offset + region->len)
828 const struct vgic_register_region *
829 vgic_find_mmio_region(const struct vgic_register_region *regions,
830 int nr_regions, unsigned int offset)
832 return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
833 sizeof(regions[0]), match_region);
836 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
838 if (kvm_vgic_global_state.type == VGIC_V2)
839 vgic_v2_set_vmcr(vcpu, vmcr);
841 vgic_v3_set_vmcr(vcpu, vmcr);
844 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
846 if (kvm_vgic_global_state.type == VGIC_V2)
847 vgic_v2_get_vmcr(vcpu, vmcr);
849 vgic_v3_get_vmcr(vcpu, vmcr);
853 * kvm_mmio_read_buf() returns a value in a format where it can be converted
854 * to a byte array and be directly observed as the guest wanted it to appear
855 * in memory if it had done the store itself, which is LE for the GIC, as the
856 * guest knows the GIC is always LE.
858 * We convert this value to the CPUs native format to deal with it as a data
861 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
863 unsigned long data = kvm_mmio_read_buf(val, len);
869 return le16_to_cpu(data);
871 return le32_to_cpu(data);
873 return le64_to_cpu(data);
878 * kvm_mmio_write_buf() expects a value in a format such that if converted to
879 * a byte array it is observed as the guest would see it if it could perform
880 * the load directly. Since the GIC is LE, and the guest knows this, the
881 * guest expects a value in little endian format.
883 * We convert the data value from the CPUs native format to LE so that the
884 * value is returned in the proper format.
886 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
893 data = cpu_to_le16(data);
896 data = cpu_to_le32(data);
899 data = cpu_to_le64(data);
902 kvm_mmio_write_buf(buf, len, data);
906 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
908 return container_of(dev, struct vgic_io_device, dev);
911 static bool check_region(const struct kvm *kvm,
912 const struct vgic_register_region *region,
915 int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
919 flags = VGIC_ACCESS_8bit;
922 flags = VGIC_ACCESS_32bit;
925 flags = VGIC_ACCESS_64bit;
931 if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
932 if (!region->bits_per_irq)
935 /* Do we access a non-allocated IRQ? */
936 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
942 const struct vgic_register_region *
943 vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
946 const struct vgic_register_region *region;
948 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
949 addr - iodev->base_addr);
950 if (!region || !check_region(vcpu->kvm, region, addr, len))
956 static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
957 gpa_t addr, u32 *val)
959 const struct vgic_register_region *region;
960 struct kvm_vcpu *r_vcpu;
962 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
968 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
969 if (region->uaccess_read)
970 *val = region->uaccess_read(r_vcpu, addr, sizeof(u32));
972 *val = region->read(r_vcpu, addr, sizeof(u32));
977 static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
978 gpa_t addr, const u32 *val)
980 const struct vgic_register_region *region;
981 struct kvm_vcpu *r_vcpu;
983 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
987 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
988 if (region->uaccess_write)
989 return region->uaccess_write(r_vcpu, addr, sizeof(u32), *val);
991 region->write(r_vcpu, addr, sizeof(u32), *val);
996 * Userland access to VGIC registers.
998 int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
999 bool is_write, int offset, u32 *val)
1002 return vgic_uaccess_write(vcpu, dev, offset, val);
1004 return vgic_uaccess_read(vcpu, dev, offset, val);
1007 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
1008 gpa_t addr, int len, void *val)
1010 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
1011 const struct vgic_register_region *region;
1012 unsigned long data = 0;
1014 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
1016 memset(val, 0, len);
1020 switch (iodev->iodev_type) {
1022 data = region->read(vcpu, addr, len);
1025 data = region->read(vcpu, addr, len);
1028 data = region->read(iodev->redist_vcpu, addr, len);
1031 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
1035 vgic_data_host_to_mmio_bus(val, len, data);
1039 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
1040 gpa_t addr, int len, const void *val)
1042 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
1043 const struct vgic_register_region *region;
1044 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
1046 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
1050 switch (iodev->iodev_type) {
1052 region->write(vcpu, addr, len, data);
1055 region->write(vcpu, addr, len, data);
1058 region->write(iodev->redist_vcpu, addr, len, data);
1061 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
1068 struct kvm_io_device_ops kvm_io_gic_ops = {
1069 .read = dispatch_mmio_read,
1070 .write = dispatch_mmio_write,
1073 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
1074 enum vgic_type type)
1076 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
1082 len = vgic_v2_init_dist_iodev(io_device);
1085 len = vgic_v3_init_dist_iodev(io_device);
1091 io_device->base_addr = dist_base_address;
1092 io_device->iodev_type = IODEV_DIST;
1093 io_device->redist_vcpu = NULL;
1095 mutex_lock(&kvm->slots_lock);
1096 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
1097 len, &io_device->dev);
1098 mutex_unlock(&kvm->slots_lock);