1 // SPDX-License-Identifier: GPL-2.0-only
3 * X86 specific Hyper-V initialization code.
5 * Copyright (C) 2016, Microsoft, Inc.
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
10 #include <linux/efi.h>
11 #include <linux/types.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>
20 #include <linux/hyperv.h>
21 #include <linux/slab.h>
22 #include <linux/cpuhotplug.h>
23 #include <clocksource/hyperv_timer.h>
25 void *hv_hypercall_pg;
26 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
29 EXPORT_SYMBOL_GPL(hv_vp_index);
31 struct hv_vp_assist_page **hv_vp_assist_page;
32 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
34 void __percpu **hyperv_pcpu_input_arg;
35 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
38 EXPORT_SYMBOL_GPL(hv_max_vp_index);
40 static int hv_cpu_init(unsigned int cpu)
43 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
47 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
48 pg = alloc_page(GFP_KERNEL);
51 *input_arg = page_address(pg);
53 hv_get_vp_index(msr_vp_index);
55 hv_vp_index[smp_processor_id()] = msr_vp_index;
57 if (msr_vp_index > hv_max_vp_index)
58 hv_max_vp_index = msr_vp_index;
60 if (!hv_vp_assist_page)
64 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
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;
73 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
79 static void (*hv_reenlightenment_cb)(void);
81 static void hv_reenlightenment_notify(struct work_struct *dummy)
83 struct hv_tsc_emulation_status emu_status;
85 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
87 /* Don't issue the callback if TSC accesses are not emulated */
88 if (hv_reenlightenment_cb && emu_status.inprogress)
89 hv_reenlightenment_cb();
91 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
93 void hyperv_stop_tsc_emulation(void)
96 struct hv_tsc_emulation_status emu_status;
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);
102 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
103 tsc_khz = div64_u64(freq, 1000);
105 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
107 static inline bool hv_reenlightenment_available(void)
110 * Check for required features and priviliges to make TSC frequency
111 * change notifications work.
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;
118 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
122 inc_irq_stat(irq_hv_reenlightenment_count);
124 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
129 void set_hv_tscchange_cb(void (*cb)(void))
131 struct hv_reenlightenment_control re_ctrl = {
132 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
134 .target_vp = hv_vp_index[smp_processor_id()]
136 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
138 if (!hv_reenlightenment_available()) {
139 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
143 hv_reenlightenment_cb = cb;
145 /* Make sure callback is registered before we write to MSRs */
148 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
149 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
151 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
153 void clear_hv_tscchange_cb(void)
155 struct hv_reenlightenment_control re_ctrl;
157 if (!hv_reenlightenment_available())
160 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
162 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
164 hv_reenlightenment_cb = NULL;
166 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
168 static int hv_cpu_die(unsigned int cpu)
170 struct hv_reenlightenment_control re_ctrl;
171 unsigned int new_cpu;
174 void *input_pg = NULL;
176 local_irq_save(flags);
177 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
178 input_pg = *input_arg;
180 local_irq_restore(flags);
181 free_page((unsigned long)input_pg);
183 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
184 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
186 if (hv_reenlightenment_cb == NULL)
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);
194 re_ctrl.target_vp = hv_vp_index[new_cpu];
195 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
201 static int __init hv_pci_init(void)
203 int gen2vm = efi_enabled(EFI_BOOT);
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"
213 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
218 * This function is to be invoked early in the boot sequence after the
219 * hypervisor has been detected.
221 * 1. Setup the hypercall page.
222 * 2. Register Hyper-V specific clocksource.
223 * 3. Setup Hyper-V specific APIC entry points.
225 void __init hyperv_init(void)
227 u64 guest_id, required_msrs;
228 union hv_x64_msr_hypercall_contents hypercall_msr;
231 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
234 /* Absolutely required MSRs */
235 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
236 HV_X64_MSR_VP_INDEX_AVAILABLE;
238 if ((ms_hyperv.features & required_msrs) != required_msrs)
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
247 hyperv_pcpu_input_arg = alloc_percpu(void *);
249 BUG_ON(hyperv_pcpu_input_arg == NULL);
251 /* Allocate percpu VP index */
252 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
257 for (i = 0; i < num_possible_cpus(); i++)
258 hv_vp_index[i] = VP_INVAL;
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;
267 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
268 hv_cpu_init, hv_cpu_die);
270 goto free_vp_assist_page;
273 * Setup the hypercall page and enable hypercalls.
274 * 1. Register the guest ID
275 * 2. Enable the hypercall and register the hypercall page
277 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
278 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
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;
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);
293 x86_init.pci.arch_init = hv_pci_init;
295 /* Register Hyper-V specific clocksource */
296 hv_init_clocksource();
300 cpuhp_remove_state(cpuhp);
302 kfree(hv_vp_assist_page);
303 hv_vp_assist_page = NULL;
310 * This routine is called before kexec/kdump, it does the required cleanup.
312 void hyperv_cleanup(void)
314 union hv_x64_msr_hypercall_contents hypercall_msr;
316 /* Reset our OS id */
317 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
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
324 hv_hypercall_pg = NULL;
326 /* Reset the hypercall page */
327 hypercall_msr.as_uint64 = 0;
328 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
330 /* Reset the TSC page */
331 hypercall_msr.as_uint64 = 0;
332 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
334 EXPORT_SYMBOL_GPL(hyperv_cleanup);
336 void hyperv_report_panic(struct pt_regs *regs, long err)
338 static bool panic_reported;
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'.
348 panic_reported = true;
350 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
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);
359 * Let Hyper-V know there is crash data available
361 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
363 EXPORT_SYMBOL_GPL(hyperv_report_panic);
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
370 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
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.
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);
384 * Let Hyper-V know there is crash data available along with
387 wrmsrl(HV_X64_MSR_CRASH_CTL,
388 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
390 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
392 bool hv_is_hyperv_initialized(void)
394 union hv_x64_msr_hypercall_contents hypercall_msr;
397 * Ensure that we're really on Hyper-V, and not a KVM or Xen
398 * emulation of Hyper-V
400 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
404 * Verify that earlier initialization succeeded by checking
405 * that the hypercall page is setup
407 hypercall_msr.as_uint64 = 0;
408 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
410 return hypercall_msr.enable;
412 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);