1 // SPDX-License-Identifier: GPL-2.0
3 #ifndef DRM_KUNIT_HELPERS_H_
4 #define DRM_KUNIT_HELPERS_H_
6 #include <linux/device.h>
8 #include <kunit/test.h>
13 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
14 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
17 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
19 size_t size, size_t offset,
20 const struct drm_driver *driver);
23 * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
24 * @_test: The test context object
25 * @_dev: The parent device object
26 * @_type: the type of the struct which contains struct &drm_device
27 * @_member: the name of the &drm_device within @_type.
28 * @_drv: Mocked DRM device driver features
30 * This function creates a struct &drm_device from @_dev and @_drv.
32 * @_dev should be allocated using drm_kunit_helper_alloc_device().
34 * The driver is tied to the @_test context and will get cleaned at the
35 * end of the test. The drm_device is allocated through
36 * devm_drm_dev_alloc() and will thus be freed through a device-managed
40 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
42 #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv) \
43 ((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, \
45 offsetof(_type, _member), \
48 static inline struct drm_device *
49 __drm_kunit_helper_alloc_drm_device(struct kunit *test,
51 size_t size, size_t offset,
54 struct drm_driver *driver;
56 driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
57 KUNIT_ASSERT_NOT_NULL(test, driver);
59 driver->driver_features = features;
61 return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
67 * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
68 * @_test: The test context object
69 * @_dev: The parent device object
70 * @_type: the type of the struct which contains struct &drm_device
71 * @_member: the name of the &drm_device within @_type.
72 * @_features: Mocked DRM device driver features
74 * This function creates a struct &drm_driver and will create a struct
75 * &drm_device from @_dev and that driver.
77 * @_dev should be allocated using drm_kunit_helper_alloc_device().
79 * The driver is tied to the @_test context and will get cleaned at the
80 * end of the test. The drm_device is allocated through
81 * devm_drm_dev_alloc() and will thus be freed through a device-managed
85 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
87 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
88 ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
90 offsetof(_type, _member), \
92 struct drm_modeset_acquire_ctx *
93 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
95 struct drm_atomic_state *
96 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
97 struct drm_device *drm,
98 struct drm_modeset_acquire_ctx *ctx);
100 #endif // DRM_KUNIT_HELPERS_H_