xref: /openbmc/linux/include/kvm/arm_vgic.h (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
27275acdfSMarc Zyngier /*
350926d82SMarc Zyngier  * Copyright (C) 2015, 2016 ARM Ltd.
47275acdfSMarc Zyngier  */
550926d82SMarc Zyngier #ifndef __KVM_ARM_VGIC_H
650926d82SMarc Zyngier #define __KVM_ARM_VGIC_H
7b18b5778SChristoffer Dall 
86c9eeb5fSAndy Shevchenko #include <linux/bits.h>
97275acdfSMarc Zyngier #include <linux/kvm.h>
107275acdfSMarc Zyngier #include <linux/irqreturn.h>
116c9eeb5fSAndy Shevchenko #include <linux/kref.h>
126c9eeb5fSAndy Shevchenko #include <linux/mutex.h>
137275acdfSMarc Zyngier #include <linux/spinlock.h>
14fb5ee369SMarc Zyngier #include <linux/static_key.h>
157275acdfSMarc Zyngier #include <linux/types.h>
166777f77fSAndre Przywara #include <kvm/iodev.h>
17424c3383SAndre Przywara #include <linux/list.h>
185a7a8426SVladimir Murzin #include <linux/jump_label.h>
197275acdfSMarc Zyngier 
2074fe55dcSMarc Zyngier #include <linux/irqchip/arm-gic-v4.h>
2174fe55dcSMarc Zyngier 
22e25028c8SEric Auger #define VGIC_V3_MAX_CPUS	512
2350926d82SMarc Zyngier #define VGIC_V2_MAX_CPUS	8
245fb66da6SMarc Zyngier #define VGIC_NR_IRQS_LEGACY     256
257275acdfSMarc Zyngier #define VGIC_NR_SGIS		16
267275acdfSMarc Zyngier #define VGIC_NR_PPIS		16
277275acdfSMarc Zyngier #define VGIC_NR_PRIVATE_IRQS	(VGIC_NR_SGIS + VGIC_NR_PPIS)
2850926d82SMarc Zyngier #define VGIC_MAX_PRIVATE	(VGIC_NR_PRIVATE_IRQS - 1)
2950926d82SMarc Zyngier #define VGIC_MAX_SPI		1019
3050926d82SMarc Zyngier #define VGIC_MAX_RESERVED	1023
3150926d82SMarc Zyngier #define VGIC_MIN_LPI		8192
32180ae7b1SEric Auger #define KVM_IRQCHIP_NUM_PINS	(1020 - 32)
338d5c6b06SMarc Zyngier 
343cba4af3SChristoffer Dall #define irq_is_ppi(irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS)
35ebb127f2SChristoffer Dall #define irq_is_spi(irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \
36ebb127f2SChristoffer Dall 			 (irq) <= VGIC_MAX_SPI)
373cba4af3SChristoffer Dall 
381a9b1305SMarc Zyngier enum vgic_type {
391a9b1305SMarc Zyngier 	VGIC_V2,		/* Good ol' GICv2 */
40b2fb1c0dSMarc Zyngier 	VGIC_V3,		/* New fancy GICv3 */
411a9b1305SMarc Zyngier };
421a9b1305SMarc Zyngier 
4350926d82SMarc Zyngier /* same for all guests, as depending only on the _host's_ GIC model */
4450926d82SMarc Zyngier struct vgic_global {
4550926d82SMarc Zyngier 	/* type of the host GIC */
461a9b1305SMarc Zyngier 	enum vgic_type		type;
4750926d82SMarc Zyngier 
48ca85f623SMarc Zyngier 	/* Physical address of vgic virtual cpu interface */
49ca85f623SMarc Zyngier 	phys_addr_t		vcpu_base;
5050926d82SMarc Zyngier 
511bb32a44SMarc Zyngier 	/* GICV mapping, kernel VA */
52bf8feb39SMarc Zyngier 	void __iomem		*vcpu_base_va;
531bb32a44SMarc Zyngier 	/* GICV mapping, HYP VA */
541bb32a44SMarc Zyngier 	void __iomem		*vcpu_hyp_va;
55bf8feb39SMarc Zyngier 
561bb32a44SMarc Zyngier 	/* virtual control interface mapping, kernel VA */
57ca85f623SMarc Zyngier 	void __iomem		*vctrl_base;
581bb32a44SMarc Zyngier 	/* virtual control interface mapping, HYP VA */
591bb32a44SMarc Zyngier 	void __iomem		*vctrl_hyp;
6050926d82SMarc Zyngier 
6150926d82SMarc Zyngier 	/* Number of implemented list registers */
6250926d82SMarc Zyngier 	int			nr_lr;
6350926d82SMarc Zyngier 
6450926d82SMarc Zyngier 	/* Maintenance IRQ number */
6550926d82SMarc Zyngier 	unsigned int		maint_irq;
6650926d82SMarc Zyngier 
6750926d82SMarc Zyngier 	/* maximum number of VCPUs allowed (GICv2 limits us to 8) */
683caa2d8cSAndre Przywara 	int			max_gic_vcpus;
6950926d82SMarc Zyngier 
70b5d84ff6SAndre Przywara 	/* Only needed for the legacy KVM_CREATE_IRQCHIP */
71b5d84ff6SAndre Przywara 	bool			can_emulate_gicv2;
725a7a8426SVladimir Murzin 
73e7c48059SMarc Zyngier 	/* Hardware has GICv4? */
74e7c48059SMarc Zyngier 	bool			has_gicv4;
75ae699ad3SMarc Zyngier 	bool			has_gicv4_1;
76e7c48059SMarc Zyngier 
77f6c3e24fSMarc Zyngier 	/* Pseudo GICv3 from outer space */
78f6c3e24fSMarc Zyngier 	bool			no_hw_deactivation;
79f6c3e24fSMarc Zyngier 
805a7a8426SVladimir Murzin 	/* GIC system register CPU interface */
815a7a8426SVladimir Murzin 	struct static_key_false gicv3_cpuif;
82d017d7b0SVijaya Kumar K 
83d017d7b0SVijaya Kumar K 	u32			ich_vtr_el2;
84ca85f623SMarc Zyngier };
85ca85f623SMarc Zyngier 
8650926d82SMarc Zyngier extern struct vgic_global kvm_vgic_global_state;
8750926d82SMarc Zyngier 
8850926d82SMarc Zyngier #define VGIC_V2_MAX_LRS		(1 << 6)
8950926d82SMarc Zyngier #define VGIC_V3_MAX_LRS		16
9050926d82SMarc Zyngier #define VGIC_V3_LR_INDEX(lr)	(VGIC_V3_MAX_LRS - 1 - lr)
9150926d82SMarc Zyngier 
9250926d82SMarc Zyngier enum vgic_irq_config {
9350926d82SMarc Zyngier 	VGIC_CONFIG_EDGE = 0,
9450926d82SMarc Zyngier 	VGIC_CONFIG_LEVEL
95b26e5fdaSAndre Przywara };
96b26e5fdaSAndre Przywara 
97db75f1a3SMarc Zyngier /*
98db75f1a3SMarc Zyngier  * Per-irq ops overriding some common behavious.
99db75f1a3SMarc Zyngier  *
100db75f1a3SMarc Zyngier  * Always called in non-preemptible section and the functions can use
101db75f1a3SMarc Zyngier  * kvm_arm_get_running_vcpu() to get the vcpu pointer for private IRQs.
102db75f1a3SMarc Zyngier  */
103db75f1a3SMarc Zyngier struct irq_ops {
104354920e7SMarc Zyngier 	/* Per interrupt flags for special-cased interrupts */
105354920e7SMarc Zyngier 	unsigned long flags;
106354920e7SMarc Zyngier 
107354920e7SMarc Zyngier #define VGIC_IRQ_SW_RESAMPLE	BIT(0)	/* Clear the active state for resampling */
108354920e7SMarc Zyngier 
109db75f1a3SMarc Zyngier 	/*
110db75f1a3SMarc Zyngier 	 * Callback function pointer to in-kernel devices that can tell us the
111db75f1a3SMarc Zyngier 	 * state of the input level of mapped level-triggered IRQ faster than
112db75f1a3SMarc Zyngier 	 * peaking into the physical GIC.
113db75f1a3SMarc Zyngier 	 */
114db75f1a3SMarc Zyngier 	bool (*get_input_level)(int vintid);
115db75f1a3SMarc Zyngier };
116db75f1a3SMarc Zyngier 
11750926d82SMarc Zyngier struct vgic_irq {
1188fa3adb8SJulien Thierry 	raw_spinlock_t irq_lock;	/* Protects the content of the struct */
1193802411dSAndre Przywara 	struct list_head lpi_list;	/* Used to link all LPIs together */
12050926d82SMarc Zyngier 	struct list_head ap_list;
12150926d82SMarc Zyngier 
12250926d82SMarc Zyngier 	struct kvm_vcpu *vcpu;		/* SGIs and PPIs: The VCPU
12350926d82SMarc Zyngier 					 * SPIs and LPIs: The VCPU whose ap_list
12450926d82SMarc Zyngier 					 * this is queued on.
12550926d82SMarc Zyngier 					 */
12650926d82SMarc Zyngier 
12750926d82SMarc Zyngier 	struct kvm_vcpu *target_vcpu;	/* The VCPU that this interrupt should
12850926d82SMarc Zyngier 					 * be sent to, as a result of the
12950926d82SMarc Zyngier 					 * targets reg (v2) or the
13050926d82SMarc Zyngier 					 * affinity reg (v3).
13150926d82SMarc Zyngier 					 */
13250926d82SMarc Zyngier 
13350926d82SMarc Zyngier 	u32 intid;			/* Guest visible INTID */
13450926d82SMarc Zyngier 	bool line_level;		/* Level only */
1358694e4daSChristoffer Dall 	bool pending_latch;		/* The pending latch state used to calculate
1368694e4daSChristoffer Dall 					 * the pending state for both level
1378694e4daSChristoffer Dall 					 * and edge triggered IRQs. */
13850926d82SMarc Zyngier 	bool active;			/* not used for LPIs */
13950926d82SMarc Zyngier 	bool enabled;
14050926d82SMarc Zyngier 	bool hw;			/* Tied to HW IRQ */
1415dd4b924SAndre Przywara 	struct kref refcount;		/* Used for LPIs */
14250926d82SMarc Zyngier 	u32 hwintid;			/* HW INTID number */
14347bbd31fSEric Auger 	unsigned int host_irq;		/* linux irq corresponding to hwintid */
14450926d82SMarc Zyngier 	union {
14550926d82SMarc Zyngier 		u8 targets;			/* GICv2 target VCPUs mask */
14650926d82SMarc Zyngier 		u32 mpidr;			/* GICv3 target VCPU */
14750926d82SMarc Zyngier 	};
14850926d82SMarc Zyngier 	u8 source;			/* GICv2 SGIs only */
14953692908SMarc Zyngier 	u8 active_source;		/* GICv2 SGIs only */
15050926d82SMarc Zyngier 	u8 priority;
1518df3c8f3SChristoffer Dall 	u8 group;			/* 0 == group 0, 1 == group 1 */
15250926d82SMarc Zyngier 	enum vgic_irq_config config;	/* Level or edge */
153c6ccd30eSChristoffer Dall 
154db75f1a3SMarc Zyngier 	struct irq_ops *ops;
155b6909a65SChristoffer Dall 
156c6ccd30eSChristoffer Dall 	void *owner;			/* Opaque pointer to reserve an interrupt
157c6ccd30eSChristoffer Dall 					   for in-kernel devices. */
15850926d82SMarc Zyngier };
15950926d82SMarc Zyngier 
vgic_irq_needs_resampling(struct vgic_irq * irq)160354920e7SMarc Zyngier static inline bool vgic_irq_needs_resampling(struct vgic_irq *irq)
161354920e7SMarc Zyngier {
162354920e7SMarc Zyngier 	return irq->ops && (irq->ops->flags & VGIC_IRQ_SW_RESAMPLE);
163354920e7SMarc Zyngier }
164354920e7SMarc Zyngier 
16550926d82SMarc Zyngier struct vgic_register_region;
16659c5ab40SAndre Przywara struct vgic_its;
16759c5ab40SAndre Przywara 
16859c5ab40SAndre Przywara enum iodev_type {
16959c5ab40SAndre Przywara 	IODEV_CPUIF,
17059c5ab40SAndre Przywara 	IODEV_DIST,
17159c5ab40SAndre Przywara 	IODEV_REDIST,
17259c5ab40SAndre Przywara 	IODEV_ITS
17359c5ab40SAndre Przywara };
17450926d82SMarc Zyngier 
1756777f77fSAndre Przywara struct vgic_io_device {
17650926d82SMarc Zyngier 	gpa_t base_addr;
17759c5ab40SAndre Przywara 	union {
1786777f77fSAndre Przywara 		struct kvm_vcpu *redist_vcpu;
17959c5ab40SAndre Przywara 		struct vgic_its *its;
18059c5ab40SAndre Przywara 	};
18150926d82SMarc Zyngier 	const struct vgic_register_region *regions;
18259c5ab40SAndre Przywara 	enum iodev_type iodev_type;
18350926d82SMarc Zyngier 	int nr_regions;
1846777f77fSAndre Przywara 	struct kvm_io_device dev;
1856777f77fSAndre Przywara };
1866777f77fSAndre Przywara 
18759c5ab40SAndre Przywara struct vgic_its {
18859c5ab40SAndre Przywara 	/* The base address of the ITS control register frame */
18959c5ab40SAndre Przywara 	gpa_t			vgic_its_base;
19059c5ab40SAndre Przywara 
19159c5ab40SAndre Przywara 	bool			enabled;
19259c5ab40SAndre Przywara 	struct vgic_io_device	iodev;
193bb717644SMarc Zyngier 	struct kvm_device	*dev;
194424c3383SAndre Przywara 
195424c3383SAndre Przywara 	/* These registers correspond to GITS_BASER{0,1} */
196424c3383SAndre Przywara 	u64			baser_device_table;
197424c3383SAndre Przywara 	u64			baser_coll_table;
198424c3383SAndre Przywara 
199424c3383SAndre Przywara 	/* Protects the command queue */
200424c3383SAndre Przywara 	struct mutex		cmd_lock;
201424c3383SAndre Przywara 	u64			cbaser;
202424c3383SAndre Przywara 	u32			creadr;
203424c3383SAndre Przywara 	u32			cwriter;
204424c3383SAndre Przywara 
20571afe470SEric Auger 	/* migration ABI revision in use */
20671afe470SEric Auger 	u32			abi_rev;
20771afe470SEric Auger 
208424c3383SAndre Przywara 	/* Protects the device and collection lists */
209424c3383SAndre Przywara 	struct mutex		its_lock;
210424c3383SAndre Przywara 	struct list_head	device_list;
211424c3383SAndre Przywara 	struct list_head	collection_list;
21259c5ab40SAndre Przywara };
21359c5ab40SAndre Przywara 
21410f92c4cSChristoffer Dall struct vgic_state_iter;
21510f92c4cSChristoffer Dall 
216dbd9733aSEric Auger struct vgic_redist_region {
217dbd9733aSEric Auger 	u32 index;
218dbd9733aSEric Auger 	gpa_t base;
219dbd9733aSEric Auger 	u32 count; /* number of redistributors or 0 if single region */
220dbd9733aSEric Auger 	u32 free_index; /* index of the next free redistributor */
221dbd9733aSEric Auger 	struct list_head list;
222dbd9733aSEric Auger };
223dbd9733aSEric Auger 
2247275acdfSMarc Zyngier struct vgic_dist {
225f982cf4eSMarc Zyngier 	bool			in_kernel;
2267275acdfSMarc Zyngier 	bool			ready;
22750926d82SMarc Zyngier 	bool			initialized;
2287275acdfSMarc Zyngier 
22959892136SAndre Przywara 	/* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */
23059892136SAndre Przywara 	u32			vgic_model;
23159892136SAndre Przywara 
232aa075b0fSChristoffer Dall 	/* Implementation revision as reported in the GICD_IIDR */
233aa075b0fSChristoffer Dall 	u32			implementation_rev;
23449a1a2c7SMarc Zyngier #define KVM_VGIC_IMP_REV_2	2 /* GICv2 restorable groups */
23549a1a2c7SMarc Zyngier #define KVM_VGIC_IMP_REV_3	3 /* GICv3 GICR_CTLR.{IW,CES,RWP} */
23649a1a2c7SMarc Zyngier #define KVM_VGIC_IMP_REV_LATEST	KVM_VGIC_IMP_REV_3
237aa075b0fSChristoffer Dall 
23832f8777eSChristoffer Dall 	/* Userspace can write to GICv2 IGROUPR */
23932f8777eSChristoffer Dall 	bool			v2_groups_user_writable;
24032f8777eSChristoffer Dall 
2410e4e82f1SAndre Przywara 	/* Do injected MSIs require an additional device ID? */
2420e4e82f1SAndre Przywara 	bool			msis_require_devid;
2430e4e82f1SAndre Przywara 
24450926d82SMarc Zyngier 	int			nr_spis;
245c1bfb577SMarc Zyngier 
24650926d82SMarc Zyngier 	/* base addresses in guest physical address space: */
24750926d82SMarc Zyngier 	gpa_t			vgic_dist_base;		/* distributor */
248a0675c25SAndre Przywara 	union {
24950926d82SMarc Zyngier 		/* either a GICv2 CPU interface */
25050926d82SMarc Zyngier 		gpa_t			vgic_cpu_base;
25150926d82SMarc Zyngier 		/* or a number of GICv3 redistributor regions */
252dbd9733aSEric Auger 		struct list_head rd_regions;
253a0675c25SAndre Przywara 	};
2547275acdfSMarc Zyngier 
25550926d82SMarc Zyngier 	/* distributor enabled */
25650926d82SMarc Zyngier 	bool			enabled;
2577275acdfSMarc Zyngier 
258bacf2c60SMarc Zyngier 	/* Wants SGIs without active state */
259bacf2c60SMarc Zyngier 	bool			nassgireq;
260bacf2c60SMarc Zyngier 
26150926d82SMarc Zyngier 	struct vgic_irq		*spis;
2627275acdfSMarc Zyngier 
263a9cf86f6SAndre Przywara 	struct vgic_io_device	dist_iodev;
2640aa1de57SAndre Przywara 
2651085fdc6SAndre Przywara 	bool			has_its;
266a23eaf93SGavin Shan 	bool			table_write_in_progress;
2671085fdc6SAndre Przywara 
2680aa1de57SAndre Przywara 	/*
2690aa1de57SAndre Przywara 	 * Contains the attributes and gpa of the LPI configuration table.
2700aa1de57SAndre Przywara 	 * Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share
2710aa1de57SAndre Przywara 	 * one address across all redistributors.
272bad36e4eSZenghui Yu 	 * GICv3 spec: IHI 0069E 6.1.1 "LPI Configuration tables"
2730aa1de57SAndre Przywara 	 */
2740aa1de57SAndre Przywara 	u64			propbaser;
2753802411dSAndre Przywara 
2763802411dSAndre Przywara 	/* Protects the lpi_list and the count value below. */
277fc3bc475SJulien Thierry 	raw_spinlock_t		lpi_list_lock;
2783802411dSAndre Przywara 	struct list_head	lpi_list_head;
2793802411dSAndre Przywara 	int			lpi_list_count;
28010f92c4cSChristoffer Dall 
28124cab82cSMarc Zyngier 	/* LPI translation cache */
28224cab82cSMarc Zyngier 	struct list_head	lpi_translation_cache;
28324cab82cSMarc Zyngier 
28410f92c4cSChristoffer Dall 	/* used by vgic-debug */
28510f92c4cSChristoffer Dall 	struct vgic_state_iter *iter;
28674fe55dcSMarc Zyngier 
28774fe55dcSMarc Zyngier 	/*
28874fe55dcSMarc Zyngier 	 * GICv4 ITS per-VM data, containing the IRQ domain, the VPE
28974fe55dcSMarc Zyngier 	 * array, the property table pointer as well as allocation
29074fe55dcSMarc Zyngier 	 * data. This essentially ties the Linux IRQ core and ITS
29174fe55dcSMarc Zyngier 	 * together, and avoids leaking KVM's data structures anywhere
29274fe55dcSMarc Zyngier 	 * else.
29374fe55dcSMarc Zyngier 	 */
29474fe55dcSMarc Zyngier 	struct its_vm		its_vm;
2957275acdfSMarc Zyngier };
2967275acdfSMarc Zyngier 
297eede821dSMarc Zyngier struct vgic_v2_cpu_if {
298eede821dSMarc Zyngier 	u32		vgic_hcr;
299eede821dSMarc Zyngier 	u32		vgic_vmcr;
300eede821dSMarc Zyngier 	u32		vgic_apr;
3018f186d52SMarc Zyngier 	u32		vgic_lr[VGIC_V2_MAX_LRS];
302fc5d1f1aSChristoffer Dall 
303fc5d1f1aSChristoffer Dall 	unsigned int used_lrs;
304eede821dSMarc Zyngier };
305eede821dSMarc Zyngier 
306b2fb1c0dSMarc Zyngier struct vgic_v3_cpu_if {
307b2fb1c0dSMarc Zyngier 	u32		vgic_hcr;
308b2fb1c0dSMarc Zyngier 	u32		vgic_vmcr;
3092f5fa41aSAndre Przywara 	u32		vgic_sre;	/* Restored only, change ignored */
310b2fb1c0dSMarc Zyngier 	u32		vgic_ap0r[4];
311b2fb1c0dSMarc Zyngier 	u32		vgic_ap1r[4];
312b2fb1c0dSMarc Zyngier 	u64		vgic_lr[VGIC_V3_MAX_LRS];
31374fe55dcSMarc Zyngier 
31474fe55dcSMarc Zyngier 	/*
31574fe55dcSMarc Zyngier 	 * GICv4 ITS per-VPE data, containing the doorbell IRQ, the
31674fe55dcSMarc Zyngier 	 * pending table pointer, the its_vm pointer and a few other
31774fe55dcSMarc Zyngier 	 * HW specific things. As for the its_vm structure, this is
31874fe55dcSMarc Zyngier 	 * linking the Linux IRQ subsystem and the ITS together.
31974fe55dcSMarc Zyngier 	 */
32074fe55dcSMarc Zyngier 	struct its_vpe	its_vpe;
321fc5d1f1aSChristoffer Dall 
322fc5d1f1aSChristoffer Dall 	unsigned int used_lrs;
323b2fb1c0dSMarc Zyngier };
324b2fb1c0dSMarc Zyngier 
3257275acdfSMarc Zyngier struct vgic_cpu {
3267275acdfSMarc Zyngier 	/* CPU vif control registers for world switch */
327eede821dSMarc Zyngier 	union {
328eede821dSMarc Zyngier 		struct vgic_v2_cpu_if	vgic_v2;
329b2fb1c0dSMarc Zyngier 		struct vgic_v3_cpu_if	vgic_v3;
330eede821dSMarc Zyngier 	};
3316c3d63c9SMarc Zyngier 
33250926d82SMarc Zyngier 	struct vgic_irq private_irqs[VGIC_NR_PRIVATE_IRQS];
33350926d82SMarc Zyngier 
334e08d8d29SJulien Thierry 	raw_spinlock_t ap_list_lock;	/* Protects the ap_list */
33550926d82SMarc Zyngier 
33650926d82SMarc Zyngier 	/*
33750926d82SMarc Zyngier 	 * List of IRQs that this VCPU should consider because they are either
33850926d82SMarc Zyngier 	 * Active or Pending (hence the name; AP list), or because they recently
33950926d82SMarc Zyngier 	 * were one of the two and need to be migrated off this list to another
34050926d82SMarc Zyngier 	 * VCPU.
34150926d82SMarc Zyngier 	 */
34250926d82SMarc Zyngier 	struct list_head ap_list_head;
34359f00ff9SMarc Zyngier 
3448f6cdc1cSAndre Przywara 	/*
3458f6cdc1cSAndre Przywara 	 * Members below are used with GICv3 emulation only and represent
3468f6cdc1cSAndre Przywara 	 * parts of the redistributor.
3478f6cdc1cSAndre Przywara 	 */
3488f6cdc1cSAndre Przywara 	struct vgic_io_device	rd_iodev;
349dbd9733aSEric Auger 	struct vgic_redist_region *rdreg;
35028e9d4bcSEric Auger 	u32 rdreg_index;
3514645d11fSMarc Zyngier 	atomic_t syncr_busy;
3520aa1de57SAndre Przywara 
3530aa1de57SAndre Przywara 	/* Contains the attributes and gpa of the LPI pending tables. */
3540aa1de57SAndre Przywara 	u64 pendbaser;
35594828468SMarc Zyngier 	/* GICR_CTLR.{ENABLE_LPIS,RWP} */
35694828468SMarc Zyngier 	atomic_t ctlr;
357d017d7b0SVijaya Kumar K 
358d017d7b0SVijaya Kumar K 	/* Cache guest priority bits */
359d017d7b0SVijaya Kumar K 	u32 num_pri_bits;
360d017d7b0SVijaya Kumar K 
361d017d7b0SVijaya Kumar K 	/* Cache guest interrupt ID bits */
362d017d7b0SVijaya Kumar K 	u32 num_id_bits;
3637275acdfSMarc Zyngier };
3647275acdfSMarc Zyngier 
365fb5ee369SMarc Zyngier extern struct static_key_false vgic_v2_cpuif_trap;
36659da1cbfSMarc Zyngier extern struct static_key_false vgic_v3_cpuif_trap;
367fb5ee369SMarc Zyngier 
3689f968c92SMarc Zyngier int kvm_set_legacy_vgic_v2_addr(struct kvm *kvm, struct kvm_arm_device_addr *dev_addr);
3696c3d63c9SMarc Zyngier void kvm_vgic_early_init(struct kvm *kvm);
3701aab6f46SChristoffer Dall int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
37159892136SAndre Przywara int kvm_vgic_create(struct kvm *kvm, u32 type);
372c1bfb577SMarc Zyngier void kvm_vgic_destroy(struct kvm *kvm);
373c1bfb577SMarc Zyngier void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
37450926d82SMarc Zyngier int kvm_vgic_map_resources(struct kvm *kvm);
37550926d82SMarc Zyngier int kvm_vgic_hyp_init(void);
3765b0d2cc2SChristoffer Dall void kvm_vgic_init_cpu_hardware(void);
37750926d82SMarc Zyngier 
37850926d82SMarc Zyngier int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
379cb3f0ad8SChristoffer Dall 			bool level, void *owner);
38047bbd31fSEric Auger int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, unsigned int host_irq,
381db75f1a3SMarc Zyngier 			  u32 vintid, struct irq_ops *ops);
38247bbd31fSEric Auger int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int vintid);
38381dc9504SMarc Zyngier int kvm_vgic_get_map(struct kvm_vcpu *vcpu, unsigned int vintid);
38447bbd31fSEric Auger bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid);
3857275acdfSMarc Zyngier 
38650926d82SMarc Zyngier int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
38750926d82SMarc Zyngier 
388328e5664SChristoffer Dall void kvm_vgic_load(struct kvm_vcpu *vcpu);
389328e5664SChristoffer Dall void kvm_vgic_put(struct kvm_vcpu *vcpu);
3905eeaf10eSMarc Zyngier void kvm_vgic_vmcr_sync(struct kvm_vcpu *vcpu);
391328e5664SChristoffer Dall 
392f982cf4eSMarc Zyngier #define irqchip_in_kernel(k)	(!!((k)->arch.vgic.in_kernel))
39350926d82SMarc Zyngier #define vgic_initialized(k)	((k)->arch.vgic.initialized)
394c52edf5fSChristoffer Dall #define vgic_ready(k)		((k)->arch.vgic.ready)
3952defaff4SAndre Przywara #define vgic_valid_spi(k, i)	(((i) >= VGIC_NR_PRIVATE_IRQS) && \
39650926d82SMarc Zyngier 			((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
3977275acdfSMarc Zyngier 
39850926d82SMarc Zyngier bool kvm_vcpu_has_pending_irqs(struct kvm_vcpu *vcpu);
39950926d82SMarc Zyngier void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
40050926d82SMarc Zyngier void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
401413aa807SChristoffer Dall void kvm_vgic_reset_mapped_irq(struct kvm_vcpu *vcpu, u32 vintid);
40250926d82SMarc Zyngier 
4036249f2a4SMarc Zyngier void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg, bool allow_group1);
4048f186d52SMarc Zyngier 
40550926d82SMarc Zyngier /**
40650926d82SMarc Zyngier  * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
40750926d82SMarc Zyngier  *
40850926d82SMarc Zyngier  * The host's GIC naturally limits the maximum amount of VCPUs a guest
40950926d82SMarc Zyngier  * can use.
41050926d82SMarc Zyngier  */
kvm_vgic_get_max_vcpus(void)41150926d82SMarc Zyngier static inline int kvm_vgic_get_max_vcpus(void)
41250926d82SMarc Zyngier {
41350926d82SMarc Zyngier 	return kvm_vgic_global_state.max_gic_vcpus;
41450926d82SMarc Zyngier }
41550926d82SMarc Zyngier 
416180ae7b1SEric Auger /**
417180ae7b1SEric Auger  * kvm_vgic_setup_default_irq_routing:
418180ae7b1SEric Auger  * Setup a default flat gsi routing table mapping all SPIs
419180ae7b1SEric Auger  */
420180ae7b1SEric Auger int kvm_vgic_setup_default_irq_routing(struct kvm *kvm);
421180ae7b1SEric Auger 
422c6ccd30eSChristoffer Dall int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner);
423c6ccd30eSChristoffer Dall 
424196b1364SMarc Zyngier struct kvm_kernel_irq_routing_entry;
425196b1364SMarc Zyngier 
426196b1364SMarc Zyngier int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int irq,
427196b1364SMarc Zyngier 			       struct kvm_kernel_irq_routing_entry *irq_entry);
428196b1364SMarc Zyngier 
429196b1364SMarc Zyngier int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int irq,
430196b1364SMarc Zyngier 				 struct kvm_kernel_irq_routing_entry *irq_entry);
431196b1364SMarc Zyngier 
4328e01d9a3SMarc Zyngier int vgic_v4_load(struct kvm_vcpu *vcpu);
43357e3cebdSShenming Lu void vgic_v4_commit(struct kvm_vcpu *vcpu);
434*b321c31cSMarc Zyngier int vgic_v4_put(struct kvm_vcpu *vcpu);
435df9ba959SMarc Zyngier 
436466d27e4SMarc Zyngier /* CPU HP callbacks */
437466d27e4SMarc Zyngier void kvm_vgic_cpu_up(void);
438466d27e4SMarc Zyngier void kvm_vgic_cpu_down(void);
439466d27e4SMarc Zyngier 
44050926d82SMarc Zyngier #endif /* __KVM_ARM_VGIC_H */
441