2 * Copyright (c) 2009-2012,2016 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 <dev/hyperv/vmbus/hv_vmbus_priv.h>
64 #include <dev/hyperv/vmbus/hyperv_reg.h>
65 #include <dev/hyperv/vmbus/hyperv_var.h>
66 #include <dev/hyperv/vmbus/vmbus_reg.h>
67 #include <dev/hyperv/vmbus/vmbus_var.h>
69 #include <contrib/dev/acpica/include/acpi.h>
73 #define VMBUS_GPADL_START 0xe1e10
76 struct hypercall_postmsg_in *mh_inprm;
77 struct hypercall_postmsg_in mh_inprm_save;
78 struct hyperv_dma mh_inprm_dma;
80 struct vmbus_message *mh_resp;
81 struct vmbus_message mh_resp0;
84 struct vmbus_msghc_ctx {
85 struct vmbus_msghc *mhc_free;
86 struct mtx mhc_free_lock;
89 struct vmbus_msghc *mhc_active;
90 struct mtx mhc_active_lock;
93 #define VMBUS_MSGHC_CTXF_DESTROY 0x0001
95 static int vmbus_init(struct vmbus_softc *);
96 static int vmbus_connect(struct vmbus_softc *, uint32_t);
97 static int vmbus_req_channels(struct vmbus_softc *sc);
98 static void vmbus_disconnect(struct vmbus_softc *);
99 static int vmbus_scan(struct vmbus_softc *);
100 static void vmbus_scan_wait(struct vmbus_softc *);
101 static void vmbus_scan_newchan(struct vmbus_softc *);
102 static void vmbus_scan_newdev(struct vmbus_softc *);
103 static void vmbus_scan_done(struct vmbus_softc *,
104 const struct vmbus_message *);
105 static void vmbus_chanmsg_handle(struct vmbus_softc *,
106 const struct vmbus_message *);
108 static int vmbus_sysctl_version(SYSCTL_HANDLER_ARGS);
110 static struct vmbus_msghc_ctx *vmbus_msghc_ctx_create(bus_dma_tag_t);
111 static void vmbus_msghc_ctx_destroy(
112 struct vmbus_msghc_ctx *);
113 static void vmbus_msghc_ctx_free(struct vmbus_msghc_ctx *);
114 static struct vmbus_msghc *vmbus_msghc_alloc(bus_dma_tag_t);
115 static void vmbus_msghc_free(struct vmbus_msghc *);
116 static struct vmbus_msghc *vmbus_msghc_get1(struct vmbus_msghc_ctx *,
119 struct vmbus_softc *vmbus_sc;
121 extern inthand_t IDTVEC(rsvd), IDTVEC(vmbus_isr);
123 static const uint32_t vmbus_version[] = {
124 VMBUS_VERSION_WIN8_1,
130 static const vmbus_chanmsg_proc_t
131 vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = {
132 VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done),
133 VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP)
136 static struct vmbus_msghc *
137 vmbus_msghc_alloc(bus_dma_tag_t parent_dtag)
139 struct vmbus_msghc *mh;
141 mh = malloc(sizeof(*mh), M_DEVBUF, M_WAITOK | M_ZERO);
143 mh->mh_inprm = hyperv_dmamem_alloc(parent_dtag,
144 HYPERCALL_PARAM_ALIGN, 0, HYPERCALL_POSTMSGIN_SIZE,
145 &mh->mh_inprm_dma, BUS_DMA_WAITOK);
146 if (mh->mh_inprm == NULL) {
154 vmbus_msghc_free(struct vmbus_msghc *mh)
156 hyperv_dmamem_free(&mh->mh_inprm_dma, mh->mh_inprm);
161 vmbus_msghc_ctx_free(struct vmbus_msghc_ctx *mhc)
163 KASSERT(mhc->mhc_active == NULL, ("still have active msg hypercall"));
164 KASSERT(mhc->mhc_free == NULL, ("still have hypercall msg"));
166 mtx_destroy(&mhc->mhc_free_lock);
167 mtx_destroy(&mhc->mhc_active_lock);
171 static struct vmbus_msghc_ctx *
172 vmbus_msghc_ctx_create(bus_dma_tag_t parent_dtag)
174 struct vmbus_msghc_ctx *mhc;
176 mhc = malloc(sizeof(*mhc), M_DEVBUF, M_WAITOK | M_ZERO);
177 mtx_init(&mhc->mhc_free_lock, "vmbus msghc free", NULL, MTX_DEF);
178 mtx_init(&mhc->mhc_active_lock, "vmbus msghc act", NULL, MTX_DEF);
180 mhc->mhc_free = vmbus_msghc_alloc(parent_dtag);
181 if (mhc->mhc_free == NULL) {
182 vmbus_msghc_ctx_free(mhc);
188 static struct vmbus_msghc *
189 vmbus_msghc_get1(struct vmbus_msghc_ctx *mhc, uint32_t dtor_flag)
191 struct vmbus_msghc *mh;
193 mtx_lock(&mhc->mhc_free_lock);
195 while ((mhc->mhc_flags & dtor_flag) == 0 && mhc->mhc_free == NULL) {
196 mtx_sleep(&mhc->mhc_free, &mhc->mhc_free_lock, 0,
199 if (mhc->mhc_flags & dtor_flag) {
200 /* Being destroyed */
204 KASSERT(mh != NULL, ("no free hypercall msg"));
205 KASSERT(mh->mh_resp == NULL,
206 ("hypercall msg has pending response"));
207 mhc->mhc_free = NULL;
210 mtx_unlock(&mhc->mhc_free_lock);
216 vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize)
218 struct hypercall_postmsg_in *inprm;
220 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
221 panic("invalid data size %zu", dsize);
223 inprm = mh->mh_inprm;
224 memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE);
225 inprm->hc_connid = VMBUS_CONNID_MESSAGE;
226 inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL;
227 inprm->hc_dsize = dsize;
231 vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize)
233 struct vmbus_msghc *mh;
235 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
236 panic("invalid data size %zu", dsize);
238 mh = vmbus_msghc_get1(sc->vmbus_msg_hc, VMBUS_MSGHC_CTXF_DESTROY);
242 vmbus_msghc_reset(mh, dsize);
247 vmbus_msghc_put(struct vmbus_softc *sc, struct vmbus_msghc *mh)
249 struct vmbus_msghc_ctx *mhc = sc->vmbus_msg_hc;
251 KASSERT(mhc->mhc_active == NULL, ("msg hypercall is active"));
254 mtx_lock(&mhc->mhc_free_lock);
255 KASSERT(mhc->mhc_free == NULL, ("has free hypercall msg"));
257 mtx_unlock(&mhc->mhc_free_lock);
258 wakeup(&mhc->mhc_free);
262 vmbus_msghc_dataptr(struct vmbus_msghc *mh)
264 return mh->mh_inprm->hc_data;
268 vmbus_msghc_ctx_destroy(struct vmbus_msghc_ctx *mhc)
270 struct vmbus_msghc *mh;
272 mtx_lock(&mhc->mhc_free_lock);
273 mhc->mhc_flags |= VMBUS_MSGHC_CTXF_DESTROY;
274 mtx_unlock(&mhc->mhc_free_lock);
275 wakeup(&mhc->mhc_free);
277 mh = vmbus_msghc_get1(mhc, 0);
279 panic("can't get msghc");
281 vmbus_msghc_free(mh);
282 vmbus_msghc_ctx_free(mhc);
286 vmbus_msghc_exec_noresult(struct vmbus_msghc *mh)
288 sbintime_t time = SBT_1MS;
292 * Save the input parameter so that we could restore the input
293 * parameter if the Hypercall failed.
296 * Is this really necessary?! i.e. Will the Hypercall ever
297 * overwrite the input parameter?
299 memcpy(&mh->mh_inprm_save, mh->mh_inprm, HYPERCALL_POSTMSGIN_SIZE);
302 * In order to cope with transient failures, e.g. insufficient
303 * resources on host side, we retry the post message Hypercall
304 * several times. 20 retries seem sufficient.
306 #define HC_RETRY_MAX 20
308 for (i = 0; i < HC_RETRY_MAX; ++i) {
311 status = hypercall_post_message(mh->mh_inprm_dma.hv_paddr);
312 if (status == HYPERCALL_STATUS_SUCCESS)
315 pause_sbt("hcpmsg", time, 0, C_HARDCLOCK);
316 if (time < SBT_1S * 2)
319 /* Restore input parameter and try again */
320 memcpy(mh->mh_inprm, &mh->mh_inprm_save,
321 HYPERCALL_POSTMSGIN_SIZE);
330 vmbus_msghc_exec(struct vmbus_softc *sc, struct vmbus_msghc *mh)
332 struct vmbus_msghc_ctx *mhc = sc->vmbus_msg_hc;
335 KASSERT(mh->mh_resp == NULL, ("hypercall msg has pending response"));
337 mtx_lock(&mhc->mhc_active_lock);
338 KASSERT(mhc->mhc_active == NULL, ("pending active msg hypercall"));
339 mhc->mhc_active = mh;
340 mtx_unlock(&mhc->mhc_active_lock);
342 error = vmbus_msghc_exec_noresult(mh);
344 mtx_lock(&mhc->mhc_active_lock);
345 KASSERT(mhc->mhc_active == mh, ("msghc mismatch"));
346 mhc->mhc_active = NULL;
347 mtx_unlock(&mhc->mhc_active_lock);
352 const struct vmbus_message *
353 vmbus_msghc_wait_result(struct vmbus_softc *sc, struct vmbus_msghc *mh)
355 struct vmbus_msghc_ctx *mhc = sc->vmbus_msg_hc;
357 mtx_lock(&mhc->mhc_active_lock);
359 KASSERT(mhc->mhc_active == mh, ("msghc mismatch"));
360 while (mh->mh_resp == NULL) {
361 mtx_sleep(&mhc->mhc_active, &mhc->mhc_active_lock, 0,
364 mhc->mhc_active = NULL;
366 mtx_unlock(&mhc->mhc_active_lock);
372 vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg)
374 struct vmbus_msghc_ctx *mhc = sc->vmbus_msg_hc;
375 struct vmbus_msghc *mh;
377 mtx_lock(&mhc->mhc_active_lock);
379 mh = mhc->mhc_active;
380 KASSERT(mh != NULL, ("no pending msg hypercall"));
381 memcpy(&mh->mh_resp0, msg, sizeof(mh->mh_resp0));
382 mh->mh_resp = &mh->mh_resp0;
384 mtx_unlock(&mhc->mhc_active_lock);
385 wakeup(&mhc->mhc_active);
389 vmbus_gpadl_alloc(struct vmbus_softc *sc)
391 return atomic_fetchadd_int(&sc->vmbus_gpadl, 1);
395 vmbus_connect(struct vmbus_softc *sc, uint32_t version)
397 struct vmbus_chanmsg_connect *req;
398 const struct vmbus_message *msg;
399 struct vmbus_msghc *mh;
402 mh = vmbus_msghc_get(sc, sizeof(*req));
406 req = vmbus_msghc_dataptr(mh);
407 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT;
408 req->chm_ver = version;
409 req->chm_evtflags = sc->vmbus_evtflags_dma.hv_paddr;
410 req->chm_mnf1 = sc->vmbus_mnf1_dma.hv_paddr;
411 req->chm_mnf2 = sc->vmbus_mnf2_dma.hv_paddr;
413 error = vmbus_msghc_exec(sc, mh);
415 vmbus_msghc_put(sc, mh);
419 msg = vmbus_msghc_wait_result(sc, mh);
420 done = ((const struct vmbus_chanmsg_connect_resp *)
421 msg->msg_data)->chm_done;
423 vmbus_msghc_put(sc, mh);
425 return (done ? 0 : EOPNOTSUPP);
429 vmbus_init(struct vmbus_softc *sc)
433 for (i = 0; i < nitems(vmbus_version); ++i) {
436 error = vmbus_connect(sc, vmbus_version[i]);
438 sc->vmbus_version = vmbus_version[i];
439 device_printf(sc->vmbus_dev, "version %u.%u\n",
440 VMBUS_VERSION_MAJOR(sc->vmbus_version),
441 VMBUS_VERSION_MINOR(sc->vmbus_version));
449 vmbus_disconnect(struct vmbus_softc *sc)
451 struct vmbus_chanmsg_disconnect *req;
452 struct vmbus_msghc *mh;
455 mh = vmbus_msghc_get(sc, sizeof(*req));
457 device_printf(sc->vmbus_dev,
458 "can not get msg hypercall for disconnect\n");
462 req = vmbus_msghc_dataptr(mh);
463 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT;
465 error = vmbus_msghc_exec_noresult(mh);
466 vmbus_msghc_put(sc, mh);
469 device_printf(sc->vmbus_dev,
470 "disconnect msg hypercall failed\n");
475 vmbus_req_channels(struct vmbus_softc *sc)
477 struct vmbus_chanmsg_chrequest *req;
478 struct vmbus_msghc *mh;
481 mh = vmbus_msghc_get(sc, sizeof(*req));
485 req = vmbus_msghc_dataptr(mh);
486 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST;
488 error = vmbus_msghc_exec_noresult(mh);
489 vmbus_msghc_put(sc, mh);
495 vmbus_scan_newchan(struct vmbus_softc *sc)
497 mtx_lock(&sc->vmbus_scan_lock);
498 if ((sc->vmbus_scan_chcnt & VMBUS_SCAN_CHCNT_DONE) == 0)
499 sc->vmbus_scan_chcnt++;
500 mtx_unlock(&sc->vmbus_scan_lock);
504 vmbus_scan_done(struct vmbus_softc *sc,
505 const struct vmbus_message *msg __unused)
507 mtx_lock(&sc->vmbus_scan_lock);
508 sc->vmbus_scan_chcnt |= VMBUS_SCAN_CHCNT_DONE;
509 mtx_unlock(&sc->vmbus_scan_lock);
510 wakeup(&sc->vmbus_scan_chcnt);
514 vmbus_scan_newdev(struct vmbus_softc *sc)
516 mtx_lock(&sc->vmbus_scan_lock);
517 sc->vmbus_scan_devcnt++;
518 mtx_unlock(&sc->vmbus_scan_lock);
519 wakeup(&sc->vmbus_scan_devcnt);
523 vmbus_scan_wait(struct vmbus_softc *sc)
527 mtx_lock(&sc->vmbus_scan_lock);
528 while ((sc->vmbus_scan_chcnt & VMBUS_SCAN_CHCNT_DONE) == 0) {
529 mtx_sleep(&sc->vmbus_scan_chcnt, &sc->vmbus_scan_lock, 0,
532 chancnt = sc->vmbus_scan_chcnt & ~VMBUS_SCAN_CHCNT_DONE;
534 while (sc->vmbus_scan_devcnt != chancnt) {
535 mtx_sleep(&sc->vmbus_scan_devcnt, &sc->vmbus_scan_lock, 0,
538 mtx_unlock(&sc->vmbus_scan_lock);
542 vmbus_scan(struct vmbus_softc *sc)
547 * Start vmbus scanning.
549 error = vmbus_req_channels(sc);
551 device_printf(sc->vmbus_dev, "channel request failed: %d\n",
557 * Wait for all devices are added to vmbus.
562 * Identify, probe and attach.
564 bus_generic_probe(sc->vmbus_dev);
565 bus_generic_attach(sc->vmbus_dev);
568 device_printf(sc->vmbus_dev, "device scan, probe and attach "
575 vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg)
577 vmbus_chanmsg_proc_t msg_proc;
580 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
581 if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) {
582 device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
587 msg_proc = vmbus_chanmsg_handlers[msg_type];
588 if (msg_proc != NULL)
591 /* Channel specific processing */
592 vmbus_chan_msgproc(sc, msg);
596 vmbus_msg_task(void *xsc, int pending __unused)
598 struct vmbus_softc *sc = xsc;
599 volatile struct vmbus_message *msg;
601 msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE;
603 if (msg->msg_type == HYPERV_MSGTYPE_NONE) {
606 } else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) {
607 /* Channel message */
608 vmbus_chanmsg_handle(sc,
609 __DEVOLATILE(const struct vmbus_message *, msg));
612 msg->msg_type = HYPERV_MSGTYPE_NONE;
614 * Make sure the write to msg_type (i.e. set to
615 * HYPERV_MSGTYPE_NONE) happens before we read the
616 * msg_flags and EOMing. Otherwise, the EOMing will
617 * not deliver any more messages since there is no
621 * mb() is used here, since atomic_thread_fence_seq_cst()
622 * will become compiler fence on UP kernel.
625 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
627 * This will cause message queue rescan to possibly
628 * deliver another msg from the hypervisor
630 wrmsr(MSR_HV_EOM, 0);
636 vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu)
638 volatile struct vmbus_message *msg;
639 struct vmbus_message *msg_base;
641 msg_base = VMBUS_PCPU_GET(sc, message, cpu);
646 * TODO: move this to independent IDT vector.
648 msg = msg_base + VMBUS_SINT_TIMER;
649 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
650 msg->msg_type = HYPERV_MSGTYPE_NONE;
652 vmbus_et_intr(frame);
655 * Make sure the write to msg_type (i.e. set to
656 * HYPERV_MSGTYPE_NONE) happens before we read the
657 * msg_flags and EOMing. Otherwise, the EOMing will
658 * not deliver any more messages since there is no
662 * mb() is used here, since atomic_thread_fence_seq_cst()
663 * will become compiler fence on UP kernel.
666 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
668 * This will cause message queue rescan to possibly
669 * deliver another msg from the hypervisor
671 wrmsr(MSR_HV_EOM, 0);
676 * Check events. Hot path for network and storage I/O data; high rate.
679 * As recommended by the Windows guest fellows, we check events before
682 sc->vmbus_event_proc(sc, cpu);
685 * Check messages. Mainly management stuffs; ultra low rate.
687 msg = msg_base + VMBUS_SINT_MESSAGE;
688 if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
689 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
690 VMBUS_PCPU_PTR(sc, message_task, cpu));
693 return (FILTER_HANDLED);
697 vmbus_handle_intr(struct trapframe *trap_frame)
699 struct vmbus_softc *sc = vmbus_get_softc();
703 * Disable preemption.
708 * Do a little interrupt counting.
710 (*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++;
712 vmbus_handle_intr1(sc, trap_frame, cpu);
721 vmbus_synic_setup(void *xsc)
723 struct vmbus_softc *sc = xsc;
728 if (hyperv_features & CPUID_HV_MSR_VP_INDEX) {
729 /* Save virtual processor id. */
730 VMBUS_PCPU_GET(sc, vcpuid, cpu) = rdmsr(MSR_HV_VP_INDEX);
732 /* Set virtual processor id to 0 for compatibility. */
733 VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0;
737 * Setup the SynIC message.
739 orig = rdmsr(MSR_HV_SIMP);
740 val = MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK) |
741 ((VMBUS_PCPU_GET(sc, message_dma.hv_paddr, cpu) >> PAGE_SHIFT) <<
742 MSR_HV_SIMP_PGSHIFT);
743 wrmsr(MSR_HV_SIMP, val);
746 * Setup the SynIC event flags.
748 orig = rdmsr(MSR_HV_SIEFP);
749 val = MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK) |
750 ((VMBUS_PCPU_GET(sc, event_flags_dma.hv_paddr, cpu)
751 >> PAGE_SHIFT) << MSR_HV_SIEFP_PGSHIFT);
752 wrmsr(MSR_HV_SIEFP, val);
756 * Configure and unmask SINT for message and event flags.
758 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
760 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
761 (orig & MSR_HV_SINT_RSVD_MASK);
765 * Configure and unmask SINT for timer.
767 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
769 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
770 (orig & MSR_HV_SINT_RSVD_MASK);
774 * All done; enable SynIC.
776 orig = rdmsr(MSR_HV_SCONTROL);
777 val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK);
778 wrmsr(MSR_HV_SCONTROL, val);
782 vmbus_synic_teardown(void *arg)
790 orig = rdmsr(MSR_HV_SCONTROL);
791 wrmsr(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK));
794 * Mask message and event flags SINT.
796 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
798 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
803 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
805 wrmsr(sint, orig | MSR_HV_SINT_MASKED);
808 * Teardown SynIC message.
810 orig = rdmsr(MSR_HV_SIMP);
811 wrmsr(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK));
814 * Teardown SynIC event flags.
816 orig = rdmsr(MSR_HV_SIEFP);
817 wrmsr(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK));
821 vmbus_dma_alloc(struct vmbus_softc *sc)
823 bus_dma_tag_t parent_dtag;
827 parent_dtag = bus_get_dma_tag(sc->vmbus_dev);
832 * Per-cpu messages and event flags.
834 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
835 PAGE_SIZE, VMBUS_PCPU_PTR(sc, message_dma, cpu),
836 BUS_DMA_WAITOK | BUS_DMA_ZERO);
839 VMBUS_PCPU_GET(sc, message, cpu) = ptr;
841 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
842 PAGE_SIZE, VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
843 BUS_DMA_WAITOK | BUS_DMA_ZERO);
846 VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr;
849 evtflags = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
850 PAGE_SIZE, &sc->vmbus_evtflags_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
851 if (evtflags == NULL)
853 sc->vmbus_rx_evtflags = (u_long *)evtflags;
854 sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2));
855 sc->vmbus_evtflags = evtflags;
857 sc->vmbus_mnf1 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
858 PAGE_SIZE, &sc->vmbus_mnf1_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO);
859 if (sc->vmbus_mnf1 == NULL)
862 sc->vmbus_mnf2 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0,
863 sizeof(struct vmbus_mnf), &sc->vmbus_mnf2_dma,
864 BUS_DMA_WAITOK | BUS_DMA_ZERO);
865 if (sc->vmbus_mnf2 == NULL)
872 vmbus_dma_free(struct vmbus_softc *sc)
876 if (sc->vmbus_evtflags != NULL) {
877 hyperv_dmamem_free(&sc->vmbus_evtflags_dma, sc->vmbus_evtflags);
878 sc->vmbus_evtflags = NULL;
879 sc->vmbus_rx_evtflags = NULL;
880 sc->vmbus_tx_evtflags = NULL;
882 if (sc->vmbus_mnf1 != NULL) {
883 hyperv_dmamem_free(&sc->vmbus_mnf1_dma, sc->vmbus_mnf1);
884 sc->vmbus_mnf1 = NULL;
886 if (sc->vmbus_mnf2 != NULL) {
887 hyperv_dmamem_free(&sc->vmbus_mnf2_dma, sc->vmbus_mnf2);
888 sc->vmbus_mnf2 = NULL;
892 if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) {
894 VMBUS_PCPU_PTR(sc, message_dma, cpu),
895 VMBUS_PCPU_GET(sc, message, cpu));
896 VMBUS_PCPU_GET(sc, message, cpu) = NULL;
898 if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) {
900 VMBUS_PCPU_PTR(sc, event_flags_dma, cpu),
901 VMBUS_PCPU_GET(sc, event_flags, cpu));
902 VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL;
908 * @brief Find a free IDT slot and setup the interrupt handler.
911 vmbus_vector_alloc(void)
915 struct gate_descriptor *ip;
918 * Search backwards form the highest IDT vector available for use
919 * as vmbus channel callback vector. We install 'vmbus_isr'
920 * handler at that vector and use it to interrupt vcpus.
922 vector = APIC_SPURIOUS_INT;
923 while (--vector >= APIC_IPI_INTS) {
925 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
926 if (func == (uintptr_t)&IDTVEC(rsvd)) {
928 setidt(vector , IDTVEC(vmbus_isr), SDT_SYS386IGT,
929 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
931 setidt(vector , IDTVEC(vmbus_isr), SDT_SYSIGT,
942 * @brief Restore the IDT slot to rsvd.
945 vmbus_vector_free(int vector)
948 struct gate_descriptor *ip;
953 KASSERT(vector >= APIC_IPI_INTS && vector < APIC_SPURIOUS_INT,
954 ("invalid vector %d", vector));
957 func = ((long)ip->gd_hioffset << 16 | ip->gd_looffset);
958 KASSERT(func == (uintptr_t)&IDTVEC(vmbus_isr),
959 ("invalid vector %d", vector));
961 setidt(vector, IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
965 vmbus_cpuset_setthread_task(void *xmask, int pending __unused)
967 cpuset_t *mask = xmask;
970 error = cpuset_setthread(curthread->td_tid, mask);
972 panic("curthread=%ju: can't pin; error=%d",
973 (uintmax_t)curthread->td_tid, error);
978 vmbus_intr_setup(struct vmbus_softc *sc)
983 struct task cpuset_task;
984 char buf[MAXCOMLEN + 1];
987 /* Allocate an interrupt counter for Hyper-V interrupt */
988 snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu);
989 intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu));
992 * Setup taskqueue to handle events. Task will be per-
995 VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast(
996 "hyperv event", M_WAITOK, taskqueue_thread_enqueue,
997 VMBUS_PCPU_PTR(sc, event_tq, cpu));
998 taskqueue_start_threads(VMBUS_PCPU_PTR(sc, event_tq, cpu),
999 1, PI_NET, "hvevent%d", cpu);
1001 CPU_SETOF(cpu, &cpu_mask);
1002 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task,
1004 taskqueue_enqueue(VMBUS_PCPU_GET(sc, event_tq, cpu),
1006 taskqueue_drain(VMBUS_PCPU_GET(sc, event_tq, cpu),
1010 * Setup tasks and taskqueues to handle messages.
1012 VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast(
1013 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
1014 VMBUS_PCPU_PTR(sc, message_tq, cpu));
1015 taskqueue_start_threads(VMBUS_PCPU_PTR(sc, message_tq, cpu), 1,
1016 PI_NET, "hvmsg%d", cpu);
1017 TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0,
1018 vmbus_msg_task, sc);
1020 CPU_SETOF(cpu, &cpu_mask);
1021 TASK_INIT(&cpuset_task, 0, vmbus_cpuset_setthread_task,
1023 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
1025 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
1030 * All Hyper-V ISR required resources are setup, now let's find a
1031 * free IDT vector for Hyper-V ISR and set it up.
1033 sc->vmbus_idtvec = vmbus_vector_alloc();
1034 if (sc->vmbus_idtvec == 0) {
1035 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
1039 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
1046 vmbus_intr_teardown(struct vmbus_softc *sc)
1050 vmbus_vector_free(sc->vmbus_idtvec);
1053 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
1054 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
1055 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
1057 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
1058 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
1059 VMBUS_PCPU_PTR(sc, message_task, cpu));
1060 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
1061 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
1067 vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
1073 vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen)
1075 const struct hv_vmbus_channel *chan;
1076 char guidbuf[HYPERV_GUID_STRLEN];
1078 chan = vmbus_get_channel(child);
1080 /* Event timer device, which does not belong to a channel */
1084 strlcat(buf, "classid=", buflen);
1085 hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf));
1086 strlcat(buf, guidbuf, buflen);
1088 strlcat(buf, " deviceid=", buflen);
1089 hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf));
1090 strlcat(buf, guidbuf, buflen);
1096 vmbus_add_child(struct hv_vmbus_channel *chan)
1098 struct vmbus_softc *sc = chan->vmbus_sc;
1099 device_t parent = sc->vmbus_dev;
1102 /* New channel has been offered */
1103 vmbus_scan_newchan(sc);
1105 chan->ch_dev = device_add_child(parent, NULL, -1);
1106 if (chan->ch_dev == NULL) {
1107 device_printf(parent, "device_add_child for chan%u failed\n",
1112 device_set_ivars(chan->ch_dev, chan);
1115 /* New device has been/should be added to vmbus. */
1116 vmbus_scan_newdev(sc);
1121 vmbus_delete_child(struct hv_vmbus_channel *chan)
1125 if (chan->ch_dev == NULL) {
1126 /* Failed to add a device. */
1131 * XXXKYS: Ensure that this is the opposite of
1132 * device_add_child()
1135 error = device_delete_child(chan->vmbus_sc->vmbus_dev, chan->ch_dev);
1142 vmbus_sysctl_version(SYSCTL_HANDLER_ARGS)
1144 struct vmbus_softc *sc = arg1;
1147 snprintf(verstr, sizeof(verstr), "%u.%u",
1148 VMBUS_VERSION_MAJOR(sc->vmbus_version),
1149 VMBUS_VERSION_MINOR(sc->vmbus_version));
1150 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
1154 vmbus_get_version_method(device_t bus, device_t dev)
1156 struct vmbus_softc *sc = device_get_softc(bus);
1158 return sc->vmbus_version;
1162 vmbus_probe_guid_method(device_t bus, device_t dev,
1163 const struct hyperv_guid *guid)
1165 const struct hv_vmbus_channel *chan = vmbus_get_channel(dev);
1167 if (memcmp(&chan->ch_guid_type, guid, sizeof(struct hyperv_guid)) == 0)
1173 vmbus_probe(device_t dev)
1175 char *id[] = { "VMBUS", NULL };
1177 if (ACPI_ID_PROBE(device_get_parent(dev), dev, id) == NULL ||
1178 device_get_unit(dev) != 0 || vm_guest != VM_GUEST_HV ||
1179 (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1182 device_set_desc(dev, "Hyper-V Vmbus");
1184 return (BUS_PROBE_DEFAULT);
1188 * @brief Main vmbus driver initialization routine.
1191 * - initialize the vmbus driver context
1192 * - setup various driver entry points
1193 * - invoke the vmbus hv main init routine
1194 * - get the irq resource
1195 * - invoke the vmbus to add the vmbus root device
1196 * - setup the vmbus root device
1197 * - retrieve the channel offers
1200 vmbus_doattach(struct vmbus_softc *sc)
1202 struct sysctl_oid_list *child;
1203 struct sysctl_ctx_list *ctx;
1206 if (sc->vmbus_flags & VMBUS_FLAG_ATTACHED)
1208 sc->vmbus_flags |= VMBUS_FLAG_ATTACHED;
1210 mtx_init(&sc->vmbus_scan_lock, "vmbus scan", NULL, MTX_DEF);
1211 sc->vmbus_gpadl = VMBUS_GPADL_START;
1212 mtx_init(&sc->vmbus_prichan_lock, "vmbus prichan", NULL, MTX_DEF);
1213 TAILQ_INIT(&sc->vmbus_prichans);
1214 sc->vmbus_chmap = malloc(
1215 sizeof(struct hv_vmbus_channel *) * VMBUS_CHAN_MAX, M_DEVBUF,
1219 * Create context for "post message" Hypercalls
1221 sc->vmbus_msg_hc = vmbus_msghc_ctx_create(
1222 bus_get_dma_tag(sc->vmbus_dev));
1223 if (sc->vmbus_msg_hc == NULL) {
1229 * Allocate DMA stuffs.
1231 ret = vmbus_dma_alloc(sc);
1238 ret = vmbus_intr_setup(sc);
1246 device_printf(sc->vmbus_dev, "smp_started = %d\n", smp_started);
1247 smp_rendezvous(NULL, vmbus_synic_setup, NULL, sc);
1248 sc->vmbus_flags |= VMBUS_FLAG_SYNIC;
1251 * Initialize vmbus, e.g. connect to Hypervisor.
1253 ret = vmbus_init(sc);
1257 if (sc->vmbus_version == VMBUS_VERSION_WS2008 ||
1258 sc->vmbus_version == VMBUS_VERSION_WIN7)
1259 sc->vmbus_event_proc = vmbus_event_proc_compat;
1261 sc->vmbus_event_proc = vmbus_event_proc;
1263 ret = vmbus_scan(sc);
1267 ctx = device_get_sysctl_ctx(sc->vmbus_dev);
1268 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vmbus_dev));
1269 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "version",
1270 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1271 vmbus_sysctl_version, "A", "vmbus version");
1276 vmbus_intr_teardown(sc);
1278 if (sc->vmbus_msg_hc != NULL) {
1279 vmbus_msghc_ctx_destroy(sc->vmbus_msg_hc);
1280 sc->vmbus_msg_hc = NULL;
1282 free(sc->vmbus_chmap, M_DEVBUF);
1283 mtx_destroy(&sc->vmbus_scan_lock);
1284 mtx_destroy(&sc->vmbus_prichan_lock);
1290 vmbus_event_proc_dummy(struct vmbus_softc *sc __unused, int cpu __unused)
1295 vmbus_attach(device_t dev)
1297 vmbus_sc = device_get_softc(dev);
1298 vmbus_sc->vmbus_dev = dev;
1301 * Event processing logic will be configured:
1302 * - After the vmbus protocol version negotiation.
1303 * - Before we request channel offers.
1305 vmbus_sc->vmbus_event_proc = vmbus_event_proc_dummy;
1308 * If the system has already booted and thread
1309 * scheduling is possible indicated by the global
1310 * cold set to zero, we just call the driver
1311 * initialization directly.
1314 vmbus_doattach(vmbus_sc);
1320 vmbus_sysinit(void *arg __unused)
1322 struct vmbus_softc *sc = vmbus_get_softc();
1324 if (vm_guest != VM_GUEST_HV || sc == NULL)
1328 * If the system has already booted and thread
1329 * scheduling is possible, as indicated by the
1330 * global cold set to zero, we just call the driver
1331 * initialization directly.
1338 vmbus_detach(device_t dev)
1340 struct vmbus_softc *sc = device_get_softc(dev);
1342 vmbus_chan_destroy_all(sc);
1344 vmbus_disconnect(sc);
1346 if (sc->vmbus_flags & VMBUS_FLAG_SYNIC) {
1347 sc->vmbus_flags &= ~VMBUS_FLAG_SYNIC;
1348 smp_rendezvous(NULL, vmbus_synic_teardown, NULL, NULL);
1351 vmbus_intr_teardown(sc);
1354 if (sc->vmbus_msg_hc != NULL) {
1355 vmbus_msghc_ctx_destroy(sc->vmbus_msg_hc);
1356 sc->vmbus_msg_hc = NULL;
1359 free(sc->vmbus_chmap, M_DEVBUF);
1360 mtx_destroy(&sc->vmbus_scan_lock);
1361 mtx_destroy(&sc->vmbus_prichan_lock);
1366 static device_method_t vmbus_methods[] = {
1367 /* Device interface */
1368 DEVMETHOD(device_probe, vmbus_probe),
1369 DEVMETHOD(device_attach, vmbus_attach),
1370 DEVMETHOD(device_detach, vmbus_detach),
1371 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1372 DEVMETHOD(device_suspend, bus_generic_suspend),
1373 DEVMETHOD(device_resume, bus_generic_resume),
1376 DEVMETHOD(bus_add_child, bus_generic_add_child),
1377 DEVMETHOD(bus_print_child, bus_generic_print_child),
1378 DEVMETHOD(bus_read_ivar, vmbus_read_ivar),
1379 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str),
1381 /* Vmbus interface */
1382 DEVMETHOD(vmbus_get_version, vmbus_get_version_method),
1383 DEVMETHOD(vmbus_probe_guid, vmbus_probe_guid_method),
1388 static driver_t vmbus_driver = {
1391 sizeof(struct vmbus_softc)
1394 static devclass_t vmbus_devclass;
1396 DRIVER_MODULE(vmbus, acpi, vmbus_driver, vmbus_devclass, NULL, NULL);
1397 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
1398 MODULE_VERSION(vmbus, 1);
1402 * We have to start as the last step of SI_SUB_SMP, i.e. after SMP is
1405 SYSINIT(vmbus_initialize, SI_SUB_SMP, SI_ORDER_ANY, vmbus_sysinit, NULL);