1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2e08cae41SH. Peter Anvin #ifndef _ASM_X86_MSHYPER_H 3e08cae41SH. Peter Anvin #define _ASM_X86_MSHYPER_H 4a2a47c6cSKy Srinivasan 5e08cae41SH. Peter Anvin #include <linux/types.h> 6806c8927SVitaly Kuznetsov #include <linux/nmi.h> 71cf106d9SBoqun Feng #include <linux/msi.h> 8fc53662fSVitaly Kuznetsov #include <asm/io.h> 95a485803SVitaly Kuznetsov #include <asm/hyperv-tlfs.h> 10e70e5892SDavid Woodhouse #include <asm/nospec-branch.h> 11b9d8cf2eSMichael Kelley #include <asm/paravirt.h> 12753ed9c9SJoseph Salisbury #include <asm/mshyperv.h> 13e08cae41SH. Peter Anvin 14*faff4406STianyu Lan union hv_ghcb; 15*faff4406STianyu Lan 160cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp); 170cc4f6d9STianyu Lan 18cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)( 19cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 20cc4edae4SLan Tianyu void *data); 21cc4edae4SLan Tianyu 22dd2cb348SMichael Kelley #define hv_get_raw_timer() rdtsc_ordered() 23dd2cb348SMichael Kelley 24bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs); 258730046cSK. Y. Srinivasan 268730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV) 27dfe94d40SDexuan Cui extern int hyperv_init_cpuhp; 28dfe94d40SDexuan Cui 29fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg; 30fc53662fSVitaly Kuznetsov 3199a0f46aSWei Liu extern u64 hv_current_partition_id; 3299a0f46aSWei Liu 33*faff4406STianyu Lan extern union hv_ghcb __percpu **hv_ghcb_pg; 340cc4f6d9STianyu Lan 3586b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); 3686b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); 3786b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); 3886b5ec35SWei Liu 39fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 40fc53662fSVitaly Kuznetsov { 41fc53662fSVitaly Kuznetsov u64 input_address = input ? virt_to_phys(input) : 0; 42fc53662fSVitaly Kuznetsov u64 output_address = output ? virt_to_phys(output) : 0; 43fc53662fSVitaly Kuznetsov u64 hv_status; 44fc53662fSVitaly Kuznetsov 45fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64 46fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 47fc53662fSVitaly Kuznetsov return U64_MAX; 48fc53662fSVitaly Kuznetsov 49fc53662fSVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 50e70e5892SDavid Woodhouse CALL_NOSPEC 51f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 52fc53662fSVitaly Kuznetsov "+c" (control), "+d" (input_address) 53e70e5892SDavid Woodhouse : "r" (output_address), 54e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 55fc53662fSVitaly Kuznetsov : "cc", "memory", "r8", "r9", "r10", "r11"); 56fc53662fSVitaly Kuznetsov #else 57fc53662fSVitaly Kuznetsov u32 input_address_hi = upper_32_bits(input_address); 58fc53662fSVitaly Kuznetsov u32 input_address_lo = lower_32_bits(input_address); 59fc53662fSVitaly Kuznetsov u32 output_address_hi = upper_32_bits(output_address); 60fc53662fSVitaly Kuznetsov u32 output_address_lo = lower_32_bits(output_address); 61fc53662fSVitaly Kuznetsov 62fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 63fc53662fSVitaly Kuznetsov return U64_MAX; 64fc53662fSVitaly Kuznetsov 65e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 66fc53662fSVitaly Kuznetsov : "=A" (hv_status), 67f5caf621SJosh Poimboeuf "+c" (input_address_lo), ASM_CALL_CONSTRAINT 68fc53662fSVitaly Kuznetsov : "A" (control), 69fc53662fSVitaly Kuznetsov "b" (input_address_hi), 70fc53662fSVitaly Kuznetsov "D"(output_address_hi), "S"(output_address_lo), 71e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 72fc53662fSVitaly Kuznetsov : "cc", "memory"); 73fc53662fSVitaly Kuznetsov #endif /* !x86_64 */ 74fc53662fSVitaly Kuznetsov return hv_status; 75fc53662fSVitaly Kuznetsov } 76dee863b5SVitaly Kuznetsov 776a8edbd0SVitaly Kuznetsov /* Fast hypercall with 8 bytes of input and no output */ 786a8edbd0SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 796a8edbd0SVitaly Kuznetsov { 806a8edbd0SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 816a8edbd0SVitaly Kuznetsov 826a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64 836a8edbd0SVitaly Kuznetsov { 84e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 85f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 866a8edbd0SVitaly Kuznetsov "+c" (control), "+d" (input1) 87e70e5892SDavid Woodhouse : THUNK_TARGET(hv_hypercall_pg) 886a8edbd0SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 896a8edbd0SVitaly Kuznetsov } 906a8edbd0SVitaly Kuznetsov #else 916a8edbd0SVitaly Kuznetsov { 926a8edbd0SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 936a8edbd0SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 946a8edbd0SVitaly Kuznetsov 95e70e5892SDavid Woodhouse __asm__ __volatile__ (CALL_NOSPEC 966a8edbd0SVitaly Kuznetsov : "=A"(hv_status), 976a8edbd0SVitaly Kuznetsov "+c"(input1_lo), 98f5caf621SJosh Poimboeuf ASM_CALL_CONSTRAINT 996a8edbd0SVitaly Kuznetsov : "A" (control), 1006a8edbd0SVitaly Kuznetsov "b" (input1_hi), 101e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 1026a8edbd0SVitaly Kuznetsov : "cc", "edi", "esi"); 1036a8edbd0SVitaly Kuznetsov } 1046a8edbd0SVitaly Kuznetsov #endif 1056a8edbd0SVitaly Kuznetsov return hv_status; 1066a8edbd0SVitaly Kuznetsov } 1076a8edbd0SVitaly Kuznetsov 10853e52966SVitaly Kuznetsov /* Fast hypercall with 16 bytes of input */ 10953e52966SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 11053e52966SVitaly Kuznetsov { 11153e52966SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 11253e52966SVitaly Kuznetsov 11353e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64 11453e52966SVitaly Kuznetsov { 11553e52966SVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 11653e52966SVitaly Kuznetsov CALL_NOSPEC 11753e52966SVitaly Kuznetsov : "=a" (hv_status), ASM_CALL_CONSTRAINT, 11853e52966SVitaly Kuznetsov "+c" (control), "+d" (input1) 11953e52966SVitaly Kuznetsov : "r" (input2), 12053e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 12153e52966SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 12253e52966SVitaly Kuznetsov } 12353e52966SVitaly Kuznetsov #else 12453e52966SVitaly Kuznetsov { 12553e52966SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 12653e52966SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 12753e52966SVitaly Kuznetsov u32 input2_hi = upper_32_bits(input2); 12853e52966SVitaly Kuznetsov u32 input2_lo = lower_32_bits(input2); 12953e52966SVitaly Kuznetsov 13053e52966SVitaly Kuznetsov __asm__ __volatile__ (CALL_NOSPEC 13153e52966SVitaly Kuznetsov : "=A"(hv_status), 13253e52966SVitaly Kuznetsov "+c"(input1_lo), ASM_CALL_CONSTRAINT 13353e52966SVitaly Kuznetsov : "A" (control), "b" (input1_hi), 13453e52966SVitaly Kuznetsov "D"(input2_hi), "S"(input2_lo), 13553e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 13653e52966SVitaly Kuznetsov : "cc"); 13753e52966SVitaly Kuznetsov } 13853e52966SVitaly Kuznetsov #endif 13953e52966SVitaly Kuznetsov return hv_status; 14053e52966SVitaly Kuznetsov } 14153e52966SVitaly Kuznetsov 142a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page; 143a46d15ccSVitaly Kuznetsov 144a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 145a46d15ccSVitaly Kuznetsov { 146a46d15ccSVitaly Kuznetsov if (!hv_vp_assist_page) 147a46d15ccSVitaly Kuznetsov return NULL; 148a46d15ccSVitaly Kuznetsov 149a46d15ccSVitaly Kuznetsov return hv_vp_assist_page[cpu]; 150a46d15ccSVitaly Kuznetsov } 1517415aea6SVitaly Kuznetsov 1526b48cb5fSK. Y. Srinivasan void __init hyperv_init(void); 1532ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void); 15493286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void)); 15593286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void); 15693286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void); 157eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as); 158cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as, 159cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data); 160cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list( 161cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 162cc4edae4SLan Tianyu u64 start_gfn, u64 end_gfn); 1632d2ccf24SThomas Gleixner 1642d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64 1656b48cb5fSK. Y. Srinivasan void hv_apic_init(void); 1663a025de6SYi Sun void __init hv_init_spinlocks(void); 1673a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu); 1682d2ccf24SThomas Gleixner #else 1692d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {} 1702d2ccf24SThomas Gleixner #endif 1712d2ccf24SThomas Gleixner 1721cf106d9SBoqun Feng static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, 1731cf106d9SBoqun Feng struct msi_desc *msi_desc) 1741cf106d9SBoqun Feng { 175d589ae61SWei Liu msi_entry->address.as_uint32 = msi_desc->msg.address_lo; 176d589ae61SWei Liu msi_entry->data.as_uint32 = msi_desc->msg.data; 1771cf106d9SBoqun Feng } 1781cf106d9SBoqun Feng 179e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void); 180e39397d1SWei Liu 181fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector, 182fb5ef351SWei Liu struct hv_interrupt_entry *entry); 183fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry); 184810a5212STianyu Lan int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible); 185*faff4406STianyu Lan 186*faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT 187*faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value); 188*faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value); 189*faff4406STianyu Lan #else 190*faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} 191*faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} 192*faff4406STianyu Lan #endif 193*faff4406STianyu Lan 194*faff4406STianyu Lan extern bool hv_isolation_type_snp(void); 195*faff4406STianyu Lan 196*faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg) 197*faff4406STianyu Lan { 198*faff4406STianyu Lan if ((reg >= HV_REGISTER_SCONTROL) && 199*faff4406STianyu Lan (reg <= HV_REGISTER_SINT15)) 200*faff4406STianyu Lan return true; 201*faff4406STianyu Lan return false; 202*faff4406STianyu Lan } 203*faff4406STianyu Lan 204*faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) 205*faff4406STianyu Lan { 206*faff4406STianyu Lan u64 value; 207*faff4406STianyu Lan 208*faff4406STianyu Lan if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) 209*faff4406STianyu Lan hv_ghcb_msr_read(reg, &value); 210*faff4406STianyu Lan else 211*faff4406STianyu Lan rdmsrl(reg, value); 212*faff4406STianyu Lan return value; 213*faff4406STianyu Lan } 214*faff4406STianyu Lan 215*faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) 216*faff4406STianyu Lan { 217*faff4406STianyu Lan if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) { 218*faff4406STianyu Lan hv_ghcb_msr_write(reg, value); 219*faff4406STianyu Lan 220*faff4406STianyu Lan /* Write proxy bit via wrmsl instruction */ 221*faff4406STianyu Lan if (reg >= HV_REGISTER_SINT0 && 222*faff4406STianyu Lan reg <= HV_REGISTER_SINT15) 223*faff4406STianyu Lan wrmsrl(reg, value | 1 << 20); 224*faff4406STianyu Lan } else { 225*faff4406STianyu Lan wrmsrl(reg, value); 226*faff4406STianyu Lan } 227*faff4406STianyu Lan } 228*faff4406STianyu Lan 22979cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */ 23079cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {} 2312ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {} 23293286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {} 23393286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {} 23493286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {}; 235a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 236a46d15ccSVitaly Kuznetsov { 237a46d15ccSVitaly Kuznetsov return NULL; 238a46d15ccSVitaly Kuznetsov } 239eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } 240cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as, 241cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data) 242cc4edae4SLan Tianyu { 243cc4edae4SLan Tianyu return -1; 244cc4edae4SLan Tianyu } 245*faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { } 246*faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; } 247810a5212STianyu Lan static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages, 248810a5212STianyu Lan bool visible) 249810a5212STianyu Lan { 250810a5212STianyu Lan return -1; 251810a5212STianyu Lan } 25279cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */ 25379cadff2SVitaly Kuznetsov 254765e33f5SMichael Kelley 255765e33f5SMichael Kelley #include <asm-generic/mshyperv.h> 256765e33f5SMichael Kelley 257a2a47c6cSKy Srinivasan #endif 258