blob: 748a1c4172a6466b1ffd710942a50b378c202c26 [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>
Michael Kelley248e7422018-03-04 22:17:18 -070030#include <linux/random.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080031#include <linux/clockchips.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080032#include <asm/mshyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070033#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070034
Bill Pemberton454f18a2009-07-27 16:47:24 -040035/* The one and only */
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -080036struct hv_context hv_context = {
37 .synic_initialized = false,
Hank Janssen3e7ee492009-07-13 16:02:34 -070038};
39
Michael Kelley248e7422018-03-04 22:17:18 -070040/*
41 * If false, we're using the old mechanism for stimer0 interrupts
42 * where it sends a VMbus message when it expires. The old
43 * mechanism is used when running on older versions of Hyper-V
44 * that don't support Direct Mode. While Hyper-V provides
45 * four stimer's per CPU, Linux uses only stimer0.
46 */
47static bool direct_mode_enabled;
48static int stimer0_irq;
49static int stimer0_vector;
50
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080051#define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
52#define HV_MAX_MAX_DELTA_TICKS 0xffffffff
53#define HV_MIN_DELTA_TICKS 1
54
Hank Janssen3e189512010-03-04 22:11:00 +000055/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080056 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070057 *
58 * This routine must be called before any other routines in here are called
59 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080060int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070061{
Stephen Hemminger37cdd992017-02-11 23:02:19 -070062 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
63 if (!hv_context.cpu_context)
64 return -ENOMEM;
65
Michael Kelley248e7422018-03-04 22:17:18 -070066 direct_mode_enabled = ms_hyperv.misc_features &
Michael Kelley7dc9b6b2018-06-05 13:37:54 -070067 HV_STIMER_DIRECT_MODE_AVAILABLE;
K. Y. Srinivasan5433e002011-08-25 09:48:51 -070068 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070069}
70
Hank Janssen3e189512010-03-04 22:11:00 +000071/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080072 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070073 *
74 * This involves a hypercall.
75 */
Dan Carpenter415f0a02012-03-28 09:58:07 +030076int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080077 enum hv_message_type message_type,
78 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -070079{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080080 struct hv_input_post_message *aligned_msg;
Stephen Hemminger37cdd992017-02-11 23:02:19 -070081 struct hv_per_cpu_context *hv_cpu;
Jake Oshinsa1083932015-12-14 16:01:40 -080082 u64 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -070083
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080084 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -070085 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -070086
Stephen Hemminger37cdd992017-02-11 23:02:19 -070087 hv_cpu = get_cpu_ptr(hv_context.cpu_context);
88 aligned_msg = hv_cpu->post_msg_page;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080089 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -070090 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080091 aligned_msg->message_type = message_type;
92 aligned_msg->payload_size = payload_size;
93 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070094
Jake Oshinsa1083932015-12-14 16:01:40 -080095 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070096
Michael Kelley13b9abf2017-05-18 10:46:07 -070097 /* Preemption must remain disabled until after the hypercall
98 * so some other thread can't get scheduled onto this cpu and
99 * corrupt the per-cpu post_msg_page
100 */
101 put_cpu_ptr(hv_cpu);
102
Jake Oshinsa1083932015-12-14 16:01:40 -0800103 return status & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700104}
105
Michael Kelley248e7422018-03-04 22:17:18 -0700106/*
107 * ISR for when stimer0 is operating in Direct Mode. Direct Mode
108 * does not use VMbus or any VMbus messages, so process here and not
109 * in the VMbus driver code.
110 */
111
112static void hv_stimer0_isr(void)
113{
114 struct hv_per_cpu_context *hv_cpu;
115
116 hv_cpu = this_cpu_ptr(hv_context.cpu_context);
117 hv_cpu->clk_evt->event_handler(hv_cpu->clk_evt);
118 add_interrupt_randomness(stimer0_vector, 0);
119}
120
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800121static int hv_ce_set_next_event(unsigned long delta,
122 struct clock_event_device *evt)
123{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100124 u64 current_tick;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800125
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700126 WARN_ON(!clockevent_state_oneshot(evt));
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800127
K. Y. Srinivasane546d772017-05-18 10:46:02 -0700128 current_tick = hyperv_cs->read(NULL);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800129 current_tick += delta;
Michael Kelley619a4c82018-06-05 13:37:53 -0700130 hv_init_timer(0, current_tick);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800131 return 0;
132}
133
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700134static int hv_ce_shutdown(struct clock_event_device *evt)
135{
Michael Kelley619a4c82018-06-05 13:37:53 -0700136 hv_init_timer(0, 0);
137 hv_init_timer_config(0, 0);
Michael Kelley248e7422018-03-04 22:17:18 -0700138 if (direct_mode_enabled)
139 hv_disable_stimer0_percpu_irq(stimer0_irq);
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700140
141 return 0;
142}
143
144static int hv_ce_set_oneshot(struct clock_event_device *evt)
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800145{
146 union hv_timer_config timer_cfg;
147
Michael Kelley248e7422018-03-04 22:17:18 -0700148 timer_cfg.as_uint64 = 0;
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700149 timer_cfg.enable = 1;
150 timer_cfg.auto_enable = 1;
Michael Kelley248e7422018-03-04 22:17:18 -0700151 if (direct_mode_enabled) {
152 /*
153 * When it expires, the timer will directly interrupt
154 * on the specified hardware vector/IRQ.
155 */
156 timer_cfg.direct_mode = 1;
157 timer_cfg.apic_vector = stimer0_vector;
158 hv_enable_stimer0_percpu_irq(stimer0_irq);
159 } else {
160 /*
161 * When it expires, the timer will generate a VMbus message,
162 * to be handled by the normal VMbus interrupt handler.
163 */
164 timer_cfg.direct_mode = 0;
165 timer_cfg.sintx = VMBUS_MESSAGE_SINT;
166 }
Michael Kelley619a4c82018-06-05 13:37:53 -0700167 hv_init_timer_config(0, timer_cfg.as_uint64);
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700168 return 0;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800169}
170
171static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
172{
173 dev->name = "Hyper-V clockevent";
174 dev->features = CLOCK_EVT_FEAT_ONESHOT;
175 dev->cpumask = cpumask_of(cpu);
176 dev->rating = 1000;
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800177 /*
178 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
179 * result in clockevents_config_and_register() taking additional
180 * references to the hv_vmbus module making it impossible to unload.
181 */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800182
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700183 dev->set_state_shutdown = hv_ce_shutdown;
184 dev->set_state_oneshot = hv_ce_set_oneshot;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800185 dev->set_next_event = hv_ce_set_next_event;
186}
187
Jason Wang2608fb62013-06-19 11:28:10 +0800188
189int hv_synic_alloc(void)
190{
Jason Wang2608fb62013-06-19 11:28:10 +0800191 int cpu;
192
Kees Cook6396bb22018-06-12 14:03:40 -0700193 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
Jia-Ju Bai597ff722018-03-04 22:17:12 -0700194 GFP_KERNEL);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700195 if (hv_context.hv_numa_map == NULL) {
196 pr_err("Unable to allocate NUMA map\n");
197 goto err;
198 }
199
Vitaly Kuznetsov421b8f22016-12-07 01:16:25 -0800200 for_each_present_cpu(cpu) {
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700201 struct hv_per_cpu_context *hv_cpu
202 = per_cpu_ptr(hv_context.cpu_context, cpu);
Jason Wang2608fb62013-06-19 11:28:10 +0800203
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700204 memset(hv_cpu, 0, sizeof(*hv_cpu));
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700205 tasklet_init(&hv_cpu->msg_dpc,
206 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800207
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700208 hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
209 GFP_KERNEL);
210 if (hv_cpu->clk_evt == NULL) {
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800211 pr_err("Unable to allocate clock event device\n");
212 goto err;
213 }
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700214 hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700215
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700216 hv_cpu->synic_message_page =
Jason Wang2608fb62013-06-19 11:28:10 +0800217 (void *)get_zeroed_page(GFP_ATOMIC);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700218 if (hv_cpu->synic_message_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800219 pr_err("Unable to allocate SYNIC message page\n");
220 goto err;
221 }
222
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700223 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
224 if (hv_cpu->synic_event_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800225 pr_err("Unable to allocate SYNIC event page\n");
226 goto err;
227 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700228
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700229 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
230 if (hv_cpu->post_msg_page == NULL) {
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700231 pr_err("Unable to allocate post msg page\n");
232 goto err;
233 }
Vitaly Kuznetsov3c7630d2016-12-07 01:16:26 -0800234
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700235 INIT_LIST_HEAD(&hv_cpu->chan_list);
Jason Wang2608fb62013-06-19 11:28:10 +0800236 }
237
Michael Kelley248e7422018-03-04 22:17:18 -0700238 if (direct_mode_enabled &&
239 hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
240 hv_stimer0_isr))
241 goto err;
242
Jason Wang2608fb62013-06-19 11:28:10 +0800243 return 0;
244err:
Michael Kelley57208632018-08-02 03:08:25 +0000245 /*
246 * Any memory allocations that succeeded will be freed when
247 * the caller cleans up by calling hv_synic_free()
248 */
Jason Wang2608fb62013-06-19 11:28:10 +0800249 return -ENOMEM;
250}
251
Jason Wang2608fb62013-06-19 11:28:10 +0800252
253void hv_synic_free(void)
254{
255 int cpu;
256
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700257 for_each_present_cpu(cpu) {
258 struct hv_per_cpu_context *hv_cpu
259 = per_cpu_ptr(hv_context.cpu_context, cpu);
260
Michael Kelley57208632018-08-02 03:08:25 +0000261 kfree(hv_cpu->clk_evt);
262 free_page((unsigned long)hv_cpu->synic_event_page);
263 free_page((unsigned long)hv_cpu->synic_message_page);
264 free_page((unsigned long)hv_cpu->post_msg_page);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700265 }
266
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700267 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800268}
269
Hank Janssen3e189512010-03-04 22:11:00 +0000270/*
Joe Perches68cb8112018-03-04 22:17:13 -0700271 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700272 *
273 * If it is already initialized by another entity (ie x2v shim), we need to
274 * retrieve the initialized message and event pages. Otherwise, we create and
275 * initialize the message and event pages.
276 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800277int hv_synic_init(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700278{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700279 struct hv_per_cpu_context *hv_cpu
280 = per_cpu_ptr(hv_context.cpu_context, cpu);
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700281 union hv_synic_simp simp;
282 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800283 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700284 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000285
Hank Janssena73e6b72010-01-22 19:17:50 +0000286 /* Setup the Synic's message page */
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700287 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800288 simp.simp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700289 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000290 >> PAGE_SHIFT;
291
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700292 hv_set_simp(simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000293
294 /* Setup the Synic's event page */
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700295 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800296 siefp.siefp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700297 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000298 >> PAGE_SHIFT;
299
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700300 hv_set_siefp(siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000301
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700302 /* Setup the shared SINT. */
Michael Kelley619a4c82018-06-05 13:37:53 -0700303 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700304
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800305 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800306 shared_sint.masked = false;
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700307 if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
K. Y. Srinivasan6c248aa2017-03-14 18:01:39 -0700308 shared_sint.auto_eoi = false;
309 else
310 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700311
Michael Kelley619a4c82018-06-05 13:37:53 -0700312 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
Bill Pemberton454f18a2009-07-27 16:47:24 -0400314 /* Enable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700315 hv_get_synic_state(sctrl.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800316 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700317
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700318 hv_set_synic_state(sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700319
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800320 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800321
322 /*
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800323 * Register the per-cpu clockevent source.
324 */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700325 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700326 clockevents_config_and_register(hv_cpu->clk_evt,
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800327 HV_TIMER_FREQUENCY,
328 HV_MIN_DELTA_TICKS,
329 HV_MAX_MAX_DELTA_TICKS);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800330 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700331}
332
Hank Janssen3e189512010-03-04 22:11:00 +0000333/*
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800334 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
335 */
336void hv_synic_clockevents_cleanup(void)
337{
338 int cpu;
339
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700340 if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800341 return;
342
Michael Kelley248e7422018-03-04 22:17:18 -0700343 if (direct_mode_enabled)
344 hv_remove_stimer0_irq(stimer0_irq);
345
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700346 for_each_present_cpu(cpu) {
347 struct hv_per_cpu_context *hv_cpu
348 = per_cpu_ptr(hv_context.cpu_context, cpu);
349
350 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
351 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800352}
353
354/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800355 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700356 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800357int hv_synic_cleanup(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800359 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700360 union hv_synic_simp simp;
361 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800362 union hv_synic_scontrol sctrl;
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800363 struct vmbus_channel *channel, *sc;
364 bool channel_found = false;
365 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700366
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800367 if (!hv_context.synic_initialized)
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800368 return -EFAULT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700369
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800370 /*
371 * Search for channels which are bound to the CPU we're about to
372 * cleanup. In case we find one and vmbus is still connected we need to
373 * fail, this will effectively prevent CPU offlining. There is no way
374 * we can re-bind channels to different CPUs for now.
375 */
376 mutex_lock(&vmbus_connection.channel_mutex);
377 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
378 if (channel->target_cpu == cpu) {
379 channel_found = true;
380 break;
381 }
382 spin_lock_irqsave(&channel->lock, flags);
383 list_for_each_entry(sc, &channel->sc_list, sc_list) {
384 if (sc->target_cpu == cpu) {
385 channel_found = true;
386 break;
387 }
388 }
389 spin_unlock_irqrestore(&channel->lock, flags);
390 if (channel_found)
391 break;
392 }
393 mutex_unlock(&vmbus_connection.channel_mutex);
394
395 if (channel_found && vmbus_connection.conn_state == CONNECTED)
396 return -EBUSY;
397
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800398 /* Turn off clockevent device */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700399 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700400 struct hv_per_cpu_context *hv_cpu
401 = this_cpu_ptr(hv_context.cpu_context);
402
403 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
404 hv_ce_shutdown(hv_cpu->clk_evt);
405 put_cpu_ptr(hv_cpu);
Vitaly Kuznetsov6ffc4b82016-12-03 12:34:35 -0800406 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800407
Michael Kelley619a4c82018-06-05 13:37:53 -0700408 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700409
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800410 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800412 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400413 /* Disable the interrupt */
Michael Kelley619a4c82018-06-05 13:37:53 -0700414 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700415
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700416 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800417 simp.simp_enabled = 0;
418 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700419
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700420 hv_set_simp(simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700421
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700422 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800423 siefp.siefp_enabled = 0;
424 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700426 hv_set_siefp(siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700427
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800428 /* Disable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700429 hv_get_synic_state(sctrl.as_uint64);
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800430 sctrl.enable = 0;
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700431 hv_set_synic_state(sctrl.as_uint64);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800432
433 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434}