xref: /linux/drivers/gpu/drm/vkms/vkms_drv.c (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
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/device/faux.h>
14 #include <linux/dma-mapping.h>
15 
16 #include <drm/clients/drm_client_setup.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fbdev_shmem.h>
22 #include <drm/drm_file.h>
23 #include <drm/drm_gem_framebuffer_helper.h>
24 #include <drm/drm_ioctl.h>
25 #include <drm/drm_managed.h>
26 #include <drm/drm_print.h>
27 #include <drm/drm_probe_helper.h>
28 #include <drm/drm_gem_shmem_helper.h>
29 #include <drm/drm_vblank.h>
30 
31 #include "vkms_config.h"
32 #include "vkms_configfs.h"
33 #include "vkms_drv.h"
34 
35 #define DRIVER_NAME	"vkms"
36 #define DRIVER_DESC	"Virtual Kernel Mode Setting"
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 static bool enable_plane_pipeline;
55 module_param_named(enable_plane_pipeline, enable_plane_pipeline, bool, 0444);
56 MODULE_PARM_DESC(enable_plane_pipeline, "Enable/Disable plane pipeline support");
57 
58 static bool create_default_dev = true;
59 module_param_named(create_default_dev, create_default_dev, bool, 0444);
60 MODULE_PARM_DESC(create_default_dev, "Create or not the default VKMS device");
61 
62 DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
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 = to_vkms_crtc_state(old_crtc_state);
85 
86 		flush_work(&vkms_state->composer_work);
87 	}
88 
89 	drm_atomic_helper_cleanup_planes(dev, old_state);
90 }
91 
92 static const struct drm_driver vkms_driver = {
93 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
94 	.fops			= &vkms_driver_fops,
95 	DRM_GEM_SHMEM_DRIVER_OPS,
96 	DRM_FBDEV_SHMEM_DRIVER_OPS,
97 
98 	.name			= DRIVER_NAME,
99 	.desc			= DRIVER_DESC,
100 	.major			= DRIVER_MAJOR,
101 	.minor			= DRIVER_MINOR,
102 };
103 
vkms_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)104 static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
105 {
106 	struct drm_crtc *crtc;
107 	struct drm_crtc_state *new_crtc_state;
108 	int i;
109 
110 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
111 		if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
112 			continue;
113 
114 		if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
115 		    > VKMS_LUT_SIZE)
116 			return -EINVAL;
117 	}
118 
119 	return drm_atomic_helper_check(dev, state);
120 }
121 
122 static const struct drm_mode_config_funcs vkms_mode_funcs = {
123 	.fb_create = drm_gem_fb_create,
124 	.atomic_check = vkms_atomic_check,
125 	.atomic_commit = drm_atomic_helper_commit,
126 };
127 
128 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
129 	.atomic_commit_tail = vkms_atomic_commit_tail,
130 };
131 
vkms_modeset_init(struct vkms_device * vkmsdev)132 static int vkms_modeset_init(struct vkms_device *vkmsdev)
133 {
134 	struct drm_device *dev = &vkmsdev->drm;
135 	int ret;
136 
137 	ret = drmm_mode_config_init(dev);
138 	if (ret)
139 		return ret;
140 
141 	dev->mode_config.funcs = &vkms_mode_funcs;
142 	dev->mode_config.min_width = XRES_MIN;
143 	dev->mode_config.min_height = YRES_MIN;
144 	dev->mode_config.max_width = XRES_MAX;
145 	dev->mode_config.max_height = YRES_MAX;
146 	dev->mode_config.cursor_width = 512;
147 	dev->mode_config.cursor_height = 512;
148 	/*
149 	 * FIXME: There's a confusion between bpp and depth between this and
150 	 * fbdev helpers. We have to go with 0, meaning "pick the default",
151 	 * which is XRGB8888 in all cases.
152 	 */
153 	dev->mode_config.preferred_depth = 0;
154 	dev->mode_config.helper_private = &vkms_mode_config_helpers;
155 
156 	return vkms_output_init(vkmsdev);
157 }
158 
vkms_create(struct vkms_config * config)159 int vkms_create(struct vkms_config *config)
160 {
161 	int ret;
162 	struct faux_device *fdev;
163 	struct vkms_device *vkms_device;
164 	const char *dev_name;
165 
166 	dev_name = vkms_config_get_device_name(config);
167 	fdev = faux_device_create(dev_name, NULL, NULL);
168 	if (!fdev)
169 		return -ENODEV;
170 
171 	if (!devres_open_group(&fdev->dev, NULL, GFP_KERNEL)) {
172 		ret = -ENOMEM;
173 		goto out_unregister;
174 	}
175 
176 	vkms_device = devm_drm_dev_alloc(&fdev->dev, &vkms_driver,
177 					 struct vkms_device, drm);
178 	if (IS_ERR(vkms_device)) {
179 		ret = PTR_ERR(vkms_device);
180 		goto out_devres;
181 	}
182 	vkms_device->faux_dev = fdev;
183 	vkms_device->config = config;
184 	config->dev = vkms_device;
185 
186 	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
187 					   DMA_BIT_MASK(64));
188 
189 	if (ret) {
190 		DRM_ERROR("Could not initialize DMA support\n");
191 		goto out_devres;
192 	}
193 
194 	ret = drm_vblank_init(&vkms_device->drm,
195 			      vkms_config_get_num_crtcs(config));
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 	vkms_config_register_debugfs(vkms_device);
206 
207 	ret = drm_dev_register(&vkms_device->drm, 0);
208 	if (ret)
209 		goto out_devres;
210 
211 	drm_client_setup(&vkms_device->drm, NULL);
212 
213 	return 0;
214 
215 out_devres:
216 	devres_release_group(&fdev->dev, NULL);
217 out_unregister:
218 	faux_device_destroy(fdev);
219 	return ret;
220 }
221 
vkms_init(void)222 static int __init vkms_init(void)
223 {
224 	int ret;
225 	struct vkms_config *config;
226 
227 	ret = vkms_configfs_register();
228 	if (ret)
229 		return ret;
230 
231 	if (!create_default_dev)
232 		return 0;
233 
234 	config = vkms_config_default_create(enable_cursor, enable_writeback,
235 					    enable_overlay, enable_plane_pipeline);
236 	if (IS_ERR(config))
237 		return PTR_ERR(config);
238 
239 	ret = vkms_create(config);
240 	if (ret) {
241 		vkms_config_destroy(config);
242 		return ret;
243 	}
244 
245 	default_config = config;
246 
247 	return 0;
248 }
249 
vkms_destroy(struct vkms_config * config)250 void vkms_destroy(struct vkms_config *config)
251 {
252 	struct faux_device *fdev;
253 
254 	if (!config->dev) {
255 		DRM_INFO("vkms_device is NULL.\n");
256 		return;
257 	}
258 
259 	fdev = config->dev->faux_dev;
260 
261 	drm_colorop_pipeline_destroy(&config->dev->drm);
262 	drm_dev_unregister(&config->dev->drm);
263 	drm_atomic_helper_shutdown(&config->dev->drm);
264 	devres_release_group(&fdev->dev, NULL);
265 	faux_device_destroy(fdev);
266 
267 	config->dev = NULL;
268 }
269 
vkms_exit(void)270 static void __exit vkms_exit(void)
271 {
272 	vkms_configfs_unregister();
273 
274 	if (!default_config)
275 		return;
276 
277 	vkms_destroy(default_config);
278 	vkms_config_destroy(default_config);
279 }
280 
281 module_init(vkms_init);
282 module_exit(vkms_exit);
283 
284 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
285 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
286 MODULE_DESCRIPTION(DRIVER_DESC);
287 MODULE_LICENSE("GPL");
288