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>
21 #include <asm/hypervisor.h>
22 #include <asm/hyperv.h>
23 #include <asm/mshyperv.h>
24 #include <linux/version.h>
25 #include <linux/vmalloc.h>
27 #include <linux/clockchips.h>
28 #include <linux/hyperv.h>
29 #include <linux/slab.h>
30 #include <linux/cpuhotplug.h>
32 #ifdef CONFIG_HYPERV_TSCPAGE
34 static struct ms_hyperv_tsc_page *tsc_pg;
36 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
41 static u64 read_hv_clock_tsc(struct clocksource *arg)
43 u64 current_tick = hv_read_tsc_page(tsc_pg);
45 if (current_tick == U64_MAX)
46 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
51 static struct clocksource hyperv_cs_tsc = {
52 .name = "hyperv_clocksource_tsc_page",
54 .read = read_hv_clock_tsc,
55 .mask = CLOCKSOURCE_MASK(64),
56 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
60 static u64 read_hv_clock_msr(struct clocksource *arg)
64 * Read the partition counter to get the current tick count. This count
65 * is set to 0 when the partition is created and is incremented in
66 * 100 nanosecond units.
68 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
72 static struct clocksource hyperv_cs_msr = {
73 .name = "hyperv_clocksource_msr",
75 .read = read_hv_clock_msr,
76 .mask = CLOCKSOURCE_MASK(64),
77 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
80 void *hv_hypercall_pg;
81 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
82 struct clocksource *hyperv_cs;
83 EXPORT_SYMBOL_GPL(hyperv_cs);
86 EXPORT_SYMBOL_GPL(hv_vp_index);
90 static int hv_cpu_init(unsigned int cpu)
94 hv_get_vp_index(msr_vp_index);
96 hv_vp_index[smp_processor_id()] = msr_vp_index;
98 if (msr_vp_index > hv_max_vp_index)
99 hv_max_vp_index = msr_vp_index;
105 * This function is to be invoked early in the boot sequence after the
106 * hypervisor has been detected.
108 * 1. Setup the hypercall page.
109 * 2. Register Hyper-V specific clocksource.
111 void hyperv_init(void)
114 union hv_x64_msr_hypercall_contents hypercall_msr;
116 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
119 /* Allocate percpu VP index */
120 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
125 if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
126 hv_cpu_init, NULL) < 0)
130 * Setup the hypercall page and enable hypercalls.
131 * 1. Register the guest ID
132 * 2. Enable the hypercall and register the hypercall page
134 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
135 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
137 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
138 if (hv_hypercall_pg == NULL) {
139 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
143 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
144 hypercall_msr.enable = 1;
145 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
146 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
151 * Register Hyper-V specific clocksource.
153 #ifdef CONFIG_HYPERV_TSCPAGE
154 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
155 union hv_x64_msr_hypercall_contents tsc_msr;
157 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
159 goto register_msr_cs;
161 hyperv_cs = &hyperv_cs_tsc;
163 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
166 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
168 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
170 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
172 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
178 * For 32 bit guests just use the MSR based mechanism for reading
179 * the partition counter.
182 hyperv_cs = &hyperv_cs_msr;
183 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
184 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
194 * This routine is called before kexec/kdump, it does the required cleanup.
196 void hyperv_cleanup(void)
198 union hv_x64_msr_hypercall_contents hypercall_msr;
200 /* Reset our OS id */
201 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
203 /* Reset the hypercall page */
204 hypercall_msr.as_uint64 = 0;
205 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
207 /* Reset the TSC page */
208 hypercall_msr.as_uint64 = 0;
209 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
211 EXPORT_SYMBOL_GPL(hyperv_cleanup);
213 void hyperv_report_panic(struct pt_regs *regs)
215 static bool panic_reported;
218 * We prefer to report panic on 'die' chain as we have proper
219 * registers to report, but if we miss it (e.g. on BUG()) we need
220 * to report it on 'panic'.
224 panic_reported = true;
226 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
227 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
228 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
229 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
230 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
233 * Let Hyper-V know there is crash data available
235 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
237 EXPORT_SYMBOL_GPL(hyperv_report_panic);
239 bool hv_is_hypercall_page_setup(void)
241 union hv_x64_msr_hypercall_contents hypercall_msr;
243 /* Check if the hypercall page is setup */
244 hypercall_msr.as_uint64 = 0;
245 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
247 if (!hypercall_msr.enable)
252 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);