msm_fence.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013-2016 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. #include <linux/dma-fence.h>
  7. #include "msm_drv.h"
  8. #include "msm_fence.h"
  9. #include "msm_gpu.h"
  10. static struct msm_gpu *fctx2gpu(struct msm_fence_context *fctx)
  11. {
  12. struct msm_drm_private *priv = fctx->dev->dev_private;
  13. return priv->gpu;
  14. }
  15. static enum hrtimer_restart deadline_timer(struct hrtimer *t)
  16. {
  17. struct msm_fence_context *fctx = container_of(t,
  18. struct msm_fence_context, deadline_timer);
  19. kthread_queue_work(fctx2gpu(fctx)->worker, &fctx->deadline_work);
  20. return HRTIMER_NORESTART;
  21. }
  22. static void deadline_work(struct kthread_work *work)
  23. {
  24. struct msm_fence_context *fctx = container_of(work,
  25. struct msm_fence_context, deadline_work);
  26. /* If deadline fence has already passed, nothing to do: */
  27. if (msm_fence_completed(fctx, fctx->next_deadline_fence))
  28. return;
  29. msm_devfreq_boost(fctx2gpu(fctx), 2);
  30. }
  31. struct msm_fence_context *
  32. msm_fence_context_alloc(struct drm_device *dev, volatile uint32_t *fenceptr,
  33. const char *name)
  34. {
  35. struct msm_fence_context *fctx;
  36. static int index = 0;
  37. fctx = kzalloc_obj(*fctx);
  38. if (!fctx)
  39. return ERR_PTR(-ENOMEM);
  40. fctx->dev = dev;
  41. strscpy(fctx->name, name, sizeof(fctx->name));
  42. fctx->context = dma_fence_context_alloc(1);
  43. fctx->index = index++;
  44. fctx->fenceptr = fenceptr;
  45. spin_lock_init(&fctx->spinlock);
  46. /*
  47. * Start out close to the 32b fence rollover point, so we can
  48. * catch bugs with fence comparisons.
  49. */
  50. fctx->last_fence = 0xffffff00;
  51. fctx->completed_fence = fctx->last_fence;
  52. *fctx->fenceptr = fctx->last_fence;
  53. hrtimer_setup(&fctx->deadline_timer, deadline_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  54. kthread_init_work(&fctx->deadline_work, deadline_work);
  55. fctx->next_deadline = ktime_get();
  56. return fctx;
  57. }
  58. void msm_fence_context_free(struct msm_fence_context *fctx)
  59. {
  60. kfree(fctx);
  61. }
  62. bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence)
  63. {
  64. /*
  65. * Note: Check completed_fence first, as fenceptr is in a write-combine
  66. * mapping, so it will be more expensive to read.
  67. */
  68. return (int32_t)(fctx->completed_fence - fence) >= 0 ||
  69. (int32_t)(*fctx->fenceptr - fence) >= 0;
  70. }
  71. /* called from irq handler and workqueue (in recover path) */
  72. void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&fctx->spinlock, flags);
  76. if (fence_after(fence, fctx->completed_fence))
  77. fctx->completed_fence = fence;
  78. if (msm_fence_completed(fctx, fctx->next_deadline_fence))
  79. hrtimer_cancel(&fctx->deadline_timer);
  80. spin_unlock_irqrestore(&fctx->spinlock, flags);
  81. }
  82. struct msm_fence {
  83. struct dma_fence base;
  84. struct msm_fence_context *fctx;
  85. };
  86. static inline struct msm_fence *to_msm_fence(struct dma_fence *fence)
  87. {
  88. return container_of(fence, struct msm_fence, base);
  89. }
  90. static const char *msm_fence_get_driver_name(struct dma_fence *fence)
  91. {
  92. return "msm";
  93. }
  94. static const char *msm_fence_get_timeline_name(struct dma_fence *fence)
  95. {
  96. struct msm_fence *f = to_msm_fence(fence);
  97. return f->fctx->name;
  98. }
  99. static bool msm_fence_signaled(struct dma_fence *fence)
  100. {
  101. struct msm_fence *f = to_msm_fence(fence);
  102. return msm_fence_completed(f->fctx, f->base.seqno);
  103. }
  104. static void msm_fence_set_deadline(struct dma_fence *fence, ktime_t deadline)
  105. {
  106. struct msm_fence *f = to_msm_fence(fence);
  107. struct msm_fence_context *fctx = f->fctx;
  108. unsigned long flags;
  109. ktime_t now;
  110. spin_lock_irqsave(&fctx->spinlock, flags);
  111. now = ktime_get();
  112. if (ktime_after(now, fctx->next_deadline) ||
  113. ktime_before(deadline, fctx->next_deadline)) {
  114. fctx->next_deadline = deadline;
  115. fctx->next_deadline_fence =
  116. max(fctx->next_deadline_fence, (uint32_t)fence->seqno);
  117. /*
  118. * Set timer to trigger boost 3ms before deadline, or
  119. * if we are already less than 3ms before the deadline
  120. * schedule boost work immediately.
  121. */
  122. deadline = ktime_sub(deadline, ms_to_ktime(3));
  123. if (ktime_after(now, deadline)) {
  124. kthread_queue_work(fctx2gpu(fctx)->worker,
  125. &fctx->deadline_work);
  126. } else {
  127. hrtimer_start(&fctx->deadline_timer, deadline,
  128. HRTIMER_MODE_ABS);
  129. }
  130. }
  131. spin_unlock_irqrestore(&fctx->spinlock, flags);
  132. }
  133. static const struct dma_fence_ops msm_fence_ops = {
  134. .get_driver_name = msm_fence_get_driver_name,
  135. .get_timeline_name = msm_fence_get_timeline_name,
  136. .signaled = msm_fence_signaled,
  137. .set_deadline = msm_fence_set_deadline,
  138. };
  139. struct dma_fence *
  140. msm_fence_alloc(void)
  141. {
  142. struct msm_fence *f;
  143. f = kzalloc_obj(*f);
  144. if (!f)
  145. return ERR_PTR(-ENOMEM);
  146. return &f->base;
  147. }
  148. void
  149. msm_fence_init(struct dma_fence *fence, struct msm_fence_context *fctx)
  150. {
  151. struct msm_fence *f = to_msm_fence(fence);
  152. f->fctx = fctx;
  153. /*
  154. * Until this point, the fence was just some pre-allocated memory,
  155. * no-one should have taken a reference to it yet.
  156. */
  157. WARN_ON(kref_read(&fence->refcount));
  158. dma_fence_init(&f->base, &msm_fence_ops, &fctx->spinlock,
  159. fctx->context, ++fctx->last_fence);
  160. }