1 // SPDX-License-Identifier: GPL-2.0+
4 * DOC: vkms (Virtual Kernel Modesetting)
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.
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
16 #include <drm/drm_gem.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_client_setup.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>
32 #include <drm/drm_print.h>
33 #include <drm/drm_debugfs.h>
35 #define DRIVER_NAME "vkms"
36 #define DRIVER_DESC "Virtual Kernel Mode Setting"
37 #define DRIVER_DATE "20180514"
38 #define DRIVER_MAJOR 1
39 #define DRIVER_MINOR 0
41 static struct vkms_config *default_config;
43 static bool enable_cursor = true;
44 module_param_named(enable_cursor, enable_cursor, bool, 0444);
45 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
47 static bool enable_writeback = true;
48 module_param_named(enable_writeback, enable_writeback, bool, 0444);
49 MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
51 static bool enable_overlay;
52 module_param_named(enable_overlay, enable_overlay, bool, 0444);
53 MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
55 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
57 static void vkms_release(struct drm_device *dev)
59 struct vkms_device *vkms = drm_device_to_vkms_device(dev);
61 if (vkms->output.composer_workq)
62 destroy_workqueue(vkms->output.composer_workq);
65 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
67 struct drm_device *dev = old_state->dev;
68 struct drm_crtc *crtc;
69 struct drm_crtc_state *old_crtc_state;
72 drm_atomic_helper_commit_modeset_disables(dev, old_state);
74 drm_atomic_helper_commit_planes(dev, old_state, 0);
76 drm_atomic_helper_commit_modeset_enables(dev, old_state);
78 drm_atomic_helper_fake_vblank(old_state);
80 drm_atomic_helper_commit_hw_done(old_state);
82 drm_atomic_helper_wait_for_flip_done(dev, old_state);
84 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
85 struct vkms_crtc_state *vkms_state =
86 to_vkms_crtc_state(old_crtc_state);
88 flush_work(&vkms_state->composer_work);
91 drm_atomic_helper_cleanup_planes(dev, old_state);
94 static int vkms_config_show(struct seq_file *m, void *data)
96 struct drm_debugfs_entry *entry = m->private;
97 struct drm_device *dev = entry->dev;
98 struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
100 seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback);
101 seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor);
102 seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay);
107 static const struct drm_debugfs_info vkms_config_debugfs_list[] = {
108 { "vkms_config", vkms_config_show, 0 },
111 static const struct drm_driver vkms_driver = {
112 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
113 .release = vkms_release,
114 .fops = &vkms_driver_fops,
115 DRM_GEM_SHMEM_DRIVER_OPS,
116 DRM_FBDEV_SHMEM_DRIVER_OPS,
121 .major = DRIVER_MAJOR,
122 .minor = DRIVER_MINOR,
125 static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
127 struct drm_crtc *crtc;
128 struct drm_crtc_state *new_crtc_state;
131 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
132 if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
135 if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
140 return drm_atomic_helper_check(dev, state);
143 static const struct drm_mode_config_funcs vkms_mode_funcs = {
144 .fb_create = drm_gem_fb_create,
145 .atomic_check = vkms_atomic_check,
146 .atomic_commit = drm_atomic_helper_commit,
149 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
150 .atomic_commit_tail = vkms_atomic_commit_tail,
153 static int vkms_modeset_init(struct vkms_device *vkmsdev)
155 struct drm_device *dev = &vkmsdev->drm;
158 ret = drmm_mode_config_init(dev);
162 dev->mode_config.funcs = &vkms_mode_funcs;
163 dev->mode_config.min_width = XRES_MIN;
164 dev->mode_config.min_height = YRES_MIN;
165 dev->mode_config.max_width = XRES_MAX;
166 dev->mode_config.max_height = YRES_MAX;
167 dev->mode_config.cursor_width = 512;
168 dev->mode_config.cursor_height = 512;
170 * FIXME: There's a confusion between bpp and depth between this and
171 * fbdev helpers. We have to go with 0, meaning "pick the default",
172 * which is XRGB8888 in all cases.
174 dev->mode_config.preferred_depth = 0;
175 dev->mode_config.helper_private = &vkms_mode_config_helpers;
177 return vkms_output_init(vkmsdev, 0);
180 static int vkms_create(struct vkms_config *config)
183 struct platform_device *pdev;
184 struct vkms_device *vkms_device;
186 pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
188 return PTR_ERR(pdev);
190 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
195 vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
196 struct vkms_device, drm);
197 if (IS_ERR(vkms_device)) {
198 ret = PTR_ERR(vkms_device);
201 vkms_device->platform = pdev;
202 vkms_device->config = config;
203 config->dev = vkms_device;
205 ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
209 DRM_ERROR("Could not initialize DMA support\n");
213 ret = drm_vblank_init(&vkms_device->drm, 1);
215 DRM_ERROR("Failed to vblank\n");
219 ret = vkms_modeset_init(vkms_device);
223 drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list,
224 ARRAY_SIZE(vkms_config_debugfs_list));
226 ret = drm_dev_register(&vkms_device->drm, 0);
230 drm_client_setup(&vkms_device->drm, NULL);
235 devres_release_group(&pdev->dev, NULL);
237 platform_device_unregister(pdev);
241 static int __init vkms_init(void)
244 struct vkms_config *config;
246 config = kmalloc(sizeof(*config), GFP_KERNEL);
250 default_config = config;
252 config->cursor = enable_cursor;
253 config->writeback = enable_writeback;
254 config->overlay = enable_overlay;
256 ret = vkms_create(config);
263 static void vkms_destroy(struct vkms_config *config)
265 struct platform_device *pdev;
268 DRM_INFO("vkms_device is NULL.\n");
272 pdev = config->dev->platform;
274 drm_dev_unregister(&config->dev->drm);
275 drm_atomic_helper_shutdown(&config->dev->drm);
276 devres_release_group(&pdev->dev, NULL);
277 platform_device_unregister(pdev);
282 static void __exit vkms_exit(void)
284 if (default_config->dev)
285 vkms_destroy(default_config);
287 kfree(default_config);
290 module_init(vkms_init);
291 module_exit(vkms_exit);
295 MODULE_DESCRIPTION(DRIVER_DESC);
296 MODULE_LICENSE("GPL");