Register a vfio iommufd container for CPR. Add a blocker if the kernel does not support IOMMU_IOAS_CHANGE_PROCESS.
Signed-off-by: Steve Sistare <steven.sist...@oracle.com> --- backends/iommufd.c | 8 ++++++ hw/vfio/cpr-iommufd.c | 60 +++++++++++++++++++++++++++++++++++++++++++ hw/vfio/iommufd.c | 2 ++ hw/vfio/meson.build | 1 + include/hw/vfio/vfio-common.h | 3 +++ include/sysemu/iommufd.h | 1 + 6 files changed, 75 insertions(+) create mode 100644 hw/vfio/cpr-iommufd.c diff --git a/backends/iommufd.c b/backends/iommufd.c index 4bdbad2..243178e 100644 --- a/backends/iommufd.c +++ b/backends/iommufd.c @@ -73,6 +73,14 @@ static void iommufd_backend_class_init(ObjectClass *oc, void *data) object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd); } +bool iommufd_change_process_capable(IOMMUFDBackend *be) +{ + struct iommu_ioas_change_process args = {.n_umap = -1}; + + ioctl(be->fd, IOMMU_IOAS_CHANGE_PROCESS, &args); + return (errno != ENOTTY); +} + bool iommufd_backend_connect(IOMMUFDBackend *be, const char *name, Error **errp) { int fd; diff --git a/hw/vfio/cpr-iommufd.c b/hw/vfio/cpr-iommufd.c new file mode 100644 index 0000000..f2e34f4 --- /dev/null +++ b/hw/vfio/cpr-iommufd.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021-2024 Oracle and/or its affiliates. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/vfio/vfio-common.h" +#include "migration/blocker.h" +#include "migration/cpr.h" +#include "migration/migration.h" +#include "migration/vmstate.h" +#include "sysemu/iommufd.h" + +#define IOMMUFD_CONTAINER(base) \ + container_of(base, VFIOIOMMUFDContainer, bcontainer) + +static bool vfio_can_cpr_exec(VFIOIOMMUFDContainer *container, Error **errp) +{ + if (!iommufd_change_process_capable(container->be)) { + error_setg(errp, + "VFIO container does not support IOMMU_IOAS_CHANGE_PROCESS"); + return false; + } + return true; +} + +static const VMStateDescription vfio_container_vmstate = { + .name = "vfio-iommufd-container", + .version_id = 0, + .minimum_version_id = 0, + .needed = cpr_needed_for_reuse, + .fields = (VMStateField[]) { + VMSTATE_END_OF_LIST() + } +}; + +bool vfio_iommufd_cpr_register_container(VFIOContainerBase *bcontainer, + Error **errp) +{ + VFIOIOMMUFDContainer *container = IOMMUFD_CONTAINER(bcontainer); + + if (!vfio_can_cpr_exec(container, &bcontainer->cpr_blocker)) { + return migrate_add_blocker_modes(&bcontainer->cpr_blocker, errp, + MIG_MODE_CPR_EXEC, -1) == 0; + } + + vmstate_register(NULL, -1, &vfio_container_vmstate, container); + + return true; +} + +void vfio_iommufd_cpr_unregister_container(VFIOContainerBase *bcontainer) +{ + VFIOIOMMUFDContainer *container = IOMMUFD_CONTAINER(bcontainer); + + vmstate_unregister(NULL, &vfio_container_vmstate, container); +} diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 6d77daa..585bf09 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -632,6 +632,8 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data) vioc->attach_device = iommufd_cdev_attach; vioc->detach_device = iommufd_cdev_detach; vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset; + vioc->cpr_register = vfio_iommufd_cpr_register_container; + vioc->cpr_unregister = vfio_iommufd_cpr_unregister_container; }; static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, diff --git a/hw/vfio/meson.build b/hw/vfio/meson.build index 5487815..998adb5 100644 --- a/hw/vfio/meson.build +++ b/hw/vfio/meson.build @@ -13,6 +13,7 @@ vfio_ss.add(when: 'CONFIG_IOMMUFD', if_true: files( vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( 'cpr.c', 'cpr-legacy.c', + 'cpr-iommufd.c', 'display.c', 'pci-quirks.c', 'pci.c', diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index ec5b7168..8aa02d4 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -247,6 +247,9 @@ bool vfio_legacy_cpr_register_container(VFIOContainerBase *bcontainer, void vfio_legacy_cpr_unregister_container(VFIOContainerBase *bcontainer); bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container, uint32_t ioas_id, Error **errp); +bool vfio_iommufd_cpr_register_container(VFIOContainerBase *bcontainer, + Error **errp); +void vfio_iommufd_cpr_unregister_container(VFIOContainerBase *bcontainer); extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList; diff --git a/include/sysemu/iommufd.h b/include/sysemu/iommufd.h index 6955ebd..f80b968 100644 --- a/include/sysemu/iommufd.h +++ b/include/sysemu/iommufd.h @@ -52,6 +52,7 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, bool iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, uint32_t *type, void *data, uint32_t len, Error **errp); +bool iommufd_change_process_capable(IOMMUFDBackend *be); #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd" #endif -- 1.8.3.1