1 // SPDX-License-Identifier: GPL-2.0 AND MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 #include "ttm_kunit_helpers.h"
6 
7 struct ttm_device_funcs ttm_dev_funcs = {
8 };
9 EXPORT_SYMBOL_GPL(ttm_dev_funcs);
10 
ttm_device_kunit_init(struct ttm_test_devices * priv,struct ttm_device * ttm,bool use_dma_alloc,bool use_dma32)11 int ttm_device_kunit_init(struct ttm_test_devices *priv,
12 			  struct ttm_device *ttm,
13 			  bool use_dma_alloc,
14 			  bool use_dma32)
15 {
16 	struct drm_device *drm = priv->drm;
17 	int err;
18 
19 	err = ttm_device_init(ttm, &ttm_dev_funcs, drm->dev,
20 			      drm->anon_inode->i_mapping,
21 			      drm->vma_offset_manager,
22 			      use_dma_alloc, use_dma32);
23 
24 	return err;
25 }
26 EXPORT_SYMBOL_GPL(ttm_device_kunit_init);
27 
ttm_bo_kunit_init(struct kunit * test,struct ttm_test_devices * devs,size_t size)28 struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
29 					    struct ttm_test_devices *devs,
30 					    size_t size)
31 {
32 	struct drm_gem_object gem_obj = { .size = size };
33 	struct ttm_buffer_object *bo;
34 
35 	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
36 	KUNIT_ASSERT_NOT_NULL(test, bo);
37 
38 	bo->base = gem_obj;
39 	bo->bdev = devs->ttm_dev;
40 
41 	return bo;
42 }
43 EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
44 
ttm_test_devices_basic(struct kunit * test)45 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test)
46 {
47 	struct ttm_test_devices *devs;
48 
49 	devs = kunit_kzalloc(test, sizeof(*devs), GFP_KERNEL);
50 	KUNIT_ASSERT_NOT_NULL(test, devs);
51 
52 	devs->dev = drm_kunit_helper_alloc_device(test);
53 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->dev);
54 
55 	devs->drm = __drm_kunit_helper_alloc_drm_device(test, devs->dev,
56 							sizeof(*devs->drm), 0,
57 							DRIVER_GEM);
58 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, devs->drm);
59 
60 	return devs;
61 }
62 EXPORT_SYMBOL_GPL(ttm_test_devices_basic);
63 
ttm_test_devices_all(struct kunit * test)64 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test)
65 {
66 	struct ttm_test_devices *devs;
67 	struct ttm_device *ttm_dev;
68 	int err;
69 
70 	devs = ttm_test_devices_basic(test);
71 
72 	ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
73 	KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
74 
75 	err = ttm_device_kunit_init(devs, ttm_dev, false, false);
76 	KUNIT_ASSERT_EQ(test, err, 0);
77 
78 	devs->ttm_dev = ttm_dev;
79 
80 	return devs;
81 }
82 EXPORT_SYMBOL_GPL(ttm_test_devices_all);
83 
ttm_test_devices_put(struct kunit * test,struct ttm_test_devices * devs)84 void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs)
85 {
86 	if (devs->ttm_dev)
87 		ttm_device_fini(devs->ttm_dev);
88 
89 	drm_kunit_helper_free_device(test, devs->dev);
90 }
91 EXPORT_SYMBOL_GPL(ttm_test_devices_put);
92 
ttm_test_devices_init(struct kunit * test)93 int ttm_test_devices_init(struct kunit *test)
94 {
95 	struct ttm_test_devices *priv;
96 
97 	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
98 	KUNIT_ASSERT_NOT_NULL(test, priv);
99 
100 	priv = ttm_test_devices_basic(test);
101 	test->priv = priv;
102 
103 	return 0;
104 }
105 EXPORT_SYMBOL_GPL(ttm_test_devices_init);
106 
ttm_test_devices_fini(struct kunit * test)107 void ttm_test_devices_fini(struct kunit *test)
108 {
109 	ttm_test_devices_put(test, test->priv);
110 }
111 EXPORT_SYMBOL_GPL(ttm_test_devices_fini);
112 
113 MODULE_LICENSE("GPL");
114