ttm_device_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0 AND MIT
  2. /*
  3. * Copyright © 2023 Intel Corporation
  4. */
  5. #include <drm/ttm/ttm_resource.h>
  6. #include <drm/ttm/ttm_device.h>
  7. #include <drm/ttm/ttm_placement.h>
  8. #include "ttm_kunit_helpers.h"
  9. #include "../ttm_pool_internal.h"
  10. struct ttm_device_test_case {
  11. const char *description;
  12. unsigned int alloc_flags;
  13. bool pools_init_expected;
  14. };
  15. static void ttm_device_init_basic(struct kunit *test)
  16. {
  17. struct ttm_test_devices *priv = test->priv;
  18. struct ttm_device *ttm_dev;
  19. struct ttm_resource_manager *ttm_sys_man;
  20. int err;
  21. ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
  22. KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
  23. err = ttm_device_kunit_init(priv, ttm_dev, 0);
  24. KUNIT_ASSERT_EQ(test, err, 0);
  25. KUNIT_EXPECT_PTR_EQ(test, ttm_dev->funcs, &ttm_dev_funcs);
  26. KUNIT_ASSERT_NOT_NULL(test, ttm_dev->wq);
  27. KUNIT_ASSERT_NOT_NULL(test, ttm_dev->man_drv[TTM_PL_SYSTEM]);
  28. ttm_sys_man = &ttm_dev->sysman;
  29. KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man);
  30. KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_tt);
  31. KUNIT_EXPECT_TRUE(test, ttm_sys_man->use_type);
  32. KUNIT_ASSERT_NOT_NULL(test, ttm_sys_man->func);
  33. KUNIT_EXPECT_PTR_EQ(test, ttm_dev->dev_mapping,
  34. priv->drm->anon_inode->i_mapping);
  35. ttm_device_fini(ttm_dev);
  36. }
  37. static void ttm_device_init_multiple(struct kunit *test)
  38. {
  39. struct ttm_test_devices *priv = test->priv;
  40. struct ttm_device *ttm_devs;
  41. unsigned int i, num_dev = 3;
  42. int err;
  43. ttm_devs = kunit_kcalloc(test, num_dev, sizeof(*ttm_devs), GFP_KERNEL);
  44. KUNIT_ASSERT_NOT_NULL(test, ttm_devs);
  45. for (i = 0; i < num_dev; i++) {
  46. err = ttm_device_kunit_init(priv, &ttm_devs[i], 0);
  47. KUNIT_ASSERT_EQ(test, err, 0);
  48. KUNIT_EXPECT_PTR_EQ(test, ttm_devs[i].dev_mapping,
  49. priv->drm->anon_inode->i_mapping);
  50. KUNIT_ASSERT_NOT_NULL(test, ttm_devs[i].wq);
  51. KUNIT_EXPECT_PTR_EQ(test, ttm_devs[i].funcs, &ttm_dev_funcs);
  52. KUNIT_ASSERT_NOT_NULL(test, ttm_devs[i].man_drv[TTM_PL_SYSTEM]);
  53. }
  54. KUNIT_ASSERT_EQ(test, list_count_nodes(&ttm_devs[0].device_list), num_dev);
  55. for (i = 0; i < num_dev; i++)
  56. ttm_device_fini(&ttm_devs[i]);
  57. }
  58. static void ttm_device_fini_basic(struct kunit *test)
  59. {
  60. struct ttm_test_devices *priv = test->priv;
  61. struct ttm_device *ttm_dev;
  62. struct ttm_resource_manager *man;
  63. int err;
  64. ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
  65. KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
  66. err = ttm_device_kunit_init(priv, ttm_dev, 0);
  67. KUNIT_ASSERT_EQ(test, err, 0);
  68. man = ttm_manager_type(ttm_dev, TTM_PL_SYSTEM);
  69. KUNIT_ASSERT_NOT_NULL(test, man);
  70. ttm_device_fini(ttm_dev);
  71. KUNIT_ASSERT_FALSE(test, man->use_type);
  72. KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[0]));
  73. KUNIT_ASSERT_NULL(test, ttm_dev->man_drv[TTM_PL_SYSTEM]);
  74. }
  75. static void ttm_device_init_no_vma_man(struct kunit *test)
  76. {
  77. struct ttm_test_devices *priv = test->priv;
  78. struct drm_device *drm = priv->drm;
  79. struct ttm_device *ttm_dev;
  80. struct drm_vma_offset_manager *vma_man;
  81. int err;
  82. ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
  83. KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
  84. /* Let's pretend there's no VMA manager allocated */
  85. vma_man = drm->vma_offset_manager;
  86. drm->vma_offset_manager = NULL;
  87. err = ttm_device_kunit_init(priv, ttm_dev, 0);
  88. KUNIT_EXPECT_EQ(test, err, -EINVAL);
  89. /* Bring the manager back for a graceful cleanup */
  90. drm->vma_offset_manager = vma_man;
  91. }
  92. static const struct ttm_device_test_case ttm_device_cases[] = {
  93. {
  94. .description = "No DMA allocations, no DMA32 required",
  95. .pools_init_expected = false,
  96. },
  97. {
  98. .description = "DMA allocations, DMA32 required",
  99. .alloc_flags = TTM_ALLOCATION_POOL_USE_DMA_ALLOC |
  100. TTM_ALLOCATION_POOL_USE_DMA32,
  101. .pools_init_expected = true,
  102. },
  103. {
  104. .description = "No DMA allocations, DMA32 required",
  105. .alloc_flags = TTM_ALLOCATION_POOL_USE_DMA32,
  106. .pools_init_expected = false,
  107. },
  108. {
  109. .description = "DMA allocations, no DMA32 required",
  110. .alloc_flags = TTM_ALLOCATION_POOL_USE_DMA_ALLOC,
  111. .pools_init_expected = true,
  112. },
  113. };
  114. static void ttm_device_case_desc(const struct ttm_device_test_case *t, char *desc)
  115. {
  116. strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
  117. }
  118. KUNIT_ARRAY_PARAM(ttm_device, ttm_device_cases, ttm_device_case_desc);
  119. static void ttm_device_init_pools(struct kunit *test)
  120. {
  121. struct ttm_test_devices *priv = test->priv;
  122. const struct ttm_device_test_case *params = test->param_value;
  123. struct ttm_device *ttm_dev;
  124. struct ttm_pool *pool;
  125. struct ttm_pool_type pt;
  126. int err;
  127. ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
  128. KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
  129. err = ttm_device_kunit_init(priv, ttm_dev, params->alloc_flags);
  130. KUNIT_ASSERT_EQ(test, err, 0);
  131. pool = &ttm_dev->pool;
  132. KUNIT_ASSERT_NOT_NULL(test, pool);
  133. KUNIT_EXPECT_PTR_EQ(test, pool->dev, priv->dev);
  134. KUNIT_EXPECT_EQ(test, pool->alloc_flags, params->alloc_flags);
  135. if (params->pools_init_expected) {
  136. for (int i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
  137. for (int j = 0; j < NR_PAGE_ORDERS; ++j) {
  138. pt = pool->caching[i].orders[j];
  139. KUNIT_EXPECT_PTR_EQ(test, pt.pool, pool);
  140. KUNIT_EXPECT_EQ(test, pt.caching, i);
  141. KUNIT_EXPECT_EQ(test, pt.order, j);
  142. if (ttm_pool_uses_dma_alloc(pool))
  143. KUNIT_ASSERT_FALSE(test,
  144. list_empty(&pt.pages));
  145. }
  146. }
  147. }
  148. ttm_device_fini(ttm_dev);
  149. }
  150. static struct kunit_case ttm_device_test_cases[] = {
  151. KUNIT_CASE(ttm_device_init_basic),
  152. KUNIT_CASE(ttm_device_init_multiple),
  153. KUNIT_CASE(ttm_device_fini_basic),
  154. KUNIT_CASE(ttm_device_init_no_vma_man),
  155. KUNIT_CASE_PARAM(ttm_device_init_pools, ttm_device_gen_params),
  156. {}
  157. };
  158. static struct kunit_suite ttm_device_test_suite = {
  159. .name = "ttm_device",
  160. .init = ttm_test_devices_init,
  161. .exit = ttm_test_devices_fini,
  162. .test_cases = ttm_device_test_cases,
  163. };
  164. kunit_test_suites(&ttm_device_test_suite);
  165. MODULE_DESCRIPTION("KUnit tests for ttm_device APIs");
  166. MODULE_LICENSE("GPL and additional rights");