]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/hv/hv.c
Drivers: hv: vmbus: Move the extracting of Hypervisor version information
[mirror_ubuntu-hirsute-kernel.git] / drivers / hv / hv.c
1 /*
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 */
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/kernel.h>
25 #include <linux/mm.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"
35
36 /* The one and only */
37 struct hv_context hv_context = {
38 .synic_initialized = false,
39 };
40
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
44
45 /*
46 * hv_init - Main initialization routine.
47 *
48 * This routine must be called before any other routines in here are called
49 */
50 int hv_init(void)
51 {
52 union hv_x64_msr_hypercall_contents hypercall_msr;
53
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);
67
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);
71
72 if (!hypercall_msr.enable)
73 return -ENOTSUPP;
74
75 return 0;
76 }
77
78 /*
79 * hv_cleanup - Cleanup routine.
80 *
81 * This routine is called normally during driver unloading or exiting.
82 */
83 void hv_cleanup(bool crash)
84 {
85
86 }
87
88 /*
89 * hv_post_message - Post a message using the hypervisor message IPC.
90 *
91 * This involves a hypercall.
92 */
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)
96 {
97
98 struct hv_input_post_message *aligned_msg;
99 u64 status;
100
101 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
102 return -EMSGSIZE;
103
104 aligned_msg = (struct hv_input_post_message *)
105 hv_context.post_msg_page[get_cpu()];
106
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);
112
113 status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL);
114
115 put_cpu();
116 return status & 0xFFFF;
117 }
118
119 static int hv_ce_set_next_event(unsigned long delta,
120 struct clock_event_device *evt)
121 {
122 u64 current_tick;
123
124 WARN_ON(!clockevent_state_oneshot(evt));
125
126 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
127 current_tick += delta;
128 wrmsrl(HV_X64_MSR_STIMER0_COUNT, current_tick);
129 return 0;
130 }
131
132 static int hv_ce_shutdown(struct clock_event_device *evt)
133 {
134 wrmsrl(HV_X64_MSR_STIMER0_COUNT, 0);
135 wrmsrl(HV_X64_MSR_STIMER0_CONFIG, 0);
136
137 return 0;
138 }
139
140 static int hv_ce_set_oneshot(struct clock_event_device *evt)
141 {
142 union hv_timer_config timer_cfg;
143
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);
148
149 return 0;
150 }
151
152 static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu)
153 {
154 dev->name = "Hyper-V clockevent";
155 dev->features = CLOCK_EVT_FEAT_ONESHOT;
156 dev->cpumask = cpumask_of(cpu);
157 dev->rating = 1000;
158 /*
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.
162 */
163
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;
167 }
168
169
170 int hv_synic_alloc(void)
171 {
172 size_t size = sizeof(struct tasklet_struct);
173 size_t ced_size = sizeof(struct clock_event_device);
174 int cpu;
175
176 hv_context.hv_numa_map = kzalloc(sizeof(struct cpumask) * nr_node_ids,
177 GFP_ATOMIC);
178 if (hv_context.hv_numa_map == NULL) {
179 pr_err("Unable to allocate NUMA map\n");
180 goto err;
181 }
182
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");
187 goto err;
188 }
189 tasklet_init(hv_context.event_dpc[cpu], vmbus_on_event, cpu);
190
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");
194 goto err;
195 }
196 tasklet_init(hv_context.msg_dpc[cpu], vmbus_on_msg_dpc, cpu);
197
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");
201 goto err;
202 }
203
204 hv_init_clockevent_device(hv_context.clk_evt[cpu], cpu);
205
206 hv_context.synic_message_page[cpu] =
207 (void *)get_zeroed_page(GFP_ATOMIC);
208
209 if (hv_context.synic_message_page[cpu] == NULL) {
210 pr_err("Unable to allocate SYNIC message page\n");
211 goto err;
212 }
213
214 hv_context.synic_event_page[cpu] =
215 (void *)get_zeroed_page(GFP_ATOMIC);
216
217 if (hv_context.synic_event_page[cpu] == NULL) {
218 pr_err("Unable to allocate SYNIC event page\n");
219 goto err;
220 }
221
222 hv_context.post_msg_page[cpu] =
223 (void *)get_zeroed_page(GFP_ATOMIC);
224
225 if (hv_context.post_msg_page[cpu] == NULL) {
226 pr_err("Unable to allocate post msg page\n");
227 goto err;
228 }
229
230 INIT_LIST_HEAD(&hv_context.percpu_list[cpu]);
231 }
232
233 return 0;
234 err:
235 return -ENOMEM;
236 }
237
238 static void hv_synic_free_cpu(int cpu)
239 {
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]);
249 }
250
251 void hv_synic_free(void)
252 {
253 int cpu;
254
255 kfree(hv_context.hv_numa_map);
256 for_each_present_cpu(cpu)
257 hv_synic_free_cpu(cpu);
258 }
259
260 /*
261 * hv_synic_init - Initialize the Synthethic Interrupt Controller.
262 *
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.
266 */
267 int hv_synic_init(unsigned int cpu)
268 {
269 u64 version;
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;
274 u64 vp_index;
275
276 /* Check the version */
277 rdmsrl(HV_X64_MSR_SVERSION, version);
278
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])
283 >> PAGE_SHIFT;
284
285 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
286
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])
291 >> PAGE_SHIFT;
292
293 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
294
295 /* Setup the shared SINT. */
296 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
297
298 shared_sint.as_uint64 = 0;
299 shared_sint.vector = HYPERVISOR_CALLBACK_VECTOR;
300 shared_sint.masked = false;
301 shared_sint.auto_eoi = true;
302
303 wrmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
304
305 /* Enable the global synic bit */
306 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
307 sctrl.enable = 1;
308
309 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
310
311 hv_context.synic_initialized = true;
312
313 /*
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.
317 */
318 rdmsrl(HV_X64_MSR_VP_INDEX, vp_index);
319 hv_context.vp_index[cpu] = (u32)vp_index;
320
321 /*
322 * Register the per-cpu clockevent source.
323 */
324 if (ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE)
325 clockevents_config_and_register(hv_context.clk_evt[cpu],
326 HV_TIMER_FREQUENCY,
327 HV_MIN_DELTA_TICKS,
328 HV_MAX_MAX_DELTA_TICKS);
329 return 0;
330 }
331
332 /*
333 * hv_synic_clockevents_cleanup - Cleanup clockevent devices
334 */
335 void hv_synic_clockevents_cleanup(void)
336 {
337 int cpu;
338
339 if (!(ms_hyperv.features & HV_X64_MSR_SYNTIMER_AVAILABLE))
340 return;
341
342 for_each_present_cpu(cpu)
343 clockevents_unbind_device(hv_context.clk_evt[cpu], cpu);
344 }
345
346 /*
347 * hv_synic_cleanup - Cleanup routine for hv_synic_init().
348 */
349 int hv_synic_cleanup(unsigned int cpu)
350 {
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;
357 unsigned long flags;
358
359 if (!hv_context.synic_initialized)
360 return -EFAULT;
361
362 /*
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.
367 */
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;
372 break;
373 }
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;
378 break;
379 }
380 }
381 spin_unlock_irqrestore(&channel->lock, flags);
382 if (channel_found)
383 break;
384 }
385 mutex_unlock(&vmbus_connection.channel_mutex);
386
387 if (channel_found && vmbus_connection.conn_state == CONNECTED)
388 return -EBUSY;
389
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]);
394 }
395
396 rdmsrl(HV_X64_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
397
398 shared_sint.masked = 1;
399
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);
403
404 rdmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
405 simp.simp_enabled = 0;
406 simp.base_simp_gpa = 0;
407
408 wrmsrl(HV_X64_MSR_SIMP, simp.as_uint64);
409
410 rdmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
411 siefp.siefp_enabled = 0;
412 siefp.base_siefp_gpa = 0;
413
414 wrmsrl(HV_X64_MSR_SIEFP, siefp.as_uint64);
415
416 /* Disable the global synic bit */
417 rdmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
418 sctrl.enable = 0;
419 wrmsrl(HV_X64_MSR_SCONTROL, sctrl.as_uint64);
420
421 return 0;
422 }