2 * X86 specific Hyper-V initialization code.
4 * Copyright (C) 2016, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
20 #include <linux/types.h>
23 #include <asm/hypervisor.h>
24 #include <asm/hyperv-tlfs.h>
25 #include <asm/mshyperv.h>
26 #include <linux/version.h>
27 #include <linux/vmalloc.h>
29 #include <linux/clockchips.h>
30 #include <linux/hyperv.h>
31 #include <linux/slab.h>
32 #include <linux/cpuhotplug.h>
34 #ifdef CONFIG_HYPERV_TSCPAGE
36 static struct ms_hyperv_tsc_page *tsc_pg;
38 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
42 EXPORT_SYMBOL_GPL(hv_get_tsc_page);
44 static u64 read_hv_clock_tsc(struct clocksource *arg)
46 u64 current_tick = hv_read_tsc_page(tsc_pg);
48 if (current_tick == U64_MAX)
49 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
54 static struct clocksource hyperv_cs_tsc = {
55 .name = "hyperv_clocksource_tsc_page",
57 .read = read_hv_clock_tsc,
58 .mask = CLOCKSOURCE_MASK(64),
59 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
63 static u64 read_hv_clock_msr(struct clocksource *arg)
67 * Read the partition counter to get the current tick count. This count
68 * is set to 0 when the partition is created and is incremented in
69 * 100 nanosecond units.
71 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
75 static struct clocksource hyperv_cs_msr = {
76 .name = "hyperv_clocksource_msr",
78 .read = read_hv_clock_msr,
79 .mask = CLOCKSOURCE_MASK(64),
80 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
83 void *hv_hypercall_pg;
84 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
85 struct clocksource *hyperv_cs;
86 EXPORT_SYMBOL_GPL(hyperv_cs);
89 EXPORT_SYMBOL_GPL(hv_vp_index);
91 struct hv_vp_assist_page **hv_vp_assist_page;
92 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
94 void __percpu **hyperv_pcpu_input_arg;
95 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
99 static int hv_cpu_init(unsigned int cpu)
102 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
105 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
106 *input_arg = page_address(alloc_page(GFP_KERNEL));
108 hv_get_vp_index(msr_vp_index);
110 hv_vp_index[smp_processor_id()] = msr_vp_index;
112 if (msr_vp_index > hv_max_vp_index)
113 hv_max_vp_index = msr_vp_index;
115 if (!hv_vp_assist_page)
119 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
124 val = vmalloc_to_pfn(*hvp);
125 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
126 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
128 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
134 static void (*hv_reenlightenment_cb)(void);
136 static void hv_reenlightenment_notify(struct work_struct *dummy)
138 struct hv_tsc_emulation_status emu_status;
140 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
142 /* Don't issue the callback if TSC accesses are not emulated */
143 if (hv_reenlightenment_cb && emu_status.inprogress)
144 hv_reenlightenment_cb();
146 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
148 void hyperv_stop_tsc_emulation(void)
151 struct hv_tsc_emulation_status emu_status;
153 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
154 emu_status.inprogress = 0;
155 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
157 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
158 tsc_khz = div64_u64(freq, 1000);
160 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
162 static inline bool hv_reenlightenment_available(void)
165 * Check for required features and priviliges to make TSC frequency
166 * change notifications work.
168 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
169 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
170 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
173 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
177 inc_irq_stat(irq_hv_reenlightenment_count);
179 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
184 void set_hv_tscchange_cb(void (*cb)(void))
186 struct hv_reenlightenment_control re_ctrl = {
187 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
189 .target_vp = hv_vp_index[smp_processor_id()]
191 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
193 if (!hv_reenlightenment_available()) {
194 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
198 hv_reenlightenment_cb = cb;
200 /* Make sure callback is registered before we write to MSRs */
203 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
204 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
206 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
208 void clear_hv_tscchange_cb(void)
210 struct hv_reenlightenment_control re_ctrl;
212 if (!hv_reenlightenment_available())
215 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
217 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
219 hv_reenlightenment_cb = NULL;
221 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
223 static int hv_cpu_die(unsigned int cpu)
225 struct hv_reenlightenment_control re_ctrl;
226 unsigned int new_cpu;
229 void *input_pg = NULL;
231 local_irq_save(flags);
232 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
233 input_pg = *input_arg;
235 local_irq_restore(flags);
236 free_page((unsigned long)input_pg);
238 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
239 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
241 if (hv_reenlightenment_cb == NULL)
244 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
245 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
246 /* Reassign to some other online CPU */
247 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
249 re_ctrl.target_vp = hv_vp_index[new_cpu];
250 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
257 * This function is to be invoked early in the boot sequence after the
258 * hypervisor has been detected.
260 * 1. Setup the hypercall page.
261 * 2. Register Hyper-V specific clocksource.
262 * 3. Setup Hyper-V specific APIC entry points.
264 void __init hyperv_init(void)
266 u64 guest_id, required_msrs;
267 union hv_x64_msr_hypercall_contents hypercall_msr;
270 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
273 /* Absolutely required MSRs */
274 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
275 HV_X64_MSR_VP_INDEX_AVAILABLE;
277 if ((ms_hyperv.features & required_msrs) != required_msrs)
281 * Allocate the per-CPU state for the hypercall input arg.
282 * If this allocation fails, we will not be able to setup
283 * (per-CPU) hypercall input page and thus this failure is
286 hyperv_pcpu_input_arg = alloc_percpu(void *);
288 BUG_ON(hyperv_pcpu_input_arg == NULL);
290 /* Allocate percpu VP index */
291 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
296 for (i = 0; i < num_possible_cpus(); i++)
297 hv_vp_index[i] = VP_INVAL;
299 hv_vp_assist_page = kcalloc(num_possible_cpus(),
300 sizeof(*hv_vp_assist_page), GFP_KERNEL);
301 if (!hv_vp_assist_page) {
302 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
306 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
307 hv_cpu_init, hv_cpu_die);
309 goto free_vp_assist_page;
312 * Setup the hypercall page and enable hypercalls.
313 * 1. Register the guest ID
314 * 2. Enable the hypercall and register the hypercall page
316 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
317 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
319 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
320 if (hv_hypercall_pg == NULL) {
321 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
322 goto remove_cpuhp_state;
325 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
326 hypercall_msr.enable = 1;
327 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
328 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
333 * Register Hyper-V specific clocksource.
335 #ifdef CONFIG_HYPERV_TSCPAGE
336 if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
337 union hv_x64_msr_hypercall_contents tsc_msr;
339 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
341 goto register_msr_cs;
343 hyperv_cs = &hyperv_cs_tsc;
345 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
348 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
350 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
352 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
354 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
360 * For 32 bit guests just use the MSR based mechanism for reading
361 * the partition counter.
364 hyperv_cs = &hyperv_cs_msr;
365 if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
366 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
371 cpuhp_remove_state(cpuhp);
373 kfree(hv_vp_assist_page);
374 hv_vp_assist_page = NULL;
381 * This routine is called before kexec/kdump, it does the required cleanup.
383 void hyperv_cleanup(void)
385 union hv_x64_msr_hypercall_contents hypercall_msr;
387 /* Reset our OS id */
388 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
390 /* Reset the hypercall page */
391 hypercall_msr.as_uint64 = 0;
392 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
394 /* Reset the TSC page */
395 hypercall_msr.as_uint64 = 0;
396 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
398 EXPORT_SYMBOL_GPL(hyperv_cleanup);
400 void hyperv_report_panic(struct pt_regs *regs, long err)
402 static bool panic_reported;
406 * We prefer to report panic on 'die' chain as we have proper
407 * registers to report, but if we miss it (e.g. on BUG()) we need
408 * to report it on 'panic'.
412 panic_reported = true;
414 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
416 wrmsrl(HV_X64_MSR_CRASH_P0, err);
417 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
418 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
419 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
420 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
423 * Let Hyper-V know there is crash data available
425 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
427 EXPORT_SYMBOL_GPL(hyperv_report_panic);
430 * hyperv_report_panic_msg - report panic message to Hyper-V
431 * @pa: physical address of the panic page containing the message
432 * @size: size of the message in the page
434 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
437 * P3 to contain the physical address of the panic page & P4 to
438 * contain the size of the panic data in that page. Rest of the
439 * registers are no-op when the NOTIFY_MSG flag is set.
441 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
442 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
443 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
444 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
445 wrmsrl(HV_X64_MSR_CRASH_P4, size);
448 * Let Hyper-V know there is crash data available along with
451 wrmsrl(HV_X64_MSR_CRASH_CTL,
452 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
454 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
456 bool hv_is_hyperv_initialized(void)
458 union hv_x64_msr_hypercall_contents hypercall_msr;
461 * Ensure that we're really on Hyper-V, and not a KVM or Xen
462 * emulation of Hyper-V
464 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
468 * Verify that earlier initialization succeeded by checking
469 * that the hypercall page is setup
471 hypercall_msr.as_uint64 = 0;
472 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
474 return hypercall_msr.enable;
476 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);