712ac40081f7e30a9813e895ed1b4af11bd55c82
[linux-2.6-block.git] / arch / x86 / hyperv / hv_init.c
1 /*
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/apic.h>
22 #include <asm/desc.h>
23 #include <asm/hypervisor.h>
24 #include <asm/hyperv.h>
25 #include <asm/mshyperv.h>
26 #include <linux/version.h>
27 #include <linux/vmalloc.h>
28 #include <linux/mm.h>
29 #include <linux/clockchips.h>
30 #include <linux/hyperv.h>
31 #include <linux/slab.h>
32 #include <linux/cpuhotplug.h>
33
34 #ifdef CONFIG_HYPERV_TSCPAGE
35
36 static struct ms_hyperv_tsc_page *tsc_pg;
37
38 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
39 {
40         return tsc_pg;
41 }
42 EXPORT_SYMBOL_GPL(hv_get_tsc_page);
43
44 static u64 read_hv_clock_tsc(struct clocksource *arg)
45 {
46         u64 current_tick = hv_read_tsc_page(tsc_pg);
47
48         if (current_tick == U64_MAX)
49                 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
50
51         return current_tick;
52 }
53
54 static 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
63 static 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
75 static 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 };
82
83 void *hv_hypercall_pg;
84 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
85 struct clocksource *hyperv_cs;
86 EXPORT_SYMBOL_GPL(hyperv_cs);
87
88 u32 *hv_vp_index;
89 EXPORT_SYMBOL_GPL(hv_vp_index);
90
91 u32 hv_max_vp_index;
92
93 static 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
101         if (msr_vp_index > hv_max_vp_index)
102                 hv_max_vp_index = msr_vp_index;
103
104         return 0;
105 }
106
107 static void (*hv_reenlightenment_cb)(void);
108
109 static 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 }
119 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
120
121 void 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 }
133 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
134
135 static 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
150         schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
151
152         exiting_irq();
153 }
154
155 void set_hv_tscchange_cb(void (*cb)(void))
156 {
157         struct hv_reenlightenment_control re_ctrl = {
158                 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
159                 .enabled = 1,
160                 .target_vp = hv_vp_index[smp_processor_id()]
161         };
162         struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
163
164         if (!hv_reenlightenment_available()) {
165                 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
166                 return;
167         }
168
169         hv_reenlightenment_cb = cb;
170
171         /* Make sure callback is registered before we write to MSRs */
172         wmb();
173
174         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
175         wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
176 }
177 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
178
179 void clear_hv_tscchange_cb(void)
180 {
181         struct hv_reenlightenment_control re_ctrl;
182
183         if (!hv_reenlightenment_available())
184                 return;
185
186         rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
187         re_ctrl.enabled = 0;
188         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
189
190         hv_reenlightenment_cb = NULL;
191 }
192 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
193
194 /*
195  * This function is to be invoked early in the boot sequence after the
196  * hypervisor has been detected.
197  *
198  * 1. Setup the hypercall page.
199  * 2. Register Hyper-V specific clocksource.
200  */
201 void hyperv_init(void)
202 {
203         u64 guest_id, required_msrs;
204         union hv_x64_msr_hypercall_contents hypercall_msr;
205
206         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
207                 return;
208
209         /* Absolutely required MSRs */
210         required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
211                 HV_X64_MSR_VP_INDEX_AVAILABLE;
212
213         if ((ms_hyperv.features & required_msrs) != required_msrs)
214                 return;
215
216         /* Allocate percpu VP index */
217         hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
218                                     GFP_KERNEL);
219         if (!hv_vp_index)
220                 return;
221
222         if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
223                               hv_cpu_init, NULL) < 0)
224                 goto free_vp_index;
225
226         /*
227          * Setup the hypercall page and enable hypercalls.
228          * 1. Register the guest ID
229          * 2. Enable the hypercall and register the hypercall page
230          */
231         guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
232         wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
233
234         hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
235         if (hv_hypercall_pg == NULL) {
236                 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
237                 goto free_vp_index;
238         }
239
240         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
241         hypercall_msr.enable = 1;
242         hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
243         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
244
245         hyper_alloc_mmu();
246
247         /*
248          * Register Hyper-V specific clocksource.
249          */
250 #ifdef CONFIG_HYPERV_TSCPAGE
251         if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
252                 union hv_x64_msr_hypercall_contents tsc_msr;
253
254                 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
255                 if (!tsc_pg)
256                         goto register_msr_cs;
257
258                 hyperv_cs = &hyperv_cs_tsc;
259
260                 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
261
262                 tsc_msr.enable = 1;
263                 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
264
265                 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
266
267                 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
268
269                 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
270                 return;
271         }
272 register_msr_cs:
273 #endif
274         /*
275          * For 32 bit guests just use the MSR based mechanism for reading
276          * the partition counter.
277          */
278
279         hyperv_cs = &hyperv_cs_msr;
280         if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
281                 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
282
283         return;
284
285 free_vp_index:
286         kfree(hv_vp_index);
287         hv_vp_index = NULL;
288 }
289
290 /*
291  * This routine is called before kexec/kdump, it does the required cleanup.
292  */
293 void hyperv_cleanup(void)
294 {
295         union hv_x64_msr_hypercall_contents hypercall_msr;
296
297         /* Reset our OS id */
298         wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
299
300         /* Reset the hypercall page */
301         hypercall_msr.as_uint64 = 0;
302         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
303
304         /* Reset the TSC page */
305         hypercall_msr.as_uint64 = 0;
306         wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
307 }
308 EXPORT_SYMBOL_GPL(hyperv_cleanup);
309
310 void hyperv_report_panic(struct pt_regs *regs, long err)
311 {
312         static bool panic_reported;
313         u64 guest_id;
314
315         /*
316          * We prefer to report panic on 'die' chain as we have proper
317          * registers to report, but if we miss it (e.g. on BUG()) we need
318          * to report it on 'panic'.
319          */
320         if (panic_reported)
321                 return;
322         panic_reported = true;
323
324         rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
325
326         wrmsrl(HV_X64_MSR_CRASH_P0, err);
327         wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
328         wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
329         wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
330         wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
331
332         /*
333          * Let Hyper-V know there is crash data available
334          */
335         wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
336 }
337 EXPORT_SYMBOL_GPL(hyperv_report_panic);
338
339 bool hv_is_hypercall_page_setup(void)
340 {
341         union hv_x64_msr_hypercall_contents hypercall_msr;
342
343         /* Check if the hypercall page is setup */
344         hypercall_msr.as_uint64 = 0;
345         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
346
347         if (!hypercall_msr.enable)
348                 return false;
349
350         return true;
351 }
352 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);