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 <dev/hyperv/include/hyperv.h>
63 #include "hv_vmbus_priv.h"
65 #include <contrib/dev/acpica/include/acpi.h>
68 static device_t vmbus_devp;
69 static int vmbus_inited;
70 static hv_setup_args setup_args; /* only CPU 0 supported at this time */
72 static char *vmbus_ids[] = { "VMBUS", NULL };
75 * @brief Software interrupt thread routine to handle channel messages from
79 vmbus_msg_swintr(void *arg, int pending __unused)
83 hv_vmbus_channel_msg_header *hdr;
84 hv_vmbus_channel_msg_table_entry *entry;
85 hv_vmbus_channel_msg_type msg_type;
86 hv_vmbus_message* msg;
89 KASSERT(cpu <= mp_maxid, ("VMBUS: vmbus_msg_swintr: "
90 "cpu out of range!"));
92 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
93 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
96 if (msg->header.message_type == HV_MESSAGE_TYPE_NONE)
97 break; /* no message */
99 hdr = (hv_vmbus_channel_msg_header *)msg->u.payload;
100 msg_type = hdr->message_type;
102 if (msg_type >= HV_CHANNEL_MESSAGE_COUNT) {
103 printf("VMBUS: unknown message type = %d\n", msg_type);
107 entry = &g_channel_message_table[msg_type];
109 if (entry->messageHandler)
110 entry->messageHandler(hdr);
112 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
115 * Make sure the write to message_type (ie set to
116 * HV_MESSAGE_TYPE_NONE) happens before we read the
117 * message_pending and EOMing. Otherwise, the EOMing will
118 * not deliver any more messages
119 * since there is no empty slot
122 * mb() is used here, since atomic_thread_fence_seq_cst()
123 * will become compler fence on UP kernel.
127 if (msg->header.message_flags.u.message_pending) {
129 * This will cause message queue rescan to possibly
130 * deliver another msg from the hypervisor
132 wrmsr(HV_X64_MSR_EOM, 0);
138 * @brief Interrupt filter routine for VMBUS.
140 * The purpose of this routine is to determine the type of VMBUS protocol
141 * message to process - an event or a channel message.
144 hv_vmbus_isr(struct trapframe *frame)
147 hv_vmbus_message* msg;
148 hv_vmbus_synic_event_flags* event;
151 cpu = PCPU_GET(cpuid);
154 * The Windows team has advised that we check for events
155 * before checking for messages. This is the way they do it
156 * in Windows when running as a guest in Hyper-V
159 page_addr = hv_vmbus_g_context.syn_ic_event_page[cpu];
160 event = (hv_vmbus_synic_event_flags*)
161 page_addr + HV_VMBUS_MESSAGE_SINT;
163 if ((hv_vmbus_protocal_version == HV_VMBUS_VERSION_WS2008) ||
164 (hv_vmbus_protocal_version == HV_VMBUS_VERSION_WIN7)) {
165 /* Since we are a child, we only need to check bit 0 */
166 if (synch_test_and_clear_bit(0, &event->flags32[0])) {
167 hv_vmbus_on_events(cpu);
171 * On host with Win8 or above, we can directly look at
172 * the event page. If bit n is set, we have an interrupt
173 * on the channel with id n.
174 * Directly schedule the event software interrupt on
177 hv_vmbus_on_events(cpu);
180 /* Check if there are actual msgs to be process */
181 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
182 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_TIMER_SINT;
184 /* we call eventtimer process the message */
185 if (msg->header.message_type == HV_MESSAGE_TIMER_EXPIRED) {
186 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
188 /* call intrrupt handler of event timer */
192 * Make sure the write to message_type (ie set to
193 * HV_MESSAGE_TYPE_NONE) happens before we read the
194 * message_pending and EOMing. Otherwise, the EOMing will
195 * not deliver any more messages
196 * since there is no empty slot
199 * mb() is used here, since atomic_thread_fence_seq_cst()
200 * will become compler fence on UP kernel.
204 if (msg->header.message_flags.u.message_pending) {
206 * This will cause message queue rescan to possibly
207 * deliver another msg from the hypervisor
209 wrmsr(HV_X64_MSR_EOM, 0);
213 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
214 if (msg->header.message_type != HV_MESSAGE_TYPE_NONE) {
215 taskqueue_enqueue(hv_vmbus_g_context.hv_msg_tq[cpu],
216 &hv_vmbus_g_context.hv_msg_task[cpu]);
219 return (FILTER_HANDLED);
222 u_long *hv_vmbus_intr_cpu[MAXCPU];
225 hv_vector_handler(struct trapframe *trap_frame)
230 * Disable preemption.
235 * Do a little interrupt counting.
237 cpu = PCPU_GET(cpuid);
238 (*hv_vmbus_intr_cpu[cpu])++;
240 hv_vmbus_isr(trap_frame);
255 struct hv_device *child_dev_ctx = device_get_ivars(child);
259 case HV_VMBUS_IVAR_TYPE:
260 *result = (uintptr_t) &child_dev_ctx->class_id;
262 case HV_VMBUS_IVAR_INSTANCE:
263 *result = (uintptr_t) &child_dev_ctx->device_id;
265 case HV_VMBUS_IVAR_DEVCTX:
266 *result = (uintptr_t) child_dev_ctx;
268 case HV_VMBUS_IVAR_NODE:
269 *result = (uintptr_t) child_dev_ctx->device;
284 case HV_VMBUS_IVAR_TYPE:
285 case HV_VMBUS_IVAR_INSTANCE:
286 case HV_VMBUS_IVAR_DEVCTX:
287 case HV_VMBUS_IVAR_NODE:
295 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
298 struct hv_device *dev_ctx = device_get_ivars(child);
300 strlcat(buf, "classid=", buflen);
301 snprintf_hv_guid(guidbuf, sizeof(guidbuf), &dev_ctx->class_id);
302 strlcat(buf, guidbuf, buflen);
304 strlcat(buf, " deviceid=", buflen);
305 snprintf_hv_guid(guidbuf, sizeof(guidbuf), &dev_ctx->device_id);
306 strlcat(buf, guidbuf, buflen);
312 hv_vmbus_child_device_create(
315 hv_vmbus_channel* channel)
317 hv_device* child_dev;
320 * Allocate the new child device
322 child_dev = malloc(sizeof(hv_device), M_DEVBUF,
325 child_dev->channel = channel;
326 memcpy(&child_dev->class_id, &type, sizeof(hv_guid));
327 memcpy(&child_dev->device_id, &instance, sizeof(hv_guid));
333 snprintf_hv_guid(char *buf, size_t sz, const hv_guid *guid)
336 const unsigned char *d = guid->data;
338 cnt = snprintf(buf, sz,
339 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
340 d[3], d[2], d[1], d[0], d[5], d[4], d[7], d[6],
341 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
346 hv_vmbus_child_device_register(struct hv_device *child_dev)
353 snprintf_hv_guid(name, sizeof(name), &child_dev->class_id);
354 printf("VMBUS: Class ID: %s\n", name);
357 child = device_add_child(vmbus_devp, NULL, -1);
358 child_dev->device = child;
359 device_set_ivars(child, child_dev);
362 ret = device_probe_and_attach(child);
369 hv_vmbus_child_device_unregister(struct hv_device *child_dev)
373 * XXXKYS: Ensure that this is the opposite of
377 ret = device_delete_child(vmbus_devp, child_dev->device);
383 vmbus_probe(device_t dev) {
384 if (ACPI_ID_PROBE(device_get_parent(dev), dev, vmbus_ids) == NULL ||
385 device_get_unit(dev) != 0)
388 device_set_desc(dev, "Vmbus Devices");
390 return (BUS_PROBE_DEFAULT);
394 extern inthand_t IDTVEC(rsvd), IDTVEC(hv_vmbus_callback);
397 * @brief Find a free IDT slot and setup the interrupt handler.
400 vmbus_vector_alloc(void)
404 struct gate_descriptor *ip;
407 * Search backwards form the highest IDT vector available for use
408 * as vmbus channel callback vector. We install 'hv_vmbus_callback'
409 * handler at that vector and use it to interrupt vcpus.
411 vector = APIC_SPURIOUS_INT;
412 while (--vector >= APIC_IPI_INTS) {
414 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
415 if (func == (uintptr_t)&IDTVEC(rsvd)) {
417 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYS386IGT,
418 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
420 setidt(vector , IDTVEC(hv_vmbus_callback), SDT_SYSIGT,
431 * @brief Restore the IDT slot to rsvd.
434 vmbus_vector_free(int vector)
437 struct gate_descriptor *ip;
442 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
443 ("invalid vector %d", vector));
446 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
447 KASSERT(func == (uintptr_t)&IDTVEC(hv_vmbus_callback),
448 ("invalid vector %d", vector));
450 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
456 vmbus_vector_alloc(void)
462 vmbus_vector_free(int vector)
469 vmbus_cpuset_setthread_task(void *xmask, int pending __unused)
471 cpuset_t *mask = xmask;
474 error = cpuset_setthread(curthread->td_tid, mask);
476 panic("curthread=%ju: can't pin; error=%d",
477 (uintmax_t)curthread->td_tid, error);
482 * @brief Main vmbus driver initialization routine.
485 * - initialize the vmbus driver context
486 * - setup various driver entry points
487 * - invoke the vmbus hv main init routine
488 * - get the irq resource
489 * - invoke the vmbus to add the vmbus root device
490 * - setup the vmbus root device
491 * - retrieve the channel offers
497 char buf[MAXCOMLEN + 1];
505 ret = hv_vmbus_init();
509 printf("Error VMBUS: Hypervisor Initialization Failed!\n");
514 * Find a free IDT slot for vmbus callback.
516 hv_vmbus_g_context.hv_cb_vector = vmbus_vector_alloc();
518 if (hv_vmbus_g_context.hv_cb_vector == 0) {
520 printf("Error VMBUS: Cannot find free IDT slot for "
521 "vmbus callback!\n");
526 printf("VMBUS: vmbus callback vector %d\n",
527 hv_vmbus_g_context.hv_cb_vector);
530 * Notify the hypervisor of our vector.
532 setup_args.vector = hv_vmbus_g_context.hv_cb_vector;
535 snprintf(buf, sizeof(buf), "cpu%d:hyperv", j);
536 intrcnt_add(buf, &hv_vmbus_intr_cpu[j]);
538 for (i = 0; i < 2; i++)
539 setup_args.page_buffers[2 * j + i] = NULL;
546 struct task cpuset_task;
549 * Setup taskqueue to handle events
551 hv_vmbus_g_context.hv_event_queue[j] = taskqueue_create_fast("hyperv event", M_WAITOK,
552 taskqueue_thread_enqueue, &hv_vmbus_g_context.hv_event_queue[j]);
553 taskqueue_start_threads(&hv_vmbus_g_context.hv_event_queue[j], 1, PI_NET,
556 CPU_SETOF(j, &cpu_mask);
557 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task, &cpu_mask);
558 taskqueue_enqueue(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
559 taskqueue_drain(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
562 * Setup per-cpu tasks and taskqueues to handle msg.
564 hv_vmbus_g_context.hv_msg_tq[j] = taskqueue_create_fast(
565 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
566 &hv_vmbus_g_context.hv_msg_tq[j]);
567 taskqueue_start_threads(&hv_vmbus_g_context.hv_msg_tq[j], 1, PI_NET,
569 TASK_INIT(&hv_vmbus_g_context.hv_msg_task[j], 0,
570 vmbus_msg_swintr, (void *)(long)j);
572 CPU_SETOF(j, &cpu_mask);
573 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task, &cpu_mask);
574 taskqueue_enqueue(hv_vmbus_g_context.hv_msg_tq[j], &cpuset_task);
575 taskqueue_drain(hv_vmbus_g_context.hv_msg_tq[j], &cpuset_task);
578 * Prepare the per cpu msg and event pages to be called on each cpu.
580 for(i = 0; i < 2; i++) {
581 setup_args.page_buffers[2 * j + i] =
582 malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
587 printf("VMBUS: Calling smp_rendezvous, smp_started = %d\n",
590 smp_rendezvous(NULL, hv_vmbus_synic_init, NULL, &setup_args);
593 * Connect to VMBus in the root partition
595 ret = hv_vmbus_connect();
600 hv_vmbus_request_channel_offers();
605 * Free pages alloc'ed
607 for (n = 0; n < 2 * MAXCPU; n++)
608 if (setup_args.page_buffers[n] != NULL)
609 free(setup_args.page_buffers[n], M_DEVBUF);
612 * remove swi and vmbus callback vector;
615 if (hv_vmbus_g_context.hv_event_queue[j] != NULL) {
616 taskqueue_free(hv_vmbus_g_context.hv_event_queue[j]);
617 hv_vmbus_g_context.hv_event_queue[j] = NULL;
621 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
630 vmbus_attach(device_t dev)
633 device_printf(dev, "VMBUS: attach dev: %p\n", dev);
637 * If the system has already booted and thread
638 * scheduling is possible indicated by the global
639 * cold set to zero, we just call the driver
640 * initialization directly.
651 if (vm_guest != VM_GUEST_HV)
655 * If the system has already booted and thread
656 * scheduling is possible, as indicated by the
657 * global cold set to zero, we just call the driver
658 * initialization directly.
669 hv_vmbus_release_unattached_channels();
670 hv_vmbus_disconnect();
672 smp_rendezvous(NULL, hv_vmbus_synic_cleanup, NULL, NULL);
674 for(i = 0; i < 2 * MAXCPU; i++) {
675 if (setup_args.page_buffers[i] != 0)
676 free(setup_args.page_buffers[i], M_DEVBUF);
683 if (hv_vmbus_g_context.hv_event_queue[i] != NULL) {
684 taskqueue_free(hv_vmbus_g_context.hv_event_queue[i]);
685 hv_vmbus_g_context.hv_event_queue[i] = NULL;
689 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
701 vmbus_detach(device_t dev)
711 printf("VMBUS: load\n");
715 vmbus_mod_unload(void)
718 printf("VMBUS: unload\n");
722 vmbus_modevent(module_t mod, int what, void *arg)
737 static device_method_t vmbus_methods[] = {
738 /** Device interface */
739 DEVMETHOD(device_probe, vmbus_probe),
740 DEVMETHOD(device_attach, vmbus_attach),
741 DEVMETHOD(device_detach, vmbus_detach),
742 DEVMETHOD(device_shutdown, bus_generic_shutdown),
743 DEVMETHOD(device_suspend, bus_generic_suspend),
744 DEVMETHOD(device_resume, bus_generic_resume),
747 DEVMETHOD(bus_add_child, bus_generic_add_child),
748 DEVMETHOD(bus_print_child, bus_generic_print_child),
749 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
750 DEVMETHOD(bus_write_ivar, vmbus_write_ivar),
751 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str),
755 static char driver_name[] = "vmbus";
756 static driver_t vmbus_driver = { driver_name, vmbus_methods,0, };
759 devclass_t vmbus_devclass;
761 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, vmbus_modevent, 0);
762 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
763 MODULE_VERSION(vmbus, 1);
765 /* We want to be started after SMP is initialized */
766 SYSINIT(vmb_init, SI_SUB_SMP + 1, SI_ORDER_FIRST, vmbus_init, NULL);