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: Test simple BO creation and validation

Add tests for ttm_bo_init_reserved() and ttm_bo_validate() that use
sys manager. Define a simple move function in ttm_device_funcs. Expose
destroy callback of the buffer object to make testing of
ttm_bo_init_reserved() behaviour easier.

Signed-off-by: Karolina Stolarek <karolina.stolarek@intel.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Reviewed-by: Somalapuram, Amaranath <asomalap@amd.com>
Tested-by: Somalapuram, Amaranath <asomalap@amd.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/ffba0d62eb98b2cbc61ae7ca90fee7dc0855719c.1718192625.git.karolina.stolarek@intel.com

authored by

Karolina Stolarek and committed by
Arunpravin Paneer Selvam
8bd1ff5d f7ed0a7e

+228 -1
+1
drivers/gpu/drm/ttm/tests/Makefile
··· 6 6 ttm_resource_test.o \ 7 7 ttm_tt_test.o \ 8 8 ttm_bo_test.o \ 9 + ttm_bo_validate_test.o \ 9 10 ttm_kunit_helpers.o
+213
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 AND MIT 2 + /* 3 + * Copyright © 2023 Intel Corporation 4 + */ 5 + 6 + #include <drm/ttm/ttm_resource.h> 7 + #include <drm/ttm/ttm_placement.h> 8 + #include <drm/ttm/ttm_tt.h> 9 + 10 + #include "ttm_kunit_helpers.h" 11 + 12 + #define BO_SIZE SZ_4K 13 + 14 + struct ttm_bo_validate_test_case { 15 + const char *description; 16 + enum ttm_bo_type bo_type; 17 + bool with_ttm; 18 + }; 19 + 20 + static struct ttm_placement *ttm_placement_kunit_init(struct kunit *test, 21 + struct ttm_place *places, 22 + unsigned int num_places) 23 + { 24 + struct ttm_placement *placement; 25 + 26 + placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL); 27 + KUNIT_ASSERT_NOT_NULL(test, placement); 28 + 29 + placement->num_placement = num_places; 30 + placement->placement = places; 31 + 32 + return placement; 33 + } 34 + 35 + static void ttm_bo_validate_case_desc(const struct ttm_bo_validate_test_case *t, 36 + char *desc) 37 + { 38 + strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE); 39 + } 40 + 41 + static const struct ttm_bo_validate_test_case ttm_bo_type_cases[] = { 42 + { 43 + .description = "Buffer object for userspace", 44 + .bo_type = ttm_bo_type_device, 45 + }, 46 + { 47 + .description = "Kernel buffer object", 48 + .bo_type = ttm_bo_type_kernel, 49 + }, 50 + { 51 + .description = "Shared buffer object", 52 + .bo_type = ttm_bo_type_sg, 53 + }, 54 + }; 55 + 56 + KUNIT_ARRAY_PARAM(ttm_bo_types, ttm_bo_type_cases, 57 + ttm_bo_validate_case_desc); 58 + 59 + static void ttm_bo_init_reserved_sys_man(struct kunit *test) 60 + { 61 + const struct ttm_bo_validate_test_case *params = test->param_value; 62 + struct ttm_test_devices *priv = test->priv; 63 + enum ttm_bo_type bo_type = params->bo_type; 64 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 65 + struct ttm_operation_ctx ctx = { }; 66 + struct ttm_placement *placement; 67 + struct ttm_buffer_object *bo; 68 + struct ttm_place *place; 69 + int err; 70 + 71 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 72 + KUNIT_ASSERT_NOT_NULL(test, bo); 73 + 74 + place = ttm_place_kunit_init(test, TTM_PL_SYSTEM, 0); 75 + placement = ttm_placement_kunit_init(test, place, 1); 76 + 77 + drm_gem_private_object_init(priv->drm, &bo->base, size); 78 + 79 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, bo_type, placement, 80 + PAGE_SIZE, &ctx, NULL, NULL, 81 + &dummy_ttm_bo_destroy); 82 + dma_resv_unlock(bo->base.resv); 83 + 84 + KUNIT_EXPECT_EQ(test, err, 0); 85 + KUNIT_EXPECT_EQ(test, kref_read(&bo->kref), 1); 86 + KUNIT_EXPECT_PTR_EQ(test, bo->bdev, priv->ttm_dev); 87 + KUNIT_EXPECT_EQ(test, bo->type, bo_type); 88 + KUNIT_EXPECT_EQ(test, bo->page_alignment, PAGE_SIZE); 89 + KUNIT_EXPECT_PTR_EQ(test, bo->destroy, &dummy_ttm_bo_destroy); 90 + KUNIT_EXPECT_EQ(test, bo->pin_count, 0); 91 + KUNIT_EXPECT_NULL(test, bo->bulk_move); 92 + KUNIT_EXPECT_NOT_NULL(test, bo->ttm); 93 + KUNIT_EXPECT_FALSE(test, ttm_tt_is_populated(bo->ttm)); 94 + KUNIT_EXPECT_NOT_NULL(test, (void *)bo->base.resv->fences); 95 + KUNIT_EXPECT_EQ(test, ctx.bytes_moved, size); 96 + 97 + if (bo_type != ttm_bo_type_kernel) 98 + KUNIT_EXPECT_TRUE(test, 99 + drm_mm_node_allocated(&bo->base.vma_node.vm_node)); 100 + 101 + ttm_resource_free(bo, &bo->resource); 102 + ttm_bo_put(bo); 103 + } 104 + 105 + static void ttm_bo_init_reserved_resv(struct kunit *test) 106 + { 107 + enum ttm_bo_type bo_type = ttm_bo_type_device; 108 + struct ttm_test_devices *priv = test->priv; 109 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 110 + struct ttm_operation_ctx ctx = { }; 111 + struct ttm_placement *placement; 112 + struct ttm_buffer_object *bo; 113 + struct ttm_place *place; 114 + struct dma_resv resv; 115 + int err; 116 + 117 + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); 118 + KUNIT_ASSERT_NOT_NULL(test, bo); 119 + 120 + place = ttm_place_kunit_init(test, TTM_PL_SYSTEM, 0); 121 + placement = ttm_placement_kunit_init(test, place, 1); 122 + 123 + drm_gem_private_object_init(priv->drm, &bo->base, size); 124 + dma_resv_init(&resv); 125 + dma_resv_lock(&resv, NULL); 126 + 127 + err = ttm_bo_init_reserved(priv->ttm_dev, bo, bo_type, placement, 128 + PAGE_SIZE, &ctx, NULL, &resv, 129 + &dummy_ttm_bo_destroy); 130 + dma_resv_unlock(bo->base.resv); 131 + 132 + KUNIT_EXPECT_EQ(test, err, 0); 133 + KUNIT_EXPECT_PTR_EQ(test, bo->base.resv, &resv); 134 + 135 + ttm_resource_free(bo, &bo->resource); 136 + ttm_bo_put(bo); 137 + } 138 + 139 + static void ttm_bo_validate_invalid_placement(struct kunit *test) 140 + { 141 + enum ttm_bo_type bo_type = ttm_bo_type_device; 142 + u32 unknown_mem_type = TTM_PL_PRIV + 1; 143 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 144 + struct ttm_operation_ctx ctx = { }; 145 + struct ttm_placement *placement; 146 + struct ttm_buffer_object *bo; 147 + struct ttm_place *place; 148 + int err; 149 + 150 + place = ttm_place_kunit_init(test, unknown_mem_type, 0); 151 + placement = ttm_placement_kunit_init(test, place, 1); 152 + 153 + bo = ttm_bo_kunit_init(test, test->priv, size, NULL); 154 + bo->type = bo_type; 155 + 156 + ttm_bo_reserve(bo, false, false, NULL); 157 + err = ttm_bo_validate(bo, placement, &ctx); 158 + dma_resv_unlock(bo->base.resv); 159 + 160 + KUNIT_EXPECT_EQ(test, err, -ENOMEM); 161 + 162 + ttm_bo_put(bo); 163 + } 164 + 165 + static void ttm_bo_validate_pinned(struct kunit *test) 166 + { 167 + enum ttm_bo_type bo_type = ttm_bo_type_device; 168 + u32 size = ALIGN(BO_SIZE, PAGE_SIZE); 169 + struct ttm_operation_ctx ctx = { }; 170 + u32 mem_type = TTM_PL_SYSTEM; 171 + struct ttm_placement *placement; 172 + struct ttm_buffer_object *bo; 173 + struct ttm_place *place; 174 + int err; 175 + 176 + place = ttm_place_kunit_init(test, mem_type, 0); 177 + placement = ttm_placement_kunit_init(test, place, 1); 178 + 179 + bo = ttm_bo_kunit_init(test, test->priv, size, NULL); 180 + bo->type = bo_type; 181 + 182 + ttm_bo_reserve(bo, false, false, NULL); 183 + ttm_bo_pin(bo); 184 + err = ttm_bo_validate(bo, placement, &ctx); 185 + dma_resv_unlock(bo->base.resv); 186 + 187 + KUNIT_EXPECT_EQ(test, err, -EINVAL); 188 + 189 + ttm_bo_reserve(bo, false, false, NULL); 190 + ttm_bo_unpin(bo); 191 + dma_resv_unlock(bo->base.resv); 192 + 193 + ttm_bo_put(bo); 194 + } 195 + 196 + static struct kunit_case ttm_bo_validate_test_cases[] = { 197 + KUNIT_CASE_PARAM(ttm_bo_init_reserved_sys_man, ttm_bo_types_gen_params), 198 + KUNIT_CASE(ttm_bo_init_reserved_resv), 199 + KUNIT_CASE(ttm_bo_validate_invalid_placement), 200 + KUNIT_CASE(ttm_bo_validate_pinned), 201 + {} 202 + }; 203 + 204 + static struct kunit_suite ttm_bo_validate_test_suite = { 205 + .name = "ttm_bo_validate", 206 + .init = ttm_test_devices_all_init, 207 + .exit = ttm_test_devices_fini, 208 + .test_cases = ttm_bo_validate_test_cases, 209 + }; 210 + 211 + kunit_test_suites(&ttm_bo_validate_test_suite); 212 + 213 + MODULE_LICENSE("GPL and additional rights");
+13 -1
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
··· 22 22 kfree(ttm); 23 23 } 24 24 25 - static void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo) 25 + static int mock_move(struct ttm_buffer_object *bo, bool evict, 26 + struct ttm_operation_ctx *ctx, 27 + struct ttm_resource *new_mem, 28 + struct ttm_place *hop) 26 29 { 30 + bo->resource = new_mem; 31 + return 0; 27 32 } 28 33 29 34 struct ttm_device_funcs ttm_dev_funcs = { 30 35 .ttm_tt_create = ttm_tt_simple_create, 31 36 .ttm_tt_destroy = ttm_tt_simple_destroy, 37 + .move = mock_move, 32 38 }; 33 39 EXPORT_SYMBOL_GPL(ttm_dev_funcs); 34 40 ··· 98 92 return place; 99 93 } 100 94 EXPORT_SYMBOL_GPL(ttm_place_kunit_init); 95 + 96 + void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo) 97 + { 98 + drm_gem_object_release(&bo->base); 99 + } 100 + EXPORT_SYMBOL_GPL(dummy_ttm_bo_destroy); 101 101 102 102 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test) 103 103 {
+1
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h
··· 32 32 struct dma_resv *obj); 33 33 struct ttm_place *ttm_place_kunit_init(struct kunit *test, 34 34 uint32_t mem_type, uint32_t flags); 35 + void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo); 35 36 36 37 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test); 37 38 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test);