1366f6083SPeter Grehan /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 31de7b4b8SPedro 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> 3500ef17beSBartek Rutkowski #ifndef WITHOUT_CAPSICUM 3600ef17beSBartek Rutkowski #include <sys/capsicum.h> 3700ef17beSBartek Rutkowski #endif 38366f6083SPeter Grehan #include <sys/types.h> 395c40acf8SJohn Baldwin #include <sys/mman.h> 40366f6083SPeter Grehan #include <sys/pciio.h> 41366f6083SPeter Grehan #include <sys/ioctl.h> 42e47fe318SCorvin Köhne #include <sys/stat.h> 43366f6083SPeter Grehan 44366f6083SPeter Grehan #include <dev/io/iodev.h> 452e81a7e8SNeel Natu #include <dev/pci/pcireg.h> 462e81a7e8SNeel Natu 477fa23353SMark Johnston #include <vm/vm.h> 487fa23353SMark Johnston 49366f6083SPeter Grehan #include <machine/iodev.h> 507fa23353SMark Johnston #include <machine/vm.h> 51366f6083SPeter Grehan 52abfa3c39SMarcelo Araujo #ifndef WITHOUT_CAPSICUM 53abfa3c39SMarcelo Araujo #include <capsicum_helpers.h> 54abfa3c39SMarcelo Araujo #endif 55baf753ccSJohn Baldwin #include <ctype.h> 56366f6083SPeter Grehan #include <stdio.h> 57366f6083SPeter Grehan #include <stdlib.h> 58366f6083SPeter Grehan #include <string.h> 59cff92ffdSJohn Baldwin #include <err.h> 6000ef17beSBartek Rutkowski #include <errno.h> 61366f6083SPeter Grehan #include <fcntl.h> 6200ef17beSBartek Rutkowski #include <sysexits.h> 63366f6083SPeter Grehan #include <unistd.h> 64366f6083SPeter Grehan 65366f6083SPeter Grehan #include <machine/vmm.h> 66621b5090SJohn Baldwin 67621b5090SJohn Baldwin #include "config.h" 68621b5090SJohn Baldwin #include "debug.h" 694d1e669cSPeter Grehan #include "mem.h" 70563fd224SCorvin Köhne #include "pci_passthru.h" 71366f6083SPeter Grehan 72366f6083SPeter Grehan #ifndef _PATH_DEVPCI 73366f6083SPeter Grehan #define _PATH_DEVPCI "/dev/pci" 74366f6083SPeter Grehan #endif 75366f6083SPeter Grehan 76366f6083SPeter Grehan #define LEGACY_SUPPORT 1 77366f6083SPeter Grehan 782e81a7e8SNeel Natu #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1) 79cd942e0fSPeter Grehan #define MSIX_CAPLEN 12 80cd942e0fSPeter Grehan 81366f6083SPeter Grehan static int pcifd = -1; 82366f6083SPeter Grehan 83366f6083SPeter Grehan struct passthru_softc { 84366f6083SPeter Grehan struct pci_devinst *psc_pi; 85e47fe318SCorvin Köhne /* ROM is handled like a BAR */ 86e47fe318SCorvin Köhne struct pcibar psc_bar[PCI_BARMAX_WITH_ROM + 1]; 87366f6083SPeter Grehan struct { 88366f6083SPeter Grehan int capoff; 89366f6083SPeter Grehan int msgctrl; 90366f6083SPeter Grehan int emulated; 91366f6083SPeter Grehan } psc_msi; 92cd942e0fSPeter Grehan struct { 93cd942e0fSPeter Grehan int capoff; 94cd942e0fSPeter Grehan } psc_msix; 95366f6083SPeter Grehan struct pcisel psc_sel; 96366f6083SPeter Grehan }; 97366f6083SPeter Grehan 98366f6083SPeter Grehan static int 99366f6083SPeter Grehan msi_caplen(int msgctrl) 100366f6083SPeter Grehan { 101366f6083SPeter Grehan int len; 102366f6083SPeter Grehan 103366f6083SPeter Grehan len = 10; /* minimum length of msi capability */ 104366f6083SPeter Grehan 105366f6083SPeter Grehan if (msgctrl & PCIM_MSICTRL_64BIT) 106366f6083SPeter Grehan len += 4; 107366f6083SPeter Grehan 108366f6083SPeter Grehan #if 0 109366f6083SPeter Grehan /* 110366f6083SPeter Grehan * Ignore the 'mask' and 'pending' bits in the MSI capability. 111366f6083SPeter Grehan * We'll let the guest manipulate them directly. 112366f6083SPeter Grehan */ 113366f6083SPeter Grehan if (msgctrl & PCIM_MSICTRL_VECTOR) 114366f6083SPeter Grehan len += 10; 115366f6083SPeter Grehan #endif 116366f6083SPeter Grehan 117366f6083SPeter Grehan return (len); 118366f6083SPeter Grehan } 119366f6083SPeter Grehan 120563fd224SCorvin Köhne static int 12175ce327aSMark Johnston pcifd_init(void) 12275ce327aSMark Johnston { 123563fd224SCorvin Köhne pcifd = open(_PATH_DEVPCI, O_RDWR, 0); 124563fd224SCorvin Köhne if (pcifd < 0) { 125563fd224SCorvin Köhne warn("failed to open %s", _PATH_DEVPCI); 126563fd224SCorvin Köhne return (1); 127563fd224SCorvin Köhne } 128563fd224SCorvin Köhne 129563fd224SCorvin Köhne #ifndef WITHOUT_CAPSICUM 130563fd224SCorvin Köhne cap_rights_t pcifd_rights; 131563fd224SCorvin Köhne cap_rights_init(&pcifd_rights, CAP_IOCTL, CAP_READ, CAP_WRITE); 132563fd224SCorvin Köhne if (caph_rights_limit(pcifd, &pcifd_rights) == -1) 133563fd224SCorvin Köhne errx(EX_OSERR, "Unable to apply rights for sandbox"); 134563fd224SCorvin Köhne 135563fd224SCorvin Köhne const cap_ioctl_t pcifd_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR, 136baf753ccSJohn Baldwin PCIOCBARIO, PCIOCBARMMAP, PCIOCGETCONF }; 137563fd224SCorvin Köhne if (caph_ioctls_limit(pcifd, pcifd_ioctls, nitems(pcifd_ioctls)) == -1) 138563fd224SCorvin Köhne errx(EX_OSERR, "Unable to apply rights for sandbox"); 139563fd224SCorvin Köhne #endif 140563fd224SCorvin Köhne 141563fd224SCorvin Köhne return (0); 142563fd224SCorvin Köhne } 143563fd224SCorvin Köhne 144563fd224SCorvin Köhne uint32_t 145366f6083SPeter Grehan read_config(const struct pcisel *sel, long reg, int width) 146366f6083SPeter Grehan { 147bcab868aSJohn Baldwin struct pci_io pi; 148bcab868aSJohn Baldwin 149563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 150563fd224SCorvin Köhne return (0); 151563fd224SCorvin Köhne } 152563fd224SCorvin Köhne 153366f6083SPeter Grehan bzero(&pi, sizeof(pi)); 154366f6083SPeter Grehan pi.pi_sel = *sel; 155366f6083SPeter Grehan pi.pi_reg = reg; 156366f6083SPeter Grehan pi.pi_width = width; 157366f6083SPeter Grehan 158366f6083SPeter Grehan if (ioctl(pcifd, PCIOCREAD, &pi) < 0) 159366f6083SPeter Grehan return (0); /* XXX */ 160366f6083SPeter Grehan else 161366f6083SPeter Grehan return (pi.pi_data); 162366f6083SPeter Grehan } 163366f6083SPeter Grehan 164563fd224SCorvin Köhne void 165366f6083SPeter Grehan write_config(const struct pcisel *sel, long reg, int width, uint32_t data) 166366f6083SPeter Grehan { 167bcab868aSJohn Baldwin struct pci_io pi; 168bcab868aSJohn Baldwin 169563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 170563fd224SCorvin Köhne return; 171563fd224SCorvin Köhne } 172563fd224SCorvin Köhne 173366f6083SPeter Grehan bzero(&pi, sizeof(pi)); 174366f6083SPeter Grehan pi.pi_sel = *sel; 175366f6083SPeter Grehan pi.pi_reg = reg; 176366f6083SPeter Grehan pi.pi_width = width; 177366f6083SPeter Grehan pi.pi_data = data; 178366f6083SPeter Grehan 179366f6083SPeter Grehan (void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */ 180366f6083SPeter Grehan } 181366f6083SPeter Grehan 182366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 183366f6083SPeter Grehan static int 184366f6083SPeter Grehan passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr) 185366f6083SPeter Grehan { 186ed721684SMark Johnston int capoff; 187366f6083SPeter Grehan struct msicap msicap; 188366f6083SPeter Grehan u_char *capdata; 189366f6083SPeter Grehan 190366f6083SPeter Grehan pci_populate_msicap(&msicap, msgnum, nextptr); 191366f6083SPeter Grehan 192366f6083SPeter Grehan /* 193366f6083SPeter Grehan * XXX 194366f6083SPeter Grehan * Copy the msi capability structure in the last 16 bytes of the 195366f6083SPeter Grehan * config space. This is wrong because it could shadow something 196366f6083SPeter Grehan * useful to the device. 197366f6083SPeter Grehan */ 198366f6083SPeter Grehan capoff = 256 - roundup(sizeof(msicap), 4); 199366f6083SPeter Grehan capdata = (u_char *)&msicap; 200ed721684SMark Johnston for (size_t i = 0; i < sizeof(msicap); i++) 201366f6083SPeter Grehan pci_set_cfgdata8(pi, capoff + i, capdata[i]); 202366f6083SPeter Grehan 203366f6083SPeter Grehan return (capoff); 204366f6083SPeter Grehan } 205366f6083SPeter Grehan #endif /* LEGACY_SUPPORT */ 206366f6083SPeter Grehan 207366f6083SPeter Grehan static int 208366f6083SPeter Grehan cfginitmsi(struct passthru_softc *sc) 209366f6083SPeter Grehan { 2102e81a7e8SNeel Natu int i, ptr, capptr, cap, sts, caplen, table_size; 211366f6083SPeter Grehan uint32_t u32; 212366f6083SPeter Grehan struct pcisel sel; 213366f6083SPeter Grehan struct pci_devinst *pi; 214cd942e0fSPeter Grehan struct msixcap msixcap; 21532b21dd2SJohn Baldwin char *msixcap_ptr; 216366f6083SPeter Grehan 217366f6083SPeter Grehan pi = sc->psc_pi; 218366f6083SPeter Grehan sel = sc->psc_sel; 219366f6083SPeter Grehan 220366f6083SPeter Grehan /* 221366f6083SPeter Grehan * Parse the capabilities and cache the location of the MSI 222cd942e0fSPeter Grehan * and MSI-X capabilities. 223366f6083SPeter Grehan */ 224366f6083SPeter Grehan sts = read_config(&sel, PCIR_STATUS, 2); 225366f6083SPeter Grehan if (sts & PCIM_STATUS_CAPPRESENT) { 226366f6083SPeter Grehan ptr = read_config(&sel, PCIR_CAP_PTR, 1); 227366f6083SPeter Grehan while (ptr != 0 && ptr != 0xff) { 228366f6083SPeter Grehan cap = read_config(&sel, ptr + PCICAP_ID, 1); 229366f6083SPeter Grehan if (cap == PCIY_MSI) { 230366f6083SPeter Grehan /* 231366f6083SPeter Grehan * Copy the MSI capability into the config 232366f6083SPeter Grehan * space of the emulated pci device 233366f6083SPeter Grehan */ 234366f6083SPeter Grehan sc->psc_msi.capoff = ptr; 235366f6083SPeter Grehan sc->psc_msi.msgctrl = read_config(&sel, 236366f6083SPeter Grehan ptr + 2, 2); 237366f6083SPeter Grehan sc->psc_msi.emulated = 0; 238366f6083SPeter Grehan caplen = msi_caplen(sc->psc_msi.msgctrl); 239cd942e0fSPeter Grehan capptr = ptr; 240366f6083SPeter Grehan while (caplen > 0) { 241cd942e0fSPeter Grehan u32 = read_config(&sel, capptr, 4); 242cd942e0fSPeter Grehan pci_set_cfgdata32(pi, capptr, u32); 243366f6083SPeter Grehan caplen -= 4; 244cd942e0fSPeter Grehan capptr += 4; 245366f6083SPeter Grehan } 246cd942e0fSPeter Grehan } else if (cap == PCIY_MSIX) { 247cd942e0fSPeter Grehan /* 248cd942e0fSPeter Grehan * Copy the MSI-X capability 249cd942e0fSPeter Grehan */ 250cd942e0fSPeter Grehan sc->psc_msix.capoff = ptr; 251cd942e0fSPeter Grehan caplen = 12; 25232b21dd2SJohn Baldwin msixcap_ptr = (char *)&msixcap; 253cd942e0fSPeter Grehan capptr = ptr; 254cd942e0fSPeter Grehan while (caplen > 0) { 255cd942e0fSPeter Grehan u32 = read_config(&sel, capptr, 4); 25632b21dd2SJohn Baldwin memcpy(msixcap_ptr, &u32, 4); 257cd942e0fSPeter Grehan pci_set_cfgdata32(pi, capptr, u32); 258cd942e0fSPeter Grehan caplen -= 4; 259cd942e0fSPeter Grehan capptr += 4; 26032b21dd2SJohn Baldwin msixcap_ptr += 4; 261cd942e0fSPeter Grehan } 262366f6083SPeter Grehan } 263366f6083SPeter Grehan ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1); 264366f6083SPeter Grehan } 265366f6083SPeter Grehan } 266366f6083SPeter Grehan 2674d1e669cSPeter Grehan if (sc->psc_msix.capoff != 0) { 2684d1e669cSPeter Grehan pi->pi_msix.pba_bar = 2692e81a7e8SNeel Natu msixcap.pba_info & PCIM_MSIX_BIR_MASK; 2704d1e669cSPeter Grehan pi->pi_msix.pba_offset = 2712e81a7e8SNeel Natu msixcap.pba_info & ~PCIM_MSIX_BIR_MASK; 2724d1e669cSPeter Grehan pi->pi_msix.table_bar = 2732e81a7e8SNeel Natu msixcap.table_info & PCIM_MSIX_BIR_MASK; 2744d1e669cSPeter Grehan pi->pi_msix.table_offset = 2752e81a7e8SNeel Natu msixcap.table_info & ~PCIM_MSIX_BIR_MASK; 276cd942e0fSPeter Grehan pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl); 2777a902ec0SNeel Natu pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count); 2782e81a7e8SNeel Natu 2792e81a7e8SNeel Natu /* Allocate the emulated MSI-X table array */ 2802e81a7e8SNeel Natu table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 281994f858aSXin LI pi->pi_msix.table = calloc(1, table_size); 2822e81a7e8SNeel Natu 2832e81a7e8SNeel Natu /* Mask all table entries */ 2842e81a7e8SNeel Natu for (i = 0; i < pi->pi_msix.table_count; i++) { 2852e81a7e8SNeel Natu pi->pi_msix.table[i].vector_control |= 2862e81a7e8SNeel Natu PCIM_MSIX_VCTRL_MASK; 2872e81a7e8SNeel Natu } 2884d1e669cSPeter Grehan } 289cd942e0fSPeter Grehan 290366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 291366f6083SPeter Grehan /* 292366f6083SPeter Grehan * If the passthrough device does not support MSI then craft a 293366f6083SPeter Grehan * MSI capability for it. We link the new MSI capability at the 294366f6083SPeter Grehan * head of the list of capabilities. 295366f6083SPeter Grehan */ 296366f6083SPeter Grehan if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) { 297366f6083SPeter Grehan int origptr, msiptr; 298366f6083SPeter Grehan origptr = read_config(&sel, PCIR_CAP_PTR, 1); 299366f6083SPeter Grehan msiptr = passthru_add_msicap(pi, 1, origptr); 300366f6083SPeter Grehan sc->psc_msi.capoff = msiptr; 301366f6083SPeter Grehan sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2); 302366f6083SPeter Grehan sc->psc_msi.emulated = 1; 303366f6083SPeter Grehan pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr); 304366f6083SPeter Grehan } 305366f6083SPeter Grehan #endif 306366f6083SPeter Grehan 307cd942e0fSPeter Grehan /* Make sure one of the capabilities is present */ 308cd942e0fSPeter Grehan if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0) 309366f6083SPeter Grehan return (-1); 310366f6083SPeter Grehan else 311366f6083SPeter Grehan return (0); 312366f6083SPeter Grehan } 313366f6083SPeter Grehan 3144d1e669cSPeter Grehan static uint64_t 3154d1e669cSPeter Grehan msix_table_read(struct passthru_softc *sc, uint64_t offset, int size) 316cd942e0fSPeter Grehan { 317cd942e0fSPeter Grehan struct pci_devinst *pi; 3184d1e669cSPeter Grehan struct msix_table_entry *entry; 319cd942e0fSPeter Grehan uint8_t *src8; 320cd942e0fSPeter Grehan uint16_t *src16; 321cd942e0fSPeter Grehan uint32_t *src32; 322cd942e0fSPeter Grehan uint64_t *src64; 3234d1e669cSPeter Grehan uint64_t data; 3244d1e669cSPeter Grehan size_t entry_offset; 3257fa23353SMark Johnston uint32_t table_offset; 3267fa23353SMark Johnston int index, table_count; 327cd942e0fSPeter Grehan 328cd942e0fSPeter Grehan pi = sc->psc_pi; 3297fa23353SMark Johnston 3307fa23353SMark Johnston table_offset = pi->pi_msix.table_offset; 3317fa23353SMark Johnston table_count = pi->pi_msix.table_count; 3327fa23353SMark Johnston if (offset < table_offset || 3337fa23353SMark Johnston offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) { 3345c40acf8SJohn Baldwin switch (size) { 3355c40acf8SJohn Baldwin case 1: 3367fa23353SMark Johnston src8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset); 3375c40acf8SJohn Baldwin data = *src8; 3385c40acf8SJohn Baldwin break; 3395c40acf8SJohn Baldwin case 2: 3407fa23353SMark Johnston src16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset); 3415c40acf8SJohn Baldwin data = *src16; 3425c40acf8SJohn Baldwin break; 3435c40acf8SJohn Baldwin case 4: 3447fa23353SMark Johnston src32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset); 3455c40acf8SJohn Baldwin data = *src32; 3465c40acf8SJohn Baldwin break; 3475c40acf8SJohn Baldwin case 8: 3487fa23353SMark Johnston src64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset); 3495c40acf8SJohn Baldwin data = *src64; 3505c40acf8SJohn Baldwin break; 3515c40acf8SJohn Baldwin default: 3525c40acf8SJohn Baldwin return (-1); 3535c40acf8SJohn Baldwin } 3545c40acf8SJohn Baldwin return (data); 3555c40acf8SJohn Baldwin } 3565c40acf8SJohn Baldwin 3577fa23353SMark Johnston offset -= table_offset; 358cd942e0fSPeter Grehan index = offset / MSIX_TABLE_ENTRY_SIZE; 3597fa23353SMark Johnston assert(index < table_count); 3602e81a7e8SNeel Natu 361cd942e0fSPeter Grehan entry = &pi->pi_msix.table[index]; 3622e81a7e8SNeel Natu entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 363cd942e0fSPeter Grehan 364cd942e0fSPeter Grehan switch (size) { 365cd942e0fSPeter Grehan case 1: 3667fa23353SMark Johnston src8 = (uint8_t *)((uint8_t *)entry + entry_offset); 3674d1e669cSPeter Grehan data = *src8; 368cd942e0fSPeter Grehan break; 369cd942e0fSPeter Grehan case 2: 3707fa23353SMark Johnston src16 = (uint16_t *)((uint8_t *)entry + entry_offset); 3714d1e669cSPeter Grehan data = *src16; 372cd942e0fSPeter Grehan break; 373cd942e0fSPeter Grehan case 4: 3747fa23353SMark Johnston src32 = (uint32_t *)((uint8_t *)entry + entry_offset); 3754d1e669cSPeter Grehan data = *src32; 376cd942e0fSPeter Grehan break; 377cd942e0fSPeter Grehan case 8: 3787fa23353SMark Johnston src64 = (uint64_t *)((uint8_t *)entry + entry_offset); 3794d1e669cSPeter Grehan data = *src64; 380cd942e0fSPeter Grehan break; 381cd942e0fSPeter Grehan default: 382cd942e0fSPeter Grehan return (-1); 383cd942e0fSPeter Grehan } 384cd942e0fSPeter Grehan 3854d1e669cSPeter Grehan return (data); 386cd942e0fSPeter Grehan } 387cd942e0fSPeter Grehan 3884d1e669cSPeter Grehan static void 389*6a284cacSJohn Baldwin msix_table_write(struct passthru_softc *sc, uint64_t offset, int size, 390*6a284cacSJohn Baldwin uint64_t data) 391cd942e0fSPeter Grehan { 392cd942e0fSPeter Grehan struct pci_devinst *pi; 393cd942e0fSPeter Grehan struct msix_table_entry *entry; 3945c40acf8SJohn Baldwin uint8_t *dest8; 3955c40acf8SJohn Baldwin uint16_t *dest16; 3965c40acf8SJohn Baldwin uint32_t *dest32; 3975c40acf8SJohn Baldwin uint64_t *dest64; 3984d1e669cSPeter Grehan size_t entry_offset; 3997fa23353SMark Johnston uint32_t table_offset, vector_control; 4007fa23353SMark Johnston int index, table_count; 401cd942e0fSPeter Grehan 402cd942e0fSPeter Grehan pi = sc->psc_pi; 4037fa23353SMark Johnston 4047fa23353SMark Johnston table_offset = pi->pi_msix.table_offset; 4057fa23353SMark Johnston table_count = pi->pi_msix.table_count; 4067fa23353SMark Johnston if (offset < table_offset || 4077fa23353SMark Johnston offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) { 4085c40acf8SJohn Baldwin switch (size) { 4095c40acf8SJohn Baldwin case 1: 4107fa23353SMark Johnston dest8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset); 4115c40acf8SJohn Baldwin *dest8 = data; 4125c40acf8SJohn Baldwin break; 4135c40acf8SJohn Baldwin case 2: 4147fa23353SMark Johnston dest16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset); 4155c40acf8SJohn Baldwin *dest16 = data; 4165c40acf8SJohn Baldwin break; 4175c40acf8SJohn Baldwin case 4: 4187fa23353SMark Johnston dest32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset); 4195c40acf8SJohn Baldwin *dest32 = data; 4205c40acf8SJohn Baldwin break; 4215c40acf8SJohn Baldwin case 8: 4227fa23353SMark Johnston dest64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset); 4235c40acf8SJohn Baldwin *dest64 = data; 4245c40acf8SJohn Baldwin break; 4255c40acf8SJohn Baldwin } 4265c40acf8SJohn Baldwin return; 4275c40acf8SJohn Baldwin } 4285c40acf8SJohn Baldwin 4297fa23353SMark Johnston offset -= table_offset; 430cd942e0fSPeter Grehan index = offset / MSIX_TABLE_ENTRY_SIZE; 4317fa23353SMark Johnston assert(index < table_count); 4322e81a7e8SNeel Natu 433cd942e0fSPeter Grehan entry = &pi->pi_msix.table[index]; 4342e81a7e8SNeel Natu entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 435cd942e0fSPeter Grehan 436cd942e0fSPeter Grehan /* Only 4 byte naturally-aligned writes are supported */ 4374d1e669cSPeter Grehan assert(size == 4); 4384d1e669cSPeter Grehan assert(entry_offset % 4 == 0); 4394d1e669cSPeter Grehan 440cd942e0fSPeter Grehan vector_control = entry->vector_control; 44163898728SMark Johnston dest32 = (uint32_t *)((uint8_t *)entry + entry_offset); 4425c40acf8SJohn Baldwin *dest32 = data; 443cd942e0fSPeter Grehan /* If MSI-X hasn't been enabled, do nothing */ 444cd942e0fSPeter Grehan if (pi->pi_msix.enabled) { 445cd942e0fSPeter Grehan /* If the entry is masked, don't set it up */ 446cd942e0fSPeter Grehan if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 || 447cd942e0fSPeter Grehan (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { 448*6a284cacSJohn Baldwin (void)vm_setup_pptdev_msix(sc->psc_pi->pi_vmctx, 0, 44955888cfaSNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 45055888cfaSNeel Natu sc->psc_sel.pc_func, index, entry->addr, 45155888cfaSNeel Natu entry->msg_data, entry->vector_control); 452cd942e0fSPeter Grehan } 453cd942e0fSPeter Grehan } 454cd942e0fSPeter Grehan } 455cd942e0fSPeter Grehan 456cd942e0fSPeter Grehan static int 457*6a284cacSJohn Baldwin init_msix_table(struct passthru_softc *sc) 458cd942e0fSPeter Grehan { 459cd942e0fSPeter Grehan struct pci_devinst *pi = sc->psc_pi; 4607fa23353SMark Johnston struct pci_bar_mmap pbm; 4617fa23353SMark Johnston int b, s, f; 4627fa23353SMark Johnston uint32_t table_size, table_offset; 463cd942e0fSPeter Grehan 464aa12663fSNeel Natu assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0); 465aa12663fSNeel Natu 4662b89a044SNeel Natu b = sc->psc_sel.pc_bus; 4672b89a044SNeel Natu s = sc->psc_sel.pc_dev; 4682b89a044SNeel Natu f = sc->psc_sel.pc_func; 4692b89a044SNeel Natu 470cd942e0fSPeter Grehan /* 4717fa23353SMark Johnston * Map the region of the BAR containing the MSI-X table. This is 4727fa23353SMark Johnston * necessary for two reasons: 4737fa23353SMark Johnston * 1. The PBA may reside in the first or last page containing the MSI-X 4747fa23353SMark Johnston * table. 4757fa23353SMark Johnston * 2. While PCI devices are not supposed to use the page(s) containing 4767fa23353SMark Johnston * the MSI-X table for other purposes, some do in practice. 477cd942e0fSPeter Grehan */ 4787fa23353SMark Johnston memset(&pbm, 0, sizeof(pbm)); 4797fa23353SMark Johnston pbm.pbm_sel = sc->psc_sel; 4807fa23353SMark Johnston pbm.pbm_flags = PCIIO_BAR_MMAP_RW; 48176b45e68SMark Johnston pbm.pbm_reg = PCIR_BAR(pi->pi_msix.table_bar); 4827fa23353SMark Johnston pbm.pbm_memattr = VM_MEMATTR_DEVICE; 4837fa23353SMark Johnston 4847fa23353SMark Johnston if (ioctl(pcifd, PCIOCBARMMAP, &pbm) != 0) { 4857fa23353SMark Johnston warn("Failed to map MSI-X table BAR on %d/%d/%d", b, s, f); 4867fa23353SMark Johnston return (-1); 4877fa23353SMark Johnston } 4887fa23353SMark Johnston assert(pbm.pbm_bar_off == 0); 4897fa23353SMark Johnston pi->pi_msix.mapped_addr = (uint8_t *)(uintptr_t)pbm.pbm_map_base; 4907fa23353SMark Johnston pi->pi_msix.mapped_size = pbm.pbm_map_length; 4917fa23353SMark Johnston 4927a902ec0SNeel Natu table_offset = rounddown2(pi->pi_msix.table_offset, 4096); 4937a902ec0SNeel Natu 4947a902ec0SNeel Natu table_size = pi->pi_msix.table_offset - table_offset; 4957a902ec0SNeel Natu table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 4967a902ec0SNeel Natu table_size = roundup2(table_size, 4096); 4977a902ec0SNeel Natu 4987a902ec0SNeel Natu /* 4994558c11fSMark Johnston * Unmap any pages not containing the table, we do not need to emulate 5007fa23353SMark Johnston * accesses to them. Avoid releasing address space to help ensure that 5017fa23353SMark Johnston * a buggy out-of-bounds access causes a crash. 5027a902ec0SNeel Natu */ 5037fa23353SMark Johnston if (table_offset != 0) 5047fa23353SMark Johnston if (mprotect(pi->pi_msix.mapped_addr, table_offset, 5057fa23353SMark Johnston PROT_NONE) != 0) 5067fa23353SMark Johnston warn("Failed to unmap MSI-X table BAR region"); 5077fa23353SMark Johnston if (table_offset + table_size != pi->pi_msix.mapped_size) 5084558c11fSMark Johnston if (mprotect( 5094558c11fSMark Johnston pi->pi_msix.mapped_addr + table_offset + table_size, 5107fa23353SMark Johnston pi->pi_msix.mapped_size - (table_offset + table_size), 5117fa23353SMark Johnston PROT_NONE) != 0) 5127fa23353SMark Johnston warn("Failed to unmap MSI-X table BAR region"); 5132b89a044SNeel Natu 5142b89a044SNeel Natu return (0); 515cd942e0fSPeter Grehan } 516cd942e0fSPeter Grehan 517cd942e0fSPeter Grehan static int 518*6a284cacSJohn Baldwin cfginitbar(struct passthru_softc *sc) 519366f6083SPeter Grehan { 520366f6083SPeter Grehan int i, error; 521366f6083SPeter Grehan struct pci_devinst *pi; 522366f6083SPeter Grehan struct pci_bar_io bar; 523366f6083SPeter Grehan enum pcibar_type bartype; 5247a902ec0SNeel Natu uint64_t base, size; 525366f6083SPeter Grehan 526366f6083SPeter Grehan pi = sc->psc_pi; 527366f6083SPeter Grehan 528366f6083SPeter Grehan /* 529366f6083SPeter Grehan * Initialize BAR registers 530366f6083SPeter Grehan */ 531366f6083SPeter Grehan for (i = 0; i <= PCI_BARMAX; i++) { 532366f6083SPeter Grehan bzero(&bar, sizeof(bar)); 533366f6083SPeter Grehan bar.pbi_sel = sc->psc_sel; 534366f6083SPeter Grehan bar.pbi_reg = PCIR_BAR(i); 535366f6083SPeter Grehan 536366f6083SPeter Grehan if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0) 537366f6083SPeter Grehan continue; 538366f6083SPeter Grehan 539366f6083SPeter Grehan if (PCI_BAR_IO(bar.pbi_base)) { 540366f6083SPeter Grehan bartype = PCIBAR_IO; 541366f6083SPeter Grehan base = bar.pbi_base & PCIM_BAR_IO_BASE; 542366f6083SPeter Grehan } else { 543366f6083SPeter Grehan switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) { 544366f6083SPeter Grehan case PCIM_BAR_MEM_64: 545366f6083SPeter Grehan bartype = PCIBAR_MEM64; 546366f6083SPeter Grehan break; 547366f6083SPeter Grehan default: 548366f6083SPeter Grehan bartype = PCIBAR_MEM32; 549366f6083SPeter Grehan break; 550366f6083SPeter Grehan } 551366f6083SPeter Grehan base = bar.pbi_base & PCIM_BAR_MEM_BASE; 552366f6083SPeter Grehan } 5537a902ec0SNeel Natu size = bar.pbi_length; 5547a902ec0SNeel Natu 5557a902ec0SNeel Natu if (bartype != PCIBAR_IO) { 5567a902ec0SNeel Natu if (((base | size) & PAGE_MASK) != 0) { 557cff92ffdSJohn Baldwin warnx("passthru device %d/%d/%d BAR %d: " 5587a902ec0SNeel Natu "base %#lx or size %#lx not page aligned\n", 5597a902ec0SNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 5607a902ec0SNeel Natu sc->psc_sel.pc_func, i, base, size); 5617a902ec0SNeel Natu return (-1); 5627a902ec0SNeel Natu } 5637a902ec0SNeel Natu } 564366f6083SPeter Grehan 565366f6083SPeter Grehan /* Cache information about the "real" BAR */ 566366f6083SPeter Grehan sc->psc_bar[i].type = bartype; 5677a902ec0SNeel Natu sc->psc_bar[i].size = size; 568366f6083SPeter Grehan sc->psc_bar[i].addr = base; 569e87a6f3eSCorvin Köhne sc->psc_bar[i].lobits = 0; 570366f6083SPeter Grehan 571366f6083SPeter Grehan /* Allocate the BAR in the guest I/O or MMIO space */ 572038f5c7bSKonstantin Belousov error = pci_emul_alloc_bar(pi, i, bartype, size); 573366f6083SPeter Grehan if (error) 574366f6083SPeter Grehan return (-1); 575366f6083SPeter Grehan 576e87a6f3eSCorvin Köhne /* Use same lobits as physical bar */ 577e87a6f3eSCorvin Köhne uint8_t lobits = read_config(&sc->psc_sel, PCIR_BAR(i), 0x01); 578e87a6f3eSCorvin Köhne if (bartype == PCIBAR_MEM32 || bartype == PCIBAR_MEM64) { 579e87a6f3eSCorvin Köhne lobits &= ~PCIM_BAR_MEM_BASE; 580e87a6f3eSCorvin Köhne } else { 581e87a6f3eSCorvin Köhne lobits &= ~PCIM_BAR_IO_BASE; 582e87a6f3eSCorvin Köhne } 583e87a6f3eSCorvin Köhne sc->psc_bar[i].lobits = lobits; 584e87a6f3eSCorvin Köhne pi->pi_bar[i].lobits = lobits; 585e87a6f3eSCorvin Köhne 586366f6083SPeter Grehan /* 587366f6083SPeter Grehan * 64-bit BAR takes up two slots so skip the next one. 588366f6083SPeter Grehan */ 589366f6083SPeter Grehan if (bartype == PCIBAR_MEM64) { 590366f6083SPeter Grehan i++; 591366f6083SPeter Grehan assert(i <= PCI_BARMAX); 592366f6083SPeter Grehan sc->psc_bar[i].type = PCIBAR_MEMHI64; 593366f6083SPeter Grehan } 594366f6083SPeter Grehan } 595366f6083SPeter Grehan return (0); 596366f6083SPeter Grehan } 597366f6083SPeter Grehan 598366f6083SPeter Grehan static int 599*6a284cacSJohn Baldwin cfginit(struct pci_devinst *pi, int bus, int slot, int func) 600366f6083SPeter Grehan { 601366f6083SPeter Grehan int error; 602366f6083SPeter Grehan struct passthru_softc *sc; 603366f6083SPeter Grehan 604366f6083SPeter Grehan error = 1; 605366f6083SPeter Grehan sc = pi->pi_arg; 606366f6083SPeter Grehan 607366f6083SPeter Grehan bzero(&sc->psc_sel, sizeof(struct pcisel)); 608366f6083SPeter Grehan sc->psc_sel.pc_bus = bus; 609366f6083SPeter Grehan sc->psc_sel.pc_dev = slot; 610366f6083SPeter Grehan sc->psc_sel.pc_func = func; 611366f6083SPeter Grehan 612cff92ffdSJohn Baldwin if (cfginitmsi(sc) != 0) { 613cff92ffdSJohn Baldwin warnx("failed to initialize MSI for PCI %d/%d/%d", 614cff92ffdSJohn Baldwin bus, slot, func); 615cd942e0fSPeter Grehan goto done; 616cff92ffdSJohn Baldwin } 617cd942e0fSPeter Grehan 618*6a284cacSJohn Baldwin if (cfginitbar(sc) != 0) { 619cff92ffdSJohn Baldwin warnx("failed to initialize BARs for PCI %d/%d/%d", 620cff92ffdSJohn Baldwin bus, slot, func); 621366f6083SPeter Grehan goto done; 622cff92ffdSJohn Baldwin } 623366f6083SPeter Grehan 6242eb20795SCorvin Köhne write_config(&sc->psc_sel, PCIR_COMMAND, 2, 6252eb20795SCorvin Köhne pci_get_cfgdata16(pi, PCIR_COMMAND)); 62656282675SJohn Baldwin 627f1442847SBjoern A. Zeeb /* 628f1442847SBjoern A. Zeeb * We need to do this after PCIR_COMMAND got possibly updated, e.g., 629f1442847SBjoern A. Zeeb * a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us. 630f1442847SBjoern A. Zeeb */ 631338a1be8SCorvin Köhne if (pci_msix_table_bar(pi) >= 0) { 632*6a284cacSJohn Baldwin error = init_msix_table(sc); 633f1442847SBjoern A. Zeeb if (error != 0) { 634338a1be8SCorvin Köhne warnx( 635338a1be8SCorvin Köhne "failed to initialize MSI-X table for PCI %d/%d/%d: %d", 636f1442847SBjoern A. Zeeb bus, slot, func, error); 637f1442847SBjoern A. Zeeb goto done; 638f1442847SBjoern A. Zeeb } 639338a1be8SCorvin Köhne } 640f1442847SBjoern A. Zeeb 641338a1be8SCorvin Köhne error = 0; /* success */ 642366f6083SPeter Grehan done: 643366f6083SPeter Grehan return (error); 644366f6083SPeter Grehan } 645366f6083SPeter Grehan 646366f6083SPeter Grehan static int 647621b5090SJohn Baldwin passthru_legacy_config(nvlist_t *nvl, const char *opts) 648621b5090SJohn Baldwin { 649baf753ccSJohn Baldwin const char *cp; 650baf753ccSJohn Baldwin char *tofree; 651621b5090SJohn Baldwin char value[16]; 652621b5090SJohn Baldwin int bus, slot, func; 653621b5090SJohn Baldwin 654621b5090SJohn Baldwin if (opts == NULL) 655621b5090SJohn Baldwin return (0); 656621b5090SJohn Baldwin 657baf753ccSJohn Baldwin cp = strchr(opts, ','); 658621b5090SJohn Baldwin 659baf753ccSJohn Baldwin if (strncmp(opts, "ppt", strlen("ppt")) == 0) { 660baf753ccSJohn Baldwin tofree = strndup(opts, cp - opts); 661baf753ccSJohn Baldwin set_config_value_node(nvl, "pptdev", tofree); 662baf753ccSJohn Baldwin free(tofree); 663baf753ccSJohn Baldwin } else if (sscanf(opts, "pci0:%d:%d:%d", &bus, &slot, &func) == 3 || 664baf753ccSJohn Baldwin sscanf(opts, "pci%d:%d:%d", &bus, &slot, &func) == 3 || 665baf753ccSJohn Baldwin sscanf(opts, "%d/%d/%d", &bus, &slot, &func) == 3) { 666621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", bus); 667621b5090SJohn Baldwin set_config_value_node(nvl, "bus", value); 668621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", slot); 669621b5090SJohn Baldwin set_config_value_node(nvl, "slot", value); 670621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", func); 671621b5090SJohn Baldwin set_config_value_node(nvl, "func", value); 672baf753ccSJohn Baldwin } else { 673baf753ccSJohn Baldwin EPRINTLN("passthru: invalid options \"%s\"", opts); 674baf753ccSJohn Baldwin return (-1); 675baf753ccSJohn Baldwin } 676e47fe318SCorvin Köhne 677baf753ccSJohn Baldwin if (cp == NULL) { 6783256b7caSCorvin Köhne return (0); 6793256b7caSCorvin Köhne } 6803256b7caSCorvin Köhne 681baf753ccSJohn Baldwin return (pci_parse_legacy_config(nvl, cp + 1)); 682e47fe318SCorvin Köhne } 683e47fe318SCorvin Köhne 684e47fe318SCorvin Köhne static int 685*6a284cacSJohn Baldwin passthru_init_rom(struct passthru_softc *const sc, const char *const romfile) 686e47fe318SCorvin Köhne { 687e47fe318SCorvin Köhne if (romfile == NULL) { 688e47fe318SCorvin Köhne return (0); 689e47fe318SCorvin Köhne } 690e47fe318SCorvin Köhne 691e47fe318SCorvin Köhne const int fd = open(romfile, O_RDONLY); 692e47fe318SCorvin Köhne if (fd < 0) { 693e47fe318SCorvin Köhne warnx("%s: can't open romfile \"%s\"", __func__, romfile); 694e47fe318SCorvin Köhne return (-1); 695e47fe318SCorvin Köhne } 696e47fe318SCorvin Köhne 697e47fe318SCorvin Köhne struct stat sbuf; 698e47fe318SCorvin Köhne if (fstat(fd, &sbuf) < 0) { 699e47fe318SCorvin Köhne warnx("%s: can't fstat romfile \"%s\"", __func__, romfile); 700e47fe318SCorvin Köhne close(fd); 701e47fe318SCorvin Köhne return (-1); 702e47fe318SCorvin Köhne } 703e47fe318SCorvin Köhne const uint64_t rom_size = sbuf.st_size; 704e47fe318SCorvin Köhne 705e47fe318SCorvin Köhne void *const rom_data = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, fd, 706e47fe318SCorvin Köhne 0); 707e47fe318SCorvin Köhne if (rom_data == MAP_FAILED) { 708e47fe318SCorvin Köhne warnx("%s: unable to mmap romfile \"%s\" (%d)", __func__, 709e47fe318SCorvin Köhne romfile, errno); 710e47fe318SCorvin Köhne close(fd); 711e47fe318SCorvin Köhne return (-1); 712e47fe318SCorvin Köhne } 713e47fe318SCorvin Köhne 714e47fe318SCorvin Köhne void *rom_addr; 715e47fe318SCorvin Köhne int error = pci_emul_alloc_rom(sc->psc_pi, rom_size, &rom_addr); 716e47fe318SCorvin Köhne if (error) { 717e47fe318SCorvin Köhne warnx("%s: failed to alloc rom segment", __func__); 718e47fe318SCorvin Köhne munmap(rom_data, rom_size); 719e47fe318SCorvin Köhne close(fd); 720e47fe318SCorvin Köhne return (error); 721e47fe318SCorvin Köhne } 722e47fe318SCorvin Köhne memcpy(rom_addr, rom_data, rom_size); 723e47fe318SCorvin Köhne 724e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].type = PCIBAR_ROM; 725e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].addr = (uint64_t)rom_addr; 726e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].size = rom_size; 727e47fe318SCorvin Köhne 728e47fe318SCorvin Köhne munmap(rom_data, rom_size); 729e47fe318SCorvin Köhne close(fd); 730e47fe318SCorvin Köhne 731621b5090SJohn Baldwin return (0); 732621b5090SJohn Baldwin } 733621b5090SJohn Baldwin 734baf753ccSJohn Baldwin static bool 735baf753ccSJohn Baldwin passthru_lookup_pptdev(const char *name, int *bus, int *slot, int *func) 736baf753ccSJohn Baldwin { 737baf753ccSJohn Baldwin struct pci_conf_io pc; 738baf753ccSJohn Baldwin struct pci_conf conf[1]; 739baf753ccSJohn Baldwin struct pci_match_conf patterns[1]; 740baf753ccSJohn Baldwin char *cp; 741baf753ccSJohn Baldwin 742baf753ccSJohn Baldwin bzero(&pc, sizeof(struct pci_conf_io)); 743baf753ccSJohn Baldwin pc.match_buf_len = sizeof(conf); 744baf753ccSJohn Baldwin pc.matches = conf; 745baf753ccSJohn Baldwin 746baf753ccSJohn Baldwin bzero(&patterns, sizeof(patterns)); 747baf753ccSJohn Baldwin 748baf753ccSJohn Baldwin /* 749baf753ccSJohn Baldwin * The pattern structure requires the unit to be split out from 750baf753ccSJohn Baldwin * the driver name. Walk backwards from the end of the name to 751baf753ccSJohn Baldwin * find the start of the unit. 752baf753ccSJohn Baldwin */ 753baf753ccSJohn Baldwin cp = strchr(name, '\0'); 754baf753ccSJohn Baldwin assert(cp != NULL); 755baf753ccSJohn Baldwin while (cp != name && isdigit(cp[-1])) 756baf753ccSJohn Baldwin cp--; 757baf753ccSJohn Baldwin if (cp == name || !isdigit(*cp)) { 758baf753ccSJohn Baldwin EPRINTLN("Invalid passthru device name %s", name); 759baf753ccSJohn Baldwin return (false); 760baf753ccSJohn Baldwin } 761baf753ccSJohn Baldwin if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name)) { 762baf753ccSJohn Baldwin EPRINTLN("Passthru device name %s is too long", name); 763baf753ccSJohn Baldwin return (false); 764baf753ccSJohn Baldwin } 765baf753ccSJohn Baldwin memcpy(patterns[0].pd_name, name, cp - name); 766baf753ccSJohn Baldwin patterns[0].pd_unit = strtol(cp, &cp, 10); 767baf753ccSJohn Baldwin if (*cp != '\0') { 768baf753ccSJohn Baldwin EPRINTLN("Invalid passthru device name %s", name); 769baf753ccSJohn Baldwin return (false); 770baf753ccSJohn Baldwin } 771baf753ccSJohn Baldwin patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT; 772baf753ccSJohn Baldwin pc.num_patterns = 1; 773baf753ccSJohn Baldwin pc.pat_buf_len = sizeof(patterns); 774baf753ccSJohn Baldwin pc.patterns = patterns; 775baf753ccSJohn Baldwin 776baf753ccSJohn Baldwin if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1) { 777baf753ccSJohn Baldwin EPRINTLN("ioctl(PCIOCGETCONF): %s", strerror(errno)); 778baf753ccSJohn Baldwin return (false); 779baf753ccSJohn Baldwin } 780baf753ccSJohn Baldwin if (pc.status != PCI_GETCONF_LAST_DEVICE && 781baf753ccSJohn Baldwin pc.status != PCI_GETCONF_MORE_DEVS) { 782baf753ccSJohn Baldwin EPRINTLN("error returned from PCIOCGETCONF ioctl"); 783baf753ccSJohn Baldwin return (false); 784baf753ccSJohn Baldwin } 785baf753ccSJohn Baldwin if (pc.num_matches == 0) { 786baf753ccSJohn Baldwin EPRINTLN("Passthru device %s not found", name); 787baf753ccSJohn Baldwin return (false); 788baf753ccSJohn Baldwin } 789baf753ccSJohn Baldwin 790baf753ccSJohn Baldwin if (conf[0].pc_sel.pc_domain != 0) { 791baf753ccSJohn Baldwin EPRINTLN("Passthru device %s on unsupported domain", name); 792baf753ccSJohn Baldwin return (false); 793baf753ccSJohn Baldwin } 794baf753ccSJohn Baldwin *bus = conf[0].pc_sel.pc_bus; 795baf753ccSJohn Baldwin *slot = conf[0].pc_sel.pc_dev; 796baf753ccSJohn Baldwin *func = conf[0].pc_sel.pc_func; 797baf753ccSJohn Baldwin return (true); 798baf753ccSJohn Baldwin } 799baf753ccSJohn Baldwin 800621b5090SJohn Baldwin static int 801*6a284cacSJohn Baldwin passthru_init(struct pci_devinst *pi, nvlist_t *nvl) 802366f6083SPeter Grehan { 8039b1aa8d6SNeel Natu int bus, slot, func, error, memflags; 804366f6083SPeter Grehan struct passthru_softc *sc; 805621b5090SJohn Baldwin const char *value; 806366f6083SPeter Grehan 807366f6083SPeter Grehan sc = NULL; 808366f6083SPeter Grehan error = 1; 809366f6083SPeter Grehan 810*6a284cacSJohn Baldwin memflags = vm_get_memflags(pi->pi_vmctx); 8119b1aa8d6SNeel Natu if (!(memflags & VM_MEM_F_WIRED)) { 812cff92ffdSJohn Baldwin warnx("passthru requires guest memory to be wired"); 813dbb15211SSean Chittenden return (error); 8149b1aa8d6SNeel Natu } 8159b1aa8d6SNeel Natu 816563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 817dbb15211SSean Chittenden return (error); 818366f6083SPeter Grehan } 81900ef17beSBartek Rutkowski 820621b5090SJohn Baldwin #define GET_INT_CONFIG(var, name) do { \ 821621b5090SJohn Baldwin value = get_config_value_node(nvl, name); \ 822621b5090SJohn Baldwin if (value == NULL) { \ 823621b5090SJohn Baldwin EPRINTLN("passthru: missing required %s setting", name); \ 824621b5090SJohn Baldwin return (error); \ 825621b5090SJohn Baldwin } \ 826621b5090SJohn Baldwin var = atoi(value); \ 827621b5090SJohn Baldwin } while (0) 828621b5090SJohn Baldwin 829baf753ccSJohn Baldwin value = get_config_value_node(nvl, "pptdev"); 830baf753ccSJohn Baldwin if (value != NULL) { 831baf753ccSJohn Baldwin if (!passthru_lookup_pptdev(value, &bus, &slot, &func)) 832baf753ccSJohn Baldwin return (error); 833baf753ccSJohn Baldwin } else { 834621b5090SJohn Baldwin GET_INT_CONFIG(bus, "bus"); 835621b5090SJohn Baldwin GET_INT_CONFIG(slot, "slot"); 836621b5090SJohn Baldwin GET_INT_CONFIG(func, "func"); 837baf753ccSJohn Baldwin } 838366f6083SPeter Grehan 839*6a284cacSJohn Baldwin if (vm_assign_pptdev(pi->pi_vmctx, bus, slot, func) != 0) { 840cff92ffdSJohn Baldwin warnx("PCI device at %d/%d/%d is not using the ppt(4) driver", 841cff92ffdSJohn Baldwin bus, slot, func); 842366f6083SPeter Grehan goto done; 843cff92ffdSJohn Baldwin } 844366f6083SPeter Grehan 845994f858aSXin LI sc = calloc(1, sizeof(struct passthru_softc)); 846366f6083SPeter Grehan 847366f6083SPeter Grehan pi->pi_arg = sc; 848366f6083SPeter Grehan sc->psc_pi = pi; 849366f6083SPeter Grehan 850366f6083SPeter Grehan /* initialize config space */ 851*6a284cacSJohn Baldwin if ((error = cfginit(pi, bus, slot, func)) != 0) 852e47fe318SCorvin Köhne goto done; 853e47fe318SCorvin Köhne 854e47fe318SCorvin Köhne /* initialize ROM */ 855*6a284cacSJohn Baldwin if ((error = passthru_init_rom(sc, 856e47fe318SCorvin Köhne get_config_value_node(nvl, "rom"))) != 0) 857e47fe318SCorvin Köhne goto done; 858e47fe318SCorvin Köhne 859e47fe318SCorvin Köhne error = 0; /* success */ 860366f6083SPeter Grehan done: 861366f6083SPeter Grehan if (error) { 862366f6083SPeter Grehan free(sc); 863*6a284cacSJohn Baldwin vm_unassign_pptdev(pi->pi_vmctx, bus, slot, func); 864366f6083SPeter Grehan } 865366f6083SPeter Grehan return (error); 866366f6083SPeter Grehan } 867366f6083SPeter Grehan 868366f6083SPeter Grehan static int 869366f6083SPeter Grehan bar_access(int coff) 870366f6083SPeter Grehan { 871e47fe318SCorvin Köhne if ((coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) || 872e47fe318SCorvin Köhne coff == PCIR_BIOS) 873366f6083SPeter Grehan return (1); 874366f6083SPeter Grehan else 875366f6083SPeter Grehan return (0); 876366f6083SPeter Grehan } 877366f6083SPeter Grehan 878366f6083SPeter Grehan static int 879366f6083SPeter Grehan msicap_access(struct passthru_softc *sc, int coff) 880366f6083SPeter Grehan { 881366f6083SPeter Grehan int caplen; 882366f6083SPeter Grehan 883366f6083SPeter Grehan if (sc->psc_msi.capoff == 0) 884366f6083SPeter Grehan return (0); 885366f6083SPeter Grehan 886366f6083SPeter Grehan caplen = msi_caplen(sc->psc_msi.msgctrl); 887366f6083SPeter Grehan 888366f6083SPeter Grehan if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen) 889366f6083SPeter Grehan return (1); 890366f6083SPeter Grehan else 891366f6083SPeter Grehan return (0); 892366f6083SPeter Grehan } 893366f6083SPeter Grehan 894366f6083SPeter Grehan static int 895cd942e0fSPeter Grehan msixcap_access(struct passthru_softc *sc, int coff) 896cd942e0fSPeter Grehan { 897cd942e0fSPeter Grehan if (sc->psc_msix.capoff == 0) 898cd942e0fSPeter Grehan return (0); 899cd942e0fSPeter Grehan 900cd942e0fSPeter Grehan return (coff >= sc->psc_msix.capoff && 901cd942e0fSPeter Grehan coff < sc->psc_msix.capoff + MSIX_CAPLEN); 902cd942e0fSPeter Grehan } 903cd942e0fSPeter Grehan 904cd942e0fSPeter Grehan static int 905*6a284cacSJohn Baldwin passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv) 906366f6083SPeter Grehan { 907366f6083SPeter Grehan struct passthru_softc *sc; 908366f6083SPeter Grehan 909366f6083SPeter Grehan sc = pi->pi_arg; 910366f6083SPeter Grehan 911366f6083SPeter Grehan /* 912366f6083SPeter Grehan * PCI BARs and MSI capability is emulated. 913366f6083SPeter Grehan */ 914fe66bcf9SCorvin Köhne if (bar_access(coff) || msicap_access(sc, coff) || 915fe66bcf9SCorvin Köhne msixcap_access(sc, coff)) 916366f6083SPeter Grehan return (-1); 917366f6083SPeter Grehan 918366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 919366f6083SPeter Grehan /* 920366f6083SPeter Grehan * Emulate PCIR_CAP_PTR if this device does not support MSI capability 921366f6083SPeter Grehan * natively. 922366f6083SPeter Grehan */ 923366f6083SPeter Grehan if (sc->psc_msi.emulated) { 924366f6083SPeter Grehan if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4) 925366f6083SPeter Grehan return (-1); 926366f6083SPeter Grehan } 927366f6083SPeter Grehan #endif 928366f6083SPeter Grehan 929c7ba149dSJohn Baldwin /* 930c7ba149dSJohn Baldwin * Emulate the command register. If a single read reads both the 931c7ba149dSJohn Baldwin * command and status registers, read the status register from the 932c7ba149dSJohn Baldwin * device's config space. 933c7ba149dSJohn Baldwin */ 934c7ba149dSJohn Baldwin if (coff == PCIR_COMMAND) { 935c7ba149dSJohn Baldwin if (bytes <= 2) 936c7ba149dSJohn Baldwin return (-1); 93721368498SPeter Grehan *rv = read_config(&sc->psc_sel, PCIR_STATUS, 2) << 16 | 93821368498SPeter Grehan pci_get_cfgdata16(pi, PCIR_COMMAND); 939c7ba149dSJohn Baldwin return (0); 940c7ba149dSJohn Baldwin } 941c7ba149dSJohn Baldwin 942366f6083SPeter Grehan /* Everything else just read from the device's config space */ 943366f6083SPeter Grehan *rv = read_config(&sc->psc_sel, coff, bytes); 944366f6083SPeter Grehan 945366f6083SPeter Grehan return (0); 946366f6083SPeter Grehan } 947366f6083SPeter Grehan 948366f6083SPeter Grehan static int 949*6a284cacSJohn Baldwin passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val) 950366f6083SPeter Grehan { 951cd942e0fSPeter Grehan int error, msix_table_entries, i; 952366f6083SPeter Grehan struct passthru_softc *sc; 95356282675SJohn Baldwin uint16_t cmd_old; 954366f6083SPeter Grehan 955366f6083SPeter Grehan sc = pi->pi_arg; 956366f6083SPeter Grehan 957366f6083SPeter Grehan /* 958366f6083SPeter Grehan * PCI BARs are emulated 959366f6083SPeter Grehan */ 960366f6083SPeter Grehan if (bar_access(coff)) 961366f6083SPeter Grehan return (-1); 962366f6083SPeter Grehan 963366f6083SPeter Grehan /* 964366f6083SPeter Grehan * MSI capability is emulated 965366f6083SPeter Grehan */ 966366f6083SPeter Grehan if (msicap_access(sc, coff)) { 96721368498SPeter Grehan pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff, 96821368498SPeter Grehan PCIY_MSI); 969*6a284cacSJohn Baldwin error = vm_setup_pptdev_msi(pi->pi_vmctx, 0, sc->psc_sel.pc_bus, 9704f8be175SNeel Natu sc->psc_sel.pc_dev, sc->psc_sel.pc_func, 9714f8be175SNeel Natu pi->pi_msi.addr, pi->pi_msi.msg_data, 9724f8be175SNeel Natu pi->pi_msi.maxmsgnum); 973cff92ffdSJohn Baldwin if (error != 0) 974cff92ffdSJohn Baldwin err(1, "vm_setup_pptdev_msi"); 975366f6083SPeter Grehan return (0); 976366f6083SPeter Grehan } 977366f6083SPeter Grehan 978cd942e0fSPeter Grehan if (msixcap_access(sc, coff)) { 97921368498SPeter Grehan pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msix.capoff, 98021368498SPeter Grehan PCIY_MSIX); 981cd942e0fSPeter Grehan if (pi->pi_msix.enabled) { 982cd942e0fSPeter Grehan msix_table_entries = pi->pi_msix.table_count; 983cd942e0fSPeter Grehan for (i = 0; i < msix_table_entries; i++) { 984*6a284cacSJohn Baldwin error = vm_setup_pptdev_msix(pi->pi_vmctx, 0, 9854f8be175SNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 986cd942e0fSPeter Grehan sc->psc_sel.pc_func, i, 9874f8be175SNeel Natu pi->pi_msix.table[i].addr, 988cd942e0fSPeter Grehan pi->pi_msix.table[i].msg_data, 9894f8be175SNeel Natu pi->pi_msix.table[i].vector_control); 990cd942e0fSPeter Grehan 991cff92ffdSJohn Baldwin if (error) 992cff92ffdSJohn Baldwin err(1, "vm_setup_pptdev_msix"); 993cd942e0fSPeter Grehan } 9941925586eSJohn Baldwin } else { 995*6a284cacSJohn Baldwin error = vm_disable_pptdev_msix(pi->pi_vmctx, 996*6a284cacSJohn Baldwin sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 997*6a284cacSJohn Baldwin sc->psc_sel.pc_func); 9981925586eSJohn Baldwin if (error) 9991925586eSJohn Baldwin err(1, "vm_disable_pptdev_msix"); 1000cd942e0fSPeter Grehan } 1001cd942e0fSPeter Grehan return (0); 1002cd942e0fSPeter Grehan } 1003cd942e0fSPeter Grehan 1004366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 1005366f6083SPeter Grehan /* 1006366f6083SPeter Grehan * If this device does not support MSI natively then we cannot let 1007366f6083SPeter Grehan * the guest disable legacy interrupts from the device. It is the 1008366f6083SPeter Grehan * legacy interrupt that is triggering the virtual MSI to the guest. 1009366f6083SPeter Grehan */ 1010366f6083SPeter Grehan if (sc->psc_msi.emulated && pci_msi_enabled(pi)) { 1011366f6083SPeter Grehan if (coff == PCIR_COMMAND && bytes == 2) 1012366f6083SPeter Grehan val &= ~PCIM_CMD_INTxDIS; 1013366f6083SPeter Grehan } 1014366f6083SPeter Grehan #endif 1015366f6083SPeter Grehan 1016366f6083SPeter Grehan write_config(&sc->psc_sel, coff, bytes, val); 101756282675SJohn Baldwin if (coff == PCIR_COMMAND) { 101856282675SJohn Baldwin cmd_old = pci_get_cfgdata16(pi, PCIR_COMMAND); 101956282675SJohn Baldwin if (bytes == 1) 102056282675SJohn Baldwin pci_set_cfgdata8(pi, PCIR_COMMAND, val); 102156282675SJohn Baldwin else if (bytes == 2) 102256282675SJohn Baldwin pci_set_cfgdata16(pi, PCIR_COMMAND, val); 102356282675SJohn Baldwin pci_emul_cmd_changed(pi, cmd_old); 102456282675SJohn Baldwin } 1025366f6083SPeter Grehan 1026366f6083SPeter Grehan return (0); 1027366f6083SPeter Grehan } 1028366f6083SPeter Grehan 1029366f6083SPeter Grehan static void 1030*6a284cacSJohn Baldwin passthru_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size, 1031*6a284cacSJohn Baldwin uint64_t value) 1032366f6083SPeter Grehan { 1033366f6083SPeter Grehan struct passthru_softc *sc; 103442375556SMark Johnston struct pci_bar_ioreq pio; 1035366f6083SPeter Grehan 1036366f6083SPeter Grehan sc = pi->pi_arg; 1037366f6083SPeter Grehan 1038aa12663fSNeel Natu if (baridx == pci_msix_table_bar(pi)) { 1039*6a284cacSJohn Baldwin msix_table_write(sc, offset, size, value); 10404d1e669cSPeter Grehan } else { 10414d1e669cSPeter Grehan assert(pi->pi_bar[baridx].type == PCIBAR_IO); 104242375556SMark Johnston assert(size == 1 || size == 2 || size == 4); 104342375556SMark Johnston assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX); 1044366f6083SPeter Grehan 104542375556SMark Johnston bzero(&pio, sizeof(pio)); 104642375556SMark Johnston pio.pbi_sel = sc->psc_sel; 104742375556SMark Johnston pio.pbi_op = PCIBARIO_WRITE; 104842375556SMark Johnston pio.pbi_bar = baridx; 104942375556SMark Johnston pio.pbi_offset = (uint32_t)offset; 105042375556SMark Johnston pio.pbi_width = size; 105142375556SMark Johnston pio.pbi_value = (uint32_t)value; 105242375556SMark Johnston 105342375556SMark Johnston (void)ioctl(pcifd, PCIOCBARIO, &pio); 1054366f6083SPeter Grehan } 10554d1e669cSPeter Grehan } 1056366f6083SPeter Grehan 10574d1e669cSPeter Grehan static uint64_t 1058*6a284cacSJohn Baldwin passthru_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size) 1059366f6083SPeter Grehan { 1060366f6083SPeter Grehan struct passthru_softc *sc; 106142375556SMark Johnston struct pci_bar_ioreq pio; 10624d1e669cSPeter Grehan uint64_t val; 1063366f6083SPeter Grehan 1064366f6083SPeter Grehan sc = pi->pi_arg; 1065366f6083SPeter Grehan 1066aa12663fSNeel Natu if (baridx == pci_msix_table_bar(pi)) { 10674d1e669cSPeter Grehan val = msix_table_read(sc, offset, size); 10684d1e669cSPeter Grehan } else { 10694d1e669cSPeter Grehan assert(pi->pi_bar[baridx].type == PCIBAR_IO); 107042375556SMark Johnston assert(size == 1 || size == 2 || size == 4); 107142375556SMark Johnston assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX); 1072366f6083SPeter Grehan 107342375556SMark Johnston bzero(&pio, sizeof(pio)); 107442375556SMark Johnston pio.pbi_sel = sc->psc_sel; 107542375556SMark Johnston pio.pbi_op = PCIBARIO_READ; 107642375556SMark Johnston pio.pbi_bar = baridx; 107742375556SMark Johnston pio.pbi_offset = (uint32_t)offset; 107842375556SMark Johnston pio.pbi_width = size; 1079366f6083SPeter Grehan 108042375556SMark Johnston (void)ioctl(pcifd, PCIOCBARIO, &pio); 108142375556SMark Johnston 108242375556SMark Johnston val = pio.pbi_value; 10834d1e669cSPeter Grehan } 10844d1e669cSPeter Grehan 10854d1e669cSPeter Grehan return (val); 1086366f6083SPeter Grehan } 1087366f6083SPeter Grehan 1088f8a6ec2dSD Scott Phillips static void 1089*6a284cacSJohn Baldwin passthru_msix_addr(struct pci_devinst *pi, int baridx, int enabled, 1090*6a284cacSJohn Baldwin uint64_t address) 1091f8a6ec2dSD Scott Phillips { 1092f8a6ec2dSD Scott Phillips struct passthru_softc *sc; 1093f8a6ec2dSD Scott Phillips size_t remaining; 1094f8a6ec2dSD Scott Phillips uint32_t table_size, table_offset; 1095f8a6ec2dSD Scott Phillips 1096f8a6ec2dSD Scott Phillips sc = pi->pi_arg; 1097f8a6ec2dSD Scott Phillips table_offset = rounddown2(pi->pi_msix.table_offset, 4096); 1098f8a6ec2dSD Scott Phillips if (table_offset > 0) { 1099f8a6ec2dSD Scott Phillips if (!enabled) { 1100*6a284cacSJohn Baldwin if (vm_unmap_pptdev_mmio(pi->pi_vmctx, 1101*6a284cacSJohn Baldwin sc->psc_sel.pc_bus, 1102f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1103f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1104f8a6ec2dSD Scott Phillips table_offset) != 0) 1105f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1106f8a6ec2dSD Scott Phillips } else { 1107*6a284cacSJohn Baldwin if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus, 1108f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1109f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1110f8a6ec2dSD Scott Phillips table_offset, 1111f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr) != 0) 1112f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1113f8a6ec2dSD Scott Phillips } 1114f8a6ec2dSD Scott Phillips } 1115f8a6ec2dSD Scott Phillips table_size = pi->pi_msix.table_offset - table_offset; 1116f8a6ec2dSD Scott Phillips table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 1117f8a6ec2dSD Scott Phillips table_size = roundup2(table_size, 4096); 1118f8a6ec2dSD Scott Phillips remaining = pi->pi_bar[baridx].size - table_offset - table_size; 1119f8a6ec2dSD Scott Phillips if (remaining > 0) { 1120f8a6ec2dSD Scott Phillips address += table_offset + table_size; 1121f8a6ec2dSD Scott Phillips if (!enabled) { 1122*6a284cacSJohn Baldwin if (vm_unmap_pptdev_mmio(pi->pi_vmctx, 1123*6a284cacSJohn Baldwin sc->psc_sel.pc_bus, 1124f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1125f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1126f8a6ec2dSD Scott Phillips remaining) != 0) 1127f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1128f8a6ec2dSD Scott Phillips } else { 1129*6a284cacSJohn Baldwin if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus, 1130f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1131f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1132f8a6ec2dSD Scott Phillips remaining, 1133f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr + 1134f8a6ec2dSD Scott Phillips table_offset + table_size) != 0) 1135f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1136f8a6ec2dSD Scott Phillips } 1137f8a6ec2dSD Scott Phillips } 1138f8a6ec2dSD Scott Phillips } 1139f8a6ec2dSD Scott Phillips 1140f8a6ec2dSD Scott Phillips static void 1141*6a284cacSJohn Baldwin passthru_mmio_addr(struct pci_devinst *pi, int baridx, int enabled, 1142*6a284cacSJohn Baldwin uint64_t address) 1143f8a6ec2dSD Scott Phillips { 1144f8a6ec2dSD Scott Phillips struct passthru_softc *sc; 1145f8a6ec2dSD Scott Phillips 1146f8a6ec2dSD Scott Phillips sc = pi->pi_arg; 1147f8a6ec2dSD Scott Phillips if (!enabled) { 1148*6a284cacSJohn Baldwin if (vm_unmap_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus, 1149f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1150f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1151f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].size) != 0) 1152f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1153f8a6ec2dSD Scott Phillips } else { 1154*6a284cacSJohn Baldwin if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus, 1155f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1156f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1157f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].size, 1158f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr) != 0) 1159f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1160f8a6ec2dSD Scott Phillips } 1161f8a6ec2dSD Scott Phillips } 1162f8a6ec2dSD Scott Phillips 1163f8a6ec2dSD Scott Phillips static void 1164e47fe318SCorvin Köhne passthru_addr_rom(struct pci_devinst *const pi, const int idx, 1165e47fe318SCorvin Köhne const int enabled) 1166e47fe318SCorvin Köhne { 1167e47fe318SCorvin Köhne const uint64_t addr = pi->pi_bar[idx].addr; 1168e47fe318SCorvin Köhne const uint64_t size = pi->pi_bar[idx].size; 1169e47fe318SCorvin Köhne 1170e47fe318SCorvin Köhne if (!enabled) { 1171e47fe318SCorvin Köhne if (vm_munmap_memseg(pi->pi_vmctx, addr, size) != 0) { 1172e47fe318SCorvin Köhne errx(4, "%s: munmap_memseg @ [%016lx - %016lx] failed", 1173e47fe318SCorvin Köhne __func__, addr, addr + size); 1174e47fe318SCorvin Köhne } 1175e47fe318SCorvin Köhne 1176e47fe318SCorvin Köhne } else { 1177e47fe318SCorvin Köhne if (vm_mmap_memseg(pi->pi_vmctx, addr, VM_PCIROM, 1178e47fe318SCorvin Köhne pi->pi_romoffset, size, PROT_READ | PROT_EXEC) != 0) { 117950526f52SCorvin Köhne errx(4, "%s: mmap_memseg @ [%016lx - %016lx] failed", 1180e47fe318SCorvin Köhne __func__, addr, addr + size); 1181e47fe318SCorvin Köhne } 1182e47fe318SCorvin Köhne } 1183e47fe318SCorvin Köhne } 1184e47fe318SCorvin Köhne 1185e47fe318SCorvin Köhne static void 1186*6a284cacSJohn Baldwin passthru_addr(struct pci_devinst *pi, int baridx, int enabled, uint64_t address) 1187f8a6ec2dSD Scott Phillips { 1188e47fe318SCorvin Köhne switch (pi->pi_bar[baridx].type) { 1189e47fe318SCorvin Köhne case PCIBAR_IO: 1190e47fe318SCorvin Köhne /* IO BARs are emulated */ 1191e47fe318SCorvin Köhne break; 1192e47fe318SCorvin Köhne case PCIBAR_ROM: 1193e47fe318SCorvin Köhne passthru_addr_rom(pi, baridx, enabled); 1194e47fe318SCorvin Köhne break; 1195e47fe318SCorvin Köhne case PCIBAR_MEM32: 1196e47fe318SCorvin Köhne case PCIBAR_MEM64: 1197f8a6ec2dSD Scott Phillips if (baridx == pci_msix_table_bar(pi)) 1198*6a284cacSJohn Baldwin passthru_msix_addr(pi, baridx, enabled, address); 1199f8a6ec2dSD Scott Phillips else 1200*6a284cacSJohn Baldwin passthru_mmio_addr(pi, baridx, enabled, address); 1201e47fe318SCorvin Köhne break; 1202e47fe318SCorvin Köhne default: 1203e47fe318SCorvin Köhne errx(4, "%s: invalid BAR type %d", __func__, 1204e47fe318SCorvin Köhne pi->pi_bar[baridx].type); 1205e47fe318SCorvin Köhne } 1206f8a6ec2dSD Scott Phillips } 1207f8a6ec2dSD Scott Phillips 120837045dfaSMark Johnston static const struct pci_devemu passthru = { 1209366f6083SPeter Grehan .pe_emu = "passthru", 1210366f6083SPeter Grehan .pe_init = passthru_init, 1211621b5090SJohn Baldwin .pe_legacy_config = passthru_legacy_config, 1212366f6083SPeter Grehan .pe_cfgwrite = passthru_cfgwrite, 1213366f6083SPeter Grehan .pe_cfgread = passthru_cfgread, 12144d1e669cSPeter Grehan .pe_barwrite = passthru_write, 12154d1e669cSPeter Grehan .pe_barread = passthru_read, 1216f8a6ec2dSD Scott Phillips .pe_baraddr = passthru_addr, 1217366f6083SPeter Grehan }; 1218366f6083SPeter Grehan PCI_EMUL_SET(passthru); 1219