intel_wakeref.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2019 Intel Corporation
  5. */
  6. #include <linux/wait_bit.h>
  7. #include "intel_runtime_pm.h"
  8. #include "intel_wakeref.h"
  9. #include "i915_drv.h"
  10. int __intel_wakeref_get_first(struct intel_wakeref *wf)
  11. {
  12. intel_wakeref_t wakeref;
  13. int ret = 0;
  14. wakeref = intel_runtime_pm_get(&wf->i915->runtime_pm);
  15. /*
  16. * Treat get/put as different subclasses, as we may need to run
  17. * the put callback from under the shrinker and do not want to
  18. * cross-contanimate that callback with any extra work performed
  19. * upon acquiring the wakeref.
  20. */
  21. mutex_lock_nested(&wf->mutex, SINGLE_DEPTH_NESTING);
  22. if (!atomic_read(&wf->count)) {
  23. INTEL_WAKEREF_BUG_ON(wf->wakeref);
  24. wf->wakeref = wakeref;
  25. wakeref = 0;
  26. ret = wf->ops->get(wf);
  27. if (ret) {
  28. wakeref = xchg(&wf->wakeref, 0);
  29. wake_up_var(&wf->wakeref);
  30. goto unlock;
  31. }
  32. smp_mb__before_atomic(); /* release wf->count */
  33. }
  34. atomic_inc(&wf->count);
  35. INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
  36. unlock:
  37. mutex_unlock(&wf->mutex);
  38. if (unlikely(wakeref))
  39. intel_runtime_pm_put(&wf->i915->runtime_pm, wakeref);
  40. return ret;
  41. }
  42. static void ____intel_wakeref_put_last(struct intel_wakeref *wf)
  43. {
  44. intel_wakeref_t wakeref = 0;
  45. INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
  46. if (unlikely(!atomic_dec_and_test(&wf->count)))
  47. goto unlock;
  48. /* ops->put() must reschedule its own release on error/deferral */
  49. if (likely(!wf->ops->put(wf))) {
  50. INTEL_WAKEREF_BUG_ON(!wf->wakeref);
  51. wakeref = xchg(&wf->wakeref, 0);
  52. wake_up_var(&wf->wakeref);
  53. }
  54. unlock:
  55. mutex_unlock(&wf->mutex);
  56. if (wakeref)
  57. intel_runtime_pm_put(&wf->i915->runtime_pm, wakeref);
  58. }
  59. void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags)
  60. {
  61. INTEL_WAKEREF_BUG_ON(delayed_work_pending(&wf->work));
  62. /* Assume we are not in process context and so cannot sleep. */
  63. if (flags & INTEL_WAKEREF_PUT_ASYNC || !mutex_trylock(&wf->mutex)) {
  64. mod_delayed_work(wf->i915->unordered_wq, &wf->work,
  65. FIELD_GET(INTEL_WAKEREF_PUT_DELAY, flags));
  66. return;
  67. }
  68. ____intel_wakeref_put_last(wf);
  69. }
  70. static void __intel_wakeref_put_work(struct work_struct *wrk)
  71. {
  72. struct intel_wakeref *wf = container_of(wrk, typeof(*wf), work.work);
  73. if (atomic_add_unless(&wf->count, -1, 1))
  74. return;
  75. mutex_lock(&wf->mutex);
  76. ____intel_wakeref_put_last(wf);
  77. }
  78. void __intel_wakeref_init(struct intel_wakeref *wf,
  79. struct drm_i915_private *i915,
  80. const struct intel_wakeref_ops *ops,
  81. struct intel_wakeref_lockclass *key,
  82. const char *name)
  83. {
  84. wf->i915 = i915;
  85. wf->ops = ops;
  86. __mutex_init(&wf->mutex, "wakeref.mutex", &key->mutex);
  87. atomic_set(&wf->count, 0);
  88. wf->wakeref = 0;
  89. INIT_DELAYED_WORK(&wf->work, __intel_wakeref_put_work);
  90. lockdep_init_map(&wf->work.work.lockdep_map,
  91. "wakeref.work", &key->work, 0);
  92. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_WAKEREF)
  93. ref_tracker_dir_init(&wf->debug, INTEL_REFTRACK_DEAD_COUNT, name);
  94. #endif
  95. }
  96. int intel_wakeref_wait_for_idle(struct intel_wakeref *wf)
  97. {
  98. int err;
  99. might_sleep();
  100. err = wait_var_event_killable(&wf->wakeref,
  101. !intel_wakeref_is_active(wf));
  102. if (err)
  103. return err;
  104. intel_wakeref_unlock_wait(wf);
  105. return 0;
  106. }
  107. static void wakeref_auto_timeout(struct timer_list *t)
  108. {
  109. struct intel_wakeref_auto *wf = from_timer(wf, t, timer);
  110. intel_wakeref_t wakeref;
  111. unsigned long flags;
  112. if (!refcount_dec_and_lock_irqsave(&wf->count, &wf->lock, &flags))
  113. return;
  114. wakeref = fetch_and_zero(&wf->wakeref);
  115. spin_unlock_irqrestore(&wf->lock, flags);
  116. intel_runtime_pm_put(&wf->i915->runtime_pm, wakeref);
  117. }
  118. void intel_wakeref_auto_init(struct intel_wakeref_auto *wf,
  119. struct drm_i915_private *i915)
  120. {
  121. spin_lock_init(&wf->lock);
  122. timer_setup(&wf->timer, wakeref_auto_timeout, 0);
  123. refcount_set(&wf->count, 0);
  124. wf->wakeref = 0;
  125. wf->i915 = i915;
  126. }
  127. void intel_wakeref_auto(struct intel_wakeref_auto *wf, unsigned long timeout)
  128. {
  129. unsigned long flags;
  130. if (!timeout) {
  131. if (del_timer_sync(&wf->timer))
  132. wakeref_auto_timeout(&wf->timer);
  133. return;
  134. }
  135. /* Our mission is that we only extend an already active wakeref */
  136. assert_rpm_wakelock_held(&wf->i915->runtime_pm);
  137. if (!refcount_inc_not_zero(&wf->count)) {
  138. spin_lock_irqsave(&wf->lock, flags);
  139. if (!refcount_inc_not_zero(&wf->count)) {
  140. INTEL_WAKEREF_BUG_ON(wf->wakeref);
  141. wf->wakeref =
  142. intel_runtime_pm_get_if_in_use(&wf->i915->runtime_pm);
  143. refcount_set(&wf->count, 1);
  144. }
  145. spin_unlock_irqrestore(&wf->lock, flags);
  146. }
  147. /*
  148. * If we extend a pending timer, we will only get a single timer
  149. * callback and so need to cancel the local inc by running the
  150. * elided callback to keep the wf->count balanced.
  151. */
  152. if (mod_timer(&wf->timer, jiffies + timeout))
  153. wakeref_auto_timeout(&wf->timer);
  154. }
  155. void intel_wakeref_auto_fini(struct intel_wakeref_auto *wf)
  156. {
  157. intel_wakeref_auto(wf, 0);
  158. INTEL_WAKEREF_BUG_ON(wf->wakeref);
  159. }
  160. void intel_ref_tracker_show(struct ref_tracker_dir *dir,
  161. struct drm_printer *p)
  162. {
  163. const size_t buf_size = PAGE_SIZE;
  164. char *buf, *sb, *se;
  165. size_t count;
  166. buf = kmalloc(buf_size, GFP_NOWAIT);
  167. if (!buf)
  168. return;
  169. count = ref_tracker_dir_snprint(dir, buf, buf_size);
  170. if (!count)
  171. goto free;
  172. /* printk does not like big buffers, so we split it */
  173. for (sb = buf; *sb; sb = se + 1) {
  174. se = strchrnul(sb, '\n');
  175. drm_printf(p, "%.*s", (int)(se - sb + 1), sb);
  176. if (!*se)
  177. break;
  178. }
  179. if (count >= buf_size)
  180. drm_printf(p, "\n...dropped %zd extra bytes of leak report.\n",
  181. count + 1 - buf_size);
  182. free:
  183. kfree(buf);
  184. }