xref: /openbmc/linux/arch/x86/hyperv/hv_init.c (revision 6ab42a66)
1 /*
2  * X86 specific Hyper-V initialization code.
3  *
4  * Copyright (C) 2016, Microsoft, Inc.
5  *
6  * Author : K. Y. Srinivasan <kys@microsoft.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15  * NON INFRINGEMENT.  See the GNU General Public License for more
16  * details.
17  *
18  */
19 
20 #include <linux/types.h>
21 #include <asm/hypervisor.h>
22 #include <asm/hyperv.h>
23 #include <asm/mshyperv.h>
24 #include <linux/version.h>
25 #include <linux/vmalloc.h>
26 #include <linux/mm.h>
27 
28 static void *hypercall_pg;
29 /*
30  * This function is to be invoked early in the boot sequence after the
31  * hypervisor has been detected.
32  *
33  * 1. Setup the hypercall page.
34  */
35 void hyperv_init(void)
36 {
37 	u64 guest_id;
38 	union hv_x64_msr_hypercall_contents hypercall_msr;
39 
40 	if (x86_hyper != &x86_hyper_ms_hyperv)
41 		return;
42 
43 	/*
44 	 * Setup the hypercall page and enable hypercalls.
45 	 * 1. Register the guest ID
46 	 * 2. Enable the hypercall and register the hypercall page
47 	 */
48 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
49 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
50 
51 	hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC);
52 	if (hypercall_pg == NULL) {
53 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
54 		return;
55 	}
56 
57 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
58 	hypercall_msr.enable = 1;
59 	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hypercall_pg);
60 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
61 }
62 
63 /*
64  * hv_do_hypercall- Invoke the specified hypercall
65  */
66 u64 hv_do_hypercall(u64 control, void *input, void *output)
67 {
68 	u64 input_address = (input) ? virt_to_phys(input) : 0;
69 	u64 output_address = (output) ? virt_to_phys(output) : 0;
70 #ifdef CONFIG_X86_64
71 	u64 hv_status = 0;
72 
73 	if (!hypercall_pg)
74 		return (u64)ULLONG_MAX;
75 
76 	__asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8");
77 	__asm__ __volatile__("call *%3" : "=a" (hv_status) :
78 			     "c" (control), "d" (input_address),
79 			     "m" (hypercall_pg));
80 
81 	return hv_status;
82 
83 #else
84 
85 	u32 control_hi = control >> 32;
86 	u32 control_lo = control & 0xFFFFFFFF;
87 	u32 hv_status_hi = 1;
88 	u32 hv_status_lo = 1;
89 	u32 input_address_hi = input_address >> 32;
90 	u32 input_address_lo = input_address & 0xFFFFFFFF;
91 	u32 output_address_hi = output_address >> 32;
92 	u32 output_address_lo = output_address & 0xFFFFFFFF;
93 
94 	if (!hypercall_pg)
95 		return (u64)ULLONG_MAX;
96 
97 	__asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi),
98 			      "=a"(hv_status_lo) : "d" (control_hi),
99 			      "a" (control_lo), "b" (input_address_hi),
100 			      "c" (input_address_lo), "D"(output_address_hi),
101 			      "S"(output_address_lo), "m" (hypercall_pg));
102 
103 	return hv_status_lo | ((u64)hv_status_hi << 32);
104 #endif /* !x86_64 */
105 }
106 EXPORT_SYMBOL_GPL(hv_do_hypercall);
107