blob: 11273cd384d6dd75d135e2faea1b115dfdf19a4d [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{
Vitaly Kuznetsov0aa67252018-11-26 16:47:29 +0100146 union hv_stimer_config timer_cfg;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800147
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;
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000192 struct hv_per_cpu_context *hv_cpu;
193
194 /*
195 * First, zero all per-cpu memory areas so hv_synic_free() can
196 * detect what memory has been allocated and cleanup properly
197 * after any failures.
198 */
199 for_each_present_cpu(cpu) {
200 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
201 memset(hv_cpu, 0, sizeof(*hv_cpu));
202 }
Jason Wang2608fb62013-06-19 11:28:10 +0800203
Kees Cook6396bb22018-06-12 14:03:40 -0700204 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
Jia-Ju Bai597ff722018-03-04 22:17:12 -0700205 GFP_KERNEL);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700206 if (hv_context.hv_numa_map == NULL) {
207 pr_err("Unable to allocate NUMA map\n");
208 goto err;
209 }
210
Vitaly Kuznetsov421b8f22016-12-07 01:16:25 -0800211 for_each_present_cpu(cpu) {
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000212 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
Jason Wang2608fb62013-06-19 11:28:10 +0800213
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700214 tasklet_init(&hv_cpu->msg_dpc,
215 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800216
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700217 hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
218 GFP_KERNEL);
219 if (hv_cpu->clk_evt == NULL) {
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800220 pr_err("Unable to allocate clock event device\n");
221 goto err;
222 }
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700223 hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700224
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700225 hv_cpu->synic_message_page =
Jason Wang2608fb62013-06-19 11:28:10 +0800226 (void *)get_zeroed_page(GFP_ATOMIC);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700227 if (hv_cpu->synic_message_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800228 pr_err("Unable to allocate SYNIC message page\n");
229 goto err;
230 }
231
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700232 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
233 if (hv_cpu->synic_event_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800234 pr_err("Unable to allocate SYNIC event page\n");
235 goto err;
236 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700237
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700238 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
239 if (hv_cpu->post_msg_page == NULL) {
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700240 pr_err("Unable to allocate post msg page\n");
241 goto err;
242 }
Vitaly Kuznetsov3c7630d2016-12-07 01:16:26 -0800243
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700244 INIT_LIST_HEAD(&hv_cpu->chan_list);
Jason Wang2608fb62013-06-19 11:28:10 +0800245 }
246
Michael Kelley248e7422018-03-04 22:17:18 -0700247 if (direct_mode_enabled &&
248 hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
249 hv_stimer0_isr))
250 goto err;
251
Jason Wang2608fb62013-06-19 11:28:10 +0800252 return 0;
253err:
Michael Kelley57208632018-08-02 03:08:25 +0000254 /*
255 * Any memory allocations that succeeded will be freed when
256 * the caller cleans up by calling hv_synic_free()
257 */
Jason Wang2608fb62013-06-19 11:28:10 +0800258 return -ENOMEM;
259}
260
Jason Wang2608fb62013-06-19 11:28:10 +0800261
262void hv_synic_free(void)
263{
264 int cpu;
265
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700266 for_each_present_cpu(cpu) {
267 struct hv_per_cpu_context *hv_cpu
268 = per_cpu_ptr(hv_context.cpu_context, cpu);
269
Michael Kelley57208632018-08-02 03:08:25 +0000270 kfree(hv_cpu->clk_evt);
271 free_page((unsigned long)hv_cpu->synic_event_page);
272 free_page((unsigned long)hv_cpu->synic_message_page);
273 free_page((unsigned long)hv_cpu->post_msg_page);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700274 }
275
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700276 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800277}
278
Hank Janssen3e189512010-03-04 22:11:00 +0000279/*
Joe Perches68cb8112018-03-04 22:17:13 -0700280 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700281 *
282 * If it is already initialized by another entity (ie x2v shim), we need to
283 * retrieve the initialized message and event pages. Otherwise, we create and
284 * initialize the message and event pages.
285 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800286int hv_synic_init(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700287{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700288 struct hv_per_cpu_context *hv_cpu
289 = per_cpu_ptr(hv_context.cpu_context, cpu);
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700290 union hv_synic_simp simp;
291 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800292 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700293 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000294
Hank Janssena73e6b72010-01-22 19:17:50 +0000295 /* Setup the Synic's message page */
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700296 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800297 simp.simp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700298 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000299 >> PAGE_SHIFT;
300
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700301 hv_set_simp(simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000302
303 /* Setup the Synic's event page */
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700304 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800305 siefp.siefp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700306 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000307 >> PAGE_SHIFT;
308
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700309 hv_set_siefp(siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000310
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700311 /* Setup the shared SINT. */
Michael Kelley619a4c82018-06-05 13:37:53 -0700312 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800314 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800315 shared_sint.masked = false;
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700316 if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
K. Y. Srinivasan6c248aa2017-03-14 18:01:39 -0700317 shared_sint.auto_eoi = false;
318 else
319 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700320
Michael Kelley619a4c82018-06-05 13:37:53 -0700321 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700322
Bill Pemberton454f18a2009-07-27 16:47:24 -0400323 /* Enable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700324 hv_get_synic_state(sctrl.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800325 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700326
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700327 hv_set_synic_state(sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700328
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800329 hv_context.synic_initialized = true;
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800330
331 /*
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800332 * Register the per-cpu clockevent source.
333 */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700334 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700335 clockevents_config_and_register(hv_cpu->clk_evt,
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800336 HV_TIMER_FREQUENCY,
337 HV_MIN_DELTA_TICKS,
338 HV_MAX_MAX_DELTA_TICKS);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800339 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700340}
341
Hank Janssen3e189512010-03-04 22:11:00 +0000342/*
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800343 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
344 */
345void hv_synic_clockevents_cleanup(void)
346{
347 int cpu;
348
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700349 if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800350 return;
351
Michael Kelley248e7422018-03-04 22:17:18 -0700352 if (direct_mode_enabled)
353 hv_remove_stimer0_irq(stimer0_irq);
354
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700355 for_each_present_cpu(cpu) {
356 struct hv_per_cpu_context *hv_cpu
357 = per_cpu_ptr(hv_context.cpu_context, cpu);
358
359 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
360 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800361}
362
363/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800364 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700365 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800366int hv_synic_cleanup(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700367{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800368 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700369 union hv_synic_simp simp;
370 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800371 union hv_synic_scontrol sctrl;
Vitaly Kuznetsov523b94082016-12-07 14:53:12 -0800372 struct vmbus_channel *channel, *sc;
373 bool channel_found = false;
374 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700375
Haiyang Zhang6a0aaa12010-11-08 14:04:40 -0800376 if (!hv_context.synic_initialized)
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800377 return -EFAULT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700378
Vitaly Kuznetsov523b94082016-12-07 14:53:12 -0800379 /*
380 * Search for channels which are bound to the CPU we're about to
381 * cleanup. In case we find one and vmbus is still connected we need to
382 * fail, this will effectively prevent CPU offlining. There is no way
383 * we can re-bind channels to different CPUs for now.
384 */
385 mutex_lock(&vmbus_connection.channel_mutex);
386 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
387 if (channel->target_cpu == cpu) {
388 channel_found = true;
389 break;
390 }
391 spin_lock_irqsave(&channel->lock, flags);
392 list_for_each_entry(sc, &channel->sc_list, sc_list) {
393 if (sc->target_cpu == cpu) {
394 channel_found = true;
395 break;
396 }
397 }
398 spin_unlock_irqrestore(&channel->lock, flags);
399 if (channel_found)
400 break;
401 }
402 mutex_unlock(&vmbus_connection.channel_mutex);
403
404 if (channel_found && vmbus_connection.conn_state == CONNECTED)
405 return -EBUSY;
406
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800407 /* Turn off clockevent device */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700408 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700409 struct hv_per_cpu_context *hv_cpu
410 = this_cpu_ptr(hv_context.cpu_context);
411
412 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
413 hv_ce_shutdown(hv_cpu->clk_evt);
414 put_cpu_ptr(hv_cpu);
Vitaly Kuznetsov6ffc4b82016-12-03 12:34:35 -0800415 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800416
Michael Kelley619a4c82018-06-05 13:37:53 -0700417 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700418
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800419 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700420
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800421 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400422 /* Disable the interrupt */
Michael Kelley619a4c82018-06-05 13:37:53 -0700423 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700424
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700425 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800426 simp.simp_enabled = 0;
427 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700428
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700429 hv_set_simp(simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700430
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700431 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800432 siefp.siefp_enabled = 0;
433 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700434
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700435 hv_set_siefp(siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700436
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800437 /* Disable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700438 hv_get_synic_state(sctrl.as_uint64);
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800439 sctrl.enable = 0;
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700440 hv_set_synic_state(sctrl.as_uint64);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800441
442 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700443}