1// SPDX-License-Identifier: GPL-2.0
2
3#ifndef DRM_KUNIT_HELPERS_H_
4#define DRM_KUNIT_HELPERS_H_
5
6#include <drm/drm_drv.h>
7
8#include <linux/device.h>
9
10#include <kunit/test.h>
11
12struct drm_device;
13struct kunit;
14
15struct device *drm_kunit_helper_alloc_device(struct kunit *test);
16void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
17
18struct drm_device *
19__drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
20						struct device *dev,
21						size_t size, size_t offset,
22						const struct drm_driver *driver);
23
24/**
25 * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
26 * @_test: The test context object
27 * @_dev: The parent device object
28 * @_type: the type of the struct which contains struct &drm_device
29 * @_member: the name of the &drm_device within @_type.
30 * @_drv: Mocked DRM device driver features
31 *
32 * This function creates a struct &drm_device from @_dev and @_drv.
33 *
34 * @_dev should be allocated using drm_kunit_helper_alloc_device().
35 *
36 * The driver is tied to the @_test context and will get cleaned at the
37 * end of the test. The drm_device is allocated through
38 * devm_drm_dev_alloc() and will thus be freed through a device-managed
39 * resource.
40 *
41 * Returns:
42 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
43 */
44#define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv)	\
45	((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev,			\
46						       sizeof(_type),				\
47						       offsetof(_type, _member),		\
48						       _drv))
49
50static inline struct drm_device *
51__drm_kunit_helper_alloc_drm_device(struct kunit *test,
52				    struct device *dev,
53				    size_t size, size_t offset,
54				    u32 features)
55{
56	struct drm_driver *driver;
57
58	driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
59	KUNIT_ASSERT_NOT_NULL(test, driver);
60
61	driver->driver_features = features;
62
63	return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
64							       size, offset,
65							       driver);
66}
67
68/**
69 * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
70 * @_test: The test context object
71 * @_dev: The parent device object
72 * @_type: the type of the struct which contains struct &drm_device
73 * @_member: the name of the &drm_device within @_type.
74 * @_features: Mocked DRM device driver features
75 *
76 * This function creates a struct &drm_driver and will create a struct
77 * &drm_device from @_dev and that driver.
78 *
79 * @_dev should be allocated using drm_kunit_helper_alloc_device().
80 *
81 * The driver is tied to the @_test context and will get cleaned at the
82 * end of the test. The drm_device is allocated through
83 * devm_drm_dev_alloc() and will thus be freed through a device-managed
84 * resource.
85 *
86 * Returns:
87 * A pointer to the new drm_device, or an ERR_PTR() otherwise.
88 */
89#define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat)	\
90	((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev,		\
91						      sizeof(_type),		\
92						      offsetof(_type, _member),	\
93						      _feat))
94struct drm_modeset_acquire_ctx *
95drm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
96
97struct drm_atomic_state *
98drm_kunit_helper_atomic_state_alloc(struct kunit *test,
99				    struct drm_device *drm,
100				    struct drm_modeset_acquire_ctx *ctx);
101
102#endif // DRM_KUNIT_HELPERS_H_
103