1 // SPDX-License-Identifier: GPL-2.0
3 #include <drm/drm_atomic.h>
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_drv.h>
6 #include <drm/drm_fourcc.h>
7 #include <drm/drm_kunit_helpers.h>
8 #include <drm/drm_managed.h>
10 #include <kunit/device.h>
11 #include <kunit/resource.h>
13 #include <linux/device.h>
14 #include <linux/platform_device.h>
16 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
18 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
19 .atomic_check = drm_atomic_helper_check,
20 .atomic_commit = drm_atomic_helper_commit,
24 * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
25 * @test: The test context object
27 * This allocates a fake struct &device to create a mock for a KUnit
28 * test. The device will also be bound to a fake driver. It will thus be
29 * able to leverage the usual infrastructure and most notably the
30 * device-managed resources just like a "real" device.
32 * Resources will be cleaned up automatically, but the removal can be
33 * forced using @drm_kunit_helper_free_device.
36 * A pointer to the new device, or an ERR_PTR() otherwise.
38 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
40 return kunit_device_register(test, KUNIT_DEVICE_NAME);
42 EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
45 * drm_kunit_helper_free_device - Frees a mock device
46 * @test: The test context object
47 * @dev: The device to free
49 * Frees a device allocated with drm_kunit_helper_alloc_device().
51 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
53 kunit_device_unregister(test, dev);
55 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
58 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
60 size_t size, size_t offset,
61 const struct drm_driver *driver)
63 struct drm_device *drm;
67 container = __devm_drm_dev_alloc(dev, driver, size, offset);
68 if (IS_ERR(container))
69 return ERR_CAST(container);
71 drm = container + offset;
72 drm->mode_config.funcs = &drm_mode_config_funcs;
74 ret = drmm_mode_config_init(drm);
80 EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
82 static void action_drm_release_context(void *ptr)
84 struct drm_modeset_acquire_ctx *ctx = ptr;
86 drm_modeset_drop_locks(ctx);
87 drm_modeset_acquire_fini(ctx);
91 * drm_kunit_helper_acquire_ctx_alloc - Allocates an acquire context
92 * @test: The test context object
94 * Allocates and initializes a modeset acquire context.
96 * The context is tied to the kunit test context, so we must not call
97 * drm_modeset_acquire_fini() on it, it will be done so automatically.
100 * An ERR_PTR on error, a pointer to the newly allocated context otherwise
102 struct drm_modeset_acquire_ctx *
103 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test)
105 struct drm_modeset_acquire_ctx *ctx;
108 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
109 KUNIT_ASSERT_NOT_NULL(test, ctx);
111 drm_modeset_acquire_init(ctx, 0);
113 ret = kunit_add_action_or_reset(test,
114 action_drm_release_context,
121 EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc);
123 static void kunit_action_drm_atomic_state_put(void *ptr)
125 struct drm_atomic_state *state = ptr;
127 drm_atomic_state_put(state);
131 * drm_kunit_helper_atomic_state_alloc - Allocates an atomic state
132 * @test: The test context object
133 * @drm: The device to alloc the state for
134 * @ctx: Locking context for that atomic update
136 * Allocates a empty atomic state.
138 * The state is tied to the kunit test context, so we must not call
139 * drm_atomic_state_put() on it, it will be done so automatically.
142 * An ERR_PTR on error, a pointer to the newly allocated state otherwise
144 struct drm_atomic_state *
145 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
146 struct drm_device *drm,
147 struct drm_modeset_acquire_ctx *ctx)
149 struct drm_atomic_state *state;
152 state = drm_atomic_state_alloc(drm);
154 return ERR_PTR(-ENOMEM);
156 ret = kunit_add_action_or_reset(test,
157 kunit_action_drm_atomic_state_put,
162 state->acquire_ctx = ctx;
166 EXPORT_SYMBOL_GPL(drm_kunit_helper_atomic_state_alloc);
168 static const uint32_t default_plane_formats[] = {
172 static const uint64_t default_plane_modifiers[] = {
173 DRM_FORMAT_MOD_LINEAR,
174 DRM_FORMAT_MOD_INVALID
177 static const struct drm_plane_helper_funcs default_plane_helper_funcs = {
180 static const struct drm_plane_funcs default_plane_funcs = {
181 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
182 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
183 .reset = drm_atomic_helper_plane_reset,
187 * drm_kunit_helper_create_primary_plane - Creates a mock primary plane for a KUnit test
188 * @test: The test context object
189 * @drm: The device to alloc the plane for
190 * @funcs: Callbacks for the new plane. Optional.
191 * @helper_funcs: Helpers callbacks for the new plane. Optional.
192 * @formats: array of supported formats (DRM_FORMAT\_\*). Optional.
193 * @num_formats: number of elements in @formats
194 * @modifiers: array of struct drm_format modifiers terminated by
195 * DRM_FORMAT_MOD_INVALID. Optional.
197 * This allocates and initializes a mock struct &drm_plane meant to be
198 * part of a mock device for a KUnit test.
200 * Resources will be cleaned up automatically.
202 * @funcs will default to the default helpers implementations.
203 * @helper_funcs will default to an empty implementation. @formats will
204 * default to XRGB8888 only. @modifiers will default to a linear
208 * A pointer to the new plane, or an ERR_PTR() otherwise.
211 drm_kunit_helper_create_primary_plane(struct kunit *test,
212 struct drm_device *drm,
213 const struct drm_plane_funcs *funcs,
214 const struct drm_plane_helper_funcs *helper_funcs,
215 const uint32_t *formats,
216 unsigned int num_formats,
217 const uint64_t *modifiers)
219 struct drm_plane *plane;
222 funcs = &default_plane_funcs;
225 helper_funcs = &default_plane_helper_funcs;
227 if (!formats || !num_formats) {
228 formats = default_plane_formats;
229 num_formats = ARRAY_SIZE(default_plane_formats);
233 modifiers = default_plane_modifiers;
235 plane = __drmm_universal_plane_alloc(drm,
236 sizeof(struct drm_plane), 0,
241 default_plane_modifiers,
242 DRM_PLANE_TYPE_PRIMARY,
244 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
246 drm_plane_helper_add(plane, helper_funcs);
250 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_primary_plane);
252 static const struct drm_crtc_helper_funcs default_crtc_helper_funcs = {
255 static const struct drm_crtc_funcs default_crtc_funcs = {
256 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
257 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
258 .reset = drm_atomic_helper_crtc_reset,
262 * drm_kunit_helper_create_crtc - Creates a mock CRTC for a KUnit test
263 * @test: The test context object
264 * @drm: The device to alloc the plane for
265 * @primary: Primary plane for CRTC
266 * @cursor: Cursor plane for CRTC. Optional.
267 * @funcs: Callbacks for the new plane. Optional.
268 * @helper_funcs: Helpers callbacks for the new plane. Optional.
270 * This allocates and initializes a mock struct &drm_crtc meant to be
271 * part of a mock device for a KUnit test.
273 * Resources will be cleaned up automatically.
275 * @funcs will default to the default helpers implementations.
276 * @helper_funcs will default to an empty implementation.
279 * A pointer to the new CRTC, or an ERR_PTR() otherwise.
282 drm_kunit_helper_create_crtc(struct kunit *test,
283 struct drm_device *drm,
284 struct drm_plane *primary,
285 struct drm_plane *cursor,
286 const struct drm_crtc_funcs *funcs,
287 const struct drm_crtc_helper_funcs *helper_funcs)
289 struct drm_crtc *crtc;
293 funcs = &default_crtc_funcs;
296 helper_funcs = &default_crtc_helper_funcs;
298 crtc = drmm_kzalloc(drm, sizeof(*crtc), GFP_KERNEL);
299 KUNIT_ASSERT_NOT_NULL(test, crtc);
301 ret = drmm_crtc_init_with_planes(drm, crtc,
306 KUNIT_ASSERT_EQ(test, ret, 0);
308 drm_crtc_helper_add(crtc, helper_funcs);
312 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
315 MODULE_DESCRIPTION("KUnit test suite helper functions");
316 MODULE_LICENSE("GPL");