blob: 88f4096fa078d7f4e2ae1eeee41fd7188ab1a313 [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
Jason Wang2608fb62013-06-19 11:28:10 +0800268
269int hv_synic_alloc(void)
270{
271 size_t size = sizeof(struct tasklet_struct);
272 int cpu;
273
274 for_each_online_cpu(cpu) {
275 hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
276 if (hv_context.event_dpc[cpu] == NULL) {
277 pr_err("Unable to allocate event dpc\n");
278 goto err;
279 }
280 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
281
282 hv_context.synic_message_page[cpu] =
283 (void *)get_zeroed_page(GFP_ATOMIC);
284
285 if (hv_context.synic_message_page[cpu] == NULL) {
286 pr_err("Unable to allocate SYNIC message page\n");
287 goto err;
288 }
289
290 hv_context.synic_event_page[cpu] =
291 (void *)get_zeroed_page(GFP_ATOMIC);
292
293 if (hv_context.synic_event_page[cpu] == NULL) {
294 pr_err("Unable to allocate SYNIC event page\n");
295 goto err;
296 }
297 }
298
299 return 0;
300err:
301 return -ENOMEM;
302}
303
304void hv_synic_free_cpu(int cpu)
305{
306 kfree(hv_context.event_dpc[cpu]);
307 if (hv_context.synic_message_page[cpu])
308 free_page((unsigned long)hv_context.synic_event_page[cpu]);
309 if (hv_context.synic_message_page[cpu])
310 free_page((unsigned long)hv_context.synic_message_page[cpu]);
311}
312
313void hv_synic_free(void)
314{
315 int cpu;
316
317 for_each_online_cpu(cpu)
318 hv_synic_free_cpu(cpu);
319}
320
Hank Janssen3e189512010-03-04 22:11:00 +0000321/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800322 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700323 *
324 * If it is already initialized by another entity (ie x2v shim), we need to
325 * retrieve the initialized message and event pages. Otherwise, we create and
326 * initialize the message and event pages.
327 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800328void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700329{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700330 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700331 union hv_synic_simp simp;
332 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800333 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700334 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800335 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000336
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800337 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700338
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800339 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800340 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341
Bill Pemberton454f18a2009-07-27 16:47:24 -0400342 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700343 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344
Hank Janssena73e6b72010-01-22 19:17:50 +0000345 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800346 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
347 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800348 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000349 >> PAGE_SHIFT;
350
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800351 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000352
353 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800354 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
355 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800356 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000357 >> PAGE_SHIFT;
358
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800359 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000360
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700361 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800362 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700363
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800364 shared_sint.as_uint64 = 0;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800365 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800366 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800367 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700368
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800369 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700370
Bill Pemberton454f18a2009-07-27 16:47:24 -0400371 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800372 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
373 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700374
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800375 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700376
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800377 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800378
379 /*
380 * Setup the mapping between Hyper-V's notion
381 * of cpuid and Linux' notion of cpuid.
382 * This array will be indexed using Linux cpuid.
383 */
384 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
385 hv_context.vp_index[cpu] = (u32)vp_index;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800386 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700387}
388
Hank Janssen3e189512010-03-04 22:11:00 +0000389/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800390 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700391 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800392void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700393{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800394 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700395 union hv_synic_simp simp;
396 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800397 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800399 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700400 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800402 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800404 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800406 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400407 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800408 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800410 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
411 simp.simp_enabled = 0;
412 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800414 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700415
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800416 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
417 siefp.siefp_enabled = 0;
418 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700419
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800420 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800422 free_page((unsigned long)hv_context.synic_message_page[cpu]);
423 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424}