xref: /openbmc/linux/drivers/base/power/runtime.c (revision 7b6d864b)
1 /*
2  * drivers/base/power/runtime.c - Helper functions for device runtime PM
3  *
4  * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5  * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
6  *
7  * This file is released under the GPLv2.
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include <trace/events/rpm.h>
14 #include "power.h"
15 
16 static int rpm_resume(struct device *dev, int rpmflags);
17 static int rpm_suspend(struct device *dev, int rpmflags);
18 
19 /**
20  * update_pm_runtime_accounting - Update the time accounting of power states
21  * @dev: Device to update the accounting for
22  *
23  * In order to be able to have time accounting of the various power states
24  * (as used by programs such as PowerTOP to show the effectiveness of runtime
25  * PM), we need to track the time spent in each state.
26  * update_pm_runtime_accounting must be called each time before the
27  * runtime_status field is updated, to account the time in the old state
28  * correctly.
29  */
30 void update_pm_runtime_accounting(struct device *dev)
31 {
32 	unsigned long now = jiffies;
33 	unsigned long delta;
34 
35 	delta = now - dev->power.accounting_timestamp;
36 
37 	dev->power.accounting_timestamp = now;
38 
39 	if (dev->power.disable_depth > 0)
40 		return;
41 
42 	if (dev->power.runtime_status == RPM_SUSPENDED)
43 		dev->power.suspended_jiffies += delta;
44 	else
45 		dev->power.active_jiffies += delta;
46 }
47 
48 static void __update_runtime_status(struct device *dev, enum rpm_status status)
49 {
50 	update_pm_runtime_accounting(dev);
51 	dev->power.runtime_status = status;
52 }
53 
54 /**
55  * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
56  * @dev: Device to handle.
57  */
58 static void pm_runtime_deactivate_timer(struct device *dev)
59 {
60 	if (dev->power.timer_expires > 0) {
61 		del_timer(&dev->power.suspend_timer);
62 		dev->power.timer_expires = 0;
63 	}
64 }
65 
66 /**
67  * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
68  * @dev: Device to handle.
69  */
70 static void pm_runtime_cancel_pending(struct device *dev)
71 {
72 	pm_runtime_deactivate_timer(dev);
73 	/*
74 	 * In case there's a request pending, make sure its work function will
75 	 * return without doing anything.
76 	 */
77 	dev->power.request = RPM_REQ_NONE;
78 }
79 
80 /*
81  * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
82  * @dev: Device to handle.
83  *
84  * Compute the autosuspend-delay expiration time based on the device's
85  * power.last_busy time.  If the delay has already expired or is disabled
86  * (negative) or the power.use_autosuspend flag isn't set, return 0.
87  * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
88  *
89  * This function may be called either with or without dev->power.lock held.
90  * Either way it can be racy, since power.last_busy may be updated at any time.
91  */
92 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
93 {
94 	int autosuspend_delay;
95 	long elapsed;
96 	unsigned long last_busy;
97 	unsigned long expires = 0;
98 
99 	if (!dev->power.use_autosuspend)
100 		goto out;
101 
102 	autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
103 	if (autosuspend_delay < 0)
104 		goto out;
105 
106 	last_busy = ACCESS_ONCE(dev->power.last_busy);
107 	elapsed = jiffies - last_busy;
108 	if (elapsed < 0)
109 		goto out;	/* jiffies has wrapped around. */
110 
111 	/*
112 	 * If the autosuspend_delay is >= 1 second, align the timer by rounding
113 	 * up to the nearest second.
114 	 */
115 	expires = last_busy + msecs_to_jiffies(autosuspend_delay);
116 	if (autosuspend_delay >= 1000)
117 		expires = round_jiffies(expires);
118 	expires += !expires;
119 	if (elapsed >= expires - last_busy)
120 		expires = 0;	/* Already expired. */
121 
122  out:
123 	return expires;
124 }
125 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
126 
127 static int dev_memalloc_noio(struct device *dev, void *data)
128 {
129 	return dev->power.memalloc_noio;
130 }
131 
132 /*
133  * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
134  * @dev: Device to handle.
135  * @enable: True for setting the flag and False for clearing the flag.
136  *
137  * Set the flag for all devices in the path from the device to the
138  * root device in the device tree if @enable is true, otherwise clear
139  * the flag for devices in the path whose siblings don't set the flag.
140  *
141  * The function should only be called by block device, or network
142  * device driver for solving the deadlock problem during runtime
143  * resume/suspend:
144  *
145  *     If memory allocation with GFP_KERNEL is called inside runtime
146  *     resume/suspend callback of any one of its ancestors(or the
147  *     block device itself), the deadlock may be triggered inside the
148  *     memory allocation since it might not complete until the block
149  *     device becomes active and the involed page I/O finishes. The
150  *     situation is pointed out first by Alan Stern. Network device
151  *     are involved in iSCSI kind of situation.
152  *
153  * The lock of dev_hotplug_mutex is held in the function for handling
154  * hotplug race because pm_runtime_set_memalloc_noio() may be called
155  * in async probe().
156  *
157  * The function should be called between device_add() and device_del()
158  * on the affected device(block/network device).
159  */
160 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
161 {
162 	static DEFINE_MUTEX(dev_hotplug_mutex);
163 
164 	mutex_lock(&dev_hotplug_mutex);
165 	for (;;) {
166 		bool enabled;
167 
168 		/* hold power lock since bitfield is not SMP-safe. */
169 		spin_lock_irq(&dev->power.lock);
170 		enabled = dev->power.memalloc_noio;
171 		dev->power.memalloc_noio = enable;
172 		spin_unlock_irq(&dev->power.lock);
173 
174 		/*
175 		 * not need to enable ancestors any more if the device
176 		 * has been enabled.
177 		 */
178 		if (enabled && enable)
179 			break;
180 
181 		dev = dev->parent;
182 
183 		/*
184 		 * clear flag of the parent device only if all the
185 		 * children don't set the flag because ancestor's
186 		 * flag was set by any one of the descendants.
187 		 */
188 		if (!dev || (!enable &&
189 			     device_for_each_child(dev, NULL,
190 						   dev_memalloc_noio)))
191 			break;
192 	}
193 	mutex_unlock(&dev_hotplug_mutex);
194 }
195 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
196 
197 /**
198  * rpm_check_suspend_allowed - Test whether a device may be suspended.
199  * @dev: Device to test.
200  */
201 static int rpm_check_suspend_allowed(struct device *dev)
202 {
203 	int retval = 0;
204 
205 	if (dev->power.runtime_error)
206 		retval = -EINVAL;
207 	else if (dev->power.disable_depth > 0)
208 		retval = -EACCES;
209 	else if (atomic_read(&dev->power.usage_count) > 0)
210 		retval = -EAGAIN;
211 	else if (!pm_children_suspended(dev))
212 		retval = -EBUSY;
213 
214 	/* Pending resume requests take precedence over suspends. */
215 	else if ((dev->power.deferred_resume
216 			&& dev->power.runtime_status == RPM_SUSPENDING)
217 	    || (dev->power.request_pending
218 			&& dev->power.request == RPM_REQ_RESUME))
219 		retval = -EAGAIN;
220 	else if (__dev_pm_qos_read_value(dev) < 0)
221 		retval = -EPERM;
222 	else if (dev->power.runtime_status == RPM_SUSPENDED)
223 		retval = 1;
224 
225 	return retval;
226 }
227 
228 /**
229  * __rpm_callback - Run a given runtime PM callback for a given device.
230  * @cb: Runtime PM callback to run.
231  * @dev: Device to run the callback for.
232  */
233 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
234 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
235 {
236 	int retval;
237 
238 	if (dev->power.irq_safe)
239 		spin_unlock(&dev->power.lock);
240 	else
241 		spin_unlock_irq(&dev->power.lock);
242 
243 	retval = cb(dev);
244 
245 	if (dev->power.irq_safe)
246 		spin_lock(&dev->power.lock);
247 	else
248 		spin_lock_irq(&dev->power.lock);
249 
250 	return retval;
251 }
252 
253 /**
254  * rpm_idle - Notify device bus type if the device can be suspended.
255  * @dev: Device to notify the bus type about.
256  * @rpmflags: Flag bits.
257  *
258  * Check if the device's runtime PM status allows it to be suspended.  If
259  * another idle notification has been started earlier, return immediately.  If
260  * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
261  * run the ->runtime_idle() callback directly.
262  *
263  * This function must be called under dev->power.lock with interrupts disabled.
264  */
265 static int rpm_idle(struct device *dev, int rpmflags)
266 {
267 	int (*callback)(struct device *);
268 	int retval;
269 
270 	trace_rpm_idle(dev, rpmflags);
271 	retval = rpm_check_suspend_allowed(dev);
272 	if (retval < 0)
273 		;	/* Conditions are wrong. */
274 
275 	/* Idle notifications are allowed only in the RPM_ACTIVE state. */
276 	else if (dev->power.runtime_status != RPM_ACTIVE)
277 		retval = -EAGAIN;
278 
279 	/*
280 	 * Any pending request other than an idle notification takes
281 	 * precedence over us, except that the timer may be running.
282 	 */
283 	else if (dev->power.request_pending &&
284 	    dev->power.request > RPM_REQ_IDLE)
285 		retval = -EAGAIN;
286 
287 	/* Act as though RPM_NOWAIT is always set. */
288 	else if (dev->power.idle_notification)
289 		retval = -EINPROGRESS;
290 	if (retval)
291 		goto out;
292 
293 	/* Pending requests need to be canceled. */
294 	dev->power.request = RPM_REQ_NONE;
295 
296 	if (dev->power.no_callbacks)
297 		goto out;
298 
299 	/* Carry out an asynchronous or a synchronous idle notification. */
300 	if (rpmflags & RPM_ASYNC) {
301 		dev->power.request = RPM_REQ_IDLE;
302 		if (!dev->power.request_pending) {
303 			dev->power.request_pending = true;
304 			queue_work(pm_wq, &dev->power.work);
305 		}
306 		trace_rpm_return_int(dev, _THIS_IP_, 0);
307 		return 0;
308 	}
309 
310 	dev->power.idle_notification = true;
311 
312 	if (dev->pm_domain)
313 		callback = dev->pm_domain->ops.runtime_idle;
314 	else if (dev->type && dev->type->pm)
315 		callback = dev->type->pm->runtime_idle;
316 	else if (dev->class && dev->class->pm)
317 		callback = dev->class->pm->runtime_idle;
318 	else if (dev->bus && dev->bus->pm)
319 		callback = dev->bus->pm->runtime_idle;
320 	else
321 		callback = NULL;
322 
323 	if (!callback && dev->driver && dev->driver->pm)
324 		callback = dev->driver->pm->runtime_idle;
325 
326 	if (callback)
327 		retval = __rpm_callback(callback, dev);
328 
329 	dev->power.idle_notification = false;
330 	wake_up_all(&dev->power.wait_queue);
331 
332  out:
333 	trace_rpm_return_int(dev, _THIS_IP_, retval);
334 	return retval ? retval : rpm_suspend(dev, rpmflags);
335 }
336 
337 /**
338  * rpm_callback - Run a given runtime PM callback for a given device.
339  * @cb: Runtime PM callback to run.
340  * @dev: Device to run the callback for.
341  */
342 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
343 {
344 	int retval;
345 
346 	if (!cb)
347 		return -ENOSYS;
348 
349 	if (dev->power.memalloc_noio) {
350 		unsigned int noio_flag;
351 
352 		/*
353 		 * Deadlock might be caused if memory allocation with
354 		 * GFP_KERNEL happens inside runtime_suspend and
355 		 * runtime_resume callbacks of one block device's
356 		 * ancestor or the block device itself. Network
357 		 * device might be thought as part of iSCSI block
358 		 * device, so network device and its ancestor should
359 		 * be marked as memalloc_noio too.
360 		 */
361 		noio_flag = memalloc_noio_save();
362 		retval = __rpm_callback(cb, dev);
363 		memalloc_noio_restore(noio_flag);
364 	} else {
365 		retval = __rpm_callback(cb, dev);
366 	}
367 
368 	dev->power.runtime_error = retval;
369 	return retval != -EACCES ? retval : -EIO;
370 }
371 
372 /**
373  * rpm_suspend - Carry out runtime suspend of given device.
374  * @dev: Device to suspend.
375  * @rpmflags: Flag bits.
376  *
377  * Check if the device's runtime PM status allows it to be suspended.
378  * Cancel a pending idle notification, autosuspend or suspend. If
379  * another suspend has been started earlier, either return immediately
380  * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
381  * flags. If the RPM_ASYNC flag is set then queue a suspend request;
382  * otherwise run the ->runtime_suspend() callback directly. When
383  * ->runtime_suspend succeeded, if a deferred resume was requested while
384  * the callback was running then carry it out, otherwise send an idle
385  * notification for its parent (if the suspend succeeded and both
386  * ignore_children of parent->power and irq_safe of dev->power are not set).
387  * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
388  * flag is set and the next autosuspend-delay expiration time is in the
389  * future, schedule another autosuspend attempt.
390  *
391  * This function must be called under dev->power.lock with interrupts disabled.
392  */
393 static int rpm_suspend(struct device *dev, int rpmflags)
394 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
395 {
396 	int (*callback)(struct device *);
397 	struct device *parent = NULL;
398 	int retval;
399 
400 	trace_rpm_suspend(dev, rpmflags);
401 
402  repeat:
403 	retval = rpm_check_suspend_allowed(dev);
404 
405 	if (retval < 0)
406 		;	/* Conditions are wrong. */
407 
408 	/* Synchronous suspends are not allowed in the RPM_RESUMING state. */
409 	else if (dev->power.runtime_status == RPM_RESUMING &&
410 	    !(rpmflags & RPM_ASYNC))
411 		retval = -EAGAIN;
412 	if (retval)
413 		goto out;
414 
415 	/* If the autosuspend_delay time hasn't expired yet, reschedule. */
416 	if ((rpmflags & RPM_AUTO)
417 	    && dev->power.runtime_status != RPM_SUSPENDING) {
418 		unsigned long expires = pm_runtime_autosuspend_expiration(dev);
419 
420 		if (expires != 0) {
421 			/* Pending requests need to be canceled. */
422 			dev->power.request = RPM_REQ_NONE;
423 
424 			/*
425 			 * Optimization: If the timer is already running and is
426 			 * set to expire at or before the autosuspend delay,
427 			 * avoid the overhead of resetting it.  Just let it
428 			 * expire; pm_suspend_timer_fn() will take care of the
429 			 * rest.
430 			 */
431 			if (!(dev->power.timer_expires && time_before_eq(
432 			    dev->power.timer_expires, expires))) {
433 				dev->power.timer_expires = expires;
434 				mod_timer(&dev->power.suspend_timer, expires);
435 			}
436 			dev->power.timer_autosuspends = 1;
437 			goto out;
438 		}
439 	}
440 
441 	/* Other scheduled or pending requests need to be canceled. */
442 	pm_runtime_cancel_pending(dev);
443 
444 	if (dev->power.runtime_status == RPM_SUSPENDING) {
445 		DEFINE_WAIT(wait);
446 
447 		if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
448 			retval = -EINPROGRESS;
449 			goto out;
450 		}
451 
452 		if (dev->power.irq_safe) {
453 			spin_unlock(&dev->power.lock);
454 
455 			cpu_relax();
456 
457 			spin_lock(&dev->power.lock);
458 			goto repeat;
459 		}
460 
461 		/* Wait for the other suspend running in parallel with us. */
462 		for (;;) {
463 			prepare_to_wait(&dev->power.wait_queue, &wait,
464 					TASK_UNINTERRUPTIBLE);
465 			if (dev->power.runtime_status != RPM_SUSPENDING)
466 				break;
467 
468 			spin_unlock_irq(&dev->power.lock);
469 
470 			schedule();
471 
472 			spin_lock_irq(&dev->power.lock);
473 		}
474 		finish_wait(&dev->power.wait_queue, &wait);
475 		goto repeat;
476 	}
477 
478 	if (dev->power.no_callbacks)
479 		goto no_callback;	/* Assume success. */
480 
481 	/* Carry out an asynchronous or a synchronous suspend. */
482 	if (rpmflags & RPM_ASYNC) {
483 		dev->power.request = (rpmflags & RPM_AUTO) ?
484 		    RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
485 		if (!dev->power.request_pending) {
486 			dev->power.request_pending = true;
487 			queue_work(pm_wq, &dev->power.work);
488 		}
489 		goto out;
490 	}
491 
492 	__update_runtime_status(dev, RPM_SUSPENDING);
493 
494 	if (dev->pm_domain)
495 		callback = dev->pm_domain->ops.runtime_suspend;
496 	else if (dev->type && dev->type->pm)
497 		callback = dev->type->pm->runtime_suspend;
498 	else if (dev->class && dev->class->pm)
499 		callback = dev->class->pm->runtime_suspend;
500 	else if (dev->bus && dev->bus->pm)
501 		callback = dev->bus->pm->runtime_suspend;
502 	else
503 		callback = NULL;
504 
505 	if (!callback && dev->driver && dev->driver->pm)
506 		callback = dev->driver->pm->runtime_suspend;
507 
508 	retval = rpm_callback(callback, dev);
509 	if (retval)
510 		goto fail;
511 
512  no_callback:
513 	__update_runtime_status(dev, RPM_SUSPENDED);
514 	pm_runtime_deactivate_timer(dev);
515 
516 	if (dev->parent) {
517 		parent = dev->parent;
518 		atomic_add_unless(&parent->power.child_count, -1, 0);
519 	}
520 	wake_up_all(&dev->power.wait_queue);
521 
522 	if (dev->power.deferred_resume) {
523 		dev->power.deferred_resume = false;
524 		rpm_resume(dev, 0);
525 		retval = -EAGAIN;
526 		goto out;
527 	}
528 
529 	/* Maybe the parent is now able to suspend. */
530 	if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
531 		spin_unlock(&dev->power.lock);
532 
533 		spin_lock(&parent->power.lock);
534 		rpm_idle(parent, RPM_ASYNC);
535 		spin_unlock(&parent->power.lock);
536 
537 		spin_lock(&dev->power.lock);
538 	}
539 
540  out:
541 	trace_rpm_return_int(dev, _THIS_IP_, retval);
542 
543 	return retval;
544 
545  fail:
546 	__update_runtime_status(dev, RPM_ACTIVE);
547 	dev->power.deferred_resume = false;
548 	wake_up_all(&dev->power.wait_queue);
549 
550 	if (retval == -EAGAIN || retval == -EBUSY) {
551 		dev->power.runtime_error = 0;
552 
553 		/*
554 		 * If the callback routine failed an autosuspend, and
555 		 * if the last_busy time has been updated so that there
556 		 * is a new autosuspend expiration time, automatically
557 		 * reschedule another autosuspend.
558 		 */
559 		if ((rpmflags & RPM_AUTO) &&
560 		    pm_runtime_autosuspend_expiration(dev) != 0)
561 			goto repeat;
562 	} else {
563 		pm_runtime_cancel_pending(dev);
564 	}
565 	goto out;
566 }
567 
568 /**
569  * rpm_resume - Carry out runtime resume of given device.
570  * @dev: Device to resume.
571  * @rpmflags: Flag bits.
572  *
573  * Check if the device's runtime PM status allows it to be resumed.  Cancel
574  * any scheduled or pending requests.  If another resume has been started
575  * earlier, either return immediately or wait for it to finish, depending on the
576  * RPM_NOWAIT and RPM_ASYNC flags.  Similarly, if there's a suspend running in
577  * parallel with this function, either tell the other process to resume after
578  * suspending (deferred_resume) or wait for it to finish.  If the RPM_ASYNC
579  * flag is set then queue a resume request; otherwise run the
580  * ->runtime_resume() callback directly.  Queue an idle notification for the
581  * device if the resume succeeded.
582  *
583  * This function must be called under dev->power.lock with interrupts disabled.
584  */
585 static int rpm_resume(struct device *dev, int rpmflags)
586 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
587 {
588 	int (*callback)(struct device *);
589 	struct device *parent = NULL;
590 	int retval = 0;
591 
592 	trace_rpm_resume(dev, rpmflags);
593 
594  repeat:
595 	if (dev->power.runtime_error)
596 		retval = -EINVAL;
597 	else if (dev->power.disable_depth == 1 && dev->power.is_suspended
598 	    && dev->power.runtime_status == RPM_ACTIVE)
599 		retval = 1;
600 	else if (dev->power.disable_depth > 0)
601 		retval = -EACCES;
602 	if (retval)
603 		goto out;
604 
605 	/*
606 	 * Other scheduled or pending requests need to be canceled.  Small
607 	 * optimization: If an autosuspend timer is running, leave it running
608 	 * rather than cancelling it now only to restart it again in the near
609 	 * future.
610 	 */
611 	dev->power.request = RPM_REQ_NONE;
612 	if (!dev->power.timer_autosuspends)
613 		pm_runtime_deactivate_timer(dev);
614 
615 	if (dev->power.runtime_status == RPM_ACTIVE) {
616 		retval = 1;
617 		goto out;
618 	}
619 
620 	if (dev->power.runtime_status == RPM_RESUMING
621 	    || dev->power.runtime_status == RPM_SUSPENDING) {
622 		DEFINE_WAIT(wait);
623 
624 		if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
625 			if (dev->power.runtime_status == RPM_SUSPENDING)
626 				dev->power.deferred_resume = true;
627 			else
628 				retval = -EINPROGRESS;
629 			goto out;
630 		}
631 
632 		if (dev->power.irq_safe) {
633 			spin_unlock(&dev->power.lock);
634 
635 			cpu_relax();
636 
637 			spin_lock(&dev->power.lock);
638 			goto repeat;
639 		}
640 
641 		/* Wait for the operation carried out in parallel with us. */
642 		for (;;) {
643 			prepare_to_wait(&dev->power.wait_queue, &wait,
644 					TASK_UNINTERRUPTIBLE);
645 			if (dev->power.runtime_status != RPM_RESUMING
646 			    && dev->power.runtime_status != RPM_SUSPENDING)
647 				break;
648 
649 			spin_unlock_irq(&dev->power.lock);
650 
651 			schedule();
652 
653 			spin_lock_irq(&dev->power.lock);
654 		}
655 		finish_wait(&dev->power.wait_queue, &wait);
656 		goto repeat;
657 	}
658 
659 	/*
660 	 * See if we can skip waking up the parent.  This is safe only if
661 	 * power.no_callbacks is set, because otherwise we don't know whether
662 	 * the resume will actually succeed.
663 	 */
664 	if (dev->power.no_callbacks && !parent && dev->parent) {
665 		spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
666 		if (dev->parent->power.disable_depth > 0
667 		    || dev->parent->power.ignore_children
668 		    || dev->parent->power.runtime_status == RPM_ACTIVE) {
669 			atomic_inc(&dev->parent->power.child_count);
670 			spin_unlock(&dev->parent->power.lock);
671 			retval = 1;
672 			goto no_callback;	/* Assume success. */
673 		}
674 		spin_unlock(&dev->parent->power.lock);
675 	}
676 
677 	/* Carry out an asynchronous or a synchronous resume. */
678 	if (rpmflags & RPM_ASYNC) {
679 		dev->power.request = RPM_REQ_RESUME;
680 		if (!dev->power.request_pending) {
681 			dev->power.request_pending = true;
682 			queue_work(pm_wq, &dev->power.work);
683 		}
684 		retval = 0;
685 		goto out;
686 	}
687 
688 	if (!parent && dev->parent) {
689 		/*
690 		 * Increment the parent's usage counter and resume it if
691 		 * necessary.  Not needed if dev is irq-safe; then the
692 		 * parent is permanently resumed.
693 		 */
694 		parent = dev->parent;
695 		if (dev->power.irq_safe)
696 			goto skip_parent;
697 		spin_unlock(&dev->power.lock);
698 
699 		pm_runtime_get_noresume(parent);
700 
701 		spin_lock(&parent->power.lock);
702 		/*
703 		 * We can resume if the parent's runtime PM is disabled or it
704 		 * is set to ignore children.
705 		 */
706 		if (!parent->power.disable_depth
707 		    && !parent->power.ignore_children) {
708 			rpm_resume(parent, 0);
709 			if (parent->power.runtime_status != RPM_ACTIVE)
710 				retval = -EBUSY;
711 		}
712 		spin_unlock(&parent->power.lock);
713 
714 		spin_lock(&dev->power.lock);
715 		if (retval)
716 			goto out;
717 		goto repeat;
718 	}
719  skip_parent:
720 
721 	if (dev->power.no_callbacks)
722 		goto no_callback;	/* Assume success. */
723 
724 	__update_runtime_status(dev, RPM_RESUMING);
725 
726 	if (dev->pm_domain)
727 		callback = dev->pm_domain->ops.runtime_resume;
728 	else if (dev->type && dev->type->pm)
729 		callback = dev->type->pm->runtime_resume;
730 	else if (dev->class && dev->class->pm)
731 		callback = dev->class->pm->runtime_resume;
732 	else if (dev->bus && dev->bus->pm)
733 		callback = dev->bus->pm->runtime_resume;
734 	else
735 		callback = NULL;
736 
737 	if (!callback && dev->driver && dev->driver->pm)
738 		callback = dev->driver->pm->runtime_resume;
739 
740 	retval = rpm_callback(callback, dev);
741 	if (retval) {
742 		__update_runtime_status(dev, RPM_SUSPENDED);
743 		pm_runtime_cancel_pending(dev);
744 	} else {
745  no_callback:
746 		__update_runtime_status(dev, RPM_ACTIVE);
747 		if (parent)
748 			atomic_inc(&parent->power.child_count);
749 	}
750 	wake_up_all(&dev->power.wait_queue);
751 
752 	if (retval >= 0)
753 		rpm_idle(dev, RPM_ASYNC);
754 
755  out:
756 	if (parent && !dev->power.irq_safe) {
757 		spin_unlock_irq(&dev->power.lock);
758 
759 		pm_runtime_put(parent);
760 
761 		spin_lock_irq(&dev->power.lock);
762 	}
763 
764 	trace_rpm_return_int(dev, _THIS_IP_, retval);
765 
766 	return retval;
767 }
768 
769 /**
770  * pm_runtime_work - Universal runtime PM work function.
771  * @work: Work structure used for scheduling the execution of this function.
772  *
773  * Use @work to get the device object the work is to be done for, determine what
774  * is to be done and execute the appropriate runtime PM function.
775  */
776 static void pm_runtime_work(struct work_struct *work)
777 {
778 	struct device *dev = container_of(work, struct device, power.work);
779 	enum rpm_request req;
780 
781 	spin_lock_irq(&dev->power.lock);
782 
783 	if (!dev->power.request_pending)
784 		goto out;
785 
786 	req = dev->power.request;
787 	dev->power.request = RPM_REQ_NONE;
788 	dev->power.request_pending = false;
789 
790 	switch (req) {
791 	case RPM_REQ_NONE:
792 		break;
793 	case RPM_REQ_IDLE:
794 		rpm_idle(dev, RPM_NOWAIT);
795 		break;
796 	case RPM_REQ_SUSPEND:
797 		rpm_suspend(dev, RPM_NOWAIT);
798 		break;
799 	case RPM_REQ_AUTOSUSPEND:
800 		rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
801 		break;
802 	case RPM_REQ_RESUME:
803 		rpm_resume(dev, RPM_NOWAIT);
804 		break;
805 	}
806 
807  out:
808 	spin_unlock_irq(&dev->power.lock);
809 }
810 
811 /**
812  * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
813  * @data: Device pointer passed by pm_schedule_suspend().
814  *
815  * Check if the time is right and queue a suspend request.
816  */
817 static void pm_suspend_timer_fn(unsigned long data)
818 {
819 	struct device *dev = (struct device *)data;
820 	unsigned long flags;
821 	unsigned long expires;
822 
823 	spin_lock_irqsave(&dev->power.lock, flags);
824 
825 	expires = dev->power.timer_expires;
826 	/* If 'expire' is after 'jiffies' we've been called too early. */
827 	if (expires > 0 && !time_after(expires, jiffies)) {
828 		dev->power.timer_expires = 0;
829 		rpm_suspend(dev, dev->power.timer_autosuspends ?
830 		    (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
831 	}
832 
833 	spin_unlock_irqrestore(&dev->power.lock, flags);
834 }
835 
836 /**
837  * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
838  * @dev: Device to suspend.
839  * @delay: Time to wait before submitting a suspend request, in milliseconds.
840  */
841 int pm_schedule_suspend(struct device *dev, unsigned int delay)
842 {
843 	unsigned long flags;
844 	int retval;
845 
846 	spin_lock_irqsave(&dev->power.lock, flags);
847 
848 	if (!delay) {
849 		retval = rpm_suspend(dev, RPM_ASYNC);
850 		goto out;
851 	}
852 
853 	retval = rpm_check_suspend_allowed(dev);
854 	if (retval)
855 		goto out;
856 
857 	/* Other scheduled or pending requests need to be canceled. */
858 	pm_runtime_cancel_pending(dev);
859 
860 	dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
861 	dev->power.timer_expires += !dev->power.timer_expires;
862 	dev->power.timer_autosuspends = 0;
863 	mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
864 
865  out:
866 	spin_unlock_irqrestore(&dev->power.lock, flags);
867 
868 	return retval;
869 }
870 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
871 
872 /**
873  * __pm_runtime_idle - Entry point for runtime idle operations.
874  * @dev: Device to send idle notification for.
875  * @rpmflags: Flag bits.
876  *
877  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
878  * return immediately if it is larger than zero.  Then carry out an idle
879  * notification, either synchronous or asynchronous.
880  *
881  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
882  * or if pm_runtime_irq_safe() has been called.
883  */
884 int __pm_runtime_idle(struct device *dev, int rpmflags)
885 {
886 	unsigned long flags;
887 	int retval;
888 
889 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
890 
891 	if (rpmflags & RPM_GET_PUT) {
892 		if (!atomic_dec_and_test(&dev->power.usage_count))
893 			return 0;
894 	}
895 
896 	spin_lock_irqsave(&dev->power.lock, flags);
897 	retval = rpm_idle(dev, rpmflags);
898 	spin_unlock_irqrestore(&dev->power.lock, flags);
899 
900 	return retval;
901 }
902 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
903 
904 /**
905  * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
906  * @dev: Device to suspend.
907  * @rpmflags: Flag bits.
908  *
909  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
910  * return immediately if it is larger than zero.  Then carry out a suspend,
911  * either synchronous or asynchronous.
912  *
913  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
914  * or if pm_runtime_irq_safe() has been called.
915  */
916 int __pm_runtime_suspend(struct device *dev, int rpmflags)
917 {
918 	unsigned long flags;
919 	int retval;
920 
921 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
922 
923 	if (rpmflags & RPM_GET_PUT) {
924 		if (!atomic_dec_and_test(&dev->power.usage_count))
925 			return 0;
926 	}
927 
928 	spin_lock_irqsave(&dev->power.lock, flags);
929 	retval = rpm_suspend(dev, rpmflags);
930 	spin_unlock_irqrestore(&dev->power.lock, flags);
931 
932 	return retval;
933 }
934 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
935 
936 /**
937  * __pm_runtime_resume - Entry point for runtime resume operations.
938  * @dev: Device to resume.
939  * @rpmflags: Flag bits.
940  *
941  * If the RPM_GET_PUT flag is set, increment the device's usage count.  Then
942  * carry out a resume, either synchronous or asynchronous.
943  *
944  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
945  * or if pm_runtime_irq_safe() has been called.
946  */
947 int __pm_runtime_resume(struct device *dev, int rpmflags)
948 {
949 	unsigned long flags;
950 	int retval;
951 
952 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
953 
954 	if (rpmflags & RPM_GET_PUT)
955 		atomic_inc(&dev->power.usage_count);
956 
957 	spin_lock_irqsave(&dev->power.lock, flags);
958 	retval = rpm_resume(dev, rpmflags);
959 	spin_unlock_irqrestore(&dev->power.lock, flags);
960 
961 	return retval;
962 }
963 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
964 
965 /**
966  * __pm_runtime_set_status - Set runtime PM status of a device.
967  * @dev: Device to handle.
968  * @status: New runtime PM status of the device.
969  *
970  * If runtime PM of the device is disabled or its power.runtime_error field is
971  * different from zero, the status may be changed either to RPM_ACTIVE, or to
972  * RPM_SUSPENDED, as long as that reflects the actual state of the device.
973  * However, if the device has a parent and the parent is not active, and the
974  * parent's power.ignore_children flag is unset, the device's status cannot be
975  * set to RPM_ACTIVE, so -EBUSY is returned in that case.
976  *
977  * If successful, __pm_runtime_set_status() clears the power.runtime_error field
978  * and the device parent's counter of unsuspended children is modified to
979  * reflect the new status.  If the new status is RPM_SUSPENDED, an idle
980  * notification request for the parent is submitted.
981  */
982 int __pm_runtime_set_status(struct device *dev, unsigned int status)
983 {
984 	struct device *parent = dev->parent;
985 	unsigned long flags;
986 	bool notify_parent = false;
987 	int error = 0;
988 
989 	if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
990 		return -EINVAL;
991 
992 	spin_lock_irqsave(&dev->power.lock, flags);
993 
994 	if (!dev->power.runtime_error && !dev->power.disable_depth) {
995 		error = -EAGAIN;
996 		goto out;
997 	}
998 
999 	if (dev->power.runtime_status == status)
1000 		goto out_set;
1001 
1002 	if (status == RPM_SUSPENDED) {
1003 		/* It always is possible to set the status to 'suspended'. */
1004 		if (parent) {
1005 			atomic_add_unless(&parent->power.child_count, -1, 0);
1006 			notify_parent = !parent->power.ignore_children;
1007 		}
1008 		goto out_set;
1009 	}
1010 
1011 	if (parent) {
1012 		spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1013 
1014 		/*
1015 		 * It is invalid to put an active child under a parent that is
1016 		 * not active, has runtime PM enabled and the
1017 		 * 'power.ignore_children' flag unset.
1018 		 */
1019 		if (!parent->power.disable_depth
1020 		    && !parent->power.ignore_children
1021 		    && parent->power.runtime_status != RPM_ACTIVE)
1022 			error = -EBUSY;
1023 		else if (dev->power.runtime_status == RPM_SUSPENDED)
1024 			atomic_inc(&parent->power.child_count);
1025 
1026 		spin_unlock(&parent->power.lock);
1027 
1028 		if (error)
1029 			goto out;
1030 	}
1031 
1032  out_set:
1033 	__update_runtime_status(dev, status);
1034 	dev->power.runtime_error = 0;
1035  out:
1036 	spin_unlock_irqrestore(&dev->power.lock, flags);
1037 
1038 	if (notify_parent)
1039 		pm_request_idle(parent);
1040 
1041 	return error;
1042 }
1043 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1044 
1045 /**
1046  * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1047  * @dev: Device to handle.
1048  *
1049  * Flush all pending requests for the device from pm_wq and wait for all
1050  * runtime PM operations involving the device in progress to complete.
1051  *
1052  * Should be called under dev->power.lock with interrupts disabled.
1053  */
1054 static void __pm_runtime_barrier(struct device *dev)
1055 {
1056 	pm_runtime_deactivate_timer(dev);
1057 
1058 	if (dev->power.request_pending) {
1059 		dev->power.request = RPM_REQ_NONE;
1060 		spin_unlock_irq(&dev->power.lock);
1061 
1062 		cancel_work_sync(&dev->power.work);
1063 
1064 		spin_lock_irq(&dev->power.lock);
1065 		dev->power.request_pending = false;
1066 	}
1067 
1068 	if (dev->power.runtime_status == RPM_SUSPENDING
1069 	    || dev->power.runtime_status == RPM_RESUMING
1070 	    || dev->power.idle_notification) {
1071 		DEFINE_WAIT(wait);
1072 
1073 		/* Suspend, wake-up or idle notification in progress. */
1074 		for (;;) {
1075 			prepare_to_wait(&dev->power.wait_queue, &wait,
1076 					TASK_UNINTERRUPTIBLE);
1077 			if (dev->power.runtime_status != RPM_SUSPENDING
1078 			    && dev->power.runtime_status != RPM_RESUMING
1079 			    && !dev->power.idle_notification)
1080 				break;
1081 			spin_unlock_irq(&dev->power.lock);
1082 
1083 			schedule();
1084 
1085 			spin_lock_irq(&dev->power.lock);
1086 		}
1087 		finish_wait(&dev->power.wait_queue, &wait);
1088 	}
1089 }
1090 
1091 /**
1092  * pm_runtime_barrier - Flush pending requests and wait for completions.
1093  * @dev: Device to handle.
1094  *
1095  * Prevent the device from being suspended by incrementing its usage counter and
1096  * if there's a pending resume request for the device, wake the device up.
1097  * Next, make sure that all pending requests for the device have been flushed
1098  * from pm_wq and wait for all runtime PM operations involving the device in
1099  * progress to complete.
1100  *
1101  * Return value:
1102  * 1, if there was a resume request pending and the device had to be woken up,
1103  * 0, otherwise
1104  */
1105 int pm_runtime_barrier(struct device *dev)
1106 {
1107 	int retval = 0;
1108 
1109 	pm_runtime_get_noresume(dev);
1110 	spin_lock_irq(&dev->power.lock);
1111 
1112 	if (dev->power.request_pending
1113 	    && dev->power.request == RPM_REQ_RESUME) {
1114 		rpm_resume(dev, 0);
1115 		retval = 1;
1116 	}
1117 
1118 	__pm_runtime_barrier(dev);
1119 
1120 	spin_unlock_irq(&dev->power.lock);
1121 	pm_runtime_put_noidle(dev);
1122 
1123 	return retval;
1124 }
1125 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1126 
1127 /**
1128  * __pm_runtime_disable - Disable runtime PM of a device.
1129  * @dev: Device to handle.
1130  * @check_resume: If set, check if there's a resume request for the device.
1131  *
1132  * Increment power.disable_depth for the device and if was zero previously,
1133  * cancel all pending runtime PM requests for the device and wait for all
1134  * operations in progress to complete.  The device can be either active or
1135  * suspended after its runtime PM has been disabled.
1136  *
1137  * If @check_resume is set and there's a resume request pending when
1138  * __pm_runtime_disable() is called and power.disable_depth is zero, the
1139  * function will wake up the device before disabling its runtime PM.
1140  */
1141 void __pm_runtime_disable(struct device *dev, bool check_resume)
1142 {
1143 	spin_lock_irq(&dev->power.lock);
1144 
1145 	if (dev->power.disable_depth > 0) {
1146 		dev->power.disable_depth++;
1147 		goto out;
1148 	}
1149 
1150 	/*
1151 	 * Wake up the device if there's a resume request pending, because that
1152 	 * means there probably is some I/O to process and disabling runtime PM
1153 	 * shouldn't prevent the device from processing the I/O.
1154 	 */
1155 	if (check_resume && dev->power.request_pending
1156 	    && dev->power.request == RPM_REQ_RESUME) {
1157 		/*
1158 		 * Prevent suspends and idle notifications from being carried
1159 		 * out after we have woken up the device.
1160 		 */
1161 		pm_runtime_get_noresume(dev);
1162 
1163 		rpm_resume(dev, 0);
1164 
1165 		pm_runtime_put_noidle(dev);
1166 	}
1167 
1168 	if (!dev->power.disable_depth++)
1169 		__pm_runtime_barrier(dev);
1170 
1171  out:
1172 	spin_unlock_irq(&dev->power.lock);
1173 }
1174 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1175 
1176 /**
1177  * pm_runtime_enable - Enable runtime PM of a device.
1178  * @dev: Device to handle.
1179  */
1180 void pm_runtime_enable(struct device *dev)
1181 {
1182 	unsigned long flags;
1183 
1184 	spin_lock_irqsave(&dev->power.lock, flags);
1185 
1186 	if (dev->power.disable_depth > 0)
1187 		dev->power.disable_depth--;
1188 	else
1189 		dev_warn(dev, "Unbalanced %s!\n", __func__);
1190 
1191 	spin_unlock_irqrestore(&dev->power.lock, flags);
1192 }
1193 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1194 
1195 /**
1196  * pm_runtime_forbid - Block runtime PM of a device.
1197  * @dev: Device to handle.
1198  *
1199  * Increase the device's usage count and clear its power.runtime_auto flag,
1200  * so that it cannot be suspended at run time until pm_runtime_allow() is called
1201  * for it.
1202  */
1203 void pm_runtime_forbid(struct device *dev)
1204 {
1205 	spin_lock_irq(&dev->power.lock);
1206 	if (!dev->power.runtime_auto)
1207 		goto out;
1208 
1209 	dev->power.runtime_auto = false;
1210 	atomic_inc(&dev->power.usage_count);
1211 	rpm_resume(dev, 0);
1212 
1213  out:
1214 	spin_unlock_irq(&dev->power.lock);
1215 }
1216 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1217 
1218 /**
1219  * pm_runtime_allow - Unblock runtime PM of a device.
1220  * @dev: Device to handle.
1221  *
1222  * Decrease the device's usage count and set its power.runtime_auto flag.
1223  */
1224 void pm_runtime_allow(struct device *dev)
1225 {
1226 	spin_lock_irq(&dev->power.lock);
1227 	if (dev->power.runtime_auto)
1228 		goto out;
1229 
1230 	dev->power.runtime_auto = true;
1231 	if (atomic_dec_and_test(&dev->power.usage_count))
1232 		rpm_idle(dev, RPM_AUTO);
1233 
1234  out:
1235 	spin_unlock_irq(&dev->power.lock);
1236 }
1237 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1238 
1239 /**
1240  * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1241  * @dev: Device to handle.
1242  *
1243  * Set the power.no_callbacks flag, which tells the PM core that this
1244  * device is power-managed through its parent and has no runtime PM
1245  * callbacks of its own.  The runtime sysfs attributes will be removed.
1246  */
1247 void pm_runtime_no_callbacks(struct device *dev)
1248 {
1249 	spin_lock_irq(&dev->power.lock);
1250 	dev->power.no_callbacks = 1;
1251 	spin_unlock_irq(&dev->power.lock);
1252 	if (device_is_registered(dev))
1253 		rpm_sysfs_remove(dev);
1254 }
1255 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1256 
1257 /**
1258  * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1259  * @dev: Device to handle
1260  *
1261  * Set the power.irq_safe flag, which tells the PM core that the
1262  * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1263  * always be invoked with the spinlock held and interrupts disabled.  It also
1264  * causes the parent's usage counter to be permanently incremented, preventing
1265  * the parent from runtime suspending -- otherwise an irq-safe child might have
1266  * to wait for a non-irq-safe parent.
1267  */
1268 void pm_runtime_irq_safe(struct device *dev)
1269 {
1270 	if (dev->parent)
1271 		pm_runtime_get_sync(dev->parent);
1272 	spin_lock_irq(&dev->power.lock);
1273 	dev->power.irq_safe = 1;
1274 	spin_unlock_irq(&dev->power.lock);
1275 }
1276 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1277 
1278 /**
1279  * update_autosuspend - Handle a change to a device's autosuspend settings.
1280  * @dev: Device to handle.
1281  * @old_delay: The former autosuspend_delay value.
1282  * @old_use: The former use_autosuspend value.
1283  *
1284  * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1285  * set; otherwise allow it.  Send an idle notification if suspends are allowed.
1286  *
1287  * This function must be called under dev->power.lock with interrupts disabled.
1288  */
1289 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1290 {
1291 	int delay = dev->power.autosuspend_delay;
1292 
1293 	/* Should runtime suspend be prevented now? */
1294 	if (dev->power.use_autosuspend && delay < 0) {
1295 
1296 		/* If it used to be allowed then prevent it. */
1297 		if (!old_use || old_delay >= 0) {
1298 			atomic_inc(&dev->power.usage_count);
1299 			rpm_resume(dev, 0);
1300 		}
1301 	}
1302 
1303 	/* Runtime suspend should be allowed now. */
1304 	else {
1305 
1306 		/* If it used to be prevented then allow it. */
1307 		if (old_use && old_delay < 0)
1308 			atomic_dec(&dev->power.usage_count);
1309 
1310 		/* Maybe we can autosuspend now. */
1311 		rpm_idle(dev, RPM_AUTO);
1312 	}
1313 }
1314 
1315 /**
1316  * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1317  * @dev: Device to handle.
1318  * @delay: Value of the new delay in milliseconds.
1319  *
1320  * Set the device's power.autosuspend_delay value.  If it changes to negative
1321  * and the power.use_autosuspend flag is set, prevent runtime suspends.  If it
1322  * changes the other way, allow runtime suspends.
1323  */
1324 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1325 {
1326 	int old_delay, old_use;
1327 
1328 	spin_lock_irq(&dev->power.lock);
1329 	old_delay = dev->power.autosuspend_delay;
1330 	old_use = dev->power.use_autosuspend;
1331 	dev->power.autosuspend_delay = delay;
1332 	update_autosuspend(dev, old_delay, old_use);
1333 	spin_unlock_irq(&dev->power.lock);
1334 }
1335 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1336 
1337 /**
1338  * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1339  * @dev: Device to handle.
1340  * @use: New value for use_autosuspend.
1341  *
1342  * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1343  * suspends as needed.
1344  */
1345 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1346 {
1347 	int old_delay, old_use;
1348 
1349 	spin_lock_irq(&dev->power.lock);
1350 	old_delay = dev->power.autosuspend_delay;
1351 	old_use = dev->power.use_autosuspend;
1352 	dev->power.use_autosuspend = use;
1353 	update_autosuspend(dev, old_delay, old_use);
1354 	spin_unlock_irq(&dev->power.lock);
1355 }
1356 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1357 
1358 /**
1359  * pm_runtime_init - Initialize runtime PM fields in given device object.
1360  * @dev: Device object to initialize.
1361  */
1362 void pm_runtime_init(struct device *dev)
1363 {
1364 	dev->power.runtime_status = RPM_SUSPENDED;
1365 	dev->power.idle_notification = false;
1366 
1367 	dev->power.disable_depth = 1;
1368 	atomic_set(&dev->power.usage_count, 0);
1369 
1370 	dev->power.runtime_error = 0;
1371 
1372 	atomic_set(&dev->power.child_count, 0);
1373 	pm_suspend_ignore_children(dev, false);
1374 	dev->power.runtime_auto = true;
1375 
1376 	dev->power.request_pending = false;
1377 	dev->power.request = RPM_REQ_NONE;
1378 	dev->power.deferred_resume = false;
1379 	dev->power.accounting_timestamp = jiffies;
1380 	INIT_WORK(&dev->power.work, pm_runtime_work);
1381 
1382 	dev->power.timer_expires = 0;
1383 	setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1384 			(unsigned long)dev);
1385 
1386 	init_waitqueue_head(&dev->power.wait_queue);
1387 }
1388 
1389 /**
1390  * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1391  * @dev: Device object being removed from device hierarchy.
1392  */
1393 void pm_runtime_remove(struct device *dev)
1394 {
1395 	__pm_runtime_disable(dev, false);
1396 
1397 	/* Change the status back to 'suspended' to match the initial status. */
1398 	if (dev->power.runtime_status == RPM_ACTIVE)
1399 		pm_runtime_set_suspended(dev);
1400 	if (dev->power.irq_safe && dev->parent)
1401 		pm_runtime_put(dev->parent);
1402 }
1403