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