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