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