Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_colorop.h>
21#include <drm/drm_drv.h>
22#include <drm/drm_fbdev_shmem.h>
23#include <drm/drm_file.h>
24#include <drm/drm_gem_framebuffer_helper.h>
25#include <drm/drm_ioctl.h>
26#include <drm/drm_managed.h>
27#include <drm/drm_print.h>
28#include <drm/drm_probe_helper.h>
29#include <drm/drm_gem_shmem_helper.h>
30#include <drm/drm_vblank.h>
31
32#include "vkms_config.h"
33#include "vkms_configfs.h"
34#include "vkms_drv.h"
35
36#define DRIVER_NAME "vkms"
37#define DRIVER_DESC "Virtual Kernel Mode Setting"
38#define DRIVER_MAJOR 1
39#define DRIVER_MINOR 0
40
41static struct vkms_config *default_config;
42
43static bool enable_cursor = true;
44module_param_named(enable_cursor, enable_cursor, bool, 0444);
45MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
46
47static bool enable_writeback = true;
48module_param_named(enable_writeback, enable_writeback, bool, 0444);
49MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
50
51static bool enable_overlay;
52module_param_named(enable_overlay, enable_overlay, bool, 0444);
53MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
54
55static bool enable_plane_pipeline;
56module_param_named(enable_plane_pipeline, enable_plane_pipeline, bool, 0444);
57MODULE_PARM_DESC(enable_plane_pipeline, "Enable/Disable plane pipeline support");
58
59static bool create_default_dev = true;
60module_param_named(create_default_dev, create_default_dev, bool, 0444);
61MODULE_PARM_DESC(create_default_dev, "Create or not the default VKMS device");
62
63DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
64
65static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
66{
67 struct drm_device *dev = old_state->dev;
68 struct drm_crtc *crtc;
69 struct drm_crtc_state *old_crtc_state;
70 int i;
71
72 drm_atomic_helper_commit_modeset_disables(dev, old_state);
73
74 drm_atomic_helper_commit_planes(dev, old_state, 0);
75
76 drm_atomic_helper_commit_modeset_enables(dev, old_state);
77
78 drm_atomic_helper_fake_vblank(old_state);
79
80 drm_atomic_helper_commit_hw_done(old_state);
81
82 drm_atomic_helper_wait_for_flip_done(dev, old_state);
83
84 for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
85 struct vkms_crtc_state *vkms_state = 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
93static const struct drm_driver vkms_driver = {
94 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
95 .fops = &vkms_driver_fops,
96 DRM_GEM_SHMEM_DRIVER_OPS,
97 DRM_FBDEV_SHMEM_DRIVER_OPS,
98
99 .name = DRIVER_NAME,
100 .desc = DRIVER_DESC,
101 .major = DRIVER_MAJOR,
102 .minor = DRIVER_MINOR,
103};
104
105static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
106{
107 struct drm_crtc *crtc;
108 struct drm_crtc_state *new_crtc_state;
109 int i;
110
111 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
112 if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
113 continue;
114
115 if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
116 > VKMS_LUT_SIZE)
117 return -EINVAL;
118 }
119
120 return drm_atomic_helper_check(dev, state);
121}
122
123static const struct drm_mode_config_funcs vkms_mode_funcs = {
124 .fb_create = drm_gem_fb_create,
125 .atomic_check = vkms_atomic_check,
126 .atomic_commit = drm_atomic_helper_commit,
127};
128
129static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
130 .atomic_commit_tail = vkms_atomic_commit_tail,
131};
132
133static int vkms_modeset_init(struct vkms_device *vkmsdev)
134{
135 struct drm_device *dev = &vkmsdev->drm;
136 int ret;
137
138 ret = drmm_mode_config_init(dev);
139 if (ret)
140 return ret;
141
142 dev->mode_config.funcs = &vkms_mode_funcs;
143 dev->mode_config.min_width = XRES_MIN;
144 dev->mode_config.min_height = YRES_MIN;
145 dev->mode_config.max_width = XRES_MAX;
146 dev->mode_config.max_height = YRES_MAX;
147 dev->mode_config.cursor_width = 512;
148 dev->mode_config.cursor_height = 512;
149 /*
150 * FIXME: There's a confusion between bpp and depth between this and
151 * fbdev helpers. We have to go with 0, meaning "pick the default",
152 * which is XRGB8888 in all cases.
153 */
154 dev->mode_config.preferred_depth = 0;
155 dev->mode_config.helper_private = &vkms_mode_config_helpers;
156
157 return vkms_output_init(vkmsdev);
158}
159
160int vkms_create(struct vkms_config *config)
161{
162 int ret;
163 struct faux_device *fdev;
164 struct vkms_device *vkms_device;
165 const char *dev_name;
166
167 dev_name = vkms_config_get_device_name(config);
168 fdev = faux_device_create(dev_name, NULL, NULL);
169 if (!fdev)
170 return -ENODEV;
171
172 if (!devres_open_group(&fdev->dev, NULL, GFP_KERNEL)) {
173 ret = -ENOMEM;
174 goto out_unregister;
175 }
176
177 vkms_device = devm_drm_dev_alloc(&fdev->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->faux_dev = fdev;
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,
196 vkms_config_get_num_crtcs(config));
197 if (ret) {
198 DRM_ERROR("Failed to vblank\n");
199 goto out_devres;
200 }
201
202 ret = vkms_modeset_init(vkms_device);
203 if (ret)
204 goto out_devres;
205
206 vkms_config_register_debugfs(vkms_device);
207
208 ret = drm_dev_register(&vkms_device->drm, 0);
209 if (ret)
210 goto out_devres;
211
212 drm_client_setup(&vkms_device->drm, NULL);
213
214 return 0;
215
216out_devres:
217 devres_release_group(&fdev->dev, NULL);
218out_unregister:
219 faux_device_destroy(fdev);
220 return ret;
221}
222
223static int __init vkms_init(void)
224{
225 int ret;
226 struct vkms_config *config;
227
228 ret = vkms_configfs_register();
229 if (ret)
230 return ret;
231
232 if (!create_default_dev)
233 return 0;
234
235 config = vkms_config_default_create(enable_cursor, enable_writeback,
236 enable_overlay, enable_plane_pipeline);
237 if (IS_ERR(config))
238 return PTR_ERR(config);
239
240 ret = vkms_create(config);
241 if (ret) {
242 vkms_config_destroy(config);
243 return ret;
244 }
245
246 default_config = config;
247
248 return 0;
249}
250
251void vkms_destroy(struct vkms_config *config)
252{
253 struct faux_device *fdev;
254
255 if (!config->dev) {
256 DRM_INFO("vkms_device is NULL.\n");
257 return;
258 }
259
260 fdev = config->dev->faux_dev;
261
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
270static 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
281module_init(vkms_init);
282module_exit(vkms_exit);
283
284MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
285MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
286MODULE_DESCRIPTION(DRIVER_DESC);
287MODULE_LICENSE("GPL");