1 // SPDX-License-Identifier: GPL-2.0
3 #ifndef DRM_KUNIT_HELPERS_H_
4 #define DRM_KUNIT_HELPERS_H_
6 #include <drm/drm_drv.h>
8 #include <linux/device.h>
10 #include <kunit/test.h>
15 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
16 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
19 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
21 size_t size, size_t offset,
22 const struct drm_driver *driver);
25 * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
26 * @_test: The test context object
27 * @_dev: The parent device object
28 * @_type: the type of the struct which contains struct &drm_device
29 * @_member: the name of the &drm_device within @_type.
30 * @_drv: Mocked DRM device driver features
32 * This function creates a struct &drm_device from @_dev and @_drv.
34 * @_dev should be allocated using drm_kunit_helper_alloc_device().
36 * The driver is tied to the @_test context and will get cleaned at the
37 * end of the test. The drm_device is allocated through
38 * devm_drm_dev_alloc() and will thus be freed through a device-managed
42 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
44 #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv) \
45 ((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, \
47 offsetof(_type, _member), \
50 static inline struct drm_device *
51 __drm_kunit_helper_alloc_drm_device(struct kunit *test,
53 size_t size, size_t offset,
56 struct drm_driver *driver;
58 driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
59 KUNIT_ASSERT_NOT_NULL(test, driver);
61 driver->driver_features = features;
63 return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
69 * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
70 * @_test: The test context object
71 * @_dev: The parent device object
72 * @_type: the type of the struct which contains struct &drm_device
73 * @_member: the name of the &drm_device within @_type.
74 * @_features: Mocked DRM device driver features
76 * This function creates a struct &drm_driver and will create a struct
77 * &drm_device from @_dev and that driver.
79 * @_dev should be allocated using drm_kunit_helper_alloc_device().
81 * The driver is tied to the @_test context and will get cleaned at the
82 * end of the test. The drm_device is allocated through
83 * devm_drm_dev_alloc() and will thus be freed through a device-managed
87 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
89 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
90 ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
92 offsetof(_type, _member), \
94 struct drm_modeset_acquire_ctx *
95 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
97 struct drm_atomic_state *
98 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
99 struct drm_device *drm,
100 struct drm_modeset_acquire_ctx *ctx);
102 #endif // DRM_KUNIT_HELPERS_H_