staging: hv: make HYPERV_UTILS depend on NLS to prevent a build error
[linux-2.6/next.git] / drivers / staging / hv / hv.c
blobe6312bd47d7e8fb79e52d6433a1b5e39a85bc045
1 /*
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
11 * more details.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include "hv_api.h"
29 #include "logging.h"
30 #include "vmbus_private.h"
32 /* The one and only */
33 struct hv_context hv_context = {
34 .synic_initialized = false,
35 .hypercall_page = NULL,
36 .signal_event_param = NULL,
37 .signal_event_buffer = NULL,
41 * query_hypervisor_presence
42 * - Query the cpuid for presence of windows hypervisor
44 static int query_hypervisor_presence(void)
46 unsigned int eax;
47 unsigned int ebx;
48 unsigned int ecx;
49 unsigned int edx;
50 unsigned int op;
52 eax = 0;
53 ebx = 0;
54 ecx = 0;
55 edx = 0;
56 op = HVCPUID_VERSION_FEATURES;
57 cpuid(op, &eax, &ebx, &ecx, &edx);
59 return ecx & HV_PRESENT_BIT;
63 * query_hypervisor_info - Get version info of the windows hypervisor
65 static int query_hypervisor_info(void)
67 unsigned int eax;
68 unsigned int ebx;
69 unsigned int ecx;
70 unsigned int edx;
71 unsigned int max_leaf;
72 unsigned int op;
75 * Its assumed that this is called after confirming that Viridian
76 * is present. Query id and revision.
78 eax = 0;
79 ebx = 0;
80 ecx = 0;
81 edx = 0;
82 op = HVCPUID_VENDOR_MAXFUNCTION;
83 cpuid(op, &eax, &ebx, &ecx, &edx);
85 /* DPRINT_INFO(VMBUS, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
86 (ebx & 0xFF),
87 ((ebx >> 8) & 0xFF),
88 ((ebx >> 16) & 0xFF),
89 ((ebx >> 24) & 0xFF),
90 (ecx & 0xFF),
91 ((ecx >> 8) & 0xFF),
92 ((ecx >> 16) & 0xFF),
93 ((ecx >> 24) & 0xFF),
94 (edx & 0xFF),
95 ((edx >> 8) & 0xFF),
96 ((edx >> 16) & 0xFF),
97 ((edx >> 24) & 0xFF));
99 max_leaf = eax;
100 /* eax = 0;
101 ebx = 0;
102 ecx = 0;
103 edx = 0;
104 op = HVCPUID_INTERFACE;
105 cpuid(op, &eax, &ebx, &ecx, &edx);
107 DPRINT_INFO(VMBUS, "Interface ID: %c%c%c%c",
108 (eax & 0xFF),
109 ((eax >> 8) & 0xFF),
110 ((eax >> 16) & 0xFF),
111 ((eax >> 24) & 0xFF));
114 if (max_leaf >= HVCPUID_VERSION) {
115 eax = 0;
116 ebx = 0;
117 ecx = 0;
118 edx = 0;
119 op = HVCPUID_VERSION;
120 cpuid(op, &eax, &ebx, &ecx, &edx);
121 pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
122 eax,
123 ebx >> 16,
124 ebx & 0xFFFF,
125 ecx,
126 edx >> 24,
127 edx & 0xFFFFFF);
129 return max_leaf;
133 * do_hypercall- Invoke the specified hypercall
135 static u64 do_hypercall(u64 control, void *input, void *output)
137 #ifdef CONFIG_X86_64
138 u64 hv_status = 0;
139 u64 input_address = (input) ? virt_to_phys(input) : 0;
140 u64 output_address = (output) ? virt_to_phys(output) : 0;
141 volatile void *hypercall_page = hv_context.hypercall_page;
143 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
144 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
145 "c" (control), "d" (input_address),
146 "m" (hypercall_page));
148 return hv_status;
150 #else
152 u32 control_hi = control >> 32;
153 u32 control_lo = control & 0xFFFFFFFF;
154 u32 hv_status_hi = 1;
155 u32 hv_status_lo = 1;
156 u64 input_address = (input) ? virt_to_phys(input) : 0;
157 u32 input_address_hi = input_address >> 32;
158 u32 input_address_lo = input_address & 0xFFFFFFFF;
159 u64 output_address = (output) ? virt_to_phys(output) : 0;
160 u32 output_address_hi = output_address >> 32;
161 u32 output_address_lo = output_address & 0xFFFFFFFF;
162 volatile void *hypercall_page = hv_context.hypercall_page;
164 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
165 "=a"(hv_status_lo) : "d" (control_hi),
166 "a" (control_lo), "b" (input_address_hi),
167 "c" (input_address_lo), "D"(output_address_hi),
168 "S"(output_address_lo), "m" (hypercall_page));
170 return hv_status_lo | ((u64)hv_status_hi << 32);
171 #endif /* !x86_64 */
175 * hv_init - Main initialization routine.
177 * This routine must be called before any other routines in here are called
179 int hv_init(void)
181 int ret = 0;
182 int max_leaf;
183 union hv_x64_msr_hypercall_contents hypercall_msr;
184 void *virtaddr = NULL;
186 memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS);
187 memset(hv_context.synic_message_page, 0,
188 sizeof(void *) * MAX_NUM_CPUS);
190 if (!query_hypervisor_presence())
191 goto Cleanup;
193 max_leaf = query_hypervisor_info();
194 /* HvQueryHypervisorFeatures(maxLeaf); */
197 * We only support running on top of Hyper-V
199 rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
201 if (hv_context.guestid != 0)
202 goto Cleanup;
204 /* Write our OS info */
205 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
206 hv_context.guestid = HV_LINUX_GUEST_ID;
208 /* See if the hypercall page is already set */
209 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
212 * Allocate the hypercall page memory
213 * virtaddr = osd_page_alloc(1);
215 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
217 if (!virtaddr)
218 goto Cleanup;
220 hypercall_msr.enable = 1;
222 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
223 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
225 /* Confirm that hypercall page did get setup. */
226 hypercall_msr.as_uint64 = 0;
227 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
229 if (!hypercall_msr.enable)
230 goto Cleanup;
232 hv_context.hypercall_page = virtaddr;
234 /* Setup the global signal event param for the signal event hypercall */
235 hv_context.signal_event_buffer =
236 kmalloc(sizeof(struct hv_input_signal_event_buffer),
237 GFP_KERNEL);
238 if (!hv_context.signal_event_buffer)
239 goto Cleanup;
241 hv_context.signal_event_param =
242 (struct hv_input_signal_event *)
243 (ALIGN((unsigned long)
244 hv_context.signal_event_buffer,
245 HV_HYPERCALL_PARAM_ALIGN));
246 hv_context.signal_event_param->connectionid.asu32 = 0;
247 hv_context.signal_event_param->connectionid.u.id =
248 VMBUS_EVENT_CONNECTION_ID;
249 hv_context.signal_event_param->flag_number = 0;
250 hv_context.signal_event_param->rsvdz = 0;
252 return ret;
254 Cleanup:
255 if (virtaddr) {
256 if (hypercall_msr.enable) {
257 hypercall_msr.as_uint64 = 0;
258 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
261 vfree(virtaddr);
263 ret = -1;
264 return ret;
268 * hv_cleanup - Cleanup routine.
270 * This routine is called normally during driver unloading or exiting.
272 void hv_cleanup(void)
274 union hv_x64_msr_hypercall_contents hypercall_msr;
276 kfree(hv_context.signal_event_buffer);
277 hv_context.signal_event_buffer = NULL;
278 hv_context.signal_event_param = NULL;
280 if (hv_context.hypercall_page) {
281 hypercall_msr.as_uint64 = 0;
282 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
283 vfree(hv_context.hypercall_page);
284 hv_context.hypercall_page = NULL;
289 * hv_post_message - Post a message using the hypervisor message IPC.
291 * This involves a hypercall.
293 u16 hv_post_message(union hv_connection_id connection_id,
294 enum hv_message_type message_type,
295 void *payload, size_t payload_size)
297 struct aligned_input {
298 u64 alignment8;
299 struct hv_input_post_message msg;
302 struct hv_input_post_message *aligned_msg;
303 u16 status;
304 unsigned long addr;
306 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
307 return -1;
309 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
310 if (!addr)
311 return -1;
313 aligned_msg = (struct hv_input_post_message *)
314 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
316 aligned_msg->connectionid = connection_id;
317 aligned_msg->message_type = message_type;
318 aligned_msg->payload_size = payload_size;
319 memcpy((void *)aligned_msg->payload, payload, payload_size);
321 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
322 & 0xFFFF;
324 kfree((void *)addr);
326 return status;
331 * hv_signal_event -
332 * Signal an event on the specified connection using the hypervisor event IPC.
334 * This involves a hypercall.
336 u16 hv_signal_event(void)
338 u16 status;
340 status = do_hypercall(HVCALL_SIGNAL_EVENT,
341 hv_context.signal_event_param,
342 NULL) & 0xFFFF;
343 return status;
347 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
349 * If it is already initialized by another entity (ie x2v shim), we need to
350 * retrieve the initialized message and event pages. Otherwise, we create and
351 * initialize the message and event pages.
353 void hv_synic_init(void *irqarg)
355 u64 version;
356 union hv_synic_simp simp;
357 union hv_synic_siefp siefp;
358 union hv_synic_sint shared_sint;
359 union hv_synic_scontrol sctrl;
361 u32 irq_vector = *((u32 *)(irqarg));
362 int cpu = smp_processor_id();
364 if (!hv_context.hypercall_page)
365 return;
367 /* Check the version */
368 rdmsrl(HV_X64_MSR_SVERSION, version);
370 hv_context.synic_message_page[cpu] =
371 (void *)get_zeroed_page(GFP_ATOMIC);
373 if (hv_context.synic_message_page[cpu] == NULL) {
374 pr_err("Unable to allocate SYNIC message page\n");
375 goto Cleanup;
378 hv_context.synic_event_page[cpu] =
379 (void *)get_zeroed_page(GFP_ATOMIC);
381 if (hv_context.synic_event_page[cpu] == NULL) {
382 pr_err("Unable to allocate SYNIC event page\n");
383 goto Cleanup;
386 /* Setup the Synic's message page */
387 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
388 simp.simp_enabled = 1;
389 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
390 >> PAGE_SHIFT;
392 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
394 /* Setup the Synic's event page */
395 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
396 siefp.siefp_enabled = 1;
397 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
398 >> PAGE_SHIFT;
400 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
402 /* Setup the interception SINT. */
403 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
404 /* interceptionSint.as_uint64); */
406 /* Setup the shared SINT. */
407 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
409 shared_sint.as_uint64 = 0;
410 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
411 shared_sint.masked = false;
412 shared_sint.auto_eoi = true;
414 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
416 /* Enable the global synic bit */
417 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
418 sctrl.enable = 1;
420 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
422 hv_context.synic_initialized = true;
423 return;
425 Cleanup:
426 if (hv_context.synic_event_page[cpu])
427 free_page((unsigned long)hv_context.synic_event_page[cpu]);
429 if (hv_context.synic_message_page[cpu])
430 free_page((unsigned long)hv_context.synic_message_page[cpu]);
431 return;
435 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
437 void hv_synic_cleanup(void *arg)
439 union hv_synic_sint shared_sint;
440 union hv_synic_simp simp;
441 union hv_synic_siefp siefp;
442 int cpu = smp_processor_id();
444 if (!hv_context.synic_initialized)
445 return;
447 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
449 shared_sint.masked = 1;
451 /* Need to correctly cleanup in the case of SMP!!! */
452 /* Disable the interrupt */
453 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
455 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
456 simp.simp_enabled = 0;
457 simp.base_simp_gpa = 0;
459 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
461 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
462 siefp.siefp_enabled = 0;
463 siefp.base_siefp_gpa = 0;
465 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
467 free_page((unsigned long)hv_context.synic_message_page[cpu]);
468 free_page((unsigned long)hv_context.synic_event_page[cpu]);