blob: ae4923756d98788490166af3393fecba5a84b6a0 [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>
K. Y. Srinivasandb11f122012-12-01 06:46:53 -080030#include <linux/interrupt.h>
Greg Kroah-Hartman407dd162011-10-11 08:36:44 -060031#include <asm/hyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070032#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070033
Bill Pemberton454f18a2009-07-27 16:47:24 -040034/* The one and only */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -080035struct hv_context hv_context = {
36 .synic_initialized = false,
37 .hypercall_page = 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_info - Get version info of the windows hypervisor
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070042 */
K. Y. Srinivasan5fbebb22012-12-01 06:46:58 -080043unsigned int host_info_eax;
44unsigned int host_info_ebx;
45unsigned int host_info_ecx;
46unsigned int host_info_edx;
47
Haiyang Zhangd44890c2010-11-08 14:04:42 -080048static int query_hypervisor_info(void)
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070049{
50 unsigned int eax;
51 unsigned int ebx;
52 unsigned int ecx;
53 unsigned int edx;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080054 unsigned int max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070055 unsigned int op;
Hank Janssen3e7ee492009-07-13 16:02:34 -070056
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070057 /*
58 * Its assumed that this is called after confirming that Viridian
59 * is present. Query id and revision.
60 */
61 eax = 0;
62 ebx = 0;
63 ecx = 0;
64 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080065 op = HVCPUID_VENDOR_MAXFUNCTION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070066 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen3e7ee492009-07-13 16:02:34 -070067
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080068 max_leaf = eax;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070069
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080070 if (max_leaf >= HVCPUID_VERSION) {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070071 eax = 0;
72 ebx = 0;
73 ecx = 0;
74 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080075 op = HVCPUID_VERSION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070076 cpuid(op, &eax, &ebx, &ecx, &edx);
K. Y. Srinivasan5fbebb22012-12-01 06:46:58 -080077 host_info_eax = eax;
78 host_info_ebx = ebx;
79 host_info_ecx = ecx;
80 host_info_edx = edx;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070081 }
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080082 return max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070083}
84
Hank Janssen3e189512010-03-04 22:11:00 +000085/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080086 * do_hypercall- Invoke the specified hypercall
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070087 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080088static u64 do_hypercall(u64 control, void *input, void *output)
Hank Janssen3e7ee492009-07-13 16:02:34 -070089{
Greg Kroah-Hartman530cf202009-07-16 22:31:15 -070090#ifdef CONFIG_X86_64
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080091 u64 hv_status = 0;
92 u64 input_address = (input) ? virt_to_phys(input) : 0;
93 u64 output_address = (output) ? virt_to_phys(output) : 0;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -070094 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -070095
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080096 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
97 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
98 "c" (control), "d" (input_address),
99 "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700100
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800101 return hv_status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700102
103#else
104
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800105 u32 control_hi = control >> 32;
106 u32 control_lo = control & 0xFFFFFFFF;
107 u32 hv_status_hi = 1;
108 u32 hv_status_lo = 1;
109 u64 input_address = (input) ? virt_to_phys(input) : 0;
110 u32 input_address_hi = input_address >> 32;
111 u32 input_address_lo = input_address & 0xFFFFFFFF;
112 u64 output_address = (output) ? virt_to_phys(output) : 0;
113 u32 output_address_hi = output_address >> 32;
114 u32 output_address_lo = output_address & 0xFFFFFFFF;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -0700115 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700116
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800117 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
118 "=a"(hv_status_lo) : "d" (control_hi),
119 "a" (control_lo), "b" (input_address_hi),
120 "c" (input_address_lo), "D"(output_address_hi),
121 "S"(output_address_lo), "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800123 return hv_status_lo | ((u64)hv_status_hi << 32);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700124#endif /* !x86_64 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700125}
126
Hank Janssen3e189512010-03-04 22:11:00 +0000127/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800128 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700129 *
130 * This routine must be called before any other routines in here are called
131 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800132int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700133{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800134 int max_leaf;
135 union hv_x64_msr_hypercall_contents hypercall_msr;
136 void *virtaddr = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700137
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800138 memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800139 memset(hv_context.synic_message_page, 0,
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800140 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800141 memset(hv_context.vp_index, 0,
142 sizeof(int) * NR_CPUS);
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800143 memset(hv_context.event_dpc, 0,
144 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700145
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800146 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -0700148 /*
149 * Write our OS ID.
150 */
151 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
152 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
Hank Janssena73e6b72010-01-22 19:17:50 +0000153
154 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800155 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000156
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800157 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000158
Hank Janssen98e08702011-03-29 13:58:44 -0700159 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700160 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000161
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800162 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000163
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800164 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
165 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000166
167 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800168 hypercall_msr.as_uint64 = 0;
169 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000170
Hank Janssen98e08702011-03-29 13:58:44 -0700171 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700172 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000173
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800174 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000175
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700176 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700177
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700178cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800179 if (virtaddr) {
180 if (hypercall_msr.enable) {
181 hypercall_msr.as_uint64 = 0;
182 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700183 }
184
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800185 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700186 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700187
188 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700189}
190
Hank Janssen3e189512010-03-04 22:11:00 +0000191/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800192 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700193 *
194 * This routine is called normally during driver unloading or exiting.
195 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800196void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800198 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800200 /* Reset our OS id */
201 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
202
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800203 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800204 hypercall_msr.as_uint64 = 0;
205 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800206 vfree(hv_context.hypercall_page);
207 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700208 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209}
210
Hank Janssen3e189512010-03-04 22:11:00 +0000211/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800212 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700213 *
214 * This involves a hypercall.
215 */
Dan Carpenter415f0a02012-03-28 09:58:07 +0300216int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800217 enum hv_message_type message_type,
218 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800220 struct aligned_input {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700221 u64 alignment8;
Greg Kroah-Hartmancba4dec2009-08-19 16:21:28 -0700222 struct hv_input_post_message msg;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700223 };
224
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800225 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700226 u16 status;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700227 unsigned long addr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800229 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700230 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700231
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800232 addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233 if (!addr)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700234 return -ENOMEM;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700235
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800236 aligned_msg = (struct hv_input_post_message *)
Uwe Kleine-König73509682011-01-20 09:32:01 +0100237 (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800239 aligned_msg->connectionid = connection_id;
240 aligned_msg->message_type = message_type;
241 aligned_msg->payload_size = payload_size;
242 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700243
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800244 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
245 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700246
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700247 kfree((void *)addr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
249 return status;
250}
251
252
Hank Janssen3e189512010-03-04 22:11:00 +0000253/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800254 * hv_signal_event -
255 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700256 *
257 * This involves a hypercall.
258 */
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800259u16 hv_signal_event(void *con_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700260{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700261 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700262
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800263 status = (do_hypercall(HVCALL_SIGNAL_EVENT, con_id, NULL) & 0xFFFF);
264
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265 return status;
266}
267
Hank Janssen3e189512010-03-04 22:11:00 +0000268/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800269 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700270 *
271 * If it is already initialized by another entity (ie x2v shim), we need to
272 * retrieve the initialized message and event pages. Otherwise, we create and
273 * initialize the message and event pages.
274 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800275void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700276{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700277 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700278 union hv_synic_simp simp;
279 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800280 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700281 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800282 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000283
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800284 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700285
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800286 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800287 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700288
Bill Pemberton454f18a2009-07-27 16:47:24 -0400289 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700290 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700291
Zhang Yanfei33349482013-03-12 13:10:40 +0800292 hv_context.event_dpc[cpu] = kmalloc(sizeof(struct tasklet_struct),
293 GFP_ATOMIC);
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800294 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
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800300 hv_context.synic_message_page[cpu] =
301 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700302
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800303 if (hv_context.synic_message_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700304 pr_err("Unable to allocate SYNIC message page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700305 goto cleanup;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700306 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800308 hv_context.synic_event_page[cpu] =
309 (void *)get_zeroed_page(GFP_ATOMIC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000310
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800311 if (hv_context.synic_event_page[cpu] == NULL) {
Hank Janssen0a466182011-03-29 13:58:47 -0700312 pr_err("Unable to allocate SYNIC event page\n");
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700313 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000314 }
315
316 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800317 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
318 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800319 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000320 >> PAGE_SHIFT;
321
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800322 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000323
324 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800325 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
326 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800327 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000328 >> PAGE_SHIFT;
329
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800330 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000331
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700332 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800333 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700334
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800335 shared_sint.as_uint64 = 0;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800336 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800337 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800338 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700339
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800340 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341
Bill Pemberton454f18a2009-07-27 16:47:24 -0400342 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800343 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
344 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700345
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800346 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700347
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800348 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800349
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;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800357 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700359cleanup:
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800360 if (hv_context.synic_event_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800361 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800363 if (hv_context.synic_message_page[cpu])
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800364 free_page((unsigned long)hv_context.synic_message_page[cpu]);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800365 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700366}
367
Hank Janssen3e189512010-03-04 22:11:00 +0000368/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800369 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700370 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800371void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700372{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800373 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700374 union hv_synic_simp simp;
375 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800376 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700377
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800378 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700379 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700380
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800381 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700382
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800383 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700384
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800385 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400386 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800387 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700388
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800389 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
390 simp.simp_enabled = 0;
391 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700392
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800393 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700394
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800395 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
396 siefp.siefp_enabled = 0;
397 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800399 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800401 free_page((unsigned long)hv_context.synic_message_page[cpu]);
402 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403}