2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/hyperv.h>
29 #include <linux/version.h>
30 #include <linux/interrupt.h>
31 #include <linux/clockchips.h>
32 #include <asm/hyperv.h>
33 #include <asm/mshyperv.h>
34 #include "hyperv_vmbus.h"
36 /* The one and only */
37 struct hv_context hv_context
= {
38 .synic_initialized
= false,
41 #define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */
42 #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
43 #define HV_MIN_DELTA_TICKS 1
46 * query_hypervisor_info - Get version info of the windows hypervisor
48 unsigned int host_info_eax
;
49 unsigned int host_info_ebx
;
50 unsigned int host_info_ecx
;
51 unsigned int host_info_edx
;
53 static int query_hypervisor_info(void)
59 unsigned int max_leaf
;
63 * Its assumed that this is called after confirming that Viridian
64 * is present. Query id and revision.
70 op
= HVCPUID_VENDOR_MAXFUNCTION
;
71 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
75 if (max_leaf
>= HVCPUID_VERSION
) {
81 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
91 * hv_init - Main initialization routine.
93 * This routine must be called before any other routines in here are called
98 union hv_x64_msr_hypercall_contents hypercall_msr
;
100 memset(hv_context
.synic_event_page
, 0, sizeof(void *) * NR_CPUS
);
101 memset(hv_context
.synic_message_page
, 0,
102 sizeof(void *) * NR_CPUS
);
103 memset(hv_context
.post_msg_page
, 0,
104 sizeof(void *) * NR_CPUS
);
105 memset(hv_context
.vp_index
, 0,
106 sizeof(int) * NR_CPUS
);
107 memset(hv_context
.event_dpc
, 0,
108 sizeof(void *) * NR_CPUS
);
109 memset(hv_context
.msg_dpc
, 0,
110 sizeof(void *) * NR_CPUS
);
111 memset(hv_context
.clk_evt
, 0,
112 sizeof(void *) * NR_CPUS
);
114 max_leaf
= query_hypervisor_info();
117 /* See if the hypercall page is already set */
118 hypercall_msr
.as_uint64
= 0;
119 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
121 if (!hypercall_msr
.enable
)
128 * hv_cleanup - Cleanup routine.
130 * This routine is called normally during driver unloading or exiting.
132 void hv_cleanup(bool crash
)
138 * hv_post_message - Post a message using the hypervisor message IPC.
140 * This involves a hypercall.
142 int hv_post_message(union hv_connection_id connection_id
,
143 enum hv_message_type message_type
,
144 void *payload
, size_t payload_size
)
147 struct hv_input_post_message
*aligned_msg
;
150 if (payload_size
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
153 aligned_msg
= (struct hv_input_post_message
*)
154 hv_context
.post_msg_page
[get_cpu()];
156 aligned_msg
->connectionid
= connection_id
;
157 aligned_msg
->reserved
= 0;
158 aligned_msg
->message_type
= message_type
;
159 aligned_msg
->payload_size
= payload_size
;
160 memcpy((void *)aligned_msg
->payload
, payload
, payload_size
);
162 status
= hv_do_hypercall(HVCALL_POST_MESSAGE
, aligned_msg
, NULL
);
165 return status
& 0xFFFF;
168 static int hv_ce_set_next_event(unsigned long delta
,
169 struct clock_event_device
*evt
)
173 WARN_ON(!clockevent_state_oneshot(evt
));
175 rdmsrl(HV_X64_MSR_TIME_REF_COUNT
, current_tick
);
176 current_tick
+= delta
;
177 wrmsrl(HV_X64_MSR_STIMER0_COUNT
, current_tick
);
181 static int hv_ce_shutdown(struct clock_event_device
*evt
)
183 wrmsrl(HV_X64_MSR_STIMER0_COUNT
, 0);
184 wrmsrl(HV_X64_MSR_STIMER0_CONFIG
, 0);
189 static int hv_ce_set_oneshot(struct clock_event_device
*evt
)
191 union hv_timer_config timer_cfg
;
193 timer_cfg
.enable
= 1;
194 timer_cfg
.auto_enable
= 1;
195 timer_cfg
.sintx
= VMBUS_MESSAGE_SINT
;
196 wrmsrl(HV_X64_MSR_STIMER0_CONFIG
, timer_cfg
.as_uint64
);
201 static void hv_init_clockevent_device(struct clock_event_device
*dev
, int cpu
)
203 dev
->name
= "Hyper-V clockevent";
204 dev
->features
= CLOCK_EVT_FEAT_ONESHOT
;
205 dev
->cpumask
= cpumask_of(cpu
);
208 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
209 * result in clockevents_config_and_register() taking additional
210 * references to the hv_vmbus module making it impossible to unload.
213 dev
->set_state_shutdown
= hv_ce_shutdown
;
214 dev
->set_state_oneshot
= hv_ce_set_oneshot
;
215 dev
->set_next_event
= hv_ce_set_next_event
;
219 int hv_synic_alloc(void)
221 size_t size
= sizeof(struct tasklet_struct
);
222 size_t ced_size
= sizeof(struct clock_event_device
);
225 hv_context
.hv_numa_map
= kzalloc(sizeof(struct cpumask
) * nr_node_ids
,
227 if (hv_context
.hv_numa_map
== NULL
) {
228 pr_err("Unable to allocate NUMA map\n");
232 for_each_present_cpu(cpu
) {
233 hv_context
.event_dpc
[cpu
] = kmalloc(size
, GFP_ATOMIC
);
234 if (hv_context
.event_dpc
[cpu
] == NULL
) {
235 pr_err("Unable to allocate event dpc\n");
238 tasklet_init(hv_context
.event_dpc
[cpu
], vmbus_on_event
, cpu
);
240 hv_context
.msg_dpc
[cpu
] = kmalloc(size
, GFP_ATOMIC
);
241 if (hv_context
.msg_dpc
[cpu
] == NULL
) {
242 pr_err("Unable to allocate event dpc\n");
245 tasklet_init(hv_context
.msg_dpc
[cpu
], vmbus_on_msg_dpc
, cpu
);
247 hv_context
.clk_evt
[cpu
] = kzalloc(ced_size
, GFP_ATOMIC
);
248 if (hv_context
.clk_evt
[cpu
] == NULL
) {
249 pr_err("Unable to allocate clock event device\n");
253 hv_init_clockevent_device(hv_context
.clk_evt
[cpu
], cpu
);
255 hv_context
.synic_message_page
[cpu
] =
256 (void *)get_zeroed_page(GFP_ATOMIC
);
258 if (hv_context
.synic_message_page
[cpu
] == NULL
) {
259 pr_err("Unable to allocate SYNIC message page\n");
263 hv_context
.synic_event_page
[cpu
] =
264 (void *)get_zeroed_page(GFP_ATOMIC
);
266 if (hv_context
.synic_event_page
[cpu
] == NULL
) {
267 pr_err("Unable to allocate SYNIC event page\n");
271 hv_context
.post_msg_page
[cpu
] =
272 (void *)get_zeroed_page(GFP_ATOMIC
);
274 if (hv_context
.post_msg_page
[cpu
] == NULL
) {
275 pr_err("Unable to allocate post msg page\n");
279 INIT_LIST_HEAD(&hv_context
.percpu_list
[cpu
]);
287 static void hv_synic_free_cpu(int cpu
)
289 kfree(hv_context
.event_dpc
[cpu
]);
290 kfree(hv_context
.msg_dpc
[cpu
]);
291 kfree(hv_context
.clk_evt
[cpu
]);
292 if (hv_context
.synic_event_page
[cpu
])
293 free_page((unsigned long)hv_context
.synic_event_page
[cpu
]);
294 if (hv_context
.synic_message_page
[cpu
])
295 free_page((unsigned long)hv_context
.synic_message_page
[cpu
]);
296 if (hv_context
.post_msg_page
[cpu
])
297 free_page((unsigned long)hv_context
.post_msg_page
[cpu
]);
300 void hv_synic_free(void)
304 kfree(hv_context
.hv_numa_map
);
305 for_each_present_cpu(cpu
)
306 hv_synic_free_cpu(cpu
);
310 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
312 * If it is already initialized by another entity (ie x2v shim), we need to
313 * retrieve the initialized message and event pages. Otherwise, we create and
314 * initialize the message and event pages.
316 int hv_synic_init(unsigned int cpu
)
319 union hv_synic_simp simp
;
320 union hv_synic_siefp siefp
;
321 union hv_synic_sint shared_sint
;
322 union hv_synic_scontrol sctrl
;
325 /* Check the version */
326 rdmsrl(HV_X64_MSR_SVERSION
, version
);
328 /* Setup the Synic's message page */
329 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
330 simp
.simp_enabled
= 1;
331 simp
.base_simp_gpa
= virt_to_phys(hv_context
.synic_message_page
[cpu
])
334 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
336 /* Setup the Synic's event page */
337 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
338 siefp
.siefp_enabled
= 1;
339 siefp
.base_siefp_gpa
= virt_to_phys(hv_context
.synic_event_page
[cpu
])
342 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
344 /* Setup the shared SINT. */
345 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
347 shared_sint
.as_uint64
= 0;
348 shared_sint
.vector
= HYPERVISOR_CALLBACK_VECTOR
;
349 shared_sint
.masked
= false;
350 shared_sint
.auto_eoi
= true;
352 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
354 /* Enable the global synic bit */
355 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
358 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
360 hv_context
.synic_initialized
= true;
363 * Setup the mapping between Hyper-V's notion
364 * of cpuid and Linux' notion of cpuid.
365 * This array will be indexed using Linux cpuid.
367 rdmsrl(HV_X64_MSR_VP_INDEX
, vp_index
);
368 hv_context
.vp_index
[cpu
] = (u32
)vp_index
;
371 * Register the per-cpu clockevent source.
373 if (ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
)
374 clockevents_config_and_register(hv_context
.clk_evt
[cpu
],
377 HV_MAX_MAX_DELTA_TICKS
);
382 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
384 void hv_synic_clockevents_cleanup(void)
388 if (!(ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
))
391 for_each_present_cpu(cpu
)
392 clockevents_unbind_device(hv_context
.clk_evt
[cpu
], cpu
);
396 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
398 int hv_synic_cleanup(unsigned int cpu
)
400 union hv_synic_sint shared_sint
;
401 union hv_synic_simp simp
;
402 union hv_synic_siefp siefp
;
403 union hv_synic_scontrol sctrl
;
404 struct vmbus_channel
*channel
, *sc
;
405 bool channel_found
= false;
408 if (!hv_context
.synic_initialized
)
412 * Search for channels which are bound to the CPU we're about to
413 * cleanup. In case we find one and vmbus is still connected we need to
414 * fail, this will effectively prevent CPU offlining. There is no way
415 * we can re-bind channels to different CPUs for now.
417 mutex_lock(&vmbus_connection
.channel_mutex
);
418 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
419 if (channel
->target_cpu
== cpu
) {
420 channel_found
= true;
423 spin_lock_irqsave(&channel
->lock
, flags
);
424 list_for_each_entry(sc
, &channel
->sc_list
, sc_list
) {
425 if (sc
->target_cpu
== cpu
) {
426 channel_found
= true;
430 spin_unlock_irqrestore(&channel
->lock
, flags
);
434 mutex_unlock(&vmbus_connection
.channel_mutex
);
436 if (channel_found
&& vmbus_connection
.conn_state
== CONNECTED
)
439 /* Turn off clockevent device */
440 if (ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
) {
441 clockevents_unbind_device(hv_context
.clk_evt
[cpu
], cpu
);
442 hv_ce_shutdown(hv_context
.clk_evt
[cpu
]);
445 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
447 shared_sint
.masked
= 1;
449 /* Need to correctly cleanup in the case of SMP!!! */
450 /* Disable the interrupt */
451 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
453 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
454 simp
.simp_enabled
= 0;
455 simp
.base_simp_gpa
= 0;
457 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
459 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
460 siefp
.siefp_enabled
= 0;
461 siefp
.base_siefp_gpa
= 0;
463 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
465 /* Disable the global synic bit */
466 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
468 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);