blob: 6188fb7dda4245e914a7fa46515f5b9161048b67 [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 Kelleyfd1fea62019-07-01 04:25:56 +000019#include <clocksource/hyperv_timer.h>
K. Y. Srinivasan4061ed92015-01-09 23:54:32 -080020#include <asm/mshyperv.h>
K. Y. Srinivasan0f2a6612011-05-12 19:34:28 -070021#include "hyperv_vmbus.h"
Hank Janssen3e7ee492009-07-13 16:02:34 -070022
Bill Pemberton454f18a2009-07-27 16:47:24 -040023/* The one and only */
K. Y. Srinivasana3cadf32018-10-18 05:09:28 +000024struct hv_context hv_context;
Hank Janssen3e7ee492009-07-13 16:02:34 -070025
Michael Kelley248e7422018-03-04 22:17:18 -070026/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080027 * hv_init - Main initialization routine.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070028 *
29 * This routine must be called before any other routines in here are called
30 */
Haiyang Zhangd44890c2010-11-08 14:04:42 -080031int hv_init(void)
Hank Janssen3e7ee492009-07-13 16:02:34 -070032{
Stephen Hemminger37cdd992017-02-11 23:02:19 -070033 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
34 if (!hv_context.cpu_context)
35 return -ENOMEM;
K. Y. Srinivasan5433e002011-08-25 09:48:51 -070036 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -070037}
38
Hank Janssen3e189512010-03-04 22:11:00 +000039/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -080040 * hv_post_message - Post a message using the hypervisor message IPC.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -070041 *
42 * This involves a hypercall.
43 */
Dan Carpenter415f0a02012-03-28 09:58:07 +030044int hv_post_message(union hv_connection_id connection_id,
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080045 enum hv_message_type message_type,
46 void *payload, size_t payload_size)
Hank Janssen3e7ee492009-07-13 16:02:34 -070047{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080048 struct hv_input_post_message *aligned_msg;
Stephen Hemminger37cdd992017-02-11 23:02:19 -070049 struct hv_per_cpu_context *hv_cpu;
Jake Oshinsa1083932015-12-14 16:01:40 -080050 u64 status;
Hank Janssen3e7ee492009-07-13 16:02:34 -070051
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080052 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
K. Y. Srinivasan39594ab2011-06-06 15:50:09 -070053 return -EMSGSIZE;
Hank Janssen3e7ee492009-07-13 16:02:34 -070054
Stephen Hemminger37cdd992017-02-11 23:02:19 -070055 hv_cpu = get_cpu_ptr(hv_context.cpu_context);
56 aligned_msg = hv_cpu->post_msg_page;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080057 aligned_msg->connectionid = connection_id;
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -070058 aligned_msg->reserved = 0;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -080059 aligned_msg->message_type = message_type;
60 aligned_msg->payload_size = payload_size;
61 memcpy((void *)aligned_msg->payload, payload, payload_size);
Hank Janssen3e7ee492009-07-13 16:02:34 -070062
Jake Oshinsa1083932015-12-14 16:01:40 -080063 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
Hank Janssen3e7ee492009-07-13 16:02:34 -070064
Michael Kelley13b9abf2017-05-18 10:46:07 -070065 /* Preemption must remain disabled until after the hypercall
66 * so some other thread can't get scheduled onto this cpu and
67 * corrupt the per-cpu post_msg_page
68 */
69 put_cpu_ptr(hv_cpu);
70
Jake Oshinsa1083932015-12-14 16:01:40 -080071 return status & 0xFFFF;
Hank Janssen3e7ee492009-07-13 16:02:34 -070072}
73
Jason Wang2608fb62013-06-19 11:28:10 +080074int hv_synic_alloc(void)
75{
Jason Wang2608fb62013-06-19 11:28:10 +080076 int cpu;
Michael Kelleyf25a7ec2018-08-10 23:06:11 +000077 struct hv_per_cpu_context *hv_cpu;
78
79 /*
80 * First, zero all per-cpu memory areas so hv_synic_free() can
81 * detect what memory has been allocated and cleanup properly
82 * after any failures.
83 */
84 for_each_present_cpu(cpu) {
85 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
86 memset(hv_cpu, 0, sizeof(*hv_cpu));
87 }
Jason Wang2608fb62013-06-19 11:28:10 +080088
Kees Cook6396bb22018-06-12 14:03:40 -070089 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
Jia-Ju Bai597ff722018-03-04 22:17:12 -070090 GFP_KERNEL);
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -070091 if (hv_context.hv_numa_map == NULL) {
92 pr_err("Unable to allocate NUMA map\n");
93 goto err;
94 }
95
Vitaly Kuznetsov421b8f22016-12-07 01:16:25 -080096 for_each_present_cpu(cpu) {
Michael Kelleyf25a7ec2018-08-10 23:06:11 +000097 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
Jason Wang2608fb62013-06-19 11:28:10 +080098
Stephen Hemminger37cdd992017-02-11 23:02:19 -070099 tasklet_init(&hv_cpu->msg_dpc,
100 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
K. Y. Srinivasand81274a2016-02-26 15:13:21 -0800101
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700102 hv_cpu->synic_message_page =
Jason Wang2608fb62013-06-19 11:28:10 +0800103 (void *)get_zeroed_page(GFP_ATOMIC);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700104 if (hv_cpu->synic_message_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800105 pr_err("Unable to allocate SYNIC message page\n");
106 goto err;
107 }
108
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700109 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
110 if (hv_cpu->synic_event_page == NULL) {
Jason Wang2608fb62013-06-19 11:28:10 +0800111 pr_err("Unable to allocate SYNIC event page\n");
112 goto err;
113 }
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700114
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700115 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
116 if (hv_cpu->post_msg_page == NULL) {
K. Y. Srinivasanb29ef352014-08-28 18:29:52 -0700117 pr_err("Unable to allocate post msg page\n");
118 goto err;
119 }
Vitaly Kuznetsov3c7630d2016-12-07 01:16:26 -0800120
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700121 INIT_LIST_HEAD(&hv_cpu->chan_list);
Jason Wang2608fb62013-06-19 11:28:10 +0800122 }
123
124 return 0;
125err:
Michael Kelley57208632018-08-02 03:08:25 +0000126 /*
127 * Any memory allocations that succeeded will be freed when
128 * the caller cleans up by calling hv_synic_free()
129 */
Jason Wang2608fb62013-06-19 11:28:10 +0800130 return -ENOMEM;
131}
132
Jason Wang2608fb62013-06-19 11:28:10 +0800133
134void hv_synic_free(void)
135{
136 int cpu;
137
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700138 for_each_present_cpu(cpu) {
139 struct hv_per_cpu_context *hv_cpu
140 = per_cpu_ptr(hv_context.cpu_context, cpu);
141
Michael Kelley57208632018-08-02 03:08:25 +0000142 free_page((unsigned long)hv_cpu->synic_event_page);
143 free_page((unsigned long)hv_cpu->synic_message_page);
144 free_page((unsigned long)hv_cpu->post_msg_page);
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700145 }
146
K. Y. Srinivasan9f01ec52015-08-05 00:52:38 -0700147 kfree(hv_context.hv_numa_map);
Jason Wang2608fb62013-06-19 11:28:10 +0800148}
149
Hank Janssen3e189512010-03-04 22:11:00 +0000150/*
Joe Perches68cb8112018-03-04 22:17:13 -0700151 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700152 *
153 * If it is already initialized by another entity (ie x2v shim), we need to
154 * retrieve the initialized message and event pages. Otherwise, we create and
155 * initialize the message and event pages.
156 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800157int hv_synic_init(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700158{
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700159 struct hv_per_cpu_context *hv_cpu
160 = per_cpu_ptr(hv_context.cpu_context, cpu);
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700161 union hv_synic_simp simp;
162 union hv_synic_siefp siefp;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800163 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700164 union hv_synic_scontrol sctrl;
Hank Janssena73e6b72010-01-22 19:17:50 +0000165
Hank Janssena73e6b72010-01-22 19:17:50 +0000166 /* Setup the Synic's message page */
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700167 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800168 simp.simp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700169 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000170 >> PAGE_SHIFT;
171
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700172 hv_set_simp(simp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000173
174 /* Setup the Synic's event page */
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700175 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800176 siefp.siefp_enabled = 1;
Stephen Hemminger37cdd992017-02-11 23:02:19 -0700177 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
Hank Janssena73e6b72010-01-22 19:17:50 +0000178 >> PAGE_SHIFT;
179
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700180 hv_set_siefp(siefp.as_uint64);
Hank Janssena73e6b72010-01-22 19:17:50 +0000181
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700182 /* Setup the shared SINT. */
Michael Kelley619a4c82018-06-05 13:37:53 -0700183 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700184
K. Y. Srinivasan302a3c02013-02-17 11:30:44 -0800185 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800186 shared_sint.masked = false;
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700187 if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
K. Y. Srinivasan6c248aa2017-03-14 18:01:39 -0700188 shared_sint.auto_eoi = false;
189 else
190 shared_sint.auto_eoi = true;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700191
Michael Kelley619a4c82018-06-05 13:37:53 -0700192 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700193
Bill Pemberton454f18a2009-07-27 16:47:24 -0400194 /* Enable the global synic bit */
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700195 hv_get_synic_state(sctrl.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800196 sctrl.enable = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700197
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700198 hv_set_synic_state(sctrl.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700199
Michael Kelleyfd1fea62019-07-01 04:25:56 +0000200 hv_stimer_init(cpu);
201
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800202 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700203}
204
Hank Janssen3e189512010-03-04 22:11:00 +0000205/*
Haiyang Zhangd44890c2010-11-08 14:04:42 -0800206 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
Greg Kroah-Hartman0831ad02009-08-31 20:23:33 -0700207 */
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800208int hv_synic_cleanup(unsigned int cpu)
Hank Janssen3e7ee492009-07-13 16:02:34 -0700209{
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800210 union hv_synic_sint shared_sint;
Greg Kroah-Hartmaneacb1b42009-08-20 12:11:26 -0700211 union hv_synic_simp simp;
212 union hv_synic_siefp siefp;
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800213 union hv_synic_scontrol sctrl;
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800214 struct vmbus_channel *channel, *sc;
215 bool channel_found = false;
216 unsigned long flags;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700217
K. Y. Srinivasana3cadf32018-10-18 05:09:28 +0000218 hv_get_synic_state(sctrl.as_uint64);
219 if (sctrl.enable != 1)
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800220 return -EFAULT;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700221
Vitaly Kuznetsov523b9402016-12-07 14:53:12 -0800222 /*
223 * Search for channels which are bound to the CPU we're about to
224 * cleanup. In case we find one and vmbus is still connected we need to
225 * fail, this will effectively prevent CPU offlining. There is no way
226 * we can re-bind channels to different CPUs for now.
227 */
228 mutex_lock(&vmbus_connection.channel_mutex);
229 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
230 if (channel->target_cpu == cpu) {
231 channel_found = true;
232 break;
233 }
234 spin_lock_irqsave(&channel->lock, flags);
235 list_for_each_entry(sc, &channel->sc_list, sc_list) {
236 if (sc->target_cpu == cpu) {
237 channel_found = true;
238 break;
239 }
240 }
241 spin_unlock_irqrestore(&channel->lock, flags);
242 if (channel_found)
243 break;
244 }
245 mutex_unlock(&vmbus_connection.channel_mutex);
246
247 if (channel_found && vmbus_connection.conn_state == CONNECTED)
248 return -EBUSY;
249
Michael Kelleyfd1fea62019-07-01 04:25:56 +0000250 hv_stimer_cleanup(cpu);
Vitaly Kuznetsove0867482015-02-27 11:25:57 -0800251
Michael Kelley619a4c82018-06-05 13:37:53 -0700252 hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700253
Haiyang Zhangb8dfb262010-11-08 14:04:41 -0800254 shared_sint.masked = 1;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700255
Greg Kroah-Hartman7692fd42010-01-08 09:06:40 -0800256 /* Need to correctly cleanup in the case of SMP!!! */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400257 /* Disable the interrupt */
Michael Kelley619a4c82018-06-05 13:37:53 -0700258 hv_set_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700259
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700260 hv_get_simp(simp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800261 simp.simp_enabled = 0;
262 simp.base_simp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700263
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700264 hv_set_simp(simp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700265
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700266 hv_get_siefp(siefp.as_uint64);
Haiyang Zhangf6feebe2010-11-08 14:04:39 -0800267 siefp.siefp_enabled = 0;
268 siefp.base_siefp_gpa = 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700269
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700270 hv_set_siefp(siefp.as_uint64);
Hank Janssen3e7ee492009-07-13 16:02:34 -0700271
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800272 /* Disable the global synic bit */
Vitaly Kuznetsove72e7ac2015-02-27 11:25:55 -0800273 sctrl.enable = 0;
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700274 hv_set_synic_state(sctrl.as_uint64);
Vitaly Kuznetsov76d36ab2016-12-07 14:53:11 -0800275
276 return 0;
Hank Janssen3e7ee492009-07-13 16:02:34 -0700277}