virtgpu_drv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Authors:
  6. * Dave Airlie <airlied@redhat.com>
  7. * Gerd Hoffmann <kraxel@redhat.com>
  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_fbdev_shmem.h>
  37. #include <drm/drm_file.h>
  38. #include "virtgpu_drv.h"
  39. static const struct drm_driver driver;
  40. static int virtio_gpu_modeset = -1;
  41. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  42. module_param_named(modeset, virtio_gpu_modeset, int, 0400);
  43. static int virtio_gpu_pci_quirk(struct drm_device *dev)
  44. {
  45. struct pci_dev *pdev = to_pci_dev(dev->dev);
  46. const char *pname = dev_name(&pdev->dev);
  47. bool vga = pci_is_vga(pdev);
  48. int ret;
  49. DRM_INFO("pci: %s detected at %s\n",
  50. vga ? "virtio-vga" : "virtio-gpu-pci",
  51. pname);
  52. if (vga) {
  53. ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
  54. if (ret)
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int virtio_gpu_probe(struct virtio_device *vdev)
  60. {
  61. struct drm_device *dev;
  62. int ret;
  63. if (drm_firmware_drivers_only() && virtio_gpu_modeset == -1)
  64. return -EINVAL;
  65. if (virtio_gpu_modeset == 0)
  66. return -EINVAL;
  67. /*
  68. * The virtio-gpu device is a virtual device that doesn't have DMA
  69. * ops assigned to it, nor DMA mask set and etc. Its parent device
  70. * is actual GPU device we want to use it for the DRM's device in
  71. * order to benefit from using generic DRM APIs.
  72. */
  73. dev = drm_dev_alloc(&driver, vdev->dev.parent);
  74. if (IS_ERR(dev))
  75. return PTR_ERR(dev);
  76. vdev->priv = dev;
  77. if (dev_is_pci(vdev->dev.parent)) {
  78. ret = virtio_gpu_pci_quirk(dev);
  79. if (ret)
  80. goto err_free;
  81. }
  82. dma_set_max_seg_size(dev->dev, dma_max_mapping_size(dev->dev) ?: UINT_MAX);
  83. ret = virtio_gpu_init(vdev, dev);
  84. if (ret)
  85. goto err_free;
  86. ret = drm_dev_register(dev, 0);
  87. if (ret)
  88. goto err_deinit;
  89. drm_fbdev_shmem_setup(vdev->priv, 32);
  90. return 0;
  91. err_deinit:
  92. virtio_gpu_deinit(dev);
  93. err_free:
  94. drm_dev_put(dev);
  95. return ret;
  96. }
  97. static void virtio_gpu_remove(struct virtio_device *vdev)
  98. {
  99. struct drm_device *dev = vdev->priv;
  100. drm_dev_unplug(dev);
  101. drm_atomic_helper_shutdown(dev);
  102. virtio_gpu_deinit(dev);
  103. drm_dev_put(dev);
  104. }
  105. static void virtio_gpu_config_changed(struct virtio_device *vdev)
  106. {
  107. struct drm_device *dev = vdev->priv;
  108. struct virtio_gpu_device *vgdev = dev->dev_private;
  109. schedule_work(&vgdev->config_changed_work);
  110. }
  111. static struct virtio_device_id id_table[] = {
  112. { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
  113. { 0 },
  114. };
  115. static unsigned int features[] = {
  116. #ifdef __LITTLE_ENDIAN
  117. /*
  118. * Gallium command stream send by virgl is native endian.
  119. * Because of that we only support little endian guests on
  120. * little endian hosts.
  121. */
  122. VIRTIO_GPU_F_VIRGL,
  123. #endif
  124. VIRTIO_GPU_F_EDID,
  125. VIRTIO_GPU_F_RESOURCE_UUID,
  126. VIRTIO_GPU_F_RESOURCE_BLOB,
  127. VIRTIO_GPU_F_CONTEXT_INIT,
  128. };
  129. static struct virtio_driver virtio_gpu_driver = {
  130. .feature_table = features,
  131. .feature_table_size = ARRAY_SIZE(features),
  132. .driver.name = KBUILD_MODNAME,
  133. .id_table = id_table,
  134. .probe = virtio_gpu_probe,
  135. .remove = virtio_gpu_remove,
  136. .config_changed = virtio_gpu_config_changed
  137. };
  138. module_virtio_driver(virtio_gpu_driver);
  139. MODULE_DEVICE_TABLE(virtio, id_table);
  140. MODULE_DESCRIPTION("Virtio GPU driver");
  141. MODULE_LICENSE("GPL and additional rights");
  142. MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
  143. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  144. MODULE_AUTHOR("Alon Levy");
  145. DEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
  146. static const struct drm_driver driver = {
  147. /*
  148. * If KMS is disabled DRIVER_MODESET and DRIVER_ATOMIC are masked
  149. * out via drm_device::driver_features:
  150. */
  151. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC |
  152. DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE | DRIVER_CURSOR_HOTSPOT,
  153. .open = virtio_gpu_driver_open,
  154. .postclose = virtio_gpu_driver_postclose,
  155. .dumb_create = virtio_gpu_mode_dumb_create,
  156. .dumb_map_offset = virtio_gpu_mode_dumb_mmap,
  157. #if defined(CONFIG_DEBUG_FS)
  158. .debugfs_init = virtio_gpu_debugfs_init,
  159. #endif
  160. .gem_prime_import = virtgpu_gem_prime_import,
  161. .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
  162. .gem_create_object = virtio_gpu_create_object,
  163. .fops = &virtio_gpu_driver_fops,
  164. .ioctls = virtio_gpu_ioctls,
  165. .num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
  166. .name = DRIVER_NAME,
  167. .desc = DRIVER_DESC,
  168. .date = DRIVER_DATE,
  169. .major = DRIVER_MAJOR,
  170. .minor = DRIVER_MINOR,
  171. .patchlevel = DRIVER_PATCHLEVEL,
  172. .release = virtio_gpu_release,
  173. };