1/*- 2 * Copyright (c) 2009-2012,2016-2017 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 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. 16 * 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. 27 */ 28 29/* 30 * VM Bus Driver Implementation 31 */ 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD$"); 34 35#include <sys/param.h> 36#include <sys/bus.h> 37#include <sys/kernel.h> 38#include <sys/linker.h> 39#include <sys/lock.h> 40#include <sys/malloc.h> 41#include <sys/module.h> 42#include <sys/mutex.h> 43#include <sys/smp.h> 44#include <sys/sysctl.h> 45#include <sys/systm.h> 46#include <sys/taskqueue.h> 47 48#include <machine/bus.h> 49#include <machine/intr_machdep.h> 50#include <machine/metadata.h> 51#include <machine/md_var.h> 52#include <machine/resource.h> 53#include <x86/include/apicvar.h> 54 55#include <contrib/dev/acpica/include/acpi.h> 56#include <dev/acpica/acpivar.h> 57 58#include <dev/hyperv/include/hyperv.h> 59#include <dev/hyperv/include/vmbus_xact.h> 60#include <dev/hyperv/vmbus/hyperv_reg.h> 61#include <dev/hyperv/vmbus/hyperv_var.h> 62#include <dev/hyperv/vmbus/vmbus_reg.h> 63#include <dev/hyperv/vmbus/vmbus_var.h> 64#include <dev/hyperv/vmbus/vmbus_chanvar.h> 65 66#include "acpi_if.h" 67#include "pcib_if.h" 68#include "vmbus_if.h" 69 70#define VMBUS_GPADL_START 0xe1e10 71 72struct vmbus_msghc { 73 struct vmbus_xact *mh_xact; 74 struct hypercall_postmsg_in mh_inprm_save; 75}; 76 77static void vmbus_identify(driver_t *, device_t); 78static int vmbus_probe(device_t); 79static int vmbus_attach(device_t); 80static int vmbus_detach(device_t); 81static int vmbus_read_ivar(device_t, device_t, int, 82 uintptr_t *); 83static int vmbus_child_pnpinfo_str(device_t, device_t, 84 char *, size_t); 85static struct resource *vmbus_alloc_resource(device_t dev, 86 device_t child, int type, int *rid, 87 rman_res_t start, rman_res_t end, 88 rman_res_t count, u_int flags); 89static int vmbus_alloc_msi(device_t bus, device_t dev, 90 int count, int maxcount, int *irqs); 91static int vmbus_release_msi(device_t bus, device_t dev, 92 int count, int *irqs); 93static int vmbus_alloc_msix(device_t bus, device_t dev, 94 int *irq); 95static int vmbus_release_msix(device_t bus, device_t dev, 96 int irq); 97static int vmbus_map_msi(device_t bus, device_t dev, 98 int irq, uint64_t *addr, uint32_t *data); 99static uint32_t vmbus_get_version_method(device_t, device_t); 100static int vmbus_probe_guid_method(device_t, device_t, 101 const struct hyperv_guid *); 102static uint32_t vmbus_get_vcpu_id_method(device_t bus, 103 device_t dev, int cpu); 104static struct taskqueue *vmbus_get_eventtq_method(device_t, device_t, 105 int); 106#ifdef EARLY_AP_STARTUP 107static void vmbus_intrhook(void *); 108#endif 109 110static int vmbus_init(struct vmbus_softc *); 111static int vmbus_connect(struct vmbus_softc *, uint32_t); 112static int vmbus_req_channels(struct vmbus_softc *sc); 113static void vmbus_disconnect(struct vmbus_softc *); 114static int vmbus_scan(struct vmbus_softc *); 115static void vmbus_scan_teardown(struct vmbus_softc *); 116static void vmbus_scan_done(struct vmbus_softc *, 117 const struct vmbus_message *); 118static void vmbus_chanmsg_handle(struct vmbus_softc *, 119 const struct vmbus_message *); 120static void vmbus_msg_task(void *, int); 121static void vmbus_synic_setup(void *); 122static void vmbus_synic_teardown(void *); 123static int vmbus_sysctl_version(SYSCTL_HANDLER_ARGS); 124static int vmbus_dma_alloc(struct vmbus_softc *); 125static void vmbus_dma_free(struct vmbus_softc *); 126static int vmbus_intr_setup(struct vmbus_softc *); 127static void vmbus_intr_teardown(struct vmbus_softc *); 128static int vmbus_doattach(struct vmbus_softc *); 129static void vmbus_event_proc_dummy(struct vmbus_softc *, 130 int); 131 132static struct vmbus_softc *vmbus_sc; 133 134SYSCTL_NODE(_hw, OID_AUTO, vmbus, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 135 "Hyper-V vmbus"); 136 137static int vmbus_pin_evttask = 1; 138SYSCTL_INT(_hw_vmbus, OID_AUTO, pin_evttask, CTLFLAG_RDTUN, 139 &vmbus_pin_evttask, 0, "Pin event tasks to their respective CPU"); 140 141extern inthand_t IDTVEC(vmbus_isr), IDTVEC(vmbus_isr_pti); 142 143uint32_t vmbus_current_version; 144 145static const uint32_t vmbus_version[] = { 146 VMBUS_VERSION_WIN10, 147 VMBUS_VERSION_WIN8_1, 148 VMBUS_VERSION_WIN8, 149 VMBUS_VERSION_WIN7, 150 VMBUS_VERSION_WS2008 151}; 152 153static const vmbus_chanmsg_proc_t 154vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = { 155 VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done), 156 VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP) 157}; 158 159static device_method_t vmbus_methods[] = { 160 /* Device interface */ 161 DEVMETHOD(device_identify, vmbus_identify), 162 DEVMETHOD(device_probe, vmbus_probe), 163 DEVMETHOD(device_attach, vmbus_attach), 164 DEVMETHOD(device_detach, vmbus_detach), 165 DEVMETHOD(device_shutdown, bus_generic_shutdown), 166 DEVMETHOD(device_suspend, bus_generic_suspend), 167 DEVMETHOD(device_resume, bus_generic_resume), 168 169 /* Bus interface */ 170 DEVMETHOD(bus_add_child, bus_generic_add_child), 171 DEVMETHOD(bus_print_child, bus_generic_print_child), 172 DEVMETHOD(bus_read_ivar, vmbus_read_ivar), 173 DEVMETHOD(bus_child_pnpinfo_str, vmbus_child_pnpinfo_str), 174 DEVMETHOD(bus_alloc_resource, vmbus_alloc_resource), 175 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 176 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 177 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 178 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 179 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 180#if __FreeBSD_version >= 1100000 181 DEVMETHOD(bus_get_cpus, bus_generic_get_cpus), 182#endif 183 184 /* pcib interface */ 185 DEVMETHOD(pcib_alloc_msi, vmbus_alloc_msi), 186 DEVMETHOD(pcib_release_msi, vmbus_release_msi), 187 DEVMETHOD(pcib_alloc_msix, vmbus_alloc_msix), 188 DEVMETHOD(pcib_release_msix, vmbus_release_msix), 189 DEVMETHOD(pcib_map_msi, vmbus_map_msi), 190 191 /* Vmbus interface */ 192 DEVMETHOD(vmbus_get_version, vmbus_get_version_method), 193 DEVMETHOD(vmbus_probe_guid, vmbus_probe_guid_method), 194 DEVMETHOD(vmbus_get_vcpu_id, vmbus_get_vcpu_id_method), 195 DEVMETHOD(vmbus_get_event_taskq, vmbus_get_eventtq_method), 196 197 DEVMETHOD_END 198}; 199 200static driver_t vmbus_driver = { 201 "vmbus", 202 vmbus_methods, 203 sizeof(struct vmbus_softc) 204}; 205 206static devclass_t vmbus_devclass; 207 208DRIVER_MODULE(vmbus, pcib, vmbus_driver, vmbus_devclass, NULL, NULL); 209DRIVER_MODULE(vmbus, acpi_syscontainer, vmbus_driver, vmbus_devclass, 210 NULL, NULL); 211 212MODULE_DEPEND(vmbus, acpi, 1, 1, 1); 213MODULE_DEPEND(vmbus, pci, 1, 1, 1); 214MODULE_VERSION(vmbus, 1); 215 216static __inline struct vmbus_softc * 217vmbus_get_softc(void) 218{ 219 return vmbus_sc; 220} 221 222void 223vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize) 224{ 225 struct hypercall_postmsg_in *inprm; 226 227 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX) 228 panic("invalid data size %zu", dsize); 229 230 inprm = vmbus_xact_req_data(mh->mh_xact); 231 memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE); 232 inprm->hc_connid = VMBUS_CONNID_MESSAGE; 233 inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL; 234 inprm->hc_dsize = dsize; 235} 236 237struct vmbus_msghc * 238vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize) 239{ 240 struct vmbus_msghc *mh; 241 struct vmbus_xact *xact; 242 243 if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX) 244 panic("invalid data size %zu", dsize); 245 246 xact = vmbus_xact_get(sc->vmbus_xc, 247 dsize + __offsetof(struct hypercall_postmsg_in, hc_data[0])); 248 if (xact == NULL) 249 return (NULL); 250 251 mh = vmbus_xact_priv(xact, sizeof(*mh)); 252 mh->mh_xact = xact; 253 254 vmbus_msghc_reset(mh, dsize); 255 return (mh); 256} 257 258void 259vmbus_msghc_put(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh) 260{ 261 262 vmbus_xact_put(mh->mh_xact); 263} 264 265void * 266vmbus_msghc_dataptr(struct vmbus_msghc *mh) 267{ 268 struct hypercall_postmsg_in *inprm; 269 270 inprm = vmbus_xact_req_data(mh->mh_xact); 271 return (inprm->hc_data); 272} 273 274int 275vmbus_msghc_exec_noresult(struct vmbus_msghc *mh) 276{ 277 sbintime_t time = SBT_1MS; 278 struct hypercall_postmsg_in *inprm; 279 bus_addr_t inprm_paddr; 280 int i; 281 282 inprm = vmbus_xact_req_data(mh->mh_xact); 283 inprm_paddr = vmbus_xact_req_paddr(mh->mh_xact); 284 285 /* 286 * Save the input parameter so that we could restore the input 287 * parameter if the Hypercall failed. 288 * 289 * XXX 290 * Is this really necessary?! i.e. Will the Hypercall ever 291 * overwrite the input parameter? 292 */ 293 memcpy(&mh->mh_inprm_save, inprm, HYPERCALL_POSTMSGIN_SIZE); 294 295 /* 296 * In order to cope with transient failures, e.g. insufficient 297 * resources on host side, we retry the post message Hypercall 298 * several times. 20 retries seem sufficient. 299 */ 300#define HC_RETRY_MAX 20 301 302 for (i = 0; i < HC_RETRY_MAX; ++i) { 303 uint64_t status; 304 305 status = hypercall_post_message(inprm_paddr); 306 if (status == HYPERCALL_STATUS_SUCCESS) 307 return 0; 308 309 pause_sbt("hcpmsg", time, 0, C_HARDCLOCK); 310 if (time < SBT_1S * 2) 311 time *= 2; 312 313 /* Restore input parameter and try again */ 314 memcpy(inprm, &mh->mh_inprm_save, HYPERCALL_POSTMSGIN_SIZE); 315 } 316 317#undef HC_RETRY_MAX 318 319 return EIO; 320} 321 322int 323vmbus_msghc_exec(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh) 324{ 325 int error; 326 327 vmbus_xact_activate(mh->mh_xact); 328 error = vmbus_msghc_exec_noresult(mh); 329 if (error) 330 vmbus_xact_deactivate(mh->mh_xact); 331 return error; 332} 333 334void 335vmbus_msghc_exec_cancel(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh) 336{ 337 338 vmbus_xact_deactivate(mh->mh_xact); 339} 340 341const struct vmbus_message * 342vmbus_msghc_wait_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh) 343{ 344 size_t resp_len; 345 346 return (vmbus_xact_wait(mh->mh_xact, &resp_len)); 347} 348 349const struct vmbus_message * 350vmbus_msghc_poll_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh) 351{ 352 size_t resp_len; 353 354 return (vmbus_xact_poll(mh->mh_xact, &resp_len)); 355} 356 357void 358vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg) 359{ 360 361 vmbus_xact_ctx_wakeup(sc->vmbus_xc, msg, sizeof(*msg)); 362} 363 364uint32_t 365vmbus_gpadl_alloc(struct vmbus_softc *sc) 366{ 367 uint32_t gpadl; 368 369again: 370 gpadl = atomic_fetchadd_int(&sc->vmbus_gpadl, 1); 371 if (gpadl == 0) 372 goto again; 373 return (gpadl); 374} 375 376static int 377vmbus_connect(struct vmbus_softc *sc, uint32_t version) 378{ 379 struct vmbus_chanmsg_connect *req; 380 const struct vmbus_message *msg; 381 struct vmbus_msghc *mh; 382 int error, done = 0; 383 384 mh = vmbus_msghc_get(sc, sizeof(*req)); 385 if (mh == NULL) 386 return ENXIO; 387 388 req = vmbus_msghc_dataptr(mh); 389 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT; 390 req->chm_ver = version; 391 req->chm_evtflags = sc->vmbus_evtflags_dma.hv_paddr; 392 req->chm_mnf1 = sc->vmbus_mnf1_dma.hv_paddr; 393 req->chm_mnf2 = sc->vmbus_mnf2_dma.hv_paddr; 394 395 error = vmbus_msghc_exec(sc, mh); 396 if (error) { 397 vmbus_msghc_put(sc, mh); 398 return error; 399 } 400 401 msg = vmbus_msghc_wait_result(sc, mh); 402 done = ((const struct vmbus_chanmsg_connect_resp *) 403 msg->msg_data)->chm_done; 404 405 vmbus_msghc_put(sc, mh); 406 407 return (done ? 0 : EOPNOTSUPP); 408} 409 410static int 411vmbus_init(struct vmbus_softc *sc) 412{ 413 int i; 414 415 for (i = 0; i < nitems(vmbus_version); ++i) { 416 int error; 417 418 error = vmbus_connect(sc, vmbus_version[i]); 419 if (!error) { 420 vmbus_current_version = vmbus_version[i]; 421 sc->vmbus_version = vmbus_version[i]; 422 device_printf(sc->vmbus_dev, "version %u.%u\n", 423 VMBUS_VERSION_MAJOR(sc->vmbus_version), 424 VMBUS_VERSION_MINOR(sc->vmbus_version)); 425 return 0; 426 } 427 } 428 return ENXIO; 429} 430 431static void 432vmbus_disconnect(struct vmbus_softc *sc) 433{ 434 struct vmbus_chanmsg_disconnect *req; 435 struct vmbus_msghc *mh; 436 int error; 437 438 mh = vmbus_msghc_get(sc, sizeof(*req)); 439 if (mh == NULL) { 440 device_printf(sc->vmbus_dev, 441 "can not get msg hypercall for disconnect\n"); 442 return; 443 } 444 445 req = vmbus_msghc_dataptr(mh); 446 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT; 447 448 error = vmbus_msghc_exec_noresult(mh); 449 vmbus_msghc_put(sc, mh); 450 451 if (error) { 452 device_printf(sc->vmbus_dev, 453 "disconnect msg hypercall failed\n"); 454 } 455} 456 457static int 458vmbus_req_channels(struct vmbus_softc *sc) 459{ 460 struct vmbus_chanmsg_chrequest *req; 461 struct vmbus_msghc *mh; 462 int error; 463 464 mh = vmbus_msghc_get(sc, sizeof(*req)); 465 if (mh == NULL) 466 return ENXIO; 467 468 req = vmbus_msghc_dataptr(mh); 469 req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST; 470 471 error = vmbus_msghc_exec_noresult(mh); 472 vmbus_msghc_put(sc, mh); 473 474 return error; 475} 476 477static void 478vmbus_scan_done_task(void *xsc, int pending __unused) 479{ 480 struct vmbus_softc *sc = xsc; 481 482 mtx_lock(&Giant); 483 sc->vmbus_scandone = true; 484 mtx_unlock(&Giant); 485 wakeup(&sc->vmbus_scandone); 486} 487 488static void 489vmbus_scan_done(struct vmbus_softc *sc, 490 const struct vmbus_message *msg __unused) 491{ 492 493 taskqueue_enqueue(sc->vmbus_devtq, &sc->vmbus_scandone_task); 494} 495 496static int 497vmbus_scan(struct vmbus_softc *sc) 498{ 499 int error; 500 501 /* 502 * Identify, probe and attach for non-channel devices. 503 */ 504 bus_generic_probe(sc->vmbus_dev); 505 bus_generic_attach(sc->vmbus_dev); 506 507 /* 508 * This taskqueue serializes vmbus devices' attach and detach 509 * for channel offer and rescind messages. 510 */ 511 sc->vmbus_devtq = taskqueue_create("vmbus dev", M_WAITOK, 512 taskqueue_thread_enqueue, &sc->vmbus_devtq); 513 taskqueue_start_threads(&sc->vmbus_devtq, 1, PI_NET, "vmbusdev"); 514 TASK_INIT(&sc->vmbus_scandone_task, 0, vmbus_scan_done_task, sc); 515 516 /* 517 * This taskqueue handles sub-channel detach, so that vmbus 518 * device's detach running in vmbus_devtq can drain its sub- 519 * channels. 520 */ 521 sc->vmbus_subchtq = taskqueue_create("vmbus subch", M_WAITOK, 522 taskqueue_thread_enqueue, &sc->vmbus_subchtq); 523 taskqueue_start_threads(&sc->vmbus_subchtq, 1, PI_NET, "vmbussch"); 524 525 /* 526 * Start vmbus scanning. 527 */ 528 error = vmbus_req_channels(sc); 529 if (error) { 530 device_printf(sc->vmbus_dev, "channel request failed: %d\n", 531 error); 532 return (error); 533 } 534 535 /* 536 * Wait for all vmbus devices from the initial channel offers to be 537 * attached. 538 */ 539 GIANT_REQUIRED; 540 while (!sc->vmbus_scandone) 541 mtx_sleep(&sc->vmbus_scandone, &Giant, 0, "vmbusdev", 0); 542 543 if (bootverbose) { 544 device_printf(sc->vmbus_dev, "device scan, probe and attach " 545 "done\n"); 546 } 547 return (0); 548} 549 550static void 551vmbus_scan_teardown(struct vmbus_softc *sc) 552{ 553 554 GIANT_REQUIRED; 555 if (sc->vmbus_devtq != NULL) { 556 mtx_unlock(&Giant); 557 taskqueue_free(sc->vmbus_devtq); 558 mtx_lock(&Giant); 559 sc->vmbus_devtq = NULL; 560 } 561 if (sc->vmbus_subchtq != NULL) { 562 mtx_unlock(&Giant); 563 taskqueue_free(sc->vmbus_subchtq); 564 mtx_lock(&Giant); 565 sc->vmbus_subchtq = NULL; 566 } 567} 568 569static void 570vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg) 571{ 572 vmbus_chanmsg_proc_t msg_proc; 573 uint32_t msg_type; 574 575 msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type; 576 if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) { 577 device_printf(sc->vmbus_dev, "unknown message type 0x%x\n", 578 msg_type); 579 return; 580 } 581 582 msg_proc = vmbus_chanmsg_handlers[msg_type]; 583 if (msg_proc != NULL) 584 msg_proc(sc, msg); 585 586 /* Channel specific processing */ 587 vmbus_chan_msgproc(sc, msg); 588} 589 590static void 591vmbus_msg_task(void *xsc, int pending __unused) 592{ 593 struct vmbus_softc *sc = xsc; 594 volatile struct vmbus_message *msg; 595 596 msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE; 597 for (;;) { 598 if (msg->msg_type == HYPERV_MSGTYPE_NONE) { 599 /* No message */ 600 break; 601 } else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) { 602 /* Channel message */ 603 vmbus_chanmsg_handle(sc, 604 __DEVOLATILE(const struct vmbus_message *, msg)); 605 } 606 607 msg->msg_type = HYPERV_MSGTYPE_NONE; 608 /* 609 * Make sure the write to msg_type (i.e. set to 610 * HYPERV_MSGTYPE_NONE) happens before we read the 611 * msg_flags and EOMing. Otherwise, the EOMing will 612 * not deliver any more messages since there is no 613 * empty slot 614 * 615 * NOTE: 616 * mb() is used here, since atomic_thread_fence_seq_cst() 617 * will become compiler fence on UP kernel. 618 */ 619 mb(); 620 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) { 621 /* 622 * This will cause message queue rescan to possibly 623 * deliver another msg from the hypervisor 624 */ 625 wrmsr(MSR_HV_EOM, 0); 626 } 627 } 628} 629 630static __inline int 631vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu) 632{ 633 volatile struct vmbus_message *msg; 634 struct vmbus_message *msg_base; 635 636 msg_base = VMBUS_PCPU_GET(sc, message, cpu); 637 638 /* 639 * Check event timer. 640 * 641 * TODO: move this to independent IDT vector. 642 */ 643 msg = msg_base + VMBUS_SINT_TIMER; 644 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) { 645 msg->msg_type = HYPERV_MSGTYPE_NONE; 646 647 vmbus_et_intr(frame); 648 649 /* 650 * Make sure the write to msg_type (i.e. set to 651 * HYPERV_MSGTYPE_NONE) happens before we read the 652 * msg_flags and EOMing. Otherwise, the EOMing will 653 * not deliver any more messages since there is no 654 * empty slot 655 * 656 * NOTE: 657 * mb() is used here, since atomic_thread_fence_seq_cst() 658 * will become compiler fence on UP kernel. 659 */ 660 mb(); 661 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) { 662 /* 663 * This will cause message queue rescan to possibly 664 * deliver another msg from the hypervisor 665 */ 666 wrmsr(MSR_HV_EOM, 0); 667 } 668 } 669 670 /* 671 * Check events. Hot path for network and storage I/O data; high rate. 672 * 673 * NOTE: 674 * As recommended by the Windows guest fellows, we check events before 675 * checking messages. 676 */ 677 sc->vmbus_event_proc(sc, cpu); 678 679 /* 680 * Check messages. Mainly management stuffs; ultra low rate. 681 */ 682 msg = msg_base + VMBUS_SINT_MESSAGE; 683 if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) { 684 taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu), 685 VMBUS_PCPU_PTR(sc, message_task, cpu)); 686 } 687 688 return (FILTER_HANDLED); 689} 690 691void 692vmbus_handle_intr(struct trapframe *trap_frame) 693{ 694 struct vmbus_softc *sc = vmbus_get_softc(); 695 int cpu = curcpu; 696 697 /* 698 * Disable preemption. 699 */ 700 critical_enter(); 701 702 /* 703 * Do a little interrupt counting. 704 */ 705 (*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++; 706 707 vmbus_handle_intr1(sc, trap_frame, cpu); 708 709 /* 710 * Enable preemption. 711 */ 712 critical_exit(); 713} 714 715static void 716vmbus_synic_setup(void *xsc) 717{ 718 struct vmbus_softc *sc = xsc; 719 int cpu = curcpu; 720 uint64_t val, orig; 721 uint32_t sint; 722 723 if (hyperv_features & CPUID_HV_MSR_VP_INDEX) { 724 /* Save virtual processor id. */ 725 VMBUS_PCPU_GET(sc, vcpuid, cpu) = rdmsr(MSR_HV_VP_INDEX); 726 } else { 727 /* Set virtual processor id to 0 for compatibility. */ 728 VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0; 729 } 730 731 /* 732 * Setup the SynIC message. 733 */ 734 orig = rdmsr(MSR_HV_SIMP); 735 val = MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK) | 736 ((VMBUS_PCPU_GET(sc, message_dma.hv_paddr, cpu) >> PAGE_SHIFT) << 737 MSR_HV_SIMP_PGSHIFT); 738 wrmsr(MSR_HV_SIMP, val); 739 740 /* 741 * Setup the SynIC event flags. 742 */ 743 orig = rdmsr(MSR_HV_SIEFP); 744 val = MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK) | 745 ((VMBUS_PCPU_GET(sc, event_flags_dma.hv_paddr, cpu) 746 >> PAGE_SHIFT) << MSR_HV_SIEFP_PGSHIFT); 747 wrmsr(MSR_HV_SIEFP, val); 748 749 750 /* 751 * Configure and unmask SINT for message and event flags. 752 */ 753 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE; 754 orig = rdmsr(sint); 755 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI | 756 (orig & MSR_HV_SINT_RSVD_MASK); 757 wrmsr(sint, val); 758 759 /* 760 * Configure and unmask SINT for timer. 761 */ 762 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER; 763 orig = rdmsr(sint); 764 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI | 765 (orig & MSR_HV_SINT_RSVD_MASK); 766 wrmsr(sint, val); 767 768 /* 769 * All done; enable SynIC. 770 */ 771 orig = rdmsr(MSR_HV_SCONTROL); 772 val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK); 773 wrmsr(MSR_HV_SCONTROL, val); 774} 775 776static void 777vmbus_synic_teardown(void *arg) 778{ 779 uint64_t orig; 780 uint32_t sint; 781 782 /* 783 * Disable SynIC. 784 */ 785 orig = rdmsr(MSR_HV_SCONTROL); 786 wrmsr(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK)); 787 788 /* 789 * Mask message and event flags SINT. 790 */ 791 sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE; 792 orig = rdmsr(sint); 793 wrmsr(sint, orig | MSR_HV_SINT_MASKED); 794 795 /* 796 * Mask timer SINT. 797 */ 798 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER; 799 orig = rdmsr(sint); 800 wrmsr(sint, orig | MSR_HV_SINT_MASKED); 801 802 /* 803 * Teardown SynIC message. 804 */ 805 orig = rdmsr(MSR_HV_SIMP); 806 wrmsr(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK)); 807 808 /* 809 * Teardown SynIC event flags. 810 */ 811 orig = rdmsr(MSR_HV_SIEFP); 812 wrmsr(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK)); 813} 814 815static int 816vmbus_dma_alloc(struct vmbus_softc *sc) 817{ 818 bus_dma_tag_t parent_dtag; 819 uint8_t *evtflags; 820 int cpu; 821 822 parent_dtag = bus_get_dma_tag(sc->vmbus_dev); 823 CPU_FOREACH(cpu) { 824 void *ptr; 825 826 /* 827 * Per-cpu messages and event flags. 828 */ 829 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0, 830 PAGE_SIZE, VMBUS_PCPU_PTR(sc, message_dma, cpu), 831 BUS_DMA_WAITOK | BUS_DMA_ZERO); 832 if (ptr == NULL) 833 return ENOMEM; 834 VMBUS_PCPU_GET(sc, message, cpu) = ptr; 835 836 ptr = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0, 837 PAGE_SIZE, VMBUS_PCPU_PTR(sc, event_flags_dma, cpu), 838 BUS_DMA_WAITOK | BUS_DMA_ZERO); 839 if (ptr == NULL) 840 return ENOMEM; 841 VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr; 842 } 843 844 evtflags = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0, 845 PAGE_SIZE, &sc->vmbus_evtflags_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 846 if (evtflags == NULL) 847 return ENOMEM; 848 sc->vmbus_rx_evtflags = (u_long *)evtflags; 849 sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2)); 850 sc->vmbus_evtflags = evtflags; 851 852 sc->vmbus_mnf1 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0, 853 PAGE_SIZE, &sc->vmbus_mnf1_dma, BUS_DMA_WAITOK | BUS_DMA_ZERO); 854 if (sc->vmbus_mnf1 == NULL) 855 return ENOMEM; 856 857 sc->vmbus_mnf2 = hyperv_dmamem_alloc(parent_dtag, PAGE_SIZE, 0, 858 sizeof(struct vmbus_mnf), &sc->vmbus_mnf2_dma, 859 BUS_DMA_WAITOK | BUS_DMA_ZERO); 860 if (sc->vmbus_mnf2 == NULL) 861 return ENOMEM; 862 863 return 0; 864} 865 866static void 867vmbus_dma_free(struct vmbus_softc *sc) 868{ 869 int cpu; 870 871 if (sc->vmbus_evtflags != NULL) { 872 hyperv_dmamem_free(&sc->vmbus_evtflags_dma, sc->vmbus_evtflags); 873 sc->vmbus_evtflags = NULL; 874 sc->vmbus_rx_evtflags = NULL; 875 sc->vmbus_tx_evtflags = NULL; 876 } 877 if (sc->vmbus_mnf1 != NULL) { 878 hyperv_dmamem_free(&sc->vmbus_mnf1_dma, sc->vmbus_mnf1); 879 sc->vmbus_mnf1 = NULL; 880 } 881 if (sc->vmbus_mnf2 != NULL) { 882 hyperv_dmamem_free(&sc->vmbus_mnf2_dma, sc->vmbus_mnf2); 883 sc->vmbus_mnf2 = NULL; 884 } 885 886 CPU_FOREACH(cpu) { 887 if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) { 888 hyperv_dmamem_free( 889 VMBUS_PCPU_PTR(sc, message_dma, cpu), 890 VMBUS_PCPU_GET(sc, message, cpu)); 891 VMBUS_PCPU_GET(sc, message, cpu) = NULL; 892 } 893 if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) { 894 hyperv_dmamem_free( 895 VMBUS_PCPU_PTR(sc, event_flags_dma, cpu), 896 VMBUS_PCPU_GET(sc, event_flags, cpu)); 897 VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL; 898 } 899 } 900} 901 902static int 903vmbus_intr_setup(struct vmbus_softc *sc) 904{ 905 int cpu; 906 907 CPU_FOREACH(cpu) { 908 char buf[MAXCOMLEN + 1]; 909 cpuset_t cpu_mask; 910 911 /* Allocate an interrupt counter for Hyper-V interrupt */ 912 snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu); 913 intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu)); 914 915 /* 916 * Setup taskqueue to handle events. Task will be per- 917 * channel. 918 */ 919 VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast( 920 "hyperv event", M_WAITOK, taskqueue_thread_enqueue, 921 VMBUS_PCPU_PTR(sc, event_tq, cpu)); 922 if (vmbus_pin_evttask) { 923 CPU_SETOF(cpu, &cpu_mask); 924 taskqueue_start_threads_cpuset( 925 VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET, 926 &cpu_mask, "hvevent%d", cpu); 927 } else { 928 taskqueue_start_threads( 929 VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET, 930 "hvevent%d", cpu); 931 } 932 933 /* 934 * Setup tasks and taskqueues to handle messages. 935 */ 936 VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast( 937 "hyperv msg", M_WAITOK, taskqueue_thread_enqueue, 938 VMBUS_PCPU_PTR(sc, message_tq, cpu)); 939 CPU_SETOF(cpu, &cpu_mask); 940 taskqueue_start_threads_cpuset( 941 VMBUS_PCPU_PTR(sc, message_tq, cpu), 1, PI_NET, &cpu_mask, 942 "hvmsg%d", cpu); 943 TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0, 944 vmbus_msg_task, sc); 945 } 946 947 /* 948 * All Hyper-V ISR required resources are setup, now let's find a 949 * free IDT vector for Hyper-V ISR and set it up. 950 */ 951 sc->vmbus_idtvec = lapic_ipi_alloc(pti ? IDTVEC(vmbus_isr_pti) : 952 IDTVEC(vmbus_isr)); 953 if (sc->vmbus_idtvec < 0) { 954 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n"); 955 return ENXIO; 956 } 957 if (bootverbose) { 958 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n", 959 sc->vmbus_idtvec); 960 } 961 return 0; 962} 963 964static void 965vmbus_intr_teardown(struct vmbus_softc *sc) 966{ 967 int cpu; 968 969 if (sc->vmbus_idtvec >= 0) { 970 lapic_ipi_free(sc->vmbus_idtvec); 971 sc->vmbus_idtvec = -1; 972 } 973 974 CPU_FOREACH(cpu) { 975 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) { 976 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu)); 977 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL; 978 } 979 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) { 980 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu), 981 VMBUS_PCPU_PTR(sc, message_task, cpu)); 982 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu)); 983 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL; 984 } 985 } 986} 987 988static int 989vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 990{ 991 return (ENOENT); 992} 993 994static int 995vmbus_child_pnpinfo_str(device_t dev, device_t child, char *buf, size_t buflen) 996{ 997 const struct vmbus_channel *chan; 998 char guidbuf[HYPERV_GUID_STRLEN]; 999 1000 chan = vmbus_get_channel(child); 1001 if (chan == NULL) { 1002 /* Event timer device, which does not belong to a channel */ 1003 return (0); 1004 } 1005 1006 strlcat(buf, "classid=", buflen); 1007 hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf)); 1008 strlcat(buf, guidbuf, buflen); 1009 1010 strlcat(buf, " deviceid=", buflen); 1011 hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf)); 1012 strlcat(buf, guidbuf, buflen); 1013 1014 return (0); 1015} 1016 1017int 1018vmbus_add_child(struct vmbus_channel *chan) 1019{ 1020 struct vmbus_softc *sc = chan->ch_vmbus; 1021 device_t parent = sc->vmbus_dev; 1022 1023 mtx_lock(&Giant); 1024 1025 chan->ch_dev = device_add_child(parent, NULL, -1); 1026 if (chan->ch_dev == NULL) { 1027 mtx_unlock(&Giant); 1028 device_printf(parent, "device_add_child for chan%u failed\n", 1029 chan->ch_id); 1030 return (ENXIO); 1031 } 1032 device_set_ivars(chan->ch_dev, chan); 1033 device_probe_and_attach(chan->ch_dev); 1034 1035 mtx_unlock(&Giant); 1036 return (0); 1037} 1038 1039int 1040vmbus_delete_child(struct vmbus_channel *chan) 1041{ 1042 int error = 0; 1043 1044 mtx_lock(&Giant); 1045 if (chan->ch_dev != NULL) { 1046 error = device_delete_child(chan->ch_vmbus->vmbus_dev, 1047 chan->ch_dev); 1048 chan->ch_dev = NULL; 1049 } 1050 mtx_unlock(&Giant); 1051 return (error); 1052} 1053 1054static int 1055vmbus_sysctl_version(SYSCTL_HANDLER_ARGS) 1056{ 1057 struct vmbus_softc *sc = arg1; 1058 char verstr[16]; 1059 1060 snprintf(verstr, sizeof(verstr), "%u.%u", 1061 VMBUS_VERSION_MAJOR(sc->vmbus_version), 1062 VMBUS_VERSION_MINOR(sc->vmbus_version)); 1063 return sysctl_handle_string(oidp, verstr, sizeof(verstr), req); 1064} 1065 1066/* 1067 * We need the function to make sure the MMIO resource is allocated from the 1068 * ranges found in _CRS. 1069 * 1070 * For the release function, we can use bus_generic_release_resource(). 1071 */ 1072static struct resource * 1073vmbus_alloc_resource(device_t dev, device_t child, int type, int *rid, 1074 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1075{ 1076 device_t parent = device_get_parent(dev); 1077 struct resource *res; 1078 1079#ifdef NEW_PCIB 1080 if (type == SYS_RES_MEMORY) { 1081 struct vmbus_softc *sc = device_get_softc(dev); 1082 1083 res = pcib_host_res_alloc(&sc->vmbus_mmio_res, child, type, 1084 rid, start, end, count, flags); 1085 } else 1086#endif 1087 { 1088 res = BUS_ALLOC_RESOURCE(parent, child, type, rid, start, 1089 end, count, flags); 1090 } 1091 1092 return (res); 1093} 1094 1095static int 1096vmbus_alloc_msi(device_t bus, device_t dev, int count, int maxcount, int *irqs) 1097{ 1098 1099 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 1100 irqs)); 1101} 1102 1103static int 1104vmbus_release_msi(device_t bus, device_t dev, int count, int *irqs) 1105{ 1106 1107 return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 1108} 1109 1110static int 1111vmbus_alloc_msix(device_t bus, device_t dev, int *irq) 1112{ 1113 1114 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 1115} 1116 1117static int 1118vmbus_release_msix(device_t bus, device_t dev, int irq) 1119{ 1120 1121 return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 1122} 1123 1124static int 1125vmbus_map_msi(device_t bus, device_t dev, int irq, uint64_t *addr, 1126 uint32_t *data) 1127{ 1128 1129 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data)); 1130} 1131 1132static uint32_t 1133vmbus_get_version_method(device_t bus, device_t dev) 1134{ 1135 struct vmbus_softc *sc = device_get_softc(bus); 1136 1137 return sc->vmbus_version; 1138} 1139 1140static int 1141vmbus_probe_guid_method(device_t bus, device_t dev, 1142 const struct hyperv_guid *guid) 1143{ 1144 const struct vmbus_channel *chan = vmbus_get_channel(dev); 1145 1146 if (memcmp(&chan->ch_guid_type, guid, sizeof(struct hyperv_guid)) == 0) 1147 return 0; 1148 return ENXIO; 1149} 1150 1151static uint32_t 1152vmbus_get_vcpu_id_method(device_t bus, device_t dev, int cpu) 1153{ 1154 const struct vmbus_softc *sc = device_get_softc(bus); 1155 1156 return (VMBUS_PCPU_GET(sc, vcpuid, cpu)); 1157} 1158 1159static struct taskqueue * 1160vmbus_get_eventtq_method(device_t bus, device_t dev __unused, int cpu) 1161{ 1162 const struct vmbus_softc *sc = device_get_softc(bus); 1163 1164 KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu%d", cpu)); 1165 return (VMBUS_PCPU_GET(sc, event_tq, cpu)); 1166} 1167 1168#ifdef NEW_PCIB 1169#define VTPM_BASE_ADDR 0xfed40000 1170#define FOUR_GB (1ULL << 32) 1171 1172enum parse_pass { parse_64, parse_32 }; 1173 1174struct parse_context { 1175 device_t vmbus_dev; 1176 enum parse_pass pass; 1177}; 1178 1179static ACPI_STATUS 1180parse_crs(ACPI_RESOURCE *res, void *ctx) 1181{ 1182 const struct parse_context *pc = ctx; 1183 device_t vmbus_dev = pc->vmbus_dev; 1184 1185 struct vmbus_softc *sc = device_get_softc(vmbus_dev); 1186 UINT64 start, end; 1187 1188 switch (res->Type) { 1189 case ACPI_RESOURCE_TYPE_ADDRESS32: 1190 start = res->Data.Address32.Address.Minimum; 1191 end = res->Data.Address32.Address.Maximum; 1192 break; 1193 1194 case ACPI_RESOURCE_TYPE_ADDRESS64: 1195 start = res->Data.Address64.Address.Minimum; 1196 end = res->Data.Address64.Address.Maximum; 1197 break; 1198 1199 default: 1200 /* Unused types. */ 1201 return (AE_OK); 1202 } 1203 1204 /* 1205 * We don't use <1MB addresses. 1206 */ 1207 if (end < 0x100000) 1208 return (AE_OK); 1209 1210 /* Don't conflict with vTPM. */ 1211 if (end >= VTPM_BASE_ADDR && start < VTPM_BASE_ADDR) 1212 end = VTPM_BASE_ADDR - 1; 1213 1214 if ((pc->pass == parse_32 && start < FOUR_GB) || 1215 (pc->pass == parse_64 && start >= FOUR_GB)) 1216 pcib_host_res_decodes(&sc->vmbus_mmio_res, SYS_RES_MEMORY, 1217 start, end, 0); 1218 1219 return (AE_OK); 1220} 1221 1222static void 1223vmbus_get_crs(device_t dev, device_t vmbus_dev, enum parse_pass pass) 1224{ 1225 struct parse_context pc; 1226 ACPI_STATUS status; 1227 1228 if (bootverbose) 1229 device_printf(dev, "walking _CRS, pass=%d\n", pass); 1230 1231 pc.vmbus_dev = vmbus_dev; 1232 pc.pass = pass; 1233 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 1234 parse_crs, &pc); 1235 1236 if (bootverbose && ACPI_FAILURE(status)) 1237 device_printf(dev, "_CRS: not found, pass=%d\n", pass); 1238} 1239 1240static void 1241vmbus_get_mmio_res_pass(device_t dev, enum parse_pass pass) 1242{ 1243 device_t acpi0, parent; 1244 1245 parent = device_get_parent(dev); 1246 1247 acpi0 = device_get_parent(parent); 1248 if (strcmp("acpi0", device_get_nameunit(acpi0)) == 0) { 1249 device_t *children; 1250 int count; 1251 1252 /* 1253 * Try to locate VMBUS resources and find _CRS on them. 1254 */ 1255 if (device_get_children(acpi0, &children, &count) == 0) { 1256 int i; 1257 1258 for (i = 0; i < count; ++i) { 1259 if (!device_is_attached(children[i])) 1260 continue; 1261 1262 if (strcmp("vmbus_res", 1263 device_get_name(children[i])) == 0) 1264 vmbus_get_crs(children[i], dev, pass); 1265 } 1266 free(children, M_TEMP); 1267 } 1268 1269 /* 1270 * Try to find _CRS on acpi. 1271 */ 1272 vmbus_get_crs(acpi0, dev, pass); 1273 } else { 1274 device_printf(dev, "not grandchild of acpi\n"); 1275 } 1276 1277 /* 1278 * Try to find _CRS on parent. 1279 */ 1280 vmbus_get_crs(parent, dev, pass); 1281} 1282 1283static void 1284vmbus_get_mmio_res(device_t dev) 1285{ 1286 struct vmbus_softc *sc = device_get_softc(dev); 1287 /* 1288 * We walk the resources twice to make sure that: in the resource 1289 * list, the 32-bit resources appear behind the 64-bit resources. 1290 * NB: resource_list_add() uses INSERT_TAIL. This way, when we 1291 * iterate through the list to find a range for a 64-bit BAR in 1292 * vmbus_alloc_resource(), we can make sure we try to use >4GB 1293 * ranges first. 1294 */ 1295 pcib_host_res_init(dev, &sc->vmbus_mmio_res); 1296 1297 vmbus_get_mmio_res_pass(dev, parse_64); 1298 vmbus_get_mmio_res_pass(dev, parse_32); 1299} 1300 1301/* 1302 * On Gen2 VMs, Hyper-V provides mmio space for framebuffer. 1303 * This mmio address range is not useable for other PCI devices. 1304 * Currently only efifb driver is using this range without reserving 1305 * it from system. 1306 * Therefore, vmbus driver reserves it before any other PCI device 1307 * drivers start to request mmio addresses. 1308 */ 1309static struct resource *hv_fb_res; 1310 1311static void 1312vmbus_fb_mmio_res(device_t dev) 1313{ 1314 struct efi_fb *efifb; 1315 caddr_t kmdp; 1316 1317 struct vmbus_softc *sc = device_get_softc(dev); 1318 int rid = 0; 1319 1320 kmdp = preload_search_by_type("elf kernel"); 1321 if (kmdp == NULL) 1322 kmdp = preload_search_by_type("elf64 kernel"); 1323 efifb = (struct efi_fb *)preload_search_info(kmdp, 1324 MODINFO_METADATA | MODINFOMD_EFI_FB); 1325 if (efifb == NULL) { 1326 if (bootverbose) 1327 device_printf(dev, 1328 "fb has no preloaded kernel efi information\n"); 1329 /* We are on Gen1 VM, just return. */ 1330 return; 1331 } else { 1332 if (bootverbose) 1333 device_printf(dev, 1334 "efifb: fb_addr: %#jx, size: %#jx, " 1335 "actual size needed: 0x%x\n", 1336 efifb->fb_addr, efifb->fb_size, 1337 (int) efifb->fb_height * efifb->fb_width); 1338 } 1339 1340 hv_fb_res = pcib_host_res_alloc(&sc->vmbus_mmio_res, dev, 1341 SYS_RES_MEMORY, &rid, 1342 efifb->fb_addr, efifb->fb_addr + efifb->fb_size, efifb->fb_size, 1343 RF_ACTIVE | rman_make_alignment_flags(PAGE_SIZE)); 1344 1345 if (hv_fb_res && bootverbose) 1346 device_printf(dev, 1347 "successfully reserved memory for framebuffer " 1348 "starting at %#jx, size %#jx\n", 1349 efifb->fb_addr, efifb->fb_size); 1350} 1351 1352static void 1353vmbus_free_mmio_res(device_t dev) 1354{ 1355 struct vmbus_softc *sc = device_get_softc(dev); 1356 1357 pcib_host_res_free(dev, &sc->vmbus_mmio_res); 1358 1359 if (hv_fb_res) 1360 hv_fb_res = NULL; 1361} 1362#endif /* NEW_PCIB */ 1363 1364static void 1365vmbus_identify(driver_t *driver, device_t parent) 1366{ 1367 1368 if (device_get_unit(parent) != 0 || vm_guest != VM_GUEST_HV || 1369 (hyperv_features & CPUID_HV_MSR_SYNIC) == 0) 1370 return; 1371 device_add_child(parent, "vmbus", -1); 1372} 1373 1374static int 1375vmbus_probe(device_t dev) 1376{ 1377 1378 if (device_get_unit(dev) != 0 || vm_guest != VM_GUEST_HV || 1379 (hyperv_features & CPUID_HV_MSR_SYNIC) == 0) 1380 return (ENXIO); 1381 1382 device_set_desc(dev, "Hyper-V Vmbus"); 1383 return (BUS_PROBE_DEFAULT); 1384} 1385 1386/** 1387 * @brief Main vmbus driver initialization routine. 1388 * 1389 * Here, we 1390 * - initialize the vmbus driver context 1391 * - setup various driver entry points 1392 * - invoke the vmbus hv main init routine 1393 * - get the irq resource 1394 * - invoke the vmbus to add the vmbus root device 1395 * - setup the vmbus root device 1396 * - retrieve the channel offers 1397 */ 1398static int 1399vmbus_doattach(struct vmbus_softc *sc) 1400{ 1401 struct sysctl_oid_list *child; 1402 struct sysctl_ctx_list *ctx; 1403 int ret; 1404 1405 if (sc->vmbus_flags & VMBUS_FLAG_ATTACHED) 1406 return (0); 1407 1408#ifdef NEW_PCIB 1409 vmbus_get_mmio_res(sc->vmbus_dev); 1410 vmbus_fb_mmio_res(sc->vmbus_dev); 1411#endif 1412 1413 sc->vmbus_flags |= VMBUS_FLAG_ATTACHED; 1414 1415 sc->vmbus_gpadl = VMBUS_GPADL_START; 1416 mtx_init(&sc->vmbus_prichan_lock, "vmbus prichan", NULL, MTX_DEF); 1417 TAILQ_INIT(&sc->vmbus_prichans); 1418 mtx_init(&sc->vmbus_chan_lock, "vmbus channel", NULL, MTX_DEF); 1419 TAILQ_INIT(&sc->vmbus_chans); 1420 sc->vmbus_chmap = malloc( 1421 sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX, M_DEVBUF, 1422 M_WAITOK | M_ZERO); 1423 1424 /* 1425 * Create context for "post message" Hypercalls 1426 */ 1427 sc->vmbus_xc = vmbus_xact_ctx_create(bus_get_dma_tag(sc->vmbus_dev), 1428 HYPERCALL_POSTMSGIN_SIZE, VMBUS_MSG_SIZE, 1429 sizeof(struct vmbus_msghc)); 1430 if (sc->vmbus_xc == NULL) { 1431 ret = ENXIO; 1432 goto cleanup; 1433 } 1434 1435 /* 1436 * Allocate DMA stuffs. 1437 */ 1438 ret = vmbus_dma_alloc(sc); 1439 if (ret != 0) 1440 goto cleanup; 1441 1442 /* 1443 * Setup interrupt. 1444 */ 1445 ret = vmbus_intr_setup(sc); 1446 if (ret != 0) 1447 goto cleanup; 1448 1449 /* 1450 * Setup SynIC. 1451 */ 1452 if (bootverbose) 1453 device_printf(sc->vmbus_dev, "smp_started = %d\n", smp_started); 1454 smp_rendezvous(NULL, vmbus_synic_setup, NULL, sc); 1455 sc->vmbus_flags |= VMBUS_FLAG_SYNIC; 1456 1457 /* 1458 * Initialize vmbus, e.g. connect to Hypervisor. 1459 */ 1460 ret = vmbus_init(sc); 1461 if (ret != 0) 1462 goto cleanup; 1463 1464 if (sc->vmbus_version == VMBUS_VERSION_WS2008 || 1465 sc->vmbus_version == VMBUS_VERSION_WIN7) 1466 sc->vmbus_event_proc = vmbus_event_proc_compat; 1467 else 1468 sc->vmbus_event_proc = vmbus_event_proc; 1469 1470 ret = vmbus_scan(sc); 1471 if (ret != 0) 1472 goto cleanup; 1473 1474 ctx = device_get_sysctl_ctx(sc->vmbus_dev); 1475 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vmbus_dev)); 1476 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "version", 1477 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, 1478 vmbus_sysctl_version, "A", "vmbus version"); 1479 1480 return (ret); 1481 1482cleanup: 1483 vmbus_scan_teardown(sc); 1484 vmbus_intr_teardown(sc); 1485 vmbus_dma_free(sc); 1486 if (sc->vmbus_xc != NULL) { 1487 vmbus_xact_ctx_destroy(sc->vmbus_xc); 1488 sc->vmbus_xc = NULL; 1489 } 1490 free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF); 1491 mtx_destroy(&sc->vmbus_prichan_lock); 1492 mtx_destroy(&sc->vmbus_chan_lock); 1493 1494 return (ret); 1495} 1496 1497static void 1498vmbus_event_proc_dummy(struct vmbus_softc *sc __unused, int cpu __unused) 1499{ 1500} 1501 1502#ifdef EARLY_AP_STARTUP 1503 1504static void 1505vmbus_intrhook(void *xsc) 1506{ 1507 struct vmbus_softc *sc = xsc; 1508 1509 if (bootverbose) 1510 device_printf(sc->vmbus_dev, "intrhook\n"); 1511 vmbus_doattach(sc); 1512 config_intrhook_disestablish(&sc->vmbus_intrhook); 1513} 1514 1515#endif /* EARLY_AP_STARTUP */ 1516 1517static int 1518vmbus_attach(device_t dev) 1519{ 1520 vmbus_sc = device_get_softc(dev); 1521 vmbus_sc->vmbus_dev = dev; 1522 vmbus_sc->vmbus_idtvec = -1; 1523 1524 /* 1525 * Event processing logic will be configured: 1526 * - After the vmbus protocol version negotiation. 1527 * - Before we request channel offers. 1528 */ 1529 vmbus_sc->vmbus_event_proc = vmbus_event_proc_dummy; 1530 1531#ifdef EARLY_AP_STARTUP 1532 /* 1533 * Defer the real attach until the pause(9) works as expected. 1534 */ 1535 vmbus_sc->vmbus_intrhook.ich_func = vmbus_intrhook; 1536 vmbus_sc->vmbus_intrhook.ich_arg = vmbus_sc; 1537 config_intrhook_establish(&vmbus_sc->vmbus_intrhook); 1538#else /* !EARLY_AP_STARTUP */ 1539 /* 1540 * If the system has already booted and thread 1541 * scheduling is possible indicated by the global 1542 * cold set to zero, we just call the driver 1543 * initialization directly. 1544 */ 1545 if (!cold) 1546 vmbus_doattach(vmbus_sc); 1547#endif /* EARLY_AP_STARTUP */ 1548 1549 return (0); 1550} 1551 1552static int 1553vmbus_detach(device_t dev) 1554{ 1555 struct vmbus_softc *sc = device_get_softc(dev); 1556 1557 bus_generic_detach(dev); 1558 vmbus_chan_destroy_all(sc); 1559 1560 vmbus_scan_teardown(sc); 1561 1562 vmbus_disconnect(sc); 1563 1564 if (sc->vmbus_flags & VMBUS_FLAG_SYNIC) { 1565 sc->vmbus_flags &= ~VMBUS_FLAG_SYNIC; 1566 smp_rendezvous(NULL, vmbus_synic_teardown, NULL, NULL); 1567 } 1568 1569 vmbus_intr_teardown(sc); 1570 vmbus_dma_free(sc); 1571 1572 if (sc->vmbus_xc != NULL) { 1573 vmbus_xact_ctx_destroy(sc->vmbus_xc); 1574 sc->vmbus_xc = NULL; 1575 } 1576 1577 free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF); 1578 mtx_destroy(&sc->vmbus_prichan_lock); 1579 mtx_destroy(&sc->vmbus_chan_lock); 1580 1581#ifdef NEW_PCIB 1582 vmbus_free_mmio_res(dev); 1583#endif 1584 1585 return (0); 1586} 1587 1588#ifndef EARLY_AP_STARTUP 1589 1590static void 1591vmbus_sysinit(void *arg __unused) 1592{ 1593 struct vmbus_softc *sc = vmbus_get_softc(); 1594 1595 if (vm_guest != VM_GUEST_HV || sc == NULL) 1596 return; 1597 1598 /* 1599 * If the system has already booted and thread 1600 * scheduling is possible, as indicated by the 1601 * global cold set to zero, we just call the driver 1602 * initialization directly. 1603 */ 1604 if (!cold) 1605 vmbus_doattach(sc); 1606} 1607/* 1608 * NOTE: 1609 * We have to start as the last step of SI_SUB_SMP, i.e. after SMP is 1610 * initialized. 1611 */ 1612SYSINIT(vmbus_initialize, SI_SUB_SMP, SI_ORDER_ANY, vmbus_sysinit, NULL); 1613 1614#endif /* !EARLY_AP_STARTUP */ 1615