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 hv_context = {
32 .synic_initialized = false,
33 .hypercall_page = NULL,
34 .signal_event_param = NULL,
35 .signal_event_buffer = 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)
68 unsigned int max_leaf;
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 (max_leaf >= 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 input_address = (input) ? virt_to_phys(input) : 0;
136 u64 output_address = (output) ? virt_to_phys(output) : 0;
137 volatile void *hypercall_page = hv_context.hypercall_page;
139 DPRINT_DBG(VMBUS, "Hypercall <control %llx input phys %llx virt %p "
140 "output phys %llx virt %p hypercall %p>",
141 control, input_address, input,
142 output_address, output, hypercall_page);
144 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
145 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
146 "c" (control), "d" (input_address),
147 "m" (hypercall_page));
149 DPRINT_DBG(VMBUS, "Hypercall <return %llx>", hv_status);
155 u32 control_hi = control >> 32;
156 u32 control_lo = control & 0xFFFFFFFF;
157 u32 hv_status_hi = 1;
158 u32 hv_status_lo = 1;
159 u64 input_address = (input) ? virt_to_phys(input) : 0;
160 u32 input_address_hi = input_address >> 32;
161 u32 input_address_lo = input_address & 0xFFFFFFFF;
162 u64 output_address = (output) ? virt_to_phys(output) : 0;
163 u32 output_address_hi = output_address >> 32;
164 u32 output_address_lo = output_address & 0xFFFFFFFF;
165 volatile void *hypercall_page = hv_context.hypercall_page;
167 DPRINT_DBG(VMBUS, "Hypercall <control %llx input %p output %p>",
168 control, input, output);
170 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
171 "=a"(hv_status_lo) : "d" (control_hi),
172 "a" (control_lo), "b" (input_address_hi),
173 "c" (input_address_lo), "D"(output_address_hi),
174 "S"(output_address_lo), "m" (hypercall_page));
176 DPRINT_DBG(VMBUS, "Hypercall <return %llx>",
177 hv_status_lo | ((u64)hv_status_hi << 32));
179 return hv_status_lo | ((u64)hv_status_hi << 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 hypercall_msr;
193 void *virtaddr = NULL;
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);
199 if (!HvQueryHypervisorPresence()) {
200 DPRINT_ERR(VMBUS, "No Windows hypervisor detected!!");
205 "Windows hypervisor detected! Retrieving more info...");
207 max_leaf = HvQueryHypervisorInfo();
208 /* HvQueryHypervisorFeatures(maxLeaf); */
211 * We only support running on top of Hyper-V
213 rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
215 if (hv_context.guestid != 0) {
216 DPRINT_ERR(VMBUS, "Unknown guest id (0x%llx)!!",
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;
225 /* See if the hypercall page is already set */
226 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
229 * Allocate the hypercall page memory
230 * virtaddr = osd_PageAlloc(1);
232 virtaddr = osd_VirtualAllocExec(PAGE_SIZE);
236 "unable to allocate hypercall page!!");
240 hypercall_msr.enable = 1;
242 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
243 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
245 /* Confirm that hypercall page did get setup. */
246 hypercall_msr.as_uint64 = 0;
247 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
249 if (!hypercall_msr.enable) {
250 DPRINT_ERR(VMBUS, "unable to set hypercall page!!");
254 hv_context.hypercall_page = virtaddr;
256 DPRINT_INFO(VMBUS, "Hypercall page VA=%p, PA=0x%0llx",
257 hv_context.hypercall_page,
258 (u64)hypercall_msr.guest_physical_address << PAGE_SHIFT);
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),
264 if (!hv_context.signal_event_buffer)
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;
282 if (hypercall_msr.enable) {
283 hypercall_msr.as_uint64 = 0;
284 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
294 * HvCleanup - Cleanup routine.
296 * This routine is called normally during driver unloading or exiting.
300 union hv_x64_msr_hypercall_contents hypercall_msr;
302 kfree(hv_context.signal_event_buffer);
303 hv_context.signal_event_buffer = NULL;
304 hv_context.signal_event_param = NULL;
306 if (hv_context.hypercall_page) {
307 hypercall_msr.as_uint64 = 0;
308 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
309 vfree(hv_context.hypercall_page);
310 hv_context.hypercall_page = NULL;
315 * HvPostMessage - Post a message using the hypervisor message IPC.
317 * This involves a hypercall.
319 u16 HvPostMessage(union hv_connection_id connection_id,
320 enum hv_message_type message_type,
321 void *payload, size_t payload_size)
323 struct aligned_input {
325 struct hv_input_post_message msg;
328 struct hv_input_post_message *aligned_msg;
332 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
335 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
339 aligned_msg = (struct hv_input_post_message *)
340 (ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
342 aligned_msg->connectionid = connection_id;
343 aligned_msg->message_type = message_type;
344 aligned_msg->payload_size = payload_size;
345 memcpy((void *)aligned_msg->payload, payload, payload_size);
347 status = HvDoHypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL) & 0xFFFF;
356 * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
358 * This involves a hypercall.
360 u16 HvSignalEvent(void)
364 status = HvDoHypercall(HVCALL_SIGNAL_EVENT,
365 hv_context.signal_event_param,
371 * HvSynicInit - Initialize the Synthethic Interrupt Controller.
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.
377 void HvSynicInit(void *irqarg)
380 union hv_synic_simp simp;
381 union hv_synic_siefp siefp;
382 union hv_synic_sint shared_sint;
383 union hv_synic_scontrol sctrl;
385 u32 irq_vector = *((u32 *)(irqarg));
386 int cpu = smp_processor_id();
388 if (!hv_context.hypercall_page)
391 /* Check the version */
392 rdmsrl(HV_X64_MSR_SVERSION, version);
394 DPRINT_INFO(VMBUS, "SynIC version: %llx", version);
396 hv_context.synic_message_page[cpu] =
397 (void *)get_zeroed_page(GFP_ATOMIC);
399 if (hv_context.synic_message_page[cpu] == NULL) {
401 "unable to allocate SYNIC message page!!");
405 hv_context.synic_event_page[cpu] =
406 (void *)get_zeroed_page(GFP_ATOMIC);
408 if (hv_context.synic_event_page[cpu] == NULL) {
410 "unable to allocate SYNIC event page!!");
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])
420 DPRINT_DBG(VMBUS, "HV_X64_MSR_SIMP msr set to: %llx", simp.as_uint64);
422 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
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])
430 DPRINT_DBG(VMBUS, "HV_X64_MSR_SIEFP msr set to: %llx", siefp.as_uint64);
432 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
434 /* Setup the interception SINT. */
435 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
436 /* interceptionSint.as_uint64); */
438 /* Setup the shared SINT. */
439 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
441 shared_sint.as_uint64 = 0;
442 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
443 shared_sint.masked = false;
444 shared_sint.auto_eoi = true;
446 DPRINT_DBG(VMBUS, "HV_X64_MSR_SINT1 msr set to: %llx",
447 shared_sint.as_uint64);
449 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
451 /* Enable the global synic bit */
452 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
455 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
457 hv_context.synic_initialized = true;
461 if (hv_context.synic_event_page[cpu])
462 osd_PageFree(hv_context.synic_event_page[cpu], 1);
464 if (hv_context.synic_message_page[cpu])
465 osd_PageFree(hv_context.synic_message_page[cpu], 1);
470 * HvSynicCleanup - Cleanup routine for HvSynicInit().
472 void HvSynicCleanup(void *arg)
474 union hv_synic_sint shared_sint;
475 union hv_synic_simp simp;
476 union hv_synic_siefp siefp;
477 int cpu = smp_processor_id();
479 if (!hv_context.synic_initialized)
482 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
484 shared_sint.masked = 1;
486 /* Need to correctly cleanup in the case of SMP!!! */
487 /* Disable the interrupt */
488 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
490 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
491 simp.simp_enabled = 0;
492 simp.base_simp_gpa = 0;
494 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
496 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
497 siefp.siefp_enabled = 0;
498 siefp.base_siefp_gpa = 0;
500 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
502 osd_PageFree(hv_context.synic_message_page[cpu], 1);
503 osd_PageFree(hv_context.synic_event_page[cpu], 1);