1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/hyperv.h>
17 #include <linux/random.h>
18 #include <linux/clockchips.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <clocksource/hyperv_timer.h>
22 #include <asm/mshyperv.h>
23 #include "hyperv_vmbus.h"
25 /* The one and only */
26 struct hv_context hv_context;
29 * hv_init - Main initialization routine.
31 * This routine must be called before any other routines in here are called
35 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
36 if (!hv_context.cpu_context)
42 * hv_post_message - Post a message using the hypervisor message IPC.
44 * This involves a hypercall.
46 int hv_post_message(union hv_connection_id connection_id,
47 enum hv_message_type message_type,
48 void *payload, size_t payload_size)
50 struct hv_input_post_message *aligned_msg;
54 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
57 local_irq_save(flags);
59 aligned_msg = *this_cpu_ptr(hyperv_pcpu_input_arg);
60 aligned_msg->connectionid = connection_id;
61 aligned_msg->reserved = 0;
62 aligned_msg->message_type = message_type;
63 aligned_msg->payload_size = payload_size;
64 memcpy((void *)aligned_msg->payload, payload, payload_size);
66 if (hv_isolation_type_snp())
67 status = hv_ghcb_hypercall(HVCALL_POST_MESSAGE,
68 (void *)aligned_msg, NULL,
69 sizeof(*aligned_msg));
71 status = hv_do_hypercall(HVCALL_POST_MESSAGE,
74 local_irq_restore(flags);
76 return hv_result(status);
79 int hv_synic_alloc(void)
82 struct hv_per_cpu_context *hv_cpu;
85 * First, zero all per-cpu memory areas so hv_synic_free() can
86 * detect what memory has been allocated and cleanup properly
89 for_each_present_cpu(cpu) {
90 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
91 memset(hv_cpu, 0, sizeof(*hv_cpu));
94 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
96 if (hv_context.hv_numa_map == NULL) {
97 pr_err("Unable to allocate NUMA map\n");
101 for_each_present_cpu(cpu) {
102 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
104 tasklet_init(&hv_cpu->msg_dpc,
105 vmbus_on_msg_dpc, (unsigned long) hv_cpu);
108 * Synic message and event pages are allocated by paravisor.
109 * Skip these pages allocation here.
111 if (!hv_isolation_type_snp() && !hv_root_partition) {
112 hv_cpu->synic_message_page =
113 (void *)get_zeroed_page(GFP_ATOMIC);
114 if (hv_cpu->synic_message_page == NULL) {
115 pr_err("Unable to allocate SYNIC message page\n");
119 hv_cpu->synic_event_page =
120 (void *)get_zeroed_page(GFP_ATOMIC);
121 if (hv_cpu->synic_event_page == NULL) {
122 pr_err("Unable to allocate SYNIC event page\n");
131 * Any memory allocations that succeeded will be freed when
132 * the caller cleans up by calling hv_synic_free()
138 void hv_synic_free(void)
142 for_each_present_cpu(cpu) {
143 struct hv_per_cpu_context *hv_cpu
144 = per_cpu_ptr(hv_context.cpu_context, cpu);
146 free_page((unsigned long)hv_cpu->synic_event_page);
147 free_page((unsigned long)hv_cpu->synic_message_page);
150 kfree(hv_context.hv_numa_map);
154 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
156 * If it is already initialized by another entity (ie x2v shim), we need to
157 * retrieve the initialized message and event pages. Otherwise, we create and
158 * initialize the message and event pages.
160 void hv_synic_enable_regs(unsigned int cpu)
162 struct hv_per_cpu_context *hv_cpu
163 = per_cpu_ptr(hv_context.cpu_context, cpu);
164 union hv_synic_simp simp;
165 union hv_synic_siefp siefp;
166 union hv_synic_sint shared_sint;
167 union hv_synic_scontrol sctrl;
169 /* Setup the Synic's message page */
170 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
171 simp.simp_enabled = 1;
173 if (hv_isolation_type_snp() || hv_root_partition) {
174 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
175 u64 base = (simp.base_simp_gpa << HV_HYP_PAGE_SHIFT) &
176 ~ms_hyperv.shared_gpa_boundary;
177 hv_cpu->synic_message_page
178 = (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
179 if (!hv_cpu->synic_message_page)
180 pr_err("Fail to map synic message page.\n");
182 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
183 >> HV_HYP_PAGE_SHIFT;
186 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
188 /* Setup the Synic's event page */
189 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
190 siefp.siefp_enabled = 1;
192 if (hv_isolation_type_snp() || hv_root_partition) {
193 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
194 u64 base = (siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT) &
195 ~ms_hyperv.shared_gpa_boundary;
196 hv_cpu->synic_event_page
197 = (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
198 if (!hv_cpu->synic_event_page)
199 pr_err("Fail to map synic event page.\n");
201 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
202 >> HV_HYP_PAGE_SHIFT;
205 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
207 /* Setup the shared SINT. */
209 enable_percpu_irq(vmbus_irq, 0);
210 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
213 shared_sint.vector = vmbus_interrupt;
214 shared_sint.masked = false;
217 * On architectures where Hyper-V doesn't support AEOI (e.g., ARM64),
218 * it doesn't provide a recommendation flag and AEOI must be disabled.
220 #ifdef HV_DEPRECATING_AEOI_RECOMMENDED
221 shared_sint.auto_eoi =
222 !(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED);
224 shared_sint.auto_eoi = 0;
226 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
227 shared_sint.as_uint64);
229 /* Enable the global synic bit */
230 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
233 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
236 int hv_synic_init(unsigned int cpu)
238 hv_synic_enable_regs(cpu);
240 hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
246 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
248 void hv_synic_disable_regs(unsigned int cpu)
250 struct hv_per_cpu_context *hv_cpu
251 = per_cpu_ptr(hv_context.cpu_context, cpu);
252 union hv_synic_sint shared_sint;
253 union hv_synic_simp simp;
254 union hv_synic_siefp siefp;
255 union hv_synic_scontrol sctrl;
257 shared_sint.as_uint64 = hv_get_register(HV_REGISTER_SINT0 +
260 shared_sint.masked = 1;
262 /* Need to correctly cleanup in the case of SMP!!! */
263 /* Disable the interrupt */
264 hv_set_register(HV_REGISTER_SINT0 + VMBUS_MESSAGE_SINT,
265 shared_sint.as_uint64);
267 simp.as_uint64 = hv_get_register(HV_REGISTER_SIMP);
269 * In Isolation VM, sim and sief pages are allocated by
270 * paravisor. These pages also will be used by kdump
271 * kernel. So just reset enable bit here and keep page
274 simp.simp_enabled = 0;
275 if (hv_isolation_type_snp() || hv_root_partition) {
276 iounmap(hv_cpu->synic_message_page);
277 hv_cpu->synic_message_page = NULL;
279 simp.base_simp_gpa = 0;
282 hv_set_register(HV_REGISTER_SIMP, simp.as_uint64);
284 siefp.as_uint64 = hv_get_register(HV_REGISTER_SIEFP);
285 siefp.siefp_enabled = 0;
287 if (hv_isolation_type_snp() || hv_root_partition) {
288 iounmap(hv_cpu->synic_event_page);
289 hv_cpu->synic_event_page = NULL;
291 siefp.base_siefp_gpa = 0;
294 hv_set_register(HV_REGISTER_SIEFP, siefp.as_uint64);
296 /* Disable the global synic bit */
297 sctrl.as_uint64 = hv_get_register(HV_REGISTER_SCONTROL);
299 hv_set_register(HV_REGISTER_SCONTROL, sctrl.as_uint64);
302 disable_percpu_irq(vmbus_irq);
305 #define HV_MAX_TRIES 3
307 * Scan the event flags page of 'this' CPU looking for any bit that is set. If we find one
308 * bit set, then wait for a few milliseconds. Repeat these steps for a maximum of 3 times.
309 * Return 'true', if there is still any set bit after this operation; 'false', otherwise.
311 * If a bit is set, that means there is a pending channel interrupt. The expectation is
312 * that the normal interrupt handling mechanism will find and process the channel interrupt
313 * "very soon", and in the process clear the bit.
315 static bool hv_synic_event_pending(void)
317 struct hv_per_cpu_context *hv_cpu = this_cpu_ptr(hv_context.cpu_context);
318 union hv_synic_event_flags *event =
319 (union hv_synic_event_flags *)hv_cpu->synic_event_page + VMBUS_MESSAGE_SINT;
320 unsigned long *recv_int_page = event->flags; /* assumes VMBus version >= VERSION_WIN8 */
327 for_each_set_bit(relid, recv_int_page, HV_EVENT_FLAGS_COUNT) {
328 /* Special case - VMBus channel protocol messages */
334 if (pending && tries++ < HV_MAX_TRIES) {
335 usleep_range(10000, 20000);
341 int hv_synic_cleanup(unsigned int cpu)
343 struct vmbus_channel *channel, *sc;
344 bool channel_found = false;
346 if (vmbus_connection.conn_state != CONNECTED)
350 * Hyper-V does not provide a way to change the connect CPU once
351 * it is set; we must prevent the connect CPU from going offline
352 * while the VM is running normally. But in the panic or kexec()
353 * path where the vmbus is already disconnected, the CPU must be
354 * allowed to shut down.
356 if (cpu == VMBUS_CONNECT_CPU)
360 * Search for channels which are bound to the CPU we're about to
361 * cleanup. In case we find one and vmbus is still connected, we
362 * fail; this will effectively prevent CPU offlining.
364 * TODO: Re-bind the channels to different CPUs.
366 mutex_lock(&vmbus_connection.channel_mutex);
367 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
368 if (channel->target_cpu == cpu) {
369 channel_found = true;
372 list_for_each_entry(sc, &channel->sc_list, sc_list) {
373 if (sc->target_cpu == cpu) {
374 channel_found = true;
381 mutex_unlock(&vmbus_connection.channel_mutex);
387 * channel_found == false means that any channels that were previously
388 * assigned to the CPU have been reassigned elsewhere with a call of
389 * vmbus_send_modifychannel(). Scan the event flags page looking for
390 * bits that are set and waiting with a timeout for vmbus_chan_sched()
391 * to process such bits. If bits are still set after this operation
392 * and VMBus is connected, fail the CPU offlining operation.
394 if (vmbus_proto_version >= VERSION_WIN10_V4_1 && hv_synic_event_pending())
398 hv_stimer_legacy_cleanup(cpu);
400 hv_synic_disable_regs(cpu);