staging: hv: Convert camel cased struct fields in hv_api.h to lower cases
[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>
4983b39a 26#include "osd.h"
645954c5 27#include "logging.h"
72daf320 28#include "vmbus_private.h"
3e7ee490 29
454f18a9 30/* The one and only */
af248e1f
GKH
31struct hv_context gHvContext = {
32 .SynICInitialized = false,
33 .HypercallPage = NULL,
34 .SignalEventParam = NULL,
35 .SignalEventBuffer = NULL,
3e7ee490
HJ
36};
37
3e189519 38/*
0831ad04
GKH
39 * HvQueryHypervisorPresence - Query the cpuid for presense of windows hypervisor
40 */
41static int HvQueryHypervisorPresence(void)
3e7ee490 42{
0831ad04
GKH
43 unsigned int eax;
44 unsigned int ebx;
45 unsigned int ecx;
46 unsigned int edx;
47 unsigned int op;
3e7ee490 48
454f18a9
BP
49 eax = 0;
50 ebx = 0;
51 ecx = 0;
52 edx = 0;
f6feebe0 53 op = HVCPUID_VERSION_FEATURES;
f931a70c 54 cpuid(op, &eax, &ebx, &ecx, &edx);
0831ad04
GKH
55
56 return ecx & HV_PRESENT_BIT;
3e7ee490
HJ
57}
58
3e189519 59/*
0831ad04
GKH
60 * HvQueryHypervisorInfo - Get version info of the windows hypervisor
61 */
62static int HvQueryHypervisorInfo(void)
63{
64 unsigned int eax;
65 unsigned int ebx;
66 unsigned int ecx;
67 unsigned int edx;
68 unsigned int maxLeaf;
69 unsigned int op;
3e7ee490 70
0831ad04
GKH
71 /*
72 * Its assumed that this is called after confirming that Viridian
73 * is present. Query id and revision.
74 */
75 eax = 0;
76 ebx = 0;
77 ecx = 0;
78 edx = 0;
f6feebe0 79 op = HVCPUID_VENDOR_MAXFUNCTION;
0831ad04 80 cpuid(op, &eax, &ebx, &ecx, &edx);
3e7ee490 81
0831ad04
GKH
82 DPRINT_INFO(VMBUS, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
83 (ebx & 0xFF),
84 ((ebx >> 8) & 0xFF),
85 ((ebx >> 16) & 0xFF),
86 ((ebx >> 24) & 0xFF),
87 (ecx & 0xFF),
88 ((ecx >> 8) & 0xFF),
89 ((ecx >> 16) & 0xFF),
90 ((ecx >> 24) & 0xFF),
91 (edx & 0xFF),
92 ((edx >> 8) & 0xFF),
93 ((edx >> 16) & 0xFF),
94 ((edx >> 24) & 0xFF));
95
96 maxLeaf = eax;
97 eax = 0;
98 ebx = 0;
99 ecx = 0;
100 edx = 0;
f6feebe0 101 op = HVCPUID_INTERFACE;
0831ad04 102 cpuid(op, &eax, &ebx, &ecx, &edx);
3e7ee490 103
0831ad04
GKH
104 DPRINT_INFO(VMBUS, "Interface ID: %c%c%c%c",
105 (eax & 0xFF),
106 ((eax >> 8) & 0xFF),
107 ((eax >> 16) & 0xFF),
108 ((eax >> 24) & 0xFF));
109
f6feebe0 110 if (maxLeaf >= HVCPUID_VERSION) {
0831ad04
GKH
111 eax = 0;
112 ebx = 0;
113 ecx = 0;
114 edx = 0;
f6feebe0 115 op = HVCPUID_VERSION;
0831ad04
GKH
116 cpuid(op, &eax, &ebx, &ecx, &edx);
117 DPRINT_INFO(VMBUS, "OS Build:%d-%d.%d-%d-%d.%d",\
118 eax,
119 ebx >> 16,
120 ebx & 0xFFFF,
121 ecx,
122 edx >> 24,
123 edx & 0xFFFFFF);
124 }
125 return maxLeaf;
126}
3e7ee490 127
3e189519 128/*
0831ad04
GKH
129 * HvDoHypercall - Invoke the specified hypercall
130 */
131static u64 HvDoHypercall(u64 Control, void *Input, void *Output)
3e7ee490 132{
530cf207 133#ifdef CONFIG_X86_64
0831ad04
GKH
134 u64 hvStatus = 0;
135 u64 inputAddress = (Input) ? virt_to_phys(Input) : 0;
136 u64 outputAddress = (Output) ? virt_to_phys(Output) : 0;
137 volatile void *hypercallPage = gHvContext.HypercallPage;
3e7ee490 138
0831ad04
GKH
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);
3e7ee490 143
0831ad04
GKH
144 __asm__ __volatile__("mov %0, %%r8" : : "r" (outputAddress) : "r8");
145 __asm__ __volatile__("call *%3" : "=a" (hvStatus) :
146 "c" (Control), "d" (inputAddress),
147 "m" (hypercallPage));
3e7ee490 148
0831ad04 149 DPRINT_DBG(VMBUS, "Hypercall <return %llx>", hvStatus);
3e7ee490 150
0831ad04 151 return hvStatus;
3e7ee490
HJ
152
153#else
154
0831ad04
GKH
155 u32 controlHi = Control >> 32;
156 u32 controlLo = Control & 0xFFFFFFFF;
157 u32 hvStatusHi = 1;
158 u32 hvStatusLo = 1;
159 u64 inputAddress = (Input) ? virt_to_phys(Input) : 0;
160 u32 inputAddressHi = inputAddress >> 32;
161 u32 inputAddressLo = inputAddress & 0xFFFFFFFF;
fa56d361 162 u64 outputAddress = (Output) ? virt_to_phys(Output) : 0;
0831ad04
GKH
163 u32 outputAddressHi = outputAddress >> 32;
164 u32 outputAddressLo = outputAddress & 0xFFFFFFFF;
165 volatile void *hypercallPage = gHvContext.HypercallPage;
3e7ee490 166
0831ad04
GKH
167 DPRINT_DBG(VMBUS, "Hypercall <control %llx input %p output %p>",
168 Control, Input, Output);
3e7ee490 169
0831ad04
GKH
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));
3e7ee490 175
0831ad04
GKH
176 DPRINT_DBG(VMBUS, "Hypercall <return %llx>",
177 hvStatusLo | ((u64)hvStatusHi << 32));
3e7ee490 178
0831ad04
GKH
179 return hvStatusLo | ((u64)hvStatusHi << 32);
180#endif /* !x86_64 */
3e7ee490
HJ
181}
182
3e189519 183/*
0831ad04
GKH
184 * HvInit - Main initialization routine.
185 *
186 * This routine must be called before any other routines in here are called
187 */
188int HvInit(void)
3e7ee490 189{
0831ad04
GKH
190 int ret = 0;
191 int maxLeaf;
f80b3d51 192 union hv_x64_msr_hypercall_contents hypercallMsr;
949cadaa 193 void *virtAddr = NULL;
3e7ee490 194
44f357f8
BP
195 memset(gHvContext.synICEventPage, 0, sizeof(void *) * MAX_NUM_CPUS);
196 memset(gHvContext.synICMessagePage, 0, sizeof(void *) * MAX_NUM_CPUS);
3e7ee490 197
0831ad04 198 if (!HvQueryHypervisorPresence()) {
3e7ee490
HJ
199 DPRINT_ERR(VMBUS, "No Windows hypervisor detected!!");
200 goto Cleanup;
201 }
202
0831ad04
GKH
203 DPRINT_INFO(VMBUS,
204 "Windows hypervisor detected! Retrieving more info...");
3e7ee490 205
0831ad04
GKH
206 maxLeaf = HvQueryHypervisorInfo();
207 /* HvQueryHypervisorFeatures(maxLeaf); */
3e7ee490 208
0831ad04 209 /*
a73e6b7c 210 * We only support running on top of Hyper-V
0831ad04 211 */
a51ed7d6 212 rdmsrl(HV_X64_MSR_GUEST_OS_ID, gHvContext.GuestId);
a73e6b7c
HJ
213
214 if (gHvContext.GuestId != 0) {
215 DPRINT_ERR(VMBUS, "Unknown guest id (0x%llx)!!",
216 gHvContext.GuestId);
217 goto Cleanup;
3e7ee490
HJ
218 }
219
a73e6b7c
HJ
220 /* Write our OS info */
221 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
222 gHvContext.GuestId = HV_LINUX_GUEST_ID;
223
454f18a9 224 /* See if the hypercall page is already set */
f6feebe0 225 rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
3e7ee490 226
a73e6b7c
HJ
227 /*
228 * Allocate the hypercall page memory
229 * virtAddr = osd_PageAlloc(1);
230 */
231 virtAddr = osd_VirtualAllocExec(PAGE_SIZE);
3e7ee490 232
a73e6b7c
HJ
233 if (!virtAddr) {
234 DPRINT_ERR(VMBUS,
235 "unable to allocate hypercall page!!");
236 goto Cleanup;
237 }
3e7ee490 238
f6feebe0 239 hypercallMsr.enable = 1;
a73e6b7c 240
f6feebe0
HZ
241 hypercallMsr.guest_physical_address = vmalloc_to_pfn(virtAddr);
242 wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
a73e6b7c
HJ
243
244 /* Confirm that hypercall page did get setup. */
f6feebe0
HZ
245 hypercallMsr.as_uint64 = 0;
246 rdmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
a73e6b7c 247
f6feebe0 248 if (!hypercallMsr.enable) {
a73e6b7c 249 DPRINT_ERR(VMBUS, "unable to set hypercall page!!");
3e7ee490
HJ
250 goto Cleanup;
251 }
252
a73e6b7c
HJ
253 gHvContext.HypercallPage = virtAddr;
254
2701f686
GKH
255 DPRINT_INFO(VMBUS, "Hypercall page VA=%p, PA=0x%0llx",
256 gHvContext.HypercallPage,
f6feebe0 257 (u64)hypercallMsr.guest_physical_address << PAGE_SHIFT);
3e7ee490 258
454f18a9 259 /* Setup the global signal event param for the signal event hypercall */
0831ad04
GKH
260 gHvContext.SignalEventBuffer =
261 kmalloc(sizeof(struct hv_input_signal_event_buffer),
262 GFP_KERNEL);
3e7ee490 263 if (!gHvContext.SignalEventBuffer)
3e7ee490 264 goto Cleanup;
3e7ee490 265
0831ad04
GKH
266 gHvContext.SignalEventParam =
267 (struct hv_input_signal_event *)
268 (ALIGN_UP((unsigned long)gHvContext.SignalEventBuffer,
269 HV_HYPERCALL_PARAM_ALIGN));
f6feebe0
HZ
270 gHvContext.SignalEventParam->connectionid.asu32 = 0;
271 gHvContext.SignalEventParam->connectionid.u.id =
0831ad04 272 VMBUS_EVENT_CONNECTION_ID;
f6feebe0
HZ
273 gHvContext.SignalEventParam->flag_number = 0;
274 gHvContext.SignalEventParam->rsvdz = 0;
3e7ee490 275
0831ad04 276 return ret;
3e7ee490
HJ
277
278Cleanup:
0831ad04 279 if (virtAddr) {
f6feebe0
HZ
280 if (hypercallMsr.enable) {
281 hypercallMsr.as_uint64 = 0;
282 wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
3e7ee490
HJ
283 }
284
b7c947f0 285 vfree(virtAddr);
3e7ee490
HJ
286 }
287 ret = -1;
3e7ee490
HJ
288 return ret;
289}
290
3e189519 291/*
0831ad04
GKH
292 * HvCleanup - Cleanup routine.
293 *
294 * This routine is called normally during driver unloading or exiting.
295 */
296void HvCleanup(void)
3e7ee490 297{
f80b3d51 298 union hv_x64_msr_hypercall_contents hypercallMsr;
3e7ee490 299
1e19c054
BP
300 kfree(gHvContext.SignalEventBuffer);
301 gHvContext.SignalEventBuffer = NULL;
302 gHvContext.SignalEventParam = NULL;
3e7ee490 303
a73e6b7c 304 if (gHvContext.HypercallPage) {
f6feebe0
HZ
305 hypercallMsr.as_uint64 = 0;
306 wrmsrl(HV_X64_MSR_HYPERCALL, hypercallMsr.as_uint64);
a73e6b7c
HJ
307 vfree(gHvContext.HypercallPage);
308 gHvContext.HypercallPage = NULL;
3e7ee490 309 }
3e7ee490
HJ
310}
311
3e189519 312/*
0831ad04
GKH
313 * HvPostMessage - Post a message using the hypervisor message IPC.
314 *
315 * This involves a hypercall.
316 */
034469e6
GKH
317u16 HvPostMessage(union hv_connection_id connectionId,
318 enum hv_message_type messageType,
319 void *payload, size_t payloadSize)
3e7ee490
HJ
320{
321 struct alignedInput {
0831ad04 322 u64 alignment8;
cba4decd 323 struct hv_input_post_message msg;
3e7ee490
HJ
324 };
325
cba4decd 326 struct hv_input_post_message *alignedMsg;
034469e6 327 u16 status;
c4b0bc94 328 unsigned long addr;
3e7ee490
HJ
329
330 if (payloadSize > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
3e7ee490 331 return -1;
3e7ee490 332
0a72f3cf 333 addr = (unsigned long)kmalloc(sizeof(struct alignedInput), GFP_ATOMIC);
3e7ee490 334 if (!addr)
3e7ee490 335 return -1;
3e7ee490 336
0831ad04
GKH
337 alignedMsg = (struct hv_input_post_message *)
338 (ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
3e7ee490 339
f6feebe0
HZ
340 alignedMsg->connectionid = connectionId;
341 alignedMsg->message_type = messageType;
342 alignedMsg->payload_size = payloadSize;
343 memcpy((void *)alignedMsg->payload, payload, payloadSize);
3e7ee490 344
f6feebe0 345 status = HvDoHypercall(HVCALL_POST_MESSAGE, alignedMsg, NULL) & 0xFFFF;
3e7ee490 346
0831ad04 347 kfree((void *)addr);
3e7ee490
HJ
348
349 return status;
350}
351
352
3e189519 353/*
0831ad04
GKH
354 * HvSignalEvent - Signal an event on the specified connection using the hypervisor event IPC.
355 *
356 * This involves a hypercall.
357 */
034469e6 358u16 HvSignalEvent(void)
3e7ee490 359{
034469e6 360 u16 status;
3e7ee490 361
f6feebe0 362 status = HvDoHypercall(HVCALL_SIGNAL_EVENT, gHvContext.SignalEventParam,
0831ad04 363 NULL) & 0xFFFF;
3e7ee490
HJ
364 return status;
365}
366
3e189519 367/*
0831ad04
GKH
368 * HvSynicInit - Initialize the Synthethic Interrupt Controller.
369 *
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.
373 */
7692fd4d 374void HvSynicInit(void *irqarg)
3e7ee490 375{
0831ad04 376 u64 version;
eacb1b4d
GKH
377 union hv_synic_simp simp;
378 union hv_synic_siefp siefp;
379 union hv_synic_sint sharedSint;
380 union hv_synic_scontrol sctrl;
a73e6b7c 381
7692fd4d
GKH
382 u32 irqVector = *((u32 *)(irqarg));
383 int cpu = smp_processor_id();
3e7ee490 384
83c720ea 385 if (!gHvContext.HypercallPage)
7692fd4d 386 return;
3e7ee490 387
454f18a9 388 /* Check the version */
a51ed7d6 389 rdmsrl(HV_X64_MSR_SVERSION, version);
3e7ee490
HJ
390
391 DPRINT_INFO(VMBUS, "SynIC version: %llx", version);
392
a73e6b7c 393 gHvContext.synICMessagePage[cpu] = (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 394
a73e6b7c
HJ
395 if (gHvContext.synICMessagePage[cpu] == NULL) {
396 DPRINT_ERR(VMBUS,
397 "unable to allocate SYNIC message page!!");
398 goto Cleanup;
399 }
3e7ee490 400
a73e6b7c 401 gHvContext.synICEventPage[cpu] = (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 402
a73e6b7c
HJ
403 if (gHvContext.synICEventPage[cpu] == NULL) {
404 DPRINT_ERR(VMBUS,
405 "unable to allocate SYNIC event page!!");
406 goto Cleanup;
407 }
3e7ee490 408
a73e6b7c 409 /* Setup the Synic's message page */
f6feebe0
HZ
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])
a73e6b7c 413 >> PAGE_SHIFT;
3e7ee490 414
f6feebe0 415 DPRINT_DBG(VMBUS, "HV_X64_MSR_SIMP msr set to: %llx", simp.as_uint64);
3e7ee490 416
f6feebe0 417 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 418
a73e6b7c 419 /* Setup the Synic's event page */
f6feebe0
HZ
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])
a73e6b7c
HJ
423 >> PAGE_SHIFT;
424
f6feebe0 425 DPRINT_DBG(VMBUS, "HV_X64_MSR_SIEFP msr set to: %llx", siefp.as_uint64);
a73e6b7c 426
f6feebe0 427 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
0831ad04
GKH
428
429 /* Setup the interception SINT. */
a51ed7d6 430 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
f6feebe0 431 /* interceptionSint.as_uint64); */
454f18a9 432
0831ad04 433 /* Setup the shared SINT. */
f6feebe0 434 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
3e7ee490 435
f6feebe0
HZ
436 sharedSint.as_uint64 = 0;
437 sharedSint.vector = irqVector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
438 sharedSint.masked = false;
439 sharedSint.auto_eoi = true;
3e7ee490 440
0831ad04 441 DPRINT_DBG(VMBUS, "HV_X64_MSR_SINT1 msr set to: %llx",
f6feebe0 442 sharedSint.as_uint64);
3e7ee490 443
f6feebe0 444 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
3e7ee490 445
454f18a9 446 /* Enable the global synic bit */
f6feebe0
HZ
447 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
448 sctrl.enable = 1;
3e7ee490 449
f6feebe0 450 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
3e7ee490 451
0e727613 452 gHvContext.SynICInitialized = true;
7692fd4d 453 return;
3e7ee490
HJ
454
455Cleanup:
a73e6b7c
HJ
456 if (gHvContext.synICEventPage[cpu])
457 osd_PageFree(gHvContext.synICEventPage[cpu], 1);
3e7ee490 458
a73e6b7c
HJ
459 if (gHvContext.synICMessagePage[cpu])
460 osd_PageFree(gHvContext.synICMessagePage[cpu], 1);
7692fd4d 461 return;
3e7ee490
HJ
462}
463
3e189519 464/*
0831ad04
GKH
465 * HvSynicCleanup - Cleanup routine for HvSynicInit().
466 */
7692fd4d 467void HvSynicCleanup(void *arg)
3e7ee490 468{
eacb1b4d
GKH
469 union hv_synic_sint sharedSint;
470 union hv_synic_simp simp;
471 union hv_synic_siefp siefp;
7692fd4d 472 int cpu = smp_processor_id();
3e7ee490 473
83c720ea 474 if (!gHvContext.SynICInitialized)
3e7ee490 475 return;
3e7ee490 476
f6feebe0 477 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
3e7ee490 478
f6feebe0 479 sharedSint.masked = 1;
3e7ee490 480
7692fd4d 481 /* Need to correctly cleanup in the case of SMP!!! */
454f18a9 482 /* Disable the interrupt */
f6feebe0 483 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, sharedSint.as_uint64);
3e7ee490 484
f6feebe0
HZ
485 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
486 simp.simp_enabled = 0;
487 simp.base_simp_gpa = 0;
3e7ee490 488
f6feebe0 489 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 490
f6feebe0
HZ
491 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
492 siefp.siefp_enabled = 0;
493 siefp.base_siefp_gpa = 0;
3e7ee490 494
f6feebe0 495 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
3e7ee490 496
a73e6b7c
HJ
497 osd_PageFree(gHvContext.synICMessagePage[cpu], 1);
498 osd_PageFree(gHvContext.synICEventPage[cpu], 1);
3e7ee490 499}
This page took 0.204333 seconds and 5 git commands to generate.