blob: 4b82bc20692961708d1b65e227d63e5bf3e2bbaf [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>
Vitaly Kuznetsov93286262018-01-24 14:23:33 +010021#include <asm/apic.h>
22#include <asm/desc.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070023#include <asm/hypervisor.h>
Vitaly Kuznetsov5a485802018-03-20 15:02:05 +010024#include <asm/hyperv-tlfs.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070025#include <asm/mshyperv.h>
26#include <linux/version.h>
27#include <linux/vmalloc.h>
28#include <linux/mm.h>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070029#include <linux/clockchips.h>
Stephen Hemminger67071812017-03-04 18:27:11 -070030#include <linux/hyperv.h>
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020031#include <linux/slab.h>
32#include <linux/cpuhotplug.h>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070033
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010034#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070035
36static struct ms_hyperv_tsc_page *tsc_pg;
37
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010038struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
39{
40 return tsc_pg;
41}
Vitaly Kuznetsove2768ea2018-01-24 14:23:32 +010042EXPORT_SYMBOL_GPL(hv_get_tsc_page);
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010043
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070044static u64 read_hv_clock_tsc(struct clocksource *arg)
45{
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010046 u64 current_tick = hv_read_tsc_page(tsc_pg);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070047
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010048 if (current_tick == U64_MAX)
49 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070050
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070051 return current_tick;
52}
53
54static struct clocksource hyperv_cs_tsc = {
55 .name = "hyperv_clocksource_tsc_page",
56 .rating = 400,
57 .read = read_hv_clock_tsc,
58 .mask = CLOCKSOURCE_MASK(64),
59 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
60};
61#endif
62
63static u64 read_hv_clock_msr(struct clocksource *arg)
64{
65 u64 current_tick;
66 /*
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.
70 */
71 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
72 return current_tick;
73}
74
75static struct clocksource hyperv_cs_msr = {
76 .name = "hyperv_clocksource_msr",
77 .rating = 400,
78 .read = read_hv_clock_msr,
79 .mask = CLOCKSOURCE_MASK(64),
80 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
81};
K. Y. Srinivasan87300462017-01-18 16:45:02 -070082
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020083void *hv_hypercall_pg;
84EXPORT_SYMBOL_GPL(hv_hypercall_pg);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070085struct clocksource *hyperv_cs;
86EXPORT_SYMBOL_GPL(hyperv_cs);
87
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020088u32 *hv_vp_index;
89EXPORT_SYMBOL_GPL(hv_vp_index);
90
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +020091u32 hv_max_vp_index;
92
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020093static int hv_cpu_init(unsigned int cpu)
94{
95 u64 msr_vp_index;
96
97 hv_get_vp_index(msr_vp_index);
98
99 hv_vp_index[smp_processor_id()] = msr_vp_index;
100
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +0200101 if (msr_vp_index > hv_max_vp_index)
102 hv_max_vp_index = msr_vp_index;
103
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200104 return 0;
105}
106
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100107static void (*hv_reenlightenment_cb)(void);
108
109static void hv_reenlightenment_notify(struct work_struct *dummy)
110{
111 struct hv_tsc_emulation_status emu_status;
112
113 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
114
115 /* Don't issue the callback if TSC accesses are not emulated */
116 if (hv_reenlightenment_cb && emu_status.inprogress)
117 hv_reenlightenment_cb();
118}
119static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
120
121void hyperv_stop_tsc_emulation(void)
122{
123 u64 freq;
124 struct hv_tsc_emulation_status emu_status;
125
126 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
127 emu_status.inprogress = 0;
128 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
129
130 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
131 tsc_khz = div64_u64(freq, 1000);
132}
133EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
134
135static inline bool hv_reenlightenment_available(void)
136{
137 /*
138 * Check for required features and priviliges to make TSC frequency
139 * change notifications work.
140 */
141 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
142 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
143 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
144}
145
146__visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
147{
148 entering_ack_irq();
149
Vitaly Kuznetsov51d4e5d2018-01-24 14:23:35 +0100150 inc_irq_stat(irq_hv_reenlightenment_count);
151
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100152 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
153
154 exiting_irq();
155}
156
157void set_hv_tscchange_cb(void (*cb)(void))
158{
159 struct hv_reenlightenment_control re_ctrl = {
160 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
161 .enabled = 1,
162 .target_vp = hv_vp_index[smp_processor_id()]
163 };
164 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
165
166 if (!hv_reenlightenment_available()) {
167 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
168 return;
169 }
170
171 hv_reenlightenment_cb = cb;
172
173 /* Make sure callback is registered before we write to MSRs */
174 wmb();
175
176 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
177 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
178}
179EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
180
181void clear_hv_tscchange_cb(void)
182{
183 struct hv_reenlightenment_control re_ctrl;
184
185 if (!hv_reenlightenment_available())
186 return;
187
188 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
189 re_ctrl.enabled = 0;
190 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
191
192 hv_reenlightenment_cb = NULL;
193}
194EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
195
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100196static int hv_cpu_die(unsigned int cpu)
197{
198 struct hv_reenlightenment_control re_ctrl;
199 unsigned int new_cpu;
200
201 if (hv_reenlightenment_cb == NULL)
202 return 0;
203
204 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
205 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
206 /* Reassign to some other online CPU */
207 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
208
209 re_ctrl.target_vp = hv_vp_index[new_cpu];
210 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
211 }
212
213 return 0;
214}
215
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700216/*
217 * This function is to be invoked early in the boot sequence after the
218 * hypervisor has been detected.
219 *
220 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700221 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700222 */
223void hyperv_init(void)
224{
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100225 u64 guest_id, required_msrs;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700226 union hv_x64_msr_hypercall_contents hypercall_msr;
227
Juergen Gross03b2a322017-11-09 14:27:36 +0100228 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700229 return;
230
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100231 /* Absolutely required MSRs */
232 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
233 HV_X64_MSR_VP_INDEX_AVAILABLE;
234
235 if ((ms_hyperv.features & required_msrs) != required_msrs)
236 return;
237
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200238 /* Allocate percpu VP index */
239 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
240 GFP_KERNEL);
241 if (!hv_vp_index)
242 return;
243
244 if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100245 hv_cpu_init, hv_cpu_die) < 0)
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200246 goto free_vp_index;
247
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700248 /*
249 * Setup the hypercall page and enable hypercalls.
250 * 1. Register the guest ID
251 * 2. Enable the hypercall and register the hypercall page
252 */
253 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
254 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
255
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200256 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
257 if (hv_hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700258 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200259 goto free_vp_index;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700260 }
261
262 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
263 hypercall_msr.enable = 1;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200264 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700265 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700266
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200267 hyper_alloc_mmu();
268
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700269 /*
270 * Register Hyper-V specific clocksource.
271 */
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100272#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700273 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
274 union hv_x64_msr_hypercall_contents tsc_msr;
275
276 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700277 if (!tsc_pg)
278 goto register_msr_cs;
279
280 hyperv_cs = &hyperv_cs_tsc;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700281
282 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
283
284 tsc_msr.enable = 1;
285 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
286
287 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
Vitaly Kuznetsov90b20432017-03-03 14:21:42 +0100288
289 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
290
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700291 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
292 return;
293 }
Arnd Bergmann73667e32017-02-14 22:17:17 +0100294register_msr_cs:
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700295#endif
296 /*
297 * For 32 bit guests just use the MSR based mechanism for reading
298 * the partition counter.
299 */
300
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700301 hyperv_cs = &hyperv_cs_msr;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700302 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
303 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200304
305 return;
306
307free_vp_index:
308 kfree(hv_vp_index);
309 hv_vp_index = NULL;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700310}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700311
312/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700313 * This routine is called before kexec/kdump, it does the required cleanup.
314 */
315void hyperv_cleanup(void)
316{
317 union hv_x64_msr_hypercall_contents hypercall_msr;
318
319 /* Reset our OS id */
320 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
321
322 /* Reset the hypercall page */
323 hypercall_msr.as_uint64 = 0;
324 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Vitaly Kuznetsov5647dbf2017-01-28 12:37:15 -0700325
326 /* Reset the TSC page */
327 hypercall_msr.as_uint64 = 0;
328 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700329}
330EXPORT_SYMBOL_GPL(hyperv_cleanup);
331
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700332void hyperv_report_panic(struct pt_regs *regs, long err)
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700333{
334 static bool panic_reported;
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700335 u64 guest_id;
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700336
337 /*
338 * We prefer to report panic on 'die' chain as we have proper
339 * registers to report, but if we miss it (e.g. on BUG()) we need
340 * to report it on 'panic'.
341 */
342 if (panic_reported)
343 return;
344 panic_reported = true;
345
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700346 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
347
348 wrmsrl(HV_X64_MSR_CRASH_P0, err);
349 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
350 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
351 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
352 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700353
354 /*
355 * Let Hyper-V know there is crash data available
356 */
357 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
358}
359EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700360
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700361bool hv_is_hyperv_initialized(void)
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700362{
363 union hv_x64_msr_hypercall_contents hypercall_msr;
364
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700365 /*
366 * Ensure that we're really on Hyper-V, and not a KVM or Xen
367 * emulation of Hyper-V
368 */
369 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
370 return false;
371
372 /*
373 * Verify that earlier initialization succeeded by checking
374 * that the hypercall page is setup
375 */
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700376 hypercall_msr.as_uint64 = 0;
377 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
378
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700379 return hypercall_msr.enable;
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700380}
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700381EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);