Merge tag 'pm-4.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux/fpc-iii.git] / arch / x86 / hyperv / hv_init.c
blob5b882cc0c0e9a96e1bd8beeec260ef657a439de4
1 /*
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
16 * details.
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>
26 #include <linux/mm.h>
27 #include <linux/clockchips.h>
28 #include <linux/hyperv.h>
30 #ifdef CONFIG_HYPERV_TSCPAGE
32 static struct ms_hyperv_tsc_page *tsc_pg;
34 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
36 return tsc_pg;
39 static u64 read_hv_clock_tsc(struct clocksource *arg)
41 u64 current_tick = hv_read_tsc_page(tsc_pg);
43 if (current_tick == U64_MAX)
44 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
46 return current_tick;
49 static struct clocksource hyperv_cs_tsc = {
50 .name = "hyperv_clocksource_tsc_page",
51 .rating = 400,
52 .read = read_hv_clock_tsc,
53 .mask = CLOCKSOURCE_MASK(64),
54 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
56 #endif
58 static u64 read_hv_clock_msr(struct clocksource *arg)
60 u64 current_tick;
62 * Read the partition counter to get the current tick count. This count
63 * is set to 0 when the partition is created and is incremented in
64 * 100 nanosecond units.
66 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
67 return current_tick;
70 static struct clocksource hyperv_cs_msr = {
71 .name = "hyperv_clocksource_msr",
72 .rating = 400,
73 .read = read_hv_clock_msr,
74 .mask = CLOCKSOURCE_MASK(64),
75 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
78 static void *hypercall_pg;
79 struct clocksource *hyperv_cs;
80 EXPORT_SYMBOL_GPL(hyperv_cs);
83 * This function is to be invoked early in the boot sequence after the
84 * hypervisor has been detected.
86 * 1. Setup the hypercall page.
87 * 2. Register Hyper-V specific clocksource.
89 void hyperv_init(void)
91 u64 guest_id;
92 union hv_x64_msr_hypercall_contents hypercall_msr;
94 if (x86_hyper != &x86_hyper_ms_hyperv)
95 return;
98 * Setup the hypercall page and enable hypercalls.
99 * 1. Register the guest ID
100 * 2. Enable the hypercall and register the hypercall page
102 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
103 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
105 hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
106 if (hypercall_pg == NULL) {
107 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
108 return;
111 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
112 hypercall_msr.enable = 1;
113 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
114 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
117 * Register Hyper-V specific clocksource.
119 #ifdef CONFIG_HYPERV_TSCPAGE
120 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
121 union hv_x64_msr_hypercall_contents tsc_msr;
123 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
124 if (!tsc_pg)
125 goto register_msr_cs;
127 hyperv_cs = &hyperv_cs_tsc;
129 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
131 tsc_msr.enable = 1;
132 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
134 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
136 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
138 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
139 return;
141 register_msr_cs:
142 #endif
144 * For 32 bit guests just use the MSR based mechanism for reading
145 * the partition counter.
148 hyperv_cs = &hyperv_cs_msr;
149 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
150 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
154 * This routine is called before kexec/kdump, it does the required cleanup.
156 void hyperv_cleanup(void)
158 union hv_x64_msr_hypercall_contents hypercall_msr;
160 /* Reset our OS id */
161 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
163 /* Reset the hypercall page */
164 hypercall_msr.as_uint64 = 0;
165 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
167 /* Reset the TSC page */
168 hypercall_msr.as_uint64 = 0;
169 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
171 EXPORT_SYMBOL_GPL(hyperv_cleanup);
174 * hv_do_hypercall- Invoke the specified hypercall
176 u64 hv_do_hypercall(u64 control, void *input, void *output)
178 u64 input_address = (input) ? virt_to_phys(input) : 0;
179 u64 output_address = (output) ? virt_to_phys(output) : 0;
180 #ifdef CONFIG_X86_64
181 u64 hv_status = 0;
183 if (!hypercall_pg)
184 return (u64)ULLONG_MAX;
186 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
187 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
188 "c" (control), "d" (input_address),
189 "m" (hypercall_pg));
191 return hv_status;
193 #else
195 u32 control_hi = control >> 32;
196 u32 control_lo = control & 0xFFFFFFFF;
197 u32 hv_status_hi = 1;
198 u32 hv_status_lo = 1;
199 u32 input_address_hi = input_address >> 32;
200 u32 input_address_lo = input_address & 0xFFFFFFFF;
201 u32 output_address_hi = output_address >> 32;
202 u32 output_address_lo = output_address & 0xFFFFFFFF;
204 if (!hypercall_pg)
205 return (u64)ULLONG_MAX;
207 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
208 "=a"(hv_status_lo) : "d" (control_hi),
209 "a" (control_lo), "b" (input_address_hi),
210 "c" (input_address_lo), "D"(output_address_hi),
211 "S"(output_address_lo), "m" (hypercall_pg));
213 return hv_status_lo | ((u64)hv_status_hi << 32);
214 #endif /* !x86_64 */
216 EXPORT_SYMBOL_GPL(hv_do_hypercall);
218 void hyperv_report_panic(struct pt_regs *regs)
220 static bool panic_reported;
223 * We prefer to report panic on 'die' chain as we have proper
224 * registers to report, but if we miss it (e.g. on BUG()) we need
225 * to report it on 'panic'.
227 if (panic_reported)
228 return;
229 panic_reported = true;
231 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
232 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
233 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
234 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
235 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
238 * Let Hyper-V know there is crash data available
240 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
242 EXPORT_SYMBOL_GPL(hyperv_report_panic);
244 bool hv_is_hypercall_page_setup(void)
246 union hv_x64_msr_hypercall_contents hypercall_msr;
248 /* Check if the hypercall page is setup */
249 hypercall_msr.as_uint64 = 0;
250 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
252 if (!hypercall_msr.enable)
253 return false;
255 return true;
257 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);