xref: /linux/include/drm/drm_kunit_helpers.h (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #ifndef DRM_KUNIT_HELPERS_H_
4 #define DRM_KUNIT_HELPERS_H_
5 
6 #include <kunit/test.h>
7 
8 struct drm_device;
9 struct kunit;
10 
11 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
12 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
13 
14 struct drm_device *
15 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
16 						struct device *dev,
17 						size_t size, size_t offset,
18 						const struct drm_driver *driver);
19 
20 /**
21  * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
22  * @_test: The test context object
23  * @_dev: The parent device object
24  * @_type: the type of the struct which contains struct &drm_device
25  * @_member: the name of the &drm_device within @_type.
26  * @_drv: Mocked DRM device driver features
27  *
28  * This function creates a struct &drm_device from @_dev and @_drv.
29  *
30  * @_dev should be allocated using drm_kunit_helper_alloc_device().
31  *
32  * The driver is tied to the @_test context and will get cleaned at the
33  * end of the test. The drm_device is allocated through
34  * devm_drm_dev_alloc() and will thus be freed through a device-managed
35  * resource.
36  *
37  * Returns:
38  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
39  */
40 #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv)	\
41 	((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev,			\
42 						       sizeof(_type),				\
43 						       offsetof(_type, _member),		\
44 						       _drv))
45 
46 static inline struct drm_device *
47 __drm_kunit_helper_alloc_drm_device(struct kunit *test,
48 				    struct device *dev,
49 				    size_t size, size_t offset,
50 				    u32 features)
51 {
52 	struct drm_driver *driver;
53 
54 	driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
55 	KUNIT_ASSERT_NOT_NULL(test, driver);
56 
57 	driver->driver_features = features;
58 
59 	return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
60 							       size, offset,
61 							       driver);
62 }
63 
64 /**
65  * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
66  * @_test: The test context object
67  * @_dev: The parent device object
68  * @_type: the type of the struct which contains struct &drm_device
69  * @_member: the name of the &drm_device within @_type.
70  * @_features: Mocked DRM device driver features
71  *
72  * This function creates a struct &drm_driver and will create a struct
73  * &drm_device from @_dev and that driver.
74  *
75  * @_dev should be allocated using drm_kunit_helper_alloc_device().
76  *
77  * The driver is tied to the @_test context and will get cleaned at the
78  * end of the test. The drm_device is allocated through
79  * devm_drm_dev_alloc() and will thus be freed through a device-managed
80  * resource.
81  *
82  * Returns:
83  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
84  */
85 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat)	\
86 	((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev,		\
87 						      sizeof(_type),		\
88 						      offsetof(_type, _member),	\
89 						      _feat))
90 
91 #endif // DRM_KUNIT_HELPERS_H_
92