virtgpu_drv.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie <[email protected]>
  7. * Gerd Hoffmann <[email protected]>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/pci.h>
  30. #include <linux/poll.h>
  31. #include <linux/wait.h>
  32. #include <drm/drm.h>
  33. #include <drm/drm_aperture.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/drm_drv.h>
  36. #include <drm/drm_file.h>
  37. #include "virtgpu_drv.h"
  38. static const struct drm_driver driver;
  39. static int virtio_gpu_modeset = -1;
  40. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  41. module_param_named(modeset, virtio_gpu_modeset, int, 0400);
  42. static int virtio_gpu_pci_quirk(struct drm_device *dev)
  43. {
  44. struct pci_dev *pdev = to_pci_dev(dev->dev);
  45. const char *pname = dev_name(&pdev->dev);
  46. bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
  47. int ret;
  48. DRM_INFO("pci: %s detected at %s\n",
  49. vga ? "virtio-vga" : "virtio-gpu-pci",
  50. pname);
  51. if (vga) {
  52. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
  53. if (ret)
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static int virtio_gpu_probe(struct virtio_device *vdev)
  59. {
  60. struct drm_device *dev;
  61. int ret;
  62. if (drm_firmware_drivers_only() && virtio_gpu_modeset == -1)
  63. return -EINVAL;
  64. if (virtio_gpu_modeset == 0)
  65. return -EINVAL;
  66. /*
  67. * The virtio-gpu device is a virtual device that doesn't have DMA
  68. * ops assigned to it, nor DMA mask set and etc. Its parent device
  69. * is actual GPU device we want to use it for the DRM's device in
  70. * order to benefit from using generic DRM APIs.
  71. */
  72. dev = drm_dev_alloc(&driver, vdev->dev.parent);
  73. if (IS_ERR(dev))
  74. return PTR_ERR(dev);
  75. vdev->priv = dev;
  76. if (dev_is_pci(vdev->dev.parent)) {
  77. ret = virtio_gpu_pci_quirk(dev);
  78. if (ret)
  79. goto err_free;
  80. }
  81. ret = virtio_gpu_init(vdev, dev);
  82. if (ret)
  83. goto err_free;
  84. ret = drm_dev_register(dev, 0);
  85. if (ret)
  86. goto err_deinit;
  87. drm_fbdev_generic_setup(vdev->priv, 32);
  88. return 0;
  89. err_deinit:
  90. virtio_gpu_deinit(dev);
  91. err_free:
  92. drm_dev_put(dev);
  93. return ret;
  94. }
  95. static void virtio_gpu_remove(struct virtio_device *vdev)
  96. {
  97. struct drm_device *dev = vdev->priv;
  98. drm_dev_unplug(dev);
  99. drm_atomic_helper_shutdown(dev);
  100. virtio_gpu_deinit(dev);
  101. drm_dev_put(dev);
  102. }
  103. static void virtio_gpu_config_changed(struct virtio_device *vdev)
  104. {
  105. struct drm_device *dev = vdev->priv;
  106. struct virtio_gpu_device *vgdev = dev->dev_private;
  107. schedule_work(&vgdev->config_changed_work);
  108. }
  109. static struct virtio_device_id id_table[] = {
  110. { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
  111. { 0 },
  112. };
  113. static unsigned int features[] = {
  114. #ifdef __LITTLE_ENDIAN
  115. /*
  116. * Gallium command stream send by virgl is native endian.
  117. * Because of that we only support little endian guests on
  118. * little endian hosts.
  119. */
  120. VIRTIO_GPU_F_VIRGL,
  121. #endif
  122. VIRTIO_GPU_F_EDID,
  123. VIRTIO_GPU_F_RESOURCE_UUID,
  124. VIRTIO_GPU_F_RESOURCE_BLOB,
  125. VIRTIO_GPU_F_CONTEXT_INIT,
  126. };
  127. static struct virtio_driver virtio_gpu_driver = {
  128. .feature_table = features,
  129. .feature_table_size = ARRAY_SIZE(features),
  130. .driver.name = KBUILD_MODNAME,
  131. .driver.owner = THIS_MODULE,
  132. .id_table = id_table,
  133. .probe = virtio_gpu_probe,
  134. .remove = virtio_gpu_remove,
  135. .config_changed = virtio_gpu_config_changed
  136. };
  137. module_virtio_driver(virtio_gpu_driver);
  138. MODULE_DEVICE_TABLE(virtio, id_table);
  139. MODULE_DESCRIPTION("Virtio GPU driver");
  140. MODULE_LICENSE("GPL and additional rights");
  141. MODULE_AUTHOR("Dave Airlie <[email protected]>");
  142. MODULE_AUTHOR("Gerd Hoffmann <[email protected]>");
  143. MODULE_AUTHOR("Alon Levy");
  144. DEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
  145. static const struct drm_driver driver = {
  146. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC,
  147. .open = virtio_gpu_driver_open,
  148. .postclose = virtio_gpu_driver_postclose,
  149. .dumb_create = virtio_gpu_mode_dumb_create,
  150. .dumb_map_offset = virtio_gpu_mode_dumb_mmap,
  151. #if defined(CONFIG_DEBUG_FS)
  152. .debugfs_init = virtio_gpu_debugfs_init,
  153. #endif
  154. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  155. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  156. .gem_prime_mmap = drm_gem_prime_mmap,
  157. .gem_prime_import = virtgpu_gem_prime_import,
  158. .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
  159. .gem_create_object = virtio_gpu_create_object,
  160. .fops = &virtio_gpu_driver_fops,
  161. .ioctls = virtio_gpu_ioctls,
  162. .num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
  163. .name = DRIVER_NAME,
  164. .desc = DRIVER_DESC,
  165. .date = DRIVER_DATE,
  166. .major = DRIVER_MAJOR,
  167. .minor = DRIVER_MINOR,
  168. .patchlevel = DRIVER_PATCHLEVEL,
  169. .release = virtio_gpu_release,
  170. };