xref: /openbmc/linux/arch/arm64/kvm/vgic/vgic.h (revision 9d7629bec5c3f80bd0e3bf8103c06a2f7046bd92)
19ed24f4bSMarc Zyngier /* SPDX-License-Identifier: GPL-2.0-only */
29ed24f4bSMarc Zyngier /*
39ed24f4bSMarc Zyngier  * Copyright (C) 2015, 2016 ARM Ltd.
49ed24f4bSMarc Zyngier  */
59ed24f4bSMarc Zyngier #ifndef __KVM_ARM_VGIC_NEW_H__
69ed24f4bSMarc Zyngier #define __KVM_ARM_VGIC_NEW_H__
79ed24f4bSMarc Zyngier 
89ed24f4bSMarc Zyngier #include <linux/irqchip/arm-gic-common.h>
9a23eaf93SGavin Shan #include <asm/kvm_mmu.h>
109ed24f4bSMarc Zyngier 
119ed24f4bSMarc Zyngier #define PRODUCT_ID_KVM		0x4b	/* ASCII code K */
129ed24f4bSMarc Zyngier #define IMPLEMENTER_ARM		0x43b
139ed24f4bSMarc Zyngier 
149ed24f4bSMarc Zyngier #define VGIC_ADDR_UNDEF		(-1)
159ed24f4bSMarc Zyngier #define IS_VGIC_ADDR_UNDEF(_x)  ((_x) == VGIC_ADDR_UNDEF)
169ed24f4bSMarc Zyngier 
179ed24f4bSMarc Zyngier #define INTERRUPT_ID_BITS_SPIS	10
189ed24f4bSMarc Zyngier #define INTERRUPT_ID_BITS_ITS	16
199ed24f4bSMarc Zyngier #define VGIC_PRI_BITS		5
209ed24f4bSMarc Zyngier 
219ed24f4bSMarc Zyngier #define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
229ed24f4bSMarc Zyngier 
239ed24f4bSMarc Zyngier #define VGIC_AFFINITY_0_SHIFT 0
249ed24f4bSMarc Zyngier #define VGIC_AFFINITY_0_MASK (0xffUL << VGIC_AFFINITY_0_SHIFT)
259ed24f4bSMarc Zyngier #define VGIC_AFFINITY_1_SHIFT 8
269ed24f4bSMarc Zyngier #define VGIC_AFFINITY_1_MASK (0xffUL << VGIC_AFFINITY_1_SHIFT)
279ed24f4bSMarc Zyngier #define VGIC_AFFINITY_2_SHIFT 16
289ed24f4bSMarc Zyngier #define VGIC_AFFINITY_2_MASK (0xffUL << VGIC_AFFINITY_2_SHIFT)
299ed24f4bSMarc Zyngier #define VGIC_AFFINITY_3_SHIFT 24
309ed24f4bSMarc Zyngier #define VGIC_AFFINITY_3_MASK (0xffUL << VGIC_AFFINITY_3_SHIFT)
319ed24f4bSMarc Zyngier 
329ed24f4bSMarc Zyngier #define VGIC_AFFINITY_LEVEL(reg, level) \
339ed24f4bSMarc Zyngier 	((((reg) & VGIC_AFFINITY_## level ##_MASK) \
349ed24f4bSMarc Zyngier 	>> VGIC_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level))
359ed24f4bSMarc Zyngier 
369ed24f4bSMarc Zyngier /*
379ed24f4bSMarc Zyngier  * The Userspace encodes the affinity differently from the MPIDR,
389ed24f4bSMarc Zyngier  * Below macro converts vgic userspace format to MPIDR reg format.
399ed24f4bSMarc Zyngier  */
409ed24f4bSMarc Zyngier #define VGIC_TO_MPIDR(val) (VGIC_AFFINITY_LEVEL(val, 0) | \
419ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 1) | \
429ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 2) | \
439ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 3))
449ed24f4bSMarc Zyngier 
459ed24f4bSMarc Zyngier /*
46039aeb9dSLinus Torvalds  * As per Documentation/virt/kvm/devices/arm-vgic-v3.rst,
479ed24f4bSMarc Zyngier  * below macros are defined for CPUREG encoding.
489ed24f4bSMarc Zyngier  */
499ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP0_MASK   0x000000000000c000
509ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP0_SHIFT  14
519ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP1_MASK   0x0000000000003800
529ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP1_SHIFT  11
539ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRN_MASK   0x0000000000000780
549ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRN_SHIFT  7
559ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRM_MASK   0x0000000000000078
569ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRM_SHIFT  3
579ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP2_MASK   0x0000000000000007
589ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP2_SHIFT  0
599ed24f4bSMarc Zyngier 
609ed24f4bSMarc Zyngier #define KVM_DEV_ARM_VGIC_SYSREG_MASK (KVM_REG_ARM_VGIC_SYSREG_OP0_MASK | \
619ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_OP1_MASK | \
629ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_CRN_MASK | \
639ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \
649ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_OP2_MASK)
659ed24f4bSMarc Zyngier 
669ed24f4bSMarc Zyngier /*
67039aeb9dSLinus Torvalds  * As per Documentation/virt/kvm/devices/arm-vgic-its.rst,
689ed24f4bSMarc Zyngier  * below macros are defined for ITS table entry encoding.
699ed24f4bSMarc Zyngier  */
709ed24f4bSMarc Zyngier #define KVM_ITS_CTE_VALID_SHIFT		63
719ed24f4bSMarc Zyngier #define KVM_ITS_CTE_VALID_MASK		BIT_ULL(63)
729ed24f4bSMarc Zyngier #define KVM_ITS_CTE_RDBASE_SHIFT	16
739ed24f4bSMarc Zyngier #define KVM_ITS_CTE_ICID_MASK		GENMASK_ULL(15, 0)
749ed24f4bSMarc Zyngier #define KVM_ITS_ITE_NEXT_SHIFT		48
759ed24f4bSMarc Zyngier #define KVM_ITS_ITE_PINTID_SHIFT	16
769ed24f4bSMarc Zyngier #define KVM_ITS_ITE_PINTID_MASK		GENMASK_ULL(47, 16)
779ed24f4bSMarc Zyngier #define KVM_ITS_ITE_ICID_MASK		GENMASK_ULL(15, 0)
789ed24f4bSMarc Zyngier #define KVM_ITS_DTE_VALID_SHIFT		63
799ed24f4bSMarc Zyngier #define KVM_ITS_DTE_VALID_MASK		BIT_ULL(63)
809ed24f4bSMarc Zyngier #define KVM_ITS_DTE_NEXT_SHIFT		49
819ed24f4bSMarc Zyngier #define KVM_ITS_DTE_NEXT_MASK		GENMASK_ULL(62, 49)
829ed24f4bSMarc Zyngier #define KVM_ITS_DTE_ITTADDR_SHIFT	5
839ed24f4bSMarc Zyngier #define KVM_ITS_DTE_ITTADDR_MASK	GENMASK_ULL(48, 5)
849ed24f4bSMarc Zyngier #define KVM_ITS_DTE_SIZE_MASK		GENMASK_ULL(4, 0)
859ed24f4bSMarc Zyngier #define KVM_ITS_L1E_VALID_MASK		BIT_ULL(63)
869ed24f4bSMarc Zyngier /* we only support 64 kB translation table page size */
879ed24f4bSMarc Zyngier #define KVM_ITS_L1E_ADDR_MASK		GENMASK_ULL(51, 16)
889ed24f4bSMarc Zyngier 
899ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_INDEX_MASK	GENMASK_ULL(11, 0)
909ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_FLAGS_MASK	GENMASK_ULL(15, 12)
919ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_FLAGS_SHIFT	12
929ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_BASE_MASK	GENMASK_ULL(51, 16)
939ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_COUNT_MASK	GENMASK_ULL(63, 52)
949ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_COUNT_SHIFT	52
959ed24f4bSMarc Zyngier 
969ed24f4bSMarc Zyngier #ifdef CONFIG_DEBUG_SPINLOCK
979ed24f4bSMarc Zyngier #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
989ed24f4bSMarc Zyngier #else
999ed24f4bSMarc Zyngier #define DEBUG_SPINLOCK_BUG_ON(p)
1009ed24f4bSMarc Zyngier #endif
1019ed24f4bSMarc Zyngier 
10249a1a2c7SMarc Zyngier static inline u32 vgic_get_implementation_rev(struct kvm_vcpu *vcpu)
10349a1a2c7SMarc Zyngier {
10449a1a2c7SMarc Zyngier 	return vcpu->kvm->arch.vgic.implementation_rev;
10549a1a2c7SMarc Zyngier }
10649a1a2c7SMarc Zyngier 
1079ed24f4bSMarc Zyngier /* Requires the irq_lock to be held by the caller. */
1089ed24f4bSMarc Zyngier static inline bool irq_is_pending(struct vgic_irq *irq)
1099ed24f4bSMarc Zyngier {
1109ed24f4bSMarc Zyngier 	if (irq->config == VGIC_CONFIG_EDGE)
1119ed24f4bSMarc Zyngier 		return irq->pending_latch;
1129ed24f4bSMarc Zyngier 	else
1139ed24f4bSMarc Zyngier 		return irq->pending_latch || irq->line_level;
1149ed24f4bSMarc Zyngier }
1159ed24f4bSMarc Zyngier 
1169ed24f4bSMarc Zyngier static inline bool vgic_irq_is_mapped_level(struct vgic_irq *irq)
1179ed24f4bSMarc Zyngier {
1189ed24f4bSMarc Zyngier 	return irq->config == VGIC_CONFIG_LEVEL && irq->hw;
1199ed24f4bSMarc Zyngier }
1209ed24f4bSMarc Zyngier 
1219ed24f4bSMarc Zyngier static inline int vgic_irq_get_lr_count(struct vgic_irq *irq)
1229ed24f4bSMarc Zyngier {
1239ed24f4bSMarc Zyngier 	/* Account for the active state as an interrupt */
1249ed24f4bSMarc Zyngier 	if (vgic_irq_is_sgi(irq->intid) && irq->source)
1259ed24f4bSMarc Zyngier 		return hweight8(irq->source) + irq->active;
1269ed24f4bSMarc Zyngier 
1279ed24f4bSMarc Zyngier 	return irq_is_pending(irq) || irq->active;
1289ed24f4bSMarc Zyngier }
1299ed24f4bSMarc Zyngier 
1309ed24f4bSMarc Zyngier static inline bool vgic_irq_is_multi_sgi(struct vgic_irq *irq)
1319ed24f4bSMarc Zyngier {
1329ed24f4bSMarc Zyngier 	return vgic_irq_get_lr_count(irq) > 1;
1339ed24f4bSMarc Zyngier }
1349ed24f4bSMarc Zyngier 
135a23eaf93SGavin Shan static inline int vgic_write_guest_lock(struct kvm *kvm, gpa_t gpa,
136a23eaf93SGavin Shan 					const void *data, unsigned long len)
137a23eaf93SGavin Shan {
138a23eaf93SGavin Shan 	struct vgic_dist *dist = &kvm->arch.vgic;
139a23eaf93SGavin Shan 	int ret;
140a23eaf93SGavin Shan 
141a23eaf93SGavin Shan 	dist->table_write_in_progress = true;
142a23eaf93SGavin Shan 	ret = kvm_write_guest_lock(kvm, gpa, data, len);
143a23eaf93SGavin Shan 	dist->table_write_in_progress = false;
144a23eaf93SGavin Shan 
145a23eaf93SGavin Shan 	return ret;
146a23eaf93SGavin Shan }
147a23eaf93SGavin Shan 
1489ed24f4bSMarc Zyngier /*
1499ed24f4bSMarc Zyngier  * This struct provides an intermediate representation of the fields contained
1509ed24f4bSMarc Zyngier  * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
1519ed24f4bSMarc Zyngier  * state to userspace can generate either GICv2 or GICv3 CPU interface
1529ed24f4bSMarc Zyngier  * registers regardless of the hardware backed GIC used.
1539ed24f4bSMarc Zyngier  */
1549ed24f4bSMarc Zyngier struct vgic_vmcr {
1559ed24f4bSMarc Zyngier 	u32	grpen0;
1569ed24f4bSMarc Zyngier 	u32	grpen1;
1579ed24f4bSMarc Zyngier 
1589ed24f4bSMarc Zyngier 	u32	ackctl;
1599ed24f4bSMarc Zyngier 	u32	fiqen;
1609ed24f4bSMarc Zyngier 	u32	cbpr;
1619ed24f4bSMarc Zyngier 	u32	eoim;
1629ed24f4bSMarc Zyngier 
1639ed24f4bSMarc Zyngier 	u32	abpr;
1649ed24f4bSMarc Zyngier 	u32	bpr;
1659ed24f4bSMarc Zyngier 	u32	pmr;  /* Priority mask field in the GICC_PMR and
1669ed24f4bSMarc Zyngier 		       * ICC_PMR_EL1 priority field format */
1679ed24f4bSMarc Zyngier };
1689ed24f4bSMarc Zyngier 
1699ed24f4bSMarc Zyngier struct vgic_reg_attr {
1709ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
1719ed24f4bSMarc Zyngier 	gpa_t addr;
1729ed24f4bSMarc Zyngier };
1739ed24f4bSMarc Zyngier 
1749ed24f4bSMarc Zyngier int vgic_v3_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
1759ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
1769ed24f4bSMarc Zyngier int vgic_v2_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
1779ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
1789ed24f4bSMarc Zyngier const struct vgic_register_region *
1799ed24f4bSMarc Zyngier vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
1809ed24f4bSMarc Zyngier 		     gpa_t addr, int len);
1819ed24f4bSMarc Zyngier struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
1829ed24f4bSMarc Zyngier 			      u32 intid);
1839ed24f4bSMarc Zyngier void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq);
1849ed24f4bSMarc Zyngier void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
1859ed24f4bSMarc Zyngier bool vgic_get_phys_line_level(struct vgic_irq *irq);
1869ed24f4bSMarc Zyngier void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending);
1879ed24f4bSMarc Zyngier void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active);
1889ed24f4bSMarc Zyngier bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
1899ed24f4bSMarc Zyngier 			   unsigned long flags);
1909ed24f4bSMarc Zyngier void vgic_kick_vcpus(struct kvm *kvm);
1913134cc8bSMarc Zyngier void vgic_irq_handle_resampling(struct vgic_irq *irq,
1923134cc8bSMarc Zyngier 				bool lr_deactivated, bool lr_pending);
1939ed24f4bSMarc Zyngier 
194f25c5e4dSRicardo Koller int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
195f25c5e4dSRicardo Koller 		       phys_addr_t addr, phys_addr_t alignment,
196f25c5e4dSRicardo Koller 		       phys_addr_t size);
197f25c5e4dSRicardo Koller 
1989ed24f4bSMarc Zyngier void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu);
1999ed24f4bSMarc Zyngier void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
2009ed24f4bSMarc Zyngier void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr);
2019ed24f4bSMarc Zyngier void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);
2029ed24f4bSMarc Zyngier int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
2039ed24f4bSMarc Zyngier int vgic_v2_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2049ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2059ed24f4bSMarc Zyngier int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2069ed24f4bSMarc Zyngier 			  int offset, u32 *val);
2079ed24f4bSMarc Zyngier void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2089ed24f4bSMarc Zyngier void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2099ed24f4bSMarc Zyngier void vgic_v2_enable(struct kvm_vcpu *vcpu);
2109ed24f4bSMarc Zyngier int vgic_v2_probe(const struct gic_kvm_info *info);
2119ed24f4bSMarc Zyngier int vgic_v2_map_resources(struct kvm *kvm);
2129ed24f4bSMarc Zyngier int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
2139ed24f4bSMarc Zyngier 			     enum vgic_type);
2149ed24f4bSMarc Zyngier 
2159ed24f4bSMarc Zyngier void vgic_v2_init_lrs(void);
2169ed24f4bSMarc Zyngier void vgic_v2_load(struct kvm_vcpu *vcpu);
2179ed24f4bSMarc Zyngier void vgic_v2_put(struct kvm_vcpu *vcpu);
2189ed24f4bSMarc Zyngier void vgic_v2_vmcr_sync(struct kvm_vcpu *vcpu);
2199ed24f4bSMarc Zyngier 
2209ed24f4bSMarc Zyngier void vgic_v2_save_state(struct kvm_vcpu *vcpu);
2219ed24f4bSMarc Zyngier void vgic_v2_restore_state(struct kvm_vcpu *vcpu);
2229ed24f4bSMarc Zyngier 
2239ed24f4bSMarc Zyngier static inline void vgic_get_irq_kref(struct vgic_irq *irq)
2249ed24f4bSMarc Zyngier {
2259ed24f4bSMarc Zyngier 	if (irq->intid < VGIC_MIN_LPI)
2269ed24f4bSMarc Zyngier 		return;
2279ed24f4bSMarc Zyngier 
2289ed24f4bSMarc Zyngier 	kref_get(&irq->refcount);
2299ed24f4bSMarc Zyngier }
2309ed24f4bSMarc Zyngier 
2319ed24f4bSMarc Zyngier void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);
2329ed24f4bSMarc Zyngier void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
2339ed24f4bSMarc Zyngier void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);
2349ed24f4bSMarc Zyngier void vgic_v3_set_underflow(struct kvm_vcpu *vcpu);
2359ed24f4bSMarc Zyngier void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2369ed24f4bSMarc Zyngier void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2379ed24f4bSMarc Zyngier void vgic_v3_enable(struct kvm_vcpu *vcpu);
2389ed24f4bSMarc Zyngier int vgic_v3_probe(const struct gic_kvm_info *info);
2399ed24f4bSMarc Zyngier int vgic_v3_map_resources(struct kvm *kvm);
2409ed24f4bSMarc Zyngier int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq);
2419ed24f4bSMarc Zyngier int vgic_v3_save_pending_tables(struct kvm *kvm);
2429ed24f4bSMarc Zyngier int vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count);
2439ed24f4bSMarc Zyngier int vgic_register_redist_iodev(struct kvm_vcpu *vcpu);
244e13ce009SMarc Zyngier void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu);
2459ed24f4bSMarc Zyngier bool vgic_v3_check_base(struct kvm *kvm);
2469ed24f4bSMarc Zyngier 
2479ed24f4bSMarc Zyngier void vgic_v3_load(struct kvm_vcpu *vcpu);
2489ed24f4bSMarc Zyngier void vgic_v3_put(struct kvm_vcpu *vcpu);
2499ed24f4bSMarc Zyngier void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu);
2509ed24f4bSMarc Zyngier 
2519ed24f4bSMarc Zyngier bool vgic_has_its(struct kvm *kvm);
2529ed24f4bSMarc Zyngier int kvm_vgic_register_its_device(void);
2539ed24f4bSMarc Zyngier void vgic_enable_lpis(struct kvm_vcpu *vcpu);
2549ed24f4bSMarc Zyngier void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu);
2559ed24f4bSMarc Zyngier int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi);
2569ed24f4bSMarc Zyngier int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
2579ed24f4bSMarc Zyngier int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2589ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2599ed24f4bSMarc Zyngier int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2609ed24f4bSMarc Zyngier 			 int offset, u32 *val);
261db25081eSMarc Zyngier int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu,
262db25081eSMarc Zyngier 				struct kvm_device_attr *attr, bool is_write);
263b61fc085SMarc Zyngier int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
2649ed24f4bSMarc Zyngier int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
26538cf0bb7SMarc Zyngier 				    u32 intid, u32 *val);
2669ed24f4bSMarc Zyngier int kvm_register_vgic_device(unsigned long type);
2679ed24f4bSMarc Zyngier void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2689ed24f4bSMarc Zyngier void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2699ed24f4bSMarc Zyngier int vgic_lazy_init(struct kvm *kvm);
2709ed24f4bSMarc Zyngier int vgic_init(struct kvm *kvm);
2719ed24f4bSMarc Zyngier 
2729ed24f4bSMarc Zyngier void vgic_debug_init(struct kvm *kvm);
2739ed24f4bSMarc Zyngier void vgic_debug_destroy(struct kvm *kvm);
2749ed24f4bSMarc Zyngier 
2759ed24f4bSMarc Zyngier static inline int vgic_v3_max_apr_idx(struct kvm_vcpu *vcpu)
2769ed24f4bSMarc Zyngier {
2779ed24f4bSMarc Zyngier 	struct vgic_cpu *cpu_if = &vcpu->arch.vgic_cpu;
2789ed24f4bSMarc Zyngier 
2799ed24f4bSMarc Zyngier 	/*
2809ed24f4bSMarc Zyngier 	 * num_pri_bits are initialized with HW supported values.
2819ed24f4bSMarc Zyngier 	 * We can rely safely on num_pri_bits even if VM has not
2829ed24f4bSMarc Zyngier 	 * restored ICC_CTLR_EL1 before restoring APnR registers.
2839ed24f4bSMarc Zyngier 	 */
2849ed24f4bSMarc Zyngier 	switch (cpu_if->num_pri_bits) {
2859ed24f4bSMarc Zyngier 	case 7: return 3;
2869ed24f4bSMarc Zyngier 	case 6: return 1;
2879ed24f4bSMarc Zyngier 	default: return 0;
2889ed24f4bSMarc Zyngier 	}
2899ed24f4bSMarc Zyngier }
2909ed24f4bSMarc Zyngier 
2919ed24f4bSMarc Zyngier static inline bool
2929ed24f4bSMarc Zyngier vgic_v3_redist_region_full(struct vgic_redist_region *region)
2939ed24f4bSMarc Zyngier {
2949ed24f4bSMarc Zyngier 	if (!region->count)
2959ed24f4bSMarc Zyngier 		return false;
2969ed24f4bSMarc Zyngier 
2979ed24f4bSMarc Zyngier 	return (region->free_index >= region->count);
2989ed24f4bSMarc Zyngier }
2999ed24f4bSMarc Zyngier 
3009ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rdregs);
3019ed24f4bSMarc Zyngier 
3029ed24f4bSMarc Zyngier static inline size_t
3039ed24f4bSMarc Zyngier vgic_v3_rd_region_size(struct kvm *kvm, struct vgic_redist_region *rdreg)
3049ed24f4bSMarc Zyngier {
3059ed24f4bSMarc Zyngier 	if (!rdreg->count)
3069ed24f4bSMarc Zyngier 		return atomic_read(&kvm->online_vcpus) * KVM_VGIC_V3_REDIST_SIZE;
3079ed24f4bSMarc Zyngier 	else
3089ed24f4bSMarc Zyngier 		return rdreg->count * KVM_VGIC_V3_REDIST_SIZE;
3099ed24f4bSMarc Zyngier }
3109ed24f4bSMarc Zyngier 
3119ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm,
3129ed24f4bSMarc Zyngier 							   u32 index);
31348bb6285SMarc Zyngier void vgic_v3_free_redist_region(struct kvm *kvm, struct vgic_redist_region *rdreg);
3149ed24f4bSMarc Zyngier 
3159ed24f4bSMarc Zyngier bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size);
3169ed24f4bSMarc Zyngier 
3179ed24f4bSMarc Zyngier static inline bool vgic_dist_overlap(struct kvm *kvm, gpa_t base, size_t size)
3189ed24f4bSMarc Zyngier {
3199ed24f4bSMarc Zyngier 	struct vgic_dist *d = &kvm->arch.vgic;
3209ed24f4bSMarc Zyngier 
3219ed24f4bSMarc Zyngier 	return (base + size > d->vgic_dist_base) &&
3229ed24f4bSMarc Zyngier 		(base < d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE);
3239ed24f4bSMarc Zyngier }
3249ed24f4bSMarc Zyngier 
32594828468SMarc Zyngier bool vgic_lpis_enabled(struct kvm_vcpu *vcpu);
3269ed24f4bSMarc Zyngier int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr);
3279ed24f4bSMarc Zyngier int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
3289ed24f4bSMarc Zyngier 			 u32 devid, u32 eventid, struct vgic_irq **irq);
3299ed24f4bSMarc Zyngier struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi);
3309ed24f4bSMarc Zyngier int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi);
3319ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_init(struct kvm *kvm);
3329ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_destroy(struct kvm *kvm);
3339ed24f4bSMarc Zyngier void vgic_its_invalidate_cache(struct kvm *kvm);
3349ed24f4bSMarc Zyngier 
3354645d11fSMarc Zyngier /* GICv4.1 MMIO interface */
3364645d11fSMarc Zyngier int vgic_its_inv_lpi(struct kvm *kvm, struct vgic_irq *irq);
3374645d11fSMarc Zyngier int vgic_its_invall(struct kvm_vcpu *vcpu);
3384645d11fSMarc Zyngier 
3399ed24f4bSMarc Zyngier bool vgic_supports_direct_msis(struct kvm *kvm);
3409ed24f4bSMarc Zyngier int vgic_v4_init(struct kvm *kvm);
3419ed24f4bSMarc Zyngier void vgic_v4_teardown(struct kvm *kvm);
3429ed24f4bSMarc Zyngier void vgic_v4_configure_vsgis(struct kvm *kvm);
34380317fe4SShenming Lu void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val);
344ef369168SMarc Zyngier int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq);
3459ed24f4bSMarc Zyngier 
346*9d7629beSMarc Zyngier static inline bool kvm_has_gicv3(struct kvm *kvm)
347*9d7629beSMarc Zyngier {
348*9d7629beSMarc Zyngier 	return (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) &&
349*9d7629beSMarc Zyngier 		irqchip_in_kernel(kvm) &&
350*9d7629beSMarc Zyngier 		kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3);
351*9d7629beSMarc Zyngier }
352*9d7629beSMarc Zyngier 
3539ed24f4bSMarc Zyngier #endif
354