K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 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> |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 27 | #include <linux/clockchips.h> |
Stephen Hemminger | 6707181 | 2017-03-04 18:27:11 -0700 | [diff] [blame] | 28 | #include <linux/hyperv.h> |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 29 | #include <linux/slab.h> |
| 30 | #include <linux/cpuhotplug.h> |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 31 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 32 | #ifdef CONFIG_HYPERV_TSCPAGE |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 33 | |
| 34 | static struct ms_hyperv_tsc_page *tsc_pg; |
| 35 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 36 | struct ms_hyperv_tsc_page *hv_get_tsc_page(void) |
| 37 | { |
| 38 | return tsc_pg; |
| 39 | } |
Vitaly Kuznetsov | e2768ea | 2018-01-24 14:23:32 +0100 | [diff] [blame^] | 40 | EXPORT_SYMBOL_GPL(hv_get_tsc_page); |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 41 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 42 | static u64 read_hv_clock_tsc(struct clocksource *arg) |
| 43 | { |
Vitaly Kuznetsov | 0733379 | 2017-03-03 14:21:41 +0100 | [diff] [blame] | 44 | u64 current_tick = hv_read_tsc_page(tsc_pg); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 45 | |
Vitaly Kuznetsov | 0733379 | 2017-03-03 14:21:41 +0100 | [diff] [blame] | 46 | if (current_tick == U64_MAX) |
| 47 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 48 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 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 | }; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 80 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 81 | void *hv_hypercall_pg; |
| 82 | EXPORT_SYMBOL_GPL(hv_hypercall_pg); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 83 | struct clocksource *hyperv_cs; |
| 84 | EXPORT_SYMBOL_GPL(hyperv_cs); |
| 85 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 86 | u32 *hv_vp_index; |
| 87 | EXPORT_SYMBOL_GPL(hv_vp_index); |
| 88 | |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 89 | u32 hv_max_vp_index; |
| 90 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 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 | |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 99 | if (msr_vp_index > hv_max_vp_index) |
| 100 | hv_max_vp_index = msr_vp_index; |
| 101 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 105 | /* |
| 106 | * This function is to be invoked early in the boot sequence after the |
| 107 | * hypervisor has been detected. |
| 108 | * |
| 109 | * 1. Setup the hypercall page. |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 110 | * 2. Register Hyper-V specific clocksource. |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 111 | */ |
| 112 | void hyperv_init(void) |
| 113 | { |
Vitaly Kuznetsov | 89a8f6d | 2018-01-24 14:23:31 +0100 | [diff] [blame] | 114 | u64 guest_id, required_msrs; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 115 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 116 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 117 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 118 | return; |
| 119 | |
Vitaly Kuznetsov | 89a8f6d | 2018-01-24 14:23:31 +0100 | [diff] [blame] | 120 | /* Absolutely required MSRs */ |
| 121 | required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE | |
| 122 | HV_X64_MSR_VP_INDEX_AVAILABLE; |
| 123 | |
| 124 | if ((ms_hyperv.features & required_msrs) != required_msrs) |
| 125 | return; |
| 126 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 127 | /* Allocate percpu VP index */ |
| 128 | hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), |
| 129 | GFP_KERNEL); |
| 130 | if (!hv_vp_index) |
| 131 | return; |
| 132 | |
| 133 | if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", |
| 134 | hv_cpu_init, NULL) < 0) |
| 135 | goto free_vp_index; |
| 136 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 137 | /* |
| 138 | * Setup the hypercall page and enable hypercalls. |
| 139 | * 1. Register the guest ID |
| 140 | * 2. Enable the hypercall and register the hypercall page |
| 141 | */ |
| 142 | guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 143 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 144 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 145 | hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX); |
| 146 | if (hv_hypercall_pg == NULL) { |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 147 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 148 | goto free_vp_index; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 152 | hypercall_msr.enable = 1; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 153 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 154 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 155 | |
Vitaly Kuznetsov | 2ffd9e3 | 2017-08-02 18:09:19 +0200 | [diff] [blame] | 156 | hyper_alloc_mmu(); |
| 157 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 158 | /* |
| 159 | * Register Hyper-V specific clocksource. |
| 160 | */ |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_HYPERV_TSCPAGE |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 162 | if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) { |
| 163 | union hv_x64_msr_hypercall_contents tsc_msr; |
| 164 | |
| 165 | tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 166 | if (!tsc_pg) |
| 167 | goto register_msr_cs; |
| 168 | |
| 169 | hyperv_cs = &hyperv_cs_tsc; |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 170 | |
| 171 | rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
| 172 | |
| 173 | tsc_msr.enable = 1; |
| 174 | tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg); |
| 175 | |
| 176 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
Vitaly Kuznetsov | 90b2043 | 2017-03-03 14:21:42 +0100 | [diff] [blame] | 177 | |
| 178 | hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK; |
| 179 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 180 | clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100); |
| 181 | return; |
| 182 | } |
Arnd Bergmann | 73667e3 | 2017-02-14 22:17:17 +0100 | [diff] [blame] | 183 | register_msr_cs: |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 184 | #endif |
| 185 | /* |
| 186 | * For 32 bit guests just use the MSR based mechanism for reading |
| 187 | * the partition counter. |
| 188 | */ |
| 189 | |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 190 | hyperv_cs = &hyperv_cs_msr; |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 191 | if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE) |
| 192 | clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 193 | |
| 194 | return; |
| 195 | |
| 196 | free_vp_index: |
| 197 | kfree(hv_vp_index); |
| 198 | hv_vp_index = NULL; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 199 | } |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 200 | |
| 201 | /* |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 202 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 203 | */ |
| 204 | void hyperv_cleanup(void) |
| 205 | { |
| 206 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 207 | |
| 208 | /* Reset our OS id */ |
| 209 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 210 | |
| 211 | /* Reset the hypercall page */ |
| 212 | hypercall_msr.as_uint64 = 0; |
| 213 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | 5647dbf | 2017-01-28 12:37:15 -0700 | [diff] [blame] | 214 | |
| 215 | /* Reset the TSC page */ |
| 216 | hypercall_msr.as_uint64 = 0; |
| 217 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 218 | } |
| 219 | EXPORT_SYMBOL_GPL(hyperv_cleanup); |
| 220 | |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 221 | void hyperv_report_panic(struct pt_regs *regs, long err) |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 222 | { |
| 223 | static bool panic_reported; |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 224 | u64 guest_id; |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * We prefer to report panic on 'die' chain as we have proper |
| 228 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 229 | * to report it on 'panic'. |
| 230 | */ |
| 231 | if (panic_reported) |
| 232 | return; |
| 233 | panic_reported = true; |
| 234 | |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 235 | rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 236 | |
| 237 | wrmsrl(HV_X64_MSR_CRASH_P0, err); |
| 238 | wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); |
| 239 | wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); |
| 240 | wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); |
| 241 | wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * Let Hyper-V know there is crash data available |
| 245 | */ |
| 246 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 249 | |
| 250 | bool hv_is_hypercall_page_setup(void) |
| 251 | { |
| 252 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 253 | |
| 254 | /* Check if the hypercall page is setup */ |
| 255 | hypercall_msr.as_uint64 = 0; |
| 256 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 257 | |
| 258 | if (!hypercall_msr.enable) |
| 259 | return false; |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup); |