1 // SPDX-License-Identifier: GPL-2.0
4 * Architecture neutral utility routines for interacting with
5 * Hyper-V. This file is specifically for code that must be
6 * built-in to the kernel image when CONFIG_HYPERV is set
7 * (vs. being in a module) because it is called from architecture
8 * specific code under arch/.
10 * Copyright (C) 2021, Microsoft, Inc.
12 * Author : Michael Kelley <mikelley@microsoft.com>
15 #include <linux/types.h>
16 #include <linux/export.h>
17 #include <linux/bitfield.h>
18 #include <linux/cpumask.h>
19 #include <linux/slab.h>
20 #include <asm/hyperv-tlfs.h>
21 #include <asm/mshyperv.h>
24 * hv_root_partition and ms_hyperv are defined here with other Hyper-V
25 * specific globals so they are shared across all architectures and are
26 * built only when CONFIG_HYPERV is defined. But on x86,
27 * ms_hyperv_init_platform() is built even when CONFIG_HYPERV is not
28 * defined, and it uses these two variables. So mark them as __weak
29 * here, allowing for an overriding definition in the module containing
30 * ms_hyperv_init_platform().
32 bool __weak hv_root_partition;
33 EXPORT_SYMBOL_GPL(hv_root_partition);
35 struct ms_hyperv_info __weak ms_hyperv;
36 EXPORT_SYMBOL_GPL(ms_hyperv);
39 EXPORT_SYMBOL_GPL(hv_vp_index);
42 EXPORT_SYMBOL_GPL(hv_max_vp_index);
44 void __percpu **hyperv_pcpu_input_arg;
45 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
47 void __percpu **hyperv_pcpu_output_arg;
48 EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
51 * Hyper-V specific initialization and shutdown code that is
52 * common across all architectures. Called from architecture
53 * specific initialization functions.
56 void __init hv_common_free(void)
61 free_percpu(hyperv_pcpu_output_arg);
62 hyperv_pcpu_output_arg = NULL;
64 free_percpu(hyperv_pcpu_input_arg);
65 hyperv_pcpu_input_arg = NULL;
68 int __init hv_common_init(void)
73 * Allocate the per-CPU state for the hypercall input arg.
74 * If this allocation fails, we will not be able to setup
75 * (per-CPU) hypercall input page and thus this failure is
78 hyperv_pcpu_input_arg = alloc_percpu(void *);
79 BUG_ON(!hyperv_pcpu_input_arg);
81 /* Allocate the per-CPU state for output arg for root */
82 if (hv_root_partition) {
83 hyperv_pcpu_output_arg = alloc_percpu(void *);
84 BUG_ON(!hyperv_pcpu_output_arg);
87 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
94 for (i = 0; i < num_possible_cpus(); i++)
95 hv_vp_index[i] = VP_INVAL;
101 * Hyper-V specific initialization and die code for
102 * individual CPUs that is common across all architectures.
103 * Called by the CPU hotplug mechanism.
106 int hv_common_cpu_init(unsigned int cpu)
108 void **inputarg, **outputarg;
111 int pgcount = hv_root_partition ? 2 : 1;
113 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
114 flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL;
116 inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
117 *inputarg = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
121 if (hv_root_partition) {
122 outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
123 *outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
126 msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
128 hv_vp_index[cpu] = msr_vp_index;
130 if (msr_vp_index > hv_max_vp_index)
131 hv_max_vp_index = msr_vp_index;
136 int hv_common_cpu_die(unsigned int cpu)
139 void **inputarg, **outputarg;
142 local_irq_save(flags);
144 inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
148 if (hv_root_partition) {
149 outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
153 local_irq_restore(flags);
160 /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
161 bool hv_query_ext_cap(u64 cap_query)
164 * The address of the 'hv_extended_cap' variable will be used as an
165 * output parameter to the hypercall below and so it should be
166 * compatible with 'virt_to_phys'. Which means, it's address should be
167 * directly mapped. Use 'static' to keep it compatible; stack variables
168 * can be virtually mapped, making them incompatible with
170 * Hypercall input/output addresses should also be 8-byte aligned.
172 static u64 hv_extended_cap __aligned(8);
173 static bool hv_extended_cap_queried;
177 * Querying extended capabilities is an extended hypercall. Check if the
178 * partition supports extended hypercall, first.
180 if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
183 /* Extended capabilities do not change at runtime. */
184 if (hv_extended_cap_queried)
185 return hv_extended_cap & cap_query;
187 status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
191 * The query extended capabilities hypercall should not fail under
192 * any normal circumstances. Avoid repeatedly making the hypercall, on
195 hv_extended_cap_queried = true;
196 if (!hv_result_success(status)) {
197 pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n",
202 return hv_extended_cap & cap_query;
204 EXPORT_SYMBOL_GPL(hv_query_ext_cap);