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