blk-pm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/blk-mq.h>
  3. #include <linux/blk-pm.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/pm_runtime.h>
  6. #include "blk-mq.h"
  7. #include "blk-mq-tag.h"
  8. /**
  9. * blk_pm_runtime_init - Block layer runtime PM initialization routine
  10. * @q: the queue of the device
  11. * @dev: the device the queue belongs to
  12. *
  13. * Description:
  14. * Initialize runtime-PM-related fields for @q and start auto suspend for
  15. * @dev. Drivers that want to take advantage of request-based runtime PM
  16. * should call this function after @dev has been initialized, and its
  17. * request queue @q has been allocated, and runtime PM for it can not happen
  18. * yet(either due to disabled/forbidden or its usage_count > 0). In most
  19. * cases, driver should call this function before any I/O has taken place.
  20. *
  21. * This function takes care of setting up using auto suspend for the device,
  22. * the autosuspend delay is set to -1 to make runtime suspend impossible
  23. * until an updated value is either set by user or by driver. Drivers do
  24. * not need to touch other autosuspend settings.
  25. *
  26. * The block layer runtime PM is request based, so only works for drivers
  27. * that use request as their IO unit instead of those directly use bio's.
  28. */
  29. void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
  30. {
  31. q->dev = dev;
  32. q->rpm_status = RPM_ACTIVE;
  33. pm_runtime_set_autosuspend_delay(q->dev, -1);
  34. pm_runtime_use_autosuspend(q->dev);
  35. }
  36. EXPORT_SYMBOL(blk_pm_runtime_init);
  37. /**
  38. * blk_pre_runtime_suspend - Pre runtime suspend check
  39. * @q: the queue of the device
  40. *
  41. * Description:
  42. * This function will check if runtime suspend is allowed for the device
  43. * by examining if there are any requests pending in the queue. If there
  44. * are requests pending, the device can not be runtime suspended; otherwise,
  45. * the queue's status will be updated to SUSPENDING and the driver can
  46. * proceed to suspend the device.
  47. *
  48. * For the not allowed case, we mark last busy for the device so that
  49. * runtime PM core will try to autosuspend it some time later.
  50. *
  51. * This function should be called near the start of the device's
  52. * runtime_suspend callback.
  53. *
  54. * Return:
  55. * 0 - OK to runtime suspend the device
  56. * -EBUSY - Device should not be runtime suspended
  57. */
  58. int blk_pre_runtime_suspend(struct request_queue *q)
  59. {
  60. int ret = 0;
  61. if (!q->dev)
  62. return ret;
  63. WARN_ON_ONCE(q->rpm_status != RPM_ACTIVE);
  64. spin_lock_irq(&q->queue_lock);
  65. q->rpm_status = RPM_SUSPENDING;
  66. spin_unlock_irq(&q->queue_lock);
  67. /*
  68. * Increase the pm_only counter before checking whether any
  69. * non-PM blk_queue_enter() calls are in progress to avoid that any
  70. * new non-PM blk_queue_enter() calls succeed before the pm_only
  71. * counter is decreased again.
  72. */
  73. blk_set_pm_only(q);
  74. ret = -EBUSY;
  75. /* Switch q_usage_counter from per-cpu to atomic mode. */
  76. blk_freeze_queue_start(q);
  77. /*
  78. * Wait until atomic mode has been reached. Since that
  79. * involves calling call_rcu(), it is guaranteed that later
  80. * blk_queue_enter() calls see the pm-only state. See also
  81. * http://lwn.net/Articles/573497/.
  82. */
  83. percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
  84. if (percpu_ref_is_zero(&q->q_usage_counter))
  85. ret = 0;
  86. /* Switch q_usage_counter back to per-cpu mode. */
  87. blk_mq_unfreeze_queue(q);
  88. if (ret < 0) {
  89. spin_lock_irq(&q->queue_lock);
  90. q->rpm_status = RPM_ACTIVE;
  91. pm_runtime_mark_last_busy(q->dev);
  92. spin_unlock_irq(&q->queue_lock);
  93. blk_clear_pm_only(q);
  94. }
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(blk_pre_runtime_suspend);
  98. /**
  99. * blk_post_runtime_suspend - Post runtime suspend processing
  100. * @q: the queue of the device
  101. * @err: return value of the device's runtime_suspend function
  102. *
  103. * Description:
  104. * Update the queue's runtime status according to the return value of the
  105. * device's runtime suspend function and mark last busy for the device so
  106. * that PM core will try to auto suspend the device at a later time.
  107. *
  108. * This function should be called near the end of the device's
  109. * runtime_suspend callback.
  110. */
  111. void blk_post_runtime_suspend(struct request_queue *q, int err)
  112. {
  113. if (!q->dev)
  114. return;
  115. spin_lock_irq(&q->queue_lock);
  116. if (!err) {
  117. q->rpm_status = RPM_SUSPENDED;
  118. } else {
  119. q->rpm_status = RPM_ACTIVE;
  120. pm_runtime_mark_last_busy(q->dev);
  121. }
  122. spin_unlock_irq(&q->queue_lock);
  123. if (err)
  124. blk_clear_pm_only(q);
  125. }
  126. EXPORT_SYMBOL(blk_post_runtime_suspend);
  127. /**
  128. * blk_pre_runtime_resume - Pre runtime resume processing
  129. * @q: the queue of the device
  130. *
  131. * Description:
  132. * Update the queue's runtime status to RESUMING in preparation for the
  133. * runtime resume of the device.
  134. *
  135. * This function should be called near the start of the device's
  136. * runtime_resume callback.
  137. */
  138. void blk_pre_runtime_resume(struct request_queue *q)
  139. {
  140. if (!q->dev)
  141. return;
  142. spin_lock_irq(&q->queue_lock);
  143. q->rpm_status = RPM_RESUMING;
  144. spin_unlock_irq(&q->queue_lock);
  145. }
  146. EXPORT_SYMBOL(blk_pre_runtime_resume);
  147. /**
  148. * blk_post_runtime_resume - Post runtime resume processing
  149. * @q: the queue of the device
  150. *
  151. * Description:
  152. * For historical reasons, this routine merely calls blk_set_runtime_active()
  153. * to do the real work of restarting the queue. It does this regardless of
  154. * whether the device's runtime-resume succeeded; even if it failed the
  155. * driver or error handler will need to communicate with the device.
  156. *
  157. * This function should be called near the end of the device's
  158. * runtime_resume callback.
  159. */
  160. void blk_post_runtime_resume(struct request_queue *q)
  161. {
  162. blk_set_runtime_active(q);
  163. }
  164. EXPORT_SYMBOL(blk_post_runtime_resume);
  165. /**
  166. * blk_set_runtime_active - Force runtime status of the queue to be active
  167. * @q: the queue of the device
  168. *
  169. * If the device is left runtime suspended during system suspend the resume
  170. * hook typically resumes the device and corrects runtime status
  171. * accordingly. However, that does not affect the queue runtime PM status
  172. * which is still "suspended". This prevents processing requests from the
  173. * queue.
  174. *
  175. * This function can be used in driver's resume hook to correct queue
  176. * runtime PM status and re-enable peeking requests from the queue. It
  177. * should be called before first request is added to the queue.
  178. *
  179. * This function is also called by blk_post_runtime_resume() for
  180. * runtime resumes. It does everything necessary to restart the queue.
  181. */
  182. void blk_set_runtime_active(struct request_queue *q)
  183. {
  184. int old_status;
  185. if (!q->dev)
  186. return;
  187. spin_lock_irq(&q->queue_lock);
  188. old_status = q->rpm_status;
  189. q->rpm_status = RPM_ACTIVE;
  190. pm_runtime_mark_last_busy(q->dev);
  191. pm_request_autosuspend(q->dev);
  192. spin_unlock_irq(&q->queue_lock);
  193. if (old_status != RPM_ACTIVE)
  194. blk_clear_pm_only(q);
  195. }
  196. EXPORT_SYMBOL(blk_set_runtime_active);