summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/ttm/tests/ttm_tt_test.c
blob: fd4502c18de67cb6d317b70c0d18a7002c32b4f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// SPDX-License-Identifier: GPL-2.0 AND MIT
/*
 * Copyright © 2023 Intel Corporation
 */
#include <linux/shmem_fs.h>
#include <drm/ttm/ttm_tt.h>

#include "ttm_kunit_helpers.h"

#define BO_SIZE		SZ_4K

struct ttm_tt_test_case {
	const char *description;
	uint32_t size;
	uint32_t extra_pages_num;
};

static int ttm_tt_test_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_all(test);
	test->priv = priv;

	return 0;
}

static const struct ttm_tt_test_case ttm_tt_init_basic_cases[] = {
	{
		.description = "Page-aligned size",
		.size = SZ_4K,
	},
	{
		.description = "Extra pages requested",
		.size = SZ_4K,
		.extra_pages_num = 1,
	},
};

static void ttm_tt_init_case_desc(const struct ttm_tt_test_case *t,
				  char *desc)
{
	strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
}

KUNIT_ARRAY_PARAM(ttm_tt_init_basic, ttm_tt_init_basic_cases,
		  ttm_tt_init_case_desc);

static void ttm_tt_init_basic(struct kunit *test)
{
	const struct ttm_tt_test_case *params = test->param_value;
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	uint32_t page_flags = TTM_TT_FLAG_ZERO_ALLOC;
	enum ttm_caching caching = ttm_cached;
	uint32_t extra_pages = params->extra_pages_num;
	int num_pages = params->size >> PAGE_SHIFT;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, params->size);

	err = ttm_tt_init(tt, bo, page_flags, caching, extra_pages);
	KUNIT_ASSERT_EQ(test, err, 0);

	KUNIT_ASSERT_EQ(test, tt->num_pages, num_pages + extra_pages);

	KUNIT_ASSERT_EQ(test, tt->page_flags, page_flags);
	KUNIT_ASSERT_EQ(test, tt->caching, caching);

	KUNIT_ASSERT_NULL(test, tt->dma_address);
	KUNIT_ASSERT_NULL(test, tt->swap_storage);
}

static void ttm_tt_init_misaligned(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	enum ttm_caching caching = ttm_cached;
	uint32_t size = SZ_8K;
	int num_pages = (size + SZ_4K) >> PAGE_SHIFT;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, size);

	/* Make the object size misaligned */
	bo->base.size += 1;

	err = ttm_tt_init(tt, bo, 0, caching, 0);
	KUNIT_ASSERT_EQ(test, err, 0);

	KUNIT_ASSERT_EQ(test, tt->num_pages, num_pages);
}

static void ttm_tt_fini_basic(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	enum ttm_caching caching = ttm_cached;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	err = ttm_tt_init(tt, bo, 0, caching, 0);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_NOT_NULL(test, tt->pages);

	ttm_tt_fini(tt);
	KUNIT_ASSERT_NULL(test, tt->pages);
}

static void ttm_tt_fini_sg(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	enum ttm_caching caching = ttm_cached;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	err = ttm_sg_tt_init(tt, bo, 0, caching);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_NOT_NULL(test, tt->dma_address);

	ttm_tt_fini(tt);
	KUNIT_ASSERT_NULL(test, tt->dma_address);
}

static void ttm_tt_fini_shmem(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	struct file *shmem;
	enum ttm_caching caching = ttm_cached;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	err = ttm_tt_init(tt, bo, 0, caching, 0);
	KUNIT_ASSERT_EQ(test, err, 0);

	shmem = shmem_file_setup("ttm swap", BO_SIZE, 0);
	tt->swap_storage = shmem;

	ttm_tt_fini(tt);
	KUNIT_ASSERT_NULL(test, tt->swap_storage);
}

static void ttm_tt_create_basic(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	int err;

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
	bo->type = ttm_bo_type_device;

	dma_resv_lock(bo->base.resv, NULL);
	err = ttm_tt_create(bo, false);
	dma_resv_unlock(bo->base.resv);

	KUNIT_EXPECT_EQ(test, err, 0);
	KUNIT_EXPECT_NOT_NULL(test, bo->ttm);

	/* Free manually, as it was allocated outside of KUnit */
	kfree(bo->ttm);
}

static void ttm_tt_create_invalid_bo_type(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	int err;

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
	bo->type = ttm_bo_type_sg + 1;

	dma_resv_lock(bo->base.resv, NULL);
	err = ttm_tt_create(bo, false);
	dma_resv_unlock(bo->base.resv);

	KUNIT_EXPECT_EQ(test, err, -EINVAL);
	KUNIT_EXPECT_NULL(test, bo->ttm);
}

static void ttm_tt_create_ttm_exists(struct kunit *test)
{
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	enum ttm_caching caching = ttm_cached;
	int err;

	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	err = ttm_tt_init(tt, bo, 0, caching, 0);
	KUNIT_ASSERT_EQ(test, err, 0);
	bo->ttm = tt;

	dma_resv_lock(bo->base.resv, NULL);
	err = ttm_tt_create(bo, false);
	dma_resv_unlock(bo->base.resv);

	/* Expect to keep the previous TTM */
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_PTR_EQ(test, tt, bo->ttm);
}

static struct ttm_tt *ttm_tt_null_create(struct ttm_buffer_object *bo,
					 uint32_t page_flags)
{
	return NULL;
}

static struct ttm_device_funcs ttm_dev_empty_funcs = {
	.ttm_tt_create = ttm_tt_null_create,
};

static void ttm_tt_create_failed(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	struct ttm_buffer_object *bo;
	int err;

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	/* Update ttm_device_funcs so we don't alloc ttm_tt */
	devs->ttm_dev->funcs = &ttm_dev_empty_funcs;

	dma_resv_lock(bo->base.resv, NULL);
	err = ttm_tt_create(bo, false);
	dma_resv_unlock(bo->base.resv);

	KUNIT_ASSERT_EQ(test, err, -ENOMEM);
}

static void ttm_tt_destroy_basic(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	struct ttm_buffer_object *bo;
	int err;

	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);

	dma_resv_lock(bo->base.resv, NULL);
	err = ttm_tt_create(bo, false);
	dma_resv_unlock(bo->base.resv);

	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_NOT_NULL(test, bo->ttm);

	ttm_tt_destroy(devs->ttm_dev, bo->ttm);
}

static struct kunit_case ttm_tt_test_cases[] = {
	KUNIT_CASE_PARAM(ttm_tt_init_basic, ttm_tt_init_basic_gen_params),
	KUNIT_CASE(ttm_tt_init_misaligned),
	KUNIT_CASE(ttm_tt_fini_basic),
	KUNIT_CASE(ttm_tt_fini_sg),
	KUNIT_CASE(ttm_tt_fini_shmem),
	KUNIT_CASE(ttm_tt_create_basic),
	KUNIT_CASE(ttm_tt_create_invalid_bo_type),
	KUNIT_CASE(ttm_tt_create_ttm_exists),
	KUNIT_CASE(ttm_tt_create_failed),
	KUNIT_CASE(ttm_tt_destroy_basic),
	{}
};

static struct kunit_suite ttm_tt_test_suite = {
	.name = "ttm_tt",
	.init = ttm_tt_test_init,
	.exit = ttm_test_devices_fini,
	.test_cases = ttm_tt_test_cases,
};

kunit_test_suites(&ttm_tt_test_suite);

MODULE_LICENSE("GPL");