virtgpu_fence.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <drm/drmP.h>
  26. #include "virtgpu_drv.h"
  27. static const char *virtio_get_driver_name(struct dma_fence *f)
  28. {
  29. return "virtio_gpu";
  30. }
  31. static const char *virtio_get_timeline_name(struct dma_fence *f)
  32. {
  33. return "controlq";
  34. }
  35. static bool virtio_enable_signaling(struct dma_fence *f)
  36. {
  37. return true;
  38. }
  39. static bool virtio_signaled(struct dma_fence *f)
  40. {
  41. struct virtio_gpu_fence *fence = to_virtio_fence(f);
  42. if (atomic64_read(&fence->drv->last_seq) >= fence->seq)
  43. return true;
  44. return false;
  45. }
  46. static void virtio_fence_value_str(struct dma_fence *f, char *str, int size)
  47. {
  48. struct virtio_gpu_fence *fence = to_virtio_fence(f);
  49. snprintf(str, size, "%llu", fence->seq);
  50. }
  51. static void virtio_timeline_value_str(struct dma_fence *f, char *str, int size)
  52. {
  53. struct virtio_gpu_fence *fence = to_virtio_fence(f);
  54. snprintf(str, size, "%llu", (u64)atomic64_read(&fence->drv->last_seq));
  55. }
  56. static const struct dma_fence_ops virtio_fence_ops = {
  57. .get_driver_name = virtio_get_driver_name,
  58. .get_timeline_name = virtio_get_timeline_name,
  59. .enable_signaling = virtio_enable_signaling,
  60. .signaled = virtio_signaled,
  61. .wait = dma_fence_default_wait,
  62. .fence_value_str = virtio_fence_value_str,
  63. .timeline_value_str = virtio_timeline_value_str,
  64. };
  65. int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,
  66. struct virtio_gpu_ctrl_hdr *cmd_hdr,
  67. struct virtio_gpu_fence **fence)
  68. {
  69. struct virtio_gpu_fence_driver *drv = &vgdev->fence_drv;
  70. unsigned long irq_flags;
  71. *fence = kmalloc(sizeof(struct virtio_gpu_fence), GFP_ATOMIC);
  72. if ((*fence) == NULL)
  73. return -ENOMEM;
  74. spin_lock_irqsave(&drv->lock, irq_flags);
  75. (*fence)->drv = drv;
  76. (*fence)->seq = ++drv->sync_seq;
  77. dma_fence_init(&(*fence)->f, &virtio_fence_ops, &drv->lock,
  78. drv->context, (*fence)->seq);
  79. dma_fence_get(&(*fence)->f);
  80. list_add_tail(&(*fence)->node, &drv->fences);
  81. spin_unlock_irqrestore(&drv->lock, irq_flags);
  82. cmd_hdr->flags |= cpu_to_le32(VIRTIO_GPU_FLAG_FENCE);
  83. cmd_hdr->fence_id = cpu_to_le64((*fence)->seq);
  84. return 0;
  85. }
  86. void virtio_gpu_fence_event_process(struct virtio_gpu_device *vgdev,
  87. u64 last_seq)
  88. {
  89. struct virtio_gpu_fence_driver *drv = &vgdev->fence_drv;
  90. struct virtio_gpu_fence *fence, *tmp;
  91. unsigned long irq_flags;
  92. spin_lock_irqsave(&drv->lock, irq_flags);
  93. atomic64_set(&vgdev->fence_drv.last_seq, last_seq);
  94. list_for_each_entry_safe(fence, tmp, &drv->fences, node) {
  95. if (last_seq < fence->seq)
  96. continue;
  97. dma_fence_signal_locked(&fence->f);
  98. list_del(&fence->node);
  99. dma_fence_put(&fence->f);
  100. }
  101. spin_unlock_irqrestore(&drv->lock, irq_flags);
  102. }