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