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. /*
  65. * Increase the pm_only counter before checking whether any
  66. * non-PM blk_queue_enter() calls are in progress to avoid that any
  67. * new non-PM blk_queue_enter() calls succeed before the pm_only
  68. * counter is decreased again.
  69. */
  70. blk_set_pm_only(q);
  71. ret = -EBUSY;
  72. /* Switch q_usage_counter from per-cpu to atomic mode. */
  73. blk_freeze_queue_start(q);
  74. /*
  75. * Wait until atomic mode has been reached. Since that
  76. * involves calling call_rcu(), it is guaranteed that later
  77. * blk_queue_enter() calls see the pm-only state. See also
  78. * http://lwn.net/Articles/573497/.
  79. */
  80. percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
  81. if (percpu_ref_is_zero(&q->q_usage_counter))
  82. ret = 0;
  83. /* Switch q_usage_counter back to per-cpu mode. */
  84. blk_mq_unfreeze_queue(q);
  85. spin_lock_irq(q->queue_lock);
  86. if (ret < 0)
  87. pm_runtime_mark_last_busy(q->dev);
  88. else
  89. q->rpm_status = RPM_SUSPENDING;
  90. spin_unlock_irq(q->queue_lock);
  91. if (ret)
  92. blk_clear_pm_only(q);
  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. * @err: return value of the device's runtime_resume function
  149. *
  150. * Description:
  151. * Update the queue's runtime status according to the return value of the
  152. * device's runtime_resume function. If it is successfully resumed, process
  153. * the requests that are queued into the device's queue when it is resuming
  154. * and then mark last busy and initiate autosuspend for it.
  155. *
  156. * This function should be called near the end of the device's
  157. * runtime_resume callback.
  158. */
  159. void blk_post_runtime_resume(struct request_queue *q, int err)
  160. {
  161. if (!q->dev)
  162. return;
  163. spin_lock_irq(q->queue_lock);
  164. if (!err) {
  165. q->rpm_status = RPM_ACTIVE;
  166. pm_runtime_mark_last_busy(q->dev);
  167. pm_request_autosuspend(q->dev);
  168. } else {
  169. q->rpm_status = RPM_SUSPENDED;
  170. }
  171. spin_unlock_irq(q->queue_lock);
  172. if (!err)
  173. blk_clear_pm_only(q);
  174. }
  175. EXPORT_SYMBOL(blk_post_runtime_resume);
  176. /**
  177. * blk_set_runtime_active - Force runtime status of the queue to be active
  178. * @q: the queue of the device
  179. *
  180. * If the device is left runtime suspended during system suspend the resume
  181. * hook typically resumes the device and corrects runtime status
  182. * accordingly. However, that does not affect the queue runtime PM status
  183. * which is still "suspended". This prevents processing requests from the
  184. * queue.
  185. *
  186. * This function can be used in driver's resume hook to correct queue
  187. * runtime PM status and re-enable peeking requests from the queue. It
  188. * should be called before first request is added to the queue.
  189. */
  190. void blk_set_runtime_active(struct request_queue *q)
  191. {
  192. spin_lock_irq(q->queue_lock);
  193. q->rpm_status = RPM_ACTIVE;
  194. pm_runtime_mark_last_busy(q->dev);
  195. pm_request_autosuspend(q->dev);
  196. spin_unlock_irq(q->queue_lock);
  197. }
  198. EXPORT_SYMBOL(blk_set_runtime_active);