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