2011-05-13 04:54:01 +00:00
|
|
|
/*-
|
2023-05-10 15:40:58 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2017-11-27 15:37:16 +00:00
|
|
|
*
|
2011-05-13 04:54:01 +00:00
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <sys/capsicum.h>
|
|
|
|
#endif
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <sys/types.h>
|
2016-04-13 18:39:33 +00:00
|
|
|
#include <sys/mman.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <sys/pciio.h>
|
|
|
|
#include <sys/ioctl.h>
|
2022-03-10 10:28:06 +00:00
|
|
|
#include <sys/stat.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#include <dev/io/iodev.h>
|
2013-01-21 22:07:05 +00:00
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
#include <vm/vm.h>
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <machine/iodev.h>
|
2021-10-09 15:36:19 +00:00
|
|
|
#include <machine/vm.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2019-01-16 00:39:23 +00:00
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
#include <capsicum_helpers.h>
|
|
|
|
#endif
|
2022-08-19 21:55:29 +00:00
|
|
|
#include <ctype.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-04-19 20:43:05 +00:00
|
|
|
#include <err.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <errno.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <fcntl.h>
|
2017-02-14 13:35:59 +00:00
|
|
|
#include <sysexits.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <machine/vmm.h>
|
2019-06-26 20:30:41 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
2012-10-19 18:11:17 +00:00
|
|
|
#include "mem.h"
|
2022-03-10 10:26:19 +00:00
|
|
|
#include "pci_passthru.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#ifndef _PATH_DEVPCI
|
|
|
|
#define _PATH_DEVPCI "/dev/pci"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LEGACY_SUPPORT 1
|
|
|
|
|
2013-01-21 22:07:05 +00:00
|
|
|
#define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
|
2012-04-28 16:28:00 +00:00
|
|
|
#define MSIX_CAPLEN 12
|
|
|
|
|
2023-05-10 10:22:33 +00:00
|
|
|
#define PASSTHRU_MMIO_MAX 2
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int pcifd = -1;
|
|
|
|
|
2023-05-10 10:31:50 +00:00
|
|
|
SET_DECLARE(passthru_dev_set, struct passthru_dev);
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
struct passthru_softc {
|
|
|
|
struct pci_devinst *psc_pi;
|
2022-03-10 10:28:06 +00:00
|
|
|
/* ROM is handled like a BAR */
|
|
|
|
struct pcibar psc_bar[PCI_BARMAX_WITH_ROM + 1];
|
2011-05-13 04:54:01 +00:00
|
|
|
struct {
|
|
|
|
int capoff;
|
|
|
|
int msgctrl;
|
|
|
|
int emulated;
|
|
|
|
} psc_msi;
|
2012-04-28 16:28:00 +00:00
|
|
|
struct {
|
|
|
|
int capoff;
|
|
|
|
} psc_msix;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct pcisel psc_sel;
|
2021-03-19 12:48:34 +00:00
|
|
|
|
2023-05-10 10:22:33 +00:00
|
|
|
struct passthru_mmio_mapping psc_mmio_map[PASSTHRU_MMIO_MAX];
|
2021-03-19 12:48:34 +00:00
|
|
|
cfgread_handler psc_pcir_rhandler[PCI_REGMAX + 1];
|
|
|
|
cfgwrite_handler psc_pcir_whandler[PCI_REGMAX + 1];
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
msi_caplen(int msgctrl)
|
|
|
|
{
|
|
|
|
int len;
|
2021-12-26 07:52:38 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
len = 10; /* minimum length of msi capability */
|
|
|
|
|
|
|
|
if (msgctrl & PCIM_MSICTRL_64BIT)
|
|
|
|
len += 4;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Ignore the 'mask' and 'pending' bits in the MSI capability.
|
|
|
|
* We'll let the guest manipulate them directly.
|
|
|
|
*/
|
|
|
|
if (msgctrl & PCIM_MSICTRL_VECTOR)
|
|
|
|
len += 10;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:26:19 +00:00
|
|
|
static int
|
2022-08-16 17:10:58 +00:00
|
|
|
pcifd_init(void)
|
|
|
|
{
|
2022-03-10 10:26:19 +00:00
|
|
|
pcifd = open(_PATH_DEVPCI, O_RDWR, 0);
|
|
|
|
if (pcifd < 0) {
|
|
|
|
warn("failed to open %s", _PATH_DEVPCI);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef WITHOUT_CAPSICUM
|
|
|
|
cap_rights_t pcifd_rights;
|
|
|
|
cap_rights_init(&pcifd_rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
|
|
|
|
if (caph_rights_limit(pcifd, &pcifd_rights) == -1)
|
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
|
|
|
|
const cap_ioctl_t pcifd_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR,
|
2022-08-19 21:55:29 +00:00
|
|
|
PCIOCBARIO, PCIOCBARMMAP, PCIOCGETCONF };
|
2022-03-10 10:26:19 +00:00
|
|
|
if (caph_ioctls_limit(pcifd, pcifd_ioctls, nitems(pcifd_ioctls)) == -1)
|
|
|
|
errx(EX_OSERR, "Unable to apply rights for sandbox");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2011-05-13 04:54:01 +00:00
|
|
|
read_config(const struct pcisel *sel, long reg, int width)
|
|
|
|
{
|
2022-08-17 17:00:09 +00:00
|
|
|
struct pci_io pi;
|
|
|
|
|
2022-03-10 10:26:19 +00:00
|
|
|
if (pcifd < 0 && pcifd_init()) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
bzero(&pi, sizeof(pi));
|
|
|
|
pi.pi_sel = *sel;
|
|
|
|
pi.pi_reg = reg;
|
|
|
|
pi.pi_width = width;
|
|
|
|
|
|
|
|
if (ioctl(pcifd, PCIOCREAD, &pi) < 0)
|
|
|
|
return (0); /* XXX */
|
|
|
|
else
|
|
|
|
return (pi.pi_data);
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:26:19 +00:00
|
|
|
void
|
2011-05-13 04:54:01 +00:00
|
|
|
write_config(const struct pcisel *sel, long reg, int width, uint32_t data)
|
|
|
|
{
|
2022-08-17 17:00:09 +00:00
|
|
|
struct pci_io pi;
|
|
|
|
|
2022-03-10 10:26:19 +00:00
|
|
|
if (pcifd < 0 && pcifd_init()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
bzero(&pi, sizeof(pi));
|
|
|
|
pi.pi_sel = *sel;
|
|
|
|
pi.pi_reg = reg;
|
|
|
|
pi.pi_width = width;
|
|
|
|
pi.pi_data = data;
|
|
|
|
|
|
|
|
(void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LEGACY_SUPPORT
|
|
|
|
static int
|
|
|
|
passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
|
|
|
|
{
|
2022-10-23 14:32:45 +00:00
|
|
|
int capoff;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct msicap msicap;
|
|
|
|
u_char *capdata;
|
|
|
|
|
|
|
|
pci_populate_msicap(&msicap, msgnum, nextptr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Copy the msi capability structure in the last 16 bytes of the
|
|
|
|
* config space. This is wrong because it could shadow something
|
|
|
|
* useful to the device.
|
|
|
|
*/
|
|
|
|
capoff = 256 - roundup(sizeof(msicap), 4);
|
|
|
|
capdata = (u_char *)&msicap;
|
2022-10-23 14:32:45 +00:00
|
|
|
for (size_t i = 0; i < sizeof(msicap); i++)
|
2011-05-13 04:54:01 +00:00
|
|
|
pci_set_cfgdata8(pi, capoff + i, capdata[i]);
|
|
|
|
|
|
|
|
return (capoff);
|
|
|
|
}
|
|
|
|
#endif /* LEGACY_SUPPORT */
|
|
|
|
|
|
|
|
static int
|
|
|
|
cfginitmsi(struct passthru_softc *sc)
|
|
|
|
{
|
2013-01-21 22:07:05 +00:00
|
|
|
int i, ptr, capptr, cap, sts, caplen, table_size;
|
2011-05-13 04:54:01 +00:00
|
|
|
uint32_t u32;
|
|
|
|
struct pcisel sel;
|
|
|
|
struct pci_devinst *pi;
|
2012-04-28 16:28:00 +00:00
|
|
|
struct msixcap msixcap;
|
2022-11-29 01:10:07 +00:00
|
|
|
char *msixcap_ptr;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
pi = sc->psc_pi;
|
|
|
|
sel = sc->psc_sel;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the capabilities and cache the location of the MSI
|
2012-04-28 16:28:00 +00:00
|
|
|
* and MSI-X capabilities.
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
|
|
|
sts = read_config(&sel, PCIR_STATUS, 2);
|
|
|
|
if (sts & PCIM_STATUS_CAPPRESENT) {
|
|
|
|
ptr = read_config(&sel, PCIR_CAP_PTR, 1);
|
|
|
|
while (ptr != 0 && ptr != 0xff) {
|
|
|
|
cap = read_config(&sel, ptr + PCICAP_ID, 1);
|
|
|
|
if (cap == PCIY_MSI) {
|
|
|
|
/*
|
|
|
|
* Copy the MSI capability into the config
|
|
|
|
* space of the emulated pci device
|
|
|
|
*/
|
|
|
|
sc->psc_msi.capoff = ptr;
|
|
|
|
sc->psc_msi.msgctrl = read_config(&sel,
|
|
|
|
ptr + 2, 2);
|
|
|
|
sc->psc_msi.emulated = 0;
|
|
|
|
caplen = msi_caplen(sc->psc_msi.msgctrl);
|
2012-04-28 16:28:00 +00:00
|
|
|
capptr = ptr;
|
2011-05-13 04:54:01 +00:00
|
|
|
while (caplen > 0) {
|
2012-04-28 16:28:00 +00:00
|
|
|
u32 = read_config(&sel, capptr, 4);
|
|
|
|
pci_set_cfgdata32(pi, capptr, u32);
|
2011-05-13 04:54:01 +00:00
|
|
|
caplen -= 4;
|
2012-04-28 16:28:00 +00:00
|
|
|
capptr += 4;
|
|
|
|
}
|
|
|
|
} else if (cap == PCIY_MSIX) {
|
|
|
|
/*
|
2021-12-26 07:52:38 +00:00
|
|
|
* Copy the MSI-X capability
|
2012-04-28 16:28:00 +00:00
|
|
|
*/
|
|
|
|
sc->psc_msix.capoff = ptr;
|
|
|
|
caplen = 12;
|
2022-11-29 01:10:07 +00:00
|
|
|
msixcap_ptr = (char *)&msixcap;
|
2012-04-28 16:28:00 +00:00
|
|
|
capptr = ptr;
|
|
|
|
while (caplen > 0) {
|
|
|
|
u32 = read_config(&sel, capptr, 4);
|
2022-11-29 01:10:07 +00:00
|
|
|
memcpy(msixcap_ptr, &u32, 4);
|
2012-04-28 16:28:00 +00:00
|
|
|
pci_set_cfgdata32(pi, capptr, u32);
|
|
|
|
caplen -= 4;
|
|
|
|
capptr += 4;
|
2022-11-29 01:10:07 +00:00
|
|
|
msixcap_ptr += 4;
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
if (sc->psc_msix.capoff != 0) {
|
|
|
|
pi->pi_msix.pba_bar =
|
2013-01-21 22:07:05 +00:00
|
|
|
msixcap.pba_info & PCIM_MSIX_BIR_MASK;
|
2012-10-19 18:11:17 +00:00
|
|
|
pi->pi_msix.pba_offset =
|
2013-01-21 22:07:05 +00:00
|
|
|
msixcap.pba_info & ~PCIM_MSIX_BIR_MASK;
|
2012-10-19 18:11:17 +00:00
|
|
|
pi->pi_msix.table_bar =
|
2013-01-21 22:07:05 +00:00
|
|
|
msixcap.table_info & PCIM_MSIX_BIR_MASK;
|
2012-10-19 18:11:17 +00:00
|
|
|
pi->pi_msix.table_offset =
|
2013-01-21 22:07:05 +00:00
|
|
|
msixcap.table_info & ~PCIM_MSIX_BIR_MASK;
|
2012-10-19 18:11:17 +00:00
|
|
|
pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
|
2014-02-18 19:00:15 +00:00
|
|
|
pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count);
|
2013-01-21 22:07:05 +00:00
|
|
|
|
|
|
|
/* Allocate the emulated MSI-X table array */
|
|
|
|
table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
|
2014-04-22 18:55:21 +00:00
|
|
|
pi->pi_msix.table = calloc(1, table_size);
|
2013-01-21 22:07:05 +00:00
|
|
|
|
|
|
|
/* Mask all table entries */
|
|
|
|
for (i = 0; i < pi->pi_msix.table_count; i++) {
|
|
|
|
pi->pi_msix.table[i].vector_control |=
|
|
|
|
PCIM_MSIX_VCTRL_MASK;
|
|
|
|
}
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#ifdef LEGACY_SUPPORT
|
|
|
|
/*
|
|
|
|
* If the passthrough device does not support MSI then craft a
|
|
|
|
* MSI capability for it. We link the new MSI capability at the
|
|
|
|
* head of the list of capabilities.
|
|
|
|
*/
|
|
|
|
if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
|
|
|
|
int origptr, msiptr;
|
|
|
|
origptr = read_config(&sel, PCIR_CAP_PTR, 1);
|
|
|
|
msiptr = passthru_add_msicap(pi, 1, origptr);
|
|
|
|
sc->psc_msi.capoff = msiptr;
|
|
|
|
sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
|
|
|
|
sc->psc_msi.emulated = 1;
|
|
|
|
pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
/* Make sure one of the capabilities is present */
|
2021-12-26 07:52:38 +00:00
|
|
|
if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0)
|
2011-05-13 04:54:01 +00:00
|
|
|
return (-1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
static uint64_t
|
|
|
|
msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
|
2012-04-28 16:28:00 +00:00
|
|
|
{
|
|
|
|
struct pci_devinst *pi;
|
2012-10-19 18:11:17 +00:00
|
|
|
struct msix_table_entry *entry;
|
2012-04-28 16:28:00 +00:00
|
|
|
uint8_t *src8;
|
|
|
|
uint16_t *src16;
|
|
|
|
uint32_t *src32;
|
|
|
|
uint64_t *src64;
|
2012-10-19 18:11:17 +00:00
|
|
|
uint64_t data;
|
|
|
|
size_t entry_offset;
|
2021-10-09 15:36:19 +00:00
|
|
|
uint32_t table_offset;
|
|
|
|
int index, table_count;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
|
|
|
pi = sc->psc_pi;
|
2021-10-09 15:36:19 +00:00
|
|
|
|
|
|
|
table_offset = pi->pi_msix.table_offset;
|
|
|
|
table_count = pi->pi_msix.table_count;
|
|
|
|
if (offset < table_offset ||
|
|
|
|
offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
|
|
|
|
switch (size) {
|
2016-04-13 18:39:33 +00:00
|
|
|
case 1:
|
2021-10-09 15:36:19 +00:00
|
|
|
src8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
data = *src8;
|
|
|
|
break;
|
|
|
|
case 2:
|
2021-10-09 15:36:19 +00:00
|
|
|
src16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
data = *src16;
|
|
|
|
break;
|
|
|
|
case 4:
|
2021-10-09 15:36:19 +00:00
|
|
|
src32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
data = *src32;
|
|
|
|
break;
|
|
|
|
case 8:
|
2021-10-09 15:36:19 +00:00
|
|
|
src64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
data = *src64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (data);
|
|
|
|
}
|
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
offset -= table_offset;
|
2012-04-28 16:28:00 +00:00
|
|
|
index = offset / MSIX_TABLE_ENTRY_SIZE;
|
2021-10-09 15:36:19 +00:00
|
|
|
assert(index < table_count);
|
2013-01-21 22:07:05 +00:00
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
entry = &pi->pi_msix.table[index];
|
2013-01-21 22:07:05 +00:00
|
|
|
entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
switch (size) {
|
2012-04-28 16:28:00 +00:00
|
|
|
case 1:
|
2021-10-09 15:36:19 +00:00
|
|
|
src8 = (uint8_t *)((uint8_t *)entry + entry_offset);
|
2012-10-19 18:11:17 +00:00
|
|
|
data = *src8;
|
2012-04-28 16:28:00 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-10-09 15:36:19 +00:00
|
|
|
src16 = (uint16_t *)((uint8_t *)entry + entry_offset);
|
2012-10-19 18:11:17 +00:00
|
|
|
data = *src16;
|
2012-04-28 16:28:00 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-10-09 15:36:19 +00:00
|
|
|
src32 = (uint32_t *)((uint8_t *)entry + entry_offset);
|
2012-10-19 18:11:17 +00:00
|
|
|
data = *src32;
|
2012-04-28 16:28:00 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2021-10-09 15:36:19 +00:00
|
|
|
src64 = (uint64_t *)((uint8_t *)entry + entry_offset);
|
2012-10-19 18:11:17 +00:00
|
|
|
data = *src64;
|
2012-04-28 16:28:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
return (data);
|
2012-04-28 16:28:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
static void
|
2023-01-19 18:30:18 +00:00
|
|
|
msix_table_write(struct passthru_softc *sc, uint64_t offset, int size,
|
|
|
|
uint64_t data)
|
2012-04-28 16:28:00 +00:00
|
|
|
{
|
|
|
|
struct pci_devinst *pi;
|
|
|
|
struct msix_table_entry *entry;
|
2016-04-13 18:39:33 +00:00
|
|
|
uint8_t *dest8;
|
|
|
|
uint16_t *dest16;
|
|
|
|
uint32_t *dest32;
|
|
|
|
uint64_t *dest64;
|
2012-10-19 18:11:17 +00:00
|
|
|
size_t entry_offset;
|
2021-10-09 15:36:19 +00:00
|
|
|
uint32_t table_offset, vector_control;
|
|
|
|
int index, table_count;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
|
|
|
pi = sc->psc_pi;
|
2021-10-09 15:36:19 +00:00
|
|
|
|
|
|
|
table_offset = pi->pi_msix.table_offset;
|
|
|
|
table_count = pi->pi_msix.table_count;
|
|
|
|
if (offset < table_offset ||
|
|
|
|
offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
|
|
|
|
switch (size) {
|
2016-04-13 18:39:33 +00:00
|
|
|
case 1:
|
2021-10-09 15:36:19 +00:00
|
|
|
dest8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
*dest8 = data;
|
|
|
|
break;
|
|
|
|
case 2:
|
2021-10-09 15:36:19 +00:00
|
|
|
dest16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
*dest16 = data;
|
|
|
|
break;
|
|
|
|
case 4:
|
2021-10-09 15:36:19 +00:00
|
|
|
dest32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
*dest32 = data;
|
|
|
|
break;
|
|
|
|
case 8:
|
2021-10-09 15:36:19 +00:00
|
|
|
dest64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
*dest64 = data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
offset -= table_offset;
|
2012-04-28 16:28:00 +00:00
|
|
|
index = offset / MSIX_TABLE_ENTRY_SIZE;
|
2021-10-09 15:36:19 +00:00
|
|
|
assert(index < table_count);
|
2013-01-21 22:07:05 +00:00
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
entry = &pi->pi_msix.table[index];
|
2013-01-21 22:07:05 +00:00
|
|
|
entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
|
|
|
/* Only 4 byte naturally-aligned writes are supported */
|
2012-10-19 18:11:17 +00:00
|
|
|
assert(size == 4);
|
|
|
|
assert(entry_offset % 4 == 0);
|
|
|
|
|
|
|
|
vector_control = entry->vector_control;
|
2022-10-22 17:40:20 +00:00
|
|
|
dest32 = (uint32_t *)((uint8_t *)entry + entry_offset);
|
2016-04-13 18:39:33 +00:00
|
|
|
*dest32 = data;
|
2012-10-19 18:11:17 +00:00
|
|
|
/* If MSI-X hasn't been enabled, do nothing */
|
|
|
|
if (pi->pi_msix.enabled) {
|
|
|
|
/* If the entry is masked, don't set it up */
|
|
|
|
if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
|
|
|
|
(vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
|
2023-03-24 18:49:06 +00:00
|
|
|
(void)vm_setup_pptdev_msix(sc->psc_pi->pi_vmctx,
|
2016-07-06 05:05:03 +00:00
|
|
|
sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
|
2013-12-18 03:58:51 +00:00
|
|
|
sc->psc_sel.pc_func, index, entry->addr,
|
|
|
|
entry->msg_data, entry->vector_control);
|
2012-04-28 16:28:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-01-19 18:30:18 +00:00
|
|
|
init_msix_table(struct passthru_softc *sc)
|
2012-04-28 16:28:00 +00:00
|
|
|
{
|
2021-10-09 15:36:19 +00:00
|
|
|
struct pci_devinst *pi = sc->psc_pi;
|
|
|
|
struct pci_bar_mmap pbm;
|
2013-02-01 03:49:09 +00:00
|
|
|
int b, s, f;
|
2014-02-18 19:00:15 +00:00
|
|
|
uint32_t table_size, table_offset;
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2013-02-01 02:41:47 +00:00
|
|
|
assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0);
|
|
|
|
|
2013-02-01 03:49:09 +00:00
|
|
|
b = sc->psc_sel.pc_bus;
|
|
|
|
s = sc->psc_sel.pc_dev;
|
|
|
|
f = sc->psc_sel.pc_func;
|
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
/*
|
|
|
|
* Map the region of the BAR containing the MSI-X table. This is
|
|
|
|
* necessary for two reasons:
|
|
|
|
* 1. The PBA may reside in the first or last page containing the MSI-X
|
|
|
|
* table.
|
|
|
|
* 2. While PCI devices are not supposed to use the page(s) containing
|
|
|
|
* the MSI-X table for other purposes, some do in practice.
|
2012-04-28 16:28:00 +00:00
|
|
|
*/
|
2021-10-09 15:36:19 +00:00
|
|
|
memset(&pbm, 0, sizeof(pbm));
|
|
|
|
pbm.pbm_sel = sc->psc_sel;
|
|
|
|
pbm.pbm_flags = PCIIO_BAR_MMAP_RW;
|
2022-01-04 19:02:55 +00:00
|
|
|
pbm.pbm_reg = PCIR_BAR(pi->pi_msix.table_bar);
|
2021-10-09 15:36:19 +00:00
|
|
|
pbm.pbm_memattr = VM_MEMATTR_DEVICE;
|
|
|
|
|
|
|
|
if (ioctl(pcifd, PCIOCBARMMAP, &pbm) != 0) {
|
|
|
|
warn("Failed to map MSI-X table BAR on %d/%d/%d", b, s, f);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
assert(pbm.pbm_bar_off == 0);
|
|
|
|
pi->pi_msix.mapped_addr = (uint8_t *)(uintptr_t)pbm.pbm_map_base;
|
|
|
|
pi->pi_msix.mapped_size = pbm.pbm_map_length;
|
|
|
|
|
2014-02-18 19:00:15 +00:00
|
|
|
table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
|
2012-10-19 18:11:17 +00:00
|
|
|
|
2014-02-18 19:00:15 +00:00
|
|
|
table_size = pi->pi_msix.table_offset - table_offset;
|
|
|
|
table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
|
2013-02-01 03:49:09 +00:00
|
|
|
table_size = roundup2(table_size, 4096);
|
|
|
|
|
2021-10-09 15:36:19 +00:00
|
|
|
/*
|
2022-01-05 15:08:13 +00:00
|
|
|
* Unmap any pages not containing the table, we do not need to emulate
|
2021-10-09 15:36:19 +00:00
|
|
|
* accesses to them. Avoid releasing address space to help ensure that
|
|
|
|
* a buggy out-of-bounds access causes a crash.
|
|
|
|
*/
|
|
|
|
if (table_offset != 0)
|
|
|
|
if (mprotect(pi->pi_msix.mapped_addr, table_offset,
|
|
|
|
PROT_NONE) != 0)
|
|
|
|
warn("Failed to unmap MSI-X table BAR region");
|
|
|
|
if (table_offset + table_size != pi->pi_msix.mapped_size)
|
2022-01-05 15:08:13 +00:00
|
|
|
if (mprotect(
|
|
|
|
pi->pi_msix.mapped_addr + table_offset + table_size,
|
2021-10-09 15:36:19 +00:00
|
|
|
pi->pi_msix.mapped_size - (table_offset + table_size),
|
|
|
|
PROT_NONE) != 0)
|
|
|
|
warn("Failed to unmap MSI-X table BAR region");
|
2014-02-18 19:00:15 +00:00
|
|
|
|
2013-02-01 03:49:09 +00:00
|
|
|
return (0);
|
2012-04-28 16:28:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
2023-01-19 18:30:18 +00:00
|
|
|
cfginitbar(struct passthru_softc *sc)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
int i, error;
|
|
|
|
struct pci_devinst *pi;
|
|
|
|
struct pci_bar_io bar;
|
|
|
|
enum pcibar_type bartype;
|
2014-02-18 19:00:15 +00:00
|
|
|
uint64_t base, size;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
pi = sc->psc_pi;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize BAR registers
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= PCI_BARMAX; i++) {
|
|
|
|
bzero(&bar, sizeof(bar));
|
|
|
|
bar.pbi_sel = sc->psc_sel;
|
|
|
|
bar.pbi_reg = PCIR_BAR(i);
|
|
|
|
|
|
|
|
if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (PCI_BAR_IO(bar.pbi_base)) {
|
|
|
|
bartype = PCIBAR_IO;
|
|
|
|
base = bar.pbi_base & PCIM_BAR_IO_BASE;
|
|
|
|
} else {
|
|
|
|
switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
|
|
|
|
case PCIM_BAR_MEM_64:
|
|
|
|
bartype = PCIBAR_MEM64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bartype = PCIBAR_MEM32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
base = bar.pbi_base & PCIM_BAR_MEM_BASE;
|
|
|
|
}
|
2014-02-18 19:00:15 +00:00
|
|
|
size = bar.pbi_length;
|
|
|
|
|
|
|
|
if (bartype != PCIBAR_IO) {
|
|
|
|
if (((base | size) & PAGE_MASK) != 0) {
|
2016-04-19 20:43:05 +00:00
|
|
|
warnx("passthru device %d/%d/%d BAR %d: "
|
2014-02-18 19:00:15 +00:00
|
|
|
"base %#lx or size %#lx not page aligned\n",
|
|
|
|
sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, i, base, size);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/* Cache information about the "real" BAR */
|
|
|
|
sc->psc_bar[i].type = bartype;
|
2014-02-18 19:00:15 +00:00
|
|
|
sc->psc_bar[i].size = size;
|
2011-05-13 04:54:01 +00:00
|
|
|
sc->psc_bar[i].addr = base;
|
2021-11-18 15:25:09 +00:00
|
|
|
sc->psc_bar[i].lobits = 0;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/* Allocate the BAR in the guest I/O or MMIO space */
|
2020-11-12 02:52:01 +00:00
|
|
|
error = pci_emul_alloc_bar(pi, i, bartype, size);
|
2011-05-13 04:54:01 +00:00
|
|
|
if (error)
|
|
|
|
return (-1);
|
|
|
|
|
2021-11-18 15:25:09 +00:00
|
|
|
/* Use same lobits as physical bar */
|
|
|
|
uint8_t lobits = read_config(&sc->psc_sel, PCIR_BAR(i), 0x01);
|
|
|
|
if (bartype == PCIBAR_MEM32 || bartype == PCIBAR_MEM64) {
|
|
|
|
lobits &= ~PCIM_BAR_MEM_BASE;
|
|
|
|
} else {
|
|
|
|
lobits &= ~PCIM_BAR_IO_BASE;
|
|
|
|
}
|
|
|
|
sc->psc_bar[i].lobits = lobits;
|
|
|
|
pi->pi_bar[i].lobits = lobits;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* 64-bit BAR takes up two slots so skip the next one.
|
|
|
|
*/
|
|
|
|
if (bartype == PCIBAR_MEM64) {
|
|
|
|
i++;
|
|
|
|
assert(i <= PCI_BARMAX);
|
|
|
|
sc->psc_bar[i].type = PCIBAR_MEMHI64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-01-19 18:30:18 +00:00
|
|
|
cfginit(struct pci_devinst *pi, int bus, int slot, int func)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct passthru_softc *sc;
|
2021-09-07 11:42:25 +00:00
|
|
|
uint8_t intline, intpin;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
error = 1;
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
|
|
|
|
bzero(&sc->psc_sel, sizeof(struct pcisel));
|
|
|
|
sc->psc_sel.pc_bus = bus;
|
|
|
|
sc->psc_sel.pc_dev = slot;
|
|
|
|
sc->psc_sel.pc_func = func;
|
|
|
|
|
2021-09-07 11:42:25 +00:00
|
|
|
/*
|
|
|
|
* Copy physical PCI header to virtual config space. INTLINE and INTPIN
|
|
|
|
* shouldn't be aligned with their physical value and they are already set by
|
|
|
|
* pci_emul_init().
|
|
|
|
*/
|
|
|
|
intline = pci_get_cfgdata8(pi, PCIR_INTLINE);
|
|
|
|
intpin = pci_get_cfgdata8(pi, PCIR_INTPIN);
|
|
|
|
for (int i = 0; i <= PCIR_MAXLAT; i += 4) {
|
|
|
|
pci_set_cfgdata32(pi, i, read_config(&sc->psc_sel, i, 4));
|
|
|
|
}
|
|
|
|
pci_set_cfgdata8(pi, PCIR_INTLINE, intline);
|
|
|
|
pci_set_cfgdata8(pi, PCIR_INTPIN, intpin);
|
|
|
|
|
2016-04-19 20:43:05 +00:00
|
|
|
if (cfginitmsi(sc) != 0) {
|
|
|
|
warnx("failed to initialize MSI for PCI %d/%d/%d",
|
|
|
|
bus, slot, func);
|
2012-04-28 16:28:00 +00:00
|
|
|
goto done;
|
2016-04-19 20:43:05 +00:00
|
|
|
}
|
2012-04-28 16:28:00 +00:00
|
|
|
|
2023-01-19 18:30:18 +00:00
|
|
|
if (cfginitbar(sc) != 0) {
|
2016-04-19 20:43:05 +00:00
|
|
|
warnx("failed to initialize BARs for PCI %d/%d/%d",
|
|
|
|
bus, slot, func);
|
2011-05-13 04:54:01 +00:00
|
|
|
goto done;
|
2016-04-19 20:43:05 +00:00
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2021-11-22 15:26:03 +00:00
|
|
|
write_config(&sc->psc_sel, PCIR_COMMAND, 2,
|
|
|
|
pci_get_cfgdata16(pi, PCIR_COMMAND));
|
2019-06-07 15:53:27 +00:00
|
|
|
|
2021-12-23 14:59:49 +00:00
|
|
|
/*
|
|
|
|
* We need to do this after PCIR_COMMAND got possibly updated, e.g.,
|
|
|
|
* a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us.
|
|
|
|
*/
|
2022-01-03 14:48:10 +00:00
|
|
|
if (pci_msix_table_bar(pi) >= 0) {
|
2023-01-19 18:30:18 +00:00
|
|
|
error = init_msix_table(sc);
|
2022-01-03 14:48:10 +00:00
|
|
|
if (error != 0) {
|
|
|
|
warnx(
|
|
|
|
"failed to initialize MSI-X table for PCI %d/%d/%d: %d",
|
|
|
|
bus, slot, func, error);
|
|
|
|
goto done;
|
|
|
|
}
|
2021-12-23 14:59:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:48:10 +00:00
|
|
|
error = 0; /* success */
|
2011-05-13 04:54:01 +00:00
|
|
|
done:
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2023-05-10 10:22:33 +00:00
|
|
|
struct passthru_mmio_mapping *
|
|
|
|
passthru_get_mmio(struct passthru_softc *sc, int num)
|
|
|
|
{
|
|
|
|
assert(sc != NULL);
|
|
|
|
assert(num < PASSTHRU_MMIO_MAX);
|
|
|
|
|
|
|
|
return (&sc->psc_mmio_map[num]);
|
|
|
|
}
|
|
|
|
|
2023-05-10 10:19:49 +00:00
|
|
|
struct pcisel *
|
|
|
|
passthru_get_sel(struct passthru_softc *sc)
|
|
|
|
{
|
|
|
|
assert(sc != NULL);
|
|
|
|
|
|
|
|
return (&sc->psc_sel);
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:48:34 +00:00
|
|
|
int
|
|
|
|
set_pcir_handler(struct passthru_softc *sc, int reg, int len,
|
|
|
|
cfgread_handler rhandler, cfgwrite_handler whandler)
|
|
|
|
{
|
|
|
|
if (reg > PCI_REGMAX || reg + len > PCI_REGMAX + 1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
for (int i = reg; i < reg + len; ++i) {
|
|
|
|
assert(sc->psc_pcir_rhandler[i] == NULL || rhandler == NULL);
|
|
|
|
assert(sc->psc_pcir_whandler[i] == NULL || whandler == NULL);
|
|
|
|
sc->psc_pcir_rhandler[i] = rhandler;
|
|
|
|
sc->psc_pcir_whandler[i] = whandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
2019-06-26 20:30:41 +00:00
|
|
|
passthru_legacy_config(nvlist_t *nvl, const char *opts)
|
|
|
|
{
|
2022-08-19 21:55:29 +00:00
|
|
|
const char *cp;
|
|
|
|
char *tofree;
|
2019-06-26 20:30:41 +00:00
|
|
|
char value[16];
|
|
|
|
int bus, slot, func;
|
|
|
|
|
|
|
|
if (opts == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2022-08-19 21:55:29 +00:00
|
|
|
cp = strchr(opts, ',');
|
|
|
|
|
|
|
|
if (strncmp(opts, "ppt", strlen("ppt")) == 0) {
|
|
|
|
tofree = strndup(opts, cp - opts);
|
|
|
|
set_config_value_node(nvl, "pptdev", tofree);
|
|
|
|
free(tofree);
|
|
|
|
} else if (sscanf(opts, "pci0:%d:%d:%d", &bus, &slot, &func) == 3 ||
|
|
|
|
sscanf(opts, "pci%d:%d:%d", &bus, &slot, &func) == 3 ||
|
|
|
|
sscanf(opts, "%d/%d/%d", &bus, &slot, &func) == 3) {
|
|
|
|
snprintf(value, sizeof(value), "%d", bus);
|
|
|
|
set_config_value_node(nvl, "bus", value);
|
|
|
|
snprintf(value, sizeof(value), "%d", slot);
|
|
|
|
set_config_value_node(nvl, "slot", value);
|
|
|
|
snprintf(value, sizeof(value), "%d", func);
|
|
|
|
set_config_value_node(nvl, "func", value);
|
|
|
|
} else {
|
2019-06-26 20:30:41 +00:00
|
|
|
EPRINTLN("passthru: invalid options \"%s\"", opts);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:55:29 +00:00
|
|
|
if (cp == NULL) {
|
2022-04-01 08:20:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:55:29 +00:00
|
|
|
return (pci_parse_legacy_config(nvl, cp + 1));
|
2022-03-10 10:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_init_rom(struct passthru_softc *const sc, const char *const romfile)
|
2022-03-10 10:28:06 +00:00
|
|
|
{
|
|
|
|
if (romfile == NULL) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int fd = open(romfile, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
warnx("%s: can't open romfile \"%s\"", __func__, romfile);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sbuf;
|
|
|
|
if (fstat(fd, &sbuf) < 0) {
|
|
|
|
warnx("%s: can't fstat romfile \"%s\"", __func__, romfile);
|
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
const uint64_t rom_size = sbuf.st_size;
|
|
|
|
|
|
|
|
void *const rom_data = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, fd,
|
|
|
|
0);
|
|
|
|
if (rom_data == MAP_FAILED) {
|
|
|
|
warnx("%s: unable to mmap romfile \"%s\" (%d)", __func__,
|
|
|
|
romfile, errno);
|
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *rom_addr;
|
|
|
|
int error = pci_emul_alloc_rom(sc->psc_pi, rom_size, &rom_addr);
|
|
|
|
if (error) {
|
|
|
|
warnx("%s: failed to alloc rom segment", __func__);
|
|
|
|
munmap(rom_data, rom_size);
|
|
|
|
close(fd);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
memcpy(rom_addr, rom_data, rom_size);
|
|
|
|
|
|
|
|
sc->psc_bar[PCI_ROM_IDX].type = PCIBAR_ROM;
|
|
|
|
sc->psc_bar[PCI_ROM_IDX].addr = (uint64_t)rom_addr;
|
|
|
|
sc->psc_bar[PCI_ROM_IDX].size = rom_size;
|
|
|
|
|
|
|
|
munmap(rom_data, rom_size);
|
|
|
|
close(fd);
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:55:29 +00:00
|
|
|
static bool
|
|
|
|
passthru_lookup_pptdev(const char *name, int *bus, int *slot, int *func)
|
|
|
|
{
|
|
|
|
struct pci_conf_io pc;
|
|
|
|
struct pci_conf conf[1];
|
|
|
|
struct pci_match_conf patterns[1];
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
bzero(&pc, sizeof(struct pci_conf_io));
|
|
|
|
pc.match_buf_len = sizeof(conf);
|
|
|
|
pc.matches = conf;
|
|
|
|
|
|
|
|
bzero(&patterns, sizeof(patterns));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The pattern structure requires the unit to be split out from
|
|
|
|
* the driver name. Walk backwards from the end of the name to
|
|
|
|
* find the start of the unit.
|
|
|
|
*/
|
|
|
|
cp = strchr(name, '\0');
|
|
|
|
assert(cp != NULL);
|
|
|
|
while (cp != name && isdigit(cp[-1]))
|
|
|
|
cp--;
|
|
|
|
if (cp == name || !isdigit(*cp)) {
|
|
|
|
EPRINTLN("Invalid passthru device name %s", name);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name)) {
|
|
|
|
EPRINTLN("Passthru device name %s is too long", name);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
memcpy(patterns[0].pd_name, name, cp - name);
|
|
|
|
patterns[0].pd_unit = strtol(cp, &cp, 10);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
EPRINTLN("Invalid passthru device name %s", name);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
|
|
|
|
pc.num_patterns = 1;
|
|
|
|
pc.pat_buf_len = sizeof(patterns);
|
|
|
|
pc.patterns = patterns;
|
|
|
|
|
|
|
|
if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1) {
|
|
|
|
EPRINTLN("ioctl(PCIOCGETCONF): %s", strerror(errno));
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
if (pc.status != PCI_GETCONF_LAST_DEVICE &&
|
|
|
|
pc.status != PCI_GETCONF_MORE_DEVS) {
|
|
|
|
EPRINTLN("error returned from PCIOCGETCONF ioctl");
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
if (pc.num_matches == 0) {
|
|
|
|
EPRINTLN("Passthru device %s not found", name);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf[0].pc_sel.pc_domain != 0) {
|
|
|
|
EPRINTLN("Passthru device %s on unsupported domain", name);
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
*bus = conf[0].pc_sel.pc_bus;
|
|
|
|
*slot = conf[0].pc_sel.pc_dev;
|
|
|
|
*func = conf[0].pc_sel.pc_func;
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
static int
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2015-06-18 06:00:17 +00:00
|
|
|
int bus, slot, func, error, memflags;
|
2011-05-13 04:54:01 +00:00
|
|
|
struct passthru_softc *sc;
|
2023-05-10 10:31:50 +00:00
|
|
|
struct passthru_dev **devpp;
|
|
|
|
struct passthru_dev *devp, *dev = NULL;
|
2019-06-26 20:30:41 +00:00
|
|
|
const char *value;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
sc = NULL;
|
|
|
|
error = 1;
|
|
|
|
|
2023-01-19 18:30:18 +00:00
|
|
|
memflags = vm_get_memflags(pi->pi_vmctx);
|
2015-06-18 06:00:17 +00:00
|
|
|
if (!(memflags & VM_MEM_F_WIRED)) {
|
2016-04-19 20:43:05 +00:00
|
|
|
warnx("passthru requires guest memory to be wired");
|
2019-07-12 18:33:58 +00:00
|
|
|
return (error);
|
2015-06-18 06:00:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 10:26:19 +00:00
|
|
|
if (pcifd < 0 && pcifd_init()) {
|
|
|
|
return (error);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
#define GET_INT_CONFIG(var, name) do { \
|
|
|
|
value = get_config_value_node(nvl, name); \
|
|
|
|
if (value == NULL) { \
|
|
|
|
EPRINTLN("passthru: missing required %s setting", name); \
|
|
|
|
return (error); \
|
|
|
|
} \
|
|
|
|
var = atoi(value); \
|
|
|
|
} while (0)
|
|
|
|
|
2022-08-19 21:55:29 +00:00
|
|
|
value = get_config_value_node(nvl, "pptdev");
|
|
|
|
if (value != NULL) {
|
|
|
|
if (!passthru_lookup_pptdev(value, &bus, &slot, &func))
|
|
|
|
return (error);
|
|
|
|
} else {
|
|
|
|
GET_INT_CONFIG(bus, "bus");
|
|
|
|
GET_INT_CONFIG(slot, "slot");
|
|
|
|
GET_INT_CONFIG(func, "func");
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_assign_pptdev(pi->pi_vmctx, bus, slot, func) != 0) {
|
2016-04-19 20:43:05 +00:00
|
|
|
warnx("PCI device at %d/%d/%d is not using the ppt(4) driver",
|
|
|
|
bus, slot, func);
|
2011-05-13 04:54:01 +00:00
|
|
|
goto done;
|
2016-04-19 20:43:05 +00:00
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-04-22 18:55:21 +00:00
|
|
|
sc = calloc(1, sizeof(struct passthru_softc));
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
pi->pi_arg = sc;
|
|
|
|
sc->psc_pi = pi;
|
|
|
|
|
|
|
|
/* initialize config space */
|
2023-01-19 18:30:18 +00:00
|
|
|
if ((error = cfginit(pi, bus, slot, func)) != 0)
|
2022-03-10 10:28:06 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* initialize ROM */
|
2023-01-19 18:30:18 +00:00
|
|
|
if ((error = passthru_init_rom(sc,
|
2022-03-10 10:28:06 +00:00
|
|
|
get_config_value_node(nvl, "rom"))) != 0)
|
|
|
|
goto done;
|
|
|
|
|
2021-09-07 11:42:25 +00:00
|
|
|
/* Emulate most PCI header register. */
|
|
|
|
if ((error = set_pcir_handler(sc, 0, PCIR_MAXLAT + 1,
|
|
|
|
passthru_cfgread_emulate, passthru_cfgwrite_emulate)) != 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Allow access to the physical command and status register. */
|
|
|
|
if ((error = set_pcir_handler(sc, PCIR_COMMAND, 0x04, NULL, NULL)) != 0)
|
|
|
|
goto done;
|
|
|
|
|
2023-05-10 10:31:50 +00:00
|
|
|
SET_FOREACH(devpp, passthru_dev_set) {
|
|
|
|
devp = *devpp;
|
|
|
|
assert(devp->probe != NULL);
|
|
|
|
if (devp->probe(pi) == 0) {
|
|
|
|
dev = devp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev != NULL) {
|
|
|
|
error = dev->init(pi, nvl);
|
|
|
|
if (error != 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2022-03-10 10:28:06 +00:00
|
|
|
error = 0; /* success */
|
2011-05-13 04:54:01 +00:00
|
|
|
done:
|
|
|
|
if (error) {
|
2023-05-10 10:31:50 +00:00
|
|
|
if (dev != NULL)
|
|
|
|
dev->deinit(pi);
|
2011-05-13 04:54:01 +00:00
|
|
|
free(sc);
|
2023-01-19 18:30:18 +00:00
|
|
|
vm_unassign_pptdev(pi->pi_vmctx, bus, slot, func);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
msicap_access(struct passthru_softc *sc, int coff)
|
|
|
|
{
|
|
|
|
int caplen;
|
|
|
|
|
|
|
|
if (sc->psc_msi.capoff == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
caplen = msi_caplen(sc->psc_msi.msgctrl);
|
|
|
|
|
|
|
|
if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-12-26 07:52:38 +00:00
|
|
|
static int
|
2012-04-28 16:28:00 +00:00
|
|
|
msixcap_access(struct passthru_softc *sc, int coff)
|
|
|
|
{
|
2021-12-26 07:52:38 +00:00
|
|
|
if (sc->psc_msix.capoff == 0)
|
2012-04-28 16:28:00 +00:00
|
|
|
return (0);
|
|
|
|
|
2021-12-26 07:52:38 +00:00
|
|
|
return (coff >= sc->psc_msix.capoff &&
|
2012-04-28 16:28:00 +00:00
|
|
|
coff < sc->psc_msix.capoff + MSIX_CAPLEN);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
2021-03-19 12:48:34 +00:00
|
|
|
passthru_cfgread_default(struct passthru_softc *sc,
|
|
|
|
struct pci_devinst *pi __unused, int coff, int bytes, uint32_t *rv)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
/*
|
2021-09-07 11:42:25 +00:00
|
|
|
* MSI capability is emulated.
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
2021-09-07 11:42:25 +00:00
|
|
|
if (msicap_access(sc, coff) || msixcap_access(sc, coff))
|
2011-05-13 04:54:01 +00:00
|
|
|
return (-1);
|
|
|
|
|
2019-12-11 23:41:39 +00:00
|
|
|
/*
|
|
|
|
* Emulate the command register. If a single read reads both the
|
|
|
|
* command and status registers, read the status register from the
|
|
|
|
* device's config space.
|
|
|
|
*/
|
|
|
|
if (coff == PCIR_COMMAND) {
|
|
|
|
if (bytes <= 2)
|
|
|
|
return (-1);
|
2020-05-25 06:25:31 +00:00
|
|
|
*rv = read_config(&sc->psc_sel, PCIR_STATUS, 2) << 16 |
|
|
|
|
pci_get_cfgdata16(pi, PCIR_COMMAND);
|
2019-12-11 23:41:39 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/* Everything else just read from the device's config space */
|
|
|
|
*rv = read_config(&sc->psc_sel, coff, bytes);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:42:25 +00:00
|
|
|
int
|
|
|
|
passthru_cfgread_emulate(struct passthru_softc *sc __unused,
|
|
|
|
struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
|
|
|
|
uint32_t *rv __unused)
|
|
|
|
{
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static int
|
2021-03-19 12:48:34 +00:00
|
|
|
passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
|
2021-03-19 12:48:34 +00:00
|
|
|
if (sc->psc_pcir_rhandler[coff] != NULL)
|
|
|
|
return (sc->psc_pcir_rhandler[coff](sc, pi, coff, bytes, rv));
|
|
|
|
|
|
|
|
return (passthru_cfgread_default(sc, pi, coff, bytes, rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
passthru_cfgwrite_default(struct passthru_softc *sc, struct pci_devinst *pi,
|
|
|
|
int coff, int bytes, uint32_t val)
|
|
|
|
{
|
|
|
|
int error, msix_table_entries, i;
|
|
|
|
uint16_t cmd_old;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* MSI capability is emulated
|
|
|
|
*/
|
|
|
|
if (msicap_access(sc, coff)) {
|
2020-05-25 06:25:31 +00:00
|
|
|
pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff,
|
|
|
|
PCIY_MSI);
|
2023-03-24 18:49:06 +00:00
|
|
|
error = vm_setup_pptdev_msi(pi->pi_vmctx, sc->psc_sel.pc_bus,
|
2013-12-16 19:59:31 +00:00
|
|
|
sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
|
|
|
|
pi->pi_msi.addr, pi->pi_msi.msg_data,
|
|
|
|
pi->pi_msi.maxmsgnum);
|
2016-04-19 20:43:05 +00:00
|
|
|
if (error != 0)
|
|
|
|
err(1, "vm_setup_pptdev_msi");
|
2011-05-13 04:54:01 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-04-28 16:28:00 +00:00
|
|
|
if (msixcap_access(sc, coff)) {
|
2020-05-25 06:25:31 +00:00
|
|
|
pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msix.capoff,
|
|
|
|
PCIY_MSIX);
|
2012-04-28 16:28:00 +00:00
|
|
|
if (pi->pi_msix.enabled) {
|
|
|
|
msix_table_entries = pi->pi_msix.table_count;
|
|
|
|
for (i = 0; i < msix_table_entries; i++) {
|
2023-03-24 18:49:06 +00:00
|
|
|
error = vm_setup_pptdev_msix(pi->pi_vmctx,
|
2021-12-26 07:52:38 +00:00
|
|
|
sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, i,
|
2013-12-16 19:59:31 +00:00
|
|
|
pi->pi_msix.table[i].addr,
|
|
|
|
pi->pi_msix.table[i].msg_data,
|
|
|
|
pi->pi_msix.table[i].vector_control);
|
2021-12-26 07:52:38 +00:00
|
|
|
|
2016-04-19 20:43:05 +00:00
|
|
|
if (error)
|
|
|
|
err(1, "vm_setup_pptdev_msix");
|
2012-04-28 16:28:00 +00:00
|
|
|
}
|
2020-11-24 23:18:52 +00:00
|
|
|
} else {
|
2023-01-19 18:30:18 +00:00
|
|
|
error = vm_disable_pptdev_msix(pi->pi_vmctx,
|
|
|
|
sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func);
|
2020-11-24 23:18:52 +00:00
|
|
|
if (error)
|
|
|
|
err(1, "vm_disable_pptdev_msix");
|
2012-04-28 16:28:00 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#ifdef LEGACY_SUPPORT
|
|
|
|
/*
|
|
|
|
* If this device does not support MSI natively then we cannot let
|
|
|
|
* the guest disable legacy interrupts from the device. It is the
|
|
|
|
* legacy interrupt that is triggering the virtual MSI to the guest.
|
|
|
|
*/
|
|
|
|
if (sc->psc_msi.emulated && pci_msi_enabled(pi)) {
|
|
|
|
if (coff == PCIR_COMMAND && bytes == 2)
|
|
|
|
val &= ~PCIM_CMD_INTxDIS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
write_config(&sc->psc_sel, coff, bytes, val);
|
2019-06-07 15:53:27 +00:00
|
|
|
if (coff == PCIR_COMMAND) {
|
|
|
|
cmd_old = pci_get_cfgdata16(pi, PCIR_COMMAND);
|
|
|
|
if (bytes == 1)
|
|
|
|
pci_set_cfgdata8(pi, PCIR_COMMAND, val);
|
|
|
|
else if (bytes == 2)
|
|
|
|
pci_set_cfgdata16(pi, PCIR_COMMAND, val);
|
|
|
|
pci_emul_cmd_changed(pi, cmd_old);
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-09-07 11:42:25 +00:00
|
|
|
int
|
|
|
|
passthru_cfgwrite_emulate(struct passthru_softc *sc __unused,
|
|
|
|
struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
|
|
|
|
uint32_t val __unused)
|
|
|
|
{
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:48:34 +00:00
|
|
|
static int
|
|
|
|
passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
|
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
|
|
|
|
if (sc->psc_pcir_whandler[coff] != NULL)
|
|
|
|
return (sc->psc_pcir_whandler[coff](sc, pi, coff, bytes, val));
|
|
|
|
|
|
|
|
return (passthru_cfgwrite_default(sc, pi, coff, bytes, val));
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static void
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
|
|
|
|
uint64_t value)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
2021-08-14 14:42:34 +00:00
|
|
|
struct pci_bar_ioreq pio;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
|
2013-02-01 02:41:47 +00:00
|
|
|
if (baridx == pci_msix_table_bar(pi)) {
|
2023-01-19 18:30:18 +00:00
|
|
|
msix_table_write(sc, offset, size, value);
|
2012-10-19 18:11:17 +00:00
|
|
|
} else {
|
|
|
|
assert(pi->pi_bar[baridx].type == PCIBAR_IO);
|
2021-08-14 14:42:34 +00:00
|
|
|
assert(size == 1 || size == 2 || size == 4);
|
|
|
|
assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
|
|
|
|
|
|
|
|
bzero(&pio, sizeof(pio));
|
|
|
|
pio.pbi_sel = sc->psc_sel;
|
|
|
|
pio.pbi_op = PCIBARIO_WRITE;
|
|
|
|
pio.pbi_bar = baridx;
|
|
|
|
pio.pbi_offset = (uint32_t)offset;
|
|
|
|
pio.pbi_width = size;
|
|
|
|
pio.pbi_value = (uint32_t)value;
|
|
|
|
|
|
|
|
(void)ioctl(pcifd, PCIOCBARIO, &pio);
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
static uint64_t
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
2021-08-14 14:42:34 +00:00
|
|
|
struct pci_bar_ioreq pio;
|
2012-10-19 18:11:17 +00:00
|
|
|
uint64_t val;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
|
2013-02-01 02:41:47 +00:00
|
|
|
if (baridx == pci_msix_table_bar(pi)) {
|
2012-10-19 18:11:17 +00:00
|
|
|
val = msix_table_read(sc, offset, size);
|
|
|
|
} else {
|
|
|
|
assert(pi->pi_bar[baridx].type == PCIBAR_IO);
|
2021-08-14 14:42:34 +00:00
|
|
|
assert(size == 1 || size == 2 || size == 4);
|
|
|
|
assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
|
|
|
|
|
|
|
|
bzero(&pio, sizeof(pio));
|
|
|
|
pio.pbi_sel = sc->psc_sel;
|
|
|
|
pio.pbi_op = PCIBARIO_READ;
|
|
|
|
pio.pbi_bar = baridx;
|
|
|
|
pio.pbi_offset = (uint32_t)offset;
|
|
|
|
pio.pbi_width = size;
|
2012-10-19 18:11:17 +00:00
|
|
|
|
2021-08-14 14:42:34 +00:00
|
|
|
(void)ioctl(pcifd, PCIOCBARIO, &pio);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2021-08-14 14:42:34 +00:00
|
|
|
val = pio.pbi_value;
|
2012-10-19 18:11:17 +00:00
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2012-10-19 18:11:17 +00:00
|
|
|
return (val);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:08:52 +00:00
|
|
|
static void
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_msix_addr(struct pci_devinst *pi, int baridx, int enabled,
|
|
|
|
uint64_t address)
|
2021-03-18 16:08:52 +00:00
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
|
|
|
size_t remaining;
|
|
|
|
uint32_t table_size, table_offset;
|
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
|
|
|
|
if (table_offset > 0) {
|
|
|
|
if (!enabled) {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_unmap_pptdev_mmio(pi->pi_vmctx,
|
|
|
|
sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
table_offset) != 0)
|
|
|
|
warnx("pci_passthru: unmap_pptdev_mmio failed");
|
|
|
|
} else {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
table_offset,
|
|
|
|
sc->psc_bar[baridx].addr) != 0)
|
|
|
|
warnx("pci_passthru: map_pptdev_mmio failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
table_size = pi->pi_msix.table_offset - table_offset;
|
|
|
|
table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
|
|
|
|
table_size = roundup2(table_size, 4096);
|
|
|
|
remaining = pi->pi_bar[baridx].size - table_offset - table_size;
|
|
|
|
if (remaining > 0) {
|
|
|
|
address += table_offset + table_size;
|
|
|
|
if (!enabled) {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_unmap_pptdev_mmio(pi->pi_vmctx,
|
|
|
|
sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
remaining) != 0)
|
|
|
|
warnx("pci_passthru: unmap_pptdev_mmio failed");
|
|
|
|
} else {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
remaining,
|
|
|
|
sc->psc_bar[baridx].addr +
|
|
|
|
table_offset + table_size) != 0)
|
|
|
|
warnx("pci_passthru: map_pptdev_mmio failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_mmio_addr(struct pci_devinst *pi, int baridx, int enabled,
|
|
|
|
uint64_t address)
|
2021-03-18 16:08:52 +00:00
|
|
|
{
|
|
|
|
struct passthru_softc *sc;
|
|
|
|
|
|
|
|
sc = pi->pi_arg;
|
|
|
|
if (!enabled) {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_unmap_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
sc->psc_bar[baridx].size) != 0)
|
|
|
|
warnx("pci_passthru: unmap_pptdev_mmio failed");
|
|
|
|
} else {
|
2023-01-19 18:30:18 +00:00
|
|
|
if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
|
2021-03-18 16:08:52 +00:00
|
|
|
sc->psc_sel.pc_dev,
|
|
|
|
sc->psc_sel.pc_func, address,
|
|
|
|
sc->psc_bar[baridx].size,
|
|
|
|
sc->psc_bar[baridx].addr) != 0)
|
|
|
|
warnx("pci_passthru: map_pptdev_mmio failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-03-10 10:28:06 +00:00
|
|
|
passthru_addr_rom(struct pci_devinst *const pi, const int idx,
|
|
|
|
const int enabled)
|
2021-03-18 16:08:52 +00:00
|
|
|
{
|
2022-03-10 10:28:06 +00:00
|
|
|
const uint64_t addr = pi->pi_bar[idx].addr;
|
|
|
|
const uint64_t size = pi->pi_bar[idx].size;
|
2021-03-18 16:08:52 +00:00
|
|
|
|
2022-03-10 10:28:06 +00:00
|
|
|
if (!enabled) {
|
|
|
|
if (vm_munmap_memseg(pi->pi_vmctx, addr, size) != 0) {
|
|
|
|
errx(4, "%s: munmap_memseg @ [%016lx - %016lx] failed",
|
|
|
|
__func__, addr, addr + size);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (vm_mmap_memseg(pi->pi_vmctx, addr, VM_PCIROM,
|
|
|
|
pi->pi_romoffset, size, PROT_READ | PROT_EXEC) != 0) {
|
2022-07-27 14:43:01 +00:00
|
|
|
errx(4, "%s: mmap_memseg @ [%016lx - %016lx] failed",
|
2022-03-10 10:28:06 +00:00
|
|
|
__func__, addr, addr + size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_addr(struct pci_devinst *pi, int baridx, int enabled, uint64_t address)
|
2022-03-10 10:28:06 +00:00
|
|
|
{
|
|
|
|
switch (pi->pi_bar[baridx].type) {
|
|
|
|
case PCIBAR_IO:
|
|
|
|
/* IO BARs are emulated */
|
|
|
|
break;
|
|
|
|
case PCIBAR_ROM:
|
|
|
|
passthru_addr_rom(pi, baridx, enabled);
|
|
|
|
break;
|
|
|
|
case PCIBAR_MEM32:
|
|
|
|
case PCIBAR_MEM64:
|
|
|
|
if (baridx == pci_msix_table_bar(pi))
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_msix_addr(pi, baridx, enabled, address);
|
2022-03-10 10:28:06 +00:00
|
|
|
else
|
2023-01-19 18:30:18 +00:00
|
|
|
passthru_mmio_addr(pi, baridx, enabled, address);
|
2022-03-10 10:28:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(4, "%s: invalid BAR type %d", __func__,
|
|
|
|
pi->pi_bar[baridx].type);
|
|
|
|
}
|
2021-03-18 16:08:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 17:12:32 +00:00
|
|
|
static const struct pci_devemu passthru = {
|
2011-05-13 04:54:01 +00:00
|
|
|
.pe_emu = "passthru",
|
|
|
|
.pe_init = passthru_init,
|
2019-06-26 20:30:41 +00:00
|
|
|
.pe_legacy_config = passthru_legacy_config,
|
2011-05-13 04:54:01 +00:00
|
|
|
.pe_cfgwrite = passthru_cfgwrite,
|
|
|
|
.pe_cfgread = passthru_cfgread,
|
2012-10-19 18:11:17 +00:00
|
|
|
.pe_barwrite = passthru_write,
|
|
|
|
.pe_barread = passthru_read,
|
2021-03-18 16:08:52 +00:00
|
|
|
.pe_baraddr = passthru_addr,
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
PCI_EMUL_SET(passthru);
|