blob: d6b018b86c424cfd6d54806e7f2d2592f649306f [file] [log] [blame]
K. Y. Srinivasan87300462017-01-18 16:45:02 -07001/*
2 * X86 specific Hyper-V initialization code.
3 *
4 * Copyright (C) 2016, Microsoft, Inc.
5 *
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
7 *
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.
11 *
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.
17 *
18 */
19
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>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070027#include <linux/clockchips.h>
28
29
30#ifdef CONFIG_X86_64
31
32static struct ms_hyperv_tsc_page *tsc_pg;
33
34static u64 read_hv_clock_tsc(struct clocksource *arg)
35{
36 u64 current_tick;
37
38 if (tsc_pg->tsc_sequence != 0) {
39 /*
40 * Use the tsc page to compute the value.
41 */
42
43 while (1) {
44 u64 tmp;
45 u32 sequence = tsc_pg->tsc_sequence;
46 u64 cur_tsc;
47 u64 scale = tsc_pg->tsc_scale;
48 s64 offset = tsc_pg->tsc_offset;
49
50 rdtscll(cur_tsc);
51 /* current_tick = ((cur_tsc *scale) >> 64) + offset */
52 asm("mulq %3"
53 : "=d" (current_tick), "=a" (tmp)
54 : "a" (cur_tsc), "r" (scale));
55
56 current_tick += offset;
57 if (tsc_pg->tsc_sequence == sequence)
58 return current_tick;
59
60 if (tsc_pg->tsc_sequence != 0)
61 continue;
62 /*
63 * Fallback using MSR method.
64 */
65 break;
66 }
67 }
68 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
69 return current_tick;
70}
71
72static struct clocksource hyperv_cs_tsc = {
73 .name = "hyperv_clocksource_tsc_page",
74 .rating = 400,
75 .read = read_hv_clock_tsc,
76 .mask = CLOCKSOURCE_MASK(64),
77 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
78};
79#endif
80
81static u64 read_hv_clock_msr(struct clocksource *arg)
82{
83 u64 current_tick;
84 /*
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.
88 */
89 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
90 return current_tick;
91}
92
93static struct clocksource hyperv_cs_msr = {
94 .name = "hyperv_clocksource_msr",
95 .rating = 400,
96 .read = read_hv_clock_msr,
97 .mask = CLOCKSOURCE_MASK(64),
98 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
99};
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700100
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700101static void *hypercall_pg;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700102/*
103 * This function is to be invoked early in the boot sequence after the
104 * hypervisor has been detected.
105 *
106 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700107 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700108 */
109void hyperv_init(void)
110{
111 u64 guest_id;
112 union hv_x64_msr_hypercall_contents hypercall_msr;
113
114 if (x86_hyper != &x86_hyper_ms_hyperv)
115 return;
116
117 /*
118 * Setup the hypercall page and enable hypercalls.
119 * 1. Register the guest ID
120 * 2. Enable the hypercall and register the hypercall page
121 */
122 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
123 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
124
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700125 hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
126 if (hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700127 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
128 return;
129 }
130
131 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
132 hypercall_msr.enable = 1;
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700133 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700134 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700135
136 /*
137 * Register Hyper-V specific clocksource.
138 */
139#ifdef CONFIG_X86_64
140 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
141 union hv_x64_msr_hypercall_contents tsc_msr;
142
143 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
144 if (!tsc_pg) {
145 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
146 return;
147 }
148
149 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
150
151 tsc_msr.enable = 1;
152 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
153
154 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
155 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
156 return;
157 }
158#endif
159 /*
160 * For 32 bit guests just use the MSR based mechanism for reading
161 * the partition counter.
162 */
163
164 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
165 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700166}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700167
168/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700169 * This routine is called before kexec/kdump, it does the required cleanup.
170 */
171void hyperv_cleanup(void)
172{
173 union hv_x64_msr_hypercall_contents hypercall_msr;
174
175 /* Reset our OS id */
176 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
177
178 /* Reset the hypercall page */
179 hypercall_msr.as_uint64 = 0;
180 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
181}
182EXPORT_SYMBOL_GPL(hyperv_cleanup);
183
184/*
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700185 * hv_do_hypercall- Invoke the specified hypercall
186 */
187u64 hv_do_hypercall(u64 control, void *input, void *output)
188{
189 u64 input_address = (input) ? virt_to_phys(input) : 0;
190 u64 output_address = (output) ? virt_to_phys(output) : 0;
191#ifdef CONFIG_X86_64
192 u64 hv_status = 0;
193
194 if (!hypercall_pg)
195 return (u64)ULLONG_MAX;
196
197 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
198 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
199 "c" (control), "d" (input_address),
200 "m" (hypercall_pg));
201
202 return hv_status;
203
204#else
205
206 u32 control_hi = control >> 32;
207 u32 control_lo = control & 0xFFFFFFFF;
208 u32 hv_status_hi = 1;
209 u32 hv_status_lo = 1;
210 u32 input_address_hi = input_address >> 32;
211 u32 input_address_lo = input_address & 0xFFFFFFFF;
212 u32 output_address_hi = output_address >> 32;
213 u32 output_address_lo = output_address & 0xFFFFFFFF;
214
215 if (!hypercall_pg)
216 return (u64)ULLONG_MAX;
217
218 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
219 "=a"(hv_status_lo) : "d" (control_hi),
220 "a" (control_lo), "b" (input_address_hi),
221 "c" (input_address_lo), "D"(output_address_hi),
222 "S"(output_address_lo), "m" (hypercall_pg));
223
224 return hv_status_lo | ((u64)hv_status_hi << 32);
225#endif /* !x86_64 */
226}
227EXPORT_SYMBOL_GPL(hv_do_hypercall);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700228
229void hyperv_report_panic(struct pt_regs *regs)
230{
231 static bool panic_reported;
232
233 /*
234 * We prefer to report panic on 'die' chain as we have proper
235 * registers to report, but if we miss it (e.g. on BUG()) we need
236 * to report it on 'panic'.
237 */
238 if (panic_reported)
239 return;
240 panic_reported = true;
241
242 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
243 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
244 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
245 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
246 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
247
248 /*
249 * Let Hyper-V know there is crash data available
250 */
251 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
252}
253EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700254
255bool hv_is_hypercall_page_setup(void)
256{
257 union hv_x64_msr_hypercall_contents hypercall_msr;
258
259 /* Check if the hypercall page is setup */
260 hypercall_msr.as_uint64 = 0;
261 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
262
263 if (!hypercall_msr.enable)
264 return false;
265
266 return true;
267}
268EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);