summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/ttm/tests/ttm_tt_test.c
blob: 61ec6f580b626fd549431f456ff2cda947ef9a93 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// 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;
	u32 size;
	u32 extra_pages_num;
};

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;
	u32 page_flags = TTM_TT_FLAG_ZERO_ALLOC;
	enum ttm_caching caching = ttm_cached;
	u32 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, NULL);

	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;
	u32 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, NULL);

	/* 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, NULL);

	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, NULL);

	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, NULL);

	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, NULL);
	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, NULL);
	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, NULL);

	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,
					 u32 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, NULL);

	/* 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, NULL);

	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 void ttm_tt_populate_null_ttm(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	struct ttm_operation_ctx ctx = { };
	int err;

	err = ttm_tt_populate(devs->ttm_dev, NULL, &ctx);
	KUNIT_ASSERT_EQ(test, err, -EINVAL);
}

static void ttm_tt_populate_populated_ttm(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	struct ttm_operation_ctx ctx = { };
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	struct page *populated_page;
	int err;

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

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

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

	err = ttm_tt_populate(devs->ttm_dev, tt, &ctx);
	KUNIT_ASSERT_EQ(test, err, 0);
	populated_page = *tt->pages;

	err = ttm_tt_populate(devs->ttm_dev, tt, &ctx);
	KUNIT_ASSERT_PTR_EQ(test, populated_page, *tt->pages);
}

static void ttm_tt_unpopulate_basic(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	struct ttm_operation_ctx ctx = { };
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	int err;

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

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

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

	err = ttm_tt_populate(devs->ttm_dev, tt, &ctx);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_TRUE(test, ttm_tt_is_populated(tt));

	ttm_tt_unpopulate(devs->ttm_dev, tt);
	KUNIT_ASSERT_FALSE(test, ttm_tt_is_populated(tt));
}

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

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

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

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

	ttm_tt_unpopulate(devs->ttm_dev, tt);
	/* Expect graceful handling of unpopulated TTs */
}

static void ttm_tt_swapin_basic(struct kunit *test)
{
	const struct ttm_test_devices *devs = test->priv;
	int expected_num_pages = BO_SIZE >> PAGE_SHIFT;
	struct ttm_operation_ctx ctx = { };
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	int err, num_pages;

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

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

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

	err = ttm_tt_populate(devs->ttm_dev, tt, &ctx);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_TRUE(test, ttm_tt_is_populated(tt));

	num_pages = ttm_tt_swapout(devs->ttm_dev, tt, GFP_KERNEL);
	KUNIT_ASSERT_EQ(test, num_pages, expected_num_pages);
	KUNIT_ASSERT_NOT_NULL(test, tt->swap_storage);
	KUNIT_ASSERT_TRUE(test, tt->page_flags & TTM_TT_FLAG_SWAPPED);

	/* Swapout depopulates TT, allocate pages and then swap them in */
	err = ttm_pool_alloc(&devs->ttm_dev->pool, tt, &ctx);
	KUNIT_ASSERT_EQ(test, err, 0);

	err = ttm_tt_swapin(tt);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_NULL(test, tt->swap_storage);
	KUNIT_ASSERT_FALSE(test, tt->page_flags & TTM_TT_FLAG_SWAPPED);
}

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),
	KUNIT_CASE(ttm_tt_populate_null_ttm),
	KUNIT_CASE(ttm_tt_populate_populated_ttm),
	KUNIT_CASE(ttm_tt_unpopulate_basic),
	KUNIT_CASE(ttm_tt_unpopulate_empty_ttm),
	KUNIT_CASE(ttm_tt_swapin_basic),
	{}
};

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

kunit_test_suites(&ttm_tt_test_suite);

MODULE_DESCRIPTION("KUnit tests for ttm_tt APIs");
MODULE_LICENSE("GPL and additional rights");