1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_MSHYPER_H
3 #define _ASM_X86_MSHYPER_H
4 
5 #include <linux/types.h>
6 #include <linux/nmi.h>
7 #include <linux/msi.h>
8 #include <asm/io.h>
9 #include <asm/hyperv-tlfs.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/paravirt.h>
12 #include <asm/mshyperv.h>
13 
14 typedef int (*hyperv_fill_flush_list_func)(
15 		struct hv_guest_mapping_flush_list *flush,
16 		void *data);
17 
hv_set_register(unsigned int reg,u64 value)18 static inline void hv_set_register(unsigned int reg, u64 value)
19 {
20 	wrmsrl(reg, value);
21 }
22 
hv_get_register(unsigned int reg)23 static inline u64 hv_get_register(unsigned int reg)
24 {
25 	u64 value;
26 
27 	rdmsrl(reg, value);
28 	return value;
29 }
30 
31 #define hv_get_raw_timer() rdtsc_ordered()
32 
33 void hyperv_vector_handler(struct pt_regs *regs);
34 
35 #if IS_ENABLED(CONFIG_HYPERV)
36 extern int hyperv_init_cpuhp;
37 
38 extern void *hv_hypercall_pg;
39 extern void  __percpu  **hyperv_pcpu_input_arg;
40 extern void  __percpu  **hyperv_pcpu_output_arg;
41 
42 extern u64 hv_current_partition_id;
43 
44 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
45 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
46 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
47 
hv_do_hypercall(u64 control,void * input,void * output)48 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
49 {
50 	u64 input_address = input ? virt_to_phys(input) : 0;
51 	u64 output_address = output ? virt_to_phys(output) : 0;
52 	u64 hv_status;
53 
54 #ifdef CONFIG_X86_64
55 	if (!hv_hypercall_pg)
56 		return U64_MAX;
57 
58 	__asm__ __volatile__("mov %4, %%r8\n"
59 			     CALL_NOSPEC
60 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
61 			       "+c" (control), "+d" (input_address)
62 			     :  "r" (output_address),
63 				THUNK_TARGET(hv_hypercall_pg)
64 			     : "cc", "memory", "r8", "r9", "r10", "r11");
65 #else
66 	u32 input_address_hi = upper_32_bits(input_address);
67 	u32 input_address_lo = lower_32_bits(input_address);
68 	u32 output_address_hi = upper_32_bits(output_address);
69 	u32 output_address_lo = lower_32_bits(output_address);
70 
71 	if (!hv_hypercall_pg)
72 		return U64_MAX;
73 
74 	__asm__ __volatile__(CALL_NOSPEC
75 			     : "=A" (hv_status),
76 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
77 			     : "A" (control),
78 			       "b" (input_address_hi),
79 			       "D"(output_address_hi), "S"(output_address_lo),
80 			       THUNK_TARGET(hv_hypercall_pg)
81 			     : "cc", "memory");
82 #endif /* !x86_64 */
83 	return hv_status;
84 }
85 
86 /* Fast hypercall with 8 bytes of input and no output */
hv_do_fast_hypercall8(u16 code,u64 input1)87 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
88 {
89 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
90 
91 #ifdef CONFIG_X86_64
92 	{
93 		__asm__ __volatile__(CALL_NOSPEC
94 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
95 				       "+c" (control), "+d" (input1)
96 				     : THUNK_TARGET(hv_hypercall_pg)
97 				     : "cc", "r8", "r9", "r10", "r11");
98 	}
99 #else
100 	{
101 		u32 input1_hi = upper_32_bits(input1);
102 		u32 input1_lo = lower_32_bits(input1);
103 
104 		__asm__ __volatile__ (CALL_NOSPEC
105 				      : "=A"(hv_status),
106 					"+c"(input1_lo),
107 					ASM_CALL_CONSTRAINT
108 				      :	"A" (control),
109 					"b" (input1_hi),
110 					THUNK_TARGET(hv_hypercall_pg)
111 				      : "cc", "edi", "esi");
112 	}
113 #endif
114 		return hv_status;
115 }
116 
117 /* Fast hypercall with 16 bytes of input */
hv_do_fast_hypercall16(u16 code,u64 input1,u64 input2)118 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
119 {
120 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
121 
122 #ifdef CONFIG_X86_64
123 	{
124 		__asm__ __volatile__("mov %4, %%r8\n"
125 				     CALL_NOSPEC
126 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
127 				       "+c" (control), "+d" (input1)
128 				     : "r" (input2),
129 				       THUNK_TARGET(hv_hypercall_pg)
130 				     : "cc", "r8", "r9", "r10", "r11");
131 	}
132 #else
133 	{
134 		u32 input1_hi = upper_32_bits(input1);
135 		u32 input1_lo = lower_32_bits(input1);
136 		u32 input2_hi = upper_32_bits(input2);
137 		u32 input2_lo = lower_32_bits(input2);
138 
139 		__asm__ __volatile__ (CALL_NOSPEC
140 				      : "=A"(hv_status),
141 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
142 				      :	"A" (control), "b" (input1_hi),
143 					"D"(input2_hi), "S"(input2_lo),
144 					THUNK_TARGET(hv_hypercall_pg)
145 				      : "cc");
146 	}
147 #endif
148 	return hv_status;
149 }
150 
151 extern struct hv_vp_assist_page **hv_vp_assist_page;
152 
hv_get_vp_assist_page(unsigned int cpu)153 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
154 {
155 	if (!hv_vp_assist_page)
156 		return NULL;
157 
158 	return hv_vp_assist_page[cpu];
159 }
160 
161 void __init hyperv_init(void);
162 void hyperv_setup_mmu_ops(void);
163 void set_hv_tscchange_cb(void (*cb)(void));
164 void clear_hv_tscchange_cb(void);
165 void hyperv_stop_tsc_emulation(void);
166 int hyperv_flush_guest_mapping(u64 as);
167 int hyperv_flush_guest_mapping_range(u64 as,
168 		hyperv_fill_flush_list_func fill_func, void *data);
169 int hyperv_fill_flush_guest_mapping_list(
170 		struct hv_guest_mapping_flush_list *flush,
171 		u64 start_gfn, u64 end_gfn);
172 
173 extern bool hv_root_partition;
174 
175 #ifdef CONFIG_X86_64
176 void hv_apic_init(void);
177 void __init hv_init_spinlocks(void);
178 bool hv_vcpu_is_preempted(int vcpu);
179 #else
hv_apic_init(void)180 static inline void hv_apic_init(void) {}
181 #endif
182 
hv_set_msi_entry_from_desc(union hv_msi_entry * msi_entry,struct msi_desc * msi_desc)183 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
184 					      struct msi_desc *msi_desc)
185 {
186 	msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
187 	msi_entry->data.as_uint32 = msi_desc->msg.data;
188 }
189 
190 struct irq_domain *hv_create_pci_msi_domain(void);
191 
192 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
193 		struct hv_interrupt_entry *entry);
194 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
195 
196 #else /* CONFIG_HYPERV */
hyperv_init(void)197 static inline void hyperv_init(void) {}
hyperv_setup_mmu_ops(void)198 static inline void hyperv_setup_mmu_ops(void) {}
set_hv_tscchange_cb(void (* cb)(void))199 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
clear_hv_tscchange_cb(void)200 static inline void clear_hv_tscchange_cb(void) {}
hyperv_stop_tsc_emulation(void)201 static inline void hyperv_stop_tsc_emulation(void) {};
hv_get_vp_assist_page(unsigned int cpu)202 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
203 {
204 	return NULL;
205 }
hyperv_flush_guest_mapping(u64 as)206 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
hyperv_flush_guest_mapping_range(u64 as,hyperv_fill_flush_list_func fill_func,void * data)207 static inline int hyperv_flush_guest_mapping_range(u64 as,
208 		hyperv_fill_flush_list_func fill_func, void *data)
209 {
210 	return -1;
211 }
212 #endif /* CONFIG_HYPERV */
213 
214 
215 #include <asm-generic/mshyperv.h>
216 
217 #endif
218