msm_fence.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2013-2016 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/dma-fence.h>
  18. #include "msm_drv.h"
  19. #include "msm_fence.h"
  20. struct msm_fence_context *
  21. msm_fence_context_alloc(struct drm_device *dev, const char *name)
  22. {
  23. struct msm_fence_context *fctx;
  24. fctx = kzalloc(sizeof(*fctx), GFP_KERNEL);
  25. if (!fctx)
  26. return ERR_PTR(-ENOMEM);
  27. fctx->dev = dev;
  28. strncpy(fctx->name, name, sizeof(fctx->name));
  29. fctx->context = dma_fence_context_alloc(1);
  30. init_waitqueue_head(&fctx->event);
  31. spin_lock_init(&fctx->spinlock);
  32. return fctx;
  33. }
  34. void msm_fence_context_free(struct msm_fence_context *fctx)
  35. {
  36. kfree(fctx);
  37. }
  38. static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fence)
  39. {
  40. return (int32_t)(fctx->completed_fence - fence) >= 0;
  41. }
  42. /* legacy path for WAIT_FENCE ioctl: */
  43. int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
  44. ktime_t *timeout, bool interruptible)
  45. {
  46. int ret;
  47. if (fence > fctx->last_fence) {
  48. DRM_ERROR("%s: waiting on invalid fence: %u (of %u)\n",
  49. fctx->name, fence, fctx->last_fence);
  50. return -EINVAL;
  51. }
  52. if (!timeout) {
  53. /* no-wait: */
  54. ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
  55. } else {
  56. unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
  57. if (interruptible)
  58. ret = wait_event_interruptible_timeout(fctx->event,
  59. fence_completed(fctx, fence),
  60. remaining_jiffies);
  61. else
  62. ret = wait_event_timeout(fctx->event,
  63. fence_completed(fctx, fence),
  64. remaining_jiffies);
  65. if (ret == 0) {
  66. DBG("timeout waiting for fence: %u (completed: %u)",
  67. fence, fctx->completed_fence);
  68. ret = -ETIMEDOUT;
  69. } else if (ret != -ERESTARTSYS) {
  70. ret = 0;
  71. }
  72. }
  73. return ret;
  74. }
  75. /* called from workqueue */
  76. void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  77. {
  78. spin_lock(&fctx->spinlock);
  79. fctx->completed_fence = max(fence, fctx->completed_fence);
  80. spin_unlock(&fctx->spinlock);
  81. wake_up_all(&fctx->event);
  82. }
  83. struct msm_fence {
  84. struct dma_fence base;
  85. struct msm_fence_context *fctx;
  86. };
  87. static inline struct msm_fence *to_msm_fence(struct dma_fence *fence)
  88. {
  89. return container_of(fence, struct msm_fence, base);
  90. }
  91. static const char *msm_fence_get_driver_name(struct dma_fence *fence)
  92. {
  93. return "msm";
  94. }
  95. static const char *msm_fence_get_timeline_name(struct dma_fence *fence)
  96. {
  97. struct msm_fence *f = to_msm_fence(fence);
  98. return f->fctx->name;
  99. }
  100. static bool msm_fence_enable_signaling(struct dma_fence *fence)
  101. {
  102. return true;
  103. }
  104. static bool msm_fence_signaled(struct dma_fence *fence)
  105. {
  106. struct msm_fence *f = to_msm_fence(fence);
  107. return fence_completed(f->fctx, f->base.seqno);
  108. }
  109. static const struct dma_fence_ops msm_fence_ops = {
  110. .get_driver_name = msm_fence_get_driver_name,
  111. .get_timeline_name = msm_fence_get_timeline_name,
  112. .enable_signaling = msm_fence_enable_signaling,
  113. .signaled = msm_fence_signaled,
  114. .wait = dma_fence_default_wait,
  115. .release = dma_fence_free,
  116. };
  117. struct dma_fence *
  118. msm_fence_alloc(struct msm_fence_context *fctx)
  119. {
  120. struct msm_fence *f;
  121. f = kzalloc(sizeof(*f), GFP_KERNEL);
  122. if (!f)
  123. return ERR_PTR(-ENOMEM);
  124. f->fctx = fctx;
  125. dma_fence_init(&f->base, &msm_fence_ops, &fctx->spinlock,
  126. fctx->context, ++fctx->last_fence);
  127. return &f->base;
  128. }