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