2 * VGIC MMIO handling functions
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/bitops.h>
15 #include <linux/bsearch.h>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <kvm/iodev.h>
19 #include <kvm/arm_arch_timer.h>
20 #include <kvm/arm_vgic.h>
23 #include "vgic-mmio.h"
25 unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
26 gpa_t addr, unsigned int len)
31 unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
32 gpa_t addr, unsigned int len)
37 void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
38 unsigned int len, unsigned long val)
43 int vgic_mmio_uaccess_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
44 unsigned int len, unsigned long val)
50 unsigned long vgic_mmio_read_group(struct kvm_vcpu *vcpu,
51 gpa_t addr, unsigned int len)
53 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
57 /* Loop over all IRQs affected by this read */
58 for (i = 0; i < len * 8; i++) {
59 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
64 vgic_put_irq(vcpu->kvm, irq);
70 void vgic_mmio_write_group(struct kvm_vcpu *vcpu, gpa_t addr,
71 unsigned int len, unsigned long val)
73 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
77 for (i = 0; i < len * 8; i++) {
78 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
80 spin_lock_irqsave(&irq->irq_lock, flags);
81 irq->group = !!(val & BIT(i));
82 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
84 vgic_put_irq(vcpu->kvm, irq);
89 * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
90 * of the enabled bit, so there is only one function for both here.
92 unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
93 gpa_t addr, unsigned int len)
95 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
99 /* Loop over all IRQs affected by this read */
100 for (i = 0; i < len * 8; i++) {
101 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
106 vgic_put_irq(vcpu->kvm, irq);
112 void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
113 gpa_t addr, unsigned int len,
116 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
120 for_each_set_bit(i, &val, len * 8) {
121 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
123 spin_lock_irqsave(&irq->irq_lock, flags);
125 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
127 vgic_put_irq(vcpu->kvm, irq);
131 void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
132 gpa_t addr, unsigned int len,
135 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
139 for_each_set_bit(i, &val, len * 8) {
140 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
142 spin_lock_irqsave(&irq->irq_lock, flags);
144 irq->enabled = false;
146 spin_unlock_irqrestore(&irq->irq_lock, flags);
147 vgic_put_irq(vcpu->kvm, irq);
151 unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
152 gpa_t addr, unsigned int len)
154 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
158 /* Loop over all IRQs affected by this read */
159 for (i = 0; i < len * 8; i++) {
160 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
163 spin_lock_irqsave(&irq->irq_lock, flags);
164 if (irq_is_pending(irq))
166 spin_unlock_irqrestore(&irq->irq_lock, flags);
168 vgic_put_irq(vcpu->kvm, irq);
175 * This function will return the VCPU that performed the MMIO access and
176 * trapped from within the VM, and will return NULL if this is a userspace
179 * We can disable preemption locally around accessing the per-CPU variable,
180 * and use the resolved vcpu pointer after enabling preemption again, because
181 * even if the current thread is migrated to another CPU, reading the per-CPU
182 * value later will give us the same value as we update the per-CPU variable
183 * in the preempt notifier handlers.
185 static struct kvm_vcpu *vgic_get_mmio_requester_vcpu(void)
187 struct kvm_vcpu *vcpu;
190 vcpu = kvm_arm_get_running_vcpu();
195 /* Must be called with irq->irq_lock held */
196 static void vgic_hw_irq_spending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
202 irq->pending_latch = true;
203 vgic_irq_set_phys_active(irq, true);
206 static bool is_vgic_v2_sgi(struct kvm_vcpu *vcpu, struct vgic_irq *irq)
208 return (vgic_irq_is_sgi(irq->intid) &&
209 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2);
212 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
213 gpa_t addr, unsigned int len,
216 bool is_uaccess = !vgic_get_mmio_requester_vcpu();
217 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
221 for_each_set_bit(i, &val, len * 8) {
222 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
224 /* GICD_ISPENDR0 SGI bits are WI */
225 if (is_vgic_v2_sgi(vcpu, irq)) {
226 vgic_put_irq(vcpu->kvm, irq);
230 spin_lock_irqsave(&irq->irq_lock, flags);
232 vgic_hw_irq_spending(vcpu, irq, is_uaccess);
234 irq->pending_latch = true;
235 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
236 vgic_put_irq(vcpu->kvm, irq);
240 /* Must be called with irq->irq_lock held */
241 static void vgic_hw_irq_cpending(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
247 irq->pending_latch = false;
250 * We don't want the guest to effectively mask the physical
251 * interrupt by doing a write to SPENDR followed by a write to
252 * CPENDR for HW interrupts, so we clear the active state on
253 * the physical side if the virtual interrupt is not active.
254 * This may lead to taking an additional interrupt on the
255 * host, but that should not be a problem as the worst that
256 * can happen is an additional vgic injection. We also clear
257 * the pending state to maintain proper semantics for edge HW
260 vgic_irq_set_phys_pending(irq, false);
262 vgic_irq_set_phys_active(irq, false);
265 void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
266 gpa_t addr, unsigned int len,
269 bool is_uaccess = !vgic_get_mmio_requester_vcpu();
270 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
274 for_each_set_bit(i, &val, len * 8) {
275 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
277 /* GICD_ICPENDR0 SGI bits are WI */
278 if (is_vgic_v2_sgi(vcpu, irq)) {
279 vgic_put_irq(vcpu->kvm, irq);
283 spin_lock_irqsave(&irq->irq_lock, flags);
286 vgic_hw_irq_cpending(vcpu, irq, is_uaccess);
288 irq->pending_latch = false;
290 spin_unlock_irqrestore(&irq->irq_lock, flags);
291 vgic_put_irq(vcpu->kvm, irq);
295 unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,
296 gpa_t addr, unsigned int len)
298 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
302 /* Loop over all IRQs affected by this read */
303 for (i = 0; i < len * 8; i++) {
304 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
309 vgic_put_irq(vcpu->kvm, irq);
315 /* Must be called with irq->irq_lock held */
316 static void vgic_hw_irq_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
317 bool active, bool is_uaccess)
322 irq->active = active;
323 vgic_irq_set_phys_active(irq, active);
326 static void vgic_mmio_change_active(struct kvm_vcpu *vcpu, struct vgic_irq *irq,
330 struct kvm_vcpu *requester_vcpu = vgic_get_mmio_requester_vcpu();
332 spin_lock_irqsave(&irq->irq_lock, flags);
335 vgic_hw_irq_change_active(vcpu, irq, active, !requester_vcpu);
337 u32 model = vcpu->kvm->arch.vgic.vgic_model;
340 irq->active = active;
343 * The GICv2 architecture indicates that the source CPUID for
344 * an SGI should be provided during an EOI which implies that
345 * the active state is stored somewhere, but at the same time
346 * this state is not architecturally exposed anywhere and we
347 * have no way of knowing the right source.
349 * This may lead to a VCPU not being able to receive
350 * additional instances of a particular SGI after migration
351 * for a GICv2 VM on some GIC implementations. Oh well.
353 active_source = (requester_vcpu) ? requester_vcpu->vcpu_id : 0;
355 if (model == KVM_DEV_TYPE_ARM_VGIC_V2 &&
356 active && vgic_irq_is_sgi(irq->intid))
357 irq->active_source = active_source;
361 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
363 spin_unlock_irqrestore(&irq->irq_lock, flags);
367 * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
368 * is not queued on some running VCPU's LRs, because then the change to the
369 * active state can be overwritten when the VCPU's state is synced coming back
372 * For shared interrupts, we have to stop all the VCPUs because interrupts can
373 * be migrated while we don't hold the IRQ locks and we don't want to be
374 * chasing moving targets.
376 * For private interrupts we don't have to do anything because userspace
377 * accesses to the VGIC state already require all VCPUs to be stopped, and
378 * only the VCPU itself can modify its private interrupts active state, which
379 * guarantees that the VCPU is not running.
381 static void vgic_change_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
383 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
384 intid >= VGIC_NR_PRIVATE_IRQS)
385 kvm_arm_halt_guest(vcpu->kvm);
388 /* See vgic_change_active_prepare */
389 static void vgic_change_active_finish(struct kvm_vcpu *vcpu, u32 intid)
391 if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 ||
392 intid >= VGIC_NR_PRIVATE_IRQS)
393 kvm_arm_resume_guest(vcpu->kvm);
396 static void __vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
397 gpa_t addr, unsigned int len,
400 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
403 for_each_set_bit(i, &val, len * 8) {
404 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
405 vgic_mmio_change_active(vcpu, irq, false);
406 vgic_put_irq(vcpu->kvm, irq);
410 void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu,
411 gpa_t addr, unsigned int len,
414 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
416 mutex_lock(&vcpu->kvm->lock);
417 vgic_change_active_prepare(vcpu, intid);
419 __vgic_mmio_write_cactive(vcpu, addr, len, val);
421 vgic_change_active_finish(vcpu, intid);
422 mutex_unlock(&vcpu->kvm->lock);
425 int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
426 gpa_t addr, unsigned int len,
429 __vgic_mmio_write_cactive(vcpu, addr, len, val);
433 static void __vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
434 gpa_t addr, unsigned int len,
437 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
440 for_each_set_bit(i, &val, len * 8) {
441 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
442 vgic_mmio_change_active(vcpu, irq, true);
443 vgic_put_irq(vcpu->kvm, irq);
447 void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu,
448 gpa_t addr, unsigned int len,
451 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
453 mutex_lock(&vcpu->kvm->lock);
454 vgic_change_active_prepare(vcpu, intid);
456 __vgic_mmio_write_sactive(vcpu, addr, len, val);
458 vgic_change_active_finish(vcpu, intid);
459 mutex_unlock(&vcpu->kvm->lock);
462 int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
463 gpa_t addr, unsigned int len,
466 __vgic_mmio_write_sactive(vcpu, addr, len, val);
470 unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu,
471 gpa_t addr, unsigned int len)
473 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
477 for (i = 0; i < len; i++) {
478 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
480 val |= (u64)irq->priority << (i * 8);
482 vgic_put_irq(vcpu->kvm, irq);
489 * We currently don't handle changing the priority of an interrupt that
490 * is already pending on a VCPU. If there is a need for this, we would
491 * need to make this VCPU exit and re-evaluate the priorities, potentially
492 * leading to this interrupt getting presented now to the guest (if it has
493 * been masked by the priority mask before).
495 void vgic_mmio_write_priority(struct kvm_vcpu *vcpu,
496 gpa_t addr, unsigned int len,
499 u32 intid = VGIC_ADDR_TO_INTID(addr, 8);
503 for (i = 0; i < len; i++) {
504 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
506 spin_lock_irqsave(&irq->irq_lock, flags);
507 /* Narrow the priority range to what we actually support */
508 irq->priority = (val >> (i * 8)) & GENMASK(7, 8 - VGIC_PRI_BITS);
509 spin_unlock_irqrestore(&irq->irq_lock, flags);
511 vgic_put_irq(vcpu->kvm, irq);
515 unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu,
516 gpa_t addr, unsigned int len)
518 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
522 for (i = 0; i < len * 4; i++) {
523 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
525 if (irq->config == VGIC_CONFIG_EDGE)
526 value |= (2U << (i * 2));
528 vgic_put_irq(vcpu->kvm, irq);
534 void vgic_mmio_write_config(struct kvm_vcpu *vcpu,
535 gpa_t addr, unsigned int len,
538 u32 intid = VGIC_ADDR_TO_INTID(addr, 2);
542 for (i = 0; i < len * 4; i++) {
543 struct vgic_irq *irq;
546 * The configuration cannot be changed for SGIs in general,
547 * for PPIs this is IMPLEMENTATION DEFINED. The arch timer
548 * code relies on PPIs being level triggered, so we also
549 * make them read-only here.
551 if (intid + i < VGIC_NR_PRIVATE_IRQS)
554 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
555 spin_lock_irqsave(&irq->irq_lock, flags);
557 if (test_bit(i * 2 + 1, &val))
558 irq->config = VGIC_CONFIG_EDGE;
560 irq->config = VGIC_CONFIG_LEVEL;
562 spin_unlock_irqrestore(&irq->irq_lock, flags);
563 vgic_put_irq(vcpu->kvm, irq);
567 u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid)
571 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
573 for (i = 0; i < 32; i++) {
574 struct vgic_irq *irq;
576 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
579 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
580 if (irq->config == VGIC_CONFIG_LEVEL && irq->line_level)
583 vgic_put_irq(vcpu->kvm, irq);
589 void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid,
593 int nr_irqs = vcpu->kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
596 for (i = 0; i < 32; i++) {
597 struct vgic_irq *irq;
600 if ((intid + i) < VGIC_NR_SGIS || (intid + i) >= nr_irqs)
603 irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
606 * Line level is set irrespective of irq type
607 * (level or edge) to avoid dependency that VM should
608 * restore irq config before line level.
610 new_level = !!(val & (1U << i));
611 spin_lock_irqsave(&irq->irq_lock, flags);
612 irq->line_level = new_level;
614 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
616 spin_unlock_irqrestore(&irq->irq_lock, flags);
618 vgic_put_irq(vcpu->kvm, irq);
622 static int match_region(const void *key, const void *elt)
624 const unsigned int offset = (unsigned long)key;
625 const struct vgic_register_region *region = elt;
627 if (offset < region->reg_offset)
630 if (offset >= region->reg_offset + region->len)
636 const struct vgic_register_region *
637 vgic_find_mmio_region(const struct vgic_register_region *regions,
638 int nr_regions, unsigned int offset)
640 return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
641 sizeof(regions[0]), match_region);
644 void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
646 if (kvm_vgic_global_state.type == VGIC_V2)
647 vgic_v2_set_vmcr(vcpu, vmcr);
649 vgic_v3_set_vmcr(vcpu, vmcr);
652 void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr)
654 if (kvm_vgic_global_state.type == VGIC_V2)
655 vgic_v2_get_vmcr(vcpu, vmcr);
657 vgic_v3_get_vmcr(vcpu, vmcr);
661 * kvm_mmio_read_buf() returns a value in a format where it can be converted
662 * to a byte array and be directly observed as the guest wanted it to appear
663 * in memory if it had done the store itself, which is LE for the GIC, as the
664 * guest knows the GIC is always LE.
666 * We convert this value to the CPUs native format to deal with it as a data
669 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
671 unsigned long data = kvm_mmio_read_buf(val, len);
677 return le16_to_cpu(data);
679 return le32_to_cpu(data);
681 return le64_to_cpu(data);
686 * kvm_mmio_write_buf() expects a value in a format such that if converted to
687 * a byte array it is observed as the guest would see it if it could perform
688 * the load directly. Since the GIC is LE, and the guest knows this, the
689 * guest expects a value in little endian format.
691 * We convert the data value from the CPUs native format to LE so that the
692 * value is returned in the proper format.
694 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
701 data = cpu_to_le16(data);
704 data = cpu_to_le32(data);
707 data = cpu_to_le64(data);
710 kvm_mmio_write_buf(buf, len, data);
714 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
716 return container_of(dev, struct vgic_io_device, dev);
719 static bool check_region(const struct kvm *kvm,
720 const struct vgic_register_region *region,
723 int flags, nr_irqs = kvm->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
727 flags = VGIC_ACCESS_8bit;
730 flags = VGIC_ACCESS_32bit;
733 flags = VGIC_ACCESS_64bit;
739 if ((region->access_flags & flags) && IS_ALIGNED(addr, len)) {
740 if (!region->bits_per_irq)
743 /* Do we access a non-allocated IRQ? */
744 return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
750 const struct vgic_register_region *
751 vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
754 const struct vgic_register_region *region;
756 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
757 addr - iodev->base_addr);
758 if (!region || !check_region(vcpu->kvm, region, addr, len))
764 static int vgic_uaccess_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
765 gpa_t addr, u32 *val)
767 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
768 const struct vgic_register_region *region;
769 struct kvm_vcpu *r_vcpu;
771 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
777 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
778 if (region->uaccess_read)
779 *val = region->uaccess_read(r_vcpu, addr, sizeof(u32));
781 *val = region->read(r_vcpu, addr, sizeof(u32));
786 static int vgic_uaccess_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
787 gpa_t addr, const u32 *val)
789 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
790 const struct vgic_register_region *region;
791 struct kvm_vcpu *r_vcpu;
793 region = vgic_get_mmio_region(vcpu, iodev, addr, sizeof(u32));
797 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
798 if (region->uaccess_write)
799 return region->uaccess_write(r_vcpu, addr, sizeof(u32), *val);
801 region->write(r_vcpu, addr, sizeof(u32), *val);
806 * Userland access to VGIC registers.
808 int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev,
809 bool is_write, int offset, u32 *val)
812 return vgic_uaccess_write(vcpu, &dev->dev, offset, val);
814 return vgic_uaccess_read(vcpu, &dev->dev, offset, val);
817 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
818 gpa_t addr, int len, void *val)
820 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
821 const struct vgic_register_region *region;
822 unsigned long data = 0;
824 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
830 switch (iodev->iodev_type) {
832 data = region->read(vcpu, addr, len);
835 data = region->read(vcpu, addr, len);
838 data = region->read(iodev->redist_vcpu, addr, len);
841 data = region->its_read(vcpu->kvm, iodev->its, addr, len);
845 vgic_data_host_to_mmio_bus(val, len, data);
849 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
850 gpa_t addr, int len, const void *val)
852 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
853 const struct vgic_register_region *region;
854 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
856 region = vgic_get_mmio_region(vcpu, iodev, addr, len);
860 switch (iodev->iodev_type) {
862 region->write(vcpu, addr, len, data);
865 region->write(vcpu, addr, len, data);
868 region->write(iodev->redist_vcpu, addr, len, data);
871 region->its_write(vcpu->kvm, iodev->its, addr, len, data);
878 struct kvm_io_device_ops kvm_io_gic_ops = {
879 .read = dispatch_mmio_read,
880 .write = dispatch_mmio_write,
883 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
886 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
892 len = vgic_v2_init_dist_iodev(io_device);
895 len = vgic_v3_init_dist_iodev(io_device);
901 io_device->base_addr = dist_base_address;
902 io_device->iodev_type = IODEV_DIST;
903 io_device->redist_vcpu = NULL;
905 mutex_lock(&kvm->slots_lock);
906 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
907 len, &io_device->dev);
908 mutex_unlock(&kvm->slots_lock);