blob: 3648f8f0f36886282da6abf3bb2df05a355d3001 [file] [log] [blame]
Hank Janssen3e7ee492009-07-13 16:02:34 -07001/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07002 * 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 */
Hank Janssen0a466182011-03-29 13:58:47 -070022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070024#include <linux/kernel.h>
25#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Bill Pembertonb7c947f2009-07-29 17:00:13 -040027#include <linux/vmalloc.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070028#include <linux/hyperv.h>
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -070029#include <linux/version.h>
Greg Kroah-Hartman407dd162011-10-11 08:36:44 -060030#include <asm/hyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070031#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070032
Bill Pemberton454f18a2009-07-27 16:47:24 -040033/* The one and only */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -080034struct hv_context hv_context = {
35 .synic_initialized = false,
36 .hypercall_page = NULL,
37 .signal_event_param = NULL,
38 .signal_event_buffer = NULL,
Hank Janssen3e7ee492009-07-13 16:02:34 -070039};
40
Hank Janssen3e189512010-03-04 22:11:00 +000041/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080042 * query_hypervisor_info - Get version info of the windows hypervisor
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070043 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080044static int query_hypervisor_info(void)
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070045{
46 unsigned int eax;
47 unsigned int ebx;
48 unsigned int ecx;
49 unsigned int edx;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080050 unsigned int max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070051 unsigned int op;
Hank Janssen3e7ee492009-07-13 16:02:34 -070052
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070053 /*
54 * Its assumed that this is called after confirming that Viridian
55 * is present. Query id and revision.
56 */
57 eax = 0;
58 ebx = 0;
59 ecx = 0;
60 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080061 op = HVCPUID_VENDOR_MAXFUNCTION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070062 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen3e7ee492009-07-13 16:02:34 -070063
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080064 max_leaf = eax;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070065
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080066 if (max_leaf >= HVCPUID_VERSION) {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070067 eax = 0;
68 ebx = 0;
69 ecx = 0;
70 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080071 op = HVCPUID_VERSION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070072 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen0a466182011-03-29 13:58:47 -070073 pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070074 eax,
75 ebx >> 16,
76 ebx & 0xFFFF,
77 ecx,
78 edx >> 24,
79 edx & 0xFFFFFF);
80 }
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080081 return max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070082}
83
Hank Janssen3e189512010-03-04 22:11:00 +000084/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080085 * do_hypercall- Invoke the specified hypercall
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070086 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080087static u64 do_hypercall(u64 control, void *input, void *output)
Hank Janssen3e7ee492009-07-13 16:02:34 -070088{
Greg Kroah-Hartman530cf202009-07-16 22:31:15 -070089#ifdef CONFIG_X86_64
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080090 u64 hv_status = 0;
91 u64 input_address = (input) ? virt_to_phys(input) : 0;
92 u64 output_address = (output) ? virt_to_phys(output) : 0;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -070093 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070094
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080095 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
96 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
97 "c" (control), "d" (input_address),
98 "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -070099
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800100 return hv_status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
102#else
103
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800104 u32 control_hi = control >> 32;
105 u32 control_lo = control & 0xFFFFFFFF;
106 u32 hv_status_hi = 1;
107 u32 hv_status_lo = 1;
108 u64 input_address = (input) ? virt_to_phys(input) : 0;
109 u32 input_address_hi = input_address >> 32;
110 u32 input_address_lo = input_address & 0xFFFFFFFF;
111 u64 output_address = (output) ? virt_to_phys(output) : 0;
112 u32 output_address_hi = output_address >> 32;
113 u32 output_address_lo = output_address & 0xFFFFFFFF;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -0700114 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700115
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800116 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
117 "=a"(hv_status_lo) : "d" (control_hi),
118 "a" (control_lo), "b" (input_address_hi),
119 "c" (input_address_lo), "D"(output_address_hi),
120 "S"(output_address_lo), "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700121
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800122 return hv_status_lo | ((u64)hv_status_hi << 32);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700123#endif /* !x86_64 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700124}
125
Hank Janssen3e189512010-03-04 22:11:00 +0000126/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800127 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700128 *
129 * This routine must be called before any other routines in here are called
130 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800131int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700132{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800133 int max_leaf;
134 union hv_x64_msr_hypercall_contents hypercall_msr;
135 void *virtaddr = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700136
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800137 memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800138 memset(hv_context.synic_message_page, 0,
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800139 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700140
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800141 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -0700143 /*
144 * Write our OS ID.
145 */
146 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
147 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
Hank Janssena73e6b72010-01-22 19:17:50 +0000148
149 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800150 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000151
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800152 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000153
Hank Janssen98e08702011-03-29 13:58:44 -0700154 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700155 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000156
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800157 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000158
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800159 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
160 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000161
162 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800163 hypercall_msr.as_uint64 = 0;
164 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000165
Hank Janssen98e08702011-03-29 13:58:44 -0700166 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700167 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000168
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800169 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000170
Bill Pemberton454f18a2009-07-27 16:47:24 -0400171 /* Setup the global signal event param for the signal event hypercall */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800172 hv_context.signal_event_buffer =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700173 kmalloc(sizeof(struct hv_input_signal_event_buffer),
174 GFP_KERNEL);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800175 if (!hv_context.signal_event_buffer)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700176 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800178 hv_context.signal_event_param =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700179 (struct hv_input_signal_event *)
Uwe Kleine-König73509682011-01-20 09:32:01 +0100180 (ALIGN((unsigned long)
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800181 hv_context.signal_event_buffer,
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700182 HV_HYPERCALL_PARAM_ALIGN));
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800183 hv_context.signal_event_param->connectionid.asu32 = 0;
184 hv_context.signal_event_param->connectionid.u.id =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700185 VMBUS_EVENT_CONNECTION_ID;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800186 hv_context.signal_event_param->flag_number = 0;
187 hv_context.signal_event_param->rsvdz = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700189 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700191cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800192 if (virtaddr) {
193 if (hypercall_msr.enable) {
194 hypercall_msr.as_uint64 = 0;
195 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700196 }
197
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800198 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700200
201 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202}
203
Hank Janssen3e189512010-03-04 22:11:00 +0000204/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800205 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700206 *
207 * This routine is called normally during driver unloading or exiting.
208 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800209void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800211 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800213 /* Reset our OS id */
214 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
215
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800216 kfree(hv_context.signal_event_buffer);
217 hv_context.signal_event_buffer = NULL;
218 hv_context.signal_event_param = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800220 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800221 hypercall_msr.as_uint64 = 0;
222 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800223 vfree(hv_context.hypercall_page);
224 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700226}
227
Hank Janssen3e189512010-03-04 22:11:00 +0000228/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800229 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700230 *
231 * This involves a hypercall.
232 */
Dan Carpenter415f0a02012-03-28 09:58:07 +0300233int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800234 enum hv_message_type message_type,
235 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800237 struct aligned_input {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700238 u64 alignment8;
Greg Kroah-Hartmancba4dec2009-08-19 16:21:28 -0700239 struct hv_input_post_message msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240 };
241
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800242 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700243 u16 status;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700244 unsigned long addr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800246 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700247 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800249 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700250 if (!addr)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700251 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700252
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800253 aligned_msg = (struct hv_input_post_message *)
Uwe Kleine-König73509682011-01-20 09:32:01 +0100254 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800256 aligned_msg->connectionid = connection_id;
257 aligned_msg->message_type = message_type;
258 aligned_msg->payload_size = payload_size;
259 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700260
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800261 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
262 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700264 kfree((void *)addr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265
266 return status;
267}
268
269
Hank Janssen3e189512010-03-04 22:11:00 +0000270/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800271 * hv_signal_event -
272 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700273 *
274 * This involves a hypercall.
275 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800276u16 hv_signal_event(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700278 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700279
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800280 status = do_hypercall(HVCALL_SIGNAL_EVENT,
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800281 hv_context.signal_event_param,
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700282 NULL) & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700283 return status;
284}
285
Hank Janssen3e189512010-03-04 22:11:00 +0000286/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800287 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700288 *
289 * If it is already initialized by another entity (ie x2v shim), we need to
290 * retrieve the initialized message and event pages. Otherwise, we create and
291 * initialize the message and event pages.
292 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800293void hv_synic_init(void *irqarg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700294{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700295 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700296 union hv_synic_simp simp;
297 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800298 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700299 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000300
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800301 u32 irq_vector = *((u32 *)(irqarg));
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800302 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700303
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800304 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800305 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306
Bill Pemberton454f18a2009-07-27 16:47:24 -0400307 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700308 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700309
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800310 hv_context.synic_message_page[cpu] =
311 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700312
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800313 if (hv_context.synic_message_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700314 pr_err("Unable to allocate SYNIC message page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700315 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700317
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800318 hv_context.synic_event_page[cpu] =
319 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000320
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800321 if (hv_context.synic_event_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700322 pr_err("Unable to allocate SYNIC event page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700323 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000324 }
325
326 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800327 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
328 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800329 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000330 >> PAGE_SHIFT;
331
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800332 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000333
334 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800335 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
336 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800337 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000338 >> PAGE_SHIFT;
339
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800340 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000341
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700342 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800343 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800345 shared_sint.as_uint64 = 0;
346 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
347 shared_sint.masked = false;
K. Y. Srinivasan93511ba2011-08-27 11:31:37 -0700348 shared_sint.auto_eoi = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700349
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800350 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700351
Bill Pemberton454f18a2009-07-27 16:47:24 -0400352 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800353 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
354 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700355
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800356 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700357
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800358 hv_context.synic_initialized = true;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800359 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700360
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700361cleanup:
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800362 if (hv_context.synic_event_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800363 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700364
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800365 if (hv_context.synic_message_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800366 free_page((unsigned long)hv_context.synic_message_page[cpu]);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800367 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368}
369
Hank Janssen3e189512010-03-04 22:11:00 +0000370/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800371 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700372 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800373void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800375 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700376 union hv_synic_simp simp;
377 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800378 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800380 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700381 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800383 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700384
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800385 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800387 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400388 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800389 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800391 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
392 simp.simp_enabled = 0;
393 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800395 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700396
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800397 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
398 siefp.siefp_enabled = 0;
399 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800401 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700402
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800403 free_page((unsigned long)hv_context.synic_message_page[cpu]);
404 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405}