bhyve: add hook for PCI header of passthru devices

Most register of the PCI header are either constant values or require
emulation anyway. The command and status register are the only exception which
require hardware access. So, we're adding an emulation handler for all
other register.

As this emulation handler will be reused by some future features like
GPU passthrough, we directly export it.

Reviewed by:		markj
MFC after:		1 week
Sponsored by:		Beckhoff Automation GmbH & Co. KG
Differential Revision:	https://reviews.freebsd.org/D33010
This commit is contained in:
Corvin Köhne 2021-09-07 13:42:25 +02:00
parent 931bb7bf1c
commit b6e67875a3
No known key found for this signature in database
GPG Key ID: D854DA56315E026A
2 changed files with 45 additions and 30 deletions

View File

@ -603,6 +603,7 @@ cfginit(struct pci_devinst *pi, int bus, int slot, int func)
{
int error;
struct passthru_softc *sc;
uint8_t intline, intpin;
error = 1;
sc = pi->pi_arg;
@ -612,6 +613,19 @@ cfginit(struct pci_devinst *pi, int bus, int slot, int func)
sc->psc_sel.pc_dev = slot;
sc->psc_sel.pc_func = func;
/*
* 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);
if (cfginitmsi(sc) != 0) {
warnx("failed to initialize MSI for PCI %d/%d/%d",
bus, slot, func);
@ -876,6 +890,15 @@ passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
get_config_value_node(nvl, "rom"))) != 0)
goto done;
/* 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;
error = 0; /* success */
done:
if (error) {
@ -885,16 +908,6 @@ passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
return (error);
}
static int
bar_access(int coff)
{
if ((coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) ||
coff == PCIR_BIOS)
return (1);
else
return (0);
}
static int
msicap_access(struct passthru_softc *sc, int coff)
{
@ -926,23 +939,11 @@ passthru_cfgread_default(struct passthru_softc *sc,
struct pci_devinst *pi __unused, int coff, int bytes, uint32_t *rv)
{
/*
* PCI BARs and MSI capability is emulated.
* MSI capability is emulated.
*/
if (bar_access(coff) || msicap_access(sc, coff) ||
msixcap_access(sc, coff))
if (msicap_access(sc, coff) || msixcap_access(sc, coff))
return (-1);
#ifdef LEGACY_SUPPORT
/*
* Emulate PCIR_CAP_PTR if this device does not support MSI capability
* natively.
*/
if (sc->psc_msi.emulated) {
if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4)
return (-1);
}
#endif
/*
* Emulate the command register. If a single read reads both the
* command and status registers, read the status register from the
@ -962,6 +963,14 @@ passthru_cfgread_default(struct passthru_softc *sc,
return (0);
}
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);
}
static int
passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv)
{
@ -982,12 +991,6 @@ passthru_cfgwrite_default(struct passthru_softc *sc, struct pci_devinst *pi,
int error, msix_table_entries, i;
uint16_t cmd_old;
/*
* PCI BARs are emulated
*/
if (bar_access(coff))
return (-1);
/*
* MSI capability is emulated
*/
@ -1054,6 +1057,14 @@ passthru_cfgwrite_default(struct passthru_softc *sc, struct pci_devinst *pi,
return (0);
}
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);
}
static int
passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
{

View File

@ -20,5 +20,9 @@ typedef int (*cfgwrite_handler)(struct passthru_softc *sc,
uint32_t read_config(const struct pcisel *sel, long reg, int width);
void write_config(const struct pcisel *sel, long reg, int width, uint32_t data);
int passthru_cfgread_emulate(struct passthru_softc *sc, struct pci_devinst *pi,
int coff, int bytes, uint32_t *rv);
int passthru_cfgwrite_emulate(struct passthru_softc *sc, struct pci_devinst *pi,
int coff, int bytes, uint32_t val);
int set_pcir_handler(struct passthru_softc *sc, int reg, int len,
cfgread_handler rhandler, cfgwrite_handler whandler);