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 101e6e2755SSuravee Suthikulpanit #define KVM_APIC_LVT_NUM 6 1166450a21SJan Kiszka 12e9d90d47SAvi Kivity struct kvm_timer { 13e9d90d47SAvi Kivity struct hrtimer timer; 14e9d90d47SAvi Kivity s64 period; /* unit: ns */ 15a323b409SRadim Krčmář u32 timer_mode; 16e9d90d47SAvi Kivity u32 timer_mode_mask; 17e9d90d47SAvi Kivity u64 tscdeadline; 18d0659d94SMarcelo Tosatti u64 expired_tscdeadline; 19e9d90d47SAvi Kivity atomic_t pending; /* accumulated triggered timers */ 20e9d90d47SAvi Kivity }; 21e9d90d47SAvi Kivity 2282470196SZhang Xiantao struct kvm_lapic { 2382470196SZhang Xiantao unsigned long base_address; 2482470196SZhang Xiantao struct kvm_io_device dev; 25d3c7b77dSMarcelo Tosatti struct kvm_timer lapic_timer; 2682470196SZhang Xiantao u32 divide_count; 2782470196SZhang Xiantao struct kvm_vcpu *vcpu; 28e462755cSRadim Krčmář bool sw_enabled; 2933e4c686SGleb Natapov bool irr_pending; 3059fd1323SRadim Krčmář bool lvt0_in_nmi_mode; 318680b94bSMichael S. Tsirkin /* Number of bits set in ISR. */ 328680b94bSMichael S. Tsirkin s16 isr_count; 338680b94bSMichael S. Tsirkin /* The highest vector set in ISR; if -1 - invalid, must scan ISR. */ 348680b94bSMichael S. Tsirkin int highest_isr_cache; 355eadf916SMichael S. Tsirkin /** 365eadf916SMichael S. Tsirkin * APIC register page. The layout matches the register layout seen by 375eadf916SMichael S. Tsirkin * the guest 1:1, because it is accessed by the vmx microcode. 385eadf916SMichael S. Tsirkin * Note: Only one register, the TPR, is used by the microcode. 395eadf916SMichael S. Tsirkin */ 4082470196SZhang Xiantao void *regs; 41b93463aaSAvi Kivity gpa_t vapic_addr; 42fda4e2e8SAndy Honig struct gfn_to_hva_cache vapic_cache; 4366450a21SJan Kiszka unsigned long pending_events; 4466450a21SJan Kiszka unsigned int sipi_vector; 4582470196SZhang Xiantao }; 469e4aabe2SJoerg Roedel 479e4aabe2SJoerg Roedel struct dest_map; 489e4aabe2SJoerg Roedel 4982470196SZhang Xiantao int kvm_create_lapic(struct kvm_vcpu *vcpu); 5082470196SZhang Xiantao void kvm_free_lapic(struct kvm_vcpu *vcpu); 5182470196SZhang Xiantao 5282470196SZhang Xiantao int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu); 5382470196SZhang Xiantao int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu); 5482470196SZhang Xiantao int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu); 5566450a21SJan Kiszka void kvm_apic_accept_events(struct kvm_vcpu *vcpu); 56d28bc9ddSNadav Amit void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event); 5782470196SZhang Xiantao u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); 5882470196SZhang Xiantao void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); 5958fbbf26SKevin Tian void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu); 6082470196SZhang Xiantao void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value); 618b2cf73cSHarvey Harrison u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu); 62fc61b800SGleb Natapov void kvm_apic_set_version(struct kvm_vcpu *vcpu); 631e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val); 641e6e2755SSuravee Suthikulpanit int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, 651e6e2755SSuravee Suthikulpanit void *data); 661e6e2755SSuravee Suthikulpanit bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, 671e6e2755SSuravee Suthikulpanit int short_hand, unsigned int dest, int dest_mode); 6882470196SZhang Xiantao 69705699a1SWincy Van void __kvm_apic_update_irr(u32 *pir, void *regs); 70a20ed54dSYang Zhang void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir); 71b4f2225cSYang Zhang int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, 729e4aabe2SJoerg Roedel struct dest_map *dest_map); 7389342082SAvi Kivity int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type); 7482470196SZhang Xiantao 751e08ec4aSGleb Natapov bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, 769e4aabe2SJoerg Roedel struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); 771e08ec4aSGleb Natapov 7882470196SZhang Xiantao u64 kvm_get_apic_base(struct kvm_vcpu *vcpu); 7958cb628dSJan Kiszka int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); 8064eb0620SGleb Natapov void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu, 8164eb0620SGleb Natapov struct kvm_lapic_state *s); 8282470196SZhang Xiantao int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); 8382470196SZhang Xiantao 84a3e06bbeSLiu, Jinsong u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu); 85a3e06bbeSLiu, Jinsong void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data); 86a3e06bbeSLiu, Jinsong 8783d4c286SYang Zhang void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset); 88c7c9c56cSYang Zhang void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector); 8983d4c286SYang Zhang 90fda4e2e8SAndy Honig int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr); 91b93463aaSAvi Kivity void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu); 92b93463aaSAvi Kivity void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu); 93b93463aaSAvi Kivity 940105d1a5SGleb Natapov int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 950105d1a5SGleb Natapov int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 9610388a07SGleb Natapov 9710388a07SGleb Natapov int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data); 9810388a07SGleb Natapov int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data); 9910388a07SGleb Natapov 10010388a07SGleb Natapov static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu) 10110388a07SGleb Natapov { 102e83d5887SAndrey Smetanin return vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE; 10310388a07SGleb Natapov } 104ae7a2a3fSMichael S. Tsirkin 105ae7a2a3fSMichael S. Tsirkin int kvm_lapic_enable_pv_eoi(struct kvm_vcpu *vcpu, u64 data); 106c5cc421bSGleb Natapov void kvm_lapic_init(void); 107c48f1496SGleb Natapov 1081e6e2755SSuravee Suthikulpanit #define VEC_POS(v) ((v) & (32 - 1)) 1091e6e2755SSuravee Suthikulpanit #define REG_POS(v) (((v) >> 5) << 4) 1101e6e2755SSuravee Suthikulpanit 1111e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_vector(int vec, void *bitmap) 1121e6e2755SSuravee Suthikulpanit { 1131e6e2755SSuravee Suthikulpanit set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec)); 1141e6e2755SSuravee Suthikulpanit } 1151e6e2755SSuravee Suthikulpanit 1161e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic) 1171e6e2755SSuravee Suthikulpanit { 1181e6e2755SSuravee Suthikulpanit kvm_lapic_set_vector(vec, apic->regs + APIC_IRR); 1191e6e2755SSuravee Suthikulpanit /* 1201e6e2755SSuravee Suthikulpanit * irr_pending must be true if any interrupt is pending; set it after 1211e6e2755SSuravee Suthikulpanit * APIC_IRR to avoid race with apic_clear_irr 1221e6e2755SSuravee Suthikulpanit */ 1231e6e2755SSuravee Suthikulpanit apic->irr_pending = true; 1241e6e2755SSuravee Suthikulpanit } 1251e6e2755SSuravee Suthikulpanit 126*dfb95954SSuravee Suthikulpanit static inline u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off) 127c48f1496SGleb Natapov { 128c48f1496SGleb Natapov return *((u32 *) (apic->regs + reg_off)); 129c48f1496SGleb Natapov } 130c48f1496SGleb Natapov 1311e6e2755SSuravee Suthikulpanit static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val) 1321e6e2755SSuravee Suthikulpanit { 1331e6e2755SSuravee Suthikulpanit *((u32 *) (apic->regs + reg_off)) = val; 1341e6e2755SSuravee Suthikulpanit } 1351e6e2755SSuravee Suthikulpanit 136c48f1496SGleb Natapov extern struct static_key kvm_no_apic_vcpu; 137c48f1496SGleb Natapov 138bce87cceSPaolo Bonzini static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu) 139c48f1496SGleb Natapov { 140c48f1496SGleb Natapov if (static_key_false(&kvm_no_apic_vcpu)) 141c48f1496SGleb Natapov return vcpu->arch.apic; 142c48f1496SGleb Natapov return true; 143c48f1496SGleb Natapov } 144c48f1496SGleb Natapov 145c48f1496SGleb Natapov extern struct static_key_deferred apic_hw_disabled; 146c48f1496SGleb Natapov 147c48f1496SGleb Natapov static inline int kvm_apic_hw_enabled(struct kvm_lapic *apic) 148c48f1496SGleb Natapov { 149c48f1496SGleb Natapov if (static_key_false(&apic_hw_disabled.key)) 150c48f1496SGleb Natapov return apic->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE; 151c48f1496SGleb Natapov return MSR_IA32_APICBASE_ENABLE; 152c48f1496SGleb Natapov } 153c48f1496SGleb Natapov 154c48f1496SGleb Natapov extern struct static_key_deferred apic_sw_disabled; 155c48f1496SGleb Natapov 156f30ebc31SRadim Krčmář static inline bool kvm_apic_sw_enabled(struct kvm_lapic *apic) 157c48f1496SGleb Natapov { 158c48f1496SGleb Natapov if (static_key_false(&apic_sw_disabled.key)) 159f30ebc31SRadim Krčmář return apic->sw_enabled; 160f30ebc31SRadim Krčmář return true; 161c48f1496SGleb Natapov } 162c48f1496SGleb Natapov 163c48f1496SGleb Natapov static inline bool kvm_apic_present(struct kvm_vcpu *vcpu) 164c48f1496SGleb Natapov { 165bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic); 166c48f1496SGleb Natapov } 167c48f1496SGleb Natapov 168c48f1496SGleb Natapov static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu) 169c48f1496SGleb Natapov { 170c48f1496SGleb Natapov return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic); 171c48f1496SGleb Natapov } 172c48f1496SGleb Natapov 1738d14695fSYang Zhang static inline int apic_x2apic_mode(struct kvm_lapic *apic) 1748d14695fSYang Zhang { 1758d14695fSYang Zhang return apic->vcpu->arch.apic_base & X2APIC_ENABLE; 1768d14695fSYang Zhang } 1778d14695fSYang Zhang 178d62caabbSAndrey Smetanin static inline bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu) 179c7c9c56cSYang Zhang { 180d62caabbSAndrey Smetanin return vcpu->arch.apic && vcpu->arch.apicv_active; 181c7c9c56cSYang Zhang } 182c7c9c56cSYang Zhang 18366450a21SJan Kiszka static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu) 18466450a21SJan Kiszka { 185bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && vcpu->arch.apic->pending_events; 18666450a21SJan Kiszka } 18766450a21SJan Kiszka 188d1ebdbf9SJames Sullivan static inline bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq) 189d1ebdbf9SJames Sullivan { 190d1ebdbf9SJames Sullivan return (irq->delivery_mode == APIC_DM_LOWEST || 191d1ebdbf9SJames Sullivan irq->msi_redir_hint); 192d1ebdbf9SJames Sullivan } 193d1ebdbf9SJames Sullivan 194f077825aSPaolo Bonzini static inline int kvm_lapic_latched_init(struct kvm_vcpu *vcpu) 195f077825aSPaolo Bonzini { 196bce87cceSPaolo Bonzini return lapic_in_kernel(vcpu) && test_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); 197f077825aSPaolo Bonzini } 198f077825aSPaolo Bonzini 1995c919412SAndrey Smetanin static inline int kvm_apic_id(struct kvm_lapic *apic) 2005c919412SAndrey Smetanin { 201*dfb95954SSuravee Suthikulpanit return (kvm_lapic_get_reg(apic, APIC_ID) >> 24) & 0xff; 2025c919412SAndrey Smetanin } 2035c919412SAndrey Smetanin 20410606919SYang Zhang bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector); 20510606919SYang Zhang 206d0659d94SMarcelo Tosatti void wait_lapic_expire(struct kvm_vcpu *vcpu); 207d0659d94SMarcelo Tosatti 2088feb4a04SFeng Wu bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm, struct kvm_lapic_irq *irq, 2098feb4a04SFeng Wu struct kvm_vcpu **dest_vcpu); 21052004014SFeng Wu int kvm_vector_to_index(u32 vector, u32 dest_vcpus, 21152004014SFeng Wu const unsigned long *bitmap, u32 bitmap_size); 21282470196SZhang Xiantao #endif 213