blob: a1ea482183e88c4924b62879a516fe9ce63fa6d2 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Hank Janssen3e7ee492009-07-13 16:02:34 -07002/*
Hank Janssen3e7ee492009-07-13 16:02:34 -07003 * Copyright (c) 2009, Microsoft Corporation.
4 *
Hank Janssen3e7ee492009-07-13 16:02:34 -07005 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssen3e7ee492009-07-13 16:02:34 -07008 */
Hank Janssen0a466182011-03-29 13:58:47 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Greg Kroah-Hartmana0086dc2009-08-17 17:22:08 -070011#include <linux/kernel.h>
12#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Bill Pembertonb7c947f2009-07-29 17:00:13 -040014#include <linux/vmalloc.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070015#include <linux/hyperv.h>
K. Y. Srinivasan83ba0c42012-07-24 16:11:58 -070016#include <linux/version.h>
Michael Kelley248e7422018-03-04 22:17:18 -070017#include <linux/random.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080018#include <linux/clockchips.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080019#include <asm/mshyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070020#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070021
Bill Pemberton454f18a2009-07-27 16:47:24 -040022/* The one and only */
K. Y. Srinivasana3cadf32018-10-18 05:09:28 +000023struct hv_context hv_context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070024
Michael Kelley248e7422018-03-04 22:17:18 -070025/*
26 * If false, we're using the old mechanism for stimer0 interrupts
27 * where it sends a VMbus message when it expires. The old
28 * mechanism is used when running on older versions of Hyper-V
29 * that don't support Direct Mode. While Hyper-V provides
30 * four stimer's per CPU, Linux uses only stimer0.
31 */
32static bool direct_mode_enabled;
33static int stimer0_irq;
34static int stimer0_vector;
35
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080036#define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
37#define HV_MAX_MAX_DELTA_TICKS 0xffffffff
38#define HV_MIN_DELTA_TICKS 1
39
Hank Janssen3e189512010-03-04 22:11:00 +000040/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080041 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070042 *
43 * This routine must be called before any other routines in here are called
44 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080045int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070046{
Stephen Hemminger37cdd992017-02-11 23:02:19 -070047 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
48 if (!hv_context.cpu_context)
49 return -ENOMEM;
50
Michael Kelley248e7422018-03-04 22:17:18 -070051 direct_mode_enabled = ms_hyperv.misc_features &
Michael Kelley7dc9b6b2018-06-05 13:37:54 -070052 HV_STIMER_DIRECT_MODE_AVAILABLE;
K. Y. Srinivasan5433e002011-08-25 09:48:51 -070053 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070054}
55
Hank Janssen3e189512010-03-04 22:11:00 +000056/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080057 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070058 *
59 * This involves a hypercall.
60 */
Dan Carpenter415f0a02012-03-28 09:58:07 +030061int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080062 enum hv_message_type message_type,
63 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -070064{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080065 struct hv_input_post_message *aligned_msg;
Stephen Hemminger37cdd992017-02-11 23:02:19 -070066 struct hv_per_cpu_context *hv_cpu;
Jake Oshinsa1083932015-12-14 16:01:40 -080067 u64 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -070068
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080069 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -070070 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -070071
Stephen Hemminger37cdd992017-02-11 23:02:19 -070072 hv_cpu = get_cpu_ptr(hv_context.cpu_context);
73 aligned_msg = hv_cpu->post_msg_page;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080074 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -070075 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080076 aligned_msg->message_type = message_type;
77 aligned_msg->payload_size = payload_size;
78 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070079
Jake Oshinsa1083932015-12-14 16:01:40 -080080 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070081
Michael Kelley13b9abf2017-05-18 10:46:07 -070082 /* Preemption must remain disabled until after the hypercall
83 * so some other thread can't get scheduled onto this cpu and
84 * corrupt the per-cpu post_msg_page
85 */
86 put_cpu_ptr(hv_cpu);
87
Jake Oshinsa1083932015-12-14 16:01:40 -080088 return status & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -070089}
90
Michael Kelley248e7422018-03-04 22:17:18 -070091/*
92 * ISR for when stimer0 is operating in Direct Mode. Direct Mode
93 * does not use VMbus or any VMbus messages, so process here and not
94 * in the VMbus driver code.
95 */
96
97static void hv_stimer0_isr(void)
98{
99 struct hv_per_cpu_context *hv_cpu;
100
101 hv_cpu = this_cpu_ptr(hv_context.cpu_context);
102 hv_cpu->clk_evt->event_handler(hv_cpu->clk_evt);
103 add_interrupt_randomness(stimer0_vector, 0);
104}
105
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800106static int hv_ce_set_next_event(unsigned long delta,
107 struct clock_event_device *evt)
108{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100109 u64 current_tick;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800110
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700111 WARN_ON(!clockevent_state_oneshot(evt));
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800112
K. Y. Srinivasane546d772017-05-18 10:46:02 -0700113 current_tick = hyperv_cs->read(NULL);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800114 current_tick += delta;
Michael Kelley619a4c82018-06-05 13:37:53 -0700115 hv_init_timer(0, current_tick);
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800116 return 0;
117}
118
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700119static int hv_ce_shutdown(struct clock_event_device *evt)
120{
Michael Kelley619a4c82018-06-05 13:37:53 -0700121 hv_init_timer(0, 0);
122 hv_init_timer_config(0, 0);
Michael Kelley248e7422018-03-04 22:17:18 -0700123 if (direct_mode_enabled)
124 hv_disable_stimer0_percpu_irq(stimer0_irq);
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700125
126 return 0;
127}
128
129static int hv_ce_set_oneshot(struct clock_event_device *evt)
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800130{
Vitaly Kuznetsov0aa67252018-11-26 16:47:29 +0100131 union hv_stimer_config timer_cfg;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800132
Michael Kelley248e7422018-03-04 22:17:18 -0700133 timer_cfg.as_uint64 = 0;
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700134 timer_cfg.enable = 1;
135 timer_cfg.auto_enable = 1;
Michael Kelley248e7422018-03-04 22:17:18 -0700136 if (direct_mode_enabled) {
137 /*
138 * When it expires, the timer will directly interrupt
139 * on the specified hardware vector/IRQ.
140 */
141 timer_cfg.direct_mode = 1;
142 timer_cfg.apic_vector = stimer0_vector;
143 hv_enable_stimer0_percpu_irq(stimer0_irq);
144 } else {
145 /*
146 * When it expires, the timer will generate a VMbus message,
147 * to be handled by the normal VMbus interrupt handler.
148 */
149 timer_cfg.direct_mode = 0;
150 timer_cfg.sintx = VMBUS_MESSAGE_SINT;
151 }
Michael Kelley619a4c82018-06-05 13:37:53 -0700152 hv_init_timer_config(0, timer_cfg.as_uint64);
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700153 return 0;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800154}
155
156static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
157{
158 dev->name = "Hyper-V clockevent";
159 dev->features = CLOCK_EVT_FEAT_ONESHOT;
160 dev->cpumask = cpumask_of(cpu);
161 dev->rating = 1000;
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800162 /*
163 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
164 * result in clockevents_config_and_register() taking additional
165 * references to the hv_vmbus module making it impossible to unload.
166 */
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800167
Viresh Kumarbc609cb2015-08-05 00:52:41 -0700168 dev->set_state_shutdown = hv_ce_shutdown;
169 dev->set_state_oneshot = hv_ce_set_oneshot;
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800170 dev->set_next_event = hv_ce_set_next_event;
171}
172
Jason Wang2608fb62013-06-19 11:28:10 +0800173
174int hv_synic_alloc(void)
175{
Jason Wang2608fb62013-06-19 11:28:10 +0800176 int cpu;
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000177 struct hv_per_cpu_context *hv_cpu;
178
179 /*
180 * First, zero all per-cpu memory areas so hv_synic_free() can
181 * detect what memory has been allocated and cleanup properly
182 * after any failures.
183 */
184 for_each_present_cpu(cpu) {
185 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
186 memset(hv_cpu, 0, sizeof(*hv_cpu));
187 }
Jason Wang2608fb62013-06-19 11:28:10 +0800188
Kees Cook6396bb22018-06-12 14:03:40 -0700189 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
Jia-Ju Bai597ff722018-03-04 22:17:12 -0700190 GFP_KERNEL);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700191 if (hv_context.hv_numa_map == NULL) {
192 pr_err("Unable to allocate NUMA map\n");
193 goto err;
194 }
195
Vitaly Kuznetsov421b8f22016-12-07 01:16:25 -0800196 for_each_present_cpu(cpu) {
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000197 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
Jason Wang2608fb62013-06-19 11:28:10 +0800198
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700199 tasklet_init(&hv_cpu->msg_dpc,
200 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800201
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700202 hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device),
203 GFP_KERNEL);
204 if (hv_cpu->clk_evt == NULL) {
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800205 pr_err("Unable to allocate clock event device\n");
206 goto err;
207 }
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700208 hv_init_clockevent_device(hv_cpu->clk_evt, cpu);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700209
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700210 hv_cpu->synic_message_page =
Jason Wang2608fb62013-06-19 11:28:10 +0800211 (void *)get_zeroed_page(GFP_ATOMIC);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700212 if (hv_cpu->synic_message_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800213 pr_err("Unable to allocate SYNIC message page\n");
214 goto err;
215 }
216
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700217 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
218 if (hv_cpu->synic_event_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800219 pr_err("Unable to allocate SYNIC event page\n");
220 goto err;
221 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700222
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700223 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
224 if (hv_cpu->post_msg_page == NULL) {
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700225 pr_err("Unable to allocate post msg page\n");
226 goto err;
227 }
Vitaly Kuznetsov3c7630d2016-12-07 01:16:26 -0800228
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700229 INIT_LIST_HEAD(&hv_cpu->chan_list);
Jason Wang2608fb62013-06-19 11:28:10 +0800230 }
231
Michael Kelley248e7422018-03-04 22:17:18 -0700232 if (direct_mode_enabled &&
233 hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector,
234 hv_stimer0_isr))
235 goto err;
236
Jason Wang2608fb62013-06-19 11:28:10 +0800237 return 0;
238err:
Michael Kelley57208632018-08-02 03:08:25 +0000239 /*
240 * Any memory allocations that succeeded will be freed when
241 * the caller cleans up by calling hv_synic_free()
242 */
Jason Wang2608fb62013-06-19 11:28:10 +0800243 return -ENOMEM;
244}
245
Jason Wang2608fb62013-06-19 11:28:10 +0800246
247void hv_synic_free(void)
248{
249 int cpu;
250
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700251 for_each_present_cpu(cpu) {
252 struct hv_per_cpu_context *hv_cpu
253 = per_cpu_ptr(hv_context.cpu_context, cpu);
254
Michael Kelley57208632018-08-02 03:08:25 +0000255 kfree(hv_cpu->clk_evt);
256 free_page((unsigned long)hv_cpu->synic_event_page);
257 free_page((unsigned long)hv_cpu->synic_message_page);
258 free_page((unsigned long)hv_cpu->post_msg_page);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700259 }
260
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700261 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800262}
263
Hank Janssen3e189512010-03-04 22:11:00 +0000264/*
Joe Perches68cb8112018-03-04 22:17:13 -0700265 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700266 *
267 * If it is already initialized by another entity (ie x2v shim), we need to
268 * retrieve the initialized message and event pages. Otherwise, we create and
269 * initialize the message and event pages.
270 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800271int hv_synic_init(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700272{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700273 struct hv_per_cpu_context *hv_cpu
274 = per_cpu_ptr(hv_context.cpu_context, cpu);
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700275 union hv_synic_simp simp;
276 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800277 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700278 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000279
Hank Janssena73e6b72010-01-22 19:17:50 +0000280 /* Setup the Synic's message page */
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700281 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800282 simp.simp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700283 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000284 >> PAGE_SHIFT;
285
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700286 hv_set_simp(simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000287
288 /* Setup the Synic's event page */
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700289 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800290 siefp.siefp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700291 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000292 >> PAGE_SHIFT;
293
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700294 hv_set_siefp(siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000295
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700296 /* Setup the shared SINT. */
Michael Kelley619a4c82018-06-05 13:37:53 -0700297 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700298
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800299 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800300 shared_sint.masked = false;
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700301 if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
K. Y. Srinivasan6c248aa2017-03-14 18:01:39 -0700302 shared_sint.auto_eoi = false;
303 else
304 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700305
Michael Kelley619a4c82018-06-05 13:37:53 -0700306 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700307
Bill Pemberton454f18a2009-07-27 16:47:24 -0400308 /* Enable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700309 hv_get_synic_state(sctrl.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800310 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700311
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700312 hv_set_synic_state(sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700313
K. Y. Srinivasan917ea422012-12-01 06:46:47 -0800314 /*
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800315 * Register the per-cpu clockevent source.
316 */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700317 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700318 clockevents_config_and_register(hv_cpu->clk_evt,
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -0800319 HV_TIMER_FREQUENCY,
320 HV_MIN_DELTA_TICKS,
321 HV_MAX_MAX_DELTA_TICKS);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800322 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700323}
324
Hank Janssen3e189512010-03-04 22:11:00 +0000325/*
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800326 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
327 */
328void hv_synic_clockevents_cleanup(void)
329{
330 int cpu;
331
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700332 if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800333 return;
334
Michael Kelley248e7422018-03-04 22:17:18 -0700335 if (direct_mode_enabled)
336 hv_remove_stimer0_irq(stimer0_irq);
337
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700338 for_each_present_cpu(cpu) {
339 struct hv_per_cpu_context *hv_cpu
340 = per_cpu_ptr(hv_context.cpu_context, cpu);
341
342 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
343 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800344}
345
346/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800347 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700348 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800349int hv_synic_cleanup(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700350{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800351 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700352 union hv_synic_simp simp;
353 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800354 union hv_synic_scontrol sctrl;
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800355 struct vmbus_channel *channel, *sc;
356 bool channel_found = false;
357 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700358
K. Y. Srinivasana3cadf32018-10-18 05:09:28 +0000359 hv_get_synic_state(sctrl.as_uint64);
360 if (sctrl.enable != 1)
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800361 return -EFAULT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700362
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800363 /*
364 * Search for channels which are bound to the CPU we're about to
365 * cleanup. In case we find one and vmbus is still connected we need to
366 * fail, this will effectively prevent CPU offlining. There is no way
367 * we can re-bind channels to different CPUs for now.
368 */
369 mutex_lock(&vmbus_connection.channel_mutex);
370 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
371 if (channel->target_cpu == cpu) {
372 channel_found = true;
373 break;
374 }
375 spin_lock_irqsave(&channel->lock, flags);
376 list_for_each_entry(sc, &channel->sc_list, sc_list) {
377 if (sc->target_cpu == cpu) {
378 channel_found = true;
379 break;
380 }
381 }
382 spin_unlock_irqrestore(&channel->lock, flags);
383 if (channel_found)
384 break;
385 }
386 mutex_unlock(&vmbus_connection.channel_mutex);
387
388 if (channel_found && vmbus_connection.conn_state == CONNECTED)
389 return -EBUSY;
390
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800391 /* Turn off clockevent device */
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700392 if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) {
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700393 struct hv_per_cpu_context *hv_cpu
394 = this_cpu_ptr(hv_context.cpu_context);
395
396 clockevents_unbind_device(hv_cpu->clk_evt, cpu);
397 hv_ce_shutdown(hv_cpu->clk_evt);
Vitaly Kuznetsov6ffc4b82016-12-03 12:34:35 -0800398 }
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800399
Michael Kelley619a4c82018-06-05 13:37:53 -0700400 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700401
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800402 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700403
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800404 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400405 /* Disable the interrupt */
Michael Kelley619a4c82018-06-05 13:37:53 -0700406 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700407
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700408 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800409 simp.simp_enabled = 0;
410 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700411
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700412 hv_set_simp(simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700413
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700414 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800415 siefp.siefp_enabled = 0;
416 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700417
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700418 hv_set_siefp(siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700419
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800420 /* Disable the global synic bit */
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800421 sctrl.enable = 0;
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700422 hv_set_synic_state(sctrl.as_uint64);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800423
424 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700425}