Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_MSHYPER_H |
| 3 | #define _ASM_X86_MSHYPER_H |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 4 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 5 | #include <linux/types.h> |
Vitaly Kuznetsov | 806c892 | 2017-08-02 18:09:17 +0200 | [diff] [blame] | 6 | #include <linux/nmi.h> |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame] | 7 | #include <linux/msi.h> |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 8 | #include <asm/io.h> |
Vitaly Kuznetsov | 5a48580 | 2018-03-20 15:02:05 +0100 | [diff] [blame] | 9 | #include <asm/hyperv-tlfs.h> |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 10 | #include <asm/nospec-branch.h> |
Michael Kelley | b9d8cf2 | 2020-08-09 18:29:51 -0700 | [diff] [blame] | 11 | #include <asm/paravirt.h> |
Joseph Salisbury | 753ed9c | 2021-04-16 17:43:03 -0700 | [diff] [blame] | 12 | #include <asm/mshyperv.h> |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 13 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 14 | union hv_ghcb; |
| 15 | |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 16 | DECLARE_STATIC_KEY_FALSE(isolation_type_snp); |
| 17 | |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 18 | typedef int (*hyperv_fill_flush_list_func)( |
| 19 | struct hv_guest_mapping_flush_list *flush, |
| 20 | void *data); |
| 21 | |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 22 | #define hv_get_raw_timer() rdtsc_ordered() |
| 23 | |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 24 | void hyperv_vector_handler(struct pt_regs *regs); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 25 | |
| 26 | #if IS_ENABLED(CONFIG_HYPERV) |
Dexuan Cui | dfe94d4 | 2020-12-21 22:55:41 -0800 | [diff] [blame] | 27 | extern int hyperv_init_cpuhp; |
| 28 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 29 | extern void *hv_hypercall_pg; |
| 30 | |
Wei Liu | 99a0f46 | 2021-02-03 15:04:25 +0000 | [diff] [blame] | 31 | extern u64 hv_current_partition_id; |
| 32 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 33 | extern union hv_ghcb __percpu **hv_ghcb_pg; |
Tianyu Lan | 0cc4f6d | 2021-10-25 08:21:06 -0400 | [diff] [blame] | 34 | |
Wei Liu | 86b5ec3 | 2021-02-03 15:04:28 +0000 | [diff] [blame] | 35 | int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); |
| 36 | int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); |
| 37 | int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); |
| 38 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 39 | static inline u64 hv_do_hypercall(u64 control, void *input, void *output) |
| 40 | { |
| 41 | u64 input_address = input ? virt_to_phys(input) : 0; |
| 42 | u64 output_address = output ? virt_to_phys(output) : 0; |
| 43 | u64 hv_status; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 44 | |
| 45 | #ifdef CONFIG_X86_64 |
| 46 | if (!hv_hypercall_pg) |
| 47 | return U64_MAX; |
| 48 | |
| 49 | __asm__ __volatile__("mov %4, %%r8\n" |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 50 | CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 51 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 52 | "+c" (control), "+d" (input_address) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 53 | : "r" (output_address), |
| 54 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 55 | : "cc", "memory", "r8", "r9", "r10", "r11"); |
| 56 | #else |
| 57 | u32 input_address_hi = upper_32_bits(input_address); |
| 58 | u32 input_address_lo = lower_32_bits(input_address); |
| 59 | u32 output_address_hi = upper_32_bits(output_address); |
| 60 | u32 output_address_lo = lower_32_bits(output_address); |
| 61 | |
| 62 | if (!hv_hypercall_pg) |
| 63 | return U64_MAX; |
| 64 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 65 | __asm__ __volatile__(CALL_NOSPEC |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 66 | : "=A" (hv_status), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 67 | "+c" (input_address_lo), ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 68 | : "A" (control), |
| 69 | "b" (input_address_hi), |
| 70 | "D"(output_address_hi), "S"(output_address_lo), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 71 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 72 | : "cc", "memory"); |
| 73 | #endif /* !x86_64 */ |
| 74 | return hv_status; |
| 75 | } |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 76 | |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 77 | /* Fast hypercall with 8 bytes of input and no output */ |
| 78 | static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) |
| 79 | { |
| 80 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 81 | |
| 82 | #ifdef CONFIG_X86_64 |
| 83 | { |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 84 | __asm__ __volatile__(CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 85 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 86 | "+c" (control), "+d" (input1) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 87 | : THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 88 | : "cc", "r8", "r9", "r10", "r11"); |
| 89 | } |
| 90 | #else |
| 91 | { |
| 92 | u32 input1_hi = upper_32_bits(input1); |
| 93 | u32 input1_lo = lower_32_bits(input1); |
| 94 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 95 | __asm__ __volatile__ (CALL_NOSPEC |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 96 | : "=A"(hv_status), |
| 97 | "+c"(input1_lo), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 98 | ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 99 | : "A" (control), |
| 100 | "b" (input1_hi), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 101 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 102 | : "cc", "edi", "esi"); |
| 103 | } |
| 104 | #endif |
| 105 | return hv_status; |
| 106 | } |
| 107 | |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 108 | /* Fast hypercall with 16 bytes of input */ |
| 109 | static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) |
| 110 | { |
| 111 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 112 | |
| 113 | #ifdef CONFIG_X86_64 |
| 114 | { |
| 115 | __asm__ __volatile__("mov %4, %%r8\n" |
| 116 | CALL_NOSPEC |
| 117 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
| 118 | "+c" (control), "+d" (input1) |
| 119 | : "r" (input2), |
| 120 | THUNK_TARGET(hv_hypercall_pg) |
| 121 | : "cc", "r8", "r9", "r10", "r11"); |
| 122 | } |
| 123 | #else |
| 124 | { |
| 125 | u32 input1_hi = upper_32_bits(input1); |
| 126 | u32 input1_lo = lower_32_bits(input1); |
| 127 | u32 input2_hi = upper_32_bits(input2); |
| 128 | u32 input2_lo = lower_32_bits(input2); |
| 129 | |
| 130 | __asm__ __volatile__ (CALL_NOSPEC |
| 131 | : "=A"(hv_status), |
| 132 | "+c"(input1_lo), ASM_CALL_CONSTRAINT |
| 133 | : "A" (control), "b" (input1_hi), |
| 134 | "D"(input2_hi), "S"(input2_lo), |
| 135 | THUNK_TARGET(hv_hypercall_pg) |
| 136 | : "cc"); |
| 137 | } |
| 138 | #endif |
Yi Wang | b42967d | 2018-10-29 15:17:31 +0800 | [diff] [blame] | 139 | return hv_status; |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 142 | extern struct hv_vp_assist_page **hv_vp_assist_page; |
| 143 | |
| 144 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 145 | { |
| 146 | if (!hv_vp_assist_page) |
| 147 | return NULL; |
| 148 | |
| 149 | return hv_vp_assist_page[cpu]; |
| 150 | } |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 151 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 152 | void __init hyperv_init(void); |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 153 | void hyperv_setup_mmu_ops(void); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 154 | void set_hv_tscchange_cb(void (*cb)(void)); |
| 155 | void clear_hv_tscchange_cb(void); |
| 156 | void hyperv_stop_tsc_emulation(void); |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 157 | int hyperv_flush_guest_mapping(u64 as); |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 158 | int hyperv_flush_guest_mapping_range(u64 as, |
| 159 | hyperv_fill_flush_list_func fill_func, void *data); |
| 160 | int hyperv_fill_flush_guest_mapping_list( |
| 161 | struct hv_guest_mapping_flush_list *flush, |
| 162 | u64 start_gfn, u64 end_gfn); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 163 | |
| 164 | #ifdef CONFIG_X86_64 |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 165 | void hv_apic_init(void); |
Yi Sun | 3a025de | 2018-10-08 16:29:34 +0800 | [diff] [blame] | 166 | void __init hv_init_spinlocks(void); |
| 167 | bool hv_vcpu_is_preempted(int vcpu); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 168 | #else |
| 169 | static inline void hv_apic_init(void) {} |
| 170 | #endif |
| 171 | |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame] | 172 | static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, |
| 173 | struct msi_desc *msi_desc) |
| 174 | { |
Wei Liu | d589ae6 | 2021-02-03 15:04:30 +0000 | [diff] [blame] | 175 | msi_entry->address.as_uint32 = msi_desc->msg.address_lo; |
| 176 | msi_entry->data.as_uint32 = msi_desc->msg.data; |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Wei Liu | e39397d | 2021-02-03 15:04:34 +0000 | [diff] [blame] | 179 | struct irq_domain *hv_create_pci_msi_domain(void); |
| 180 | |
Wei Liu | fb5ef35 | 2021-02-03 15:04:35 +0000 | [diff] [blame] | 181 | int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector, |
| 182 | struct hv_interrupt_entry *entry); |
| 183 | int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry); |
Tianyu Lan | 810a521 | 2021-10-25 08:21:08 -0400 | [diff] [blame] | 184 | int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible); |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 185 | |
| 186 | #ifdef CONFIG_AMD_MEM_ENCRYPT |
| 187 | void hv_ghcb_msr_write(u64 msr, u64 value); |
| 188 | void hv_ghcb_msr_read(u64 msr, u64 *value); |
| 189 | #else |
| 190 | static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} |
| 191 | static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} |
| 192 | #endif |
| 193 | |
| 194 | extern bool hv_isolation_type_snp(void); |
| 195 | |
| 196 | static inline bool hv_is_synic_reg(unsigned int reg) |
| 197 | { |
| 198 | if ((reg >= HV_REGISTER_SCONTROL) && |
| 199 | (reg <= HV_REGISTER_SINT15)) |
| 200 | return true; |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | static inline u64 hv_get_register(unsigned int reg) |
| 205 | { |
| 206 | u64 value; |
| 207 | |
| 208 | if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) |
| 209 | hv_ghcb_msr_read(reg, &value); |
| 210 | else |
| 211 | rdmsrl(reg, value); |
| 212 | return value; |
| 213 | } |
| 214 | |
| 215 | static inline void hv_set_register(unsigned int reg, u64 value) |
| 216 | { |
| 217 | if (hv_is_synic_reg(reg) && hv_isolation_type_snp()) { |
| 218 | hv_ghcb_msr_write(reg, value); |
| 219 | |
| 220 | /* Write proxy bit via wrmsl instruction */ |
| 221 | if (reg >= HV_REGISTER_SINT0 && |
| 222 | reg <= HV_REGISTER_SINT15) |
| 223 | wrmsrl(reg, value | 1 << 20); |
| 224 | } else { |
| 225 | wrmsrl(reg, value); |
| 226 | } |
| 227 | } |
| 228 | |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 229 | #else /* CONFIG_HYPERV */ |
| 230 | static inline void hyperv_init(void) {} |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 231 | static inline void hyperv_setup_mmu_ops(void) {} |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 232 | static inline void set_hv_tscchange_cb(void (*cb)(void)) {} |
| 233 | static inline void clear_hv_tscchange_cb(void) {} |
| 234 | static inline void hyperv_stop_tsc_emulation(void) {}; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 235 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 236 | { |
| 237 | return NULL; |
| 238 | } |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 239 | static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 240 | static inline int hyperv_flush_guest_mapping_range(u64 as, |
| 241 | hyperv_fill_flush_list_func fill_func, void *data) |
| 242 | { |
| 243 | return -1; |
| 244 | } |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 245 | static inline void hv_set_register(unsigned int reg, u64 value) { } |
| 246 | static inline u64 hv_get_register(unsigned int reg) { return 0; } |
Tianyu Lan | 810a521 | 2021-10-25 08:21:08 -0400 | [diff] [blame] | 247 | static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages, |
| 248 | bool visible) |
| 249 | { |
| 250 | return -1; |
| 251 | } |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 252 | #endif /* CONFIG_HYPERV */ |
| 253 | |
Michael Kelley | 765e33f | 2019-05-30 00:14:00 +0000 | [diff] [blame] | 254 | |
| 255 | #include <asm-generic/mshyperv.h> |
| 256 | |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 257 | #endif |