blob: c641faf92e607e54256282db2046a531507e6dc6 [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{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080096 u64 input_address = (input) ? virt_to_phys(input) : 0;
97 u64 output_address = (output) ? virt_to_phys(output) : 0;
K. Y. Srinivasandec317f2011-07-19 11:44:21 -070098 void *hypercall_page = hv_context.hypercall_page;
Vitaly Kuznetsovd7646ea2015-08-01 16:08:08 -070099#ifdef CONFIG_X86_64
100 u64 hv_status = 0;
101
102 if (!hypercall_page)
103 return (u64)ULLONG_MAX;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700104
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800105 __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
106 __asm__ __volatile__("call *%3" : "=a" (hv_status) :
107 "c" (control), "d" (input_address),
108 "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800110 return hv_status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700111
112#else
113
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800114 u32 control_hi = control >> 32;
115 u32 control_lo = control & 0xFFFFFFFF;
116 u32 hv_status_hi = 1;
117 u32 hv_status_lo = 1;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800118 u32 input_address_hi = input_address >> 32;
119 u32 input_address_lo = input_address & 0xFFFFFFFF;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800120 u32 output_address_hi = output_address >> 32;
121 u32 output_address_lo = output_address & 0xFFFFFFFF;
Vitaly Kuznetsovd7646ea2015-08-01 16:08:08 -0700122
123 if (!hypercall_page)
124 return (u64)ULLONG_MAX;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700125
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800126 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
127 "=a"(hv_status_lo) : "d" (control_hi),
128 "a" (control_lo), "b" (input_address_hi),
129 "c" (input_address_lo), "D"(output_address_hi),
130 "S"(output_address_lo), "m" (hypercall_page));
Hank Janssen3e7ee492009-07-13 16:02:34 -0700131
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800132 return hv_status_lo | ((u64)hv_status_hi << 32);
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700133#endif /* !x86_64 */
Hank Janssen3e7ee492009-07-13 16:02:34 -0700134}
135
Hank Janssen3e189512010-03-04 22:11:00 +0000136/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800137 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700138 *
139 * This routine must be called before any other routines in here are called
140 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800141int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700142{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800143 int max_leaf;
144 union hv_x64_msr_hypercall_contents hypercall_msr;
145 void *virtaddr = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700146
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800147 memset(hv_context.synic_event_page, 0, sizeof(void *) * NR_CPUS);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800148 memset(hv_context.synic_message_page, 0,
K. Y. Srinivasan14c1bf82012-02-02 16:56:51 -0800149 sizeof(void *) * NR_CPUS);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700150 memset(hv_context.post_msg_page, 0,
151 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800152 memset(hv_context.vp_index, 0,
153 sizeof(int) * NR_CPUS);
K. Y. Srinivasandb11f122012-12-01 06:46:53 -0800154 memset(hv_context.event_dpc, 0,
155 sizeof(void *) * NR_CPUS);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800156 memset(hv_context.clk_evt, 0,
157 sizeof(void *) * NR_CPUS);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700158
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800159 max_leaf = query_hypervisor_info();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700160
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -0700161 /*
162 * Write our OS ID.
163 */
164 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
165 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
Hank Janssena73e6b72010-01-22 19:17:50 +0000166
167 /* See if the hypercall page is already set */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800168 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000169
K. Y. Srinivasandf3493e2011-02-11 09:59:00 -0800170 virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
Hank Janssena73e6b72010-01-22 19:17:50 +0000171
Hank Janssen98e08702011-03-29 13:58:44 -0700172 if (!virtaddr)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700173 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000174
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800175 hypercall_msr.enable = 1;
Hank Janssena73e6b72010-01-22 19:17:50 +0000176
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800177 hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr);
178 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000179
180 /* Confirm that hypercall page did get setup. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800181 hypercall_msr.as_uint64 = 0;
182 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000183
Hank Janssen98e08702011-03-29 13:58:44 -0700184 if (!hypercall_msr.enable)
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700185 goto cleanup;
Hank Janssena73e6b72010-01-22 19:17:50 +0000186
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800187 hv_context.hypercall_page = virtaddr;
Hank Janssena73e6b72010-01-22 19:17:50 +0000188
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700189 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700190
K. Y. Srinivasan44939d32011-05-10 07:55:47 -0700191cleanup:
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800192 if (virtaddr) {
193 if (hypercall_msr.enable) {
194 hypercall_msr.as_uint64 = 0;
195 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700196 }
197
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800198 vfree(virtaddr);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199 }
K. Y. Srinivasan5433e002011-08-25 09:48:51 -0700200
201 return -ENOTSUPP;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700202}
203
Hank Janssen3e189512010-03-04 22:11:00 +0000204/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800205 * hv_cleanup - Cleanup routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700206 *
207 * This routine is called normally during driver unloading or exiting.
208 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800209void hv_cleanup(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700210{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800211 union hv_x64_msr_hypercall_contents hypercall_msr;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700212
K. Y. Srinivasan93e5bd02011-12-12 09:29:17 -0800213 /* Reset our OS id */
214 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
215
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800216 if (hv_context.hypercall_page) {
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800217 hypercall_msr.as_uint64 = 0;
218 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800219 vfree(hv_context.hypercall_page);
220 hv_context.hypercall_page = NULL;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221 }
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222}
223
Hank Janssen3e189512010-03-04 22:11:00 +0000224/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800225 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700226 *
227 * This involves a hypercall.
228 */
Dan Carpenter415f0a02012-03-28 09:58:07 +0300229int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800230 enum hv_message_type message_type,
231 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700232{
Hank Janssen3e7ee492009-07-13 16:02:34 -0700233
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800234 struct hv_input_post_message *aligned_msg;
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700235 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700236
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800237 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -0700238 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700239
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800240 aligned_msg = (struct hv_input_post_message *)
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700241 hv_context.post_msg_page[get_cpu()];
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800243 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700244 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800245 aligned_msg->message_type = message_type;
246 aligned_msg->payload_size = payload_size;
247 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700248
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800249 status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL)
250 & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700251
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700252 put_cpu();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253 return status;
254}
255
256
Hank Janssen3e189512010-03-04 22:11:00 +0000257/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800258 * hv_signal_event -
259 * Signal an event on the specified connection using the hypervisor event IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700260 *
261 * This involves a hypercall.
262 */
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800263u16 hv_signal_event(void *con_id)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700264{
Greg Kroah-Hartman034469e2009-08-20 12:14:11 -0700265 u16 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700266
K. Y. Srinivasan1f422482012-12-01 06:46:42 -0800267 status = (do_hypercall(HVCALL_SIGNAL_EVENT, con_id, NULL) & 0xFFFF);
268
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269 return status;
270}
271
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800272static int hv_ce_set_next_event(unsigned long delta,
273 struct clock_event_device *evt)
274{
275 cycle_t current_tick;
276
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700277 WARN_ON(!clockevent_state_oneshot(evt));
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800278
279 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
280 current_tick += delta;
281 wrmsrl(HV_X64_MSR_STIMER0_COUNT, current_tick);
282 return 0;
283}
284
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700285static int hv_ce_shutdown(struct clock_event_device *evt)
286{
287 wrmsrl(HV_X64_MSR_STIMER0_COUNT, 0);
288 wrmsrl(HV_X64_MSR_STIMER0_CONFIG, 0);
289
290 return 0;
291}
292
293static int hv_ce_set_oneshot(struct clock_event_device *evt)
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800294{
295 union hv_timer_config timer_cfg;
296
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700297 timer_cfg.enable = 1;
298 timer_cfg.auto_enable = 1;
299 timer_cfg.sintx = VMBUS_MESSAGE_SINT;
300 wrmsrl(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800301
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700302 return 0;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800303}
304
305static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
306{
307 dev->name = "Hyper-V clockevent";
308 dev->features = CLOCK_EVT_FEAT_ONESHOT;
309 dev->cpumask = cpumask_of(cpu);
310 dev->rating = 1000;
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800311 /*
312 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
313 * result in clockevents_config_and_register() taking additional
314 * references to the hv_vmbus module making it impossible to unload.
315 */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800316
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700317 dev->set_state_shutdown = hv_ce_shutdown;
318 dev->set_state_oneshot = hv_ce_set_oneshot;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800319 dev->set_next_event = hv_ce_set_next_event;
320}
321
Jason Wang2608fb62013-06-19 11:28:10 +0800322
323int hv_synic_alloc(void)
324{
325 size_t size = sizeof(struct tasklet_struct);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800326 size_t ced_size = sizeof(struct clock_event_device);
Jason Wang2608fb62013-06-19 11:28:10 +0800327 int cpu;
328
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700329 hv_context.hv_numa_map = kzalloc(sizeof(struct cpumask) * nr_node_ids,
330 GFP_ATOMIC);
331 if (hv_context.hv_numa_map == NULL) {
332 pr_err("Unable to allocate NUMA map\n");
333 goto err;
334 }
335
Jason Wang2608fb62013-06-19 11:28:10 +0800336 for_each_online_cpu(cpu) {
337 hv_context.event_dpc[cpu] = kmalloc(size, GFP_ATOMIC);
338 if (hv_context.event_dpc[cpu] == NULL) {
339 pr_err("Unable to allocate event dpc\n");
340 goto err;
341 }
342 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
343
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800344 hv_context.clk_evt[cpu] = kzalloc(ced_size, GFP_ATOMIC);
345 if (hv_context.clk_evt[cpu] == NULL) {
346 pr_err("Unable to allocate clock event device\n");
347 goto err;
348 }
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700349
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800350 hv_init_clockevent_device(hv_context.clk_evt[cpu], cpu);
351
Jason Wang2608fb62013-06-19 11:28:10 +0800352 hv_context.synic_message_page[cpu] =
353 (void *)get_zeroed_page(GFP_ATOMIC);
354
355 if (hv_context.synic_message_page[cpu] == NULL) {
356 pr_err("Unable to allocate SYNIC message page\n");
357 goto err;
358 }
359
360 hv_context.synic_event_page[cpu] =
361 (void *)get_zeroed_page(GFP_ATOMIC);
362
363 if (hv_context.synic_event_page[cpu] == NULL) {
364 pr_err("Unable to allocate SYNIC event page\n");
365 goto err;
366 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700367
368 hv_context.post_msg_page[cpu] =
369 (void *)get_zeroed_page(GFP_ATOMIC);
370
371 if (hv_context.post_msg_page[cpu] == NULL) {
372 pr_err("Unable to allocate post msg page\n");
373 goto err;
374 }
Jason Wang2608fb62013-06-19 11:28:10 +0800375 }
376
377 return 0;
378err:
379 return -ENOMEM;
380}
381
Rashika Kheria87129542013-12-14 19:00:06 +0530382static void hv_synic_free_cpu(int cpu)
Jason Wang2608fb62013-06-19 11:28:10 +0800383{
384 kfree(hv_context.event_dpc[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800385 kfree(hv_context.clk_evt[cpu]);
Felipe Penafdf91da2013-10-15 20:22:32 -0300386 if (hv_context.synic_event_page[cpu])
Jason Wang2608fb62013-06-19 11:28:10 +0800387 free_page((unsigned long)hv_context.synic_event_page[cpu]);
388 if (hv_context.synic_message_page[cpu])
389 free_page((unsigned long)hv_context.synic_message_page[cpu]);
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700390 if (hv_context.post_msg_page[cpu])
391 free_page((unsigned long)hv_context.post_msg_page[cpu]);
Jason Wang2608fb62013-06-19 11:28:10 +0800392}
393
394void hv_synic_free(void)
395{
396 int cpu;
397
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700398 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800399 for_each_online_cpu(cpu)
400 hv_synic_free_cpu(cpu);
401}
402
Hank Janssen3e189512010-03-04 22:11:00 +0000403/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800404 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700405 *
406 * If it is already initialized by another entity (ie x2v shim), we need to
407 * retrieve the initialized message and event pages. Otherwise, we create and
408 * initialize the message and event pages.
409 */
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800410void hv_synic_init(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411{
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700412 u64 version;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700413 union hv_synic_simp simp;
414 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800415 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700416 union hv_synic_scontrol sctrl;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800417 u64 vp_index;
Hank Janssena73e6b72010-01-22 19:17:50 +0000418
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800419 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800421 if (!hv_context.hypercall_page)
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800422 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700423
Bill Pemberton454f18a2009-07-27 16:47:24 -0400424 /* Check the version */
Greg Kroah-Hartmana51ed7d2009-08-17 17:20:02 -0700425 rdmsrl(HV_X64_MSR_SVERSION, version);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700426
Hank Janssena73e6b72010-01-22 19:17:50 +0000427 /* Setup the Synic's message page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800428 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
429 simp.simp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800430 simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000431 >> PAGE_SHIFT;
432
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800433 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000434
435 /* Setup the Synic's event page */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800436 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
437 siefp.siefp_enabled = 1;
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800438 siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu])
Hank Janssena73e6b72010-01-22 19:17:50 +0000439 >> PAGE_SHIFT;
440
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800441 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000442
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700443 /* Setup the shared SINT. */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800444 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700445
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800446 shared_sint.as_uint64 = 0;
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800447 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800448 shared_sint.masked = false;
K. Y. Srinivasanb0209502012-12-01 06:46:54 -0800449 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700450
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800451 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700452
Bill Pemberton454f18a2009-07-27 16:47:24 -0400453 /* Enable the global synic bit */
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800454 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
455 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700456
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800457 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700458
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800459 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800460
461 /*
462 * Setup the mapping between Hyper-V's notion
463 * of cpuid and Linux' notion of cpuid.
464 * This array will be indexed using Linux cpuid.
465 */
466 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
467 hv_context.vp_index[cpu] = (u32)vp_index;
K. Y. Srinivasan3a28fa32014-04-08 18:45:54 -0700468
469 INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800470
471 /*
472 * Register the per-cpu clockevent source.
473 */
474 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
475 clockevents_config_and_register(hv_context.clk_evt[cpu],
476 HV_TIMER_FREQUENCY,
477 HV_MIN_DELTA_TICKS,
478 HV_MAX_MAX_DELTA_TICKS);
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800479 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700480}
481
Hank Janssen3e189512010-03-04 22:11:00 +0000482/*
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800483 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
484 */
485void hv_synic_clockevents_cleanup(void)
486{
487 int cpu;
488
489 if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
490 return;
491
492 for_each_online_cpu(cpu)
493 clockevents_unbind_device(hv_context.clk_evt[cpu], cpu);
494}
495
496/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800497 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700498 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800499void hv_synic_cleanup(void *arg)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700500{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800501 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700502 union hv_synic_simp simp;
503 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800504 union hv_synic_scontrol sctrl;
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800505 int cpu = smp_processor_id();
Hank Janssen3e7ee492009-07-13 16:02:34 -0700506
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800507 if (!hv_context.synic_initialized)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700508 return;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700509
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800510 /* Turn off clockevent device */
511 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700512 hv_ce_shutdown(hv_context.clk_evt[cpu]);
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800513
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800514 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700515
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800516 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700517
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800518 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400519 /* Disable the interrupt */
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800520 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700521
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800522 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
523 simp.simp_enabled = 0;
524 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700525
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800526 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700527
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800528 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
529 siefp.siefp_enabled = 0;
530 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700531
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800532 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700533
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800534 /* Disable the global synic bit */
535 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
536 sctrl.enable = 0;
537 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700538}