xref: /linux/drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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 struct ttm_tt *ttm_tt_simple_create(struct ttm_buffer_object *bo,
10 					   uint32_t page_flags)
11 {
12 	struct ttm_tt *tt;
13 
14 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
15 	ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
16 
17 	return tt;
18 }
19 
20 static void ttm_tt_simple_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
21 {
22 	kfree(ttm);
23 }
24 
25 static void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo)
26 {
27 }
28 
29 struct ttm_device_funcs ttm_dev_funcs = {
30 	.ttm_tt_create = ttm_tt_simple_create,
31 	.ttm_tt_destroy = ttm_tt_simple_destroy,
32 };
33 EXPORT_SYMBOL_GPL(ttm_dev_funcs);
34 
35 int ttm_device_kunit_init(struct ttm_test_devices *priv,
36 			  struct ttm_device *ttm,
37 			  bool use_dma_alloc,
38 			  bool use_dma32)
39 {
40 	struct drm_device *drm = priv->drm;
41 	int err;
42 
43 	err = ttm_device_init(ttm, &ttm_dev_funcs, drm->dev,
44 			      drm->anon_inode->i_mapping,
45 			      drm->vma_offset_manager,
46 			      use_dma_alloc, use_dma32);
47 
48 	return err;
49 }
50 EXPORT_SYMBOL_GPL(ttm_device_kunit_init);
51 
52 struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
53 					    struct ttm_test_devices *devs,
54 					    size_t size)
55 {
56 	struct drm_gem_object gem_obj = { };
57 	struct ttm_buffer_object *bo;
58 	int err;
59 
60 	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
61 	KUNIT_ASSERT_NOT_NULL(test, bo);
62 
63 	bo->base = gem_obj;
64 	err = drm_gem_object_init(devs->drm, &bo->base, size);
65 	KUNIT_ASSERT_EQ(test, err, 0);
66 
67 	bo->bdev = devs->ttm_dev;
68 	bo->destroy = dummy_ttm_bo_destroy;
69 
70 	kref_init(&bo->kref);
71 
72 	return bo;
73 }
74 EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
75 
76 struct ttm_place *ttm_place_kunit_init(struct kunit *test,
77 				       uint32_t mem_type, uint32_t flags)
78 {
79 	struct ttm_place *place;
80 
81 	place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
82 	KUNIT_ASSERT_NOT_NULL(test, place);
83 
84 	place->mem_type = mem_type;
85 	place->flags = flags;
86 
87 	return place;
88 }
89 EXPORT_SYMBOL_GPL(ttm_place_kunit_init);
90 
91 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test)
92 {
93 	struct ttm_test_devices *devs;
94 
95 	devs = kunit_kzalloc(test, sizeof(*devs), GFP_KERNEL);
96 	KUNIT_ASSERT_NOT_NULL(test, devs);
97 
98 	devs->dev = drm_kunit_helper_alloc_device(test);
99 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->dev);
100 
101 	devs->drm = __drm_kunit_helper_alloc_drm_device(test, devs->dev,
102 							sizeof(*devs->drm), 0,
103 							DRIVER_GEM);
104 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->drm);
105 
106 	return devs;
107 }
108 EXPORT_SYMBOL_GPL(ttm_test_devices_basic);
109 
110 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test)
111 {
112 	struct ttm_test_devices *devs;
113 	struct ttm_device *ttm_dev;
114 	int err;
115 
116 	devs = ttm_test_devices_basic(test);
117 
118 	ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
119 	KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
120 
121 	err = ttm_device_kunit_init(devs, ttm_dev, false, false);
122 	KUNIT_ASSERT_EQ(test, err, 0);
123 
124 	devs->ttm_dev = ttm_dev;
125 
126 	return devs;
127 }
128 EXPORT_SYMBOL_GPL(ttm_test_devices_all);
129 
130 void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs)
131 {
132 	if (devs->ttm_dev)
133 		ttm_device_fini(devs->ttm_dev);
134 
135 	drm_kunit_helper_free_device(test, devs->dev);
136 }
137 EXPORT_SYMBOL_GPL(ttm_test_devices_put);
138 
139 int ttm_test_devices_init(struct kunit *test)
140 {
141 	struct ttm_test_devices *priv;
142 
143 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
144 	KUNIT_ASSERT_NOT_NULL(test, priv);
145 
146 	priv = ttm_test_devices_basic(test);
147 	test->priv = priv;
148 
149 	return 0;
150 }
151 EXPORT_SYMBOL_GPL(ttm_test_devices_init);
152 
153 void ttm_test_devices_fini(struct kunit *test)
154 {
155 	ttm_test_devices_put(test, test->priv);
156 }
157 EXPORT_SYMBOL_GPL(ttm_test_devices_fini);
158 
159 MODULE_LICENSE("GPL");
160