2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/pciio.h>
41 #include <sys/sysctl.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
46 #include <machine/resource.h>
48 #include <machine/vmm.h>
49 #include <machine/vmm_dev.h>
51 #include "vmm_lapic.h"
59 #define MAX_PPTDEVS (sizeof(pptdevs) / sizeof(pptdevs[0]))
60 #define MAX_MSIMSGS 32
63 * If the MSI-X table is located in the middle of a BAR then that MMIO
64 * region gets split into two segments - one segment above the MSI-X table
65 * and the other segment below the MSI-X table - with a hole in place of
66 * the MSI-X table so accesses to it can be trapped and emulated.
68 * So, allocate a MMIO segment for each BAR register + 1 additional segment.
70 #define MAX_MMIOSEGS ((PCIR_MAX_BAR_0 + 1) + 1)
72 MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
74 struct pptintr_arg { /* pptintr(pptintr_arg) */
75 struct pptdev *pptdev;
80 static struct pptdev {
82 struct vm *vm; /* owner of this device */
83 struct vm_memory_segment mmio[MAX_MMIOSEGS];
85 int num_msgs; /* guest state */
87 int startrid; /* host state */
88 struct resource *res[MAX_MSIMSGS];
89 void *cookie[MAX_MSIMSGS];
90 struct pptintr_arg arg[MAX_MSIMSGS];
97 struct resource *msix_table_res;
98 struct resource **res;
100 struct pptintr_arg *arg;
104 SYSCTL_DECL(_hw_vmm);
105 SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW, 0, "bhyve passthru devices");
107 static int num_pptdevs;
108 SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
109 "number of pci passthru devices");
112 ppt_probe(device_t dev)
115 struct pci_devinfo *dinfo;
117 dinfo = (struct pci_devinfo *)device_get_ivars(dev);
119 bus = pci_get_bus(dev);
120 slot = pci_get_slot(dev);
121 func = pci_get_function(dev);
124 * To qualify as a pci passthrough device a device must:
125 * - be allowed by administrator to be used in this role
126 * - be an endpoint device
128 if (vmm_is_pptdev(bus, slot, func) &&
129 (dinfo->cfg.hdrtype & PCIM_HDRTYPE) == PCIM_HDRTYPE_NORMAL)
136 ppt_attach(device_t dev)
140 if (num_pptdevs >= MAX_PPTDEVS) {
141 printf("ppt_attach: maximum number of pci passthrough devices "
147 pptdevs[n].dev = dev;
150 device_printf(dev, "attached\n");
156 ppt_detach(device_t dev)
159 * XXX check whether there are any pci passthrough devices assigned
160 * to guests before we allow this driver to detach.
166 static device_method_t ppt_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, ppt_probe),
169 DEVMETHOD(device_attach, ppt_attach),
170 DEVMETHOD(device_detach, ppt_detach),
174 static devclass_t ppt_devclass;
175 DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, 0);
176 DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL);
178 static struct pptdev *
179 ppt_find(int bus, int slot, int func)
184 for (i = 0; i < num_pptdevs; i++) {
185 dev = pptdevs[i].dev;
186 b = pci_get_bus(dev);
187 s = pci_get_slot(dev);
188 f = pci_get_function(dev);
189 if (bus == b && slot == s && func == f)
190 return (&pptdevs[i]);
196 ppt_unmap_mmio(struct vm *vm, struct pptdev *ppt)
199 struct vm_memory_segment *seg;
201 for (i = 0; i < MAX_MMIOSEGS; i++) {
205 (void)vm_unmap_mmio(vm, seg->gpa, seg->len);
206 bzero(seg, sizeof(struct vm_memory_segment));
211 ppt_teardown_msi(struct pptdev *ppt)
215 struct resource *res;
217 if (ppt->msi.num_msgs == 0)
220 for (i = 0; i < ppt->msi.num_msgs; i++) {
221 rid = ppt->msi.startrid + i;
222 res = ppt->msi.res[i];
223 cookie = ppt->msi.cookie[i];
226 bus_teardown_intr(ppt->dev, res, cookie);
229 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
231 ppt->msi.res[i] = NULL;
232 ppt->msi.cookie[i] = NULL;
235 if (ppt->msi.startrid == 1)
236 pci_release_msi(ppt->dev);
238 ppt->msi.num_msgs = 0;
242 ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
245 struct resource *res;
248 rid = ppt->msix.startrid + idx;
249 res = ppt->msix.res[idx];
250 cookie = ppt->msix.cookie[idx];
253 bus_teardown_intr(ppt->dev, res, cookie);
256 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
258 ppt->msix.res[idx] = NULL;
259 ppt->msix.cookie[idx] = NULL;
263 ppt_teardown_msix(struct pptdev *ppt)
267 if (ppt->msix.num_msgs == 0)
270 for (i = 0; i < ppt->msix.num_msgs; i++)
271 ppt_teardown_msix_intr(ppt, i);
273 if (ppt->msix.msix_table_res) {
274 bus_release_resource(ppt->dev, SYS_RES_MEMORY,
275 ppt->msix.msix_table_rid,
276 ppt->msix.msix_table_res);
277 ppt->msix.msix_table_res = NULL;
278 ppt->msix.msix_table_rid = 0;
281 free(ppt->msix.res, M_PPTMSIX);
282 free(ppt->msix.cookie, M_PPTMSIX);
283 free(ppt->msix.arg, M_PPTMSIX);
285 pci_release_msi(ppt->dev);
287 ppt->msix.num_msgs = 0;
291 ppt_avail_devices(void)
294 return (num_pptdevs);
298 ppt_assigned_devices(struct vm *vm)
303 for (i = 0; i < num_pptdevs; i++) {
304 if (pptdevs[i].vm == vm)
311 ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
315 struct vm_memory_segment *seg;
317 for (n = 0; n < num_pptdevs; n++) {
322 for (i = 0; i < MAX_MMIOSEGS; i++) {
326 if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
335 ppt_assign_device(struct vm *vm, int bus, int slot, int func)
339 ppt = ppt_find(bus, slot, func);
342 * If this device is owned by a different VM then we
343 * cannot change its owner.
345 if (ppt->vm != NULL && ppt->vm != vm)
349 iommu_add_device(vm_iommu_domain(vm), bus, slot, func);
356 ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
360 ppt = ppt_find(bus, slot, func);
363 * If this device is not owned by this 'vm' then bail out.
367 ppt_unmap_mmio(vm, ppt);
368 ppt_teardown_msi(ppt);
369 ppt_teardown_msix(ppt);
370 iommu_remove_device(vm_iommu_domain(vm), bus, slot, func);
378 ppt_unassign_all(struct vm *vm)
380 int i, bus, slot, func;
383 for (i = 0; i < num_pptdevs; i++) {
384 if (pptdevs[i].vm == vm) {
385 dev = pptdevs[i].dev;
386 bus = pci_get_bus(dev);
387 slot = pci_get_slot(dev);
388 func = pci_get_function(dev);
389 vm_unassign_pptdev(vm, bus, slot, func);
397 ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
398 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
401 struct vm_memory_segment *seg;
404 ppt = ppt_find(bus, slot, func);
409 for (i = 0; i < MAX_MMIOSEGS; i++) {
412 error = vm_map_mmio(vm, gpa, len, hpa);
429 struct pptintr_arg *pptarg;
432 ppt = pptarg->pptdev;
435 lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
439 * This is not expected to happen - panic?
444 * For legacy interrupts give other filters a chance in case
445 * the interrupt was not generated by the passthrough device.
447 if (ppt->msi.startrid == 0)
448 return (FILTER_STRAY);
450 return (FILTER_HANDLED);
454 ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func,
455 uint64_t addr, uint64_t msg, int numvec)
458 int msi_count, startrid, error, tmp;
461 if (numvec < 0 || numvec > MAX_MSIMSGS)
464 ppt = ppt_find(bus, slot, func);
467 if (ppt->vm != vm) /* Make sure we own this device */
470 /* Free any allocated resources */
471 ppt_teardown_msi(ppt);
473 if (numvec == 0) /* nothing more to do */
477 msi_count = pci_msi_count(ppt->dev);
478 if (msi_count == 0) {
479 startrid = 0; /* legacy interrupt */
481 flags |= RF_SHAREABLE;
483 startrid = 1; /* MSI */
486 * The device must be capable of supporting the number of vectors
487 * the guest wants to allocate.
489 if (numvec > msi_count)
493 * Make sure that we can allocate all the MSI vectors that are needed
498 error = pci_alloc_msi(ppt->dev, &tmp);
501 else if (tmp != numvec) {
502 pci_release_msi(ppt->dev);
509 ppt->msi.startrid = startrid;
512 * Allocate the irq resource and attach it to the interrupt handler.
514 for (i = 0; i < numvec; i++) {
515 ppt->msi.num_msgs = i + 1;
516 ppt->msi.cookie[i] = NULL;
519 ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
521 if (ppt->msi.res[i] == NULL)
524 ppt->msi.arg[i].pptdev = ppt;
525 ppt->msi.arg[i].addr = addr;
526 ppt->msi.arg[i].msg_data = msg + i;
528 error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
529 INTR_TYPE_NET | INTR_MPSAFE,
530 pptintr, NULL, &ppt->msi.arg[i],
531 &ppt->msi.cookie[i]);
537 ppt_teardown_msi(ppt);
545 ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func,
546 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
549 struct pci_devinfo *dinfo;
550 int numvec, alloced, rid, error;
551 size_t res_size, cookie_size, arg_size;
553 ppt = ppt_find(bus, slot, func);
556 if (ppt->vm != vm) /* Make sure we own this device */
559 dinfo = device_get_ivars(ppt->dev);
564 * First-time configuration:
565 * Allocate the MSI-X table
566 * Allocate the IRQ resources
567 * Set up some variables in ppt->msix
569 if (ppt->msix.num_msgs == 0) {
570 numvec = pci_msix_count(ppt->dev);
574 ppt->msix.startrid = 1;
575 ppt->msix.num_msgs = numvec;
577 res_size = numvec * sizeof(ppt->msix.res[0]);
578 cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
579 arg_size = numvec * sizeof(ppt->msix.arg[0]);
581 ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
582 ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
584 ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
586 rid = dinfo->cfg.msix.msix_table_bar;
587 ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
588 SYS_RES_MEMORY, &rid, RF_ACTIVE);
590 if (ppt->msix.msix_table_res == NULL) {
591 ppt_teardown_msix(ppt);
594 ppt->msix.msix_table_rid = rid;
597 error = pci_alloc_msix(ppt->dev, &alloced);
598 if (error || alloced != numvec) {
599 ppt_teardown_msix(ppt);
600 return (error == 0 ? ENOSPC: error);
604 if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
605 /* Tear down the IRQ if it's already set up */
606 ppt_teardown_msix_intr(ppt, idx);
608 /* Allocate the IRQ resource */
609 ppt->msix.cookie[idx] = NULL;
610 rid = ppt->msix.startrid + idx;
611 ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
613 if (ppt->msix.res[idx] == NULL)
616 ppt->msix.arg[idx].pptdev = ppt;
617 ppt->msix.arg[idx].addr = addr;
618 ppt->msix.arg[idx].msg_data = msg;
620 /* Setup the MSI-X interrupt */
621 error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
622 INTR_TYPE_NET | INTR_MPSAFE,
623 pptintr, NULL, &ppt->msix.arg[idx],
624 &ppt->msix.cookie[idx]);
627 bus_teardown_intr(ppt->dev, ppt->msix.res[idx], ppt->msix.cookie[idx]);
628 bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
629 ppt->msix.cookie[idx] = NULL;
630 ppt->msix.res[idx] = NULL;
634 /* Masked, tear it down if it's already been set up */
635 ppt_teardown_msix_intr(ppt, idx);