]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/hv/hv.c
Staging: hv: remove DPRINT_EXIT macro
[lisovros/linux_canprio.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 gHvContext = {
32         .SynICInitialized       = false,
33         .HypercallPage          = NULL,
34         .SignalEventParam       = NULL,
35         .SignalEventBuffer      = 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 = HvCpuIdFunctionVersionAndFeatures;
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 = HvCpuIdFunctionHvVendorAndMaxFunction;
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 = HvCpuIdFunctionHvInterface;
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 >= HvCpuIdFunctionMsHvVersion) {
111                 eax = 0;
112                 ebx = 0;
113                 ecx = 0;
114                 edx = 0;
115                 op = HvCpuIdFunctionMsHvVersion;
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 = gHvContext.HypercallPage;
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 = gHvContext.HypercallPage;
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(gHvContext.synICEventPage, 0, sizeof(void *) * MAX_NUM_CPUS);
196         memset(gHvContext.synICMessagePage, 0, sizeof(void *) * MAX_NUM_CPUS);
197
198         if (!HvQueryHypervisorPresence()) {
199                 DPRINT_ERR(VMBUS, "No Windows hypervisor detected!!");
200                 goto Cleanup;
201         }
202
203         DPRINT_INFO(VMBUS,
204                     "Windows hypervisor detected! Retrieving more info...");
205
206         maxLeaf = HvQueryHypervisorInfo();
207         /* HvQueryHypervisorFeatures(maxLeaf); */
208
209         /*
210          * We only support running on top of Hyper-V
211          */
212         rdmsrl(HV_X64_MSR_GUEST_OS_ID, gHvContext.GuestId);
213
214         if (gHvContext.GuestId != 0) {
215                 DPRINT_ERR(VMBUS, "Unknown guest id (0x%llx)!!",
216                                 gHvContext.GuestId);
217                 goto Cleanup;
218         }
219
220         /* Write our OS info */
221         wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
222         gHvContext.GuestId = HV_LINUX_GUEST_ID;
223
224         /* See if the hypercall page is already set */
225         rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.AsUINT64);
226
227         /*
228         * Allocate the hypercall page memory
229         * virtAddr = osd_PageAlloc(1);
230         */
231         virtAddr = osd_VirtualAllocExec(PAGE_SIZE);
232
233         if (!virtAddr) {
234                 DPRINT_ERR(VMBUS,
235                            "unable to allocate hypercall page!!");
236                 goto Cleanup;
237         }
238
239         hypercallMsr.Enable = 1;
240
241         hypercallMsr.GuestPhysicalAddress = vmalloc_to_pfn(virtAddr);
242         wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.AsUINT64);
243
244         /* Confirm that hypercall page did get setup. */
245         hypercallMsr.AsUINT64 = 0;
246         rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.AsUINT64);
247
248         if (!hypercallMsr.Enable) {
249                 DPRINT_ERR(VMBUS, "unable to set hypercall page!!");
250                 goto Cleanup;
251         }
252
253         gHvContext.HypercallPage = virtAddr;
254
255         DPRINT_INFO(VMBUS, "Hypercall page VA=%p, PA=0x%0llx",
256                     gHvContext.HypercallPage,
257                     (u64)hypercallMsr.GuestPhysicalAddress << PAGE_SHIFT);
258
259         /* Setup the global signal event param for the signal event hypercall */
260         gHvContext.SignalEventBuffer =
261                         kmalloc(sizeof(struct hv_input_signal_event_buffer),
262                                 GFP_KERNEL);
263         if (!gHvContext.SignalEventBuffer)
264                 goto Cleanup;
265
266         gHvContext.SignalEventParam =
267                 (struct hv_input_signal_event *)
268                         (ALIGN_UP((unsigned long)gHvContext.SignalEventBuffer,
269                                   HV_HYPERCALL_PARAM_ALIGN));
270         gHvContext.SignalEventParam->ConnectionId.Asu32 = 0;
271         gHvContext.SignalEventParam->ConnectionId.u.Id =
272                                                 VMBUS_EVENT_CONNECTION_ID;
273         gHvContext.SignalEventParam->FlagNumber = 0;
274         gHvContext.SignalEventParam->RsvdZ = 0;
275
276         return ret;
277
278 Cleanup:
279         if (virtAddr) {
280                 if (hypercallMsr.Enable) {
281                         hypercallMsr.AsUINT64 = 0;
282                         wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.AsUINT64);
283                 }
284
285                 vfree(virtAddr);
286         }
287         ret = -1;
288         return ret;
289 }
290
291 /*
292  * HvCleanup - Cleanup routine.
293  *
294  * This routine is called normally during driver unloading or exiting.
295  */
296 void HvCleanup(void)
297 {
298         union hv_x64_msr_hypercall_contents hypercallMsr;
299
300         kfree(gHvContext.SignalEventBuffer);
301         gHvContext.SignalEventBuffer = NULL;
302         gHvContext.SignalEventParam = NULL;
303
304         if (gHvContext.HypercallPage) {
305                 hypercallMsr.AsUINT64 = 0;
306                 wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.AsUINT64);
307                 vfree(gHvContext.HypercallPage);
308                 gHvContext.HypercallPage = NULL;
309         }
310 }
311
312 /*
313  * HvPostMessage - Post a message using the hypervisor message IPC.
314  *
315  * This involves a hypercall.
316  */
317 u16 HvPostMessage(union hv_connection_id connectionId,
318                   enum hv_message_type messageType,
319                   void *payload, size_t payloadSize)
320 {
321         struct alignedInput {
322                 u64 alignment8;
323                 struct hv_input_post_message msg;
324         };
325
326         struct hv_input_post_message *alignedMsg;
327         u16 status;
328         unsigned long addr;
329
330         if (payloadSize > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
331                 return -1;
332
333         addr = (unsigned long)kmalloc(sizeof(struct alignedInput), GFP_ATOMIC);
334         if (!addr)
335                 return -1;
336
337         alignedMsg = (struct hv_input_post_message *)
338                         (ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
339
340         alignedMsg->ConnectionId = connectionId;
341         alignedMsg->MessageType = messageType;
342         alignedMsg->PayloadSize = payloadSize;
343         memcpy((void *)alignedMsg->Payload, payload, payloadSize);
344
345         status = HvDoHypercall(HvCallPostMessage, alignedMsg, NULL) & 0xFFFF;
346
347         kfree((void *)addr);
348
349         return status;
350 }
351
352
353 /*
354  * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
355  *
356  * This involves a hypercall.
357  */
358 u16 HvSignalEvent(void)
359 {
360         u16 status;
361
362         status = HvDoHypercall(HvCallSignalEvent, gHvContext.SignalEventParam,
363                                NULL) & 0xFFFF;
364         return status;
365 }
366
367 /*
368  * HvSynicInit - Initialize the Synthethic Interrupt Controller.
369  *
370  * If it is already initialized by another entity (ie x2v shim), we need to
371  * retrieve the initialized message and event pages.  Otherwise, we create and
372  * initialize the message and event pages.
373  */
374 void HvSynicInit(void *irqarg)
375 {
376         u64 version;
377         union hv_synic_simp simp;
378         union hv_synic_siefp siefp;
379         union hv_synic_sint sharedSint;
380         union hv_synic_scontrol sctrl;
381
382         u32 irqVector = *((u32 *)(irqarg));
383         int cpu = smp_processor_id();
384
385         if (!gHvContext.HypercallPage)
386                 return;
387
388         /* Check the version */
389         rdmsrl(HV_X64_MSR_SVERSION, version);
390
391         DPRINT_INFO(VMBUS, "SynIC version: %llx", version);
392
393         gHvContext.synICMessagePage[cpu] = (void *)get_zeroed_page(GFP_ATOMIC);
394
395         if (gHvContext.synICMessagePage[cpu] == NULL) {
396                 DPRINT_ERR(VMBUS,
397                            "unable to allocate SYNIC message page!!");
398                 goto Cleanup;
399         }
400
401         gHvContext.synICEventPage[cpu] = (void *)get_zeroed_page(GFP_ATOMIC);
402
403         if (gHvContext.synICEventPage[cpu] == NULL) {
404                 DPRINT_ERR(VMBUS,
405                            "unable to allocate SYNIC event page!!");
406                 goto Cleanup;
407         }
408
409         /* Setup the Synic's message page */
410         rdmsrl(HV_X64_MSR_SIMP, simp.AsUINT64);
411         simp.SimpEnabled = 1;
412         simp.BaseSimpGpa = virt_to_phys(gHvContext.synICMessagePage[cpu])
413                 >> PAGE_SHIFT;
414
415         DPRINT_DBG(VMBUS, "HV_X64_MSR_SIMP msr set to: %llx", simp.AsUINT64);
416
417         wrmsrl(HV_X64_MSR_SIMP, simp.AsUINT64);
418
419         /* Setup the Synic's event page */
420         rdmsrl(HV_X64_MSR_SIEFP, siefp.AsUINT64);
421         siefp.SiefpEnabled = 1;
422         siefp.BaseSiefpGpa = virt_to_phys(gHvContext.synICEventPage[cpu])
423                 >> PAGE_SHIFT;
424
425         DPRINT_DBG(VMBUS, "HV_X64_MSR_SIEFP msr set to: %llx", siefp.AsUINT64);
426
427         wrmsrl(HV_X64_MSR_SIEFP, siefp.AsUINT64);
428
429         /* Setup the interception SINT. */
430         /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
431         /*        interceptionSint.AsUINT64); */
432
433         /* Setup the shared SINT. */
434         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.AsUINT64);
435
436         sharedSint.AsUINT64 = 0;
437         sharedSint.Vector = irqVector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
438         sharedSint.Masked = false;
439         sharedSint.AutoEoi = true;
440
441         DPRINT_DBG(VMBUS, "HV_X64_MSR_SINT1 msr set to: %llx",
442                    sharedSint.AsUINT64);
443
444         wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.AsUINT64);
445
446         /* Enable the global synic bit */
447         rdmsrl(HV_X64_MSR_SCONTROL, sctrl.AsUINT64);
448         sctrl.Enable = 1;
449
450         wrmsrl(HV_X64_MSR_SCONTROL, sctrl.AsUINT64);
451
452         gHvContext.SynICInitialized = true;
453         return;
454
455 Cleanup:
456         if (gHvContext.synICEventPage[cpu])
457                 osd_PageFree(gHvContext.synICEventPage[cpu], 1);
458
459         if (gHvContext.synICMessagePage[cpu])
460                 osd_PageFree(gHvContext.synICMessagePage[cpu], 1);
461         return;
462 }
463
464 /*
465  * HvSynicCleanup - Cleanup routine for HvSynicInit().
466  */
467 void HvSynicCleanup(void *arg)
468 {
469         union hv_synic_sint sharedSint;
470         union hv_synic_simp simp;
471         union hv_synic_siefp siefp;
472         int cpu = smp_processor_id();
473
474         if (!gHvContext.SynICInitialized)
475                 return;
476
477         rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.AsUINT64);
478
479         sharedSint.Masked = 1;
480
481         /* Need to correctly cleanup in the case of SMP!!! */
482         /* Disable the interrupt */
483         wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.AsUINT64);
484
485         rdmsrl(HV_X64_MSR_SIMP, simp.AsUINT64);
486         simp.SimpEnabled = 0;
487         simp.BaseSimpGpa = 0;
488
489         wrmsrl(HV_X64_MSR_SIMP, simp.AsUINT64);
490
491         rdmsrl(HV_X64_MSR_SIEFP, siefp.AsUINT64);
492         siefp.SiefpEnabled = 0;
493         siefp.BaseSiefpGpa = 0;
494
495         wrmsrl(HV_X64_MSR_SIEFP, siefp.AsUINT64);
496
497         osd_PageFree(gHvContext.synICMessagePage[cpu], 1);
498         osd_PageFree(gHvContext.synICEventPage[cpu], 1);
499 }