ttm_pool_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0 AND MIT
  2. /*
  3. * Copyright © 2023 Intel Corporation
  4. */
  5. #include <linux/mm.h>
  6. #include <drm/ttm/ttm_tt.h>
  7. #include <drm/ttm/ttm_pool.h>
  8. #include "ttm_kunit_helpers.h"
  9. #include "../ttm_pool_internal.h"
  10. struct ttm_pool_test_case {
  11. const char *description;
  12. unsigned int order;
  13. unsigned int alloc_flags;
  14. };
  15. struct ttm_pool_test_priv {
  16. struct ttm_test_devices *devs;
  17. /* Used to create mock ttm_tts */
  18. struct ttm_buffer_object *mock_bo;
  19. };
  20. static struct ttm_operation_ctx simple_ctx = {
  21. .interruptible = true,
  22. .no_wait_gpu = false,
  23. };
  24. static int ttm_pool_test_init(struct kunit *test)
  25. {
  26. struct ttm_pool_test_priv *priv;
  27. priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
  28. KUNIT_ASSERT_NOT_NULL(test, priv);
  29. priv->devs = ttm_test_devices_basic(test);
  30. test->priv = priv;
  31. return 0;
  32. }
  33. static void ttm_pool_test_fini(struct kunit *test)
  34. {
  35. struct ttm_pool_test_priv *priv = test->priv;
  36. ttm_test_devices_put(test, priv->devs);
  37. }
  38. static struct ttm_tt *ttm_tt_kunit_init(struct kunit *test,
  39. u32 page_flags,
  40. enum ttm_caching caching,
  41. size_t size)
  42. {
  43. struct ttm_pool_test_priv *priv = test->priv;
  44. struct ttm_buffer_object *bo;
  45. struct ttm_tt *tt;
  46. int err;
  47. bo = ttm_bo_kunit_init(test, priv->devs, size, NULL);
  48. KUNIT_ASSERT_NOT_NULL(test, bo);
  49. priv->mock_bo = bo;
  50. tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
  51. KUNIT_ASSERT_NOT_NULL(test, tt);
  52. err = ttm_tt_init(tt, priv->mock_bo, page_flags, caching, 0);
  53. KUNIT_ASSERT_EQ(test, err, 0);
  54. return tt;
  55. }
  56. static struct ttm_pool *ttm_pool_pre_populated(struct kunit *test,
  57. size_t size,
  58. enum ttm_caching caching)
  59. {
  60. struct ttm_pool_test_priv *priv = test->priv;
  61. struct ttm_test_devices *devs = priv->devs;
  62. struct ttm_pool *pool;
  63. struct ttm_tt *tt;
  64. int err;
  65. tt = ttm_tt_kunit_init(test, 0, caching, size);
  66. KUNIT_ASSERT_NOT_NULL(test, tt);
  67. pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
  68. KUNIT_ASSERT_NOT_NULL(test, pool);
  69. ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, TTM_ALLOCATION_POOL_USE_DMA_ALLOC);
  70. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  71. KUNIT_ASSERT_EQ(test, err, 0);
  72. ttm_pool_free(pool, tt);
  73. ttm_tt_fini(tt);
  74. return pool;
  75. }
  76. static const struct ttm_pool_test_case ttm_pool_basic_cases[] = {
  77. {
  78. .description = "One page",
  79. .order = 0,
  80. },
  81. {
  82. .description = "More than one page",
  83. .order = 2,
  84. },
  85. {
  86. .description = "Above the allocation limit",
  87. .order = MAX_PAGE_ORDER + 1,
  88. },
  89. {
  90. .description = "One page, with coherent DMA mappings enabled",
  91. .order = 0,
  92. .alloc_flags = TTM_ALLOCATION_POOL_USE_DMA_ALLOC,
  93. },
  94. {
  95. .description = "Above the allocation limit, with coherent DMA mappings enabled",
  96. .order = MAX_PAGE_ORDER + 1,
  97. .alloc_flags = TTM_ALLOCATION_POOL_USE_DMA_ALLOC,
  98. },
  99. };
  100. static void ttm_pool_alloc_case_desc(const struct ttm_pool_test_case *t,
  101. char *desc)
  102. {
  103. strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
  104. }
  105. KUNIT_ARRAY_PARAM(ttm_pool_alloc_basic, ttm_pool_basic_cases,
  106. ttm_pool_alloc_case_desc);
  107. static void ttm_pool_alloc_basic(struct kunit *test)
  108. {
  109. struct ttm_pool_test_priv *priv = test->priv;
  110. struct ttm_test_devices *devs = priv->devs;
  111. const struct ttm_pool_test_case *params = test->param_value;
  112. struct ttm_tt *tt;
  113. struct ttm_pool *pool;
  114. struct page *fst_page, *last_page;
  115. enum ttm_caching caching = ttm_uncached;
  116. unsigned int expected_num_pages = 1 << params->order;
  117. size_t size = expected_num_pages * PAGE_SIZE;
  118. int err;
  119. tt = ttm_tt_kunit_init(test, 0, caching, size);
  120. KUNIT_ASSERT_NOT_NULL(test, tt);
  121. pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
  122. KUNIT_ASSERT_NOT_NULL(test, pool);
  123. ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, params->alloc_flags);
  124. KUNIT_ASSERT_PTR_EQ(test, pool->dev, devs->dev);
  125. KUNIT_ASSERT_EQ(test, pool->nid, NUMA_NO_NODE);
  126. KUNIT_ASSERT_EQ(test, pool->alloc_flags, params->alloc_flags);
  127. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  128. KUNIT_ASSERT_EQ(test, err, 0);
  129. KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);
  130. fst_page = tt->pages[0];
  131. last_page = tt->pages[tt->num_pages - 1];
  132. if (params->order <= MAX_PAGE_ORDER) {
  133. if (ttm_pool_uses_dma_alloc(pool)) {
  134. KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
  135. KUNIT_ASSERT_NOT_NULL(test, (void *)last_page->private);
  136. } else {
  137. KUNIT_ASSERT_EQ(test, fst_page->private, params->order);
  138. }
  139. } else {
  140. if (ttm_pool_uses_dma_alloc(pool)) {
  141. KUNIT_ASSERT_NOT_NULL(test, (void *)fst_page->private);
  142. KUNIT_ASSERT_NULL(test, (void *)last_page->private);
  143. } else {
  144. /*
  145. * We expect to alloc one big block, followed by
  146. * order 0 blocks
  147. */
  148. KUNIT_ASSERT_EQ(test, fst_page->private,
  149. min_t(unsigned int, MAX_PAGE_ORDER,
  150. params->order));
  151. KUNIT_ASSERT_EQ(test, last_page->private, 0);
  152. }
  153. }
  154. ttm_pool_free(pool, tt);
  155. ttm_tt_fini(tt);
  156. ttm_pool_fini(pool);
  157. }
  158. static void ttm_pool_alloc_basic_dma_addr(struct kunit *test)
  159. {
  160. struct ttm_pool_test_priv *priv = test->priv;
  161. struct ttm_test_devices *devs = priv->devs;
  162. const struct ttm_pool_test_case *params = test->param_value;
  163. struct ttm_tt *tt;
  164. struct ttm_pool *pool;
  165. struct ttm_buffer_object *bo;
  166. dma_addr_t dma1, dma2;
  167. enum ttm_caching caching = ttm_uncached;
  168. unsigned int expected_num_pages = 1 << params->order;
  169. size_t size = expected_num_pages * PAGE_SIZE;
  170. int err;
  171. tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
  172. KUNIT_ASSERT_NOT_NULL(test, tt);
  173. bo = ttm_bo_kunit_init(test, devs, size, NULL);
  174. KUNIT_ASSERT_NOT_NULL(test, bo);
  175. err = ttm_sg_tt_init(tt, bo, 0, caching);
  176. KUNIT_ASSERT_EQ(test, err, 0);
  177. pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
  178. KUNIT_ASSERT_NOT_NULL(test, pool);
  179. ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, TTM_ALLOCATION_POOL_USE_DMA_ALLOC);
  180. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  181. KUNIT_ASSERT_EQ(test, err, 0);
  182. KUNIT_ASSERT_EQ(test, tt->num_pages, expected_num_pages);
  183. dma1 = tt->dma_address[0];
  184. dma2 = tt->dma_address[tt->num_pages - 1];
  185. KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma1);
  186. KUNIT_ASSERT_NOT_NULL(test, (void *)(uintptr_t)dma2);
  187. ttm_pool_free(pool, tt);
  188. ttm_tt_fini(tt);
  189. ttm_pool_fini(pool);
  190. }
  191. static void ttm_pool_alloc_order_caching_match(struct kunit *test)
  192. {
  193. struct ttm_tt *tt;
  194. struct ttm_pool *pool;
  195. struct ttm_pool_type *pt;
  196. enum ttm_caching caching = ttm_uncached;
  197. unsigned int order = 0;
  198. size_t size = PAGE_SIZE;
  199. int err;
  200. pool = ttm_pool_pre_populated(test, size, caching);
  201. pt = &pool->caching[caching].orders[order];
  202. KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
  203. tt = ttm_tt_kunit_init(test, 0, caching, size);
  204. KUNIT_ASSERT_NOT_NULL(test, tt);
  205. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  206. KUNIT_ASSERT_EQ(test, err, 0);
  207. KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
  208. ttm_pool_free(pool, tt);
  209. ttm_tt_fini(tt);
  210. ttm_pool_fini(pool);
  211. }
  212. static void ttm_pool_alloc_caching_mismatch(struct kunit *test)
  213. {
  214. struct ttm_tt *tt;
  215. struct ttm_pool *pool;
  216. struct ttm_pool_type *pt_pool, *pt_tt;
  217. enum ttm_caching tt_caching = ttm_uncached;
  218. enum ttm_caching pool_caching = ttm_cached;
  219. size_t size = PAGE_SIZE;
  220. unsigned int order = 0;
  221. int err;
  222. pool = ttm_pool_pre_populated(test, size, pool_caching);
  223. pt_pool = &pool->caching[pool_caching].orders[order];
  224. pt_tt = &pool->caching[tt_caching].orders[order];
  225. tt = ttm_tt_kunit_init(test, 0, tt_caching, size);
  226. KUNIT_ASSERT_NOT_NULL(test, tt);
  227. KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
  228. KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));
  229. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  230. KUNIT_ASSERT_EQ(test, err, 0);
  231. ttm_pool_free(pool, tt);
  232. ttm_tt_fini(tt);
  233. KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
  234. KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));
  235. ttm_pool_fini(pool);
  236. }
  237. static void ttm_pool_alloc_order_mismatch(struct kunit *test)
  238. {
  239. struct ttm_tt *tt;
  240. struct ttm_pool *pool;
  241. struct ttm_pool_type *pt_pool, *pt_tt;
  242. enum ttm_caching caching = ttm_uncached;
  243. unsigned int order = 2;
  244. size_t fst_size = (1 << order) * PAGE_SIZE;
  245. size_t snd_size = PAGE_SIZE;
  246. int err;
  247. pool = ttm_pool_pre_populated(test, fst_size, caching);
  248. pt_pool = &pool->caching[caching].orders[order];
  249. pt_tt = &pool->caching[caching].orders[0];
  250. tt = ttm_tt_kunit_init(test, 0, caching, snd_size);
  251. KUNIT_ASSERT_NOT_NULL(test, tt);
  252. KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
  253. KUNIT_ASSERT_TRUE(test, list_empty(&pt_tt->pages));
  254. err = ttm_pool_alloc(pool, tt, &simple_ctx);
  255. KUNIT_ASSERT_EQ(test, err, 0);
  256. ttm_pool_free(pool, tt);
  257. ttm_tt_fini(tt);
  258. KUNIT_ASSERT_FALSE(test, list_empty(&pt_pool->pages));
  259. KUNIT_ASSERT_FALSE(test, list_empty(&pt_tt->pages));
  260. ttm_pool_fini(pool);
  261. }
  262. static void ttm_pool_free_dma_alloc(struct kunit *test)
  263. {
  264. struct ttm_pool_test_priv *priv = test->priv;
  265. struct ttm_test_devices *devs = priv->devs;
  266. struct ttm_tt *tt;
  267. struct ttm_pool *pool;
  268. struct ttm_pool_type *pt;
  269. enum ttm_caching caching = ttm_uncached;
  270. unsigned int order = 2;
  271. size_t size = (1 << order) * PAGE_SIZE;
  272. tt = ttm_tt_kunit_init(test, 0, caching, size);
  273. KUNIT_ASSERT_NOT_NULL(test, tt);
  274. pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
  275. KUNIT_ASSERT_NOT_NULL(test, pool);
  276. ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, TTM_ALLOCATION_POOL_USE_DMA_ALLOC);
  277. ttm_pool_alloc(pool, tt, &simple_ctx);
  278. pt = &pool->caching[caching].orders[order];
  279. KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
  280. ttm_pool_free(pool, tt);
  281. ttm_tt_fini(tt);
  282. KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
  283. ttm_pool_fini(pool);
  284. }
  285. static void ttm_pool_free_no_dma_alloc(struct kunit *test)
  286. {
  287. struct ttm_pool_test_priv *priv = test->priv;
  288. struct ttm_test_devices *devs = priv->devs;
  289. struct ttm_tt *tt;
  290. struct ttm_pool *pool;
  291. struct ttm_pool_type *pt;
  292. enum ttm_caching caching = ttm_uncached;
  293. unsigned int order = 2;
  294. size_t size = (1 << order) * PAGE_SIZE;
  295. tt = ttm_tt_kunit_init(test, 0, caching, size);
  296. KUNIT_ASSERT_NOT_NULL(test, tt);
  297. pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
  298. KUNIT_ASSERT_NOT_NULL(test, pool);
  299. ttm_pool_init(pool, devs->dev, NUMA_NO_NODE, 0);
  300. ttm_pool_alloc(pool, tt, &simple_ctx);
  301. pt = &pool->caching[caching].orders[order];
  302. KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));
  303. ttm_pool_free(pool, tt);
  304. ttm_tt_fini(tt);
  305. KUNIT_ASSERT_TRUE(test, list_is_singular(&pt->pages));
  306. ttm_pool_fini(pool);
  307. }
  308. static void ttm_pool_fini_basic(struct kunit *test)
  309. {
  310. struct ttm_pool *pool;
  311. struct ttm_pool_type *pt;
  312. enum ttm_caching caching = ttm_uncached;
  313. unsigned int order = 0;
  314. size_t size = PAGE_SIZE;
  315. pool = ttm_pool_pre_populated(test, size, caching);
  316. pt = &pool->caching[caching].orders[order];
  317. KUNIT_ASSERT_FALSE(test, list_empty(&pt->pages));
  318. ttm_pool_fini(pool);
  319. KUNIT_ASSERT_TRUE(test, list_empty(&pt->pages));
  320. }
  321. static struct kunit_case ttm_pool_test_cases[] = {
  322. KUNIT_CASE_PARAM(ttm_pool_alloc_basic, ttm_pool_alloc_basic_gen_params),
  323. KUNIT_CASE_PARAM(ttm_pool_alloc_basic_dma_addr,
  324. ttm_pool_alloc_basic_gen_params),
  325. KUNIT_CASE(ttm_pool_alloc_order_caching_match),
  326. KUNIT_CASE(ttm_pool_alloc_caching_mismatch),
  327. KUNIT_CASE(ttm_pool_alloc_order_mismatch),
  328. KUNIT_CASE(ttm_pool_free_dma_alloc),
  329. KUNIT_CASE(ttm_pool_free_no_dma_alloc),
  330. KUNIT_CASE(ttm_pool_fini_basic),
  331. {}
  332. };
  333. static struct kunit_suite ttm_pool_test_suite = {
  334. .name = "ttm_pool",
  335. .init = ttm_pool_test_init,
  336. .exit = ttm_pool_test_fini,
  337. .test_cases = ttm_pool_test_cases,
  338. };
  339. kunit_test_suites(&ttm_pool_test_suite);
  340. MODULE_DESCRIPTION("KUnit tests for ttm_pool APIs");
  341. MODULE_LICENSE("GPL and additional rights");