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> 12*753ed9c9SJoseph Salisbury #include <asm/mshyperv.h> 13e08cae41SH. Peter Anvin 14cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)( 15cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 16cc4edae4SLan Tianyu void *data); 17cc4edae4SLan Tianyu 18f3c5e63cSMichael Kelley static inline void hv_set_register(unsigned int reg, u64 value) 19f3c5e63cSMichael Kelley { 20f3c5e63cSMichael Kelley wrmsrl(reg, value); 21f3c5e63cSMichael Kelley } 22d5116b40SK. Y. Srinivasan 23f3c5e63cSMichael Kelley static inline u64 hv_get_register(unsigned int reg) 24f3c5e63cSMichael Kelley { 25f3c5e63cSMichael Kelley u64 value; 26155e4a2fSK. Y. Srinivasan 27f3c5e63cSMichael Kelley rdmsrl(reg, value); 28f3c5e63cSMichael Kelley return value; 29f3c5e63cSMichael Kelley } 308e307bf8SK. Y. Srinivasan 31dd2cb348SMichael Kelley #define hv_get_raw_timer() rdtsc_ordered() 32dd2cb348SMichael Kelley 33bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs); 348730046cSK. Y. Srinivasan 358730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV) 36dfe94d40SDexuan Cui extern int hyperv_init_cpuhp; 37dfe94d40SDexuan Cui 38fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg; 3968bb7bfbSK. Y. Srinivasan extern void __percpu **hyperv_pcpu_input_arg; 405d0f077eSWei Liu extern void __percpu **hyperv_pcpu_output_arg; 41fc53662fSVitaly Kuznetsov 4299a0f46aSWei Liu extern u64 hv_current_partition_id; 4399a0f46aSWei Liu 4486b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); 4586b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); 4686b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); 4786b5ec35SWei Liu 48fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 49fc53662fSVitaly Kuznetsov { 50fc53662fSVitaly Kuznetsov u64 input_address = input ? virt_to_phys(input) : 0; 51fc53662fSVitaly Kuznetsov u64 output_address = output ? virt_to_phys(output) : 0; 52fc53662fSVitaly Kuznetsov u64 hv_status; 53fc53662fSVitaly Kuznetsov 54fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64 55fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 56fc53662fSVitaly Kuznetsov return U64_MAX; 57fc53662fSVitaly Kuznetsov 58fc53662fSVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 59e70e5892SDavid Woodhouse CALL_NOSPEC 60f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 61fc53662fSVitaly Kuznetsov "+c" (control), "+d" (input_address) 62e70e5892SDavid Woodhouse : "r" (output_address), 63e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 64fc53662fSVitaly Kuznetsov : "cc", "memory", "r8", "r9", "r10", "r11"); 65fc53662fSVitaly Kuznetsov #else 66fc53662fSVitaly Kuznetsov u32 input_address_hi = upper_32_bits(input_address); 67fc53662fSVitaly Kuznetsov u32 input_address_lo = lower_32_bits(input_address); 68fc53662fSVitaly Kuznetsov u32 output_address_hi = upper_32_bits(output_address); 69fc53662fSVitaly Kuznetsov u32 output_address_lo = lower_32_bits(output_address); 70fc53662fSVitaly Kuznetsov 71fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 72fc53662fSVitaly Kuznetsov return U64_MAX; 73fc53662fSVitaly Kuznetsov 74e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 75fc53662fSVitaly Kuznetsov : "=A" (hv_status), 76f5caf621SJosh Poimboeuf "+c" (input_address_lo), ASM_CALL_CONSTRAINT 77fc53662fSVitaly Kuznetsov : "A" (control), 78fc53662fSVitaly Kuznetsov "b" (input_address_hi), 79fc53662fSVitaly Kuznetsov "D"(output_address_hi), "S"(output_address_lo), 80e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 81fc53662fSVitaly Kuznetsov : "cc", "memory"); 82fc53662fSVitaly Kuznetsov #endif /* !x86_64 */ 83fc53662fSVitaly Kuznetsov return hv_status; 84fc53662fSVitaly Kuznetsov } 85dee863b5SVitaly Kuznetsov 866a8edbd0SVitaly Kuznetsov /* Fast hypercall with 8 bytes of input and no output */ 876a8edbd0SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 886a8edbd0SVitaly Kuznetsov { 896a8edbd0SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 906a8edbd0SVitaly Kuznetsov 916a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64 926a8edbd0SVitaly Kuznetsov { 93e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 94f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 956a8edbd0SVitaly Kuznetsov "+c" (control), "+d" (input1) 96e70e5892SDavid Woodhouse : THUNK_TARGET(hv_hypercall_pg) 976a8edbd0SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 986a8edbd0SVitaly Kuznetsov } 996a8edbd0SVitaly Kuznetsov #else 1006a8edbd0SVitaly Kuznetsov { 1016a8edbd0SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 1026a8edbd0SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 1036a8edbd0SVitaly Kuznetsov 104e70e5892SDavid Woodhouse __asm__ __volatile__ (CALL_NOSPEC 1056a8edbd0SVitaly Kuznetsov : "=A"(hv_status), 1066a8edbd0SVitaly Kuznetsov "+c"(input1_lo), 107f5caf621SJosh Poimboeuf ASM_CALL_CONSTRAINT 1086a8edbd0SVitaly Kuznetsov : "A" (control), 1096a8edbd0SVitaly Kuznetsov "b" (input1_hi), 110e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 1116a8edbd0SVitaly Kuznetsov : "cc", "edi", "esi"); 1126a8edbd0SVitaly Kuznetsov } 1136a8edbd0SVitaly Kuznetsov #endif 1146a8edbd0SVitaly Kuznetsov return hv_status; 1156a8edbd0SVitaly Kuznetsov } 1166a8edbd0SVitaly Kuznetsov 11753e52966SVitaly Kuznetsov /* Fast hypercall with 16 bytes of input */ 11853e52966SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 11953e52966SVitaly Kuznetsov { 12053e52966SVitaly Kuznetsov u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 12153e52966SVitaly Kuznetsov 12253e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64 12353e52966SVitaly Kuznetsov { 12453e52966SVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 12553e52966SVitaly Kuznetsov CALL_NOSPEC 12653e52966SVitaly Kuznetsov : "=a" (hv_status), ASM_CALL_CONSTRAINT, 12753e52966SVitaly Kuznetsov "+c" (control), "+d" (input1) 12853e52966SVitaly Kuznetsov : "r" (input2), 12953e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 13053e52966SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 13153e52966SVitaly Kuznetsov } 13253e52966SVitaly Kuznetsov #else 13353e52966SVitaly Kuznetsov { 13453e52966SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 13553e52966SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 13653e52966SVitaly Kuznetsov u32 input2_hi = upper_32_bits(input2); 13753e52966SVitaly Kuznetsov u32 input2_lo = lower_32_bits(input2); 13853e52966SVitaly Kuznetsov 13953e52966SVitaly Kuznetsov __asm__ __volatile__ (CALL_NOSPEC 14053e52966SVitaly Kuznetsov : "=A"(hv_status), 14153e52966SVitaly Kuznetsov "+c"(input1_lo), ASM_CALL_CONSTRAINT 14253e52966SVitaly Kuznetsov : "A" (control), "b" (input1_hi), 14353e52966SVitaly Kuznetsov "D"(input2_hi), "S"(input2_lo), 14453e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 14553e52966SVitaly Kuznetsov : "cc"); 14653e52966SVitaly Kuznetsov } 14753e52966SVitaly Kuznetsov #endif 14853e52966SVitaly Kuznetsov return hv_status; 14953e52966SVitaly Kuznetsov } 15053e52966SVitaly Kuznetsov 151a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page; 152a46d15ccSVitaly Kuznetsov 153a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 154a46d15ccSVitaly Kuznetsov { 155a46d15ccSVitaly Kuznetsov if (!hv_vp_assist_page) 156a46d15ccSVitaly Kuznetsov return NULL; 157a46d15ccSVitaly Kuznetsov 158a46d15ccSVitaly Kuznetsov return hv_vp_assist_page[cpu]; 159a46d15ccSVitaly Kuznetsov } 1607415aea6SVitaly Kuznetsov 1616b48cb5fSK. Y. Srinivasan void __init hyperv_init(void); 1622ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void); 16393286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void)); 16493286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void); 16593286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void); 166eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as); 167cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as, 168cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data); 169cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list( 170cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 171cc4edae4SLan Tianyu u64 start_gfn, u64 end_gfn); 1722d2ccf24SThomas Gleixner 173e9977202SWei Liu extern bool hv_root_partition; 174e9977202SWei Liu 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