| .. | .. |
|---|
| 1 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
|---|
| 2 | 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 | + |
|---|
| 3 | 12 | #include <linux/module.h> |
|---|
| 13 | +#include <linux/platform_device.h> |
|---|
| 14 | +#include <linux/dma-mapping.h> |
|---|
| 15 | + |
|---|
| 4 | 16 | #include <drm/drm_gem.h> |
|---|
| 5 | | -#include <drm/drm_crtc_helper.h> |
|---|
| 17 | +#include <drm/drm_atomic.h> |
|---|
| 6 | 18 | #include <drm/drm_atomic_helper.h> |
|---|
| 7 | | -#include <drm/drm_gem_framebuffer_helper.h> |
|---|
| 19 | +#include <drm/drm_drv.h> |
|---|
| 8 | 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_vblank.h> |
|---|
| 27 | + |
|---|
| 9 | 28 | #include "vkms_drv.h" |
|---|
| 10 | 29 | |
|---|
| 11 | 30 | #define DRIVER_NAME "vkms" |
|---|
| .. | .. |
|---|
| 15 | 34 | #define DRIVER_MINOR 0 |
|---|
| 16 | 35 | |
|---|
| 17 | 36 | static struct vkms_device *vkms_device; |
|---|
| 37 | + |
|---|
| 38 | +bool enable_cursor = true; |
|---|
| 39 | +module_param_named(enable_cursor, enable_cursor, bool, 0444); |
|---|
| 40 | +MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support"); |
|---|
| 18 | 41 | |
|---|
| 19 | 42 | static const struct file_operations vkms_driver_fops = { |
|---|
| 20 | 43 | .owner = THIS_MODULE, |
|---|
| .. | .. |
|---|
| 38 | 61 | { |
|---|
| 39 | 62 | struct vkms_device *vkms = container_of(dev, struct vkms_device, drm); |
|---|
| 40 | 63 | |
|---|
| 41 | | - platform_device_unregister(vkms->platform); |
|---|
| 42 | | - drm_atomic_helper_shutdown(&vkms->drm); |
|---|
| 43 | | - drm_mode_config_cleanup(&vkms->drm); |
|---|
| 44 | | - drm_dev_fini(&vkms->drm); |
|---|
| 64 | + if (vkms->output.composer_workq) |
|---|
| 65 | + destroy_workqueue(vkms->output.composer_workq); |
|---|
| 66 | +} |
|---|
| 67 | + |
|---|
| 68 | +static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state) |
|---|
| 69 | +{ |
|---|
| 70 | + struct drm_device *dev = old_state->dev; |
|---|
| 71 | + struct drm_crtc *crtc; |
|---|
| 72 | + struct drm_crtc_state *old_crtc_state; |
|---|
| 73 | + int i; |
|---|
| 74 | + |
|---|
| 75 | + drm_atomic_helper_commit_modeset_disables(dev, old_state); |
|---|
| 76 | + |
|---|
| 77 | + drm_atomic_helper_commit_planes(dev, old_state, 0); |
|---|
| 78 | + |
|---|
| 79 | + drm_atomic_helper_commit_modeset_enables(dev, old_state); |
|---|
| 80 | + |
|---|
| 81 | + drm_atomic_helper_fake_vblank(old_state); |
|---|
| 82 | + |
|---|
| 83 | + drm_atomic_helper_commit_hw_done(old_state); |
|---|
| 84 | + |
|---|
| 85 | + drm_atomic_helper_wait_for_flip_done(dev, old_state); |
|---|
| 86 | + |
|---|
| 87 | + for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
|---|
| 88 | + struct vkms_crtc_state *vkms_state = |
|---|
| 89 | + to_vkms_crtc_state(old_crtc_state); |
|---|
| 90 | + |
|---|
| 91 | + flush_work(&vkms_state->composer_work); |
|---|
| 92 | + } |
|---|
| 93 | + |
|---|
| 94 | + drm_atomic_helper_cleanup_planes(dev, old_state); |
|---|
| 45 | 95 | } |
|---|
| 46 | 96 | |
|---|
| 47 | 97 | static struct drm_driver vkms_driver = { |
|---|
| .. | .. |
|---|
| 49 | 99 | .release = vkms_release, |
|---|
| 50 | 100 | .fops = &vkms_driver_fops, |
|---|
| 51 | 101 | .dumb_create = vkms_dumb_create, |
|---|
| 52 | | - .dumb_map_offset = vkms_dumb_map, |
|---|
| 53 | 102 | .gem_vm_ops = &vkms_gem_vm_ops, |
|---|
| 54 | 103 | .gem_free_object_unlocked = vkms_gem_free_object, |
|---|
| 55 | | - .get_vblank_timestamp = vkms_get_vblank_timestamp, |
|---|
| 104 | + .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
|---|
| 105 | + .gem_prime_import_sg_table = vkms_prime_import_sg_table, |
|---|
| 56 | 106 | |
|---|
| 57 | 107 | .name = DRIVER_NAME, |
|---|
| 58 | 108 | .desc = DRIVER_DESC, |
|---|
| .. | .. |
|---|
| 67 | 117 | .atomic_commit = drm_atomic_helper_commit, |
|---|
| 68 | 118 | }; |
|---|
| 69 | 119 | |
|---|
| 120 | +static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = { |
|---|
| 121 | + .atomic_commit_tail = vkms_atomic_commit_tail, |
|---|
| 122 | +}; |
|---|
| 123 | + |
|---|
| 70 | 124 | static int vkms_modeset_init(struct vkms_device *vkmsdev) |
|---|
| 71 | 125 | { |
|---|
| 72 | 126 | struct drm_device *dev = &vkmsdev->drm; |
|---|
| .. | .. |
|---|
| 77 | 131 | dev->mode_config.min_height = YRES_MIN; |
|---|
| 78 | 132 | dev->mode_config.max_width = XRES_MAX; |
|---|
| 79 | 133 | dev->mode_config.max_height = YRES_MAX; |
|---|
| 134 | + dev->mode_config.cursor_width = 512; |
|---|
| 135 | + dev->mode_config.cursor_height = 512; |
|---|
| 136 | + dev->mode_config.preferred_depth = 24; |
|---|
| 137 | + dev->mode_config.helper_private = &vkms_mode_config_helpers; |
|---|
| 80 | 138 | |
|---|
| 81 | | - return vkms_output_init(vkmsdev); |
|---|
| 139 | + return vkms_output_init(vkmsdev, 0); |
|---|
| 82 | 140 | } |
|---|
| 83 | 141 | |
|---|
| 84 | 142 | static int __init vkms_init(void) |
|---|
| 85 | 143 | { |
|---|
| 86 | 144 | int ret; |
|---|
| 145 | + struct platform_device *pdev; |
|---|
| 87 | 146 | |
|---|
| 88 | | - vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL); |
|---|
| 89 | | - if (!vkms_device) |
|---|
| 90 | | - return -ENOMEM; |
|---|
| 147 | + pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
|---|
| 148 | + if (IS_ERR(pdev)) |
|---|
| 149 | + return PTR_ERR(pdev); |
|---|
| 91 | 150 | |
|---|
| 92 | | - ret = drm_dev_init(&vkms_device->drm, &vkms_driver, NULL); |
|---|
| 93 | | - if (ret) |
|---|
| 94 | | - goto out_free; |
|---|
| 151 | + if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) { |
|---|
| 152 | + ret = -ENOMEM; |
|---|
| 153 | + goto out_unregister; |
|---|
| 154 | + } |
|---|
| 95 | 155 | |
|---|
| 96 | | - vkms_device->platform = |
|---|
| 97 | | - platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); |
|---|
| 98 | | - if (IS_ERR(vkms_device->platform)) { |
|---|
| 99 | | - ret = PTR_ERR(vkms_device->platform); |
|---|
| 100 | | - goto out_fini; |
|---|
| 156 | + vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver, |
|---|
| 157 | + struct vkms_device, drm); |
|---|
| 158 | + if (IS_ERR(vkms_device)) { |
|---|
| 159 | + ret = PTR_ERR(vkms_device); |
|---|
| 160 | + goto out_devres; |
|---|
| 161 | + } |
|---|
| 162 | + vkms_device->platform = pdev; |
|---|
| 163 | + |
|---|
| 164 | + ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev, |
|---|
| 165 | + DMA_BIT_MASK(64)); |
|---|
| 166 | + |
|---|
| 167 | + if (ret) { |
|---|
| 168 | + DRM_ERROR("Could not initialize DMA support\n"); |
|---|
| 169 | + goto out_devres; |
|---|
| 101 | 170 | } |
|---|
| 102 | 171 | |
|---|
| 103 | 172 | vkms_device->drm.irq_enabled = true; |
|---|
| .. | .. |
|---|
| 105 | 174 | ret = drm_vblank_init(&vkms_device->drm, 1); |
|---|
| 106 | 175 | if (ret) { |
|---|
| 107 | 176 | DRM_ERROR("Failed to vblank\n"); |
|---|
| 108 | | - goto out_fini; |
|---|
| 177 | + goto out_devres; |
|---|
| 109 | 178 | } |
|---|
| 110 | 179 | |
|---|
| 111 | 180 | ret = vkms_modeset_init(vkms_device); |
|---|
| 112 | 181 | if (ret) |
|---|
| 113 | | - goto out_unregister; |
|---|
| 182 | + goto out_devres; |
|---|
| 114 | 183 | |
|---|
| 115 | 184 | ret = drm_dev_register(&vkms_device->drm, 0); |
|---|
| 116 | 185 | if (ret) |
|---|
| 117 | | - goto out_unregister; |
|---|
| 186 | + goto out_devres; |
|---|
| 118 | 187 | |
|---|
| 119 | 188 | return 0; |
|---|
| 120 | 189 | |
|---|
| 190 | +out_devres: |
|---|
| 191 | + devres_release_group(&pdev->dev, NULL); |
|---|
| 121 | 192 | out_unregister: |
|---|
| 122 | | - platform_device_unregister(vkms_device->platform); |
|---|
| 123 | | - |
|---|
| 124 | | -out_fini: |
|---|
| 125 | | - drm_dev_fini(&vkms_device->drm); |
|---|
| 126 | | - |
|---|
| 127 | | -out_free: |
|---|
| 128 | | - kfree(vkms_device); |
|---|
| 193 | + platform_device_unregister(pdev); |
|---|
| 129 | 194 | return ret; |
|---|
| 130 | 195 | } |
|---|
| 131 | 196 | |
|---|
| 132 | 197 | static void __exit vkms_exit(void) |
|---|
| 133 | 198 | { |
|---|
| 199 | + struct platform_device *pdev; |
|---|
| 200 | + |
|---|
| 134 | 201 | if (!vkms_device) { |
|---|
| 135 | 202 | DRM_INFO("vkms_device is NULL.\n"); |
|---|
| 136 | 203 | return; |
|---|
| 137 | 204 | } |
|---|
| 138 | 205 | |
|---|
| 139 | | - drm_dev_unregister(&vkms_device->drm); |
|---|
| 140 | | - drm_dev_put(&vkms_device->drm); |
|---|
| 206 | + pdev = vkms_device->platform; |
|---|
| 141 | 207 | |
|---|
| 142 | | - kfree(vkms_device); |
|---|
| 208 | + drm_dev_unregister(&vkms_device->drm); |
|---|
| 209 | + drm_atomic_helper_shutdown(&vkms_device->drm); |
|---|
| 210 | + devres_release_group(&pdev->dev, NULL); |
|---|
| 211 | + platform_device_unregister(pdev); |
|---|
| 143 | 212 | } |
|---|
| 144 | 213 | |
|---|
| 145 | 214 | module_init(vkms_init); |
|---|