Add a new iommufd_viommu FIXTURE and setup it up with a vIOMMU object.

Any new vIOMMU feature will be added as a TEST_F under that.

Signed-off-by: Nicolin Chen <nicol...@nvidia.com>
---
 tools/testing/selftests/iommu/iommufd_utils.h | 28 ++++++
 tools/testing/selftests/iommu/iommufd.c       | 87 +++++++++++++++++++
 .../selftests/iommu/iommufd_fail_nth.c        | 11 +++
 3 files changed, 126 insertions(+)

diff --git a/tools/testing/selftests/iommu/iommufd_utils.h 
b/tools/testing/selftests/iommu/iommufd_utils.h
index 40f6f14ce136..ca09308dad6a 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -762,3 +762,31 @@ static int _test_cmd_trigger_iopf(int fd, __u32 device_id, 
__u32 fault_fd)
 
 #define test_cmd_trigger_iopf(device_id, fault_fd) \
        ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd))
+
+static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id,
+                                 __u32 type, __u32 flags, __u32 *viommu_id)
+{
+       struct iommu_viommu_alloc cmd = {
+               .size = sizeof(cmd),
+               .flags = flags,
+               .type = type,
+               .dev_id = device_id,
+               .hwpt_id = hwpt_id,
+       };
+       int ret;
+
+       ret = ioctl(fd, IOMMU_VIOMMU_ALLOC, &cmd);
+       if (ret)
+               return ret;
+       if (viommu_id)
+               *viommu_id = cmd.out_viommu_id;
+       return 0;
+}
+
+#define test_cmd_viommu_alloc(device_id, hwpt_id, type, viommu_id)        \
+       ASSERT_EQ(0, _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \
+                                           type, 0, viommu_id))
+#define test_err_viommu_alloc(_errno, device_id, hwpt_id, type, viommu_id) \
+       EXPECT_ERRNO(_errno,                                               \
+                    _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id,  \
+                                           type, 0, viommu_id))
diff --git a/tools/testing/selftests/iommu/iommufd.c 
b/tools/testing/selftests/iommu/iommufd.c
index 4927b9add5ad..b48b22d33ad4 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -128,6 +128,7 @@ TEST_F(iommufd, cmd_length)
        TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP, length);
        TEST_LENGTH(iommu_option, IOMMU_OPTION, val64);
        TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
+       TEST_LENGTH(iommu_viommu_alloc, IOMMU_VIOMMU_ALLOC, out_viommu_id);
 #undef TEST_LENGTH
 }
 
@@ -2386,4 +2387,90 @@ TEST_F(vfio_compat_mock_domain, huge_map)
        }
 }
 
+FIXTURE(iommufd_viommu)
+{
+       int fd;
+       uint32_t ioas_id;
+       uint32_t stdev_id;
+       uint32_t hwpt_id;
+       uint32_t nested_hwpt_id;
+       uint32_t device_id;
+       uint32_t viommu_id;
+};
+
+FIXTURE_VARIANT(iommufd_viommu)
+{
+       unsigned int viommu;
+};
+
+FIXTURE_SETUP(iommufd_viommu)
+{
+       self->fd = open("/dev/iommu", O_RDWR);
+       ASSERT_NE(-1, self->fd);
+       test_ioctl_ioas_alloc(&self->ioas_id);
+       test_ioctl_set_default_memory_limit();
+
+       if (variant->viommu) {
+               struct iommu_hwpt_selftest data = {
+                       .iotlb = IOMMU_TEST_IOTLB_DEFAULT,
+               };
+
+               test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL,
+                                    &self->device_id);
+
+               /* Negative test -- invalid hwpt */
+               test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id,
+                                     IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+
+               /* Negative test -- not a nesting parent hwpt */
+               test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0,
+                                   &self->hwpt_id);
+               test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id,
+                                     IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+               test_ioctl_destroy(self->hwpt_id);
+
+               /* Allocate a nesting parent HWP */
+               test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
+                                   IOMMU_HWPT_ALLOC_NEST_PARENT,
+                                   &self->hwpt_id);
+               /* Negative test -- unsupported viommu type */
+               test_err_viommu_alloc(EOPNOTSUPP, self->device_id,
+                                     self->hwpt_id, 0xdead, NULL);
+
+               test_cmd_viommu_alloc(self->device_id, self->hwpt_id,
+                                     IOMMU_VIOMMU_TYPE_SELFTEST,
+                                     &self->viommu_id);
+               EXPECT_ERRNO(EBUSY,
+                            _test_ioctl_destroy(self->fd, self->hwpt_id));
+               test_cmd_hwpt_alloc_nested(self->device_id, self->viommu_id, 0,
+                                          &self->nested_hwpt_id,
+                                          IOMMU_HWPT_DATA_SELFTEST, &data,
+                                          sizeof(data));
+               EXPECT_ERRNO(EBUSY,
+                            _test_ioctl_destroy(self->fd, self->viommu_id));
+       } else {
+               test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id,
+                                     IOMMU_VIOMMU_TYPE_SELFTEST, NULL);
+       }
+}
+
+FIXTURE_TEARDOWN(iommufd_viommu)
+{
+       teardown_iommufd(self->fd, _metadata);
+}
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, no_viommu)
+{
+       .viommu = 0,
+};
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, mock_viommu)
+{
+       .viommu = 1,
+};
+
+TEST_F(iommufd_viommu, viommu_auto_destroy)
+{
+}
+
 TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c 
b/tools/testing/selftests/iommu/iommufd_fail_nth.c
index c5d5e69452b0..e9a980b7729b 100644
--- a/tools/testing/selftests/iommu/iommufd_fail_nth.c
+++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c
@@ -582,6 +582,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
        uint32_t stdev_id;
        uint32_t idev_id;
        uint32_t hwpt_id;
+       uint32_t viommu_id;
        __u64 iova;
 
        self->fd = open("/dev/iommu", O_RDWR);
@@ -624,6 +625,16 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 
        if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL))
                return -1;
+
+       if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0,
+                                IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id,
+                                IOMMU_HWPT_DATA_NONE, 0, 0))
+               return -1;
+
+       if (_test_cmd_viommu_alloc(self->fd, idev_id, hwpt_id,
+                                  IOMMU_VIOMMU_TYPE_SELFTEST, 0, &viommu_id))
+               return -1;
+
        return 0;
 }
 
-- 
2.43.0


Reply via email to