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