blob: 691603ee91792fe474f8c9a83274c9394179f579 [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>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070029
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010030#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070031
32static struct ms_hyperv_tsc_page *tsc_pg;
33
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010034struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
35{
36 return tsc_pg;
37}
38
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070039static u64 read_hv_clock_tsc(struct clocksource *arg)
40{
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010041 u64 current_tick = hv_read_tsc_page(tsc_pg);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070042
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010043 if (current_tick == U64_MAX)
44 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070045
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070046 return current_tick;
47}
48
49static struct clocksource hyperv_cs_tsc = {
50 .name = "hyperv_clocksource_tsc_page",
51 .rating = 400,
52 .read = read_hv_clock_tsc,
53 .mask = CLOCKSOURCE_MASK(64),
54 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
55};
56#endif
57
58static u64 read_hv_clock_msr(struct clocksource *arg)
59{
60 u64 current_tick;
61 /*
62 * Read the partition counter to get the current tick count. This count
63 * is set to 0 when the partition is created and is incremented in
64 * 100 nanosecond units.
65 */
66 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
67 return current_tick;
68}
69
70static struct clocksource hyperv_cs_msr = {
71 .name = "hyperv_clocksource_msr",
72 .rating = 400,
73 .read = read_hv_clock_msr,
74 .mask = CLOCKSOURCE_MASK(64),
75 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
76};
K. Y. Srinivasan87300462017-01-18 16:45:02 -070077
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020078void *hv_hypercall_pg;
79EXPORT_SYMBOL_GPL(hv_hypercall_pg);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070080struct clocksource *hyperv_cs;
81EXPORT_SYMBOL_GPL(hyperv_cs);
82
K. Y. Srinivasan87300462017-01-18 16:45:02 -070083/*
84 * This function is to be invoked early in the boot sequence after the
85 * hypervisor has been detected.
86 *
87 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070088 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan87300462017-01-18 16:45:02 -070089 */
90void hyperv_init(void)
91{
92 u64 guest_id;
93 union hv_x64_msr_hypercall_contents hypercall_msr;
94
95 if (x86_hyper != &x86_hyper_ms_hyperv)
96 return;
97
98 /*
99 * Setup the hypercall page and enable hypercalls.
100 * 1. Register the guest ID
101 * 2. Enable the hypercall and register the hypercall page
102 */
103 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
104 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
105
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200106 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
107 if (hv_hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700108 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
109 return;
110 }
111
112 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
113 hypercall_msr.enable = 1;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200114 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700115 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700116
117 /*
118 * Register Hyper-V specific clocksource.
119 */
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100120#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700121 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
122 union hv_x64_msr_hypercall_contents tsc_msr;
123
124 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700125 if (!tsc_pg)
126 goto register_msr_cs;
127
128 hyperv_cs = &hyperv_cs_tsc;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700129
130 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
131
132 tsc_msr.enable = 1;
133 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
134
135 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
Vitaly Kuznetsov90b20432017-03-03 14:21:42 +0100136
137 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
138
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700139 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
140 return;
141 }
Arnd Bergmann73667e32017-02-14 22:17:17 +0100142register_msr_cs:
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700143#endif
144 /*
145 * For 32 bit guests just use the MSR based mechanism for reading
146 * the partition counter.
147 */
148
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700149 hyperv_cs = &hyperv_cs_msr;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700150 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
151 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700152}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700153
154/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700155 * This routine is called before kexec/kdump, it does the required cleanup.
156 */
157void hyperv_cleanup(void)
158{
159 union hv_x64_msr_hypercall_contents hypercall_msr;
160
161 /* Reset our OS id */
162 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
163
164 /* Reset the hypercall page */
165 hypercall_msr.as_uint64 = 0;
166 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Vitaly Kuznetsov5647dbf2017-01-28 12:37:15 -0700167
168 /* Reset the TSC page */
169 hypercall_msr.as_uint64 = 0;
170 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700171}
172EXPORT_SYMBOL_GPL(hyperv_cleanup);
173
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700174void hyperv_report_panic(struct pt_regs *regs)
175{
176 static bool panic_reported;
177
178 /*
179 * We prefer to report panic on 'die' chain as we have proper
180 * registers to report, but if we miss it (e.g. on BUG()) we need
181 * to report it on 'panic'.
182 */
183 if (panic_reported)
184 return;
185 panic_reported = true;
186
187 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
188 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
189 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
190 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
191 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
192
193 /*
194 * Let Hyper-V know there is crash data available
195 */
196 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
197}
198EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700199
200bool hv_is_hypercall_page_setup(void)
201{
202 union hv_x64_msr_hypercall_contents hypercall_msr;
203
204 /* Check if the hypercall page is setup */
205 hypercall_msr.as_uint64 = 0;
206 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
207
208 if (!hypercall_msr.enable)
209 return false;
210
211 return true;
212}
213EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);