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 <machine/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;
86 hv_vmbus_message* copied;
87 static bool warned = false;
90 KASSERT(cpu <= mp_maxid, ("VMBUS: vmbus_msg_swintr: "
91 "cpu out of range!"));
93 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
94 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
97 if (msg->header.message_type == HV_MESSAGE_TYPE_NONE)
98 break; /* no message */
100 hdr = (hv_vmbus_channel_msg_header *)msg->u.payload;
101 msg_type = hdr->message_type;
103 if (msg_type >= HV_CHANNEL_MESSAGE_COUNT && !warned) {
105 printf("VMBUS: unknown message type = %d\n", msg_type);
109 entry = &g_channel_message_table[msg_type];
111 if (entry->handler_no_sleep)
112 entry->messageHandler(hdr);
115 copied = malloc(sizeof(hv_vmbus_message),
117 KASSERT(copied != NULL,
118 ("Error VMBUS: malloc failed to allocate"
119 " hv_vmbus_message!"));
123 memcpy(copied, msg, sizeof(hv_vmbus_message));
124 hv_queue_work_item(hv_vmbus_g_connection.work_queue,
125 hv_vmbus_on_channel_message,
129 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
132 * Make sure the write to message_type (ie set to
133 * HV_MESSAGE_TYPE_NONE) happens before we read the
134 * message_pending and EOMing. Otherwise, the EOMing will
135 * not deliver any more messages
136 * since there is no empty slot
140 if (msg->header.message_flags.u.message_pending) {
142 * This will cause message queue rescan to possibly
143 * deliver another msg from the hypervisor
145 wrmsr(HV_X64_MSR_EOM, 0);
151 * @brief Interrupt filter routine for VMBUS.
153 * The purpose of this routine is to determine the type of VMBUS protocol
154 * message to process - an event or a channel message.
157 hv_vmbus_isr(struct trapframe *frame)
160 hv_vmbus_message* msg;
161 hv_vmbus_synic_event_flags* event;
164 cpu = PCPU_GET(cpuid);
167 * The Windows team has advised that we check for events
168 * before checking for messages. This is the way they do it
169 * in Windows when running as a guest in Hyper-V
172 page_addr = hv_vmbus_g_context.syn_ic_event_page[cpu];
173 event = (hv_vmbus_synic_event_flags*)
174 page_addr + HV_VMBUS_MESSAGE_SINT;
176 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
177 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7)) {
178 /* Since we are a child, we only need to check bit 0 */
179 if (synch_test_and_clear_bit(0, &event->flags32[0])) {
180 swi_sched(hv_vmbus_g_context.event_swintr[cpu], 0);
184 * On host with Win8 or above, we can directly look at
185 * the event page. If bit n is set, we have an interrupt
186 * on the channel with id n.
187 * Directly schedule the event software interrupt on
190 swi_sched(hv_vmbus_g_context.event_swintr[cpu], 0);
193 /* Check if there are actual msgs to be process */
194 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
195 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
197 /* we call eventtimer process the message */
198 if (msg->header.message_type == HV_MESSAGE_TIMER_EXPIRED) {
199 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
202 * Make sure the write to message_type (ie set to
203 * HV_MESSAGE_TYPE_NONE) happens before we read the
204 * message_pending and EOMing. Otherwise, the EOMing will
205 * not deliver any more messages
206 * since there is no empty slot
210 if (msg->header.message_flags.u.message_pending) {
212 * This will cause message queue rescan to possibly
213 * deliver another msg from the hypervisor
215 wrmsr(HV_X64_MSR_EOM, 0);
218 return (FILTER_HANDLED);
221 if (msg->header.message_type != HV_MESSAGE_TYPE_NONE) {
222 swi_sched(hv_vmbus_g_context.msg_swintr[cpu], 0);
225 return (FILTER_HANDLED);
228 uint32_t hv_vmbus_swintr_event_cpu[MAXCPU];
229 u_long *hv_vmbus_intr_cpu[MAXCPU];
232 hv_vector_handler(struct trapframe *trap_frame)
237 * Disable preemption.
242 * Do a little interrupt counting.
244 cpu = PCPU_GET(cpuid);
245 (*hv_vmbus_intr_cpu[cpu])++;
247 hv_vmbus_isr(trap_frame);
262 struct hv_device *child_dev_ctx = device_get_ivars(child);
266 case HV_VMBUS_IVAR_TYPE:
267 *result = (uintptr_t) &child_dev_ctx->class_id;
269 case HV_VMBUS_IVAR_INSTANCE:
270 *result = (uintptr_t) &child_dev_ctx->device_id;
272 case HV_VMBUS_IVAR_DEVCTX:
273 *result = (uintptr_t) child_dev_ctx;
275 case HV_VMBUS_IVAR_NODE:
276 *result = (uintptr_t) child_dev_ctx->device;
291 case HV_VMBUS_IVAR_TYPE:
292 case HV_VMBUS_IVAR_INSTANCE:
293 case HV_VMBUS_IVAR_DEVCTX:
294 case HV_VMBUS_IVAR_NODE:
302 hv_vmbus_child_device_create(
305 hv_vmbus_channel* channel)
307 hv_device* child_dev;
310 * Allocate the new child device
312 child_dev = malloc(sizeof(hv_device), M_DEVBUF,
314 KASSERT(child_dev != NULL,
315 ("Error VMBUS: malloc failed to allocate hv_device!"));
317 if (child_dev == NULL)
320 child_dev->channel = channel;
321 memcpy(&child_dev->class_id, &type, sizeof(hv_guid));
322 memcpy(&child_dev->device_id, &instance, sizeof(hv_guid));
328 print_dev_guid(struct hv_device *dev)
331 unsigned char guid_name[100];
332 for (i = 0; i < 32; i += 2)
333 sprintf(&guid_name[i], "%02x", dev->class_id.data[i / 2]);
335 printf("VMBUS: Class ID: %s\n", guid_name);
339 hv_vmbus_child_device_register(struct hv_device *child_dev)
344 print_dev_guid(child_dev);
347 child = device_add_child(vmbus_devp, NULL, -1);
348 child_dev->device = child;
349 device_set_ivars(child, child_dev);
352 ret = device_probe_and_attach(child);
359 hv_vmbus_child_device_unregister(struct hv_device *child_dev)
363 * XXXKYS: Ensure that this is the opposite of
367 ret = device_delete_child(vmbus_devp, child_dev->device);
373 vmbus_probe(device_t dev) {
374 if (ACPI_ID_PROBE(device_get_parent(dev), dev, vmbus_ids) == NULL ||
375 device_get_unit(dev) != 0)
378 device_set_desc(dev, "Vmbus Devices");
380 return (BUS_PROBE_DEFAULT);
384 extern inthand_t IDTVEC(rsvd), IDTVEC(hv_vmbus_callback);
387 * @brief Find a free IDT slot and setup the interrupt handler.
390 vmbus_vector_alloc(void)
394 struct gate_descriptor *ip;
397 * Search backwards form the highest IDT vector available for use
398 * as vmbus channel callback vector. We install 'hv_vmbus_callback'
399 * handler at that vector and use it to interrupt vcpus.
401 vector = APIC_SPURIOUS_INT;
402 while (--vector >= APIC_IPI_INTS) {
404 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
405 if (func == (uintptr_t)&IDTVEC(rsvd)) {
407 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYS386IGT,
408 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
410 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYSIGT,
421 * @brief Restore the IDT slot to rsvd.
424 vmbus_vector_free(int vector)
427 struct gate_descriptor *ip;
432 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
433 ("invalid vector %d", vector));
436 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
437 KASSERT(func == (uintptr_t)&IDTVEC(hv_vmbus_callback),
438 ("invalid vector %d", vector));
440 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
446 vmbus_vector_alloc(void)
452 vmbus_vector_free(int vector)
459 * @brief Main vmbus driver initialization routine.
462 * - initialize the vmbus driver context
463 * - setup various driver entry points
464 * - invoke the vmbus hv main init routine
465 * - get the irq resource
466 * - invoke the vmbus to add the vmbus root device
467 * - setup the vmbus root device
468 * - retrieve the channel offers
474 char buf[MAXCOMLEN + 1];
481 ret = hv_vmbus_init();
485 printf("Error VMBUS: Hypervisor Initialization Failed!\n");
490 * Find a free IDT slot for vmbus callback.
492 hv_vmbus_g_context.hv_cb_vector = vmbus_vector_alloc();
494 if (hv_vmbus_g_context.hv_cb_vector == 0) {
496 printf("Error VMBUS: Cannot find free IDT slot for "
497 "vmbus callback!\n");
502 printf("VMBUS: vmbus callback vector %d\n",
503 hv_vmbus_g_context.hv_cb_vector);
506 * Notify the hypervisor of our vector.
508 setup_args.vector = hv_vmbus_g_context.hv_cb_vector;
511 hv_vmbus_swintr_event_cpu[j] = 0;
512 hv_vmbus_g_context.hv_event_intr_event[j] = NULL;
513 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
514 hv_vmbus_g_context.event_swintr[j] = NULL;
515 hv_vmbus_g_context.msg_swintr[j] = NULL;
517 snprintf(buf, sizeof(buf), "cpu%d:hyperv", j);
518 intrcnt_add(buf, &hv_vmbus_intr_cpu[j]);
520 for (i = 0; i < 2; i++)
521 setup_args.page_buffers[2 * j + i] = NULL;
529 * Setup software interrupt thread and handler for msg handling.
531 ret = swi_add(&hv_vmbus_g_context.hv_msg_intr_event[j],
532 "hv_msg", vmbus_msg_swintr, (void *)(long)j, SWI_CLOCK, 0,
533 &hv_vmbus_g_context.msg_swintr[j]);
536 printf("VMBUS: failed to setup msg swi for "
542 * Bind the swi thread to the cpu.
544 ret = intr_event_bind(hv_vmbus_g_context.hv_msg_intr_event[j],
548 printf("VMBUS: failed to bind msg swi thread "
554 * Setup software interrupt thread and handler for
557 ret = swi_add(&hv_vmbus_g_context.hv_event_intr_event[j],
558 "hv_event", hv_vmbus_on_events, (void *)(long)j,
559 SWI_CLOCK, 0, &hv_vmbus_g_context.event_swintr[j]);
562 printf("VMBUS: failed to setup event swi for "
568 * Prepare the per cpu msg and event pages to be called on each cpu.
570 for(i = 0; i < 2; i++) {
571 setup_args.page_buffers[2 * j + i] =
572 malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO);
573 if (setup_args.page_buffers[2 * j + i] == NULL) {
574 KASSERT(setup_args.page_buffers[2 * j + i] != NULL,
575 ("Error VMBUS: malloc failed!"));
582 printf("VMBUS: Calling smp_rendezvous, smp_started = %d\n",
585 smp_rendezvous(NULL, hv_vmbus_synic_init, NULL, &setup_args);
588 * Connect to VMBus in the root partition
590 ret = hv_vmbus_connect();
595 hv_vmbus_request_channel_offers();
600 * Free pages alloc'ed
602 for (n = 0; n < 2 * MAXCPU; n++)
603 if (setup_args.page_buffers[n] != NULL)
604 free(setup_args.page_buffers[n], M_DEVBUF);
607 * remove swi and vmbus callback vector;
610 if (hv_vmbus_g_context.msg_swintr[j] != NULL)
611 swi_remove(hv_vmbus_g_context.msg_swintr[j]);
612 if (hv_vmbus_g_context.event_swintr[j] != NULL)
613 swi_remove(hv_vmbus_g_context.event_swintr[j]);
614 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
615 hv_vmbus_g_context.hv_event_intr_event[j] = NULL;
618 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
627 vmbus_attach(device_t dev)
630 device_printf(dev, "VMBUS: attach dev: %p\n", dev);
634 * If the system has already booted and thread
635 * scheduling is possible indicated by the global
636 * cold set to zero, we just call the driver
637 * initialization directly.
648 if (vm_guest != VM_GUEST_HV)
652 * If the system has already booted and thread
653 * scheduling is possible, as indicated by the
654 * global cold set to zero, we just call the driver
655 * initialization directly.
666 hv_vmbus_release_unattached_channels();
667 hv_vmbus_disconnect();
669 smp_rendezvous(NULL, hv_vmbus_synic_cleanup, NULL, NULL);
671 for(i = 0; i < 2 * MAXCPU; i++) {
672 if (setup_args.page_buffers[i] != 0)
673 free(setup_args.page_buffers[i], M_DEVBUF);
680 if (hv_vmbus_g_context.msg_swintr[i] != NULL)
681 swi_remove(hv_vmbus_g_context.msg_swintr[i]);
682 if (hv_vmbus_g_context.event_swintr[i] != NULL)
683 swi_remove(hv_vmbus_g_context.event_swintr[i]);
684 hv_vmbus_g_context.hv_msg_intr_event[i] = NULL;
685 hv_vmbus_g_context.hv_event_intr_event[i] = NULL;
688 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
700 vmbus_detach(device_t dev)
710 printf("VMBUS: load\n");
714 vmbus_mod_unload(void)
717 printf("VMBUS: unload\n");
721 vmbus_modevent(module_t mod, int what, void *arg)
736 static device_method_t vmbus_methods[] = {
737 /** Device interface */
738 DEVMETHOD(device_probe, vmbus_probe),
739 DEVMETHOD(device_attach, vmbus_attach),
740 DEVMETHOD(device_detach, vmbus_detach),
741 DEVMETHOD(device_shutdown, bus_generic_shutdown),
742 DEVMETHOD(device_suspend, bus_generic_suspend),
743 DEVMETHOD(device_resume, bus_generic_resume),
746 DEVMETHOD(bus_add_child, bus_generic_add_child),
747 DEVMETHOD(bus_print_child, bus_generic_print_child),
748 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
749 DEVMETHOD(bus_write_ivar, vmbus_write_ivar),
753 static char driver_name[] = "vmbus";
754 static driver_t vmbus_driver = { driver_name, vmbus_methods,0, };
757 devclass_t vmbus_devclass;
759 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, vmbus_modevent, 0);
760 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
761 MODULE_VERSION(vmbus, 1);
763 /* We want to be started after SMP is initialized */
764 SYSINIT(vmb_init, SI_SUB_SMP + 1, SI_ORDER_FIRST, vmbus_init, NULL);