xref: /linux/drivers/gpu/drm/vkms/vkms_drv.c (revision 75b1a8f9d62e50f05d0e4e9f3c8bcde32527ffc1)
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_device *vkms_device;
38 
39 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 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
44 
45 static void vkms_release(struct drm_device *dev)
46 {
47 	struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
48 
49 	destroy_workqueue(vkms->output.composer_workq);
50 }
51 
52 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
53 {
54 	struct drm_device *dev = old_state->dev;
55 	struct drm_crtc *crtc;
56 	struct drm_crtc_state *old_crtc_state;
57 	int i;
58 
59 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
60 
61 	drm_atomic_helper_commit_planes(dev, old_state, 0);
62 
63 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
64 
65 	drm_atomic_helper_fake_vblank(old_state);
66 
67 	drm_atomic_helper_commit_hw_done(old_state);
68 
69 	drm_atomic_helper_wait_for_flip_done(dev, old_state);
70 
71 	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
72 		struct vkms_crtc_state *vkms_state =
73 			to_vkms_crtc_state(old_crtc_state);
74 
75 		flush_work(&vkms_state->composer_work);
76 	}
77 
78 	drm_atomic_helper_cleanup_planes(dev, old_state);
79 }
80 
81 static const struct drm_driver vkms_driver = {
82 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
83 	.release		= vkms_release,
84 	.fops			= &vkms_driver_fops,
85 	DRM_GEM_SHMEM_DRIVER_OPS,
86 
87 	.name			= DRIVER_NAME,
88 	.desc			= DRIVER_DESC,
89 	.date			= DRIVER_DATE,
90 	.major			= DRIVER_MAJOR,
91 	.minor			= DRIVER_MINOR,
92 };
93 
94 static const struct drm_mode_config_funcs vkms_mode_funcs = {
95 	.fb_create = drm_gem_fb_create,
96 	.atomic_check = drm_atomic_helper_check,
97 	.atomic_commit = drm_atomic_helper_commit,
98 };
99 
100 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
101 	.atomic_commit_tail = vkms_atomic_commit_tail,
102 };
103 
104 static int vkms_modeset_init(struct vkms_device *vkmsdev)
105 {
106 	struct drm_device *dev = &vkmsdev->drm;
107 
108 	drm_mode_config_init(dev);
109 	dev->mode_config.funcs = &vkms_mode_funcs;
110 	dev->mode_config.min_width = XRES_MIN;
111 	dev->mode_config.min_height = YRES_MIN;
112 	dev->mode_config.max_width = XRES_MAX;
113 	dev->mode_config.max_height = YRES_MAX;
114 	dev->mode_config.cursor_width = 512;
115 	dev->mode_config.cursor_height = 512;
116 	dev->mode_config.preferred_depth = 32;
117 	dev->mode_config.helper_private = &vkms_mode_config_helpers;
118 
119 	return vkms_output_init(vkmsdev, 0);
120 }
121 
122 static int __init vkms_init(void)
123 {
124 	int ret;
125 	struct platform_device *pdev;
126 
127 	pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
128 	if (IS_ERR(pdev))
129 		return PTR_ERR(pdev);
130 
131 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
132 		ret = -ENOMEM;
133 		goto out_unregister;
134 	}
135 
136 	vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
137 					 struct vkms_device, drm);
138 	if (IS_ERR(vkms_device)) {
139 		ret = PTR_ERR(vkms_device);
140 		goto out_devres;
141 	}
142 	vkms_device->platform = pdev;
143 
144 	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
145 					   DMA_BIT_MASK(64));
146 
147 	if (ret) {
148 		DRM_ERROR("Could not initialize DMA support\n");
149 		goto out_devres;
150 	}
151 
152 	vkms_device->drm.irq_enabled = true;
153 
154 	ret = drm_vblank_init(&vkms_device->drm, 1);
155 	if (ret) {
156 		DRM_ERROR("Failed to vblank\n");
157 		goto out_devres;
158 	}
159 
160 	ret = vkms_modeset_init(vkms_device);
161 	if (ret)
162 		goto out_devres;
163 
164 	ret = drm_dev_register(&vkms_device->drm, 0);
165 	if (ret)
166 		goto out_devres;
167 
168 	drm_fbdev_generic_setup(&vkms_device->drm, 0);
169 
170 	return 0;
171 
172 out_devres:
173 	devres_release_group(&pdev->dev, NULL);
174 out_unregister:
175 	platform_device_unregister(pdev);
176 	return ret;
177 }
178 
179 static void __exit vkms_exit(void)
180 {
181 	struct platform_device *pdev;
182 
183 	if (!vkms_device) {
184 		DRM_INFO("vkms_device is NULL.\n");
185 		return;
186 	}
187 
188 	pdev = vkms_device->platform;
189 
190 	drm_dev_unregister(&vkms_device->drm);
191 	drm_atomic_helper_shutdown(&vkms_device->drm);
192 	devres_release_group(&pdev->dev, NULL);
193 	platform_device_unregister(pdev);
194 }
195 
196 module_init(vkms_init);
197 module_exit(vkms_exit);
198 
199 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
200 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
201 MODULE_DESCRIPTION(DRIVER_DESC);
202 MODULE_LICENSE("GPL");
203