staging: hv: Convert camel cased struct fields in hv.h to lower cases
[firefly-linux-kernel-4.4.55.git] / drivers / staging / hv / hv.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  *
21  */
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include "osd.h"
27 #include "logging.h"
28 #include "vmbus_private.h"
29
30 /* The one and only */
31 struct hv_context hv_context = {
32         .synic_initialized      = false,
33         .hypercall_page         = NULL,
34         .signal_event_param     = NULL,
35         .signal_event_buffer    = NULL,
36 };
37
38 /*
39  * HvQueryHypervisorPresence - Query the cpuid for presense of windows hypervisor
40  */
41 static int HvQueryHypervisorPresence(void)
42 {
43         unsigned int eax;
44         unsigned int ebx;
45         unsigned int ecx;
46         unsigned int edx;
47         unsigned int op;
48
49         eax = 0;
50         ebx = 0;
51         ecx = 0;
52         edx = 0;
53         op = HVCPUID_VERSION_FEATURES;
54         cpuid(op, &eax, &ebx, &ecx, &edx);
55
56         return ecx & HV_PRESENT_BIT;
57 }
58
59 /*
60  * HvQueryHypervisorInfo - Get version info of the windows hypervisor
61  */
62 static int HvQueryHypervisorInfo(void)
63 {
64         unsigned int eax;
65         unsigned int ebx;
66         unsigned int ecx;
67         unsigned int edx;
68         unsigned int maxLeaf;
69         unsigned int op;
70
71         /*
72         * Its assumed that this is called after confirming that Viridian
73         * is present. Query id and revision.
74         */
75         eax = 0;
76         ebx = 0;
77         ecx = 0;
78         edx = 0;
79         op = HVCPUID_VENDOR_MAXFUNCTION;
80         cpuid(op, &eax, &ebx, &ecx, &edx);
81
82         DPRINT_INFO(VMBUS, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
83                     (ebx & 0xFF),
84                     ((ebx >> 8) & 0xFF),
85                     ((ebx >> 16) & 0xFF),
86                     ((ebx >> 24) & 0xFF),
87                     (ecx & 0xFF),
88                     ((ecx >> 8) & 0xFF),
89                     ((ecx >> 16) & 0xFF),
90                     ((ecx >> 24) & 0xFF),
91                     (edx & 0xFF),
92                     ((edx >> 8) & 0xFF),
93                     ((edx >> 16) & 0xFF),
94                     ((edx >> 24) & 0xFF));
95
96         maxLeaf = eax;
97         eax = 0;
98         ebx = 0;
99         ecx = 0;
100         edx = 0;
101         op = HVCPUID_INTERFACE;
102         cpuid(op, &eax, &ebx, &ecx, &edx);
103
104         DPRINT_INFO(VMBUS, "Interface ID: %c%c%c%c",
105                     (eax & 0xFF),
106                     ((eax >> 8) & 0xFF),
107                     ((eax >> 16) & 0xFF),
108                     ((eax >> 24) & 0xFF));
109
110         if (maxLeaf >= HVCPUID_VERSION) {
111                 eax = 0;
112                 ebx = 0;
113                 ecx = 0;
114                 edx = 0;
115                 op = HVCPUID_VERSION;
116                 cpuid(op, &eax, &ebx, &ecx, &edx);
117                 DPRINT_INFO(VMBUS, "OS Build:%d-%d.%d-%d-%d.%d",\
118                             eax,
119                             ebx >> 16,
120                             ebx & 0xFFFF,
121                             ecx,
122                             edx >> 24,
123                             edx & 0xFFFFFF);
124         }
125         return maxLeaf;
126 }
127
128 /*
129  * HvDoHypercall - Invoke the specified hypercall
130  */
131 static u64 HvDoHypercall(u64 Control, void *Input, void *Output)
132 {
133 #ifdef CONFIG_X86_64
134         u64 hvStatus = 0;
135         u64 inputAddress = (Input) ? virt_to_phys(Input) : 0;
136         u64 outputAddress = (Output) ? virt_to_phys(Output) : 0;
137         volatile void *hypercallPage = hv_context.hypercall_page;
138
139         DPRINT_DBG(VMBUS, "Hypercall <control %llx input phys %llx virt %p "
140                    "output phys %llx virt %p hypercall %p>",
141                    Control, inputAddress, Input,
142                    outputAddress, Output, hypercallPage);
143
144         __asm__ __volatile__("mov %0, %%r8" : : "r" (outputAddress) : "r8");
145         __asm__ __volatile__("call *%3" : "=a" (hvStatus) :
146                              "c" (Control), "d" (inputAddress),
147                              "m" (hypercallPage));
148
149         DPRINT_DBG(VMBUS, "Hypercall <return %llx>",  hvStatus);
150
151         return hvStatus;
152
153 #else
154
155         u32 controlHi = Control >> 32;
156         u32 controlLo = Control & 0xFFFFFFFF;
157         u32 hvStatusHi = 1;
158         u32 hvStatusLo = 1;
159         u64 inputAddress = (Input) ? virt_to_phys(Input) : 0;
160         u32 inputAddressHi = inputAddress >> 32;
161         u32 inputAddressLo = inputAddress & 0xFFFFFFFF;
162         u64 outputAddress = (Output) ? virt_to_phys(Output) : 0;
163         u32 outputAddressHi = outputAddress >> 32;
164         u32 outputAddressLo = outputAddress & 0xFFFFFFFF;
165         volatile void *hypercallPage = hv_context.hypercall_page;
166
167         DPRINT_DBG(VMBUS, "Hypercall <control %llx input %p output %p>",
168                    Control, Input, Output);
169
170         __asm__ __volatile__ ("call *%8" : "=d"(hvStatusHi),
171                               "=a"(hvStatusLo) : "d" (controlHi),
172                               "a" (controlLo), "b" (inputAddressHi),
173                               "c" (inputAddressLo), "D"(outputAddressHi),
174                               "S"(outputAddressLo), "m" (hypercallPage));
175
176         DPRINT_DBG(VMBUS, "Hypercall <return %llx>",
177                    hvStatusLo | ((u64)hvStatusHi << 32));
178
179         return hvStatusLo | ((u64)hvStatusHi << 32);
180 #endif /* !x86_64 */
181 }
182
183 /*
184  * HvInit - Main initialization routine.
185  *
186  * This routine must be called before any other routines in here are called
187  */
188 int HvInit(void)
189 {
190         int ret = 0;
191         int maxLeaf;
192         union hv_x64_msr_hypercall_contents hypercallMsr;
193         void *virtAddr = NULL;
194
195         memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS);
196         memset(hv_context.synic_message_page, 0,
197                sizeof(void *) * MAX_NUM_CPUS);
198
199         if (!HvQueryHypervisorPresence()) {
200                 DPRINT_ERR(VMBUS, "No Windows hypervisor detected!!");
201                 goto Cleanup;
202         }
203
204         DPRINT_INFO(VMBUS,
205                     "Windows hypervisor detected! Retrieving more info...");
206
207         maxLeaf = HvQueryHypervisorInfo();
208         /* HvQueryHypervisorFeatures(maxLeaf); */
209
210         /*
211          * We only support running on top of Hyper-V
212          */
213         rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
214
215         if (hv_context.guestid != 0) {
216                 DPRINT_ERR(VMBUS, "Unknown guest id (0x%llx)!!",
217                                 hv_context.guestid);
218                 goto Cleanup;
219         }
220
221         /* Write our OS info */
222         wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
223         hv_context.guestid = HV_LINUX_GUEST_ID;
224
225         /* See if the hypercall page is already set */
226         rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
227
228         /*
229         * Allocate the hypercall page memory
230         * virtAddr = osd_PageAlloc(1);
231         */
232         virtAddr = osd_VirtualAllocExec(PAGE_SIZE);
233
234         if (!virtAddr) {
235                 DPRINT_ERR(VMBUS,
236                            "unable to allocate hypercall page!!");
237                 goto Cleanup;
238         }
239
240         hypercallMsr.enable = 1;
241
242         hypercallMsr.guest_physical_address = vmalloc_to_pfn(virtAddr);
243         wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
244
245         /* Confirm that hypercall page did get setup. */
246         hypercallMsr.as_uint64 = 0;
247         rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
248
249         if (!hypercallMsr.enable) {
250                 DPRINT_ERR(VMBUS, "unable to set hypercall page!!");
251                 goto Cleanup;
252         }
253
254         hv_context.hypercall_page = virtAddr;
255
256         DPRINT_INFO(VMBUS, "Hypercall page VA=%p, PA=0x%0llx",
257                     hv_context.hypercall_page,
258                     (u64)hypercallMsr.guest_physical_address << PAGE_SHIFT);
259
260         /* Setup the global signal event param for the signal event hypercall */
261         hv_context.signal_event_buffer =
262                         kmalloc(sizeof(struct hv_input_signal_event_buffer),
263                                 GFP_KERNEL);
264         if (!hv_context.signal_event_buffer)
265                 goto Cleanup;
266
267         hv_context.signal_event_param =
268                 (struct hv_input_signal_event *)
269                         (ALIGN_UP((unsigned long)
270                                   hv_context.signal_event_buffer,
271                                   HV_HYPERCALL_PARAM_ALIGN));
272         hv_context.signal_event_param->connectionid.asu32 = 0;
273         hv_context.signal_event_param->connectionid.u.id =
274                                                 VMBUS_EVENT_CONNECTION_ID;
275         hv_context.signal_event_param->flag_number = 0;
276         hv_context.signal_event_param->rsvdz = 0;
277
278         return ret;
279
280 Cleanup:
281         if (virtAddr) {
282                 if (hypercallMsr.enable) {
283                         hypercallMsr.as_uint64 = 0;
284                         wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
285                 }
286
287                 vfree(virtAddr);
288         }
289         ret = -1;
290         return ret;
291 }
292
293 /*
294  * HvCleanup - Cleanup routine.
295  *
296  * This routine is called normally during driver unloading or exiting.
297  */
298 void HvCleanup(void)
299 {
300         union hv_x64_msr_hypercall_contents hypercallMsr;
301
302         kfree(hv_context.signal_event_buffer);
303         hv_context.signal_event_buffer = NULL;
304         hv_context.signal_event_param = NULL;
305
306         if (hv_context.hypercall_page) {
307                 hypercallMsr.as_uint64 = 0;
308                 wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
309                 vfree(hv_context.hypercall_page);
310                 hv_context.hypercall_page = NULL;
311         }
312 }
313
314 /*
315  * HvPostMessage - Post a message using the hypervisor message IPC.
316  *
317  * This involves a hypercall.
318  */
319 u16 HvPostMessage(union hv_connection_id connectionId,
320                   enum hv_message_type messageType,
321                   void *payload, size_t payloadSize)
322 {
323         struct alignedInput {
324                 u64 alignment8;
325                 struct hv_input_post_message msg;
326         };
327
328         struct hv_input_post_message *alignedMsg;
329         u16 status;
330         unsigned long addr;
331
332         if (payloadSize > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
333                 return -1;
334
335         addr = (unsigned long)kmalloc(sizeof(struct alignedInput), GFP_ATOMIC);
336         if (!addr)
337                 return -1;
338
339         alignedMsg = (struct hv_input_post_message *)
340                         (ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
341
342         alignedMsg->connectionid = connectionId;
343         alignedMsg->message_type = messageType;
344         alignedMsg->payload_size = payloadSize;
345         memcpy((void *)alignedMsg->payload, payload, payloadSize);
346
347         status = HvDoHypercall(HVCALL_POST_MESSAGE, alignedMsg, NULL) & 0xFFFF;
348
349         kfree((void *)addr);
350
351         return status;
352 }
353
354
355 /*
356  * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
357  *
358  * This involves a hypercall.
359  */
360 u16 HvSignalEvent(void)
361 {
362         u16 status;
363
364         status = HvDoHypercall(HVCALL_SIGNAL_EVENT,
365                                hv_context.signal_event_param,
366                                NULL) & 0xFFFF;
367         return status;
368 }
369
370 /*
371  * HvSynicInit - Initialize the Synthethic Interrupt Controller.
372  *
373  * If it is already initialized by another entity (ie x2v shim), we need to
374  * retrieve the initialized message and event pages.  Otherwise, we create and
375  * initialize the message and event pages.
376  */
377 void HvSynicInit(void *irqarg)
378 {
379         u64 version;
380         union hv_synic_simp simp;
381         union hv_synic_siefp siefp;
382         union hv_synic_sint sharedSint;
383         union hv_synic_scontrol sctrl;
384
385         u32 irqVector = *((u32 *)(irqarg));
386         int cpu = smp_processor_id();
387
388         if (!hv_context.hypercall_page)
389                 return;
390
391         /* Check the version */
392         rdmsrl(HV_X64_MSR_SVERSION, version);
393
394         DPRINT_INFO(VMBUS, "SynIC version: %llx", version);
395
396         hv_context.synic_message_page[cpu] =
397                 (void *)get_zeroed_page(GFP_ATOMIC);
398
399         if (hv_context.synic_message_page[cpu] == NULL) {
400                 DPRINT_ERR(VMBUS,
401                            "unable to allocate SYNIC message page!!");
402                 goto Cleanup;
403         }
404
405         hv_context.synic_event_page[cpu] =
406                 (void *)get_zeroed_page(GFP_ATOMIC);
407
408         if (hv_context.synic_event_page[cpu] == NULL) {
409                 DPRINT_ERR(VMBUS,
410                            "unable to allocate SYNIC event page!!");
411                 goto Cleanup;
412         }
413
414         /* Setup the Synic's message page */
415         rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
416         simp.simp_enabled = 1;
417         simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
418                 >> PAGE_SHIFT;
419
420         DPRINT_DBG(VMBUS, "HV_X64_MSR_SIMP msr set to: %llx", simp.as_uint64);
421
422         wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
423
424         /* Setup the Synic's event page */
425         rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
426         siefp.siefp_enabled = 1;
427         siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
428                 >> PAGE_SHIFT;
429
430         DPRINT_DBG(VMBUS, "HV_X64_MSR_SIEFP msr set to: %llx", siefp.as_uint64);
431
432         wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
433
434         /* Setup the interception SINT. */
435         /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
436         /*        interceptionSint.as_uint64); */
437
438         /* Setup the shared SINT. */
439         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
440
441         sharedSint.as_uint64 = 0;
442         sharedSint.vector = irqVector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
443         sharedSint.masked = false;
444         sharedSint.auto_eoi = true;
445
446         DPRINT_DBG(VMBUS, "HV_X64_MSR_SINT1 msr set to: %llx",
447                    sharedSint.as_uint64);
448
449         wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
450
451         /* Enable the global synic bit */
452         rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
453         sctrl.enable = 1;
454
455         wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
456
457         hv_context.synic_initialized = true;
458         return;
459
460 Cleanup:
461         if (hv_context.synic_event_page[cpu])
462                 osd_PageFree(hv_context.synic_event_page[cpu], 1);
463
464         if (hv_context.synic_message_page[cpu])
465                 osd_PageFree(hv_context.synic_message_page[cpu], 1);
466         return;
467 }
468
469 /*
470  * HvSynicCleanup - Cleanup routine for HvSynicInit().
471  */
472 void HvSynicCleanup(void *arg)
473 {
474         union hv_synic_sint sharedSint;
475         union hv_synic_simp simp;
476         union hv_synic_siefp siefp;
477         int cpu = smp_processor_id();
478
479         if (!hv_context.synic_initialized)
480                 return;
481
482         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
483
484         sharedSint.masked = 1;
485
486         /* Need to correctly cleanup in the case of SMP!!! */
487         /* Disable the interrupt */
488         wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
489
490         rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
491         simp.simp_enabled = 0;
492         simp.base_simp_gpa = 0;
493
494         wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
495
496         rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
497         siefp.siefp_enabled = 0;
498         siefp.base_siefp_gpa = 0;
499
500         wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
501
502         osd_PageFree(hv_context.synic_message_page[cpu], 1);
503         osd_PageFree(hv_context.synic_event_page[cpu], 1);
504 }