xref: /openbmc/linux/arch/x86/kvm/lapic.h (revision 199a8b84c455cfeb81bc060c6824dea473e54dc3)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
282470196SZhang Xiantao #ifndef __KVM_X86_LAPIC_H
382470196SZhang Xiantao #define __KVM_X86_LAPIC_H
482470196SZhang Xiantao 
5af669ac6SAndre Przywara #include <kvm/iodev.h>
682470196SZhang Xiantao 
782470196SZhang Xiantao #include <linux/kvm_host.h>
882470196SZhang Xiantao 
966450a21SJan Kiszka #define KVM_APIC_INIT		0
1066450a21SJan Kiszka #define KVM_APIC_SIPI		1
111e6e2755SSuravee Suthikulpanit #define KVM_APIC_LVT_NUM	6
1266450a21SJan Kiszka 
13ac8ef992SPeter Xu #define APIC_SHORT_MASK			0xc0000
14ac8ef992SPeter Xu #define APIC_DEST_NOSHORT		0x0
15ac8ef992SPeter Xu #define APIC_DEST_MASK			0x800
1618f40c53SSuravee Suthikulpanit 
1772c139baSLadi Prosek #define APIC_BUS_CYCLE_NS       1
1872c139baSLadi Prosek #define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
1972c139baSLadi Prosek 
204064a4c6SWanpeng Li #define APIC_BROADCAST			0xFF
214064a4c6SWanpeng Li #define X2APIC_BROADCAST		0xFFFFFFFFul
224064a4c6SWanpeng Li 
2358871649SJim Mattson enum lapic_mode {
2458871649SJim Mattson 	LAPIC_MODE_DISABLED = 0,
2558871649SJim Mattson 	LAPIC_MODE_INVALID = X2APIC_ENABLE,
2658871649SJim Mattson 	LAPIC_MODE_XAPIC = MSR_IA32_APICBASE_ENABLE,
2758871649SJim Mattson 	LAPIC_MODE_X2APIC = MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE,
2858871649SJim Mattson };
2958871649SJim Mattson 
30e9d90d47SAvi Kivity struct kvm_timer {
31e9d90d47SAvi Kivity 	struct hrtimer timer;
32e9d90d47SAvi Kivity 	s64 period; 				/* unit: ns */
338003c9aeSWanpeng Li 	ktime_t target_expiration;
34a323b409SRadim Krčmář 	u32 timer_mode;
35e9d90d47SAvi Kivity 	u32 timer_mode_mask;
36e9d90d47SAvi Kivity 	u64 tscdeadline;
37d0659d94SMarcelo Tosatti 	u64 expired_tscdeadline;
3839497d76SSean Christopherson 	u32 timer_advance_ns;
39ec0671d5SWanpeng Li 	s64 advance_expire_delta;
40e9d90d47SAvi Kivity 	atomic_t pending;			/* accumulated triggered timers */
41ce7a058aSYunhong Jiang 	bool hv_timer_in_use;
42e9d90d47SAvi Kivity };
43e9d90d47SAvi Kivity 
4482470196SZhang Xiantao struct kvm_lapic {
4582470196SZhang Xiantao 	unsigned long base_address;
4682470196SZhang Xiantao 	struct kvm_io_device dev;
47d3c7b77dSMarcelo Tosatti 	struct kvm_timer lapic_timer;
4882470196SZhang Xiantao 	u32 divide_count;
4982470196SZhang Xiantao 	struct kvm_vcpu *vcpu;
50e462755cSRadim Krčmář 	bool sw_enabled;
5133e4c686SGleb Natapov 	bool irr_pending;
5259fd1323SRadim Krčmář 	bool lvt0_in_nmi_mode;
538680b94bSMichael S. Tsirkin 	/* Number of bits set in ISR. */
548680b94bSMichael S. Tsirkin 	s16 isr_count;
558680b94bSMichael S. Tsirkin 	/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
568680b94bSMichael S. Tsirkin 	int highest_isr_cache;
575eadf916SMichael S. Tsirkin 	/**
585eadf916SMichael S. Tsirkin 	 * APIC register page.  The layout matches the register layout seen by
595eadf916SMichael S. Tsirkin 	 * the guest 1:1, because it is accessed by the vmx microcode.
605eadf916SMichael S. Tsirkin 	 * Note: Only one register, the TPR, is used by the microcode.
615eadf916SMichael S. Tsirkin 	 */
6282470196SZhang Xiantao 	void *regs;
63b93463aaSAvi Kivity 	gpa_t vapic_addr;
64fda4e2e8SAndy Honig 	struct gfn_to_hva_cache vapic_cache;
6566450a21SJan Kiszka 	unsigned long pending_events;
6666450a21SJan Kiszka 	unsigned int sipi_vector;
6782470196SZhang Xiantao };
689e4aabe2SJoerg Roedel 
699e4aabe2SJoerg Roedel struct dest_map;
709e4aabe2SJoerg Roedel 
71c3941d9eSSean Christopherson int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns);
7282470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu);
7382470196SZhang Xiantao 
7482470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
7582470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
7682470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
7766450a21SJan Kiszka void kvm_apic_accept_events(struct kvm_vcpu *vcpu);
78d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event);
7982470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
8082470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
8158fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
8282470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
838b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
844abaffceSWanpeng Li void kvm_recalculate_apic_map(struct kvm *kvm);
85fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu);
861e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val);
871e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
881e6e2755SSuravee Suthikulpanit 		       void *data);
891e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
905c69d5c1SPeter Xu 			   int shorthand, unsigned int dest, int dest_mode);
9159508b30SPeter Xu int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
92e7387b0eSLiran Alon bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr);
93e7387b0eSLiran Alon bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr);
94eb90f341SPaolo Bonzini void kvm_apic_update_ppr(struct kvm_vcpu *vcpu);
95b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
969e4aabe2SJoerg Roedel 		     struct dest_map *dest_map);
9789342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
98b26a695aSSuravee Suthikulpanit void kvm_apic_update_apicv(struct kvm_vcpu *vcpu);
9982470196SZhang Xiantao 
1001e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
1019e4aabe2SJoerg Roedel 		struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map);
102d5361678SWanpeng Li void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high);
1031e08ec4aSGleb Natapov 
10482470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
10558cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
106a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
107a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
10858871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu);
10982470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
11082470196SZhang Xiantao 
111a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
112a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
113a3e06bbeSLiu, Jinsong 
11483d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
115c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
11683d4c286SYang Zhang 
117fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
118b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
119b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
120b93463aaSAvi Kivity 
1210105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
1220105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
12310388a07SGleb Natapov 
12410388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
12510388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
12610388a07SGleb Natapov 
12710388a07SGleb Natapov static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu)
12810388a07SGleb Natapov {
129d4abc577SLadi Prosek 	return vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
13010388a07SGleb Natapov }
131ae7a2a3fSMichael S. Tsirkin 
13272bbf935SLadi Prosek int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
133c5cc421bSGleb Natapov void kvm_lapic_init(void);
134cef84c30SDavid Matlack void kvm_lapic_exit(void);
135c48f1496SGleb Natapov 
1361e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1))
1371e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4)
1381e6e2755SSuravee Suthikulpanit 
139ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap)
140ee171d2fSWei Yang {
141ee171d2fSWei Yang 	clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
142ee171d2fSWei Yang }
143ee171d2fSWei Yang 
1441e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap)
1451e6e2755SSuravee Suthikulpanit {
1461e6e2755SSuravee Suthikulpanit 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
1471e6e2755SSuravee Suthikulpanit }
1481e6e2755SSuravee Suthikulpanit 
1491e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic)
1501e6e2755SSuravee Suthikulpanit {
1511e6e2755SSuravee Suthikulpanit 	kvm_lapic_set_vector(vec, apic->regs + APIC_IRR);
1521e6e2755SSuravee Suthikulpanit 	/*
1531e6e2755SSuravee Suthikulpanit 	 * irr_pending must be true if any interrupt is pending; set it after
1541e6e2755SSuravee Suthikulpanit 	 * APIC_IRR to avoid race with apic_clear_irr
1551e6e2755SSuravee Suthikulpanit 	 */
1561e6e2755SSuravee Suthikulpanit 	apic->irr_pending = true;
1571e6e2755SSuravee Suthikulpanit }
1581e6e2755SSuravee Suthikulpanit 
159dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off)
160c48f1496SGleb Natapov {
161c48f1496SGleb Natapov 	return *((u32 *) (apic->regs + reg_off));
162c48f1496SGleb Natapov }
163c48f1496SGleb Natapov 
16424647e0aSPeter Shier static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val)
16524647e0aSPeter Shier {
16624647e0aSPeter Shier 	*((u32 *) (regs + reg_off)) = val;
16724647e0aSPeter Shier }
16824647e0aSPeter Shier 
1691e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
1701e6e2755SSuravee Suthikulpanit {
17124647e0aSPeter Shier 	__kvm_lapic_set_reg(apic->regs, reg_off, val);
1721e6e2755SSuravee Suthikulpanit }
1731e6e2755SSuravee Suthikulpanit 
174c48f1496SGleb Natapov extern struct static_key kvm_no_apic_vcpu;
175c48f1496SGleb Natapov 
176bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu)
177c48f1496SGleb Natapov {
178c48f1496SGleb Natapov 	if (static_key_false(&kvm_no_apic_vcpu))
179c48f1496SGleb Natapov 		return vcpu->arch.apic;
180c48f1496SGleb Natapov 	return true;
181c48f1496SGleb Natapov }
182c48f1496SGleb Natapov 
183c48f1496SGleb Natapov extern struct static_key_deferred apic_hw_disabled;
184c48f1496SGleb Natapov 
185c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic)
186c48f1496SGleb Natapov {
187c48f1496SGleb Natapov 	if (static_key_false(&apic_hw_disabled.key))
188c48f1496SGleb Natapov 		return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
189c48f1496SGleb Natapov 	return MSR_IA32_APICBASE_ENABLE;
190c48f1496SGleb Natapov }
191c48f1496SGleb Natapov 
192c48f1496SGleb Natapov extern struct static_key_deferred apic_sw_disabled;
193c48f1496SGleb Natapov 
194f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
195c48f1496SGleb Natapov {
196c48f1496SGleb Natapov 	if (static_key_false(&apic_sw_disabled.key))
197f30ebc31SRadim Krčmář 		return apic->sw_enabled;
198f30ebc31SRadim Krčmář 	return true;
199c48f1496SGleb Natapov }
200c48f1496SGleb Natapov 
201c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
202c48f1496SGleb Natapov {
203bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
204c48f1496SGleb Natapov }
205c48f1496SGleb Natapov 
206c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
207c48f1496SGleb Natapov {
208c48f1496SGleb Natapov 	return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
209c48f1496SGleb Natapov }
210c48f1496SGleb Natapov 
2118d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic)
2128d14695fSYang Zhang {
2138d14695fSYang Zhang 	return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
2148d14695fSYang Zhang }
2158d14695fSYang Zhang 
216d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu)
217c7c9c56cSYang Zhang {
218d62caabbSAndrey Smetanin 	return vcpu->arch.apic && vcpu->arch.apicv_active;
219c7c9c56cSYang Zhang }
220c7c9c56cSYang Zhang 
22166450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
22266450a21SJan Kiszka {
223bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events;
22466450a21SJan Kiszka }
22566450a21SJan Kiszka 
226d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
227d1ebdbf9SJames Sullivan {
228d1ebdbf9SJames Sullivan 	return (irq->delivery_mode == APIC_DM_LOWEST ||
229d1ebdbf9SJames Sullivan 			irq->msi_redir_hint);
230d1ebdbf9SJames Sullivan }
231d1ebdbf9SJames Sullivan 
232f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)
233f077825aSPaolo Bonzini {
234bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
235f077825aSPaolo Bonzini }
236f077825aSPaolo Bonzini 
23710606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
23810606919SYang Zhang 
239b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu);
240d0659d94SMarcelo Tosatti 
2417ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
2427ee30bc1SNitesh Narayan Lal 			      unsigned long *vcpu_bitmap);
2437ee30bc1SNitesh Narayan Lal 
2448feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
2458feb4a04SFeng Wu 			struct kvm_vcpu **dest_vcpu);
24652004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
24752004014SFeng Wu 			const unsigned long *bitmap, u32 bitmap_size);
248ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu);
249ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu);
250ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu);
251ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu);
252a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu);
253*199a8b84SPaolo Bonzini bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu);
25458871649SJim Mattson 
25558871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base)
25658871649SJim Mattson {
25758871649SJim Mattson 	return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
25858871649SJim Mattson }
25958871649SJim Mattson 
2605c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
2615c94ac5dSMiaohe Lin {
2625c94ac5dSMiaohe Lin 	return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
2635c94ac5dSMiaohe Lin }
2645c94ac5dSMiaohe Lin 
26582470196SZhang Xiantao #endif
266