2 * Copyright (c) 2009-2012 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * VM Bus Driver Implementation
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <sys/syslog.h>
44 #include <sys/systm.h>
45 #include <sys/rtprio.h>
46 #include <sys/interrupt.h>
48 #include <sys/taskqueue.h>
49 #include <sys/mutex.h>
52 #include <machine/resource.h>
55 #include <machine/stdarg.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/md_var.h>
58 #include <machine/segments.h>
60 #include <x86/apicvar.h>
62 #include "hv_vmbus_priv.h"
64 #include <contrib/dev/acpica/include/acpi.h>
67 static device_t vmbus_devp;
68 static int vmbus_inited;
69 static hv_setup_args setup_args; /* only CPU 0 supported at this time */
71 static char *vmbus_ids[] = { "VMBUS", NULL };
74 * @brief Software interrupt thread routine to handle channel messages from
78 vmbus_msg_swintr(void *arg)
82 hv_vmbus_channel_msg_header *hdr;
83 hv_vmbus_channel_msg_table_entry *entry;
84 hv_vmbus_channel_msg_type msg_type;
85 hv_vmbus_message* msg;
88 KASSERT(cpu <= mp_maxid, ("VMBUS: vmbus_msg_swintr: "
89 "cpu out of range!"));
91 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
92 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
95 if (msg->header.message_type == HV_MESSAGE_TYPE_NONE)
96 break; /* no message */
98 hdr = (hv_vmbus_channel_msg_header *)msg->u.payload;
99 msg_type = hdr->message_type;
101 if (msg_type >= HV_CHANNEL_MESSAGE_COUNT) {
102 printf("VMBUS: unknown message type = %d\n", msg_type);
106 entry = &g_channel_message_table[msg_type];
108 if (entry->messageHandler)
109 entry->messageHandler(hdr);
111 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
114 * Make sure the write to message_type (ie set to
115 * HV_MESSAGE_TYPE_NONE) happens before we read the
116 * message_pending and EOMing. Otherwise, the EOMing will
117 * not deliver any more messages
118 * since there is no empty slot
122 if (msg->header.message_flags.u.message_pending) {
124 * This will cause message queue rescan to possibly
125 * deliver another msg from the hypervisor
127 wrmsr(HV_X64_MSR_EOM, 0);
133 * @brief Interrupt filter routine for VMBUS.
135 * The purpose of this routine is to determine the type of VMBUS protocol
136 * message to process - an event or a channel message.
139 hv_vmbus_isr(struct trapframe *frame)
142 hv_vmbus_message* msg;
143 hv_vmbus_synic_event_flags* event;
146 cpu = PCPU_GET(cpuid);
149 * The Windows team has advised that we check for events
150 * before checking for messages. This is the way they do it
151 * in Windows when running as a guest in Hyper-V
154 page_addr = hv_vmbus_g_context.syn_ic_event_page[cpu];
155 event = (hv_vmbus_synic_event_flags*)
156 page_addr + HV_VMBUS_MESSAGE_SINT;
158 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
159 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7)) {
160 /* Since we are a child, we only need to check bit 0 */
161 if (synch_test_and_clear_bit(0, &event->flags32[0])) {
162 hv_vmbus_on_events(cpu);
166 * On host with Win8 or above, we can directly look at
167 * the event page. If bit n is set, we have an interrupt
168 * on the channel with id n.
169 * Directly schedule the event software interrupt on
172 hv_vmbus_on_events(cpu);
175 /* Check if there are actual msgs to be process */
176 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
177 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
179 /* we call eventtimer process the message */
180 if (msg->header.message_type == HV_MESSAGE_TIMER_EXPIRED) {
181 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
184 * Make sure the write to message_type (ie set to
185 * HV_MESSAGE_TYPE_NONE) happens before we read the
186 * message_pending and EOMing. Otherwise, the EOMing will
187 * not deliver any more messages
188 * since there is no empty slot
192 if (msg->header.message_flags.u.message_pending) {
194 * This will cause message queue rescan to possibly
195 * deliver another msg from the hypervisor
197 wrmsr(HV_X64_MSR_EOM, 0);
200 return (FILTER_HANDLED);
203 if (msg->header.message_type != HV_MESSAGE_TYPE_NONE) {
204 swi_sched(hv_vmbus_g_context.msg_swintr[cpu], 0);
207 return (FILTER_HANDLED);
210 u_long *hv_vmbus_intr_cpu[MAXCPU];
213 hv_vector_handler(struct trapframe *trap_frame)
218 * Disable preemption.
223 * Do a little interrupt counting.
225 cpu = PCPU_GET(cpuid);
226 (*hv_vmbus_intr_cpu[cpu])++;
228 hv_vmbus_isr(trap_frame);
243 struct hv_device *child_dev_ctx = device_get_ivars(child);
247 case HV_VMBUS_IVAR_TYPE:
248 *result = (uintptr_t) &child_dev_ctx->class_id;
250 case HV_VMBUS_IVAR_INSTANCE:
251 *result = (uintptr_t) &child_dev_ctx->device_id;
253 case HV_VMBUS_IVAR_DEVCTX:
254 *result = (uintptr_t) child_dev_ctx;
256 case HV_VMBUS_IVAR_NODE:
257 *result = (uintptr_t) child_dev_ctx->device;
272 case HV_VMBUS_IVAR_TYPE:
273 case HV_VMBUS_IVAR_INSTANCE:
274 case HV_VMBUS_IVAR_DEVCTX:
275 case HV_VMBUS_IVAR_NODE:
283 hv_vmbus_child_device_create(
286 hv_vmbus_channel* channel)
288 hv_device* child_dev;
291 * Allocate the new child device
293 child_dev = malloc(sizeof(hv_device), M_DEVBUF,
295 KASSERT(child_dev != NULL,
296 ("Error VMBUS: malloc failed to allocate hv_device!"));
298 if (child_dev == NULL)
301 child_dev->channel = channel;
302 memcpy(&child_dev->class_id, &type, sizeof(hv_guid));
303 memcpy(&child_dev->device_id, &instance, sizeof(hv_guid));
309 print_dev_guid(struct hv_device *dev)
312 unsigned char guid_name[100];
313 for (i = 0; i < 32; i += 2)
314 sprintf(&guid_name[i], "%02x", dev->class_id.data[i / 2]);
316 printf("VMBUS: Class ID: %s\n", guid_name);
320 hv_vmbus_child_device_register(struct hv_device *child_dev)
325 print_dev_guid(child_dev);
328 child = device_add_child(vmbus_devp, NULL, -1);
329 child_dev->device = child;
330 device_set_ivars(child, child_dev);
333 ret = device_probe_and_attach(child);
340 hv_vmbus_child_device_unregister(struct hv_device *child_dev)
344 * XXXKYS: Ensure that this is the opposite of
348 ret = device_delete_child(vmbus_devp, child_dev->device);
354 vmbus_probe(device_t dev) {
355 if (ACPI_ID_PROBE(device_get_parent(dev), dev, vmbus_ids) == NULL ||
356 device_get_unit(dev) != 0)
359 device_set_desc(dev, "Vmbus Devices");
361 return (BUS_PROBE_DEFAULT);
365 extern inthand_t IDTVEC(rsvd), IDTVEC(hv_vmbus_callback);
368 * @brief Find a free IDT slot and setup the interrupt handler.
371 vmbus_vector_alloc(void)
375 struct gate_descriptor *ip;
378 * Search backwards form the highest IDT vector available for use
379 * as vmbus channel callback vector. We install 'hv_vmbus_callback'
380 * handler at that vector and use it to interrupt vcpus.
382 vector = APIC_SPURIOUS_INT;
383 while (--vector >= APIC_IPI_INTS) {
385 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
386 if (func == (uintptr_t)&IDTVEC(rsvd)) {
388 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYS386IGT,
389 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
391 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYSIGT,
402 * @brief Restore the IDT slot to rsvd.
405 vmbus_vector_free(int vector)
408 struct gate_descriptor *ip;
413 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
414 ("invalid vector %d", vector));
417 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
418 KASSERT(func == (uintptr_t)&IDTVEC(hv_vmbus_callback),
419 ("invalid vector %d", vector));
421 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
427 vmbus_vector_alloc(void)
433 vmbus_vector_free(int vector)
440 * @brief Main vmbus driver initialization routine.
443 * - initialize the vmbus driver context
444 * - setup various driver entry points
445 * - invoke the vmbus hv main init routine
446 * - get the irq resource
447 * - invoke the vmbus to add the vmbus root device
448 * - setup the vmbus root device
449 * - retrieve the channel offers
455 char buf[MAXCOMLEN + 1];
463 ret = hv_vmbus_init();
467 printf("Error VMBUS: Hypervisor Initialization Failed!\n");
472 * Find a free IDT slot for vmbus callback.
474 hv_vmbus_g_context.hv_cb_vector = vmbus_vector_alloc();
476 if (hv_vmbus_g_context.hv_cb_vector == 0) {
478 printf("Error VMBUS: Cannot find free IDT slot for "
479 "vmbus callback!\n");
484 printf("VMBUS: vmbus callback vector %d\n",
485 hv_vmbus_g_context.hv_cb_vector);
488 * Notify the hypervisor of our vector.
490 setup_args.vector = hv_vmbus_g_context.hv_cb_vector;
493 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
494 hv_vmbus_g_context.msg_swintr[j] = NULL;
496 snprintf(buf, sizeof(buf), "cpu%d:hyperv", j);
497 intrcnt_add(buf, &hv_vmbus_intr_cpu[j]);
499 for (i = 0; i < 2; i++)
500 setup_args.page_buffers[2 * j + i] = NULL;
508 * Setup taskqueue to handle events
510 hv_vmbus_g_context.hv_event_queue[j] = taskqueue_create_fast("hyperv event", M_WAITOK,
511 taskqueue_thread_enqueue, &hv_vmbus_g_context.hv_event_queue[j]);
512 if (hv_vmbus_g_context.hv_event_queue[j] == NULL) {
514 printf("VMBUS: failed to setup taskqueue\n");
517 CPU_SETOF(j, &cpu_mask);
518 taskqueue_start_threads_cpuset(&hv_vmbus_g_context.hv_event_queue[j], 1, PI_NET, &cpu_mask,
522 * Setup software interrupt thread and handler for msg handling.
524 ret = swi_add(&hv_vmbus_g_context.hv_msg_intr_event[j],
525 "hv_msg", vmbus_msg_swintr, (void *)(long)j, SWI_CLOCK, 0,
526 &hv_vmbus_g_context.msg_swintr[j]);
529 printf("VMBUS: failed to setup msg swi for "
535 * Bind the swi thread to the cpu.
537 ret = intr_event_bind(hv_vmbus_g_context.hv_msg_intr_event[j],
541 printf("VMBUS: failed to bind msg swi thread "
547 * Prepare the per cpu msg and event pages to be called on each cpu.
549 for(i = 0; i < 2; i++) {
550 setup_args.page_buffers[2 * j + i] =
551 malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
552 if (setup_args.page_buffers[2 * j + i] == NULL) {
553 KASSERT(setup_args.page_buffers[2 * j + i] != NULL,
554 ("Error VMBUS: malloc failed!"));
561 printf("VMBUS: Calling smp_rendezvous, smp_started = %d\n",
564 smp_rendezvous(NULL, hv_vmbus_synic_init, NULL, &setup_args);
567 * Connect to VMBus in the root partition
569 ret = hv_vmbus_connect();
574 hv_vmbus_request_channel_offers();
579 * Free pages alloc'ed
581 for (n = 0; n < 2 * MAXCPU; n++)
582 if (setup_args.page_buffers[n] != NULL)
583 free(setup_args.page_buffers[n], M_DEVBUF);
586 * remove swi and vmbus callback vector;
589 if (hv_vmbus_g_context.hv_event_queue[j] != NULL)
590 taskqueue_free(hv_vmbus_g_context.hv_event_queue[j]);
591 if (hv_vmbus_g_context.msg_swintr[j] != NULL)
592 swi_remove(hv_vmbus_g_context.msg_swintr[j]);
593 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
596 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
605 vmbus_attach(device_t dev)
608 device_printf(dev, "VMBUS: attach dev: %p\n", dev);
612 * If the system has already booted and thread
613 * scheduling is possible indicated by the global
614 * cold set to zero, we just call the driver
615 * initialization directly.
626 if (vm_guest != VM_GUEST_HV)
630 * If the system has already booted and thread
631 * scheduling is possible, as indicated by the
632 * global cold set to zero, we just call the driver
633 * initialization directly.
644 hv_vmbus_release_unattached_channels();
645 hv_vmbus_disconnect();
647 smp_rendezvous(NULL, hv_vmbus_synic_cleanup, NULL, NULL);
649 for(i = 0; i < 2 * MAXCPU; i++) {
650 if (setup_args.page_buffers[i] != 0)
651 free(setup_args.page_buffers[i], M_DEVBUF);
658 if (hv_vmbus_g_context.hv_event_queue[i] != NULL)
659 taskqueue_free(hv_vmbus_g_context.hv_event_queue[i]);
660 if (hv_vmbus_g_context.msg_swintr[i] != NULL)
661 swi_remove(hv_vmbus_g_context.msg_swintr[i]);
662 hv_vmbus_g_context.hv_msg_intr_event[i] = NULL;
665 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
677 vmbus_detach(device_t dev)
687 printf("VMBUS: load\n");
691 vmbus_mod_unload(void)
694 printf("VMBUS: unload\n");
698 vmbus_modevent(module_t mod, int what, void *arg)
713 static device_method_t vmbus_methods[] = {
714 /** Device interface */
715 DEVMETHOD(device_probe, vmbus_probe),
716 DEVMETHOD(device_attach, vmbus_attach),
717 DEVMETHOD(device_detach, vmbus_detach),
718 DEVMETHOD(device_shutdown, bus_generic_shutdown),
719 DEVMETHOD(device_suspend, bus_generic_suspend),
720 DEVMETHOD(device_resume, bus_generic_resume),
723 DEVMETHOD(bus_add_child, bus_generic_add_child),
724 DEVMETHOD(bus_print_child, bus_generic_print_child),
725 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
726 DEVMETHOD(bus_write_ivar, vmbus_write_ivar),
730 static char driver_name[] = "vmbus";
731 static driver_t vmbus_driver = { driver_name, vmbus_methods,0, };
734 devclass_t vmbus_devclass;
736 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, vmbus_modevent, 0);
737 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
738 MODULE_VERSION(vmbus, 1);
740 /* We want to be started after SMP is initialized */
741 SYSINIT(vmb_init, SI_SUB_SMP + 1, SI_ORDER_FIRST, vmbus_init, NULL);