virtgpu_kms.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2015 Red Hat, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/virtio.h>
  26. #include <linux/virtio_config.h>
  27. #include <drm/drmP.h>
  28. #include "virtgpu_drv.h"
  29. static int virtio_gpu_fbdev = 1;
  30. MODULE_PARM_DESC(fbdev, "Disable/Enable framebuffer device & console");
  31. module_param_named(fbdev, virtio_gpu_fbdev, int, 0400);
  32. static void virtio_gpu_config_changed_work_func(struct work_struct *work)
  33. {
  34. struct virtio_gpu_device *vgdev =
  35. container_of(work, struct virtio_gpu_device,
  36. config_changed_work);
  37. u32 events_read, events_clear = 0;
  38. /* read the config space */
  39. virtio_cread(vgdev->vdev, struct virtio_gpu_config,
  40. events_read, &events_read);
  41. if (events_read & VIRTIO_GPU_EVENT_DISPLAY) {
  42. virtio_gpu_cmd_get_display_info(vgdev);
  43. drm_helper_hpd_irq_event(vgdev->ddev);
  44. events_clear |= VIRTIO_GPU_EVENT_DISPLAY;
  45. }
  46. virtio_cwrite(vgdev->vdev, struct virtio_gpu_config,
  47. events_clear, &events_clear);
  48. }
  49. static void virtio_gpu_init_vq(struct virtio_gpu_queue *vgvq,
  50. void (*work_func)(struct work_struct *work))
  51. {
  52. spin_lock_init(&vgvq->qlock);
  53. init_waitqueue_head(&vgvq->ack_queue);
  54. INIT_WORK(&vgvq->dequeue_work, work_func);
  55. }
  56. int virtio_gpu_driver_load(struct drm_device *dev, unsigned long flags)
  57. {
  58. static vq_callback_t *callbacks[] = {
  59. virtio_gpu_ctrl_ack, virtio_gpu_cursor_ack
  60. };
  61. static const char *names[] = { "control", "cursor" };
  62. struct virtio_gpu_device *vgdev;
  63. /* this will expand later */
  64. struct virtqueue *vqs[2];
  65. u32 num_scanouts;
  66. int ret;
  67. if (!virtio_has_feature(dev->virtdev, VIRTIO_F_VERSION_1))
  68. return -ENODEV;
  69. vgdev = kzalloc(sizeof(struct virtio_gpu_device), GFP_KERNEL);
  70. if (!vgdev)
  71. return -ENOMEM;
  72. vgdev->ddev = dev;
  73. dev->dev_private = vgdev;
  74. vgdev->vdev = dev->virtdev;
  75. vgdev->dev = dev->dev;
  76. spin_lock_init(&vgdev->display_info_lock);
  77. spin_lock_init(&vgdev->ctx_id_idr_lock);
  78. idr_init(&vgdev->ctx_id_idr);
  79. spin_lock_init(&vgdev->resource_idr_lock);
  80. idr_init(&vgdev->resource_idr);
  81. init_waitqueue_head(&vgdev->resp_wq);
  82. virtio_gpu_init_vq(&vgdev->ctrlq, virtio_gpu_dequeue_ctrl_func);
  83. virtio_gpu_init_vq(&vgdev->cursorq, virtio_gpu_dequeue_cursor_func);
  84. spin_lock_init(&vgdev->fence_drv.lock);
  85. INIT_LIST_HEAD(&vgdev->fence_drv.fences);
  86. INIT_WORK(&vgdev->config_changed_work,
  87. virtio_gpu_config_changed_work_func);
  88. ret = vgdev->vdev->config->find_vqs(vgdev->vdev, 2, vqs,
  89. callbacks, names);
  90. if (ret) {
  91. DRM_ERROR("failed to find virt queues\n");
  92. goto err_vqs;
  93. }
  94. vgdev->ctrlq.vq = vqs[0];
  95. vgdev->cursorq.vq = vqs[1];
  96. ret = virtio_gpu_alloc_vbufs(vgdev);
  97. if (ret) {
  98. DRM_ERROR("failed to alloc vbufs\n");
  99. goto err_vbufs;
  100. }
  101. ret = virtio_gpu_ttm_init(vgdev);
  102. if (ret) {
  103. DRM_ERROR("failed to init ttm %d\n", ret);
  104. goto err_ttm;
  105. }
  106. /* get display info */
  107. virtio_cread(vgdev->vdev, struct virtio_gpu_config,
  108. num_scanouts, &num_scanouts);
  109. vgdev->num_scanouts = min_t(uint32_t, num_scanouts,
  110. VIRTIO_GPU_MAX_SCANOUTS);
  111. if (!vgdev->num_scanouts) {
  112. DRM_ERROR("num_scanouts is zero\n");
  113. ret = -EINVAL;
  114. goto err_scanouts;
  115. }
  116. ret = virtio_gpu_modeset_init(vgdev);
  117. if (ret)
  118. goto err_modeset;
  119. virtio_device_ready(vgdev->vdev);
  120. vgdev->vqs_ready = true;
  121. virtio_gpu_cmd_get_display_info(vgdev);
  122. wait_event_timeout(vgdev->resp_wq, !vgdev->display_info_pending,
  123. 5 * HZ);
  124. if (virtio_gpu_fbdev)
  125. virtio_gpu_fbdev_init(vgdev);
  126. return 0;
  127. err_modeset:
  128. err_scanouts:
  129. virtio_gpu_ttm_fini(vgdev);
  130. err_ttm:
  131. virtio_gpu_free_vbufs(vgdev);
  132. err_vbufs:
  133. vgdev->vdev->config->del_vqs(vgdev->vdev);
  134. err_vqs:
  135. kfree(vgdev);
  136. return ret;
  137. }
  138. int virtio_gpu_driver_unload(struct drm_device *dev)
  139. {
  140. struct virtio_gpu_device *vgdev = dev->dev_private;
  141. vgdev->vqs_ready = false;
  142. flush_work(&vgdev->ctrlq.dequeue_work);
  143. flush_work(&vgdev->cursorq.dequeue_work);
  144. flush_work(&vgdev->config_changed_work);
  145. vgdev->vdev->config->del_vqs(vgdev->vdev);
  146. virtio_gpu_modeset_fini(vgdev);
  147. virtio_gpu_ttm_fini(vgdev);
  148. virtio_gpu_free_vbufs(vgdev);
  149. kfree(vgdev);
  150. return 0;
  151. }