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 <asm/hypervisor.h>
10 #include <asm/mshyperv.h>
11 #include <asm/apic.h>
12
13 #include <asm/trace/hyperv.h>
14
15 /*
16 * See struct hv_deposit_memory. The first u64 is partition ID, the rest
17 * are GPAs.
18 */
19 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
20
21 /* Deposits exact number of pages. Must be called with interrupts enabled. */
hv_call_deposit_pages(int node,u64 partition_id,u32 num_pages)22 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
23 {
24 struct page **pages, *page;
25 int *counts;
26 int num_allocations;
27 int i, j, page_count;
28 int order;
29 u64 status;
30 int ret;
31 u64 base_pfn;
32 struct hv_deposit_memory *input_page;
33 unsigned long flags;
34
35 if (num_pages > HV_DEPOSIT_MAX)
36 return -E2BIG;
37 if (!num_pages)
38 return 0;
39
40 /* One buffer for page pointers and counts */
41 page = alloc_page(GFP_KERNEL);
42 if (!page)
43 return -ENOMEM;
44 pages = page_address(page);
45
46 counts = kcalloc(HV_DEPOSIT_MAX, sizeof(int), GFP_KERNEL);
47 if (!counts) {
48 free_page((unsigned long)pages);
49 return -ENOMEM;
50 }
51
52 /* Allocate all the pages before disabling interrupts */
53 i = 0;
54
55 while (num_pages) {
56 /* Find highest order we can actually allocate */
57 order = 31 - __builtin_clz(num_pages);
58
59 while (1) {
60 pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
61 if (pages[i])
62 break;
63 if (!order) {
64 ret = -ENOMEM;
65 num_allocations = i;
66 goto err_free_allocations;
67 }
68 --order;
69 }
70
71 split_page(pages[i], order);
72 counts[i] = 1 << order;
73 num_pages -= counts[i];
74 i++;
75 }
76 num_allocations = i;
77
78 local_irq_save(flags);
79
80 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
81
82 input_page->partition_id = partition_id;
83
84 /* Populate gpa_page_list - these will fit on the input page */
85 for (i = 0, page_count = 0; i < num_allocations; ++i) {
86 base_pfn = page_to_pfn(pages[i]);
87 for (j = 0; j < counts[i]; ++j, ++page_count)
88 input_page->gpa_page_list[page_count] = base_pfn + j;
89 }
90 status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
91 page_count, 0, input_page, NULL);
92 local_irq_restore(flags);
93 if (!hv_result_success(status)) {
94 pr_err("Failed to deposit pages: %lld\n", status);
95 ret = hv_result(status);
96 goto err_free_allocations;
97 }
98
99 ret = 0;
100 goto free_buf;
101
102 err_free_allocations:
103 for (i = 0; i < num_allocations; ++i) {
104 base_pfn = page_to_pfn(pages[i]);
105 for (j = 0; j < counts[i]; ++j)
106 __free_page(pfn_to_page(base_pfn + j));
107 }
108
109 free_buf:
110 free_page((unsigned long)pages);
111 kfree(counts);
112 return ret;
113 }
114
hv_call_add_logical_proc(int node,u32 lp_index,u32 apic_id)115 int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
116 {
117 struct hv_input_add_logical_processor *input;
118 struct hv_output_add_logical_processor *output;
119 u64 status;
120 unsigned long flags;
121 int ret = HV_STATUS_SUCCESS;
122
123 /*
124 * When adding a logical processor, the hypervisor may return
125 * HV_STATUS_INSUFFICIENT_MEMORY. When that happens, we deposit more
126 * pages and retry.
127 */
128 do {
129 local_irq_save(flags);
130
131 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
132 /* We don't do anything with the output right now */
133 output = *this_cpu_ptr(hyperv_pcpu_output_arg);
134
135 input->lp_index = lp_index;
136 input->apic_id = apic_id;
137 input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
138 status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
139 input, output);
140 local_irq_restore(flags);
141
142 if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
143 if (!hv_result_success(status)) {
144 pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
145 lp_index, apic_id, status);
146 ret = hv_result(status);
147 }
148 break;
149 }
150 ret = hv_call_deposit_pages(node, hv_current_partition_id, 1);
151 } while (!ret);
152
153 return ret;
154 }
155
hv_call_create_vp(int node,u64 partition_id,u32 vp_index,u32 flags)156 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
157 {
158 struct hv_create_vp *input;
159 u64 status;
160 unsigned long irq_flags;
161 int ret = HV_STATUS_SUCCESS;
162
163 /* Root VPs don't seem to need pages deposited */
164 if (partition_id != hv_current_partition_id) {
165 /* The value 90 is empirically determined. It may change. */
166 ret = hv_call_deposit_pages(node, partition_id, 90);
167 if (ret)
168 return ret;
169 }
170
171 do {
172 local_irq_save(irq_flags);
173
174 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
175
176 input->partition_id = partition_id;
177 input->vp_index = vp_index;
178 input->flags = flags;
179 input->subnode_type = HV_SUBNODE_ANY;
180 input->proximity_domain_info = hv_numa_node_to_pxm_info(node);
181 status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
182 local_irq_restore(irq_flags);
183
184 if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
185 if (!hv_result_success(status)) {
186 pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
187 vp_index, flags, status);
188 ret = hv_result(status);
189 }
190 break;
191 }
192 ret = hv_call_deposit_pages(node, partition_id, 1);
193
194 } while (!ret);
195
196 return ret;
197 }
198
199