xref: /openbmc/linux/include/drm/drm_kunit_helpers.h (revision 5ee9cd065836e5934710ca35653bce7905add20b)
10c3627c7SMaxime Ripard // SPDX-License-Identifier: GPL-2.0
20c3627c7SMaxime Ripard 
30c3627c7SMaxime Ripard #ifndef DRM_KUNIT_HELPERS_H_
40c3627c7SMaxime Ripard #define DRM_KUNIT_HELPERS_H_
50c3627c7SMaxime Ripard 
6*4f14cbcdSMaxime Ripard #include <drm/drm_drv.h>
7*4f14cbcdSMaxime Ripard 
8139a2785SThomas Hellström #include <linux/device.h>
9139a2785SThomas Hellström 
10d9878031SMaxime Ripard #include <kunit/test.h>
11d9878031SMaxime Ripard 
120c3627c7SMaxime Ripard struct drm_device;
130c3627c7SMaxime Ripard struct kunit;
140c3627c7SMaxime Ripard 
159ecd8045SMaxime Ripard struct device *drm_kunit_helper_alloc_device(struct kunit *test);
169ecd8045SMaxime Ripard void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
179ecd8045SMaxime Ripard 
1883ee69a8SMaxime Ripard struct drm_device *
19d9878031SMaxime Ripard __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
20d9878031SMaxime Ripard 						struct device *dev,
21a9143c58SMaxime Ripard 						size_t size, size_t offset,
22d9878031SMaxime Ripard 						const struct drm_driver *driver);
23d9878031SMaxime Ripard 
24d9878031SMaxime Ripard /**
25d9878031SMaxime Ripard  * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
26d9878031SMaxime Ripard  * @_test: The test context object
27d9878031SMaxime Ripard  * @_dev: The parent device object
28d9878031SMaxime Ripard  * @_type: the type of the struct which contains struct &drm_device
29d9878031SMaxime Ripard  * @_member: the name of the &drm_device within @_type.
30d9878031SMaxime Ripard  * @_drv: Mocked DRM device driver features
31d9878031SMaxime Ripard  *
32d9878031SMaxime Ripard  * This function creates a struct &drm_device from @_dev and @_drv.
33d9878031SMaxime Ripard  *
34d9878031SMaxime Ripard  * @_dev should be allocated using drm_kunit_helper_alloc_device().
35d9878031SMaxime Ripard  *
36d9878031SMaxime Ripard  * The driver is tied to the @_test context and will get cleaned at the
37d9878031SMaxime Ripard  * end of the test. The drm_device is allocated through
38d9878031SMaxime Ripard  * devm_drm_dev_alloc() and will thus be freed through a device-managed
39d9878031SMaxime Ripard  * resource.
40d9878031SMaxime Ripard  *
41d9878031SMaxime Ripard  * Returns:
42d9878031SMaxime Ripard  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
43d9878031SMaxime Ripard  */
44d9878031SMaxime Ripard #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv)	\
45d9878031SMaxime Ripard 	((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev,			\
46d9878031SMaxime Ripard 						       sizeof(_type),				\
47d9878031SMaxime Ripard 						       offsetof(_type, _member),		\
48d9878031SMaxime Ripard 						       _drv))
49d9878031SMaxime Ripard 
50d9878031SMaxime Ripard static inline struct drm_device *
__drm_kunit_helper_alloc_drm_device(struct kunit * test,struct device * dev,size_t size,size_t offset,u32 features)51d9878031SMaxime Ripard __drm_kunit_helper_alloc_drm_device(struct kunit *test,
52d9878031SMaxime Ripard 				    struct device *dev,
53d9878031SMaxime Ripard 				    size_t size, size_t offset,
54d9878031SMaxime Ripard 				    u32 features)
55d9878031SMaxime Ripard {
56d9878031SMaxime Ripard 	struct drm_driver *driver;
57d9878031SMaxime Ripard 
58139a2785SThomas Hellström 	driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
59d9878031SMaxime Ripard 	KUNIT_ASSERT_NOT_NULL(test, driver);
60d9878031SMaxime Ripard 
61d9878031SMaxime Ripard 	driver->driver_features = features;
62d9878031SMaxime Ripard 
63d9878031SMaxime Ripard 	return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
64d9878031SMaxime Ripard 							       size, offset,
65d9878031SMaxime Ripard 							       driver);
66d9878031SMaxime Ripard }
670c3627c7SMaxime Ripard 
68a9143c58SMaxime Ripard /**
69a9143c58SMaxime Ripard  * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
70a9143c58SMaxime Ripard  * @_test: The test context object
71a9143c58SMaxime Ripard  * @_dev: The parent device object
72a9143c58SMaxime Ripard  * @_type: the type of the struct which contains struct &drm_device
73a9143c58SMaxime Ripard  * @_member: the name of the &drm_device within @_type.
74a9143c58SMaxime Ripard  * @_features: Mocked DRM device driver features
75a9143c58SMaxime Ripard  *
76a9143c58SMaxime Ripard  * This function creates a struct &drm_driver and will create a struct
77a9143c58SMaxime Ripard  * &drm_device from @_dev and that driver.
78a9143c58SMaxime Ripard  *
79a9143c58SMaxime Ripard  * @_dev should be allocated using drm_kunit_helper_alloc_device().
80a9143c58SMaxime Ripard  *
81a9143c58SMaxime Ripard  * The driver is tied to the @_test context and will get cleaned at the
82a9143c58SMaxime Ripard  * end of the test. The drm_device is allocated through
83a9143c58SMaxime Ripard  * devm_drm_dev_alloc() and will thus be freed through a device-managed
84a9143c58SMaxime Ripard  * resource.
85a9143c58SMaxime Ripard  *
86a9143c58SMaxime Ripard  * Returns:
87a9143c58SMaxime Ripard  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
88a9143c58SMaxime Ripard  */
89a9143c58SMaxime Ripard #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat)	\
90a9143c58SMaxime Ripard 	((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev,		\
91a9143c58SMaxime Ripard 						      sizeof(_type),		\
92a9143c58SMaxime Ripard 						      offsetof(_type, _member),	\
93a9143c58SMaxime Ripard 						      _feat))
946e193f9fSMaxime Ripard struct drm_modeset_acquire_ctx *
956e193f9fSMaxime Ripard drm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
96a9143c58SMaxime Ripard 
97394ba10eSMaxime Ripard struct drm_atomic_state *
98394ba10eSMaxime Ripard drm_kunit_helper_atomic_state_alloc(struct kunit *test,
99394ba10eSMaxime Ripard 				    struct drm_device *drm,
100394ba10eSMaxime Ripard 				    struct drm_modeset_acquire_ctx *ctx);
101394ba10eSMaxime Ripard 
1020c3627c7SMaxime Ripard #endif // DRM_KUNIT_HELPERS_H_
103