|
15 | 15 | #include <linux/types.h> |
16 | 16 | #include <linux/export.h> |
17 | 17 | #include <linux/bitfield.h> |
| 18 | +#include <linux/cpumask.h> |
| 19 | +#include <linux/slab.h> |
18 | 20 | #include <asm/hyperv-tlfs.h> |
19 | 21 | #include <asm/mshyperv.h> |
20 | 22 |
|
| 23 | +/* |
| 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(). |
| 31 | + */ |
| 32 | +bool __weak hv_root_partition; |
| 33 | +EXPORT_SYMBOL_GPL(hv_root_partition); |
| 34 | + |
| 35 | +struct ms_hyperv_info __weak ms_hyperv; |
| 36 | +EXPORT_SYMBOL_GPL(ms_hyperv); |
| 37 | + |
| 38 | +u32 *hv_vp_index; |
| 39 | +EXPORT_SYMBOL_GPL(hv_vp_index); |
| 40 | + |
| 41 | +u32 hv_max_vp_index; |
| 42 | +EXPORT_SYMBOL_GPL(hv_max_vp_index); |
| 43 | + |
| 44 | +void __percpu **hyperv_pcpu_input_arg; |
| 45 | +EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg); |
| 46 | + |
| 47 | +void __percpu **hyperv_pcpu_output_arg; |
| 48 | +EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg); |
| 49 | + |
| 50 | +/* |
| 51 | + * Hyper-V specific initialization and shutdown code that is |
| 52 | + * common across all architectures. Called from architecture |
| 53 | + * specific initialization functions. |
| 54 | + */ |
| 55 | + |
| 56 | +void __init hv_common_free(void) |
| 57 | +{ |
| 58 | + kfree(hv_vp_index); |
| 59 | + hv_vp_index = NULL; |
| 60 | + |
| 61 | + free_percpu(hyperv_pcpu_output_arg); |
| 62 | + hyperv_pcpu_output_arg = NULL; |
| 63 | + |
| 64 | + free_percpu(hyperv_pcpu_input_arg); |
| 65 | + hyperv_pcpu_input_arg = NULL; |
| 66 | +} |
| 67 | + |
| 68 | +int __init hv_common_init(void) |
| 69 | +{ |
| 70 | + int i; |
| 71 | + |
| 72 | + /* |
| 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 |
| 76 | + * fatal on Hyper-V. |
| 77 | + */ |
| 78 | + hyperv_pcpu_input_arg = alloc_percpu(void *); |
| 79 | + BUG_ON(!hyperv_pcpu_input_arg); |
| 80 | + |
| 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); |
| 85 | + } |
| 86 | + |
| 87 | + hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), |
| 88 | + GFP_KERNEL); |
| 89 | + if (!hv_vp_index) { |
| 90 | + hv_common_free(); |
| 91 | + return -ENOMEM; |
| 92 | + } |
| 93 | + |
| 94 | + for (i = 0; i < num_possible_cpus(); i++) |
| 95 | + hv_vp_index[i] = VP_INVAL; |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +/* |
| 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. |
| 104 | + */ |
| 105 | + |
| 106 | +int hv_common_cpu_init(unsigned int cpu) |
| 107 | +{ |
| 108 | + void **inputarg, **outputarg; |
| 109 | + u64 msr_vp_index; |
| 110 | + gfp_t flags; |
| 111 | + int pgcount = hv_root_partition ? 2 : 1; |
| 112 | + |
| 113 | + /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ |
| 114 | + flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL; |
| 115 | + |
| 116 | + inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
| 117 | + *inputarg = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags); |
| 118 | + if (!(*inputarg)) |
| 119 | + return -ENOMEM; |
| 120 | + |
| 121 | + if (hv_root_partition) { |
| 122 | + outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg); |
| 123 | + *outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE; |
| 124 | + } |
| 125 | + |
| 126 | + msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX); |
| 127 | + |
| 128 | + hv_vp_index[cpu] = msr_vp_index; |
| 129 | + |
| 130 | + if (msr_vp_index > hv_max_vp_index) |
| 131 | + hv_max_vp_index = msr_vp_index; |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +int hv_common_cpu_die(unsigned int cpu) |
| 137 | +{ |
| 138 | + unsigned long flags; |
| 139 | + void **inputarg, **outputarg; |
| 140 | + void *mem; |
| 141 | + |
| 142 | + local_irq_save(flags); |
| 143 | + |
| 144 | + inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
| 145 | + mem = *inputarg; |
| 146 | + *inputarg = NULL; |
| 147 | + |
| 148 | + if (hv_root_partition) { |
| 149 | + outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg); |
| 150 | + *outputarg = NULL; |
| 151 | + } |
| 152 | + |
| 153 | + local_irq_restore(flags); |
| 154 | + |
| 155 | + kfree(mem); |
| 156 | + |
| 157 | + return 0; |
| 158 | +} |
21 | 159 |
|
22 | 160 | /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */ |
23 | 161 | bool hv_query_ext_cap(u64 cap_query) |
|
0 commit comments