blk-pm.c 5.7 KB

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