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 compiler 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;
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 hv_vmbus_on_events(cpu);
160 /* Check if there are actual msgs to be process */
161 page_addr = hv_vmbus_g_context.syn_ic_msg_page[cpu];
162 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_TIMER_SINT;
164 /* we call eventtimer process the message */
165 if (msg->header.message_type == HV_MESSAGE_TIMER_EXPIRED) {
166 msg->header.message_type = HV_MESSAGE_TYPE_NONE;
168 /* call intrrupt handler of event timer */
172 * Make sure the write to message_type (ie set to
173 * HV_MESSAGE_TYPE_NONE) happens before we read the
174 * message_pending and EOMing. Otherwise, the EOMing will
175 * not deliver any more messages
176 * since there is no empty slot
179 * mb() is used here, since atomic_thread_fence_seq_cst()
180 * will become compiler fence on UP kernel.
184 if (msg->header.message_flags.u.message_pending) {
186 * This will cause message queue rescan to possibly
187 * deliver another msg from the hypervisor
189 wrmsr(HV_X64_MSR_EOM, 0);
193 msg = (hv_vmbus_message*) page_addr + HV_VMBUS_MESSAGE_SINT;
194 if (msg->header.message_type != HV_MESSAGE_TYPE_NONE) {
195 taskqueue_enqueue(hv_vmbus_g_context.hv_msg_tq[cpu],
196 &hv_vmbus_g_context.hv_msg_task[cpu]);
199 return (FILTER_HANDLED);
202 u_long *hv_vmbus_intr_cpu[MAXCPU];
205 hv_vector_handler(struct trapframe *trap_frame)
210 * Disable preemption.
215 * Do a little interrupt counting.
217 cpu = PCPU_GET(cpuid);
218 (*hv_vmbus_intr_cpu[cpu])++;
220 hv_vmbus_isr(trap_frame);
235 struct hv_device *child_dev_ctx = device_get_ivars(child);
239 case HV_VMBUS_IVAR_TYPE:
240 *result = (uintptr_t) &child_dev_ctx->class_id;
242 case HV_VMBUS_IVAR_INSTANCE:
243 *result = (uintptr_t) &child_dev_ctx->device_id;
245 case HV_VMBUS_IVAR_DEVCTX:
246 *result = (uintptr_t) child_dev_ctx;
248 case HV_VMBUS_IVAR_NODE:
249 *result = (uintptr_t) child_dev_ctx->device;
264 case HV_VMBUS_IVAR_TYPE:
265 case HV_VMBUS_IVAR_INSTANCE:
266 case HV_VMBUS_IVAR_DEVCTX:
267 case HV_VMBUS_IVAR_NODE:
275 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
278 struct hv_device *dev_ctx = device_get_ivars(child);
280 strlcat(buf, "classid=", buflen);
281 snprintf_hv_guid(guidbuf, sizeof(guidbuf), &dev_ctx->class_id);
282 strlcat(buf, guidbuf, buflen);
284 strlcat(buf, " deviceid=", buflen);
285 snprintf_hv_guid(guidbuf, sizeof(guidbuf), &dev_ctx->device_id);
286 strlcat(buf, guidbuf, buflen);
292 hv_vmbus_child_device_create(
295 hv_vmbus_channel* channel)
297 hv_device* child_dev;
300 * Allocate the new child device
302 child_dev = malloc(sizeof(hv_device), M_DEVBUF,
305 child_dev->channel = channel;
306 memcpy(&child_dev->class_id, &type, sizeof(hv_guid));
307 memcpy(&child_dev->device_id, &instance, sizeof(hv_guid));
313 snprintf_hv_guid(char *buf, size_t sz, const hv_guid *guid)
316 const unsigned char *d = guid->data;
318 cnt = snprintf(buf, sz,
319 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
320 d[3], d[2], d[1], d[0], d[5], d[4], d[7], d[6],
321 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
326 hv_vmbus_child_device_register(struct hv_device *child_dev)
332 snprintf_hv_guid(name, sizeof(name), &child_dev->class_id);
333 printf("VMBUS: Class ID: %s\n", name);
336 child = device_add_child(vmbus_devp, NULL, -1);
337 child_dev->device = child;
338 device_set_ivars(child, child_dev);
344 hv_vmbus_child_device_unregister(struct hv_device *child_dev)
348 * XXXKYS: Ensure that this is the opposite of
352 ret = device_delete_child(vmbus_devp, child_dev->device);
358 vmbus_probe(device_t dev) {
359 if (ACPI_ID_PROBE(device_get_parent(dev), dev, vmbus_ids) == NULL ||
360 device_get_unit(dev) != 0)
363 device_set_desc(dev, "Vmbus Devices");
365 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);
428 vmbus_cpuset_setthread_task(void *xmask, int pending __unused)
430 cpuset_t *mask = xmask;
433 error = cpuset_setthread(curthread->td_tid, mask);
435 panic("curthread=%ju: can't pin; error=%d",
436 (uintmax_t)curthread->td_tid, error);
441 * @brief Main vmbus driver initialization routine.
444 * - initialize the vmbus driver context
445 * - setup various driver entry points
446 * - invoke the vmbus hv main init routine
447 * - get the irq resource
448 * - invoke the vmbus to add the vmbus root device
449 * - setup the vmbus root device
450 * - retrieve the channel offers
456 char buf[MAXCOMLEN + 1];
464 ret = hv_vmbus_init();
468 printf("Error VMBUS: Hypervisor Initialization Failed!\n");
473 * Find a free IDT slot for vmbus callback.
475 hv_vmbus_g_context.hv_cb_vector = vmbus_vector_alloc();
477 if (hv_vmbus_g_context.hv_cb_vector == 0) {
479 printf("Error VMBUS: Cannot find free IDT slot for "
480 "vmbus callback!\n");
485 printf("VMBUS: vmbus callback vector %d\n",
486 hv_vmbus_g_context.hv_cb_vector);
489 * Notify the hypervisor of our vector.
491 setup_args.vector = hv_vmbus_g_context.hv_cb_vector;
494 snprintf(buf, sizeof(buf), "cpu%d:hyperv", j);
495 intrcnt_add(buf, &hv_vmbus_intr_cpu[j]);
497 for (i = 0; i < 2; i++)
498 setup_args.page_buffers[2 * j + i] = NULL;
505 struct task cpuset_task;
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 taskqueue_start_threads(&hv_vmbus_g_context.hv_event_queue[j], 1, PI_NET,
515 CPU_SETOF(j, &cpu_mask);
516 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task, &cpu_mask);
517 taskqueue_enqueue(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
518 taskqueue_drain(hv_vmbus_g_context.hv_event_queue[j], &cpuset_task);
521 * Setup per-cpu tasks and taskqueues to handle msg.
523 hv_vmbus_g_context.hv_msg_tq[j] = taskqueue_create_fast(
524 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
525 &hv_vmbus_g_context.hv_msg_tq[j]);
526 taskqueue_start_threads(&hv_vmbus_g_context.hv_msg_tq[j], 1, PI_NET,
528 TASK_INIT(&hv_vmbus_g_context.hv_msg_task[j], 0,
529 vmbus_msg_swintr, (void *)(long)j);
531 CPU_SETOF(j, &cpu_mask);
532 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task, &cpu_mask);
533 taskqueue_enqueue(hv_vmbus_g_context.hv_msg_tq[j], &cpuset_task);
534 taskqueue_drain(hv_vmbus_g_context.hv_msg_tq[j], &cpuset_task);
537 * Prepare the per cpu msg and event pages to be called on each cpu.
539 for(i = 0; i < 2; i++) {
540 setup_args.page_buffers[2 * j + i] =
541 malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
546 printf("VMBUS: Calling smp_rendezvous, smp_started = %d\n",
549 smp_rendezvous(NULL, hv_vmbus_synic_init, NULL, &setup_args);
552 * Connect to VMBus in the root partition
554 ret = hv_vmbus_connect();
559 hv_vmbus_request_channel_offers();
562 bus_generic_attach(vmbus_devp);
563 device_printf(vmbus_devp, "device scan, probe and attach done\n");
569 * Free pages alloc'ed
571 for (n = 0; n < 2 * MAXCPU; n++)
572 if (setup_args.page_buffers[n] != NULL)
573 free(setup_args.page_buffers[n], M_DEVBUF);
576 * remove swi and vmbus callback vector;
579 if (hv_vmbus_g_context.hv_event_queue[j] != NULL) {
580 taskqueue_free(hv_vmbus_g_context.hv_event_queue[j]);
581 hv_vmbus_g_context.hv_event_queue[j] = NULL;
585 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
594 vmbus_attach(device_t dev)
597 device_printf(dev, "VMBUS: attach dev: %p\n", dev);
601 * If the system has already booted and thread
602 * scheduling is possible indicated by the global
603 * cold set to zero, we just call the driver
604 * initialization directly.
615 if (vm_guest != VM_GUEST_HV)
619 * If the system has already booted and thread
620 * scheduling is possible, as indicated by the
621 * global cold set to zero, we just call the driver
622 * initialization directly.
633 hv_vmbus_release_unattached_channels();
634 hv_vmbus_disconnect();
636 smp_rendezvous(NULL, hv_vmbus_synic_cleanup, NULL, NULL);
638 for(i = 0; i < 2 * MAXCPU; i++) {
639 if (setup_args.page_buffers[i] != NULL)
640 free(setup_args.page_buffers[i], M_DEVBUF);
647 if (hv_vmbus_g_context.hv_event_queue[i] != NULL) {
648 taskqueue_free(hv_vmbus_g_context.hv_event_queue[i]);
649 hv_vmbus_g_context.hv_event_queue[i] = NULL;
653 vmbus_vector_free(hv_vmbus_g_context.hv_cb_vector);
665 vmbus_detach(device_t dev)
675 printf("VMBUS: load\n");
679 vmbus_mod_unload(void)
682 printf("VMBUS: unload\n");
686 vmbus_modevent(module_t mod, int what, void *arg)
701 static device_method_t vmbus_methods[] = {
702 /** Device interface */
703 DEVMETHOD(device_probe, vmbus_probe),
704 DEVMETHOD(device_attach, vmbus_attach),
705 DEVMETHOD(device_detach, vmbus_detach),
706 DEVMETHOD(device_shutdown, bus_generic_shutdown),
707 DEVMETHOD(device_suspend, bus_generic_suspend),
708 DEVMETHOD(device_resume, bus_generic_resume),
711 DEVMETHOD(bus_add_child, bus_generic_add_child),
712 DEVMETHOD(bus_print_child, bus_generic_print_child),
713 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
714 DEVMETHOD(bus_write_ivar, vmbus_write_ivar),
715 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str),
719 static char driver_name[] = "vmbus";
720 static driver_t vmbus_driver = { driver_name, vmbus_methods,0, };
723 devclass_t vmbus_devclass;
725 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, vmbus_modevent, 0);
726 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
727 MODULE_VERSION(vmbus, 1);
729 /* We want to be started after SMP is initialized */
730 SYSINIT(vmb_init, SI_SUB_SMP + 1, SI_ORDER_FIRST, vmbus_init, NULL);