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" 101b7a1b78SSean Christopherson #include "kvm_cache_regs.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); 11582470196SZhang Xiantao 1161e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, 1179e4aabe2SJoerg Roedel struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); 118d5361678SWanpeng Li void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); 1191e08ec4aSGleb Natapov 12082470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu); 12158cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 122a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); 123a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); 12458871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu); 12582470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); 12682470196SZhang Xiantao 127a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu); 128a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data); 129a3e06bbeSLiu, Jinsong 13083d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset); 131c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector); 13283d4c286SYang Zhang 133fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr); 134b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu); 135b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu); 136b93463aaSAvi Kivity 137b9964ee3SSean Christopherson int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data); 1380105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 1390105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 14010388a07SGleb Natapov 14110388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 14210388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 14310388a07SGleb Natapov 14477c3323fSVitaly Kuznetsov int kvm_lapic_set_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len); 145cef84c30SDavid Matlack void kvm_lapic_exit(void); 146c48f1496SGleb Natapov 1471e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1)) 1481e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4) 1491e6e2755SSuravee Suthikulpanit 150ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap) 151ee171d2fSWei Yang { 152ee171d2fSWei Yang clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); 153ee171d2fSWei Yang } 154ee171d2fSWei Yang 1551e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap) 1561e6e2755SSuravee Suthikulpanit { 1571e6e2755SSuravee Suthikulpanit set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); 1581e6e2755SSuravee Suthikulpanit } 1591e6e2755SSuravee Suthikulpanit 1601e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic) 1611e6e2755SSuravee Suthikulpanit { 1621e6e2755SSuravee Suthikulpanit kvm_lapic_set_vector(vec, apic->regs + APIC_IRR); 1631e6e2755SSuravee Suthikulpanit /* 1641e6e2755SSuravee Suthikulpanit * irr_pending must be true if any interrupt is pending; set it after 1651e6e2755SSuravee Suthikulpanit * APIC_IRR to avoid race with apic_clear_irr 1661e6e2755SSuravee Suthikulpanit */ 1671e6e2755SSuravee Suthikulpanit apic->irr_pending = true; 1681e6e2755SSuravee Suthikulpanit } 1691e6e2755SSuravee Suthikulpanit 170b9964ee3SSean Christopherson static inline u32 __kvm_lapic_get_reg(char *regs, int reg_off) 171b9964ee3SSean Christopherson { 172b9964ee3SSean Christopherson return *((u32 *) (regs + reg_off)); 173b9964ee3SSean Christopherson } 174b9964ee3SSean Christopherson 175dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off) 176c48f1496SGleb Natapov { 177b9964ee3SSean Christopherson return __kvm_lapic_get_reg(apic->regs, reg_off); 1781e6e2755SSuravee Suthikulpanit } 1791e6e2755SSuravee Suthikulpanit 1806e4e3b4dSCun Li DECLARE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu); 181c48f1496SGleb Natapov 182bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu) 183c48f1496SGleb Natapov { 1846e4e3b4dSCun Li if (static_branch_unlikely(&kvm_has_noapic_vcpu)) 185c48f1496SGleb Natapov return vcpu->arch.apic; 186c48f1496SGleb Natapov return true; 187c48f1496SGleb Natapov } 188c48f1496SGleb Natapov 1896e4e3b4dSCun Li extern struct static_key_false_deferred apic_hw_disabled; 190c48f1496SGleb Natapov 191c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic) 192c48f1496SGleb Natapov { 1936e4e3b4dSCun Li if (static_branch_unlikely(&apic_hw_disabled.key)) 194c48f1496SGleb Natapov return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE; 195c48f1496SGleb Natapov return MSR_IA32_APICBASE_ENABLE; 196c48f1496SGleb Natapov } 197c48f1496SGleb Natapov 1986e4e3b4dSCun Li extern struct static_key_false_deferred apic_sw_disabled; 199c48f1496SGleb Natapov 200f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic) 201c48f1496SGleb Natapov { 2026e4e3b4dSCun Li if (static_branch_unlikely(&apic_sw_disabled.key)) 203f30ebc31SRadim Krčmář return apic->sw_enabled; 204f30ebc31SRadim Krčmář return true; 205c48f1496SGleb Natapov } 206c48f1496SGleb Natapov 207c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu) 208c48f1496SGleb Natapov { 209bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic); 210c48f1496SGleb Natapov } 211c48f1496SGleb Natapov 212c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu) 213c48f1496SGleb Natapov { 214c48f1496SGleb Natapov return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic); 215c48f1496SGleb Natapov } 216c48f1496SGleb Natapov 2178d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic) 2188d14695fSYang Zhang { 2198d14695fSYang Zhang return apic->vcpu->arch.apic_base & X2APIC_ENABLE; 2208d14695fSYang Zhang } 2218d14695fSYang Zhang 222d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu) 223c7c9c56cSYang Zhang { 224b8e1b962SSean Christopherson return lapic_in_kernel(vcpu) && vcpu->arch.apic->apicv_active; 225c7c9c56cSYang Zhang } 226c7c9c56cSYang Zhang 227*a61353acSSean Christopherson static inline bool kvm_apic_has_pending_init_or_sipi(struct kvm_vcpu *vcpu) 22866450a21SJan Kiszka { 229bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events; 23066450a21SJan Kiszka } 23166450a21SJan Kiszka 2321b7a1b78SSean Christopherson static inline bool kvm_apic_init_sipi_allowed(struct kvm_vcpu *vcpu) 2331b7a1b78SSean Christopherson { 2341b7a1b78SSean Christopherson return !is_smm(vcpu) && 2351b7a1b78SSean Christopherson !static_call(kvm_x86_apic_init_signal_blocked)(vcpu); 2361b7a1b78SSean Christopherson } 2371b7a1b78SSean Christopherson 238d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq) 239d1ebdbf9SJames Sullivan { 240d1ebdbf9SJames Sullivan return (irq->delivery_mode == APIC_DM_LOWEST || 241d1ebdbf9SJames Sullivan irq->msi_redir_hint); 242d1ebdbf9SJames Sullivan } 243d1ebdbf9SJames Sullivan 244f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu) 245f077825aSPaolo Bonzini { 246bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); 247f077825aSPaolo Bonzini } 248f077825aSPaolo Bonzini 24910606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector); 25010606919SYang Zhang 251b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu); 252d0659d94SMarcelo Tosatti 2537ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq, 2547ee30bc1SNitesh Narayan Lal unsigned long *vcpu_bitmap); 2557ee30bc1SNitesh Narayan Lal 2568feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, 2578feb4a04SFeng Wu struct kvm_vcpu **dest_vcpu); 25852004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus, 25952004014SFeng Wu const unsigned long *bitmap, u32 bitmap_size); 260ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu); 261ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu); 262ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu); 263ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu); 264a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu); 265199a8b84SPaolo Bonzini bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu); 26658871649SJim Mattson 26758871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base) 26858871649SJim Mattson { 26958871649SJim Mattson return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE); 27058871649SJim Mattson } 27158871649SJim Mattson 2725c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic) 2735c94ac5dSMiaohe Lin { 2745c94ac5dSMiaohe Lin return kvm_lapic_get_reg(apic, APIC_ID) >> 24; 2755c94ac5dSMiaohe Lin } 2765c94ac5dSMiaohe Lin 27782470196SZhang Xiantao #endif 278