xref: /openbmc/linux/arch/x86/kvm/lapic.h (revision d1ebdbf99a3ce90f3b886c2cf0dfd7da17703d2a)
182470196SZhang Xiantao #ifndef __KVM_X86_LAPIC_H
282470196SZhang Xiantao #define __KVM_X86_LAPIC_H
382470196SZhang Xiantao 
4af669ac6SAndre Przywara #include <kvm/iodev.h>
582470196SZhang Xiantao 
682470196SZhang Xiantao #include <linux/kvm_host.h>
782470196SZhang Xiantao 
866450a21SJan Kiszka #define KVM_APIC_INIT		0
966450a21SJan Kiszka #define KVM_APIC_SIPI		1
1066450a21SJan Kiszka 
11e9d90d47SAvi Kivity struct kvm_timer {
12e9d90d47SAvi Kivity 	struct hrtimer timer;
13e9d90d47SAvi Kivity 	s64 period; 				/* unit: ns */
14a323b409SRadim Krčmář 	u32 timer_mode;
15e9d90d47SAvi Kivity 	u32 timer_mode_mask;
16e9d90d47SAvi Kivity 	u64 tscdeadline;
17d0659d94SMarcelo Tosatti 	u64 expired_tscdeadline;
18e9d90d47SAvi Kivity 	atomic_t pending;			/* accumulated triggered timers */
19e9d90d47SAvi Kivity };
20e9d90d47SAvi Kivity 
2182470196SZhang Xiantao struct kvm_lapic {
2282470196SZhang Xiantao 	unsigned long base_address;
2382470196SZhang Xiantao 	struct kvm_io_device dev;
24d3c7b77dSMarcelo Tosatti 	struct kvm_timer lapic_timer;
2582470196SZhang Xiantao 	u32 divide_count;
2682470196SZhang Xiantao 	struct kvm_vcpu *vcpu;
27e462755cSRadim Krčmář 	bool sw_enabled;
2833e4c686SGleb Natapov 	bool irr_pending;
298680b94bSMichael S. Tsirkin 	/* Number of bits set in ISR. */
308680b94bSMichael S. Tsirkin 	s16 isr_count;
318680b94bSMichael S. Tsirkin 	/* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
328680b94bSMichael S. Tsirkin 	int highest_isr_cache;
335eadf916SMichael S. Tsirkin 	/**
345eadf916SMichael S. Tsirkin 	 * APIC register page.  The layout matches the register layout seen by
355eadf916SMichael S. Tsirkin 	 * the guest 1:1, because it is accessed by the vmx microcode.
365eadf916SMichael S. Tsirkin 	 * Note: Only one register, the TPR, is used by the microcode.
375eadf916SMichael S. Tsirkin 	 */
3882470196SZhang Xiantao 	void *regs;
39b93463aaSAvi Kivity 	gpa_t vapic_addr;
40fda4e2e8SAndy Honig 	struct gfn_to_hva_cache vapic_cache;
4166450a21SJan Kiszka 	unsigned long pending_events;
4266450a21SJan Kiszka 	unsigned int sipi_vector;
4382470196SZhang Xiantao };
4482470196SZhang Xiantao int kvm_create_lapic(struct kvm_vcpu *vcpu);
4582470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu);
4682470196SZhang Xiantao 
4782470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
4882470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
4982470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
5066450a21SJan Kiszka void kvm_apic_accept_events(struct kvm_vcpu *vcpu);
51d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event);
5282470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
5382470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
5458fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
5582470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
568b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
57fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu);
5882470196SZhang Xiantao 
59cf9e65b7SYang Zhang void kvm_apic_update_tmr(struct kvm_vcpu *vcpu, u32 *tmr);
60705699a1SWincy Van void __kvm_apic_update_irr(u32 *pir, void *regs);
61a20ed54dSYang Zhang void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir);
62b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
63b4f2225cSYang Zhang 		unsigned long *dest_map);
6489342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
6582470196SZhang Xiantao 
661e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
67b4f2225cSYang Zhang 		struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map);
681e08ec4aSGleb Natapov 
6982470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
7058cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
7164eb0620SGleb Natapov void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
7264eb0620SGleb Natapov 		struct kvm_lapic_state *s);
7382470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
7482470196SZhang Xiantao 
75a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
76a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
77a3e06bbeSLiu, Jinsong 
7883d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
79c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
8083d4c286SYang Zhang 
81fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
82b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
83b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
84b93463aaSAvi Kivity 
850105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
860105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
8710388a07SGleb Natapov 
8810388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
8910388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
9010388a07SGleb Natapov 
9110388a07SGleb Natapov static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu)
9210388a07SGleb Natapov {
9310388a07SGleb Natapov 	return vcpu->arch.hv_vapic & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE;
9410388a07SGleb Natapov }
95ae7a2a3fSMichael S. Tsirkin 
96ae7a2a3fSMichael S. Tsirkin int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data);
97c5cc421bSGleb Natapov void kvm_lapic_init(void);
98c48f1496SGleb Natapov 
99c48f1496SGleb Natapov static inline u32 kvm_apic_get_reg(struct kvm_lapic *apic, int reg_off)
100c48f1496SGleb Natapov {
101c48f1496SGleb Natapov 	        return *((u32 *) (apic->regs + reg_off));
102c48f1496SGleb Natapov }
103c48f1496SGleb Natapov 
104c48f1496SGleb Natapov extern struct static_key kvm_no_apic_vcpu;
105c48f1496SGleb Natapov 
106c48f1496SGleb Natapov static inline bool kvm_vcpu_has_lapic(struct kvm_vcpu *vcpu)
107c48f1496SGleb Natapov {
108c48f1496SGleb Natapov 	if (static_key_false(&kvm_no_apic_vcpu))
109c48f1496SGleb Natapov 		return vcpu->arch.apic;
110c48f1496SGleb Natapov 	return true;
111c48f1496SGleb Natapov }
112c48f1496SGleb Natapov 
113c48f1496SGleb Natapov extern struct static_key_deferred apic_hw_disabled;
114c48f1496SGleb Natapov 
115c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic)
116c48f1496SGleb Natapov {
117c48f1496SGleb Natapov 	if (static_key_false(&apic_hw_disabled.key))
118c48f1496SGleb Natapov 		return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
119c48f1496SGleb Natapov 	return MSR_IA32_APICBASE_ENABLE;
120c48f1496SGleb Natapov }
121c48f1496SGleb Natapov 
122c48f1496SGleb Natapov extern struct static_key_deferred apic_sw_disabled;
123c48f1496SGleb Natapov 
124f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
125c48f1496SGleb Natapov {
126c48f1496SGleb Natapov 	if (static_key_false(&apic_sw_disabled.key))
127f30ebc31SRadim Krčmář 		return apic->sw_enabled;
128f30ebc31SRadim Krčmář 	return true;
129c48f1496SGleb Natapov }
130c48f1496SGleb Natapov 
131c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
132c48f1496SGleb Natapov {
133c48f1496SGleb Natapov 	return kvm_vcpu_has_lapic(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
134c48f1496SGleb Natapov }
135c48f1496SGleb Natapov 
136c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
137c48f1496SGleb Natapov {
138c48f1496SGleb Natapov 	return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
139c48f1496SGleb Natapov }
140c48f1496SGleb Natapov 
1418d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic)
1428d14695fSYang Zhang {
1438d14695fSYang Zhang 	return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
1448d14695fSYang Zhang }
1458d14695fSYang Zhang 
146c7c9c56cSYang Zhang static inline bool kvm_apic_vid_enabled(struct kvm *kvm)
147c7c9c56cSYang Zhang {
148c7c9c56cSYang Zhang 	return kvm_x86_ops->vm_has_apicv(kvm);
149c7c9c56cSYang Zhang }
150c7c9c56cSYang Zhang 
15166450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
15266450a21SJan Kiszka {
15366450a21SJan Kiszka 	return vcpu->arch.apic->pending_events;
15466450a21SJan Kiszka }
15566450a21SJan Kiszka 
156*d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
157*d1ebdbf9SJames Sullivan {
158*d1ebdbf9SJames Sullivan 	return (irq->delivery_mode == APIC_DM_LOWEST ||
159*d1ebdbf9SJames Sullivan 			irq->msi_redir_hint);
160*d1ebdbf9SJames Sullivan }
161*d1ebdbf9SJames Sullivan 
16210606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
16310606919SYang Zhang 
164d0659d94SMarcelo Tosatti void wait_lapic_expire(struct kvm_vcpu *vcpu);
165d0659d94SMarcelo Tosatti 
16682470196SZhang Xiantao #endif
167