xref: /openbmc/linux/arch/arm64/kvm/vgic/vgic.h (revision 49a1a2c70a7fd820fe60a65a3e38ab9095082dc9)
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>
99ed24f4bSMarc Zyngier 
109ed24f4bSMarc Zyngier #define PRODUCT_ID_KVM		0x4b	/* ASCII code K */
119ed24f4bSMarc Zyngier #define IMPLEMENTER_ARM		0x43b
129ed24f4bSMarc Zyngier 
139ed24f4bSMarc Zyngier #define VGIC_ADDR_UNDEF		(-1)
149ed24f4bSMarc Zyngier #define IS_VGIC_ADDR_UNDEF(_x)  ((_x) == VGIC_ADDR_UNDEF)
159ed24f4bSMarc Zyngier 
169ed24f4bSMarc Zyngier #define INTERRUPT_ID_BITS_SPIS	10
179ed24f4bSMarc Zyngier #define INTERRUPT_ID_BITS_ITS	16
189ed24f4bSMarc Zyngier #define VGIC_PRI_BITS		5
199ed24f4bSMarc Zyngier 
209ed24f4bSMarc Zyngier #define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
219ed24f4bSMarc Zyngier 
229ed24f4bSMarc Zyngier #define VGIC_AFFINITY_0_SHIFT 0
239ed24f4bSMarc Zyngier #define VGIC_AFFINITY_0_MASK (0xffUL << VGIC_AFFINITY_0_SHIFT)
249ed24f4bSMarc Zyngier #define VGIC_AFFINITY_1_SHIFT 8
259ed24f4bSMarc Zyngier #define VGIC_AFFINITY_1_MASK (0xffUL << VGIC_AFFINITY_1_SHIFT)
269ed24f4bSMarc Zyngier #define VGIC_AFFINITY_2_SHIFT 16
279ed24f4bSMarc Zyngier #define VGIC_AFFINITY_2_MASK (0xffUL << VGIC_AFFINITY_2_SHIFT)
289ed24f4bSMarc Zyngier #define VGIC_AFFINITY_3_SHIFT 24
299ed24f4bSMarc Zyngier #define VGIC_AFFINITY_3_MASK (0xffUL << VGIC_AFFINITY_3_SHIFT)
309ed24f4bSMarc Zyngier 
319ed24f4bSMarc Zyngier #define VGIC_AFFINITY_LEVEL(reg, level) \
329ed24f4bSMarc Zyngier 	((((reg) & VGIC_AFFINITY_## level ##_MASK) \
339ed24f4bSMarc Zyngier 	>> VGIC_AFFINITY_## level ##_SHIFT) << MPIDR_LEVEL_SHIFT(level))
349ed24f4bSMarc Zyngier 
359ed24f4bSMarc Zyngier /*
369ed24f4bSMarc Zyngier  * The Userspace encodes the affinity differently from the MPIDR,
379ed24f4bSMarc Zyngier  * Below macro converts vgic userspace format to MPIDR reg format.
389ed24f4bSMarc Zyngier  */
399ed24f4bSMarc Zyngier #define VGIC_TO_MPIDR(val) (VGIC_AFFINITY_LEVEL(val, 0) | \
409ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 1) | \
419ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 2) | \
429ed24f4bSMarc Zyngier 			    VGIC_AFFINITY_LEVEL(val, 3))
439ed24f4bSMarc Zyngier 
449ed24f4bSMarc Zyngier /*
45039aeb9dSLinus Torvalds  * As per Documentation/virt/kvm/devices/arm-vgic-v3.rst,
469ed24f4bSMarc Zyngier  * below macros are defined for CPUREG encoding.
479ed24f4bSMarc Zyngier  */
489ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP0_MASK   0x000000000000c000
499ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP0_SHIFT  14
509ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP1_MASK   0x0000000000003800
519ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP1_SHIFT  11
529ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRN_MASK   0x0000000000000780
539ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRN_SHIFT  7
549ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRM_MASK   0x0000000000000078
559ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_CRM_SHIFT  3
569ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP2_MASK   0x0000000000000007
579ed24f4bSMarc Zyngier #define KVM_REG_ARM_VGIC_SYSREG_OP2_SHIFT  0
589ed24f4bSMarc Zyngier 
599ed24f4bSMarc Zyngier #define KVM_DEV_ARM_VGIC_SYSREG_MASK (KVM_REG_ARM_VGIC_SYSREG_OP0_MASK | \
609ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_OP1_MASK | \
619ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_CRN_MASK | \
629ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_CRM_MASK | \
639ed24f4bSMarc Zyngier 				      KVM_REG_ARM_VGIC_SYSREG_OP2_MASK)
649ed24f4bSMarc Zyngier 
659ed24f4bSMarc Zyngier /*
66039aeb9dSLinus Torvalds  * As per Documentation/virt/kvm/devices/arm-vgic-its.rst,
679ed24f4bSMarc Zyngier  * below macros are defined for ITS table entry encoding.
689ed24f4bSMarc Zyngier  */
699ed24f4bSMarc Zyngier #define KVM_ITS_CTE_VALID_SHIFT		63
709ed24f4bSMarc Zyngier #define KVM_ITS_CTE_VALID_MASK		BIT_ULL(63)
719ed24f4bSMarc Zyngier #define KVM_ITS_CTE_RDBASE_SHIFT	16
729ed24f4bSMarc Zyngier #define KVM_ITS_CTE_ICID_MASK		GENMASK_ULL(15, 0)
739ed24f4bSMarc Zyngier #define KVM_ITS_ITE_NEXT_SHIFT		48
749ed24f4bSMarc Zyngier #define KVM_ITS_ITE_PINTID_SHIFT	16
759ed24f4bSMarc Zyngier #define KVM_ITS_ITE_PINTID_MASK		GENMASK_ULL(47, 16)
769ed24f4bSMarc Zyngier #define KVM_ITS_ITE_ICID_MASK		GENMASK_ULL(15, 0)
779ed24f4bSMarc Zyngier #define KVM_ITS_DTE_VALID_SHIFT		63
789ed24f4bSMarc Zyngier #define KVM_ITS_DTE_VALID_MASK		BIT_ULL(63)
799ed24f4bSMarc Zyngier #define KVM_ITS_DTE_NEXT_SHIFT		49
809ed24f4bSMarc Zyngier #define KVM_ITS_DTE_NEXT_MASK		GENMASK_ULL(62, 49)
819ed24f4bSMarc Zyngier #define KVM_ITS_DTE_ITTADDR_SHIFT	5
829ed24f4bSMarc Zyngier #define KVM_ITS_DTE_ITTADDR_MASK	GENMASK_ULL(48, 5)
839ed24f4bSMarc Zyngier #define KVM_ITS_DTE_SIZE_MASK		GENMASK_ULL(4, 0)
849ed24f4bSMarc Zyngier #define KVM_ITS_L1E_VALID_MASK		BIT_ULL(63)
859ed24f4bSMarc Zyngier /* we only support 64 kB translation table page size */
869ed24f4bSMarc Zyngier #define KVM_ITS_L1E_ADDR_MASK		GENMASK_ULL(51, 16)
879ed24f4bSMarc Zyngier 
889ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_INDEX_MASK	GENMASK_ULL(11, 0)
899ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_FLAGS_MASK	GENMASK_ULL(15, 12)
909ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_FLAGS_SHIFT	12
919ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_BASE_MASK	GENMASK_ULL(51, 16)
929ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_COUNT_MASK	GENMASK_ULL(63, 52)
939ed24f4bSMarc Zyngier #define KVM_VGIC_V3_RDIST_COUNT_SHIFT	52
949ed24f4bSMarc Zyngier 
959ed24f4bSMarc Zyngier #ifdef CONFIG_DEBUG_SPINLOCK
969ed24f4bSMarc Zyngier #define DEBUG_SPINLOCK_BUG_ON(p) BUG_ON(p)
979ed24f4bSMarc Zyngier #else
989ed24f4bSMarc Zyngier #define DEBUG_SPINLOCK_BUG_ON(p)
999ed24f4bSMarc Zyngier #endif
1009ed24f4bSMarc Zyngier 
101*49a1a2c7SMarc Zyngier static inline u32 vgic_get_implementation_rev(struct kvm_vcpu *vcpu)
102*49a1a2c7SMarc Zyngier {
103*49a1a2c7SMarc Zyngier 	return vcpu->kvm->arch.vgic.implementation_rev;
104*49a1a2c7SMarc Zyngier }
105*49a1a2c7SMarc Zyngier 
1069ed24f4bSMarc Zyngier /* Requires the irq_lock to be held by the caller. */
1079ed24f4bSMarc Zyngier static inline bool irq_is_pending(struct vgic_irq *irq)
1089ed24f4bSMarc Zyngier {
1099ed24f4bSMarc Zyngier 	if (irq->config == VGIC_CONFIG_EDGE)
1109ed24f4bSMarc Zyngier 		return irq->pending_latch;
1119ed24f4bSMarc Zyngier 	else
1129ed24f4bSMarc Zyngier 		return irq->pending_latch || irq->line_level;
1139ed24f4bSMarc Zyngier }
1149ed24f4bSMarc Zyngier 
1159ed24f4bSMarc Zyngier static inline bool vgic_irq_is_mapped_level(struct vgic_irq *irq)
1169ed24f4bSMarc Zyngier {
1179ed24f4bSMarc Zyngier 	return irq->config == VGIC_CONFIG_LEVEL && irq->hw;
1189ed24f4bSMarc Zyngier }
1199ed24f4bSMarc Zyngier 
1209ed24f4bSMarc Zyngier static inline int vgic_irq_get_lr_count(struct vgic_irq *irq)
1219ed24f4bSMarc Zyngier {
1229ed24f4bSMarc Zyngier 	/* Account for the active state as an interrupt */
1239ed24f4bSMarc Zyngier 	if (vgic_irq_is_sgi(irq->intid) && irq->source)
1249ed24f4bSMarc Zyngier 		return hweight8(irq->source) + irq->active;
1259ed24f4bSMarc Zyngier 
1269ed24f4bSMarc Zyngier 	return irq_is_pending(irq) || irq->active;
1279ed24f4bSMarc Zyngier }
1289ed24f4bSMarc Zyngier 
1299ed24f4bSMarc Zyngier static inline bool vgic_irq_is_multi_sgi(struct vgic_irq *irq)
1309ed24f4bSMarc Zyngier {
1319ed24f4bSMarc Zyngier 	return vgic_irq_get_lr_count(irq) > 1;
1329ed24f4bSMarc Zyngier }
1339ed24f4bSMarc Zyngier 
1349ed24f4bSMarc Zyngier /*
1359ed24f4bSMarc Zyngier  * This struct provides an intermediate representation of the fields contained
1369ed24f4bSMarc Zyngier  * in the GICH_VMCR and ICH_VMCR registers, such that code exporting the GIC
1379ed24f4bSMarc Zyngier  * state to userspace can generate either GICv2 or GICv3 CPU interface
1389ed24f4bSMarc Zyngier  * registers regardless of the hardware backed GIC used.
1399ed24f4bSMarc Zyngier  */
1409ed24f4bSMarc Zyngier struct vgic_vmcr {
1419ed24f4bSMarc Zyngier 	u32	grpen0;
1429ed24f4bSMarc Zyngier 	u32	grpen1;
1439ed24f4bSMarc Zyngier 
1449ed24f4bSMarc Zyngier 	u32	ackctl;
1459ed24f4bSMarc Zyngier 	u32	fiqen;
1469ed24f4bSMarc Zyngier 	u32	cbpr;
1479ed24f4bSMarc Zyngier 	u32	eoim;
1489ed24f4bSMarc Zyngier 
1499ed24f4bSMarc Zyngier 	u32	abpr;
1509ed24f4bSMarc Zyngier 	u32	bpr;
1519ed24f4bSMarc Zyngier 	u32	pmr;  /* Priority mask field in the GICC_PMR and
1529ed24f4bSMarc Zyngier 		       * ICC_PMR_EL1 priority field format */
1539ed24f4bSMarc Zyngier };
1549ed24f4bSMarc Zyngier 
1559ed24f4bSMarc Zyngier struct vgic_reg_attr {
1569ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
1579ed24f4bSMarc Zyngier 	gpa_t addr;
1589ed24f4bSMarc Zyngier };
1599ed24f4bSMarc Zyngier 
1609ed24f4bSMarc Zyngier int vgic_v3_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
1619ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
1629ed24f4bSMarc Zyngier int vgic_v2_parse_attr(struct kvm_device *dev, struct kvm_device_attr *attr,
1639ed24f4bSMarc Zyngier 		       struct vgic_reg_attr *reg_attr);
1649ed24f4bSMarc Zyngier const struct vgic_register_region *
1659ed24f4bSMarc Zyngier vgic_get_mmio_region(struct kvm_vcpu *vcpu, struct vgic_io_device *iodev,
1669ed24f4bSMarc Zyngier 		     gpa_t addr, int len);
1679ed24f4bSMarc Zyngier struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
1689ed24f4bSMarc Zyngier 			      u32 intid);
1699ed24f4bSMarc Zyngier void __vgic_put_lpi_locked(struct kvm *kvm, struct vgic_irq *irq);
1709ed24f4bSMarc Zyngier void vgic_put_irq(struct kvm *kvm, struct vgic_irq *irq);
1719ed24f4bSMarc Zyngier bool vgic_get_phys_line_level(struct vgic_irq *irq);
1729ed24f4bSMarc Zyngier void vgic_irq_set_phys_pending(struct vgic_irq *irq, bool pending);
1739ed24f4bSMarc Zyngier void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active);
1749ed24f4bSMarc Zyngier bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
1759ed24f4bSMarc Zyngier 			   unsigned long flags);
1769ed24f4bSMarc Zyngier void vgic_kick_vcpus(struct kvm *kvm);
1773134cc8bSMarc Zyngier void vgic_irq_handle_resampling(struct vgic_irq *irq,
1783134cc8bSMarc Zyngier 				bool lr_deactivated, bool lr_pending);
1799ed24f4bSMarc Zyngier 
180f25c5e4dSRicardo Koller int vgic_check_iorange(struct kvm *kvm, phys_addr_t ioaddr,
181f25c5e4dSRicardo Koller 		       phys_addr_t addr, phys_addr_t alignment,
182f25c5e4dSRicardo Koller 		       phys_addr_t size);
183f25c5e4dSRicardo Koller 
1849ed24f4bSMarc Zyngier void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu);
1859ed24f4bSMarc Zyngier void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
1869ed24f4bSMarc Zyngier void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr);
1879ed24f4bSMarc Zyngier void vgic_v2_set_underflow(struct kvm_vcpu *vcpu);
1889ed24f4bSMarc Zyngier void vgic_v2_set_npie(struct kvm_vcpu *vcpu);
1899ed24f4bSMarc Zyngier int vgic_v2_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
1909ed24f4bSMarc Zyngier int vgic_v2_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
1919ed24f4bSMarc Zyngier 			 int offset, u32 *val);
1929ed24f4bSMarc Zyngier int vgic_v2_cpuif_uaccess(struct kvm_vcpu *vcpu, bool is_write,
1939ed24f4bSMarc Zyngier 			  int offset, u32 *val);
1949ed24f4bSMarc Zyngier void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
1959ed24f4bSMarc Zyngier void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
1969ed24f4bSMarc Zyngier void vgic_v2_enable(struct kvm_vcpu *vcpu);
1979ed24f4bSMarc Zyngier int vgic_v2_probe(const struct gic_kvm_info *info);
1989ed24f4bSMarc Zyngier int vgic_v2_map_resources(struct kvm *kvm);
1999ed24f4bSMarc Zyngier int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
2009ed24f4bSMarc Zyngier 			     enum vgic_type);
2019ed24f4bSMarc Zyngier 
2029ed24f4bSMarc Zyngier void vgic_v2_init_lrs(void);
2039ed24f4bSMarc Zyngier void vgic_v2_load(struct kvm_vcpu *vcpu);
2049ed24f4bSMarc Zyngier void vgic_v2_put(struct kvm_vcpu *vcpu);
2059ed24f4bSMarc Zyngier void vgic_v2_vmcr_sync(struct kvm_vcpu *vcpu);
2069ed24f4bSMarc Zyngier 
2079ed24f4bSMarc Zyngier void vgic_v2_save_state(struct kvm_vcpu *vcpu);
2089ed24f4bSMarc Zyngier void vgic_v2_restore_state(struct kvm_vcpu *vcpu);
2099ed24f4bSMarc Zyngier 
2109ed24f4bSMarc Zyngier static inline void vgic_get_irq_kref(struct vgic_irq *irq)
2119ed24f4bSMarc Zyngier {
2129ed24f4bSMarc Zyngier 	if (irq->intid < VGIC_MIN_LPI)
2139ed24f4bSMarc Zyngier 		return;
2149ed24f4bSMarc Zyngier 
2159ed24f4bSMarc Zyngier 	kref_get(&irq->refcount);
2169ed24f4bSMarc Zyngier }
2179ed24f4bSMarc Zyngier 
2189ed24f4bSMarc Zyngier void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);
2199ed24f4bSMarc Zyngier void vgic_v3_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr);
2209ed24f4bSMarc Zyngier void vgic_v3_clear_lr(struct kvm_vcpu *vcpu, int lr);
2219ed24f4bSMarc Zyngier void vgic_v3_set_underflow(struct kvm_vcpu *vcpu);
2229ed24f4bSMarc Zyngier void vgic_v3_set_npie(struct kvm_vcpu *vcpu);
2239ed24f4bSMarc Zyngier void vgic_v3_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2249ed24f4bSMarc Zyngier void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2259ed24f4bSMarc Zyngier void vgic_v3_enable(struct kvm_vcpu *vcpu);
2269ed24f4bSMarc Zyngier int vgic_v3_probe(const struct gic_kvm_info *info);
2279ed24f4bSMarc Zyngier int vgic_v3_map_resources(struct kvm *kvm);
2289ed24f4bSMarc Zyngier int vgic_v3_lpi_sync_pending_status(struct kvm *kvm, struct vgic_irq *irq);
2299ed24f4bSMarc Zyngier int vgic_v3_save_pending_tables(struct kvm *kvm);
2309ed24f4bSMarc Zyngier int vgic_v3_set_redist_base(struct kvm *kvm, u32 index, u64 addr, u32 count);
2319ed24f4bSMarc Zyngier int vgic_register_redist_iodev(struct kvm_vcpu *vcpu);
2329ed24f4bSMarc Zyngier bool vgic_v3_check_base(struct kvm *kvm);
2339ed24f4bSMarc Zyngier 
2349ed24f4bSMarc Zyngier void vgic_v3_load(struct kvm_vcpu *vcpu);
2359ed24f4bSMarc Zyngier void vgic_v3_put(struct kvm_vcpu *vcpu);
2369ed24f4bSMarc Zyngier void vgic_v3_vmcr_sync(struct kvm_vcpu *vcpu);
2379ed24f4bSMarc Zyngier 
2389ed24f4bSMarc Zyngier bool vgic_has_its(struct kvm *kvm);
2399ed24f4bSMarc Zyngier int kvm_vgic_register_its_device(void);
2409ed24f4bSMarc Zyngier void vgic_enable_lpis(struct kvm_vcpu *vcpu);
2419ed24f4bSMarc Zyngier void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu);
2429ed24f4bSMarc Zyngier int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi);
2439ed24f4bSMarc Zyngier int vgic_v3_has_attr_regs(struct kvm_device *dev, struct kvm_device_attr *attr);
2449ed24f4bSMarc Zyngier int vgic_v3_dist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2459ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2469ed24f4bSMarc Zyngier int vgic_v3_redist_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2479ed24f4bSMarc Zyngier 			 int offset, u32 *val);
2489ed24f4bSMarc Zyngier int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2499ed24f4bSMarc Zyngier 			 u64 id, u64 *val);
2509ed24f4bSMarc Zyngier int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id,
2519ed24f4bSMarc Zyngier 				u64 *reg);
2529ed24f4bSMarc Zyngier int vgic_v3_line_level_info_uaccess(struct kvm_vcpu *vcpu, bool is_write,
2539ed24f4bSMarc Zyngier 				    u32 intid, u64 *val);
2549ed24f4bSMarc Zyngier int kvm_register_vgic_device(unsigned long type);
2559ed24f4bSMarc Zyngier void vgic_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2569ed24f4bSMarc Zyngier void vgic_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
2579ed24f4bSMarc Zyngier int vgic_lazy_init(struct kvm *kvm);
2589ed24f4bSMarc Zyngier int vgic_init(struct kvm *kvm);
2599ed24f4bSMarc Zyngier 
2609ed24f4bSMarc Zyngier void vgic_debug_init(struct kvm *kvm);
2619ed24f4bSMarc Zyngier void vgic_debug_destroy(struct kvm *kvm);
2629ed24f4bSMarc Zyngier 
2639ed24f4bSMarc Zyngier bool lock_all_vcpus(struct kvm *kvm);
2649ed24f4bSMarc Zyngier void unlock_all_vcpus(struct kvm *kvm);
2659ed24f4bSMarc Zyngier 
2669ed24f4bSMarc Zyngier static inline int vgic_v3_max_apr_idx(struct kvm_vcpu *vcpu)
2679ed24f4bSMarc Zyngier {
2689ed24f4bSMarc Zyngier 	struct vgic_cpu *cpu_if = &vcpu->arch.vgic_cpu;
2699ed24f4bSMarc Zyngier 
2709ed24f4bSMarc Zyngier 	/*
2719ed24f4bSMarc Zyngier 	 * num_pri_bits are initialized with HW supported values.
2729ed24f4bSMarc Zyngier 	 * We can rely safely on num_pri_bits even if VM has not
2739ed24f4bSMarc Zyngier 	 * restored ICC_CTLR_EL1 before restoring APnR registers.
2749ed24f4bSMarc Zyngier 	 */
2759ed24f4bSMarc Zyngier 	switch (cpu_if->num_pri_bits) {
2769ed24f4bSMarc Zyngier 	case 7: return 3;
2779ed24f4bSMarc Zyngier 	case 6: return 1;
2789ed24f4bSMarc Zyngier 	default: return 0;
2799ed24f4bSMarc Zyngier 	}
2809ed24f4bSMarc Zyngier }
2819ed24f4bSMarc Zyngier 
2829ed24f4bSMarc Zyngier static inline bool
2839ed24f4bSMarc Zyngier vgic_v3_redist_region_full(struct vgic_redist_region *region)
2849ed24f4bSMarc Zyngier {
2859ed24f4bSMarc Zyngier 	if (!region->count)
2869ed24f4bSMarc Zyngier 		return false;
2879ed24f4bSMarc Zyngier 
2889ed24f4bSMarc Zyngier 	return (region->free_index >= region->count);
2899ed24f4bSMarc Zyngier }
2909ed24f4bSMarc Zyngier 
2919ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_free_slot(struct list_head *rdregs);
2929ed24f4bSMarc Zyngier 
2939ed24f4bSMarc Zyngier static inline size_t
2949ed24f4bSMarc Zyngier vgic_v3_rd_region_size(struct kvm *kvm, struct vgic_redist_region *rdreg)
2959ed24f4bSMarc Zyngier {
2969ed24f4bSMarc Zyngier 	if (!rdreg->count)
2979ed24f4bSMarc Zyngier 		return atomic_read(&kvm->online_vcpus) * KVM_VGIC_V3_REDIST_SIZE;
2989ed24f4bSMarc Zyngier 	else
2999ed24f4bSMarc Zyngier 		return rdreg->count * KVM_VGIC_V3_REDIST_SIZE;
3009ed24f4bSMarc Zyngier }
3019ed24f4bSMarc Zyngier 
3029ed24f4bSMarc Zyngier struct vgic_redist_region *vgic_v3_rdist_region_from_index(struct kvm *kvm,
3039ed24f4bSMarc Zyngier 							   u32 index);
304e5a35635SEric Auger void vgic_v3_free_redist_region(struct vgic_redist_region *rdreg);
3059ed24f4bSMarc Zyngier 
3069ed24f4bSMarc Zyngier bool vgic_v3_rdist_overlap(struct kvm *kvm, gpa_t base, size_t size);
3079ed24f4bSMarc Zyngier 
3089ed24f4bSMarc Zyngier static inline bool vgic_dist_overlap(struct kvm *kvm, gpa_t base, size_t size)
3099ed24f4bSMarc Zyngier {
3109ed24f4bSMarc Zyngier 	struct vgic_dist *d = &kvm->arch.vgic;
3119ed24f4bSMarc Zyngier 
3129ed24f4bSMarc Zyngier 	return (base + size > d->vgic_dist_base) &&
3139ed24f4bSMarc Zyngier 		(base < d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE);
3149ed24f4bSMarc Zyngier }
3159ed24f4bSMarc Zyngier 
31694828468SMarc Zyngier bool vgic_lpis_enabled(struct kvm_vcpu *vcpu);
3179ed24f4bSMarc Zyngier int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr);
3189ed24f4bSMarc Zyngier int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
3199ed24f4bSMarc Zyngier 			 u32 devid, u32 eventid, struct vgic_irq **irq);
3209ed24f4bSMarc Zyngier struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi);
3219ed24f4bSMarc Zyngier int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi);
3229ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_init(struct kvm *kvm);
3239ed24f4bSMarc Zyngier void vgic_lpi_translation_cache_destroy(struct kvm *kvm);
3249ed24f4bSMarc Zyngier void vgic_its_invalidate_cache(struct kvm *kvm);
3259ed24f4bSMarc Zyngier 
3264645d11fSMarc Zyngier /* GICv4.1 MMIO interface */
3274645d11fSMarc Zyngier int vgic_its_inv_lpi(struct kvm *kvm, struct vgic_irq *irq);
3284645d11fSMarc Zyngier int vgic_its_invall(struct kvm_vcpu *vcpu);
3294645d11fSMarc Zyngier 
3309ed24f4bSMarc Zyngier bool vgic_supports_direct_msis(struct kvm *kvm);
3319ed24f4bSMarc Zyngier int vgic_v4_init(struct kvm *kvm);
3329ed24f4bSMarc Zyngier void vgic_v4_teardown(struct kvm *kvm);
3339ed24f4bSMarc Zyngier void vgic_v4_configure_vsgis(struct kvm *kvm);
33480317fe4SShenming Lu void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val);
3359ed24f4bSMarc Zyngier 
3369ed24f4bSMarc Zyngier #endif
337