GNU Linux-libre 4.14.259-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/hypervisor.h>
23 #include <asm/hyperv.h>
24 #include <asm/mshyperv.h>
25 #include <linux/version.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mm.h>
28 #include <linux/clockchips.h>
29 #include <linux/hyperv.h>
30 #include <linux/slab.h>
31 #include <linux/cpuhotplug.h>
32
33 #ifdef CONFIG_HYPERV_TSCPAGE
34
35 static struct ms_hyperv_tsc_page *tsc_pg;
36
37 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
38 {
39         return tsc_pg;
40 }
41
42 static u64 read_hv_clock_tsc(struct clocksource *arg)
43 {
44         u64 current_tick = hv_read_tsc_page(tsc_pg);
45
46         if (current_tick == U64_MAX)
47                 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
48
49         return current_tick;
50 }
51
52 static struct clocksource hyperv_cs_tsc = {
53                 .name           = "hyperv_clocksource_tsc_page",
54                 .rating         = 400,
55                 .read           = read_hv_clock_tsc,
56                 .mask           = CLOCKSOURCE_MASK(64),
57                 .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
58 };
59 #endif
60
61 static u64 read_hv_clock_msr(struct clocksource *arg)
62 {
63         u64 current_tick;
64         /*
65          * Read the partition counter to get the current tick count. This count
66          * is set to 0 when the partition is created and is incremented in
67          * 100 nanosecond units.
68          */
69         rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
70         return current_tick;
71 }
72
73 static struct clocksource hyperv_cs_msr = {
74         .name           = "hyperv_clocksource_msr",
75         .rating         = 400,
76         .read           = read_hv_clock_msr,
77         .mask           = CLOCKSOURCE_MASK(64),
78         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
79 };
80
81 void *hv_hypercall_pg;
82 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
83 struct clocksource *hyperv_cs;
84 EXPORT_SYMBOL_GPL(hyperv_cs);
85
86 u32 *hv_vp_index;
87 EXPORT_SYMBOL_GPL(hv_vp_index);
88
89 u32 hv_max_vp_index;
90
91 static int hv_cpu_init(unsigned int cpu)
92 {
93         u64 msr_vp_index;
94
95         hv_get_vp_index(msr_vp_index);
96
97         hv_vp_index[smp_processor_id()] = msr_vp_index;
98
99         if (msr_vp_index > hv_max_vp_index)
100                 hv_max_vp_index = msr_vp_index;
101
102         return 0;
103 }
104
105 static int __init hv_pci_init(void)
106 {
107         int gen2vm = efi_enabled(EFI_BOOT);
108
109         /*
110          * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
111          * The purpose is to suppress the harmless warning:
112          * "PCI: Fatal: No config space access function found"
113          */
114         if (gen2vm)
115                 return 0;
116
117         /* For Generation-1 VM, we'll proceed in pci_arch_init().  */
118         return 1;
119 }
120
121 /*
122  * This function is to be invoked early in the boot sequence after the
123  * hypervisor has been detected.
124  *
125  * 1. Setup the hypercall page.
126  * 2. Register Hyper-V specific clocksource.
127  */
128 void __init hyperv_init(void)
129 {
130         u64 guest_id, required_msrs;
131         union hv_x64_msr_hypercall_contents hypercall_msr;
132
133         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
134                 return;
135
136         /* Absolutely required MSRs */
137         required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
138                 HV_X64_MSR_VP_INDEX_AVAILABLE;
139
140         if ((ms_hyperv.features & required_msrs) != required_msrs)
141                 return;
142
143         /* Allocate percpu VP index */
144         hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
145                                     GFP_KERNEL);
146         if (!hv_vp_index)
147                 return;
148
149         if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
150                               hv_cpu_init, NULL) < 0)
151                 goto free_vp_index;
152
153         /*
154          * Setup the hypercall page and enable hypercalls.
155          * 1. Register the guest ID
156          * 2. Enable the hypercall and register the hypercall page
157          */
158         guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
159         wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
160
161         hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
162         if (hv_hypercall_pg == NULL) {
163                 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
164                 goto free_vp_index;
165         }
166
167         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
168         hypercall_msr.enable = 1;
169         hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
170         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
171
172         hyper_alloc_mmu();
173
174         x86_init.pci.arch_init = hv_pci_init;
175
176         /*
177          * Register Hyper-V specific clocksource.
178          */
179 #ifdef CONFIG_HYPERV_TSCPAGE
180         if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
181                 union hv_x64_msr_hypercall_contents tsc_msr;
182
183                 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
184                 if (!tsc_pg)
185                         goto register_msr_cs;
186
187                 hyperv_cs = &hyperv_cs_tsc;
188
189                 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
190
191                 tsc_msr.enable = 1;
192                 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
193
194                 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
195
196                 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
197
198                 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
199                 return;
200         }
201 register_msr_cs:
202 #endif
203         /*
204          * For 32 bit guests just use the MSR based mechanism for reading
205          * the partition counter.
206          */
207
208         hyperv_cs = &hyperv_cs_msr;
209         if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
210                 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
211
212         return;
213
214 free_vp_index:
215         kfree(hv_vp_index);
216         hv_vp_index = NULL;
217 }
218
219 /*
220  * This routine is called before kexec/kdump, it does the required cleanup.
221  */
222 void hyperv_cleanup(void)
223 {
224         union hv_x64_msr_hypercall_contents hypercall_msr;
225
226         /* Reset our OS id */
227         wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
228
229         /* Reset the hypercall page */
230         hypercall_msr.as_uint64 = 0;
231         wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
232
233         /* Reset the TSC page */
234         hypercall_msr.as_uint64 = 0;
235         wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
236 }
237 EXPORT_SYMBOL_GPL(hyperv_cleanup);
238
239 void hyperv_report_panic(struct pt_regs *regs)
240 {
241         static bool panic_reported;
242
243         /*
244          * We prefer to report panic on 'die' chain as we have proper
245          * registers to report, but if we miss it (e.g. on BUG()) we need
246          * to report it on 'panic'.
247          */
248         if (panic_reported)
249                 return;
250         panic_reported = true;
251
252         wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
253         wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
254         wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
255         wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
256         wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
257
258         /*
259          * Let Hyper-V know there is crash data available
260          */
261         wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
262 }
263 EXPORT_SYMBOL_GPL(hyperv_report_panic);
264
265 bool hv_is_hypercall_page_setup(void)
266 {
267         union hv_x64_msr_hypercall_contents hypercall_msr;
268
269         /* Check if the hypercall page is setup */
270         hypercall_msr.as_uint64 = 0;
271         rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
272
273         if (!hypercall_msr.enable)
274                 return false;
275
276         return true;
277 }
278 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);