blob: da3972fe5a7aec787d41a1343e3a8467a2316b19 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvine08cae42010-05-07 16:57:28 -07002#ifndef _ASM_X86_MSHYPER_H
3#define _ASM_X86_MSHYPER_H
Ky Srinivasana2a47c62010-05-06 12:08:41 -07004
H. Peter Anvine08cae42010-05-07 16:57:28 -07005#include <linux/types.h>
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +02006#include <linux/nmi.h>
Boqun Feng1cf106d2020-02-10 11:39:53 +08007#include <linux/msi.h>
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +02008#include <asm/io.h>
Vitaly Kuznetsov5a485802018-03-20 15:02:05 +01009#include <asm/hyperv-tlfs.h>
David Woodhousee70e5892018-01-11 21:46:30 +000010#include <asm/nospec-branch.h>
Michael Kelleyb9d8cf22020-08-09 18:29:51 -070011#include <asm/paravirt.h>
Joseph Salisbury753ed9c2021-04-16 17:43:03 -070012#include <asm/mshyperv.h>
H. Peter Anvine08cae42010-05-07 16:57:28 -070013
Tianyu Lanfaff4402021-10-25 08:21:11 -040014union hv_ghcb;
15
Tianyu Lan0cc4f6d2021-10-25 08:21:06 -040016DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
17
Lan Tianyucc4edae42018-12-06 21:21:05 +080018typedef int (*hyperv_fill_flush_list_func)(
19 struct hv_guest_mapping_flush_list *flush,
20 void *data);
21
Michael Kelleydd2cb342019-07-01 04:26:06 +000022#define hv_get_raw_timer() rdtsc_ordered()
23
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080024void hyperv_vector_handler(struct pt_regs *regs);
K. Y. Srinivasan87300462017-01-18 16:45:02 -070025
26#if IS_ENABLED(CONFIG_HYPERV)
Dexuan Cuidfe94d42020-12-21 22:55:41 -080027extern int hyperv_init_cpuhp;
28
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020029extern void *hv_hypercall_pg;
30
Wei Liu99a0f462021-02-03 15:04:25 +000031extern u64 hv_current_partition_id;
32
Tianyu Lanfaff4402021-10-25 08:21:11 -040033extern union hv_ghcb __percpu **hv_ghcb_pg;
Tianyu Lan0cc4f6d2021-10-25 08:21:06 -040034
Wei Liu86b5ec32021-02-03 15:04:28 +000035int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
36int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
37int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
38
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020039static 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 Kuznetsovfc536622017-08-02 18:09:14 +020044
45#ifdef CONFIG_X86_64
46 if (!hv_hypercall_pg)
47 return U64_MAX;
48
49 __asm__ __volatile__("mov %4, %%r8\n"
David Woodhousee70e5892018-01-11 21:46:30 +000050 CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -050051 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020052 "+c" (control), "+d" (input_address)
David Woodhousee70e5892018-01-11 21:46:30 +000053 : "r" (output_address),
54 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020055 : "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 Woodhousee70e5892018-01-11 21:46:30 +000065 __asm__ __volatile__(CALL_NOSPEC
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020066 : "=A" (hv_status),
Josh Poimboeuff5caf622017-09-20 16:24:33 -050067 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020068 : "A" (control),
69 "b" (input_address_hi),
70 "D"(output_address_hi), "S"(output_address_lo),
David Woodhousee70e5892018-01-11 21:46:30 +000071 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020072 : "cc", "memory");
73#endif /* !x86_64 */
74 return hv_status;
75}
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070076
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020077/* Fast hypercall with 8 bytes of input and no output */
78static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
79{
80 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020081
82#ifdef CONFIG_X86_64
83 {
David Woodhousee70e5892018-01-11 21:46:30 +000084 __asm__ __volatile__(CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -050085 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020086 "+c" (control), "+d" (input1)
David Woodhousee70e5892018-01-11 21:46:30 +000087 : THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020088 : "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 Woodhousee70e5892018-01-11 21:46:30 +000095 __asm__ __volatile__ (CALL_NOSPEC
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020096 : "=A"(hv_status),
97 "+c"(input1_lo),
Josh Poimboeuff5caf622017-09-20 16:24:33 -050098 ASM_CALL_CONSTRAINT
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +020099 : "A" (control),
100 "b" (input1_hi),
David Woodhousee70e5892018-01-11 21:46:30 +0000101 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200102 : "cc", "edi", "esi");
103 }
104#endif
105 return hv_status;
106}
107
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200108/* Fast hypercall with 16 bytes of input */
109static 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 Wangb42967d2018-10-29 15:17:31 +0800139 return hv_status;
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200140}
141
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100142extern struct hv_vp_assist_page **hv_vp_assist_page;
143
144static 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 Kuznetsov7415aea2017-08-02 18:09:18 +0200151
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700152void __init hyperv_init(void);
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200153void hyperv_setup_mmu_ops(void);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100154void set_hv_tscchange_cb(void (*cb)(void));
155void clear_hv_tscchange_cb(void);
156void hyperv_stop_tsc_emulation(void);
Tianyu Laneb914cf2018-07-19 08:40:06 +0000157int hyperv_flush_guest_mapping(u64 as);
Lan Tianyucc4edae42018-12-06 21:21:05 +0800158int hyperv_flush_guest_mapping_range(u64 as,
159 hyperv_fill_flush_list_func fill_func, void *data);
160int hyperv_fill_flush_guest_mapping_list(
161 struct hv_guest_mapping_flush_list *flush,
162 u64 start_gfn, u64 end_gfn);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200163
164#ifdef CONFIG_X86_64
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700165void hv_apic_init(void);
Yi Sun3a025de2018-10-08 16:29:34 +0800166void __init hv_init_spinlocks(void);
167bool hv_vcpu_is_preempted(int vcpu);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200168#else
169static inline void hv_apic_init(void) {}
170#endif
171
Boqun Feng1cf106d2020-02-10 11:39:53 +0800172static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
173 struct msi_desc *msi_desc)
174{
Wei Liud589ae62021-02-03 15:04:30 +0000175 msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
176 msi_entry->data.as_uint32 = msi_desc->msg.data;
Boqun Feng1cf106d2020-02-10 11:39:53 +0800177}
178
Wei Liue39397d2021-02-03 15:04:34 +0000179struct irq_domain *hv_create_pci_msi_domain(void);
180
Wei Liufb5ef352021-02-03 15:04:35 +0000181int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
182 struct hv_interrupt_entry *entry);
183int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
Tianyu Lan810a5212021-10-25 08:21:08 -0400184int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible);
Tianyu Lanfaff4402021-10-25 08:21:11 -0400185
186#ifdef CONFIG_AMD_MEM_ENCRYPT
187void hv_ghcb_msr_write(u64 msr, u64 value);
188void hv_ghcb_msr_read(u64 msr, u64 *value);
189#else
190static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
191static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
192#endif
193
194extern bool hv_isolation_type_snp(void);
195
196static 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
204static 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
215static 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 Kuznetsov79cadff2017-08-02 18:09:13 +0200229#else /* CONFIG_HYPERV */
230static inline void hyperv_init(void) {}
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200231static inline void hyperv_setup_mmu_ops(void) {}
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100232static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
233static inline void clear_hv_tscchange_cb(void) {}
234static inline void hyperv_stop_tsc_emulation(void) {};
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100235static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
236{
237 return NULL;
238}
Tianyu Laneb914cf2018-07-19 08:40:06 +0000239static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
Lan Tianyucc4edae42018-12-06 21:21:05 +0800240static 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 Lanfaff4402021-10-25 08:21:11 -0400245static inline void hv_set_register(unsigned int reg, u64 value) { }
246static inline u64 hv_get_register(unsigned int reg) { return 0; }
Tianyu Lan810a5212021-10-25 08:21:08 -0400247static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages,
248 bool visible)
249{
250 return -1;
251}
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200252#endif /* CONFIG_HYPERV */
253
Michael Kelley765e33f2019-05-30 00:14:00 +0000254
255#include <asm-generic/mshyperv.h>
256
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700257#endif