staging: hv: Remove all unneeded DPRINT from hv_utils
[deliverable/linux.git] / drivers / staging / hv / hv.c
CommitLineData
3e7ee490 1/*
3e7ee490
HJ
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 *
21 */
a0086dc5
GKH
22#include <linux/kernel.h>
23#include <linux/mm.h>
5a0e3ad6 24#include <linux/slab.h>
b7c947f0 25#include <linux/vmalloc.h>
e3fe0bb6 26#include "hv_api.h"
645954c5 27#include "logging.h"
72daf320 28#include "vmbus_private.h"
3e7ee490 29
454f18a9 30/* The one and only */
6a0aaa18
HZ
31struct hv_context hv_context = {
32 .synic_initialized = false,
33 .hypercall_page = NULL,
34 .signal_event_param = NULL,
35 .signal_event_buffer = NULL,
3e7ee490
HJ
36};
37
3e189519 38/*
d44890c8
HZ
39 * query_hypervisor_presence
40 * - Query the cpuid for presense of windows hypervisor
0831ad04 41 */
d44890c8 42static int query_hypervisor_presence(void)
3e7ee490 43{
0831ad04
GKH
44 unsigned int eax;
45 unsigned int ebx;
46 unsigned int ecx;
47 unsigned int edx;
48 unsigned int op;
3e7ee490 49
454f18a9
BP
50 eax = 0;
51 ebx = 0;
52 ecx = 0;
53 edx = 0;
f6feebe0 54 op = HVCPUID_VERSION_FEATURES;
f931a70c 55 cpuid(op, &eax, &ebx, &ecx, &edx);
0831ad04
GKH
56
57 return ecx & HV_PRESENT_BIT;
3e7ee490
HJ
58}
59
3e189519 60/*
d44890c8 61 * query_hypervisor_info - Get version info of the windows hypervisor
0831ad04 62 */
d44890c8 63static int query_hypervisor_info(void)
0831ad04
GKH
64{
65 unsigned int eax;
66 unsigned int ebx;
67 unsigned int ecx;
68 unsigned int edx;
b8dfb264 69 unsigned int max_leaf;
0831ad04 70 unsigned int op;
3e7ee490 71
0831ad04
GKH
72 /*
73 * Its assumed that this is called after confirming that Viridian
74 * is present. Query id and revision.
75 */
76 eax = 0;
77 ebx = 0;
78 ecx = 0;
79 edx = 0;
f6feebe0 80 op = HVCPUID_VENDOR_MAXFUNCTION;
0831ad04 81 cpuid(op, &eax, &ebx, &ecx, &edx);
3e7ee490 82
98e08702 83/* DPRINT_INFO(VMBUS, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
0831ad04
GKH
84 (ebx & 0xFF),
85 ((ebx >> 8) & 0xFF),
86 ((ebx >> 16) & 0xFF),
87 ((ebx >> 24) & 0xFF),
88 (ecx & 0xFF),
89 ((ecx >> 8) & 0xFF),
90 ((ecx >> 16) & 0xFF),
91 ((ecx >> 24) & 0xFF),
92 (edx & 0xFF),
93 ((edx >> 8) & 0xFF),
94 ((edx >> 16) & 0xFF),
95 ((edx >> 24) & 0xFF));
98e08702 96*/
b8dfb264 97 max_leaf = eax;
98e08702 98/* eax = 0;
0831ad04
GKH
99 ebx = 0;
100 ecx = 0;
101 edx = 0;
f6feebe0 102 op = HVCPUID_INTERFACE;
0831ad04 103 cpuid(op, &eax, &ebx, &ecx, &edx);
3e7ee490 104
0831ad04
GKH
105 DPRINT_INFO(VMBUS, "Interface ID: %c%c%c%c",
106 (eax & 0xFF),
107 ((eax >> 8) & 0xFF),
108 ((eax >> 16) & 0xFF),
109 ((eax >> 24) & 0xFF));
98e08702 110*/
0831ad04 111
b8dfb264 112 if (max_leaf >= HVCPUID_VERSION) {
0831ad04
GKH
113 eax = 0;
114 ebx = 0;
115 ecx = 0;
116 edx = 0;
f6feebe0 117 op = HVCPUID_VERSION;
0831ad04
GKH
118 cpuid(op, &eax, &ebx, &ecx, &edx);
119 DPRINT_INFO(VMBUS, "OS Build:%d-%d.%d-%d-%d.%d",\
120 eax,
121 ebx >> 16,
122 ebx & 0xFFFF,
123 ecx,
124 edx >> 24,
125 edx & 0xFFFFFF);
126 }
b8dfb264 127 return max_leaf;
0831ad04 128}
3e7ee490 129
3e189519 130/*
d44890c8 131 * do_hypercall- Invoke the specified hypercall
0831ad04 132 */
d44890c8 133static u64 do_hypercall(u64 control, void *input, void *output)
3e7ee490 134{
530cf207 135#ifdef CONFIG_X86_64
b8dfb264
HZ
136 u64 hv_status = 0;
137 u64 input_address = (input) ? virt_to_phys(input) : 0;
138 u64 output_address = (output) ? virt_to_phys(output) : 0;
139 volatile void *hypercall_page = hv_context.hypercall_page;
3e7ee490 140
b8dfb264
HZ
141 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
142 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
143 "c" (control), "d" (input_address),
144 "m" (hypercall_page));
3e7ee490 145
b8dfb264 146 return hv_status;
3e7ee490
HJ
147
148#else
149
b8dfb264
HZ
150 u32 control_hi = control >> 32;
151 u32 control_lo = control & 0xFFFFFFFF;
152 u32 hv_status_hi = 1;
153 u32 hv_status_lo = 1;
154 u64 input_address = (input) ? virt_to_phys(input) : 0;
155 u32 input_address_hi = input_address >> 32;
156 u32 input_address_lo = input_address & 0xFFFFFFFF;
157 u64 output_address = (output) ? virt_to_phys(output) : 0;
158 u32 output_address_hi = output_address >> 32;
159 u32 output_address_lo = output_address & 0xFFFFFFFF;
160 volatile void *hypercall_page = hv_context.hypercall_page;
3e7ee490 161
b8dfb264
HZ
162 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
163 "=a"(hv_status_lo) : "d" (control_hi),
164 "a" (control_lo), "b" (input_address_hi),
165 "c" (input_address_lo), "D"(output_address_hi),
166 "S"(output_address_lo), "m" (hypercall_page));
3e7ee490 167
b8dfb264 168 return hv_status_lo | ((u64)hv_status_hi << 32);
0831ad04 169#endif /* !x86_64 */
3e7ee490
HJ
170}
171
3e189519 172/*
d44890c8 173 * hv_init - Main initialization routine.
0831ad04
GKH
174 *
175 * This routine must be called before any other routines in here are called
176 */
d44890c8 177int hv_init(void)
3e7ee490 178{
0831ad04 179 int ret = 0;
b8dfb264
HZ
180 int max_leaf;
181 union hv_x64_msr_hypercall_contents hypercall_msr;
182 void *virtaddr = NULL;
3e7ee490 183
6a0aaa18
HZ
184 memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS);
185 memset(hv_context.synic_message_page, 0,
186 sizeof(void *) * MAX_NUM_CPUS);
3e7ee490 187
98e08702 188 if (!query_hypervisor_presence())
3e7ee490 189 goto Cleanup;
3e7ee490 190
d44890c8 191 max_leaf = query_hypervisor_info();
0831ad04 192 /* HvQueryHypervisorFeatures(maxLeaf); */
3e7ee490 193
0831ad04 194 /*
a73e6b7c 195 * We only support running on top of Hyper-V
0831ad04 196 */
6a0aaa18 197 rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
a73e6b7c 198
98e08702 199 if (hv_context.guestid != 0)
a73e6b7c 200 goto Cleanup;
3e7ee490 201
a73e6b7c
HJ
202 /* Write our OS info */
203 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
6a0aaa18 204 hv_context.guestid = HV_LINUX_GUEST_ID;
a73e6b7c 205
454f18a9 206 /* See if the hypercall page is already set */
b8dfb264 207 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
3e7ee490 208
a73e6b7c
HJ
209 /*
210 * Allocate the hypercall page memory
203df82d 211 * virtaddr = osd_page_alloc(1);
a73e6b7c 212 */
df3493e0 213 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
3e7ee490 214
98e08702 215 if (!virtaddr)
a73e6b7c 216 goto Cleanup;
3e7ee490 217
b8dfb264 218 hypercall_msr.enable = 1;
a73e6b7c 219
b8dfb264
HZ
220 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
221 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
a73e6b7c
HJ
222
223 /* Confirm that hypercall page did get setup. */
b8dfb264
HZ
224 hypercall_msr.as_uint64 = 0;
225 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
a73e6b7c 226
98e08702 227 if (!hypercall_msr.enable)
3e7ee490 228 goto Cleanup;
3e7ee490 229
b8dfb264 230 hv_context.hypercall_page = virtaddr;
a73e6b7c 231
454f18a9 232 /* Setup the global signal event param for the signal event hypercall */
6a0aaa18 233 hv_context.signal_event_buffer =
0831ad04
GKH
234 kmalloc(sizeof(struct hv_input_signal_event_buffer),
235 GFP_KERNEL);
6a0aaa18 236 if (!hv_context.signal_event_buffer)
3e7ee490 237 goto Cleanup;
3e7ee490 238
6a0aaa18 239 hv_context.signal_event_param =
0831ad04 240 (struct hv_input_signal_event *)
73509681 241 (ALIGN((unsigned long)
6a0aaa18 242 hv_context.signal_event_buffer,
0831ad04 243 HV_HYPERCALL_PARAM_ALIGN));
6a0aaa18
HZ
244 hv_context.signal_event_param->connectionid.asu32 = 0;
245 hv_context.signal_event_param->connectionid.u.id =
0831ad04 246 VMBUS_EVENT_CONNECTION_ID;
6a0aaa18
HZ
247 hv_context.signal_event_param->flag_number = 0;
248 hv_context.signal_event_param->rsvdz = 0;
3e7ee490 249
0831ad04 250 return ret;
3e7ee490
HJ
251
252Cleanup:
b8dfb264
HZ
253 if (virtaddr) {
254 if (hypercall_msr.enable) {
255 hypercall_msr.as_uint64 = 0;
256 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
3e7ee490
HJ
257 }
258
b8dfb264 259 vfree(virtaddr);
3e7ee490
HJ
260 }
261 ret = -1;
3e7ee490
HJ
262 return ret;
263}
264
3e189519 265/*
d44890c8 266 * hv_cleanup - Cleanup routine.
0831ad04
GKH
267 *
268 * This routine is called normally during driver unloading or exiting.
269 */
d44890c8 270void hv_cleanup(void)
3e7ee490 271{
b8dfb264 272 union hv_x64_msr_hypercall_contents hypercall_msr;
3e7ee490 273
6a0aaa18
HZ
274 kfree(hv_context.signal_event_buffer);
275 hv_context.signal_event_buffer = NULL;
276 hv_context.signal_event_param = NULL;
3e7ee490 277
6a0aaa18 278 if (hv_context.hypercall_page) {
b8dfb264
HZ
279 hypercall_msr.as_uint64 = 0;
280 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
6a0aaa18
HZ
281 vfree(hv_context.hypercall_page);
282 hv_context.hypercall_page = NULL;
3e7ee490 283 }
3e7ee490
HJ
284}
285
3e189519 286/*
d44890c8 287 * hv_post_message - Post a message using the hypervisor message IPC.
0831ad04
GKH
288 *
289 * This involves a hypercall.
290 */
d44890c8 291u16 hv_post_message(union hv_connection_id connection_id,
b8dfb264
HZ
292 enum hv_message_type message_type,
293 void *payload, size_t payload_size)
3e7ee490 294{
b8dfb264 295 struct aligned_input {
0831ad04 296 u64 alignment8;
cba4decd 297 struct hv_input_post_message msg;
3e7ee490
HJ
298 };
299
b8dfb264 300 struct hv_input_post_message *aligned_msg;
034469e6 301 u16 status;
c4b0bc94 302 unsigned long addr;
3e7ee490 303
b8dfb264 304 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
3e7ee490 305 return -1;
3e7ee490 306
b8dfb264 307 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
3e7ee490 308 if (!addr)
3e7ee490 309 return -1;
3e7ee490 310
b8dfb264 311 aligned_msg = (struct hv_input_post_message *)
73509681 312 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
3e7ee490 313
b8dfb264
HZ
314 aligned_msg->connectionid = connection_id;
315 aligned_msg->message_type = message_type;
316 aligned_msg->payload_size = payload_size;
317 memcpy((void *)aligned_msg->payload, payload, payload_size);
3e7ee490 318
d44890c8
HZ
319 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
320 & 0xFFFF;
3e7ee490 321
0831ad04 322 kfree((void *)addr);
3e7ee490
HJ
323
324 return status;
325}
326
327
3e189519 328/*
d44890c8
HZ
329 * hv_signal_event -
330 * Signal an event on the specified connection using the hypervisor event IPC.
0831ad04
GKH
331 *
332 * This involves a hypercall.
333 */
d44890c8 334u16 hv_signal_event(void)
3e7ee490 335{
034469e6 336 u16 status;
3e7ee490 337
d44890c8 338 status = do_hypercall(HVCALL_SIGNAL_EVENT,
6a0aaa18 339 hv_context.signal_event_param,
0831ad04 340 NULL) & 0xFFFF;
3e7ee490
HJ
341 return status;
342}
343
3e189519 344/*
d44890c8 345 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
0831ad04
GKH
346 *
347 * If it is already initialized by another entity (ie x2v shim), we need to
348 * retrieve the initialized message and event pages. Otherwise, we create and
349 * initialize the message and event pages.
350 */
d44890c8 351void hv_synic_init(void *irqarg)
3e7ee490 352{
0831ad04 353 u64 version;
eacb1b4d
GKH
354 union hv_synic_simp simp;
355 union hv_synic_siefp siefp;
b8dfb264 356 union hv_synic_sint shared_sint;
eacb1b4d 357 union hv_synic_scontrol sctrl;
a73e6b7c 358
b8dfb264 359 u32 irq_vector = *((u32 *)(irqarg));
7692fd4d 360 int cpu = smp_processor_id();
3e7ee490 361
6a0aaa18 362 if (!hv_context.hypercall_page)
7692fd4d 363 return;
3e7ee490 364
454f18a9 365 /* Check the version */
a51ed7d6 366 rdmsrl(HV_X64_MSR_SVERSION, version);
3e7ee490 367
6a0aaa18
HZ
368 hv_context.synic_message_page[cpu] =
369 (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 370
6a0aaa18 371 if (hv_context.synic_message_page[cpu] == NULL) {
a73e6b7c
HJ
372 DPRINT_ERR(VMBUS,
373 "unable to allocate SYNIC message page!!");
374 goto Cleanup;
375 }
3e7ee490 376
6a0aaa18
HZ
377 hv_context.synic_event_page[cpu] =
378 (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 379
6a0aaa18 380 if (hv_context.synic_event_page[cpu] == NULL) {
a73e6b7c
HJ
381 DPRINT_ERR(VMBUS,
382 "unable to allocate SYNIC event page!!");
383 goto Cleanup;
384 }
3e7ee490 385
a73e6b7c 386 /* Setup the Synic's message page */
f6feebe0
HZ
387 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
388 simp.simp_enabled = 1;
6a0aaa18 389 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
a73e6b7c 390 >> PAGE_SHIFT;
3e7ee490 391
f6feebe0 392 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 393
a73e6b7c 394 /* Setup the Synic's event page */
f6feebe0
HZ
395 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
396 siefp.siefp_enabled = 1;
6a0aaa18 397 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
a73e6b7c
HJ
398 >> PAGE_SHIFT;
399
f6feebe0 400 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
0831ad04
GKH
401
402 /* Setup the interception SINT. */
a51ed7d6 403 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
f6feebe0 404 /* interceptionSint.as_uint64); */
454f18a9 405
0831ad04 406 /* Setup the shared SINT. */
b8dfb264 407 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 408
b8dfb264
HZ
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;
3e7ee490 413
b8dfb264 414 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 415
454f18a9 416 /* Enable the global synic bit */
f6feebe0
HZ
417 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
418 sctrl.enable = 1;
3e7ee490 419
f6feebe0 420 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
3e7ee490 421
6a0aaa18 422 hv_context.synic_initialized = true;
7692fd4d 423 return;
3e7ee490
HJ
424
425Cleanup:
6a0aaa18 426 if (hv_context.synic_event_page[cpu])
df3493e0 427 free_page((unsigned long)hv_context.synic_event_page[cpu]);
3e7ee490 428
6a0aaa18 429 if (hv_context.synic_message_page[cpu])
df3493e0 430 free_page((unsigned long)hv_context.synic_message_page[cpu]);
7692fd4d 431 return;
3e7ee490
HJ
432}
433
3e189519 434/*
d44890c8 435 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
0831ad04 436 */
d44890c8 437void hv_synic_cleanup(void *arg)
3e7ee490 438{
b8dfb264 439 union hv_synic_sint shared_sint;
eacb1b4d
GKH
440 union hv_synic_simp simp;
441 union hv_synic_siefp siefp;
7692fd4d 442 int cpu = smp_processor_id();
3e7ee490 443
6a0aaa18 444 if (!hv_context.synic_initialized)
3e7ee490 445 return;
3e7ee490 446
b8dfb264 447 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 448
b8dfb264 449 shared_sint.masked = 1;
3e7ee490 450
7692fd4d 451 /* Need to correctly cleanup in the case of SMP!!! */
454f18a9 452 /* Disable the interrupt */
b8dfb264 453 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 454
f6feebe0
HZ
455 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
456 simp.simp_enabled = 0;
457 simp.base_simp_gpa = 0;
3e7ee490 458
f6feebe0 459 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 460
f6feebe0
HZ
461 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
462 siefp.siefp_enabled = 0;
463 siefp.base_siefp_gpa = 0;
3e7ee490 464
f6feebe0 465 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
3e7ee490 466
df3493e0
S
467 free_page((unsigned long)hv_context.synic_message_page[cpu]);
468 free_page((unsigned long)hv_context.synic_event_page[cpu]);
3e7ee490 469}
This page took 0.248699 seconds and 5 git commands to generate.