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 * or for running X (or similar) on headless machines and be able to still 8 * use the GPU. vkms aims to enable a virtual display without the need for 9 * a hardware display capability. 10 */ 11 12 #include <linux/module.h> 13 #include <drm/drm_gem.h> 14 #include <drm/drm_crtc_helper.h> 15 #include <drm/drm_atomic_helper.h> 16 #include <drm/drm_gem_framebuffer_helper.h> 17 #include <drm/drm_fb_helper.h> 18 #include "vkms_drv.h" 19 20 #define DRIVER_NAME "vkms" 21 #define DRIVER_DESC "Virtual Kernel Mode Setting" 22 #define DRIVER_DATE "20180514" 23 #define DRIVER_MAJOR 1 24 #define DRIVER_MINOR 0 25 26 static struct vkms_device *vkms_device; 27 28 bool enable_cursor; 29 module_param_named(enable_cursor, enable_cursor, bool, 0444); 30 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support"); 31 32 static const struct file_operations vkms_driver_fops = { 33 .owner = THIS_MODULE, 34 .open = drm_open, 35 .mmap = drm_gem_mmap, 36 .unlocked_ioctl = drm_ioctl, 37 .compat_ioctl = drm_compat_ioctl, 38 .poll = drm_poll, 39 .read = drm_read, 40 .llseek = no_llseek, 41 .release = drm_release, 42 }; 43 44 static const struct vm_operations_struct vkms_gem_vm_ops = { 45 .fault = vkms_gem_fault, 46 .open = drm_gem_vm_open, 47 .close = drm_gem_vm_close, 48 }; 49 50 static void vkms_release(struct drm_device *dev) 51 { 52 struct vkms_device *vkms = container_of(dev, struct vkms_device, drm); 53 54 platform_device_unregister(vkms->platform); 55 drm_atomic_helper_shutdown(&vkms->drm); 56 drm_mode_config_cleanup(&vkms->drm); 57 drm_dev_fini(&vkms->drm); 58 destroy_workqueue(vkms->output.crc_workq); 59 } 60 61 static struct drm_driver vkms_driver = { 62 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM, 63 .release = vkms_release, 64 .fops = &vkms_driver_fops, 65 .dumb_create = vkms_dumb_create, 66 .gem_vm_ops = &vkms_gem_vm_ops, 67 .gem_free_object_unlocked = vkms_gem_free_object, 68 .get_vblank_timestamp = vkms_get_vblank_timestamp, 69 70 .name = DRIVER_NAME, 71 .desc = DRIVER_DESC, 72 .date = DRIVER_DATE, 73 .major = DRIVER_MAJOR, 74 .minor = DRIVER_MINOR, 75 }; 76 77 static const struct drm_mode_config_funcs vkms_mode_funcs = { 78 .fb_create = drm_gem_fb_create, 79 .atomic_check = drm_atomic_helper_check, 80 .atomic_commit = drm_atomic_helper_commit, 81 }; 82 83 static int vkms_modeset_init(struct vkms_device *vkmsdev) 84 { 85 struct drm_device *dev = &vkmsdev->drm; 86 87 drm_mode_config_init(dev); 88 dev->mode_config.funcs = &vkms_mode_funcs; 89 dev->mode_config.min_width = XRES_MIN; 90 dev->mode_config.min_height = YRES_MIN; 91 dev->mode_config.max_width = XRES_MAX; 92 dev->mode_config.max_height = YRES_MAX; 93 94 return vkms_output_init(vkmsdev); 95 } 96 97 static int __init vkms_init(void) 98 { 99 int ret; 100 101 vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL); 102 if (!vkms_device) 103 return -ENOMEM; 104 105 vkms_device->platform = 106 platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); 107 if (IS_ERR(vkms_device->platform)) { 108 ret = PTR_ERR(vkms_device->platform); 109 goto out_free; 110 } 111 112 ret = drm_dev_init(&vkms_device->drm, &vkms_driver, 113 &vkms_device->platform->dev); 114 if (ret) 115 goto out_unregister; 116 117 vkms_device->drm.irq_enabled = true; 118 119 ret = drm_vblank_init(&vkms_device->drm, 1); 120 if (ret) { 121 DRM_ERROR("Failed to vblank\n"); 122 goto out_fini; 123 } 124 125 ret = vkms_modeset_init(vkms_device); 126 if (ret) 127 goto out_fini; 128 129 ret = drm_dev_register(&vkms_device->drm, 0); 130 if (ret) 131 goto out_fini; 132 133 return 0; 134 135 out_fini: 136 drm_dev_fini(&vkms_device->drm); 137 138 out_unregister: 139 platform_device_unregister(vkms_device->platform); 140 141 out_free: 142 kfree(vkms_device); 143 return ret; 144 } 145 146 static void __exit vkms_exit(void) 147 { 148 if (!vkms_device) { 149 DRM_INFO("vkms_device is NULL.\n"); 150 return; 151 } 152 153 drm_dev_unregister(&vkms_device->drm); 154 drm_dev_put(&vkms_device->drm); 155 156 kfree(vkms_device); 157 } 158 159 module_init(vkms_init); 160 module_exit(vkms_exit); 161 162 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>"); 163 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>"); 164 MODULE_DESCRIPTION(DRIVER_DESC); 165 MODULE_LICENSE("GPL"); 166