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_drv.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_file.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_ioctl.h>
24 #include <drm/drm_managed.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_gem_shmem_helper.h>
27 #include <drm/drm_vblank.h>
31 #define DRIVER_NAME "vkms"
32 #define DRIVER_DESC "Virtual Kernel Mode Setting"
33 #define DRIVER_DATE "20180514"
34 #define DRIVER_MAJOR 1
35 #define DRIVER_MINOR 0
37 static struct vkms_config *default_config;
39 static bool enable_cursor = true;
40 module_param_named(enable_cursor, enable_cursor, bool, 0444);
41 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
43 static bool enable_writeback = true;
44 module_param_named(enable_writeback, enable_writeback, bool, 0444);
45 MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
47 static bool enable_overlay;
48 module_param_named(enable_overlay, enable_overlay, bool, 0444);
49 MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
51 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
53 static void vkms_release(struct drm_device *dev)
55 struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
57 destroy_workqueue(vkms->output.composer_workq);
60 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
62 struct drm_device *dev = old_state->dev;
63 struct drm_crtc *crtc;
64 struct drm_crtc_state *old_crtc_state;
67 drm_atomic_helper_commit_modeset_disables(dev, old_state);
69 drm_atomic_helper_commit_planes(dev, old_state, 0);
71 drm_atomic_helper_commit_modeset_enables(dev, old_state);
73 drm_atomic_helper_fake_vblank(old_state);
75 drm_atomic_helper_commit_hw_done(old_state);
77 drm_atomic_helper_wait_for_flip_done(dev, old_state);
79 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
80 struct vkms_crtc_state *vkms_state =
81 to_vkms_crtc_state(old_crtc_state);
83 flush_work(&vkms_state->composer_work);
86 drm_atomic_helper_cleanup_planes(dev, old_state);
89 static const struct drm_driver vkms_driver = {
90 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
91 .release = vkms_release,
92 .fops = &vkms_driver_fops,
93 DRM_GEM_SHMEM_DRIVER_OPS,
98 .major = DRIVER_MAJOR,
99 .minor = DRIVER_MINOR,
102 static const struct drm_mode_config_funcs vkms_mode_funcs = {
103 .fb_create = drm_gem_fb_create,
104 .atomic_check = drm_atomic_helper_check,
105 .atomic_commit = drm_atomic_helper_commit,
108 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
109 .atomic_commit_tail = vkms_atomic_commit_tail,
112 static int vkms_modeset_init(struct vkms_device *vkmsdev)
114 struct drm_device *dev = &vkmsdev->drm;
116 drm_mode_config_init(dev);
117 dev->mode_config.funcs = &vkms_mode_funcs;
118 dev->mode_config.min_width = XRES_MIN;
119 dev->mode_config.min_height = YRES_MIN;
120 dev->mode_config.max_width = XRES_MAX;
121 dev->mode_config.max_height = YRES_MAX;
122 dev->mode_config.cursor_width = 512;
123 dev->mode_config.cursor_height = 512;
124 /* FIXME: There's a confusion between bpp and depth between this and
125 * fbdev helpers. We have to go with 0, meaning "pick the default",
126 * which ix XRGB8888 in all cases. */
127 dev->mode_config.preferred_depth = 0;
128 dev->mode_config.helper_private = &vkms_mode_config_helpers;
130 return vkms_output_init(vkmsdev, 0);
133 static int vkms_create(struct vkms_config *config)
136 struct platform_device *pdev;
137 struct vkms_device *vkms_device;
139 pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
141 return PTR_ERR(pdev);
143 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
148 vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
149 struct vkms_device, drm);
150 if (IS_ERR(vkms_device)) {
151 ret = PTR_ERR(vkms_device);
154 vkms_device->platform = pdev;
155 vkms_device->config = config;
156 config->dev = vkms_device;
158 ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
162 DRM_ERROR("Could not initialize DMA support\n");
166 vkms_device->drm.irq_enabled = true;
168 ret = drm_vblank_init(&vkms_device->drm, 1);
170 DRM_ERROR("Failed to vblank\n");
174 ret = vkms_modeset_init(vkms_device);
178 ret = drm_dev_register(&vkms_device->drm, 0);
182 drm_fbdev_generic_setup(&vkms_device->drm, 0);
187 devres_release_group(&pdev->dev, NULL);
189 platform_device_unregister(pdev);
193 static int __init vkms_init(void)
195 struct vkms_config *config;
197 config = kmalloc(sizeof(*config), GFP_KERNEL);
201 default_config = config;
203 config->cursor = enable_cursor;
204 config->writeback = enable_writeback;
205 config->overlay = enable_overlay;
207 return vkms_create(config);
210 static void vkms_destroy(struct vkms_config *config)
212 struct platform_device *pdev;
215 DRM_INFO("vkms_device is NULL.\n");
219 pdev = config->dev->platform;
221 drm_dev_unregister(&config->dev->drm);
222 drm_atomic_helper_shutdown(&config->dev->drm);
223 devres_release_group(&pdev->dev, NULL);
224 platform_device_unregister(pdev);
229 static void __exit vkms_exit(void)
231 if (default_config->dev)
232 vkms_destroy(default_config);
234 kfree(default_config);
237 module_init(vkms_init);
238 module_exit(vkms_exit);
242 MODULE_DESCRIPTION(DRIVER_DESC);
243 MODULE_LICENSE("GPL");