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