1 // SPDX-License-Identifier: GPL-2.0
3 #ifndef DRM_KUNIT_HELPERS_H_
4 #define DRM_KUNIT_HELPERS_H_
6 #include <kunit/test.h>
11 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
12 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
15 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
17 size_t size, size_t offset,
18 const struct drm_driver *driver);
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
28 * This function creates a struct &drm_device from @_dev and @_drv.
30 * @_dev should be allocated using drm_kunit_helper_alloc_device().
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
38 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
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, \
43 offsetof(_type, _member), \
46 static inline struct drm_device *
47 __drm_kunit_helper_alloc_drm_device(struct kunit *test,
49 size_t size, size_t offset,
52 struct drm_driver *driver;
54 driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
55 KUNIT_ASSERT_NOT_NULL(test, driver);
57 driver->driver_features = features;
59 return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
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
72 * This function creates a struct &drm_driver and will create a struct
73 * &drm_device from @_dev and that driver.
75 * @_dev should be allocated using drm_kunit_helper_alloc_device().
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
83 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
85 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
86 ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
88 offsetof(_type, _member), \
90 struct drm_modeset_acquire_ctx *
91 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
93 struct drm_atomic_state *
94 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
95 struct drm_device *drm,
96 struct drm_modeset_acquire_ctx *ctx);
98 #endif // DRM_KUNIT_HELPERS_H_