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