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_vblank.h>
27 
28 #include "vkms_drv.h"
29 
30 #define DRIVER_NAME	"vkms"
31 #define DRIVER_DESC	"Virtual Kernel Mode Setting"
32 #define DRIVER_DATE	"20180514"
33 #define DRIVER_MAJOR	1
34 #define DRIVER_MINOR	0
35 
36 static struct vkms_device *vkms_device;
37 
38 bool enable_cursor = true;
39 module_param_named(enable_cursor, enable_cursor, bool, 0444);
40 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
41 
42 static const struct file_operations vkms_driver_fops = {
43 	.owner		= THIS_MODULE,
44 	.open		= drm_open,
45 	.mmap		= drm_gem_mmap,
46 	.unlocked_ioctl	= drm_ioctl,
47 	.compat_ioctl	= drm_compat_ioctl,
48 	.poll		= drm_poll,
49 	.read		= drm_read,
50 	.llseek		= no_llseek,
51 	.release	= drm_release,
52 };
53 
54 static const struct vm_operations_struct vkms_gem_vm_ops = {
55 	.fault = vkms_gem_fault,
56 	.open = drm_gem_vm_open,
57 	.close = drm_gem_vm_close,
58 };
59 
vkms_release(struct drm_device * dev)60 static void vkms_release(struct drm_device *dev)
61 {
62 	struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
63 
64 	destroy_workqueue(vkms->output.composer_workq);
65 }
66 
vkms_atomic_commit_tail(struct drm_atomic_state * old_state)67 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
68 {
69 	struct drm_device *dev = old_state->dev;
70 	struct drm_crtc *crtc;
71 	struct drm_crtc_state *old_crtc_state;
72 	int i;
73 
74 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
75 
76 	drm_atomic_helper_commit_planes(dev, old_state, 0);
77 
78 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
79 
80 	drm_atomic_helper_fake_vblank(old_state);
81 
82 	drm_atomic_helper_commit_hw_done(old_state);
83 
84 	drm_atomic_helper_wait_for_flip_done(dev, old_state);
85 
86 	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
87 		struct vkms_crtc_state *vkms_state =
88 			to_vkms_crtc_state(old_crtc_state);
89 
90 		flush_work(&vkms_state->composer_work);
91 	}
92 
93 	drm_atomic_helper_cleanup_planes(dev, old_state);
94 }
95 
96 static struct drm_driver vkms_driver = {
97 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
98 	.release		= vkms_release,
99 	.fops			= &vkms_driver_fops,
100 	.dumb_create		= vkms_dumb_create,
101 	.gem_vm_ops		= &vkms_gem_vm_ops,
102 	.gem_free_object_unlocked = vkms_gem_free_object,
103 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
104 	.gem_prime_import_sg_table = vkms_prime_import_sg_table,
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 
vkms_modeset_init(struct vkms_device * vkmsdev)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.cursor_width = 512;
134 	dev->mode_config.cursor_height = 512;
135 	dev->mode_config.preferred_depth = 24;
136 	dev->mode_config.helper_private = &vkms_mode_config_helpers;
137 
138 	return vkms_output_init(vkmsdev, 0);
139 }
140 
vkms_init(void)141 static int __init vkms_init(void)
142 {
143 	int ret;
144 	struct platform_device *pdev;
145 
146 	pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
147 	if (IS_ERR(pdev))
148 		return PTR_ERR(pdev);
149 
150 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
151 		ret = -ENOMEM;
152 		goto out_unregister;
153 	}
154 
155 	vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
156 					 struct vkms_device, drm);
157 	if (IS_ERR(vkms_device)) {
158 		ret = PTR_ERR(vkms_device);
159 		goto out_devres;
160 	}
161 	vkms_device->platform = pdev;
162 
163 	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
164 					   DMA_BIT_MASK(64));
165 
166 	if (ret) {
167 		DRM_ERROR("Could not initialize DMA support\n");
168 		goto out_devres;
169 	}
170 
171 	vkms_device->drm.irq_enabled = true;
172 
173 	ret = drm_vblank_init(&vkms_device->drm, 1);
174 	if (ret) {
175 		DRM_ERROR("Failed to vblank\n");
176 		goto out_devres;
177 	}
178 
179 	ret = vkms_modeset_init(vkms_device);
180 	if (ret)
181 		goto out_devres;
182 
183 	ret = drm_dev_register(&vkms_device->drm, 0);
184 	if (ret)
185 		goto out_devres;
186 
187 	return 0;
188 
189 out_devres:
190 	devres_release_group(&pdev->dev, NULL);
191 out_unregister:
192 	platform_device_unregister(pdev);
193 	return ret;
194 }
195 
vkms_exit(void)196 static void __exit vkms_exit(void)
197 {
198 	struct platform_device *pdev;
199 
200 	if (!vkms_device) {
201 		DRM_INFO("vkms_device is NULL.\n");
202 		return;
203 	}
204 
205 	pdev = vkms_device->platform;
206 
207 	drm_dev_unregister(&vkms_device->drm);
208 	drm_atomic_helper_shutdown(&vkms_device->drm);
209 	devres_release_group(&pdev->dev, NULL);
210 	platform_device_unregister(pdev);
211 }
212 
213 module_init(vkms_init);
214 module_exit(vkms_exit);
215 
216 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
217 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
218 MODULE_DESCRIPTION(DRIVER_DESC);
219 MODULE_LICENSE("GPL");
220