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 * hv_init - Main initialization routine.
48 * This routine must be called before any other routines in here are called
52 union hv_x64_msr_hypercall_contents hypercall_msr
;
54 memset(hv_context
.synic_event_page
, 0, sizeof(void *) * NR_CPUS
);
55 memset(hv_context
.synic_message_page
, 0,
56 sizeof(void *) * NR_CPUS
);
57 memset(hv_context
.post_msg_page
, 0,
58 sizeof(void *) * NR_CPUS
);
59 memset(hv_context
.vp_index
, 0,
60 sizeof(int) * NR_CPUS
);
61 memset(hv_context
.event_dpc
, 0,
62 sizeof(void *) * NR_CPUS
);
63 memset(hv_context
.msg_dpc
, 0,
64 sizeof(void *) * NR_CPUS
);
65 memset(hv_context
.clk_evt
, 0,
66 sizeof(void *) * NR_CPUS
);
68 /* See if the hypercall page is already set */
69 hypercall_msr
.as_uint64
= 0;
70 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercall_msr
.as_uint64
);
72 if (!hypercall_msr
.enable
)
79 * hv_cleanup - Cleanup routine.
81 * This routine is called normally during driver unloading or exiting.
83 void hv_cleanup(bool crash
)
89 * hv_post_message - Post a message using the hypervisor message IPC.
91 * This involves a hypercall.
93 int hv_post_message(union hv_connection_id connection_id
,
94 enum hv_message_type message_type
,
95 void *payload
, size_t payload_size
)
98 struct hv_input_post_message
*aligned_msg
;
101 if (payload_size
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
104 aligned_msg
= (struct hv_input_post_message
*)
105 hv_context
.post_msg_page
[get_cpu()];
107 aligned_msg
->connectionid
= connection_id
;
108 aligned_msg
->reserved
= 0;
109 aligned_msg
->message_type
= message_type
;
110 aligned_msg
->payload_size
= payload_size
;
111 memcpy((void *)aligned_msg
->payload
, payload
, payload_size
);
113 status
= hv_do_hypercall(HVCALL_POST_MESSAGE
, aligned_msg
, NULL
);
116 return status
& 0xFFFF;
119 static int hv_ce_set_next_event(unsigned long delta
,
120 struct clock_event_device
*evt
)
124 WARN_ON(!clockevent_state_oneshot(evt
));
126 rdmsrl(HV_X64_MSR_TIME_REF_COUNT
, current_tick
);
127 current_tick
+= delta
;
128 wrmsrl(HV_X64_MSR_STIMER0_COUNT
, current_tick
);
132 static int hv_ce_shutdown(struct clock_event_device
*evt
)
134 wrmsrl(HV_X64_MSR_STIMER0_COUNT
, 0);
135 wrmsrl(HV_X64_MSR_STIMER0_CONFIG
, 0);
140 static int hv_ce_set_oneshot(struct clock_event_device
*evt
)
142 union hv_timer_config timer_cfg
;
144 timer_cfg
.enable
= 1;
145 timer_cfg
.auto_enable
= 1;
146 timer_cfg
.sintx
= VMBUS_MESSAGE_SINT
;
147 wrmsrl(HV_X64_MSR_STIMER0_CONFIG
, timer_cfg
.as_uint64
);
152 static void hv_init_clockevent_device(struct clock_event_device
*dev
, int cpu
)
154 dev
->name
= "Hyper-V clockevent";
155 dev
->features
= CLOCK_EVT_FEAT_ONESHOT
;
156 dev
->cpumask
= cpumask_of(cpu
);
159 * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will
160 * result in clockevents_config_and_register() taking additional
161 * references to the hv_vmbus module making it impossible to unload.
164 dev
->set_state_shutdown
= hv_ce_shutdown
;
165 dev
->set_state_oneshot
= hv_ce_set_oneshot
;
166 dev
->set_next_event
= hv_ce_set_next_event
;
170 int hv_synic_alloc(void)
172 size_t size
= sizeof(struct tasklet_struct
);
173 size_t ced_size
= sizeof(struct clock_event_device
);
176 hv_context
.hv_numa_map
= kzalloc(sizeof(struct cpumask
) * nr_node_ids
,
178 if (hv_context
.hv_numa_map
== NULL
) {
179 pr_err("Unable to allocate NUMA map\n");
183 for_each_present_cpu(cpu
) {
184 hv_context
.event_dpc
[cpu
] = kmalloc(size
, GFP_ATOMIC
);
185 if (hv_context
.event_dpc
[cpu
] == NULL
) {
186 pr_err("Unable to allocate event dpc\n");
189 tasklet_init(hv_context
.event_dpc
[cpu
], vmbus_on_event
, cpu
);
191 hv_context
.msg_dpc
[cpu
] = kmalloc(size
, GFP_ATOMIC
);
192 if (hv_context
.msg_dpc
[cpu
] == NULL
) {
193 pr_err("Unable to allocate event dpc\n");
196 tasklet_init(hv_context
.msg_dpc
[cpu
], vmbus_on_msg_dpc
, cpu
);
198 hv_context
.clk_evt
[cpu
] = kzalloc(ced_size
, GFP_ATOMIC
);
199 if (hv_context
.clk_evt
[cpu
] == NULL
) {
200 pr_err("Unable to allocate clock event device\n");
204 hv_init_clockevent_device(hv_context
.clk_evt
[cpu
], cpu
);
206 hv_context
.synic_message_page
[cpu
] =
207 (void *)get_zeroed_page(GFP_ATOMIC
);
209 if (hv_context
.synic_message_page
[cpu
] == NULL
) {
210 pr_err("Unable to allocate SYNIC message page\n");
214 hv_context
.synic_event_page
[cpu
] =
215 (void *)get_zeroed_page(GFP_ATOMIC
);
217 if (hv_context
.synic_event_page
[cpu
] == NULL
) {
218 pr_err("Unable to allocate SYNIC event page\n");
222 hv_context
.post_msg_page
[cpu
] =
223 (void *)get_zeroed_page(GFP_ATOMIC
);
225 if (hv_context
.post_msg_page
[cpu
] == NULL
) {
226 pr_err("Unable to allocate post msg page\n");
230 INIT_LIST_HEAD(&hv_context
.percpu_list
[cpu
]);
238 static void hv_synic_free_cpu(int cpu
)
240 kfree(hv_context
.event_dpc
[cpu
]);
241 kfree(hv_context
.msg_dpc
[cpu
]);
242 kfree(hv_context
.clk_evt
[cpu
]);
243 if (hv_context
.synic_event_page
[cpu
])
244 free_page((unsigned long)hv_context
.synic_event_page
[cpu
]);
245 if (hv_context
.synic_message_page
[cpu
])
246 free_page((unsigned long)hv_context
.synic_message_page
[cpu
]);
247 if (hv_context
.post_msg_page
[cpu
])
248 free_page((unsigned long)hv_context
.post_msg_page
[cpu
]);
251 void hv_synic_free(void)
255 kfree(hv_context
.hv_numa_map
);
256 for_each_present_cpu(cpu
)
257 hv_synic_free_cpu(cpu
);
261 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
263 * If it is already initialized by another entity (ie x2v shim), we need to
264 * retrieve the initialized message and event pages. Otherwise, we create and
265 * initialize the message and event pages.
267 int hv_synic_init(unsigned int cpu
)
270 union hv_synic_simp simp
;
271 union hv_synic_siefp siefp
;
272 union hv_synic_sint shared_sint
;
273 union hv_synic_scontrol sctrl
;
276 /* Check the version */
277 rdmsrl(HV_X64_MSR_SVERSION
, version
);
279 /* Setup the Synic's message page */
280 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
281 simp
.simp_enabled
= 1;
282 simp
.base_simp_gpa
= virt_to_phys(hv_context
.synic_message_page
[cpu
])
285 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
287 /* Setup the Synic's event page */
288 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
289 siefp
.siefp_enabled
= 1;
290 siefp
.base_siefp_gpa
= virt_to_phys(hv_context
.synic_event_page
[cpu
])
293 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
295 /* Setup the shared SINT. */
296 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
298 shared_sint
.as_uint64
= 0;
299 shared_sint
.vector
= HYPERVISOR_CALLBACK_VECTOR
;
300 shared_sint
.masked
= false;
301 shared_sint
.auto_eoi
= true;
303 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
305 /* Enable the global synic bit */
306 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
309 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
311 hv_context
.synic_initialized
= true;
314 * Setup the mapping between Hyper-V's notion
315 * of cpuid and Linux' notion of cpuid.
316 * This array will be indexed using Linux cpuid.
318 rdmsrl(HV_X64_MSR_VP_INDEX
, vp_index
);
319 hv_context
.vp_index
[cpu
] = (u32
)vp_index
;
322 * Register the per-cpu clockevent source.
324 if (ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
)
325 clockevents_config_and_register(hv_context
.clk_evt
[cpu
],
328 HV_MAX_MAX_DELTA_TICKS
);
333 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
335 void hv_synic_clockevents_cleanup(void)
339 if (!(ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
))
342 for_each_present_cpu(cpu
)
343 clockevents_unbind_device(hv_context
.clk_evt
[cpu
], cpu
);
347 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
349 int hv_synic_cleanup(unsigned int cpu
)
351 union hv_synic_sint shared_sint
;
352 union hv_synic_simp simp
;
353 union hv_synic_siefp siefp
;
354 union hv_synic_scontrol sctrl
;
355 struct vmbus_channel
*channel
, *sc
;
356 bool channel_found
= false;
359 if (!hv_context
.synic_initialized
)
363 * Search for channels which are bound to the CPU we're about to
364 * cleanup. In case we find one and vmbus is still connected we need to
365 * fail, this will effectively prevent CPU offlining. There is no way
366 * we can re-bind channels to different CPUs for now.
368 mutex_lock(&vmbus_connection
.channel_mutex
);
369 list_for_each_entry(channel
, &vmbus_connection
.chn_list
, listentry
) {
370 if (channel
->target_cpu
== cpu
) {
371 channel_found
= true;
374 spin_lock_irqsave(&channel
->lock
, flags
);
375 list_for_each_entry(sc
, &channel
->sc_list
, sc_list
) {
376 if (sc
->target_cpu
== cpu
) {
377 channel_found
= true;
381 spin_unlock_irqrestore(&channel
->lock
, flags
);
385 mutex_unlock(&vmbus_connection
.channel_mutex
);
387 if (channel_found
&& vmbus_connection
.conn_state
== CONNECTED
)
390 /* Turn off clockevent device */
391 if (ms_hyperv
.features
& HV_X64_MSR_SYNTIMER_AVAILABLE
) {
392 clockevents_unbind_device(hv_context
.clk_evt
[cpu
], cpu
);
393 hv_ce_shutdown(hv_context
.clk_evt
[cpu
]);
396 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
398 shared_sint
.masked
= 1;
400 /* Need to correctly cleanup in the case of SMP!!! */
401 /* Disable the interrupt */
402 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, shared_sint
.as_uint64
);
404 rdmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
405 simp
.simp_enabled
= 0;
406 simp
.base_simp_gpa
= 0;
408 wrmsrl(HV_X64_MSR_SIMP
, simp
.as_uint64
);
410 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
411 siefp
.siefp_enabled
= 0;
412 siefp
.base_siefp_gpa
= 0;
414 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.as_uint64
);
416 /* Disable the global synic bit */
417 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);
419 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.as_uint64
);