Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / drivers / hv / hv.c
CommitLineData
3b20eb23 1// SPDX-License-Identifier: GPL-2.0-only
3e7ee490 2/*
3e7ee490
HJ
3 * Copyright (c) 2009, Microsoft Corporation.
4 *
3e7ee490
HJ
5 * Authors:
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
3e7ee490 8 */
0a46618d
HJ
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
faff4406 11#include <linux/io.h>
a0086dc5
GKH
12#include <linux/kernel.h>
13#include <linux/mm.h>
5a0e3ad6 14#include <linux/slab.h>
b7c947f0 15#include <linux/vmalloc.h>
46a97191 16#include <linux/hyperv.h>
248e742a 17#include <linux/random.h>
4061ed9e 18#include <linux/clockchips.h>
b635ccc1 19#include <linux/delay.h>
d608715d 20#include <linux/interrupt.h>
fd1fea68 21#include <clocksource/hyperv_timer.h>
4061ed9e 22#include <asm/mshyperv.h>
193061ea 23#include <linux/set_memory.h>
0f2a6619 24#include "hyperv_vmbus.h"
3e7ee490 25
454f18a9 26/* The one and only */
a3cadf38 27struct hv_context hv_context;
3e7ee490 28
3e189519 29/*
d44890c8 30 * hv_init - Main initialization routine.
0831ad04
GKH
31 *
32 * This routine must be called before any other routines in here are called
33 */
d44890c8 34int hv_init(void)
3e7ee490 35{
37cdd991
SH
36 hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
37 if (!hv_context.cpu_context)
38 return -ENOMEM;
5433e003 39 return 0;
3e7ee490
HJ
40}
41
3e189519 42/*
d44890c8 43 * hv_post_message - Post a message using the hypervisor message IPC.
0831ad04
GKH
44 *
45 * This involves a hypercall.
46 */
415f0a02 47int hv_post_message(union hv_connection_id connection_id,
831bcbce
AN
48 enum hv_message_type message_type,
49 void *payload, size_t payload_size)
3e7ee490 50{
b8dfb264 51 struct hv_input_post_message *aligned_msg;
9a6b1a17 52 unsigned long flags;
a108393d 53 u64 status;
3e7ee490 54
b8dfb264 55 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
39594abc 56 return -EMSGSIZE;
3e7ee490 57
9a6b1a17
DC
58 local_irq_save(flags);
59
23378295
DC
60 /*
61 * A TDX VM with the paravisor must use the decrypted post_msg_page: see
62 * the comment in struct hv_per_cpu_context. A SNP VM with the paravisor
63 * can use the encrypted hyperv_pcpu_input_arg because it copies the
64 * input into the GHCB page, which has been decrypted by the paravisor.
65 */
66 if (hv_isolation_type_tdx() && ms_hyperv.paravisor_present)
67 aligned_msg = this_cpu_ptr(hv_context.cpu_context)->post_msg_page;
68 else
69 aligned_msg = *this_cpu_ptr(hyperv_pcpu_input_arg);
70
b8dfb264 71 aligned_msg->connectionid = connection_id;
b29ef354 72 aligned_msg->reserved = 0;
b8dfb264
HZ
73 aligned_msg->message_type = message_type;
74 aligned_msg->payload_size = payload_size;
75 memcpy((void *)aligned_msg->payload, payload, payload_size);
3e7ee490 76
23378295
DC
77 if (ms_hyperv.paravisor_present) {
78 if (hv_isolation_type_tdx())
79 status = hv_tdx_hypercall(HVCALL_POST_MESSAGE,
80 virt_to_phys(aligned_msg), 0);
81 else if (hv_isolation_type_snp())
82 status = hv_ghcb_hypercall(HVCALL_POST_MESSAGE,
83 aligned_msg, NULL,
84 sizeof(*aligned_msg));
85 else
86 status = HV_STATUS_INVALID_PARAMETER;
87 } else {
20c89a55 88 status = hv_do_hypercall(HVCALL_POST_MESSAGE,
831bcbce 89 aligned_msg, NULL);
23378295 90 }
3e7ee490 91
9a6b1a17 92 local_irq_restore(flags);
13b9abfc 93
753ed9c9 94 return hv_result(status);
3e7ee490
HJ
95}
96
2608fb65
JW
97int hv_synic_alloc(void)
98{
193061ea 99 int cpu, ret = -ENOMEM;
f25a7ece
MK
100 struct hv_per_cpu_context *hv_cpu;
101
102 /*
103 * First, zero all per-cpu memory areas so hv_synic_free() can
104 * detect what memory has been allocated and cleanup properly
105 * after any failures.
106 */
107 for_each_present_cpu(cpu) {
108 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
109 memset(hv_cpu, 0, sizeof(*hv_cpu));
110 }
2608fb65 111
6396bb22 112 hv_context.hv_numa_map = kcalloc(nr_node_ids, sizeof(struct cpumask),
597ff72f 113 GFP_KERNEL);
831bcbce 114 if (!hv_context.hv_numa_map) {
9f01ec53
S
115 pr_err("Unable to allocate NUMA map\n");
116 goto err;
117 }
118
421b8f20 119 for_each_present_cpu(cpu) {
f25a7ece 120 hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
37cdd991 121
37cdd991 122 tasklet_init(&hv_cpu->msg_dpc,
831bcbce 123 vmbus_on_msg_dpc, (unsigned long)hv_cpu);
37cdd991 124
23378295
DC
125 if (ms_hyperv.paravisor_present && hv_isolation_type_tdx()) {
126 hv_cpu->post_msg_page = (void *)get_zeroed_page(GFP_ATOMIC);
831bcbce 127 if (!hv_cpu->post_msg_page) {
23378295
DC
128 pr_err("Unable to allocate post msg page\n");
129 goto err;
130 }
131
132 ret = set_memory_decrypted((unsigned long)hv_cpu->post_msg_page, 1);
133 if (ret) {
134 pr_err("Failed to decrypt post msg page: %d\n", ret);
135 /* Just leak the page, as it's unsafe to free the page. */
136 hv_cpu->post_msg_page = NULL;
137 goto err;
138 }
139
140 memset(hv_cpu->post_msg_page, 0, PAGE_SIZE);
141 }
142
faff4406
TL
143 /*
144 * Synic message and event pages are allocated by paravisor.
145 * Skip these pages allocation here.
146 */
db912b89 147 if (!ms_hyperv.paravisor_present && !hv_root_partition()) {
faff4406
TL
148 hv_cpu->synic_message_page =
149 (void *)get_zeroed_page(GFP_ATOMIC);
831bcbce 150 if (!hv_cpu->synic_message_page) {
faff4406
TL
151 pr_err("Unable to allocate SYNIC message page\n");
152 goto err;
153 }
154
155 hv_cpu->synic_event_page =
156 (void *)get_zeroed_page(GFP_ATOMIC);
831bcbce 157 if (!hv_cpu->synic_event_page) {
faff4406 158 pr_err("Unable to allocate SYNIC event page\n");
68f2f2bc
DC
159
160 free_page((unsigned long)hv_cpu->synic_message_page);
161 hv_cpu->synic_message_page = NULL;
faff4406
TL
162 goto err;
163 }
2608fb65 164 }
193061ea 165
68f2f2bc 166 if (!ms_hyperv.paravisor_present &&
e3131f1c 167 (hv_isolation_type_snp() || hv_isolation_type_tdx())) {
193061ea
TL
168 ret = set_memory_decrypted((unsigned long)
169 hv_cpu->synic_message_page, 1);
170 if (ret) {
171 pr_err("Failed to decrypt SYNIC msg page: %d\n", ret);
172 hv_cpu->synic_message_page = NULL;
173
174 /*
175 * Free the event page here so that hv_synic_free()
176 * won't later try to re-encrypt it.
177 */
178 free_page((unsigned long)hv_cpu->synic_event_page);
179 hv_cpu->synic_event_page = NULL;
180 goto err;
181 }
182
183 ret = set_memory_decrypted((unsigned long)
184 hv_cpu->synic_event_page, 1);
185 if (ret) {
186 pr_err("Failed to decrypt SYNIC event page: %d\n", ret);
187 hv_cpu->synic_event_page = NULL;
188 goto err;
189 }
190
191 memset(hv_cpu->synic_message_page, 0, PAGE_SIZE);
192 memset(hv_cpu->synic_event_page, 0, PAGE_SIZE);
193 }
2608fb65
JW
194 }
195
196 return 0;
193061ea 197
2608fb65 198err:
57208632
MK
199 /*
200 * Any memory allocations that succeeded will be freed when
201 * the caller cleans up by calling hv_synic_free()
202 */
193061ea 203 return ret;
2608fb65
JW
204}
205
2608fb65
JW
206void hv_synic_free(void)
207{
193061ea 208 int cpu, ret;
2608fb65 209
37cdd991 210 for_each_present_cpu(cpu) {
831bcbce
AN
211 struct hv_per_cpu_context *hv_cpu =
212 per_cpu_ptr(hv_context.cpu_context, cpu);
37cdd991 213
193061ea 214 /* It's better to leak the page if the encryption fails. */
23378295
DC
215 if (ms_hyperv.paravisor_present && hv_isolation_type_tdx()) {
216 if (hv_cpu->post_msg_page) {
217 ret = set_memory_encrypted((unsigned long)
218 hv_cpu->post_msg_page, 1);
219 if (ret) {
220 pr_err("Failed to encrypt post msg page: %d\n", ret);
221 hv_cpu->post_msg_page = NULL;
222 }
223 }
224 }
225
68f2f2bc 226 if (!ms_hyperv.paravisor_present &&
e3131f1c 227 (hv_isolation_type_snp() || hv_isolation_type_tdx())) {
193061ea
TL
228 if (hv_cpu->synic_message_page) {
229 ret = set_memory_encrypted((unsigned long)
230 hv_cpu->synic_message_page, 1);
231 if (ret) {
232 pr_err("Failed to encrypt SYNIC msg page: %d\n", ret);
233 hv_cpu->synic_message_page = NULL;
234 }
235 }
236
237 if (hv_cpu->synic_event_page) {
238 ret = set_memory_encrypted((unsigned long)
239 hv_cpu->synic_event_page, 1);
240 if (ret) {
241 pr_err("Failed to encrypt SYNIC event page: %d\n", ret);
242 hv_cpu->synic_event_page = NULL;
243 }
244 }
245 }
246
23378295 247 free_page((unsigned long)hv_cpu->post_msg_page);
57208632
MK
248 free_page((unsigned long)hv_cpu->synic_event_page);
249 free_page((unsigned long)hv_cpu->synic_message_page);
37cdd991
SH
250 }
251
9f01ec53 252 kfree(hv_context.hv_numa_map);
2608fb65
JW
253}
254
3e189519 255/*
68cb8117 256 * hv_synic_init - Initialize the Synthetic Interrupt Controller.
0831ad04
GKH
257 *
258 * If it is already initialized by another entity (ie x2v shim), we need to
259 * retrieve the initialized message and event pages. Otherwise, we create and
260 * initialize the message and event pages.
261 */
dba61cda 262void hv_synic_enable_regs(unsigned int cpu)
3e7ee490 263{
831bcbce
AN
264 struct hv_per_cpu_context *hv_cpu =
265 per_cpu_ptr(hv_context.cpu_context, cpu);
eacb1b4d
GKH
266 union hv_synic_simp simp;
267 union hv_synic_siefp siefp;
b8dfb264 268 union hv_synic_sint shared_sint;
eacb1b4d 269 union hv_synic_scontrol sctrl;
a73e6b7c 270
a73e6b7c 271 /* Setup the Synic's message page */
0e3f7d12 272 simp.as_uint64 = hv_get_msr(HV_MSR_SIMP);
f6feebe0 273 simp.simp_enabled = 1;
faff4406 274
db912b89 275 if (ms_hyperv.paravisor_present || hv_root_partition()) {
6afd9dc1
MK
276 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
277 u64 base = (simp.base_simp_gpa << HV_HYP_PAGE_SHIFT) &
278 ~ms_hyperv.shared_gpa_boundary;
831bcbce
AN
279 hv_cpu->synic_message_page =
280 (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
faff4406 281 if (!hv_cpu->synic_message_page)
6afd9dc1 282 pr_err("Fail to map synic message page.\n");
faff4406
TL
283 } else {
284 simp.base_simp_gpa = virt_to_phys(hv_cpu->synic_message_page)
285 >> HV_HYP_PAGE_SHIFT;
286 }
3e7ee490 287
0e3f7d12 288 hv_set_msr(HV_MSR_SIMP, simp.as_uint64);
3e7ee490 289
a73e6b7c 290 /* Setup the Synic's event page */
0e3f7d12 291 siefp.as_uint64 = hv_get_msr(HV_MSR_SIEFP);
f6feebe0 292 siefp.siefp_enabled = 1;
faff4406 293
db912b89 294 if (ms_hyperv.paravisor_present || hv_root_partition()) {
6afd9dc1
MK
295 /* Mask out vTOM bit. ioremap_cache() maps decrypted */
296 u64 base = (siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT) &
297 ~ms_hyperv.shared_gpa_boundary;
831bcbce
AN
298 hv_cpu->synic_event_page =
299 (void *)ioremap_cache(base, HV_HYP_PAGE_SIZE);
faff4406 300 if (!hv_cpu->synic_event_page)
6afd9dc1 301 pr_err("Fail to map synic event page.\n");
faff4406
TL
302 } else {
303 siefp.base_siefp_gpa = virt_to_phys(hv_cpu->synic_event_page)
304 >> HV_HYP_PAGE_SHIFT;
305 }
a73e6b7c 306
0e3f7d12 307 hv_set_msr(HV_MSR_SIEFP, siefp.as_uint64);
0831ad04 308
0831ad04 309 /* Setup the shared SINT. */
d608715d
MK
310 if (vmbus_irq != -1)
311 enable_percpu_irq(vmbus_irq, 0);
0e3f7d12 312 shared_sint.as_uint64 = hv_get_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT);
3e7ee490 313
d608715d 314 shared_sint.vector = vmbus_interrupt;
b8dfb264 315 shared_sint.masked = false;
af37bc75 316 shared_sint.auto_eoi = hv_recommend_using_aeoi();
0e3f7d12 317 hv_set_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
3e7ee490 318
454f18a9 319 /* Enable the global synic bit */
0e3f7d12 320 sctrl.as_uint64 = hv_get_msr(HV_MSR_SCONTROL);
f6feebe0 321 sctrl.enable = 1;
3e7ee490 322
0e3f7d12 323 hv_set_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
dba61cda
DC
324}
325
326int hv_synic_init(unsigned int cpu)
327{
328 hv_synic_enable_regs(cpu);
3e7ee490 329
4df4cb9e 330 hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
e086748c 331
fd1fea68 332 return 0;
e086748c
VK
333}
334
dba61cda 335void hv_synic_disable_regs(unsigned int cpu)
3e7ee490 336{
831bcbce
AN
337 struct hv_per_cpu_context *hv_cpu =
338 per_cpu_ptr(hv_context.cpu_context, cpu);
b8dfb264 339 union hv_synic_sint shared_sint;
eacb1b4d
GKH
340 union hv_synic_simp simp;
341 union hv_synic_siefp siefp;
e72e7ac5 342 union hv_synic_scontrol sctrl;
dba61cda 343
0e3f7d12 344 shared_sint.as_uint64 = hv_get_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT);
dba61cda
DC
345
346 shared_sint.masked = 1;
347
348 /* Need to correctly cleanup in the case of SMP!!! */
349 /* Disable the interrupt */
0e3f7d12 350 hv_set_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
dba61cda 351
0e3f7d12 352 simp.as_uint64 = hv_get_msr(HV_MSR_SIMP);
faff4406
TL
353 /*
354 * In Isolation VM, sim and sief pages are allocated by
355 * paravisor. These pages also will be used by kdump
356 * kernel. So just reset enable bit here and keep page
357 * addresses.
358 */
dba61cda 359 simp.simp_enabled = 0;
db912b89 360 if (ms_hyperv.paravisor_present || hv_root_partition()) {
6afd9dc1 361 iounmap(hv_cpu->synic_message_page);
7fec185a
JJ
362 hv_cpu->synic_message_page = NULL;
363 } else {
faff4406 364 simp.base_simp_gpa = 0;
7fec185a 365 }
dba61cda 366
0e3f7d12 367 hv_set_msr(HV_MSR_SIMP, simp.as_uint64);
dba61cda 368
0e3f7d12 369 siefp.as_uint64 = hv_get_msr(HV_MSR_SIEFP);
dba61cda 370 siefp.siefp_enabled = 0;
faff4406 371
db912b89 372 if (ms_hyperv.paravisor_present || hv_root_partition()) {
6afd9dc1 373 iounmap(hv_cpu->synic_event_page);
7fec185a
JJ
374 hv_cpu->synic_event_page = NULL;
375 } else {
faff4406 376 siefp.base_siefp_gpa = 0;
7fec185a 377 }
dba61cda 378
0e3f7d12 379 hv_set_msr(HV_MSR_SIEFP, siefp.as_uint64);
dba61cda
DC
380
381 /* Disable the global synic bit */
0e3f7d12 382 sctrl.as_uint64 = hv_get_msr(HV_MSR_SCONTROL);
dba61cda 383 sctrl.enable = 0;
0e3f7d12 384 hv_set_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
d608715d
MK
385
386 if (vmbus_irq != -1)
387 disable_percpu_irq(vmbus_irq);
dba61cda
DC
388}
389
b635ccc1
APM
390#define HV_MAX_TRIES 3
391/*
392 * Scan the event flags page of 'this' CPU looking for any bit that is set. If we find one
393 * bit set, then wait for a few milliseconds. Repeat these steps for a maximum of 3 times.
394 * Return 'true', if there is still any set bit after this operation; 'false', otherwise.
395 *
396 * If a bit is set, that means there is a pending channel interrupt. The expectation is
397 * that the normal interrupt handling mechanism will find and process the channel interrupt
398 * "very soon", and in the process clear the bit.
399 */
400static bool hv_synic_event_pending(void)
401{
402 struct hv_per_cpu_context *hv_cpu = this_cpu_ptr(hv_context.cpu_context);
403 union hv_synic_event_flags *event =
404 (union hv_synic_event_flags *)hv_cpu->synic_event_page + VMBUS_MESSAGE_SINT;
405 unsigned long *recv_int_page = event->flags; /* assumes VMBus version >= VERSION_WIN8 */
406 bool pending;
407 u32 relid;
408 int tries = 0;
409
410retry:
411 pending = false;
412 for_each_set_bit(relid, recv_int_page, HV_EVENT_FLAGS_COUNT) {
413 /* Special case - VMBus channel protocol messages */
414 if (relid == 0)
415 continue;
416 pending = true;
417 break;
418 }
419 if (pending && tries++ < HV_MAX_TRIES) {
420 usleep_range(10000, 20000);
421 goto retry;
422 }
423 return pending;
424}
f3c5e63c 425
3a7f7785
HM
426static int hv_pick_new_cpu(struct vmbus_channel *channel)
427{
428 int ret = -EBUSY;
429 int start;
430 int cpu;
431
432 lockdep_assert_cpus_held();
433 lockdep_assert_held(&vmbus_connection.channel_mutex);
434
435 /*
436 * We can't assume that the relevant interrupts will be sent before
437 * the cpu is offlined on older versions of hyperv.
438 */
439 if (vmbus_proto_version < VERSION_WIN10_V5_3)
440 return -EBUSY;
441
442 start = get_random_u32_below(nr_cpu_ids);
443
444 for_each_cpu_wrap(cpu, cpu_online_mask, start) {
445 if (channel->target_cpu == cpu ||
446 channel->target_cpu == VMBUS_CONNECT_CPU)
447 continue;
448
449 ret = vmbus_channel_set_cpu(channel, cpu);
450 if (!ret)
451 break;
452 }
453
454 if (ret)
455 ret = vmbus_channel_set_cpu(channel, VMBUS_CONNECT_CPU);
456
457 return ret;
458}
459
44305569
RK
460/*
461 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
462 */
dba61cda
DC
463int hv_synic_cleanup(unsigned int cpu)
464{
523b9408 465 struct vmbus_channel *channel, *sc;
3a7f7785 466 int ret = 0;
3e7ee490 467
b635ccc1
APM
468 if (vmbus_connection.conn_state != CONNECTED)
469 goto always_cleanup;
470
8a857c55
APM
471 /*
472 * Hyper-V does not provide a way to change the connect CPU once
92e4dc8b
CC
473 * it is set; we must prevent the connect CPU from going offline
474 * while the VM is running normally. But in the panic or kexec()
475 * path where the vmbus is already disconnected, the CPU must be
476 * allowed to shut down.
8a857c55 477 */
b635ccc1 478 if (cpu == VMBUS_CONNECT_CPU)
8a857c55
APM
479 return -EBUSY;
480
523b9408
VK
481 /*
482 * Search for channels which are bound to the CPU we're about to
3a7f7785 483 * cleanup.
523b9408
VK
484 */
485 mutex_lock(&vmbus_connection.channel_mutex);
486 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
487 if (channel->target_cpu == cpu) {
3a7f7785
HM
488 ret = hv_pick_new_cpu(channel);
489 if (ret) {
490 mutex_unlock(&vmbus_connection.channel_mutex);
491 return ret;
492 }
523b9408 493 }
523b9408
VK
494 list_for_each_entry(sc, &channel->sc_list, sc_list) {
495 if (sc->target_cpu == cpu) {
3a7f7785
HM
496 ret = hv_pick_new_cpu(sc);
497 if (ret) {
498 mutex_unlock(&vmbus_connection.channel_mutex);
499 return ret;
500 }
523b9408
VK
501 }
502 }
523b9408
VK
503 }
504 mutex_unlock(&vmbus_connection.channel_mutex);
505
b635ccc1 506 /*
3a7f7785
HM
507 * Scan the event flags page looking for bits that are set and waiting
508 * with a timeout for vmbus_chan_sched() to process such bits. If bits
509 * are still set after this operation and VMBus is connected, fail the
510 * CPU offlining operation.
b635ccc1
APM
511 */
512 if (vmbus_proto_version >= VERSION_WIN10_V4_1 && hv_synic_event_pending())
523b9408
VK
513 return -EBUSY;
514
b635ccc1 515always_cleanup:
4df4cb9e 516 hv_stimer_legacy_cleanup(cpu);
e086748c 517
dba61cda 518 hv_synic_disable_regs(cpu);
76d36ab7 519
3a7f7785 520 return ret;
3e7ee490 521}