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_edid.h>
7 #include <drm/drm_fourcc.h>
8 #include <drm/drm_kunit_helpers.h>
9 #include <drm/drm_managed.h>
11 #include <kunit/device.h>
12 #include <kunit/resource.h>
14 #include <linux/device.h>
15 #include <linux/platform_device.h>
17 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
19 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
20 .atomic_check = drm_atomic_helper_check,
21 .atomic_commit = drm_atomic_helper_commit,
25 * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
26 * @test: The test context object
28 * This allocates a fake struct &device to create a mock for a KUnit
29 * test. The device will also be bound to a fake driver. It will thus be
30 * able to leverage the usual infrastructure and most notably the
31 * device-managed resources just like a "real" device.
33 * Resources will be cleaned up automatically, but the removal can be
34 * forced using @drm_kunit_helper_free_device.
37 * A pointer to the new device, or an ERR_PTR() otherwise.
39 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
41 return kunit_device_register(test, KUNIT_DEVICE_NAME);
43 EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
46 * drm_kunit_helper_free_device - Frees a mock device
47 * @test: The test context object
48 * @dev: The device to free
50 * Frees a device allocated with drm_kunit_helper_alloc_device().
52 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
54 kunit_device_unregister(test, dev);
56 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
59 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
61 size_t size, size_t offset,
62 const struct drm_driver *driver)
64 struct drm_device *drm;
68 container = __devm_drm_dev_alloc(dev, driver, size, offset);
69 if (IS_ERR(container))
70 return ERR_CAST(container);
72 drm = container + offset;
73 drm->mode_config.funcs = &drm_mode_config_funcs;
75 ret = drmm_mode_config_init(drm);
81 EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
83 static void action_drm_release_context(void *ptr)
85 struct drm_modeset_acquire_ctx *ctx = ptr;
87 drm_modeset_drop_locks(ctx);
88 drm_modeset_acquire_fini(ctx);
92 * drm_kunit_helper_acquire_ctx_alloc - Allocates an acquire context
93 * @test: The test context object
95 * Allocates and initializes a modeset acquire context.
97 * The context is tied to the kunit test context, so we must not call
98 * drm_modeset_acquire_fini() on it, it will be done so automatically.
101 * An ERR_PTR on error, a pointer to the newly allocated context otherwise
103 struct drm_modeset_acquire_ctx *
104 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test)
106 struct drm_modeset_acquire_ctx *ctx;
109 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
110 KUNIT_ASSERT_NOT_NULL(test, ctx);
112 drm_modeset_acquire_init(ctx, 0);
114 ret = kunit_add_action_or_reset(test,
115 action_drm_release_context,
122 EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc);
124 static void kunit_action_drm_atomic_state_put(void *ptr)
126 struct drm_atomic_state *state = ptr;
128 drm_atomic_state_put(state);
132 * drm_kunit_helper_atomic_state_alloc - Allocates an atomic state
133 * @test: The test context object
134 * @drm: The device to alloc the state for
135 * @ctx: Locking context for that atomic update
137 * Allocates a empty atomic state.
139 * The state is tied to the kunit test context, so we must not call
140 * drm_atomic_state_put() on it, it will be done so automatically.
143 * An ERR_PTR on error, a pointer to the newly allocated state otherwise
145 struct drm_atomic_state *
146 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
147 struct drm_device *drm,
148 struct drm_modeset_acquire_ctx *ctx)
150 struct drm_atomic_state *state;
153 state = drm_atomic_state_alloc(drm);
155 return ERR_PTR(-ENOMEM);
157 ret = kunit_add_action_or_reset(test,
158 kunit_action_drm_atomic_state_put,
163 state->acquire_ctx = ctx;
167 EXPORT_SYMBOL_GPL(drm_kunit_helper_atomic_state_alloc);
169 static const uint32_t default_plane_formats[] = {
173 static const uint64_t default_plane_modifiers[] = {
174 DRM_FORMAT_MOD_LINEAR,
175 DRM_FORMAT_MOD_INVALID
178 static const struct drm_plane_helper_funcs default_plane_helper_funcs = {
181 static const struct drm_plane_funcs default_plane_funcs = {
182 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
183 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
184 .reset = drm_atomic_helper_plane_reset,
188 * drm_kunit_helper_create_primary_plane - Creates a mock primary plane for a KUnit test
189 * @test: The test context object
190 * @drm: The device to alloc the plane for
191 * @funcs: Callbacks for the new plane. Optional.
192 * @helper_funcs: Helpers callbacks for the new plane. Optional.
193 * @formats: array of supported formats (DRM_FORMAT\_\*). Optional.
194 * @num_formats: number of elements in @formats
195 * @modifiers: array of struct drm_format modifiers terminated by
196 * DRM_FORMAT_MOD_INVALID. Optional.
198 * This allocates and initializes a mock struct &drm_plane meant to be
199 * part of a mock device for a KUnit test.
201 * Resources will be cleaned up automatically.
203 * @funcs will default to the default helpers implementations.
204 * @helper_funcs will default to an empty implementation. @formats will
205 * default to XRGB8888 only. @modifiers will default to a linear
209 * A pointer to the new plane, or an ERR_PTR() otherwise.
212 drm_kunit_helper_create_primary_plane(struct kunit *test,
213 struct drm_device *drm,
214 const struct drm_plane_funcs *funcs,
215 const struct drm_plane_helper_funcs *helper_funcs,
216 const uint32_t *formats,
217 unsigned int num_formats,
218 const uint64_t *modifiers)
220 struct drm_plane *plane;
223 funcs = &default_plane_funcs;
226 helper_funcs = &default_plane_helper_funcs;
228 if (!formats || !num_formats) {
229 formats = default_plane_formats;
230 num_formats = ARRAY_SIZE(default_plane_formats);
234 modifiers = default_plane_modifiers;
236 plane = __drmm_universal_plane_alloc(drm,
237 sizeof(struct drm_plane), 0,
242 default_plane_modifiers,
243 DRM_PLANE_TYPE_PRIMARY,
245 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
247 drm_plane_helper_add(plane, helper_funcs);
251 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_primary_plane);
253 static const struct drm_crtc_helper_funcs default_crtc_helper_funcs = {
256 static const struct drm_crtc_funcs default_crtc_funcs = {
257 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
258 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
259 .reset = drm_atomic_helper_crtc_reset,
263 * drm_kunit_helper_create_crtc - Creates a mock CRTC for a KUnit test
264 * @test: The test context object
265 * @drm: The device to alloc the plane for
266 * @primary: Primary plane for CRTC
267 * @cursor: Cursor plane for CRTC. Optional.
268 * @funcs: Callbacks for the new plane. Optional.
269 * @helper_funcs: Helpers callbacks for the new plane. Optional.
271 * This allocates and initializes a mock struct &drm_crtc meant to be
272 * part of a mock device for a KUnit test.
274 * Resources will be cleaned up automatically.
276 * @funcs will default to the default helpers implementations.
277 * @helper_funcs will default to an empty implementation.
280 * A pointer to the new CRTC, or an ERR_PTR() otherwise.
283 drm_kunit_helper_create_crtc(struct kunit *test,
284 struct drm_device *drm,
285 struct drm_plane *primary,
286 struct drm_plane *cursor,
287 const struct drm_crtc_funcs *funcs,
288 const struct drm_crtc_helper_funcs *helper_funcs)
290 struct drm_crtc *crtc;
294 funcs = &default_crtc_funcs;
297 helper_funcs = &default_crtc_helper_funcs;
299 crtc = drmm_kzalloc(drm, sizeof(*crtc), GFP_KERNEL);
300 KUNIT_ASSERT_NOT_NULL(test, crtc);
302 ret = drmm_crtc_init_with_planes(drm, crtc,
307 KUNIT_ASSERT_EQ(test, ret, 0);
309 drm_crtc_helper_add(crtc, helper_funcs);
313 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
315 static void kunit_action_drm_mode_destroy(void *ptr)
317 struct drm_display_mode *mode = ptr;
319 drm_mode_destroy(NULL, mode);
323 * drm_kunit_display_mode_from_cea_vic() - return a mode for CEA VIC for a KUnit test
324 * @test: The test context object
326 * @video_code: CEA VIC of the mode
328 * Creates a new mode matching the specified CEA VIC for a KUnit test.
330 * Resources will be cleaned up automatically.
332 * Returns: A new drm_display_mode on success or NULL on failure
334 struct drm_display_mode *
335 drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
338 struct drm_display_mode *mode;
341 mode = drm_display_mode_from_cea_vic(dev, video_code);
345 ret = kunit_add_action_or_reset(test,
346 kunit_action_drm_mode_destroy,
353 EXPORT_SYMBOL_GPL(drm_kunit_display_mode_from_cea_vic);
356 MODULE_DESCRIPTION("KUnit test suite helper functions");
357 MODULE_LICENSE("GPL");