@@ -1007,6 +1007,8 @@ vfio_get_container_ops(VFIOIOMMUBackendType be)
switch (be) {
case VFIO_IOMMU_BACKEND_TYPE_LEGACY:
return &legacy_container_ops;
+ case VFIO_IOMMU_BACKEND_TYPE_IOMMUFD:
+ return &iommufd_container_ops;
default:
return NULL;
}
@@ -1016,9 +1018,10 @@ int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **errp)
{
const VFIOContainerOps *ops;
- ops = vfio_get_container_ops(VFIO_IOMMU_BACKEND_TYPE_LEGACY);
- if (!ops) {
- return -ENOENT;
+ if (vbasedev->iommufd) {
+ ops = vfio_get_container_ops(VFIO_IOMMU_BACKEND_TYPE_IOMMUFD);
+ } else {
+ ops = vfio_get_container_ops(VFIO_IOMMU_BACKEND_TYPE_LEGACY);
}
return ops->attach_device(vbasedev, as, errp);
}
@@ -42,6 +42,7 @@
#include "qapi/error.h"
#include "migration/blocker.h"
#include "migration/qemu-file.h"
+#include "sysemu/iommufd.h"
#define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
@@ -2852,6 +2853,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
int i, ret;
bool is_mdev;
+ if (vbasedev->iommufd) {
+ iommufd_backend_connect(vbasedev->iommufd, errp);
+ if (*errp) {
+ return;
+ }
+ }
+
if (!vbasedev->sysfsdev) {
if (!(~vdev->host.domain || ~vdev->host.bus ||
~vdev->host.slot || ~vdev->host.function)) {
@@ -3134,6 +3142,7 @@ error:
static void vfio_instance_finalize(Object *obj)
{
VFIOPCIDevice *vdev = VFIO_PCI(obj);
+ VFIODevice *vbasedev = &vdev->vbasedev;
vfio_display_finalize(vdev);
vfio_bars_finalize(vdev);
@@ -3146,6 +3155,9 @@ static void vfio_instance_finalize(Object *obj)
*
* g_free(vdev->igd_opregion);
*/
+ if (vbasedev->iommufd) {
+ iommufd_backend_disconnect(vbasedev->iommufd);
+ }
vfio_put_device(vdev);
}
@@ -3281,11 +3293,8 @@ static Property vfio_pci_dev_properties[] = {
qdev_prop_nv_gpudirect_clique, uint8_t),
DEFINE_PROP_OFF_AUTO_PCIBAR("x-msix-relocation", VFIOPCIDevice, msix_relo,
OFF_AUTOPCIBAR_OFF),
- /*
- * TODO - support passed fds... is this necessary?
- * DEFINE_PROP_STRING("vfiofd", VFIOPCIDevice, vfiofd_name),
- * DEFINE_PROP_STRING("vfiogroupfd, VFIOPCIDevice, vfiogroupfd_name),
- */
+ DEFINE_PROP_LINK("iommufd", VFIOPCIDevice, vbasedev.iommufd,
+ TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *),
DEFINE_PROP_END_OF_LIST(),
};