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"
10b0b42197SPaolo Bonzini #include "smm.h"
119ff5e030SVitaly Kuznetsov
1266450a21SJan Kiszka #define KVM_APIC_INIT 0
1366450a21SJan Kiszka #define KVM_APIC_SIPI 1
1466450a21SJan Kiszka
15ac8ef992SPeter Xu #define APIC_SHORT_MASK 0xc0000
16ac8ef992SPeter Xu #define APIC_DEST_NOSHORT 0x0
17ac8ef992SPeter Xu #define APIC_DEST_MASK 0x800
1818f40c53SSuravee Suthikulpanit
1972c139baSLadi Prosek #define APIC_BUS_CYCLE_NS 1
2072c139baSLadi Prosek #define APIC_BUS_FREQUENCY (1000000000ULL / APIC_BUS_CYCLE_NS)
2172c139baSLadi Prosek
224064a4c6SWanpeng Li #define APIC_BROADCAST 0xFF
234064a4c6SWanpeng Li #define X2APIC_BROADCAST 0xFFFFFFFFul
244064a4c6SWanpeng Li
2558871649SJim Mattson enum lapic_mode {
2658871649SJim Mattson LAPIC_MODE_DISABLED = 0,
2758871649SJim Mattson LAPIC_MODE_INVALID = X2APIC_ENABLE,
2858871649SJim Mattson LAPIC_MODE_XAPIC = MSR_IA32_APICBASE_ENABLE,
2958871649SJim Mattson LAPIC_MODE_X2APIC = MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE,
3058871649SJim Mattson };
3158871649SJim Mattson
321d8c681fSJue Wang enum lapic_lvt_entry {
331d8c681fSJue Wang LVT_TIMER,
341d8c681fSJue Wang LVT_THERMAL_MONITOR,
351d8c681fSJue Wang LVT_PERFORMANCE_COUNTER,
361d8c681fSJue Wang LVT_LINT0,
371d8c681fSJue Wang LVT_LINT1,
381d8c681fSJue Wang LVT_ERROR,
394b903561SJue Wang LVT_CMCI,
401d8c681fSJue Wang
411d8c681fSJue Wang KVM_APIC_MAX_NR_LVT_ENTRIES,
421d8c681fSJue Wang };
431d8c681fSJue Wang
444b903561SJue Wang #define APIC_LVTx(x) ((x) == LVT_CMCI ? APIC_LVTCMCI : APIC_LVTT + 0x10 * (x))
45987f625eSJue Wang
46e9d90d47SAvi Kivity struct kvm_timer {
47e9d90d47SAvi Kivity struct hrtimer timer;
48e9d90d47SAvi Kivity s64 period; /* unit: ns */
498003c9aeSWanpeng Li ktime_t target_expiration;
50a323b409SRadim Krčmář u32 timer_mode;
51e9d90d47SAvi Kivity u32 timer_mode_mask;
52e9d90d47SAvi Kivity u64 tscdeadline;
53d0659d94SMarcelo Tosatti u64 expired_tscdeadline;
5439497d76SSean Christopherson u32 timer_advance_ns;
55e9d90d47SAvi Kivity atomic_t pending; /* accumulated triggered timers */
56ce7a058aSYunhong Jiang bool hv_timer_in_use;
57e9d90d47SAvi Kivity };
58e9d90d47SAvi Kivity
5982470196SZhang Xiantao struct kvm_lapic {
6082470196SZhang Xiantao unsigned long base_address;
6182470196SZhang Xiantao struct kvm_io_device dev;
62d3c7b77dSMarcelo Tosatti struct kvm_timer lapic_timer;
6382470196SZhang Xiantao u32 divide_count;
6482470196SZhang Xiantao struct kvm_vcpu *vcpu;
65ce0a58f4SSean Christopherson bool apicv_active;
66e462755cSRadim Krčmář bool sw_enabled;
6733e4c686SGleb Natapov bool irr_pending;
6859fd1323SRadim Krčmář bool lvt0_in_nmi_mode;
698680b94bSMichael S. Tsirkin /* Number of bits set in ISR. */
708680b94bSMichael S. Tsirkin s16 isr_count;
718680b94bSMichael S. Tsirkin /* The highest vector set in ISR; if -1 - invalid, must scan ISR. */
728680b94bSMichael S. Tsirkin int highest_isr_cache;
735eadf916SMichael S. Tsirkin /**
745eadf916SMichael S. Tsirkin * APIC register page. The layout matches the register layout seen by
755eadf916SMichael S. Tsirkin * the guest 1:1, because it is accessed by the vmx microcode.
765eadf916SMichael S. Tsirkin * Note: Only one register, the TPR, is used by the microcode.
775eadf916SMichael S. Tsirkin */
7882470196SZhang Xiantao void *regs;
79b93463aaSAvi Kivity gpa_t vapic_addr;
80fda4e2e8SAndy Honig struct gfn_to_hva_cache vapic_cache;
8166450a21SJan Kiszka unsigned long pending_events;
8266450a21SJan Kiszka unsigned int sipi_vector;
834b903561SJue Wang int nr_lvt_entries;
8482470196SZhang Xiantao };
859e4aabe2SJoerg Roedel
869e4aabe2SJoerg Roedel struct dest_map;
879e4aabe2SJoerg Roedel
88c3941d9eSSean Christopherson int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns);
8982470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu);
9082470196SZhang Xiantao
9182470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
9282470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
9382470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
944fe09bcfSJim Mattson int kvm_apic_accept_events(struct kvm_vcpu *vcpu);
95d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event);
9682470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
9782470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
9858fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu);
9982470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value);
1008b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu);
1014abaffceSWanpeng Li void kvm_recalculate_apic_map(struct kvm *kvm);
102fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu);
103f83894b2SSean Christopherson void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu);
1041e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1055c69d5c1SPeter Xu int shorthand, unsigned int dest, int dest_mode);
10659508b30SPeter Xu int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
10725bb2cf9SSean Christopherson void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec);
108e7387b0eSLiran Alon bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr);
109e7387b0eSLiran Alon bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr);
110eb90f341SPaolo Bonzini void kvm_apic_update_ppr(struct kvm_vcpu *vcpu);
111b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
1129e4aabe2SJoerg Roedel struct dest_map *dest_map);
11389342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
114b26a695aSSuravee Suthikulpanit void kvm_apic_update_apicv(struct kvm_vcpu *vcpu);
115c482f2ceSSean Christopherson int kvm_alloc_apic_access_page(struct kvm *kvm);
1162008fab3SSean Christopherson void kvm_inhibit_apic_access_page(struct kvm_vcpu *vcpu);
11782470196SZhang Xiantao
1181e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
1199e4aabe2SJoerg Roedel struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map);
120d5361678SWanpeng Li void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high);
1211e08ec4aSGleb Natapov
12282470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
12358cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
124a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
125a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s);
12658871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu);
12782470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu);
12882470196SZhang Xiantao
129a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
130a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
131a3e06bbeSLiu, Jinsong
13283d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
133c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
13483d4c286SYang Zhang
135fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
136b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
137b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
138b93463aaSAvi Kivity
139b9964ee3SSean Christopherson int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data);
1400105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
1410105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
14210388a07SGleb Natapov
14310388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
14410388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
14510388a07SGleb Natapov
14677c3323fSVitaly Kuznetsov int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len);
147cef84c30SDavid Matlack void kvm_lapic_exit(void);
148c48f1496SGleb Natapov
149*b5fcc59bSSean Christopherson u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic);
150*b5fcc59bSSean Christopherson
1511e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1))
1521e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4)
1531e6e2755SSuravee Suthikulpanit
kvm_lapic_clear_vector(int vec,void * bitmap)154ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap)
155ee171d2fSWei Yang {
156ee171d2fSWei Yang clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
157ee171d2fSWei Yang }
158ee171d2fSWei Yang
kvm_lapic_set_vector(int vec,void * bitmap)1591e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap)
1601e6e2755SSuravee Suthikulpanit {
1611e6e2755SSuravee Suthikulpanit set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
1621e6e2755SSuravee Suthikulpanit }
1631e6e2755SSuravee Suthikulpanit
kvm_lapic_set_irr(int vec,struct kvm_lapic * apic)1641e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic)
1651e6e2755SSuravee Suthikulpanit {
1661e6e2755SSuravee Suthikulpanit kvm_lapic_set_vector(vec, apic->regs + APIC_IRR);
1671e6e2755SSuravee Suthikulpanit /*
1681e6e2755SSuravee Suthikulpanit * irr_pending must be true if any interrupt is pending; set it after
1691e6e2755SSuravee Suthikulpanit * APIC_IRR to avoid race with apic_clear_irr
1701e6e2755SSuravee Suthikulpanit */
1711e6e2755SSuravee Suthikulpanit apic->irr_pending = true;
1721e6e2755SSuravee Suthikulpanit }
1731e6e2755SSuravee Suthikulpanit
__kvm_lapic_get_reg(char * regs,int reg_off)174b9964ee3SSean Christopherson static inline u32 __kvm_lapic_get_reg(char *regs, int reg_off)
175b9964ee3SSean Christopherson {
176b9964ee3SSean Christopherson return *((u32 *) (regs + reg_off));
177b9964ee3SSean Christopherson }
178b9964ee3SSean Christopherson
kvm_lapic_get_reg(struct kvm_lapic * apic,int reg_off)179dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off)
180c48f1496SGleb Natapov {
181b9964ee3SSean Christopherson return __kvm_lapic_get_reg(apic->regs, reg_off);
1821e6e2755SSuravee Suthikulpanit }
1831e6e2755SSuravee Suthikulpanit
1846e4e3b4dSCun Li DECLARE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
185c48f1496SGleb Natapov
lapic_in_kernel(struct kvm_vcpu * vcpu)186bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu)
187c48f1496SGleb Natapov {
1886e4e3b4dSCun Li if (static_branch_unlikely(&kvm_has_noapic_vcpu))
189c48f1496SGleb Natapov return vcpu->arch.apic;
190c48f1496SGleb Natapov return true;
191c48f1496SGleb Natapov }
192c48f1496SGleb Natapov
1936e4e3b4dSCun Li extern struct static_key_false_deferred apic_hw_disabled;
194c48f1496SGleb Natapov
kvm_apic_hw_enabled(struct kvm_lapic * apic)1953c649918SPeng Hao static inline bool kvm_apic_hw_enabled(struct kvm_lapic *apic)
196c48f1496SGleb Natapov {
1976e4e3b4dSCun Li if (static_branch_unlikely(&apic_hw_disabled.key))
198c48f1496SGleb Natapov return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
1993c649918SPeng Hao return true;
200c48f1496SGleb Natapov }
201c48f1496SGleb Natapov
2026e4e3b4dSCun Li extern struct static_key_false_deferred apic_sw_disabled;
203c48f1496SGleb Natapov
kvm_apic_sw_enabled(struct kvm_lapic * apic)204f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
205c48f1496SGleb Natapov {
2066e4e3b4dSCun Li if (static_branch_unlikely(&apic_sw_disabled.key))
207f30ebc31SRadim Krčmář return apic->sw_enabled;
208f30ebc31SRadim Krčmář return true;
209c48f1496SGleb Natapov }
210c48f1496SGleb Natapov
kvm_apic_present(struct kvm_vcpu * vcpu)211c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
212c48f1496SGleb Natapov {
213bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
214c48f1496SGleb Natapov }
215c48f1496SGleb Natapov
kvm_lapic_enabled(struct kvm_vcpu * vcpu)216c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
217c48f1496SGleb Natapov {
218c48f1496SGleb Natapov return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
219c48f1496SGleb Natapov }
220c48f1496SGleb Natapov
apic_x2apic_mode(struct kvm_lapic * apic)2218d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic)
2228d14695fSYang Zhang {
2238d14695fSYang Zhang return apic->vcpu->arch.apic_base & X2APIC_ENABLE;
2248d14695fSYang Zhang }
2258d14695fSYang Zhang
kvm_vcpu_apicv_active(struct kvm_vcpu * vcpu)226d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu)
227c7c9c56cSYang Zhang {
228b8e1b962SSean Christopherson return lapic_in_kernel(vcpu) && vcpu->arch.apic->apicv_active;
229c7c9c56cSYang Zhang }
230c7c9c56cSYang Zhang
kvm_apic_has_pending_init_or_sipi(struct kvm_vcpu * vcpu)231a61353acSSean Christopherson static inline bool kvm_apic_has_pending_init_or_sipi(struct kvm_vcpu *vcpu)
23266450a21SJan Kiszka {
233bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events;
23466450a21SJan Kiszka }
23566450a21SJan Kiszka
kvm_apic_init_sipi_allowed(struct kvm_vcpu * vcpu)2361b7a1b78SSean Christopherson static inline bool kvm_apic_init_sipi_allowed(struct kvm_vcpu *vcpu)
2371b7a1b78SSean Christopherson {
2381b7a1b78SSean Christopherson return !is_smm(vcpu) &&
2391b7a1b78SSean Christopherson !static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
2401b7a1b78SSean Christopherson }
2411b7a1b78SSean Christopherson
kvm_lowest_prio_delivery(struct kvm_lapic_irq * irq)242d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
243d1ebdbf9SJames Sullivan {
244d1ebdbf9SJames Sullivan return (irq->delivery_mode == APIC_DM_LOWEST ||
245d1ebdbf9SJames Sullivan irq->msi_redir_hint);
246d1ebdbf9SJames Sullivan }
247d1ebdbf9SJames Sullivan
kvm_lapic_latched_init(struct kvm_vcpu * vcpu)248f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu)
249f077825aSPaolo Bonzini {
250bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
251f077825aSPaolo Bonzini }
252f077825aSPaolo Bonzini
25310606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
25410606919SYang Zhang
255b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu);
256d0659d94SMarcelo Tosatti
2577ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq,
2587ee30bc1SNitesh Narayan Lal unsigned long *vcpu_bitmap);
2597ee30bc1SNitesh Narayan Lal
2608feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq,
2618feb4a04SFeng Wu struct kvm_vcpu **dest_vcpu);
26252004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus,
26352004014SFeng Wu const unsigned long *bitmap, u32 bitmap_size);
264ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu);
265ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu);
266ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu);
267ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu);
268a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu);
269199a8b84SPaolo Bonzini bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu);
27058871649SJim Mattson
kvm_apic_mode(u64 apic_base)27158871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base)
27258871649SJim Mattson {
27358871649SJim Mattson return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE);
27458871649SJim Mattson }
27558871649SJim Mattson
kvm_xapic_id(struct kvm_lapic * apic)2765c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic)
2775c94ac5dSMiaohe Lin {
2785c94ac5dSMiaohe Lin return kvm_lapic_get_reg(apic, APIC_ID) >> 24;
2795c94ac5dSMiaohe Lin }
2805c94ac5dSMiaohe Lin
28182470196SZhang Xiantao #endif
282