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