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 14812b0597SMichael Kelley /* 15812b0597SMichael Kelley * Hyper-V always provides a single IO-APIC at this MMIO address. 16812b0597SMichael Kelley * Ideally, the value should be looked up in ACPI tables, but it 17812b0597SMichael Kelley * is needed for mapping the IO-APIC early in boot on Confidential 18812b0597SMichael Kelley * VMs, before ACPI functions can be used. 19812b0597SMichael Kelley */ 20812b0597SMichael Kelley #define HV_IOAPIC_BASE_ADDRESS 0xfec00000 21812b0597SMichael Kelley 223be1bc2fSSaurabh Sengar #define HV_VTL_NORMAL 0x0 233be1bc2fSSaurabh Sengar #define HV_VTL_SECURE 0x1 243be1bc2fSSaurabh Sengar #define HV_VTL_MGMT 0x2 253be1bc2fSSaurabh Sengar 26faff4406STianyu Lan union hv_ghcb; 27faff4406STianyu Lan 280cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp); 29d6e2d652STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_en_snp); 300cc4f6d9STianyu Lan 31cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)( 32cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 33cc4edae4SLan Tianyu void *data); 34cc4edae4SLan Tianyu 35bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs); 368730046cSK. Y. Srinivasan 370a7a0058SSaurabh Sengar static inline unsigned char hv_get_nmi_reason(void) 380a7a0058SSaurabh Sengar { 390a7a0058SSaurabh Sengar return 0; 400a7a0058SSaurabh Sengar } 410a7a0058SSaurabh Sengar 428730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV) 43dfe94d40SDexuan Cui extern int hyperv_init_cpuhp; 44dfe94d40SDexuan Cui 45fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg; 46fc53662fSVitaly Kuznetsov 4799a0f46aSWei Liu extern u64 hv_current_partition_id; 4899a0f46aSWei Liu 49e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg; 500cc4f6d9STianyu Lan 5148b1f683STianyu Lan extern bool hv_isolation_type_en_snp(void); 52*44676bb9STianyu Lan /* 53*44676bb9STianyu Lan * DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA 54*44676bb9STianyu Lan * to start AP in enlightened SEV guest. 55*44676bb9STianyu Lan */ 56*44676bb9STianyu Lan #define HV_AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL 57*44676bb9STianyu Lan #define HV_AP_SEGMENT_LIMIT 0xffffffff 5848b1f683STianyu Lan 5986b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); 6086b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); 6186b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); 6286b5ec35SWei Liu 63fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 64fc53662fSVitaly Kuznetsov { 65fc53662fSVitaly Kuznetsov u64 input_address = input ? virt_to_phys(input) : 0; 66fc53662fSVitaly Kuznetsov u64 output_address = output ? virt_to_phys(output) : 0; 67fc53662fSVitaly Kuznetsov u64 hv_status; 68fc53662fSVitaly Kuznetsov 69fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64 7048b1f683STianyu Lan if (hv_isolation_type_en_snp()) { 7148b1f683STianyu Lan __asm__ __volatile__("mov %4, %%r8\n" 7248b1f683STianyu Lan "vmmcall" 7348b1f683STianyu Lan : "=a" (hv_status), ASM_CALL_CONSTRAINT, 7448b1f683STianyu Lan "+c" (control), "+d" (input_address) 7548b1f683STianyu Lan : "r" (output_address) 7648b1f683STianyu Lan : "cc", "memory", "r8", "r9", "r10", "r11"); 7748b1f683STianyu Lan return hv_status; 7848b1f683STianyu Lan } 7948b1f683STianyu Lan 80fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 81fc53662fSVitaly Kuznetsov return U64_MAX; 82fc53662fSVitaly Kuznetsov 83fc53662fSVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 84e70e5892SDavid Woodhouse CALL_NOSPEC 85f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 86fc53662fSVitaly Kuznetsov "+c" (control), "+d" (input_address) 87e70e5892SDavid Woodhouse : "r" (output_address), 88e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 89fc53662fSVitaly Kuznetsov : "cc", "memory", "r8", "r9", "r10", "r11"); 90fc53662fSVitaly Kuznetsov #else 91fc53662fSVitaly Kuznetsov u32 input_address_hi = upper_32_bits(input_address); 92fc53662fSVitaly Kuznetsov u32 input_address_lo = lower_32_bits(input_address); 93fc53662fSVitaly Kuznetsov u32 output_address_hi = upper_32_bits(output_address); 94fc53662fSVitaly Kuznetsov u32 output_address_lo = lower_32_bits(output_address); 95fc53662fSVitaly Kuznetsov 96fc53662fSVitaly Kuznetsov if (!hv_hypercall_pg) 97fc53662fSVitaly Kuznetsov return U64_MAX; 98fc53662fSVitaly Kuznetsov 99e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 100fc53662fSVitaly Kuznetsov : "=A" (hv_status), 101f5caf621SJosh Poimboeuf "+c" (input_address_lo), ASM_CALL_CONSTRAINT 102fc53662fSVitaly Kuznetsov : "A" (control), 103fc53662fSVitaly Kuznetsov "b" (input_address_hi), 104fc53662fSVitaly Kuznetsov "D"(output_address_hi), "S"(output_address_lo), 105e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 106fc53662fSVitaly Kuznetsov : "cc", "memory"); 107fc53662fSVitaly Kuznetsov #endif /* !x86_64 */ 108fc53662fSVitaly Kuznetsov return hv_status; 109fc53662fSVitaly Kuznetsov } 110dee863b5SVitaly Kuznetsov 111f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */ 112f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output) 1136a8edbd0SVitaly Kuznetsov { 114f0d2f5c2SJinank Jain return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output); 115f0d2f5c2SJinank Jain } 116f0d2f5c2SJinank Jain 117f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */ 118f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1) 119f0d2f5c2SJinank Jain { 120f0d2f5c2SJinank Jain u64 hv_status; 1216a8edbd0SVitaly Kuznetsov 1226a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64 12348b1f683STianyu Lan if (hv_isolation_type_en_snp()) { 12448b1f683STianyu Lan __asm__ __volatile__( 12548b1f683STianyu Lan "vmmcall" 12648b1f683STianyu Lan : "=a" (hv_status), ASM_CALL_CONSTRAINT, 12748b1f683STianyu Lan "+c" (control), "+d" (input1) 12848b1f683STianyu Lan :: "cc", "r8", "r9", "r10", "r11"); 12948b1f683STianyu Lan } else { 130e70e5892SDavid Woodhouse __asm__ __volatile__(CALL_NOSPEC 131f5caf621SJosh Poimboeuf : "=a" (hv_status), ASM_CALL_CONSTRAINT, 1326a8edbd0SVitaly Kuznetsov "+c" (control), "+d" (input1) 133e70e5892SDavid Woodhouse : THUNK_TARGET(hv_hypercall_pg) 1346a8edbd0SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 1356a8edbd0SVitaly Kuznetsov } 1366a8edbd0SVitaly Kuznetsov #else 1376a8edbd0SVitaly Kuznetsov { 1386a8edbd0SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 1396a8edbd0SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 1406a8edbd0SVitaly Kuznetsov 141e70e5892SDavid Woodhouse __asm__ __volatile__ (CALL_NOSPEC 1426a8edbd0SVitaly Kuznetsov : "=A"(hv_status), 1436a8edbd0SVitaly Kuznetsov "+c"(input1_lo), 144f5caf621SJosh Poimboeuf ASM_CALL_CONSTRAINT 1456a8edbd0SVitaly Kuznetsov : "A" (control), 1466a8edbd0SVitaly Kuznetsov "b" (input1_hi), 147e70e5892SDavid Woodhouse THUNK_TARGET(hv_hypercall_pg) 1486a8edbd0SVitaly Kuznetsov : "cc", "edi", "esi"); 1496a8edbd0SVitaly Kuznetsov } 1506a8edbd0SVitaly Kuznetsov #endif 1516a8edbd0SVitaly Kuznetsov return hv_status; 1526a8edbd0SVitaly Kuznetsov } 1536a8edbd0SVitaly Kuznetsov 154f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 15553e52966SVitaly Kuznetsov { 156f0d2f5c2SJinank Jain u64 control = (u64)code | HV_HYPERCALL_FAST_BIT; 157f0d2f5c2SJinank Jain 158f0d2f5c2SJinank Jain return _hv_do_fast_hypercall8(control, input1); 159f0d2f5c2SJinank Jain } 160f0d2f5c2SJinank Jain 161f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1) 162f0d2f5c2SJinank Jain { 163f0d2f5c2SJinank Jain u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED; 164f0d2f5c2SJinank Jain 165f0d2f5c2SJinank Jain return _hv_do_fast_hypercall8(control, input1); 166f0d2f5c2SJinank Jain } 167f0d2f5c2SJinank Jain 168f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */ 169f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2) 170f0d2f5c2SJinank Jain { 171f0d2f5c2SJinank Jain u64 hv_status; 17253e52966SVitaly Kuznetsov 17353e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64 17448b1f683STianyu Lan if (hv_isolation_type_en_snp()) { 17548b1f683STianyu Lan __asm__ __volatile__("mov %4, %%r8\n" 17648b1f683STianyu Lan "vmmcall" 17748b1f683STianyu Lan : "=a" (hv_status), ASM_CALL_CONSTRAINT, 17848b1f683STianyu Lan "+c" (control), "+d" (input1) 17948b1f683STianyu Lan : "r" (input2) 18048b1f683STianyu Lan : "cc", "r8", "r9", "r10", "r11"); 18148b1f683STianyu Lan } else { 18253e52966SVitaly Kuznetsov __asm__ __volatile__("mov %4, %%r8\n" 18353e52966SVitaly Kuznetsov CALL_NOSPEC 18453e52966SVitaly Kuznetsov : "=a" (hv_status), ASM_CALL_CONSTRAINT, 18553e52966SVitaly Kuznetsov "+c" (control), "+d" (input1) 18653e52966SVitaly Kuznetsov : "r" (input2), 18753e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 18853e52966SVitaly Kuznetsov : "cc", "r8", "r9", "r10", "r11"); 18953e52966SVitaly Kuznetsov } 19053e52966SVitaly Kuznetsov #else 19153e52966SVitaly Kuznetsov { 19253e52966SVitaly Kuznetsov u32 input1_hi = upper_32_bits(input1); 19353e52966SVitaly Kuznetsov u32 input1_lo = lower_32_bits(input1); 19453e52966SVitaly Kuznetsov u32 input2_hi = upper_32_bits(input2); 19553e52966SVitaly Kuznetsov u32 input2_lo = lower_32_bits(input2); 19653e52966SVitaly Kuznetsov 19753e52966SVitaly Kuznetsov __asm__ __volatile__ (CALL_NOSPEC 19853e52966SVitaly Kuznetsov : "=A"(hv_status), 19953e52966SVitaly Kuznetsov "+c"(input1_lo), ASM_CALL_CONSTRAINT 20053e52966SVitaly Kuznetsov : "A" (control), "b" (input1_hi), 20153e52966SVitaly Kuznetsov "D"(input2_hi), "S"(input2_lo), 20253e52966SVitaly Kuznetsov THUNK_TARGET(hv_hypercall_pg) 20353e52966SVitaly Kuznetsov : "cc"); 20453e52966SVitaly Kuznetsov } 20553e52966SVitaly Kuznetsov #endif 20653e52966SVitaly Kuznetsov return hv_status; 20753e52966SVitaly Kuznetsov } 20853e52966SVitaly Kuznetsov 209f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 210f0d2f5c2SJinank Jain { 211f0d2f5c2SJinank Jain u64 control = (u64)code | HV_HYPERCALL_FAST_BIT; 212f0d2f5c2SJinank Jain 213f0d2f5c2SJinank Jain return _hv_do_fast_hypercall16(control, input1, input2); 214f0d2f5c2SJinank Jain } 215f0d2f5c2SJinank Jain 216f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2) 217f0d2f5c2SJinank Jain { 218f0d2f5c2SJinank Jain u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED; 219f0d2f5c2SJinank Jain 220f0d2f5c2SJinank Jain return _hv_do_fast_hypercall16(control, input1, input2); 221f0d2f5c2SJinank Jain } 222f0d2f5c2SJinank Jain 223a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page; 224a46d15ccSVitaly Kuznetsov 225a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 226a46d15ccSVitaly Kuznetsov { 227a46d15ccSVitaly Kuznetsov if (!hv_vp_assist_page) 228a46d15ccSVitaly Kuznetsov return NULL; 229a46d15ccSVitaly Kuznetsov 230a46d15ccSVitaly Kuznetsov return hv_vp_assist_page[cpu]; 231a46d15ccSVitaly Kuznetsov } 2327415aea6SVitaly Kuznetsov 2336b48cb5fSK. Y. Srinivasan void __init hyperv_init(void); 2342ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void); 23593286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void)); 23693286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void); 23793286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void); 238eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as); 239cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as, 240cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data); 241cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list( 242cc4edae4SLan Tianyu struct hv_guest_mapping_flush_list *flush, 243cc4edae4SLan Tianyu u64 start_gfn, u64 end_gfn); 2442d2ccf24SThomas Gleixner 2452d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64 2466b48cb5fSK. Y. Srinivasan void hv_apic_init(void); 2473a025de6SYi Sun void __init hv_init_spinlocks(void); 2483a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu); 2492d2ccf24SThomas Gleixner #else 2502d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {} 2512d2ccf24SThomas Gleixner #endif 2522d2ccf24SThomas Gleixner 253e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void); 254e39397d1SWei Liu 255fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector, 256fb5ef351SWei Liu struct hv_interrupt_entry *entry); 257fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry); 258faff4406STianyu Lan 259faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT 260faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value); 261faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value); 26249d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void); 263611d4c71SGuilherme G. Piccoli void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason); 264812b0597SMichael Kelley void hv_vtom_init(void); 265*44676bb9STianyu Lan int hv_snp_boot_ap(int cpu, unsigned long start_ip); 266faff4406STianyu Lan #else 267faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} 268faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} 26949d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; } 27049d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {} 271812b0597SMichael Kelley static inline void hv_vtom_init(void) {} 272*44676bb9STianyu Lan static int hv_snp_boot_ap(int cpu, unsigned long start_ip) { return 0; } 273faff4406STianyu Lan #endif 274faff4406STianyu Lan 275faff4406STianyu Lan extern bool hv_isolation_type_snp(void); 276faff4406STianyu Lan 277faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg) 278faff4406STianyu Lan { 279b14033a3SNuno Das Neves return (reg >= HV_REGISTER_SCONTROL) && 280b14033a3SNuno Das Neves (reg <= HV_REGISTER_SINT15); 281b14033a3SNuno Das Neves } 282b14033a3SNuno Das Neves 283b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg) 284b14033a3SNuno Das Neves { 285b14033a3SNuno Das Neves return (reg >= HV_REGISTER_SINT0) && 286b14033a3SNuno Das Neves (reg <= HV_REGISTER_SINT15); 287faff4406STianyu Lan } 288faff4406STianyu Lan 2897fec185aSJinank Jain u64 hv_get_register(unsigned int reg); 2907fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value); 2917fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg); 2927fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value); 293faff4406STianyu Lan 294e39acc37SPeter Zijlstra static __always_inline u64 hv_raw_get_register(unsigned int reg) 295e39acc37SPeter Zijlstra { 296e39acc37SPeter Zijlstra return __rdmsr(reg); 297e39acc37SPeter Zijlstra } 298e39acc37SPeter Zijlstra 29979cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */ 30079cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {} 3012ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {} 30293286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {} 30393286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {} 30493286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {}; 305a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 306a46d15ccSVitaly Kuznetsov { 307a46d15ccSVitaly Kuznetsov return NULL; 308a46d15ccSVitaly Kuznetsov } 309eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } 310cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as, 311cc4edae4SLan Tianyu hyperv_fill_flush_list_func fill_func, void *data) 312cc4edae4SLan Tianyu { 313cc4edae4SLan Tianyu return -1; 314cc4edae4SLan Tianyu } 315faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { } 316faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; } 3177fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { } 3187fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; } 31979cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */ 32079cadff2SVitaly Kuznetsov 321765e33f5SMichael Kelley 3223be1bc2fSSaurabh Sengar #ifdef CONFIG_HYPERV_VTL_MODE 3233be1bc2fSSaurabh Sengar void __init hv_vtl_init_platform(void); 3243be1bc2fSSaurabh Sengar #else 3253be1bc2fSSaurabh Sengar static inline void __init hv_vtl_init_platform(void) {} 3263be1bc2fSSaurabh Sengar #endif 3273be1bc2fSSaurabh Sengar 328765e33f5SMichael Kelley #include <asm-generic/mshyperv.h> 329765e33f5SMichael Kelley 330a2a47c6cSKy Srinivasan #endif 331