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*0cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp); 15*0cc4f6d9STianyu Lan 16cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)( 17cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 18cc4edae4SLan Tianyu void *data); 19cc4edae4SLan Tianyu 20f3c5e63cSMichael Kelley static inline void hv_set_register(unsigned int reg, u64 value) 21f3c5e63cSMichael Kelley { 22f3c5e63cSMichael Kelley wrmsrl(reg, value); 23f3c5e63cSMichael Kelley } 24d5116b40SK. Y. Srinivasan 25f3c5e63cSMichael Kelley static inline u64 hv_get_register(unsigned int reg) 26f3c5e63cSMichael Kelley { 27f3c5e63cSMichael Kelley u64 value; 28155e4a2fSK. Y. Srinivasan 29f3c5e63cSMichael Kelley rdmsrl(reg, value); 30f3c5e63cSMichael Kelley return value; 31f3c5e63cSMichael Kelley } 328e307bf8SK. Y. Srinivasan 33dd2cb348SMichael Kelley #define hv_get_raw_timer() rdtsc_ordered() 34dd2cb348SMichael Kelley 35bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs); 368730046cSK. Y. Srinivasan 378730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV) 38dfe94d40SDexuan Cui extern int hyperv_init_cpuhp; 39dfe94d40SDexuan Cui 40fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg; 41fc53662fSVitaly Kuznetsov 4299a0f46aSWei Liu extern u64 hv_current_partition_id; 4399a0f46aSWei Liu 44*0cc4f6d9STianyu Lan extern void __percpu **hv_ghcb_pg; 45*0cc4f6d9STianyu Lan 4686b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); 4786b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); 4886b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); 4986b5ec35SWei Liu 50fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 51fc53662fSVitaly Kuznetsov { 52fc53662fSVitaly Kuznetsov u64 input_address = input ? virt_to_phys(input) : 0; 53fc53662fSVitaly Kuznetsov u64 output_address = output ? virt_to_phys(output) : 0; 54fc53662fSVitaly Kuznetsov u64 hv_status; 55fc53662fSVitaly Kuznetsov 56fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64 57fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 58fc53662fSVitaly Kuznetsov return U64_MAX; 59fc53662fSVitaly Kuznetsov 60fc53662fSVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 61e70e5892SDavid Woodhouse CALL_NOSPEC 62f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 63fc53662fSVitaly Kuznetsov "+c" (control), "+d" (input_address) 64e70e5892SDavid Woodhouse : "r" (output_address), 65e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 66fc53662fSVitaly Kuznetsov : "cc", "memory", "r8", "r9", "r10", "r11"); 67fc53662fSVitaly Kuznetsov #else 68fc53662fSVitaly Kuznetsov u32 input_address_hi = upper_32_bits(input_address); 69fc53662fSVitaly Kuznetsov u32 input_address_lo = lower_32_bits(input_address); 70fc53662fSVitaly Kuznetsov u32 output_address_hi = upper_32_bits(output_address); 71fc53662fSVitaly Kuznetsov u32 output_address_lo = lower_32_bits(output_address); 72fc53662fSVitaly Kuznetsov 73fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 74fc53662fSVitaly Kuznetsov return U64_MAX; 75fc53662fSVitaly Kuznetsov 76e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 77fc53662fSVitaly Kuznetsov : "=A" (hv_status), 78f5caf621SJosh Poimboeuf "+c" (input_address_lo), ASM_CALL_CONSTRAINT 79fc53662fSVitaly Kuznetsov : "A" (control), 80fc53662fSVitaly Kuznetsov "b" (input_address_hi), 81fc53662fSVitaly Kuznetsov "D"(output_address_hi), "S"(output_address_lo), 82e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 83fc53662fSVitaly Kuznetsov : "cc", "memory"); 84fc53662fSVitaly Kuznetsov #endif /* !x86_64 */ 85fc53662fSVitaly Kuznetsov return hv_status; 86fc53662fSVitaly Kuznetsov } 87dee863b5SVitaly Kuznetsov 886a8edbd0SVitaly Kuznetsov /* Fast hypercall with 8 bytes of input and no output */ 896a8edbd0SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 906a8edbd0SVitaly Kuznetsov { 916a8edbd0SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 926a8edbd0SVitaly Kuznetsov 936a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64 946a8edbd0SVitaly Kuznetsov { 95e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 96f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 976a8edbd0SVitaly Kuznetsov "+c" (control), "+d" (input1) 98e70e5892SDavid Woodhouse : THUNK_TARGET(hv_hypercall_pg) 996a8edbd0SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 1006a8edbd0SVitaly Kuznetsov } 1016a8edbd0SVitaly Kuznetsov #else 1026a8edbd0SVitaly Kuznetsov { 1036a8edbd0SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 1046a8edbd0SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 1056a8edbd0SVitaly Kuznetsov 106e70e5892SDavid Woodhouse __asm__ __volatile__ (CALL_NOSPEC 1076a8edbd0SVitaly Kuznetsov : "=A"(hv_status), 1086a8edbd0SVitaly Kuznetsov "+c"(input1_lo), 109f5caf621SJosh Poimboeuf ASM_CALL_CONSTRAINT 1106a8edbd0SVitaly Kuznetsov : "A" (control), 1116a8edbd0SVitaly Kuznetsov "b" (input1_hi), 112e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 1136a8edbd0SVitaly Kuznetsov : "cc", "edi", "esi"); 1146a8edbd0SVitaly Kuznetsov } 1156a8edbd0SVitaly Kuznetsov #endif 1166a8edbd0SVitaly Kuznetsov return hv_status; 1176a8edbd0SVitaly Kuznetsov } 1186a8edbd0SVitaly Kuznetsov 11953e52966SVitaly Kuznetsov /* Fast hypercall with 16 bytes of input */ 12053e52966SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 12153e52966SVitaly Kuznetsov { 12253e52966SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 12353e52966SVitaly Kuznetsov 12453e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64 12553e52966SVitaly Kuznetsov { 12653e52966SVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 12753e52966SVitaly Kuznetsov CALL_NOSPEC 12853e52966SVitaly Kuznetsov : "=a" (hv_status), ASM_CALL_CONSTRAINT, 12953e52966SVitaly Kuznetsov "+c" (control), "+d" (input1) 13053e52966SVitaly Kuznetsov : "r" (input2), 13153e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 13253e52966SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 13353e52966SVitaly Kuznetsov } 13453e52966SVitaly Kuznetsov #else 13553e52966SVitaly Kuznetsov { 13653e52966SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 13753e52966SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 13853e52966SVitaly Kuznetsov u32 input2_hi = upper_32_bits(input2); 13953e52966SVitaly Kuznetsov u32 input2_lo = lower_32_bits(input2); 14053e52966SVitaly Kuznetsov 14153e52966SVitaly Kuznetsov __asm__ __volatile__ (CALL_NOSPEC 14253e52966SVitaly Kuznetsov : "=A"(hv_status), 14353e52966SVitaly Kuznetsov "+c"(input1_lo), ASM_CALL_CONSTRAINT 14453e52966SVitaly Kuznetsov : "A" (control), "b" (input1_hi), 14553e52966SVitaly Kuznetsov "D"(input2_hi), "S"(input2_lo), 14653e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 14753e52966SVitaly Kuznetsov : "cc"); 14853e52966SVitaly Kuznetsov } 14953e52966SVitaly Kuznetsov #endif 15053e52966SVitaly Kuznetsov return hv_status; 15153e52966SVitaly Kuznetsov } 15253e52966SVitaly Kuznetsov 153a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page; 154a46d15ccSVitaly Kuznetsov 155a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 156a46d15ccSVitaly Kuznetsov { 157a46d15ccSVitaly Kuznetsov if (!hv_vp_assist_page) 158a46d15ccSVitaly Kuznetsov return NULL; 159a46d15ccSVitaly Kuznetsov 160a46d15ccSVitaly Kuznetsov return hv_vp_assist_page[cpu]; 161a46d15ccSVitaly Kuznetsov } 1627415aea6SVitaly Kuznetsov 1636b48cb5fSK. Y. Srinivasan void __init hyperv_init(void); 1642ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void); 16593286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void)); 16693286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void); 16793286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void); 168eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as); 169cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as, 170cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data); 171cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list( 172cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 173cc4edae4SLan Tianyu u64 start_gfn, u64 end_gfn); 1742d2ccf24SThomas Gleixner 1752d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64 1766b48cb5fSK. Y. Srinivasan void hv_apic_init(void); 1773a025de6SYi Sun void __init hv_init_spinlocks(void); 1783a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu); 1792d2ccf24SThomas Gleixner #else 1802d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {} 1812d2ccf24SThomas Gleixner #endif 1822d2ccf24SThomas Gleixner 1831cf106d9SBoqun Feng static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, 1841cf106d9SBoqun Feng struct msi_desc *msi_desc) 1851cf106d9SBoqun Feng { 186d589ae61SWei Liu msi_entry->address.as_uint32 = msi_desc->msg.address_lo; 187d589ae61SWei Liu msi_entry->data.as_uint32 = msi_desc->msg.data; 1881cf106d9SBoqun Feng } 1891cf106d9SBoqun Feng 190e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void); 191e39397d1SWei Liu 192fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector, 193fb5ef351SWei Liu struct hv_interrupt_entry *entry); 194fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry); 195fb5ef351SWei Liu 19679cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */ 19779cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {} 1982ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {} 19993286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {} 20093286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {} 20193286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {}; 202a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 203a46d15ccSVitaly Kuznetsov { 204a46d15ccSVitaly Kuznetsov return NULL; 205a46d15ccSVitaly Kuznetsov } 206eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } 207cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as, 208cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data) 209cc4edae4SLan Tianyu { 210cc4edae4SLan Tianyu return -1; 211cc4edae4SLan Tianyu } 21279cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */ 21379cadff2SVitaly Kuznetsov 214765e33f5SMichael Kelley 215765e33f5SMichael Kelley #include <asm-generic/mshyperv.h> 216765e33f5SMichael Kelley 217a2a47c6cSKy Srinivasan #endif 218