1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 NetApp, 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, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/bus.h> 40 #include <sys/pciio.h> 41 #include <sys/rman.h> 42 #include <sys/smp.h> 43 #include <sys/sysctl.h> 44 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pcireg.h> 47 48 #include <machine/resource.h> 49 50 #include <machine/vmm.h> 51 #include <machine/vmm_dev.h> 52 53 #include "vmm_lapic.h" 54 #include "vmm_ktr.h" 55 56 #include "iommu.h" 57 #include "ppt.h" 58 59 /* XXX locking */ 60 61 #define MAX_MSIMSGS 32 62 63 /* 64 * If the MSI-X table is located in the middle of a BAR then that MMIO 65 * region gets split into two segments - one segment above the MSI-X table 66 * and the other segment below the MSI-X table - with a hole in place of 67 * the MSI-X table so accesses to it can be trapped and emulated. 68 * 69 * So, allocate a MMIO segment for each BAR register + 1 additional segment. 70 */ 71 #define MAX_MMIOSEGS ((PCIR_MAX_BAR_0 + 1) + 1) 72 73 MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources"); 74 75 struct pptintr_arg { /* pptintr(pptintr_arg) */ 76 struct pptdev *pptdev; 77 uint64_t addr; 78 uint64_t msg_data; 79 }; 80 81 struct pptseg { 82 vm_paddr_t gpa; 83 size_t len; 84 int wired; 85 }; 86 87 struct pptdev { 88 device_t dev; 89 struct vm *vm; /* owner of this device */ 90 TAILQ_ENTRY(pptdev) next; 91 struct pptseg mmio[MAX_MMIOSEGS]; 92 struct { 93 int num_msgs; /* guest state */ 94 95 int startrid; /* host state */ 96 struct resource *res[MAX_MSIMSGS]; 97 void *cookie[MAX_MSIMSGS]; 98 struct pptintr_arg arg[MAX_MSIMSGS]; 99 } msi; 100 101 struct { 102 int num_msgs; 103 int startrid; 104 int msix_table_rid; 105 struct resource *msix_table_res; 106 struct resource **res; 107 void **cookie; 108 struct pptintr_arg *arg; 109 } msix; 110 }; 111 112 SYSCTL_DECL(_hw_vmm); 113 SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW, 0, "bhyve passthru devices"); 114 115 static int num_pptdevs; 116 SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0, 117 "number of pci passthru devices"); 118 119 static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list); 120 121 static int 122 ppt_probe(device_t dev) 123 { 124 int bus, slot, func; 125 struct pci_devinfo *dinfo; 126 127 dinfo = (struct pci_devinfo *)device_get_ivars(dev); 128 129 bus = pci_get_bus(dev); 130 slot = pci_get_slot(dev); 131 func = pci_get_function(dev); 132 133 /* 134 * To qualify as a pci passthrough device a device must: 135 * - be allowed by administrator to be used in this role 136 * - be an endpoint device 137 */ 138 if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL) 139 return (ENXIO); 140 else if (vmm_is_pptdev(bus, slot, func)) 141 return (0); 142 else 143 /* 144 * Returning BUS_PROBE_NOWILDCARD here matches devices that the 145 * SR-IOV infrastructure specified as "ppt" passthrough devices. 146 * All normal devices that did not have "ppt" specified as their 147 * driver will not be matched by this. 148 */ 149 return (BUS_PROBE_NOWILDCARD); 150 } 151 152 static int 153 ppt_attach(device_t dev) 154 { 155 struct pptdev *ppt; 156 157 ppt = device_get_softc(dev); 158 159 iommu_remove_device(iommu_host_domain(), pci_get_rid(dev)); 160 num_pptdevs++; 161 TAILQ_INSERT_TAIL(&pptdev_list, ppt, next); 162 ppt->dev = dev; 163 164 if (bootverbose) 165 device_printf(dev, "attached\n"); 166 167 return (0); 168 } 169 170 static int 171 ppt_detach(device_t dev) 172 { 173 struct pptdev *ppt; 174 175 ppt = device_get_softc(dev); 176 177 if (ppt->vm != NULL) 178 return (EBUSY); 179 num_pptdevs--; 180 TAILQ_REMOVE(&pptdev_list, ppt, next); 181 pci_disable_busmaster(dev); 182 iommu_add_device(iommu_host_domain(), pci_get_rid(dev)); 183 184 return (0); 185 } 186 187 static device_method_t ppt_methods[] = { 188 /* Device interface */ 189 DEVMETHOD(device_probe, ppt_probe), 190 DEVMETHOD(device_attach, ppt_attach), 191 DEVMETHOD(device_detach, ppt_detach), 192 {0, 0} 193 }; 194 195 static devclass_t ppt_devclass; 196 DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev)); 197 DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL); 198 199 static struct pptdev * 200 ppt_find(int bus, int slot, int func) 201 { 202 device_t dev; 203 struct pptdev *ppt; 204 int b, s, f; 205 206 TAILQ_FOREACH(ppt, &pptdev_list, next) { 207 dev = ppt->dev; 208 b = pci_get_bus(dev); 209 s = pci_get_slot(dev); 210 f = pci_get_function(dev); 211 if (bus == b && slot == s && func == f) 212 return (ppt); 213 } 214 return (NULL); 215 } 216 217 static void 218 ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt) 219 { 220 int i; 221 struct pptseg *seg; 222 223 for (i = 0; i < MAX_MMIOSEGS; i++) { 224 seg = &ppt->mmio[i]; 225 if (seg->len == 0) 226 continue; 227 (void)vm_unmap_mmio(vm, seg->gpa, seg->len); 228 bzero(seg, sizeof(struct pptseg)); 229 } 230 } 231 232 static void 233 ppt_teardown_msi(struct pptdev *ppt) 234 { 235 int i, rid; 236 void *cookie; 237 struct resource *res; 238 239 if (ppt->msi.num_msgs == 0) 240 return; 241 242 for (i = 0; i < ppt->msi.num_msgs; i++) { 243 rid = ppt->msi.startrid + i; 244 res = ppt->msi.res[i]; 245 cookie = ppt->msi.cookie[i]; 246 247 if (cookie != NULL) 248 bus_teardown_intr(ppt->dev, res, cookie); 249 250 if (res != NULL) 251 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); 252 253 ppt->msi.res[i] = NULL; 254 ppt->msi.cookie[i] = NULL; 255 } 256 257 if (ppt->msi.startrid == 1) 258 pci_release_msi(ppt->dev); 259 260 ppt->msi.num_msgs = 0; 261 } 262 263 static void 264 ppt_teardown_msix_intr(struct pptdev *ppt, int idx) 265 { 266 int rid; 267 struct resource *res; 268 void *cookie; 269 270 rid = ppt->msix.startrid + idx; 271 res = ppt->msix.res[idx]; 272 cookie = ppt->msix.cookie[idx]; 273 274 if (cookie != NULL) 275 bus_teardown_intr(ppt->dev, res, cookie); 276 277 if (res != NULL) 278 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); 279 280 ppt->msix.res[idx] = NULL; 281 ppt->msix.cookie[idx] = NULL; 282 } 283 284 static void 285 ppt_teardown_msix(struct pptdev *ppt) 286 { 287 int i; 288 289 if (ppt->msix.num_msgs == 0) 290 return; 291 292 for (i = 0; i < ppt->msix.num_msgs; i++) 293 ppt_teardown_msix_intr(ppt, i); 294 295 if (ppt->msix.msix_table_res) { 296 bus_release_resource(ppt->dev, SYS_RES_MEMORY, 297 ppt->msix.msix_table_rid, 298 ppt->msix.msix_table_res); 299 ppt->msix.msix_table_res = NULL; 300 ppt->msix.msix_table_rid = 0; 301 } 302 303 free(ppt->msix.res, M_PPTMSIX); 304 free(ppt->msix.cookie, M_PPTMSIX); 305 free(ppt->msix.arg, M_PPTMSIX); 306 307 pci_release_msi(ppt->dev); 308 309 ppt->msix.num_msgs = 0; 310 } 311 312 int 313 ppt_avail_devices(void) 314 { 315 316 return (num_pptdevs); 317 } 318 319 int 320 ppt_assigned_devices(struct vm *vm) 321 { 322 struct pptdev *ppt; 323 int num; 324 325 num = 0; 326 TAILQ_FOREACH(ppt, &pptdev_list, next) { 327 if (ppt->vm == vm) 328 num++; 329 } 330 return (num); 331 } 332 333 boolean_t 334 ppt_is_mmio(struct vm *vm, vm_paddr_t gpa) 335 { 336 int i; 337 struct pptdev *ppt; 338 struct pptseg *seg; 339 340 TAILQ_FOREACH(ppt, &pptdev_list, next) { 341 if (ppt->vm != vm) 342 continue; 343 344 for (i = 0; i < MAX_MMIOSEGS; i++) { 345 seg = &ppt->mmio[i]; 346 if (seg->len == 0) 347 continue; 348 if (gpa >= seg->gpa && gpa < seg->gpa + seg->len) 349 return (TRUE); 350 } 351 } 352 353 return (FALSE); 354 } 355 356 static void 357 ppt_pci_reset(device_t dev) 358 { 359 int ps; 360 361 if (pcie_flr(dev, 362 max(pcie_get_max_completion_timeout(dev) / 1000, 10), 363 true)) 364 return; 365 366 /* 367 * If FLR fails, attempt a power-management reset by cycling 368 * the device in/out of D3 state. 369 * PCI spec says we can only go into D3 state from D0 state. 370 * Transition from D[12] into D0 before going to D3 state. 371 */ 372 ps = pci_get_powerstate(dev); 373 if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3) 374 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 375 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3) 376 pci_set_powerstate(dev, PCI_POWERSTATE_D3); 377 pci_set_powerstate(dev, ps); 378 } 379 380 int 381 ppt_assign_device(struct vm *vm, int bus, int slot, int func) 382 { 383 struct pptdev *ppt; 384 385 ppt = ppt_find(bus, slot, func); 386 if (ppt != NULL) { 387 /* 388 * If this device is owned by a different VM then we 389 * cannot change its owner. 390 */ 391 if (ppt->vm != NULL && ppt->vm != vm) 392 return (EBUSY); 393 394 pci_save_state(ppt->dev); 395 ppt_pci_reset(ppt->dev); 396 pci_restore_state(ppt->dev); 397 ppt->vm = vm; 398 iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); 399 return (0); 400 } 401 return (ENOENT); 402 } 403 404 int 405 ppt_unassign_device(struct vm *vm, int bus, int slot, int func) 406 { 407 struct pptdev *ppt; 408 409 ppt = ppt_find(bus, slot, func); 410 if (ppt != NULL) { 411 /* 412 * If this device is not owned by this 'vm' then bail out. 413 */ 414 if (ppt->vm != vm) 415 return (EBUSY); 416 417 pci_save_state(ppt->dev); 418 ppt_pci_reset(ppt->dev); 419 pci_restore_state(ppt->dev); 420 ppt_unmap_mmio(vm, ppt); 421 ppt_teardown_msi(ppt); 422 ppt_teardown_msix(ppt); 423 iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); 424 ppt->vm = NULL; 425 return (0); 426 } 427 return (ENOENT); 428 } 429 430 int 431 ppt_unassign_all(struct vm *vm) 432 { 433 struct pptdev *ppt; 434 int bus, slot, func; 435 device_t dev; 436 437 TAILQ_FOREACH(ppt, &pptdev_list, next) { 438 if (ppt->vm == vm) { 439 dev = ppt->dev; 440 bus = pci_get_bus(dev); 441 slot = pci_get_slot(dev); 442 func = pci_get_function(dev); 443 vm_unassign_pptdev(vm, bus, slot, func); 444 } 445 } 446 447 return (0); 448 } 449 450 int 451 ppt_map_mmio(struct vm *vm, int bus, int slot, int func, 452 vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 453 { 454 int i, error; 455 struct pptseg *seg; 456 struct pptdev *ppt; 457 458 ppt = ppt_find(bus, slot, func); 459 if (ppt != NULL) { 460 if (ppt->vm != vm) 461 return (EBUSY); 462 463 for (i = 0; i < MAX_MMIOSEGS; i++) { 464 seg = &ppt->mmio[i]; 465 if (seg->len == 0) { 466 error = vm_map_mmio(vm, gpa, len, hpa); 467 if (error == 0) { 468 seg->gpa = gpa; 469 seg->len = len; 470 } 471 return (error); 472 } 473 } 474 return (ENOSPC); 475 } 476 return (ENOENT); 477 } 478 479 static int 480 pptintr(void *arg) 481 { 482 struct pptdev *ppt; 483 struct pptintr_arg *pptarg; 484 485 pptarg = arg; 486 ppt = pptarg->pptdev; 487 488 if (ppt->vm != NULL) 489 lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data); 490 else { 491 /* 492 * XXX 493 * This is not expected to happen - panic? 494 */ 495 } 496 497 /* 498 * For legacy interrupts give other filters a chance in case 499 * the interrupt was not generated by the passthrough device. 500 */ 501 if (ppt->msi.startrid == 0) 502 return (FILTER_STRAY); 503 else 504 return (FILTER_HANDLED); 505 } 506 507 int 508 ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func, 509 uint64_t addr, uint64_t msg, int numvec) 510 { 511 int i, rid, flags; 512 int msi_count, startrid, error, tmp; 513 struct pptdev *ppt; 514 515 if (numvec < 0 || numvec > MAX_MSIMSGS) 516 return (EINVAL); 517 518 ppt = ppt_find(bus, slot, func); 519 if (ppt == NULL) 520 return (ENOENT); 521 if (ppt->vm != vm) /* Make sure we own this device */ 522 return (EBUSY); 523 524 /* Free any allocated resources */ 525 ppt_teardown_msi(ppt); 526 527 if (numvec == 0) /* nothing more to do */ 528 return (0); 529 530 flags = RF_ACTIVE; 531 msi_count = pci_msi_count(ppt->dev); 532 if (msi_count == 0) { 533 startrid = 0; /* legacy interrupt */ 534 msi_count = 1; 535 flags |= RF_SHAREABLE; 536 } else 537 startrid = 1; /* MSI */ 538 539 /* 540 * The device must be capable of supporting the number of vectors 541 * the guest wants to allocate. 542 */ 543 if (numvec > msi_count) 544 return (EINVAL); 545 546 /* 547 * Make sure that we can allocate all the MSI vectors that are needed 548 * by the guest. 549 */ 550 if (startrid == 1) { 551 tmp = numvec; 552 error = pci_alloc_msi(ppt->dev, &tmp); 553 if (error) 554 return (error); 555 else if (tmp != numvec) { 556 pci_release_msi(ppt->dev); 557 return (ENOSPC); 558 } else { 559 /* success */ 560 } 561 } 562 563 ppt->msi.startrid = startrid; 564 565 /* 566 * Allocate the irq resource and attach it to the interrupt handler. 567 */ 568 for (i = 0; i < numvec; i++) { 569 ppt->msi.num_msgs = i + 1; 570 ppt->msi.cookie[i] = NULL; 571 572 rid = startrid + i; 573 ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, 574 &rid, flags); 575 if (ppt->msi.res[i] == NULL) 576 break; 577 578 ppt->msi.arg[i].pptdev = ppt; 579 ppt->msi.arg[i].addr = addr; 580 ppt->msi.arg[i].msg_data = msg + i; 581 582 error = bus_setup_intr(ppt->dev, ppt->msi.res[i], 583 INTR_TYPE_NET | INTR_MPSAFE, 584 pptintr, NULL, &ppt->msi.arg[i], 585 &ppt->msi.cookie[i]); 586 if (error != 0) 587 break; 588 } 589 590 if (i < numvec) { 591 ppt_teardown_msi(ppt); 592 return (ENXIO); 593 } 594 595 return (0); 596 } 597 598 int 599 ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func, 600 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) 601 { 602 struct pptdev *ppt; 603 struct pci_devinfo *dinfo; 604 int numvec, alloced, rid, error; 605 size_t res_size, cookie_size, arg_size; 606 607 ppt = ppt_find(bus, slot, func); 608 if (ppt == NULL) 609 return (ENOENT); 610 if (ppt->vm != vm) /* Make sure we own this device */ 611 return (EBUSY); 612 613 dinfo = device_get_ivars(ppt->dev); 614 if (!dinfo) 615 return (ENXIO); 616 617 /* 618 * First-time configuration: 619 * Allocate the MSI-X table 620 * Allocate the IRQ resources 621 * Set up some variables in ppt->msix 622 */ 623 if (ppt->msix.num_msgs == 0) { 624 numvec = pci_msix_count(ppt->dev); 625 if (numvec <= 0) 626 return (EINVAL); 627 628 ppt->msix.startrid = 1; 629 ppt->msix.num_msgs = numvec; 630 631 res_size = numvec * sizeof(ppt->msix.res[0]); 632 cookie_size = numvec * sizeof(ppt->msix.cookie[0]); 633 arg_size = numvec * sizeof(ppt->msix.arg[0]); 634 635 ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO); 636 ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX, 637 M_WAITOK | M_ZERO); 638 ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO); 639 640 rid = dinfo->cfg.msix.msix_table_bar; 641 ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev, 642 SYS_RES_MEMORY, &rid, RF_ACTIVE); 643 644 if (ppt->msix.msix_table_res == NULL) { 645 ppt_teardown_msix(ppt); 646 return (ENOSPC); 647 } 648 ppt->msix.msix_table_rid = rid; 649 650 alloced = numvec; 651 error = pci_alloc_msix(ppt->dev, &alloced); 652 if (error || alloced != numvec) { 653 ppt_teardown_msix(ppt); 654 return (error == 0 ? ENOSPC: error); 655 } 656 } 657 658 if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { 659 /* Tear down the IRQ if it's already set up */ 660 ppt_teardown_msix_intr(ppt, idx); 661 662 /* Allocate the IRQ resource */ 663 ppt->msix.cookie[idx] = NULL; 664 rid = ppt->msix.startrid + idx; 665 ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, 666 &rid, RF_ACTIVE); 667 if (ppt->msix.res[idx] == NULL) 668 return (ENXIO); 669 670 ppt->msix.arg[idx].pptdev = ppt; 671 ppt->msix.arg[idx].addr = addr; 672 ppt->msix.arg[idx].msg_data = msg; 673 674 /* Setup the MSI-X interrupt */ 675 error = bus_setup_intr(ppt->dev, ppt->msix.res[idx], 676 INTR_TYPE_NET | INTR_MPSAFE, 677 pptintr, NULL, &ppt->msix.arg[idx], 678 &ppt->msix.cookie[idx]); 679 680 if (error != 0) { 681 bus_teardown_intr(ppt->dev, ppt->msix.res[idx], ppt->msix.cookie[idx]); 682 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]); 683 ppt->msix.cookie[idx] = NULL; 684 ppt->msix.res[idx] = NULL; 685 return (ENXIO); 686 } 687 } else { 688 /* Masked, tear it down if it's already been set up */ 689 ppt_teardown_msix_intr(ppt, idx); 690 } 691 692 return (0); 693 } 694