Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm/ttm/tests: Add tests with mock resource managers

Add mock resource manager to test ttm_bo_validate() with non-system
placements. Update KConfig entry to enable DRM Buddy allocator, used
by the mock manager. Update move function to do more than just assign
a resource.

Signed-off-by: Karolina Stolarek <karolina.stolarek@intel.com>
Tested-by: Somalapuram, Amaranath <asomalap@amd.com>
Reviewed-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/664791dbb7cbada29e705d2fcaf176023f1b8649.1718192625.git.karolina.stolarek@intel.com

authored by

Karolina Stolarek and committed by
Arunpravin Paneer Selvam
32d618e9 8bd1ff5d

+535 -2
+1
drivers/gpu/drm/Kconfig
··· 248 248 default n 249 249 depends on DRM && KUNIT && MMU && (UML || COMPILE_TEST) 250 250 select DRM_TTM 251 + select DRM_BUDDY 251 252 select DRM_EXPORT_FOR_TESTS if m 252 253 select DRM_KUNIT_TEST_HELPERS 253 254 default KUNIT_ALL_TESTS
+1
drivers/gpu/drm/ttm/tests/Makefile
··· 7 7 ttm_tt_test.o \ 8 8 ttm_bo_test.o \ 9 9 ttm_bo_validate_test.o \ 10 + ttm_mock_manager.o \ 10 11 ttm_kunit_helpers.o
+273
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
··· 8 8 #include <drm/ttm/ttm_tt.h> 9 9 10 10 #include "ttm_kunit_helpers.h" 11 + #include "ttm_mock_manager.h" 11 12 12 13 #define BO_SIZE SZ_4K 14 + #define MANAGER_SIZE SZ_1M 13 15 14 16 struct ttm_bo_validate_test_case { 15 17 const char *description; 16 18 enum ttm_bo_type bo_type; 19 + u32 mem_type; 17 20 bool with_ttm; 18 21 }; 19 22 ··· 105 102 ttm_bo_put(bo); 106 103 } 107 104 105 + static void ttm_bo_init_reserved_mock_man(struct kunit *test) 106 + { 107 + const struct ttm_bo_validate_test_case *params = test->param_value; 108 + enum ttm_bo_type bo_type = params->bo_type; 109 + struct ttm_test_devices *priv = test->priv; 110 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 111 + struct ttm_operation_ctx ctx = { }; 112 + struct ttm_placement *placement; 113 + u32 mem_type = TTM_PL_VRAM; 114 + struct ttm_buffer_object *bo; 115 + struct ttm_place *place; 116 + int err; 117 + 118 + ttm_mock_manager_init(priv->ttm_dev, mem_type, MANAGER_SIZE); 119 + 120 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 121 + KUNIT_ASSERT_NOT_NULL(test, bo); 122 + 123 + place = ttm_place_kunit_init(test, mem_type, 0); 124 + placement = ttm_placement_kunit_init(test, place, 1); 125 + 126 + drm_gem_private_object_init(priv->drm, &bo->base, size); 127 + 128 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, bo_type, placement, 129 + PAGE_SIZE, &ctx, NULL, NULL, 130 + &dummy_ttm_bo_destroy); 131 + dma_resv_unlock(bo->base.resv); 132 + 133 + KUNIT_EXPECT_EQ(test, err, 0); 134 + KUNIT_EXPECT_EQ(test, kref_read(&bo->kref), 1); 135 + KUNIT_EXPECT_PTR_EQ(test, bo->bdev, priv->ttm_dev); 136 + KUNIT_EXPECT_EQ(test, bo->type, bo_type); 137 + KUNIT_EXPECT_EQ(test, ctx.bytes_moved, size); 138 + 139 + if (bo_type != ttm_bo_type_kernel) 140 + KUNIT_EXPECT_TRUE(test, 141 + drm_mm_node_allocated(&bo->base.vma_node.vm_node)); 142 + 143 + ttm_resource_free(bo, &bo->resource); 144 + ttm_bo_put(bo); 145 + ttm_mock_manager_fini(priv->ttm_dev, mem_type); 146 + } 147 + 108 148 static void ttm_bo_init_reserved_resv(struct kunit *test) 109 149 { 110 150 enum ttm_bo_type bo_type = ttm_bo_type_device; ··· 182 136 ttm_bo_put(bo); 183 137 } 184 138 139 + static void ttm_bo_validate_basic(struct kunit *test) 140 + { 141 + const struct ttm_bo_validate_test_case *params = test->param_value; 142 + u32 fst_mem = TTM_PL_SYSTEM, snd_mem = TTM_PL_VRAM; 143 + struct ttm_operation_ctx ctx_init = { }, ctx_val = { }; 144 + struct ttm_placement *fst_placement, *snd_placement; 145 + struct ttm_test_devices *priv = test->priv; 146 + struct ttm_place *fst_place, *snd_place; 147 + u32 size = ALIGN(SZ_8K, PAGE_SIZE); 148 + struct ttm_buffer_object *bo; 149 + int err; 150 + 151 + ttm_mock_manager_init(priv->ttm_dev, snd_mem, MANAGER_SIZE); 152 + 153 + fst_place = ttm_place_kunit_init(test, fst_mem, 0); 154 + fst_placement = ttm_placement_kunit_init(test, fst_place, 1); 155 + 156 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 157 + KUNIT_ASSERT_NOT_NULL(test, bo); 158 + 159 + drm_gem_private_object_init(priv->drm, &bo->base, size); 160 + 161 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, params->bo_type, 162 + fst_placement, PAGE_SIZE, &ctx_init, NULL, 163 + NULL, &dummy_ttm_bo_destroy); 164 + KUNIT_EXPECT_EQ(test, err, 0); 165 + 166 + snd_place = ttm_place_kunit_init(test, snd_mem, DRM_BUDDY_TOPDOWN_ALLOCATION); 167 + snd_placement = ttm_placement_kunit_init(test, snd_place, 1); 168 + 169 + err = ttm_bo_validate(bo, snd_placement, &ctx_val); 170 + dma_resv_unlock(bo->base.resv); 171 + 172 + KUNIT_EXPECT_EQ(test, err, 0); 173 + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, bo->base.size); 174 + KUNIT_EXPECT_NOT_NULL(test, bo->ttm); 175 + KUNIT_EXPECT_TRUE(test, ttm_tt_is_populated(bo->ttm)); 176 + KUNIT_EXPECT_EQ(test, bo->resource->mem_type, snd_mem); 177 + KUNIT_EXPECT_EQ(test, bo->resource->placement, 178 + DRM_BUDDY_TOPDOWN_ALLOCATION); 179 + 180 + ttm_bo_put(bo); 181 + ttm_mock_manager_fini(priv->ttm_dev, snd_mem); 182 + } 183 + 185 184 static void ttm_bo_validate_invalid_placement(struct kunit *test) 186 185 { 187 186 enum ttm_bo_type bo_type = ttm_bo_type_device; ··· 251 160 KUNIT_EXPECT_EQ(test, err, -ENOMEM); 252 161 253 162 ttm_bo_put(bo); 163 + } 164 + 165 + static void ttm_bo_validate_failed_alloc(struct kunit *test) 166 + { 167 + enum ttm_bo_type bo_type = ttm_bo_type_device; 168 + struct ttm_test_devices *priv = test->priv; 169 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 170 + struct ttm_operation_ctx ctx = { }; 171 + struct ttm_placement *placement; 172 + u32 mem_type = TTM_PL_VRAM; 173 + struct ttm_buffer_object *bo; 174 + struct ttm_place *place; 175 + int err; 176 + 177 + bo = ttm_bo_kunit_init(test, test->priv, size, NULL); 178 + bo->type = bo_type; 179 + 180 + ttm_bad_manager_init(priv->ttm_dev, mem_type, MANAGER_SIZE); 181 + 182 + place = ttm_place_kunit_init(test, mem_type, 0); 183 + placement = ttm_placement_kunit_init(test, place, 1); 184 + 185 + ttm_bo_reserve(bo, false, false, NULL); 186 + err = ttm_bo_validate(bo, placement, &ctx); 187 + dma_resv_unlock(bo->base.resv); 188 + 189 + KUNIT_EXPECT_EQ(test, err, -ENOMEM); 190 + 191 + ttm_bo_put(bo); 192 + ttm_bad_manager_fini(priv->ttm_dev, mem_type); 254 193 } 255 194 256 195 static void ttm_bo_validate_pinned(struct kunit *test) ··· 314 193 ttm_bo_put(bo); 315 194 } 316 195 196 + static const struct ttm_bo_validate_test_case ttm_mem_type_cases[] = { 197 + { 198 + .description = "System manager", 199 + .mem_type = TTM_PL_SYSTEM, 200 + }, 201 + { 202 + .description = "VRAM manager", 203 + .mem_type = TTM_PL_VRAM, 204 + }, 205 + }; 206 + 207 + KUNIT_ARRAY_PARAM(ttm_bo_validate_mem, ttm_mem_type_cases, 208 + ttm_bo_validate_case_desc); 209 + 210 + static void ttm_bo_validate_same_placement(struct kunit *test) 211 + { 212 + const struct ttm_bo_validate_test_case *params = test->param_value; 213 + struct ttm_operation_ctx ctx_init = { }, ctx_val = { }; 214 + struct ttm_test_devices *priv = test->priv; 215 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 216 + struct ttm_placement *placement; 217 + struct ttm_buffer_object *bo; 218 + struct ttm_place *place; 219 + int err; 220 + 221 + place = ttm_place_kunit_init(test, params->mem_type, 0); 222 + placement = ttm_placement_kunit_init(test, place, 1); 223 + 224 + if (params->mem_type != TTM_PL_SYSTEM) 225 + ttm_mock_manager_init(priv->ttm_dev, params->mem_type, MANAGER_SIZE); 226 + 227 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 228 + KUNIT_ASSERT_NOT_NULL(test, bo); 229 + 230 + drm_gem_private_object_init(priv->drm, &bo->base, size); 231 + 232 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, params->bo_type, 233 + placement, PAGE_SIZE, &ctx_init, NULL, 234 + NULL, &dummy_ttm_bo_destroy); 235 + KUNIT_EXPECT_EQ(test, err, 0); 236 + 237 + err = ttm_bo_validate(bo, placement, &ctx_val); 238 + dma_resv_unlock(bo->base.resv); 239 + 240 + KUNIT_EXPECT_EQ(test, err, 0); 241 + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, 0); 242 + 243 + ttm_bo_put(bo); 244 + 245 + if (params->mem_type != TTM_PL_SYSTEM) 246 + ttm_mock_manager_fini(priv->ttm_dev, params->mem_type); 247 + } 248 + 249 + static void ttm_bo_validate_busy_placement(struct kunit *test) 250 + { 251 + u32 fst_mem = TTM_PL_VRAM, snd_mem = TTM_PL_VRAM + 1; 252 + struct ttm_operation_ctx ctx_init = { }, ctx_val = { }; 253 + struct ttm_placement *placement_init, *placement_val; 254 + enum ttm_bo_type bo_type = ttm_bo_type_device; 255 + struct ttm_test_devices *priv = test->priv; 256 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 257 + struct ttm_place *init_place, places[2]; 258 + struct ttm_resource_manager *man; 259 + struct ttm_buffer_object *bo; 260 + int err; 261 + 262 + ttm_bad_manager_init(priv->ttm_dev, fst_mem, MANAGER_SIZE); 263 + ttm_mock_manager_init(priv->ttm_dev, snd_mem, MANAGER_SIZE); 264 + 265 + init_place = ttm_place_kunit_init(test, TTM_PL_SYSTEM, 0); 266 + placement_init = ttm_placement_kunit_init(test, init_place, 1); 267 + 268 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 269 + KUNIT_ASSERT_NOT_NULL(test, bo); 270 + 271 + drm_gem_private_object_init(priv->drm, &bo->base, size); 272 + 273 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, bo_type, placement_init, 274 + PAGE_SIZE, &ctx_init, NULL, NULL, 275 + &dummy_ttm_bo_destroy); 276 + KUNIT_EXPECT_EQ(test, err, 0); 277 + 278 + places[0] = (struct ttm_place){ .mem_type = fst_mem, .flags = TTM_PL_FLAG_DESIRED }; 279 + places[1] = (struct ttm_place){ .mem_type = snd_mem, .flags = TTM_PL_FLAG_FALLBACK }; 280 + placement_val = ttm_placement_kunit_init(test, places, 2); 281 + 282 + err = ttm_bo_validate(bo, placement_val, &ctx_val); 283 + dma_resv_unlock(bo->base.resv); 284 + 285 + man = ttm_manager_type(priv->ttm_dev, snd_mem); 286 + 287 + KUNIT_EXPECT_EQ(test, err, 0); 288 + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, bo->base.size); 289 + KUNIT_EXPECT_EQ(test, bo->resource->mem_type, snd_mem); 290 + KUNIT_ASSERT_TRUE(test, list_is_singular(&man->lru[bo->priority])); 291 + 292 + ttm_bo_put(bo); 293 + ttm_bad_manager_fini(priv->ttm_dev, fst_mem); 294 + ttm_mock_manager_fini(priv->ttm_dev, snd_mem); 295 + } 296 + 297 + static void ttm_bo_validate_multihop(struct kunit *test) 298 + { 299 + const struct ttm_bo_validate_test_case *params = test->param_value; 300 + struct ttm_operation_ctx ctx_init = { }, ctx_val = { }; 301 + struct ttm_placement *placement_init, *placement_val; 302 + u32 fst_mem = TTM_PL_VRAM, tmp_mem = TTM_PL_TT, final_mem = TTM_PL_SYSTEM; 303 + struct ttm_test_devices *priv = test->priv; 304 + struct ttm_place *fst_place, *final_place; 305 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 306 + struct ttm_buffer_object *bo; 307 + int err; 308 + 309 + ttm_mock_manager_init(priv->ttm_dev, fst_mem, MANAGER_SIZE); 310 + ttm_mock_manager_init(priv->ttm_dev, tmp_mem, MANAGER_SIZE); 311 + 312 + fst_place = ttm_place_kunit_init(test, fst_mem, 0); 313 + placement_init = ttm_placement_kunit_init(test, fst_place, 1); 314 + 315 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 316 + KUNIT_ASSERT_NOT_NULL(test, bo); 317 + 318 + drm_gem_private_object_init(priv->drm, &bo->base, size); 319 + 320 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, params->bo_type, 321 + placement_init, PAGE_SIZE, &ctx_init, NULL, 322 + NULL, &dummy_ttm_bo_destroy); 323 + KUNIT_EXPECT_EQ(test, err, 0); 324 + 325 + final_place = ttm_place_kunit_init(test, final_mem, 0); 326 + placement_val = ttm_placement_kunit_init(test, final_place, 1); 327 + 328 + err = ttm_bo_validate(bo, placement_val, &ctx_val); 329 + dma_resv_unlock(bo->base.resv); 330 + 331 + KUNIT_EXPECT_EQ(test, err, 0); 332 + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, size * 2); 333 + KUNIT_EXPECT_EQ(test, bo->resource->mem_type, final_mem); 334 + 335 + ttm_bo_put(bo); 336 + 337 + ttm_mock_manager_fini(priv->ttm_dev, fst_mem); 338 + ttm_mock_manager_fini(priv->ttm_dev, tmp_mem); 339 + } 340 + 317 341 static struct kunit_case ttm_bo_validate_test_cases[] = { 318 342 KUNIT_CASE_PARAM(ttm_bo_init_reserved_sys_man, ttm_bo_types_gen_params), 343 + KUNIT_CASE_PARAM(ttm_bo_init_reserved_mock_man, ttm_bo_types_gen_params), 319 344 KUNIT_CASE(ttm_bo_init_reserved_resv), 345 + KUNIT_CASE_PARAM(ttm_bo_validate_basic, ttm_bo_types_gen_params), 320 346 KUNIT_CASE(ttm_bo_validate_invalid_placement), 347 + KUNIT_CASE_PARAM(ttm_bo_validate_same_placement, 348 + ttm_bo_validate_mem_gen_params), 349 + KUNIT_CASE(ttm_bo_validate_failed_alloc), 321 350 KUNIT_CASE(ttm_bo_validate_pinned), 351 + KUNIT_CASE(ttm_bo_validate_busy_placement), 352 + KUNIT_CASE_PARAM(ttm_bo_validate_multihop, ttm_bo_types_gen_params), 322 353 {} 323 354 }; 324 355
+25 -2
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
··· 27 27 struct ttm_resource *new_mem, 28 28 struct ttm_place *hop) 29 29 { 30 - bo->resource = new_mem; 31 - return 0; 30 + struct ttm_resource *old_mem = bo->resource; 31 + 32 + if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm)) { 33 + ttm_bo_move_null(bo, new_mem); 34 + return 0; 35 + } 36 + 37 + if (bo->resource->mem_type == TTM_PL_VRAM && 38 + new_mem->mem_type == TTM_PL_SYSTEM) { 39 + hop->mem_type = TTM_PL_TT; 40 + hop->flags = TTM_PL_FLAG_TEMPORARY; 41 + hop->fpfn = 0; 42 + hop->lpfn = 0; 43 + return -EMULTIHOP; 44 + } 45 + 46 + if ((old_mem->mem_type == TTM_PL_SYSTEM && 47 + new_mem->mem_type == TTM_PL_TT) || 48 + (old_mem->mem_type == TTM_PL_TT && 49 + new_mem->mem_type == TTM_PL_SYSTEM)) { 50 + ttm_bo_move_null(bo, new_mem); 51 + return 0; 52 + } 53 + 54 + return ttm_bo_move_memcpy(bo, ctx, new_mem); 32 55 } 33 56 34 57 struct ttm_device_funcs ttm_dev_funcs = {
+206
drivers/gpu/drm/ttm/tests/ttm_mock_manager.c
··· 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_mock_manager.h" 10 + 11 + static inline struct ttm_mock_manager * 12 + to_mock_mgr(struct ttm_resource_manager *man) 13 + { 14 + return container_of(man, struct ttm_mock_manager, man); 15 + } 16 + 17 + static inline struct ttm_mock_resource * 18 + to_mock_mgr_resource(struct ttm_resource *res) 19 + { 20 + return container_of(res, struct ttm_mock_resource, base); 21 + } 22 + 23 + static int ttm_mock_manager_alloc(struct ttm_resource_manager *man, 24 + struct ttm_buffer_object *bo, 25 + const struct ttm_place *place, 26 + struct ttm_resource **res) 27 + { 28 + struct ttm_mock_manager *manager = to_mock_mgr(man); 29 + struct ttm_mock_resource *mock_res; 30 + struct drm_buddy *mm = &manager->mm; 31 + u64 lpfn, fpfn, alloc_size; 32 + int err; 33 + 34 + mock_res = kzalloc(sizeof(*mock_res), GFP_KERNEL); 35 + 36 + if (!mock_res) 37 + return -ENOMEM; 38 + 39 + fpfn = 0; 40 + lpfn = man->size; 41 + 42 + ttm_resource_init(bo, place, &mock_res->base); 43 + INIT_LIST_HEAD(&mock_res->blocks); 44 + 45 + if (place->flags & TTM_PL_FLAG_TOPDOWN) 46 + mock_res->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION; 47 + 48 + if (place->flags & TTM_PL_FLAG_CONTIGUOUS) 49 + mock_res->flags |= DRM_BUDDY_CONTIGUOUS_ALLOCATION; 50 + 51 + alloc_size = (uint64_t)mock_res->base.size; 52 + mutex_lock(&manager->lock); 53 + err = drm_buddy_alloc_blocks(mm, fpfn, lpfn, alloc_size, 54 + manager->default_page_size, 55 + &mock_res->blocks, 56 + mock_res->flags); 57 + 58 + if (err) 59 + goto error_free_blocks; 60 + mutex_unlock(&manager->lock); 61 + 62 + *res = &mock_res->base; 63 + return 0; 64 + 65 + error_free_blocks: 66 + drm_buddy_free_list(mm, &mock_res->blocks, 0); 67 + ttm_resource_fini(man, &mock_res->base); 68 + mutex_unlock(&manager->lock); 69 + 70 + return err; 71 + } 72 + 73 + static void ttm_mock_manager_free(struct ttm_resource_manager *man, 74 + struct ttm_resource *res) 75 + { 76 + struct ttm_mock_manager *manager = to_mock_mgr(man); 77 + struct ttm_mock_resource *mock_res = to_mock_mgr_resource(res); 78 + struct drm_buddy *mm = &manager->mm; 79 + 80 + mutex_lock(&manager->lock); 81 + drm_buddy_free_list(mm, &mock_res->blocks, 0); 82 + mutex_unlock(&manager->lock); 83 + 84 + ttm_resource_fini(man, res); 85 + kfree(mock_res); 86 + } 87 + 88 + static const struct ttm_resource_manager_func ttm_mock_manager_funcs = { 89 + .alloc = ttm_mock_manager_alloc, 90 + .free = ttm_mock_manager_free, 91 + }; 92 + 93 + int ttm_mock_manager_init(struct ttm_device *bdev, u32 mem_type, u32 size) 94 + { 95 + struct ttm_mock_manager *manager; 96 + struct ttm_resource_manager *base; 97 + int err; 98 + 99 + manager = kzalloc(sizeof(*manager), GFP_KERNEL); 100 + if (!manager) 101 + return -ENOMEM; 102 + 103 + mutex_init(&manager->lock); 104 + 105 + err = drm_buddy_init(&manager->mm, size, PAGE_SIZE); 106 + 107 + if (err) { 108 + kfree(manager); 109 + return err; 110 + } 111 + 112 + manager->default_page_size = PAGE_SIZE; 113 + base = &manager->man; 114 + base->func = &ttm_mock_manager_funcs; 115 + base->use_tt = true; 116 + 117 + ttm_resource_manager_init(base, bdev, size); 118 + ttm_set_driver_manager(bdev, mem_type, base); 119 + ttm_resource_manager_set_used(base, true); 120 + 121 + return 0; 122 + } 123 + EXPORT_SYMBOL_GPL(ttm_mock_manager_init); 124 + 125 + void ttm_mock_manager_fini(struct ttm_device *bdev, u32 mem_type) 126 + { 127 + struct ttm_resource_manager *man; 128 + struct ttm_mock_manager *mock_man; 129 + int err; 130 + 131 + man = ttm_manager_type(bdev, mem_type); 132 + mock_man = to_mock_mgr(man); 133 + 134 + err = ttm_resource_manager_evict_all(bdev, man); 135 + if (err) 136 + return; 137 + 138 + ttm_resource_manager_set_used(man, false); 139 + 140 + mutex_lock(&mock_man->lock); 141 + drm_buddy_fini(&mock_man->mm); 142 + mutex_unlock(&mock_man->lock); 143 + 144 + ttm_set_driver_manager(bdev, mem_type, NULL); 145 + } 146 + EXPORT_SYMBOL_GPL(ttm_mock_manager_fini); 147 + 148 + static int ttm_bad_manager_alloc(struct ttm_resource_manager *man, 149 + struct ttm_buffer_object *bo, 150 + const struct ttm_place *place, 151 + struct ttm_resource **res) 152 + { 153 + return -ENOSPC; 154 + } 155 + 156 + static void ttm_bad_manager_free(struct ttm_resource_manager *man, 157 + struct ttm_resource *res) 158 + { 159 + } 160 + 161 + static bool ttm_bad_manager_compatible(struct ttm_resource_manager *man, 162 + struct ttm_resource *res, 163 + const struct ttm_place *place, 164 + size_t size) 165 + { 166 + return true; 167 + } 168 + 169 + static const struct ttm_resource_manager_func ttm_bad_manager_funcs = { 170 + .alloc = ttm_bad_manager_alloc, 171 + .free = ttm_bad_manager_free, 172 + .compatible = ttm_bad_manager_compatible 173 + }; 174 + 175 + int ttm_bad_manager_init(struct ttm_device *bdev, u32 mem_type, u32 size) 176 + { 177 + struct ttm_resource_manager *man; 178 + 179 + man = kzalloc(sizeof(*man), GFP_KERNEL); 180 + if (!man) 181 + return -ENOMEM; 182 + 183 + man->func = &ttm_bad_manager_funcs; 184 + 185 + ttm_resource_manager_init(man, bdev, size); 186 + ttm_set_driver_manager(bdev, mem_type, man); 187 + ttm_resource_manager_set_used(man, true); 188 + 189 + return 0; 190 + } 191 + EXPORT_SYMBOL_GPL(ttm_bad_manager_init); 192 + 193 + void ttm_bad_manager_fini(struct ttm_device *bdev, u32 mem_type) 194 + { 195 + struct ttm_resource_manager *man; 196 + 197 + man = ttm_manager_type(bdev, mem_type); 198 + 199 + ttm_resource_manager_set_used(man, false); 200 + ttm_set_driver_manager(bdev, mem_type, NULL); 201 + 202 + kfree(man); 203 + } 204 + EXPORT_SYMBOL_GPL(ttm_bad_manager_fini); 205 + 206 + MODULE_LICENSE("GPL and additional rights");
+29
drivers/gpu/drm/ttm/tests/ttm_mock_manager.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 AND MIT */ 2 + /* 3 + * Copyright © 2023 Intel Corporation 4 + */ 5 + #ifndef TTM_MOCK_MANAGER_H 6 + #define TTM_MOCK_MANAGER_H 7 + 8 + #include <drm/drm_buddy.h> 9 + 10 + struct ttm_mock_manager { 11 + struct ttm_resource_manager man; 12 + struct drm_buddy mm; 13 + u64 default_page_size; 14 + /* protects allocations of mock buffer objects */ 15 + struct mutex lock; 16 + }; 17 + 18 + struct ttm_mock_resource { 19 + struct ttm_resource base; 20 + struct list_head blocks; 21 + unsigned long flags; 22 + }; 23 + 24 + int ttm_mock_manager_init(struct ttm_device *bdev, u32 mem_type, u32 size); 25 + int ttm_bad_manager_init(struct ttm_device *bdev, u32 mem_type, u32 size); 26 + void ttm_mock_manager_fini(struct ttm_device *bdev, u32 mem_type); 27 + void ttm_bad_manager_fini(struct ttm_device *bdev, u32 mem_type); 28 + 29 + #endif // TTM_MOCK_MANAGER_H