xref: /openbmc/linux/arch/x86/hyperv/hv_init.c (revision 0d07cf5e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * X86 specific Hyper-V initialization code.
4  *
5  * Copyright (C) 2016, Microsoft, Inc.
6  *
7  * Author : K. Y. Srinivasan <kys@microsoft.com>
8  */
9 
10 #include <linux/efi.h>
11 #include <linux/types.h>
12 #include <asm/apic.h>
13 #include <asm/desc.h>
14 #include <asm/hypervisor.h>
15 #include <asm/hyperv-tlfs.h>
16 #include <asm/mshyperv.h>
17 #include <linux/version.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mm.h>
20 #include <linux/hyperv.h>
21 #include <linux/slab.h>
22 #include <linux/cpuhotplug.h>
23 #include <clocksource/hyperv_timer.h>
24 
25 void *hv_hypercall_pg;
26 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
27 
28 u32 *hv_vp_index;
29 EXPORT_SYMBOL_GPL(hv_vp_index);
30 
31 struct hv_vp_assist_page **hv_vp_assist_page;
32 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
33 
34 void  __percpu **hyperv_pcpu_input_arg;
35 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
36 
37 u32 hv_max_vp_index;
38 EXPORT_SYMBOL_GPL(hv_max_vp_index);
39 
40 static int hv_cpu_init(unsigned int cpu)
41 {
42 	u64 msr_vp_index;
43 	struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
44 	void **input_arg;
45 	struct page *pg;
46 
47 	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
48 	pg = alloc_page(GFP_KERNEL);
49 	if (unlikely(!pg))
50 		return -ENOMEM;
51 	*input_arg = page_address(pg);
52 
53 	hv_get_vp_index(msr_vp_index);
54 
55 	hv_vp_index[smp_processor_id()] = msr_vp_index;
56 
57 	if (msr_vp_index > hv_max_vp_index)
58 		hv_max_vp_index = msr_vp_index;
59 
60 	if (!hv_vp_assist_page)
61 		return 0;
62 
63 	if (!*hvp)
64 		*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
65 
66 	if (*hvp) {
67 		u64 val;
68 
69 		val = vmalloc_to_pfn(*hvp);
70 		val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
71 			HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
72 
73 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
74 	}
75 
76 	return 0;
77 }
78 
79 static void (*hv_reenlightenment_cb)(void);
80 
81 static void hv_reenlightenment_notify(struct work_struct *dummy)
82 {
83 	struct hv_tsc_emulation_status emu_status;
84 
85 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
86 
87 	/* Don't issue the callback if TSC accesses are not emulated */
88 	if (hv_reenlightenment_cb && emu_status.inprogress)
89 		hv_reenlightenment_cb();
90 }
91 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
92 
93 void hyperv_stop_tsc_emulation(void)
94 {
95 	u64 freq;
96 	struct hv_tsc_emulation_status emu_status;
97 
98 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
99 	emu_status.inprogress = 0;
100 	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
101 
102 	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
103 	tsc_khz = div64_u64(freq, 1000);
104 }
105 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
106 
107 static inline bool hv_reenlightenment_available(void)
108 {
109 	/*
110 	 * Check for required features and priviliges to make TSC frequency
111 	 * change notifications work.
112 	 */
113 	return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
114 		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
115 		ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
116 }
117 
118 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
119 {
120 	entering_ack_irq();
121 
122 	inc_irq_stat(irq_hv_reenlightenment_count);
123 
124 	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
125 
126 	exiting_irq();
127 }
128 
129 void set_hv_tscchange_cb(void (*cb)(void))
130 {
131 	struct hv_reenlightenment_control re_ctrl = {
132 		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
133 		.enabled = 1,
134 		.target_vp = hv_vp_index[smp_processor_id()]
135 	};
136 	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
137 
138 	if (!hv_reenlightenment_available()) {
139 		pr_warn("Hyper-V: reenlightenment support is unavailable\n");
140 		return;
141 	}
142 
143 	hv_reenlightenment_cb = cb;
144 
145 	/* Make sure callback is registered before we write to MSRs */
146 	wmb();
147 
148 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
149 	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
150 }
151 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
152 
153 void clear_hv_tscchange_cb(void)
154 {
155 	struct hv_reenlightenment_control re_ctrl;
156 
157 	if (!hv_reenlightenment_available())
158 		return;
159 
160 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
161 	re_ctrl.enabled = 0;
162 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
163 
164 	hv_reenlightenment_cb = NULL;
165 }
166 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
167 
168 static int hv_cpu_die(unsigned int cpu)
169 {
170 	struct hv_reenlightenment_control re_ctrl;
171 	unsigned int new_cpu;
172 	unsigned long flags;
173 	void **input_arg;
174 	void *input_pg = NULL;
175 
176 	local_irq_save(flags);
177 	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
178 	input_pg = *input_arg;
179 	*input_arg = NULL;
180 	local_irq_restore(flags);
181 	free_page((unsigned long)input_pg);
182 
183 	if (hv_vp_assist_page && hv_vp_assist_page[cpu])
184 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
185 
186 	if (hv_reenlightenment_cb == NULL)
187 		return 0;
188 
189 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
190 	if (re_ctrl.target_vp == hv_vp_index[cpu]) {
191 		/* Reassign to some other online CPU */
192 		new_cpu = cpumask_any_but(cpu_online_mask, cpu);
193 
194 		re_ctrl.target_vp = hv_vp_index[new_cpu];
195 		wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
196 	}
197 
198 	return 0;
199 }
200 
201 static int __init hv_pci_init(void)
202 {
203 	int gen2vm = efi_enabled(EFI_BOOT);
204 
205 	/*
206 	 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
207 	 * The purpose is to suppress the harmless warning:
208 	 * "PCI: Fatal: No config space access function found"
209 	 */
210 	if (gen2vm)
211 		return 0;
212 
213 	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
214 	return 1;
215 }
216 
217 /*
218  * This function is to be invoked early in the boot sequence after the
219  * hypervisor has been detected.
220  *
221  * 1. Setup the hypercall page.
222  * 2. Register Hyper-V specific clocksource.
223  * 3. Setup Hyper-V specific APIC entry points.
224  */
225 void __init hyperv_init(void)
226 {
227 	u64 guest_id, required_msrs;
228 	union hv_x64_msr_hypercall_contents hypercall_msr;
229 	int cpuhp, i;
230 
231 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
232 		return;
233 
234 	/* Absolutely required MSRs */
235 	required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
236 		HV_X64_MSR_VP_INDEX_AVAILABLE;
237 
238 	if ((ms_hyperv.features & required_msrs) != required_msrs)
239 		return;
240 
241 	/*
242 	 * Allocate the per-CPU state for the hypercall input arg.
243 	 * If this allocation fails, we will not be able to setup
244 	 * (per-CPU) hypercall input page and thus this failure is
245 	 * fatal on Hyper-V.
246 	 */
247 	hyperv_pcpu_input_arg = alloc_percpu(void  *);
248 
249 	BUG_ON(hyperv_pcpu_input_arg == NULL);
250 
251 	/* Allocate percpu VP index */
252 	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
253 				    GFP_KERNEL);
254 	if (!hv_vp_index)
255 		return;
256 
257 	for (i = 0; i < num_possible_cpus(); i++)
258 		hv_vp_index[i] = VP_INVAL;
259 
260 	hv_vp_assist_page = kcalloc(num_possible_cpus(),
261 				    sizeof(*hv_vp_assist_page), GFP_KERNEL);
262 	if (!hv_vp_assist_page) {
263 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
264 		goto free_vp_index;
265 	}
266 
267 	cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
268 				  hv_cpu_init, hv_cpu_die);
269 	if (cpuhp < 0)
270 		goto free_vp_assist_page;
271 
272 	/*
273 	 * Setup the hypercall page and enable hypercalls.
274 	 * 1. Register the guest ID
275 	 * 2. Enable the hypercall and register the hypercall page
276 	 */
277 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
278 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
279 
280 	hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
281 	if (hv_hypercall_pg == NULL) {
282 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
283 		goto remove_cpuhp_state;
284 	}
285 
286 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
287 	hypercall_msr.enable = 1;
288 	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
289 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
290 
291 	hv_apic_init();
292 
293 	x86_init.pci.arch_init = hv_pci_init;
294 
295 	/* Register Hyper-V specific clocksource */
296 	hv_init_clocksource();
297 	return;
298 
299 remove_cpuhp_state:
300 	cpuhp_remove_state(cpuhp);
301 free_vp_assist_page:
302 	kfree(hv_vp_assist_page);
303 	hv_vp_assist_page = NULL;
304 free_vp_index:
305 	kfree(hv_vp_index);
306 	hv_vp_index = NULL;
307 }
308 
309 /*
310  * This routine is called before kexec/kdump, it does the required cleanup.
311  */
312 void hyperv_cleanup(void)
313 {
314 	union hv_x64_msr_hypercall_contents hypercall_msr;
315 
316 	/* Reset our OS id */
317 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
318 
319 	/*
320 	 * Reset hypercall page reference before reset the page,
321 	 * let hypercall operations fail safely rather than
322 	 * panic the kernel for using invalid hypercall page
323 	 */
324 	hv_hypercall_pg = NULL;
325 
326 	/* Reset the hypercall page */
327 	hypercall_msr.as_uint64 = 0;
328 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
329 
330 	/* Reset the TSC page */
331 	hypercall_msr.as_uint64 = 0;
332 	wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
333 }
334 EXPORT_SYMBOL_GPL(hyperv_cleanup);
335 
336 void hyperv_report_panic(struct pt_regs *regs, long err)
337 {
338 	static bool panic_reported;
339 	u64 guest_id;
340 
341 	/*
342 	 * We prefer to report panic on 'die' chain as we have proper
343 	 * registers to report, but if we miss it (e.g. on BUG()) we need
344 	 * to report it on 'panic'.
345 	 */
346 	if (panic_reported)
347 		return;
348 	panic_reported = true;
349 
350 	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
351 
352 	wrmsrl(HV_X64_MSR_CRASH_P0, err);
353 	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
354 	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
355 	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
356 	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
357 
358 	/*
359 	 * Let Hyper-V know there is crash data available
360 	 */
361 	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
362 }
363 EXPORT_SYMBOL_GPL(hyperv_report_panic);
364 
365 /**
366  * hyperv_report_panic_msg - report panic message to Hyper-V
367  * @pa: physical address of the panic page containing the message
368  * @size: size of the message in the page
369  */
370 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
371 {
372 	/*
373 	 * P3 to contain the physical address of the panic page & P4 to
374 	 * contain the size of the panic data in that page. Rest of the
375 	 * registers are no-op when the NOTIFY_MSG flag is set.
376 	 */
377 	wrmsrl(HV_X64_MSR_CRASH_P0, 0);
378 	wrmsrl(HV_X64_MSR_CRASH_P1, 0);
379 	wrmsrl(HV_X64_MSR_CRASH_P2, 0);
380 	wrmsrl(HV_X64_MSR_CRASH_P3, pa);
381 	wrmsrl(HV_X64_MSR_CRASH_P4, size);
382 
383 	/*
384 	 * Let Hyper-V know there is crash data available along with
385 	 * the panic message.
386 	 */
387 	wrmsrl(HV_X64_MSR_CRASH_CTL,
388 	       (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
389 }
390 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
391 
392 bool hv_is_hyperv_initialized(void)
393 {
394 	union hv_x64_msr_hypercall_contents hypercall_msr;
395 
396 	/*
397 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
398 	 * emulation of Hyper-V
399 	 */
400 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
401 		return false;
402 
403 	/*
404 	 * Verify that earlier initialization succeeded by checking
405 	 * that the hypercall page is setup
406 	 */
407 	hypercall_msr.as_uint64 = 0;
408 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
409 
410 	return hypercall_msr.enable;
411 }
412 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
413