blob: d3943bceecc367c7b5241de3ba7933aae45f41c4 [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-Hartman407dd162011-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;
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800315 /*
316 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
317 * result in clockevents_config_and_register() taking additional
318 * references to the hv_vmbus module making it impossible to unload.
319 */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800320
321 dev->set_mode = hv_ce_setmode;
322 dev->set_next_event = hv_ce_set_next_event;
323}
324
Jason Wang2608fb62013-06-19 11:28:10 +0800325
326int hv_synic_alloc(void)
327{
328 size_t size = sizeof(struct tasklet_struct);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800329 size_t ced_size = sizeof(struct clock_event_device);
Jason Wang2608fb62013-06-19 11:28:10 +0800330 int cpu;
331
332 for_each_online_cpu(cpu) {
333 hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
334 if (hv_context.event_dpc[cpu] == NULL) {
335 pr_err("Unable to allocate event dpc\n");
336 goto err;
337 }
338 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
339
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800340 hv_context.clk_evt[cpu] = kzalloc(ced_size, GFP_ATOMIC);
341 if (hv_context.clk_evt[cpu] == NULL) {
342 pr_err("Unable to allocate clock event device\n");
343 goto err;
344 }
345 hv_init_clockevent_device(hv_context.clk_evt[cpu], cpu);
346
Jason Wang2608fb62013-06-19 11:28:10 +0800347 hv_context.synic_message_page[cpu] =
348 (void *)get_zeroed_page(GFP_ATOMIC);
349
350 if (hv_context.synic_message_page[cpu] == NULL) {
351 pr_err("Unable to allocate SYNIC message page\n");
352 goto err;
353 }
354
355 hv_context.synic_event_page[cpu] =
356 (void *)get_zeroed_page(GFP_ATOMIC);
357
358 if (hv_context.synic_event_page[cpu] == NULL) {
359 pr_err("Unable to allocate SYNIC event page\n");
360 goto err;
361 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700362
363 hv_context.post_msg_page[cpu] =
364 (void *)get_zeroed_page(GFP_ATOMIC);
365
366 if (hv_context.post_msg_page[cpu] == NULL) {
367 pr_err("Unable to allocate post msg page\n");
368 goto err;
369 }
Jason Wang2608fb62013-06-19 11:28:10 +0800370 }
371
372 return 0;
373err:
374 return -ENOMEM;
375}
376
Rashika Kheria87129542013-12-14 19:00:06 +0530377static void hv_synic_free_cpu(int cpu)
Jason Wang2608fb62013-06-19 11:28:10 +0800378{
379 kfree(hv_context.event_dpc[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800380 kfree(hv_context.clk_evt[cpu]);
Felipe Penafdf91da2013-10-15 20:22:32 -0300381 if (hv_context.synic_event_page[cpu])
Jason Wang2608fb62013-06-19 11:28:10 +0800382 free_page((unsigned long)hv_context.synic_event_page[cpu]);
383 if (hv_context.synic_message_page[cpu])
384 free_page((unsigned long)hv_context.synic_message_page[cpu]);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700385 if (hv_context.post_msg_page[cpu])
386 free_page((unsigned long)hv_context.post_msg_page[cpu]);
Jason Wang2608fb62013-06-19 11:28:10 +0800387}
388
389void hv_synic_free(void)
390{
391 int cpu;
392
393 for_each_online_cpu(cpu)
394 hv_synic_free_cpu(cpu);
395}
396
Hank Janssen3e189512010-03-04 22:11:00 +0000397/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800398 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700399 *
400 * If it is already initialized by another entity (ie x2v shim), we need to
401 * retrieve the initialized message and event pages. Otherwise, we create and
402 * initialize the message and event pages.
403 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800404void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700405{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700406 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700407 union hv_synic_simp simp;
408 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800409 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700410 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800411 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000412
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800413 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700414
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800415 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800416 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
Bill Pemberton454f18a2009-07-27 16:47:24 -0400418 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700419 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Hank Janssena73e6b72010-01-22 19:17:50 +0000421 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800422 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
423 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800424 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000425 >> PAGE_SHIFT;
426
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800427 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000428
429 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800430 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
431 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800432 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000433 >> PAGE_SHIFT;
434
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800435 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000436
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700437 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800438 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700439
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800440 shared_sint.as_uint64 = 0;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800441 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800442 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800443 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700444
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800445 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700446
Bill Pemberton454f18a2009-07-27 16:47:24 -0400447 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800448 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
449 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800451 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800453 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800454
455 /*
456 * Setup the mapping between Hyper-V's notion
457 * of cpuid and Linux' notion of cpuid.
458 * This array will be indexed using Linux cpuid.
459 */
460 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
461 hv_context.vp_index[cpu] = (u32)vp_index;
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700462
463 INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800464
465 /*
466 * Register the per-cpu clockevent source.
467 */
468 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
469 clockevents_config_and_register(hv_context.clk_evt[cpu],
470 HV_TIMER_FREQUENCY,
471 HV_MIN_DELTA_TICKS,
472 HV_MAX_MAX_DELTA_TICKS);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800473 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700474}
475
Hank Janssen3e189512010-03-04 22:11:00 +0000476/*
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800477 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
478 */
479void hv_synic_clockevents_cleanup(void)
480{
481 int cpu;
482
483 if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
484 return;
485
486 for_each_online_cpu(cpu)
487 clockevents_unbind_device(hv_context.clk_evt[cpu], cpu);
488}
489
490/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800491 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700492 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800493void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700494{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800495 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700496 union hv_synic_simp simp;
497 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800498 union hv_synic_scontrol sctrl;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800499 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800501 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700502 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700503
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800504 /* Turn off clockevent device */
505 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
506 hv_ce_setmode(CLOCK_EVT_MODE_SHUTDOWN,
507 hv_context.clk_evt[cpu]);
508
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800509 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700510
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800511 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700512
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800513 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400514 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800515 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700516
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800517 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
518 simp.simp_enabled = 0;
519 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700520
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800521 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700522
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800523 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
524 siefp.siefp_enabled = 0;
525 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700526
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800527 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700528
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800529 /* Disable the global synic bit */
530 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
531 sctrl.enable = 0;
532 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
533
534 hv_synic_free_cpu(cpu);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700535}