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/param.h> | |||||
#include <sys/types.h> | |||||
#include <sys/mman.h> | |||||
#include <dev/pci/pcireg.h> | |||||
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
#include <string.h> | |||||
#include <err.h> | |||||
#include <errno.h> | |||||
#include <fcntl.h> | |||||
#include <sysexits.h> | |||||
#include <unistd.h> | |||||
#include <machine/vmm.h> | |||||
#include "inout.h" | |||||
#include "pci_passthru.h" | |||||
#define KB (1024UL) | |||||
#define MB (1024 * 1024UL) | |||||
#define GB (1024 * 1024 * 1024UL) | |||||
#define VBIOS_ADDR 0xC0000 | |||||
static int | |||||
apu_d_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) | |||||
{ | |||||
int error; | |||||
struct passthru_softc *sc; | |||||
error = 0; | |||||
if ((error = passthru_init(ctx, pi, opts)) != 0) | |||||
goto done; | |||||
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); | |||||
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); | |||||
// 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; | |||||
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; | |||||
} | |||||
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); | |||||
if ((error = pci_emul_alloc_rom(pi, VM_VIDEOBIOS, pi->pi_vbios.gpa, pi->pi_vbios.len)) != 0) { | |||||
warnx("pci_emul_alloc_rom: %x", error); | |||||
goto done; | |||||
} | |||||
done: | |||||
return (error); | |||||
} | |||||
static int | |||||
apu_d_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int coff, int bytes, uint32_t val) | |||||
{ | |||||
return passthru_cfgwrite(ctx, vcpu, pi, coff, bytes, val); | |||||
} | |||||
static int | |||||
apu_d_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int coff, int bytes, uint32_t *rv) | |||||
{ | |||||
return passthru_cfgread(ctx, vcpu, pi, coff, bytes, rv); | |||||
} | |||||
static void | |||||
apu_d_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, uint64_t offset, int size, uint64_t value) | |||||
{ | |||||
passthru_write(ctx, vcpu, pi, baridx, offset, size, value); | |||||
} | |||||
static uint64_t | |||||
apu_d_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, uint64_t offset, int size) | |||||
{ | |||||
return passthru_read(ctx, vcpu, pi, baridx, offset, size); | |||||
} | |||||
struct pci_devemu apu_d = { | |||||
.pe_emu = "apu-d", | |||||
.pe_init = apu_d_init, | |||||
.pe_cfgwrite = apu_d_cfgwrite, | |||||
.pe_cfgread = apu_d_cfgread, | |||||
.pe_barwrite = apu_d_write, | |||||
.pe_barread = apu_d_read, | |||||
}; | |||||
PCI_EMUL_SET(apu_d); | |||||