Skip to content

Commit e3912d0

Browse files
drobnikChristianKoenigAMD
authored andcommitted
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 <[email protected]> Reviewed-by: Christian König <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/3d1cc45c8a0cf536b92a850e0025f6c555de0169.1691487006.git.karolina.stolarek@intel.com Signed-off-by: Christian König <[email protected]>
1 parent 79cdc56 commit e3912d0

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed

drivers/gpu/drm/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ config DRM_TTM
195195
GPU memory types. Will be enabled automatically if a device driver
196196
uses it.
197197

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+
198213
config DRM_EXEC
199214
tristate
200215
depends on DRM

drivers/gpu/drm/ttm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
88
ttm-$(CONFIG_AGP) += ttm_agp_backend.o
99

1010
obj-$(CONFIG_DRM_TTM) += ttm.o
11+
obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += tests/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_DRM=y
3+
CONFIG_DRM_KUNIT_TEST_HELPERS=y
4+
CONFIG_DRM_TTM_KUNIT_TEST=y

drivers/gpu/drm/ttm/tests/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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");
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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");
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

0 commit comments

Comments
 (0)