virtgpu_drv.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/console.h>
  30. #include <linux/pci.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm.h>
  33. #include "virtgpu_drv.h"
  34. static struct drm_driver driver;
  35. static int virtio_gpu_modeset = -1;
  36. MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
  37. module_param_named(modeset, virtio_gpu_modeset, int, 0400);
  38. static int virtio_gpu_probe(struct virtio_device *vdev)
  39. {
  40. if (vgacon_text_force() && virtio_gpu_modeset == -1)
  41. return -EINVAL;
  42. if (virtio_gpu_modeset == 0)
  43. return -EINVAL;
  44. return drm_virtio_init(&driver, vdev);
  45. }
  46. static void virtio_gpu_remove(struct virtio_device *vdev)
  47. {
  48. struct drm_device *dev = vdev->priv;
  49. drm_put_dev(dev);
  50. }
  51. static void virtio_gpu_config_changed(struct virtio_device *vdev)
  52. {
  53. struct drm_device *dev = vdev->priv;
  54. struct virtio_gpu_device *vgdev = dev->dev_private;
  55. schedule_work(&vgdev->config_changed_work);
  56. }
  57. static struct virtio_device_id id_table[] = {
  58. { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
  59. { 0 },
  60. };
  61. static unsigned int features[] = {
  62. #ifdef __LITTLE_ENDIAN
  63. /*
  64. * Gallium command stream send by virgl is native endian.
  65. * Because of that we only support little endian guests on
  66. * little endian hosts.
  67. */
  68. VIRTIO_GPU_F_VIRGL,
  69. #endif
  70. };
  71. static struct virtio_driver virtio_gpu_driver = {
  72. .feature_table = features,
  73. .feature_table_size = ARRAY_SIZE(features),
  74. .driver.name = KBUILD_MODNAME,
  75. .driver.owner = THIS_MODULE,
  76. .id_table = id_table,
  77. .probe = virtio_gpu_probe,
  78. .remove = virtio_gpu_remove,
  79. .config_changed = virtio_gpu_config_changed
  80. };
  81. module_virtio_driver(virtio_gpu_driver);
  82. MODULE_DEVICE_TABLE(virtio, id_table);
  83. MODULE_DESCRIPTION("Virtio GPU driver");
  84. MODULE_LICENSE("GPL and additional rights");
  85. MODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
  86. MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
  87. MODULE_AUTHOR("Alon Levy");
  88. static const struct file_operations virtio_gpu_driver_fops = {
  89. .owner = THIS_MODULE,
  90. .open = drm_open,
  91. .mmap = virtio_gpu_mmap,
  92. .poll = drm_poll,
  93. .read = drm_read,
  94. .unlocked_ioctl = drm_ioctl,
  95. .release = drm_release,
  96. .compat_ioctl = drm_compat_ioctl,
  97. .llseek = noop_llseek,
  98. };
  99. static struct drm_driver driver = {
  100. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | DRIVER_RENDER | DRIVER_ATOMIC,
  101. .load = virtio_gpu_driver_load,
  102. .unload = virtio_gpu_driver_unload,
  103. .open = virtio_gpu_driver_open,
  104. .postclose = virtio_gpu_driver_postclose,
  105. .dumb_create = virtio_gpu_mode_dumb_create,
  106. .dumb_map_offset = virtio_gpu_mode_dumb_mmap,
  107. #if defined(CONFIG_DEBUG_FS)
  108. .debugfs_init = virtio_gpu_debugfs_init,
  109. #endif
  110. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  111. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  112. .gem_prime_export = drm_gem_prime_export,
  113. .gem_prime_import = drm_gem_prime_import,
  114. .gem_prime_pin = virtgpu_gem_prime_pin,
  115. .gem_prime_unpin = virtgpu_gem_prime_unpin,
  116. .gem_prime_get_sg_table = virtgpu_gem_prime_get_sg_table,
  117. .gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
  118. .gem_prime_vmap = virtgpu_gem_prime_vmap,
  119. .gem_prime_vunmap = virtgpu_gem_prime_vunmap,
  120. .gem_prime_mmap = virtgpu_gem_prime_mmap,
  121. .gem_free_object_unlocked = virtio_gpu_gem_free_object,
  122. .gem_open_object = virtio_gpu_gem_object_open,
  123. .gem_close_object = virtio_gpu_gem_object_close,
  124. .fops = &virtio_gpu_driver_fops,
  125. .ioctls = virtio_gpu_ioctls,
  126. .num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
  127. .name = DRIVER_NAME,
  128. .desc = DRIVER_DESC,
  129. .date = DRIVER_DATE,
  130. .major = DRIVER_MAJOR,
  131. .minor = DRIVER_MINOR,
  132. .patchlevel = DRIVER_PATCHLEVEL,
  133. };