xref: /linux/include/drm/drm_kunit_helpers.h (revision 785151f50ddacac06c7a3c5f3d31642794507fdf)
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 
12 struct drm_connector;
13 struct drm_crtc_funcs;
14 struct drm_crtc_helper_funcs;
15 struct drm_device;
16 struct drm_plane_funcs;
17 struct drm_plane_helper_funcs;
18 struct kunit;
19 
20 struct device *drm_kunit_helper_alloc_device(struct kunit *test);
21 void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
22 
23 struct drm_device *
24 __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
25 						struct device *dev,
26 						size_t size, size_t offset,
27 						const struct drm_driver *driver);
28 
29 /**
30  * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
31  * @_test: The test context object
32  * @_dev: The parent device object
33  * @_type: the type of the struct which contains struct &drm_device
34  * @_member: the name of the &drm_device within @_type.
35  * @_drv: Mocked DRM device driver features
36  *
37  * This function creates a struct &drm_device from @_dev and @_drv.
38  *
39  * @_dev should be allocated using drm_kunit_helper_alloc_device().
40  *
41  * The driver is tied to the @_test context and will get cleaned at the
42  * end of the test. The drm_device is allocated through
43  * devm_drm_dev_alloc() and will thus be freed through a device-managed
44  * resource.
45  *
46  * Returns:
47  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
48  */
49 #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv)	\
50 	((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev,			\
51 						       sizeof(_type),				\
52 						       offsetof(_type, _member),		\
53 						       _drv))
54 
55 static inline struct drm_device *
__drm_kunit_helper_alloc_drm_device(struct kunit * test,struct device * dev,size_t size,size_t offset,u32 features)56 __drm_kunit_helper_alloc_drm_device(struct kunit *test,
57 				    struct device *dev,
58 				    size_t size, size_t offset,
59 				    u32 features)
60 {
61 	struct drm_driver *driver;
62 
63 	driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
64 	KUNIT_ASSERT_NOT_NULL(test, driver);
65 
66 	driver->driver_features = features;
67 
68 	return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
69 							       size, offset,
70 							       driver);
71 }
72 
73 /**
74  * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
75  * @_test: The test context object
76  * @_dev: The parent device object
77  * @_type: the type of the struct which contains struct &drm_device
78  * @_member: the name of the &drm_device within @_type.
79  * @_feat: Mocked DRM device driver features
80  *
81  * This function creates a struct &drm_driver and will create a struct
82  * &drm_device from @_dev and that driver.
83  *
84  * @_dev should be allocated using drm_kunit_helper_alloc_device().
85  *
86  * The driver is tied to the @_test context and will get cleaned at the
87  * end of the test. The drm_device is allocated through
88  * devm_drm_dev_alloc() and will thus be freed through a device-managed
89  * resource.
90  *
91  * Returns:
92  * A pointer to the new drm_device, or an ERR_PTR() otherwise.
93  */
94 #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat)	\
95 	((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev,		\
96 						      sizeof(_type),		\
97 						      offsetof(_type, _member),	\
98 						      _feat))
99 
100 struct drm_atomic_state *
101 drm_kunit_helper_atomic_state_alloc(struct kunit *test,
102 				    struct drm_device *drm,
103 				    struct drm_modeset_acquire_ctx *ctx);
104 
105 struct drm_plane *
106 drm_kunit_helper_create_primary_plane(struct kunit *test,
107 				      struct drm_device *drm,
108 				      const struct drm_plane_funcs *funcs,
109 				      const struct drm_plane_helper_funcs *helper_funcs,
110 				      const uint32_t *formats,
111 				      unsigned int num_formats,
112 				      const uint64_t *modifiers);
113 
114 struct drm_crtc *
115 drm_kunit_helper_create_crtc(struct kunit *test,
116 			     struct drm_device *drm,
117 			     struct drm_plane *primary,
118 			     struct drm_plane *cursor,
119 			     const struct drm_crtc_funcs *funcs,
120 			     const struct drm_crtc_helper_funcs *helper_funcs);
121 
122 int drm_kunit_helper_enable_crtc_connector(struct kunit *test,
123 					   struct drm_device *drm,
124 					   struct drm_crtc *crtc,
125 					   struct drm_connector *connector,
126 					   const struct drm_display_mode *mode,
127 					   struct drm_modeset_acquire_ctx *ctx);
128 
129 int drm_kunit_add_mode_destroy_action(struct kunit *test,
130 				      struct drm_display_mode *mode);
131 
132 struct drm_display_mode *
133 drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
134 				    u8 video_code);
135 
136 #endif // DRM_KUNIT_HELPERS_H_
137