blob: aeb8edf32e2babe606dd8db29147b093baa9c355 [file] [log] [blame]
K. Y. Srinivasan87300462017-01-18 16:45:02 -07001/*
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>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070027#include <linux/clockchips.h>
Stephen Hemminger67071812017-03-04 18:27:11 -070028#include <linux/hyperv.h>
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020029#include <linux/slab.h>
30#include <linux/cpuhotplug.h>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070031
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010032#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070033
34static struct ms_hyperv_tsc_page *tsc_pg;
35
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010036struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
37{
38 return tsc_pg;
39}
40
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070041static u64 read_hv_clock_tsc(struct clocksource *arg)
42{
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010043 u64 current_tick = hv_read_tsc_page(tsc_pg);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070044
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010045 if (current_tick == U64_MAX)
46 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070047
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070048 return current_tick;
49}
50
51static 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
60static 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
72static 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};
K. Y. Srinivasan87300462017-01-18 16:45:02 -070079
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020080void *hv_hypercall_pg;
81EXPORT_SYMBOL_GPL(hv_hypercall_pg);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070082struct clocksource *hyperv_cs;
83EXPORT_SYMBOL_GPL(hyperv_cs);
84
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020085u32 *hv_vp_index;
86EXPORT_SYMBOL_GPL(hv_vp_index);
87
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +020088u32 hv_max_vp_index;
89
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020090static int hv_cpu_init(unsigned int cpu)
91{
92 u64 msr_vp_index;
93
94 hv_get_vp_index(msr_vp_index);
95
96 hv_vp_index[smp_processor_id()] = msr_vp_index;
97
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +020098 if (msr_vp_index > hv_max_vp_index)
99 hv_max_vp_index = msr_vp_index;
100
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200101 return 0;
102}
103
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700104/*
105 * This function is to be invoked early in the boot sequence after the
106 * hypervisor has been detected.
107 *
108 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700109 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700110 */
111void hyperv_init(void)
112{
113 u64 guest_id;
114 union hv_x64_msr_hypercall_contents hypercall_msr;
115
116 if (x86_hyper != &x86_hyper_ms_hyperv)
117 return;
118
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200119 /* Allocate percpu VP index */
120 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
121 GFP_KERNEL);
122 if (!hv_vp_index)
123 return;
124
125 if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
126 hv_cpu_init, NULL) < 0)
127 goto free_vp_index;
128
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700129 /*
130 * Setup the hypercall page and enable hypercalls.
131 * 1. Register the guest ID
132 * 2. Enable the hypercall and register the hypercall page
133 */
134 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
135 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
136
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200137 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
138 if (hv_hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700139 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200140 goto free_vp_index;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700141 }
142
143 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
144 hypercall_msr.enable = 1;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200145 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700146 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700147
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200148 hyper_alloc_mmu();
149
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700150 /*
151 * Register Hyper-V specific clocksource.
152 */
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100153#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700154 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
155 union hv_x64_msr_hypercall_contents tsc_msr;
156
157 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700158 if (!tsc_pg)
159 goto register_msr_cs;
160
161 hyperv_cs = &hyperv_cs_tsc;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700162
163 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
164
165 tsc_msr.enable = 1;
166 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
167
168 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
Vitaly Kuznetsov90b20432017-03-03 14:21:42 +0100169
170 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
171
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700172 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
173 return;
174 }
Arnd Bergmann73667e32017-02-14 22:17:17 +0100175register_msr_cs:
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700176#endif
177 /*
178 * For 32 bit guests just use the MSR based mechanism for reading
179 * the partition counter.
180 */
181
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700182 hyperv_cs = &hyperv_cs_msr;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700183 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
184 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200185
186 return;
187
188free_vp_index:
189 kfree(hv_vp_index);
190 hv_vp_index = NULL;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700191}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700192
193/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700194 * This routine is called before kexec/kdump, it does the required cleanup.
195 */
196void hyperv_cleanup(void)
197{
198 union hv_x64_msr_hypercall_contents hypercall_msr;
199
200 /* Reset our OS id */
201 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
202
203 /* Reset the hypercall page */
204 hypercall_msr.as_uint64 = 0;
205 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Vitaly Kuznetsov5647dbf2017-01-28 12:37:15 -0700206
207 /* Reset the TSC page */
208 hypercall_msr.as_uint64 = 0;
209 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700210}
211EXPORT_SYMBOL_GPL(hyperv_cleanup);
212
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700213void hyperv_report_panic(struct pt_regs *regs, long err)
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700214{
215 static bool panic_reported;
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700216 u64 guest_id;
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700217
218 /*
219 * We prefer to report panic on 'die' chain as we have proper
220 * registers to report, but if we miss it (e.g. on BUG()) we need
221 * to report it on 'panic'.
222 */
223 if (panic_reported)
224 return;
225 panic_reported = true;
226
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700227 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
228
229 wrmsrl(HV_X64_MSR_CRASH_P0, err);
230 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
231 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
232 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
233 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700234
235 /*
236 * Let Hyper-V know there is crash data available
237 */
238 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
239}
240EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700241
242bool hv_is_hypercall_page_setup(void)
243{
244 union hv_x64_msr_hypercall_contents hypercall_msr;
245
246 /* Check if the hypercall page is setup */
247 hypercall_msr.as_uint64 = 0;
248 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
249
250 if (!hypercall_msr.enable)
251 return false;
252
253 return true;
254}
255EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);