xref: /openbmc/linux/arch/x86/kvm/lapic.h (revision 987f625e0799c9666ce0a0e18c2d0c001db96fad)
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 
99ff5e030SVitaly Kuznetsov #include "hyperv.h"
109ff5e030SVitaly Kuznetsov 
1166450a21SJan Kiszka #define KVM_APIC_INIT		0
1266450a21SJan Kiszka #define KVM_APIC_SIPI		1
1366450a21SJan Kiszka 
14ac8ef992SPeter Xu #define APIC_SHORT_MASK			0xc0000
15ac8ef992SPeter Xu #define APIC_DEST_NOSHORT		0x0
16ac8ef992SPeter Xu #define APIC_DEST_MASK			0x800
1718f40c53SSuravee Suthikulpanit 
1872c139baSLadi Prosek #define APIC_BUS_CYCLE_NS       1
1972c139baSLadi Prosek #define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
2072c139baSLadi Prosek 
214064a4c6SWanpeng Li #define APIC_BROADCAST			0xFF
224064a4c6SWanpeng Li #define X2APIC_BROADCAST		0xFFFFFFFFul
234064a4c6SWanpeng Li 
2458871649SJim Mattson enum lapic_mode {
2558871649SJim Mattson 	LAPIC_MODE_DISABLED = 0,
2658871649SJim Mattson 	LAPIC_MODE_INVALID = X2APIC_ENABLE,
2758871649SJim Mattson 	LAPIC_MODE_XAPIC = MSR_IA32_APICBASE_ENABLE,
2858871649SJim Mattson 	LAPIC_MODE_X2APIC = MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE,
2958871649SJim Mattson };
3058871649SJim Mattson 
311d8c681fSJue Wang enum lapic_lvt_entry {
321d8c681fSJue Wang 	LVT_TIMER,
331d8c681fSJue Wang 	LVT_THERMAL_MONITOR,
341d8c681fSJue Wang 	LVT_PERFORMANCE_COUNTER,
351d8c681fSJue Wang 	LVT_LINT0,
361d8c681fSJue Wang 	LVT_LINT1,
371d8c681fSJue Wang 	LVT_ERROR,
381d8c681fSJue Wang 
391d8c681fSJue Wang 	KVM_APIC_MAX_NR_LVT_ENTRIES,
401d8c681fSJue Wang };
411d8c681fSJue Wang 
42*987f625eSJue Wang #define APIC_LVTx(x) (APIC_LVTT + 0x10 * (x))
43*987f625eSJue Wang 
44e9d90d47SAvi Kivity struct kvm_timer {
45e9d90d47SAvi Kivity 	struct hrtimer timer;
46e9d90d47SAvi Kivity 	s64 period; 				/* unit: ns */
478003c9aeSWanpeng Li 	ktime_t target_expiration;
48a323b409SRadim Krčmář 	u32 timer_mode;
49e9d90d47SAvi Kivity 	u32 timer_mode_mask;
50e9d90d47SAvi Kivity 	u64 tscdeadline;
51d0659d94SMarcelo Tosatti 	u64 expired_tscdeadline;
5239497d76SSean Christopherson 	u32 timer_advance_ns;
53e9d90d47SAvi Kivity 	atomic_t pending;			/* accumulated triggered timers */
54ce7a058aSYunhong Jiang 	bool hv_timer_in_use;
55e9d90d47SAvi Kivity };
56e9d90d47SAvi Kivity 
5782470196SZhang Xiantao struct kvm_lapic {
5882470196SZhang Xiantao 	unsigned long base_address;
5982470196SZhang Xiantao 	struct kvm_io_device dev;
60d3c7b77dSMarcelo Tosatti 	struct kvm_timer lapic_timer;
6182470196SZhang Xiantao 	u32 divide_count;
6282470196SZhang Xiantao 	struct kvm_vcpu *vcpu;
63ce0a58f4SSean Christopherson 	bool apicv_active;
64e462755cSRadim Krčmář 	bool sw_enabled;
6533e4c686SGleb Natapov 	bool irr_pending;
6659fd1323SRadim Krčmář 	bool lvt0_in_nmi_mode;
678680b94bSMichael S. Tsirkin 	/* Number of bits set in ISR. */
688680b94bSMichael S. Tsirkin 	s16 isr_count;
698680b94bSMichael S. Tsirkin 	/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
708680b94bSMichael S. Tsirkin 	int highest_isr_cache;
715eadf916SMichael S. Tsirkin 	/**
725eadf916SMichael S. Tsirkin 	 * APIC register page.  The layout matches the register layout seen by
735eadf916SMichael S. Tsirkin 	 * the guest 1:1, because it is accessed by the vmx microcode.
745eadf916SMichael S. Tsirkin 	 * Note: Only one register, the TPR, is used by the microcode.
755eadf916SMichael S. Tsirkin 	 */
7682470196SZhang Xiantao 	void *regs;
77b93463aaSAvi Kivity 	gpa_t vapic_addr;
78fda4e2e8SAndy Honig 	struct gfn_to_hva_cache vapic_cache;
7966450a21SJan Kiszka 	unsigned long pending_events;
8066450a21SJan Kiszka 	unsigned int sipi_vector;
8182470196SZhang Xiantao };
829e4aabe2SJoerg Roedel 
839e4aabe2SJoerg Roedel struct dest_map;
849e4aabe2SJoerg Roedel 
85c3941d9eSSean Christopherson int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns);
8682470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu);
8782470196SZhang Xiantao 
8882470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
8982470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
9082470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
914fe09bcfSJim Mattson int kvm_apic_accept_events(struct kvm_vcpu *vcpu);
92d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event);
9382470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
9482470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
9558fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
9682470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
978b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
984abaffceSWanpeng Li void kvm_recalculate_apic_map(struct kvm *kvm);
99fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu);
1001e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1015c69d5c1SPeter Xu 			   int shorthand, unsigned int dest, int dest_mode);
10259508b30SPeter Xu int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
10325bb2cf9SSean Christopherson void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec);
104e7387b0eSLiran Alon bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr);
105e7387b0eSLiran Alon bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr);
106eb90f341SPaolo Bonzini void kvm_apic_update_ppr(struct kvm_vcpu *vcpu);
107b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
1089e4aabe2SJoerg Roedel 		     struct dest_map *dest_map);
10989342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
110b26a695aSSuravee Suthikulpanit void kvm_apic_update_apicv(struct kvm_vcpu *vcpu);
11182470196SZhang Xiantao 
1121e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
1139e4aabe2SJoerg Roedel 		struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map);
114d5361678SWanpeng Li void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high);
1151e08ec4aSGleb Natapov 
11682470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
11758cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
118a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
119a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
12058871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu);
12182470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
12282470196SZhang Xiantao 
123a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
124a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
125a3e06bbeSLiu, Jinsong 
12683d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
127c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
12883d4c286SYang Zhang 
129fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
130b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
131b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
132b93463aaSAvi Kivity 
133b9964ee3SSean Christopherson int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data);
1340105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
1350105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
13610388a07SGleb Natapov 
13710388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
13810388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
13910388a07SGleb Natapov 
14077c3323fSVitaly Kuznetsov int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
141cef84c30SDavid Matlack void kvm_lapic_exit(void);
142c48f1496SGleb Natapov 
1431e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1))
1441e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4)
1451e6e2755SSuravee Suthikulpanit 
146ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap)
147ee171d2fSWei Yang {
148ee171d2fSWei Yang 	clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
149ee171d2fSWei Yang }
150ee171d2fSWei Yang 
1511e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap)
1521e6e2755SSuravee Suthikulpanit {
1531e6e2755SSuravee Suthikulpanit 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
1541e6e2755SSuravee Suthikulpanit }
1551e6e2755SSuravee Suthikulpanit 
1561e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic)
1571e6e2755SSuravee Suthikulpanit {
1581e6e2755SSuravee Suthikulpanit 	kvm_lapic_set_vector(vec, apic->regs + APIC_IRR);
1591e6e2755SSuravee Suthikulpanit 	/*
1601e6e2755SSuravee Suthikulpanit 	 * irr_pending must be true if any interrupt is pending; set it after
1611e6e2755SSuravee Suthikulpanit 	 * APIC_IRR to avoid race with apic_clear_irr
1621e6e2755SSuravee Suthikulpanit 	 */
1631e6e2755SSuravee Suthikulpanit 	apic->irr_pending = true;
1641e6e2755SSuravee Suthikulpanit }
1651e6e2755SSuravee Suthikulpanit 
166b9964ee3SSean Christopherson static inline u32 __kvm_lapic_get_reg(char *regs, int reg_off)
167b9964ee3SSean Christopherson {
168b9964ee3SSean Christopherson 	return *((u32 *) (regs + reg_off));
169b9964ee3SSean Christopherson }
170b9964ee3SSean Christopherson 
171dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off)
172c48f1496SGleb Natapov {
173b9964ee3SSean Christopherson 	return __kvm_lapic_get_reg(apic->regs, reg_off);
1741e6e2755SSuravee Suthikulpanit }
1751e6e2755SSuravee Suthikulpanit 
1766e4e3b4dSCun Li DECLARE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
177c48f1496SGleb Natapov 
178bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu)
179c48f1496SGleb Natapov {
1806e4e3b4dSCun Li 	if (static_branch_unlikely(&kvm_has_noapic_vcpu))
181c48f1496SGleb Natapov 		return vcpu->arch.apic;
182c48f1496SGleb Natapov 	return true;
183c48f1496SGleb Natapov }
184c48f1496SGleb Natapov 
1856e4e3b4dSCun Li extern struct static_key_false_deferred apic_hw_disabled;
186c48f1496SGleb Natapov 
187c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic)
188c48f1496SGleb Natapov {
1896e4e3b4dSCun Li 	if (static_branch_unlikely(&apic_hw_disabled.key))
190c48f1496SGleb Natapov 		return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
191c48f1496SGleb Natapov 	return MSR_IA32_APICBASE_ENABLE;
192c48f1496SGleb Natapov }
193c48f1496SGleb Natapov 
1946e4e3b4dSCun Li extern struct static_key_false_deferred apic_sw_disabled;
195c48f1496SGleb Natapov 
196f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
197c48f1496SGleb Natapov {
1986e4e3b4dSCun Li 	if (static_branch_unlikely(&apic_sw_disabled.key))
199f30ebc31SRadim Krčmář 		return apic->sw_enabled;
200f30ebc31SRadim Krčmář 	return true;
201c48f1496SGleb Natapov }
202c48f1496SGleb Natapov 
203c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
204c48f1496SGleb Natapov {
205bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
206c48f1496SGleb Natapov }
207c48f1496SGleb Natapov 
208c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
209c48f1496SGleb Natapov {
210c48f1496SGleb Natapov 	return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
211c48f1496SGleb Natapov }
212c48f1496SGleb Natapov 
2138d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic)
2148d14695fSYang Zhang {
2158d14695fSYang Zhang 	return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
2168d14695fSYang Zhang }
2178d14695fSYang Zhang 
218d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu)
219c7c9c56cSYang Zhang {
220b8e1b962SSean Christopherson 	return lapic_in_kernel(vcpu) && vcpu->arch.apic->apicv_active;
221c7c9c56cSYang Zhang }
222c7c9c56cSYang Zhang 
22366450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
22466450a21SJan Kiszka {
225bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events;
22666450a21SJan Kiszka }
22766450a21SJan Kiszka 
228d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
229d1ebdbf9SJames Sullivan {
230d1ebdbf9SJames Sullivan 	return (irq->delivery_mode == APIC_DM_LOWEST ||
231d1ebdbf9SJames Sullivan 			irq->msi_redir_hint);
232d1ebdbf9SJames Sullivan }
233d1ebdbf9SJames Sullivan 
234f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)
235f077825aSPaolo Bonzini {
236bce87cceSPaolo Bonzini 	return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
237f077825aSPaolo Bonzini }
238f077825aSPaolo Bonzini 
23910606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
24010606919SYang Zhang 
241b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu);
242d0659d94SMarcelo Tosatti 
2437ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
2447ee30bc1SNitesh Narayan Lal 			      unsigned long *vcpu_bitmap);
2457ee30bc1SNitesh Narayan Lal 
2468feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
2478feb4a04SFeng Wu 			struct kvm_vcpu **dest_vcpu);
24852004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
24952004014SFeng Wu 			const unsigned long *bitmap, u32 bitmap_size);
250ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu);
251ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu);
252ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu);
253ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu);
254a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu);
255199a8b84SPaolo Bonzini bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu);
25658871649SJim Mattson 
25758871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base)
25858871649SJim Mattson {
25958871649SJim Mattson 	return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
26058871649SJim Mattson }
26158871649SJim Mattson 
2625c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
2635c94ac5dSMiaohe Lin {
2645c94ac5dSMiaohe Lin 	return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
2655c94ac5dSMiaohe Lin }
2665c94ac5dSMiaohe Lin 
26782470196SZhang Xiantao #endif
268