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>
32 static struct ms_hyperv_tsc_page *tsc_pg;
34 static u64 read_hv_clock_tsc(struct clocksource *arg)
38 if (tsc_pg->tsc_sequence != 0) {
40 * Use the tsc page to compute the value.
45 u32 sequence = tsc_pg->tsc_sequence;
47 u64 scale = tsc_pg->tsc_scale;
48 s64 offset = tsc_pg->tsc_offset;
51 /* current_tick = ((cur_tsc *scale) >> 64) + offset */
53 : "=d" (current_tick), "=a" (tmp)
54 : "a" (cur_tsc), "r" (scale));
56 current_tick += offset;
57 if (tsc_pg->tsc_sequence == sequence)
60 if (tsc_pg->tsc_sequence != 0)
63 * Fallback using MSR method.
68 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
72 static struct clocksource hyperv_cs_tsc = {
73 .name = "hyperv_clocksource_tsc_page",
75 .read = read_hv_clock_tsc,
76 .mask = CLOCKSOURCE_MASK(64),
77 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
81 static u64 read_hv_clock_msr(struct clocksource *arg)
85 * Read the partition counter to get the current tick count. This count
86 * is set to 0 when the partition is created and is incremented in
87 * 100 nanosecond units.
89 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
93 static struct clocksource hyperv_cs_msr = {
94 .name = "hyperv_clocksource_msr",
96 .read = read_hv_clock_msr,
97 .mask = CLOCKSOURCE_MASK(64),
98 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
101 static void *hypercall_pg;
102 struct clocksource *hyperv_cs;
103 EXPORT_SYMBOL_GPL(hyperv_cs);
106 * This function is to be invoked early in the boot sequence after the
107 * hypervisor has been detected.
109 * 1. Setup the hypercall page.
110 * 2. Register Hyper-V specific clocksource.
112 void hyperv_init(void)
115 union hv_x64_msr_hypercall_contents hypercall_msr;
117 if (x86_hyper != &x86_hyper_ms_hyperv)
121 * Setup the hypercall page and enable hypercalls.
122 * 1. Register the guest ID
123 * 2. Enable the hypercall and register the hypercall page
125 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
126 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
128 hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
129 if (hypercall_pg == NULL) {
130 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
134 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
135 hypercall_msr.enable = 1;
136 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
137 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
140 * Register Hyper-V specific clocksource.
143 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
144 union hv_x64_msr_hypercall_contents tsc_msr;
146 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
148 goto register_msr_cs;
150 hyperv_cs = &hyperv_cs_tsc;
152 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
155 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
157 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
158 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
163 * For 32 bit guests just use the MSR based mechanism for reading
164 * the partition counter.
168 hyperv_cs = &hyperv_cs_msr;
169 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
170 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
174 * This routine is called before kexec/kdump, it does the required cleanup.
176 void hyperv_cleanup(void)
178 union hv_x64_msr_hypercall_contents hypercall_msr;
180 /* Reset our OS id */
181 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
183 /* Reset the hypercall page */
184 hypercall_msr.as_uint64 = 0;
185 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
187 /* Reset the TSC page */
188 hypercall_msr.as_uint64 = 0;
189 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
191 EXPORT_SYMBOL_GPL(hyperv_cleanup);
194 * hv_do_hypercall- Invoke the specified hypercall
196 u64 hv_do_hypercall(u64 control, void *input, void *output)
198 u64 input_address = (input) ? virt_to_phys(input) : 0;
199 u64 output_address = (output) ? virt_to_phys(output) : 0;
204 return (u64)ULLONG_MAX;
206 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
207 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
208 "c" (control), "d" (input_address),
215 u32 control_hi = control >> 32;
216 u32 control_lo = control & 0xFFFFFFFF;
217 u32 hv_status_hi = 1;
218 u32 hv_status_lo = 1;
219 u32 input_address_hi = input_address >> 32;
220 u32 input_address_lo = input_address & 0xFFFFFFFF;
221 u32 output_address_hi = output_address >> 32;
222 u32 output_address_lo = output_address & 0xFFFFFFFF;
225 return (u64)ULLONG_MAX;
227 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
228 "=a"(hv_status_lo) : "d" (control_hi),
229 "a" (control_lo), "b" (input_address_hi),
230 "c" (input_address_lo), "D"(output_address_hi),
231 "S"(output_address_lo), "m" (hypercall_pg));
233 return hv_status_lo | ((u64)hv_status_hi << 32);
236 EXPORT_SYMBOL_GPL(hv_do_hypercall);
238 void hyperv_report_panic(struct pt_regs *regs)
240 static bool panic_reported;
243 * We prefer to report panic on 'die' chain as we have proper
244 * registers to report, but if we miss it (e.g. on BUG()) we need
245 * to report it on 'panic'.
249 panic_reported = true;
251 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
252 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
253 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
254 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
255 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
258 * Let Hyper-V know there is crash data available
260 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
262 EXPORT_SYMBOL_GPL(hyperv_report_panic);
264 bool hv_is_hypercall_page_setup(void)
266 union hv_x64_msr_hypercall_contents hypercall_msr;
268 /* Check if the hypercall page is setup */
269 hypercall_msr.as_uint64 = 0;
270 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
272 if (!hypercall_msr.enable)
277 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);