blob: 917b29e873c51313b52a7c966da3327d24fc55c5 [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>
Michael Kelleyd6087152021-03-02 13:38:18 -080019#include <linux/interrupt.h>
Michael Kelleyfd1fea62019-07-01 04:25:56 +000020#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080021#include <asm/mshyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070022#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070023
Bill Pemberton454f18a2009-07-27 16:47:24 -040024/* The one and only */
K. Y. Srinivasana3cadf32018-10-18 05:09:28 +000025struct hv_context hv_context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070026
Michael Kelley248e7422018-03-04 22:17:18 -070027/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080028 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070029 *
30 * This routine must be called before any other routines in here are called
31 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080032int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070033{
Stephen Hemminger37cdd992017-02-11 23:02:19 -070034 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
35 if (!hv_context.cpu_context)
36 return -ENOMEM;
K. Y. Srinivasan5433e002011-08-25 09:48:51 -070037 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070038}
39
Hank Janssen3e189512010-03-04 22:11:00 +000040/*
Michael Kelleyca48739e2021-03-02 13:38:13 -080041 * Functions for allocating and freeing memory with size and
42 * alignment HV_HYP_PAGE_SIZE. These functions are needed because
43 * the guest page size may not be the same as the Hyper-V page
44 * size. We depend upon kmalloc() aligning power-of-two size
45 * allocations to the allocation size boundary, so that the
46 * allocated memory appears to Hyper-V as a page of the size
47 * it expects.
48 */
49
50void *hv_alloc_hyperv_page(void)
51{
52 BUILD_BUG_ON(PAGE_SIZE < HV_HYP_PAGE_SIZE);
53
54 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
55 return (void *)__get_free_page(GFP_KERNEL);
56 else
57 return kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
58}
59
60void *hv_alloc_hyperv_zeroed_page(void)
61{
62 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
63 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
64 else
65 return kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
66}
67
68void hv_free_hyperv_page(unsigned long addr)
69{
70 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
71 free_page(addr);
72 else
73 kfree((void *)addr);
74}
75
76/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080077 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070078 *
79 * This involves a hypercall.
80 */
Dan Carpenter415f0a02012-03-28 09:58:07 +030081int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080082 enum hv_message_type message_type,
83 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -070084{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080085 struct hv_input_post_message *aligned_msg;
Stephen Hemminger37cdd992017-02-11 23:02:19 -070086 struct hv_per_cpu_context *hv_cpu;
Jake Oshinsa1083932015-12-14 16:01:40 -080087 u64 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -070088
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080089 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -070090 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -070091
Stephen Hemminger37cdd992017-02-11 23:02:19 -070092 hv_cpu = get_cpu_ptr(hv_context.cpu_context);
93 aligned_msg = hv_cpu->post_msg_page;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080094 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -070095 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080096 aligned_msg->message_type = message_type;
97 aligned_msg->payload_size = payload_size;
98 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070099
Jake Oshinsa1083932015-12-14 16:01:40 -0800100 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700101
Michael Kelley13b9abf2017-05-18 10:46:07 -0700102 /* Preemption must remain disabled until after the hypercall
103 * so some other thread can't get scheduled onto this cpu and
104 * corrupt the per-cpu post_msg_page
105 */
106 put_cpu_ptr(hv_cpu);
107
Jake Oshinsa1083932015-12-14 16:01:40 -0800108 return status & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700109}
110
Jason Wang2608fb62013-06-19 11:28:10 +0800111int hv_synic_alloc(void)
112{
Jason Wang2608fb62013-06-19 11:28:10 +0800113 int cpu;
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000114 struct hv_per_cpu_context *hv_cpu;
115
116 /*
117 * First, zero all per-cpu memory areas so hv_synic_free() can
118 * detect what memory has been allocated and cleanup properly
119 * after any failures.
120 */
121 for_each_present_cpu(cpu) {
122 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
123 memset(hv_cpu, 0, sizeof(*hv_cpu));
124 }
Jason Wang2608fb62013-06-19 11:28:10 +0800125
Kees Cook6396bb22018-06-12 14:03:40 -0700126 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
Jia-Ju Bai597ff722018-03-04 22:17:12 -0700127 GFP_KERNEL);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700128 if (hv_context.hv_numa_map == NULL) {
129 pr_err("Unable to allocate NUMA map\n");
130 goto err;
131 }
132
Vitaly Kuznetsov421b8f22016-12-07 01:16:25 -0800133 for_each_present_cpu(cpu) {
Michael Kelleyf25a7ec2018-08-10 23:06:11 +0000134 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
Jason Wang2608fb62013-06-19 11:28:10 +0800135
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700136 tasklet_init(&hv_cpu->msg_dpc,
137 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800138
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700139 hv_cpu->synic_message_page =
Jason Wang2608fb62013-06-19 11:28:10 +0800140 (void *)get_zeroed_page(GFP_ATOMIC);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700141 if (hv_cpu->synic_message_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800142 pr_err("Unable to allocate SYNIC message page\n");
143 goto err;
144 }
145
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700146 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
147 if (hv_cpu->synic_event_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800148 pr_err("Unable to allocate SYNIC event page\n");
149 goto err;
150 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700151
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700152 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
153 if (hv_cpu->post_msg_page == NULL) {
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700154 pr_err("Unable to allocate post msg page\n");
155 goto err;
156 }
Jason Wang2608fb62013-06-19 11:28:10 +0800157 }
158
159 return 0;
160err:
Michael Kelley57208632018-08-02 03:08:25 +0000161 /*
162 * Any memory allocations that succeeded will be freed when
163 * the caller cleans up by calling hv_synic_free()
164 */
Jason Wang2608fb62013-06-19 11:28:10 +0800165 return -ENOMEM;
166}
167
Jason Wang2608fb62013-06-19 11:28:10 +0800168
169void hv_synic_free(void)
170{
171 int cpu;
172
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700173 for_each_present_cpu(cpu) {
174 struct hv_per_cpu_context *hv_cpu
175 = per_cpu_ptr(hv_context.cpu_context, cpu);
176
Michael Kelley57208632018-08-02 03:08:25 +0000177 free_page((unsigned long)hv_cpu->synic_event_page);
178 free_page((unsigned long)hv_cpu->synic_message_page);
179 free_page((unsigned long)hv_cpu->post_msg_page);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700180 }
181
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700182 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800183}
184
Hank Janssen3e189512010-03-04 22:11:00 +0000185/*
Joe Perches68cb8112018-03-04 22:17:13 -0700186 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700187 *
188 * If it is already initialized by another entity (ie x2v shim), we need to
189 * retrieve the initialized message and event pages. Otherwise, we create and
190 * initialize the message and event pages.
191 */
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000192void hv_synic_enable_regs(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700194 struct hv_per_cpu_context *hv_cpu
195 = per_cpu_ptr(hv_context.cpu_context, cpu);
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700196 union hv_synic_simp simp;
197 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800198 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700199 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000200
Hank Janssena73e6b72010-01-22 19:17:50 +0000201 /* Setup the Synic's message page */
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800202 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800203 simp.simp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700204 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
Boqun Fengef514d32020-09-16 11:48:10 +0800205 >> HV_HYP_PAGE_SHIFT;
Hank Janssena73e6b72010-01-22 19:17:50 +0000206
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800207 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000208
209 /* Setup the Synic's event page */
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800210 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800211 siefp.siefp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700212 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
Boqun Fengef514d32020-09-16 11:48:10 +0800213 >> HV_HYP_PAGE_SHIFT;
Hank Janssena73e6b72010-01-22 19:17:50 +0000214
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800215 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000216
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700217 /* Setup the shared SINT. */
Michael Kelleyd6087152021-03-02 13:38:18 -0800218 if (vmbus_irq != -1)
219 enable_percpu_irq(vmbus_irq, 0);
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800220 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
221 VMBUS_MESSAGE_SINT);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700222
Michael Kelleyd6087152021-03-02 13:38:18 -0800223 shared_sint.vector = vmbus_interrupt;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800224 shared_sint.masked = false;
Michael Kelley946f4b82021-03-02 13:38:17 -0800225
226 /*
227 * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64),
228 * it doesn't provide a recommendation flag and AEOI must be disabled.
229 */
230#ifdef HV_DEPRECATING_AEOI_RECOMMENDED
231 shared_sint.auto_eoi =
232 !(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED);
233#else
234 shared_sint.auto_eoi = 0;
235#endif
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800236 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
237 shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700238
Bill Pemberton454f18a2009-07-27 16:47:24 -0400239 /* Enable the global synic bit */
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800240 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800241 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700242
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800243 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000244}
245
246int hv_synic_init(unsigned int cpu)
247{
248 hv_synic_enable_regs(cpu);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700249
Michael Kelley4df4cb9e92019-11-13 01:11:49 +0000250 hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
Michael Kelleyfd1fea62019-07-01 04:25:56 +0000251
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800252 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253}
254
Hank Janssen3e189512010-03-04 22:11:00 +0000255/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800256 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700257 */
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000258void hv_synic_disable_regs(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800260 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700261 union hv_synic_simp simp;
262 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800263 union hv_synic_scontrol sctrl;
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000264
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800265 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
266 VMBUS_MESSAGE_SINT);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000267
268 shared_sint.masked = 1;
269
270 /* Need to correctly cleanup in the case of SMP!!! */
271 /* Disable the interrupt */
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800272 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
273 shared_sint.as_uint64);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000274
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800275 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000276 simp.simp_enabled = 0;
277 simp.base_simp_gpa = 0;
278
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800279 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000280
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800281 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000282 siefp.siefp_enabled = 0;
283 siefp.base_siefp_gpa = 0;
284
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800285 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000286
287 /* Disable the global synic bit */
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800288 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000289 sctrl.enable = 0;
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800290 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
Michael Kelleyd6087152021-03-02 13:38:18 -0800291
292 if (vmbus_irq != -1)
293 disable_percpu_irq(vmbus_irq);
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000294}
295
Michael Kelleyf3c5e632021-03-02 13:38:15 -0800296
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000297int hv_synic_cleanup(unsigned int cpu)
298{
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800299 struct vmbus_channel *channel, *sc;
300 bool channel_found = false;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700301
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800302 /*
Andrea Parri (Microsoft)8a857c52020-04-06 02:15:04 +0200303 * Hyper-V does not provide a way to change the connect CPU once
Chris Co92e4dc82020-11-10 19:01:18 +0000304 * it is set; we must prevent the connect CPU from going offline
305 * while the VM is running normally. But in the panic or kexec()
306 * path where the vmbus is already disconnected, the CPU must be
307 * allowed to shut down.
Andrea Parri (Microsoft)8a857c52020-04-06 02:15:04 +0200308 */
Chris Co92e4dc82020-11-10 19:01:18 +0000309 if (cpu == VMBUS_CONNECT_CPU &&
310 vmbus_connection.conn_state == CONNECTED)
Andrea Parri (Microsoft)8a857c52020-04-06 02:15:04 +0200311 return -EBUSY;
312
313 /*
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800314 * Search for channels which are bound to the CPU we're about to
Andrea Parri (Microsoft)d570aec2020-04-06 02:15:12 +0200315 * cleanup. In case we find one and vmbus is still connected, we
316 * fail; this will effectively prevent CPU offlining.
317 *
318 * TODO: Re-bind the channels to different CPUs.
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800319 */
320 mutex_lock(&vmbus_connection.channel_mutex);
321 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
322 if (channel->target_cpu == cpu) {
323 channel_found = true;
324 break;
325 }
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800326 list_for_each_entry(sc, &channel->sc_list, sc_list) {
327 if (sc->target_cpu == cpu) {
328 channel_found = true;
329 break;
330 }
331 }
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800332 if (channel_found)
333 break;
334 }
335 mutex_unlock(&vmbus_connection.channel_mutex);
336
337 if (channel_found && vmbus_connection.conn_state == CONNECTED)
338 return -EBUSY;
339
Michael Kelley4df4cb9e92019-11-13 01:11:49 +0000340 hv_stimer_legacy_cleanup(cpu);
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800341
Dexuan Cuidba61cd2019-09-05 23:01:15 +0000342 hv_synic_disable_regs(cpu);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800343
344 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700345}