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