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>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bus.h>
|
2014-01-21 03:01:34 +00:00
|
|
|
#include <sys/sysctl.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
|
2016-08-26 20:15:22 +00:00
|
|
|
#include <machine/cpu.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <machine/md_var.h>
|
|
|
|
|
|
|
|
#include "vmm_util.h"
|
2012-10-08 23:41:26 +00:00
|
|
|
#include "vmm_mem.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "iommu.h"
|
|
|
|
|
2014-01-21 03:01:34 +00:00
|
|
|
SYSCTL_DECL(_hw_vmm);
|
|
|
|
SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW, 0, "bhyve iommu parameters");
|
|
|
|
|
|
|
|
static int iommu_avail;
|
|
|
|
SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
|
|
|
|
0, "bhyve iommu initialized?");
|
|
|
|
|
2016-08-26 20:15:22 +00:00
|
|
|
static int iommu_enable = 1;
|
|
|
|
SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, enable, CTLFLAG_RDTUN, &iommu_enable, 0,
|
|
|
|
"Enable use of I/O MMU (required for PCI passthrough).");
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static struct iommu_ops *ops;
|
|
|
|
static void *host_domain;
|
2016-09-06 20:17:54 +00:00
|
|
|
static eventhandler_tag add_tag, delete_tag;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static __inline int
|
|
|
|
IOMMU_INIT(void)
|
|
|
|
{
|
|
|
|
if (ops != NULL)
|
|
|
|
return ((*ops->init)());
|
|
|
|
else
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
IOMMU_CLEANUP(void)
|
|
|
|
{
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
(*ops->cleanup)();
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void *
|
|
|
|
IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
return ((*ops->create_domain)(maxaddr));
|
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
IOMMU_DESTROY_DOMAIN(void *dom)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
(*ops->destroy_domain)(dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline uint64_t
|
|
|
|
IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
return ((*ops->create_mapping)(domain, gpa, hpa, len));
|
|
|
|
else
|
|
|
|
return (len); /* XXX */
|
|
|
|
}
|
|
|
|
|
2012-10-08 23:41:26 +00:00
|
|
|
static __inline uint64_t
|
|
|
|
IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
return ((*ops->remove_mapping)(domain, gpa, len));
|
|
|
|
else
|
|
|
|
return (len); /* XXX */
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static __inline void
|
2014-04-01 15:54:03 +00:00
|
|
|
IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
2014-04-01 15:54:03 +00:00
|
|
|
(*ops->add_device)(domain, rid);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
2014-04-01 15:54:03 +00:00
|
|
|
IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
2014-04-01 15:54:03 +00:00
|
|
|
(*ops->remove_device)(domain, rid);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-08 23:41:26 +00:00
|
|
|
static __inline void
|
|
|
|
IOMMU_INVALIDATE_TLB(void *domain)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
(*ops->invalidate_tlb)(domain);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static __inline void
|
|
|
|
IOMMU_ENABLE(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
(*ops->enable)();
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
IOMMU_DISABLE(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ops != NULL && iommu_avail)
|
|
|
|
(*ops->disable)();
|
|
|
|
}
|
|
|
|
|
2016-09-06 20:17:54 +00:00
|
|
|
static void
|
|
|
|
iommu_pci_add(void *arg, device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Add new devices to the host domain. */
|
|
|
|
iommu_add_device(host_domain, pci_get_rid(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iommu_pci_delete(void *arg, device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
iommu_remove_device(host_domain, pci_get_rid(dev));
|
|
|
|
}
|
|
|
|
|
2016-08-26 20:15:22 +00:00
|
|
|
static void
|
2011-05-13 04:54:01 +00:00
|
|
|
iommu_init(void)
|
|
|
|
{
|
|
|
|
int error, bus, slot, func;
|
|
|
|
vm_paddr_t maxaddr;
|
|
|
|
device_t dev;
|
|
|
|
|
2016-08-26 20:15:22 +00:00
|
|
|
if (!iommu_enable)
|
|
|
|
return;
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
if (vmm_is_intel())
|
|
|
|
ops = &iommu_ops_intel;
|
|
|
|
else if (vmm_is_amd())
|
|
|
|
ops = &iommu_ops_amd;
|
|
|
|
else
|
|
|
|
ops = NULL;
|
|
|
|
|
|
|
|
error = IOMMU_INIT();
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
2014-01-21 03:01:34 +00:00
|
|
|
iommu_avail = 1;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a domain for the devices owned by the host
|
|
|
|
*/
|
2012-10-08 23:41:26 +00:00
|
|
|
maxaddr = vmm_mem_maxaddr();
|
2011-05-13 04:54:01 +00:00
|
|
|
host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
|
2016-08-26 20:15:22 +00:00
|
|
|
if (host_domain == NULL) {
|
|
|
|
printf("iommu_init: unable to create a host domain");
|
|
|
|
IOMMU_CLEANUP();
|
|
|
|
ops = NULL;
|
|
|
|
iommu_avail = 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
2012-10-08 23:41:26 +00:00
|
|
|
* Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
|
2011-05-13 04:54:01 +00:00
|
|
|
* the host
|
|
|
|
*/
|
|
|
|
iommu_create_mapping(host_domain, 0, 0, maxaddr);
|
|
|
|
|
2016-09-06 20:17:54 +00:00
|
|
|
add_tag = EVENTHANDLER_REGISTER(pci_add_device, iommu_pci_add, NULL, 0);
|
|
|
|
delete_tag = EVENTHANDLER_REGISTER(pci_delete_device, iommu_pci_delete,
|
|
|
|
NULL, 0);
|
2011-05-13 04:54:01 +00:00
|
|
|
for (bus = 0; bus <= PCI_BUSMAX; bus++) {
|
|
|
|
for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
|
|
|
|
for (func = 0; func <= PCI_FUNCMAX; func++) {
|
|
|
|
dev = pci_find_dbsf(0, bus, slot, func);
|
|
|
|
if (dev == NULL)
|
|
|
|
continue;
|
|
|
|
|
2016-09-06 18:53:17 +00:00
|
|
|
/* Everything belongs to the host domain. */
|
2014-04-01 15:54:03 +00:00
|
|
|
iommu_add_device(host_domain,
|
|
|
|
pci_get_rid(dev));
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IOMMU_ENABLE();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
iommu_cleanup(void)
|
|
|
|
{
|
2016-09-06 20:17:54 +00:00
|
|
|
|
|
|
|
if (add_tag != NULL) {
|
|
|
|
EVENTHANDLER_DEREGISTER(pci_add_device, add_tag);
|
|
|
|
add_tag = NULL;
|
|
|
|
}
|
|
|
|
if (delete_tag != NULL) {
|
|
|
|
EVENTHANDLER_DEREGISTER(pci_delete_device, delete_tag);
|
|
|
|
delete_tag = NULL;
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
IOMMU_DISABLE();
|
|
|
|
IOMMU_DESTROY_DOMAIN(host_domain);
|
|
|
|
IOMMU_CLEANUP();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
iommu_create_domain(vm_paddr_t maxaddr)
|
|
|
|
{
|
2016-08-26 20:15:22 +00:00
|
|
|
static volatile int iommu_initted;
|
|
|
|
|
|
|
|
if (iommu_initted < 2) {
|
|
|
|
if (atomic_cmpset_int(&iommu_initted, 0, 1)) {
|
|
|
|
iommu_init();
|
|
|
|
atomic_store_rel_int(&iommu_initted, 2);
|
|
|
|
} else
|
|
|
|
while (iommu_initted == 1)
|
|
|
|
cpu_spinwait();
|
|
|
|
}
|
2011-05-13 04:54:01 +00:00
|
|
|
return (IOMMU_CREATE_DOMAIN(maxaddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
iommu_destroy_domain(void *dom)
|
|
|
|
{
|
|
|
|
|
|
|
|
IOMMU_DESTROY_DOMAIN(dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t mapped, remaining;
|
|
|
|
|
|
|
|
remaining = len;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
|
|
|
|
gpa += mapped;
|
|
|
|
hpa += mapped;
|
|
|
|
remaining -= mapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 23:41:26 +00:00
|
|
|
void
|
|
|
|
iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t unmapped, remaining;
|
|
|
|
|
|
|
|
remaining = len;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
|
|
|
|
gpa += unmapped;
|
|
|
|
remaining -= unmapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
iommu_host_domain(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (host_domain);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
void
|
2014-04-01 15:54:03 +00:00
|
|
|
iommu_add_device(void *dom, uint16_t rid)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
2014-04-01 15:54:03 +00:00
|
|
|
IOMMU_ADD_DEVICE(dom, rid);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-01 15:54:03 +00:00
|
|
|
iommu_remove_device(void *dom, uint16_t rid)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
|
2014-04-01 15:54:03 +00:00
|
|
|
IOMMU_REMOVE_DEVICE(dom, rid);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
2012-10-08 23:41:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
iommu_invalidate_tlb(void *domain)
|
|
|
|
{
|
|
|
|
|
|
|
|
IOMMU_INVALIDATE_TLB(domain);
|
|
|
|
}
|