X86/Hyper-V: Enlighten APIC access
[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-tlfs.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 struct hv_vp_assist_page **hv_vp_assist_page;
92 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
93
94 u32 hv_max_vp_index;
95
96 static int hv_cpu_init(unsigned int cpu)
97 {
98         u64 msr_vp_index;
99         struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
100
101         hv_get_vp_index(msr_vp_index);
102
103         hv_vp_index[smp_processor_id()] = msr_vp_index;
104
105         if (msr_vp_index > hv_max_vp_index)
106                 hv_max_vp_index = msr_vp_index;
107
108         if (!hv_vp_assist_page)
109                 return 0;
110
111         if (!*hvp)
112                 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
113
114         if (*hvp) {
115                 u64 val;
116
117                 val = vmalloc_to_pfn(*hvp);
118                 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
119                         HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
120
121                 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
122         }
123
124         return 0;
125 }
126
127 static void (*hv_reenlightenment_cb)(void);
128
129 static void hv_reenlightenment_notify(struct work_struct *dummy)
130 {
131         struct hv_tsc_emulation_status emu_status;
132
133         rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
134
135         /* Don't issue the callback if TSC accesses are not emulated */
136         if (hv_reenlightenment_cb && emu_status.inprogress)
137                 hv_reenlightenment_cb();
138 }
139 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
140
141 void hyperv_stop_tsc_emulation(void)
142 {
143         u64 freq;
144         struct hv_tsc_emulation_status emu_status;
145
146         rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
147         emu_status.inprogress = 0;
148         wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
149
150         rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
151         tsc_khz = div64_u64(freq, 1000);
152 }
153 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
154
155 static inline bool hv_reenlightenment_available(void)
156 {
157         /*
158          * Check for required features and priviliges to make TSC frequency
159          * change notifications work.
160          */
161         return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
162                 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
163                 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
164 }
165
166 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
167 {
168         entering_ack_irq();
169
170         inc_irq_stat(irq_hv_reenlightenment_count);
171
172         schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
173
174         exiting_irq();
175 }
176
177 void set_hv_tscchange_cb(void (*cb)(void))
178 {
179         struct hv_reenlightenment_control re_ctrl = {
180                 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
181                 .enabled = 1,
182                 .target_vp = hv_vp_index[smp_processor_id()]
183         };
184         struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
185
186         if (!hv_reenlightenment_available()) {
187                 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
188                 return;
189         }
190
191         hv_reenlightenment_cb = cb;
192
193         /* Make sure callback is registered before we write to MSRs */
194         wmb();
195
196         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
197         wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
198 }
199 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
200
201 void clear_hv_tscchange_cb(void)
202 {
203         struct hv_reenlightenment_control re_ctrl;
204
205         if (!hv_reenlightenment_available())
206                 return;
207
208         rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
209         re_ctrl.enabled = 0;
210         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
211
212         hv_reenlightenment_cb = NULL;
213 }
214 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
215
216 static int hv_cpu_die(unsigned int cpu)
217 {
218         struct hv_reenlightenment_control re_ctrl;
219         unsigned int new_cpu;
220
221         if (hv_vp_assist_page && hv_vp_assist_page[cpu])
222                 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
223
224         if (hv_reenlightenment_cb == NULL)
225                 return 0;
226
227         rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
228         if (re_ctrl.target_vp == hv_vp_index[cpu]) {
229                 /* Reassign to some other online CPU */
230                 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
231
232                 re_ctrl.target_vp = hv_vp_index[new_cpu];
233                 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
234         }
235
236         return 0;
237 }
238
239 /*
240  * This function is to be invoked early in the boot sequence after the
241  * hypervisor has been detected.
242  *
243  * 1. Setup the hypercall page.
244  * 2. Register Hyper-V specific clocksource.
245  * 3. Setup Hyper-V specific APIC entry points.
246  */
247 void __init hyperv_init(void)
248 {
249         u64 guest_id, required_msrs;
250         union hv_x64_msr_hypercall_contents hypercall_msr;
251         int cpuhp;
252
253         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
254                 return;
255
256         /* Absolutely required MSRs */
257         required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
258                 HV_X64_MSR_VP_INDEX_AVAILABLE;
259
260         if ((ms_hyperv.features & required_msrs) != required_msrs)
261                 return;
262
263         /* Allocate percpu VP index */
264         hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
265                                     GFP_KERNEL);
266         if (!hv_vp_index)
267                 return;
268
269         hv_vp_assist_page = kcalloc(num_possible_cpus(),
270                                     sizeof(*hv_vp_assist_page), GFP_KERNEL);
271         if (!hv_vp_assist_page) {
272                 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
273                 goto free_vp_index;
274         }
275
276         cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
277                                   hv_cpu_init, hv_cpu_die);
278         if (cpuhp < 0)
279                 goto free_vp_assist_page;
280
281         /*
282          * Setup the hypercall page and enable hypercalls.
283          * 1. Register the guest ID
284          * 2. Enable the hypercall and register the hypercall page
285          */
286         guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
287         wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
288
289         hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
290         if (hv_hypercall_pg == NULL) {
291                 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
292                 goto remove_cpuhp_state;
293         }
294
295         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
296         hypercall_msr.enable = 1;
297         hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
298         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
299
300         hyper_alloc_mmu();
301
302         hv_apic_init();
303
304         /*
305          * Register Hyper-V specific clocksource.
306          */
307 #ifdef CONFIG_HYPERV_TSCPAGE
308         if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
309                 union hv_x64_msr_hypercall_contents tsc_msr;
310
311                 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
312                 if (!tsc_pg)
313                         goto register_msr_cs;
314
315                 hyperv_cs = &hyperv_cs_tsc;
316
317                 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
318
319                 tsc_msr.enable = 1;
320                 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
321
322                 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
323
324                 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
325
326                 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
327                 return;
328         }
329 register_msr_cs:
330 #endif
331         /*
332          * For 32 bit guests just use the MSR based mechanism for reading
333          * the partition counter.
334          */
335
336         hyperv_cs = &hyperv_cs_msr;
337         if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
338                 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
339
340         return;
341
342 remove_cpuhp_state:
343         cpuhp_remove_state(cpuhp);
344 free_vp_assist_page:
345         kfree(hv_vp_assist_page);
346         hv_vp_assist_page = NULL;
347 free_vp_index:
348         kfree(hv_vp_index);
349         hv_vp_index = NULL;
350 }
351
352 /*
353  * This routine is called before kexec/kdump, it does the required cleanup.
354  */
355 void hyperv_cleanup(void)
356 {
357         union hv_x64_msr_hypercall_contents hypercall_msr;
358
359         /* Reset our OS id */
360         wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
361
362         /* Reset the hypercall page */
363         hypercall_msr.as_uint64 = 0;
364         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
365
366         /* Reset the TSC page */
367         hypercall_msr.as_uint64 = 0;
368         wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
369 }
370 EXPORT_SYMBOL_GPL(hyperv_cleanup);
371
372 void hyperv_report_panic(struct pt_regs *regs, long err)
373 {
374         static bool panic_reported;
375         u64 guest_id;
376
377         /*
378          * We prefer to report panic on 'die' chain as we have proper
379          * registers to report, but if we miss it (e.g. on BUG()) we need
380          * to report it on 'panic'.
381          */
382         if (panic_reported)
383                 return;
384         panic_reported = true;
385
386         rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
387
388         wrmsrl(HV_X64_MSR_CRASH_P0, err);
389         wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
390         wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
391         wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
392         wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
393
394         /*
395          * Let Hyper-V know there is crash data available
396          */
397         wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
398 }
399 EXPORT_SYMBOL_GPL(hyperv_report_panic);
400
401 bool hv_is_hyperv_initialized(void)
402 {
403         union hv_x64_msr_hypercall_contents hypercall_msr;
404
405         /*
406          * Ensure that we're really on Hyper-V, and not a KVM or Xen
407          * emulation of Hyper-V
408          */
409         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
410                 return false;
411
412         /*
413          * Verify that earlier initialization succeeded by checking
414          * that the hypercall page is setup
415          */
416         hypercall_msr.as_uint64 = 0;
417         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
418
419         return hypercall_msr.enable;
420 }
421 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);