xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision f3c5e63c)
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 
13 typedef int (*hyperv_fill_flush_list_func)(
14 		struct hv_guest_mapping_flush_list *flush,
15 		void *data);
16 
17 static inline void hv_set_register(unsigned int reg, u64 value)
18 {
19 	wrmsrl(reg, value);
20 }
21 
22 static inline u64 hv_get_register(unsigned int reg)
23 {
24 	u64 value;
25 
26 	rdmsrl(reg, value);
27 	return value;
28 }
29 
30 #define hv_recommend_using_aeoi() \
31 	(!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED))
32 
33 #define hv_set_clocksource_vdso(val) \
34 	((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK)
35 #define hv_enable_vdso_clocksource() \
36 	vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
37 #define hv_get_raw_timer() rdtsc_ordered()
38 #define hv_get_vector() HYPERVISOR_CALLBACK_VECTOR
39 
40 /*
41  * Reference to pv_ops must be inline so objtool
42  * detection of noinstr violations can work correctly.
43  */
44 static __always_inline void hv_setup_sched_clock(void *sched_clock)
45 {
46 #ifdef CONFIG_PARAVIRT
47 	pv_ops.time.sched_clock = sched_clock;
48 #endif
49 }
50 
51 void hyperv_vector_handler(struct pt_regs *regs);
52 
53 static inline void hv_enable_stimer0_percpu_irq(int irq) {}
54 static inline void hv_disable_stimer0_percpu_irq(int irq) {}
55 
56 
57 #if IS_ENABLED(CONFIG_HYPERV)
58 extern int hyperv_init_cpuhp;
59 
60 extern void *hv_hypercall_pg;
61 extern void  __percpu  **hyperv_pcpu_input_arg;
62 extern void  __percpu  **hyperv_pcpu_output_arg;
63 
64 extern u64 hv_current_partition_id;
65 
66 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
67 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
68 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
69 
70 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
71 {
72 	u64 input_address = input ? virt_to_phys(input) : 0;
73 	u64 output_address = output ? virt_to_phys(output) : 0;
74 	u64 hv_status;
75 
76 #ifdef CONFIG_X86_64
77 	if (!hv_hypercall_pg)
78 		return U64_MAX;
79 
80 	__asm__ __volatile__("mov %4, %%r8\n"
81 			     CALL_NOSPEC
82 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
83 			       "+c" (control), "+d" (input_address)
84 			     :  "r" (output_address),
85 				THUNK_TARGET(hv_hypercall_pg)
86 			     : "cc", "memory", "r8", "r9", "r10", "r11");
87 #else
88 	u32 input_address_hi = upper_32_bits(input_address);
89 	u32 input_address_lo = lower_32_bits(input_address);
90 	u32 output_address_hi = upper_32_bits(output_address);
91 	u32 output_address_lo = lower_32_bits(output_address);
92 
93 	if (!hv_hypercall_pg)
94 		return U64_MAX;
95 
96 	__asm__ __volatile__(CALL_NOSPEC
97 			     : "=A" (hv_status),
98 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
99 			     : "A" (control),
100 			       "b" (input_address_hi),
101 			       "D"(output_address_hi), "S"(output_address_lo),
102 			       THUNK_TARGET(hv_hypercall_pg)
103 			     : "cc", "memory");
104 #endif /* !x86_64 */
105 	return hv_status;
106 }
107 
108 /* Fast hypercall with 8 bytes of input and no output */
109 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
110 {
111 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
112 
113 #ifdef CONFIG_X86_64
114 	{
115 		__asm__ __volatile__(CALL_NOSPEC
116 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
117 				       "+c" (control), "+d" (input1)
118 				     : THUNK_TARGET(hv_hypercall_pg)
119 				     : "cc", "r8", "r9", "r10", "r11");
120 	}
121 #else
122 	{
123 		u32 input1_hi = upper_32_bits(input1);
124 		u32 input1_lo = lower_32_bits(input1);
125 
126 		__asm__ __volatile__ (CALL_NOSPEC
127 				      : "=A"(hv_status),
128 					"+c"(input1_lo),
129 					ASM_CALL_CONSTRAINT
130 				      :	"A" (control),
131 					"b" (input1_hi),
132 					THUNK_TARGET(hv_hypercall_pg)
133 				      : "cc", "edi", "esi");
134 	}
135 #endif
136 		return hv_status;
137 }
138 
139 /* Fast hypercall with 16 bytes of input */
140 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
141 {
142 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
143 
144 #ifdef CONFIG_X86_64
145 	{
146 		__asm__ __volatile__("mov %4, %%r8\n"
147 				     CALL_NOSPEC
148 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
149 				       "+c" (control), "+d" (input1)
150 				     : "r" (input2),
151 				       THUNK_TARGET(hv_hypercall_pg)
152 				     : "cc", "r8", "r9", "r10", "r11");
153 	}
154 #else
155 	{
156 		u32 input1_hi = upper_32_bits(input1);
157 		u32 input1_lo = lower_32_bits(input1);
158 		u32 input2_hi = upper_32_bits(input2);
159 		u32 input2_lo = lower_32_bits(input2);
160 
161 		__asm__ __volatile__ (CALL_NOSPEC
162 				      : "=A"(hv_status),
163 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
164 				      :	"A" (control), "b" (input1_hi),
165 					"D"(input2_hi), "S"(input2_lo),
166 					THUNK_TARGET(hv_hypercall_pg)
167 				      : "cc");
168 	}
169 #endif
170 	return hv_status;
171 }
172 
173 /*
174  * Rep hypercalls. Callers of this functions are supposed to ensure that
175  * rep_count and varhead_size comply with Hyper-V hypercall definition.
176  */
177 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
178 				      void *input, void *output)
179 {
180 	u64 control = code;
181 	u64 status;
182 	u16 rep_comp;
183 
184 	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
185 	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
186 
187 	do {
188 		status = hv_do_hypercall(control, input, output);
189 		if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
190 			return status;
191 
192 		/* Bits 32-43 of status have 'Reps completed' data. */
193 		rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
194 			HV_HYPERCALL_REP_COMP_OFFSET;
195 
196 		control &= ~HV_HYPERCALL_REP_START_MASK;
197 		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
198 
199 		touch_nmi_watchdog();
200 	} while (rep_comp < rep_count);
201 
202 	return status;
203 }
204 
205 extern struct hv_vp_assist_page **hv_vp_assist_page;
206 
207 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
208 {
209 	if (!hv_vp_assist_page)
210 		return NULL;
211 
212 	return hv_vp_assist_page[cpu];
213 }
214 
215 void __init hyperv_init(void);
216 void hyperv_setup_mmu_ops(void);
217 void set_hv_tscchange_cb(void (*cb)(void));
218 void clear_hv_tscchange_cb(void);
219 void hyperv_stop_tsc_emulation(void);
220 int hyperv_flush_guest_mapping(u64 as);
221 int hyperv_flush_guest_mapping_range(u64 as,
222 		hyperv_fill_flush_list_func fill_func, void *data);
223 int hyperv_fill_flush_guest_mapping_list(
224 		struct hv_guest_mapping_flush_list *flush,
225 		u64 start_gfn, u64 end_gfn);
226 
227 extern bool hv_root_partition;
228 
229 #ifdef CONFIG_X86_64
230 void hv_apic_init(void);
231 void __init hv_init_spinlocks(void);
232 bool hv_vcpu_is_preempted(int vcpu);
233 #else
234 static inline void hv_apic_init(void) {}
235 #endif
236 
237 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
238 					      struct msi_desc *msi_desc)
239 {
240 	msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
241 	msi_entry->data.as_uint32 = msi_desc->msg.data;
242 }
243 
244 struct irq_domain *hv_create_pci_msi_domain(void);
245 
246 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
247 		struct hv_interrupt_entry *entry);
248 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
249 
250 #else /* CONFIG_HYPERV */
251 static inline void hyperv_init(void) {}
252 static inline void hyperv_setup_mmu_ops(void) {}
253 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
254 static inline void clear_hv_tscchange_cb(void) {}
255 static inline void hyperv_stop_tsc_emulation(void) {};
256 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
257 {
258 	return NULL;
259 }
260 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
261 static inline int hyperv_flush_guest_mapping_range(u64 as,
262 		hyperv_fill_flush_list_func fill_func, void *data)
263 {
264 	return -1;
265 }
266 #endif /* CONFIG_HYPERV */
267 
268 
269 #include <asm-generic/mshyperv.h>
270 
271 #endif
272