1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/vmalloc.h>
4 #include <linux/mm.h>
5 #include <linux/clockchips.h>
6 #include <linux/slab.h>
7 #include <linux/cpuhotplug.h>
8 #include <linux/minmax.h>
9 #include <linux/export.h>
10 #include <asm/mshyperv.h>
11
12 /*
13 * See struct hv_deposit_memory. The first u64 is partition ID, the rest
14 * are GPAs.
15 */
16 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
17
18 /* Deposits exact number of pages. Must be called with interrupts enabled. */
hv_call_deposit_pages(int node,u64 partition_id,u32 num_pages)19 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
20 {
21 struct page **pages, *page;
22 int *counts;
23 int num_allocations;
24 int i, j, page_count;
25 int order;
26 u64 status;
27 int ret;
28 u64 base_pfn;
29 struct hv_deposit_memory *input_page;
30 unsigned long flags;
31
32 if (num_pages > HV_DEPOSIT_MAX)
33 return -E2BIG;
34 if (!num_pages)
35 return 0;
36
37 /* One buffer for page pointers and counts */
38 page = alloc_page(GFP_KERNEL);
39 if (!page)
40 return -ENOMEM;
41 pages = page_address(page);
42
43 counts = kzalloc_objs(int, HV_DEPOSIT_MAX);
44 if (!counts) {
45 free_page((unsigned long)pages);
46 return -ENOMEM;
47 }
48
49 /* Allocate all the pages before disabling interrupts */
50 i = 0;
51
52 while (num_pages) {
53 /* Find highest order we can actually allocate */
54 order = 31 - __builtin_clz(num_pages);
55
56 while (1) {
57 pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
58 if (pages[i])
59 break;
60 if (!order) {
61 ret = -ENOMEM;
62 num_allocations = i;
63 goto err_free_allocations;
64 }
65 --order;
66 }
67
68 split_page(pages[i], order);
69 counts[i] = 1 << order;
70 num_pages -= counts[i];
71 i++;
72 }
73 num_allocations = i;
74
75 local_irq_save(flags);
76
77 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
78
79 input_page->partition_id = partition_id;
80
81 /* Populate gpa_page_list - these will fit on the input page */
82 for (i = 0, page_count = 0; i < num_allocations; ++i) {
83 base_pfn = page_to_pfn(pages[i]);
84 for (j = 0; j < counts[i]; ++j, ++page_count)
85 input_page->gpa_page_list[page_count] = base_pfn + j;
86 }
87 status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
88 page_count, 0, input_page, NULL);
89 local_irq_restore(flags);
90 if (!hv_result_success(status)) {
91 hv_status_err(status, "\n");
92 ret = hv_result_to_errno(status);
93 goto err_free_allocations;
94 }
95
96 ret = 0;
97 goto free_buf;
98
99 err_free_allocations:
100 for (i = 0; i < num_allocations; ++i) {
101 base_pfn = page_to_pfn(pages[i]);
102 for (j = 0; j < counts[i]; ++j)
103 __free_page(pfn_to_page(base_pfn + j));
104 }
105
106 free_buf:
107 free_page((unsigned long)pages);
108 kfree(counts);
109 return ret;
110 }
111 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
112
hv_deposit_memory_node(int node,u64 partition_id,u64 hv_status)113 int hv_deposit_memory_node(int node, u64 partition_id,
114 u64 hv_status)
115 {
116 u32 num_pages = 1;
117
118 switch (hv_result(hv_status)) {
119 case HV_STATUS_INSUFFICIENT_MEMORY:
120 break;
121 case HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY:
122 num_pages = HV_MAX_CONTIGUOUS_ALLOCATION_PAGES;
123 break;
124
125 case HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY:
126 num_pages = HV_MAX_CONTIGUOUS_ALLOCATION_PAGES;
127 fallthrough;
128 case HV_STATUS_INSUFFICIENT_ROOT_MEMORY:
129 if (!hv_root_partition()) {
130 hv_status_err(hv_status, "Unexpected root memory deposit\n");
131 return -ENOMEM;
132 }
133 partition_id = HV_PARTITION_ID_SELF;
134 break;
135
136 default:
137 hv_status_err(hv_status, "Unexpected!\n");
138 return -ENOMEM;
139 }
140 return hv_call_deposit_pages(node, partition_id, num_pages);
141 }
142 EXPORT_SYMBOL_GPL(hv_deposit_memory_node);
143
hv_result_needs_memory(u64 status)144 bool hv_result_needs_memory(u64 status)
145 {
146 switch (hv_result(status)) {
147 case HV_STATUS_INSUFFICIENT_MEMORY:
148 case HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY:
149 case HV_STATUS_INSUFFICIENT_ROOT_MEMORY:
150 case HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY:
151 return true;
152 }
153 return false;
154 }
155 EXPORT_SYMBOL_GPL(hv_result_needs_memory);
156
hv_call_add_logical_proc(int node,u32 lp_index,u32 apic_id)157 int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
158 {
159 struct hv_input_add_logical_processor *input;
160 struct hv_output_add_logical_processor *output;
161 u64 status;
162 unsigned long flags;
163 int ret = 0;
164
165 /*
166 * When adding a logical processor, the hypervisor may return
167 * HV_STATUS_INSUFFICIENT_MEMORY. When that happens, we deposit more
168 * pages and retry.
169 */
170 do {
171 local_irq_save(flags);
172
173 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
174 /* We don't do anything with the output right now */
175 output = *this_cpu_ptr(hyperv_pcpu_output_arg);
176
177 input->lp_index = lp_index;
178 input->apic_id = apic_id;
179 input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
180 status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
181 input, output);
182 local_irq_restore(flags);
183
184 if (!hv_result_needs_memory(status)) {
185 if (!hv_result_success(status)) {
186 hv_status_err(status, "cpu %u apic ID: %u\n",
187 lp_index, apic_id);
188 ret = hv_result_to_errno(status);
189 }
190 break;
191 }
192 ret = hv_deposit_memory_node(node, hv_current_partition_id,
193 status);
194 } while (!ret);
195
196 return ret;
197 }
198
hv_call_create_vp(int node,u64 partition_id,u32 vp_index,u32 flags)199 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
200 {
201 struct hv_create_vp *input;
202 u64 status;
203 unsigned long irq_flags;
204 int ret = 0;
205
206 /* Root VPs don't seem to need pages deposited */
207 if (partition_id != hv_current_partition_id) {
208 /* The value 90 is empirically determined. It may change. */
209 ret = hv_call_deposit_pages(node, partition_id, 90);
210 if (ret)
211 return ret;
212 }
213
214 do {
215 local_irq_save(irq_flags);
216
217 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
218
219 input->partition_id = partition_id;
220 input->vp_index = vp_index;
221 input->flags = flags;
222 input->subnode_type = HV_SUBNODE_ANY;
223 input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
224 status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
225 local_irq_restore(irq_flags);
226
227 if (!hv_result_needs_memory(status)) {
228 if (!hv_result_success(status)) {
229 hv_status_err(status, "vcpu: %u, lp: %u\n",
230 vp_index, flags);
231 ret = hv_result_to_errno(status);
232 }
233 break;
234 }
235 ret = hv_deposit_memory_node(node, partition_id, status);
236
237 } while (!ret);
238
239 return ret;
240 }
241 EXPORT_SYMBOL_GPL(hv_call_create_vp);
242
hv_call_notify_all_processors_started(void)243 int hv_call_notify_all_processors_started(void)
244 {
245 struct hv_input_notify_partition_event *input;
246 u64 status;
247 unsigned long irq_flags;
248 int ret = 0;
249
250 local_irq_save(irq_flags);
251 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
252 memset(input, 0, sizeof(*input));
253 input->event = HV_PARTITION_ALL_LOGICAL_PROCESSORS_STARTED;
254 status = hv_do_hypercall(HVCALL_NOTIFY_PARTITION_EVENT,
255 input, NULL);
256 local_irq_restore(irq_flags);
257
258 if (!hv_result_success(status)) {
259 hv_status_err(status, "\n");
260 ret = hv_result_to_errno(status);
261 }
262 return ret;
263 }
264
hv_lp_exists(u32 lp_index)265 bool hv_lp_exists(u32 lp_index)
266 {
267 struct hv_input_get_logical_processor_run_time *input;
268 struct hv_output_get_logical_processor_run_time *output;
269 unsigned long flags;
270 u64 status;
271
272 local_irq_save(flags);
273 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
274 output = *this_cpu_ptr(hyperv_pcpu_output_arg);
275
276 input->lp_index = lp_index;
277 status = hv_do_hypercall(HVCALL_GET_LOGICAL_PROCESSOR_RUN_TIME,
278 input, output);
279 local_irq_restore(flags);
280
281 if (!hv_result_success(status) &&
282 hv_result(status) != HV_STATUS_INVALID_LP_INDEX) {
283 hv_status_err(status, "\n");
284 BUG();
285 }
286
287 return hv_result_success(status);
288 }
289