blob: 3e4235c7a47fd30697b4c6d1e9b37b3bf55b7e6b [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. Srinivasanb29ef352014-08-28 18:29:52 -0700141 memset(hv_context.post_msg_page, 0,
142 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800143 memset(hv_context.vp_index, 0,
144 sizeof(int) * NR_CPUS);
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800145 memset(hv_context.event_dpc, 0,
146 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700147
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800148 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700149
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -0700150 /*
151 * Write our OS ID.
152 */
153 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
154 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
Hank Janssena73e6b72010-01-22 19:17:50 +0000155
156 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800157 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000158
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800159 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000160
Hank Janssen98e08702011-03-29 13:58:44 -0700161 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700162 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000163
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800164 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000165
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800166 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
167 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000168
169 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800170 hypercall_msr.as_uint64 = 0;
171 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000172
Hank Janssen98e08702011-03-29 13:58:44 -0700173 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700174 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000175
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800176 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000177
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700178 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700179
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700180cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800181 if (virtaddr) {
182 if (hypercall_msr.enable) {
183 hypercall_msr.as_uint64 = 0;
184 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700185 }
186
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800187 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700188 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700189
190 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700191}
192
Hank Janssen3e189512010-03-04 22:11:00 +0000193/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800194 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700195 *
196 * This routine is called normally during driver unloading or exiting.
197 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800198void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800200 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700201
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800202 /* Reset our OS id */
203 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
204
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800205 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800206 hypercall_msr.as_uint64 = 0;
207 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800208 vfree(hv_context.hypercall_page);
209 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700211}
212
Hank Janssen3e189512010-03-04 22:11:00 +0000213/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800214 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700215 *
216 * This involves a hypercall.
217 */
Dan Carpenter415f0a02012-03-28 09:58:07 +0300218int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800219 enum hv_message_type message_type,
220 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800223 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700224 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700225
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800226 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700227 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700228
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800229 aligned_msg = (struct hv_input_post_message *)
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700230 hv_context.post_msg_page[get_cpu()];
Hank Janssen3e7ee492009-07-13 16:02:34 -0700231
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800232 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700233 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800234 aligned_msg->message_type = message_type;
235 aligned_msg->payload_size = payload_size;
236 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700237
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800238 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
239 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700240
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700241 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242 return status;
243}
244
245
Hank Janssen3e189512010-03-04 22:11:00 +0000246/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800247 * hv_signal_event -
248 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700249 *
250 * This involves a hypercall.
251 */
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800252u16 hv_signal_event(void *con_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700254 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800256 status = (do_hypercall(HVCALL_SIGNAL_EVENT, con_id, NULL) & 0xFFFF);
257
Hank Janssen3e7ee492009-07-13 16:02:34 -0700258 return status;
259}
260
Jason Wang2608fb62013-06-19 11:28:10 +0800261
262int hv_synic_alloc(void)
263{
264 size_t size = sizeof(struct tasklet_struct);
265 int cpu;
266
267 for_each_online_cpu(cpu) {
268 hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
269 if (hv_context.event_dpc[cpu] == NULL) {
270 pr_err("Unable to allocate event dpc\n");
271 goto err;
272 }
273 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
274
275 hv_context.synic_message_page[cpu] =
276 (void *)get_zeroed_page(GFP_ATOMIC);
277
278 if (hv_context.synic_message_page[cpu] == NULL) {
279 pr_err("Unable to allocate SYNIC message page\n");
280 goto err;
281 }
282
283 hv_context.synic_event_page[cpu] =
284 (void *)get_zeroed_page(GFP_ATOMIC);
285
286 if (hv_context.synic_event_page[cpu] == NULL) {
287 pr_err("Unable to allocate SYNIC event page\n");
288 goto err;
289 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700290
291 hv_context.post_msg_page[cpu] =
292 (void *)get_zeroed_page(GFP_ATOMIC);
293
294 if (hv_context.post_msg_page[cpu] == NULL) {
295 pr_err("Unable to allocate post msg page\n");
296 goto err;
297 }
Jason Wang2608fb62013-06-19 11:28:10 +0800298 }
299
300 return 0;
301err:
302 return -ENOMEM;
303}
304
Rashika Kheria87129542013-12-14 19:00:06 +0530305static void hv_synic_free_cpu(int cpu)
Jason Wang2608fb62013-06-19 11:28:10 +0800306{
307 kfree(hv_context.event_dpc[cpu]);
Felipe Penafdf91da2013-10-15 20:22:32 -0300308 if (hv_context.synic_event_page[cpu])
Jason Wang2608fb62013-06-19 11:28:10 +0800309 free_page((unsigned long)hv_context.synic_event_page[cpu]);
310 if (hv_context.synic_message_page[cpu])
311 free_page((unsigned long)hv_context.synic_message_page[cpu]);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700312 if (hv_context.post_msg_page[cpu])
313 free_page((unsigned long)hv_context.post_msg_page[cpu]);
Jason Wang2608fb62013-06-19 11:28:10 +0800314}
315
316void hv_synic_free(void)
317{
318 int cpu;
319
320 for_each_online_cpu(cpu)
321 hv_synic_free_cpu(cpu);
322}
323
Hank Janssen3e189512010-03-04 22:11:00 +0000324/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800325 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700326 *
327 * If it is already initialized by another entity (ie x2v shim), we need to
328 * retrieve the initialized message and event pages. Otherwise, we create and
329 * initialize the message and event pages.
330 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800331void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700332{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700333 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700334 union hv_synic_simp simp;
335 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800336 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700337 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800338 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000339
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800340 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700341
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800342 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800343 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700344
Bill Pemberton454f18a2009-07-27 16:47:24 -0400345 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700346 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700347
Hank Janssena73e6b72010-01-22 19:17:50 +0000348 /* 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;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800368 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800369 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800370 shared_sint.auto_eoi = true;
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;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800381
382 /*
383 * Setup the mapping between Hyper-V's notion
384 * of cpuid and Linux' notion of cpuid.
385 * This array will be indexed using Linux cpuid.
386 */
387 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
388 hv_context.vp_index[cpu] = (u32)vp_index;
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700389
390 INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800391 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700392}
393
Hank Janssen3e189512010-03-04 22:11:00 +0000394/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800395 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700396 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800397void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700398{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800399 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700400 union hv_synic_simp simp;
401 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800402 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800404 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700406
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800407 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700408
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800409 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800411 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400412 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800413 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800415 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
416 simp.simp_enabled = 0;
417 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800419 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800421 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
422 siefp.siefp_enabled = 0;
423 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800425 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800427 free_page((unsigned long)hv_context.synic_message_page[cpu]);
428 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700429}