blob: 15956bd48b48b327744a36338bb420e957731944 [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>
Greg Kroah-Hartman407dd162011-10-11 08:36:44 -060029#include <asm/hyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070030#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070031
Bill Pemberton454f18a2009-07-27 16:47:24 -040032/* The one and only */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -080033struct hv_context hv_context = {
34 .synic_initialized = false,
35 .hypercall_page = NULL,
36 .signal_event_param = NULL,
37 .signal_event_buffer = NULL,
Hank Janssen3e7ee492009-07-13 16:02:34 -070038};
39
Hank Janssen3e189512010-03-04 22:11:00 +000040/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080041 * query_hypervisor_presence
Lucas De Marchi25985ed2011-03-30 22:57:33 -030042 * - Query the cpuid for presence of windows hypervisor
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070043 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080044static int query_hypervisor_presence(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070045{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070046 unsigned int eax;
47 unsigned int ebx;
48 unsigned int ecx;
49 unsigned int edx;
50 unsigned int op;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Bill Pemberton454f18a2009-07-27 16:47:24 -040052 eax = 0;
53 ebx = 0;
54 ecx = 0;
55 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080056 op = HVCPUID_VERSION_FEATURES;
Greg Kroah-Hartmanf931a702009-07-29 15:37:12 -070057 cpuid(op, &eax, &ebx, &ecx, &edx);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070058
59 return ecx & HV_PRESENT_BIT;
Hank Janssen3e7ee492009-07-13 16:02:34 -070060}
61
Hank Janssen3e189512010-03-04 22:11:00 +000062/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080063 * query_hypervisor_info - Get version info of the windows hypervisor
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070064 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080065static int query_hypervisor_info(void)
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070066{
67 unsigned int eax;
68 unsigned int ebx;
69 unsigned int ecx;
70 unsigned int edx;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080071 unsigned int max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070072 unsigned int op;
Hank Janssen3e7ee492009-07-13 16:02:34 -070073
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070074 /*
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;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080082 op = HVCPUID_VENDOR_MAXFUNCTION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070083 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen3e7ee492009-07-13 16:02:34 -070084
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080085 max_leaf = eax;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070086
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080087 if (max_leaf >= HVCPUID_VERSION) {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070088 eax = 0;
89 ebx = 0;
90 ecx = 0;
91 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080092 op = HVCPUID_VERSION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070093 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen0a466182011-03-29 13:58:47 -070094 pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n",
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070095 eax,
96 ebx >> 16,
97 ebx & 0xFFFF,
98 ecx,
99 edx >> 24,
100 edx & 0xFFFFFF);
101 }
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800102 return max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700103}
104
Hank Janssen3e189512010-03-04 22:11:00 +0000105/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800106 * do_hypercall- Invoke the specified hypercall
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700107 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800108static u64 do_hypercall(u64 control, void *input, void *output)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109{
Greg Kroah-Hartman530cf202009-07-16 22:31:15 -0700110#ifdef CONFIG_X86_64
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800111 u64 hv_status = 0;
112 u64 input_address = (input) ? virt_to_phys(input) : 0;
113 u64 output_address = (output) ? virt_to_phys(output) : 0;
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__("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));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700120
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800121 return hv_status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
123#else
124
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800125 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;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -0700135 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700136
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800137 __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));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800143 return hv_status_lo | ((u64)hv_status_hi << 32);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700144#endif /* !x86_64 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145}
146
Hank Janssen3e189512010-03-04 22:11:00 +0000147/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800148 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700149 *
150 * This routine must be called before any other routines in here are called
151 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800152int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700153{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800154 int max_leaf;
155 union hv_x64_msr_hypercall_contents hypercall_msr;
156 void *virtaddr = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700157
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800158 memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800159 memset(hv_context.synic_message_page, 0,
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800160 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700161
Hank Janssen98e08702011-03-29 13:58:44 -0700162 if (!query_hypervisor_presence())
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700163 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700164
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800165 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700166
Hank Janssena73e6b72010-01-22 19:17:50 +0000167 /* Write our OS info */
168 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800169 hv_context.guestid = HV_LINUX_GUEST_ID;
Hank Janssena73e6b72010-01-22 19:17:50 +0000170
171 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800172 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000173
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800174 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000175
Hank Janssen98e08702011-03-29 13:58:44 -0700176 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700177 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000178
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800179 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000180
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800181 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
182 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000183
184 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800185 hypercall_msr.as_uint64 = 0;
186 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000187
Hank Janssen98e08702011-03-29 13:58:44 -0700188 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700189 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000190
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800191 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000192
Bill Pemberton454f18a2009-07-27 16:47:24 -0400193 /* Setup the global signal event param for the signal event hypercall */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800194 hv_context.signal_event_buffer =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700195 kmalloc(sizeof(struct hv_input_signal_event_buffer),
196 GFP_KERNEL);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800197 if (!hv_context.signal_event_buffer)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700198 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800200 hv_context.signal_event_param =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700201 (struct hv_input_signal_event *)
Uwe Kleine-König73509682011-01-20 09:32:01 +0100202 (ALIGN((unsigned long)
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800203 hv_context.signal_event_buffer,
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700204 HV_HYPERCALL_PARAM_ALIGN));
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800205 hv_context.signal_event_param->connectionid.asu32 = 0;
206 hv_context.signal_event_param->connectionid.u.id =
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700207 VMBUS_EVENT_CONNECTION_ID;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800208 hv_context.signal_event_param->flag_number = 0;
209 hv_context.signal_event_param->rsvdz = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700211 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700213cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800214 if (virtaddr) {
215 if (hypercall_msr.enable) {
216 hypercall_msr.as_uint64 = 0;
217 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218 }
219
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800220 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700222
223 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700224}
225
Hank Janssen3e189512010-03-04 22:11:00 +0000226/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800227 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700228 *
229 * This routine is called normally during driver unloading or exiting.
230 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800231void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700232{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800233 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700234
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800235 /* Reset our OS id */
236 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
237
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800238 kfree(hv_context.signal_event_buffer);
239 hv_context.signal_event_buffer = NULL;
240 hv_context.signal_event_param = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700241
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800242 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800243 hypercall_msr.as_uint64 = 0;
244 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800245 vfree(hv_context.hypercall_page);
246 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700247 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248}
249
Hank Janssen3e189512010-03-04 22:11:00 +0000250/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800251 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700252 *
253 * This involves a hypercall.
254 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800255u16 hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800256 enum hv_message_type message_type,
257 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800259 struct aligned_input {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700260 u64 alignment8;
Greg Kroah-Hartmancba4dec2009-08-19 16:21:28 -0700261 struct hv_input_post_message msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262 };
263
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800264 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700265 u16 status;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700266 unsigned long addr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700267
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800268 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700269 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700270
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800271 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272 if (!addr)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700273 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700274
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800275 aligned_msg = (struct hv_input_post_message *)
Uwe Kleine-König73509682011-01-20 09:32:01 +0100276 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800278 aligned_msg->connectionid = connection_id;
279 aligned_msg->message_type = message_type;
280 aligned_msg->payload_size = payload_size;
281 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700282
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800283 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
284 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700285
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700286 kfree((void *)addr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287
288 return status;
289}
290
291
Hank Janssen3e189512010-03-04 22:11:00 +0000292/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800293 * hv_signal_event -
294 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700295 *
296 * This involves a hypercall.
297 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800298u16 hv_signal_event(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700299{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700300 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800302 status = do_hypercall(HVCALL_SIGNAL_EVENT,
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800303 hv_context.signal_event_param,
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700304 NULL) & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305 return status;
306}
307
Hank Janssen3e189512010-03-04 22:11:00 +0000308/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800309 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700310 *
311 * If it is already initialized by another entity (ie x2v shim), we need to
312 * retrieve the initialized message and event pages. Otherwise, we create and
313 * initialize the message and event pages.
314 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800315void hv_synic_init(void *irqarg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700316{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700317 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700318 union hv_synic_simp simp;
319 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800320 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700321 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000322
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800323 u32 irq_vector = *((u32 *)(irqarg));
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800324 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700325
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800326 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800327 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700328
Bill Pemberton454f18a2009-07-27 16:47:24 -0400329 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700330 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800332 hv_context.synic_message_page[cpu] =
333 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800335 if (hv_context.synic_message_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700336 pr_err("Unable to allocate SYNIC message page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700337 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700339
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800340 hv_context.synic_event_page[cpu] =
341 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000342
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800343 if (hv_context.synic_event_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700344 pr_err("Unable to allocate SYNIC event page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700345 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000346 }
347
348 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800349 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
350 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800351 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000352 >> PAGE_SHIFT;
353
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800354 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000355
356 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800357 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
358 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800359 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000360 >> PAGE_SHIFT;
361
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800362 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000363
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700364 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800365 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700366
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800367 shared_sint.as_uint64 = 0;
368 shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
369 shared_sint.masked = false;
K. Y. Srinivasan93511ba2011-08-27 11:31:37 -0700370 shared_sint.auto_eoi = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700371
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800372 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700373
Bill Pemberton454f18a2009-07-27 16:47:24 -0400374 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800375 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
376 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700377
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800378 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800380 hv_context.synic_initialized = true;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800381 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700383cleanup:
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800384 if (hv_context.synic_event_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800385 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700386
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800387 if (hv_context.synic_message_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800388 free_page((unsigned long)hv_context.synic_message_page[cpu]);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800389 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700390}
391
Hank Janssen3e189512010-03-04 22:11:00 +0000392/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800393 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700394 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800395void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700396{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800397 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700398 union hv_synic_simp simp;
399 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800400 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800402 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700404
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800405 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800407 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800409 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400410 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800411 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700412
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800413 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
414 simp.simp_enabled = 0;
415 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800417 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800419 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
420 siefp.siefp_enabled = 0;
421 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700422
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800423 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800425 free_page((unsigned long)hv_context.synic_message_page[cpu]);
426 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427}