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
= HVCPUID_VERSION_FEATURES
;
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
= HVCPUID_VENDOR_MAXFUNCTION
;
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
= HVCPUID_INTERFACE
;
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
>= HVCPUID_VERSION
) {
115 op
= HVCPUID_VERSION
;
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
;
195 memset(gHvContext
.synICEventPage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
196 memset(gHvContext
.synICMessagePage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
198 if (!HvQueryHypervisorPresence()) {
199 DPRINT_ERR(VMBUS
, "No Windows hypervisor detected!!");
204 "Windows hypervisor detected! Retrieving more info...");
206 maxLeaf
= HvQueryHypervisorInfo();
207 /* HvQueryHypervisorFeatures(maxLeaf); */
210 * We only support running on top of Hyper-V
212 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, gHvContext
.GuestId
);
214 if (gHvContext
.GuestId
!= 0) {
215 DPRINT_ERR(VMBUS
, "Unknown guest id (0x%llx)!!",
220 /* Write our OS info */
221 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, HV_LINUX_GUEST_ID
);
222 gHvContext
.GuestId
= HV_LINUX_GUEST_ID
;
224 /* See if the hypercall page is already set */
225 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.as_uint64
);
228 * Allocate the hypercall page memory
229 * virtAddr = osd_PageAlloc(1);
231 virtAddr
= osd_VirtualAllocExec(PAGE_SIZE
);
235 "unable to allocate hypercall page!!");
239 hypercallMsr
.enable
= 1;
241 hypercallMsr
.guest_physical_address
= vmalloc_to_pfn(virtAddr
);
242 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.as_uint64
);
244 /* Confirm that hypercall page did get setup. */
245 hypercallMsr
.as_uint64
= 0;
246 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.as_uint64
);
248 if (!hypercallMsr
.enable
) {
249 DPRINT_ERR(VMBUS
, "unable to set hypercall page!!");
253 gHvContext
.HypercallPage
= virtAddr
;
255 DPRINT_INFO(VMBUS
, "Hypercall page VA=%p, PA=0x%0llx",
256 gHvContext
.HypercallPage
,
257 (u64
)hypercallMsr
.guest_physical_address
<< 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
->flag_number
= 0;
274 gHvContext
.SignalEventParam
->rsvdz
= 0;
280 if (hypercallMsr
.enable
) {
281 hypercallMsr
.as_uint64
= 0;
282 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.as_uint64
);
292 * HvCleanup - Cleanup routine.
294 * This routine is called normally during driver unloading or exiting.
298 union hv_x64_msr_hypercall_contents hypercallMsr
;
300 kfree(gHvContext
.SignalEventBuffer
);
301 gHvContext
.SignalEventBuffer
= NULL
;
302 gHvContext
.SignalEventParam
= NULL
;
304 if (gHvContext
.HypercallPage
) {
305 hypercallMsr
.as_uint64
= 0;
306 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.as_uint64
);
307 vfree(gHvContext
.HypercallPage
);
308 gHvContext
.HypercallPage
= NULL
;
313 * HvPostMessage - Post a message using the hypervisor message IPC.
315 * This involves a hypercall.
317 u16
HvPostMessage(union hv_connection_id connectionId
,
318 enum hv_message_type messageType
,
319 void *payload
, size_t payloadSize
)
321 struct alignedInput
{
323 struct hv_input_post_message msg
;
326 struct hv_input_post_message
*alignedMsg
;
330 if (payloadSize
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
333 addr
= (unsigned long)kmalloc(sizeof(struct alignedInput
), GFP_ATOMIC
);
337 alignedMsg
= (struct hv_input_post_message
*)
338 (ALIGN_UP(addr
, HV_HYPERCALL_PARAM_ALIGN
));
340 alignedMsg
->connectionid
= connectionId
;
341 alignedMsg
->message_type
= messageType
;
342 alignedMsg
->payload_size
= payloadSize
;
343 memcpy((void *)alignedMsg
->payload
, payload
, payloadSize
);
345 status
= HvDoHypercall(HVCALL_POST_MESSAGE
, alignedMsg
, NULL
) & 0xFFFF;
354 * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
356 * This involves a hypercall.
358 u16
HvSignalEvent(void)
362 status
= HvDoHypercall(HVCALL_SIGNAL_EVENT
, gHvContext
.SignalEventParam
,
368 * HvSynicInit - Initialize the Synthethic Interrupt Controller.
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.
374 void HvSynicInit(void *irqarg
)
377 union hv_synic_simp simp
;
378 union hv_synic_siefp siefp
;
379 union hv_synic_sint sharedSint
;
380 union hv_synic_scontrol sctrl
;
382 u32 irqVector
= *((u32
*)(irqarg
));
383 int cpu
= smp_processor_id();
385 if (!gHvContext
.HypercallPage
)
388 /* Check the version */
389 rdmsrl(HV_X64_MSR_SVERSION
, version
);
391 DPRINT_INFO(VMBUS
, "SynIC version: %llx", version
);
393 gHvContext
.synICMessagePage
[cpu
] = (void *)get_zeroed_page(GFP_ATOMIC
);
395 if (gHvContext
.synICMessagePage
[cpu
] == NULL
) {
397 "unable to allocate SYNIC message page!!");
401 gHvContext
.synICEventPage
[cpu
] = (void *)get_zeroed_page(GFP_ATOMIC
);
403 if (gHvContext
.synICEventPage
[cpu
] == NULL
) {
405 "unable to allocate SYNIC event page!!");
409 /* Setup the Synic's message page */
410 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
411 simp
.simp_enabled
= 1;
412 simp
.base_simp_gpa
= virt_to_phys(gHvContext
.synICMessagePage
[cpu
])
415 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIMP msr set to: %llx", simp
.as_uint64
);
417 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
419 /* Setup the Synic's event page */
420 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
421 siefp
.siefp_enabled
= 1;
422 siefp
.base_siefp_gpa
= virt_to_phys(gHvContext
.synICEventPage
[cpu
])
425 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIEFP msr set to: %llx", siefp
.as_uint64
);
427 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
429 /* Setup the interception SINT. */
430 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
431 /* interceptionSint.as_uint64); */
433 /* Setup the shared SINT. */
434 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.as_uint64
);
436 sharedSint
.as_uint64
= 0;
437 sharedSint
.vector
= irqVector
; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
438 sharedSint
.masked
= false;
439 sharedSint
.auto_eoi
= true;
441 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SINT1 msr set to: %llx",
442 sharedSint
.as_uint64
);
444 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.as_uint64
);
446 /* Enable the global synic bit */
447 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
450 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
452 gHvContext
.SynICInitialized
= true;
456 if (gHvContext
.synICEventPage
[cpu
])
457 osd_PageFree(gHvContext
.synICEventPage
[cpu
], 1);
459 if (gHvContext
.synICMessagePage
[cpu
])
460 osd_PageFree(gHvContext
.synICMessagePage
[cpu
], 1);
465 * HvSynicCleanup - Cleanup routine for HvSynicInit().
467 void HvSynicCleanup(void *arg
)
469 union hv_synic_sint sharedSint
;
470 union hv_synic_simp simp
;
471 union hv_synic_siefp siefp
;
472 int cpu
= smp_processor_id();
474 if (!gHvContext
.SynICInitialized
)
477 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.as_uint64
);
479 sharedSint
.masked
= 1;
481 /* Need to correctly cleanup in the case of SMP!!! */
482 /* Disable the interrupt */
483 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.as_uint64
);
485 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
486 simp
.simp_enabled
= 0;
487 simp
.base_simp_gpa
= 0;
489 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
491 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
492 siefp
.siefp_enabled
= 0;
493 siefp
.base_siefp_gpa
= 0;
495 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
497 osd_PageFree(gHvContext
.synICMessagePage
[cpu
], 1);
498 osd_PageFree(gHvContext
.synICEventPage
[cpu
], 1);