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