GNU Linux-libre 4.19.264-gnu1
[releases.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/efi.h>
21 #include <linux/types.h>
22 #include <asm/apic.h>
23 #include <asm/desc.h>
24 #include <asm/hypervisor.h>
25 #include <asm/hyperv-tlfs.h>
26 #include <asm/mshyperv.h>
27 #include <linux/version.h>
28 #include <linux/vmalloc.h>
29 #include <linux/mm.h>
30 #include <linux/clockchips.h>
31 #include <linux/hyperv.h>
32 #include <linux/slab.h>
33 #include <linux/kernel.h>
34 #include <linux/cpuhotplug.h>
35
36 #ifdef CONFIG_HYPERV_TSCPAGE
37
38 static struct ms_hyperv_tsc_page *tsc_pg;
39
40 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
41 {
42         return tsc_pg;
43 }
44 EXPORT_SYMBOL_GPL(hv_get_tsc_page);
45
46 static u64 read_hv_clock_tsc(struct clocksource *arg)
47 {
48         u64 current_tick = hv_read_tsc_page(tsc_pg);
49
50         if (current_tick == U64_MAX)
51                 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
52
53         return current_tick;
54 }
55
56 static struct clocksource hyperv_cs_tsc = {
57                 .name           = "hyperv_clocksource_tsc_page",
58                 .rating         = 400,
59                 .read           = read_hv_clock_tsc,
60                 .mask           = CLOCKSOURCE_MASK(64),
61                 .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
62 };
63 #endif
64
65 static u64 read_hv_clock_msr(struct clocksource *arg)
66 {
67         u64 current_tick;
68         /*
69          * Read the partition counter to get the current tick count. This count
70          * is set to 0 when the partition is created and is incremented in
71          * 100 nanosecond units.
72          */
73         rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
74         return current_tick;
75 }
76
77 static struct clocksource hyperv_cs_msr = {
78         .name           = "hyperv_clocksource_msr",
79         .rating         = 400,
80         .read           = read_hv_clock_msr,
81         .mask           = CLOCKSOURCE_MASK(64),
82         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
83 };
84
85 void *hv_hypercall_pg;
86 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
87 struct clocksource *hyperv_cs;
88 EXPORT_SYMBOL_GPL(hyperv_cs);
89
90 u32 *hv_vp_index;
91 EXPORT_SYMBOL_GPL(hv_vp_index);
92
93 struct hv_vp_assist_page **hv_vp_assist_page;
94 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
95
96 void  __percpu **hyperv_pcpu_input_arg;
97 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
98
99 u32 hv_max_vp_index;
100
101 static int hv_cpu_init(unsigned int cpu)
102 {
103         u64 msr_vp_index;
104         struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
105         void **input_arg;
106         struct page *pg;
107
108         input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
109         pg = alloc_page(GFP_KERNEL);
110         if (unlikely(!pg))
111                 return -ENOMEM;
112         *input_arg = page_address(pg);
113
114         hv_get_vp_index(msr_vp_index);
115
116         hv_vp_index[smp_processor_id()] = msr_vp_index;
117
118         if (msr_vp_index > hv_max_vp_index)
119                 hv_max_vp_index = msr_vp_index;
120
121         if (!hv_vp_assist_page)
122                 return 0;
123
124         if (!*hvp)
125                 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
126
127         if (*hvp) {
128                 u64 val;
129
130                 val = vmalloc_to_pfn(*hvp);
131                 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
132                         HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
133
134                 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
135         }
136
137         return 0;
138 }
139
140 static void (*hv_reenlightenment_cb)(void);
141
142 static void hv_reenlightenment_notify(struct work_struct *dummy)
143 {
144         struct hv_tsc_emulation_status emu_status;
145
146         rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
147
148         /* Don't issue the callback if TSC accesses are not emulated */
149         if (hv_reenlightenment_cb && emu_status.inprogress)
150                 hv_reenlightenment_cb();
151 }
152 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
153
154 void hyperv_stop_tsc_emulation(void)
155 {
156         u64 freq;
157         struct hv_tsc_emulation_status emu_status;
158
159         rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
160         emu_status.inprogress = 0;
161         wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
162
163         rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
164         tsc_khz = div64_u64(freq, 1000);
165 }
166 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
167
168 static inline bool hv_reenlightenment_available(void)
169 {
170         /*
171          * Check for required features and priviliges to make TSC frequency
172          * change notifications work.
173          */
174         return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
175                 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
176                 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
177 }
178
179 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
180 {
181         entering_ack_irq();
182
183         inc_irq_stat(irq_hv_reenlightenment_count);
184
185         schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
186
187         exiting_irq();
188 }
189
190 void set_hv_tscchange_cb(void (*cb)(void))
191 {
192         struct hv_reenlightenment_control re_ctrl = {
193                 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
194                 .enabled = 1,
195         };
196         struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
197
198         if (!hv_reenlightenment_available()) {
199                 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
200                 return;
201         }
202
203         if (!hv_vp_index)
204                 return;
205
206         hv_reenlightenment_cb = cb;
207
208         /* Make sure callback is registered before we write to MSRs */
209         wmb();
210
211         re_ctrl.target_vp = hv_vp_index[get_cpu()];
212
213         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
214         wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
215
216         put_cpu();
217 }
218 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
219
220 void clear_hv_tscchange_cb(void)
221 {
222         struct hv_reenlightenment_control re_ctrl;
223
224         if (!hv_reenlightenment_available())
225                 return;
226
227         rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
228         re_ctrl.enabled = 0;
229         wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
230
231         hv_reenlightenment_cb = NULL;
232 }
233 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
234
235 static int hv_cpu_die(unsigned int cpu)
236 {
237         struct hv_reenlightenment_control re_ctrl;
238         unsigned int new_cpu;
239         unsigned long flags;
240         void **input_arg;
241         void *input_pg = NULL;
242
243         local_irq_save(flags);
244         input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
245         input_pg = *input_arg;
246         *input_arg = NULL;
247         local_irq_restore(flags);
248         free_page((unsigned long)input_pg);
249
250         if (hv_vp_assist_page && hv_vp_assist_page[cpu])
251                 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
252
253         if (hv_reenlightenment_cb == NULL)
254                 return 0;
255
256         rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
257         if (re_ctrl.target_vp == hv_vp_index[cpu]) {
258                 /* Reassign to some other online CPU */
259                 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
260
261                 re_ctrl.target_vp = hv_vp_index[new_cpu];
262                 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
263         }
264
265         return 0;
266 }
267
268 static int __init hv_pci_init(void)
269 {
270         int gen2vm = efi_enabled(EFI_BOOT);
271
272         /*
273          * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
274          * The purpose is to suppress the harmless warning:
275          * "PCI: Fatal: No config space access function found"
276          */
277         if (gen2vm)
278                 return 0;
279
280         /* For Generation-1 VM, we'll proceed in pci_arch_init().  */
281         return 1;
282 }
283
284 /*
285  * This function is to be invoked early in the boot sequence after the
286  * hypervisor has been detected.
287  *
288  * 1. Setup the hypercall page.
289  * 2. Register Hyper-V specific clocksource.
290  * 3. Setup Hyper-V specific APIC entry points.
291  */
292 void __init hyperv_init(void)
293 {
294         u64 guest_id, required_msrs;
295         union hv_x64_msr_hypercall_contents hypercall_msr;
296         int cpuhp, i;
297
298         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
299                 return;
300
301         /* Absolutely required MSRs */
302         required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
303                 HV_X64_MSR_VP_INDEX_AVAILABLE;
304
305         if ((ms_hyperv.features & required_msrs) != required_msrs)
306                 return;
307
308         /*
309          * Allocate the per-CPU state for the hypercall input arg.
310          * If this allocation fails, we will not be able to setup
311          * (per-CPU) hypercall input page and thus this failure is
312          * fatal on Hyper-V.
313          */
314         hyperv_pcpu_input_arg = alloc_percpu(void  *);
315
316         BUG_ON(hyperv_pcpu_input_arg == NULL);
317
318         /* Allocate percpu VP index */
319         hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
320                                     GFP_KERNEL);
321         if (!hv_vp_index)
322                 return;
323
324         for (i = 0; i < num_possible_cpus(); i++)
325                 hv_vp_index[i] = VP_INVAL;
326
327         hv_vp_assist_page = kcalloc(num_possible_cpus(),
328                                     sizeof(*hv_vp_assist_page), GFP_KERNEL);
329         if (!hv_vp_assist_page) {
330                 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
331                 goto free_vp_index;
332         }
333
334         cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
335                                   hv_cpu_init, hv_cpu_die);
336         if (cpuhp < 0)
337                 goto free_vp_assist_page;
338
339         /*
340          * Setup the hypercall page and enable hypercalls.
341          * 1. Register the guest ID
342          * 2. Enable the hypercall and register the hypercall page
343          */
344         guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
345         wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
346
347         hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
348         if (hv_hypercall_pg == NULL) {
349                 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
350                 goto remove_cpuhp_state;
351         }
352
353         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
354         hypercall_msr.enable = 1;
355         hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
356         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
357
358         hv_apic_init();
359
360         x86_init.pci.arch_init = hv_pci_init;
361
362         /*
363          * Register Hyper-V specific clocksource.
364          */
365 #ifdef CONFIG_HYPERV_TSCPAGE
366         if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
367                 union hv_x64_msr_hypercall_contents tsc_msr;
368
369                 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
370                 if (!tsc_pg)
371                         goto register_msr_cs;
372
373                 hyperv_cs = &hyperv_cs_tsc;
374
375                 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
376
377                 tsc_msr.enable = 1;
378                 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
379
380                 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
381
382                 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
383
384                 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
385                 return;
386         }
387 register_msr_cs:
388 #endif
389         /*
390          * For 32 bit guests just use the MSR based mechanism for reading
391          * the partition counter.
392          */
393
394         hyperv_cs = &hyperv_cs_msr;
395         if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
396                 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
397
398         return;
399
400 remove_cpuhp_state:
401         cpuhp_remove_state(cpuhp);
402 free_vp_assist_page:
403         kfree(hv_vp_assist_page);
404         hv_vp_assist_page = NULL;
405 free_vp_index:
406         kfree(hv_vp_index);
407         hv_vp_index = NULL;
408 }
409
410 /*
411  * This routine is called before kexec/kdump, it does the required cleanup.
412  */
413 void hyperv_cleanup(void)
414 {
415         union hv_x64_msr_hypercall_contents hypercall_msr;
416
417         /* Reset our OS id */
418         wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
419
420         /*
421          * Reset hypercall page reference before reset the page,
422          * let hypercall operations fail safely rather than
423          * panic the kernel for using invalid hypercall page
424          */
425         hv_hypercall_pg = NULL;
426
427         /* Reset the hypercall page */
428         hypercall_msr.as_uint64 = 0;
429         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
430
431         /* Reset the TSC page */
432         hypercall_msr.as_uint64 = 0;
433         wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
434 }
435 EXPORT_SYMBOL_GPL(hyperv_cleanup);
436
437 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
438 {
439         static bool panic_reported;
440         u64 guest_id;
441
442         if (in_die && !panic_on_oops)
443                 return;
444
445         /*
446          * We prefer to report panic on 'die' chain as we have proper
447          * registers to report, but if we miss it (e.g. on BUG()) we need
448          * to report it on 'panic'.
449          */
450         if (panic_reported)
451                 return;
452         panic_reported = true;
453
454         rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
455
456         wrmsrl(HV_X64_MSR_CRASH_P0, err);
457         wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
458         wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
459         wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
460         wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
461
462         /*
463          * Let Hyper-V know there is crash data available
464          */
465         wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
466 }
467 EXPORT_SYMBOL_GPL(hyperv_report_panic);
468
469 /**
470  * hyperv_report_panic_msg - report panic message to Hyper-V
471  * @pa: physical address of the panic page containing the message
472  * @size: size of the message in the page
473  */
474 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
475 {
476         /*
477          * P3 to contain the physical address of the panic page & P4 to
478          * contain the size of the panic data in that page. Rest of the
479          * registers are no-op when the NOTIFY_MSG flag is set.
480          */
481         wrmsrl(HV_X64_MSR_CRASH_P0, 0);
482         wrmsrl(HV_X64_MSR_CRASH_P1, 0);
483         wrmsrl(HV_X64_MSR_CRASH_P2, 0);
484         wrmsrl(HV_X64_MSR_CRASH_P3, pa);
485         wrmsrl(HV_X64_MSR_CRASH_P4, size);
486
487         /*
488          * Let Hyper-V know there is crash data available along with
489          * the panic message.
490          */
491         wrmsrl(HV_X64_MSR_CRASH_CTL,
492                (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
493 }
494 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
495
496 bool hv_is_hyperv_initialized(void)
497 {
498         union hv_x64_msr_hypercall_contents hypercall_msr;
499
500         /*
501          * Ensure that we're really on Hyper-V, and not a KVM or Xen
502          * emulation of Hyper-V
503          */
504         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
505                 return false;
506
507         /*
508          * Verify that earlier initialization succeeded by checking
509          * that the hypercall page is setup
510          */
511         hypercall_msr.as_uint64 = 0;
512         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
513
514         return hypercall_msr.enable;
515 }
516 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);