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/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_probe_helper.h> 27 #include <drm/drm_gem_shmem_helper.h> 28 #include <drm/drm_vblank.h> 29 30 #include "vkms_drv.h" 31 32 #include <drm/drm_print.h> 33 #include <drm/drm_debugfs.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 DEFINE_DRM_GEM_FOPS(vkms_driver_fops); 55 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 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 int vkms_config_show(struct seq_file *m, void *data) 93 { 94 struct drm_debugfs_entry *entry = m->private; 95 struct drm_device *dev = entry->dev; 96 struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev); 97 98 seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback); 99 seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor); 100 seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay); 101 102 return 0; 103 } 104 105 static const struct drm_debugfs_info vkms_config_debugfs_list[] = { 106 { "vkms_config", vkms_config_show, 0 }, 107 }; 108 109 static const struct drm_driver vkms_driver = { 110 .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM, 111 .release = vkms_release, 112 .fops = &vkms_driver_fops, 113 DRM_GEM_SHMEM_DRIVER_OPS, 114 DRM_FBDEV_SHMEM_DRIVER_OPS, 115 116 .name = DRIVER_NAME, 117 .desc = DRIVER_DESC, 118 .major = DRIVER_MAJOR, 119 .minor = DRIVER_MINOR, 120 }; 121 122 static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 123 { 124 struct drm_crtc *crtc; 125 struct drm_crtc_state *new_crtc_state; 126 int i; 127 128 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 129 if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed) 130 continue; 131 132 if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *) 133 > VKMS_LUT_SIZE) 134 return -EINVAL; 135 } 136 137 return drm_atomic_helper_check(dev, state); 138 } 139 140 static const struct drm_mode_config_funcs vkms_mode_funcs = { 141 .fb_create = drm_gem_fb_create, 142 .atomic_check = vkms_atomic_check, 143 .atomic_commit = drm_atomic_helper_commit, 144 }; 145 146 static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = { 147 .atomic_commit_tail = vkms_atomic_commit_tail, 148 }; 149 150 static int vkms_modeset_init(struct vkms_device *vkmsdev) 151 { 152 struct drm_device *dev = &vkmsdev->drm; 153 int ret; 154 155 ret = drmm_mode_config_init(dev); 156 if (ret) 157 return ret; 158 159 dev->mode_config.funcs = &vkms_mode_funcs; 160 dev->mode_config.min_width = XRES_MIN; 161 dev->mode_config.min_height = YRES_MIN; 162 dev->mode_config.max_width = XRES_MAX; 163 dev->mode_config.max_height = YRES_MAX; 164 dev->mode_config.cursor_width = 512; 165 dev->mode_config.cursor_height = 512; 166 /* 167 * FIXME: There's a confusion between bpp and depth between this and 168 * fbdev helpers. We have to go with 0, meaning "pick the default", 169 * which is XRGB8888 in all cases. 170 */ 171 dev->mode_config.preferred_depth = 0; 172 dev->mode_config.helper_private = &vkms_mode_config_helpers; 173 174 return vkms_output_init(vkmsdev); 175 } 176 177 static int vkms_create(struct vkms_config *config) 178 { 179 int ret; 180 struct platform_device *pdev; 181 struct vkms_device *vkms_device; 182 183 pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); 184 if (IS_ERR(pdev)) 185 return PTR_ERR(pdev); 186 187 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) { 188 ret = -ENOMEM; 189 goto out_unregister; 190 } 191 192 vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver, 193 struct vkms_device, drm); 194 if (IS_ERR(vkms_device)) { 195 ret = PTR_ERR(vkms_device); 196 goto out_devres; 197 } 198 vkms_device->platform = pdev; 199 vkms_device->config = config; 200 config->dev = vkms_device; 201 202 ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev, 203 DMA_BIT_MASK(64)); 204 205 if (ret) { 206 DRM_ERROR("Could not initialize DMA support\n"); 207 goto out_devres; 208 } 209 210 ret = drm_vblank_init(&vkms_device->drm, 1); 211 if (ret) { 212 DRM_ERROR("Failed to vblank\n"); 213 goto out_devres; 214 } 215 216 ret = vkms_modeset_init(vkms_device); 217 if (ret) 218 goto out_devres; 219 220 drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list, 221 ARRAY_SIZE(vkms_config_debugfs_list)); 222 223 ret = drm_dev_register(&vkms_device->drm, 0); 224 if (ret) 225 goto out_devres; 226 227 drm_client_setup(&vkms_device->drm, NULL); 228 229 return 0; 230 231 out_devres: 232 devres_release_group(&pdev->dev, NULL); 233 out_unregister: 234 platform_device_unregister(pdev); 235 return ret; 236 } 237 238 static int __init vkms_init(void) 239 { 240 int ret; 241 struct vkms_config *config; 242 243 config = kmalloc(sizeof(*config), GFP_KERNEL); 244 if (!config) 245 return -ENOMEM; 246 247 default_config = config; 248 249 config->cursor = enable_cursor; 250 config->writeback = enable_writeback; 251 config->overlay = enable_overlay; 252 253 ret = vkms_create(config); 254 if (ret) 255 kfree(config); 256 257 return ret; 258 } 259 260 static void vkms_destroy(struct vkms_config *config) 261 { 262 struct platform_device *pdev; 263 264 if (!config->dev) { 265 DRM_INFO("vkms_device is NULL.\n"); 266 return; 267 } 268 269 pdev = config->dev->platform; 270 271 drm_dev_unregister(&config->dev->drm); 272 drm_atomic_helper_shutdown(&config->dev->drm); 273 devres_release_group(&pdev->dev, NULL); 274 platform_device_unregister(pdev); 275 276 config->dev = NULL; 277 } 278 279 static void __exit vkms_exit(void) 280 { 281 if (default_config->dev) 282 vkms_destroy(default_config); 283 284 kfree(default_config); 285 } 286 287 module_init(vkms_init); 288 module_exit(vkms_exit); 289 290 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>"); 291 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>"); 292 MODULE_DESCRIPTION(DRIVER_DESC); 293 MODULE_LICENSE("GPL"); 294