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       | 84 +++++++++++++++++++
 2 files changed, 112 insertions(+)

diff --git a/tools/testing/selftests/iommu/iommufd_utils.h 
b/tools/testing/selftests/iommu/iommufd_utils.h
index 40f6f14ce136..307d097db9dd 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..c03705825576 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,87 @@ 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 device_id;
+       uint32_t viommu_id;
+};
+
+FIXTURE_VARIANT(iommufd_viommu)
+{
+       unsigned int viommu;
+       unsigned int viommu_type;
+};
+
+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) {
+               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,
+                                     variant->viommu_type, &self->viommu_id);
+
+               /* 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,
+                                     variant->viommu_type, &self->viommu_id);
+               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, &self->viommu_id);
+               /* Allocate a default type of viommu */
+               test_cmd_viommu_alloc(self->device_id, self->hwpt_id,
+                                     variant->viommu_type, &self->viommu_id);
+       } else {
+               test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id,
+                                     variant->viommu_type, &self->viommu_id);
+       }
+}
+
+FIXTURE_TEARDOWN(iommufd_viommu)
+{
+       if (variant->viommu) {
+               test_ioctl_destroy(self->viommu_id);
+               test_ioctl_destroy(self->hwpt_id);
+       }
+       teardown_iommufd(self->fd, _metadata);
+}
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, no_viommu)
+{
+};
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, viommu_default)
+{
+       .viommu = 1,
+       .viommu_type = IOMMU_VIOMMU_TYPE_DEFAULT,
+};
+
+FIXTURE_VARIANT_ADD(iommufd_viommu, mock_viommu)
+{
+       .viommu = 1,
+       .viommu_type = IOMMU_VIOMMU_TYPE_SELFTEST,
+};
+
+TEST_F(iommufd_viommu, viommu_auto_destroy)
+{
+}
+
 TEST_HARNESS_MAIN
-- 
2.43.0


Reply via email to