1 // SPDX-License-Identifier: GPL-2.0
3 #include <drm/drm_atomic.h>
4 #include <drm/drm_drv.h>
5 #include <drm/drm_kunit_helpers.h>
6 #include <drm/drm_managed.h>
8 #include <kunit/device.h>
9 #include <kunit/resource.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
14 #define KUNIT_DEVICE_NAME "drm-kunit-mock-device"
16 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
20 * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
21 * @test: The test context object
23 * This allocates a fake struct &device to create a mock for a KUnit
24 * test. The device will also be bound to a fake driver. It will thus be
25 * able to leverage the usual infrastructure and most notably the
26 * device-managed resources just like a "real" device.
28 * Resources will be cleaned up automatically, but the removal can be
29 * forced using @drm_kunit_helper_free_device.
32 * A pointer to the new device, or an ERR_PTR() otherwise.
34 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
36 return kunit_device_register(test, KUNIT_DEVICE_NAME);
38 EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
41 * drm_kunit_helper_free_device - Frees a mock device
42 * @test: The test context object
43 * @dev: The device to free
45 * Frees a device allocated with drm_kunit_helper_alloc_device().
47 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
49 kunit_device_unregister(test, dev);
51 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
54 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
56 size_t size, size_t offset,
57 const struct drm_driver *driver)
59 struct drm_device *drm;
63 container = __devm_drm_dev_alloc(dev, driver, size, offset);
64 if (IS_ERR(container))
65 return ERR_CAST(container);
67 drm = container + offset;
68 drm->mode_config.funcs = &drm_mode_config_funcs;
70 ret = drmm_mode_config_init(drm);
76 EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
78 static void action_drm_release_context(void *ptr)
80 struct drm_modeset_acquire_ctx *ctx = ptr;
82 drm_modeset_drop_locks(ctx);
83 drm_modeset_acquire_fini(ctx);
87 * drm_kunit_helper_acquire_ctx_alloc - Allocates an acquire context
88 * @test: The test context object
90 * Allocates and initializes a modeset acquire context.
92 * The context is tied to the kunit test context, so we must not call
93 * drm_modeset_acquire_fini() on it, it will be done so automatically.
96 * An ERR_PTR on error, a pointer to the newly allocated context otherwise
98 struct drm_modeset_acquire_ctx *
99 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test)
101 struct drm_modeset_acquire_ctx *ctx;
104 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
105 KUNIT_ASSERT_NOT_NULL(test, ctx);
107 drm_modeset_acquire_init(ctx, 0);
109 ret = kunit_add_action_or_reset(test,
110 action_drm_release_context,
117 EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc);
119 static void kunit_action_drm_atomic_state_put(void *ptr)
121 struct drm_atomic_state *state = ptr;
123 drm_atomic_state_put(state);
127 * drm_kunit_helper_atomic_state_alloc - Allocates an atomic state
128 * @test: The test context object
129 * @drm: The device to alloc the state for
130 * @ctx: Locking context for that atomic update
132 * Allocates a empty atomic state.
134 * The state is tied to the kunit test context, so we must not call
135 * drm_atomic_state_put() on it, it will be done so automatically.
138 * An ERR_PTR on error, a pointer to the newly allocated state otherwise
140 struct drm_atomic_state *
141 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
142 struct drm_device *drm,
143 struct drm_modeset_acquire_ctx *ctx)
145 struct drm_atomic_state *state;
148 state = drm_atomic_state_alloc(drm);
150 return ERR_PTR(-ENOMEM);
152 ret = kunit_add_action_or_reset(test,
153 kunit_action_drm_atomic_state_put,
158 state->acquire_ctx = ctx;
162 EXPORT_SYMBOL_GPL(drm_kunit_helper_atomic_state_alloc);
165 MODULE_LICENSE("GPL");