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