intel_runtime_pm.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #ifndef __INTEL_RUNTIME_PM_H__
  6. #define __INTEL_RUNTIME_PM_H__
  7. #include <linux/pm_runtime.h>
  8. #include <linux/types.h>
  9. #include "intel_wakeref.h"
  10. struct device;
  11. struct drm_i915_private;
  12. struct drm_printer;
  13. struct intel_display_rpm_interface;
  14. /*
  15. * This struct helps tracking the state needed for runtime PM, which puts the
  16. * device in PCI D3 state. Notice that when this happens, nothing on the
  17. * graphics device works, even register access, so we don't get interrupts nor
  18. * anything else.
  19. *
  20. * Every piece of our code that needs to actually touch the hardware needs to
  21. * either call intel_runtime_pm_get or call intel_display_power_get with the
  22. * appropriate power domain.
  23. *
  24. * Our driver uses the autosuspend delay feature, which means we'll only really
  25. * suspend if we stay with zero refcount for a certain amount of time. The
  26. * default value is currently very conservative (see intel_runtime_pm_enable), but
  27. * it can be changed with the standard runtime PM files from sysfs.
  28. *
  29. * The irqs_disabled variable becomes true exactly after we disable the IRQs and
  30. * goes back to false exactly before we re-enable the IRQs. We use this variable
  31. * to check if someone is trying to enable/disable IRQs while they're supposed
  32. * to be disabled. This shouldn't happen and we'll print some error messages in
  33. * case it happens.
  34. *
  35. * For more, read the Documentation/power/runtime_pm.rst.
  36. */
  37. struct intel_runtime_pm {
  38. atomic_t wakeref_count;
  39. struct device *kdev; /* points to i915->drm.dev */
  40. bool available;
  41. bool no_wakeref_tracking;
  42. /*
  43. * Protects access to lmem usefault list.
  44. * It is required, if we are outside of the runtime suspend path,
  45. * access to @lmem_userfault_list requires always first grabbing the
  46. * runtime pm, to ensure we can't race against runtime suspend.
  47. * Once we have that we also need to grab @lmem_userfault_lock,
  48. * at which point we have exclusive access.
  49. * The runtime suspend path is special since it doesn't really hold any locks,
  50. * but instead has exclusive access by virtue of all other accesses requiring
  51. * holding the runtime pm wakeref.
  52. */
  53. spinlock_t lmem_userfault_lock;
  54. /*
  55. * Keep list of userfaulted gem obj, which require to release their
  56. * mmap mappings at runtime suspend path.
  57. */
  58. struct list_head lmem_userfault_list;
  59. /* Manual runtime pm autosuspend delay for user GGTT/lmem mmaps */
  60. struct intel_wakeref_auto userfault_wakeref;
  61. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
  62. /*
  63. * To aide detection of wakeref leaks and general misuse, we
  64. * track all wakeref holders. With manual markup (i.e. returning
  65. * a cookie to each rpm_get caller which they then supply to their
  66. * paired rpm_put) we can remove corresponding pairs of and keep
  67. * the array trimmed to active wakerefs.
  68. */
  69. struct ref_tracker_dir debug;
  70. #endif
  71. };
  72. #define BITS_PER_WAKEREF \
  73. BITS_PER_TYPE(typeof_member(struct intel_runtime_pm, wakeref_count))
  74. #define INTEL_RPM_WAKELOCK_SHIFT (BITS_PER_WAKEREF / 2)
  75. #define INTEL_RPM_WAKELOCK_BIAS (1 << INTEL_RPM_WAKELOCK_SHIFT)
  76. #define INTEL_RPM_RAW_WAKEREF_MASK (INTEL_RPM_WAKELOCK_BIAS - 1)
  77. static inline int
  78. intel_rpm_raw_wakeref_count(int wakeref_count)
  79. {
  80. return wakeref_count & INTEL_RPM_RAW_WAKEREF_MASK;
  81. }
  82. static inline int
  83. intel_rpm_wakelock_count(int wakeref_count)
  84. {
  85. return wakeref_count >> INTEL_RPM_WAKELOCK_SHIFT;
  86. }
  87. static inline bool
  88. intel_runtime_pm_suspended(struct intel_runtime_pm *rpm)
  89. {
  90. return pm_runtime_suspended(rpm->kdev);
  91. }
  92. static inline void
  93. assert_rpm_device_not_suspended(struct intel_runtime_pm *rpm)
  94. {
  95. WARN_ONCE(intel_runtime_pm_suspended(rpm),
  96. "Device suspended during HW access\n");
  97. }
  98. static inline void
  99. __assert_rpm_raw_wakeref_held(struct intel_runtime_pm *rpm, int wakeref_count)
  100. {
  101. assert_rpm_device_not_suspended(rpm);
  102. WARN_ONCE(!intel_rpm_raw_wakeref_count(wakeref_count),
  103. "RPM raw-wakeref not held\n");
  104. }
  105. static inline void
  106. __assert_rpm_wakelock_held(struct intel_runtime_pm *rpm, int wakeref_count)
  107. {
  108. __assert_rpm_raw_wakeref_held(rpm, wakeref_count);
  109. WARN_ONCE(!intel_rpm_wakelock_count(wakeref_count),
  110. "RPM wakelock ref not held during HW access\n");
  111. }
  112. static inline void
  113. assert_rpm_raw_wakeref_held(struct intel_runtime_pm *rpm)
  114. {
  115. __assert_rpm_raw_wakeref_held(rpm, atomic_read(&rpm->wakeref_count));
  116. }
  117. static inline void
  118. assert_rpm_wakelock_held(struct intel_runtime_pm *rpm)
  119. {
  120. __assert_rpm_wakelock_held(rpm, atomic_read(&rpm->wakeref_count));
  121. }
  122. /**
  123. * disable_rpm_wakeref_asserts - disable the RPM assert checks
  124. * @rpm: the intel_runtime_pm structure
  125. *
  126. * This function disable asserts that check if we hold an RPM wakelock
  127. * reference, while keeping the device-not-suspended checks still enabled.
  128. * It's meant to be used only in special circumstances where our rule about
  129. * the wakelock refcount wrt. the device power state doesn't hold. According
  130. * to this rule at any point where we access the HW or want to keep the HW in
  131. * an active state we must hold an RPM wakelock reference acquired via one of
  132. * the intel_runtime_pm_get() helpers. Currently there are a few special spots
  133. * where this rule doesn't hold: the IRQ and suspend/resume handlers, the
  134. * forcewake release timer, and the GPU RPS and hangcheck works. All other
  135. * users should avoid using this function.
  136. *
  137. * Any calls to this function must have a symmetric call to
  138. * enable_rpm_wakeref_asserts().
  139. */
  140. static inline void
  141. disable_rpm_wakeref_asserts(struct intel_runtime_pm *rpm)
  142. {
  143. atomic_add(INTEL_RPM_WAKELOCK_BIAS + 1,
  144. &rpm->wakeref_count);
  145. }
  146. /**
  147. * enable_rpm_wakeref_asserts - re-enable the RPM assert checks
  148. * @rpm: the intel_runtime_pm structure
  149. *
  150. * This function re-enables the RPM assert checks after disabling them with
  151. * disable_rpm_wakeref_asserts. It's meant to be used only in special
  152. * circumstances otherwise its use should be avoided.
  153. *
  154. * Any calls to this function must have a symmetric call to
  155. * disable_rpm_wakeref_asserts().
  156. */
  157. static inline void
  158. enable_rpm_wakeref_asserts(struct intel_runtime_pm *rpm)
  159. {
  160. atomic_sub(INTEL_RPM_WAKELOCK_BIAS + 1,
  161. &rpm->wakeref_count);
  162. }
  163. void intel_runtime_pm_init_early(struct intel_runtime_pm *rpm);
  164. void intel_runtime_pm_enable(struct intel_runtime_pm *rpm);
  165. void intel_runtime_pm_disable(struct intel_runtime_pm *rpm);
  166. void intel_runtime_pm_driver_release(struct intel_runtime_pm *rpm);
  167. void intel_runtime_pm_driver_last_release(struct intel_runtime_pm *rpm);
  168. intel_wakeref_t intel_runtime_pm_get(struct intel_runtime_pm *rpm);
  169. intel_wakeref_t intel_runtime_pm_get_if_in_use(struct intel_runtime_pm *rpm);
  170. intel_wakeref_t intel_runtime_pm_get_if_active(struct intel_runtime_pm *rpm);
  171. intel_wakeref_t intel_runtime_pm_get_noresume(struct intel_runtime_pm *rpm);
  172. intel_wakeref_t intel_runtime_pm_get_raw(struct intel_runtime_pm *rpm);
  173. #define with_intel_runtime_pm(rpm, wf) \
  174. for ((wf) = intel_runtime_pm_get(rpm); (wf); \
  175. intel_runtime_pm_put((rpm), (wf)), (wf) = NULL)
  176. #define with_intel_runtime_pm_if_in_use(rpm, wf) \
  177. for ((wf) = intel_runtime_pm_get_if_in_use(rpm); (wf); \
  178. intel_runtime_pm_put((rpm), (wf)), (wf) = NULL)
  179. #define with_intel_runtime_pm_if_active(rpm, wf) \
  180. for ((wf) = intel_runtime_pm_get_if_active(rpm); (wf); \
  181. intel_runtime_pm_put((rpm), (wf)), (wf) = NULL)
  182. void intel_runtime_pm_put_unchecked(struct intel_runtime_pm *rpm);
  183. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
  184. void intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref);
  185. #else
  186. static inline void
  187. intel_runtime_pm_put(struct intel_runtime_pm *rpm, intel_wakeref_t wref)
  188. {
  189. intel_runtime_pm_put_unchecked(rpm);
  190. }
  191. #endif
  192. void intel_runtime_pm_put_raw(struct intel_runtime_pm *rpm, intel_wakeref_t wref);
  193. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)
  194. void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
  195. struct drm_printer *p);
  196. #else
  197. static inline void print_intel_runtime_pm_wakeref(struct intel_runtime_pm *rpm,
  198. struct drm_printer *p)
  199. {
  200. }
  201. #endif
  202. extern const struct intel_display_rpm_interface i915_display_rpm_interface;
  203. #endif /* __INTEL_RUNTIME_PM_H__ */