]>
Commit | Line | Data |
---|---|---|
3e7ee490 | 1 | /* |
3e7ee490 HJ |
2 | * Copyright (c) 2009, Microsoft Corporation. |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
20 | * | |
21 | */ | |
0a46618d HJ |
22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
23 | ||
a0086dc5 GKH |
24 | #include <linux/kernel.h> |
25 | #include <linux/mm.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
b7c947f0 | 27 | #include <linux/vmalloc.h> |
46a97191 | 28 | #include <linux/hyperv.h> |
407dd164 | 29 | #include <asm/hyperv.h> |
0f2a6619 | 30 | #include "hyperv_vmbus.h" |
3e7ee490 | 31 | |
454f18a9 | 32 | /* The one and only */ |
6a0aaa18 HZ |
33 | struct hv_context hv_context = { |
34 | .synic_initialized = false, | |
35 | .hypercall_page = NULL, | |
36 | .signal_event_param = NULL, | |
37 | .signal_event_buffer = NULL, | |
3e7ee490 HJ |
38 | }; |
39 | ||
3e189519 | 40 | /* |
d44890c8 | 41 | * query_hypervisor_presence |
25985edc | 42 | * - Query the cpuid for presence of windows hypervisor |
0831ad04 | 43 | */ |
d44890c8 | 44 | static int query_hypervisor_presence(void) |
3e7ee490 | 45 | { |
0831ad04 GKH |
46 | unsigned int eax; |
47 | unsigned int ebx; | |
48 | unsigned int ecx; | |
49 | unsigned int edx; | |
50 | unsigned int op; | |
3e7ee490 | 51 | |
454f18a9 BP |
52 | eax = 0; |
53 | ebx = 0; | |
54 | ecx = 0; | |
55 | edx = 0; | |
f6feebe0 | 56 | op = HVCPUID_VERSION_FEATURES; |
f931a70c | 57 | cpuid(op, &eax, &ebx, &ecx, &edx); |
0831ad04 GKH |
58 | |
59 | return ecx & HV_PRESENT_BIT; | |
3e7ee490 HJ |
60 | } |
61 | ||
3e189519 | 62 | /* |
d44890c8 | 63 | * query_hypervisor_info - Get version info of the windows hypervisor |
0831ad04 | 64 | */ |
d44890c8 | 65 | static int query_hypervisor_info(void) |
0831ad04 GKH |
66 | { |
67 | unsigned int eax; | |
68 | unsigned int ebx; | |
69 | unsigned int ecx; | |
70 | unsigned int edx; | |
b8dfb264 | 71 | unsigned int max_leaf; |
0831ad04 | 72 | unsigned int op; |
3e7ee490 | 73 | |
0831ad04 GKH |
74 | /* |
75 | * Its assumed that this is called after confirming that Viridian | |
76 | * is present. Query id and revision. | |
77 | */ | |
78 | eax = 0; | |
79 | ebx = 0; | |
80 | ecx = 0; | |
81 | edx = 0; | |
f6feebe0 | 82 | op = HVCPUID_VENDOR_MAXFUNCTION; |
0831ad04 | 83 | cpuid(op, &eax, &ebx, &ecx, &edx); |
3e7ee490 | 84 | |
b8dfb264 | 85 | max_leaf = eax; |
0831ad04 | 86 | |
b8dfb264 | 87 | if (max_leaf >= HVCPUID_VERSION) { |
0831ad04 GKH |
88 | eax = 0; |
89 | ebx = 0; | |
90 | ecx = 0; | |
91 | edx = 0; | |
f6feebe0 | 92 | op = HVCPUID_VERSION; |
0831ad04 | 93 | cpuid(op, &eax, &ebx, &ecx, &edx); |
0a46618d | 94 | pr_info("Hyper-V Host OS Build:%d-%d.%d-%d-%d.%d\n", |
0831ad04 GKH |
95 | eax, |
96 | ebx >> 16, | |
97 | ebx & 0xFFFF, | |
98 | ecx, | |
99 | edx >> 24, | |
100 | edx & 0xFFFFFF); | |
101 | } | |
b8dfb264 | 102 | return max_leaf; |
0831ad04 | 103 | } |
3e7ee490 | 104 | |
3e189519 | 105 | /* |
d44890c8 | 106 | * do_hypercall- Invoke the specified hypercall |
0831ad04 | 107 | */ |
d44890c8 | 108 | static u64 do_hypercall(u64 control, void *input, void *output) |
3e7ee490 | 109 | { |
530cf207 | 110 | #ifdef CONFIG_X86_64 |
b8dfb264 HZ |
111 | u64 hv_status = 0; |
112 | u64 input_address = (input) ? virt_to_phys(input) : 0; | |
113 | u64 output_address = (output) ? virt_to_phys(output) : 0; | |
dec317fd | 114 | void *hypercall_page = hv_context.hypercall_page; |
3e7ee490 | 115 | |
b8dfb264 HZ |
116 | __asm__ __volatile__("mov %0, %%r8" : : "r" (output_address) : "r8"); |
117 | __asm__ __volatile__("call *%3" : "=a" (hv_status) : | |
118 | "c" (control), "d" (input_address), | |
119 | "m" (hypercall_page)); | |
3e7ee490 | 120 | |
b8dfb264 | 121 | return hv_status; |
3e7ee490 HJ |
122 | |
123 | #else | |
124 | ||
b8dfb264 HZ |
125 | u32 control_hi = control >> 32; |
126 | u32 control_lo = control & 0xFFFFFFFF; | |
127 | u32 hv_status_hi = 1; | |
128 | u32 hv_status_lo = 1; | |
129 | u64 input_address = (input) ? virt_to_phys(input) : 0; | |
130 | u32 input_address_hi = input_address >> 32; | |
131 | u32 input_address_lo = input_address & 0xFFFFFFFF; | |
132 | u64 output_address = (output) ? virt_to_phys(output) : 0; | |
133 | u32 output_address_hi = output_address >> 32; | |
134 | u32 output_address_lo = output_address & 0xFFFFFFFF; | |
dec317fd | 135 | void *hypercall_page = hv_context.hypercall_page; |
3e7ee490 | 136 | |
b8dfb264 HZ |
137 | __asm__ __volatile__ ("call *%8" : "=d"(hv_status_hi), |
138 | "=a"(hv_status_lo) : "d" (control_hi), | |
139 | "a" (control_lo), "b" (input_address_hi), | |
140 | "c" (input_address_lo), "D"(output_address_hi), | |
141 | "S"(output_address_lo), "m" (hypercall_page)); | |
3e7ee490 | 142 | |
b8dfb264 | 143 | return hv_status_lo | ((u64)hv_status_hi << 32); |
0831ad04 | 144 | #endif /* !x86_64 */ |
3e7ee490 HJ |
145 | } |
146 | ||
3e189519 | 147 | /* |
d44890c8 | 148 | * hv_init - Main initialization routine. |
0831ad04 GKH |
149 | * |
150 | * This routine must be called before any other routines in here are called | |
151 | */ | |
d44890c8 | 152 | int hv_init(void) |
3e7ee490 | 153 | { |
b8dfb264 HZ |
154 | int max_leaf; |
155 | union hv_x64_msr_hypercall_contents hypercall_msr; | |
156 | void *virtaddr = NULL; | |
3e7ee490 | 157 | |
6a0aaa18 HZ |
158 | memset(hv_context.synic_event_page, 0, sizeof(void *) * MAX_NUM_CPUS); |
159 | memset(hv_context.synic_message_page, 0, | |
160 | sizeof(void *) * MAX_NUM_CPUS); | |
3e7ee490 | 161 | |
98e08702 | 162 | if (!query_hypervisor_presence()) |
44939d37 | 163 | goto cleanup; |
3e7ee490 | 164 | |
d44890c8 | 165 | max_leaf = query_hypervisor_info(); |
3e7ee490 | 166 | |
a73e6b7c HJ |
167 | /* Write our OS info */ |
168 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID); | |
6a0aaa18 | 169 | hv_context.guestid = HV_LINUX_GUEST_ID; |
a73e6b7c | 170 | |
454f18a9 | 171 | /* See if the hypercall page is already set */ |
b8dfb264 | 172 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
3e7ee490 | 173 | |
df3493e0 | 174 | virtaddr = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_EXEC); |
3e7ee490 | 175 | |
98e08702 | 176 | if (!virtaddr) |
44939d37 | 177 | goto cleanup; |
3e7ee490 | 178 | |
b8dfb264 | 179 | hypercall_msr.enable = 1; |
a73e6b7c | 180 | |
b8dfb264 HZ |
181 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(virtaddr); |
182 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); | |
a73e6b7c HJ |
183 | |
184 | /* Confirm that hypercall page did get setup. */ | |
b8dfb264 HZ |
185 | hypercall_msr.as_uint64 = 0; |
186 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); | |
a73e6b7c | 187 | |
98e08702 | 188 | if (!hypercall_msr.enable) |
44939d37 | 189 | goto cleanup; |
3e7ee490 | 190 | |
b8dfb264 | 191 | hv_context.hypercall_page = virtaddr; |
a73e6b7c | 192 | |
454f18a9 | 193 | /* Setup the global signal event param for the signal event hypercall */ |
6a0aaa18 | 194 | hv_context.signal_event_buffer = |
0831ad04 GKH |
195 | kmalloc(sizeof(struct hv_input_signal_event_buffer), |
196 | GFP_KERNEL); | |
6a0aaa18 | 197 | if (!hv_context.signal_event_buffer) |
44939d37 | 198 | goto cleanup; |
3e7ee490 | 199 | |
6a0aaa18 | 200 | hv_context.signal_event_param = |
0831ad04 | 201 | (struct hv_input_signal_event *) |
73509681 | 202 | (ALIGN((unsigned long) |
6a0aaa18 | 203 | hv_context.signal_event_buffer, |
0831ad04 | 204 | HV_HYPERCALL_PARAM_ALIGN)); |
6a0aaa18 HZ |
205 | hv_context.signal_event_param->connectionid.asu32 = 0; |
206 | hv_context.signal_event_param->connectionid.u.id = | |
0831ad04 | 207 | VMBUS_EVENT_CONNECTION_ID; |
6a0aaa18 HZ |
208 | hv_context.signal_event_param->flag_number = 0; |
209 | hv_context.signal_event_param->rsvdz = 0; | |
3e7ee490 | 210 | |
5433e003 | 211 | return 0; |
3e7ee490 | 212 | |
44939d37 | 213 | cleanup: |
b8dfb264 HZ |
214 | if (virtaddr) { |
215 | if (hypercall_msr.enable) { | |
216 | hypercall_msr.as_uint64 = 0; | |
217 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); | |
3e7ee490 HJ |
218 | } |
219 | ||
b8dfb264 | 220 | vfree(virtaddr); |
3e7ee490 | 221 | } |
5433e003 S |
222 | |
223 | return -ENOTSUPP; | |
3e7ee490 HJ |
224 | } |
225 | ||
3e189519 | 226 | /* |
d44890c8 | 227 | * hv_cleanup - Cleanup routine. |
0831ad04 GKH |
228 | * |
229 | * This routine is called normally during driver unloading or exiting. | |
230 | */ | |
d44890c8 | 231 | void hv_cleanup(void) |
3e7ee490 | 232 | { |
b8dfb264 | 233 | union hv_x64_msr_hypercall_contents hypercall_msr; |
3e7ee490 | 234 | |
93e5bd06 S |
235 | /* Reset our OS id */ |
236 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); | |
237 | ||
6a0aaa18 HZ |
238 | kfree(hv_context.signal_event_buffer); |
239 | hv_context.signal_event_buffer = NULL; | |
240 | hv_context.signal_event_param = NULL; | |
3e7ee490 | 241 | |
6a0aaa18 | 242 | if (hv_context.hypercall_page) { |
b8dfb264 HZ |
243 | hypercall_msr.as_uint64 = 0; |
244 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); | |
6a0aaa18 HZ |
245 | vfree(hv_context.hypercall_page); |
246 | hv_context.hypercall_page = NULL; | |
3e7ee490 | 247 | } |
3e7ee490 HJ |
248 | } |
249 | ||
3e189519 | 250 | /* |
d44890c8 | 251 | * hv_post_message - Post a message using the hypervisor message IPC. |
0831ad04 GKH |
252 | * |
253 | * This involves a hypercall. | |
254 | */ | |
d44890c8 | 255 | u16 hv_post_message(union hv_connection_id connection_id, |
b8dfb264 HZ |
256 | enum hv_message_type message_type, |
257 | void *payload, size_t payload_size) | |
3e7ee490 | 258 | { |
b8dfb264 | 259 | struct aligned_input { |
0831ad04 | 260 | u64 alignment8; |
cba4decd | 261 | struct hv_input_post_message msg; |
3e7ee490 HJ |
262 | }; |
263 | ||
b8dfb264 | 264 | struct hv_input_post_message *aligned_msg; |
034469e6 | 265 | u16 status; |
c4b0bc94 | 266 | unsigned long addr; |
3e7ee490 | 267 | |
b8dfb264 | 268 | if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) |
39594abc | 269 | return -EMSGSIZE; |
3e7ee490 | 270 | |
b8dfb264 | 271 | addr = (unsigned long)kmalloc(sizeof(struct aligned_input), GFP_ATOMIC); |
3e7ee490 | 272 | if (!addr) |
39594abc | 273 | return -ENOMEM; |
3e7ee490 | 274 | |
b8dfb264 | 275 | aligned_msg = (struct hv_input_post_message *) |
73509681 | 276 | (ALIGN(addr, HV_HYPERCALL_PARAM_ALIGN)); |
3e7ee490 | 277 | |
b8dfb264 HZ |
278 | aligned_msg->connectionid = connection_id; |
279 | aligned_msg->message_type = message_type; | |
280 | aligned_msg->payload_size = payload_size; | |
281 | memcpy((void *)aligned_msg->payload, payload, payload_size); | |
3e7ee490 | 282 | |
d44890c8 HZ |
283 | status = do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL) |
284 | & 0xFFFF; | |
3e7ee490 | 285 | |
0831ad04 | 286 | kfree((void *)addr); |
3e7ee490 HJ |
287 | |
288 | return status; | |
289 | } | |
290 | ||
291 | ||
3e189519 | 292 | /* |
d44890c8 HZ |
293 | * hv_signal_event - |
294 | * Signal an event on the specified connection using the hypervisor event IPC. | |
0831ad04 GKH |
295 | * |
296 | * This involves a hypercall. | |
297 | */ | |
d44890c8 | 298 | u16 hv_signal_event(void) |
3e7ee490 | 299 | { |
034469e6 | 300 | u16 status; |
3e7ee490 | 301 | |
d44890c8 | 302 | status = do_hypercall(HVCALL_SIGNAL_EVENT, |
6a0aaa18 | 303 | hv_context.signal_event_param, |
0831ad04 | 304 | NULL) & 0xFFFF; |
3e7ee490 HJ |
305 | return status; |
306 | } | |
307 | ||
3e189519 | 308 | /* |
d44890c8 | 309 | * hv_synic_init - Initialize the Synthethic Interrupt Controller. |
0831ad04 GKH |
310 | * |
311 | * If it is already initialized by another entity (ie x2v shim), we need to | |
312 | * retrieve the initialized message and event pages. Otherwise, we create and | |
313 | * initialize the message and event pages. | |
314 | */ | |
d44890c8 | 315 | void hv_synic_init(void *irqarg) |
3e7ee490 | 316 | { |
0831ad04 | 317 | u64 version; |
eacb1b4d GKH |
318 | union hv_synic_simp simp; |
319 | union hv_synic_siefp siefp; | |
b8dfb264 | 320 | union hv_synic_sint shared_sint; |
eacb1b4d | 321 | union hv_synic_scontrol sctrl; |
a73e6b7c | 322 | |
b8dfb264 | 323 | u32 irq_vector = *((u32 *)(irqarg)); |
7692fd4d | 324 | int cpu = smp_processor_id(); |
3e7ee490 | 325 | |
6a0aaa18 | 326 | if (!hv_context.hypercall_page) |
7692fd4d | 327 | return; |
3e7ee490 | 328 | |
454f18a9 | 329 | /* Check the version */ |
a51ed7d6 | 330 | rdmsrl(HV_X64_MSR_SVERSION, version); |
3e7ee490 | 331 | |
6a0aaa18 HZ |
332 | hv_context.synic_message_page[cpu] = |
333 | (void *)get_zeroed_page(GFP_ATOMIC); | |
3e7ee490 | 334 | |
6a0aaa18 | 335 | if (hv_context.synic_message_page[cpu] == NULL) { |
0a46618d | 336 | pr_err("Unable to allocate SYNIC message page\n"); |
44939d37 | 337 | goto cleanup; |
a73e6b7c | 338 | } |
3e7ee490 | 339 | |
6a0aaa18 HZ |
340 | hv_context.synic_event_page[cpu] = |
341 | (void *)get_zeroed_page(GFP_ATOMIC); | |
3e7ee490 | 342 | |
6a0aaa18 | 343 | if (hv_context.synic_event_page[cpu] == NULL) { |
0a46618d | 344 | pr_err("Unable to allocate SYNIC event page\n"); |
44939d37 | 345 | goto cleanup; |
a73e6b7c | 346 | } |
3e7ee490 | 347 | |
a73e6b7c | 348 | /* Setup the Synic's message page */ |
f6feebe0 HZ |
349 | rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64); |
350 | simp.simp_enabled = 1; | |
6a0aaa18 | 351 | simp.base_simp_gpa = virt_to_phys(hv_context.synic_message_page[cpu]) |
a73e6b7c | 352 | >> PAGE_SHIFT; |
3e7ee490 | 353 | |
f6feebe0 | 354 | wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64); |
3e7ee490 | 355 | |
a73e6b7c | 356 | /* Setup the Synic's event page */ |
f6feebe0 HZ |
357 | rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64); |
358 | siefp.siefp_enabled = 1; | |
6a0aaa18 | 359 | siefp.base_siefp_gpa = virt_to_phys(hv_context.synic_event_page[cpu]) |
a73e6b7c HJ |
360 | >> PAGE_SHIFT; |
361 | ||
f6feebe0 | 362 | wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64); |
0831ad04 | 363 | |
0831ad04 | 364 | /* Setup the shared SINT. */ |
b8dfb264 | 365 | rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64); |
3e7ee490 | 366 | |
b8dfb264 HZ |
367 | shared_sint.as_uint64 = 0; |
368 | shared_sint.vector = irq_vector; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */ | |
369 | shared_sint.masked = false; | |
93511baa | 370 | shared_sint.auto_eoi = false; |
3e7ee490 | 371 | |
b8dfb264 | 372 | wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64); |
3e7ee490 | 373 | |
454f18a9 | 374 | /* Enable the global synic bit */ |
f6feebe0 HZ |
375 | rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64); |
376 | sctrl.enable = 1; | |
3e7ee490 | 377 | |
f6feebe0 | 378 | wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64); |
3e7ee490 | 379 | |
6a0aaa18 | 380 | hv_context.synic_initialized = true; |
7692fd4d | 381 | return; |
3e7ee490 | 382 | |
44939d37 | 383 | cleanup: |
6a0aaa18 | 384 | if (hv_context.synic_event_page[cpu]) |
df3493e0 | 385 | free_page((unsigned long)hv_context.synic_event_page[cpu]); |
3e7ee490 | 386 | |
6a0aaa18 | 387 | if (hv_context.synic_message_page[cpu]) |
df3493e0 | 388 | free_page((unsigned long)hv_context.synic_message_page[cpu]); |
7692fd4d | 389 | return; |
3e7ee490 HJ |
390 | } |
391 | ||
3e189519 | 392 | /* |
d44890c8 | 393 | * hv_synic_cleanup - Cleanup routine for hv_synic_init(). |
0831ad04 | 394 | */ |
d44890c8 | 395 | void hv_synic_cleanup(void *arg) |
3e7ee490 | 396 | { |
b8dfb264 | 397 | union hv_synic_sint shared_sint; |
eacb1b4d GKH |
398 | union hv_synic_simp simp; |
399 | union hv_synic_siefp siefp; | |
7692fd4d | 400 | int cpu = smp_processor_id(); |
3e7ee490 | 401 | |
6a0aaa18 | 402 | if (!hv_context.synic_initialized) |
3e7ee490 | 403 | return; |
3e7ee490 | 404 | |
b8dfb264 | 405 | rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64); |
3e7ee490 | 406 | |
b8dfb264 | 407 | shared_sint.masked = 1; |
3e7ee490 | 408 | |
7692fd4d | 409 | /* Need to correctly cleanup in the case of SMP!!! */ |
454f18a9 | 410 | /* Disable the interrupt */ |
b8dfb264 | 411 | wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64); |
3e7ee490 | 412 | |
f6feebe0 HZ |
413 | rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64); |
414 | simp.simp_enabled = 0; | |
415 | simp.base_simp_gpa = 0; | |
3e7ee490 | 416 | |
f6feebe0 | 417 | wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64); |
3e7ee490 | 418 | |
f6feebe0 HZ |
419 | rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64); |
420 | siefp.siefp_enabled = 0; | |
421 | siefp.base_siefp_gpa = 0; | |
3e7ee490 | 422 | |
f6feebe0 | 423 | wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64); |
3e7ee490 | 424 | |
df3493e0 S |
425 | free_page((unsigned long)hv_context.synic_message_page[cpu]); |
426 | free_page((unsigned long)hv_context.synic_event_page[cpu]); | |
3e7ee490 | 427 | } |