Thomas Gleixner | 3b20eb2 | 2019-05-29 16:57:35 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 2 | /* |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 5 | * Authors: |
| 6 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 7 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 8 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 11 | #include <linux/io.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Bill Pemberton | b7c947f | 2009-07-29 17:00:13 -0400 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 16 | #include <linux/hyperv.h> |
Michael Kelley | 248e742 | 2018-03-04 22:17:18 -0700 | [diff] [blame] | 17 | #include <linux/random.h> |
K. Y. Srinivasan | 4061ed9 | 2015-01-09 23:54:32 -0800 | [diff] [blame] | 18 | #include <linux/clockchips.h> |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 19 | #include <linux/delay.h> |
Michael Kelley | d608715 | 2021-03-02 13:38:18 -0800 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Michael Kelley | fd1fea6 | 2019-07-01 04:25:56 +0000 | [diff] [blame] | 21 | #include <clocksource/hyperv_timer.h> |
K. Y. Srinivasan | 4061ed9 | 2015-01-09 23:54:32 -0800 | [diff] [blame] | 22 | #include <asm/mshyperv.h> |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 23 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 24 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 25 | /* The one and only */ |
K. Y. Srinivasan | a3cadf3 | 2018-10-18 05:09:28 +0000 | [diff] [blame] | 26 | struct hv_context hv_context; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 27 | |
Michael Kelley | 248e742 | 2018-03-04 22:17:18 -0700 | [diff] [blame] | 28 | /* |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 29 | * hv_init - Main initialization routine. |
Greg Kroah-Hartman | 0831ad0 | 2009-08-31 20:23:33 -0700 | [diff] [blame] | 30 | * |
| 31 | * This routine must be called before any other routines in here are called |
| 32 | */ |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 33 | int hv_init(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 34 | { |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 35 | hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context); |
| 36 | if (!hv_context.cpu_context) |
| 37 | return -ENOMEM; |
K. Y. Srinivasan | 5433e00 | 2011-08-25 09:48:51 -0700 | [diff] [blame] | 38 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 41 | /* |
Michael Kelley | ca48739e | 2021-03-02 13:38:13 -0800 | [diff] [blame] | 42 | * Functions for allocating and freeing memory with size and |
| 43 | * alignment HV_HYP_PAGE_SIZE. These functions are needed because |
| 44 | * the guest page size may not be the same as the Hyper-V page |
| 45 | * size. We depend upon kmalloc() aligning power-of-two size |
| 46 | * allocations to the allocation size boundary, so that the |
| 47 | * allocated memory appears to Hyper-V as a page of the size |
| 48 | * it expects. |
| 49 | */ |
| 50 | |
| 51 | void *hv_alloc_hyperv_page(void) |
| 52 | { |
| 53 | BUILD_BUG_ON(PAGE_SIZE < HV_HYP_PAGE_SIZE); |
| 54 | |
| 55 | if (PAGE_SIZE == HV_HYP_PAGE_SIZE) |
| 56 | return (void *)__get_free_page(GFP_KERNEL); |
| 57 | else |
| 58 | return kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); |
| 59 | } |
| 60 | |
| 61 | void *hv_alloc_hyperv_zeroed_page(void) |
| 62 | { |
| 63 | if (PAGE_SIZE == HV_HYP_PAGE_SIZE) |
| 64 | return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO); |
| 65 | else |
| 66 | return kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); |
| 67 | } |
| 68 | |
| 69 | void hv_free_hyperv_page(unsigned long addr) |
| 70 | { |
| 71 | if (PAGE_SIZE == HV_HYP_PAGE_SIZE) |
| 72 | free_page(addr); |
| 73 | else |
| 74 | kfree((void *)addr); |
| 75 | } |
| 76 | |
| 77 | /* |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 78 | * hv_post_message - Post a message using the hypervisor message IPC. |
Greg Kroah-Hartman | 0831ad0 | 2009-08-31 20:23:33 -0700 | [diff] [blame] | 79 | * |
| 80 | * This involves a hypercall. |
| 81 | */ |
Dan Carpenter | 415f0a0 | 2012-03-28 09:58:07 +0300 | [diff] [blame] | 82 | int hv_post_message(union hv_connection_id connection_id, |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 83 | enum hv_message_type message_type, |
| 84 | void *payload, size_t payload_size) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 85 | { |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 86 | struct hv_input_post_message *aligned_msg; |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 87 | struct hv_per_cpu_context *hv_cpu; |
Jake Oshins | a108393 | 2015-12-14 16:01:40 -0800 | [diff] [blame] | 88 | u64 status; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 89 | |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 90 | if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) |
K. Y. Srinivasan | 39594ab | 2011-06-06 15:50:09 -0700 | [diff] [blame] | 91 | return -EMSGSIZE; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 92 | |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 93 | hv_cpu = get_cpu_ptr(hv_context.cpu_context); |
| 94 | aligned_msg = hv_cpu->post_msg_page; |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 95 | aligned_msg->connectionid = connection_id; |
K. Y. Srinivasan | b29ef35 | 2014-08-28 18:29:52 -0700 | [diff] [blame] | 96 | aligned_msg->reserved = 0; |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 97 | aligned_msg->message_type = message_type; |
| 98 | aligned_msg->payload_size = payload_size; |
| 99 | memcpy((void *)aligned_msg->payload, payload, payload_size); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 100 | |
Tianyu Lan | 20c89a5 | 2021-10-25 08:21:12 -0400 | [diff] [blame] | 101 | if (hv_isolation_type_snp()) |
| 102 | status = hv_ghcb_hypercall(HVCALL_POST_MESSAGE, |
| 103 | (void *)aligned_msg, NULL, |
| 104 | sizeof(*aligned_msg)); |
| 105 | else |
| 106 | status = hv_do_hypercall(HVCALL_POST_MESSAGE, |
| 107 | aligned_msg, NULL); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 108 | |
Michael Kelley | 13b9abf | 2017-05-18 10:46:07 -0700 | [diff] [blame] | 109 | /* Preemption must remain disabled until after the hypercall |
| 110 | * so some other thread can't get scheduled onto this cpu and |
| 111 | * corrupt the per-cpu post_msg_page |
| 112 | */ |
| 113 | put_cpu_ptr(hv_cpu); |
| 114 | |
Joseph Salisbury | 753ed9c | 2021-04-16 17:43:03 -0700 | [diff] [blame] | 115 | return hv_result(status); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 118 | int hv_synic_alloc(void) |
| 119 | { |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 120 | int cpu; |
Michael Kelley | f25a7ec | 2018-08-10 23:06:11 +0000 | [diff] [blame] | 121 | struct hv_per_cpu_context *hv_cpu; |
| 122 | |
| 123 | /* |
| 124 | * First, zero all per-cpu memory areas so hv_synic_free() can |
| 125 | * detect what memory has been allocated and cleanup properly |
| 126 | * after any failures. |
| 127 | */ |
| 128 | for_each_present_cpu(cpu) { |
| 129 | hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu); |
| 130 | memset(hv_cpu, 0, sizeof(*hv_cpu)); |
| 131 | } |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 132 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 133 | hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask), |
Jia-Ju Bai | 597ff72 | 2018-03-04 22:17:12 -0700 | [diff] [blame] | 134 | GFP_KERNEL); |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 135 | if (hv_context.hv_numa_map == NULL) { |
| 136 | pr_err("Unable to allocate NUMA map\n"); |
| 137 | goto err; |
| 138 | } |
| 139 | |
Vitaly Kuznetsov | 421b8f2 | 2016-12-07 01:16:25 -0800 | [diff] [blame] | 140 | for_each_present_cpu(cpu) { |
Michael Kelley | f25a7ec | 2018-08-10 23:06:11 +0000 | [diff] [blame] | 141 | hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu); |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 142 | |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 143 | tasklet_init(&hv_cpu->msg_dpc, |
| 144 | vmbus_on_msg_dpc, (unsigned long) hv_cpu); |
K. Y. Srinivasan | d81274a | 2016-02-26 15:13:21 -0800 | [diff] [blame] | 145 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 146 | /* |
| 147 | * Synic message and event pages are allocated by paravisor. |
| 148 | * Skip these pages allocation here. |
| 149 | */ |
| 150 | if (!hv_isolation_type_snp()) { |
| 151 | hv_cpu->synic_message_page = |
| 152 | (void *)get_zeroed_page(GFP_ATOMIC); |
| 153 | if (hv_cpu->synic_message_page == NULL) { |
| 154 | pr_err("Unable to allocate SYNIC message page\n"); |
| 155 | goto err; |
| 156 | } |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 157 | |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 158 | hv_cpu->synic_event_page = |
| 159 | (void *)get_zeroed_page(GFP_ATOMIC); |
| 160 | if (hv_cpu->synic_event_page == NULL) { |
| 161 | pr_err("Unable to allocate SYNIC event page\n"); |
| 162 | goto err; |
| 163 | } |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 164 | } |
K. Y. Srinivasan | b29ef35 | 2014-08-28 18:29:52 -0700 | [diff] [blame] | 165 | |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 166 | hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC); |
| 167 | if (hv_cpu->post_msg_page == NULL) { |
K. Y. Srinivasan | b29ef35 | 2014-08-28 18:29:52 -0700 | [diff] [blame] | 168 | pr_err("Unable to allocate post msg page\n"); |
| 169 | goto err; |
| 170 | } |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | err: |
Michael Kelley | 5720863 | 2018-08-02 03:08:25 +0000 | [diff] [blame] | 175 | /* |
| 176 | * Any memory allocations that succeeded will be freed when |
| 177 | * the caller cleans up by calling hv_synic_free() |
| 178 | */ |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 179 | return -ENOMEM; |
| 180 | } |
| 181 | |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 182 | |
| 183 | void hv_synic_free(void) |
| 184 | { |
| 185 | int cpu; |
| 186 | |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 187 | for_each_present_cpu(cpu) { |
| 188 | struct hv_per_cpu_context *hv_cpu |
| 189 | = per_cpu_ptr(hv_context.cpu_context, cpu); |
| 190 | |
Michael Kelley | 5720863 | 2018-08-02 03:08:25 +0000 | [diff] [blame] | 191 | free_page((unsigned long)hv_cpu->synic_event_page); |
| 192 | free_page((unsigned long)hv_cpu->synic_message_page); |
| 193 | free_page((unsigned long)hv_cpu->post_msg_page); |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 194 | } |
| 195 | |
K. Y. Srinivasan | 9f01ec5 | 2015-08-05 00:52:38 -0700 | [diff] [blame] | 196 | kfree(hv_context.hv_numa_map); |
Jason Wang | 2608fb6 | 2013-06-19 11:28:10 +0800 | [diff] [blame] | 197 | } |
| 198 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 199 | /* |
Joe Perches | 68cb811 | 2018-03-04 22:17:13 -0700 | [diff] [blame] | 200 | * hv_synic_init - Initialize the Synthetic Interrupt Controller. |
Greg Kroah-Hartman | 0831ad0 | 2009-08-31 20:23:33 -0700 | [diff] [blame] | 201 | * |
| 202 | * If it is already initialized by another entity (ie x2v shim), we need to |
| 203 | * retrieve the initialized message and event pages. Otherwise, we create and |
| 204 | * initialize the message and event pages. |
| 205 | */ |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 206 | void hv_synic_enable_regs(unsigned int cpu) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 207 | { |
Stephen Hemminger | 37cdd99 | 2017-02-11 23:02:19 -0700 | [diff] [blame] | 208 | struct hv_per_cpu_context *hv_cpu |
| 209 | = per_cpu_ptr(hv_context.cpu_context, cpu); |
Greg Kroah-Hartman | eacb1b4 | 2009-08-20 12:11:26 -0700 | [diff] [blame] | 210 | union hv_synic_simp simp; |
| 211 | union hv_synic_siefp siefp; |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 212 | union hv_synic_sint shared_sint; |
Greg Kroah-Hartman | eacb1b4 | 2009-08-20 12:11:26 -0700 | [diff] [blame] | 213 | union hv_synic_scontrol sctrl; |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 214 | |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 215 | /* Setup the Synic's message page */ |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 216 | simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP); |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 217 | simp.simp_enabled = 1; |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 218 | |
| 219 | if (hv_isolation_type_snp()) { |
| 220 | hv_cpu->synic_message_page |
| 221 | = memremap(simp.base_simp_gpa << HV_HYP_PAGE_SHIFT, |
| 222 | HV_HYP_PAGE_SIZE, MEMREMAP_WB); |
| 223 | if (!hv_cpu->synic_message_page) |
| 224 | pr_err("Fail to map syinc message page.\n"); |
| 225 | } else { |
| 226 | simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page) |
| 227 | >> HV_HYP_PAGE_SHIFT; |
| 228 | } |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 229 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 230 | hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 231 | |
| 232 | /* Setup the Synic's event page */ |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 233 | siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP); |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 234 | siefp.siefp_enabled = 1; |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 235 | |
| 236 | if (hv_isolation_type_snp()) { |
| 237 | hv_cpu->synic_event_page = |
| 238 | memremap(siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT, |
| 239 | HV_HYP_PAGE_SIZE, MEMREMAP_WB); |
| 240 | |
| 241 | if (!hv_cpu->synic_event_page) |
| 242 | pr_err("Fail to map syinc event page.\n"); |
| 243 | } else { |
| 244 | siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page) |
| 245 | >> HV_HYP_PAGE_SHIFT; |
| 246 | } |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 247 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 248 | hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); |
Hank Janssen | a73e6b7 | 2010-01-22 19:17:50 +0000 | [diff] [blame] | 249 | |
Greg Kroah-Hartman | 0831ad0 | 2009-08-31 20:23:33 -0700 | [diff] [blame] | 250 | /* Setup the shared SINT. */ |
Michael Kelley | d608715 | 2021-03-02 13:38:18 -0800 | [diff] [blame] | 251 | if (vmbus_irq != -1) |
| 252 | enable_percpu_irq(vmbus_irq, 0); |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 253 | shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 + |
| 254 | VMBUS_MESSAGE_SINT); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 255 | |
Michael Kelley | d608715 | 2021-03-02 13:38:18 -0800 | [diff] [blame] | 256 | shared_sint.vector = vmbus_interrupt; |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 257 | shared_sint.masked = false; |
Michael Kelley | 946f4b8 | 2021-03-02 13:38:17 -0800 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64), |
| 261 | * it doesn't provide a recommendation flag and AEOI must be disabled. |
| 262 | */ |
| 263 | #ifdef HV_DEPRECATING_AEOI_RECOMMENDED |
| 264 | shared_sint.auto_eoi = |
| 265 | !(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED); |
| 266 | #else |
| 267 | shared_sint.auto_eoi = 0; |
| 268 | #endif |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 269 | hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, |
| 270 | shared_sint.as_uint64); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 271 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 272 | /* Enable the global synic bit */ |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 273 | sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL); |
Haiyang Zhang | f6feebe | 2010-11-08 14:04:39 -0800 | [diff] [blame] | 274 | sctrl.enable = 1; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 275 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 276 | hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | int hv_synic_init(unsigned int cpu) |
| 280 | { |
| 281 | hv_synic_enable_regs(cpu); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 282 | |
Michael Kelley | 4df4cb9e9 | 2019-11-13 01:11:49 +0000 | [diff] [blame] | 283 | hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT); |
Michael Kelley | fd1fea6 | 2019-07-01 04:25:56 +0000 | [diff] [blame] | 284 | |
Vitaly Kuznetsov | 76d36ab | 2016-12-07 14:53:11 -0800 | [diff] [blame] | 285 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 288 | /* |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 289 | * hv_synic_cleanup - Cleanup routine for hv_synic_init(). |
Greg Kroah-Hartman | 0831ad0 | 2009-08-31 20:23:33 -0700 | [diff] [blame] | 290 | */ |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 291 | void hv_synic_disable_regs(unsigned int cpu) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 292 | { |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 293 | struct hv_per_cpu_context *hv_cpu |
| 294 | = per_cpu_ptr(hv_context.cpu_context, cpu); |
Haiyang Zhang | b8dfb26 | 2010-11-08 14:04:41 -0800 | [diff] [blame] | 295 | union hv_synic_sint shared_sint; |
Greg Kroah-Hartman | eacb1b4 | 2009-08-20 12:11:26 -0700 | [diff] [blame] | 296 | union hv_synic_simp simp; |
| 297 | union hv_synic_siefp siefp; |
Vitaly Kuznetsov | e72e7ac | 2015-02-27 11:25:55 -0800 | [diff] [blame] | 298 | union hv_synic_scontrol sctrl; |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 299 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 300 | shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 + |
| 301 | VMBUS_MESSAGE_SINT); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 302 | |
| 303 | shared_sint.masked = 1; |
| 304 | |
| 305 | /* Need to correctly cleanup in the case of SMP!!! */ |
| 306 | /* Disable the interrupt */ |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 307 | hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT, |
| 308 | shared_sint.as_uint64); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 309 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 310 | simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP); |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 311 | /* |
| 312 | * In Isolation VM, sim and sief pages are allocated by |
| 313 | * paravisor. These pages also will be used by kdump |
| 314 | * kernel. So just reset enable bit here and keep page |
| 315 | * addresses. |
| 316 | */ |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 317 | simp.simp_enabled = 0; |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 318 | if (hv_isolation_type_snp()) |
| 319 | memunmap(hv_cpu->synic_message_page); |
| 320 | else |
| 321 | simp.base_simp_gpa = 0; |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 322 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 323 | hv_set_register(HV_REGISTER_SIMP, simp.as_uint64); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 324 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 325 | siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 326 | siefp.siefp_enabled = 0; |
Tianyu Lan | faff440 | 2021-10-25 08:21:11 -0400 | [diff] [blame] | 327 | |
| 328 | if (hv_isolation_type_snp()) |
| 329 | memunmap(hv_cpu->synic_event_page); |
| 330 | else |
| 331 | siefp.base_siefp_gpa = 0; |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 332 | |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 333 | hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 334 | |
| 335 | /* Disable the global synic bit */ |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 336 | sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 337 | sctrl.enable = 0; |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 338 | hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64); |
Michael Kelley | d608715 | 2021-03-02 13:38:18 -0800 | [diff] [blame] | 339 | |
| 340 | if (vmbus_irq != -1) |
| 341 | disable_percpu_irq(vmbus_irq); |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 344 | #define HV_MAX_TRIES 3 |
| 345 | /* |
| 346 | * Scan the event flags page of 'this' CPU looking for any bit that is set. If we find one |
| 347 | * bit set, then wait for a few milliseconds. Repeat these steps for a maximum of 3 times. |
| 348 | * Return 'true', if there is still any set bit after this operation; 'false', otherwise. |
| 349 | * |
| 350 | * If a bit is set, that means there is a pending channel interrupt. The expectation is |
| 351 | * that the normal interrupt handling mechanism will find and process the channel interrupt |
| 352 | * "very soon", and in the process clear the bit. |
| 353 | */ |
| 354 | static bool hv_synic_event_pending(void) |
| 355 | { |
| 356 | struct hv_per_cpu_context *hv_cpu = this_cpu_ptr(hv_context.cpu_context); |
| 357 | union hv_synic_event_flags *event = |
| 358 | (union hv_synic_event_flags *)hv_cpu->synic_event_page + VMBUS_MESSAGE_SINT; |
| 359 | unsigned long *recv_int_page = event->flags; /* assumes VMBus version >= VERSION_WIN8 */ |
| 360 | bool pending; |
| 361 | u32 relid; |
| 362 | int tries = 0; |
| 363 | |
| 364 | retry: |
| 365 | pending = false; |
| 366 | for_each_set_bit(relid, recv_int_page, HV_EVENT_FLAGS_COUNT) { |
| 367 | /* Special case - VMBus channel protocol messages */ |
| 368 | if (relid == 0) |
| 369 | continue; |
| 370 | pending = true; |
| 371 | break; |
| 372 | } |
| 373 | if (pending && tries++ < HV_MAX_TRIES) { |
| 374 | usleep_range(10000, 20000); |
| 375 | goto retry; |
| 376 | } |
| 377 | return pending; |
| 378 | } |
Michael Kelley | f3c5e63 | 2021-03-02 13:38:15 -0800 | [diff] [blame] | 379 | |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 380 | int hv_synic_cleanup(unsigned int cpu) |
| 381 | { |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 382 | struct vmbus_channel *channel, *sc; |
| 383 | bool channel_found = false; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 384 | |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 385 | if (vmbus_connection.conn_state != CONNECTED) |
| 386 | goto always_cleanup; |
| 387 | |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 388 | /* |
Andrea Parri (Microsoft) | 8a857c5 | 2020-04-06 02:15:04 +0200 | [diff] [blame] | 389 | * Hyper-V does not provide a way to change the connect CPU once |
Chris Co | 92e4dc8 | 2020-11-10 19:01:18 +0000 | [diff] [blame] | 390 | * it is set; we must prevent the connect CPU from going offline |
| 391 | * while the VM is running normally. But in the panic or kexec() |
| 392 | * path where the vmbus is already disconnected, the CPU must be |
| 393 | * allowed to shut down. |
Andrea Parri (Microsoft) | 8a857c5 | 2020-04-06 02:15:04 +0200 | [diff] [blame] | 394 | */ |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 395 | if (cpu == VMBUS_CONNECT_CPU) |
Andrea Parri (Microsoft) | 8a857c5 | 2020-04-06 02:15:04 +0200 | [diff] [blame] | 396 | return -EBUSY; |
| 397 | |
| 398 | /* |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 399 | * Search for channels which are bound to the CPU we're about to |
Andrea Parri (Microsoft) | d570aec | 2020-04-06 02:15:12 +0200 | [diff] [blame] | 400 | * cleanup. In case we find one and vmbus is still connected, we |
| 401 | * fail; this will effectively prevent CPU offlining. |
| 402 | * |
| 403 | * TODO: Re-bind the channels to different CPUs. |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 404 | */ |
| 405 | mutex_lock(&vmbus_connection.channel_mutex); |
| 406 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
| 407 | if (channel->target_cpu == cpu) { |
| 408 | channel_found = true; |
| 409 | break; |
| 410 | } |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 411 | list_for_each_entry(sc, &channel->sc_list, sc_list) { |
| 412 | if (sc->target_cpu == cpu) { |
| 413 | channel_found = true; |
| 414 | break; |
| 415 | } |
| 416 | } |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 417 | if (channel_found) |
| 418 | break; |
| 419 | } |
| 420 | mutex_unlock(&vmbus_connection.channel_mutex); |
| 421 | |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 422 | if (channel_found) |
Vitaly Kuznetsov | 523b940 | 2016-12-07 14:53:12 -0800 | [diff] [blame] | 423 | return -EBUSY; |
| 424 | |
Andrea Parri (Microsoft) | b635ccc | 2021-04-16 16:34:49 +0200 | [diff] [blame] | 425 | /* |
| 426 | * channel_found == false means that any channels that were previously |
| 427 | * assigned to the CPU have been reassigned elsewhere with a call of |
| 428 | * vmbus_send_modifychannel(). Scan the event flags page looking for |
| 429 | * bits that are set and waiting with a timeout for vmbus_chan_sched() |
| 430 | * to process such bits. If bits are still set after this operation |
| 431 | * and VMBus is connected, fail the CPU offlining operation. |
| 432 | */ |
| 433 | if (vmbus_proto_version >= VERSION_WIN10_V4_1 && hv_synic_event_pending()) |
| 434 | return -EBUSY; |
| 435 | |
| 436 | always_cleanup: |
Michael Kelley | 4df4cb9e9 | 2019-11-13 01:11:49 +0000 | [diff] [blame] | 437 | hv_stimer_legacy_cleanup(cpu); |
Vitaly Kuznetsov | e086748 | 2015-02-27 11:25:57 -0800 | [diff] [blame] | 438 | |
Dexuan Cui | dba61cd | 2019-09-05 23:01:15 +0000 | [diff] [blame] | 439 | hv_synic_disable_regs(cpu); |
Vitaly Kuznetsov | 76d36ab | 2016-12-07 14:53:11 -0800 | [diff] [blame] | 440 | |
| 441 | return 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 442 | } |