vkms_drv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_fbdev_shmem.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_debugfs_entry *entry = m->private;
  72. struct drm_device *dev = entry->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_debugfs_info vkms_config_debugfs_list[] = {
  80. { "vkms_config", vkms_config_show, 0 },
  81. };
  82. static const struct drm_driver vkms_driver = {
  83. .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
  84. .release = vkms_release,
  85. .fops = &vkms_driver_fops,
  86. DRM_GEM_SHMEM_DRIVER_OPS,
  87. .name = DRIVER_NAME,
  88. .desc = DRIVER_DESC,
  89. .date = DRIVER_DATE,
  90. .major = DRIVER_MAJOR,
  91. .minor = DRIVER_MINOR,
  92. };
  93. static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  94. {
  95. struct drm_crtc *crtc;
  96. struct drm_crtc_state *new_crtc_state;
  97. int i;
  98. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  99. if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
  100. continue;
  101. if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
  102. > VKMS_LUT_SIZE)
  103. return -EINVAL;
  104. }
  105. return drm_atomic_helper_check(dev, state);
  106. }
  107. static const struct drm_mode_config_funcs vkms_mode_funcs = {
  108. .fb_create = drm_gem_fb_create,
  109. .atomic_check = vkms_atomic_check,
  110. .atomic_commit = drm_atomic_helper_commit,
  111. };
  112. static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
  113. .atomic_commit_tail = vkms_atomic_commit_tail,
  114. };
  115. static int vkms_modeset_init(struct vkms_device *vkmsdev)
  116. {
  117. struct drm_device *dev = &vkmsdev->drm;
  118. int ret;
  119. ret = drmm_mode_config_init(dev);
  120. if (ret)
  121. return ret;
  122. dev->mode_config.funcs = &vkms_mode_funcs;
  123. dev->mode_config.min_width = XRES_MIN;
  124. dev->mode_config.min_height = YRES_MIN;
  125. dev->mode_config.max_width = XRES_MAX;
  126. dev->mode_config.max_height = YRES_MAX;
  127. dev->mode_config.cursor_width = 512;
  128. dev->mode_config.cursor_height = 512;
  129. /*
  130. * FIXME: There's a confusion between bpp and depth between this and
  131. * fbdev helpers. We have to go with 0, meaning "pick the default",
  132. * which is XRGB8888 in all cases.
  133. */
  134. dev->mode_config.preferred_depth = 0;
  135. dev->mode_config.helper_private = &vkms_mode_config_helpers;
  136. return vkms_output_init(vkmsdev, 0);
  137. }
  138. static int vkms_create(struct vkms_config *config)
  139. {
  140. int ret;
  141. struct platform_device *pdev;
  142. struct vkms_device *vkms_device;
  143. pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
  144. if (IS_ERR(pdev))
  145. return PTR_ERR(pdev);
  146. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  147. ret = -ENOMEM;
  148. goto out_unregister;
  149. }
  150. vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
  151. struct vkms_device, drm);
  152. if (IS_ERR(vkms_device)) {
  153. ret = PTR_ERR(vkms_device);
  154. goto out_devres;
  155. }
  156. vkms_device->platform = pdev;
  157. vkms_device->config = config;
  158. config->dev = vkms_device;
  159. ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
  160. DMA_BIT_MASK(64));
  161. if (ret) {
  162. DRM_ERROR("Could not initialize DMA support\n");
  163. goto out_devres;
  164. }
  165. ret = drm_vblank_init(&vkms_device->drm, 1);
  166. if (ret) {
  167. DRM_ERROR("Failed to vblank\n");
  168. goto out_devres;
  169. }
  170. ret = vkms_modeset_init(vkms_device);
  171. if (ret)
  172. goto out_devres;
  173. drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list,
  174. ARRAY_SIZE(vkms_config_debugfs_list));
  175. ret = drm_dev_register(&vkms_device->drm, 0);
  176. if (ret)
  177. goto out_devres;
  178. drm_fbdev_shmem_setup(&vkms_device->drm, 0);
  179. return 0;
  180. out_devres:
  181. devres_release_group(&pdev->dev, NULL);
  182. out_unregister:
  183. platform_device_unregister(pdev);
  184. return ret;
  185. }
  186. static int __init vkms_init(void)
  187. {
  188. int ret;
  189. struct vkms_config *config;
  190. config = kmalloc(sizeof(*config), GFP_KERNEL);
  191. if (!config)
  192. return -ENOMEM;
  193. config->cursor = enable_cursor;
  194. config->writeback = enable_writeback;
  195. config->overlay = enable_overlay;
  196. ret = vkms_create(config);
  197. if (ret) {
  198. kfree(config);
  199. return ret;
  200. }
  201. default_config = config;
  202. return 0;
  203. }
  204. static void vkms_destroy(struct vkms_config *config)
  205. {
  206. struct platform_device *pdev;
  207. if (!config->dev) {
  208. DRM_INFO("vkms_device is NULL.\n");
  209. return;
  210. }
  211. pdev = config->dev->platform;
  212. drm_dev_unregister(&config->dev->drm);
  213. drm_atomic_helper_shutdown(&config->dev->drm);
  214. devres_release_group(&pdev->dev, NULL);
  215. platform_device_unregister(pdev);
  216. config->dev = NULL;
  217. }
  218. static void __exit vkms_exit(void)
  219. {
  220. if (!default_config)
  221. return;
  222. vkms_destroy(default_config);
  223. kfree(default_config);
  224. }
  225. module_init(vkms_init);
  226. module_exit(vkms_exit);
  227. MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
  228. MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
  229. MODULE_DESCRIPTION(DRIVER_DESC);
  230. MODULE_LICENSE("GPL");