18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * X86 specific Hyper-V initialization code. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2016, Microsoft, Inc. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author : K. Y. Srinivasan <kys@microsoft.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/acpi.h> 118c2ecf20Sopenharmony_ci#include <linux/efi.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <asm/apic.h> 148c2ecf20Sopenharmony_ci#include <asm/desc.h> 158c2ecf20Sopenharmony_ci#include <asm/hypervisor.h> 168c2ecf20Sopenharmony_ci#include <asm/hyperv-tlfs.h> 178c2ecf20Sopenharmony_ci#include <asm/mshyperv.h> 188c2ecf20Sopenharmony_ci#include <asm/idtentry.h> 198c2ecf20Sopenharmony_ci#include <linux/kexec.h> 208c2ecf20Sopenharmony_ci#include <linux/version.h> 218c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 228c2ecf20Sopenharmony_ci#include <linux/mm.h> 238c2ecf20Sopenharmony_ci#include <linux/hyperv.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/kernel.h> 268c2ecf20Sopenharmony_ci#include <linux/cpuhotplug.h> 278c2ecf20Sopenharmony_ci#include <linux/syscore_ops.h> 288c2ecf20Sopenharmony_ci#include <clocksource/hyperv_timer.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciint hyperv_init_cpuhp; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_civoid *hv_hypercall_pg; 338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_hypercall_pg); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* Storage to save the hypercall page temporarily for hibernation */ 368c2ecf20Sopenharmony_cistatic void *hv_hypercall_pg_saved; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciu32 *hv_vp_index; 398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_vp_index); 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct hv_vp_assist_page **hv_vp_assist_page; 428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_vp_assist_page); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_civoid __percpu **hyperv_pcpu_input_arg; 458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciu32 hv_max_vp_index; 488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_max_vp_index); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_civoid *hv_alloc_hyperv_page(void) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return (void *)__get_free_page(GFP_KERNEL); 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_alloc_hyperv_page); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_civoid *hv_alloc_hyperv_zeroed_page(void) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_civoid hv_free_hyperv_page(unsigned long addr) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci free_page(addr); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_free_hyperv_page); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int hv_cpu_init(unsigned int cpu) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci u64 msr_vp_index; 758c2ecf20Sopenharmony_ci struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()]; 768c2ecf20Sopenharmony_ci void **input_arg; 778c2ecf20Sopenharmony_ci struct page *pg; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); 808c2ecf20Sopenharmony_ci /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ 818c2ecf20Sopenharmony_ci pg = alloc_page(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL); 828c2ecf20Sopenharmony_ci if (unlikely(!pg)) 838c2ecf20Sopenharmony_ci return -ENOMEM; 848c2ecf20Sopenharmony_ci *input_arg = page_address(pg); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci hv_get_vp_index(msr_vp_index); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci hv_vp_index[smp_processor_id()] = msr_vp_index; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if (msr_vp_index > hv_max_vp_index) 918c2ecf20Sopenharmony_ci hv_max_vp_index = msr_vp_index; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (!hv_vp_assist_page) 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci /* 978c2ecf20Sopenharmony_ci * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section 988c2ecf20Sopenharmony_ci * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure 998c2ecf20Sopenharmony_ci * we always write the EOI MSR in hv_apic_eoi_write() *after* the 1008c2ecf20Sopenharmony_ci * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may 1018c2ecf20Sopenharmony_ci * not be stopped in the case of CPU offlining and the VM will hang. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ci if (!*hvp) { 1048c2ecf20Sopenharmony_ci *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci if (*hvp) { 1088c2ecf20Sopenharmony_ci u64 val; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci val = vmalloc_to_pfn(*hvp); 1118c2ecf20Sopenharmony_ci val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) | 1128c2ecf20Sopenharmony_ci HV_X64_MSR_VP_ASSIST_PAGE_ENABLE; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci return 0; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic void (*hv_reenlightenment_cb)(void); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic void hv_reenlightenment_notify(struct work_struct *dummy) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci struct hv_tsc_emulation_status emu_status; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* Don't issue the callback if TSC accesses are not emulated */ 1298c2ecf20Sopenharmony_ci if (hv_reenlightenment_cb && emu_status.inprogress) 1308c2ecf20Sopenharmony_ci hv_reenlightenment_cb(); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_cistatic DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_civoid hyperv_stop_tsc_emulation(void) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci u64 freq; 1378c2ecf20Sopenharmony_ci struct hv_tsc_emulation_status emu_status; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 1408c2ecf20Sopenharmony_ci emu_status.inprogress = 0; 1418c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); 1448c2ecf20Sopenharmony_ci tsc_khz = div64_u64(freq, 1000); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic inline bool hv_reenlightenment_available(void) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci /* 1518c2ecf20Sopenharmony_ci * Check for required features and priviliges to make TSC frequency 1528c2ecf20Sopenharmony_ci * change notifications work. 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_ci return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS && 1558c2ecf20Sopenharmony_ci ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && 1568c2ecf20Sopenharmony_ci ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciDEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci ack_APIC_irq(); 1628c2ecf20Sopenharmony_ci inc_irq_stat(irq_hv_reenlightenment_count); 1638c2ecf20Sopenharmony_ci schedule_delayed_work(&hv_reenlightenment_work, HZ/10); 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_civoid set_hv_tscchange_cb(void (*cb)(void)) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci struct hv_reenlightenment_control re_ctrl = { 1698c2ecf20Sopenharmony_ci .vector = HYPERV_REENLIGHTENMENT_VECTOR, 1708c2ecf20Sopenharmony_ci .enabled = 1, 1718c2ecf20Sopenharmony_ci }; 1728c2ecf20Sopenharmony_ci struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (!hv_reenlightenment_available()) { 1758c2ecf20Sopenharmony_ci pr_warn("Hyper-V: reenlightenment support is unavailable\n"); 1768c2ecf20Sopenharmony_ci return; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci if (!hv_vp_index) 1808c2ecf20Sopenharmony_ci return; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci hv_reenlightenment_cb = cb; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* Make sure callback is registered before we write to MSRs */ 1858c2ecf20Sopenharmony_ci wmb(); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci re_ctrl.target_vp = hv_vp_index[get_cpu()]; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 1908c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci put_cpu(); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(set_hv_tscchange_cb); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_civoid clear_hv_tscchange_cb(void) 1978c2ecf20Sopenharmony_ci{ 1988c2ecf20Sopenharmony_ci struct hv_reenlightenment_control re_ctrl; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (!hv_reenlightenment_available()) 2018c2ecf20Sopenharmony_ci return; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 2048c2ecf20Sopenharmony_ci re_ctrl.enabled = 0; 2058c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci hv_reenlightenment_cb = NULL; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistatic int hv_cpu_die(unsigned int cpu) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct hv_reenlightenment_control re_ctrl; 2148c2ecf20Sopenharmony_ci unsigned int new_cpu; 2158c2ecf20Sopenharmony_ci unsigned long flags; 2168c2ecf20Sopenharmony_ci void **input_arg; 2178c2ecf20Sopenharmony_ci void *input_pg = NULL; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci local_irq_save(flags); 2208c2ecf20Sopenharmony_ci input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); 2218c2ecf20Sopenharmony_ci input_pg = *input_arg; 2228c2ecf20Sopenharmony_ci *input_arg = NULL; 2238c2ecf20Sopenharmony_ci local_irq_restore(flags); 2248c2ecf20Sopenharmony_ci free_page((unsigned long)input_pg); 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci if (hv_vp_assist_page && hv_vp_assist_page[cpu]) 2278c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci if (hv_reenlightenment_cb == NULL) 2308c2ecf20Sopenharmony_ci return 0; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 2338c2ecf20Sopenharmony_ci if (re_ctrl.target_vp == hv_vp_index[cpu]) { 2348c2ecf20Sopenharmony_ci /* 2358c2ecf20Sopenharmony_ci * Reassign reenlightenment notifications to some other online 2368c2ecf20Sopenharmony_ci * CPU or just disable the feature if there are no online CPUs 2378c2ecf20Sopenharmony_ci * left (happens on hibernation). 2388c2ecf20Sopenharmony_ci */ 2398c2ecf20Sopenharmony_ci new_cpu = cpumask_any_but(cpu_online_mask, cpu); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci if (new_cpu < nr_cpu_ids) 2428c2ecf20Sopenharmony_ci re_ctrl.target_vp = hv_vp_index[new_cpu]; 2438c2ecf20Sopenharmony_ci else 2448c2ecf20Sopenharmony_ci re_ctrl.enabled = 0; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci return 0; 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic int __init hv_pci_init(void) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci int gen2vm = efi_enabled(EFI_BOOT); 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* 2578c2ecf20Sopenharmony_ci * For Generation-2 VM, we exit from pci_arch_init() by returning 0. 2588c2ecf20Sopenharmony_ci * The purpose is to suppress the harmless warning: 2598c2ecf20Sopenharmony_ci * "PCI: Fatal: No config space access function found" 2608c2ecf20Sopenharmony_ci */ 2618c2ecf20Sopenharmony_ci if (gen2vm) 2628c2ecf20Sopenharmony_ci return 0; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ 2658c2ecf20Sopenharmony_ci return 1; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic int hv_suspend(void) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci union hv_x64_msr_hypercall_contents hypercall_msr; 2718c2ecf20Sopenharmony_ci int ret; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci /* 2748c2ecf20Sopenharmony_ci * Reset the hypercall page as it is going to be invalidated 2758c2ecf20Sopenharmony_ci * accross hibernation. Setting hv_hypercall_pg to NULL ensures 2768c2ecf20Sopenharmony_ci * that any subsequent hypercall operation fails safely instead of 2778c2ecf20Sopenharmony_ci * crashing due to an access of an invalid page. The hypercall page 2788c2ecf20Sopenharmony_ci * pointer is restored on resume. 2798c2ecf20Sopenharmony_ci */ 2808c2ecf20Sopenharmony_ci hv_hypercall_pg_saved = hv_hypercall_pg; 2818c2ecf20Sopenharmony_ci hv_hypercall_pg = NULL; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci /* Disable the hypercall page in the hypervisor */ 2848c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 2858c2ecf20Sopenharmony_ci hypercall_msr.enable = 0; 2868c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci ret = hv_cpu_die(0); 2898c2ecf20Sopenharmony_ci return ret; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic void hv_resume(void) 2938c2ecf20Sopenharmony_ci{ 2948c2ecf20Sopenharmony_ci union hv_x64_msr_hypercall_contents hypercall_msr; 2958c2ecf20Sopenharmony_ci int ret; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci ret = hv_cpu_init(0); 2988c2ecf20Sopenharmony_ci WARN_ON(ret); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci /* Re-enable the hypercall page */ 3018c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 3028c2ecf20Sopenharmony_ci hypercall_msr.enable = 1; 3038c2ecf20Sopenharmony_ci hypercall_msr.guest_physical_address = 3048c2ecf20Sopenharmony_ci vmalloc_to_pfn(hv_hypercall_pg_saved); 3058c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci hv_hypercall_pg = hv_hypercall_pg_saved; 3088c2ecf20Sopenharmony_ci hv_hypercall_pg_saved = NULL; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci /* 3118c2ecf20Sopenharmony_ci * Reenlightenment notifications are disabled by hv_cpu_die(0), 3128c2ecf20Sopenharmony_ci * reenable them here if hv_reenlightenment_cb was previously set. 3138c2ecf20Sopenharmony_ci */ 3148c2ecf20Sopenharmony_ci if (hv_reenlightenment_cb) 3158c2ecf20Sopenharmony_ci set_hv_tscchange_cb(hv_reenlightenment_cb); 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci/* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */ 3198c2ecf20Sopenharmony_cistatic struct syscore_ops hv_syscore_ops = { 3208c2ecf20Sopenharmony_ci .suspend = hv_suspend, 3218c2ecf20Sopenharmony_ci .resume = hv_resume, 3228c2ecf20Sopenharmony_ci}; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_cistatic void (* __initdata old_setup_percpu_clockev)(void); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_cistatic void __init hv_stimer_setup_percpu_clockev(void) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci /* 3298c2ecf20Sopenharmony_ci * Ignore any errors in setting up stimer clockevents 3308c2ecf20Sopenharmony_ci * as we can run with the LAPIC timer as a fallback. 3318c2ecf20Sopenharmony_ci */ 3328c2ecf20Sopenharmony_ci (void)hv_stimer_alloc(); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci /* 3358c2ecf20Sopenharmony_ci * Still register the LAPIC timer, because the direct-mode STIMER is 3368c2ecf20Sopenharmony_ci * not supported by old versions of Hyper-V. This also allows users 3378c2ecf20Sopenharmony_ci * to switch to LAPIC timer via /sys, if they want to. 3388c2ecf20Sopenharmony_ci */ 3398c2ecf20Sopenharmony_ci if (old_setup_percpu_clockev) 3408c2ecf20Sopenharmony_ci old_setup_percpu_clockev(); 3418c2ecf20Sopenharmony_ci} 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci/* 3448c2ecf20Sopenharmony_ci * This function is to be invoked early in the boot sequence after the 3458c2ecf20Sopenharmony_ci * hypervisor has been detected. 3468c2ecf20Sopenharmony_ci * 3478c2ecf20Sopenharmony_ci * 1. Setup the hypercall page. 3488c2ecf20Sopenharmony_ci * 2. Register Hyper-V specific clocksource. 3498c2ecf20Sopenharmony_ci * 3. Setup Hyper-V specific APIC entry points. 3508c2ecf20Sopenharmony_ci */ 3518c2ecf20Sopenharmony_civoid __init hyperv_init(void) 3528c2ecf20Sopenharmony_ci{ 3538c2ecf20Sopenharmony_ci u64 guest_id, required_msrs; 3548c2ecf20Sopenharmony_ci union hv_x64_msr_hypercall_contents hypercall_msr; 3558c2ecf20Sopenharmony_ci int cpuhp, i; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci if (x86_hyper_type != X86_HYPER_MS_HYPERV) 3588c2ecf20Sopenharmony_ci return; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci /* Absolutely required MSRs */ 3618c2ecf20Sopenharmony_ci required_msrs = HV_MSR_HYPERCALL_AVAILABLE | 3628c2ecf20Sopenharmony_ci HV_MSR_VP_INDEX_AVAILABLE; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci if ((ms_hyperv.features & required_msrs) != required_msrs) 3658c2ecf20Sopenharmony_ci return; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci /* 3688c2ecf20Sopenharmony_ci * Allocate the per-CPU state for the hypercall input arg. 3698c2ecf20Sopenharmony_ci * If this allocation fails, we will not be able to setup 3708c2ecf20Sopenharmony_ci * (per-CPU) hypercall input page and thus this failure is 3718c2ecf20Sopenharmony_ci * fatal on Hyper-V. 3728c2ecf20Sopenharmony_ci */ 3738c2ecf20Sopenharmony_ci hyperv_pcpu_input_arg = alloc_percpu(void *); 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci BUG_ON(hyperv_pcpu_input_arg == NULL); 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci /* Allocate percpu VP index */ 3788c2ecf20Sopenharmony_ci hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), 3798c2ecf20Sopenharmony_ci GFP_KERNEL); 3808c2ecf20Sopenharmony_ci if (!hv_vp_index) 3818c2ecf20Sopenharmony_ci return; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci for (i = 0; i < num_possible_cpus(); i++) 3848c2ecf20Sopenharmony_ci hv_vp_index[i] = VP_INVAL; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci hv_vp_assist_page = kcalloc(num_possible_cpus(), 3878c2ecf20Sopenharmony_ci sizeof(*hv_vp_assist_page), GFP_KERNEL); 3888c2ecf20Sopenharmony_ci if (!hv_vp_assist_page) { 3898c2ecf20Sopenharmony_ci ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 3908c2ecf20Sopenharmony_ci goto free_vp_index; 3918c2ecf20Sopenharmony_ci } 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", 3948c2ecf20Sopenharmony_ci hv_cpu_init, hv_cpu_die); 3958c2ecf20Sopenharmony_ci if (cpuhp < 0) 3968c2ecf20Sopenharmony_ci goto free_vp_assist_page; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* 3998c2ecf20Sopenharmony_ci * Setup the hypercall page and enable hypercalls. 4008c2ecf20Sopenharmony_ci * 1. Register the guest ID 4018c2ecf20Sopenharmony_ci * 2. Enable the hypercall and register the hypercall page 4028c2ecf20Sopenharmony_ci */ 4038c2ecf20Sopenharmony_ci guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); 4048c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, 4078c2ecf20Sopenharmony_ci VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, 4088c2ecf20Sopenharmony_ci VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, 4098c2ecf20Sopenharmony_ci __builtin_return_address(0)); 4108c2ecf20Sopenharmony_ci if (hv_hypercall_pg == NULL) { 4118c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 4128c2ecf20Sopenharmony_ci goto remove_cpuhp_state; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 4168c2ecf20Sopenharmony_ci hypercall_msr.enable = 1; 4178c2ecf20Sopenharmony_ci hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); 4188c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci /* 4218c2ecf20Sopenharmony_ci * hyperv_init() is called before LAPIC is initialized: see 4228c2ecf20Sopenharmony_ci * apic_intr_mode_init() -> x86_platform.apic_post_init() and 4238c2ecf20Sopenharmony_ci * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER 4248c2ecf20Sopenharmony_ci * depends on LAPIC, so hv_stimer_alloc() should be called from 4258c2ecf20Sopenharmony_ci * x86_init.timers.setup_percpu_clockev. 4268c2ecf20Sopenharmony_ci */ 4278c2ecf20Sopenharmony_ci old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev; 4288c2ecf20Sopenharmony_ci x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci hv_apic_init(); 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci x86_init.pci.arch_init = hv_pci_init; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci register_syscore_ops(&hv_syscore_ops); 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci hyperv_init_cpuhp = cpuhp; 4378c2ecf20Sopenharmony_ci return; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ciremove_cpuhp_state: 4408c2ecf20Sopenharmony_ci cpuhp_remove_state(cpuhp); 4418c2ecf20Sopenharmony_cifree_vp_assist_page: 4428c2ecf20Sopenharmony_ci kfree(hv_vp_assist_page); 4438c2ecf20Sopenharmony_ci hv_vp_assist_page = NULL; 4448c2ecf20Sopenharmony_cifree_vp_index: 4458c2ecf20Sopenharmony_ci kfree(hv_vp_index); 4468c2ecf20Sopenharmony_ci hv_vp_index = NULL; 4478c2ecf20Sopenharmony_ci} 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci/* 4508c2ecf20Sopenharmony_ci * This routine is called before kexec/kdump, it does the required cleanup. 4518c2ecf20Sopenharmony_ci */ 4528c2ecf20Sopenharmony_civoid hyperv_cleanup(void) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci union hv_x64_msr_hypercall_contents hypercall_msr; 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* Reset our OS id */ 4578c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci /* 4608c2ecf20Sopenharmony_ci * Reset hypercall page reference before reset the page, 4618c2ecf20Sopenharmony_ci * let hypercall operations fail safely rather than 4628c2ecf20Sopenharmony_ci * panic the kernel for using invalid hypercall page 4638c2ecf20Sopenharmony_ci */ 4648c2ecf20Sopenharmony_ci hv_hypercall_pg = NULL; 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci /* Reset the hypercall page */ 4678c2ecf20Sopenharmony_ci hypercall_msr.as_uint64 = 0; 4688c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 4698c2ecf20Sopenharmony_ci 4708c2ecf20Sopenharmony_ci /* Reset the TSC page */ 4718c2ecf20Sopenharmony_ci hypercall_msr.as_uint64 = 0; 4728c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); 4738c2ecf20Sopenharmony_ci} 4748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hyperv_cleanup); 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_civoid hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) 4778c2ecf20Sopenharmony_ci{ 4788c2ecf20Sopenharmony_ci static bool panic_reported; 4798c2ecf20Sopenharmony_ci u64 guest_id; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci if (in_die && !panic_on_oops) 4828c2ecf20Sopenharmony_ci return; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci /* 4858c2ecf20Sopenharmony_ci * We prefer to report panic on 'die' chain as we have proper 4868c2ecf20Sopenharmony_ci * registers to report, but if we miss it (e.g. on BUG()) we need 4878c2ecf20Sopenharmony_ci * to report it on 'panic'. 4888c2ecf20Sopenharmony_ci */ 4898c2ecf20Sopenharmony_ci if (panic_reported) 4908c2ecf20Sopenharmony_ci return; 4918c2ecf20Sopenharmony_ci panic_reported = true; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P0, err); 4968c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); 4978c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); 4988c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); 4998c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci /* 5028c2ecf20Sopenharmony_ci * Let Hyper-V know there is crash data available 5038c2ecf20Sopenharmony_ci */ 5048c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); 5058c2ecf20Sopenharmony_ci} 5068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hyperv_report_panic); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci/** 5098c2ecf20Sopenharmony_ci * hyperv_report_panic_msg - report panic message to Hyper-V 5108c2ecf20Sopenharmony_ci * @pa: physical address of the panic page containing the message 5118c2ecf20Sopenharmony_ci * @size: size of the message in the page 5128c2ecf20Sopenharmony_ci */ 5138c2ecf20Sopenharmony_civoid hyperv_report_panic_msg(phys_addr_t pa, size_t size) 5148c2ecf20Sopenharmony_ci{ 5158c2ecf20Sopenharmony_ci /* 5168c2ecf20Sopenharmony_ci * P3 to contain the physical address of the panic page & P4 to 5178c2ecf20Sopenharmony_ci * contain the size of the panic data in that page. Rest of the 5188c2ecf20Sopenharmony_ci * registers are no-op when the NOTIFY_MSG flag is set. 5198c2ecf20Sopenharmony_ci */ 5208c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P0, 0); 5218c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P1, 0); 5228c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P2, 0); 5238c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P3, pa); 5248c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_P4, size); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci /* 5278c2ecf20Sopenharmony_ci * Let Hyper-V know there is crash data available along with 5288c2ecf20Sopenharmony_ci * the panic message. 5298c2ecf20Sopenharmony_ci */ 5308c2ecf20Sopenharmony_ci wrmsrl(HV_X64_MSR_CRASH_CTL, 5318c2ecf20Sopenharmony_ci (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG)); 5328c2ecf20Sopenharmony_ci} 5338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hyperv_report_panic_msg); 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_cibool hv_is_hyperv_initialized(void) 5368c2ecf20Sopenharmony_ci{ 5378c2ecf20Sopenharmony_ci union hv_x64_msr_hypercall_contents hypercall_msr; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci /* 5408c2ecf20Sopenharmony_ci * Ensure that we're really on Hyper-V, and not a KVM or Xen 5418c2ecf20Sopenharmony_ci * emulation of Hyper-V 5428c2ecf20Sopenharmony_ci */ 5438c2ecf20Sopenharmony_ci if (x86_hyper_type != X86_HYPER_MS_HYPERV) 5448c2ecf20Sopenharmony_ci return false; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci /* 5478c2ecf20Sopenharmony_ci * Verify that earlier initialization succeeded by checking 5488c2ecf20Sopenharmony_ci * that the hypercall page is setup 5498c2ecf20Sopenharmony_ci */ 5508c2ecf20Sopenharmony_ci hypercall_msr.as_uint64 = 0; 5518c2ecf20Sopenharmony_ci rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci return hypercall_msr.enable; 5548c2ecf20Sopenharmony_ci} 5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_cibool hv_is_hibernation_supported(void) 5588c2ecf20Sopenharmony_ci{ 5598c2ecf20Sopenharmony_ci return acpi_sleep_state_supported(ACPI_STATE_S4); 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hv_is_hibernation_supported); 562