blob: 50e51a51ff8bde317def929e744a810e2ccaa62d [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>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080031#include <linux/clockchips.h>
Greg Kroah-Hartman407dd1642011-10-11 08:36:44 -060032#include <asm/hyperv.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080033#include <asm/mshyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070034#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070035
Bill Pemberton454f18a2009-07-27 16:47:24 -040036/* The one and only */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -080037struct hv_context hv_context = {
38 .synic_initialized = false,
39 .hypercall_page = NULL,
Hank Janssen3e7ee492009-07-13 16:02:34 -070040};
41
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080042#define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
43#define HV_MAX_MAX_DELTA_TICKS 0xffffffff
44#define HV_MIN_DELTA_TICKS 1
45
Hank Janssen3e189512010-03-04 22:11:00 +000046/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080047 * query_hypervisor_info - Get version info of the windows hypervisor
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070048 */
K. Y. Srinivasan5fbebb22012-12-01 06:46:58 -080049unsigned int host_info_eax;
50unsigned int host_info_ebx;
51unsigned int host_info_ecx;
52unsigned int host_info_edx;
53
Haiyang Zhangd44890c2010-11-08 14:04:42 -080054static int query_hypervisor_info(void)
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070055{
56 unsigned int eax;
57 unsigned int ebx;
58 unsigned int ecx;
59 unsigned int edx;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080060 unsigned int max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070061 unsigned int op;
Hank Janssen3e7ee492009-07-13 16:02:34 -070062
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070063 /*
64 * Its assumed that this is called after confirming that Viridian
65 * is present. Query id and revision.
66 */
67 eax = 0;
68 ebx = 0;
69 ecx = 0;
70 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080071 op = HVCPUID_VENDOR_MAXFUNCTION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070072 cpuid(op, &eax, &ebx, &ecx, &edx);
Hank Janssen3e7ee492009-07-13 16:02:34 -070073
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080074 max_leaf = eax;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070075
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080076 if (max_leaf >= HVCPUID_VERSION) {
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070077 eax = 0;
78 ebx = 0;
79 ecx = 0;
80 edx = 0;
Haiyang Zhangf6feebe2010-11-08 14:04:39 -080081 op = HVCPUID_VERSION;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070082 cpuid(op, &eax, &ebx, &ecx, &edx);
K. Y. Srinivasan5fbebb22012-12-01 06:46:58 -080083 host_info_eax = eax;
84 host_info_ebx = ebx;
85 host_info_ecx = ecx;
86 host_info_edx = edx;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070087 }
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080088 return max_leaf;
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070089}
90
Hank Janssen3e189512010-03-04 22:11:00 +000091/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080092 * do_hypercall- Invoke the specified hypercall
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070093 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080094static u64 do_hypercall(u64 control, void *input, void *output)
Hank Janssen3e7ee492009-07-13 16:02:34 -070095{
Greg Kroah-Hartman530cf202009-07-16 22:31:15 -070096#ifdef CONFIG_X86_64
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080097 u64 hv_status = 0;
98 u64 input_address = (input) ? virt_to_phys(input) : 0;
99 u64 output_address = (output) ? virt_to_phys(output) : 0;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -0700100 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800102 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
103 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
104 "c" (control), "d" (input_address),
105 "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700106
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800107 return hv_status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700108
109#else
110
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800111 u32 control_hi = control >> 32;
112 u32 control_lo = control & 0xFFFFFFFF;
113 u32 hv_status_hi = 1;
114 u32 hv_status_lo = 1;
115 u64 input_address = (input) ? virt_to_phys(input) : 0;
116 u32 input_address_hi = input_address >> 32;
117 u32 input_address_lo = input_address & 0xFFFFFFFF;
118 u64 output_address = (output) ? virt_to_phys(output) : 0;
119 u32 output_address_hi = output_address >> 32;
120 u32 output_address_lo = output_address & 0xFFFFFFFF;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -0700121 void *hypercall_page = hv_context.hypercall_page;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700122
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800123 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
124 "=a"(hv_status_lo) : "d" (control_hi),
125 "a" (control_lo), "b" (input_address_hi),
126 "c" (input_address_lo), "D"(output_address_hi),
127 "S"(output_address_lo), "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700128
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800129 return hv_status_lo | ((u64)hv_status_hi << 32);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700130#endif /* !x86_64 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700131}
132
Hank Janssen3e189512010-03-04 22:11:00 +0000133/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800134 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700135 *
136 * This routine must be called before any other routines in here are called
137 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800138int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700139{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800140 int max_leaf;
141 union hv_x64_msr_hypercall_contents hypercall_msr;
142 void *virtaddr = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700143
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800144 memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800145 memset(hv_context.synic_message_page, 0,
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800146 sizeof(void *) * NR_CPUS);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700147 memset(hv_context.post_msg_page, 0,
148 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800149 memset(hv_context.vp_index, 0,
150 sizeof(int) * NR_CPUS);
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800151 memset(hv_context.event_dpc, 0,
152 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800153 memset(hv_context.clk_evt, 0,
154 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700155
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800156 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700157
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -0700158 /*
159 * Write our OS ID.
160 */
161 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
162 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
Hank Janssena73e6b72010-01-22 19:17:50 +0000163
164 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800165 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000166
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800167 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000168
Hank Janssen98e08702011-03-29 13:58:44 -0700169 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700170 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000171
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800172 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000173
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800174 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
175 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000176
177 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800178 hypercall_msr.as_uint64 = 0;
179 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000180
Hank Janssen98e08702011-03-29 13:58:44 -0700181 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700182 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000183
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800184 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000185
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700186 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700187
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700188cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800189 if (virtaddr) {
190 if (hypercall_msr.enable) {
191 hypercall_msr.as_uint64 = 0;
192 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193 }
194
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800195 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700196 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700197
198 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199}
200
Hank Janssen3e189512010-03-04 22:11:00 +0000201/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800202 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700203 *
204 * This routine is called normally during driver unloading or exiting.
205 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800206void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700207{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800208 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800210 /* Reset our OS id */
211 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
212
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800213 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800214 hypercall_msr.as_uint64 = 0;
215 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800216 vfree(hv_context.hypercall_page);
217 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700218 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700219}
220
Hank Janssen3e189512010-03-04 22:11:00 +0000221/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800222 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700223 *
224 * This involves a hypercall.
225 */
Dan Carpenter415f0a02012-03-28 09:58:07 +0300226int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800227 enum hv_message_type message_type,
228 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700229{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700230
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800231 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700232 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800234 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700235 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800237 aligned_msg = (struct hv_input_post_message *)
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700238 hv_context.post_msg_page[get_cpu()];
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800240 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700241 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800242 aligned_msg->message_type = message_type;
243 aligned_msg->payload_size = payload_size;
244 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700245
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800246 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
247 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700249 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700250 return status;
251}
252
253
Hank Janssen3e189512010-03-04 22:11:00 +0000254/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800255 * hv_signal_event -
256 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700257 *
258 * This involves a hypercall.
259 */
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800260u16 hv_signal_event(void *con_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700261{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700262 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800264 status = (do_hypercall(HVCALL_SIGNAL_EVENT, con_id, NULL) & 0xFFFF);
265
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266 return status;
267}
268
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800269static int hv_ce_set_next_event(unsigned long delta,
270 struct clock_event_device *evt)
271{
272 cycle_t current_tick;
273
274 WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
275
276 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
277 current_tick += delta;
278 wrmsrl(HV_X64_MSR_STIMER0_COUNT, current_tick);
279 return 0;
280}
281
282static void hv_ce_setmode(enum clock_event_mode mode,
283 struct clock_event_device *evt)
284{
285 union hv_timer_config timer_cfg;
286
287 switch (mode) {
288 case CLOCK_EVT_MODE_PERIODIC:
289 /* unsupported */
290 break;
291
292 case CLOCK_EVT_MODE_ONESHOT:
293 timer_cfg.enable = 1;
294 timer_cfg.auto_enable = 1;
295 timer_cfg.sintx = VMBUS_MESSAGE_SINT;
296 wrmsrl(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
297 break;
298
299 case CLOCK_EVT_MODE_UNUSED:
300 case CLOCK_EVT_MODE_SHUTDOWN:
301 wrmsrl(HV_X64_MSR_STIMER0_COUNT, 0);
302 wrmsrl(HV_X64_MSR_STIMER0_CONFIG, 0);
303 break;
304 case CLOCK_EVT_MODE_RESUME:
305 break;
306 }
307}
308
309static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
310{
311 dev->name = "Hyper-V clockevent";
312 dev->features = CLOCK_EVT_FEAT_ONESHOT;
313 dev->cpumask = cpumask_of(cpu);
314 dev->rating = 1000;
315 dev->owner = THIS_MODULE;
316
317 dev->set_mode = hv_ce_setmode;
318 dev->set_next_event = hv_ce_set_next_event;
319}
320
Jason Wang2608fb62013-06-19 11:28:10 +0800321
322int hv_synic_alloc(void)
323{
324 size_t size = sizeof(struct tasklet_struct);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800325 size_t ced_size = sizeof(struct clock_event_device);
Jason Wang2608fb62013-06-19 11:28:10 +0800326 int cpu;
327
328 for_each_online_cpu(cpu) {
329 hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
330 if (hv_context.event_dpc[cpu] == NULL) {
331 pr_err("Unable to allocate event dpc\n");
332 goto err;
333 }
334 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
335
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800336 hv_context.clk_evt[cpu] = kzalloc(ced_size, GFP_ATOMIC);
337 if (hv_context.clk_evt[cpu] == NULL) {
338 pr_err("Unable to allocate clock event device\n");
339 goto err;
340 }
341 hv_init_clockevent_device(hv_context.clk_evt[cpu], cpu);
342
Jason Wang2608fb62013-06-19 11:28:10 +0800343 hv_context.synic_message_page[cpu] =
344 (void *)get_zeroed_page(GFP_ATOMIC);
345
346 if (hv_context.synic_message_page[cpu] == NULL) {
347 pr_err("Unable to allocate SYNIC message page\n");
348 goto err;
349 }
350
351 hv_context.synic_event_page[cpu] =
352 (void *)get_zeroed_page(GFP_ATOMIC);
353
354 if (hv_context.synic_event_page[cpu] == NULL) {
355 pr_err("Unable to allocate SYNIC event page\n");
356 goto err;
357 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700358
359 hv_context.post_msg_page[cpu] =
360 (void *)get_zeroed_page(GFP_ATOMIC);
361
362 if (hv_context.post_msg_page[cpu] == NULL) {
363 pr_err("Unable to allocate post msg page\n");
364 goto err;
365 }
Jason Wang2608fb62013-06-19 11:28:10 +0800366 }
367
368 return 0;
369err:
370 return -ENOMEM;
371}
372
Rashika Kheria87129542013-12-14 19:00:06 +0530373static void hv_synic_free_cpu(int cpu)
Jason Wang2608fb62013-06-19 11:28:10 +0800374{
375 kfree(hv_context.event_dpc[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800376 kfree(hv_context.clk_evt[cpu]);
Felipe Penafdf91da2013-10-15 20:22:32 -0300377 if (hv_context.synic_event_page[cpu])
Jason Wang2608fb62013-06-19 11:28:10 +0800378 free_page((unsigned long)hv_context.synic_event_page[cpu]);
379 if (hv_context.synic_message_page[cpu])
380 free_page((unsigned long)hv_context.synic_message_page[cpu]);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700381 if (hv_context.post_msg_page[cpu])
382 free_page((unsigned long)hv_context.post_msg_page[cpu]);
Jason Wang2608fb62013-06-19 11:28:10 +0800383}
384
385void hv_synic_free(void)
386{
387 int cpu;
388
389 for_each_online_cpu(cpu)
390 hv_synic_free_cpu(cpu);
391}
392
Hank Janssen3e189512010-03-04 22:11:00 +0000393/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800394 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700395 *
396 * If it is already initialized by another entity (ie x2v shim), we need to
397 * retrieve the initialized message and event pages. Otherwise, we create and
398 * initialize the message and event pages.
399 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800400void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700402 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700403 union hv_synic_simp simp;
404 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800405 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700406 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800407 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000408
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800409 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700410
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800411 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800412 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
Bill Pemberton454f18a2009-07-27 16:47:24 -0400414 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700415 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700416
Hank Janssena73e6b72010-01-22 19:17:50 +0000417 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800418 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
419 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800420 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000421 >> PAGE_SHIFT;
422
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800423 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000424
425 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800426 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
427 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800428 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000429 >> PAGE_SHIFT;
430
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800431 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000432
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700433 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800434 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700435
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800436 shared_sint.as_uint64 = 0;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800437 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800438 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800439 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700440
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800441 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700442
Bill Pemberton454f18a2009-07-27 16:47:24 -0400443 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800444 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
445 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800447 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700448
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800449 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800450
451 /*
452 * Setup the mapping between Hyper-V's notion
453 * of cpuid and Linux' notion of cpuid.
454 * This array will be indexed using Linux cpuid.
455 */
456 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
457 hv_context.vp_index[cpu] = (u32)vp_index;
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700458
459 INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800460
461 /*
462 * Register the per-cpu clockevent source.
463 */
464 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
465 clockevents_config_and_register(hv_context.clk_evt[cpu],
466 HV_TIMER_FREQUENCY,
467 HV_MIN_DELTA_TICKS,
468 HV_MAX_MAX_DELTA_TICKS);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800469 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700470}
471
Hank Janssen3e189512010-03-04 22:11:00 +0000472/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800473 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700474 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800475void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700476{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800477 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700478 union hv_synic_simp simp;
479 union hv_synic_siefp siefp;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800480 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700481
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800482 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700483 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700484
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800485 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700486
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800487 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700488
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800489 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400490 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800491 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700492
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800493 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
494 simp.simp_enabled = 0;
495 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700496
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800497 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700498
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800499 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
500 siefp.siefp_enabled = 0;
501 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800503 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700504
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800505 free_page((unsigned long)hv_context.synic_message_page[cpu]);
506 free_page((unsigned long)hv_context.synic_event_page[cpu]);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700507}