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);
244 * When used from userspace with a GICv3 model:
246 * Pending state of interrupt is latched in pending_latch
247 * variable. Userspace will save and restore pending state
248 * and line_level separately.
249 * Refer to Documentation/virt/kvm/devices/arm-vgic-v3.rst
250 * for handling of ISPENDR and ICPENDR.
252 raw_spin_lock_irqsave(&irq->irq_lock, flags);
253 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
257 err = irq_get_irqchip_state(irq->host_irq,
258 IRQCHIP_STATE_PENDING,
260 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
261 } else if (!is_user && vgic_irq_is_mapped_level(irq)) {
262 val = vgic_get_phys_line_level(irq);
264 switch (vcpu->kvm->arch.vgic.vgic_model) {
265 case KVM_DEV_TYPE_ARM_VGIC_V3:
267 val = irq->pending_latch;
272 val = irq_is_pending(irq);
277 value |= ((u32)val << i);
278 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
280 vgic_put_irq(vcpu->kvm, irq);
286 unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
287 gpa_t addr, unsigned int len)
289 return __read_pending(vcpu, addr, len, false);
292 unsigned long vgic_uaccess_read_pending(struct kvm_vcpu *vcpu,
293 gpa_t addr, unsigned int len)
295 return __read_pending(vcpu, addr, len, true);
298 static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
300 return (vgic_irq_is_sgi(irq->intid) &&
301 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2);
304 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
305 gpa_t addr, unsigned int len,
308 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
312 for_each_set_bit(i, &val, len * 8) {
313 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
315 /* GICD_ISPENDR0 SGI bits are WI */
316 if (is_vgic_v2_sgi(vcpu, irq)) {
317 vgic_put_irq(vcpu->kvm, irq);
321 raw_spin_lock_irqsave(&irq->irq_lock, flags);
323 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
324 /* HW SGI? Ask the GIC to inject it */
326 err = irq_set_irqchip_state(irq->host_irq,
327 IRQCHIP_STATE_PENDING,
329 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
331 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
332 vgic_put_irq(vcpu->kvm, irq);
337 irq->pending_latch = true;
339 vgic_irq_set_phys_active(irq, true);
341 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
342 vgic_put_irq(vcpu->kvm, irq);
346 int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,
347 gpa_t addr, unsigned int len,
350 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
354 for_each_set_bit(i, &val, len * 8) {
355 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
357 raw_spin_lock_irqsave(&irq->irq_lock, flags);
358 irq->pending_latch = true;
361 * GICv2 SGIs are terribly broken. We can't restore
362 * the source of the interrupt, so just pick the vcpu
363 * itself as the source...
365 if (is_vgic_v2_sgi(vcpu, irq))
366 irq->source |= BIT(vcpu->vcpu_id);
368 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
370 vgic_put_irq(vcpu->kvm, irq);
376 /* Must be called with irq->irq_lock held */
377 static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
379 irq->pending_latch = false;
382 * We don't want the guest to effectively mask the physical
383 * interrupt by doing a write to SPENDR followed by a write to
384 * CPENDR for HW interrupts, so we clear the active state on
385 * the physical side if the virtual interrupt is not active.
386 * This may lead to taking an additional interrupt on the
387 * host, but that should not be a problem as the worst that
388 * can happen is an additional vgic injection. We also clear
389 * the pending state to maintain proper semantics for edge HW
392 vgic_irq_set_phys_pending(irq, false);
394 vgic_irq_set_phys_active(irq, false);
397 void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
398 gpa_t addr, unsigned int len,
401 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
405 for_each_set_bit(i, &val, len * 8) {
406 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
408 /* GICD_ICPENDR0 SGI bits are WI */
409 if (is_vgic_v2_sgi(vcpu, irq)) {
410 vgic_put_irq(vcpu->kvm, irq);
414 raw_spin_lock_irqsave(&irq->irq_lock, flags);
416 if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
417 /* HW SGI? Ask the GIC to clear its pending bit */
419 err = irq_set_irqchip_state(irq->host_irq,
420 IRQCHIP_STATE_PENDING,
422 WARN_RATELIMIT(err, "IRQ %d", irq->host_irq);
424 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
425 vgic_put_irq(vcpu->kvm, irq);
431 vgic_hw_irq_cpending(vcpu, irq);
433 irq->pending_latch = false;
435 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
436 vgic_put_irq(vcpu->kvm, irq);
440 int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,
441 gpa_t addr, unsigned int len,
444 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
448 for_each_set_bit(i, &val, len * 8) {
449 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
451 raw_spin_lock_irqsave(&irq->irq_lock, flags);
453 * More fun with GICv2 SGIs! If we're clearing one of them
454 * from userspace, which source vcpu to clear? Let's not
455 * even think of it, and blow the whole set.
457 if (is_vgic_v2_sgi(vcpu, irq))
460 irq->pending_latch = false;
462 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
464 vgic_put_irq(vcpu->kvm, irq);
471 * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
472 * is not queued on some running VCPU's LRs, because then the change to the
473 * active state can be overwritten when the VCPU's state is synced coming back
476 * For shared interrupts as well as GICv3 private interrupts, we have to
477 * stop all the VCPUs because interrupts can be migrated while we don't hold
478 * the IRQ locks and we don't want to be chasing moving targets.
480 * For GICv2 private interrupts we don't have to do anything because
481 * userspace accesses to the VGIC state already require all VCPUs to be
482 * stopped, and only the VCPU itself can modify its private interrupts
483 * active state, which guarantees that the VCPU is not running.
485 static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
487 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
488 intid >= VGIC_NR_PRIVATE_IRQS)
489 kvm_arm_halt_guest(vcpu->kvm);
492 /* See vgic_access_active_prepare */
493 static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid)
495 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
496 intid >= VGIC_NR_PRIVATE_IRQS)
497 kvm_arm_resume_guest(vcpu->kvm);
500 static unsigned long __vgic_mmio_read_active(struct kvm_vcpu *vcpu,
501 gpa_t addr, unsigned int len)
503 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
507 /* Loop over all IRQs affected by this read */
508 for (i = 0; i < len * 8; i++) {
509 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
512 * Even for HW interrupts, don't evaluate the HW state as
513 * all the guest is interested in is the virtual state.
518 vgic_put_irq(vcpu->kvm, irq);
524 unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
525 gpa_t addr, unsigned int len)
527 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
530 mutex_lock(&vcpu->kvm->lock);
531 vgic_access_active_prepare(vcpu, intid);
533 val = __vgic_mmio_read_active(vcpu, addr, len);
535 vgic_access_active_finish(vcpu, intid);
536 mutex_unlock(&vcpu->kvm->lock);
541 unsigned long vgic_uaccess_read_active(struct kvm_vcpu *vcpu,
542 gpa_t addr, unsigned int len)
544 return __vgic_mmio_read_active(vcpu, addr, len);
547 /* Must be called with irq->irq_lock held */
548 static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
549 bool active, bool is_uaccess)
554 irq->active = active;
555 vgic_irq_set_phys_active(irq, active);
558 static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
562 struct kvm_vcpu *requester_vcpu = kvm_get_running_vcpu();
564 raw_spin_lock_irqsave(&irq->irq_lock, flags);
566 if (irq->hw && !vgic_irq_is_sgi(irq->intid)) {
567 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu);
568 } else if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
570 * GICv4.1 VSGI feature doesn't track an active state,
571 * so let's not kid ourselves, there is nothing we can
576 u32 model = vcpu->kvm->arch.vgic.vgic_model;
579 irq->active = active;
582 * The GICv2 architecture indicates that the source CPUID for
583 * an SGI should be provided during an EOI which implies that
584 * the active state is stored somewhere, but at the same time
585 * this state is not architecturally exposed anywhere and we
586 * have no way of knowing the right source.
588 * This may lead to a VCPU not being able to receive
589 * additional instances of a particular SGI after migration
590 * for a GICv2 VM on some GIC implementations. Oh well.
592 active_source = (requester_vcpu) ? requester_vcpu->vcpu_id : 0;
594 if (model == KVM_DEV_TYPE_ARM_VGIC_V2 &&
595 active && vgic_irq_is_sgi(irq->intid))
596 irq->active_source = active_source;
600 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
602 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
605 static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
606 gpa_t addr, unsigned int len,
609 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
612 for_each_set_bit(i, &val, len * 8) {
613 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
614 vgic_mmio_change_active(vcpu, irq, false);
615 vgic_put_irq(vcpu->kvm, irq);
619 void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
620 gpa_t addr, unsigned int len,
623 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
625 mutex_lock(&vcpu->kvm->lock);
626 vgic_access_active_prepare(vcpu, intid);
628 __vgic_mmio_write_cactive(vcpu, addr, len, val);
630 vgic_access_active_finish(vcpu, intid);
631 mutex_unlock(&vcpu->kvm->lock);
634 int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
635 gpa_t addr, unsigned int len,
638 __vgic_mmio_write_cactive(vcpu, addr, len, val);
642 static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
643 gpa_t addr, unsigned int len,
646 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
649 for_each_set_bit(i, &val, len * 8) {
650 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
651 vgic_mmio_change_active(vcpu, irq, true);
652 vgic_put_irq(vcpu->kvm, irq);
656 void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
657 gpa_t addr, unsigned int len,
660 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
662 mutex_lock(&vcpu->kvm->lock);
663 vgic_access_active_prepare(vcpu, intid);
665 __vgic_mmio_write_sactive(vcpu, addr, len, val);
667 vgic_access_active_finish(vcpu, intid);
668 mutex_unlock(&vcpu->kvm->lock);
671 int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
672 gpa_t addr, unsigned int len,
675 __vgic_mmio_write_sactive(vcpu, addr, len, val);
679 unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
680 gpa_t addr, unsigned int len)
682 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
686 for (i = 0; i < len; i++) {
687 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
689 val |= (u64)irq->priority << (i * 8);
691 vgic_put_irq(vcpu->kvm, irq);
698 * We currently don't handle changing the priority of an interrupt that
699 * is already pending on a VCPU. If there is a need for this, we would
700 * need to make this VCPU exit and re-evaluate the priorities, potentially
701 * leading to this interrupt getting presented now to the guest (if it has
702 * been masked by the priority mask before).
704 void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
705 gpa_t addr, unsigned int len,
708 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
712 for (i = 0; i < len; i++) {
713 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
715 raw_spin_lock_irqsave(&irq->irq_lock, flags);
716 /* Narrow the priority range to what we actually support */
717 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
718 if (irq->hw && vgic_irq_is_sgi(irq->intid))
719 vgic_update_vsgi(irq);
720 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
722 vgic_put_irq(vcpu->kvm, irq);
726 unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
727 gpa_t addr, unsigned int len)
729 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
733 for (i = 0; i < len * 4; i++) {
734 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
736 if (irq->config == VGIC_CONFIG_EDGE)
737 value |= (2U << (i * 2));
739 vgic_put_irq(vcpu->kvm, irq);
745 void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
746 gpa_t addr, unsigned int len,
749 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
753 for (i = 0; i < len * 4; i++) {
754 struct vgic_irq *irq;
757 * The configuration cannot be changed for SGIs in general,
758 * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
759 * code relies on PPIs being level triggered, so we also
760 * make them read-only here.
762 if (intid + i < VGIC_NR_PRIVATE_IRQS)
765 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
766 raw_spin_lock_irqsave(&irq->irq_lock, flags);
768 if (test_bit(i * 2 + 1, &val))
769 irq->config = VGIC_CONFIG_EDGE;
771 irq->config = VGIC_CONFIG_LEVEL;
773 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
774 vgic_put_irq(vcpu->kvm, irq);
778 u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
782 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
784 for (i = 0; i < 32; i++) {
785 struct vgic_irq *irq;
787 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
790 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
791 if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level)
794 vgic_put_irq(vcpu->kvm, irq);
800 void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
804 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
807 for (i = 0; i < 32; i++) {
808 struct vgic_irq *irq;
811 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
814 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
817 * Line level is set irrespective of irq type
818 * (level or edge) to avoid dependency that VM should
819 * restore irq config before line level.
821 new_level = !!(val & (1U << i));
822 raw_spin_lock_irqsave(&irq->irq_lock, flags);
823 irq->line_level = new_level;
825 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
827 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
829 vgic_put_irq(vcpu->kvm, irq);
833 static int match_region(const void *key, const void *elt)
835 const unsigned int offset = (unsigned long)key;
836 const struct vgic_register_region *region = elt;
838 if (offset < region->reg_offset)
841 if (offset >= region->reg_offset + region->len)
847 const struct vgic_register_region *
848 vgic_find_mmio_region(const struct vgic_register_region *regions,
849 int nr_regions, unsigned int offset)
851 return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
852 sizeof(regions[0]), match_region);
855 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
857 if (kvm_vgic_global_state.type == VGIC_V2)
858 vgic_v2_set_vmcr(vcpu, vmcr);
860 vgic_v3_set_vmcr(vcpu, vmcr);
863 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
865 if (kvm_vgic_global_state.type == VGIC_V2)
866 vgic_v2_get_vmcr(vcpu, vmcr);
868 vgic_v3_get_vmcr(vcpu, vmcr);
872 * kvm_mmio_read_buf() returns a value in a format where it can be converted
873 * to a byte array and be directly observed as the guest wanted it to appear
874 * in memory if it had done the store itself, which is LE for the GIC, as the
875 * guest knows the GIC is always LE.
877 * We convert this value to the CPUs native format to deal with it as a data
880 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
882 unsigned long data = kvm_mmio_read_buf(val, len);
888 return le16_to_cpu(data);
890 return le32_to_cpu(data);
892 return le64_to_cpu(data);
897 * kvm_mmio_write_buf() expects a value in a format such that if converted to
898 * a byte array it is observed as the guest would see it if it could perform
899 * the load directly. Since the GIC is LE, and the guest knows this, the
900 * guest expects a value in little endian format.
902 * We convert the data value from the CPUs native format to LE so that the
903 * value is returned in the proper format.
905 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
912 data = cpu_to_le16(data);
915 data = cpu_to_le32(data);
918 data = cpu_to_le64(data);
921 kvm_mmio_write_buf(buf, len, data);
925 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
927 return container_of(dev, struct vgic_io_device, dev);
930 static bool check_region(const struct kvm *kvm,
931 const struct vgic_register_region *region,
934 int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
938 flags = VGIC_ACCESS_8bit;
941 flags = VGIC_ACCESS_32bit;
944 flags = VGIC_ACCESS_64bit;
950 if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
951 if (!region->bits_per_irq)
954 /* Do we access a non-allocated IRQ? */
955 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
961 const struct vgic_register_region *
962 vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
965 const struct vgic_register_region *region;
967 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
968 addr - iodev->base_addr);
969 if (!region || !check_region(vcpu->kvm, region, addr, len))
975 static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
976 gpa_t addr, u32 *val)
978 const struct vgic_register_region *region;
979 struct kvm_vcpu *r_vcpu;
981 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
987 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
988 if (region->uaccess_read)
989 *val = region->uaccess_read(r_vcpu, addr, sizeof(u32));
991 *val = region->read(r_vcpu, addr, sizeof(u32));
996 static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
997 gpa_t addr, const u32 *val)
999 const struct vgic_register_region *region;
1000 struct kvm_vcpu *r_vcpu;
1002 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
1006 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
1007 if (region->uaccess_write)
1008 return region->uaccess_write(r_vcpu, addr, sizeof(u32), *val);
1010 region->write(r_vcpu, addr, sizeof(u32), *val);
1015 * Userland access to VGIC registers.
1017 int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
1018 bool is_write, int offset, u32 *val)
1021 return vgic_uaccess_write(vcpu, dev, offset, val);
1023 return vgic_uaccess_read(vcpu, dev, offset, val);
1026 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
1027 gpa_t addr, int len, void *val)
1029 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
1030 const struct vgic_register_region *region;
1031 unsigned long data = 0;
1033 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
1035 memset(val, 0, len);
1039 switch (iodev->iodev_type) {
1041 data = region->read(vcpu, addr, len);
1044 data = region->read(vcpu, addr, len);
1047 data = region->read(iodev->redist_vcpu, addr, len);
1050 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
1054 vgic_data_host_to_mmio_bus(val, len, data);
1058 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
1059 gpa_t addr, int len, const void *val)
1061 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
1062 const struct vgic_register_region *region;
1063 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
1065 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
1069 switch (iodev->iodev_type) {
1071 region->write(vcpu, addr, len, data);
1074 region->write(vcpu, addr, len, data);
1077 region->write(iodev->redist_vcpu, addr, len, data);
1080 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
1087 const struct kvm_io_device_ops kvm_io_gic_ops = {
1088 .read = dispatch_mmio_read,
1089 .write = dispatch_mmio_write,
1092 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
1093 enum vgic_type type)
1095 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
1101 len = vgic_v2_init_dist_iodev(io_device);
1104 len = vgic_v3_init_dist_iodev(io_device);
1110 io_device->base_addr = dist_base_address;
1111 io_device->iodev_type = IODEV_DIST;
1112 io_device->redist_vcpu = NULL;
1114 mutex_lock(&kvm->slots_lock);
1115 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
1116 len, &io_device->dev);
1117 mutex_unlock(&kvm->slots_lock);