Drivers: hv: Make portions of Hyper-V init code be arch neutral
[linux-2.6-microblaze.git] / drivers / hv / hv_common.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
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/.
9  *
10  * Copyright (C) 2021, Microsoft, Inc.
11  *
12  * Author : Michael Kelley <mikelley@microsoft.com>
13  */
14
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>
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 }
159
160 /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
161 bool hv_query_ext_cap(u64 cap_query)
162 {
163         /*
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
169          * 'virt_to_phys'.
170          * Hypercall input/output addresses should also be 8-byte aligned.
171          */
172         static u64 hv_extended_cap __aligned(8);
173         static bool hv_extended_cap_queried;
174         u64 status;
175
176         /*
177          * Querying extended capabilities is an extended hypercall. Check if the
178          * partition supports extended hypercall, first.
179          */
180         if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
181                 return false;
182
183         /* Extended capabilities do not change at runtime. */
184         if (hv_extended_cap_queried)
185                 return hv_extended_cap & cap_query;
186
187         status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
188                                  &hv_extended_cap);
189
190         /*
191          * The query extended capabilities hypercall should not fail under
192          * any normal circumstances. Avoid repeatedly making the hypercall, on
193          * error.
194          */
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",
198                        status);
199                 return false;
200         }
201
202         return hv_extended_cap & cap_query;
203 }
204 EXPORT_SYMBOL_GPL(hv_query_ext_cap);