162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci#include <linux/blk-pm.h> 462306a36Sopenharmony_ci#include <linux/blkdev.h> 562306a36Sopenharmony_ci#include <linux/pm_runtime.h> 662306a36Sopenharmony_ci#include "blk-mq.h" 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/** 962306a36Sopenharmony_ci * blk_pm_runtime_init - Block layer runtime PM initialization routine 1062306a36Sopenharmony_ci * @q: the queue of the device 1162306a36Sopenharmony_ci * @dev: the device the queue belongs to 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * Description: 1462306a36Sopenharmony_ci * Initialize runtime-PM-related fields for @q and start auto suspend for 1562306a36Sopenharmony_ci * @dev. Drivers that want to take advantage of request-based runtime PM 1662306a36Sopenharmony_ci * should call this function after @dev has been initialized, and its 1762306a36Sopenharmony_ci * request queue @q has been allocated, and runtime PM for it can not happen 1862306a36Sopenharmony_ci * yet(either due to disabled/forbidden or its usage_count > 0). In most 1962306a36Sopenharmony_ci * cases, driver should call this function before any I/O has taken place. 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * This function takes care of setting up using auto suspend for the device, 2262306a36Sopenharmony_ci * the autosuspend delay is set to -1 to make runtime suspend impossible 2362306a36Sopenharmony_ci * until an updated value is either set by user or by driver. Drivers do 2462306a36Sopenharmony_ci * not need to touch other autosuspend settings. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * The block layer runtime PM is request based, so only works for drivers 2762306a36Sopenharmony_ci * that use request as their IO unit instead of those directly use bio's. 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_civoid blk_pm_runtime_init(struct request_queue *q, struct device *dev) 3062306a36Sopenharmony_ci{ 3162306a36Sopenharmony_ci q->dev = dev; 3262306a36Sopenharmony_ci q->rpm_status = RPM_ACTIVE; 3362306a36Sopenharmony_ci pm_runtime_set_autosuspend_delay(q->dev, -1); 3462306a36Sopenharmony_ci pm_runtime_use_autosuspend(q->dev); 3562306a36Sopenharmony_ci} 3662306a36Sopenharmony_ciEXPORT_SYMBOL(blk_pm_runtime_init); 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci/** 3962306a36Sopenharmony_ci * blk_pre_runtime_suspend - Pre runtime suspend check 4062306a36Sopenharmony_ci * @q: the queue of the device 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * Description: 4362306a36Sopenharmony_ci * This function will check if runtime suspend is allowed for the device 4462306a36Sopenharmony_ci * by examining if there are any requests pending in the queue. If there 4562306a36Sopenharmony_ci * are requests pending, the device can not be runtime suspended; otherwise, 4662306a36Sopenharmony_ci * the queue's status will be updated to SUSPENDING and the driver can 4762306a36Sopenharmony_ci * proceed to suspend the device. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * For the not allowed case, we mark last busy for the device so that 5062306a36Sopenharmony_ci * runtime PM core will try to autosuspend it some time later. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * This function should be called near the start of the device's 5362306a36Sopenharmony_ci * runtime_suspend callback. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * Return: 5662306a36Sopenharmony_ci * 0 - OK to runtime suspend the device 5762306a36Sopenharmony_ci * -EBUSY - Device should not be runtime suspended 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ciint blk_pre_runtime_suspend(struct request_queue *q) 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci int ret = 0; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci if (!q->dev) 6462306a36Sopenharmony_ci return ret; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci WARN_ON_ONCE(q->rpm_status != RPM_ACTIVE); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci spin_lock_irq(&q->queue_lock); 6962306a36Sopenharmony_ci q->rpm_status = RPM_SUSPENDING; 7062306a36Sopenharmony_ci spin_unlock_irq(&q->queue_lock); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci /* 7362306a36Sopenharmony_ci * Increase the pm_only counter before checking whether any 7462306a36Sopenharmony_ci * non-PM blk_queue_enter() calls are in progress to avoid that any 7562306a36Sopenharmony_ci * new non-PM blk_queue_enter() calls succeed before the pm_only 7662306a36Sopenharmony_ci * counter is decreased again. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci blk_set_pm_only(q); 7962306a36Sopenharmony_ci ret = -EBUSY; 8062306a36Sopenharmony_ci /* Switch q_usage_counter from per-cpu to atomic mode. */ 8162306a36Sopenharmony_ci blk_freeze_queue_start(q); 8262306a36Sopenharmony_ci /* 8362306a36Sopenharmony_ci * Wait until atomic mode has been reached. Since that 8462306a36Sopenharmony_ci * involves calling call_rcu(), it is guaranteed that later 8562306a36Sopenharmony_ci * blk_queue_enter() calls see the pm-only state. See also 8662306a36Sopenharmony_ci * http://lwn.net/Articles/573497/. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_ci percpu_ref_switch_to_atomic_sync(&q->q_usage_counter); 8962306a36Sopenharmony_ci if (percpu_ref_is_zero(&q->q_usage_counter)) 9062306a36Sopenharmony_ci ret = 0; 9162306a36Sopenharmony_ci /* Switch q_usage_counter back to per-cpu mode. */ 9262306a36Sopenharmony_ci blk_mq_unfreeze_queue(q); 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci if (ret < 0) { 9562306a36Sopenharmony_ci spin_lock_irq(&q->queue_lock); 9662306a36Sopenharmony_ci q->rpm_status = RPM_ACTIVE; 9762306a36Sopenharmony_ci pm_runtime_mark_last_busy(q->dev); 9862306a36Sopenharmony_ci spin_unlock_irq(&q->queue_lock); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci blk_clear_pm_only(q); 10162306a36Sopenharmony_ci } 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci return ret; 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ciEXPORT_SYMBOL(blk_pre_runtime_suspend); 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/** 10862306a36Sopenharmony_ci * blk_post_runtime_suspend - Post runtime suspend processing 10962306a36Sopenharmony_ci * @q: the queue of the device 11062306a36Sopenharmony_ci * @err: return value of the device's runtime_suspend function 11162306a36Sopenharmony_ci * 11262306a36Sopenharmony_ci * Description: 11362306a36Sopenharmony_ci * Update the queue's runtime status according to the return value of the 11462306a36Sopenharmony_ci * device's runtime suspend function and mark last busy for the device so 11562306a36Sopenharmony_ci * that PM core will try to auto suspend the device at a later time. 11662306a36Sopenharmony_ci * 11762306a36Sopenharmony_ci * This function should be called near the end of the device's 11862306a36Sopenharmony_ci * runtime_suspend callback. 11962306a36Sopenharmony_ci */ 12062306a36Sopenharmony_civoid blk_post_runtime_suspend(struct request_queue *q, int err) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci if (!q->dev) 12362306a36Sopenharmony_ci return; 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci spin_lock_irq(&q->queue_lock); 12662306a36Sopenharmony_ci if (!err) { 12762306a36Sopenharmony_ci q->rpm_status = RPM_SUSPENDED; 12862306a36Sopenharmony_ci } else { 12962306a36Sopenharmony_ci q->rpm_status = RPM_ACTIVE; 13062306a36Sopenharmony_ci pm_runtime_mark_last_busy(q->dev); 13162306a36Sopenharmony_ci } 13262306a36Sopenharmony_ci spin_unlock_irq(&q->queue_lock); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci if (err) 13562306a36Sopenharmony_ci blk_clear_pm_only(q); 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ciEXPORT_SYMBOL(blk_post_runtime_suspend); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/** 14062306a36Sopenharmony_ci * blk_pre_runtime_resume - Pre runtime resume processing 14162306a36Sopenharmony_ci * @q: the queue of the device 14262306a36Sopenharmony_ci * 14362306a36Sopenharmony_ci * Description: 14462306a36Sopenharmony_ci * Update the queue's runtime status to RESUMING in preparation for the 14562306a36Sopenharmony_ci * runtime resume of the device. 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * This function should be called near the start of the device's 14862306a36Sopenharmony_ci * runtime_resume callback. 14962306a36Sopenharmony_ci */ 15062306a36Sopenharmony_civoid blk_pre_runtime_resume(struct request_queue *q) 15162306a36Sopenharmony_ci{ 15262306a36Sopenharmony_ci if (!q->dev) 15362306a36Sopenharmony_ci return; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci spin_lock_irq(&q->queue_lock); 15662306a36Sopenharmony_ci q->rpm_status = RPM_RESUMING; 15762306a36Sopenharmony_ci spin_unlock_irq(&q->queue_lock); 15862306a36Sopenharmony_ci} 15962306a36Sopenharmony_ciEXPORT_SYMBOL(blk_pre_runtime_resume); 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/** 16262306a36Sopenharmony_ci * blk_post_runtime_resume - Post runtime resume processing 16362306a36Sopenharmony_ci * @q: the queue of the device 16462306a36Sopenharmony_ci * 16562306a36Sopenharmony_ci * Description: 16662306a36Sopenharmony_ci * For historical reasons, this routine merely calls blk_set_runtime_active() 16762306a36Sopenharmony_ci * to do the real work of restarting the queue. It does this regardless of 16862306a36Sopenharmony_ci * whether the device's runtime-resume succeeded; even if it failed the 16962306a36Sopenharmony_ci * driver or error handler will need to communicate with the device. 17062306a36Sopenharmony_ci * 17162306a36Sopenharmony_ci * This function should be called near the end of the device's 17262306a36Sopenharmony_ci * runtime_resume callback. 17362306a36Sopenharmony_ci */ 17462306a36Sopenharmony_civoid blk_post_runtime_resume(struct request_queue *q) 17562306a36Sopenharmony_ci{ 17662306a36Sopenharmony_ci blk_set_runtime_active(q); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ciEXPORT_SYMBOL(blk_post_runtime_resume); 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci/** 18162306a36Sopenharmony_ci * blk_set_runtime_active - Force runtime status of the queue to be active 18262306a36Sopenharmony_ci * @q: the queue of the device 18362306a36Sopenharmony_ci * 18462306a36Sopenharmony_ci * If the device is left runtime suspended during system suspend the resume 18562306a36Sopenharmony_ci * hook typically resumes the device and corrects runtime status 18662306a36Sopenharmony_ci * accordingly. However, that does not affect the queue runtime PM status 18762306a36Sopenharmony_ci * which is still "suspended". This prevents processing requests from the 18862306a36Sopenharmony_ci * queue. 18962306a36Sopenharmony_ci * 19062306a36Sopenharmony_ci * This function can be used in driver's resume hook to correct queue 19162306a36Sopenharmony_ci * runtime PM status and re-enable peeking requests from the queue. It 19262306a36Sopenharmony_ci * should be called before first request is added to the queue. 19362306a36Sopenharmony_ci * 19462306a36Sopenharmony_ci * This function is also called by blk_post_runtime_resume() for 19562306a36Sopenharmony_ci * runtime resumes. It does everything necessary to restart the queue. 19662306a36Sopenharmony_ci */ 19762306a36Sopenharmony_civoid blk_set_runtime_active(struct request_queue *q) 19862306a36Sopenharmony_ci{ 19962306a36Sopenharmony_ci int old_status; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci if (!q->dev) 20262306a36Sopenharmony_ci return; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci spin_lock_irq(&q->queue_lock); 20562306a36Sopenharmony_ci old_status = q->rpm_status; 20662306a36Sopenharmony_ci q->rpm_status = RPM_ACTIVE; 20762306a36Sopenharmony_ci pm_runtime_mark_last_busy(q->dev); 20862306a36Sopenharmony_ci pm_request_autosuspend(q->dev); 20962306a36Sopenharmony_ci spin_unlock_irq(&q->queue_lock); 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci if (old_status != RPM_ACTIVE) 21262306a36Sopenharmony_ci blk_clear_pm_only(q); 21362306a36Sopenharmony_ci} 21462306a36Sopenharmony_ciEXPORT_SYMBOL(blk_set_runtime_active); 215