1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/hyperv.h>
16 #include <linux/version.h>
17 #include <linux/random.h>
18 #include <linux/clockchips.h>
19 #include <clocksource/hyperv_timer.h>
20 #include <asm/mshyperv.h>
21 #include "hyperv_vmbus.h"
23 /* The one and only */
24 struct hv_context hv_context;
27 * hv_init - Main initialization routine.
29 * This routine must be called before any other routines in here are called
33 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
34 if (!hv_context.cpu_context)
40 * Functions for allocating and freeing memory with size and
41 * alignment HV_HYP_PAGE_SIZE. These functions are needed because
42 * the guest page size may not be the same as the Hyper-V page
43 * size. We depend upon kmalloc() aligning power-of-two size
44 * allocations to the allocation size boundary, so that the
45 * allocated memory appears to Hyper-V as a page of the size
49 void *hv_alloc_hyperv_page(void)
51 BUILD_BUG_ON(PAGE_SIZE < HV_HYP_PAGE_SIZE);
53 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
54 return (void *)__get_free_page(GFP_KERNEL);
56 return kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
59 void *hv_alloc_hyperv_zeroed_page(void)
61 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
62 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
64 return kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
67 void hv_free_hyperv_page(unsigned long addr)
69 if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
76 * hv_post_message - Post a message using the hypervisor message IPC.
78 * This involves a hypercall.
80 int hv_post_message(union hv_connection_id connection_id,
81 enum hv_message_type message_type,
82 void *payload, size_t payload_size)
84 struct hv_input_post_message *aligned_msg;
85 struct hv_per_cpu_context *hv_cpu;
88 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
91 hv_cpu = get_cpu_ptr(hv_context.cpu_context);
92 aligned_msg = hv_cpu->post_msg_page;
93 aligned_msg->connectionid = connection_id;
94 aligned_msg->reserved = 0;
95 aligned_msg->message_type = message_type;
96 aligned_msg->payload_size = payload_size;
97 memcpy((void *)aligned_msg->payload, payload, payload_size);
99 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
101 /* Preemption must remain disabled until after the hypercall
102 * so some other thread can't get scheduled onto this cpu and
103 * corrupt the per-cpu post_msg_page
107 return status & 0xFFFF;
110 int hv_synic_alloc(void)
113 struct hv_per_cpu_context *hv_cpu;
116 * First, zero all per-cpu memory areas so hv_synic_free() can
117 * detect what memory has been allocated and cleanup properly
118 * after any failures.
120 for_each_present_cpu(cpu) {
121 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
122 memset(hv_cpu, 0, sizeof(*hv_cpu));
125 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
127 if (hv_context.hv_numa_map == NULL) {
128 pr_err("Unable to allocate NUMA map\n");
132 for_each_present_cpu(cpu) {
133 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
135 tasklet_init(&hv_cpu->msg_dpc,
136 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
138 hv_cpu->synic_message_page =
139 (void *)get_zeroed_page(GFP_ATOMIC);
140 if (hv_cpu->synic_message_page == NULL) {
141 pr_err("Unable to allocate SYNIC message page\n");
145 hv_cpu->synic_event_page = (void *)get_zeroed_page(GFP_ATOMIC);
146 if (hv_cpu->synic_event_page == NULL) {
147 pr_err("Unable to allocate SYNIC event page\n");
151 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
152 if (hv_cpu->post_msg_page == NULL) {
153 pr_err("Unable to allocate post msg page\n");
161 * Any memory allocations that succeeded will be freed when
162 * the caller cleans up by calling hv_synic_free()
168 void hv_synic_free(void)
172 for_each_present_cpu(cpu) {
173 struct hv_per_cpu_context *hv_cpu
174 = per_cpu_ptr(hv_context.cpu_context, cpu);
176 free_page((unsigned long)hv_cpu->synic_event_page);
177 free_page((unsigned long)hv_cpu->synic_message_page);
178 free_page((unsigned long)hv_cpu->post_msg_page);
181 kfree(hv_context.hv_numa_map);
185 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
187 * If it is already initialized by another entity (ie x2v shim), we need to
188 * retrieve the initialized message and event pages. Otherwise, we create and
189 * initialize the message and event pages.
191 void hv_synic_enable_regs(unsigned int cpu)
193 struct hv_per_cpu_context *hv_cpu
194 = per_cpu_ptr(hv_context.cpu_context, cpu);
195 union hv_synic_simp simp;
196 union hv_synic_siefp siefp;
197 union hv_synic_sint shared_sint;
198 union hv_synic_scontrol sctrl;
200 /* Setup the Synic's message page */
201 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
202 simp.simp_enabled = 1;
203 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
204 >> HV_HYP_PAGE_SHIFT;
206 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
208 /* Setup the Synic's event page */
209 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
210 siefp.siefp_enabled = 1;
211 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
212 >> HV_HYP_PAGE_SHIFT;
214 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
216 /* Setup the shared SINT. */
217 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
220 shared_sint.vector = hv_get_vector();
221 shared_sint.masked = false;
222 shared_sint.auto_eoi = hv_recommend_using_aeoi();
223 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
224 shared_sint.as_uint64);
226 /* Enable the global synic bit */
227 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
230 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
233 int hv_synic_init(unsigned int cpu)
235 hv_synic_enable_regs(cpu);
237 hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
243 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
245 void hv_synic_disable_regs(unsigned int cpu)
247 union hv_synic_sint shared_sint;
248 union hv_synic_simp simp;
249 union hv_synic_siefp siefp;
250 union hv_synic_scontrol sctrl;
252 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
255 shared_sint.masked = 1;
257 /* Need to correctly cleanup in the case of SMP!!! */
258 /* Disable the interrupt */
259 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
260 shared_sint.as_uint64);
262 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
263 simp.simp_enabled = 0;
264 simp.base_simp_gpa = 0;
266 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
268 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
269 siefp.siefp_enabled = 0;
270 siefp.base_siefp_gpa = 0;
272 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
274 /* Disable the global synic bit */
275 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
277 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
281 int hv_synic_cleanup(unsigned int cpu)
283 struct vmbus_channel *channel, *sc;
284 bool channel_found = false;
287 * Hyper-V does not provide a way to change the connect CPU once
288 * it is set; we must prevent the connect CPU from going offline
289 * while the VM is running normally. But in the panic or kexec()
290 * path where the vmbus is already disconnected, the CPU must be
291 * allowed to shut down.
293 if (cpu == VMBUS_CONNECT_CPU &&
294 vmbus_connection.conn_state == CONNECTED)
298 * Search for channels which are bound to the CPU we're about to
299 * cleanup. In case we find one and vmbus is still connected, we
300 * fail; this will effectively prevent CPU offlining.
302 * TODO: Re-bind the channels to different CPUs.
304 mutex_lock(&vmbus_connection.channel_mutex);
305 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
306 if (channel->target_cpu == cpu) {
307 channel_found = true;
310 list_for_each_entry(sc, &channel->sc_list, sc_list) {
311 if (sc->target_cpu == cpu) {
312 channel_found = true;
319 mutex_unlock(&vmbus_connection.channel_mutex);
321 if (channel_found && vmbus_connection.conn_state == CONNECTED)
324 hv_stimer_legacy_cleanup(cpu);
326 hv_synic_disable_regs(cpu);