// SPDX-License-Identifier: GPL-2.012#include <linux/blk-pm.h>3#include <linux/blkdev.h>4#include <linux/pm_runtime.h>5#include "blk-mq.h"67/**8* blk_pm_runtime_init - Block layer runtime PM initialization routine9* @q: the queue of the device10* @dev: the device the queue belongs to11*12* Description:13* Initialize runtime-PM-related fields for @q and start auto suspend for14* @dev. Drivers that want to take advantage of request-based runtime PM15* should call this function after @dev has been initialized, and its16* request queue @q has been allocated, and runtime PM for it can not happen17* yet(either due to disabled/forbidden or its usage_count > 0). In most18* cases, driver should call this function before any I/O has taken place.19*20* This function takes care of setting up using auto suspend for the device,21* the autosuspend delay is set to -1 to make runtime suspend impossible22* until an updated value is either set by user or by driver. Drivers do23* not need to touch other autosuspend settings.24*25* The block layer runtime PM is request based, so only works for drivers26* that use request as their IO unit instead of those directly use bio's.27*/28void blk_pm_runtime_init(struct request_queue *q, struct device *dev)29{30q->dev = dev;31q->rpm_status = RPM_ACTIVE;32pm_runtime_set_autosuspend_delay(q->dev, -1);33pm_runtime_use_autosuspend(q->dev);34}35EXPORT_SYMBOL(blk_pm_runtime_init);3637/**38* blk_pre_runtime_suspend - Pre runtime suspend check39* @q: the queue of the device40*41* Description:42* This function will check if runtime suspend is allowed for the device43* by examining if there are any requests pending in the queue. If there44* are requests pending, the device can not be runtime suspended; otherwise,45* the queue's status will be updated to SUSPENDING and the driver can46* proceed to suspend the device.47*48* For the not allowed case, we mark last busy for the device so that49* runtime PM core will try to autosuspend it some time later.50*51* This function should be called near the start of the device's52* runtime_suspend callback.53*54* Return:55* 0 - OK to runtime suspend the device56* -EBUSY - Device should not be runtime suspended57*/58int blk_pre_runtime_suspend(struct request_queue *q)59{60int ret = 0;6162if (!q->dev)63return ret;6465WARN_ON_ONCE(q->rpm_status != RPM_ACTIVE);6667spin_lock_irq(&q->queue_lock);68q->rpm_status = RPM_SUSPENDING;69spin_unlock_irq(&q->queue_lock);7071/*72* Increase the pm_only counter before checking whether any73* non-PM blk_queue_enter() calls are in progress to avoid that any74* new non-PM blk_queue_enter() calls succeed before the pm_only75* counter is decreased again.76*/77blk_set_pm_only(q);78ret = -EBUSY;79/* Switch q_usage_counter from per-cpu to atomic mode. */80blk_freeze_queue_start(q);81/*82* Wait until atomic mode has been reached. Since that83* involves calling call_rcu(), it is guaranteed that later84* blk_queue_enter() calls see the pm-only state. See also85* http://lwn.net/Articles/573497/.86*/87percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);88if (percpu_ref_is_zero(&q->q_usage_counter))89ret = 0;90/* Switch q_usage_counter back to per-cpu mode. */91blk_mq_unfreeze_queue_nomemrestore(q);9293if (ret < 0) {94spin_lock_irq(&q->queue_lock);95q->rpm_status = RPM_ACTIVE;96pm_runtime_mark_last_busy(q->dev);97spin_unlock_irq(&q->queue_lock);9899blk_clear_pm_only(q);100}101102return ret;103}104EXPORT_SYMBOL(blk_pre_runtime_suspend);105106/**107* blk_post_runtime_suspend - Post runtime suspend processing108* @q: the queue of the device109* @err: return value of the device's runtime_suspend function110*111* Description:112* Update the queue's runtime status according to the return value of the113* device's runtime suspend function and mark last busy for the device so114* that PM core will try to auto suspend the device at a later time.115*116* This function should be called near the end of the device's117* runtime_suspend callback.118*/119void blk_post_runtime_suspend(struct request_queue *q, int err)120{121if (!q->dev)122return;123124spin_lock_irq(&q->queue_lock);125if (!err) {126q->rpm_status = RPM_SUSPENDED;127} else {128q->rpm_status = RPM_ACTIVE;129pm_runtime_mark_last_busy(q->dev);130}131spin_unlock_irq(&q->queue_lock);132133if (err)134blk_clear_pm_only(q);135}136EXPORT_SYMBOL(blk_post_runtime_suspend);137138/**139* blk_pre_runtime_resume - Pre runtime resume processing140* @q: the queue of the device141*142* Description:143* Update the queue's runtime status to RESUMING in preparation for the144* runtime resume of the device.145*146* This function should be called near the start of the device's147* runtime_resume callback.148*/149void blk_pre_runtime_resume(struct request_queue *q)150{151if (!q->dev)152return;153154spin_lock_irq(&q->queue_lock);155q->rpm_status = RPM_RESUMING;156spin_unlock_irq(&q->queue_lock);157}158EXPORT_SYMBOL(blk_pre_runtime_resume);159160/**161* blk_post_runtime_resume - Post runtime resume processing162* @q: the queue of the device163*164* Description:165* Restart the queue of a runtime suspended device. It does this regardless166* of whether the device's runtime-resume succeeded; even if it failed the167* driver or error handler will need to communicate with the device.168*169* This function should be called near the end of the device's170* runtime_resume callback to correct queue runtime PM status and re-enable171* peeking requests from the queue.172*/173void blk_post_runtime_resume(struct request_queue *q)174{175int old_status;176177if (!q->dev)178return;179180spin_lock_irq(&q->queue_lock);181old_status = q->rpm_status;182q->rpm_status = RPM_ACTIVE;183pm_runtime_mark_last_busy(q->dev);184pm_request_autosuspend(q->dev);185spin_unlock_irq(&q->queue_lock);186187if (old_status != RPM_ACTIVE)188blk_clear_pm_only(q);189}190EXPORT_SYMBOL(blk_post_runtime_resume);191192193