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/slab.h>
25 #include <linux/vmalloc.h>
28 #include "vmbus_private.h"
30 /* The one and only */
31 struct hv_context gHvContext
= {
32 .SynICInitialized
= false,
33 .HypercallPage
= NULL
,
34 .SignalEventParam
= NULL
,
35 .SignalEventBuffer
= NULL
,
39 * HvQueryHypervisorPresence - Query the cpuid for presense of windows hypervisor
41 static int HvQueryHypervisorPresence(void)
53 op
= HvCpuIdFunctionVersionAndFeatures
;
54 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
56 return ecx
& HV_PRESENT_BIT
;
60 * HvQueryHypervisorInfo - Get version info of the windows hypervisor
62 static int HvQueryHypervisorInfo(void)
72 * Its assumed that this is called after confirming that Viridian
73 * is present. Query id and revision.
79 op
= HvCpuIdFunctionHvVendorAndMaxFunction
;
80 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
82 DPRINT_INFO(VMBUS
, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
94 ((edx
>> 24) & 0xFF));
101 op
= HvCpuIdFunctionHvInterface
;
102 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
104 DPRINT_INFO(VMBUS
, "Interface ID: %c%c%c%c",
107 ((eax
>> 16) & 0xFF),
108 ((eax
>> 24) & 0xFF));
110 if (maxLeaf
>= HvCpuIdFunctionMsHvVersion
) {
115 op
= HvCpuIdFunctionMsHvVersion
;
116 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
117 DPRINT_INFO(VMBUS
, "OS Build:%d-%d.%d-%d-%d.%d",\
129 * HvDoHypercall - Invoke the specified hypercall
131 static u64
HvDoHypercall(u64 Control
, void *Input
, void *Output
)
135 u64 inputAddress
= (Input
) ? virt_to_phys(Input
) : 0;
136 u64 outputAddress
= (Output
) ? virt_to_phys(Output
) : 0;
137 volatile void *hypercallPage
= gHvContext
.HypercallPage
;
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
);
144 __asm__
__volatile__("mov %0, %%r8" : : "r" (outputAddress
) : "r8");
145 __asm__
__volatile__("call *%3" : "=a" (hvStatus
) :
146 "c" (Control
), "d" (inputAddress
),
147 "m" (hypercallPage
));
149 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>", hvStatus
);
155 u32 controlHi
= Control
>> 32;
156 u32 controlLo
= Control
& 0xFFFFFFFF;
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
;
167 DPRINT_DBG(VMBUS
, "Hypercall <control %llx input %p output %p>",
168 Control
, Input
, Output
);
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
));
176 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>",
177 hvStatusLo
| ((u64
)hvStatusHi
<< 32));
179 return hvStatusLo
| ((u64
)hvStatusHi
<< 32);
184 * HvInit - Main initialization routine.
186 * This routine must be called before any other routines in here are called
192 union hv_x64_msr_hypercall_contents hypercallMsr
;
193 void *virtAddr
= NULL
;
197 memset(gHvContext
.synICEventPage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
198 memset(gHvContext
.synICMessagePage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
200 if (!HvQueryHypervisorPresence()) {
201 DPRINT_ERR(VMBUS
, "No Windows hypervisor detected!!");
206 "Windows hypervisor detected! Retrieving more info...");
208 maxLeaf
= HvQueryHypervisorInfo();
209 /* HvQueryHypervisorFeatures(maxLeaf); */
212 * We only support running on top of Hyper-V
214 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, gHvContext
.GuestId
);
216 if (gHvContext
.GuestId
!= 0) {
217 DPRINT_ERR(VMBUS
, "Unknown guest id (0x%llx)!!",
222 /* Write our OS info */
223 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, HV_LINUX_GUEST_ID
);
224 gHvContext
.GuestId
= HV_LINUX_GUEST_ID
;
226 /* See if the hypercall page is already set */
227 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
230 * Allocate the hypercall page memory
231 * virtAddr = osd_PageAlloc(1);
233 virtAddr
= osd_VirtualAllocExec(PAGE_SIZE
);
237 "unable to allocate hypercall page!!");
241 hypercallMsr
.Enable
= 1;
243 hypercallMsr
.GuestPhysicalAddress
= vmalloc_to_pfn(virtAddr
);
244 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
246 /* Confirm that hypercall page did get setup. */
247 hypercallMsr
.AsUINT64
= 0;
248 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
250 if (!hypercallMsr
.Enable
) {
251 DPRINT_ERR(VMBUS
, "unable to set hypercall page!!");
255 gHvContext
.HypercallPage
= virtAddr
;
257 DPRINT_INFO(VMBUS
, "Hypercall page VA=%p, PA=0x%0llx",
258 gHvContext
.HypercallPage
,
259 (u64
)hypercallMsr
.GuestPhysicalAddress
<< PAGE_SHIFT
);
261 /* Setup the global signal event param for the signal event hypercall */
262 gHvContext
.SignalEventBuffer
=
263 kmalloc(sizeof(struct hv_input_signal_event_buffer
),
265 if (!gHvContext
.SignalEventBuffer
)
268 gHvContext
.SignalEventParam
=
269 (struct hv_input_signal_event
*)
270 (ALIGN_UP((unsigned long)gHvContext
.SignalEventBuffer
,
271 HV_HYPERCALL_PARAM_ALIGN
));
272 gHvContext
.SignalEventParam
->ConnectionId
.Asu32
= 0;
273 gHvContext
.SignalEventParam
->ConnectionId
.u
.Id
=
274 VMBUS_EVENT_CONNECTION_ID
;
275 gHvContext
.SignalEventParam
->FlagNumber
= 0;
276 gHvContext
.SignalEventParam
->RsvdZ
= 0;
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 kfree(gHvContext
.SignalEventBuffer
);
309 gHvContext
.SignalEventBuffer
= NULL
;
310 gHvContext
.SignalEventParam
= NULL
;
312 if (gHvContext
.HypercallPage
) {
313 hypercallMsr
.AsUINT64
= 0;
314 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
315 vfree(gHvContext
.HypercallPage
);
316 gHvContext
.HypercallPage
= NULL
;
323 * HvPostMessage - Post a message using the hypervisor message IPC.
325 * This involves a hypercall.
327 u16
HvPostMessage(union hv_connection_id connectionId
,
328 enum hv_message_type messageType
,
329 void *payload
, size_t payloadSize
)
331 struct alignedInput
{
333 struct hv_input_post_message msg
;
336 struct hv_input_post_message
*alignedMsg
;
340 if (payloadSize
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
343 addr
= (unsigned long)kmalloc(sizeof(struct alignedInput
), GFP_ATOMIC
);
347 alignedMsg
= (struct hv_input_post_message
*)
348 (ALIGN_UP(addr
, HV_HYPERCALL_PARAM_ALIGN
));
350 alignedMsg
->ConnectionId
= connectionId
;
351 alignedMsg
->MessageType
= messageType
;
352 alignedMsg
->PayloadSize
= payloadSize
;
353 memcpy((void *)alignedMsg
->Payload
, payload
, payloadSize
);
355 status
= HvDoHypercall(HvCallPostMessage
, alignedMsg
, NULL
) & 0xFFFF;
364 * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
366 * This involves a hypercall.
368 u16
HvSignalEvent(void)
372 status
= HvDoHypercall(HvCallSignalEvent
, gHvContext
.SignalEventParam
,
378 * HvSynicInit - Initialize the Synthethic Interrupt Controller.
380 * If it is already initialized by another entity (ie x2v shim), we need to
381 * retrieve the initialized message and event pages. Otherwise, we create and
382 * initialize the message and event pages.
384 void HvSynicInit(void *irqarg
)
387 union hv_synic_simp simp
;
388 union hv_synic_siefp siefp
;
389 union hv_synic_sint sharedSint
;
390 union hv_synic_scontrol sctrl
;
392 u32 irqVector
= *((u32
*)(irqarg
));
393 int cpu
= smp_processor_id();
397 if (!gHvContext
.HypercallPage
) {
402 /* Check the version */
403 rdmsrl(HV_X64_MSR_SVERSION
, version
);
405 DPRINT_INFO(VMBUS
, "SynIC version: %llx", version
);
407 gHvContext
.synICMessagePage
[cpu
] = (void *)get_zeroed_page(GFP_ATOMIC
);
409 if (gHvContext
.synICMessagePage
[cpu
] == NULL
) {
411 "unable to allocate SYNIC message page!!");
415 gHvContext
.synICEventPage
[cpu
] = (void *)get_zeroed_page(GFP_ATOMIC
);
417 if (gHvContext
.synICEventPage
[cpu
] == NULL
) {
419 "unable to allocate SYNIC event page!!");
423 /* Setup the Synic's message page */
424 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
425 simp
.SimpEnabled
= 1;
426 simp
.BaseSimpGpa
= virt_to_phys(gHvContext
.synICMessagePage
[cpu
])
429 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIMP msr set to: %llx", simp
.AsUINT64
);
431 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
433 /* Setup the Synic's event page */
434 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
435 siefp
.SiefpEnabled
= 1;
436 siefp
.BaseSiefpGpa
= virt_to_phys(gHvContext
.synICEventPage
[cpu
])
439 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIEFP msr set to: %llx", siefp
.AsUINT64
);
441 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
443 /* Setup the interception SINT. */
444 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
445 /* interceptionSint.AsUINT64); */
447 /* Setup the shared SINT. */
448 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
450 sharedSint
.AsUINT64
= 0;
451 sharedSint
.Vector
= irqVector
; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
452 sharedSint
.Masked
= false;
453 sharedSint
.AutoEoi
= true;
455 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SINT1 msr set to: %llx",
456 sharedSint
.AsUINT64
);
458 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
460 /* Enable the global synic bit */
461 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
464 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
466 gHvContext
.SynICInitialized
= true;
473 if (gHvContext
.synICEventPage
[cpu
])
474 osd_PageFree(gHvContext
.synICEventPage
[cpu
], 1);
476 if (gHvContext
.synICMessagePage
[cpu
])
477 osd_PageFree(gHvContext
.synICMessagePage
[cpu
], 1);
484 * HvSynicCleanup - Cleanup routine for HvSynicInit().
486 void HvSynicCleanup(void *arg
)
488 union hv_synic_sint sharedSint
;
489 union hv_synic_simp simp
;
490 union hv_synic_siefp siefp
;
491 int cpu
= smp_processor_id();
495 if (!gHvContext
.SynICInitialized
) {
500 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
502 sharedSint
.Masked
= 1;
504 /* Need to correctly cleanup in the case of SMP!!! */
505 /* Disable the interrupt */
506 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
508 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
509 simp
.SimpEnabled
= 0;
510 simp
.BaseSimpGpa
= 0;
512 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
514 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
515 siefp
.SiefpEnabled
= 0;
516 siefp
.BaseSiefpGpa
= 0;
518 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
520 osd_PageFree(gHvContext
.synICMessagePage
[cpu
], 1);
521 osd_PageFree(gHvContext
.synICEventPage
[cpu
], 1);