• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <drm/drm_print.h>
32 #include <drm/drm_debugfs.h>
33 
34 #define DRIVER_NAME	"vkms"
35 #define DRIVER_DESC	"Virtual Kernel Mode Setting"
36 #define DRIVER_DATE	"20180514"
37 #define DRIVER_MAJOR	1
38 #define DRIVER_MINOR	0
39 
40 static struct vkms_config *default_config;
41 
42 static bool enable_cursor = true;
43 module_param_named(enable_cursor, enable_cursor, bool, 0444);
44 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
45 
46 static bool enable_writeback = true;
47 module_param_named(enable_writeback, enable_writeback, bool, 0444);
48 MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
49 
50 static bool enable_overlay;
51 module_param_named(enable_overlay, enable_overlay, bool, 0444);
52 MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
53 
54 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
55 
vkms_release(struct drm_device * dev)56 static void vkms_release(struct drm_device *dev)
57 {
58 	struct vkms_device *vkms = drm_device_to_vkms_device(dev);
59 
60 	if (vkms->output.composer_workq)
61 		destroy_workqueue(vkms->output.composer_workq);
62 }
63 
vkms_atomic_commit_tail(struct drm_atomic_state * old_state)64 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
65 {
66 	struct drm_device *dev = old_state->dev;
67 	struct drm_crtc *crtc;
68 	struct drm_crtc_state *old_crtc_state;
69 	int i;
70 
71 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
72 
73 	drm_atomic_helper_commit_planes(dev, old_state, 0);
74 
75 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
76 
77 	drm_atomic_helper_fake_vblank(old_state);
78 
79 	drm_atomic_helper_commit_hw_done(old_state);
80 
81 	drm_atomic_helper_wait_for_flip_done(dev, old_state);
82 
83 	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
84 		struct vkms_crtc_state *vkms_state =
85 			to_vkms_crtc_state(old_crtc_state);
86 
87 		flush_work(&vkms_state->composer_work);
88 	}
89 
90 	drm_atomic_helper_cleanup_planes(dev, old_state);
91 }
92 
vkms_config_show(struct seq_file * m,void * data)93 static int vkms_config_show(struct seq_file *m, void *data)
94 {
95 	struct drm_info_node *node = (struct drm_info_node *)m->private;
96 	struct drm_device *dev = node->minor->dev;
97 	struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
98 
99 	seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback);
100 	seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor);
101 	seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay);
102 
103 	return 0;
104 }
105 
106 static const struct drm_info_list vkms_config_debugfs_list[] = {
107 	{ "vkms_config", vkms_config_show, 0 },
108 };
109 
vkms_config_debugfs_init(struct drm_minor * minor)110 static void vkms_config_debugfs_init(struct drm_minor *minor)
111 {
112 	drm_debugfs_create_files(vkms_config_debugfs_list, ARRAY_SIZE(vkms_config_debugfs_list),
113 				 minor->debugfs_root, minor);
114 }
115 
116 static const struct drm_driver vkms_driver = {
117 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
118 	.release		= vkms_release,
119 	.fops			= &vkms_driver_fops,
120 	DRM_GEM_SHMEM_DRIVER_OPS,
121 
122 	.debugfs_init           = vkms_config_debugfs_init,
123 
124 	.name			= DRIVER_NAME,
125 	.desc			= DRIVER_DESC,
126 	.date			= DRIVER_DATE,
127 	.major			= DRIVER_MAJOR,
128 	.minor			= DRIVER_MINOR,
129 };
130 
131 static const struct drm_mode_config_funcs vkms_mode_funcs = {
132 	.fb_create = drm_gem_fb_create,
133 	.atomic_check = drm_atomic_helper_check,
134 	.atomic_commit = drm_atomic_helper_commit,
135 };
136 
137 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
138 	.atomic_commit_tail = vkms_atomic_commit_tail,
139 };
140 
vkms_modeset_init(struct vkms_device * vkmsdev)141 static int vkms_modeset_init(struct vkms_device *vkmsdev)
142 {
143 	struct drm_device *dev = &vkmsdev->drm;
144 
145 	drm_mode_config_init(dev);
146 	dev->mode_config.funcs = &vkms_mode_funcs;
147 	dev->mode_config.min_width = XRES_MIN;
148 	dev->mode_config.min_height = YRES_MIN;
149 	dev->mode_config.max_width = XRES_MAX;
150 	dev->mode_config.max_height = YRES_MAX;
151 	dev->mode_config.cursor_width = 512;
152 	dev->mode_config.cursor_height = 512;
153 	/* FIXME: There's a confusion between bpp and depth between this and
154 	 * fbdev helpers. We have to go with 0, meaning "pick the default",
155 	 * which ix XRGB8888 in all cases. */
156 	dev->mode_config.preferred_depth = 0;
157 	dev->mode_config.helper_private = &vkms_mode_config_helpers;
158 
159 	return vkms_output_init(vkmsdev, 0);
160 }
161 
vkms_create(struct vkms_config * config)162 static int vkms_create(struct vkms_config *config)
163 {
164 	int ret;
165 	struct platform_device *pdev;
166 	struct vkms_device *vkms_device;
167 
168 	pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
169 	if (IS_ERR(pdev))
170 		return PTR_ERR(pdev);
171 
172 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
173 		ret = -ENOMEM;
174 		goto out_unregister;
175 	}
176 
177 	vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
178 					 struct vkms_device, drm);
179 	if (IS_ERR(vkms_device)) {
180 		ret = PTR_ERR(vkms_device);
181 		goto out_devres;
182 	}
183 	vkms_device->platform = pdev;
184 	vkms_device->config = config;
185 	config->dev = vkms_device;
186 
187 	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
188 					   DMA_BIT_MASK(64));
189 
190 	if (ret) {
191 		DRM_ERROR("Could not initialize DMA support\n");
192 		goto out_devres;
193 	}
194 
195 	ret = drm_vblank_init(&vkms_device->drm, 1);
196 	if (ret) {
197 		DRM_ERROR("Failed to vblank\n");
198 		goto out_devres;
199 	}
200 
201 	ret = vkms_modeset_init(vkms_device);
202 	if (ret)
203 		goto out_devres;
204 
205 	ret = drm_dev_register(&vkms_device->drm, 0);
206 	if (ret)
207 		goto out_devres;
208 
209 	drm_fbdev_generic_setup(&vkms_device->drm, 0);
210 
211 	return 0;
212 
213 out_devres:
214 	devres_release_group(&pdev->dev, NULL);
215 out_unregister:
216 	platform_device_unregister(pdev);
217 	return ret;
218 }
219 
vkms_init(void)220 static int __init vkms_init(void)
221 {
222 	int ret;
223 	struct vkms_config *config;
224 
225 	config = kmalloc(sizeof(*config), GFP_KERNEL);
226 	if (!config)
227 		return -ENOMEM;
228 
229 	default_config = config;
230 
231 	config->cursor = enable_cursor;
232 	config->writeback = enable_writeback;
233 	config->overlay = enable_overlay;
234 
235 	ret = vkms_create(config);
236 	if (ret)
237 		kfree(config);
238 
239 	return ret;
240 }
241 
vkms_destroy(struct vkms_config * config)242 static void vkms_destroy(struct vkms_config *config)
243 {
244 	struct platform_device *pdev;
245 
246 	if (!config->dev) {
247 		DRM_INFO("vkms_device is NULL.\n");
248 		return;
249 	}
250 
251 	pdev = config->dev->platform;
252 
253 	drm_dev_unregister(&config->dev->drm);
254 	drm_atomic_helper_shutdown(&config->dev->drm);
255 	devres_release_group(&pdev->dev, NULL);
256 	platform_device_unregister(pdev);
257 
258 	config->dev = NULL;
259 }
260 
vkms_exit(void)261 static void __exit vkms_exit(void)
262 {
263 	if (default_config->dev)
264 		vkms_destroy(default_config);
265 
266 	kfree(default_config);
267 }
268 
269 module_init(vkms_init);
270 module_exit(vkms_exit);
271 
272 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
273 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
274 MODULE_DESCRIPTION(DRIVER_DESC);
275 MODULE_LICENSE("GPL");
276