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