vkms_drv.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /**
  3. * DOC: vkms (Virtual Kernel Modesetting)
  4. *
  5. * VKMS is a software-only model of a KMS driver that is useful for testing
  6. * and for running X (or similar) on headless machines. VKMS aims to enable
  7. * a virtual display with no need of a hardware display capability, releasing
  8. * the GPU in DRM API tests.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <drm/drm_gem.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_drv.h>
  17. #include <drm/drm_fb_helper.h>
  18. #include <drm/drm_file.h>
  19. #include <drm/drm_gem_framebuffer_helper.h>
  20. #include <drm/drm_ioctl.h>
  21. #include <drm/drm_managed.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_gem_shmem_helper.h>
  24. #include <drm/drm_vblank.h>
  25. #include "vkms_drv.h"
  26. #include <drm/drm_print.h>
  27. #include <drm/drm_debugfs.h>
  28. #define DRIVER_NAME "vkms"
  29. #define DRIVER_DESC "Virtual Kernel Mode Setting"
  30. #define DRIVER_DATE "20180514"
  31. #define DRIVER_MAJOR 1
  32. #define DRIVER_MINOR 0
  33. static struct vkms_config *default_config;
  34. static bool enable_cursor = true;
  35. module_param_named(enable_cursor, enable_cursor, bool, 0444);
  36. MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
  37. static bool enable_writeback = true;
  38. module_param_named(enable_writeback, enable_writeback, bool, 0444);
  39. MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
  40. static bool enable_overlay;
  41. module_param_named(enable_overlay, enable_overlay, bool, 0444);
  42. MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
  43. DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
  44. static void vkms_release(struct drm_device *dev)
  45. {
  46. struct vkms_device *vkms = drm_device_to_vkms_device(dev);
  47. if (vkms->output.composer_workq)
  48. destroy_workqueue(vkms->output.composer_workq);
  49. }
  50. static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
  51. {
  52. struct drm_device *dev = old_state->dev;
  53. struct drm_crtc *crtc;
  54. struct drm_crtc_state *old_crtc_state;
  55. int i;
  56. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  57. drm_atomic_helper_commit_planes(dev, old_state, 0);
  58. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  59. drm_atomic_helper_fake_vblank(old_state);
  60. drm_atomic_helper_commit_hw_done(old_state);
  61. drm_atomic_helper_wait_for_flip_done(dev, old_state);
  62. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  63. struct vkms_crtc_state *vkms_state =
  64. to_vkms_crtc_state(old_crtc_state);
  65. flush_work(&vkms_state->composer_work);
  66. }
  67. drm_atomic_helper_cleanup_planes(dev, old_state);
  68. }
  69. static int vkms_config_show(struct seq_file *m, void *data)
  70. {
  71. struct drm_info_node *node = (struct drm_info_node *)m->private;
  72. struct drm_device *dev = node->minor->dev;
  73. struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
  74. seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback);
  75. seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor);
  76. seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay);
  77. return 0;
  78. }
  79. static const struct drm_info_list vkms_config_debugfs_list[] = {
  80. { "vkms_config", vkms_config_show, 0 },
  81. };
  82. static void vkms_config_debugfs_init(struct drm_minor *minor)
  83. {
  84. drm_debugfs_create_files(vkms_config_debugfs_list, ARRAY_SIZE(vkms_config_debugfs_list),
  85. minor->debugfs_root, minor);
  86. }
  87. static const struct drm_driver vkms_driver = {
  88. .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
  89. .release = vkms_release,
  90. .fops = &vkms_driver_fops,
  91. DRM_GEM_SHMEM_DRIVER_OPS,
  92. .debugfs_init = vkms_config_debugfs_init,
  93. .name = DRIVER_NAME,
  94. .desc = DRIVER_DESC,
  95. .date = DRIVER_DATE,
  96. .major = DRIVER_MAJOR,
  97. .minor = DRIVER_MINOR,
  98. };
  99. static const struct drm_mode_config_funcs vkms_mode_funcs = {
  100. .fb_create = drm_gem_fb_create,
  101. .atomic_check = drm_atomic_helper_check,
  102. .atomic_commit = drm_atomic_helper_commit,
  103. };
  104. static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
  105. .atomic_commit_tail = vkms_atomic_commit_tail,
  106. };
  107. static int vkms_modeset_init(struct vkms_device *vkmsdev)
  108. {
  109. struct drm_device *dev = &vkmsdev->drm;
  110. drm_mode_config_init(dev);
  111. dev->mode_config.funcs = &vkms_mode_funcs;
  112. dev->mode_config.min_width = XRES_MIN;
  113. dev->mode_config.min_height = YRES_MIN;
  114. dev->mode_config.max_width = XRES_MAX;
  115. dev->mode_config.max_height = YRES_MAX;
  116. dev->mode_config.cursor_width = 512;
  117. dev->mode_config.cursor_height = 512;
  118. /* FIXME: There's a confusion between bpp and depth between this and
  119. * fbdev helpers. We have to go with 0, meaning "pick the default",
  120. * which ix XRGB8888 in all cases. */
  121. dev->mode_config.preferred_depth = 0;
  122. dev->mode_config.helper_private = &vkms_mode_config_helpers;
  123. return vkms_output_init(vkmsdev, 0);
  124. }
  125. static int vkms_create(struct vkms_config *config)
  126. {
  127. int ret;
  128. struct platform_device *pdev;
  129. struct vkms_device *vkms_device;
  130. pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  131. if (IS_ERR(pdev))
  132. return PTR_ERR(pdev);
  133. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  134. ret = -ENOMEM;
  135. goto out_unregister;
  136. }
  137. vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
  138. struct vkms_device, drm);
  139. if (IS_ERR(vkms_device)) {
  140. ret = PTR_ERR(vkms_device);
  141. goto out_devres;
  142. }
  143. vkms_device->platform = pdev;
  144. vkms_device->config = config;
  145. config->dev = vkms_device;
  146. ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
  147. DMA_BIT_MASK(64));
  148. if (ret) {
  149. DRM_ERROR("Could not initialize DMA support\n");
  150. goto out_devres;
  151. }
  152. ret = drm_vblank_init(&vkms_device->drm, 1);
  153. if (ret) {
  154. DRM_ERROR("Failed to vblank\n");
  155. goto out_devres;
  156. }
  157. ret = vkms_modeset_init(vkms_device);
  158. if (ret)
  159. goto out_devres;
  160. ret = drm_dev_register(&vkms_device->drm, 0);
  161. if (ret)
  162. goto out_devres;
  163. drm_fbdev_generic_setup(&vkms_device->drm, 0);
  164. return 0;
  165. out_devres:
  166. devres_release_group(&pdev->dev, NULL);
  167. out_unregister:
  168. platform_device_unregister(pdev);
  169. return ret;
  170. }
  171. static int __init vkms_init(void)
  172. {
  173. int ret;
  174. struct vkms_config *config;
  175. config = kmalloc(sizeof(*config), GFP_KERNEL);
  176. if (!config)
  177. return -ENOMEM;
  178. default_config = config;
  179. config->cursor = enable_cursor;
  180. config->writeback = enable_writeback;
  181. config->overlay = enable_overlay;
  182. ret = vkms_create(config);
  183. if (ret)
  184. kfree(config);
  185. return ret;
  186. }
  187. static void vkms_destroy(struct vkms_config *config)
  188. {
  189. struct platform_device *pdev;
  190. if (!config->dev) {
  191. DRM_INFO("vkms_device is NULL.\n");
  192. return;
  193. }
  194. pdev = config->dev->platform;
  195. drm_dev_unregister(&config->dev->drm);
  196. drm_atomic_helper_shutdown(&config->dev->drm);
  197. devres_release_group(&pdev->dev, NULL);
  198. platform_device_unregister(pdev);
  199. config->dev = NULL;
  200. }
  201. static void __exit vkms_exit(void)
  202. {
  203. if (default_config->dev)
  204. vkms_destroy(default_config);
  205. kfree(default_config);
  206. }
  207. module_init(vkms_init);
  208. module_exit(vkms_exit);
  209. MODULE_AUTHOR("Haneen Mohammed <[email protected]>");
  210. MODULE_AUTHOR("Rodrigo Siqueira <[email protected]>");
  211. MODULE_DESCRIPTION(DRIVER_DESC);
  212. MODULE_LICENSE("GPL");