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); 814abaffceSWanpeng Li void kvm_recalculate_apic_map(struct kvm *kvm); 82fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu); 831e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val); 841e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, 851e6e2755SSuravee Suthikulpanit void *data); 861e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, 875c69d5c1SPeter Xu int shorthand, unsigned int dest, int dest_mode); 8859508b30SPeter Xu int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2); 89e7387b0eSLiran Alon bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr); 90e7387b0eSLiran Alon bool kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir, int *max_irr); 91eb90f341SPaolo Bonzini void kvm_apic_update_ppr(struct kvm_vcpu *vcpu); 92b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, 939e4aabe2SJoerg Roedel struct dest_map *dest_map); 9489342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type); 95b26a695aSSuravee Suthikulpanit void kvm_apic_update_apicv(struct kvm_vcpu *vcpu); 9682470196SZhang Xiantao 971e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, 989e4aabe2SJoerg Roedel struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); 99*d5361678SWanpeng Li void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); 1001e08ec4aSGleb Natapov 10182470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu); 10258cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 103a92e2543SRadim Krčmář int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); 104a92e2543SRadim Krčmář int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); 10558871649SJim Mattson enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu); 10682470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); 10782470196SZhang Xiantao 108a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu); 109a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data); 110a3e06bbeSLiu, Jinsong 11183d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset); 112c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector); 11383d4c286SYang Zhang 114fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr); 115b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu); 116b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu); 117b93463aaSAvi Kivity 1180105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 1190105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 12010388a07SGleb Natapov 12110388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 12210388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 12310388a07SGleb Natapov 12410388a07SGleb Natapov static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu) 12510388a07SGleb Natapov { 126d4abc577SLadi Prosek return vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE; 12710388a07SGleb Natapov } 128ae7a2a3fSMichael S. Tsirkin 12972bbf935SLadi Prosek int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data, unsigned long len); 130c5cc421bSGleb Natapov void kvm_lapic_init(void); 131cef84c30SDavid Matlack void kvm_lapic_exit(void); 132c48f1496SGleb Natapov 1331e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1)) 1341e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4) 1351e6e2755SSuravee Suthikulpanit 136ee171d2fSWei Yang static inline void kvm_lapic_clear_vector(int vec, void *bitmap) 137ee171d2fSWei Yang { 138ee171d2fSWei Yang clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); 139ee171d2fSWei Yang } 140ee171d2fSWei Yang 1411e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap) 1421e6e2755SSuravee Suthikulpanit { 1431e6e2755SSuravee Suthikulpanit set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); 1441e6e2755SSuravee Suthikulpanit } 1451e6e2755SSuravee Suthikulpanit 1461e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic) 1471e6e2755SSuravee Suthikulpanit { 1481e6e2755SSuravee Suthikulpanit kvm_lapic_set_vector(vec, apic->regs + APIC_IRR); 1491e6e2755SSuravee Suthikulpanit /* 1501e6e2755SSuravee Suthikulpanit * irr_pending must be true if any interrupt is pending; set it after 1511e6e2755SSuravee Suthikulpanit * APIC_IRR to avoid race with apic_clear_irr 1521e6e2755SSuravee Suthikulpanit */ 1531e6e2755SSuravee Suthikulpanit apic->irr_pending = true; 1541e6e2755SSuravee Suthikulpanit } 1551e6e2755SSuravee Suthikulpanit 156dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off) 157c48f1496SGleb Natapov { 158c48f1496SGleb Natapov return *((u32 *) (apic->regs + reg_off)); 159c48f1496SGleb Natapov } 160c48f1496SGleb Natapov 1611e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val) 1621e6e2755SSuravee Suthikulpanit { 1631e6e2755SSuravee Suthikulpanit *((u32 *) (apic->regs + reg_off)) = val; 1641e6e2755SSuravee Suthikulpanit } 1651e6e2755SSuravee Suthikulpanit 166c48f1496SGleb Natapov extern struct static_key kvm_no_apic_vcpu; 167c48f1496SGleb Natapov 168bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu) 169c48f1496SGleb Natapov { 170c48f1496SGleb Natapov if (static_key_false(&kvm_no_apic_vcpu)) 171c48f1496SGleb Natapov return vcpu->arch.apic; 172c48f1496SGleb Natapov return true; 173c48f1496SGleb Natapov } 174c48f1496SGleb Natapov 175c48f1496SGleb Natapov extern struct static_key_deferred apic_hw_disabled; 176c48f1496SGleb Natapov 177c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic) 178c48f1496SGleb Natapov { 179c48f1496SGleb Natapov if (static_key_false(&apic_hw_disabled.key)) 180c48f1496SGleb Natapov return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE; 181c48f1496SGleb Natapov return MSR_IA32_APICBASE_ENABLE; 182c48f1496SGleb Natapov } 183c48f1496SGleb Natapov 184c48f1496SGleb Natapov extern struct static_key_deferred apic_sw_disabled; 185c48f1496SGleb Natapov 186f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic) 187c48f1496SGleb Natapov { 188c48f1496SGleb Natapov if (static_key_false(&apic_sw_disabled.key)) 189f30ebc31SRadim Krčmář return apic->sw_enabled; 190f30ebc31SRadim Krčmář return true; 191c48f1496SGleb Natapov } 192c48f1496SGleb Natapov 193c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu) 194c48f1496SGleb Natapov { 195bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic); 196c48f1496SGleb Natapov } 197c48f1496SGleb Natapov 198c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu) 199c48f1496SGleb Natapov { 200c48f1496SGleb Natapov return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic); 201c48f1496SGleb Natapov } 202c48f1496SGleb Natapov 2038d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic) 2048d14695fSYang Zhang { 2058d14695fSYang Zhang return apic->vcpu->arch.apic_base & X2APIC_ENABLE; 2068d14695fSYang Zhang } 2078d14695fSYang Zhang 208d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu) 209c7c9c56cSYang Zhang { 210d62caabbSAndrey Smetanin return vcpu->arch.apic && vcpu->arch.apicv_active; 211c7c9c56cSYang Zhang } 212c7c9c56cSYang Zhang 21366450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu) 21466450a21SJan Kiszka { 215bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events; 21666450a21SJan Kiszka } 21766450a21SJan Kiszka 218d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq) 219d1ebdbf9SJames Sullivan { 220d1ebdbf9SJames Sullivan return (irq->delivery_mode == APIC_DM_LOWEST || 221d1ebdbf9SJames Sullivan irq->msi_redir_hint); 222d1ebdbf9SJames Sullivan } 223d1ebdbf9SJames Sullivan 224f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu) 225f077825aSPaolo Bonzini { 226bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); 227f077825aSPaolo Bonzini } 228f077825aSPaolo Bonzini 22910606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector); 23010606919SYang Zhang 231b6c4bc65SWanpeng Li void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu); 232d0659d94SMarcelo Tosatti 2337ee30bc1SNitesh Narayan Lal void kvm_bitmap_or_dest_vcpus(struct kvm *kvm, struct kvm_lapic_irq *irq, 2347ee30bc1SNitesh Narayan Lal unsigned long *vcpu_bitmap); 2357ee30bc1SNitesh Narayan Lal 2368feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, 2378feb4a04SFeng Wu struct kvm_vcpu **dest_vcpu); 23852004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus, 23952004014SFeng Wu const unsigned long *bitmap, u32 bitmap_size); 240ce7a058aSYunhong Jiang void kvm_lapic_switch_to_sw_timer(struct kvm_vcpu *vcpu); 241ce7a058aSYunhong Jiang void kvm_lapic_switch_to_hv_timer(struct kvm_vcpu *vcpu); 242ce7a058aSYunhong Jiang void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu); 243ce7a058aSYunhong Jiang bool kvm_lapic_hv_timer_in_use(struct kvm_vcpu *vcpu); 244a749e247SPaolo Bonzini void kvm_lapic_restart_hv_timer(struct kvm_vcpu *vcpu); 2450c5f81daSWanpeng Li bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu); 24658871649SJim Mattson 24758871649SJim Mattson static inline enum lapic_mode kvm_apic_mode(u64 apic_base) 24858871649SJim Mattson { 24958871649SJim Mattson return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE); 25058871649SJim Mattson } 25158871649SJim Mattson 2525c94ac5dSMiaohe Lin static inline u8 kvm_xapic_id(struct kvm_lapic *apic) 2535c94ac5dSMiaohe Lin { 2545c94ac5dSMiaohe Lin return kvm_lapic_get_reg(apic, APIC_ID) >> 24; 2555c94ac5dSMiaohe Lin } 2565c94ac5dSMiaohe Lin 25782470196SZhang Xiantao #endif 258