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/types.h>
34 #include <sys/pciio.h>
35 #include <sys/ioctl.h>
37 #include <dev/io/iodev.h>
38 #include <dev/pci/pcireg.h>
40 #include <machine/iodev.h>
49 #include <machine/vmm.h>
55 #define _PATH_DEVPCI "/dev/pci"
59 #define _PATH_DEVIO "/dev/io"
62 #define LEGACY_SUPPORT 1
64 #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
65 #define MSIX_CAPLEN 12
67 static int pcifd = -1;
70 struct passthru_softc {
71 struct pci_devinst *psc_pi;
72 struct pcibar psc_bar[PCI_BARMAX + 1];
81 struct pcisel psc_sel;
85 msi_caplen(int msgctrl)
89 len = 10; /* minimum length of msi capability */
91 if (msgctrl & PCIM_MSICTRL_64BIT)
96 * Ignore the 'mask' and 'pending' bits in the MSI capability.
97 * We'll let the guest manipulate them directly.
99 if (msgctrl & PCIM_MSICTRL_VECTOR)
107 read_config(const struct pcisel *sel, long reg, int width)
111 bzero(&pi, sizeof(pi));
116 if (ioctl(pcifd, PCIOCREAD, &pi) < 0)
117 return (0); /* XXX */
123 write_config(const struct pcisel *sel, long reg, int width, uint32_t data)
127 bzero(&pi, sizeof(pi));
133 (void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */
136 #ifdef LEGACY_SUPPORT
138 passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
141 struct msicap msicap;
144 pci_populate_msicap(&msicap, msgnum, nextptr);
148 * Copy the msi capability structure in the last 16 bytes of the
149 * config space. This is wrong because it could shadow something
150 * useful to the device.
152 capoff = 256 - roundup(sizeof(msicap), 4);
153 capdata = (u_char *)&msicap;
154 for (i = 0; i < sizeof(msicap); i++)
155 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
159 #endif /* LEGACY_SUPPORT */
162 cfginitmsi(struct passthru_softc *sc)
164 int i, ptr, capptr, cap, sts, caplen, table_size;
167 struct pci_devinst *pi;
168 struct msixcap msixcap;
169 uint32_t *msixcap_ptr;
175 * Parse the capabilities and cache the location of the MSI
176 * and MSI-X capabilities.
178 sts = read_config(&sel, PCIR_STATUS, 2);
179 if (sts & PCIM_STATUS_CAPPRESENT) {
180 ptr = read_config(&sel, PCIR_CAP_PTR, 1);
181 while (ptr != 0 && ptr != 0xff) {
182 cap = read_config(&sel, ptr + PCICAP_ID, 1);
183 if (cap == PCIY_MSI) {
185 * Copy the MSI capability into the config
186 * space of the emulated pci device
188 sc->psc_msi.capoff = ptr;
189 sc->psc_msi.msgctrl = read_config(&sel,
191 sc->psc_msi.emulated = 0;
192 caplen = msi_caplen(sc->psc_msi.msgctrl);
195 u32 = read_config(&sel, capptr, 4);
196 pci_set_cfgdata32(pi, capptr, u32);
200 } else if (cap == PCIY_MSIX) {
202 * Copy the MSI-X capability
204 sc->psc_msix.capoff = ptr;
206 msixcap_ptr = (uint32_t*) &msixcap;
209 u32 = read_config(&sel, capptr, 4);
211 pci_set_cfgdata32(pi, capptr, u32);
217 ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1);
221 if (sc->psc_msix.capoff != 0) {
222 pi->pi_msix.pba_bar =
223 msixcap.pba_info & PCIM_MSIX_BIR_MASK;
224 pi->pi_msix.pba_offset =
225 msixcap.pba_info & ~PCIM_MSIX_BIR_MASK;
226 pi->pi_msix.table_bar =
227 msixcap.table_info & PCIM_MSIX_BIR_MASK;
228 pi->pi_msix.table_offset =
229 msixcap.table_info & ~PCIM_MSIX_BIR_MASK;
230 pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
232 /* Allocate the emulated MSI-X table array */
233 table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
234 pi->pi_msix.table = malloc(table_size);
235 bzero(pi->pi_msix.table, table_size);
237 /* Mask all table entries */
238 for (i = 0; i < pi->pi_msix.table_count; i++) {
239 pi->pi_msix.table[i].vector_control |=
240 PCIM_MSIX_VCTRL_MASK;
244 #ifdef LEGACY_SUPPORT
246 * If the passthrough device does not support MSI then craft a
247 * MSI capability for it. We link the new MSI capability at the
248 * head of the list of capabilities.
250 if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
252 origptr = read_config(&sel, PCIR_CAP_PTR, 1);
253 msiptr = passthru_add_msicap(pi, 1, origptr);
254 sc->psc_msi.capoff = msiptr;
255 sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
256 sc->psc_msi.emulated = 1;
257 pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
261 /* Make sure one of the capabilities is present */
262 if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0)
269 msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
271 struct pci_devinst *pi;
272 struct msix_table_entry *entry;
282 offset -= pi->pi_msix.table_offset;
284 index = offset / MSIX_TABLE_ENTRY_SIZE;
285 if (index >= pi->pi_msix.table_count)
288 entry = &pi->pi_msix.table[index];
289 entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
293 src8 = (uint8_t *)((void *)entry + entry_offset);
297 src16 = (uint16_t *)((void *)entry + entry_offset);
301 src32 = (uint32_t *)((void *)entry + entry_offset);
305 src64 = (uint64_t *)((void *)entry + entry_offset);
316 msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc,
317 uint64_t offset, int size, uint64_t data)
319 struct pci_devinst *pi;
320 struct msix_table_entry *entry;
323 uint32_t vector_control;
327 offset -= pi->pi_msix.table_offset;
329 index = offset / MSIX_TABLE_ENTRY_SIZE;
330 if (index >= pi->pi_msix.table_count)
333 entry = &pi->pi_msix.table[index];
334 entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
336 /* Only 4 byte naturally-aligned writes are supported */
338 assert(entry_offset % 4 == 0);
340 vector_control = entry->vector_control;
341 dest = (uint32_t *)((void *)entry + entry_offset);
343 /* If MSI-X hasn't been enabled, do nothing */
344 if (pi->pi_msix.enabled) {
345 /* If the entry is masked, don't set it up */
346 if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
347 (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
348 error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
351 index, entry->msg_data,
352 entry->vector_control,
359 init_msix_table(struct vmctx *ctx, struct passthru_softc *sc, uint64_t base)
363 size_t len, remaining, table_size;
365 struct pci_devinst *pi = sc->psc_pi;
367 assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0);
369 b = sc->psc_sel.pc_bus;
370 s = sc->psc_sel.pc_dev;
371 f = sc->psc_sel.pc_func;
374 * If the MSI-X table BAR maps memory intended for
375 * other uses, it is at least assured that the table
376 * either resides in its own page within the region,
377 * or it resides in a page shared with only the PBA.
379 if (pi->pi_msix.pba_bar == pi->pi_msix.table_bar &&
380 ((pi->pi_msix.pba_offset - pi->pi_msix.table_offset) < 4096)) {
381 /* Need to also emulate the PBA, not supported yet */
382 printf("Unsupported MSI-X configuration: %d/%d/%d\n", b, s, f);
386 /* Compute the MSI-X table size */
387 table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
388 table_size = roundup2(table_size, 4096);
390 idx = pi->pi_msix.table_bar;
391 start = pi->pi_bar[idx].addr;
392 remaining = pi->pi_bar[idx].size;
394 /* Map everything before the MSI-X table */
395 if (pi->pi_msix.table_offset > 0) {
396 len = pi->pi_msix.table_offset;
397 error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base);
406 /* Skip the MSI-X table */
409 remaining -= table_size;
411 /* Map everything beyond the end of the MSI-X table */
414 error = vm_map_pptdev_mmio(ctx, b, s, f, start, len, base);
423 cfginitbar(struct vmctx *ctx, struct passthru_softc *sc)
426 struct pci_devinst *pi;
427 struct pci_bar_io bar;
428 enum pcibar_type bartype;
434 * Initialize BAR registers
436 for (i = 0; i <= PCI_BARMAX; i++) {
437 bzero(&bar, sizeof(bar));
438 bar.pbi_sel = sc->psc_sel;
439 bar.pbi_reg = PCIR_BAR(i);
441 if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0)
444 if (PCI_BAR_IO(bar.pbi_base)) {
446 base = bar.pbi_base & PCIM_BAR_IO_BASE;
448 switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
449 case PCIM_BAR_MEM_64:
450 bartype = PCIBAR_MEM64;
453 bartype = PCIBAR_MEM32;
456 base = bar.pbi_base & PCIM_BAR_MEM_BASE;
459 /* Cache information about the "real" BAR */
460 sc->psc_bar[i].type = bartype;
461 sc->psc_bar[i].size = bar.pbi_length;
462 sc->psc_bar[i].addr = base;
464 /* Allocate the BAR in the guest I/O or MMIO space */
465 error = pci_emul_alloc_pbar(pi, i, base, bartype,
470 /* The MSI-X table needs special handling */
471 if (i == pci_msix_table_bar(pi)) {
472 error = init_msix_table(ctx, sc, base);
475 } else if (bartype != PCIBAR_IO) {
476 /* Map the physical MMIO space in the guest MMIO space */
477 error = vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus,
478 sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
479 pi->pi_bar[i].addr, pi->pi_bar[i].size, base);
485 * 64-bit BAR takes up two slots so skip the next one.
487 if (bartype == PCIBAR_MEM64) {
489 assert(i <= PCI_BARMAX);
490 sc->psc_bar[i].type = PCIBAR_MEMHI64;
497 cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
500 struct passthru_softc *sc;
505 bzero(&sc->psc_sel, sizeof(struct pcisel));
506 sc->psc_sel.pc_bus = bus;
507 sc->psc_sel.pc_dev = slot;
508 sc->psc_sel.pc_func = func;
510 if (cfginitmsi(sc) != 0)
513 if (cfginitbar(ctx, sc) != 0)
516 error = 0; /* success */
522 passthru_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
524 int bus, slot, func, error;
525 struct passthru_softc *sc;
531 pcifd = open(_PATH_DEVPCI, O_RDWR, 0);
537 iofd = open(_PATH_DEVIO, O_RDWR, 0);
543 sscanf(opts, "%d/%d/%d", &bus, &slot, &func) != 3)
546 if (vm_assign_pptdev(ctx, bus, slot, func) != 0)
549 sc = malloc(sizeof(struct passthru_softc));
550 memset(sc, 0, sizeof(struct passthru_softc));
555 /* initialize config space */
556 if ((error = cfginit(ctx, pi, bus, slot, func)) != 0)
559 error = 0; /* success */
563 vm_unassign_pptdev(ctx, bus, slot, func);
571 if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1))
578 msicap_access(struct passthru_softc *sc, int coff)
582 if (sc->psc_msi.capoff == 0)
585 caplen = msi_caplen(sc->psc_msi.msgctrl);
587 if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
594 msixcap_access(struct passthru_softc *sc, int coff)
596 if (sc->psc_msix.capoff == 0)
599 return (coff >= sc->psc_msix.capoff &&
600 coff < sc->psc_msix.capoff + MSIX_CAPLEN);
604 passthru_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
605 int coff, int bytes, uint32_t *rv)
607 struct passthru_softc *sc;
612 * PCI BARs and MSI capability is emulated.
614 if (bar_access(coff) || msicap_access(sc, coff))
617 #ifdef LEGACY_SUPPORT
619 * Emulate PCIR_CAP_PTR if this device does not support MSI capability
622 if (sc->psc_msi.emulated) {
623 if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4)
628 /* Everything else just read from the device's config space */
629 *rv = read_config(&sc->psc_sel, coff, bytes);
635 passthru_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
636 int coff, int bytes, uint32_t val)
638 int error, msix_table_entries, i;
639 struct passthru_softc *sc;
644 * PCI BARs are emulated
646 if (bar_access(coff))
650 * MSI capability is emulated
652 if (msicap_access(sc, coff)) {
653 msicap_cfgwrite(pi, sc->psc_msi.capoff, coff, bytes, val);
655 error = vm_setup_msi(ctx, vcpu, sc->psc_sel.pc_bus,
656 sc->psc_sel.pc_dev, sc->psc_sel.pc_func, pi->pi_msi.cpu,
657 pi->pi_msi.vector, pi->pi_msi.msgnum);
659 printf("vm_setup_msi returned error %d\r\n", errno);
665 if (msixcap_access(sc, coff)) {
666 msixcap_cfgwrite(pi, sc->psc_msix.capoff, coff, bytes, val);
667 if (pi->pi_msix.enabled) {
668 msix_table_entries = pi->pi_msix.table_count;
669 for (i = 0; i < msix_table_entries; i++) {
670 error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
672 sc->psc_sel.pc_func, i,
673 pi->pi_msix.table[i].msg_data,
674 pi->pi_msix.table[i].vector_control,
675 pi->pi_msix.table[i].addr);
678 printf("vm_setup_msix returned error %d\r\n", errno);
686 #ifdef LEGACY_SUPPORT
688 * If this device does not support MSI natively then we cannot let
689 * the guest disable legacy interrupts from the device. It is the
690 * legacy interrupt that is triggering the virtual MSI to the guest.
692 if (sc->psc_msi.emulated && pci_msi_enabled(pi)) {
693 if (coff == PCIR_COMMAND && bytes == 2)
694 val &= ~PCIM_CMD_INTxDIS;
698 write_config(&sc->psc_sel, coff, bytes, val);
704 passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
705 uint64_t offset, int size, uint64_t value)
707 struct passthru_softc *sc;
708 struct iodev_pio_req pio;
712 if (baridx == pci_msix_table_bar(pi)) {
713 msix_table_write(ctx, vcpu, sc, offset, size, value);
715 assert(pi->pi_bar[baridx].type == PCIBAR_IO);
716 bzero(&pio, sizeof(struct iodev_pio_req));
717 pio.access = IODEV_PIO_WRITE;
718 pio.port = sc->psc_bar[baridx].addr + offset;
722 (void)ioctl(iofd, IODEV_PIO, &pio);
727 passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
728 uint64_t offset, int size)
730 struct passthru_softc *sc;
731 struct iodev_pio_req pio;
736 if (baridx == pci_msix_table_bar(pi)) {
737 val = msix_table_read(sc, offset, size);
739 assert(pi->pi_bar[baridx].type == PCIBAR_IO);
740 bzero(&pio, sizeof(struct iodev_pio_req));
741 pio.access = IODEV_PIO_READ;
742 pio.port = sc->psc_bar[baridx].addr + offset;
746 (void)ioctl(iofd, IODEV_PIO, &pio);
754 struct pci_devemu passthru = {
755 .pe_emu = "passthru",
756 .pe_init = passthru_init,
757 .pe_cfgwrite = passthru_cfgwrite,
758 .pe_cfgread = passthru_cfgread,
759 .pe_barwrite = passthru_write,
760 .pe_barread = passthru_read,
762 PCI_EMUL_SET(passthru);