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 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 20 | #include <linux/efi.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 21 | #include <linux/types.h> |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 22 | #include <asm/apic.h> |
| 23 | #include <asm/desc.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 24 | #include <asm/hypervisor.h> |
Vitaly Kuznetsov | 5a48580 | 2018-03-20 15:02:05 +0100 | [diff] [blame] | 25 | #include <asm/hyperv-tlfs.h> |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 26 | #include <asm/mshyperv.h> |
| 27 | #include <linux/version.h> |
| 28 | #include <linux/vmalloc.h> |
| 29 | #include <linux/mm.h> |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 30 | #include <linux/clockchips.h> |
Stephen Hemminger | 6707181 | 2017-03-04 18:27:11 -0700 | [diff] [blame] | 31 | #include <linux/hyperv.h> |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 32 | #include <linux/slab.h> |
| 33 | #include <linux/cpuhotplug.h> |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 34 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 35 | #ifdef CONFIG_HYPERV_TSCPAGE |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 36 | |
| 37 | static struct ms_hyperv_tsc_page *tsc_pg; |
| 38 | |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 39 | struct ms_hyperv_tsc_page *hv_get_tsc_page(void) |
| 40 | { |
| 41 | return tsc_pg; |
| 42 | } |
Vitaly Kuznetsov | e2768ea | 2018-01-24 14:23:32 +0100 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(hv_get_tsc_page); |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 44 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 45 | static u64 read_hv_clock_tsc(struct clocksource *arg) |
| 46 | { |
Vitaly Kuznetsov | 0733379 | 2017-03-03 14:21:41 +0100 | [diff] [blame] | 47 | u64 current_tick = hv_read_tsc_page(tsc_pg); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 48 | |
Vitaly Kuznetsov | 0733379 | 2017-03-03 14:21:41 +0100 | [diff] [blame] | 49 | if (current_tick == U64_MAX) |
| 50 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 51 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 52 | return current_tick; |
| 53 | } |
| 54 | |
| 55 | static struct clocksource hyperv_cs_tsc = { |
| 56 | .name = "hyperv_clocksource_tsc_page", |
| 57 | .rating = 400, |
| 58 | .read = read_hv_clock_tsc, |
| 59 | .mask = CLOCKSOURCE_MASK(64), |
| 60 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 61 | }; |
| 62 | #endif |
| 63 | |
| 64 | static u64 read_hv_clock_msr(struct clocksource *arg) |
| 65 | { |
| 66 | u64 current_tick; |
| 67 | /* |
| 68 | * Read the partition counter to get the current tick count. This count |
| 69 | * is set to 0 when the partition is created and is incremented in |
| 70 | * 100 nanosecond units. |
| 71 | */ |
| 72 | rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); |
| 73 | return current_tick; |
| 74 | } |
| 75 | |
| 76 | static struct clocksource hyperv_cs_msr = { |
| 77 | .name = "hyperv_clocksource_msr", |
| 78 | .rating = 400, |
| 79 | .read = read_hv_clock_msr, |
| 80 | .mask = CLOCKSOURCE_MASK(64), |
| 81 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 82 | }; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 83 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 84 | void *hv_hypercall_pg; |
| 85 | EXPORT_SYMBOL_GPL(hv_hypercall_pg); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 86 | struct clocksource *hyperv_cs; |
| 87 | EXPORT_SYMBOL_GPL(hyperv_cs); |
| 88 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 89 | u32 *hv_vp_index; |
| 90 | EXPORT_SYMBOL_GPL(hv_vp_index); |
| 91 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 92 | struct hv_vp_assist_page **hv_vp_assist_page; |
| 93 | EXPORT_SYMBOL_GPL(hv_vp_assist_page); |
| 94 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 95 | void __percpu **hyperv_pcpu_input_arg; |
| 96 | EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg); |
| 97 | |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 98 | u32 hv_max_vp_index; |
Maya Nakamura | c8ccf75 | 2019-03-01 07:04:17 +0000 | [diff] [blame] | 99 | EXPORT_SYMBOL_GPL(hv_max_vp_index); |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 100 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 101 | static int hv_cpu_init(unsigned int cpu) |
| 102 | { |
| 103 | u64 msr_vp_index; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 104 | struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()]; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 105 | void **input_arg; |
Kangjie Lu | 534c89c | 2019-03-14 00:46:51 -0500 | [diff] [blame] | 106 | struct page *pg; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 107 | |
| 108 | input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
Kangjie Lu | 534c89c | 2019-03-14 00:46:51 -0500 | [diff] [blame] | 109 | pg = alloc_page(GFP_KERNEL); |
| 110 | if (unlikely(!pg)) |
| 111 | return -ENOMEM; |
| 112 | *input_arg = page_address(pg); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 113 | |
| 114 | hv_get_vp_index(msr_vp_index); |
| 115 | |
| 116 | hv_vp_index[smp_processor_id()] = msr_vp_index; |
| 117 | |
Vitaly Kuznetsov | a3b7424 | 2017-10-06 17:48:54 +0200 | [diff] [blame] | 118 | if (msr_vp_index > hv_max_vp_index) |
| 119 | hv_max_vp_index = msr_vp_index; |
| 120 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 121 | if (!hv_vp_assist_page) |
| 122 | return 0; |
| 123 | |
| 124 | if (!*hvp) |
| 125 | *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL); |
| 126 | |
| 127 | if (*hvp) { |
| 128 | u64 val; |
| 129 | |
| 130 | val = vmalloc_to_pfn(*hvp); |
| 131 | val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) | |
| 132 | HV_X64_MSR_VP_ASSIST_PAGE_ENABLE; |
| 133 | |
| 134 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); |
| 135 | } |
| 136 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 140 | static void (*hv_reenlightenment_cb)(void); |
| 141 | |
| 142 | static void hv_reenlightenment_notify(struct work_struct *dummy) |
| 143 | { |
| 144 | struct hv_tsc_emulation_status emu_status; |
| 145 | |
| 146 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 147 | |
| 148 | /* Don't issue the callback if TSC accesses are not emulated */ |
| 149 | if (hv_reenlightenment_cb && emu_status.inprogress) |
| 150 | hv_reenlightenment_cb(); |
| 151 | } |
| 152 | static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); |
| 153 | |
| 154 | void hyperv_stop_tsc_emulation(void) |
| 155 | { |
| 156 | u64 freq; |
| 157 | struct hv_tsc_emulation_status emu_status; |
| 158 | |
| 159 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 160 | emu_status.inprogress = 0; |
| 161 | wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 162 | |
| 163 | rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); |
| 164 | tsc_khz = div64_u64(freq, 1000); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); |
| 167 | |
| 168 | static inline bool hv_reenlightenment_available(void) |
| 169 | { |
| 170 | /* |
| 171 | * Check for required features and priviliges to make TSC frequency |
| 172 | * change notifications work. |
| 173 | */ |
| 174 | return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS && |
| 175 | ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && |
| 176 | ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT; |
| 177 | } |
| 178 | |
| 179 | __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs) |
| 180 | { |
| 181 | entering_ack_irq(); |
| 182 | |
Vitaly Kuznetsov | 51d4e5d | 2018-01-24 14:23:35 +0100 | [diff] [blame] | 183 | inc_irq_stat(irq_hv_reenlightenment_count); |
| 184 | |
Vitaly Kuznetsov | 9328626 | 2018-01-24 14:23:33 +0100 | [diff] [blame] | 185 | schedule_delayed_work(&hv_reenlightenment_work, HZ/10); |
| 186 | |
| 187 | exiting_irq(); |
| 188 | } |
| 189 | |
| 190 | void set_hv_tscchange_cb(void (*cb)(void)) |
| 191 | { |
| 192 | struct hv_reenlightenment_control re_ctrl = { |
| 193 | .vector = HYPERV_REENLIGHTENMENT_VECTOR, |
| 194 | .enabled = 1, |
| 195 | .target_vp = hv_vp_index[smp_processor_id()] |
| 196 | }; |
| 197 | struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; |
| 198 | |
| 199 | if (!hv_reenlightenment_available()) { |
| 200 | pr_warn("Hyper-V: reenlightenment support is unavailable\n"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | hv_reenlightenment_cb = cb; |
| 205 | |
| 206 | /* Make sure callback is registered before we write to MSRs */ |
| 207 | wmb(); |
| 208 | |
| 209 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 210 | wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); |
| 211 | } |
| 212 | EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); |
| 213 | |
| 214 | void clear_hv_tscchange_cb(void) |
| 215 | { |
| 216 | struct hv_reenlightenment_control re_ctrl; |
| 217 | |
| 218 | if (!hv_reenlightenment_available()) |
| 219 | return; |
| 220 | |
| 221 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 222 | re_ctrl.enabled = 0; |
| 223 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 224 | |
| 225 | hv_reenlightenment_cb = NULL; |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); |
| 228 | |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 229 | static int hv_cpu_die(unsigned int cpu) |
| 230 | { |
| 231 | struct hv_reenlightenment_control re_ctrl; |
| 232 | unsigned int new_cpu; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 233 | unsigned long flags; |
| 234 | void **input_arg; |
| 235 | void *input_pg = NULL; |
| 236 | |
| 237 | local_irq_save(flags); |
| 238 | input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
| 239 | input_pg = *input_arg; |
| 240 | *input_arg = NULL; |
| 241 | local_irq_restore(flags); |
| 242 | free_page((unsigned long)input_pg); |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 243 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 244 | if (hv_vp_assist_page && hv_vp_assist_page[cpu]) |
| 245 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0); |
| 246 | |
Vitaly Kuznetsov | e7c4e36 | 2018-01-24 14:23:34 +0100 | [diff] [blame] | 247 | if (hv_reenlightenment_cb == NULL) |
| 248 | return 0; |
| 249 | |
| 250 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 251 | if (re_ctrl.target_vp == hv_vp_index[cpu]) { |
| 252 | /* Reassign to some other online CPU */ |
| 253 | new_cpu = cpumask_any_but(cpu_online_mask, cpu); |
| 254 | |
| 255 | re_ctrl.target_vp = hv_vp_index[new_cpu]; |
| 256 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 262 | static int __init hv_pci_init(void) |
| 263 | { |
| 264 | int gen2vm = efi_enabled(EFI_BOOT); |
| 265 | |
| 266 | /* |
| 267 | * For Generation-2 VM, we exit from pci_arch_init() by returning 0. |
| 268 | * The purpose is to suppress the harmless warning: |
| 269 | * "PCI: Fatal: No config space access function found" |
| 270 | */ |
| 271 | if (gen2vm) |
| 272 | return 0; |
| 273 | |
| 274 | /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ |
| 275 | return 1; |
| 276 | } |
| 277 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 278 | /* |
| 279 | * This function is to be invoked early in the boot sequence after the |
| 280 | * hypervisor has been detected. |
| 281 | * |
| 282 | * 1. Setup the hypercall page. |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 283 | * 2. Register Hyper-V specific clocksource. |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 284 | * 3. Setup Hyper-V specific APIC entry points. |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 285 | */ |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 286 | void __init hyperv_init(void) |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 287 | { |
Vitaly Kuznetsov | 89a8f6d | 2018-01-24 14:23:31 +0100 | [diff] [blame] | 288 | u64 guest_id, required_msrs; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 289 | union hv_x64_msr_hypercall_contents hypercall_msr; |
K. Y. Srinivasan | 1268ed0 | 2018-07-03 16:01:55 -0700 | [diff] [blame] | 290 | int cpuhp, i; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 291 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 292 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 293 | return; |
| 294 | |
Vitaly Kuznetsov | 89a8f6d | 2018-01-24 14:23:31 +0100 | [diff] [blame] | 295 | /* Absolutely required MSRs */ |
| 296 | required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE | |
| 297 | HV_X64_MSR_VP_INDEX_AVAILABLE; |
| 298 | |
| 299 | if ((ms_hyperv.features & required_msrs) != required_msrs) |
| 300 | return; |
| 301 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 302 | /* |
| 303 | * Allocate the per-CPU state for the hypercall input arg. |
| 304 | * If this allocation fails, we will not be able to setup |
| 305 | * (per-CPU) hypercall input page and thus this failure is |
| 306 | * fatal on Hyper-V. |
| 307 | */ |
| 308 | hyperv_pcpu_input_arg = alloc_percpu(void *); |
| 309 | |
| 310 | BUG_ON(hyperv_pcpu_input_arg == NULL); |
| 311 | |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 312 | /* Allocate percpu VP index */ |
| 313 | hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), |
| 314 | GFP_KERNEL); |
| 315 | if (!hv_vp_index) |
| 316 | return; |
| 317 | |
K. Y. Srinivasan | 1268ed0 | 2018-07-03 16:01:55 -0700 | [diff] [blame] | 318 | for (i = 0; i < num_possible_cpus(); i++) |
| 319 | hv_vp_index[i] = VP_INVAL; |
| 320 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 321 | hv_vp_assist_page = kcalloc(num_possible_cpus(), |
| 322 | sizeof(*hv_vp_assist_page), GFP_KERNEL); |
| 323 | if (!hv_vp_assist_page) { |
| 324 | ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 325 | goto free_vp_index; |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", |
| 329 | hv_cpu_init, hv_cpu_die); |
| 330 | if (cpuhp < 0) |
| 331 | goto free_vp_assist_page; |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 332 | |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 333 | /* |
| 334 | * Setup the hypercall page and enable hypercalls. |
| 335 | * 1. Register the guest ID |
| 336 | * 2. Enable the hypercall and register the hypercall page |
| 337 | */ |
| 338 | guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 339 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 340 | |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 341 | hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX); |
| 342 | if (hv_hypercall_pg == NULL) { |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 343 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 344 | goto remove_cpuhp_state; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 348 | hypercall_msr.enable = 1; |
Vitaly Kuznetsov | fc53662 | 2017-08-02 18:09:14 +0200 | [diff] [blame] | 349 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 350 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 351 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 352 | hv_apic_init(); |
| 353 | |
Dexuan Cui | 2f285f4 | 2018-09-18 22:29:50 +0000 | [diff] [blame] | 354 | x86_init.pci.arch_init = hv_pci_init; |
| 355 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 356 | /* |
| 357 | * Register Hyper-V specific clocksource. |
| 358 | */ |
Vitaly Kuznetsov | bd2a9ad | 2017-03-03 14:21:40 +0100 | [diff] [blame] | 359 | #ifdef CONFIG_HYPERV_TSCPAGE |
Michael Kelley | 7dc9b6b | 2018-06-05 13:37:54 -0700 | [diff] [blame] | 360 | if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) { |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 361 | union hv_x64_msr_hypercall_contents tsc_msr; |
| 362 | |
| 363 | tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL); |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 364 | if (!tsc_pg) |
| 365 | goto register_msr_cs; |
| 366 | |
| 367 | hyperv_cs = &hyperv_cs_tsc; |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 368 | |
| 369 | rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
| 370 | |
| 371 | tsc_msr.enable = 1; |
| 372 | tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg); |
| 373 | |
| 374 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); |
Vitaly Kuznetsov | 90b2043 | 2017-03-03 14:21:42 +0100 | [diff] [blame] | 375 | |
| 376 | hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK; |
| 377 | |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 378 | clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100); |
| 379 | return; |
| 380 | } |
Arnd Bergmann | 73667e3 | 2017-02-14 22:17:17 +0100 | [diff] [blame] | 381 | register_msr_cs: |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 382 | #endif |
| 383 | /* |
| 384 | * For 32 bit guests just use the MSR based mechanism for reading |
| 385 | * the partition counter. |
| 386 | */ |
| 387 | |
Vitaly Kuznetsov | dee863b | 2017-02-04 09:57:13 -0700 | [diff] [blame] | 388 | hyperv_cs = &hyperv_cs_msr; |
Michael Kelley | 7dc9b6b | 2018-06-05 13:37:54 -0700 | [diff] [blame] | 389 | if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE) |
K. Y. Srinivasan | 63ed4e0 | 2017-01-19 11:51:46 -0700 | [diff] [blame] | 390 | clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100); |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 391 | |
| 392 | return; |
| 393 | |
Vitaly Kuznetsov | a46d15c | 2018-03-20 15:02:08 +0100 | [diff] [blame] | 394 | remove_cpuhp_state: |
| 395 | cpuhp_remove_state(cpuhp); |
| 396 | free_vp_assist_page: |
| 397 | kfree(hv_vp_assist_page); |
| 398 | hv_vp_assist_page = NULL; |
Vitaly Kuznetsov | 7415aea | 2017-08-02 18:09:18 +0200 | [diff] [blame] | 399 | free_vp_index: |
| 400 | kfree(hv_vp_index); |
| 401 | hv_vp_index = NULL; |
K. Y. Srinivasan | 8730046 | 2017-01-18 16:45:02 -0700 | [diff] [blame] | 402 | } |
K. Y. Srinivasan | 6ab42a6 | 2017-01-18 16:45:03 -0700 | [diff] [blame] | 403 | |
| 404 | /* |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 405 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 406 | */ |
| 407 | void hyperv_cleanup(void) |
| 408 | { |
| 409 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 410 | |
| 411 | /* Reset our OS id */ |
| 412 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 413 | |
Kairui Song | 179fb36 | 2019-03-06 19:18:27 +0800 | [diff] [blame] | 414 | /* |
| 415 | * Reset hypercall page reference before reset the page, |
| 416 | * let hypercall operations fail safely rather than |
| 417 | * panic the kernel for using invalid hypercall page |
| 418 | */ |
| 419 | hv_hypercall_pg = NULL; |
| 420 | |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 421 | /* Reset the hypercall page */ |
| 422 | hypercall_msr.as_uint64 = 0; |
| 423 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | 5647dbf | 2017-01-28 12:37:15 -0700 | [diff] [blame] | 424 | |
| 425 | /* Reset the TSC page */ |
| 426 | hypercall_msr.as_uint64 = 0; |
| 427 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); |
Vitaly Kuznetsov | d6f3609 | 2017-01-28 12:37:14 -0700 | [diff] [blame] | 428 | } |
| 429 | EXPORT_SYMBOL_GPL(hyperv_cleanup); |
| 430 | |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 431 | void hyperv_report_panic(struct pt_regs *regs, long err) |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 432 | { |
| 433 | static bool panic_reported; |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 434 | u64 guest_id; |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * We prefer to report panic on 'die' chain as we have proper |
| 438 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 439 | * to report it on 'panic'. |
| 440 | */ |
| 441 | if (panic_reported) |
| 442 | return; |
| 443 | panic_reported = true; |
| 444 | |
K. Y. Srinivasan | 7ed4325 | 2017-10-29 11:33:41 -0700 | [diff] [blame] | 445 | rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 446 | |
| 447 | wrmsrl(HV_X64_MSR_CRASH_P0, err); |
| 448 | wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); |
| 449 | wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); |
| 450 | wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); |
| 451 | wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); |
K. Y. Srinivasan | d058fa7 | 2017-01-19 11:51:48 -0700 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * Let Hyper-V know there is crash data available |
| 455 | */ |
| 456 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 459 | |
Sunil Muthuswamy | 81b18bc | 2018-07-08 02:56:51 +0000 | [diff] [blame] | 460 | /** |
| 461 | * hyperv_report_panic_msg - report panic message to Hyper-V |
| 462 | * @pa: physical address of the panic page containing the message |
| 463 | * @size: size of the message in the page |
| 464 | */ |
| 465 | void hyperv_report_panic_msg(phys_addr_t pa, size_t size) |
| 466 | { |
| 467 | /* |
| 468 | * P3 to contain the physical address of the panic page & P4 to |
| 469 | * contain the size of the panic data in that page. Rest of the |
| 470 | * registers are no-op when the NOTIFY_MSG flag is set. |
| 471 | */ |
| 472 | wrmsrl(HV_X64_MSR_CRASH_P0, 0); |
| 473 | wrmsrl(HV_X64_MSR_CRASH_P1, 0); |
| 474 | wrmsrl(HV_X64_MSR_CRASH_P2, 0); |
| 475 | wrmsrl(HV_X64_MSR_CRASH_P3, pa); |
| 476 | wrmsrl(HV_X64_MSR_CRASH_P4, size); |
| 477 | |
| 478 | /* |
| 479 | * Let Hyper-V know there is crash data available along with |
| 480 | * the panic message. |
| 481 | */ |
| 482 | wrmsrl(HV_X64_MSR_CRASH_CTL, |
| 483 | (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG)); |
| 484 | } |
| 485 | EXPORT_SYMBOL_GPL(hyperv_report_panic_msg); |
| 486 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 487 | bool hv_is_hyperv_initialized(void) |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 488 | { |
| 489 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 490 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 491 | /* |
| 492 | * Ensure that we're really on Hyper-V, and not a KVM or Xen |
| 493 | * emulation of Hyper-V |
| 494 | */ |
| 495 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
| 496 | return false; |
| 497 | |
| 498 | /* |
| 499 | * Verify that earlier initialization succeeded by checking |
| 500 | * that the hypercall page is setup |
| 501 | */ |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 502 | hypercall_msr.as_uint64 = 0; |
| 503 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 504 | |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 505 | return hypercall_msr.enable; |
K. Y. Srinivasan | 73638cd | 2017-01-19 11:51:49 -0700 | [diff] [blame] | 506 | } |
Michael Kelley | 4a5f3cd | 2017-12-22 11:19:02 -0700 | [diff] [blame] | 507 | EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); |