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