Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyve/pci_apu-d.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2020 Beckhoff Automation GmbH & Co. KG | |||||
* 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 THE AUTHOR OR CONTRIBUTORS ``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 THE AUTHOR 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/types.h> | |||||
#include <sys/param.h> | |||||
#include <sys/mman.h> | |||||
#include <machine/iodev.h> | |||||
#include <machine/vmm.h> | |||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <fcntl.h> | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <string.h> | |||||
#include <sysexits.h> | |||||
#include <unistd.h> | |||||
#include "pci_passthru.h" | |||||
#define VBIOS_ADDR 0xC0000 | |||||
#define max(a, b) (((a) > (b)) ? (a) : (b)) | |||||
int | |||||
apu_d_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) | |||||
{ | |||||
int error; | |||||
struct passthru_softc *sc; | |||||
error = 0; | |||||
sc = pi->pi_arg; | |||||
uint16_t vendor, dev_id; | |||||
uint64_t bios_size; | |||||
vendor = read_config(&sc->psc_sel, PCIR_VENDOR, 0x02); | |||||
dev_id = read_config(&sc->psc_sel, PCIR_DEVICE, 0x02); | |||||
/* get VBIOS size */ | |||||
if ((error = vm_get_vbios(ctx, sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, | |||||
sc->psc_sel.pc_func, vendor, dev_id, NULL, &bios_size)) != 0) { | |||||
warnx("vm_get_vbios: %x", errno); | |||||
goto done; | |||||
} | |||||
if (bios_size == 0) { | |||||
warnx("Could not determine VBIOS size"); | |||||
goto done; | |||||
} | |||||
/* | |||||
* round up size to a power of two | |||||
* check in descendig order to avoid endless loop | |||||
*/ | |||||
pi->pi_vbios.len = 1ULL << 63; | |||||
while (pi->pi_vbios.len >= bios_size) { | |||||
pi->pi_vbios.len >>= 1; | |||||
} | |||||
pi->pi_vbios.len <<= 1; | |||||
/* ROM size should be greater than 2 KB */ | |||||
pi->pi_vbios.len = max(pi->pi_vbios.len, (~PCIM_BIOS_ADDR_MASK) + 1); | |||||
pi->pi_vbios.gpa = VBIOS_ADDR; | |||||
pi->pi_vbios.hpa = (uint64_t)vm_create_devmem( | |||||
ctx, VM_VIDEOBIOS, "videobios", pi->pi_vbios.len); | |||||
if ((void *)pi->pi_vbios.hpa == MAP_FAILED) { | |||||
warnx("vm_create_devmem: %x", errno); | |||||
error = -1; | |||||
goto done; | |||||
} | |||||
/* get VBIOS */ | |||||
if ((error = vm_get_vbios(ctx, sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, | |||||
sc->psc_sel.pc_func, vendor, dev_id, (void *)pi->pi_vbios.hpa, | |||||
&bios_size)) != 0) { | |||||
warnx("vm_get_vbios: %x", errno); | |||||
goto done; | |||||
} | |||||
/* assign a ROM to this device */ | |||||
if ((error = pci_emul_alloc_bar(pi, PCI_BARMAX + 1, PCIBAR_ROM, pi->pi_vbios.len)) != 0) { | |||||
warnx("pci_emul_alloc_rom: %x", error); | |||||
goto done; | |||||
} | |||||
done: | |||||
return (error); | |||||
} | |||||
int apu_d_addr_rom(struct pci_devinst *pi, int idx, int enabled) | |||||
{ | |||||
int error; | |||||
if (!enabled) | |||||
error = vm_munmap_memseg(pi->pi_vmctx, pi->pi_bar[idx].addr, pi->pi_bar[idx].size); | |||||
else | |||||
error = vm_mmap_memseg(pi->pi_vmctx, pi->pi_bar[idx].addr, VM_VIDEOBIOS, 0, pi->pi_bar[idx].size, PROT_READ | PROT_EXEC); | |||||
return error; | |||||
} |