1 // SPDX-License-Identifier: GPL-2.0 AND MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 #include <drm/ttm/ttm_tt.h> 6 7 #include "ttm_kunit_helpers.h" 8 9 static const struct ttm_place sys_place = { 10 .fpfn = 0, 11 .lpfn = 0, 12 .mem_type = TTM_PL_SYSTEM, 13 .flags = TTM_PL_FLAG_FALLBACK, 14 }; 15 16 static const struct ttm_place mock1_place = { 17 .fpfn = 0, 18 .lpfn = 0, 19 .mem_type = TTM_PL_MOCK1, 20 .flags = TTM_PL_FLAG_FALLBACK, 21 }; 22 23 static const struct ttm_place mock2_place = { 24 .fpfn = 0, 25 .lpfn = 0, 26 .mem_type = TTM_PL_MOCK2, 27 .flags = TTM_PL_FLAG_FALLBACK, 28 }; 29 30 static struct ttm_placement sys_placement = { 31 .num_placement = 1, 32 .placement = &sys_place, 33 }; 34 35 static struct ttm_placement bad_placement = { 36 .num_placement = 1, 37 .placement = &mock1_place, 38 }; 39 40 static struct ttm_placement mock_placement = { 41 .num_placement = 1, 42 .placement = &mock2_place, 43 }; 44 45 static struct ttm_tt *ttm_tt_simple_create(struct ttm_buffer_object *bo, 46 uint32_t page_flags) 47 { 48 struct ttm_tt *tt; 49 50 tt = kzalloc(sizeof(*tt), GFP_KERNEL); 51 ttm_tt_init(tt, bo, page_flags, ttm_cached, 0); 52 53 return tt; 54 } 55 56 static void ttm_tt_simple_destroy(struct ttm_device *bdev, struct ttm_tt *ttm) 57 { 58 kfree(ttm); 59 } 60 61 static int mock_move(struct ttm_buffer_object *bo, bool evict, 62 struct ttm_operation_ctx *ctx, 63 struct ttm_resource *new_mem, 64 struct ttm_place *hop) 65 { 66 struct ttm_resource *old_mem = bo->resource; 67 68 if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm)) { 69 ttm_bo_move_null(bo, new_mem); 70 return 0; 71 } 72 73 if (bo->resource->mem_type == TTM_PL_VRAM && 74 new_mem->mem_type == TTM_PL_SYSTEM) { 75 hop->mem_type = TTM_PL_TT; 76 hop->flags = TTM_PL_FLAG_TEMPORARY; 77 hop->fpfn = 0; 78 hop->lpfn = 0; 79 return -EMULTIHOP; 80 } 81 82 if ((old_mem->mem_type == TTM_PL_SYSTEM && 83 new_mem->mem_type == TTM_PL_TT) || 84 (old_mem->mem_type == TTM_PL_TT && 85 new_mem->mem_type == TTM_PL_SYSTEM)) { 86 ttm_bo_move_null(bo, new_mem); 87 return 0; 88 } 89 90 return ttm_bo_move_memcpy(bo, ctx, new_mem); 91 } 92 93 static void mock_evict_flags(struct ttm_buffer_object *bo, 94 struct ttm_placement *placement) 95 { 96 switch (bo->resource->mem_type) { 97 case TTM_PL_VRAM: 98 case TTM_PL_SYSTEM: 99 *placement = sys_placement; 100 break; 101 case TTM_PL_TT: 102 *placement = mock_placement; 103 break; 104 case TTM_PL_MOCK1: 105 /* Purge objects coming from this domain */ 106 break; 107 } 108 } 109 110 static void bad_evict_flags(struct ttm_buffer_object *bo, 111 struct ttm_placement *placement) 112 { 113 *placement = bad_placement; 114 } 115 116 static int ttm_device_kunit_init_with_funcs(struct ttm_test_devices *priv, 117 struct ttm_device *ttm, 118 bool use_dma_alloc, 119 bool use_dma32, 120 struct ttm_device_funcs *funcs) 121 { 122 struct drm_device *drm = priv->drm; 123 int err; 124 125 err = ttm_device_init(ttm, funcs, drm->dev, 126 drm->anon_inode->i_mapping, 127 drm->vma_offset_manager, 128 use_dma_alloc, use_dma32); 129 130 return err; 131 } 132 133 struct ttm_device_funcs ttm_dev_funcs = { 134 .ttm_tt_create = ttm_tt_simple_create, 135 .ttm_tt_destroy = ttm_tt_simple_destroy, 136 .move = mock_move, 137 .eviction_valuable = ttm_bo_eviction_valuable, 138 .evict_flags = mock_evict_flags, 139 }; 140 EXPORT_SYMBOL_GPL(ttm_dev_funcs); 141 142 int ttm_device_kunit_init(struct ttm_test_devices *priv, 143 struct ttm_device *ttm, 144 bool use_dma_alloc, 145 bool use_dma32) 146 { 147 return ttm_device_kunit_init_with_funcs(priv, ttm, use_dma_alloc, 148 use_dma32, &ttm_dev_funcs); 149 } 150 EXPORT_SYMBOL_GPL(ttm_device_kunit_init); 151 152 struct ttm_device_funcs ttm_dev_funcs_bad_evict = { 153 .ttm_tt_create = ttm_tt_simple_create, 154 .ttm_tt_destroy = ttm_tt_simple_destroy, 155 .move = mock_move, 156 .eviction_valuable = ttm_bo_eviction_valuable, 157 .evict_flags = bad_evict_flags, 158 }; 159 EXPORT_SYMBOL_GPL(ttm_dev_funcs_bad_evict); 160 161 int ttm_device_kunit_init_bad_evict(struct ttm_test_devices *priv, 162 struct ttm_device *ttm, 163 bool use_dma_alloc, 164 bool use_dma32) 165 { 166 return ttm_device_kunit_init_with_funcs(priv, ttm, use_dma_alloc, 167 use_dma32, &ttm_dev_funcs_bad_evict); 168 } 169 EXPORT_SYMBOL_GPL(ttm_device_kunit_init_bad_evict); 170 171 struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test, 172 struct ttm_test_devices *devs, 173 size_t size, 174 struct dma_resv *obj) 175 { 176 struct drm_gem_object gem_obj = { }; 177 struct ttm_buffer_object *bo; 178 int err; 179 180 bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 181 KUNIT_ASSERT_NOT_NULL(test, bo); 182 183 bo->base = gem_obj; 184 185 if (obj) 186 bo->base.resv = obj; 187 188 err = drm_gem_object_init(devs->drm, &bo->base, size); 189 KUNIT_ASSERT_EQ(test, err, 0); 190 191 bo->bdev = devs->ttm_dev; 192 bo->destroy = dummy_ttm_bo_destroy; 193 194 kref_init(&bo->kref); 195 196 return bo; 197 } 198 EXPORT_SYMBOL_GPL(ttm_bo_kunit_init); 199 200 struct ttm_place *ttm_place_kunit_init(struct kunit *test, 201 uint32_t mem_type, uint32_t flags) 202 { 203 struct ttm_place *place; 204 205 place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL); 206 KUNIT_ASSERT_NOT_NULL(test, place); 207 208 place->mem_type = mem_type; 209 place->flags = flags; 210 211 return place; 212 } 213 EXPORT_SYMBOL_GPL(ttm_place_kunit_init); 214 215 void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo) 216 { 217 drm_gem_object_release(&bo->base); 218 } 219 EXPORT_SYMBOL_GPL(dummy_ttm_bo_destroy); 220 221 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test) 222 { 223 struct ttm_test_devices *devs; 224 225 devs = kunit_kzalloc(test, sizeof(*devs), GFP_KERNEL); 226 KUNIT_ASSERT_NOT_NULL(test, devs); 227 228 devs->dev = drm_kunit_helper_alloc_device(test); 229 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->dev); 230 231 /* Set mask for alloc_coherent mappings to enable ttm_pool_alloc testing */ 232 devs->dev->coherent_dma_mask = -1; 233 234 devs->drm = __drm_kunit_helper_alloc_drm_device(test, devs->dev, 235 sizeof(*devs->drm), 0, 236 DRIVER_GEM); 237 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->drm); 238 239 return devs; 240 } 241 EXPORT_SYMBOL_GPL(ttm_test_devices_basic); 242 243 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test) 244 { 245 struct ttm_test_devices *devs; 246 struct ttm_device *ttm_dev; 247 int err; 248 249 devs = ttm_test_devices_basic(test); 250 251 ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL); 252 KUNIT_ASSERT_NOT_NULL(test, ttm_dev); 253 254 err = ttm_device_kunit_init(devs, ttm_dev, false, false); 255 KUNIT_ASSERT_EQ(test, err, 0); 256 257 devs->ttm_dev = ttm_dev; 258 259 return devs; 260 } 261 EXPORT_SYMBOL_GPL(ttm_test_devices_all); 262 263 void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs) 264 { 265 if (devs->ttm_dev) 266 ttm_device_fini(devs->ttm_dev); 267 268 drm_kunit_helper_free_device(test, devs->dev); 269 } 270 EXPORT_SYMBOL_GPL(ttm_test_devices_put); 271 272 int ttm_test_devices_init(struct kunit *test) 273 { 274 struct ttm_test_devices *priv; 275 276 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); 277 KUNIT_ASSERT_NOT_NULL(test, priv); 278 279 priv = ttm_test_devices_basic(test); 280 test->priv = priv; 281 282 return 0; 283 } 284 EXPORT_SYMBOL_GPL(ttm_test_devices_init); 285 286 int ttm_test_devices_all_init(struct kunit *test) 287 { 288 struct ttm_test_devices *priv; 289 290 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL); 291 KUNIT_ASSERT_NOT_NULL(test, priv); 292 293 priv = ttm_test_devices_all(test); 294 test->priv = priv; 295 296 return 0; 297 } 298 EXPORT_SYMBOL_GPL(ttm_test_devices_all_init); 299 300 void ttm_test_devices_fini(struct kunit *test) 301 { 302 ttm_test_devices_put(test, test->priv); 303 } 304 EXPORT_SYMBOL_GPL(ttm_test_devices_fini); 305 306 MODULE_LICENSE("GPL and additional rights"); 307