aboutsummaryrefslogtreecommitdiff
path: root/hw/vfio/container.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw/vfio/container.c')
-rw-r--r--hw/vfio/container.c1325
1 files changed, 200 insertions, 1125 deletions
diff --git a/hw/vfio/container.c b/hw/vfio/container.c
index 030c6d3..250b20f 100644
--- a/hw/vfio/container.c
+++ b/hw/vfio/container.c
@@ -1,1275 +1,350 @@
/*
- * generic functions used by VFIO devices
+ * VFIO BASE CONTAINER
*
- * Copyright Red Hat, Inc. 2012
+ * Copyright (C) 2023 Intel Corporation.
+ * Copyright Red Hat, Inc. 2023
*
- * Authors:
- * Alex Williamson <alex.williamson@redhat.com>
+ * Authors: Yi Liu <yi.l.liu@intel.com>
+ * Eric Auger <eric.auger@redhat.com>
*
- * This work is licensed under the terms of the GNU GPL, version 2. See
- * the COPYING file in the top-level directory.
- *
- * Based on qemu-kvm device-assignment:
- * Adapted for KVM by Qumranet.
- * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
- * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
- * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
- * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
- * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
+ * SPDX-License-Identifier: GPL-2.0-or-later
*/
-#include "qemu/osdep.h"
#include <sys/ioctl.h>
#include <linux/vfio.h>
-#include "hw/vfio/vfio-device.h"
-#include "system/address-spaces.h"
-#include "system/memory.h"
+#include "qemu/osdep.h"
+#include "system/tcg.h"
#include "system/ram_addr.h"
-#include "qemu/error-report.h"
-#include "qemu/range.h"
-#include "system/reset.h"
-#include "trace.h"
#include "qapi/error.h"
-#include "migration/cpr.h"
-#include "migration/blocker.h"
-#include "pci.h"
+#include "qemu/error-report.h"
#include "hw/vfio/vfio-container.h"
+#include "hw/vfio/vfio-device.h" /* vfio_device_reset_handler */
+#include "system/reset.h"
#include "vfio-helpers.h"
-#include "vfio-listener.h"
-
-#define TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
-
-typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
-static VFIOGroupList vfio_group_list =
- QLIST_HEAD_INITIALIZER(vfio_group_list);
-
-static int vfio_ram_block_discard_disable(VFIOContainer *container, bool state)
-{
- switch (container->iommu_type) {
- case VFIO_TYPE1v2_IOMMU:
- case VFIO_TYPE1_IOMMU:
- /*
- * We support coordinated discarding of RAM via the RamDiscardManager.
- */
- return ram_block_uncoordinated_discard_disable(state);
- default:
- /*
- * VFIO_SPAPR_TCE_IOMMU most probably works just fine with
- * RamDiscardManager, however, it is completely untested.
- *
- * VFIO_SPAPR_TCE_v2_IOMMU with "DMA memory preregistering" does
- * completely the opposite of managing mapping/pinning dynamically as
- * required by RamDiscardManager. We would have to special-case sections
- * with a RamDiscardManager.
- */
- return ram_block_discard_disable(state);
- }
-}
-
-static int vfio_dma_unmap_bitmap(const VFIOContainer *container,
- hwaddr iova, ram_addr_t size,
- IOMMUTLBEntry *iotlb)
-{
- const VFIOContainerBase *bcontainer = VFIO_IOMMU(container);
- struct vfio_iommu_type1_dma_unmap *unmap;
- struct vfio_bitmap *bitmap;
- VFIOBitmap vbmap;
- int ret;
-
- ret = vfio_bitmap_alloc(&vbmap, size);
- if (ret) {
- return ret;
- }
-
- unmap = g_malloc0(sizeof(*unmap) + sizeof(*bitmap));
-
- unmap->argsz = sizeof(*unmap) + sizeof(*bitmap);
- unmap->iova = iova;
- unmap->size = size;
- unmap->flags |= VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP;
- bitmap = (struct vfio_bitmap *)&unmap->data;
-
- /*
- * cpu_physical_memory_set_dirty_lebitmap() supports pages in bitmap of
- * qemu_real_host_page_size to mark those dirty. Hence set bitmap_pgsize
- * to qemu_real_host_page_size.
- */
- bitmap->pgsize = qemu_real_host_page_size();
- bitmap->size = vbmap.size;
- bitmap->data = (__u64 *)vbmap.bitmap;
-
- if (vbmap.size > bcontainer->max_dirty_bitmap_size) {
- error_report("UNMAP: Size of bitmap too big 0x%"PRIx64, vbmap.size);
- ret = -E2BIG;
- goto unmap_exit;
- }
-
- ret = ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, unmap);
- if (!ret) {
- cpu_physical_memory_set_dirty_lebitmap(vbmap.bitmap,
- iotlb->translated_addr, vbmap.pages);
- } else {
- error_report("VFIO_UNMAP_DMA with DIRTY_BITMAP : %m");
- }
-
-unmap_exit:
- g_free(unmap);
- g_free(vbmap.bitmap);
-
- return ret;
-}
-
-static int vfio_legacy_dma_unmap_one(const VFIOContainerBase *bcontainer,
- hwaddr iova, ram_addr_t size,
- IOMMUTLBEntry *iotlb)
-{
- const VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
- struct vfio_iommu_type1_dma_unmap unmap = {
- .argsz = sizeof(unmap),
- .flags = 0,
- .iova = iova,
- .size = size,
- };
- bool need_dirty_sync = false;
- int ret;
- Error *local_err = NULL;
-
- g_assert(!cpr_is_incoming());
-
- if (iotlb && vfio_container_dirty_tracking_is_started(bcontainer)) {
- if (!vfio_container_devices_dirty_tracking_is_supported(bcontainer) &&
- bcontainer->dirty_pages_supported) {
- return vfio_dma_unmap_bitmap(container, iova, size, iotlb);
- }
- need_dirty_sync = true;
- }
-
- while (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
- /*
- * The type1 backend has an off-by-one bug in the kernel (71a7d3d78e3c
- * v4.15) where an overflow in its wrap-around check prevents us from
- * unmapping the last page of the address space. Test for the error
- * condition and re-try the unmap excluding the last page. The
- * expectation is that we've never mapped the last page anyway and this
- * unmap request comes via vIOMMU support which also makes it unlikely
- * that this page is used. This bug was introduced well after type1 v2
- * support was introduced, so we shouldn't need to test for v1. A fix
- * is queued for kernel v5.0 so this workaround can be removed once
- * affected kernels are sufficiently deprecated.
- */
- if (errno == EINVAL && unmap.size && !(unmap.iova + unmap.size) &&
- container->iommu_type == VFIO_TYPE1v2_IOMMU) {
- trace_vfio_legacy_dma_unmap_overflow_workaround();
- unmap.size -= 1ULL << ctz64(bcontainer->pgsizes);
- continue;
- }
- return -errno;
- }
-
- if (need_dirty_sync) {
- ret = vfio_container_query_dirty_bitmap(bcontainer, iova, size,
- iotlb->translated_addr, &local_err);
- if (ret) {
- error_report_err(local_err);
- return ret;
- }
- }
+#include "trace.h"
- return 0;
-}
+static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
+ QLIST_HEAD_INITIALIZER(vfio_address_spaces);
-/*
- * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86
- */
-static int vfio_legacy_dma_unmap(const VFIOContainerBase *bcontainer,
- hwaddr iova, ram_addr_t size,
- IOMMUTLBEntry *iotlb, bool unmap_all)
+VFIOAddressSpace *vfio_address_space_get(AddressSpace *as)
{
- int ret;
-
- if (unmap_all) {
- /* The unmap ioctl doesn't accept a full 64-bit span. */
- Int128 llsize = int128_rshift(int128_2_64(), 1);
-
- ret = vfio_legacy_dma_unmap_one(bcontainer, 0, int128_get64(llsize),
- iotlb);
+ VFIOAddressSpace *space;
- if (ret == 0) {
- ret = vfio_legacy_dma_unmap_one(bcontainer, int128_get64(llsize),
- int128_get64(llsize), iotlb);
+ QLIST_FOREACH(space, &vfio_address_spaces, list) {
+ if (space->as == as) {
+ return space;
}
-
- } else {
- ret = vfio_legacy_dma_unmap_one(bcontainer, iova, size, iotlb);
- }
-
- return ret;
-}
-
-static int vfio_legacy_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
- ram_addr_t size, void *vaddr, bool readonly,
- MemoryRegion *mr)
-{
- const VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
- struct vfio_iommu_type1_dma_map map = {
- .argsz = sizeof(map),
- .flags = VFIO_DMA_MAP_FLAG_READ,
- .vaddr = (__u64)(uintptr_t)vaddr,
- .iova = iova,
- .size = size,
- };
-
- if (!readonly) {
- map.flags |= VFIO_DMA_MAP_FLAG_WRITE;
- }
-
- /*
- * Try the mapping, if it fails with EBUSY, unmap the region and try
- * again. This shouldn't be necessary, but we sometimes see it in
- * the VGA ROM space.
- */
- if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 ||
- (errno == EBUSY &&
- vfio_legacy_dma_unmap(bcontainer, iova, size, NULL, false) == 0 &&
- ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) {
- return 0;
- }
-
- return -errno;
-}
-
-static int
-vfio_legacy_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
- bool start, Error **errp)
-{
- const VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
- int ret;
- struct vfio_iommu_type1_dirty_bitmap dirty = {
- .argsz = sizeof(dirty),
- };
-
- if (start) {
- dirty.flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_START;
- } else {
- dirty.flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP;
- }
-
- ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, &dirty);
- if (ret) {
- ret = -errno;
- error_setg_errno(errp, errno, "Failed to set dirty tracking flag 0x%x",
- dirty.flags);
- }
-
- return ret;
-}
-
-static int vfio_legacy_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
- VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
-{
- const VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
- struct vfio_iommu_type1_dirty_bitmap *dbitmap;
- struct vfio_iommu_type1_dirty_bitmap_get *range;
- int ret;
-
- dbitmap = g_malloc0(sizeof(*dbitmap) + sizeof(*range));
-
- dbitmap->argsz = sizeof(*dbitmap) + sizeof(*range);
- dbitmap->flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP;
- range = (struct vfio_iommu_type1_dirty_bitmap_get *)&dbitmap->data;
- range->iova = iova;
- range->size = size;
-
- /*
- * cpu_physical_memory_set_dirty_lebitmap() supports pages in bitmap of
- * qemu_real_host_page_size to mark those dirty. Hence set bitmap's pgsize
- * to qemu_real_host_page_size.
- */
- range->bitmap.pgsize = qemu_real_host_page_size();
- range->bitmap.size = vbmap->size;
- range->bitmap.data = (__u64 *)vbmap->bitmap;
-
- ret = ioctl(container->fd, VFIO_IOMMU_DIRTY_PAGES, dbitmap);
- if (ret) {
- ret = -errno;
- error_setg_errno(errp, errno,
- "Failed to get dirty bitmap for iova: 0x%"PRIx64
- " size: 0x%"PRIx64, (uint64_t)range->iova,
- (uint64_t)range->size);
- }
-
- g_free(dbitmap);
-
- return ret;
-}
-
-static bool vfio_get_info_iova_range(struct vfio_iommu_type1_info *info,
- VFIOContainerBase *bcontainer)
-{
- struct vfio_info_cap_header *hdr;
- struct vfio_iommu_type1_info_cap_iova_range *cap;
-
- hdr = vfio_get_iommu_type1_info_cap(info,
- VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE);
- if (!hdr) {
- return false;
}
- cap = (void *)hdr;
-
- for (int i = 0; i < cap->nr_iovas; i++) {
- Range *range = g_new(Range, 1);
+ /* No suitable VFIOAddressSpace, create a new one */
+ space = g_malloc0(sizeof(*space));
+ space->as = as;
+ QLIST_INIT(&space->containers);
- range_set_bounds(range, cap->iova_ranges[i].start,
- cap->iova_ranges[i].end);
- bcontainer->iova_ranges =
- range_list_insert(bcontainer->iova_ranges, range);
+ if (QLIST_EMPTY(&vfio_address_spaces)) {
+ qemu_register_reset(vfio_device_reset_handler, NULL);
}
- return true;
-}
-
-static void vfio_group_add_kvm_device(VFIOGroup *group)
-{
- Error *err = NULL;
+ QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
- if (vfio_kvm_device_add_fd(group->fd, &err)) {
- error_reportf_err(err, "group ID %d: ", group->groupid);
- }
+ return space;
}
-static void vfio_group_del_kvm_device(VFIOGroup *group)
+void vfio_address_space_put(VFIOAddressSpace *space)
{
- Error *err = NULL;
-
- if (vfio_kvm_device_del_fd(group->fd, &err)) {
- error_reportf_err(err, "group ID %d: ", group->groupid);
+ if (!QLIST_EMPTY(&space->containers)) {
+ return;
}
-}
-/*
- * vfio_get_iommu_type - selects the richest iommu_type (v2 first)
- */
-static int vfio_get_iommu_type(int container_fd,
- Error **errp)
-{
- int iommu_types[] = { VFIO_TYPE1v2_IOMMU, VFIO_TYPE1_IOMMU,
- VFIO_SPAPR_TCE_v2_IOMMU, VFIO_SPAPR_TCE_IOMMU };
- int i;
+ QLIST_REMOVE(space, list);
+ g_free(space);
- for (i = 0; i < ARRAY_SIZE(iommu_types); i++) {
- if (ioctl(container_fd, VFIO_CHECK_EXTENSION, iommu_types[i])) {
- return iommu_types[i];
- }
+ if (QLIST_EMPTY(&vfio_address_spaces)) {
+ qemu_unregister_reset(vfio_device_reset_handler, NULL);
}
- error_setg(errp, "No available IOMMU models");
- return -EINVAL;
}
-/*
- * vfio_get_iommu_ops - get a VFIOIOMMUClass associated with a type
- */
-static const char *vfio_get_iommu_class_name(int iommu_type)
+void vfio_address_space_insert(VFIOAddressSpace *space,
+ VFIOContainer *bcontainer)
{
- switch (iommu_type) {
- case VFIO_TYPE1v2_IOMMU:
- case VFIO_TYPE1_IOMMU:
- return TYPE_VFIO_IOMMU_LEGACY;
- break;
- case VFIO_SPAPR_TCE_v2_IOMMU:
- case VFIO_SPAPR_TCE_IOMMU:
- return TYPE_VFIO_IOMMU_SPAPR;
- break;
- default:
- g_assert_not_reached();
- };
+ QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
+ bcontainer->space = space;
}
-static bool vfio_set_iommu(int container_fd, int group_fd,
- int *iommu_type, Error **errp)
+int vfio_container_dma_map(VFIOContainer *bcontainer,
+ hwaddr iova, ram_addr_t size,
+ void *vaddr, bool readonly, MemoryRegion *mr)
{
- if (ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container_fd)) {
- error_setg_errno(errp, errno, "Failed to set group container");
- return false;
- }
-
- while (ioctl(container_fd, VFIO_SET_IOMMU, *iommu_type)) {
- if (*iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
- /*
- * On sPAPR, despite the IOMMU subdriver always advertises v1 and
- * v2, the running platform may not support v2 and there is no
- * way to guess it until an IOMMU group gets added to the container.
- * So in case it fails with v2, try v1 as a fallback.
- */
- *iommu_type = VFIO_SPAPR_TCE_IOMMU;
- continue;
- }
- error_setg_errno(errp, errno, "Failed to set iommu for container");
- return false;
- }
-
- return true;
-}
-
-static VFIOContainer *vfio_create_container(int fd, VFIOGroup *group,
- Error **errp)
-{
- int iommu_type;
- const char *vioc_name;
- VFIOContainer *container;
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+ RAMBlock *rb = mr->ram_block;
+ int mfd = rb ? qemu_ram_get_fd(rb) : -1;
- iommu_type = vfio_get_iommu_type(fd, errp);
- if (iommu_type < 0) {
- return NULL;
- }
+ if (mfd >= 0 && vioc->dma_map_file) {
+ unsigned long start = vaddr - qemu_ram_get_host_addr(rb);
+ unsigned long offset = qemu_ram_get_fd_offset(rb);
- /*
- * During CPR, just set the container type and skip the ioctls, as the
- * container and group are already configured in the kernel.
- */
- if (!cpr_is_incoming() &&
- !vfio_set_iommu(fd, group->fd, &iommu_type, errp)) {
- return NULL;
+ return vioc->dma_map_file(bcontainer, iova, size, mfd, start + offset,
+ readonly);
}
-
- vioc_name = vfio_get_iommu_class_name(iommu_type);
-
- container = VFIO_IOMMU_LEGACY(object_new(vioc_name));
- container->fd = fd;
- container->iommu_type = iommu_type;
- return container;
+ g_assert(vioc->dma_map);
+ return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
}
-static int vfio_get_iommu_info(VFIOContainer *container,
- struct vfio_iommu_type1_info **info)
+int vfio_container_dma_unmap(VFIOContainer *bcontainer,
+ hwaddr iova, ram_addr_t size,
+ IOMMUTLBEntry *iotlb, bool unmap_all)
{
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- size_t argsz = sizeof(struct vfio_iommu_type1_info);
-
- *info = g_new0(struct vfio_iommu_type1_info, 1);
-again:
- (*info)->argsz = argsz;
-
- if (ioctl(container->fd, VFIO_IOMMU_GET_INFO, *info)) {
- g_free(*info);
- *info = NULL;
- return -errno;
- }
-
- if (((*info)->argsz > argsz)) {
- argsz = (*info)->argsz;
- *info = g_realloc(*info, argsz);
- goto again;
- }
-
- return 0;
+ g_assert(vioc->dma_unmap);
+ return vioc->dma_unmap(bcontainer, iova, size, iotlb, unmap_all);
}
-static struct vfio_info_cap_header *
-vfio_get_iommu_info_cap(struct vfio_iommu_type1_info *info, uint16_t id)
+bool vfio_container_add_section_window(VFIOContainer *bcontainer,
+ MemoryRegionSection *section,
+ Error **errp)
{
- struct vfio_info_cap_header *hdr;
- void *ptr = info;
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) {
- return NULL;
+ if (!vioc->add_window) {
+ return true;
}
- for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
- if (hdr->id == id) {
- return hdr;
- }
- }
-
- return NULL;
+ return vioc->add_window(bcontainer, section, errp);
}
-static void vfio_get_iommu_info_migration(VFIOContainer *container,
- struct vfio_iommu_type1_info *info)
+void vfio_container_del_section_window(VFIOContainer *bcontainer,
+ MemoryRegionSection *section)
{
- struct vfio_info_cap_header *hdr;
- struct vfio_iommu_type1_info_cap_migration *cap_mig;
- VFIOContainerBase *bcontainer = VFIO_IOMMU(container);
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- hdr = vfio_get_iommu_info_cap(info, VFIO_IOMMU_TYPE1_INFO_CAP_MIGRATION);
- if (!hdr) {
+ if (!vioc->del_window) {
return;
}
- cap_mig = container_of(hdr, struct vfio_iommu_type1_info_cap_migration,
- header);
-
- /*
- * cpu_physical_memory_set_dirty_lebitmap() supports pages in bitmap of
- * qemu_real_host_page_size to mark those dirty.
- */
- if (cap_mig->pgsize_bitmap & qemu_real_host_page_size()) {
- bcontainer->dirty_pages_supported = true;
- bcontainer->max_dirty_bitmap_size = cap_mig->max_dirty_bitmap_size;
- bcontainer->dirty_pgsizes = cap_mig->pgsize_bitmap;
- }
+ return vioc->del_window(bcontainer, section);
}
-static bool vfio_legacy_setup(VFIOContainerBase *bcontainer, Error **errp)
+int vfio_container_set_dirty_page_tracking(VFIOContainer *bcontainer,
+ bool start, Error **errp)
{
- VFIOContainer *container = VFIO_IOMMU_LEGACY(bcontainer);
- g_autofree struct vfio_iommu_type1_info *info = NULL;
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
int ret;
- ret = vfio_get_iommu_info(container, &info);
- if (ret) {
- error_setg_errno(errp, -ret, "Failed to get VFIO IOMMU info");
- return false;
+ if (!bcontainer->dirty_pages_supported) {
+ return 0;
}
- if (info->flags & VFIO_IOMMU_INFO_PGSIZES) {
- bcontainer->pgsizes = info->iova_pgsizes;
- } else {
- bcontainer->pgsizes = qemu_real_host_page_size();
+ g_assert(vioc->set_dirty_page_tracking);
+ if (bcontainer->dirty_pages_started == start) {
+ return 0;
}
- if (!vfio_get_info_dma_avail(info, &bcontainer->dma_max_mappings)) {
- bcontainer->dma_max_mappings = 65535;
+ ret = vioc->set_dirty_page_tracking(bcontainer, start, errp);
+ if (!ret) {
+ bcontainer->dirty_pages_started = start;
}
- vfio_get_info_iova_range(info, bcontainer);
-
- vfio_get_iommu_info_migration(container, info);
- return true;
+ return ret;
}
-static bool vfio_container_attach_discard_disable(VFIOContainer *container,
- VFIOGroup *group, Error **errp)
+static bool vfio_container_devices_dirty_tracking_is_started(
+ const VFIOContainer *bcontainer)
{
- int ret;
+ VFIODevice *vbasedev;
- /*
- * VFIO is currently incompatible with discarding of RAM insofar as the
- * madvise to purge (zap) the page from QEMU's address space does not
- * interact with the memory API and therefore leaves stale virtual to
- * physical mappings in the IOMMU if the page was previously pinned. We
- * therefore set discarding broken for each group added to a container,
- * whether the container is used individually or shared. This provides
- * us with options to allow devices within a group to opt-in and allow
- * discarding, so long as it is done consistently for a group (for instance
- * if the device is an mdev device where it is known that the host vendor
- * driver will never pin pages outside of the working set of the guest
- * driver, which would thus not be discarding candidates).
- *
- * The first opportunity to induce pinning occurs here where we attempt to
- * attach the group to existing containers within the AddressSpace. If any
- * pages are already zapped from the virtual address space, such as from
- * previous discards, new pinning will cause valid mappings to be
- * re-established. Likewise, when the overall MemoryListener for a new
- * container is registered, a replay of mappings within the AddressSpace
- * will occur, re-establishing any previously zapped pages as well.
- *
- * Especially virtio-balloon is currently only prevented from discarding
- * new memory, it will not yet set ram_block_discard_set_required() and
- * therefore, neither stops us here or deals with the sudden memory
- * consumption of inflated memory.
- *
- * We do support discarding of memory coordinated via the RamDiscardManager
- * with some IOMMU types. vfio_ram_block_discard_disable() handles the
- * details once we know which type of IOMMU we are using.
- */
-
- ret = vfio_ram_block_discard_disable(container, true);
- if (ret) {
- error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken");
- if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
- error_report("vfio: error disconnecting group %d from"
- " container", group->groupid);
+ QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
+ if (!vbasedev->dirty_tracking) {
+ return false;
}
}
- return !ret;
-}
-static bool vfio_container_group_add(VFIOContainer *container, VFIOGroup *group,
- Error **errp)
-{
- if (!vfio_container_attach_discard_disable(container, group, errp)) {
- return false;
- }
- group->container = container;
- QLIST_INSERT_HEAD(&container->group_list, group, container_next);
- vfio_group_add_kvm_device(group);
- /*
- * Remember the container fd for each group, so we can attach to the same
- * container after CPR.
- */
- cpr_resave_fd("vfio_container_for_group", group->groupid, container->fd);
return true;
}
-static void vfio_container_group_del(VFIOContainer *container, VFIOGroup *group)
+bool vfio_container_dirty_tracking_is_started(
+ const VFIOContainer *bcontainer)
{
- QLIST_REMOVE(group, container_next);
- group->container = NULL;
- vfio_group_del_kvm_device(group);
- vfio_ram_block_discard_disable(container, false);
- cpr_delete_fd("vfio_container_for_group", group->groupid);
+ return vfio_container_devices_dirty_tracking_is_started(bcontainer) ||
+ bcontainer->dirty_pages_started;
}
-static bool vfio_container_connect(VFIOGroup *group, AddressSpace *as,
- Error **errp)
+bool vfio_container_devices_dirty_tracking_is_supported(
+ const VFIOContainer *bcontainer)
{
- VFIOContainer *container;
- VFIOContainerBase *bcontainer;
- int ret, fd = -1;
- VFIOAddressSpace *space;
- VFIOIOMMUClass *vioc = NULL;
- bool new_container = false;
- bool group_was_added = false;
-
- space = vfio_address_space_get(as);
- fd = cpr_find_fd("vfio_container_for_group", group->groupid);
-
- if (!cpr_is_incoming()) {
- QLIST_FOREACH(bcontainer, &space->containers, next) {
- container = VFIO_IOMMU_LEGACY(bcontainer);
- if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) {
- return vfio_container_group_add(container, group, errp);
- }
- }
+ VFIODevice *vbasedev;
- fd = qemu_open("/dev/vfio/vfio", O_RDWR, errp);
- if (fd < 0) {
- goto fail;
+ QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
+ if (vbasedev->device_dirty_page_tracking == ON_OFF_AUTO_OFF) {
+ return false;
}
- } else {
- /*
- * For incoming CPR, the group is already attached in the kernel.
- * If a container with matching fd is found, then update the
- * userland group list and return. If not, then after the loop,
- * create the container struct and group list.
- */
- QLIST_FOREACH(bcontainer, &space->containers, next) {
- container = VFIO_IOMMU_LEGACY(bcontainer);
-
- if (vfio_cpr_container_match(container, group, fd)) {
- return vfio_container_group_add(container, group, errp);
- }
- }
- }
-
- ret = ioctl(fd, VFIO_GET_API_VERSION);
- if (ret != VFIO_API_VERSION) {
- error_setg(errp, "supported vfio version: %d, "
- "reported version: %d", VFIO_API_VERSION, ret);
- goto fail;
- }
-
- container = vfio_create_container(fd, group, errp);
- if (!container) {
- goto fail;
- }
- new_container = true;
- bcontainer = VFIO_IOMMU(container);
-
- if (!vfio_legacy_cpr_register_container(container, errp)) {
- goto fail;
- }
-
- vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- assert(vioc->setup);
-
- if (!vioc->setup(bcontainer, errp)) {
- goto fail;
- }
-
- vfio_address_space_insert(space, bcontainer);
-
- if (!vfio_container_group_add(container, group, errp)) {
- goto fail;
- }
- group_was_added = true;
-
- /*
- * If CPR, register the listener later, after all state that may
- * affect regions and mapping boundaries has been cpr load'ed. Later,
- * the listener will invoke its callback on each flat section and call
- * dma_map to supply the new vaddr, and the calls will match the mappings
- * remembered by the kernel.
- */
- if (!cpr_is_incoming()) {
- if (!vfio_listener_register(bcontainer, errp)) {
- goto fail;
+ if (!vbasedev->dirty_pages_supported) {
+ return false;
}
}
- bcontainer->initialized = true;
-
return true;
-
-fail:
- if (new_container) {
- vfio_listener_unregister(bcontainer);
- }
-
- if (group_was_added) {
- vfio_container_group_del(container, group);
- }
- if (vioc && vioc->release) {
- vioc->release(bcontainer);
- }
- if (new_container) {
- vfio_legacy_cpr_unregister_container(container);
- object_unref(container);
- }
- if (fd >= 0) {
- close(fd);
- }
- vfio_address_space_put(space);
-
- return false;
-}
-
-static void vfio_container_disconnect(VFIOGroup *group)
-{
- VFIOContainer *container = group->container;
- VFIOContainerBase *bcontainer = VFIO_IOMMU(container);
- VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
-
- QLIST_REMOVE(group, container_next);
- group->container = NULL;
- cpr_delete_fd("vfio_container_for_group", group->groupid);
-
- /*
- * Explicitly release the listener first before unset container,
- * since unset may destroy the backend container if it's the last
- * group.
- */
- if (QLIST_EMPTY(&container->group_list)) {
- vfio_listener_unregister(bcontainer);
- if (vioc->release) {
- vioc->release(bcontainer);
- }
- }
-
- if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) {
- error_report("vfio: error disconnecting group %d from container",
- group->groupid);
- }
-
- if (QLIST_EMPTY(&container->group_list)) {
- VFIOAddressSpace *space = bcontainer->space;
-
- trace_vfio_container_disconnect(container->fd);
- vfio_legacy_cpr_unregister_container(container);
- close(container->fd);
- object_unref(container);
-
- vfio_address_space_put(space);
- }
}
-static VFIOGroup *vfio_group_get(int groupid, AddressSpace *as, Error **errp)
+static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
+ hwaddr size, void *bitmap)
{
- ERRP_GUARD();
- VFIOGroup *group;
- char path[32];
- struct vfio_group_status status = { .argsz = sizeof(status) };
-
- QLIST_FOREACH(group, &vfio_group_list, next) {
- if (group->groupid == groupid) {
- /* Found it. Now is it already in the right context? */
- if (VFIO_IOMMU(group->container)->space->as == as) {
- return group;
- } else {
- error_setg(errp, "group %d used in multiple address spaces",
- group->groupid);
- return NULL;
- }
- }
- }
+ uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
+ sizeof(struct vfio_device_feature_dma_logging_report),
+ sizeof(uint64_t))] = {};
+ struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
+ struct vfio_device_feature_dma_logging_report *report =
+ (struct vfio_device_feature_dma_logging_report *)feature->data;
- group = g_malloc0(sizeof(*group));
+ report->iova = iova;
+ report->length = size;
+ report->page_size = qemu_real_host_page_size();
+ report->bitmap = (uintptr_t)bitmap;
- snprintf(path, sizeof(path), "/dev/vfio/%d", groupid);
- group->fd = cpr_open_fd(path, O_RDWR, "vfio_group", groupid, errp);
- if (group->fd < 0) {
- goto free_group_exit;
- }
+ feature->argsz = sizeof(buf);
+ feature->flags = VFIO_DEVICE_FEATURE_GET |
+ VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT;
- if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) {
- error_setg_errno(errp, errno, "failed to get group %d status", groupid);
- goto close_fd_exit;
- }
-
- if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
- error_setg(errp, "group %d is not viable", groupid);
- error_append_hint(errp,
- "Please ensure all devices within the iommu_group "
- "are bound to their vfio bus driver.\n");
- goto close_fd_exit;
- }
-
- group->groupid = groupid;
- QLIST_INIT(&group->device_list);
-
- if (!vfio_container_connect(group, as, errp)) {
- error_prepend(errp, "failed to setup container for group %d: ",
- groupid);
- goto close_fd_exit;
- }
-
- QLIST_INSERT_HEAD(&vfio_group_list, group, next);
-
- return group;
-
-close_fd_exit:
- cpr_delete_fd("vfio_group", groupid);
- close(group->fd);
-
-free_group_exit:
- g_free(group);
-
- return NULL;
-}
-
-static void vfio_group_put(VFIOGroup *group)
-{
- if (!group || !QLIST_EMPTY(&group->device_list)) {
- return;
- }
-
- if (!group->ram_block_discard_allowed) {
- vfio_ram_block_discard_disable(group->container, false);
- }
- vfio_group_del_kvm_device(group);
- vfio_container_disconnect(group);
- QLIST_REMOVE(group, next);
- trace_vfio_group_put(group->fd);
- cpr_delete_fd("vfio_group", group->groupid);
- close(group->fd);
- g_free(group);
-}
-
-static bool vfio_device_get(VFIOGroup *group, const char *name,
- VFIODevice *vbasedev, Error **errp)
-{
- g_autofree struct vfio_device_info *info = NULL;
- int fd;
-
- fd = vfio_cpr_group_get_device_fd(group->fd, name);
- if (fd < 0) {
- error_setg_errno(errp, errno, "error getting device from group %d",
- group->groupid);
- error_append_hint(errp,
- "Verify all devices in group %d are bound to vfio-<bus> "
- "or pci-stub and not already in use\n", group->groupid);
- return false;
- }
-
- info = vfio_get_device_info(fd);
- if (!info) {
- error_setg_errno(errp, errno, "error getting device info");
- goto fail;
- }
-
- /*
- * Set discarding of RAM as not broken for this group if the driver knows
- * the device operates compatibly with discarding. Setting must be
- * consistent per group, but since compatibility is really only possible
- * with mdev currently, we expect singleton groups.
- */
- if (vbasedev->ram_block_discard_allowed !=
- group->ram_block_discard_allowed) {
- if (!QLIST_EMPTY(&group->device_list)) {
- error_setg(errp, "Inconsistent setting of support for discarding "
- "RAM (e.g., balloon) within group");
- goto fail;
- }
-
- if (!group->ram_block_discard_allowed) {
- group->ram_block_discard_allowed = true;
- vfio_ram_block_discard_disable(group->container, false);
- }
- }
-
- vfio_device_prepare(vbasedev, VFIO_IOMMU(group->container), info);
-
- vbasedev->fd = fd;
- vbasedev->group = group;
- QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
-
- trace_vfio_device_get(name, info->flags, info->num_regions, info->num_irqs);
-
- return true;
-
-fail:
- close(fd);
- cpr_delete_fd(name, 0);
- return false;
-}
-
-static void vfio_device_put(VFIODevice *vbasedev)
-{
- if (!vbasedev->group) {
- return;
- }
- QLIST_REMOVE(vbasedev, next);
- vbasedev->group = NULL;
- trace_vfio_device_put(vbasedev->fd);
- cpr_delete_fd(vbasedev->name, 0);
- close(vbasedev->fd);
+ return vbasedev->io_ops->device_feature(vbasedev, feature);
}
-static int vfio_device_get_groupid(VFIODevice *vbasedev, Error **errp)
+static int vfio_container_iommu_query_dirty_bitmap(
+ const VFIOContainer *bcontainer, VFIOBitmap *vbmap, hwaddr iova,
+ hwaddr size, Error **errp)
{
- char *tmp, group_path[PATH_MAX];
- g_autofree char *group_name = NULL;
- int ret, groupid;
- ssize_t len;
-
- tmp = g_strdup_printf("%s/iommu_group", vbasedev->sysfsdev);
- len = readlink(tmp, group_path, sizeof(group_path));
- g_free(tmp);
-
- if (len <= 0 || len >= sizeof(group_path)) {
- ret = len < 0 ? -errno : -ENAMETOOLONG;
- error_setg_errno(errp, -ret, "no iommu_group found");
- return ret;
- }
-
- group_path[len] = 0;
+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
- group_name = g_path_get_basename(group_path);
- if (sscanf(group_name, "%d", &groupid) != 1) {
- error_setg_errno(errp, errno, "failed to read %s", group_path);
- return -errno;
- }
- return groupid;
+ g_assert(vioc->query_dirty_bitmap);
+ return vioc->query_dirty_bitmap(bcontainer, vbmap, iova, size,
+ errp);
}
-/*
- * vfio_device_attach: attach a device to a security context
- * @name and @vbasedev->name are likely to be different depending
- * on the type of the device, hence the need for passing @name
- */
-static bool vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
- AddressSpace *as, Error **errp)
+static int vfio_container_devices_query_dirty_bitmap(
+ const VFIOContainer *bcontainer, VFIOBitmap *vbmap, hwaddr iova,
+ hwaddr size, Error **errp)
{
- int groupid = vfio_device_get_groupid(vbasedev, errp);
- VFIODevice *vbasedev_iter;
- VFIOGroup *group;
-
- if (groupid < 0) {
- return false;
- }
-
- trace_vfio_device_attach(vbasedev->name, groupid);
-
- group = vfio_group_get(groupid, as, errp);
- if (!group) {
- return false;
- }
-
- QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
- if (strcmp(vbasedev_iter->name, vbasedev->name) == 0) {
- error_setg(errp, "device is already attached");
- goto group_put_exit;
- }
- }
- if (!vfio_device_get(group, name, vbasedev, errp)) {
- goto group_put_exit;
- }
+ VFIODevice *vbasedev;
+ int ret;
- if (!vfio_device_hiod_create_and_realize(vbasedev,
- TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO,
- errp)) {
- goto device_put_exit;
- }
+ QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
+ ret = vfio_device_dma_logging_report(vbasedev, iova, size,
+ vbmap->bitmap);
+ if (ret) {
+ error_setg_errno(errp, -ret,
+ "%s: Failed to get DMA logging report, iova: "
+ "0x%" HWADDR_PRIx ", size: 0x%" HWADDR_PRIx,
+ vbasedev->name, iova, size);
- if (vbasedev->mdev) {
- error_setg(&vbasedev->cpr.mdev_blocker,
- "CPR does not support vfio mdev %s", vbasedev->name);
- if (migrate_add_blocker_modes(&vbasedev->cpr.mdev_blocker, errp,
- MIG_MODE_CPR_TRANSFER, -1) < 0) {
- goto hiod_unref_exit;
+ return ret;
}
}
- return true;
-
-hiod_unref_exit:
- object_unref(vbasedev->hiod);
-device_put_exit:
- vfio_device_put(vbasedev);
-group_put_exit:
- vfio_group_put(group);
- return false;
+ return 0;
}
-static void vfio_legacy_detach_device(VFIODevice *vbasedev)
+int vfio_container_query_dirty_bitmap(const VFIOContainer *bcontainer,
+ uint64_t iova, uint64_t size,
+ ram_addr_t ram_addr, Error **errp)
{
- VFIOGroup *group = vbasedev->group;
-
- trace_vfio_device_detach(vbasedev->name, group->groupid);
-
- vfio_device_unprepare(vbasedev);
-
- migrate_del_blocker(&vbasedev->cpr.mdev_blocker);
- object_unref(vbasedev->hiod);
- vfio_device_put(vbasedev);
- vfio_group_put(group);
-}
+ bool all_device_dirty_tracking =
+ vfio_container_devices_dirty_tracking_is_supported(bcontainer);
+ uint64_t dirty_pages;
+ VFIOBitmap vbmap;
+ int ret;
-static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
-{
- VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
- VFIOGroup *group;
- struct vfio_pci_hot_reset_info *info = NULL;
- struct vfio_pci_dependent_device *devices;
- struct vfio_pci_hot_reset *reset;
- int32_t *fds;
- int ret, i, count;
- bool multi = false;
-
- trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
-
- if (!single) {
- vfio_pci_pre_reset(vdev);
+ if (!bcontainer->dirty_pages_supported && !all_device_dirty_tracking) {
+ cpu_physical_memory_set_dirty_range(ram_addr, size,
+ tcg_enabled() ? DIRTY_CLIENTS_ALL :
+ DIRTY_CLIENTS_NOCODE);
+ return 0;
}
- vdev->vbasedev.needs_reset = false;
-
- ret = vfio_pci_get_pci_hot_reset_info(vdev, &info);
+ ret = vfio_bitmap_alloc(&vbmap, size);
if (ret) {
- goto out_single;
- }
- devices = &info->devices[0];
-
- trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
-
- /* Verify that we have all the groups required */
- for (i = 0; i < info->count; i++) {
- PCIHostDeviceAddress host;
- VFIOPCIDevice *tmp;
- VFIODevice *vbasedev_iter;
-
- host.domain = devices[i].segment;
- host.bus = devices[i].bus;
- host.slot = PCI_SLOT(devices[i].devfn);
- host.function = PCI_FUNC(devices[i].devfn);
-
- trace_vfio_pci_hot_reset_dep_devices(host.domain,
- host.bus, host.slot, host.function, devices[i].group_id);
-
- if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
- continue;
- }
-
- QLIST_FOREACH(group, &vfio_group_list, next) {
- if (group->groupid == devices[i].group_id) {
- break;
- }
- }
-
- if (!group) {
- if (!vdev->has_pm_reset) {
- error_report("vfio: Cannot reset device %s, "
- "depends on group %d which is not owned.",
- vdev->vbasedev.name, devices[i].group_id);
- }
- ret = -EPERM;
- goto out;
- }
-
- /* Prep dependent devices for reset and clear our marker. */
- QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
- if (!vbasedev_iter->dev->realized ||
- !vfio_pci_from_vfio_device(vbasedev_iter)) {
- continue;
- }
- tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
- if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
- if (single) {
- ret = -EINVAL;
- goto out_single;
- }
- vfio_pci_pre_reset(tmp);
- tmp->vbasedev.needs_reset = false;
- multi = true;
- break;
- }
- }
- }
-
- if (!single && !multi) {
- ret = -EINVAL;
- goto out_single;
- }
-
- /* Determine how many group fds need to be passed */
- count = 0;
- QLIST_FOREACH(group, &vfio_group_list, next) {
- for (i = 0; i < info->count; i++) {
- if (group->groupid == devices[i].group_id) {
- count++;
- break;
- }
- }
+ error_setg_errno(errp, -ret,
+ "Failed to allocate dirty tracking bitmap");
+ return ret;
}
- reset = g_malloc0(sizeof(*reset) + (count * sizeof(*fds)));
- reset->argsz = sizeof(*reset) + (count * sizeof(*fds));
- fds = &reset->group_fds[0];
-
- /* Fill in group fds */
- QLIST_FOREACH(group, &vfio_group_list, next) {
- for (i = 0; i < info->count; i++) {
- if (group->groupid == devices[i].group_id) {
- fds[reset->count++] = group->fd;
- break;
- }
- }
+ if (all_device_dirty_tracking) {
+ ret = vfio_container_devices_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
+ errp);
+ } else {
+ ret = vfio_container_iommu_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
+ errp);
}
- /* Bus reset! */
- ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
- g_free(reset);
if (ret) {
- ret = -errno;
+ goto out;
}
- trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
- ret ? strerror(errno) : "Success");
+ dirty_pages = cpu_physical_memory_set_dirty_lebitmap(vbmap.bitmap, ram_addr,
+ vbmap.pages);
+ trace_vfio_container_query_dirty_bitmap(iova, size, vbmap.size, ram_addr,
+ dirty_pages);
out:
- /* Re-enable INTx on affected devices */
- for (i = 0; i < info->count; i++) {
- PCIHostDeviceAddress host;
- VFIOPCIDevice *tmp;
- VFIODevice *vbasedev_iter;
-
- host.domain = devices[i].segment;
- host.bus = devices[i].bus;
- host.slot = PCI_SLOT(devices[i].devfn);
- host.function = PCI_FUNC(devices[i].devfn);
-
- if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
- continue;
- }
-
- QLIST_FOREACH(group, &vfio_group_list, next) {
- if (group->groupid == devices[i].group_id) {
- break;
- }
- }
-
- if (!group) {
- break;
- }
-
- QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
- if (!vbasedev_iter->dev->realized ||
- !vfio_pci_from_vfio_device(vbasedev_iter)) {
- continue;
- }
- tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
- if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
- vfio_pci_post_reset(tmp);
- break;
- }
- }
- }
-out_single:
- if (!single) {
- vfio_pci_post_reset(vdev);
- }
- g_free(info);
+ g_free(vbmap.bitmap);
return ret;
}
-static void vfio_iommu_legacy_class_init(ObjectClass *klass, const void *data)
+static gpointer copy_iova_range(gconstpointer src, gpointer data)
{
- VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
-
- vioc->setup = vfio_legacy_setup;
- vioc->dma_map = vfio_legacy_dma_map;
- vioc->dma_unmap = vfio_legacy_dma_unmap;
- vioc->attach_device = vfio_legacy_attach_device;
- vioc->detach_device = vfio_legacy_detach_device;
- vioc->set_dirty_page_tracking = vfio_legacy_set_dirty_page_tracking;
- vioc->query_dirty_bitmap = vfio_legacy_query_dirty_bitmap;
- vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
-};
-
-static bool hiod_legacy_vfio_realize(HostIOMMUDevice *hiod, void *opaque,
- Error **errp)
-{
- VFIODevice *vdev = opaque;
+ Range *source = (Range *)src;
+ Range *dest = g_new(Range, 1);
- hiod->name = g_strdup(vdev->name);
- hiod->agent = opaque;
-
- return true;
+ range_set_bounds(dest, range_lob(source), range_upb(source));
+ return dest;
}
-static int hiod_legacy_vfio_get_cap(HostIOMMUDevice *hiod, int cap,
- Error **errp)
+GList *vfio_container_get_iova_ranges(const VFIOContainer *bcontainer)
{
- switch (cap) {
- case HOST_IOMMU_DEVICE_CAP_AW_BITS:
- return vfio_device_get_aw_bits(hiod->agent);
- default:
- error_setg(errp, "%s: unsupported capability %x", hiod->name, cap);
- return -EINVAL;
- }
+ assert(bcontainer);
+ return g_list_copy_deep(bcontainer->iova_ranges, copy_iova_range, NULL);
}
-static GList *
-hiod_legacy_vfio_get_iova_ranges(HostIOMMUDevice *hiod)
+static void vfio_container_instance_finalize(Object *obj)
{
- VFIODevice *vdev = hiod->agent;
+ VFIOContainer *bcontainer = VFIO_IOMMU(obj);
+ VFIOGuestIOMMU *giommu, *tmp;
- g_assert(vdev);
- return vfio_container_get_iova_ranges(vdev->bcontainer);
-}
+ QLIST_SAFE_REMOVE(bcontainer, next);
-static uint64_t
-hiod_legacy_vfio_get_page_size_mask(HostIOMMUDevice *hiod)
-{
- VFIODevice *vdev = hiod->agent;
+ QLIST_FOREACH_SAFE(giommu, &bcontainer->giommu_list, giommu_next, tmp) {
+ memory_region_unregister_iommu_notifier(
+ MEMORY_REGION(giommu->iommu_mr), &giommu->n);
+ QLIST_REMOVE(giommu, giommu_next);
+ g_free(giommu);
+ }
- g_assert(vdev);
- return vfio_container_get_page_size_mask(vdev->bcontainer);
+ g_list_free_full(bcontainer->iova_ranges, g_free);
}
-static void vfio_iommu_legacy_instance_init(Object *obj)
+static void vfio_container_instance_init(Object *obj)
{
- VFIOContainer *container = VFIO_IOMMU_LEGACY(obj);
+ VFIOContainer *bcontainer = VFIO_IOMMU(obj);
- QLIST_INIT(&container->group_list);
+ bcontainer->error = NULL;
+ bcontainer->dirty_pages_supported = false;
+ bcontainer->dma_max_mappings = 0;
+ bcontainer->iova_ranges = NULL;
+ QLIST_INIT(&bcontainer->giommu_list);
+ QLIST_INIT(&bcontainer->vrdl_list);
}
-static void hiod_legacy_vfio_class_init(ObjectClass *oc, const void *data)
-{
- HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
-
- hioc->realize = hiod_legacy_vfio_realize;
- hioc->get_cap = hiod_legacy_vfio_get_cap;
- hioc->get_iova_ranges = hiod_legacy_vfio_get_iova_ranges;
- hioc->get_page_size_mask = hiod_legacy_vfio_get_page_size_mask;
-};
-
static const TypeInfo types[] = {
{
- .name = TYPE_VFIO_IOMMU_LEGACY,
- .parent = TYPE_VFIO_IOMMU,
- .instance_init = vfio_iommu_legacy_instance_init,
+ .name = TYPE_VFIO_IOMMU,
+ .parent = TYPE_OBJECT,
+ .instance_init = vfio_container_instance_init,
+ .instance_finalize = vfio_container_instance_finalize,
.instance_size = sizeof(VFIOContainer),
- .class_init = vfio_iommu_legacy_class_init,
- }, {
- .name = TYPE_HOST_IOMMU_DEVICE_LEGACY_VFIO,
- .parent = TYPE_HOST_IOMMU_DEVICE,
- .class_init = hiod_legacy_vfio_class_init,
- }
+ .class_size = sizeof(VFIOIOMMUClass),
+ .abstract = true,
+ },
};
DEFINE_TYPES(types)