|
18 | 18 | #include <linux/slab.h> |
19 | 19 | #include <linux/hyperv.h> |
20 | 20 | #include <linux/arm-smccc.h> |
| 21 | +#include <linux/vmalloc.h> |
| 22 | +#include <linux/acpi.h> |
| 23 | +#include <linux/module.h> |
| 24 | +#include <linux/cpuhotplug.h> |
| 25 | +#include <linux/psci.h> |
21 | 26 | #include <asm-generic/bug.h> |
22 | 27 | #include <asm/hyperv-tlfs.h> |
23 | 28 | #include <asm/mshyperv.h> |
| 29 | +#include <asm/sysreg.h> |
| 30 | +#include <clocksource/hyperv_timer.h> |
24 | 31 |
|
| 32 | +static bool hyperv_initialized; |
| 33 | + |
| 34 | +struct ms_hyperv_info ms_hyperv __ro_after_init; |
| 35 | +EXPORT_SYMBOL_GPL(ms_hyperv); |
| 36 | + |
| 37 | +u32 *hv_vp_index; |
| 38 | +EXPORT_SYMBOL_GPL(hv_vp_index); |
| 39 | + |
| 40 | +u32 hv_max_vp_index; |
| 41 | +EXPORT_SYMBOL_GPL(hv_max_vp_index); |
| 42 | + |
| 43 | +static int hv_cpu_init(unsigned int cpu) |
| 44 | +{ |
| 45 | + u64 msr_vp_index; |
| 46 | + |
| 47 | + msr_vp_index = hv_get_vpreg(HV_REGISTER_VPINDEX); |
| 48 | + |
| 49 | + hv_vp_index[smp_processor_id()] = msr_vp_index; |
| 50 | + |
| 51 | + if (msr_vp_index > hv_max_vp_index) |
| 52 | + hv_max_vp_index = msr_vp_index; |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
25 | 56 |
|
26 | 57 | /* |
27 | 58 | * Functions for allocating and freeing memory with size and |
@@ -66,6 +97,107 @@ void hv_free_hyperv_page(unsigned long addr) |
66 | 97 | EXPORT_SYMBOL_GPL(hv_free_hyperv_page); |
67 | 98 |
|
68 | 99 |
|
| 100 | +/* |
| 101 | + * This function is invoked via the ACPI clocksource probe mechanism. We |
| 102 | + * don't actually use any values from the ACPI GTDT table, but we set up |
| 103 | + * the Hyper-V synthetic clocksource and do other initialization for |
| 104 | + * interacting with Hyper-V the first time. Using early_initcall to invoke |
| 105 | + * this function is too late because interrupts are already enabled at that |
| 106 | + * point, and hv_init_clocksource() must run before interrupts are enabled. |
| 107 | + * |
| 108 | + * 1. Setup the guest ID. |
| 109 | + * 2. Get features and hints info from Hyper-V |
| 110 | + * 3. Setup per-cpu VP indices. |
| 111 | + * 4. Initialize the Hyper-V clocksource. |
| 112 | + */ |
| 113 | + |
| 114 | +static int __init hyperv_init(struct acpi_table_header *table) |
| 115 | +{ |
| 116 | + struct hv_get_vp_registers_output result; |
| 117 | + u32 a, b, c, d; |
| 118 | + u64 guest_id; |
| 119 | + int i, cpuhp; |
| 120 | + |
| 121 | + /* |
| 122 | + * If we're in a VM on Hyper-V, the ACPI hypervisor_id field will |
| 123 | + * have the string "MsHyperV". |
| 124 | + */ |
| 125 | + if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8)) |
| 126 | + return -EINVAL; |
| 127 | + |
| 128 | + /* Setup the guest ID */ |
| 129 | + guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 130 | + hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id); |
| 131 | + |
| 132 | + /* Get the features and hints from Hyper-V */ |
| 133 | + hv_get_vpreg_128(HV_REGISTER_FEATURES, &result); |
| 134 | + ms_hyperv.features = result.as32.a; |
| 135 | + ms_hyperv.misc_features = result.as32.c; |
| 136 | + |
| 137 | + hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result); |
| 138 | + ms_hyperv.hints = result.as32.a; |
| 139 | + |
| 140 | + pr_info("Hyper-V: Features 0x%x, hints 0x%x, misc 0x%x\n", |
| 141 | + ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features); |
| 142 | + |
| 143 | + /* |
| 144 | + * If Hyper-V has crash notifications, set crash_kexec_post_notifiers |
| 145 | + * so that we will report the panic to Hyper-V before running kdump. |
| 146 | + */ |
| 147 | + if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) |
| 148 | + crash_kexec_post_notifiers = true; |
| 149 | + |
| 150 | + /* Get information about the Hyper-V host version */ |
| 151 | + hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result); |
| 152 | + a = result.as32.a; |
| 153 | + b = result.as32.b; |
| 154 | + c = result.as32.c; |
| 155 | + d = result.as32.d; |
| 156 | + pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n", |
| 157 | + b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24); |
| 158 | + |
| 159 | + /* Allocate and initialize percpu VP index array */ |
| 160 | + hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), |
| 161 | + GFP_KERNEL); |
| 162 | + if (!hv_vp_index) |
| 163 | + return -ENOMEM; |
| 164 | + |
| 165 | + for (i = 0; i < num_possible_cpus(); i++) |
| 166 | + hv_vp_index[i] = VP_INVAL; |
| 167 | + |
| 168 | + cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, |
| 169 | + "arm64/hyperv_init:online", hv_cpu_init, NULL); |
| 170 | + if (cpuhp < 0) |
| 171 | + goto free_vp_index; |
| 172 | + |
| 173 | + hv_init_clocksource(); |
| 174 | + if (hv_stimer_alloc()) |
| 175 | + goto remove_cpuhp_state; |
| 176 | + |
| 177 | + hyperv_initialized = true; |
| 178 | + return 0; |
| 179 | + |
| 180 | +remove_cpuhp_state: |
| 181 | + cpuhp_remove_state(cpuhp); |
| 182 | +free_vp_index: |
| 183 | + kfree(hv_vp_index); |
| 184 | + hv_vp_index = NULL; |
| 185 | + return -EINVAL; |
| 186 | +} |
| 187 | +TIMER_ACPI_DECLARE(hyperv, ACPI_SIG_GTDT, hyperv_init); |
| 188 | + |
| 189 | +/* |
| 190 | + * This routine is called before kexec/kdump, it does the required cleanup. |
| 191 | + */ |
| 192 | +void hyperv_cleanup(void) |
| 193 | +{ |
| 194 | + /* Reset our OS id */ |
| 195 | + hv_set_vpreg(HV_REGISTER_GUEST_OSID, 0); |
| 196 | + |
| 197 | +} |
| 198 | +EXPORT_SYMBOL_GPL(hyperv_cleanup); |
| 199 | + |
| 200 | + |
69 | 201 | /* |
70 | 202 | * hv_do_hypercall- Invoke the specified hypercall |
71 | 203 | */ |
@@ -291,3 +423,15 @@ void hyperv_report_panic_msg(phys_addr_t pa, size_t size) |
291 | 423 | (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG)); |
292 | 424 | } |
293 | 425 | EXPORT_SYMBOL_GPL(hyperv_report_panic_msg); |
| 426 | + |
| 427 | +bool hv_is_hyperv_initialized(void) |
| 428 | +{ |
| 429 | + return hyperv_initialized; |
| 430 | +} |
| 431 | +EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); |
| 432 | + |
| 433 | +bool hv_is_hibernation_supported(void) |
| 434 | +{ |
| 435 | + return false; |
| 436 | +} |
| 437 | +EXPORT_SYMBOL_GPL(hv_is_hibernation_supported); |
0 commit comments