virtgpu_drv.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/aperture.h>
  29. #include <linux/module.h>
  30. #include <linux/pci.h>
  31. #include <linux/poll.h>
  32. #include <linux/vgaarb.h>
  33. #include <linux/wait.h>
  34. #include <drm/clients/drm_client_setup.h>
  35. #include <drm/drm.h>
  36. #include <drm/drm_atomic_helper.h>
  37. #include <drm/drm_drv.h>
  38. #include <drm/drm_fbdev_shmem.h>
  39. #include <drm/drm_file.h>
  40. #include <drm/drm_print.h>
  41. #include "virtgpu_drv.h"
  42. #define PCI_DEVICE_ID_VIRTIO_GPU 0x1050
  43. static const struct drm_driver driver;
  44. static int virtio_gpu_modeset = -1;
  45. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  46. module_param_named(modeset, virtio_gpu_modeset, int, 0400);
  47. static int virtio_gpu_pci_quirk(struct drm_device *dev)
  48. {
  49. struct pci_dev *pdev = to_pci_dev(dev->dev);
  50. const char *pname = dev_name(&pdev->dev);
  51. bool vga = pci_is_vga(pdev);
  52. int ret;
  53. DRM_INFO("pci: %s detected at %s\n",
  54. vga ? "virtio-vga" : "virtio-gpu-pci",
  55. pname);
  56. if (vga) {
  57. ret = aperture_remove_conflicting_pci_devices(pdev, driver.name);
  58. if (ret)
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. static int virtio_gpu_probe(struct virtio_device *vdev)
  64. {
  65. struct drm_device *dev;
  66. int ret;
  67. if (drm_firmware_drivers_only() && virtio_gpu_modeset == -1)
  68. return -EINVAL;
  69. if (virtio_gpu_modeset == 0)
  70. return -EINVAL;
  71. /*
  72. * The virtio-gpu device is a virtual device that doesn't have DMA
  73. * ops assigned to it, nor DMA mask set and etc. Its parent device
  74. * is actual GPU device we want to use it for the DRM's device in
  75. * order to benefit from using generic DRM APIs.
  76. */
  77. dev = drm_dev_alloc(&driver, vdev->dev.parent);
  78. if (IS_ERR(dev))
  79. return PTR_ERR(dev);
  80. vdev->priv = dev;
  81. if (dev_is_pci(vdev->dev.parent)) {
  82. ret = virtio_gpu_pci_quirk(dev);
  83. if (ret)
  84. goto err_free;
  85. }
  86. dma_set_max_seg_size(dev->dev, dma_max_mapping_size(dev->dev) ?: UINT_MAX);
  87. ret = virtio_gpu_init(vdev, dev);
  88. if (ret)
  89. goto err_free;
  90. ret = drm_dev_register(dev, 0);
  91. if (ret)
  92. goto err_deinit;
  93. drm_client_setup(vdev->priv, NULL);
  94. return 0;
  95. err_deinit:
  96. virtio_gpu_deinit(dev);
  97. err_free:
  98. drm_dev_put(dev);
  99. return ret;
  100. }
  101. static void virtio_gpu_remove(struct virtio_device *vdev)
  102. {
  103. struct drm_device *dev = vdev->priv;
  104. drm_dev_unplug(dev);
  105. drm_atomic_helper_shutdown(dev);
  106. virtio_gpu_deinit(dev);
  107. drm_dev_put(dev);
  108. }
  109. static void virtio_gpu_shutdown(struct virtio_device *vdev)
  110. {
  111. struct drm_device *dev = vdev->priv;
  112. /* stop talking to the device */
  113. drm_dev_unplug(dev);
  114. }
  115. static void virtio_gpu_config_changed(struct virtio_device *vdev)
  116. {
  117. struct drm_device *dev = vdev->priv;
  118. struct virtio_gpu_device *vgdev = dev->dev_private;
  119. schedule_work(&vgdev->config_changed_work);
  120. }
  121. static struct virtio_device_id id_table[] = {
  122. { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
  123. { 0 },
  124. };
  125. static unsigned int features[] = {
  126. #ifdef __LITTLE_ENDIAN
  127. /*
  128. * Gallium command stream send by virgl is native endian.
  129. * Because of that we only support little endian guests on
  130. * little endian hosts.
  131. */
  132. VIRTIO_GPU_F_VIRGL,
  133. #endif
  134. VIRTIO_GPU_F_EDID,
  135. VIRTIO_GPU_F_RESOURCE_UUID,
  136. VIRTIO_GPU_F_RESOURCE_BLOB,
  137. VIRTIO_GPU_F_CONTEXT_INIT,
  138. };
  139. static struct virtio_driver virtio_gpu_driver = {
  140. .feature_table = features,
  141. .feature_table_size = ARRAY_SIZE(features),
  142. .driver.name = KBUILD_MODNAME,
  143. .id_table = id_table,
  144. .probe = virtio_gpu_probe,
  145. .remove = virtio_gpu_remove,
  146. .shutdown = virtio_gpu_shutdown,
  147. .config_changed = virtio_gpu_config_changed
  148. };
  149. static int __init virtio_gpu_driver_init(void)
  150. {
  151. struct pci_dev *pdev;
  152. int ret;
  153. pdev = pci_get_device(PCI_VENDOR_ID_REDHAT_QUMRANET,
  154. PCI_DEVICE_ID_VIRTIO_GPU,
  155. NULL);
  156. if (pdev && pci_is_vga(pdev)) {
  157. ret = vga_get_interruptible(pdev,
  158. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
  159. if (ret) {
  160. pci_dev_put(pdev);
  161. return ret;
  162. }
  163. }
  164. ret = register_virtio_driver(&virtio_gpu_driver);
  165. if (pdev) {
  166. if (pci_is_vga(pdev))
  167. vga_put(pdev,
  168. VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
  169. pci_dev_put(pdev);
  170. }
  171. return ret;
  172. }
  173. static void __exit virtio_gpu_driver_exit(void)
  174. {
  175. unregister_virtio_driver(&virtio_gpu_driver);
  176. }
  177. module_init(virtio_gpu_driver_init);
  178. module_exit(virtio_gpu_driver_exit);
  179. MODULE_DEVICE_TABLE(virtio, id_table);
  180. MODULE_DESCRIPTION("Virtio GPU driver");
  181. MODULE_LICENSE("GPL and additional rights");
  182. MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
  183. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  184. MODULE_AUTHOR("Alon Levy");
  185. DEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
  186. static const struct drm_driver driver = {
  187. /*
  188. * If KMS is disabled DRIVER_MODESET and DRIVER_ATOMIC are masked
  189. * out via drm_device::driver_features:
  190. */
  191. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC |
  192. DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE | DRIVER_CURSOR_HOTSPOT,
  193. .open = virtio_gpu_driver_open,
  194. .postclose = virtio_gpu_driver_postclose,
  195. .dumb_create = virtio_gpu_mode_dumb_create,
  196. DRM_FBDEV_SHMEM_DRIVER_OPS,
  197. #if defined(CONFIG_DEBUG_FS)
  198. .debugfs_init = virtio_gpu_debugfs_init,
  199. #endif
  200. .gem_prime_import = virtgpu_gem_prime_import,
  201. .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
  202. .gem_create_object = virtio_gpu_create_object,
  203. .fops = &virtio_gpu_driver_fops,
  204. .ioctls = virtio_gpu_ioctls,
  205. .num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
  206. .name = DRIVER_NAME,
  207. .desc = DRIVER_DESC,
  208. .major = DRIVER_MAJOR,
  209. .minor = DRIVER_MINOR,
  210. .patchlevel = DRIVER_PATCHLEVEL,
  211. .release = virtio_gpu_release,
  212. };