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>
12 struct drm_crtc_funcs
;
13 struct drm_crtc_helper_funcs
;
15 struct drm_plane_funcs
;
16 struct drm_plane_helper_funcs
;
19 struct device
*drm_kunit_helper_alloc_device(struct kunit
*test
);
20 void drm_kunit_helper_free_device(struct kunit
*test
, struct device
*dev
);
23 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit
*test
,
25 size_t size
, size_t offset
,
26 const struct drm_driver
*driver
);
29 * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
30 * @_test: The test context object
31 * @_dev: The parent device object
32 * @_type: the type of the struct which contains struct &drm_device
33 * @_member: the name of the &drm_device within @_type.
34 * @_drv: Mocked DRM device driver features
36 * This function creates a struct &drm_device from @_dev and @_drv.
38 * @_dev should be allocated using drm_kunit_helper_alloc_device().
40 * The driver is tied to the @_test context and will get cleaned at the
41 * end of the test. The drm_device is allocated through
42 * devm_drm_dev_alloc() and will thus be freed through a device-managed
46 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
48 #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv) \
49 ((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, \
51 offsetof(_type, _member), \
54 static inline struct drm_device
*
55 __drm_kunit_helper_alloc_drm_device(struct kunit
*test
,
57 size_t size
, size_t offset
,
60 struct drm_driver
*driver
;
62 driver
= devm_kzalloc(dev
, sizeof(*driver
), GFP_KERNEL
);
63 KUNIT_ASSERT_NOT_NULL(test
, driver
);
65 driver
->driver_features
= features
;
67 return __drm_kunit_helper_alloc_drm_device_with_driver(test
, dev
,
73 * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
74 * @_test: The test context object
75 * @_dev: The parent device object
76 * @_type: the type of the struct which contains struct &drm_device
77 * @_member: the name of the &drm_device within @_type.
78 * @_feat: Mocked DRM device driver features
80 * This function creates a struct &drm_driver and will create a struct
81 * &drm_device from @_dev and that driver.
83 * @_dev should be allocated using drm_kunit_helper_alloc_device().
85 * The driver is tied to the @_test context and will get cleaned at the
86 * end of the test. The drm_device is allocated through
87 * devm_drm_dev_alloc() and will thus be freed through a device-managed
91 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
93 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
94 ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
96 offsetof(_type, _member), \
98 struct drm_modeset_acquire_ctx
*
99 drm_kunit_helper_acquire_ctx_alloc(struct kunit
*test
);
101 struct drm_atomic_state
*
102 drm_kunit_helper_atomic_state_alloc(struct kunit
*test
,
103 struct drm_device
*drm
,
104 struct drm_modeset_acquire_ctx
*ctx
);
107 drm_kunit_helper_create_primary_plane(struct kunit
*test
,
108 struct drm_device
*drm
,
109 const struct drm_plane_funcs
*funcs
,
110 const struct drm_plane_helper_funcs
*helper_funcs
,
111 const uint32_t *formats
,
112 unsigned int num_formats
,
113 const uint64_t *modifiers
);
116 drm_kunit_helper_create_crtc(struct kunit
*test
,
117 struct drm_device
*drm
,
118 struct drm_plane
*primary
,
119 struct drm_plane
*cursor
,
120 const struct drm_crtc_funcs
*funcs
,
121 const struct drm_crtc_helper_funcs
*helper_funcs
);
123 struct drm_display_mode
*
124 drm_kunit_display_mode_from_cea_vic(struct kunit
*test
, struct drm_device
*dev
,
127 #endif // DRM_KUNIT_HELPERS_H_