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