xref: /linux/drivers/gpu/drm/vkms/vkms_drv.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 /**
4  * DOC: vkms (Virtual Kernel Modesetting)
5  *
6  * VKMS is a software-only model of a KMS driver that is useful for testing
7  * and for running X (or similar) on headless machines. VKMS aims to enable
8  * a virtual display with no need of a hardware display capability, releasing
9  * the GPU in DRM API tests.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/device/faux.h>
14 #include <linux/dma-mapping.h>
15 
16 #include <drm/clients/drm_client_setup.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fbdev_shmem.h>
22 #include <drm/drm_file.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_ioctl.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_print.h>
27 #include <drm/drm_probe_helper.h>
28 #include <drm/drm_gem_shmem_helper.h>
29 #include <drm/drm_vblank.h>
30 
31 #include "vkms_config.h"
32 #include "vkms_configfs.h"
33 #include "vkms_drv.h"
34 
35 #define DRIVER_NAME	"vkms"
36 #define DRIVER_DESC	"Virtual Kernel Mode Setting"
37 #define DRIVER_MAJOR	1
38 #define DRIVER_MINOR	0
39 
40 static struct vkms_config *default_config;
41 
42 static bool enable_cursor = true;
43 module_param_named(enable_cursor, enable_cursor, bool, 0444);
44 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
45 
46 static bool enable_writeback = true;
47 module_param_named(enable_writeback, enable_writeback, bool, 0444);
48 MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
49 
50 static bool enable_overlay;
51 module_param_named(enable_overlay, enable_overlay, bool, 0444);
52 MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
53 
54 static bool create_default_dev = true;
55 module_param_named(create_default_dev, create_default_dev, bool, 0444);
56 MODULE_PARM_DESC(create_default_dev, "Create or not the default VKMS device");
57 
58 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
59 
60 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
61 {
62 	struct drm_device *dev = old_state->dev;
63 	struct drm_crtc *crtc;
64 	struct drm_crtc_state *old_crtc_state;
65 	int i;
66 
67 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
68 
69 	drm_atomic_helper_commit_planes(dev, old_state, 0);
70 
71 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
72 
73 	drm_atomic_helper_fake_vblank(old_state);
74 
75 	drm_atomic_helper_commit_hw_done(old_state);
76 
77 	drm_atomic_helper_wait_for_flip_done(dev, old_state);
78 
79 	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
80 		struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(old_crtc_state);
81 
82 		flush_work(&vkms_state->composer_work);
83 	}
84 
85 	drm_atomic_helper_cleanup_planes(dev, old_state);
86 }
87 
88 static const struct drm_driver vkms_driver = {
89 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
90 	.fops			= &vkms_driver_fops,
91 	DRM_GEM_SHMEM_DRIVER_OPS,
92 	DRM_FBDEV_SHMEM_DRIVER_OPS,
93 
94 	.name			= DRIVER_NAME,
95 	.desc			= DRIVER_DESC,
96 	.major			= DRIVER_MAJOR,
97 	.minor			= DRIVER_MINOR,
98 };
99 
100 static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
101 {
102 	struct drm_crtc *crtc;
103 	struct drm_crtc_state *new_crtc_state;
104 	int i;
105 
106 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
107 		if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
108 			continue;
109 
110 		if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
111 		    > VKMS_LUT_SIZE)
112 			return -EINVAL;
113 	}
114 
115 	return drm_atomic_helper_check(dev, state);
116 }
117 
118 static const struct drm_mode_config_funcs vkms_mode_funcs = {
119 	.fb_create = drm_gem_fb_create,
120 	.atomic_check = vkms_atomic_check,
121 	.atomic_commit = drm_atomic_helper_commit,
122 };
123 
124 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
125 	.atomic_commit_tail = vkms_atomic_commit_tail,
126 };
127 
128 static int vkms_modeset_init(struct vkms_device *vkmsdev)
129 {
130 	struct drm_device *dev = &vkmsdev->drm;
131 	int ret;
132 
133 	ret = drmm_mode_config_init(dev);
134 	if (ret)
135 		return ret;
136 
137 	dev->mode_config.funcs = &vkms_mode_funcs;
138 	dev->mode_config.min_width = XRES_MIN;
139 	dev->mode_config.min_height = YRES_MIN;
140 	dev->mode_config.max_width = XRES_MAX;
141 	dev->mode_config.max_height = YRES_MAX;
142 	dev->mode_config.cursor_width = 512;
143 	dev->mode_config.cursor_height = 512;
144 	/*
145 	 * FIXME: There's a confusion between bpp and depth between this and
146 	 * fbdev helpers. We have to go with 0, meaning "pick the default",
147 	 * which is XRGB8888 in all cases.
148 	 */
149 	dev->mode_config.preferred_depth = 0;
150 	dev->mode_config.helper_private = &vkms_mode_config_helpers;
151 
152 	return vkms_output_init(vkmsdev);
153 }
154 
155 int vkms_create(struct vkms_config *config)
156 {
157 	int ret;
158 	struct faux_device *fdev;
159 	struct vkms_device *vkms_device;
160 	const char *dev_name;
161 
162 	dev_name = vkms_config_get_device_name(config);
163 	fdev = faux_device_create(dev_name, NULL, NULL);
164 	if (!fdev)
165 		return -ENODEV;
166 
167 	if (!devres_open_group(&fdev->dev, NULL, GFP_KERNEL)) {
168 		ret = -ENOMEM;
169 		goto out_unregister;
170 	}
171 
172 	vkms_device = devm_drm_dev_alloc(&fdev->dev, &vkms_driver,
173 					 struct vkms_device, drm);
174 	if (IS_ERR(vkms_device)) {
175 		ret = PTR_ERR(vkms_device);
176 		goto out_devres;
177 	}
178 	vkms_device->faux_dev = fdev;
179 	vkms_device->config = config;
180 	config->dev = vkms_device;
181 
182 	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
183 					   DMA_BIT_MASK(64));
184 
185 	if (ret) {
186 		DRM_ERROR("Could not initialize DMA support\n");
187 		goto out_devres;
188 	}
189 
190 	ret = drm_vblank_init(&vkms_device->drm,
191 			      vkms_config_get_num_crtcs(config));
192 	if (ret) {
193 		DRM_ERROR("Failed to vblank\n");
194 		goto out_devres;
195 	}
196 
197 	ret = vkms_modeset_init(vkms_device);
198 	if (ret)
199 		goto out_devres;
200 
201 	vkms_config_register_debugfs(vkms_device);
202 
203 	ret = drm_dev_register(&vkms_device->drm, 0);
204 	if (ret)
205 		goto out_devres;
206 
207 	drm_client_setup(&vkms_device->drm, NULL);
208 
209 	return 0;
210 
211 out_devres:
212 	devres_release_group(&fdev->dev, NULL);
213 out_unregister:
214 	faux_device_destroy(fdev);
215 	return ret;
216 }
217 
218 static int __init vkms_init(void)
219 {
220 	int ret;
221 	struct vkms_config *config;
222 
223 	ret = vkms_configfs_register();
224 	if (ret)
225 		return ret;
226 
227 	if (!create_default_dev)
228 		return 0;
229 
230 	config = vkms_config_default_create(enable_cursor, enable_writeback, enable_overlay);
231 	if (IS_ERR(config))
232 		return PTR_ERR(config);
233 
234 	ret = vkms_create(config);
235 	if (ret) {
236 		vkms_config_destroy(config);
237 		return ret;
238 	}
239 
240 	default_config = config;
241 
242 	return 0;
243 }
244 
245 void vkms_destroy(struct vkms_config *config)
246 {
247 	struct faux_device *fdev;
248 
249 	if (!config->dev) {
250 		DRM_INFO("vkms_device is NULL.\n");
251 		return;
252 	}
253 
254 	fdev = config->dev->faux_dev;
255 
256 	drm_dev_unregister(&config->dev->drm);
257 	drm_atomic_helper_shutdown(&config->dev->drm);
258 	devres_release_group(&fdev->dev, NULL);
259 	faux_device_destroy(fdev);
260 
261 	config->dev = NULL;
262 }
263 
264 static void __exit vkms_exit(void)
265 {
266 	vkms_configfs_unregister();
267 
268 	if (!default_config)
269 		return;
270 
271 	vkms_destroy(default_config);
272 	vkms_config_destroy(default_config);
273 }
274 
275 module_init(vkms_init);
276 module_exit(vkms_exit);
277 
278 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
279 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
280 MODULE_DESCRIPTION(DRIVER_DESC);
281 MODULE_LICENSE("GPL");
282