xref: /openbmc/linux/arch/arm64/kvm/vgic/vgic.h (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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 
vgic_get_implementation_rev(struct kvm_vcpu * vcpu)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. */
irq_is_pending(struct vgic_irq * irq)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 
vgic_irq_is_mapped_level(struct vgic_irq * irq)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 
vgic_irq_get_lr_count(struct vgic_irq * irq)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 
vgic_irq_is_multi_sgi(struct vgic_irq * irq)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 
vgic_write_guest_lock(struct kvm * kvm,gpa_t gpa,const void * data,unsigned long len)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 
vgic_its_read_entry_lock(struct vgic_its * its,gpa_t eaddr,u64 * eval,unsigned long esize)148*2a4ec241SJing Zhang static inline int vgic_its_read_entry_lock(struct vgic_its *its, gpa_t eaddr,
149*2a4ec241SJing Zhang 					   u64 *eval, unsigned long esize)
150*2a4ec241SJing Zhang {
151*2a4ec241SJing Zhang 	struct kvm *kvm = its->dev->kvm;
152*2a4ec241SJing Zhang 
153*2a4ec241SJing Zhang 	if (KVM_BUG_ON(esize != sizeof(*eval), kvm))
154*2a4ec241SJing Zhang 		return -EINVAL;
155*2a4ec241SJing Zhang 
156*2a4ec241SJing Zhang 	return kvm_read_guest_lock(kvm, eaddr, eval, esize);
157*2a4ec241SJing Zhang 
158*2a4ec241SJing Zhang }
159*2a4ec241SJing Zhang 
vgic_its_write_entry_lock(struct vgic_its * its,gpa_t eaddr,u64 eval,unsigned long esize)160*2a4ec241SJing Zhang static inline int vgic_its_write_entry_lock(struct vgic_its *its, gpa_t eaddr,
161*2a4ec241SJing Zhang 					    u64 eval, unsigned long esize)
162*2a4ec241SJing Zhang {
163*2a4ec241SJing Zhang 	struct kvm *kvm = its->dev->kvm;
164*2a4ec241SJing Zhang 
165*2a4ec241SJing Zhang 	if (KVM_BUG_ON(esize != sizeof(eval), kvm))
166*2a4ec241SJing Zhang 		return -EINVAL;
167*2a4ec241SJing Zhang 
168*2a4ec241SJing Zhang 	return vgic_write_guest_lock(kvm, eaddr, &eval, esize);
169*2a4ec241SJing Zhang }
170*2a4ec241SJing Zhang 
1719ed24f4bSMarc Zyngier /*
1729ed24f4bSMarc Zyngier  * This struct provides an intermediate representation of the fields contained
1739ed24f4bSMarc Zyngier  * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
1749ed24f4bSMarc Zyngier  * state to userspace can generate either GICv2 or GICv3 CPU interface
1759ed24f4bSMarc Zyngier  * registers regardless of the hardware backed GIC used.
1769ed24f4bSMarc Zyngier  */
1779ed24f4bSMarc Zyngier struct vgic_vmcr {
1789ed24f4bSMarc Zyngier 	u32	grpen0;
1799ed24f4bSMarc Zyngier 	u32	grpen1;
1809ed24f4bSMarc Zyngier 
1819ed24f4bSMarc Zyngier 	u32	ackctl;
1829ed24f4bSMarc Zyngier 	u32	fiqen;
1839ed24f4bSMarc Zyngier 	u32	cbpr;
1849ed24f4bSMarc Zyngier 	u32	eoim;
1859ed24f4bSMarc Zyngier 
1869ed24f4bSMarc Zyngier 	u32	abpr;
1879ed24f4bSMarc Zyngier 	u32	bpr;
1889ed24f4bSMarc Zyngier 	u32	pmr;  /* Priority mask field in the GICC_PMR and
1899ed24f4bSMarc Zyngier 		       * ICC_PMR_EL1 priority field format */
1909ed24f4bSMarc Zyngier };
1919ed24f4bSMarc Zyngier 
1929ed24f4bSMarc Zyngier struct vgic_reg_attr {
1939ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
1949ed24f4bSMarc Zyngier 	gpa_t addr;
1959ed24f4bSMarc Zyngier };
1969ed24f4bSMarc Zyngier 
1979ed24f4bSMarc Zyngier int vgic_v3_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
1989ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
1999ed24f4bSMarc Zyngier int vgic_v2_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
2009ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
2019ed24f4bSMarc Zyngier const struct vgic_register_region *
2029ed24f4bSMarc Zyngier vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
2039ed24f4bSMarc Zyngier 		     gpa_t addr, int len);
2049ed24f4bSMarc Zyngier struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
2059ed24f4bSMarc Zyngier 			      u32 intid);
2069ed24f4bSMarc Zyngier void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq);
2079ed24f4bSMarc Zyngier void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
2089ed24f4bSMarc Zyngier bool vgic_get_phys_line_level(struct vgic_irq *irq);
2099ed24f4bSMarc Zyngier void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending);
2109ed24f4bSMarc Zyngier void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active);
2119ed24f4bSMarc Zyngier bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
2129ed24f4bSMarc Zyngier 			   unsigned long flags);
2139ed24f4bSMarc Zyngier void vgic_kick_vcpus(struct kvm *kvm);
2143134cc8bSMarc Zyngier void vgic_irq_handle_resampling(struct vgic_irq *irq,
2153134cc8bSMarc Zyngier 				bool lr_deactivated, bool lr_pending);
2169ed24f4bSMarc Zyngier 
217f25c5e4dSRicardo Koller int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
218f25c5e4dSRicardo Koller 		       phys_addr_t addr, phys_addr_t alignment,
219f25c5e4dSRicardo Koller 		       phys_addr_t size);
220f25c5e4dSRicardo Koller 
2219ed24f4bSMarc Zyngier void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu);
2229ed24f4bSMarc Zyngier void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
2239ed24f4bSMarc Zyngier void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr);
2249ed24f4bSMarc Zyngier void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);
2259ed24f4bSMarc Zyngier int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
2269ed24f4bSMarc Zyngier int vgic_v2_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2279ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2289ed24f4bSMarc Zyngier int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2299ed24f4bSMarc Zyngier 			  int offset, u32 *val);
2309ed24f4bSMarc Zyngier void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2319ed24f4bSMarc Zyngier void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2329ed24f4bSMarc Zyngier void vgic_v2_enable(struct kvm_vcpu *vcpu);
2339ed24f4bSMarc Zyngier int vgic_v2_probe(const struct gic_kvm_info *info);
2349ed24f4bSMarc Zyngier int vgic_v2_map_resources(struct kvm *kvm);
2359ed24f4bSMarc Zyngier int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
2369ed24f4bSMarc Zyngier 			     enum vgic_type);
2379ed24f4bSMarc Zyngier 
2389ed24f4bSMarc Zyngier void vgic_v2_init_lrs(void);
2399ed24f4bSMarc Zyngier void vgic_v2_load(struct kvm_vcpu *vcpu);
2409ed24f4bSMarc Zyngier void vgic_v2_put(struct kvm_vcpu *vcpu);
2419ed24f4bSMarc Zyngier void vgic_v2_vmcr_sync(struct kvm_vcpu *vcpu);
2429ed24f4bSMarc Zyngier 
2439ed24f4bSMarc Zyngier void vgic_v2_save_state(struct kvm_vcpu *vcpu);
2449ed24f4bSMarc Zyngier void vgic_v2_restore_state(struct kvm_vcpu *vcpu);
2459ed24f4bSMarc Zyngier 
vgic_get_irq_kref(struct vgic_irq * irq)2469ed24f4bSMarc Zyngier static inline void vgic_get_irq_kref(struct vgic_irq *irq)
2479ed24f4bSMarc Zyngier {
2489ed24f4bSMarc Zyngier 	if (irq->intid < VGIC_MIN_LPI)
2499ed24f4bSMarc Zyngier 		return;
2509ed24f4bSMarc Zyngier 
2519ed24f4bSMarc Zyngier 	kref_get(&irq->refcount);
2529ed24f4bSMarc Zyngier }
2539ed24f4bSMarc Zyngier 
2549ed24f4bSMarc Zyngier void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);
2559ed24f4bSMarc Zyngier void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
2569ed24f4bSMarc Zyngier void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);
2579ed24f4bSMarc Zyngier void vgic_v3_set_underflow(struct kvm_vcpu *vcpu);
2589ed24f4bSMarc Zyngier void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2599ed24f4bSMarc Zyngier void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2609ed24f4bSMarc Zyngier void vgic_v3_enable(struct kvm_vcpu *vcpu);
2619ed24f4bSMarc Zyngier int vgic_v3_probe(const struct gic_kvm_info *info);
2629ed24f4bSMarc Zyngier int vgic_v3_map_resources(struct kvm *kvm);
2639ed24f4bSMarc Zyngier int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq);
2649ed24f4bSMarc Zyngier int vgic_v3_save_pending_tables(struct kvm *kvm);
2659ed24f4bSMarc Zyngier int vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count);
2669ed24f4bSMarc Zyngier int vgic_register_redist_iodev(struct kvm_vcpu *vcpu);
267e13ce009SMarc Zyngier void vgic_unregister_redist_iodev(struct kvm_vcpu *vcpu);
2689ed24f4bSMarc Zyngier bool vgic_v3_check_base(struct kvm *kvm);
2699ed24f4bSMarc Zyngier 
2709ed24f4bSMarc Zyngier void vgic_v3_load(struct kvm_vcpu *vcpu);
2719ed24f4bSMarc Zyngier void vgic_v3_put(struct kvm_vcpu *vcpu);
2729ed24f4bSMarc Zyngier void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu);
2739ed24f4bSMarc Zyngier 
2749ed24f4bSMarc Zyngier bool vgic_has_its(struct kvm *kvm);
2759ed24f4bSMarc Zyngier int kvm_vgic_register_its_device(void);
2769ed24f4bSMarc Zyngier void vgic_enable_lpis(struct kvm_vcpu *vcpu);
2779ed24f4bSMarc Zyngier void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu);
2789ed24f4bSMarc Zyngier int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi);
2799ed24f4bSMarc Zyngier int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
2809ed24f4bSMarc Zyngier int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2819ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2829ed24f4bSMarc Zyngier int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2839ed24f4bSMarc Zyngier 			 int offset, u32 *val);
284db25081eSMarc Zyngier int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu,
285db25081eSMarc Zyngier 				struct kvm_device_attr *attr, bool is_write);
286b61fc085SMarc Zyngier int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
2879ed24f4bSMarc Zyngier int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
28838cf0bb7SMarc Zyngier 				    u32 intid, u32 *val);
2899ed24f4bSMarc Zyngier int kvm_register_vgic_device(unsigned long type);
2909ed24f4bSMarc Zyngier void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2919ed24f4bSMarc Zyngier void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2929ed24f4bSMarc Zyngier int vgic_lazy_init(struct kvm *kvm);
2939ed24f4bSMarc Zyngier int vgic_init(struct kvm *kvm);
2949ed24f4bSMarc Zyngier 
2959ed24f4bSMarc Zyngier void vgic_debug_init(struct kvm *kvm);
2969ed24f4bSMarc Zyngier void vgic_debug_destroy(struct kvm *kvm);
2979ed24f4bSMarc Zyngier 
vgic_v3_max_apr_idx(struct kvm_vcpu * vcpu)2989ed24f4bSMarc Zyngier static inline int vgic_v3_max_apr_idx(struct kvm_vcpu *vcpu)
2999ed24f4bSMarc Zyngier {
3009ed24f4bSMarc Zyngier 	struct vgic_cpu *cpu_if = &vcpu->arch.vgic_cpu;
3019ed24f4bSMarc Zyngier 
3029ed24f4bSMarc Zyngier 	/*
3039ed24f4bSMarc Zyngier 	 * num_pri_bits are initialized with HW supported values.
3049ed24f4bSMarc Zyngier 	 * We can rely safely on num_pri_bits even if VM has not
3059ed24f4bSMarc Zyngier 	 * restored ICC_CTLR_EL1 before restoring APnR registers.
3069ed24f4bSMarc Zyngier 	 */
3079ed24f4bSMarc Zyngier 	switch (cpu_if->num_pri_bits) {
3089ed24f4bSMarc Zyngier 	case 7: return 3;
3099ed24f4bSMarc Zyngier 	case 6: return 1;
3109ed24f4bSMarc Zyngier 	default: return 0;
3119ed24f4bSMarc Zyngier 	}
3129ed24f4bSMarc Zyngier }
3139ed24f4bSMarc Zyngier 
3149ed24f4bSMarc Zyngier static inline bool
vgic_v3_redist_region_full(struct vgic_redist_region * region)3159ed24f4bSMarc Zyngier vgic_v3_redist_region_full(struct vgic_redist_region *region)
3169ed24f4bSMarc Zyngier {
3179ed24f4bSMarc Zyngier 	if (!region->count)
3189ed24f4bSMarc Zyngier 		return false;
3199ed24f4bSMarc Zyngier 
3209ed24f4bSMarc Zyngier 	return (region->free_index >= region->count);
3219ed24f4bSMarc Zyngier }
3229ed24f4bSMarc Zyngier 
3239ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rdregs);
3249ed24f4bSMarc Zyngier 
3259ed24f4bSMarc Zyngier static inline size_t
vgic_v3_rd_region_size(struct kvm * kvm,struct vgic_redist_region * rdreg)3269ed24f4bSMarc Zyngier vgic_v3_rd_region_size(struct kvm *kvm, struct vgic_redist_region *rdreg)
3279ed24f4bSMarc Zyngier {
3289ed24f4bSMarc Zyngier 	if (!rdreg->count)
3299ed24f4bSMarc Zyngier 		return atomic_read(&kvm->online_vcpus) * KVM_VGIC_V3_REDIST_SIZE;
3309ed24f4bSMarc Zyngier 	else
3319ed24f4bSMarc Zyngier 		return rdreg->count * KVM_VGIC_V3_REDIST_SIZE;
3329ed24f4bSMarc Zyngier }
3339ed24f4bSMarc Zyngier 
3349ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm,
3359ed24f4bSMarc Zyngier 							   u32 index);
33648bb6285SMarc Zyngier void vgic_v3_free_redist_region(struct kvm *kvm, struct vgic_redist_region *rdreg);
3379ed24f4bSMarc Zyngier 
3389ed24f4bSMarc Zyngier bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size);
3399ed24f4bSMarc Zyngier 
vgic_dist_overlap(struct kvm * kvm,gpa_t base,size_t size)3409ed24f4bSMarc Zyngier static inline bool vgic_dist_overlap(struct kvm *kvm, gpa_t base, size_t size)
3419ed24f4bSMarc Zyngier {
3429ed24f4bSMarc Zyngier 	struct vgic_dist *d = &kvm->arch.vgic;
3439ed24f4bSMarc Zyngier 
3449ed24f4bSMarc Zyngier 	return (base + size > d->vgic_dist_base) &&
3459ed24f4bSMarc Zyngier 		(base < d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE);
3469ed24f4bSMarc Zyngier }
3479ed24f4bSMarc Zyngier 
34894828468SMarc Zyngier bool vgic_lpis_enabled(struct kvm_vcpu *vcpu);
3499ed24f4bSMarc Zyngier int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr);
3509ed24f4bSMarc Zyngier int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
3519ed24f4bSMarc Zyngier 			 u32 devid, u32 eventid, struct vgic_irq **irq);
3529ed24f4bSMarc Zyngier struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi);
3539ed24f4bSMarc Zyngier int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi);
3549ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_init(struct kvm *kvm);
3559ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_destroy(struct kvm *kvm);
3569ed24f4bSMarc Zyngier void vgic_its_invalidate_cache(struct kvm *kvm);
3579ed24f4bSMarc Zyngier 
3584645d11fSMarc Zyngier /* GICv4.1 MMIO interface */
3594645d11fSMarc Zyngier int vgic_its_inv_lpi(struct kvm *kvm, struct vgic_irq *irq);
3604645d11fSMarc Zyngier int vgic_its_invall(struct kvm_vcpu *vcpu);
3614645d11fSMarc Zyngier 
3629ed24f4bSMarc Zyngier bool vgic_supports_direct_msis(struct kvm *kvm);
3639ed24f4bSMarc Zyngier int vgic_v4_init(struct kvm *kvm);
3649ed24f4bSMarc Zyngier void vgic_v4_teardown(struct kvm *kvm);
3659ed24f4bSMarc Zyngier void vgic_v4_configure_vsgis(struct kvm *kvm);
36680317fe4SShenming Lu void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val);
367ef369168SMarc Zyngier int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq);
3689ed24f4bSMarc Zyngier 
kvm_has_gicv3(struct kvm * kvm)3699d7629beSMarc Zyngier static inline bool kvm_has_gicv3(struct kvm *kvm)
3709d7629beSMarc Zyngier {
3719d7629beSMarc Zyngier 	return (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif) &&
3729d7629beSMarc Zyngier 		irqchip_in_kernel(kvm) &&
3739d7629beSMarc Zyngier 		kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3);
3749d7629beSMarc Zyngier }
3759d7629beSMarc Zyngier 
3769ed24f4bSMarc Zyngier #endif
377