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