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/acpi.h>
11 #include <linux/efi.h>
12 #include <linux/types.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>
21 #include <linux/hyperv.h>
22 #include <linux/slab.h>
23 #include <linux/cpuhotplug.h>
24 #include <clocksource/hyperv_timer.h>
26 void *hv_hypercall_pg
;
27 EXPORT_SYMBOL_GPL(hv_hypercall_pg
);
30 EXPORT_SYMBOL_GPL(hv_vp_index
);
32 struct hv_vp_assist_page
**hv_vp_assist_page
;
33 EXPORT_SYMBOL_GPL(hv_vp_assist_page
);
35 void __percpu
**hyperv_pcpu_input_arg
;
36 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg
);
39 EXPORT_SYMBOL_GPL(hv_max_vp_index
);
41 void *hv_alloc_hyperv_page(void)
43 BUILD_BUG_ON(PAGE_SIZE
!= HV_HYP_PAGE_SIZE
);
45 return (void *)__get_free_page(GFP_KERNEL
);
47 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page
);
49 void *hv_alloc_hyperv_zeroed_page(void)
51 BUILD_BUG_ON(PAGE_SIZE
!= HV_HYP_PAGE_SIZE
);
53 return (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
55 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page
);
57 void hv_free_hyperv_page(unsigned long addr
)
61 EXPORT_SYMBOL_GPL(hv_free_hyperv_page
);
63 static int hv_cpu_init(unsigned int cpu
)
66 struct hv_vp_assist_page
**hvp
= &hv_vp_assist_page
[smp_processor_id()];
70 input_arg
= (void **)this_cpu_ptr(hyperv_pcpu_input_arg
);
71 pg
= alloc_page(GFP_KERNEL
);
74 *input_arg
= page_address(pg
);
76 hv_get_vp_index(msr_vp_index
);
78 hv_vp_index
[smp_processor_id()] = msr_vp_index
;
80 if (msr_vp_index
> hv_max_vp_index
)
81 hv_max_vp_index
= msr_vp_index
;
83 if (!hv_vp_assist_page
)
87 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
88 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
89 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
90 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
91 * not be stopped in the case of CPU offlining and the VM will hang.
94 *hvp
= __vmalloc(PAGE_SIZE
, GFP_KERNEL
| __GFP_ZERO
,
101 val
= vmalloc_to_pfn(*hvp
);
102 val
= (val
<< HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT
) |
103 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE
;
105 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE
, val
);
111 static void (*hv_reenlightenment_cb
)(void);
113 static void hv_reenlightenment_notify(struct work_struct
*dummy
)
115 struct hv_tsc_emulation_status emu_status
;
117 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
119 /* Don't issue the callback if TSC accesses are not emulated */
120 if (hv_reenlightenment_cb
&& emu_status
.inprogress
)
121 hv_reenlightenment_cb();
123 static DECLARE_DELAYED_WORK(hv_reenlightenment_work
, hv_reenlightenment_notify
);
125 void hyperv_stop_tsc_emulation(void)
128 struct hv_tsc_emulation_status emu_status
;
130 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
131 emu_status
.inprogress
= 0;
132 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS
, *(u64
*)&emu_status
);
134 rdmsrl(HV_X64_MSR_TSC_FREQUENCY
, freq
);
135 tsc_khz
= div64_u64(freq
, 1000);
137 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation
);
139 static inline bool hv_reenlightenment_available(void)
142 * Check for required features and priviliges to make TSC frequency
143 * change notifications work.
145 return ms_hyperv
.features
& HV_X64_ACCESS_FREQUENCY_MSRS
&&
146 ms_hyperv
.misc_features
& HV_FEATURE_FREQUENCY_MSRS_AVAILABLE
&&
147 ms_hyperv
.features
& HV_X64_ACCESS_REENLIGHTENMENT
;
150 __visible
void __irq_entry
hyperv_reenlightenment_intr(struct pt_regs
*regs
)
154 inc_irq_stat(irq_hv_reenlightenment_count
);
156 schedule_delayed_work(&hv_reenlightenment_work
, HZ
/10);
161 void set_hv_tscchange_cb(void (*cb
)(void))
163 struct hv_reenlightenment_control re_ctrl
= {
164 .vector
= HYPERV_REENLIGHTENMENT_VECTOR
,
166 .target_vp
= hv_vp_index
[smp_processor_id()]
168 struct hv_tsc_emulation_control emu_ctrl
= {.enabled
= 1};
170 if (!hv_reenlightenment_available()) {
171 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
175 hv_reenlightenment_cb
= cb
;
177 /* Make sure callback is registered before we write to MSRs */
180 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
181 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL
, *((u64
*)&emu_ctrl
));
183 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb
);
185 void clear_hv_tscchange_cb(void)
187 struct hv_reenlightenment_control re_ctrl
;
189 if (!hv_reenlightenment_available())
192 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *(u64
*)&re_ctrl
);
194 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *(u64
*)&re_ctrl
);
196 hv_reenlightenment_cb
= NULL
;
198 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb
);
200 static int hv_cpu_die(unsigned int cpu
)
202 struct hv_reenlightenment_control re_ctrl
;
203 unsigned int new_cpu
;
206 void *input_pg
= NULL
;
208 local_irq_save(flags
);
209 input_arg
= (void **)this_cpu_ptr(hyperv_pcpu_input_arg
);
210 input_pg
= *input_arg
;
212 local_irq_restore(flags
);
213 free_page((unsigned long)input_pg
);
215 if (hv_vp_assist_page
&& hv_vp_assist_page
[cpu
])
216 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE
, 0);
218 if (hv_reenlightenment_cb
== NULL
)
221 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
222 if (re_ctrl
.target_vp
== hv_vp_index
[cpu
]) {
223 /* Reassign to some other online CPU */
224 new_cpu
= cpumask_any_but(cpu_online_mask
, cpu
);
226 re_ctrl
.target_vp
= hv_vp_index
[new_cpu
];
227 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL
, *((u64
*)&re_ctrl
));
233 static int __init
hv_pci_init(void)
235 int gen2vm
= efi_enabled(EFI_BOOT
);
238 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
239 * The purpose is to suppress the harmless warning:
240 * "PCI: Fatal: No config space access function found"
245 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
250 * This function is to be invoked early in the boot sequence after the
251 * hypervisor has been detected.
253 * 1. Setup the hypercall page.
254 * 2. Register Hyper-V specific clocksource.
255 * 3. Setup Hyper-V specific APIC entry points.
257 void __init
hyperv_init(void)
259 u64 guest_id
, required_msrs
;
260 union hv_x64_msr_hypercall_contents hypercall_msr
;
263 if (x86_hyper_type
!= X86_HYPER_MS_HYPERV
)
266 /* Absolutely required MSRs */
267 required_msrs
= HV_X64_MSR_HYPERCALL_AVAILABLE
|
268 HV_X64_MSR_VP_INDEX_AVAILABLE
;
270 if ((ms_hyperv
.features
& required_msrs
) != required_msrs
)
274 * Allocate the per-CPU state for the hypercall input arg.
275 * If this allocation fails, we will not be able to setup
276 * (per-CPU) hypercall input page and thus this failure is
279 hyperv_pcpu_input_arg
= alloc_percpu(void *);
281 BUG_ON(hyperv_pcpu_input_arg
== NULL
);
283 /* Allocate percpu VP index */
284 hv_vp_index
= kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index
),
289 for (i
= 0; i
< num_possible_cpus(); i
++)
290 hv_vp_index
[i
] = VP_INVAL
;
292 hv_vp_assist_page
= kcalloc(num_possible_cpus(),
293 sizeof(*hv_vp_assist_page
), GFP_KERNEL
);
294 if (!hv_vp_assist_page
) {
295 ms_hyperv
.hints
&= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED
;
299 cpuhp
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "x86/hyperv_init:online",
300 hv_cpu_init
, hv_cpu_die
);
302 goto free_vp_assist_page
;
305 * Setup the hypercall page and enable hypercalls.
306 * 1. Register the guest ID
307 * 2. Enable the hypercall and register the hypercall page
309 guest_id
= generate_guest_id(0, LINUX_VERSION_CODE
, 0);
310 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, guest_id
);
312 hv_hypercall_pg
= __vmalloc(PAGE_SIZE
, GFP_KERNEL
, PAGE_KERNEL_RX
);
313 if (hv_hypercall_pg
== NULL
) {
314 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, 0);
315 goto remove_cpuhp_state
;
318 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
319 hypercall_msr
.enable
= 1;
320 hypercall_msr
.guest_physical_address
= vmalloc_to_pfn(hv_hypercall_pg
);
321 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
324 * Ignore any errors in setting up stimer clockevents
325 * as we can run with the LAPIC timer as a fallback.
327 (void)hv_stimer_alloc();
331 x86_init
.pci
.arch_init
= hv_pci_init
;
336 cpuhp_remove_state(cpuhp
);
338 kfree(hv_vp_assist_page
);
339 hv_vp_assist_page
= NULL
;
346 * This routine is called before kexec/kdump, it does the required cleanup.
348 void hyperv_cleanup(void)
350 union hv_x64_msr_hypercall_contents hypercall_msr
;
352 /* Reset our OS id */
353 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, 0);
356 * Reset hypercall page reference before reset the page,
357 * let hypercall operations fail safely rather than
358 * panic the kernel for using invalid hypercall page
360 hv_hypercall_pg
= NULL
;
362 /* Reset the hypercall page */
363 hypercall_msr
.as_uint64
= 0;
364 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
366 /* Reset the TSC page */
367 hypercall_msr
.as_uint64
= 0;
368 wrmsrl(HV_X64_MSR_REFERENCE_TSC
, hypercall_msr
.as_uint64
);
370 EXPORT_SYMBOL_GPL(hyperv_cleanup
);
372 void hyperv_report_panic(struct pt_regs
*regs
, long err
)
374 static bool panic_reported
;
378 * We prefer to report panic on 'die' chain as we have proper
379 * registers to report, but if we miss it (e.g. on BUG()) we need
380 * to report it on 'panic'.
384 panic_reported
= true;
386 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, guest_id
);
388 wrmsrl(HV_X64_MSR_CRASH_P0
, err
);
389 wrmsrl(HV_X64_MSR_CRASH_P1
, guest_id
);
390 wrmsrl(HV_X64_MSR_CRASH_P2
, regs
->ip
);
391 wrmsrl(HV_X64_MSR_CRASH_P3
, regs
->ax
);
392 wrmsrl(HV_X64_MSR_CRASH_P4
, regs
->sp
);
395 * Let Hyper-V know there is crash data available
397 wrmsrl(HV_X64_MSR_CRASH_CTL
, HV_CRASH_CTL_CRASH_NOTIFY
);
399 EXPORT_SYMBOL_GPL(hyperv_report_panic
);
402 * hyperv_report_panic_msg - report panic message to Hyper-V
403 * @pa: physical address of the panic page containing the message
404 * @size: size of the message in the page
406 void hyperv_report_panic_msg(phys_addr_t pa
, size_t size
)
409 * P3 to contain the physical address of the panic page & P4 to
410 * contain the size of the panic data in that page. Rest of the
411 * registers are no-op when the NOTIFY_MSG flag is set.
413 wrmsrl(HV_X64_MSR_CRASH_P0
, 0);
414 wrmsrl(HV_X64_MSR_CRASH_P1
, 0);
415 wrmsrl(HV_X64_MSR_CRASH_P2
, 0);
416 wrmsrl(HV_X64_MSR_CRASH_P3
, pa
);
417 wrmsrl(HV_X64_MSR_CRASH_P4
, size
);
420 * Let Hyper-V know there is crash data available along with
423 wrmsrl(HV_X64_MSR_CRASH_CTL
,
424 (HV_CRASH_CTL_CRASH_NOTIFY
| HV_CRASH_CTL_CRASH_NOTIFY_MSG
));
426 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg
);
428 bool hv_is_hyperv_initialized(void)
430 union hv_x64_msr_hypercall_contents hypercall_msr
;
433 * Ensure that we're really on Hyper-V, and not a KVM or Xen
434 * emulation of Hyper-V
436 if (x86_hyper_type
!= X86_HYPER_MS_HYPERV
)
440 * Verify that earlier initialization succeeded by checking
441 * that the hypercall page is setup
443 hypercall_msr
.as_uint64
= 0;
444 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
446 return hypercall_msr
.enable
;
448 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized
);
450 bool hv_is_hibernation_supported(void)
452 return acpi_sleep_state_supported(ACPI_STATE_S4
);
454 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported
);