xref: /openbmc/linux/drivers/base/power/runtime.c (revision 31eeb6b0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/base/power/runtime.c - Helper functions for device runtime PM
4  *
5  * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6  * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
7  */
8 #include <linux/sched/mm.h>
9 #include <linux/ktime.h>
10 #include <linux/hrtimer.h>
11 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_wakeirq.h>
14 #include <trace/events/rpm.h>
15 
16 #include "../base.h"
17 #include "power.h"
18 
19 typedef int (*pm_callback_t)(struct device *);
20 
21 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
22 {
23 	pm_callback_t cb;
24 	const struct dev_pm_ops *ops;
25 
26 	if (dev->pm_domain)
27 		ops = &dev->pm_domain->ops;
28 	else if (dev->type && dev->type->pm)
29 		ops = dev->type->pm;
30 	else if (dev->class && dev->class->pm)
31 		ops = dev->class->pm;
32 	else if (dev->bus && dev->bus->pm)
33 		ops = dev->bus->pm;
34 	else
35 		ops = NULL;
36 
37 	if (ops)
38 		cb = *(pm_callback_t *)((void *)ops + cb_offset);
39 	else
40 		cb = NULL;
41 
42 	if (!cb && dev->driver && dev->driver->pm)
43 		cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
44 
45 	return cb;
46 }
47 
48 #define RPM_GET_CALLBACK(dev, callback) \
49 		__rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
50 
51 static int rpm_resume(struct device *dev, int rpmflags);
52 static int rpm_suspend(struct device *dev, int rpmflags);
53 
54 /**
55  * update_pm_runtime_accounting - Update the time accounting of power states
56  * @dev: Device to update the accounting for
57  *
58  * In order to be able to have time accounting of the various power states
59  * (as used by programs such as PowerTOP to show the effectiveness of runtime
60  * PM), we need to track the time spent in each state.
61  * update_pm_runtime_accounting must be called each time before the
62  * runtime_status field is updated, to account the time in the old state
63  * correctly.
64  */
65 static void update_pm_runtime_accounting(struct device *dev)
66 {
67 	u64 now, last, delta;
68 
69 	if (dev->power.disable_depth > 0)
70 		return;
71 
72 	last = dev->power.accounting_timestamp;
73 
74 	now = ktime_get_mono_fast_ns();
75 	dev->power.accounting_timestamp = now;
76 
77 	/*
78 	 * Because ktime_get_mono_fast_ns() is not monotonic during
79 	 * timekeeping updates, ensure that 'now' is after the last saved
80 	 * timesptamp.
81 	 */
82 	if (now < last)
83 		return;
84 
85 	delta = now - last;
86 
87 	if (dev->power.runtime_status == RPM_SUSPENDED)
88 		dev->power.suspended_time += delta;
89 	else
90 		dev->power.active_time += delta;
91 }
92 
93 static void __update_runtime_status(struct device *dev, enum rpm_status status)
94 {
95 	update_pm_runtime_accounting(dev);
96 	dev->power.runtime_status = status;
97 }
98 
99 static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
100 {
101 	u64 time;
102 	unsigned long flags;
103 
104 	spin_lock_irqsave(&dev->power.lock, flags);
105 
106 	update_pm_runtime_accounting(dev);
107 	time = suspended ? dev->power.suspended_time : dev->power.active_time;
108 
109 	spin_unlock_irqrestore(&dev->power.lock, flags);
110 
111 	return time;
112 }
113 
114 u64 pm_runtime_active_time(struct device *dev)
115 {
116 	return rpm_get_accounted_time(dev, false);
117 }
118 
119 u64 pm_runtime_suspended_time(struct device *dev)
120 {
121 	return rpm_get_accounted_time(dev, true);
122 }
123 EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
124 
125 /**
126  * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
127  * @dev: Device to handle.
128  */
129 static void pm_runtime_deactivate_timer(struct device *dev)
130 {
131 	if (dev->power.timer_expires > 0) {
132 		hrtimer_try_to_cancel(&dev->power.suspend_timer);
133 		dev->power.timer_expires = 0;
134 	}
135 }
136 
137 /**
138  * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
139  * @dev: Device to handle.
140  */
141 static void pm_runtime_cancel_pending(struct device *dev)
142 {
143 	pm_runtime_deactivate_timer(dev);
144 	/*
145 	 * In case there's a request pending, make sure its work function will
146 	 * return without doing anything.
147 	 */
148 	dev->power.request = RPM_REQ_NONE;
149 }
150 
151 /*
152  * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
153  * @dev: Device to handle.
154  *
155  * Compute the autosuspend-delay expiration time based on the device's
156  * power.last_busy time.  If the delay has already expired or is disabled
157  * (negative) or the power.use_autosuspend flag isn't set, return 0.
158  * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
159  *
160  * This function may be called either with or without dev->power.lock held.
161  * Either way it can be racy, since power.last_busy may be updated at any time.
162  */
163 u64 pm_runtime_autosuspend_expiration(struct device *dev)
164 {
165 	int autosuspend_delay;
166 	u64 expires;
167 
168 	if (!dev->power.use_autosuspend)
169 		return 0;
170 
171 	autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
172 	if (autosuspend_delay < 0)
173 		return 0;
174 
175 	expires  = READ_ONCE(dev->power.last_busy);
176 	expires += (u64)autosuspend_delay * NSEC_PER_MSEC;
177 	if (expires > ktime_get_mono_fast_ns())
178 		return expires;	/* Expires in the future */
179 
180 	return 0;
181 }
182 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
183 
184 static int dev_memalloc_noio(struct device *dev, void *data)
185 {
186 	return dev->power.memalloc_noio;
187 }
188 
189 /*
190  * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
191  * @dev: Device to handle.
192  * @enable: True for setting the flag and False for clearing the flag.
193  *
194  * Set the flag for all devices in the path from the device to the
195  * root device in the device tree if @enable is true, otherwise clear
196  * the flag for devices in the path whose siblings don't set the flag.
197  *
198  * The function should only be called by block device, or network
199  * device driver for solving the deadlock problem during runtime
200  * resume/suspend:
201  *
202  *     If memory allocation with GFP_KERNEL is called inside runtime
203  *     resume/suspend callback of any one of its ancestors(or the
204  *     block device itself), the deadlock may be triggered inside the
205  *     memory allocation since it might not complete until the block
206  *     device becomes active and the involed page I/O finishes. The
207  *     situation is pointed out first by Alan Stern. Network device
208  *     are involved in iSCSI kind of situation.
209  *
210  * The lock of dev_hotplug_mutex is held in the function for handling
211  * hotplug race because pm_runtime_set_memalloc_noio() may be called
212  * in async probe().
213  *
214  * The function should be called between device_add() and device_del()
215  * on the affected device(block/network device).
216  */
217 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
218 {
219 	static DEFINE_MUTEX(dev_hotplug_mutex);
220 
221 	mutex_lock(&dev_hotplug_mutex);
222 	for (;;) {
223 		bool enabled;
224 
225 		/* hold power lock since bitfield is not SMP-safe. */
226 		spin_lock_irq(&dev->power.lock);
227 		enabled = dev->power.memalloc_noio;
228 		dev->power.memalloc_noio = enable;
229 		spin_unlock_irq(&dev->power.lock);
230 
231 		/*
232 		 * not need to enable ancestors any more if the device
233 		 * has been enabled.
234 		 */
235 		if (enabled && enable)
236 			break;
237 
238 		dev = dev->parent;
239 
240 		/*
241 		 * clear flag of the parent device only if all the
242 		 * children don't set the flag because ancestor's
243 		 * flag was set by any one of the descendants.
244 		 */
245 		if (!dev || (!enable &&
246 			     device_for_each_child(dev, NULL,
247 						   dev_memalloc_noio)))
248 			break;
249 	}
250 	mutex_unlock(&dev_hotplug_mutex);
251 }
252 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
253 
254 /**
255  * rpm_check_suspend_allowed - Test whether a device may be suspended.
256  * @dev: Device to test.
257  */
258 static int rpm_check_suspend_allowed(struct device *dev)
259 {
260 	int retval = 0;
261 
262 	if (dev->power.runtime_error)
263 		retval = -EINVAL;
264 	else if (dev->power.disable_depth > 0)
265 		retval = -EACCES;
266 	else if (atomic_read(&dev->power.usage_count) > 0)
267 		retval = -EAGAIN;
268 	else if (!dev->power.ignore_children &&
269 			atomic_read(&dev->power.child_count))
270 		retval = -EBUSY;
271 
272 	/* Pending resume requests take precedence over suspends. */
273 	else if ((dev->power.deferred_resume
274 			&& dev->power.runtime_status == RPM_SUSPENDING)
275 	    || (dev->power.request_pending
276 			&& dev->power.request == RPM_REQ_RESUME))
277 		retval = -EAGAIN;
278 	else if (__dev_pm_qos_resume_latency(dev) == 0)
279 		retval = -EPERM;
280 	else if (dev->power.runtime_status == RPM_SUSPENDED)
281 		retval = 1;
282 
283 	return retval;
284 }
285 
286 static int rpm_get_suppliers(struct device *dev)
287 {
288 	struct device_link *link;
289 
290 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
291 				device_links_read_lock_held()) {
292 		int retval;
293 
294 		if (!(link->flags & DL_FLAG_PM_RUNTIME))
295 			continue;
296 
297 		retval = pm_runtime_get_sync(link->supplier);
298 		/* Ignore suppliers with disabled runtime PM. */
299 		if (retval < 0 && retval != -EACCES) {
300 			pm_runtime_put_noidle(link->supplier);
301 			return retval;
302 		}
303 		refcount_inc(&link->rpm_active);
304 	}
305 	return 0;
306 }
307 
308 /**
309  * pm_runtime_release_supplier - Drop references to device link's supplier.
310  * @link: Target device link.
311  * @check_idle: Whether or not to check if the supplier device is idle.
312  *
313  * Drop all runtime PM references associated with @link to its supplier device
314  * and if @check_idle is set, check if that device is idle (and so it can be
315  * suspended).
316  */
317 void pm_runtime_release_supplier(struct device_link *link, bool check_idle)
318 {
319 	struct device *supplier = link->supplier;
320 
321 	/*
322 	 * The additional power.usage_count check is a safety net in case
323 	 * the rpm_active refcount becomes saturated, in which case
324 	 * refcount_dec_not_one() would return true forever, but it is not
325 	 * strictly necessary.
326 	 */
327 	while (refcount_dec_not_one(&link->rpm_active) &&
328 	       atomic_read(&supplier->power.usage_count) > 0)
329 		pm_runtime_put_noidle(supplier);
330 
331 	if (check_idle)
332 		pm_request_idle(supplier);
333 }
334 
335 static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
336 {
337 	struct device_link *link;
338 
339 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
340 				device_links_read_lock_held())
341 		pm_runtime_release_supplier(link, try_to_suspend);
342 }
343 
344 static void rpm_put_suppliers(struct device *dev)
345 {
346 	__rpm_put_suppliers(dev, true);
347 }
348 
349 static void rpm_suspend_suppliers(struct device *dev)
350 {
351 	struct device_link *link;
352 	int idx = device_links_read_lock();
353 
354 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
355 				device_links_read_lock_held())
356 		pm_request_idle(link->supplier);
357 
358 	device_links_read_unlock(idx);
359 }
360 
361 /**
362  * __rpm_callback - Run a given runtime PM callback for a given device.
363  * @cb: Runtime PM callback to run.
364  * @dev: Device to run the callback for.
365  */
366 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
367 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
368 {
369 	int retval = 0, idx;
370 	bool use_links = dev->power.links_count > 0;
371 
372 	if (dev->power.irq_safe) {
373 		spin_unlock(&dev->power.lock);
374 	} else {
375 		spin_unlock_irq(&dev->power.lock);
376 
377 		/*
378 		 * Resume suppliers if necessary.
379 		 *
380 		 * The device's runtime PM status cannot change until this
381 		 * routine returns, so it is safe to read the status outside of
382 		 * the lock.
383 		 */
384 		if (use_links && dev->power.runtime_status == RPM_RESUMING) {
385 			idx = device_links_read_lock();
386 
387 			retval = rpm_get_suppliers(dev);
388 			if (retval) {
389 				rpm_put_suppliers(dev);
390 				goto fail;
391 			}
392 
393 			device_links_read_unlock(idx);
394 		}
395 	}
396 
397 	if (cb)
398 		retval = cb(dev);
399 
400 	if (dev->power.irq_safe) {
401 		spin_lock(&dev->power.lock);
402 	} else {
403 		/*
404 		 * If the device is suspending and the callback has returned
405 		 * success, drop the usage counters of the suppliers that have
406 		 * been reference counted on its resume.
407 		 *
408 		 * Do that if resume fails too.
409 		 */
410 		if (use_links
411 		    && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
412 		    || (dev->power.runtime_status == RPM_RESUMING && retval))) {
413 			idx = device_links_read_lock();
414 
415 			__rpm_put_suppliers(dev, false);
416 
417 fail:
418 			device_links_read_unlock(idx);
419 		}
420 
421 		spin_lock_irq(&dev->power.lock);
422 	}
423 
424 	return retval;
425 }
426 
427 /**
428  * rpm_idle - Notify device bus type if the device can be suspended.
429  * @dev: Device to notify the bus type about.
430  * @rpmflags: Flag bits.
431  *
432  * Check if the device's runtime PM status allows it to be suspended.  If
433  * another idle notification has been started earlier, return immediately.  If
434  * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
435  * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
436  * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
437  *
438  * This function must be called under dev->power.lock with interrupts disabled.
439  */
440 static int rpm_idle(struct device *dev, int rpmflags)
441 {
442 	int (*callback)(struct device *);
443 	int retval;
444 
445 	trace_rpm_idle_rcuidle(dev, rpmflags);
446 	retval = rpm_check_suspend_allowed(dev);
447 	if (retval < 0)
448 		;	/* Conditions are wrong. */
449 
450 	/* Idle notifications are allowed only in the RPM_ACTIVE state. */
451 	else if (dev->power.runtime_status != RPM_ACTIVE)
452 		retval = -EAGAIN;
453 
454 	/*
455 	 * Any pending request other than an idle notification takes
456 	 * precedence over us, except that the timer may be running.
457 	 */
458 	else if (dev->power.request_pending &&
459 	    dev->power.request > RPM_REQ_IDLE)
460 		retval = -EAGAIN;
461 
462 	/* Act as though RPM_NOWAIT is always set. */
463 	else if (dev->power.idle_notification)
464 		retval = -EINPROGRESS;
465 	if (retval)
466 		goto out;
467 
468 	/* Pending requests need to be canceled. */
469 	dev->power.request = RPM_REQ_NONE;
470 
471 	callback = RPM_GET_CALLBACK(dev, runtime_idle);
472 
473 	/* If no callback assume success. */
474 	if (!callback || dev->power.no_callbacks)
475 		goto out;
476 
477 	/* Carry out an asynchronous or a synchronous idle notification. */
478 	if (rpmflags & RPM_ASYNC) {
479 		dev->power.request = RPM_REQ_IDLE;
480 		if (!dev->power.request_pending) {
481 			dev->power.request_pending = true;
482 			queue_work(pm_wq, &dev->power.work);
483 		}
484 		trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0);
485 		return 0;
486 	}
487 
488 	dev->power.idle_notification = true;
489 
490 	retval = __rpm_callback(callback, dev);
491 
492 	dev->power.idle_notification = false;
493 	wake_up_all(&dev->power.wait_queue);
494 
495  out:
496 	trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
497 	return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
498 }
499 
500 /**
501  * rpm_callback - Run a given runtime PM callback for a given device.
502  * @cb: Runtime PM callback to run.
503  * @dev: Device to run the callback for.
504  */
505 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
506 {
507 	int retval;
508 
509 	if (dev->power.memalloc_noio) {
510 		unsigned int noio_flag;
511 
512 		/*
513 		 * Deadlock might be caused if memory allocation with
514 		 * GFP_KERNEL happens inside runtime_suspend and
515 		 * runtime_resume callbacks of one block device's
516 		 * ancestor or the block device itself. Network
517 		 * device might be thought as part of iSCSI block
518 		 * device, so network device and its ancestor should
519 		 * be marked as memalloc_noio too.
520 		 */
521 		noio_flag = memalloc_noio_save();
522 		retval = __rpm_callback(cb, dev);
523 		memalloc_noio_restore(noio_flag);
524 	} else {
525 		retval = __rpm_callback(cb, dev);
526 	}
527 
528 	dev->power.runtime_error = retval;
529 	return retval != -EACCES ? retval : -EIO;
530 }
531 
532 /**
533  * rpm_suspend - Carry out runtime suspend of given device.
534  * @dev: Device to suspend.
535  * @rpmflags: Flag bits.
536  *
537  * Check if the device's runtime PM status allows it to be suspended.
538  * Cancel a pending idle notification, autosuspend or suspend. If
539  * another suspend has been started earlier, either return immediately
540  * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
541  * flags. If the RPM_ASYNC flag is set then queue a suspend request;
542  * otherwise run the ->runtime_suspend() callback directly. When
543  * ->runtime_suspend succeeded, if a deferred resume was requested while
544  * the callback was running then carry it out, otherwise send an idle
545  * notification for its parent (if the suspend succeeded and both
546  * ignore_children of parent->power and irq_safe of dev->power are not set).
547  * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
548  * flag is set and the next autosuspend-delay expiration time is in the
549  * future, schedule another autosuspend attempt.
550  *
551  * This function must be called under dev->power.lock with interrupts disabled.
552  */
553 static int rpm_suspend(struct device *dev, int rpmflags)
554 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
555 {
556 	int (*callback)(struct device *);
557 	struct device *parent = NULL;
558 	int retval;
559 
560 	trace_rpm_suspend_rcuidle(dev, rpmflags);
561 
562  repeat:
563 	retval = rpm_check_suspend_allowed(dev);
564 	if (retval < 0)
565 		goto out;	/* Conditions are wrong. */
566 
567 	/* Synchronous suspends are not allowed in the RPM_RESUMING state. */
568 	if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
569 		retval = -EAGAIN;
570 	if (retval)
571 		goto out;
572 
573 	/* If the autosuspend_delay time hasn't expired yet, reschedule. */
574 	if ((rpmflags & RPM_AUTO)
575 	    && dev->power.runtime_status != RPM_SUSPENDING) {
576 		u64 expires = pm_runtime_autosuspend_expiration(dev);
577 
578 		if (expires != 0) {
579 			/* Pending requests need to be canceled. */
580 			dev->power.request = RPM_REQ_NONE;
581 
582 			/*
583 			 * Optimization: If the timer is already running and is
584 			 * set to expire at or before the autosuspend delay,
585 			 * avoid the overhead of resetting it.  Just let it
586 			 * expire; pm_suspend_timer_fn() will take care of the
587 			 * rest.
588 			 */
589 			if (!(dev->power.timer_expires &&
590 					dev->power.timer_expires <= expires)) {
591 				/*
592 				 * We add a slack of 25% to gather wakeups
593 				 * without sacrificing the granularity.
594 				 */
595 				u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
596 						    (NSEC_PER_MSEC >> 2);
597 
598 				dev->power.timer_expires = expires;
599 				hrtimer_start_range_ns(&dev->power.suspend_timer,
600 						ns_to_ktime(expires),
601 						slack,
602 						HRTIMER_MODE_ABS);
603 			}
604 			dev->power.timer_autosuspends = 1;
605 			goto out;
606 		}
607 	}
608 
609 	/* Other scheduled or pending requests need to be canceled. */
610 	pm_runtime_cancel_pending(dev);
611 
612 	if (dev->power.runtime_status == RPM_SUSPENDING) {
613 		DEFINE_WAIT(wait);
614 
615 		if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
616 			retval = -EINPROGRESS;
617 			goto out;
618 		}
619 
620 		if (dev->power.irq_safe) {
621 			spin_unlock(&dev->power.lock);
622 
623 			cpu_relax();
624 
625 			spin_lock(&dev->power.lock);
626 			goto repeat;
627 		}
628 
629 		/* Wait for the other suspend running in parallel with us. */
630 		for (;;) {
631 			prepare_to_wait(&dev->power.wait_queue, &wait,
632 					TASK_UNINTERRUPTIBLE);
633 			if (dev->power.runtime_status != RPM_SUSPENDING)
634 				break;
635 
636 			spin_unlock_irq(&dev->power.lock);
637 
638 			schedule();
639 
640 			spin_lock_irq(&dev->power.lock);
641 		}
642 		finish_wait(&dev->power.wait_queue, &wait);
643 		goto repeat;
644 	}
645 
646 	if (dev->power.no_callbacks)
647 		goto no_callback;	/* Assume success. */
648 
649 	/* Carry out an asynchronous or a synchronous suspend. */
650 	if (rpmflags & RPM_ASYNC) {
651 		dev->power.request = (rpmflags & RPM_AUTO) ?
652 		    RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
653 		if (!dev->power.request_pending) {
654 			dev->power.request_pending = true;
655 			queue_work(pm_wq, &dev->power.work);
656 		}
657 		goto out;
658 	}
659 
660 	__update_runtime_status(dev, RPM_SUSPENDING);
661 
662 	callback = RPM_GET_CALLBACK(dev, runtime_suspend);
663 
664 	dev_pm_enable_wake_irq_check(dev, true);
665 	retval = rpm_callback(callback, dev);
666 	if (retval)
667 		goto fail;
668 
669 	dev_pm_enable_wake_irq_complete(dev);
670 
671  no_callback:
672 	__update_runtime_status(dev, RPM_SUSPENDED);
673 	pm_runtime_deactivate_timer(dev);
674 
675 	if (dev->parent) {
676 		parent = dev->parent;
677 		atomic_add_unless(&parent->power.child_count, -1, 0);
678 	}
679 	wake_up_all(&dev->power.wait_queue);
680 
681 	if (dev->power.deferred_resume) {
682 		dev->power.deferred_resume = false;
683 		rpm_resume(dev, 0);
684 		retval = -EAGAIN;
685 		goto out;
686 	}
687 
688 	if (dev->power.irq_safe)
689 		goto out;
690 
691 	/* Maybe the parent is now able to suspend. */
692 	if (parent && !parent->power.ignore_children) {
693 		spin_unlock(&dev->power.lock);
694 
695 		spin_lock(&parent->power.lock);
696 		rpm_idle(parent, RPM_ASYNC);
697 		spin_unlock(&parent->power.lock);
698 
699 		spin_lock(&dev->power.lock);
700 	}
701 	/* Maybe the suppliers are now able to suspend. */
702 	if (dev->power.links_count > 0) {
703 		spin_unlock_irq(&dev->power.lock);
704 
705 		rpm_suspend_suppliers(dev);
706 
707 		spin_lock_irq(&dev->power.lock);
708 	}
709 
710  out:
711 	trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
712 
713 	return retval;
714 
715  fail:
716 	dev_pm_disable_wake_irq_check(dev, true);
717 	__update_runtime_status(dev, RPM_ACTIVE);
718 	dev->power.deferred_resume = false;
719 	wake_up_all(&dev->power.wait_queue);
720 
721 	if (retval == -EAGAIN || retval == -EBUSY) {
722 		dev->power.runtime_error = 0;
723 
724 		/*
725 		 * If the callback routine failed an autosuspend, and
726 		 * if the last_busy time has been updated so that there
727 		 * is a new autosuspend expiration time, automatically
728 		 * reschedule another autosuspend.
729 		 */
730 		if ((rpmflags & RPM_AUTO) &&
731 		    pm_runtime_autosuspend_expiration(dev) != 0)
732 			goto repeat;
733 	} else {
734 		pm_runtime_cancel_pending(dev);
735 	}
736 	goto out;
737 }
738 
739 /**
740  * rpm_resume - Carry out runtime resume of given device.
741  * @dev: Device to resume.
742  * @rpmflags: Flag bits.
743  *
744  * Check if the device's runtime PM status allows it to be resumed.  Cancel
745  * any scheduled or pending requests.  If another resume has been started
746  * earlier, either return immediately or wait for it to finish, depending on the
747  * RPM_NOWAIT and RPM_ASYNC flags.  Similarly, if there's a suspend running in
748  * parallel with this function, either tell the other process to resume after
749  * suspending (deferred_resume) or wait for it to finish.  If the RPM_ASYNC
750  * flag is set then queue a resume request; otherwise run the
751  * ->runtime_resume() callback directly.  Queue an idle notification for the
752  * device if the resume succeeded.
753  *
754  * This function must be called under dev->power.lock with interrupts disabled.
755  */
756 static int rpm_resume(struct device *dev, int rpmflags)
757 	__releases(&dev->power.lock) __acquires(&dev->power.lock)
758 {
759 	int (*callback)(struct device *);
760 	struct device *parent = NULL;
761 	int retval = 0;
762 
763 	trace_rpm_resume_rcuidle(dev, rpmflags);
764 
765  repeat:
766 	if (dev->power.runtime_error) {
767 		retval = -EINVAL;
768 	} else if (dev->power.disable_depth > 0) {
769 		if (dev->power.runtime_status == RPM_ACTIVE &&
770 		    dev->power.last_status == RPM_ACTIVE)
771 			retval = 1;
772 		else
773 			retval = -EACCES;
774 	}
775 	if (retval)
776 		goto out;
777 
778 	/*
779 	 * Other scheduled or pending requests need to be canceled.  Small
780 	 * optimization: If an autosuspend timer is running, leave it running
781 	 * rather than cancelling it now only to restart it again in the near
782 	 * future.
783 	 */
784 	dev->power.request = RPM_REQ_NONE;
785 	if (!dev->power.timer_autosuspends)
786 		pm_runtime_deactivate_timer(dev);
787 
788 	if (dev->power.runtime_status == RPM_ACTIVE) {
789 		retval = 1;
790 		goto out;
791 	}
792 
793 	if (dev->power.runtime_status == RPM_RESUMING
794 	    || dev->power.runtime_status == RPM_SUSPENDING) {
795 		DEFINE_WAIT(wait);
796 
797 		if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
798 			if (dev->power.runtime_status == RPM_SUSPENDING)
799 				dev->power.deferred_resume = true;
800 			else
801 				retval = -EINPROGRESS;
802 			goto out;
803 		}
804 
805 		if (dev->power.irq_safe) {
806 			spin_unlock(&dev->power.lock);
807 
808 			cpu_relax();
809 
810 			spin_lock(&dev->power.lock);
811 			goto repeat;
812 		}
813 
814 		/* Wait for the operation carried out in parallel with us. */
815 		for (;;) {
816 			prepare_to_wait(&dev->power.wait_queue, &wait,
817 					TASK_UNINTERRUPTIBLE);
818 			if (dev->power.runtime_status != RPM_RESUMING
819 			    && dev->power.runtime_status != RPM_SUSPENDING)
820 				break;
821 
822 			spin_unlock_irq(&dev->power.lock);
823 
824 			schedule();
825 
826 			spin_lock_irq(&dev->power.lock);
827 		}
828 		finish_wait(&dev->power.wait_queue, &wait);
829 		goto repeat;
830 	}
831 
832 	/*
833 	 * See if we can skip waking up the parent.  This is safe only if
834 	 * power.no_callbacks is set, because otherwise we don't know whether
835 	 * the resume will actually succeed.
836 	 */
837 	if (dev->power.no_callbacks && !parent && dev->parent) {
838 		spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
839 		if (dev->parent->power.disable_depth > 0
840 		    || dev->parent->power.ignore_children
841 		    || dev->parent->power.runtime_status == RPM_ACTIVE) {
842 			atomic_inc(&dev->parent->power.child_count);
843 			spin_unlock(&dev->parent->power.lock);
844 			retval = 1;
845 			goto no_callback;	/* Assume success. */
846 		}
847 		spin_unlock(&dev->parent->power.lock);
848 	}
849 
850 	/* Carry out an asynchronous or a synchronous resume. */
851 	if (rpmflags & RPM_ASYNC) {
852 		dev->power.request = RPM_REQ_RESUME;
853 		if (!dev->power.request_pending) {
854 			dev->power.request_pending = true;
855 			queue_work(pm_wq, &dev->power.work);
856 		}
857 		retval = 0;
858 		goto out;
859 	}
860 
861 	if (!parent && dev->parent) {
862 		/*
863 		 * Increment the parent's usage counter and resume it if
864 		 * necessary.  Not needed if dev is irq-safe; then the
865 		 * parent is permanently resumed.
866 		 */
867 		parent = dev->parent;
868 		if (dev->power.irq_safe)
869 			goto skip_parent;
870 		spin_unlock(&dev->power.lock);
871 
872 		pm_runtime_get_noresume(parent);
873 
874 		spin_lock(&parent->power.lock);
875 		/*
876 		 * Resume the parent if it has runtime PM enabled and not been
877 		 * set to ignore its children.
878 		 */
879 		if (!parent->power.disable_depth
880 		    && !parent->power.ignore_children) {
881 			rpm_resume(parent, 0);
882 			if (parent->power.runtime_status != RPM_ACTIVE)
883 				retval = -EBUSY;
884 		}
885 		spin_unlock(&parent->power.lock);
886 
887 		spin_lock(&dev->power.lock);
888 		if (retval)
889 			goto out;
890 		goto repeat;
891 	}
892  skip_parent:
893 
894 	if (dev->power.no_callbacks)
895 		goto no_callback;	/* Assume success. */
896 
897 	__update_runtime_status(dev, RPM_RESUMING);
898 
899 	callback = RPM_GET_CALLBACK(dev, runtime_resume);
900 
901 	dev_pm_disable_wake_irq_check(dev, false);
902 	retval = rpm_callback(callback, dev);
903 	if (retval) {
904 		__update_runtime_status(dev, RPM_SUSPENDED);
905 		pm_runtime_cancel_pending(dev);
906 		dev_pm_enable_wake_irq_check(dev, false);
907 	} else {
908  no_callback:
909 		__update_runtime_status(dev, RPM_ACTIVE);
910 		pm_runtime_mark_last_busy(dev);
911 		if (parent)
912 			atomic_inc(&parent->power.child_count);
913 	}
914 	wake_up_all(&dev->power.wait_queue);
915 
916 	if (retval >= 0)
917 		rpm_idle(dev, RPM_ASYNC);
918 
919  out:
920 	if (parent && !dev->power.irq_safe) {
921 		spin_unlock_irq(&dev->power.lock);
922 
923 		pm_runtime_put(parent);
924 
925 		spin_lock_irq(&dev->power.lock);
926 	}
927 
928 	trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
929 
930 	return retval;
931 }
932 
933 /**
934  * pm_runtime_work - Universal runtime PM work function.
935  * @work: Work structure used for scheduling the execution of this function.
936  *
937  * Use @work to get the device object the work is to be done for, determine what
938  * is to be done and execute the appropriate runtime PM function.
939  */
940 static void pm_runtime_work(struct work_struct *work)
941 {
942 	struct device *dev = container_of(work, struct device, power.work);
943 	enum rpm_request req;
944 
945 	spin_lock_irq(&dev->power.lock);
946 
947 	if (!dev->power.request_pending)
948 		goto out;
949 
950 	req = dev->power.request;
951 	dev->power.request = RPM_REQ_NONE;
952 	dev->power.request_pending = false;
953 
954 	switch (req) {
955 	case RPM_REQ_NONE:
956 		break;
957 	case RPM_REQ_IDLE:
958 		rpm_idle(dev, RPM_NOWAIT);
959 		break;
960 	case RPM_REQ_SUSPEND:
961 		rpm_suspend(dev, RPM_NOWAIT);
962 		break;
963 	case RPM_REQ_AUTOSUSPEND:
964 		rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
965 		break;
966 	case RPM_REQ_RESUME:
967 		rpm_resume(dev, RPM_NOWAIT);
968 		break;
969 	}
970 
971  out:
972 	spin_unlock_irq(&dev->power.lock);
973 }
974 
975 /**
976  * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
977  * @timer: hrtimer used by pm_schedule_suspend().
978  *
979  * Check if the time is right and queue a suspend request.
980  */
981 static enum hrtimer_restart  pm_suspend_timer_fn(struct hrtimer *timer)
982 {
983 	struct device *dev = container_of(timer, struct device, power.suspend_timer);
984 	unsigned long flags;
985 	u64 expires;
986 
987 	spin_lock_irqsave(&dev->power.lock, flags);
988 
989 	expires = dev->power.timer_expires;
990 	/*
991 	 * If 'expires' is after the current time, we've been called
992 	 * too early.
993 	 */
994 	if (expires > 0 && expires < ktime_get_mono_fast_ns()) {
995 		dev->power.timer_expires = 0;
996 		rpm_suspend(dev, dev->power.timer_autosuspends ?
997 		    (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
998 	}
999 
1000 	spin_unlock_irqrestore(&dev->power.lock, flags);
1001 
1002 	return HRTIMER_NORESTART;
1003 }
1004 
1005 /**
1006  * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1007  * @dev: Device to suspend.
1008  * @delay: Time to wait before submitting a suspend request, in milliseconds.
1009  */
1010 int pm_schedule_suspend(struct device *dev, unsigned int delay)
1011 {
1012 	unsigned long flags;
1013 	u64 expires;
1014 	int retval;
1015 
1016 	spin_lock_irqsave(&dev->power.lock, flags);
1017 
1018 	if (!delay) {
1019 		retval = rpm_suspend(dev, RPM_ASYNC);
1020 		goto out;
1021 	}
1022 
1023 	retval = rpm_check_suspend_allowed(dev);
1024 	if (retval)
1025 		goto out;
1026 
1027 	/* Other scheduled or pending requests need to be canceled. */
1028 	pm_runtime_cancel_pending(dev);
1029 
1030 	expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
1031 	dev->power.timer_expires = expires;
1032 	dev->power.timer_autosuspends = 0;
1033 	hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
1034 
1035  out:
1036 	spin_unlock_irqrestore(&dev->power.lock, flags);
1037 
1038 	return retval;
1039 }
1040 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
1041 
1042 /**
1043  * __pm_runtime_idle - Entry point for runtime idle operations.
1044  * @dev: Device to send idle notification for.
1045  * @rpmflags: Flag bits.
1046  *
1047  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1048  * return immediately if it is larger than zero.  Then carry out an idle
1049  * notification, either synchronous or asynchronous.
1050  *
1051  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1052  * or if pm_runtime_irq_safe() has been called.
1053  */
1054 int __pm_runtime_idle(struct device *dev, int rpmflags)
1055 {
1056 	unsigned long flags;
1057 	int retval;
1058 
1059 	if (rpmflags & RPM_GET_PUT) {
1060 		if (!atomic_dec_and_test(&dev->power.usage_count)) {
1061 			trace_rpm_usage_rcuidle(dev, rpmflags);
1062 			return 0;
1063 		}
1064 	}
1065 
1066 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1067 
1068 	spin_lock_irqsave(&dev->power.lock, flags);
1069 	retval = rpm_idle(dev, rpmflags);
1070 	spin_unlock_irqrestore(&dev->power.lock, flags);
1071 
1072 	return retval;
1073 }
1074 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
1075 
1076 /**
1077  * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1078  * @dev: Device to suspend.
1079  * @rpmflags: Flag bits.
1080  *
1081  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1082  * return immediately if it is larger than zero.  Then carry out a suspend,
1083  * either synchronous or asynchronous.
1084  *
1085  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1086  * or if pm_runtime_irq_safe() has been called.
1087  */
1088 int __pm_runtime_suspend(struct device *dev, int rpmflags)
1089 {
1090 	unsigned long flags;
1091 	int retval;
1092 
1093 	if (rpmflags & RPM_GET_PUT) {
1094 		if (!atomic_dec_and_test(&dev->power.usage_count)) {
1095 			trace_rpm_usage_rcuidle(dev, rpmflags);
1096 			return 0;
1097 		}
1098 	}
1099 
1100 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1101 
1102 	spin_lock_irqsave(&dev->power.lock, flags);
1103 	retval = rpm_suspend(dev, rpmflags);
1104 	spin_unlock_irqrestore(&dev->power.lock, flags);
1105 
1106 	return retval;
1107 }
1108 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
1109 
1110 /**
1111  * __pm_runtime_resume - Entry point for runtime resume operations.
1112  * @dev: Device to resume.
1113  * @rpmflags: Flag bits.
1114  *
1115  * If the RPM_GET_PUT flag is set, increment the device's usage count.  Then
1116  * carry out a resume, either synchronous or asynchronous.
1117  *
1118  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1119  * or if pm_runtime_irq_safe() has been called.
1120  */
1121 int __pm_runtime_resume(struct device *dev, int rpmflags)
1122 {
1123 	unsigned long flags;
1124 	int retval;
1125 
1126 	might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1127 			dev->power.runtime_status != RPM_ACTIVE);
1128 
1129 	if (rpmflags & RPM_GET_PUT)
1130 		atomic_inc(&dev->power.usage_count);
1131 
1132 	spin_lock_irqsave(&dev->power.lock, flags);
1133 	retval = rpm_resume(dev, rpmflags);
1134 	spin_unlock_irqrestore(&dev->power.lock, flags);
1135 
1136 	return retval;
1137 }
1138 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
1139 
1140 /**
1141  * pm_runtime_get_if_active - Conditionally bump up device usage counter.
1142  * @dev: Device to handle.
1143  * @ign_usage_count: Whether or not to look at the current usage counter value.
1144  *
1145  * Return -EINVAL if runtime PM is disabled for @dev.
1146  *
1147  * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either
1148  * @ign_usage_count is %true or the runtime PM usage counter of @dev is not
1149  * zero, increment the usage counter of @dev and return 1. Otherwise, return 0
1150  * without changing the usage counter.
1151  *
1152  * If @ign_usage_count is %true, this function can be used to prevent suspending
1153  * the device when its runtime PM status is %RPM_ACTIVE.
1154  *
1155  * If @ign_usage_count is %false, this function can be used to prevent
1156  * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
1157  * runtime PM usage counter is not zero.
1158  *
1159  * The caller is responsible for decrementing the runtime PM usage counter of
1160  * @dev after this function has returned a positive value for it.
1161  */
1162 int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count)
1163 {
1164 	unsigned long flags;
1165 	int retval;
1166 
1167 	spin_lock_irqsave(&dev->power.lock, flags);
1168 	if (dev->power.disable_depth > 0) {
1169 		retval = -EINVAL;
1170 	} else if (dev->power.runtime_status != RPM_ACTIVE) {
1171 		retval = 0;
1172 	} else if (ign_usage_count) {
1173 		retval = 1;
1174 		atomic_inc(&dev->power.usage_count);
1175 	} else {
1176 		retval = atomic_inc_not_zero(&dev->power.usage_count);
1177 	}
1178 	trace_rpm_usage_rcuidle(dev, 0);
1179 	spin_unlock_irqrestore(&dev->power.lock, flags);
1180 
1181 	return retval;
1182 }
1183 EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
1184 
1185 /**
1186  * __pm_runtime_set_status - Set runtime PM status of a device.
1187  * @dev: Device to handle.
1188  * @status: New runtime PM status of the device.
1189  *
1190  * If runtime PM of the device is disabled or its power.runtime_error field is
1191  * different from zero, the status may be changed either to RPM_ACTIVE, or to
1192  * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1193  * However, if the device has a parent and the parent is not active, and the
1194  * parent's power.ignore_children flag is unset, the device's status cannot be
1195  * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1196  *
1197  * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1198  * and the device parent's counter of unsuspended children is modified to
1199  * reflect the new status.  If the new status is RPM_SUSPENDED, an idle
1200  * notification request for the parent is submitted.
1201  *
1202  * If @dev has any suppliers (as reflected by device links to them), and @status
1203  * is RPM_ACTIVE, they will be activated upfront and if the activation of one
1204  * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
1205  * of the @status value) and the suppliers will be deacticated on exit.  The
1206  * error returned by the failing supplier activation will be returned in that
1207  * case.
1208  */
1209 int __pm_runtime_set_status(struct device *dev, unsigned int status)
1210 {
1211 	struct device *parent = dev->parent;
1212 	bool notify_parent = false;
1213 	int error = 0;
1214 
1215 	if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1216 		return -EINVAL;
1217 
1218 	spin_lock_irq(&dev->power.lock);
1219 
1220 	/*
1221 	 * Prevent PM-runtime from being enabled for the device or return an
1222 	 * error if it is enabled already and working.
1223 	 */
1224 	if (dev->power.runtime_error || dev->power.disable_depth)
1225 		dev->power.disable_depth++;
1226 	else
1227 		error = -EAGAIN;
1228 
1229 	spin_unlock_irq(&dev->power.lock);
1230 
1231 	if (error)
1232 		return error;
1233 
1234 	/*
1235 	 * If the new status is RPM_ACTIVE, the suppliers can be activated
1236 	 * upfront regardless of the current status, because next time
1237 	 * rpm_put_suppliers() runs, the rpm_active refcounts of the links
1238 	 * involved will be dropped down to one anyway.
1239 	 */
1240 	if (status == RPM_ACTIVE) {
1241 		int idx = device_links_read_lock();
1242 
1243 		error = rpm_get_suppliers(dev);
1244 		if (error)
1245 			status = RPM_SUSPENDED;
1246 
1247 		device_links_read_unlock(idx);
1248 	}
1249 
1250 	spin_lock_irq(&dev->power.lock);
1251 
1252 	if (dev->power.runtime_status == status || !parent)
1253 		goto out_set;
1254 
1255 	if (status == RPM_SUSPENDED) {
1256 		atomic_add_unless(&parent->power.child_count, -1, 0);
1257 		notify_parent = !parent->power.ignore_children;
1258 	} else {
1259 		spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1260 
1261 		/*
1262 		 * It is invalid to put an active child under a parent that is
1263 		 * not active, has runtime PM enabled and the
1264 		 * 'power.ignore_children' flag unset.
1265 		 */
1266 		if (!parent->power.disable_depth
1267 		    && !parent->power.ignore_children
1268 		    && parent->power.runtime_status != RPM_ACTIVE) {
1269 			dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1270 				dev_name(dev),
1271 				dev_name(parent));
1272 			error = -EBUSY;
1273 		} else if (dev->power.runtime_status == RPM_SUSPENDED) {
1274 			atomic_inc(&parent->power.child_count);
1275 		}
1276 
1277 		spin_unlock(&parent->power.lock);
1278 
1279 		if (error) {
1280 			status = RPM_SUSPENDED;
1281 			goto out;
1282 		}
1283 	}
1284 
1285  out_set:
1286 	__update_runtime_status(dev, status);
1287 	if (!error)
1288 		dev->power.runtime_error = 0;
1289 
1290  out:
1291 	spin_unlock_irq(&dev->power.lock);
1292 
1293 	if (notify_parent)
1294 		pm_request_idle(parent);
1295 
1296 	if (status == RPM_SUSPENDED) {
1297 		int idx = device_links_read_lock();
1298 
1299 		rpm_put_suppliers(dev);
1300 
1301 		device_links_read_unlock(idx);
1302 	}
1303 
1304 	pm_runtime_enable(dev);
1305 
1306 	return error;
1307 }
1308 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1309 
1310 /**
1311  * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1312  * @dev: Device to handle.
1313  *
1314  * Flush all pending requests for the device from pm_wq and wait for all
1315  * runtime PM operations involving the device in progress to complete.
1316  *
1317  * Should be called under dev->power.lock with interrupts disabled.
1318  */
1319 static void __pm_runtime_barrier(struct device *dev)
1320 {
1321 	pm_runtime_deactivate_timer(dev);
1322 
1323 	if (dev->power.request_pending) {
1324 		dev->power.request = RPM_REQ_NONE;
1325 		spin_unlock_irq(&dev->power.lock);
1326 
1327 		cancel_work_sync(&dev->power.work);
1328 
1329 		spin_lock_irq(&dev->power.lock);
1330 		dev->power.request_pending = false;
1331 	}
1332 
1333 	if (dev->power.runtime_status == RPM_SUSPENDING
1334 	    || dev->power.runtime_status == RPM_RESUMING
1335 	    || dev->power.idle_notification) {
1336 		DEFINE_WAIT(wait);
1337 
1338 		/* Suspend, wake-up or idle notification in progress. */
1339 		for (;;) {
1340 			prepare_to_wait(&dev->power.wait_queue, &wait,
1341 					TASK_UNINTERRUPTIBLE);
1342 			if (dev->power.runtime_status != RPM_SUSPENDING
1343 			    && dev->power.runtime_status != RPM_RESUMING
1344 			    && !dev->power.idle_notification)
1345 				break;
1346 			spin_unlock_irq(&dev->power.lock);
1347 
1348 			schedule();
1349 
1350 			spin_lock_irq(&dev->power.lock);
1351 		}
1352 		finish_wait(&dev->power.wait_queue, &wait);
1353 	}
1354 }
1355 
1356 /**
1357  * pm_runtime_barrier - Flush pending requests and wait for completions.
1358  * @dev: Device to handle.
1359  *
1360  * Prevent the device from being suspended by incrementing its usage counter and
1361  * if there's a pending resume request for the device, wake the device up.
1362  * Next, make sure that all pending requests for the device have been flushed
1363  * from pm_wq and wait for all runtime PM operations involving the device in
1364  * progress to complete.
1365  *
1366  * Return value:
1367  * 1, if there was a resume request pending and the device had to be woken up,
1368  * 0, otherwise
1369  */
1370 int pm_runtime_barrier(struct device *dev)
1371 {
1372 	int retval = 0;
1373 
1374 	pm_runtime_get_noresume(dev);
1375 	spin_lock_irq(&dev->power.lock);
1376 
1377 	if (dev->power.request_pending
1378 	    && dev->power.request == RPM_REQ_RESUME) {
1379 		rpm_resume(dev, 0);
1380 		retval = 1;
1381 	}
1382 
1383 	__pm_runtime_barrier(dev);
1384 
1385 	spin_unlock_irq(&dev->power.lock);
1386 	pm_runtime_put_noidle(dev);
1387 
1388 	return retval;
1389 }
1390 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1391 
1392 /**
1393  * __pm_runtime_disable - Disable runtime PM of a device.
1394  * @dev: Device to handle.
1395  * @check_resume: If set, check if there's a resume request for the device.
1396  *
1397  * Increment power.disable_depth for the device and if it was zero previously,
1398  * cancel all pending runtime PM requests for the device and wait for all
1399  * operations in progress to complete.  The device can be either active or
1400  * suspended after its runtime PM has been disabled.
1401  *
1402  * If @check_resume is set and there's a resume request pending when
1403  * __pm_runtime_disable() is called and power.disable_depth is zero, the
1404  * function will wake up the device before disabling its runtime PM.
1405  */
1406 void __pm_runtime_disable(struct device *dev, bool check_resume)
1407 {
1408 	spin_lock_irq(&dev->power.lock);
1409 
1410 	if (dev->power.disable_depth > 0) {
1411 		dev->power.disable_depth++;
1412 		goto out;
1413 	}
1414 
1415 	/*
1416 	 * Wake up the device if there's a resume request pending, because that
1417 	 * means there probably is some I/O to process and disabling runtime PM
1418 	 * shouldn't prevent the device from processing the I/O.
1419 	 */
1420 	if (check_resume && dev->power.request_pending
1421 	    && dev->power.request == RPM_REQ_RESUME) {
1422 		/*
1423 		 * Prevent suspends and idle notifications from being carried
1424 		 * out after we have woken up the device.
1425 		 */
1426 		pm_runtime_get_noresume(dev);
1427 
1428 		rpm_resume(dev, 0);
1429 
1430 		pm_runtime_put_noidle(dev);
1431 	}
1432 
1433 	/* Update time accounting before disabling PM-runtime. */
1434 	update_pm_runtime_accounting(dev);
1435 
1436 	if (!dev->power.disable_depth++) {
1437 		__pm_runtime_barrier(dev);
1438 		dev->power.last_status = dev->power.runtime_status;
1439 	}
1440 
1441  out:
1442 	spin_unlock_irq(&dev->power.lock);
1443 }
1444 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1445 
1446 /**
1447  * pm_runtime_enable - Enable runtime PM of a device.
1448  * @dev: Device to handle.
1449  */
1450 void pm_runtime_enable(struct device *dev)
1451 {
1452 	unsigned long flags;
1453 
1454 	spin_lock_irqsave(&dev->power.lock, flags);
1455 
1456 	if (!dev->power.disable_depth) {
1457 		dev_warn(dev, "Unbalanced %s!\n", __func__);
1458 		goto out;
1459 	}
1460 
1461 	if (--dev->power.disable_depth > 0)
1462 		goto out;
1463 
1464 	dev->power.last_status = RPM_INVALID;
1465 	dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
1466 
1467 	if (dev->power.runtime_status == RPM_SUSPENDED &&
1468 	    !dev->power.ignore_children &&
1469 	    atomic_read(&dev->power.child_count) > 0)
1470 		dev_warn(dev, "Enabling runtime PM for inactive device with active children\n");
1471 
1472 out:
1473 	spin_unlock_irqrestore(&dev->power.lock, flags);
1474 }
1475 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1476 
1477 static void pm_runtime_disable_action(void *data)
1478 {
1479 	pm_runtime_disable(data);
1480 }
1481 
1482 /**
1483  * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable.
1484  * @dev: Device to handle.
1485  */
1486 int devm_pm_runtime_enable(struct device *dev)
1487 {
1488 	pm_runtime_enable(dev);
1489 
1490 	return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev);
1491 }
1492 EXPORT_SYMBOL_GPL(devm_pm_runtime_enable);
1493 
1494 /**
1495  * pm_runtime_forbid - Block runtime PM of a device.
1496  * @dev: Device to handle.
1497  *
1498  * Increase the device's usage count and clear its power.runtime_auto flag,
1499  * so that it cannot be suspended at run time until pm_runtime_allow() is called
1500  * for it.
1501  */
1502 void pm_runtime_forbid(struct device *dev)
1503 {
1504 	spin_lock_irq(&dev->power.lock);
1505 	if (!dev->power.runtime_auto)
1506 		goto out;
1507 
1508 	dev->power.runtime_auto = false;
1509 	atomic_inc(&dev->power.usage_count);
1510 	rpm_resume(dev, 0);
1511 
1512  out:
1513 	spin_unlock_irq(&dev->power.lock);
1514 }
1515 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1516 
1517 /**
1518  * pm_runtime_allow - Unblock runtime PM of a device.
1519  * @dev: Device to handle.
1520  *
1521  * Decrease the device's usage count and set its power.runtime_auto flag.
1522  */
1523 void pm_runtime_allow(struct device *dev)
1524 {
1525 	spin_lock_irq(&dev->power.lock);
1526 	if (dev->power.runtime_auto)
1527 		goto out;
1528 
1529 	dev->power.runtime_auto = true;
1530 	if (atomic_dec_and_test(&dev->power.usage_count))
1531 		rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
1532 	else
1533 		trace_rpm_usage_rcuidle(dev, RPM_AUTO | RPM_ASYNC);
1534 
1535  out:
1536 	spin_unlock_irq(&dev->power.lock);
1537 }
1538 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1539 
1540 /**
1541  * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1542  * @dev: Device to handle.
1543  *
1544  * Set the power.no_callbacks flag, which tells the PM core that this
1545  * device is power-managed through its parent and has no runtime PM
1546  * callbacks of its own.  The runtime sysfs attributes will be removed.
1547  */
1548 void pm_runtime_no_callbacks(struct device *dev)
1549 {
1550 	spin_lock_irq(&dev->power.lock);
1551 	dev->power.no_callbacks = 1;
1552 	spin_unlock_irq(&dev->power.lock);
1553 	if (device_is_registered(dev))
1554 		rpm_sysfs_remove(dev);
1555 }
1556 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1557 
1558 /**
1559  * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1560  * @dev: Device to handle
1561  *
1562  * Set the power.irq_safe flag, which tells the PM core that the
1563  * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1564  * always be invoked with the spinlock held and interrupts disabled.  It also
1565  * causes the parent's usage counter to be permanently incremented, preventing
1566  * the parent from runtime suspending -- otherwise an irq-safe child might have
1567  * to wait for a non-irq-safe parent.
1568  */
1569 void pm_runtime_irq_safe(struct device *dev)
1570 {
1571 	if (dev->parent)
1572 		pm_runtime_get_sync(dev->parent);
1573 	spin_lock_irq(&dev->power.lock);
1574 	dev->power.irq_safe = 1;
1575 	spin_unlock_irq(&dev->power.lock);
1576 }
1577 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1578 
1579 /**
1580  * update_autosuspend - Handle a change to a device's autosuspend settings.
1581  * @dev: Device to handle.
1582  * @old_delay: The former autosuspend_delay value.
1583  * @old_use: The former use_autosuspend value.
1584  *
1585  * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1586  * set; otherwise allow it.  Send an idle notification if suspends are allowed.
1587  *
1588  * This function must be called under dev->power.lock with interrupts disabled.
1589  */
1590 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1591 {
1592 	int delay = dev->power.autosuspend_delay;
1593 
1594 	/* Should runtime suspend be prevented now? */
1595 	if (dev->power.use_autosuspend && delay < 0) {
1596 
1597 		/* If it used to be allowed then prevent it. */
1598 		if (!old_use || old_delay >= 0) {
1599 			atomic_inc(&dev->power.usage_count);
1600 			rpm_resume(dev, 0);
1601 		} else {
1602 			trace_rpm_usage_rcuidle(dev, 0);
1603 		}
1604 	}
1605 
1606 	/* Runtime suspend should be allowed now. */
1607 	else {
1608 
1609 		/* If it used to be prevented then allow it. */
1610 		if (old_use && old_delay < 0)
1611 			atomic_dec(&dev->power.usage_count);
1612 
1613 		/* Maybe we can autosuspend now. */
1614 		rpm_idle(dev, RPM_AUTO);
1615 	}
1616 }
1617 
1618 /**
1619  * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1620  * @dev: Device to handle.
1621  * @delay: Value of the new delay in milliseconds.
1622  *
1623  * Set the device's power.autosuspend_delay value.  If it changes to negative
1624  * and the power.use_autosuspend flag is set, prevent runtime suspends.  If it
1625  * changes the other way, allow runtime suspends.
1626  */
1627 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1628 {
1629 	int old_delay, old_use;
1630 
1631 	spin_lock_irq(&dev->power.lock);
1632 	old_delay = dev->power.autosuspend_delay;
1633 	old_use = dev->power.use_autosuspend;
1634 	dev->power.autosuspend_delay = delay;
1635 	update_autosuspend(dev, old_delay, old_use);
1636 	spin_unlock_irq(&dev->power.lock);
1637 }
1638 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1639 
1640 /**
1641  * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1642  * @dev: Device to handle.
1643  * @use: New value for use_autosuspend.
1644  *
1645  * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1646  * suspends as needed.
1647  */
1648 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1649 {
1650 	int old_delay, old_use;
1651 
1652 	spin_lock_irq(&dev->power.lock);
1653 	old_delay = dev->power.autosuspend_delay;
1654 	old_use = dev->power.use_autosuspend;
1655 	dev->power.use_autosuspend = use;
1656 	update_autosuspend(dev, old_delay, old_use);
1657 	spin_unlock_irq(&dev->power.lock);
1658 }
1659 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1660 
1661 /**
1662  * pm_runtime_init - Initialize runtime PM fields in given device object.
1663  * @dev: Device object to initialize.
1664  */
1665 void pm_runtime_init(struct device *dev)
1666 {
1667 	dev->power.runtime_status = RPM_SUSPENDED;
1668 	dev->power.last_status = RPM_INVALID;
1669 	dev->power.idle_notification = false;
1670 
1671 	dev->power.disable_depth = 1;
1672 	atomic_set(&dev->power.usage_count, 0);
1673 
1674 	dev->power.runtime_error = 0;
1675 
1676 	atomic_set(&dev->power.child_count, 0);
1677 	pm_suspend_ignore_children(dev, false);
1678 	dev->power.runtime_auto = true;
1679 
1680 	dev->power.request_pending = false;
1681 	dev->power.request = RPM_REQ_NONE;
1682 	dev->power.deferred_resume = false;
1683 	dev->power.needs_force_resume = 0;
1684 	INIT_WORK(&dev->power.work, pm_runtime_work);
1685 
1686 	dev->power.timer_expires = 0;
1687 	hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1688 	dev->power.suspend_timer.function = pm_suspend_timer_fn;
1689 
1690 	init_waitqueue_head(&dev->power.wait_queue);
1691 }
1692 
1693 /**
1694  * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1695  * @dev: Device object to re-initialize.
1696  */
1697 void pm_runtime_reinit(struct device *dev)
1698 {
1699 	if (!pm_runtime_enabled(dev)) {
1700 		if (dev->power.runtime_status == RPM_ACTIVE)
1701 			pm_runtime_set_suspended(dev);
1702 		if (dev->power.irq_safe) {
1703 			spin_lock_irq(&dev->power.lock);
1704 			dev->power.irq_safe = 0;
1705 			spin_unlock_irq(&dev->power.lock);
1706 			if (dev->parent)
1707 				pm_runtime_put(dev->parent);
1708 		}
1709 	}
1710 }
1711 
1712 /**
1713  * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1714  * @dev: Device object being removed from device hierarchy.
1715  */
1716 void pm_runtime_remove(struct device *dev)
1717 {
1718 	__pm_runtime_disable(dev, false);
1719 	pm_runtime_reinit(dev);
1720 }
1721 
1722 /**
1723  * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1724  * @dev: Consumer device.
1725  */
1726 void pm_runtime_get_suppliers(struct device *dev)
1727 {
1728 	struct device_link *link;
1729 	int idx;
1730 
1731 	idx = device_links_read_lock();
1732 
1733 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1734 				device_links_read_lock_held())
1735 		if (link->flags & DL_FLAG_PM_RUNTIME) {
1736 			link->supplier_preactivated = true;
1737 			pm_runtime_get_sync(link->supplier);
1738 			refcount_inc(&link->rpm_active);
1739 		}
1740 
1741 	device_links_read_unlock(idx);
1742 }
1743 
1744 /**
1745  * pm_runtime_put_suppliers - Drop references to supplier devices.
1746  * @dev: Consumer device.
1747  */
1748 void pm_runtime_put_suppliers(struct device *dev)
1749 {
1750 	struct device_link *link;
1751 	int idx;
1752 
1753 	idx = device_links_read_lock();
1754 
1755 	list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1756 				device_links_read_lock_held())
1757 		if (link->supplier_preactivated) {
1758 			bool put;
1759 
1760 			link->supplier_preactivated = false;
1761 
1762 			spin_lock_irq(&dev->power.lock);
1763 
1764 			put = pm_runtime_status_suspended(dev) &&
1765 			      refcount_dec_not_one(&link->rpm_active);
1766 
1767 			spin_unlock_irq(&dev->power.lock);
1768 
1769 			if (put)
1770 				pm_runtime_put(link->supplier);
1771 		}
1772 
1773 	device_links_read_unlock(idx);
1774 }
1775 
1776 void pm_runtime_new_link(struct device *dev)
1777 {
1778 	spin_lock_irq(&dev->power.lock);
1779 	dev->power.links_count++;
1780 	spin_unlock_irq(&dev->power.lock);
1781 }
1782 
1783 static void pm_runtime_drop_link_count(struct device *dev)
1784 {
1785 	spin_lock_irq(&dev->power.lock);
1786 	WARN_ON(dev->power.links_count == 0);
1787 	dev->power.links_count--;
1788 	spin_unlock_irq(&dev->power.lock);
1789 }
1790 
1791 /**
1792  * pm_runtime_drop_link - Prepare for device link removal.
1793  * @link: Device link going away.
1794  *
1795  * Drop the link count of the consumer end of @link and decrement the supplier
1796  * device's runtime PM usage counter as many times as needed to drop all of the
1797  * PM runtime reference to it from the consumer.
1798  */
1799 void pm_runtime_drop_link(struct device_link *link)
1800 {
1801 	if (!(link->flags & DL_FLAG_PM_RUNTIME))
1802 		return;
1803 
1804 	pm_runtime_drop_link_count(link->consumer);
1805 	pm_runtime_release_supplier(link, true);
1806 }
1807 
1808 static bool pm_runtime_need_not_resume(struct device *dev)
1809 {
1810 	return atomic_read(&dev->power.usage_count) <= 1 &&
1811 		(atomic_read(&dev->power.child_count) == 0 ||
1812 		 dev->power.ignore_children);
1813 }
1814 
1815 /**
1816  * pm_runtime_force_suspend - Force a device into suspend state if needed.
1817  * @dev: Device to suspend.
1818  *
1819  * Disable runtime PM so we safely can check the device's runtime PM status and
1820  * if it is active, invoke its ->runtime_suspend callback to suspend it and
1821  * change its runtime PM status field to RPM_SUSPENDED.  Also, if the device's
1822  * usage and children counters don't indicate that the device was in use before
1823  * the system-wide transition under way, decrement its parent's children counter
1824  * (if there is a parent).  Keep runtime PM disabled to preserve the state
1825  * unless we encounter errors.
1826  *
1827  * Typically this function may be invoked from a system suspend callback to make
1828  * sure the device is put into low power state and it should only be used during
1829  * system-wide PM transitions to sleep states.  It assumes that the analogous
1830  * pm_runtime_force_resume() will be used to resume the device.
1831  */
1832 int pm_runtime_force_suspend(struct device *dev)
1833 {
1834 	int (*callback)(struct device *);
1835 	int ret;
1836 
1837 	pm_runtime_disable(dev);
1838 	if (pm_runtime_status_suspended(dev))
1839 		return 0;
1840 
1841 	callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1842 
1843 	ret = callback ? callback(dev) : 0;
1844 	if (ret)
1845 		goto err;
1846 
1847 	/*
1848 	 * If the device can stay in suspend after the system-wide transition
1849 	 * to the working state that will follow, drop the children counter of
1850 	 * its parent, but set its status to RPM_SUSPENDED anyway in case this
1851 	 * function will be called again for it in the meantime.
1852 	 */
1853 	if (pm_runtime_need_not_resume(dev)) {
1854 		pm_runtime_set_suspended(dev);
1855 	} else {
1856 		__update_runtime_status(dev, RPM_SUSPENDED);
1857 		dev->power.needs_force_resume = 1;
1858 	}
1859 
1860 	return 0;
1861 
1862 err:
1863 	pm_runtime_enable(dev);
1864 	return ret;
1865 }
1866 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1867 
1868 /**
1869  * pm_runtime_force_resume - Force a device into resume state if needed.
1870  * @dev: Device to resume.
1871  *
1872  * Prior invoking this function we expect the user to have brought the device
1873  * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1874  * those actions and bring the device into full power, if it is expected to be
1875  * used on system resume.  In the other case, we defer the resume to be managed
1876  * via runtime PM.
1877  *
1878  * Typically this function may be invoked from a system resume callback.
1879  */
1880 int pm_runtime_force_resume(struct device *dev)
1881 {
1882 	int (*callback)(struct device *);
1883 	int ret = 0;
1884 
1885 	if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume)
1886 		goto out;
1887 
1888 	/*
1889 	 * The value of the parent's children counter is correct already, so
1890 	 * just update the status of the device.
1891 	 */
1892 	__update_runtime_status(dev, RPM_ACTIVE);
1893 
1894 	callback = RPM_GET_CALLBACK(dev, runtime_resume);
1895 
1896 	ret = callback ? callback(dev) : 0;
1897 	if (ret) {
1898 		pm_runtime_set_suspended(dev);
1899 		goto out;
1900 	}
1901 
1902 	pm_runtime_mark_last_busy(dev);
1903 out:
1904 	dev->power.needs_force_resume = 0;
1905 	pm_runtime_enable(dev);
1906 	return ret;
1907 }
1908 EXPORT_SYMBOL_GPL(pm_runtime_force_resume);
1909