Staging: hv: vmbus: Cleanup error handling in vmbus_bus_init()
[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 */
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>
3f335ea2
S
28
29#include "hyperv.h"
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;
114 volatile 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;
135 volatile 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{
0831ad04 154 int ret = 0;
b8dfb264
HZ
155 int max_leaf;
156 union hv_x64_msr_hypercall_contents hypercall_msr;
157 void *virtaddr = NULL;
3e7ee490 158
6a0aaa18
HZ
159 memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS);
160 memset(hv_context.synic_message_page, 0,
161 sizeof(void *) * MAX_NUM_CPUS);
3e7ee490 162
98e08702 163 if (!query_hypervisor_presence())
44939d37 164 goto cleanup;
3e7ee490 165
d44890c8 166 max_leaf = query_hypervisor_info();
0831ad04 167 /* HvQueryHypervisorFeatures(maxLeaf); */
3e7ee490 168
0831ad04 169 /*
a73e6b7c 170 * We only support running on top of Hyper-V
0831ad04 171 */
6a0aaa18 172 rdmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
a73e6b7c 173
98e08702 174 if (hv_context.guestid != 0)
44939d37 175 goto cleanup;
3e7ee490 176
a73e6b7c
HJ
177 /* Write our OS info */
178 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
6a0aaa18 179 hv_context.guestid = HV_LINUX_GUEST_ID;
a73e6b7c 180
454f18a9 181 /* See if the hypercall page is already set */
b8dfb264 182 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
3e7ee490 183
a73e6b7c
HJ
184 /*
185 * Allocate the hypercall page memory
203df82d 186 * virtaddr = osd_page_alloc(1);
a73e6b7c 187 */
df3493e0 188 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
3e7ee490 189
98e08702 190 if (!virtaddr)
44939d37 191 goto cleanup;
3e7ee490 192
b8dfb264 193 hypercall_msr.enable = 1;
a73e6b7c 194
b8dfb264
HZ
195 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
196 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
a73e6b7c
HJ
197
198 /* Confirm that hypercall page did get setup. */
b8dfb264
HZ
199 hypercall_msr.as_uint64 = 0;
200 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
a73e6b7c 201
98e08702 202 if (!hypercall_msr.enable)
44939d37 203 goto cleanup;
3e7ee490 204
b8dfb264 205 hv_context.hypercall_page = virtaddr;
a73e6b7c 206
454f18a9 207 /* Setup the global signal event param for the signal event hypercall */
6a0aaa18 208 hv_context.signal_event_buffer =
0831ad04
GKH
209 kmalloc(sizeof(struct hv_input_signal_event_buffer),
210 GFP_KERNEL);
6a0aaa18 211 if (!hv_context.signal_event_buffer)
44939d37 212 goto cleanup;
3e7ee490 213
6a0aaa18 214 hv_context.signal_event_param =
0831ad04 215 (struct hv_input_signal_event *)
73509681 216 (ALIGN((unsigned long)
6a0aaa18 217 hv_context.signal_event_buffer,
0831ad04 218 HV_HYPERCALL_PARAM_ALIGN));
6a0aaa18
HZ
219 hv_context.signal_event_param->connectionid.asu32 = 0;
220 hv_context.signal_event_param->connectionid.u.id =
0831ad04 221 VMBUS_EVENT_CONNECTION_ID;
6a0aaa18
HZ
222 hv_context.signal_event_param->flag_number = 0;
223 hv_context.signal_event_param->rsvdz = 0;
3e7ee490 224
0831ad04 225 return ret;
3e7ee490 226
44939d37 227cleanup:
b8dfb264
HZ
228 if (virtaddr) {
229 if (hypercall_msr.enable) {
230 hypercall_msr.as_uint64 = 0;
231 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
3e7ee490
HJ
232 }
233
b8dfb264 234 vfree(virtaddr);
3e7ee490
HJ
235 }
236 ret = -1;
3e7ee490
HJ
237 return ret;
238}
239
3e189519 240/*
d44890c8 241 * hv_cleanup - Cleanup routine.
0831ad04
GKH
242 *
243 * This routine is called normally during driver unloading or exiting.
244 */
d44890c8 245void hv_cleanup(void)
3e7ee490 246{
b8dfb264 247 union hv_x64_msr_hypercall_contents hypercall_msr;
3e7ee490 248
6a0aaa18
HZ
249 kfree(hv_context.signal_event_buffer);
250 hv_context.signal_event_buffer = NULL;
251 hv_context.signal_event_param = NULL;
3e7ee490 252
6a0aaa18 253 if (hv_context.hypercall_page) {
b8dfb264
HZ
254 hypercall_msr.as_uint64 = 0;
255 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
6a0aaa18
HZ
256 vfree(hv_context.hypercall_page);
257 hv_context.hypercall_page = NULL;
3e7ee490 258 }
3e7ee490
HJ
259}
260
3e189519 261/*
d44890c8 262 * hv_post_message - Post a message using the hypervisor message IPC.
0831ad04
GKH
263 *
264 * This involves a hypercall.
265 */
d44890c8 266u16 hv_post_message(union hv_connection_id connection_id,
b8dfb264
HZ
267 enum hv_message_type message_type,
268 void *payload, size_t payload_size)
3e7ee490 269{
b8dfb264 270 struct aligned_input {
0831ad04 271 u64 alignment8;
cba4decd 272 struct hv_input_post_message msg;
3e7ee490
HJ
273 };
274
b8dfb264 275 struct hv_input_post_message *aligned_msg;
034469e6 276 u16 status;
c4b0bc94 277 unsigned long addr;
3e7ee490 278
b8dfb264 279 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
3e7ee490 280 return -1;
3e7ee490 281
b8dfb264 282 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
3e7ee490 283 if (!addr)
3e7ee490 284 return -1;
3e7ee490 285
b8dfb264 286 aligned_msg = (struct hv_input_post_message *)
73509681 287 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
3e7ee490 288
b8dfb264
HZ
289 aligned_msg->connectionid = connection_id;
290 aligned_msg->message_type = message_type;
291 aligned_msg->payload_size = payload_size;
292 memcpy((void *)aligned_msg->payload, payload, payload_size);
3e7ee490 293
d44890c8
HZ
294 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
295 & 0xFFFF;
3e7ee490 296
0831ad04 297 kfree((void *)addr);
3e7ee490
HJ
298
299 return status;
300}
301
302
3e189519 303/*
d44890c8
HZ
304 * hv_signal_event -
305 * Signal an event on the specified connection using the hypervisor event IPC.
0831ad04
GKH
306 *
307 * This involves a hypercall.
308 */
d44890c8 309u16 hv_signal_event(void)
3e7ee490 310{
034469e6 311 u16 status;
3e7ee490 312
d44890c8 313 status = do_hypercall(HVCALL_SIGNAL_EVENT,
6a0aaa18 314 hv_context.signal_event_param,
0831ad04 315 NULL) & 0xFFFF;
3e7ee490
HJ
316 return status;
317}
318
3e189519 319/*
d44890c8 320 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
0831ad04
GKH
321 *
322 * If it is already initialized by another entity (ie x2v shim), we need to
323 * retrieve the initialized message and event pages. Otherwise, we create and
324 * initialize the message and event pages.
325 */
d44890c8 326void hv_synic_init(void *irqarg)
3e7ee490 327{
0831ad04 328 u64 version;
eacb1b4d
GKH
329 union hv_synic_simp simp;
330 union hv_synic_siefp siefp;
b8dfb264 331 union hv_synic_sint shared_sint;
eacb1b4d 332 union hv_synic_scontrol sctrl;
a73e6b7c 333
b8dfb264 334 u32 irq_vector = *((u32 *)(irqarg));
7692fd4d 335 int cpu = smp_processor_id();
3e7ee490 336
6a0aaa18 337 if (!hv_context.hypercall_page)
7692fd4d 338 return;
3e7ee490 339
454f18a9 340 /* Check the version */
a51ed7d6 341 rdmsrl(HV_X64_MSR_SVERSION, version);
3e7ee490 342
6a0aaa18
HZ
343 hv_context.synic_message_page[cpu] =
344 (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 345
6a0aaa18 346 if (hv_context.synic_message_page[cpu] == NULL) {
0a46618d 347 pr_err("Unable to allocate SYNIC message page\n");
44939d37 348 goto cleanup;
a73e6b7c 349 }
3e7ee490 350
6a0aaa18
HZ
351 hv_context.synic_event_page[cpu] =
352 (void *)get_zeroed_page(GFP_ATOMIC);
3e7ee490 353
6a0aaa18 354 if (hv_context.synic_event_page[cpu] == NULL) {
0a46618d 355 pr_err("Unable to allocate SYNIC event page\n");
44939d37 356 goto cleanup;
a73e6b7c 357 }
3e7ee490 358
a73e6b7c 359 /* Setup the Synic's message page */
f6feebe0
HZ
360 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
361 simp.simp_enabled = 1;
6a0aaa18 362 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
a73e6b7c 363 >> PAGE_SHIFT;
3e7ee490 364
f6feebe0 365 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 366
a73e6b7c 367 /* Setup the Synic's event page */
f6feebe0
HZ
368 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
369 siefp.siefp_enabled = 1;
6a0aaa18 370 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
a73e6b7c
HJ
371 >> PAGE_SHIFT;
372
f6feebe0 373 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
0831ad04 374
0831ad04 375 /* Setup the shared SINT. */
b8dfb264 376 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 377
b8dfb264
HZ
378 shared_sint.as_uint64 = 0;
379 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
380 shared_sint.masked = false;
381 shared_sint.auto_eoi = true;
3e7ee490 382
b8dfb264 383 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 384
454f18a9 385 /* Enable the global synic bit */
f6feebe0
HZ
386 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
387 sctrl.enable = 1;
3e7ee490 388
f6feebe0 389 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
3e7ee490 390
6a0aaa18 391 hv_context.synic_initialized = true;
7692fd4d 392 return;
3e7ee490 393
44939d37 394cleanup:
6a0aaa18 395 if (hv_context.synic_event_page[cpu])
df3493e0 396 free_page((unsigned long)hv_context.synic_event_page[cpu]);
3e7ee490 397
6a0aaa18 398 if (hv_context.synic_message_page[cpu])
df3493e0 399 free_page((unsigned long)hv_context.synic_message_page[cpu]);
7692fd4d 400 return;
3e7ee490
HJ
401}
402
3e189519 403/*
d44890c8 404 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
0831ad04 405 */
d44890c8 406void hv_synic_cleanup(void *arg)
3e7ee490 407{
b8dfb264 408 union hv_synic_sint shared_sint;
eacb1b4d
GKH
409 union hv_synic_simp simp;
410 union hv_synic_siefp siefp;
7692fd4d 411 int cpu = smp_processor_id();
3e7ee490 412
6a0aaa18 413 if (!hv_context.synic_initialized)
3e7ee490 414 return;
3e7ee490 415
b8dfb264 416 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 417
b8dfb264 418 shared_sint.masked = 1;
3e7ee490 419
7692fd4d 420 /* Need to correctly cleanup in the case of SMP!!! */
454f18a9 421 /* Disable the interrupt */
b8dfb264 422 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 423
f6feebe0
HZ
424 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
425 simp.simp_enabled = 0;
426 simp.base_simp_gpa = 0;
3e7ee490 427
f6feebe0 428 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
3e7ee490 429
f6feebe0
HZ
430 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
431 siefp.siefp_enabled = 0;
432 siefp.base_siefp_gpa = 0;
3e7ee490 433
f6feebe0 434 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
3e7ee490 435
df3493e0
S
436 free_page((unsigned long)hv_context.synic_message_page[cpu]);
437 free_page((unsigned long)hv_context.synic_event_page[cpu]);
3e7ee490 438}
This page took 0.338334 seconds and 5 git commands to generate.