xref: /openbmc/linux/drivers/gpu/drm/tests/drm_kunit_helpers.c (revision d699090510c3223641a23834b4710e2d4309a6ad)
1 // SPDX-License-Identifier: GPL-2.0
2 
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>
10 
11 #include <kunit/resource.h>
12 
13 #include <linux/device.h>
14 #include <linux/platform_device.h>
15 
16 #define KUNIT_DEVICE_NAME	"drm-kunit-mock-device"
17 
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,
21 };
22 
fake_probe(struct platform_device * pdev)23 static int fake_probe(struct platform_device *pdev)
24 {
25 	return 0;
26 }
27 
28 static struct platform_driver fake_platform_driver = {
29 	.probe	= fake_probe,
30 	.driver = {
31 		.name	= KUNIT_DEVICE_NAME,
32 	},
33 };
34 
kunit_action_platform_driver_unregister(void * ptr)35 static void kunit_action_platform_driver_unregister(void *ptr)
36 {
37 	struct platform_driver *drv = ptr;
38 
39 	platform_driver_unregister(drv);
40 
41 }
42 
kunit_action_platform_device_put(void * ptr)43 static void kunit_action_platform_device_put(void *ptr)
44 {
45 	struct platform_device *pdev = ptr;
46 
47 	platform_device_put(pdev);
48 }
49 
kunit_action_platform_device_del(void * ptr)50 static void kunit_action_platform_device_del(void *ptr)
51 {
52 	struct platform_device *pdev = ptr;
53 
54 	platform_device_del(pdev);
55 }
56 
57 /**
58  * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
59  * @test: The test context object
60  *
61  * This allocates a fake struct &device to create a mock for a KUnit
62  * test. The device will also be bound to a fake driver. It will thus be
63  * able to leverage the usual infrastructure and most notably the
64  * device-managed resources just like a "real" device.
65  *
66  * Resources will be cleaned up automatically, but the removal can be
67  * forced using @drm_kunit_helper_free_device.
68  *
69  * Returns:
70  * A pointer to the new device, or an ERR_PTR() otherwise.
71  */
drm_kunit_helper_alloc_device(struct kunit * test)72 struct device *drm_kunit_helper_alloc_device(struct kunit *test)
73 {
74 	struct platform_device *pdev;
75 	int ret;
76 
77 	ret = platform_driver_register(&fake_platform_driver);
78 	KUNIT_ASSERT_EQ(test, ret, 0);
79 
80 	ret = kunit_add_action_or_reset(test,
81 					kunit_action_platform_driver_unregister,
82 					&fake_platform_driver);
83 	KUNIT_ASSERT_EQ(test, ret, 0);
84 
85 	pdev = platform_device_alloc(KUNIT_DEVICE_NAME, PLATFORM_DEVID_NONE);
86 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
87 
88 	ret = kunit_add_action_or_reset(test,
89 					kunit_action_platform_device_put,
90 					pdev);
91 	KUNIT_ASSERT_EQ(test, ret, 0);
92 
93 	ret = platform_device_add(pdev);
94 	KUNIT_ASSERT_EQ(test, ret, 0);
95 
96 	ret = kunit_add_action_or_reset(test,
97 					kunit_action_platform_device_del,
98 					pdev);
99 	KUNIT_ASSERT_EQ(test, ret, 0);
100 
101 	return &pdev->dev;
102 }
103 EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
104 
105 /**
106  * drm_kunit_helper_free_device - Frees a mock device
107  * @test: The test context object
108  * @dev: The device to free
109  *
110  * Frees a device allocated with drm_kunit_helper_alloc_device().
111  */
drm_kunit_helper_free_device(struct kunit * test,struct device * dev)112 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
113 {
114 	struct platform_device *pdev = to_platform_device(dev);
115 
116 	kunit_release_action(test,
117 			     kunit_action_platform_device_del,
118 			     pdev);
119 
120 	kunit_release_action(test,
121 			     kunit_action_platform_device_put,
122 			     pdev);
123 
124 	kunit_release_action(test,
125 			     kunit_action_platform_driver_unregister,
126 			     &fake_platform_driver);
127 }
128 EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
129 
130 struct drm_device *
__drm_kunit_helper_alloc_drm_device_with_driver(struct kunit * test,struct device * dev,size_t size,size_t offset,const struct drm_driver * driver)131 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
132 						struct device *dev,
133 						size_t size, size_t offset,
134 						const struct drm_driver *driver)
135 {
136 	struct drm_device *drm;
137 	void *container;
138 	int ret;
139 
140 	container = __devm_drm_dev_alloc(dev, driver, size, offset);
141 	if (IS_ERR(container))
142 		return ERR_CAST(container);
143 
144 	drm = container + offset;
145 	drm->mode_config.funcs = &drm_mode_config_funcs;
146 
147 	ret = drmm_mode_config_init(drm);
148 	if (ret)
149 		return ERR_PTR(ret);
150 
151 	return drm;
152 }
153 EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
154 
action_drm_release_context(void * ptr)155 static void action_drm_release_context(void *ptr)
156 {
157 	struct drm_modeset_acquire_ctx *ctx = ptr;
158 
159 	drm_modeset_drop_locks(ctx);
160 	drm_modeset_acquire_fini(ctx);
161 }
162 
163 /**
164  * drm_kunit_helper_acquire_ctx_alloc - Allocates an acquire context
165  * @test: The test context object
166  *
167  * Allocates and initializes a modeset acquire context.
168  *
169  * The context is tied to the kunit test context, so we must not call
170  * drm_modeset_acquire_fini() on it, it will be done so automatically.
171  *
172  * Returns:
173  * An ERR_PTR on error, a pointer to the newly allocated context otherwise
174  */
175 struct drm_modeset_acquire_ctx *
drm_kunit_helper_acquire_ctx_alloc(struct kunit * test)176 drm_kunit_helper_acquire_ctx_alloc(struct kunit *test)
177 {
178 	struct drm_modeset_acquire_ctx *ctx;
179 	int ret;
180 
181 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
182 	KUNIT_ASSERT_NOT_NULL(test, ctx);
183 
184 	drm_modeset_acquire_init(ctx, 0);
185 
186 	ret = kunit_add_action_or_reset(test,
187 					action_drm_release_context,
188 					ctx);
189 	if (ret)
190 		return ERR_PTR(ret);
191 
192 	return ctx;
193 }
194 EXPORT_SYMBOL_GPL(drm_kunit_helper_acquire_ctx_alloc);
195 
kunit_action_drm_atomic_state_put(void * ptr)196 static void kunit_action_drm_atomic_state_put(void *ptr)
197 {
198 	struct drm_atomic_state *state = ptr;
199 
200 	drm_atomic_state_put(state);
201 }
202 
203 /**
204  * drm_kunit_helper_atomic_state_alloc - Allocates an atomic state
205  * @test: The test context object
206  * @drm: The device to alloc the state for
207  * @ctx: Locking context for that atomic update
208  *
209  * Allocates a empty atomic state.
210  *
211  * The state is tied to the kunit test context, so we must not call
212  * drm_atomic_state_put() on it, it will be done so automatically.
213  *
214  * Returns:
215  * An ERR_PTR on error, a pointer to the newly allocated state otherwise
216  */
217 struct drm_atomic_state *
drm_kunit_helper_atomic_state_alloc(struct kunit * test,struct drm_device * drm,struct drm_modeset_acquire_ctx * ctx)218 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
219 				    struct drm_device *drm,
220 				    struct drm_modeset_acquire_ctx *ctx)
221 {
222 	struct drm_atomic_state *state;
223 	int ret;
224 
225 	state = drm_atomic_state_alloc(drm);
226 	if (!state)
227 		return ERR_PTR(-ENOMEM);
228 
229 	ret = kunit_add_action_or_reset(test,
230 					kunit_action_drm_atomic_state_put,
231 					state);
232 	if (ret)
233 		return ERR_PTR(ret);
234 
235 	state->acquire_ctx = ctx;
236 
237 	return state;
238 }
239 EXPORT_SYMBOL_GPL(drm_kunit_helper_atomic_state_alloc);
240 
241 static const uint32_t default_plane_formats[] = {
242 	DRM_FORMAT_XRGB8888,
243 };
244 
245 static const uint64_t default_plane_modifiers[] = {
246 	DRM_FORMAT_MOD_LINEAR,
247 	DRM_FORMAT_MOD_INVALID
248 };
249 
250 static const struct drm_plane_helper_funcs default_plane_helper_funcs = {
251 };
252 
253 static const struct drm_plane_funcs default_plane_funcs = {
254 	.atomic_destroy_state	= drm_atomic_helper_plane_destroy_state,
255 	.atomic_duplicate_state	= drm_atomic_helper_plane_duplicate_state,
256 	.reset			= drm_atomic_helper_plane_reset,
257 };
258 
259 /**
260  * drm_kunit_helper_create_primary_plane - Creates a mock primary plane for a KUnit test
261  * @test: The test context object
262  * @drm: The device to alloc the plane for
263  * @funcs: Callbacks for the new plane. Optional.
264  * @helper_funcs: Helpers callbacks for the new plane. Optional.
265  * @formats: array of supported formats (DRM_FORMAT\_\*). Optional.
266  * @num_formats: number of elements in @formats
267  * @modifiers: array of struct drm_format modifiers terminated by
268  *             DRM_FORMAT_MOD_INVALID. Optional.
269  *
270  * This allocates and initializes a mock struct &drm_plane meant to be
271  * part of a mock device for a KUnit test.
272  *
273  * Resources will be cleaned up automatically.
274  *
275  * @funcs will default to the default helpers implementations.
276  * @helper_funcs will default to an empty implementation. @formats will
277  * default to XRGB8888 only. @modifiers will default to a linear
278  * modifier only.
279  *
280  * Returns:
281  * A pointer to the new plane, or an ERR_PTR() otherwise.
282  */
283 struct drm_plane *
drm_kunit_helper_create_primary_plane(struct kunit * test,struct drm_device * drm,const struct drm_plane_funcs * funcs,const struct drm_plane_helper_funcs * helper_funcs,const uint32_t * formats,unsigned int num_formats,const uint64_t * modifiers)284 drm_kunit_helper_create_primary_plane(struct kunit *test,
285 				      struct drm_device *drm,
286 				      const struct drm_plane_funcs *funcs,
287 				      const struct drm_plane_helper_funcs *helper_funcs,
288 				      const uint32_t *formats,
289 				      unsigned int num_formats,
290 				      const uint64_t *modifiers)
291 {
292 	struct drm_plane *plane;
293 
294 	if (!funcs)
295 		funcs = &default_plane_funcs;
296 
297 	if (!helper_funcs)
298 		helper_funcs = &default_plane_helper_funcs;
299 
300 	if (!formats || !num_formats) {
301 		formats = default_plane_formats;
302 		num_formats = ARRAY_SIZE(default_plane_formats);
303 	}
304 
305 	if (!modifiers)
306 		modifiers = default_plane_modifiers;
307 
308 	plane = __drmm_universal_plane_alloc(drm,
309 					     sizeof(struct drm_plane), 0,
310 					     0,
311 					     funcs,
312 					     formats,
313 					     num_formats,
314 					     default_plane_modifiers,
315 					     DRM_PLANE_TYPE_PRIMARY,
316 					     NULL);
317 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
318 
319 	drm_plane_helper_add(plane, helper_funcs);
320 
321 	return plane;
322 }
323 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_primary_plane);
324 
325 static const struct drm_crtc_helper_funcs default_crtc_helper_funcs = {
326 };
327 
328 static const struct drm_crtc_funcs default_crtc_funcs = {
329 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
330 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
331 	.reset                  = drm_atomic_helper_crtc_reset,
332 };
333 
334 /**
335  * drm_kunit_helper_create_crtc - Creates a mock CRTC for a KUnit test
336  * @test: The test context object
337  * @drm: The device to alloc the plane for
338  * @primary: Primary plane for CRTC
339  * @cursor: Cursor plane for CRTC. Optional.
340  * @funcs: Callbacks for the new plane. Optional.
341  * @helper_funcs: Helpers callbacks for the new plane. Optional.
342  *
343  * This allocates and initializes a mock struct &drm_crtc meant to be
344  * part of a mock device for a KUnit test.
345  *
346  * Resources will be cleaned up automatically.
347  *
348  * @funcs will default to the default helpers implementations.
349  * @helper_funcs will default to an empty implementation.
350  *
351  * Returns:
352  * A pointer to the new CRTC, or an ERR_PTR() otherwise.
353  */
354 struct drm_crtc *
drm_kunit_helper_create_crtc(struct kunit * test,struct drm_device * drm,struct drm_plane * primary,struct drm_plane * cursor,const struct drm_crtc_funcs * funcs,const struct drm_crtc_helper_funcs * helper_funcs)355 drm_kunit_helper_create_crtc(struct kunit *test,
356 			     struct drm_device *drm,
357 			     struct drm_plane *primary,
358 			     struct drm_plane *cursor,
359 			     const struct drm_crtc_funcs *funcs,
360 			     const struct drm_crtc_helper_funcs *helper_funcs)
361 {
362 	struct drm_crtc *crtc;
363 	int ret;
364 
365 	if (!funcs)
366 		funcs = &default_crtc_funcs;
367 
368 	if (!helper_funcs)
369 		helper_funcs = &default_crtc_helper_funcs;
370 
371 	crtc = drmm_kzalloc(drm, sizeof(*crtc), GFP_KERNEL);
372 	KUNIT_ASSERT_NOT_NULL(test, crtc);
373 
374 	ret = drmm_crtc_init_with_planes(drm, crtc,
375 					 primary,
376 					 cursor,
377 					 funcs,
378 					 NULL);
379 	KUNIT_ASSERT_EQ(test, ret, 0);
380 
381 	drm_crtc_helper_add(crtc, helper_funcs);
382 
383 	return crtc;
384 }
385 EXPORT_SYMBOL_GPL(drm_kunit_helper_create_crtc);
386 
kunit_action_drm_mode_destroy(void * ptr)387 static void kunit_action_drm_mode_destroy(void *ptr)
388 {
389 	struct drm_display_mode *mode = ptr;
390 
391 	drm_mode_destroy(NULL, mode);
392 }
393 
394 /**
395  * drm_kunit_add_mode_destroy_action() - Add a drm_destroy_mode kunit action
396  * @test: The test context object
397  * @mode: The drm_display_mode to destroy eventually
398  *
399  * Registers a kunit action that will destroy the drm_display_mode at
400  * the end of the test.
401  *
402  * If an error occurs, the drm_display_mode will be destroyed.
403  *
404  * Returns:
405  * 0 on success, an error code otherwise.
406  */
drm_kunit_add_mode_destroy_action(struct kunit * test,struct drm_display_mode * mode)407 int drm_kunit_add_mode_destroy_action(struct kunit *test,
408 				      struct drm_display_mode *mode)
409 {
410 	return kunit_add_action_or_reset(test,
411 					 kunit_action_drm_mode_destroy,
412 					 mode);
413 }
414 EXPORT_SYMBOL_GPL(drm_kunit_add_mode_destroy_action);
415 
416 /**
417  * drm_kunit_display_mode_from_cea_vic() - return a mode for CEA VIC for a KUnit test
418  * @test: The test context object
419  * @dev: DRM device
420  * @video_code: CEA VIC of the mode
421  *
422  * Creates a new mode matching the specified CEA VIC for a KUnit test.
423  *
424  * Resources will be cleaned up automatically.
425  *
426  * Returns: A new drm_display_mode on success or NULL on failure
427  */
428 struct drm_display_mode *
drm_kunit_display_mode_from_cea_vic(struct kunit * test,struct drm_device * dev,u8 video_code)429 drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
430 				    u8 video_code)
431 {
432 	struct drm_display_mode *mode;
433 	int ret;
434 
435 	mode = drm_display_mode_from_cea_vic(dev, video_code);
436 	if (!mode)
437 		return NULL;
438 
439 	ret = kunit_add_action_or_reset(test,
440 					kunit_action_drm_mode_destroy,
441 					mode);
442 	if (ret)
443 		return NULL;
444 
445 	return mode;
446 }
447 EXPORT_SYMBOL_GPL(drm_kunit_display_mode_from_cea_vic);
448 
449 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
450 MODULE_LICENSE("GPL");
451