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> |
| 28 | |
| 29 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame^] | 30 | #ifdef CONFIG_HYPERV_TSCPAGE |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 31 | |
| 32 | static struct ms_hyperv_tsc_page *tsc_pg; |
| 33 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame^] | 34 | struct ms_hyperv_tsc_page *hv_get_tsc_page(void) |
| 35 | { |
| 36 | return tsc_pg; |
| 37 | } |
| 38 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 39 | static u64 read_hv_clock_tsc(struct clocksource *arg) |
| 40 | { |
| 41 | u64 current_tick; |
| 42 | |
| 43 | if (tsc_pg->tsc_sequence != 0) { |
| 44 | /* |
| 45 | * Use the tsc page to compute the value. |
| 46 | */ |
| 47 | |
| 48 | while (1) { |
| 49 | u64 tmp; |
| 50 | u32 sequence = tsc_pg->tsc_sequence; |
| 51 | u64 cur_tsc; |
| 52 | u64 scale = tsc_pg->tsc_scale; |
| 53 | s64 offset = tsc_pg->tsc_offset; |
| 54 | |
| 55 | rdtscll(cur_tsc); |
| 56 | /* current_tick = ((cur_tsc *scale) >> 64) + offset */ |
| 57 | asm("mulq %3" |
| 58 | : "=d" (current_tick), "=a" (tmp) |
| 59 | : "a" (cur_tsc), "r" (scale)); |
| 60 | |
| 61 | current_tick += offset; |
| 62 | if (tsc_pg->tsc_sequence == sequence) |
| 63 | return current_tick; |
| 64 | |
| 65 | if (tsc_pg->tsc_sequence != 0) |
| 66 | continue; |
| 67 | /* |
| 68 | * Fallback using MSR method. |
| 69 | */ |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); |
| 74 | return current_tick; |
| 75 | } |
| 76 | |
| 77 | static struct clocksource hyperv_cs_tsc = { |
| 78 | .name = "hyperv_clocksource_tsc_page", |
| 79 | .rating = 400, |
| 80 | .read = read_hv_clock_tsc, |
| 81 | .mask = CLOCKSOURCE_MASK(64), |
| 82 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 83 | }; |
| 84 | #endif |
| 85 | |
| 86 | static u64 read_hv_clock_msr(struct clocksource *arg) |
| 87 | { |
| 88 | u64 current_tick; |
| 89 | /* |
| 90 | * Read the partition counter to get the current tick count. This count |
| 91 | * is set to 0 when the partition is created and is incremented in |
| 92 | * 100 nanosecond units. |
| 93 | */ |
| 94 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); |
| 95 | return current_tick; |
| 96 | } |
| 97 | |
| 98 | static struct clocksource hyperv_cs_msr = { |
| 99 | .name = "hyperv_clocksource_msr", |
| 100 | .rating = 400, |
| 101 | .read = read_hv_clock_msr, |
| 102 | .mask = CLOCKSOURCE_MASK(64), |
| 103 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 104 | }; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 105 | |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 106 | static void *hypercall_pg; |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 107 | struct clocksource *hyperv_cs; |
| 108 | EXPORT_SYMBOL_GPL(hyperv_cs); |
| 109 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 110 | /* |
| 111 | * This function is to be invoked early in the boot sequence after the |
| 112 | * hypervisor has been detected. |
| 113 | * |
| 114 | * 1. Setup the hypercall page. |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 115 | * 2. Register Hyper-V specific clocksource. |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 116 | */ |
| 117 | void hyperv_init(void) |
| 118 | { |
| 119 | u64 guest_id; |
| 120 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 121 | |
| 122 | if (x86_hyper != &x86_hyper_ms_hyperv) |
| 123 | return; |
| 124 | |
| 125 | /* |
| 126 | * Setup the hypercall page and enable hypercalls. |
| 127 | * 1. Register the guest ID |
| 128 | * 2. Enable the hypercall and register the hypercall page |
| 129 | */ |
| 130 | guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 131 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 132 | |
K. Y. Srinivasan | 372b1e9 | 2017-02-08 18:30:56 -0700 | [diff] [blame] | 133 | hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX); |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 134 | if (hypercall_pg == NULL) { |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 135 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 140 | hypercall_msr.enable = 1; |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 141 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 142 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Register Hyper-V specific clocksource. |
| 146 | */ |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame^] | 147 | #ifdef CONFIG_HYPERV_TSCPAGE |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 148 | if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) { |
| 149 | union hv_x64_msr_hypercall_contents tsc_msr; |
| 150 | |
| 151 | tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 152 | if (!tsc_pg) |
| 153 | goto register_msr_cs; |
| 154 | |
| 155 | hyperv_cs = &hyperv_cs_tsc; |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 156 | |
| 157 | rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
| 158 | |
| 159 | tsc_msr.enable = 1; |
| 160 | tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg); |
| 161 | |
| 162 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
| 163 | clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100); |
| 164 | return; |
| 165 | } |
Arnd Bergmann | 73667e3 | 2017-02-14 22:17:17 +0100 | [diff] [blame] | 166 | register_msr_cs: |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 167 | #endif |
| 168 | /* |
| 169 | * For 32 bit guests just use the MSR based mechanism for reading |
| 170 | * the partition counter. |
| 171 | */ |
| 172 | |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 173 | hyperv_cs = &hyperv_cs_msr; |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 174 | if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE) |
| 175 | clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 176 | } |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 179 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 180 | */ |
| 181 | void hyperv_cleanup(void) |
| 182 | { |
| 183 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 184 | |
| 185 | /* Reset our OS id */ |
| 186 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 187 | |
| 188 | /* Reset the hypercall page */ |
| 189 | hypercall_msr.as_uint64 = 0; |
| 190 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | 5647dbf | 2017-01-28 12:37:15 -0700 | [diff] [blame] | 191 | |
| 192 | /* Reset the TSC page */ |
| 193 | hypercall_msr.as_uint64 = 0; |
| 194 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 195 | } |
| 196 | EXPORT_SYMBOL_GPL(hyperv_cleanup); |
| 197 | |
| 198 | /* |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 199 | * hv_do_hypercall- Invoke the specified hypercall |
| 200 | */ |
| 201 | u64 hv_do_hypercall(u64 control, void *input, void *output) |
| 202 | { |
| 203 | u64 input_address = (input) ? virt_to_phys(input) : 0; |
| 204 | u64 output_address = (output) ? virt_to_phys(output) : 0; |
| 205 | #ifdef CONFIG_X86_64 |
| 206 | u64 hv_status = 0; |
| 207 | |
| 208 | if (!hypercall_pg) |
| 209 | return (u64)ULLONG_MAX; |
| 210 | |
| 211 | __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8"); |
| 212 | __asm__ __volatile__("call *%3" : "=a" (hv_status) : |
| 213 | "c" (control), "d" (input_address), |
| 214 | "m" (hypercall_pg)); |
| 215 | |
| 216 | return hv_status; |
| 217 | |
| 218 | #else |
| 219 | |
| 220 | u32 control_hi = control >> 32; |
| 221 | u32 control_lo = control & 0xFFFFFFFF; |
| 222 | u32 hv_status_hi = 1; |
| 223 | u32 hv_status_lo = 1; |
| 224 | u32 input_address_hi = input_address >> 32; |
| 225 | u32 input_address_lo = input_address & 0xFFFFFFFF; |
| 226 | u32 output_address_hi = output_address >> 32; |
| 227 | u32 output_address_lo = output_address & 0xFFFFFFFF; |
| 228 | |
| 229 | if (!hypercall_pg) |
| 230 | return (u64)ULLONG_MAX; |
| 231 | |
| 232 | __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi), |
| 233 | "=a"(hv_status_lo) : "d" (control_hi), |
| 234 | "a" (control_lo), "b" (input_address_hi), |
| 235 | "c" (input_address_lo), "D"(output_address_hi), |
| 236 | "S"(output_address_lo), "m" (hypercall_pg)); |
| 237 | |
| 238 | return hv_status_lo | ((u64)hv_status_hi << 32); |
| 239 | #endif /* !x86_64 */ |
| 240 | } |
| 241 | EXPORT_SYMBOL_GPL(hv_do_hypercall); |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 242 | |
| 243 | void hyperv_report_panic(struct pt_regs *regs) |
| 244 | { |
| 245 | static bool panic_reported; |
| 246 | |
| 247 | /* |
| 248 | * We prefer to report panic on 'die' chain as we have proper |
| 249 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 250 | * to report it on 'panic'. |
| 251 | */ |
| 252 | if (panic_reported) |
| 253 | return; |
| 254 | panic_reported = true; |
| 255 | |
| 256 | wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip); |
| 257 | wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax); |
| 258 | wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx); |
| 259 | wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx); |
| 260 | wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx); |
| 261 | |
| 262 | /* |
| 263 | * Let Hyper-V know there is crash data available |
| 264 | */ |
| 265 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 268 | |
| 269 | bool hv_is_hypercall_page_setup(void) |
| 270 | { |
| 271 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 272 | |
| 273 | /* Check if the hypercall page is setup */ |
| 274 | hypercall_msr.as_uint64 = 0; |
| 275 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 276 | |
| 277 | if (!hypercall_msr.enable) |
| 278 | return false; |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup); |