summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/ttm/tests/ttm_pool_test.c
blob: 2d9cae8cd9844643292b702960dbf5de005a6eb9 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// SPDX-License-Identifier: GPL-2.0 AND MIT
/*
 * Copyright © 2023 Intel Corporation
 */
#include <linux/mm.h>

#include <drm/ttm/ttm_tt.h>
#include <drm/ttm/ttm_pool.h>

#include "ttm_kunit_helpers.h"

struct ttm_pool_test_case {
	const char *description;
	unsigned int order;
	bool use_dma_alloc;
};

struct ttm_pool_test_priv {
	struct ttm_test_devices *devs;

	/* Used to create mock ttm_tts */
	struct ttm_buffer_object *mock_bo;
};

static struct ttm_operation_ctx simple_ctx = {
	.interruptible = true,
	.no_wait_gpu = false,
};

static int ttm_pool_test_init(struct kunit *test)
{
	struct ttm_pool_test_priv *priv;

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

	priv->devs = ttm_test_devices_basic(test);
	test->priv = priv;

	return 0;
}

static void ttm_pool_test_fini(struct kunit *test)
{
	struct ttm_pool_test_priv *priv = test->priv;

	ttm_test_devices_put(test, priv->devs);
}

static struct ttm_tt *ttm_tt_kunit_init(struct kunit *test,
					uint32_t page_flags,
					enum ttm_caching caching,
					size_t size)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_buffer_object *bo;
	struct ttm_tt *tt;
	int err;

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

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

	err = ttm_tt_init(tt, priv->mock_bo, page_flags, caching, 0);
	KUNIT_ASSERT_EQ(test, err, 0);

	return tt;
}

static struct ttm_pool *ttm_pool_pre_populated(struct kunit *test,
					       size_t size,
					       enum ttm_caching caching)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_test_devices *devs = priv->devs;
	struct ttm_pool *pool;
	struct ttm_tt *tt;
	unsigned long order = __fls(size / PAGE_SIZE);
	int err;

	tt = ttm_tt_kunit_init(test, order, caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

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

	ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);

	return pool;
}

static const struct ttm_pool_test_case ttm_pool_basic_cases[] = {
	{
		.description = "One page",
		.order = 0,
	},
	{
		.description = "More than one page",
		.order = 2,
	},
	{
		.description = "Above the allocation limit",
		.order = MAX_ORDER + 1,
	},
	{
		.description = "One page, with coherent DMA mappings enabled",
		.order = 0,
		.use_dma_alloc = true,
	},
	{
		.description = "Above the allocation limit, with coherent DMA mappings enabled",
		.order = MAX_ORDER + 1,
		.use_dma_alloc = true,
	},
};

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

KUNIT_ARRAY_PARAM(ttm_pool_alloc_basic, ttm_pool_basic_cases,
		  ttm_pool_alloc_case_desc);

static void ttm_pool_alloc_basic(struct kunit *test)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_test_devices *devs = priv->devs;
	const struct ttm_pool_test_case *params = test->param_value;
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct page *fst_page, *last_page;
	enum ttm_caching caching = ttm_uncached;
	unsigned int expected_num_pages = 1 << params->order;
	size_t size = expected_num_pages * PAGE_SIZE;
	int err;

	tt = ttm_tt_kunit_init(test, 0, caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

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

	ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, params->use_dma_alloc,
		      false);

	KUNIT_ASSERT_PTR_EQ(test, pool->dev, devs->dev);
	KUNIT_ASSERT_EQ(test, pool->nid, NUMA_NO_NODE);
	KUNIT_ASSERT_EQ(test, pool->use_dma_alloc, params->use_dma_alloc);

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);

	fst_page = tt->pages[0];
	last_page = tt->pages[tt->num_pages - 1];

	if (params->order <= MAX_ORDER) {
		if (params->use_dma_alloc) {
			KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
			KUNIT_ASSERT_NOT_NULL(test, (void *)last_page->private);
		} else {
			KUNIT_ASSERT_EQ(test, fst_page->private, params->order);
		}
	} else {
		if (params->use_dma_alloc) {
			KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
			KUNIT_ASSERT_NULL(test, (void *)last_page->private);
		} else {
			/*
			 * We expect to alloc one big block, followed by
			 * order 0 blocks
			 */
			KUNIT_ASSERT_EQ(test, fst_page->private,
					min_t(unsigned int, MAX_ORDER,
					      params->order));
			KUNIT_ASSERT_EQ(test, last_page->private, 0);
		}
	}

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);
	ttm_pool_fini(pool);
}

static void ttm_pool_alloc_basic_dma_addr(struct kunit *test)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_test_devices *devs = priv->devs;
	const struct ttm_pool_test_case *params = test->param_value;
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_buffer_object *bo;
	dma_addr_t dma1, dma2;
	enum ttm_caching caching = ttm_uncached;
	unsigned int expected_num_pages = 1 << params->order;
	size_t size = expected_num_pages * PAGE_SIZE;
	int err;

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

	bo = ttm_bo_kunit_init(test, devs, size);
	KUNIT_ASSERT_NOT_NULL(test, bo);

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

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

	ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);
	KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);

	dma1 = tt->dma_address[0];
	dma2 = tt->dma_address[tt->num_pages - 1];

	KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma1);
	KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma2);

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);
	ttm_pool_fini(pool);
}

static void ttm_pool_alloc_order_caching_match(struct kunit *test)
{
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_pool_type *pt;
	enum ttm_caching caching = ttm_uncached;
	unsigned int order = 0;
	size_t size = PAGE_SIZE;
	int err;

	pool = ttm_pool_pre_populated(test, size, caching);

	pt = &pool->caching[caching].orders[order];
	KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));

	tt = ttm_tt_kunit_init(test, 0, caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);

	KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);
	ttm_pool_fini(pool);
}

static void ttm_pool_alloc_caching_mismatch(struct kunit *test)
{
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_pool_type *pt_pool, *pt_tt;
	enum ttm_caching tt_caching = ttm_uncached;
	enum ttm_caching pool_caching = ttm_cached;
	size_t size = PAGE_SIZE;
	unsigned int order = 0;
	int err;

	pool = ttm_pool_pre_populated(test, size, pool_caching);

	pt_pool = &pool->caching[pool_caching].orders[order];
	pt_tt = &pool->caching[tt_caching].orders[order];

	tt = ttm_tt_kunit_init(test, 0, tt_caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
	KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);

	KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
	KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));

	ttm_pool_fini(pool);
}

static void ttm_pool_alloc_order_mismatch(struct kunit *test)
{
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_pool_type *pt_pool, *pt_tt;
	enum ttm_caching caching = ttm_uncached;
	unsigned int order = 2;
	size_t fst_size = (1 << order) * PAGE_SIZE;
	size_t snd_size = PAGE_SIZE;
	int err;

	pool = ttm_pool_pre_populated(test, fst_size, caching);

	pt_pool = &pool->caching[caching].orders[order];
	pt_tt = &pool->caching[caching].orders[0];

	tt = ttm_tt_kunit_init(test, 0, caching, snd_size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

	KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
	KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));

	err = ttm_pool_alloc(pool, tt, &simple_ctx);
	KUNIT_ASSERT_EQ(test, err, 0);

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);

	KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
	KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));

	ttm_pool_fini(pool);
}

static void ttm_pool_free_dma_alloc(struct kunit *test)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_test_devices *devs = priv->devs;
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_pool_type *pt;
	enum ttm_caching caching = ttm_uncached;
	unsigned int order = 2;
	size_t size = (1 << order) * PAGE_SIZE;

	tt = ttm_tt_kunit_init(test, 0, caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

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

	ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);
	ttm_pool_alloc(pool, tt, &simple_ctx);

	pt = &pool->caching[caching].orders[order];
	KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);

	KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));

	ttm_pool_fini(pool);
}

static void ttm_pool_free_no_dma_alloc(struct kunit *test)
{
	struct ttm_pool_test_priv *priv = test->priv;
	struct ttm_test_devices *devs = priv->devs;
	struct ttm_tt *tt;
	struct ttm_pool *pool;
	struct ttm_pool_type *pt;
	enum ttm_caching caching = ttm_uncached;
	unsigned int order = 2;
	size_t size = (1 << order) * PAGE_SIZE;

	tt = ttm_tt_kunit_init(test, 0, caching, size);
	KUNIT_ASSERT_NOT_NULL(test, tt);

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

	ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, false, false);
	ttm_pool_alloc(pool, tt, &simple_ctx);

	pt = &pool->caching[caching].orders[order];
	KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));

	ttm_pool_free(pool, tt);
	ttm_tt_fini(tt);

	KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));

	ttm_pool_fini(pool);
}

static void ttm_pool_fini_basic(struct kunit *test)
{
	struct ttm_pool *pool;
	struct ttm_pool_type *pt;
	enum ttm_caching caching = ttm_uncached;
	unsigned int order = 0;
	size_t size = PAGE_SIZE;

	pool = ttm_pool_pre_populated(test, size, caching);
	pt = &pool->caching[caching].orders[order];

	KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));

	ttm_pool_fini(pool);

	KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
}

static struct kunit_case ttm_pool_test_cases[] = {
	KUNIT_CASE_PARAM(ttm_pool_alloc_basic, ttm_pool_alloc_basic_gen_params),
	KUNIT_CASE_PARAM(ttm_pool_alloc_basic_dma_addr,
			 ttm_pool_alloc_basic_gen_params),
	KUNIT_CASE(ttm_pool_alloc_order_caching_match),
	KUNIT_CASE(ttm_pool_alloc_caching_mismatch),
	KUNIT_CASE(ttm_pool_alloc_order_mismatch),
	KUNIT_CASE(ttm_pool_free_dma_alloc),
	KUNIT_CASE(ttm_pool_free_no_dma_alloc),
	KUNIT_CASE(ttm_pool_fini_basic),
	{}
};

static struct kunit_suite ttm_pool_test_suite = {
	.name = "ttm_pool",
	.init = ttm_pool_test_init,
	.exit = ttm_pool_test_fini,
	.test_cases = ttm_pool_test_cases,
};

kunit_test_suites(&ttm_pool_test_suite);

MODULE_LICENSE("GPL");