[PATCH v2 05/19] iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage

Nicolin Chen
2024-08-27T17:01:56Z

Use IOMMU_VIOMMU_TYPE_DEFAULT to cover the new IOMMU_VIOMMU_ALLOC ioctl.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
tools/testing/selftests/iommu/iommufd.c | 35 +++++++++++++++++++
tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++++++++++
2 files changed, 63 insertions(+)

diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 6343f4053bd4..5c770e94f299 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -554,6 +554,41 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)
}
}

+TEST_F(iommufd_ioas, viommu_default)
+{
+ uint32_t dev_id = self->device_id;
+ uint32_t viommu_id = 0;
+ uint32_t hwpt_id = 0;
+
+ if (dev_id) {
+ /* Negative test -- invalid hwpt */
+ test_err_viommu_alloc(ENOENT, dev_id, hwpt_id,
+ IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id);
+
+ /* Negative test -- not a nested parent hwpt */
+ test_cmd_hwpt_alloc(dev_id, self->ioas_id, 0, &hwpt_id);
+ test_err_viommu_alloc(EINVAL, dev_id, hwpt_id,
+ IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id);
+ test_ioctl_destroy(hwpt_id);
+
+ /* Allocate a nested parent HWP */
+ test_cmd_hwpt_alloc(dev_id, self->ioas_id,
+ IOMMU_HWPT_ALLOC_NEST_PARENT,
+ &hwpt_id);
+ /* Negative test -- unsupported viommu type */
+ test_err_viommu_alloc(EOPNOTSUPP, dev_id, hwpt_id,
+ 0xdead, &viommu_id);
+ /* Allocate a default type of viommu */
+ test_cmd_viommu_alloc(dev_id, hwpt_id,
+ IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id);
+ test_ioctl_destroy(viommu_id);
+ test_ioctl_destroy(hwpt_id);
+ } else {
+ test_err_viommu_alloc(ENOENT, dev_id, hwpt_id,
+ IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id);
+ }
+}
+
TEST_F(iommufd_ioas, hwpt_attach)
{
/* Create a device attached directly to a hwpt */
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))
--
2.43.0