drm/ttm: Introduce KUnit test
authorKarolina Stolarek <karolina.stolarek@intel.com>
Tue, 8 Aug 2023 09:51:12 +0000 (11:51 +0200)
committerChristian König <christian.koenig@amd.com>
Wed, 9 Aug 2023 16:04:22 +0000 (18:04 +0200)
Add the initial version of unit tests for ttm_device struct, together
with helper functions.

Signed-off-by: Karolina Stolarek <karolina.stolarek@intel.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/3d1cc45c8a0cf536b92a850e0025f6c555de0169.1691487006.git.karolina.stolarek@intel.com
Signed-off-by: Christian König <christian.koenig@amd.com>
drivers/gpu/drm/Kconfig
drivers/gpu/drm/ttm/Makefile
drivers/gpu/drm/ttm/tests/.kunitconfig [new file with mode: 0644]
drivers/gpu/drm/ttm/tests/Makefile [new file with mode: 0644]
drivers/gpu/drm/ttm/tests/ttm_device_test.c [new file with mode: 0644]
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c [new file with mode: 0644]
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h [new file with mode: 0644]

index 2a44b94..9d1f0e0 100644 (file)
@@ -195,6 +195,21 @@ config DRM_TTM
          GPU memory types. Will be enabled automatically if a device driver
          uses it.
 
+config DRM_TTM_KUNIT_TEST
+        tristate "KUnit tests for TTM" if !KUNIT_ALL_TESTS
+        default n
+        depends on DRM && KUNIT
+        select DRM_TTM
+        select DRM_EXPORT_FOR_TESTS if m
+        select DRM_KUNIT_TEST_HELPERS
+        default KUNIT_ALL_TESTS
+        help
+          Enables unit tests for TTM, a GPU memory manager subsystem used
+          to manage memory buffers. This option is mostly useful for kernel
+          developers.
+
+          If in doubt, say "N".
+
 config DRM_EXEC
        tristate
        depends on DRM
index f906b22..dad2981 100644 (file)
@@ -8,3 +8,4 @@ ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
 ttm-$(CONFIG_AGP) += ttm_agp_backend.o
 
 obj-$(CONFIG_DRM_TTM) += ttm.o
+obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += tests/
diff --git a/drivers/gpu/drm/ttm/tests/.kunitconfig b/drivers/gpu/drm/ttm/tests/.kunitconfig
new file mode 100644 (file)
index 0000000..75fdce0
--- /dev/null
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_DRM=y
+CONFIG_DRM_KUNIT_TEST_HELPERS=y
+CONFIG_DRM_TTM_KUNIT_TEST=y
diff --git a/drivers/gpu/drm/ttm/tests/Makefile b/drivers/gpu/drm/ttm/tests/Makefile
new file mode 100644 (file)
index 0000000..7917805
--- /dev/null
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0 AND MIT
+
+obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
+        ttm_device_test.o \
+        ttm_kunit_helpers.o
diff --git a/drivers/gpu/drm/ttm/tests/ttm_device_test.c b/drivers/gpu/drm/ttm/tests/ttm_device_test.c
new file mode 100644 (file)
index 0000000..76d927d
--- /dev/null
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+#include <drm/ttm/ttm_device.h>
+#include <drm/ttm/ttm_placement.h>
+
+#include "ttm_kunit_helpers.h"
+
+static void ttm_device_init_basic(struct kunit *test)
+{
+       struct ttm_test_devices *priv = test->priv;
+       struct ttm_device *ttm_dev;
+       struct ttm_resource_manager *ttm_sys_man;
+       int err;
+
+       ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
+
+       err = ttm_device_kunit_init(priv, ttm_dev, false, false);
+       KUNIT_ASSERT_EQ(test, err, 0);
+
+       KUNIT_EXPECT_PTR_EQ(test, ttm_dev->funcs, &ttm_dev_funcs);
+       KUNIT_ASSERT_NOT_NULL(test, ttm_dev->wq);
+       KUNIT_ASSERT_NOT_NULL(test, ttm_dev->man_drv[TTM_PL_SYSTEM]);
+
+       ttm_sys_man = &ttm_dev->sysman;
+       KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man);
+       KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_tt);
+       KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_type);
+       KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man->func);
+
+       KUNIT_EXPECT_PTR_EQ(test, ttm_dev->dev_mapping,
+                           priv->drm->anon_inode->i_mapping);
+
+       ttm_device_fini(ttm_dev);
+}
+
+static struct kunit_case ttm_device_test_cases[] = {
+       KUNIT_CASE(ttm_device_init_basic),
+       {}
+};
+
+static struct kunit_suite ttm_device_test_suite = {
+       .name = "ttm_device",
+       .init = ttm_test_devices_init,
+       .exit = ttm_test_devices_fini,
+       .test_cases = ttm_device_test_cases,
+};
+
+kunit_test_suites(&ttm_device_test_suite);
+
+MODULE_LICENSE("GPL");
diff --git a/drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c b/drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
new file mode 100644 (file)
index 0000000..dedc185
--- /dev/null
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include "ttm_kunit_helpers.h"
+
+struct ttm_device_funcs ttm_dev_funcs = {
+};
+EXPORT_SYMBOL_GPL(ttm_dev_funcs);
+
+int ttm_device_kunit_init(struct ttm_test_devices *priv,
+                         struct ttm_device *ttm,
+                         bool use_dma_alloc,
+                         bool use_dma32)
+{
+       struct drm_device *drm = priv->drm;
+       int err;
+
+       err = ttm_device_init(ttm, &ttm_dev_funcs, drm->dev,
+                             drm->anon_inode->i_mapping,
+                             drm->vma_offset_manager,
+                             use_dma_alloc, use_dma32);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(ttm_device_kunit_init);
+
+struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test)
+{
+       struct ttm_test_devices *devs;
+
+       devs = kunit_kzalloc(test, sizeof(*devs), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, devs);
+
+       devs->dev = drm_kunit_helper_alloc_device(test);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->dev);
+
+       devs->drm = __drm_kunit_helper_alloc_drm_device(test, devs->dev,
+                                                       sizeof(*devs->drm), 0,
+                                                       DRIVER_GEM);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->drm);
+
+       return devs;
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_basic);
+
+struct ttm_test_devices *ttm_test_devices_all(struct kunit *test)
+{
+       struct ttm_test_devices *devs;
+       struct ttm_device *ttm_dev;
+       int err;
+
+       devs = ttm_test_devices_basic(test);
+
+       ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
+
+       err = ttm_device_kunit_init(devs, ttm_dev, false, false);
+       KUNIT_ASSERT_EQ(test, err, 0);
+
+       devs->ttm_dev = ttm_dev;
+
+       return devs;
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_all);
+
+void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs)
+{
+       if (devs->ttm_dev)
+               ttm_device_fini(devs->ttm_dev);
+
+       drm_kunit_helper_free_device(test, devs->dev);
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_put);
+
+int ttm_test_devices_init(struct kunit *test)
+{
+       struct ttm_test_devices *priv;
+
+       priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+       KUNIT_ASSERT_NOT_NULL(test, priv);
+
+       priv = ttm_test_devices_basic(test);
+       test->priv = priv;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_init);
+
+void ttm_test_devices_fini(struct kunit *test)
+{
+       ttm_test_devices_put(test, test->priv);
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_fini);
+
+MODULE_LICENSE("GPL");
diff --git a/drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h b/drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h
new file mode 100644 (file)
index 0000000..f9f5bc0
--- /dev/null
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 AND MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#ifndef TTM_KUNIT_HELPERS_H
+#define TTM_KUNIT_HELPERS_H
+
+#include <drm/drm_drv.h>
+#include <drm/ttm/ttm_device.h>
+
+#include <drm/drm_kunit_helpers.h>
+#include <kunit/test.h>
+
+extern struct ttm_device_funcs ttm_dev_funcs;
+
+struct ttm_test_devices {
+       struct drm_device *drm;
+       struct device *dev;
+       struct ttm_device *ttm_dev;
+};
+
+/* Building blocks for test-specific init functions */
+int ttm_device_kunit_init(struct ttm_test_devices *priv,
+                         struct ttm_device *ttm,
+                         bool use_dma_alloc,
+                         bool use_dma32);
+
+struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test);
+struct ttm_test_devices *ttm_test_devices_all(struct kunit *test);
+
+void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs);
+
+/* Generic init/fini for tests that only need DRM/TTM devices */
+int ttm_test_devices_init(struct kunit *test);
+void ttm_test_devices_fini(struct kunit *test);
+
+#endif // TTM_KUNIT_HELPERS_H