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