Add new IOMMU modes for using iommufd in compatibility mode with
VFIO_TYPE1_IOMMU and VFIO_TYPE1v2_IOMMU.

In these modes, VFIO selftests will open /dev/iommu and treats it as a
container FD (as if it had opened /dev/vfio/vfio) and the kernel
translates the container ioctls to iommufd calls transparently.

Acked-by: Shuah Khan <sk...@linuxfoundation.org>
Signed-off-by: David Matlack <dmatl...@google.com>
---
 tools/testing/selftests/vfio/lib/include/vfio_util.h |  4 +++-
 tools/testing/selftests/vfio/lib/vfio_pci_device.c   | 10 ++++++++++
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 12 ++++++++++--
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h 
b/tools/testing/selftests/vfio/lib/include/vfio_util.h
index 981ddc9a52a9..035ef5b9d678 100644
--- a/tools/testing/selftests/vfio/lib/include/vfio_util.h
+++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h
@@ -60,7 +60,9 @@ struct vfio_iommu_mode {
  */
 #define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(...) \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__)
+FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \
+FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \
+FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__)
 
 struct vfio_pci_bar {
        struct vfio_region_info info;
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c 
b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index cc1b732dd8ba..b6fefe2b3ec8 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -376,6 +376,16 @@ static const struct vfio_iommu_mode iommu_modes[] = {
                .container_path = "/dev/vfio/vfio",
                .iommu_type = VFIO_TYPE1v2_IOMMU,
        },
+       {
+               .name = "iommufd_compat_type1",
+               .container_path = "/dev/iommu",
+               .iommu_type = VFIO_TYPE1_IOMMU,
+       },
+       {
+               .name = "iommufd_compat_type1v2",
+               .container_path = "/dev/iommu",
+               .iommu_type = VFIO_TYPE1v2_IOMMU,
+       },
 };
 
 const char *default_iommu_mode = "vfio_type1_iommu";
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c 
b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index b65949c6b846..ab19c54a774d 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -128,6 +128,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
        const int flags = variant->mmap_flags;
        struct vfio_dma_region region;
        struct iommu_mapping mapping;
+       u64 mapping_size = size;
        int rc;
 
        region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
@@ -150,6 +151,13 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
        if (rc == -EOPNOTSUPP)
                goto unmap;
 
+       /*
+        * IOMMUFD compatibility-mode does not support huge mappings when
+        * using VFIO_TYPE1_IOMMU.
+        */
+       if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
+               mapping_size = SZ_4K;
+
        ASSERT_EQ(0, rc);
        printf("Found IOMMU mappings for IOVA 0x%lx:\n", region.iova);
        printf("PGD: 0x%016lx\n", mapping.pgd);
@@ -158,7 +166,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
        printf("PMD: 0x%016lx\n", mapping.pmd);
        printf("PTE: 0x%016lx\n", mapping.pte);
 
-       switch (size) {
+       switch (mapping_size) {
        case SZ_4K:
                ASSERT_NE(0, mapping.pte);
                break;
@@ -172,7 +180,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
                ASSERT_NE(0, mapping.pud);
                break;
        default:
-               VFIO_FAIL("Unrecognized size: 0x%lx\n", size);
+               VFIO_FAIL("Unrecognized size: 0x%lx\n", mapping_size);
        }
 
 unmap:
-- 
2.51.0.rc2.233.g662b1ed5c5-goog


Reply via email to