Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/ttm: Introduce KUnit test

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>

authored by

Karolina Stolarek and committed by
Christian König
e3912d09 79cdc56c

+212
+15
drivers/gpu/drm/Kconfig
··· 195 195 GPU memory types. Will be enabled automatically if a device driver 196 196 uses it. 197 197 198 + config DRM_TTM_KUNIT_TEST 199 + tristate "KUnit tests for TTM" if !KUNIT_ALL_TESTS 200 + default n 201 + depends on DRM && KUNIT 202 + select DRM_TTM 203 + select DRM_EXPORT_FOR_TESTS if m 204 + select DRM_KUNIT_TEST_HELPERS 205 + default KUNIT_ALL_TESTS 206 + help 207 + Enables unit tests for TTM, a GPU memory manager subsystem used 208 + to manage memory buffers. This option is mostly useful for kernel 209 + developers. 210 + 211 + If in doubt, say "N". 212 + 198 213 config DRM_EXEC 199 214 tristate 200 215 depends on DRM
+1
drivers/gpu/drm/ttm/Makefile
··· 8 8 ttm-$(CONFIG_AGP) += ttm_agp_backend.o 9 9 10 10 obj-$(CONFIG_DRM_TTM) += ttm.o 11 + obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += tests/
+4
drivers/gpu/drm/ttm/tests/.kunitconfig
··· 1 + CONFIG_KUNIT=y 2 + CONFIG_DRM=y 3 + CONFIG_DRM_KUNIT_TEST_HELPERS=y 4 + CONFIG_DRM_TTM_KUNIT_TEST=y
+5
drivers/gpu/drm/ttm/tests/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 AND MIT 2 + 3 + obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \ 4 + ttm_device_test.o \ 5 + ttm_kunit_helpers.o
+54
drivers/gpu/drm/ttm/tests/ttm_device_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 AND MIT 2 + /* 3 + * Copyright © 2023 Intel Corporation 4 + */ 5 + #include <drm/ttm/ttm_resource.h> 6 + #include <drm/ttm/ttm_device.h> 7 + #include <drm/ttm/ttm_placement.h> 8 + 9 + #include "ttm_kunit_helpers.h" 10 + 11 + static void ttm_device_init_basic(struct kunit *test) 12 + { 13 + struct ttm_test_devices *priv = test->priv; 14 + struct ttm_device *ttm_dev; 15 + struct ttm_resource_manager *ttm_sys_man; 16 + int err; 17 + 18 + ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL); 19 + KUNIT_ASSERT_NOT_NULL(test, ttm_dev); 20 + 21 + err = ttm_device_kunit_init(priv, ttm_dev, false, false); 22 + KUNIT_ASSERT_EQ(test, err, 0); 23 + 24 + KUNIT_EXPECT_PTR_EQ(test, ttm_dev->funcs, &ttm_dev_funcs); 25 + KUNIT_ASSERT_NOT_NULL(test, ttm_dev->wq); 26 + KUNIT_ASSERT_NOT_NULL(test, ttm_dev->man_drv[TTM_PL_SYSTEM]); 27 + 28 + ttm_sys_man = &ttm_dev->sysman; 29 + KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man); 30 + KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_tt); 31 + KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_type); 32 + KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man->func); 33 + 34 + KUNIT_EXPECT_PTR_EQ(test, ttm_dev->dev_mapping, 35 + priv->drm->anon_inode->i_mapping); 36 + 37 + ttm_device_fini(ttm_dev); 38 + } 39 + 40 + static struct kunit_case ttm_device_test_cases[] = { 41 + KUNIT_CASE(ttm_device_init_basic), 42 + {} 43 + }; 44 + 45 + static struct kunit_suite ttm_device_test_suite = { 46 + .name = "ttm_device", 47 + .init = ttm_test_devices_init, 48 + .exit = ttm_test_devices_fini, 49 + .test_cases = ttm_device_test_cases, 50 + }; 51 + 52 + kunit_test_suites(&ttm_device_test_suite); 53 + 54 + MODULE_LICENSE("GPL");
+96
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 AND MIT 2 + /* 3 + * Copyright © 2023 Intel Corporation 4 + */ 5 + #include "ttm_kunit_helpers.h" 6 + 7 + struct ttm_device_funcs ttm_dev_funcs = { 8 + }; 9 + EXPORT_SYMBOL_GPL(ttm_dev_funcs); 10 + 11 + int ttm_device_kunit_init(struct ttm_test_devices *priv, 12 + struct ttm_device *ttm, 13 + bool use_dma_alloc, 14 + bool use_dma32) 15 + { 16 + struct drm_device *drm = priv->drm; 17 + int err; 18 + 19 + err = ttm_device_init(ttm, &ttm_dev_funcs, drm->dev, 20 + drm->anon_inode->i_mapping, 21 + drm->vma_offset_manager, 22 + use_dma_alloc, use_dma32); 23 + 24 + return err; 25 + } 26 + EXPORT_SYMBOL_GPL(ttm_device_kunit_init); 27 + 28 + struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test) 29 + { 30 + struct ttm_test_devices *devs; 31 + 32 + devs = kunit_kzalloc(test, sizeof(*devs), GFP_KERNEL); 33 + KUNIT_ASSERT_NOT_NULL(test, devs); 34 + 35 + devs->dev = drm_kunit_helper_alloc_device(test); 36 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->dev); 37 + 38 + devs->drm = __drm_kunit_helper_alloc_drm_device(test, devs->dev, 39 + sizeof(*devs->drm), 0, 40 + DRIVER_GEM); 41 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->drm); 42 + 43 + return devs; 44 + } 45 + EXPORT_SYMBOL_GPL(ttm_test_devices_basic); 46 + 47 + struct ttm_test_devices *ttm_test_devices_all(struct kunit *test) 48 + { 49 + struct ttm_test_devices *devs; 50 + struct ttm_device *ttm_dev; 51 + int err; 52 + 53 + devs = ttm_test_devices_basic(test); 54 + 55 + ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL); 56 + KUNIT_ASSERT_NOT_NULL(test, ttm_dev); 57 + 58 + err = ttm_device_kunit_init(devs, ttm_dev, false, false); 59 + KUNIT_ASSERT_EQ(test, err, 0); 60 + 61 + devs->ttm_dev = ttm_dev; 62 + 63 + return devs; 64 + } 65 + EXPORT_SYMBOL_GPL(ttm_test_devices_all); 66 + 67 + void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs) 68 + { 69 + if (devs->ttm_dev) 70 + ttm_device_fini(devs->ttm_dev); 71 + 72 + drm_kunit_helper_free_device(test, devs->dev); 73 + } 74 + EXPORT_SYMBOL_GPL(ttm_test_devices_put); 75 + 76 + int ttm_test_devices_init(struct kunit *test) 77 + { 78 + struct ttm_test_devices *priv; 79 + 80 + priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); 81 + KUNIT_ASSERT_NOT_NULL(test, priv); 82 + 83 + priv = ttm_test_devices_basic(test); 84 + test->priv = priv; 85 + 86 + return 0; 87 + } 88 + EXPORT_SYMBOL_GPL(ttm_test_devices_init); 89 + 90 + void ttm_test_devices_fini(struct kunit *test) 91 + { 92 + ttm_test_devices_put(test, test->priv); 93 + } 94 + EXPORT_SYMBOL_GPL(ttm_test_devices_fini); 95 + 96 + MODULE_LICENSE("GPL");
+37
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 AND MIT */ 2 + /* 3 + * Copyright © 2023 Intel Corporation 4 + */ 5 + #ifndef TTM_KUNIT_HELPERS_H 6 + #define TTM_KUNIT_HELPERS_H 7 + 8 + #include <drm/drm_drv.h> 9 + #include <drm/ttm/ttm_device.h> 10 + 11 + #include <drm/drm_kunit_helpers.h> 12 + #include <kunit/test.h> 13 + 14 + extern struct ttm_device_funcs ttm_dev_funcs; 15 + 16 + struct ttm_test_devices { 17 + struct drm_device *drm; 18 + struct device *dev; 19 + struct ttm_device *ttm_dev; 20 + }; 21 + 22 + /* Building blocks for test-specific init functions */ 23 + int ttm_device_kunit_init(struct ttm_test_devices *priv, 24 + struct ttm_device *ttm, 25 + bool use_dma_alloc, 26 + bool use_dma32); 27 + 28 + struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test); 29 + struct ttm_test_devices *ttm_test_devices_all(struct kunit *test); 30 + 31 + void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs); 32 + 33 + /* Generic init/fini for tests that only need DRM/TTM devices */ 34 + int ttm_test_devices_init(struct kunit *test); 35 + void ttm_test_devices_fini(struct kunit *test); 36 + 37 + #endif // TTM_KUNIT_HELPERS_H