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;
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
121 * mb() is used here, since atomic_thread_fence_seq_cst()
122 * will become compler fence on UP kernel.
126 if (msg->header.message_flags.u.message_pending) {
128 * This will cause message queue rescan to possibly
129 * deliver another msg from the hypervisor
131 wrmsr(HV_X64_MSR_EOM, 0);
137 * @brief Interrupt filter routine for VMBUS.
139 * The purpose of this routine is to determine the type of VMBUS protocol
140 * message to process - an event or a channel message.
143 hv_vmbus_isr(struct trapframe *frame)
146 hv_vmbus_message* msg;
147 hv_vmbus_synic_event_flags* event;
150 cpu = PCPU_GET(cpuid);
153 * The Windows team has advised that we check for events
154 * before checking for messages. This is the way they do it
155 * in Windows when running as a guest in Hyper-V
158 page_addr = hv_vmbus_g_context.syn_ic_event_page[cpu];
159 event = (hv_vmbus_synic_event_flags*)
160 page_addr + HV_VMBUS_MESSAGE_SINT;
162 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
163 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7)) {
164 /* Since we are a child, we only need to check bit 0 */
165 if (synch_test_and_clear_bit(0, &event->flags32[0])) {
166 hv_vmbus_on_events(cpu);
170 * On host with Win8 or above, we can directly look at
171 * the event page. If bit n is set, we have an interrupt
172 * on the channel with id n.
173 * Directly schedule the event software interrupt on
176 hv_vmbus_on_events(cpu);
179 /* Check if there are actual msgs to be process */
180 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
181 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
183 /* we call eventtimer process the message */
184 if (msg->header.message_type == HV_MESSAGE_TIMER_EXPIRED) {
185 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
188 * Make sure the write to message_type (ie set to
189 * HV_MESSAGE_TYPE_NONE) happens before we read the
190 * message_pending and EOMing. Otherwise, the EOMing will
191 * not deliver any more messages
192 * since there is no empty slot
195 * mb() is used here, since atomic_thread_fence_seq_cst()
196 * will become compler fence on UP kernel.
200 if (msg->header.message_flags.u.message_pending) {
202 * This will cause message queue rescan to possibly
203 * deliver another msg from the hypervisor
205 wrmsr(HV_X64_MSR_EOM, 0);
208 return (FILTER_HANDLED);
211 if (msg->header.message_type != HV_MESSAGE_TYPE_NONE) {
212 swi_sched(hv_vmbus_g_context.msg_swintr[cpu], 0);
215 return (FILTER_HANDLED);
218 u_long *hv_vmbus_intr_cpu[MAXCPU];
221 hv_vector_handler(struct trapframe *trap_frame)
226 * Disable preemption.
231 * Do a little interrupt counting.
233 cpu = PCPU_GET(cpuid);
234 (*hv_vmbus_intr_cpu[cpu])++;
236 hv_vmbus_isr(trap_frame);
251 struct hv_device *child_dev_ctx = device_get_ivars(child);
255 case HV_VMBUS_IVAR_TYPE:
256 *result = (uintptr_t) &child_dev_ctx->class_id;
258 case HV_VMBUS_IVAR_INSTANCE:
259 *result = (uintptr_t) &child_dev_ctx->device_id;
261 case HV_VMBUS_IVAR_DEVCTX:
262 *result = (uintptr_t) child_dev_ctx;
264 case HV_VMBUS_IVAR_NODE:
265 *result = (uintptr_t) child_dev_ctx->device;
280 case HV_VMBUS_IVAR_TYPE:
281 case HV_VMBUS_IVAR_INSTANCE:
282 case HV_VMBUS_IVAR_DEVCTX:
283 case HV_VMBUS_IVAR_NODE:
291 hv_vmbus_child_device_create(
294 hv_vmbus_channel* channel)
296 hv_device* child_dev;
299 * Allocate the new child device
301 child_dev = malloc(sizeof(hv_device), M_DEVBUF,
304 child_dev->channel = channel;
305 memcpy(&child_dev->class_id, &type, sizeof(hv_guid));
306 memcpy(&child_dev->device_id, &instance, sizeof(hv_guid));
312 print_dev_guid(struct hv_device *dev)
315 unsigned char guid_name[100];
316 for (i = 0; i < 32; i += 2)
317 sprintf(&guid_name[i], "%02x", dev->class_id.data[i / 2]);
319 printf("VMBUS: Class ID: %s\n", guid_name);
323 hv_vmbus_child_device_register(struct hv_device *child_dev)
328 print_dev_guid(child_dev);
331 child = device_add_child(vmbus_devp, NULL, -1);
332 child_dev->device = child;
333 device_set_ivars(child, child_dev);
336 ret = device_probe_and_attach(child);
343 hv_vmbus_child_device_unregister(struct hv_device *child_dev)
347 * XXXKYS: Ensure that this is the opposite of
351 ret = device_delete_child(vmbus_devp, child_dev->device);
357 vmbus_probe(device_t dev) {
358 if (ACPI_ID_PROBE(device_get_parent(dev), dev, vmbus_ids) == NULL ||
359 device_get_unit(dev) != 0)
362 device_set_desc(dev, "Vmbus Devices");
364 return (BUS_PROBE_DEFAULT);
368 extern inthand_t IDTVEC(rsvd), IDTVEC(hv_vmbus_callback);
371 * @brief Find a free IDT slot and setup the interrupt handler.
374 vmbus_vector_alloc(void)
378 struct gate_descriptor *ip;
381 * Search backwards form the highest IDT vector available for use
382 * as vmbus channel callback vector. We install 'hv_vmbus_callback'
383 * handler at that vector and use it to interrupt vcpus.
385 vector = APIC_SPURIOUS_INT;
386 while (--vector >= APIC_IPI_INTS) {
388 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
389 if (func == (uintptr_t)&IDTVEC(rsvd)) {
391 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYS386IGT,
392 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
394 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYSIGT,
405 * @brief Restore the IDT slot to rsvd.
408 vmbus_vector_free(int vector)
411 struct gate_descriptor *ip;
416 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
417 ("invalid vector %d", vector));
420 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
421 KASSERT(func == (uintptr_t)&IDTVEC(hv_vmbus_callback),
422 ("invalid vector %d", vector));
424 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
430 vmbus_vector_alloc(void)
436 vmbus_vector_free(int vector)
443 vmbus_cpuset_setthread_task(void *xmask, int pending __unused)
445 cpuset_t *mask = xmask;
448 error = cpuset_setthread(curthread->td_tid, mask);
450 panic("curthread=%ju: can't pin; error=%d",
451 (uintmax_t)curthread->td_tid, error);
456 * @brief Main vmbus driver initialization routine.
459 * - initialize the vmbus driver context
460 * - setup various driver entry points
461 * - invoke the vmbus hv main init routine
462 * - get the irq resource
463 * - invoke the vmbus to add the vmbus root device
464 * - setup the vmbus root device
465 * - retrieve the channel offers
471 char buf[MAXCOMLEN + 1];
479 ret = hv_vmbus_init();
483 printf("Error VMBUS: Hypervisor Initialization Failed!\n");
488 * Find a free IDT slot for vmbus callback.
490 hv_vmbus_g_context.hv_cb_vector = vmbus_vector_alloc();
492 if (hv_vmbus_g_context.hv_cb_vector == 0) {
494 printf("Error VMBUS: Cannot find free IDT slot for "
495 "vmbus callback!\n");
500 printf("VMBUS: vmbus callback vector %d\n",
501 hv_vmbus_g_context.hv_cb_vector);
504 * Notify the hypervisor of our vector.
506 setup_args.vector = hv_vmbus_g_context.hv_cb_vector;
509 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
510 hv_vmbus_g_context.msg_swintr[j] = NULL;
512 snprintf(buf, sizeof(buf), "cpu%d:hyperv", j);
513 intrcnt_add(buf, &hv_vmbus_intr_cpu[j]);
515 for (i = 0; i < 2; i++)
516 setup_args.page_buffers[2 * j + i] = NULL;
523 struct task cpuset_task;
526 * Setup taskqueue to handle events
528 hv_vmbus_g_context.hv_event_queue[j] = taskqueue_create_fast("hyperv event", M_WAITOK,
529 taskqueue_thread_enqueue, &hv_vmbus_g_context.hv_event_queue[j]);
530 if (hv_vmbus_g_context.hv_event_queue[j] == NULL) {
532 printf("VMBUS: failed to setup taskqueue\n");
535 taskqueue_start_threads(&hv_vmbus_g_context.hv_event_queue[j], 1, PI_NET,
538 CPU_SETOF(j, &cpu_mask);
539 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task, &cpu_mask);
540 taskqueue_enqueue(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
541 taskqueue_drain(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
544 * Setup software interrupt thread and handler for msg handling.
546 ret = swi_add(&hv_vmbus_g_context.hv_msg_intr_event[j],
547 "hv_msg", vmbus_msg_swintr, (void *)(long)j, SWI_CLOCK, 0,
548 &hv_vmbus_g_context.msg_swintr[j]);
551 printf("VMBUS: failed to setup msg swi for "
557 * Bind the swi thread to the cpu.
559 ret = intr_event_bind(hv_vmbus_g_context.hv_msg_intr_event[j],
563 printf("VMBUS: failed to bind msg swi thread "
569 * Prepare the per cpu msg and event pages to be called on each cpu.
571 for(i = 0; i < 2; i++) {
572 setup_args.page_buffers[2 * j + i] =
573 malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
578 printf("VMBUS: Calling smp_rendezvous, smp_started = %d\n",
581 smp_rendezvous(NULL, hv_vmbus_synic_init, NULL, &setup_args);
584 * Connect to VMBus in the root partition
586 ret = hv_vmbus_connect();
591 hv_vmbus_request_channel_offers();
596 * Free pages alloc'ed
598 for (n = 0; n < 2 * MAXCPU; n++)
599 if (setup_args.page_buffers[n] != NULL)
600 free(setup_args.page_buffers[n], M_DEVBUF);
603 * remove swi and vmbus callback vector;
606 if (hv_vmbus_g_context.hv_event_queue[j] != NULL)
607 taskqueue_free(hv_vmbus_g_context.hv_event_queue[j]);
608 if (hv_vmbus_g_context.msg_swintr[j] != NULL)
609 swi_remove(hv_vmbus_g_context.msg_swintr[j]);
610 hv_vmbus_g_context.hv_msg_intr_event[j] = NULL;
613 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
622 vmbus_attach(device_t dev)
625 device_printf(dev, "VMBUS: attach dev: %p\n", dev);
629 * If the system has already booted and thread
630 * scheduling is possible indicated by the global
631 * cold set to zero, we just call the driver
632 * initialization directly.
643 if (vm_guest != VM_GUEST_HV)
647 * If the system has already booted and thread
648 * scheduling is possible, as indicated by the
649 * global cold set to zero, we just call the driver
650 * initialization directly.
661 hv_vmbus_release_unattached_channels();
662 hv_vmbus_disconnect();
664 smp_rendezvous(NULL, hv_vmbus_synic_cleanup, NULL, NULL);
666 for(i = 0; i < 2 * MAXCPU; i++) {
667 if (setup_args.page_buffers[i] != 0)
668 free(setup_args.page_buffers[i], M_DEVBUF);
675 if (hv_vmbus_g_context.hv_event_queue[i] != NULL)
676 taskqueue_free(hv_vmbus_g_context.hv_event_queue[i]);
677 if (hv_vmbus_g_context.msg_swintr[i] != NULL)
678 swi_remove(hv_vmbus_g_context.msg_swintr[i]);
679 hv_vmbus_g_context.hv_msg_intr_event[i] = NULL;
682 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
694 vmbus_detach(device_t dev)
704 printf("VMBUS: load\n");
708 vmbus_mod_unload(void)
711 printf("VMBUS: unload\n");
715 vmbus_modevent(module_t mod, int what, void *arg)
730 static device_method_t vmbus_methods[] = {
731 /** Device interface */
732 DEVMETHOD(device_probe, vmbus_probe),
733 DEVMETHOD(device_attach, vmbus_attach),
734 DEVMETHOD(device_detach, vmbus_detach),
735 DEVMETHOD(device_shutdown, bus_generic_shutdown),
736 DEVMETHOD(device_suspend, bus_generic_suspend),
737 DEVMETHOD(device_resume, bus_generic_resume),
740 DEVMETHOD(bus_add_child, bus_generic_add_child),
741 DEVMETHOD(bus_print_child, bus_generic_print_child),
742 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
743 DEVMETHOD(bus_write_ivar, vmbus_write_ivar),
747 static char driver_name[] = "vmbus";
748 static driver_t vmbus_driver = { driver_name, vmbus_methods,0, };
751 devclass_t vmbus_devclass;
753 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, vmbus_modevent, 0);
754 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
755 MODULE_VERSION(vmbus, 1);
757 /* We want to be started after SMP is initialized */
758 SYSINIT(vmb_init, SI_SUB_SMP + 1, SI_ORDER_FIRST, vmbus_init, NULL);