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> |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 11 | |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 12 | typedef int (*hyperv_fill_flush_list_func)( |
| 13 | struct hv_guest_mapping_flush_list *flush, |
| 14 | void *data); |
| 15 | |
Michael Kelley | 619a4c8 | 2018-06-05 13:37:53 -0700 | [diff] [blame] | 16 | #define hv_init_timer(timer, tick) \ |
| 17 | wrmsrl(HV_X64_MSR_STIMER0_COUNT + (2*timer), tick) |
| 18 | #define hv_init_timer_config(timer, val) \ |
| 19 | wrmsrl(HV_X64_MSR_STIMER0_CONFIG + (2*timer), val) |
K. Y. Srinivasan | d5116b4 | 2017-01-19 11:51:51 -0700 | [diff] [blame] | 20 | |
K. Y. Srinivasan | 155e4a2 | 2017-01-19 11:51:54 -0700 | [diff] [blame] | 21 | #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) |
| 22 | #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) |
| 23 | |
K. Y. Srinivasan | 8e307bf | 2017-01-19 11:51:55 -0700 | [diff] [blame] | 24 | #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) |
| 25 | #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) |
| 26 | |
K. Y. Srinivasan | 06d1d98 | 2017-01-19 11:51:56 -0700 | [diff] [blame] | 27 | #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) |
| 28 | #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) |
| 29 | |
K. Y. Srinivasan | 7297ff0 | 2017-01-19 11:51:57 -0700 | [diff] [blame] | 30 | #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) |
| 31 | |
Michael Kelley | 765e33f | 2019-05-30 00:14:00 +0000 | [diff] [blame] | 32 | #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0) |
| 33 | |
Michael Kelley | 619a4c8 | 2018-06-05 13:37:53 -0700 | [diff] [blame] | 34 | #define hv_get_synint_state(int_num, val) \ |
| 35 | rdmsrl(HV_X64_MSR_SINT0 + int_num, val) |
| 36 | #define hv_set_synint_state(int_num, val) \ |
| 37 | wrmsrl(HV_X64_MSR_SINT0 + int_num, val) |
K. Y. Srinivasan | 37e11d5 | 2017-01-19 11:51:58 -0700 | [diff] [blame] | 38 | |
Sunil Muthuswamy | 9d9c965 | 2018-07-28 21:58:47 +0000 | [diff] [blame] | 39 | #define hv_get_crash_ctl(val) \ |
| 40 | rdmsrl(HV_X64_MSR_CRASH_CTL, val) |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 41 | |
Michael Kelley | dd2cb34 | 2019-07-01 04:26:06 +0000 | [diff] [blame] | 42 | #define hv_get_time_ref_count(val) \ |
| 43 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val) |
| 44 | |
| 45 | #define hv_get_reference_tsc(val) \ |
| 46 | rdmsrl(HV_X64_MSR_REFERENCE_TSC, val) |
| 47 | #define hv_set_reference_tsc(val) \ |
| 48 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, val) |
| 49 | #define hv_set_clocksource_vdso(val) \ |
| 50 | ((val).archdata.vclock_mode = VCLOCK_HVCLOCK) |
| 51 | #define hv_get_raw_timer() rdtsc_ordered() |
| 52 | |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 53 | void hyperv_callback_vector(void); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 54 | void hyperv_reenlightenment_vector(void); |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 55 | #ifdef CONFIG_TRACING |
| 56 | #define trace_hyperv_callback_vector hyperv_callback_vector |
| 57 | #endif |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 58 | void hyperv_vector_handler(struct pt_regs *regs); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 59 | |
Michael Kelley | 248e742 | 2018-03-04 22:17:18 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Routines for stimer0 Direct Mode handling. |
| 62 | * On x86/x64, there are no percpu actions to take. |
| 63 | */ |
| 64 | void hv_stimer0_vector_handler(struct pt_regs *regs); |
| 65 | void hv_stimer0_callback_vector(void); |
Michael Kelley | 248e742 | 2018-03-04 22:17:18 -0700 | [diff] [blame] | 66 | |
| 67 | static inline void hv_enable_stimer0_percpu_irq(int irq) {} |
| 68 | static inline void hv_disable_stimer0_percpu_irq(int irq) {} |
| 69 | |
| 70 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 71 | #if IS_ENABLED(CONFIG_HYPERV) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 72 | extern void *hv_hypercall_pg; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 73 | extern void __percpu **hyperv_pcpu_input_arg; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 74 | |
| 75 | static inline u64 hv_do_hypercall(u64 control, void *input, void *output) |
| 76 | { |
| 77 | u64 input_address = input ? virt_to_phys(input) : 0; |
| 78 | u64 output_address = output ? virt_to_phys(output) : 0; |
| 79 | u64 hv_status; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 80 | |
| 81 | #ifdef CONFIG_X86_64 |
| 82 | if (!hv_hypercall_pg) |
| 83 | return U64_MAX; |
| 84 | |
| 85 | __asm__ __volatile__("mov %4, %%r8\n" |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 86 | CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 87 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 88 | "+c" (control), "+d" (input_address) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 89 | : "r" (output_address), |
| 90 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 91 | : "cc", "memory", "r8", "r9", "r10", "r11"); |
| 92 | #else |
| 93 | u32 input_address_hi = upper_32_bits(input_address); |
| 94 | u32 input_address_lo = lower_32_bits(input_address); |
| 95 | u32 output_address_hi = upper_32_bits(output_address); |
| 96 | u32 output_address_lo = lower_32_bits(output_address); |
| 97 | |
| 98 | if (!hv_hypercall_pg) |
| 99 | return U64_MAX; |
| 100 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 101 | __asm__ __volatile__(CALL_NOSPEC |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 102 | : "=A" (hv_status), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 103 | "+c" (input_address_lo), ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 104 | : "A" (control), |
| 105 | "b" (input_address_hi), |
| 106 | "D"(output_address_hi), "S"(output_address_lo), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 107 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 108 | : "cc", "memory"); |
| 109 | #endif /* !x86_64 */ |
| 110 | return hv_status; |
| 111 | } |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 112 | |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 113 | /* Fast hypercall with 8 bytes of input and no output */ |
| 114 | static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) |
| 115 | { |
| 116 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 117 | |
| 118 | #ifdef CONFIG_X86_64 |
| 119 | { |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 120 | __asm__ __volatile__(CALL_NOSPEC |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 121 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 122 | "+c" (control), "+d" (input1) |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 123 | : THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 124 | : "cc", "r8", "r9", "r10", "r11"); |
| 125 | } |
| 126 | #else |
| 127 | { |
| 128 | u32 input1_hi = upper_32_bits(input1); |
| 129 | u32 input1_lo = lower_32_bits(input1); |
| 130 | |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 131 | __asm__ __volatile__ (CALL_NOSPEC |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 132 | : "=A"(hv_status), |
| 133 | "+c"(input1_lo), |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 134 | ASM_CALL_CONSTRAINT |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 135 | : "A" (control), |
| 136 | "b" (input1_hi), |
David Woodhouse | e70e589 | 2018-01-11 21:46:30 +0000 | [diff] [blame] | 137 | THUNK_TARGET(hv_hypercall_pg) |
Vitaly Kuznetsov | 6a8edbd | 2017-08-02 18:09:15 +0200 | [diff] [blame] | 138 | : "cc", "edi", "esi"); |
| 139 | } |
| 140 | #endif |
| 141 | return hv_status; |
| 142 | } |
| 143 | |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 144 | /* Fast hypercall with 16 bytes of input */ |
| 145 | static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) |
| 146 | { |
| 147 | u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; |
| 148 | |
| 149 | #ifdef CONFIG_X86_64 |
| 150 | { |
| 151 | __asm__ __volatile__("mov %4, %%r8\n" |
| 152 | CALL_NOSPEC |
| 153 | : "=a" (hv_status), ASM_CALL_CONSTRAINT, |
| 154 | "+c" (control), "+d" (input1) |
| 155 | : "r" (input2), |
| 156 | THUNK_TARGET(hv_hypercall_pg) |
| 157 | : "cc", "r8", "r9", "r10", "r11"); |
| 158 | } |
| 159 | #else |
| 160 | { |
| 161 | u32 input1_hi = upper_32_bits(input1); |
| 162 | u32 input1_lo = lower_32_bits(input1); |
| 163 | u32 input2_hi = upper_32_bits(input2); |
| 164 | u32 input2_lo = lower_32_bits(input2); |
| 165 | |
| 166 | __asm__ __volatile__ (CALL_NOSPEC |
| 167 | : "=A"(hv_status), |
| 168 | "+c"(input1_lo), ASM_CALL_CONSTRAINT |
| 169 | : "A" (control), "b" (input1_hi), |
| 170 | "D"(input2_hi), "S"(input2_lo), |
| 171 | THUNK_TARGET(hv_hypercall_pg) |
| 172 | : "cc"); |
| 173 | } |
| 174 | #endif |
Yi Wang | b42967d | 2018-10-29 15:17:31 +0800 | [diff] [blame] | 175 | return hv_status; |
Vitaly Kuznetsov | 53e5296 | 2018-06-22 19:06:22 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Vitaly Kuznetsov | 806c892 | 2017-08-02 18:09:17 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Rep hypercalls. Callers of this functions are supposed to ensure that |
| 180 | * rep_count and varhead_size comply with Hyper-V hypercall definition. |
| 181 | */ |
| 182 | static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, |
| 183 | void *input, void *output) |
| 184 | { |
| 185 | u64 control = code; |
| 186 | u64 status; |
| 187 | u16 rep_comp; |
| 188 | |
| 189 | control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; |
| 190 | control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; |
| 191 | |
| 192 | do { |
| 193 | status = hv_do_hypercall(control, input, output); |
| 194 | if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) |
| 195 | return status; |
| 196 | |
| 197 | /* Bits 32-43 of status have 'Reps completed' data. */ |
| 198 | rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> |
| 199 | HV_HYPERCALL_REP_COMP_OFFSET; |
| 200 | |
| 201 | control &= ~HV_HYPERCALL_REP_START_MASK; |
| 202 | control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; |
| 203 | |
| 204 | touch_nmi_watchdog(); |
| 205 | } while (rep_comp < rep_count); |
| 206 | |
| 207 | return status; |
| 208 | } |
| 209 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 210 | extern struct hv_vp_assist_page **hv_vp_assist_page; |
| 211 | |
| 212 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 213 | { |
| 214 | if (!hv_vp_assist_page) |
| 215 | return NULL; |
| 216 | |
| 217 | return hv_vp_assist_page[cpu]; |
| 218 | } |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 219 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 220 | void __init hyperv_init(void); |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 221 | void hyperv_setup_mmu_ops(void); |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 222 | void *hv_alloc_hyperv_page(void); |
Himadri Pandya | fa36dcd | 2019-07-30 09:49:43 +0000 | [diff] [blame] | 223 | void *hv_alloc_hyperv_zeroed_page(void); |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 224 | void hv_free_hyperv_page(unsigned long addr); |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 225 | void hyperv_reenlightenment_intr(struct pt_regs *regs); |
| 226 | void set_hv_tscchange_cb(void (*cb)(void)); |
| 227 | void clear_hv_tscchange_cb(void); |
| 228 | void hyperv_stop_tsc_emulation(void); |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 229 | int hyperv_flush_guest_mapping(u64 as); |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 230 | int hyperv_flush_guest_mapping_range(u64 as, |
| 231 | hyperv_fill_flush_list_func fill_func, void *data); |
| 232 | int hyperv_fill_flush_guest_mapping_list( |
| 233 | struct hv_guest_mapping_flush_list *flush, |
| 234 | u64 start_gfn, u64 end_gfn); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 235 | |
| 236 | #ifdef CONFIG_X86_64 |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 237 | void hv_apic_init(void); |
Yi Sun | 3a025de | 2018-10-08 16:29:34 +0800 | [diff] [blame] | 238 | void __init hv_init_spinlocks(void); |
| 239 | bool hv_vcpu_is_preempted(int vcpu); |
Thomas Gleixner | 2d2ccf24 | 2018-05-19 21:22:48 +0200 | [diff] [blame] | 240 | #else |
| 241 | static inline void hv_apic_init(void) {} |
| 242 | #endif |
| 243 | |
Boqun Feng | 1cf106d | 2020-02-10 11:39:53 +0800 | [diff] [blame^] | 244 | static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, |
| 245 | struct msi_desc *msi_desc) |
| 246 | { |
| 247 | msi_entry->address = msi_desc->msg.address_lo; |
| 248 | msi_entry->data = msi_desc->msg.data; |
| 249 | } |
| 250 | |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 251 | #else /* CONFIG_HYPERV */ |
| 252 | static inline void hyperv_init(void) {} |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 253 | static inline void hyperv_setup_mmu_ops(void) {} |
Maya Nakamura | 8c3e44b | 2019-07-12 08:21:25 +0000 | [diff] [blame] | 254 | static inline void *hv_alloc_hyperv_page(void) { return NULL; } |
| 255 | static inline void hv_free_hyperv_page(unsigned long addr) {} |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 256 | static inline void set_hv_tscchange_cb(void (*cb)(void)) {} |
| 257 | static inline void clear_hv_tscchange_cb(void) {} |
| 258 | static inline void hyperv_stop_tsc_emulation(void) {}; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 259 | static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) |
| 260 | { |
| 261 | return NULL; |
| 262 | } |
Tianyu Lan | eb914cf | 2018-07-19 08:40:06 +0000 | [diff] [blame] | 263 | static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } |
Lan Tianyu | cc4edae4 | 2018-12-06 21:21:05 +0800 | [diff] [blame] | 264 | static inline int hyperv_flush_guest_mapping_range(u64 as, |
| 265 | hyperv_fill_flush_list_func fill_func, void *data) |
| 266 | { |
| 267 | return -1; |
| 268 | } |
Vitaly Kuznetsov | 79cadff | 2017-08-02 18:09:13 +0200 | [diff] [blame] | 269 | #endif /* CONFIG_HYPERV */ |
| 270 | |
Michael Kelley | 765e33f | 2019-05-30 00:14:00 +0000 | [diff] [blame] | 271 | |
| 272 | #include <asm-generic/mshyperv.h> |
| 273 | |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 274 | #endif |