xref: /openbmc/linux/arch/x86/kvm/lapic.h (revision b26a695a1d78cc415fe26d74d0463f5d887980de)
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 
2058871649SJim Mattson enum lapic_mode {
2158871649SJim Mattson 	LAPIC_MODE_DISABLED = 0,
2258871649SJim Mattson 	LAPIC_MODE_INVALID = X2APIC_ENABLE,
2358871649SJim Mattson 	LAPIC_MODE_XAPIC = MSR_IA32_APICBASE_ENABLE,
2458871649SJim Mattson 	LAPIC_MODE_X2APIC = MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE,
2558871649SJim Mattson };
2658871649SJim Mattson 
27e9d90d47SAvi Kivity struct kvm_timer {
28e9d90d47SAvi Kivity 	struct hrtimer timer;
29e9d90d47SAvi Kivity 	s64 period; 				/* unit: ns */
308003c9aeSWanpeng Li 	ktime_t target_expiration;
31a323b409SRadim Krčmář 	u32 timer_mode;
32e9d90d47SAvi Kivity 	u32 timer_mode_mask;
33e9d90d47SAvi Kivity 	u64 tscdeadline;
34d0659d94SMarcelo Tosatti 	u64 expired_tscdeadline;
3539497d76SSean Christopherson 	u32 timer_advance_ns;
36ec0671d5SWanpeng Li 	s64 advance_expire_delta;
37e9d90d47SAvi Kivity 	atomic_t pending;			/* accumulated triggered timers */
38ce7a058aSYunhong Jiang 	bool hv_timer_in_use;
39e9d90d47SAvi Kivity };
40e9d90d47SAvi Kivity 
4182470196SZhang Xiantao struct kvm_lapic {
4282470196SZhang Xiantao 	unsigned long base_address;
4382470196SZhang Xiantao 	struct kvm_io_device dev;
44d3c7b77dSMarcelo Tosatti 	struct kvm_timer lapic_timer;
4582470196SZhang Xiantao 	u32 divide_count;
4682470196SZhang Xiantao 	struct kvm_vcpu *vcpu;
47e462755cSRadim Krčmář 	bool sw_enabled;
4833e4c686SGleb Natapov 	bool irr_pending;
4959fd1323SRadim Krčmář 	bool lvt0_in_nmi_mode;
508680b94bSMichael S. Tsirkin 	/* Number of bits set in ISR. */
518680b94bSMichael S. Tsirkin 	s16 isr_count;
528680b94bSMichael S. Tsirkin 	/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
538680b94bSMichael S. Tsirkin 	int highest_isr_cache;
545eadf916SMichael S. Tsirkin 	/**
555eadf916SMichael S. Tsirkin 	 * APIC register page.  The layout matches the register layout seen by
565eadf916SMichael S. Tsirkin 	 * the guest 1:1, because it is accessed by the vmx microcode.
575eadf916SMichael S. Tsirkin 	 * Note: Only one register, the TPR, is used by the microcode.
585eadf916SMichael S. Tsirkin 	 */
5982470196SZhang Xiantao 	void *regs;
60b93463aaSAvi Kivity 	gpa_t vapic_addr;
61fda4e2e8SAndy Honig 	struct gfn_to_hva_cache vapic_cache;
6266450a21SJan Kiszka 	unsigned long pending_events;
6366450a21SJan Kiszka 	unsigned int sipi_vector;
6482470196SZhang Xiantao };
659e4aabe2SJoerg Roedel 
669e4aabe2SJoerg Roedel struct dest_map;
679e4aabe2SJoerg Roedel 
68c3941d9eSSean Christopherson int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns);
6982470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu);
7082470196SZhang Xiantao 
7182470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
7282470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
7382470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
7466450a21SJan Kiszka void kvm_apic_accept_events(struct kvm_vcpu *vcpu);
75d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event);
7682470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
7782470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
7858fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
7982470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
808b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
81fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu);
821e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val);
831e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len,
841e6e2755SSuravee Suthikulpanit 		       void *data);
851e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
865c69d5c1SPeter Xu 			   int shorthand, unsigned int dest, int dest_mode);
8759508b30SPeter Xu int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
88e7387b0eSLiran Alon bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr);
89e7387b0eSLiran Alon bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr);
90eb90f341SPaolo Bonzini void kvm_apic_update_ppr(struct kvm_vcpu *vcpu);
91b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
929e4aabe2SJoerg Roedel 		     struct dest_map *dest_map);
9389342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
94*b26a695aSSuravee Suthikulpanit void kvm_apic_update_apicv(struct kvm_vcpu *vcpu);
9582470196SZhang Xiantao 
961e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
979e4aabe2SJoerg Roedel 		struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map);
981e08ec4aSGleb Natapov 
9982470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
10058cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
101a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
102a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
10358871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu);
10482470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
10582470196SZhang Xiantao 
106a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
107a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
108a3e06bbeSLiu, Jinsong 
10983d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
110c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
11183d4c286SYang Zhang 
112fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
113b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
114b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
115b93463aaSAvi Kivity 
1160105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
1170105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
11810388a07SGleb Natapov 
11910388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
12010388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
12110388a07SGleb Natapov 
12210388a07SGleb Natapov static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu)
12310388a07SGleb Natapov {
124d4abc577SLadi Prosek 	return vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
12510388a07SGleb Natapov }
126ae7a2a3fSMichael S. Tsirkin 
12772bbf935SLadi Prosek int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
128c5cc421bSGleb Natapov void kvm_lapic_init(void);
129cef84c30SDavid Matlack void kvm_lapic_exit(void);
130c48f1496SGleb Natapov 
1311e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1))
1321e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4)
1331e6e2755SSuravee Suthikulpanit 
134ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap)
135ee171d2fSWei Yang {
136ee171d2fSWei Yang 	clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
137ee171d2fSWei Yang }
138ee171d2fSWei Yang 
1391e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap)
1401e6e2755SSuravee Suthikulpanit {
1411e6e2755SSuravee Suthikulpanit 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
1421e6e2755SSuravee Suthikulpanit }
1431e6e2755SSuravee Suthikulpanit 
1441e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic)
1451e6e2755SSuravee Suthikulpanit {
1461e6e2755SSuravee Suthikulpanit 	kvm_lapic_set_vector(vec, apic->regs + APIC_IRR);
1471e6e2755SSuravee Suthikulpanit 	/*
1481e6e2755SSuravee Suthikulpanit 	 * irr_pending must be true if any interrupt is pending; set it after
1491e6e2755SSuravee Suthikulpanit 	 * APIC_IRR to avoid race with apic_clear_irr
1501e6e2755SSuravee Suthikulpanit 	 */
1511e6e2755SSuravee Suthikulpanit 	apic->irr_pending = true;
1521e6e2755SSuravee Suthikulpanit }
1531e6e2755SSuravee Suthikulpanit 
154dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off)
155c48f1496SGleb Natapov {
156c48f1496SGleb Natapov 	return *((u32 *) (apic->regs + reg_off));
157c48f1496SGleb Natapov }
158c48f1496SGleb Natapov 
1591e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
1601e6e2755SSuravee Suthikulpanit {
1611e6e2755SSuravee Suthikulpanit 	*((u32 *) (apic->regs + reg_off)) = val;
1621e6e2755SSuravee Suthikulpanit }
1631e6e2755SSuravee Suthikulpanit 
164c48f1496SGleb Natapov extern struct static_key kvm_no_apic_vcpu;
165c48f1496SGleb Natapov 
166bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu)
167c48f1496SGleb Natapov {
168c48f1496SGleb Natapov 	if (static_key_false(&kvm_no_apic_vcpu))
169c48f1496SGleb Natapov 		return vcpu->arch.apic;
170c48f1496SGleb Natapov 	return true;
171c48f1496SGleb Natapov }
172c48f1496SGleb Natapov 
173c48f1496SGleb Natapov extern struct static_key_deferred apic_hw_disabled;
174c48f1496SGleb Natapov 
175c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic)
176c48f1496SGleb Natapov {
177c48f1496SGleb Natapov 	if (static_key_false(&apic_hw_disabled.key))
178c48f1496SGleb Natapov 		return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
179c48f1496SGleb Natapov 	return MSR_IA32_APICBASE_ENABLE;
180c48f1496SGleb Natapov }
181c48f1496SGleb Natapov 
182c48f1496SGleb Natapov extern struct static_key_deferred apic_sw_disabled;
183c48f1496SGleb Natapov 
184f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
185c48f1496SGleb Natapov {
186c48f1496SGleb Natapov 	if (static_key_false(&apic_sw_disabled.key))
187f30ebc31SRadim Krčmář 		return apic->sw_enabled;
188f30ebc31SRadim Krčmář 	return true;
189c48f1496SGleb Natapov }
190c48f1496SGleb Natapov 
191c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
192c48f1496SGleb Natapov {
193bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
194c48f1496SGleb Natapov }
195c48f1496SGleb Natapov 
196c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
197c48f1496SGleb Natapov {
198c48f1496SGleb Natapov 	return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
199c48f1496SGleb Natapov }
200c48f1496SGleb Natapov 
2018d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic)
2028d14695fSYang Zhang {
2038d14695fSYang Zhang 	return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
2048d14695fSYang Zhang }
2058d14695fSYang Zhang 
206d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu)
207c7c9c56cSYang Zhang {
208d62caabbSAndrey Smetanin 	return vcpu->arch.apic && vcpu->arch.apicv_active;
209c7c9c56cSYang Zhang }
210c7c9c56cSYang Zhang 
21166450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
21266450a21SJan Kiszka {
213bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events;
21466450a21SJan Kiszka }
21566450a21SJan Kiszka 
216d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
217d1ebdbf9SJames Sullivan {
218d1ebdbf9SJames Sullivan 	return (irq->delivery_mode == APIC_DM_LOWEST ||
219d1ebdbf9SJames Sullivan 			irq->msi_redir_hint);
220d1ebdbf9SJames Sullivan }
221d1ebdbf9SJames Sullivan 
222f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)
223f077825aSPaolo Bonzini {
224bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
225f077825aSPaolo Bonzini }
226f077825aSPaolo Bonzini 
22710606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
22810606919SYang Zhang 
229b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu);
230d0659d94SMarcelo Tosatti 
2317ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
2327ee30bc1SNitesh Narayan Lal 			      unsigned long *vcpu_bitmap);
2337ee30bc1SNitesh Narayan Lal 
2348feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
2358feb4a04SFeng Wu 			struct kvm_vcpu **dest_vcpu);
23652004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
23752004014SFeng Wu 			const unsigned long *bitmap, u32 bitmap_size);
238ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu);
239ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu);
240ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu);
241ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu);
242a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu);
2430c5f81daSWanpeng Li bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu);
24458871649SJim Mattson 
24558871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base)
24658871649SJim Mattson {
24758871649SJim Mattson 	return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
24858871649SJim Mattson }
24958871649SJim Mattson 
2505c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
2515c94ac5dSMiaohe Lin {
2525c94ac5dSMiaohe Lin 	return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
2535c94ac5dSMiaohe Lin }
2545c94ac5dSMiaohe Lin 
25582470196SZhang Xiantao #endif
256