b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 <linux/platform_device.h> |
| 14 | |
| 15 | #include <drm/drm_atomic.h> |
| 16 | #include <drm/drm_atomic_helper.h> |
| 17 | #include <drm/drm_drv.h> |
| 18 | #include <drm/drm_fb_helper.h> |
| 19 | #include <drm/drm_file.h> |
| 20 | #include <drm/drm_gem.h> |
| 21 | #include <drm/drm_gem_framebuffer_helper.h> |
| 22 | #include <drm/drm_ioctl.h> |
| 23 | #include <drm/drm_probe_helper.h> |
| 24 | #include <drm/drm_vblank.h> |
| 25 | |
| 26 | #include "vkms_drv.h" |
| 27 | |
| 28 | #define DRIVER_NAME "vkms" |
| 29 | #define DRIVER_DESC "Virtual Kernel Mode Setting" |
| 30 | #define DRIVER_DATE "20180514" |
| 31 | #define DRIVER_MAJOR 1 |
| 32 | #define DRIVER_MINOR 0 |
| 33 | |
| 34 | static struct vkms_device *vkms_device; |
| 35 | |
| 36 | bool enable_cursor; |
| 37 | module_param_named(enable_cursor, enable_cursor, bool, 0444); |
| 38 | MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support"); |
| 39 | |
| 40 | static const struct file_operations vkms_driver_fops = { |
| 41 | .owner = THIS_MODULE, |
| 42 | .open = drm_open, |
| 43 | .mmap = drm_gem_mmap, |
| 44 | .unlocked_ioctl = drm_ioctl, |
| 45 | .compat_ioctl = drm_compat_ioctl, |
| 46 | .poll = drm_poll, |
| 47 | .read = drm_read, |
| 48 | .llseek = no_llseek, |
| 49 | .release = drm_release, |
| 50 | }; |
| 51 | |
| 52 | static const struct vm_operations_struct vkms_gem_vm_ops = { |
| 53 | .fault = vkms_gem_fault, |
| 54 | .open = drm_gem_vm_open, |
| 55 | .close = drm_gem_vm_close, |
| 56 | }; |
| 57 | |
| 58 | static void vkms_release(struct drm_device *dev) |
| 59 | { |
| 60 | struct vkms_device *vkms = container_of(dev, struct vkms_device, drm); |
| 61 | |
| 62 | platform_device_unregister(vkms->platform); |
| 63 | drm_mode_config_cleanup(&vkms->drm); |
| 64 | drm_dev_fini(&vkms->drm); |
| 65 | destroy_workqueue(vkms->output.composer_workq); |
| 66 | } |
| 67 | |
| 68 | static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state) |
| 69 | { |
| 70 | struct drm_device *dev = old_state->dev; |
| 71 | struct drm_crtc *crtc; |
| 72 | struct drm_crtc_state *old_crtc_state; |
| 73 | int i; |
| 74 | |
| 75 | drm_atomic_helper_commit_modeset_disables(dev, old_state); |
| 76 | |
| 77 | drm_atomic_helper_commit_planes(dev, old_state, 0); |
| 78 | |
| 79 | drm_atomic_helper_commit_modeset_enables(dev, old_state); |
| 80 | |
| 81 | drm_atomic_helper_fake_vblank(old_state); |
| 82 | |
| 83 | drm_atomic_helper_commit_hw_done(old_state); |
| 84 | |
| 85 | drm_atomic_helper_wait_for_vblanks(dev, old_state); |
| 86 | |
| 87 | for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
| 88 | struct vkms_crtc_state *vkms_state = |
| 89 | to_vkms_crtc_state(old_crtc_state); |
| 90 | |
| 91 | flush_work(&vkms_state->composer_work); |
| 92 | } |
| 93 | |
| 94 | drm_atomic_helper_cleanup_planes(dev, old_state); |
| 95 | } |
| 96 | |
| 97 | static struct drm_driver vkms_driver = { |
| 98 | .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM, |
| 99 | .release = vkms_release, |
| 100 | .fops = &vkms_driver_fops, |
| 101 | .dumb_create = vkms_dumb_create, |
| 102 | .gem_vm_ops = &vkms_gem_vm_ops, |
| 103 | .gem_free_object_unlocked = vkms_gem_free_object, |
| 104 | .get_vblank_timestamp = vkms_get_vblank_timestamp, |
| 105 | |
| 106 | .name = DRIVER_NAME, |
| 107 | .desc = DRIVER_DESC, |
| 108 | .date = DRIVER_DATE, |
| 109 | .major = DRIVER_MAJOR, |
| 110 | .minor = DRIVER_MINOR, |
| 111 | }; |
| 112 | |
| 113 | static const struct drm_mode_config_funcs vkms_mode_funcs = { |
| 114 | .fb_create = drm_gem_fb_create, |
| 115 | .atomic_check = drm_atomic_helper_check, |
| 116 | .atomic_commit = drm_atomic_helper_commit, |
| 117 | }; |
| 118 | |
| 119 | static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = { |
| 120 | .atomic_commit_tail = vkms_atomic_commit_tail, |
| 121 | }; |
| 122 | |
| 123 | static int vkms_modeset_init(struct vkms_device *vkmsdev) |
| 124 | { |
| 125 | struct drm_device *dev = &vkmsdev->drm; |
| 126 | |
| 127 | drm_mode_config_init(dev); |
| 128 | dev->mode_config.funcs = &vkms_mode_funcs; |
| 129 | dev->mode_config.min_width = XRES_MIN; |
| 130 | dev->mode_config.min_height = YRES_MIN; |
| 131 | dev->mode_config.max_width = XRES_MAX; |
| 132 | dev->mode_config.max_height = YRES_MAX; |
| 133 | dev->mode_config.preferred_depth = 24; |
| 134 | dev->mode_config.helper_private = &vkms_mode_config_helpers; |
| 135 | |
| 136 | return vkms_output_init(vkmsdev, 0); |
| 137 | } |
| 138 | |
| 139 | static int __init vkms_init(void) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL); |
| 144 | if (!vkms_device) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | vkms_device->platform = |
| 148 | platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
| 149 | if (IS_ERR(vkms_device->platform)) { |
| 150 | ret = PTR_ERR(vkms_device->platform); |
| 151 | goto out_free; |
| 152 | } |
| 153 | |
| 154 | ret = drm_dev_init(&vkms_device->drm, &vkms_driver, |
| 155 | &vkms_device->platform->dev); |
| 156 | if (ret) |
| 157 | goto out_unregister; |
| 158 | |
| 159 | vkms_device->drm.irq_enabled = true; |
| 160 | |
| 161 | ret = drm_vblank_init(&vkms_device->drm, 1); |
| 162 | if (ret) { |
| 163 | DRM_ERROR("Failed to vblank\n"); |
| 164 | goto out_fini; |
| 165 | } |
| 166 | |
| 167 | ret = vkms_modeset_init(vkms_device); |
| 168 | if (ret) |
| 169 | goto out_fini; |
| 170 | |
| 171 | ret = drm_dev_register(&vkms_device->drm, 0); |
| 172 | if (ret) |
| 173 | goto out_fini; |
| 174 | |
| 175 | return 0; |
| 176 | |
| 177 | out_fini: |
| 178 | drm_dev_fini(&vkms_device->drm); |
| 179 | |
| 180 | out_unregister: |
| 181 | platform_device_unregister(vkms_device->platform); |
| 182 | |
| 183 | out_free: |
| 184 | kfree(vkms_device); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | static void __exit vkms_exit(void) |
| 189 | { |
| 190 | if (!vkms_device) { |
| 191 | DRM_INFO("vkms_device is NULL.\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | drm_dev_unregister(&vkms_device->drm); |
| 196 | drm_atomic_helper_shutdown(&vkms_device->drm); |
| 197 | drm_dev_put(&vkms_device->drm); |
| 198 | |
| 199 | kfree(vkms_device); |
| 200 | } |
| 201 | |
| 202 | module_init(vkms_init); |
| 203 | module_exit(vkms_exit); |
| 204 | |
| 205 | MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>"); |
| 206 | MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>"); |
| 207 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 208 | MODULE_LICENSE("GPL"); |