1366f6083SPeter Grehan /*- 2c49761ddSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3c49761ddSPedro F. Giffuni * 4366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc. 5366f6083SPeter Grehan * All rights reserved. 6366f6083SPeter Grehan * 7366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without 8366f6083SPeter Grehan * modification, are permitted provided that the following conditions 9366f6083SPeter Grehan * are met: 10366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright 11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer. 12366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 13366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the 14366f6083SPeter Grehan * documentation and/or other materials provided with the distribution. 15366f6083SPeter Grehan * 16366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26366f6083SPeter Grehan * SUCH DAMAGE. 27366f6083SPeter Grehan * 28366f6083SPeter Grehan * $FreeBSD$ 29366f6083SPeter Grehan */ 30366f6083SPeter Grehan 31366f6083SPeter Grehan #include <sys/cdefs.h> 32366f6083SPeter Grehan __FBSDID("$FreeBSD$"); 33366f6083SPeter Grehan 34366f6083SPeter Grehan #include <sys/param.h> 35366f6083SPeter Grehan #include <sys/systm.h> 36366f6083SPeter Grehan #include <sys/kernel.h> 37cd942e0fSPeter Grehan #include <sys/malloc.h> 38366f6083SPeter Grehan #include <sys/module.h> 39366f6083SPeter Grehan #include <sys/bus.h> 40366f6083SPeter Grehan #include <sys/pciio.h> 41366f6083SPeter Grehan #include <sys/rman.h> 42a5615c90SPeter Grehan #include <sys/smp.h> 4351f45d01SNeel Natu #include <sys/sysctl.h> 44366f6083SPeter Grehan 45366f6083SPeter Grehan #include <dev/pci/pcivar.h> 46366f6083SPeter Grehan #include <dev/pci/pcireg.h> 47366f6083SPeter Grehan 48366f6083SPeter Grehan #include <machine/resource.h> 49366f6083SPeter Grehan 50366f6083SPeter Grehan #include <machine/vmm.h> 51366f6083SPeter Grehan #include <machine/vmm_dev.h> 52366f6083SPeter Grehan 53366f6083SPeter Grehan #include "vmm_lapic.h" 54366f6083SPeter Grehan #include "vmm_ktr.h" 55366f6083SPeter Grehan 56366f6083SPeter Grehan #include "iommu.h" 57366f6083SPeter Grehan #include "ppt.h" 58366f6083SPeter Grehan 5975dd3366SNeel Natu /* XXX locking */ 6075dd3366SNeel Natu 61366f6083SPeter Grehan #define MAX_MSIMSGS 32 62366f6083SPeter Grehan 632b89a044SNeel Natu /* 642b89a044SNeel Natu * If the MSI-X table is located in the middle of a BAR then that MMIO 652b89a044SNeel Natu * region gets split into two segments - one segment above the MSI-X table 662b89a044SNeel Natu * and the other segment below the MSI-X table - with a hole in place of 672b89a044SNeel Natu * the MSI-X table so accesses to it can be trapped and emulated. 682b89a044SNeel Natu * 692b89a044SNeel Natu * So, allocate a MMIO segment for each BAR register + 1 additional segment. 702b89a044SNeel Natu */ 712b89a044SNeel Natu #define MAX_MMIOSEGS ((PCIR_MAX_BAR_0 + 1) + 1) 722b89a044SNeel Natu 73cd942e0fSPeter Grehan MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources"); 74cd942e0fSPeter Grehan 75366f6083SPeter Grehan struct pptintr_arg { /* pptintr(pptintr_arg) */ 76366f6083SPeter Grehan struct pptdev *pptdev; 774f8be175SNeel Natu uint64_t addr; 784f8be175SNeel Natu uint64_t msg_data; 79366f6083SPeter Grehan }; 80366f6083SPeter Grehan 819b1aa8d6SNeel Natu struct pptseg { 829b1aa8d6SNeel Natu vm_paddr_t gpa; 839b1aa8d6SNeel Natu size_t len; 849b1aa8d6SNeel Natu int wired; 859b1aa8d6SNeel Natu }; 869b1aa8d6SNeel Natu 87a15f820aSRyan Stone struct pptdev { 88366f6083SPeter Grehan device_t dev; 89366f6083SPeter Grehan struct vm *vm; /* owner of this device */ 90a15f820aSRyan Stone TAILQ_ENTRY(pptdev) next; 919b1aa8d6SNeel Natu struct pptseg mmio[MAX_MMIOSEGS]; 92366f6083SPeter Grehan struct { 93366f6083SPeter Grehan int num_msgs; /* guest state */ 94366f6083SPeter Grehan 95366f6083SPeter Grehan int startrid; /* host state */ 96366f6083SPeter Grehan struct resource *res[MAX_MSIMSGS]; 97366f6083SPeter Grehan void *cookie[MAX_MSIMSGS]; 98366f6083SPeter Grehan struct pptintr_arg arg[MAX_MSIMSGS]; 99366f6083SPeter Grehan } msi; 100cd942e0fSPeter Grehan 101cd942e0fSPeter Grehan struct { 102cd942e0fSPeter Grehan int num_msgs; 103cd942e0fSPeter Grehan int startrid; 104cd942e0fSPeter Grehan int msix_table_rid; 1050d1fd6e5SJohn Baldwin int msix_pba_rid; 106cd942e0fSPeter Grehan struct resource *msix_table_res; 1070d1fd6e5SJohn Baldwin struct resource *msix_pba_res; 108cd942e0fSPeter Grehan struct resource **res; 109cd942e0fSPeter Grehan void **cookie; 110cd942e0fSPeter Grehan struct pptintr_arg *arg; 111cd942e0fSPeter Grehan } msix; 112a15f820aSRyan Stone }; 113366f6083SPeter Grehan 11451f45d01SNeel Natu SYSCTL_DECL(_hw_vmm); 115b40598c5SPawel Biernacki SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 116b40598c5SPawel Biernacki "bhyve passthru devices"); 11751f45d01SNeel Natu 118366f6083SPeter Grehan static int num_pptdevs; 11951f45d01SNeel Natu SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0, 12051f45d01SNeel Natu "number of pci passthru devices"); 121366f6083SPeter Grehan 122a15f820aSRyan Stone static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list); 123a15f820aSRyan Stone 124366f6083SPeter Grehan static int 125366f6083SPeter Grehan ppt_probe(device_t dev) 126366f6083SPeter Grehan { 127366f6083SPeter Grehan int bus, slot, func; 128366f6083SPeter Grehan struct pci_devinfo *dinfo; 129366f6083SPeter Grehan 130366f6083SPeter Grehan dinfo = (struct pci_devinfo *)device_get_ivars(dev); 131366f6083SPeter Grehan 132366f6083SPeter Grehan bus = pci_get_bus(dev); 133366f6083SPeter Grehan slot = pci_get_slot(dev); 134366f6083SPeter Grehan func = pci_get_function(dev); 135366f6083SPeter Grehan 136366f6083SPeter Grehan /* 137366f6083SPeter Grehan * To qualify as a pci passthrough device a device must: 138366f6083SPeter Grehan * - be allowed by administrator to be used in this role 139366f6083SPeter Grehan * - be an endpoint device 140366f6083SPeter Grehan */ 141a15f820aSRyan Stone if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL) 142a15f820aSRyan Stone return (ENXIO); 143a15f820aSRyan Stone else if (vmm_is_pptdev(bus, slot, func)) 144366f6083SPeter Grehan return (0); 145366f6083SPeter Grehan else 146a15f820aSRyan Stone /* 147a15f820aSRyan Stone * Returning BUS_PROBE_NOWILDCARD here matches devices that the 148a15f820aSRyan Stone * SR-IOV infrastructure specified as "ppt" passthrough devices. 149a15f820aSRyan Stone * All normal devices that did not have "ppt" specified as their 150a15f820aSRyan Stone * driver will not be matched by this. 151a15f820aSRyan Stone */ 152a15f820aSRyan Stone return (BUS_PROBE_NOWILDCARD); 153366f6083SPeter Grehan } 154366f6083SPeter Grehan 155366f6083SPeter Grehan static int 156366f6083SPeter Grehan ppt_attach(device_t dev) 157366f6083SPeter Grehan { 158a15f820aSRyan Stone struct pptdev *ppt; 159366f6083SPeter Grehan 160a15f820aSRyan Stone ppt = device_get_softc(dev); 161366f6083SPeter Grehan 1626db55a0fSJohn Baldwin iommu_remove_device(iommu_host_domain(), pci_get_rid(dev)); 163a15f820aSRyan Stone num_pptdevs++; 164a15f820aSRyan Stone TAILQ_INSERT_TAIL(&pptdev_list, ppt, next); 165a15f820aSRyan Stone ppt->dev = dev; 166366f6083SPeter Grehan 167366f6083SPeter Grehan if (bootverbose) 168366f6083SPeter Grehan device_printf(dev, "attached\n"); 169366f6083SPeter Grehan 170366f6083SPeter Grehan return (0); 171366f6083SPeter Grehan } 172366f6083SPeter Grehan 173366f6083SPeter Grehan static int 174366f6083SPeter Grehan ppt_detach(device_t dev) 175366f6083SPeter Grehan { 176a15f820aSRyan Stone struct pptdev *ppt; 177a15f820aSRyan Stone 178a15f820aSRyan Stone ppt = device_get_softc(dev); 179a15f820aSRyan Stone 180a15f820aSRyan Stone if (ppt->vm != NULL) 181a15f820aSRyan Stone return (EBUSY); 182a15f820aSRyan Stone num_pptdevs--; 183a15f820aSRyan Stone TAILQ_REMOVE(&pptdev_list, ppt, next); 1846db55a0fSJohn Baldwin pci_disable_busmaster(dev); 1856db55a0fSJohn Baldwin iommu_add_device(iommu_host_domain(), pci_get_rid(dev)); 186366f6083SPeter Grehan 187366f6083SPeter Grehan return (0); 188366f6083SPeter Grehan } 189366f6083SPeter Grehan 190366f6083SPeter Grehan static device_method_t ppt_methods[] = { 191366f6083SPeter Grehan /* Device interface */ 192366f6083SPeter Grehan DEVMETHOD(device_probe, ppt_probe), 193366f6083SPeter Grehan DEVMETHOD(device_attach, ppt_attach), 194366f6083SPeter Grehan DEVMETHOD(device_detach, ppt_detach), 195366f6083SPeter Grehan {0, 0} 196366f6083SPeter Grehan }; 197366f6083SPeter Grehan 198366f6083SPeter Grehan static devclass_t ppt_devclass; 199a15f820aSRyan Stone DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev)); 200366f6083SPeter Grehan DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL); 201366f6083SPeter Grehan 202*908dca3eSJohn Baldwin static int 203*908dca3eSJohn Baldwin ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp) 204366f6083SPeter Grehan { 205366f6083SPeter Grehan device_t dev; 206a15f820aSRyan Stone struct pptdev *ppt; 207a15f820aSRyan Stone int b, s, f; 208366f6083SPeter Grehan 209a15f820aSRyan Stone TAILQ_FOREACH(ppt, &pptdev_list, next) { 210a15f820aSRyan Stone dev = ppt->dev; 211366f6083SPeter Grehan b = pci_get_bus(dev); 212366f6083SPeter Grehan s = pci_get_slot(dev); 213366f6083SPeter Grehan f = pci_get_function(dev); 214366f6083SPeter Grehan if (bus == b && slot == s && func == f) 215*908dca3eSJohn Baldwin break; 216366f6083SPeter Grehan } 217*908dca3eSJohn Baldwin 218*908dca3eSJohn Baldwin if (ppt == NULL) 219*908dca3eSJohn Baldwin return (ENOENT); 220*908dca3eSJohn Baldwin if (ppt->vm != vm) /* Make sure we own this device */ 221*908dca3eSJohn Baldwin return (EBUSY); 222*908dca3eSJohn Baldwin *pptp = ppt; 223*908dca3eSJohn Baldwin return (0); 224366f6083SPeter Grehan } 225366f6083SPeter Grehan 226366f6083SPeter Grehan static void 227366f6083SPeter Grehan ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt) 228366f6083SPeter Grehan { 229366f6083SPeter Grehan int i; 2309b1aa8d6SNeel Natu struct pptseg *seg; 231366f6083SPeter Grehan 232366f6083SPeter Grehan for (i = 0; i < MAX_MMIOSEGS; i++) { 233366f6083SPeter Grehan seg = &ppt->mmio[i]; 234366f6083SPeter Grehan if (seg->len == 0) 235366f6083SPeter Grehan continue; 236366f6083SPeter Grehan (void)vm_unmap_mmio(vm, seg->gpa, seg->len); 2379b1aa8d6SNeel Natu bzero(seg, sizeof(struct pptseg)); 238366f6083SPeter Grehan } 239366f6083SPeter Grehan } 240366f6083SPeter Grehan 241366f6083SPeter Grehan static void 242366f6083SPeter Grehan ppt_teardown_msi(struct pptdev *ppt) 243366f6083SPeter Grehan { 244366f6083SPeter Grehan int i, rid; 245366f6083SPeter Grehan void *cookie; 246366f6083SPeter Grehan struct resource *res; 247366f6083SPeter Grehan 248366f6083SPeter Grehan if (ppt->msi.num_msgs == 0) 249366f6083SPeter Grehan return; 250366f6083SPeter Grehan 251366f6083SPeter Grehan for (i = 0; i < ppt->msi.num_msgs; i++) { 252366f6083SPeter Grehan rid = ppt->msi.startrid + i; 253366f6083SPeter Grehan res = ppt->msi.res[i]; 254366f6083SPeter Grehan cookie = ppt->msi.cookie[i]; 255366f6083SPeter Grehan 256366f6083SPeter Grehan if (cookie != NULL) 257366f6083SPeter Grehan bus_teardown_intr(ppt->dev, res, cookie); 258366f6083SPeter Grehan 259366f6083SPeter Grehan if (res != NULL) 260366f6083SPeter Grehan bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); 261366f6083SPeter Grehan 262366f6083SPeter Grehan ppt->msi.res[i] = NULL; 263366f6083SPeter Grehan ppt->msi.cookie[i] = NULL; 264366f6083SPeter Grehan } 265366f6083SPeter Grehan 266366f6083SPeter Grehan if (ppt->msi.startrid == 1) 267366f6083SPeter Grehan pci_release_msi(ppt->dev); 268366f6083SPeter Grehan 269366f6083SPeter Grehan ppt->msi.num_msgs = 0; 270366f6083SPeter Grehan } 271366f6083SPeter Grehan 272cd942e0fSPeter Grehan static void 273cd942e0fSPeter Grehan ppt_teardown_msix_intr(struct pptdev *ppt, int idx) 274cd942e0fSPeter Grehan { 275cd942e0fSPeter Grehan int rid; 276cd942e0fSPeter Grehan struct resource *res; 277cd942e0fSPeter Grehan void *cookie; 278cd942e0fSPeter Grehan 279cd942e0fSPeter Grehan rid = ppt->msix.startrid + idx; 280cd942e0fSPeter Grehan res = ppt->msix.res[idx]; 281cd942e0fSPeter Grehan cookie = ppt->msix.cookie[idx]; 282cd942e0fSPeter Grehan 283cd942e0fSPeter Grehan if (cookie != NULL) 284cd942e0fSPeter Grehan bus_teardown_intr(ppt->dev, res, cookie); 285cd942e0fSPeter Grehan 286cd942e0fSPeter Grehan if (res != NULL) 287cd942e0fSPeter Grehan bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res); 288cd942e0fSPeter Grehan 289cd942e0fSPeter Grehan ppt->msix.res[idx] = NULL; 290cd942e0fSPeter Grehan ppt->msix.cookie[idx] = NULL; 291cd942e0fSPeter Grehan } 292cd942e0fSPeter Grehan 293cd942e0fSPeter Grehan static void 294cd942e0fSPeter Grehan ppt_teardown_msix(struct pptdev *ppt) 295cd942e0fSPeter Grehan { 296920bc340SNeel Natu int i; 297cd942e0fSPeter Grehan 298cd942e0fSPeter Grehan if (ppt->msix.num_msgs == 0) 299cd942e0fSPeter Grehan return; 300cd942e0fSPeter Grehan 301cd942e0fSPeter Grehan for (i = 0; i < ppt->msix.num_msgs; i++) 302cd942e0fSPeter Grehan ppt_teardown_msix_intr(ppt, i); 303cd942e0fSPeter Grehan 3040d1fd6e5SJohn Baldwin free(ppt->msix.res, M_PPTMSIX); 3050d1fd6e5SJohn Baldwin free(ppt->msix.cookie, M_PPTMSIX); 3060d1fd6e5SJohn Baldwin free(ppt->msix.arg, M_PPTMSIX); 3070d1fd6e5SJohn Baldwin 3080d1fd6e5SJohn Baldwin pci_release_msi(ppt->dev); 3090d1fd6e5SJohn Baldwin 310cd942e0fSPeter Grehan if (ppt->msix.msix_table_res) { 311cd942e0fSPeter Grehan bus_release_resource(ppt->dev, SYS_RES_MEMORY, 312cd942e0fSPeter Grehan ppt->msix.msix_table_rid, 313cd942e0fSPeter Grehan ppt->msix.msix_table_res); 314cd942e0fSPeter Grehan ppt->msix.msix_table_res = NULL; 315cd942e0fSPeter Grehan ppt->msix.msix_table_rid = 0; 316cd942e0fSPeter Grehan } 3170d1fd6e5SJohn Baldwin if (ppt->msix.msix_pba_res) { 3180d1fd6e5SJohn Baldwin bus_release_resource(ppt->dev, SYS_RES_MEMORY, 3190d1fd6e5SJohn Baldwin ppt->msix.msix_pba_rid, 3200d1fd6e5SJohn Baldwin ppt->msix.msix_pba_res); 3210d1fd6e5SJohn Baldwin ppt->msix.msix_pba_res = NULL; 3220d1fd6e5SJohn Baldwin ppt->msix.msix_pba_rid = 0; 3230d1fd6e5SJohn Baldwin } 324cd942e0fSPeter Grehan 325cd942e0fSPeter Grehan ppt->msix.num_msgs = 0; 326cd942e0fSPeter Grehan } 327cd942e0fSPeter Grehan 328366f6083SPeter Grehan int 32951f45d01SNeel Natu ppt_avail_devices(void) 33051f45d01SNeel Natu { 33151f45d01SNeel Natu 33251f45d01SNeel Natu return (num_pptdevs); 33351f45d01SNeel Natu } 33451f45d01SNeel Natu 33551f45d01SNeel Natu int 33651f45d01SNeel Natu ppt_assigned_devices(struct vm *vm) 337318224bbSNeel Natu { 338a15f820aSRyan Stone struct pptdev *ppt; 339a15f820aSRyan Stone int num; 340318224bbSNeel Natu 341318224bbSNeel Natu num = 0; 342a15f820aSRyan Stone TAILQ_FOREACH(ppt, &pptdev_list, next) { 343a15f820aSRyan Stone if (ppt->vm == vm) 344318224bbSNeel Natu num++; 345318224bbSNeel Natu } 346318224bbSNeel Natu return (num); 347318224bbSNeel Natu } 348318224bbSNeel Natu 349490d56c5SEd Maste bool 350318224bbSNeel Natu ppt_is_mmio(struct vm *vm, vm_paddr_t gpa) 351318224bbSNeel Natu { 352a15f820aSRyan Stone int i; 353318224bbSNeel Natu struct pptdev *ppt; 3549b1aa8d6SNeel Natu struct pptseg *seg; 355318224bbSNeel Natu 356a15f820aSRyan Stone TAILQ_FOREACH(ppt, &pptdev_list, next) { 357318224bbSNeel Natu if (ppt->vm != vm) 358318224bbSNeel Natu continue; 359318224bbSNeel Natu 360318224bbSNeel Natu for (i = 0; i < MAX_MMIOSEGS; i++) { 361318224bbSNeel Natu seg = &ppt->mmio[i]; 362318224bbSNeel Natu if (seg->len == 0) 363318224bbSNeel Natu continue; 364318224bbSNeel Natu if (gpa >= seg->gpa && gpa < seg->gpa + seg->len) 365490d56c5SEd Maste return (true); 366318224bbSNeel Natu } 367318224bbSNeel Natu } 368318224bbSNeel Natu 369490d56c5SEd Maste return (false); 370318224bbSNeel Natu } 371318224bbSNeel Natu 372adb947a6SPeter Grehan static void 373adb947a6SPeter Grehan ppt_pci_reset(device_t dev) 374adb947a6SPeter Grehan { 375adb947a6SPeter Grehan 376adb947a6SPeter Grehan if (pcie_flr(dev, 3775db2a4a8SKonstantin Belousov max(pcie_get_max_completion_timeout(dev) / 1000, 10), true)) 378adb947a6SPeter Grehan return; 379adb947a6SPeter Grehan 3805db2a4a8SKonstantin Belousov pci_power_reset(dev); 381adb947a6SPeter Grehan } 382adb947a6SPeter Grehan 383318224bbSNeel Natu int 384366f6083SPeter Grehan ppt_assign_device(struct vm *vm, int bus, int slot, int func) 385366f6083SPeter Grehan { 386366f6083SPeter Grehan struct pptdev *ppt; 387*908dca3eSJohn Baldwin int error; 388366f6083SPeter Grehan 389*908dca3eSJohn Baldwin /* Passing NULL requires the device to be unowned. */ 390*908dca3eSJohn Baldwin error = ppt_find(NULL, bus, slot, func, &ppt); 391*908dca3eSJohn Baldwin if (error) 392*908dca3eSJohn Baldwin return (error); 393366f6083SPeter Grehan 394da0fc925SJohn Baldwin pci_save_state(ppt->dev); 395adb947a6SPeter Grehan ppt_pci_reset(ppt->dev); 396da0fc925SJohn Baldwin pci_restore_state(ppt->dev); 397366f6083SPeter Grehan ppt->vm = vm; 398a8667250SRyan Stone iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); 399366f6083SPeter Grehan return (0); 400366f6083SPeter Grehan } 401366f6083SPeter Grehan 402366f6083SPeter Grehan int 403366f6083SPeter Grehan ppt_unassign_device(struct vm *vm, int bus, int slot, int func) 404366f6083SPeter Grehan { 405366f6083SPeter Grehan struct pptdev *ppt; 406*908dca3eSJohn Baldwin int error; 407366f6083SPeter Grehan 408*908dca3eSJohn Baldwin error = ppt_find(vm, bus, slot, func, &ppt); 409*908dca3eSJohn Baldwin if (error) 410*908dca3eSJohn Baldwin return (error); 411da0fc925SJohn Baldwin 412da0fc925SJohn Baldwin pci_save_state(ppt->dev); 413adb947a6SPeter Grehan ppt_pci_reset(ppt->dev); 414da0fc925SJohn Baldwin pci_restore_state(ppt->dev); 415366f6083SPeter Grehan ppt_unmap_mmio(vm, ppt); 416366f6083SPeter Grehan ppt_teardown_msi(ppt); 417cd942e0fSPeter Grehan ppt_teardown_msix(ppt); 418a8667250SRyan Stone iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); 419366f6083SPeter Grehan ppt->vm = NULL; 420366f6083SPeter Grehan return (0); 421366f6083SPeter Grehan } 422366f6083SPeter Grehan 423366f6083SPeter Grehan int 424366f6083SPeter Grehan ppt_unassign_all(struct vm *vm) 425366f6083SPeter Grehan { 426a15f820aSRyan Stone struct pptdev *ppt; 427a15f820aSRyan Stone int bus, slot, func; 428366f6083SPeter Grehan device_t dev; 429366f6083SPeter Grehan 430a15f820aSRyan Stone TAILQ_FOREACH(ppt, &pptdev_list, next) { 431a15f820aSRyan Stone if (ppt->vm == vm) { 432a15f820aSRyan Stone dev = ppt->dev; 433366f6083SPeter Grehan bus = pci_get_bus(dev); 434366f6083SPeter Grehan slot = pci_get_slot(dev); 435366f6083SPeter Grehan func = pci_get_function(dev); 436318224bbSNeel Natu vm_unassign_pptdev(vm, bus, slot, func); 437366f6083SPeter Grehan } 438366f6083SPeter Grehan } 439366f6083SPeter Grehan 440366f6083SPeter Grehan return (0); 441366f6083SPeter Grehan } 442366f6083SPeter Grehan 443366f6083SPeter Grehan int 444366f6083SPeter Grehan ppt_map_mmio(struct vm *vm, int bus, int slot, int func, 445366f6083SPeter Grehan vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 446366f6083SPeter Grehan { 447366f6083SPeter Grehan int i, error; 4489b1aa8d6SNeel Natu struct pptseg *seg; 449366f6083SPeter Grehan struct pptdev *ppt; 450366f6083SPeter Grehan 451*908dca3eSJohn Baldwin error = ppt_find(vm, bus, slot, func, &ppt); 452*908dca3eSJohn Baldwin if (error) 453*908dca3eSJohn Baldwin return (error); 454366f6083SPeter Grehan 455366f6083SPeter Grehan for (i = 0; i < MAX_MMIOSEGS; i++) { 456366f6083SPeter Grehan seg = &ppt->mmio[i]; 457366f6083SPeter Grehan if (seg->len == 0) { 458366f6083SPeter Grehan error = vm_map_mmio(vm, gpa, len, hpa); 459366f6083SPeter Grehan if (error == 0) { 460366f6083SPeter Grehan seg->gpa = gpa; 461366f6083SPeter Grehan seg->len = len; 462366f6083SPeter Grehan } 463366f6083SPeter Grehan return (error); 464366f6083SPeter Grehan } 465366f6083SPeter Grehan } 466366f6083SPeter Grehan return (ENOSPC); 467366f6083SPeter Grehan } 468366f6083SPeter Grehan 469366f6083SPeter Grehan static int 470366f6083SPeter Grehan pptintr(void *arg) 471366f6083SPeter Grehan { 472366f6083SPeter Grehan struct pptdev *ppt; 473366f6083SPeter Grehan struct pptintr_arg *pptarg; 474366f6083SPeter Grehan 475366f6083SPeter Grehan pptarg = arg; 476366f6083SPeter Grehan ppt = pptarg->pptdev; 477366f6083SPeter Grehan 478366f6083SPeter Grehan if (ppt->vm != NULL) 4794f8be175SNeel Natu lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data); 480366f6083SPeter Grehan else { 481366f6083SPeter Grehan /* 482366f6083SPeter Grehan * XXX 483366f6083SPeter Grehan * This is not expected to happen - panic? 484366f6083SPeter Grehan */ 485366f6083SPeter Grehan } 486366f6083SPeter Grehan 487366f6083SPeter Grehan /* 488366f6083SPeter Grehan * For legacy interrupts give other filters a chance in case 489366f6083SPeter Grehan * the interrupt was not generated by the passthrough device. 490366f6083SPeter Grehan */ 491366f6083SPeter Grehan if (ppt->msi.startrid == 0) 492366f6083SPeter Grehan return (FILTER_STRAY); 493366f6083SPeter Grehan else 494366f6083SPeter Grehan return (FILTER_HANDLED); 495366f6083SPeter Grehan } 496366f6083SPeter Grehan 497366f6083SPeter Grehan int 498366f6083SPeter Grehan ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func, 4994f8be175SNeel Natu uint64_t addr, uint64_t msg, int numvec) 500366f6083SPeter Grehan { 501366f6083SPeter Grehan int i, rid, flags; 502366f6083SPeter Grehan int msi_count, startrid, error, tmp; 503366f6083SPeter Grehan struct pptdev *ppt; 504366f6083SPeter Grehan 5054f8be175SNeel Natu if (numvec < 0 || numvec > MAX_MSIMSGS) 506366f6083SPeter Grehan return (EINVAL); 507366f6083SPeter Grehan 508*908dca3eSJohn Baldwin error = ppt_find(vm, bus, slot, func, &ppt); 509*908dca3eSJohn Baldwin if (error) 510*908dca3eSJohn Baldwin return (error); 511366f6083SPeter Grehan 5121925586eSJohn Baldwin /* Reject attempts to enable MSI while MSI-X is active. */ 5131925586eSJohn Baldwin if (ppt->msix.num_msgs != 0 && numvec != 0) 5141925586eSJohn Baldwin return (EBUSY); 5151925586eSJohn Baldwin 516366f6083SPeter Grehan /* Free any allocated resources */ 517485b3300SNeel Natu ppt_teardown_msi(ppt); 518366f6083SPeter Grehan 519366f6083SPeter Grehan if (numvec == 0) /* nothing more to do */ 520366f6083SPeter Grehan return (0); 521366f6083SPeter Grehan 522366f6083SPeter Grehan flags = RF_ACTIVE; 523366f6083SPeter Grehan msi_count = pci_msi_count(ppt->dev); 524366f6083SPeter Grehan if (msi_count == 0) { 525366f6083SPeter Grehan startrid = 0; /* legacy interrupt */ 526366f6083SPeter Grehan msi_count = 1; 527366f6083SPeter Grehan flags |= RF_SHAREABLE; 528366f6083SPeter Grehan } else 529366f6083SPeter Grehan startrid = 1; /* MSI */ 530366f6083SPeter Grehan 531366f6083SPeter Grehan /* 532366f6083SPeter Grehan * The device must be capable of supporting the number of vectors 533366f6083SPeter Grehan * the guest wants to allocate. 534366f6083SPeter Grehan */ 535366f6083SPeter Grehan if (numvec > msi_count) 536366f6083SPeter Grehan return (EINVAL); 537366f6083SPeter Grehan 538366f6083SPeter Grehan /* 539366f6083SPeter Grehan * Make sure that we can allocate all the MSI vectors that are needed 540366f6083SPeter Grehan * by the guest. 541366f6083SPeter Grehan */ 542366f6083SPeter Grehan if (startrid == 1) { 543366f6083SPeter Grehan tmp = numvec; 544366f6083SPeter Grehan error = pci_alloc_msi(ppt->dev, &tmp); 545366f6083SPeter Grehan if (error) 546366f6083SPeter Grehan return (error); 547366f6083SPeter Grehan else if (tmp != numvec) { 548366f6083SPeter Grehan pci_release_msi(ppt->dev); 549366f6083SPeter Grehan return (ENOSPC); 550366f6083SPeter Grehan } else { 551366f6083SPeter Grehan /* success */ 552366f6083SPeter Grehan } 553366f6083SPeter Grehan } 554366f6083SPeter Grehan 555366f6083SPeter Grehan ppt->msi.startrid = startrid; 556366f6083SPeter Grehan 557366f6083SPeter Grehan /* 558366f6083SPeter Grehan * Allocate the irq resource and attach it to the interrupt handler. 559366f6083SPeter Grehan */ 560366f6083SPeter Grehan for (i = 0; i < numvec; i++) { 561366f6083SPeter Grehan ppt->msi.num_msgs = i + 1; 562366f6083SPeter Grehan ppt->msi.cookie[i] = NULL; 563366f6083SPeter Grehan 564366f6083SPeter Grehan rid = startrid + i; 565366f6083SPeter Grehan ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, 566366f6083SPeter Grehan &rid, flags); 567366f6083SPeter Grehan if (ppt->msi.res[i] == NULL) 568366f6083SPeter Grehan break; 569366f6083SPeter Grehan 570366f6083SPeter Grehan ppt->msi.arg[i].pptdev = ppt; 5714f8be175SNeel Natu ppt->msi.arg[i].addr = addr; 5724f8be175SNeel Natu ppt->msi.arg[i].msg_data = msg + i; 573366f6083SPeter Grehan 574366f6083SPeter Grehan error = bus_setup_intr(ppt->dev, ppt->msi.res[i], 57534a6b2d6SJohn Baldwin INTR_TYPE_NET | INTR_MPSAFE, 576366f6083SPeter Grehan pptintr, NULL, &ppt->msi.arg[i], 577366f6083SPeter Grehan &ppt->msi.cookie[i]); 578366f6083SPeter Grehan if (error != 0) 579366f6083SPeter Grehan break; 580366f6083SPeter Grehan } 581366f6083SPeter Grehan 582366f6083SPeter Grehan if (i < numvec) { 583485b3300SNeel Natu ppt_teardown_msi(ppt); 584366f6083SPeter Grehan return (ENXIO); 585366f6083SPeter Grehan } 586366f6083SPeter Grehan 587366f6083SPeter Grehan return (0); 588366f6083SPeter Grehan } 589cd942e0fSPeter Grehan 590cd942e0fSPeter Grehan int 591cd942e0fSPeter Grehan ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func, 5924f8be175SNeel Natu int idx, uint64_t addr, uint64_t msg, uint32_t vector_control) 593cd942e0fSPeter Grehan { 594cd942e0fSPeter Grehan struct pptdev *ppt; 595cd942e0fSPeter Grehan struct pci_devinfo *dinfo; 596920bc340SNeel Natu int numvec, alloced, rid, error; 597cd942e0fSPeter Grehan size_t res_size, cookie_size, arg_size; 598cd942e0fSPeter Grehan 599*908dca3eSJohn Baldwin error = ppt_find(vm, bus, slot, func, &ppt); 600*908dca3eSJohn Baldwin if (error) 601*908dca3eSJohn Baldwin return (error); 602cd942e0fSPeter Grehan 6031925586eSJohn Baldwin /* Reject attempts to enable MSI-X while MSI is active. */ 6041925586eSJohn Baldwin if (ppt->msi.num_msgs != 0) 6051925586eSJohn Baldwin return (EBUSY); 6061925586eSJohn Baldwin 607cd942e0fSPeter Grehan dinfo = device_get_ivars(ppt->dev); 608cd942e0fSPeter Grehan if (!dinfo) 609cd942e0fSPeter Grehan return (ENXIO); 610cd942e0fSPeter Grehan 611cd942e0fSPeter Grehan /* 612cd942e0fSPeter Grehan * First-time configuration: 613cd942e0fSPeter Grehan * Allocate the MSI-X table 614cd942e0fSPeter Grehan * Allocate the IRQ resources 615cd942e0fSPeter Grehan * Set up some variables in ppt->msix 616cd942e0fSPeter Grehan */ 617920bc340SNeel Natu if (ppt->msix.num_msgs == 0) { 618920bc340SNeel Natu numvec = pci_msix_count(ppt->dev); 619920bc340SNeel Natu if (numvec <= 0) 620920bc340SNeel Natu return (EINVAL); 621cd942e0fSPeter Grehan 622cd942e0fSPeter Grehan ppt->msix.startrid = 1; 623920bc340SNeel Natu ppt->msix.num_msgs = numvec; 624cd942e0fSPeter Grehan 625cd942e0fSPeter Grehan res_size = numvec * sizeof(ppt->msix.res[0]); 626cd942e0fSPeter Grehan cookie_size = numvec * sizeof(ppt->msix.cookie[0]); 627cd942e0fSPeter Grehan arg_size = numvec * sizeof(ppt->msix.arg[0]); 628cd942e0fSPeter Grehan 629920bc340SNeel Natu ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO); 630920bc340SNeel Natu ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX, 631920bc340SNeel Natu M_WAITOK | M_ZERO); 632920bc340SNeel Natu ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO); 633920bc340SNeel Natu 634920bc340SNeel Natu rid = dinfo->cfg.msix.msix_table_bar; 635920bc340SNeel Natu ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev, 636920bc340SNeel Natu SYS_RES_MEMORY, &rid, RF_ACTIVE); 637920bc340SNeel Natu 638920bc340SNeel Natu if (ppt->msix.msix_table_res == NULL) { 639cd942e0fSPeter Grehan ppt_teardown_msix(ppt); 640cd942e0fSPeter Grehan return (ENOSPC); 641cd942e0fSPeter Grehan } 642920bc340SNeel Natu ppt->msix.msix_table_rid = rid; 643920bc340SNeel Natu 6440d1fd6e5SJohn Baldwin if (dinfo->cfg.msix.msix_table_bar != 6450d1fd6e5SJohn Baldwin dinfo->cfg.msix.msix_pba_bar) { 6460d1fd6e5SJohn Baldwin rid = dinfo->cfg.msix.msix_pba_bar; 6470d1fd6e5SJohn Baldwin ppt->msix.msix_pba_res = bus_alloc_resource_any( 6480d1fd6e5SJohn Baldwin ppt->dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 6490d1fd6e5SJohn Baldwin 6500d1fd6e5SJohn Baldwin if (ppt->msix.msix_pba_res == NULL) { 6510d1fd6e5SJohn Baldwin ppt_teardown_msix(ppt); 6520d1fd6e5SJohn Baldwin return (ENOSPC); 6530d1fd6e5SJohn Baldwin } 6540d1fd6e5SJohn Baldwin ppt->msix.msix_pba_rid = rid; 6550d1fd6e5SJohn Baldwin } 6560d1fd6e5SJohn Baldwin 657920bc340SNeel Natu alloced = numvec; 658920bc340SNeel Natu error = pci_alloc_msix(ppt->dev, &alloced); 659920bc340SNeel Natu if (error || alloced != numvec) { 660920bc340SNeel Natu ppt_teardown_msix(ppt); 661920bc340SNeel Natu return (error == 0 ? ENOSPC: error); 662920bc340SNeel Natu } 663cd942e0fSPeter Grehan } 664cd942e0fSPeter Grehan 665cd942e0fSPeter Grehan if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { 666cd942e0fSPeter Grehan /* Tear down the IRQ if it's already set up */ 667cd942e0fSPeter Grehan ppt_teardown_msix_intr(ppt, idx); 668cd942e0fSPeter Grehan 669cd942e0fSPeter Grehan /* Allocate the IRQ resource */ 670cd942e0fSPeter Grehan ppt->msix.cookie[idx] = NULL; 671cd942e0fSPeter Grehan rid = ppt->msix.startrid + idx; 672cd942e0fSPeter Grehan ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ, 673cd942e0fSPeter Grehan &rid, RF_ACTIVE); 674cd942e0fSPeter Grehan if (ppt->msix.res[idx] == NULL) 675cd942e0fSPeter Grehan return (ENXIO); 676cd942e0fSPeter Grehan 677cd942e0fSPeter Grehan ppt->msix.arg[idx].pptdev = ppt; 6784f8be175SNeel Natu ppt->msix.arg[idx].addr = addr; 6794f8be175SNeel Natu ppt->msix.arg[idx].msg_data = msg; 680cd942e0fSPeter Grehan 681cd942e0fSPeter Grehan /* Setup the MSI-X interrupt */ 682cd942e0fSPeter Grehan error = bus_setup_intr(ppt->dev, ppt->msix.res[idx], 683cd942e0fSPeter Grehan INTR_TYPE_NET | INTR_MPSAFE, 684cd942e0fSPeter Grehan pptintr, NULL, &ppt->msix.arg[idx], 685cd942e0fSPeter Grehan &ppt->msix.cookie[idx]); 686cd942e0fSPeter Grehan 687cd942e0fSPeter Grehan if (error != 0) { 688cd942e0fSPeter Grehan bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]); 689cd942e0fSPeter Grehan ppt->msix.cookie[idx] = NULL; 690cd942e0fSPeter Grehan ppt->msix.res[idx] = NULL; 691cd942e0fSPeter Grehan return (ENXIO); 692cd942e0fSPeter Grehan } 693cd942e0fSPeter Grehan } else { 694cd942e0fSPeter Grehan /* Masked, tear it down if it's already been set up */ 695cd942e0fSPeter Grehan ppt_teardown_msix_intr(ppt, idx); 696cd942e0fSPeter Grehan } 697cd942e0fSPeter Grehan 698cd942e0fSPeter Grehan return (0); 699cd942e0fSPeter Grehan } 7001925586eSJohn Baldwin 7011925586eSJohn Baldwin int 7021925586eSJohn Baldwin ppt_disable_msix(struct vm *vm, int bus, int slot, int func) 7031925586eSJohn Baldwin { 7041925586eSJohn Baldwin struct pptdev *ppt; 705*908dca3eSJohn Baldwin int error; 7061925586eSJohn Baldwin 707*908dca3eSJohn Baldwin error = ppt_find(vm, bus, slot, func, &ppt); 708*908dca3eSJohn Baldwin if (error) 709*908dca3eSJohn Baldwin return (error); 7101925586eSJohn Baldwin 7111925586eSJohn Baldwin ppt_teardown_msix(ppt); 7121925586eSJohn Baldwin return (0); 7131925586eSJohn Baldwin } 714