1 // SPDX-License-Identifier: GPL-2.0 AND MIT
3 * Copyright © 2023 Intel Corporation
7 #include <drm/ttm/ttm_tt.h>
8 #include <drm/ttm/ttm_pool.h>
10 #include "ttm_kunit_helpers.h"
12 struct ttm_pool_test_case {
13 const char *description;
18 struct ttm_pool_test_priv {
19 struct ttm_test_devices *devs;
21 /* Used to create mock ttm_tts */
22 struct ttm_buffer_object *mock_bo;
25 static struct ttm_operation_ctx simple_ctx = {
26 .interruptible = true,
30 static int ttm_pool_test_init(struct kunit *test)
32 struct ttm_pool_test_priv *priv;
34 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
35 KUNIT_ASSERT_NOT_NULL(test, priv);
37 priv->devs = ttm_test_devices_basic(test);
43 static void ttm_pool_test_fini(struct kunit *test)
45 struct ttm_pool_test_priv *priv = test->priv;
47 ttm_test_devices_put(test, priv->devs);
50 static struct ttm_tt *ttm_tt_kunit_init(struct kunit *test,
52 enum ttm_caching caching,
55 struct ttm_pool_test_priv *priv = test->priv;
56 struct ttm_buffer_object *bo;
60 bo = ttm_bo_kunit_init(test, priv->devs, size, NULL);
61 KUNIT_ASSERT_NOT_NULL(test, bo);
64 tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
65 KUNIT_ASSERT_NOT_NULL(test, tt);
67 err = ttm_tt_init(tt, priv->mock_bo, page_flags, caching, 0);
68 KUNIT_ASSERT_EQ(test, err, 0);
73 static struct ttm_pool *ttm_pool_pre_populated(struct kunit *test,
75 enum ttm_caching caching)
77 struct ttm_pool_test_priv *priv = test->priv;
78 struct ttm_test_devices *devs = priv->devs;
79 struct ttm_pool *pool;
83 tt = ttm_tt_kunit_init(test, 0, caching, size);
84 KUNIT_ASSERT_NOT_NULL(test, tt);
86 pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
87 KUNIT_ASSERT_NOT_NULL(test, pool);
89 ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);
91 err = ttm_pool_alloc(pool, tt, &simple_ctx);
92 KUNIT_ASSERT_EQ(test, err, 0);
94 ttm_pool_free(pool, tt);
100 static const struct ttm_pool_test_case ttm_pool_basic_cases[] = {
102 .description = "One page",
106 .description = "More than one page",
110 .description = "Above the allocation limit",
111 .order = MAX_PAGE_ORDER + 1,
114 .description = "One page, with coherent DMA mappings enabled",
116 .use_dma_alloc = true,
119 .description = "Above the allocation limit, with coherent DMA mappings enabled",
120 .order = MAX_PAGE_ORDER + 1,
121 .use_dma_alloc = true,
125 static void ttm_pool_alloc_case_desc(const struct ttm_pool_test_case *t,
128 strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
131 KUNIT_ARRAY_PARAM(ttm_pool_alloc_basic, ttm_pool_basic_cases,
132 ttm_pool_alloc_case_desc);
134 static void ttm_pool_alloc_basic(struct kunit *test)
136 struct ttm_pool_test_priv *priv = test->priv;
137 struct ttm_test_devices *devs = priv->devs;
138 const struct ttm_pool_test_case *params = test->param_value;
140 struct ttm_pool *pool;
141 struct page *fst_page, *last_page;
142 enum ttm_caching caching = ttm_uncached;
143 unsigned int expected_num_pages = 1 << params->order;
144 size_t size = expected_num_pages * PAGE_SIZE;
147 tt = ttm_tt_kunit_init(test, 0, caching, size);
148 KUNIT_ASSERT_NOT_NULL(test, tt);
150 pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
151 KUNIT_ASSERT_NOT_NULL(test, pool);
153 ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, params->use_dma_alloc,
156 KUNIT_ASSERT_PTR_EQ(test, pool->dev, devs->dev);
157 KUNIT_ASSERT_EQ(test, pool->nid, NUMA_NO_NODE);
158 KUNIT_ASSERT_EQ(test, pool->use_dma_alloc, params->use_dma_alloc);
160 err = ttm_pool_alloc(pool, tt, &simple_ctx);
161 KUNIT_ASSERT_EQ(test, err, 0);
162 KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);
164 fst_page = tt->pages[0];
165 last_page = tt->pages[tt->num_pages - 1];
167 if (params->order <= MAX_PAGE_ORDER) {
168 if (params->use_dma_alloc) {
169 KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
170 KUNIT_ASSERT_NOT_NULL(test, (void *)last_page->private);
172 KUNIT_ASSERT_EQ(test, fst_page->private, params->order);
175 if (params->use_dma_alloc) {
176 KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
177 KUNIT_ASSERT_NULL(test, (void *)last_page->private);
180 * We expect to alloc one big block, followed by
183 KUNIT_ASSERT_EQ(test, fst_page->private,
184 min_t(unsigned int, MAX_PAGE_ORDER,
186 KUNIT_ASSERT_EQ(test, last_page->private, 0);
190 ttm_pool_free(pool, tt);
195 static void ttm_pool_alloc_basic_dma_addr(struct kunit *test)
197 struct ttm_pool_test_priv *priv = test->priv;
198 struct ttm_test_devices *devs = priv->devs;
199 const struct ttm_pool_test_case *params = test->param_value;
201 struct ttm_pool *pool;
202 struct ttm_buffer_object *bo;
203 dma_addr_t dma1, dma2;
204 enum ttm_caching caching = ttm_uncached;
205 unsigned int expected_num_pages = 1 << params->order;
206 size_t size = expected_num_pages * PAGE_SIZE;
209 tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
210 KUNIT_ASSERT_NOT_NULL(test, tt);
212 bo = ttm_bo_kunit_init(test, devs, size, NULL);
213 KUNIT_ASSERT_NOT_NULL(test, bo);
215 err = ttm_sg_tt_init(tt, bo, 0, caching);
216 KUNIT_ASSERT_EQ(test, err, 0);
218 pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
219 KUNIT_ASSERT_NOT_NULL(test, pool);
221 ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);
223 err = ttm_pool_alloc(pool, tt, &simple_ctx);
224 KUNIT_ASSERT_EQ(test, err, 0);
225 KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);
227 dma1 = tt->dma_address[0];
228 dma2 = tt->dma_address[tt->num_pages - 1];
230 KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma1);
231 KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma2);
233 ttm_pool_free(pool, tt);
238 static void ttm_pool_alloc_order_caching_match(struct kunit *test)
241 struct ttm_pool *pool;
242 struct ttm_pool_type *pt;
243 enum ttm_caching caching = ttm_uncached;
244 unsigned int order = 0;
245 size_t size = PAGE_SIZE;
248 pool = ttm_pool_pre_populated(test, size, caching);
250 pt = &pool->caching[caching].orders[order];
251 KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
253 tt = ttm_tt_kunit_init(test, 0, caching, size);
254 KUNIT_ASSERT_NOT_NULL(test, tt);
256 err = ttm_pool_alloc(pool, tt, &simple_ctx);
257 KUNIT_ASSERT_EQ(test, err, 0);
259 KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
261 ttm_pool_free(pool, tt);
266 static void ttm_pool_alloc_caching_mismatch(struct kunit *test)
269 struct ttm_pool *pool;
270 struct ttm_pool_type *pt_pool, *pt_tt;
271 enum ttm_caching tt_caching = ttm_uncached;
272 enum ttm_caching pool_caching = ttm_cached;
273 size_t size = PAGE_SIZE;
274 unsigned int order = 0;
277 pool = ttm_pool_pre_populated(test, size, pool_caching);
279 pt_pool = &pool->caching[pool_caching].orders[order];
280 pt_tt = &pool->caching[tt_caching].orders[order];
282 tt = ttm_tt_kunit_init(test, 0, tt_caching, size);
283 KUNIT_ASSERT_NOT_NULL(test, tt);
285 KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
286 KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));
288 err = ttm_pool_alloc(pool, tt, &simple_ctx);
289 KUNIT_ASSERT_EQ(test, err, 0);
291 ttm_pool_free(pool, tt);
294 KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
295 KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));
300 static void ttm_pool_alloc_order_mismatch(struct kunit *test)
303 struct ttm_pool *pool;
304 struct ttm_pool_type *pt_pool, *pt_tt;
305 enum ttm_caching caching = ttm_uncached;
306 unsigned int order = 2;
307 size_t fst_size = (1 << order) * PAGE_SIZE;
308 size_t snd_size = PAGE_SIZE;
311 pool = ttm_pool_pre_populated(test, fst_size, caching);
313 pt_pool = &pool->caching[caching].orders[order];
314 pt_tt = &pool->caching[caching].orders[0];
316 tt = ttm_tt_kunit_init(test, 0, caching, snd_size);
317 KUNIT_ASSERT_NOT_NULL(test, tt);
319 KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
320 KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));
322 err = ttm_pool_alloc(pool, tt, &simple_ctx);
323 KUNIT_ASSERT_EQ(test, err, 0);
325 ttm_pool_free(pool, tt);
328 KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
329 KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));
334 static void ttm_pool_free_dma_alloc(struct kunit *test)
336 struct ttm_pool_test_priv *priv = test->priv;
337 struct ttm_test_devices *devs = priv->devs;
339 struct ttm_pool *pool;
340 struct ttm_pool_type *pt;
341 enum ttm_caching caching = ttm_uncached;
342 unsigned int order = 2;
343 size_t size = (1 << order) * PAGE_SIZE;
345 tt = ttm_tt_kunit_init(test, 0, caching, size);
346 KUNIT_ASSERT_NOT_NULL(test, tt);
348 pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
349 KUNIT_ASSERT_NOT_NULL(test, pool);
351 ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, true, false);
352 ttm_pool_alloc(pool, tt, &simple_ctx);
354 pt = &pool->caching[caching].orders[order];
355 KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
357 ttm_pool_free(pool, tt);
360 KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
365 static void ttm_pool_free_no_dma_alloc(struct kunit *test)
367 struct ttm_pool_test_priv *priv = test->priv;
368 struct ttm_test_devices *devs = priv->devs;
370 struct ttm_pool *pool;
371 struct ttm_pool_type *pt;
372 enum ttm_caching caching = ttm_uncached;
373 unsigned int order = 2;
374 size_t size = (1 << order) * PAGE_SIZE;
376 tt = ttm_tt_kunit_init(test, 0, caching, size);
377 KUNIT_ASSERT_NOT_NULL(test, tt);
379 pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
380 KUNIT_ASSERT_NOT_NULL(test, pool);
382 ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, false, false);
383 ttm_pool_alloc(pool, tt, &simple_ctx);
385 pt = &pool->caching[caching].orders[order];
386 KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));
388 ttm_pool_free(pool, tt);
391 KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));
396 static void ttm_pool_fini_basic(struct kunit *test)
398 struct ttm_pool *pool;
399 struct ttm_pool_type *pt;
400 enum ttm_caching caching = ttm_uncached;
401 unsigned int order = 0;
402 size_t size = PAGE_SIZE;
404 pool = ttm_pool_pre_populated(test, size, caching);
405 pt = &pool->caching[caching].orders[order];
407 KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
411 KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
414 static struct kunit_case ttm_pool_test_cases[] = {
415 KUNIT_CASE_PARAM(ttm_pool_alloc_basic, ttm_pool_alloc_basic_gen_params),
416 KUNIT_CASE_PARAM(ttm_pool_alloc_basic_dma_addr,
417 ttm_pool_alloc_basic_gen_params),
418 KUNIT_CASE(ttm_pool_alloc_order_caching_match),
419 KUNIT_CASE(ttm_pool_alloc_caching_mismatch),
420 KUNIT_CASE(ttm_pool_alloc_order_mismatch),
421 KUNIT_CASE(ttm_pool_free_dma_alloc),
422 KUNIT_CASE(ttm_pool_free_no_dma_alloc),
423 KUNIT_CASE(ttm_pool_fini_basic),
427 static struct kunit_suite ttm_pool_test_suite = {
429 .init = ttm_pool_test_init,
430 .exit = ttm_pool_test_fini,
431 .test_cases = ttm_pool_test_cases,
434 kunit_test_suites(&ttm_pool_test_suite);
436 MODULE_DESCRIPTION("KUnit tests for ttm_pool APIs");
437 MODULE_LICENSE("GPL and additional rights");