]> git.ipfire.org Git - thirdparty/linux.git/blob - arch/x86/hyperv/hv_init.c
io_uring: reset -EBUSY error when io sq thread is waken up
[thirdparty/linux.git] / arch / x86 / hyperv / hv_init.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * X86 specific Hyper-V initialization code.
4 *
5 * Copyright (C) 2016, Microsoft, Inc.
6 *
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
8 */
9
10 #include <linux/acpi.h>
11 #include <linux/efi.h>
12 #include <linux/types.h>
13 #include <asm/apic.h>
14 #include <asm/desc.h>
15 #include <asm/hypervisor.h>
16 #include <asm/hyperv-tlfs.h>
17 #include <asm/mshyperv.h>
18 #include <linux/version.h>
19 #include <linux/vmalloc.h>
20 #include <linux/mm.h>
21 #include <linux/hyperv.h>
22 #include <linux/slab.h>
23 #include <linux/kernel.h>
24 #include <linux/cpuhotplug.h>
25 #include <linux/syscore_ops.h>
26 #include <clocksource/hyperv_timer.h>
27
28 void *hv_hypercall_pg;
29 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
30
31 /* Storage to save the hypercall page temporarily for hibernation */
32 static void *hv_hypercall_pg_saved;
33
34 u32 *hv_vp_index;
35 EXPORT_SYMBOL_GPL(hv_vp_index);
36
37 struct hv_vp_assist_page **hv_vp_assist_page;
38 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
39
40 void __percpu **hyperv_pcpu_input_arg;
41 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
42
43 u32 hv_max_vp_index;
44 EXPORT_SYMBOL_GPL(hv_max_vp_index);
45
46 void *hv_alloc_hyperv_page(void)
47 {
48 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
49
50 return (void *)__get_free_page(GFP_KERNEL);
51 }
52 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
53
54 void *hv_alloc_hyperv_zeroed_page(void)
55 {
56 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
57
58 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
59 }
60 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
61
62 void hv_free_hyperv_page(unsigned long addr)
63 {
64 free_page(addr);
65 }
66 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
67
68 static int hv_cpu_init(unsigned int cpu)
69 {
70 u64 msr_vp_index;
71 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
72 void **input_arg;
73 struct page *pg;
74
75 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
76 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
77 pg = alloc_page(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
78 if (unlikely(!pg))
79 return -ENOMEM;
80 *input_arg = page_address(pg);
81
82 hv_get_vp_index(msr_vp_index);
83
84 hv_vp_index[smp_processor_id()] = msr_vp_index;
85
86 if (msr_vp_index > hv_max_vp_index)
87 hv_max_vp_index = msr_vp_index;
88
89 if (!hv_vp_assist_page)
90 return 0;
91
92 /*
93 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
94 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
95 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
96 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
97 * not be stopped in the case of CPU offlining and the VM will hang.
98 */
99 if (!*hvp) {
100 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
101 PAGE_KERNEL);
102 }
103
104 if (*hvp) {
105 u64 val;
106
107 val = vmalloc_to_pfn(*hvp);
108 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
109 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
110
111 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
112 }
113
114 return 0;
115 }
116
117 static void (*hv_reenlightenment_cb)(void);
118
119 static void hv_reenlightenment_notify(struct work_struct *dummy)
120 {
121 struct hv_tsc_emulation_status emu_status;
122
123 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
124
125 /* Don't issue the callback if TSC accesses are not emulated */
126 if (hv_reenlightenment_cb && emu_status.inprogress)
127 hv_reenlightenment_cb();
128 }
129 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
130
131 void hyperv_stop_tsc_emulation(void)
132 {
133 u64 freq;
134 struct hv_tsc_emulation_status emu_status;
135
136 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
137 emu_status.inprogress = 0;
138 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
139
140 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
141 tsc_khz = div64_u64(freq, 1000);
142 }
143 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
144
145 static inline bool hv_reenlightenment_available(void)
146 {
147 /*
148 * Check for required features and priviliges to make TSC frequency
149 * change notifications work.
150 */
151 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
152 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
153 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
154 }
155
156 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
157 {
158 entering_ack_irq();
159
160 inc_irq_stat(irq_hv_reenlightenment_count);
161
162 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
163
164 exiting_irq();
165 }
166
167 void set_hv_tscchange_cb(void (*cb)(void))
168 {
169 struct hv_reenlightenment_control re_ctrl = {
170 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
171 .enabled = 1,
172 .target_vp = hv_vp_index[smp_processor_id()]
173 };
174 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
175
176 if (!hv_reenlightenment_available()) {
177 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
178 return;
179 }
180
181 hv_reenlightenment_cb = cb;
182
183 /* Make sure callback is registered before we write to MSRs */
184 wmb();
185
186 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
187 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
188 }
189 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
190
191 void clear_hv_tscchange_cb(void)
192 {
193 struct hv_reenlightenment_control re_ctrl;
194
195 if (!hv_reenlightenment_available())
196 return;
197
198 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
199 re_ctrl.enabled = 0;
200 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
201
202 hv_reenlightenment_cb = NULL;
203 }
204 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
205
206 static int hv_cpu_die(unsigned int cpu)
207 {
208 struct hv_reenlightenment_control re_ctrl;
209 unsigned int new_cpu;
210 unsigned long flags;
211 void **input_arg;
212 void *input_pg = NULL;
213
214 local_irq_save(flags);
215 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
216 input_pg = *input_arg;
217 *input_arg = NULL;
218 local_irq_restore(flags);
219 free_page((unsigned long)input_pg);
220
221 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
222 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
223
224 if (hv_reenlightenment_cb == NULL)
225 return 0;
226
227 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
228 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
229 /* Reassign to some other online CPU */
230 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
231
232 re_ctrl.target_vp = hv_vp_index[new_cpu];
233 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
234 }
235
236 return 0;
237 }
238
239 static int __init hv_pci_init(void)
240 {
241 int gen2vm = efi_enabled(EFI_BOOT);
242
243 /*
244 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
245 * The purpose is to suppress the harmless warning:
246 * "PCI: Fatal: No config space access function found"
247 */
248 if (gen2vm)
249 return 0;
250
251 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
252 return 1;
253 }
254
255 static int hv_suspend(void)
256 {
257 union hv_x64_msr_hypercall_contents hypercall_msr;
258 int ret;
259
260 /*
261 * Reset the hypercall page as it is going to be invalidated
262 * accross hibernation. Setting hv_hypercall_pg to NULL ensures
263 * that any subsequent hypercall operation fails safely instead of
264 * crashing due to an access of an invalid page. The hypercall page
265 * pointer is restored on resume.
266 */
267 hv_hypercall_pg_saved = hv_hypercall_pg;
268 hv_hypercall_pg = NULL;
269
270 /* Disable the hypercall page in the hypervisor */
271 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
272 hypercall_msr.enable = 0;
273 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
274
275 ret = hv_cpu_die(0);
276 return ret;
277 }
278
279 static void hv_resume(void)
280 {
281 union hv_x64_msr_hypercall_contents hypercall_msr;
282 int ret;
283
284 ret = hv_cpu_init(0);
285 WARN_ON(ret);
286
287 /* Re-enable the hypercall page */
288 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
289 hypercall_msr.enable = 1;
290 hypercall_msr.guest_physical_address =
291 vmalloc_to_pfn(hv_hypercall_pg_saved);
292 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
293
294 hv_hypercall_pg = hv_hypercall_pg_saved;
295 hv_hypercall_pg_saved = NULL;
296 }
297
298 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
299 static struct syscore_ops hv_syscore_ops = {
300 .suspend = hv_suspend,
301 .resume = hv_resume,
302 };
303
304 /*
305 * This function is to be invoked early in the boot sequence after the
306 * hypervisor has been detected.
307 *
308 * 1. Setup the hypercall page.
309 * 2. Register Hyper-V specific clocksource.
310 * 3. Setup Hyper-V specific APIC entry points.
311 */
312 void __init hyperv_init(void)
313 {
314 u64 guest_id, required_msrs;
315 union hv_x64_msr_hypercall_contents hypercall_msr;
316 int cpuhp, i;
317
318 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
319 return;
320
321 /* Absolutely required MSRs */
322 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
323 HV_X64_MSR_VP_INDEX_AVAILABLE;
324
325 if ((ms_hyperv.features & required_msrs) != required_msrs)
326 return;
327
328 /*
329 * Allocate the per-CPU state for the hypercall input arg.
330 * If this allocation fails, we will not be able to setup
331 * (per-CPU) hypercall input page and thus this failure is
332 * fatal on Hyper-V.
333 */
334 hyperv_pcpu_input_arg = alloc_percpu(void *);
335
336 BUG_ON(hyperv_pcpu_input_arg == NULL);
337
338 /* Allocate percpu VP index */
339 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
340 GFP_KERNEL);
341 if (!hv_vp_index)
342 return;
343
344 for (i = 0; i < num_possible_cpus(); i++)
345 hv_vp_index[i] = VP_INVAL;
346
347 hv_vp_assist_page = kcalloc(num_possible_cpus(),
348 sizeof(*hv_vp_assist_page), GFP_KERNEL);
349 if (!hv_vp_assist_page) {
350 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
351 goto free_vp_index;
352 }
353
354 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
355 hv_cpu_init, hv_cpu_die);
356 if (cpuhp < 0)
357 goto free_vp_assist_page;
358
359 /*
360 * Setup the hypercall page and enable hypercalls.
361 * 1. Register the guest ID
362 * 2. Enable the hypercall and register the hypercall page
363 */
364 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
365 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
366
367 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
368 if (hv_hypercall_pg == NULL) {
369 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
370 goto remove_cpuhp_state;
371 }
372
373 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
374 hypercall_msr.enable = 1;
375 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
376 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
377
378 /*
379 * Ignore any errors in setting up stimer clockevents
380 * as we can run with the LAPIC timer as a fallback.
381 */
382 (void)hv_stimer_alloc();
383
384 hv_apic_init();
385
386 x86_init.pci.arch_init = hv_pci_init;
387
388 register_syscore_ops(&hv_syscore_ops);
389
390 return;
391
392 remove_cpuhp_state:
393 cpuhp_remove_state(cpuhp);
394 free_vp_assist_page:
395 kfree(hv_vp_assist_page);
396 hv_vp_assist_page = NULL;
397 free_vp_index:
398 kfree(hv_vp_index);
399 hv_vp_index = NULL;
400 }
401
402 /*
403 * This routine is called before kexec/kdump, it does the required cleanup.
404 */
405 void hyperv_cleanup(void)
406 {
407 union hv_x64_msr_hypercall_contents hypercall_msr;
408
409 unregister_syscore_ops(&hv_syscore_ops);
410
411 /* Reset our OS id */
412 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
413
414 /*
415 * Reset hypercall page reference before reset the page,
416 * let hypercall operations fail safely rather than
417 * panic the kernel for using invalid hypercall page
418 */
419 hv_hypercall_pg = NULL;
420
421 /* Reset the hypercall page */
422 hypercall_msr.as_uint64 = 0;
423 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
424
425 /* Reset the TSC page */
426 hypercall_msr.as_uint64 = 0;
427 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
428 }
429 EXPORT_SYMBOL_GPL(hyperv_cleanup);
430
431 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
432 {
433 static bool panic_reported;
434 u64 guest_id;
435
436 if (in_die && !panic_on_oops)
437 return;
438
439 /*
440 * We prefer to report panic on 'die' chain as we have proper
441 * registers to report, but if we miss it (e.g. on BUG()) we need
442 * to report it on 'panic'.
443 */
444 if (panic_reported)
445 return;
446 panic_reported = true;
447
448 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
449
450 wrmsrl(HV_X64_MSR_CRASH_P0, err);
451 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
452 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
453 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
454 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
455
456 /*
457 * Let Hyper-V know there is crash data available
458 */
459 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
460 }
461 EXPORT_SYMBOL_GPL(hyperv_report_panic);
462
463 /**
464 * hyperv_report_panic_msg - report panic message to Hyper-V
465 * @pa: physical address of the panic page containing the message
466 * @size: size of the message in the page
467 */
468 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
469 {
470 /*
471 * P3 to contain the physical address of the panic page & P4 to
472 * contain the size of the panic data in that page. Rest of the
473 * registers are no-op when the NOTIFY_MSG flag is set.
474 */
475 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
476 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
477 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
478 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
479 wrmsrl(HV_X64_MSR_CRASH_P4, size);
480
481 /*
482 * Let Hyper-V know there is crash data available along with
483 * the panic message.
484 */
485 wrmsrl(HV_X64_MSR_CRASH_CTL,
486 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
487 }
488 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
489
490 bool hv_is_hyperv_initialized(void)
491 {
492 union hv_x64_msr_hypercall_contents hypercall_msr;
493
494 /*
495 * Ensure that we're really on Hyper-V, and not a KVM or Xen
496 * emulation of Hyper-V
497 */
498 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
499 return false;
500
501 /*
502 * Verify that earlier initialization succeeded by checking
503 * that the hypercall page is setup
504 */
505 hypercall_msr.as_uint64 = 0;
506 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
507
508 return hypercall_msr.enable;
509 }
510 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
511
512 bool hv_is_hibernation_supported(void)
513 {
514 return acpi_sleep_state_supported(ACPI_STATE_S4);
515 }
516 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);