blob: 1573319404888ad8b07bffdb4f0620d4dc553a4b [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008#include <linux/sched/mm.h>
David Brazdil0f672f62019-12-10 10:32:29 +00009#include <linux/ktime.h>
10#include <linux/hrtimer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#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
19typedef int (*pm_callback_t)(struct device *);
20
21static 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
51static int rpm_resume(struct device *dev, int rpmflags);
52static 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 */
David Brazdil0f672f62019-12-10 10:32:29 +000065static void update_pm_runtime_accounting(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066{
David Brazdil0f672f62019-12-10 10:32:29 +000067 u64 now, last, delta;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068
69 if (dev->power.disable_depth > 0)
70 return;
71
David Brazdil0f672f62019-12-10 10:32:29 +000072 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 if (dev->power.runtime_status == RPM_SUSPENDED)
David Brazdil0f672f62019-12-10 10:32:29 +000088 dev->power.suspended_time += delta;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089 else
David Brazdil0f672f62019-12-10 10:32:29 +000090 dev->power.active_time += delta;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091}
92
93static 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
David Brazdil0f672f62019-12-10 10:32:29 +000099static 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
114u64 pm_runtime_active_time(struct device *dev)
115{
116 return rpm_get_accounted_time(dev, false);
117}
118
119u64 pm_runtime_suspended_time(struct device *dev)
120{
121 return rpm_get_accounted_time(dev, true);
122}
123EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
124
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125/**
126 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
127 * @dev: Device to handle.
128 */
129static void pm_runtime_deactivate_timer(struct device *dev)
130{
131 if (dev->power.timer_expires > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000132 hrtimer_try_to_cancel(&dev->power.suspend_timer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 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 */
141static 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.
David Brazdil0f672f62019-12-10 10:32:29 +0000158 * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159 *
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 */
David Brazdil0f672f62019-12-10 10:32:29 +0000163u64 pm_runtime_autosuspend_expiration(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164{
165 int autosuspend_delay;
David Brazdil0f672f62019-12-10 10:32:29 +0000166 u64 expires;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000167
168 if (!dev->power.use_autosuspend)
David Brazdil0f672f62019-12-10 10:32:29 +0000169 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000170
171 autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
172 if (autosuspend_delay < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000173 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174
David Brazdil0f672f62019-12-10 10:32:29 +0000175 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 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179
David Brazdil0f672f62019-12-10 10:32:29 +0000180 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181}
182EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
183
184static 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 */
217void 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}
252EXPORT_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 */
258static 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;
David Brazdil0f672f62019-12-10 10:32:29 +0000278 else if (__dev_pm_qos_resume_latency(dev) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 retval = -EPERM;
280 else if (dev->power.runtime_status == RPM_SUSPENDED)
281 retval = 1;
282
283 return retval;
284}
285
286static int rpm_get_suppliers(struct device *dev)
287{
288 struct device_link *link;
289
David Brazdil0f672f62019-12-10 10:32:29 +0000290 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
291 device_links_read_lock_held()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 int retval;
293
Olivier Deprez0e641232021-09-23 10:07:05 +0200294 if (!(link->flags & DL_FLAG_PM_RUNTIME))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295 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 }
David Brazdil0f672f62019-12-10 10:32:29 +0000303 refcount_inc(&link->rpm_active);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304 }
305 return 0;
306}
307
Olivier Deprez157378f2022-04-04 15:47:50 +0200308/**
309 * pm_runtime_release_supplier - Drop references to device link's supplier.
310 * @link: Target device link.
311 * @check_idle: Whether or not to check if the supplier device is idle.
312 *
313 * Drop all runtime PM references associated with @link to its supplier device
314 * and if @check_idle is set, check if that device is idle (and so it can be
315 * suspended).
316 */
317void pm_runtime_release_supplier(struct device_link *link, bool check_idle)
318{
319 struct device *supplier = link->supplier;
320
321 /*
322 * The additional power.usage_count check is a safety net in case
323 * the rpm_active refcount becomes saturated, in which case
324 * refcount_dec_not_one() would return true forever, but it is not
325 * strictly necessary.
326 */
327 while (refcount_dec_not_one(&link->rpm_active) &&
328 atomic_read(&supplier->power.usage_count) > 0)
329 pm_runtime_put_noidle(supplier);
330
331 if (check_idle)
332 pm_request_idle(supplier);
333}
334
Olivier Deprez0e641232021-09-23 10:07:05 +0200335static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336{
337 struct device_link *link;
338
David Brazdil0f672f62019-12-10 10:32:29 +0000339 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
Olivier Deprez157378f2022-04-04 15:47:50 +0200340 device_links_read_lock_held())
341 pm_runtime_release_supplier(link, try_to_suspend);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342}
343
Olivier Deprez0e641232021-09-23 10:07:05 +0200344static void rpm_put_suppliers(struct device *dev)
345{
346 __rpm_put_suppliers(dev, true);
347}
348
349static void rpm_suspend_suppliers(struct device *dev)
350{
351 struct device_link *link;
352 int idx = device_links_read_lock();
353
354 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
355 device_links_read_lock_held())
356 pm_request_idle(link->supplier);
357
358 device_links_read_unlock(idx);
359}
360
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361/**
362 * __rpm_callback - Run a given runtime PM callback for a given device.
363 * @cb: Runtime PM callback to run.
364 * @dev: Device to run the callback for.
365 */
366static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
367 __releases(&dev->power.lock) __acquires(&dev->power.lock)
368{
369 int retval, idx;
370 bool use_links = dev->power.links_count > 0;
371
372 if (dev->power.irq_safe) {
373 spin_unlock(&dev->power.lock);
374 } else {
375 spin_unlock_irq(&dev->power.lock);
376
377 /*
378 * Resume suppliers if necessary.
379 *
380 * The device's runtime PM status cannot change until this
381 * routine returns, so it is safe to read the status outside of
382 * the lock.
383 */
384 if (use_links && dev->power.runtime_status == RPM_RESUMING) {
385 idx = device_links_read_lock();
386
387 retval = rpm_get_suppliers(dev);
Olivier Deprez0e641232021-09-23 10:07:05 +0200388 if (retval) {
389 rpm_put_suppliers(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390 goto fail;
Olivier Deprez0e641232021-09-23 10:07:05 +0200391 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392
393 device_links_read_unlock(idx);
394 }
395 }
396
397 retval = cb(dev);
398
399 if (dev->power.irq_safe) {
400 spin_lock(&dev->power.lock);
401 } else {
402 /*
403 * If the device is suspending and the callback has returned
404 * success, drop the usage counters of the suppliers that have
405 * been reference counted on its resume.
406 *
407 * Do that if resume fails too.
408 */
409 if (use_links
410 && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
411 || (dev->power.runtime_status == RPM_RESUMING && retval))) {
412 idx = device_links_read_lock();
413
Olivier Deprez0e641232021-09-23 10:07:05 +0200414 __rpm_put_suppliers(dev, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415
Olivier Deprez0e641232021-09-23 10:07:05 +0200416fail:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 device_links_read_unlock(idx);
418 }
419
420 spin_lock_irq(&dev->power.lock);
421 }
422
423 return retval;
424}
425
426/**
427 * rpm_idle - Notify device bus type if the device can be suspended.
428 * @dev: Device to notify the bus type about.
429 * @rpmflags: Flag bits.
430 *
431 * Check if the device's runtime PM status allows it to be suspended. If
432 * another idle notification has been started earlier, return immediately. If
433 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
434 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
435 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
436 *
437 * This function must be called under dev->power.lock with interrupts disabled.
438 */
439static int rpm_idle(struct device *dev, int rpmflags)
440{
441 int (*callback)(struct device *);
442 int retval;
443
444 trace_rpm_idle_rcuidle(dev, rpmflags);
445 retval = rpm_check_suspend_allowed(dev);
446 if (retval < 0)
447 ; /* Conditions are wrong. */
448
449 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
450 else if (dev->power.runtime_status != RPM_ACTIVE)
451 retval = -EAGAIN;
452
453 /*
454 * Any pending request other than an idle notification takes
455 * precedence over us, except that the timer may be running.
456 */
457 else if (dev->power.request_pending &&
458 dev->power.request > RPM_REQ_IDLE)
459 retval = -EAGAIN;
460
461 /* Act as though RPM_NOWAIT is always set. */
462 else if (dev->power.idle_notification)
463 retval = -EINPROGRESS;
464 if (retval)
465 goto out;
466
467 /* Pending requests need to be canceled. */
468 dev->power.request = RPM_REQ_NONE;
469
470 if (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 callback = RPM_GET_CALLBACK(dev, runtime_idle);
487
488 if (callback)
489 retval = __rpm_callback(callback, dev);
490
491 dev->power.idle_notification = false;
492 wake_up_all(&dev->power.wait_queue);
493
494 out:
495 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
496 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
497}
498
499/**
500 * rpm_callback - Run a given runtime PM callback for a given device.
501 * @cb: Runtime PM callback to run.
502 * @dev: Device to run the callback for.
503 */
504static int rpm_callback(int (*cb)(struct device *), struct device *dev)
505{
506 int retval;
507
508 if (!cb)
509 return -ENOSYS;
510
511 if (dev->power.memalloc_noio) {
512 unsigned int noio_flag;
513
514 /*
515 * Deadlock might be caused if memory allocation with
516 * GFP_KERNEL happens inside runtime_suspend and
517 * runtime_resume callbacks of one block device's
518 * ancestor or the block device itself. Network
519 * device might be thought as part of iSCSI block
520 * device, so network device and its ancestor should
521 * be marked as memalloc_noio too.
522 */
523 noio_flag = memalloc_noio_save();
524 retval = __rpm_callback(cb, dev);
525 memalloc_noio_restore(noio_flag);
526 } else {
527 retval = __rpm_callback(cb, dev);
528 }
529
530 dev->power.runtime_error = retval;
531 return retval != -EACCES ? retval : -EIO;
532}
533
534/**
535 * rpm_suspend - Carry out runtime suspend of given device.
536 * @dev: Device to suspend.
537 * @rpmflags: Flag bits.
538 *
539 * Check if the device's runtime PM status allows it to be suspended.
540 * Cancel a pending idle notification, autosuspend or suspend. If
541 * another suspend has been started earlier, either return immediately
542 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
543 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
544 * otherwise run the ->runtime_suspend() callback directly. When
545 * ->runtime_suspend succeeded, if a deferred resume was requested while
546 * the callback was running then carry it out, otherwise send an idle
547 * notification for its parent (if the suspend succeeded and both
548 * ignore_children of parent->power and irq_safe of dev->power are not set).
549 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
550 * flag is set and the next autosuspend-delay expiration time is in the
551 * future, schedule another autosuspend attempt.
552 *
553 * This function must be called under dev->power.lock with interrupts disabled.
554 */
555static int rpm_suspend(struct device *dev, int rpmflags)
556 __releases(&dev->power.lock) __acquires(&dev->power.lock)
557{
558 int (*callback)(struct device *);
559 struct device *parent = NULL;
560 int retval;
561
562 trace_rpm_suspend_rcuidle(dev, rpmflags);
563
564 repeat:
565 retval = rpm_check_suspend_allowed(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 if (retval < 0)
Olivier Deprez157378f2022-04-04 15:47:50 +0200567 goto out; /* Conditions are wrong. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568
569 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000571 retval = -EAGAIN;
572 if (retval)
573 goto out;
574
575 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
576 if ((rpmflags & RPM_AUTO)
577 && dev->power.runtime_status != RPM_SUSPENDING) {
David Brazdil0f672f62019-12-10 10:32:29 +0000578 u64 expires = pm_runtime_autosuspend_expiration(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579
580 if (expires != 0) {
581 /* Pending requests need to be canceled. */
582 dev->power.request = RPM_REQ_NONE;
583
584 /*
585 * Optimization: If the timer is already running and is
586 * set to expire at or before the autosuspend delay,
587 * avoid the overhead of resetting it. Just let it
588 * expire; pm_suspend_timer_fn() will take care of the
589 * rest.
590 */
David Brazdil0f672f62019-12-10 10:32:29 +0000591 if (!(dev->power.timer_expires &&
592 dev->power.timer_expires <= expires)) {
593 /*
594 * We add a slack of 25% to gather wakeups
595 * without sacrificing the granularity.
596 */
597 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
598 (NSEC_PER_MSEC >> 2);
599
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600 dev->power.timer_expires = expires;
David Brazdil0f672f62019-12-10 10:32:29 +0000601 hrtimer_start_range_ns(&dev->power.suspend_timer,
602 ns_to_ktime(expires),
603 slack,
604 HRTIMER_MODE_ABS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605 }
606 dev->power.timer_autosuspends = 1;
607 goto out;
608 }
609 }
610
611 /* Other scheduled or pending requests need to be canceled. */
612 pm_runtime_cancel_pending(dev);
613
614 if (dev->power.runtime_status == RPM_SUSPENDING) {
615 DEFINE_WAIT(wait);
616
617 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
618 retval = -EINPROGRESS;
619 goto out;
620 }
621
622 if (dev->power.irq_safe) {
623 spin_unlock(&dev->power.lock);
624
625 cpu_relax();
626
627 spin_lock(&dev->power.lock);
628 goto repeat;
629 }
630
631 /* Wait for the other suspend running in parallel with us. */
632 for (;;) {
633 prepare_to_wait(&dev->power.wait_queue, &wait,
634 TASK_UNINTERRUPTIBLE);
635 if (dev->power.runtime_status != RPM_SUSPENDING)
636 break;
637
638 spin_unlock_irq(&dev->power.lock);
639
640 schedule();
641
642 spin_lock_irq(&dev->power.lock);
643 }
644 finish_wait(&dev->power.wait_queue, &wait);
645 goto repeat;
646 }
647
648 if (dev->power.no_callbacks)
649 goto no_callback; /* Assume success. */
650
651 /* Carry out an asynchronous or a synchronous suspend. */
652 if (rpmflags & RPM_ASYNC) {
653 dev->power.request = (rpmflags & RPM_AUTO) ?
654 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
655 if (!dev->power.request_pending) {
656 dev->power.request_pending = true;
657 queue_work(pm_wq, &dev->power.work);
658 }
659 goto out;
660 }
661
662 __update_runtime_status(dev, RPM_SUSPENDING);
663
664 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
665
666 dev_pm_enable_wake_irq_check(dev, true);
667 retval = rpm_callback(callback, dev);
668 if (retval)
669 goto fail;
670
671 no_callback:
672 __update_runtime_status(dev, RPM_SUSPENDED);
673 pm_runtime_deactivate_timer(dev);
674
675 if (dev->parent) {
676 parent = dev->parent;
677 atomic_add_unless(&parent->power.child_count, -1, 0);
678 }
679 wake_up_all(&dev->power.wait_queue);
680
681 if (dev->power.deferred_resume) {
682 dev->power.deferred_resume = false;
683 rpm_resume(dev, 0);
684 retval = -EAGAIN;
685 goto out;
686 }
687
Olivier Deprez0e641232021-09-23 10:07:05 +0200688 if (dev->power.irq_safe)
689 goto out;
690
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691 /* Maybe the parent is now able to suspend. */
Olivier Deprez0e641232021-09-23 10:07:05 +0200692 if (parent && !parent->power.ignore_children) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693 spin_unlock(&dev->power.lock);
694
695 spin_lock(&parent->power.lock);
696 rpm_idle(parent, RPM_ASYNC);
697 spin_unlock(&parent->power.lock);
698
699 spin_lock(&dev->power.lock);
700 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200701 /* Maybe the suppliers are now able to suspend. */
702 if (dev->power.links_count > 0) {
703 spin_unlock_irq(&dev->power.lock);
704
705 rpm_suspend_suppliers(dev);
706
707 spin_lock_irq(&dev->power.lock);
708 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709
710 out:
711 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
712
713 return retval;
714
715 fail:
716 dev_pm_disable_wake_irq_check(dev);
717 __update_runtime_status(dev, RPM_ACTIVE);
718 dev->power.deferred_resume = false;
719 wake_up_all(&dev->power.wait_queue);
720
721 if (retval == -EAGAIN || retval == -EBUSY) {
722 dev->power.runtime_error = 0;
723
724 /*
725 * If the callback routine failed an autosuspend, and
726 * if the last_busy time has been updated so that there
727 * is a new autosuspend expiration time, automatically
728 * reschedule another autosuspend.
729 */
730 if ((rpmflags & RPM_AUTO) &&
731 pm_runtime_autosuspend_expiration(dev) != 0)
732 goto repeat;
733 } else {
734 pm_runtime_cancel_pending(dev);
735 }
736 goto out;
737}
738
739/**
740 * rpm_resume - Carry out runtime resume of given device.
741 * @dev: Device to resume.
742 * @rpmflags: Flag bits.
743 *
744 * Check if the device's runtime PM status allows it to be resumed. Cancel
745 * any scheduled or pending requests. If another resume has been started
746 * earlier, either return immediately or wait for it to finish, depending on the
747 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
748 * parallel with this function, either tell the other process to resume after
749 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
750 * flag is set then queue a resume request; otherwise run the
751 * ->runtime_resume() callback directly. Queue an idle notification for the
752 * device if the resume succeeded.
753 *
754 * This function must be called under dev->power.lock with interrupts disabled.
755 */
756static int rpm_resume(struct device *dev, int rpmflags)
757 __releases(&dev->power.lock) __acquires(&dev->power.lock)
758{
759 int (*callback)(struct device *);
760 struct device *parent = NULL;
761 int retval = 0;
762
763 trace_rpm_resume_rcuidle(dev, rpmflags);
764
765 repeat:
766 if (dev->power.runtime_error)
767 retval = -EINVAL;
768 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
769 && dev->power.runtime_status == RPM_ACTIVE)
770 retval = 1;
771 else if (dev->power.disable_depth > 0)
772 retval = -EACCES;
773 if (retval)
774 goto out;
775
776 /*
777 * Other scheduled or pending requests need to be canceled. Small
778 * optimization: If an autosuspend timer is running, leave it running
779 * rather than cancelling it now only to restart it again in the near
780 * future.
781 */
782 dev->power.request = RPM_REQ_NONE;
783 if (!dev->power.timer_autosuspends)
784 pm_runtime_deactivate_timer(dev);
785
786 if (dev->power.runtime_status == RPM_ACTIVE) {
787 retval = 1;
788 goto out;
789 }
790
791 if (dev->power.runtime_status == RPM_RESUMING
792 || dev->power.runtime_status == RPM_SUSPENDING) {
793 DEFINE_WAIT(wait);
794
795 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
796 if (dev->power.runtime_status == RPM_SUSPENDING)
797 dev->power.deferred_resume = true;
798 else
799 retval = -EINPROGRESS;
800 goto out;
801 }
802
803 if (dev->power.irq_safe) {
804 spin_unlock(&dev->power.lock);
805
806 cpu_relax();
807
808 spin_lock(&dev->power.lock);
809 goto repeat;
810 }
811
812 /* Wait for the operation carried out in parallel with us. */
813 for (;;) {
814 prepare_to_wait(&dev->power.wait_queue, &wait,
815 TASK_UNINTERRUPTIBLE);
816 if (dev->power.runtime_status != RPM_RESUMING
817 && dev->power.runtime_status != RPM_SUSPENDING)
818 break;
819
820 spin_unlock_irq(&dev->power.lock);
821
822 schedule();
823
824 spin_lock_irq(&dev->power.lock);
825 }
826 finish_wait(&dev->power.wait_queue, &wait);
827 goto repeat;
828 }
829
830 /*
831 * See if we can skip waking up the parent. This is safe only if
832 * power.no_callbacks is set, because otherwise we don't know whether
833 * the resume will actually succeed.
834 */
835 if (dev->power.no_callbacks && !parent && dev->parent) {
836 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
837 if (dev->parent->power.disable_depth > 0
838 || dev->parent->power.ignore_children
839 || dev->parent->power.runtime_status == RPM_ACTIVE) {
840 atomic_inc(&dev->parent->power.child_count);
841 spin_unlock(&dev->parent->power.lock);
842 retval = 1;
843 goto no_callback; /* Assume success. */
844 }
845 spin_unlock(&dev->parent->power.lock);
846 }
847
848 /* Carry out an asynchronous or a synchronous resume. */
849 if (rpmflags & RPM_ASYNC) {
850 dev->power.request = RPM_REQ_RESUME;
851 if (!dev->power.request_pending) {
852 dev->power.request_pending = true;
853 queue_work(pm_wq, &dev->power.work);
854 }
855 retval = 0;
856 goto out;
857 }
858
859 if (!parent && dev->parent) {
860 /*
861 * Increment the parent's usage counter and resume it if
862 * necessary. Not needed if dev is irq-safe; then the
863 * parent is permanently resumed.
864 */
865 parent = dev->parent;
866 if (dev->power.irq_safe)
867 goto skip_parent;
868 spin_unlock(&dev->power.lock);
869
870 pm_runtime_get_noresume(parent);
871
872 spin_lock(&parent->power.lock);
873 /*
874 * Resume the parent if it has runtime PM enabled and not been
875 * set to ignore its children.
876 */
877 if (!parent->power.disable_depth
878 && !parent->power.ignore_children) {
879 rpm_resume(parent, 0);
880 if (parent->power.runtime_status != RPM_ACTIVE)
881 retval = -EBUSY;
882 }
883 spin_unlock(&parent->power.lock);
884
885 spin_lock(&dev->power.lock);
886 if (retval)
887 goto out;
888 goto repeat;
889 }
890 skip_parent:
891
892 if (dev->power.no_callbacks)
893 goto no_callback; /* Assume success. */
894
895 __update_runtime_status(dev, RPM_RESUMING);
896
897 callback = RPM_GET_CALLBACK(dev, runtime_resume);
898
899 dev_pm_disable_wake_irq_check(dev);
900 retval = rpm_callback(callback, dev);
901 if (retval) {
902 __update_runtime_status(dev, RPM_SUSPENDED);
903 pm_runtime_cancel_pending(dev);
904 dev_pm_enable_wake_irq_check(dev, false);
905 } else {
906 no_callback:
907 __update_runtime_status(dev, RPM_ACTIVE);
908 pm_runtime_mark_last_busy(dev);
909 if (parent)
910 atomic_inc(&parent->power.child_count);
911 }
912 wake_up_all(&dev->power.wait_queue);
913
914 if (retval >= 0)
915 rpm_idle(dev, RPM_ASYNC);
916
917 out:
918 if (parent && !dev->power.irq_safe) {
919 spin_unlock_irq(&dev->power.lock);
920
921 pm_runtime_put(parent);
922
923 spin_lock_irq(&dev->power.lock);
924 }
925
926 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
927
928 return retval;
929}
930
931/**
932 * pm_runtime_work - Universal runtime PM work function.
933 * @work: Work structure used for scheduling the execution of this function.
934 *
935 * Use @work to get the device object the work is to be done for, determine what
936 * is to be done and execute the appropriate runtime PM function.
937 */
938static void pm_runtime_work(struct work_struct *work)
939{
940 struct device *dev = container_of(work, struct device, power.work);
941 enum rpm_request req;
942
943 spin_lock_irq(&dev->power.lock);
944
945 if (!dev->power.request_pending)
946 goto out;
947
948 req = dev->power.request;
949 dev->power.request = RPM_REQ_NONE;
950 dev->power.request_pending = false;
951
952 switch (req) {
953 case RPM_REQ_NONE:
954 break;
955 case RPM_REQ_IDLE:
956 rpm_idle(dev, RPM_NOWAIT);
957 break;
958 case RPM_REQ_SUSPEND:
959 rpm_suspend(dev, RPM_NOWAIT);
960 break;
961 case RPM_REQ_AUTOSUSPEND:
962 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
963 break;
964 case RPM_REQ_RESUME:
965 rpm_resume(dev, RPM_NOWAIT);
966 break;
967 }
968
969 out:
970 spin_unlock_irq(&dev->power.lock);
971}
972
973/**
974 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
975 * @data: Device pointer passed by pm_schedule_suspend().
976 *
977 * Check if the time is right and queue a suspend request.
978 */
David Brazdil0f672f62019-12-10 10:32:29 +0000979static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980{
David Brazdil0f672f62019-12-10 10:32:29 +0000981 struct device *dev = container_of(timer, struct device, power.suspend_timer);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982 unsigned long flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000983 u64 expires;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984
985 spin_lock_irqsave(&dev->power.lock, flags);
986
987 expires = dev->power.timer_expires;
David Brazdil0f672f62019-12-10 10:32:29 +0000988 /*
989 * If 'expires' is after the current time, we've been called
990 * too early.
991 */
992 if (expires > 0 && expires < ktime_get_mono_fast_ns()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000993 dev->power.timer_expires = 0;
994 rpm_suspend(dev, dev->power.timer_autosuspends ?
995 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
996 }
997
998 spin_unlock_irqrestore(&dev->power.lock, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000999
1000 return HRTIMER_NORESTART;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001001}
1002
1003/**
1004 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1005 * @dev: Device to suspend.
1006 * @delay: Time to wait before submitting a suspend request, in milliseconds.
1007 */
1008int pm_schedule_suspend(struct device *dev, unsigned int delay)
1009{
1010 unsigned long flags;
David Brazdil0f672f62019-12-10 10:32:29 +00001011 u64 expires;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001012 int retval;
1013
1014 spin_lock_irqsave(&dev->power.lock, flags);
1015
1016 if (!delay) {
1017 retval = rpm_suspend(dev, RPM_ASYNC);
1018 goto out;
1019 }
1020
1021 retval = rpm_check_suspend_allowed(dev);
1022 if (retval)
1023 goto out;
1024
1025 /* Other scheduled or pending requests need to be canceled. */
1026 pm_runtime_cancel_pending(dev);
1027
David Brazdil0f672f62019-12-10 10:32:29 +00001028 expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
1029 dev->power.timer_expires = expires;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 dev->power.timer_autosuspends = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001031 hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032
1033 out:
1034 spin_unlock_irqrestore(&dev->power.lock, flags);
1035
1036 return retval;
1037}
1038EXPORT_SYMBOL_GPL(pm_schedule_suspend);
1039
1040/**
1041 * __pm_runtime_idle - Entry point for runtime idle operations.
1042 * @dev: Device to send idle notification for.
1043 * @rpmflags: Flag bits.
1044 *
1045 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1046 * return immediately if it is larger than zero. Then carry out an idle
1047 * notification, either synchronous or asynchronous.
1048 *
1049 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1050 * or if pm_runtime_irq_safe() has been called.
1051 */
1052int __pm_runtime_idle(struct device *dev, int rpmflags)
1053{
1054 unsigned long flags;
1055 int retval;
1056
1057 if (rpmflags & RPM_GET_PUT) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001058 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1059 trace_rpm_usage_rcuidle(dev, rpmflags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001060 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001061 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001062 }
1063
1064 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1065
1066 spin_lock_irqsave(&dev->power.lock, flags);
1067 retval = rpm_idle(dev, rpmflags);
1068 spin_unlock_irqrestore(&dev->power.lock, flags);
1069
1070 return retval;
1071}
1072EXPORT_SYMBOL_GPL(__pm_runtime_idle);
1073
1074/**
1075 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1076 * @dev: Device to suspend.
1077 * @rpmflags: Flag bits.
1078 *
1079 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1080 * return immediately if it is larger than zero. Then carry out a suspend,
1081 * either synchronous or asynchronous.
1082 *
1083 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1084 * or if pm_runtime_irq_safe() has been called.
1085 */
1086int __pm_runtime_suspend(struct device *dev, int rpmflags)
1087{
1088 unsigned long flags;
1089 int retval;
1090
1091 if (rpmflags & RPM_GET_PUT) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001092 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1093 trace_rpm_usage_rcuidle(dev, rpmflags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001094 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001095 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001096 }
1097
1098 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1099
1100 spin_lock_irqsave(&dev->power.lock, flags);
1101 retval = rpm_suspend(dev, rpmflags);
1102 spin_unlock_irqrestore(&dev->power.lock, flags);
1103
1104 return retval;
1105}
1106EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
1107
1108/**
1109 * __pm_runtime_resume - Entry point for runtime resume operations.
1110 * @dev: Device to resume.
1111 * @rpmflags: Flag bits.
1112 *
1113 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
1114 * carry out a resume, either synchronous or asynchronous.
1115 *
1116 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1117 * or if pm_runtime_irq_safe() has been called.
1118 */
1119int __pm_runtime_resume(struct device *dev, int rpmflags)
1120{
1121 unsigned long flags;
1122 int retval;
1123
1124 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1125 dev->power.runtime_status != RPM_ACTIVE);
1126
1127 if (rpmflags & RPM_GET_PUT)
1128 atomic_inc(&dev->power.usage_count);
1129
1130 spin_lock_irqsave(&dev->power.lock, flags);
1131 retval = rpm_resume(dev, rpmflags);
1132 spin_unlock_irqrestore(&dev->power.lock, flags);
1133
1134 return retval;
1135}
1136EXPORT_SYMBOL_GPL(__pm_runtime_resume);
1137
1138/**
Olivier Deprez157378f2022-04-04 15:47:50 +02001139 * pm_runtime_get_if_active - Conditionally bump up device usage counter.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001140 * @dev: Device to handle.
Olivier Deprez157378f2022-04-04 15:47:50 +02001141 * @ign_usage_count: Whether or not to look at the current usage counter value.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 *
Olivier Deprez157378f2022-04-04 15:47:50 +02001143 * Return -EINVAL if runtime PM is disabled for @dev.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001144 *
Olivier Deprez157378f2022-04-04 15:47:50 +02001145 * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either
1146 * @ign_usage_count is %true or the runtime PM usage counter of @dev is not
1147 * zero, increment the usage counter of @dev and return 1. Otherwise, return 0
1148 * without changing the usage counter.
1149 *
1150 * If @ign_usage_count is %true, this function can be used to prevent suspending
1151 * the device when its runtime PM status is %RPM_ACTIVE.
1152 *
1153 * If @ign_usage_count is %false, this function can be used to prevent
1154 * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
1155 * runtime PM usage counter is not zero.
1156 *
1157 * The caller is resposible for decrementing the runtime PM usage counter of
1158 * @dev after this function has returned a positive value for it.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001159 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001160int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001161{
1162 unsigned long flags;
1163 int retval;
1164
1165 spin_lock_irqsave(&dev->power.lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001166 if (dev->power.disable_depth > 0) {
1167 retval = -EINVAL;
1168 } else if (dev->power.runtime_status != RPM_ACTIVE) {
1169 retval = 0;
1170 } else if (ign_usage_count) {
1171 retval = 1;
1172 atomic_inc(&dev->power.usage_count);
1173 } else {
1174 retval = atomic_inc_not_zero(&dev->power.usage_count);
1175 }
1176 trace_rpm_usage_rcuidle(dev, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001177 spin_unlock_irqrestore(&dev->power.lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001178
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 return retval;
1180}
Olivier Deprez157378f2022-04-04 15:47:50 +02001181EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001182
1183/**
1184 * __pm_runtime_set_status - Set runtime PM status of a device.
1185 * @dev: Device to handle.
1186 * @status: New runtime PM status of the device.
1187 *
1188 * If runtime PM of the device is disabled or its power.runtime_error field is
1189 * different from zero, the status may be changed either to RPM_ACTIVE, or to
1190 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1191 * However, if the device has a parent and the parent is not active, and the
1192 * parent's power.ignore_children flag is unset, the device's status cannot be
1193 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1194 *
1195 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1196 * and the device parent's counter of unsuspended children is modified to
1197 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
1198 * notification request for the parent is submitted.
David Brazdil0f672f62019-12-10 10:32:29 +00001199 *
1200 * If @dev has any suppliers (as reflected by device links to them), and @status
1201 * is RPM_ACTIVE, they will be activated upfront and if the activation of one
1202 * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
1203 * of the @status value) and the suppliers will be deacticated on exit. The
1204 * error returned by the failing supplier activation will be returned in that
1205 * case.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001206 */
1207int __pm_runtime_set_status(struct device *dev, unsigned int status)
1208{
1209 struct device *parent = dev->parent;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001210 bool notify_parent = false;
1211 int error = 0;
1212
1213 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1214 return -EINVAL;
1215
David Brazdil0f672f62019-12-10 10:32:29 +00001216 spin_lock_irq(&dev->power.lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001217
David Brazdil0f672f62019-12-10 10:32:29 +00001218 /*
1219 * Prevent PM-runtime from being enabled for the device or return an
1220 * error if it is enabled already and working.
1221 */
1222 if (dev->power.runtime_error || dev->power.disable_depth)
1223 dev->power.disable_depth++;
1224 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225 error = -EAGAIN;
David Brazdil0f672f62019-12-10 10:32:29 +00001226
1227 spin_unlock_irq(&dev->power.lock);
1228
1229 if (error)
1230 return error;
1231
1232 /*
1233 * If the new status is RPM_ACTIVE, the suppliers can be activated
1234 * upfront regardless of the current status, because next time
1235 * rpm_put_suppliers() runs, the rpm_active refcounts of the links
1236 * involved will be dropped down to one anyway.
1237 */
1238 if (status == RPM_ACTIVE) {
1239 int idx = device_links_read_lock();
1240
1241 error = rpm_get_suppliers(dev);
1242 if (error)
1243 status = RPM_SUSPENDED;
1244
1245 device_links_read_unlock(idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001246 }
1247
David Brazdil0f672f62019-12-10 10:32:29 +00001248 spin_lock_irq(&dev->power.lock);
1249
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001250 if (dev->power.runtime_status == status || !parent)
1251 goto out_set;
1252
1253 if (status == RPM_SUSPENDED) {
1254 atomic_add_unless(&parent->power.child_count, -1, 0);
1255 notify_parent = !parent->power.ignore_children;
1256 } else {
1257 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1258
1259 /*
1260 * It is invalid to put an active child under a parent that is
1261 * not active, has runtime PM enabled and the
1262 * 'power.ignore_children' flag unset.
1263 */
1264 if (!parent->power.disable_depth
1265 && !parent->power.ignore_children
1266 && parent->power.runtime_status != RPM_ACTIVE) {
1267 dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1268 dev_name(dev),
1269 dev_name(parent));
1270 error = -EBUSY;
1271 } else if (dev->power.runtime_status == RPM_SUSPENDED) {
1272 atomic_inc(&parent->power.child_count);
1273 }
1274
1275 spin_unlock(&parent->power.lock);
1276
David Brazdil0f672f62019-12-10 10:32:29 +00001277 if (error) {
1278 status = RPM_SUSPENDED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001279 goto out;
David Brazdil0f672f62019-12-10 10:32:29 +00001280 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001281 }
1282
1283 out_set:
1284 __update_runtime_status(dev, status);
David Brazdil0f672f62019-12-10 10:32:29 +00001285 if (!error)
1286 dev->power.runtime_error = 0;
1287
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001288 out:
David Brazdil0f672f62019-12-10 10:32:29 +00001289 spin_unlock_irq(&dev->power.lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001290
1291 if (notify_parent)
1292 pm_request_idle(parent);
1293
David Brazdil0f672f62019-12-10 10:32:29 +00001294 if (status == RPM_SUSPENDED) {
1295 int idx = device_links_read_lock();
1296
1297 rpm_put_suppliers(dev);
1298
1299 device_links_read_unlock(idx);
1300 }
1301
1302 pm_runtime_enable(dev);
1303
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001304 return error;
1305}
1306EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1307
1308/**
1309 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1310 * @dev: Device to handle.
1311 *
1312 * Flush all pending requests for the device from pm_wq and wait for all
1313 * runtime PM operations involving the device in progress to complete.
1314 *
1315 * Should be called under dev->power.lock with interrupts disabled.
1316 */
1317static void __pm_runtime_barrier(struct device *dev)
1318{
1319 pm_runtime_deactivate_timer(dev);
1320
1321 if (dev->power.request_pending) {
1322 dev->power.request = RPM_REQ_NONE;
1323 spin_unlock_irq(&dev->power.lock);
1324
1325 cancel_work_sync(&dev->power.work);
1326
1327 spin_lock_irq(&dev->power.lock);
1328 dev->power.request_pending = false;
1329 }
1330
1331 if (dev->power.runtime_status == RPM_SUSPENDING
1332 || dev->power.runtime_status == RPM_RESUMING
1333 || dev->power.idle_notification) {
1334 DEFINE_WAIT(wait);
1335
1336 /* Suspend, wake-up or idle notification in progress. */
1337 for (;;) {
1338 prepare_to_wait(&dev->power.wait_queue, &wait,
1339 TASK_UNINTERRUPTIBLE);
1340 if (dev->power.runtime_status != RPM_SUSPENDING
1341 && dev->power.runtime_status != RPM_RESUMING
1342 && !dev->power.idle_notification)
1343 break;
1344 spin_unlock_irq(&dev->power.lock);
1345
1346 schedule();
1347
1348 spin_lock_irq(&dev->power.lock);
1349 }
1350 finish_wait(&dev->power.wait_queue, &wait);
1351 }
1352}
1353
1354/**
1355 * pm_runtime_barrier - Flush pending requests and wait for completions.
1356 * @dev: Device to handle.
1357 *
1358 * Prevent the device from being suspended by incrementing its usage counter and
1359 * if there's a pending resume request for the device, wake the device up.
1360 * Next, make sure that all pending requests for the device have been flushed
1361 * from pm_wq and wait for all runtime PM operations involving the device in
1362 * progress to complete.
1363 *
1364 * Return value:
1365 * 1, if there was a resume request pending and the device had to be woken up,
1366 * 0, otherwise
1367 */
1368int pm_runtime_barrier(struct device *dev)
1369{
1370 int retval = 0;
1371
1372 pm_runtime_get_noresume(dev);
1373 spin_lock_irq(&dev->power.lock);
1374
1375 if (dev->power.request_pending
1376 && dev->power.request == RPM_REQ_RESUME) {
1377 rpm_resume(dev, 0);
1378 retval = 1;
1379 }
1380
1381 __pm_runtime_barrier(dev);
1382
1383 spin_unlock_irq(&dev->power.lock);
1384 pm_runtime_put_noidle(dev);
1385
1386 return retval;
1387}
1388EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1389
1390/**
1391 * __pm_runtime_disable - Disable runtime PM of a device.
1392 * @dev: Device to handle.
1393 * @check_resume: If set, check if there's a resume request for the device.
1394 *
1395 * Increment power.disable_depth for the device and if it was zero previously,
1396 * cancel all pending runtime PM requests for the device and wait for all
1397 * operations in progress to complete. The device can be either active or
1398 * suspended after its runtime PM has been disabled.
1399 *
1400 * If @check_resume is set and there's a resume request pending when
1401 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1402 * function will wake up the device before disabling its runtime PM.
1403 */
1404void __pm_runtime_disable(struct device *dev, bool check_resume)
1405{
1406 spin_lock_irq(&dev->power.lock);
1407
1408 if (dev->power.disable_depth > 0) {
1409 dev->power.disable_depth++;
1410 goto out;
1411 }
1412
1413 /*
1414 * Wake up the device if there's a resume request pending, because that
1415 * means there probably is some I/O to process and disabling runtime PM
1416 * shouldn't prevent the device from processing the I/O.
1417 */
1418 if (check_resume && dev->power.request_pending
1419 && dev->power.request == RPM_REQ_RESUME) {
1420 /*
1421 * Prevent suspends and idle notifications from being carried
1422 * out after we have woken up the device.
1423 */
1424 pm_runtime_get_noresume(dev);
1425
1426 rpm_resume(dev, 0);
1427
1428 pm_runtime_put_noidle(dev);
1429 }
1430
David Brazdil0f672f62019-12-10 10:32:29 +00001431 /* Update time accounting before disabling PM-runtime. */
1432 update_pm_runtime_accounting(dev);
1433
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001434 if (!dev->power.disable_depth++)
1435 __pm_runtime_barrier(dev);
1436
1437 out:
1438 spin_unlock_irq(&dev->power.lock);
1439}
1440EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1441
1442/**
1443 * pm_runtime_enable - Enable runtime PM of a device.
1444 * @dev: Device to handle.
1445 */
1446void pm_runtime_enable(struct device *dev)
1447{
1448 unsigned long flags;
1449
1450 spin_lock_irqsave(&dev->power.lock, flags);
1451
David Brazdil0f672f62019-12-10 10:32:29 +00001452 if (dev->power.disable_depth > 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001453 dev->power.disable_depth--;
David Brazdil0f672f62019-12-10 10:32:29 +00001454
1455 /* About to enable runtime pm, set accounting_timestamp to now */
1456 if (!dev->power.disable_depth)
1457 dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
1458 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001459 dev_warn(dev, "Unbalanced %s!\n", __func__);
David Brazdil0f672f62019-12-10 10:32:29 +00001460 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461
1462 WARN(!dev->power.disable_depth &&
1463 dev->power.runtime_status == RPM_SUSPENDED &&
1464 !dev->power.ignore_children &&
1465 atomic_read(&dev->power.child_count) > 0,
1466 "Enabling runtime PM for inactive device (%s) with active children\n",
1467 dev_name(dev));
1468
1469 spin_unlock_irqrestore(&dev->power.lock, flags);
1470}
1471EXPORT_SYMBOL_GPL(pm_runtime_enable);
1472
1473/**
1474 * pm_runtime_forbid - Block runtime PM of a device.
1475 * @dev: Device to handle.
1476 *
1477 * Increase the device's usage count and clear its power.runtime_auto flag,
1478 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1479 * for it.
1480 */
1481void pm_runtime_forbid(struct device *dev)
1482{
1483 spin_lock_irq(&dev->power.lock);
1484 if (!dev->power.runtime_auto)
1485 goto out;
1486
1487 dev->power.runtime_auto = false;
1488 atomic_inc(&dev->power.usage_count);
1489 rpm_resume(dev, 0);
1490
1491 out:
1492 spin_unlock_irq(&dev->power.lock);
1493}
1494EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1495
1496/**
1497 * pm_runtime_allow - Unblock runtime PM of a device.
1498 * @dev: Device to handle.
1499 *
1500 * Decrease the device's usage count and set its power.runtime_auto flag.
1501 */
1502void pm_runtime_allow(struct device *dev)
1503{
1504 spin_lock_irq(&dev->power.lock);
1505 if (dev->power.runtime_auto)
1506 goto out;
1507
1508 dev->power.runtime_auto = true;
1509 if (atomic_dec_and_test(&dev->power.usage_count))
1510 rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
Olivier Deprez157378f2022-04-04 15:47:50 +02001511 else
1512 trace_rpm_usage_rcuidle(dev, RPM_AUTO | RPM_ASYNC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513
1514 out:
1515 spin_unlock_irq(&dev->power.lock);
1516}
1517EXPORT_SYMBOL_GPL(pm_runtime_allow);
1518
1519/**
1520 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1521 * @dev: Device to handle.
1522 *
1523 * Set the power.no_callbacks flag, which tells the PM core that this
1524 * device is power-managed through its parent and has no runtime PM
1525 * callbacks of its own. The runtime sysfs attributes will be removed.
1526 */
1527void pm_runtime_no_callbacks(struct device *dev)
1528{
1529 spin_lock_irq(&dev->power.lock);
1530 dev->power.no_callbacks = 1;
1531 spin_unlock_irq(&dev->power.lock);
1532 if (device_is_registered(dev))
1533 rpm_sysfs_remove(dev);
1534}
1535EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1536
1537/**
1538 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1539 * @dev: Device to handle
1540 *
1541 * Set the power.irq_safe flag, which tells the PM core that the
1542 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1543 * always be invoked with the spinlock held and interrupts disabled. It also
1544 * causes the parent's usage counter to be permanently incremented, preventing
1545 * the parent from runtime suspending -- otherwise an irq-safe child might have
1546 * to wait for a non-irq-safe parent.
1547 */
1548void pm_runtime_irq_safe(struct device *dev)
1549{
1550 if (dev->parent)
1551 pm_runtime_get_sync(dev->parent);
1552 spin_lock_irq(&dev->power.lock);
1553 dev->power.irq_safe = 1;
1554 spin_unlock_irq(&dev->power.lock);
1555}
1556EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1557
1558/**
1559 * update_autosuspend - Handle a change to a device's autosuspend settings.
1560 * @dev: Device to handle.
1561 * @old_delay: The former autosuspend_delay value.
1562 * @old_use: The former use_autosuspend value.
1563 *
1564 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1565 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1566 *
1567 * This function must be called under dev->power.lock with interrupts disabled.
1568 */
1569static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1570{
1571 int delay = dev->power.autosuspend_delay;
1572
1573 /* Should runtime suspend be prevented now? */
1574 if (dev->power.use_autosuspend && delay < 0) {
1575
1576 /* If it used to be allowed then prevent it. */
1577 if (!old_use || old_delay >= 0) {
1578 atomic_inc(&dev->power.usage_count);
1579 rpm_resume(dev, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +02001580 } else {
1581 trace_rpm_usage_rcuidle(dev, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001582 }
1583 }
1584
1585 /* Runtime suspend should be allowed now. */
1586 else {
1587
1588 /* If it used to be prevented then allow it. */
1589 if (old_use && old_delay < 0)
1590 atomic_dec(&dev->power.usage_count);
1591
1592 /* Maybe we can autosuspend now. */
1593 rpm_idle(dev, RPM_AUTO);
1594 }
1595}
1596
1597/**
1598 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1599 * @dev: Device to handle.
1600 * @delay: Value of the new delay in milliseconds.
1601 *
1602 * Set the device's power.autosuspend_delay value. If it changes to negative
1603 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1604 * changes the other way, allow runtime suspends.
1605 */
1606void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1607{
1608 int old_delay, old_use;
1609
1610 spin_lock_irq(&dev->power.lock);
1611 old_delay = dev->power.autosuspend_delay;
1612 old_use = dev->power.use_autosuspend;
1613 dev->power.autosuspend_delay = delay;
1614 update_autosuspend(dev, old_delay, old_use);
1615 spin_unlock_irq(&dev->power.lock);
1616}
1617EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1618
1619/**
1620 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1621 * @dev: Device to handle.
1622 * @use: New value for use_autosuspend.
1623 *
1624 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1625 * suspends as needed.
1626 */
1627void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1628{
1629 int old_delay, old_use;
1630
1631 spin_lock_irq(&dev->power.lock);
1632 old_delay = dev->power.autosuspend_delay;
1633 old_use = dev->power.use_autosuspend;
1634 dev->power.use_autosuspend = use;
1635 update_autosuspend(dev, old_delay, old_use);
1636 spin_unlock_irq(&dev->power.lock);
1637}
1638EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1639
1640/**
1641 * pm_runtime_init - Initialize runtime PM fields in given device object.
1642 * @dev: Device object to initialize.
1643 */
1644void pm_runtime_init(struct device *dev)
1645{
1646 dev->power.runtime_status = RPM_SUSPENDED;
1647 dev->power.idle_notification = false;
1648
1649 dev->power.disable_depth = 1;
1650 atomic_set(&dev->power.usage_count, 0);
1651
1652 dev->power.runtime_error = 0;
1653
1654 atomic_set(&dev->power.child_count, 0);
1655 pm_suspend_ignore_children(dev, false);
1656 dev->power.runtime_auto = true;
1657
1658 dev->power.request_pending = false;
1659 dev->power.request = RPM_REQ_NONE;
1660 dev->power.deferred_resume = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02001661 dev->power.needs_force_resume = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001662 INIT_WORK(&dev->power.work, pm_runtime_work);
1663
1664 dev->power.timer_expires = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001665 hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1666 dev->power.suspend_timer.function = pm_suspend_timer_fn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001667
1668 init_waitqueue_head(&dev->power.wait_queue);
1669}
1670
1671/**
1672 * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1673 * @dev: Device object to re-initialize.
1674 */
1675void pm_runtime_reinit(struct device *dev)
1676{
1677 if (!pm_runtime_enabled(dev)) {
1678 if (dev->power.runtime_status == RPM_ACTIVE)
1679 pm_runtime_set_suspended(dev);
1680 if (dev->power.irq_safe) {
1681 spin_lock_irq(&dev->power.lock);
1682 dev->power.irq_safe = 0;
1683 spin_unlock_irq(&dev->power.lock);
1684 if (dev->parent)
1685 pm_runtime_put(dev->parent);
1686 }
1687 }
1688}
1689
1690/**
1691 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1692 * @dev: Device object being removed from device hierarchy.
1693 */
1694void pm_runtime_remove(struct device *dev)
1695{
1696 __pm_runtime_disable(dev, false);
1697 pm_runtime_reinit(dev);
1698}
1699
1700/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001701 * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1702 * @dev: Consumer device.
1703 */
1704void pm_runtime_get_suppliers(struct device *dev)
1705{
1706 struct device_link *link;
1707 int idx;
1708
1709 idx = device_links_read_lock();
1710
David Brazdil0f672f62019-12-10 10:32:29 +00001711 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1712 device_links_read_lock_held())
1713 if (link->flags & DL_FLAG_PM_RUNTIME) {
1714 link->supplier_preactivated = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001715 pm_runtime_get_sync(link->supplier);
Olivier Deprez0e641232021-09-23 10:07:05 +02001716 refcount_inc(&link->rpm_active);
David Brazdil0f672f62019-12-10 10:32:29 +00001717 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001718
1719 device_links_read_unlock(idx);
1720}
1721
1722/**
1723 * pm_runtime_put_suppliers - Drop references to supplier devices.
1724 * @dev: Consumer device.
1725 */
1726void pm_runtime_put_suppliers(struct device *dev)
1727{
1728 struct device_link *link;
Olivier Deprez0e641232021-09-23 10:07:05 +02001729 unsigned long flags;
1730 bool put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001731 int idx;
1732
1733 idx = device_links_read_lock();
1734
David Brazdil0f672f62019-12-10 10:32:29 +00001735 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1736 device_links_read_lock_held())
1737 if (link->supplier_preactivated) {
1738 link->supplier_preactivated = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02001739 spin_lock_irqsave(&dev->power.lock, flags);
1740 put = pm_runtime_status_suspended(dev) &&
1741 refcount_dec_not_one(&link->rpm_active);
1742 spin_unlock_irqrestore(&dev->power.lock, flags);
1743 if (put)
David Brazdil0f672f62019-12-10 10:32:29 +00001744 pm_runtime_put(link->supplier);
1745 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001746
1747 device_links_read_unlock(idx);
1748}
1749
1750void pm_runtime_new_link(struct device *dev)
1751{
1752 spin_lock_irq(&dev->power.lock);
1753 dev->power.links_count++;
1754 spin_unlock_irq(&dev->power.lock);
1755}
1756
Olivier Deprez0e641232021-09-23 10:07:05 +02001757static void pm_runtime_drop_link_count(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001758{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001759 spin_lock_irq(&dev->power.lock);
1760 WARN_ON(dev->power.links_count == 0);
1761 dev->power.links_count--;
1762 spin_unlock_irq(&dev->power.lock);
1763}
1764
Olivier Deprez0e641232021-09-23 10:07:05 +02001765/**
1766 * pm_runtime_drop_link - Prepare for device link removal.
1767 * @link: Device link going away.
1768 *
1769 * Drop the link count of the consumer end of @link and decrement the supplier
1770 * device's runtime PM usage counter as many times as needed to drop all of the
1771 * PM runtime reference to it from the consumer.
1772 */
1773void pm_runtime_drop_link(struct device_link *link)
1774{
1775 if (!(link->flags & DL_FLAG_PM_RUNTIME))
1776 return;
1777
1778 pm_runtime_drop_link_count(link->consumer);
Olivier Deprez157378f2022-04-04 15:47:50 +02001779 pm_runtime_release_supplier(link, true);
Olivier Deprez0e641232021-09-23 10:07:05 +02001780}
1781
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001782static bool pm_runtime_need_not_resume(struct device *dev)
1783{
1784 return atomic_read(&dev->power.usage_count) <= 1 &&
1785 (atomic_read(&dev->power.child_count) == 0 ||
1786 dev->power.ignore_children);
1787}
1788
1789/**
1790 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1791 * @dev: Device to suspend.
1792 *
1793 * Disable runtime PM so we safely can check the device's runtime PM status and
1794 * if it is active, invoke its ->runtime_suspend callback to suspend it and
1795 * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's
1796 * usage and children counters don't indicate that the device was in use before
1797 * the system-wide transition under way, decrement its parent's children counter
1798 * (if there is a parent). Keep runtime PM disabled to preserve the state
1799 * unless we encounter errors.
1800 *
1801 * Typically this function may be invoked from a system suspend callback to make
1802 * sure the device is put into low power state and it should only be used during
1803 * system-wide PM transitions to sleep states. It assumes that the analogous
1804 * pm_runtime_force_resume() will be used to resume the device.
1805 */
1806int pm_runtime_force_suspend(struct device *dev)
1807{
1808 int (*callback)(struct device *);
1809 int ret;
1810
1811 pm_runtime_disable(dev);
1812 if (pm_runtime_status_suspended(dev))
1813 return 0;
1814
1815 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1816
1817 ret = callback ? callback(dev) : 0;
1818 if (ret)
1819 goto err;
1820
1821 /*
1822 * If the device can stay in suspend after the system-wide transition
1823 * to the working state that will follow, drop the children counter of
1824 * its parent, but set its status to RPM_SUSPENDED anyway in case this
1825 * function will be called again for it in the meantime.
1826 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001827 if (pm_runtime_need_not_resume(dev)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001828 pm_runtime_set_suspended(dev);
Olivier Deprez0e641232021-09-23 10:07:05 +02001829 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001830 __update_runtime_status(dev, RPM_SUSPENDED);
Olivier Deprez0e641232021-09-23 10:07:05 +02001831 dev->power.needs_force_resume = 1;
1832 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001833
1834 return 0;
1835
1836err:
1837 pm_runtime_enable(dev);
1838 return ret;
1839}
1840EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1841
1842/**
1843 * pm_runtime_force_resume - Force a device into resume state if needed.
1844 * @dev: Device to resume.
1845 *
1846 * Prior invoking this function we expect the user to have brought the device
1847 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1848 * those actions and bring the device into full power, if it is expected to be
1849 * used on system resume. In the other case, we defer the resume to be managed
1850 * via runtime PM.
1851 *
1852 * Typically this function may be invoked from a system resume callback.
1853 */
1854int pm_runtime_force_resume(struct device *dev)
1855{
1856 int (*callback)(struct device *);
1857 int ret = 0;
1858
Olivier Deprez0e641232021-09-23 10:07:05 +02001859 if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860 goto out;
1861
1862 /*
1863 * The value of the parent's children counter is correct already, so
1864 * just update the status of the device.
1865 */
1866 __update_runtime_status(dev, RPM_ACTIVE);
1867
1868 callback = RPM_GET_CALLBACK(dev, runtime_resume);
1869
1870 ret = callback ? callback(dev) : 0;
1871 if (ret) {
1872 pm_runtime_set_suspended(dev);
1873 goto out;
1874 }
1875
1876 pm_runtime_mark_last_busy(dev);
1877out:
Olivier Deprez0e641232021-09-23 10:07:05 +02001878 dev->power.needs_force_resume = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001879 pm_runtime_enable(dev);
1880 return ret;
1881}
1882EXPORT_SYMBOL_GPL(pm_runtime_force_resume);