vkms_drv.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/device/faux.h>
  12. #include <linux/dma-mapping.h>
  13. #include <drm/clients/drm_client_setup.h>
  14. #include <drm/drm_gem.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_colorop.h>
  18. #include <drm/drm_drv.h>
  19. #include <drm/drm_fbdev_shmem.h>
  20. #include <drm/drm_file.h>
  21. #include <drm/drm_gem_framebuffer_helper.h>
  22. #include <drm/drm_ioctl.h>
  23. #include <drm/drm_managed.h>
  24. #include <drm/drm_print.h>
  25. #include <drm/drm_probe_helper.h>
  26. #include <drm/drm_gem_shmem_helper.h>
  27. #include <drm/drm_vblank.h>
  28. #include "vkms_config.h"
  29. #include "vkms_configfs.h"
  30. #include "vkms_drv.h"
  31. #define DRIVER_NAME "vkms"
  32. #define DRIVER_DESC "Virtual Kernel Mode Setting"
  33. #define DRIVER_MAJOR 1
  34. #define DRIVER_MINOR 0
  35. static struct vkms_config *default_config;
  36. static bool enable_cursor = true;
  37. module_param_named(enable_cursor, enable_cursor, bool, 0444);
  38. MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
  39. static bool enable_writeback = true;
  40. module_param_named(enable_writeback, enable_writeback, bool, 0444);
  41. MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
  42. static bool enable_overlay;
  43. module_param_named(enable_overlay, enable_overlay, bool, 0444);
  44. MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
  45. static bool enable_plane_pipeline;
  46. module_param_named(enable_plane_pipeline, enable_plane_pipeline, bool, 0444);
  47. MODULE_PARM_DESC(enable_plane_pipeline, "Enable/Disable plane pipeline support");
  48. static bool create_default_dev = true;
  49. module_param_named(create_default_dev, create_default_dev, bool, 0444);
  50. MODULE_PARM_DESC(create_default_dev, "Create or not the default VKMS device");
  51. DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
  52. static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
  53. {
  54. struct drm_device *dev = old_state->dev;
  55. struct drm_crtc *crtc;
  56. struct drm_crtc_state *old_crtc_state;
  57. int i;
  58. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  59. drm_atomic_helper_commit_planes(dev, old_state, 0);
  60. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  61. drm_atomic_helper_fake_vblank(old_state);
  62. drm_atomic_helper_commit_hw_done(old_state);
  63. drm_atomic_helper_wait_for_flip_done(dev, old_state);
  64. for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  65. struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(old_crtc_state);
  66. flush_work(&vkms_state->composer_work);
  67. }
  68. drm_atomic_helper_cleanup_planes(dev, old_state);
  69. }
  70. static const struct drm_driver vkms_driver = {
  71. .driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
  72. .fops = &vkms_driver_fops,
  73. DRM_GEM_SHMEM_DRIVER_OPS,
  74. DRM_FBDEV_SHMEM_DRIVER_OPS,
  75. .name = DRIVER_NAME,
  76. .desc = DRIVER_DESC,
  77. .major = DRIVER_MAJOR,
  78. .minor = DRIVER_MINOR,
  79. };
  80. static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  81. {
  82. struct drm_crtc *crtc;
  83. struct drm_crtc_state *new_crtc_state;
  84. int i;
  85. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  86. if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
  87. continue;
  88. if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
  89. > VKMS_LUT_SIZE)
  90. return -EINVAL;
  91. }
  92. return drm_atomic_helper_check(dev, state);
  93. }
  94. static const struct drm_mode_config_funcs vkms_mode_funcs = {
  95. .fb_create = drm_gem_fb_create,
  96. .atomic_check = vkms_atomic_check,
  97. .atomic_commit = drm_atomic_helper_commit,
  98. };
  99. static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
  100. .atomic_commit_tail = vkms_atomic_commit_tail,
  101. };
  102. static int vkms_modeset_init(struct vkms_device *vkmsdev)
  103. {
  104. struct drm_device *dev = &vkmsdev->drm;
  105. int ret;
  106. ret = drmm_mode_config_init(dev);
  107. if (ret)
  108. return ret;
  109. dev->mode_config.funcs = &vkms_mode_funcs;
  110. dev->mode_config.min_width = XRES_MIN;
  111. dev->mode_config.min_height = YRES_MIN;
  112. dev->mode_config.max_width = XRES_MAX;
  113. dev->mode_config.max_height = YRES_MAX;
  114. dev->mode_config.cursor_width = 512;
  115. dev->mode_config.cursor_height = 512;
  116. /*
  117. * FIXME: There's a confusion between bpp and depth between this and
  118. * fbdev helpers. We have to go with 0, meaning "pick the default",
  119. * which is XRGB8888 in all cases.
  120. */
  121. dev->mode_config.preferred_depth = 0;
  122. dev->mode_config.helper_private = &vkms_mode_config_helpers;
  123. return vkms_output_init(vkmsdev);
  124. }
  125. int vkms_create(struct vkms_config *config)
  126. {
  127. int ret;
  128. struct faux_device *fdev;
  129. struct vkms_device *vkms_device;
  130. const char *dev_name;
  131. dev_name = vkms_config_get_device_name(config);
  132. fdev = faux_device_create(dev_name, NULL, NULL);
  133. if (!fdev)
  134. return -ENODEV;
  135. if (!devres_open_group(&fdev->dev, NULL, GFP_KERNEL)) {
  136. ret = -ENOMEM;
  137. goto out_unregister;
  138. }
  139. vkms_device = devm_drm_dev_alloc(&fdev->dev, &vkms_driver,
  140. struct vkms_device, drm);
  141. if (IS_ERR(vkms_device)) {
  142. ret = PTR_ERR(vkms_device);
  143. goto out_devres;
  144. }
  145. vkms_device->faux_dev = fdev;
  146. vkms_device->config = config;
  147. config->dev = vkms_device;
  148. ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
  149. DMA_BIT_MASK(64));
  150. if (ret) {
  151. DRM_ERROR("Could not initialize DMA support\n");
  152. goto out_devres;
  153. }
  154. ret = drm_vblank_init(&vkms_device->drm,
  155. vkms_config_get_num_crtcs(config));
  156. if (ret) {
  157. DRM_ERROR("Failed to vblank\n");
  158. goto out_devres;
  159. }
  160. ret = vkms_modeset_init(vkms_device);
  161. if (ret)
  162. goto out_devres;
  163. vkms_config_register_debugfs(vkms_device);
  164. ret = drm_dev_register(&vkms_device->drm, 0);
  165. if (ret)
  166. goto out_devres;
  167. drm_client_setup(&vkms_device->drm, NULL);
  168. return 0;
  169. out_devres:
  170. devres_release_group(&fdev->dev, NULL);
  171. out_unregister:
  172. faux_device_destroy(fdev);
  173. return ret;
  174. }
  175. static int __init vkms_init(void)
  176. {
  177. int ret;
  178. struct vkms_config *config;
  179. ret = vkms_configfs_register();
  180. if (ret)
  181. return ret;
  182. if (!create_default_dev)
  183. return 0;
  184. config = vkms_config_default_create(enable_cursor, enable_writeback,
  185. enable_overlay, enable_plane_pipeline);
  186. if (IS_ERR(config))
  187. return PTR_ERR(config);
  188. ret = vkms_create(config);
  189. if (ret) {
  190. vkms_config_destroy(config);
  191. return ret;
  192. }
  193. default_config = config;
  194. return 0;
  195. }
  196. void vkms_destroy(struct vkms_config *config)
  197. {
  198. struct faux_device *fdev;
  199. if (!config->dev) {
  200. DRM_INFO("vkms_device is NULL.\n");
  201. return;
  202. }
  203. fdev = config->dev->faux_dev;
  204. drm_colorop_pipeline_destroy(&config->dev->drm);
  205. drm_dev_unregister(&config->dev->drm);
  206. drm_atomic_helper_shutdown(&config->dev->drm);
  207. devres_release_group(&fdev->dev, NULL);
  208. faux_device_destroy(fdev);
  209. config->dev = NULL;
  210. }
  211. static void __exit vkms_exit(void)
  212. {
  213. vkms_configfs_unregister();
  214. if (!default_config)
  215. return;
  216. vkms_destroy(default_config);
  217. vkms_config_destroy(default_config);
  218. }
  219. module_init(vkms_init);
  220. module_exit(vkms_exit);
  221. MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
  222. MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
  223. MODULE_DESCRIPTION(DRIVER_DESC);
  224. MODULE_LICENSE("GPL");