Merge branch '00.00-inst' of git://github.com/skeggsb/linux into drm-fixes
[linux-2.6-microblaze.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  * 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.
10  */
11
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15
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>
28
29 #include "vkms_drv.h"
30
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
36
37 static struct vkms_config *default_config;
38
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");
42
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");
46
47 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
48
49 static void vkms_release(struct drm_device *dev)
50 {
51         struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
52
53         destroy_workqueue(vkms->output.composer_workq);
54 }
55
56 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
57 {
58         struct drm_device *dev = old_state->dev;
59         struct drm_crtc *crtc;
60         struct drm_crtc_state *old_crtc_state;
61         int i;
62
63         drm_atomic_helper_commit_modeset_disables(dev, old_state);
64
65         drm_atomic_helper_commit_planes(dev, old_state, 0);
66
67         drm_atomic_helper_commit_modeset_enables(dev, old_state);
68
69         drm_atomic_helper_fake_vblank(old_state);
70
71         drm_atomic_helper_commit_hw_done(old_state);
72
73         drm_atomic_helper_wait_for_flip_done(dev, old_state);
74
75         for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
76                 struct vkms_crtc_state *vkms_state =
77                         to_vkms_crtc_state(old_crtc_state);
78
79                 flush_work(&vkms_state->composer_work);
80         }
81
82         drm_atomic_helper_cleanup_planes(dev, old_state);
83 }
84
85 static const struct drm_driver vkms_driver = {
86         .driver_features        = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
87         .release                = vkms_release,
88         .fops                   = &vkms_driver_fops,
89         DRM_GEM_SHMEM_DRIVER_OPS,
90
91         .name                   = DRIVER_NAME,
92         .desc                   = DRIVER_DESC,
93         .date                   = DRIVER_DATE,
94         .major                  = DRIVER_MAJOR,
95         .minor                  = DRIVER_MINOR,
96 };
97
98 static const struct drm_mode_config_funcs vkms_mode_funcs = {
99         .fb_create = drm_gem_fb_create,
100         .atomic_check = drm_atomic_helper_check,
101         .atomic_commit = drm_atomic_helper_commit,
102 };
103
104 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
105         .atomic_commit_tail = vkms_atomic_commit_tail,
106 };
107
108 static int vkms_modeset_init(struct vkms_device *vkmsdev)
109 {
110         struct drm_device *dev = &vkmsdev->drm;
111
112         drm_mode_config_init(dev);
113         dev->mode_config.funcs = &vkms_mode_funcs;
114         dev->mode_config.min_width = XRES_MIN;
115         dev->mode_config.min_height = YRES_MIN;
116         dev->mode_config.max_width = XRES_MAX;
117         dev->mode_config.max_height = YRES_MAX;
118         dev->mode_config.cursor_width = 512;
119         dev->mode_config.cursor_height = 512;
120         /* FIXME: There's a confusion between bpp and depth between this and
121          * fbdev helpers. We have to go with 0, meaning "pick the default",
122          * which ix XRGB8888 in all cases. */
123         dev->mode_config.preferred_depth = 0;
124         dev->mode_config.helper_private = &vkms_mode_config_helpers;
125
126         return vkms_output_init(vkmsdev, 0);
127 }
128
129 static int vkms_create(struct vkms_config *config)
130 {
131         int ret;
132         struct platform_device *pdev;
133         struct vkms_device *vkms_device;
134
135         pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
136         if (IS_ERR(pdev))
137                 return PTR_ERR(pdev);
138
139         if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
140                 ret = -ENOMEM;
141                 goto out_unregister;
142         }
143
144         vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
145                                          struct vkms_device, drm);
146         if (IS_ERR(vkms_device)) {
147                 ret = PTR_ERR(vkms_device);
148                 goto out_devres;
149         }
150         vkms_device->platform = pdev;
151         vkms_device->config = config;
152         config->dev = vkms_device;
153
154         ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
155                                            DMA_BIT_MASK(64));
156
157         if (ret) {
158                 DRM_ERROR("Could not initialize DMA support\n");
159                 goto out_devres;
160         }
161
162         vkms_device->drm.irq_enabled = true;
163
164         ret = drm_vblank_init(&vkms_device->drm, 1);
165         if (ret) {
166                 DRM_ERROR("Failed to vblank\n");
167                 goto out_devres;
168         }
169
170         ret = vkms_modeset_init(vkms_device);
171         if (ret)
172                 goto out_devres;
173
174         ret = drm_dev_register(&vkms_device->drm, 0);
175         if (ret)
176                 goto out_devres;
177
178         drm_fbdev_generic_setup(&vkms_device->drm, 0);
179
180         return 0;
181
182 out_devres:
183         devres_release_group(&pdev->dev, NULL);
184 out_unregister:
185         platform_device_unregister(pdev);
186         return ret;
187 }
188
189 static int __init vkms_init(void)
190 {
191         struct vkms_config *config;
192
193         config = kmalloc(sizeof(*config), GFP_KERNEL);
194         if (!config)
195                 return -ENOMEM;
196
197         default_config = config;
198
199         config->cursor = enable_cursor;
200         config->writeback = enable_writeback;
201
202         return vkms_create(config);
203 }
204
205 static void vkms_destroy(struct vkms_config *config)
206 {
207         struct platform_device *pdev;
208
209         if (!config->dev) {
210                 DRM_INFO("vkms_device is NULL.\n");
211                 return;
212         }
213
214         pdev = config->dev->platform;
215
216         drm_dev_unregister(&config->dev->drm);
217         drm_atomic_helper_shutdown(&config->dev->drm);
218         devres_release_group(&pdev->dev, NULL);
219         platform_device_unregister(pdev);
220
221         config->dev = NULL;
222 }
223
224 static void __exit vkms_exit(void)
225 {
226         if (default_config->dev)
227                 vkms_destroy(default_config);
228
229         kfree(default_config);
230 }
231
232 module_init(vkms_init);
233 module_exit(vkms_exit);
234
235 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
236 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
237 MODULE_DESCRIPTION(DRIVER_DESC);
238 MODULE_LICENSE("GPL");