0c1fa69381f7fdbab99d7c43c8f3e23547926382
[linux-2.6-microblaze.git] / drivers / hv / hv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
8  */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/hyperv.h>
16 #include <linux/version.h>
17 #include <linux/random.h>
18 #include <linux/clockchips.h>
19 #include <clocksource/hyperv_timer.h>
20 #include <asm/mshyperv.h>
21 #include "hyperv_vmbus.h"
22
23 /* The one and only */
24 struct hv_context hv_context;
25
26 /*
27  * hv_init - Main initialization routine.
28  *
29  * This routine must be called before any other routines in here are called
30  */
31 int hv_init(void)
32 {
33         hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
34         if (!hv_context.cpu_context)
35                 return -ENOMEM;
36         return 0;
37 }
38
39 /*
40  * Functions for allocating and freeing memory with size and
41  * alignment HV_HYP_PAGE_SIZE. These functions are needed because
42  * the guest page size may not be the same as the Hyper-V page
43  * size. We depend upon kmalloc() aligning power-of-two size
44  * allocations to the allocation size boundary, so that the
45  * allocated memory appears to Hyper-V as a page of the size
46  * it expects.
47  */
48
49 void *hv_alloc_hyperv_page(void)
50 {
51         BUILD_BUG_ON(PAGE_SIZE <  HV_HYP_PAGE_SIZE);
52
53         if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
54                 return (void *)__get_free_page(GFP_KERNEL);
55         else
56                 return kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
57 }
58
59 void *hv_alloc_hyperv_zeroed_page(void)
60 {
61         if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
62                 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
63         else
64                 return kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
65 }
66
67 void hv_free_hyperv_page(unsigned long addr)
68 {
69         if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
70                 free_page(addr);
71         else
72                 kfree((void *)addr);
73 }
74
75 /*
76  * hv_post_message - Post a message using the hypervisor message IPC.
77  *
78  * This involves a hypercall.
79  */
80 int hv_post_message(union hv_connection_id connection_id,
81                   enum hv_message_type message_type,
82                   void *payload, size_t payload_size)
83 {
84         struct hv_input_post_message *aligned_msg;
85         struct hv_per_cpu_context *hv_cpu;
86         u64 status;
87
88         if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
89                 return -EMSGSIZE;
90
91         hv_cpu = get_cpu_ptr(hv_context.cpu_context);
92         aligned_msg = hv_cpu->post_msg_page;
93         aligned_msg->connectionid = connection_id;
94         aligned_msg->reserved = 0;
95         aligned_msg->message_type = message_type;
96         aligned_msg->payload_size = payload_size;
97         memcpy((void *)aligned_msg->payload, payload, payload_size);
98
99         status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
100
101         /* Preemption must remain disabled until after the hypercall
102          * so some other thread can't get scheduled onto this cpu and
103          * corrupt the per-cpu post_msg_page
104          */
105         put_cpu_ptr(hv_cpu);
106
107         return status & 0xFFFF;
108 }
109
110 int hv_synic_alloc(void)
111 {
112         int cpu;
113         struct hv_per_cpu_context *hv_cpu;
114
115         /*
116          * First, zero all per-cpu memory areas so hv_synic_free() can
117          * detect what memory has been allocated and cleanup properly
118          * after any failures.
119          */
120         for_each_present_cpu(cpu) {
121                 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
122                 memset(hv_cpu, 0, sizeof(*hv_cpu));
123         }
124
125         hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
126                                          GFP_KERNEL);
127         if (hv_context.hv_numa_map == NULL) {
128                 pr_err("Unable to allocate NUMA map\n");
129                 goto err;
130         }
131
132         for_each_present_cpu(cpu) {
133                 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
134
135                 tasklet_init(&hv_cpu->msg_dpc,
136                              vmbus_on_msg_dpc, (unsigned long) hv_cpu);
137
138                 hv_cpu->synic_message_page =
139                         (void *)get_zeroed_page(GFP_ATOMIC);
140                 if (hv_cpu->synic_message_page == NULL) {
141                         pr_err("Unable to allocate SYNIC message page\n");
142                         goto err;
143                 }
144
145                 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
146                 if (hv_cpu->synic_event_page == NULL) {
147                         pr_err("Unable to allocate SYNIC event page\n");
148                         goto err;
149                 }
150
151                 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
152                 if (hv_cpu->post_msg_page == NULL) {
153                         pr_err("Unable to allocate post msg page\n");
154                         goto err;
155                 }
156         }
157
158         return 0;
159 err:
160         /*
161          * Any memory allocations that succeeded will be freed when
162          * the caller cleans up by calling hv_synic_free()
163          */
164         return -ENOMEM;
165 }
166
167
168 void hv_synic_free(void)
169 {
170         int cpu;
171
172         for_each_present_cpu(cpu) {
173                 struct hv_per_cpu_context *hv_cpu
174                         = per_cpu_ptr(hv_context.cpu_context, cpu);
175
176                 free_page((unsigned long)hv_cpu->synic_event_page);
177                 free_page((unsigned long)hv_cpu->synic_message_page);
178                 free_page((unsigned long)hv_cpu->post_msg_page);
179         }
180
181         kfree(hv_context.hv_numa_map);
182 }
183
184 /*
185  * hv_synic_init - Initialize the Synthetic Interrupt Controller.
186  *
187  * If it is already initialized by another entity (ie x2v shim), we need to
188  * retrieve the initialized message and event pages.  Otherwise, we create and
189  * initialize the message and event pages.
190  */
191 void hv_synic_enable_regs(unsigned int cpu)
192 {
193         struct hv_per_cpu_context *hv_cpu
194                 = per_cpu_ptr(hv_context.cpu_context, cpu);
195         union hv_synic_simp simp;
196         union hv_synic_siefp siefp;
197         union hv_synic_sint shared_sint;
198         union hv_synic_scontrol sctrl;
199
200         /* Setup the Synic's message page */
201         simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
202         simp.simp_enabled = 1;
203         simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
204                 >> HV_HYP_PAGE_SHIFT;
205
206         hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
207
208         /* Setup the Synic's event page */
209         siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
210         siefp.siefp_enabled = 1;
211         siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
212                 >> HV_HYP_PAGE_SHIFT;
213
214         hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
215
216         /* Setup the shared SINT. */
217         shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
218                                         VMBUS_MESSAGE_SINT);
219
220         shared_sint.vector = hv_get_vector();
221         shared_sint.masked = false;
222         shared_sint.auto_eoi = hv_recommend_using_aeoi();
223         hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
224                                 shared_sint.as_uint64);
225
226         /* Enable the global synic bit */
227         sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
228         sctrl.enable = 1;
229
230         hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
231 }
232
233 int hv_synic_init(unsigned int cpu)
234 {
235         hv_synic_enable_regs(cpu);
236
237         hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
238
239         return 0;
240 }
241
242 /*
243  * hv_synic_cleanup - Cleanup routine for hv_synic_init().
244  */
245 void hv_synic_disable_regs(unsigned int cpu)
246 {
247         union hv_synic_sint shared_sint;
248         union hv_synic_simp simp;
249         union hv_synic_siefp siefp;
250         union hv_synic_scontrol sctrl;
251
252         shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
253                                         VMBUS_MESSAGE_SINT);
254
255         shared_sint.masked = 1;
256
257         /* Need to correctly cleanup in the case of SMP!!! */
258         /* Disable the interrupt */
259         hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
260                                 shared_sint.as_uint64);
261
262         simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
263         simp.simp_enabled = 0;
264         simp.base_simp_gpa = 0;
265
266         hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
267
268         siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
269         siefp.siefp_enabled = 0;
270         siefp.base_siefp_gpa = 0;
271
272         hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
273
274         /* Disable the global synic bit */
275         sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
276         sctrl.enable = 0;
277         hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
278 }
279
280
281 int hv_synic_cleanup(unsigned int cpu)
282 {
283         struct vmbus_channel *channel, *sc;
284         bool channel_found = false;
285
286         /*
287          * Hyper-V does not provide a way to change the connect CPU once
288          * it is set; we must prevent the connect CPU from going offline
289          * while the VM is running normally. But in the panic or kexec()
290          * path where the vmbus is already disconnected, the CPU must be
291          * allowed to shut down.
292          */
293         if (cpu == VMBUS_CONNECT_CPU &&
294             vmbus_connection.conn_state == CONNECTED)
295                 return -EBUSY;
296
297         /*
298          * Search for channels which are bound to the CPU we're about to
299          * cleanup.  In case we find one and vmbus is still connected, we
300          * fail; this will effectively prevent CPU offlining.
301          *
302          * TODO: Re-bind the channels to different CPUs.
303          */
304         mutex_lock(&vmbus_connection.channel_mutex);
305         list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
306                 if (channel->target_cpu == cpu) {
307                         channel_found = true;
308                         break;
309                 }
310                 list_for_each_entry(sc, &channel->sc_list, sc_list) {
311                         if (sc->target_cpu == cpu) {
312                                 channel_found = true;
313                                 break;
314                         }
315                 }
316                 if (channel_found)
317                         break;
318         }
319         mutex_unlock(&vmbus_connection.channel_mutex);
320
321         if (channel_found && vmbus_connection.conn_state == CONNECTED)
322                 return -EBUSY;
323
324         hv_stimer_legacy_cleanup(cpu);
325
326         hv_synic_disable_regs(cpu);
327
328         return 0;
329 }