2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
22 #include <linux/kernel.h>
24 #include <linux/vmalloc.h>
27 #include "VmbusPrivate.h"
29 /* The one and only */
30 struct hv_context gHvContext
= {
31 .SynICInitialized
= false,
32 .HypercallPage
= NULL
,
33 .SignalEventParam
= NULL
,
34 .SignalEventBuffer
= NULL
,
38 * HvQueryHypervisorPresence - Query the cpuid for presense of windows hypervisor
40 static int HvQueryHypervisorPresence(void)
52 op
= HvCpuIdFunctionVersionAndFeatures
;
53 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
55 return ecx
& HV_PRESENT_BIT
;
59 * HvQueryHypervisorInfo - Get version info of the windows hypervisor
61 static int HvQueryHypervisorInfo(void)
71 * Its assumed that this is called after confirming that Viridian
72 * is present. Query id and revision.
78 op
= HvCpuIdFunctionHvVendorAndMaxFunction
;
79 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
81 DPRINT_INFO(VMBUS
, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
93 ((edx
>> 24) & 0xFF));
100 op
= HvCpuIdFunctionHvInterface
;
101 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
103 DPRINT_INFO(VMBUS
, "Interface ID: %c%c%c%c",
106 ((eax
>> 16) & 0xFF),
107 ((eax
>> 24) & 0xFF));
109 if (maxLeaf
>= HvCpuIdFunctionMsHvVersion
) {
114 op
= HvCpuIdFunctionMsHvVersion
;
115 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
116 DPRINT_INFO(VMBUS
, "OS Build:%d-%d.%d-%d-%d.%d",\
128 * HvDoHypercall - Invoke the specified hypercall
130 static u64
HvDoHypercall(u64 Control
, void *Input
, void *Output
)
134 u64 inputAddress
= (Input
) ? virt_to_phys(Input
) : 0;
135 u64 outputAddress
= (Output
) ? virt_to_phys(Output
) : 0;
136 volatile void *hypercallPage
= gHvContext
.HypercallPage
;
138 DPRINT_DBG(VMBUS
, "Hypercall <control %llx input phys %llx virt %p "
139 "output phys %llx virt %p hypercall %p>",
140 Control
, inputAddress
, Input
,
141 outputAddress
, Output
, hypercallPage
);
143 __asm__
__volatile__("mov %0, %%r8" : : "r" (outputAddress
) : "r8");
144 __asm__
__volatile__("call *%3" : "=a" (hvStatus
) :
145 "c" (Control
), "d" (inputAddress
),
146 "m" (hypercallPage
));
148 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>", hvStatus
);
154 u32 controlHi
= Control
>> 32;
155 u32 controlLo
= Control
& 0xFFFFFFFF;
158 u64 inputAddress
= (Input
) ? virt_to_phys(Input
) : 0;
159 u32 inputAddressHi
= inputAddress
>> 32;
160 u32 inputAddressLo
= inputAddress
& 0xFFFFFFFF;
161 u64 outputAddress
= (Output
) ? virt_to_phys(Output
) : 0;
162 u32 outputAddressHi
= outputAddress
>> 32;
163 u32 outputAddressLo
= outputAddress
& 0xFFFFFFFF;
164 volatile void *hypercallPage
= gHvContext
.HypercallPage
;
166 DPRINT_DBG(VMBUS
, "Hypercall <control %llx input %p output %p>",
167 Control
, Input
, Output
);
169 __asm__
__volatile__ ("call *%8" : "=d"(hvStatusHi
),
170 "=a"(hvStatusLo
) : "d" (controlHi
),
171 "a" (controlLo
), "b" (inputAddressHi
),
172 "c" (inputAddressLo
), "D"(outputAddressHi
),
173 "S"(outputAddressLo
), "m" (hypercallPage
));
175 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>",
176 hvStatusLo
| ((u64
)hvStatusHi
<< 32));
178 return hvStatusLo
| ((u64
)hvStatusHi
<< 32);
183 * HvInit - Main initialization routine.
185 * This routine must be called before any other routines in here are called
191 union hv_x64_msr_hypercall_contents hypercallMsr
;
192 void *virtAddr
= NULL
;
196 memset(gHvContext
.synICEventPage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
197 memset(gHvContext
.synICMessagePage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
199 if (!HvQueryHypervisorPresence()) {
200 DPRINT_ERR(VMBUS
, "No Windows hypervisor detected!!");
205 "Windows hypervisor detected! Retrieving more info...");
207 maxLeaf
= HvQueryHypervisorInfo();
208 /* HvQueryHypervisorFeatures(maxLeaf); */
211 * Determine if we are running on xenlinux (ie x2v shim) or native
214 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, gHvContext
.GuestId
);
215 if (gHvContext
.GuestId
== 0) {
216 /* Write our OS info */
217 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, HV_LINUX_GUEST_ID
);
218 gHvContext
.GuestId
= HV_LINUX_GUEST_ID
;
221 /* See if the hypercall page is already set */
222 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
223 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
) {
224 /* Allocate the hypercall page memory */
225 /* virtAddr = osd_PageAlloc(1); */
226 virtAddr
= osd_VirtualAllocExec(PAGE_SIZE
);
230 "unable to allocate hypercall page!!");
234 hypercallMsr
.Enable
= 1;
235 /* hypercallMsr.GuestPhysicalAddress =
236 * virt_to_phys(virtAddr) >> PAGE_SHIFT; */
237 hypercallMsr
.GuestPhysicalAddress
= vmalloc_to_pfn(virtAddr
);
238 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
240 /* Confirm that hypercall page did get setup. */
241 hypercallMsr
.AsUINT64
= 0;
242 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
243 if (!hypercallMsr
.Enable
) {
244 DPRINT_ERR(VMBUS
, "unable to set hypercall page!!");
248 gHvContext
.HypercallPage
= virtAddr
;
250 DPRINT_ERR(VMBUS
, "Unknown guest id (0x%llx)!!",
255 DPRINT_INFO(VMBUS
, "Hypercall page VA=%p, PA=0x%0llx",
256 gHvContext
.HypercallPage
,
257 (u64
)hypercallMsr
.GuestPhysicalAddress
<< PAGE_SHIFT
);
259 /* Setup the global signal event param for the signal event hypercall */
260 gHvContext
.SignalEventBuffer
=
261 kmalloc(sizeof(struct hv_input_signal_event_buffer
),
263 if (!gHvContext
.SignalEventBuffer
)
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;
276 /* DPRINT_DBG(VMBUS, "My id %llu", HvGetCurrentPartitionId()); */
284 if (hypercallMsr
.Enable
) {
285 hypercallMsr
.AsUINT64
= 0;
286 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
298 * HvCleanup - Cleanup routine.
300 * This routine is called normally during driver unloading or exiting.
304 union hv_x64_msr_hypercall_contents hypercallMsr
;
308 if (gHvContext
.SignalEventBuffer
) {
309 gHvContext
.SignalEventBuffer
= NULL
;
310 gHvContext
.SignalEventParam
= NULL
;
311 kfree(gHvContext
.SignalEventBuffer
);
314 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
) {
315 if (gHvContext
.HypercallPage
) {
316 hypercallMsr
.AsUINT64
= 0;
317 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
318 vfree(gHvContext
.HypercallPage
);
319 gHvContext
.HypercallPage
= NULL
;
328 * HvPostMessage - Post a message using the hypervisor message IPC.
330 * This involves a hypercall.
332 u16
HvPostMessage(union hv_connection_id connectionId
,
333 enum hv_message_type messageType
,
334 void *payload
, size_t payloadSize
)
336 struct alignedInput
{
338 struct hv_input_post_message msg
;
341 struct hv_input_post_message
*alignedMsg
;
345 if (payloadSize
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
348 addr
= (unsigned long)kmalloc(sizeof(struct alignedInput
), GFP_ATOMIC
);
352 alignedMsg
= (struct hv_input_post_message
*)
353 (ALIGN_UP(addr
, HV_HYPERCALL_PARAM_ALIGN
));
355 alignedMsg
->ConnectionId
= connectionId
;
356 alignedMsg
->MessageType
= messageType
;
357 alignedMsg
->PayloadSize
= payloadSize
;
358 memcpy((void *)alignedMsg
->Payload
, payload
, payloadSize
);
360 status
= HvDoHypercall(HvCallPostMessage
, alignedMsg
, NULL
) & 0xFFFF;
369 * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
371 * This involves a hypercall.
373 u16
HvSignalEvent(void)
377 status
= HvDoHypercall(HvCallSignalEvent
, gHvContext
.SignalEventParam
,
383 * HvSynicInit - Initialize the Synthethic Interrupt Controller.
385 * If it is already initialized by another entity (ie x2v shim), we need to
386 * retrieve the initialized message and event pages. Otherwise, we create and
387 * initialize the message and event pages.
389 int HvSynicInit(u32 irqVector
)
392 union hv_synic_simp simp
;
393 union hv_synic_siefp siefp
;
394 union hv_synic_sint sharedSint
;
395 union hv_synic_scontrol sctrl
;
401 if (!gHvContext
.HypercallPage
) {
406 /* Check the version */
407 rdmsrl(HV_X64_MSR_SVERSION
, version
);
409 DPRINT_INFO(VMBUS
, "SynIC version: %llx", version
);
411 /* TODO: Handle SMP */
412 if (gHvContext
.GuestId
== HV_XENLINUX_GUEST_ID
) {
413 DPRINT_INFO(VMBUS
, "Skipping SIMP and SIEFP setup since "
414 "it is already set.");
416 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
417 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
419 DPRINT_DBG(VMBUS
, "Simp: %llx, Sifep: %llx",
420 simp
.AsUINT64
, siefp
.AsUINT64
);
423 * Determine if we are running on xenlinux (ie x2v shim) or
426 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, guestID
);
427 if (guestID
== HV_LINUX_GUEST_ID
) {
428 gHvContext
.synICMessagePage
[0] =
429 phys_to_virt(simp
.BaseSimpGpa
<< PAGE_SHIFT
);
430 gHvContext
.synICEventPage
[0] =
431 phys_to_virt(siefp
.BaseSiefpGpa
<< PAGE_SHIFT
);
433 DPRINT_ERR(VMBUS
, "unknown guest id!!");
436 DPRINT_DBG(VMBUS
, "MAPPED: Simp: %p, Sifep: %p",
437 gHvContext
.synICMessagePage
[0],
438 gHvContext
.synICEventPage
[0]);
440 gHvContext
.synICMessagePage
[0] = osd_PageAlloc(1);
441 if (gHvContext
.synICMessagePage
[0] == NULL
) {
443 "unable to allocate SYNIC message page!!");
447 gHvContext
.synICEventPage
[0] = osd_PageAlloc(1);
448 if (gHvContext
.synICEventPage
[0] == NULL
) {
450 "unable to allocate SYNIC event page!!");
454 /* Setup the Synic's message page */
455 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
456 simp
.SimpEnabled
= 1;
457 simp
.BaseSimpGpa
= virt_to_phys(gHvContext
.synICMessagePage
[0])
460 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIMP msr set to: %llx",
463 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
465 /* Setup the Synic's event page */
466 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
467 siefp
.SiefpEnabled
= 1;
468 siefp
.BaseSiefpGpa
= virt_to_phys(gHvContext
.synICEventPage
[0])
471 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIEFP msr set to: %llx",
474 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
477 /* Setup the interception SINT. */
478 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
479 /* interceptionSint.AsUINT64); */
481 /* Setup the shared SINT. */
482 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
484 sharedSint
.AsUINT64
= 0;
485 sharedSint
.Vector
= irqVector
; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
486 sharedSint
.Masked
= false;
487 sharedSint
.AutoEoi
= true;
489 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SINT1 msr set to: %llx",
490 sharedSint
.AsUINT64
);
492 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
494 /* Enable the global synic bit */
495 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
498 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
500 gHvContext
.SynICInitialized
= true;
509 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
) {
510 if (gHvContext
.synICEventPage
[0])
511 osd_PageFree(gHvContext
.synICEventPage
[0], 1);
513 if (gHvContext
.synICMessagePage
[0])
514 osd_PageFree(gHvContext
.synICMessagePage
[0], 1);
523 * HvSynicCleanup - Cleanup routine for HvSynicInit().
525 void HvSynicCleanup(void)
527 union hv_synic_sint sharedSint
;
528 union hv_synic_simp simp
;
529 union hv_synic_siefp siefp
;
533 if (!gHvContext
.SynICInitialized
) {
538 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
540 sharedSint
.Masked
= 1;
542 /* Disable the interrupt */
543 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
546 * Disable and free the resources only if we are running as
547 * native linux since in xenlinux, we are sharing the
548 * resources with the x2v shim
550 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
) {
551 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
552 simp
.SimpEnabled
= 0;
553 simp
.BaseSimpGpa
= 0;
555 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
557 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
558 siefp
.SiefpEnabled
= 0;
559 siefp
.BaseSiefpGpa
= 0;
561 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
563 osd_PageFree(gHvContext
.synICMessagePage
[0], 1);
564 osd_PageFree(gHvContext
.synICEventPage
[0], 1);